bond_main.c 136 KB

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