bond_main.c 132 KB

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