bond_main.c 129 KB

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