bond_main.c 125 KB

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