spectrum_router.c 200 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /* Copyright (c) 2016-2018 Mellanox Technologies. All rights reserved */
  3. #include <linux/kernel.h>
  4. #include <linux/types.h>
  5. #include <linux/rhashtable.h>
  6. #include <linux/bitops.h>
  7. #include <linux/in6.h>
  8. #include <linux/notifier.h>
  9. #include <linux/inetdevice.h>
  10. #include <linux/netdevice.h>
  11. #include <linux/if_bridge.h>
  12. #include <linux/socket.h>
  13. #include <linux/route.h>
  14. #include <linux/gcd.h>
  15. #include <linux/random.h>
  16. #include <linux/if_macvlan.h>
  17. #include <net/netevent.h>
  18. #include <net/neighbour.h>
  19. #include <net/arp.h>
  20. #include <net/ip_fib.h>
  21. #include <net/ip6_fib.h>
  22. #include <net/fib_rules.h>
  23. #include <net/ip_tunnels.h>
  24. #include <net/l3mdev.h>
  25. #include <net/addrconf.h>
  26. #include <net/ndisc.h>
  27. #include <net/ipv6.h>
  28. #include <net/fib_notifier.h>
  29. #include <net/switchdev.h>
  30. #include "spectrum.h"
  31. #include "core.h"
  32. #include "reg.h"
  33. #include "spectrum_cnt.h"
  34. #include "spectrum_dpipe.h"
  35. #include "spectrum_ipip.h"
  36. #include "spectrum_mr.h"
  37. #include "spectrum_mr_tcam.h"
  38. #include "spectrum_router.h"
  39. #include "spectrum_span.h"
  40. struct mlxsw_sp_fib;
  41. struct mlxsw_sp_vr;
  42. struct mlxsw_sp_lpm_tree;
  43. struct mlxsw_sp_rif_ops;
  44. struct mlxsw_sp_router {
  45. struct mlxsw_sp *mlxsw_sp;
  46. struct mlxsw_sp_rif **rifs;
  47. struct mlxsw_sp_vr *vrs;
  48. struct rhashtable neigh_ht;
  49. struct rhashtable nexthop_group_ht;
  50. struct rhashtable nexthop_ht;
  51. struct list_head nexthop_list;
  52. struct {
  53. /* One tree for each protocol: IPv4 and IPv6 */
  54. struct mlxsw_sp_lpm_tree *proto_trees[2];
  55. struct mlxsw_sp_lpm_tree *trees;
  56. unsigned int tree_count;
  57. } lpm;
  58. struct {
  59. struct delayed_work dw;
  60. unsigned long interval; /* ms */
  61. } neighs_update;
  62. struct delayed_work nexthop_probe_dw;
  63. #define MLXSW_SP_UNRESOLVED_NH_PROBE_INTERVAL 5000 /* ms */
  64. struct list_head nexthop_neighs_list;
  65. struct list_head ipip_list;
  66. bool aborted;
  67. struct notifier_block fib_nb;
  68. struct notifier_block netevent_nb;
  69. const struct mlxsw_sp_rif_ops **rif_ops_arr;
  70. const struct mlxsw_sp_ipip_ops **ipip_ops_arr;
  71. };
  72. struct mlxsw_sp_rif {
  73. struct list_head nexthop_list;
  74. struct list_head neigh_list;
  75. struct net_device *dev;
  76. struct mlxsw_sp_fid *fid;
  77. unsigned char addr[ETH_ALEN];
  78. int mtu;
  79. u16 rif_index;
  80. u16 vr_id;
  81. const struct mlxsw_sp_rif_ops *ops;
  82. struct mlxsw_sp *mlxsw_sp;
  83. unsigned int counter_ingress;
  84. bool counter_ingress_valid;
  85. unsigned int counter_egress;
  86. bool counter_egress_valid;
  87. };
  88. struct mlxsw_sp_rif_params {
  89. struct net_device *dev;
  90. union {
  91. u16 system_port;
  92. u16 lag_id;
  93. };
  94. u16 vid;
  95. bool lag;
  96. };
  97. struct mlxsw_sp_rif_subport {
  98. struct mlxsw_sp_rif common;
  99. union {
  100. u16 system_port;
  101. u16 lag_id;
  102. };
  103. u16 vid;
  104. bool lag;
  105. };
  106. struct mlxsw_sp_rif_ipip_lb {
  107. struct mlxsw_sp_rif common;
  108. struct mlxsw_sp_rif_ipip_lb_config lb_config;
  109. u16 ul_vr_id; /* Reserved for Spectrum-2. */
  110. };
  111. struct mlxsw_sp_rif_params_ipip_lb {
  112. struct mlxsw_sp_rif_params common;
  113. struct mlxsw_sp_rif_ipip_lb_config lb_config;
  114. };
  115. struct mlxsw_sp_rif_ops {
  116. enum mlxsw_sp_rif_type type;
  117. size_t rif_size;
  118. void (*setup)(struct mlxsw_sp_rif *rif,
  119. const struct mlxsw_sp_rif_params *params);
  120. int (*configure)(struct mlxsw_sp_rif *rif);
  121. void (*deconfigure)(struct mlxsw_sp_rif *rif);
  122. struct mlxsw_sp_fid * (*fid_get)(struct mlxsw_sp_rif *rif,
  123. struct netlink_ext_ack *extack);
  124. void (*fdb_del)(struct mlxsw_sp_rif *rif, const char *mac);
  125. };
  126. static void mlxsw_sp_lpm_tree_hold(struct mlxsw_sp_lpm_tree *lpm_tree);
  127. static void mlxsw_sp_lpm_tree_put(struct mlxsw_sp *mlxsw_sp,
  128. struct mlxsw_sp_lpm_tree *lpm_tree);
  129. static int mlxsw_sp_vr_lpm_tree_bind(struct mlxsw_sp *mlxsw_sp,
  130. const struct mlxsw_sp_fib *fib,
  131. u8 tree_id);
  132. static int mlxsw_sp_vr_lpm_tree_unbind(struct mlxsw_sp *mlxsw_sp,
  133. const struct mlxsw_sp_fib *fib);
  134. static unsigned int *
  135. mlxsw_sp_rif_p_counter_get(struct mlxsw_sp_rif *rif,
  136. enum mlxsw_sp_rif_counter_dir dir)
  137. {
  138. switch (dir) {
  139. case MLXSW_SP_RIF_COUNTER_EGRESS:
  140. return &rif->counter_egress;
  141. case MLXSW_SP_RIF_COUNTER_INGRESS:
  142. return &rif->counter_ingress;
  143. }
  144. return NULL;
  145. }
  146. static bool
  147. mlxsw_sp_rif_counter_valid_get(struct mlxsw_sp_rif *rif,
  148. enum mlxsw_sp_rif_counter_dir dir)
  149. {
  150. switch (dir) {
  151. case MLXSW_SP_RIF_COUNTER_EGRESS:
  152. return rif->counter_egress_valid;
  153. case MLXSW_SP_RIF_COUNTER_INGRESS:
  154. return rif->counter_ingress_valid;
  155. }
  156. return false;
  157. }
  158. static void
  159. mlxsw_sp_rif_counter_valid_set(struct mlxsw_sp_rif *rif,
  160. enum mlxsw_sp_rif_counter_dir dir,
  161. bool valid)
  162. {
  163. switch (dir) {
  164. case MLXSW_SP_RIF_COUNTER_EGRESS:
  165. rif->counter_egress_valid = valid;
  166. break;
  167. case MLXSW_SP_RIF_COUNTER_INGRESS:
  168. rif->counter_ingress_valid = valid;
  169. break;
  170. }
  171. }
  172. static int mlxsw_sp_rif_counter_edit(struct mlxsw_sp *mlxsw_sp, u16 rif_index,
  173. unsigned int counter_index, bool enable,
  174. enum mlxsw_sp_rif_counter_dir dir)
  175. {
  176. char ritr_pl[MLXSW_REG_RITR_LEN];
  177. bool is_egress = false;
  178. int err;
  179. if (dir == MLXSW_SP_RIF_COUNTER_EGRESS)
  180. is_egress = true;
  181. mlxsw_reg_ritr_rif_pack(ritr_pl, rif_index);
  182. err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
  183. if (err)
  184. return err;
  185. mlxsw_reg_ritr_counter_pack(ritr_pl, counter_index, enable,
  186. is_egress);
  187. return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
  188. }
  189. int mlxsw_sp_rif_counter_value_get(struct mlxsw_sp *mlxsw_sp,
  190. struct mlxsw_sp_rif *rif,
  191. enum mlxsw_sp_rif_counter_dir dir, u64 *cnt)
  192. {
  193. char ricnt_pl[MLXSW_REG_RICNT_LEN];
  194. unsigned int *p_counter_index;
  195. bool valid;
  196. int err;
  197. valid = mlxsw_sp_rif_counter_valid_get(rif, dir);
  198. if (!valid)
  199. return -EINVAL;
  200. p_counter_index = mlxsw_sp_rif_p_counter_get(rif, dir);
  201. if (!p_counter_index)
  202. return -EINVAL;
  203. mlxsw_reg_ricnt_pack(ricnt_pl, *p_counter_index,
  204. MLXSW_REG_RICNT_OPCODE_NOP);
  205. err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ricnt), ricnt_pl);
  206. if (err)
  207. return err;
  208. *cnt = mlxsw_reg_ricnt_good_unicast_packets_get(ricnt_pl);
  209. return 0;
  210. }
  211. static int mlxsw_sp_rif_counter_clear(struct mlxsw_sp *mlxsw_sp,
  212. unsigned int counter_index)
  213. {
  214. char ricnt_pl[MLXSW_REG_RICNT_LEN];
  215. mlxsw_reg_ricnt_pack(ricnt_pl, counter_index,
  216. MLXSW_REG_RICNT_OPCODE_CLEAR);
  217. return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ricnt), ricnt_pl);
  218. }
  219. int mlxsw_sp_rif_counter_alloc(struct mlxsw_sp *mlxsw_sp,
  220. struct mlxsw_sp_rif *rif,
  221. enum mlxsw_sp_rif_counter_dir dir)
  222. {
  223. unsigned int *p_counter_index;
  224. int err;
  225. p_counter_index = mlxsw_sp_rif_p_counter_get(rif, dir);
  226. if (!p_counter_index)
  227. return -EINVAL;
  228. err = mlxsw_sp_counter_alloc(mlxsw_sp, MLXSW_SP_COUNTER_SUB_POOL_RIF,
  229. p_counter_index);
  230. if (err)
  231. return err;
  232. err = mlxsw_sp_rif_counter_clear(mlxsw_sp, *p_counter_index);
  233. if (err)
  234. goto err_counter_clear;
  235. err = mlxsw_sp_rif_counter_edit(mlxsw_sp, rif->rif_index,
  236. *p_counter_index, true, dir);
  237. if (err)
  238. goto err_counter_edit;
  239. mlxsw_sp_rif_counter_valid_set(rif, dir, true);
  240. return 0;
  241. err_counter_edit:
  242. err_counter_clear:
  243. mlxsw_sp_counter_free(mlxsw_sp, MLXSW_SP_COUNTER_SUB_POOL_RIF,
  244. *p_counter_index);
  245. return err;
  246. }
  247. void mlxsw_sp_rif_counter_free(struct mlxsw_sp *mlxsw_sp,
  248. struct mlxsw_sp_rif *rif,
  249. enum mlxsw_sp_rif_counter_dir dir)
  250. {
  251. unsigned int *p_counter_index;
  252. if (!mlxsw_sp_rif_counter_valid_get(rif, dir))
  253. return;
  254. p_counter_index = mlxsw_sp_rif_p_counter_get(rif, dir);
  255. if (WARN_ON(!p_counter_index))
  256. return;
  257. mlxsw_sp_rif_counter_edit(mlxsw_sp, rif->rif_index,
  258. *p_counter_index, false, dir);
  259. mlxsw_sp_counter_free(mlxsw_sp, MLXSW_SP_COUNTER_SUB_POOL_RIF,
  260. *p_counter_index);
  261. mlxsw_sp_rif_counter_valid_set(rif, dir, false);
  262. }
  263. static void mlxsw_sp_rif_counters_alloc(struct mlxsw_sp_rif *rif)
  264. {
  265. struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
  266. struct devlink *devlink;
  267. devlink = priv_to_devlink(mlxsw_sp->core);
  268. if (!devlink_dpipe_table_counter_enabled(devlink,
  269. MLXSW_SP_DPIPE_TABLE_NAME_ERIF))
  270. return;
  271. mlxsw_sp_rif_counter_alloc(mlxsw_sp, rif, MLXSW_SP_RIF_COUNTER_EGRESS);
  272. }
  273. static void mlxsw_sp_rif_counters_free(struct mlxsw_sp_rif *rif)
  274. {
  275. struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
  276. mlxsw_sp_rif_counter_free(mlxsw_sp, rif, MLXSW_SP_RIF_COUNTER_EGRESS);
  277. }
  278. #define MLXSW_SP_PREFIX_COUNT (sizeof(struct in6_addr) * BITS_PER_BYTE + 1)
  279. struct mlxsw_sp_prefix_usage {
  280. DECLARE_BITMAP(b, MLXSW_SP_PREFIX_COUNT);
  281. };
  282. #define mlxsw_sp_prefix_usage_for_each(prefix, prefix_usage) \
  283. for_each_set_bit(prefix, (prefix_usage)->b, MLXSW_SP_PREFIX_COUNT)
  284. static bool
  285. mlxsw_sp_prefix_usage_eq(struct mlxsw_sp_prefix_usage *prefix_usage1,
  286. struct mlxsw_sp_prefix_usage *prefix_usage2)
  287. {
  288. return !memcmp(prefix_usage1, prefix_usage2, sizeof(*prefix_usage1));
  289. }
  290. static void
  291. mlxsw_sp_prefix_usage_cpy(struct mlxsw_sp_prefix_usage *prefix_usage1,
  292. struct mlxsw_sp_prefix_usage *prefix_usage2)
  293. {
  294. memcpy(prefix_usage1, prefix_usage2, sizeof(*prefix_usage1));
  295. }
  296. static void
  297. mlxsw_sp_prefix_usage_set(struct mlxsw_sp_prefix_usage *prefix_usage,
  298. unsigned char prefix_len)
  299. {
  300. set_bit(prefix_len, prefix_usage->b);
  301. }
  302. static void
  303. mlxsw_sp_prefix_usage_clear(struct mlxsw_sp_prefix_usage *prefix_usage,
  304. unsigned char prefix_len)
  305. {
  306. clear_bit(prefix_len, prefix_usage->b);
  307. }
  308. struct mlxsw_sp_fib_key {
  309. unsigned char addr[sizeof(struct in6_addr)];
  310. unsigned char prefix_len;
  311. };
  312. enum mlxsw_sp_fib_entry_type {
  313. MLXSW_SP_FIB_ENTRY_TYPE_REMOTE,
  314. MLXSW_SP_FIB_ENTRY_TYPE_LOCAL,
  315. MLXSW_SP_FIB_ENTRY_TYPE_TRAP,
  316. /* This is a special case of local delivery, where a packet should be
  317. * decapsulated on reception. Note that there is no corresponding ENCAP,
  318. * because that's a type of next hop, not of FIB entry. (There can be
  319. * several next hops in a REMOTE entry, and some of them may be
  320. * encapsulating entries.)
  321. */
  322. MLXSW_SP_FIB_ENTRY_TYPE_IPIP_DECAP,
  323. };
  324. struct mlxsw_sp_nexthop_group;
  325. struct mlxsw_sp_fib_node {
  326. struct list_head entry_list;
  327. struct list_head list;
  328. struct rhash_head ht_node;
  329. struct mlxsw_sp_fib *fib;
  330. struct mlxsw_sp_fib_key key;
  331. };
  332. struct mlxsw_sp_fib_entry_decap {
  333. struct mlxsw_sp_ipip_entry *ipip_entry;
  334. u32 tunnel_index;
  335. };
  336. struct mlxsw_sp_fib_entry {
  337. struct list_head list;
  338. struct mlxsw_sp_fib_node *fib_node;
  339. enum mlxsw_sp_fib_entry_type type;
  340. struct list_head nexthop_group_node;
  341. struct mlxsw_sp_nexthop_group *nh_group;
  342. struct mlxsw_sp_fib_entry_decap decap; /* Valid for decap entries. */
  343. };
  344. struct mlxsw_sp_fib4_entry {
  345. struct mlxsw_sp_fib_entry common;
  346. u32 tb_id;
  347. u32 prio;
  348. u8 tos;
  349. u8 type;
  350. };
  351. struct mlxsw_sp_fib6_entry {
  352. struct mlxsw_sp_fib_entry common;
  353. struct list_head rt6_list;
  354. unsigned int nrt6;
  355. };
  356. struct mlxsw_sp_rt6 {
  357. struct list_head list;
  358. struct fib6_info *rt;
  359. };
  360. struct mlxsw_sp_lpm_tree {
  361. u8 id; /* tree ID */
  362. unsigned int ref_count;
  363. enum mlxsw_sp_l3proto proto;
  364. unsigned long prefix_ref_count[MLXSW_SP_PREFIX_COUNT];
  365. struct mlxsw_sp_prefix_usage prefix_usage;
  366. };
  367. struct mlxsw_sp_fib {
  368. struct rhashtable ht;
  369. struct list_head node_list;
  370. struct mlxsw_sp_vr *vr;
  371. struct mlxsw_sp_lpm_tree *lpm_tree;
  372. enum mlxsw_sp_l3proto proto;
  373. };
  374. struct mlxsw_sp_vr {
  375. u16 id; /* virtual router ID */
  376. u32 tb_id; /* kernel fib table id */
  377. unsigned int rif_count;
  378. struct mlxsw_sp_fib *fib4;
  379. struct mlxsw_sp_fib *fib6;
  380. struct mlxsw_sp_mr_table *mr_table[MLXSW_SP_L3_PROTO_MAX];
  381. };
  382. static const struct rhashtable_params mlxsw_sp_fib_ht_params;
  383. static struct mlxsw_sp_fib *mlxsw_sp_fib_create(struct mlxsw_sp *mlxsw_sp,
  384. struct mlxsw_sp_vr *vr,
  385. enum mlxsw_sp_l3proto proto)
  386. {
  387. struct mlxsw_sp_lpm_tree *lpm_tree;
  388. struct mlxsw_sp_fib *fib;
  389. int err;
  390. lpm_tree = mlxsw_sp->router->lpm.proto_trees[proto];
  391. fib = kzalloc(sizeof(*fib), GFP_KERNEL);
  392. if (!fib)
  393. return ERR_PTR(-ENOMEM);
  394. err = rhashtable_init(&fib->ht, &mlxsw_sp_fib_ht_params);
  395. if (err)
  396. goto err_rhashtable_init;
  397. INIT_LIST_HEAD(&fib->node_list);
  398. fib->proto = proto;
  399. fib->vr = vr;
  400. fib->lpm_tree = lpm_tree;
  401. mlxsw_sp_lpm_tree_hold(lpm_tree);
  402. err = mlxsw_sp_vr_lpm_tree_bind(mlxsw_sp, fib, lpm_tree->id);
  403. if (err)
  404. goto err_lpm_tree_bind;
  405. return fib;
  406. err_lpm_tree_bind:
  407. mlxsw_sp_lpm_tree_put(mlxsw_sp, lpm_tree);
  408. err_rhashtable_init:
  409. kfree(fib);
  410. return ERR_PTR(err);
  411. }
  412. static void mlxsw_sp_fib_destroy(struct mlxsw_sp *mlxsw_sp,
  413. struct mlxsw_sp_fib *fib)
  414. {
  415. mlxsw_sp_vr_lpm_tree_unbind(mlxsw_sp, fib);
  416. mlxsw_sp_lpm_tree_put(mlxsw_sp, fib->lpm_tree);
  417. WARN_ON(!list_empty(&fib->node_list));
  418. rhashtable_destroy(&fib->ht);
  419. kfree(fib);
  420. }
  421. static struct mlxsw_sp_lpm_tree *
  422. mlxsw_sp_lpm_tree_find_unused(struct mlxsw_sp *mlxsw_sp)
  423. {
  424. static struct mlxsw_sp_lpm_tree *lpm_tree;
  425. int i;
  426. for (i = 0; i < mlxsw_sp->router->lpm.tree_count; i++) {
  427. lpm_tree = &mlxsw_sp->router->lpm.trees[i];
  428. if (lpm_tree->ref_count == 0)
  429. return lpm_tree;
  430. }
  431. return NULL;
  432. }
  433. static int mlxsw_sp_lpm_tree_alloc(struct mlxsw_sp *mlxsw_sp,
  434. struct mlxsw_sp_lpm_tree *lpm_tree)
  435. {
  436. char ralta_pl[MLXSW_REG_RALTA_LEN];
  437. mlxsw_reg_ralta_pack(ralta_pl, true,
  438. (enum mlxsw_reg_ralxx_protocol) lpm_tree->proto,
  439. lpm_tree->id);
  440. return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralta), ralta_pl);
  441. }
  442. static void mlxsw_sp_lpm_tree_free(struct mlxsw_sp *mlxsw_sp,
  443. struct mlxsw_sp_lpm_tree *lpm_tree)
  444. {
  445. char ralta_pl[MLXSW_REG_RALTA_LEN];
  446. mlxsw_reg_ralta_pack(ralta_pl, false,
  447. (enum mlxsw_reg_ralxx_protocol) lpm_tree->proto,
  448. lpm_tree->id);
  449. mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralta), ralta_pl);
  450. }
  451. static int
  452. mlxsw_sp_lpm_tree_left_struct_set(struct mlxsw_sp *mlxsw_sp,
  453. struct mlxsw_sp_prefix_usage *prefix_usage,
  454. struct mlxsw_sp_lpm_tree *lpm_tree)
  455. {
  456. char ralst_pl[MLXSW_REG_RALST_LEN];
  457. u8 root_bin = 0;
  458. u8 prefix;
  459. u8 last_prefix = MLXSW_REG_RALST_BIN_NO_CHILD;
  460. mlxsw_sp_prefix_usage_for_each(prefix, prefix_usage)
  461. root_bin = prefix;
  462. mlxsw_reg_ralst_pack(ralst_pl, root_bin, lpm_tree->id);
  463. mlxsw_sp_prefix_usage_for_each(prefix, prefix_usage) {
  464. if (prefix == 0)
  465. continue;
  466. mlxsw_reg_ralst_bin_pack(ralst_pl, prefix, last_prefix,
  467. MLXSW_REG_RALST_BIN_NO_CHILD);
  468. last_prefix = prefix;
  469. }
  470. return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralst), ralst_pl);
  471. }
  472. static struct mlxsw_sp_lpm_tree *
  473. mlxsw_sp_lpm_tree_create(struct mlxsw_sp *mlxsw_sp,
  474. struct mlxsw_sp_prefix_usage *prefix_usage,
  475. enum mlxsw_sp_l3proto proto)
  476. {
  477. struct mlxsw_sp_lpm_tree *lpm_tree;
  478. int err;
  479. lpm_tree = mlxsw_sp_lpm_tree_find_unused(mlxsw_sp);
  480. if (!lpm_tree)
  481. return ERR_PTR(-EBUSY);
  482. lpm_tree->proto = proto;
  483. err = mlxsw_sp_lpm_tree_alloc(mlxsw_sp, lpm_tree);
  484. if (err)
  485. return ERR_PTR(err);
  486. err = mlxsw_sp_lpm_tree_left_struct_set(mlxsw_sp, prefix_usage,
  487. lpm_tree);
  488. if (err)
  489. goto err_left_struct_set;
  490. memcpy(&lpm_tree->prefix_usage, prefix_usage,
  491. sizeof(lpm_tree->prefix_usage));
  492. memset(&lpm_tree->prefix_ref_count, 0,
  493. sizeof(lpm_tree->prefix_ref_count));
  494. lpm_tree->ref_count = 1;
  495. return lpm_tree;
  496. err_left_struct_set:
  497. mlxsw_sp_lpm_tree_free(mlxsw_sp, lpm_tree);
  498. return ERR_PTR(err);
  499. }
  500. static void mlxsw_sp_lpm_tree_destroy(struct mlxsw_sp *mlxsw_sp,
  501. struct mlxsw_sp_lpm_tree *lpm_tree)
  502. {
  503. mlxsw_sp_lpm_tree_free(mlxsw_sp, lpm_tree);
  504. }
  505. static struct mlxsw_sp_lpm_tree *
  506. mlxsw_sp_lpm_tree_get(struct mlxsw_sp *mlxsw_sp,
  507. struct mlxsw_sp_prefix_usage *prefix_usage,
  508. enum mlxsw_sp_l3proto proto)
  509. {
  510. struct mlxsw_sp_lpm_tree *lpm_tree;
  511. int i;
  512. for (i = 0; i < mlxsw_sp->router->lpm.tree_count; i++) {
  513. lpm_tree = &mlxsw_sp->router->lpm.trees[i];
  514. if (lpm_tree->ref_count != 0 &&
  515. lpm_tree->proto == proto &&
  516. mlxsw_sp_prefix_usage_eq(&lpm_tree->prefix_usage,
  517. prefix_usage)) {
  518. mlxsw_sp_lpm_tree_hold(lpm_tree);
  519. return lpm_tree;
  520. }
  521. }
  522. return mlxsw_sp_lpm_tree_create(mlxsw_sp, prefix_usage, proto);
  523. }
  524. static void mlxsw_sp_lpm_tree_hold(struct mlxsw_sp_lpm_tree *lpm_tree)
  525. {
  526. lpm_tree->ref_count++;
  527. }
  528. static void mlxsw_sp_lpm_tree_put(struct mlxsw_sp *mlxsw_sp,
  529. struct mlxsw_sp_lpm_tree *lpm_tree)
  530. {
  531. if (--lpm_tree->ref_count == 0)
  532. mlxsw_sp_lpm_tree_destroy(mlxsw_sp, lpm_tree);
  533. }
  534. #define MLXSW_SP_LPM_TREE_MIN 1 /* tree 0 is reserved */
  535. static int mlxsw_sp_lpm_init(struct mlxsw_sp *mlxsw_sp)
  536. {
  537. struct mlxsw_sp_prefix_usage req_prefix_usage = {{ 0 } };
  538. struct mlxsw_sp_lpm_tree *lpm_tree;
  539. u64 max_trees;
  540. int err, i;
  541. if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_LPM_TREES))
  542. return -EIO;
  543. max_trees = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_LPM_TREES);
  544. mlxsw_sp->router->lpm.tree_count = max_trees - MLXSW_SP_LPM_TREE_MIN;
  545. mlxsw_sp->router->lpm.trees = kcalloc(mlxsw_sp->router->lpm.tree_count,
  546. sizeof(struct mlxsw_sp_lpm_tree),
  547. GFP_KERNEL);
  548. if (!mlxsw_sp->router->lpm.trees)
  549. return -ENOMEM;
  550. for (i = 0; i < mlxsw_sp->router->lpm.tree_count; i++) {
  551. lpm_tree = &mlxsw_sp->router->lpm.trees[i];
  552. lpm_tree->id = i + MLXSW_SP_LPM_TREE_MIN;
  553. }
  554. lpm_tree = mlxsw_sp_lpm_tree_get(mlxsw_sp, &req_prefix_usage,
  555. MLXSW_SP_L3_PROTO_IPV4);
  556. if (IS_ERR(lpm_tree)) {
  557. err = PTR_ERR(lpm_tree);
  558. goto err_ipv4_tree_get;
  559. }
  560. mlxsw_sp->router->lpm.proto_trees[MLXSW_SP_L3_PROTO_IPV4] = lpm_tree;
  561. lpm_tree = mlxsw_sp_lpm_tree_get(mlxsw_sp, &req_prefix_usage,
  562. MLXSW_SP_L3_PROTO_IPV6);
  563. if (IS_ERR(lpm_tree)) {
  564. err = PTR_ERR(lpm_tree);
  565. goto err_ipv6_tree_get;
  566. }
  567. mlxsw_sp->router->lpm.proto_trees[MLXSW_SP_L3_PROTO_IPV6] = lpm_tree;
  568. return 0;
  569. err_ipv6_tree_get:
  570. lpm_tree = mlxsw_sp->router->lpm.proto_trees[MLXSW_SP_L3_PROTO_IPV4];
  571. mlxsw_sp_lpm_tree_put(mlxsw_sp, lpm_tree);
  572. err_ipv4_tree_get:
  573. kfree(mlxsw_sp->router->lpm.trees);
  574. return err;
  575. }
  576. static void mlxsw_sp_lpm_fini(struct mlxsw_sp *mlxsw_sp)
  577. {
  578. struct mlxsw_sp_lpm_tree *lpm_tree;
  579. lpm_tree = mlxsw_sp->router->lpm.proto_trees[MLXSW_SP_L3_PROTO_IPV6];
  580. mlxsw_sp_lpm_tree_put(mlxsw_sp, lpm_tree);
  581. lpm_tree = mlxsw_sp->router->lpm.proto_trees[MLXSW_SP_L3_PROTO_IPV4];
  582. mlxsw_sp_lpm_tree_put(mlxsw_sp, lpm_tree);
  583. kfree(mlxsw_sp->router->lpm.trees);
  584. }
  585. static bool mlxsw_sp_vr_is_used(const struct mlxsw_sp_vr *vr)
  586. {
  587. return !!vr->fib4 || !!vr->fib6 ||
  588. !!vr->mr_table[MLXSW_SP_L3_PROTO_IPV4] ||
  589. !!vr->mr_table[MLXSW_SP_L3_PROTO_IPV6];
  590. }
  591. static struct mlxsw_sp_vr *mlxsw_sp_vr_find_unused(struct mlxsw_sp *mlxsw_sp)
  592. {
  593. struct mlxsw_sp_vr *vr;
  594. int i;
  595. for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS); i++) {
  596. vr = &mlxsw_sp->router->vrs[i];
  597. if (!mlxsw_sp_vr_is_used(vr))
  598. return vr;
  599. }
  600. return NULL;
  601. }
  602. static int mlxsw_sp_vr_lpm_tree_bind(struct mlxsw_sp *mlxsw_sp,
  603. const struct mlxsw_sp_fib *fib, u8 tree_id)
  604. {
  605. char raltb_pl[MLXSW_REG_RALTB_LEN];
  606. mlxsw_reg_raltb_pack(raltb_pl, fib->vr->id,
  607. (enum mlxsw_reg_ralxx_protocol) fib->proto,
  608. tree_id);
  609. return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(raltb), raltb_pl);
  610. }
  611. static int mlxsw_sp_vr_lpm_tree_unbind(struct mlxsw_sp *mlxsw_sp,
  612. const struct mlxsw_sp_fib *fib)
  613. {
  614. char raltb_pl[MLXSW_REG_RALTB_LEN];
  615. /* Bind to tree 0 which is default */
  616. mlxsw_reg_raltb_pack(raltb_pl, fib->vr->id,
  617. (enum mlxsw_reg_ralxx_protocol) fib->proto, 0);
  618. return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(raltb), raltb_pl);
  619. }
  620. static u32 mlxsw_sp_fix_tb_id(u32 tb_id)
  621. {
  622. /* For our purpose, squash main, default and local tables into one */
  623. if (tb_id == RT_TABLE_LOCAL || tb_id == RT_TABLE_DEFAULT)
  624. tb_id = RT_TABLE_MAIN;
  625. return tb_id;
  626. }
  627. static struct mlxsw_sp_vr *mlxsw_sp_vr_find(struct mlxsw_sp *mlxsw_sp,
  628. u32 tb_id)
  629. {
  630. struct mlxsw_sp_vr *vr;
  631. int i;
  632. tb_id = mlxsw_sp_fix_tb_id(tb_id);
  633. for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS); i++) {
  634. vr = &mlxsw_sp->router->vrs[i];
  635. if (mlxsw_sp_vr_is_used(vr) && vr->tb_id == tb_id)
  636. return vr;
  637. }
  638. return NULL;
  639. }
  640. static struct mlxsw_sp_fib *mlxsw_sp_vr_fib(const struct mlxsw_sp_vr *vr,
  641. enum mlxsw_sp_l3proto proto)
  642. {
  643. switch (proto) {
  644. case MLXSW_SP_L3_PROTO_IPV4:
  645. return vr->fib4;
  646. case MLXSW_SP_L3_PROTO_IPV6:
  647. return vr->fib6;
  648. }
  649. return NULL;
  650. }
  651. static struct mlxsw_sp_vr *mlxsw_sp_vr_create(struct mlxsw_sp *mlxsw_sp,
  652. u32 tb_id,
  653. struct netlink_ext_ack *extack)
  654. {
  655. struct mlxsw_sp_mr_table *mr4_table, *mr6_table;
  656. struct mlxsw_sp_fib *fib4;
  657. struct mlxsw_sp_fib *fib6;
  658. struct mlxsw_sp_vr *vr;
  659. int err;
  660. vr = mlxsw_sp_vr_find_unused(mlxsw_sp);
  661. if (!vr) {
  662. NL_SET_ERR_MSG_MOD(extack, "Exceeded number of supported virtual routers");
  663. return ERR_PTR(-EBUSY);
  664. }
  665. fib4 = mlxsw_sp_fib_create(mlxsw_sp, vr, MLXSW_SP_L3_PROTO_IPV4);
  666. if (IS_ERR(fib4))
  667. return ERR_CAST(fib4);
  668. fib6 = mlxsw_sp_fib_create(mlxsw_sp, vr, MLXSW_SP_L3_PROTO_IPV6);
  669. if (IS_ERR(fib6)) {
  670. err = PTR_ERR(fib6);
  671. goto err_fib6_create;
  672. }
  673. mr4_table = mlxsw_sp_mr_table_create(mlxsw_sp, vr->id,
  674. MLXSW_SP_L3_PROTO_IPV4);
  675. if (IS_ERR(mr4_table)) {
  676. err = PTR_ERR(mr4_table);
  677. goto err_mr4_table_create;
  678. }
  679. mr6_table = mlxsw_sp_mr_table_create(mlxsw_sp, vr->id,
  680. MLXSW_SP_L3_PROTO_IPV6);
  681. if (IS_ERR(mr6_table)) {
  682. err = PTR_ERR(mr6_table);
  683. goto err_mr6_table_create;
  684. }
  685. vr->fib4 = fib4;
  686. vr->fib6 = fib6;
  687. vr->mr_table[MLXSW_SP_L3_PROTO_IPV4] = mr4_table;
  688. vr->mr_table[MLXSW_SP_L3_PROTO_IPV6] = mr6_table;
  689. vr->tb_id = tb_id;
  690. return vr;
  691. err_mr6_table_create:
  692. mlxsw_sp_mr_table_destroy(mr4_table);
  693. err_mr4_table_create:
  694. mlxsw_sp_fib_destroy(mlxsw_sp, fib6);
  695. err_fib6_create:
  696. mlxsw_sp_fib_destroy(mlxsw_sp, fib4);
  697. return ERR_PTR(err);
  698. }
  699. static void mlxsw_sp_vr_destroy(struct mlxsw_sp *mlxsw_sp,
  700. struct mlxsw_sp_vr *vr)
  701. {
  702. mlxsw_sp_mr_table_destroy(vr->mr_table[MLXSW_SP_L3_PROTO_IPV6]);
  703. vr->mr_table[MLXSW_SP_L3_PROTO_IPV6] = NULL;
  704. mlxsw_sp_mr_table_destroy(vr->mr_table[MLXSW_SP_L3_PROTO_IPV4]);
  705. vr->mr_table[MLXSW_SP_L3_PROTO_IPV4] = NULL;
  706. mlxsw_sp_fib_destroy(mlxsw_sp, vr->fib6);
  707. vr->fib6 = NULL;
  708. mlxsw_sp_fib_destroy(mlxsw_sp, vr->fib4);
  709. vr->fib4 = NULL;
  710. }
  711. static struct mlxsw_sp_vr *mlxsw_sp_vr_get(struct mlxsw_sp *mlxsw_sp, u32 tb_id,
  712. struct netlink_ext_ack *extack)
  713. {
  714. struct mlxsw_sp_vr *vr;
  715. tb_id = mlxsw_sp_fix_tb_id(tb_id);
  716. vr = mlxsw_sp_vr_find(mlxsw_sp, tb_id);
  717. if (!vr)
  718. vr = mlxsw_sp_vr_create(mlxsw_sp, tb_id, extack);
  719. return vr;
  720. }
  721. static void mlxsw_sp_vr_put(struct mlxsw_sp *mlxsw_sp, struct mlxsw_sp_vr *vr)
  722. {
  723. if (!vr->rif_count && list_empty(&vr->fib4->node_list) &&
  724. list_empty(&vr->fib6->node_list) &&
  725. mlxsw_sp_mr_table_empty(vr->mr_table[MLXSW_SP_L3_PROTO_IPV4]) &&
  726. mlxsw_sp_mr_table_empty(vr->mr_table[MLXSW_SP_L3_PROTO_IPV6]))
  727. mlxsw_sp_vr_destroy(mlxsw_sp, vr);
  728. }
  729. static bool
  730. mlxsw_sp_vr_lpm_tree_should_replace(struct mlxsw_sp_vr *vr,
  731. enum mlxsw_sp_l3proto proto, u8 tree_id)
  732. {
  733. struct mlxsw_sp_fib *fib = mlxsw_sp_vr_fib(vr, proto);
  734. if (!mlxsw_sp_vr_is_used(vr))
  735. return false;
  736. if (fib->lpm_tree->id == tree_id)
  737. return true;
  738. return false;
  739. }
  740. static int mlxsw_sp_vr_lpm_tree_replace(struct mlxsw_sp *mlxsw_sp,
  741. struct mlxsw_sp_fib *fib,
  742. struct mlxsw_sp_lpm_tree *new_tree)
  743. {
  744. struct mlxsw_sp_lpm_tree *old_tree = fib->lpm_tree;
  745. int err;
  746. fib->lpm_tree = new_tree;
  747. mlxsw_sp_lpm_tree_hold(new_tree);
  748. err = mlxsw_sp_vr_lpm_tree_bind(mlxsw_sp, fib, new_tree->id);
  749. if (err)
  750. goto err_tree_bind;
  751. mlxsw_sp_lpm_tree_put(mlxsw_sp, old_tree);
  752. return 0;
  753. err_tree_bind:
  754. mlxsw_sp_lpm_tree_put(mlxsw_sp, new_tree);
  755. fib->lpm_tree = old_tree;
  756. return err;
  757. }
  758. static int mlxsw_sp_vrs_lpm_tree_replace(struct mlxsw_sp *mlxsw_sp,
  759. struct mlxsw_sp_fib *fib,
  760. struct mlxsw_sp_lpm_tree *new_tree)
  761. {
  762. enum mlxsw_sp_l3proto proto = fib->proto;
  763. struct mlxsw_sp_lpm_tree *old_tree;
  764. u8 old_id, new_id = new_tree->id;
  765. struct mlxsw_sp_vr *vr;
  766. int i, err;
  767. old_tree = mlxsw_sp->router->lpm.proto_trees[proto];
  768. old_id = old_tree->id;
  769. for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS); i++) {
  770. vr = &mlxsw_sp->router->vrs[i];
  771. if (!mlxsw_sp_vr_lpm_tree_should_replace(vr, proto, old_id))
  772. continue;
  773. err = mlxsw_sp_vr_lpm_tree_replace(mlxsw_sp,
  774. mlxsw_sp_vr_fib(vr, proto),
  775. new_tree);
  776. if (err)
  777. goto err_tree_replace;
  778. }
  779. memcpy(new_tree->prefix_ref_count, old_tree->prefix_ref_count,
  780. sizeof(new_tree->prefix_ref_count));
  781. mlxsw_sp->router->lpm.proto_trees[proto] = new_tree;
  782. mlxsw_sp_lpm_tree_put(mlxsw_sp, old_tree);
  783. return 0;
  784. err_tree_replace:
  785. for (i--; i >= 0; i--) {
  786. if (!mlxsw_sp_vr_lpm_tree_should_replace(vr, proto, new_id))
  787. continue;
  788. mlxsw_sp_vr_lpm_tree_replace(mlxsw_sp,
  789. mlxsw_sp_vr_fib(vr, proto),
  790. old_tree);
  791. }
  792. return err;
  793. }
  794. static int mlxsw_sp_vrs_init(struct mlxsw_sp *mlxsw_sp)
  795. {
  796. struct mlxsw_sp_vr *vr;
  797. u64 max_vrs;
  798. int i;
  799. if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_VRS))
  800. return -EIO;
  801. max_vrs = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS);
  802. mlxsw_sp->router->vrs = kcalloc(max_vrs, sizeof(struct mlxsw_sp_vr),
  803. GFP_KERNEL);
  804. if (!mlxsw_sp->router->vrs)
  805. return -ENOMEM;
  806. for (i = 0; i < max_vrs; i++) {
  807. vr = &mlxsw_sp->router->vrs[i];
  808. vr->id = i;
  809. }
  810. return 0;
  811. }
  812. static void mlxsw_sp_router_fib_flush(struct mlxsw_sp *mlxsw_sp);
  813. static void mlxsw_sp_vrs_fini(struct mlxsw_sp *mlxsw_sp)
  814. {
  815. /* At this stage we're guaranteed not to have new incoming
  816. * FIB notifications and the work queue is free from FIBs
  817. * sitting on top of mlxsw netdevs. However, we can still
  818. * have other FIBs queued. Flush the queue before flushing
  819. * the device's tables. No need for locks, as we're the only
  820. * writer.
  821. */
  822. mlxsw_core_flush_owq();
  823. mlxsw_sp_router_fib_flush(mlxsw_sp);
  824. kfree(mlxsw_sp->router->vrs);
  825. }
  826. static struct net_device *
  827. __mlxsw_sp_ipip_netdev_ul_dev_get(const struct net_device *ol_dev)
  828. {
  829. struct ip_tunnel *tun = netdev_priv(ol_dev);
  830. struct net *net = dev_net(ol_dev);
  831. return __dev_get_by_index(net, tun->parms.link);
  832. }
  833. u32 mlxsw_sp_ipip_dev_ul_tb_id(const struct net_device *ol_dev)
  834. {
  835. struct net_device *d = __mlxsw_sp_ipip_netdev_ul_dev_get(ol_dev);
  836. if (d)
  837. return l3mdev_fib_table(d) ? : RT_TABLE_MAIN;
  838. else
  839. return RT_TABLE_MAIN;
  840. }
  841. static struct mlxsw_sp_rif *
  842. mlxsw_sp_rif_create(struct mlxsw_sp *mlxsw_sp,
  843. const struct mlxsw_sp_rif_params *params,
  844. struct netlink_ext_ack *extack);
  845. static struct mlxsw_sp_rif_ipip_lb *
  846. mlxsw_sp_ipip_ol_ipip_lb_create(struct mlxsw_sp *mlxsw_sp,
  847. enum mlxsw_sp_ipip_type ipipt,
  848. struct net_device *ol_dev,
  849. struct netlink_ext_ack *extack)
  850. {
  851. struct mlxsw_sp_rif_params_ipip_lb lb_params;
  852. const struct mlxsw_sp_ipip_ops *ipip_ops;
  853. struct mlxsw_sp_rif *rif;
  854. ipip_ops = mlxsw_sp->router->ipip_ops_arr[ipipt];
  855. lb_params = (struct mlxsw_sp_rif_params_ipip_lb) {
  856. .common.dev = ol_dev,
  857. .common.lag = false,
  858. .lb_config = ipip_ops->ol_loopback_config(mlxsw_sp, ol_dev),
  859. };
  860. rif = mlxsw_sp_rif_create(mlxsw_sp, &lb_params.common, extack);
  861. if (IS_ERR(rif))
  862. return ERR_CAST(rif);
  863. return container_of(rif, struct mlxsw_sp_rif_ipip_lb, common);
  864. }
  865. static struct mlxsw_sp_ipip_entry *
  866. mlxsw_sp_ipip_entry_alloc(struct mlxsw_sp *mlxsw_sp,
  867. enum mlxsw_sp_ipip_type ipipt,
  868. struct net_device *ol_dev)
  869. {
  870. const struct mlxsw_sp_ipip_ops *ipip_ops;
  871. struct mlxsw_sp_ipip_entry *ipip_entry;
  872. struct mlxsw_sp_ipip_entry *ret = NULL;
  873. ipip_ops = mlxsw_sp->router->ipip_ops_arr[ipipt];
  874. ipip_entry = kzalloc(sizeof(*ipip_entry), GFP_KERNEL);
  875. if (!ipip_entry)
  876. return ERR_PTR(-ENOMEM);
  877. ipip_entry->ol_lb = mlxsw_sp_ipip_ol_ipip_lb_create(mlxsw_sp, ipipt,
  878. ol_dev, NULL);
  879. if (IS_ERR(ipip_entry->ol_lb)) {
  880. ret = ERR_CAST(ipip_entry->ol_lb);
  881. goto err_ol_ipip_lb_create;
  882. }
  883. ipip_entry->ipipt = ipipt;
  884. ipip_entry->ol_dev = ol_dev;
  885. switch (ipip_ops->ul_proto) {
  886. case MLXSW_SP_L3_PROTO_IPV4:
  887. ipip_entry->parms4 = mlxsw_sp_ipip_netdev_parms4(ol_dev);
  888. break;
  889. case MLXSW_SP_L3_PROTO_IPV6:
  890. WARN_ON(1);
  891. break;
  892. }
  893. return ipip_entry;
  894. err_ol_ipip_lb_create:
  895. kfree(ipip_entry);
  896. return ret;
  897. }
  898. static void
  899. mlxsw_sp_ipip_entry_dealloc(struct mlxsw_sp_ipip_entry *ipip_entry)
  900. {
  901. mlxsw_sp_rif_destroy(&ipip_entry->ol_lb->common);
  902. kfree(ipip_entry);
  903. }
  904. static bool
  905. mlxsw_sp_ipip_entry_saddr_matches(struct mlxsw_sp *mlxsw_sp,
  906. const enum mlxsw_sp_l3proto ul_proto,
  907. union mlxsw_sp_l3addr saddr,
  908. u32 ul_tb_id,
  909. struct mlxsw_sp_ipip_entry *ipip_entry)
  910. {
  911. u32 tun_ul_tb_id = mlxsw_sp_ipip_dev_ul_tb_id(ipip_entry->ol_dev);
  912. enum mlxsw_sp_ipip_type ipipt = ipip_entry->ipipt;
  913. union mlxsw_sp_l3addr tun_saddr;
  914. if (mlxsw_sp->router->ipip_ops_arr[ipipt]->ul_proto != ul_proto)
  915. return false;
  916. tun_saddr = mlxsw_sp_ipip_netdev_saddr(ul_proto, ipip_entry->ol_dev);
  917. return tun_ul_tb_id == ul_tb_id &&
  918. mlxsw_sp_l3addr_eq(&tun_saddr, &saddr);
  919. }
  920. static int
  921. mlxsw_sp_fib_entry_decap_init(struct mlxsw_sp *mlxsw_sp,
  922. struct mlxsw_sp_fib_entry *fib_entry,
  923. struct mlxsw_sp_ipip_entry *ipip_entry)
  924. {
  925. u32 tunnel_index;
  926. int err;
  927. err = mlxsw_sp_kvdl_alloc(mlxsw_sp, MLXSW_SP_KVDL_ENTRY_TYPE_ADJ,
  928. 1, &tunnel_index);
  929. if (err)
  930. return err;
  931. ipip_entry->decap_fib_entry = fib_entry;
  932. fib_entry->decap.ipip_entry = ipip_entry;
  933. fib_entry->decap.tunnel_index = tunnel_index;
  934. return 0;
  935. }
  936. static void mlxsw_sp_fib_entry_decap_fini(struct mlxsw_sp *mlxsw_sp,
  937. struct mlxsw_sp_fib_entry *fib_entry)
  938. {
  939. /* Unlink this node from the IPIP entry that it's the decap entry of. */
  940. fib_entry->decap.ipip_entry->decap_fib_entry = NULL;
  941. fib_entry->decap.ipip_entry = NULL;
  942. mlxsw_sp_kvdl_free(mlxsw_sp, MLXSW_SP_KVDL_ENTRY_TYPE_ADJ,
  943. 1, fib_entry->decap.tunnel_index);
  944. }
  945. static struct mlxsw_sp_fib_node *
  946. mlxsw_sp_fib_node_lookup(struct mlxsw_sp_fib *fib, const void *addr,
  947. size_t addr_len, unsigned char prefix_len);
  948. static int mlxsw_sp_fib_entry_update(struct mlxsw_sp *mlxsw_sp,
  949. struct mlxsw_sp_fib_entry *fib_entry);
  950. static void
  951. mlxsw_sp_ipip_entry_demote_decap(struct mlxsw_sp *mlxsw_sp,
  952. struct mlxsw_sp_ipip_entry *ipip_entry)
  953. {
  954. struct mlxsw_sp_fib_entry *fib_entry = ipip_entry->decap_fib_entry;
  955. mlxsw_sp_fib_entry_decap_fini(mlxsw_sp, fib_entry);
  956. fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_TRAP;
  957. mlxsw_sp_fib_entry_update(mlxsw_sp, fib_entry);
  958. }
  959. static void
  960. mlxsw_sp_ipip_entry_promote_decap(struct mlxsw_sp *mlxsw_sp,
  961. struct mlxsw_sp_ipip_entry *ipip_entry,
  962. struct mlxsw_sp_fib_entry *decap_fib_entry)
  963. {
  964. if (mlxsw_sp_fib_entry_decap_init(mlxsw_sp, decap_fib_entry,
  965. ipip_entry))
  966. return;
  967. decap_fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_IPIP_DECAP;
  968. if (mlxsw_sp_fib_entry_update(mlxsw_sp, decap_fib_entry))
  969. mlxsw_sp_ipip_entry_demote_decap(mlxsw_sp, ipip_entry);
  970. }
  971. /* Given an IPIP entry, find the corresponding decap route. */
  972. static struct mlxsw_sp_fib_entry *
  973. mlxsw_sp_ipip_entry_find_decap(struct mlxsw_sp *mlxsw_sp,
  974. struct mlxsw_sp_ipip_entry *ipip_entry)
  975. {
  976. static struct mlxsw_sp_fib_node *fib_node;
  977. const struct mlxsw_sp_ipip_ops *ipip_ops;
  978. struct mlxsw_sp_fib_entry *fib_entry;
  979. unsigned char saddr_prefix_len;
  980. union mlxsw_sp_l3addr saddr;
  981. struct mlxsw_sp_fib *ul_fib;
  982. struct mlxsw_sp_vr *ul_vr;
  983. const void *saddrp;
  984. size_t saddr_len;
  985. u32 ul_tb_id;
  986. u32 saddr4;
  987. ipip_ops = mlxsw_sp->router->ipip_ops_arr[ipip_entry->ipipt];
  988. ul_tb_id = mlxsw_sp_ipip_dev_ul_tb_id(ipip_entry->ol_dev);
  989. ul_vr = mlxsw_sp_vr_find(mlxsw_sp, ul_tb_id);
  990. if (!ul_vr)
  991. return NULL;
  992. ul_fib = mlxsw_sp_vr_fib(ul_vr, ipip_ops->ul_proto);
  993. saddr = mlxsw_sp_ipip_netdev_saddr(ipip_ops->ul_proto,
  994. ipip_entry->ol_dev);
  995. switch (ipip_ops->ul_proto) {
  996. case MLXSW_SP_L3_PROTO_IPV4:
  997. saddr4 = be32_to_cpu(saddr.addr4);
  998. saddrp = &saddr4;
  999. saddr_len = 4;
  1000. saddr_prefix_len = 32;
  1001. break;
  1002. case MLXSW_SP_L3_PROTO_IPV6:
  1003. WARN_ON(1);
  1004. return NULL;
  1005. }
  1006. fib_node = mlxsw_sp_fib_node_lookup(ul_fib, saddrp, saddr_len,
  1007. saddr_prefix_len);
  1008. if (!fib_node || list_empty(&fib_node->entry_list))
  1009. return NULL;
  1010. fib_entry = list_first_entry(&fib_node->entry_list,
  1011. struct mlxsw_sp_fib_entry, list);
  1012. if (fib_entry->type != MLXSW_SP_FIB_ENTRY_TYPE_TRAP)
  1013. return NULL;
  1014. return fib_entry;
  1015. }
  1016. static struct mlxsw_sp_ipip_entry *
  1017. mlxsw_sp_ipip_entry_create(struct mlxsw_sp *mlxsw_sp,
  1018. enum mlxsw_sp_ipip_type ipipt,
  1019. struct net_device *ol_dev)
  1020. {
  1021. struct mlxsw_sp_ipip_entry *ipip_entry;
  1022. ipip_entry = mlxsw_sp_ipip_entry_alloc(mlxsw_sp, ipipt, ol_dev);
  1023. if (IS_ERR(ipip_entry))
  1024. return ipip_entry;
  1025. list_add_tail(&ipip_entry->ipip_list_node,
  1026. &mlxsw_sp->router->ipip_list);
  1027. return ipip_entry;
  1028. }
  1029. static void
  1030. mlxsw_sp_ipip_entry_destroy(struct mlxsw_sp *mlxsw_sp,
  1031. struct mlxsw_sp_ipip_entry *ipip_entry)
  1032. {
  1033. list_del(&ipip_entry->ipip_list_node);
  1034. mlxsw_sp_ipip_entry_dealloc(ipip_entry);
  1035. }
  1036. static bool
  1037. mlxsw_sp_ipip_entry_matches_decap(struct mlxsw_sp *mlxsw_sp,
  1038. const struct net_device *ul_dev,
  1039. enum mlxsw_sp_l3proto ul_proto,
  1040. union mlxsw_sp_l3addr ul_dip,
  1041. struct mlxsw_sp_ipip_entry *ipip_entry)
  1042. {
  1043. u32 ul_tb_id = l3mdev_fib_table(ul_dev) ? : RT_TABLE_MAIN;
  1044. enum mlxsw_sp_ipip_type ipipt = ipip_entry->ipipt;
  1045. if (mlxsw_sp->router->ipip_ops_arr[ipipt]->ul_proto != ul_proto)
  1046. return false;
  1047. return mlxsw_sp_ipip_entry_saddr_matches(mlxsw_sp, ul_proto, ul_dip,
  1048. ul_tb_id, ipip_entry);
  1049. }
  1050. /* Given decap parameters, find the corresponding IPIP entry. */
  1051. static struct mlxsw_sp_ipip_entry *
  1052. mlxsw_sp_ipip_entry_find_by_decap(struct mlxsw_sp *mlxsw_sp,
  1053. const struct net_device *ul_dev,
  1054. enum mlxsw_sp_l3proto ul_proto,
  1055. union mlxsw_sp_l3addr ul_dip)
  1056. {
  1057. struct mlxsw_sp_ipip_entry *ipip_entry;
  1058. list_for_each_entry(ipip_entry, &mlxsw_sp->router->ipip_list,
  1059. ipip_list_node)
  1060. if (mlxsw_sp_ipip_entry_matches_decap(mlxsw_sp, ul_dev,
  1061. ul_proto, ul_dip,
  1062. ipip_entry))
  1063. return ipip_entry;
  1064. return NULL;
  1065. }
  1066. static bool mlxsw_sp_netdev_ipip_type(const struct mlxsw_sp *mlxsw_sp,
  1067. const struct net_device *dev,
  1068. enum mlxsw_sp_ipip_type *p_type)
  1069. {
  1070. struct mlxsw_sp_router *router = mlxsw_sp->router;
  1071. const struct mlxsw_sp_ipip_ops *ipip_ops;
  1072. enum mlxsw_sp_ipip_type ipipt;
  1073. for (ipipt = 0; ipipt < MLXSW_SP_IPIP_TYPE_MAX; ++ipipt) {
  1074. ipip_ops = router->ipip_ops_arr[ipipt];
  1075. if (dev->type == ipip_ops->dev_type) {
  1076. if (p_type)
  1077. *p_type = ipipt;
  1078. return true;
  1079. }
  1080. }
  1081. return false;
  1082. }
  1083. bool mlxsw_sp_netdev_is_ipip_ol(const struct mlxsw_sp *mlxsw_sp,
  1084. const struct net_device *dev)
  1085. {
  1086. return mlxsw_sp_netdev_ipip_type(mlxsw_sp, dev, NULL);
  1087. }
  1088. static struct mlxsw_sp_ipip_entry *
  1089. mlxsw_sp_ipip_entry_find_by_ol_dev(struct mlxsw_sp *mlxsw_sp,
  1090. const struct net_device *ol_dev)
  1091. {
  1092. struct mlxsw_sp_ipip_entry *ipip_entry;
  1093. list_for_each_entry(ipip_entry, &mlxsw_sp->router->ipip_list,
  1094. ipip_list_node)
  1095. if (ipip_entry->ol_dev == ol_dev)
  1096. return ipip_entry;
  1097. return NULL;
  1098. }
  1099. static struct mlxsw_sp_ipip_entry *
  1100. mlxsw_sp_ipip_entry_find_by_ul_dev(const struct mlxsw_sp *mlxsw_sp,
  1101. const struct net_device *ul_dev,
  1102. struct mlxsw_sp_ipip_entry *start)
  1103. {
  1104. struct mlxsw_sp_ipip_entry *ipip_entry;
  1105. ipip_entry = list_prepare_entry(start, &mlxsw_sp->router->ipip_list,
  1106. ipip_list_node);
  1107. list_for_each_entry_continue(ipip_entry, &mlxsw_sp->router->ipip_list,
  1108. ipip_list_node) {
  1109. struct net_device *ipip_ul_dev =
  1110. __mlxsw_sp_ipip_netdev_ul_dev_get(ipip_entry->ol_dev);
  1111. if (ipip_ul_dev == ul_dev)
  1112. return ipip_entry;
  1113. }
  1114. return NULL;
  1115. }
  1116. bool mlxsw_sp_netdev_is_ipip_ul(const struct mlxsw_sp *mlxsw_sp,
  1117. const struct net_device *dev)
  1118. {
  1119. return mlxsw_sp_ipip_entry_find_by_ul_dev(mlxsw_sp, dev, NULL);
  1120. }
  1121. static bool mlxsw_sp_netdevice_ipip_can_offload(struct mlxsw_sp *mlxsw_sp,
  1122. const struct net_device *ol_dev,
  1123. enum mlxsw_sp_ipip_type ipipt)
  1124. {
  1125. const struct mlxsw_sp_ipip_ops *ops
  1126. = mlxsw_sp->router->ipip_ops_arr[ipipt];
  1127. /* For deciding whether decap should be offloaded, we don't care about
  1128. * overlay protocol, so ask whether either one is supported.
  1129. */
  1130. return ops->can_offload(mlxsw_sp, ol_dev, MLXSW_SP_L3_PROTO_IPV4) ||
  1131. ops->can_offload(mlxsw_sp, ol_dev, MLXSW_SP_L3_PROTO_IPV6);
  1132. }
  1133. static int mlxsw_sp_netdevice_ipip_ol_reg_event(struct mlxsw_sp *mlxsw_sp,
  1134. struct net_device *ol_dev)
  1135. {
  1136. struct mlxsw_sp_ipip_entry *ipip_entry;
  1137. enum mlxsw_sp_l3proto ul_proto;
  1138. enum mlxsw_sp_ipip_type ipipt;
  1139. union mlxsw_sp_l3addr saddr;
  1140. u32 ul_tb_id;
  1141. mlxsw_sp_netdev_ipip_type(mlxsw_sp, ol_dev, &ipipt);
  1142. if (mlxsw_sp_netdevice_ipip_can_offload(mlxsw_sp, ol_dev, ipipt)) {
  1143. ul_tb_id = mlxsw_sp_ipip_dev_ul_tb_id(ol_dev);
  1144. ul_proto = mlxsw_sp->router->ipip_ops_arr[ipipt]->ul_proto;
  1145. saddr = mlxsw_sp_ipip_netdev_saddr(ul_proto, ol_dev);
  1146. if (!mlxsw_sp_ipip_demote_tunnel_by_saddr(mlxsw_sp, ul_proto,
  1147. saddr, ul_tb_id,
  1148. NULL)) {
  1149. ipip_entry = mlxsw_sp_ipip_entry_create(mlxsw_sp, ipipt,
  1150. ol_dev);
  1151. if (IS_ERR(ipip_entry))
  1152. return PTR_ERR(ipip_entry);
  1153. }
  1154. }
  1155. return 0;
  1156. }
  1157. static void mlxsw_sp_netdevice_ipip_ol_unreg_event(struct mlxsw_sp *mlxsw_sp,
  1158. struct net_device *ol_dev)
  1159. {
  1160. struct mlxsw_sp_ipip_entry *ipip_entry;
  1161. ipip_entry = mlxsw_sp_ipip_entry_find_by_ol_dev(mlxsw_sp, ol_dev);
  1162. if (ipip_entry)
  1163. mlxsw_sp_ipip_entry_destroy(mlxsw_sp, ipip_entry);
  1164. }
  1165. static void
  1166. mlxsw_sp_ipip_entry_ol_up_event(struct mlxsw_sp *mlxsw_sp,
  1167. struct mlxsw_sp_ipip_entry *ipip_entry)
  1168. {
  1169. struct mlxsw_sp_fib_entry *decap_fib_entry;
  1170. decap_fib_entry = mlxsw_sp_ipip_entry_find_decap(mlxsw_sp, ipip_entry);
  1171. if (decap_fib_entry)
  1172. mlxsw_sp_ipip_entry_promote_decap(mlxsw_sp, ipip_entry,
  1173. decap_fib_entry);
  1174. }
  1175. static int
  1176. mlxsw_sp_rif_ipip_lb_op(struct mlxsw_sp_rif_ipip_lb *lb_rif,
  1177. struct mlxsw_sp_vr *ul_vr, bool enable)
  1178. {
  1179. struct mlxsw_sp_rif_ipip_lb_config lb_cf = lb_rif->lb_config;
  1180. struct mlxsw_sp_rif *rif = &lb_rif->common;
  1181. struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
  1182. char ritr_pl[MLXSW_REG_RITR_LEN];
  1183. u32 saddr4;
  1184. switch (lb_cf.ul_protocol) {
  1185. case MLXSW_SP_L3_PROTO_IPV4:
  1186. saddr4 = be32_to_cpu(lb_cf.saddr.addr4);
  1187. mlxsw_reg_ritr_pack(ritr_pl, enable, MLXSW_REG_RITR_LOOPBACK_IF,
  1188. rif->rif_index, rif->vr_id, rif->dev->mtu);
  1189. mlxsw_reg_ritr_loopback_ipip4_pack(ritr_pl, lb_cf.lb_ipipt,
  1190. MLXSW_REG_RITR_LOOPBACK_IPIP_OPTIONS_GRE_KEY_PRESET,
  1191. ul_vr->id, saddr4, lb_cf.okey);
  1192. break;
  1193. case MLXSW_SP_L3_PROTO_IPV6:
  1194. return -EAFNOSUPPORT;
  1195. }
  1196. return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
  1197. }
  1198. static int mlxsw_sp_netdevice_ipip_ol_update_mtu(struct mlxsw_sp *mlxsw_sp,
  1199. struct net_device *ol_dev)
  1200. {
  1201. struct mlxsw_sp_ipip_entry *ipip_entry;
  1202. struct mlxsw_sp_rif_ipip_lb *lb_rif;
  1203. struct mlxsw_sp_vr *ul_vr;
  1204. int err = 0;
  1205. ipip_entry = mlxsw_sp_ipip_entry_find_by_ol_dev(mlxsw_sp, ol_dev);
  1206. if (ipip_entry) {
  1207. lb_rif = ipip_entry->ol_lb;
  1208. ul_vr = &mlxsw_sp->router->vrs[lb_rif->ul_vr_id];
  1209. err = mlxsw_sp_rif_ipip_lb_op(lb_rif, ul_vr, true);
  1210. if (err)
  1211. goto out;
  1212. lb_rif->common.mtu = ol_dev->mtu;
  1213. }
  1214. out:
  1215. return err;
  1216. }
  1217. static void mlxsw_sp_netdevice_ipip_ol_up_event(struct mlxsw_sp *mlxsw_sp,
  1218. struct net_device *ol_dev)
  1219. {
  1220. struct mlxsw_sp_ipip_entry *ipip_entry;
  1221. ipip_entry = mlxsw_sp_ipip_entry_find_by_ol_dev(mlxsw_sp, ol_dev);
  1222. if (ipip_entry)
  1223. mlxsw_sp_ipip_entry_ol_up_event(mlxsw_sp, ipip_entry);
  1224. }
  1225. static void
  1226. mlxsw_sp_ipip_entry_ol_down_event(struct mlxsw_sp *mlxsw_sp,
  1227. struct mlxsw_sp_ipip_entry *ipip_entry)
  1228. {
  1229. if (ipip_entry->decap_fib_entry)
  1230. mlxsw_sp_ipip_entry_demote_decap(mlxsw_sp, ipip_entry);
  1231. }
  1232. static void mlxsw_sp_netdevice_ipip_ol_down_event(struct mlxsw_sp *mlxsw_sp,
  1233. struct net_device *ol_dev)
  1234. {
  1235. struct mlxsw_sp_ipip_entry *ipip_entry;
  1236. ipip_entry = mlxsw_sp_ipip_entry_find_by_ol_dev(mlxsw_sp, ol_dev);
  1237. if (ipip_entry)
  1238. mlxsw_sp_ipip_entry_ol_down_event(mlxsw_sp, ipip_entry);
  1239. }
  1240. static void mlxsw_sp_nexthop_rif_migrate(struct mlxsw_sp *mlxsw_sp,
  1241. struct mlxsw_sp_rif *old_rif,
  1242. struct mlxsw_sp_rif *new_rif);
  1243. static int
  1244. mlxsw_sp_ipip_entry_ol_lb_update(struct mlxsw_sp *mlxsw_sp,
  1245. struct mlxsw_sp_ipip_entry *ipip_entry,
  1246. bool keep_encap,
  1247. struct netlink_ext_ack *extack)
  1248. {
  1249. struct mlxsw_sp_rif_ipip_lb *old_lb_rif = ipip_entry->ol_lb;
  1250. struct mlxsw_sp_rif_ipip_lb *new_lb_rif;
  1251. new_lb_rif = mlxsw_sp_ipip_ol_ipip_lb_create(mlxsw_sp,
  1252. ipip_entry->ipipt,
  1253. ipip_entry->ol_dev,
  1254. extack);
  1255. if (IS_ERR(new_lb_rif))
  1256. return PTR_ERR(new_lb_rif);
  1257. ipip_entry->ol_lb = new_lb_rif;
  1258. if (keep_encap)
  1259. mlxsw_sp_nexthop_rif_migrate(mlxsw_sp, &old_lb_rif->common,
  1260. &new_lb_rif->common);
  1261. mlxsw_sp_rif_destroy(&old_lb_rif->common);
  1262. return 0;
  1263. }
  1264. static void mlxsw_sp_nexthop_rif_update(struct mlxsw_sp *mlxsw_sp,
  1265. struct mlxsw_sp_rif *rif);
  1266. /**
  1267. * Update the offload related to an IPIP entry. This always updates decap, and
  1268. * in addition to that it also:
  1269. * @recreate_loopback: recreates the associated loopback RIF
  1270. * @keep_encap: updates next hops that use the tunnel netdevice. This is only
  1271. * relevant when recreate_loopback is true.
  1272. * @update_nexthops: updates next hops, keeping the current loopback RIF. This
  1273. * is only relevant when recreate_loopback is false.
  1274. */
  1275. int __mlxsw_sp_ipip_entry_update_tunnel(struct mlxsw_sp *mlxsw_sp,
  1276. struct mlxsw_sp_ipip_entry *ipip_entry,
  1277. bool recreate_loopback,
  1278. bool keep_encap,
  1279. bool update_nexthops,
  1280. struct netlink_ext_ack *extack)
  1281. {
  1282. int err;
  1283. /* RIFs can't be edited, so to update loopback, we need to destroy and
  1284. * recreate it. That creates a window of opportunity where RALUE and
  1285. * RATR registers end up referencing a RIF that's already gone. RATRs
  1286. * are handled in mlxsw_sp_ipip_entry_ol_lb_update(), and to take care
  1287. * of RALUE, demote the decap route back.
  1288. */
  1289. if (ipip_entry->decap_fib_entry)
  1290. mlxsw_sp_ipip_entry_demote_decap(mlxsw_sp, ipip_entry);
  1291. if (recreate_loopback) {
  1292. err = mlxsw_sp_ipip_entry_ol_lb_update(mlxsw_sp, ipip_entry,
  1293. keep_encap, extack);
  1294. if (err)
  1295. return err;
  1296. } else if (update_nexthops) {
  1297. mlxsw_sp_nexthop_rif_update(mlxsw_sp,
  1298. &ipip_entry->ol_lb->common);
  1299. }
  1300. if (ipip_entry->ol_dev->flags & IFF_UP)
  1301. mlxsw_sp_ipip_entry_ol_up_event(mlxsw_sp, ipip_entry);
  1302. return 0;
  1303. }
  1304. static int mlxsw_sp_netdevice_ipip_ol_vrf_event(struct mlxsw_sp *mlxsw_sp,
  1305. struct net_device *ol_dev,
  1306. struct netlink_ext_ack *extack)
  1307. {
  1308. struct mlxsw_sp_ipip_entry *ipip_entry =
  1309. mlxsw_sp_ipip_entry_find_by_ol_dev(mlxsw_sp, ol_dev);
  1310. if (!ipip_entry)
  1311. return 0;
  1312. return __mlxsw_sp_ipip_entry_update_tunnel(mlxsw_sp, ipip_entry,
  1313. true, false, false, extack);
  1314. }
  1315. static int
  1316. mlxsw_sp_netdevice_ipip_ul_vrf_event(struct mlxsw_sp *mlxsw_sp,
  1317. struct mlxsw_sp_ipip_entry *ipip_entry,
  1318. struct net_device *ul_dev,
  1319. struct netlink_ext_ack *extack)
  1320. {
  1321. return __mlxsw_sp_ipip_entry_update_tunnel(mlxsw_sp, ipip_entry,
  1322. true, true, false, extack);
  1323. }
  1324. static int
  1325. mlxsw_sp_netdevice_ipip_ul_up_event(struct mlxsw_sp *mlxsw_sp,
  1326. struct mlxsw_sp_ipip_entry *ipip_entry,
  1327. struct net_device *ul_dev)
  1328. {
  1329. return __mlxsw_sp_ipip_entry_update_tunnel(mlxsw_sp, ipip_entry,
  1330. false, false, true, NULL);
  1331. }
  1332. static int
  1333. mlxsw_sp_netdevice_ipip_ul_down_event(struct mlxsw_sp *mlxsw_sp,
  1334. struct mlxsw_sp_ipip_entry *ipip_entry,
  1335. struct net_device *ul_dev)
  1336. {
  1337. /* A down underlay device causes encapsulated packets to not be
  1338. * forwarded, but decap still works. So refresh next hops without
  1339. * touching anything else.
  1340. */
  1341. return __mlxsw_sp_ipip_entry_update_tunnel(mlxsw_sp, ipip_entry,
  1342. false, false, true, NULL);
  1343. }
  1344. static int
  1345. mlxsw_sp_netdevice_ipip_ol_change_event(struct mlxsw_sp *mlxsw_sp,
  1346. struct net_device *ol_dev,
  1347. struct netlink_ext_ack *extack)
  1348. {
  1349. const struct mlxsw_sp_ipip_ops *ipip_ops;
  1350. struct mlxsw_sp_ipip_entry *ipip_entry;
  1351. int err;
  1352. ipip_entry = mlxsw_sp_ipip_entry_find_by_ol_dev(mlxsw_sp, ol_dev);
  1353. if (!ipip_entry)
  1354. /* A change might make a tunnel eligible for offloading, but
  1355. * that is currently not implemented. What falls to slow path
  1356. * stays there.
  1357. */
  1358. return 0;
  1359. /* A change might make a tunnel not eligible for offloading. */
  1360. if (!mlxsw_sp_netdevice_ipip_can_offload(mlxsw_sp, ol_dev,
  1361. ipip_entry->ipipt)) {
  1362. mlxsw_sp_ipip_entry_demote_tunnel(mlxsw_sp, ipip_entry);
  1363. return 0;
  1364. }
  1365. ipip_ops = mlxsw_sp->router->ipip_ops_arr[ipip_entry->ipipt];
  1366. err = ipip_ops->ol_netdev_change(mlxsw_sp, ipip_entry, extack);
  1367. return err;
  1368. }
  1369. void mlxsw_sp_ipip_entry_demote_tunnel(struct mlxsw_sp *mlxsw_sp,
  1370. struct mlxsw_sp_ipip_entry *ipip_entry)
  1371. {
  1372. struct net_device *ol_dev = ipip_entry->ol_dev;
  1373. if (ol_dev->flags & IFF_UP)
  1374. mlxsw_sp_ipip_entry_ol_down_event(mlxsw_sp, ipip_entry);
  1375. mlxsw_sp_ipip_entry_destroy(mlxsw_sp, ipip_entry);
  1376. }
  1377. /* The configuration where several tunnels have the same local address in the
  1378. * same underlay table needs special treatment in the HW. That is currently not
  1379. * implemented in the driver. This function finds and demotes the first tunnel
  1380. * with a given source address, except the one passed in in the argument
  1381. * `except'.
  1382. */
  1383. bool
  1384. mlxsw_sp_ipip_demote_tunnel_by_saddr(struct mlxsw_sp *mlxsw_sp,
  1385. enum mlxsw_sp_l3proto ul_proto,
  1386. union mlxsw_sp_l3addr saddr,
  1387. u32 ul_tb_id,
  1388. const struct mlxsw_sp_ipip_entry *except)
  1389. {
  1390. struct mlxsw_sp_ipip_entry *ipip_entry, *tmp;
  1391. list_for_each_entry_safe(ipip_entry, tmp, &mlxsw_sp->router->ipip_list,
  1392. ipip_list_node) {
  1393. if (ipip_entry != except &&
  1394. mlxsw_sp_ipip_entry_saddr_matches(mlxsw_sp, ul_proto, saddr,
  1395. ul_tb_id, ipip_entry)) {
  1396. mlxsw_sp_ipip_entry_demote_tunnel(mlxsw_sp, ipip_entry);
  1397. return true;
  1398. }
  1399. }
  1400. return false;
  1401. }
  1402. static void mlxsw_sp_ipip_demote_tunnel_by_ul_netdev(struct mlxsw_sp *mlxsw_sp,
  1403. struct net_device *ul_dev)
  1404. {
  1405. struct mlxsw_sp_ipip_entry *ipip_entry, *tmp;
  1406. list_for_each_entry_safe(ipip_entry, tmp, &mlxsw_sp->router->ipip_list,
  1407. ipip_list_node) {
  1408. struct net_device *ipip_ul_dev =
  1409. __mlxsw_sp_ipip_netdev_ul_dev_get(ipip_entry->ol_dev);
  1410. if (ipip_ul_dev == ul_dev)
  1411. mlxsw_sp_ipip_entry_demote_tunnel(mlxsw_sp, ipip_entry);
  1412. }
  1413. }
  1414. int mlxsw_sp_netdevice_ipip_ol_event(struct mlxsw_sp *mlxsw_sp,
  1415. struct net_device *ol_dev,
  1416. unsigned long event,
  1417. struct netdev_notifier_info *info)
  1418. {
  1419. struct netdev_notifier_changeupper_info *chup;
  1420. struct netlink_ext_ack *extack;
  1421. switch (event) {
  1422. case NETDEV_REGISTER:
  1423. return mlxsw_sp_netdevice_ipip_ol_reg_event(mlxsw_sp, ol_dev);
  1424. case NETDEV_UNREGISTER:
  1425. mlxsw_sp_netdevice_ipip_ol_unreg_event(mlxsw_sp, ol_dev);
  1426. return 0;
  1427. case NETDEV_UP:
  1428. mlxsw_sp_netdevice_ipip_ol_up_event(mlxsw_sp, ol_dev);
  1429. return 0;
  1430. case NETDEV_DOWN:
  1431. mlxsw_sp_netdevice_ipip_ol_down_event(mlxsw_sp, ol_dev);
  1432. return 0;
  1433. case NETDEV_CHANGEUPPER:
  1434. chup = container_of(info, typeof(*chup), info);
  1435. extack = info->extack;
  1436. if (netif_is_l3_master(chup->upper_dev))
  1437. return mlxsw_sp_netdevice_ipip_ol_vrf_event(mlxsw_sp,
  1438. ol_dev,
  1439. extack);
  1440. return 0;
  1441. case NETDEV_CHANGE:
  1442. extack = info->extack;
  1443. return mlxsw_sp_netdevice_ipip_ol_change_event(mlxsw_sp,
  1444. ol_dev, extack);
  1445. case NETDEV_CHANGEMTU:
  1446. return mlxsw_sp_netdevice_ipip_ol_update_mtu(mlxsw_sp, ol_dev);
  1447. }
  1448. return 0;
  1449. }
  1450. static int
  1451. __mlxsw_sp_netdevice_ipip_ul_event(struct mlxsw_sp *mlxsw_sp,
  1452. struct mlxsw_sp_ipip_entry *ipip_entry,
  1453. struct net_device *ul_dev,
  1454. unsigned long event,
  1455. struct netdev_notifier_info *info)
  1456. {
  1457. struct netdev_notifier_changeupper_info *chup;
  1458. struct netlink_ext_ack *extack;
  1459. switch (event) {
  1460. case NETDEV_CHANGEUPPER:
  1461. chup = container_of(info, typeof(*chup), info);
  1462. extack = info->extack;
  1463. if (netif_is_l3_master(chup->upper_dev))
  1464. return mlxsw_sp_netdevice_ipip_ul_vrf_event(mlxsw_sp,
  1465. ipip_entry,
  1466. ul_dev,
  1467. extack);
  1468. break;
  1469. case NETDEV_UP:
  1470. return mlxsw_sp_netdevice_ipip_ul_up_event(mlxsw_sp, ipip_entry,
  1471. ul_dev);
  1472. case NETDEV_DOWN:
  1473. return mlxsw_sp_netdevice_ipip_ul_down_event(mlxsw_sp,
  1474. ipip_entry,
  1475. ul_dev);
  1476. }
  1477. return 0;
  1478. }
  1479. int
  1480. mlxsw_sp_netdevice_ipip_ul_event(struct mlxsw_sp *mlxsw_sp,
  1481. struct net_device *ul_dev,
  1482. unsigned long event,
  1483. struct netdev_notifier_info *info)
  1484. {
  1485. struct mlxsw_sp_ipip_entry *ipip_entry = NULL;
  1486. int err;
  1487. while ((ipip_entry = mlxsw_sp_ipip_entry_find_by_ul_dev(mlxsw_sp,
  1488. ul_dev,
  1489. ipip_entry))) {
  1490. err = __mlxsw_sp_netdevice_ipip_ul_event(mlxsw_sp, ipip_entry,
  1491. ul_dev, event, info);
  1492. if (err) {
  1493. mlxsw_sp_ipip_demote_tunnel_by_ul_netdev(mlxsw_sp,
  1494. ul_dev);
  1495. return err;
  1496. }
  1497. }
  1498. return 0;
  1499. }
  1500. struct mlxsw_sp_neigh_key {
  1501. struct neighbour *n;
  1502. };
  1503. struct mlxsw_sp_neigh_entry {
  1504. struct list_head rif_list_node;
  1505. struct rhash_head ht_node;
  1506. struct mlxsw_sp_neigh_key key;
  1507. u16 rif;
  1508. bool connected;
  1509. unsigned char ha[ETH_ALEN];
  1510. struct list_head nexthop_list; /* list of nexthops using
  1511. * this neigh entry
  1512. */
  1513. struct list_head nexthop_neighs_list_node;
  1514. unsigned int counter_index;
  1515. bool counter_valid;
  1516. };
  1517. static const struct rhashtable_params mlxsw_sp_neigh_ht_params = {
  1518. .key_offset = offsetof(struct mlxsw_sp_neigh_entry, key),
  1519. .head_offset = offsetof(struct mlxsw_sp_neigh_entry, ht_node),
  1520. .key_len = sizeof(struct mlxsw_sp_neigh_key),
  1521. };
  1522. struct mlxsw_sp_neigh_entry *
  1523. mlxsw_sp_rif_neigh_next(struct mlxsw_sp_rif *rif,
  1524. struct mlxsw_sp_neigh_entry *neigh_entry)
  1525. {
  1526. if (!neigh_entry) {
  1527. if (list_empty(&rif->neigh_list))
  1528. return NULL;
  1529. else
  1530. return list_first_entry(&rif->neigh_list,
  1531. typeof(*neigh_entry),
  1532. rif_list_node);
  1533. }
  1534. if (list_is_last(&neigh_entry->rif_list_node, &rif->neigh_list))
  1535. return NULL;
  1536. return list_next_entry(neigh_entry, rif_list_node);
  1537. }
  1538. int mlxsw_sp_neigh_entry_type(struct mlxsw_sp_neigh_entry *neigh_entry)
  1539. {
  1540. return neigh_entry->key.n->tbl->family;
  1541. }
  1542. unsigned char *
  1543. mlxsw_sp_neigh_entry_ha(struct mlxsw_sp_neigh_entry *neigh_entry)
  1544. {
  1545. return neigh_entry->ha;
  1546. }
  1547. u32 mlxsw_sp_neigh4_entry_dip(struct mlxsw_sp_neigh_entry *neigh_entry)
  1548. {
  1549. struct neighbour *n;
  1550. n = neigh_entry->key.n;
  1551. return ntohl(*((__be32 *) n->primary_key));
  1552. }
  1553. struct in6_addr *
  1554. mlxsw_sp_neigh6_entry_dip(struct mlxsw_sp_neigh_entry *neigh_entry)
  1555. {
  1556. struct neighbour *n;
  1557. n = neigh_entry->key.n;
  1558. return (struct in6_addr *) &n->primary_key;
  1559. }
  1560. int mlxsw_sp_neigh_counter_get(struct mlxsw_sp *mlxsw_sp,
  1561. struct mlxsw_sp_neigh_entry *neigh_entry,
  1562. u64 *p_counter)
  1563. {
  1564. if (!neigh_entry->counter_valid)
  1565. return -EINVAL;
  1566. return mlxsw_sp_flow_counter_get(mlxsw_sp, neigh_entry->counter_index,
  1567. p_counter, NULL);
  1568. }
  1569. static struct mlxsw_sp_neigh_entry *
  1570. mlxsw_sp_neigh_entry_alloc(struct mlxsw_sp *mlxsw_sp, struct neighbour *n,
  1571. u16 rif)
  1572. {
  1573. struct mlxsw_sp_neigh_entry *neigh_entry;
  1574. neigh_entry = kzalloc(sizeof(*neigh_entry), GFP_KERNEL);
  1575. if (!neigh_entry)
  1576. return NULL;
  1577. neigh_entry->key.n = n;
  1578. neigh_entry->rif = rif;
  1579. INIT_LIST_HEAD(&neigh_entry->nexthop_list);
  1580. return neigh_entry;
  1581. }
  1582. static void mlxsw_sp_neigh_entry_free(struct mlxsw_sp_neigh_entry *neigh_entry)
  1583. {
  1584. kfree(neigh_entry);
  1585. }
  1586. static int
  1587. mlxsw_sp_neigh_entry_insert(struct mlxsw_sp *mlxsw_sp,
  1588. struct mlxsw_sp_neigh_entry *neigh_entry)
  1589. {
  1590. return rhashtable_insert_fast(&mlxsw_sp->router->neigh_ht,
  1591. &neigh_entry->ht_node,
  1592. mlxsw_sp_neigh_ht_params);
  1593. }
  1594. static void
  1595. mlxsw_sp_neigh_entry_remove(struct mlxsw_sp *mlxsw_sp,
  1596. struct mlxsw_sp_neigh_entry *neigh_entry)
  1597. {
  1598. rhashtable_remove_fast(&mlxsw_sp->router->neigh_ht,
  1599. &neigh_entry->ht_node,
  1600. mlxsw_sp_neigh_ht_params);
  1601. }
  1602. static bool
  1603. mlxsw_sp_neigh_counter_should_alloc(struct mlxsw_sp *mlxsw_sp,
  1604. struct mlxsw_sp_neigh_entry *neigh_entry)
  1605. {
  1606. struct devlink *devlink;
  1607. const char *table_name;
  1608. switch (mlxsw_sp_neigh_entry_type(neigh_entry)) {
  1609. case AF_INET:
  1610. table_name = MLXSW_SP_DPIPE_TABLE_NAME_HOST4;
  1611. break;
  1612. case AF_INET6:
  1613. table_name = MLXSW_SP_DPIPE_TABLE_NAME_HOST6;
  1614. break;
  1615. default:
  1616. WARN_ON(1);
  1617. return false;
  1618. }
  1619. devlink = priv_to_devlink(mlxsw_sp->core);
  1620. return devlink_dpipe_table_counter_enabled(devlink, table_name);
  1621. }
  1622. static void
  1623. mlxsw_sp_neigh_counter_alloc(struct mlxsw_sp *mlxsw_sp,
  1624. struct mlxsw_sp_neigh_entry *neigh_entry)
  1625. {
  1626. if (!mlxsw_sp_neigh_counter_should_alloc(mlxsw_sp, neigh_entry))
  1627. return;
  1628. if (mlxsw_sp_flow_counter_alloc(mlxsw_sp, &neigh_entry->counter_index))
  1629. return;
  1630. neigh_entry->counter_valid = true;
  1631. }
  1632. static void
  1633. mlxsw_sp_neigh_counter_free(struct mlxsw_sp *mlxsw_sp,
  1634. struct mlxsw_sp_neigh_entry *neigh_entry)
  1635. {
  1636. if (!neigh_entry->counter_valid)
  1637. return;
  1638. mlxsw_sp_flow_counter_free(mlxsw_sp,
  1639. neigh_entry->counter_index);
  1640. neigh_entry->counter_valid = false;
  1641. }
  1642. static struct mlxsw_sp_neigh_entry *
  1643. mlxsw_sp_neigh_entry_create(struct mlxsw_sp *mlxsw_sp, struct neighbour *n)
  1644. {
  1645. struct mlxsw_sp_neigh_entry *neigh_entry;
  1646. struct mlxsw_sp_rif *rif;
  1647. int err;
  1648. rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, n->dev);
  1649. if (!rif)
  1650. return ERR_PTR(-EINVAL);
  1651. neigh_entry = mlxsw_sp_neigh_entry_alloc(mlxsw_sp, n, rif->rif_index);
  1652. if (!neigh_entry)
  1653. return ERR_PTR(-ENOMEM);
  1654. err = mlxsw_sp_neigh_entry_insert(mlxsw_sp, neigh_entry);
  1655. if (err)
  1656. goto err_neigh_entry_insert;
  1657. mlxsw_sp_neigh_counter_alloc(mlxsw_sp, neigh_entry);
  1658. list_add(&neigh_entry->rif_list_node, &rif->neigh_list);
  1659. return neigh_entry;
  1660. err_neigh_entry_insert:
  1661. mlxsw_sp_neigh_entry_free(neigh_entry);
  1662. return ERR_PTR(err);
  1663. }
  1664. static void
  1665. mlxsw_sp_neigh_entry_destroy(struct mlxsw_sp *mlxsw_sp,
  1666. struct mlxsw_sp_neigh_entry *neigh_entry)
  1667. {
  1668. list_del(&neigh_entry->rif_list_node);
  1669. mlxsw_sp_neigh_counter_free(mlxsw_sp, neigh_entry);
  1670. mlxsw_sp_neigh_entry_remove(mlxsw_sp, neigh_entry);
  1671. mlxsw_sp_neigh_entry_free(neigh_entry);
  1672. }
  1673. static struct mlxsw_sp_neigh_entry *
  1674. mlxsw_sp_neigh_entry_lookup(struct mlxsw_sp *mlxsw_sp, struct neighbour *n)
  1675. {
  1676. struct mlxsw_sp_neigh_key key;
  1677. key.n = n;
  1678. return rhashtable_lookup_fast(&mlxsw_sp->router->neigh_ht,
  1679. &key, mlxsw_sp_neigh_ht_params);
  1680. }
  1681. static void
  1682. mlxsw_sp_router_neighs_update_interval_init(struct mlxsw_sp *mlxsw_sp)
  1683. {
  1684. unsigned long interval;
  1685. #if IS_ENABLED(CONFIG_IPV6)
  1686. interval = min_t(unsigned long,
  1687. NEIGH_VAR(&arp_tbl.parms, DELAY_PROBE_TIME),
  1688. NEIGH_VAR(&nd_tbl.parms, DELAY_PROBE_TIME));
  1689. #else
  1690. interval = NEIGH_VAR(&arp_tbl.parms, DELAY_PROBE_TIME);
  1691. #endif
  1692. mlxsw_sp->router->neighs_update.interval = jiffies_to_msecs(interval);
  1693. }
  1694. static void mlxsw_sp_router_neigh_ent_ipv4_process(struct mlxsw_sp *mlxsw_sp,
  1695. char *rauhtd_pl,
  1696. int ent_index)
  1697. {
  1698. struct net_device *dev;
  1699. struct neighbour *n;
  1700. __be32 dipn;
  1701. u32 dip;
  1702. u16 rif;
  1703. mlxsw_reg_rauhtd_ent_ipv4_unpack(rauhtd_pl, ent_index, &rif, &dip);
  1704. if (!mlxsw_sp->router->rifs[rif]) {
  1705. dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Incorrect RIF in neighbour entry\n");
  1706. return;
  1707. }
  1708. dipn = htonl(dip);
  1709. dev = mlxsw_sp->router->rifs[rif]->dev;
  1710. n = neigh_lookup(&arp_tbl, &dipn, dev);
  1711. if (!n)
  1712. return;
  1713. netdev_dbg(dev, "Updating neighbour with IP=%pI4h\n", &dip);
  1714. neigh_event_send(n, NULL);
  1715. neigh_release(n);
  1716. }
  1717. #if IS_ENABLED(CONFIG_IPV6)
  1718. static void mlxsw_sp_router_neigh_ent_ipv6_process(struct mlxsw_sp *mlxsw_sp,
  1719. char *rauhtd_pl,
  1720. int rec_index)
  1721. {
  1722. struct net_device *dev;
  1723. struct neighbour *n;
  1724. struct in6_addr dip;
  1725. u16 rif;
  1726. mlxsw_reg_rauhtd_ent_ipv6_unpack(rauhtd_pl, rec_index, &rif,
  1727. (char *) &dip);
  1728. if (!mlxsw_sp->router->rifs[rif]) {
  1729. dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Incorrect RIF in neighbour entry\n");
  1730. return;
  1731. }
  1732. dev = mlxsw_sp->router->rifs[rif]->dev;
  1733. n = neigh_lookup(&nd_tbl, &dip, dev);
  1734. if (!n)
  1735. return;
  1736. netdev_dbg(dev, "Updating neighbour with IP=%pI6c\n", &dip);
  1737. neigh_event_send(n, NULL);
  1738. neigh_release(n);
  1739. }
  1740. #else
  1741. static void mlxsw_sp_router_neigh_ent_ipv6_process(struct mlxsw_sp *mlxsw_sp,
  1742. char *rauhtd_pl,
  1743. int rec_index)
  1744. {
  1745. }
  1746. #endif
  1747. static void mlxsw_sp_router_neigh_rec_ipv4_process(struct mlxsw_sp *mlxsw_sp,
  1748. char *rauhtd_pl,
  1749. int rec_index)
  1750. {
  1751. u8 num_entries;
  1752. int i;
  1753. num_entries = mlxsw_reg_rauhtd_ipv4_rec_num_entries_get(rauhtd_pl,
  1754. rec_index);
  1755. /* Hardware starts counting at 0, so add 1. */
  1756. num_entries++;
  1757. /* Each record consists of several neighbour entries. */
  1758. for (i = 0; i < num_entries; i++) {
  1759. int ent_index;
  1760. ent_index = rec_index * MLXSW_REG_RAUHTD_IPV4_ENT_PER_REC + i;
  1761. mlxsw_sp_router_neigh_ent_ipv4_process(mlxsw_sp, rauhtd_pl,
  1762. ent_index);
  1763. }
  1764. }
  1765. static void mlxsw_sp_router_neigh_rec_ipv6_process(struct mlxsw_sp *mlxsw_sp,
  1766. char *rauhtd_pl,
  1767. int rec_index)
  1768. {
  1769. /* One record contains one entry. */
  1770. mlxsw_sp_router_neigh_ent_ipv6_process(mlxsw_sp, rauhtd_pl,
  1771. rec_index);
  1772. }
  1773. static void mlxsw_sp_router_neigh_rec_process(struct mlxsw_sp *mlxsw_sp,
  1774. char *rauhtd_pl, int rec_index)
  1775. {
  1776. switch (mlxsw_reg_rauhtd_rec_type_get(rauhtd_pl, rec_index)) {
  1777. case MLXSW_REG_RAUHTD_TYPE_IPV4:
  1778. mlxsw_sp_router_neigh_rec_ipv4_process(mlxsw_sp, rauhtd_pl,
  1779. rec_index);
  1780. break;
  1781. case MLXSW_REG_RAUHTD_TYPE_IPV6:
  1782. mlxsw_sp_router_neigh_rec_ipv6_process(mlxsw_sp, rauhtd_pl,
  1783. rec_index);
  1784. break;
  1785. }
  1786. }
  1787. static bool mlxsw_sp_router_rauhtd_is_full(char *rauhtd_pl)
  1788. {
  1789. u8 num_rec, last_rec_index, num_entries;
  1790. num_rec = mlxsw_reg_rauhtd_num_rec_get(rauhtd_pl);
  1791. last_rec_index = num_rec - 1;
  1792. if (num_rec < MLXSW_REG_RAUHTD_REC_MAX_NUM)
  1793. return false;
  1794. if (mlxsw_reg_rauhtd_rec_type_get(rauhtd_pl, last_rec_index) ==
  1795. MLXSW_REG_RAUHTD_TYPE_IPV6)
  1796. return true;
  1797. num_entries = mlxsw_reg_rauhtd_ipv4_rec_num_entries_get(rauhtd_pl,
  1798. last_rec_index);
  1799. if (++num_entries == MLXSW_REG_RAUHTD_IPV4_ENT_PER_REC)
  1800. return true;
  1801. return false;
  1802. }
  1803. static int
  1804. __mlxsw_sp_router_neighs_update_rauhtd(struct mlxsw_sp *mlxsw_sp,
  1805. char *rauhtd_pl,
  1806. enum mlxsw_reg_rauhtd_type type)
  1807. {
  1808. int i, num_rec;
  1809. int err;
  1810. /* Make sure the neighbour's netdev isn't removed in the
  1811. * process.
  1812. */
  1813. rtnl_lock();
  1814. do {
  1815. mlxsw_reg_rauhtd_pack(rauhtd_pl, type);
  1816. err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(rauhtd),
  1817. rauhtd_pl);
  1818. if (err) {
  1819. dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to dump neighbour table\n");
  1820. break;
  1821. }
  1822. num_rec = mlxsw_reg_rauhtd_num_rec_get(rauhtd_pl);
  1823. for (i = 0; i < num_rec; i++)
  1824. mlxsw_sp_router_neigh_rec_process(mlxsw_sp, rauhtd_pl,
  1825. i);
  1826. } while (mlxsw_sp_router_rauhtd_is_full(rauhtd_pl));
  1827. rtnl_unlock();
  1828. return err;
  1829. }
  1830. static int mlxsw_sp_router_neighs_update_rauhtd(struct mlxsw_sp *mlxsw_sp)
  1831. {
  1832. enum mlxsw_reg_rauhtd_type type;
  1833. char *rauhtd_pl;
  1834. int err;
  1835. rauhtd_pl = kmalloc(MLXSW_REG_RAUHTD_LEN, GFP_KERNEL);
  1836. if (!rauhtd_pl)
  1837. return -ENOMEM;
  1838. type = MLXSW_REG_RAUHTD_TYPE_IPV4;
  1839. err = __mlxsw_sp_router_neighs_update_rauhtd(mlxsw_sp, rauhtd_pl, type);
  1840. if (err)
  1841. goto out;
  1842. type = MLXSW_REG_RAUHTD_TYPE_IPV6;
  1843. err = __mlxsw_sp_router_neighs_update_rauhtd(mlxsw_sp, rauhtd_pl, type);
  1844. out:
  1845. kfree(rauhtd_pl);
  1846. return err;
  1847. }
  1848. static void mlxsw_sp_router_neighs_update_nh(struct mlxsw_sp *mlxsw_sp)
  1849. {
  1850. struct mlxsw_sp_neigh_entry *neigh_entry;
  1851. /* Take RTNL mutex here to prevent lists from changes */
  1852. rtnl_lock();
  1853. list_for_each_entry(neigh_entry, &mlxsw_sp->router->nexthop_neighs_list,
  1854. nexthop_neighs_list_node)
  1855. /* If this neigh have nexthops, make the kernel think this neigh
  1856. * is active regardless of the traffic.
  1857. */
  1858. neigh_event_send(neigh_entry->key.n, NULL);
  1859. rtnl_unlock();
  1860. }
  1861. static void
  1862. mlxsw_sp_router_neighs_update_work_schedule(struct mlxsw_sp *mlxsw_sp)
  1863. {
  1864. unsigned long interval = mlxsw_sp->router->neighs_update.interval;
  1865. mlxsw_core_schedule_dw(&mlxsw_sp->router->neighs_update.dw,
  1866. msecs_to_jiffies(interval));
  1867. }
  1868. static void mlxsw_sp_router_neighs_update_work(struct work_struct *work)
  1869. {
  1870. struct mlxsw_sp_router *router;
  1871. int err;
  1872. router = container_of(work, struct mlxsw_sp_router,
  1873. neighs_update.dw.work);
  1874. err = mlxsw_sp_router_neighs_update_rauhtd(router->mlxsw_sp);
  1875. if (err)
  1876. dev_err(router->mlxsw_sp->bus_info->dev, "Could not update kernel for neigh activity");
  1877. mlxsw_sp_router_neighs_update_nh(router->mlxsw_sp);
  1878. mlxsw_sp_router_neighs_update_work_schedule(router->mlxsw_sp);
  1879. }
  1880. static void mlxsw_sp_router_probe_unresolved_nexthops(struct work_struct *work)
  1881. {
  1882. struct mlxsw_sp_neigh_entry *neigh_entry;
  1883. struct mlxsw_sp_router *router;
  1884. router = container_of(work, struct mlxsw_sp_router,
  1885. nexthop_probe_dw.work);
  1886. /* Iterate over nexthop neighbours, find those who are unresolved and
  1887. * send arp on them. This solves the chicken-egg problem when
  1888. * the nexthop wouldn't get offloaded until the neighbor is resolved
  1889. * but it wouldn't get resolved ever in case traffic is flowing in HW
  1890. * using different nexthop.
  1891. *
  1892. * Take RTNL mutex here to prevent lists from changes.
  1893. */
  1894. rtnl_lock();
  1895. list_for_each_entry(neigh_entry, &router->nexthop_neighs_list,
  1896. nexthop_neighs_list_node)
  1897. if (!neigh_entry->connected)
  1898. neigh_event_send(neigh_entry->key.n, NULL);
  1899. rtnl_unlock();
  1900. mlxsw_core_schedule_dw(&router->nexthop_probe_dw,
  1901. MLXSW_SP_UNRESOLVED_NH_PROBE_INTERVAL);
  1902. }
  1903. static void
  1904. mlxsw_sp_nexthop_neigh_update(struct mlxsw_sp *mlxsw_sp,
  1905. struct mlxsw_sp_neigh_entry *neigh_entry,
  1906. bool removing, bool dead);
  1907. static enum mlxsw_reg_rauht_op mlxsw_sp_rauht_op(bool adding)
  1908. {
  1909. return adding ? MLXSW_REG_RAUHT_OP_WRITE_ADD :
  1910. MLXSW_REG_RAUHT_OP_WRITE_DELETE;
  1911. }
  1912. static void
  1913. mlxsw_sp_router_neigh_entry_op4(struct mlxsw_sp *mlxsw_sp,
  1914. struct mlxsw_sp_neigh_entry *neigh_entry,
  1915. enum mlxsw_reg_rauht_op op)
  1916. {
  1917. struct neighbour *n = neigh_entry->key.n;
  1918. u32 dip = ntohl(*((__be32 *) n->primary_key));
  1919. char rauht_pl[MLXSW_REG_RAUHT_LEN];
  1920. mlxsw_reg_rauht_pack4(rauht_pl, op, neigh_entry->rif, neigh_entry->ha,
  1921. dip);
  1922. if (neigh_entry->counter_valid)
  1923. mlxsw_reg_rauht_pack_counter(rauht_pl,
  1924. neigh_entry->counter_index);
  1925. mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rauht), rauht_pl);
  1926. }
  1927. static void
  1928. mlxsw_sp_router_neigh_entry_op6(struct mlxsw_sp *mlxsw_sp,
  1929. struct mlxsw_sp_neigh_entry *neigh_entry,
  1930. enum mlxsw_reg_rauht_op op)
  1931. {
  1932. struct neighbour *n = neigh_entry->key.n;
  1933. char rauht_pl[MLXSW_REG_RAUHT_LEN];
  1934. const char *dip = n->primary_key;
  1935. mlxsw_reg_rauht_pack6(rauht_pl, op, neigh_entry->rif, neigh_entry->ha,
  1936. dip);
  1937. if (neigh_entry->counter_valid)
  1938. mlxsw_reg_rauht_pack_counter(rauht_pl,
  1939. neigh_entry->counter_index);
  1940. mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rauht), rauht_pl);
  1941. }
  1942. bool mlxsw_sp_neigh_ipv6_ignore(struct mlxsw_sp_neigh_entry *neigh_entry)
  1943. {
  1944. struct neighbour *n = neigh_entry->key.n;
  1945. /* Packets with a link-local destination address are trapped
  1946. * after LPM lookup and never reach the neighbour table, so
  1947. * there is no need to program such neighbours to the device.
  1948. */
  1949. if (ipv6_addr_type((struct in6_addr *) &n->primary_key) &
  1950. IPV6_ADDR_LINKLOCAL)
  1951. return true;
  1952. return false;
  1953. }
  1954. static void
  1955. mlxsw_sp_neigh_entry_update(struct mlxsw_sp *mlxsw_sp,
  1956. struct mlxsw_sp_neigh_entry *neigh_entry,
  1957. bool adding)
  1958. {
  1959. if (!adding && !neigh_entry->connected)
  1960. return;
  1961. neigh_entry->connected = adding;
  1962. if (neigh_entry->key.n->tbl->family == AF_INET) {
  1963. mlxsw_sp_router_neigh_entry_op4(mlxsw_sp, neigh_entry,
  1964. mlxsw_sp_rauht_op(adding));
  1965. } else if (neigh_entry->key.n->tbl->family == AF_INET6) {
  1966. if (mlxsw_sp_neigh_ipv6_ignore(neigh_entry))
  1967. return;
  1968. mlxsw_sp_router_neigh_entry_op6(mlxsw_sp, neigh_entry,
  1969. mlxsw_sp_rauht_op(adding));
  1970. } else {
  1971. WARN_ON_ONCE(1);
  1972. }
  1973. }
  1974. void
  1975. mlxsw_sp_neigh_entry_counter_update(struct mlxsw_sp *mlxsw_sp,
  1976. struct mlxsw_sp_neigh_entry *neigh_entry,
  1977. bool adding)
  1978. {
  1979. if (adding)
  1980. mlxsw_sp_neigh_counter_alloc(mlxsw_sp, neigh_entry);
  1981. else
  1982. mlxsw_sp_neigh_counter_free(mlxsw_sp, neigh_entry);
  1983. mlxsw_sp_neigh_entry_update(mlxsw_sp, neigh_entry, true);
  1984. }
  1985. struct mlxsw_sp_netevent_work {
  1986. struct work_struct work;
  1987. struct mlxsw_sp *mlxsw_sp;
  1988. struct neighbour *n;
  1989. };
  1990. static void mlxsw_sp_router_neigh_event_work(struct work_struct *work)
  1991. {
  1992. struct mlxsw_sp_netevent_work *net_work =
  1993. container_of(work, struct mlxsw_sp_netevent_work, work);
  1994. struct mlxsw_sp *mlxsw_sp = net_work->mlxsw_sp;
  1995. struct mlxsw_sp_neigh_entry *neigh_entry;
  1996. struct neighbour *n = net_work->n;
  1997. unsigned char ha[ETH_ALEN];
  1998. bool entry_connected;
  1999. u8 nud_state, dead;
  2000. /* If these parameters are changed after we release the lock,
  2001. * then we are guaranteed to receive another event letting us
  2002. * know about it.
  2003. */
  2004. read_lock_bh(&n->lock);
  2005. memcpy(ha, n->ha, ETH_ALEN);
  2006. nud_state = n->nud_state;
  2007. dead = n->dead;
  2008. read_unlock_bh(&n->lock);
  2009. rtnl_lock();
  2010. mlxsw_sp_span_respin(mlxsw_sp);
  2011. entry_connected = nud_state & NUD_VALID && !dead;
  2012. neigh_entry = mlxsw_sp_neigh_entry_lookup(mlxsw_sp, n);
  2013. if (!entry_connected && !neigh_entry)
  2014. goto out;
  2015. if (!neigh_entry) {
  2016. neigh_entry = mlxsw_sp_neigh_entry_create(mlxsw_sp, n);
  2017. if (IS_ERR(neigh_entry))
  2018. goto out;
  2019. }
  2020. memcpy(neigh_entry->ha, ha, ETH_ALEN);
  2021. mlxsw_sp_neigh_entry_update(mlxsw_sp, neigh_entry, entry_connected);
  2022. mlxsw_sp_nexthop_neigh_update(mlxsw_sp, neigh_entry, !entry_connected,
  2023. dead);
  2024. if (!neigh_entry->connected && list_empty(&neigh_entry->nexthop_list))
  2025. mlxsw_sp_neigh_entry_destroy(mlxsw_sp, neigh_entry);
  2026. out:
  2027. rtnl_unlock();
  2028. neigh_release(n);
  2029. kfree(net_work);
  2030. }
  2031. static int mlxsw_sp_mp_hash_init(struct mlxsw_sp *mlxsw_sp);
  2032. static void mlxsw_sp_router_mp_hash_event_work(struct work_struct *work)
  2033. {
  2034. struct mlxsw_sp_netevent_work *net_work =
  2035. container_of(work, struct mlxsw_sp_netevent_work, work);
  2036. struct mlxsw_sp *mlxsw_sp = net_work->mlxsw_sp;
  2037. mlxsw_sp_mp_hash_init(mlxsw_sp);
  2038. kfree(net_work);
  2039. }
  2040. static int __mlxsw_sp_router_init(struct mlxsw_sp *mlxsw_sp);
  2041. static void mlxsw_sp_router_update_priority_work(struct work_struct *work)
  2042. {
  2043. struct mlxsw_sp_netevent_work *net_work =
  2044. container_of(work, struct mlxsw_sp_netevent_work, work);
  2045. struct mlxsw_sp *mlxsw_sp = net_work->mlxsw_sp;
  2046. __mlxsw_sp_router_init(mlxsw_sp);
  2047. kfree(net_work);
  2048. }
  2049. static int mlxsw_sp_router_schedule_work(struct net *net,
  2050. struct notifier_block *nb,
  2051. void (*cb)(struct work_struct *))
  2052. {
  2053. struct mlxsw_sp_netevent_work *net_work;
  2054. struct mlxsw_sp_router *router;
  2055. if (!net_eq(net, &init_net))
  2056. return NOTIFY_DONE;
  2057. net_work = kzalloc(sizeof(*net_work), GFP_ATOMIC);
  2058. if (!net_work)
  2059. return NOTIFY_BAD;
  2060. router = container_of(nb, struct mlxsw_sp_router, netevent_nb);
  2061. INIT_WORK(&net_work->work, cb);
  2062. net_work->mlxsw_sp = router->mlxsw_sp;
  2063. mlxsw_core_schedule_work(&net_work->work);
  2064. return NOTIFY_DONE;
  2065. }
  2066. static int mlxsw_sp_router_netevent_event(struct notifier_block *nb,
  2067. unsigned long event, void *ptr)
  2068. {
  2069. struct mlxsw_sp_netevent_work *net_work;
  2070. struct mlxsw_sp_port *mlxsw_sp_port;
  2071. struct mlxsw_sp *mlxsw_sp;
  2072. unsigned long interval;
  2073. struct neigh_parms *p;
  2074. struct neighbour *n;
  2075. switch (event) {
  2076. case NETEVENT_DELAY_PROBE_TIME_UPDATE:
  2077. p = ptr;
  2078. /* We don't care about changes in the default table. */
  2079. if (!p->dev || (p->tbl->family != AF_INET &&
  2080. p->tbl->family != AF_INET6))
  2081. return NOTIFY_DONE;
  2082. /* We are in atomic context and can't take RTNL mutex,
  2083. * so use RCU variant to walk the device chain.
  2084. */
  2085. mlxsw_sp_port = mlxsw_sp_port_lower_dev_hold(p->dev);
  2086. if (!mlxsw_sp_port)
  2087. return NOTIFY_DONE;
  2088. mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
  2089. interval = jiffies_to_msecs(NEIGH_VAR(p, DELAY_PROBE_TIME));
  2090. mlxsw_sp->router->neighs_update.interval = interval;
  2091. mlxsw_sp_port_dev_put(mlxsw_sp_port);
  2092. break;
  2093. case NETEVENT_NEIGH_UPDATE:
  2094. n = ptr;
  2095. if (n->tbl->family != AF_INET && n->tbl->family != AF_INET6)
  2096. return NOTIFY_DONE;
  2097. mlxsw_sp_port = mlxsw_sp_port_lower_dev_hold(n->dev);
  2098. if (!mlxsw_sp_port)
  2099. return NOTIFY_DONE;
  2100. net_work = kzalloc(sizeof(*net_work), GFP_ATOMIC);
  2101. if (!net_work) {
  2102. mlxsw_sp_port_dev_put(mlxsw_sp_port);
  2103. return NOTIFY_BAD;
  2104. }
  2105. INIT_WORK(&net_work->work, mlxsw_sp_router_neigh_event_work);
  2106. net_work->mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
  2107. net_work->n = n;
  2108. /* Take a reference to ensure the neighbour won't be
  2109. * destructed until we drop the reference in delayed
  2110. * work.
  2111. */
  2112. neigh_clone(n);
  2113. mlxsw_core_schedule_work(&net_work->work);
  2114. mlxsw_sp_port_dev_put(mlxsw_sp_port);
  2115. break;
  2116. case NETEVENT_IPV4_MPATH_HASH_UPDATE:
  2117. case NETEVENT_IPV6_MPATH_HASH_UPDATE:
  2118. return mlxsw_sp_router_schedule_work(ptr, nb,
  2119. mlxsw_sp_router_mp_hash_event_work);
  2120. case NETEVENT_IPV4_FWD_UPDATE_PRIORITY_UPDATE:
  2121. return mlxsw_sp_router_schedule_work(ptr, nb,
  2122. mlxsw_sp_router_update_priority_work);
  2123. }
  2124. return NOTIFY_DONE;
  2125. }
  2126. static int mlxsw_sp_neigh_init(struct mlxsw_sp *mlxsw_sp)
  2127. {
  2128. int err;
  2129. err = rhashtable_init(&mlxsw_sp->router->neigh_ht,
  2130. &mlxsw_sp_neigh_ht_params);
  2131. if (err)
  2132. return err;
  2133. /* Initialize the polling interval according to the default
  2134. * table.
  2135. */
  2136. mlxsw_sp_router_neighs_update_interval_init(mlxsw_sp);
  2137. /* Create the delayed works for the activity_update */
  2138. INIT_DELAYED_WORK(&mlxsw_sp->router->neighs_update.dw,
  2139. mlxsw_sp_router_neighs_update_work);
  2140. INIT_DELAYED_WORK(&mlxsw_sp->router->nexthop_probe_dw,
  2141. mlxsw_sp_router_probe_unresolved_nexthops);
  2142. mlxsw_core_schedule_dw(&mlxsw_sp->router->neighs_update.dw, 0);
  2143. mlxsw_core_schedule_dw(&mlxsw_sp->router->nexthop_probe_dw, 0);
  2144. return 0;
  2145. }
  2146. static void mlxsw_sp_neigh_fini(struct mlxsw_sp *mlxsw_sp)
  2147. {
  2148. cancel_delayed_work_sync(&mlxsw_sp->router->neighs_update.dw);
  2149. cancel_delayed_work_sync(&mlxsw_sp->router->nexthop_probe_dw);
  2150. rhashtable_destroy(&mlxsw_sp->router->neigh_ht);
  2151. }
  2152. static void mlxsw_sp_neigh_rif_gone_sync(struct mlxsw_sp *mlxsw_sp,
  2153. struct mlxsw_sp_rif *rif)
  2154. {
  2155. struct mlxsw_sp_neigh_entry *neigh_entry, *tmp;
  2156. list_for_each_entry_safe(neigh_entry, tmp, &rif->neigh_list,
  2157. rif_list_node) {
  2158. mlxsw_sp_neigh_entry_update(mlxsw_sp, neigh_entry, false);
  2159. mlxsw_sp_neigh_entry_destroy(mlxsw_sp, neigh_entry);
  2160. }
  2161. }
  2162. enum mlxsw_sp_nexthop_type {
  2163. MLXSW_SP_NEXTHOP_TYPE_ETH,
  2164. MLXSW_SP_NEXTHOP_TYPE_IPIP,
  2165. };
  2166. struct mlxsw_sp_nexthop_key {
  2167. struct fib_nh *fib_nh;
  2168. };
  2169. struct mlxsw_sp_nexthop {
  2170. struct list_head neigh_list_node; /* member of neigh entry list */
  2171. struct list_head rif_list_node;
  2172. struct list_head router_list_node;
  2173. struct mlxsw_sp_nexthop_group *nh_grp; /* pointer back to the group
  2174. * this belongs to
  2175. */
  2176. struct rhash_head ht_node;
  2177. struct mlxsw_sp_nexthop_key key;
  2178. unsigned char gw_addr[sizeof(struct in6_addr)];
  2179. int ifindex;
  2180. int nh_weight;
  2181. int norm_nh_weight;
  2182. int num_adj_entries;
  2183. struct mlxsw_sp_rif *rif;
  2184. u8 should_offload:1, /* set indicates this neigh is connected and
  2185. * should be put to KVD linear area of this group.
  2186. */
  2187. offloaded:1, /* set in case the neigh is actually put into
  2188. * KVD linear area of this group.
  2189. */
  2190. update:1; /* set indicates that MAC of this neigh should be
  2191. * updated in HW
  2192. */
  2193. enum mlxsw_sp_nexthop_type type;
  2194. union {
  2195. struct mlxsw_sp_neigh_entry *neigh_entry;
  2196. struct mlxsw_sp_ipip_entry *ipip_entry;
  2197. };
  2198. unsigned int counter_index;
  2199. bool counter_valid;
  2200. };
  2201. struct mlxsw_sp_nexthop_group {
  2202. void *priv;
  2203. struct rhash_head ht_node;
  2204. struct list_head fib_list; /* list of fib entries that use this group */
  2205. struct neigh_table *neigh_tbl;
  2206. u8 adj_index_valid:1,
  2207. gateway:1; /* routes using the group use a gateway */
  2208. u32 adj_index;
  2209. u16 ecmp_size;
  2210. u16 count;
  2211. int sum_norm_weight;
  2212. struct mlxsw_sp_nexthop nexthops[0];
  2213. #define nh_rif nexthops[0].rif
  2214. };
  2215. void mlxsw_sp_nexthop_counter_alloc(struct mlxsw_sp *mlxsw_sp,
  2216. struct mlxsw_sp_nexthop *nh)
  2217. {
  2218. struct devlink *devlink;
  2219. devlink = priv_to_devlink(mlxsw_sp->core);
  2220. if (!devlink_dpipe_table_counter_enabled(devlink,
  2221. MLXSW_SP_DPIPE_TABLE_NAME_ADJ))
  2222. return;
  2223. if (mlxsw_sp_flow_counter_alloc(mlxsw_sp, &nh->counter_index))
  2224. return;
  2225. nh->counter_valid = true;
  2226. }
  2227. void mlxsw_sp_nexthop_counter_free(struct mlxsw_sp *mlxsw_sp,
  2228. struct mlxsw_sp_nexthop *nh)
  2229. {
  2230. if (!nh->counter_valid)
  2231. return;
  2232. mlxsw_sp_flow_counter_free(mlxsw_sp, nh->counter_index);
  2233. nh->counter_valid = false;
  2234. }
  2235. int mlxsw_sp_nexthop_counter_get(struct mlxsw_sp *mlxsw_sp,
  2236. struct mlxsw_sp_nexthop *nh, u64 *p_counter)
  2237. {
  2238. if (!nh->counter_valid)
  2239. return -EINVAL;
  2240. return mlxsw_sp_flow_counter_get(mlxsw_sp, nh->counter_index,
  2241. p_counter, NULL);
  2242. }
  2243. struct mlxsw_sp_nexthop *mlxsw_sp_nexthop_next(struct mlxsw_sp_router *router,
  2244. struct mlxsw_sp_nexthop *nh)
  2245. {
  2246. if (!nh) {
  2247. if (list_empty(&router->nexthop_list))
  2248. return NULL;
  2249. else
  2250. return list_first_entry(&router->nexthop_list,
  2251. typeof(*nh), router_list_node);
  2252. }
  2253. if (list_is_last(&nh->router_list_node, &router->nexthop_list))
  2254. return NULL;
  2255. return list_next_entry(nh, router_list_node);
  2256. }
  2257. bool mlxsw_sp_nexthop_offload(struct mlxsw_sp_nexthop *nh)
  2258. {
  2259. return nh->offloaded;
  2260. }
  2261. unsigned char *mlxsw_sp_nexthop_ha(struct mlxsw_sp_nexthop *nh)
  2262. {
  2263. if (!nh->offloaded)
  2264. return NULL;
  2265. return nh->neigh_entry->ha;
  2266. }
  2267. int mlxsw_sp_nexthop_indexes(struct mlxsw_sp_nexthop *nh, u32 *p_adj_index,
  2268. u32 *p_adj_size, u32 *p_adj_hash_index)
  2269. {
  2270. struct mlxsw_sp_nexthop_group *nh_grp = nh->nh_grp;
  2271. u32 adj_hash_index = 0;
  2272. int i;
  2273. if (!nh->offloaded || !nh_grp->adj_index_valid)
  2274. return -EINVAL;
  2275. *p_adj_index = nh_grp->adj_index;
  2276. *p_adj_size = nh_grp->ecmp_size;
  2277. for (i = 0; i < nh_grp->count; i++) {
  2278. struct mlxsw_sp_nexthop *nh_iter = &nh_grp->nexthops[i];
  2279. if (nh_iter == nh)
  2280. break;
  2281. if (nh_iter->offloaded)
  2282. adj_hash_index += nh_iter->num_adj_entries;
  2283. }
  2284. *p_adj_hash_index = adj_hash_index;
  2285. return 0;
  2286. }
  2287. struct mlxsw_sp_rif *mlxsw_sp_nexthop_rif(struct mlxsw_sp_nexthop *nh)
  2288. {
  2289. return nh->rif;
  2290. }
  2291. bool mlxsw_sp_nexthop_group_has_ipip(struct mlxsw_sp_nexthop *nh)
  2292. {
  2293. struct mlxsw_sp_nexthop_group *nh_grp = nh->nh_grp;
  2294. int i;
  2295. for (i = 0; i < nh_grp->count; i++) {
  2296. struct mlxsw_sp_nexthop *nh_iter = &nh_grp->nexthops[i];
  2297. if (nh_iter->type == MLXSW_SP_NEXTHOP_TYPE_IPIP)
  2298. return true;
  2299. }
  2300. return false;
  2301. }
  2302. static struct fib_info *
  2303. mlxsw_sp_nexthop4_group_fi(const struct mlxsw_sp_nexthop_group *nh_grp)
  2304. {
  2305. return nh_grp->priv;
  2306. }
  2307. struct mlxsw_sp_nexthop_group_cmp_arg {
  2308. enum mlxsw_sp_l3proto proto;
  2309. union {
  2310. struct fib_info *fi;
  2311. struct mlxsw_sp_fib6_entry *fib6_entry;
  2312. };
  2313. };
  2314. static bool
  2315. mlxsw_sp_nexthop6_group_has_nexthop(const struct mlxsw_sp_nexthop_group *nh_grp,
  2316. const struct in6_addr *gw, int ifindex,
  2317. int weight)
  2318. {
  2319. int i;
  2320. for (i = 0; i < nh_grp->count; i++) {
  2321. const struct mlxsw_sp_nexthop *nh;
  2322. nh = &nh_grp->nexthops[i];
  2323. if (nh->ifindex == ifindex && nh->nh_weight == weight &&
  2324. ipv6_addr_equal(gw, (struct in6_addr *) nh->gw_addr))
  2325. return true;
  2326. }
  2327. return false;
  2328. }
  2329. static bool
  2330. mlxsw_sp_nexthop6_group_cmp(const struct mlxsw_sp_nexthop_group *nh_grp,
  2331. const struct mlxsw_sp_fib6_entry *fib6_entry)
  2332. {
  2333. struct mlxsw_sp_rt6 *mlxsw_sp_rt6;
  2334. if (nh_grp->count != fib6_entry->nrt6)
  2335. return false;
  2336. list_for_each_entry(mlxsw_sp_rt6, &fib6_entry->rt6_list, list) {
  2337. struct in6_addr *gw;
  2338. int ifindex, weight;
  2339. ifindex = mlxsw_sp_rt6->rt->fib6_nh.nh_dev->ifindex;
  2340. weight = mlxsw_sp_rt6->rt->fib6_nh.nh_weight;
  2341. gw = &mlxsw_sp_rt6->rt->fib6_nh.nh_gw;
  2342. if (!mlxsw_sp_nexthop6_group_has_nexthop(nh_grp, gw, ifindex,
  2343. weight))
  2344. return false;
  2345. }
  2346. return true;
  2347. }
  2348. static int
  2349. mlxsw_sp_nexthop_group_cmp(struct rhashtable_compare_arg *arg, const void *ptr)
  2350. {
  2351. const struct mlxsw_sp_nexthop_group_cmp_arg *cmp_arg = arg->key;
  2352. const struct mlxsw_sp_nexthop_group *nh_grp = ptr;
  2353. switch (cmp_arg->proto) {
  2354. case MLXSW_SP_L3_PROTO_IPV4:
  2355. return cmp_arg->fi != mlxsw_sp_nexthop4_group_fi(nh_grp);
  2356. case MLXSW_SP_L3_PROTO_IPV6:
  2357. return !mlxsw_sp_nexthop6_group_cmp(nh_grp,
  2358. cmp_arg->fib6_entry);
  2359. default:
  2360. WARN_ON(1);
  2361. return 1;
  2362. }
  2363. }
  2364. static int
  2365. mlxsw_sp_nexthop_group_type(const struct mlxsw_sp_nexthop_group *nh_grp)
  2366. {
  2367. return nh_grp->neigh_tbl->family;
  2368. }
  2369. static u32 mlxsw_sp_nexthop_group_hash_obj(const void *data, u32 len, u32 seed)
  2370. {
  2371. const struct mlxsw_sp_nexthop_group *nh_grp = data;
  2372. const struct mlxsw_sp_nexthop *nh;
  2373. struct fib_info *fi;
  2374. unsigned int val;
  2375. int i;
  2376. switch (mlxsw_sp_nexthop_group_type(nh_grp)) {
  2377. case AF_INET:
  2378. fi = mlxsw_sp_nexthop4_group_fi(nh_grp);
  2379. return jhash(&fi, sizeof(fi), seed);
  2380. case AF_INET6:
  2381. val = nh_grp->count;
  2382. for (i = 0; i < nh_grp->count; i++) {
  2383. nh = &nh_grp->nexthops[i];
  2384. val ^= nh->ifindex;
  2385. }
  2386. return jhash(&val, sizeof(val), seed);
  2387. default:
  2388. WARN_ON(1);
  2389. return 0;
  2390. }
  2391. }
  2392. static u32
  2393. mlxsw_sp_nexthop6_group_hash(struct mlxsw_sp_fib6_entry *fib6_entry, u32 seed)
  2394. {
  2395. unsigned int val = fib6_entry->nrt6;
  2396. struct mlxsw_sp_rt6 *mlxsw_sp_rt6;
  2397. struct net_device *dev;
  2398. list_for_each_entry(mlxsw_sp_rt6, &fib6_entry->rt6_list, list) {
  2399. dev = mlxsw_sp_rt6->rt->fib6_nh.nh_dev;
  2400. val ^= dev->ifindex;
  2401. }
  2402. return jhash(&val, sizeof(val), seed);
  2403. }
  2404. static u32
  2405. mlxsw_sp_nexthop_group_hash(const void *data, u32 len, u32 seed)
  2406. {
  2407. const struct mlxsw_sp_nexthop_group_cmp_arg *cmp_arg = data;
  2408. switch (cmp_arg->proto) {
  2409. case MLXSW_SP_L3_PROTO_IPV4:
  2410. return jhash(&cmp_arg->fi, sizeof(cmp_arg->fi), seed);
  2411. case MLXSW_SP_L3_PROTO_IPV6:
  2412. return mlxsw_sp_nexthop6_group_hash(cmp_arg->fib6_entry, seed);
  2413. default:
  2414. WARN_ON(1);
  2415. return 0;
  2416. }
  2417. }
  2418. static const struct rhashtable_params mlxsw_sp_nexthop_group_ht_params = {
  2419. .head_offset = offsetof(struct mlxsw_sp_nexthop_group, ht_node),
  2420. .hashfn = mlxsw_sp_nexthop_group_hash,
  2421. .obj_hashfn = mlxsw_sp_nexthop_group_hash_obj,
  2422. .obj_cmpfn = mlxsw_sp_nexthop_group_cmp,
  2423. };
  2424. static int mlxsw_sp_nexthop_group_insert(struct mlxsw_sp *mlxsw_sp,
  2425. struct mlxsw_sp_nexthop_group *nh_grp)
  2426. {
  2427. if (mlxsw_sp_nexthop_group_type(nh_grp) == AF_INET6 &&
  2428. !nh_grp->gateway)
  2429. return 0;
  2430. return rhashtable_insert_fast(&mlxsw_sp->router->nexthop_group_ht,
  2431. &nh_grp->ht_node,
  2432. mlxsw_sp_nexthop_group_ht_params);
  2433. }
  2434. static void mlxsw_sp_nexthop_group_remove(struct mlxsw_sp *mlxsw_sp,
  2435. struct mlxsw_sp_nexthop_group *nh_grp)
  2436. {
  2437. if (mlxsw_sp_nexthop_group_type(nh_grp) == AF_INET6 &&
  2438. !nh_grp->gateway)
  2439. return;
  2440. rhashtable_remove_fast(&mlxsw_sp->router->nexthop_group_ht,
  2441. &nh_grp->ht_node,
  2442. mlxsw_sp_nexthop_group_ht_params);
  2443. }
  2444. static struct mlxsw_sp_nexthop_group *
  2445. mlxsw_sp_nexthop4_group_lookup(struct mlxsw_sp *mlxsw_sp,
  2446. struct fib_info *fi)
  2447. {
  2448. struct mlxsw_sp_nexthop_group_cmp_arg cmp_arg;
  2449. cmp_arg.proto = MLXSW_SP_L3_PROTO_IPV4;
  2450. cmp_arg.fi = fi;
  2451. return rhashtable_lookup_fast(&mlxsw_sp->router->nexthop_group_ht,
  2452. &cmp_arg,
  2453. mlxsw_sp_nexthop_group_ht_params);
  2454. }
  2455. static struct mlxsw_sp_nexthop_group *
  2456. mlxsw_sp_nexthop6_group_lookup(struct mlxsw_sp *mlxsw_sp,
  2457. struct mlxsw_sp_fib6_entry *fib6_entry)
  2458. {
  2459. struct mlxsw_sp_nexthop_group_cmp_arg cmp_arg;
  2460. cmp_arg.proto = MLXSW_SP_L3_PROTO_IPV6;
  2461. cmp_arg.fib6_entry = fib6_entry;
  2462. return rhashtable_lookup_fast(&mlxsw_sp->router->nexthop_group_ht,
  2463. &cmp_arg,
  2464. mlxsw_sp_nexthop_group_ht_params);
  2465. }
  2466. static const struct rhashtable_params mlxsw_sp_nexthop_ht_params = {
  2467. .key_offset = offsetof(struct mlxsw_sp_nexthop, key),
  2468. .head_offset = offsetof(struct mlxsw_sp_nexthop, ht_node),
  2469. .key_len = sizeof(struct mlxsw_sp_nexthop_key),
  2470. };
  2471. static int mlxsw_sp_nexthop_insert(struct mlxsw_sp *mlxsw_sp,
  2472. struct mlxsw_sp_nexthop *nh)
  2473. {
  2474. return rhashtable_insert_fast(&mlxsw_sp->router->nexthop_ht,
  2475. &nh->ht_node, mlxsw_sp_nexthop_ht_params);
  2476. }
  2477. static void mlxsw_sp_nexthop_remove(struct mlxsw_sp *mlxsw_sp,
  2478. struct mlxsw_sp_nexthop *nh)
  2479. {
  2480. rhashtable_remove_fast(&mlxsw_sp->router->nexthop_ht, &nh->ht_node,
  2481. mlxsw_sp_nexthop_ht_params);
  2482. }
  2483. static struct mlxsw_sp_nexthop *
  2484. mlxsw_sp_nexthop_lookup(struct mlxsw_sp *mlxsw_sp,
  2485. struct mlxsw_sp_nexthop_key key)
  2486. {
  2487. return rhashtable_lookup_fast(&mlxsw_sp->router->nexthop_ht, &key,
  2488. mlxsw_sp_nexthop_ht_params);
  2489. }
  2490. static int mlxsw_sp_adj_index_mass_update_vr(struct mlxsw_sp *mlxsw_sp,
  2491. const struct mlxsw_sp_fib *fib,
  2492. u32 adj_index, u16 ecmp_size,
  2493. u32 new_adj_index,
  2494. u16 new_ecmp_size)
  2495. {
  2496. char raleu_pl[MLXSW_REG_RALEU_LEN];
  2497. mlxsw_reg_raleu_pack(raleu_pl,
  2498. (enum mlxsw_reg_ralxx_protocol) fib->proto,
  2499. fib->vr->id, adj_index, ecmp_size, new_adj_index,
  2500. new_ecmp_size);
  2501. return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(raleu), raleu_pl);
  2502. }
  2503. static int mlxsw_sp_adj_index_mass_update(struct mlxsw_sp *mlxsw_sp,
  2504. struct mlxsw_sp_nexthop_group *nh_grp,
  2505. u32 old_adj_index, u16 old_ecmp_size)
  2506. {
  2507. struct mlxsw_sp_fib_entry *fib_entry;
  2508. struct mlxsw_sp_fib *fib = NULL;
  2509. int err;
  2510. list_for_each_entry(fib_entry, &nh_grp->fib_list, nexthop_group_node) {
  2511. if (fib == fib_entry->fib_node->fib)
  2512. continue;
  2513. fib = fib_entry->fib_node->fib;
  2514. err = mlxsw_sp_adj_index_mass_update_vr(mlxsw_sp, fib,
  2515. old_adj_index,
  2516. old_ecmp_size,
  2517. nh_grp->adj_index,
  2518. nh_grp->ecmp_size);
  2519. if (err)
  2520. return err;
  2521. }
  2522. return 0;
  2523. }
  2524. static int __mlxsw_sp_nexthop_update(struct mlxsw_sp *mlxsw_sp, u32 adj_index,
  2525. struct mlxsw_sp_nexthop *nh)
  2526. {
  2527. struct mlxsw_sp_neigh_entry *neigh_entry = nh->neigh_entry;
  2528. char ratr_pl[MLXSW_REG_RATR_LEN];
  2529. mlxsw_reg_ratr_pack(ratr_pl, MLXSW_REG_RATR_OP_WRITE_WRITE_ENTRY,
  2530. true, MLXSW_REG_RATR_TYPE_ETHERNET,
  2531. adj_index, neigh_entry->rif);
  2532. mlxsw_reg_ratr_eth_entry_pack(ratr_pl, neigh_entry->ha);
  2533. if (nh->counter_valid)
  2534. mlxsw_reg_ratr_counter_pack(ratr_pl, nh->counter_index, true);
  2535. else
  2536. mlxsw_reg_ratr_counter_pack(ratr_pl, 0, false);
  2537. return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ratr), ratr_pl);
  2538. }
  2539. int mlxsw_sp_nexthop_update(struct mlxsw_sp *mlxsw_sp, u32 adj_index,
  2540. struct mlxsw_sp_nexthop *nh)
  2541. {
  2542. int i;
  2543. for (i = 0; i < nh->num_adj_entries; i++) {
  2544. int err;
  2545. err = __mlxsw_sp_nexthop_update(mlxsw_sp, adj_index + i, nh);
  2546. if (err)
  2547. return err;
  2548. }
  2549. return 0;
  2550. }
  2551. static int __mlxsw_sp_nexthop_ipip_update(struct mlxsw_sp *mlxsw_sp,
  2552. u32 adj_index,
  2553. struct mlxsw_sp_nexthop *nh)
  2554. {
  2555. const struct mlxsw_sp_ipip_ops *ipip_ops;
  2556. ipip_ops = mlxsw_sp->router->ipip_ops_arr[nh->ipip_entry->ipipt];
  2557. return ipip_ops->nexthop_update(mlxsw_sp, adj_index, nh->ipip_entry);
  2558. }
  2559. static int mlxsw_sp_nexthop_ipip_update(struct mlxsw_sp *mlxsw_sp,
  2560. u32 adj_index,
  2561. struct mlxsw_sp_nexthop *nh)
  2562. {
  2563. int i;
  2564. for (i = 0; i < nh->num_adj_entries; i++) {
  2565. int err;
  2566. err = __mlxsw_sp_nexthop_ipip_update(mlxsw_sp, adj_index + i,
  2567. nh);
  2568. if (err)
  2569. return err;
  2570. }
  2571. return 0;
  2572. }
  2573. static int
  2574. mlxsw_sp_nexthop_group_update(struct mlxsw_sp *mlxsw_sp,
  2575. struct mlxsw_sp_nexthop_group *nh_grp,
  2576. bool reallocate)
  2577. {
  2578. u32 adj_index = nh_grp->adj_index; /* base */
  2579. struct mlxsw_sp_nexthop *nh;
  2580. int i;
  2581. int err;
  2582. for (i = 0; i < nh_grp->count; i++) {
  2583. nh = &nh_grp->nexthops[i];
  2584. if (!nh->should_offload) {
  2585. nh->offloaded = 0;
  2586. continue;
  2587. }
  2588. if (nh->update || reallocate) {
  2589. switch (nh->type) {
  2590. case MLXSW_SP_NEXTHOP_TYPE_ETH:
  2591. err = mlxsw_sp_nexthop_update
  2592. (mlxsw_sp, adj_index, nh);
  2593. break;
  2594. case MLXSW_SP_NEXTHOP_TYPE_IPIP:
  2595. err = mlxsw_sp_nexthop_ipip_update
  2596. (mlxsw_sp, adj_index, nh);
  2597. break;
  2598. }
  2599. if (err)
  2600. return err;
  2601. nh->update = 0;
  2602. nh->offloaded = 1;
  2603. }
  2604. adj_index += nh->num_adj_entries;
  2605. }
  2606. return 0;
  2607. }
  2608. static bool
  2609. mlxsw_sp_fib_node_entry_is_first(const struct mlxsw_sp_fib_node *fib_node,
  2610. const struct mlxsw_sp_fib_entry *fib_entry);
  2611. static int
  2612. mlxsw_sp_nexthop_fib_entries_update(struct mlxsw_sp *mlxsw_sp,
  2613. struct mlxsw_sp_nexthop_group *nh_grp)
  2614. {
  2615. struct mlxsw_sp_fib_entry *fib_entry;
  2616. int err;
  2617. list_for_each_entry(fib_entry, &nh_grp->fib_list, nexthop_group_node) {
  2618. if (!mlxsw_sp_fib_node_entry_is_first(fib_entry->fib_node,
  2619. fib_entry))
  2620. continue;
  2621. err = mlxsw_sp_fib_entry_update(mlxsw_sp, fib_entry);
  2622. if (err)
  2623. return err;
  2624. }
  2625. return 0;
  2626. }
  2627. static void
  2628. mlxsw_sp_fib_entry_offload_refresh(struct mlxsw_sp_fib_entry *fib_entry,
  2629. enum mlxsw_reg_ralue_op op, int err);
  2630. static void
  2631. mlxsw_sp_nexthop_fib_entries_refresh(struct mlxsw_sp_nexthop_group *nh_grp)
  2632. {
  2633. enum mlxsw_reg_ralue_op op = MLXSW_REG_RALUE_OP_WRITE_WRITE;
  2634. struct mlxsw_sp_fib_entry *fib_entry;
  2635. list_for_each_entry(fib_entry, &nh_grp->fib_list, nexthop_group_node) {
  2636. if (!mlxsw_sp_fib_node_entry_is_first(fib_entry->fib_node,
  2637. fib_entry))
  2638. continue;
  2639. mlxsw_sp_fib_entry_offload_refresh(fib_entry, op, 0);
  2640. }
  2641. }
  2642. static void mlxsw_sp_adj_grp_size_round_up(u16 *p_adj_grp_size)
  2643. {
  2644. /* Valid sizes for an adjacency group are:
  2645. * 1-64, 512, 1024, 2048 and 4096.
  2646. */
  2647. if (*p_adj_grp_size <= 64)
  2648. return;
  2649. else if (*p_adj_grp_size <= 512)
  2650. *p_adj_grp_size = 512;
  2651. else if (*p_adj_grp_size <= 1024)
  2652. *p_adj_grp_size = 1024;
  2653. else if (*p_adj_grp_size <= 2048)
  2654. *p_adj_grp_size = 2048;
  2655. else
  2656. *p_adj_grp_size = 4096;
  2657. }
  2658. static void mlxsw_sp_adj_grp_size_round_down(u16 *p_adj_grp_size,
  2659. unsigned int alloc_size)
  2660. {
  2661. if (alloc_size >= 4096)
  2662. *p_adj_grp_size = 4096;
  2663. else if (alloc_size >= 2048)
  2664. *p_adj_grp_size = 2048;
  2665. else if (alloc_size >= 1024)
  2666. *p_adj_grp_size = 1024;
  2667. else if (alloc_size >= 512)
  2668. *p_adj_grp_size = 512;
  2669. }
  2670. static int mlxsw_sp_fix_adj_grp_size(struct mlxsw_sp *mlxsw_sp,
  2671. u16 *p_adj_grp_size)
  2672. {
  2673. unsigned int alloc_size;
  2674. int err;
  2675. /* Round up the requested group size to the next size supported
  2676. * by the device and make sure the request can be satisfied.
  2677. */
  2678. mlxsw_sp_adj_grp_size_round_up(p_adj_grp_size);
  2679. err = mlxsw_sp_kvdl_alloc_count_query(mlxsw_sp,
  2680. MLXSW_SP_KVDL_ENTRY_TYPE_ADJ,
  2681. *p_adj_grp_size, &alloc_size);
  2682. if (err)
  2683. return err;
  2684. /* It is possible the allocation results in more allocated
  2685. * entries than requested. Try to use as much of them as
  2686. * possible.
  2687. */
  2688. mlxsw_sp_adj_grp_size_round_down(p_adj_grp_size, alloc_size);
  2689. return 0;
  2690. }
  2691. static void
  2692. mlxsw_sp_nexthop_group_normalize(struct mlxsw_sp_nexthop_group *nh_grp)
  2693. {
  2694. int i, g = 0, sum_norm_weight = 0;
  2695. struct mlxsw_sp_nexthop *nh;
  2696. for (i = 0; i < nh_grp->count; i++) {
  2697. nh = &nh_grp->nexthops[i];
  2698. if (!nh->should_offload)
  2699. continue;
  2700. if (g > 0)
  2701. g = gcd(nh->nh_weight, g);
  2702. else
  2703. g = nh->nh_weight;
  2704. }
  2705. for (i = 0; i < nh_grp->count; i++) {
  2706. nh = &nh_grp->nexthops[i];
  2707. if (!nh->should_offload)
  2708. continue;
  2709. nh->norm_nh_weight = nh->nh_weight / g;
  2710. sum_norm_weight += nh->norm_nh_weight;
  2711. }
  2712. nh_grp->sum_norm_weight = sum_norm_weight;
  2713. }
  2714. static void
  2715. mlxsw_sp_nexthop_group_rebalance(struct mlxsw_sp_nexthop_group *nh_grp)
  2716. {
  2717. int total = nh_grp->sum_norm_weight;
  2718. u16 ecmp_size = nh_grp->ecmp_size;
  2719. int i, weight = 0, lower_bound = 0;
  2720. for (i = 0; i < nh_grp->count; i++) {
  2721. struct mlxsw_sp_nexthop *nh = &nh_grp->nexthops[i];
  2722. int upper_bound;
  2723. if (!nh->should_offload)
  2724. continue;
  2725. weight += nh->norm_nh_weight;
  2726. upper_bound = DIV_ROUND_CLOSEST(ecmp_size * weight, total);
  2727. nh->num_adj_entries = upper_bound - lower_bound;
  2728. lower_bound = upper_bound;
  2729. }
  2730. }
  2731. static void
  2732. mlxsw_sp_nexthop_group_refresh(struct mlxsw_sp *mlxsw_sp,
  2733. struct mlxsw_sp_nexthop_group *nh_grp)
  2734. {
  2735. u16 ecmp_size, old_ecmp_size;
  2736. struct mlxsw_sp_nexthop *nh;
  2737. bool offload_change = false;
  2738. u32 adj_index;
  2739. bool old_adj_index_valid;
  2740. u32 old_adj_index;
  2741. int i;
  2742. int err;
  2743. if (!nh_grp->gateway) {
  2744. mlxsw_sp_nexthop_fib_entries_update(mlxsw_sp, nh_grp);
  2745. return;
  2746. }
  2747. for (i = 0; i < nh_grp->count; i++) {
  2748. nh = &nh_grp->nexthops[i];
  2749. if (nh->should_offload != nh->offloaded) {
  2750. offload_change = true;
  2751. if (nh->should_offload)
  2752. nh->update = 1;
  2753. }
  2754. }
  2755. if (!offload_change) {
  2756. /* Nothing was added or removed, so no need to reallocate. Just
  2757. * update MAC on existing adjacency indexes.
  2758. */
  2759. err = mlxsw_sp_nexthop_group_update(mlxsw_sp, nh_grp, false);
  2760. if (err) {
  2761. dev_warn(mlxsw_sp->bus_info->dev, "Failed to update neigh MAC in adjacency table.\n");
  2762. goto set_trap;
  2763. }
  2764. return;
  2765. }
  2766. mlxsw_sp_nexthop_group_normalize(nh_grp);
  2767. if (!nh_grp->sum_norm_weight)
  2768. /* No neigh of this group is connected so we just set
  2769. * the trap and let everthing flow through kernel.
  2770. */
  2771. goto set_trap;
  2772. ecmp_size = nh_grp->sum_norm_weight;
  2773. err = mlxsw_sp_fix_adj_grp_size(mlxsw_sp, &ecmp_size);
  2774. if (err)
  2775. /* No valid allocation size available. */
  2776. goto set_trap;
  2777. err = mlxsw_sp_kvdl_alloc(mlxsw_sp, MLXSW_SP_KVDL_ENTRY_TYPE_ADJ,
  2778. ecmp_size, &adj_index);
  2779. if (err) {
  2780. /* We ran out of KVD linear space, just set the
  2781. * trap and let everything flow through kernel.
  2782. */
  2783. dev_warn(mlxsw_sp->bus_info->dev, "Failed to allocate KVD linear area for nexthop group.\n");
  2784. goto set_trap;
  2785. }
  2786. old_adj_index_valid = nh_grp->adj_index_valid;
  2787. old_adj_index = nh_grp->adj_index;
  2788. old_ecmp_size = nh_grp->ecmp_size;
  2789. nh_grp->adj_index_valid = 1;
  2790. nh_grp->adj_index = adj_index;
  2791. nh_grp->ecmp_size = ecmp_size;
  2792. mlxsw_sp_nexthop_group_rebalance(nh_grp);
  2793. err = mlxsw_sp_nexthop_group_update(mlxsw_sp, nh_grp, true);
  2794. if (err) {
  2795. dev_warn(mlxsw_sp->bus_info->dev, "Failed to update neigh MAC in adjacency table.\n");
  2796. goto set_trap;
  2797. }
  2798. if (!old_adj_index_valid) {
  2799. /* The trap was set for fib entries, so we have to call
  2800. * fib entry update to unset it and use adjacency index.
  2801. */
  2802. err = mlxsw_sp_nexthop_fib_entries_update(mlxsw_sp, nh_grp);
  2803. if (err) {
  2804. dev_warn(mlxsw_sp->bus_info->dev, "Failed to add adjacency index to fib entries.\n");
  2805. goto set_trap;
  2806. }
  2807. return;
  2808. }
  2809. err = mlxsw_sp_adj_index_mass_update(mlxsw_sp, nh_grp,
  2810. old_adj_index, old_ecmp_size);
  2811. mlxsw_sp_kvdl_free(mlxsw_sp, MLXSW_SP_KVDL_ENTRY_TYPE_ADJ,
  2812. old_ecmp_size, old_adj_index);
  2813. if (err) {
  2814. dev_warn(mlxsw_sp->bus_info->dev, "Failed to mass-update adjacency index for nexthop group.\n");
  2815. goto set_trap;
  2816. }
  2817. /* Offload state within the group changed, so update the flags. */
  2818. mlxsw_sp_nexthop_fib_entries_refresh(nh_grp);
  2819. return;
  2820. set_trap:
  2821. old_adj_index_valid = nh_grp->adj_index_valid;
  2822. nh_grp->adj_index_valid = 0;
  2823. for (i = 0; i < nh_grp->count; i++) {
  2824. nh = &nh_grp->nexthops[i];
  2825. nh->offloaded = 0;
  2826. }
  2827. err = mlxsw_sp_nexthop_fib_entries_update(mlxsw_sp, nh_grp);
  2828. if (err)
  2829. dev_warn(mlxsw_sp->bus_info->dev, "Failed to set traps for fib entries.\n");
  2830. if (old_adj_index_valid)
  2831. mlxsw_sp_kvdl_free(mlxsw_sp, MLXSW_SP_KVDL_ENTRY_TYPE_ADJ,
  2832. nh_grp->ecmp_size, nh_grp->adj_index);
  2833. }
  2834. static void __mlxsw_sp_nexthop_neigh_update(struct mlxsw_sp_nexthop *nh,
  2835. bool removing)
  2836. {
  2837. if (!removing)
  2838. nh->should_offload = 1;
  2839. else
  2840. nh->should_offload = 0;
  2841. nh->update = 1;
  2842. }
  2843. static int
  2844. mlxsw_sp_nexthop_dead_neigh_replace(struct mlxsw_sp *mlxsw_sp,
  2845. struct mlxsw_sp_neigh_entry *neigh_entry)
  2846. {
  2847. struct neighbour *n, *old_n = neigh_entry->key.n;
  2848. struct mlxsw_sp_nexthop *nh;
  2849. bool entry_connected;
  2850. u8 nud_state, dead;
  2851. int err;
  2852. nh = list_first_entry(&neigh_entry->nexthop_list,
  2853. struct mlxsw_sp_nexthop, neigh_list_node);
  2854. n = neigh_lookup(nh->nh_grp->neigh_tbl, &nh->gw_addr, nh->rif->dev);
  2855. if (!n) {
  2856. n = neigh_create(nh->nh_grp->neigh_tbl, &nh->gw_addr,
  2857. nh->rif->dev);
  2858. if (IS_ERR(n))
  2859. return PTR_ERR(n);
  2860. neigh_event_send(n, NULL);
  2861. }
  2862. mlxsw_sp_neigh_entry_remove(mlxsw_sp, neigh_entry);
  2863. neigh_entry->key.n = n;
  2864. err = mlxsw_sp_neigh_entry_insert(mlxsw_sp, neigh_entry);
  2865. if (err)
  2866. goto err_neigh_entry_insert;
  2867. read_lock_bh(&n->lock);
  2868. nud_state = n->nud_state;
  2869. dead = n->dead;
  2870. read_unlock_bh(&n->lock);
  2871. entry_connected = nud_state & NUD_VALID && !dead;
  2872. list_for_each_entry(nh, &neigh_entry->nexthop_list,
  2873. neigh_list_node) {
  2874. neigh_release(old_n);
  2875. neigh_clone(n);
  2876. __mlxsw_sp_nexthop_neigh_update(nh, !entry_connected);
  2877. mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh->nh_grp);
  2878. }
  2879. neigh_release(n);
  2880. return 0;
  2881. err_neigh_entry_insert:
  2882. neigh_entry->key.n = old_n;
  2883. mlxsw_sp_neigh_entry_insert(mlxsw_sp, neigh_entry);
  2884. neigh_release(n);
  2885. return err;
  2886. }
  2887. static void
  2888. mlxsw_sp_nexthop_neigh_update(struct mlxsw_sp *mlxsw_sp,
  2889. struct mlxsw_sp_neigh_entry *neigh_entry,
  2890. bool removing, bool dead)
  2891. {
  2892. struct mlxsw_sp_nexthop *nh;
  2893. if (list_empty(&neigh_entry->nexthop_list))
  2894. return;
  2895. if (dead) {
  2896. int err;
  2897. err = mlxsw_sp_nexthop_dead_neigh_replace(mlxsw_sp,
  2898. neigh_entry);
  2899. if (err)
  2900. dev_err(mlxsw_sp->bus_info->dev, "Failed to replace dead neigh\n");
  2901. return;
  2902. }
  2903. list_for_each_entry(nh, &neigh_entry->nexthop_list,
  2904. neigh_list_node) {
  2905. __mlxsw_sp_nexthop_neigh_update(nh, removing);
  2906. mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh->nh_grp);
  2907. }
  2908. }
  2909. static void mlxsw_sp_nexthop_rif_init(struct mlxsw_sp_nexthop *nh,
  2910. struct mlxsw_sp_rif *rif)
  2911. {
  2912. if (nh->rif)
  2913. return;
  2914. nh->rif = rif;
  2915. list_add(&nh->rif_list_node, &rif->nexthop_list);
  2916. }
  2917. static void mlxsw_sp_nexthop_rif_fini(struct mlxsw_sp_nexthop *nh)
  2918. {
  2919. if (!nh->rif)
  2920. return;
  2921. list_del(&nh->rif_list_node);
  2922. nh->rif = NULL;
  2923. }
  2924. static int mlxsw_sp_nexthop_neigh_init(struct mlxsw_sp *mlxsw_sp,
  2925. struct mlxsw_sp_nexthop *nh)
  2926. {
  2927. struct mlxsw_sp_neigh_entry *neigh_entry;
  2928. struct neighbour *n;
  2929. u8 nud_state, dead;
  2930. int err;
  2931. if (!nh->nh_grp->gateway || nh->neigh_entry)
  2932. return 0;
  2933. /* Take a reference of neigh here ensuring that neigh would
  2934. * not be destructed before the nexthop entry is finished.
  2935. * The reference is taken either in neigh_lookup() or
  2936. * in neigh_create() in case n is not found.
  2937. */
  2938. n = neigh_lookup(nh->nh_grp->neigh_tbl, &nh->gw_addr, nh->rif->dev);
  2939. if (!n) {
  2940. n = neigh_create(nh->nh_grp->neigh_tbl, &nh->gw_addr,
  2941. nh->rif->dev);
  2942. if (IS_ERR(n))
  2943. return PTR_ERR(n);
  2944. neigh_event_send(n, NULL);
  2945. }
  2946. neigh_entry = mlxsw_sp_neigh_entry_lookup(mlxsw_sp, n);
  2947. if (!neigh_entry) {
  2948. neigh_entry = mlxsw_sp_neigh_entry_create(mlxsw_sp, n);
  2949. if (IS_ERR(neigh_entry)) {
  2950. err = -EINVAL;
  2951. goto err_neigh_entry_create;
  2952. }
  2953. }
  2954. /* If that is the first nexthop connected to that neigh, add to
  2955. * nexthop_neighs_list
  2956. */
  2957. if (list_empty(&neigh_entry->nexthop_list))
  2958. list_add_tail(&neigh_entry->nexthop_neighs_list_node,
  2959. &mlxsw_sp->router->nexthop_neighs_list);
  2960. nh->neigh_entry = neigh_entry;
  2961. list_add_tail(&nh->neigh_list_node, &neigh_entry->nexthop_list);
  2962. read_lock_bh(&n->lock);
  2963. nud_state = n->nud_state;
  2964. dead = n->dead;
  2965. read_unlock_bh(&n->lock);
  2966. __mlxsw_sp_nexthop_neigh_update(nh, !(nud_state & NUD_VALID && !dead));
  2967. return 0;
  2968. err_neigh_entry_create:
  2969. neigh_release(n);
  2970. return err;
  2971. }
  2972. static void mlxsw_sp_nexthop_neigh_fini(struct mlxsw_sp *mlxsw_sp,
  2973. struct mlxsw_sp_nexthop *nh)
  2974. {
  2975. struct mlxsw_sp_neigh_entry *neigh_entry = nh->neigh_entry;
  2976. struct neighbour *n;
  2977. if (!neigh_entry)
  2978. return;
  2979. n = neigh_entry->key.n;
  2980. __mlxsw_sp_nexthop_neigh_update(nh, true);
  2981. list_del(&nh->neigh_list_node);
  2982. nh->neigh_entry = NULL;
  2983. /* If that is the last nexthop connected to that neigh, remove from
  2984. * nexthop_neighs_list
  2985. */
  2986. if (list_empty(&neigh_entry->nexthop_list))
  2987. list_del(&neigh_entry->nexthop_neighs_list_node);
  2988. if (!neigh_entry->connected && list_empty(&neigh_entry->nexthop_list))
  2989. mlxsw_sp_neigh_entry_destroy(mlxsw_sp, neigh_entry);
  2990. neigh_release(n);
  2991. }
  2992. static bool mlxsw_sp_ipip_netdev_ul_up(struct net_device *ol_dev)
  2993. {
  2994. struct net_device *ul_dev = __mlxsw_sp_ipip_netdev_ul_dev_get(ol_dev);
  2995. return ul_dev ? (ul_dev->flags & IFF_UP) : true;
  2996. }
  2997. static void mlxsw_sp_nexthop_ipip_init(struct mlxsw_sp *mlxsw_sp,
  2998. struct mlxsw_sp_nexthop *nh,
  2999. struct mlxsw_sp_ipip_entry *ipip_entry)
  3000. {
  3001. bool removing;
  3002. if (!nh->nh_grp->gateway || nh->ipip_entry)
  3003. return;
  3004. nh->ipip_entry = ipip_entry;
  3005. removing = !mlxsw_sp_ipip_netdev_ul_up(ipip_entry->ol_dev);
  3006. __mlxsw_sp_nexthop_neigh_update(nh, removing);
  3007. mlxsw_sp_nexthop_rif_init(nh, &ipip_entry->ol_lb->common);
  3008. }
  3009. static void mlxsw_sp_nexthop_ipip_fini(struct mlxsw_sp *mlxsw_sp,
  3010. struct mlxsw_sp_nexthop *nh)
  3011. {
  3012. struct mlxsw_sp_ipip_entry *ipip_entry = nh->ipip_entry;
  3013. if (!ipip_entry)
  3014. return;
  3015. __mlxsw_sp_nexthop_neigh_update(nh, true);
  3016. nh->ipip_entry = NULL;
  3017. }
  3018. static bool mlxsw_sp_nexthop4_ipip_type(const struct mlxsw_sp *mlxsw_sp,
  3019. const struct fib_nh *fib_nh,
  3020. enum mlxsw_sp_ipip_type *p_ipipt)
  3021. {
  3022. struct net_device *dev = fib_nh->nh_dev;
  3023. return dev &&
  3024. fib_nh->nh_parent->fib_type == RTN_UNICAST &&
  3025. mlxsw_sp_netdev_ipip_type(mlxsw_sp, dev, p_ipipt);
  3026. }
  3027. static void mlxsw_sp_nexthop_type_fini(struct mlxsw_sp *mlxsw_sp,
  3028. struct mlxsw_sp_nexthop *nh)
  3029. {
  3030. switch (nh->type) {
  3031. case MLXSW_SP_NEXTHOP_TYPE_ETH:
  3032. mlxsw_sp_nexthop_neigh_fini(mlxsw_sp, nh);
  3033. mlxsw_sp_nexthop_rif_fini(nh);
  3034. break;
  3035. case MLXSW_SP_NEXTHOP_TYPE_IPIP:
  3036. mlxsw_sp_nexthop_rif_fini(nh);
  3037. mlxsw_sp_nexthop_ipip_fini(mlxsw_sp, nh);
  3038. break;
  3039. }
  3040. }
  3041. static int mlxsw_sp_nexthop4_type_init(struct mlxsw_sp *mlxsw_sp,
  3042. struct mlxsw_sp_nexthop *nh,
  3043. struct fib_nh *fib_nh)
  3044. {
  3045. const struct mlxsw_sp_ipip_ops *ipip_ops;
  3046. struct net_device *dev = fib_nh->nh_dev;
  3047. struct mlxsw_sp_ipip_entry *ipip_entry;
  3048. struct mlxsw_sp_rif *rif;
  3049. int err;
  3050. ipip_entry = mlxsw_sp_ipip_entry_find_by_ol_dev(mlxsw_sp, dev);
  3051. if (ipip_entry) {
  3052. ipip_ops = mlxsw_sp->router->ipip_ops_arr[ipip_entry->ipipt];
  3053. if (ipip_ops->can_offload(mlxsw_sp, dev,
  3054. MLXSW_SP_L3_PROTO_IPV4)) {
  3055. nh->type = MLXSW_SP_NEXTHOP_TYPE_IPIP;
  3056. mlxsw_sp_nexthop_ipip_init(mlxsw_sp, nh, ipip_entry);
  3057. return 0;
  3058. }
  3059. }
  3060. nh->type = MLXSW_SP_NEXTHOP_TYPE_ETH;
  3061. rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
  3062. if (!rif)
  3063. return 0;
  3064. mlxsw_sp_nexthop_rif_init(nh, rif);
  3065. err = mlxsw_sp_nexthop_neigh_init(mlxsw_sp, nh);
  3066. if (err)
  3067. goto err_neigh_init;
  3068. return 0;
  3069. err_neigh_init:
  3070. mlxsw_sp_nexthop_rif_fini(nh);
  3071. return err;
  3072. }
  3073. static void mlxsw_sp_nexthop4_type_fini(struct mlxsw_sp *mlxsw_sp,
  3074. struct mlxsw_sp_nexthop *nh)
  3075. {
  3076. mlxsw_sp_nexthop_type_fini(mlxsw_sp, nh);
  3077. }
  3078. static int mlxsw_sp_nexthop4_init(struct mlxsw_sp *mlxsw_sp,
  3079. struct mlxsw_sp_nexthop_group *nh_grp,
  3080. struct mlxsw_sp_nexthop *nh,
  3081. struct fib_nh *fib_nh)
  3082. {
  3083. struct net_device *dev = fib_nh->nh_dev;
  3084. struct in_device *in_dev;
  3085. int err;
  3086. nh->nh_grp = nh_grp;
  3087. nh->key.fib_nh = fib_nh;
  3088. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  3089. nh->nh_weight = fib_nh->nh_weight;
  3090. #else
  3091. nh->nh_weight = 1;
  3092. #endif
  3093. memcpy(&nh->gw_addr, &fib_nh->nh_gw, sizeof(fib_nh->nh_gw));
  3094. err = mlxsw_sp_nexthop_insert(mlxsw_sp, nh);
  3095. if (err)
  3096. return err;
  3097. mlxsw_sp_nexthop_counter_alloc(mlxsw_sp, nh);
  3098. list_add_tail(&nh->router_list_node, &mlxsw_sp->router->nexthop_list);
  3099. if (!dev)
  3100. return 0;
  3101. in_dev = __in_dev_get_rtnl(dev);
  3102. if (in_dev && IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev) &&
  3103. fib_nh->nh_flags & RTNH_F_LINKDOWN)
  3104. return 0;
  3105. err = mlxsw_sp_nexthop4_type_init(mlxsw_sp, nh, fib_nh);
  3106. if (err)
  3107. goto err_nexthop_neigh_init;
  3108. return 0;
  3109. err_nexthop_neigh_init:
  3110. mlxsw_sp_nexthop_remove(mlxsw_sp, nh);
  3111. return err;
  3112. }
  3113. static void mlxsw_sp_nexthop4_fini(struct mlxsw_sp *mlxsw_sp,
  3114. struct mlxsw_sp_nexthop *nh)
  3115. {
  3116. mlxsw_sp_nexthop4_type_fini(mlxsw_sp, nh);
  3117. list_del(&nh->router_list_node);
  3118. mlxsw_sp_nexthop_counter_free(mlxsw_sp, nh);
  3119. mlxsw_sp_nexthop_remove(mlxsw_sp, nh);
  3120. }
  3121. static void mlxsw_sp_nexthop4_event(struct mlxsw_sp *mlxsw_sp,
  3122. unsigned long event, struct fib_nh *fib_nh)
  3123. {
  3124. struct mlxsw_sp_nexthop_key key;
  3125. struct mlxsw_sp_nexthop *nh;
  3126. if (mlxsw_sp->router->aborted)
  3127. return;
  3128. key.fib_nh = fib_nh;
  3129. nh = mlxsw_sp_nexthop_lookup(mlxsw_sp, key);
  3130. if (WARN_ON_ONCE(!nh))
  3131. return;
  3132. switch (event) {
  3133. case FIB_EVENT_NH_ADD:
  3134. mlxsw_sp_nexthop4_type_init(mlxsw_sp, nh, fib_nh);
  3135. break;
  3136. case FIB_EVENT_NH_DEL:
  3137. mlxsw_sp_nexthop4_type_fini(mlxsw_sp, nh);
  3138. break;
  3139. }
  3140. mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh->nh_grp);
  3141. }
  3142. static void mlxsw_sp_nexthop_rif_update(struct mlxsw_sp *mlxsw_sp,
  3143. struct mlxsw_sp_rif *rif)
  3144. {
  3145. struct mlxsw_sp_nexthop *nh;
  3146. bool removing;
  3147. list_for_each_entry(nh, &rif->nexthop_list, rif_list_node) {
  3148. switch (nh->type) {
  3149. case MLXSW_SP_NEXTHOP_TYPE_ETH:
  3150. removing = false;
  3151. break;
  3152. case MLXSW_SP_NEXTHOP_TYPE_IPIP:
  3153. removing = !mlxsw_sp_ipip_netdev_ul_up(rif->dev);
  3154. break;
  3155. default:
  3156. WARN_ON(1);
  3157. continue;
  3158. }
  3159. __mlxsw_sp_nexthop_neigh_update(nh, removing);
  3160. mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh->nh_grp);
  3161. }
  3162. }
  3163. static void mlxsw_sp_nexthop_rif_migrate(struct mlxsw_sp *mlxsw_sp,
  3164. struct mlxsw_sp_rif *old_rif,
  3165. struct mlxsw_sp_rif *new_rif)
  3166. {
  3167. struct mlxsw_sp_nexthop *nh;
  3168. list_splice_init(&old_rif->nexthop_list, &new_rif->nexthop_list);
  3169. list_for_each_entry(nh, &new_rif->nexthop_list, rif_list_node)
  3170. nh->rif = new_rif;
  3171. mlxsw_sp_nexthop_rif_update(mlxsw_sp, new_rif);
  3172. }
  3173. static void mlxsw_sp_nexthop_rif_gone_sync(struct mlxsw_sp *mlxsw_sp,
  3174. struct mlxsw_sp_rif *rif)
  3175. {
  3176. struct mlxsw_sp_nexthop *nh, *tmp;
  3177. list_for_each_entry_safe(nh, tmp, &rif->nexthop_list, rif_list_node) {
  3178. mlxsw_sp_nexthop_type_fini(mlxsw_sp, nh);
  3179. mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh->nh_grp);
  3180. }
  3181. }
  3182. static bool mlxsw_sp_fi_is_gateway(const struct mlxsw_sp *mlxsw_sp,
  3183. const struct fib_info *fi)
  3184. {
  3185. return fi->fib_nh->nh_scope == RT_SCOPE_LINK ||
  3186. mlxsw_sp_nexthop4_ipip_type(mlxsw_sp, fi->fib_nh, NULL);
  3187. }
  3188. static struct mlxsw_sp_nexthop_group *
  3189. mlxsw_sp_nexthop4_group_create(struct mlxsw_sp *mlxsw_sp, struct fib_info *fi)
  3190. {
  3191. struct mlxsw_sp_nexthop_group *nh_grp;
  3192. struct mlxsw_sp_nexthop *nh;
  3193. struct fib_nh *fib_nh;
  3194. size_t alloc_size;
  3195. int i;
  3196. int err;
  3197. alloc_size = sizeof(*nh_grp) +
  3198. fi->fib_nhs * sizeof(struct mlxsw_sp_nexthop);
  3199. nh_grp = kzalloc(alloc_size, GFP_KERNEL);
  3200. if (!nh_grp)
  3201. return ERR_PTR(-ENOMEM);
  3202. nh_grp->priv = fi;
  3203. INIT_LIST_HEAD(&nh_grp->fib_list);
  3204. nh_grp->neigh_tbl = &arp_tbl;
  3205. nh_grp->gateway = mlxsw_sp_fi_is_gateway(mlxsw_sp, fi);
  3206. nh_grp->count = fi->fib_nhs;
  3207. fib_info_hold(fi);
  3208. for (i = 0; i < nh_grp->count; i++) {
  3209. nh = &nh_grp->nexthops[i];
  3210. fib_nh = &fi->fib_nh[i];
  3211. err = mlxsw_sp_nexthop4_init(mlxsw_sp, nh_grp, nh, fib_nh);
  3212. if (err)
  3213. goto err_nexthop4_init;
  3214. }
  3215. err = mlxsw_sp_nexthop_group_insert(mlxsw_sp, nh_grp);
  3216. if (err)
  3217. goto err_nexthop_group_insert;
  3218. mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh_grp);
  3219. return nh_grp;
  3220. err_nexthop_group_insert:
  3221. err_nexthop4_init:
  3222. for (i--; i >= 0; i--) {
  3223. nh = &nh_grp->nexthops[i];
  3224. mlxsw_sp_nexthop4_fini(mlxsw_sp, nh);
  3225. }
  3226. fib_info_put(fi);
  3227. kfree(nh_grp);
  3228. return ERR_PTR(err);
  3229. }
  3230. static void
  3231. mlxsw_sp_nexthop4_group_destroy(struct mlxsw_sp *mlxsw_sp,
  3232. struct mlxsw_sp_nexthop_group *nh_grp)
  3233. {
  3234. struct mlxsw_sp_nexthop *nh;
  3235. int i;
  3236. mlxsw_sp_nexthop_group_remove(mlxsw_sp, nh_grp);
  3237. for (i = 0; i < nh_grp->count; i++) {
  3238. nh = &nh_grp->nexthops[i];
  3239. mlxsw_sp_nexthop4_fini(mlxsw_sp, nh);
  3240. }
  3241. mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh_grp);
  3242. WARN_ON_ONCE(nh_grp->adj_index_valid);
  3243. fib_info_put(mlxsw_sp_nexthop4_group_fi(nh_grp));
  3244. kfree(nh_grp);
  3245. }
  3246. static int mlxsw_sp_nexthop4_group_get(struct mlxsw_sp *mlxsw_sp,
  3247. struct mlxsw_sp_fib_entry *fib_entry,
  3248. struct fib_info *fi)
  3249. {
  3250. struct mlxsw_sp_nexthop_group *nh_grp;
  3251. nh_grp = mlxsw_sp_nexthop4_group_lookup(mlxsw_sp, fi);
  3252. if (!nh_grp) {
  3253. nh_grp = mlxsw_sp_nexthop4_group_create(mlxsw_sp, fi);
  3254. if (IS_ERR(nh_grp))
  3255. return PTR_ERR(nh_grp);
  3256. }
  3257. list_add_tail(&fib_entry->nexthop_group_node, &nh_grp->fib_list);
  3258. fib_entry->nh_group = nh_grp;
  3259. return 0;
  3260. }
  3261. static void mlxsw_sp_nexthop4_group_put(struct mlxsw_sp *mlxsw_sp,
  3262. struct mlxsw_sp_fib_entry *fib_entry)
  3263. {
  3264. struct mlxsw_sp_nexthop_group *nh_grp = fib_entry->nh_group;
  3265. list_del(&fib_entry->nexthop_group_node);
  3266. if (!list_empty(&nh_grp->fib_list))
  3267. return;
  3268. mlxsw_sp_nexthop4_group_destroy(mlxsw_sp, nh_grp);
  3269. }
  3270. static bool
  3271. mlxsw_sp_fib4_entry_should_offload(const struct mlxsw_sp_fib_entry *fib_entry)
  3272. {
  3273. struct mlxsw_sp_fib4_entry *fib4_entry;
  3274. fib4_entry = container_of(fib_entry, struct mlxsw_sp_fib4_entry,
  3275. common);
  3276. return !fib4_entry->tos;
  3277. }
  3278. static bool
  3279. mlxsw_sp_fib_entry_should_offload(const struct mlxsw_sp_fib_entry *fib_entry)
  3280. {
  3281. struct mlxsw_sp_nexthop_group *nh_group = fib_entry->nh_group;
  3282. switch (fib_entry->fib_node->fib->proto) {
  3283. case MLXSW_SP_L3_PROTO_IPV4:
  3284. if (!mlxsw_sp_fib4_entry_should_offload(fib_entry))
  3285. return false;
  3286. break;
  3287. case MLXSW_SP_L3_PROTO_IPV6:
  3288. break;
  3289. }
  3290. switch (fib_entry->type) {
  3291. case MLXSW_SP_FIB_ENTRY_TYPE_REMOTE:
  3292. return !!nh_group->adj_index_valid;
  3293. case MLXSW_SP_FIB_ENTRY_TYPE_LOCAL:
  3294. return !!nh_group->nh_rif;
  3295. case MLXSW_SP_FIB_ENTRY_TYPE_IPIP_DECAP:
  3296. return true;
  3297. default:
  3298. return false;
  3299. }
  3300. }
  3301. static struct mlxsw_sp_nexthop *
  3302. mlxsw_sp_rt6_nexthop(struct mlxsw_sp_nexthop_group *nh_grp,
  3303. const struct mlxsw_sp_rt6 *mlxsw_sp_rt6)
  3304. {
  3305. int i;
  3306. for (i = 0; i < nh_grp->count; i++) {
  3307. struct mlxsw_sp_nexthop *nh = &nh_grp->nexthops[i];
  3308. struct fib6_info *rt = mlxsw_sp_rt6->rt;
  3309. if (nh->rif && nh->rif->dev == rt->fib6_nh.nh_dev &&
  3310. ipv6_addr_equal((const struct in6_addr *) &nh->gw_addr,
  3311. &rt->fib6_nh.nh_gw))
  3312. return nh;
  3313. continue;
  3314. }
  3315. return NULL;
  3316. }
  3317. static void
  3318. mlxsw_sp_fib4_entry_offload_set(struct mlxsw_sp_fib_entry *fib_entry)
  3319. {
  3320. struct mlxsw_sp_nexthop_group *nh_grp = fib_entry->nh_group;
  3321. int i;
  3322. if (fib_entry->type == MLXSW_SP_FIB_ENTRY_TYPE_LOCAL ||
  3323. fib_entry->type == MLXSW_SP_FIB_ENTRY_TYPE_IPIP_DECAP) {
  3324. nh_grp->nexthops->key.fib_nh->nh_flags |= RTNH_F_OFFLOAD;
  3325. return;
  3326. }
  3327. for (i = 0; i < nh_grp->count; i++) {
  3328. struct mlxsw_sp_nexthop *nh = &nh_grp->nexthops[i];
  3329. if (nh->offloaded)
  3330. nh->key.fib_nh->nh_flags |= RTNH_F_OFFLOAD;
  3331. else
  3332. nh->key.fib_nh->nh_flags &= ~RTNH_F_OFFLOAD;
  3333. }
  3334. }
  3335. static void
  3336. mlxsw_sp_fib4_entry_offload_unset(struct mlxsw_sp_fib_entry *fib_entry)
  3337. {
  3338. struct mlxsw_sp_nexthop_group *nh_grp = fib_entry->nh_group;
  3339. int i;
  3340. if (!list_is_singular(&nh_grp->fib_list))
  3341. return;
  3342. for (i = 0; i < nh_grp->count; i++) {
  3343. struct mlxsw_sp_nexthop *nh = &nh_grp->nexthops[i];
  3344. nh->key.fib_nh->nh_flags &= ~RTNH_F_OFFLOAD;
  3345. }
  3346. }
  3347. static void
  3348. mlxsw_sp_fib6_entry_offload_set(struct mlxsw_sp_fib_entry *fib_entry)
  3349. {
  3350. struct mlxsw_sp_fib6_entry *fib6_entry;
  3351. struct mlxsw_sp_rt6 *mlxsw_sp_rt6;
  3352. fib6_entry = container_of(fib_entry, struct mlxsw_sp_fib6_entry,
  3353. common);
  3354. if (fib_entry->type == MLXSW_SP_FIB_ENTRY_TYPE_LOCAL) {
  3355. list_first_entry(&fib6_entry->rt6_list, struct mlxsw_sp_rt6,
  3356. list)->rt->fib6_nh.nh_flags |= RTNH_F_OFFLOAD;
  3357. return;
  3358. }
  3359. list_for_each_entry(mlxsw_sp_rt6, &fib6_entry->rt6_list, list) {
  3360. struct mlxsw_sp_nexthop_group *nh_grp = fib_entry->nh_group;
  3361. struct mlxsw_sp_nexthop *nh;
  3362. nh = mlxsw_sp_rt6_nexthop(nh_grp, mlxsw_sp_rt6);
  3363. if (nh && nh->offloaded)
  3364. mlxsw_sp_rt6->rt->fib6_nh.nh_flags |= RTNH_F_OFFLOAD;
  3365. else
  3366. mlxsw_sp_rt6->rt->fib6_nh.nh_flags &= ~RTNH_F_OFFLOAD;
  3367. }
  3368. }
  3369. static void
  3370. mlxsw_sp_fib6_entry_offload_unset(struct mlxsw_sp_fib_entry *fib_entry)
  3371. {
  3372. struct mlxsw_sp_fib6_entry *fib6_entry;
  3373. struct mlxsw_sp_rt6 *mlxsw_sp_rt6;
  3374. fib6_entry = container_of(fib_entry, struct mlxsw_sp_fib6_entry,
  3375. common);
  3376. list_for_each_entry(mlxsw_sp_rt6, &fib6_entry->rt6_list, list) {
  3377. struct fib6_info *rt = mlxsw_sp_rt6->rt;
  3378. rt->fib6_nh.nh_flags &= ~RTNH_F_OFFLOAD;
  3379. }
  3380. }
  3381. static void mlxsw_sp_fib_entry_offload_set(struct mlxsw_sp_fib_entry *fib_entry)
  3382. {
  3383. switch (fib_entry->fib_node->fib->proto) {
  3384. case MLXSW_SP_L3_PROTO_IPV4:
  3385. mlxsw_sp_fib4_entry_offload_set(fib_entry);
  3386. break;
  3387. case MLXSW_SP_L3_PROTO_IPV6:
  3388. mlxsw_sp_fib6_entry_offload_set(fib_entry);
  3389. break;
  3390. }
  3391. }
  3392. static void
  3393. mlxsw_sp_fib_entry_offload_unset(struct mlxsw_sp_fib_entry *fib_entry)
  3394. {
  3395. switch (fib_entry->fib_node->fib->proto) {
  3396. case MLXSW_SP_L3_PROTO_IPV4:
  3397. mlxsw_sp_fib4_entry_offload_unset(fib_entry);
  3398. break;
  3399. case MLXSW_SP_L3_PROTO_IPV6:
  3400. mlxsw_sp_fib6_entry_offload_unset(fib_entry);
  3401. break;
  3402. }
  3403. }
  3404. static void
  3405. mlxsw_sp_fib_entry_offload_refresh(struct mlxsw_sp_fib_entry *fib_entry,
  3406. enum mlxsw_reg_ralue_op op, int err)
  3407. {
  3408. switch (op) {
  3409. case MLXSW_REG_RALUE_OP_WRITE_DELETE:
  3410. return mlxsw_sp_fib_entry_offload_unset(fib_entry);
  3411. case MLXSW_REG_RALUE_OP_WRITE_WRITE:
  3412. if (err)
  3413. return;
  3414. if (mlxsw_sp_fib_entry_should_offload(fib_entry))
  3415. mlxsw_sp_fib_entry_offload_set(fib_entry);
  3416. else
  3417. mlxsw_sp_fib_entry_offload_unset(fib_entry);
  3418. return;
  3419. default:
  3420. return;
  3421. }
  3422. }
  3423. static void
  3424. mlxsw_sp_fib_entry_ralue_pack(char *ralue_pl,
  3425. const struct mlxsw_sp_fib_entry *fib_entry,
  3426. enum mlxsw_reg_ralue_op op)
  3427. {
  3428. struct mlxsw_sp_fib *fib = fib_entry->fib_node->fib;
  3429. enum mlxsw_reg_ralxx_protocol proto;
  3430. u32 *p_dip;
  3431. proto = (enum mlxsw_reg_ralxx_protocol) fib->proto;
  3432. switch (fib->proto) {
  3433. case MLXSW_SP_L3_PROTO_IPV4:
  3434. p_dip = (u32 *) fib_entry->fib_node->key.addr;
  3435. mlxsw_reg_ralue_pack4(ralue_pl, proto, op, fib->vr->id,
  3436. fib_entry->fib_node->key.prefix_len,
  3437. *p_dip);
  3438. break;
  3439. case MLXSW_SP_L3_PROTO_IPV6:
  3440. mlxsw_reg_ralue_pack6(ralue_pl, proto, op, fib->vr->id,
  3441. fib_entry->fib_node->key.prefix_len,
  3442. fib_entry->fib_node->key.addr);
  3443. break;
  3444. }
  3445. }
  3446. static int mlxsw_sp_fib_entry_op_remote(struct mlxsw_sp *mlxsw_sp,
  3447. struct mlxsw_sp_fib_entry *fib_entry,
  3448. enum mlxsw_reg_ralue_op op)
  3449. {
  3450. char ralue_pl[MLXSW_REG_RALUE_LEN];
  3451. enum mlxsw_reg_ralue_trap_action trap_action;
  3452. u16 trap_id = 0;
  3453. u32 adjacency_index = 0;
  3454. u16 ecmp_size = 0;
  3455. /* In case the nexthop group adjacency index is valid, use it
  3456. * with provided ECMP size. Otherwise, setup trap and pass
  3457. * traffic to kernel.
  3458. */
  3459. if (mlxsw_sp_fib_entry_should_offload(fib_entry)) {
  3460. trap_action = MLXSW_REG_RALUE_TRAP_ACTION_NOP;
  3461. adjacency_index = fib_entry->nh_group->adj_index;
  3462. ecmp_size = fib_entry->nh_group->ecmp_size;
  3463. } else {
  3464. trap_action = MLXSW_REG_RALUE_TRAP_ACTION_TRAP;
  3465. trap_id = MLXSW_TRAP_ID_RTR_INGRESS0;
  3466. }
  3467. mlxsw_sp_fib_entry_ralue_pack(ralue_pl, fib_entry, op);
  3468. mlxsw_reg_ralue_act_remote_pack(ralue_pl, trap_action, trap_id,
  3469. adjacency_index, ecmp_size);
  3470. return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue), ralue_pl);
  3471. }
  3472. static int mlxsw_sp_fib_entry_op_local(struct mlxsw_sp *mlxsw_sp,
  3473. struct mlxsw_sp_fib_entry *fib_entry,
  3474. enum mlxsw_reg_ralue_op op)
  3475. {
  3476. struct mlxsw_sp_rif *rif = fib_entry->nh_group->nh_rif;
  3477. enum mlxsw_reg_ralue_trap_action trap_action;
  3478. char ralue_pl[MLXSW_REG_RALUE_LEN];
  3479. u16 trap_id = 0;
  3480. u16 rif_index = 0;
  3481. if (mlxsw_sp_fib_entry_should_offload(fib_entry)) {
  3482. trap_action = MLXSW_REG_RALUE_TRAP_ACTION_NOP;
  3483. rif_index = rif->rif_index;
  3484. } else {
  3485. trap_action = MLXSW_REG_RALUE_TRAP_ACTION_TRAP;
  3486. trap_id = MLXSW_TRAP_ID_RTR_INGRESS0;
  3487. }
  3488. mlxsw_sp_fib_entry_ralue_pack(ralue_pl, fib_entry, op);
  3489. mlxsw_reg_ralue_act_local_pack(ralue_pl, trap_action, trap_id,
  3490. rif_index);
  3491. return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue), ralue_pl);
  3492. }
  3493. static int mlxsw_sp_fib_entry_op_trap(struct mlxsw_sp *mlxsw_sp,
  3494. struct mlxsw_sp_fib_entry *fib_entry,
  3495. enum mlxsw_reg_ralue_op op)
  3496. {
  3497. char ralue_pl[MLXSW_REG_RALUE_LEN];
  3498. mlxsw_sp_fib_entry_ralue_pack(ralue_pl, fib_entry, op);
  3499. mlxsw_reg_ralue_act_ip2me_pack(ralue_pl);
  3500. return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue), ralue_pl);
  3501. }
  3502. static int
  3503. mlxsw_sp_fib_entry_op_ipip_decap(struct mlxsw_sp *mlxsw_sp,
  3504. struct mlxsw_sp_fib_entry *fib_entry,
  3505. enum mlxsw_reg_ralue_op op)
  3506. {
  3507. struct mlxsw_sp_ipip_entry *ipip_entry = fib_entry->decap.ipip_entry;
  3508. const struct mlxsw_sp_ipip_ops *ipip_ops;
  3509. if (WARN_ON(!ipip_entry))
  3510. return -EINVAL;
  3511. ipip_ops = mlxsw_sp->router->ipip_ops_arr[ipip_entry->ipipt];
  3512. return ipip_ops->fib_entry_op(mlxsw_sp, ipip_entry, op,
  3513. fib_entry->decap.tunnel_index);
  3514. }
  3515. static int __mlxsw_sp_fib_entry_op(struct mlxsw_sp *mlxsw_sp,
  3516. struct mlxsw_sp_fib_entry *fib_entry,
  3517. enum mlxsw_reg_ralue_op op)
  3518. {
  3519. switch (fib_entry->type) {
  3520. case MLXSW_SP_FIB_ENTRY_TYPE_REMOTE:
  3521. return mlxsw_sp_fib_entry_op_remote(mlxsw_sp, fib_entry, op);
  3522. case MLXSW_SP_FIB_ENTRY_TYPE_LOCAL:
  3523. return mlxsw_sp_fib_entry_op_local(mlxsw_sp, fib_entry, op);
  3524. case MLXSW_SP_FIB_ENTRY_TYPE_TRAP:
  3525. return mlxsw_sp_fib_entry_op_trap(mlxsw_sp, fib_entry, op);
  3526. case MLXSW_SP_FIB_ENTRY_TYPE_IPIP_DECAP:
  3527. return mlxsw_sp_fib_entry_op_ipip_decap(mlxsw_sp,
  3528. fib_entry, op);
  3529. }
  3530. return -EINVAL;
  3531. }
  3532. static int mlxsw_sp_fib_entry_op(struct mlxsw_sp *mlxsw_sp,
  3533. struct mlxsw_sp_fib_entry *fib_entry,
  3534. enum mlxsw_reg_ralue_op op)
  3535. {
  3536. int err = __mlxsw_sp_fib_entry_op(mlxsw_sp, fib_entry, op);
  3537. mlxsw_sp_fib_entry_offload_refresh(fib_entry, op, err);
  3538. return err;
  3539. }
  3540. static int mlxsw_sp_fib_entry_update(struct mlxsw_sp *mlxsw_sp,
  3541. struct mlxsw_sp_fib_entry *fib_entry)
  3542. {
  3543. return mlxsw_sp_fib_entry_op(mlxsw_sp, fib_entry,
  3544. MLXSW_REG_RALUE_OP_WRITE_WRITE);
  3545. }
  3546. static int mlxsw_sp_fib_entry_del(struct mlxsw_sp *mlxsw_sp,
  3547. struct mlxsw_sp_fib_entry *fib_entry)
  3548. {
  3549. return mlxsw_sp_fib_entry_op(mlxsw_sp, fib_entry,
  3550. MLXSW_REG_RALUE_OP_WRITE_DELETE);
  3551. }
  3552. static int
  3553. mlxsw_sp_fib4_entry_type_set(struct mlxsw_sp *mlxsw_sp,
  3554. const struct fib_entry_notifier_info *fen_info,
  3555. struct mlxsw_sp_fib_entry *fib_entry)
  3556. {
  3557. union mlxsw_sp_l3addr dip = { .addr4 = htonl(fen_info->dst) };
  3558. struct net_device *dev = fen_info->fi->fib_dev;
  3559. struct mlxsw_sp_ipip_entry *ipip_entry;
  3560. struct fib_info *fi = fen_info->fi;
  3561. switch (fen_info->type) {
  3562. case RTN_LOCAL:
  3563. ipip_entry = mlxsw_sp_ipip_entry_find_by_decap(mlxsw_sp, dev,
  3564. MLXSW_SP_L3_PROTO_IPV4, dip);
  3565. if (ipip_entry && ipip_entry->ol_dev->flags & IFF_UP) {
  3566. fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_IPIP_DECAP;
  3567. return mlxsw_sp_fib_entry_decap_init(mlxsw_sp,
  3568. fib_entry,
  3569. ipip_entry);
  3570. }
  3571. /* fall through */
  3572. case RTN_BROADCAST:
  3573. fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_TRAP;
  3574. return 0;
  3575. case RTN_UNREACHABLE: /* fall through */
  3576. case RTN_BLACKHOLE: /* fall through */
  3577. case RTN_PROHIBIT:
  3578. /* Packets hitting these routes need to be trapped, but
  3579. * can do so with a lower priority than packets directed
  3580. * at the host, so use action type local instead of trap.
  3581. */
  3582. fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_LOCAL;
  3583. return 0;
  3584. case RTN_UNICAST:
  3585. if (mlxsw_sp_fi_is_gateway(mlxsw_sp, fi))
  3586. fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_REMOTE;
  3587. else
  3588. fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_LOCAL;
  3589. return 0;
  3590. default:
  3591. return -EINVAL;
  3592. }
  3593. }
  3594. static struct mlxsw_sp_fib4_entry *
  3595. mlxsw_sp_fib4_entry_create(struct mlxsw_sp *mlxsw_sp,
  3596. struct mlxsw_sp_fib_node *fib_node,
  3597. const struct fib_entry_notifier_info *fen_info)
  3598. {
  3599. struct mlxsw_sp_fib4_entry *fib4_entry;
  3600. struct mlxsw_sp_fib_entry *fib_entry;
  3601. int err;
  3602. fib4_entry = kzalloc(sizeof(*fib4_entry), GFP_KERNEL);
  3603. if (!fib4_entry)
  3604. return ERR_PTR(-ENOMEM);
  3605. fib_entry = &fib4_entry->common;
  3606. err = mlxsw_sp_fib4_entry_type_set(mlxsw_sp, fen_info, fib_entry);
  3607. if (err)
  3608. goto err_fib4_entry_type_set;
  3609. err = mlxsw_sp_nexthop4_group_get(mlxsw_sp, fib_entry, fen_info->fi);
  3610. if (err)
  3611. goto err_nexthop4_group_get;
  3612. fib4_entry->prio = fen_info->fi->fib_priority;
  3613. fib4_entry->tb_id = fen_info->tb_id;
  3614. fib4_entry->type = fen_info->type;
  3615. fib4_entry->tos = fen_info->tos;
  3616. fib_entry->fib_node = fib_node;
  3617. return fib4_entry;
  3618. err_nexthop4_group_get:
  3619. err_fib4_entry_type_set:
  3620. kfree(fib4_entry);
  3621. return ERR_PTR(err);
  3622. }
  3623. static void mlxsw_sp_fib4_entry_destroy(struct mlxsw_sp *mlxsw_sp,
  3624. struct mlxsw_sp_fib4_entry *fib4_entry)
  3625. {
  3626. mlxsw_sp_nexthop4_group_put(mlxsw_sp, &fib4_entry->common);
  3627. kfree(fib4_entry);
  3628. }
  3629. static struct mlxsw_sp_fib4_entry *
  3630. mlxsw_sp_fib4_entry_lookup(struct mlxsw_sp *mlxsw_sp,
  3631. const struct fib_entry_notifier_info *fen_info)
  3632. {
  3633. struct mlxsw_sp_fib4_entry *fib4_entry;
  3634. struct mlxsw_sp_fib_node *fib_node;
  3635. struct mlxsw_sp_fib *fib;
  3636. struct mlxsw_sp_vr *vr;
  3637. vr = mlxsw_sp_vr_find(mlxsw_sp, fen_info->tb_id);
  3638. if (!vr)
  3639. return NULL;
  3640. fib = mlxsw_sp_vr_fib(vr, MLXSW_SP_L3_PROTO_IPV4);
  3641. fib_node = mlxsw_sp_fib_node_lookup(fib, &fen_info->dst,
  3642. sizeof(fen_info->dst),
  3643. fen_info->dst_len);
  3644. if (!fib_node)
  3645. return NULL;
  3646. list_for_each_entry(fib4_entry, &fib_node->entry_list, common.list) {
  3647. if (fib4_entry->tb_id == fen_info->tb_id &&
  3648. fib4_entry->tos == fen_info->tos &&
  3649. fib4_entry->type == fen_info->type &&
  3650. mlxsw_sp_nexthop4_group_fi(fib4_entry->common.nh_group) ==
  3651. fen_info->fi) {
  3652. return fib4_entry;
  3653. }
  3654. }
  3655. return NULL;
  3656. }
  3657. static const struct rhashtable_params mlxsw_sp_fib_ht_params = {
  3658. .key_offset = offsetof(struct mlxsw_sp_fib_node, key),
  3659. .head_offset = offsetof(struct mlxsw_sp_fib_node, ht_node),
  3660. .key_len = sizeof(struct mlxsw_sp_fib_key),
  3661. .automatic_shrinking = true,
  3662. };
  3663. static int mlxsw_sp_fib_node_insert(struct mlxsw_sp_fib *fib,
  3664. struct mlxsw_sp_fib_node *fib_node)
  3665. {
  3666. return rhashtable_insert_fast(&fib->ht, &fib_node->ht_node,
  3667. mlxsw_sp_fib_ht_params);
  3668. }
  3669. static void mlxsw_sp_fib_node_remove(struct mlxsw_sp_fib *fib,
  3670. struct mlxsw_sp_fib_node *fib_node)
  3671. {
  3672. rhashtable_remove_fast(&fib->ht, &fib_node->ht_node,
  3673. mlxsw_sp_fib_ht_params);
  3674. }
  3675. static struct mlxsw_sp_fib_node *
  3676. mlxsw_sp_fib_node_lookup(struct mlxsw_sp_fib *fib, const void *addr,
  3677. size_t addr_len, unsigned char prefix_len)
  3678. {
  3679. struct mlxsw_sp_fib_key key;
  3680. memset(&key, 0, sizeof(key));
  3681. memcpy(key.addr, addr, addr_len);
  3682. key.prefix_len = prefix_len;
  3683. return rhashtable_lookup_fast(&fib->ht, &key, mlxsw_sp_fib_ht_params);
  3684. }
  3685. static struct mlxsw_sp_fib_node *
  3686. mlxsw_sp_fib_node_create(struct mlxsw_sp_fib *fib, const void *addr,
  3687. size_t addr_len, unsigned char prefix_len)
  3688. {
  3689. struct mlxsw_sp_fib_node *fib_node;
  3690. fib_node = kzalloc(sizeof(*fib_node), GFP_KERNEL);
  3691. if (!fib_node)
  3692. return NULL;
  3693. INIT_LIST_HEAD(&fib_node->entry_list);
  3694. list_add(&fib_node->list, &fib->node_list);
  3695. memcpy(fib_node->key.addr, addr, addr_len);
  3696. fib_node->key.prefix_len = prefix_len;
  3697. return fib_node;
  3698. }
  3699. static void mlxsw_sp_fib_node_destroy(struct mlxsw_sp_fib_node *fib_node)
  3700. {
  3701. list_del(&fib_node->list);
  3702. WARN_ON(!list_empty(&fib_node->entry_list));
  3703. kfree(fib_node);
  3704. }
  3705. static bool
  3706. mlxsw_sp_fib_node_entry_is_first(const struct mlxsw_sp_fib_node *fib_node,
  3707. const struct mlxsw_sp_fib_entry *fib_entry)
  3708. {
  3709. return list_first_entry(&fib_node->entry_list,
  3710. struct mlxsw_sp_fib_entry, list) == fib_entry;
  3711. }
  3712. static int mlxsw_sp_fib_lpm_tree_link(struct mlxsw_sp *mlxsw_sp,
  3713. struct mlxsw_sp_fib_node *fib_node)
  3714. {
  3715. struct mlxsw_sp_prefix_usage req_prefix_usage;
  3716. struct mlxsw_sp_fib *fib = fib_node->fib;
  3717. struct mlxsw_sp_lpm_tree *lpm_tree;
  3718. int err;
  3719. lpm_tree = mlxsw_sp->router->lpm.proto_trees[fib->proto];
  3720. if (lpm_tree->prefix_ref_count[fib_node->key.prefix_len] != 0)
  3721. goto out;
  3722. mlxsw_sp_prefix_usage_cpy(&req_prefix_usage, &lpm_tree->prefix_usage);
  3723. mlxsw_sp_prefix_usage_set(&req_prefix_usage, fib_node->key.prefix_len);
  3724. lpm_tree = mlxsw_sp_lpm_tree_get(mlxsw_sp, &req_prefix_usage,
  3725. fib->proto);
  3726. if (IS_ERR(lpm_tree))
  3727. return PTR_ERR(lpm_tree);
  3728. err = mlxsw_sp_vrs_lpm_tree_replace(mlxsw_sp, fib, lpm_tree);
  3729. if (err)
  3730. goto err_lpm_tree_replace;
  3731. out:
  3732. lpm_tree->prefix_ref_count[fib_node->key.prefix_len]++;
  3733. return 0;
  3734. err_lpm_tree_replace:
  3735. mlxsw_sp_lpm_tree_put(mlxsw_sp, lpm_tree);
  3736. return err;
  3737. }
  3738. static void mlxsw_sp_fib_lpm_tree_unlink(struct mlxsw_sp *mlxsw_sp,
  3739. struct mlxsw_sp_fib_node *fib_node)
  3740. {
  3741. struct mlxsw_sp_lpm_tree *lpm_tree = fib_node->fib->lpm_tree;
  3742. struct mlxsw_sp_prefix_usage req_prefix_usage;
  3743. struct mlxsw_sp_fib *fib = fib_node->fib;
  3744. int err;
  3745. if (--lpm_tree->prefix_ref_count[fib_node->key.prefix_len] != 0)
  3746. return;
  3747. /* Try to construct a new LPM tree from the current prefix usage
  3748. * minus the unused one. If we fail, continue using the old one.
  3749. */
  3750. mlxsw_sp_prefix_usage_cpy(&req_prefix_usage, &lpm_tree->prefix_usage);
  3751. mlxsw_sp_prefix_usage_clear(&req_prefix_usage,
  3752. fib_node->key.prefix_len);
  3753. lpm_tree = mlxsw_sp_lpm_tree_get(mlxsw_sp, &req_prefix_usage,
  3754. fib->proto);
  3755. if (IS_ERR(lpm_tree))
  3756. return;
  3757. err = mlxsw_sp_vrs_lpm_tree_replace(mlxsw_sp, fib, lpm_tree);
  3758. if (err)
  3759. goto err_lpm_tree_replace;
  3760. return;
  3761. err_lpm_tree_replace:
  3762. mlxsw_sp_lpm_tree_put(mlxsw_sp, lpm_tree);
  3763. }
  3764. static int mlxsw_sp_fib_node_init(struct mlxsw_sp *mlxsw_sp,
  3765. struct mlxsw_sp_fib_node *fib_node,
  3766. struct mlxsw_sp_fib *fib)
  3767. {
  3768. int err;
  3769. err = mlxsw_sp_fib_node_insert(fib, fib_node);
  3770. if (err)
  3771. return err;
  3772. fib_node->fib = fib;
  3773. err = mlxsw_sp_fib_lpm_tree_link(mlxsw_sp, fib_node);
  3774. if (err)
  3775. goto err_fib_lpm_tree_link;
  3776. return 0;
  3777. err_fib_lpm_tree_link:
  3778. fib_node->fib = NULL;
  3779. mlxsw_sp_fib_node_remove(fib, fib_node);
  3780. return err;
  3781. }
  3782. static void mlxsw_sp_fib_node_fini(struct mlxsw_sp *mlxsw_sp,
  3783. struct mlxsw_sp_fib_node *fib_node)
  3784. {
  3785. struct mlxsw_sp_fib *fib = fib_node->fib;
  3786. mlxsw_sp_fib_lpm_tree_unlink(mlxsw_sp, fib_node);
  3787. fib_node->fib = NULL;
  3788. mlxsw_sp_fib_node_remove(fib, fib_node);
  3789. }
  3790. static struct mlxsw_sp_fib_node *
  3791. mlxsw_sp_fib_node_get(struct mlxsw_sp *mlxsw_sp, u32 tb_id, const void *addr,
  3792. size_t addr_len, unsigned char prefix_len,
  3793. enum mlxsw_sp_l3proto proto)
  3794. {
  3795. struct mlxsw_sp_fib_node *fib_node;
  3796. struct mlxsw_sp_fib *fib;
  3797. struct mlxsw_sp_vr *vr;
  3798. int err;
  3799. vr = mlxsw_sp_vr_get(mlxsw_sp, tb_id, NULL);
  3800. if (IS_ERR(vr))
  3801. return ERR_CAST(vr);
  3802. fib = mlxsw_sp_vr_fib(vr, proto);
  3803. fib_node = mlxsw_sp_fib_node_lookup(fib, addr, addr_len, prefix_len);
  3804. if (fib_node)
  3805. return fib_node;
  3806. fib_node = mlxsw_sp_fib_node_create(fib, addr, addr_len, prefix_len);
  3807. if (!fib_node) {
  3808. err = -ENOMEM;
  3809. goto err_fib_node_create;
  3810. }
  3811. err = mlxsw_sp_fib_node_init(mlxsw_sp, fib_node, fib);
  3812. if (err)
  3813. goto err_fib_node_init;
  3814. return fib_node;
  3815. err_fib_node_init:
  3816. mlxsw_sp_fib_node_destroy(fib_node);
  3817. err_fib_node_create:
  3818. mlxsw_sp_vr_put(mlxsw_sp, vr);
  3819. return ERR_PTR(err);
  3820. }
  3821. static void mlxsw_sp_fib_node_put(struct mlxsw_sp *mlxsw_sp,
  3822. struct mlxsw_sp_fib_node *fib_node)
  3823. {
  3824. struct mlxsw_sp_vr *vr = fib_node->fib->vr;
  3825. if (!list_empty(&fib_node->entry_list))
  3826. return;
  3827. mlxsw_sp_fib_node_fini(mlxsw_sp, fib_node);
  3828. mlxsw_sp_fib_node_destroy(fib_node);
  3829. mlxsw_sp_vr_put(mlxsw_sp, vr);
  3830. }
  3831. static struct mlxsw_sp_fib4_entry *
  3832. mlxsw_sp_fib4_node_entry_find(const struct mlxsw_sp_fib_node *fib_node,
  3833. const struct mlxsw_sp_fib4_entry *new4_entry)
  3834. {
  3835. struct mlxsw_sp_fib4_entry *fib4_entry;
  3836. list_for_each_entry(fib4_entry, &fib_node->entry_list, common.list) {
  3837. if (fib4_entry->tb_id > new4_entry->tb_id)
  3838. continue;
  3839. if (fib4_entry->tb_id != new4_entry->tb_id)
  3840. break;
  3841. if (fib4_entry->tos > new4_entry->tos)
  3842. continue;
  3843. if (fib4_entry->prio >= new4_entry->prio ||
  3844. fib4_entry->tos < new4_entry->tos)
  3845. return fib4_entry;
  3846. }
  3847. return NULL;
  3848. }
  3849. static int
  3850. mlxsw_sp_fib4_node_list_append(struct mlxsw_sp_fib4_entry *fib4_entry,
  3851. struct mlxsw_sp_fib4_entry *new4_entry)
  3852. {
  3853. struct mlxsw_sp_fib_node *fib_node;
  3854. if (WARN_ON(!fib4_entry))
  3855. return -EINVAL;
  3856. fib_node = fib4_entry->common.fib_node;
  3857. list_for_each_entry_from(fib4_entry, &fib_node->entry_list,
  3858. common.list) {
  3859. if (fib4_entry->tb_id != new4_entry->tb_id ||
  3860. fib4_entry->tos != new4_entry->tos ||
  3861. fib4_entry->prio != new4_entry->prio)
  3862. break;
  3863. }
  3864. list_add_tail(&new4_entry->common.list, &fib4_entry->common.list);
  3865. return 0;
  3866. }
  3867. static int
  3868. mlxsw_sp_fib4_node_list_insert(struct mlxsw_sp_fib4_entry *new4_entry,
  3869. bool replace, bool append)
  3870. {
  3871. struct mlxsw_sp_fib_node *fib_node = new4_entry->common.fib_node;
  3872. struct mlxsw_sp_fib4_entry *fib4_entry;
  3873. fib4_entry = mlxsw_sp_fib4_node_entry_find(fib_node, new4_entry);
  3874. if (append)
  3875. return mlxsw_sp_fib4_node_list_append(fib4_entry, new4_entry);
  3876. if (replace && WARN_ON(!fib4_entry))
  3877. return -EINVAL;
  3878. /* Insert new entry before replaced one, so that we can later
  3879. * remove the second.
  3880. */
  3881. if (fib4_entry) {
  3882. list_add_tail(&new4_entry->common.list,
  3883. &fib4_entry->common.list);
  3884. } else {
  3885. struct mlxsw_sp_fib4_entry *last;
  3886. list_for_each_entry(last, &fib_node->entry_list, common.list) {
  3887. if (new4_entry->tb_id > last->tb_id)
  3888. break;
  3889. fib4_entry = last;
  3890. }
  3891. if (fib4_entry)
  3892. list_add(&new4_entry->common.list,
  3893. &fib4_entry->common.list);
  3894. else
  3895. list_add(&new4_entry->common.list,
  3896. &fib_node->entry_list);
  3897. }
  3898. return 0;
  3899. }
  3900. static void
  3901. mlxsw_sp_fib4_node_list_remove(struct mlxsw_sp_fib4_entry *fib4_entry)
  3902. {
  3903. list_del(&fib4_entry->common.list);
  3904. }
  3905. static int mlxsw_sp_fib_node_entry_add(struct mlxsw_sp *mlxsw_sp,
  3906. struct mlxsw_sp_fib_entry *fib_entry)
  3907. {
  3908. struct mlxsw_sp_fib_node *fib_node = fib_entry->fib_node;
  3909. if (!mlxsw_sp_fib_node_entry_is_first(fib_node, fib_entry))
  3910. return 0;
  3911. /* To prevent packet loss, overwrite the previously offloaded
  3912. * entry.
  3913. */
  3914. if (!list_is_singular(&fib_node->entry_list)) {
  3915. enum mlxsw_reg_ralue_op op = MLXSW_REG_RALUE_OP_WRITE_DELETE;
  3916. struct mlxsw_sp_fib_entry *n = list_next_entry(fib_entry, list);
  3917. mlxsw_sp_fib_entry_offload_refresh(n, op, 0);
  3918. }
  3919. return mlxsw_sp_fib_entry_update(mlxsw_sp, fib_entry);
  3920. }
  3921. static void mlxsw_sp_fib_node_entry_del(struct mlxsw_sp *mlxsw_sp,
  3922. struct mlxsw_sp_fib_entry *fib_entry)
  3923. {
  3924. struct mlxsw_sp_fib_node *fib_node = fib_entry->fib_node;
  3925. if (!mlxsw_sp_fib_node_entry_is_first(fib_node, fib_entry))
  3926. return;
  3927. /* Promote the next entry by overwriting the deleted entry */
  3928. if (!list_is_singular(&fib_node->entry_list)) {
  3929. struct mlxsw_sp_fib_entry *n = list_next_entry(fib_entry, list);
  3930. enum mlxsw_reg_ralue_op op = MLXSW_REG_RALUE_OP_WRITE_DELETE;
  3931. mlxsw_sp_fib_entry_update(mlxsw_sp, n);
  3932. mlxsw_sp_fib_entry_offload_refresh(fib_entry, op, 0);
  3933. return;
  3934. }
  3935. mlxsw_sp_fib_entry_del(mlxsw_sp, fib_entry);
  3936. }
  3937. static int mlxsw_sp_fib4_node_entry_link(struct mlxsw_sp *mlxsw_sp,
  3938. struct mlxsw_sp_fib4_entry *fib4_entry,
  3939. bool replace, bool append)
  3940. {
  3941. int err;
  3942. err = mlxsw_sp_fib4_node_list_insert(fib4_entry, replace, append);
  3943. if (err)
  3944. return err;
  3945. err = mlxsw_sp_fib_node_entry_add(mlxsw_sp, &fib4_entry->common);
  3946. if (err)
  3947. goto err_fib_node_entry_add;
  3948. return 0;
  3949. err_fib_node_entry_add:
  3950. mlxsw_sp_fib4_node_list_remove(fib4_entry);
  3951. return err;
  3952. }
  3953. static void
  3954. mlxsw_sp_fib4_node_entry_unlink(struct mlxsw_sp *mlxsw_sp,
  3955. struct mlxsw_sp_fib4_entry *fib4_entry)
  3956. {
  3957. mlxsw_sp_fib_node_entry_del(mlxsw_sp, &fib4_entry->common);
  3958. mlxsw_sp_fib4_node_list_remove(fib4_entry);
  3959. if (fib4_entry->common.type == MLXSW_SP_FIB_ENTRY_TYPE_IPIP_DECAP)
  3960. mlxsw_sp_fib_entry_decap_fini(mlxsw_sp, &fib4_entry->common);
  3961. }
  3962. static void mlxsw_sp_fib4_entry_replace(struct mlxsw_sp *mlxsw_sp,
  3963. struct mlxsw_sp_fib4_entry *fib4_entry,
  3964. bool replace)
  3965. {
  3966. struct mlxsw_sp_fib_node *fib_node = fib4_entry->common.fib_node;
  3967. struct mlxsw_sp_fib4_entry *replaced;
  3968. if (!replace)
  3969. return;
  3970. /* We inserted the new entry before replaced one */
  3971. replaced = list_next_entry(fib4_entry, common.list);
  3972. mlxsw_sp_fib4_node_entry_unlink(mlxsw_sp, replaced);
  3973. mlxsw_sp_fib4_entry_destroy(mlxsw_sp, replaced);
  3974. mlxsw_sp_fib_node_put(mlxsw_sp, fib_node);
  3975. }
  3976. static int
  3977. mlxsw_sp_router_fib4_add(struct mlxsw_sp *mlxsw_sp,
  3978. const struct fib_entry_notifier_info *fen_info,
  3979. bool replace, bool append)
  3980. {
  3981. struct mlxsw_sp_fib4_entry *fib4_entry;
  3982. struct mlxsw_sp_fib_node *fib_node;
  3983. int err;
  3984. if (mlxsw_sp->router->aborted)
  3985. return 0;
  3986. fib_node = mlxsw_sp_fib_node_get(mlxsw_sp, fen_info->tb_id,
  3987. &fen_info->dst, sizeof(fen_info->dst),
  3988. fen_info->dst_len,
  3989. MLXSW_SP_L3_PROTO_IPV4);
  3990. if (IS_ERR(fib_node)) {
  3991. dev_warn(mlxsw_sp->bus_info->dev, "Failed to get FIB node\n");
  3992. return PTR_ERR(fib_node);
  3993. }
  3994. fib4_entry = mlxsw_sp_fib4_entry_create(mlxsw_sp, fib_node, fen_info);
  3995. if (IS_ERR(fib4_entry)) {
  3996. dev_warn(mlxsw_sp->bus_info->dev, "Failed to create FIB entry\n");
  3997. err = PTR_ERR(fib4_entry);
  3998. goto err_fib4_entry_create;
  3999. }
  4000. err = mlxsw_sp_fib4_node_entry_link(mlxsw_sp, fib4_entry, replace,
  4001. append);
  4002. if (err) {
  4003. dev_warn(mlxsw_sp->bus_info->dev, "Failed to link FIB entry to node\n");
  4004. goto err_fib4_node_entry_link;
  4005. }
  4006. mlxsw_sp_fib4_entry_replace(mlxsw_sp, fib4_entry, replace);
  4007. return 0;
  4008. err_fib4_node_entry_link:
  4009. mlxsw_sp_fib4_entry_destroy(mlxsw_sp, fib4_entry);
  4010. err_fib4_entry_create:
  4011. mlxsw_sp_fib_node_put(mlxsw_sp, fib_node);
  4012. return err;
  4013. }
  4014. static void mlxsw_sp_router_fib4_del(struct mlxsw_sp *mlxsw_sp,
  4015. struct fib_entry_notifier_info *fen_info)
  4016. {
  4017. struct mlxsw_sp_fib4_entry *fib4_entry;
  4018. struct mlxsw_sp_fib_node *fib_node;
  4019. if (mlxsw_sp->router->aborted)
  4020. return;
  4021. fib4_entry = mlxsw_sp_fib4_entry_lookup(mlxsw_sp, fen_info);
  4022. if (WARN_ON(!fib4_entry))
  4023. return;
  4024. fib_node = fib4_entry->common.fib_node;
  4025. mlxsw_sp_fib4_node_entry_unlink(mlxsw_sp, fib4_entry);
  4026. mlxsw_sp_fib4_entry_destroy(mlxsw_sp, fib4_entry);
  4027. mlxsw_sp_fib_node_put(mlxsw_sp, fib_node);
  4028. }
  4029. static bool mlxsw_sp_fib6_rt_should_ignore(const struct fib6_info *rt)
  4030. {
  4031. /* Packets with link-local destination IP arriving to the router
  4032. * are trapped to the CPU, so no need to program specific routes
  4033. * for them.
  4034. */
  4035. if (ipv6_addr_type(&rt->fib6_dst.addr) & IPV6_ADDR_LINKLOCAL)
  4036. return true;
  4037. /* Multicast routes aren't supported, so ignore them. Neighbour
  4038. * Discovery packets are specifically trapped.
  4039. */
  4040. if (ipv6_addr_type(&rt->fib6_dst.addr) & IPV6_ADDR_MULTICAST)
  4041. return true;
  4042. /* Cloned routes are irrelevant in the forwarding path. */
  4043. if (rt->fib6_flags & RTF_CACHE)
  4044. return true;
  4045. return false;
  4046. }
  4047. static struct mlxsw_sp_rt6 *mlxsw_sp_rt6_create(struct fib6_info *rt)
  4048. {
  4049. struct mlxsw_sp_rt6 *mlxsw_sp_rt6;
  4050. mlxsw_sp_rt6 = kzalloc(sizeof(*mlxsw_sp_rt6), GFP_KERNEL);
  4051. if (!mlxsw_sp_rt6)
  4052. return ERR_PTR(-ENOMEM);
  4053. /* In case of route replace, replaced route is deleted with
  4054. * no notification. Take reference to prevent accessing freed
  4055. * memory.
  4056. */
  4057. mlxsw_sp_rt6->rt = rt;
  4058. fib6_info_hold(rt);
  4059. return mlxsw_sp_rt6;
  4060. }
  4061. #if IS_ENABLED(CONFIG_IPV6)
  4062. static void mlxsw_sp_rt6_release(struct fib6_info *rt)
  4063. {
  4064. fib6_info_release(rt);
  4065. }
  4066. #else
  4067. static void mlxsw_sp_rt6_release(struct fib6_info *rt)
  4068. {
  4069. }
  4070. #endif
  4071. static void mlxsw_sp_rt6_destroy(struct mlxsw_sp_rt6 *mlxsw_sp_rt6)
  4072. {
  4073. mlxsw_sp_rt6_release(mlxsw_sp_rt6->rt);
  4074. kfree(mlxsw_sp_rt6);
  4075. }
  4076. static bool mlxsw_sp_fib6_rt_can_mp(const struct fib6_info *rt)
  4077. {
  4078. /* RTF_CACHE routes are ignored */
  4079. return (rt->fib6_flags & (RTF_GATEWAY | RTF_ADDRCONF)) == RTF_GATEWAY;
  4080. }
  4081. static struct fib6_info *
  4082. mlxsw_sp_fib6_entry_rt(const struct mlxsw_sp_fib6_entry *fib6_entry)
  4083. {
  4084. return list_first_entry(&fib6_entry->rt6_list, struct mlxsw_sp_rt6,
  4085. list)->rt;
  4086. }
  4087. static struct mlxsw_sp_fib6_entry *
  4088. mlxsw_sp_fib6_node_mp_entry_find(const struct mlxsw_sp_fib_node *fib_node,
  4089. const struct fib6_info *nrt, bool replace)
  4090. {
  4091. struct mlxsw_sp_fib6_entry *fib6_entry;
  4092. if (!mlxsw_sp_fib6_rt_can_mp(nrt) || replace)
  4093. return NULL;
  4094. list_for_each_entry(fib6_entry, &fib_node->entry_list, common.list) {
  4095. struct fib6_info *rt = mlxsw_sp_fib6_entry_rt(fib6_entry);
  4096. /* RT6_TABLE_LOCAL and RT6_TABLE_MAIN share the same
  4097. * virtual router.
  4098. */
  4099. if (rt->fib6_table->tb6_id > nrt->fib6_table->tb6_id)
  4100. continue;
  4101. if (rt->fib6_table->tb6_id != nrt->fib6_table->tb6_id)
  4102. break;
  4103. if (rt->fib6_metric < nrt->fib6_metric)
  4104. continue;
  4105. if (rt->fib6_metric == nrt->fib6_metric &&
  4106. mlxsw_sp_fib6_rt_can_mp(rt))
  4107. return fib6_entry;
  4108. if (rt->fib6_metric > nrt->fib6_metric)
  4109. break;
  4110. }
  4111. return NULL;
  4112. }
  4113. static struct mlxsw_sp_rt6 *
  4114. mlxsw_sp_fib6_entry_rt_find(const struct mlxsw_sp_fib6_entry *fib6_entry,
  4115. const struct fib6_info *rt)
  4116. {
  4117. struct mlxsw_sp_rt6 *mlxsw_sp_rt6;
  4118. list_for_each_entry(mlxsw_sp_rt6, &fib6_entry->rt6_list, list) {
  4119. if (mlxsw_sp_rt6->rt == rt)
  4120. return mlxsw_sp_rt6;
  4121. }
  4122. return NULL;
  4123. }
  4124. static bool mlxsw_sp_nexthop6_ipip_type(const struct mlxsw_sp *mlxsw_sp,
  4125. const struct fib6_info *rt,
  4126. enum mlxsw_sp_ipip_type *ret)
  4127. {
  4128. return rt->fib6_nh.nh_dev &&
  4129. mlxsw_sp_netdev_ipip_type(mlxsw_sp, rt->fib6_nh.nh_dev, ret);
  4130. }
  4131. static int mlxsw_sp_nexthop6_type_init(struct mlxsw_sp *mlxsw_sp,
  4132. struct mlxsw_sp_nexthop_group *nh_grp,
  4133. struct mlxsw_sp_nexthop *nh,
  4134. const struct fib6_info *rt)
  4135. {
  4136. const struct mlxsw_sp_ipip_ops *ipip_ops;
  4137. struct mlxsw_sp_ipip_entry *ipip_entry;
  4138. struct net_device *dev = rt->fib6_nh.nh_dev;
  4139. struct mlxsw_sp_rif *rif;
  4140. int err;
  4141. ipip_entry = mlxsw_sp_ipip_entry_find_by_ol_dev(mlxsw_sp, dev);
  4142. if (ipip_entry) {
  4143. ipip_ops = mlxsw_sp->router->ipip_ops_arr[ipip_entry->ipipt];
  4144. if (ipip_ops->can_offload(mlxsw_sp, dev,
  4145. MLXSW_SP_L3_PROTO_IPV6)) {
  4146. nh->type = MLXSW_SP_NEXTHOP_TYPE_IPIP;
  4147. mlxsw_sp_nexthop_ipip_init(mlxsw_sp, nh, ipip_entry);
  4148. return 0;
  4149. }
  4150. }
  4151. nh->type = MLXSW_SP_NEXTHOP_TYPE_ETH;
  4152. rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
  4153. if (!rif)
  4154. return 0;
  4155. mlxsw_sp_nexthop_rif_init(nh, rif);
  4156. err = mlxsw_sp_nexthop_neigh_init(mlxsw_sp, nh);
  4157. if (err)
  4158. goto err_nexthop_neigh_init;
  4159. return 0;
  4160. err_nexthop_neigh_init:
  4161. mlxsw_sp_nexthop_rif_fini(nh);
  4162. return err;
  4163. }
  4164. static void mlxsw_sp_nexthop6_type_fini(struct mlxsw_sp *mlxsw_sp,
  4165. struct mlxsw_sp_nexthop *nh)
  4166. {
  4167. mlxsw_sp_nexthop_type_fini(mlxsw_sp, nh);
  4168. }
  4169. static int mlxsw_sp_nexthop6_init(struct mlxsw_sp *mlxsw_sp,
  4170. struct mlxsw_sp_nexthop_group *nh_grp,
  4171. struct mlxsw_sp_nexthop *nh,
  4172. const struct fib6_info *rt)
  4173. {
  4174. struct net_device *dev = rt->fib6_nh.nh_dev;
  4175. nh->nh_grp = nh_grp;
  4176. nh->nh_weight = rt->fib6_nh.nh_weight;
  4177. memcpy(&nh->gw_addr, &rt->fib6_nh.nh_gw, sizeof(nh->gw_addr));
  4178. mlxsw_sp_nexthop_counter_alloc(mlxsw_sp, nh);
  4179. list_add_tail(&nh->router_list_node, &mlxsw_sp->router->nexthop_list);
  4180. if (!dev)
  4181. return 0;
  4182. nh->ifindex = dev->ifindex;
  4183. return mlxsw_sp_nexthop6_type_init(mlxsw_sp, nh_grp, nh, rt);
  4184. }
  4185. static void mlxsw_sp_nexthop6_fini(struct mlxsw_sp *mlxsw_sp,
  4186. struct mlxsw_sp_nexthop *nh)
  4187. {
  4188. mlxsw_sp_nexthop6_type_fini(mlxsw_sp, nh);
  4189. list_del(&nh->router_list_node);
  4190. mlxsw_sp_nexthop_counter_free(mlxsw_sp, nh);
  4191. }
  4192. static bool mlxsw_sp_rt6_is_gateway(const struct mlxsw_sp *mlxsw_sp,
  4193. const struct fib6_info *rt)
  4194. {
  4195. return rt->fib6_flags & RTF_GATEWAY ||
  4196. mlxsw_sp_nexthop6_ipip_type(mlxsw_sp, rt, NULL);
  4197. }
  4198. static struct mlxsw_sp_nexthop_group *
  4199. mlxsw_sp_nexthop6_group_create(struct mlxsw_sp *mlxsw_sp,
  4200. struct mlxsw_sp_fib6_entry *fib6_entry)
  4201. {
  4202. struct mlxsw_sp_nexthop_group *nh_grp;
  4203. struct mlxsw_sp_rt6 *mlxsw_sp_rt6;
  4204. struct mlxsw_sp_nexthop *nh;
  4205. size_t alloc_size;
  4206. int i = 0;
  4207. int err;
  4208. alloc_size = sizeof(*nh_grp) +
  4209. fib6_entry->nrt6 * sizeof(struct mlxsw_sp_nexthop);
  4210. nh_grp = kzalloc(alloc_size, GFP_KERNEL);
  4211. if (!nh_grp)
  4212. return ERR_PTR(-ENOMEM);
  4213. INIT_LIST_HEAD(&nh_grp->fib_list);
  4214. #if IS_ENABLED(CONFIG_IPV6)
  4215. nh_grp->neigh_tbl = &nd_tbl;
  4216. #endif
  4217. mlxsw_sp_rt6 = list_first_entry(&fib6_entry->rt6_list,
  4218. struct mlxsw_sp_rt6, list);
  4219. nh_grp->gateway = mlxsw_sp_rt6_is_gateway(mlxsw_sp, mlxsw_sp_rt6->rt);
  4220. nh_grp->count = fib6_entry->nrt6;
  4221. for (i = 0; i < nh_grp->count; i++) {
  4222. struct fib6_info *rt = mlxsw_sp_rt6->rt;
  4223. nh = &nh_grp->nexthops[i];
  4224. err = mlxsw_sp_nexthop6_init(mlxsw_sp, nh_grp, nh, rt);
  4225. if (err)
  4226. goto err_nexthop6_init;
  4227. mlxsw_sp_rt6 = list_next_entry(mlxsw_sp_rt6, list);
  4228. }
  4229. err = mlxsw_sp_nexthop_group_insert(mlxsw_sp, nh_grp);
  4230. if (err)
  4231. goto err_nexthop_group_insert;
  4232. mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh_grp);
  4233. return nh_grp;
  4234. err_nexthop_group_insert:
  4235. err_nexthop6_init:
  4236. for (i--; i >= 0; i--) {
  4237. nh = &nh_grp->nexthops[i];
  4238. mlxsw_sp_nexthop6_fini(mlxsw_sp, nh);
  4239. }
  4240. kfree(nh_grp);
  4241. return ERR_PTR(err);
  4242. }
  4243. static void
  4244. mlxsw_sp_nexthop6_group_destroy(struct mlxsw_sp *mlxsw_sp,
  4245. struct mlxsw_sp_nexthop_group *nh_grp)
  4246. {
  4247. struct mlxsw_sp_nexthop *nh;
  4248. int i = nh_grp->count;
  4249. mlxsw_sp_nexthop_group_remove(mlxsw_sp, nh_grp);
  4250. for (i--; i >= 0; i--) {
  4251. nh = &nh_grp->nexthops[i];
  4252. mlxsw_sp_nexthop6_fini(mlxsw_sp, nh);
  4253. }
  4254. mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh_grp);
  4255. WARN_ON(nh_grp->adj_index_valid);
  4256. kfree(nh_grp);
  4257. }
  4258. static int mlxsw_sp_nexthop6_group_get(struct mlxsw_sp *mlxsw_sp,
  4259. struct mlxsw_sp_fib6_entry *fib6_entry)
  4260. {
  4261. struct mlxsw_sp_nexthop_group *nh_grp;
  4262. nh_grp = mlxsw_sp_nexthop6_group_lookup(mlxsw_sp, fib6_entry);
  4263. if (!nh_grp) {
  4264. nh_grp = mlxsw_sp_nexthop6_group_create(mlxsw_sp, fib6_entry);
  4265. if (IS_ERR(nh_grp))
  4266. return PTR_ERR(nh_grp);
  4267. }
  4268. list_add_tail(&fib6_entry->common.nexthop_group_node,
  4269. &nh_grp->fib_list);
  4270. fib6_entry->common.nh_group = nh_grp;
  4271. return 0;
  4272. }
  4273. static void mlxsw_sp_nexthop6_group_put(struct mlxsw_sp *mlxsw_sp,
  4274. struct mlxsw_sp_fib_entry *fib_entry)
  4275. {
  4276. struct mlxsw_sp_nexthop_group *nh_grp = fib_entry->nh_group;
  4277. list_del(&fib_entry->nexthop_group_node);
  4278. if (!list_empty(&nh_grp->fib_list))
  4279. return;
  4280. mlxsw_sp_nexthop6_group_destroy(mlxsw_sp, nh_grp);
  4281. }
  4282. static int
  4283. mlxsw_sp_nexthop6_group_update(struct mlxsw_sp *mlxsw_sp,
  4284. struct mlxsw_sp_fib6_entry *fib6_entry)
  4285. {
  4286. struct mlxsw_sp_nexthop_group *old_nh_grp = fib6_entry->common.nh_group;
  4287. int err;
  4288. fib6_entry->common.nh_group = NULL;
  4289. list_del(&fib6_entry->common.nexthop_group_node);
  4290. err = mlxsw_sp_nexthop6_group_get(mlxsw_sp, fib6_entry);
  4291. if (err)
  4292. goto err_nexthop6_group_get;
  4293. /* In case this entry is offloaded, then the adjacency index
  4294. * currently associated with it in the device's table is that
  4295. * of the old group. Start using the new one instead.
  4296. */
  4297. err = mlxsw_sp_fib_node_entry_add(mlxsw_sp, &fib6_entry->common);
  4298. if (err)
  4299. goto err_fib_node_entry_add;
  4300. if (list_empty(&old_nh_grp->fib_list))
  4301. mlxsw_sp_nexthop6_group_destroy(mlxsw_sp, old_nh_grp);
  4302. return 0;
  4303. err_fib_node_entry_add:
  4304. mlxsw_sp_nexthop6_group_put(mlxsw_sp, &fib6_entry->common);
  4305. err_nexthop6_group_get:
  4306. list_add_tail(&fib6_entry->common.nexthop_group_node,
  4307. &old_nh_grp->fib_list);
  4308. fib6_entry->common.nh_group = old_nh_grp;
  4309. return err;
  4310. }
  4311. static int
  4312. mlxsw_sp_fib6_entry_nexthop_add(struct mlxsw_sp *mlxsw_sp,
  4313. struct mlxsw_sp_fib6_entry *fib6_entry,
  4314. struct fib6_info *rt)
  4315. {
  4316. struct mlxsw_sp_rt6 *mlxsw_sp_rt6;
  4317. int err;
  4318. mlxsw_sp_rt6 = mlxsw_sp_rt6_create(rt);
  4319. if (IS_ERR(mlxsw_sp_rt6))
  4320. return PTR_ERR(mlxsw_sp_rt6);
  4321. list_add_tail(&mlxsw_sp_rt6->list, &fib6_entry->rt6_list);
  4322. fib6_entry->nrt6++;
  4323. err = mlxsw_sp_nexthop6_group_update(mlxsw_sp, fib6_entry);
  4324. if (err)
  4325. goto err_nexthop6_group_update;
  4326. return 0;
  4327. err_nexthop6_group_update:
  4328. fib6_entry->nrt6--;
  4329. list_del(&mlxsw_sp_rt6->list);
  4330. mlxsw_sp_rt6_destroy(mlxsw_sp_rt6);
  4331. return err;
  4332. }
  4333. static void
  4334. mlxsw_sp_fib6_entry_nexthop_del(struct mlxsw_sp *mlxsw_sp,
  4335. struct mlxsw_sp_fib6_entry *fib6_entry,
  4336. struct fib6_info *rt)
  4337. {
  4338. struct mlxsw_sp_rt6 *mlxsw_sp_rt6;
  4339. mlxsw_sp_rt6 = mlxsw_sp_fib6_entry_rt_find(fib6_entry, rt);
  4340. if (WARN_ON(!mlxsw_sp_rt6))
  4341. return;
  4342. fib6_entry->nrt6--;
  4343. list_del(&mlxsw_sp_rt6->list);
  4344. mlxsw_sp_nexthop6_group_update(mlxsw_sp, fib6_entry);
  4345. mlxsw_sp_rt6_destroy(mlxsw_sp_rt6);
  4346. }
  4347. static void mlxsw_sp_fib6_entry_type_set(struct mlxsw_sp *mlxsw_sp,
  4348. struct mlxsw_sp_fib_entry *fib_entry,
  4349. const struct fib6_info *rt)
  4350. {
  4351. /* Packets hitting RTF_REJECT routes need to be discarded by the
  4352. * stack. We can rely on their destination device not having a
  4353. * RIF (it's the loopback device) and can thus use action type
  4354. * local, which will cause them to be trapped with a lower
  4355. * priority than packets that need to be locally received.
  4356. */
  4357. if (rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST))
  4358. fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_TRAP;
  4359. else if (rt->fib6_flags & RTF_REJECT)
  4360. fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_LOCAL;
  4361. else if (mlxsw_sp_rt6_is_gateway(mlxsw_sp, rt))
  4362. fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_REMOTE;
  4363. else
  4364. fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_LOCAL;
  4365. }
  4366. static void
  4367. mlxsw_sp_fib6_entry_rt_destroy_all(struct mlxsw_sp_fib6_entry *fib6_entry)
  4368. {
  4369. struct mlxsw_sp_rt6 *mlxsw_sp_rt6, *tmp;
  4370. list_for_each_entry_safe(mlxsw_sp_rt6, tmp, &fib6_entry->rt6_list,
  4371. list) {
  4372. fib6_entry->nrt6--;
  4373. list_del(&mlxsw_sp_rt6->list);
  4374. mlxsw_sp_rt6_destroy(mlxsw_sp_rt6);
  4375. }
  4376. }
  4377. static struct mlxsw_sp_fib6_entry *
  4378. mlxsw_sp_fib6_entry_create(struct mlxsw_sp *mlxsw_sp,
  4379. struct mlxsw_sp_fib_node *fib_node,
  4380. struct fib6_info *rt)
  4381. {
  4382. struct mlxsw_sp_fib6_entry *fib6_entry;
  4383. struct mlxsw_sp_fib_entry *fib_entry;
  4384. struct mlxsw_sp_rt6 *mlxsw_sp_rt6;
  4385. int err;
  4386. fib6_entry = kzalloc(sizeof(*fib6_entry), GFP_KERNEL);
  4387. if (!fib6_entry)
  4388. return ERR_PTR(-ENOMEM);
  4389. fib_entry = &fib6_entry->common;
  4390. mlxsw_sp_rt6 = mlxsw_sp_rt6_create(rt);
  4391. if (IS_ERR(mlxsw_sp_rt6)) {
  4392. err = PTR_ERR(mlxsw_sp_rt6);
  4393. goto err_rt6_create;
  4394. }
  4395. mlxsw_sp_fib6_entry_type_set(mlxsw_sp, fib_entry, mlxsw_sp_rt6->rt);
  4396. INIT_LIST_HEAD(&fib6_entry->rt6_list);
  4397. list_add_tail(&mlxsw_sp_rt6->list, &fib6_entry->rt6_list);
  4398. fib6_entry->nrt6 = 1;
  4399. err = mlxsw_sp_nexthop6_group_get(mlxsw_sp, fib6_entry);
  4400. if (err)
  4401. goto err_nexthop6_group_get;
  4402. fib_entry->fib_node = fib_node;
  4403. return fib6_entry;
  4404. err_nexthop6_group_get:
  4405. list_del(&mlxsw_sp_rt6->list);
  4406. mlxsw_sp_rt6_destroy(mlxsw_sp_rt6);
  4407. err_rt6_create:
  4408. kfree(fib6_entry);
  4409. return ERR_PTR(err);
  4410. }
  4411. static void mlxsw_sp_fib6_entry_destroy(struct mlxsw_sp *mlxsw_sp,
  4412. struct mlxsw_sp_fib6_entry *fib6_entry)
  4413. {
  4414. mlxsw_sp_nexthop6_group_put(mlxsw_sp, &fib6_entry->common);
  4415. mlxsw_sp_fib6_entry_rt_destroy_all(fib6_entry);
  4416. WARN_ON(fib6_entry->nrt6);
  4417. kfree(fib6_entry);
  4418. }
  4419. static struct mlxsw_sp_fib6_entry *
  4420. mlxsw_sp_fib6_node_entry_find(const struct mlxsw_sp_fib_node *fib_node,
  4421. const struct fib6_info *nrt, bool replace)
  4422. {
  4423. struct mlxsw_sp_fib6_entry *fib6_entry, *fallback = NULL;
  4424. list_for_each_entry(fib6_entry, &fib_node->entry_list, common.list) {
  4425. struct fib6_info *rt = mlxsw_sp_fib6_entry_rt(fib6_entry);
  4426. if (rt->fib6_table->tb6_id > nrt->fib6_table->tb6_id)
  4427. continue;
  4428. if (rt->fib6_table->tb6_id != nrt->fib6_table->tb6_id)
  4429. break;
  4430. if (replace && rt->fib6_metric == nrt->fib6_metric) {
  4431. if (mlxsw_sp_fib6_rt_can_mp(rt) ==
  4432. mlxsw_sp_fib6_rt_can_mp(nrt))
  4433. return fib6_entry;
  4434. if (mlxsw_sp_fib6_rt_can_mp(nrt))
  4435. fallback = fallback ?: fib6_entry;
  4436. }
  4437. if (rt->fib6_metric > nrt->fib6_metric)
  4438. return fallback ?: fib6_entry;
  4439. }
  4440. return fallback;
  4441. }
  4442. static int
  4443. mlxsw_sp_fib6_node_list_insert(struct mlxsw_sp_fib6_entry *new6_entry,
  4444. bool replace)
  4445. {
  4446. struct mlxsw_sp_fib_node *fib_node = new6_entry->common.fib_node;
  4447. struct fib6_info *nrt = mlxsw_sp_fib6_entry_rt(new6_entry);
  4448. struct mlxsw_sp_fib6_entry *fib6_entry;
  4449. fib6_entry = mlxsw_sp_fib6_node_entry_find(fib_node, nrt, replace);
  4450. if (replace && WARN_ON(!fib6_entry))
  4451. return -EINVAL;
  4452. if (fib6_entry) {
  4453. list_add_tail(&new6_entry->common.list,
  4454. &fib6_entry->common.list);
  4455. } else {
  4456. struct mlxsw_sp_fib6_entry *last;
  4457. list_for_each_entry(last, &fib_node->entry_list, common.list) {
  4458. struct fib6_info *rt = mlxsw_sp_fib6_entry_rt(last);
  4459. if (nrt->fib6_table->tb6_id > rt->fib6_table->tb6_id)
  4460. break;
  4461. fib6_entry = last;
  4462. }
  4463. if (fib6_entry)
  4464. list_add(&new6_entry->common.list,
  4465. &fib6_entry->common.list);
  4466. else
  4467. list_add(&new6_entry->common.list,
  4468. &fib_node->entry_list);
  4469. }
  4470. return 0;
  4471. }
  4472. static void
  4473. mlxsw_sp_fib6_node_list_remove(struct mlxsw_sp_fib6_entry *fib6_entry)
  4474. {
  4475. list_del(&fib6_entry->common.list);
  4476. }
  4477. static int mlxsw_sp_fib6_node_entry_link(struct mlxsw_sp *mlxsw_sp,
  4478. struct mlxsw_sp_fib6_entry *fib6_entry,
  4479. bool replace)
  4480. {
  4481. int err;
  4482. err = mlxsw_sp_fib6_node_list_insert(fib6_entry, replace);
  4483. if (err)
  4484. return err;
  4485. err = mlxsw_sp_fib_node_entry_add(mlxsw_sp, &fib6_entry->common);
  4486. if (err)
  4487. goto err_fib_node_entry_add;
  4488. return 0;
  4489. err_fib_node_entry_add:
  4490. mlxsw_sp_fib6_node_list_remove(fib6_entry);
  4491. return err;
  4492. }
  4493. static void
  4494. mlxsw_sp_fib6_node_entry_unlink(struct mlxsw_sp *mlxsw_sp,
  4495. struct mlxsw_sp_fib6_entry *fib6_entry)
  4496. {
  4497. mlxsw_sp_fib_node_entry_del(mlxsw_sp, &fib6_entry->common);
  4498. mlxsw_sp_fib6_node_list_remove(fib6_entry);
  4499. }
  4500. static struct mlxsw_sp_fib6_entry *
  4501. mlxsw_sp_fib6_entry_lookup(struct mlxsw_sp *mlxsw_sp,
  4502. const struct fib6_info *rt)
  4503. {
  4504. struct mlxsw_sp_fib6_entry *fib6_entry;
  4505. struct mlxsw_sp_fib_node *fib_node;
  4506. struct mlxsw_sp_fib *fib;
  4507. struct mlxsw_sp_vr *vr;
  4508. vr = mlxsw_sp_vr_find(mlxsw_sp, rt->fib6_table->tb6_id);
  4509. if (!vr)
  4510. return NULL;
  4511. fib = mlxsw_sp_vr_fib(vr, MLXSW_SP_L3_PROTO_IPV6);
  4512. fib_node = mlxsw_sp_fib_node_lookup(fib, &rt->fib6_dst.addr,
  4513. sizeof(rt->fib6_dst.addr),
  4514. rt->fib6_dst.plen);
  4515. if (!fib_node)
  4516. return NULL;
  4517. list_for_each_entry(fib6_entry, &fib_node->entry_list, common.list) {
  4518. struct fib6_info *iter_rt = mlxsw_sp_fib6_entry_rt(fib6_entry);
  4519. if (rt->fib6_table->tb6_id == iter_rt->fib6_table->tb6_id &&
  4520. rt->fib6_metric == iter_rt->fib6_metric &&
  4521. mlxsw_sp_fib6_entry_rt_find(fib6_entry, rt))
  4522. return fib6_entry;
  4523. }
  4524. return NULL;
  4525. }
  4526. static void mlxsw_sp_fib6_entry_replace(struct mlxsw_sp *mlxsw_sp,
  4527. struct mlxsw_sp_fib6_entry *fib6_entry,
  4528. bool replace)
  4529. {
  4530. struct mlxsw_sp_fib_node *fib_node = fib6_entry->common.fib_node;
  4531. struct mlxsw_sp_fib6_entry *replaced;
  4532. if (!replace)
  4533. return;
  4534. replaced = list_next_entry(fib6_entry, common.list);
  4535. mlxsw_sp_fib6_node_entry_unlink(mlxsw_sp, replaced);
  4536. mlxsw_sp_fib6_entry_destroy(mlxsw_sp, replaced);
  4537. mlxsw_sp_fib_node_put(mlxsw_sp, fib_node);
  4538. }
  4539. static int mlxsw_sp_router_fib6_add(struct mlxsw_sp *mlxsw_sp,
  4540. struct fib6_info *rt, bool replace)
  4541. {
  4542. struct mlxsw_sp_fib6_entry *fib6_entry;
  4543. struct mlxsw_sp_fib_node *fib_node;
  4544. int err;
  4545. if (mlxsw_sp->router->aborted)
  4546. return 0;
  4547. if (rt->fib6_src.plen)
  4548. return -EINVAL;
  4549. if (mlxsw_sp_fib6_rt_should_ignore(rt))
  4550. return 0;
  4551. fib_node = mlxsw_sp_fib_node_get(mlxsw_sp, rt->fib6_table->tb6_id,
  4552. &rt->fib6_dst.addr,
  4553. sizeof(rt->fib6_dst.addr),
  4554. rt->fib6_dst.plen,
  4555. MLXSW_SP_L3_PROTO_IPV6);
  4556. if (IS_ERR(fib_node))
  4557. return PTR_ERR(fib_node);
  4558. /* Before creating a new entry, try to append route to an existing
  4559. * multipath entry.
  4560. */
  4561. fib6_entry = mlxsw_sp_fib6_node_mp_entry_find(fib_node, rt, replace);
  4562. if (fib6_entry) {
  4563. err = mlxsw_sp_fib6_entry_nexthop_add(mlxsw_sp, fib6_entry, rt);
  4564. if (err)
  4565. goto err_fib6_entry_nexthop_add;
  4566. return 0;
  4567. }
  4568. fib6_entry = mlxsw_sp_fib6_entry_create(mlxsw_sp, fib_node, rt);
  4569. if (IS_ERR(fib6_entry)) {
  4570. err = PTR_ERR(fib6_entry);
  4571. goto err_fib6_entry_create;
  4572. }
  4573. err = mlxsw_sp_fib6_node_entry_link(mlxsw_sp, fib6_entry, replace);
  4574. if (err)
  4575. goto err_fib6_node_entry_link;
  4576. mlxsw_sp_fib6_entry_replace(mlxsw_sp, fib6_entry, replace);
  4577. return 0;
  4578. err_fib6_node_entry_link:
  4579. mlxsw_sp_fib6_entry_destroy(mlxsw_sp, fib6_entry);
  4580. err_fib6_entry_create:
  4581. err_fib6_entry_nexthop_add:
  4582. mlxsw_sp_fib_node_put(mlxsw_sp, fib_node);
  4583. return err;
  4584. }
  4585. static void mlxsw_sp_router_fib6_del(struct mlxsw_sp *mlxsw_sp,
  4586. struct fib6_info *rt)
  4587. {
  4588. struct mlxsw_sp_fib6_entry *fib6_entry;
  4589. struct mlxsw_sp_fib_node *fib_node;
  4590. if (mlxsw_sp->router->aborted)
  4591. return;
  4592. if (mlxsw_sp_fib6_rt_should_ignore(rt))
  4593. return;
  4594. fib6_entry = mlxsw_sp_fib6_entry_lookup(mlxsw_sp, rt);
  4595. if (WARN_ON(!fib6_entry))
  4596. return;
  4597. /* If route is part of a multipath entry, but not the last one
  4598. * removed, then only reduce its nexthop group.
  4599. */
  4600. if (!list_is_singular(&fib6_entry->rt6_list)) {
  4601. mlxsw_sp_fib6_entry_nexthop_del(mlxsw_sp, fib6_entry, rt);
  4602. return;
  4603. }
  4604. fib_node = fib6_entry->common.fib_node;
  4605. mlxsw_sp_fib6_node_entry_unlink(mlxsw_sp, fib6_entry);
  4606. mlxsw_sp_fib6_entry_destroy(mlxsw_sp, fib6_entry);
  4607. mlxsw_sp_fib_node_put(mlxsw_sp, fib_node);
  4608. }
  4609. static int __mlxsw_sp_router_set_abort_trap(struct mlxsw_sp *mlxsw_sp,
  4610. enum mlxsw_reg_ralxx_protocol proto,
  4611. u8 tree_id)
  4612. {
  4613. char ralta_pl[MLXSW_REG_RALTA_LEN];
  4614. char ralst_pl[MLXSW_REG_RALST_LEN];
  4615. int i, err;
  4616. mlxsw_reg_ralta_pack(ralta_pl, true, proto, tree_id);
  4617. err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralta), ralta_pl);
  4618. if (err)
  4619. return err;
  4620. mlxsw_reg_ralst_pack(ralst_pl, 0xff, tree_id);
  4621. err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralst), ralst_pl);
  4622. if (err)
  4623. return err;
  4624. for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS); i++) {
  4625. struct mlxsw_sp_vr *vr = &mlxsw_sp->router->vrs[i];
  4626. char raltb_pl[MLXSW_REG_RALTB_LEN];
  4627. char ralue_pl[MLXSW_REG_RALUE_LEN];
  4628. mlxsw_reg_raltb_pack(raltb_pl, vr->id, proto, tree_id);
  4629. err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(raltb),
  4630. raltb_pl);
  4631. if (err)
  4632. return err;
  4633. mlxsw_reg_ralue_pack(ralue_pl, proto,
  4634. MLXSW_REG_RALUE_OP_WRITE_WRITE, vr->id, 0);
  4635. mlxsw_reg_ralue_act_ip2me_pack(ralue_pl);
  4636. err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue),
  4637. ralue_pl);
  4638. if (err)
  4639. return err;
  4640. }
  4641. return 0;
  4642. }
  4643. static struct mlxsw_sp_mr_table *
  4644. mlxsw_sp_router_fibmr_family_to_table(struct mlxsw_sp_vr *vr, int family)
  4645. {
  4646. if (family == RTNL_FAMILY_IPMR)
  4647. return vr->mr_table[MLXSW_SP_L3_PROTO_IPV4];
  4648. else
  4649. return vr->mr_table[MLXSW_SP_L3_PROTO_IPV6];
  4650. }
  4651. static int mlxsw_sp_router_fibmr_add(struct mlxsw_sp *mlxsw_sp,
  4652. struct mfc_entry_notifier_info *men_info,
  4653. bool replace)
  4654. {
  4655. struct mlxsw_sp_mr_table *mrt;
  4656. struct mlxsw_sp_vr *vr;
  4657. if (mlxsw_sp->router->aborted)
  4658. return 0;
  4659. vr = mlxsw_sp_vr_get(mlxsw_sp, men_info->tb_id, NULL);
  4660. if (IS_ERR(vr))
  4661. return PTR_ERR(vr);
  4662. mrt = mlxsw_sp_router_fibmr_family_to_table(vr, men_info->info.family);
  4663. return mlxsw_sp_mr_route_add(mrt, men_info->mfc, replace);
  4664. }
  4665. static void mlxsw_sp_router_fibmr_del(struct mlxsw_sp *mlxsw_sp,
  4666. struct mfc_entry_notifier_info *men_info)
  4667. {
  4668. struct mlxsw_sp_mr_table *mrt;
  4669. struct mlxsw_sp_vr *vr;
  4670. if (mlxsw_sp->router->aborted)
  4671. return;
  4672. vr = mlxsw_sp_vr_find(mlxsw_sp, men_info->tb_id);
  4673. if (WARN_ON(!vr))
  4674. return;
  4675. mrt = mlxsw_sp_router_fibmr_family_to_table(vr, men_info->info.family);
  4676. mlxsw_sp_mr_route_del(mrt, men_info->mfc);
  4677. mlxsw_sp_vr_put(mlxsw_sp, vr);
  4678. }
  4679. static int
  4680. mlxsw_sp_router_fibmr_vif_add(struct mlxsw_sp *mlxsw_sp,
  4681. struct vif_entry_notifier_info *ven_info)
  4682. {
  4683. struct mlxsw_sp_mr_table *mrt;
  4684. struct mlxsw_sp_rif *rif;
  4685. struct mlxsw_sp_vr *vr;
  4686. if (mlxsw_sp->router->aborted)
  4687. return 0;
  4688. vr = mlxsw_sp_vr_get(mlxsw_sp, ven_info->tb_id, NULL);
  4689. if (IS_ERR(vr))
  4690. return PTR_ERR(vr);
  4691. mrt = mlxsw_sp_router_fibmr_family_to_table(vr, ven_info->info.family);
  4692. rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, ven_info->dev);
  4693. return mlxsw_sp_mr_vif_add(mrt, ven_info->dev,
  4694. ven_info->vif_index,
  4695. ven_info->vif_flags, rif);
  4696. }
  4697. static void
  4698. mlxsw_sp_router_fibmr_vif_del(struct mlxsw_sp *mlxsw_sp,
  4699. struct vif_entry_notifier_info *ven_info)
  4700. {
  4701. struct mlxsw_sp_mr_table *mrt;
  4702. struct mlxsw_sp_vr *vr;
  4703. if (mlxsw_sp->router->aborted)
  4704. return;
  4705. vr = mlxsw_sp_vr_find(mlxsw_sp, ven_info->tb_id);
  4706. if (WARN_ON(!vr))
  4707. return;
  4708. mrt = mlxsw_sp_router_fibmr_family_to_table(vr, ven_info->info.family);
  4709. mlxsw_sp_mr_vif_del(mrt, ven_info->vif_index);
  4710. mlxsw_sp_vr_put(mlxsw_sp, vr);
  4711. }
  4712. static int mlxsw_sp_router_set_abort_trap(struct mlxsw_sp *mlxsw_sp)
  4713. {
  4714. enum mlxsw_reg_ralxx_protocol proto = MLXSW_REG_RALXX_PROTOCOL_IPV4;
  4715. int err;
  4716. err = __mlxsw_sp_router_set_abort_trap(mlxsw_sp, proto,
  4717. MLXSW_SP_LPM_TREE_MIN);
  4718. if (err)
  4719. return err;
  4720. /* The multicast router code does not need an abort trap as by default,
  4721. * packets that don't match any routes are trapped to the CPU.
  4722. */
  4723. proto = MLXSW_REG_RALXX_PROTOCOL_IPV6;
  4724. return __mlxsw_sp_router_set_abort_trap(mlxsw_sp, proto,
  4725. MLXSW_SP_LPM_TREE_MIN + 1);
  4726. }
  4727. static void mlxsw_sp_fib4_node_flush(struct mlxsw_sp *mlxsw_sp,
  4728. struct mlxsw_sp_fib_node *fib_node)
  4729. {
  4730. struct mlxsw_sp_fib4_entry *fib4_entry, *tmp;
  4731. list_for_each_entry_safe(fib4_entry, tmp, &fib_node->entry_list,
  4732. common.list) {
  4733. bool do_break = &tmp->common.list == &fib_node->entry_list;
  4734. mlxsw_sp_fib4_node_entry_unlink(mlxsw_sp, fib4_entry);
  4735. mlxsw_sp_fib4_entry_destroy(mlxsw_sp, fib4_entry);
  4736. mlxsw_sp_fib_node_put(mlxsw_sp, fib_node);
  4737. /* Break when entry list is empty and node was freed.
  4738. * Otherwise, we'll access freed memory in the next
  4739. * iteration.
  4740. */
  4741. if (do_break)
  4742. break;
  4743. }
  4744. }
  4745. static void mlxsw_sp_fib6_node_flush(struct mlxsw_sp *mlxsw_sp,
  4746. struct mlxsw_sp_fib_node *fib_node)
  4747. {
  4748. struct mlxsw_sp_fib6_entry *fib6_entry, *tmp;
  4749. list_for_each_entry_safe(fib6_entry, tmp, &fib_node->entry_list,
  4750. common.list) {
  4751. bool do_break = &tmp->common.list == &fib_node->entry_list;
  4752. mlxsw_sp_fib6_node_entry_unlink(mlxsw_sp, fib6_entry);
  4753. mlxsw_sp_fib6_entry_destroy(mlxsw_sp, fib6_entry);
  4754. mlxsw_sp_fib_node_put(mlxsw_sp, fib_node);
  4755. if (do_break)
  4756. break;
  4757. }
  4758. }
  4759. static void mlxsw_sp_fib_node_flush(struct mlxsw_sp *mlxsw_sp,
  4760. struct mlxsw_sp_fib_node *fib_node)
  4761. {
  4762. switch (fib_node->fib->proto) {
  4763. case MLXSW_SP_L3_PROTO_IPV4:
  4764. mlxsw_sp_fib4_node_flush(mlxsw_sp, fib_node);
  4765. break;
  4766. case MLXSW_SP_L3_PROTO_IPV6:
  4767. mlxsw_sp_fib6_node_flush(mlxsw_sp, fib_node);
  4768. break;
  4769. }
  4770. }
  4771. static void mlxsw_sp_vr_fib_flush(struct mlxsw_sp *mlxsw_sp,
  4772. struct mlxsw_sp_vr *vr,
  4773. enum mlxsw_sp_l3proto proto)
  4774. {
  4775. struct mlxsw_sp_fib *fib = mlxsw_sp_vr_fib(vr, proto);
  4776. struct mlxsw_sp_fib_node *fib_node, *tmp;
  4777. list_for_each_entry_safe(fib_node, tmp, &fib->node_list, list) {
  4778. bool do_break = &tmp->list == &fib->node_list;
  4779. mlxsw_sp_fib_node_flush(mlxsw_sp, fib_node);
  4780. if (do_break)
  4781. break;
  4782. }
  4783. }
  4784. static void mlxsw_sp_router_fib_flush(struct mlxsw_sp *mlxsw_sp)
  4785. {
  4786. int i, j;
  4787. for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS); i++) {
  4788. struct mlxsw_sp_vr *vr = &mlxsw_sp->router->vrs[i];
  4789. if (!mlxsw_sp_vr_is_used(vr))
  4790. continue;
  4791. for (j = 0; j < MLXSW_SP_L3_PROTO_MAX; j++)
  4792. mlxsw_sp_mr_table_flush(vr->mr_table[j]);
  4793. mlxsw_sp_vr_fib_flush(mlxsw_sp, vr, MLXSW_SP_L3_PROTO_IPV4);
  4794. /* If virtual router was only used for IPv4, then it's no
  4795. * longer used.
  4796. */
  4797. if (!mlxsw_sp_vr_is_used(vr))
  4798. continue;
  4799. mlxsw_sp_vr_fib_flush(mlxsw_sp, vr, MLXSW_SP_L3_PROTO_IPV6);
  4800. }
  4801. }
  4802. static void mlxsw_sp_router_fib_abort(struct mlxsw_sp *mlxsw_sp)
  4803. {
  4804. int err;
  4805. if (mlxsw_sp->router->aborted)
  4806. return;
  4807. dev_warn(mlxsw_sp->bus_info->dev, "FIB abort triggered. Note that FIB entries are no longer being offloaded to this device.\n");
  4808. mlxsw_sp_router_fib_flush(mlxsw_sp);
  4809. mlxsw_sp->router->aborted = true;
  4810. err = mlxsw_sp_router_set_abort_trap(mlxsw_sp);
  4811. if (err)
  4812. dev_warn(mlxsw_sp->bus_info->dev, "Failed to set abort trap.\n");
  4813. }
  4814. struct mlxsw_sp_fib_event_work {
  4815. struct work_struct work;
  4816. union {
  4817. struct fib6_entry_notifier_info fen6_info;
  4818. struct fib_entry_notifier_info fen_info;
  4819. struct fib_rule_notifier_info fr_info;
  4820. struct fib_nh_notifier_info fnh_info;
  4821. struct mfc_entry_notifier_info men_info;
  4822. struct vif_entry_notifier_info ven_info;
  4823. };
  4824. struct mlxsw_sp *mlxsw_sp;
  4825. unsigned long event;
  4826. };
  4827. static void mlxsw_sp_router_fib4_event_work(struct work_struct *work)
  4828. {
  4829. struct mlxsw_sp_fib_event_work *fib_work =
  4830. container_of(work, struct mlxsw_sp_fib_event_work, work);
  4831. struct mlxsw_sp *mlxsw_sp = fib_work->mlxsw_sp;
  4832. bool replace, append;
  4833. int err;
  4834. /* Protect internal structures from changes */
  4835. rtnl_lock();
  4836. mlxsw_sp_span_respin(mlxsw_sp);
  4837. switch (fib_work->event) {
  4838. case FIB_EVENT_ENTRY_REPLACE: /* fall through */
  4839. case FIB_EVENT_ENTRY_APPEND: /* fall through */
  4840. case FIB_EVENT_ENTRY_ADD:
  4841. replace = fib_work->event == FIB_EVENT_ENTRY_REPLACE;
  4842. append = fib_work->event == FIB_EVENT_ENTRY_APPEND;
  4843. err = mlxsw_sp_router_fib4_add(mlxsw_sp, &fib_work->fen_info,
  4844. replace, append);
  4845. if (err)
  4846. mlxsw_sp_router_fib_abort(mlxsw_sp);
  4847. fib_info_put(fib_work->fen_info.fi);
  4848. break;
  4849. case FIB_EVENT_ENTRY_DEL:
  4850. mlxsw_sp_router_fib4_del(mlxsw_sp, &fib_work->fen_info);
  4851. fib_info_put(fib_work->fen_info.fi);
  4852. break;
  4853. case FIB_EVENT_RULE_ADD:
  4854. /* if we get here, a rule was added that we do not support.
  4855. * just do the fib_abort
  4856. */
  4857. mlxsw_sp_router_fib_abort(mlxsw_sp);
  4858. break;
  4859. case FIB_EVENT_NH_ADD: /* fall through */
  4860. case FIB_EVENT_NH_DEL:
  4861. mlxsw_sp_nexthop4_event(mlxsw_sp, fib_work->event,
  4862. fib_work->fnh_info.fib_nh);
  4863. fib_info_put(fib_work->fnh_info.fib_nh->nh_parent);
  4864. break;
  4865. }
  4866. rtnl_unlock();
  4867. kfree(fib_work);
  4868. }
  4869. static void mlxsw_sp_router_fib6_event_work(struct work_struct *work)
  4870. {
  4871. struct mlxsw_sp_fib_event_work *fib_work =
  4872. container_of(work, struct mlxsw_sp_fib_event_work, work);
  4873. struct mlxsw_sp *mlxsw_sp = fib_work->mlxsw_sp;
  4874. bool replace;
  4875. int err;
  4876. rtnl_lock();
  4877. mlxsw_sp_span_respin(mlxsw_sp);
  4878. switch (fib_work->event) {
  4879. case FIB_EVENT_ENTRY_REPLACE: /* fall through */
  4880. case FIB_EVENT_ENTRY_APPEND: /* fall through */
  4881. case FIB_EVENT_ENTRY_ADD:
  4882. replace = fib_work->event == FIB_EVENT_ENTRY_REPLACE;
  4883. err = mlxsw_sp_router_fib6_add(mlxsw_sp,
  4884. fib_work->fen6_info.rt, replace);
  4885. if (err)
  4886. mlxsw_sp_router_fib_abort(mlxsw_sp);
  4887. mlxsw_sp_rt6_release(fib_work->fen6_info.rt);
  4888. break;
  4889. case FIB_EVENT_ENTRY_DEL:
  4890. mlxsw_sp_router_fib6_del(mlxsw_sp, fib_work->fen6_info.rt);
  4891. mlxsw_sp_rt6_release(fib_work->fen6_info.rt);
  4892. break;
  4893. case FIB_EVENT_RULE_ADD:
  4894. /* if we get here, a rule was added that we do not support.
  4895. * just do the fib_abort
  4896. */
  4897. mlxsw_sp_router_fib_abort(mlxsw_sp);
  4898. break;
  4899. }
  4900. rtnl_unlock();
  4901. kfree(fib_work);
  4902. }
  4903. static void mlxsw_sp_router_fibmr_event_work(struct work_struct *work)
  4904. {
  4905. struct mlxsw_sp_fib_event_work *fib_work =
  4906. container_of(work, struct mlxsw_sp_fib_event_work, work);
  4907. struct mlxsw_sp *mlxsw_sp = fib_work->mlxsw_sp;
  4908. bool replace;
  4909. int err;
  4910. rtnl_lock();
  4911. switch (fib_work->event) {
  4912. case FIB_EVENT_ENTRY_REPLACE: /* fall through */
  4913. case FIB_EVENT_ENTRY_ADD:
  4914. replace = fib_work->event == FIB_EVENT_ENTRY_REPLACE;
  4915. err = mlxsw_sp_router_fibmr_add(mlxsw_sp, &fib_work->men_info,
  4916. replace);
  4917. if (err)
  4918. mlxsw_sp_router_fib_abort(mlxsw_sp);
  4919. mr_cache_put(fib_work->men_info.mfc);
  4920. break;
  4921. case FIB_EVENT_ENTRY_DEL:
  4922. mlxsw_sp_router_fibmr_del(mlxsw_sp, &fib_work->men_info);
  4923. mr_cache_put(fib_work->men_info.mfc);
  4924. break;
  4925. case FIB_EVENT_VIF_ADD:
  4926. err = mlxsw_sp_router_fibmr_vif_add(mlxsw_sp,
  4927. &fib_work->ven_info);
  4928. if (err)
  4929. mlxsw_sp_router_fib_abort(mlxsw_sp);
  4930. dev_put(fib_work->ven_info.dev);
  4931. break;
  4932. case FIB_EVENT_VIF_DEL:
  4933. mlxsw_sp_router_fibmr_vif_del(mlxsw_sp,
  4934. &fib_work->ven_info);
  4935. dev_put(fib_work->ven_info.dev);
  4936. break;
  4937. case FIB_EVENT_RULE_ADD:
  4938. /* if we get here, a rule was added that we do not support.
  4939. * just do the fib_abort
  4940. */
  4941. mlxsw_sp_router_fib_abort(mlxsw_sp);
  4942. break;
  4943. }
  4944. rtnl_unlock();
  4945. kfree(fib_work);
  4946. }
  4947. static void mlxsw_sp_router_fib4_event(struct mlxsw_sp_fib_event_work *fib_work,
  4948. struct fib_notifier_info *info)
  4949. {
  4950. struct fib_entry_notifier_info *fen_info;
  4951. struct fib_nh_notifier_info *fnh_info;
  4952. switch (fib_work->event) {
  4953. case FIB_EVENT_ENTRY_REPLACE: /* fall through */
  4954. case FIB_EVENT_ENTRY_APPEND: /* fall through */
  4955. case FIB_EVENT_ENTRY_ADD: /* fall through */
  4956. case FIB_EVENT_ENTRY_DEL:
  4957. fen_info = container_of(info, struct fib_entry_notifier_info,
  4958. info);
  4959. fib_work->fen_info = *fen_info;
  4960. /* Take reference on fib_info to prevent it from being
  4961. * freed while work is queued. Release it afterwards.
  4962. */
  4963. fib_info_hold(fib_work->fen_info.fi);
  4964. break;
  4965. case FIB_EVENT_NH_ADD: /* fall through */
  4966. case FIB_EVENT_NH_DEL:
  4967. fnh_info = container_of(info, struct fib_nh_notifier_info,
  4968. info);
  4969. fib_work->fnh_info = *fnh_info;
  4970. fib_info_hold(fib_work->fnh_info.fib_nh->nh_parent);
  4971. break;
  4972. }
  4973. }
  4974. static void mlxsw_sp_router_fib6_event(struct mlxsw_sp_fib_event_work *fib_work,
  4975. struct fib_notifier_info *info)
  4976. {
  4977. struct fib6_entry_notifier_info *fen6_info;
  4978. switch (fib_work->event) {
  4979. case FIB_EVENT_ENTRY_REPLACE: /* fall through */
  4980. case FIB_EVENT_ENTRY_APPEND: /* fall through */
  4981. case FIB_EVENT_ENTRY_ADD: /* fall through */
  4982. case FIB_EVENT_ENTRY_DEL:
  4983. fen6_info = container_of(info, struct fib6_entry_notifier_info,
  4984. info);
  4985. fib_work->fen6_info = *fen6_info;
  4986. fib6_info_hold(fib_work->fen6_info.rt);
  4987. break;
  4988. }
  4989. }
  4990. static void
  4991. mlxsw_sp_router_fibmr_event(struct mlxsw_sp_fib_event_work *fib_work,
  4992. struct fib_notifier_info *info)
  4993. {
  4994. switch (fib_work->event) {
  4995. case FIB_EVENT_ENTRY_REPLACE: /* fall through */
  4996. case FIB_EVENT_ENTRY_ADD: /* fall through */
  4997. case FIB_EVENT_ENTRY_DEL:
  4998. memcpy(&fib_work->men_info, info, sizeof(fib_work->men_info));
  4999. mr_cache_hold(fib_work->men_info.mfc);
  5000. break;
  5001. case FIB_EVENT_VIF_ADD: /* fall through */
  5002. case FIB_EVENT_VIF_DEL:
  5003. memcpy(&fib_work->ven_info, info, sizeof(fib_work->ven_info));
  5004. dev_hold(fib_work->ven_info.dev);
  5005. break;
  5006. }
  5007. }
  5008. static int mlxsw_sp_router_fib_rule_event(unsigned long event,
  5009. struct fib_notifier_info *info,
  5010. struct mlxsw_sp *mlxsw_sp)
  5011. {
  5012. struct netlink_ext_ack *extack = info->extack;
  5013. struct fib_rule_notifier_info *fr_info;
  5014. struct fib_rule *rule;
  5015. int err = 0;
  5016. /* nothing to do at the moment */
  5017. if (event == FIB_EVENT_RULE_DEL)
  5018. return 0;
  5019. if (mlxsw_sp->router->aborted)
  5020. return 0;
  5021. fr_info = container_of(info, struct fib_rule_notifier_info, info);
  5022. rule = fr_info->rule;
  5023. switch (info->family) {
  5024. case AF_INET:
  5025. if (!fib4_rule_default(rule) && !rule->l3mdev)
  5026. err = -EOPNOTSUPP;
  5027. break;
  5028. case AF_INET6:
  5029. if (!fib6_rule_default(rule) && !rule->l3mdev)
  5030. err = -EOPNOTSUPP;
  5031. break;
  5032. case RTNL_FAMILY_IPMR:
  5033. if (!ipmr_rule_default(rule) && !rule->l3mdev)
  5034. err = -EOPNOTSUPP;
  5035. break;
  5036. case RTNL_FAMILY_IP6MR:
  5037. if (!ip6mr_rule_default(rule) && !rule->l3mdev)
  5038. err = -EOPNOTSUPP;
  5039. break;
  5040. }
  5041. if (err < 0)
  5042. NL_SET_ERR_MSG_MOD(extack, "FIB rules not supported");
  5043. return err;
  5044. }
  5045. /* Called with rcu_read_lock() */
  5046. static int mlxsw_sp_router_fib_event(struct notifier_block *nb,
  5047. unsigned long event, void *ptr)
  5048. {
  5049. struct mlxsw_sp_fib_event_work *fib_work;
  5050. struct fib_notifier_info *info = ptr;
  5051. struct mlxsw_sp_router *router;
  5052. int err;
  5053. if (!net_eq(info->net, &init_net) ||
  5054. (info->family != AF_INET && info->family != AF_INET6 &&
  5055. info->family != RTNL_FAMILY_IPMR &&
  5056. info->family != RTNL_FAMILY_IP6MR))
  5057. return NOTIFY_DONE;
  5058. router = container_of(nb, struct mlxsw_sp_router, fib_nb);
  5059. switch (event) {
  5060. case FIB_EVENT_RULE_ADD: /* fall through */
  5061. case FIB_EVENT_RULE_DEL:
  5062. err = mlxsw_sp_router_fib_rule_event(event, info,
  5063. router->mlxsw_sp);
  5064. if (!err || info->extack)
  5065. return notifier_from_errno(err);
  5066. break;
  5067. case FIB_EVENT_ENTRY_ADD:
  5068. if (router->aborted) {
  5069. NL_SET_ERR_MSG_MOD(info->extack, "FIB offload was aborted. Not configuring route");
  5070. return notifier_from_errno(-EINVAL);
  5071. }
  5072. break;
  5073. }
  5074. fib_work = kzalloc(sizeof(*fib_work), GFP_ATOMIC);
  5075. if (WARN_ON(!fib_work))
  5076. return NOTIFY_BAD;
  5077. fib_work->mlxsw_sp = router->mlxsw_sp;
  5078. fib_work->event = event;
  5079. switch (info->family) {
  5080. case AF_INET:
  5081. INIT_WORK(&fib_work->work, mlxsw_sp_router_fib4_event_work);
  5082. mlxsw_sp_router_fib4_event(fib_work, info);
  5083. break;
  5084. case AF_INET6:
  5085. INIT_WORK(&fib_work->work, mlxsw_sp_router_fib6_event_work);
  5086. mlxsw_sp_router_fib6_event(fib_work, info);
  5087. break;
  5088. case RTNL_FAMILY_IP6MR:
  5089. case RTNL_FAMILY_IPMR:
  5090. INIT_WORK(&fib_work->work, mlxsw_sp_router_fibmr_event_work);
  5091. mlxsw_sp_router_fibmr_event(fib_work, info);
  5092. break;
  5093. }
  5094. mlxsw_core_schedule_work(&fib_work->work);
  5095. return NOTIFY_DONE;
  5096. }
  5097. struct mlxsw_sp_rif *
  5098. mlxsw_sp_rif_find_by_dev(const struct mlxsw_sp *mlxsw_sp,
  5099. const struct net_device *dev)
  5100. {
  5101. int i;
  5102. for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS); i++)
  5103. if (mlxsw_sp->router->rifs[i] &&
  5104. mlxsw_sp->router->rifs[i]->dev == dev)
  5105. return mlxsw_sp->router->rifs[i];
  5106. return NULL;
  5107. }
  5108. static int mlxsw_sp_router_rif_disable(struct mlxsw_sp *mlxsw_sp, u16 rif)
  5109. {
  5110. char ritr_pl[MLXSW_REG_RITR_LEN];
  5111. int err;
  5112. mlxsw_reg_ritr_rif_pack(ritr_pl, rif);
  5113. err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
  5114. if (WARN_ON_ONCE(err))
  5115. return err;
  5116. mlxsw_reg_ritr_enable_set(ritr_pl, false);
  5117. return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
  5118. }
  5119. static void mlxsw_sp_router_rif_gone_sync(struct mlxsw_sp *mlxsw_sp,
  5120. struct mlxsw_sp_rif *rif)
  5121. {
  5122. mlxsw_sp_router_rif_disable(mlxsw_sp, rif->rif_index);
  5123. mlxsw_sp_nexthop_rif_gone_sync(mlxsw_sp, rif);
  5124. mlxsw_sp_neigh_rif_gone_sync(mlxsw_sp, rif);
  5125. }
  5126. static bool
  5127. mlxsw_sp_rif_should_config(struct mlxsw_sp_rif *rif, struct net_device *dev,
  5128. unsigned long event)
  5129. {
  5130. struct inet6_dev *inet6_dev;
  5131. bool addr_list_empty = true;
  5132. struct in_device *idev;
  5133. switch (event) {
  5134. case NETDEV_UP:
  5135. return rif == NULL;
  5136. case NETDEV_DOWN:
  5137. idev = __in_dev_get_rtnl(dev);
  5138. if (idev && idev->ifa_list)
  5139. addr_list_empty = false;
  5140. inet6_dev = __in6_dev_get(dev);
  5141. if (addr_list_empty && inet6_dev &&
  5142. !list_empty(&inet6_dev->addr_list))
  5143. addr_list_empty = false;
  5144. /* macvlans do not have a RIF, but rather piggy back on the
  5145. * RIF of their lower device.
  5146. */
  5147. if (netif_is_macvlan(dev) && addr_list_empty)
  5148. return true;
  5149. if (rif && addr_list_empty &&
  5150. !netif_is_l3_slave(rif->dev))
  5151. return true;
  5152. /* It is possible we already removed the RIF ourselves
  5153. * if it was assigned to a netdev that is now a bridge
  5154. * or LAG slave.
  5155. */
  5156. return false;
  5157. }
  5158. return false;
  5159. }
  5160. static enum mlxsw_sp_rif_type
  5161. mlxsw_sp_dev_rif_type(const struct mlxsw_sp *mlxsw_sp,
  5162. const struct net_device *dev)
  5163. {
  5164. enum mlxsw_sp_fid_type type;
  5165. if (mlxsw_sp_netdev_ipip_type(mlxsw_sp, dev, NULL))
  5166. return MLXSW_SP_RIF_TYPE_IPIP_LB;
  5167. /* Otherwise RIF type is derived from the type of the underlying FID. */
  5168. if (is_vlan_dev(dev) && netif_is_bridge_master(vlan_dev_real_dev(dev)))
  5169. type = MLXSW_SP_FID_TYPE_8021Q;
  5170. else if (netif_is_bridge_master(dev) && br_vlan_enabled(dev))
  5171. type = MLXSW_SP_FID_TYPE_8021Q;
  5172. else if (netif_is_bridge_master(dev))
  5173. type = MLXSW_SP_FID_TYPE_8021D;
  5174. else
  5175. type = MLXSW_SP_FID_TYPE_RFID;
  5176. return mlxsw_sp_fid_type_rif_type(mlxsw_sp, type);
  5177. }
  5178. static int mlxsw_sp_rif_index_alloc(struct mlxsw_sp *mlxsw_sp, u16 *p_rif_index)
  5179. {
  5180. int i;
  5181. for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS); i++) {
  5182. if (!mlxsw_sp->router->rifs[i]) {
  5183. *p_rif_index = i;
  5184. return 0;
  5185. }
  5186. }
  5187. return -ENOBUFS;
  5188. }
  5189. static struct mlxsw_sp_rif *mlxsw_sp_rif_alloc(size_t rif_size, u16 rif_index,
  5190. u16 vr_id,
  5191. struct net_device *l3_dev)
  5192. {
  5193. struct mlxsw_sp_rif *rif;
  5194. rif = kzalloc(rif_size, GFP_KERNEL);
  5195. if (!rif)
  5196. return NULL;
  5197. INIT_LIST_HEAD(&rif->nexthop_list);
  5198. INIT_LIST_HEAD(&rif->neigh_list);
  5199. ether_addr_copy(rif->addr, l3_dev->dev_addr);
  5200. rif->mtu = l3_dev->mtu;
  5201. rif->vr_id = vr_id;
  5202. rif->dev = l3_dev;
  5203. rif->rif_index = rif_index;
  5204. return rif;
  5205. }
  5206. struct mlxsw_sp_rif *mlxsw_sp_rif_by_index(const struct mlxsw_sp *mlxsw_sp,
  5207. u16 rif_index)
  5208. {
  5209. return mlxsw_sp->router->rifs[rif_index];
  5210. }
  5211. u16 mlxsw_sp_rif_index(const struct mlxsw_sp_rif *rif)
  5212. {
  5213. return rif->rif_index;
  5214. }
  5215. u16 mlxsw_sp_ipip_lb_rif_index(const struct mlxsw_sp_rif_ipip_lb *lb_rif)
  5216. {
  5217. return lb_rif->common.rif_index;
  5218. }
  5219. u16 mlxsw_sp_ipip_lb_ul_vr_id(const struct mlxsw_sp_rif_ipip_lb *lb_rif)
  5220. {
  5221. return lb_rif->ul_vr_id;
  5222. }
  5223. int mlxsw_sp_rif_dev_ifindex(const struct mlxsw_sp_rif *rif)
  5224. {
  5225. return rif->dev->ifindex;
  5226. }
  5227. const struct net_device *mlxsw_sp_rif_dev(const struct mlxsw_sp_rif *rif)
  5228. {
  5229. return rif->dev;
  5230. }
  5231. struct mlxsw_sp_fid *mlxsw_sp_rif_fid(const struct mlxsw_sp_rif *rif)
  5232. {
  5233. return rif->fid;
  5234. }
  5235. static struct mlxsw_sp_rif *
  5236. mlxsw_sp_rif_create(struct mlxsw_sp *mlxsw_sp,
  5237. const struct mlxsw_sp_rif_params *params,
  5238. struct netlink_ext_ack *extack)
  5239. {
  5240. u32 tb_id = l3mdev_fib_table(params->dev);
  5241. const struct mlxsw_sp_rif_ops *ops;
  5242. struct mlxsw_sp_fid *fid = NULL;
  5243. enum mlxsw_sp_rif_type type;
  5244. struct mlxsw_sp_rif *rif;
  5245. struct mlxsw_sp_vr *vr;
  5246. u16 rif_index;
  5247. int i, err;
  5248. type = mlxsw_sp_dev_rif_type(mlxsw_sp, params->dev);
  5249. ops = mlxsw_sp->router->rif_ops_arr[type];
  5250. vr = mlxsw_sp_vr_get(mlxsw_sp, tb_id ? : RT_TABLE_MAIN, extack);
  5251. if (IS_ERR(vr))
  5252. return ERR_CAST(vr);
  5253. vr->rif_count++;
  5254. err = mlxsw_sp_rif_index_alloc(mlxsw_sp, &rif_index);
  5255. if (err) {
  5256. NL_SET_ERR_MSG_MOD(extack, "Exceeded number of supported router interfaces");
  5257. goto err_rif_index_alloc;
  5258. }
  5259. rif = mlxsw_sp_rif_alloc(ops->rif_size, rif_index, vr->id, params->dev);
  5260. if (!rif) {
  5261. err = -ENOMEM;
  5262. goto err_rif_alloc;
  5263. }
  5264. rif->mlxsw_sp = mlxsw_sp;
  5265. rif->ops = ops;
  5266. if (ops->fid_get) {
  5267. fid = ops->fid_get(rif, extack);
  5268. if (IS_ERR(fid)) {
  5269. err = PTR_ERR(fid);
  5270. goto err_fid_get;
  5271. }
  5272. rif->fid = fid;
  5273. }
  5274. if (ops->setup)
  5275. ops->setup(rif, params);
  5276. err = ops->configure(rif);
  5277. if (err)
  5278. goto err_configure;
  5279. for (i = 0; i < MLXSW_SP_L3_PROTO_MAX; i++) {
  5280. err = mlxsw_sp_mr_rif_add(vr->mr_table[i], rif);
  5281. if (err)
  5282. goto err_mr_rif_add;
  5283. }
  5284. mlxsw_sp_rif_counters_alloc(rif);
  5285. mlxsw_sp->router->rifs[rif_index] = rif;
  5286. return rif;
  5287. err_mr_rif_add:
  5288. for (i--; i >= 0; i--)
  5289. mlxsw_sp_mr_rif_del(vr->mr_table[i], rif);
  5290. ops->deconfigure(rif);
  5291. err_configure:
  5292. if (fid)
  5293. mlxsw_sp_fid_put(fid);
  5294. err_fid_get:
  5295. kfree(rif);
  5296. err_rif_alloc:
  5297. err_rif_index_alloc:
  5298. vr->rif_count--;
  5299. mlxsw_sp_vr_put(mlxsw_sp, vr);
  5300. return ERR_PTR(err);
  5301. }
  5302. void mlxsw_sp_rif_destroy(struct mlxsw_sp_rif *rif)
  5303. {
  5304. const struct mlxsw_sp_rif_ops *ops = rif->ops;
  5305. struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
  5306. struct mlxsw_sp_fid *fid = rif->fid;
  5307. struct mlxsw_sp_vr *vr;
  5308. int i;
  5309. mlxsw_sp_router_rif_gone_sync(mlxsw_sp, rif);
  5310. vr = &mlxsw_sp->router->vrs[rif->vr_id];
  5311. mlxsw_sp->router->rifs[rif->rif_index] = NULL;
  5312. mlxsw_sp_rif_counters_free(rif);
  5313. for (i = 0; i < MLXSW_SP_L3_PROTO_MAX; i++)
  5314. mlxsw_sp_mr_rif_del(vr->mr_table[i], rif);
  5315. ops->deconfigure(rif);
  5316. if (fid)
  5317. /* Loopback RIFs are not associated with a FID. */
  5318. mlxsw_sp_fid_put(fid);
  5319. kfree(rif);
  5320. vr->rif_count--;
  5321. mlxsw_sp_vr_put(mlxsw_sp, vr);
  5322. }
  5323. void mlxsw_sp_rif_destroy_by_dev(struct mlxsw_sp *mlxsw_sp,
  5324. struct net_device *dev)
  5325. {
  5326. struct mlxsw_sp_rif *rif;
  5327. rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
  5328. if (!rif)
  5329. return;
  5330. mlxsw_sp_rif_destroy(rif);
  5331. }
  5332. static void
  5333. mlxsw_sp_rif_subport_params_init(struct mlxsw_sp_rif_params *params,
  5334. struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
  5335. {
  5336. struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
  5337. params->vid = mlxsw_sp_port_vlan->vid;
  5338. params->lag = mlxsw_sp_port->lagged;
  5339. if (params->lag)
  5340. params->lag_id = mlxsw_sp_port->lag_id;
  5341. else
  5342. params->system_port = mlxsw_sp_port->local_port;
  5343. }
  5344. static int
  5345. mlxsw_sp_port_vlan_router_join(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan,
  5346. struct net_device *l3_dev,
  5347. struct netlink_ext_ack *extack)
  5348. {
  5349. struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
  5350. struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
  5351. u16 vid = mlxsw_sp_port_vlan->vid;
  5352. struct mlxsw_sp_rif *rif;
  5353. struct mlxsw_sp_fid *fid;
  5354. int err;
  5355. rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, l3_dev);
  5356. if (!rif) {
  5357. struct mlxsw_sp_rif_params params = {
  5358. .dev = l3_dev,
  5359. };
  5360. mlxsw_sp_rif_subport_params_init(&params, mlxsw_sp_port_vlan);
  5361. rif = mlxsw_sp_rif_create(mlxsw_sp, &params, extack);
  5362. if (IS_ERR(rif))
  5363. return PTR_ERR(rif);
  5364. }
  5365. /* FID was already created, just take a reference */
  5366. fid = rif->ops->fid_get(rif, extack);
  5367. err = mlxsw_sp_fid_port_vid_map(fid, mlxsw_sp_port, vid);
  5368. if (err)
  5369. goto err_fid_port_vid_map;
  5370. err = mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, false);
  5371. if (err)
  5372. goto err_port_vid_learning_set;
  5373. err = mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid,
  5374. BR_STATE_FORWARDING);
  5375. if (err)
  5376. goto err_port_vid_stp_set;
  5377. mlxsw_sp_port_vlan->fid = fid;
  5378. return 0;
  5379. err_port_vid_stp_set:
  5380. mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, true);
  5381. err_port_vid_learning_set:
  5382. mlxsw_sp_fid_port_vid_unmap(fid, mlxsw_sp_port, vid);
  5383. err_fid_port_vid_map:
  5384. mlxsw_sp_fid_put(fid);
  5385. return err;
  5386. }
  5387. void
  5388. mlxsw_sp_port_vlan_router_leave(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
  5389. {
  5390. struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
  5391. struct mlxsw_sp_fid *fid = mlxsw_sp_port_vlan->fid;
  5392. u16 vid = mlxsw_sp_port_vlan->vid;
  5393. if (WARN_ON(mlxsw_sp_fid_type(fid) != MLXSW_SP_FID_TYPE_RFID))
  5394. return;
  5395. mlxsw_sp_port_vlan->fid = NULL;
  5396. mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid, BR_STATE_BLOCKING);
  5397. mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, true);
  5398. mlxsw_sp_fid_port_vid_unmap(fid, mlxsw_sp_port, vid);
  5399. /* If router port holds the last reference on the rFID, then the
  5400. * associated Sub-port RIF will be destroyed.
  5401. */
  5402. mlxsw_sp_fid_put(fid);
  5403. }
  5404. static int mlxsw_sp_inetaddr_port_vlan_event(struct net_device *l3_dev,
  5405. struct net_device *port_dev,
  5406. unsigned long event, u16 vid,
  5407. struct netlink_ext_ack *extack)
  5408. {
  5409. struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(port_dev);
  5410. struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
  5411. mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
  5412. if (WARN_ON(!mlxsw_sp_port_vlan))
  5413. return -EINVAL;
  5414. switch (event) {
  5415. case NETDEV_UP:
  5416. return mlxsw_sp_port_vlan_router_join(mlxsw_sp_port_vlan,
  5417. l3_dev, extack);
  5418. case NETDEV_DOWN:
  5419. mlxsw_sp_port_vlan_router_leave(mlxsw_sp_port_vlan);
  5420. break;
  5421. }
  5422. return 0;
  5423. }
  5424. static int mlxsw_sp_inetaddr_port_event(struct net_device *port_dev,
  5425. unsigned long event,
  5426. struct netlink_ext_ack *extack)
  5427. {
  5428. if (netif_is_bridge_port(port_dev) ||
  5429. netif_is_lag_port(port_dev) ||
  5430. netif_is_ovs_port(port_dev))
  5431. return 0;
  5432. return mlxsw_sp_inetaddr_port_vlan_event(port_dev, port_dev, event, 1,
  5433. extack);
  5434. }
  5435. static int __mlxsw_sp_inetaddr_lag_event(struct net_device *l3_dev,
  5436. struct net_device *lag_dev,
  5437. unsigned long event, u16 vid,
  5438. struct netlink_ext_ack *extack)
  5439. {
  5440. struct net_device *port_dev;
  5441. struct list_head *iter;
  5442. int err;
  5443. netdev_for_each_lower_dev(lag_dev, port_dev, iter) {
  5444. if (mlxsw_sp_port_dev_check(port_dev)) {
  5445. err = mlxsw_sp_inetaddr_port_vlan_event(l3_dev,
  5446. port_dev,
  5447. event, vid,
  5448. extack);
  5449. if (err)
  5450. return err;
  5451. }
  5452. }
  5453. return 0;
  5454. }
  5455. static int mlxsw_sp_inetaddr_lag_event(struct net_device *lag_dev,
  5456. unsigned long event,
  5457. struct netlink_ext_ack *extack)
  5458. {
  5459. if (netif_is_bridge_port(lag_dev))
  5460. return 0;
  5461. return __mlxsw_sp_inetaddr_lag_event(lag_dev, lag_dev, event, 1,
  5462. extack);
  5463. }
  5464. static int mlxsw_sp_inetaddr_bridge_event(struct net_device *l3_dev,
  5465. unsigned long event,
  5466. struct netlink_ext_ack *extack)
  5467. {
  5468. struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(l3_dev);
  5469. struct mlxsw_sp_rif_params params = {
  5470. .dev = l3_dev,
  5471. };
  5472. struct mlxsw_sp_rif *rif;
  5473. switch (event) {
  5474. case NETDEV_UP:
  5475. rif = mlxsw_sp_rif_create(mlxsw_sp, &params, extack);
  5476. if (IS_ERR(rif))
  5477. return PTR_ERR(rif);
  5478. break;
  5479. case NETDEV_DOWN:
  5480. rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, l3_dev);
  5481. mlxsw_sp_rif_destroy(rif);
  5482. break;
  5483. }
  5484. return 0;
  5485. }
  5486. static int mlxsw_sp_inetaddr_vlan_event(struct net_device *vlan_dev,
  5487. unsigned long event,
  5488. struct netlink_ext_ack *extack)
  5489. {
  5490. struct net_device *real_dev = vlan_dev_real_dev(vlan_dev);
  5491. u16 vid = vlan_dev_vlan_id(vlan_dev);
  5492. if (netif_is_bridge_port(vlan_dev))
  5493. return 0;
  5494. if (mlxsw_sp_port_dev_check(real_dev))
  5495. return mlxsw_sp_inetaddr_port_vlan_event(vlan_dev, real_dev,
  5496. event, vid, extack);
  5497. else if (netif_is_lag_master(real_dev))
  5498. return __mlxsw_sp_inetaddr_lag_event(vlan_dev, real_dev, event,
  5499. vid, extack);
  5500. else if (netif_is_bridge_master(real_dev) && br_vlan_enabled(real_dev))
  5501. return mlxsw_sp_inetaddr_bridge_event(vlan_dev, event, extack);
  5502. return 0;
  5503. }
  5504. static bool mlxsw_sp_rif_macvlan_is_vrrp4(const u8 *mac)
  5505. {
  5506. u8 vrrp4[ETH_ALEN] = { 0x00, 0x00, 0x5e, 0x00, 0x01, 0x00 };
  5507. u8 mask[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0x00 };
  5508. return ether_addr_equal_masked(mac, vrrp4, mask);
  5509. }
  5510. static bool mlxsw_sp_rif_macvlan_is_vrrp6(const u8 *mac)
  5511. {
  5512. u8 vrrp6[ETH_ALEN] = { 0x00, 0x00, 0x5e, 0x00, 0x02, 0x00 };
  5513. u8 mask[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0x00 };
  5514. return ether_addr_equal_masked(mac, vrrp6, mask);
  5515. }
  5516. static int mlxsw_sp_rif_vrrp_op(struct mlxsw_sp *mlxsw_sp, u16 rif_index,
  5517. const u8 *mac, bool adding)
  5518. {
  5519. char ritr_pl[MLXSW_REG_RITR_LEN];
  5520. u8 vrrp_id = adding ? mac[5] : 0;
  5521. int err;
  5522. if (!mlxsw_sp_rif_macvlan_is_vrrp4(mac) &&
  5523. !mlxsw_sp_rif_macvlan_is_vrrp6(mac))
  5524. return 0;
  5525. mlxsw_reg_ritr_rif_pack(ritr_pl, rif_index);
  5526. err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
  5527. if (err)
  5528. return err;
  5529. if (mlxsw_sp_rif_macvlan_is_vrrp4(mac))
  5530. mlxsw_reg_ritr_if_vrrp_id_ipv4_set(ritr_pl, vrrp_id);
  5531. else
  5532. mlxsw_reg_ritr_if_vrrp_id_ipv6_set(ritr_pl, vrrp_id);
  5533. return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
  5534. }
  5535. static int mlxsw_sp_rif_macvlan_add(struct mlxsw_sp *mlxsw_sp,
  5536. const struct net_device *macvlan_dev,
  5537. struct netlink_ext_ack *extack)
  5538. {
  5539. struct macvlan_dev *vlan = netdev_priv(macvlan_dev);
  5540. struct mlxsw_sp_rif *rif;
  5541. int err;
  5542. rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, vlan->lowerdev);
  5543. if (!rif) {
  5544. NL_SET_ERR_MSG_MOD(extack, "macvlan is only supported on top of router interfaces");
  5545. return -EOPNOTSUPP;
  5546. }
  5547. err = mlxsw_sp_rif_fdb_op(mlxsw_sp, macvlan_dev->dev_addr,
  5548. mlxsw_sp_fid_index(rif->fid), true);
  5549. if (err)
  5550. return err;
  5551. err = mlxsw_sp_rif_vrrp_op(mlxsw_sp, rif->rif_index,
  5552. macvlan_dev->dev_addr, true);
  5553. if (err)
  5554. goto err_rif_vrrp_add;
  5555. /* Make sure the bridge driver does not have this MAC pointing at
  5556. * some other port.
  5557. */
  5558. if (rif->ops->fdb_del)
  5559. rif->ops->fdb_del(rif, macvlan_dev->dev_addr);
  5560. return 0;
  5561. err_rif_vrrp_add:
  5562. mlxsw_sp_rif_fdb_op(mlxsw_sp, macvlan_dev->dev_addr,
  5563. mlxsw_sp_fid_index(rif->fid), false);
  5564. return err;
  5565. }
  5566. void mlxsw_sp_rif_macvlan_del(struct mlxsw_sp *mlxsw_sp,
  5567. const struct net_device *macvlan_dev)
  5568. {
  5569. struct macvlan_dev *vlan = netdev_priv(macvlan_dev);
  5570. struct mlxsw_sp_rif *rif;
  5571. rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, vlan->lowerdev);
  5572. /* If we do not have a RIF, then we already took care of
  5573. * removing the macvlan's MAC during RIF deletion.
  5574. */
  5575. if (!rif)
  5576. return;
  5577. mlxsw_sp_rif_vrrp_op(mlxsw_sp, rif->rif_index, macvlan_dev->dev_addr,
  5578. false);
  5579. mlxsw_sp_rif_fdb_op(mlxsw_sp, macvlan_dev->dev_addr,
  5580. mlxsw_sp_fid_index(rif->fid), false);
  5581. }
  5582. static int mlxsw_sp_inetaddr_macvlan_event(struct net_device *macvlan_dev,
  5583. unsigned long event,
  5584. struct netlink_ext_ack *extack)
  5585. {
  5586. struct mlxsw_sp *mlxsw_sp;
  5587. mlxsw_sp = mlxsw_sp_lower_get(macvlan_dev);
  5588. if (!mlxsw_sp)
  5589. return 0;
  5590. switch (event) {
  5591. case NETDEV_UP:
  5592. return mlxsw_sp_rif_macvlan_add(mlxsw_sp, macvlan_dev, extack);
  5593. case NETDEV_DOWN:
  5594. mlxsw_sp_rif_macvlan_del(mlxsw_sp, macvlan_dev);
  5595. break;
  5596. }
  5597. return 0;
  5598. }
  5599. static int __mlxsw_sp_inetaddr_event(struct net_device *dev,
  5600. unsigned long event,
  5601. struct netlink_ext_ack *extack)
  5602. {
  5603. if (mlxsw_sp_port_dev_check(dev))
  5604. return mlxsw_sp_inetaddr_port_event(dev, event, extack);
  5605. else if (netif_is_lag_master(dev))
  5606. return mlxsw_sp_inetaddr_lag_event(dev, event, extack);
  5607. else if (netif_is_bridge_master(dev))
  5608. return mlxsw_sp_inetaddr_bridge_event(dev, event, extack);
  5609. else if (is_vlan_dev(dev))
  5610. return mlxsw_sp_inetaddr_vlan_event(dev, event, extack);
  5611. else if (netif_is_macvlan(dev))
  5612. return mlxsw_sp_inetaddr_macvlan_event(dev, event, extack);
  5613. else
  5614. return 0;
  5615. }
  5616. int mlxsw_sp_inetaddr_event(struct notifier_block *unused,
  5617. unsigned long event, void *ptr)
  5618. {
  5619. struct in_ifaddr *ifa = (struct in_ifaddr *) ptr;
  5620. struct net_device *dev = ifa->ifa_dev->dev;
  5621. struct mlxsw_sp *mlxsw_sp;
  5622. struct mlxsw_sp_rif *rif;
  5623. int err = 0;
  5624. /* NETDEV_UP event is handled by mlxsw_sp_inetaddr_valid_event */
  5625. if (event == NETDEV_UP)
  5626. goto out;
  5627. mlxsw_sp = mlxsw_sp_lower_get(dev);
  5628. if (!mlxsw_sp)
  5629. goto out;
  5630. rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
  5631. if (!mlxsw_sp_rif_should_config(rif, dev, event))
  5632. goto out;
  5633. err = __mlxsw_sp_inetaddr_event(dev, event, NULL);
  5634. out:
  5635. return notifier_from_errno(err);
  5636. }
  5637. int mlxsw_sp_inetaddr_valid_event(struct notifier_block *unused,
  5638. unsigned long event, void *ptr)
  5639. {
  5640. struct in_validator_info *ivi = (struct in_validator_info *) ptr;
  5641. struct net_device *dev = ivi->ivi_dev->dev;
  5642. struct mlxsw_sp *mlxsw_sp;
  5643. struct mlxsw_sp_rif *rif;
  5644. int err = 0;
  5645. mlxsw_sp = mlxsw_sp_lower_get(dev);
  5646. if (!mlxsw_sp)
  5647. goto out;
  5648. rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
  5649. if (!mlxsw_sp_rif_should_config(rif, dev, event))
  5650. goto out;
  5651. err = __mlxsw_sp_inetaddr_event(dev, event, ivi->extack);
  5652. out:
  5653. return notifier_from_errno(err);
  5654. }
  5655. struct mlxsw_sp_inet6addr_event_work {
  5656. struct work_struct work;
  5657. struct net_device *dev;
  5658. unsigned long event;
  5659. };
  5660. static void mlxsw_sp_inet6addr_event_work(struct work_struct *work)
  5661. {
  5662. struct mlxsw_sp_inet6addr_event_work *inet6addr_work =
  5663. container_of(work, struct mlxsw_sp_inet6addr_event_work, work);
  5664. struct net_device *dev = inet6addr_work->dev;
  5665. unsigned long event = inet6addr_work->event;
  5666. struct mlxsw_sp *mlxsw_sp;
  5667. struct mlxsw_sp_rif *rif;
  5668. rtnl_lock();
  5669. mlxsw_sp = mlxsw_sp_lower_get(dev);
  5670. if (!mlxsw_sp)
  5671. goto out;
  5672. rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
  5673. if (!mlxsw_sp_rif_should_config(rif, dev, event))
  5674. goto out;
  5675. __mlxsw_sp_inetaddr_event(dev, event, NULL);
  5676. out:
  5677. rtnl_unlock();
  5678. dev_put(dev);
  5679. kfree(inet6addr_work);
  5680. }
  5681. /* Called with rcu_read_lock() */
  5682. int mlxsw_sp_inet6addr_event(struct notifier_block *unused,
  5683. unsigned long event, void *ptr)
  5684. {
  5685. struct inet6_ifaddr *if6 = (struct inet6_ifaddr *) ptr;
  5686. struct mlxsw_sp_inet6addr_event_work *inet6addr_work;
  5687. struct net_device *dev = if6->idev->dev;
  5688. /* NETDEV_UP event is handled by mlxsw_sp_inet6addr_valid_event */
  5689. if (event == NETDEV_UP)
  5690. return NOTIFY_DONE;
  5691. if (!mlxsw_sp_port_dev_lower_find_rcu(dev))
  5692. return NOTIFY_DONE;
  5693. inet6addr_work = kzalloc(sizeof(*inet6addr_work), GFP_ATOMIC);
  5694. if (!inet6addr_work)
  5695. return NOTIFY_BAD;
  5696. INIT_WORK(&inet6addr_work->work, mlxsw_sp_inet6addr_event_work);
  5697. inet6addr_work->dev = dev;
  5698. inet6addr_work->event = event;
  5699. dev_hold(dev);
  5700. mlxsw_core_schedule_work(&inet6addr_work->work);
  5701. return NOTIFY_DONE;
  5702. }
  5703. int mlxsw_sp_inet6addr_valid_event(struct notifier_block *unused,
  5704. unsigned long event, void *ptr)
  5705. {
  5706. struct in6_validator_info *i6vi = (struct in6_validator_info *) ptr;
  5707. struct net_device *dev = i6vi->i6vi_dev->dev;
  5708. struct mlxsw_sp *mlxsw_sp;
  5709. struct mlxsw_sp_rif *rif;
  5710. int err = 0;
  5711. mlxsw_sp = mlxsw_sp_lower_get(dev);
  5712. if (!mlxsw_sp)
  5713. goto out;
  5714. rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
  5715. if (!mlxsw_sp_rif_should_config(rif, dev, event))
  5716. goto out;
  5717. err = __mlxsw_sp_inetaddr_event(dev, event, i6vi->extack);
  5718. out:
  5719. return notifier_from_errno(err);
  5720. }
  5721. static int mlxsw_sp_rif_edit(struct mlxsw_sp *mlxsw_sp, u16 rif_index,
  5722. const char *mac, int mtu)
  5723. {
  5724. char ritr_pl[MLXSW_REG_RITR_LEN];
  5725. int err;
  5726. mlxsw_reg_ritr_rif_pack(ritr_pl, rif_index);
  5727. err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
  5728. if (err)
  5729. return err;
  5730. mlxsw_reg_ritr_mtu_set(ritr_pl, mtu);
  5731. mlxsw_reg_ritr_if_mac_memcpy_to(ritr_pl, mac);
  5732. mlxsw_reg_ritr_op_set(ritr_pl, MLXSW_REG_RITR_RIF_CREATE);
  5733. return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
  5734. }
  5735. int mlxsw_sp_netdevice_router_port_event(struct net_device *dev)
  5736. {
  5737. struct mlxsw_sp *mlxsw_sp;
  5738. struct mlxsw_sp_rif *rif;
  5739. u16 fid_index;
  5740. int err;
  5741. mlxsw_sp = mlxsw_sp_lower_get(dev);
  5742. if (!mlxsw_sp)
  5743. return 0;
  5744. rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
  5745. if (!rif)
  5746. return 0;
  5747. fid_index = mlxsw_sp_fid_index(rif->fid);
  5748. err = mlxsw_sp_rif_fdb_op(mlxsw_sp, rif->addr, fid_index, false);
  5749. if (err)
  5750. return err;
  5751. err = mlxsw_sp_rif_edit(mlxsw_sp, rif->rif_index, dev->dev_addr,
  5752. dev->mtu);
  5753. if (err)
  5754. goto err_rif_edit;
  5755. err = mlxsw_sp_rif_fdb_op(mlxsw_sp, dev->dev_addr, fid_index, true);
  5756. if (err)
  5757. goto err_rif_fdb_op;
  5758. if (rif->mtu != dev->mtu) {
  5759. struct mlxsw_sp_vr *vr;
  5760. int i;
  5761. /* The RIF is relevant only to its mr_table instance, as unlike
  5762. * unicast routing, in multicast routing a RIF cannot be shared
  5763. * between several multicast routing tables.
  5764. */
  5765. vr = &mlxsw_sp->router->vrs[rif->vr_id];
  5766. for (i = 0; i < MLXSW_SP_L3_PROTO_MAX; i++)
  5767. mlxsw_sp_mr_rif_mtu_update(vr->mr_table[i],
  5768. rif, dev->mtu);
  5769. }
  5770. ether_addr_copy(rif->addr, dev->dev_addr);
  5771. rif->mtu = dev->mtu;
  5772. netdev_dbg(dev, "Updated RIF=%d\n", rif->rif_index);
  5773. return 0;
  5774. err_rif_fdb_op:
  5775. mlxsw_sp_rif_edit(mlxsw_sp, rif->rif_index, rif->addr, rif->mtu);
  5776. err_rif_edit:
  5777. mlxsw_sp_rif_fdb_op(mlxsw_sp, rif->addr, fid_index, true);
  5778. return err;
  5779. }
  5780. static int mlxsw_sp_port_vrf_join(struct mlxsw_sp *mlxsw_sp,
  5781. struct net_device *l3_dev,
  5782. struct netlink_ext_ack *extack)
  5783. {
  5784. struct mlxsw_sp_rif *rif;
  5785. /* If netdev is already associated with a RIF, then we need to
  5786. * destroy it and create a new one with the new virtual router ID.
  5787. */
  5788. rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, l3_dev);
  5789. if (rif)
  5790. __mlxsw_sp_inetaddr_event(l3_dev, NETDEV_DOWN, extack);
  5791. return __mlxsw_sp_inetaddr_event(l3_dev, NETDEV_UP, extack);
  5792. }
  5793. static void mlxsw_sp_port_vrf_leave(struct mlxsw_sp *mlxsw_sp,
  5794. struct net_device *l3_dev)
  5795. {
  5796. struct mlxsw_sp_rif *rif;
  5797. rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, l3_dev);
  5798. if (!rif)
  5799. return;
  5800. __mlxsw_sp_inetaddr_event(l3_dev, NETDEV_DOWN, NULL);
  5801. }
  5802. int mlxsw_sp_netdevice_vrf_event(struct net_device *l3_dev, unsigned long event,
  5803. struct netdev_notifier_changeupper_info *info)
  5804. {
  5805. struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(l3_dev);
  5806. int err = 0;
  5807. /* We do not create a RIF for a macvlan, but only use it to
  5808. * direct more MAC addresses to the router.
  5809. */
  5810. if (!mlxsw_sp || netif_is_macvlan(l3_dev))
  5811. return 0;
  5812. switch (event) {
  5813. case NETDEV_PRECHANGEUPPER:
  5814. return 0;
  5815. case NETDEV_CHANGEUPPER:
  5816. if (info->linking) {
  5817. struct netlink_ext_ack *extack;
  5818. extack = netdev_notifier_info_to_extack(&info->info);
  5819. err = mlxsw_sp_port_vrf_join(mlxsw_sp, l3_dev, extack);
  5820. } else {
  5821. mlxsw_sp_port_vrf_leave(mlxsw_sp, l3_dev);
  5822. }
  5823. break;
  5824. }
  5825. return err;
  5826. }
  5827. static int __mlxsw_sp_rif_macvlan_flush(struct net_device *dev, void *data)
  5828. {
  5829. struct mlxsw_sp_rif *rif = data;
  5830. if (!netif_is_macvlan(dev))
  5831. return 0;
  5832. return mlxsw_sp_rif_fdb_op(rif->mlxsw_sp, dev->dev_addr,
  5833. mlxsw_sp_fid_index(rif->fid), false);
  5834. }
  5835. static int mlxsw_sp_rif_macvlan_flush(struct mlxsw_sp_rif *rif)
  5836. {
  5837. if (!netif_is_macvlan_port(rif->dev))
  5838. return 0;
  5839. netdev_warn(rif->dev, "Router interface is deleted. Upper macvlans will not work\n");
  5840. return netdev_walk_all_upper_dev_rcu(rif->dev,
  5841. __mlxsw_sp_rif_macvlan_flush, rif);
  5842. }
  5843. static struct mlxsw_sp_rif_subport *
  5844. mlxsw_sp_rif_subport_rif(const struct mlxsw_sp_rif *rif)
  5845. {
  5846. return container_of(rif, struct mlxsw_sp_rif_subport, common);
  5847. }
  5848. static void mlxsw_sp_rif_subport_setup(struct mlxsw_sp_rif *rif,
  5849. const struct mlxsw_sp_rif_params *params)
  5850. {
  5851. struct mlxsw_sp_rif_subport *rif_subport;
  5852. rif_subport = mlxsw_sp_rif_subport_rif(rif);
  5853. rif_subport->vid = params->vid;
  5854. rif_subport->lag = params->lag;
  5855. if (params->lag)
  5856. rif_subport->lag_id = params->lag_id;
  5857. else
  5858. rif_subport->system_port = params->system_port;
  5859. }
  5860. static int mlxsw_sp_rif_subport_op(struct mlxsw_sp_rif *rif, bool enable)
  5861. {
  5862. struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
  5863. struct mlxsw_sp_rif_subport *rif_subport;
  5864. char ritr_pl[MLXSW_REG_RITR_LEN];
  5865. rif_subport = mlxsw_sp_rif_subport_rif(rif);
  5866. mlxsw_reg_ritr_pack(ritr_pl, enable, MLXSW_REG_RITR_SP_IF,
  5867. rif->rif_index, rif->vr_id, rif->dev->mtu);
  5868. mlxsw_reg_ritr_mac_pack(ritr_pl, rif->dev->dev_addr);
  5869. mlxsw_reg_ritr_sp_if_pack(ritr_pl, rif_subport->lag,
  5870. rif_subport->lag ? rif_subport->lag_id :
  5871. rif_subport->system_port,
  5872. rif_subport->vid);
  5873. return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
  5874. }
  5875. static int mlxsw_sp_rif_subport_configure(struct mlxsw_sp_rif *rif)
  5876. {
  5877. int err;
  5878. err = mlxsw_sp_rif_subport_op(rif, true);
  5879. if (err)
  5880. return err;
  5881. err = mlxsw_sp_rif_fdb_op(rif->mlxsw_sp, rif->dev->dev_addr,
  5882. mlxsw_sp_fid_index(rif->fid), true);
  5883. if (err)
  5884. goto err_rif_fdb_op;
  5885. mlxsw_sp_fid_rif_set(rif->fid, rif);
  5886. return 0;
  5887. err_rif_fdb_op:
  5888. mlxsw_sp_rif_subport_op(rif, false);
  5889. return err;
  5890. }
  5891. static void mlxsw_sp_rif_subport_deconfigure(struct mlxsw_sp_rif *rif)
  5892. {
  5893. struct mlxsw_sp_fid *fid = rif->fid;
  5894. mlxsw_sp_fid_rif_set(fid, NULL);
  5895. mlxsw_sp_rif_fdb_op(rif->mlxsw_sp, rif->dev->dev_addr,
  5896. mlxsw_sp_fid_index(fid), false);
  5897. mlxsw_sp_rif_macvlan_flush(rif);
  5898. mlxsw_sp_rif_subport_op(rif, false);
  5899. }
  5900. static struct mlxsw_sp_fid *
  5901. mlxsw_sp_rif_subport_fid_get(struct mlxsw_sp_rif *rif,
  5902. struct netlink_ext_ack *extack)
  5903. {
  5904. return mlxsw_sp_fid_rfid_get(rif->mlxsw_sp, rif->rif_index);
  5905. }
  5906. static const struct mlxsw_sp_rif_ops mlxsw_sp_rif_subport_ops = {
  5907. .type = MLXSW_SP_RIF_TYPE_SUBPORT,
  5908. .rif_size = sizeof(struct mlxsw_sp_rif_subport),
  5909. .setup = mlxsw_sp_rif_subport_setup,
  5910. .configure = mlxsw_sp_rif_subport_configure,
  5911. .deconfigure = mlxsw_sp_rif_subport_deconfigure,
  5912. .fid_get = mlxsw_sp_rif_subport_fid_get,
  5913. };
  5914. static int mlxsw_sp_rif_vlan_fid_op(struct mlxsw_sp_rif *rif,
  5915. enum mlxsw_reg_ritr_if_type type,
  5916. u16 vid_fid, bool enable)
  5917. {
  5918. struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
  5919. char ritr_pl[MLXSW_REG_RITR_LEN];
  5920. mlxsw_reg_ritr_pack(ritr_pl, enable, type, rif->rif_index, rif->vr_id,
  5921. rif->dev->mtu);
  5922. mlxsw_reg_ritr_mac_pack(ritr_pl, rif->dev->dev_addr);
  5923. mlxsw_reg_ritr_fid_set(ritr_pl, type, vid_fid);
  5924. return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
  5925. }
  5926. u8 mlxsw_sp_router_port(const struct mlxsw_sp *mlxsw_sp)
  5927. {
  5928. return mlxsw_core_max_ports(mlxsw_sp->core) + 1;
  5929. }
  5930. static int mlxsw_sp_rif_vlan_configure(struct mlxsw_sp_rif *rif)
  5931. {
  5932. struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
  5933. u16 vid = mlxsw_sp_fid_8021q_vid(rif->fid);
  5934. int err;
  5935. err = mlxsw_sp_rif_vlan_fid_op(rif, MLXSW_REG_RITR_VLAN_IF, vid, true);
  5936. if (err)
  5937. return err;
  5938. err = mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_MC,
  5939. mlxsw_sp_router_port(mlxsw_sp), true);
  5940. if (err)
  5941. goto err_fid_mc_flood_set;
  5942. err = mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_BC,
  5943. mlxsw_sp_router_port(mlxsw_sp), true);
  5944. if (err)
  5945. goto err_fid_bc_flood_set;
  5946. err = mlxsw_sp_rif_fdb_op(rif->mlxsw_sp, rif->dev->dev_addr,
  5947. mlxsw_sp_fid_index(rif->fid), true);
  5948. if (err)
  5949. goto err_rif_fdb_op;
  5950. mlxsw_sp_fid_rif_set(rif->fid, rif);
  5951. return 0;
  5952. err_rif_fdb_op:
  5953. mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_BC,
  5954. mlxsw_sp_router_port(mlxsw_sp), false);
  5955. err_fid_bc_flood_set:
  5956. mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_MC,
  5957. mlxsw_sp_router_port(mlxsw_sp), false);
  5958. err_fid_mc_flood_set:
  5959. mlxsw_sp_rif_vlan_fid_op(rif, MLXSW_REG_RITR_VLAN_IF, vid, false);
  5960. return err;
  5961. }
  5962. static void mlxsw_sp_rif_vlan_deconfigure(struct mlxsw_sp_rif *rif)
  5963. {
  5964. u16 vid = mlxsw_sp_fid_8021q_vid(rif->fid);
  5965. struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
  5966. struct mlxsw_sp_fid *fid = rif->fid;
  5967. mlxsw_sp_fid_rif_set(fid, NULL);
  5968. mlxsw_sp_rif_fdb_op(rif->mlxsw_sp, rif->dev->dev_addr,
  5969. mlxsw_sp_fid_index(fid), false);
  5970. mlxsw_sp_rif_macvlan_flush(rif);
  5971. mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_BC,
  5972. mlxsw_sp_router_port(mlxsw_sp), false);
  5973. mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_MC,
  5974. mlxsw_sp_router_port(mlxsw_sp), false);
  5975. mlxsw_sp_rif_vlan_fid_op(rif, MLXSW_REG_RITR_VLAN_IF, vid, false);
  5976. }
  5977. static struct mlxsw_sp_fid *
  5978. mlxsw_sp_rif_vlan_fid_get(struct mlxsw_sp_rif *rif,
  5979. struct netlink_ext_ack *extack)
  5980. {
  5981. u16 vid;
  5982. int err;
  5983. if (is_vlan_dev(rif->dev)) {
  5984. vid = vlan_dev_vlan_id(rif->dev);
  5985. } else {
  5986. err = br_vlan_get_pvid(rif->dev, &vid);
  5987. if (err < 0 || !vid) {
  5988. NL_SET_ERR_MSG_MOD(extack, "Couldn't determine bridge PVID");
  5989. return ERR_PTR(-EINVAL);
  5990. }
  5991. }
  5992. return mlxsw_sp_fid_8021q_get(rif->mlxsw_sp, vid);
  5993. }
  5994. static void mlxsw_sp_rif_vlan_fdb_del(struct mlxsw_sp_rif *rif, const char *mac)
  5995. {
  5996. u16 vid = mlxsw_sp_fid_8021q_vid(rif->fid);
  5997. struct switchdev_notifier_fdb_info info;
  5998. struct net_device *br_dev;
  5999. struct net_device *dev;
  6000. br_dev = is_vlan_dev(rif->dev) ? vlan_dev_real_dev(rif->dev) : rif->dev;
  6001. dev = br_fdb_find_port(br_dev, mac, vid);
  6002. if (!dev)
  6003. return;
  6004. info.addr = mac;
  6005. info.vid = vid;
  6006. call_switchdev_notifiers(SWITCHDEV_FDB_DEL_TO_BRIDGE, dev, &info.info);
  6007. }
  6008. static const struct mlxsw_sp_rif_ops mlxsw_sp_rif_vlan_ops = {
  6009. .type = MLXSW_SP_RIF_TYPE_VLAN,
  6010. .rif_size = sizeof(struct mlxsw_sp_rif),
  6011. .configure = mlxsw_sp_rif_vlan_configure,
  6012. .deconfigure = mlxsw_sp_rif_vlan_deconfigure,
  6013. .fid_get = mlxsw_sp_rif_vlan_fid_get,
  6014. .fdb_del = mlxsw_sp_rif_vlan_fdb_del,
  6015. };
  6016. static int mlxsw_sp_rif_fid_configure(struct mlxsw_sp_rif *rif)
  6017. {
  6018. struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
  6019. u16 fid_index = mlxsw_sp_fid_index(rif->fid);
  6020. int err;
  6021. err = mlxsw_sp_rif_vlan_fid_op(rif, MLXSW_REG_RITR_FID_IF, fid_index,
  6022. true);
  6023. if (err)
  6024. return err;
  6025. err = mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_MC,
  6026. mlxsw_sp_router_port(mlxsw_sp), true);
  6027. if (err)
  6028. goto err_fid_mc_flood_set;
  6029. err = mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_BC,
  6030. mlxsw_sp_router_port(mlxsw_sp), true);
  6031. if (err)
  6032. goto err_fid_bc_flood_set;
  6033. err = mlxsw_sp_rif_fdb_op(rif->mlxsw_sp, rif->dev->dev_addr,
  6034. mlxsw_sp_fid_index(rif->fid), true);
  6035. if (err)
  6036. goto err_rif_fdb_op;
  6037. mlxsw_sp_fid_rif_set(rif->fid, rif);
  6038. return 0;
  6039. err_rif_fdb_op:
  6040. mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_BC,
  6041. mlxsw_sp_router_port(mlxsw_sp), false);
  6042. err_fid_bc_flood_set:
  6043. mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_MC,
  6044. mlxsw_sp_router_port(mlxsw_sp), false);
  6045. err_fid_mc_flood_set:
  6046. mlxsw_sp_rif_vlan_fid_op(rif, MLXSW_REG_RITR_FID_IF, fid_index, false);
  6047. return err;
  6048. }
  6049. static void mlxsw_sp_rif_fid_deconfigure(struct mlxsw_sp_rif *rif)
  6050. {
  6051. u16 fid_index = mlxsw_sp_fid_index(rif->fid);
  6052. struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
  6053. struct mlxsw_sp_fid *fid = rif->fid;
  6054. mlxsw_sp_fid_rif_set(fid, NULL);
  6055. mlxsw_sp_rif_fdb_op(rif->mlxsw_sp, rif->dev->dev_addr,
  6056. mlxsw_sp_fid_index(fid), false);
  6057. mlxsw_sp_rif_macvlan_flush(rif);
  6058. mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_BC,
  6059. mlxsw_sp_router_port(mlxsw_sp), false);
  6060. mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_MC,
  6061. mlxsw_sp_router_port(mlxsw_sp), false);
  6062. mlxsw_sp_rif_vlan_fid_op(rif, MLXSW_REG_RITR_FID_IF, fid_index, false);
  6063. }
  6064. static struct mlxsw_sp_fid *
  6065. mlxsw_sp_rif_fid_fid_get(struct mlxsw_sp_rif *rif,
  6066. struct netlink_ext_ack *extack)
  6067. {
  6068. return mlxsw_sp_fid_8021d_get(rif->mlxsw_sp, rif->dev->ifindex);
  6069. }
  6070. static void mlxsw_sp_rif_fid_fdb_del(struct mlxsw_sp_rif *rif, const char *mac)
  6071. {
  6072. struct switchdev_notifier_fdb_info info;
  6073. struct net_device *dev;
  6074. dev = br_fdb_find_port(rif->dev, mac, 0);
  6075. if (!dev)
  6076. return;
  6077. info.addr = mac;
  6078. info.vid = 0;
  6079. call_switchdev_notifiers(SWITCHDEV_FDB_DEL_TO_BRIDGE, dev, &info.info);
  6080. }
  6081. static const struct mlxsw_sp_rif_ops mlxsw_sp_rif_fid_ops = {
  6082. .type = MLXSW_SP_RIF_TYPE_FID,
  6083. .rif_size = sizeof(struct mlxsw_sp_rif),
  6084. .configure = mlxsw_sp_rif_fid_configure,
  6085. .deconfigure = mlxsw_sp_rif_fid_deconfigure,
  6086. .fid_get = mlxsw_sp_rif_fid_fid_get,
  6087. .fdb_del = mlxsw_sp_rif_fid_fdb_del,
  6088. };
  6089. static struct mlxsw_sp_rif_ipip_lb *
  6090. mlxsw_sp_rif_ipip_lb_rif(struct mlxsw_sp_rif *rif)
  6091. {
  6092. return container_of(rif, struct mlxsw_sp_rif_ipip_lb, common);
  6093. }
  6094. static void
  6095. mlxsw_sp_rif_ipip_lb_setup(struct mlxsw_sp_rif *rif,
  6096. const struct mlxsw_sp_rif_params *params)
  6097. {
  6098. struct mlxsw_sp_rif_params_ipip_lb *params_lb;
  6099. struct mlxsw_sp_rif_ipip_lb *rif_lb;
  6100. params_lb = container_of(params, struct mlxsw_sp_rif_params_ipip_lb,
  6101. common);
  6102. rif_lb = mlxsw_sp_rif_ipip_lb_rif(rif);
  6103. rif_lb->lb_config = params_lb->lb_config;
  6104. }
  6105. static int
  6106. mlxsw_sp_rif_ipip_lb_configure(struct mlxsw_sp_rif *rif)
  6107. {
  6108. struct mlxsw_sp_rif_ipip_lb *lb_rif = mlxsw_sp_rif_ipip_lb_rif(rif);
  6109. u32 ul_tb_id = mlxsw_sp_ipip_dev_ul_tb_id(rif->dev);
  6110. struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
  6111. struct mlxsw_sp_vr *ul_vr;
  6112. int err;
  6113. ul_vr = mlxsw_sp_vr_get(mlxsw_sp, ul_tb_id, NULL);
  6114. if (IS_ERR(ul_vr))
  6115. return PTR_ERR(ul_vr);
  6116. err = mlxsw_sp_rif_ipip_lb_op(lb_rif, ul_vr, true);
  6117. if (err)
  6118. goto err_loopback_op;
  6119. lb_rif->ul_vr_id = ul_vr->id;
  6120. ++ul_vr->rif_count;
  6121. return 0;
  6122. err_loopback_op:
  6123. mlxsw_sp_vr_put(mlxsw_sp, ul_vr);
  6124. return err;
  6125. }
  6126. static void mlxsw_sp_rif_ipip_lb_deconfigure(struct mlxsw_sp_rif *rif)
  6127. {
  6128. struct mlxsw_sp_rif_ipip_lb *lb_rif = mlxsw_sp_rif_ipip_lb_rif(rif);
  6129. struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
  6130. struct mlxsw_sp_vr *ul_vr;
  6131. ul_vr = &mlxsw_sp->router->vrs[lb_rif->ul_vr_id];
  6132. mlxsw_sp_rif_ipip_lb_op(lb_rif, ul_vr, false);
  6133. --ul_vr->rif_count;
  6134. mlxsw_sp_vr_put(mlxsw_sp, ul_vr);
  6135. }
  6136. static const struct mlxsw_sp_rif_ops mlxsw_sp_rif_ipip_lb_ops = {
  6137. .type = MLXSW_SP_RIF_TYPE_IPIP_LB,
  6138. .rif_size = sizeof(struct mlxsw_sp_rif_ipip_lb),
  6139. .setup = mlxsw_sp_rif_ipip_lb_setup,
  6140. .configure = mlxsw_sp_rif_ipip_lb_configure,
  6141. .deconfigure = mlxsw_sp_rif_ipip_lb_deconfigure,
  6142. };
  6143. static const struct mlxsw_sp_rif_ops *mlxsw_sp_rif_ops_arr[] = {
  6144. [MLXSW_SP_RIF_TYPE_SUBPORT] = &mlxsw_sp_rif_subport_ops,
  6145. [MLXSW_SP_RIF_TYPE_VLAN] = &mlxsw_sp_rif_vlan_ops,
  6146. [MLXSW_SP_RIF_TYPE_FID] = &mlxsw_sp_rif_fid_ops,
  6147. [MLXSW_SP_RIF_TYPE_IPIP_LB] = &mlxsw_sp_rif_ipip_lb_ops,
  6148. };
  6149. static int mlxsw_sp_rifs_init(struct mlxsw_sp *mlxsw_sp)
  6150. {
  6151. u64 max_rifs = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS);
  6152. mlxsw_sp->router->rifs = kcalloc(max_rifs,
  6153. sizeof(struct mlxsw_sp_rif *),
  6154. GFP_KERNEL);
  6155. if (!mlxsw_sp->router->rifs)
  6156. return -ENOMEM;
  6157. mlxsw_sp->router->rif_ops_arr = mlxsw_sp_rif_ops_arr;
  6158. return 0;
  6159. }
  6160. static void mlxsw_sp_rifs_fini(struct mlxsw_sp *mlxsw_sp)
  6161. {
  6162. int i;
  6163. for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS); i++)
  6164. WARN_ON_ONCE(mlxsw_sp->router->rifs[i]);
  6165. kfree(mlxsw_sp->router->rifs);
  6166. }
  6167. static int
  6168. mlxsw_sp_ipip_config_tigcr(struct mlxsw_sp *mlxsw_sp)
  6169. {
  6170. char tigcr_pl[MLXSW_REG_TIGCR_LEN];
  6171. mlxsw_reg_tigcr_pack(tigcr_pl, true, 0);
  6172. return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(tigcr), tigcr_pl);
  6173. }
  6174. static int mlxsw_sp_ipips_init(struct mlxsw_sp *mlxsw_sp)
  6175. {
  6176. mlxsw_sp->router->ipip_ops_arr = mlxsw_sp_ipip_ops_arr;
  6177. INIT_LIST_HEAD(&mlxsw_sp->router->ipip_list);
  6178. return mlxsw_sp_ipip_config_tigcr(mlxsw_sp);
  6179. }
  6180. static void mlxsw_sp_ipips_fini(struct mlxsw_sp *mlxsw_sp)
  6181. {
  6182. WARN_ON(!list_empty(&mlxsw_sp->router->ipip_list));
  6183. }
  6184. static void mlxsw_sp_router_fib_dump_flush(struct notifier_block *nb)
  6185. {
  6186. struct mlxsw_sp_router *router;
  6187. /* Flush pending FIB notifications and then flush the device's
  6188. * table before requesting another dump. The FIB notification
  6189. * block is unregistered, so no need to take RTNL.
  6190. */
  6191. mlxsw_core_flush_owq();
  6192. router = container_of(nb, struct mlxsw_sp_router, fib_nb);
  6193. mlxsw_sp_router_fib_flush(router->mlxsw_sp);
  6194. }
  6195. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  6196. static void mlxsw_sp_mp_hash_header_set(char *recr2_pl, int header)
  6197. {
  6198. mlxsw_reg_recr2_outer_header_enables_set(recr2_pl, header, true);
  6199. }
  6200. static void mlxsw_sp_mp_hash_field_set(char *recr2_pl, int field)
  6201. {
  6202. mlxsw_reg_recr2_outer_header_fields_enable_set(recr2_pl, field, true);
  6203. }
  6204. static void mlxsw_sp_mp4_hash_init(char *recr2_pl)
  6205. {
  6206. bool only_l3 = !init_net.ipv4.sysctl_fib_multipath_hash_policy;
  6207. mlxsw_sp_mp_hash_header_set(recr2_pl,
  6208. MLXSW_REG_RECR2_IPV4_EN_NOT_TCP_NOT_UDP);
  6209. mlxsw_sp_mp_hash_header_set(recr2_pl, MLXSW_REG_RECR2_IPV4_EN_TCP_UDP);
  6210. mlxsw_reg_recr2_ipv4_sip_enable(recr2_pl);
  6211. mlxsw_reg_recr2_ipv4_dip_enable(recr2_pl);
  6212. if (only_l3)
  6213. return;
  6214. mlxsw_sp_mp_hash_header_set(recr2_pl, MLXSW_REG_RECR2_TCP_UDP_EN_IPV4);
  6215. mlxsw_sp_mp_hash_field_set(recr2_pl, MLXSW_REG_RECR2_IPV4_PROTOCOL);
  6216. mlxsw_sp_mp_hash_field_set(recr2_pl, MLXSW_REG_RECR2_TCP_UDP_SPORT);
  6217. mlxsw_sp_mp_hash_field_set(recr2_pl, MLXSW_REG_RECR2_TCP_UDP_DPORT);
  6218. }
  6219. static void mlxsw_sp_mp6_hash_init(char *recr2_pl)
  6220. {
  6221. bool only_l3 = !ip6_multipath_hash_policy(&init_net);
  6222. mlxsw_sp_mp_hash_header_set(recr2_pl,
  6223. MLXSW_REG_RECR2_IPV6_EN_NOT_TCP_NOT_UDP);
  6224. mlxsw_sp_mp_hash_header_set(recr2_pl, MLXSW_REG_RECR2_IPV6_EN_TCP_UDP);
  6225. mlxsw_reg_recr2_ipv6_sip_enable(recr2_pl);
  6226. mlxsw_reg_recr2_ipv6_dip_enable(recr2_pl);
  6227. mlxsw_sp_mp_hash_field_set(recr2_pl, MLXSW_REG_RECR2_IPV6_NEXT_HEADER);
  6228. if (only_l3) {
  6229. mlxsw_sp_mp_hash_field_set(recr2_pl,
  6230. MLXSW_REG_RECR2_IPV6_FLOW_LABEL);
  6231. } else {
  6232. mlxsw_sp_mp_hash_header_set(recr2_pl,
  6233. MLXSW_REG_RECR2_TCP_UDP_EN_IPV6);
  6234. mlxsw_sp_mp_hash_field_set(recr2_pl,
  6235. MLXSW_REG_RECR2_TCP_UDP_SPORT);
  6236. mlxsw_sp_mp_hash_field_set(recr2_pl,
  6237. MLXSW_REG_RECR2_TCP_UDP_DPORT);
  6238. }
  6239. }
  6240. static int mlxsw_sp_mp_hash_init(struct mlxsw_sp *mlxsw_sp)
  6241. {
  6242. char recr2_pl[MLXSW_REG_RECR2_LEN];
  6243. u32 seed;
  6244. get_random_bytes(&seed, sizeof(seed));
  6245. mlxsw_reg_recr2_pack(recr2_pl, seed);
  6246. mlxsw_sp_mp4_hash_init(recr2_pl);
  6247. mlxsw_sp_mp6_hash_init(recr2_pl);
  6248. return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(recr2), recr2_pl);
  6249. }
  6250. #else
  6251. static int mlxsw_sp_mp_hash_init(struct mlxsw_sp *mlxsw_sp)
  6252. {
  6253. return 0;
  6254. }
  6255. #endif
  6256. static int mlxsw_sp_dscp_init(struct mlxsw_sp *mlxsw_sp)
  6257. {
  6258. char rdpm_pl[MLXSW_REG_RDPM_LEN];
  6259. unsigned int i;
  6260. MLXSW_REG_ZERO(rdpm, rdpm_pl);
  6261. /* HW is determining switch priority based on DSCP-bits, but the
  6262. * kernel is still doing that based on the ToS. Since there's a
  6263. * mismatch in bits we need to make sure to translate the right
  6264. * value ToS would observe, skipping the 2 least-significant ECN bits.
  6265. */
  6266. for (i = 0; i < MLXSW_REG_RDPM_DSCP_ENTRY_REC_MAX_COUNT; i++)
  6267. mlxsw_reg_rdpm_pack(rdpm_pl, i, rt_tos2priority(i << 2));
  6268. return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rdpm), rdpm_pl);
  6269. }
  6270. static int __mlxsw_sp_router_init(struct mlxsw_sp *mlxsw_sp)
  6271. {
  6272. bool usp = init_net.ipv4.sysctl_ip_fwd_update_priority;
  6273. char rgcr_pl[MLXSW_REG_RGCR_LEN];
  6274. u64 max_rifs;
  6275. int err;
  6276. if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_RIFS))
  6277. return -EIO;
  6278. max_rifs = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS);
  6279. mlxsw_reg_rgcr_pack(rgcr_pl, true, true);
  6280. mlxsw_reg_rgcr_max_router_interfaces_set(rgcr_pl, max_rifs);
  6281. mlxsw_reg_rgcr_usp_set(rgcr_pl, usp);
  6282. err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rgcr), rgcr_pl);
  6283. if (err)
  6284. return err;
  6285. return 0;
  6286. }
  6287. static void __mlxsw_sp_router_fini(struct mlxsw_sp *mlxsw_sp)
  6288. {
  6289. char rgcr_pl[MLXSW_REG_RGCR_LEN];
  6290. mlxsw_reg_rgcr_pack(rgcr_pl, false, false);
  6291. mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rgcr), rgcr_pl);
  6292. }
  6293. int mlxsw_sp_router_init(struct mlxsw_sp *mlxsw_sp)
  6294. {
  6295. struct mlxsw_sp_router *router;
  6296. int err;
  6297. router = kzalloc(sizeof(*mlxsw_sp->router), GFP_KERNEL);
  6298. if (!router)
  6299. return -ENOMEM;
  6300. mlxsw_sp->router = router;
  6301. router->mlxsw_sp = mlxsw_sp;
  6302. INIT_LIST_HEAD(&mlxsw_sp->router->nexthop_neighs_list);
  6303. err = __mlxsw_sp_router_init(mlxsw_sp);
  6304. if (err)
  6305. goto err_router_init;
  6306. err = mlxsw_sp_rifs_init(mlxsw_sp);
  6307. if (err)
  6308. goto err_rifs_init;
  6309. err = mlxsw_sp_ipips_init(mlxsw_sp);
  6310. if (err)
  6311. goto err_ipips_init;
  6312. err = rhashtable_init(&mlxsw_sp->router->nexthop_ht,
  6313. &mlxsw_sp_nexthop_ht_params);
  6314. if (err)
  6315. goto err_nexthop_ht_init;
  6316. err = rhashtable_init(&mlxsw_sp->router->nexthop_group_ht,
  6317. &mlxsw_sp_nexthop_group_ht_params);
  6318. if (err)
  6319. goto err_nexthop_group_ht_init;
  6320. INIT_LIST_HEAD(&mlxsw_sp->router->nexthop_list);
  6321. err = mlxsw_sp_lpm_init(mlxsw_sp);
  6322. if (err)
  6323. goto err_lpm_init;
  6324. err = mlxsw_sp_mr_init(mlxsw_sp, &mlxsw_sp_mr_tcam_ops);
  6325. if (err)
  6326. goto err_mr_init;
  6327. err = mlxsw_sp_vrs_init(mlxsw_sp);
  6328. if (err)
  6329. goto err_vrs_init;
  6330. err = mlxsw_sp_neigh_init(mlxsw_sp);
  6331. if (err)
  6332. goto err_neigh_init;
  6333. mlxsw_sp->router->netevent_nb.notifier_call =
  6334. mlxsw_sp_router_netevent_event;
  6335. err = register_netevent_notifier(&mlxsw_sp->router->netevent_nb);
  6336. if (err)
  6337. goto err_register_netevent_notifier;
  6338. err = mlxsw_sp_mp_hash_init(mlxsw_sp);
  6339. if (err)
  6340. goto err_mp_hash_init;
  6341. err = mlxsw_sp_dscp_init(mlxsw_sp);
  6342. if (err)
  6343. goto err_dscp_init;
  6344. mlxsw_sp->router->fib_nb.notifier_call = mlxsw_sp_router_fib_event;
  6345. err = register_fib_notifier(&mlxsw_sp->router->fib_nb,
  6346. mlxsw_sp_router_fib_dump_flush);
  6347. if (err)
  6348. goto err_register_fib_notifier;
  6349. return 0;
  6350. err_register_fib_notifier:
  6351. err_dscp_init:
  6352. err_mp_hash_init:
  6353. unregister_netevent_notifier(&mlxsw_sp->router->netevent_nb);
  6354. err_register_netevent_notifier:
  6355. mlxsw_sp_neigh_fini(mlxsw_sp);
  6356. err_neigh_init:
  6357. mlxsw_sp_vrs_fini(mlxsw_sp);
  6358. err_vrs_init:
  6359. mlxsw_sp_mr_fini(mlxsw_sp);
  6360. err_mr_init:
  6361. mlxsw_sp_lpm_fini(mlxsw_sp);
  6362. err_lpm_init:
  6363. rhashtable_destroy(&mlxsw_sp->router->nexthop_group_ht);
  6364. err_nexthop_group_ht_init:
  6365. rhashtable_destroy(&mlxsw_sp->router->nexthop_ht);
  6366. err_nexthop_ht_init:
  6367. mlxsw_sp_ipips_fini(mlxsw_sp);
  6368. err_ipips_init:
  6369. mlxsw_sp_rifs_fini(mlxsw_sp);
  6370. err_rifs_init:
  6371. __mlxsw_sp_router_fini(mlxsw_sp);
  6372. err_router_init:
  6373. kfree(mlxsw_sp->router);
  6374. return err;
  6375. }
  6376. void mlxsw_sp_router_fini(struct mlxsw_sp *mlxsw_sp)
  6377. {
  6378. unregister_fib_notifier(&mlxsw_sp->router->fib_nb);
  6379. unregister_netevent_notifier(&mlxsw_sp->router->netevent_nb);
  6380. mlxsw_sp_neigh_fini(mlxsw_sp);
  6381. mlxsw_sp_vrs_fini(mlxsw_sp);
  6382. mlxsw_sp_mr_fini(mlxsw_sp);
  6383. mlxsw_sp_lpm_fini(mlxsw_sp);
  6384. rhashtable_destroy(&mlxsw_sp->router->nexthop_group_ht);
  6385. rhashtable_destroy(&mlxsw_sp->router->nexthop_ht);
  6386. mlxsw_sp_ipips_fini(mlxsw_sp);
  6387. mlxsw_sp_rifs_fini(mlxsw_sp);
  6388. __mlxsw_sp_router_fini(mlxsw_sp);
  6389. kfree(mlxsw_sp->router);
  6390. }