bond_main.c 139 KB

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