bond_main.c 135 KB

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