bond_main.c 122 KB

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