bond_main.c 126 KB

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