bond_main.c 136 KB

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