bond_main.c 141 KB

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