bond_main.c 128 KB

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