bond_main.c 131 KB

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