bond_main.c 125 KB

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