bond_main.c 133 KB

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