bond_main.c 141 KB

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