bond_main.c 128 KB

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