bond_main.c 121 KB

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