bond_main.c 134 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186
  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. static int (*ioctl)(struct net_device *, struct ifreq *, int);
  598. struct ifreq ifr;
  599. struct mii_ioctl_data *mii;
  600. if (bond->params.use_carrier)
  601. return netif_carrier_ok(slave_dev) ? BMSR_LSTATUS : 0;
  602. /* Try to get link status using Ethtool first. */
  603. if (slave_dev->ethtool_ops) {
  604. if (slave_dev->ethtool_ops->get_link) {
  605. u32 link;
  606. link = slave_dev->ethtool_ops->get_link(slave_dev);
  607. return link ? BMSR_LSTATUS : 0;
  608. }
  609. }
  610. /* Ethtool can't be used, fallback to MII ioctls. */
  611. ioctl = slave_ops->ndo_do_ioctl;
  612. if (ioctl) {
  613. /* TODO: set pointer to correct ioctl on a per team member */
  614. /* bases to make this more efficient. that is, once */
  615. /* we determine the correct ioctl, we will always */
  616. /* call it and not the others for that team */
  617. /* member. */
  618. /*
  619. * We cannot assume that SIOCGMIIPHY will also read a
  620. * register; not all network drivers (e.g., e100)
  621. * support that.
  622. */
  623. /* Yes, the mii is overlaid on the ifreq.ifr_ifru */
  624. strncpy(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
  625. mii = if_mii(&ifr);
  626. if (IOCTL(slave_dev, &ifr, SIOCGMIIPHY) == 0) {
  627. mii->reg_num = MII_BMSR;
  628. if (IOCTL(slave_dev, &ifr, SIOCGMIIREG) == 0)
  629. return mii->val_out & BMSR_LSTATUS;
  630. }
  631. }
  632. /*
  633. * If reporting, report that either there's no dev->do_ioctl,
  634. * or both SIOCGMIIREG and get_link failed (meaning that we
  635. * cannot report link status). If not reporting, pretend
  636. * we're ok.
  637. */
  638. return reporting ? -1 : BMSR_LSTATUS;
  639. }
  640. /*----------------------------- Multicast list ------------------------------*/
  641. /*
  642. * Returns 0 if dmi1 and dmi2 are the same, non-0 otherwise
  643. */
  644. static inline int bond_is_dmi_same(const struct dev_mc_list *dmi1,
  645. const struct dev_mc_list *dmi2)
  646. {
  647. return memcmp(dmi1->dmi_addr, dmi2->dmi_addr, dmi1->dmi_addrlen) == 0 &&
  648. dmi1->dmi_addrlen == dmi2->dmi_addrlen;
  649. }
  650. /*
  651. * returns dmi entry if found, NULL otherwise
  652. */
  653. static struct dev_mc_list *bond_mc_list_find_dmi(struct dev_mc_list *dmi,
  654. struct dev_mc_list *mc_list)
  655. {
  656. struct dev_mc_list *idmi;
  657. for (idmi = mc_list; idmi; idmi = idmi->next) {
  658. if (bond_is_dmi_same(dmi, idmi))
  659. return idmi;
  660. }
  661. return NULL;
  662. }
  663. /*
  664. * Push the promiscuity flag down to appropriate slaves
  665. */
  666. static int bond_set_promiscuity(struct bonding *bond, int inc)
  667. {
  668. int err = 0;
  669. if (USES_PRIMARY(bond->params.mode)) {
  670. /* write lock already acquired */
  671. if (bond->curr_active_slave) {
  672. err = dev_set_promiscuity(bond->curr_active_slave->dev,
  673. inc);
  674. }
  675. } else {
  676. struct slave *slave;
  677. int i;
  678. bond_for_each_slave(bond, slave, i) {
  679. err = dev_set_promiscuity(slave->dev, inc);
  680. if (err)
  681. return err;
  682. }
  683. }
  684. return err;
  685. }
  686. /*
  687. * Push the allmulti flag down to all slaves
  688. */
  689. static int bond_set_allmulti(struct bonding *bond, int inc)
  690. {
  691. int err = 0;
  692. if (USES_PRIMARY(bond->params.mode)) {
  693. /* write lock already acquired */
  694. if (bond->curr_active_slave) {
  695. err = dev_set_allmulti(bond->curr_active_slave->dev,
  696. inc);
  697. }
  698. } else {
  699. struct slave *slave;
  700. int i;
  701. bond_for_each_slave(bond, slave, i) {
  702. err = dev_set_allmulti(slave->dev, inc);
  703. if (err)
  704. return err;
  705. }
  706. }
  707. return err;
  708. }
  709. /*
  710. * Add a Multicast address to slaves
  711. * according to mode
  712. */
  713. static void bond_mc_add(struct bonding *bond, void *addr, int alen)
  714. {
  715. if (USES_PRIMARY(bond->params.mode)) {
  716. /* write lock already acquired */
  717. if (bond->curr_active_slave)
  718. dev_mc_add(bond->curr_active_slave->dev, addr, alen, 0);
  719. } else {
  720. struct slave *slave;
  721. int i;
  722. bond_for_each_slave(bond, slave, i)
  723. dev_mc_add(slave->dev, addr, alen, 0);
  724. }
  725. }
  726. /*
  727. * Remove a multicast address from slave
  728. * according to mode
  729. */
  730. static void bond_mc_delete(struct bonding *bond, void *addr, int alen)
  731. {
  732. if (USES_PRIMARY(bond->params.mode)) {
  733. /* write lock already acquired */
  734. if (bond->curr_active_slave)
  735. dev_mc_delete(bond->curr_active_slave->dev, addr,
  736. alen, 0);
  737. } else {
  738. struct slave *slave;
  739. int i;
  740. bond_for_each_slave(bond, slave, i) {
  741. dev_mc_delete(slave->dev, addr, alen, 0);
  742. }
  743. }
  744. }
  745. /*
  746. * Retrieve the list of registered multicast addresses for the bonding
  747. * device and retransmit an IGMP JOIN request to the current active
  748. * slave.
  749. */
  750. static void bond_resend_igmp_join_requests(struct bonding *bond)
  751. {
  752. struct in_device *in_dev;
  753. struct ip_mc_list *im;
  754. rcu_read_lock();
  755. in_dev = __in_dev_get_rcu(bond->dev);
  756. if (in_dev) {
  757. for (im = in_dev->mc_list; im; im = im->next)
  758. ip_mc_rejoin_group(im);
  759. }
  760. rcu_read_unlock();
  761. }
  762. /*
  763. * Totally destroys the mc_list in bond
  764. */
  765. static void bond_mc_list_destroy(struct bonding *bond)
  766. {
  767. struct dev_mc_list *dmi;
  768. dmi = bond->mc_list;
  769. while (dmi) {
  770. bond->mc_list = dmi->next;
  771. kfree(dmi);
  772. dmi = bond->mc_list;
  773. }
  774. bond->mc_list = NULL;
  775. }
  776. /*
  777. * Copy all the Multicast addresses from src to the bonding device dst
  778. */
  779. static int bond_mc_list_copy(struct dev_mc_list *mc_list, struct bonding *bond,
  780. gfp_t gfp_flag)
  781. {
  782. struct dev_mc_list *dmi, *new_dmi;
  783. for (dmi = mc_list; dmi; dmi = dmi->next) {
  784. new_dmi = kmalloc(sizeof(struct dev_mc_list), gfp_flag);
  785. if (!new_dmi) {
  786. /* FIXME: Potential memory leak !!! */
  787. return -ENOMEM;
  788. }
  789. new_dmi->next = bond->mc_list;
  790. bond->mc_list = new_dmi;
  791. new_dmi->dmi_addrlen = dmi->dmi_addrlen;
  792. memcpy(new_dmi->dmi_addr, dmi->dmi_addr, dmi->dmi_addrlen);
  793. new_dmi->dmi_users = dmi->dmi_users;
  794. new_dmi->dmi_gusers = dmi->dmi_gusers;
  795. }
  796. return 0;
  797. }
  798. /*
  799. * flush all members of flush->mc_list from device dev->mc_list
  800. */
  801. static void bond_mc_list_flush(struct net_device *bond_dev,
  802. struct net_device *slave_dev)
  803. {
  804. struct bonding *bond = netdev_priv(bond_dev);
  805. struct dev_mc_list *dmi;
  806. for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next)
  807. dev_mc_delete(slave_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
  808. if (bond->params.mode == BOND_MODE_8023AD) {
  809. /* del lacpdu mc addr from mc list */
  810. u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
  811. dev_mc_delete(slave_dev, lacpdu_multicast, ETH_ALEN, 0);
  812. }
  813. }
  814. /*--------------------------- Active slave change ---------------------------*/
  815. /*
  816. * Update the mc list and multicast-related flags for the new and
  817. * old active slaves (if any) according to the multicast mode, and
  818. * promiscuous flags unconditionally.
  819. */
  820. static void bond_mc_swap(struct bonding *bond, struct slave *new_active,
  821. struct slave *old_active)
  822. {
  823. struct dev_mc_list *dmi;
  824. if (!USES_PRIMARY(bond->params.mode))
  825. /* nothing to do - mc list is already up-to-date on
  826. * all slaves
  827. */
  828. return;
  829. if (old_active) {
  830. if (bond->dev->flags & IFF_PROMISC)
  831. dev_set_promiscuity(old_active->dev, -1);
  832. if (bond->dev->flags & IFF_ALLMULTI)
  833. dev_set_allmulti(old_active->dev, -1);
  834. for (dmi = bond->dev->mc_list; dmi; dmi = dmi->next)
  835. dev_mc_delete(old_active->dev, dmi->dmi_addr,
  836. dmi->dmi_addrlen, 0);
  837. }
  838. if (new_active) {
  839. /* FIXME: Signal errors upstream. */
  840. if (bond->dev->flags & IFF_PROMISC)
  841. dev_set_promiscuity(new_active->dev, 1);
  842. if (bond->dev->flags & IFF_ALLMULTI)
  843. dev_set_allmulti(new_active->dev, 1);
  844. for (dmi = bond->dev->mc_list; dmi; dmi = dmi->next)
  845. dev_mc_add(new_active->dev, dmi->dmi_addr,
  846. dmi->dmi_addrlen, 0);
  847. bond_resend_igmp_join_requests(bond);
  848. }
  849. }
  850. /*
  851. * bond_do_fail_over_mac
  852. *
  853. * Perform special MAC address swapping for fail_over_mac settings
  854. *
  855. * Called with RTNL, bond->lock for read, curr_slave_lock for write_bh.
  856. */
  857. static void bond_do_fail_over_mac(struct bonding *bond,
  858. struct slave *new_active,
  859. struct slave *old_active)
  860. __releases(&bond->curr_slave_lock)
  861. __releases(&bond->lock)
  862. __acquires(&bond->lock)
  863. __acquires(&bond->curr_slave_lock)
  864. {
  865. u8 tmp_mac[ETH_ALEN];
  866. struct sockaddr saddr;
  867. int rv;
  868. switch (bond->params.fail_over_mac) {
  869. case BOND_FOM_ACTIVE:
  870. if (new_active)
  871. memcpy(bond->dev->dev_addr, new_active->dev->dev_addr,
  872. new_active->dev->addr_len);
  873. break;
  874. case BOND_FOM_FOLLOW:
  875. /*
  876. * if new_active && old_active, swap them
  877. * if just old_active, do nothing (going to no active slave)
  878. * if just new_active, set new_active to bond's MAC
  879. */
  880. if (!new_active)
  881. return;
  882. write_unlock_bh(&bond->curr_slave_lock);
  883. read_unlock(&bond->lock);
  884. if (old_active) {
  885. memcpy(tmp_mac, new_active->dev->dev_addr, ETH_ALEN);
  886. memcpy(saddr.sa_data, old_active->dev->dev_addr,
  887. ETH_ALEN);
  888. saddr.sa_family = new_active->dev->type;
  889. } else {
  890. memcpy(saddr.sa_data, bond->dev->dev_addr, ETH_ALEN);
  891. saddr.sa_family = bond->dev->type;
  892. }
  893. rv = dev_set_mac_address(new_active->dev, &saddr);
  894. if (rv) {
  895. pr_err(DRV_NAME
  896. ": %s: Error %d setting MAC of slave %s\n",
  897. bond->dev->name, -rv, new_active->dev->name);
  898. goto out;
  899. }
  900. if (!old_active)
  901. goto out;
  902. memcpy(saddr.sa_data, tmp_mac, ETH_ALEN);
  903. saddr.sa_family = old_active->dev->type;
  904. rv = dev_set_mac_address(old_active->dev, &saddr);
  905. if (rv)
  906. pr_err(DRV_NAME
  907. ": %s: Error %d setting MAC of slave %s\n",
  908. bond->dev->name, -rv, new_active->dev->name);
  909. out:
  910. read_lock(&bond->lock);
  911. write_lock_bh(&bond->curr_slave_lock);
  912. break;
  913. default:
  914. pr_err(DRV_NAME
  915. ": %s: bond_do_fail_over_mac impossible: bad policy %d\n",
  916. bond->dev->name, bond->params.fail_over_mac);
  917. break;
  918. }
  919. }
  920. /**
  921. * find_best_interface - select the best available slave to be the active one
  922. * @bond: our bonding struct
  923. *
  924. * Warning: Caller must hold curr_slave_lock for writing.
  925. */
  926. static struct slave *bond_find_best_slave(struct bonding *bond)
  927. {
  928. struct slave *new_active, *old_active;
  929. struct slave *bestslave = NULL;
  930. int mintime = bond->params.updelay;
  931. int i;
  932. new_active = old_active = bond->curr_active_slave;
  933. if (!new_active) { /* there were no active slaves left */
  934. if (bond->slave_cnt > 0) /* found one slave */
  935. new_active = bond->first_slave;
  936. else
  937. return NULL; /* still no slave, return NULL */
  938. }
  939. /*
  940. * first try the primary link; if arping, a link must tx/rx
  941. * traffic before it can be considered the curr_active_slave.
  942. * also, we would skip slaves between the curr_active_slave
  943. * and primary_slave that may be up and able to arp
  944. */
  945. if ((bond->primary_slave) &&
  946. (!bond->params.arp_interval) &&
  947. (IS_UP(bond->primary_slave->dev))) {
  948. new_active = bond->primary_slave;
  949. }
  950. /* remember where to stop iterating over the slaves */
  951. old_active = new_active;
  952. bond_for_each_slave_from(bond, new_active, i, old_active) {
  953. if (IS_UP(new_active->dev)) {
  954. if (new_active->link == BOND_LINK_UP) {
  955. return new_active;
  956. } else if (new_active->link == BOND_LINK_BACK) {
  957. /* link up, but waiting for stabilization */
  958. if (new_active->delay < mintime) {
  959. mintime = new_active->delay;
  960. bestslave = new_active;
  961. }
  962. }
  963. }
  964. }
  965. return bestslave;
  966. }
  967. /**
  968. * change_active_interface - change the active slave into the specified one
  969. * @bond: our bonding struct
  970. * @new: the new slave to make the active one
  971. *
  972. * Set the new slave to the bond's settings and unset them on the old
  973. * curr_active_slave.
  974. * Setting include flags, mc-list, promiscuity, allmulti, etc.
  975. *
  976. * If @new's link state is %BOND_LINK_BACK we'll set it to %BOND_LINK_UP,
  977. * because it is apparently the best available slave we have, even though its
  978. * updelay hasn't timed out yet.
  979. *
  980. * If new_active is not NULL, caller must hold bond->lock for read and
  981. * curr_slave_lock for write_bh.
  982. */
  983. void bond_change_active_slave(struct bonding *bond, struct slave *new_active)
  984. {
  985. struct slave *old_active = bond->curr_active_slave;
  986. if (old_active == new_active)
  987. return;
  988. if (new_active) {
  989. new_active->jiffies = jiffies;
  990. if (new_active->link == BOND_LINK_BACK) {
  991. if (USES_PRIMARY(bond->params.mode)) {
  992. pr_info(DRV_NAME
  993. ": %s: making interface %s the new "
  994. "active one %d ms earlier.\n",
  995. bond->dev->name, new_active->dev->name,
  996. (bond->params.updelay - new_active->delay) * bond->params.miimon);
  997. }
  998. new_active->delay = 0;
  999. new_active->link = BOND_LINK_UP;
  1000. if (bond->params.mode == BOND_MODE_8023AD)
  1001. bond_3ad_handle_link_change(new_active, BOND_LINK_UP);
  1002. if (bond_is_lb(bond))
  1003. bond_alb_handle_link_change(bond, new_active, BOND_LINK_UP);
  1004. } else {
  1005. if (USES_PRIMARY(bond->params.mode)) {
  1006. pr_info(DRV_NAME
  1007. ": %s: making interface %s the new "
  1008. "active one.\n",
  1009. bond->dev->name, new_active->dev->name);
  1010. }
  1011. }
  1012. }
  1013. if (USES_PRIMARY(bond->params.mode))
  1014. bond_mc_swap(bond, new_active, old_active);
  1015. if (bond_is_lb(bond)) {
  1016. bond_alb_handle_active_change(bond, new_active);
  1017. if (old_active)
  1018. bond_set_slave_inactive_flags(old_active);
  1019. if (new_active)
  1020. bond_set_slave_active_flags(new_active);
  1021. } else {
  1022. bond->curr_active_slave = new_active;
  1023. }
  1024. if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
  1025. if (old_active)
  1026. bond_set_slave_inactive_flags(old_active);
  1027. if (new_active) {
  1028. bond_set_slave_active_flags(new_active);
  1029. if (bond->params.fail_over_mac)
  1030. bond_do_fail_over_mac(bond, new_active,
  1031. old_active);
  1032. bond->send_grat_arp = bond->params.num_grat_arp;
  1033. bond_send_gratuitous_arp(bond);
  1034. bond->send_unsol_na = bond->params.num_unsol_na;
  1035. bond_send_unsolicited_na(bond);
  1036. write_unlock_bh(&bond->curr_slave_lock);
  1037. read_unlock(&bond->lock);
  1038. netdev_bonding_change(bond->dev);
  1039. read_lock(&bond->lock);
  1040. write_lock_bh(&bond->curr_slave_lock);
  1041. }
  1042. }
  1043. }
  1044. /**
  1045. * bond_select_active_slave - select a new active slave, if needed
  1046. * @bond: our bonding struct
  1047. *
  1048. * This functions should be called when one of the following occurs:
  1049. * - The old curr_active_slave has been released or lost its link.
  1050. * - The primary_slave has got its link back.
  1051. * - A slave has got its link back and there's no old curr_active_slave.
  1052. *
  1053. * Caller must hold bond->lock for read and curr_slave_lock for write_bh.
  1054. */
  1055. void bond_select_active_slave(struct bonding *bond)
  1056. {
  1057. struct slave *best_slave;
  1058. int rv;
  1059. best_slave = bond_find_best_slave(bond);
  1060. if (best_slave != bond->curr_active_slave) {
  1061. bond_change_active_slave(bond, best_slave);
  1062. rv = bond_set_carrier(bond);
  1063. if (!rv)
  1064. return;
  1065. if (netif_carrier_ok(bond->dev)) {
  1066. pr_info(DRV_NAME
  1067. ": %s: first active interface up!\n",
  1068. bond->dev->name);
  1069. } else {
  1070. pr_info(DRV_NAME ": %s: "
  1071. "now running without any active interface !\n",
  1072. bond->dev->name);
  1073. }
  1074. }
  1075. }
  1076. /*--------------------------- slave list handling ---------------------------*/
  1077. /*
  1078. * This function attaches the slave to the end of list.
  1079. *
  1080. * bond->lock held for writing by caller.
  1081. */
  1082. static void bond_attach_slave(struct bonding *bond, struct slave *new_slave)
  1083. {
  1084. if (bond->first_slave == NULL) { /* attaching the first slave */
  1085. new_slave->next = new_slave;
  1086. new_slave->prev = new_slave;
  1087. bond->first_slave = new_slave;
  1088. } else {
  1089. new_slave->next = bond->first_slave;
  1090. new_slave->prev = bond->first_slave->prev;
  1091. new_slave->next->prev = new_slave;
  1092. new_slave->prev->next = new_slave;
  1093. }
  1094. bond->slave_cnt++;
  1095. }
  1096. /*
  1097. * This function detaches the slave from the list.
  1098. * WARNING: no check is made to verify if the slave effectively
  1099. * belongs to <bond>.
  1100. * Nothing is freed on return, structures are just unchained.
  1101. * If any slave pointer in bond was pointing to <slave>,
  1102. * it should be changed by the calling function.
  1103. *
  1104. * bond->lock held for writing by caller.
  1105. */
  1106. static void bond_detach_slave(struct bonding *bond, struct slave *slave)
  1107. {
  1108. if (slave->next)
  1109. slave->next->prev = slave->prev;
  1110. if (slave->prev)
  1111. slave->prev->next = slave->next;
  1112. if (bond->first_slave == slave) { /* slave is the first slave */
  1113. if (bond->slave_cnt > 1) { /* there are more slave */
  1114. bond->first_slave = slave->next;
  1115. } else {
  1116. bond->first_slave = NULL; /* slave was the last one */
  1117. }
  1118. }
  1119. slave->next = NULL;
  1120. slave->prev = NULL;
  1121. bond->slave_cnt--;
  1122. }
  1123. /*---------------------------------- IOCTL ----------------------------------*/
  1124. static int bond_sethwaddr(struct net_device *bond_dev,
  1125. struct net_device *slave_dev)
  1126. {
  1127. pr_debug("bond_dev=%p\n", bond_dev);
  1128. pr_debug("slave_dev=%p\n", slave_dev);
  1129. pr_debug("slave_dev->addr_len=%d\n", slave_dev->addr_len);
  1130. memcpy(bond_dev->dev_addr, slave_dev->dev_addr, slave_dev->addr_len);
  1131. return 0;
  1132. }
  1133. #define BOND_VLAN_FEATURES \
  1134. (NETIF_F_VLAN_CHALLENGED | NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_TX | \
  1135. NETIF_F_HW_VLAN_FILTER)
  1136. /*
  1137. * Compute the common dev->feature set available to all slaves. Some
  1138. * feature bits are managed elsewhere, so preserve those feature bits
  1139. * on the master device.
  1140. */
  1141. static int bond_compute_features(struct bonding *bond)
  1142. {
  1143. struct slave *slave;
  1144. struct net_device *bond_dev = bond->dev;
  1145. unsigned long features = bond_dev->features;
  1146. unsigned short max_hard_header_len = max((u16)ETH_HLEN,
  1147. bond_dev->hard_header_len);
  1148. int i;
  1149. features &= ~(NETIF_F_ALL_CSUM | BOND_VLAN_FEATURES);
  1150. features |= NETIF_F_GSO_MASK | NETIF_F_NO_CSUM;
  1151. if (!bond->first_slave)
  1152. goto done;
  1153. features &= ~NETIF_F_ONE_FOR_ALL;
  1154. bond_for_each_slave(bond, slave, i) {
  1155. features = netdev_increment_features(features,
  1156. slave->dev->features,
  1157. NETIF_F_ONE_FOR_ALL);
  1158. if (slave->dev->hard_header_len > max_hard_header_len)
  1159. max_hard_header_len = slave->dev->hard_header_len;
  1160. }
  1161. done:
  1162. features |= (bond_dev->features & BOND_VLAN_FEATURES);
  1163. bond_dev->features = netdev_fix_features(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. dev_close(bond_dev);
  1259. pr_debug("%s: change device type from %d to %d\n",
  1260. bond_dev->name, bond_dev->type, slave_dev->type);
  1261. if (slave_dev->type != ARPHRD_ETHER)
  1262. bond_setup_by_slave(bond_dev, slave_dev);
  1263. else
  1264. ether_setup(bond_dev);
  1265. dev_open(bond_dev);
  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. int mac_addr_differ;
  1543. /* slave is not a slave or master is not master of this slave */
  1544. if (!(slave_dev->flags & IFF_SLAVE) ||
  1545. (slave_dev->master != bond_dev)) {
  1546. pr_err(DRV_NAME
  1547. ": %s: Error: cannot release %s.\n",
  1548. bond_dev->name, slave_dev->name);
  1549. return -EINVAL;
  1550. }
  1551. write_lock_bh(&bond->lock);
  1552. slave = bond_get_slave_by_dev(bond, slave_dev);
  1553. if (!slave) {
  1554. /* not a slave of this bond */
  1555. pr_info(DRV_NAME
  1556. ": %s: %s not enslaved\n",
  1557. bond_dev->name, slave_dev->name);
  1558. write_unlock_bh(&bond->lock);
  1559. return -EINVAL;
  1560. }
  1561. if (!bond->params.fail_over_mac) {
  1562. mac_addr_differ = memcmp(bond_dev->dev_addr, slave->perm_hwaddr,
  1563. ETH_ALEN);
  1564. if (!mac_addr_differ && (bond->slave_cnt > 1))
  1565. pr_warning(DRV_NAME
  1566. ": %s: Warning: the permanent HWaddr of %s - "
  1567. "%pM - is still in use by %s. "
  1568. "Set the HWaddr of %s to a different address "
  1569. "to avoid conflicts.\n",
  1570. bond_dev->name, slave_dev->name,
  1571. slave->perm_hwaddr,
  1572. bond_dev->name, slave_dev->name);
  1573. }
  1574. /* Inform AD package of unbinding of slave. */
  1575. if (bond->params.mode == BOND_MODE_8023AD) {
  1576. /* must be called before the slave is
  1577. * detached from the list
  1578. */
  1579. bond_3ad_unbind_slave(slave);
  1580. }
  1581. pr_info(DRV_NAME
  1582. ": %s: releasing %s interface %s\n",
  1583. bond_dev->name,
  1584. (slave->state == BOND_STATE_ACTIVE)
  1585. ? "active" : "backup",
  1586. slave_dev->name);
  1587. oldcurrent = bond->curr_active_slave;
  1588. bond->current_arp_slave = NULL;
  1589. /* release the slave from its bond */
  1590. bond_detach_slave(bond, slave);
  1591. bond_compute_features(bond);
  1592. if (bond->primary_slave == slave)
  1593. bond->primary_slave = NULL;
  1594. if (oldcurrent == slave)
  1595. bond_change_active_slave(bond, NULL);
  1596. if (bond_is_lb(bond)) {
  1597. /* Must be called only after the slave has been
  1598. * detached from the list and the curr_active_slave
  1599. * has been cleared (if our_slave == old_current),
  1600. * but before a new active slave is selected.
  1601. */
  1602. write_unlock_bh(&bond->lock);
  1603. bond_alb_deinit_slave(bond, slave);
  1604. write_lock_bh(&bond->lock);
  1605. }
  1606. if (oldcurrent == slave) {
  1607. /*
  1608. * Note that we hold RTNL over this sequence, so there
  1609. * is no concern that another slave add/remove event
  1610. * will interfere.
  1611. */
  1612. write_unlock_bh(&bond->lock);
  1613. read_lock(&bond->lock);
  1614. write_lock_bh(&bond->curr_slave_lock);
  1615. bond_select_active_slave(bond);
  1616. write_unlock_bh(&bond->curr_slave_lock);
  1617. read_unlock(&bond->lock);
  1618. write_lock_bh(&bond->lock);
  1619. }
  1620. if (bond->slave_cnt == 0) {
  1621. bond_set_carrier(bond);
  1622. /* if the last slave was removed, zero the mac address
  1623. * of the master so it will be set by the application
  1624. * to the mac address of the first slave
  1625. */
  1626. memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
  1627. if (list_empty(&bond->vlan_list)) {
  1628. bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
  1629. } else {
  1630. pr_warning(DRV_NAME
  1631. ": %s: Warning: clearing HW address of %s while it "
  1632. "still has VLANs.\n",
  1633. bond_dev->name, bond_dev->name);
  1634. pr_warning(DRV_NAME
  1635. ": %s: When re-adding slaves, make sure the bond's "
  1636. "HW address matches its VLANs'.\n",
  1637. bond_dev->name);
  1638. }
  1639. } else if ((bond_dev->features & NETIF_F_VLAN_CHALLENGED) &&
  1640. !bond_has_challenged_slaves(bond)) {
  1641. pr_info(DRV_NAME
  1642. ": %s: last VLAN challenged slave %s "
  1643. "left bond %s. VLAN blocking is removed\n",
  1644. bond_dev->name, slave_dev->name, bond_dev->name);
  1645. bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
  1646. }
  1647. write_unlock_bh(&bond->lock);
  1648. /* must do this from outside any spinlocks */
  1649. bond_destroy_slave_symlinks(bond_dev, slave_dev);
  1650. bond_del_vlans_from_slave(bond, slave_dev);
  1651. /* If the mode USES_PRIMARY, then we should only remove its
  1652. * promisc and mc settings if it was the curr_active_slave, but that was
  1653. * already taken care of above when we detached the slave
  1654. */
  1655. if (!USES_PRIMARY(bond->params.mode)) {
  1656. /* unset promiscuity level from slave */
  1657. if (bond_dev->flags & IFF_PROMISC)
  1658. dev_set_promiscuity(slave_dev, -1);
  1659. /* unset allmulti level from slave */
  1660. if (bond_dev->flags & IFF_ALLMULTI)
  1661. dev_set_allmulti(slave_dev, -1);
  1662. /* flush master's mc_list from slave */
  1663. netif_addr_lock_bh(bond_dev);
  1664. bond_mc_list_flush(bond_dev, slave_dev);
  1665. netif_addr_unlock_bh(bond_dev);
  1666. }
  1667. netdev_set_master(slave_dev, NULL);
  1668. /* close slave before restoring its mac address */
  1669. dev_close(slave_dev);
  1670. if (bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
  1671. /* restore original ("permanent") mac address */
  1672. memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
  1673. addr.sa_family = slave_dev->type;
  1674. dev_set_mac_address(slave_dev, &addr);
  1675. }
  1676. slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
  1677. IFF_SLAVE_INACTIVE | IFF_BONDING |
  1678. IFF_SLAVE_NEEDARP);
  1679. kfree(slave);
  1680. return 0; /* deletion OK */
  1681. }
  1682. /*
  1683. * Destroy a bonding device.
  1684. * Must be under rtnl_lock when this function is called.
  1685. */
  1686. static void bond_uninit(struct net_device *bond_dev)
  1687. {
  1688. struct bonding *bond = netdev_priv(bond_dev);
  1689. bond_deinit(bond_dev);
  1690. bond_destroy_sysfs_entry(bond);
  1691. if (bond->wq)
  1692. destroy_workqueue(bond->wq);
  1693. netif_addr_lock_bh(bond_dev);
  1694. bond_mc_list_destroy(bond);
  1695. netif_addr_unlock_bh(bond_dev);
  1696. }
  1697. /*
  1698. * First release a slave and than destroy the bond if no more slaves are left.
  1699. * Must be under rtnl_lock when this function is called.
  1700. */
  1701. int bond_release_and_destroy(struct net_device *bond_dev,
  1702. struct net_device *slave_dev)
  1703. {
  1704. struct bonding *bond = netdev_priv(bond_dev);
  1705. int ret;
  1706. ret = bond_release(bond_dev, slave_dev);
  1707. if ((ret == 0) && (bond->slave_cnt == 0)) {
  1708. pr_info(DRV_NAME ": %s: destroying bond %s.\n",
  1709. bond_dev->name, bond_dev->name);
  1710. unregister_netdevice(bond_dev);
  1711. }
  1712. return ret;
  1713. }
  1714. /*
  1715. * This function releases all slaves.
  1716. */
  1717. static int bond_release_all(struct net_device *bond_dev)
  1718. {
  1719. struct bonding *bond = netdev_priv(bond_dev);
  1720. struct slave *slave;
  1721. struct net_device *slave_dev;
  1722. struct sockaddr addr;
  1723. write_lock_bh(&bond->lock);
  1724. netif_carrier_off(bond_dev);
  1725. if (bond->slave_cnt == 0)
  1726. goto out;
  1727. bond->current_arp_slave = NULL;
  1728. bond->primary_slave = NULL;
  1729. bond_change_active_slave(bond, NULL);
  1730. while ((slave = bond->first_slave) != NULL) {
  1731. /* Inform AD package of unbinding of slave
  1732. * before slave is detached from the list.
  1733. */
  1734. if (bond->params.mode == BOND_MODE_8023AD)
  1735. bond_3ad_unbind_slave(slave);
  1736. slave_dev = slave->dev;
  1737. bond_detach_slave(bond, slave);
  1738. /* now that the slave is detached, unlock and perform
  1739. * all the undo steps that should not be called from
  1740. * within a lock.
  1741. */
  1742. write_unlock_bh(&bond->lock);
  1743. if (bond_is_lb(bond)) {
  1744. /* must be called only after the slave
  1745. * has been detached from the list
  1746. */
  1747. bond_alb_deinit_slave(bond, slave);
  1748. }
  1749. bond_compute_features(bond);
  1750. bond_destroy_slave_symlinks(bond_dev, slave_dev);
  1751. bond_del_vlans_from_slave(bond, slave_dev);
  1752. /* If the mode USES_PRIMARY, then we should only remove its
  1753. * promisc and mc settings if it was the curr_active_slave, but that was
  1754. * already taken care of above when we detached the slave
  1755. */
  1756. if (!USES_PRIMARY(bond->params.mode)) {
  1757. /* unset promiscuity level from slave */
  1758. if (bond_dev->flags & IFF_PROMISC)
  1759. dev_set_promiscuity(slave_dev, -1);
  1760. /* unset allmulti level from slave */
  1761. if (bond_dev->flags & IFF_ALLMULTI)
  1762. dev_set_allmulti(slave_dev, -1);
  1763. /* flush master's mc_list from slave */
  1764. netif_addr_lock_bh(bond_dev);
  1765. bond_mc_list_flush(bond_dev, slave_dev);
  1766. netif_addr_unlock_bh(bond_dev);
  1767. }
  1768. netdev_set_master(slave_dev, NULL);
  1769. /* close slave before restoring its mac address */
  1770. dev_close(slave_dev);
  1771. if (!bond->params.fail_over_mac) {
  1772. /* restore original ("permanent") mac address*/
  1773. memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
  1774. addr.sa_family = slave_dev->type;
  1775. dev_set_mac_address(slave_dev, &addr);
  1776. }
  1777. slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
  1778. IFF_SLAVE_INACTIVE);
  1779. kfree(slave);
  1780. /* re-acquire the lock before getting the next slave */
  1781. write_lock_bh(&bond->lock);
  1782. }
  1783. /* zero the mac address of the master so it will be
  1784. * set by the application to the mac address of the
  1785. * first slave
  1786. */
  1787. memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
  1788. if (list_empty(&bond->vlan_list))
  1789. bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
  1790. else {
  1791. pr_warning(DRV_NAME
  1792. ": %s: Warning: clearing HW address of %s while it "
  1793. "still has VLANs.\n",
  1794. bond_dev->name, bond_dev->name);
  1795. pr_warning(DRV_NAME
  1796. ": %s: When re-adding slaves, make sure the bond's "
  1797. "HW address matches its VLANs'.\n",
  1798. bond_dev->name);
  1799. }
  1800. pr_info(DRV_NAME
  1801. ": %s: released all slaves\n",
  1802. bond_dev->name);
  1803. out:
  1804. write_unlock_bh(&bond->lock);
  1805. return 0;
  1806. }
  1807. /*
  1808. * This function changes the active slave to slave <slave_dev>.
  1809. * It returns -EINVAL in the following cases.
  1810. * - <slave_dev> is not found in the list.
  1811. * - There is not active slave now.
  1812. * - <slave_dev> is already active.
  1813. * - The link state of <slave_dev> is not BOND_LINK_UP.
  1814. * - <slave_dev> is not running.
  1815. * In these cases, this function does nothing.
  1816. * In the other cases, current_slave pointer is changed and 0 is returned.
  1817. */
  1818. static int bond_ioctl_change_active(struct net_device *bond_dev, struct net_device *slave_dev)
  1819. {
  1820. struct bonding *bond = netdev_priv(bond_dev);
  1821. struct slave *old_active = NULL;
  1822. struct slave *new_active = NULL;
  1823. int res = 0;
  1824. if (!USES_PRIMARY(bond->params.mode))
  1825. return -EINVAL;
  1826. /* Verify that master_dev is indeed the master of slave_dev */
  1827. if (!(slave_dev->flags & IFF_SLAVE) || (slave_dev->master != bond_dev))
  1828. return -EINVAL;
  1829. read_lock(&bond->lock);
  1830. read_lock(&bond->curr_slave_lock);
  1831. old_active = bond->curr_active_slave;
  1832. read_unlock(&bond->curr_slave_lock);
  1833. new_active = bond_get_slave_by_dev(bond, slave_dev);
  1834. /*
  1835. * Changing to the current active: do nothing; return success.
  1836. */
  1837. if (new_active && (new_active == old_active)) {
  1838. read_unlock(&bond->lock);
  1839. return 0;
  1840. }
  1841. if ((new_active) &&
  1842. (old_active) &&
  1843. (new_active->link == BOND_LINK_UP) &&
  1844. IS_UP(new_active->dev)) {
  1845. write_lock_bh(&bond->curr_slave_lock);
  1846. bond_change_active_slave(bond, new_active);
  1847. write_unlock_bh(&bond->curr_slave_lock);
  1848. } else
  1849. res = -EINVAL;
  1850. read_unlock(&bond->lock);
  1851. return res;
  1852. }
  1853. static int bond_info_query(struct net_device *bond_dev, struct ifbond *info)
  1854. {
  1855. struct bonding *bond = netdev_priv(bond_dev);
  1856. info->bond_mode = bond->params.mode;
  1857. info->miimon = bond->params.miimon;
  1858. read_lock(&bond->lock);
  1859. info->num_slaves = bond->slave_cnt;
  1860. read_unlock(&bond->lock);
  1861. return 0;
  1862. }
  1863. static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info)
  1864. {
  1865. struct bonding *bond = netdev_priv(bond_dev);
  1866. struct slave *slave;
  1867. int i, res = -ENODEV;
  1868. read_lock(&bond->lock);
  1869. bond_for_each_slave(bond, slave, i) {
  1870. if (i == (int)info->slave_id) {
  1871. res = 0;
  1872. strcpy(info->slave_name, slave->dev->name);
  1873. info->link = slave->link;
  1874. info->state = slave->state;
  1875. info->link_failure_count = slave->link_failure_count;
  1876. break;
  1877. }
  1878. }
  1879. read_unlock(&bond->lock);
  1880. return res;
  1881. }
  1882. /*-------------------------------- Monitoring -------------------------------*/
  1883. static int bond_miimon_inspect(struct bonding *bond)
  1884. {
  1885. struct slave *slave;
  1886. int i, link_state, commit = 0;
  1887. bool ignore_updelay;
  1888. ignore_updelay = !bond->curr_active_slave ? true : false;
  1889. bond_for_each_slave(bond, slave, i) {
  1890. slave->new_link = BOND_LINK_NOCHANGE;
  1891. link_state = bond_check_dev_link(bond, slave->dev, 0);
  1892. switch (slave->link) {
  1893. case BOND_LINK_UP:
  1894. if (link_state)
  1895. continue;
  1896. slave->link = BOND_LINK_FAIL;
  1897. slave->delay = bond->params.downdelay;
  1898. if (slave->delay) {
  1899. pr_info(DRV_NAME
  1900. ": %s: link status down for %s"
  1901. "interface %s, disabling it in %d ms.\n",
  1902. bond->dev->name,
  1903. (bond->params.mode ==
  1904. BOND_MODE_ACTIVEBACKUP) ?
  1905. ((slave->state == BOND_STATE_ACTIVE) ?
  1906. "active " : "backup ") : "",
  1907. slave->dev->name,
  1908. bond->params.downdelay * bond->params.miimon);
  1909. }
  1910. /*FALLTHRU*/
  1911. case BOND_LINK_FAIL:
  1912. if (link_state) {
  1913. /*
  1914. * recovered before downdelay expired
  1915. */
  1916. slave->link = BOND_LINK_UP;
  1917. slave->jiffies = jiffies;
  1918. pr_info(DRV_NAME
  1919. ": %s: link status up again after %d "
  1920. "ms for interface %s.\n",
  1921. bond->dev->name,
  1922. (bond->params.downdelay - slave->delay) *
  1923. bond->params.miimon,
  1924. slave->dev->name);
  1925. continue;
  1926. }
  1927. if (slave->delay <= 0) {
  1928. slave->new_link = BOND_LINK_DOWN;
  1929. commit++;
  1930. continue;
  1931. }
  1932. slave->delay--;
  1933. break;
  1934. case BOND_LINK_DOWN:
  1935. if (!link_state)
  1936. continue;
  1937. slave->link = BOND_LINK_BACK;
  1938. slave->delay = bond->params.updelay;
  1939. if (slave->delay) {
  1940. pr_info(DRV_NAME
  1941. ": %s: link status up for "
  1942. "interface %s, enabling it in %d ms.\n",
  1943. bond->dev->name, slave->dev->name,
  1944. ignore_updelay ? 0 :
  1945. bond->params.updelay *
  1946. bond->params.miimon);
  1947. }
  1948. /*FALLTHRU*/
  1949. case BOND_LINK_BACK:
  1950. if (!link_state) {
  1951. slave->link = BOND_LINK_DOWN;
  1952. pr_info(DRV_NAME
  1953. ": %s: link status down again after %d "
  1954. "ms for interface %s.\n",
  1955. bond->dev->name,
  1956. (bond->params.updelay - slave->delay) *
  1957. bond->params.miimon,
  1958. slave->dev->name);
  1959. continue;
  1960. }
  1961. if (ignore_updelay)
  1962. slave->delay = 0;
  1963. if (slave->delay <= 0) {
  1964. slave->new_link = BOND_LINK_UP;
  1965. commit++;
  1966. ignore_updelay = false;
  1967. continue;
  1968. }
  1969. slave->delay--;
  1970. break;
  1971. }
  1972. }
  1973. return commit;
  1974. }
  1975. static void bond_miimon_commit(struct bonding *bond)
  1976. {
  1977. struct slave *slave;
  1978. int i;
  1979. bond_for_each_slave(bond, slave, i) {
  1980. switch (slave->new_link) {
  1981. case BOND_LINK_NOCHANGE:
  1982. continue;
  1983. case BOND_LINK_UP:
  1984. slave->link = BOND_LINK_UP;
  1985. slave->jiffies = jiffies;
  1986. if (bond->params.mode == BOND_MODE_8023AD) {
  1987. /* prevent it from being the active one */
  1988. slave->state = BOND_STATE_BACKUP;
  1989. } else if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
  1990. /* make it immediately active */
  1991. slave->state = BOND_STATE_ACTIVE;
  1992. } else if (slave != bond->primary_slave) {
  1993. /* prevent it from being the active one */
  1994. slave->state = BOND_STATE_BACKUP;
  1995. }
  1996. pr_info(DRV_NAME
  1997. ": %s: link status definitely "
  1998. "up for interface %s.\n",
  1999. bond->dev->name, slave->dev->name);
  2000. /* notify ad that the link status has changed */
  2001. if (bond->params.mode == BOND_MODE_8023AD)
  2002. bond_3ad_handle_link_change(slave, BOND_LINK_UP);
  2003. if (bond_is_lb(bond))
  2004. bond_alb_handle_link_change(bond, slave,
  2005. BOND_LINK_UP);
  2006. if (!bond->curr_active_slave ||
  2007. (slave == bond->primary_slave))
  2008. goto do_failover;
  2009. continue;
  2010. case BOND_LINK_DOWN:
  2011. if (slave->link_failure_count < UINT_MAX)
  2012. slave->link_failure_count++;
  2013. slave->link = BOND_LINK_DOWN;
  2014. if (bond->params.mode == BOND_MODE_ACTIVEBACKUP ||
  2015. bond->params.mode == BOND_MODE_8023AD)
  2016. bond_set_slave_inactive_flags(slave);
  2017. pr_info(DRV_NAME
  2018. ": %s: link status definitely down for "
  2019. "interface %s, disabling it\n",
  2020. bond->dev->name, slave->dev->name);
  2021. if (bond->params.mode == BOND_MODE_8023AD)
  2022. bond_3ad_handle_link_change(slave,
  2023. BOND_LINK_DOWN);
  2024. if (bond_is_lb(bond))
  2025. bond_alb_handle_link_change(bond, slave,
  2026. BOND_LINK_DOWN);
  2027. if (slave == bond->curr_active_slave)
  2028. goto do_failover;
  2029. continue;
  2030. default:
  2031. pr_err(DRV_NAME
  2032. ": %s: invalid new link %d on slave %s\n",
  2033. bond->dev->name, slave->new_link,
  2034. slave->dev->name);
  2035. slave->new_link = BOND_LINK_NOCHANGE;
  2036. continue;
  2037. }
  2038. do_failover:
  2039. ASSERT_RTNL();
  2040. write_lock_bh(&bond->curr_slave_lock);
  2041. bond_select_active_slave(bond);
  2042. write_unlock_bh(&bond->curr_slave_lock);
  2043. }
  2044. bond_set_carrier(bond);
  2045. }
  2046. /*
  2047. * bond_mii_monitor
  2048. *
  2049. * Really a wrapper that splits the mii monitor into two phases: an
  2050. * inspection, then (if inspection indicates something needs to be done)
  2051. * an acquisition of appropriate locks followed by a commit phase to
  2052. * implement whatever link state changes are indicated.
  2053. */
  2054. void bond_mii_monitor(struct work_struct *work)
  2055. {
  2056. struct bonding *bond = container_of(work, struct bonding,
  2057. mii_work.work);
  2058. read_lock(&bond->lock);
  2059. if (bond->kill_timers)
  2060. goto out;
  2061. if (bond->slave_cnt == 0)
  2062. goto re_arm;
  2063. if (bond->send_grat_arp) {
  2064. read_lock(&bond->curr_slave_lock);
  2065. bond_send_gratuitous_arp(bond);
  2066. read_unlock(&bond->curr_slave_lock);
  2067. }
  2068. if (bond->send_unsol_na) {
  2069. read_lock(&bond->curr_slave_lock);
  2070. bond_send_unsolicited_na(bond);
  2071. read_unlock(&bond->curr_slave_lock);
  2072. }
  2073. if (bond_miimon_inspect(bond)) {
  2074. read_unlock(&bond->lock);
  2075. rtnl_lock();
  2076. read_lock(&bond->lock);
  2077. bond_miimon_commit(bond);
  2078. read_unlock(&bond->lock);
  2079. rtnl_unlock(); /* might sleep, hold no other locks */
  2080. read_lock(&bond->lock);
  2081. }
  2082. re_arm:
  2083. if (bond->params.miimon)
  2084. queue_delayed_work(bond->wq, &bond->mii_work,
  2085. msecs_to_jiffies(bond->params.miimon));
  2086. out:
  2087. read_unlock(&bond->lock);
  2088. }
  2089. static __be32 bond_glean_dev_ip(struct net_device *dev)
  2090. {
  2091. struct in_device *idev;
  2092. struct in_ifaddr *ifa;
  2093. __be32 addr = 0;
  2094. if (!dev)
  2095. return 0;
  2096. rcu_read_lock();
  2097. idev = __in_dev_get_rcu(dev);
  2098. if (!idev)
  2099. goto out;
  2100. ifa = idev->ifa_list;
  2101. if (!ifa)
  2102. goto out;
  2103. addr = ifa->ifa_local;
  2104. out:
  2105. rcu_read_unlock();
  2106. return addr;
  2107. }
  2108. static int bond_has_this_ip(struct bonding *bond, __be32 ip)
  2109. {
  2110. struct vlan_entry *vlan;
  2111. if (ip == bond->master_ip)
  2112. return 1;
  2113. list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
  2114. if (ip == vlan->vlan_ip)
  2115. return 1;
  2116. }
  2117. return 0;
  2118. }
  2119. /*
  2120. * We go to the (large) trouble of VLAN tagging ARP frames because
  2121. * switches in VLAN mode (especially if ports are configured as
  2122. * "native" to a VLAN) might not pass non-tagged frames.
  2123. */
  2124. static void bond_arp_send(struct net_device *slave_dev, int arp_op, __be32 dest_ip, __be32 src_ip, unsigned short vlan_id)
  2125. {
  2126. struct sk_buff *skb;
  2127. pr_debug("arp %d on slave %s: dst %x src %x vid %d\n", arp_op,
  2128. slave_dev->name, dest_ip, src_ip, vlan_id);
  2129. skb = arp_create(arp_op, ETH_P_ARP, dest_ip, slave_dev, src_ip,
  2130. NULL, slave_dev->dev_addr, NULL);
  2131. if (!skb) {
  2132. pr_err(DRV_NAME ": ARP packet allocation failed\n");
  2133. return;
  2134. }
  2135. if (vlan_id) {
  2136. skb = vlan_put_tag(skb, vlan_id);
  2137. if (!skb) {
  2138. pr_err(DRV_NAME ": failed to insert VLAN tag\n");
  2139. return;
  2140. }
  2141. }
  2142. arp_xmit(skb);
  2143. }
  2144. static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
  2145. {
  2146. int i, vlan_id, rv;
  2147. __be32 *targets = bond->params.arp_targets;
  2148. struct vlan_entry *vlan;
  2149. struct net_device *vlan_dev;
  2150. struct flowi fl;
  2151. struct rtable *rt;
  2152. for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
  2153. if (!targets[i])
  2154. break;
  2155. pr_debug("basa: target %x\n", targets[i]);
  2156. if (list_empty(&bond->vlan_list)) {
  2157. pr_debug("basa: empty vlan: arp_send\n");
  2158. bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
  2159. bond->master_ip, 0);
  2160. continue;
  2161. }
  2162. /*
  2163. * If VLANs are configured, we do a route lookup to
  2164. * determine which VLAN interface would be used, so we
  2165. * can tag the ARP with the proper VLAN tag.
  2166. */
  2167. memset(&fl, 0, sizeof(fl));
  2168. fl.fl4_dst = targets[i];
  2169. fl.fl4_tos = RTO_ONLINK;
  2170. rv = ip_route_output_key(&init_net, &rt, &fl);
  2171. if (rv) {
  2172. if (net_ratelimit()) {
  2173. pr_warning(DRV_NAME
  2174. ": %s: no route to arp_ip_target %pI4\n",
  2175. bond->dev->name, &fl.fl4_dst);
  2176. }
  2177. continue;
  2178. }
  2179. /*
  2180. * This target is not on a VLAN
  2181. */
  2182. if (rt->u.dst.dev == bond->dev) {
  2183. ip_rt_put(rt);
  2184. pr_debug("basa: rtdev == bond->dev: arp_send\n");
  2185. bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
  2186. bond->master_ip, 0);
  2187. continue;
  2188. }
  2189. vlan_id = 0;
  2190. list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
  2191. vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
  2192. if (vlan_dev == rt->u.dst.dev) {
  2193. vlan_id = vlan->vlan_id;
  2194. pr_debug("basa: vlan match on %s %d\n",
  2195. vlan_dev->name, vlan_id);
  2196. break;
  2197. }
  2198. }
  2199. if (vlan_id) {
  2200. ip_rt_put(rt);
  2201. bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
  2202. vlan->vlan_ip, vlan_id);
  2203. continue;
  2204. }
  2205. if (net_ratelimit()) {
  2206. pr_warning(DRV_NAME
  2207. ": %s: no path to arp_ip_target %pI4 via rt.dev %s\n",
  2208. bond->dev->name, &fl.fl4_dst,
  2209. rt->u.dst.dev ? rt->u.dst.dev->name : "NULL");
  2210. }
  2211. ip_rt_put(rt);
  2212. }
  2213. }
  2214. /*
  2215. * Kick out a gratuitous ARP for an IP on the bonding master plus one
  2216. * for each VLAN above us.
  2217. *
  2218. * Caller must hold curr_slave_lock for read or better
  2219. */
  2220. static void bond_send_gratuitous_arp(struct bonding *bond)
  2221. {
  2222. struct slave *slave = bond->curr_active_slave;
  2223. struct vlan_entry *vlan;
  2224. struct net_device *vlan_dev;
  2225. pr_debug("bond_send_grat_arp: bond %s slave %s\n", bond->dev->name,
  2226. slave ? slave->dev->name : "NULL");
  2227. if (!slave || !bond->send_grat_arp ||
  2228. test_bit(__LINK_STATE_LINKWATCH_PENDING, &slave->dev->state))
  2229. return;
  2230. bond->send_grat_arp--;
  2231. if (bond->master_ip) {
  2232. bond_arp_send(slave->dev, ARPOP_REPLY, bond->master_ip,
  2233. bond->master_ip, 0);
  2234. }
  2235. list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
  2236. vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
  2237. if (vlan->vlan_ip) {
  2238. bond_arp_send(slave->dev, ARPOP_REPLY, vlan->vlan_ip,
  2239. vlan->vlan_ip, vlan->vlan_id);
  2240. }
  2241. }
  2242. }
  2243. static void bond_validate_arp(struct bonding *bond, struct slave *slave, __be32 sip, __be32 tip)
  2244. {
  2245. int i;
  2246. __be32 *targets = bond->params.arp_targets;
  2247. for (i = 0; (i < BOND_MAX_ARP_TARGETS) && targets[i]; i++) {
  2248. pr_debug("bva: sip %pI4 tip %pI4 t[%d] %pI4 bhti(tip) %d\n",
  2249. &sip, &tip, i, &targets[i], bond_has_this_ip(bond, tip));
  2250. if (sip == targets[i]) {
  2251. if (bond_has_this_ip(bond, tip))
  2252. slave->last_arp_rx = jiffies;
  2253. return;
  2254. }
  2255. }
  2256. }
  2257. static int bond_arp_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
  2258. {
  2259. struct arphdr *arp;
  2260. struct slave *slave;
  2261. struct bonding *bond;
  2262. unsigned char *arp_ptr;
  2263. __be32 sip, tip;
  2264. if (dev_net(dev) != &init_net)
  2265. goto out;
  2266. if (!(dev->priv_flags & IFF_BONDING) || !(dev->flags & IFF_MASTER))
  2267. goto out;
  2268. bond = netdev_priv(dev);
  2269. read_lock(&bond->lock);
  2270. pr_debug("bond_arp_rcv: bond %s skb->dev %s orig_dev %s\n",
  2271. bond->dev->name, skb->dev ? skb->dev->name : "NULL",
  2272. orig_dev ? orig_dev->name : "NULL");
  2273. slave = bond_get_slave_by_dev(bond, orig_dev);
  2274. if (!slave || !slave_do_arp_validate(bond, slave))
  2275. goto out_unlock;
  2276. if (!pskb_may_pull(skb, arp_hdr_len(dev)))
  2277. goto out_unlock;
  2278. arp = arp_hdr(skb);
  2279. if (arp->ar_hln != dev->addr_len ||
  2280. skb->pkt_type == PACKET_OTHERHOST ||
  2281. skb->pkt_type == PACKET_LOOPBACK ||
  2282. arp->ar_hrd != htons(ARPHRD_ETHER) ||
  2283. arp->ar_pro != htons(ETH_P_IP) ||
  2284. arp->ar_pln != 4)
  2285. goto out_unlock;
  2286. arp_ptr = (unsigned char *)(arp + 1);
  2287. arp_ptr += dev->addr_len;
  2288. memcpy(&sip, arp_ptr, 4);
  2289. arp_ptr += 4 + dev->addr_len;
  2290. memcpy(&tip, arp_ptr, 4);
  2291. pr_debug("bond_arp_rcv: %s %s/%d av %d sv %d sip %pI4 tip %pI4\n",
  2292. bond->dev->name, slave->dev->name, slave->state,
  2293. bond->params.arp_validate, slave_do_arp_validate(bond, slave),
  2294. &sip, &tip);
  2295. /*
  2296. * Backup slaves won't see the ARP reply, but do come through
  2297. * here for each ARP probe (so we swap the sip/tip to validate
  2298. * the probe). In a "redundant switch, common router" type of
  2299. * configuration, the ARP probe will (hopefully) travel from
  2300. * the active, through one switch, the router, then the other
  2301. * switch before reaching the backup.
  2302. */
  2303. if (slave->state == BOND_STATE_ACTIVE)
  2304. bond_validate_arp(bond, slave, sip, tip);
  2305. else
  2306. bond_validate_arp(bond, slave, tip, sip);
  2307. out_unlock:
  2308. read_unlock(&bond->lock);
  2309. out:
  2310. dev_kfree_skb(skb);
  2311. return NET_RX_SUCCESS;
  2312. }
  2313. /*
  2314. * this function is called regularly to monitor each slave's link
  2315. * ensuring that traffic is being sent and received when arp monitoring
  2316. * is used in load-balancing mode. if the adapter has been dormant, then an
  2317. * arp is transmitted to generate traffic. see activebackup_arp_monitor for
  2318. * arp monitoring in active backup mode.
  2319. */
  2320. void bond_loadbalance_arp_mon(struct work_struct *work)
  2321. {
  2322. struct bonding *bond = container_of(work, struct bonding,
  2323. arp_work.work);
  2324. struct slave *slave, *oldcurrent;
  2325. int do_failover = 0;
  2326. int delta_in_ticks;
  2327. int i;
  2328. read_lock(&bond->lock);
  2329. delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
  2330. if (bond->kill_timers)
  2331. goto out;
  2332. if (bond->slave_cnt == 0)
  2333. goto re_arm;
  2334. read_lock(&bond->curr_slave_lock);
  2335. oldcurrent = bond->curr_active_slave;
  2336. read_unlock(&bond->curr_slave_lock);
  2337. /* see if any of the previous devices are up now (i.e. they have
  2338. * xmt and rcv traffic). the curr_active_slave does not come into
  2339. * the picture unless it is null. also, slave->jiffies is not needed
  2340. * here because we send an arp on each slave and give a slave as
  2341. * long as it needs to get the tx/rx within the delta.
  2342. * TODO: what about up/down delay in arp mode? it wasn't here before
  2343. * so it can wait
  2344. */
  2345. bond_for_each_slave(bond, slave, i) {
  2346. if (slave->link != BOND_LINK_UP) {
  2347. if (time_before_eq(jiffies, dev_trans_start(slave->dev) + delta_in_ticks) &&
  2348. time_before_eq(jiffies, slave->dev->last_rx + delta_in_ticks)) {
  2349. slave->link = BOND_LINK_UP;
  2350. slave->state = BOND_STATE_ACTIVE;
  2351. /* primary_slave has no meaning in round-robin
  2352. * mode. the window of a slave being up and
  2353. * curr_active_slave being null after enslaving
  2354. * is closed.
  2355. */
  2356. if (!oldcurrent) {
  2357. pr_info(DRV_NAME
  2358. ": %s: link status definitely "
  2359. "up for interface %s, ",
  2360. bond->dev->name,
  2361. slave->dev->name);
  2362. do_failover = 1;
  2363. } else {
  2364. pr_info(DRV_NAME
  2365. ": %s: interface %s is now up\n",
  2366. bond->dev->name,
  2367. slave->dev->name);
  2368. }
  2369. }
  2370. } else {
  2371. /* slave->link == BOND_LINK_UP */
  2372. /* not all switches will respond to an arp request
  2373. * when the source ip is 0, so don't take the link down
  2374. * if we don't know our ip yet
  2375. */
  2376. if (time_after_eq(jiffies, dev_trans_start(slave->dev) + 2*delta_in_ticks) ||
  2377. (time_after_eq(jiffies, slave->dev->last_rx + 2*delta_in_ticks))) {
  2378. slave->link = BOND_LINK_DOWN;
  2379. slave->state = BOND_STATE_BACKUP;
  2380. if (slave->link_failure_count < UINT_MAX)
  2381. slave->link_failure_count++;
  2382. pr_info(DRV_NAME
  2383. ": %s: interface %s is now down.\n",
  2384. bond->dev->name,
  2385. slave->dev->name);
  2386. if (slave == oldcurrent)
  2387. do_failover = 1;
  2388. }
  2389. }
  2390. /* note: if switch is in round-robin mode, all links
  2391. * must tx arp to ensure all links rx an arp - otherwise
  2392. * links may oscillate or not come up at all; if switch is
  2393. * in something like xor mode, there is nothing we can
  2394. * do - all replies will be rx'ed on same link causing slaves
  2395. * to be unstable during low/no traffic periods
  2396. */
  2397. if (IS_UP(slave->dev))
  2398. bond_arp_send_all(bond, slave);
  2399. }
  2400. if (do_failover) {
  2401. write_lock_bh(&bond->curr_slave_lock);
  2402. bond_select_active_slave(bond);
  2403. write_unlock_bh(&bond->curr_slave_lock);
  2404. }
  2405. re_arm:
  2406. if (bond->params.arp_interval)
  2407. queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
  2408. out:
  2409. read_unlock(&bond->lock);
  2410. }
  2411. /*
  2412. * Called to inspect slaves for active-backup mode ARP monitor link state
  2413. * changes. Sets new_link in slaves to specify what action should take
  2414. * place for the slave. Returns 0 if no changes are found, >0 if changes
  2415. * to link states must be committed.
  2416. *
  2417. * Called with bond->lock held for read.
  2418. */
  2419. static int bond_ab_arp_inspect(struct bonding *bond, int delta_in_ticks)
  2420. {
  2421. struct slave *slave;
  2422. int i, commit = 0;
  2423. bond_for_each_slave(bond, slave, i) {
  2424. slave->new_link = BOND_LINK_NOCHANGE;
  2425. if (slave->link != BOND_LINK_UP) {
  2426. if (time_before_eq(jiffies, slave_last_rx(bond, slave) +
  2427. delta_in_ticks)) {
  2428. slave->new_link = BOND_LINK_UP;
  2429. commit++;
  2430. }
  2431. continue;
  2432. }
  2433. /*
  2434. * Give slaves 2*delta after being enslaved or made
  2435. * active. This avoids bouncing, as the last receive
  2436. * times need a full ARP monitor cycle to be updated.
  2437. */
  2438. if (!time_after_eq(jiffies, slave->jiffies +
  2439. 2 * delta_in_ticks))
  2440. continue;
  2441. /*
  2442. * Backup slave is down if:
  2443. * - No current_arp_slave AND
  2444. * - more than 3*delta since last receive AND
  2445. * - the bond has an IP address
  2446. *
  2447. * Note: a non-null current_arp_slave indicates
  2448. * the curr_active_slave went down and we are
  2449. * searching for a new one; under this condition
  2450. * we only take the curr_active_slave down - this
  2451. * gives each slave a chance to tx/rx traffic
  2452. * before being taken out
  2453. */
  2454. if (slave->state == BOND_STATE_BACKUP &&
  2455. !bond->current_arp_slave &&
  2456. time_after(jiffies, slave_last_rx(bond, slave) +
  2457. 3 * delta_in_ticks)) {
  2458. slave->new_link = BOND_LINK_DOWN;
  2459. commit++;
  2460. }
  2461. /*
  2462. * Active slave is down if:
  2463. * - more than 2*delta since transmitting OR
  2464. * - (more than 2*delta since receive AND
  2465. * the bond has an IP address)
  2466. */
  2467. if ((slave->state == BOND_STATE_ACTIVE) &&
  2468. (time_after_eq(jiffies, dev_trans_start(slave->dev) +
  2469. 2 * delta_in_ticks) ||
  2470. (time_after_eq(jiffies, slave_last_rx(bond, slave)
  2471. + 2 * delta_in_ticks)))) {
  2472. slave->new_link = BOND_LINK_DOWN;
  2473. commit++;
  2474. }
  2475. }
  2476. read_lock(&bond->curr_slave_lock);
  2477. /*
  2478. * Trigger a commit if the primary option setting has changed.
  2479. */
  2480. if (bond->primary_slave &&
  2481. (bond->primary_slave != bond->curr_active_slave) &&
  2482. (bond->primary_slave->link == BOND_LINK_UP))
  2483. commit++;
  2484. read_unlock(&bond->curr_slave_lock);
  2485. return commit;
  2486. }
  2487. /*
  2488. * Called to commit link state changes noted by inspection step of
  2489. * active-backup mode ARP monitor.
  2490. *
  2491. * Called with RTNL and bond->lock for read.
  2492. */
  2493. static void bond_ab_arp_commit(struct bonding *bond, int delta_in_ticks)
  2494. {
  2495. struct slave *slave;
  2496. int i;
  2497. bond_for_each_slave(bond, slave, i) {
  2498. switch (slave->new_link) {
  2499. case BOND_LINK_NOCHANGE:
  2500. continue;
  2501. case BOND_LINK_UP:
  2502. write_lock_bh(&bond->curr_slave_lock);
  2503. if (!bond->curr_active_slave &&
  2504. time_before_eq(jiffies, dev_trans_start(slave->dev) +
  2505. delta_in_ticks)) {
  2506. slave->link = BOND_LINK_UP;
  2507. bond_change_active_slave(bond, slave);
  2508. bond->current_arp_slave = NULL;
  2509. pr_info(DRV_NAME
  2510. ": %s: %s is up and now the "
  2511. "active interface\n",
  2512. bond->dev->name, slave->dev->name);
  2513. } else if (bond->curr_active_slave != slave) {
  2514. /* this slave has just come up but we
  2515. * already have a current slave; this can
  2516. * also happen if bond_enslave adds a new
  2517. * slave that is up while we are searching
  2518. * for a new slave
  2519. */
  2520. slave->link = BOND_LINK_UP;
  2521. bond_set_slave_inactive_flags(slave);
  2522. bond->current_arp_slave = NULL;
  2523. pr_info(DRV_NAME
  2524. ": %s: backup interface %s is now up\n",
  2525. bond->dev->name, slave->dev->name);
  2526. }
  2527. write_unlock_bh(&bond->curr_slave_lock);
  2528. break;
  2529. case BOND_LINK_DOWN:
  2530. if (slave->link_failure_count < UINT_MAX)
  2531. slave->link_failure_count++;
  2532. slave->link = BOND_LINK_DOWN;
  2533. if (slave == bond->curr_active_slave) {
  2534. pr_info(DRV_NAME
  2535. ": %s: link status down for active "
  2536. "interface %s, disabling it\n",
  2537. bond->dev->name, slave->dev->name);
  2538. bond_set_slave_inactive_flags(slave);
  2539. write_lock_bh(&bond->curr_slave_lock);
  2540. bond_select_active_slave(bond);
  2541. if (bond->curr_active_slave)
  2542. bond->curr_active_slave->jiffies =
  2543. jiffies;
  2544. write_unlock_bh(&bond->curr_slave_lock);
  2545. bond->current_arp_slave = NULL;
  2546. } else if (slave->state == BOND_STATE_BACKUP) {
  2547. pr_info(DRV_NAME
  2548. ": %s: backup interface %s is now down\n",
  2549. bond->dev->name, slave->dev->name);
  2550. bond_set_slave_inactive_flags(slave);
  2551. }
  2552. break;
  2553. default:
  2554. pr_err(DRV_NAME
  2555. ": %s: impossible: new_link %d on slave %s\n",
  2556. bond->dev->name, slave->new_link,
  2557. slave->dev->name);
  2558. }
  2559. }
  2560. /*
  2561. * No race with changes to primary via sysfs, as we hold rtnl.
  2562. */
  2563. if (bond->primary_slave &&
  2564. (bond->primary_slave != bond->curr_active_slave) &&
  2565. (bond->primary_slave->link == BOND_LINK_UP)) {
  2566. write_lock_bh(&bond->curr_slave_lock);
  2567. bond_change_active_slave(bond, bond->primary_slave);
  2568. write_unlock_bh(&bond->curr_slave_lock);
  2569. }
  2570. bond_set_carrier(bond);
  2571. }
  2572. /*
  2573. * Send ARP probes for active-backup mode ARP monitor.
  2574. *
  2575. * Called with bond->lock held for read.
  2576. */
  2577. static void bond_ab_arp_probe(struct bonding *bond)
  2578. {
  2579. struct slave *slave;
  2580. int i;
  2581. read_lock(&bond->curr_slave_lock);
  2582. if (bond->current_arp_slave && bond->curr_active_slave)
  2583. pr_info(DRV_NAME "PROBE: c_arp %s && cas %s BAD\n",
  2584. bond->current_arp_slave->dev->name,
  2585. bond->curr_active_slave->dev->name);
  2586. if (bond->curr_active_slave) {
  2587. bond_arp_send_all(bond, bond->curr_active_slave);
  2588. read_unlock(&bond->curr_slave_lock);
  2589. return;
  2590. }
  2591. read_unlock(&bond->curr_slave_lock);
  2592. /* if we don't have a curr_active_slave, search for the next available
  2593. * backup slave from the current_arp_slave and make it the candidate
  2594. * for becoming the curr_active_slave
  2595. */
  2596. if (!bond->current_arp_slave) {
  2597. bond->current_arp_slave = bond->first_slave;
  2598. if (!bond->current_arp_slave)
  2599. return;
  2600. }
  2601. bond_set_slave_inactive_flags(bond->current_arp_slave);
  2602. /* search for next candidate */
  2603. bond_for_each_slave_from(bond, slave, i, bond->current_arp_slave->next) {
  2604. if (IS_UP(slave->dev)) {
  2605. slave->link = BOND_LINK_BACK;
  2606. bond_set_slave_active_flags(slave);
  2607. bond_arp_send_all(bond, slave);
  2608. slave->jiffies = jiffies;
  2609. bond->current_arp_slave = slave;
  2610. break;
  2611. }
  2612. /* if the link state is up at this point, we
  2613. * mark it down - this can happen if we have
  2614. * simultaneous link failures and
  2615. * reselect_active_interface doesn't make this
  2616. * one the current slave so it is still marked
  2617. * up when it is actually down
  2618. */
  2619. if (slave->link == BOND_LINK_UP) {
  2620. slave->link = BOND_LINK_DOWN;
  2621. if (slave->link_failure_count < UINT_MAX)
  2622. slave->link_failure_count++;
  2623. bond_set_slave_inactive_flags(slave);
  2624. pr_info(DRV_NAME
  2625. ": %s: backup interface %s is now down.\n",
  2626. bond->dev->name, slave->dev->name);
  2627. }
  2628. }
  2629. }
  2630. void bond_activebackup_arp_mon(struct work_struct *work)
  2631. {
  2632. struct bonding *bond = container_of(work, struct bonding,
  2633. arp_work.work);
  2634. int delta_in_ticks;
  2635. read_lock(&bond->lock);
  2636. if (bond->kill_timers)
  2637. goto out;
  2638. delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
  2639. if (bond->slave_cnt == 0)
  2640. goto re_arm;
  2641. if (bond->send_grat_arp) {
  2642. read_lock(&bond->curr_slave_lock);
  2643. bond_send_gratuitous_arp(bond);
  2644. read_unlock(&bond->curr_slave_lock);
  2645. }
  2646. if (bond->send_unsol_na) {
  2647. read_lock(&bond->curr_slave_lock);
  2648. bond_send_unsolicited_na(bond);
  2649. read_unlock(&bond->curr_slave_lock);
  2650. }
  2651. if (bond_ab_arp_inspect(bond, delta_in_ticks)) {
  2652. read_unlock(&bond->lock);
  2653. rtnl_lock();
  2654. read_lock(&bond->lock);
  2655. bond_ab_arp_commit(bond, delta_in_ticks);
  2656. read_unlock(&bond->lock);
  2657. rtnl_unlock();
  2658. read_lock(&bond->lock);
  2659. }
  2660. bond_ab_arp_probe(bond);
  2661. re_arm:
  2662. if (bond->params.arp_interval)
  2663. queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
  2664. out:
  2665. read_unlock(&bond->lock);
  2666. }
  2667. /*------------------------------ proc/seq_file-------------------------------*/
  2668. #ifdef CONFIG_PROC_FS
  2669. static void *bond_info_seq_start(struct seq_file *seq, loff_t *pos)
  2670. __acquires(&dev_base_lock)
  2671. __acquires(&bond->lock)
  2672. {
  2673. struct bonding *bond = seq->private;
  2674. loff_t off = 0;
  2675. struct slave *slave;
  2676. int i;
  2677. /* make sure the bond won't be taken away */
  2678. read_lock(&dev_base_lock);
  2679. read_lock(&bond->lock);
  2680. if (*pos == 0)
  2681. return SEQ_START_TOKEN;
  2682. bond_for_each_slave(bond, slave, i) {
  2683. if (++off == *pos)
  2684. return slave;
  2685. }
  2686. return NULL;
  2687. }
  2688. static void *bond_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2689. {
  2690. struct bonding *bond = seq->private;
  2691. struct slave *slave = v;
  2692. ++*pos;
  2693. if (v == SEQ_START_TOKEN)
  2694. return bond->first_slave;
  2695. slave = slave->next;
  2696. return (slave == bond->first_slave) ? NULL : slave;
  2697. }
  2698. static void bond_info_seq_stop(struct seq_file *seq, void *v)
  2699. __releases(&bond->lock)
  2700. __releases(&dev_base_lock)
  2701. {
  2702. struct bonding *bond = seq->private;
  2703. read_unlock(&bond->lock);
  2704. read_unlock(&dev_base_lock);
  2705. }
  2706. static void bond_info_show_master(struct seq_file *seq)
  2707. {
  2708. struct bonding *bond = seq->private;
  2709. struct slave *curr;
  2710. int i;
  2711. read_lock(&bond->curr_slave_lock);
  2712. curr = bond->curr_active_slave;
  2713. read_unlock(&bond->curr_slave_lock);
  2714. seq_printf(seq, "Bonding Mode: %s",
  2715. bond_mode_name(bond->params.mode));
  2716. if (bond->params.mode == BOND_MODE_ACTIVEBACKUP &&
  2717. bond->params.fail_over_mac)
  2718. seq_printf(seq, " (fail_over_mac %s)",
  2719. fail_over_mac_tbl[bond->params.fail_over_mac].modename);
  2720. seq_printf(seq, "\n");
  2721. if (bond->params.mode == BOND_MODE_XOR ||
  2722. bond->params.mode == BOND_MODE_8023AD) {
  2723. seq_printf(seq, "Transmit Hash Policy: %s (%d)\n",
  2724. xmit_hashtype_tbl[bond->params.xmit_policy].modename,
  2725. bond->params.xmit_policy);
  2726. }
  2727. if (USES_PRIMARY(bond->params.mode)) {
  2728. seq_printf(seq, "Primary Slave: %s\n",
  2729. (bond->primary_slave) ?
  2730. bond->primary_slave->dev->name : "None");
  2731. seq_printf(seq, "Currently Active Slave: %s\n",
  2732. (curr) ? curr->dev->name : "None");
  2733. }
  2734. seq_printf(seq, "MII Status: %s\n", netif_carrier_ok(bond->dev) ?
  2735. "up" : "down");
  2736. seq_printf(seq, "MII Polling Interval (ms): %d\n", bond->params.miimon);
  2737. seq_printf(seq, "Up Delay (ms): %d\n",
  2738. bond->params.updelay * bond->params.miimon);
  2739. seq_printf(seq, "Down Delay (ms): %d\n",
  2740. bond->params.downdelay * bond->params.miimon);
  2741. /* ARP information */
  2742. if (bond->params.arp_interval > 0) {
  2743. int printed = 0;
  2744. seq_printf(seq, "ARP Polling Interval (ms): %d\n",
  2745. bond->params.arp_interval);
  2746. seq_printf(seq, "ARP IP target/s (n.n.n.n form):");
  2747. for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
  2748. if (!bond->params.arp_targets[i])
  2749. break;
  2750. if (printed)
  2751. seq_printf(seq, ",");
  2752. seq_printf(seq, " %pI4", &bond->params.arp_targets[i]);
  2753. printed = 1;
  2754. }
  2755. seq_printf(seq, "\n");
  2756. }
  2757. if (bond->params.mode == BOND_MODE_8023AD) {
  2758. struct ad_info ad_info;
  2759. seq_puts(seq, "\n802.3ad info\n");
  2760. seq_printf(seq, "LACP rate: %s\n",
  2761. (bond->params.lacp_fast) ? "fast" : "slow");
  2762. seq_printf(seq, "Aggregator selection policy (ad_select): %s\n",
  2763. ad_select_tbl[bond->params.ad_select].modename);
  2764. if (bond_3ad_get_active_agg_info(bond, &ad_info)) {
  2765. seq_printf(seq, "bond %s has no active aggregator\n",
  2766. bond->dev->name);
  2767. } else {
  2768. seq_printf(seq, "Active Aggregator Info:\n");
  2769. seq_printf(seq, "\tAggregator ID: %d\n",
  2770. ad_info.aggregator_id);
  2771. seq_printf(seq, "\tNumber of ports: %d\n",
  2772. ad_info.ports);
  2773. seq_printf(seq, "\tActor Key: %d\n",
  2774. ad_info.actor_key);
  2775. seq_printf(seq, "\tPartner Key: %d\n",
  2776. ad_info.partner_key);
  2777. seq_printf(seq, "\tPartner Mac Address: %pM\n",
  2778. ad_info.partner_system);
  2779. }
  2780. }
  2781. }
  2782. static void bond_info_show_slave(struct seq_file *seq,
  2783. const struct slave *slave)
  2784. {
  2785. struct bonding *bond = seq->private;
  2786. seq_printf(seq, "\nSlave Interface: %s\n", slave->dev->name);
  2787. seq_printf(seq, "MII Status: %s\n",
  2788. (slave->link == BOND_LINK_UP) ? "up" : "down");
  2789. seq_printf(seq, "Link Failure Count: %u\n",
  2790. slave->link_failure_count);
  2791. seq_printf(seq, "Permanent HW addr: %pM\n", slave->perm_hwaddr);
  2792. if (bond->params.mode == BOND_MODE_8023AD) {
  2793. const struct aggregator *agg
  2794. = SLAVE_AD_INFO(slave).port.aggregator;
  2795. if (agg)
  2796. seq_printf(seq, "Aggregator ID: %d\n",
  2797. agg->aggregator_identifier);
  2798. else
  2799. seq_puts(seq, "Aggregator ID: N/A\n");
  2800. }
  2801. }
  2802. static int bond_info_seq_show(struct seq_file *seq, void *v)
  2803. {
  2804. if (v == SEQ_START_TOKEN) {
  2805. seq_printf(seq, "%s\n", version);
  2806. bond_info_show_master(seq);
  2807. } else
  2808. bond_info_show_slave(seq, v);
  2809. return 0;
  2810. }
  2811. static const struct seq_operations bond_info_seq_ops = {
  2812. .start = bond_info_seq_start,
  2813. .next = bond_info_seq_next,
  2814. .stop = bond_info_seq_stop,
  2815. .show = bond_info_seq_show,
  2816. };
  2817. static int bond_info_open(struct inode *inode, struct file *file)
  2818. {
  2819. struct seq_file *seq;
  2820. struct proc_dir_entry *proc;
  2821. int res;
  2822. res = seq_open(file, &bond_info_seq_ops);
  2823. if (!res) {
  2824. /* recover the pointer buried in proc_dir_entry data */
  2825. seq = file->private_data;
  2826. proc = PDE(inode);
  2827. seq->private = proc->data;
  2828. }
  2829. return res;
  2830. }
  2831. static const struct file_operations bond_info_fops = {
  2832. .owner = THIS_MODULE,
  2833. .open = bond_info_open,
  2834. .read = seq_read,
  2835. .llseek = seq_lseek,
  2836. .release = seq_release,
  2837. };
  2838. static int bond_create_proc_entry(struct bonding *bond)
  2839. {
  2840. struct net_device *bond_dev = bond->dev;
  2841. if (bond_proc_dir) {
  2842. bond->proc_entry = proc_create_data(bond_dev->name,
  2843. S_IRUGO, bond_proc_dir,
  2844. &bond_info_fops, bond);
  2845. if (bond->proc_entry == NULL)
  2846. pr_warning(DRV_NAME
  2847. ": Warning: Cannot create /proc/net/%s/%s\n",
  2848. DRV_NAME, bond_dev->name);
  2849. else
  2850. memcpy(bond->proc_file_name, bond_dev->name, IFNAMSIZ);
  2851. }
  2852. return 0;
  2853. }
  2854. static void bond_remove_proc_entry(struct bonding *bond)
  2855. {
  2856. if (bond_proc_dir && bond->proc_entry) {
  2857. remove_proc_entry(bond->proc_file_name, bond_proc_dir);
  2858. memset(bond->proc_file_name, 0, IFNAMSIZ);
  2859. bond->proc_entry = NULL;
  2860. }
  2861. }
  2862. /* Create the bonding directory under /proc/net, if doesn't exist yet.
  2863. * Caller must hold rtnl_lock.
  2864. */
  2865. static void bond_create_proc_dir(void)
  2866. {
  2867. if (!bond_proc_dir) {
  2868. bond_proc_dir = proc_mkdir(DRV_NAME, init_net.proc_net);
  2869. if (!bond_proc_dir)
  2870. pr_warning(DRV_NAME
  2871. ": Warning: cannot create /proc/net/%s\n",
  2872. DRV_NAME);
  2873. }
  2874. }
  2875. /* Destroy the bonding directory under /proc/net, if empty.
  2876. * Caller must hold rtnl_lock.
  2877. */
  2878. static void bond_destroy_proc_dir(void)
  2879. {
  2880. if (bond_proc_dir) {
  2881. remove_proc_entry(DRV_NAME, init_net.proc_net);
  2882. bond_proc_dir = NULL;
  2883. }
  2884. }
  2885. #else /* !CONFIG_PROC_FS */
  2886. static int bond_create_proc_entry(struct bonding *bond)
  2887. {
  2888. }
  2889. static void bond_remove_proc_entry(struct bonding *bond)
  2890. {
  2891. }
  2892. static void bond_create_proc_dir(void)
  2893. {
  2894. }
  2895. static void bond_destroy_proc_dir(void)
  2896. {
  2897. }
  2898. #endif /* CONFIG_PROC_FS */
  2899. /*-------------------------- netdev event handling --------------------------*/
  2900. /*
  2901. * Change device name
  2902. */
  2903. static int bond_event_changename(struct bonding *bond)
  2904. {
  2905. bond_remove_proc_entry(bond);
  2906. bond_create_proc_entry(bond);
  2907. bond_destroy_sysfs_entry(bond);
  2908. bond_create_sysfs_entry(bond);
  2909. return NOTIFY_DONE;
  2910. }
  2911. static int bond_master_netdev_event(unsigned long event,
  2912. struct net_device *bond_dev)
  2913. {
  2914. struct bonding *event_bond = netdev_priv(bond_dev);
  2915. switch (event) {
  2916. case NETDEV_CHANGENAME:
  2917. return bond_event_changename(event_bond);
  2918. case NETDEV_UNREGISTER:
  2919. bond_release_all(event_bond->dev);
  2920. break;
  2921. default:
  2922. break;
  2923. }
  2924. return NOTIFY_DONE;
  2925. }
  2926. static int bond_slave_netdev_event(unsigned long event,
  2927. struct net_device *slave_dev)
  2928. {
  2929. struct net_device *bond_dev = slave_dev->master;
  2930. struct bonding *bond = netdev_priv(bond_dev);
  2931. switch (event) {
  2932. case NETDEV_UNREGISTER:
  2933. if (bond_dev) {
  2934. if (bond->setup_by_slave)
  2935. bond_release_and_destroy(bond_dev, slave_dev);
  2936. else
  2937. bond_release(bond_dev, slave_dev);
  2938. }
  2939. break;
  2940. case NETDEV_CHANGE:
  2941. if (bond->params.mode == BOND_MODE_8023AD || bond_is_lb(bond)) {
  2942. struct slave *slave;
  2943. slave = bond_get_slave_by_dev(bond, slave_dev);
  2944. if (slave) {
  2945. u16 old_speed = slave->speed;
  2946. u16 old_duplex = slave->duplex;
  2947. bond_update_speed_duplex(slave);
  2948. if (bond_is_lb(bond))
  2949. break;
  2950. if (old_speed != slave->speed)
  2951. bond_3ad_adapter_speed_changed(slave);
  2952. if (old_duplex != slave->duplex)
  2953. bond_3ad_adapter_duplex_changed(slave);
  2954. }
  2955. }
  2956. break;
  2957. case NETDEV_DOWN:
  2958. /*
  2959. * ... Or is it this?
  2960. */
  2961. break;
  2962. case NETDEV_CHANGEMTU:
  2963. /*
  2964. * TODO: Should slaves be allowed to
  2965. * independently alter their MTU? For
  2966. * an active-backup bond, slaves need
  2967. * not be the same type of device, so
  2968. * MTUs may vary. For other modes,
  2969. * slaves arguably should have the
  2970. * same MTUs. To do this, we'd need to
  2971. * take over the slave's change_mtu
  2972. * function for the duration of their
  2973. * servitude.
  2974. */
  2975. break;
  2976. case NETDEV_CHANGENAME:
  2977. /*
  2978. * TODO: handle changing the primary's name
  2979. */
  2980. break;
  2981. case NETDEV_FEAT_CHANGE:
  2982. bond_compute_features(bond);
  2983. break;
  2984. default:
  2985. break;
  2986. }
  2987. return NOTIFY_DONE;
  2988. }
  2989. /*
  2990. * bond_netdev_event: handle netdev notifier chain events.
  2991. *
  2992. * This function receives events for the netdev chain. The caller (an
  2993. * ioctl handler calling blocking_notifier_call_chain) holds the necessary
  2994. * locks for us to safely manipulate the slave devices (RTNL lock,
  2995. * dev_probe_lock).
  2996. */
  2997. static int bond_netdev_event(struct notifier_block *this,
  2998. unsigned long event, void *ptr)
  2999. {
  3000. struct net_device *event_dev = (struct net_device *)ptr;
  3001. if (dev_net(event_dev) != &init_net)
  3002. return NOTIFY_DONE;
  3003. pr_debug("event_dev: %s, event: %lx\n",
  3004. (event_dev ? event_dev->name : "None"),
  3005. event);
  3006. if (!(event_dev->priv_flags & IFF_BONDING))
  3007. return NOTIFY_DONE;
  3008. if (event_dev->flags & IFF_MASTER) {
  3009. pr_debug("IFF_MASTER\n");
  3010. return bond_master_netdev_event(event, event_dev);
  3011. }
  3012. if (event_dev->flags & IFF_SLAVE) {
  3013. pr_debug("IFF_SLAVE\n");
  3014. return bond_slave_netdev_event(event, event_dev);
  3015. }
  3016. return NOTIFY_DONE;
  3017. }
  3018. /*
  3019. * bond_inetaddr_event: handle inetaddr notifier chain events.
  3020. *
  3021. * We keep track of device IPs primarily to use as source addresses in
  3022. * ARP monitor probes (rather than spewing out broadcasts all the time).
  3023. *
  3024. * We track one IP for the main device (if it has one), plus one per VLAN.
  3025. */
  3026. static int bond_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
  3027. {
  3028. struct in_ifaddr *ifa = ptr;
  3029. struct net_device *vlan_dev, *event_dev = ifa->ifa_dev->dev;
  3030. struct bonding *bond;
  3031. struct vlan_entry *vlan;
  3032. if (dev_net(ifa->ifa_dev->dev) != &init_net)
  3033. return NOTIFY_DONE;
  3034. list_for_each_entry(bond, &bond_dev_list, bond_list) {
  3035. if (bond->dev == event_dev) {
  3036. switch (event) {
  3037. case NETDEV_UP:
  3038. bond->master_ip = ifa->ifa_local;
  3039. return NOTIFY_OK;
  3040. case NETDEV_DOWN:
  3041. bond->master_ip = bond_glean_dev_ip(bond->dev);
  3042. return NOTIFY_OK;
  3043. default:
  3044. return NOTIFY_DONE;
  3045. }
  3046. }
  3047. list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
  3048. vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
  3049. if (vlan_dev == event_dev) {
  3050. switch (event) {
  3051. case NETDEV_UP:
  3052. vlan->vlan_ip = ifa->ifa_local;
  3053. return NOTIFY_OK;
  3054. case NETDEV_DOWN:
  3055. vlan->vlan_ip =
  3056. bond_glean_dev_ip(vlan_dev);
  3057. return NOTIFY_OK;
  3058. default:
  3059. return NOTIFY_DONE;
  3060. }
  3061. }
  3062. }
  3063. }
  3064. return NOTIFY_DONE;
  3065. }
  3066. static struct notifier_block bond_netdev_notifier = {
  3067. .notifier_call = bond_netdev_event,
  3068. };
  3069. static struct notifier_block bond_inetaddr_notifier = {
  3070. .notifier_call = bond_inetaddr_event,
  3071. };
  3072. /*-------------------------- Packet type handling ---------------------------*/
  3073. /* register to receive lacpdus on a bond */
  3074. static void bond_register_lacpdu(struct bonding *bond)
  3075. {
  3076. struct packet_type *pk_type = &(BOND_AD_INFO(bond).ad_pkt_type);
  3077. /* initialize packet type */
  3078. pk_type->type = PKT_TYPE_LACPDU;
  3079. pk_type->dev = bond->dev;
  3080. pk_type->func = bond_3ad_lacpdu_recv;
  3081. dev_add_pack(pk_type);
  3082. }
  3083. /* unregister to receive lacpdus on a bond */
  3084. static void bond_unregister_lacpdu(struct bonding *bond)
  3085. {
  3086. dev_remove_pack(&(BOND_AD_INFO(bond).ad_pkt_type));
  3087. }
  3088. void bond_register_arp(struct bonding *bond)
  3089. {
  3090. struct packet_type *pt = &bond->arp_mon_pt;
  3091. if (pt->type)
  3092. return;
  3093. pt->type = htons(ETH_P_ARP);
  3094. pt->dev = bond->dev;
  3095. pt->func = bond_arp_rcv;
  3096. dev_add_pack(pt);
  3097. }
  3098. void bond_unregister_arp(struct bonding *bond)
  3099. {
  3100. struct packet_type *pt = &bond->arp_mon_pt;
  3101. dev_remove_pack(pt);
  3102. pt->type = 0;
  3103. }
  3104. /*---------------------------- Hashing Policies -----------------------------*/
  3105. /*
  3106. * Hash for the output device based upon layer 2 and layer 3 data. If
  3107. * the packet is not IP mimic bond_xmit_hash_policy_l2()
  3108. */
  3109. static int bond_xmit_hash_policy_l23(struct sk_buff *skb,
  3110. struct net_device *bond_dev, int count)
  3111. {
  3112. struct ethhdr *data = (struct ethhdr *)skb->data;
  3113. struct iphdr *iph = ip_hdr(skb);
  3114. if (skb->protocol == htons(ETH_P_IP)) {
  3115. return ((ntohl(iph->saddr ^ iph->daddr) & 0xffff) ^
  3116. (data->h_dest[5] ^ bond_dev->dev_addr[5])) % count;
  3117. }
  3118. return (data->h_dest[5] ^ bond_dev->dev_addr[5]) % count;
  3119. }
  3120. /*
  3121. * Hash for the output device based upon layer 3 and layer 4 data. If
  3122. * the packet is a frag or not TCP or UDP, just use layer 3 data. If it is
  3123. * altogether not IP, mimic bond_xmit_hash_policy_l2()
  3124. */
  3125. static int bond_xmit_hash_policy_l34(struct sk_buff *skb,
  3126. struct net_device *bond_dev, int count)
  3127. {
  3128. struct ethhdr *data = (struct ethhdr *)skb->data;
  3129. struct iphdr *iph = ip_hdr(skb);
  3130. __be16 *layer4hdr = (__be16 *)((u32 *)iph + iph->ihl);
  3131. int layer4_xor = 0;
  3132. if (skb->protocol == htons(ETH_P_IP)) {
  3133. if (!(iph->frag_off & htons(IP_MF|IP_OFFSET)) &&
  3134. (iph->protocol == IPPROTO_TCP ||
  3135. iph->protocol == IPPROTO_UDP)) {
  3136. layer4_xor = ntohs((*layer4hdr ^ *(layer4hdr + 1)));
  3137. }
  3138. return (layer4_xor ^
  3139. ((ntohl(iph->saddr ^ iph->daddr)) & 0xffff)) % count;
  3140. }
  3141. return (data->h_dest[5] ^ bond_dev->dev_addr[5]) % count;
  3142. }
  3143. /*
  3144. * Hash for the output device based upon layer 2 data
  3145. */
  3146. static int bond_xmit_hash_policy_l2(struct sk_buff *skb,
  3147. struct net_device *bond_dev, int count)
  3148. {
  3149. struct ethhdr *data = (struct ethhdr *)skb->data;
  3150. return (data->h_dest[5] ^ bond_dev->dev_addr[5]) % count;
  3151. }
  3152. /*-------------------------- Device entry points ----------------------------*/
  3153. static int bond_open(struct net_device *bond_dev)
  3154. {
  3155. struct bonding *bond = netdev_priv(bond_dev);
  3156. bond->kill_timers = 0;
  3157. if (bond_is_lb(bond)) {
  3158. /* bond_alb_initialize must be called before the timer
  3159. * is started.
  3160. */
  3161. if (bond_alb_initialize(bond, (bond->params.mode == BOND_MODE_ALB))) {
  3162. /* something went wrong - fail the open operation */
  3163. return -1;
  3164. }
  3165. INIT_DELAYED_WORK(&bond->alb_work, bond_alb_monitor);
  3166. queue_delayed_work(bond->wq, &bond->alb_work, 0);
  3167. }
  3168. if (bond->params.miimon) { /* link check interval, in milliseconds. */
  3169. INIT_DELAYED_WORK(&bond->mii_work, bond_mii_monitor);
  3170. queue_delayed_work(bond->wq, &bond->mii_work, 0);
  3171. }
  3172. if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
  3173. if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
  3174. INIT_DELAYED_WORK(&bond->arp_work,
  3175. bond_activebackup_arp_mon);
  3176. else
  3177. INIT_DELAYED_WORK(&bond->arp_work,
  3178. bond_loadbalance_arp_mon);
  3179. queue_delayed_work(bond->wq, &bond->arp_work, 0);
  3180. if (bond->params.arp_validate)
  3181. bond_register_arp(bond);
  3182. }
  3183. if (bond->params.mode == BOND_MODE_8023AD) {
  3184. INIT_DELAYED_WORK(&bond->ad_work, bond_3ad_state_machine_handler);
  3185. queue_delayed_work(bond->wq, &bond->ad_work, 0);
  3186. /* register to receive LACPDUs */
  3187. bond_register_lacpdu(bond);
  3188. bond_3ad_initiate_agg_selection(bond, 1);
  3189. }
  3190. return 0;
  3191. }
  3192. static int bond_close(struct net_device *bond_dev)
  3193. {
  3194. struct bonding *bond = netdev_priv(bond_dev);
  3195. if (bond->params.mode == BOND_MODE_8023AD) {
  3196. /* Unregister the receive of LACPDUs */
  3197. bond_unregister_lacpdu(bond);
  3198. }
  3199. if (bond->params.arp_validate)
  3200. bond_unregister_arp(bond);
  3201. write_lock_bh(&bond->lock);
  3202. bond->send_grat_arp = 0;
  3203. bond->send_unsol_na = 0;
  3204. /* signal timers not to re-arm */
  3205. bond->kill_timers = 1;
  3206. write_unlock_bh(&bond->lock);
  3207. if (bond->params.miimon) { /* link check interval, in milliseconds. */
  3208. cancel_delayed_work(&bond->mii_work);
  3209. }
  3210. if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
  3211. cancel_delayed_work(&bond->arp_work);
  3212. }
  3213. switch (bond->params.mode) {
  3214. case BOND_MODE_8023AD:
  3215. cancel_delayed_work(&bond->ad_work);
  3216. break;
  3217. case BOND_MODE_TLB:
  3218. case BOND_MODE_ALB:
  3219. cancel_delayed_work(&bond->alb_work);
  3220. break;
  3221. default:
  3222. break;
  3223. }
  3224. if (bond_is_lb(bond)) {
  3225. /* Must be called only after all
  3226. * slaves have been released
  3227. */
  3228. bond_alb_deinitialize(bond);
  3229. }
  3230. return 0;
  3231. }
  3232. static struct net_device_stats *bond_get_stats(struct net_device *bond_dev)
  3233. {
  3234. struct bonding *bond = netdev_priv(bond_dev);
  3235. struct net_device_stats *stats = &bond->stats;
  3236. struct net_device_stats local_stats;
  3237. struct slave *slave;
  3238. int i;
  3239. memset(&local_stats, 0, sizeof(struct net_device_stats));
  3240. read_lock_bh(&bond->lock);
  3241. bond_for_each_slave(bond, slave, i) {
  3242. const struct net_device_stats *sstats = dev_get_stats(slave->dev);
  3243. local_stats.rx_packets += sstats->rx_packets;
  3244. local_stats.rx_bytes += sstats->rx_bytes;
  3245. local_stats.rx_errors += sstats->rx_errors;
  3246. local_stats.rx_dropped += sstats->rx_dropped;
  3247. local_stats.tx_packets += sstats->tx_packets;
  3248. local_stats.tx_bytes += sstats->tx_bytes;
  3249. local_stats.tx_errors += sstats->tx_errors;
  3250. local_stats.tx_dropped += sstats->tx_dropped;
  3251. local_stats.multicast += sstats->multicast;
  3252. local_stats.collisions += sstats->collisions;
  3253. local_stats.rx_length_errors += sstats->rx_length_errors;
  3254. local_stats.rx_over_errors += sstats->rx_over_errors;
  3255. local_stats.rx_crc_errors += sstats->rx_crc_errors;
  3256. local_stats.rx_frame_errors += sstats->rx_frame_errors;
  3257. local_stats.rx_fifo_errors += sstats->rx_fifo_errors;
  3258. local_stats.rx_missed_errors += sstats->rx_missed_errors;
  3259. local_stats.tx_aborted_errors += sstats->tx_aborted_errors;
  3260. local_stats.tx_carrier_errors += sstats->tx_carrier_errors;
  3261. local_stats.tx_fifo_errors += sstats->tx_fifo_errors;
  3262. local_stats.tx_heartbeat_errors += sstats->tx_heartbeat_errors;
  3263. local_stats.tx_window_errors += sstats->tx_window_errors;
  3264. }
  3265. memcpy(stats, &local_stats, sizeof(struct net_device_stats));
  3266. read_unlock_bh(&bond->lock);
  3267. return stats;
  3268. }
  3269. static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
  3270. {
  3271. struct net_device *slave_dev = NULL;
  3272. struct ifbond k_binfo;
  3273. struct ifbond __user *u_binfo = NULL;
  3274. struct ifslave k_sinfo;
  3275. struct ifslave __user *u_sinfo = NULL;
  3276. struct mii_ioctl_data *mii = NULL;
  3277. int res = 0;
  3278. pr_debug("bond_ioctl: master=%s, cmd=%d\n",
  3279. bond_dev->name, cmd);
  3280. switch (cmd) {
  3281. case SIOCGMIIPHY:
  3282. mii = if_mii(ifr);
  3283. if (!mii)
  3284. return -EINVAL;
  3285. mii->phy_id = 0;
  3286. /* Fall Through */
  3287. case SIOCGMIIREG:
  3288. /*
  3289. * We do this again just in case we were called by SIOCGMIIREG
  3290. * instead of SIOCGMIIPHY.
  3291. */
  3292. mii = if_mii(ifr);
  3293. if (!mii)
  3294. return -EINVAL;
  3295. if (mii->reg_num == 1) {
  3296. struct bonding *bond = netdev_priv(bond_dev);
  3297. mii->val_out = 0;
  3298. read_lock(&bond->lock);
  3299. read_lock(&bond->curr_slave_lock);
  3300. if (netif_carrier_ok(bond->dev))
  3301. mii->val_out = BMSR_LSTATUS;
  3302. read_unlock(&bond->curr_slave_lock);
  3303. read_unlock(&bond->lock);
  3304. }
  3305. return 0;
  3306. case BOND_INFO_QUERY_OLD:
  3307. case SIOCBONDINFOQUERY:
  3308. u_binfo = (struct ifbond __user *)ifr->ifr_data;
  3309. if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond)))
  3310. return -EFAULT;
  3311. res = bond_info_query(bond_dev, &k_binfo);
  3312. if (res == 0 &&
  3313. copy_to_user(u_binfo, &k_binfo, sizeof(ifbond)))
  3314. return -EFAULT;
  3315. return res;
  3316. case BOND_SLAVE_INFO_QUERY_OLD:
  3317. case SIOCBONDSLAVEINFOQUERY:
  3318. u_sinfo = (struct ifslave __user *)ifr->ifr_data;
  3319. if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave)))
  3320. return -EFAULT;
  3321. res = bond_slave_info_query(bond_dev, &k_sinfo);
  3322. if (res == 0 &&
  3323. copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave)))
  3324. return -EFAULT;
  3325. return res;
  3326. default:
  3327. /* Go on */
  3328. break;
  3329. }
  3330. if (!capable(CAP_NET_ADMIN))
  3331. return -EPERM;
  3332. slave_dev = dev_get_by_name(&init_net, ifr->ifr_slave);
  3333. pr_debug("slave_dev=%p: \n", slave_dev);
  3334. if (!slave_dev)
  3335. res = -ENODEV;
  3336. else {
  3337. pr_debug("slave_dev->name=%s: \n", slave_dev->name);
  3338. switch (cmd) {
  3339. case BOND_ENSLAVE_OLD:
  3340. case SIOCBONDENSLAVE:
  3341. res = bond_enslave(bond_dev, slave_dev);
  3342. break;
  3343. case BOND_RELEASE_OLD:
  3344. case SIOCBONDRELEASE:
  3345. res = bond_release(bond_dev, slave_dev);
  3346. break;
  3347. case BOND_SETHWADDR_OLD:
  3348. case SIOCBONDSETHWADDR:
  3349. res = bond_sethwaddr(bond_dev, slave_dev);
  3350. break;
  3351. case BOND_CHANGE_ACTIVE_OLD:
  3352. case SIOCBONDCHANGEACTIVE:
  3353. res = bond_ioctl_change_active(bond_dev, slave_dev);
  3354. break;
  3355. default:
  3356. res = -EOPNOTSUPP;
  3357. }
  3358. dev_put(slave_dev);
  3359. }
  3360. return res;
  3361. }
  3362. static void bond_set_multicast_list(struct net_device *bond_dev)
  3363. {
  3364. struct bonding *bond = netdev_priv(bond_dev);
  3365. struct dev_mc_list *dmi;
  3366. /*
  3367. * Do promisc before checking multicast_mode
  3368. */
  3369. if ((bond_dev->flags & IFF_PROMISC) && !(bond->flags & IFF_PROMISC))
  3370. /*
  3371. * FIXME: Need to handle the error when one of the multi-slaves
  3372. * encounters error.
  3373. */
  3374. bond_set_promiscuity(bond, 1);
  3375. if (!(bond_dev->flags & IFF_PROMISC) && (bond->flags & IFF_PROMISC))
  3376. bond_set_promiscuity(bond, -1);
  3377. /* set allmulti flag to slaves */
  3378. if ((bond_dev->flags & IFF_ALLMULTI) && !(bond->flags & IFF_ALLMULTI))
  3379. /*
  3380. * FIXME: Need to handle the error when one of the multi-slaves
  3381. * encounters error.
  3382. */
  3383. bond_set_allmulti(bond, 1);
  3384. if (!(bond_dev->flags & IFF_ALLMULTI) && (bond->flags & IFF_ALLMULTI))
  3385. bond_set_allmulti(bond, -1);
  3386. read_lock(&bond->lock);
  3387. bond->flags = bond_dev->flags;
  3388. /* looking for addresses to add to slaves' mc list */
  3389. for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
  3390. if (!bond_mc_list_find_dmi(dmi, bond->mc_list))
  3391. bond_mc_add(bond, dmi->dmi_addr, dmi->dmi_addrlen);
  3392. }
  3393. /* looking for addresses to delete from slaves' list */
  3394. for (dmi = bond->mc_list; dmi; dmi = dmi->next) {
  3395. if (!bond_mc_list_find_dmi(dmi, bond_dev->mc_list))
  3396. bond_mc_delete(bond, dmi->dmi_addr, dmi->dmi_addrlen);
  3397. }
  3398. /* save master's multicast list */
  3399. bond_mc_list_destroy(bond);
  3400. bond_mc_list_copy(bond_dev->mc_list, bond, GFP_ATOMIC);
  3401. read_unlock(&bond->lock);
  3402. }
  3403. static int bond_neigh_setup(struct net_device *dev, struct neigh_parms *parms)
  3404. {
  3405. struct bonding *bond = netdev_priv(dev);
  3406. struct slave *slave = bond->first_slave;
  3407. if (slave) {
  3408. const struct net_device_ops *slave_ops
  3409. = slave->dev->netdev_ops;
  3410. if (slave_ops->ndo_neigh_setup)
  3411. return slave_ops->ndo_neigh_setup(slave->dev, parms);
  3412. }
  3413. return 0;
  3414. }
  3415. /*
  3416. * Change the MTU of all of a master's slaves to match the master
  3417. */
  3418. static int bond_change_mtu(struct net_device *bond_dev, int new_mtu)
  3419. {
  3420. struct bonding *bond = netdev_priv(bond_dev);
  3421. struct slave *slave, *stop_at;
  3422. int res = 0;
  3423. int i;
  3424. pr_debug("bond=%p, name=%s, new_mtu=%d\n", bond,
  3425. (bond_dev ? bond_dev->name : "None"), new_mtu);
  3426. /* Can't hold bond->lock with bh disabled here since
  3427. * some base drivers panic. On the other hand we can't
  3428. * hold bond->lock without bh disabled because we'll
  3429. * deadlock. The only solution is to rely on the fact
  3430. * that we're under rtnl_lock here, and the slaves
  3431. * list won't change. This doesn't solve the problem
  3432. * of setting the slave's MTU while it is
  3433. * transmitting, but the assumption is that the base
  3434. * driver can handle that.
  3435. *
  3436. * TODO: figure out a way to safely iterate the slaves
  3437. * list, but without holding a lock around the actual
  3438. * call to the base driver.
  3439. */
  3440. bond_for_each_slave(bond, slave, i) {
  3441. pr_debug("s %p s->p %p c_m %p\n", slave,
  3442. slave->prev, slave->dev->netdev_ops->ndo_change_mtu);
  3443. res = dev_set_mtu(slave->dev, new_mtu);
  3444. if (res) {
  3445. /* If we failed to set the slave's mtu to the new value
  3446. * we must abort the operation even in ACTIVE_BACKUP
  3447. * mode, because if we allow the backup slaves to have
  3448. * different mtu values than the active slave we'll
  3449. * need to change their mtu when doing a failover. That
  3450. * means changing their mtu from timer context, which
  3451. * is probably not a good idea.
  3452. */
  3453. pr_debug("err %d %s\n", res, slave->dev->name);
  3454. goto unwind;
  3455. }
  3456. }
  3457. bond_dev->mtu = new_mtu;
  3458. return 0;
  3459. unwind:
  3460. /* unwind from head to the slave that failed */
  3461. stop_at = slave;
  3462. bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
  3463. int tmp_res;
  3464. tmp_res = dev_set_mtu(slave->dev, bond_dev->mtu);
  3465. if (tmp_res) {
  3466. pr_debug("unwind err %d dev %s\n", tmp_res,
  3467. slave->dev->name);
  3468. }
  3469. }
  3470. return res;
  3471. }
  3472. /*
  3473. * Change HW address
  3474. *
  3475. * Note that many devices must be down to change the HW address, and
  3476. * downing the master releases all slaves. We can make bonds full of
  3477. * bonding devices to test this, however.
  3478. */
  3479. static int bond_set_mac_address(struct net_device *bond_dev, void *addr)
  3480. {
  3481. struct bonding *bond = netdev_priv(bond_dev);
  3482. struct sockaddr *sa = addr, tmp_sa;
  3483. struct slave *slave, *stop_at;
  3484. int res = 0;
  3485. int i;
  3486. if (bond->params.mode == BOND_MODE_ALB)
  3487. return bond_alb_set_mac_address(bond_dev, addr);
  3488. pr_debug("bond=%p, name=%s\n", bond, (bond_dev ? bond_dev->name : "None"));
  3489. /*
  3490. * If fail_over_mac is set to active, do nothing and return
  3491. * success. Returning an error causes ifenslave to fail.
  3492. */
  3493. if (bond->params.fail_over_mac == BOND_FOM_ACTIVE)
  3494. return 0;
  3495. if (!is_valid_ether_addr(sa->sa_data))
  3496. return -EADDRNOTAVAIL;
  3497. /* Can't hold bond->lock with bh disabled here since
  3498. * some base drivers panic. On the other hand we can't
  3499. * hold bond->lock without bh disabled because we'll
  3500. * deadlock. The only solution is to rely on the fact
  3501. * that we're under rtnl_lock here, and the slaves
  3502. * list won't change. This doesn't solve the problem
  3503. * of setting the slave's hw address while it is
  3504. * transmitting, but the assumption is that the base
  3505. * driver can handle that.
  3506. *
  3507. * TODO: figure out a way to safely iterate the slaves
  3508. * list, but without holding a lock around the actual
  3509. * call to the base driver.
  3510. */
  3511. bond_for_each_slave(bond, slave, i) {
  3512. const struct net_device_ops *slave_ops = slave->dev->netdev_ops;
  3513. pr_debug("slave %p %s\n", slave, slave->dev->name);
  3514. if (slave_ops->ndo_set_mac_address == NULL) {
  3515. res = -EOPNOTSUPP;
  3516. pr_debug("EOPNOTSUPP %s\n", slave->dev->name);
  3517. goto unwind;
  3518. }
  3519. res = dev_set_mac_address(slave->dev, addr);
  3520. if (res) {
  3521. /* TODO: consider downing the slave
  3522. * and retry ?
  3523. * User should expect communications
  3524. * breakage anyway until ARP finish
  3525. * updating, so...
  3526. */
  3527. pr_debug("err %d %s\n", res, slave->dev->name);
  3528. goto unwind;
  3529. }
  3530. }
  3531. /* success */
  3532. memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
  3533. return 0;
  3534. unwind:
  3535. memcpy(tmp_sa.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
  3536. tmp_sa.sa_family = bond_dev->type;
  3537. /* unwind from head to the slave that failed */
  3538. stop_at = slave;
  3539. bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
  3540. int tmp_res;
  3541. tmp_res = dev_set_mac_address(slave->dev, &tmp_sa);
  3542. if (tmp_res) {
  3543. pr_debug("unwind err %d dev %s\n", tmp_res,
  3544. slave->dev->name);
  3545. }
  3546. }
  3547. return res;
  3548. }
  3549. static int bond_xmit_roundrobin(struct sk_buff *skb, struct net_device *bond_dev)
  3550. {
  3551. struct bonding *bond = netdev_priv(bond_dev);
  3552. struct slave *slave, *start_at;
  3553. int i, slave_no, res = 1;
  3554. read_lock(&bond->lock);
  3555. if (!BOND_IS_OK(bond))
  3556. goto out;
  3557. /*
  3558. * Concurrent TX may collide on rr_tx_counter; we accept that
  3559. * as being rare enough not to justify using an atomic op here
  3560. */
  3561. slave_no = bond->rr_tx_counter++ % bond->slave_cnt;
  3562. bond_for_each_slave(bond, slave, i) {
  3563. slave_no--;
  3564. if (slave_no < 0)
  3565. break;
  3566. }
  3567. start_at = slave;
  3568. bond_for_each_slave_from(bond, slave, i, start_at) {
  3569. if (IS_UP(slave->dev) &&
  3570. (slave->link == BOND_LINK_UP) &&
  3571. (slave->state == BOND_STATE_ACTIVE)) {
  3572. res = bond_dev_queue_xmit(bond, skb, slave->dev);
  3573. break;
  3574. }
  3575. }
  3576. out:
  3577. if (res) {
  3578. /* no suitable interface, frame not sent */
  3579. dev_kfree_skb(skb);
  3580. }
  3581. read_unlock(&bond->lock);
  3582. return 0;
  3583. }
  3584. /*
  3585. * in active-backup mode, we know that bond->curr_active_slave is always valid if
  3586. * the bond has a usable interface.
  3587. */
  3588. static int bond_xmit_activebackup(struct sk_buff *skb, struct net_device *bond_dev)
  3589. {
  3590. struct bonding *bond = netdev_priv(bond_dev);
  3591. int res = 1;
  3592. read_lock(&bond->lock);
  3593. read_lock(&bond->curr_slave_lock);
  3594. if (!BOND_IS_OK(bond))
  3595. goto out;
  3596. if (!bond->curr_active_slave)
  3597. goto out;
  3598. res = bond_dev_queue_xmit(bond, skb, bond->curr_active_slave->dev);
  3599. out:
  3600. if (res)
  3601. /* no suitable interface, frame not sent */
  3602. dev_kfree_skb(skb);
  3603. read_unlock(&bond->curr_slave_lock);
  3604. read_unlock(&bond->lock);
  3605. return 0;
  3606. }
  3607. /*
  3608. * In bond_xmit_xor() , we determine the output device by using a pre-
  3609. * determined xmit_hash_policy(), If the selected device is not enabled,
  3610. * find the next active slave.
  3611. */
  3612. static int bond_xmit_xor(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. int slave_no;
  3617. int i;
  3618. int res = 1;
  3619. read_lock(&bond->lock);
  3620. if (!BOND_IS_OK(bond))
  3621. goto out;
  3622. slave_no = bond->xmit_hash_policy(skb, bond_dev, bond->slave_cnt);
  3623. bond_for_each_slave(bond, slave, i) {
  3624. slave_no--;
  3625. if (slave_no < 0)
  3626. break;
  3627. }
  3628. start_at = slave;
  3629. bond_for_each_slave_from(bond, slave, i, start_at) {
  3630. if (IS_UP(slave->dev) &&
  3631. (slave->link == BOND_LINK_UP) &&
  3632. (slave->state == BOND_STATE_ACTIVE)) {
  3633. res = bond_dev_queue_xmit(bond, skb, slave->dev);
  3634. break;
  3635. }
  3636. }
  3637. out:
  3638. if (res) {
  3639. /* no suitable interface, frame not sent */
  3640. dev_kfree_skb(skb);
  3641. }
  3642. read_unlock(&bond->lock);
  3643. return 0;
  3644. }
  3645. /*
  3646. * in broadcast mode, we send everything to all usable interfaces.
  3647. */
  3648. static int bond_xmit_broadcast(struct sk_buff *skb, struct net_device *bond_dev)
  3649. {
  3650. struct bonding *bond = netdev_priv(bond_dev);
  3651. struct slave *slave, *start_at;
  3652. struct net_device *tx_dev = NULL;
  3653. int i;
  3654. int res = 1;
  3655. read_lock(&bond->lock);
  3656. if (!BOND_IS_OK(bond))
  3657. goto out;
  3658. read_lock(&bond->curr_slave_lock);
  3659. start_at = bond->curr_active_slave;
  3660. read_unlock(&bond->curr_slave_lock);
  3661. if (!start_at)
  3662. goto out;
  3663. bond_for_each_slave_from(bond, slave, i, start_at) {
  3664. if (IS_UP(slave->dev) &&
  3665. (slave->link == BOND_LINK_UP) &&
  3666. (slave->state == BOND_STATE_ACTIVE)) {
  3667. if (tx_dev) {
  3668. struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
  3669. if (!skb2) {
  3670. pr_err(DRV_NAME
  3671. ": %s: Error: bond_xmit_broadcast(): "
  3672. "skb_clone() failed\n",
  3673. bond_dev->name);
  3674. continue;
  3675. }
  3676. res = bond_dev_queue_xmit(bond, skb2, tx_dev);
  3677. if (res) {
  3678. dev_kfree_skb(skb2);
  3679. continue;
  3680. }
  3681. }
  3682. tx_dev = slave->dev;
  3683. }
  3684. }
  3685. if (tx_dev)
  3686. res = bond_dev_queue_xmit(bond, skb, tx_dev);
  3687. out:
  3688. if (res)
  3689. /* no suitable interface, frame not sent */
  3690. dev_kfree_skb(skb);
  3691. /* frame sent to all suitable interfaces */
  3692. read_unlock(&bond->lock);
  3693. return 0;
  3694. }
  3695. /*------------------------- Device initialization ---------------------------*/
  3696. static void bond_set_xmit_hash_policy(struct bonding *bond)
  3697. {
  3698. switch (bond->params.xmit_policy) {
  3699. case BOND_XMIT_POLICY_LAYER23:
  3700. bond->xmit_hash_policy = bond_xmit_hash_policy_l23;
  3701. break;
  3702. case BOND_XMIT_POLICY_LAYER34:
  3703. bond->xmit_hash_policy = bond_xmit_hash_policy_l34;
  3704. break;
  3705. case BOND_XMIT_POLICY_LAYER2:
  3706. default:
  3707. bond->xmit_hash_policy = bond_xmit_hash_policy_l2;
  3708. break;
  3709. }
  3710. }
  3711. static int bond_start_xmit(struct sk_buff *skb, struct net_device *dev)
  3712. {
  3713. const struct bonding *bond = netdev_priv(dev);
  3714. switch (bond->params.mode) {
  3715. case BOND_MODE_ROUNDROBIN:
  3716. return bond_xmit_roundrobin(skb, dev);
  3717. case BOND_MODE_ACTIVEBACKUP:
  3718. return bond_xmit_activebackup(skb, dev);
  3719. case BOND_MODE_XOR:
  3720. return bond_xmit_xor(skb, dev);
  3721. case BOND_MODE_BROADCAST:
  3722. return bond_xmit_broadcast(skb, dev);
  3723. case BOND_MODE_8023AD:
  3724. return bond_3ad_xmit_xor(skb, dev);
  3725. case BOND_MODE_ALB:
  3726. case BOND_MODE_TLB:
  3727. return bond_alb_xmit(skb, dev);
  3728. default:
  3729. /* Should never happen, mode already checked */
  3730. pr_err(DRV_NAME ": %s: Error: Unknown bonding mode %d\n",
  3731. dev->name, bond->params.mode);
  3732. WARN_ON_ONCE(1);
  3733. dev_kfree_skb(skb);
  3734. return NETDEV_TX_OK;
  3735. }
  3736. }
  3737. /*
  3738. * set bond mode specific net device operations
  3739. */
  3740. void bond_set_mode_ops(struct bonding *bond, int mode)
  3741. {
  3742. struct net_device *bond_dev = bond->dev;
  3743. switch (mode) {
  3744. case BOND_MODE_ROUNDROBIN:
  3745. break;
  3746. case BOND_MODE_ACTIVEBACKUP:
  3747. break;
  3748. case BOND_MODE_XOR:
  3749. bond_set_xmit_hash_policy(bond);
  3750. break;
  3751. case BOND_MODE_BROADCAST:
  3752. break;
  3753. case BOND_MODE_8023AD:
  3754. bond_set_master_3ad_flags(bond);
  3755. bond_set_xmit_hash_policy(bond);
  3756. break;
  3757. case BOND_MODE_ALB:
  3758. bond_set_master_alb_flags(bond);
  3759. /* FALLTHRU */
  3760. case BOND_MODE_TLB:
  3761. break;
  3762. default:
  3763. /* Should never happen, mode already checked */
  3764. pr_err(DRV_NAME
  3765. ": %s: Error: Unknown bonding mode %d\n",
  3766. bond_dev->name,
  3767. mode);
  3768. break;
  3769. }
  3770. }
  3771. static void bond_ethtool_get_drvinfo(struct net_device *bond_dev,
  3772. struct ethtool_drvinfo *drvinfo)
  3773. {
  3774. strncpy(drvinfo->driver, DRV_NAME, 32);
  3775. strncpy(drvinfo->version, DRV_VERSION, 32);
  3776. snprintf(drvinfo->fw_version, 32, "%d", BOND_ABI_VERSION);
  3777. }
  3778. static const struct ethtool_ops bond_ethtool_ops = {
  3779. .get_drvinfo = bond_ethtool_get_drvinfo,
  3780. .get_link = ethtool_op_get_link,
  3781. .get_tx_csum = ethtool_op_get_tx_csum,
  3782. .get_sg = ethtool_op_get_sg,
  3783. .get_tso = ethtool_op_get_tso,
  3784. .get_ufo = ethtool_op_get_ufo,
  3785. .get_flags = ethtool_op_get_flags,
  3786. };
  3787. static const struct net_device_ops bond_netdev_ops = {
  3788. .ndo_init = bond_init,
  3789. .ndo_uninit = bond_uninit,
  3790. .ndo_open = bond_open,
  3791. .ndo_stop = bond_close,
  3792. .ndo_start_xmit = bond_start_xmit,
  3793. .ndo_get_stats = bond_get_stats,
  3794. .ndo_do_ioctl = bond_do_ioctl,
  3795. .ndo_set_multicast_list = bond_set_multicast_list,
  3796. .ndo_change_mtu = bond_change_mtu,
  3797. .ndo_set_mac_address = bond_set_mac_address,
  3798. .ndo_neigh_setup = bond_neigh_setup,
  3799. .ndo_vlan_rx_register = bond_vlan_rx_register,
  3800. .ndo_vlan_rx_add_vid = bond_vlan_rx_add_vid,
  3801. .ndo_vlan_rx_kill_vid = bond_vlan_rx_kill_vid,
  3802. };
  3803. static void bond_setup(struct net_device *bond_dev)
  3804. {
  3805. struct bonding *bond = netdev_priv(bond_dev);
  3806. /* initialize rwlocks */
  3807. rwlock_init(&bond->lock);
  3808. rwlock_init(&bond->curr_slave_lock);
  3809. bond->params = bonding_defaults;
  3810. /* Initialize pointers */
  3811. bond->dev = bond_dev;
  3812. INIT_LIST_HEAD(&bond->vlan_list);
  3813. /* Initialize the device entry points */
  3814. ether_setup(bond_dev);
  3815. bond_dev->netdev_ops = &bond_netdev_ops;
  3816. bond_dev->ethtool_ops = &bond_ethtool_ops;
  3817. bond_set_mode_ops(bond, bond->params.mode);
  3818. bond_dev->destructor = free_netdev;
  3819. /* Initialize the device options */
  3820. bond_dev->tx_queue_len = 0;
  3821. bond_dev->flags |= IFF_MASTER|IFF_MULTICAST;
  3822. bond_dev->priv_flags |= IFF_BONDING;
  3823. bond_dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  3824. if (bond->params.arp_interval)
  3825. bond_dev->priv_flags |= IFF_MASTER_ARPMON;
  3826. /* At first, we block adding VLANs. That's the only way to
  3827. * prevent problems that occur when adding VLANs over an
  3828. * empty bond. The block will be removed once non-challenged
  3829. * slaves are enslaved.
  3830. */
  3831. bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
  3832. /* don't acquire bond device's netif_tx_lock when
  3833. * transmitting */
  3834. bond_dev->features |= NETIF_F_LLTX;
  3835. /* By default, we declare the bond to be fully
  3836. * VLAN hardware accelerated capable. Special
  3837. * care is taken in the various xmit functions
  3838. * when there are slaves that are not hw accel
  3839. * capable
  3840. */
  3841. bond_dev->features |= (NETIF_F_HW_VLAN_TX |
  3842. NETIF_F_HW_VLAN_RX |
  3843. NETIF_F_HW_VLAN_FILTER);
  3844. }
  3845. static void bond_work_cancel_all(struct bonding *bond)
  3846. {
  3847. write_lock_bh(&bond->lock);
  3848. bond->kill_timers = 1;
  3849. write_unlock_bh(&bond->lock);
  3850. if (bond->params.miimon && delayed_work_pending(&bond->mii_work))
  3851. cancel_delayed_work(&bond->mii_work);
  3852. if (bond->params.arp_interval && delayed_work_pending(&bond->arp_work))
  3853. cancel_delayed_work(&bond->arp_work);
  3854. if (bond->params.mode == BOND_MODE_ALB &&
  3855. delayed_work_pending(&bond->alb_work))
  3856. cancel_delayed_work(&bond->alb_work);
  3857. if (bond->params.mode == BOND_MODE_8023AD &&
  3858. delayed_work_pending(&bond->ad_work))
  3859. cancel_delayed_work(&bond->ad_work);
  3860. }
  3861. /* De-initialize device specific data.
  3862. * Caller must hold rtnl_lock.
  3863. */
  3864. static void bond_deinit(struct net_device *bond_dev)
  3865. {
  3866. struct bonding *bond = netdev_priv(bond_dev);
  3867. list_del(&bond->bond_list);
  3868. bond_work_cancel_all(bond);
  3869. bond_remove_proc_entry(bond);
  3870. }
  3871. /* Unregister and free all bond devices.
  3872. * Caller must hold rtnl_lock.
  3873. */
  3874. static void bond_free_all(void)
  3875. {
  3876. struct bonding *bond, *nxt;
  3877. list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list) {
  3878. struct net_device *bond_dev = bond->dev;
  3879. bond_work_cancel_all(bond);
  3880. /* Release the bonded slaves */
  3881. bond_release_all(bond_dev);
  3882. unregister_netdevice(bond_dev);
  3883. }
  3884. bond_destroy_proc_dir();
  3885. }
  3886. /*------------------------- Module initialization ---------------------------*/
  3887. /*
  3888. * Convert string input module parms. Accept either the
  3889. * number of the mode or its string name. A bit complicated because
  3890. * some mode names are substrings of other names, and calls from sysfs
  3891. * may have whitespace in the name (trailing newlines, for example).
  3892. */
  3893. int bond_parse_parm(const char *buf, const struct bond_parm_tbl *tbl)
  3894. {
  3895. int modeint = -1, i, rv;
  3896. char *p, modestr[BOND_MAX_MODENAME_LEN + 1] = { 0, };
  3897. for (p = (char *)buf; *p; p++)
  3898. if (!(isdigit(*p) || isspace(*p)))
  3899. break;
  3900. if (*p)
  3901. rv = sscanf(buf, "%20s", modestr);
  3902. else
  3903. rv = sscanf(buf, "%d", &modeint);
  3904. if (!rv)
  3905. return -1;
  3906. for (i = 0; tbl[i].modename; i++) {
  3907. if (modeint == tbl[i].mode)
  3908. return tbl[i].mode;
  3909. if (strcmp(modestr, tbl[i].modename) == 0)
  3910. return tbl[i].mode;
  3911. }
  3912. return -1;
  3913. }
  3914. static int bond_check_params(struct bond_params *params)
  3915. {
  3916. int arp_validate_value, fail_over_mac_value;
  3917. /*
  3918. * Convert string parameters.
  3919. */
  3920. if (mode) {
  3921. bond_mode = bond_parse_parm(mode, bond_mode_tbl);
  3922. if (bond_mode == -1) {
  3923. pr_err(DRV_NAME
  3924. ": Error: Invalid bonding mode \"%s\"\n",
  3925. mode == NULL ? "NULL" : mode);
  3926. return -EINVAL;
  3927. }
  3928. }
  3929. if (xmit_hash_policy) {
  3930. if ((bond_mode != BOND_MODE_XOR) &&
  3931. (bond_mode != BOND_MODE_8023AD)) {
  3932. pr_info(DRV_NAME
  3933. ": xor_mode param is irrelevant in mode %s\n",
  3934. bond_mode_name(bond_mode));
  3935. } else {
  3936. xmit_hashtype = bond_parse_parm(xmit_hash_policy,
  3937. xmit_hashtype_tbl);
  3938. if (xmit_hashtype == -1) {
  3939. pr_err(DRV_NAME
  3940. ": Error: Invalid xmit_hash_policy \"%s\"\n",
  3941. xmit_hash_policy == NULL ? "NULL" :
  3942. xmit_hash_policy);
  3943. return -EINVAL;
  3944. }
  3945. }
  3946. }
  3947. if (lacp_rate) {
  3948. if (bond_mode != BOND_MODE_8023AD) {
  3949. pr_info(DRV_NAME
  3950. ": lacp_rate param is irrelevant in mode %s\n",
  3951. bond_mode_name(bond_mode));
  3952. } else {
  3953. lacp_fast = bond_parse_parm(lacp_rate, bond_lacp_tbl);
  3954. if (lacp_fast == -1) {
  3955. pr_err(DRV_NAME
  3956. ": Error: Invalid lacp rate \"%s\"\n",
  3957. lacp_rate == NULL ? "NULL" : lacp_rate);
  3958. return -EINVAL;
  3959. }
  3960. }
  3961. }
  3962. if (ad_select) {
  3963. params->ad_select = bond_parse_parm(ad_select, ad_select_tbl);
  3964. if (params->ad_select == -1) {
  3965. pr_err(DRV_NAME
  3966. ": Error: Invalid ad_select \"%s\"\n",
  3967. ad_select == NULL ? "NULL" : ad_select);
  3968. return -EINVAL;
  3969. }
  3970. if (bond_mode != BOND_MODE_8023AD) {
  3971. pr_warning(DRV_NAME
  3972. ": ad_select param only affects 802.3ad mode\n");
  3973. }
  3974. } else {
  3975. params->ad_select = BOND_AD_STABLE;
  3976. }
  3977. if (max_bonds < 0 || max_bonds > INT_MAX) {
  3978. pr_warning(DRV_NAME
  3979. ": Warning: max_bonds (%d) not in range %d-%d, so it "
  3980. "was reset to BOND_DEFAULT_MAX_BONDS (%d)\n",
  3981. max_bonds, 0, INT_MAX, BOND_DEFAULT_MAX_BONDS);
  3982. max_bonds = BOND_DEFAULT_MAX_BONDS;
  3983. }
  3984. if (miimon < 0) {
  3985. pr_warning(DRV_NAME
  3986. ": Warning: miimon module parameter (%d), "
  3987. "not in range 0-%d, so it was reset to %d\n",
  3988. miimon, INT_MAX, BOND_LINK_MON_INTERV);
  3989. miimon = BOND_LINK_MON_INTERV;
  3990. }
  3991. if (updelay < 0) {
  3992. pr_warning(DRV_NAME
  3993. ": Warning: updelay module parameter (%d), "
  3994. "not in range 0-%d, so it was reset to 0\n",
  3995. updelay, INT_MAX);
  3996. updelay = 0;
  3997. }
  3998. if (downdelay < 0) {
  3999. pr_warning(DRV_NAME
  4000. ": Warning: downdelay module parameter (%d), "
  4001. "not in range 0-%d, so it was reset to 0\n",
  4002. downdelay, INT_MAX);
  4003. downdelay = 0;
  4004. }
  4005. if ((use_carrier != 0) && (use_carrier != 1)) {
  4006. pr_warning(DRV_NAME
  4007. ": Warning: use_carrier module parameter (%d), "
  4008. "not of valid value (0/1), so it was set to 1\n",
  4009. use_carrier);
  4010. use_carrier = 1;
  4011. }
  4012. if (num_grat_arp < 0 || num_grat_arp > 255) {
  4013. pr_warning(DRV_NAME
  4014. ": Warning: num_grat_arp (%d) not in range 0-255 so it "
  4015. "was reset to 1 \n", num_grat_arp);
  4016. num_grat_arp = 1;
  4017. }
  4018. if (num_unsol_na < 0 || num_unsol_na > 255) {
  4019. pr_warning(DRV_NAME
  4020. ": Warning: num_unsol_na (%d) not in range 0-255 so it "
  4021. "was reset to 1 \n", num_unsol_na);
  4022. num_unsol_na = 1;
  4023. }
  4024. /* reset values for 802.3ad */
  4025. if (bond_mode == BOND_MODE_8023AD) {
  4026. if (!miimon) {
  4027. pr_warning(DRV_NAME
  4028. ": Warning: miimon must be specified, "
  4029. "otherwise bonding will not detect link "
  4030. "failure, speed and duplex which are "
  4031. "essential for 802.3ad operation\n");
  4032. pr_warning("Forcing miimon to 100msec\n");
  4033. miimon = 100;
  4034. }
  4035. }
  4036. /* reset values for TLB/ALB */
  4037. if ((bond_mode == BOND_MODE_TLB) ||
  4038. (bond_mode == BOND_MODE_ALB)) {
  4039. if (!miimon) {
  4040. pr_warning(DRV_NAME
  4041. ": Warning: miimon must be specified, "
  4042. "otherwise bonding will not detect link "
  4043. "failure and link speed which are essential "
  4044. "for TLB/ALB load balancing\n");
  4045. pr_warning("Forcing miimon to 100msec\n");
  4046. miimon = 100;
  4047. }
  4048. }
  4049. if (bond_mode == BOND_MODE_ALB) {
  4050. printk(KERN_NOTICE DRV_NAME
  4051. ": In ALB mode you might experience client "
  4052. "disconnections upon reconnection of a link if the "
  4053. "bonding module updelay parameter (%d msec) is "
  4054. "incompatible with the forwarding delay time of the "
  4055. "switch\n",
  4056. updelay);
  4057. }
  4058. if (!miimon) {
  4059. if (updelay || downdelay) {
  4060. /* just warn the user the up/down delay will have
  4061. * no effect since miimon is zero...
  4062. */
  4063. pr_warning(DRV_NAME
  4064. ": Warning: miimon module parameter not set "
  4065. "and updelay (%d) or downdelay (%d) module "
  4066. "parameter is set; updelay and downdelay have "
  4067. "no effect unless miimon is set\n",
  4068. updelay, downdelay);
  4069. }
  4070. } else {
  4071. /* don't allow arp monitoring */
  4072. if (arp_interval) {
  4073. pr_warning(DRV_NAME
  4074. ": Warning: miimon (%d) and arp_interval (%d) "
  4075. "can't be used simultaneously, disabling ARP "
  4076. "monitoring\n",
  4077. miimon, arp_interval);
  4078. arp_interval = 0;
  4079. }
  4080. if ((updelay % miimon) != 0) {
  4081. pr_warning(DRV_NAME
  4082. ": Warning: updelay (%d) is not a multiple "
  4083. "of miimon (%d), updelay rounded to %d ms\n",
  4084. updelay, miimon, (updelay / miimon) * miimon);
  4085. }
  4086. updelay /= miimon;
  4087. if ((downdelay % miimon) != 0) {
  4088. pr_warning(DRV_NAME
  4089. ": Warning: downdelay (%d) is not a multiple "
  4090. "of miimon (%d), downdelay rounded to %d ms\n",
  4091. downdelay, miimon,
  4092. (downdelay / miimon) * miimon);
  4093. }
  4094. downdelay /= miimon;
  4095. }
  4096. if (arp_interval < 0) {
  4097. pr_warning(DRV_NAME
  4098. ": Warning: arp_interval module parameter (%d) "
  4099. ", not in range 0-%d, so it was reset to %d\n",
  4100. arp_interval, INT_MAX, BOND_LINK_ARP_INTERV);
  4101. arp_interval = BOND_LINK_ARP_INTERV;
  4102. }
  4103. for (arp_ip_count = 0;
  4104. (arp_ip_count < BOND_MAX_ARP_TARGETS) && arp_ip_target[arp_ip_count];
  4105. arp_ip_count++) {
  4106. /* not complete check, but should be good enough to
  4107. catch mistakes */
  4108. if (!isdigit(arp_ip_target[arp_ip_count][0])) {
  4109. pr_warning(DRV_NAME
  4110. ": Warning: bad arp_ip_target module parameter "
  4111. "(%s), ARP monitoring will not be performed\n",
  4112. arp_ip_target[arp_ip_count]);
  4113. arp_interval = 0;
  4114. } else {
  4115. __be32 ip = in_aton(arp_ip_target[arp_ip_count]);
  4116. arp_target[arp_ip_count] = ip;
  4117. }
  4118. }
  4119. if (arp_interval && !arp_ip_count) {
  4120. /* don't allow arping if no arp_ip_target given... */
  4121. pr_warning(DRV_NAME
  4122. ": Warning: arp_interval module parameter (%d) "
  4123. "specified without providing an arp_ip_target "
  4124. "parameter, arp_interval was reset to 0\n",
  4125. arp_interval);
  4126. arp_interval = 0;
  4127. }
  4128. if (arp_validate) {
  4129. if (bond_mode != BOND_MODE_ACTIVEBACKUP) {
  4130. pr_err(DRV_NAME
  4131. ": arp_validate only supported in active-backup mode\n");
  4132. return -EINVAL;
  4133. }
  4134. if (!arp_interval) {
  4135. pr_err(DRV_NAME
  4136. ": arp_validate requires arp_interval\n");
  4137. return -EINVAL;
  4138. }
  4139. arp_validate_value = bond_parse_parm(arp_validate,
  4140. arp_validate_tbl);
  4141. if (arp_validate_value == -1) {
  4142. pr_err(DRV_NAME
  4143. ": Error: invalid arp_validate \"%s\"\n",
  4144. arp_validate == NULL ? "NULL" : arp_validate);
  4145. return -EINVAL;
  4146. }
  4147. } else
  4148. arp_validate_value = 0;
  4149. if (miimon) {
  4150. pr_info(DRV_NAME
  4151. ": MII link monitoring set to %d ms\n",
  4152. miimon);
  4153. } else if (arp_interval) {
  4154. int i;
  4155. pr_info(DRV_NAME ": ARP monitoring set to %d ms,"
  4156. " validate %s, with %d target(s):",
  4157. arp_interval,
  4158. arp_validate_tbl[arp_validate_value].modename,
  4159. arp_ip_count);
  4160. for (i = 0; i < arp_ip_count; i++)
  4161. printk(" %s", arp_ip_target[i]);
  4162. printk("\n");
  4163. } else if (max_bonds) {
  4164. /* miimon and arp_interval not set, we need one so things
  4165. * work as expected, see bonding.txt for details
  4166. */
  4167. pr_warning(DRV_NAME
  4168. ": Warning: either miimon or arp_interval and "
  4169. "arp_ip_target module parameters must be specified, "
  4170. "otherwise bonding will not detect link failures! see "
  4171. "bonding.txt for details.\n");
  4172. }
  4173. if (primary && !USES_PRIMARY(bond_mode)) {
  4174. /* currently, using a primary only makes sense
  4175. * in active backup, TLB or ALB modes
  4176. */
  4177. pr_warning(DRV_NAME
  4178. ": Warning: %s primary device specified but has no "
  4179. "effect in %s mode\n",
  4180. primary, bond_mode_name(bond_mode));
  4181. primary = NULL;
  4182. }
  4183. if (fail_over_mac) {
  4184. fail_over_mac_value = bond_parse_parm(fail_over_mac,
  4185. fail_over_mac_tbl);
  4186. if (fail_over_mac_value == -1) {
  4187. pr_err(DRV_NAME
  4188. ": Error: invalid fail_over_mac \"%s\"\n",
  4189. arp_validate == NULL ? "NULL" : arp_validate);
  4190. return -EINVAL;
  4191. }
  4192. if (bond_mode != BOND_MODE_ACTIVEBACKUP)
  4193. pr_warning(DRV_NAME
  4194. ": Warning: fail_over_mac only affects "
  4195. "active-backup mode.\n");
  4196. } else {
  4197. fail_over_mac_value = BOND_FOM_NONE;
  4198. }
  4199. /* fill params struct with the proper values */
  4200. params->mode = bond_mode;
  4201. params->xmit_policy = xmit_hashtype;
  4202. params->miimon = miimon;
  4203. params->num_grat_arp = num_grat_arp;
  4204. params->num_unsol_na = num_unsol_na;
  4205. params->arp_interval = arp_interval;
  4206. params->arp_validate = arp_validate_value;
  4207. params->updelay = updelay;
  4208. params->downdelay = downdelay;
  4209. params->use_carrier = use_carrier;
  4210. params->lacp_fast = lacp_fast;
  4211. params->primary[0] = 0;
  4212. params->fail_over_mac = fail_over_mac_value;
  4213. if (primary) {
  4214. strncpy(params->primary, primary, IFNAMSIZ);
  4215. params->primary[IFNAMSIZ - 1] = 0;
  4216. }
  4217. memcpy(params->arp_targets, arp_target, sizeof(arp_target));
  4218. return 0;
  4219. }
  4220. static struct lock_class_key bonding_netdev_xmit_lock_key;
  4221. static struct lock_class_key bonding_netdev_addr_lock_key;
  4222. static void bond_set_lockdep_class_one(struct net_device *dev,
  4223. struct netdev_queue *txq,
  4224. void *_unused)
  4225. {
  4226. lockdep_set_class(&txq->_xmit_lock,
  4227. &bonding_netdev_xmit_lock_key);
  4228. }
  4229. static void bond_set_lockdep_class(struct net_device *dev)
  4230. {
  4231. lockdep_set_class(&dev->addr_list_lock,
  4232. &bonding_netdev_addr_lock_key);
  4233. netdev_for_each_tx_queue(dev, bond_set_lockdep_class_one, NULL);
  4234. }
  4235. /*
  4236. * Called from registration process
  4237. */
  4238. static int bond_init(struct net_device *bond_dev)
  4239. {
  4240. struct bonding *bond = netdev_priv(bond_dev);
  4241. pr_debug("Begin bond_init for %s\n", bond_dev->name);
  4242. bond->wq = create_singlethread_workqueue(bond_dev->name);
  4243. if (!bond->wq)
  4244. return -ENOMEM;
  4245. bond_set_lockdep_class(bond_dev);
  4246. netif_carrier_off(bond_dev);
  4247. bond_create_proc_entry(bond);
  4248. list_add_tail(&bond->bond_list, &bond_dev_list);
  4249. return 0;
  4250. }
  4251. /* Create a new bond based on the specified name and bonding parameters.
  4252. * If name is NULL, obtain a suitable "bond%d" name for us.
  4253. * Caller must NOT hold rtnl_lock; we need to release it here before we
  4254. * set up our sysfs entries.
  4255. */
  4256. int bond_create(const char *name)
  4257. {
  4258. struct net_device *bond_dev;
  4259. int res;
  4260. rtnl_lock();
  4261. /* Check to see if the bond already exists. */
  4262. /* FIXME: pass netns from caller */
  4263. if (name && __dev_get_by_name(&init_net, name)) {
  4264. pr_err(DRV_NAME ": cannot add bond %s; already exists\n",
  4265. name);
  4266. res = -EEXIST;
  4267. goto out_rtnl;
  4268. }
  4269. bond_dev = alloc_netdev(sizeof(struct bonding), name ? name : "",
  4270. bond_setup);
  4271. if (!bond_dev) {
  4272. pr_err(DRV_NAME ": %s: eek! can't alloc netdev!\n",
  4273. name);
  4274. res = -ENOMEM;
  4275. goto out_rtnl;
  4276. }
  4277. if (!name) {
  4278. res = dev_alloc_name(bond_dev, "bond%d");
  4279. if (res < 0)
  4280. goto out_netdev;
  4281. }
  4282. res = register_netdevice(bond_dev);
  4283. if (res < 0)
  4284. goto out_bond;
  4285. res = bond_create_sysfs_entry(netdev_priv(bond_dev));
  4286. if (res < 0)
  4287. goto out_unreg;
  4288. rtnl_unlock();
  4289. return 0;
  4290. out_unreg:
  4291. unregister_netdevice(bond_dev);
  4292. out_bond:
  4293. bond_deinit(bond_dev);
  4294. out_netdev:
  4295. free_netdev(bond_dev);
  4296. out_rtnl:
  4297. rtnl_unlock();
  4298. return res;
  4299. }
  4300. static int __init bonding_init(void)
  4301. {
  4302. int i;
  4303. int res;
  4304. pr_info("%s", version);
  4305. res = bond_check_params(&bonding_defaults);
  4306. if (res)
  4307. goto out;
  4308. bond_create_proc_dir();
  4309. for (i = 0; i < max_bonds; i++) {
  4310. res = bond_create(NULL);
  4311. if (res)
  4312. goto err;
  4313. }
  4314. res = bond_create_sysfs();
  4315. if (res)
  4316. goto err;
  4317. register_netdevice_notifier(&bond_netdev_notifier);
  4318. register_inetaddr_notifier(&bond_inetaddr_notifier);
  4319. bond_register_ipv6_notifier();
  4320. goto out;
  4321. err:
  4322. rtnl_lock();
  4323. bond_free_all();
  4324. rtnl_unlock();
  4325. out:
  4326. return res;
  4327. }
  4328. static void __exit bonding_exit(void)
  4329. {
  4330. unregister_netdevice_notifier(&bond_netdev_notifier);
  4331. unregister_inetaddr_notifier(&bond_inetaddr_notifier);
  4332. bond_unregister_ipv6_notifier();
  4333. bond_destroy_sysfs();
  4334. rtnl_lock();
  4335. bond_free_all();
  4336. rtnl_unlock();
  4337. }
  4338. module_init(bonding_init);
  4339. module_exit(bonding_exit);
  4340. MODULE_LICENSE("GPL");
  4341. MODULE_VERSION(DRV_VERSION);
  4342. MODULE_DESCRIPTION(DRV_DESCRIPTION ", v" DRV_VERSION);
  4343. MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others");