bond_main.c 180 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673
  1. // SPDX-License-Identifier: GPL-1.0+
  2. /*
  3. * originally based on the dummy device.
  4. *
  5. * Copyright 1999, Thomas Davis, tadavis@lbl.gov.
  6. * Based on dummy.c, and eql.c devices.
  7. *
  8. * bonding.c: an Ethernet Bonding driver
  9. *
  10. * This is useful to talk to a Cisco EtherChannel compatible equipment:
  11. * Cisco 5500
  12. * Sun Trunking (Solaris)
  13. * Alteon AceDirector Trunks
  14. * Linux Bonding
  15. * and probably many L2 switches ...
  16. *
  17. * How it works:
  18. * ifconfig bond0 ipaddress netmask up
  19. * will setup a network device, with an ip address. No mac address
  20. * will be assigned at this time. The hw mac address will come from
  21. * the first slave bonded to the channel. All slaves will then use
  22. * this hw mac address.
  23. *
  24. * ifconfig bond0 down
  25. * will release all slaves, marking them as down.
  26. *
  27. * ifenslave bond0 eth0
  28. * will attach eth0 to bond0 as a slave. eth0 hw mac address will either
  29. * a: be used as initial mac address
  30. * b: if a hw mac address already is there, eth0's hw mac address
  31. * will then be set from bond0.
  32. *
  33. */
  34. #include <linux/kernel.h>
  35. #include <linux/module.h>
  36. #include <linux/types.h>
  37. #include <linux/fcntl.h>
  38. #include <linux/filter.h>
  39. #include <linux/interrupt.h>
  40. #include <linux/ptrace.h>
  41. #include <linux/ioport.h>
  42. #include <linux/in.h>
  43. #include <net/ip.h>
  44. #include <linux/ip.h>
  45. #include <linux/icmp.h>
  46. #include <linux/icmpv6.h>
  47. #include <linux/tcp.h>
  48. #include <linux/udp.h>
  49. #include <linux/slab.h>
  50. #include <linux/string.h>
  51. #include <linux/init.h>
  52. #include <linux/timer.h>
  53. #include <linux/socket.h>
  54. #include <linux/ctype.h>
  55. #include <linux/inet.h>
  56. #include <linux/bitops.h>
  57. #include <linux/io.h>
  58. #include <asm/dma.h>
  59. #include <linux/uaccess.h>
  60. #include <linux/errno.h>
  61. #include <linux/netdevice.h>
  62. #include <linux/inetdevice.h>
  63. #include <linux/igmp.h>
  64. #include <linux/etherdevice.h>
  65. #include <linux/skbuff.h>
  66. #include <net/sock.h>
  67. #include <linux/rtnetlink.h>
  68. #include <linux/smp.h>
  69. #include <linux/if_ether.h>
  70. #include <net/arp.h>
  71. #include <linux/mii.h>
  72. #include <linux/ethtool.h>
  73. #include <linux/if_vlan.h>
  74. #include <linux/if_bonding.h>
  75. #include <linux/phy.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 <net/pkt_sched.h>
  82. #include <linux/rculist.h>
  83. #include <net/flow_dissector.h>
  84. #include <net/xfrm.h>
  85. #include <net/bonding.h>
  86. #include <net/bond_3ad.h>
  87. #include <net/bond_alb.h>
  88. #if IS_ENABLED(CONFIG_TLS_DEVICE)
  89. #include <net/tls.h>
  90. #endif
  91. #include <net/ip6_route.h>
  92. #include <net/xdp.h>
  93. #include "bonding_priv.h"
  94. /*---------------------------- Module parameters ----------------------------*/
  95. /* monitor all links that often (in milliseconds). <=0 disables monitoring */
  96. static int max_bonds = BOND_DEFAULT_MAX_BONDS;
  97. static int tx_queues = BOND_DEFAULT_TX_QUEUES;
  98. static int num_peer_notif = 1;
  99. static int miimon;
  100. static int updelay;
  101. static int downdelay;
  102. static int use_carrier = 1;
  103. static char *mode;
  104. static char *primary;
  105. static char *primary_reselect;
  106. static char *lacp_rate;
  107. static int min_links;
  108. static char *ad_select;
  109. static char *xmit_hash_policy;
  110. static int arp_interval;
  111. static char *arp_ip_target[BOND_MAX_ARP_TARGETS];
  112. static char *arp_validate;
  113. static char *arp_all_targets;
  114. static char *fail_over_mac;
  115. static int all_slaves_active;
  116. static struct bond_params bonding_defaults;
  117. static int resend_igmp = BOND_DEFAULT_RESEND_IGMP;
  118. static int packets_per_slave = 1;
  119. static int lp_interval = BOND_ALB_DEFAULT_LP_INTERVAL;
  120. module_param(max_bonds, int, 0);
  121. MODULE_PARM_DESC(max_bonds, "Max number of bonded devices");
  122. module_param(tx_queues, int, 0);
  123. MODULE_PARM_DESC(tx_queues, "Max number of transmit queues (default = 16)");
  124. module_param_named(num_grat_arp, num_peer_notif, int, 0644);
  125. MODULE_PARM_DESC(num_grat_arp, "Number of peer notifications to send on "
  126. "failover event (alias of num_unsol_na)");
  127. module_param_named(num_unsol_na, num_peer_notif, int, 0644);
  128. MODULE_PARM_DESC(num_unsol_na, "Number of peer notifications to send on "
  129. "failover event (alias of num_grat_arp)");
  130. module_param(miimon, int, 0);
  131. MODULE_PARM_DESC(miimon, "Link check interval in milliseconds");
  132. module_param(updelay, int, 0);
  133. MODULE_PARM_DESC(updelay, "Delay before considering link up, in milliseconds");
  134. module_param(downdelay, int, 0);
  135. MODULE_PARM_DESC(downdelay, "Delay before considering link down, "
  136. "in milliseconds");
  137. module_param(use_carrier, int, 0);
  138. MODULE_PARM_DESC(use_carrier, "Use netif_carrier_ok (vs MII ioctls) in miimon; "
  139. "0 for off, 1 for on (default)");
  140. module_param(mode, charp, 0);
  141. MODULE_PARM_DESC(mode, "Mode of operation; 0 for balance-rr, "
  142. "1 for active-backup, 2 for balance-xor, "
  143. "3 for broadcast, 4 for 802.3ad, 5 for balance-tlb, "
  144. "6 for balance-alb");
  145. module_param(primary, charp, 0);
  146. MODULE_PARM_DESC(primary, "Primary network device to use");
  147. module_param(primary_reselect, charp, 0);
  148. MODULE_PARM_DESC(primary_reselect, "Reselect primary slave "
  149. "once it comes up; "
  150. "0 for always (default), "
  151. "1 for only if speed of primary is "
  152. "better, "
  153. "2 for only on active slave "
  154. "failure");
  155. module_param(lacp_rate, charp, 0);
  156. MODULE_PARM_DESC(lacp_rate, "LACPDU tx rate to request from 802.3ad partner; "
  157. "0 for slow, 1 for fast");
  158. module_param(ad_select, charp, 0);
  159. MODULE_PARM_DESC(ad_select, "802.3ad aggregation selection logic; "
  160. "0 for stable (default), 1 for bandwidth, "
  161. "2 for count");
  162. module_param(min_links, int, 0);
  163. MODULE_PARM_DESC(min_links, "Minimum number of available links before turning on carrier");
  164. module_param(xmit_hash_policy, charp, 0);
  165. MODULE_PARM_DESC(xmit_hash_policy, "balance-alb, balance-tlb, balance-xor, 802.3ad hashing method; "
  166. "0 for layer 2 (default), 1 for layer 3+4, "
  167. "2 for layer 2+3, 3 for encap layer 2+3, "
  168. "4 for encap layer 3+4, 5 for vlan+srcmac");
  169. module_param(arp_interval, int, 0);
  170. MODULE_PARM_DESC(arp_interval, "arp interval in milliseconds");
  171. module_param_array(arp_ip_target, charp, NULL, 0);
  172. MODULE_PARM_DESC(arp_ip_target, "arp targets in n.n.n.n form");
  173. module_param(arp_validate, charp, 0);
  174. MODULE_PARM_DESC(arp_validate, "validate src/dst of ARP probes; "
  175. "0 for none (default), 1 for active, "
  176. "2 for backup, 3 for all");
  177. module_param(arp_all_targets, charp, 0);
  178. MODULE_PARM_DESC(arp_all_targets, "fail on any/all arp targets timeout; 0 for any (default), 1 for all");
  179. module_param(fail_over_mac, charp, 0);
  180. MODULE_PARM_DESC(fail_over_mac, "For active-backup, do not set all slaves to "
  181. "the same MAC; 0 for none (default), "
  182. "1 for active, 2 for follow");
  183. module_param(all_slaves_active, int, 0);
  184. MODULE_PARM_DESC(all_slaves_active, "Keep all frames received on an interface "
  185. "by setting active flag for all slaves; "
  186. "0 for never (default), 1 for always.");
  187. module_param(resend_igmp, int, 0);
  188. MODULE_PARM_DESC(resend_igmp, "Number of IGMP membership reports to send on "
  189. "link failure");
  190. module_param(packets_per_slave, int, 0);
  191. MODULE_PARM_DESC(packets_per_slave, "Packets to send per slave in balance-rr "
  192. "mode; 0 for a random slave, 1 packet per "
  193. "slave (default), >1 packets per slave.");
  194. module_param(lp_interval, uint, 0);
  195. MODULE_PARM_DESC(lp_interval, "The number of seconds between instances where "
  196. "the bonding driver sends learning packets to "
  197. "each slaves peer switch. The default is 1.");
  198. /*----------------------------- Global variables ----------------------------*/
  199. #ifdef CONFIG_NET_POLL_CONTROLLER
  200. atomic_t netpoll_block_tx = ATOMIC_INIT(0);
  201. #endif
  202. unsigned int bond_net_id __read_mostly;
  203. static const struct flow_dissector_key flow_keys_bonding_keys[] = {
  204. {
  205. .key_id = FLOW_DISSECTOR_KEY_CONTROL,
  206. .offset = offsetof(struct flow_keys, control),
  207. },
  208. {
  209. .key_id = FLOW_DISSECTOR_KEY_BASIC,
  210. .offset = offsetof(struct flow_keys, basic),
  211. },
  212. {
  213. .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
  214. .offset = offsetof(struct flow_keys, addrs.v4addrs),
  215. },
  216. {
  217. .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
  218. .offset = offsetof(struct flow_keys, addrs.v6addrs),
  219. },
  220. {
  221. .key_id = FLOW_DISSECTOR_KEY_TIPC,
  222. .offset = offsetof(struct flow_keys, addrs.tipckey),
  223. },
  224. {
  225. .key_id = FLOW_DISSECTOR_KEY_PORTS,
  226. .offset = offsetof(struct flow_keys, ports),
  227. },
  228. {
  229. .key_id = FLOW_DISSECTOR_KEY_ICMP,
  230. .offset = offsetof(struct flow_keys, icmp),
  231. },
  232. {
  233. .key_id = FLOW_DISSECTOR_KEY_VLAN,
  234. .offset = offsetof(struct flow_keys, vlan),
  235. },
  236. {
  237. .key_id = FLOW_DISSECTOR_KEY_FLOW_LABEL,
  238. .offset = offsetof(struct flow_keys, tags),
  239. },
  240. {
  241. .key_id = FLOW_DISSECTOR_KEY_GRE_KEYID,
  242. .offset = offsetof(struct flow_keys, keyid),
  243. },
  244. };
  245. static struct flow_dissector flow_keys_bonding __read_mostly;
  246. /*-------------------------- Forward declarations ---------------------------*/
  247. static int bond_init(struct net_device *bond_dev);
  248. static void bond_uninit(struct net_device *bond_dev);
  249. static void bond_get_stats(struct net_device *bond_dev,
  250. struct rtnl_link_stats64 *stats);
  251. static void bond_slave_arr_handler(struct work_struct *work);
  252. static bool bond_time_in_interval(struct bonding *bond, unsigned long last_act,
  253. int mod);
  254. static void bond_netdev_notify_work(struct work_struct *work);
  255. /*---------------------------- General routines -----------------------------*/
  256. const char *bond_mode_name(int mode)
  257. {
  258. static const char *names[] = {
  259. [BOND_MODE_ROUNDROBIN] = "load balancing (round-robin)",
  260. [BOND_MODE_ACTIVEBACKUP] = "fault-tolerance (active-backup)",
  261. [BOND_MODE_XOR] = "load balancing (xor)",
  262. [BOND_MODE_BROADCAST] = "fault-tolerance (broadcast)",
  263. [BOND_MODE_8023AD] = "IEEE 802.3ad Dynamic link aggregation",
  264. [BOND_MODE_TLB] = "transmit load balancing",
  265. [BOND_MODE_ALB] = "adaptive load balancing",
  266. };
  267. if (mode < BOND_MODE_ROUNDROBIN || mode > BOND_MODE_ALB)
  268. return "unknown";
  269. return names[mode];
  270. }
  271. /**
  272. * bond_dev_queue_xmit - Prepare skb for xmit.
  273. *
  274. * @bond: bond device that got this skb for tx.
  275. * @skb: hw accel VLAN tagged skb to transmit
  276. * @slave_dev: slave that is supposed to xmit this skbuff
  277. */
  278. netdev_tx_t bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb,
  279. struct net_device *slave_dev)
  280. {
  281. skb->dev = slave_dev;
  282. BUILD_BUG_ON(sizeof(skb->queue_mapping) !=
  283. sizeof(qdisc_skb_cb(skb)->slave_dev_queue_mapping));
  284. skb_set_queue_mapping(skb, qdisc_skb_cb(skb)->slave_dev_queue_mapping);
  285. if (unlikely(netpoll_tx_running(bond->dev)))
  286. return bond_netpoll_send_skb(bond_get_slave_by_dev(bond, slave_dev), skb);
  287. return dev_queue_xmit(skb);
  288. }
  289. static bool bond_sk_check(struct bonding *bond)
  290. {
  291. switch (BOND_MODE(bond)) {
  292. case BOND_MODE_8023AD:
  293. case BOND_MODE_XOR:
  294. if (bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER34)
  295. return true;
  296. fallthrough;
  297. default:
  298. return false;
  299. }
  300. }
  301. static bool bond_xdp_check(struct bonding *bond)
  302. {
  303. switch (BOND_MODE(bond)) {
  304. case BOND_MODE_ROUNDROBIN:
  305. case BOND_MODE_ACTIVEBACKUP:
  306. return true;
  307. case BOND_MODE_8023AD:
  308. case BOND_MODE_XOR:
  309. /* vlan+srcmac is not supported with XDP as in most cases the 802.1q
  310. * payload is not in the packet due to hardware offload.
  311. */
  312. if (bond->params.xmit_policy != BOND_XMIT_POLICY_VLAN_SRCMAC)
  313. return true;
  314. fallthrough;
  315. default:
  316. return false;
  317. }
  318. }
  319. /*---------------------------------- VLAN -----------------------------------*/
  320. /* In the following 2 functions, bond_vlan_rx_add_vid and bond_vlan_rx_kill_vid,
  321. * We don't protect the slave list iteration with a lock because:
  322. * a. This operation is performed in IOCTL context,
  323. * b. The operation is protected by the RTNL semaphore in the 8021q code,
  324. * c. Holding a lock with BH disabled while directly calling a base driver
  325. * entry point is generally a BAD idea.
  326. *
  327. * The design of synchronization/protection for this operation in the 8021q
  328. * module is good for one or more VLAN devices over a single physical device
  329. * and cannot be extended for a teaming solution like bonding, so there is a
  330. * potential race condition here where a net device from the vlan group might
  331. * be referenced (either by a base driver or the 8021q code) while it is being
  332. * removed from the system. However, it turns out we're not making matters
  333. * worse, and if it works for regular VLAN usage it will work here too.
  334. */
  335. /**
  336. * bond_vlan_rx_add_vid - Propagates adding an id to slaves
  337. * @bond_dev: bonding net device that got called
  338. * @proto: network protocol ID
  339. * @vid: vlan id being added
  340. */
  341. static int bond_vlan_rx_add_vid(struct net_device *bond_dev,
  342. __be16 proto, u16 vid)
  343. {
  344. struct bonding *bond = netdev_priv(bond_dev);
  345. struct slave *slave, *rollback_slave;
  346. struct list_head *iter;
  347. int res;
  348. bond_for_each_slave(bond, slave, iter) {
  349. res = vlan_vid_add(slave->dev, proto, vid);
  350. if (res)
  351. goto unwind;
  352. }
  353. return 0;
  354. unwind:
  355. /* unwind to the slave that failed */
  356. bond_for_each_slave(bond, rollback_slave, iter) {
  357. if (rollback_slave == slave)
  358. break;
  359. vlan_vid_del(rollback_slave->dev, proto, vid);
  360. }
  361. return res;
  362. }
  363. /**
  364. * bond_vlan_rx_kill_vid - Propagates deleting an id to slaves
  365. * @bond_dev: bonding net device that got called
  366. * @proto: network protocol ID
  367. * @vid: vlan id being removed
  368. */
  369. static int bond_vlan_rx_kill_vid(struct net_device *bond_dev,
  370. __be16 proto, u16 vid)
  371. {
  372. struct bonding *bond = netdev_priv(bond_dev);
  373. struct list_head *iter;
  374. struct slave *slave;
  375. bond_for_each_slave(bond, slave, iter)
  376. vlan_vid_del(slave->dev, proto, vid);
  377. if (bond_is_lb(bond))
  378. bond_alb_clear_vlan(bond, vid);
  379. return 0;
  380. }
  381. /*---------------------------------- XFRM -----------------------------------*/
  382. #ifdef CONFIG_XFRM_OFFLOAD
  383. /**
  384. * bond_ipsec_dev - Get active device for IPsec offload
  385. * @xs: pointer to transformer state struct
  386. *
  387. * Context: caller must hold rcu_read_lock.
  388. *
  389. * Return: the device for ipsec offload, or NULL if not exist.
  390. **/
  391. static struct net_device *bond_ipsec_dev(struct xfrm_state *xs)
  392. {
  393. struct net_device *bond_dev = xs->xso.dev;
  394. struct bonding *bond;
  395. struct slave *slave;
  396. if (!bond_dev)
  397. return NULL;
  398. bond = netdev_priv(bond_dev);
  399. if (BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP)
  400. return NULL;
  401. slave = rcu_dereference(bond->curr_active_slave);
  402. if (!slave)
  403. return NULL;
  404. if (!xs->xso.real_dev)
  405. return NULL;
  406. if (xs->xso.real_dev != slave->dev)
  407. pr_warn_ratelimited("%s: (slave %s): not same with IPsec offload real dev %s\n",
  408. bond_dev->name, slave->dev->name, xs->xso.real_dev->name);
  409. return slave->dev;
  410. }
  411. /**
  412. * bond_ipsec_add_sa - program device with a security association
  413. * @xs: pointer to transformer state struct
  414. * @extack: extack point to fill failure reason
  415. **/
  416. static int bond_ipsec_add_sa(struct xfrm_state *xs,
  417. struct netlink_ext_ack *extack)
  418. {
  419. struct net_device *bond_dev = xs->xso.dev;
  420. struct net_device *real_dev;
  421. netdevice_tracker tracker;
  422. struct bond_ipsec *ipsec;
  423. struct bonding *bond;
  424. struct slave *slave;
  425. int err;
  426. if (!bond_dev)
  427. return -EINVAL;
  428. rcu_read_lock();
  429. bond = netdev_priv(bond_dev);
  430. slave = rcu_dereference(bond->curr_active_slave);
  431. real_dev = slave ? slave->dev : NULL;
  432. netdev_hold(real_dev, &tracker, GFP_ATOMIC);
  433. rcu_read_unlock();
  434. if (!real_dev) {
  435. err = -ENODEV;
  436. goto out;
  437. }
  438. if (!real_dev->xfrmdev_ops ||
  439. !real_dev->xfrmdev_ops->xdo_dev_state_add ||
  440. netif_is_bond_master(real_dev)) {
  441. NL_SET_ERR_MSG_MOD(extack, "Slave does not support ipsec offload");
  442. err = -EINVAL;
  443. goto out;
  444. }
  445. ipsec = kmalloc(sizeof(*ipsec), GFP_KERNEL);
  446. if (!ipsec) {
  447. err = -ENOMEM;
  448. goto out;
  449. }
  450. err = real_dev->xfrmdev_ops->xdo_dev_state_add(xs, extack);
  451. if (!err) {
  452. xs->xso.real_dev = real_dev;
  453. ipsec->xs = xs;
  454. INIT_LIST_HEAD(&ipsec->list);
  455. mutex_lock(&bond->ipsec_lock);
  456. list_add(&ipsec->list, &bond->ipsec_list);
  457. mutex_unlock(&bond->ipsec_lock);
  458. } else {
  459. kfree(ipsec);
  460. }
  461. out:
  462. netdev_put(real_dev, &tracker);
  463. return err;
  464. }
  465. static void bond_ipsec_add_sa_all(struct bonding *bond)
  466. {
  467. struct net_device *bond_dev = bond->dev;
  468. struct net_device *real_dev;
  469. struct bond_ipsec *ipsec;
  470. struct slave *slave;
  471. slave = rtnl_dereference(bond->curr_active_slave);
  472. real_dev = slave ? slave->dev : NULL;
  473. if (!real_dev)
  474. return;
  475. mutex_lock(&bond->ipsec_lock);
  476. if (!real_dev->xfrmdev_ops ||
  477. !real_dev->xfrmdev_ops->xdo_dev_state_add ||
  478. netif_is_bond_master(real_dev)) {
  479. if (!list_empty(&bond->ipsec_list))
  480. slave_warn(bond_dev, real_dev,
  481. "%s: no slave xdo_dev_state_add\n",
  482. __func__);
  483. goto out;
  484. }
  485. list_for_each_entry(ipsec, &bond->ipsec_list, list) {
  486. /* If new state is added before ipsec_lock acquired */
  487. if (ipsec->xs->xso.real_dev == real_dev)
  488. continue;
  489. if (real_dev->xfrmdev_ops->xdo_dev_state_add(ipsec->xs, NULL)) {
  490. slave_warn(bond_dev, real_dev, "%s: failed to add SA\n", __func__);
  491. continue;
  492. }
  493. ipsec->xs->xso.real_dev = real_dev;
  494. }
  495. out:
  496. mutex_unlock(&bond->ipsec_lock);
  497. }
  498. /**
  499. * bond_ipsec_del_sa - clear out this specific SA
  500. * @xs: pointer to transformer state struct
  501. **/
  502. static void bond_ipsec_del_sa(struct xfrm_state *xs)
  503. {
  504. struct net_device *bond_dev = xs->xso.dev;
  505. struct net_device *real_dev;
  506. netdevice_tracker tracker;
  507. struct bond_ipsec *ipsec;
  508. struct bonding *bond;
  509. struct slave *slave;
  510. if (!bond_dev)
  511. return;
  512. rcu_read_lock();
  513. bond = netdev_priv(bond_dev);
  514. slave = rcu_dereference(bond->curr_active_slave);
  515. real_dev = slave ? slave->dev : NULL;
  516. netdev_hold(real_dev, &tracker, GFP_ATOMIC);
  517. rcu_read_unlock();
  518. if (!slave)
  519. goto out;
  520. if (!xs->xso.real_dev)
  521. goto out;
  522. WARN_ON(xs->xso.real_dev != real_dev);
  523. if (!real_dev->xfrmdev_ops ||
  524. !real_dev->xfrmdev_ops->xdo_dev_state_delete ||
  525. netif_is_bond_master(real_dev)) {
  526. slave_warn(bond_dev, real_dev, "%s: no slave xdo_dev_state_delete\n", __func__);
  527. goto out;
  528. }
  529. real_dev->xfrmdev_ops->xdo_dev_state_delete(xs);
  530. out:
  531. netdev_put(real_dev, &tracker);
  532. mutex_lock(&bond->ipsec_lock);
  533. list_for_each_entry(ipsec, &bond->ipsec_list, list) {
  534. if (ipsec->xs == xs) {
  535. list_del(&ipsec->list);
  536. kfree(ipsec);
  537. break;
  538. }
  539. }
  540. mutex_unlock(&bond->ipsec_lock);
  541. }
  542. static void bond_ipsec_del_sa_all(struct bonding *bond)
  543. {
  544. struct net_device *bond_dev = bond->dev;
  545. struct net_device *real_dev;
  546. struct bond_ipsec *ipsec;
  547. struct slave *slave;
  548. slave = rtnl_dereference(bond->curr_active_slave);
  549. real_dev = slave ? slave->dev : NULL;
  550. if (!real_dev)
  551. return;
  552. mutex_lock(&bond->ipsec_lock);
  553. list_for_each_entry(ipsec, &bond->ipsec_list, list) {
  554. if (!ipsec->xs->xso.real_dev)
  555. continue;
  556. if (!real_dev->xfrmdev_ops ||
  557. !real_dev->xfrmdev_ops->xdo_dev_state_delete ||
  558. netif_is_bond_master(real_dev)) {
  559. slave_warn(bond_dev, real_dev,
  560. "%s: no slave xdo_dev_state_delete\n",
  561. __func__);
  562. } else {
  563. ipsec->xs->xso.real_dev = NULL;
  564. real_dev->xfrmdev_ops->xdo_dev_state_delete(ipsec->xs);
  565. if (real_dev->xfrmdev_ops->xdo_dev_state_free)
  566. real_dev->xfrmdev_ops->xdo_dev_state_free(ipsec->xs);
  567. }
  568. }
  569. mutex_unlock(&bond->ipsec_lock);
  570. }
  571. static void bond_ipsec_free_sa(struct xfrm_state *xs)
  572. {
  573. struct net_device *bond_dev = xs->xso.dev;
  574. struct net_device *real_dev;
  575. netdevice_tracker tracker;
  576. struct bonding *bond;
  577. struct slave *slave;
  578. if (!bond_dev)
  579. return;
  580. rcu_read_lock();
  581. bond = netdev_priv(bond_dev);
  582. slave = rcu_dereference(bond->curr_active_slave);
  583. real_dev = slave ? slave->dev : NULL;
  584. netdev_hold(real_dev, &tracker, GFP_ATOMIC);
  585. rcu_read_unlock();
  586. if (!slave)
  587. goto out;
  588. if (!xs->xso.real_dev)
  589. goto out;
  590. WARN_ON(xs->xso.real_dev != real_dev);
  591. xs->xso.real_dev = NULL;
  592. if (real_dev && real_dev->xfrmdev_ops &&
  593. real_dev->xfrmdev_ops->xdo_dev_state_free)
  594. real_dev->xfrmdev_ops->xdo_dev_state_free(xs);
  595. out:
  596. netdev_put(real_dev, &tracker);
  597. }
  598. /**
  599. * bond_ipsec_offload_ok - can this packet use the xfrm hw offload
  600. * @skb: current data packet
  601. * @xs: pointer to transformer state struct
  602. **/
  603. static bool bond_ipsec_offload_ok(struct sk_buff *skb, struct xfrm_state *xs)
  604. {
  605. struct net_device *real_dev;
  606. bool ok = false;
  607. rcu_read_lock();
  608. real_dev = bond_ipsec_dev(xs);
  609. if (!real_dev)
  610. goto out;
  611. if (!real_dev->xfrmdev_ops ||
  612. !real_dev->xfrmdev_ops->xdo_dev_offload_ok ||
  613. netif_is_bond_master(real_dev))
  614. goto out;
  615. ok = real_dev->xfrmdev_ops->xdo_dev_offload_ok(skb, xs);
  616. out:
  617. rcu_read_unlock();
  618. return ok;
  619. }
  620. /**
  621. * bond_advance_esn_state - ESN support for IPSec HW offload
  622. * @xs: pointer to transformer state struct
  623. **/
  624. static void bond_advance_esn_state(struct xfrm_state *xs)
  625. {
  626. struct net_device *real_dev;
  627. rcu_read_lock();
  628. real_dev = bond_ipsec_dev(xs);
  629. if (!real_dev)
  630. goto out;
  631. if (!real_dev->xfrmdev_ops ||
  632. !real_dev->xfrmdev_ops->xdo_dev_state_advance_esn) {
  633. pr_warn_ratelimited("%s: %s doesn't support xdo_dev_state_advance_esn\n", __func__, real_dev->name);
  634. goto out;
  635. }
  636. real_dev->xfrmdev_ops->xdo_dev_state_advance_esn(xs);
  637. out:
  638. rcu_read_unlock();
  639. }
  640. /**
  641. * bond_xfrm_update_stats - Update xfrm state
  642. * @xs: pointer to transformer state struct
  643. **/
  644. static void bond_xfrm_update_stats(struct xfrm_state *xs)
  645. {
  646. struct net_device *real_dev;
  647. rcu_read_lock();
  648. real_dev = bond_ipsec_dev(xs);
  649. if (!real_dev)
  650. goto out;
  651. if (!real_dev->xfrmdev_ops ||
  652. !real_dev->xfrmdev_ops->xdo_dev_state_update_stats) {
  653. pr_warn_ratelimited("%s: %s doesn't support xdo_dev_state_update_stats\n", __func__, real_dev->name);
  654. goto out;
  655. }
  656. real_dev->xfrmdev_ops->xdo_dev_state_update_stats(xs);
  657. out:
  658. rcu_read_unlock();
  659. }
  660. static const struct xfrmdev_ops bond_xfrmdev_ops = {
  661. .xdo_dev_state_add = bond_ipsec_add_sa,
  662. .xdo_dev_state_delete = bond_ipsec_del_sa,
  663. .xdo_dev_state_free = bond_ipsec_free_sa,
  664. .xdo_dev_offload_ok = bond_ipsec_offload_ok,
  665. .xdo_dev_state_advance_esn = bond_advance_esn_state,
  666. .xdo_dev_state_update_stats = bond_xfrm_update_stats,
  667. };
  668. #endif /* CONFIG_XFRM_OFFLOAD */
  669. /*------------------------------- Link status -------------------------------*/
  670. /* Set the carrier state for the master according to the state of its
  671. * slaves. If any slaves are up, the master is up. In 802.3ad mode,
  672. * do special 802.3ad magic.
  673. *
  674. * Returns zero if carrier state does not change, nonzero if it does.
  675. */
  676. int bond_set_carrier(struct bonding *bond)
  677. {
  678. struct list_head *iter;
  679. struct slave *slave;
  680. if (!bond_has_slaves(bond))
  681. goto down;
  682. if (BOND_MODE(bond) == BOND_MODE_8023AD)
  683. return bond_3ad_set_carrier(bond);
  684. bond_for_each_slave(bond, slave, iter) {
  685. if (slave->link == BOND_LINK_UP) {
  686. if (!netif_carrier_ok(bond->dev)) {
  687. netif_carrier_on(bond->dev);
  688. return 1;
  689. }
  690. return 0;
  691. }
  692. }
  693. down:
  694. if (netif_carrier_ok(bond->dev)) {
  695. netif_carrier_off(bond->dev);
  696. return 1;
  697. }
  698. return 0;
  699. }
  700. /* Get link speed and duplex from the slave's base driver
  701. * using ethtool. If for some reason the call fails or the
  702. * values are invalid, set speed and duplex to -1,
  703. * and return. Return 1 if speed or duplex settings are
  704. * UNKNOWN; 0 otherwise.
  705. */
  706. static int bond_update_speed_duplex(struct slave *slave)
  707. {
  708. struct net_device *slave_dev = slave->dev;
  709. struct ethtool_link_ksettings ecmd;
  710. int res;
  711. slave->speed = SPEED_UNKNOWN;
  712. slave->duplex = DUPLEX_UNKNOWN;
  713. res = __ethtool_get_link_ksettings(slave_dev, &ecmd);
  714. if (res < 0)
  715. return 1;
  716. if (ecmd.base.speed == 0 || ecmd.base.speed == ((__u32)-1))
  717. return 1;
  718. switch (ecmd.base.duplex) {
  719. case DUPLEX_FULL:
  720. case DUPLEX_HALF:
  721. break;
  722. default:
  723. return 1;
  724. }
  725. slave->speed = ecmd.base.speed;
  726. slave->duplex = ecmd.base.duplex;
  727. return 0;
  728. }
  729. const char *bond_slave_link_status(s8 link)
  730. {
  731. switch (link) {
  732. case BOND_LINK_UP:
  733. return "up";
  734. case BOND_LINK_FAIL:
  735. return "going down";
  736. case BOND_LINK_DOWN:
  737. return "down";
  738. case BOND_LINK_BACK:
  739. return "going back";
  740. default:
  741. return "unknown";
  742. }
  743. }
  744. /* if <dev> supports MII link status reporting, check its link status.
  745. *
  746. * We either do MII/ETHTOOL ioctls, or check netif_carrier_ok(),
  747. * depending upon the setting of the use_carrier parameter.
  748. *
  749. * Return either BMSR_LSTATUS, meaning that the link is up (or we
  750. * can't tell and just pretend it is), or 0, meaning that the link is
  751. * down.
  752. *
  753. * If reporting is non-zero, instead of faking link up, return -1 if
  754. * both ETHTOOL and MII ioctls fail (meaning the device does not
  755. * support them). If use_carrier is set, return whatever it says.
  756. * It'd be nice if there was a good way to tell if a driver supports
  757. * netif_carrier, but there really isn't.
  758. */
  759. static int bond_check_dev_link(struct bonding *bond,
  760. struct net_device *slave_dev, int reporting)
  761. {
  762. const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
  763. int (*ioctl)(struct net_device *, struct ifreq *, int);
  764. struct ifreq ifr;
  765. struct mii_ioctl_data *mii;
  766. if (!reporting && !netif_running(slave_dev))
  767. return 0;
  768. if (bond->params.use_carrier)
  769. return netif_carrier_ok(slave_dev) ? BMSR_LSTATUS : 0;
  770. /* Try to get link status using Ethtool first. */
  771. if (slave_dev->ethtool_ops->get_link)
  772. return slave_dev->ethtool_ops->get_link(slave_dev) ?
  773. BMSR_LSTATUS : 0;
  774. /* Ethtool can't be used, fallback to MII ioctls. */
  775. ioctl = slave_ops->ndo_eth_ioctl;
  776. if (ioctl) {
  777. /* TODO: set pointer to correct ioctl on a per team member
  778. * bases to make this more efficient. that is, once
  779. * we determine the correct ioctl, we will always
  780. * call it and not the others for that team
  781. * member.
  782. */
  783. /* We cannot assume that SIOCGMIIPHY will also read a
  784. * register; not all network drivers (e.g., e100)
  785. * support that.
  786. */
  787. /* Yes, the mii is overlaid on the ifreq.ifr_ifru */
  788. strscpy_pad(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
  789. mii = if_mii(&ifr);
  790. if (ioctl(slave_dev, &ifr, SIOCGMIIPHY) == 0) {
  791. mii->reg_num = MII_BMSR;
  792. if (ioctl(slave_dev, &ifr, SIOCGMIIREG) == 0)
  793. return mii->val_out & BMSR_LSTATUS;
  794. }
  795. }
  796. /* If reporting, report that either there's no ndo_eth_ioctl,
  797. * or both SIOCGMIIREG and get_link failed (meaning that we
  798. * cannot report link status). If not reporting, pretend
  799. * we're ok.
  800. */
  801. return reporting ? -1 : BMSR_LSTATUS;
  802. }
  803. /*----------------------------- Multicast list ------------------------------*/
  804. /* Push the promiscuity flag down to appropriate slaves */
  805. static int bond_set_promiscuity(struct bonding *bond, int inc)
  806. {
  807. struct list_head *iter;
  808. int err = 0;
  809. if (bond_uses_primary(bond)) {
  810. struct slave *curr_active = rtnl_dereference(bond->curr_active_slave);
  811. if (curr_active)
  812. err = dev_set_promiscuity(curr_active->dev, inc);
  813. } else {
  814. struct slave *slave;
  815. bond_for_each_slave(bond, slave, iter) {
  816. err = dev_set_promiscuity(slave->dev, inc);
  817. if (err)
  818. return err;
  819. }
  820. }
  821. return err;
  822. }
  823. /* Push the allmulti flag down to all slaves */
  824. static int bond_set_allmulti(struct bonding *bond, int inc)
  825. {
  826. struct list_head *iter;
  827. int err = 0;
  828. if (bond_uses_primary(bond)) {
  829. struct slave *curr_active = rtnl_dereference(bond->curr_active_slave);
  830. if (curr_active)
  831. err = dev_set_allmulti(curr_active->dev, inc);
  832. } else {
  833. struct slave *slave;
  834. bond_for_each_slave(bond, slave, iter) {
  835. err = dev_set_allmulti(slave->dev, inc);
  836. if (err)
  837. return err;
  838. }
  839. }
  840. return err;
  841. }
  842. /* Retrieve the list of registered multicast addresses for the bonding
  843. * device and retransmit an IGMP JOIN request to the current active
  844. * slave.
  845. */
  846. static void bond_resend_igmp_join_requests_delayed(struct work_struct *work)
  847. {
  848. struct bonding *bond = container_of(work, struct bonding,
  849. mcast_work.work);
  850. if (!rtnl_trylock()) {
  851. queue_delayed_work(bond->wq, &bond->mcast_work, 1);
  852. return;
  853. }
  854. call_netdevice_notifiers(NETDEV_RESEND_IGMP, bond->dev);
  855. if (bond->igmp_retrans > 1) {
  856. bond->igmp_retrans--;
  857. queue_delayed_work(bond->wq, &bond->mcast_work, HZ/5);
  858. }
  859. rtnl_unlock();
  860. }
  861. /* Flush bond's hardware addresses from slave */
  862. static void bond_hw_addr_flush(struct net_device *bond_dev,
  863. struct net_device *slave_dev)
  864. {
  865. struct bonding *bond = netdev_priv(bond_dev);
  866. dev_uc_unsync(slave_dev, bond_dev);
  867. dev_mc_unsync(slave_dev, bond_dev);
  868. if (BOND_MODE(bond) == BOND_MODE_8023AD)
  869. dev_mc_del(slave_dev, lacpdu_mcast_addr);
  870. }
  871. /*--------------------------- Active slave change ---------------------------*/
  872. /* Update the hardware address list and promisc/allmulti for the new and
  873. * old active slaves (if any). Modes that are not using primary keep all
  874. * slaves up date at all times; only the modes that use primary need to call
  875. * this function to swap these settings during a failover.
  876. */
  877. static void bond_hw_addr_swap(struct bonding *bond, struct slave *new_active,
  878. struct slave *old_active)
  879. {
  880. if (old_active) {
  881. if (bond->dev->flags & IFF_PROMISC)
  882. dev_set_promiscuity(old_active->dev, -1);
  883. if (bond->dev->flags & IFF_ALLMULTI)
  884. dev_set_allmulti(old_active->dev, -1);
  885. if (bond->dev->flags & IFF_UP)
  886. bond_hw_addr_flush(bond->dev, old_active->dev);
  887. bond_slave_ns_maddrs_add(bond, old_active);
  888. }
  889. if (new_active) {
  890. /* FIXME: Signal errors upstream. */
  891. if (bond->dev->flags & IFF_PROMISC)
  892. dev_set_promiscuity(new_active->dev, 1);
  893. if (bond->dev->flags & IFF_ALLMULTI)
  894. dev_set_allmulti(new_active->dev, 1);
  895. if (bond->dev->flags & IFF_UP) {
  896. netif_addr_lock_bh(bond->dev);
  897. dev_uc_sync(new_active->dev, bond->dev);
  898. dev_mc_sync(new_active->dev, bond->dev);
  899. netif_addr_unlock_bh(bond->dev);
  900. }
  901. bond_slave_ns_maddrs_del(bond, new_active);
  902. }
  903. }
  904. /**
  905. * bond_set_dev_addr - clone slave's address to bond
  906. * @bond_dev: bond net device
  907. * @slave_dev: slave net device
  908. *
  909. * Should be called with RTNL held.
  910. */
  911. static int bond_set_dev_addr(struct net_device *bond_dev,
  912. struct net_device *slave_dev)
  913. {
  914. int err;
  915. slave_dbg(bond_dev, slave_dev, "bond_dev=%p slave_dev=%p slave_dev->addr_len=%d\n",
  916. bond_dev, slave_dev, slave_dev->addr_len);
  917. err = dev_pre_changeaddr_notify(bond_dev, slave_dev->dev_addr, NULL);
  918. if (err)
  919. return err;
  920. __dev_addr_set(bond_dev, slave_dev->dev_addr, slave_dev->addr_len);
  921. bond_dev->addr_assign_type = NET_ADDR_STOLEN;
  922. call_netdevice_notifiers(NETDEV_CHANGEADDR, bond_dev);
  923. return 0;
  924. }
  925. static struct slave *bond_get_old_active(struct bonding *bond,
  926. struct slave *new_active)
  927. {
  928. struct slave *slave;
  929. struct list_head *iter;
  930. bond_for_each_slave(bond, slave, iter) {
  931. if (slave == new_active)
  932. continue;
  933. if (ether_addr_equal(bond->dev->dev_addr, slave->dev->dev_addr))
  934. return slave;
  935. }
  936. return NULL;
  937. }
  938. /* bond_do_fail_over_mac
  939. *
  940. * Perform special MAC address swapping for fail_over_mac settings
  941. *
  942. * Called with RTNL
  943. */
  944. static void bond_do_fail_over_mac(struct bonding *bond,
  945. struct slave *new_active,
  946. struct slave *old_active)
  947. {
  948. u8 tmp_mac[MAX_ADDR_LEN];
  949. struct sockaddr_storage ss;
  950. int rv;
  951. switch (bond->params.fail_over_mac) {
  952. case BOND_FOM_ACTIVE:
  953. if (new_active) {
  954. rv = bond_set_dev_addr(bond->dev, new_active->dev);
  955. if (rv)
  956. slave_err(bond->dev, new_active->dev, "Error %d setting bond MAC from slave\n",
  957. -rv);
  958. }
  959. break;
  960. case BOND_FOM_FOLLOW:
  961. /* if new_active && old_active, swap them
  962. * if just old_active, do nothing (going to no active slave)
  963. * if just new_active, set new_active to bond's MAC
  964. */
  965. if (!new_active)
  966. return;
  967. if (!old_active)
  968. old_active = bond_get_old_active(bond, new_active);
  969. if (old_active) {
  970. bond_hw_addr_copy(tmp_mac, new_active->dev->dev_addr,
  971. new_active->dev->addr_len);
  972. bond_hw_addr_copy(ss.__data,
  973. old_active->dev->dev_addr,
  974. old_active->dev->addr_len);
  975. ss.ss_family = new_active->dev->type;
  976. } else {
  977. bond_hw_addr_copy(ss.__data, bond->dev->dev_addr,
  978. bond->dev->addr_len);
  979. ss.ss_family = bond->dev->type;
  980. }
  981. rv = dev_set_mac_address(new_active->dev,
  982. (struct sockaddr *)&ss, NULL);
  983. if (rv) {
  984. slave_err(bond->dev, new_active->dev, "Error %d setting MAC of new active slave\n",
  985. -rv);
  986. goto out;
  987. }
  988. if (!old_active)
  989. goto out;
  990. bond_hw_addr_copy(ss.__data, tmp_mac,
  991. new_active->dev->addr_len);
  992. ss.ss_family = old_active->dev->type;
  993. rv = dev_set_mac_address(old_active->dev,
  994. (struct sockaddr *)&ss, NULL);
  995. if (rv)
  996. slave_err(bond->dev, old_active->dev, "Error %d setting MAC of old active slave\n",
  997. -rv);
  998. out:
  999. break;
  1000. default:
  1001. netdev_err(bond->dev, "bond_do_fail_over_mac impossible: bad policy %d\n",
  1002. bond->params.fail_over_mac);
  1003. break;
  1004. }
  1005. }
  1006. /**
  1007. * bond_choose_primary_or_current - select the primary or high priority slave
  1008. * @bond: our bonding struct
  1009. *
  1010. * - Check if there is a primary link. If the primary link was set and is up,
  1011. * go on and do link reselection.
  1012. *
  1013. * - If primary link is not set or down, find the highest priority link.
  1014. * If the highest priority link is not current slave, set it as primary
  1015. * link and do link reselection.
  1016. */
  1017. static struct slave *bond_choose_primary_or_current(struct bonding *bond)
  1018. {
  1019. struct slave *prim = rtnl_dereference(bond->primary_slave);
  1020. struct slave *curr = rtnl_dereference(bond->curr_active_slave);
  1021. struct slave *slave, *hprio = NULL;
  1022. struct list_head *iter;
  1023. if (!prim || prim->link != BOND_LINK_UP) {
  1024. bond_for_each_slave(bond, slave, iter) {
  1025. if (slave->link == BOND_LINK_UP) {
  1026. hprio = hprio ?: slave;
  1027. if (slave->prio > hprio->prio)
  1028. hprio = slave;
  1029. }
  1030. }
  1031. if (hprio && hprio != curr) {
  1032. prim = hprio;
  1033. goto link_reselect;
  1034. }
  1035. if (!curr || curr->link != BOND_LINK_UP)
  1036. return NULL;
  1037. return curr;
  1038. }
  1039. if (bond->force_primary) {
  1040. bond->force_primary = false;
  1041. return prim;
  1042. }
  1043. link_reselect:
  1044. if (!curr || curr->link != BOND_LINK_UP)
  1045. return prim;
  1046. /* At this point, prim and curr are both up */
  1047. switch (bond->params.primary_reselect) {
  1048. case BOND_PRI_RESELECT_ALWAYS:
  1049. return prim;
  1050. case BOND_PRI_RESELECT_BETTER:
  1051. if (prim->speed < curr->speed)
  1052. return curr;
  1053. if (prim->speed == curr->speed && prim->duplex <= curr->duplex)
  1054. return curr;
  1055. return prim;
  1056. case BOND_PRI_RESELECT_FAILURE:
  1057. return curr;
  1058. default:
  1059. netdev_err(bond->dev, "impossible primary_reselect %d\n",
  1060. bond->params.primary_reselect);
  1061. return curr;
  1062. }
  1063. }
  1064. /**
  1065. * bond_find_best_slave - select the best available slave to be the active one
  1066. * @bond: our bonding struct
  1067. */
  1068. static struct slave *bond_find_best_slave(struct bonding *bond)
  1069. {
  1070. struct slave *slave, *bestslave = NULL;
  1071. struct list_head *iter;
  1072. int mintime = bond->params.updelay;
  1073. slave = bond_choose_primary_or_current(bond);
  1074. if (slave)
  1075. return slave;
  1076. bond_for_each_slave(bond, slave, iter) {
  1077. if (slave->link == BOND_LINK_UP)
  1078. return slave;
  1079. if (slave->link == BOND_LINK_BACK && bond_slave_is_up(slave) &&
  1080. slave->delay < mintime) {
  1081. mintime = slave->delay;
  1082. bestslave = slave;
  1083. }
  1084. }
  1085. return bestslave;
  1086. }
  1087. /* must be called in RCU critical section or with RTNL held */
  1088. static bool bond_should_notify_peers(struct bonding *bond)
  1089. {
  1090. struct slave *slave = rcu_dereference_rtnl(bond->curr_active_slave);
  1091. if (!slave || !bond->send_peer_notif ||
  1092. bond->send_peer_notif %
  1093. max(1, bond->params.peer_notif_delay) != 0 ||
  1094. !netif_carrier_ok(bond->dev) ||
  1095. test_bit(__LINK_STATE_LINKWATCH_PENDING, &slave->dev->state))
  1096. return false;
  1097. netdev_dbg(bond->dev, "bond_should_notify_peers: slave %s\n",
  1098. slave ? slave->dev->name : "NULL");
  1099. return true;
  1100. }
  1101. /**
  1102. * bond_change_active_slave - change the active slave into the specified one
  1103. * @bond: our bonding struct
  1104. * @new_active: the new slave to make the active one
  1105. *
  1106. * Set the new slave to the bond's settings and unset them on the old
  1107. * curr_active_slave.
  1108. * Setting include flags, mc-list, promiscuity, allmulti, etc.
  1109. *
  1110. * If @new's link state is %BOND_LINK_BACK we'll set it to %BOND_LINK_UP,
  1111. * because it is apparently the best available slave we have, even though its
  1112. * updelay hasn't timed out yet.
  1113. *
  1114. * Caller must hold RTNL.
  1115. */
  1116. void bond_change_active_slave(struct bonding *bond, struct slave *new_active)
  1117. {
  1118. struct slave *old_active;
  1119. ASSERT_RTNL();
  1120. old_active = rtnl_dereference(bond->curr_active_slave);
  1121. if (old_active == new_active)
  1122. return;
  1123. #ifdef CONFIG_XFRM_OFFLOAD
  1124. bond_ipsec_del_sa_all(bond);
  1125. #endif /* CONFIG_XFRM_OFFLOAD */
  1126. if (new_active) {
  1127. new_active->last_link_up = jiffies;
  1128. if (new_active->link == BOND_LINK_BACK) {
  1129. if (bond_uses_primary(bond)) {
  1130. slave_info(bond->dev, new_active->dev, "making interface the new active one %d ms earlier\n",
  1131. (bond->params.updelay - new_active->delay) * bond->params.miimon);
  1132. }
  1133. new_active->delay = 0;
  1134. bond_set_slave_link_state(new_active, BOND_LINK_UP,
  1135. BOND_SLAVE_NOTIFY_NOW);
  1136. if (BOND_MODE(bond) == BOND_MODE_8023AD)
  1137. bond_3ad_handle_link_change(new_active, BOND_LINK_UP);
  1138. if (bond_is_lb(bond))
  1139. bond_alb_handle_link_change(bond, new_active, BOND_LINK_UP);
  1140. } else {
  1141. if (bond_uses_primary(bond))
  1142. slave_info(bond->dev, new_active->dev, "making interface the new active one\n");
  1143. }
  1144. }
  1145. if (bond_uses_primary(bond))
  1146. bond_hw_addr_swap(bond, new_active, old_active);
  1147. if (bond_is_lb(bond)) {
  1148. bond_alb_handle_active_change(bond, new_active);
  1149. if (old_active)
  1150. bond_set_slave_inactive_flags(old_active,
  1151. BOND_SLAVE_NOTIFY_NOW);
  1152. if (new_active)
  1153. bond_set_slave_active_flags(new_active,
  1154. BOND_SLAVE_NOTIFY_NOW);
  1155. } else {
  1156. rcu_assign_pointer(bond->curr_active_slave, new_active);
  1157. }
  1158. if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP) {
  1159. if (old_active)
  1160. bond_set_slave_inactive_flags(old_active,
  1161. BOND_SLAVE_NOTIFY_NOW);
  1162. if (new_active) {
  1163. bool should_notify_peers = false;
  1164. bond_set_slave_active_flags(new_active,
  1165. BOND_SLAVE_NOTIFY_NOW);
  1166. if (bond->params.fail_over_mac)
  1167. bond_do_fail_over_mac(bond, new_active,
  1168. old_active);
  1169. if (netif_running(bond->dev)) {
  1170. bond->send_peer_notif =
  1171. bond->params.num_peer_notif *
  1172. max(1, bond->params.peer_notif_delay);
  1173. should_notify_peers =
  1174. bond_should_notify_peers(bond);
  1175. }
  1176. call_netdevice_notifiers(NETDEV_BONDING_FAILOVER, bond->dev);
  1177. if (should_notify_peers) {
  1178. bond->send_peer_notif--;
  1179. call_netdevice_notifiers(NETDEV_NOTIFY_PEERS,
  1180. bond->dev);
  1181. }
  1182. }
  1183. }
  1184. #ifdef CONFIG_XFRM_OFFLOAD
  1185. bond_ipsec_add_sa_all(bond);
  1186. #endif /* CONFIG_XFRM_OFFLOAD */
  1187. /* resend IGMP joins since active slave has changed or
  1188. * all were sent on curr_active_slave.
  1189. * resend only if bond is brought up with the affected
  1190. * bonding modes and the retransmission is enabled
  1191. */
  1192. if (netif_running(bond->dev) && (bond->params.resend_igmp > 0) &&
  1193. ((bond_uses_primary(bond) && new_active) ||
  1194. BOND_MODE(bond) == BOND_MODE_ROUNDROBIN)) {
  1195. bond->igmp_retrans = bond->params.resend_igmp;
  1196. queue_delayed_work(bond->wq, &bond->mcast_work, 1);
  1197. }
  1198. }
  1199. /**
  1200. * bond_select_active_slave - select a new active slave, if needed
  1201. * @bond: our bonding struct
  1202. *
  1203. * This functions should be called when one of the following occurs:
  1204. * - The old curr_active_slave has been released or lost its link.
  1205. * - The primary_slave has got its link back.
  1206. * - A slave has got its link back and there's no old curr_active_slave.
  1207. *
  1208. * Caller must hold RTNL.
  1209. */
  1210. void bond_select_active_slave(struct bonding *bond)
  1211. {
  1212. struct slave *best_slave;
  1213. int rv;
  1214. ASSERT_RTNL();
  1215. best_slave = bond_find_best_slave(bond);
  1216. if (best_slave != rtnl_dereference(bond->curr_active_slave)) {
  1217. bond_change_active_slave(bond, best_slave);
  1218. rv = bond_set_carrier(bond);
  1219. if (!rv)
  1220. return;
  1221. if (netif_carrier_ok(bond->dev))
  1222. netdev_info(bond->dev, "active interface up!\n");
  1223. else
  1224. netdev_info(bond->dev, "now running without any active interface!\n");
  1225. }
  1226. }
  1227. #ifdef CONFIG_NET_POLL_CONTROLLER
  1228. static inline int slave_enable_netpoll(struct slave *slave)
  1229. {
  1230. struct netpoll *np;
  1231. int err = 0;
  1232. np = kzalloc(sizeof(*np), GFP_KERNEL);
  1233. err = -ENOMEM;
  1234. if (!np)
  1235. goto out;
  1236. err = __netpoll_setup(np, slave->dev);
  1237. if (err) {
  1238. kfree(np);
  1239. goto out;
  1240. }
  1241. slave->np = np;
  1242. out:
  1243. return err;
  1244. }
  1245. static inline void slave_disable_netpoll(struct slave *slave)
  1246. {
  1247. struct netpoll *np = slave->np;
  1248. if (!np)
  1249. return;
  1250. slave->np = NULL;
  1251. __netpoll_free(np);
  1252. }
  1253. static void bond_poll_controller(struct net_device *bond_dev)
  1254. {
  1255. struct bonding *bond = netdev_priv(bond_dev);
  1256. struct slave *slave = NULL;
  1257. struct list_head *iter;
  1258. struct ad_info ad_info;
  1259. if (BOND_MODE(bond) == BOND_MODE_8023AD)
  1260. if (bond_3ad_get_active_agg_info(bond, &ad_info))
  1261. return;
  1262. bond_for_each_slave_rcu(bond, slave, iter) {
  1263. if (!bond_slave_is_up(slave))
  1264. continue;
  1265. if (BOND_MODE(bond) == BOND_MODE_8023AD) {
  1266. struct aggregator *agg =
  1267. SLAVE_AD_INFO(slave)->port.aggregator;
  1268. if (agg &&
  1269. agg->aggregator_identifier != ad_info.aggregator_id)
  1270. continue;
  1271. }
  1272. netpoll_poll_dev(slave->dev);
  1273. }
  1274. }
  1275. static void bond_netpoll_cleanup(struct net_device *bond_dev)
  1276. {
  1277. struct bonding *bond = netdev_priv(bond_dev);
  1278. struct list_head *iter;
  1279. struct slave *slave;
  1280. bond_for_each_slave(bond, slave, iter)
  1281. if (bond_slave_is_up(slave))
  1282. slave_disable_netpoll(slave);
  1283. }
  1284. static int bond_netpoll_setup(struct net_device *dev, struct netpoll_info *ni)
  1285. {
  1286. struct bonding *bond = netdev_priv(dev);
  1287. struct list_head *iter;
  1288. struct slave *slave;
  1289. int err = 0;
  1290. bond_for_each_slave(bond, slave, iter) {
  1291. err = slave_enable_netpoll(slave);
  1292. if (err) {
  1293. bond_netpoll_cleanup(dev);
  1294. break;
  1295. }
  1296. }
  1297. return err;
  1298. }
  1299. #else
  1300. static inline int slave_enable_netpoll(struct slave *slave)
  1301. {
  1302. return 0;
  1303. }
  1304. static inline void slave_disable_netpoll(struct slave *slave)
  1305. {
  1306. }
  1307. static void bond_netpoll_cleanup(struct net_device *bond_dev)
  1308. {
  1309. }
  1310. #endif
  1311. /*---------------------------------- IOCTL ----------------------------------*/
  1312. static netdev_features_t bond_fix_features(struct net_device *dev,
  1313. netdev_features_t features)
  1314. {
  1315. struct bonding *bond = netdev_priv(dev);
  1316. struct list_head *iter;
  1317. netdev_features_t mask;
  1318. struct slave *slave;
  1319. mask = features;
  1320. features = netdev_base_features(features);
  1321. bond_for_each_slave(bond, slave, iter) {
  1322. features = netdev_increment_features(features,
  1323. slave->dev->features,
  1324. mask);
  1325. }
  1326. features = netdev_add_tso_features(features, mask);
  1327. return features;
  1328. }
  1329. #define BOND_VLAN_FEATURES (NETIF_F_HW_CSUM | NETIF_F_SG | \
  1330. NETIF_F_FRAGLIST | NETIF_F_GSO_SOFTWARE | \
  1331. NETIF_F_GSO_ENCAP_ALL | \
  1332. NETIF_F_HIGHDMA | NETIF_F_LRO)
  1333. #define BOND_ENC_FEATURES (NETIF_F_HW_CSUM | NETIF_F_SG | \
  1334. NETIF_F_RXCSUM | NETIF_F_GSO_SOFTWARE)
  1335. #define BOND_MPLS_FEATURES (NETIF_F_HW_CSUM | NETIF_F_SG | \
  1336. NETIF_F_GSO_SOFTWARE)
  1337. static void bond_compute_features(struct bonding *bond)
  1338. {
  1339. unsigned int dst_release_flag = IFF_XMIT_DST_RELEASE |
  1340. IFF_XMIT_DST_RELEASE_PERM;
  1341. netdev_features_t vlan_features = BOND_VLAN_FEATURES;
  1342. netdev_features_t enc_features = BOND_ENC_FEATURES;
  1343. #ifdef CONFIG_XFRM_OFFLOAD
  1344. netdev_features_t xfrm_features = BOND_XFRM_FEATURES;
  1345. #endif /* CONFIG_XFRM_OFFLOAD */
  1346. netdev_features_t mpls_features = BOND_MPLS_FEATURES;
  1347. struct net_device *bond_dev = bond->dev;
  1348. struct list_head *iter;
  1349. struct slave *slave;
  1350. unsigned short max_hard_header_len = ETH_HLEN;
  1351. unsigned int tso_max_size = TSO_MAX_SIZE;
  1352. u16 tso_max_segs = TSO_MAX_SEGS;
  1353. if (!bond_has_slaves(bond))
  1354. goto done;
  1355. vlan_features = netdev_base_features(vlan_features);
  1356. mpls_features = netdev_base_features(mpls_features);
  1357. bond_for_each_slave(bond, slave, iter) {
  1358. vlan_features = netdev_increment_features(vlan_features,
  1359. slave->dev->vlan_features, BOND_VLAN_FEATURES);
  1360. enc_features = netdev_increment_features(enc_features,
  1361. slave->dev->hw_enc_features,
  1362. BOND_ENC_FEATURES);
  1363. #ifdef CONFIG_XFRM_OFFLOAD
  1364. xfrm_features = netdev_increment_features(xfrm_features,
  1365. slave->dev->hw_enc_features,
  1366. BOND_XFRM_FEATURES);
  1367. #endif /* CONFIG_XFRM_OFFLOAD */
  1368. mpls_features = netdev_increment_features(mpls_features,
  1369. slave->dev->mpls_features,
  1370. BOND_MPLS_FEATURES);
  1371. dst_release_flag &= slave->dev->priv_flags;
  1372. if (slave->dev->hard_header_len > max_hard_header_len)
  1373. max_hard_header_len = slave->dev->hard_header_len;
  1374. tso_max_size = min(tso_max_size, slave->dev->tso_max_size);
  1375. tso_max_segs = min(tso_max_segs, slave->dev->tso_max_segs);
  1376. }
  1377. bond_dev->hard_header_len = max_hard_header_len;
  1378. done:
  1379. bond_dev->vlan_features = vlan_features;
  1380. bond_dev->hw_enc_features = enc_features | NETIF_F_GSO_ENCAP_ALL |
  1381. NETIF_F_HW_VLAN_CTAG_TX |
  1382. NETIF_F_HW_VLAN_STAG_TX;
  1383. #ifdef CONFIG_XFRM_OFFLOAD
  1384. bond_dev->hw_enc_features |= xfrm_features;
  1385. #endif /* CONFIG_XFRM_OFFLOAD */
  1386. bond_dev->mpls_features = mpls_features;
  1387. netif_set_tso_max_segs(bond_dev, tso_max_segs);
  1388. netif_set_tso_max_size(bond_dev, tso_max_size);
  1389. bond_dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  1390. if ((bond_dev->priv_flags & IFF_XMIT_DST_RELEASE_PERM) &&
  1391. dst_release_flag == (IFF_XMIT_DST_RELEASE | IFF_XMIT_DST_RELEASE_PERM))
  1392. bond_dev->priv_flags |= IFF_XMIT_DST_RELEASE;
  1393. netdev_change_features(bond_dev);
  1394. }
  1395. static void bond_setup_by_slave(struct net_device *bond_dev,
  1396. struct net_device *slave_dev)
  1397. {
  1398. bool was_up = !!(bond_dev->flags & IFF_UP);
  1399. dev_close(bond_dev);
  1400. bond_dev->header_ops = slave_dev->header_ops;
  1401. bond_dev->type = slave_dev->type;
  1402. bond_dev->hard_header_len = slave_dev->hard_header_len;
  1403. bond_dev->needed_headroom = slave_dev->needed_headroom;
  1404. bond_dev->addr_len = slave_dev->addr_len;
  1405. memcpy(bond_dev->broadcast, slave_dev->broadcast,
  1406. slave_dev->addr_len);
  1407. if (slave_dev->flags & IFF_POINTOPOINT) {
  1408. bond_dev->flags &= ~(IFF_BROADCAST | IFF_MULTICAST);
  1409. bond_dev->flags |= (IFF_POINTOPOINT | IFF_NOARP);
  1410. }
  1411. if (was_up)
  1412. dev_open(bond_dev, NULL);
  1413. }
  1414. /* On bonding slaves other than the currently active slave, suppress
  1415. * duplicates except for alb non-mcast/bcast.
  1416. */
  1417. static bool bond_should_deliver_exact_match(struct sk_buff *skb,
  1418. struct slave *slave,
  1419. struct bonding *bond)
  1420. {
  1421. if (bond_is_slave_inactive(slave)) {
  1422. if (BOND_MODE(bond) == BOND_MODE_ALB &&
  1423. skb->pkt_type != PACKET_BROADCAST &&
  1424. skb->pkt_type != PACKET_MULTICAST)
  1425. return false;
  1426. return true;
  1427. }
  1428. return false;
  1429. }
  1430. static rx_handler_result_t bond_handle_frame(struct sk_buff **pskb)
  1431. {
  1432. struct sk_buff *skb = *pskb;
  1433. struct slave *slave;
  1434. struct bonding *bond;
  1435. int (*recv_probe)(const struct sk_buff *, struct bonding *,
  1436. struct slave *);
  1437. int ret = RX_HANDLER_ANOTHER;
  1438. skb = skb_share_check(skb, GFP_ATOMIC);
  1439. if (unlikely(!skb))
  1440. return RX_HANDLER_CONSUMED;
  1441. *pskb = skb;
  1442. slave = bond_slave_get_rcu(skb->dev);
  1443. bond = slave->bond;
  1444. recv_probe = READ_ONCE(bond->recv_probe);
  1445. if (recv_probe) {
  1446. ret = recv_probe(skb, bond, slave);
  1447. if (ret == RX_HANDLER_CONSUMED) {
  1448. consume_skb(skb);
  1449. return ret;
  1450. }
  1451. }
  1452. /*
  1453. * For packets determined by bond_should_deliver_exact_match() call to
  1454. * be suppressed we want to make an exception for link-local packets.
  1455. * This is necessary for e.g. LLDP daemons to be able to monitor
  1456. * inactive slave links without being forced to bind to them
  1457. * explicitly.
  1458. *
  1459. * At the same time, packets that are passed to the bonding master
  1460. * (including link-local ones) can have their originating interface
  1461. * determined via PACKET_ORIGDEV socket option.
  1462. */
  1463. if (bond_should_deliver_exact_match(skb, slave, bond)) {
  1464. if (is_link_local_ether_addr(eth_hdr(skb)->h_dest))
  1465. return RX_HANDLER_PASS;
  1466. return RX_HANDLER_EXACT;
  1467. }
  1468. skb->dev = bond->dev;
  1469. if (BOND_MODE(bond) == BOND_MODE_ALB &&
  1470. netif_is_bridge_port(bond->dev) &&
  1471. skb->pkt_type == PACKET_HOST) {
  1472. if (unlikely(skb_cow_head(skb,
  1473. skb->data - skb_mac_header(skb)))) {
  1474. kfree_skb(skb);
  1475. return RX_HANDLER_CONSUMED;
  1476. }
  1477. bond_hw_addr_copy(eth_hdr(skb)->h_dest, bond->dev->dev_addr,
  1478. bond->dev->addr_len);
  1479. }
  1480. return ret;
  1481. }
  1482. static enum netdev_lag_tx_type bond_lag_tx_type(struct bonding *bond)
  1483. {
  1484. switch (BOND_MODE(bond)) {
  1485. case BOND_MODE_ROUNDROBIN:
  1486. return NETDEV_LAG_TX_TYPE_ROUNDROBIN;
  1487. case BOND_MODE_ACTIVEBACKUP:
  1488. return NETDEV_LAG_TX_TYPE_ACTIVEBACKUP;
  1489. case BOND_MODE_BROADCAST:
  1490. return NETDEV_LAG_TX_TYPE_BROADCAST;
  1491. case BOND_MODE_XOR:
  1492. case BOND_MODE_8023AD:
  1493. return NETDEV_LAG_TX_TYPE_HASH;
  1494. default:
  1495. return NETDEV_LAG_TX_TYPE_UNKNOWN;
  1496. }
  1497. }
  1498. static enum netdev_lag_hash bond_lag_hash_type(struct bonding *bond,
  1499. enum netdev_lag_tx_type type)
  1500. {
  1501. if (type != NETDEV_LAG_TX_TYPE_HASH)
  1502. return NETDEV_LAG_HASH_NONE;
  1503. switch (bond->params.xmit_policy) {
  1504. case BOND_XMIT_POLICY_LAYER2:
  1505. return NETDEV_LAG_HASH_L2;
  1506. case BOND_XMIT_POLICY_LAYER34:
  1507. return NETDEV_LAG_HASH_L34;
  1508. case BOND_XMIT_POLICY_LAYER23:
  1509. return NETDEV_LAG_HASH_L23;
  1510. case BOND_XMIT_POLICY_ENCAP23:
  1511. return NETDEV_LAG_HASH_E23;
  1512. case BOND_XMIT_POLICY_ENCAP34:
  1513. return NETDEV_LAG_HASH_E34;
  1514. case BOND_XMIT_POLICY_VLAN_SRCMAC:
  1515. return NETDEV_LAG_HASH_VLAN_SRCMAC;
  1516. default:
  1517. return NETDEV_LAG_HASH_UNKNOWN;
  1518. }
  1519. }
  1520. static int bond_master_upper_dev_link(struct bonding *bond, struct slave *slave,
  1521. struct netlink_ext_ack *extack)
  1522. {
  1523. struct netdev_lag_upper_info lag_upper_info;
  1524. enum netdev_lag_tx_type type;
  1525. int err;
  1526. type = bond_lag_tx_type(bond);
  1527. lag_upper_info.tx_type = type;
  1528. lag_upper_info.hash_type = bond_lag_hash_type(bond, type);
  1529. err = netdev_master_upper_dev_link(slave->dev, bond->dev, slave,
  1530. &lag_upper_info, extack);
  1531. if (err)
  1532. return err;
  1533. slave->dev->flags |= IFF_SLAVE;
  1534. return 0;
  1535. }
  1536. static void bond_upper_dev_unlink(struct bonding *bond, struct slave *slave)
  1537. {
  1538. netdev_upper_dev_unlink(slave->dev, bond->dev);
  1539. slave->dev->flags &= ~IFF_SLAVE;
  1540. }
  1541. static void slave_kobj_release(struct kobject *kobj)
  1542. {
  1543. struct slave *slave = to_slave(kobj);
  1544. struct bonding *bond = bond_get_bond_by_slave(slave);
  1545. cancel_delayed_work_sync(&slave->notify_work);
  1546. if (BOND_MODE(bond) == BOND_MODE_8023AD)
  1547. kfree(SLAVE_AD_INFO(slave));
  1548. kfree(slave);
  1549. }
  1550. static struct kobj_type slave_ktype = {
  1551. .release = slave_kobj_release,
  1552. #ifdef CONFIG_SYSFS
  1553. .sysfs_ops = &slave_sysfs_ops,
  1554. #endif
  1555. };
  1556. static int bond_kobj_init(struct slave *slave)
  1557. {
  1558. int err;
  1559. err = kobject_init_and_add(&slave->kobj, &slave_ktype,
  1560. &(slave->dev->dev.kobj), "bonding_slave");
  1561. if (err)
  1562. kobject_put(&slave->kobj);
  1563. return err;
  1564. }
  1565. static struct slave *bond_alloc_slave(struct bonding *bond,
  1566. struct net_device *slave_dev)
  1567. {
  1568. struct slave *slave = NULL;
  1569. slave = kzalloc(sizeof(*slave), GFP_KERNEL);
  1570. if (!slave)
  1571. return NULL;
  1572. slave->bond = bond;
  1573. slave->dev = slave_dev;
  1574. INIT_DELAYED_WORK(&slave->notify_work, bond_netdev_notify_work);
  1575. if (bond_kobj_init(slave))
  1576. return NULL;
  1577. if (BOND_MODE(bond) == BOND_MODE_8023AD) {
  1578. SLAVE_AD_INFO(slave) = kzalloc(sizeof(struct ad_slave_info),
  1579. GFP_KERNEL);
  1580. if (!SLAVE_AD_INFO(slave)) {
  1581. kobject_put(&slave->kobj);
  1582. return NULL;
  1583. }
  1584. }
  1585. return slave;
  1586. }
  1587. static void bond_fill_ifbond(struct bonding *bond, struct ifbond *info)
  1588. {
  1589. info->bond_mode = BOND_MODE(bond);
  1590. info->miimon = bond->params.miimon;
  1591. info->num_slaves = bond->slave_cnt;
  1592. }
  1593. static void bond_fill_ifslave(struct slave *slave, struct ifslave *info)
  1594. {
  1595. strcpy(info->slave_name, slave->dev->name);
  1596. info->link = slave->link;
  1597. info->state = bond_slave_state(slave);
  1598. info->link_failure_count = slave->link_failure_count;
  1599. }
  1600. static void bond_netdev_notify_work(struct work_struct *_work)
  1601. {
  1602. struct slave *slave = container_of(_work, struct slave,
  1603. notify_work.work);
  1604. if (rtnl_trylock()) {
  1605. struct netdev_bonding_info binfo;
  1606. bond_fill_ifslave(slave, &binfo.slave);
  1607. bond_fill_ifbond(slave->bond, &binfo.master);
  1608. netdev_bonding_info_change(slave->dev, &binfo);
  1609. rtnl_unlock();
  1610. } else {
  1611. queue_delayed_work(slave->bond->wq, &slave->notify_work, 1);
  1612. }
  1613. }
  1614. void bond_queue_slave_event(struct slave *slave)
  1615. {
  1616. queue_delayed_work(slave->bond->wq, &slave->notify_work, 0);
  1617. }
  1618. void bond_lower_state_changed(struct slave *slave)
  1619. {
  1620. struct netdev_lag_lower_state_info info;
  1621. info.link_up = slave->link == BOND_LINK_UP ||
  1622. slave->link == BOND_LINK_FAIL;
  1623. info.tx_enabled = bond_is_active_slave(slave);
  1624. netdev_lower_state_changed(slave->dev, &info);
  1625. }
  1626. #define BOND_NL_ERR(bond_dev, extack, errmsg) do { \
  1627. if (extack) \
  1628. NL_SET_ERR_MSG(extack, errmsg); \
  1629. else \
  1630. netdev_err(bond_dev, "Error: %s\n", errmsg); \
  1631. } while (0)
  1632. #define SLAVE_NL_ERR(bond_dev, slave_dev, extack, errmsg) do { \
  1633. if (extack) \
  1634. NL_SET_ERR_MSG(extack, errmsg); \
  1635. else \
  1636. slave_err(bond_dev, slave_dev, "Error: %s\n", errmsg); \
  1637. } while (0)
  1638. /* The bonding driver uses ether_setup() to convert a master bond device
  1639. * to ARPHRD_ETHER, that resets the target netdevice's flags so we always
  1640. * have to restore the IFF_MASTER flag, and only restore IFF_SLAVE and IFF_UP
  1641. * if they were set
  1642. */
  1643. static void bond_ether_setup(struct net_device *bond_dev)
  1644. {
  1645. unsigned int flags = bond_dev->flags & (IFF_SLAVE | IFF_UP);
  1646. ether_setup(bond_dev);
  1647. bond_dev->flags |= IFF_MASTER | flags;
  1648. bond_dev->priv_flags &= ~IFF_TX_SKB_SHARING;
  1649. }
  1650. void bond_xdp_set_features(struct net_device *bond_dev)
  1651. {
  1652. struct bonding *bond = netdev_priv(bond_dev);
  1653. xdp_features_t val = NETDEV_XDP_ACT_MASK;
  1654. struct list_head *iter;
  1655. struct slave *slave;
  1656. ASSERT_RTNL();
  1657. if (!bond_xdp_check(bond) || !bond_has_slaves(bond)) {
  1658. xdp_clear_features_flag(bond_dev);
  1659. return;
  1660. }
  1661. bond_for_each_slave(bond, slave, iter)
  1662. val &= slave->dev->xdp_features;
  1663. val &= ~NETDEV_XDP_ACT_XSK_ZEROCOPY;
  1664. xdp_set_features_flag(bond_dev, val);
  1665. }
  1666. /* enslave device <slave> to bond device <master> */
  1667. int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev,
  1668. struct netlink_ext_ack *extack)
  1669. {
  1670. struct bonding *bond = netdev_priv(bond_dev);
  1671. const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
  1672. struct slave *new_slave = NULL, *prev_slave;
  1673. struct sockaddr_storage ss;
  1674. int link_reporting;
  1675. int res = 0, i;
  1676. if (slave_dev->flags & IFF_MASTER &&
  1677. !netif_is_bond_master(slave_dev)) {
  1678. BOND_NL_ERR(bond_dev, extack,
  1679. "Device type (master device) cannot be enslaved");
  1680. return -EPERM;
  1681. }
  1682. if (!bond->params.use_carrier &&
  1683. slave_dev->ethtool_ops->get_link == NULL &&
  1684. slave_ops->ndo_eth_ioctl == NULL) {
  1685. slave_warn(bond_dev, slave_dev, "no link monitoring support\n");
  1686. }
  1687. /* already in-use? */
  1688. if (netdev_is_rx_handler_busy(slave_dev)) {
  1689. SLAVE_NL_ERR(bond_dev, slave_dev, extack,
  1690. "Device is in use and cannot be enslaved");
  1691. return -EBUSY;
  1692. }
  1693. if (bond_dev == slave_dev) {
  1694. BOND_NL_ERR(bond_dev, extack, "Cannot enslave bond to itself.");
  1695. return -EPERM;
  1696. }
  1697. /* vlan challenged mutual exclusion */
  1698. /* no need to lock since we're protected by rtnl_lock */
  1699. if (slave_dev->features & NETIF_F_VLAN_CHALLENGED) {
  1700. slave_dbg(bond_dev, slave_dev, "is NETIF_F_VLAN_CHALLENGED\n");
  1701. if (vlan_uses_dev(bond_dev)) {
  1702. SLAVE_NL_ERR(bond_dev, slave_dev, extack,
  1703. "Can not enslave VLAN challenged device to VLAN enabled bond");
  1704. return -EPERM;
  1705. } else {
  1706. slave_warn(bond_dev, slave_dev, "enslaved VLAN challenged slave. Adding VLANs will be blocked as long as it is part of bond.\n");
  1707. }
  1708. } else {
  1709. slave_dbg(bond_dev, slave_dev, "is !NETIF_F_VLAN_CHALLENGED\n");
  1710. }
  1711. if (slave_dev->features & NETIF_F_HW_ESP)
  1712. slave_dbg(bond_dev, slave_dev, "is esp-hw-offload capable\n");
  1713. /* Old ifenslave binaries are no longer supported. These can
  1714. * be identified with moderate accuracy by the state of the slave:
  1715. * the current ifenslave will set the interface down prior to
  1716. * enslaving it; the old ifenslave will not.
  1717. */
  1718. if (slave_dev->flags & IFF_UP) {
  1719. SLAVE_NL_ERR(bond_dev, slave_dev, extack,
  1720. "Device can not be enslaved while up");
  1721. return -EPERM;
  1722. }
  1723. /* set bonding device ether type by slave - bonding netdevices are
  1724. * created with ether_setup, so when the slave type is not ARPHRD_ETHER
  1725. * there is a need to override some of the type dependent attribs/funcs.
  1726. *
  1727. * bond ether type mutual exclusion - don't allow slaves of dissimilar
  1728. * ether type (eg ARPHRD_ETHER and ARPHRD_INFINIBAND) share the same bond
  1729. */
  1730. if (!bond_has_slaves(bond)) {
  1731. if (bond_dev->type != slave_dev->type) {
  1732. slave_dbg(bond_dev, slave_dev, "change device type from %d to %d\n",
  1733. bond_dev->type, slave_dev->type);
  1734. res = call_netdevice_notifiers(NETDEV_PRE_TYPE_CHANGE,
  1735. bond_dev);
  1736. res = notifier_to_errno(res);
  1737. if (res) {
  1738. slave_err(bond_dev, slave_dev, "refused to change device type\n");
  1739. return -EBUSY;
  1740. }
  1741. /* Flush unicast and multicast addresses */
  1742. dev_uc_flush(bond_dev);
  1743. dev_mc_flush(bond_dev);
  1744. if (slave_dev->type != ARPHRD_ETHER)
  1745. bond_setup_by_slave(bond_dev, slave_dev);
  1746. else
  1747. bond_ether_setup(bond_dev);
  1748. call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE,
  1749. bond_dev);
  1750. }
  1751. } else if (bond_dev->type != slave_dev->type) {
  1752. SLAVE_NL_ERR(bond_dev, slave_dev, extack,
  1753. "Device type is different from other slaves");
  1754. return -EINVAL;
  1755. }
  1756. if (slave_dev->type == ARPHRD_INFINIBAND &&
  1757. BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) {
  1758. SLAVE_NL_ERR(bond_dev, slave_dev, extack,
  1759. "Only active-backup mode is supported for infiniband slaves");
  1760. res = -EOPNOTSUPP;
  1761. goto err_undo_flags;
  1762. }
  1763. if (!slave_ops->ndo_set_mac_address ||
  1764. slave_dev->type == ARPHRD_INFINIBAND) {
  1765. slave_warn(bond_dev, slave_dev, "The slave device specified does not support setting the MAC address\n");
  1766. if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP &&
  1767. bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
  1768. if (!bond_has_slaves(bond)) {
  1769. bond->params.fail_over_mac = BOND_FOM_ACTIVE;
  1770. slave_warn(bond_dev, slave_dev, "Setting fail_over_mac to active for active-backup mode\n");
  1771. } else {
  1772. SLAVE_NL_ERR(bond_dev, slave_dev, extack,
  1773. "Slave device does not support setting the MAC address, but fail_over_mac is not set to active");
  1774. res = -EOPNOTSUPP;
  1775. goto err_undo_flags;
  1776. }
  1777. }
  1778. }
  1779. call_netdevice_notifiers(NETDEV_JOIN, slave_dev);
  1780. /* If this is the first slave, then we need to set the master's hardware
  1781. * address to be the same as the slave's.
  1782. */
  1783. if (!bond_has_slaves(bond) &&
  1784. bond->dev->addr_assign_type == NET_ADDR_RANDOM) {
  1785. res = bond_set_dev_addr(bond->dev, slave_dev);
  1786. if (res)
  1787. goto err_undo_flags;
  1788. }
  1789. new_slave = bond_alloc_slave(bond, slave_dev);
  1790. if (!new_slave) {
  1791. res = -ENOMEM;
  1792. goto err_undo_flags;
  1793. }
  1794. /* Set the new_slave's queue_id to be zero. Queue ID mapping
  1795. * is set via sysfs or module option if desired.
  1796. */
  1797. new_slave->queue_id = 0;
  1798. /* Save slave's original mtu and then set it to match the bond */
  1799. new_slave->original_mtu = slave_dev->mtu;
  1800. res = dev_set_mtu(slave_dev, bond->dev->mtu);
  1801. if (res) {
  1802. slave_err(bond_dev, slave_dev, "Error %d calling dev_set_mtu\n", res);
  1803. goto err_free;
  1804. }
  1805. /* Save slave's original ("permanent") mac address for modes
  1806. * that need it, and for restoring it upon release, and then
  1807. * set it to the master's address
  1808. */
  1809. bond_hw_addr_copy(new_slave->perm_hwaddr, slave_dev->dev_addr,
  1810. slave_dev->addr_len);
  1811. if (!bond->params.fail_over_mac ||
  1812. BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) {
  1813. /* Set slave to master's mac address. The application already
  1814. * set the master's mac address to that of the first slave
  1815. */
  1816. memcpy(ss.__data, bond_dev->dev_addr, bond_dev->addr_len);
  1817. } else if (bond->params.fail_over_mac == BOND_FOM_FOLLOW &&
  1818. BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP &&
  1819. bond_has_slaves(bond) &&
  1820. memcmp(slave_dev->dev_addr, bond_dev->dev_addr, bond_dev->addr_len) == 0) {
  1821. /* Set slave to random address to avoid duplicate mac
  1822. * address in later fail over.
  1823. */
  1824. eth_random_addr(ss.__data);
  1825. } else {
  1826. goto skip_mac_set;
  1827. }
  1828. ss.ss_family = slave_dev->type;
  1829. res = dev_set_mac_address(slave_dev, (struct sockaddr *)&ss, extack);
  1830. if (res) {
  1831. slave_err(bond_dev, slave_dev, "Error %d calling set_mac_address\n", res);
  1832. goto err_restore_mtu;
  1833. }
  1834. skip_mac_set:
  1835. /* set no_addrconf flag before open to prevent IPv6 addrconf */
  1836. slave_dev->priv_flags |= IFF_NO_ADDRCONF;
  1837. /* open the slave since the application closed it */
  1838. res = dev_open(slave_dev, extack);
  1839. if (res) {
  1840. slave_err(bond_dev, slave_dev, "Opening slave failed\n");
  1841. goto err_restore_mac;
  1842. }
  1843. slave_dev->priv_flags |= IFF_BONDING;
  1844. /* initialize slave stats */
  1845. dev_get_stats(new_slave->dev, &new_slave->slave_stats);
  1846. if (bond_is_lb(bond)) {
  1847. /* bond_alb_init_slave() must be called before all other stages since
  1848. * it might fail and we do not want to have to undo everything
  1849. */
  1850. res = bond_alb_init_slave(bond, new_slave);
  1851. if (res)
  1852. goto err_close;
  1853. }
  1854. res = vlan_vids_add_by_dev(slave_dev, bond_dev);
  1855. if (res) {
  1856. slave_err(bond_dev, slave_dev, "Couldn't add bond vlan ids\n");
  1857. goto err_close;
  1858. }
  1859. prev_slave = bond_last_slave(bond);
  1860. new_slave->delay = 0;
  1861. new_slave->link_failure_count = 0;
  1862. if (bond_update_speed_duplex(new_slave) &&
  1863. bond_needs_speed_duplex(bond))
  1864. new_slave->link = BOND_LINK_DOWN;
  1865. new_slave->last_rx = jiffies -
  1866. (msecs_to_jiffies(bond->params.arp_interval) + 1);
  1867. for (i = 0; i < BOND_MAX_ARP_TARGETS; i++)
  1868. new_slave->target_last_arp_rx[i] = new_slave->last_rx;
  1869. new_slave->last_tx = new_slave->last_rx;
  1870. if (bond->params.miimon && !bond->params.use_carrier) {
  1871. link_reporting = bond_check_dev_link(bond, slave_dev, 1);
  1872. if ((link_reporting == -1) && !bond->params.arp_interval) {
  1873. /* miimon is set but a bonded network driver
  1874. * does not support ETHTOOL/MII and
  1875. * arp_interval is not set. Note: if
  1876. * use_carrier is enabled, we will never go
  1877. * here (because netif_carrier is always
  1878. * supported); thus, we don't need to change
  1879. * the messages for netif_carrier.
  1880. */
  1881. slave_warn(bond_dev, slave_dev, "MII and ETHTOOL support not available for slave, and arp_interval/arp_ip_target module parameters not specified, thus bonding will not detect link failures! see bonding.txt for details\n");
  1882. } else if (link_reporting == -1) {
  1883. /* unable get link status using mii/ethtool */
  1884. slave_warn(bond_dev, slave_dev, "can't get link status from slave; 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");
  1885. }
  1886. }
  1887. /* check for initial state */
  1888. new_slave->link = BOND_LINK_NOCHANGE;
  1889. if (bond->params.miimon) {
  1890. if (bond_check_dev_link(bond, slave_dev, 0) == BMSR_LSTATUS) {
  1891. if (bond->params.updelay) {
  1892. bond_set_slave_link_state(new_slave,
  1893. BOND_LINK_BACK,
  1894. BOND_SLAVE_NOTIFY_NOW);
  1895. new_slave->delay = bond->params.updelay;
  1896. } else {
  1897. bond_set_slave_link_state(new_slave,
  1898. BOND_LINK_UP,
  1899. BOND_SLAVE_NOTIFY_NOW);
  1900. }
  1901. } else {
  1902. bond_set_slave_link_state(new_slave, BOND_LINK_DOWN,
  1903. BOND_SLAVE_NOTIFY_NOW);
  1904. }
  1905. } else if (bond->params.arp_interval) {
  1906. bond_set_slave_link_state(new_slave,
  1907. (netif_carrier_ok(slave_dev) ?
  1908. BOND_LINK_UP : BOND_LINK_DOWN),
  1909. BOND_SLAVE_NOTIFY_NOW);
  1910. } else {
  1911. bond_set_slave_link_state(new_slave, BOND_LINK_UP,
  1912. BOND_SLAVE_NOTIFY_NOW);
  1913. }
  1914. if (new_slave->link != BOND_LINK_DOWN)
  1915. new_slave->last_link_up = jiffies;
  1916. slave_dbg(bond_dev, slave_dev, "Initial state of slave is BOND_LINK_%s\n",
  1917. new_slave->link == BOND_LINK_DOWN ? "DOWN" :
  1918. (new_slave->link == BOND_LINK_UP ? "UP" : "BACK"));
  1919. if (bond_uses_primary(bond) && bond->params.primary[0]) {
  1920. /* if there is a primary slave, remember it */
  1921. if (strcmp(bond->params.primary, new_slave->dev->name) == 0) {
  1922. rcu_assign_pointer(bond->primary_slave, new_slave);
  1923. bond->force_primary = true;
  1924. }
  1925. }
  1926. switch (BOND_MODE(bond)) {
  1927. case BOND_MODE_ACTIVEBACKUP:
  1928. bond_set_slave_inactive_flags(new_slave,
  1929. BOND_SLAVE_NOTIFY_NOW);
  1930. break;
  1931. case BOND_MODE_8023AD:
  1932. /* in 802.3ad mode, the internal mechanism
  1933. * will activate the slaves in the selected
  1934. * aggregator
  1935. */
  1936. bond_set_slave_inactive_flags(new_slave, BOND_SLAVE_NOTIFY_NOW);
  1937. /* if this is the first slave */
  1938. if (!prev_slave) {
  1939. SLAVE_AD_INFO(new_slave)->id = 1;
  1940. /* Initialize AD with the number of times that the AD timer is called in 1 second
  1941. * can be called only after the mac address of the bond is set
  1942. */
  1943. bond_3ad_initialize(bond);
  1944. } else {
  1945. SLAVE_AD_INFO(new_slave)->id =
  1946. SLAVE_AD_INFO(prev_slave)->id + 1;
  1947. }
  1948. bond_3ad_bind_slave(new_slave);
  1949. break;
  1950. case BOND_MODE_TLB:
  1951. case BOND_MODE_ALB:
  1952. bond_set_active_slave(new_slave);
  1953. bond_set_slave_inactive_flags(new_slave, BOND_SLAVE_NOTIFY_NOW);
  1954. break;
  1955. default:
  1956. slave_dbg(bond_dev, slave_dev, "This slave is always active in trunk mode\n");
  1957. /* always active in trunk mode */
  1958. bond_set_active_slave(new_slave);
  1959. /* In trunking mode there is little meaning to curr_active_slave
  1960. * anyway (it holds no special properties of the bond device),
  1961. * so we can change it without calling change_active_interface()
  1962. */
  1963. if (!rcu_access_pointer(bond->curr_active_slave) &&
  1964. new_slave->link == BOND_LINK_UP)
  1965. rcu_assign_pointer(bond->curr_active_slave, new_slave);
  1966. break;
  1967. } /* switch(bond_mode) */
  1968. #ifdef CONFIG_NET_POLL_CONTROLLER
  1969. if (bond->dev->npinfo) {
  1970. if (slave_enable_netpoll(new_slave)) {
  1971. slave_info(bond_dev, slave_dev, "master_dev is using netpoll, but new slave device does not support netpoll\n");
  1972. res = -EBUSY;
  1973. goto err_detach;
  1974. }
  1975. }
  1976. #endif
  1977. if (!(bond_dev->features & NETIF_F_LRO))
  1978. dev_disable_lro(slave_dev);
  1979. res = netdev_rx_handler_register(slave_dev, bond_handle_frame,
  1980. new_slave);
  1981. if (res) {
  1982. slave_dbg(bond_dev, slave_dev, "Error %d calling netdev_rx_handler_register\n", res);
  1983. goto err_detach;
  1984. }
  1985. res = bond_master_upper_dev_link(bond, new_slave, extack);
  1986. if (res) {
  1987. slave_dbg(bond_dev, slave_dev, "Error %d calling bond_master_upper_dev_link\n", res);
  1988. goto err_unregister;
  1989. }
  1990. bond_lower_state_changed(new_slave);
  1991. res = bond_sysfs_slave_add(new_slave);
  1992. if (res) {
  1993. slave_dbg(bond_dev, slave_dev, "Error %d calling bond_sysfs_slave_add\n", res);
  1994. goto err_upper_unlink;
  1995. }
  1996. /* If the mode uses primary, then the following is handled by
  1997. * bond_change_active_slave().
  1998. */
  1999. if (!bond_uses_primary(bond)) {
  2000. /* set promiscuity level to new slave */
  2001. if (bond_dev->flags & IFF_PROMISC) {
  2002. res = dev_set_promiscuity(slave_dev, 1);
  2003. if (res)
  2004. goto err_sysfs_del;
  2005. }
  2006. /* set allmulti level to new slave */
  2007. if (bond_dev->flags & IFF_ALLMULTI) {
  2008. res = dev_set_allmulti(slave_dev, 1);
  2009. if (res) {
  2010. if (bond_dev->flags & IFF_PROMISC)
  2011. dev_set_promiscuity(slave_dev, -1);
  2012. goto err_sysfs_del;
  2013. }
  2014. }
  2015. if (bond_dev->flags & IFF_UP) {
  2016. netif_addr_lock_bh(bond_dev);
  2017. dev_mc_sync_multiple(slave_dev, bond_dev);
  2018. dev_uc_sync_multiple(slave_dev, bond_dev);
  2019. netif_addr_unlock_bh(bond_dev);
  2020. if (BOND_MODE(bond) == BOND_MODE_8023AD)
  2021. dev_mc_add(slave_dev, lacpdu_mcast_addr);
  2022. }
  2023. }
  2024. bond->slave_cnt++;
  2025. bond_compute_features(bond);
  2026. bond_set_carrier(bond);
  2027. /* Needs to be called before bond_select_active_slave(), which will
  2028. * remove the maddrs if the slave is selected as active slave.
  2029. */
  2030. bond_slave_ns_maddrs_add(bond, new_slave);
  2031. if (bond_uses_primary(bond)) {
  2032. block_netpoll_tx();
  2033. bond_select_active_slave(bond);
  2034. unblock_netpoll_tx();
  2035. }
  2036. if (bond_mode_can_use_xmit_hash(bond))
  2037. bond_update_slave_arr(bond, NULL);
  2038. if (!slave_dev->netdev_ops->ndo_bpf ||
  2039. !slave_dev->netdev_ops->ndo_xdp_xmit) {
  2040. if (bond->xdp_prog) {
  2041. SLAVE_NL_ERR(bond_dev, slave_dev, extack,
  2042. "Slave does not support XDP");
  2043. res = -EOPNOTSUPP;
  2044. goto err_sysfs_del;
  2045. }
  2046. } else if (bond->xdp_prog) {
  2047. struct netdev_bpf xdp = {
  2048. .command = XDP_SETUP_PROG,
  2049. .flags = 0,
  2050. .prog = bond->xdp_prog,
  2051. .extack = extack,
  2052. };
  2053. if (dev_xdp_prog_count(slave_dev) > 0) {
  2054. SLAVE_NL_ERR(bond_dev, slave_dev, extack,
  2055. "Slave has XDP program loaded, please unload before enslaving");
  2056. res = -EOPNOTSUPP;
  2057. goto err_sysfs_del;
  2058. }
  2059. res = dev_xdp_propagate(slave_dev, &xdp);
  2060. if (res < 0) {
  2061. /* ndo_bpf() sets extack error message */
  2062. slave_dbg(bond_dev, slave_dev, "Error %d calling ndo_bpf\n", res);
  2063. goto err_sysfs_del;
  2064. }
  2065. if (bond->xdp_prog)
  2066. bpf_prog_inc(bond->xdp_prog);
  2067. }
  2068. bond_xdp_set_features(bond_dev);
  2069. slave_info(bond_dev, slave_dev, "Enslaving as %s interface with %s link\n",
  2070. bond_is_active_slave(new_slave) ? "an active" : "a backup",
  2071. new_slave->link != BOND_LINK_DOWN ? "an up" : "a down");
  2072. /* enslave is successful */
  2073. bond_queue_slave_event(new_slave);
  2074. return 0;
  2075. /* Undo stages on error */
  2076. err_sysfs_del:
  2077. bond_sysfs_slave_del(new_slave);
  2078. err_upper_unlink:
  2079. bond_upper_dev_unlink(bond, new_slave);
  2080. err_unregister:
  2081. netdev_rx_handler_unregister(slave_dev);
  2082. err_detach:
  2083. vlan_vids_del_by_dev(slave_dev, bond_dev);
  2084. if (rcu_access_pointer(bond->primary_slave) == new_slave)
  2085. RCU_INIT_POINTER(bond->primary_slave, NULL);
  2086. if (rcu_access_pointer(bond->curr_active_slave) == new_slave) {
  2087. block_netpoll_tx();
  2088. bond_change_active_slave(bond, NULL);
  2089. bond_select_active_slave(bond);
  2090. unblock_netpoll_tx();
  2091. }
  2092. /* either primary_slave or curr_active_slave might've changed */
  2093. synchronize_rcu();
  2094. slave_disable_netpoll(new_slave);
  2095. err_close:
  2096. if (!netif_is_bond_master(slave_dev))
  2097. slave_dev->priv_flags &= ~IFF_BONDING;
  2098. dev_close(slave_dev);
  2099. err_restore_mac:
  2100. slave_dev->priv_flags &= ~IFF_NO_ADDRCONF;
  2101. if (!bond->params.fail_over_mac ||
  2102. BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) {
  2103. /* XXX TODO - fom follow mode needs to change master's
  2104. * MAC if this slave's MAC is in use by the bond, or at
  2105. * least print a warning.
  2106. */
  2107. bond_hw_addr_copy(ss.__data, new_slave->perm_hwaddr,
  2108. new_slave->dev->addr_len);
  2109. ss.ss_family = slave_dev->type;
  2110. dev_set_mac_address(slave_dev, (struct sockaddr *)&ss, NULL);
  2111. }
  2112. err_restore_mtu:
  2113. dev_set_mtu(slave_dev, new_slave->original_mtu);
  2114. err_free:
  2115. kobject_put(&new_slave->kobj);
  2116. err_undo_flags:
  2117. /* Enslave of first slave has failed and we need to fix master's mac */
  2118. if (!bond_has_slaves(bond)) {
  2119. if (ether_addr_equal_64bits(bond_dev->dev_addr,
  2120. slave_dev->dev_addr))
  2121. eth_hw_addr_random(bond_dev);
  2122. if (bond_dev->type != ARPHRD_ETHER) {
  2123. dev_close(bond_dev);
  2124. bond_ether_setup(bond_dev);
  2125. }
  2126. }
  2127. return res;
  2128. }
  2129. /* Try to release the slave device <slave> from the bond device <master>
  2130. * It is legal to access curr_active_slave without a lock because all the function
  2131. * is RTNL-locked. If "all" is true it means that the function is being called
  2132. * while destroying a bond interface and all slaves are being released.
  2133. *
  2134. * The rules for slave state should be:
  2135. * for Active/Backup:
  2136. * Active stays on all backups go down
  2137. * for Bonded connections:
  2138. * The first up interface should be left on and all others downed.
  2139. */
  2140. static int __bond_release_one(struct net_device *bond_dev,
  2141. struct net_device *slave_dev,
  2142. bool all, bool unregister)
  2143. {
  2144. struct bonding *bond = netdev_priv(bond_dev);
  2145. struct slave *slave, *oldcurrent;
  2146. struct sockaddr_storage ss;
  2147. int old_flags = bond_dev->flags;
  2148. netdev_features_t old_features = bond_dev->features;
  2149. /* slave is not a slave or master is not master of this slave */
  2150. if (!(slave_dev->flags & IFF_SLAVE) ||
  2151. !netdev_has_upper_dev(slave_dev, bond_dev)) {
  2152. slave_dbg(bond_dev, slave_dev, "cannot release slave\n");
  2153. return -EINVAL;
  2154. }
  2155. block_netpoll_tx();
  2156. slave = bond_get_slave_by_dev(bond, slave_dev);
  2157. if (!slave) {
  2158. /* not a slave of this bond */
  2159. slave_info(bond_dev, slave_dev, "interface not enslaved\n");
  2160. unblock_netpoll_tx();
  2161. return -EINVAL;
  2162. }
  2163. bond_set_slave_inactive_flags(slave, BOND_SLAVE_NOTIFY_NOW);
  2164. bond_sysfs_slave_del(slave);
  2165. /* recompute stats just before removing the slave */
  2166. bond_get_stats(bond->dev, &bond->bond_stats);
  2167. if (bond->xdp_prog) {
  2168. struct netdev_bpf xdp = {
  2169. .command = XDP_SETUP_PROG,
  2170. .flags = 0,
  2171. .prog = NULL,
  2172. .extack = NULL,
  2173. };
  2174. if (dev_xdp_propagate(slave_dev, &xdp))
  2175. slave_warn(bond_dev, slave_dev, "failed to unload XDP program\n");
  2176. }
  2177. /* unregister rx_handler early so bond_handle_frame wouldn't be called
  2178. * for this slave anymore.
  2179. */
  2180. netdev_rx_handler_unregister(slave_dev);
  2181. if (BOND_MODE(bond) == BOND_MODE_8023AD)
  2182. bond_3ad_unbind_slave(slave);
  2183. bond_upper_dev_unlink(bond, slave);
  2184. if (bond_mode_can_use_xmit_hash(bond))
  2185. bond_update_slave_arr(bond, slave);
  2186. slave_info(bond_dev, slave_dev, "Releasing %s interface\n",
  2187. bond_is_active_slave(slave) ? "active" : "backup");
  2188. oldcurrent = rcu_access_pointer(bond->curr_active_slave);
  2189. RCU_INIT_POINTER(bond->current_arp_slave, NULL);
  2190. if (!all && (bond->params.fail_over_mac != BOND_FOM_ACTIVE ||
  2191. BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP)) {
  2192. if (ether_addr_equal_64bits(bond_dev->dev_addr, slave->perm_hwaddr) &&
  2193. bond_has_slaves(bond))
  2194. slave_warn(bond_dev, slave_dev, "the permanent HWaddr of slave - %pM - is still in use by bond - set the HWaddr of slave to a different address to avoid conflicts\n",
  2195. slave->perm_hwaddr);
  2196. }
  2197. if (rtnl_dereference(bond->primary_slave) == slave)
  2198. RCU_INIT_POINTER(bond->primary_slave, NULL);
  2199. if (oldcurrent == slave)
  2200. bond_change_active_slave(bond, NULL);
  2201. /* Must be called after bond_change_active_slave () as the slave
  2202. * might change from an active slave to a backup slave. Then it is
  2203. * necessary to clear the maddrs on the backup slave.
  2204. */
  2205. bond_slave_ns_maddrs_del(bond, slave);
  2206. if (bond_is_lb(bond)) {
  2207. /* Must be called only after the slave has been
  2208. * detached from the list and the curr_active_slave
  2209. * has been cleared (if our_slave == old_current),
  2210. * but before a new active slave is selected.
  2211. */
  2212. bond_alb_deinit_slave(bond, slave);
  2213. }
  2214. if (all) {
  2215. RCU_INIT_POINTER(bond->curr_active_slave, NULL);
  2216. } else if (oldcurrent == slave) {
  2217. /* Note that we hold RTNL over this sequence, so there
  2218. * is no concern that another slave add/remove event
  2219. * will interfere.
  2220. */
  2221. bond_select_active_slave(bond);
  2222. }
  2223. bond_set_carrier(bond);
  2224. if (!bond_has_slaves(bond))
  2225. eth_hw_addr_random(bond_dev);
  2226. unblock_netpoll_tx();
  2227. synchronize_rcu();
  2228. bond->slave_cnt--;
  2229. if (!bond_has_slaves(bond)) {
  2230. call_netdevice_notifiers(NETDEV_CHANGEADDR, bond->dev);
  2231. call_netdevice_notifiers(NETDEV_RELEASE, bond->dev);
  2232. }
  2233. bond_compute_features(bond);
  2234. if (!(bond_dev->features & NETIF_F_VLAN_CHALLENGED) &&
  2235. (old_features & NETIF_F_VLAN_CHALLENGED))
  2236. slave_info(bond_dev, slave_dev, "last VLAN challenged slave left bond - VLAN blocking is removed\n");
  2237. vlan_vids_del_by_dev(slave_dev, bond_dev);
  2238. /* If the mode uses primary, then this case was handled above by
  2239. * bond_change_active_slave(..., NULL)
  2240. */
  2241. if (!bond_uses_primary(bond)) {
  2242. /* unset promiscuity level from slave
  2243. * NOTE: The NETDEV_CHANGEADDR call above may change the value
  2244. * of the IFF_PROMISC flag in the bond_dev, but we need the
  2245. * value of that flag before that change, as that was the value
  2246. * when this slave was attached, so we cache at the start of the
  2247. * function and use it here. Same goes for ALLMULTI below
  2248. */
  2249. if (old_flags & IFF_PROMISC)
  2250. dev_set_promiscuity(slave_dev, -1);
  2251. /* unset allmulti level from slave */
  2252. if (old_flags & IFF_ALLMULTI)
  2253. dev_set_allmulti(slave_dev, -1);
  2254. if (old_flags & IFF_UP)
  2255. bond_hw_addr_flush(bond_dev, slave_dev);
  2256. }
  2257. slave_disable_netpoll(slave);
  2258. /* close slave before restoring its mac address */
  2259. dev_close(slave_dev);
  2260. slave_dev->priv_flags &= ~IFF_NO_ADDRCONF;
  2261. if (bond->params.fail_over_mac != BOND_FOM_ACTIVE ||
  2262. BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) {
  2263. /* restore original ("permanent") mac address */
  2264. bond_hw_addr_copy(ss.__data, slave->perm_hwaddr,
  2265. slave->dev->addr_len);
  2266. ss.ss_family = slave_dev->type;
  2267. dev_set_mac_address(slave_dev, (struct sockaddr *)&ss, NULL);
  2268. }
  2269. if (unregister)
  2270. __dev_set_mtu(slave_dev, slave->original_mtu);
  2271. else
  2272. dev_set_mtu(slave_dev, slave->original_mtu);
  2273. if (!netif_is_bond_master(slave_dev))
  2274. slave_dev->priv_flags &= ~IFF_BONDING;
  2275. bond_xdp_set_features(bond_dev);
  2276. kobject_put(&slave->kobj);
  2277. return 0;
  2278. }
  2279. /* A wrapper used because of ndo_del_link */
  2280. int bond_release(struct net_device *bond_dev, struct net_device *slave_dev)
  2281. {
  2282. return __bond_release_one(bond_dev, slave_dev, false, false);
  2283. }
  2284. /* First release a slave and then destroy the bond if no more slaves are left.
  2285. * Must be under rtnl_lock when this function is called.
  2286. */
  2287. static int bond_release_and_destroy(struct net_device *bond_dev,
  2288. struct net_device *slave_dev)
  2289. {
  2290. struct bonding *bond = netdev_priv(bond_dev);
  2291. int ret;
  2292. ret = __bond_release_one(bond_dev, slave_dev, false, true);
  2293. if (ret == 0 && !bond_has_slaves(bond) &&
  2294. bond_dev->reg_state != NETREG_UNREGISTERING) {
  2295. bond_dev->priv_flags |= IFF_DISABLE_NETPOLL;
  2296. netdev_info(bond_dev, "Destroying bond\n");
  2297. bond_remove_proc_entry(bond);
  2298. unregister_netdevice(bond_dev);
  2299. }
  2300. return ret;
  2301. }
  2302. static void bond_info_query(struct net_device *bond_dev, struct ifbond *info)
  2303. {
  2304. struct bonding *bond = netdev_priv(bond_dev);
  2305. bond_fill_ifbond(bond, info);
  2306. }
  2307. static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info)
  2308. {
  2309. struct bonding *bond = netdev_priv(bond_dev);
  2310. struct list_head *iter;
  2311. int i = 0, res = -ENODEV;
  2312. struct slave *slave;
  2313. bond_for_each_slave(bond, slave, iter) {
  2314. if (i++ == (int)info->slave_id) {
  2315. res = 0;
  2316. bond_fill_ifslave(slave, info);
  2317. break;
  2318. }
  2319. }
  2320. return res;
  2321. }
  2322. /*-------------------------------- Monitoring -------------------------------*/
  2323. /* called with rcu_read_lock() */
  2324. static int bond_miimon_inspect(struct bonding *bond)
  2325. {
  2326. bool ignore_updelay = false;
  2327. int link_state, commit = 0;
  2328. struct list_head *iter;
  2329. struct slave *slave;
  2330. if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP) {
  2331. ignore_updelay = !rcu_dereference(bond->curr_active_slave);
  2332. } else {
  2333. struct bond_up_slave *usable_slaves;
  2334. usable_slaves = rcu_dereference(bond->usable_slaves);
  2335. if (usable_slaves && usable_slaves->count == 0)
  2336. ignore_updelay = true;
  2337. }
  2338. bond_for_each_slave_rcu(bond, slave, iter) {
  2339. bond_propose_link_state(slave, BOND_LINK_NOCHANGE);
  2340. link_state = bond_check_dev_link(bond, slave->dev, 0);
  2341. switch (slave->link) {
  2342. case BOND_LINK_UP:
  2343. if (link_state)
  2344. continue;
  2345. bond_propose_link_state(slave, BOND_LINK_FAIL);
  2346. commit++;
  2347. slave->delay = bond->params.downdelay;
  2348. if (slave->delay && net_ratelimit()) {
  2349. slave_info(bond->dev, slave->dev, "link status down for %sinterface, disabling it in %d ms\n",
  2350. (BOND_MODE(bond) ==
  2351. BOND_MODE_ACTIVEBACKUP) ?
  2352. (bond_is_active_slave(slave) ?
  2353. "active " : "backup ") : "",
  2354. bond->params.downdelay * bond->params.miimon);
  2355. }
  2356. fallthrough;
  2357. case BOND_LINK_FAIL:
  2358. if (link_state) {
  2359. /* recovered before downdelay expired */
  2360. bond_propose_link_state(slave, BOND_LINK_UP);
  2361. slave->last_link_up = jiffies;
  2362. if (net_ratelimit())
  2363. slave_info(bond->dev, slave->dev, "link status up again after %d ms\n",
  2364. (bond->params.downdelay - slave->delay) *
  2365. bond->params.miimon);
  2366. commit++;
  2367. continue;
  2368. }
  2369. if (slave->delay <= 0) {
  2370. bond_propose_link_state(slave, BOND_LINK_DOWN);
  2371. commit++;
  2372. continue;
  2373. }
  2374. slave->delay--;
  2375. break;
  2376. case BOND_LINK_DOWN:
  2377. if (!link_state)
  2378. continue;
  2379. bond_propose_link_state(slave, BOND_LINK_BACK);
  2380. commit++;
  2381. slave->delay = bond->params.updelay;
  2382. if (slave->delay && net_ratelimit()) {
  2383. slave_info(bond->dev, slave->dev, "link status up, enabling it in %d ms\n",
  2384. ignore_updelay ? 0 :
  2385. bond->params.updelay *
  2386. bond->params.miimon);
  2387. }
  2388. fallthrough;
  2389. case BOND_LINK_BACK:
  2390. if (!link_state) {
  2391. bond_propose_link_state(slave, BOND_LINK_DOWN);
  2392. if (net_ratelimit())
  2393. slave_info(bond->dev, slave->dev, "link status down again after %d ms\n",
  2394. (bond->params.updelay - slave->delay) *
  2395. bond->params.miimon);
  2396. commit++;
  2397. continue;
  2398. }
  2399. if (ignore_updelay)
  2400. slave->delay = 0;
  2401. if (slave->delay <= 0) {
  2402. bond_propose_link_state(slave, BOND_LINK_UP);
  2403. commit++;
  2404. ignore_updelay = false;
  2405. continue;
  2406. }
  2407. slave->delay--;
  2408. break;
  2409. }
  2410. }
  2411. return commit;
  2412. }
  2413. static void bond_miimon_link_change(struct bonding *bond,
  2414. struct slave *slave,
  2415. char link)
  2416. {
  2417. switch (BOND_MODE(bond)) {
  2418. case BOND_MODE_8023AD:
  2419. bond_3ad_handle_link_change(slave, link);
  2420. break;
  2421. case BOND_MODE_TLB:
  2422. case BOND_MODE_ALB:
  2423. bond_alb_handle_link_change(bond, slave, link);
  2424. break;
  2425. case BOND_MODE_XOR:
  2426. bond_update_slave_arr(bond, NULL);
  2427. break;
  2428. }
  2429. }
  2430. static void bond_miimon_commit(struct bonding *bond)
  2431. {
  2432. struct slave *slave, *primary, *active;
  2433. bool do_failover = false;
  2434. struct list_head *iter;
  2435. ASSERT_RTNL();
  2436. bond_for_each_slave(bond, slave, iter) {
  2437. switch (slave->link_new_state) {
  2438. case BOND_LINK_NOCHANGE:
  2439. /* For 802.3ad mode, check current slave speed and
  2440. * duplex again in case its port was disabled after
  2441. * invalid speed/duplex reporting but recovered before
  2442. * link monitoring could make a decision on the actual
  2443. * link status
  2444. */
  2445. if (BOND_MODE(bond) == BOND_MODE_8023AD &&
  2446. slave->link == BOND_LINK_UP)
  2447. bond_3ad_adapter_speed_duplex_changed(slave);
  2448. continue;
  2449. case BOND_LINK_UP:
  2450. if (bond_update_speed_duplex(slave) &&
  2451. bond_needs_speed_duplex(bond)) {
  2452. slave->link = BOND_LINK_DOWN;
  2453. if (net_ratelimit())
  2454. slave_warn(bond->dev, slave->dev,
  2455. "failed to get link speed/duplex\n");
  2456. continue;
  2457. }
  2458. bond_set_slave_link_state(slave, BOND_LINK_UP,
  2459. BOND_SLAVE_NOTIFY_NOW);
  2460. slave->last_link_up = jiffies;
  2461. primary = rtnl_dereference(bond->primary_slave);
  2462. if (BOND_MODE(bond) == BOND_MODE_8023AD) {
  2463. /* prevent it from being the active one */
  2464. bond_set_backup_slave(slave);
  2465. } else if (BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) {
  2466. /* make it immediately active */
  2467. bond_set_active_slave(slave);
  2468. }
  2469. slave_info(bond->dev, slave->dev, "link status definitely up, %u Mbps %s duplex\n",
  2470. slave->speed == SPEED_UNKNOWN ? 0 : slave->speed,
  2471. slave->duplex ? "full" : "half");
  2472. bond_miimon_link_change(bond, slave, BOND_LINK_UP);
  2473. active = rtnl_dereference(bond->curr_active_slave);
  2474. if (!active || slave == primary || slave->prio > active->prio)
  2475. do_failover = true;
  2476. continue;
  2477. case BOND_LINK_DOWN:
  2478. if (slave->link_failure_count < UINT_MAX)
  2479. slave->link_failure_count++;
  2480. bond_set_slave_link_state(slave, BOND_LINK_DOWN,
  2481. BOND_SLAVE_NOTIFY_NOW);
  2482. if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP ||
  2483. BOND_MODE(bond) == BOND_MODE_8023AD)
  2484. bond_set_slave_inactive_flags(slave,
  2485. BOND_SLAVE_NOTIFY_NOW);
  2486. slave_info(bond->dev, slave->dev, "link status definitely down, disabling slave\n");
  2487. bond_miimon_link_change(bond, slave, BOND_LINK_DOWN);
  2488. if (slave == rcu_access_pointer(bond->curr_active_slave))
  2489. do_failover = true;
  2490. continue;
  2491. default:
  2492. slave_err(bond->dev, slave->dev, "invalid new link %d on slave\n",
  2493. slave->link_new_state);
  2494. bond_propose_link_state(slave, BOND_LINK_NOCHANGE);
  2495. continue;
  2496. }
  2497. }
  2498. if (do_failover) {
  2499. block_netpoll_tx();
  2500. bond_select_active_slave(bond);
  2501. unblock_netpoll_tx();
  2502. }
  2503. bond_set_carrier(bond);
  2504. }
  2505. /* bond_mii_monitor
  2506. *
  2507. * Really a wrapper that splits the mii monitor into two phases: an
  2508. * inspection, then (if inspection indicates something needs to be done)
  2509. * an acquisition of appropriate locks followed by a commit phase to
  2510. * implement whatever link state changes are indicated.
  2511. */
  2512. static void bond_mii_monitor(struct work_struct *work)
  2513. {
  2514. struct bonding *bond = container_of(work, struct bonding,
  2515. mii_work.work);
  2516. bool should_notify_peers;
  2517. bool commit;
  2518. unsigned long delay;
  2519. struct slave *slave;
  2520. struct list_head *iter;
  2521. delay = msecs_to_jiffies(bond->params.miimon);
  2522. if (!bond_has_slaves(bond))
  2523. goto re_arm;
  2524. rcu_read_lock();
  2525. should_notify_peers = bond_should_notify_peers(bond);
  2526. commit = !!bond_miimon_inspect(bond);
  2527. rcu_read_unlock();
  2528. if (commit || bond->send_peer_notif) {
  2529. /* Race avoidance with bond_close cancel of workqueue */
  2530. if (!rtnl_trylock()) {
  2531. delay = 1;
  2532. goto re_arm;
  2533. }
  2534. if (commit) {
  2535. bond_for_each_slave(bond, slave, iter) {
  2536. bond_commit_link_state(slave,
  2537. BOND_SLAVE_NOTIFY_LATER);
  2538. }
  2539. bond_miimon_commit(bond);
  2540. }
  2541. if (bond->send_peer_notif) {
  2542. bond->send_peer_notif--;
  2543. if (should_notify_peers)
  2544. call_netdevice_notifiers(NETDEV_NOTIFY_PEERS,
  2545. bond->dev);
  2546. }
  2547. rtnl_unlock(); /* might sleep, hold no other locks */
  2548. }
  2549. re_arm:
  2550. if (bond->params.miimon)
  2551. queue_delayed_work(bond->wq, &bond->mii_work, delay);
  2552. }
  2553. static int bond_upper_dev_walk(struct net_device *upper,
  2554. struct netdev_nested_priv *priv)
  2555. {
  2556. __be32 ip = *(__be32 *)priv->data;
  2557. return ip == bond_confirm_addr(upper, 0, ip);
  2558. }
  2559. static bool bond_has_this_ip(struct bonding *bond, __be32 ip)
  2560. {
  2561. struct netdev_nested_priv priv = {
  2562. .data = (void *)&ip,
  2563. };
  2564. bool ret = false;
  2565. if (ip == bond_confirm_addr(bond->dev, 0, ip))
  2566. return true;
  2567. rcu_read_lock();
  2568. if (netdev_walk_all_upper_dev_rcu(bond->dev, bond_upper_dev_walk, &priv))
  2569. ret = true;
  2570. rcu_read_unlock();
  2571. return ret;
  2572. }
  2573. #define BOND_VLAN_PROTO_NONE cpu_to_be16(0xffff)
  2574. static bool bond_handle_vlan(struct slave *slave, struct bond_vlan_tag *tags,
  2575. struct sk_buff *skb)
  2576. {
  2577. struct net_device *bond_dev = slave->bond->dev;
  2578. struct net_device *slave_dev = slave->dev;
  2579. struct bond_vlan_tag *outer_tag = tags;
  2580. if (!tags || tags->vlan_proto == BOND_VLAN_PROTO_NONE)
  2581. return true;
  2582. tags++;
  2583. /* Go through all the tags backwards and add them to the packet */
  2584. while (tags->vlan_proto != BOND_VLAN_PROTO_NONE) {
  2585. if (!tags->vlan_id) {
  2586. tags++;
  2587. continue;
  2588. }
  2589. slave_dbg(bond_dev, slave_dev, "inner tag: proto %X vid %X\n",
  2590. ntohs(outer_tag->vlan_proto), tags->vlan_id);
  2591. skb = vlan_insert_tag_set_proto(skb, tags->vlan_proto,
  2592. tags->vlan_id);
  2593. if (!skb) {
  2594. net_err_ratelimited("failed to insert inner VLAN tag\n");
  2595. return false;
  2596. }
  2597. tags++;
  2598. }
  2599. /* Set the outer tag */
  2600. if (outer_tag->vlan_id) {
  2601. slave_dbg(bond_dev, slave_dev, "outer tag: proto %X vid %X\n",
  2602. ntohs(outer_tag->vlan_proto), outer_tag->vlan_id);
  2603. __vlan_hwaccel_put_tag(skb, outer_tag->vlan_proto,
  2604. outer_tag->vlan_id);
  2605. }
  2606. return true;
  2607. }
  2608. /* We go to the (large) trouble of VLAN tagging ARP frames because
  2609. * switches in VLAN mode (especially if ports are configured as
  2610. * "native" to a VLAN) might not pass non-tagged frames.
  2611. */
  2612. static void bond_arp_send(struct slave *slave, int arp_op, __be32 dest_ip,
  2613. __be32 src_ip, struct bond_vlan_tag *tags)
  2614. {
  2615. struct net_device *bond_dev = slave->bond->dev;
  2616. struct net_device *slave_dev = slave->dev;
  2617. struct sk_buff *skb;
  2618. slave_dbg(bond_dev, slave_dev, "arp %d on slave: dst %pI4 src %pI4\n",
  2619. arp_op, &dest_ip, &src_ip);
  2620. skb = arp_create(arp_op, ETH_P_ARP, dest_ip, slave_dev, src_ip,
  2621. NULL, slave_dev->dev_addr, NULL);
  2622. if (!skb) {
  2623. net_err_ratelimited("ARP packet allocation failed\n");
  2624. return;
  2625. }
  2626. if (bond_handle_vlan(slave, tags, skb)) {
  2627. slave_update_last_tx(slave);
  2628. arp_xmit(skb);
  2629. }
  2630. return;
  2631. }
  2632. /* Validate the device path between the @start_dev and the @end_dev.
  2633. * The path is valid if the @end_dev is reachable through device
  2634. * stacking.
  2635. * When the path is validated, collect any vlan information in the
  2636. * path.
  2637. */
  2638. struct bond_vlan_tag *bond_verify_device_path(struct net_device *start_dev,
  2639. struct net_device *end_dev,
  2640. int level)
  2641. {
  2642. struct bond_vlan_tag *tags;
  2643. struct net_device *upper;
  2644. struct list_head *iter;
  2645. if (start_dev == end_dev) {
  2646. tags = kcalloc(level + 1, sizeof(*tags), GFP_ATOMIC);
  2647. if (!tags)
  2648. return ERR_PTR(-ENOMEM);
  2649. tags[level].vlan_proto = BOND_VLAN_PROTO_NONE;
  2650. return tags;
  2651. }
  2652. netdev_for_each_upper_dev_rcu(start_dev, upper, iter) {
  2653. tags = bond_verify_device_path(upper, end_dev, level + 1);
  2654. if (IS_ERR_OR_NULL(tags)) {
  2655. if (IS_ERR(tags))
  2656. return tags;
  2657. continue;
  2658. }
  2659. if (is_vlan_dev(upper)) {
  2660. tags[level].vlan_proto = vlan_dev_vlan_proto(upper);
  2661. tags[level].vlan_id = vlan_dev_vlan_id(upper);
  2662. }
  2663. return tags;
  2664. }
  2665. return NULL;
  2666. }
  2667. static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
  2668. {
  2669. struct rtable *rt;
  2670. struct bond_vlan_tag *tags;
  2671. __be32 *targets = bond->params.arp_targets, addr;
  2672. int i;
  2673. for (i = 0; i < BOND_MAX_ARP_TARGETS && targets[i]; i++) {
  2674. slave_dbg(bond->dev, slave->dev, "%s: target %pI4\n",
  2675. __func__, &targets[i]);
  2676. tags = NULL;
  2677. /* Find out through which dev should the packet go */
  2678. rt = ip_route_output(dev_net(bond->dev), targets[i], 0, 0, 0,
  2679. RT_SCOPE_LINK);
  2680. if (IS_ERR(rt)) {
  2681. /* there's no route to target - try to send arp
  2682. * probe to generate any traffic (arp_validate=0)
  2683. */
  2684. if (bond->params.arp_validate)
  2685. pr_warn_once("%s: no route to arp_ip_target %pI4 and arp_validate is set\n",
  2686. bond->dev->name,
  2687. &targets[i]);
  2688. bond_arp_send(slave, ARPOP_REQUEST, targets[i],
  2689. 0, tags);
  2690. continue;
  2691. }
  2692. /* bond device itself */
  2693. if (rt->dst.dev == bond->dev)
  2694. goto found;
  2695. rcu_read_lock();
  2696. tags = bond_verify_device_path(bond->dev, rt->dst.dev, 0);
  2697. rcu_read_unlock();
  2698. if (!IS_ERR_OR_NULL(tags))
  2699. goto found;
  2700. /* Not our device - skip */
  2701. slave_dbg(bond->dev, slave->dev, "no path to arp_ip_target %pI4 via rt.dev %s\n",
  2702. &targets[i], rt->dst.dev ? rt->dst.dev->name : "NULL");
  2703. ip_rt_put(rt);
  2704. continue;
  2705. found:
  2706. addr = bond_confirm_addr(rt->dst.dev, targets[i], 0);
  2707. ip_rt_put(rt);
  2708. bond_arp_send(slave, ARPOP_REQUEST, targets[i], addr, tags);
  2709. kfree(tags);
  2710. }
  2711. }
  2712. static void bond_validate_arp(struct bonding *bond, struct slave *slave, __be32 sip, __be32 tip)
  2713. {
  2714. int i;
  2715. if (!sip || !bond_has_this_ip(bond, tip)) {
  2716. slave_dbg(bond->dev, slave->dev, "%s: sip %pI4 tip %pI4 not found\n",
  2717. __func__, &sip, &tip);
  2718. return;
  2719. }
  2720. i = bond_get_targets_ip(bond->params.arp_targets, sip);
  2721. if (i == -1) {
  2722. slave_dbg(bond->dev, slave->dev, "%s: sip %pI4 not found in targets\n",
  2723. __func__, &sip);
  2724. return;
  2725. }
  2726. slave->last_rx = jiffies;
  2727. slave->target_last_arp_rx[i] = jiffies;
  2728. }
  2729. static int bond_arp_rcv(const struct sk_buff *skb, struct bonding *bond,
  2730. struct slave *slave)
  2731. {
  2732. struct arphdr *arp = (struct arphdr *)skb->data;
  2733. struct slave *curr_active_slave, *curr_arp_slave;
  2734. unsigned char *arp_ptr;
  2735. __be32 sip, tip;
  2736. unsigned int alen;
  2737. alen = arp_hdr_len(bond->dev);
  2738. if (alen > skb_headlen(skb)) {
  2739. arp = kmalloc(alen, GFP_ATOMIC);
  2740. if (!arp)
  2741. goto out_unlock;
  2742. if (skb_copy_bits(skb, 0, arp, alen) < 0)
  2743. goto out_unlock;
  2744. }
  2745. if (arp->ar_hln != bond->dev->addr_len ||
  2746. skb->pkt_type == PACKET_OTHERHOST ||
  2747. skb->pkt_type == PACKET_LOOPBACK ||
  2748. arp->ar_hrd != htons(ARPHRD_ETHER) ||
  2749. arp->ar_pro != htons(ETH_P_IP) ||
  2750. arp->ar_pln != 4)
  2751. goto out_unlock;
  2752. arp_ptr = (unsigned char *)(arp + 1);
  2753. arp_ptr += bond->dev->addr_len;
  2754. memcpy(&sip, arp_ptr, 4);
  2755. arp_ptr += 4 + bond->dev->addr_len;
  2756. memcpy(&tip, arp_ptr, 4);
  2757. slave_dbg(bond->dev, slave->dev, "%s: %s/%d av %d sv %d sip %pI4 tip %pI4\n",
  2758. __func__, slave->dev->name, bond_slave_state(slave),
  2759. bond->params.arp_validate, slave_do_arp_validate(bond, slave),
  2760. &sip, &tip);
  2761. curr_active_slave = rcu_dereference(bond->curr_active_slave);
  2762. curr_arp_slave = rcu_dereference(bond->current_arp_slave);
  2763. /* We 'trust' the received ARP enough to validate it if:
  2764. *
  2765. * (a) the slave receiving the ARP is active (which includes the
  2766. * current ARP slave, if any), or
  2767. *
  2768. * (b) the receiving slave isn't active, but there is a currently
  2769. * active slave and it received valid arp reply(s) after it became
  2770. * the currently active slave, or
  2771. *
  2772. * (c) there is an ARP slave that sent an ARP during the prior ARP
  2773. * interval, and we receive an ARP reply on any slave. We accept
  2774. * these because switch FDB update delays may deliver the ARP
  2775. * reply to a slave other than the sender of the ARP request.
  2776. *
  2777. * Note: for (b), backup slaves are receiving the broadcast ARP
  2778. * request, not a reply. This request passes from the sending
  2779. * slave through the L2 switch(es) to the receiving slave. Since
  2780. * this is checking the request, sip/tip are swapped for
  2781. * validation.
  2782. *
  2783. * This is done to avoid endless looping when we can't reach the
  2784. * arp_ip_target and fool ourselves with our own arp requests.
  2785. */
  2786. if (bond_is_active_slave(slave))
  2787. bond_validate_arp(bond, slave, sip, tip);
  2788. else if (curr_active_slave &&
  2789. time_after(slave_last_rx(bond, curr_active_slave),
  2790. curr_active_slave->last_link_up))
  2791. bond_validate_arp(bond, slave, tip, sip);
  2792. else if (curr_arp_slave && (arp->ar_op == htons(ARPOP_REPLY)) &&
  2793. bond_time_in_interval(bond, slave_last_tx(curr_arp_slave), 1))
  2794. bond_validate_arp(bond, slave, sip, tip);
  2795. out_unlock:
  2796. if (arp != (struct arphdr *)skb->data)
  2797. kfree(arp);
  2798. return RX_HANDLER_ANOTHER;
  2799. }
  2800. #if IS_ENABLED(CONFIG_IPV6)
  2801. static void bond_ns_send(struct slave *slave, const struct in6_addr *daddr,
  2802. const struct in6_addr *saddr, struct bond_vlan_tag *tags)
  2803. {
  2804. struct net_device *bond_dev = slave->bond->dev;
  2805. struct net_device *slave_dev = slave->dev;
  2806. struct in6_addr mcaddr;
  2807. struct sk_buff *skb;
  2808. slave_dbg(bond_dev, slave_dev, "NS on slave: dst %pI6c src %pI6c\n",
  2809. daddr, saddr);
  2810. skb = ndisc_ns_create(slave_dev, daddr, saddr, 0);
  2811. if (!skb) {
  2812. net_err_ratelimited("NS packet allocation failed\n");
  2813. return;
  2814. }
  2815. addrconf_addr_solict_mult(daddr, &mcaddr);
  2816. if (bond_handle_vlan(slave, tags, skb)) {
  2817. slave_update_last_tx(slave);
  2818. ndisc_send_skb(skb, &mcaddr, saddr);
  2819. }
  2820. }
  2821. static void bond_ns_send_all(struct bonding *bond, struct slave *slave)
  2822. {
  2823. struct in6_addr *targets = bond->params.ns_targets;
  2824. struct bond_vlan_tag *tags;
  2825. struct dst_entry *dst;
  2826. struct in6_addr saddr;
  2827. struct flowi6 fl6;
  2828. int i;
  2829. for (i = 0; i < BOND_MAX_NS_TARGETS && !ipv6_addr_any(&targets[i]); i++) {
  2830. slave_dbg(bond->dev, slave->dev, "%s: target %pI6c\n",
  2831. __func__, &targets[i]);
  2832. tags = NULL;
  2833. /* Find out through which dev should the packet go */
  2834. memset(&fl6, 0, sizeof(struct flowi6));
  2835. fl6.daddr = targets[i];
  2836. dst = ip6_route_output(dev_net(bond->dev), NULL, &fl6);
  2837. if (dst->error) {
  2838. dst_release(dst);
  2839. /* there's no route to target - try to send arp
  2840. * probe to generate any traffic (arp_validate=0)
  2841. */
  2842. if (bond->params.arp_validate)
  2843. pr_warn_once("%s: no route to ns_ip6_target %pI6c and arp_validate is set\n",
  2844. bond->dev->name,
  2845. &targets[i]);
  2846. bond_ns_send(slave, &targets[i], &in6addr_any, tags);
  2847. continue;
  2848. }
  2849. /* bond device itself */
  2850. if (dst->dev == bond->dev)
  2851. goto found;
  2852. rcu_read_lock();
  2853. tags = bond_verify_device_path(bond->dev, dst->dev, 0);
  2854. rcu_read_unlock();
  2855. if (!IS_ERR_OR_NULL(tags))
  2856. goto found;
  2857. /* Not our device - skip */
  2858. slave_dbg(bond->dev, slave->dev, "no path to ns_ip6_target %pI6c via dst->dev %s\n",
  2859. &targets[i], dst->dev ? dst->dev->name : "NULL");
  2860. dst_release(dst);
  2861. continue;
  2862. found:
  2863. if (!ipv6_dev_get_saddr(dev_net(dst->dev), dst->dev, &targets[i], 0, &saddr))
  2864. bond_ns_send(slave, &targets[i], &saddr, tags);
  2865. else
  2866. bond_ns_send(slave, &targets[i], &in6addr_any, tags);
  2867. dst_release(dst);
  2868. kfree(tags);
  2869. }
  2870. }
  2871. static int bond_confirm_addr6(struct net_device *dev,
  2872. struct netdev_nested_priv *priv)
  2873. {
  2874. struct in6_addr *addr = (struct in6_addr *)priv->data;
  2875. return ipv6_chk_addr(dev_net(dev), addr, dev, 0);
  2876. }
  2877. static bool bond_has_this_ip6(struct bonding *bond, struct in6_addr *addr)
  2878. {
  2879. struct netdev_nested_priv priv = {
  2880. .data = addr,
  2881. };
  2882. int ret = false;
  2883. if (bond_confirm_addr6(bond->dev, &priv))
  2884. return true;
  2885. rcu_read_lock();
  2886. if (netdev_walk_all_upper_dev_rcu(bond->dev, bond_confirm_addr6, &priv))
  2887. ret = true;
  2888. rcu_read_unlock();
  2889. return ret;
  2890. }
  2891. static void bond_validate_na(struct bonding *bond, struct slave *slave,
  2892. struct in6_addr *saddr, struct in6_addr *daddr)
  2893. {
  2894. int i;
  2895. /* Ignore NAs that:
  2896. * 1. Source address is unspecified address.
  2897. * 2. Dest address is neither all-nodes multicast address nor
  2898. * exist on bond interface.
  2899. */
  2900. if (ipv6_addr_any(saddr) ||
  2901. (!ipv6_addr_equal(daddr, &in6addr_linklocal_allnodes) &&
  2902. !bond_has_this_ip6(bond, daddr))) {
  2903. slave_dbg(bond->dev, slave->dev, "%s: sip %pI6c tip %pI6c not found\n",
  2904. __func__, saddr, daddr);
  2905. return;
  2906. }
  2907. i = bond_get_targets_ip6(bond->params.ns_targets, saddr);
  2908. if (i == -1) {
  2909. slave_dbg(bond->dev, slave->dev, "%s: sip %pI6c not found in targets\n",
  2910. __func__, saddr);
  2911. return;
  2912. }
  2913. slave->last_rx = jiffies;
  2914. slave->target_last_arp_rx[i] = jiffies;
  2915. }
  2916. static int bond_na_rcv(const struct sk_buff *skb, struct bonding *bond,
  2917. struct slave *slave)
  2918. {
  2919. struct slave *curr_active_slave, *curr_arp_slave;
  2920. struct in6_addr *saddr, *daddr;
  2921. struct {
  2922. struct ipv6hdr ip6;
  2923. struct icmp6hdr icmp6;
  2924. } *combined, _combined;
  2925. if (skb->pkt_type == PACKET_OTHERHOST ||
  2926. skb->pkt_type == PACKET_LOOPBACK)
  2927. goto out;
  2928. combined = skb_header_pointer(skb, 0, sizeof(_combined), &_combined);
  2929. if (!combined || combined->ip6.nexthdr != NEXTHDR_ICMP ||
  2930. (combined->icmp6.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION &&
  2931. combined->icmp6.icmp6_type != NDISC_NEIGHBOUR_ADVERTISEMENT))
  2932. goto out;
  2933. saddr = &combined->ip6.saddr;
  2934. daddr = &combined->ip6.daddr;
  2935. slave_dbg(bond->dev, slave->dev, "%s: %s/%d av %d sv %d sip %pI6c tip %pI6c\n",
  2936. __func__, slave->dev->name, bond_slave_state(slave),
  2937. bond->params.arp_validate, slave_do_arp_validate(bond, slave),
  2938. saddr, daddr);
  2939. curr_active_slave = rcu_dereference(bond->curr_active_slave);
  2940. curr_arp_slave = rcu_dereference(bond->current_arp_slave);
  2941. /* We 'trust' the received ARP enough to validate it if:
  2942. * see bond_arp_rcv().
  2943. */
  2944. if (bond_is_active_slave(slave))
  2945. bond_validate_na(bond, slave, saddr, daddr);
  2946. else if (curr_active_slave &&
  2947. time_after(slave_last_rx(bond, curr_active_slave),
  2948. curr_active_slave->last_link_up))
  2949. bond_validate_na(bond, slave, daddr, saddr);
  2950. else if (curr_arp_slave &&
  2951. bond_time_in_interval(bond, slave_last_tx(curr_arp_slave), 1))
  2952. bond_validate_na(bond, slave, saddr, daddr);
  2953. out:
  2954. return RX_HANDLER_ANOTHER;
  2955. }
  2956. #endif
  2957. int bond_rcv_validate(const struct sk_buff *skb, struct bonding *bond,
  2958. struct slave *slave)
  2959. {
  2960. #if IS_ENABLED(CONFIG_IPV6)
  2961. bool is_ipv6 = skb->protocol == __cpu_to_be16(ETH_P_IPV6);
  2962. #endif
  2963. bool is_arp = skb->protocol == __cpu_to_be16(ETH_P_ARP);
  2964. slave_dbg(bond->dev, slave->dev, "%s: skb->dev %s\n",
  2965. __func__, skb->dev->name);
  2966. /* Use arp validate logic for both ARP and NS */
  2967. if (!slave_do_arp_validate(bond, slave)) {
  2968. if ((slave_do_arp_validate_only(bond) && is_arp) ||
  2969. #if IS_ENABLED(CONFIG_IPV6)
  2970. (slave_do_arp_validate_only(bond) && is_ipv6) ||
  2971. #endif
  2972. !slave_do_arp_validate_only(bond))
  2973. slave->last_rx = jiffies;
  2974. return RX_HANDLER_ANOTHER;
  2975. } else if (is_arp) {
  2976. return bond_arp_rcv(skb, bond, slave);
  2977. #if IS_ENABLED(CONFIG_IPV6)
  2978. } else if (is_ipv6) {
  2979. return bond_na_rcv(skb, bond, slave);
  2980. #endif
  2981. } else {
  2982. return RX_HANDLER_ANOTHER;
  2983. }
  2984. }
  2985. static void bond_send_validate(struct bonding *bond, struct slave *slave)
  2986. {
  2987. bond_arp_send_all(bond, slave);
  2988. #if IS_ENABLED(CONFIG_IPV6)
  2989. bond_ns_send_all(bond, slave);
  2990. #endif
  2991. }
  2992. /* function to verify if we're in the arp_interval timeslice, returns true if
  2993. * (last_act - arp_interval) <= jiffies <= (last_act + mod * arp_interval +
  2994. * arp_interval/2) . the arp_interval/2 is needed for really fast networks.
  2995. */
  2996. static bool bond_time_in_interval(struct bonding *bond, unsigned long last_act,
  2997. int mod)
  2998. {
  2999. int delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
  3000. return time_in_range(jiffies,
  3001. last_act - delta_in_ticks,
  3002. last_act + mod * delta_in_ticks + delta_in_ticks/2);
  3003. }
  3004. /* This function is called regularly to monitor each slave's link
  3005. * ensuring that traffic is being sent and received when arp monitoring
  3006. * is used in load-balancing mode. if the adapter has been dormant, then an
  3007. * arp is transmitted to generate traffic. see activebackup_arp_monitor for
  3008. * arp monitoring in active backup mode.
  3009. */
  3010. static void bond_loadbalance_arp_mon(struct bonding *bond)
  3011. {
  3012. struct slave *slave, *oldcurrent;
  3013. struct list_head *iter;
  3014. int do_failover = 0, slave_state_changed = 0;
  3015. if (!bond_has_slaves(bond))
  3016. goto re_arm;
  3017. rcu_read_lock();
  3018. oldcurrent = rcu_dereference(bond->curr_active_slave);
  3019. /* see if any of the previous devices are up now (i.e. they have
  3020. * xmt and rcv traffic). the curr_active_slave does not come into
  3021. * the picture unless it is null. also, slave->last_link_up is not
  3022. * needed here because we send an arp on each slave and give a slave
  3023. * as long as it needs to get the tx/rx within the delta.
  3024. * TODO: what about up/down delay in arp mode? it wasn't here before
  3025. * so it can wait
  3026. */
  3027. bond_for_each_slave_rcu(bond, slave, iter) {
  3028. unsigned long last_tx = slave_last_tx(slave);
  3029. bond_propose_link_state(slave, BOND_LINK_NOCHANGE);
  3030. if (slave->link != BOND_LINK_UP) {
  3031. if (bond_time_in_interval(bond, last_tx, 1) &&
  3032. bond_time_in_interval(bond, slave->last_rx, 1)) {
  3033. bond_propose_link_state(slave, BOND_LINK_UP);
  3034. slave_state_changed = 1;
  3035. /* primary_slave has no meaning in round-robin
  3036. * mode. the window of a slave being up and
  3037. * curr_active_slave being null after enslaving
  3038. * is closed.
  3039. */
  3040. if (!oldcurrent) {
  3041. slave_info(bond->dev, slave->dev, "link status definitely up\n");
  3042. do_failover = 1;
  3043. } else {
  3044. slave_info(bond->dev, slave->dev, "interface is now up\n");
  3045. }
  3046. }
  3047. } else {
  3048. /* slave->link == BOND_LINK_UP */
  3049. /* not all switches will respond to an arp request
  3050. * when the source ip is 0, so don't take the link down
  3051. * if we don't know our ip yet
  3052. */
  3053. if (!bond_time_in_interval(bond, last_tx, bond->params.missed_max) ||
  3054. !bond_time_in_interval(bond, slave->last_rx, bond->params.missed_max)) {
  3055. bond_propose_link_state(slave, BOND_LINK_DOWN);
  3056. slave_state_changed = 1;
  3057. if (slave->link_failure_count < UINT_MAX)
  3058. slave->link_failure_count++;
  3059. slave_info(bond->dev, slave->dev, "interface is now down\n");
  3060. if (slave == oldcurrent)
  3061. do_failover = 1;
  3062. }
  3063. }
  3064. /* note: if switch is in round-robin mode, all links
  3065. * must tx arp to ensure all links rx an arp - otherwise
  3066. * links may oscillate or not come up at all; if switch is
  3067. * in something like xor mode, there is nothing we can
  3068. * do - all replies will be rx'ed on same link causing slaves
  3069. * to be unstable during low/no traffic periods
  3070. */
  3071. if (bond_slave_is_up(slave))
  3072. bond_send_validate(bond, slave);
  3073. }
  3074. rcu_read_unlock();
  3075. if (do_failover || slave_state_changed) {
  3076. if (!rtnl_trylock())
  3077. goto re_arm;
  3078. bond_for_each_slave(bond, slave, iter) {
  3079. if (slave->link_new_state != BOND_LINK_NOCHANGE)
  3080. slave->link = slave->link_new_state;
  3081. }
  3082. if (slave_state_changed) {
  3083. bond_slave_state_change(bond);
  3084. if (BOND_MODE(bond) == BOND_MODE_XOR)
  3085. bond_update_slave_arr(bond, NULL);
  3086. }
  3087. if (do_failover) {
  3088. block_netpoll_tx();
  3089. bond_select_active_slave(bond);
  3090. unblock_netpoll_tx();
  3091. }
  3092. rtnl_unlock();
  3093. }
  3094. re_arm:
  3095. if (bond->params.arp_interval)
  3096. queue_delayed_work(bond->wq, &bond->arp_work,
  3097. msecs_to_jiffies(bond->params.arp_interval));
  3098. }
  3099. /* Called to inspect slaves for active-backup mode ARP monitor link state
  3100. * changes. Sets proposed link state in slaves to specify what action
  3101. * should take place for the slave. Returns 0 if no changes are found, >0
  3102. * if changes to link states must be committed.
  3103. *
  3104. * Called with rcu_read_lock held.
  3105. */
  3106. static int bond_ab_arp_inspect(struct bonding *bond)
  3107. {
  3108. unsigned long last_tx, last_rx;
  3109. struct list_head *iter;
  3110. struct slave *slave;
  3111. int commit = 0;
  3112. bond_for_each_slave_rcu(bond, slave, iter) {
  3113. bond_propose_link_state(slave, BOND_LINK_NOCHANGE);
  3114. last_rx = slave_last_rx(bond, slave);
  3115. if (slave->link != BOND_LINK_UP) {
  3116. if (bond_time_in_interval(bond, last_rx, 1)) {
  3117. bond_propose_link_state(slave, BOND_LINK_UP);
  3118. commit++;
  3119. } else if (slave->link == BOND_LINK_BACK) {
  3120. bond_propose_link_state(slave, BOND_LINK_FAIL);
  3121. commit++;
  3122. }
  3123. continue;
  3124. }
  3125. /* Give slaves 2*delta after being enslaved or made
  3126. * active. This avoids bouncing, as the last receive
  3127. * times need a full ARP monitor cycle to be updated.
  3128. */
  3129. if (bond_time_in_interval(bond, slave->last_link_up, 2))
  3130. continue;
  3131. /* Backup slave is down if:
  3132. * - No current_arp_slave AND
  3133. * - more than (missed_max+1)*delta since last receive AND
  3134. * - the bond has an IP address
  3135. *
  3136. * Note: a non-null current_arp_slave indicates
  3137. * the curr_active_slave went down and we are
  3138. * searching for a new one; under this condition
  3139. * we only take the curr_active_slave down - this
  3140. * gives each slave a chance to tx/rx traffic
  3141. * before being taken out
  3142. */
  3143. if (!bond_is_active_slave(slave) &&
  3144. !rcu_access_pointer(bond->current_arp_slave) &&
  3145. !bond_time_in_interval(bond, last_rx, bond->params.missed_max + 1)) {
  3146. bond_propose_link_state(slave, BOND_LINK_DOWN);
  3147. commit++;
  3148. }
  3149. /* Active slave is down if:
  3150. * - more than missed_max*delta since transmitting OR
  3151. * - (more than missed_max*delta since receive AND
  3152. * the bond has an IP address)
  3153. */
  3154. last_tx = slave_last_tx(slave);
  3155. if (bond_is_active_slave(slave) &&
  3156. (!bond_time_in_interval(bond, last_tx, bond->params.missed_max) ||
  3157. !bond_time_in_interval(bond, last_rx, bond->params.missed_max))) {
  3158. bond_propose_link_state(slave, BOND_LINK_DOWN);
  3159. commit++;
  3160. }
  3161. }
  3162. return commit;
  3163. }
  3164. /* Called to commit link state changes noted by inspection step of
  3165. * active-backup mode ARP monitor.
  3166. *
  3167. * Called with RTNL hold.
  3168. */
  3169. static void bond_ab_arp_commit(struct bonding *bond)
  3170. {
  3171. bool do_failover = false;
  3172. struct list_head *iter;
  3173. unsigned long last_tx;
  3174. struct slave *slave;
  3175. bond_for_each_slave(bond, slave, iter) {
  3176. switch (slave->link_new_state) {
  3177. case BOND_LINK_NOCHANGE:
  3178. continue;
  3179. case BOND_LINK_UP:
  3180. last_tx = slave_last_tx(slave);
  3181. if (rtnl_dereference(bond->curr_active_slave) != slave ||
  3182. (!rtnl_dereference(bond->curr_active_slave) &&
  3183. bond_time_in_interval(bond, last_tx, 1))) {
  3184. struct slave *current_arp_slave;
  3185. current_arp_slave = rtnl_dereference(bond->current_arp_slave);
  3186. bond_set_slave_link_state(slave, BOND_LINK_UP,
  3187. BOND_SLAVE_NOTIFY_NOW);
  3188. if (current_arp_slave) {
  3189. bond_set_slave_inactive_flags(
  3190. current_arp_slave,
  3191. BOND_SLAVE_NOTIFY_NOW);
  3192. RCU_INIT_POINTER(bond->current_arp_slave, NULL);
  3193. }
  3194. slave_info(bond->dev, slave->dev, "link status definitely up\n");
  3195. if (!rtnl_dereference(bond->curr_active_slave) ||
  3196. slave == rtnl_dereference(bond->primary_slave) ||
  3197. slave->prio > rtnl_dereference(bond->curr_active_slave)->prio)
  3198. do_failover = true;
  3199. }
  3200. continue;
  3201. case BOND_LINK_DOWN:
  3202. if (slave->link_failure_count < UINT_MAX)
  3203. slave->link_failure_count++;
  3204. bond_set_slave_link_state(slave, BOND_LINK_DOWN,
  3205. BOND_SLAVE_NOTIFY_NOW);
  3206. bond_set_slave_inactive_flags(slave,
  3207. BOND_SLAVE_NOTIFY_NOW);
  3208. slave_info(bond->dev, slave->dev, "link status definitely down, disabling slave\n");
  3209. if (slave == rtnl_dereference(bond->curr_active_slave)) {
  3210. RCU_INIT_POINTER(bond->current_arp_slave, NULL);
  3211. do_failover = true;
  3212. }
  3213. continue;
  3214. case BOND_LINK_FAIL:
  3215. bond_set_slave_link_state(slave, BOND_LINK_FAIL,
  3216. BOND_SLAVE_NOTIFY_NOW);
  3217. bond_set_slave_inactive_flags(slave,
  3218. BOND_SLAVE_NOTIFY_NOW);
  3219. /* A slave has just been enslaved and has become
  3220. * the current active slave.
  3221. */
  3222. if (rtnl_dereference(bond->curr_active_slave))
  3223. RCU_INIT_POINTER(bond->current_arp_slave, NULL);
  3224. continue;
  3225. default:
  3226. slave_err(bond->dev, slave->dev,
  3227. "impossible: link_new_state %d on slave\n",
  3228. slave->link_new_state);
  3229. continue;
  3230. }
  3231. }
  3232. if (do_failover) {
  3233. block_netpoll_tx();
  3234. bond_select_active_slave(bond);
  3235. unblock_netpoll_tx();
  3236. }
  3237. bond_set_carrier(bond);
  3238. }
  3239. /* Send ARP probes for active-backup mode ARP monitor.
  3240. *
  3241. * Called with rcu_read_lock held.
  3242. */
  3243. static bool bond_ab_arp_probe(struct bonding *bond)
  3244. {
  3245. struct slave *slave, *before = NULL, *new_slave = NULL,
  3246. *curr_arp_slave = rcu_dereference(bond->current_arp_slave),
  3247. *curr_active_slave = rcu_dereference(bond->curr_active_slave);
  3248. struct list_head *iter;
  3249. bool found = false;
  3250. bool should_notify_rtnl = BOND_SLAVE_NOTIFY_LATER;
  3251. if (curr_arp_slave && curr_active_slave)
  3252. netdev_info(bond->dev, "PROBE: c_arp %s && cas %s BAD\n",
  3253. curr_arp_slave->dev->name,
  3254. curr_active_slave->dev->name);
  3255. if (curr_active_slave) {
  3256. bond_send_validate(bond, curr_active_slave);
  3257. return should_notify_rtnl;
  3258. }
  3259. /* if we don't have a curr_active_slave, search for the next available
  3260. * backup slave from the current_arp_slave and make it the candidate
  3261. * for becoming the curr_active_slave
  3262. */
  3263. if (!curr_arp_slave) {
  3264. curr_arp_slave = bond_first_slave_rcu(bond);
  3265. if (!curr_arp_slave)
  3266. return should_notify_rtnl;
  3267. }
  3268. bond_for_each_slave_rcu(bond, slave, iter) {
  3269. if (!found && !before && bond_slave_is_up(slave))
  3270. before = slave;
  3271. if (found && !new_slave && bond_slave_is_up(slave))
  3272. new_slave = slave;
  3273. /* if the link state is up at this point, we
  3274. * mark it down - this can happen if we have
  3275. * simultaneous link failures and
  3276. * reselect_active_interface doesn't make this
  3277. * one the current slave so it is still marked
  3278. * up when it is actually down
  3279. */
  3280. if (!bond_slave_is_up(slave) && slave->link == BOND_LINK_UP) {
  3281. bond_set_slave_link_state(slave, BOND_LINK_DOWN,
  3282. BOND_SLAVE_NOTIFY_LATER);
  3283. if (slave->link_failure_count < UINT_MAX)
  3284. slave->link_failure_count++;
  3285. bond_set_slave_inactive_flags(slave,
  3286. BOND_SLAVE_NOTIFY_LATER);
  3287. slave_info(bond->dev, slave->dev, "backup interface is now down\n");
  3288. }
  3289. if (slave == curr_arp_slave)
  3290. found = true;
  3291. }
  3292. if (!new_slave && before)
  3293. new_slave = before;
  3294. if (!new_slave)
  3295. goto check_state;
  3296. bond_set_slave_link_state(new_slave, BOND_LINK_BACK,
  3297. BOND_SLAVE_NOTIFY_LATER);
  3298. bond_set_slave_active_flags(new_slave, BOND_SLAVE_NOTIFY_LATER);
  3299. bond_send_validate(bond, new_slave);
  3300. new_slave->last_link_up = jiffies;
  3301. rcu_assign_pointer(bond->current_arp_slave, new_slave);
  3302. check_state:
  3303. bond_for_each_slave_rcu(bond, slave, iter) {
  3304. if (slave->should_notify || slave->should_notify_link) {
  3305. should_notify_rtnl = BOND_SLAVE_NOTIFY_NOW;
  3306. break;
  3307. }
  3308. }
  3309. return should_notify_rtnl;
  3310. }
  3311. static void bond_activebackup_arp_mon(struct bonding *bond)
  3312. {
  3313. bool should_notify_peers = false;
  3314. bool should_notify_rtnl = false;
  3315. int delta_in_ticks;
  3316. delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
  3317. if (!bond_has_slaves(bond))
  3318. goto re_arm;
  3319. rcu_read_lock();
  3320. should_notify_peers = bond_should_notify_peers(bond);
  3321. if (bond_ab_arp_inspect(bond)) {
  3322. rcu_read_unlock();
  3323. /* Race avoidance with bond_close flush of workqueue */
  3324. if (!rtnl_trylock()) {
  3325. delta_in_ticks = 1;
  3326. should_notify_peers = false;
  3327. goto re_arm;
  3328. }
  3329. bond_ab_arp_commit(bond);
  3330. rtnl_unlock();
  3331. rcu_read_lock();
  3332. }
  3333. should_notify_rtnl = bond_ab_arp_probe(bond);
  3334. rcu_read_unlock();
  3335. re_arm:
  3336. if (bond->params.arp_interval)
  3337. queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
  3338. if (should_notify_peers || should_notify_rtnl) {
  3339. if (!rtnl_trylock())
  3340. return;
  3341. if (should_notify_peers) {
  3342. bond->send_peer_notif--;
  3343. call_netdevice_notifiers(NETDEV_NOTIFY_PEERS,
  3344. bond->dev);
  3345. }
  3346. if (should_notify_rtnl) {
  3347. bond_slave_state_notify(bond);
  3348. bond_slave_link_notify(bond);
  3349. }
  3350. rtnl_unlock();
  3351. }
  3352. }
  3353. static void bond_arp_monitor(struct work_struct *work)
  3354. {
  3355. struct bonding *bond = container_of(work, struct bonding,
  3356. arp_work.work);
  3357. if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP)
  3358. bond_activebackup_arp_mon(bond);
  3359. else
  3360. bond_loadbalance_arp_mon(bond);
  3361. }
  3362. /*-------------------------- netdev event handling --------------------------*/
  3363. /* Change device name */
  3364. static int bond_event_changename(struct bonding *bond)
  3365. {
  3366. bond_remove_proc_entry(bond);
  3367. bond_create_proc_entry(bond);
  3368. bond_debug_reregister(bond);
  3369. return NOTIFY_DONE;
  3370. }
  3371. static int bond_master_netdev_event(unsigned long event,
  3372. struct net_device *bond_dev)
  3373. {
  3374. struct bonding *event_bond = netdev_priv(bond_dev);
  3375. netdev_dbg(bond_dev, "%s called\n", __func__);
  3376. switch (event) {
  3377. case NETDEV_CHANGENAME:
  3378. return bond_event_changename(event_bond);
  3379. case NETDEV_UNREGISTER:
  3380. bond_remove_proc_entry(event_bond);
  3381. #ifdef CONFIG_XFRM_OFFLOAD
  3382. xfrm_dev_state_flush(dev_net(bond_dev), bond_dev, true);
  3383. #endif /* CONFIG_XFRM_OFFLOAD */
  3384. break;
  3385. case NETDEV_REGISTER:
  3386. bond_create_proc_entry(event_bond);
  3387. break;
  3388. default:
  3389. break;
  3390. }
  3391. return NOTIFY_DONE;
  3392. }
  3393. static int bond_slave_netdev_event(unsigned long event,
  3394. struct net_device *slave_dev)
  3395. {
  3396. struct slave *slave = bond_slave_get_rtnl(slave_dev), *primary;
  3397. struct bonding *bond;
  3398. struct net_device *bond_dev;
  3399. /* A netdev event can be generated while enslaving a device
  3400. * before netdev_rx_handler_register is called in which case
  3401. * slave will be NULL
  3402. */
  3403. if (!slave) {
  3404. netdev_dbg(slave_dev, "%s called on NULL slave\n", __func__);
  3405. return NOTIFY_DONE;
  3406. }
  3407. bond_dev = slave->bond->dev;
  3408. bond = slave->bond;
  3409. primary = rtnl_dereference(bond->primary_slave);
  3410. slave_dbg(bond_dev, slave_dev, "%s called\n", __func__);
  3411. switch (event) {
  3412. case NETDEV_UNREGISTER:
  3413. if (bond_dev->type != ARPHRD_ETHER)
  3414. bond_release_and_destroy(bond_dev, slave_dev);
  3415. else
  3416. __bond_release_one(bond_dev, slave_dev, false, true);
  3417. break;
  3418. case NETDEV_UP:
  3419. case NETDEV_CHANGE:
  3420. /* For 802.3ad mode only:
  3421. * Getting invalid Speed/Duplex values here will put slave
  3422. * in weird state. Mark it as link-fail if the link was
  3423. * previously up or link-down if it hasn't yet come up, and
  3424. * let link-monitoring (miimon) set it right when correct
  3425. * speeds/duplex are available.
  3426. */
  3427. if (bond_update_speed_duplex(slave) &&
  3428. BOND_MODE(bond) == BOND_MODE_8023AD) {
  3429. if (slave->last_link_up)
  3430. slave->link = BOND_LINK_FAIL;
  3431. else
  3432. slave->link = BOND_LINK_DOWN;
  3433. }
  3434. if (BOND_MODE(bond) == BOND_MODE_8023AD)
  3435. bond_3ad_adapter_speed_duplex_changed(slave);
  3436. fallthrough;
  3437. case NETDEV_DOWN:
  3438. /* Refresh slave-array if applicable!
  3439. * If the setup does not use miimon or arpmon (mode-specific!),
  3440. * then these events will not cause the slave-array to be
  3441. * refreshed. This will cause xmit to use a slave that is not
  3442. * usable. Avoid such situation by refeshing the array at these
  3443. * events. If these (miimon/arpmon) parameters are configured
  3444. * then array gets refreshed twice and that should be fine!
  3445. */
  3446. if (bond_mode_can_use_xmit_hash(bond))
  3447. bond_update_slave_arr(bond, NULL);
  3448. break;
  3449. case NETDEV_CHANGEMTU:
  3450. /* TODO: Should slaves be allowed to
  3451. * independently alter their MTU? For
  3452. * an active-backup bond, slaves need
  3453. * not be the same type of device, so
  3454. * MTUs may vary. For other modes,
  3455. * slaves arguably should have the
  3456. * same MTUs. To do this, we'd need to
  3457. * take over the slave's change_mtu
  3458. * function for the duration of their
  3459. * servitude.
  3460. */
  3461. break;
  3462. case NETDEV_CHANGENAME:
  3463. /* we don't care if we don't have primary set */
  3464. if (!bond_uses_primary(bond) ||
  3465. !bond->params.primary[0])
  3466. break;
  3467. if (slave == primary) {
  3468. /* slave's name changed - he's no longer primary */
  3469. RCU_INIT_POINTER(bond->primary_slave, NULL);
  3470. } else if (!strcmp(slave_dev->name, bond->params.primary)) {
  3471. /* we have a new primary slave */
  3472. rcu_assign_pointer(bond->primary_slave, slave);
  3473. } else { /* we didn't change primary - exit */
  3474. break;
  3475. }
  3476. netdev_info(bond->dev, "Primary slave changed to %s, reselecting active slave\n",
  3477. primary ? slave_dev->name : "none");
  3478. block_netpoll_tx();
  3479. bond_select_active_slave(bond);
  3480. unblock_netpoll_tx();
  3481. break;
  3482. case NETDEV_FEAT_CHANGE:
  3483. if (!bond->notifier_ctx) {
  3484. bond->notifier_ctx = true;
  3485. bond_compute_features(bond);
  3486. bond->notifier_ctx = false;
  3487. }
  3488. break;
  3489. case NETDEV_RESEND_IGMP:
  3490. /* Propagate to master device */
  3491. call_netdevice_notifiers(event, slave->bond->dev);
  3492. break;
  3493. case NETDEV_XDP_FEAT_CHANGE:
  3494. bond_xdp_set_features(bond_dev);
  3495. break;
  3496. default:
  3497. break;
  3498. }
  3499. return NOTIFY_DONE;
  3500. }
  3501. /* bond_netdev_event: handle netdev notifier chain events.
  3502. *
  3503. * This function receives events for the netdev chain. The caller (an
  3504. * ioctl handler calling blocking_notifier_call_chain) holds the necessary
  3505. * locks for us to safely manipulate the slave devices (RTNL lock,
  3506. * dev_probe_lock).
  3507. */
  3508. static int bond_netdev_event(struct notifier_block *this,
  3509. unsigned long event, void *ptr)
  3510. {
  3511. struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
  3512. netdev_dbg(event_dev, "%s received %s\n",
  3513. __func__, netdev_cmd_to_name(event));
  3514. if (!(event_dev->priv_flags & IFF_BONDING))
  3515. return NOTIFY_DONE;
  3516. if (event_dev->flags & IFF_MASTER) {
  3517. int ret;
  3518. ret = bond_master_netdev_event(event, event_dev);
  3519. if (ret != NOTIFY_DONE)
  3520. return ret;
  3521. }
  3522. if (event_dev->flags & IFF_SLAVE)
  3523. return bond_slave_netdev_event(event, event_dev);
  3524. return NOTIFY_DONE;
  3525. }
  3526. static struct notifier_block bond_netdev_notifier = {
  3527. .notifier_call = bond_netdev_event,
  3528. };
  3529. /*---------------------------- Hashing Policies -----------------------------*/
  3530. /* Helper to access data in a packet, with or without a backing skb.
  3531. * If skb is given the data is linearized if necessary via pskb_may_pull.
  3532. */
  3533. static inline const void *bond_pull_data(struct sk_buff *skb,
  3534. const void *data, int hlen, int n)
  3535. {
  3536. if (likely(n <= hlen))
  3537. return data;
  3538. else if (skb && likely(pskb_may_pull(skb, n)))
  3539. return skb->data;
  3540. return NULL;
  3541. }
  3542. /* L2 hash helper */
  3543. static inline u32 bond_eth_hash(struct sk_buff *skb, const void *data, int mhoff, int hlen)
  3544. {
  3545. struct ethhdr *ep;
  3546. data = bond_pull_data(skb, data, hlen, mhoff + sizeof(struct ethhdr));
  3547. if (!data)
  3548. return 0;
  3549. ep = (struct ethhdr *)(data + mhoff);
  3550. return ep->h_dest[5] ^ ep->h_source[5] ^ be16_to_cpu(ep->h_proto);
  3551. }
  3552. static bool bond_flow_ip(struct sk_buff *skb, struct flow_keys *fk, const void *data,
  3553. int hlen, __be16 l2_proto, int *nhoff, int *ip_proto, bool l34)
  3554. {
  3555. const struct ipv6hdr *iph6;
  3556. const struct iphdr *iph;
  3557. if (l2_proto == htons(ETH_P_IP)) {
  3558. data = bond_pull_data(skb, data, hlen, *nhoff + sizeof(*iph));
  3559. if (!data)
  3560. return false;
  3561. iph = (const struct iphdr *)(data + *nhoff);
  3562. iph_to_flow_copy_v4addrs(fk, iph);
  3563. *nhoff += iph->ihl << 2;
  3564. if (!ip_is_fragment(iph))
  3565. *ip_proto = iph->protocol;
  3566. } else if (l2_proto == htons(ETH_P_IPV6)) {
  3567. data = bond_pull_data(skb, data, hlen, *nhoff + sizeof(*iph6));
  3568. if (!data)
  3569. return false;
  3570. iph6 = (const struct ipv6hdr *)(data + *nhoff);
  3571. iph_to_flow_copy_v6addrs(fk, iph6);
  3572. *nhoff += sizeof(*iph6);
  3573. *ip_proto = iph6->nexthdr;
  3574. } else {
  3575. return false;
  3576. }
  3577. if (l34 && *ip_proto >= 0)
  3578. fk->ports.ports = __skb_flow_get_ports(skb, *nhoff, *ip_proto, data, hlen);
  3579. return true;
  3580. }
  3581. static u32 bond_vlan_srcmac_hash(struct sk_buff *skb, const void *data, int mhoff, int hlen)
  3582. {
  3583. u32 srcmac_vendor = 0, srcmac_dev = 0;
  3584. struct ethhdr *mac_hdr;
  3585. u16 vlan = 0;
  3586. int i;
  3587. data = bond_pull_data(skb, data, hlen, mhoff + sizeof(struct ethhdr));
  3588. if (!data)
  3589. return 0;
  3590. mac_hdr = (struct ethhdr *)(data + mhoff);
  3591. for (i = 0; i < 3; i++)
  3592. srcmac_vendor = (srcmac_vendor << 8) | mac_hdr->h_source[i];
  3593. for (i = 3; i < ETH_ALEN; i++)
  3594. srcmac_dev = (srcmac_dev << 8) | mac_hdr->h_source[i];
  3595. if (skb && skb_vlan_tag_present(skb))
  3596. vlan = skb_vlan_tag_get(skb);
  3597. return vlan ^ srcmac_vendor ^ srcmac_dev;
  3598. }
  3599. /* Extract the appropriate headers based on bond's xmit policy */
  3600. static bool bond_flow_dissect(struct bonding *bond, struct sk_buff *skb, const void *data,
  3601. __be16 l2_proto, int nhoff, int hlen, struct flow_keys *fk)
  3602. {
  3603. bool l34 = bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER34;
  3604. int ip_proto = -1;
  3605. switch (bond->params.xmit_policy) {
  3606. case BOND_XMIT_POLICY_ENCAP23:
  3607. case BOND_XMIT_POLICY_ENCAP34:
  3608. memset(fk, 0, sizeof(*fk));
  3609. return __skb_flow_dissect(NULL, skb, &flow_keys_bonding,
  3610. fk, data, l2_proto, nhoff, hlen, 0);
  3611. default:
  3612. break;
  3613. }
  3614. fk->ports.ports = 0;
  3615. memset(&fk->icmp, 0, sizeof(fk->icmp));
  3616. if (!bond_flow_ip(skb, fk, data, hlen, l2_proto, &nhoff, &ip_proto, l34))
  3617. return false;
  3618. /* ICMP error packets contains at least 8 bytes of the header
  3619. * of the packet which generated the error. Use this information
  3620. * to correlate ICMP error packets within the same flow which
  3621. * generated the error.
  3622. */
  3623. if (ip_proto == IPPROTO_ICMP || ip_proto == IPPROTO_ICMPV6) {
  3624. skb_flow_get_icmp_tci(skb, &fk->icmp, data, nhoff, hlen);
  3625. if (ip_proto == IPPROTO_ICMP) {
  3626. if (!icmp_is_err(fk->icmp.type))
  3627. return true;
  3628. nhoff += sizeof(struct icmphdr);
  3629. } else if (ip_proto == IPPROTO_ICMPV6) {
  3630. if (!icmpv6_is_err(fk->icmp.type))
  3631. return true;
  3632. nhoff += sizeof(struct icmp6hdr);
  3633. }
  3634. return bond_flow_ip(skb, fk, data, hlen, l2_proto, &nhoff, &ip_proto, l34);
  3635. }
  3636. return true;
  3637. }
  3638. static u32 bond_ip_hash(u32 hash, struct flow_keys *flow, int xmit_policy)
  3639. {
  3640. hash ^= (__force u32)flow_get_u32_dst(flow) ^
  3641. (__force u32)flow_get_u32_src(flow);
  3642. hash ^= (hash >> 16);
  3643. hash ^= (hash >> 8);
  3644. /* discard lowest hash bit to deal with the common even ports pattern */
  3645. if (xmit_policy == BOND_XMIT_POLICY_LAYER34 ||
  3646. xmit_policy == BOND_XMIT_POLICY_ENCAP34)
  3647. return hash >> 1;
  3648. return hash;
  3649. }
  3650. /* Generate hash based on xmit policy. If @skb is given it is used to linearize
  3651. * the data as required, but this function can be used without it if the data is
  3652. * known to be linear (e.g. with xdp_buff).
  3653. */
  3654. static u32 __bond_xmit_hash(struct bonding *bond, struct sk_buff *skb, const void *data,
  3655. __be16 l2_proto, int mhoff, int nhoff, int hlen)
  3656. {
  3657. struct flow_keys flow;
  3658. u32 hash;
  3659. if (bond->params.xmit_policy == BOND_XMIT_POLICY_VLAN_SRCMAC)
  3660. return bond_vlan_srcmac_hash(skb, data, mhoff, hlen);
  3661. if (bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER2 ||
  3662. !bond_flow_dissect(bond, skb, data, l2_proto, nhoff, hlen, &flow))
  3663. return bond_eth_hash(skb, data, mhoff, hlen);
  3664. if (bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER23 ||
  3665. bond->params.xmit_policy == BOND_XMIT_POLICY_ENCAP23) {
  3666. hash = bond_eth_hash(skb, data, mhoff, hlen);
  3667. } else {
  3668. if (flow.icmp.id)
  3669. memcpy(&hash, &flow.icmp, sizeof(hash));
  3670. else
  3671. memcpy(&hash, &flow.ports.ports, sizeof(hash));
  3672. }
  3673. return bond_ip_hash(hash, &flow, bond->params.xmit_policy);
  3674. }
  3675. /**
  3676. * bond_xmit_hash - generate a hash value based on the xmit policy
  3677. * @bond: bonding device
  3678. * @skb: buffer to use for headers
  3679. *
  3680. * This function will extract the necessary headers from the skb buffer and use
  3681. * them to generate a hash based on the xmit_policy set in the bonding device
  3682. */
  3683. u32 bond_xmit_hash(struct bonding *bond, struct sk_buff *skb)
  3684. {
  3685. if (bond->params.xmit_policy == BOND_XMIT_POLICY_ENCAP34 &&
  3686. skb->l4_hash)
  3687. return skb->hash;
  3688. return __bond_xmit_hash(bond, skb, skb->data, skb->protocol,
  3689. 0, skb_network_offset(skb),
  3690. skb_headlen(skb));
  3691. }
  3692. /**
  3693. * bond_xmit_hash_xdp - generate a hash value based on the xmit policy
  3694. * @bond: bonding device
  3695. * @xdp: buffer to use for headers
  3696. *
  3697. * The XDP variant of bond_xmit_hash.
  3698. */
  3699. static u32 bond_xmit_hash_xdp(struct bonding *bond, struct xdp_buff *xdp)
  3700. {
  3701. struct ethhdr *eth;
  3702. if (xdp->data + sizeof(struct ethhdr) > xdp->data_end)
  3703. return 0;
  3704. eth = (struct ethhdr *)xdp->data;
  3705. return __bond_xmit_hash(bond, NULL, xdp->data, eth->h_proto, 0,
  3706. sizeof(struct ethhdr), xdp->data_end - xdp->data);
  3707. }
  3708. /*-------------------------- Device entry points ----------------------------*/
  3709. void bond_work_init_all(struct bonding *bond)
  3710. {
  3711. INIT_DELAYED_WORK(&bond->mcast_work,
  3712. bond_resend_igmp_join_requests_delayed);
  3713. INIT_DELAYED_WORK(&bond->alb_work, bond_alb_monitor);
  3714. INIT_DELAYED_WORK(&bond->mii_work, bond_mii_monitor);
  3715. INIT_DELAYED_WORK(&bond->arp_work, bond_arp_monitor);
  3716. INIT_DELAYED_WORK(&bond->ad_work, bond_3ad_state_machine_handler);
  3717. INIT_DELAYED_WORK(&bond->slave_arr_work, bond_slave_arr_handler);
  3718. }
  3719. static void bond_work_cancel_all(struct bonding *bond)
  3720. {
  3721. cancel_delayed_work_sync(&bond->mii_work);
  3722. cancel_delayed_work_sync(&bond->arp_work);
  3723. cancel_delayed_work_sync(&bond->alb_work);
  3724. cancel_delayed_work_sync(&bond->ad_work);
  3725. cancel_delayed_work_sync(&bond->mcast_work);
  3726. cancel_delayed_work_sync(&bond->slave_arr_work);
  3727. }
  3728. static int bond_open(struct net_device *bond_dev)
  3729. {
  3730. struct bonding *bond = netdev_priv(bond_dev);
  3731. struct list_head *iter;
  3732. struct slave *slave;
  3733. if (BOND_MODE(bond) == BOND_MODE_ROUNDROBIN && !bond->rr_tx_counter) {
  3734. bond->rr_tx_counter = alloc_percpu(u32);
  3735. if (!bond->rr_tx_counter)
  3736. return -ENOMEM;
  3737. }
  3738. /* reset slave->backup and slave->inactive */
  3739. if (bond_has_slaves(bond)) {
  3740. bond_for_each_slave(bond, slave, iter) {
  3741. if (bond_uses_primary(bond) &&
  3742. slave != rcu_access_pointer(bond->curr_active_slave)) {
  3743. bond_set_slave_inactive_flags(slave,
  3744. BOND_SLAVE_NOTIFY_NOW);
  3745. } else if (BOND_MODE(bond) != BOND_MODE_8023AD) {
  3746. bond_set_slave_active_flags(slave,
  3747. BOND_SLAVE_NOTIFY_NOW);
  3748. }
  3749. }
  3750. }
  3751. if (bond_is_lb(bond)) {
  3752. /* bond_alb_initialize must be called before the timer
  3753. * is started.
  3754. */
  3755. if (bond_alb_initialize(bond, (BOND_MODE(bond) == BOND_MODE_ALB)))
  3756. return -ENOMEM;
  3757. if (bond->params.tlb_dynamic_lb || BOND_MODE(bond) == BOND_MODE_ALB)
  3758. queue_delayed_work(bond->wq, &bond->alb_work, 0);
  3759. }
  3760. if (bond->params.miimon) /* link check interval, in milliseconds. */
  3761. queue_delayed_work(bond->wq, &bond->mii_work, 0);
  3762. if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
  3763. queue_delayed_work(bond->wq, &bond->arp_work, 0);
  3764. bond->recv_probe = bond_rcv_validate;
  3765. }
  3766. if (BOND_MODE(bond) == BOND_MODE_8023AD) {
  3767. queue_delayed_work(bond->wq, &bond->ad_work, 0);
  3768. /* register to receive LACPDUs */
  3769. bond->recv_probe = bond_3ad_lacpdu_recv;
  3770. bond_3ad_initiate_agg_selection(bond, 1);
  3771. bond_for_each_slave(bond, slave, iter)
  3772. dev_mc_add(slave->dev, lacpdu_mcast_addr);
  3773. }
  3774. if (bond_mode_can_use_xmit_hash(bond))
  3775. bond_update_slave_arr(bond, NULL);
  3776. return 0;
  3777. }
  3778. static int bond_close(struct net_device *bond_dev)
  3779. {
  3780. struct bonding *bond = netdev_priv(bond_dev);
  3781. struct slave *slave;
  3782. bond_work_cancel_all(bond);
  3783. bond->send_peer_notif = 0;
  3784. if (bond_is_lb(bond))
  3785. bond_alb_deinitialize(bond);
  3786. bond->recv_probe = NULL;
  3787. if (bond_uses_primary(bond)) {
  3788. rcu_read_lock();
  3789. slave = rcu_dereference(bond->curr_active_slave);
  3790. if (slave)
  3791. bond_hw_addr_flush(bond_dev, slave->dev);
  3792. rcu_read_unlock();
  3793. } else {
  3794. struct list_head *iter;
  3795. bond_for_each_slave(bond, slave, iter)
  3796. bond_hw_addr_flush(bond_dev, slave->dev);
  3797. }
  3798. return 0;
  3799. }
  3800. /* fold stats, assuming all rtnl_link_stats64 fields are u64, but
  3801. * that some drivers can provide 32bit values only.
  3802. */
  3803. static void bond_fold_stats(struct rtnl_link_stats64 *_res,
  3804. const struct rtnl_link_stats64 *_new,
  3805. const struct rtnl_link_stats64 *_old)
  3806. {
  3807. const u64 *new = (const u64 *)_new;
  3808. const u64 *old = (const u64 *)_old;
  3809. u64 *res = (u64 *)_res;
  3810. int i;
  3811. for (i = 0; i < sizeof(*_res) / sizeof(u64); i++) {
  3812. u64 nv = new[i];
  3813. u64 ov = old[i];
  3814. s64 delta = nv - ov;
  3815. /* detects if this particular field is 32bit only */
  3816. if (((nv | ov) >> 32) == 0)
  3817. delta = (s64)(s32)((u32)nv - (u32)ov);
  3818. /* filter anomalies, some drivers reset their stats
  3819. * at down/up events.
  3820. */
  3821. if (delta > 0)
  3822. res[i] += delta;
  3823. }
  3824. }
  3825. #ifdef CONFIG_LOCKDEP
  3826. static int bond_get_lowest_level_rcu(struct net_device *dev)
  3827. {
  3828. struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
  3829. struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
  3830. int cur = 0, max = 0;
  3831. now = dev;
  3832. iter = &dev->adj_list.lower;
  3833. while (1) {
  3834. next = NULL;
  3835. while (1) {
  3836. ldev = netdev_next_lower_dev_rcu(now, &iter);
  3837. if (!ldev)
  3838. break;
  3839. next = ldev;
  3840. niter = &ldev->adj_list.lower;
  3841. dev_stack[cur] = now;
  3842. iter_stack[cur++] = iter;
  3843. if (max <= cur)
  3844. max = cur;
  3845. break;
  3846. }
  3847. if (!next) {
  3848. if (!cur)
  3849. return max;
  3850. next = dev_stack[--cur];
  3851. niter = iter_stack[cur];
  3852. }
  3853. now = next;
  3854. iter = niter;
  3855. }
  3856. return max;
  3857. }
  3858. #endif
  3859. static void bond_get_stats(struct net_device *bond_dev,
  3860. struct rtnl_link_stats64 *stats)
  3861. {
  3862. struct bonding *bond = netdev_priv(bond_dev);
  3863. struct rtnl_link_stats64 temp;
  3864. struct list_head *iter;
  3865. struct slave *slave;
  3866. int nest_level = 0;
  3867. rcu_read_lock();
  3868. #ifdef CONFIG_LOCKDEP
  3869. nest_level = bond_get_lowest_level_rcu(bond_dev);
  3870. #endif
  3871. spin_lock_nested(&bond->stats_lock, nest_level);
  3872. memcpy(stats, &bond->bond_stats, sizeof(*stats));
  3873. bond_for_each_slave_rcu(bond, slave, iter) {
  3874. const struct rtnl_link_stats64 *new =
  3875. dev_get_stats(slave->dev, &temp);
  3876. bond_fold_stats(stats, new, &slave->slave_stats);
  3877. /* save off the slave stats for the next run */
  3878. memcpy(&slave->slave_stats, new, sizeof(*new));
  3879. }
  3880. memcpy(&bond->bond_stats, stats, sizeof(*stats));
  3881. spin_unlock(&bond->stats_lock);
  3882. rcu_read_unlock();
  3883. }
  3884. static int bond_eth_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
  3885. {
  3886. struct bonding *bond = netdev_priv(bond_dev);
  3887. struct mii_ioctl_data *mii = NULL;
  3888. netdev_dbg(bond_dev, "bond_eth_ioctl: cmd=%d\n", cmd);
  3889. switch (cmd) {
  3890. case SIOCGMIIPHY:
  3891. mii = if_mii(ifr);
  3892. if (!mii)
  3893. return -EINVAL;
  3894. mii->phy_id = 0;
  3895. fallthrough;
  3896. case SIOCGMIIREG:
  3897. /* We do this again just in case we were called by SIOCGMIIREG
  3898. * instead of SIOCGMIIPHY.
  3899. */
  3900. mii = if_mii(ifr);
  3901. if (!mii)
  3902. return -EINVAL;
  3903. if (mii->reg_num == 1) {
  3904. mii->val_out = 0;
  3905. if (netif_carrier_ok(bond->dev))
  3906. mii->val_out = BMSR_LSTATUS;
  3907. }
  3908. break;
  3909. default:
  3910. return -EOPNOTSUPP;
  3911. }
  3912. return 0;
  3913. }
  3914. static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
  3915. {
  3916. struct bonding *bond = netdev_priv(bond_dev);
  3917. struct net_device *slave_dev = NULL;
  3918. struct ifbond k_binfo;
  3919. struct ifbond __user *u_binfo = NULL;
  3920. struct ifslave k_sinfo;
  3921. struct ifslave __user *u_sinfo = NULL;
  3922. struct bond_opt_value newval;
  3923. struct net *net;
  3924. int res = 0;
  3925. netdev_dbg(bond_dev, "bond_ioctl: cmd=%d\n", cmd);
  3926. switch (cmd) {
  3927. case SIOCBONDINFOQUERY:
  3928. u_binfo = (struct ifbond __user *)ifr->ifr_data;
  3929. if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond)))
  3930. return -EFAULT;
  3931. bond_info_query(bond_dev, &k_binfo);
  3932. if (copy_to_user(u_binfo, &k_binfo, sizeof(ifbond)))
  3933. return -EFAULT;
  3934. return 0;
  3935. case SIOCBONDSLAVEINFOQUERY:
  3936. u_sinfo = (struct ifslave __user *)ifr->ifr_data;
  3937. if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave)))
  3938. return -EFAULT;
  3939. res = bond_slave_info_query(bond_dev, &k_sinfo);
  3940. if (res == 0 &&
  3941. copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave)))
  3942. return -EFAULT;
  3943. return res;
  3944. default:
  3945. break;
  3946. }
  3947. net = dev_net(bond_dev);
  3948. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  3949. return -EPERM;
  3950. slave_dev = __dev_get_by_name(net, ifr->ifr_slave);
  3951. slave_dbg(bond_dev, slave_dev, "slave_dev=%p:\n", slave_dev);
  3952. if (!slave_dev)
  3953. return -ENODEV;
  3954. switch (cmd) {
  3955. case SIOCBONDENSLAVE:
  3956. res = bond_enslave(bond_dev, slave_dev, NULL);
  3957. break;
  3958. case SIOCBONDRELEASE:
  3959. res = bond_release(bond_dev, slave_dev);
  3960. break;
  3961. case SIOCBONDSETHWADDR:
  3962. res = bond_set_dev_addr(bond_dev, slave_dev);
  3963. break;
  3964. case SIOCBONDCHANGEACTIVE:
  3965. bond_opt_initstr(&newval, slave_dev->name);
  3966. res = __bond_opt_set_notify(bond, BOND_OPT_ACTIVE_SLAVE,
  3967. &newval);
  3968. break;
  3969. default:
  3970. res = -EOPNOTSUPP;
  3971. }
  3972. return res;
  3973. }
  3974. static int bond_siocdevprivate(struct net_device *bond_dev, struct ifreq *ifr,
  3975. void __user *data, int cmd)
  3976. {
  3977. struct ifreq ifrdata = { .ifr_data = data };
  3978. switch (cmd) {
  3979. case BOND_INFO_QUERY_OLD:
  3980. return bond_do_ioctl(bond_dev, &ifrdata, SIOCBONDINFOQUERY);
  3981. case BOND_SLAVE_INFO_QUERY_OLD:
  3982. return bond_do_ioctl(bond_dev, &ifrdata, SIOCBONDSLAVEINFOQUERY);
  3983. case BOND_ENSLAVE_OLD:
  3984. return bond_do_ioctl(bond_dev, ifr, SIOCBONDENSLAVE);
  3985. case BOND_RELEASE_OLD:
  3986. return bond_do_ioctl(bond_dev, ifr, SIOCBONDRELEASE);
  3987. case BOND_SETHWADDR_OLD:
  3988. return bond_do_ioctl(bond_dev, ifr, SIOCBONDSETHWADDR);
  3989. case BOND_CHANGE_ACTIVE_OLD:
  3990. return bond_do_ioctl(bond_dev, ifr, SIOCBONDCHANGEACTIVE);
  3991. }
  3992. return -EOPNOTSUPP;
  3993. }
  3994. static void bond_change_rx_flags(struct net_device *bond_dev, int change)
  3995. {
  3996. struct bonding *bond = netdev_priv(bond_dev);
  3997. if (change & IFF_PROMISC)
  3998. bond_set_promiscuity(bond,
  3999. bond_dev->flags & IFF_PROMISC ? 1 : -1);
  4000. if (change & IFF_ALLMULTI)
  4001. bond_set_allmulti(bond,
  4002. bond_dev->flags & IFF_ALLMULTI ? 1 : -1);
  4003. }
  4004. static void bond_set_rx_mode(struct net_device *bond_dev)
  4005. {
  4006. struct bonding *bond = netdev_priv(bond_dev);
  4007. struct list_head *iter;
  4008. struct slave *slave;
  4009. rcu_read_lock();
  4010. if (bond_uses_primary(bond)) {
  4011. slave = rcu_dereference(bond->curr_active_slave);
  4012. if (slave) {
  4013. dev_uc_sync(slave->dev, bond_dev);
  4014. dev_mc_sync(slave->dev, bond_dev);
  4015. }
  4016. } else {
  4017. bond_for_each_slave_rcu(bond, slave, iter) {
  4018. dev_uc_sync_multiple(slave->dev, bond_dev);
  4019. dev_mc_sync_multiple(slave->dev, bond_dev);
  4020. }
  4021. }
  4022. rcu_read_unlock();
  4023. }
  4024. static int bond_neigh_init(struct neighbour *n)
  4025. {
  4026. struct bonding *bond = netdev_priv(n->dev);
  4027. const struct net_device_ops *slave_ops;
  4028. struct neigh_parms parms;
  4029. struct slave *slave;
  4030. int ret = 0;
  4031. rcu_read_lock();
  4032. slave = bond_first_slave_rcu(bond);
  4033. if (!slave)
  4034. goto out;
  4035. slave_ops = slave->dev->netdev_ops;
  4036. if (!slave_ops->ndo_neigh_setup)
  4037. goto out;
  4038. /* TODO: find another way [1] to implement this.
  4039. * Passing a zeroed structure is fragile,
  4040. * but at least we do not pass garbage.
  4041. *
  4042. * [1] One way would be that ndo_neigh_setup() never touch
  4043. * struct neigh_parms, but propagate the new neigh_setup()
  4044. * back to ___neigh_create() / neigh_parms_alloc()
  4045. */
  4046. memset(&parms, 0, sizeof(parms));
  4047. ret = slave_ops->ndo_neigh_setup(slave->dev, &parms);
  4048. if (ret)
  4049. goto out;
  4050. if (parms.neigh_setup)
  4051. ret = parms.neigh_setup(n);
  4052. out:
  4053. rcu_read_unlock();
  4054. return ret;
  4055. }
  4056. /* The bonding ndo_neigh_setup is called at init time beofre any
  4057. * slave exists. So we must declare proxy setup function which will
  4058. * be used at run time to resolve the actual slave neigh param setup.
  4059. *
  4060. * It's also called by master devices (such as vlans) to setup their
  4061. * underlying devices. In that case - do nothing, we're already set up from
  4062. * our init.
  4063. */
  4064. static int bond_neigh_setup(struct net_device *dev,
  4065. struct neigh_parms *parms)
  4066. {
  4067. /* modify only our neigh_parms */
  4068. if (parms->dev == dev)
  4069. parms->neigh_setup = bond_neigh_init;
  4070. return 0;
  4071. }
  4072. /* Change the MTU of all of a master's slaves to match the master */
  4073. static int bond_change_mtu(struct net_device *bond_dev, int new_mtu)
  4074. {
  4075. struct bonding *bond = netdev_priv(bond_dev);
  4076. struct slave *slave, *rollback_slave;
  4077. struct list_head *iter;
  4078. int res = 0;
  4079. netdev_dbg(bond_dev, "bond=%p, new_mtu=%d\n", bond, new_mtu);
  4080. bond_for_each_slave(bond, slave, iter) {
  4081. slave_dbg(bond_dev, slave->dev, "s %p c_m %p\n",
  4082. slave, slave->dev->netdev_ops->ndo_change_mtu);
  4083. res = dev_set_mtu(slave->dev, new_mtu);
  4084. if (res) {
  4085. /* If we failed to set the slave's mtu to the new value
  4086. * we must abort the operation even in ACTIVE_BACKUP
  4087. * mode, because if we allow the backup slaves to have
  4088. * different mtu values than the active slave we'll
  4089. * need to change their mtu when doing a failover. That
  4090. * means changing their mtu from timer context, which
  4091. * is probably not a good idea.
  4092. */
  4093. slave_dbg(bond_dev, slave->dev, "err %d setting mtu to %d\n",
  4094. res, new_mtu);
  4095. goto unwind;
  4096. }
  4097. }
  4098. WRITE_ONCE(bond_dev->mtu, new_mtu);
  4099. return 0;
  4100. unwind:
  4101. /* unwind from head to the slave that failed */
  4102. bond_for_each_slave(bond, rollback_slave, iter) {
  4103. int tmp_res;
  4104. if (rollback_slave == slave)
  4105. break;
  4106. tmp_res = dev_set_mtu(rollback_slave->dev, bond_dev->mtu);
  4107. if (tmp_res)
  4108. slave_dbg(bond_dev, rollback_slave->dev, "unwind err %d\n",
  4109. tmp_res);
  4110. }
  4111. return res;
  4112. }
  4113. /* Change HW address
  4114. *
  4115. * Note that many devices must be down to change the HW address, and
  4116. * downing the master releases all slaves. We can make bonds full of
  4117. * bonding devices to test this, however.
  4118. */
  4119. static int bond_set_mac_address(struct net_device *bond_dev, void *addr)
  4120. {
  4121. struct bonding *bond = netdev_priv(bond_dev);
  4122. struct slave *slave, *rollback_slave;
  4123. struct sockaddr_storage *ss = addr, tmp_ss;
  4124. struct list_head *iter;
  4125. int res = 0;
  4126. if (BOND_MODE(bond) == BOND_MODE_ALB)
  4127. return bond_alb_set_mac_address(bond_dev, addr);
  4128. netdev_dbg(bond_dev, "%s: bond=%p\n", __func__, bond);
  4129. /* If fail_over_mac is enabled, do nothing and return success.
  4130. * Returning an error causes ifenslave to fail.
  4131. */
  4132. if (bond->params.fail_over_mac &&
  4133. BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP)
  4134. return 0;
  4135. if (!is_valid_ether_addr(ss->__data))
  4136. return -EADDRNOTAVAIL;
  4137. bond_for_each_slave(bond, slave, iter) {
  4138. slave_dbg(bond_dev, slave->dev, "%s: slave=%p\n",
  4139. __func__, slave);
  4140. res = dev_set_mac_address(slave->dev, addr, NULL);
  4141. if (res) {
  4142. /* TODO: consider downing the slave
  4143. * and retry ?
  4144. * User should expect communications
  4145. * breakage anyway until ARP finish
  4146. * updating, so...
  4147. */
  4148. slave_dbg(bond_dev, slave->dev, "%s: err %d\n",
  4149. __func__, res);
  4150. goto unwind;
  4151. }
  4152. }
  4153. /* success */
  4154. dev_addr_set(bond_dev, ss->__data);
  4155. return 0;
  4156. unwind:
  4157. memcpy(tmp_ss.__data, bond_dev->dev_addr, bond_dev->addr_len);
  4158. tmp_ss.ss_family = bond_dev->type;
  4159. /* unwind from head to the slave that failed */
  4160. bond_for_each_slave(bond, rollback_slave, iter) {
  4161. int tmp_res;
  4162. if (rollback_slave == slave)
  4163. break;
  4164. tmp_res = dev_set_mac_address(rollback_slave->dev,
  4165. (struct sockaddr *)&tmp_ss, NULL);
  4166. if (tmp_res) {
  4167. slave_dbg(bond_dev, rollback_slave->dev, "%s: unwind err %d\n",
  4168. __func__, tmp_res);
  4169. }
  4170. }
  4171. return res;
  4172. }
  4173. /**
  4174. * bond_get_slave_by_id - get xmit slave with slave_id
  4175. * @bond: bonding device that is transmitting
  4176. * @slave_id: slave id up to slave_cnt-1 through which to transmit
  4177. *
  4178. * This function tries to get slave with slave_id but in case
  4179. * it fails, it tries to find the first available slave for transmission.
  4180. */
  4181. static struct slave *bond_get_slave_by_id(struct bonding *bond,
  4182. int slave_id)
  4183. {
  4184. struct list_head *iter;
  4185. struct slave *slave;
  4186. int i = slave_id;
  4187. /* Here we start from the slave with slave_id */
  4188. bond_for_each_slave_rcu(bond, slave, iter) {
  4189. if (--i < 0) {
  4190. if (bond_slave_can_tx(slave))
  4191. return slave;
  4192. }
  4193. }
  4194. /* Here we start from the first slave up to slave_id */
  4195. i = slave_id;
  4196. bond_for_each_slave_rcu(bond, slave, iter) {
  4197. if (--i < 0)
  4198. break;
  4199. if (bond_slave_can_tx(slave))
  4200. return slave;
  4201. }
  4202. /* no slave that can tx has been found */
  4203. return NULL;
  4204. }
  4205. /**
  4206. * bond_rr_gen_slave_id - generate slave id based on packets_per_slave
  4207. * @bond: bonding device to use
  4208. *
  4209. * Based on the value of the bonding device's packets_per_slave parameter
  4210. * this function generates a slave id, which is usually used as the next
  4211. * slave to transmit through.
  4212. */
  4213. static u32 bond_rr_gen_slave_id(struct bonding *bond)
  4214. {
  4215. u32 slave_id;
  4216. struct reciprocal_value reciprocal_packets_per_slave;
  4217. int packets_per_slave = bond->params.packets_per_slave;
  4218. switch (packets_per_slave) {
  4219. case 0:
  4220. slave_id = get_random_u32();
  4221. break;
  4222. case 1:
  4223. slave_id = this_cpu_inc_return(*bond->rr_tx_counter);
  4224. break;
  4225. default:
  4226. reciprocal_packets_per_slave =
  4227. bond->params.reciprocal_packets_per_slave;
  4228. slave_id = this_cpu_inc_return(*bond->rr_tx_counter);
  4229. slave_id = reciprocal_divide(slave_id,
  4230. reciprocal_packets_per_slave);
  4231. break;
  4232. }
  4233. return slave_id;
  4234. }
  4235. static struct slave *bond_xmit_roundrobin_slave_get(struct bonding *bond,
  4236. struct sk_buff *skb)
  4237. {
  4238. struct slave *slave;
  4239. int slave_cnt;
  4240. u32 slave_id;
  4241. /* Start with the curr_active_slave that joined the bond as the
  4242. * default for sending IGMP traffic. For failover purposes one
  4243. * needs to maintain some consistency for the interface that will
  4244. * send the join/membership reports. The curr_active_slave found
  4245. * will send all of this type of traffic.
  4246. */
  4247. if (skb->protocol == htons(ETH_P_IP)) {
  4248. int noff = skb_network_offset(skb);
  4249. struct iphdr *iph;
  4250. if (unlikely(!pskb_may_pull(skb, noff + sizeof(*iph))))
  4251. goto non_igmp;
  4252. iph = ip_hdr(skb);
  4253. if (iph->protocol == IPPROTO_IGMP) {
  4254. slave = rcu_dereference(bond->curr_active_slave);
  4255. if (slave)
  4256. return slave;
  4257. return bond_get_slave_by_id(bond, 0);
  4258. }
  4259. }
  4260. non_igmp:
  4261. slave_cnt = READ_ONCE(bond->slave_cnt);
  4262. if (likely(slave_cnt)) {
  4263. slave_id = bond_rr_gen_slave_id(bond) % slave_cnt;
  4264. return bond_get_slave_by_id(bond, slave_id);
  4265. }
  4266. return NULL;
  4267. }
  4268. static struct slave *bond_xdp_xmit_roundrobin_slave_get(struct bonding *bond,
  4269. struct xdp_buff *xdp)
  4270. {
  4271. struct slave *slave;
  4272. int slave_cnt;
  4273. u32 slave_id;
  4274. const struct ethhdr *eth;
  4275. void *data = xdp->data;
  4276. if (data + sizeof(struct ethhdr) > xdp->data_end)
  4277. goto non_igmp;
  4278. eth = (struct ethhdr *)data;
  4279. data += sizeof(struct ethhdr);
  4280. /* See comment on IGMP in bond_xmit_roundrobin_slave_get() */
  4281. if (eth->h_proto == htons(ETH_P_IP)) {
  4282. const struct iphdr *iph;
  4283. if (data + sizeof(struct iphdr) > xdp->data_end)
  4284. goto non_igmp;
  4285. iph = (struct iphdr *)data;
  4286. if (iph->protocol == IPPROTO_IGMP) {
  4287. slave = rcu_dereference(bond->curr_active_slave);
  4288. if (slave)
  4289. return slave;
  4290. return bond_get_slave_by_id(bond, 0);
  4291. }
  4292. }
  4293. non_igmp:
  4294. slave_cnt = READ_ONCE(bond->slave_cnt);
  4295. if (likely(slave_cnt)) {
  4296. slave_id = bond_rr_gen_slave_id(bond) % slave_cnt;
  4297. return bond_get_slave_by_id(bond, slave_id);
  4298. }
  4299. return NULL;
  4300. }
  4301. static netdev_tx_t bond_xmit_roundrobin(struct sk_buff *skb,
  4302. struct net_device *bond_dev)
  4303. {
  4304. struct bonding *bond = netdev_priv(bond_dev);
  4305. struct slave *slave;
  4306. slave = bond_xmit_roundrobin_slave_get(bond, skb);
  4307. if (likely(slave))
  4308. return bond_dev_queue_xmit(bond, skb, slave->dev);
  4309. return bond_tx_drop(bond_dev, skb);
  4310. }
  4311. static struct slave *bond_xmit_activebackup_slave_get(struct bonding *bond)
  4312. {
  4313. return rcu_dereference(bond->curr_active_slave);
  4314. }
  4315. /* In active-backup mode, we know that bond->curr_active_slave is always valid if
  4316. * the bond has a usable interface.
  4317. */
  4318. static netdev_tx_t bond_xmit_activebackup(struct sk_buff *skb,
  4319. struct net_device *bond_dev)
  4320. {
  4321. struct bonding *bond = netdev_priv(bond_dev);
  4322. struct slave *slave;
  4323. slave = bond_xmit_activebackup_slave_get(bond);
  4324. if (slave)
  4325. return bond_dev_queue_xmit(bond, skb, slave->dev);
  4326. return bond_tx_drop(bond_dev, skb);
  4327. }
  4328. /* Use this to update slave_array when (a) it's not appropriate to update
  4329. * slave_array right away (note that update_slave_array() may sleep)
  4330. * and / or (b) RTNL is not held.
  4331. */
  4332. void bond_slave_arr_work_rearm(struct bonding *bond, unsigned long delay)
  4333. {
  4334. queue_delayed_work(bond->wq, &bond->slave_arr_work, delay);
  4335. }
  4336. /* Slave array work handler. Holds only RTNL */
  4337. static void bond_slave_arr_handler(struct work_struct *work)
  4338. {
  4339. struct bonding *bond = container_of(work, struct bonding,
  4340. slave_arr_work.work);
  4341. int ret;
  4342. if (!rtnl_trylock())
  4343. goto err;
  4344. ret = bond_update_slave_arr(bond, NULL);
  4345. rtnl_unlock();
  4346. if (ret) {
  4347. pr_warn_ratelimited("Failed to update slave array from WT\n");
  4348. goto err;
  4349. }
  4350. return;
  4351. err:
  4352. bond_slave_arr_work_rearm(bond, 1);
  4353. }
  4354. static void bond_skip_slave(struct bond_up_slave *slaves,
  4355. struct slave *skipslave)
  4356. {
  4357. int idx;
  4358. /* Rare situation where caller has asked to skip a specific
  4359. * slave but allocation failed (most likely!). BTW this is
  4360. * only possible when the call is initiated from
  4361. * __bond_release_one(). In this situation; overwrite the
  4362. * skipslave entry in the array with the last entry from the
  4363. * array to avoid a situation where the xmit path may choose
  4364. * this to-be-skipped slave to send a packet out.
  4365. */
  4366. for (idx = 0; slaves && idx < slaves->count; idx++) {
  4367. if (skipslave == slaves->arr[idx]) {
  4368. slaves->arr[idx] =
  4369. slaves->arr[slaves->count - 1];
  4370. slaves->count--;
  4371. break;
  4372. }
  4373. }
  4374. }
  4375. static void bond_set_slave_arr(struct bonding *bond,
  4376. struct bond_up_slave *usable_slaves,
  4377. struct bond_up_slave *all_slaves)
  4378. {
  4379. struct bond_up_slave *usable, *all;
  4380. usable = rtnl_dereference(bond->usable_slaves);
  4381. rcu_assign_pointer(bond->usable_slaves, usable_slaves);
  4382. kfree_rcu(usable, rcu);
  4383. all = rtnl_dereference(bond->all_slaves);
  4384. rcu_assign_pointer(bond->all_slaves, all_slaves);
  4385. kfree_rcu(all, rcu);
  4386. }
  4387. static void bond_reset_slave_arr(struct bonding *bond)
  4388. {
  4389. bond_set_slave_arr(bond, NULL, NULL);
  4390. }
  4391. /* Build the usable slaves array in control path for modes that use xmit-hash
  4392. * to determine the slave interface -
  4393. * (a) BOND_MODE_8023AD
  4394. * (b) BOND_MODE_XOR
  4395. * (c) (BOND_MODE_TLB || BOND_MODE_ALB) && tlb_dynamic_lb == 0
  4396. *
  4397. * The caller is expected to hold RTNL only and NO other lock!
  4398. */
  4399. int bond_update_slave_arr(struct bonding *bond, struct slave *skipslave)
  4400. {
  4401. struct bond_up_slave *usable_slaves = NULL, *all_slaves = NULL;
  4402. struct slave *slave;
  4403. struct list_head *iter;
  4404. int agg_id = 0;
  4405. int ret = 0;
  4406. might_sleep();
  4407. usable_slaves = kzalloc(struct_size(usable_slaves, arr,
  4408. bond->slave_cnt), GFP_KERNEL);
  4409. all_slaves = kzalloc(struct_size(all_slaves, arr,
  4410. bond->slave_cnt), GFP_KERNEL);
  4411. if (!usable_slaves || !all_slaves) {
  4412. ret = -ENOMEM;
  4413. goto out;
  4414. }
  4415. if (BOND_MODE(bond) == BOND_MODE_8023AD) {
  4416. struct ad_info ad_info;
  4417. spin_lock_bh(&bond->mode_lock);
  4418. if (bond_3ad_get_active_agg_info(bond, &ad_info)) {
  4419. spin_unlock_bh(&bond->mode_lock);
  4420. pr_debug("bond_3ad_get_active_agg_info failed\n");
  4421. /* No active aggragator means it's not safe to use
  4422. * the previous array.
  4423. */
  4424. bond_reset_slave_arr(bond);
  4425. goto out;
  4426. }
  4427. spin_unlock_bh(&bond->mode_lock);
  4428. agg_id = ad_info.aggregator_id;
  4429. }
  4430. bond_for_each_slave(bond, slave, iter) {
  4431. if (skipslave == slave)
  4432. continue;
  4433. all_slaves->arr[all_slaves->count++] = slave;
  4434. if (BOND_MODE(bond) == BOND_MODE_8023AD) {
  4435. struct aggregator *agg;
  4436. agg = SLAVE_AD_INFO(slave)->port.aggregator;
  4437. if (!agg || agg->aggregator_identifier != agg_id)
  4438. continue;
  4439. }
  4440. if (!bond_slave_can_tx(slave))
  4441. continue;
  4442. slave_dbg(bond->dev, slave->dev, "Adding slave to tx hash array[%d]\n",
  4443. usable_slaves->count);
  4444. usable_slaves->arr[usable_slaves->count++] = slave;
  4445. }
  4446. bond_set_slave_arr(bond, usable_slaves, all_slaves);
  4447. return ret;
  4448. out:
  4449. if (ret != 0 && skipslave) {
  4450. bond_skip_slave(rtnl_dereference(bond->all_slaves),
  4451. skipslave);
  4452. bond_skip_slave(rtnl_dereference(bond->usable_slaves),
  4453. skipslave);
  4454. }
  4455. kfree_rcu(all_slaves, rcu);
  4456. kfree_rcu(usable_slaves, rcu);
  4457. return ret;
  4458. }
  4459. static struct slave *bond_xmit_3ad_xor_slave_get(struct bonding *bond,
  4460. struct sk_buff *skb,
  4461. struct bond_up_slave *slaves)
  4462. {
  4463. struct slave *slave;
  4464. unsigned int count;
  4465. u32 hash;
  4466. hash = bond_xmit_hash(bond, skb);
  4467. count = slaves ? READ_ONCE(slaves->count) : 0;
  4468. if (unlikely(!count))
  4469. return NULL;
  4470. slave = slaves->arr[hash % count];
  4471. return slave;
  4472. }
  4473. static struct slave *bond_xdp_xmit_3ad_xor_slave_get(struct bonding *bond,
  4474. struct xdp_buff *xdp)
  4475. {
  4476. struct bond_up_slave *slaves;
  4477. unsigned int count;
  4478. u32 hash;
  4479. hash = bond_xmit_hash_xdp(bond, xdp);
  4480. slaves = rcu_dereference(bond->usable_slaves);
  4481. count = slaves ? READ_ONCE(slaves->count) : 0;
  4482. if (unlikely(!count))
  4483. return NULL;
  4484. return slaves->arr[hash % count];
  4485. }
  4486. /* Use this Xmit function for 3AD as well as XOR modes. The current
  4487. * usable slave array is formed in the control path. The xmit function
  4488. * just calculates hash and sends the packet out.
  4489. */
  4490. static netdev_tx_t bond_3ad_xor_xmit(struct sk_buff *skb,
  4491. struct net_device *dev)
  4492. {
  4493. struct bonding *bond = netdev_priv(dev);
  4494. struct bond_up_slave *slaves;
  4495. struct slave *slave;
  4496. slaves = rcu_dereference(bond->usable_slaves);
  4497. slave = bond_xmit_3ad_xor_slave_get(bond, skb, slaves);
  4498. if (likely(slave))
  4499. return bond_dev_queue_xmit(bond, skb, slave->dev);
  4500. return bond_tx_drop(dev, skb);
  4501. }
  4502. /* in broadcast mode, we send everything to all usable interfaces. */
  4503. static netdev_tx_t bond_xmit_broadcast(struct sk_buff *skb,
  4504. struct net_device *bond_dev)
  4505. {
  4506. struct bonding *bond = netdev_priv(bond_dev);
  4507. struct slave *slave = NULL;
  4508. struct list_head *iter;
  4509. bool xmit_suc = false;
  4510. bool skb_used = false;
  4511. bond_for_each_slave_rcu(bond, slave, iter) {
  4512. struct sk_buff *skb2;
  4513. if (!(bond_slave_is_up(slave) && slave->link == BOND_LINK_UP))
  4514. continue;
  4515. if (bond_is_last_slave(bond, slave)) {
  4516. skb2 = skb;
  4517. skb_used = true;
  4518. } else {
  4519. skb2 = skb_clone(skb, GFP_ATOMIC);
  4520. if (!skb2) {
  4521. net_err_ratelimited("%s: Error: %s: skb_clone() failed\n",
  4522. bond_dev->name, __func__);
  4523. continue;
  4524. }
  4525. }
  4526. if (bond_dev_queue_xmit(bond, skb2, slave->dev) == NETDEV_TX_OK)
  4527. xmit_suc = true;
  4528. }
  4529. if (!skb_used)
  4530. dev_kfree_skb_any(skb);
  4531. if (xmit_suc)
  4532. return NETDEV_TX_OK;
  4533. dev_core_stats_tx_dropped_inc(bond_dev);
  4534. return NET_XMIT_DROP;
  4535. }
  4536. /*------------------------- Device initialization ---------------------------*/
  4537. /* Lookup the slave that corresponds to a qid */
  4538. static inline int bond_slave_override(struct bonding *bond,
  4539. struct sk_buff *skb)
  4540. {
  4541. struct slave *slave = NULL;
  4542. struct list_head *iter;
  4543. if (!skb_rx_queue_recorded(skb))
  4544. return 1;
  4545. /* Find out if any slaves have the same mapping as this skb. */
  4546. bond_for_each_slave_rcu(bond, slave, iter) {
  4547. if (READ_ONCE(slave->queue_id) == skb_get_queue_mapping(skb)) {
  4548. if (bond_slave_is_up(slave) &&
  4549. slave->link == BOND_LINK_UP) {
  4550. bond_dev_queue_xmit(bond, skb, slave->dev);
  4551. return 0;
  4552. }
  4553. /* If the slave isn't UP, use default transmit policy. */
  4554. break;
  4555. }
  4556. }
  4557. return 1;
  4558. }
  4559. static u16 bond_select_queue(struct net_device *dev, struct sk_buff *skb,
  4560. struct net_device *sb_dev)
  4561. {
  4562. /* This helper function exists to help dev_pick_tx get the correct
  4563. * destination queue. Using a helper function skips a call to
  4564. * skb_tx_hash and will put the skbs in the queue we expect on their
  4565. * way down to the bonding driver.
  4566. */
  4567. u16 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0;
  4568. /* Save the original txq to restore before passing to the driver */
  4569. qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb_get_queue_mapping(skb);
  4570. if (unlikely(txq >= dev->real_num_tx_queues)) {
  4571. do {
  4572. txq -= dev->real_num_tx_queues;
  4573. } while (txq >= dev->real_num_tx_queues);
  4574. }
  4575. return txq;
  4576. }
  4577. static struct net_device *bond_xmit_get_slave(struct net_device *master_dev,
  4578. struct sk_buff *skb,
  4579. bool all_slaves)
  4580. {
  4581. struct bonding *bond = netdev_priv(master_dev);
  4582. struct bond_up_slave *slaves;
  4583. struct slave *slave = NULL;
  4584. switch (BOND_MODE(bond)) {
  4585. case BOND_MODE_ROUNDROBIN:
  4586. slave = bond_xmit_roundrobin_slave_get(bond, skb);
  4587. break;
  4588. case BOND_MODE_ACTIVEBACKUP:
  4589. slave = bond_xmit_activebackup_slave_get(bond);
  4590. break;
  4591. case BOND_MODE_8023AD:
  4592. case BOND_MODE_XOR:
  4593. if (all_slaves)
  4594. slaves = rcu_dereference(bond->all_slaves);
  4595. else
  4596. slaves = rcu_dereference(bond->usable_slaves);
  4597. slave = bond_xmit_3ad_xor_slave_get(bond, skb, slaves);
  4598. break;
  4599. case BOND_MODE_BROADCAST:
  4600. break;
  4601. case BOND_MODE_ALB:
  4602. slave = bond_xmit_alb_slave_get(bond, skb);
  4603. break;
  4604. case BOND_MODE_TLB:
  4605. slave = bond_xmit_tlb_slave_get(bond, skb);
  4606. break;
  4607. default:
  4608. /* Should never happen, mode already checked */
  4609. WARN_ONCE(true, "Unknown bonding mode");
  4610. break;
  4611. }
  4612. if (slave)
  4613. return slave->dev;
  4614. return NULL;
  4615. }
  4616. static void bond_sk_to_flow(struct sock *sk, struct flow_keys *flow)
  4617. {
  4618. switch (sk->sk_family) {
  4619. #if IS_ENABLED(CONFIG_IPV6)
  4620. case AF_INET6:
  4621. if (ipv6_only_sock(sk) ||
  4622. ipv6_addr_type(&sk->sk_v6_daddr) != IPV6_ADDR_MAPPED) {
  4623. flow->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
  4624. flow->addrs.v6addrs.src = inet6_sk(sk)->saddr;
  4625. flow->addrs.v6addrs.dst = sk->sk_v6_daddr;
  4626. break;
  4627. }
  4628. fallthrough;
  4629. #endif
  4630. default: /* AF_INET */
  4631. flow->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
  4632. flow->addrs.v4addrs.src = inet_sk(sk)->inet_rcv_saddr;
  4633. flow->addrs.v4addrs.dst = inet_sk(sk)->inet_daddr;
  4634. break;
  4635. }
  4636. flow->ports.src = inet_sk(sk)->inet_sport;
  4637. flow->ports.dst = inet_sk(sk)->inet_dport;
  4638. }
  4639. /**
  4640. * bond_sk_hash_l34 - generate a hash value based on the socket's L3 and L4 fields
  4641. * @sk: socket to use for headers
  4642. *
  4643. * This function will extract the necessary field from the socket and use
  4644. * them to generate a hash based on the LAYER34 xmit_policy.
  4645. * Assumes that sk is a TCP or UDP socket.
  4646. */
  4647. static u32 bond_sk_hash_l34(struct sock *sk)
  4648. {
  4649. struct flow_keys flow;
  4650. u32 hash;
  4651. bond_sk_to_flow(sk, &flow);
  4652. /* L4 */
  4653. memcpy(&hash, &flow.ports.ports, sizeof(hash));
  4654. /* L3 */
  4655. return bond_ip_hash(hash, &flow, BOND_XMIT_POLICY_LAYER34);
  4656. }
  4657. static struct net_device *__bond_sk_get_lower_dev(struct bonding *bond,
  4658. struct sock *sk)
  4659. {
  4660. struct bond_up_slave *slaves;
  4661. struct slave *slave;
  4662. unsigned int count;
  4663. u32 hash;
  4664. slaves = rcu_dereference(bond->usable_slaves);
  4665. count = slaves ? READ_ONCE(slaves->count) : 0;
  4666. if (unlikely(!count))
  4667. return NULL;
  4668. hash = bond_sk_hash_l34(sk);
  4669. slave = slaves->arr[hash % count];
  4670. return slave->dev;
  4671. }
  4672. static struct net_device *bond_sk_get_lower_dev(struct net_device *dev,
  4673. struct sock *sk)
  4674. {
  4675. struct bonding *bond = netdev_priv(dev);
  4676. struct net_device *lower = NULL;
  4677. rcu_read_lock();
  4678. if (bond_sk_check(bond))
  4679. lower = __bond_sk_get_lower_dev(bond, sk);
  4680. rcu_read_unlock();
  4681. return lower;
  4682. }
  4683. #if IS_ENABLED(CONFIG_TLS_DEVICE)
  4684. static netdev_tx_t bond_tls_device_xmit(struct bonding *bond, struct sk_buff *skb,
  4685. struct net_device *dev)
  4686. {
  4687. struct net_device *tls_netdev = rcu_dereference(tls_get_ctx(skb->sk)->netdev);
  4688. /* tls_netdev might become NULL, even if tls_is_skb_tx_device_offloaded
  4689. * was true, if tls_device_down is running in parallel, but it's OK,
  4690. * because bond_get_slave_by_dev has a NULL check.
  4691. */
  4692. if (likely(bond_get_slave_by_dev(bond, tls_netdev)))
  4693. return bond_dev_queue_xmit(bond, skb, tls_netdev);
  4694. return bond_tx_drop(dev, skb);
  4695. }
  4696. #endif
  4697. static netdev_tx_t __bond_start_xmit(struct sk_buff *skb, struct net_device *dev)
  4698. {
  4699. struct bonding *bond = netdev_priv(dev);
  4700. if (bond_should_override_tx_queue(bond) &&
  4701. !bond_slave_override(bond, skb))
  4702. return NETDEV_TX_OK;
  4703. #if IS_ENABLED(CONFIG_TLS_DEVICE)
  4704. if (tls_is_skb_tx_device_offloaded(skb))
  4705. return bond_tls_device_xmit(bond, skb, dev);
  4706. #endif
  4707. switch (BOND_MODE(bond)) {
  4708. case BOND_MODE_ROUNDROBIN:
  4709. return bond_xmit_roundrobin(skb, dev);
  4710. case BOND_MODE_ACTIVEBACKUP:
  4711. return bond_xmit_activebackup(skb, dev);
  4712. case BOND_MODE_8023AD:
  4713. case BOND_MODE_XOR:
  4714. return bond_3ad_xor_xmit(skb, dev);
  4715. case BOND_MODE_BROADCAST:
  4716. return bond_xmit_broadcast(skb, dev);
  4717. case BOND_MODE_ALB:
  4718. return bond_alb_xmit(skb, dev);
  4719. case BOND_MODE_TLB:
  4720. return bond_tlb_xmit(skb, dev);
  4721. default:
  4722. /* Should never happen, mode already checked */
  4723. netdev_err(dev, "Unknown bonding mode %d\n", BOND_MODE(bond));
  4724. WARN_ON_ONCE(1);
  4725. return bond_tx_drop(dev, skb);
  4726. }
  4727. }
  4728. static netdev_tx_t bond_start_xmit(struct sk_buff *skb, struct net_device *dev)
  4729. {
  4730. struct bonding *bond = netdev_priv(dev);
  4731. netdev_tx_t ret = NETDEV_TX_OK;
  4732. /* If we risk deadlock from transmitting this in the
  4733. * netpoll path, tell netpoll to queue the frame for later tx
  4734. */
  4735. if (unlikely(is_netpoll_tx_blocked(dev)))
  4736. return NETDEV_TX_BUSY;
  4737. rcu_read_lock();
  4738. if (bond_has_slaves(bond))
  4739. ret = __bond_start_xmit(skb, dev);
  4740. else
  4741. ret = bond_tx_drop(dev, skb);
  4742. rcu_read_unlock();
  4743. return ret;
  4744. }
  4745. static struct net_device *
  4746. bond_xdp_get_xmit_slave(struct net_device *bond_dev, struct xdp_buff *xdp)
  4747. {
  4748. struct bonding *bond = netdev_priv(bond_dev);
  4749. struct slave *slave;
  4750. /* Caller needs to hold rcu_read_lock() */
  4751. switch (BOND_MODE(bond)) {
  4752. case BOND_MODE_ROUNDROBIN:
  4753. slave = bond_xdp_xmit_roundrobin_slave_get(bond, xdp);
  4754. break;
  4755. case BOND_MODE_ACTIVEBACKUP:
  4756. slave = bond_xmit_activebackup_slave_get(bond);
  4757. break;
  4758. case BOND_MODE_8023AD:
  4759. case BOND_MODE_XOR:
  4760. slave = bond_xdp_xmit_3ad_xor_slave_get(bond, xdp);
  4761. break;
  4762. default:
  4763. if (net_ratelimit())
  4764. netdev_err(bond_dev, "Unknown bonding mode %d for xdp xmit\n",
  4765. BOND_MODE(bond));
  4766. return NULL;
  4767. }
  4768. if (slave)
  4769. return slave->dev;
  4770. return NULL;
  4771. }
  4772. static int bond_xdp_xmit(struct net_device *bond_dev,
  4773. int n, struct xdp_frame **frames, u32 flags)
  4774. {
  4775. int nxmit, err = -ENXIO;
  4776. rcu_read_lock();
  4777. for (nxmit = 0; nxmit < n; nxmit++) {
  4778. struct xdp_frame *frame = frames[nxmit];
  4779. struct xdp_frame *frames1[] = {frame};
  4780. struct net_device *slave_dev;
  4781. struct xdp_buff xdp;
  4782. xdp_convert_frame_to_buff(frame, &xdp);
  4783. slave_dev = bond_xdp_get_xmit_slave(bond_dev, &xdp);
  4784. if (!slave_dev) {
  4785. err = -ENXIO;
  4786. break;
  4787. }
  4788. err = slave_dev->netdev_ops->ndo_xdp_xmit(slave_dev, 1, frames1, flags);
  4789. if (err < 1)
  4790. break;
  4791. }
  4792. rcu_read_unlock();
  4793. /* If error happened on the first frame then we can pass the error up, otherwise
  4794. * report the number of frames that were xmitted.
  4795. */
  4796. if (err < 0)
  4797. return (nxmit == 0 ? err : nxmit);
  4798. return nxmit;
  4799. }
  4800. static int bond_xdp_set(struct net_device *dev, struct bpf_prog *prog,
  4801. struct netlink_ext_ack *extack)
  4802. {
  4803. struct bonding *bond = netdev_priv(dev);
  4804. struct list_head *iter;
  4805. struct slave *slave, *rollback_slave;
  4806. struct bpf_prog *old_prog;
  4807. struct netdev_bpf xdp = {
  4808. .command = XDP_SETUP_PROG,
  4809. .flags = 0,
  4810. .prog = prog,
  4811. .extack = extack,
  4812. };
  4813. int err;
  4814. ASSERT_RTNL();
  4815. if (!bond_xdp_check(bond))
  4816. return -EOPNOTSUPP;
  4817. old_prog = bond->xdp_prog;
  4818. bond->xdp_prog = prog;
  4819. bond_for_each_slave(bond, slave, iter) {
  4820. struct net_device *slave_dev = slave->dev;
  4821. if (!slave_dev->netdev_ops->ndo_bpf ||
  4822. !slave_dev->netdev_ops->ndo_xdp_xmit) {
  4823. SLAVE_NL_ERR(dev, slave_dev, extack,
  4824. "Slave device does not support XDP");
  4825. err = -EOPNOTSUPP;
  4826. goto err;
  4827. }
  4828. if (dev_xdp_prog_count(slave_dev) > 0) {
  4829. SLAVE_NL_ERR(dev, slave_dev, extack,
  4830. "Slave has XDP program loaded, please unload before enslaving");
  4831. err = -EOPNOTSUPP;
  4832. goto err;
  4833. }
  4834. err = dev_xdp_propagate(slave_dev, &xdp);
  4835. if (err < 0) {
  4836. /* ndo_bpf() sets extack error message */
  4837. slave_err(dev, slave_dev, "Error %d calling ndo_bpf\n", err);
  4838. goto err;
  4839. }
  4840. if (prog)
  4841. bpf_prog_inc(prog);
  4842. }
  4843. if (prog) {
  4844. static_branch_inc(&bpf_master_redirect_enabled_key);
  4845. } else if (old_prog) {
  4846. bpf_prog_put(old_prog);
  4847. static_branch_dec(&bpf_master_redirect_enabled_key);
  4848. }
  4849. return 0;
  4850. err:
  4851. /* unwind the program changes */
  4852. bond->xdp_prog = old_prog;
  4853. xdp.prog = old_prog;
  4854. xdp.extack = NULL; /* do not overwrite original error */
  4855. bond_for_each_slave(bond, rollback_slave, iter) {
  4856. struct net_device *slave_dev = rollback_slave->dev;
  4857. int err_unwind;
  4858. if (slave == rollback_slave)
  4859. break;
  4860. err_unwind = dev_xdp_propagate(slave_dev, &xdp);
  4861. if (err_unwind < 0)
  4862. slave_err(dev, slave_dev,
  4863. "Error %d when unwinding XDP program change\n", err_unwind);
  4864. else if (xdp.prog)
  4865. bpf_prog_inc(xdp.prog);
  4866. }
  4867. return err;
  4868. }
  4869. static int bond_xdp(struct net_device *dev, struct netdev_bpf *xdp)
  4870. {
  4871. switch (xdp->command) {
  4872. case XDP_SETUP_PROG:
  4873. return bond_xdp_set(dev, xdp->prog, xdp->extack);
  4874. default:
  4875. return -EINVAL;
  4876. }
  4877. }
  4878. static u32 bond_mode_bcast_speed(struct slave *slave, u32 speed)
  4879. {
  4880. if (speed == 0 || speed == SPEED_UNKNOWN)
  4881. speed = slave->speed;
  4882. else
  4883. speed = min(speed, slave->speed);
  4884. return speed;
  4885. }
  4886. /* Set the BOND_PHC_INDEX flag to notify user space */
  4887. static int bond_set_phc_index_flag(struct kernel_hwtstamp_config *kernel_cfg)
  4888. {
  4889. struct ifreq *ifr = kernel_cfg->ifr;
  4890. struct hwtstamp_config cfg;
  4891. if (kernel_cfg->copied_to_user) {
  4892. /* Lower device has a legacy implementation */
  4893. if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
  4894. return -EFAULT;
  4895. cfg.flags |= HWTSTAMP_FLAG_BONDED_PHC_INDEX;
  4896. if (copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)))
  4897. return -EFAULT;
  4898. } else {
  4899. kernel_cfg->flags |= HWTSTAMP_FLAG_BONDED_PHC_INDEX;
  4900. }
  4901. return 0;
  4902. }
  4903. static int bond_hwtstamp_get(struct net_device *dev,
  4904. struct kernel_hwtstamp_config *cfg)
  4905. {
  4906. struct bonding *bond = netdev_priv(dev);
  4907. struct net_device *real_dev;
  4908. int err;
  4909. real_dev = bond_option_active_slave_get_rcu(bond);
  4910. if (!real_dev)
  4911. return -EOPNOTSUPP;
  4912. err = generic_hwtstamp_get_lower(real_dev, cfg);
  4913. if (err)
  4914. return err;
  4915. return bond_set_phc_index_flag(cfg);
  4916. }
  4917. static int bond_hwtstamp_set(struct net_device *dev,
  4918. struct kernel_hwtstamp_config *cfg,
  4919. struct netlink_ext_ack *extack)
  4920. {
  4921. struct bonding *bond = netdev_priv(dev);
  4922. struct net_device *real_dev;
  4923. int err;
  4924. if (!(cfg->flags & HWTSTAMP_FLAG_BONDED_PHC_INDEX))
  4925. return -EOPNOTSUPP;
  4926. real_dev = bond_option_active_slave_get_rcu(bond);
  4927. if (!real_dev)
  4928. return -EOPNOTSUPP;
  4929. err = generic_hwtstamp_set_lower(real_dev, cfg, extack);
  4930. if (err)
  4931. return err;
  4932. return bond_set_phc_index_flag(cfg);
  4933. }
  4934. static int bond_ethtool_get_link_ksettings(struct net_device *bond_dev,
  4935. struct ethtool_link_ksettings *cmd)
  4936. {
  4937. struct bonding *bond = netdev_priv(bond_dev);
  4938. struct list_head *iter;
  4939. struct slave *slave;
  4940. u32 speed = 0;
  4941. cmd->base.duplex = DUPLEX_UNKNOWN;
  4942. cmd->base.port = PORT_OTHER;
  4943. /* Since bond_slave_can_tx returns false for all inactive or down slaves, we
  4944. * do not need to check mode. Though link speed might not represent
  4945. * the true receive or transmit bandwidth (not all modes are symmetric)
  4946. * this is an accurate maximum.
  4947. */
  4948. bond_for_each_slave(bond, slave, iter) {
  4949. if (bond_slave_can_tx(slave)) {
  4950. bond_update_speed_duplex(slave);
  4951. if (slave->speed != SPEED_UNKNOWN) {
  4952. if (BOND_MODE(bond) == BOND_MODE_BROADCAST)
  4953. speed = bond_mode_bcast_speed(slave,
  4954. speed);
  4955. else
  4956. speed += slave->speed;
  4957. }
  4958. if (cmd->base.duplex == DUPLEX_UNKNOWN &&
  4959. slave->duplex != DUPLEX_UNKNOWN)
  4960. cmd->base.duplex = slave->duplex;
  4961. }
  4962. }
  4963. cmd->base.speed = speed ? : SPEED_UNKNOWN;
  4964. return 0;
  4965. }
  4966. static void bond_ethtool_get_drvinfo(struct net_device *bond_dev,
  4967. struct ethtool_drvinfo *drvinfo)
  4968. {
  4969. strscpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
  4970. snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version), "%d",
  4971. BOND_ABI_VERSION);
  4972. }
  4973. static int bond_ethtool_get_ts_info(struct net_device *bond_dev,
  4974. struct kernel_ethtool_ts_info *info)
  4975. {
  4976. struct bonding *bond = netdev_priv(bond_dev);
  4977. struct kernel_ethtool_ts_info ts_info;
  4978. struct net_device *real_dev;
  4979. bool sw_tx_support = false;
  4980. struct list_head *iter;
  4981. struct slave *slave;
  4982. int ret = 0;
  4983. rcu_read_lock();
  4984. real_dev = bond_option_active_slave_get_rcu(bond);
  4985. dev_hold(real_dev);
  4986. rcu_read_unlock();
  4987. if (real_dev) {
  4988. ret = ethtool_get_ts_info_by_layer(real_dev, info);
  4989. } else {
  4990. /* Check if all slaves support software tx timestamping */
  4991. rcu_read_lock();
  4992. bond_for_each_slave_rcu(bond, slave, iter) {
  4993. ret = ethtool_get_ts_info_by_layer(slave->dev, &ts_info);
  4994. if (!ret && (ts_info.so_timestamping & SOF_TIMESTAMPING_TX_SOFTWARE)) {
  4995. sw_tx_support = true;
  4996. continue;
  4997. }
  4998. sw_tx_support = false;
  4999. break;
  5000. }
  5001. rcu_read_unlock();
  5002. }
  5003. if (sw_tx_support)
  5004. info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE;
  5005. dev_put(real_dev);
  5006. return ret;
  5007. }
  5008. static const struct ethtool_ops bond_ethtool_ops = {
  5009. .get_drvinfo = bond_ethtool_get_drvinfo,
  5010. .get_link = ethtool_op_get_link,
  5011. .get_link_ksettings = bond_ethtool_get_link_ksettings,
  5012. .get_ts_info = bond_ethtool_get_ts_info,
  5013. };
  5014. static const struct net_device_ops bond_netdev_ops = {
  5015. .ndo_init = bond_init,
  5016. .ndo_uninit = bond_uninit,
  5017. .ndo_open = bond_open,
  5018. .ndo_stop = bond_close,
  5019. .ndo_start_xmit = bond_start_xmit,
  5020. .ndo_select_queue = bond_select_queue,
  5021. .ndo_get_stats64 = bond_get_stats,
  5022. .ndo_eth_ioctl = bond_eth_ioctl,
  5023. .ndo_siocbond = bond_do_ioctl,
  5024. .ndo_siocdevprivate = bond_siocdevprivate,
  5025. .ndo_change_rx_flags = bond_change_rx_flags,
  5026. .ndo_set_rx_mode = bond_set_rx_mode,
  5027. .ndo_change_mtu = bond_change_mtu,
  5028. .ndo_set_mac_address = bond_set_mac_address,
  5029. .ndo_neigh_setup = bond_neigh_setup,
  5030. .ndo_vlan_rx_add_vid = bond_vlan_rx_add_vid,
  5031. .ndo_vlan_rx_kill_vid = bond_vlan_rx_kill_vid,
  5032. #ifdef CONFIG_NET_POLL_CONTROLLER
  5033. .ndo_netpoll_setup = bond_netpoll_setup,
  5034. .ndo_netpoll_cleanup = bond_netpoll_cleanup,
  5035. .ndo_poll_controller = bond_poll_controller,
  5036. #endif
  5037. .ndo_add_slave = bond_enslave,
  5038. .ndo_del_slave = bond_release,
  5039. .ndo_fix_features = bond_fix_features,
  5040. .ndo_features_check = passthru_features_check,
  5041. .ndo_get_xmit_slave = bond_xmit_get_slave,
  5042. .ndo_sk_get_lower_dev = bond_sk_get_lower_dev,
  5043. .ndo_bpf = bond_xdp,
  5044. .ndo_xdp_xmit = bond_xdp_xmit,
  5045. .ndo_xdp_get_xmit_slave = bond_xdp_get_xmit_slave,
  5046. .ndo_hwtstamp_get = bond_hwtstamp_get,
  5047. .ndo_hwtstamp_set = bond_hwtstamp_set,
  5048. };
  5049. static const struct device_type bond_type = {
  5050. .name = "bond",
  5051. };
  5052. static void bond_destructor(struct net_device *bond_dev)
  5053. {
  5054. struct bonding *bond = netdev_priv(bond_dev);
  5055. if (bond->wq)
  5056. destroy_workqueue(bond->wq);
  5057. free_percpu(bond->rr_tx_counter);
  5058. }
  5059. void bond_setup(struct net_device *bond_dev)
  5060. {
  5061. struct bonding *bond = netdev_priv(bond_dev);
  5062. spin_lock_init(&bond->mode_lock);
  5063. bond->params = bonding_defaults;
  5064. /* Initialize pointers */
  5065. bond->dev = bond_dev;
  5066. /* Initialize the device entry points */
  5067. ether_setup(bond_dev);
  5068. bond_dev->max_mtu = ETH_MAX_MTU;
  5069. bond_dev->netdev_ops = &bond_netdev_ops;
  5070. bond_dev->ethtool_ops = &bond_ethtool_ops;
  5071. bond_dev->needs_free_netdev = true;
  5072. bond_dev->priv_destructor = bond_destructor;
  5073. SET_NETDEV_DEVTYPE(bond_dev, &bond_type);
  5074. /* Initialize the device options */
  5075. bond_dev->flags |= IFF_MASTER;
  5076. bond_dev->priv_flags |= IFF_BONDING | IFF_UNICAST_FLT | IFF_NO_QUEUE;
  5077. bond_dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
  5078. #ifdef CONFIG_XFRM_OFFLOAD
  5079. /* set up xfrm device ops (only supported in active-backup right now) */
  5080. bond_dev->xfrmdev_ops = &bond_xfrmdev_ops;
  5081. INIT_LIST_HEAD(&bond->ipsec_list);
  5082. mutex_init(&bond->ipsec_lock);
  5083. #endif /* CONFIG_XFRM_OFFLOAD */
  5084. /* don't acquire bond device's netif_tx_lock when transmitting */
  5085. bond_dev->lltx = true;
  5086. /* Don't allow bond devices to change network namespaces. */
  5087. bond_dev->netns_local = true;
  5088. /* By default, we declare the bond to be fully
  5089. * VLAN hardware accelerated capable. Special
  5090. * care is taken in the various xmit functions
  5091. * when there are slaves that are not hw accel
  5092. * capable
  5093. */
  5094. bond_dev->hw_features = BOND_VLAN_FEATURES |
  5095. NETIF_F_HW_VLAN_CTAG_RX |
  5096. NETIF_F_HW_VLAN_CTAG_FILTER |
  5097. NETIF_F_HW_VLAN_STAG_RX |
  5098. NETIF_F_HW_VLAN_STAG_FILTER;
  5099. bond_dev->hw_features |= NETIF_F_GSO_ENCAP_ALL;
  5100. bond_dev->features |= bond_dev->hw_features;
  5101. bond_dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
  5102. #ifdef CONFIG_XFRM_OFFLOAD
  5103. bond_dev->hw_features |= BOND_XFRM_FEATURES;
  5104. /* Only enable XFRM features if this is an active-backup config */
  5105. if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP)
  5106. bond_dev->features |= BOND_XFRM_FEATURES;
  5107. #endif /* CONFIG_XFRM_OFFLOAD */
  5108. }
  5109. /* Destroy a bonding device.
  5110. * Must be under rtnl_lock when this function is called.
  5111. */
  5112. static void bond_uninit(struct net_device *bond_dev)
  5113. {
  5114. struct bonding *bond = netdev_priv(bond_dev);
  5115. struct list_head *iter;
  5116. struct slave *slave;
  5117. bond_netpoll_cleanup(bond_dev);
  5118. /* Release the bonded slaves */
  5119. bond_for_each_slave(bond, slave, iter)
  5120. __bond_release_one(bond_dev, slave->dev, true, true);
  5121. netdev_info(bond_dev, "Released all slaves\n");
  5122. #ifdef CONFIG_XFRM_OFFLOAD
  5123. mutex_destroy(&bond->ipsec_lock);
  5124. #endif /* CONFIG_XFRM_OFFLOAD */
  5125. bond_set_slave_arr(bond, NULL, NULL);
  5126. list_del_rcu(&bond->bond_list);
  5127. bond_debug_unregister(bond);
  5128. }
  5129. /*------------------------- Module initialization ---------------------------*/
  5130. static int __init bond_check_params(struct bond_params *params)
  5131. {
  5132. int arp_validate_value, fail_over_mac_value, primary_reselect_value, i;
  5133. struct bond_opt_value newval;
  5134. const struct bond_opt_value *valptr;
  5135. int arp_all_targets_value = 0;
  5136. u16 ad_actor_sys_prio = 0;
  5137. u16 ad_user_port_key = 0;
  5138. __be32 arp_target[BOND_MAX_ARP_TARGETS] = { 0 };
  5139. int arp_ip_count;
  5140. int bond_mode = BOND_MODE_ROUNDROBIN;
  5141. int xmit_hashtype = BOND_XMIT_POLICY_LAYER2;
  5142. int lacp_fast = 0;
  5143. int tlb_dynamic_lb;
  5144. /* Convert string parameters. */
  5145. if (mode) {
  5146. bond_opt_initstr(&newval, mode);
  5147. valptr = bond_opt_parse(bond_opt_get(BOND_OPT_MODE), &newval);
  5148. if (!valptr) {
  5149. pr_err("Error: Invalid bonding mode \"%s\"\n", mode);
  5150. return -EINVAL;
  5151. }
  5152. bond_mode = valptr->value;
  5153. }
  5154. if (xmit_hash_policy) {
  5155. if (bond_mode == BOND_MODE_ROUNDROBIN ||
  5156. bond_mode == BOND_MODE_ACTIVEBACKUP ||
  5157. bond_mode == BOND_MODE_BROADCAST) {
  5158. pr_info("xmit_hash_policy param is irrelevant in mode %s\n",
  5159. bond_mode_name(bond_mode));
  5160. } else {
  5161. bond_opt_initstr(&newval, xmit_hash_policy);
  5162. valptr = bond_opt_parse(bond_opt_get(BOND_OPT_XMIT_HASH),
  5163. &newval);
  5164. if (!valptr) {
  5165. pr_err("Error: Invalid xmit_hash_policy \"%s\"\n",
  5166. xmit_hash_policy);
  5167. return -EINVAL;
  5168. }
  5169. xmit_hashtype = valptr->value;
  5170. }
  5171. }
  5172. if (lacp_rate) {
  5173. if (bond_mode != BOND_MODE_8023AD) {
  5174. pr_info("lacp_rate param is irrelevant in mode %s\n",
  5175. bond_mode_name(bond_mode));
  5176. } else {
  5177. bond_opt_initstr(&newval, lacp_rate);
  5178. valptr = bond_opt_parse(bond_opt_get(BOND_OPT_LACP_RATE),
  5179. &newval);
  5180. if (!valptr) {
  5181. pr_err("Error: Invalid lacp rate \"%s\"\n",
  5182. lacp_rate);
  5183. return -EINVAL;
  5184. }
  5185. lacp_fast = valptr->value;
  5186. }
  5187. }
  5188. if (ad_select) {
  5189. bond_opt_initstr(&newval, ad_select);
  5190. valptr = bond_opt_parse(bond_opt_get(BOND_OPT_AD_SELECT),
  5191. &newval);
  5192. if (!valptr) {
  5193. pr_err("Error: Invalid ad_select \"%s\"\n", ad_select);
  5194. return -EINVAL;
  5195. }
  5196. params->ad_select = valptr->value;
  5197. if (bond_mode != BOND_MODE_8023AD)
  5198. pr_warn("ad_select param only affects 802.3ad mode\n");
  5199. } else {
  5200. params->ad_select = BOND_AD_STABLE;
  5201. }
  5202. if (max_bonds < 0) {
  5203. pr_warn("Warning: max_bonds (%d) not in range %d-%d, so it was reset to BOND_DEFAULT_MAX_BONDS (%d)\n",
  5204. max_bonds, 0, INT_MAX, BOND_DEFAULT_MAX_BONDS);
  5205. max_bonds = BOND_DEFAULT_MAX_BONDS;
  5206. }
  5207. if (miimon < 0) {
  5208. pr_warn("Warning: miimon module parameter (%d), not in range 0-%d, so it was reset to 0\n",
  5209. miimon, INT_MAX);
  5210. miimon = 0;
  5211. }
  5212. if (updelay < 0) {
  5213. pr_warn("Warning: updelay module parameter (%d), not in range 0-%d, so it was reset to 0\n",
  5214. updelay, INT_MAX);
  5215. updelay = 0;
  5216. }
  5217. if (downdelay < 0) {
  5218. pr_warn("Warning: downdelay module parameter (%d), not in range 0-%d, so it was reset to 0\n",
  5219. downdelay, INT_MAX);
  5220. downdelay = 0;
  5221. }
  5222. if ((use_carrier != 0) && (use_carrier != 1)) {
  5223. pr_warn("Warning: use_carrier module parameter (%d), not of valid value (0/1), so it was set to 1\n",
  5224. use_carrier);
  5225. use_carrier = 1;
  5226. }
  5227. if (num_peer_notif < 0 || num_peer_notif > 255) {
  5228. pr_warn("Warning: num_grat_arp/num_unsol_na (%d) not in range 0-255 so it was reset to 1\n",
  5229. num_peer_notif);
  5230. num_peer_notif = 1;
  5231. }
  5232. /* reset values for 802.3ad/TLB/ALB */
  5233. if (!bond_mode_uses_arp(bond_mode)) {
  5234. if (!miimon) {
  5235. pr_warn("Warning: miimon must be specified, otherwise bonding will not detect link failure, speed and duplex which are essential for 802.3ad operation\n");
  5236. pr_warn("Forcing miimon to 100msec\n");
  5237. miimon = BOND_DEFAULT_MIIMON;
  5238. }
  5239. }
  5240. if (tx_queues < 1 || tx_queues > 255) {
  5241. pr_warn("Warning: tx_queues (%d) should be between 1 and 255, resetting to %d\n",
  5242. tx_queues, BOND_DEFAULT_TX_QUEUES);
  5243. tx_queues = BOND_DEFAULT_TX_QUEUES;
  5244. }
  5245. if ((all_slaves_active != 0) && (all_slaves_active != 1)) {
  5246. pr_warn("Warning: all_slaves_active module parameter (%d), not of valid value (0/1), so it was set to 0\n",
  5247. all_slaves_active);
  5248. all_slaves_active = 0;
  5249. }
  5250. if (resend_igmp < 0 || resend_igmp > 255) {
  5251. pr_warn("Warning: resend_igmp (%d) should be between 0 and 255, resetting to %d\n",
  5252. resend_igmp, BOND_DEFAULT_RESEND_IGMP);
  5253. resend_igmp = BOND_DEFAULT_RESEND_IGMP;
  5254. }
  5255. bond_opt_initval(&newval, packets_per_slave);
  5256. if (!bond_opt_parse(bond_opt_get(BOND_OPT_PACKETS_PER_SLAVE), &newval)) {
  5257. pr_warn("Warning: packets_per_slave (%d) should be between 0 and %u resetting to 1\n",
  5258. packets_per_slave, USHRT_MAX);
  5259. packets_per_slave = 1;
  5260. }
  5261. if (bond_mode == BOND_MODE_ALB) {
  5262. 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",
  5263. updelay);
  5264. }
  5265. if (!miimon) {
  5266. if (updelay || downdelay) {
  5267. /* just warn the user the up/down delay will have
  5268. * no effect since miimon is zero...
  5269. */
  5270. pr_warn("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",
  5271. updelay, downdelay);
  5272. }
  5273. } else {
  5274. /* don't allow arp monitoring */
  5275. if (arp_interval) {
  5276. pr_warn("Warning: miimon (%d) and arp_interval (%d) can't be used simultaneously, disabling ARP monitoring\n",
  5277. miimon, arp_interval);
  5278. arp_interval = 0;
  5279. }
  5280. if ((updelay % miimon) != 0) {
  5281. pr_warn("Warning: updelay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
  5282. updelay, miimon, (updelay / miimon) * miimon);
  5283. }
  5284. updelay /= miimon;
  5285. if ((downdelay % miimon) != 0) {
  5286. pr_warn("Warning: downdelay (%d) is not a multiple of miimon (%d), downdelay rounded to %d ms\n",
  5287. downdelay, miimon,
  5288. (downdelay / miimon) * miimon);
  5289. }
  5290. downdelay /= miimon;
  5291. }
  5292. if (arp_interval < 0) {
  5293. pr_warn("Warning: arp_interval module parameter (%d), not in range 0-%d, so it was reset to 0\n",
  5294. arp_interval, INT_MAX);
  5295. arp_interval = 0;
  5296. }
  5297. for (arp_ip_count = 0, i = 0;
  5298. (arp_ip_count < BOND_MAX_ARP_TARGETS) && arp_ip_target[i]; i++) {
  5299. __be32 ip;
  5300. /* not a complete check, but good enough to catch mistakes */
  5301. if (!in4_pton(arp_ip_target[i], -1, (u8 *)&ip, -1, NULL) ||
  5302. !bond_is_ip_target_ok(ip)) {
  5303. pr_warn("Warning: bad arp_ip_target module parameter (%s), ARP monitoring will not be performed\n",
  5304. arp_ip_target[i]);
  5305. arp_interval = 0;
  5306. } else {
  5307. if (bond_get_targets_ip(arp_target, ip) == -1)
  5308. arp_target[arp_ip_count++] = ip;
  5309. else
  5310. pr_warn("Warning: duplicate address %pI4 in arp_ip_target, skipping\n",
  5311. &ip);
  5312. }
  5313. }
  5314. if (arp_interval && !arp_ip_count) {
  5315. /* don't allow arping if no arp_ip_target given... */
  5316. pr_warn("Warning: arp_interval module parameter (%d) specified without providing an arp_ip_target parameter, arp_interval was reset to 0\n",
  5317. arp_interval);
  5318. arp_interval = 0;
  5319. }
  5320. if (arp_validate) {
  5321. if (!arp_interval) {
  5322. pr_err("arp_validate requires arp_interval\n");
  5323. return -EINVAL;
  5324. }
  5325. bond_opt_initstr(&newval, arp_validate);
  5326. valptr = bond_opt_parse(bond_opt_get(BOND_OPT_ARP_VALIDATE),
  5327. &newval);
  5328. if (!valptr) {
  5329. pr_err("Error: invalid arp_validate \"%s\"\n",
  5330. arp_validate);
  5331. return -EINVAL;
  5332. }
  5333. arp_validate_value = valptr->value;
  5334. } else {
  5335. arp_validate_value = 0;
  5336. }
  5337. if (arp_all_targets) {
  5338. bond_opt_initstr(&newval, arp_all_targets);
  5339. valptr = bond_opt_parse(bond_opt_get(BOND_OPT_ARP_ALL_TARGETS),
  5340. &newval);
  5341. if (!valptr) {
  5342. pr_err("Error: invalid arp_all_targets_value \"%s\"\n",
  5343. arp_all_targets);
  5344. arp_all_targets_value = 0;
  5345. } else {
  5346. arp_all_targets_value = valptr->value;
  5347. }
  5348. }
  5349. if (miimon) {
  5350. pr_info("MII link monitoring set to %d ms\n", miimon);
  5351. } else if (arp_interval) {
  5352. valptr = bond_opt_get_val(BOND_OPT_ARP_VALIDATE,
  5353. arp_validate_value);
  5354. pr_info("ARP monitoring set to %d ms, validate %s, with %d target(s):",
  5355. arp_interval, valptr->string, arp_ip_count);
  5356. for (i = 0; i < arp_ip_count; i++)
  5357. pr_cont(" %s", arp_ip_target[i]);
  5358. pr_cont("\n");
  5359. } else if (max_bonds) {
  5360. /* miimon and arp_interval not set, we need one so things
  5361. * work as expected, see bonding.txt for details
  5362. */
  5363. pr_debug("Warning: either miimon or arp_interval and arp_ip_target module parameters must be specified, otherwise bonding will not detect link failures! see bonding.txt for details\n");
  5364. }
  5365. if (primary && !bond_mode_uses_primary(bond_mode)) {
  5366. /* currently, using a primary only makes sense
  5367. * in active backup, TLB or ALB modes
  5368. */
  5369. pr_warn("Warning: %s primary device specified but has no effect in %s mode\n",
  5370. primary, bond_mode_name(bond_mode));
  5371. primary = NULL;
  5372. }
  5373. if (primary && primary_reselect) {
  5374. bond_opt_initstr(&newval, primary_reselect);
  5375. valptr = bond_opt_parse(bond_opt_get(BOND_OPT_PRIMARY_RESELECT),
  5376. &newval);
  5377. if (!valptr) {
  5378. pr_err("Error: Invalid primary_reselect \"%s\"\n",
  5379. primary_reselect);
  5380. return -EINVAL;
  5381. }
  5382. primary_reselect_value = valptr->value;
  5383. } else {
  5384. primary_reselect_value = BOND_PRI_RESELECT_ALWAYS;
  5385. }
  5386. if (fail_over_mac) {
  5387. bond_opt_initstr(&newval, fail_over_mac);
  5388. valptr = bond_opt_parse(bond_opt_get(BOND_OPT_FAIL_OVER_MAC),
  5389. &newval);
  5390. if (!valptr) {
  5391. pr_err("Error: invalid fail_over_mac \"%s\"\n",
  5392. fail_over_mac);
  5393. return -EINVAL;
  5394. }
  5395. fail_over_mac_value = valptr->value;
  5396. if (bond_mode != BOND_MODE_ACTIVEBACKUP)
  5397. pr_warn("Warning: fail_over_mac only affects active-backup mode\n");
  5398. } else {
  5399. fail_over_mac_value = BOND_FOM_NONE;
  5400. }
  5401. bond_opt_initstr(&newval, "default");
  5402. valptr = bond_opt_parse(
  5403. bond_opt_get(BOND_OPT_AD_ACTOR_SYS_PRIO),
  5404. &newval);
  5405. if (!valptr) {
  5406. pr_err("Error: No ad_actor_sys_prio default value");
  5407. return -EINVAL;
  5408. }
  5409. ad_actor_sys_prio = valptr->value;
  5410. valptr = bond_opt_parse(bond_opt_get(BOND_OPT_AD_USER_PORT_KEY),
  5411. &newval);
  5412. if (!valptr) {
  5413. pr_err("Error: No ad_user_port_key default value");
  5414. return -EINVAL;
  5415. }
  5416. ad_user_port_key = valptr->value;
  5417. bond_opt_initstr(&newval, "default");
  5418. valptr = bond_opt_parse(bond_opt_get(BOND_OPT_TLB_DYNAMIC_LB), &newval);
  5419. if (!valptr) {
  5420. pr_err("Error: No tlb_dynamic_lb default value");
  5421. return -EINVAL;
  5422. }
  5423. tlb_dynamic_lb = valptr->value;
  5424. if (lp_interval == 0) {
  5425. pr_warn("Warning: ip_interval must be between 1 and %d, so it was reset to %d\n",
  5426. INT_MAX, BOND_ALB_DEFAULT_LP_INTERVAL);
  5427. lp_interval = BOND_ALB_DEFAULT_LP_INTERVAL;
  5428. }
  5429. /* fill params struct with the proper values */
  5430. params->mode = bond_mode;
  5431. params->xmit_policy = xmit_hashtype;
  5432. params->miimon = miimon;
  5433. params->num_peer_notif = num_peer_notif;
  5434. params->arp_interval = arp_interval;
  5435. params->arp_validate = arp_validate_value;
  5436. params->arp_all_targets = arp_all_targets_value;
  5437. params->missed_max = 2;
  5438. params->updelay = updelay;
  5439. params->downdelay = downdelay;
  5440. params->peer_notif_delay = 0;
  5441. params->use_carrier = use_carrier;
  5442. params->lacp_active = 1;
  5443. params->lacp_fast = lacp_fast;
  5444. params->primary[0] = 0;
  5445. params->primary_reselect = primary_reselect_value;
  5446. params->fail_over_mac = fail_over_mac_value;
  5447. params->tx_queues = tx_queues;
  5448. params->all_slaves_active = all_slaves_active;
  5449. params->resend_igmp = resend_igmp;
  5450. params->min_links = min_links;
  5451. params->lp_interval = lp_interval;
  5452. params->packets_per_slave = packets_per_slave;
  5453. params->tlb_dynamic_lb = tlb_dynamic_lb;
  5454. params->ad_actor_sys_prio = ad_actor_sys_prio;
  5455. eth_zero_addr(params->ad_actor_system);
  5456. params->ad_user_port_key = ad_user_port_key;
  5457. params->coupled_control = 1;
  5458. if (packets_per_slave > 0) {
  5459. params->reciprocal_packets_per_slave =
  5460. reciprocal_value(packets_per_slave);
  5461. } else {
  5462. /* reciprocal_packets_per_slave is unused if
  5463. * packets_per_slave is 0 or 1, just initialize it
  5464. */
  5465. params->reciprocal_packets_per_slave =
  5466. (struct reciprocal_value) { 0 };
  5467. }
  5468. if (primary)
  5469. strscpy_pad(params->primary, primary, sizeof(params->primary));
  5470. memcpy(params->arp_targets, arp_target, sizeof(arp_target));
  5471. #if IS_ENABLED(CONFIG_IPV6)
  5472. memset(params->ns_targets, 0, sizeof(struct in6_addr) * BOND_MAX_NS_TARGETS);
  5473. #endif
  5474. return 0;
  5475. }
  5476. /* Called from registration process */
  5477. static int bond_init(struct net_device *bond_dev)
  5478. {
  5479. struct bonding *bond = netdev_priv(bond_dev);
  5480. struct bond_net *bn = net_generic(dev_net(bond_dev), bond_net_id);
  5481. netdev_dbg(bond_dev, "Begin bond_init\n");
  5482. bond->wq = alloc_ordered_workqueue("%s", WQ_MEM_RECLAIM,
  5483. bond_dev->name);
  5484. if (!bond->wq)
  5485. return -ENOMEM;
  5486. bond->notifier_ctx = false;
  5487. spin_lock_init(&bond->stats_lock);
  5488. netdev_lockdep_set_classes(bond_dev);
  5489. list_add_tail_rcu(&bond->bond_list, &bn->dev_list);
  5490. bond_prepare_sysfs_group(bond);
  5491. bond_debug_register(bond);
  5492. /* Ensure valid dev_addr */
  5493. if (is_zero_ether_addr(bond_dev->dev_addr) &&
  5494. bond_dev->addr_assign_type == NET_ADDR_PERM)
  5495. eth_hw_addr_random(bond_dev);
  5496. return 0;
  5497. }
  5498. unsigned int bond_get_num_tx_queues(void)
  5499. {
  5500. return tx_queues;
  5501. }
  5502. /* Create a new bond based on the specified name and bonding parameters.
  5503. * If name is NULL, obtain a suitable "bond%d" name for us.
  5504. * Caller must NOT hold rtnl_lock; we need to release it here before we
  5505. * set up our sysfs entries.
  5506. */
  5507. int bond_create(struct net *net, const char *name)
  5508. {
  5509. struct net_device *bond_dev;
  5510. struct bonding *bond;
  5511. int res = -ENOMEM;
  5512. rtnl_lock();
  5513. bond_dev = alloc_netdev_mq(sizeof(struct bonding),
  5514. name ? name : "bond%d", NET_NAME_UNKNOWN,
  5515. bond_setup, tx_queues);
  5516. if (!bond_dev)
  5517. goto out;
  5518. bond = netdev_priv(bond_dev);
  5519. dev_net_set(bond_dev, net);
  5520. bond_dev->rtnl_link_ops = &bond_link_ops;
  5521. res = register_netdevice(bond_dev);
  5522. if (res < 0) {
  5523. free_netdev(bond_dev);
  5524. goto out;
  5525. }
  5526. netif_carrier_off(bond_dev);
  5527. bond_work_init_all(bond);
  5528. out:
  5529. rtnl_unlock();
  5530. return res;
  5531. }
  5532. static int __net_init bond_net_init(struct net *net)
  5533. {
  5534. struct bond_net *bn = net_generic(net, bond_net_id);
  5535. bn->net = net;
  5536. INIT_LIST_HEAD(&bn->dev_list);
  5537. bond_create_proc_dir(bn);
  5538. bond_create_sysfs(bn);
  5539. return 0;
  5540. }
  5541. /* According to commit 69b0216ac255 ("bonding: fix bonding_masters
  5542. * race condition in bond unloading") we need to remove sysfs files
  5543. * before we remove our devices (done later in bond_net_exit_batch_rtnl())
  5544. */
  5545. static void __net_exit bond_net_pre_exit(struct net *net)
  5546. {
  5547. struct bond_net *bn = net_generic(net, bond_net_id);
  5548. bond_destroy_sysfs(bn);
  5549. }
  5550. static void __net_exit bond_net_exit_batch_rtnl(struct list_head *net_list,
  5551. struct list_head *dev_kill_list)
  5552. {
  5553. struct bond_net *bn;
  5554. struct net *net;
  5555. /* Kill off any bonds created after unregistering bond rtnl ops */
  5556. list_for_each_entry(net, net_list, exit_list) {
  5557. struct bonding *bond, *tmp_bond;
  5558. bn = net_generic(net, bond_net_id);
  5559. list_for_each_entry_safe(bond, tmp_bond, &bn->dev_list, bond_list)
  5560. unregister_netdevice_queue(bond->dev, dev_kill_list);
  5561. }
  5562. }
  5563. /* According to commit 23fa5c2caae0 ("bonding: destroy proc directory
  5564. * only after all bonds are gone") bond_destroy_proc_dir() is called
  5565. * after bond_net_exit_batch_rtnl() has completed.
  5566. */
  5567. static void __net_exit bond_net_exit_batch(struct list_head *net_list)
  5568. {
  5569. struct bond_net *bn;
  5570. struct net *net;
  5571. list_for_each_entry(net, net_list, exit_list) {
  5572. bn = net_generic(net, bond_net_id);
  5573. bond_destroy_proc_dir(bn);
  5574. }
  5575. }
  5576. static struct pernet_operations bond_net_ops = {
  5577. .init = bond_net_init,
  5578. .pre_exit = bond_net_pre_exit,
  5579. .exit_batch_rtnl = bond_net_exit_batch_rtnl,
  5580. .exit_batch = bond_net_exit_batch,
  5581. .id = &bond_net_id,
  5582. .size = sizeof(struct bond_net),
  5583. };
  5584. static int __init bonding_init(void)
  5585. {
  5586. int i;
  5587. int res;
  5588. res = bond_check_params(&bonding_defaults);
  5589. if (res)
  5590. goto out;
  5591. bond_create_debugfs();
  5592. res = register_pernet_subsys(&bond_net_ops);
  5593. if (res)
  5594. goto err_net_ops;
  5595. res = bond_netlink_init();
  5596. if (res)
  5597. goto err_link;
  5598. for (i = 0; i < max_bonds; i++) {
  5599. res = bond_create(&init_net, NULL);
  5600. if (res)
  5601. goto err;
  5602. }
  5603. skb_flow_dissector_init(&flow_keys_bonding,
  5604. flow_keys_bonding_keys,
  5605. ARRAY_SIZE(flow_keys_bonding_keys));
  5606. register_netdevice_notifier(&bond_netdev_notifier);
  5607. out:
  5608. return res;
  5609. err:
  5610. bond_netlink_fini();
  5611. err_link:
  5612. unregister_pernet_subsys(&bond_net_ops);
  5613. err_net_ops:
  5614. bond_destroy_debugfs();
  5615. goto out;
  5616. }
  5617. static void __exit bonding_exit(void)
  5618. {
  5619. unregister_netdevice_notifier(&bond_netdev_notifier);
  5620. bond_netlink_fini();
  5621. unregister_pernet_subsys(&bond_net_ops);
  5622. bond_destroy_debugfs();
  5623. #ifdef CONFIG_NET_POLL_CONTROLLER
  5624. /* Make sure we don't have an imbalance on our netpoll blocking */
  5625. WARN_ON(atomic_read(&netpoll_block_tx));
  5626. #endif
  5627. }
  5628. module_init(bonding_init);
  5629. module_exit(bonding_exit);
  5630. MODULE_LICENSE("GPL");
  5631. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  5632. MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others");