bond_main.c 126 KB

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