bond_main.c 140 KB

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