bond_main.c 121 KB

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