bond_main.c 130 KB

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