bond_main.c 133 KB

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