bond_main.c 134 KB

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