bond_main.c 124 KB

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