bond_main.c 130 KB

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