bond_main.c 131 KB

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