bond_main.c 136 KB

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