bond_main.c 136 KB

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