bond_main.c 125 KB

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