bond_main.c 138 KB

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