bond_main.c 129 KB

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