bond_main.c 134 KB

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