bond_main.c 135 KB

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