bond_main.c 141 KB

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