bond_main.c 132 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034
  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. netif_tx_lock_bh(bond_dev);
  1274. /* upload master's mc_list to new slave */
  1275. for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
  1276. dev_mc_add (slave_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
  1277. }
  1278. netif_tx_unlock_bh(bond_dev);
  1279. }
  1280. if (bond->params.mode == BOND_MODE_8023AD) {
  1281. /* add lacpdu mc addr to mc list */
  1282. u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
  1283. dev_mc_add(slave_dev, lacpdu_multicast, ETH_ALEN, 0);
  1284. }
  1285. bond_add_vlans_on_slave(bond, slave_dev);
  1286. write_lock_bh(&bond->lock);
  1287. bond_attach_slave(bond, new_slave);
  1288. new_slave->delay = 0;
  1289. new_slave->link_failure_count = 0;
  1290. bond_compute_features(bond);
  1291. new_slave->last_arp_rx = jiffies;
  1292. if (bond->params.miimon && !bond->params.use_carrier) {
  1293. link_reporting = bond_check_dev_link(bond, slave_dev, 1);
  1294. if ((link_reporting == -1) && !bond->params.arp_interval) {
  1295. /*
  1296. * miimon is set but a bonded network driver
  1297. * does not support ETHTOOL/MII and
  1298. * arp_interval is not set. Note: if
  1299. * use_carrier is enabled, we will never go
  1300. * here (because netif_carrier is always
  1301. * supported); thus, we don't need to change
  1302. * the messages for netif_carrier.
  1303. */
  1304. printk(KERN_WARNING DRV_NAME
  1305. ": %s: Warning: MII and ETHTOOL support not "
  1306. "available for interface %s, and "
  1307. "arp_interval/arp_ip_target module parameters "
  1308. "not specified, thus bonding will not detect "
  1309. "link failures! see bonding.txt for details.\n",
  1310. bond_dev->name, slave_dev->name);
  1311. } else if (link_reporting == -1) {
  1312. /* unable get link status using mii/ethtool */
  1313. printk(KERN_WARNING DRV_NAME
  1314. ": %s: Warning: can't get link status from "
  1315. "interface %s; the network driver associated "
  1316. "with this interface does not support MII or "
  1317. "ETHTOOL link status reporting, thus miimon "
  1318. "has no effect on this interface.\n",
  1319. bond_dev->name, slave_dev->name);
  1320. }
  1321. }
  1322. /* check for initial state */
  1323. if (!bond->params.miimon ||
  1324. (bond_check_dev_link(bond, slave_dev, 0) == BMSR_LSTATUS)) {
  1325. if (bond->params.updelay) {
  1326. dprintk("Initial state of slave_dev is "
  1327. "BOND_LINK_BACK\n");
  1328. new_slave->link = BOND_LINK_BACK;
  1329. new_slave->delay = bond->params.updelay;
  1330. } else {
  1331. dprintk("Initial state of slave_dev is "
  1332. "BOND_LINK_UP\n");
  1333. new_slave->link = BOND_LINK_UP;
  1334. }
  1335. new_slave->jiffies = jiffies;
  1336. } else {
  1337. dprintk("Initial state of slave_dev is "
  1338. "BOND_LINK_DOWN\n");
  1339. new_slave->link = BOND_LINK_DOWN;
  1340. }
  1341. if (bond_update_speed_duplex(new_slave) &&
  1342. (new_slave->link != BOND_LINK_DOWN)) {
  1343. printk(KERN_WARNING DRV_NAME
  1344. ": %s: Warning: failed to get speed and duplex from %s, "
  1345. "assumed to be 100Mb/sec and Full.\n",
  1346. bond_dev->name, new_slave->dev->name);
  1347. if (bond->params.mode == BOND_MODE_8023AD) {
  1348. printk(KERN_WARNING DRV_NAME
  1349. ": %s: Warning: Operation of 802.3ad mode requires ETHTOOL "
  1350. "support in base driver for proper aggregator "
  1351. "selection.\n", bond_dev->name);
  1352. }
  1353. }
  1354. if (USES_PRIMARY(bond->params.mode) && bond->params.primary[0]) {
  1355. /* if there is a primary slave, remember it */
  1356. if (strcmp(bond->params.primary, new_slave->dev->name) == 0) {
  1357. bond->primary_slave = new_slave;
  1358. }
  1359. }
  1360. switch (bond->params.mode) {
  1361. case BOND_MODE_ACTIVEBACKUP:
  1362. bond_set_slave_inactive_flags(new_slave);
  1363. bond_select_active_slave(bond);
  1364. break;
  1365. case BOND_MODE_8023AD:
  1366. /* in 802.3ad mode, the internal mechanism
  1367. * will activate the slaves in the selected
  1368. * aggregator
  1369. */
  1370. bond_set_slave_inactive_flags(new_slave);
  1371. /* if this is the first slave */
  1372. if (bond->slave_cnt == 1) {
  1373. SLAVE_AD_INFO(new_slave).id = 1;
  1374. /* Initialize AD with the number of times that the AD timer is called in 1 second
  1375. * can be called only after the mac address of the bond is set
  1376. */
  1377. bond_3ad_initialize(bond, 1000/AD_TIMER_INTERVAL,
  1378. bond->params.lacp_fast);
  1379. } else {
  1380. SLAVE_AD_INFO(new_slave).id =
  1381. SLAVE_AD_INFO(new_slave->prev).id + 1;
  1382. }
  1383. bond_3ad_bind_slave(new_slave);
  1384. break;
  1385. case BOND_MODE_TLB:
  1386. case BOND_MODE_ALB:
  1387. new_slave->state = BOND_STATE_ACTIVE;
  1388. bond_set_slave_inactive_flags(new_slave);
  1389. break;
  1390. default:
  1391. dprintk("This slave is always active in trunk mode\n");
  1392. /* always active in trunk mode */
  1393. new_slave->state = BOND_STATE_ACTIVE;
  1394. /* In trunking mode there is little meaning to curr_active_slave
  1395. * anyway (it holds no special properties of the bond device),
  1396. * so we can change it without calling change_active_interface()
  1397. */
  1398. if (!bond->curr_active_slave) {
  1399. bond->curr_active_slave = new_slave;
  1400. }
  1401. break;
  1402. } /* switch(bond_mode) */
  1403. bond_set_carrier(bond);
  1404. write_unlock_bh(&bond->lock);
  1405. res = bond_create_slave_symlinks(bond_dev, slave_dev);
  1406. if (res)
  1407. goto err_unset_master;
  1408. printk(KERN_INFO DRV_NAME
  1409. ": %s: enslaving %s as a%s interface with a%s link.\n",
  1410. bond_dev->name, slave_dev->name,
  1411. new_slave->state == BOND_STATE_ACTIVE ? "n active" : " backup",
  1412. new_slave->link != BOND_LINK_DOWN ? "n up" : " down");
  1413. /* enslave is successful */
  1414. return 0;
  1415. /* Undo stages on error */
  1416. err_unset_master:
  1417. netdev_set_master(slave_dev, NULL);
  1418. err_close:
  1419. dev_close(slave_dev);
  1420. err_restore_mac:
  1421. if (!bond->params.fail_over_mac) {
  1422. memcpy(addr.sa_data, new_slave->perm_hwaddr, ETH_ALEN);
  1423. addr.sa_family = slave_dev->type;
  1424. dev_set_mac_address(slave_dev, &addr);
  1425. }
  1426. err_free:
  1427. kfree(new_slave);
  1428. err_undo_flags:
  1429. bond_dev->features = old_features;
  1430. return res;
  1431. }
  1432. /*
  1433. * Try to release the slave device <slave> from the bond device <master>
  1434. * It is legal to access curr_active_slave without a lock because all the function
  1435. * is write-locked.
  1436. *
  1437. * The rules for slave state should be:
  1438. * for Active/Backup:
  1439. * Active stays on all backups go down
  1440. * for Bonded connections:
  1441. * The first up interface should be left on and all others downed.
  1442. */
  1443. int bond_release(struct net_device *bond_dev, struct net_device *slave_dev)
  1444. {
  1445. struct bonding *bond = bond_dev->priv;
  1446. struct slave *slave, *oldcurrent;
  1447. struct sockaddr addr;
  1448. int mac_addr_differ;
  1449. DECLARE_MAC_BUF(mac);
  1450. /* slave is not a slave or master is not master of this slave */
  1451. if (!(slave_dev->flags & IFF_SLAVE) ||
  1452. (slave_dev->master != bond_dev)) {
  1453. printk(KERN_ERR DRV_NAME
  1454. ": %s: Error: cannot release %s.\n",
  1455. bond_dev->name, slave_dev->name);
  1456. return -EINVAL;
  1457. }
  1458. write_lock_bh(&bond->lock);
  1459. slave = bond_get_slave_by_dev(bond, slave_dev);
  1460. if (!slave) {
  1461. /* not a slave of this bond */
  1462. printk(KERN_INFO DRV_NAME
  1463. ": %s: %s not enslaved\n",
  1464. bond_dev->name, slave_dev->name);
  1465. write_unlock_bh(&bond->lock);
  1466. return -EINVAL;
  1467. }
  1468. mac_addr_differ = memcmp(bond_dev->dev_addr,
  1469. slave->perm_hwaddr,
  1470. ETH_ALEN);
  1471. if (!mac_addr_differ && (bond->slave_cnt > 1)) {
  1472. printk(KERN_WARNING DRV_NAME
  1473. ": %s: Warning: the permanent HWaddr of %s - "
  1474. "%s - is still in use by %s. "
  1475. "Set the HWaddr of %s to a different address "
  1476. "to avoid conflicts.\n",
  1477. bond_dev->name,
  1478. slave_dev->name,
  1479. print_mac(mac, slave->perm_hwaddr),
  1480. bond_dev->name,
  1481. slave_dev->name);
  1482. }
  1483. /* Inform AD package of unbinding of slave. */
  1484. if (bond->params.mode == BOND_MODE_8023AD) {
  1485. /* must be called before the slave is
  1486. * detached from the list
  1487. */
  1488. bond_3ad_unbind_slave(slave);
  1489. }
  1490. printk(KERN_INFO DRV_NAME
  1491. ": %s: releasing %s interface %s\n",
  1492. bond_dev->name,
  1493. (slave->state == BOND_STATE_ACTIVE)
  1494. ? "active" : "backup",
  1495. slave_dev->name);
  1496. oldcurrent = bond->curr_active_slave;
  1497. bond->current_arp_slave = NULL;
  1498. /* release the slave from its bond */
  1499. bond_detach_slave(bond, slave);
  1500. bond_compute_features(bond);
  1501. if (bond->primary_slave == slave) {
  1502. bond->primary_slave = NULL;
  1503. }
  1504. if (oldcurrent == slave) {
  1505. bond_change_active_slave(bond, NULL);
  1506. }
  1507. if ((bond->params.mode == BOND_MODE_TLB) ||
  1508. (bond->params.mode == BOND_MODE_ALB)) {
  1509. /* Must be called only after the slave has been
  1510. * detached from the list and the curr_active_slave
  1511. * has been cleared (if our_slave == old_current),
  1512. * but before a new active slave is selected.
  1513. */
  1514. write_unlock_bh(&bond->lock);
  1515. bond_alb_deinit_slave(bond, slave);
  1516. write_lock_bh(&bond->lock);
  1517. }
  1518. if (oldcurrent == slave) {
  1519. /*
  1520. * Note that we hold RTNL over this sequence, so there
  1521. * is no concern that another slave add/remove event
  1522. * will interfere.
  1523. */
  1524. write_unlock_bh(&bond->lock);
  1525. read_lock(&bond->lock);
  1526. write_lock_bh(&bond->curr_slave_lock);
  1527. bond_select_active_slave(bond);
  1528. write_unlock_bh(&bond->curr_slave_lock);
  1529. read_unlock(&bond->lock);
  1530. write_lock_bh(&bond->lock);
  1531. }
  1532. if (bond->slave_cnt == 0) {
  1533. bond_set_carrier(bond);
  1534. /* if the last slave was removed, zero the mac address
  1535. * of the master so it will be set by the application
  1536. * to the mac address of the first slave
  1537. */
  1538. memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
  1539. if (list_empty(&bond->vlan_list)) {
  1540. bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
  1541. } else {
  1542. printk(KERN_WARNING DRV_NAME
  1543. ": %s: Warning: clearing HW address of %s while it "
  1544. "still has VLANs.\n",
  1545. bond_dev->name, bond_dev->name);
  1546. printk(KERN_WARNING DRV_NAME
  1547. ": %s: When re-adding slaves, make sure the bond's "
  1548. "HW address matches its VLANs'.\n",
  1549. bond_dev->name);
  1550. }
  1551. } else if ((bond_dev->features & NETIF_F_VLAN_CHALLENGED) &&
  1552. !bond_has_challenged_slaves(bond)) {
  1553. printk(KERN_INFO DRV_NAME
  1554. ": %s: last VLAN challenged slave %s "
  1555. "left bond %s. VLAN blocking is removed\n",
  1556. bond_dev->name, slave_dev->name, bond_dev->name);
  1557. bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
  1558. }
  1559. write_unlock_bh(&bond->lock);
  1560. /* must do this from outside any spinlocks */
  1561. bond_destroy_slave_symlinks(bond_dev, slave_dev);
  1562. bond_del_vlans_from_slave(bond, slave_dev);
  1563. /* If the mode USES_PRIMARY, then we should only remove its
  1564. * promisc and mc settings if it was the curr_active_slave, but that was
  1565. * already taken care of above when we detached the slave
  1566. */
  1567. if (!USES_PRIMARY(bond->params.mode)) {
  1568. /* unset promiscuity level from slave */
  1569. if (bond_dev->flags & IFF_PROMISC) {
  1570. dev_set_promiscuity(slave_dev, -1);
  1571. }
  1572. /* unset allmulti level from slave */
  1573. if (bond_dev->flags & IFF_ALLMULTI) {
  1574. dev_set_allmulti(slave_dev, -1);
  1575. }
  1576. /* flush master's mc_list from slave */
  1577. netif_tx_lock_bh(bond_dev);
  1578. bond_mc_list_flush(bond_dev, slave_dev);
  1579. netif_tx_unlock_bh(bond_dev);
  1580. }
  1581. netdev_set_master(slave_dev, NULL);
  1582. /* close slave before restoring its mac address */
  1583. dev_close(slave_dev);
  1584. if (!bond->params.fail_over_mac) {
  1585. /* restore original ("permanent") mac address */
  1586. memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
  1587. addr.sa_family = slave_dev->type;
  1588. dev_set_mac_address(slave_dev, &addr);
  1589. }
  1590. slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
  1591. IFF_SLAVE_INACTIVE | IFF_BONDING |
  1592. IFF_SLAVE_NEEDARP);
  1593. kfree(slave);
  1594. return 0; /* deletion OK */
  1595. }
  1596. /*
  1597. * Destroy a bonding device.
  1598. * Must be under rtnl_lock when this function is called.
  1599. */
  1600. void bond_destroy(struct bonding *bond)
  1601. {
  1602. bond_deinit(bond->dev);
  1603. bond_destroy_sysfs_entry(bond);
  1604. unregister_netdevice(bond->dev);
  1605. }
  1606. /*
  1607. * First release a slave and than destroy the bond if no more slaves iare left.
  1608. * Must be under rtnl_lock when this function is called.
  1609. */
  1610. int bond_release_and_destroy(struct net_device *bond_dev, struct net_device *slave_dev)
  1611. {
  1612. struct bonding *bond = bond_dev->priv;
  1613. int ret;
  1614. ret = bond_release(bond_dev, slave_dev);
  1615. if ((ret == 0) && (bond->slave_cnt == 0)) {
  1616. printk(KERN_INFO DRV_NAME ": %s: destroying bond %s.\n",
  1617. bond_dev->name, bond_dev->name);
  1618. bond_destroy(bond);
  1619. }
  1620. return ret;
  1621. }
  1622. /*
  1623. * This function releases all slaves.
  1624. */
  1625. static int bond_release_all(struct net_device *bond_dev)
  1626. {
  1627. struct bonding *bond = bond_dev->priv;
  1628. struct slave *slave;
  1629. struct net_device *slave_dev;
  1630. struct sockaddr addr;
  1631. write_lock_bh(&bond->lock);
  1632. netif_carrier_off(bond_dev);
  1633. if (bond->slave_cnt == 0) {
  1634. goto out;
  1635. }
  1636. bond->current_arp_slave = NULL;
  1637. bond->primary_slave = NULL;
  1638. bond_change_active_slave(bond, NULL);
  1639. while ((slave = bond->first_slave) != NULL) {
  1640. /* Inform AD package of unbinding of slave
  1641. * before slave is detached from the list.
  1642. */
  1643. if (bond->params.mode == BOND_MODE_8023AD) {
  1644. bond_3ad_unbind_slave(slave);
  1645. }
  1646. slave_dev = slave->dev;
  1647. bond_detach_slave(bond, slave);
  1648. /* now that the slave is detached, unlock and perform
  1649. * all the undo steps that should not be called from
  1650. * within a lock.
  1651. */
  1652. write_unlock_bh(&bond->lock);
  1653. if ((bond->params.mode == BOND_MODE_TLB) ||
  1654. (bond->params.mode == BOND_MODE_ALB)) {
  1655. /* must be called only after the slave
  1656. * has been detached from the list
  1657. */
  1658. bond_alb_deinit_slave(bond, slave);
  1659. }
  1660. bond_compute_features(bond);
  1661. bond_destroy_slave_symlinks(bond_dev, slave_dev);
  1662. bond_del_vlans_from_slave(bond, slave_dev);
  1663. /* If the mode USES_PRIMARY, then we should only remove its
  1664. * promisc and mc settings if it was the curr_active_slave, but that was
  1665. * already taken care of above when we detached the slave
  1666. */
  1667. if (!USES_PRIMARY(bond->params.mode)) {
  1668. /* unset promiscuity level from slave */
  1669. if (bond_dev->flags & IFF_PROMISC) {
  1670. dev_set_promiscuity(slave_dev, -1);
  1671. }
  1672. /* unset allmulti level from slave */
  1673. if (bond_dev->flags & IFF_ALLMULTI) {
  1674. dev_set_allmulti(slave_dev, -1);
  1675. }
  1676. /* flush master's mc_list from slave */
  1677. netif_tx_lock_bh(bond_dev);
  1678. bond_mc_list_flush(bond_dev, slave_dev);
  1679. netif_tx_unlock_bh(bond_dev);
  1680. }
  1681. netdev_set_master(slave_dev, NULL);
  1682. /* close slave before restoring its mac address */
  1683. dev_close(slave_dev);
  1684. if (!bond->params.fail_over_mac) {
  1685. /* restore original ("permanent") mac address*/
  1686. memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
  1687. addr.sa_family = slave_dev->type;
  1688. dev_set_mac_address(slave_dev, &addr);
  1689. }
  1690. slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
  1691. IFF_SLAVE_INACTIVE);
  1692. kfree(slave);
  1693. /* re-acquire the lock before getting the next slave */
  1694. write_lock_bh(&bond->lock);
  1695. }
  1696. /* zero the mac address of the master so it will be
  1697. * set by the application to the mac address of the
  1698. * first slave
  1699. */
  1700. memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
  1701. if (list_empty(&bond->vlan_list)) {
  1702. bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
  1703. } else {
  1704. printk(KERN_WARNING DRV_NAME
  1705. ": %s: Warning: clearing HW address of %s while it "
  1706. "still has VLANs.\n",
  1707. bond_dev->name, bond_dev->name);
  1708. printk(KERN_WARNING DRV_NAME
  1709. ": %s: When re-adding slaves, make sure the bond's "
  1710. "HW address matches its VLANs'.\n",
  1711. bond_dev->name);
  1712. }
  1713. printk(KERN_INFO DRV_NAME
  1714. ": %s: released all slaves\n",
  1715. bond_dev->name);
  1716. out:
  1717. write_unlock_bh(&bond->lock);
  1718. return 0;
  1719. }
  1720. /*
  1721. * This function changes the active slave to slave <slave_dev>.
  1722. * It returns -EINVAL in the following cases.
  1723. * - <slave_dev> is not found in the list.
  1724. * - There is not active slave now.
  1725. * - <slave_dev> is already active.
  1726. * - The link state of <slave_dev> is not BOND_LINK_UP.
  1727. * - <slave_dev> is not running.
  1728. * In these cases, this fuction does nothing.
  1729. * In the other cases, currnt_slave pointer is changed and 0 is returned.
  1730. */
  1731. static int bond_ioctl_change_active(struct net_device *bond_dev, struct net_device *slave_dev)
  1732. {
  1733. struct bonding *bond = bond_dev->priv;
  1734. struct slave *old_active = NULL;
  1735. struct slave *new_active = NULL;
  1736. int res = 0;
  1737. if (!USES_PRIMARY(bond->params.mode)) {
  1738. return -EINVAL;
  1739. }
  1740. /* Verify that master_dev is indeed the master of slave_dev */
  1741. if (!(slave_dev->flags & IFF_SLAVE) ||
  1742. (slave_dev->master != bond_dev)) {
  1743. return -EINVAL;
  1744. }
  1745. read_lock(&bond->lock);
  1746. read_lock(&bond->curr_slave_lock);
  1747. old_active = bond->curr_active_slave;
  1748. read_unlock(&bond->curr_slave_lock);
  1749. new_active = bond_get_slave_by_dev(bond, slave_dev);
  1750. /*
  1751. * Changing to the current active: do nothing; return success.
  1752. */
  1753. if (new_active && (new_active == old_active)) {
  1754. read_unlock(&bond->lock);
  1755. return 0;
  1756. }
  1757. if ((new_active) &&
  1758. (old_active) &&
  1759. (new_active->link == BOND_LINK_UP) &&
  1760. IS_UP(new_active->dev)) {
  1761. write_lock_bh(&bond->curr_slave_lock);
  1762. bond_change_active_slave(bond, new_active);
  1763. write_unlock_bh(&bond->curr_slave_lock);
  1764. } else {
  1765. res = -EINVAL;
  1766. }
  1767. read_unlock(&bond->lock);
  1768. return res;
  1769. }
  1770. static int bond_info_query(struct net_device *bond_dev, struct ifbond *info)
  1771. {
  1772. struct bonding *bond = bond_dev->priv;
  1773. info->bond_mode = bond->params.mode;
  1774. info->miimon = bond->params.miimon;
  1775. read_lock(&bond->lock);
  1776. info->num_slaves = bond->slave_cnt;
  1777. read_unlock(&bond->lock);
  1778. return 0;
  1779. }
  1780. static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info)
  1781. {
  1782. struct bonding *bond = bond_dev->priv;
  1783. struct slave *slave;
  1784. int i, found = 0;
  1785. if (info->slave_id < 0) {
  1786. return -ENODEV;
  1787. }
  1788. read_lock(&bond->lock);
  1789. bond_for_each_slave(bond, slave, i) {
  1790. if (i == (int)info->slave_id) {
  1791. found = 1;
  1792. break;
  1793. }
  1794. }
  1795. read_unlock(&bond->lock);
  1796. if (found) {
  1797. strcpy(info->slave_name, slave->dev->name);
  1798. info->link = slave->link;
  1799. info->state = slave->state;
  1800. info->link_failure_count = slave->link_failure_count;
  1801. } else {
  1802. return -ENODEV;
  1803. }
  1804. return 0;
  1805. }
  1806. /*-------------------------------- Monitoring -------------------------------*/
  1807. /*
  1808. * if !have_locks, return nonzero if a failover is necessary. if
  1809. * have_locks, do whatever failover activities are needed.
  1810. *
  1811. * This is to separate the inspection and failover steps for locking
  1812. * purposes; failover requires rtnl, but acquiring it for every
  1813. * inspection is undesirable, so a wrapper first does inspection, and
  1814. * the acquires the necessary locks and calls again to perform
  1815. * failover if needed. Since all locks are dropped, a complete
  1816. * restart is needed between calls.
  1817. */
  1818. static int __bond_mii_monitor(struct bonding *bond, int have_locks)
  1819. {
  1820. struct slave *slave, *oldcurrent;
  1821. int do_failover = 0;
  1822. int i;
  1823. if (bond->slave_cnt == 0)
  1824. goto out;
  1825. /* we will try to read the link status of each of our slaves, and
  1826. * set their IFF_RUNNING flag appropriately. For each slave not
  1827. * supporting MII status, we won't do anything so that a user-space
  1828. * program could monitor the link itself if needed.
  1829. */
  1830. if (bond->send_grat_arp) {
  1831. if (bond->curr_active_slave && test_bit(__LINK_STATE_LINKWATCH_PENDING,
  1832. &bond->curr_active_slave->dev->state))
  1833. dprintk("Needs to send gratuitous arp but not yet\n");
  1834. else {
  1835. dprintk("sending delayed gratuitous arp on on %s\n",
  1836. bond->curr_active_slave->dev->name);
  1837. bond_send_gratuitous_arp(bond);
  1838. bond->send_grat_arp = 0;
  1839. }
  1840. }
  1841. read_lock(&bond->curr_slave_lock);
  1842. oldcurrent = bond->curr_active_slave;
  1843. read_unlock(&bond->curr_slave_lock);
  1844. bond_for_each_slave(bond, slave, i) {
  1845. struct net_device *slave_dev = slave->dev;
  1846. int link_state;
  1847. u16 old_speed = slave->speed;
  1848. u8 old_duplex = slave->duplex;
  1849. link_state = bond_check_dev_link(bond, slave_dev, 0);
  1850. switch (slave->link) {
  1851. case BOND_LINK_UP: /* the link was up */
  1852. if (link_state == BMSR_LSTATUS) {
  1853. if (!oldcurrent) {
  1854. if (!have_locks)
  1855. return 1;
  1856. do_failover = 1;
  1857. }
  1858. break;
  1859. } else { /* link going down */
  1860. slave->link = BOND_LINK_FAIL;
  1861. slave->delay = bond->params.downdelay;
  1862. if (slave->link_failure_count < UINT_MAX) {
  1863. slave->link_failure_count++;
  1864. }
  1865. if (bond->params.downdelay) {
  1866. printk(KERN_INFO DRV_NAME
  1867. ": %s: link status down for %s "
  1868. "interface %s, disabling it in "
  1869. "%d ms.\n",
  1870. bond->dev->name,
  1871. IS_UP(slave_dev)
  1872. ? ((bond->params.mode == BOND_MODE_ACTIVEBACKUP)
  1873. ? ((slave == oldcurrent)
  1874. ? "active " : "backup ")
  1875. : "")
  1876. : "idle ",
  1877. slave_dev->name,
  1878. bond->params.downdelay * bond->params.miimon);
  1879. }
  1880. }
  1881. /* no break ! fall through the BOND_LINK_FAIL test to
  1882. ensure proper action to be taken
  1883. */
  1884. case BOND_LINK_FAIL: /* the link has just gone down */
  1885. if (link_state != BMSR_LSTATUS) {
  1886. /* link stays down */
  1887. if (slave->delay <= 0) {
  1888. if (!have_locks)
  1889. return 1;
  1890. /* link down for too long time */
  1891. slave->link = BOND_LINK_DOWN;
  1892. /* in active/backup mode, we must
  1893. * completely disable this interface
  1894. */
  1895. if ((bond->params.mode == BOND_MODE_ACTIVEBACKUP) ||
  1896. (bond->params.mode == BOND_MODE_8023AD)) {
  1897. bond_set_slave_inactive_flags(slave);
  1898. }
  1899. printk(KERN_INFO DRV_NAME
  1900. ": %s: link status definitely "
  1901. "down for interface %s, "
  1902. "disabling it\n",
  1903. bond->dev->name,
  1904. slave_dev->name);
  1905. /* notify ad that the link status has changed */
  1906. if (bond->params.mode == BOND_MODE_8023AD) {
  1907. bond_3ad_handle_link_change(slave, BOND_LINK_DOWN);
  1908. }
  1909. if ((bond->params.mode == BOND_MODE_TLB) ||
  1910. (bond->params.mode == BOND_MODE_ALB)) {
  1911. bond_alb_handle_link_change(bond, slave, BOND_LINK_DOWN);
  1912. }
  1913. if (slave == oldcurrent) {
  1914. do_failover = 1;
  1915. }
  1916. } else {
  1917. slave->delay--;
  1918. }
  1919. } else {
  1920. /* link up again */
  1921. slave->link = BOND_LINK_UP;
  1922. slave->jiffies = jiffies;
  1923. printk(KERN_INFO DRV_NAME
  1924. ": %s: link status up again after %d "
  1925. "ms for interface %s.\n",
  1926. bond->dev->name,
  1927. (bond->params.downdelay - slave->delay) * bond->params.miimon,
  1928. slave_dev->name);
  1929. }
  1930. break;
  1931. case BOND_LINK_DOWN: /* the link was down */
  1932. if (link_state != BMSR_LSTATUS) {
  1933. /* the link stays down, nothing more to do */
  1934. break;
  1935. } else { /* link going up */
  1936. slave->link = BOND_LINK_BACK;
  1937. slave->delay = bond->params.updelay;
  1938. if (bond->params.updelay) {
  1939. /* if updelay == 0, no need to
  1940. advertise about a 0 ms delay */
  1941. printk(KERN_INFO DRV_NAME
  1942. ": %s: link status up for "
  1943. "interface %s, enabling it "
  1944. "in %d ms.\n",
  1945. bond->dev->name,
  1946. slave_dev->name,
  1947. bond->params.updelay * bond->params.miimon);
  1948. }
  1949. }
  1950. /* no break ! fall through the BOND_LINK_BACK state in
  1951. case there's something to do.
  1952. */
  1953. case BOND_LINK_BACK: /* the link has just come back */
  1954. if (link_state != BMSR_LSTATUS) {
  1955. /* link down again */
  1956. slave->link = BOND_LINK_DOWN;
  1957. printk(KERN_INFO DRV_NAME
  1958. ": %s: link status down again after %d "
  1959. "ms for interface %s.\n",
  1960. bond->dev->name,
  1961. (bond->params.updelay - slave->delay) * bond->params.miimon,
  1962. slave_dev->name);
  1963. } else {
  1964. /* link stays up */
  1965. if (slave->delay == 0) {
  1966. if (!have_locks)
  1967. return 1;
  1968. /* now the link has been up for long time enough */
  1969. slave->link = BOND_LINK_UP;
  1970. slave->jiffies = jiffies;
  1971. if (bond->params.mode == BOND_MODE_8023AD) {
  1972. /* prevent it from being the active one */
  1973. slave->state = BOND_STATE_BACKUP;
  1974. } else if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
  1975. /* make it immediately active */
  1976. slave->state = BOND_STATE_ACTIVE;
  1977. } else if (slave != bond->primary_slave) {
  1978. /* prevent it from being the active one */
  1979. slave->state = BOND_STATE_BACKUP;
  1980. }
  1981. printk(KERN_INFO DRV_NAME
  1982. ": %s: link status definitely "
  1983. "up for interface %s.\n",
  1984. bond->dev->name,
  1985. slave_dev->name);
  1986. /* notify ad that the link status has changed */
  1987. if (bond->params.mode == BOND_MODE_8023AD) {
  1988. bond_3ad_handle_link_change(slave, BOND_LINK_UP);
  1989. }
  1990. if ((bond->params.mode == BOND_MODE_TLB) ||
  1991. (bond->params.mode == BOND_MODE_ALB)) {
  1992. bond_alb_handle_link_change(bond, slave, BOND_LINK_UP);
  1993. }
  1994. if ((!oldcurrent) ||
  1995. (slave == bond->primary_slave)) {
  1996. do_failover = 1;
  1997. }
  1998. } else {
  1999. slave->delay--;
  2000. }
  2001. }
  2002. break;
  2003. default:
  2004. /* Should not happen */
  2005. printk(KERN_ERR DRV_NAME
  2006. ": %s: Error: %s Illegal value (link=%d)\n",
  2007. bond->dev->name,
  2008. slave->dev->name,
  2009. slave->link);
  2010. goto out;
  2011. } /* end of switch (slave->link) */
  2012. bond_update_speed_duplex(slave);
  2013. if (bond->params.mode == BOND_MODE_8023AD) {
  2014. if (old_speed != slave->speed) {
  2015. bond_3ad_adapter_speed_changed(slave);
  2016. }
  2017. if (old_duplex != slave->duplex) {
  2018. bond_3ad_adapter_duplex_changed(slave);
  2019. }
  2020. }
  2021. } /* end of for */
  2022. if (do_failover) {
  2023. ASSERT_RTNL();
  2024. write_lock_bh(&bond->curr_slave_lock);
  2025. bond_select_active_slave(bond);
  2026. write_unlock_bh(&bond->curr_slave_lock);
  2027. } else
  2028. bond_set_carrier(bond);
  2029. out:
  2030. return 0;
  2031. }
  2032. /*
  2033. * bond_mii_monitor
  2034. *
  2035. * Really a wrapper that splits the mii monitor into two phases: an
  2036. * inspection, then (if inspection indicates something needs to be
  2037. * done) an acquisition of appropriate locks followed by another pass
  2038. * to implement whatever link state changes are indicated.
  2039. */
  2040. void bond_mii_monitor(struct work_struct *work)
  2041. {
  2042. struct bonding *bond = container_of(work, struct bonding,
  2043. mii_work.work);
  2044. unsigned long delay;
  2045. read_lock(&bond->lock);
  2046. if (bond->kill_timers) {
  2047. read_unlock(&bond->lock);
  2048. return;
  2049. }
  2050. if (__bond_mii_monitor(bond, 0)) {
  2051. read_unlock(&bond->lock);
  2052. rtnl_lock();
  2053. read_lock(&bond->lock);
  2054. __bond_mii_monitor(bond, 1);
  2055. read_unlock(&bond->lock);
  2056. rtnl_unlock(); /* might sleep, hold no other locks */
  2057. read_lock(&bond->lock);
  2058. }
  2059. delay = ((bond->params.miimon * HZ) / 1000) ? : 1;
  2060. read_unlock(&bond->lock);
  2061. queue_delayed_work(bond->wq, &bond->mii_work, delay);
  2062. }
  2063. static __be32 bond_glean_dev_ip(struct net_device *dev)
  2064. {
  2065. struct in_device *idev;
  2066. struct in_ifaddr *ifa;
  2067. __be32 addr = 0;
  2068. if (!dev)
  2069. return 0;
  2070. rcu_read_lock();
  2071. idev = __in_dev_get_rcu(dev);
  2072. if (!idev)
  2073. goto out;
  2074. ifa = idev->ifa_list;
  2075. if (!ifa)
  2076. goto out;
  2077. addr = ifa->ifa_local;
  2078. out:
  2079. rcu_read_unlock();
  2080. return addr;
  2081. }
  2082. static int bond_has_ip(struct bonding *bond)
  2083. {
  2084. struct vlan_entry *vlan, *vlan_next;
  2085. if (bond->master_ip)
  2086. return 1;
  2087. if (list_empty(&bond->vlan_list))
  2088. return 0;
  2089. list_for_each_entry_safe(vlan, vlan_next, &bond->vlan_list,
  2090. vlan_list) {
  2091. if (vlan->vlan_ip)
  2092. return 1;
  2093. }
  2094. return 0;
  2095. }
  2096. static int bond_has_this_ip(struct bonding *bond, __be32 ip)
  2097. {
  2098. struct vlan_entry *vlan, *vlan_next;
  2099. if (ip == bond->master_ip)
  2100. return 1;
  2101. if (list_empty(&bond->vlan_list))
  2102. return 0;
  2103. list_for_each_entry_safe(vlan, vlan_next, &bond->vlan_list,
  2104. vlan_list) {
  2105. if (ip == vlan->vlan_ip)
  2106. return 1;
  2107. }
  2108. return 0;
  2109. }
  2110. /*
  2111. * We go to the (large) trouble of VLAN tagging ARP frames because
  2112. * switches in VLAN mode (especially if ports are configured as
  2113. * "native" to a VLAN) might not pass non-tagged frames.
  2114. */
  2115. static void bond_arp_send(struct net_device *slave_dev, int arp_op, __be32 dest_ip, __be32 src_ip, unsigned short vlan_id)
  2116. {
  2117. struct sk_buff *skb;
  2118. dprintk("arp %d on slave %s: dst %x src %x vid %d\n", arp_op,
  2119. slave_dev->name, dest_ip, src_ip, vlan_id);
  2120. skb = arp_create(arp_op, ETH_P_ARP, dest_ip, slave_dev, src_ip,
  2121. NULL, slave_dev->dev_addr, NULL);
  2122. if (!skb) {
  2123. printk(KERN_ERR DRV_NAME ": ARP packet allocation failed\n");
  2124. return;
  2125. }
  2126. if (vlan_id) {
  2127. skb = vlan_put_tag(skb, vlan_id);
  2128. if (!skb) {
  2129. printk(KERN_ERR DRV_NAME ": failed to insert VLAN tag\n");
  2130. return;
  2131. }
  2132. }
  2133. arp_xmit(skb);
  2134. }
  2135. static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
  2136. {
  2137. int i, vlan_id, rv;
  2138. __be32 *targets = bond->params.arp_targets;
  2139. struct vlan_entry *vlan, *vlan_next;
  2140. struct net_device *vlan_dev;
  2141. struct flowi fl;
  2142. struct rtable *rt;
  2143. for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
  2144. if (!targets[i])
  2145. continue;
  2146. dprintk("basa: target %x\n", targets[i]);
  2147. if (list_empty(&bond->vlan_list)) {
  2148. dprintk("basa: empty vlan: arp_send\n");
  2149. bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
  2150. bond->master_ip, 0);
  2151. continue;
  2152. }
  2153. /*
  2154. * If VLANs are configured, we do a route lookup to
  2155. * determine which VLAN interface would be used, so we
  2156. * can tag the ARP with the proper VLAN tag.
  2157. */
  2158. memset(&fl, 0, sizeof(fl));
  2159. fl.fl4_dst = targets[i];
  2160. fl.fl4_tos = RTO_ONLINK;
  2161. rv = ip_route_output_key(&init_net, &rt, &fl);
  2162. if (rv) {
  2163. if (net_ratelimit()) {
  2164. printk(KERN_WARNING DRV_NAME
  2165. ": %s: no route to arp_ip_target %u.%u.%u.%u\n",
  2166. bond->dev->name, NIPQUAD(fl.fl4_dst));
  2167. }
  2168. continue;
  2169. }
  2170. /*
  2171. * This target is not on a VLAN
  2172. */
  2173. if (rt->u.dst.dev == bond->dev) {
  2174. ip_rt_put(rt);
  2175. dprintk("basa: rtdev == bond->dev: arp_send\n");
  2176. bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
  2177. bond->master_ip, 0);
  2178. continue;
  2179. }
  2180. vlan_id = 0;
  2181. list_for_each_entry_safe(vlan, vlan_next, &bond->vlan_list,
  2182. vlan_list) {
  2183. vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
  2184. if (vlan_dev == rt->u.dst.dev) {
  2185. vlan_id = vlan->vlan_id;
  2186. dprintk("basa: vlan match on %s %d\n",
  2187. vlan_dev->name, vlan_id);
  2188. break;
  2189. }
  2190. }
  2191. if (vlan_id) {
  2192. ip_rt_put(rt);
  2193. bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
  2194. vlan->vlan_ip, vlan_id);
  2195. continue;
  2196. }
  2197. if (net_ratelimit()) {
  2198. printk(KERN_WARNING DRV_NAME
  2199. ": %s: no path to arp_ip_target %u.%u.%u.%u via rt.dev %s\n",
  2200. bond->dev->name, NIPQUAD(fl.fl4_dst),
  2201. rt->u.dst.dev ? rt->u.dst.dev->name : "NULL");
  2202. }
  2203. ip_rt_put(rt);
  2204. }
  2205. }
  2206. /*
  2207. * Kick out a gratuitous ARP for an IP on the bonding master plus one
  2208. * for each VLAN above us.
  2209. */
  2210. static void bond_send_gratuitous_arp(struct bonding *bond)
  2211. {
  2212. struct slave *slave = bond->curr_active_slave;
  2213. struct vlan_entry *vlan;
  2214. struct net_device *vlan_dev;
  2215. dprintk("bond_send_grat_arp: bond %s slave %s\n", bond->dev->name,
  2216. slave ? slave->dev->name : "NULL");
  2217. if (!slave)
  2218. return;
  2219. if (bond->master_ip) {
  2220. bond_arp_send(slave->dev, ARPOP_REPLY, bond->master_ip,
  2221. bond->master_ip, 0);
  2222. }
  2223. list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
  2224. vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
  2225. if (vlan->vlan_ip) {
  2226. bond_arp_send(slave->dev, ARPOP_REPLY, vlan->vlan_ip,
  2227. vlan->vlan_ip, vlan->vlan_id);
  2228. }
  2229. }
  2230. }
  2231. static void bond_validate_arp(struct bonding *bond, struct slave *slave, __be32 sip, __be32 tip)
  2232. {
  2233. int i;
  2234. __be32 *targets = bond->params.arp_targets;
  2235. targets = bond->params.arp_targets;
  2236. for (i = 0; (i < BOND_MAX_ARP_TARGETS) && targets[i]; i++) {
  2237. dprintk("bva: sip %u.%u.%u.%u tip %u.%u.%u.%u t[%d] "
  2238. "%u.%u.%u.%u bhti(tip) %d\n",
  2239. NIPQUAD(sip), NIPQUAD(tip), i, NIPQUAD(targets[i]),
  2240. bond_has_this_ip(bond, tip));
  2241. if (sip == targets[i]) {
  2242. if (bond_has_this_ip(bond, tip))
  2243. slave->last_arp_rx = jiffies;
  2244. return;
  2245. }
  2246. }
  2247. }
  2248. static int bond_arp_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
  2249. {
  2250. struct arphdr *arp;
  2251. struct slave *slave;
  2252. struct bonding *bond;
  2253. unsigned char *arp_ptr;
  2254. __be32 sip, tip;
  2255. if (dev->nd_net != &init_net)
  2256. goto out;
  2257. if (!(dev->priv_flags & IFF_BONDING) || !(dev->flags & IFF_MASTER))
  2258. goto out;
  2259. bond = dev->priv;
  2260. read_lock(&bond->lock);
  2261. dprintk("bond_arp_rcv: bond %s skb->dev %s orig_dev %s\n",
  2262. bond->dev->name, skb->dev ? skb->dev->name : "NULL",
  2263. orig_dev ? orig_dev->name : "NULL");
  2264. slave = bond_get_slave_by_dev(bond, orig_dev);
  2265. if (!slave || !slave_do_arp_validate(bond, slave))
  2266. goto out_unlock;
  2267. /* ARP header, plus 2 device addresses, plus 2 IP addresses. */
  2268. if (!pskb_may_pull(skb, (sizeof(struct arphdr) +
  2269. (2 * dev->addr_len) +
  2270. (2 * sizeof(u32)))))
  2271. goto out_unlock;
  2272. arp = arp_hdr(skb);
  2273. if (arp->ar_hln != dev->addr_len ||
  2274. skb->pkt_type == PACKET_OTHERHOST ||
  2275. skb->pkt_type == PACKET_LOOPBACK ||
  2276. arp->ar_hrd != htons(ARPHRD_ETHER) ||
  2277. arp->ar_pro != htons(ETH_P_IP) ||
  2278. arp->ar_pln != 4)
  2279. goto out_unlock;
  2280. arp_ptr = (unsigned char *)(arp + 1);
  2281. arp_ptr += dev->addr_len;
  2282. memcpy(&sip, arp_ptr, 4);
  2283. arp_ptr += 4 + dev->addr_len;
  2284. memcpy(&tip, arp_ptr, 4);
  2285. dprintk("bond_arp_rcv: %s %s/%d av %d sv %d sip %u.%u.%u.%u"
  2286. " tip %u.%u.%u.%u\n", bond->dev->name, slave->dev->name,
  2287. slave->state, bond->params.arp_validate,
  2288. slave_do_arp_validate(bond, slave), NIPQUAD(sip), NIPQUAD(tip));
  2289. /*
  2290. * Backup slaves won't see the ARP reply, but do come through
  2291. * here for each ARP probe (so we swap the sip/tip to validate
  2292. * the probe). In a "redundant switch, common router" type of
  2293. * configuration, the ARP probe will (hopefully) travel from
  2294. * the active, through one switch, the router, then the other
  2295. * switch before reaching the backup.
  2296. */
  2297. if (slave->state == BOND_STATE_ACTIVE)
  2298. bond_validate_arp(bond, slave, sip, tip);
  2299. else
  2300. bond_validate_arp(bond, slave, tip, sip);
  2301. out_unlock:
  2302. read_unlock(&bond->lock);
  2303. out:
  2304. dev_kfree_skb(skb);
  2305. return NET_RX_SUCCESS;
  2306. }
  2307. /*
  2308. * this function is called regularly to monitor each slave's link
  2309. * ensuring that traffic is being sent and received when arp monitoring
  2310. * is used in load-balancing mode. if the adapter has been dormant, then an
  2311. * arp is transmitted to generate traffic. see activebackup_arp_monitor for
  2312. * arp monitoring in active backup mode.
  2313. */
  2314. void bond_loadbalance_arp_mon(struct work_struct *work)
  2315. {
  2316. struct bonding *bond = container_of(work, struct bonding,
  2317. arp_work.work);
  2318. struct slave *slave, *oldcurrent;
  2319. int do_failover = 0;
  2320. int delta_in_ticks;
  2321. int i;
  2322. read_lock(&bond->lock);
  2323. delta_in_ticks = (bond->params.arp_interval * HZ) / 1000;
  2324. if (bond->kill_timers) {
  2325. goto out;
  2326. }
  2327. if (bond->slave_cnt == 0) {
  2328. goto re_arm;
  2329. }
  2330. read_lock(&bond->curr_slave_lock);
  2331. oldcurrent = bond->curr_active_slave;
  2332. read_unlock(&bond->curr_slave_lock);
  2333. /* see if any of the previous devices are up now (i.e. they have
  2334. * xmt and rcv traffic). the curr_active_slave does not come into
  2335. * the picture unless it is null. also, slave->jiffies is not needed
  2336. * here because we send an arp on each slave and give a slave as
  2337. * long as it needs to get the tx/rx within the delta.
  2338. * TODO: what about up/down delay in arp mode? it wasn't here before
  2339. * so it can wait
  2340. */
  2341. bond_for_each_slave(bond, slave, i) {
  2342. if (slave->link != BOND_LINK_UP) {
  2343. if (time_before_eq(jiffies, slave->dev->trans_start + delta_in_ticks) &&
  2344. time_before_eq(jiffies, slave->dev->last_rx + delta_in_ticks)) {
  2345. slave->link = BOND_LINK_UP;
  2346. slave->state = BOND_STATE_ACTIVE;
  2347. /* primary_slave has no meaning in round-robin
  2348. * mode. the window of a slave being up and
  2349. * curr_active_slave being null after enslaving
  2350. * is closed.
  2351. */
  2352. if (!oldcurrent) {
  2353. printk(KERN_INFO DRV_NAME
  2354. ": %s: link status definitely "
  2355. "up for interface %s, ",
  2356. bond->dev->name,
  2357. slave->dev->name);
  2358. do_failover = 1;
  2359. } else {
  2360. printk(KERN_INFO DRV_NAME
  2361. ": %s: interface %s is now up\n",
  2362. bond->dev->name,
  2363. slave->dev->name);
  2364. }
  2365. }
  2366. } else {
  2367. /* slave->link == BOND_LINK_UP */
  2368. /* not all switches will respond to an arp request
  2369. * when the source ip is 0, so don't take the link down
  2370. * if we don't know our ip yet
  2371. */
  2372. if (time_after_eq(jiffies, slave->dev->trans_start + 2*delta_in_ticks) ||
  2373. (time_after_eq(jiffies, slave->dev->last_rx + 2*delta_in_ticks) &&
  2374. bond_has_ip(bond))) {
  2375. slave->link = BOND_LINK_DOWN;
  2376. slave->state = BOND_STATE_BACKUP;
  2377. if (slave->link_failure_count < UINT_MAX) {
  2378. slave->link_failure_count++;
  2379. }
  2380. printk(KERN_INFO DRV_NAME
  2381. ": %s: interface %s is now down.\n",
  2382. bond->dev->name,
  2383. slave->dev->name);
  2384. if (slave == oldcurrent) {
  2385. do_failover = 1;
  2386. }
  2387. }
  2388. }
  2389. /* note: if switch is in round-robin mode, all links
  2390. * must tx arp to ensure all links rx an arp - otherwise
  2391. * links may oscillate or not come up at all; if switch is
  2392. * in something like xor mode, there is nothing we can
  2393. * do - all replies will be rx'ed on same link causing slaves
  2394. * to be unstable during low/no traffic periods
  2395. */
  2396. if (IS_UP(slave->dev)) {
  2397. bond_arp_send_all(bond, slave);
  2398. }
  2399. }
  2400. if (do_failover) {
  2401. write_lock_bh(&bond->curr_slave_lock);
  2402. bond_select_active_slave(bond);
  2403. write_unlock_bh(&bond->curr_slave_lock);
  2404. }
  2405. re_arm:
  2406. if (bond->params.arp_interval)
  2407. queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
  2408. out:
  2409. read_unlock(&bond->lock);
  2410. }
  2411. /*
  2412. * When using arp monitoring in active-backup mode, this function is
  2413. * called to determine if any backup slaves have went down or a new
  2414. * current slave needs to be found.
  2415. * The backup slaves never generate traffic, they are considered up by merely
  2416. * receiving traffic. If the current slave goes down, each backup slave will
  2417. * be given the opportunity to tx/rx an arp before being taken down - this
  2418. * prevents all slaves from being taken down due to the current slave not
  2419. * sending any traffic for the backups to receive. The arps are not necessarily
  2420. * necessary, any tx and rx traffic will keep the current slave up. While any
  2421. * rx traffic will keep the backup slaves up, the current slave is responsible
  2422. * for generating traffic to keep them up regardless of any other traffic they
  2423. * may have received.
  2424. * see loadbalance_arp_monitor for arp monitoring in load balancing mode
  2425. */
  2426. void bond_activebackup_arp_mon(struct work_struct *work)
  2427. {
  2428. struct bonding *bond = container_of(work, struct bonding,
  2429. arp_work.work);
  2430. struct slave *slave;
  2431. int delta_in_ticks;
  2432. int i;
  2433. read_lock(&bond->lock);
  2434. delta_in_ticks = (bond->params.arp_interval * HZ) / 1000;
  2435. if (bond->kill_timers) {
  2436. goto out;
  2437. }
  2438. if (bond->slave_cnt == 0) {
  2439. goto re_arm;
  2440. }
  2441. /* determine if any slave has come up or any backup slave has
  2442. * gone down
  2443. * TODO: what about up/down delay in arp mode? it wasn't here before
  2444. * so it can wait
  2445. */
  2446. bond_for_each_slave(bond, slave, i) {
  2447. if (slave->link != BOND_LINK_UP) {
  2448. if (time_before_eq(jiffies,
  2449. slave_last_rx(bond, slave) + delta_in_ticks)) {
  2450. slave->link = BOND_LINK_UP;
  2451. write_lock_bh(&bond->curr_slave_lock);
  2452. if ((!bond->curr_active_slave) &&
  2453. time_before_eq(jiffies, slave->dev->trans_start + delta_in_ticks)) {
  2454. bond_change_active_slave(bond, slave);
  2455. bond->current_arp_slave = NULL;
  2456. } else if (bond->curr_active_slave != slave) {
  2457. /* this slave has just come up but we
  2458. * already have a current slave; this
  2459. * can also happen if bond_enslave adds
  2460. * a new slave that is up while we are
  2461. * searching for a new slave
  2462. */
  2463. bond_set_slave_inactive_flags(slave);
  2464. bond->current_arp_slave = NULL;
  2465. }
  2466. bond_set_carrier(bond);
  2467. if (slave == bond->curr_active_slave) {
  2468. printk(KERN_INFO DRV_NAME
  2469. ": %s: %s is up and now the "
  2470. "active interface\n",
  2471. bond->dev->name,
  2472. slave->dev->name);
  2473. netif_carrier_on(bond->dev);
  2474. } else {
  2475. printk(KERN_INFO DRV_NAME
  2476. ": %s: backup interface %s is "
  2477. "now up\n",
  2478. bond->dev->name,
  2479. slave->dev->name);
  2480. }
  2481. write_unlock_bh(&bond->curr_slave_lock);
  2482. }
  2483. } else {
  2484. read_lock(&bond->curr_slave_lock);
  2485. if ((slave != bond->curr_active_slave) &&
  2486. (!bond->current_arp_slave) &&
  2487. (time_after_eq(jiffies, slave_last_rx(bond, slave) + 3*delta_in_ticks) &&
  2488. bond_has_ip(bond))) {
  2489. /* a backup slave has gone down; three times
  2490. * the delta allows the current slave to be
  2491. * taken out before the backup slave.
  2492. * note: a non-null current_arp_slave indicates
  2493. * the curr_active_slave went down and we are
  2494. * searching for a new one; under this
  2495. * condition we only take the curr_active_slave
  2496. * down - this gives each slave a chance to
  2497. * tx/rx traffic before being taken out
  2498. */
  2499. read_unlock(&bond->curr_slave_lock);
  2500. slave->link = BOND_LINK_DOWN;
  2501. if (slave->link_failure_count < UINT_MAX) {
  2502. slave->link_failure_count++;
  2503. }
  2504. bond_set_slave_inactive_flags(slave);
  2505. printk(KERN_INFO DRV_NAME
  2506. ": %s: backup interface %s is now down\n",
  2507. bond->dev->name,
  2508. slave->dev->name);
  2509. } else {
  2510. read_unlock(&bond->curr_slave_lock);
  2511. }
  2512. }
  2513. }
  2514. read_lock(&bond->curr_slave_lock);
  2515. slave = bond->curr_active_slave;
  2516. read_unlock(&bond->curr_slave_lock);
  2517. if (slave) {
  2518. /* if we have sent traffic in the past 2*arp_intervals but
  2519. * haven't xmit and rx traffic in that time interval, select
  2520. * a different slave. slave->jiffies is only updated when
  2521. * a slave first becomes the curr_active_slave - not necessarily
  2522. * after every arp; this ensures the slave has a full 2*delta
  2523. * before being taken out. if a primary is being used, check
  2524. * if it is up and needs to take over as the curr_active_slave
  2525. */
  2526. if ((time_after_eq(jiffies, slave->dev->trans_start + 2*delta_in_ticks) ||
  2527. (time_after_eq(jiffies, slave_last_rx(bond, slave) + 2*delta_in_ticks) &&
  2528. bond_has_ip(bond))) &&
  2529. time_after_eq(jiffies, slave->jiffies + 2*delta_in_ticks)) {
  2530. slave->link = BOND_LINK_DOWN;
  2531. if (slave->link_failure_count < UINT_MAX) {
  2532. slave->link_failure_count++;
  2533. }
  2534. printk(KERN_INFO DRV_NAME
  2535. ": %s: link status down for active interface "
  2536. "%s, disabling it\n",
  2537. bond->dev->name,
  2538. slave->dev->name);
  2539. write_lock_bh(&bond->curr_slave_lock);
  2540. bond_select_active_slave(bond);
  2541. slave = bond->curr_active_slave;
  2542. write_unlock_bh(&bond->curr_slave_lock);
  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. write_lock_bh(&bond->curr_slave_lock);
  2559. bond_change_active_slave(bond, bond->primary_slave);
  2560. write_unlock_bh(&bond->curr_slave_lock);
  2561. slave = bond->primary_slave;
  2562. slave->jiffies = jiffies;
  2563. } else {
  2564. bond->current_arp_slave = NULL;
  2565. }
  2566. /* the current slave must tx an arp to ensure backup slaves
  2567. * rx traffic
  2568. */
  2569. if (slave && bond_has_ip(bond)) {
  2570. bond_arp_send_all(bond, slave);
  2571. }
  2572. }
  2573. /* if we don't have a curr_active_slave, search for the next available
  2574. * backup slave from the current_arp_slave and make it the candidate
  2575. * for becoming the curr_active_slave
  2576. */
  2577. if (!slave) {
  2578. if (!bond->current_arp_slave) {
  2579. bond->current_arp_slave = bond->first_slave;
  2580. }
  2581. if (bond->current_arp_slave) {
  2582. bond_set_slave_inactive_flags(bond->current_arp_slave);
  2583. /* search for next candidate */
  2584. bond_for_each_slave_from(bond, slave, i, bond->current_arp_slave->next) {
  2585. if (IS_UP(slave->dev)) {
  2586. slave->link = BOND_LINK_BACK;
  2587. bond_set_slave_active_flags(slave);
  2588. bond_arp_send_all(bond, slave);
  2589. slave->jiffies = jiffies;
  2590. bond->current_arp_slave = slave;
  2591. break;
  2592. }
  2593. /* if the link state is up at this point, we
  2594. * mark it down - this can happen if we have
  2595. * simultaneous link failures and
  2596. * reselect_active_interface doesn't make this
  2597. * one the current slave so it is still marked
  2598. * up when it is actually down
  2599. */
  2600. if (slave->link == BOND_LINK_UP) {
  2601. slave->link = BOND_LINK_DOWN;
  2602. if (slave->link_failure_count < UINT_MAX) {
  2603. slave->link_failure_count++;
  2604. }
  2605. bond_set_slave_inactive_flags(slave);
  2606. printk(KERN_INFO DRV_NAME
  2607. ": %s: backup interface %s is "
  2608. "now down.\n",
  2609. bond->dev->name,
  2610. slave->dev->name);
  2611. }
  2612. }
  2613. }
  2614. }
  2615. re_arm:
  2616. if (bond->params.arp_interval) {
  2617. queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
  2618. }
  2619. out:
  2620. read_unlock(&bond->lock);
  2621. }
  2622. /*------------------------------ proc/seq_file-------------------------------*/
  2623. #ifdef CONFIG_PROC_FS
  2624. #define SEQ_START_TOKEN ((void *)1)
  2625. static void *bond_info_seq_start(struct seq_file *seq, loff_t *pos)
  2626. {
  2627. struct bonding *bond = seq->private;
  2628. loff_t off = 0;
  2629. struct slave *slave;
  2630. int i;
  2631. /* make sure the bond won't be taken away */
  2632. read_lock(&dev_base_lock);
  2633. read_lock(&bond->lock);
  2634. if (*pos == 0) {
  2635. return SEQ_START_TOKEN;
  2636. }
  2637. bond_for_each_slave(bond, slave, i) {
  2638. if (++off == *pos) {
  2639. return slave;
  2640. }
  2641. }
  2642. return NULL;
  2643. }
  2644. static void *bond_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2645. {
  2646. struct bonding *bond = seq->private;
  2647. struct slave *slave = v;
  2648. ++*pos;
  2649. if (v == SEQ_START_TOKEN) {
  2650. return bond->first_slave;
  2651. }
  2652. slave = slave->next;
  2653. return (slave == bond->first_slave) ? NULL : slave;
  2654. }
  2655. static void bond_info_seq_stop(struct seq_file *seq, void *v)
  2656. {
  2657. struct bonding *bond = seq->private;
  2658. read_unlock(&bond->lock);
  2659. read_unlock(&dev_base_lock);
  2660. }
  2661. static void bond_info_show_master(struct seq_file *seq)
  2662. {
  2663. struct bonding *bond = seq->private;
  2664. struct slave *curr;
  2665. int i;
  2666. u32 target;
  2667. read_lock(&bond->curr_slave_lock);
  2668. curr = bond->curr_active_slave;
  2669. read_unlock(&bond->curr_slave_lock);
  2670. seq_printf(seq, "Bonding Mode: %s",
  2671. bond_mode_name(bond->params.mode));
  2672. if (bond->params.mode == BOND_MODE_ACTIVEBACKUP &&
  2673. bond->params.fail_over_mac)
  2674. seq_printf(seq, " (fail_over_mac)");
  2675. seq_printf(seq, "\n");
  2676. if (bond->params.mode == BOND_MODE_XOR ||
  2677. bond->params.mode == BOND_MODE_8023AD) {
  2678. seq_printf(seq, "Transmit Hash Policy: %s (%d)\n",
  2679. xmit_hashtype_tbl[bond->params.xmit_policy].modename,
  2680. bond->params.xmit_policy);
  2681. }
  2682. if (USES_PRIMARY(bond->params.mode)) {
  2683. seq_printf(seq, "Primary Slave: %s\n",
  2684. (bond->primary_slave) ?
  2685. bond->primary_slave->dev->name : "None");
  2686. seq_printf(seq, "Currently Active Slave: %s\n",
  2687. (curr) ? curr->dev->name : "None");
  2688. }
  2689. seq_printf(seq, "MII Status: %s\n", netif_carrier_ok(bond->dev) ?
  2690. "up" : "down");
  2691. seq_printf(seq, "MII Polling Interval (ms): %d\n", bond->params.miimon);
  2692. seq_printf(seq, "Up Delay (ms): %d\n",
  2693. bond->params.updelay * bond->params.miimon);
  2694. seq_printf(seq, "Down Delay (ms): %d\n",
  2695. bond->params.downdelay * bond->params.miimon);
  2696. /* ARP information */
  2697. if(bond->params.arp_interval > 0) {
  2698. int printed=0;
  2699. seq_printf(seq, "ARP Polling Interval (ms): %d\n",
  2700. bond->params.arp_interval);
  2701. seq_printf(seq, "ARP IP target/s (n.n.n.n form):");
  2702. for(i = 0; (i < BOND_MAX_ARP_TARGETS) ;i++) {
  2703. if (!bond->params.arp_targets[i])
  2704. continue;
  2705. if (printed)
  2706. seq_printf(seq, ",");
  2707. target = ntohl(bond->params.arp_targets[i]);
  2708. seq_printf(seq, " %d.%d.%d.%d", HIPQUAD(target));
  2709. printed = 1;
  2710. }
  2711. seq_printf(seq, "\n");
  2712. }
  2713. if (bond->params.mode == BOND_MODE_8023AD) {
  2714. struct ad_info ad_info;
  2715. DECLARE_MAC_BUF(mac);
  2716. seq_puts(seq, "\n802.3ad info\n");
  2717. seq_printf(seq, "LACP rate: %s\n",
  2718. (bond->params.lacp_fast) ? "fast" : "slow");
  2719. if (bond_3ad_get_active_agg_info(bond, &ad_info)) {
  2720. seq_printf(seq, "bond %s has no active aggregator\n",
  2721. bond->dev->name);
  2722. } else {
  2723. seq_printf(seq, "Active Aggregator Info:\n");
  2724. seq_printf(seq, "\tAggregator ID: %d\n",
  2725. ad_info.aggregator_id);
  2726. seq_printf(seq, "\tNumber of ports: %d\n",
  2727. ad_info.ports);
  2728. seq_printf(seq, "\tActor Key: %d\n",
  2729. ad_info.actor_key);
  2730. seq_printf(seq, "\tPartner Key: %d\n",
  2731. ad_info.partner_key);
  2732. seq_printf(seq, "\tPartner Mac Address: %s\n",
  2733. print_mac(mac, ad_info.partner_system));
  2734. }
  2735. }
  2736. }
  2737. static void bond_info_show_slave(struct seq_file *seq, const struct slave *slave)
  2738. {
  2739. struct bonding *bond = seq->private;
  2740. DECLARE_MAC_BUF(mac);
  2741. seq_printf(seq, "\nSlave Interface: %s\n", slave->dev->name);
  2742. seq_printf(seq, "MII Status: %s\n",
  2743. (slave->link == BOND_LINK_UP) ? "up" : "down");
  2744. seq_printf(seq, "Link Failure Count: %u\n",
  2745. slave->link_failure_count);
  2746. seq_printf(seq,
  2747. "Permanent HW addr: %s\n",
  2748. print_mac(mac, slave->perm_hwaddr));
  2749. if (bond->params.mode == BOND_MODE_8023AD) {
  2750. const struct aggregator *agg
  2751. = SLAVE_AD_INFO(slave).port.aggregator;
  2752. if (agg) {
  2753. seq_printf(seq, "Aggregator ID: %d\n",
  2754. agg->aggregator_identifier);
  2755. } else {
  2756. seq_puts(seq, "Aggregator ID: N/A\n");
  2757. }
  2758. }
  2759. }
  2760. static int bond_info_seq_show(struct seq_file *seq, void *v)
  2761. {
  2762. if (v == SEQ_START_TOKEN) {
  2763. seq_printf(seq, "%s\n", version);
  2764. bond_info_show_master(seq);
  2765. } else {
  2766. bond_info_show_slave(seq, v);
  2767. }
  2768. return 0;
  2769. }
  2770. static struct seq_operations bond_info_seq_ops = {
  2771. .start = bond_info_seq_start,
  2772. .next = bond_info_seq_next,
  2773. .stop = bond_info_seq_stop,
  2774. .show = bond_info_seq_show,
  2775. };
  2776. static int bond_info_open(struct inode *inode, struct file *file)
  2777. {
  2778. struct seq_file *seq;
  2779. struct proc_dir_entry *proc;
  2780. int res;
  2781. res = seq_open(file, &bond_info_seq_ops);
  2782. if (!res) {
  2783. /* recover the pointer buried in proc_dir_entry data */
  2784. seq = file->private_data;
  2785. proc = PDE(inode);
  2786. seq->private = proc->data;
  2787. }
  2788. return res;
  2789. }
  2790. static const struct file_operations bond_info_fops = {
  2791. .owner = THIS_MODULE,
  2792. .open = bond_info_open,
  2793. .read = seq_read,
  2794. .llseek = seq_lseek,
  2795. .release = seq_release,
  2796. };
  2797. static int bond_create_proc_entry(struct bonding *bond)
  2798. {
  2799. struct net_device *bond_dev = bond->dev;
  2800. if (bond_proc_dir) {
  2801. bond->proc_entry = create_proc_entry(bond_dev->name,
  2802. S_IRUGO,
  2803. bond_proc_dir);
  2804. if (bond->proc_entry == NULL) {
  2805. printk(KERN_WARNING DRV_NAME
  2806. ": Warning: Cannot create /proc/net/%s/%s\n",
  2807. DRV_NAME, bond_dev->name);
  2808. } else {
  2809. bond->proc_entry->data = bond;
  2810. bond->proc_entry->proc_fops = &bond_info_fops;
  2811. bond->proc_entry->owner = THIS_MODULE;
  2812. memcpy(bond->proc_file_name, bond_dev->name, IFNAMSIZ);
  2813. }
  2814. }
  2815. return 0;
  2816. }
  2817. static void bond_remove_proc_entry(struct bonding *bond)
  2818. {
  2819. if (bond_proc_dir && bond->proc_entry) {
  2820. remove_proc_entry(bond->proc_file_name, bond_proc_dir);
  2821. memset(bond->proc_file_name, 0, IFNAMSIZ);
  2822. bond->proc_entry = NULL;
  2823. }
  2824. }
  2825. /* Create the bonding directory under /proc/net, if doesn't exist yet.
  2826. * Caller must hold rtnl_lock.
  2827. */
  2828. static void bond_create_proc_dir(void)
  2829. {
  2830. int len = strlen(DRV_NAME);
  2831. for (bond_proc_dir = init_net.proc_net->subdir; bond_proc_dir;
  2832. bond_proc_dir = bond_proc_dir->next) {
  2833. if ((bond_proc_dir->namelen == len) &&
  2834. !memcmp(bond_proc_dir->name, DRV_NAME, len)) {
  2835. break;
  2836. }
  2837. }
  2838. if (!bond_proc_dir) {
  2839. bond_proc_dir = proc_mkdir(DRV_NAME, init_net.proc_net);
  2840. if (bond_proc_dir) {
  2841. bond_proc_dir->owner = THIS_MODULE;
  2842. } else {
  2843. printk(KERN_WARNING DRV_NAME
  2844. ": Warning: cannot create /proc/net/%s\n",
  2845. DRV_NAME);
  2846. }
  2847. }
  2848. }
  2849. /* Destroy the bonding directory under /proc/net, if empty.
  2850. * Caller must hold rtnl_lock.
  2851. */
  2852. static void bond_destroy_proc_dir(void)
  2853. {
  2854. struct proc_dir_entry *de;
  2855. if (!bond_proc_dir) {
  2856. return;
  2857. }
  2858. /* verify that the /proc dir is empty */
  2859. for (de = bond_proc_dir->subdir; de; de = de->next) {
  2860. /* ignore . and .. */
  2861. if (*(de->name) != '.') {
  2862. break;
  2863. }
  2864. }
  2865. if (de) {
  2866. if (bond_proc_dir->owner == THIS_MODULE) {
  2867. bond_proc_dir->owner = NULL;
  2868. }
  2869. } else {
  2870. remove_proc_entry(DRV_NAME, init_net.proc_net);
  2871. bond_proc_dir = NULL;
  2872. }
  2873. }
  2874. #endif /* CONFIG_PROC_FS */
  2875. /*-------------------------- netdev event handling --------------------------*/
  2876. /*
  2877. * Change device name
  2878. */
  2879. static int bond_event_changename(struct bonding *bond)
  2880. {
  2881. #ifdef CONFIG_PROC_FS
  2882. bond_remove_proc_entry(bond);
  2883. bond_create_proc_entry(bond);
  2884. #endif
  2885. down_write(&(bonding_rwsem));
  2886. bond_destroy_sysfs_entry(bond);
  2887. bond_create_sysfs_entry(bond);
  2888. up_write(&(bonding_rwsem));
  2889. return NOTIFY_DONE;
  2890. }
  2891. static int bond_master_netdev_event(unsigned long event, struct net_device *bond_dev)
  2892. {
  2893. struct bonding *event_bond = bond_dev->priv;
  2894. switch (event) {
  2895. case NETDEV_CHANGENAME:
  2896. return bond_event_changename(event_bond);
  2897. case NETDEV_UNREGISTER:
  2898. bond_release_all(event_bond->dev);
  2899. break;
  2900. default:
  2901. break;
  2902. }
  2903. return NOTIFY_DONE;
  2904. }
  2905. static int bond_slave_netdev_event(unsigned long event, struct net_device *slave_dev)
  2906. {
  2907. struct net_device *bond_dev = slave_dev->master;
  2908. struct bonding *bond = bond_dev->priv;
  2909. switch (event) {
  2910. case NETDEV_UNREGISTER:
  2911. if (bond_dev) {
  2912. if (bond->setup_by_slave)
  2913. bond_release_and_destroy(bond_dev, slave_dev);
  2914. else
  2915. bond_release(bond_dev, slave_dev);
  2916. }
  2917. break;
  2918. case NETDEV_CHANGE:
  2919. /*
  2920. * TODO: is this what we get if somebody
  2921. * sets up a hierarchical bond, then rmmod's
  2922. * one of the slave bonding devices?
  2923. */
  2924. break;
  2925. case NETDEV_DOWN:
  2926. /*
  2927. * ... Or is it this?
  2928. */
  2929. break;
  2930. case NETDEV_CHANGEMTU:
  2931. /*
  2932. * TODO: Should slaves be allowed to
  2933. * independently alter their MTU? For
  2934. * an active-backup bond, slaves need
  2935. * not be the same type of device, so
  2936. * MTUs may vary. For other modes,
  2937. * slaves arguably should have the
  2938. * same MTUs. To do this, we'd need to
  2939. * take over the slave's change_mtu
  2940. * function for the duration of their
  2941. * servitude.
  2942. */
  2943. break;
  2944. case NETDEV_CHANGENAME:
  2945. /*
  2946. * TODO: handle changing the primary's name
  2947. */
  2948. break;
  2949. case NETDEV_FEAT_CHANGE:
  2950. bond_compute_features(bond);
  2951. break;
  2952. default:
  2953. break;
  2954. }
  2955. return NOTIFY_DONE;
  2956. }
  2957. /*
  2958. * bond_netdev_event: handle netdev notifier chain events.
  2959. *
  2960. * This function receives events for the netdev chain. The caller (an
  2961. * ioctl handler calling blocking_notifier_call_chain) holds the necessary
  2962. * locks for us to safely manipulate the slave devices (RTNL lock,
  2963. * dev_probe_lock).
  2964. */
  2965. static int bond_netdev_event(struct notifier_block *this, unsigned long event, void *ptr)
  2966. {
  2967. struct net_device *event_dev = (struct net_device *)ptr;
  2968. if (event_dev->nd_net != &init_net)
  2969. return NOTIFY_DONE;
  2970. dprintk("event_dev: %s, event: %lx\n",
  2971. (event_dev ? event_dev->name : "None"),
  2972. event);
  2973. if (!(event_dev->priv_flags & IFF_BONDING))
  2974. return NOTIFY_DONE;
  2975. if (event_dev->flags & IFF_MASTER) {
  2976. dprintk("IFF_MASTER\n");
  2977. return bond_master_netdev_event(event, event_dev);
  2978. }
  2979. if (event_dev->flags & IFF_SLAVE) {
  2980. dprintk("IFF_SLAVE\n");
  2981. return bond_slave_netdev_event(event, event_dev);
  2982. }
  2983. return NOTIFY_DONE;
  2984. }
  2985. /*
  2986. * bond_inetaddr_event: handle inetaddr notifier chain events.
  2987. *
  2988. * We keep track of device IPs primarily to use as source addresses in
  2989. * ARP monitor probes (rather than spewing out broadcasts all the time).
  2990. *
  2991. * We track one IP for the main device (if it has one), plus one per VLAN.
  2992. */
  2993. static int bond_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
  2994. {
  2995. struct in_ifaddr *ifa = ptr;
  2996. struct net_device *vlan_dev, *event_dev = ifa->ifa_dev->dev;
  2997. struct bonding *bond, *bond_next;
  2998. struct vlan_entry *vlan, *vlan_next;
  2999. list_for_each_entry_safe(bond, bond_next, &bond_dev_list, bond_list) {
  3000. if (bond->dev == event_dev) {
  3001. switch (event) {
  3002. case NETDEV_UP:
  3003. bond->master_ip = ifa->ifa_local;
  3004. return NOTIFY_OK;
  3005. case NETDEV_DOWN:
  3006. bond->master_ip = bond_glean_dev_ip(bond->dev);
  3007. return NOTIFY_OK;
  3008. default:
  3009. return NOTIFY_DONE;
  3010. }
  3011. }
  3012. if (list_empty(&bond->vlan_list))
  3013. continue;
  3014. list_for_each_entry_safe(vlan, vlan_next, &bond->vlan_list,
  3015. vlan_list) {
  3016. vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
  3017. if (vlan_dev == event_dev) {
  3018. switch (event) {
  3019. case NETDEV_UP:
  3020. vlan->vlan_ip = ifa->ifa_local;
  3021. return NOTIFY_OK;
  3022. case NETDEV_DOWN:
  3023. vlan->vlan_ip =
  3024. bond_glean_dev_ip(vlan_dev);
  3025. return NOTIFY_OK;
  3026. default:
  3027. return NOTIFY_DONE;
  3028. }
  3029. }
  3030. }
  3031. }
  3032. return NOTIFY_DONE;
  3033. }
  3034. static struct notifier_block bond_netdev_notifier = {
  3035. .notifier_call = bond_netdev_event,
  3036. };
  3037. static struct notifier_block bond_inetaddr_notifier = {
  3038. .notifier_call = bond_inetaddr_event,
  3039. };
  3040. /*-------------------------- Packet type handling ---------------------------*/
  3041. /* register to receive lacpdus on a bond */
  3042. static void bond_register_lacpdu(struct bonding *bond)
  3043. {
  3044. struct packet_type *pk_type = &(BOND_AD_INFO(bond).ad_pkt_type);
  3045. /* initialize packet type */
  3046. pk_type->type = PKT_TYPE_LACPDU;
  3047. pk_type->dev = bond->dev;
  3048. pk_type->func = bond_3ad_lacpdu_recv;
  3049. dev_add_pack(pk_type);
  3050. }
  3051. /* unregister to receive lacpdus on a bond */
  3052. static void bond_unregister_lacpdu(struct bonding *bond)
  3053. {
  3054. dev_remove_pack(&(BOND_AD_INFO(bond).ad_pkt_type));
  3055. }
  3056. void bond_register_arp(struct bonding *bond)
  3057. {
  3058. struct packet_type *pt = &bond->arp_mon_pt;
  3059. if (pt->type)
  3060. return;
  3061. pt->type = htons(ETH_P_ARP);
  3062. pt->dev = bond->dev;
  3063. pt->func = bond_arp_rcv;
  3064. dev_add_pack(pt);
  3065. }
  3066. void bond_unregister_arp(struct bonding *bond)
  3067. {
  3068. struct packet_type *pt = &bond->arp_mon_pt;
  3069. dev_remove_pack(pt);
  3070. pt->type = 0;
  3071. }
  3072. /*---------------------------- Hashing Policies -----------------------------*/
  3073. /*
  3074. * Hash for the output device based upon layer 2 and layer 3 data. If
  3075. * the packet is not IP mimic bond_xmit_hash_policy_l2()
  3076. */
  3077. static int bond_xmit_hash_policy_l23(struct sk_buff *skb,
  3078. struct net_device *bond_dev, int count)
  3079. {
  3080. struct ethhdr *data = (struct ethhdr *)skb->data;
  3081. struct iphdr *iph = ip_hdr(skb);
  3082. if (skb->protocol == __constant_htons(ETH_P_IP)) {
  3083. return ((ntohl(iph->saddr ^ iph->daddr) & 0xffff) ^
  3084. (data->h_dest[5] ^ bond_dev->dev_addr[5])) % count;
  3085. }
  3086. return (data->h_dest[5] ^ bond_dev->dev_addr[5]) % count;
  3087. }
  3088. /*
  3089. * Hash for the output device based upon layer 3 and layer 4 data. If
  3090. * the packet is a frag or not TCP or UDP, just use layer 3 data. If it is
  3091. * altogether not IP, mimic bond_xmit_hash_policy_l2()
  3092. */
  3093. static int bond_xmit_hash_policy_l34(struct sk_buff *skb,
  3094. struct net_device *bond_dev, int count)
  3095. {
  3096. struct ethhdr *data = (struct ethhdr *)skb->data;
  3097. struct iphdr *iph = ip_hdr(skb);
  3098. __be16 *layer4hdr = (__be16 *)((u32 *)iph + iph->ihl);
  3099. int layer4_xor = 0;
  3100. if (skb->protocol == __constant_htons(ETH_P_IP)) {
  3101. if (!(iph->frag_off & __constant_htons(IP_MF|IP_OFFSET)) &&
  3102. (iph->protocol == IPPROTO_TCP ||
  3103. iph->protocol == IPPROTO_UDP)) {
  3104. layer4_xor = ntohs((*layer4hdr ^ *(layer4hdr + 1)));
  3105. }
  3106. return (layer4_xor ^
  3107. ((ntohl(iph->saddr ^ iph->daddr)) & 0xffff)) % count;
  3108. }
  3109. return (data->h_dest[5] ^ bond_dev->dev_addr[5]) % count;
  3110. }
  3111. /*
  3112. * Hash for the output device based upon layer 2 data
  3113. */
  3114. static int bond_xmit_hash_policy_l2(struct sk_buff *skb,
  3115. struct net_device *bond_dev, int count)
  3116. {
  3117. struct ethhdr *data = (struct ethhdr *)skb->data;
  3118. return (data->h_dest[5] ^ bond_dev->dev_addr[5]) % count;
  3119. }
  3120. /*-------------------------- Device entry points ----------------------------*/
  3121. static int bond_open(struct net_device *bond_dev)
  3122. {
  3123. struct bonding *bond = bond_dev->priv;
  3124. bond->kill_timers = 0;
  3125. if ((bond->params.mode == BOND_MODE_TLB) ||
  3126. (bond->params.mode == BOND_MODE_ALB)) {
  3127. /* bond_alb_initialize must be called before the timer
  3128. * is started.
  3129. */
  3130. if (bond_alb_initialize(bond, (bond->params.mode == BOND_MODE_ALB))) {
  3131. /* something went wrong - fail the open operation */
  3132. return -1;
  3133. }
  3134. INIT_DELAYED_WORK(&bond->alb_work, bond_alb_monitor);
  3135. queue_delayed_work(bond->wq, &bond->alb_work, 0);
  3136. }
  3137. if (bond->params.miimon) { /* link check interval, in milliseconds. */
  3138. INIT_DELAYED_WORK(&bond->mii_work, bond_mii_monitor);
  3139. queue_delayed_work(bond->wq, &bond->mii_work, 0);
  3140. }
  3141. if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
  3142. if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
  3143. INIT_DELAYED_WORK(&bond->arp_work,
  3144. bond_activebackup_arp_mon);
  3145. else
  3146. INIT_DELAYED_WORK(&bond->arp_work,
  3147. bond_loadbalance_arp_mon);
  3148. queue_delayed_work(bond->wq, &bond->arp_work, 0);
  3149. if (bond->params.arp_validate)
  3150. bond_register_arp(bond);
  3151. }
  3152. if (bond->params.mode == BOND_MODE_8023AD) {
  3153. INIT_DELAYED_WORK(&bond->ad_work, bond_3ad_state_machine_handler);
  3154. queue_delayed_work(bond->wq, &bond->ad_work, 0);
  3155. /* register to receive LACPDUs */
  3156. bond_register_lacpdu(bond);
  3157. }
  3158. return 0;
  3159. }
  3160. static int bond_close(struct net_device *bond_dev)
  3161. {
  3162. struct bonding *bond = bond_dev->priv;
  3163. if (bond->params.mode == BOND_MODE_8023AD) {
  3164. /* Unregister the receive of LACPDUs */
  3165. bond_unregister_lacpdu(bond);
  3166. }
  3167. if (bond->params.arp_validate)
  3168. bond_unregister_arp(bond);
  3169. write_lock_bh(&bond->lock);
  3170. /* signal timers not to re-arm */
  3171. bond->kill_timers = 1;
  3172. write_unlock_bh(&bond->lock);
  3173. if (bond->params.miimon) { /* link check interval, in milliseconds. */
  3174. cancel_delayed_work(&bond->mii_work);
  3175. }
  3176. if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
  3177. cancel_delayed_work(&bond->arp_work);
  3178. }
  3179. switch (bond->params.mode) {
  3180. case BOND_MODE_8023AD:
  3181. cancel_delayed_work(&bond->ad_work);
  3182. break;
  3183. case BOND_MODE_TLB:
  3184. case BOND_MODE_ALB:
  3185. cancel_delayed_work(&bond->alb_work);
  3186. break;
  3187. default:
  3188. break;
  3189. }
  3190. if ((bond->params.mode == BOND_MODE_TLB) ||
  3191. (bond->params.mode == BOND_MODE_ALB)) {
  3192. /* Must be called only after all
  3193. * slaves have been released
  3194. */
  3195. bond_alb_deinitialize(bond);
  3196. }
  3197. return 0;
  3198. }
  3199. static struct net_device_stats *bond_get_stats(struct net_device *bond_dev)
  3200. {
  3201. struct bonding *bond = bond_dev->priv;
  3202. struct net_device_stats *stats = &(bond->stats), *sstats;
  3203. struct net_device_stats local_stats;
  3204. struct slave *slave;
  3205. int i;
  3206. memset(&local_stats, 0, sizeof(struct net_device_stats));
  3207. read_lock_bh(&bond->lock);
  3208. bond_for_each_slave(bond, slave, i) {
  3209. sstats = slave->dev->get_stats(slave->dev);
  3210. local_stats.rx_packets += sstats->rx_packets;
  3211. local_stats.rx_bytes += sstats->rx_bytes;
  3212. local_stats.rx_errors += sstats->rx_errors;
  3213. local_stats.rx_dropped += sstats->rx_dropped;
  3214. local_stats.tx_packets += sstats->tx_packets;
  3215. local_stats.tx_bytes += sstats->tx_bytes;
  3216. local_stats.tx_errors += sstats->tx_errors;
  3217. local_stats.tx_dropped += sstats->tx_dropped;
  3218. local_stats.multicast += sstats->multicast;
  3219. local_stats.collisions += sstats->collisions;
  3220. local_stats.rx_length_errors += sstats->rx_length_errors;
  3221. local_stats.rx_over_errors += sstats->rx_over_errors;
  3222. local_stats.rx_crc_errors += sstats->rx_crc_errors;
  3223. local_stats.rx_frame_errors += sstats->rx_frame_errors;
  3224. local_stats.rx_fifo_errors += sstats->rx_fifo_errors;
  3225. local_stats.rx_missed_errors += sstats->rx_missed_errors;
  3226. local_stats.tx_aborted_errors += sstats->tx_aborted_errors;
  3227. local_stats.tx_carrier_errors += sstats->tx_carrier_errors;
  3228. local_stats.tx_fifo_errors += sstats->tx_fifo_errors;
  3229. local_stats.tx_heartbeat_errors += sstats->tx_heartbeat_errors;
  3230. local_stats.tx_window_errors += sstats->tx_window_errors;
  3231. }
  3232. memcpy(stats, &local_stats, sizeof(struct net_device_stats));
  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. /*
  3346. * Do promisc before checking multicast_mode
  3347. */
  3348. if ((bond_dev->flags & IFF_PROMISC) && !(bond->flags & IFF_PROMISC)) {
  3349. bond_set_promiscuity(bond, 1);
  3350. }
  3351. if (!(bond_dev->flags & IFF_PROMISC) && (bond->flags & IFF_PROMISC)) {
  3352. bond_set_promiscuity(bond, -1);
  3353. }
  3354. /* set allmulti flag to slaves */
  3355. if ((bond_dev->flags & IFF_ALLMULTI) && !(bond->flags & IFF_ALLMULTI)) {
  3356. bond_set_allmulti(bond, 1);
  3357. }
  3358. if (!(bond_dev->flags & IFF_ALLMULTI) && (bond->flags & IFF_ALLMULTI)) {
  3359. bond_set_allmulti(bond, -1);
  3360. }
  3361. read_lock(&bond->lock);
  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. read_unlock(&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. netif_tx_lock_bh(bond_dev);
  3839. bond_mc_list_destroy(bond);
  3840. netif_tx_unlock_bh(bond_dev);
  3841. /* Release the bonded slaves */
  3842. bond_release_all(bond_dev);
  3843. bond_deinit(bond_dev);
  3844. unregister_netdevice(bond_dev);
  3845. }
  3846. #ifdef CONFIG_PROC_FS
  3847. bond_destroy_proc_dir();
  3848. #endif
  3849. }
  3850. /*------------------------- Module initialization ---------------------------*/
  3851. /*
  3852. * Convert string input module parms. Accept either the
  3853. * number of the mode or its string name. A bit complicated because
  3854. * some mode names are substrings of other names, and calls from sysfs
  3855. * may have whitespace in the name (trailing newlines, for example).
  3856. */
  3857. int bond_parse_parm(const char *buf, struct bond_parm_tbl *tbl)
  3858. {
  3859. int mode = -1, i, rv;
  3860. char *p, modestr[BOND_MAX_MODENAME_LEN + 1] = { 0, };
  3861. for (p = (char *)buf; *p; p++)
  3862. if (!(isdigit(*p) || isspace(*p)))
  3863. break;
  3864. if (*p)
  3865. rv = sscanf(buf, "%20s", modestr);
  3866. else
  3867. rv = sscanf(buf, "%d", &mode);
  3868. if (!rv)
  3869. return -1;
  3870. for (i = 0; tbl[i].modename; i++) {
  3871. if (mode == tbl[i].mode)
  3872. return tbl[i].mode;
  3873. if (strcmp(modestr, tbl[i].modename) == 0)
  3874. return tbl[i].mode;
  3875. }
  3876. return -1;
  3877. }
  3878. static int bond_check_params(struct bond_params *params)
  3879. {
  3880. int arp_validate_value;
  3881. /*
  3882. * Convert string parameters.
  3883. */
  3884. if (mode) {
  3885. bond_mode = bond_parse_parm(mode, bond_mode_tbl);
  3886. if (bond_mode == -1) {
  3887. printk(KERN_ERR DRV_NAME
  3888. ": Error: Invalid bonding mode \"%s\"\n",
  3889. mode == NULL ? "NULL" : mode);
  3890. return -EINVAL;
  3891. }
  3892. }
  3893. if (xmit_hash_policy) {
  3894. if ((bond_mode != BOND_MODE_XOR) &&
  3895. (bond_mode != BOND_MODE_8023AD)) {
  3896. printk(KERN_INFO DRV_NAME
  3897. ": xor_mode param is irrelevant in mode %s\n",
  3898. bond_mode_name(bond_mode));
  3899. } else {
  3900. xmit_hashtype = bond_parse_parm(xmit_hash_policy,
  3901. xmit_hashtype_tbl);
  3902. if (xmit_hashtype == -1) {
  3903. printk(KERN_ERR DRV_NAME
  3904. ": Error: Invalid xmit_hash_policy \"%s\"\n",
  3905. xmit_hash_policy == NULL ? "NULL" :
  3906. xmit_hash_policy);
  3907. return -EINVAL;
  3908. }
  3909. }
  3910. }
  3911. if (lacp_rate) {
  3912. if (bond_mode != BOND_MODE_8023AD) {
  3913. printk(KERN_INFO DRV_NAME
  3914. ": lacp_rate param is irrelevant in mode %s\n",
  3915. bond_mode_name(bond_mode));
  3916. } else {
  3917. lacp_fast = bond_parse_parm(lacp_rate, bond_lacp_tbl);
  3918. if (lacp_fast == -1) {
  3919. printk(KERN_ERR DRV_NAME
  3920. ": Error: Invalid lacp rate \"%s\"\n",
  3921. lacp_rate == NULL ? "NULL" : lacp_rate);
  3922. return -EINVAL;
  3923. }
  3924. }
  3925. }
  3926. if (max_bonds < 1 || max_bonds > INT_MAX) {
  3927. printk(KERN_WARNING DRV_NAME
  3928. ": Warning: max_bonds (%d) not in range %d-%d, so it "
  3929. "was reset to BOND_DEFAULT_MAX_BONDS (%d)\n",
  3930. max_bonds, 1, INT_MAX, BOND_DEFAULT_MAX_BONDS);
  3931. max_bonds = BOND_DEFAULT_MAX_BONDS;
  3932. }
  3933. if (miimon < 0) {
  3934. printk(KERN_WARNING DRV_NAME
  3935. ": Warning: miimon module parameter (%d), "
  3936. "not in range 0-%d, so it was reset to %d\n",
  3937. miimon, INT_MAX, BOND_LINK_MON_INTERV);
  3938. miimon = BOND_LINK_MON_INTERV;
  3939. }
  3940. if (updelay < 0) {
  3941. printk(KERN_WARNING DRV_NAME
  3942. ": Warning: updelay module parameter (%d), "
  3943. "not in range 0-%d, so it was reset to 0\n",
  3944. updelay, INT_MAX);
  3945. updelay = 0;
  3946. }
  3947. if (downdelay < 0) {
  3948. printk(KERN_WARNING DRV_NAME
  3949. ": Warning: downdelay module parameter (%d), "
  3950. "not in range 0-%d, so it was reset to 0\n",
  3951. downdelay, INT_MAX);
  3952. downdelay = 0;
  3953. }
  3954. if ((use_carrier != 0) && (use_carrier != 1)) {
  3955. printk(KERN_WARNING DRV_NAME
  3956. ": Warning: use_carrier module parameter (%d), "
  3957. "not of valid value (0/1), so it was set to 1\n",
  3958. use_carrier);
  3959. use_carrier = 1;
  3960. }
  3961. /* reset values for 802.3ad */
  3962. if (bond_mode == BOND_MODE_8023AD) {
  3963. if (!miimon) {
  3964. printk(KERN_WARNING DRV_NAME
  3965. ": Warning: miimon must be specified, "
  3966. "otherwise bonding will not detect link "
  3967. "failure, speed and duplex which are "
  3968. "essential for 802.3ad operation\n");
  3969. printk(KERN_WARNING "Forcing miimon to 100msec\n");
  3970. miimon = 100;
  3971. }
  3972. }
  3973. /* reset values for TLB/ALB */
  3974. if ((bond_mode == BOND_MODE_TLB) ||
  3975. (bond_mode == BOND_MODE_ALB)) {
  3976. if (!miimon) {
  3977. printk(KERN_WARNING DRV_NAME
  3978. ": Warning: miimon must be specified, "
  3979. "otherwise bonding will not detect link "
  3980. "failure and link speed which are essential "
  3981. "for TLB/ALB load balancing\n");
  3982. printk(KERN_WARNING "Forcing miimon to 100msec\n");
  3983. miimon = 100;
  3984. }
  3985. }
  3986. if (bond_mode == BOND_MODE_ALB) {
  3987. printk(KERN_NOTICE DRV_NAME
  3988. ": In ALB mode you might experience client "
  3989. "disconnections upon reconnection of a link if the "
  3990. "bonding module updelay parameter (%d msec) is "
  3991. "incompatible with the forwarding delay time of the "
  3992. "switch\n",
  3993. updelay);
  3994. }
  3995. if (!miimon) {
  3996. if (updelay || downdelay) {
  3997. /* just warn the user the up/down delay will have
  3998. * no effect since miimon is zero...
  3999. */
  4000. printk(KERN_WARNING DRV_NAME
  4001. ": Warning: miimon module parameter not set "
  4002. "and updelay (%d) or downdelay (%d) module "
  4003. "parameter is set; updelay and downdelay have "
  4004. "no effect unless miimon is set\n",
  4005. updelay, downdelay);
  4006. }
  4007. } else {
  4008. /* don't allow arp monitoring */
  4009. if (arp_interval) {
  4010. printk(KERN_WARNING DRV_NAME
  4011. ": Warning: miimon (%d) and arp_interval (%d) "
  4012. "can't be used simultaneously, disabling ARP "
  4013. "monitoring\n",
  4014. miimon, arp_interval);
  4015. arp_interval = 0;
  4016. }
  4017. if ((updelay % miimon) != 0) {
  4018. printk(KERN_WARNING DRV_NAME
  4019. ": Warning: updelay (%d) is not a multiple "
  4020. "of miimon (%d), updelay rounded to %d ms\n",
  4021. updelay, miimon, (updelay / miimon) * miimon);
  4022. }
  4023. updelay /= miimon;
  4024. if ((downdelay % miimon) != 0) {
  4025. printk(KERN_WARNING DRV_NAME
  4026. ": Warning: downdelay (%d) is not a multiple "
  4027. "of miimon (%d), downdelay rounded to %d ms\n",
  4028. downdelay, miimon,
  4029. (downdelay / miimon) * miimon);
  4030. }
  4031. downdelay /= miimon;
  4032. }
  4033. if (arp_interval < 0) {
  4034. printk(KERN_WARNING DRV_NAME
  4035. ": Warning: arp_interval module parameter (%d) "
  4036. ", not in range 0-%d, so it was reset to %d\n",
  4037. arp_interval, INT_MAX, BOND_LINK_ARP_INTERV);
  4038. arp_interval = BOND_LINK_ARP_INTERV;
  4039. }
  4040. for (arp_ip_count = 0;
  4041. (arp_ip_count < BOND_MAX_ARP_TARGETS) && arp_ip_target[arp_ip_count];
  4042. arp_ip_count++) {
  4043. /* not complete check, but should be good enough to
  4044. catch mistakes */
  4045. if (!isdigit(arp_ip_target[arp_ip_count][0])) {
  4046. printk(KERN_WARNING DRV_NAME
  4047. ": Warning: bad arp_ip_target module parameter "
  4048. "(%s), ARP monitoring will not be performed\n",
  4049. arp_ip_target[arp_ip_count]);
  4050. arp_interval = 0;
  4051. } else {
  4052. __be32 ip = in_aton(arp_ip_target[arp_ip_count]);
  4053. arp_target[arp_ip_count] = ip;
  4054. }
  4055. }
  4056. if (arp_interval && !arp_ip_count) {
  4057. /* don't allow arping if no arp_ip_target given... */
  4058. printk(KERN_WARNING DRV_NAME
  4059. ": Warning: arp_interval module parameter (%d) "
  4060. "specified without providing an arp_ip_target "
  4061. "parameter, arp_interval was reset to 0\n",
  4062. arp_interval);
  4063. arp_interval = 0;
  4064. }
  4065. if (arp_validate) {
  4066. if (bond_mode != BOND_MODE_ACTIVEBACKUP) {
  4067. printk(KERN_ERR DRV_NAME
  4068. ": arp_validate only supported in active-backup mode\n");
  4069. return -EINVAL;
  4070. }
  4071. if (!arp_interval) {
  4072. printk(KERN_ERR DRV_NAME
  4073. ": arp_validate requires arp_interval\n");
  4074. return -EINVAL;
  4075. }
  4076. arp_validate_value = bond_parse_parm(arp_validate,
  4077. arp_validate_tbl);
  4078. if (arp_validate_value == -1) {
  4079. printk(KERN_ERR DRV_NAME
  4080. ": Error: invalid arp_validate \"%s\"\n",
  4081. arp_validate == NULL ? "NULL" : arp_validate);
  4082. return -EINVAL;
  4083. }
  4084. } else
  4085. arp_validate_value = 0;
  4086. if (miimon) {
  4087. printk(KERN_INFO DRV_NAME
  4088. ": MII link monitoring set to %d ms\n",
  4089. miimon);
  4090. } else if (arp_interval) {
  4091. int i;
  4092. printk(KERN_INFO DRV_NAME
  4093. ": ARP monitoring set to %d ms, validate %s, with %d target(s):",
  4094. arp_interval,
  4095. arp_validate_tbl[arp_validate_value].modename,
  4096. arp_ip_count);
  4097. for (i = 0; i < arp_ip_count; i++)
  4098. printk (" %s", arp_ip_target[i]);
  4099. printk("\n");
  4100. } else {
  4101. /* miimon and arp_interval not set, we need one so things
  4102. * work as expected, see bonding.txt for details
  4103. */
  4104. printk(KERN_WARNING DRV_NAME
  4105. ": Warning: either miimon or arp_interval and "
  4106. "arp_ip_target module parameters must be specified, "
  4107. "otherwise bonding will not detect link failures! see "
  4108. "bonding.txt for details.\n");
  4109. }
  4110. if (primary && !USES_PRIMARY(bond_mode)) {
  4111. /* currently, using a primary only makes sense
  4112. * in active backup, TLB or ALB modes
  4113. */
  4114. printk(KERN_WARNING DRV_NAME
  4115. ": Warning: %s primary device specified but has no "
  4116. "effect in %s mode\n",
  4117. primary, bond_mode_name(bond_mode));
  4118. primary = NULL;
  4119. }
  4120. if (fail_over_mac && (bond_mode != BOND_MODE_ACTIVEBACKUP))
  4121. printk(KERN_WARNING DRV_NAME
  4122. ": Warning: fail_over_mac only affects "
  4123. "active-backup mode.\n");
  4124. /* fill params struct with the proper values */
  4125. params->mode = bond_mode;
  4126. params->xmit_policy = xmit_hashtype;
  4127. params->miimon = miimon;
  4128. params->arp_interval = arp_interval;
  4129. params->arp_validate = arp_validate_value;
  4130. params->updelay = updelay;
  4131. params->downdelay = downdelay;
  4132. params->use_carrier = use_carrier;
  4133. params->lacp_fast = lacp_fast;
  4134. params->primary[0] = 0;
  4135. params->fail_over_mac = fail_over_mac;
  4136. if (primary) {
  4137. strncpy(params->primary, primary, IFNAMSIZ);
  4138. params->primary[IFNAMSIZ - 1] = 0;
  4139. }
  4140. memcpy(params->arp_targets, arp_target, sizeof(arp_target));
  4141. return 0;
  4142. }
  4143. static struct lock_class_key bonding_netdev_xmit_lock_key;
  4144. /* Create a new bond based on the specified name and bonding parameters.
  4145. * If name is NULL, obtain a suitable "bond%d" name for us.
  4146. * Caller must NOT hold rtnl_lock; we need to release it here before we
  4147. * set up our sysfs entries.
  4148. */
  4149. int bond_create(char *name, struct bond_params *params, struct bonding **newbond)
  4150. {
  4151. struct net_device *bond_dev;
  4152. struct bonding *bond, *nxt;
  4153. int res;
  4154. rtnl_lock();
  4155. down_write(&bonding_rwsem);
  4156. /* Check to see if the bond already exists. */
  4157. if (name) {
  4158. list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list)
  4159. if (strnicmp(bond->dev->name, name, IFNAMSIZ) == 0) {
  4160. printk(KERN_ERR DRV_NAME
  4161. ": cannot add bond %s; it already exists\n",
  4162. name);
  4163. res = -EPERM;
  4164. goto out_rtnl;
  4165. }
  4166. }
  4167. bond_dev = alloc_netdev(sizeof(struct bonding), name ? name : "",
  4168. ether_setup);
  4169. if (!bond_dev) {
  4170. printk(KERN_ERR DRV_NAME
  4171. ": %s: eek! can't alloc netdev!\n",
  4172. name);
  4173. res = -ENOMEM;
  4174. goto out_rtnl;
  4175. }
  4176. if (!name) {
  4177. res = dev_alloc_name(bond_dev, "bond%d");
  4178. if (res < 0)
  4179. goto out_netdev;
  4180. }
  4181. /* bond_init() must be called after dev_alloc_name() (for the
  4182. * /proc files), but before register_netdevice(), because we
  4183. * need to set function pointers.
  4184. */
  4185. res = bond_init(bond_dev, params);
  4186. if (res < 0) {
  4187. goto out_netdev;
  4188. }
  4189. res = register_netdevice(bond_dev);
  4190. if (res < 0) {
  4191. goto out_bond;
  4192. }
  4193. lockdep_set_class(&bond_dev->_xmit_lock, &bonding_netdev_xmit_lock_key);
  4194. if (newbond)
  4195. *newbond = bond_dev->priv;
  4196. netif_carrier_off(bond_dev);
  4197. up_write(&bonding_rwsem);
  4198. rtnl_unlock(); /* allows sysfs registration of net device */
  4199. res = bond_create_sysfs_entry(bond_dev->priv);
  4200. if (res < 0) {
  4201. rtnl_lock();
  4202. down_write(&bonding_rwsem);
  4203. goto out_bond;
  4204. }
  4205. return 0;
  4206. out_bond:
  4207. bond_deinit(bond_dev);
  4208. out_netdev:
  4209. free_netdev(bond_dev);
  4210. out_rtnl:
  4211. up_write(&bonding_rwsem);
  4212. rtnl_unlock();
  4213. return res;
  4214. }
  4215. static int __init bonding_init(void)
  4216. {
  4217. int i;
  4218. int res;
  4219. struct bonding *bond, *nxt;
  4220. printk(KERN_INFO "%s", version);
  4221. res = bond_check_params(&bonding_defaults);
  4222. if (res) {
  4223. goto out;
  4224. }
  4225. #ifdef CONFIG_PROC_FS
  4226. bond_create_proc_dir();
  4227. #endif
  4228. init_rwsem(&bonding_rwsem);
  4229. for (i = 0; i < max_bonds; i++) {
  4230. res = bond_create(NULL, &bonding_defaults, NULL);
  4231. if (res)
  4232. goto err;
  4233. }
  4234. res = bond_create_sysfs();
  4235. if (res)
  4236. goto err;
  4237. register_netdevice_notifier(&bond_netdev_notifier);
  4238. register_inetaddr_notifier(&bond_inetaddr_notifier);
  4239. goto out;
  4240. err:
  4241. list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list) {
  4242. bond_work_cancel_all(bond);
  4243. destroy_workqueue(bond->wq);
  4244. }
  4245. rtnl_lock();
  4246. bond_free_all();
  4247. bond_destroy_sysfs();
  4248. rtnl_unlock();
  4249. out:
  4250. return res;
  4251. }
  4252. static void __exit bonding_exit(void)
  4253. {
  4254. unregister_netdevice_notifier(&bond_netdev_notifier);
  4255. unregister_inetaddr_notifier(&bond_inetaddr_notifier);
  4256. rtnl_lock();
  4257. bond_free_all();
  4258. bond_destroy_sysfs();
  4259. rtnl_unlock();
  4260. }
  4261. module_init(bonding_init);
  4262. module_exit(bonding_exit);
  4263. MODULE_LICENSE("GPL");
  4264. MODULE_VERSION(DRV_VERSION);
  4265. MODULE_DESCRIPTION(DRV_DESCRIPTION ", v" DRV_VERSION);
  4266. MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others");
  4267. MODULE_SUPPORTED_DEVICE("most ethernet devices");
  4268. /*
  4269. * Local variables:
  4270. * c-indent-level: 8
  4271. * c-basic-offset: 8
  4272. * tab-width: 8
  4273. * End:
  4274. */