bond_main.c 133 KB

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