bond_main.c 133 KB

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