bond_main.c 130 KB

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