bond_main.c 142 KB

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