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