bond_main.c 120 KB

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