bond_main.c 130 KB

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