bond_main.c 135 KB

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