bond_main.c 121 KB

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