bond_main.c 133 KB

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