bond_main.c 131 KB

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