bond_main.c 126 KB

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