bond_main.c 140 KB

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