bond_main.c 121 KB

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