bond_main.c 121 KB

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