bond_main.c 136 KB

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