bond_main.c 131 KB

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