bond_main.c 136 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276
  1. /*
  2. * originally based on the dummy device.
  3. *
  4. * Copyright 1999, Thomas Davis, tadavis@lbl.gov.
  5. * Licensed under the GPL. Based on dummy.c, and eql.c devices.
  6. *
  7. * bonding.c: an Ethernet Bonding driver
  8. *
  9. * This is useful to talk to a Cisco EtherChannel compatible equipment:
  10. * Cisco 5500
  11. * Sun Trunking (Solaris)
  12. * Alteon AceDirector Trunks
  13. * Linux Bonding
  14. * and probably many L2 switches ...
  15. *
  16. * How it works:
  17. * ifconfig bond0 ipaddress netmask up
  18. * will setup a network device, with an ip address. No mac address
  19. * will be assigned at this time. The hw mac address will come from
  20. * the first slave bonded to the channel. All slaves will then use
  21. * this hw mac address.
  22. *
  23. * ifconfig bond0 down
  24. * will release all slaves, marking them as down.
  25. *
  26. * ifenslave bond0 eth0
  27. * will attach eth0 to bond0 as a slave. eth0 hw mac address will either
  28. * a: be used as initial mac address
  29. * b: if a hw mac address already is there, eth0's hw mac address
  30. * will then be set from bond0.
  31. *
  32. */
  33. #include <linux/kernel.h>
  34. #include <linux/module.h>
  35. #include <linux/types.h>
  36. #include <linux/fcntl.h>
  37. #include <linux/interrupt.h>
  38. #include <linux/ptrace.h>
  39. #include <linux/ioport.h>
  40. #include <linux/in.h>
  41. #include <net/ip.h>
  42. #include <linux/ip.h>
  43. #include <linux/tcp.h>
  44. #include <linux/udp.h>
  45. #include <linux/slab.h>
  46. #include <linux/string.h>
  47. #include <linux/init.h>
  48. #include <linux/timer.h>
  49. #include <linux/socket.h>
  50. #include <linux/ctype.h>
  51. #include <linux/inet.h>
  52. #include <linux/bitops.h>
  53. #include <asm/system.h>
  54. #include <asm/io.h>
  55. #include <asm/dma.h>
  56. #include <asm/uaccess.h>
  57. #include <linux/errno.h>
  58. #include <linux/netdevice.h>
  59. #include <linux/inetdevice.h>
  60. #include <linux/igmp.h>
  61. #include <linux/etherdevice.h>
  62. #include <linux/skbuff.h>
  63. #include <net/sock.h>
  64. #include <linux/rtnetlink.h>
  65. #include <linux/proc_fs.h>
  66. #include <linux/seq_file.h>
  67. #include <linux/smp.h>
  68. #include <linux/if_ether.h>
  69. #include <net/arp.h>
  70. #include <linux/mii.h>
  71. #include <linux/ethtool.h>
  72. #include <linux/if_vlan.h>
  73. #include <linux/if_bonding.h>
  74. #include <linux/jiffies.h>
  75. #include <net/route.h>
  76. #include <net/net_namespace.h>
  77. #include "bonding.h"
  78. #include "bond_3ad.h"
  79. #include "bond_alb.h"
  80. /*---------------------------- Module parameters ----------------------------*/
  81. /* monitor all links that often (in milliseconds). <=0 disables monitoring */
  82. #define BOND_LINK_MON_INTERV 0
  83. #define BOND_LINK_ARP_INTERV 0
  84. static int max_bonds = BOND_DEFAULT_MAX_BONDS;
  85. static int num_grat_arp = 1;
  86. static int num_unsol_na = 1;
  87. static int miimon = BOND_LINK_MON_INTERV;
  88. static int updelay = 0;
  89. static int downdelay = 0;
  90. static int use_carrier = 1;
  91. static char *mode = NULL;
  92. static char *primary = NULL;
  93. static char *lacp_rate = NULL;
  94. static char *ad_select = NULL;
  95. static char *xmit_hash_policy = NULL;
  96. static int arp_interval = BOND_LINK_ARP_INTERV;
  97. static char *arp_ip_target[BOND_MAX_ARP_TARGETS] = { NULL, };
  98. static char *arp_validate = NULL;
  99. static char *fail_over_mac = NULL;
  100. struct bond_params bonding_defaults;
  101. module_param(max_bonds, int, 0);
  102. MODULE_PARM_DESC(max_bonds, "Max number of bonded devices");
  103. module_param(num_grat_arp, int, 0644);
  104. MODULE_PARM_DESC(num_grat_arp, "Number of gratuitous ARP packets to send on failover event");
  105. module_param(num_unsol_na, int, 0644);
  106. MODULE_PARM_DESC(num_unsol_na, "Number of unsolicited IPv6 Neighbor Advertisements packets to send on failover event");
  107. module_param(miimon, int, 0);
  108. MODULE_PARM_DESC(miimon, "Link check interval in milliseconds");
  109. module_param(updelay, int, 0);
  110. MODULE_PARM_DESC(updelay, "Delay before considering link up, in milliseconds");
  111. module_param(downdelay, int, 0);
  112. MODULE_PARM_DESC(downdelay, "Delay before considering link down, "
  113. "in milliseconds");
  114. module_param(use_carrier, int, 0);
  115. MODULE_PARM_DESC(use_carrier, "Use netif_carrier_ok (vs MII ioctls) in miimon; "
  116. "0 for off, 1 for on (default)");
  117. module_param(mode, charp, 0);
  118. MODULE_PARM_DESC(mode, "Mode of operation : 0 for balance-rr, "
  119. "1 for active-backup, 2 for balance-xor, "
  120. "3 for broadcast, 4 for 802.3ad, 5 for balance-tlb, "
  121. "6 for balance-alb");
  122. module_param(primary, charp, 0);
  123. MODULE_PARM_DESC(primary, "Primary network device to use");
  124. module_param(lacp_rate, charp, 0);
  125. MODULE_PARM_DESC(lacp_rate, "LACPDU tx rate to request from 802.3ad partner "
  126. "(slow/fast)");
  127. module_param(ad_select, charp, 0);
  128. MODULE_PARM_DESC(ad_select, "803.ad aggregation selection logic: stable (0, default), bandwidth (1), count (2)");
  129. module_param(xmit_hash_policy, charp, 0);
  130. MODULE_PARM_DESC(xmit_hash_policy, "XOR hashing method: 0 for layer 2 (default)"
  131. ", 1 for layer 3+4");
  132. module_param(arp_interval, int, 0);
  133. MODULE_PARM_DESC(arp_interval, "arp interval in milliseconds");
  134. module_param_array(arp_ip_target, charp, NULL, 0);
  135. MODULE_PARM_DESC(arp_ip_target, "arp targets in n.n.n.n form");
  136. module_param(arp_validate, charp, 0);
  137. MODULE_PARM_DESC(arp_validate, "validate src/dst of ARP probes: none (default), active, backup or all");
  138. module_param(fail_over_mac, charp, 0);
  139. MODULE_PARM_DESC(fail_over_mac, "For active-backup, do not set all slaves to the same MAC. none (default), active or follow");
  140. /*----------------------------- Global variables ----------------------------*/
  141. static const char * const version =
  142. DRV_DESCRIPTION ": v" DRV_VERSION " (" DRV_RELDATE ")\n";
  143. LIST_HEAD(bond_dev_list);
  144. #ifdef CONFIG_PROC_FS
  145. static struct proc_dir_entry *bond_proc_dir = NULL;
  146. #endif
  147. static __be32 arp_target[BOND_MAX_ARP_TARGETS] = { 0, } ;
  148. static int arp_ip_count = 0;
  149. static int bond_mode = BOND_MODE_ROUNDROBIN;
  150. static int xmit_hashtype= BOND_XMIT_POLICY_LAYER2;
  151. static int lacp_fast = 0;
  152. const struct bond_parm_tbl bond_lacp_tbl[] = {
  153. { "slow", AD_LACP_SLOW},
  154. { "fast", AD_LACP_FAST},
  155. { NULL, -1},
  156. };
  157. const struct bond_parm_tbl bond_mode_tbl[] = {
  158. { "balance-rr", BOND_MODE_ROUNDROBIN},
  159. { "active-backup", BOND_MODE_ACTIVEBACKUP},
  160. { "balance-xor", BOND_MODE_XOR},
  161. { "broadcast", BOND_MODE_BROADCAST},
  162. { "802.3ad", BOND_MODE_8023AD},
  163. { "balance-tlb", BOND_MODE_TLB},
  164. { "balance-alb", BOND_MODE_ALB},
  165. { NULL, -1},
  166. };
  167. const struct bond_parm_tbl xmit_hashtype_tbl[] = {
  168. { "layer2", BOND_XMIT_POLICY_LAYER2},
  169. { "layer3+4", BOND_XMIT_POLICY_LAYER34},
  170. { "layer2+3", BOND_XMIT_POLICY_LAYER23},
  171. { NULL, -1},
  172. };
  173. const struct bond_parm_tbl arp_validate_tbl[] = {
  174. { "none", BOND_ARP_VALIDATE_NONE},
  175. { "active", BOND_ARP_VALIDATE_ACTIVE},
  176. { "backup", BOND_ARP_VALIDATE_BACKUP},
  177. { "all", BOND_ARP_VALIDATE_ALL},
  178. { NULL, -1},
  179. };
  180. const struct bond_parm_tbl fail_over_mac_tbl[] = {
  181. { "none", BOND_FOM_NONE},
  182. { "active", BOND_FOM_ACTIVE},
  183. { "follow", BOND_FOM_FOLLOW},
  184. { NULL, -1},
  185. };
  186. struct bond_parm_tbl ad_select_tbl[] = {
  187. { "stable", BOND_AD_STABLE},
  188. { "bandwidth", BOND_AD_BANDWIDTH},
  189. { "count", BOND_AD_COUNT},
  190. { NULL, -1},
  191. };
  192. /*-------------------------- Forward declarations ---------------------------*/
  193. static void bond_send_gratuitous_arp(struct bonding *bond);
  194. static void bond_deinit(struct net_device *bond_dev);
  195. /*---------------------------- General routines -----------------------------*/
  196. static const char *bond_mode_name(int mode)
  197. {
  198. static const char *names[] = {
  199. [BOND_MODE_ROUNDROBIN] = "load balancing (round-robin)",
  200. [BOND_MODE_ACTIVEBACKUP] = "fault-tolerance (active-backup)",
  201. [BOND_MODE_XOR] = "load balancing (xor)",
  202. [BOND_MODE_BROADCAST] = "fault-tolerance (broadcast)",
  203. [BOND_MODE_8023AD]= "IEEE 802.3ad Dynamic link aggregation",
  204. [BOND_MODE_TLB] = "transmit load balancing",
  205. [BOND_MODE_ALB] = "adaptive load balancing",
  206. };
  207. if (mode < 0 || mode > BOND_MODE_ALB)
  208. return "unknown";
  209. return names[mode];
  210. }
  211. /*---------------------------------- VLAN -----------------------------------*/
  212. /**
  213. * bond_add_vlan - add a new vlan id on bond
  214. * @bond: bond that got the notification
  215. * @vlan_id: the vlan id to add
  216. *
  217. * Returns -ENOMEM if allocation failed.
  218. */
  219. static int bond_add_vlan(struct bonding *bond, unsigned short vlan_id)
  220. {
  221. struct vlan_entry *vlan;
  222. pr_debug("bond: %s, vlan id %d\n",
  223. (bond ? bond->dev->name: "None"), vlan_id);
  224. vlan = kzalloc(sizeof(struct vlan_entry), GFP_KERNEL);
  225. if (!vlan) {
  226. return -ENOMEM;
  227. }
  228. INIT_LIST_HEAD(&vlan->vlan_list);
  229. vlan->vlan_id = vlan_id;
  230. write_lock_bh(&bond->lock);
  231. list_add_tail(&vlan->vlan_list, &bond->vlan_list);
  232. write_unlock_bh(&bond->lock);
  233. pr_debug("added VLAN ID %d on bond %s\n", vlan_id, bond->dev->name);
  234. return 0;
  235. }
  236. /**
  237. * bond_del_vlan - delete a vlan id from bond
  238. * @bond: bond that got the notification
  239. * @vlan_id: the vlan id to delete
  240. *
  241. * returns -ENODEV if @vlan_id was not found in @bond.
  242. */
  243. static int bond_del_vlan(struct bonding *bond, unsigned short vlan_id)
  244. {
  245. struct vlan_entry *vlan;
  246. int res = -ENODEV;
  247. pr_debug("bond: %s, vlan id %d\n", bond->dev->name, vlan_id);
  248. write_lock_bh(&bond->lock);
  249. list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
  250. if (vlan->vlan_id == vlan_id) {
  251. list_del(&vlan->vlan_list);
  252. if (bond_is_lb(bond))
  253. bond_alb_clear_vlan(bond, vlan_id);
  254. pr_debug("removed VLAN ID %d from bond %s\n", vlan_id,
  255. bond->dev->name);
  256. kfree(vlan);
  257. if (list_empty(&bond->vlan_list) &&
  258. (bond->slave_cnt == 0)) {
  259. /* Last VLAN removed and no slaves, so
  260. * restore block on adding VLANs. This will
  261. * be removed once new slaves that are not
  262. * VLAN challenged will be added.
  263. */
  264. bond->dev->features |= NETIF_F_VLAN_CHALLENGED;
  265. }
  266. res = 0;
  267. goto out;
  268. }
  269. }
  270. pr_debug("couldn't find VLAN ID %d in bond %s\n", vlan_id,
  271. bond->dev->name);
  272. out:
  273. write_unlock_bh(&bond->lock);
  274. return res;
  275. }
  276. /**
  277. * bond_has_challenged_slaves
  278. * @bond: the bond we're working on
  279. *
  280. * Searches the slave list. Returns 1 if a vlan challenged slave
  281. * was found, 0 otherwise.
  282. *
  283. * Assumes bond->lock is held.
  284. */
  285. static int bond_has_challenged_slaves(struct bonding *bond)
  286. {
  287. struct slave *slave;
  288. int i;
  289. bond_for_each_slave(bond, slave, i) {
  290. if (slave->dev->features & NETIF_F_VLAN_CHALLENGED) {
  291. pr_debug("found VLAN challenged slave - %s\n",
  292. slave->dev->name);
  293. return 1;
  294. }
  295. }
  296. pr_debug("no VLAN challenged slaves found\n");
  297. return 0;
  298. }
  299. /**
  300. * bond_next_vlan - safely skip to the next item in the vlans list.
  301. * @bond: the bond we're working on
  302. * @curr: item we're advancing from
  303. *
  304. * Returns %NULL if list is empty, bond->next_vlan if @curr is %NULL,
  305. * or @curr->next otherwise (even if it is @curr itself again).
  306. *
  307. * Caller must hold bond->lock
  308. */
  309. struct vlan_entry *bond_next_vlan(struct bonding *bond, struct vlan_entry *curr)
  310. {
  311. struct vlan_entry *next, *last;
  312. if (list_empty(&bond->vlan_list)) {
  313. return NULL;
  314. }
  315. if (!curr) {
  316. next = list_entry(bond->vlan_list.next,
  317. struct vlan_entry, vlan_list);
  318. } else {
  319. last = list_entry(bond->vlan_list.prev,
  320. struct vlan_entry, vlan_list);
  321. if (last == curr) {
  322. next = list_entry(bond->vlan_list.next,
  323. struct vlan_entry, vlan_list);
  324. } else {
  325. next = list_entry(curr->vlan_list.next,
  326. struct vlan_entry, vlan_list);
  327. }
  328. }
  329. return next;
  330. }
  331. /**
  332. * bond_dev_queue_xmit - Prepare skb for xmit.
  333. *
  334. * @bond: bond device that got this skb for tx.
  335. * @skb: hw accel VLAN tagged skb to transmit
  336. * @slave_dev: slave that is supposed to xmit this skbuff
  337. *
  338. * When the bond gets an skb to transmit that is
  339. * already hardware accelerated VLAN tagged, and it
  340. * needs to relay this skb to a slave that is not
  341. * hw accel capable, the skb needs to be "unaccelerated",
  342. * i.e. strip the hwaccel tag and re-insert it as part
  343. * of the payload.
  344. */
  345. int bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb, struct net_device *slave_dev)
  346. {
  347. unsigned short uninitialized_var(vlan_id);
  348. if (!list_empty(&bond->vlan_list) &&
  349. !(slave_dev->features & NETIF_F_HW_VLAN_TX) &&
  350. vlan_get_tag(skb, &vlan_id) == 0) {
  351. skb->dev = slave_dev;
  352. skb = vlan_put_tag(skb, vlan_id);
  353. if (!skb) {
  354. /* vlan_put_tag() frees the skb in case of error,
  355. * so return success here so the calling functions
  356. * won't attempt to free is again.
  357. */
  358. return 0;
  359. }
  360. } else {
  361. skb->dev = slave_dev;
  362. }
  363. skb->priority = 1;
  364. dev_queue_xmit(skb);
  365. return 0;
  366. }
  367. /*
  368. * In the following 3 functions, bond_vlan_rx_register(), bond_vlan_rx_add_vid
  369. * and bond_vlan_rx_kill_vid, We don't protect the slave list iteration with a
  370. * lock because:
  371. * a. This operation is performed in IOCTL context,
  372. * b. The operation is protected by the RTNL semaphore in the 8021q code,
  373. * c. Holding a lock with BH disabled while directly calling a base driver
  374. * entry point is generally a BAD idea.
  375. *
  376. * The design of synchronization/protection for this operation in the 8021q
  377. * module is good for one or more VLAN devices over a single physical device
  378. * and cannot be extended for a teaming solution like bonding, so there is a
  379. * potential race condition here where a net device from the vlan group might
  380. * be referenced (either by a base driver or the 8021q code) while it is being
  381. * removed from the system. However, it turns out we're not making matters
  382. * worse, and if it works for regular VLAN usage it will work here too.
  383. */
  384. /**
  385. * bond_vlan_rx_register - Propagates registration to slaves
  386. * @bond_dev: bonding net device that got called
  387. * @grp: vlan group being registered
  388. */
  389. static void bond_vlan_rx_register(struct net_device *bond_dev, struct vlan_group *grp)
  390. {
  391. struct bonding *bond = netdev_priv(bond_dev);
  392. struct slave *slave;
  393. int i;
  394. bond->vlgrp = grp;
  395. bond_for_each_slave(bond, slave, i) {
  396. struct net_device *slave_dev = slave->dev;
  397. const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
  398. if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
  399. slave_ops->ndo_vlan_rx_register) {
  400. slave_ops->ndo_vlan_rx_register(slave_dev, grp);
  401. }
  402. }
  403. }
  404. /**
  405. * bond_vlan_rx_add_vid - Propagates adding an id to slaves
  406. * @bond_dev: bonding net device that got called
  407. * @vid: vlan id being added
  408. */
  409. static void bond_vlan_rx_add_vid(struct net_device *bond_dev, uint16_t vid)
  410. {
  411. struct bonding *bond = netdev_priv(bond_dev);
  412. struct slave *slave;
  413. int i, res;
  414. bond_for_each_slave(bond, slave, i) {
  415. struct net_device *slave_dev = slave->dev;
  416. const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
  417. if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
  418. slave_ops->ndo_vlan_rx_add_vid) {
  419. slave_ops->ndo_vlan_rx_add_vid(slave_dev, vid);
  420. }
  421. }
  422. res = bond_add_vlan(bond, vid);
  423. if (res) {
  424. printk(KERN_ERR DRV_NAME
  425. ": %s: Error: Failed to add vlan id %d\n",
  426. bond_dev->name, vid);
  427. }
  428. }
  429. /**
  430. * bond_vlan_rx_kill_vid - Propagates deleting an id to slaves
  431. * @bond_dev: bonding net device that got called
  432. * @vid: vlan id being removed
  433. */
  434. static void bond_vlan_rx_kill_vid(struct net_device *bond_dev, uint16_t vid)
  435. {
  436. struct bonding *bond = netdev_priv(bond_dev);
  437. struct slave *slave;
  438. struct net_device *vlan_dev;
  439. int i, res;
  440. bond_for_each_slave(bond, slave, i) {
  441. struct net_device *slave_dev = slave->dev;
  442. const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
  443. if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
  444. slave_ops->ndo_vlan_rx_kill_vid) {
  445. /* Save and then restore vlan_dev in the grp array,
  446. * since the slave's driver might clear it.
  447. */
  448. vlan_dev = vlan_group_get_device(bond->vlgrp, vid);
  449. slave_ops->ndo_vlan_rx_kill_vid(slave_dev, vid);
  450. vlan_group_set_device(bond->vlgrp, vid, vlan_dev);
  451. }
  452. }
  453. res = bond_del_vlan(bond, vid);
  454. if (res) {
  455. printk(KERN_ERR DRV_NAME
  456. ": %s: Error: Failed to remove vlan id %d\n",
  457. bond_dev->name, vid);
  458. }
  459. }
  460. static void bond_add_vlans_on_slave(struct bonding *bond, struct net_device *slave_dev)
  461. {
  462. struct vlan_entry *vlan;
  463. const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
  464. write_lock_bh(&bond->lock);
  465. if (list_empty(&bond->vlan_list))
  466. goto out;
  467. if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
  468. slave_ops->ndo_vlan_rx_register)
  469. slave_ops->ndo_vlan_rx_register(slave_dev, bond->vlgrp);
  470. if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
  471. !(slave_ops->ndo_vlan_rx_add_vid))
  472. goto out;
  473. list_for_each_entry(vlan, &bond->vlan_list, vlan_list)
  474. slave_ops->ndo_vlan_rx_add_vid(slave_dev, vlan->vlan_id);
  475. out:
  476. write_unlock_bh(&bond->lock);
  477. }
  478. static void bond_del_vlans_from_slave(struct bonding *bond, struct net_device *slave_dev)
  479. {
  480. const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
  481. struct vlan_entry *vlan;
  482. struct net_device *vlan_dev;
  483. write_lock_bh(&bond->lock);
  484. if (list_empty(&bond->vlan_list))
  485. goto out;
  486. if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
  487. !(slave_ops->ndo_vlan_rx_kill_vid))
  488. goto unreg;
  489. list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
  490. /* Save and then restore vlan_dev in the grp array,
  491. * since the slave's driver might clear it.
  492. */
  493. vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
  494. slave_ops->ndo_vlan_rx_kill_vid(slave_dev, vlan->vlan_id);
  495. vlan_group_set_device(bond->vlgrp, vlan->vlan_id, vlan_dev);
  496. }
  497. unreg:
  498. if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
  499. slave_ops->ndo_vlan_rx_register)
  500. slave_ops->ndo_vlan_rx_register(slave_dev, NULL);
  501. out:
  502. write_unlock_bh(&bond->lock);
  503. }
  504. /*------------------------------- Link status -------------------------------*/
  505. /*
  506. * Set the carrier state for the master according to the state of its
  507. * slaves. If any slaves are up, the master is up. In 802.3ad mode,
  508. * do special 802.3ad magic.
  509. *
  510. * Returns zero if carrier state does not change, nonzero if it does.
  511. */
  512. static int bond_set_carrier(struct bonding *bond)
  513. {
  514. struct slave *slave;
  515. int i;
  516. if (bond->slave_cnt == 0)
  517. goto down;
  518. if (bond->params.mode == BOND_MODE_8023AD)
  519. return bond_3ad_set_carrier(bond);
  520. bond_for_each_slave(bond, slave, i) {
  521. if (slave->link == BOND_LINK_UP) {
  522. if (!netif_carrier_ok(bond->dev)) {
  523. netif_carrier_on(bond->dev);
  524. return 1;
  525. }
  526. return 0;
  527. }
  528. }
  529. down:
  530. if (netif_carrier_ok(bond->dev)) {
  531. netif_carrier_off(bond->dev);
  532. return 1;
  533. }
  534. return 0;
  535. }
  536. /*
  537. * Get link speed and duplex from the slave's base driver
  538. * using ethtool. If for some reason the call fails or the
  539. * values are invalid, fake speed and duplex to 100/Full
  540. * and return error.
  541. */
  542. static int bond_update_speed_duplex(struct slave *slave)
  543. {
  544. struct net_device *slave_dev = slave->dev;
  545. struct ethtool_cmd etool;
  546. int res;
  547. /* Fake speed and duplex */
  548. slave->speed = SPEED_100;
  549. slave->duplex = DUPLEX_FULL;
  550. if (!slave_dev->ethtool_ops || !slave_dev->ethtool_ops->get_settings)
  551. return -1;
  552. res = slave_dev->ethtool_ops->get_settings(slave_dev, &etool);
  553. if (res < 0)
  554. return -1;
  555. switch (etool.speed) {
  556. case SPEED_10:
  557. case SPEED_100:
  558. case SPEED_1000:
  559. case SPEED_10000:
  560. break;
  561. default:
  562. return -1;
  563. }
  564. switch (etool.duplex) {
  565. case DUPLEX_FULL:
  566. case DUPLEX_HALF:
  567. break;
  568. default:
  569. return -1;
  570. }
  571. slave->speed = etool.speed;
  572. slave->duplex = etool.duplex;
  573. return 0;
  574. }
  575. /*
  576. * if <dev> supports MII link status reporting, check its link status.
  577. *
  578. * We either do MII/ETHTOOL ioctls, or check netif_carrier_ok(),
  579. * depening upon the setting of the use_carrier parameter.
  580. *
  581. * Return either BMSR_LSTATUS, meaning that the link is up (or we
  582. * can't tell and just pretend it is), or 0, meaning that the link is
  583. * down.
  584. *
  585. * If reporting is non-zero, instead of faking link up, return -1 if
  586. * both ETHTOOL and MII ioctls fail (meaning the device does not
  587. * support them). If use_carrier is set, return whatever it says.
  588. * It'd be nice if there was a good way to tell if a driver supports
  589. * netif_carrier, but there really isn't.
  590. */
  591. static int bond_check_dev_link(struct bonding *bond, struct net_device *slave_dev, int reporting)
  592. {
  593. const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
  594. static int (* ioctl)(struct net_device *, struct ifreq *, int);
  595. struct ifreq ifr;
  596. struct mii_ioctl_data *mii;
  597. if (bond->params.use_carrier)
  598. return netif_carrier_ok(slave_dev) ? BMSR_LSTATUS : 0;
  599. ioctl = slave_ops->ndo_do_ioctl;
  600. if (ioctl) {
  601. /* TODO: set pointer to correct ioctl on a per team member */
  602. /* bases to make this more efficient. that is, once */
  603. /* we determine the correct ioctl, we will always */
  604. /* call it and not the others for that team */
  605. /* member. */
  606. /*
  607. * We cannot assume that SIOCGMIIPHY will also read a
  608. * register; not all network drivers (e.g., e100)
  609. * support that.
  610. */
  611. /* Yes, the mii is overlaid on the ifreq.ifr_ifru */
  612. strncpy(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
  613. mii = if_mii(&ifr);
  614. if (IOCTL(slave_dev, &ifr, SIOCGMIIPHY) == 0) {
  615. mii->reg_num = MII_BMSR;
  616. if (IOCTL(slave_dev, &ifr, SIOCGMIIREG) == 0) {
  617. return (mii->val_out & BMSR_LSTATUS);
  618. }
  619. }
  620. }
  621. /*
  622. * Some drivers cache ETHTOOL_GLINK for a period of time so we only
  623. * attempt to get link status from it if the above MII ioctls fail.
  624. */
  625. if (slave_dev->ethtool_ops) {
  626. if (slave_dev->ethtool_ops->get_link) {
  627. u32 link;
  628. link = slave_dev->ethtool_ops->get_link(slave_dev);
  629. return link ? BMSR_LSTATUS : 0;
  630. }
  631. }
  632. /*
  633. * If reporting, report that either there's no dev->do_ioctl,
  634. * or both SIOCGMIIREG and get_link failed (meaning that we
  635. * cannot report link status). If not reporting, pretend
  636. * we're ok.
  637. */
  638. return (reporting ? -1 : BMSR_LSTATUS);
  639. }
  640. /*----------------------------- Multicast list ------------------------------*/
  641. /*
  642. * Returns 0 if dmi1 and dmi2 are the same, non-0 otherwise
  643. */
  644. static inline int bond_is_dmi_same(struct dev_mc_list *dmi1, struct dev_mc_list *dmi2)
  645. {
  646. return memcmp(dmi1->dmi_addr, dmi2->dmi_addr, dmi1->dmi_addrlen) == 0 &&
  647. dmi1->dmi_addrlen == dmi2->dmi_addrlen;
  648. }
  649. /*
  650. * returns dmi entry if found, NULL otherwise
  651. */
  652. static struct dev_mc_list *bond_mc_list_find_dmi(struct dev_mc_list *dmi, struct dev_mc_list *mc_list)
  653. {
  654. struct dev_mc_list *idmi;
  655. for (idmi = mc_list; idmi; idmi = idmi->next) {
  656. if (bond_is_dmi_same(dmi, idmi)) {
  657. return idmi;
  658. }
  659. }
  660. return NULL;
  661. }
  662. /*
  663. * Push the promiscuity flag down to appropriate slaves
  664. */
  665. static int bond_set_promiscuity(struct bonding *bond, int inc)
  666. {
  667. int err = 0;
  668. if (USES_PRIMARY(bond->params.mode)) {
  669. /* write lock already acquired */
  670. if (bond->curr_active_slave) {
  671. err = dev_set_promiscuity(bond->curr_active_slave->dev,
  672. inc);
  673. }
  674. } else {
  675. struct slave *slave;
  676. int i;
  677. bond_for_each_slave(bond, slave, i) {
  678. err = dev_set_promiscuity(slave->dev, inc);
  679. if (err)
  680. return err;
  681. }
  682. }
  683. return err;
  684. }
  685. /*
  686. * Push the allmulti flag down to all slaves
  687. */
  688. static int bond_set_allmulti(struct bonding *bond, int inc)
  689. {
  690. int err = 0;
  691. if (USES_PRIMARY(bond->params.mode)) {
  692. /* write lock already acquired */
  693. if (bond->curr_active_slave) {
  694. err = dev_set_allmulti(bond->curr_active_slave->dev,
  695. inc);
  696. }
  697. } else {
  698. struct slave *slave;
  699. int i;
  700. bond_for_each_slave(bond, slave, i) {
  701. err = dev_set_allmulti(slave->dev, inc);
  702. if (err)
  703. return err;
  704. }
  705. }
  706. return err;
  707. }
  708. /*
  709. * Add a Multicast address to slaves
  710. * according to mode
  711. */
  712. static void bond_mc_add(struct bonding *bond, void *addr, int alen)
  713. {
  714. if (USES_PRIMARY(bond->params.mode)) {
  715. /* write lock already acquired */
  716. if (bond->curr_active_slave) {
  717. dev_mc_add(bond->curr_active_slave->dev, addr, alen, 0);
  718. }
  719. } else {
  720. struct slave *slave;
  721. int i;
  722. bond_for_each_slave(bond, slave, i) {
  723. dev_mc_add(slave->dev, addr, alen, 0);
  724. }
  725. }
  726. }
  727. /*
  728. * Remove a multicast address from slave
  729. * according to mode
  730. */
  731. static void bond_mc_delete(struct bonding *bond, void *addr, int alen)
  732. {
  733. if (USES_PRIMARY(bond->params.mode)) {
  734. /* write lock already acquired */
  735. if (bond->curr_active_slave) {
  736. dev_mc_delete(bond->curr_active_slave->dev, addr, alen, 0);
  737. }
  738. } else {
  739. struct slave *slave;
  740. int i;
  741. bond_for_each_slave(bond, slave, i) {
  742. dev_mc_delete(slave->dev, addr, alen, 0);
  743. }
  744. }
  745. }
  746. /*
  747. * Retrieve the list of registered multicast addresses for the bonding
  748. * device and retransmit an IGMP JOIN request to the current active
  749. * slave.
  750. */
  751. static void bond_resend_igmp_join_requests(struct bonding *bond)
  752. {
  753. struct in_device *in_dev;
  754. struct ip_mc_list *im;
  755. rcu_read_lock();
  756. in_dev = __in_dev_get_rcu(bond->dev);
  757. if (in_dev) {
  758. for (im = in_dev->mc_list; im; im = im->next) {
  759. ip_mc_rejoin_group(im);
  760. }
  761. }
  762. rcu_read_unlock();
  763. }
  764. /*
  765. * Totally destroys the mc_list in bond
  766. */
  767. static void bond_mc_list_destroy(struct bonding *bond)
  768. {
  769. struct dev_mc_list *dmi;
  770. dmi = bond->mc_list;
  771. while (dmi) {
  772. bond->mc_list = dmi->next;
  773. kfree(dmi);
  774. dmi = bond->mc_list;
  775. }
  776. bond->mc_list = NULL;
  777. }
  778. /*
  779. * Copy all the Multicast addresses from src to the bonding device dst
  780. */
  781. static int bond_mc_list_copy(struct dev_mc_list *mc_list, struct bonding *bond,
  782. gfp_t gfp_flag)
  783. {
  784. struct dev_mc_list *dmi, *new_dmi;
  785. for (dmi = mc_list; dmi; dmi = dmi->next) {
  786. new_dmi = kmalloc(sizeof(struct dev_mc_list), gfp_flag);
  787. if (!new_dmi) {
  788. /* FIXME: Potential memory leak !!! */
  789. return -ENOMEM;
  790. }
  791. new_dmi->next = bond->mc_list;
  792. bond->mc_list = new_dmi;
  793. new_dmi->dmi_addrlen = dmi->dmi_addrlen;
  794. memcpy(new_dmi->dmi_addr, dmi->dmi_addr, dmi->dmi_addrlen);
  795. new_dmi->dmi_users = dmi->dmi_users;
  796. new_dmi->dmi_gusers = dmi->dmi_gusers;
  797. }
  798. return 0;
  799. }
  800. /*
  801. * flush all members of flush->mc_list from device dev->mc_list
  802. */
  803. static void bond_mc_list_flush(struct net_device *bond_dev, struct net_device *slave_dev)
  804. {
  805. struct bonding *bond = netdev_priv(bond_dev);
  806. struct dev_mc_list *dmi;
  807. for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
  808. dev_mc_delete(slave_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
  809. }
  810. if (bond->params.mode == BOND_MODE_8023AD) {
  811. /* del lacpdu mc addr from mc list */
  812. u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
  813. dev_mc_delete(slave_dev, lacpdu_multicast, ETH_ALEN, 0);
  814. }
  815. }
  816. /*--------------------------- Active slave change ---------------------------*/
  817. /*
  818. * Update the mc list and multicast-related flags for the new and
  819. * old active slaves (if any) according to the multicast mode, and
  820. * promiscuous flags unconditionally.
  821. */
  822. static void bond_mc_swap(struct bonding *bond, struct slave *new_active, struct slave *old_active)
  823. {
  824. struct dev_mc_list *dmi;
  825. if (!USES_PRIMARY(bond->params.mode)) {
  826. /* nothing to do - mc list is already up-to-date on
  827. * all slaves
  828. */
  829. return;
  830. }
  831. if (old_active) {
  832. if (bond->dev->flags & IFF_PROMISC) {
  833. dev_set_promiscuity(old_active->dev, -1);
  834. }
  835. if (bond->dev->flags & IFF_ALLMULTI) {
  836. dev_set_allmulti(old_active->dev, -1);
  837. }
  838. for (dmi = bond->dev->mc_list; dmi; dmi = dmi->next) {
  839. dev_mc_delete(old_active->dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
  840. }
  841. }
  842. if (new_active) {
  843. /* FIXME: Signal errors upstream. */
  844. if (bond->dev->flags & IFF_PROMISC) {
  845. dev_set_promiscuity(new_active->dev, 1);
  846. }
  847. if (bond->dev->flags & IFF_ALLMULTI) {
  848. dev_set_allmulti(new_active->dev, 1);
  849. }
  850. for (dmi = bond->dev->mc_list; dmi; dmi = dmi->next) {
  851. dev_mc_add(new_active->dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
  852. }
  853. bond_resend_igmp_join_requests(bond);
  854. }
  855. }
  856. /*
  857. * bond_do_fail_over_mac
  858. *
  859. * Perform special MAC address swapping for fail_over_mac settings
  860. *
  861. * Called with RTNL, bond->lock for read, curr_slave_lock for write_bh.
  862. */
  863. static void bond_do_fail_over_mac(struct bonding *bond,
  864. struct slave *new_active,
  865. struct slave *old_active)
  866. {
  867. u8 tmp_mac[ETH_ALEN];
  868. struct sockaddr saddr;
  869. int rv;
  870. switch (bond->params.fail_over_mac) {
  871. case BOND_FOM_ACTIVE:
  872. if (new_active)
  873. memcpy(bond->dev->dev_addr, new_active->dev->dev_addr,
  874. new_active->dev->addr_len);
  875. break;
  876. case BOND_FOM_FOLLOW:
  877. /*
  878. * if new_active && old_active, swap them
  879. * if just old_active, do nothing (going to no active slave)
  880. * if just new_active, set new_active to bond's MAC
  881. */
  882. if (!new_active)
  883. return;
  884. write_unlock_bh(&bond->curr_slave_lock);
  885. read_unlock(&bond->lock);
  886. if (old_active) {
  887. memcpy(tmp_mac, new_active->dev->dev_addr, ETH_ALEN);
  888. memcpy(saddr.sa_data, old_active->dev->dev_addr,
  889. ETH_ALEN);
  890. saddr.sa_family = new_active->dev->type;
  891. } else {
  892. memcpy(saddr.sa_data, bond->dev->dev_addr, ETH_ALEN);
  893. saddr.sa_family = bond->dev->type;
  894. }
  895. rv = dev_set_mac_address(new_active->dev, &saddr);
  896. if (rv) {
  897. printk(KERN_ERR DRV_NAME
  898. ": %s: Error %d setting MAC of slave %s\n",
  899. bond->dev->name, -rv, new_active->dev->name);
  900. goto out;
  901. }
  902. if (!old_active)
  903. goto out;
  904. memcpy(saddr.sa_data, tmp_mac, ETH_ALEN);
  905. saddr.sa_family = old_active->dev->type;
  906. rv = dev_set_mac_address(old_active->dev, &saddr);
  907. if (rv)
  908. printk(KERN_ERR DRV_NAME
  909. ": %s: Error %d setting MAC of slave %s\n",
  910. bond->dev->name, -rv, new_active->dev->name);
  911. out:
  912. read_lock(&bond->lock);
  913. write_lock_bh(&bond->curr_slave_lock);
  914. break;
  915. default:
  916. printk(KERN_ERR DRV_NAME
  917. ": %s: bond_do_fail_over_mac impossible: bad policy %d\n",
  918. bond->dev->name, bond->params.fail_over_mac);
  919. break;
  920. }
  921. }
  922. /**
  923. * find_best_interface - select the best available slave to be the active one
  924. * @bond: our bonding struct
  925. *
  926. * Warning: Caller must hold curr_slave_lock for writing.
  927. */
  928. static struct slave *bond_find_best_slave(struct bonding *bond)
  929. {
  930. struct slave *new_active, *old_active;
  931. struct slave *bestslave = NULL;
  932. int mintime = bond->params.updelay;
  933. int i;
  934. new_active = old_active = bond->curr_active_slave;
  935. if (!new_active) { /* there were no active slaves left */
  936. if (bond->slave_cnt > 0) { /* found one slave */
  937. new_active = bond->first_slave;
  938. } else {
  939. return NULL; /* still no slave, return NULL */
  940. }
  941. }
  942. /* first try the primary link; if arping, a link must tx/rx traffic
  943. * before it can be considered the curr_active_slave - also, we would skip
  944. * slaves between the curr_active_slave and primary_slave that may be up
  945. * and able to arp
  946. */
  947. if ((bond->primary_slave) &&
  948. (!bond->params.arp_interval) &&
  949. (IS_UP(bond->primary_slave->dev))) {
  950. new_active = bond->primary_slave;
  951. }
  952. /* remember where to stop iterating over the slaves */
  953. old_active = new_active;
  954. bond_for_each_slave_from(bond, new_active, i, old_active) {
  955. if (IS_UP(new_active->dev)) {
  956. if (new_active->link == BOND_LINK_UP) {
  957. return new_active;
  958. } else if (new_active->link == BOND_LINK_BACK) {
  959. /* link up, but waiting for stabilization */
  960. if (new_active->delay < mintime) {
  961. mintime = new_active->delay;
  962. bestslave = new_active;
  963. }
  964. }
  965. }
  966. }
  967. return bestslave;
  968. }
  969. /**
  970. * change_active_interface - change the active slave into the specified one
  971. * @bond: our bonding struct
  972. * @new: the new slave to make the active one
  973. *
  974. * Set the new slave to the bond's settings and unset them on the old
  975. * curr_active_slave.
  976. * Setting include flags, mc-list, promiscuity, allmulti, etc.
  977. *
  978. * If @new's link state is %BOND_LINK_BACK we'll set it to %BOND_LINK_UP,
  979. * because it is apparently the best available slave we have, even though its
  980. * updelay hasn't timed out yet.
  981. *
  982. * If new_active is not NULL, caller must hold bond->lock for read and
  983. * curr_slave_lock for write_bh.
  984. */
  985. void bond_change_active_slave(struct bonding *bond, struct slave *new_active)
  986. {
  987. struct slave *old_active = bond->curr_active_slave;
  988. if (old_active == new_active) {
  989. return;
  990. }
  991. if (new_active) {
  992. new_active->jiffies = jiffies;
  993. if (new_active->link == BOND_LINK_BACK) {
  994. if (USES_PRIMARY(bond->params.mode)) {
  995. printk(KERN_INFO DRV_NAME
  996. ": %s: making interface %s the new "
  997. "active one %d ms earlier.\n",
  998. bond->dev->name, new_active->dev->name,
  999. (bond->params.updelay - new_active->delay) * bond->params.miimon);
  1000. }
  1001. new_active->delay = 0;
  1002. new_active->link = BOND_LINK_UP;
  1003. if (bond->params.mode == BOND_MODE_8023AD) {
  1004. bond_3ad_handle_link_change(new_active, BOND_LINK_UP);
  1005. }
  1006. if (bond_is_lb(bond))
  1007. bond_alb_handle_link_change(bond, new_active, BOND_LINK_UP);
  1008. } else {
  1009. if (USES_PRIMARY(bond->params.mode)) {
  1010. printk(KERN_INFO DRV_NAME
  1011. ": %s: making interface %s the new "
  1012. "active one.\n",
  1013. bond->dev->name, new_active->dev->name);
  1014. }
  1015. }
  1016. }
  1017. if (USES_PRIMARY(bond->params.mode)) {
  1018. bond_mc_swap(bond, new_active, old_active);
  1019. }
  1020. if (bond_is_lb(bond)) {
  1021. bond_alb_handle_active_change(bond, new_active);
  1022. if (old_active)
  1023. bond_set_slave_inactive_flags(old_active);
  1024. if (new_active)
  1025. bond_set_slave_active_flags(new_active);
  1026. } else {
  1027. bond->curr_active_slave = new_active;
  1028. }
  1029. if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
  1030. if (old_active) {
  1031. bond_set_slave_inactive_flags(old_active);
  1032. }
  1033. if (new_active) {
  1034. bond_set_slave_active_flags(new_active);
  1035. if (bond->params.fail_over_mac)
  1036. bond_do_fail_over_mac(bond, new_active,
  1037. old_active);
  1038. bond->send_grat_arp = bond->params.num_grat_arp;
  1039. bond_send_gratuitous_arp(bond);
  1040. bond->send_unsol_na = bond->params.num_unsol_na;
  1041. bond_send_unsolicited_na(bond);
  1042. write_unlock_bh(&bond->curr_slave_lock);
  1043. read_unlock(&bond->lock);
  1044. netdev_bonding_change(bond->dev);
  1045. read_lock(&bond->lock);
  1046. write_lock_bh(&bond->curr_slave_lock);
  1047. }
  1048. }
  1049. }
  1050. /**
  1051. * bond_select_active_slave - select a new active slave, if needed
  1052. * @bond: our bonding struct
  1053. *
  1054. * This functions shoud be called when one of the following occurs:
  1055. * - The old curr_active_slave has been released or lost its link.
  1056. * - The primary_slave has got its link back.
  1057. * - A slave has got its link back and there's no old curr_active_slave.
  1058. *
  1059. * Caller must hold bond->lock for read and curr_slave_lock for write_bh.
  1060. */
  1061. void bond_select_active_slave(struct bonding *bond)
  1062. {
  1063. struct slave *best_slave;
  1064. int rv;
  1065. best_slave = bond_find_best_slave(bond);
  1066. if (best_slave != bond->curr_active_slave) {
  1067. bond_change_active_slave(bond, best_slave);
  1068. rv = bond_set_carrier(bond);
  1069. if (!rv)
  1070. return;
  1071. if (netif_carrier_ok(bond->dev)) {
  1072. printk(KERN_INFO DRV_NAME
  1073. ": %s: first active interface up!\n",
  1074. bond->dev->name);
  1075. } else {
  1076. printk(KERN_INFO DRV_NAME ": %s: "
  1077. "now running without any active interface !\n",
  1078. bond->dev->name);
  1079. }
  1080. }
  1081. }
  1082. /*--------------------------- slave list handling ---------------------------*/
  1083. /*
  1084. * This function attaches the slave to the end of list.
  1085. *
  1086. * bond->lock held for writing by caller.
  1087. */
  1088. static void bond_attach_slave(struct bonding *bond, struct slave *new_slave)
  1089. {
  1090. if (bond->first_slave == NULL) { /* attaching the first slave */
  1091. new_slave->next = new_slave;
  1092. new_slave->prev = new_slave;
  1093. bond->first_slave = new_slave;
  1094. } else {
  1095. new_slave->next = bond->first_slave;
  1096. new_slave->prev = bond->first_slave->prev;
  1097. new_slave->next->prev = new_slave;
  1098. new_slave->prev->next = new_slave;
  1099. }
  1100. bond->slave_cnt++;
  1101. }
  1102. /*
  1103. * This function detaches the slave from the list.
  1104. * WARNING: no check is made to verify if the slave effectively
  1105. * belongs to <bond>.
  1106. * Nothing is freed on return, structures are just unchained.
  1107. * If any slave pointer in bond was pointing to <slave>,
  1108. * it should be changed by the calling function.
  1109. *
  1110. * bond->lock held for writing by caller.
  1111. */
  1112. static void bond_detach_slave(struct bonding *bond, struct slave *slave)
  1113. {
  1114. if (slave->next) {
  1115. slave->next->prev = slave->prev;
  1116. }
  1117. if (slave->prev) {
  1118. slave->prev->next = slave->next;
  1119. }
  1120. if (bond->first_slave == slave) { /* slave is the first slave */
  1121. if (bond->slave_cnt > 1) { /* there are more slave */
  1122. bond->first_slave = slave->next;
  1123. } else {
  1124. bond->first_slave = NULL; /* slave was the last one */
  1125. }
  1126. }
  1127. slave->next = NULL;
  1128. slave->prev = NULL;
  1129. bond->slave_cnt--;
  1130. }
  1131. /*---------------------------------- IOCTL ----------------------------------*/
  1132. static int bond_sethwaddr(struct net_device *bond_dev,
  1133. struct net_device *slave_dev)
  1134. {
  1135. pr_debug("bond_dev=%p\n", bond_dev);
  1136. pr_debug("slave_dev=%p\n", slave_dev);
  1137. pr_debug("slave_dev->addr_len=%d\n", slave_dev->addr_len);
  1138. memcpy(bond_dev->dev_addr, slave_dev->dev_addr, slave_dev->addr_len);
  1139. return 0;
  1140. }
  1141. #define BOND_VLAN_FEATURES \
  1142. (NETIF_F_VLAN_CHALLENGED | NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_TX | \
  1143. NETIF_F_HW_VLAN_FILTER)
  1144. /*
  1145. * Compute the common dev->feature set available to all slaves. Some
  1146. * feature bits are managed elsewhere, so preserve those feature bits
  1147. * on the master device.
  1148. */
  1149. static int bond_compute_features(struct bonding *bond)
  1150. {
  1151. struct slave *slave;
  1152. struct net_device *bond_dev = bond->dev;
  1153. unsigned long features = bond_dev->features;
  1154. unsigned short max_hard_header_len = max((u16)ETH_HLEN,
  1155. bond_dev->hard_header_len);
  1156. int i;
  1157. features &= ~(NETIF_F_ALL_CSUM | BOND_VLAN_FEATURES);
  1158. features |= NETIF_F_GSO_MASK | NETIF_F_NO_CSUM;
  1159. if (!bond->first_slave)
  1160. goto done;
  1161. features &= ~NETIF_F_ONE_FOR_ALL;
  1162. bond_for_each_slave(bond, slave, i) {
  1163. features = netdev_increment_features(features,
  1164. slave->dev->features,
  1165. NETIF_F_ONE_FOR_ALL);
  1166. if (slave->dev->hard_header_len > max_hard_header_len)
  1167. max_hard_header_len = slave->dev->hard_header_len;
  1168. }
  1169. done:
  1170. features |= (bond_dev->features & BOND_VLAN_FEATURES);
  1171. bond_dev->features = netdev_fix_features(features, NULL);
  1172. bond_dev->hard_header_len = max_hard_header_len;
  1173. return 0;
  1174. }
  1175. static void bond_setup_by_slave(struct net_device *bond_dev,
  1176. struct net_device *slave_dev)
  1177. {
  1178. struct bonding *bond = netdev_priv(bond_dev);
  1179. bond_dev->header_ops = slave_dev->header_ops;
  1180. bond_dev->type = slave_dev->type;
  1181. bond_dev->hard_header_len = slave_dev->hard_header_len;
  1182. bond_dev->addr_len = slave_dev->addr_len;
  1183. memcpy(bond_dev->broadcast, slave_dev->broadcast,
  1184. slave_dev->addr_len);
  1185. bond->setup_by_slave = 1;
  1186. }
  1187. /* enslave device <slave> to bond device <master> */
  1188. int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
  1189. {
  1190. struct bonding *bond = netdev_priv(bond_dev);
  1191. const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
  1192. struct slave *new_slave = NULL;
  1193. struct dev_mc_list *dmi;
  1194. struct sockaddr addr;
  1195. int link_reporting;
  1196. int old_features = bond_dev->features;
  1197. int res = 0;
  1198. if (!bond->params.use_carrier && slave_dev->ethtool_ops == NULL &&
  1199. slave_ops->ndo_do_ioctl == NULL) {
  1200. printk(KERN_WARNING DRV_NAME
  1201. ": %s: Warning: no link monitoring support for %s\n",
  1202. bond_dev->name, slave_dev->name);
  1203. }
  1204. /* bond must be initialized by bond_open() before enslaving */
  1205. if (!(bond_dev->flags & IFF_UP)) {
  1206. printk(KERN_WARNING DRV_NAME
  1207. " %s: master_dev is not up in bond_enslave\n",
  1208. bond_dev->name);
  1209. }
  1210. /* already enslaved */
  1211. if (slave_dev->flags & IFF_SLAVE) {
  1212. pr_debug("Error, Device was already enslaved\n");
  1213. return -EBUSY;
  1214. }
  1215. /* vlan challenged mutual exclusion */
  1216. /* no need to lock since we're protected by rtnl_lock */
  1217. if (slave_dev->features & NETIF_F_VLAN_CHALLENGED) {
  1218. pr_debug("%s: NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
  1219. if (!list_empty(&bond->vlan_list)) {
  1220. printk(KERN_ERR DRV_NAME
  1221. ": %s: Error: cannot enslave VLAN "
  1222. "challenged slave %s on VLAN enabled "
  1223. "bond %s\n", bond_dev->name, slave_dev->name,
  1224. bond_dev->name);
  1225. return -EPERM;
  1226. } else {
  1227. printk(KERN_WARNING DRV_NAME
  1228. ": %s: Warning: enslaved VLAN challenged "
  1229. "slave %s. Adding VLANs will be blocked as "
  1230. "long as %s is part of bond %s\n",
  1231. bond_dev->name, slave_dev->name, slave_dev->name,
  1232. bond_dev->name);
  1233. bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
  1234. }
  1235. } else {
  1236. pr_debug("%s: ! NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
  1237. if (bond->slave_cnt == 0) {
  1238. /* First slave, and it is not VLAN challenged,
  1239. * so remove the block of adding VLANs over the bond.
  1240. */
  1241. bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
  1242. }
  1243. }
  1244. /*
  1245. * Old ifenslave binaries are no longer supported. These can
  1246. * be identified with moderate accurary by the state of the slave:
  1247. * the current ifenslave will set the interface down prior to
  1248. * enslaving it; the old ifenslave will not.
  1249. */
  1250. if ((slave_dev->flags & IFF_UP)) {
  1251. printk(KERN_ERR DRV_NAME ": %s is up. "
  1252. "This may be due to an out of date ifenslave.\n",
  1253. slave_dev->name);
  1254. res = -EPERM;
  1255. goto err_undo_flags;
  1256. }
  1257. /* set bonding device ether type by slave - bonding netdevices are
  1258. * created with ether_setup, so when the slave type is not ARPHRD_ETHER
  1259. * there is a need to override some of the type dependent attribs/funcs.
  1260. *
  1261. * bond ether type mutual exclusion - don't allow slaves of dissimilar
  1262. * ether type (eg ARPHRD_ETHER and ARPHRD_INFINIBAND) share the same bond
  1263. */
  1264. if (bond->slave_cnt == 0) {
  1265. if (slave_dev->type != ARPHRD_ETHER)
  1266. bond_setup_by_slave(bond_dev, slave_dev);
  1267. } else if (bond_dev->type != slave_dev->type) {
  1268. printk(KERN_ERR DRV_NAME ": %s ether type (%d) is different "
  1269. "from other slaves (%d), can not enslave it.\n",
  1270. slave_dev->name,
  1271. slave_dev->type, bond_dev->type);
  1272. res = -EINVAL;
  1273. goto err_undo_flags;
  1274. }
  1275. if (slave_ops->ndo_set_mac_address == NULL) {
  1276. if (bond->slave_cnt == 0) {
  1277. printk(KERN_WARNING DRV_NAME
  1278. ": %s: Warning: The first slave device "
  1279. "specified does not support setting the MAC "
  1280. "address. Setting fail_over_mac to active.",
  1281. bond_dev->name);
  1282. bond->params.fail_over_mac = BOND_FOM_ACTIVE;
  1283. } else if (bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
  1284. printk(KERN_ERR DRV_NAME
  1285. ": %s: Error: The slave device specified "
  1286. "does not support setting the MAC address, "
  1287. "but fail_over_mac is not set to active.\n"
  1288. , bond_dev->name);
  1289. res = -EOPNOTSUPP;
  1290. goto err_undo_flags;
  1291. }
  1292. }
  1293. new_slave = kzalloc(sizeof(struct slave), GFP_KERNEL);
  1294. if (!new_slave) {
  1295. res = -ENOMEM;
  1296. goto err_undo_flags;
  1297. }
  1298. /* save slave's original flags before calling
  1299. * netdev_set_master and dev_open
  1300. */
  1301. new_slave->original_flags = slave_dev->flags;
  1302. /*
  1303. * Save slave's original ("permanent") mac address for modes
  1304. * that need it, and for restoring it upon release, and then
  1305. * set it to the master's address
  1306. */
  1307. memcpy(new_slave->perm_hwaddr, slave_dev->dev_addr, ETH_ALEN);
  1308. if (!bond->params.fail_over_mac) {
  1309. /*
  1310. * Set slave to master's mac address. The application already
  1311. * set the master's mac address to that of the first slave
  1312. */
  1313. memcpy(addr.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
  1314. addr.sa_family = slave_dev->type;
  1315. res = dev_set_mac_address(slave_dev, &addr);
  1316. if (res) {
  1317. pr_debug("Error %d calling set_mac_address\n", res);
  1318. goto err_free;
  1319. }
  1320. }
  1321. res = netdev_set_master(slave_dev, bond_dev);
  1322. if (res) {
  1323. pr_debug("Error %d calling netdev_set_master\n", res);
  1324. goto err_restore_mac;
  1325. }
  1326. /* open the slave since the application closed it */
  1327. res = dev_open(slave_dev);
  1328. if (res) {
  1329. pr_debug("Openning slave %s failed\n", slave_dev->name);
  1330. goto err_unset_master;
  1331. }
  1332. new_slave->dev = slave_dev;
  1333. slave_dev->priv_flags |= IFF_BONDING;
  1334. if (bond_is_lb(bond)) {
  1335. /* bond_alb_init_slave() must be called before all other stages since
  1336. * it might fail and we do not want to have to undo everything
  1337. */
  1338. res = bond_alb_init_slave(bond, new_slave);
  1339. if (res) {
  1340. goto err_close;
  1341. }
  1342. }
  1343. /* If the mode USES_PRIMARY, then the new slave gets the
  1344. * master's promisc (and mc) settings only if it becomes the
  1345. * curr_active_slave, and that is taken care of later when calling
  1346. * bond_change_active()
  1347. */
  1348. if (!USES_PRIMARY(bond->params.mode)) {
  1349. /* set promiscuity level to new slave */
  1350. if (bond_dev->flags & IFF_PROMISC) {
  1351. res = dev_set_promiscuity(slave_dev, 1);
  1352. if (res)
  1353. goto err_close;
  1354. }
  1355. /* set allmulti level to new slave */
  1356. if (bond_dev->flags & IFF_ALLMULTI) {
  1357. res = dev_set_allmulti(slave_dev, 1);
  1358. if (res)
  1359. goto err_close;
  1360. }
  1361. netif_addr_lock_bh(bond_dev);
  1362. /* upload master's mc_list to new slave */
  1363. for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
  1364. dev_mc_add (slave_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
  1365. }
  1366. netif_addr_unlock_bh(bond_dev);
  1367. }
  1368. if (bond->params.mode == BOND_MODE_8023AD) {
  1369. /* add lacpdu mc addr to mc list */
  1370. u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
  1371. dev_mc_add(slave_dev, lacpdu_multicast, ETH_ALEN, 0);
  1372. }
  1373. bond_add_vlans_on_slave(bond, slave_dev);
  1374. write_lock_bh(&bond->lock);
  1375. bond_attach_slave(bond, new_slave);
  1376. new_slave->delay = 0;
  1377. new_slave->link_failure_count = 0;
  1378. bond_compute_features(bond);
  1379. write_unlock_bh(&bond->lock);
  1380. read_lock(&bond->lock);
  1381. new_slave->last_arp_rx = jiffies;
  1382. if (bond->params.miimon && !bond->params.use_carrier) {
  1383. link_reporting = bond_check_dev_link(bond, slave_dev, 1);
  1384. if ((link_reporting == -1) && !bond->params.arp_interval) {
  1385. /*
  1386. * miimon is set but a bonded network driver
  1387. * does not support ETHTOOL/MII and
  1388. * arp_interval is not set. Note: if
  1389. * use_carrier is enabled, we will never go
  1390. * here (because netif_carrier is always
  1391. * supported); thus, we don't need to change
  1392. * the messages for netif_carrier.
  1393. */
  1394. printk(KERN_WARNING DRV_NAME
  1395. ": %s: Warning: MII and ETHTOOL support not "
  1396. "available for interface %s, and "
  1397. "arp_interval/arp_ip_target module parameters "
  1398. "not specified, thus bonding will not detect "
  1399. "link failures! see bonding.txt for details.\n",
  1400. bond_dev->name, slave_dev->name);
  1401. } else if (link_reporting == -1) {
  1402. /* unable get link status using mii/ethtool */
  1403. printk(KERN_WARNING DRV_NAME
  1404. ": %s: Warning: can't get link status from "
  1405. "interface %s; the network driver associated "
  1406. "with this interface does not support MII or "
  1407. "ETHTOOL link status reporting, thus miimon "
  1408. "has no effect on this interface.\n",
  1409. bond_dev->name, slave_dev->name);
  1410. }
  1411. }
  1412. /* check for initial state */
  1413. if (!bond->params.miimon ||
  1414. (bond_check_dev_link(bond, slave_dev, 0) == BMSR_LSTATUS)) {
  1415. if (bond->params.updelay) {
  1416. pr_debug("Initial state of slave_dev is "
  1417. "BOND_LINK_BACK\n");
  1418. new_slave->link = BOND_LINK_BACK;
  1419. new_slave->delay = bond->params.updelay;
  1420. } else {
  1421. pr_debug("Initial state of slave_dev is "
  1422. "BOND_LINK_UP\n");
  1423. new_slave->link = BOND_LINK_UP;
  1424. }
  1425. new_slave->jiffies = jiffies;
  1426. } else {
  1427. pr_debug("Initial state of slave_dev is "
  1428. "BOND_LINK_DOWN\n");
  1429. new_slave->link = BOND_LINK_DOWN;
  1430. }
  1431. if (bond_update_speed_duplex(new_slave) &&
  1432. (new_slave->link != BOND_LINK_DOWN)) {
  1433. printk(KERN_WARNING DRV_NAME
  1434. ": %s: Warning: failed to get speed and duplex from %s, "
  1435. "assumed to be 100Mb/sec and Full.\n",
  1436. bond_dev->name, new_slave->dev->name);
  1437. if (bond->params.mode == BOND_MODE_8023AD) {
  1438. printk(KERN_WARNING DRV_NAME
  1439. ": %s: Warning: Operation of 802.3ad mode requires ETHTOOL "
  1440. "support in base driver for proper aggregator "
  1441. "selection.\n", bond_dev->name);
  1442. }
  1443. }
  1444. if (USES_PRIMARY(bond->params.mode) && bond->params.primary[0]) {
  1445. /* if there is a primary slave, remember it */
  1446. if (strcmp(bond->params.primary, new_slave->dev->name) == 0) {
  1447. bond->primary_slave = new_slave;
  1448. }
  1449. }
  1450. write_lock_bh(&bond->curr_slave_lock);
  1451. switch (bond->params.mode) {
  1452. case BOND_MODE_ACTIVEBACKUP:
  1453. bond_set_slave_inactive_flags(new_slave);
  1454. bond_select_active_slave(bond);
  1455. break;
  1456. case BOND_MODE_8023AD:
  1457. /* in 802.3ad mode, the internal mechanism
  1458. * will activate the slaves in the selected
  1459. * aggregator
  1460. */
  1461. bond_set_slave_inactive_flags(new_slave);
  1462. /* if this is the first slave */
  1463. if (bond->slave_cnt == 1) {
  1464. SLAVE_AD_INFO(new_slave).id = 1;
  1465. /* Initialize AD with the number of times that the AD timer is called in 1 second
  1466. * can be called only after the mac address of the bond is set
  1467. */
  1468. bond_3ad_initialize(bond, 1000/AD_TIMER_INTERVAL,
  1469. bond->params.lacp_fast);
  1470. } else {
  1471. SLAVE_AD_INFO(new_slave).id =
  1472. SLAVE_AD_INFO(new_slave->prev).id + 1;
  1473. }
  1474. bond_3ad_bind_slave(new_slave);
  1475. break;
  1476. case BOND_MODE_TLB:
  1477. case BOND_MODE_ALB:
  1478. new_slave->state = BOND_STATE_ACTIVE;
  1479. bond_set_slave_inactive_flags(new_slave);
  1480. break;
  1481. default:
  1482. pr_debug("This slave is always active in trunk mode\n");
  1483. /* always active in trunk mode */
  1484. new_slave->state = BOND_STATE_ACTIVE;
  1485. /* In trunking mode there is little meaning to curr_active_slave
  1486. * anyway (it holds no special properties of the bond device),
  1487. * so we can change it without calling change_active_interface()
  1488. */
  1489. if (!bond->curr_active_slave) {
  1490. bond->curr_active_slave = new_slave;
  1491. }
  1492. break;
  1493. } /* switch(bond_mode) */
  1494. write_unlock_bh(&bond->curr_slave_lock);
  1495. bond_set_carrier(bond);
  1496. read_unlock(&bond->lock);
  1497. res = bond_create_slave_symlinks(bond_dev, slave_dev);
  1498. if (res)
  1499. goto err_close;
  1500. printk(KERN_INFO DRV_NAME
  1501. ": %s: enslaving %s as a%s interface with a%s link.\n",
  1502. bond_dev->name, slave_dev->name,
  1503. new_slave->state == BOND_STATE_ACTIVE ? "n active" : " backup",
  1504. new_slave->link != BOND_LINK_DOWN ? "n up" : " down");
  1505. /* enslave is successful */
  1506. return 0;
  1507. /* Undo stages on error */
  1508. err_close:
  1509. dev_close(slave_dev);
  1510. err_unset_master:
  1511. netdev_set_master(slave_dev, NULL);
  1512. err_restore_mac:
  1513. if (!bond->params.fail_over_mac) {
  1514. /* XXX TODO - fom follow mode needs to change master's
  1515. * MAC if this slave's MAC is in use by the bond, or at
  1516. * least print a warning.
  1517. */
  1518. memcpy(addr.sa_data, new_slave->perm_hwaddr, ETH_ALEN);
  1519. addr.sa_family = slave_dev->type;
  1520. dev_set_mac_address(slave_dev, &addr);
  1521. }
  1522. err_free:
  1523. kfree(new_slave);
  1524. err_undo_flags:
  1525. bond_dev->features = old_features;
  1526. return res;
  1527. }
  1528. /*
  1529. * Try to release the slave device <slave> from the bond device <master>
  1530. * It is legal to access curr_active_slave without a lock because all the function
  1531. * is write-locked.
  1532. *
  1533. * The rules for slave state should be:
  1534. * for Active/Backup:
  1535. * Active stays on all backups go down
  1536. * for Bonded connections:
  1537. * The first up interface should be left on and all others downed.
  1538. */
  1539. int bond_release(struct net_device *bond_dev, struct net_device *slave_dev)
  1540. {
  1541. struct bonding *bond = netdev_priv(bond_dev);
  1542. struct slave *slave, *oldcurrent;
  1543. struct sockaddr addr;
  1544. int mac_addr_differ;
  1545. /* slave is not a slave or master is not master of this slave */
  1546. if (!(slave_dev->flags & IFF_SLAVE) ||
  1547. (slave_dev->master != bond_dev)) {
  1548. printk(KERN_ERR DRV_NAME
  1549. ": %s: Error: cannot release %s.\n",
  1550. bond_dev->name, slave_dev->name);
  1551. return -EINVAL;
  1552. }
  1553. write_lock_bh(&bond->lock);
  1554. slave = bond_get_slave_by_dev(bond, slave_dev);
  1555. if (!slave) {
  1556. /* not a slave of this bond */
  1557. printk(KERN_INFO DRV_NAME
  1558. ": %s: %s not enslaved\n",
  1559. bond_dev->name, slave_dev->name);
  1560. write_unlock_bh(&bond->lock);
  1561. return -EINVAL;
  1562. }
  1563. if (!bond->params.fail_over_mac) {
  1564. mac_addr_differ = memcmp(bond_dev->dev_addr, slave->perm_hwaddr,
  1565. ETH_ALEN);
  1566. if (!mac_addr_differ && (bond->slave_cnt > 1))
  1567. printk(KERN_WARNING DRV_NAME
  1568. ": %s: Warning: the permanent HWaddr of %s - "
  1569. "%pM - is still in use by %s. "
  1570. "Set the HWaddr of %s to a different address "
  1571. "to avoid conflicts.\n",
  1572. bond_dev->name, slave_dev->name,
  1573. slave->perm_hwaddr,
  1574. bond_dev->name, slave_dev->name);
  1575. }
  1576. /* Inform AD package of unbinding of slave. */
  1577. if (bond->params.mode == BOND_MODE_8023AD) {
  1578. /* must be called before the slave is
  1579. * detached from the list
  1580. */
  1581. bond_3ad_unbind_slave(slave);
  1582. }
  1583. printk(KERN_INFO DRV_NAME
  1584. ": %s: releasing %s interface %s\n",
  1585. bond_dev->name,
  1586. (slave->state == BOND_STATE_ACTIVE)
  1587. ? "active" : "backup",
  1588. slave_dev->name);
  1589. oldcurrent = bond->curr_active_slave;
  1590. bond->current_arp_slave = NULL;
  1591. /* release the slave from its bond */
  1592. bond_detach_slave(bond, slave);
  1593. bond_compute_features(bond);
  1594. if (bond->primary_slave == slave) {
  1595. bond->primary_slave = NULL;
  1596. }
  1597. if (oldcurrent == slave) {
  1598. bond_change_active_slave(bond, NULL);
  1599. }
  1600. if (bond_is_lb(bond)) {
  1601. /* Must be called only after the slave has been
  1602. * detached from the list and the curr_active_slave
  1603. * has been cleared (if our_slave == old_current),
  1604. * but before a new active slave is selected.
  1605. */
  1606. write_unlock_bh(&bond->lock);
  1607. bond_alb_deinit_slave(bond, slave);
  1608. write_lock_bh(&bond->lock);
  1609. }
  1610. if (oldcurrent == slave) {
  1611. /*
  1612. * Note that we hold RTNL over this sequence, so there
  1613. * is no concern that another slave add/remove event
  1614. * will interfere.
  1615. */
  1616. write_unlock_bh(&bond->lock);
  1617. read_lock(&bond->lock);
  1618. write_lock_bh(&bond->curr_slave_lock);
  1619. bond_select_active_slave(bond);
  1620. write_unlock_bh(&bond->curr_slave_lock);
  1621. read_unlock(&bond->lock);
  1622. write_lock_bh(&bond->lock);
  1623. }
  1624. if (bond->slave_cnt == 0) {
  1625. bond_set_carrier(bond);
  1626. /* if the last slave was removed, zero the mac address
  1627. * of the master so it will be set by the application
  1628. * to the mac address of the first slave
  1629. */
  1630. memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
  1631. if (list_empty(&bond->vlan_list)) {
  1632. bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
  1633. } else {
  1634. printk(KERN_WARNING DRV_NAME
  1635. ": %s: Warning: clearing HW address of %s while it "
  1636. "still has VLANs.\n",
  1637. bond_dev->name, bond_dev->name);
  1638. printk(KERN_WARNING DRV_NAME
  1639. ": %s: When re-adding slaves, make sure the bond's "
  1640. "HW address matches its VLANs'.\n",
  1641. bond_dev->name);
  1642. }
  1643. } else if ((bond_dev->features & NETIF_F_VLAN_CHALLENGED) &&
  1644. !bond_has_challenged_slaves(bond)) {
  1645. printk(KERN_INFO DRV_NAME
  1646. ": %s: last VLAN challenged slave %s "
  1647. "left bond %s. VLAN blocking is removed\n",
  1648. bond_dev->name, slave_dev->name, bond_dev->name);
  1649. bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
  1650. }
  1651. write_unlock_bh(&bond->lock);
  1652. /* must do this from outside any spinlocks */
  1653. bond_destroy_slave_symlinks(bond_dev, slave_dev);
  1654. bond_del_vlans_from_slave(bond, slave_dev);
  1655. /* If the mode USES_PRIMARY, then we should only remove its
  1656. * promisc and mc settings if it was the curr_active_slave, but that was
  1657. * already taken care of above when we detached the slave
  1658. */
  1659. if (!USES_PRIMARY(bond->params.mode)) {
  1660. /* unset promiscuity level from slave */
  1661. if (bond_dev->flags & IFF_PROMISC) {
  1662. dev_set_promiscuity(slave_dev, -1);
  1663. }
  1664. /* unset allmulti level from slave */
  1665. if (bond_dev->flags & IFF_ALLMULTI) {
  1666. dev_set_allmulti(slave_dev, -1);
  1667. }
  1668. /* flush master's mc_list from slave */
  1669. netif_addr_lock_bh(bond_dev);
  1670. bond_mc_list_flush(bond_dev, slave_dev);
  1671. netif_addr_unlock_bh(bond_dev);
  1672. }
  1673. netdev_set_master(slave_dev, NULL);
  1674. /* close slave before restoring its mac address */
  1675. dev_close(slave_dev);
  1676. if (bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
  1677. /* restore original ("permanent") mac address */
  1678. memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
  1679. addr.sa_family = slave_dev->type;
  1680. dev_set_mac_address(slave_dev, &addr);
  1681. }
  1682. slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
  1683. IFF_SLAVE_INACTIVE | IFF_BONDING |
  1684. IFF_SLAVE_NEEDARP);
  1685. kfree(slave);
  1686. return 0; /* deletion OK */
  1687. }
  1688. /*
  1689. * Destroy a bonding device.
  1690. * Must be under rtnl_lock when this function is called.
  1691. */
  1692. void bond_destroy(struct bonding *bond)
  1693. {
  1694. bond_deinit(bond->dev);
  1695. bond_destroy_sysfs_entry(bond);
  1696. unregister_netdevice(bond->dev);
  1697. }
  1698. static void bond_destructor(struct net_device *bond_dev)
  1699. {
  1700. struct bonding *bond = netdev_priv(bond_dev);
  1701. if (bond->wq)
  1702. destroy_workqueue(bond->wq);
  1703. netif_addr_lock_bh(bond_dev);
  1704. bond_mc_list_destroy(bond);
  1705. netif_addr_unlock_bh(bond_dev);
  1706. free_netdev(bond_dev);
  1707. }
  1708. /*
  1709. * First release a slave and than destroy the bond if no more slaves iare left.
  1710. * Must be under rtnl_lock when this function is called.
  1711. */
  1712. int bond_release_and_destroy(struct net_device *bond_dev, struct net_device *slave_dev)
  1713. {
  1714. struct bonding *bond = netdev_priv(bond_dev);
  1715. int ret;
  1716. ret = bond_release(bond_dev, slave_dev);
  1717. if ((ret == 0) && (bond->slave_cnt == 0)) {
  1718. printk(KERN_INFO DRV_NAME ": %s: destroying bond %s.\n",
  1719. bond_dev->name, bond_dev->name);
  1720. bond_destroy(bond);
  1721. }
  1722. return ret;
  1723. }
  1724. /*
  1725. * This function releases all slaves.
  1726. */
  1727. static int bond_release_all(struct net_device *bond_dev)
  1728. {
  1729. struct bonding *bond = netdev_priv(bond_dev);
  1730. struct slave *slave;
  1731. struct net_device *slave_dev;
  1732. struct sockaddr addr;
  1733. write_lock_bh(&bond->lock);
  1734. netif_carrier_off(bond_dev);
  1735. if (bond->slave_cnt == 0) {
  1736. goto out;
  1737. }
  1738. bond->current_arp_slave = NULL;
  1739. bond->primary_slave = NULL;
  1740. bond_change_active_slave(bond, NULL);
  1741. while ((slave = bond->first_slave) != NULL) {
  1742. /* Inform AD package of unbinding of slave
  1743. * before slave is detached from the list.
  1744. */
  1745. if (bond->params.mode == BOND_MODE_8023AD) {
  1746. bond_3ad_unbind_slave(slave);
  1747. }
  1748. slave_dev = slave->dev;
  1749. bond_detach_slave(bond, slave);
  1750. /* now that the slave is detached, unlock and perform
  1751. * all the undo steps that should not be called from
  1752. * within a lock.
  1753. */
  1754. write_unlock_bh(&bond->lock);
  1755. if (bond_is_lb(bond)) {
  1756. /* must be called only after the slave
  1757. * has been detached from the list
  1758. */
  1759. bond_alb_deinit_slave(bond, slave);
  1760. }
  1761. bond_compute_features(bond);
  1762. bond_destroy_slave_symlinks(bond_dev, slave_dev);
  1763. bond_del_vlans_from_slave(bond, slave_dev);
  1764. /* If the mode USES_PRIMARY, then we should only remove its
  1765. * promisc and mc settings if it was the curr_active_slave, but that was
  1766. * already taken care of above when we detached the slave
  1767. */
  1768. if (!USES_PRIMARY(bond->params.mode)) {
  1769. /* unset promiscuity level from slave */
  1770. if (bond_dev->flags & IFF_PROMISC) {
  1771. dev_set_promiscuity(slave_dev, -1);
  1772. }
  1773. /* unset allmulti level from slave */
  1774. if (bond_dev->flags & IFF_ALLMULTI) {
  1775. dev_set_allmulti(slave_dev, -1);
  1776. }
  1777. /* flush master's mc_list from slave */
  1778. netif_addr_lock_bh(bond_dev);
  1779. bond_mc_list_flush(bond_dev, slave_dev);
  1780. netif_addr_unlock_bh(bond_dev);
  1781. }
  1782. netdev_set_master(slave_dev, NULL);
  1783. /* close slave before restoring its mac address */
  1784. dev_close(slave_dev);
  1785. if (!bond->params.fail_over_mac) {
  1786. /* restore original ("permanent") mac address*/
  1787. memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
  1788. addr.sa_family = slave_dev->type;
  1789. dev_set_mac_address(slave_dev, &addr);
  1790. }
  1791. slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
  1792. IFF_SLAVE_INACTIVE);
  1793. kfree(slave);
  1794. /* re-acquire the lock before getting the next slave */
  1795. write_lock_bh(&bond->lock);
  1796. }
  1797. /* zero the mac address of the master so it will be
  1798. * set by the application to the mac address of the
  1799. * first slave
  1800. */
  1801. memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
  1802. if (list_empty(&bond->vlan_list)) {
  1803. bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
  1804. } else {
  1805. printk(KERN_WARNING DRV_NAME
  1806. ": %s: Warning: clearing HW address of %s while it "
  1807. "still has VLANs.\n",
  1808. bond_dev->name, bond_dev->name);
  1809. printk(KERN_WARNING DRV_NAME
  1810. ": %s: When re-adding slaves, make sure the bond's "
  1811. "HW address matches its VLANs'.\n",
  1812. bond_dev->name);
  1813. }
  1814. printk(KERN_INFO DRV_NAME
  1815. ": %s: released all slaves\n",
  1816. bond_dev->name);
  1817. out:
  1818. write_unlock_bh(&bond->lock);
  1819. return 0;
  1820. }
  1821. /*
  1822. * This function changes the active slave to slave <slave_dev>.
  1823. * It returns -EINVAL in the following cases.
  1824. * - <slave_dev> is not found in the list.
  1825. * - There is not active slave now.
  1826. * - <slave_dev> is already active.
  1827. * - The link state of <slave_dev> is not BOND_LINK_UP.
  1828. * - <slave_dev> is not running.
  1829. * In these cases, this fuction does nothing.
  1830. * In the other cases, currnt_slave pointer is changed and 0 is returned.
  1831. */
  1832. static int bond_ioctl_change_active(struct net_device *bond_dev, struct net_device *slave_dev)
  1833. {
  1834. struct bonding *bond = netdev_priv(bond_dev);
  1835. struct slave *old_active = NULL;
  1836. struct slave *new_active = NULL;
  1837. int res = 0;
  1838. if (!USES_PRIMARY(bond->params.mode)) {
  1839. return -EINVAL;
  1840. }
  1841. /* Verify that master_dev is indeed the master of slave_dev */
  1842. if (!(slave_dev->flags & IFF_SLAVE) ||
  1843. (slave_dev->master != bond_dev)) {
  1844. return -EINVAL;
  1845. }
  1846. read_lock(&bond->lock);
  1847. read_lock(&bond->curr_slave_lock);
  1848. old_active = bond->curr_active_slave;
  1849. read_unlock(&bond->curr_slave_lock);
  1850. new_active = bond_get_slave_by_dev(bond, slave_dev);
  1851. /*
  1852. * Changing to the current active: do nothing; return success.
  1853. */
  1854. if (new_active && (new_active == old_active)) {
  1855. read_unlock(&bond->lock);
  1856. return 0;
  1857. }
  1858. if ((new_active) &&
  1859. (old_active) &&
  1860. (new_active->link == BOND_LINK_UP) &&
  1861. IS_UP(new_active->dev)) {
  1862. write_lock_bh(&bond->curr_slave_lock);
  1863. bond_change_active_slave(bond, new_active);
  1864. write_unlock_bh(&bond->curr_slave_lock);
  1865. } else {
  1866. res = -EINVAL;
  1867. }
  1868. read_unlock(&bond->lock);
  1869. return res;
  1870. }
  1871. static int bond_info_query(struct net_device *bond_dev, struct ifbond *info)
  1872. {
  1873. struct bonding *bond = netdev_priv(bond_dev);
  1874. info->bond_mode = bond->params.mode;
  1875. info->miimon = bond->params.miimon;
  1876. read_lock(&bond->lock);
  1877. info->num_slaves = bond->slave_cnt;
  1878. read_unlock(&bond->lock);
  1879. return 0;
  1880. }
  1881. static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info)
  1882. {
  1883. struct bonding *bond = netdev_priv(bond_dev);
  1884. struct slave *slave;
  1885. int i, found = 0;
  1886. if (info->slave_id < 0) {
  1887. return -ENODEV;
  1888. }
  1889. read_lock(&bond->lock);
  1890. bond_for_each_slave(bond, slave, i) {
  1891. if (i == (int)info->slave_id) {
  1892. found = 1;
  1893. break;
  1894. }
  1895. }
  1896. read_unlock(&bond->lock);
  1897. if (found) {
  1898. strcpy(info->slave_name, slave->dev->name);
  1899. info->link = slave->link;
  1900. info->state = slave->state;
  1901. info->link_failure_count = slave->link_failure_count;
  1902. } else {
  1903. return -ENODEV;
  1904. }
  1905. return 0;
  1906. }
  1907. /*-------------------------------- Monitoring -------------------------------*/
  1908. static int bond_miimon_inspect(struct bonding *bond)
  1909. {
  1910. struct slave *slave;
  1911. int i, link_state, commit = 0;
  1912. bond_for_each_slave(bond, slave, i) {
  1913. slave->new_link = BOND_LINK_NOCHANGE;
  1914. link_state = bond_check_dev_link(bond, slave->dev, 0);
  1915. switch (slave->link) {
  1916. case BOND_LINK_UP:
  1917. if (link_state)
  1918. continue;
  1919. slave->link = BOND_LINK_FAIL;
  1920. slave->delay = bond->params.downdelay;
  1921. if (slave->delay) {
  1922. printk(KERN_INFO DRV_NAME
  1923. ": %s: link status down for %s"
  1924. "interface %s, disabling it in %d ms.\n",
  1925. bond->dev->name,
  1926. (bond->params.mode ==
  1927. BOND_MODE_ACTIVEBACKUP) ?
  1928. ((slave->state == BOND_STATE_ACTIVE) ?
  1929. "active " : "backup ") : "",
  1930. slave->dev->name,
  1931. bond->params.downdelay * bond->params.miimon);
  1932. }
  1933. /*FALLTHRU*/
  1934. case BOND_LINK_FAIL:
  1935. if (link_state) {
  1936. /*
  1937. * recovered before downdelay expired
  1938. */
  1939. slave->link = BOND_LINK_UP;
  1940. slave->jiffies = jiffies;
  1941. printk(KERN_INFO DRV_NAME
  1942. ": %s: link status up again after %d "
  1943. "ms for interface %s.\n",
  1944. bond->dev->name,
  1945. (bond->params.downdelay - slave->delay) *
  1946. bond->params.miimon,
  1947. slave->dev->name);
  1948. continue;
  1949. }
  1950. if (slave->delay <= 0) {
  1951. slave->new_link = BOND_LINK_DOWN;
  1952. commit++;
  1953. continue;
  1954. }
  1955. slave->delay--;
  1956. break;
  1957. case BOND_LINK_DOWN:
  1958. if (!link_state)
  1959. continue;
  1960. slave->link = BOND_LINK_BACK;
  1961. slave->delay = bond->params.updelay;
  1962. if (slave->delay) {
  1963. printk(KERN_INFO DRV_NAME
  1964. ": %s: link status up for "
  1965. "interface %s, enabling it in %d ms.\n",
  1966. bond->dev->name, slave->dev->name,
  1967. bond->params.updelay *
  1968. bond->params.miimon);
  1969. }
  1970. /*FALLTHRU*/
  1971. case BOND_LINK_BACK:
  1972. if (!link_state) {
  1973. slave->link = BOND_LINK_DOWN;
  1974. printk(KERN_INFO DRV_NAME
  1975. ": %s: link status down again after %d "
  1976. "ms for interface %s.\n",
  1977. bond->dev->name,
  1978. (bond->params.updelay - slave->delay) *
  1979. bond->params.miimon,
  1980. slave->dev->name);
  1981. continue;
  1982. }
  1983. if (slave->delay <= 0) {
  1984. slave->new_link = BOND_LINK_UP;
  1985. commit++;
  1986. continue;
  1987. }
  1988. slave->delay--;
  1989. break;
  1990. }
  1991. }
  1992. return commit;
  1993. }
  1994. static void bond_miimon_commit(struct bonding *bond)
  1995. {
  1996. struct slave *slave;
  1997. int i;
  1998. bond_for_each_slave(bond, slave, i) {
  1999. switch (slave->new_link) {
  2000. case BOND_LINK_NOCHANGE:
  2001. continue;
  2002. case BOND_LINK_UP:
  2003. slave->link = BOND_LINK_UP;
  2004. slave->jiffies = jiffies;
  2005. if (bond->params.mode == BOND_MODE_8023AD) {
  2006. /* prevent it from being the active one */
  2007. slave->state = BOND_STATE_BACKUP;
  2008. } else if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
  2009. /* make it immediately active */
  2010. slave->state = BOND_STATE_ACTIVE;
  2011. } else if (slave != bond->primary_slave) {
  2012. /* prevent it from being the active one */
  2013. slave->state = BOND_STATE_BACKUP;
  2014. }
  2015. printk(KERN_INFO DRV_NAME
  2016. ": %s: link status definitely "
  2017. "up for interface %s.\n",
  2018. bond->dev->name, slave->dev->name);
  2019. /* notify ad that the link status has changed */
  2020. if (bond->params.mode == BOND_MODE_8023AD)
  2021. bond_3ad_handle_link_change(slave, BOND_LINK_UP);
  2022. if (bond_is_lb(bond))
  2023. bond_alb_handle_link_change(bond, slave,
  2024. BOND_LINK_UP);
  2025. if (!bond->curr_active_slave ||
  2026. (slave == bond->primary_slave))
  2027. goto do_failover;
  2028. continue;
  2029. case BOND_LINK_DOWN:
  2030. if (slave->link_failure_count < UINT_MAX)
  2031. slave->link_failure_count++;
  2032. slave->link = BOND_LINK_DOWN;
  2033. if (bond->params.mode == BOND_MODE_ACTIVEBACKUP ||
  2034. bond->params.mode == BOND_MODE_8023AD)
  2035. bond_set_slave_inactive_flags(slave);
  2036. printk(KERN_INFO DRV_NAME
  2037. ": %s: link status definitely down for "
  2038. "interface %s, disabling it\n",
  2039. bond->dev->name, slave->dev->name);
  2040. if (bond->params.mode == BOND_MODE_8023AD)
  2041. bond_3ad_handle_link_change(slave,
  2042. BOND_LINK_DOWN);
  2043. if (bond->params.mode == BOND_MODE_TLB ||
  2044. bond->params.mode == BOND_MODE_ALB)
  2045. bond_alb_handle_link_change(bond, slave,
  2046. BOND_LINK_DOWN);
  2047. if (slave == bond->curr_active_slave)
  2048. goto do_failover;
  2049. continue;
  2050. default:
  2051. printk(KERN_ERR DRV_NAME
  2052. ": %s: invalid new link %d on slave %s\n",
  2053. bond->dev->name, slave->new_link,
  2054. slave->dev->name);
  2055. slave->new_link = BOND_LINK_NOCHANGE;
  2056. continue;
  2057. }
  2058. do_failover:
  2059. ASSERT_RTNL();
  2060. write_lock_bh(&bond->curr_slave_lock);
  2061. bond_select_active_slave(bond);
  2062. write_unlock_bh(&bond->curr_slave_lock);
  2063. }
  2064. bond_set_carrier(bond);
  2065. }
  2066. /*
  2067. * bond_mii_monitor
  2068. *
  2069. * Really a wrapper that splits the mii monitor into two phases: an
  2070. * inspection, then (if inspection indicates something needs to be done)
  2071. * an acquisition of appropriate locks followed by a commit phase to
  2072. * implement whatever link state changes are indicated.
  2073. */
  2074. void bond_mii_monitor(struct work_struct *work)
  2075. {
  2076. struct bonding *bond = container_of(work, struct bonding,
  2077. mii_work.work);
  2078. read_lock(&bond->lock);
  2079. if (bond->kill_timers)
  2080. goto out;
  2081. if (bond->slave_cnt == 0)
  2082. goto re_arm;
  2083. if (bond->send_grat_arp) {
  2084. read_lock(&bond->curr_slave_lock);
  2085. bond_send_gratuitous_arp(bond);
  2086. read_unlock(&bond->curr_slave_lock);
  2087. }
  2088. if (bond->send_unsol_na) {
  2089. read_lock(&bond->curr_slave_lock);
  2090. bond_send_unsolicited_na(bond);
  2091. read_unlock(&bond->curr_slave_lock);
  2092. }
  2093. if (bond_miimon_inspect(bond)) {
  2094. read_unlock(&bond->lock);
  2095. rtnl_lock();
  2096. read_lock(&bond->lock);
  2097. bond_miimon_commit(bond);
  2098. read_unlock(&bond->lock);
  2099. rtnl_unlock(); /* might sleep, hold no other locks */
  2100. read_lock(&bond->lock);
  2101. }
  2102. re_arm:
  2103. if (bond->params.miimon)
  2104. queue_delayed_work(bond->wq, &bond->mii_work,
  2105. msecs_to_jiffies(bond->params.miimon));
  2106. out:
  2107. read_unlock(&bond->lock);
  2108. }
  2109. static __be32 bond_glean_dev_ip(struct net_device *dev)
  2110. {
  2111. struct in_device *idev;
  2112. struct in_ifaddr *ifa;
  2113. __be32 addr = 0;
  2114. if (!dev)
  2115. return 0;
  2116. rcu_read_lock();
  2117. idev = __in_dev_get_rcu(dev);
  2118. if (!idev)
  2119. goto out;
  2120. ifa = idev->ifa_list;
  2121. if (!ifa)
  2122. goto out;
  2123. addr = ifa->ifa_local;
  2124. out:
  2125. rcu_read_unlock();
  2126. return addr;
  2127. }
  2128. static int bond_has_this_ip(struct bonding *bond, __be32 ip)
  2129. {
  2130. struct vlan_entry *vlan;
  2131. if (ip == bond->master_ip)
  2132. return 1;
  2133. list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
  2134. if (ip == vlan->vlan_ip)
  2135. return 1;
  2136. }
  2137. return 0;
  2138. }
  2139. /*
  2140. * We go to the (large) trouble of VLAN tagging ARP frames because
  2141. * switches in VLAN mode (especially if ports are configured as
  2142. * "native" to a VLAN) might not pass non-tagged frames.
  2143. */
  2144. static void bond_arp_send(struct net_device *slave_dev, int arp_op, __be32 dest_ip, __be32 src_ip, unsigned short vlan_id)
  2145. {
  2146. struct sk_buff *skb;
  2147. pr_debug("arp %d on slave %s: dst %x src %x vid %d\n", arp_op,
  2148. slave_dev->name, dest_ip, src_ip, vlan_id);
  2149. skb = arp_create(arp_op, ETH_P_ARP, dest_ip, slave_dev, src_ip,
  2150. NULL, slave_dev->dev_addr, NULL);
  2151. if (!skb) {
  2152. printk(KERN_ERR DRV_NAME ": ARP packet allocation failed\n");
  2153. return;
  2154. }
  2155. if (vlan_id) {
  2156. skb = vlan_put_tag(skb, vlan_id);
  2157. if (!skb) {
  2158. printk(KERN_ERR DRV_NAME ": failed to insert VLAN tag\n");
  2159. return;
  2160. }
  2161. }
  2162. arp_xmit(skb);
  2163. }
  2164. static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
  2165. {
  2166. int i, vlan_id, rv;
  2167. __be32 *targets = bond->params.arp_targets;
  2168. struct vlan_entry *vlan;
  2169. struct net_device *vlan_dev;
  2170. struct flowi fl;
  2171. struct rtable *rt;
  2172. for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
  2173. if (!targets[i])
  2174. continue;
  2175. pr_debug("basa: target %x\n", targets[i]);
  2176. if (list_empty(&bond->vlan_list)) {
  2177. pr_debug("basa: empty vlan: arp_send\n");
  2178. bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
  2179. bond->master_ip, 0);
  2180. continue;
  2181. }
  2182. /*
  2183. * If VLANs are configured, we do a route lookup to
  2184. * determine which VLAN interface would be used, so we
  2185. * can tag the ARP with the proper VLAN tag.
  2186. */
  2187. memset(&fl, 0, sizeof(fl));
  2188. fl.fl4_dst = targets[i];
  2189. fl.fl4_tos = RTO_ONLINK;
  2190. rv = ip_route_output_key(&init_net, &rt, &fl);
  2191. if (rv) {
  2192. if (net_ratelimit()) {
  2193. printk(KERN_WARNING DRV_NAME
  2194. ": %s: no route to arp_ip_target %pI4\n",
  2195. bond->dev->name, &fl.fl4_dst);
  2196. }
  2197. continue;
  2198. }
  2199. /*
  2200. * This target is not on a VLAN
  2201. */
  2202. if (rt->u.dst.dev == bond->dev) {
  2203. ip_rt_put(rt);
  2204. pr_debug("basa: rtdev == bond->dev: arp_send\n");
  2205. bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
  2206. bond->master_ip, 0);
  2207. continue;
  2208. }
  2209. vlan_id = 0;
  2210. list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
  2211. vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
  2212. if (vlan_dev == rt->u.dst.dev) {
  2213. vlan_id = vlan->vlan_id;
  2214. pr_debug("basa: vlan match on %s %d\n",
  2215. vlan_dev->name, vlan_id);
  2216. break;
  2217. }
  2218. }
  2219. if (vlan_id) {
  2220. ip_rt_put(rt);
  2221. bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
  2222. vlan->vlan_ip, vlan_id);
  2223. continue;
  2224. }
  2225. if (net_ratelimit()) {
  2226. printk(KERN_WARNING DRV_NAME
  2227. ": %s: no path to arp_ip_target %pI4 via rt.dev %s\n",
  2228. bond->dev->name, &fl.fl4_dst,
  2229. rt->u.dst.dev ? rt->u.dst.dev->name : "NULL");
  2230. }
  2231. ip_rt_put(rt);
  2232. }
  2233. }
  2234. /*
  2235. * Kick out a gratuitous ARP for an IP on the bonding master plus one
  2236. * for each VLAN above us.
  2237. *
  2238. * Caller must hold curr_slave_lock for read or better
  2239. */
  2240. static void bond_send_gratuitous_arp(struct bonding *bond)
  2241. {
  2242. struct slave *slave = bond->curr_active_slave;
  2243. struct vlan_entry *vlan;
  2244. struct net_device *vlan_dev;
  2245. pr_debug("bond_send_grat_arp: bond %s slave %s\n", bond->dev->name,
  2246. slave ? slave->dev->name : "NULL");
  2247. if (!slave || !bond->send_grat_arp ||
  2248. test_bit(__LINK_STATE_LINKWATCH_PENDING, &slave->dev->state))
  2249. return;
  2250. bond->send_grat_arp--;
  2251. if (bond->master_ip) {
  2252. bond_arp_send(slave->dev, ARPOP_REPLY, bond->master_ip,
  2253. bond->master_ip, 0);
  2254. }
  2255. list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
  2256. vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
  2257. if (vlan->vlan_ip) {
  2258. bond_arp_send(slave->dev, ARPOP_REPLY, vlan->vlan_ip,
  2259. vlan->vlan_ip, vlan->vlan_id);
  2260. }
  2261. }
  2262. }
  2263. static void bond_validate_arp(struct bonding *bond, struct slave *slave, __be32 sip, __be32 tip)
  2264. {
  2265. int i;
  2266. __be32 *targets = bond->params.arp_targets;
  2267. targets = bond->params.arp_targets;
  2268. for (i = 0; (i < BOND_MAX_ARP_TARGETS) && targets[i]; i++) {
  2269. pr_debug("bva: sip %pI4 tip %pI4 t[%d] %pI4 bhti(tip) %d\n",
  2270. &sip, &tip, i, &targets[i], bond_has_this_ip(bond, tip));
  2271. if (sip == targets[i]) {
  2272. if (bond_has_this_ip(bond, tip))
  2273. slave->last_arp_rx = jiffies;
  2274. return;
  2275. }
  2276. }
  2277. }
  2278. static int bond_arp_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
  2279. {
  2280. struct arphdr *arp;
  2281. struct slave *slave;
  2282. struct bonding *bond;
  2283. unsigned char *arp_ptr;
  2284. __be32 sip, tip;
  2285. if (dev_net(dev) != &init_net)
  2286. goto out;
  2287. if (!(dev->priv_flags & IFF_BONDING) || !(dev->flags & IFF_MASTER))
  2288. goto out;
  2289. bond = netdev_priv(dev);
  2290. read_lock(&bond->lock);
  2291. pr_debug("bond_arp_rcv: bond %s skb->dev %s orig_dev %s\n",
  2292. bond->dev->name, skb->dev ? skb->dev->name : "NULL",
  2293. orig_dev ? orig_dev->name : "NULL");
  2294. slave = bond_get_slave_by_dev(bond, orig_dev);
  2295. if (!slave || !slave_do_arp_validate(bond, slave))
  2296. goto out_unlock;
  2297. if (!pskb_may_pull(skb, arp_hdr_len(dev)))
  2298. goto out_unlock;
  2299. arp = arp_hdr(skb);
  2300. if (arp->ar_hln != dev->addr_len ||
  2301. skb->pkt_type == PACKET_OTHERHOST ||
  2302. skb->pkt_type == PACKET_LOOPBACK ||
  2303. arp->ar_hrd != htons(ARPHRD_ETHER) ||
  2304. arp->ar_pro != htons(ETH_P_IP) ||
  2305. arp->ar_pln != 4)
  2306. goto out_unlock;
  2307. arp_ptr = (unsigned char *)(arp + 1);
  2308. arp_ptr += dev->addr_len;
  2309. memcpy(&sip, arp_ptr, 4);
  2310. arp_ptr += 4 + dev->addr_len;
  2311. memcpy(&tip, arp_ptr, 4);
  2312. pr_debug("bond_arp_rcv: %s %s/%d av %d sv %d sip %pI4 tip %pI4\n",
  2313. bond->dev->name, slave->dev->name, slave->state,
  2314. bond->params.arp_validate, slave_do_arp_validate(bond, slave),
  2315. &sip, &tip);
  2316. /*
  2317. * Backup slaves won't see the ARP reply, but do come through
  2318. * here for each ARP probe (so we swap the sip/tip to validate
  2319. * the probe). In a "redundant switch, common router" type of
  2320. * configuration, the ARP probe will (hopefully) travel from
  2321. * the active, through one switch, the router, then the other
  2322. * switch before reaching the backup.
  2323. */
  2324. if (slave->state == BOND_STATE_ACTIVE)
  2325. bond_validate_arp(bond, slave, sip, tip);
  2326. else
  2327. bond_validate_arp(bond, slave, tip, sip);
  2328. out_unlock:
  2329. read_unlock(&bond->lock);
  2330. out:
  2331. dev_kfree_skb(skb);
  2332. return NET_RX_SUCCESS;
  2333. }
  2334. /*
  2335. * this function is called regularly to monitor each slave's link
  2336. * ensuring that traffic is being sent and received when arp monitoring
  2337. * is used in load-balancing mode. if the adapter has been dormant, then an
  2338. * arp is transmitted to generate traffic. see activebackup_arp_monitor for
  2339. * arp monitoring in active backup mode.
  2340. */
  2341. void bond_loadbalance_arp_mon(struct work_struct *work)
  2342. {
  2343. struct bonding *bond = container_of(work, struct bonding,
  2344. arp_work.work);
  2345. struct slave *slave, *oldcurrent;
  2346. int do_failover = 0;
  2347. int delta_in_ticks;
  2348. int i;
  2349. read_lock(&bond->lock);
  2350. delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
  2351. if (bond->kill_timers) {
  2352. goto out;
  2353. }
  2354. if (bond->slave_cnt == 0) {
  2355. goto re_arm;
  2356. }
  2357. read_lock(&bond->curr_slave_lock);
  2358. oldcurrent = bond->curr_active_slave;
  2359. read_unlock(&bond->curr_slave_lock);
  2360. /* see if any of the previous devices are up now (i.e. they have
  2361. * xmt and rcv traffic). the curr_active_slave does not come into
  2362. * the picture unless it is null. also, slave->jiffies is not needed
  2363. * here because we send an arp on each slave and give a slave as
  2364. * long as it needs to get the tx/rx within the delta.
  2365. * TODO: what about up/down delay in arp mode? it wasn't here before
  2366. * so it can wait
  2367. */
  2368. bond_for_each_slave(bond, slave, i) {
  2369. if (slave->link != BOND_LINK_UP) {
  2370. if (time_before_eq(jiffies, slave->dev->trans_start + delta_in_ticks) &&
  2371. time_before_eq(jiffies, slave->dev->last_rx + delta_in_ticks)) {
  2372. slave->link = BOND_LINK_UP;
  2373. slave->state = BOND_STATE_ACTIVE;
  2374. /* primary_slave has no meaning in round-robin
  2375. * mode. the window of a slave being up and
  2376. * curr_active_slave being null after enslaving
  2377. * is closed.
  2378. */
  2379. if (!oldcurrent) {
  2380. printk(KERN_INFO DRV_NAME
  2381. ": %s: link status definitely "
  2382. "up for interface %s, ",
  2383. bond->dev->name,
  2384. slave->dev->name);
  2385. do_failover = 1;
  2386. } else {
  2387. printk(KERN_INFO DRV_NAME
  2388. ": %s: interface %s is now up\n",
  2389. bond->dev->name,
  2390. slave->dev->name);
  2391. }
  2392. }
  2393. } else {
  2394. /* slave->link == BOND_LINK_UP */
  2395. /* not all switches will respond to an arp request
  2396. * when the source ip is 0, so don't take the link down
  2397. * if we don't know our ip yet
  2398. */
  2399. if (time_after_eq(jiffies, slave->dev->trans_start + 2*delta_in_ticks) ||
  2400. (time_after_eq(jiffies, slave->dev->last_rx + 2*delta_in_ticks))) {
  2401. slave->link = BOND_LINK_DOWN;
  2402. slave->state = BOND_STATE_BACKUP;
  2403. if (slave->link_failure_count < UINT_MAX) {
  2404. slave->link_failure_count++;
  2405. }
  2406. printk(KERN_INFO DRV_NAME
  2407. ": %s: interface %s is now down.\n",
  2408. bond->dev->name,
  2409. slave->dev->name);
  2410. if (slave == oldcurrent) {
  2411. do_failover = 1;
  2412. }
  2413. }
  2414. }
  2415. /* note: if switch is in round-robin mode, all links
  2416. * must tx arp to ensure all links rx an arp - otherwise
  2417. * links may oscillate or not come up at all; if switch is
  2418. * in something like xor mode, there is nothing we can
  2419. * do - all replies will be rx'ed on same link causing slaves
  2420. * to be unstable during low/no traffic periods
  2421. */
  2422. if (IS_UP(slave->dev)) {
  2423. bond_arp_send_all(bond, slave);
  2424. }
  2425. }
  2426. if (do_failover) {
  2427. write_lock_bh(&bond->curr_slave_lock);
  2428. bond_select_active_slave(bond);
  2429. write_unlock_bh(&bond->curr_slave_lock);
  2430. }
  2431. re_arm:
  2432. if (bond->params.arp_interval)
  2433. queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
  2434. out:
  2435. read_unlock(&bond->lock);
  2436. }
  2437. /*
  2438. * Called to inspect slaves for active-backup mode ARP monitor link state
  2439. * changes. Sets new_link in slaves to specify what action should take
  2440. * place for the slave. Returns 0 if no changes are found, >0 if changes
  2441. * to link states must be committed.
  2442. *
  2443. * Called with bond->lock held for read.
  2444. */
  2445. static int bond_ab_arp_inspect(struct bonding *bond, int delta_in_ticks)
  2446. {
  2447. struct slave *slave;
  2448. int i, commit = 0;
  2449. bond_for_each_slave(bond, slave, i) {
  2450. slave->new_link = BOND_LINK_NOCHANGE;
  2451. if (slave->link != BOND_LINK_UP) {
  2452. if (time_before_eq(jiffies, slave_last_rx(bond, slave) +
  2453. delta_in_ticks)) {
  2454. slave->new_link = BOND_LINK_UP;
  2455. commit++;
  2456. }
  2457. continue;
  2458. }
  2459. /*
  2460. * Give slaves 2*delta after being enslaved or made
  2461. * active. This avoids bouncing, as the last receive
  2462. * times need a full ARP monitor cycle to be updated.
  2463. */
  2464. if (!time_after_eq(jiffies, slave->jiffies +
  2465. 2 * delta_in_ticks))
  2466. continue;
  2467. /*
  2468. * Backup slave is down if:
  2469. * - No current_arp_slave AND
  2470. * - more than 3*delta since last receive AND
  2471. * - the bond has an IP address
  2472. *
  2473. * Note: a non-null current_arp_slave indicates
  2474. * the curr_active_slave went down and we are
  2475. * searching for a new one; under this condition
  2476. * we only take the curr_active_slave down - this
  2477. * gives each slave a chance to tx/rx traffic
  2478. * before being taken out
  2479. */
  2480. if (slave->state == BOND_STATE_BACKUP &&
  2481. !bond->current_arp_slave &&
  2482. time_after(jiffies, slave_last_rx(bond, slave) +
  2483. 3 * delta_in_ticks)) {
  2484. slave->new_link = BOND_LINK_DOWN;
  2485. commit++;
  2486. }
  2487. /*
  2488. * Active slave is down if:
  2489. * - more than 2*delta since transmitting OR
  2490. * - (more than 2*delta since receive AND
  2491. * the bond has an IP address)
  2492. */
  2493. if ((slave->state == BOND_STATE_ACTIVE) &&
  2494. (time_after_eq(jiffies, slave->dev->trans_start +
  2495. 2 * delta_in_ticks) ||
  2496. (time_after_eq(jiffies, slave_last_rx(bond, slave)
  2497. + 2 * delta_in_ticks)))) {
  2498. slave->new_link = BOND_LINK_DOWN;
  2499. commit++;
  2500. }
  2501. }
  2502. read_lock(&bond->curr_slave_lock);
  2503. /*
  2504. * Trigger a commit if the primary option setting has changed.
  2505. */
  2506. if (bond->primary_slave &&
  2507. (bond->primary_slave != bond->curr_active_slave) &&
  2508. (bond->primary_slave->link == BOND_LINK_UP))
  2509. commit++;
  2510. read_unlock(&bond->curr_slave_lock);
  2511. return commit;
  2512. }
  2513. /*
  2514. * Called to commit link state changes noted by inspection step of
  2515. * active-backup mode ARP monitor.
  2516. *
  2517. * Called with RTNL and bond->lock for read.
  2518. */
  2519. static void bond_ab_arp_commit(struct bonding *bond, int delta_in_ticks)
  2520. {
  2521. struct slave *slave;
  2522. int i;
  2523. bond_for_each_slave(bond, slave, i) {
  2524. switch (slave->new_link) {
  2525. case BOND_LINK_NOCHANGE:
  2526. continue;
  2527. case BOND_LINK_UP:
  2528. write_lock_bh(&bond->curr_slave_lock);
  2529. if (!bond->curr_active_slave &&
  2530. time_before_eq(jiffies, slave->dev->trans_start +
  2531. delta_in_ticks)) {
  2532. slave->link = BOND_LINK_UP;
  2533. bond_change_active_slave(bond, slave);
  2534. bond->current_arp_slave = NULL;
  2535. printk(KERN_INFO DRV_NAME
  2536. ": %s: %s is up and now the "
  2537. "active interface\n",
  2538. bond->dev->name, slave->dev->name);
  2539. } else if (bond->curr_active_slave != slave) {
  2540. /* this slave has just come up but we
  2541. * already have a current slave; this can
  2542. * also happen if bond_enslave adds a new
  2543. * slave that is up while we are searching
  2544. * for a new slave
  2545. */
  2546. slave->link = BOND_LINK_UP;
  2547. bond_set_slave_inactive_flags(slave);
  2548. bond->current_arp_slave = NULL;
  2549. printk(KERN_INFO DRV_NAME
  2550. ": %s: backup interface %s is now up\n",
  2551. bond->dev->name, slave->dev->name);
  2552. }
  2553. write_unlock_bh(&bond->curr_slave_lock);
  2554. break;
  2555. case BOND_LINK_DOWN:
  2556. if (slave->link_failure_count < UINT_MAX)
  2557. slave->link_failure_count++;
  2558. slave->link = BOND_LINK_DOWN;
  2559. if (slave == bond->curr_active_slave) {
  2560. printk(KERN_INFO DRV_NAME
  2561. ": %s: link status down for active "
  2562. "interface %s, disabling it\n",
  2563. bond->dev->name, slave->dev->name);
  2564. bond_set_slave_inactive_flags(slave);
  2565. write_lock_bh(&bond->curr_slave_lock);
  2566. bond_select_active_slave(bond);
  2567. if (bond->curr_active_slave)
  2568. bond->curr_active_slave->jiffies =
  2569. jiffies;
  2570. write_unlock_bh(&bond->curr_slave_lock);
  2571. bond->current_arp_slave = NULL;
  2572. } else if (slave->state == BOND_STATE_BACKUP) {
  2573. printk(KERN_INFO DRV_NAME
  2574. ": %s: backup interface %s is now down\n",
  2575. bond->dev->name, slave->dev->name);
  2576. bond_set_slave_inactive_flags(slave);
  2577. }
  2578. break;
  2579. default:
  2580. printk(KERN_ERR DRV_NAME
  2581. ": %s: impossible: new_link %d on slave %s\n",
  2582. bond->dev->name, slave->new_link,
  2583. slave->dev->name);
  2584. }
  2585. }
  2586. /*
  2587. * No race with changes to primary via sysfs, as we hold rtnl.
  2588. */
  2589. if (bond->primary_slave &&
  2590. (bond->primary_slave != bond->curr_active_slave) &&
  2591. (bond->primary_slave->link == BOND_LINK_UP)) {
  2592. write_lock_bh(&bond->curr_slave_lock);
  2593. bond_change_active_slave(bond, bond->primary_slave);
  2594. write_unlock_bh(&bond->curr_slave_lock);
  2595. }
  2596. bond_set_carrier(bond);
  2597. }
  2598. /*
  2599. * Send ARP probes for active-backup mode ARP monitor.
  2600. *
  2601. * Called with bond->lock held for read.
  2602. */
  2603. static void bond_ab_arp_probe(struct bonding *bond)
  2604. {
  2605. struct slave *slave;
  2606. int i;
  2607. read_lock(&bond->curr_slave_lock);
  2608. if (bond->current_arp_slave && bond->curr_active_slave)
  2609. printk("PROBE: c_arp %s && cas %s BAD\n",
  2610. bond->current_arp_slave->dev->name,
  2611. bond->curr_active_slave->dev->name);
  2612. if (bond->curr_active_slave) {
  2613. bond_arp_send_all(bond, bond->curr_active_slave);
  2614. read_unlock(&bond->curr_slave_lock);
  2615. return;
  2616. }
  2617. read_unlock(&bond->curr_slave_lock);
  2618. /* if we don't have a curr_active_slave, search for the next available
  2619. * backup slave from the current_arp_slave and make it the candidate
  2620. * for becoming the curr_active_slave
  2621. */
  2622. if (!bond->current_arp_slave) {
  2623. bond->current_arp_slave = bond->first_slave;
  2624. if (!bond->current_arp_slave)
  2625. return;
  2626. }
  2627. bond_set_slave_inactive_flags(bond->current_arp_slave);
  2628. /* search for next candidate */
  2629. bond_for_each_slave_from(bond, slave, i, bond->current_arp_slave->next) {
  2630. if (IS_UP(slave->dev)) {
  2631. slave->link = BOND_LINK_BACK;
  2632. bond_set_slave_active_flags(slave);
  2633. bond_arp_send_all(bond, slave);
  2634. slave->jiffies = jiffies;
  2635. bond->current_arp_slave = slave;
  2636. break;
  2637. }
  2638. /* if the link state is up at this point, we
  2639. * mark it down - this can happen if we have
  2640. * simultaneous link failures and
  2641. * reselect_active_interface doesn't make this
  2642. * one the current slave so it is still marked
  2643. * up when it is actually down
  2644. */
  2645. if (slave->link == BOND_LINK_UP) {
  2646. slave->link = BOND_LINK_DOWN;
  2647. if (slave->link_failure_count < UINT_MAX)
  2648. slave->link_failure_count++;
  2649. bond_set_slave_inactive_flags(slave);
  2650. printk(KERN_INFO DRV_NAME
  2651. ": %s: backup interface %s is now down.\n",
  2652. bond->dev->name, slave->dev->name);
  2653. }
  2654. }
  2655. }
  2656. void bond_activebackup_arp_mon(struct work_struct *work)
  2657. {
  2658. struct bonding *bond = container_of(work, struct bonding,
  2659. arp_work.work);
  2660. int delta_in_ticks;
  2661. read_lock(&bond->lock);
  2662. if (bond->kill_timers)
  2663. goto out;
  2664. delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
  2665. if (bond->slave_cnt == 0)
  2666. goto re_arm;
  2667. if (bond->send_grat_arp) {
  2668. read_lock(&bond->curr_slave_lock);
  2669. bond_send_gratuitous_arp(bond);
  2670. read_unlock(&bond->curr_slave_lock);
  2671. }
  2672. if (bond->send_unsol_na) {
  2673. read_lock(&bond->curr_slave_lock);
  2674. bond_send_unsolicited_na(bond);
  2675. read_unlock(&bond->curr_slave_lock);
  2676. }
  2677. if (bond_ab_arp_inspect(bond, delta_in_ticks)) {
  2678. read_unlock(&bond->lock);
  2679. rtnl_lock();
  2680. read_lock(&bond->lock);
  2681. bond_ab_arp_commit(bond, delta_in_ticks);
  2682. read_unlock(&bond->lock);
  2683. rtnl_unlock();
  2684. read_lock(&bond->lock);
  2685. }
  2686. bond_ab_arp_probe(bond);
  2687. re_arm:
  2688. if (bond->params.arp_interval) {
  2689. queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
  2690. }
  2691. out:
  2692. read_unlock(&bond->lock);
  2693. }
  2694. /*------------------------------ proc/seq_file-------------------------------*/
  2695. #ifdef CONFIG_PROC_FS
  2696. static void *bond_info_seq_start(struct seq_file *seq, loff_t *pos)
  2697. {
  2698. struct bonding *bond = seq->private;
  2699. loff_t off = 0;
  2700. struct slave *slave;
  2701. int i;
  2702. /* make sure the bond won't be taken away */
  2703. read_lock(&dev_base_lock);
  2704. read_lock(&bond->lock);
  2705. if (*pos == 0) {
  2706. return SEQ_START_TOKEN;
  2707. }
  2708. bond_for_each_slave(bond, slave, i) {
  2709. if (++off == *pos) {
  2710. return slave;
  2711. }
  2712. }
  2713. return NULL;
  2714. }
  2715. static void *bond_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2716. {
  2717. struct bonding *bond = seq->private;
  2718. struct slave *slave = v;
  2719. ++*pos;
  2720. if (v == SEQ_START_TOKEN) {
  2721. return bond->first_slave;
  2722. }
  2723. slave = slave->next;
  2724. return (slave == bond->first_slave) ? NULL : slave;
  2725. }
  2726. static void bond_info_seq_stop(struct seq_file *seq, void *v)
  2727. {
  2728. struct bonding *bond = seq->private;
  2729. read_unlock(&bond->lock);
  2730. read_unlock(&dev_base_lock);
  2731. }
  2732. static void bond_info_show_master(struct seq_file *seq)
  2733. {
  2734. struct bonding *bond = seq->private;
  2735. struct slave *curr;
  2736. int i;
  2737. read_lock(&bond->curr_slave_lock);
  2738. curr = bond->curr_active_slave;
  2739. read_unlock(&bond->curr_slave_lock);
  2740. seq_printf(seq, "Bonding Mode: %s",
  2741. bond_mode_name(bond->params.mode));
  2742. if (bond->params.mode == BOND_MODE_ACTIVEBACKUP &&
  2743. bond->params.fail_over_mac)
  2744. seq_printf(seq, " (fail_over_mac %s)",
  2745. fail_over_mac_tbl[bond->params.fail_over_mac].modename);
  2746. seq_printf(seq, "\n");
  2747. if (bond->params.mode == BOND_MODE_XOR ||
  2748. bond->params.mode == BOND_MODE_8023AD) {
  2749. seq_printf(seq, "Transmit Hash Policy: %s (%d)\n",
  2750. xmit_hashtype_tbl[bond->params.xmit_policy].modename,
  2751. bond->params.xmit_policy);
  2752. }
  2753. if (USES_PRIMARY(bond->params.mode)) {
  2754. seq_printf(seq, "Primary Slave: %s\n",
  2755. (bond->primary_slave) ?
  2756. bond->primary_slave->dev->name : "None");
  2757. seq_printf(seq, "Currently Active Slave: %s\n",
  2758. (curr) ? curr->dev->name : "None");
  2759. }
  2760. seq_printf(seq, "MII Status: %s\n", netif_carrier_ok(bond->dev) ?
  2761. "up" : "down");
  2762. seq_printf(seq, "MII Polling Interval (ms): %d\n", bond->params.miimon);
  2763. seq_printf(seq, "Up Delay (ms): %d\n",
  2764. bond->params.updelay * bond->params.miimon);
  2765. seq_printf(seq, "Down Delay (ms): %d\n",
  2766. bond->params.downdelay * bond->params.miimon);
  2767. /* ARP information */
  2768. if(bond->params.arp_interval > 0) {
  2769. int printed=0;
  2770. seq_printf(seq, "ARP Polling Interval (ms): %d\n",
  2771. bond->params.arp_interval);
  2772. seq_printf(seq, "ARP IP target/s (n.n.n.n form):");
  2773. for(i = 0; (i < BOND_MAX_ARP_TARGETS) ;i++) {
  2774. if (!bond->params.arp_targets[i])
  2775. continue;
  2776. if (printed)
  2777. seq_printf(seq, ",");
  2778. seq_printf(seq, " %pI4", &bond->params.arp_targets[i]);
  2779. printed = 1;
  2780. }
  2781. seq_printf(seq, "\n");
  2782. }
  2783. if (bond->params.mode == BOND_MODE_8023AD) {
  2784. struct ad_info ad_info;
  2785. seq_puts(seq, "\n802.3ad info\n");
  2786. seq_printf(seq, "LACP rate: %s\n",
  2787. (bond->params.lacp_fast) ? "fast" : "slow");
  2788. seq_printf(seq, "Aggregator selection policy (ad_select): %s\n",
  2789. ad_select_tbl[bond->params.ad_select].modename);
  2790. if (bond_3ad_get_active_agg_info(bond, &ad_info)) {
  2791. seq_printf(seq, "bond %s has no active aggregator\n",
  2792. bond->dev->name);
  2793. } else {
  2794. seq_printf(seq, "Active Aggregator Info:\n");
  2795. seq_printf(seq, "\tAggregator ID: %d\n",
  2796. ad_info.aggregator_id);
  2797. seq_printf(seq, "\tNumber of ports: %d\n",
  2798. ad_info.ports);
  2799. seq_printf(seq, "\tActor Key: %d\n",
  2800. ad_info.actor_key);
  2801. seq_printf(seq, "\tPartner Key: %d\n",
  2802. ad_info.partner_key);
  2803. seq_printf(seq, "\tPartner Mac Address: %pM\n",
  2804. ad_info.partner_system);
  2805. }
  2806. }
  2807. }
  2808. static void bond_info_show_slave(struct seq_file *seq, const struct slave *slave)
  2809. {
  2810. struct bonding *bond = seq->private;
  2811. seq_printf(seq, "\nSlave Interface: %s\n", slave->dev->name);
  2812. seq_printf(seq, "MII Status: %s\n",
  2813. (slave->link == BOND_LINK_UP) ? "up" : "down");
  2814. seq_printf(seq, "Link Failure Count: %u\n",
  2815. slave->link_failure_count);
  2816. seq_printf(seq, "Permanent HW addr: %pM\n", slave->perm_hwaddr);
  2817. if (bond->params.mode == BOND_MODE_8023AD) {
  2818. const struct aggregator *agg
  2819. = SLAVE_AD_INFO(slave).port.aggregator;
  2820. if (agg) {
  2821. seq_printf(seq, "Aggregator ID: %d\n",
  2822. agg->aggregator_identifier);
  2823. } else {
  2824. seq_puts(seq, "Aggregator ID: N/A\n");
  2825. }
  2826. }
  2827. }
  2828. static int bond_info_seq_show(struct seq_file *seq, void *v)
  2829. {
  2830. if (v == SEQ_START_TOKEN) {
  2831. seq_printf(seq, "%s\n", version);
  2832. bond_info_show_master(seq);
  2833. } else {
  2834. bond_info_show_slave(seq, v);
  2835. }
  2836. return 0;
  2837. }
  2838. static struct seq_operations bond_info_seq_ops = {
  2839. .start = bond_info_seq_start,
  2840. .next = bond_info_seq_next,
  2841. .stop = bond_info_seq_stop,
  2842. .show = bond_info_seq_show,
  2843. };
  2844. static int bond_info_open(struct inode *inode, struct file *file)
  2845. {
  2846. struct seq_file *seq;
  2847. struct proc_dir_entry *proc;
  2848. int res;
  2849. res = seq_open(file, &bond_info_seq_ops);
  2850. if (!res) {
  2851. /* recover the pointer buried in proc_dir_entry data */
  2852. seq = file->private_data;
  2853. proc = PDE(inode);
  2854. seq->private = proc->data;
  2855. }
  2856. return res;
  2857. }
  2858. static const struct file_operations bond_info_fops = {
  2859. .owner = THIS_MODULE,
  2860. .open = bond_info_open,
  2861. .read = seq_read,
  2862. .llseek = seq_lseek,
  2863. .release = seq_release,
  2864. };
  2865. static int bond_create_proc_entry(struct bonding *bond)
  2866. {
  2867. struct net_device *bond_dev = bond->dev;
  2868. if (bond_proc_dir) {
  2869. bond->proc_entry = proc_create_data(bond_dev->name,
  2870. S_IRUGO, bond_proc_dir,
  2871. &bond_info_fops, bond);
  2872. if (bond->proc_entry == NULL) {
  2873. printk(KERN_WARNING DRV_NAME
  2874. ": Warning: Cannot create /proc/net/%s/%s\n",
  2875. DRV_NAME, bond_dev->name);
  2876. } else {
  2877. memcpy(bond->proc_file_name, bond_dev->name, IFNAMSIZ);
  2878. }
  2879. }
  2880. return 0;
  2881. }
  2882. static void bond_remove_proc_entry(struct bonding *bond)
  2883. {
  2884. if (bond_proc_dir && bond->proc_entry) {
  2885. remove_proc_entry(bond->proc_file_name, bond_proc_dir);
  2886. memset(bond->proc_file_name, 0, IFNAMSIZ);
  2887. bond->proc_entry = NULL;
  2888. }
  2889. }
  2890. /* Create the bonding directory under /proc/net, if doesn't exist yet.
  2891. * Caller must hold rtnl_lock.
  2892. */
  2893. static void bond_create_proc_dir(void)
  2894. {
  2895. int len = strlen(DRV_NAME);
  2896. for (bond_proc_dir = init_net.proc_net->subdir; bond_proc_dir;
  2897. bond_proc_dir = bond_proc_dir->next) {
  2898. if ((bond_proc_dir->namelen == len) &&
  2899. !memcmp(bond_proc_dir->name, DRV_NAME, len)) {
  2900. break;
  2901. }
  2902. }
  2903. if (!bond_proc_dir) {
  2904. bond_proc_dir = proc_mkdir(DRV_NAME, init_net.proc_net);
  2905. if (bond_proc_dir) {
  2906. bond_proc_dir->owner = THIS_MODULE;
  2907. } else {
  2908. printk(KERN_WARNING DRV_NAME
  2909. ": Warning: cannot create /proc/net/%s\n",
  2910. DRV_NAME);
  2911. }
  2912. }
  2913. }
  2914. /* Destroy the bonding directory under /proc/net, if empty.
  2915. * Caller must hold rtnl_lock.
  2916. */
  2917. static void bond_destroy_proc_dir(void)
  2918. {
  2919. struct proc_dir_entry *de;
  2920. if (!bond_proc_dir) {
  2921. return;
  2922. }
  2923. /* verify that the /proc dir is empty */
  2924. for (de = bond_proc_dir->subdir; de; de = de->next) {
  2925. /* ignore . and .. */
  2926. if (*(de->name) != '.') {
  2927. break;
  2928. }
  2929. }
  2930. if (de) {
  2931. if (bond_proc_dir->owner == THIS_MODULE) {
  2932. bond_proc_dir->owner = NULL;
  2933. }
  2934. } else {
  2935. remove_proc_entry(DRV_NAME, init_net.proc_net);
  2936. bond_proc_dir = NULL;
  2937. }
  2938. }
  2939. #endif /* CONFIG_PROC_FS */
  2940. /*-------------------------- netdev event handling --------------------------*/
  2941. /*
  2942. * Change device name
  2943. */
  2944. static int bond_event_changename(struct bonding *bond)
  2945. {
  2946. #ifdef CONFIG_PROC_FS
  2947. bond_remove_proc_entry(bond);
  2948. bond_create_proc_entry(bond);
  2949. #endif
  2950. down_write(&(bonding_rwsem));
  2951. bond_destroy_sysfs_entry(bond);
  2952. bond_create_sysfs_entry(bond);
  2953. up_write(&(bonding_rwsem));
  2954. return NOTIFY_DONE;
  2955. }
  2956. static int bond_master_netdev_event(unsigned long event, struct net_device *bond_dev)
  2957. {
  2958. struct bonding *event_bond = netdev_priv(bond_dev);
  2959. switch (event) {
  2960. case NETDEV_CHANGENAME:
  2961. return bond_event_changename(event_bond);
  2962. case NETDEV_UNREGISTER:
  2963. bond_release_all(event_bond->dev);
  2964. break;
  2965. default:
  2966. break;
  2967. }
  2968. return NOTIFY_DONE;
  2969. }
  2970. static int bond_slave_netdev_event(unsigned long event, struct net_device *slave_dev)
  2971. {
  2972. struct net_device *bond_dev = slave_dev->master;
  2973. struct bonding *bond = netdev_priv(bond_dev);
  2974. switch (event) {
  2975. case NETDEV_UNREGISTER:
  2976. if (bond_dev) {
  2977. if (bond->setup_by_slave)
  2978. bond_release_and_destroy(bond_dev, slave_dev);
  2979. else
  2980. bond_release(bond_dev, slave_dev);
  2981. }
  2982. break;
  2983. case NETDEV_CHANGE:
  2984. /*
  2985. * TODO: is this what we get if somebody
  2986. * sets up a hierarchical bond, then rmmod's
  2987. * one of the slave bonding devices?
  2988. */
  2989. break;
  2990. case NETDEV_DOWN:
  2991. /*
  2992. * ... Or is it this?
  2993. */
  2994. break;
  2995. case NETDEV_CHANGEMTU:
  2996. /*
  2997. * TODO: Should slaves be allowed to
  2998. * independently alter their MTU? For
  2999. * an active-backup bond, slaves need
  3000. * not be the same type of device, so
  3001. * MTUs may vary. For other modes,
  3002. * slaves arguably should have the
  3003. * same MTUs. To do this, we'd need to
  3004. * take over the slave's change_mtu
  3005. * function for the duration of their
  3006. * servitude.
  3007. */
  3008. break;
  3009. case NETDEV_CHANGENAME:
  3010. /*
  3011. * TODO: handle changing the primary's name
  3012. */
  3013. break;
  3014. case NETDEV_FEAT_CHANGE:
  3015. bond_compute_features(bond);
  3016. break;
  3017. default:
  3018. break;
  3019. }
  3020. return NOTIFY_DONE;
  3021. }
  3022. /*
  3023. * bond_netdev_event: handle netdev notifier chain events.
  3024. *
  3025. * This function receives events for the netdev chain. The caller (an
  3026. * ioctl handler calling blocking_notifier_call_chain) holds the necessary
  3027. * locks for us to safely manipulate the slave devices (RTNL lock,
  3028. * dev_probe_lock).
  3029. */
  3030. static int bond_netdev_event(struct notifier_block *this, unsigned long event, void *ptr)
  3031. {
  3032. struct net_device *event_dev = (struct net_device *)ptr;
  3033. if (dev_net(event_dev) != &init_net)
  3034. return NOTIFY_DONE;
  3035. pr_debug("event_dev: %s, event: %lx\n",
  3036. (event_dev ? event_dev->name : "None"),
  3037. event);
  3038. if (!(event_dev->priv_flags & IFF_BONDING))
  3039. return NOTIFY_DONE;
  3040. if (event_dev->flags & IFF_MASTER) {
  3041. pr_debug("IFF_MASTER\n");
  3042. return bond_master_netdev_event(event, event_dev);
  3043. }
  3044. if (event_dev->flags & IFF_SLAVE) {
  3045. pr_debug("IFF_SLAVE\n");
  3046. return bond_slave_netdev_event(event, event_dev);
  3047. }
  3048. return NOTIFY_DONE;
  3049. }
  3050. /*
  3051. * bond_inetaddr_event: handle inetaddr notifier chain events.
  3052. *
  3053. * We keep track of device IPs primarily to use as source addresses in
  3054. * ARP monitor probes (rather than spewing out broadcasts all the time).
  3055. *
  3056. * We track one IP for the main device (if it has one), plus one per VLAN.
  3057. */
  3058. static int bond_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
  3059. {
  3060. struct in_ifaddr *ifa = ptr;
  3061. struct net_device *vlan_dev, *event_dev = ifa->ifa_dev->dev;
  3062. struct bonding *bond;
  3063. struct vlan_entry *vlan;
  3064. if (dev_net(ifa->ifa_dev->dev) != &init_net)
  3065. return NOTIFY_DONE;
  3066. list_for_each_entry(bond, &bond_dev_list, bond_list) {
  3067. if (bond->dev == event_dev) {
  3068. switch (event) {
  3069. case NETDEV_UP:
  3070. bond->master_ip = ifa->ifa_local;
  3071. return NOTIFY_OK;
  3072. case NETDEV_DOWN:
  3073. bond->master_ip = bond_glean_dev_ip(bond->dev);
  3074. return NOTIFY_OK;
  3075. default:
  3076. return NOTIFY_DONE;
  3077. }
  3078. }
  3079. list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
  3080. vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
  3081. if (vlan_dev == event_dev) {
  3082. switch (event) {
  3083. case NETDEV_UP:
  3084. vlan->vlan_ip = ifa->ifa_local;
  3085. return NOTIFY_OK;
  3086. case NETDEV_DOWN:
  3087. vlan->vlan_ip =
  3088. bond_glean_dev_ip(vlan_dev);
  3089. return NOTIFY_OK;
  3090. default:
  3091. return NOTIFY_DONE;
  3092. }
  3093. }
  3094. }
  3095. }
  3096. return NOTIFY_DONE;
  3097. }
  3098. static struct notifier_block bond_netdev_notifier = {
  3099. .notifier_call = bond_netdev_event,
  3100. };
  3101. static struct notifier_block bond_inetaddr_notifier = {
  3102. .notifier_call = bond_inetaddr_event,
  3103. };
  3104. /*-------------------------- Packet type handling ---------------------------*/
  3105. /* register to receive lacpdus on a bond */
  3106. static void bond_register_lacpdu(struct bonding *bond)
  3107. {
  3108. struct packet_type *pk_type = &(BOND_AD_INFO(bond).ad_pkt_type);
  3109. /* initialize packet type */
  3110. pk_type->type = PKT_TYPE_LACPDU;
  3111. pk_type->dev = bond->dev;
  3112. pk_type->func = bond_3ad_lacpdu_recv;
  3113. dev_add_pack(pk_type);
  3114. }
  3115. /* unregister to receive lacpdus on a bond */
  3116. static void bond_unregister_lacpdu(struct bonding *bond)
  3117. {
  3118. dev_remove_pack(&(BOND_AD_INFO(bond).ad_pkt_type));
  3119. }
  3120. void bond_register_arp(struct bonding *bond)
  3121. {
  3122. struct packet_type *pt = &bond->arp_mon_pt;
  3123. if (pt->type)
  3124. return;
  3125. pt->type = htons(ETH_P_ARP);
  3126. pt->dev = bond->dev;
  3127. pt->func = bond_arp_rcv;
  3128. dev_add_pack(pt);
  3129. }
  3130. void bond_unregister_arp(struct bonding *bond)
  3131. {
  3132. struct packet_type *pt = &bond->arp_mon_pt;
  3133. dev_remove_pack(pt);
  3134. pt->type = 0;
  3135. }
  3136. /*---------------------------- Hashing Policies -----------------------------*/
  3137. /*
  3138. * Hash for the output device based upon layer 2 and layer 3 data. If
  3139. * the packet is not IP mimic bond_xmit_hash_policy_l2()
  3140. */
  3141. static int bond_xmit_hash_policy_l23(struct sk_buff *skb,
  3142. struct net_device *bond_dev, int count)
  3143. {
  3144. struct ethhdr *data = (struct ethhdr *)skb->data;
  3145. struct iphdr *iph = ip_hdr(skb);
  3146. if (skb->protocol == htons(ETH_P_IP)) {
  3147. return ((ntohl(iph->saddr ^ iph->daddr) & 0xffff) ^
  3148. (data->h_dest[5] ^ bond_dev->dev_addr[5])) % count;
  3149. }
  3150. return (data->h_dest[5] ^ bond_dev->dev_addr[5]) % count;
  3151. }
  3152. /*
  3153. * Hash for the output device based upon layer 3 and layer 4 data. If
  3154. * the packet is a frag or not TCP or UDP, just use layer 3 data. If it is
  3155. * altogether not IP, mimic bond_xmit_hash_policy_l2()
  3156. */
  3157. static int bond_xmit_hash_policy_l34(struct sk_buff *skb,
  3158. struct net_device *bond_dev, int count)
  3159. {
  3160. struct ethhdr *data = (struct ethhdr *)skb->data;
  3161. struct iphdr *iph = ip_hdr(skb);
  3162. __be16 *layer4hdr = (__be16 *)((u32 *)iph + iph->ihl);
  3163. int layer4_xor = 0;
  3164. if (skb->protocol == htons(ETH_P_IP)) {
  3165. if (!(iph->frag_off & htons(IP_MF|IP_OFFSET)) &&
  3166. (iph->protocol == IPPROTO_TCP ||
  3167. iph->protocol == IPPROTO_UDP)) {
  3168. layer4_xor = ntohs((*layer4hdr ^ *(layer4hdr + 1)));
  3169. }
  3170. return (layer4_xor ^
  3171. ((ntohl(iph->saddr ^ iph->daddr)) & 0xffff)) % count;
  3172. }
  3173. return (data->h_dest[5] ^ bond_dev->dev_addr[5]) % count;
  3174. }
  3175. /*
  3176. * Hash for the output device based upon layer 2 data
  3177. */
  3178. static int bond_xmit_hash_policy_l2(struct sk_buff *skb,
  3179. struct net_device *bond_dev, int count)
  3180. {
  3181. struct ethhdr *data = (struct ethhdr *)skb->data;
  3182. return (data->h_dest[5] ^ bond_dev->dev_addr[5]) % count;
  3183. }
  3184. /*-------------------------- Device entry points ----------------------------*/
  3185. static int bond_open(struct net_device *bond_dev)
  3186. {
  3187. struct bonding *bond = netdev_priv(bond_dev);
  3188. bond->kill_timers = 0;
  3189. if (bond_is_lb(bond)) {
  3190. /* bond_alb_initialize must be called before the timer
  3191. * is started.
  3192. */
  3193. if (bond_alb_initialize(bond, (bond->params.mode == BOND_MODE_ALB))) {
  3194. /* something went wrong - fail the open operation */
  3195. return -1;
  3196. }
  3197. INIT_DELAYED_WORK(&bond->alb_work, bond_alb_monitor);
  3198. queue_delayed_work(bond->wq, &bond->alb_work, 0);
  3199. }
  3200. if (bond->params.miimon) { /* link check interval, in milliseconds. */
  3201. INIT_DELAYED_WORK(&bond->mii_work, bond_mii_monitor);
  3202. queue_delayed_work(bond->wq, &bond->mii_work, 0);
  3203. }
  3204. if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
  3205. if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
  3206. INIT_DELAYED_WORK(&bond->arp_work,
  3207. bond_activebackup_arp_mon);
  3208. else
  3209. INIT_DELAYED_WORK(&bond->arp_work,
  3210. bond_loadbalance_arp_mon);
  3211. queue_delayed_work(bond->wq, &bond->arp_work, 0);
  3212. if (bond->params.arp_validate)
  3213. bond_register_arp(bond);
  3214. }
  3215. if (bond->params.mode == BOND_MODE_8023AD) {
  3216. INIT_DELAYED_WORK(&bond->ad_work, bond_3ad_state_machine_handler);
  3217. queue_delayed_work(bond->wq, &bond->ad_work, 0);
  3218. /* register to receive LACPDUs */
  3219. bond_register_lacpdu(bond);
  3220. bond_3ad_initiate_agg_selection(bond, 1);
  3221. }
  3222. return 0;
  3223. }
  3224. static int bond_close(struct net_device *bond_dev)
  3225. {
  3226. struct bonding *bond = netdev_priv(bond_dev);
  3227. if (bond->params.mode == BOND_MODE_8023AD) {
  3228. /* Unregister the receive of LACPDUs */
  3229. bond_unregister_lacpdu(bond);
  3230. }
  3231. if (bond->params.arp_validate)
  3232. bond_unregister_arp(bond);
  3233. write_lock_bh(&bond->lock);
  3234. bond->send_grat_arp = 0;
  3235. bond->send_unsol_na = 0;
  3236. /* signal timers not to re-arm */
  3237. bond->kill_timers = 1;
  3238. write_unlock_bh(&bond->lock);
  3239. if (bond->params.miimon) { /* link check interval, in milliseconds. */
  3240. cancel_delayed_work(&bond->mii_work);
  3241. }
  3242. if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
  3243. cancel_delayed_work(&bond->arp_work);
  3244. }
  3245. switch (bond->params.mode) {
  3246. case BOND_MODE_8023AD:
  3247. cancel_delayed_work(&bond->ad_work);
  3248. break;
  3249. case BOND_MODE_TLB:
  3250. case BOND_MODE_ALB:
  3251. cancel_delayed_work(&bond->alb_work);
  3252. break;
  3253. default:
  3254. break;
  3255. }
  3256. if (bond_is_lb(bond)) {
  3257. /* Must be called only after all
  3258. * slaves have been released
  3259. */
  3260. bond_alb_deinitialize(bond);
  3261. }
  3262. return 0;
  3263. }
  3264. static struct net_device_stats *bond_get_stats(struct net_device *bond_dev)
  3265. {
  3266. struct bonding *bond = netdev_priv(bond_dev);
  3267. struct net_device_stats *stats = &bond->stats;
  3268. struct net_device_stats local_stats;
  3269. struct slave *slave;
  3270. int i;
  3271. memset(&local_stats, 0, sizeof(struct net_device_stats));
  3272. read_lock_bh(&bond->lock);
  3273. bond_for_each_slave(bond, slave, i) {
  3274. const struct net_device_stats *sstats = dev_get_stats(slave->dev);
  3275. local_stats.rx_packets += sstats->rx_packets;
  3276. local_stats.rx_bytes += sstats->rx_bytes;
  3277. local_stats.rx_errors += sstats->rx_errors;
  3278. local_stats.rx_dropped += sstats->rx_dropped;
  3279. local_stats.tx_packets += sstats->tx_packets;
  3280. local_stats.tx_bytes += sstats->tx_bytes;
  3281. local_stats.tx_errors += sstats->tx_errors;
  3282. local_stats.tx_dropped += sstats->tx_dropped;
  3283. local_stats.multicast += sstats->multicast;
  3284. local_stats.collisions += sstats->collisions;
  3285. local_stats.rx_length_errors += sstats->rx_length_errors;
  3286. local_stats.rx_over_errors += sstats->rx_over_errors;
  3287. local_stats.rx_crc_errors += sstats->rx_crc_errors;
  3288. local_stats.rx_frame_errors += sstats->rx_frame_errors;
  3289. local_stats.rx_fifo_errors += sstats->rx_fifo_errors;
  3290. local_stats.rx_missed_errors += sstats->rx_missed_errors;
  3291. local_stats.tx_aborted_errors += sstats->tx_aborted_errors;
  3292. local_stats.tx_carrier_errors += sstats->tx_carrier_errors;
  3293. local_stats.tx_fifo_errors += sstats->tx_fifo_errors;
  3294. local_stats.tx_heartbeat_errors += sstats->tx_heartbeat_errors;
  3295. local_stats.tx_window_errors += sstats->tx_window_errors;
  3296. }
  3297. memcpy(stats, &local_stats, sizeof(struct net_device_stats));
  3298. read_unlock_bh(&bond->lock);
  3299. return stats;
  3300. }
  3301. static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
  3302. {
  3303. struct net_device *slave_dev = NULL;
  3304. struct ifbond k_binfo;
  3305. struct ifbond __user *u_binfo = NULL;
  3306. struct ifslave k_sinfo;
  3307. struct ifslave __user *u_sinfo = NULL;
  3308. struct mii_ioctl_data *mii = NULL;
  3309. int res = 0;
  3310. pr_debug("bond_ioctl: master=%s, cmd=%d\n",
  3311. bond_dev->name, cmd);
  3312. switch (cmd) {
  3313. case SIOCGMIIPHY:
  3314. mii = if_mii(ifr);
  3315. if (!mii) {
  3316. return -EINVAL;
  3317. }
  3318. mii->phy_id = 0;
  3319. /* Fall Through */
  3320. case SIOCGMIIREG:
  3321. /*
  3322. * We do this again just in case we were called by SIOCGMIIREG
  3323. * instead of SIOCGMIIPHY.
  3324. */
  3325. mii = if_mii(ifr);
  3326. if (!mii) {
  3327. return -EINVAL;
  3328. }
  3329. if (mii->reg_num == 1) {
  3330. struct bonding *bond = netdev_priv(bond_dev);
  3331. mii->val_out = 0;
  3332. read_lock(&bond->lock);
  3333. read_lock(&bond->curr_slave_lock);
  3334. if (netif_carrier_ok(bond->dev)) {
  3335. mii->val_out = BMSR_LSTATUS;
  3336. }
  3337. read_unlock(&bond->curr_slave_lock);
  3338. read_unlock(&bond->lock);
  3339. }
  3340. return 0;
  3341. case BOND_INFO_QUERY_OLD:
  3342. case SIOCBONDINFOQUERY:
  3343. u_binfo = (struct ifbond __user *)ifr->ifr_data;
  3344. if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond))) {
  3345. return -EFAULT;
  3346. }
  3347. res = bond_info_query(bond_dev, &k_binfo);
  3348. if (res == 0) {
  3349. if (copy_to_user(u_binfo, &k_binfo, sizeof(ifbond))) {
  3350. return -EFAULT;
  3351. }
  3352. }
  3353. return res;
  3354. case BOND_SLAVE_INFO_QUERY_OLD:
  3355. case SIOCBONDSLAVEINFOQUERY:
  3356. u_sinfo = (struct ifslave __user *)ifr->ifr_data;
  3357. if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave))) {
  3358. return -EFAULT;
  3359. }
  3360. res = bond_slave_info_query(bond_dev, &k_sinfo);
  3361. if (res == 0) {
  3362. if (copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave))) {
  3363. return -EFAULT;
  3364. }
  3365. }
  3366. return res;
  3367. default:
  3368. /* Go on */
  3369. break;
  3370. }
  3371. if (!capable(CAP_NET_ADMIN)) {
  3372. return -EPERM;
  3373. }
  3374. down_write(&(bonding_rwsem));
  3375. slave_dev = dev_get_by_name(&init_net, ifr->ifr_slave);
  3376. pr_debug("slave_dev=%p: \n", slave_dev);
  3377. if (!slave_dev) {
  3378. res = -ENODEV;
  3379. } else {
  3380. pr_debug("slave_dev->name=%s: \n", slave_dev->name);
  3381. switch (cmd) {
  3382. case BOND_ENSLAVE_OLD:
  3383. case SIOCBONDENSLAVE:
  3384. res = bond_enslave(bond_dev, slave_dev);
  3385. break;
  3386. case BOND_RELEASE_OLD:
  3387. case SIOCBONDRELEASE:
  3388. res = bond_release(bond_dev, slave_dev);
  3389. break;
  3390. case BOND_SETHWADDR_OLD:
  3391. case SIOCBONDSETHWADDR:
  3392. res = bond_sethwaddr(bond_dev, slave_dev);
  3393. break;
  3394. case BOND_CHANGE_ACTIVE_OLD:
  3395. case SIOCBONDCHANGEACTIVE:
  3396. res = bond_ioctl_change_active(bond_dev, slave_dev);
  3397. break;
  3398. default:
  3399. res = -EOPNOTSUPP;
  3400. }
  3401. dev_put(slave_dev);
  3402. }
  3403. up_write(&(bonding_rwsem));
  3404. return res;
  3405. }
  3406. static void bond_set_multicast_list(struct net_device *bond_dev)
  3407. {
  3408. struct bonding *bond = netdev_priv(bond_dev);
  3409. struct dev_mc_list *dmi;
  3410. /*
  3411. * Do promisc before checking multicast_mode
  3412. */
  3413. if ((bond_dev->flags & IFF_PROMISC) && !(bond->flags & IFF_PROMISC)) {
  3414. /*
  3415. * FIXME: Need to handle the error when one of the multi-slaves
  3416. * encounters error.
  3417. */
  3418. bond_set_promiscuity(bond, 1);
  3419. }
  3420. if (!(bond_dev->flags & IFF_PROMISC) && (bond->flags & IFF_PROMISC)) {
  3421. bond_set_promiscuity(bond, -1);
  3422. }
  3423. /* set allmulti flag to slaves */
  3424. if ((bond_dev->flags & IFF_ALLMULTI) && !(bond->flags & IFF_ALLMULTI)) {
  3425. /*
  3426. * FIXME: Need to handle the error when one of the multi-slaves
  3427. * encounters error.
  3428. */
  3429. bond_set_allmulti(bond, 1);
  3430. }
  3431. if (!(bond_dev->flags & IFF_ALLMULTI) && (bond->flags & IFF_ALLMULTI)) {
  3432. bond_set_allmulti(bond, -1);
  3433. }
  3434. read_lock(&bond->lock);
  3435. bond->flags = bond_dev->flags;
  3436. /* looking for addresses to add to slaves' mc list */
  3437. for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
  3438. if (!bond_mc_list_find_dmi(dmi, bond->mc_list)) {
  3439. bond_mc_add(bond, dmi->dmi_addr, dmi->dmi_addrlen);
  3440. }
  3441. }
  3442. /* looking for addresses to delete from slaves' list */
  3443. for (dmi = bond->mc_list; dmi; dmi = dmi->next) {
  3444. if (!bond_mc_list_find_dmi(dmi, bond_dev->mc_list)) {
  3445. bond_mc_delete(bond, dmi->dmi_addr, dmi->dmi_addrlen);
  3446. }
  3447. }
  3448. /* save master's multicast list */
  3449. bond_mc_list_destroy(bond);
  3450. bond_mc_list_copy(bond_dev->mc_list, bond, GFP_ATOMIC);
  3451. read_unlock(&bond->lock);
  3452. }
  3453. static int bond_neigh_setup(struct net_device *dev, struct neigh_parms *parms)
  3454. {
  3455. struct bonding *bond = netdev_priv(dev);
  3456. struct slave *slave = bond->first_slave;
  3457. if (slave) {
  3458. const struct net_device_ops *slave_ops
  3459. = slave->dev->netdev_ops;
  3460. if (slave_ops->ndo_neigh_setup)
  3461. return slave_ops->ndo_neigh_setup(dev, parms);
  3462. }
  3463. return 0;
  3464. }
  3465. /*
  3466. * Change the MTU of all of a master's slaves to match the master
  3467. */
  3468. static int bond_change_mtu(struct net_device *bond_dev, int new_mtu)
  3469. {
  3470. struct bonding *bond = netdev_priv(bond_dev);
  3471. struct slave *slave, *stop_at;
  3472. int res = 0;
  3473. int i;
  3474. pr_debug("bond=%p, name=%s, new_mtu=%d\n", bond,
  3475. (bond_dev ? bond_dev->name : "None"), new_mtu);
  3476. /* Can't hold bond->lock with bh disabled here since
  3477. * some base drivers panic. On the other hand we can't
  3478. * hold bond->lock without bh disabled because we'll
  3479. * deadlock. The only solution is to rely on the fact
  3480. * that we're under rtnl_lock here, and the slaves
  3481. * list won't change. This doesn't solve the problem
  3482. * of setting the slave's MTU while it is
  3483. * transmitting, but the assumption is that the base
  3484. * driver can handle that.
  3485. *
  3486. * TODO: figure out a way to safely iterate the slaves
  3487. * list, but without holding a lock around the actual
  3488. * call to the base driver.
  3489. */
  3490. bond_for_each_slave(bond, slave, i) {
  3491. pr_debug("s %p s->p %p c_m %p\n", slave,
  3492. slave->prev, slave->dev->change_mtu);
  3493. res = dev_set_mtu(slave->dev, new_mtu);
  3494. if (res) {
  3495. /* If we failed to set the slave's mtu to the new value
  3496. * we must abort the operation even in ACTIVE_BACKUP
  3497. * mode, because if we allow the backup slaves to have
  3498. * different mtu values than the active slave we'll
  3499. * need to change their mtu when doing a failover. That
  3500. * means changing their mtu from timer context, which
  3501. * is probably not a good idea.
  3502. */
  3503. pr_debug("err %d %s\n", res, slave->dev->name);
  3504. goto unwind;
  3505. }
  3506. }
  3507. bond_dev->mtu = new_mtu;
  3508. return 0;
  3509. unwind:
  3510. /* unwind from head to the slave that failed */
  3511. stop_at = slave;
  3512. bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
  3513. int tmp_res;
  3514. tmp_res = dev_set_mtu(slave->dev, bond_dev->mtu);
  3515. if (tmp_res) {
  3516. pr_debug("unwind err %d dev %s\n", tmp_res,
  3517. slave->dev->name);
  3518. }
  3519. }
  3520. return res;
  3521. }
  3522. /*
  3523. * Change HW address
  3524. *
  3525. * Note that many devices must be down to change the HW address, and
  3526. * downing the master releases all slaves. We can make bonds full of
  3527. * bonding devices to test this, however.
  3528. */
  3529. static int bond_set_mac_address(struct net_device *bond_dev, void *addr)
  3530. {
  3531. struct bonding *bond = netdev_priv(bond_dev);
  3532. struct sockaddr *sa = addr, tmp_sa;
  3533. struct slave *slave, *stop_at;
  3534. int res = 0;
  3535. int i;
  3536. if (bond->params.mode == BOND_MODE_ALB)
  3537. return bond_alb_set_mac_address(bond_dev, addr);
  3538. pr_debug("bond=%p, name=%s\n", bond, (bond_dev ? bond_dev->name : "None"));
  3539. /*
  3540. * If fail_over_mac is set to active, do nothing and return
  3541. * success. Returning an error causes ifenslave to fail.
  3542. */
  3543. if (bond->params.fail_over_mac == BOND_FOM_ACTIVE)
  3544. return 0;
  3545. if (!is_valid_ether_addr(sa->sa_data)) {
  3546. return -EADDRNOTAVAIL;
  3547. }
  3548. /* Can't hold bond->lock with bh disabled here since
  3549. * some base drivers panic. On the other hand we can't
  3550. * hold bond->lock without bh disabled because we'll
  3551. * deadlock. The only solution is to rely on the fact
  3552. * that we're under rtnl_lock here, and the slaves
  3553. * list won't change. This doesn't solve the problem
  3554. * of setting the slave's hw address while it is
  3555. * transmitting, but the assumption is that the base
  3556. * driver can handle that.
  3557. *
  3558. * TODO: figure out a way to safely iterate the slaves
  3559. * list, but without holding a lock around the actual
  3560. * call to the base driver.
  3561. */
  3562. bond_for_each_slave(bond, slave, i) {
  3563. const struct net_device_ops *slave_ops = slave->dev->netdev_ops;
  3564. pr_debug("slave %p %s\n", slave, slave->dev->name);
  3565. if (slave_ops->ndo_set_mac_address == NULL) {
  3566. res = -EOPNOTSUPP;
  3567. pr_debug("EOPNOTSUPP %s\n", slave->dev->name);
  3568. goto unwind;
  3569. }
  3570. res = dev_set_mac_address(slave->dev, addr);
  3571. if (res) {
  3572. /* TODO: consider downing the slave
  3573. * and retry ?
  3574. * User should expect communications
  3575. * breakage anyway until ARP finish
  3576. * updating, so...
  3577. */
  3578. pr_debug("err %d %s\n", res, slave->dev->name);
  3579. goto unwind;
  3580. }
  3581. }
  3582. /* success */
  3583. memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
  3584. return 0;
  3585. unwind:
  3586. memcpy(tmp_sa.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
  3587. tmp_sa.sa_family = bond_dev->type;
  3588. /* unwind from head to the slave that failed */
  3589. stop_at = slave;
  3590. bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
  3591. int tmp_res;
  3592. tmp_res = dev_set_mac_address(slave->dev, &tmp_sa);
  3593. if (tmp_res) {
  3594. pr_debug("unwind err %d dev %s\n", tmp_res,
  3595. slave->dev->name);
  3596. }
  3597. }
  3598. return res;
  3599. }
  3600. static int bond_xmit_roundrobin(struct sk_buff *skb, struct net_device *bond_dev)
  3601. {
  3602. struct bonding *bond = netdev_priv(bond_dev);
  3603. struct slave *slave, *start_at;
  3604. int i, slave_no, res = 1;
  3605. read_lock(&bond->lock);
  3606. if (!BOND_IS_OK(bond)) {
  3607. goto out;
  3608. }
  3609. /*
  3610. * Concurrent TX may collide on rr_tx_counter; we accept that
  3611. * as being rare enough not to justify using an atomic op here
  3612. */
  3613. slave_no = bond->rr_tx_counter++ % bond->slave_cnt;
  3614. bond_for_each_slave(bond, slave, i) {
  3615. slave_no--;
  3616. if (slave_no < 0) {
  3617. break;
  3618. }
  3619. }
  3620. start_at = slave;
  3621. bond_for_each_slave_from(bond, slave, i, start_at) {
  3622. if (IS_UP(slave->dev) &&
  3623. (slave->link == BOND_LINK_UP) &&
  3624. (slave->state == BOND_STATE_ACTIVE)) {
  3625. res = bond_dev_queue_xmit(bond, skb, slave->dev);
  3626. break;
  3627. }
  3628. }
  3629. out:
  3630. if (res) {
  3631. /* no suitable interface, frame not sent */
  3632. dev_kfree_skb(skb);
  3633. }
  3634. read_unlock(&bond->lock);
  3635. return 0;
  3636. }
  3637. /*
  3638. * in active-backup mode, we know that bond->curr_active_slave is always valid if
  3639. * the bond has a usable interface.
  3640. */
  3641. static int bond_xmit_activebackup(struct sk_buff *skb, struct net_device *bond_dev)
  3642. {
  3643. struct bonding *bond = netdev_priv(bond_dev);
  3644. int res = 1;
  3645. read_lock(&bond->lock);
  3646. read_lock(&bond->curr_slave_lock);
  3647. if (!BOND_IS_OK(bond)) {
  3648. goto out;
  3649. }
  3650. if (!bond->curr_active_slave)
  3651. goto out;
  3652. res = bond_dev_queue_xmit(bond, skb, bond->curr_active_slave->dev);
  3653. out:
  3654. if (res) {
  3655. /* no suitable interface, frame not sent */
  3656. dev_kfree_skb(skb);
  3657. }
  3658. read_unlock(&bond->curr_slave_lock);
  3659. read_unlock(&bond->lock);
  3660. return 0;
  3661. }
  3662. /*
  3663. * In bond_xmit_xor() , we determine the output device by using a pre-
  3664. * determined xmit_hash_policy(), If the selected device is not enabled,
  3665. * find the next active slave.
  3666. */
  3667. static int bond_xmit_xor(struct sk_buff *skb, struct net_device *bond_dev)
  3668. {
  3669. struct bonding *bond = netdev_priv(bond_dev);
  3670. struct slave *slave, *start_at;
  3671. int slave_no;
  3672. int i;
  3673. int res = 1;
  3674. read_lock(&bond->lock);
  3675. if (!BOND_IS_OK(bond)) {
  3676. goto out;
  3677. }
  3678. slave_no = bond->xmit_hash_policy(skb, bond_dev, bond->slave_cnt);
  3679. bond_for_each_slave(bond, slave, i) {
  3680. slave_no--;
  3681. if (slave_no < 0) {
  3682. break;
  3683. }
  3684. }
  3685. start_at = slave;
  3686. bond_for_each_slave_from(bond, slave, i, start_at) {
  3687. if (IS_UP(slave->dev) &&
  3688. (slave->link == BOND_LINK_UP) &&
  3689. (slave->state == BOND_STATE_ACTIVE)) {
  3690. res = bond_dev_queue_xmit(bond, skb, slave->dev);
  3691. break;
  3692. }
  3693. }
  3694. out:
  3695. if (res) {
  3696. /* no suitable interface, frame not sent */
  3697. dev_kfree_skb(skb);
  3698. }
  3699. read_unlock(&bond->lock);
  3700. return 0;
  3701. }
  3702. /*
  3703. * in broadcast mode, we send everything to all usable interfaces.
  3704. */
  3705. static int bond_xmit_broadcast(struct sk_buff *skb, struct net_device *bond_dev)
  3706. {
  3707. struct bonding *bond = netdev_priv(bond_dev);
  3708. struct slave *slave, *start_at;
  3709. struct net_device *tx_dev = NULL;
  3710. int i;
  3711. int res = 1;
  3712. read_lock(&bond->lock);
  3713. if (!BOND_IS_OK(bond)) {
  3714. goto out;
  3715. }
  3716. read_lock(&bond->curr_slave_lock);
  3717. start_at = bond->curr_active_slave;
  3718. read_unlock(&bond->curr_slave_lock);
  3719. if (!start_at) {
  3720. goto out;
  3721. }
  3722. bond_for_each_slave_from(bond, slave, i, start_at) {
  3723. if (IS_UP(slave->dev) &&
  3724. (slave->link == BOND_LINK_UP) &&
  3725. (slave->state == BOND_STATE_ACTIVE)) {
  3726. if (tx_dev) {
  3727. struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
  3728. if (!skb2) {
  3729. printk(KERN_ERR DRV_NAME
  3730. ": %s: Error: bond_xmit_broadcast(): "
  3731. "skb_clone() failed\n",
  3732. bond_dev->name);
  3733. continue;
  3734. }
  3735. res = bond_dev_queue_xmit(bond, skb2, tx_dev);
  3736. if (res) {
  3737. dev_kfree_skb(skb2);
  3738. continue;
  3739. }
  3740. }
  3741. tx_dev = slave->dev;
  3742. }
  3743. }
  3744. if (tx_dev) {
  3745. res = bond_dev_queue_xmit(bond, skb, tx_dev);
  3746. }
  3747. out:
  3748. if (res) {
  3749. /* no suitable interface, frame not sent */
  3750. dev_kfree_skb(skb);
  3751. }
  3752. /* frame sent to all suitable interfaces */
  3753. read_unlock(&bond->lock);
  3754. return 0;
  3755. }
  3756. /*------------------------- Device initialization ---------------------------*/
  3757. static void bond_set_xmit_hash_policy(struct bonding *bond)
  3758. {
  3759. switch (bond->params.xmit_policy) {
  3760. case BOND_XMIT_POLICY_LAYER23:
  3761. bond->xmit_hash_policy = bond_xmit_hash_policy_l23;
  3762. break;
  3763. case BOND_XMIT_POLICY_LAYER34:
  3764. bond->xmit_hash_policy = bond_xmit_hash_policy_l34;
  3765. break;
  3766. case BOND_XMIT_POLICY_LAYER2:
  3767. default:
  3768. bond->xmit_hash_policy = bond_xmit_hash_policy_l2;
  3769. break;
  3770. }
  3771. }
  3772. static int bond_start_xmit(struct sk_buff *skb, struct net_device *dev)
  3773. {
  3774. const struct bonding *bond = netdev_priv(dev);
  3775. switch (bond->params.mode) {
  3776. case BOND_MODE_ROUNDROBIN:
  3777. return bond_xmit_roundrobin(skb, dev);
  3778. case BOND_MODE_ACTIVEBACKUP:
  3779. return bond_xmit_activebackup(skb, dev);
  3780. case BOND_MODE_XOR:
  3781. return bond_xmit_xor(skb, dev);
  3782. case BOND_MODE_BROADCAST:
  3783. return bond_xmit_broadcast(skb, dev);
  3784. case BOND_MODE_8023AD:
  3785. return bond_3ad_xmit_xor(skb, dev);
  3786. case BOND_MODE_ALB:
  3787. case BOND_MODE_TLB:
  3788. return bond_alb_xmit(skb, dev);
  3789. default:
  3790. /* Should never happen, mode already checked */
  3791. printk(KERN_ERR DRV_NAME ": %s: Error: Unknown bonding mode %d\n",
  3792. dev->name, bond->params.mode);
  3793. WARN_ON_ONCE(1);
  3794. dev_kfree_skb(skb);
  3795. return NETDEV_TX_OK;
  3796. }
  3797. }
  3798. /*
  3799. * set bond mode specific net device operations
  3800. */
  3801. void bond_set_mode_ops(struct bonding *bond, int mode)
  3802. {
  3803. struct net_device *bond_dev = bond->dev;
  3804. switch (mode) {
  3805. case BOND_MODE_ROUNDROBIN:
  3806. break;
  3807. case BOND_MODE_ACTIVEBACKUP:
  3808. break;
  3809. case BOND_MODE_XOR:
  3810. bond_set_xmit_hash_policy(bond);
  3811. break;
  3812. case BOND_MODE_BROADCAST:
  3813. break;
  3814. case BOND_MODE_8023AD:
  3815. bond_set_master_3ad_flags(bond);
  3816. bond_set_xmit_hash_policy(bond);
  3817. break;
  3818. case BOND_MODE_ALB:
  3819. bond_set_master_alb_flags(bond);
  3820. /* FALLTHRU */
  3821. case BOND_MODE_TLB:
  3822. break;
  3823. default:
  3824. /* Should never happen, mode already checked */
  3825. printk(KERN_ERR DRV_NAME
  3826. ": %s: Error: Unknown bonding mode %d\n",
  3827. bond_dev->name,
  3828. mode);
  3829. break;
  3830. }
  3831. }
  3832. static void bond_ethtool_get_drvinfo(struct net_device *bond_dev,
  3833. struct ethtool_drvinfo *drvinfo)
  3834. {
  3835. strncpy(drvinfo->driver, DRV_NAME, 32);
  3836. strncpy(drvinfo->version, DRV_VERSION, 32);
  3837. snprintf(drvinfo->fw_version, 32, "%d", BOND_ABI_VERSION);
  3838. }
  3839. static const struct ethtool_ops bond_ethtool_ops = {
  3840. .get_drvinfo = bond_ethtool_get_drvinfo,
  3841. .get_link = ethtool_op_get_link,
  3842. .get_tx_csum = ethtool_op_get_tx_csum,
  3843. .get_sg = ethtool_op_get_sg,
  3844. .get_tso = ethtool_op_get_tso,
  3845. .get_ufo = ethtool_op_get_ufo,
  3846. .get_flags = ethtool_op_get_flags,
  3847. };
  3848. static const struct net_device_ops bond_netdev_ops = {
  3849. .ndo_open = bond_open,
  3850. .ndo_stop = bond_close,
  3851. .ndo_start_xmit = bond_start_xmit,
  3852. .ndo_get_stats = bond_get_stats,
  3853. .ndo_do_ioctl = bond_do_ioctl,
  3854. .ndo_set_multicast_list = bond_set_multicast_list,
  3855. .ndo_change_mtu = bond_change_mtu,
  3856. .ndo_set_mac_address = bond_set_mac_address,
  3857. .ndo_neigh_setup = bond_neigh_setup,
  3858. .ndo_vlan_rx_register = bond_vlan_rx_register,
  3859. .ndo_vlan_rx_add_vid = bond_vlan_rx_add_vid,
  3860. .ndo_vlan_rx_kill_vid = bond_vlan_rx_kill_vid,
  3861. };
  3862. /*
  3863. * Does not allocate but creates a /proc entry.
  3864. * Allowed to fail.
  3865. */
  3866. static int bond_init(struct net_device *bond_dev, struct bond_params *params)
  3867. {
  3868. struct bonding *bond = netdev_priv(bond_dev);
  3869. pr_debug("Begin bond_init for %s\n", bond_dev->name);
  3870. /* initialize rwlocks */
  3871. rwlock_init(&bond->lock);
  3872. rwlock_init(&bond->curr_slave_lock);
  3873. bond->params = *params; /* copy params struct */
  3874. bond->wq = create_singlethread_workqueue(bond_dev->name);
  3875. if (!bond->wq)
  3876. return -ENOMEM;
  3877. /* Initialize pointers */
  3878. bond->first_slave = NULL;
  3879. bond->curr_active_slave = NULL;
  3880. bond->current_arp_slave = NULL;
  3881. bond->primary_slave = NULL;
  3882. bond->dev = bond_dev;
  3883. bond->send_grat_arp = 0;
  3884. bond->send_unsol_na = 0;
  3885. bond->setup_by_slave = 0;
  3886. INIT_LIST_HEAD(&bond->vlan_list);
  3887. /* Initialize the device entry points */
  3888. bond_dev->netdev_ops = &bond_netdev_ops;
  3889. bond_dev->ethtool_ops = &bond_ethtool_ops;
  3890. bond_set_mode_ops(bond, bond->params.mode);
  3891. bond_dev->destructor = bond_destructor;
  3892. /* Initialize the device options */
  3893. bond_dev->tx_queue_len = 0;
  3894. bond_dev->flags |= IFF_MASTER|IFF_MULTICAST;
  3895. bond_dev->priv_flags |= IFF_BONDING;
  3896. if (bond->params.arp_interval)
  3897. bond_dev->priv_flags |= IFF_MASTER_ARPMON;
  3898. /* At first, we block adding VLANs. That's the only way to
  3899. * prevent problems that occur when adding VLANs over an
  3900. * empty bond. The block will be removed once non-challenged
  3901. * slaves are enslaved.
  3902. */
  3903. bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
  3904. /* don't acquire bond device's netif_tx_lock when
  3905. * transmitting */
  3906. bond_dev->features |= NETIF_F_LLTX;
  3907. /* By default, we declare the bond to be fully
  3908. * VLAN hardware accelerated capable. Special
  3909. * care is taken in the various xmit functions
  3910. * when there are slaves that are not hw accel
  3911. * capable
  3912. */
  3913. bond_dev->features |= (NETIF_F_HW_VLAN_TX |
  3914. NETIF_F_HW_VLAN_RX |
  3915. NETIF_F_HW_VLAN_FILTER);
  3916. #ifdef CONFIG_PROC_FS
  3917. bond_create_proc_entry(bond);
  3918. #endif
  3919. list_add_tail(&bond->bond_list, &bond_dev_list);
  3920. return 0;
  3921. }
  3922. static void bond_work_cancel_all(struct bonding *bond)
  3923. {
  3924. write_lock_bh(&bond->lock);
  3925. bond->kill_timers = 1;
  3926. write_unlock_bh(&bond->lock);
  3927. if (bond->params.miimon && delayed_work_pending(&bond->mii_work))
  3928. cancel_delayed_work(&bond->mii_work);
  3929. if (bond->params.arp_interval && delayed_work_pending(&bond->arp_work))
  3930. cancel_delayed_work(&bond->arp_work);
  3931. if (bond->params.mode == BOND_MODE_ALB &&
  3932. delayed_work_pending(&bond->alb_work))
  3933. cancel_delayed_work(&bond->alb_work);
  3934. if (bond->params.mode == BOND_MODE_8023AD &&
  3935. delayed_work_pending(&bond->ad_work))
  3936. cancel_delayed_work(&bond->ad_work);
  3937. }
  3938. /* De-initialize device specific data.
  3939. * Caller must hold rtnl_lock.
  3940. */
  3941. static void bond_deinit(struct net_device *bond_dev)
  3942. {
  3943. struct bonding *bond = netdev_priv(bond_dev);
  3944. list_del(&bond->bond_list);
  3945. bond_work_cancel_all(bond);
  3946. #ifdef CONFIG_PROC_FS
  3947. bond_remove_proc_entry(bond);
  3948. #endif
  3949. }
  3950. /* Unregister and free all bond devices.
  3951. * Caller must hold rtnl_lock.
  3952. */
  3953. static void bond_free_all(void)
  3954. {
  3955. struct bonding *bond, *nxt;
  3956. list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list) {
  3957. struct net_device *bond_dev = bond->dev;
  3958. bond_work_cancel_all(bond);
  3959. /* Release the bonded slaves */
  3960. bond_release_all(bond_dev);
  3961. bond_destroy(bond);
  3962. }
  3963. #ifdef CONFIG_PROC_FS
  3964. bond_destroy_proc_dir();
  3965. #endif
  3966. }
  3967. /*------------------------- Module initialization ---------------------------*/
  3968. /*
  3969. * Convert string input module parms. Accept either the
  3970. * number of the mode or its string name. A bit complicated because
  3971. * some mode names are substrings of other names, and calls from sysfs
  3972. * may have whitespace in the name (trailing newlines, for example).
  3973. */
  3974. int bond_parse_parm(const char *buf, const struct bond_parm_tbl *tbl)
  3975. {
  3976. int mode = -1, i, rv;
  3977. char *p, modestr[BOND_MAX_MODENAME_LEN + 1] = { 0, };
  3978. for (p = (char *)buf; *p; p++)
  3979. if (!(isdigit(*p) || isspace(*p)))
  3980. break;
  3981. if (*p)
  3982. rv = sscanf(buf, "%20s", modestr);
  3983. else
  3984. rv = sscanf(buf, "%d", &mode);
  3985. if (!rv)
  3986. return -1;
  3987. for (i = 0; tbl[i].modename; i++) {
  3988. if (mode == tbl[i].mode)
  3989. return tbl[i].mode;
  3990. if (strcmp(modestr, tbl[i].modename) == 0)
  3991. return tbl[i].mode;
  3992. }
  3993. return -1;
  3994. }
  3995. static int bond_check_params(struct bond_params *params)
  3996. {
  3997. int arp_validate_value, fail_over_mac_value;
  3998. /*
  3999. * Convert string parameters.
  4000. */
  4001. if (mode) {
  4002. bond_mode = bond_parse_parm(mode, bond_mode_tbl);
  4003. if (bond_mode == -1) {
  4004. printk(KERN_ERR DRV_NAME
  4005. ": Error: Invalid bonding mode \"%s\"\n",
  4006. mode == NULL ? "NULL" : mode);
  4007. return -EINVAL;
  4008. }
  4009. }
  4010. if (xmit_hash_policy) {
  4011. if ((bond_mode != BOND_MODE_XOR) &&
  4012. (bond_mode != BOND_MODE_8023AD)) {
  4013. printk(KERN_INFO DRV_NAME
  4014. ": xor_mode param is irrelevant in mode %s\n",
  4015. bond_mode_name(bond_mode));
  4016. } else {
  4017. xmit_hashtype = bond_parse_parm(xmit_hash_policy,
  4018. xmit_hashtype_tbl);
  4019. if (xmit_hashtype == -1) {
  4020. printk(KERN_ERR DRV_NAME
  4021. ": Error: Invalid xmit_hash_policy \"%s\"\n",
  4022. xmit_hash_policy == NULL ? "NULL" :
  4023. xmit_hash_policy);
  4024. return -EINVAL;
  4025. }
  4026. }
  4027. }
  4028. if (lacp_rate) {
  4029. if (bond_mode != BOND_MODE_8023AD) {
  4030. printk(KERN_INFO DRV_NAME
  4031. ": lacp_rate param is irrelevant in mode %s\n",
  4032. bond_mode_name(bond_mode));
  4033. } else {
  4034. lacp_fast = bond_parse_parm(lacp_rate, bond_lacp_tbl);
  4035. if (lacp_fast == -1) {
  4036. printk(KERN_ERR DRV_NAME
  4037. ": Error: Invalid lacp rate \"%s\"\n",
  4038. lacp_rate == NULL ? "NULL" : lacp_rate);
  4039. return -EINVAL;
  4040. }
  4041. }
  4042. }
  4043. if (ad_select) {
  4044. params->ad_select = bond_parse_parm(ad_select, ad_select_tbl);
  4045. if (params->ad_select == -1) {
  4046. printk(KERN_ERR DRV_NAME
  4047. ": Error: Invalid ad_select \"%s\"\n",
  4048. ad_select == NULL ? "NULL" : ad_select);
  4049. return -EINVAL;
  4050. }
  4051. if (bond_mode != BOND_MODE_8023AD) {
  4052. printk(KERN_WARNING DRV_NAME
  4053. ": ad_select param only affects 802.3ad mode\n");
  4054. }
  4055. } else {
  4056. params->ad_select = BOND_AD_STABLE;
  4057. }
  4058. if (max_bonds < 0 || max_bonds > INT_MAX) {
  4059. printk(KERN_WARNING DRV_NAME
  4060. ": Warning: max_bonds (%d) not in range %d-%d, so it "
  4061. "was reset to BOND_DEFAULT_MAX_BONDS (%d)\n",
  4062. max_bonds, 0, INT_MAX, BOND_DEFAULT_MAX_BONDS);
  4063. max_bonds = BOND_DEFAULT_MAX_BONDS;
  4064. }
  4065. if (miimon < 0) {
  4066. printk(KERN_WARNING DRV_NAME
  4067. ": Warning: miimon module parameter (%d), "
  4068. "not in range 0-%d, so it was reset to %d\n",
  4069. miimon, INT_MAX, BOND_LINK_MON_INTERV);
  4070. miimon = BOND_LINK_MON_INTERV;
  4071. }
  4072. if (updelay < 0) {
  4073. printk(KERN_WARNING DRV_NAME
  4074. ": Warning: updelay module parameter (%d), "
  4075. "not in range 0-%d, so it was reset to 0\n",
  4076. updelay, INT_MAX);
  4077. updelay = 0;
  4078. }
  4079. if (downdelay < 0) {
  4080. printk(KERN_WARNING DRV_NAME
  4081. ": Warning: downdelay module parameter (%d), "
  4082. "not in range 0-%d, so it was reset to 0\n",
  4083. downdelay, INT_MAX);
  4084. downdelay = 0;
  4085. }
  4086. if ((use_carrier != 0) && (use_carrier != 1)) {
  4087. printk(KERN_WARNING DRV_NAME
  4088. ": Warning: use_carrier module parameter (%d), "
  4089. "not of valid value (0/1), so it was set to 1\n",
  4090. use_carrier);
  4091. use_carrier = 1;
  4092. }
  4093. if (num_grat_arp < 0 || num_grat_arp > 255) {
  4094. printk(KERN_WARNING DRV_NAME
  4095. ": Warning: num_grat_arp (%d) not in range 0-255 so it "
  4096. "was reset to 1 \n", num_grat_arp);
  4097. num_grat_arp = 1;
  4098. }
  4099. if (num_unsol_na < 0 || num_unsol_na > 255) {
  4100. printk(KERN_WARNING DRV_NAME
  4101. ": Warning: num_unsol_na (%d) not in range 0-255 so it "
  4102. "was reset to 1 \n", num_unsol_na);
  4103. num_unsol_na = 1;
  4104. }
  4105. /* reset values for 802.3ad */
  4106. if (bond_mode == BOND_MODE_8023AD) {
  4107. if (!miimon) {
  4108. printk(KERN_WARNING DRV_NAME
  4109. ": Warning: miimon must be specified, "
  4110. "otherwise bonding will not detect link "
  4111. "failure, speed and duplex which are "
  4112. "essential for 802.3ad operation\n");
  4113. printk(KERN_WARNING "Forcing miimon to 100msec\n");
  4114. miimon = 100;
  4115. }
  4116. }
  4117. /* reset values for TLB/ALB */
  4118. if ((bond_mode == BOND_MODE_TLB) ||
  4119. (bond_mode == BOND_MODE_ALB)) {
  4120. if (!miimon) {
  4121. printk(KERN_WARNING DRV_NAME
  4122. ": Warning: miimon must be specified, "
  4123. "otherwise bonding will not detect link "
  4124. "failure and link speed which are essential "
  4125. "for TLB/ALB load balancing\n");
  4126. printk(KERN_WARNING "Forcing miimon to 100msec\n");
  4127. miimon = 100;
  4128. }
  4129. }
  4130. if (bond_mode == BOND_MODE_ALB) {
  4131. printk(KERN_NOTICE DRV_NAME
  4132. ": In ALB mode you might experience client "
  4133. "disconnections upon reconnection of a link if the "
  4134. "bonding module updelay parameter (%d msec) is "
  4135. "incompatible with the forwarding delay time of the "
  4136. "switch\n",
  4137. updelay);
  4138. }
  4139. if (!miimon) {
  4140. if (updelay || downdelay) {
  4141. /* just warn the user the up/down delay will have
  4142. * no effect since miimon is zero...
  4143. */
  4144. printk(KERN_WARNING DRV_NAME
  4145. ": Warning: miimon module parameter not set "
  4146. "and updelay (%d) or downdelay (%d) module "
  4147. "parameter is set; updelay and downdelay have "
  4148. "no effect unless miimon is set\n",
  4149. updelay, downdelay);
  4150. }
  4151. } else {
  4152. /* don't allow arp monitoring */
  4153. if (arp_interval) {
  4154. printk(KERN_WARNING DRV_NAME
  4155. ": Warning: miimon (%d) and arp_interval (%d) "
  4156. "can't be used simultaneously, disabling ARP "
  4157. "monitoring\n",
  4158. miimon, arp_interval);
  4159. arp_interval = 0;
  4160. }
  4161. if ((updelay % miimon) != 0) {
  4162. printk(KERN_WARNING DRV_NAME
  4163. ": Warning: updelay (%d) is not a multiple "
  4164. "of miimon (%d), updelay rounded to %d ms\n",
  4165. updelay, miimon, (updelay / miimon) * miimon);
  4166. }
  4167. updelay /= miimon;
  4168. if ((downdelay % miimon) != 0) {
  4169. printk(KERN_WARNING DRV_NAME
  4170. ": Warning: downdelay (%d) is not a multiple "
  4171. "of miimon (%d), downdelay rounded to %d ms\n",
  4172. downdelay, miimon,
  4173. (downdelay / miimon) * miimon);
  4174. }
  4175. downdelay /= miimon;
  4176. }
  4177. if (arp_interval < 0) {
  4178. printk(KERN_WARNING DRV_NAME
  4179. ": Warning: arp_interval module parameter (%d) "
  4180. ", not in range 0-%d, so it was reset to %d\n",
  4181. arp_interval, INT_MAX, BOND_LINK_ARP_INTERV);
  4182. arp_interval = BOND_LINK_ARP_INTERV;
  4183. }
  4184. for (arp_ip_count = 0;
  4185. (arp_ip_count < BOND_MAX_ARP_TARGETS) && arp_ip_target[arp_ip_count];
  4186. arp_ip_count++) {
  4187. /* not complete check, but should be good enough to
  4188. catch mistakes */
  4189. if (!isdigit(arp_ip_target[arp_ip_count][0])) {
  4190. printk(KERN_WARNING DRV_NAME
  4191. ": Warning: bad arp_ip_target module parameter "
  4192. "(%s), ARP monitoring will not be performed\n",
  4193. arp_ip_target[arp_ip_count]);
  4194. arp_interval = 0;
  4195. } else {
  4196. __be32 ip = in_aton(arp_ip_target[arp_ip_count]);
  4197. arp_target[arp_ip_count] = ip;
  4198. }
  4199. }
  4200. if (arp_interval && !arp_ip_count) {
  4201. /* don't allow arping if no arp_ip_target given... */
  4202. printk(KERN_WARNING DRV_NAME
  4203. ": Warning: arp_interval module parameter (%d) "
  4204. "specified without providing an arp_ip_target "
  4205. "parameter, arp_interval was reset to 0\n",
  4206. arp_interval);
  4207. arp_interval = 0;
  4208. }
  4209. if (arp_validate) {
  4210. if (bond_mode != BOND_MODE_ACTIVEBACKUP) {
  4211. printk(KERN_ERR DRV_NAME
  4212. ": arp_validate only supported in active-backup mode\n");
  4213. return -EINVAL;
  4214. }
  4215. if (!arp_interval) {
  4216. printk(KERN_ERR DRV_NAME
  4217. ": arp_validate requires arp_interval\n");
  4218. return -EINVAL;
  4219. }
  4220. arp_validate_value = bond_parse_parm(arp_validate,
  4221. arp_validate_tbl);
  4222. if (arp_validate_value == -1) {
  4223. printk(KERN_ERR DRV_NAME
  4224. ": Error: invalid arp_validate \"%s\"\n",
  4225. arp_validate == NULL ? "NULL" : arp_validate);
  4226. return -EINVAL;
  4227. }
  4228. } else
  4229. arp_validate_value = 0;
  4230. if (miimon) {
  4231. printk(KERN_INFO DRV_NAME
  4232. ": MII link monitoring set to %d ms\n",
  4233. miimon);
  4234. } else if (arp_interval) {
  4235. int i;
  4236. printk(KERN_INFO DRV_NAME
  4237. ": ARP monitoring set to %d ms, validate %s, with %d target(s):",
  4238. arp_interval,
  4239. arp_validate_tbl[arp_validate_value].modename,
  4240. arp_ip_count);
  4241. for (i = 0; i < arp_ip_count; i++)
  4242. printk (" %s", arp_ip_target[i]);
  4243. printk("\n");
  4244. } else if (max_bonds) {
  4245. /* miimon and arp_interval not set, we need one so things
  4246. * work as expected, see bonding.txt for details
  4247. */
  4248. printk(KERN_WARNING DRV_NAME
  4249. ": Warning: either miimon or arp_interval and "
  4250. "arp_ip_target module parameters must be specified, "
  4251. "otherwise bonding will not detect link failures! see "
  4252. "bonding.txt for details.\n");
  4253. }
  4254. if (primary && !USES_PRIMARY(bond_mode)) {
  4255. /* currently, using a primary only makes sense
  4256. * in active backup, TLB or ALB modes
  4257. */
  4258. printk(KERN_WARNING DRV_NAME
  4259. ": Warning: %s primary device specified but has no "
  4260. "effect in %s mode\n",
  4261. primary, bond_mode_name(bond_mode));
  4262. primary = NULL;
  4263. }
  4264. if (fail_over_mac) {
  4265. fail_over_mac_value = bond_parse_parm(fail_over_mac,
  4266. fail_over_mac_tbl);
  4267. if (fail_over_mac_value == -1) {
  4268. printk(KERN_ERR DRV_NAME
  4269. ": Error: invalid fail_over_mac \"%s\"\n",
  4270. arp_validate == NULL ? "NULL" : arp_validate);
  4271. return -EINVAL;
  4272. }
  4273. if (bond_mode != BOND_MODE_ACTIVEBACKUP)
  4274. printk(KERN_WARNING DRV_NAME
  4275. ": Warning: fail_over_mac only affects "
  4276. "active-backup mode.\n");
  4277. } else {
  4278. fail_over_mac_value = BOND_FOM_NONE;
  4279. }
  4280. /* fill params struct with the proper values */
  4281. params->mode = bond_mode;
  4282. params->xmit_policy = xmit_hashtype;
  4283. params->miimon = miimon;
  4284. params->num_grat_arp = num_grat_arp;
  4285. params->num_unsol_na = num_unsol_na;
  4286. params->arp_interval = arp_interval;
  4287. params->arp_validate = arp_validate_value;
  4288. params->updelay = updelay;
  4289. params->downdelay = downdelay;
  4290. params->use_carrier = use_carrier;
  4291. params->lacp_fast = lacp_fast;
  4292. params->primary[0] = 0;
  4293. params->fail_over_mac = fail_over_mac_value;
  4294. if (primary) {
  4295. strncpy(params->primary, primary, IFNAMSIZ);
  4296. params->primary[IFNAMSIZ - 1] = 0;
  4297. }
  4298. memcpy(params->arp_targets, arp_target, sizeof(arp_target));
  4299. return 0;
  4300. }
  4301. static struct lock_class_key bonding_netdev_xmit_lock_key;
  4302. static struct lock_class_key bonding_netdev_addr_lock_key;
  4303. static void bond_set_lockdep_class_one(struct net_device *dev,
  4304. struct netdev_queue *txq,
  4305. void *_unused)
  4306. {
  4307. lockdep_set_class(&txq->_xmit_lock,
  4308. &bonding_netdev_xmit_lock_key);
  4309. }
  4310. static void bond_set_lockdep_class(struct net_device *dev)
  4311. {
  4312. lockdep_set_class(&dev->addr_list_lock,
  4313. &bonding_netdev_addr_lock_key);
  4314. netdev_for_each_tx_queue(dev, bond_set_lockdep_class_one, NULL);
  4315. }
  4316. /* Create a new bond based on the specified name and bonding parameters.
  4317. * If name is NULL, obtain a suitable "bond%d" name for us.
  4318. * Caller must NOT hold rtnl_lock; we need to release it here before we
  4319. * set up our sysfs entries.
  4320. */
  4321. int bond_create(char *name, struct bond_params *params)
  4322. {
  4323. struct net_device *bond_dev;
  4324. struct bonding *bond;
  4325. int res;
  4326. rtnl_lock();
  4327. down_write(&bonding_rwsem);
  4328. /* Check to see if the bond already exists. */
  4329. if (name) {
  4330. list_for_each_entry(bond, &bond_dev_list, bond_list)
  4331. if (strnicmp(bond->dev->name, name, IFNAMSIZ) == 0) {
  4332. printk(KERN_ERR DRV_NAME
  4333. ": cannot add bond %s; it already exists\n",
  4334. name);
  4335. res = -EPERM;
  4336. goto out_rtnl;
  4337. }
  4338. }
  4339. bond_dev = alloc_netdev(sizeof(struct bonding), name ? name : "",
  4340. ether_setup);
  4341. if (!bond_dev) {
  4342. printk(KERN_ERR DRV_NAME
  4343. ": %s: eek! can't alloc netdev!\n",
  4344. name);
  4345. res = -ENOMEM;
  4346. goto out_rtnl;
  4347. }
  4348. if (!name) {
  4349. res = dev_alloc_name(bond_dev, "bond%d");
  4350. if (res < 0)
  4351. goto out_netdev;
  4352. }
  4353. /* bond_init() must be called after dev_alloc_name() (for the
  4354. * /proc files), but before register_netdevice(), because we
  4355. * need to set function pointers.
  4356. */
  4357. res = bond_init(bond_dev, params);
  4358. if (res < 0) {
  4359. goto out_netdev;
  4360. }
  4361. res = register_netdevice(bond_dev);
  4362. if (res < 0) {
  4363. goto out_bond;
  4364. }
  4365. bond_set_lockdep_class(bond_dev);
  4366. netif_carrier_off(bond_dev);
  4367. up_write(&bonding_rwsem);
  4368. rtnl_unlock(); /* allows sysfs registration of net device */
  4369. res = bond_create_sysfs_entry(netdev_priv(bond_dev));
  4370. if (res < 0) {
  4371. rtnl_lock();
  4372. down_write(&bonding_rwsem);
  4373. bond_deinit(bond_dev);
  4374. unregister_netdevice(bond_dev);
  4375. goto out_rtnl;
  4376. }
  4377. return 0;
  4378. out_bond:
  4379. bond_deinit(bond_dev);
  4380. out_netdev:
  4381. free_netdev(bond_dev);
  4382. out_rtnl:
  4383. up_write(&bonding_rwsem);
  4384. rtnl_unlock();
  4385. return res;
  4386. }
  4387. static int __init bonding_init(void)
  4388. {
  4389. int i;
  4390. int res;
  4391. struct bonding *bond;
  4392. printk(KERN_INFO "%s", version);
  4393. res = bond_check_params(&bonding_defaults);
  4394. if (res) {
  4395. goto out;
  4396. }
  4397. #ifdef CONFIG_PROC_FS
  4398. bond_create_proc_dir();
  4399. #endif
  4400. init_rwsem(&bonding_rwsem);
  4401. for (i = 0; i < max_bonds; i++) {
  4402. res = bond_create(NULL, &bonding_defaults);
  4403. if (res)
  4404. goto err;
  4405. }
  4406. res = bond_create_sysfs();
  4407. if (res)
  4408. goto err;
  4409. register_netdevice_notifier(&bond_netdev_notifier);
  4410. register_inetaddr_notifier(&bond_inetaddr_notifier);
  4411. bond_register_ipv6_notifier();
  4412. goto out;
  4413. err:
  4414. list_for_each_entry(bond, &bond_dev_list, bond_list) {
  4415. bond_work_cancel_all(bond);
  4416. destroy_workqueue(bond->wq);
  4417. }
  4418. bond_destroy_sysfs();
  4419. rtnl_lock();
  4420. bond_free_all();
  4421. rtnl_unlock();
  4422. out:
  4423. return res;
  4424. }
  4425. static void __exit bonding_exit(void)
  4426. {
  4427. unregister_netdevice_notifier(&bond_netdev_notifier);
  4428. unregister_inetaddr_notifier(&bond_inetaddr_notifier);
  4429. bond_unregister_ipv6_notifier();
  4430. bond_destroy_sysfs();
  4431. rtnl_lock();
  4432. bond_free_all();
  4433. rtnl_unlock();
  4434. }
  4435. module_init(bonding_init);
  4436. module_exit(bonding_exit);
  4437. MODULE_LICENSE("GPL");
  4438. MODULE_VERSION(DRV_VERSION);
  4439. MODULE_DESCRIPTION(DRV_DESCRIPTION ", v" DRV_VERSION);
  4440. MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others");
  4441. MODULE_SUPPORTED_DEVICE("most ethernet devices");
  4442. /*
  4443. * Local variables:
  4444. * c-indent-level: 8
  4445. * c-basic-offset: 8
  4446. * tab-width: 8
  4447. * End:
  4448. */