bond_main.c 121 KB

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