bond_main.c 135 KB

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