bond_main.c 140 KB

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