bond_main.c 135 KB

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