bond_main.c 137 KB

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