bond_main.c 137 KB

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