bond_main.c 131 KB

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