bond_main.c 141 KB

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