bond_main.c 125 KB

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