bond_main.c 126 KB

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