bond_main.c 126 KB

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