bond_main.c 138 KB

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