devlink.c 122 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816
  1. /*
  2. * net/core/devlink.c - Network physical/parent device Netlink interface
  3. *
  4. * Heavily inspired by net/wireless/
  5. * Copyright (c) 2016 Mellanox Technologies. All rights reserved.
  6. * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/types.h>
  16. #include <linux/slab.h>
  17. #include <linux/gfp.h>
  18. #include <linux/device.h>
  19. #include <linux/list.h>
  20. #include <linux/netdevice.h>
  21. #include <rdma/ib_verbs.h>
  22. #include <net/netlink.h>
  23. #include <net/genetlink.h>
  24. #include <net/rtnetlink.h>
  25. #include <net/net_namespace.h>
  26. #include <net/sock.h>
  27. #include <net/devlink.h>
  28. #define CREATE_TRACE_POINTS
  29. #include <trace/events/devlink.h>
  30. static struct devlink_dpipe_field devlink_dpipe_fields_ethernet[] = {
  31. {
  32. .name = "destination mac",
  33. .id = DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC,
  34. .bitwidth = 48,
  35. },
  36. };
  37. struct devlink_dpipe_header devlink_dpipe_header_ethernet = {
  38. .name = "ethernet",
  39. .id = DEVLINK_DPIPE_HEADER_ETHERNET,
  40. .fields = devlink_dpipe_fields_ethernet,
  41. .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ethernet),
  42. .global = true,
  43. };
  44. EXPORT_SYMBOL(devlink_dpipe_header_ethernet);
  45. static struct devlink_dpipe_field devlink_dpipe_fields_ipv4[] = {
  46. {
  47. .name = "destination ip",
  48. .id = DEVLINK_DPIPE_FIELD_IPV4_DST_IP,
  49. .bitwidth = 32,
  50. },
  51. };
  52. struct devlink_dpipe_header devlink_dpipe_header_ipv4 = {
  53. .name = "ipv4",
  54. .id = DEVLINK_DPIPE_HEADER_IPV4,
  55. .fields = devlink_dpipe_fields_ipv4,
  56. .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv4),
  57. .global = true,
  58. };
  59. EXPORT_SYMBOL(devlink_dpipe_header_ipv4);
  60. static struct devlink_dpipe_field devlink_dpipe_fields_ipv6[] = {
  61. {
  62. .name = "destination ip",
  63. .id = DEVLINK_DPIPE_FIELD_IPV6_DST_IP,
  64. .bitwidth = 128,
  65. },
  66. };
  67. struct devlink_dpipe_header devlink_dpipe_header_ipv6 = {
  68. .name = "ipv6",
  69. .id = DEVLINK_DPIPE_HEADER_IPV6,
  70. .fields = devlink_dpipe_fields_ipv6,
  71. .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv6),
  72. .global = true,
  73. };
  74. EXPORT_SYMBOL(devlink_dpipe_header_ipv6);
  75. EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwmsg);
  76. static LIST_HEAD(devlink_list);
  77. /* devlink_mutex
  78. *
  79. * An overall lock guarding every operation coming from userspace.
  80. * It also guards devlink devices list and it is taken when
  81. * driver registers/unregisters it.
  82. */
  83. static DEFINE_MUTEX(devlink_mutex);
  84. static struct net *devlink_net(const struct devlink *devlink)
  85. {
  86. return read_pnet(&devlink->_net);
  87. }
  88. static void devlink_net_set(struct devlink *devlink, struct net *net)
  89. {
  90. write_pnet(&devlink->_net, net);
  91. }
  92. static struct devlink *devlink_get_from_attrs(struct net *net,
  93. struct nlattr **attrs)
  94. {
  95. struct devlink *devlink;
  96. char *busname;
  97. char *devname;
  98. if (!attrs[DEVLINK_ATTR_BUS_NAME] || !attrs[DEVLINK_ATTR_DEV_NAME])
  99. return ERR_PTR(-EINVAL);
  100. busname = nla_data(attrs[DEVLINK_ATTR_BUS_NAME]);
  101. devname = nla_data(attrs[DEVLINK_ATTR_DEV_NAME]);
  102. list_for_each_entry(devlink, &devlink_list, list) {
  103. if (strcmp(devlink->dev->bus->name, busname) == 0 &&
  104. strcmp(dev_name(devlink->dev), devname) == 0 &&
  105. net_eq(devlink_net(devlink), net))
  106. return devlink;
  107. }
  108. return ERR_PTR(-ENODEV);
  109. }
  110. static struct devlink *devlink_get_from_info(struct genl_info *info)
  111. {
  112. return devlink_get_from_attrs(genl_info_net(info), info->attrs);
  113. }
  114. static struct devlink_port *devlink_port_get_by_index(struct devlink *devlink,
  115. int port_index)
  116. {
  117. struct devlink_port *devlink_port;
  118. list_for_each_entry(devlink_port, &devlink->port_list, list) {
  119. if (devlink_port->index == port_index)
  120. return devlink_port;
  121. }
  122. return NULL;
  123. }
  124. static bool devlink_port_index_exists(struct devlink *devlink, int port_index)
  125. {
  126. return devlink_port_get_by_index(devlink, port_index);
  127. }
  128. static struct devlink_port *devlink_port_get_from_attrs(struct devlink *devlink,
  129. struct nlattr **attrs)
  130. {
  131. if (attrs[DEVLINK_ATTR_PORT_INDEX]) {
  132. u32 port_index = nla_get_u32(attrs[DEVLINK_ATTR_PORT_INDEX]);
  133. struct devlink_port *devlink_port;
  134. devlink_port = devlink_port_get_by_index(devlink, port_index);
  135. if (!devlink_port)
  136. return ERR_PTR(-ENODEV);
  137. return devlink_port;
  138. }
  139. return ERR_PTR(-EINVAL);
  140. }
  141. static struct devlink_port *devlink_port_get_from_info(struct devlink *devlink,
  142. struct genl_info *info)
  143. {
  144. return devlink_port_get_from_attrs(devlink, info->attrs);
  145. }
  146. struct devlink_sb {
  147. struct list_head list;
  148. unsigned int index;
  149. u32 size;
  150. u16 ingress_pools_count;
  151. u16 egress_pools_count;
  152. u16 ingress_tc_count;
  153. u16 egress_tc_count;
  154. };
  155. static u16 devlink_sb_pool_count(struct devlink_sb *devlink_sb)
  156. {
  157. return devlink_sb->ingress_pools_count + devlink_sb->egress_pools_count;
  158. }
  159. static struct devlink_sb *devlink_sb_get_by_index(struct devlink *devlink,
  160. unsigned int sb_index)
  161. {
  162. struct devlink_sb *devlink_sb;
  163. list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
  164. if (devlink_sb->index == sb_index)
  165. return devlink_sb;
  166. }
  167. return NULL;
  168. }
  169. static bool devlink_sb_index_exists(struct devlink *devlink,
  170. unsigned int sb_index)
  171. {
  172. return devlink_sb_get_by_index(devlink, sb_index);
  173. }
  174. static struct devlink_sb *devlink_sb_get_from_attrs(struct devlink *devlink,
  175. struct nlattr **attrs)
  176. {
  177. if (attrs[DEVLINK_ATTR_SB_INDEX]) {
  178. u32 sb_index = nla_get_u32(attrs[DEVLINK_ATTR_SB_INDEX]);
  179. struct devlink_sb *devlink_sb;
  180. devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
  181. if (!devlink_sb)
  182. return ERR_PTR(-ENODEV);
  183. return devlink_sb;
  184. }
  185. return ERR_PTR(-EINVAL);
  186. }
  187. static struct devlink_sb *devlink_sb_get_from_info(struct devlink *devlink,
  188. struct genl_info *info)
  189. {
  190. return devlink_sb_get_from_attrs(devlink, info->attrs);
  191. }
  192. static int devlink_sb_pool_index_get_from_attrs(struct devlink_sb *devlink_sb,
  193. struct nlattr **attrs,
  194. u16 *p_pool_index)
  195. {
  196. u16 val;
  197. if (!attrs[DEVLINK_ATTR_SB_POOL_INDEX])
  198. return -EINVAL;
  199. val = nla_get_u16(attrs[DEVLINK_ATTR_SB_POOL_INDEX]);
  200. if (val >= devlink_sb_pool_count(devlink_sb))
  201. return -EINVAL;
  202. *p_pool_index = val;
  203. return 0;
  204. }
  205. static int devlink_sb_pool_index_get_from_info(struct devlink_sb *devlink_sb,
  206. struct genl_info *info,
  207. u16 *p_pool_index)
  208. {
  209. return devlink_sb_pool_index_get_from_attrs(devlink_sb, info->attrs,
  210. p_pool_index);
  211. }
  212. static int
  213. devlink_sb_pool_type_get_from_attrs(struct nlattr **attrs,
  214. enum devlink_sb_pool_type *p_pool_type)
  215. {
  216. u8 val;
  217. if (!attrs[DEVLINK_ATTR_SB_POOL_TYPE])
  218. return -EINVAL;
  219. val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_TYPE]);
  220. if (val != DEVLINK_SB_POOL_TYPE_INGRESS &&
  221. val != DEVLINK_SB_POOL_TYPE_EGRESS)
  222. return -EINVAL;
  223. *p_pool_type = val;
  224. return 0;
  225. }
  226. static int
  227. devlink_sb_pool_type_get_from_info(struct genl_info *info,
  228. enum devlink_sb_pool_type *p_pool_type)
  229. {
  230. return devlink_sb_pool_type_get_from_attrs(info->attrs, p_pool_type);
  231. }
  232. static int
  233. devlink_sb_th_type_get_from_attrs(struct nlattr **attrs,
  234. enum devlink_sb_threshold_type *p_th_type)
  235. {
  236. u8 val;
  237. if (!attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE])
  238. return -EINVAL;
  239. val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE]);
  240. if (val != DEVLINK_SB_THRESHOLD_TYPE_STATIC &&
  241. val != DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC)
  242. return -EINVAL;
  243. *p_th_type = val;
  244. return 0;
  245. }
  246. static int
  247. devlink_sb_th_type_get_from_info(struct genl_info *info,
  248. enum devlink_sb_threshold_type *p_th_type)
  249. {
  250. return devlink_sb_th_type_get_from_attrs(info->attrs, p_th_type);
  251. }
  252. static int
  253. devlink_sb_tc_index_get_from_attrs(struct devlink_sb *devlink_sb,
  254. struct nlattr **attrs,
  255. enum devlink_sb_pool_type pool_type,
  256. u16 *p_tc_index)
  257. {
  258. u16 val;
  259. if (!attrs[DEVLINK_ATTR_SB_TC_INDEX])
  260. return -EINVAL;
  261. val = nla_get_u16(attrs[DEVLINK_ATTR_SB_TC_INDEX]);
  262. if (pool_type == DEVLINK_SB_POOL_TYPE_INGRESS &&
  263. val >= devlink_sb->ingress_tc_count)
  264. return -EINVAL;
  265. if (pool_type == DEVLINK_SB_POOL_TYPE_EGRESS &&
  266. val >= devlink_sb->egress_tc_count)
  267. return -EINVAL;
  268. *p_tc_index = val;
  269. return 0;
  270. }
  271. static int
  272. devlink_sb_tc_index_get_from_info(struct devlink_sb *devlink_sb,
  273. struct genl_info *info,
  274. enum devlink_sb_pool_type pool_type,
  275. u16 *p_tc_index)
  276. {
  277. return devlink_sb_tc_index_get_from_attrs(devlink_sb, info->attrs,
  278. pool_type, p_tc_index);
  279. }
  280. struct devlink_region {
  281. struct devlink *devlink;
  282. struct list_head list;
  283. const char *name;
  284. struct list_head snapshot_list;
  285. u32 max_snapshots;
  286. u32 cur_snapshots;
  287. u64 size;
  288. };
  289. struct devlink_snapshot {
  290. struct list_head list;
  291. struct devlink_region *region;
  292. devlink_snapshot_data_dest_t *data_destructor;
  293. u64 data_len;
  294. u8 *data;
  295. u32 id;
  296. };
  297. static struct devlink_region *
  298. devlink_region_get_by_name(struct devlink *devlink, const char *region_name)
  299. {
  300. struct devlink_region *region;
  301. list_for_each_entry(region, &devlink->region_list, list)
  302. if (!strcmp(region->name, region_name))
  303. return region;
  304. return NULL;
  305. }
  306. static struct devlink_snapshot *
  307. devlink_region_snapshot_get_by_id(struct devlink_region *region, u32 id)
  308. {
  309. struct devlink_snapshot *snapshot;
  310. list_for_each_entry(snapshot, &region->snapshot_list, list)
  311. if (snapshot->id == id)
  312. return snapshot;
  313. return NULL;
  314. }
  315. static void devlink_region_snapshot_del(struct devlink_snapshot *snapshot)
  316. {
  317. snapshot->region->cur_snapshots--;
  318. list_del(&snapshot->list);
  319. (*snapshot->data_destructor)(snapshot->data);
  320. kfree(snapshot);
  321. }
  322. #define DEVLINK_NL_FLAG_NEED_DEVLINK BIT(0)
  323. #define DEVLINK_NL_FLAG_NEED_PORT BIT(1)
  324. #define DEVLINK_NL_FLAG_NEED_SB BIT(2)
  325. /* The per devlink instance lock is taken by default in the pre-doit
  326. * operation, yet several commands do not require this. The global
  327. * devlink lock is taken and protects from disruption by user-calls.
  328. */
  329. #define DEVLINK_NL_FLAG_NO_LOCK BIT(3)
  330. static int devlink_nl_pre_doit(const struct genl_ops *ops,
  331. struct sk_buff *skb, struct genl_info *info)
  332. {
  333. struct devlink *devlink;
  334. int err;
  335. mutex_lock(&devlink_mutex);
  336. devlink = devlink_get_from_info(info);
  337. if (IS_ERR(devlink)) {
  338. mutex_unlock(&devlink_mutex);
  339. return PTR_ERR(devlink);
  340. }
  341. if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
  342. mutex_lock(&devlink->lock);
  343. if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_DEVLINK) {
  344. info->user_ptr[0] = devlink;
  345. } else if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_PORT) {
  346. struct devlink_port *devlink_port;
  347. devlink_port = devlink_port_get_from_info(devlink, info);
  348. if (IS_ERR(devlink_port)) {
  349. err = PTR_ERR(devlink_port);
  350. goto unlock;
  351. }
  352. info->user_ptr[0] = devlink_port;
  353. }
  354. if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_SB) {
  355. struct devlink_sb *devlink_sb;
  356. devlink_sb = devlink_sb_get_from_info(devlink, info);
  357. if (IS_ERR(devlink_sb)) {
  358. err = PTR_ERR(devlink_sb);
  359. goto unlock;
  360. }
  361. info->user_ptr[1] = devlink_sb;
  362. }
  363. return 0;
  364. unlock:
  365. if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
  366. mutex_unlock(&devlink->lock);
  367. mutex_unlock(&devlink_mutex);
  368. return err;
  369. }
  370. static void devlink_nl_post_doit(const struct genl_ops *ops,
  371. struct sk_buff *skb, struct genl_info *info)
  372. {
  373. struct devlink *devlink;
  374. devlink = devlink_get_from_info(info);
  375. if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
  376. mutex_unlock(&devlink->lock);
  377. mutex_unlock(&devlink_mutex);
  378. }
  379. static struct genl_family devlink_nl_family;
  380. enum devlink_multicast_groups {
  381. DEVLINK_MCGRP_CONFIG,
  382. };
  383. static const struct genl_multicast_group devlink_nl_mcgrps[] = {
  384. [DEVLINK_MCGRP_CONFIG] = { .name = DEVLINK_GENL_MCGRP_CONFIG_NAME },
  385. };
  386. static int devlink_nl_put_handle(struct sk_buff *msg, struct devlink *devlink)
  387. {
  388. if (nla_put_string(msg, DEVLINK_ATTR_BUS_NAME, devlink->dev->bus->name))
  389. return -EMSGSIZE;
  390. if (nla_put_string(msg, DEVLINK_ATTR_DEV_NAME, dev_name(devlink->dev)))
  391. return -EMSGSIZE;
  392. return 0;
  393. }
  394. static int devlink_nl_fill(struct sk_buff *msg, struct devlink *devlink,
  395. enum devlink_command cmd, u32 portid,
  396. u32 seq, int flags)
  397. {
  398. void *hdr;
  399. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  400. if (!hdr)
  401. return -EMSGSIZE;
  402. if (devlink_nl_put_handle(msg, devlink))
  403. goto nla_put_failure;
  404. genlmsg_end(msg, hdr);
  405. return 0;
  406. nla_put_failure:
  407. genlmsg_cancel(msg, hdr);
  408. return -EMSGSIZE;
  409. }
  410. static void devlink_notify(struct devlink *devlink, enum devlink_command cmd)
  411. {
  412. struct sk_buff *msg;
  413. int err;
  414. WARN_ON(cmd != DEVLINK_CMD_NEW && cmd != DEVLINK_CMD_DEL);
  415. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  416. if (!msg)
  417. return;
  418. err = devlink_nl_fill(msg, devlink, cmd, 0, 0, 0);
  419. if (err) {
  420. nlmsg_free(msg);
  421. return;
  422. }
  423. genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
  424. msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
  425. }
  426. static int devlink_nl_port_attrs_put(struct sk_buff *msg,
  427. struct devlink_port *devlink_port)
  428. {
  429. struct devlink_port_attrs *attrs = &devlink_port->attrs;
  430. if (!attrs->set)
  431. return 0;
  432. if (nla_put_u16(msg, DEVLINK_ATTR_PORT_FLAVOUR, attrs->flavour))
  433. return -EMSGSIZE;
  434. if (nla_put_u32(msg, DEVLINK_ATTR_PORT_NUMBER, attrs->port_number))
  435. return -EMSGSIZE;
  436. if (!attrs->split)
  437. return 0;
  438. if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_GROUP, attrs->port_number))
  439. return -EMSGSIZE;
  440. if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_SUBPORT_NUMBER,
  441. attrs->split_subport_number))
  442. return -EMSGSIZE;
  443. return 0;
  444. }
  445. static int devlink_nl_port_fill(struct sk_buff *msg, struct devlink *devlink,
  446. struct devlink_port *devlink_port,
  447. enum devlink_command cmd, u32 portid,
  448. u32 seq, int flags)
  449. {
  450. void *hdr;
  451. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  452. if (!hdr)
  453. return -EMSGSIZE;
  454. if (devlink_nl_put_handle(msg, devlink))
  455. goto nla_put_failure;
  456. if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
  457. goto nla_put_failure;
  458. if (nla_put_u16(msg, DEVLINK_ATTR_PORT_TYPE, devlink_port->type))
  459. goto nla_put_failure;
  460. if (devlink_port->desired_type != DEVLINK_PORT_TYPE_NOTSET &&
  461. nla_put_u16(msg, DEVLINK_ATTR_PORT_DESIRED_TYPE,
  462. devlink_port->desired_type))
  463. goto nla_put_failure;
  464. if (devlink_port->type == DEVLINK_PORT_TYPE_ETH) {
  465. struct net_device *netdev = devlink_port->type_dev;
  466. if (netdev &&
  467. (nla_put_u32(msg, DEVLINK_ATTR_PORT_NETDEV_IFINDEX,
  468. netdev->ifindex) ||
  469. nla_put_string(msg, DEVLINK_ATTR_PORT_NETDEV_NAME,
  470. netdev->name)))
  471. goto nla_put_failure;
  472. }
  473. if (devlink_port->type == DEVLINK_PORT_TYPE_IB) {
  474. struct ib_device *ibdev = devlink_port->type_dev;
  475. if (ibdev &&
  476. nla_put_string(msg, DEVLINK_ATTR_PORT_IBDEV_NAME,
  477. ibdev->name))
  478. goto nla_put_failure;
  479. }
  480. if (devlink_nl_port_attrs_put(msg, devlink_port))
  481. goto nla_put_failure;
  482. genlmsg_end(msg, hdr);
  483. return 0;
  484. nla_put_failure:
  485. genlmsg_cancel(msg, hdr);
  486. return -EMSGSIZE;
  487. }
  488. static void devlink_port_notify(struct devlink_port *devlink_port,
  489. enum devlink_command cmd)
  490. {
  491. struct devlink *devlink = devlink_port->devlink;
  492. struct sk_buff *msg;
  493. int err;
  494. if (!devlink_port->registered)
  495. return;
  496. WARN_ON(cmd != DEVLINK_CMD_PORT_NEW && cmd != DEVLINK_CMD_PORT_DEL);
  497. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  498. if (!msg)
  499. return;
  500. err = devlink_nl_port_fill(msg, devlink, devlink_port, cmd, 0, 0, 0);
  501. if (err) {
  502. nlmsg_free(msg);
  503. return;
  504. }
  505. genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
  506. msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
  507. }
  508. static int devlink_nl_cmd_get_doit(struct sk_buff *skb, struct genl_info *info)
  509. {
  510. struct devlink *devlink = info->user_ptr[0];
  511. struct sk_buff *msg;
  512. int err;
  513. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  514. if (!msg)
  515. return -ENOMEM;
  516. err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
  517. info->snd_portid, info->snd_seq, 0);
  518. if (err) {
  519. nlmsg_free(msg);
  520. return err;
  521. }
  522. return genlmsg_reply(msg, info);
  523. }
  524. static int devlink_nl_cmd_get_dumpit(struct sk_buff *msg,
  525. struct netlink_callback *cb)
  526. {
  527. struct devlink *devlink;
  528. int start = cb->args[0];
  529. int idx = 0;
  530. int err;
  531. mutex_lock(&devlink_mutex);
  532. list_for_each_entry(devlink, &devlink_list, list) {
  533. if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
  534. continue;
  535. if (idx < start) {
  536. idx++;
  537. continue;
  538. }
  539. err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
  540. NETLINK_CB(cb->skb).portid,
  541. cb->nlh->nlmsg_seq, NLM_F_MULTI);
  542. if (err)
  543. goto out;
  544. idx++;
  545. }
  546. out:
  547. mutex_unlock(&devlink_mutex);
  548. cb->args[0] = idx;
  549. return msg->len;
  550. }
  551. static int devlink_nl_cmd_port_get_doit(struct sk_buff *skb,
  552. struct genl_info *info)
  553. {
  554. struct devlink_port *devlink_port = info->user_ptr[0];
  555. struct devlink *devlink = devlink_port->devlink;
  556. struct sk_buff *msg;
  557. int err;
  558. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  559. if (!msg)
  560. return -ENOMEM;
  561. err = devlink_nl_port_fill(msg, devlink, devlink_port,
  562. DEVLINK_CMD_PORT_NEW,
  563. info->snd_portid, info->snd_seq, 0);
  564. if (err) {
  565. nlmsg_free(msg);
  566. return err;
  567. }
  568. return genlmsg_reply(msg, info);
  569. }
  570. static int devlink_nl_cmd_port_get_dumpit(struct sk_buff *msg,
  571. struct netlink_callback *cb)
  572. {
  573. struct devlink *devlink;
  574. struct devlink_port *devlink_port;
  575. int start = cb->args[0];
  576. int idx = 0;
  577. int err;
  578. mutex_lock(&devlink_mutex);
  579. list_for_each_entry(devlink, &devlink_list, list) {
  580. if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
  581. continue;
  582. mutex_lock(&devlink->lock);
  583. list_for_each_entry(devlink_port, &devlink->port_list, list) {
  584. if (idx < start) {
  585. idx++;
  586. continue;
  587. }
  588. err = devlink_nl_port_fill(msg, devlink, devlink_port,
  589. DEVLINK_CMD_NEW,
  590. NETLINK_CB(cb->skb).portid,
  591. cb->nlh->nlmsg_seq,
  592. NLM_F_MULTI);
  593. if (err) {
  594. mutex_unlock(&devlink->lock);
  595. goto out;
  596. }
  597. idx++;
  598. }
  599. mutex_unlock(&devlink->lock);
  600. }
  601. out:
  602. mutex_unlock(&devlink_mutex);
  603. cb->args[0] = idx;
  604. return msg->len;
  605. }
  606. static int devlink_port_type_set(struct devlink *devlink,
  607. struct devlink_port *devlink_port,
  608. enum devlink_port_type port_type)
  609. {
  610. int err;
  611. if (devlink->ops && devlink->ops->port_type_set) {
  612. if (port_type == DEVLINK_PORT_TYPE_NOTSET)
  613. return -EINVAL;
  614. if (port_type == devlink_port->type)
  615. return 0;
  616. err = devlink->ops->port_type_set(devlink_port, port_type);
  617. if (err)
  618. return err;
  619. devlink_port->desired_type = port_type;
  620. devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
  621. return 0;
  622. }
  623. return -EOPNOTSUPP;
  624. }
  625. static int devlink_nl_cmd_port_set_doit(struct sk_buff *skb,
  626. struct genl_info *info)
  627. {
  628. struct devlink_port *devlink_port = info->user_ptr[0];
  629. struct devlink *devlink = devlink_port->devlink;
  630. int err;
  631. if (info->attrs[DEVLINK_ATTR_PORT_TYPE]) {
  632. enum devlink_port_type port_type;
  633. port_type = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_TYPE]);
  634. err = devlink_port_type_set(devlink, devlink_port, port_type);
  635. if (err)
  636. return err;
  637. }
  638. return 0;
  639. }
  640. static int devlink_port_split(struct devlink *devlink, u32 port_index,
  641. u32 count, struct netlink_ext_ack *extack)
  642. {
  643. if (devlink->ops && devlink->ops->port_split)
  644. return devlink->ops->port_split(devlink, port_index, count,
  645. extack);
  646. return -EOPNOTSUPP;
  647. }
  648. static int devlink_nl_cmd_port_split_doit(struct sk_buff *skb,
  649. struct genl_info *info)
  650. {
  651. struct devlink *devlink = info->user_ptr[0];
  652. u32 port_index;
  653. u32 count;
  654. if (!info->attrs[DEVLINK_ATTR_PORT_INDEX] ||
  655. !info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT])
  656. return -EINVAL;
  657. port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
  658. count = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT]);
  659. return devlink_port_split(devlink, port_index, count, info->extack);
  660. }
  661. static int devlink_port_unsplit(struct devlink *devlink, u32 port_index,
  662. struct netlink_ext_ack *extack)
  663. {
  664. if (devlink->ops && devlink->ops->port_unsplit)
  665. return devlink->ops->port_unsplit(devlink, port_index, extack);
  666. return -EOPNOTSUPP;
  667. }
  668. static int devlink_nl_cmd_port_unsplit_doit(struct sk_buff *skb,
  669. struct genl_info *info)
  670. {
  671. struct devlink *devlink = info->user_ptr[0];
  672. u32 port_index;
  673. if (!info->attrs[DEVLINK_ATTR_PORT_INDEX])
  674. return -EINVAL;
  675. port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
  676. return devlink_port_unsplit(devlink, port_index, info->extack);
  677. }
  678. static int devlink_nl_sb_fill(struct sk_buff *msg, struct devlink *devlink,
  679. struct devlink_sb *devlink_sb,
  680. enum devlink_command cmd, u32 portid,
  681. u32 seq, int flags)
  682. {
  683. void *hdr;
  684. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  685. if (!hdr)
  686. return -EMSGSIZE;
  687. if (devlink_nl_put_handle(msg, devlink))
  688. goto nla_put_failure;
  689. if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
  690. goto nla_put_failure;
  691. if (nla_put_u32(msg, DEVLINK_ATTR_SB_SIZE, devlink_sb->size))
  692. goto nla_put_failure;
  693. if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_POOL_COUNT,
  694. devlink_sb->ingress_pools_count))
  695. goto nla_put_failure;
  696. if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_POOL_COUNT,
  697. devlink_sb->egress_pools_count))
  698. goto nla_put_failure;
  699. if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_TC_COUNT,
  700. devlink_sb->ingress_tc_count))
  701. goto nla_put_failure;
  702. if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_TC_COUNT,
  703. devlink_sb->egress_tc_count))
  704. goto nla_put_failure;
  705. genlmsg_end(msg, hdr);
  706. return 0;
  707. nla_put_failure:
  708. genlmsg_cancel(msg, hdr);
  709. return -EMSGSIZE;
  710. }
  711. static int devlink_nl_cmd_sb_get_doit(struct sk_buff *skb,
  712. struct genl_info *info)
  713. {
  714. struct devlink *devlink = info->user_ptr[0];
  715. struct devlink_sb *devlink_sb = info->user_ptr[1];
  716. struct sk_buff *msg;
  717. int err;
  718. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  719. if (!msg)
  720. return -ENOMEM;
  721. err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
  722. DEVLINK_CMD_SB_NEW,
  723. info->snd_portid, info->snd_seq, 0);
  724. if (err) {
  725. nlmsg_free(msg);
  726. return err;
  727. }
  728. return genlmsg_reply(msg, info);
  729. }
  730. static int devlink_nl_cmd_sb_get_dumpit(struct sk_buff *msg,
  731. struct netlink_callback *cb)
  732. {
  733. struct devlink *devlink;
  734. struct devlink_sb *devlink_sb;
  735. int start = cb->args[0];
  736. int idx = 0;
  737. int err;
  738. mutex_lock(&devlink_mutex);
  739. list_for_each_entry(devlink, &devlink_list, list) {
  740. if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
  741. continue;
  742. mutex_lock(&devlink->lock);
  743. list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
  744. if (idx < start) {
  745. idx++;
  746. continue;
  747. }
  748. err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
  749. DEVLINK_CMD_SB_NEW,
  750. NETLINK_CB(cb->skb).portid,
  751. cb->nlh->nlmsg_seq,
  752. NLM_F_MULTI);
  753. if (err) {
  754. mutex_unlock(&devlink->lock);
  755. goto out;
  756. }
  757. idx++;
  758. }
  759. mutex_unlock(&devlink->lock);
  760. }
  761. out:
  762. mutex_unlock(&devlink_mutex);
  763. cb->args[0] = idx;
  764. return msg->len;
  765. }
  766. static int devlink_nl_sb_pool_fill(struct sk_buff *msg, struct devlink *devlink,
  767. struct devlink_sb *devlink_sb,
  768. u16 pool_index, enum devlink_command cmd,
  769. u32 portid, u32 seq, int flags)
  770. {
  771. struct devlink_sb_pool_info pool_info;
  772. void *hdr;
  773. int err;
  774. err = devlink->ops->sb_pool_get(devlink, devlink_sb->index,
  775. pool_index, &pool_info);
  776. if (err)
  777. return err;
  778. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  779. if (!hdr)
  780. return -EMSGSIZE;
  781. if (devlink_nl_put_handle(msg, devlink))
  782. goto nla_put_failure;
  783. if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
  784. goto nla_put_failure;
  785. if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
  786. goto nla_put_failure;
  787. if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_info.pool_type))
  788. goto nla_put_failure;
  789. if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_SIZE, pool_info.size))
  790. goto nla_put_failure;
  791. if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE,
  792. pool_info.threshold_type))
  793. goto nla_put_failure;
  794. genlmsg_end(msg, hdr);
  795. return 0;
  796. nla_put_failure:
  797. genlmsg_cancel(msg, hdr);
  798. return -EMSGSIZE;
  799. }
  800. static int devlink_nl_cmd_sb_pool_get_doit(struct sk_buff *skb,
  801. struct genl_info *info)
  802. {
  803. struct devlink *devlink = info->user_ptr[0];
  804. struct devlink_sb *devlink_sb = info->user_ptr[1];
  805. struct sk_buff *msg;
  806. u16 pool_index;
  807. int err;
  808. err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
  809. &pool_index);
  810. if (err)
  811. return err;
  812. if (!devlink->ops || !devlink->ops->sb_pool_get)
  813. return -EOPNOTSUPP;
  814. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  815. if (!msg)
  816. return -ENOMEM;
  817. err = devlink_nl_sb_pool_fill(msg, devlink, devlink_sb, pool_index,
  818. DEVLINK_CMD_SB_POOL_NEW,
  819. info->snd_portid, info->snd_seq, 0);
  820. if (err) {
  821. nlmsg_free(msg);
  822. return err;
  823. }
  824. return genlmsg_reply(msg, info);
  825. }
  826. static int __sb_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
  827. struct devlink *devlink,
  828. struct devlink_sb *devlink_sb,
  829. u32 portid, u32 seq)
  830. {
  831. u16 pool_count = devlink_sb_pool_count(devlink_sb);
  832. u16 pool_index;
  833. int err;
  834. for (pool_index = 0; pool_index < pool_count; pool_index++) {
  835. if (*p_idx < start) {
  836. (*p_idx)++;
  837. continue;
  838. }
  839. err = devlink_nl_sb_pool_fill(msg, devlink,
  840. devlink_sb,
  841. pool_index,
  842. DEVLINK_CMD_SB_POOL_NEW,
  843. portid, seq, NLM_F_MULTI);
  844. if (err)
  845. return err;
  846. (*p_idx)++;
  847. }
  848. return 0;
  849. }
  850. static int devlink_nl_cmd_sb_pool_get_dumpit(struct sk_buff *msg,
  851. struct netlink_callback *cb)
  852. {
  853. struct devlink *devlink;
  854. struct devlink_sb *devlink_sb;
  855. int start = cb->args[0];
  856. int idx = 0;
  857. int err;
  858. mutex_lock(&devlink_mutex);
  859. list_for_each_entry(devlink, &devlink_list, list) {
  860. if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
  861. !devlink->ops || !devlink->ops->sb_pool_get)
  862. continue;
  863. mutex_lock(&devlink->lock);
  864. list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
  865. err = __sb_pool_get_dumpit(msg, start, &idx, devlink,
  866. devlink_sb,
  867. NETLINK_CB(cb->skb).portid,
  868. cb->nlh->nlmsg_seq);
  869. if (err && err != -EOPNOTSUPP) {
  870. mutex_unlock(&devlink->lock);
  871. goto out;
  872. }
  873. }
  874. mutex_unlock(&devlink->lock);
  875. }
  876. out:
  877. mutex_unlock(&devlink_mutex);
  878. cb->args[0] = idx;
  879. return msg->len;
  880. }
  881. static int devlink_sb_pool_set(struct devlink *devlink, unsigned int sb_index,
  882. u16 pool_index, u32 size,
  883. enum devlink_sb_threshold_type threshold_type)
  884. {
  885. const struct devlink_ops *ops = devlink->ops;
  886. if (ops && ops->sb_pool_set)
  887. return ops->sb_pool_set(devlink, sb_index, pool_index,
  888. size, threshold_type);
  889. return -EOPNOTSUPP;
  890. }
  891. static int devlink_nl_cmd_sb_pool_set_doit(struct sk_buff *skb,
  892. struct genl_info *info)
  893. {
  894. struct devlink *devlink = info->user_ptr[0];
  895. struct devlink_sb *devlink_sb = info->user_ptr[1];
  896. enum devlink_sb_threshold_type threshold_type;
  897. u16 pool_index;
  898. u32 size;
  899. int err;
  900. err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
  901. &pool_index);
  902. if (err)
  903. return err;
  904. err = devlink_sb_th_type_get_from_info(info, &threshold_type);
  905. if (err)
  906. return err;
  907. if (!info->attrs[DEVLINK_ATTR_SB_POOL_SIZE])
  908. return -EINVAL;
  909. size = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_POOL_SIZE]);
  910. return devlink_sb_pool_set(devlink, devlink_sb->index,
  911. pool_index, size, threshold_type);
  912. }
  913. static int devlink_nl_sb_port_pool_fill(struct sk_buff *msg,
  914. struct devlink *devlink,
  915. struct devlink_port *devlink_port,
  916. struct devlink_sb *devlink_sb,
  917. u16 pool_index,
  918. enum devlink_command cmd,
  919. u32 portid, u32 seq, int flags)
  920. {
  921. const struct devlink_ops *ops = devlink->ops;
  922. u32 threshold;
  923. void *hdr;
  924. int err;
  925. err = ops->sb_port_pool_get(devlink_port, devlink_sb->index,
  926. pool_index, &threshold);
  927. if (err)
  928. return err;
  929. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  930. if (!hdr)
  931. return -EMSGSIZE;
  932. if (devlink_nl_put_handle(msg, devlink))
  933. goto nla_put_failure;
  934. if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
  935. goto nla_put_failure;
  936. if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
  937. goto nla_put_failure;
  938. if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
  939. goto nla_put_failure;
  940. if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
  941. goto nla_put_failure;
  942. if (ops->sb_occ_port_pool_get) {
  943. u32 cur;
  944. u32 max;
  945. err = ops->sb_occ_port_pool_get(devlink_port, devlink_sb->index,
  946. pool_index, &cur, &max);
  947. if (err && err != -EOPNOTSUPP)
  948. goto sb_occ_get_failure;
  949. if (!err) {
  950. if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
  951. goto nla_put_failure;
  952. if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
  953. goto nla_put_failure;
  954. }
  955. }
  956. genlmsg_end(msg, hdr);
  957. return 0;
  958. nla_put_failure:
  959. err = -EMSGSIZE;
  960. sb_occ_get_failure:
  961. genlmsg_cancel(msg, hdr);
  962. return err;
  963. }
  964. static int devlink_nl_cmd_sb_port_pool_get_doit(struct sk_buff *skb,
  965. struct genl_info *info)
  966. {
  967. struct devlink_port *devlink_port = info->user_ptr[0];
  968. struct devlink *devlink = devlink_port->devlink;
  969. struct devlink_sb *devlink_sb = info->user_ptr[1];
  970. struct sk_buff *msg;
  971. u16 pool_index;
  972. int err;
  973. err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
  974. &pool_index);
  975. if (err)
  976. return err;
  977. if (!devlink->ops || !devlink->ops->sb_port_pool_get)
  978. return -EOPNOTSUPP;
  979. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  980. if (!msg)
  981. return -ENOMEM;
  982. err = devlink_nl_sb_port_pool_fill(msg, devlink, devlink_port,
  983. devlink_sb, pool_index,
  984. DEVLINK_CMD_SB_PORT_POOL_NEW,
  985. info->snd_portid, info->snd_seq, 0);
  986. if (err) {
  987. nlmsg_free(msg);
  988. return err;
  989. }
  990. return genlmsg_reply(msg, info);
  991. }
  992. static int __sb_port_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
  993. struct devlink *devlink,
  994. struct devlink_sb *devlink_sb,
  995. u32 portid, u32 seq)
  996. {
  997. struct devlink_port *devlink_port;
  998. u16 pool_count = devlink_sb_pool_count(devlink_sb);
  999. u16 pool_index;
  1000. int err;
  1001. list_for_each_entry(devlink_port, &devlink->port_list, list) {
  1002. for (pool_index = 0; pool_index < pool_count; pool_index++) {
  1003. if (*p_idx < start) {
  1004. (*p_idx)++;
  1005. continue;
  1006. }
  1007. err = devlink_nl_sb_port_pool_fill(msg, devlink,
  1008. devlink_port,
  1009. devlink_sb,
  1010. pool_index,
  1011. DEVLINK_CMD_SB_PORT_POOL_NEW,
  1012. portid, seq,
  1013. NLM_F_MULTI);
  1014. if (err)
  1015. return err;
  1016. (*p_idx)++;
  1017. }
  1018. }
  1019. return 0;
  1020. }
  1021. static int devlink_nl_cmd_sb_port_pool_get_dumpit(struct sk_buff *msg,
  1022. struct netlink_callback *cb)
  1023. {
  1024. struct devlink *devlink;
  1025. struct devlink_sb *devlink_sb;
  1026. int start = cb->args[0];
  1027. int idx = 0;
  1028. int err;
  1029. mutex_lock(&devlink_mutex);
  1030. list_for_each_entry(devlink, &devlink_list, list) {
  1031. if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
  1032. !devlink->ops || !devlink->ops->sb_port_pool_get)
  1033. continue;
  1034. mutex_lock(&devlink->lock);
  1035. list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
  1036. err = __sb_port_pool_get_dumpit(msg, start, &idx,
  1037. devlink, devlink_sb,
  1038. NETLINK_CB(cb->skb).portid,
  1039. cb->nlh->nlmsg_seq);
  1040. if (err && err != -EOPNOTSUPP) {
  1041. mutex_unlock(&devlink->lock);
  1042. goto out;
  1043. }
  1044. }
  1045. mutex_unlock(&devlink->lock);
  1046. }
  1047. out:
  1048. mutex_unlock(&devlink_mutex);
  1049. cb->args[0] = idx;
  1050. return msg->len;
  1051. }
  1052. static int devlink_sb_port_pool_set(struct devlink_port *devlink_port,
  1053. unsigned int sb_index, u16 pool_index,
  1054. u32 threshold)
  1055. {
  1056. const struct devlink_ops *ops = devlink_port->devlink->ops;
  1057. if (ops && ops->sb_port_pool_set)
  1058. return ops->sb_port_pool_set(devlink_port, sb_index,
  1059. pool_index, threshold);
  1060. return -EOPNOTSUPP;
  1061. }
  1062. static int devlink_nl_cmd_sb_port_pool_set_doit(struct sk_buff *skb,
  1063. struct genl_info *info)
  1064. {
  1065. struct devlink_port *devlink_port = info->user_ptr[0];
  1066. struct devlink_sb *devlink_sb = info->user_ptr[1];
  1067. u16 pool_index;
  1068. u32 threshold;
  1069. int err;
  1070. err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
  1071. &pool_index);
  1072. if (err)
  1073. return err;
  1074. if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
  1075. return -EINVAL;
  1076. threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
  1077. return devlink_sb_port_pool_set(devlink_port, devlink_sb->index,
  1078. pool_index, threshold);
  1079. }
  1080. static int
  1081. devlink_nl_sb_tc_pool_bind_fill(struct sk_buff *msg, struct devlink *devlink,
  1082. struct devlink_port *devlink_port,
  1083. struct devlink_sb *devlink_sb, u16 tc_index,
  1084. enum devlink_sb_pool_type pool_type,
  1085. enum devlink_command cmd,
  1086. u32 portid, u32 seq, int flags)
  1087. {
  1088. const struct devlink_ops *ops = devlink->ops;
  1089. u16 pool_index;
  1090. u32 threshold;
  1091. void *hdr;
  1092. int err;
  1093. err = ops->sb_tc_pool_bind_get(devlink_port, devlink_sb->index,
  1094. tc_index, pool_type,
  1095. &pool_index, &threshold);
  1096. if (err)
  1097. return err;
  1098. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  1099. if (!hdr)
  1100. return -EMSGSIZE;
  1101. if (devlink_nl_put_handle(msg, devlink))
  1102. goto nla_put_failure;
  1103. if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
  1104. goto nla_put_failure;
  1105. if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
  1106. goto nla_put_failure;
  1107. if (nla_put_u16(msg, DEVLINK_ATTR_SB_TC_INDEX, tc_index))
  1108. goto nla_put_failure;
  1109. if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_type))
  1110. goto nla_put_failure;
  1111. if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
  1112. goto nla_put_failure;
  1113. if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
  1114. goto nla_put_failure;
  1115. if (ops->sb_occ_tc_port_bind_get) {
  1116. u32 cur;
  1117. u32 max;
  1118. err = ops->sb_occ_tc_port_bind_get(devlink_port,
  1119. devlink_sb->index,
  1120. tc_index, pool_type,
  1121. &cur, &max);
  1122. if (err && err != -EOPNOTSUPP)
  1123. return err;
  1124. if (!err) {
  1125. if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
  1126. goto nla_put_failure;
  1127. if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
  1128. goto nla_put_failure;
  1129. }
  1130. }
  1131. genlmsg_end(msg, hdr);
  1132. return 0;
  1133. nla_put_failure:
  1134. genlmsg_cancel(msg, hdr);
  1135. return -EMSGSIZE;
  1136. }
  1137. static int devlink_nl_cmd_sb_tc_pool_bind_get_doit(struct sk_buff *skb,
  1138. struct genl_info *info)
  1139. {
  1140. struct devlink_port *devlink_port = info->user_ptr[0];
  1141. struct devlink *devlink = devlink_port->devlink;
  1142. struct devlink_sb *devlink_sb = info->user_ptr[1];
  1143. struct sk_buff *msg;
  1144. enum devlink_sb_pool_type pool_type;
  1145. u16 tc_index;
  1146. int err;
  1147. err = devlink_sb_pool_type_get_from_info(info, &pool_type);
  1148. if (err)
  1149. return err;
  1150. err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
  1151. pool_type, &tc_index);
  1152. if (err)
  1153. return err;
  1154. if (!devlink->ops || !devlink->ops->sb_tc_pool_bind_get)
  1155. return -EOPNOTSUPP;
  1156. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1157. if (!msg)
  1158. return -ENOMEM;
  1159. err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink, devlink_port,
  1160. devlink_sb, tc_index, pool_type,
  1161. DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
  1162. info->snd_portid,
  1163. info->snd_seq, 0);
  1164. if (err) {
  1165. nlmsg_free(msg);
  1166. return err;
  1167. }
  1168. return genlmsg_reply(msg, info);
  1169. }
  1170. static int __sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
  1171. int start, int *p_idx,
  1172. struct devlink *devlink,
  1173. struct devlink_sb *devlink_sb,
  1174. u32 portid, u32 seq)
  1175. {
  1176. struct devlink_port *devlink_port;
  1177. u16 tc_index;
  1178. int err;
  1179. list_for_each_entry(devlink_port, &devlink->port_list, list) {
  1180. for (tc_index = 0;
  1181. tc_index < devlink_sb->ingress_tc_count; tc_index++) {
  1182. if (*p_idx < start) {
  1183. (*p_idx)++;
  1184. continue;
  1185. }
  1186. err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
  1187. devlink_port,
  1188. devlink_sb,
  1189. tc_index,
  1190. DEVLINK_SB_POOL_TYPE_INGRESS,
  1191. DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
  1192. portid, seq,
  1193. NLM_F_MULTI);
  1194. if (err)
  1195. return err;
  1196. (*p_idx)++;
  1197. }
  1198. for (tc_index = 0;
  1199. tc_index < devlink_sb->egress_tc_count; tc_index++) {
  1200. if (*p_idx < start) {
  1201. (*p_idx)++;
  1202. continue;
  1203. }
  1204. err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
  1205. devlink_port,
  1206. devlink_sb,
  1207. tc_index,
  1208. DEVLINK_SB_POOL_TYPE_EGRESS,
  1209. DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
  1210. portid, seq,
  1211. NLM_F_MULTI);
  1212. if (err)
  1213. return err;
  1214. (*p_idx)++;
  1215. }
  1216. }
  1217. return 0;
  1218. }
  1219. static int
  1220. devlink_nl_cmd_sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
  1221. struct netlink_callback *cb)
  1222. {
  1223. struct devlink *devlink;
  1224. struct devlink_sb *devlink_sb;
  1225. int start = cb->args[0];
  1226. int idx = 0;
  1227. int err;
  1228. mutex_lock(&devlink_mutex);
  1229. list_for_each_entry(devlink, &devlink_list, list) {
  1230. if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
  1231. !devlink->ops || !devlink->ops->sb_tc_pool_bind_get)
  1232. continue;
  1233. mutex_lock(&devlink->lock);
  1234. list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
  1235. err = __sb_tc_pool_bind_get_dumpit(msg, start, &idx,
  1236. devlink,
  1237. devlink_sb,
  1238. NETLINK_CB(cb->skb).portid,
  1239. cb->nlh->nlmsg_seq);
  1240. if (err && err != -EOPNOTSUPP) {
  1241. mutex_unlock(&devlink->lock);
  1242. goto out;
  1243. }
  1244. }
  1245. mutex_unlock(&devlink->lock);
  1246. }
  1247. out:
  1248. mutex_unlock(&devlink_mutex);
  1249. cb->args[0] = idx;
  1250. return msg->len;
  1251. }
  1252. static int devlink_sb_tc_pool_bind_set(struct devlink_port *devlink_port,
  1253. unsigned int sb_index, u16 tc_index,
  1254. enum devlink_sb_pool_type pool_type,
  1255. u16 pool_index, u32 threshold)
  1256. {
  1257. const struct devlink_ops *ops = devlink_port->devlink->ops;
  1258. if (ops && ops->sb_tc_pool_bind_set)
  1259. return ops->sb_tc_pool_bind_set(devlink_port, sb_index,
  1260. tc_index, pool_type,
  1261. pool_index, threshold);
  1262. return -EOPNOTSUPP;
  1263. }
  1264. static int devlink_nl_cmd_sb_tc_pool_bind_set_doit(struct sk_buff *skb,
  1265. struct genl_info *info)
  1266. {
  1267. struct devlink_port *devlink_port = info->user_ptr[0];
  1268. struct devlink_sb *devlink_sb = info->user_ptr[1];
  1269. enum devlink_sb_pool_type pool_type;
  1270. u16 tc_index;
  1271. u16 pool_index;
  1272. u32 threshold;
  1273. int err;
  1274. err = devlink_sb_pool_type_get_from_info(info, &pool_type);
  1275. if (err)
  1276. return err;
  1277. err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
  1278. pool_type, &tc_index);
  1279. if (err)
  1280. return err;
  1281. err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
  1282. &pool_index);
  1283. if (err)
  1284. return err;
  1285. if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
  1286. return -EINVAL;
  1287. threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
  1288. return devlink_sb_tc_pool_bind_set(devlink_port, devlink_sb->index,
  1289. tc_index, pool_type,
  1290. pool_index, threshold);
  1291. }
  1292. static int devlink_nl_cmd_sb_occ_snapshot_doit(struct sk_buff *skb,
  1293. struct genl_info *info)
  1294. {
  1295. struct devlink *devlink = info->user_ptr[0];
  1296. struct devlink_sb *devlink_sb = info->user_ptr[1];
  1297. const struct devlink_ops *ops = devlink->ops;
  1298. if (ops && ops->sb_occ_snapshot)
  1299. return ops->sb_occ_snapshot(devlink, devlink_sb->index);
  1300. return -EOPNOTSUPP;
  1301. }
  1302. static int devlink_nl_cmd_sb_occ_max_clear_doit(struct sk_buff *skb,
  1303. struct genl_info *info)
  1304. {
  1305. struct devlink *devlink = info->user_ptr[0];
  1306. struct devlink_sb *devlink_sb = info->user_ptr[1];
  1307. const struct devlink_ops *ops = devlink->ops;
  1308. if (ops && ops->sb_occ_max_clear)
  1309. return ops->sb_occ_max_clear(devlink, devlink_sb->index);
  1310. return -EOPNOTSUPP;
  1311. }
  1312. static int devlink_nl_eswitch_fill(struct sk_buff *msg, struct devlink *devlink,
  1313. enum devlink_command cmd, u32 portid,
  1314. u32 seq, int flags)
  1315. {
  1316. const struct devlink_ops *ops = devlink->ops;
  1317. u8 inline_mode, encap_mode;
  1318. void *hdr;
  1319. int err = 0;
  1320. u16 mode;
  1321. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  1322. if (!hdr)
  1323. return -EMSGSIZE;
  1324. err = devlink_nl_put_handle(msg, devlink);
  1325. if (err)
  1326. goto nla_put_failure;
  1327. if (ops->eswitch_mode_get) {
  1328. err = ops->eswitch_mode_get(devlink, &mode);
  1329. if (err)
  1330. goto nla_put_failure;
  1331. err = nla_put_u16(msg, DEVLINK_ATTR_ESWITCH_MODE, mode);
  1332. if (err)
  1333. goto nla_put_failure;
  1334. }
  1335. if (ops->eswitch_inline_mode_get) {
  1336. err = ops->eswitch_inline_mode_get(devlink, &inline_mode);
  1337. if (err)
  1338. goto nla_put_failure;
  1339. err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_INLINE_MODE,
  1340. inline_mode);
  1341. if (err)
  1342. goto nla_put_failure;
  1343. }
  1344. if (ops->eswitch_encap_mode_get) {
  1345. err = ops->eswitch_encap_mode_get(devlink, &encap_mode);
  1346. if (err)
  1347. goto nla_put_failure;
  1348. err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_ENCAP_MODE, encap_mode);
  1349. if (err)
  1350. goto nla_put_failure;
  1351. }
  1352. genlmsg_end(msg, hdr);
  1353. return 0;
  1354. nla_put_failure:
  1355. genlmsg_cancel(msg, hdr);
  1356. return err;
  1357. }
  1358. static int devlink_nl_cmd_eswitch_get_doit(struct sk_buff *skb,
  1359. struct genl_info *info)
  1360. {
  1361. struct devlink *devlink = info->user_ptr[0];
  1362. const struct devlink_ops *ops = devlink->ops;
  1363. struct sk_buff *msg;
  1364. int err;
  1365. if (!ops)
  1366. return -EOPNOTSUPP;
  1367. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1368. if (!msg)
  1369. return -ENOMEM;
  1370. err = devlink_nl_eswitch_fill(msg, devlink, DEVLINK_CMD_ESWITCH_GET,
  1371. info->snd_portid, info->snd_seq, 0);
  1372. if (err) {
  1373. nlmsg_free(msg);
  1374. return err;
  1375. }
  1376. return genlmsg_reply(msg, info);
  1377. }
  1378. static int devlink_nl_cmd_eswitch_set_doit(struct sk_buff *skb,
  1379. struct genl_info *info)
  1380. {
  1381. struct devlink *devlink = info->user_ptr[0];
  1382. const struct devlink_ops *ops = devlink->ops;
  1383. u8 inline_mode, encap_mode;
  1384. int err = 0;
  1385. u16 mode;
  1386. if (!ops)
  1387. return -EOPNOTSUPP;
  1388. if (info->attrs[DEVLINK_ATTR_ESWITCH_MODE]) {
  1389. if (!ops->eswitch_mode_set)
  1390. return -EOPNOTSUPP;
  1391. mode = nla_get_u16(info->attrs[DEVLINK_ATTR_ESWITCH_MODE]);
  1392. err = ops->eswitch_mode_set(devlink, mode);
  1393. if (err)
  1394. return err;
  1395. }
  1396. if (info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]) {
  1397. if (!ops->eswitch_inline_mode_set)
  1398. return -EOPNOTSUPP;
  1399. inline_mode = nla_get_u8(
  1400. info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]);
  1401. err = ops->eswitch_inline_mode_set(devlink, inline_mode);
  1402. if (err)
  1403. return err;
  1404. }
  1405. if (info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]) {
  1406. if (!ops->eswitch_encap_mode_set)
  1407. return -EOPNOTSUPP;
  1408. encap_mode = nla_get_u8(info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]);
  1409. err = ops->eswitch_encap_mode_set(devlink, encap_mode);
  1410. if (err)
  1411. return err;
  1412. }
  1413. return 0;
  1414. }
  1415. int devlink_dpipe_match_put(struct sk_buff *skb,
  1416. struct devlink_dpipe_match *match)
  1417. {
  1418. struct devlink_dpipe_header *header = match->header;
  1419. struct devlink_dpipe_field *field = &header->fields[match->field_id];
  1420. struct nlattr *match_attr;
  1421. match_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_MATCH);
  1422. if (!match_attr)
  1423. return -EMSGSIZE;
  1424. if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_MATCH_TYPE, match->type) ||
  1425. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, match->header_index) ||
  1426. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
  1427. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
  1428. nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
  1429. goto nla_put_failure;
  1430. nla_nest_end(skb, match_attr);
  1431. return 0;
  1432. nla_put_failure:
  1433. nla_nest_cancel(skb, match_attr);
  1434. return -EMSGSIZE;
  1435. }
  1436. EXPORT_SYMBOL_GPL(devlink_dpipe_match_put);
  1437. static int devlink_dpipe_matches_put(struct devlink_dpipe_table *table,
  1438. struct sk_buff *skb)
  1439. {
  1440. struct nlattr *matches_attr;
  1441. matches_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLE_MATCHES);
  1442. if (!matches_attr)
  1443. return -EMSGSIZE;
  1444. if (table->table_ops->matches_dump(table->priv, skb))
  1445. goto nla_put_failure;
  1446. nla_nest_end(skb, matches_attr);
  1447. return 0;
  1448. nla_put_failure:
  1449. nla_nest_cancel(skb, matches_attr);
  1450. return -EMSGSIZE;
  1451. }
  1452. int devlink_dpipe_action_put(struct sk_buff *skb,
  1453. struct devlink_dpipe_action *action)
  1454. {
  1455. struct devlink_dpipe_header *header = action->header;
  1456. struct devlink_dpipe_field *field = &header->fields[action->field_id];
  1457. struct nlattr *action_attr;
  1458. action_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_ACTION);
  1459. if (!action_attr)
  1460. return -EMSGSIZE;
  1461. if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_ACTION_TYPE, action->type) ||
  1462. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, action->header_index) ||
  1463. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
  1464. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
  1465. nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
  1466. goto nla_put_failure;
  1467. nla_nest_end(skb, action_attr);
  1468. return 0;
  1469. nla_put_failure:
  1470. nla_nest_cancel(skb, action_attr);
  1471. return -EMSGSIZE;
  1472. }
  1473. EXPORT_SYMBOL_GPL(devlink_dpipe_action_put);
  1474. static int devlink_dpipe_actions_put(struct devlink_dpipe_table *table,
  1475. struct sk_buff *skb)
  1476. {
  1477. struct nlattr *actions_attr;
  1478. actions_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLE_ACTIONS);
  1479. if (!actions_attr)
  1480. return -EMSGSIZE;
  1481. if (table->table_ops->actions_dump(table->priv, skb))
  1482. goto nla_put_failure;
  1483. nla_nest_end(skb, actions_attr);
  1484. return 0;
  1485. nla_put_failure:
  1486. nla_nest_cancel(skb, actions_attr);
  1487. return -EMSGSIZE;
  1488. }
  1489. static int devlink_dpipe_table_put(struct sk_buff *skb,
  1490. struct devlink_dpipe_table *table)
  1491. {
  1492. struct nlattr *table_attr;
  1493. u64 table_size;
  1494. table_size = table->table_ops->size_get(table->priv);
  1495. table_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLE);
  1496. if (!table_attr)
  1497. return -EMSGSIZE;
  1498. if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_TABLE_NAME, table->name) ||
  1499. nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_SIZE, table_size,
  1500. DEVLINK_ATTR_PAD))
  1501. goto nla_put_failure;
  1502. if (nla_put_u8(skb, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED,
  1503. table->counters_enabled))
  1504. goto nla_put_failure;
  1505. if (table->resource_valid) {
  1506. if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID,
  1507. table->resource_id, DEVLINK_ATTR_PAD) ||
  1508. nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS,
  1509. table->resource_units, DEVLINK_ATTR_PAD))
  1510. goto nla_put_failure;
  1511. }
  1512. if (devlink_dpipe_matches_put(table, skb))
  1513. goto nla_put_failure;
  1514. if (devlink_dpipe_actions_put(table, skb))
  1515. goto nla_put_failure;
  1516. nla_nest_end(skb, table_attr);
  1517. return 0;
  1518. nla_put_failure:
  1519. nla_nest_cancel(skb, table_attr);
  1520. return -EMSGSIZE;
  1521. }
  1522. static int devlink_dpipe_send_and_alloc_skb(struct sk_buff **pskb,
  1523. struct genl_info *info)
  1524. {
  1525. int err;
  1526. if (*pskb) {
  1527. err = genlmsg_reply(*pskb, info);
  1528. if (err)
  1529. return err;
  1530. }
  1531. *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1532. if (!*pskb)
  1533. return -ENOMEM;
  1534. return 0;
  1535. }
  1536. static int devlink_dpipe_tables_fill(struct genl_info *info,
  1537. enum devlink_command cmd, int flags,
  1538. struct list_head *dpipe_tables,
  1539. const char *table_name)
  1540. {
  1541. struct devlink *devlink = info->user_ptr[0];
  1542. struct devlink_dpipe_table *table;
  1543. struct nlattr *tables_attr;
  1544. struct sk_buff *skb = NULL;
  1545. struct nlmsghdr *nlh;
  1546. bool incomplete;
  1547. void *hdr;
  1548. int i;
  1549. int err;
  1550. table = list_first_entry(dpipe_tables,
  1551. struct devlink_dpipe_table, list);
  1552. start_again:
  1553. err = devlink_dpipe_send_and_alloc_skb(&skb, info);
  1554. if (err)
  1555. return err;
  1556. hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
  1557. &devlink_nl_family, NLM_F_MULTI, cmd);
  1558. if (!hdr) {
  1559. nlmsg_free(skb);
  1560. return -EMSGSIZE;
  1561. }
  1562. if (devlink_nl_put_handle(skb, devlink))
  1563. goto nla_put_failure;
  1564. tables_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLES);
  1565. if (!tables_attr)
  1566. goto nla_put_failure;
  1567. i = 0;
  1568. incomplete = false;
  1569. list_for_each_entry_from(table, dpipe_tables, list) {
  1570. if (!table_name) {
  1571. err = devlink_dpipe_table_put(skb, table);
  1572. if (err) {
  1573. if (!i)
  1574. goto err_table_put;
  1575. incomplete = true;
  1576. break;
  1577. }
  1578. } else {
  1579. if (!strcmp(table->name, table_name)) {
  1580. err = devlink_dpipe_table_put(skb, table);
  1581. if (err)
  1582. break;
  1583. }
  1584. }
  1585. i++;
  1586. }
  1587. nla_nest_end(skb, tables_attr);
  1588. genlmsg_end(skb, hdr);
  1589. if (incomplete)
  1590. goto start_again;
  1591. send_done:
  1592. nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
  1593. NLMSG_DONE, 0, flags | NLM_F_MULTI);
  1594. if (!nlh) {
  1595. err = devlink_dpipe_send_and_alloc_skb(&skb, info);
  1596. if (err)
  1597. return err;
  1598. goto send_done;
  1599. }
  1600. return genlmsg_reply(skb, info);
  1601. nla_put_failure:
  1602. err = -EMSGSIZE;
  1603. err_table_put:
  1604. nlmsg_free(skb);
  1605. return err;
  1606. }
  1607. static int devlink_nl_cmd_dpipe_table_get(struct sk_buff *skb,
  1608. struct genl_info *info)
  1609. {
  1610. struct devlink *devlink = info->user_ptr[0];
  1611. const char *table_name = NULL;
  1612. if (info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
  1613. table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
  1614. return devlink_dpipe_tables_fill(info, DEVLINK_CMD_DPIPE_TABLE_GET, 0,
  1615. &devlink->dpipe_table_list,
  1616. table_name);
  1617. }
  1618. static int devlink_dpipe_value_put(struct sk_buff *skb,
  1619. struct devlink_dpipe_value *value)
  1620. {
  1621. if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE,
  1622. value->value_size, value->value))
  1623. return -EMSGSIZE;
  1624. if (value->mask)
  1625. if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE_MASK,
  1626. value->value_size, value->mask))
  1627. return -EMSGSIZE;
  1628. if (value->mapping_valid)
  1629. if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_VALUE_MAPPING,
  1630. value->mapping_value))
  1631. return -EMSGSIZE;
  1632. return 0;
  1633. }
  1634. static int devlink_dpipe_action_value_put(struct sk_buff *skb,
  1635. struct devlink_dpipe_value *value)
  1636. {
  1637. if (!value->action)
  1638. return -EINVAL;
  1639. if (devlink_dpipe_action_put(skb, value->action))
  1640. return -EMSGSIZE;
  1641. if (devlink_dpipe_value_put(skb, value))
  1642. return -EMSGSIZE;
  1643. return 0;
  1644. }
  1645. static int devlink_dpipe_action_values_put(struct sk_buff *skb,
  1646. struct devlink_dpipe_value *values,
  1647. unsigned int values_count)
  1648. {
  1649. struct nlattr *action_attr;
  1650. int i;
  1651. int err;
  1652. for (i = 0; i < values_count; i++) {
  1653. action_attr = nla_nest_start(skb,
  1654. DEVLINK_ATTR_DPIPE_ACTION_VALUE);
  1655. if (!action_attr)
  1656. return -EMSGSIZE;
  1657. err = devlink_dpipe_action_value_put(skb, &values[i]);
  1658. if (err)
  1659. goto err_action_value_put;
  1660. nla_nest_end(skb, action_attr);
  1661. }
  1662. return 0;
  1663. err_action_value_put:
  1664. nla_nest_cancel(skb, action_attr);
  1665. return err;
  1666. }
  1667. static int devlink_dpipe_match_value_put(struct sk_buff *skb,
  1668. struct devlink_dpipe_value *value)
  1669. {
  1670. if (!value->match)
  1671. return -EINVAL;
  1672. if (devlink_dpipe_match_put(skb, value->match))
  1673. return -EMSGSIZE;
  1674. if (devlink_dpipe_value_put(skb, value))
  1675. return -EMSGSIZE;
  1676. return 0;
  1677. }
  1678. static int devlink_dpipe_match_values_put(struct sk_buff *skb,
  1679. struct devlink_dpipe_value *values,
  1680. unsigned int values_count)
  1681. {
  1682. struct nlattr *match_attr;
  1683. int i;
  1684. int err;
  1685. for (i = 0; i < values_count; i++) {
  1686. match_attr = nla_nest_start(skb,
  1687. DEVLINK_ATTR_DPIPE_MATCH_VALUE);
  1688. if (!match_attr)
  1689. return -EMSGSIZE;
  1690. err = devlink_dpipe_match_value_put(skb, &values[i]);
  1691. if (err)
  1692. goto err_match_value_put;
  1693. nla_nest_end(skb, match_attr);
  1694. }
  1695. return 0;
  1696. err_match_value_put:
  1697. nla_nest_cancel(skb, match_attr);
  1698. return err;
  1699. }
  1700. static int devlink_dpipe_entry_put(struct sk_buff *skb,
  1701. struct devlink_dpipe_entry *entry)
  1702. {
  1703. struct nlattr *entry_attr, *matches_attr, *actions_attr;
  1704. int err;
  1705. entry_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_ENTRY);
  1706. if (!entry_attr)
  1707. return -EMSGSIZE;
  1708. if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_INDEX, entry->index,
  1709. DEVLINK_ATTR_PAD))
  1710. goto nla_put_failure;
  1711. if (entry->counter_valid)
  1712. if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_COUNTER,
  1713. entry->counter, DEVLINK_ATTR_PAD))
  1714. goto nla_put_failure;
  1715. matches_attr = nla_nest_start(skb,
  1716. DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES);
  1717. if (!matches_attr)
  1718. goto nla_put_failure;
  1719. err = devlink_dpipe_match_values_put(skb, entry->match_values,
  1720. entry->match_values_count);
  1721. if (err) {
  1722. nla_nest_cancel(skb, matches_attr);
  1723. goto err_match_values_put;
  1724. }
  1725. nla_nest_end(skb, matches_attr);
  1726. actions_attr = nla_nest_start(skb,
  1727. DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES);
  1728. if (!actions_attr)
  1729. goto nla_put_failure;
  1730. err = devlink_dpipe_action_values_put(skb, entry->action_values,
  1731. entry->action_values_count);
  1732. if (err) {
  1733. nla_nest_cancel(skb, actions_attr);
  1734. goto err_action_values_put;
  1735. }
  1736. nla_nest_end(skb, actions_attr);
  1737. nla_nest_end(skb, entry_attr);
  1738. return 0;
  1739. nla_put_failure:
  1740. err = -EMSGSIZE;
  1741. err_match_values_put:
  1742. err_action_values_put:
  1743. nla_nest_cancel(skb, entry_attr);
  1744. return err;
  1745. }
  1746. static struct devlink_dpipe_table *
  1747. devlink_dpipe_table_find(struct list_head *dpipe_tables,
  1748. const char *table_name)
  1749. {
  1750. struct devlink_dpipe_table *table;
  1751. list_for_each_entry_rcu(table, dpipe_tables, list) {
  1752. if (!strcmp(table->name, table_name))
  1753. return table;
  1754. }
  1755. return NULL;
  1756. }
  1757. int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx)
  1758. {
  1759. struct devlink *devlink;
  1760. int err;
  1761. err = devlink_dpipe_send_and_alloc_skb(&dump_ctx->skb,
  1762. dump_ctx->info);
  1763. if (err)
  1764. return err;
  1765. dump_ctx->hdr = genlmsg_put(dump_ctx->skb,
  1766. dump_ctx->info->snd_portid,
  1767. dump_ctx->info->snd_seq,
  1768. &devlink_nl_family, NLM_F_MULTI,
  1769. dump_ctx->cmd);
  1770. if (!dump_ctx->hdr)
  1771. goto nla_put_failure;
  1772. devlink = dump_ctx->info->user_ptr[0];
  1773. if (devlink_nl_put_handle(dump_ctx->skb, devlink))
  1774. goto nla_put_failure;
  1775. dump_ctx->nest = nla_nest_start(dump_ctx->skb,
  1776. DEVLINK_ATTR_DPIPE_ENTRIES);
  1777. if (!dump_ctx->nest)
  1778. goto nla_put_failure;
  1779. return 0;
  1780. nla_put_failure:
  1781. nlmsg_free(dump_ctx->skb);
  1782. return -EMSGSIZE;
  1783. }
  1784. EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_prepare);
  1785. int devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx,
  1786. struct devlink_dpipe_entry *entry)
  1787. {
  1788. return devlink_dpipe_entry_put(dump_ctx->skb, entry);
  1789. }
  1790. EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_append);
  1791. int devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx)
  1792. {
  1793. nla_nest_end(dump_ctx->skb, dump_ctx->nest);
  1794. genlmsg_end(dump_ctx->skb, dump_ctx->hdr);
  1795. return 0;
  1796. }
  1797. EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_close);
  1798. void devlink_dpipe_entry_clear(struct devlink_dpipe_entry *entry)
  1799. {
  1800. unsigned int value_count, value_index;
  1801. struct devlink_dpipe_value *value;
  1802. value = entry->action_values;
  1803. value_count = entry->action_values_count;
  1804. for (value_index = 0; value_index < value_count; value_index++) {
  1805. kfree(value[value_index].value);
  1806. kfree(value[value_index].mask);
  1807. }
  1808. value = entry->match_values;
  1809. value_count = entry->match_values_count;
  1810. for (value_index = 0; value_index < value_count; value_index++) {
  1811. kfree(value[value_index].value);
  1812. kfree(value[value_index].mask);
  1813. }
  1814. }
  1815. EXPORT_SYMBOL(devlink_dpipe_entry_clear);
  1816. static int devlink_dpipe_entries_fill(struct genl_info *info,
  1817. enum devlink_command cmd, int flags,
  1818. struct devlink_dpipe_table *table)
  1819. {
  1820. struct devlink_dpipe_dump_ctx dump_ctx;
  1821. struct nlmsghdr *nlh;
  1822. int err;
  1823. dump_ctx.skb = NULL;
  1824. dump_ctx.cmd = cmd;
  1825. dump_ctx.info = info;
  1826. err = table->table_ops->entries_dump(table->priv,
  1827. table->counters_enabled,
  1828. &dump_ctx);
  1829. if (err)
  1830. return err;
  1831. send_done:
  1832. nlh = nlmsg_put(dump_ctx.skb, info->snd_portid, info->snd_seq,
  1833. NLMSG_DONE, 0, flags | NLM_F_MULTI);
  1834. if (!nlh) {
  1835. err = devlink_dpipe_send_and_alloc_skb(&dump_ctx.skb, info);
  1836. if (err)
  1837. return err;
  1838. goto send_done;
  1839. }
  1840. return genlmsg_reply(dump_ctx.skb, info);
  1841. }
  1842. static int devlink_nl_cmd_dpipe_entries_get(struct sk_buff *skb,
  1843. struct genl_info *info)
  1844. {
  1845. struct devlink *devlink = info->user_ptr[0];
  1846. struct devlink_dpipe_table *table;
  1847. const char *table_name;
  1848. if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
  1849. return -EINVAL;
  1850. table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
  1851. table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
  1852. table_name);
  1853. if (!table)
  1854. return -EINVAL;
  1855. if (!table->table_ops->entries_dump)
  1856. return -EINVAL;
  1857. return devlink_dpipe_entries_fill(info, DEVLINK_CMD_DPIPE_ENTRIES_GET,
  1858. 0, table);
  1859. }
  1860. static int devlink_dpipe_fields_put(struct sk_buff *skb,
  1861. const struct devlink_dpipe_header *header)
  1862. {
  1863. struct devlink_dpipe_field *field;
  1864. struct nlattr *field_attr;
  1865. int i;
  1866. for (i = 0; i < header->fields_count; i++) {
  1867. field = &header->fields[i];
  1868. field_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_FIELD);
  1869. if (!field_attr)
  1870. return -EMSGSIZE;
  1871. if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_FIELD_NAME, field->name) ||
  1872. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
  1873. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH, field->bitwidth) ||
  1874. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE, field->mapping_type))
  1875. goto nla_put_failure;
  1876. nla_nest_end(skb, field_attr);
  1877. }
  1878. return 0;
  1879. nla_put_failure:
  1880. nla_nest_cancel(skb, field_attr);
  1881. return -EMSGSIZE;
  1882. }
  1883. static int devlink_dpipe_header_put(struct sk_buff *skb,
  1884. struct devlink_dpipe_header *header)
  1885. {
  1886. struct nlattr *fields_attr, *header_attr;
  1887. int err;
  1888. header_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_HEADER);
  1889. if (!header_attr)
  1890. return -EMSGSIZE;
  1891. if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_HEADER_NAME, header->name) ||
  1892. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
  1893. nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
  1894. goto nla_put_failure;
  1895. fields_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_HEADER_FIELDS);
  1896. if (!fields_attr)
  1897. goto nla_put_failure;
  1898. err = devlink_dpipe_fields_put(skb, header);
  1899. if (err) {
  1900. nla_nest_cancel(skb, fields_attr);
  1901. goto nla_put_failure;
  1902. }
  1903. nla_nest_end(skb, fields_attr);
  1904. nla_nest_end(skb, header_attr);
  1905. return 0;
  1906. nla_put_failure:
  1907. err = -EMSGSIZE;
  1908. nla_nest_cancel(skb, header_attr);
  1909. return err;
  1910. }
  1911. static int devlink_dpipe_headers_fill(struct genl_info *info,
  1912. enum devlink_command cmd, int flags,
  1913. struct devlink_dpipe_headers *
  1914. dpipe_headers)
  1915. {
  1916. struct devlink *devlink = info->user_ptr[0];
  1917. struct nlattr *headers_attr;
  1918. struct sk_buff *skb = NULL;
  1919. struct nlmsghdr *nlh;
  1920. void *hdr;
  1921. int i, j;
  1922. int err;
  1923. i = 0;
  1924. start_again:
  1925. err = devlink_dpipe_send_and_alloc_skb(&skb, info);
  1926. if (err)
  1927. return err;
  1928. hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
  1929. &devlink_nl_family, NLM_F_MULTI, cmd);
  1930. if (!hdr) {
  1931. nlmsg_free(skb);
  1932. return -EMSGSIZE;
  1933. }
  1934. if (devlink_nl_put_handle(skb, devlink))
  1935. goto nla_put_failure;
  1936. headers_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_HEADERS);
  1937. if (!headers_attr)
  1938. goto nla_put_failure;
  1939. j = 0;
  1940. for (; i < dpipe_headers->headers_count; i++) {
  1941. err = devlink_dpipe_header_put(skb, dpipe_headers->headers[i]);
  1942. if (err) {
  1943. if (!j)
  1944. goto err_table_put;
  1945. break;
  1946. }
  1947. j++;
  1948. }
  1949. nla_nest_end(skb, headers_attr);
  1950. genlmsg_end(skb, hdr);
  1951. if (i != dpipe_headers->headers_count)
  1952. goto start_again;
  1953. send_done:
  1954. nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
  1955. NLMSG_DONE, 0, flags | NLM_F_MULTI);
  1956. if (!nlh) {
  1957. err = devlink_dpipe_send_and_alloc_skb(&skb, info);
  1958. if (err)
  1959. return err;
  1960. goto send_done;
  1961. }
  1962. return genlmsg_reply(skb, info);
  1963. nla_put_failure:
  1964. err = -EMSGSIZE;
  1965. err_table_put:
  1966. nlmsg_free(skb);
  1967. return err;
  1968. }
  1969. static int devlink_nl_cmd_dpipe_headers_get(struct sk_buff *skb,
  1970. struct genl_info *info)
  1971. {
  1972. struct devlink *devlink = info->user_ptr[0];
  1973. if (!devlink->dpipe_headers)
  1974. return -EOPNOTSUPP;
  1975. return devlink_dpipe_headers_fill(info, DEVLINK_CMD_DPIPE_HEADERS_GET,
  1976. 0, devlink->dpipe_headers);
  1977. }
  1978. static int devlink_dpipe_table_counters_set(struct devlink *devlink,
  1979. const char *table_name,
  1980. bool enable)
  1981. {
  1982. struct devlink_dpipe_table *table;
  1983. table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
  1984. table_name);
  1985. if (!table)
  1986. return -EINVAL;
  1987. if (table->counter_control_extern)
  1988. return -EOPNOTSUPP;
  1989. if (!(table->counters_enabled ^ enable))
  1990. return 0;
  1991. table->counters_enabled = enable;
  1992. if (table->table_ops->counters_set_update)
  1993. table->table_ops->counters_set_update(table->priv, enable);
  1994. return 0;
  1995. }
  1996. static int devlink_nl_cmd_dpipe_table_counters_set(struct sk_buff *skb,
  1997. struct genl_info *info)
  1998. {
  1999. struct devlink *devlink = info->user_ptr[0];
  2000. const char *table_name;
  2001. bool counters_enable;
  2002. if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME] ||
  2003. !info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED])
  2004. return -EINVAL;
  2005. table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
  2006. counters_enable = !!nla_get_u8(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED]);
  2007. return devlink_dpipe_table_counters_set(devlink, table_name,
  2008. counters_enable);
  2009. }
  2010. static struct devlink_resource *
  2011. devlink_resource_find(struct devlink *devlink,
  2012. struct devlink_resource *resource, u64 resource_id)
  2013. {
  2014. struct list_head *resource_list;
  2015. if (resource)
  2016. resource_list = &resource->resource_list;
  2017. else
  2018. resource_list = &devlink->resource_list;
  2019. list_for_each_entry(resource, resource_list, list) {
  2020. struct devlink_resource *child_resource;
  2021. if (resource->id == resource_id)
  2022. return resource;
  2023. child_resource = devlink_resource_find(devlink, resource,
  2024. resource_id);
  2025. if (child_resource)
  2026. return child_resource;
  2027. }
  2028. return NULL;
  2029. }
  2030. static void
  2031. devlink_resource_validate_children(struct devlink_resource *resource)
  2032. {
  2033. struct devlink_resource *child_resource;
  2034. bool size_valid = true;
  2035. u64 parts_size = 0;
  2036. if (list_empty(&resource->resource_list))
  2037. goto out;
  2038. list_for_each_entry(child_resource, &resource->resource_list, list)
  2039. parts_size += child_resource->size_new;
  2040. if (parts_size > resource->size_new)
  2041. size_valid = false;
  2042. out:
  2043. resource->size_valid = size_valid;
  2044. }
  2045. static int
  2046. devlink_resource_validate_size(struct devlink_resource *resource, u64 size,
  2047. struct netlink_ext_ack *extack)
  2048. {
  2049. u64 reminder;
  2050. int err = 0;
  2051. if (size > resource->size_params.size_max) {
  2052. NL_SET_ERR_MSG_MOD(extack, "Size larger than maximum");
  2053. err = -EINVAL;
  2054. }
  2055. if (size < resource->size_params.size_min) {
  2056. NL_SET_ERR_MSG_MOD(extack, "Size smaller than minimum");
  2057. err = -EINVAL;
  2058. }
  2059. div64_u64_rem(size, resource->size_params.size_granularity, &reminder);
  2060. if (reminder) {
  2061. NL_SET_ERR_MSG_MOD(extack, "Wrong granularity");
  2062. err = -EINVAL;
  2063. }
  2064. return err;
  2065. }
  2066. static int devlink_nl_cmd_resource_set(struct sk_buff *skb,
  2067. struct genl_info *info)
  2068. {
  2069. struct devlink *devlink = info->user_ptr[0];
  2070. struct devlink_resource *resource;
  2071. u64 resource_id;
  2072. u64 size;
  2073. int err;
  2074. if (!info->attrs[DEVLINK_ATTR_RESOURCE_ID] ||
  2075. !info->attrs[DEVLINK_ATTR_RESOURCE_SIZE])
  2076. return -EINVAL;
  2077. resource_id = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_ID]);
  2078. resource = devlink_resource_find(devlink, NULL, resource_id);
  2079. if (!resource)
  2080. return -EINVAL;
  2081. size = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_SIZE]);
  2082. err = devlink_resource_validate_size(resource, size, info->extack);
  2083. if (err)
  2084. return err;
  2085. resource->size_new = size;
  2086. devlink_resource_validate_children(resource);
  2087. if (resource->parent)
  2088. devlink_resource_validate_children(resource->parent);
  2089. return 0;
  2090. }
  2091. static int
  2092. devlink_resource_size_params_put(struct devlink_resource *resource,
  2093. struct sk_buff *skb)
  2094. {
  2095. struct devlink_resource_size_params *size_params;
  2096. size_params = &resource->size_params;
  2097. if (nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_GRAN,
  2098. size_params->size_granularity, DEVLINK_ATTR_PAD) ||
  2099. nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MAX,
  2100. size_params->size_max, DEVLINK_ATTR_PAD) ||
  2101. nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MIN,
  2102. size_params->size_min, DEVLINK_ATTR_PAD) ||
  2103. nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_UNIT, size_params->unit))
  2104. return -EMSGSIZE;
  2105. return 0;
  2106. }
  2107. static int devlink_resource_occ_put(struct devlink_resource *resource,
  2108. struct sk_buff *skb)
  2109. {
  2110. if (!resource->occ_get)
  2111. return 0;
  2112. return nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_OCC,
  2113. resource->occ_get(resource->occ_get_priv),
  2114. DEVLINK_ATTR_PAD);
  2115. }
  2116. static int devlink_resource_put(struct devlink *devlink, struct sk_buff *skb,
  2117. struct devlink_resource *resource)
  2118. {
  2119. struct devlink_resource *child_resource;
  2120. struct nlattr *child_resource_attr;
  2121. struct nlattr *resource_attr;
  2122. resource_attr = nla_nest_start(skb, DEVLINK_ATTR_RESOURCE);
  2123. if (!resource_attr)
  2124. return -EMSGSIZE;
  2125. if (nla_put_string(skb, DEVLINK_ATTR_RESOURCE_NAME, resource->name) ||
  2126. nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE, resource->size,
  2127. DEVLINK_ATTR_PAD) ||
  2128. nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_ID, resource->id,
  2129. DEVLINK_ATTR_PAD))
  2130. goto nla_put_failure;
  2131. if (resource->size != resource->size_new)
  2132. nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_NEW,
  2133. resource->size_new, DEVLINK_ATTR_PAD);
  2134. if (devlink_resource_occ_put(resource, skb))
  2135. goto nla_put_failure;
  2136. if (devlink_resource_size_params_put(resource, skb))
  2137. goto nla_put_failure;
  2138. if (list_empty(&resource->resource_list))
  2139. goto out;
  2140. if (nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_SIZE_VALID,
  2141. resource->size_valid))
  2142. goto nla_put_failure;
  2143. child_resource_attr = nla_nest_start(skb, DEVLINK_ATTR_RESOURCE_LIST);
  2144. if (!child_resource_attr)
  2145. goto nla_put_failure;
  2146. list_for_each_entry(child_resource, &resource->resource_list, list) {
  2147. if (devlink_resource_put(devlink, skb, child_resource))
  2148. goto resource_put_failure;
  2149. }
  2150. nla_nest_end(skb, child_resource_attr);
  2151. out:
  2152. nla_nest_end(skb, resource_attr);
  2153. return 0;
  2154. resource_put_failure:
  2155. nla_nest_cancel(skb, child_resource_attr);
  2156. nla_put_failure:
  2157. nla_nest_cancel(skb, resource_attr);
  2158. return -EMSGSIZE;
  2159. }
  2160. static int devlink_resource_fill(struct genl_info *info,
  2161. enum devlink_command cmd, int flags)
  2162. {
  2163. struct devlink *devlink = info->user_ptr[0];
  2164. struct devlink_resource *resource;
  2165. struct nlattr *resources_attr;
  2166. struct sk_buff *skb = NULL;
  2167. struct nlmsghdr *nlh;
  2168. bool incomplete;
  2169. void *hdr;
  2170. int i;
  2171. int err;
  2172. resource = list_first_entry(&devlink->resource_list,
  2173. struct devlink_resource, list);
  2174. start_again:
  2175. err = devlink_dpipe_send_and_alloc_skb(&skb, info);
  2176. if (err)
  2177. return err;
  2178. hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
  2179. &devlink_nl_family, NLM_F_MULTI, cmd);
  2180. if (!hdr) {
  2181. nlmsg_free(skb);
  2182. return -EMSGSIZE;
  2183. }
  2184. if (devlink_nl_put_handle(skb, devlink))
  2185. goto nla_put_failure;
  2186. resources_attr = nla_nest_start(skb, DEVLINK_ATTR_RESOURCE_LIST);
  2187. if (!resources_attr)
  2188. goto nla_put_failure;
  2189. incomplete = false;
  2190. i = 0;
  2191. list_for_each_entry_from(resource, &devlink->resource_list, list) {
  2192. err = devlink_resource_put(devlink, skb, resource);
  2193. if (err) {
  2194. if (!i)
  2195. goto err_resource_put;
  2196. incomplete = true;
  2197. break;
  2198. }
  2199. i++;
  2200. }
  2201. nla_nest_end(skb, resources_attr);
  2202. genlmsg_end(skb, hdr);
  2203. if (incomplete)
  2204. goto start_again;
  2205. send_done:
  2206. nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
  2207. NLMSG_DONE, 0, flags | NLM_F_MULTI);
  2208. if (!nlh) {
  2209. err = devlink_dpipe_send_and_alloc_skb(&skb, info);
  2210. if (err)
  2211. return err;
  2212. goto send_done;
  2213. }
  2214. return genlmsg_reply(skb, info);
  2215. nla_put_failure:
  2216. err = -EMSGSIZE;
  2217. err_resource_put:
  2218. nlmsg_free(skb);
  2219. return err;
  2220. }
  2221. static int devlink_nl_cmd_resource_dump(struct sk_buff *skb,
  2222. struct genl_info *info)
  2223. {
  2224. struct devlink *devlink = info->user_ptr[0];
  2225. if (list_empty(&devlink->resource_list))
  2226. return -EOPNOTSUPP;
  2227. return devlink_resource_fill(info, DEVLINK_CMD_RESOURCE_DUMP, 0);
  2228. }
  2229. static int
  2230. devlink_resources_validate(struct devlink *devlink,
  2231. struct devlink_resource *resource,
  2232. struct genl_info *info)
  2233. {
  2234. struct list_head *resource_list;
  2235. int err = 0;
  2236. if (resource)
  2237. resource_list = &resource->resource_list;
  2238. else
  2239. resource_list = &devlink->resource_list;
  2240. list_for_each_entry(resource, resource_list, list) {
  2241. if (!resource->size_valid)
  2242. return -EINVAL;
  2243. err = devlink_resources_validate(devlink, resource, info);
  2244. if (err)
  2245. return err;
  2246. }
  2247. return err;
  2248. }
  2249. static int devlink_nl_cmd_reload(struct sk_buff *skb, struct genl_info *info)
  2250. {
  2251. struct devlink *devlink = info->user_ptr[0];
  2252. int err;
  2253. if (!devlink->ops->reload)
  2254. return -EOPNOTSUPP;
  2255. err = devlink_resources_validate(devlink, NULL, info);
  2256. if (err) {
  2257. NL_SET_ERR_MSG_MOD(info->extack, "resources size validation failed");
  2258. return err;
  2259. }
  2260. return devlink->ops->reload(devlink, info->extack);
  2261. }
  2262. static const struct devlink_param devlink_param_generic[] = {
  2263. {
  2264. .id = DEVLINK_PARAM_GENERIC_ID_INT_ERR_RESET,
  2265. .name = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_NAME,
  2266. .type = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_TYPE,
  2267. },
  2268. {
  2269. .id = DEVLINK_PARAM_GENERIC_ID_MAX_MACS,
  2270. .name = DEVLINK_PARAM_GENERIC_MAX_MACS_NAME,
  2271. .type = DEVLINK_PARAM_GENERIC_MAX_MACS_TYPE,
  2272. },
  2273. {
  2274. .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_SRIOV,
  2275. .name = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_NAME,
  2276. .type = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_TYPE,
  2277. },
  2278. {
  2279. .id = DEVLINK_PARAM_GENERIC_ID_REGION_SNAPSHOT,
  2280. .name = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_NAME,
  2281. .type = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_TYPE,
  2282. },
  2283. };
  2284. static int devlink_param_generic_verify(const struct devlink_param *param)
  2285. {
  2286. /* verify it match generic parameter by id and name */
  2287. if (param->id > DEVLINK_PARAM_GENERIC_ID_MAX)
  2288. return -EINVAL;
  2289. if (strcmp(param->name, devlink_param_generic[param->id].name))
  2290. return -ENOENT;
  2291. WARN_ON(param->type != devlink_param_generic[param->id].type);
  2292. return 0;
  2293. }
  2294. static int devlink_param_driver_verify(const struct devlink_param *param)
  2295. {
  2296. int i;
  2297. if (param->id <= DEVLINK_PARAM_GENERIC_ID_MAX)
  2298. return -EINVAL;
  2299. /* verify no such name in generic params */
  2300. for (i = 0; i <= DEVLINK_PARAM_GENERIC_ID_MAX; i++)
  2301. if (!strcmp(param->name, devlink_param_generic[i].name))
  2302. return -EEXIST;
  2303. return 0;
  2304. }
  2305. static struct devlink_param_item *
  2306. devlink_param_find_by_name(struct list_head *param_list,
  2307. const char *param_name)
  2308. {
  2309. struct devlink_param_item *param_item;
  2310. list_for_each_entry(param_item, param_list, list)
  2311. if (!strcmp(param_item->param->name, param_name))
  2312. return param_item;
  2313. return NULL;
  2314. }
  2315. static struct devlink_param_item *
  2316. devlink_param_find_by_id(struct list_head *param_list, u32 param_id)
  2317. {
  2318. struct devlink_param_item *param_item;
  2319. list_for_each_entry(param_item, param_list, list)
  2320. if (param_item->param->id == param_id)
  2321. return param_item;
  2322. return NULL;
  2323. }
  2324. static bool
  2325. devlink_param_cmode_is_supported(const struct devlink_param *param,
  2326. enum devlink_param_cmode cmode)
  2327. {
  2328. return test_bit(cmode, &param->supported_cmodes);
  2329. }
  2330. static int devlink_param_get(struct devlink *devlink,
  2331. const struct devlink_param *param,
  2332. struct devlink_param_gset_ctx *ctx)
  2333. {
  2334. if (!param->get)
  2335. return -EOPNOTSUPP;
  2336. return param->get(devlink, param->id, ctx);
  2337. }
  2338. static int devlink_param_set(struct devlink *devlink,
  2339. const struct devlink_param *param,
  2340. struct devlink_param_gset_ctx *ctx)
  2341. {
  2342. if (!param->set)
  2343. return -EOPNOTSUPP;
  2344. return param->set(devlink, param->id, ctx);
  2345. }
  2346. static int
  2347. devlink_param_type_to_nla_type(enum devlink_param_type param_type)
  2348. {
  2349. switch (param_type) {
  2350. case DEVLINK_PARAM_TYPE_U8:
  2351. return NLA_U8;
  2352. case DEVLINK_PARAM_TYPE_U16:
  2353. return NLA_U16;
  2354. case DEVLINK_PARAM_TYPE_U32:
  2355. return NLA_U32;
  2356. case DEVLINK_PARAM_TYPE_STRING:
  2357. return NLA_STRING;
  2358. case DEVLINK_PARAM_TYPE_BOOL:
  2359. return NLA_FLAG;
  2360. default:
  2361. return -EINVAL;
  2362. }
  2363. }
  2364. static int
  2365. devlink_nl_param_value_fill_one(struct sk_buff *msg,
  2366. enum devlink_param_type type,
  2367. enum devlink_param_cmode cmode,
  2368. union devlink_param_value val)
  2369. {
  2370. struct nlattr *param_value_attr;
  2371. param_value_attr = nla_nest_start(msg, DEVLINK_ATTR_PARAM_VALUE);
  2372. if (!param_value_attr)
  2373. goto nla_put_failure;
  2374. if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_CMODE, cmode))
  2375. goto value_nest_cancel;
  2376. switch (type) {
  2377. case DEVLINK_PARAM_TYPE_U8:
  2378. if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu8))
  2379. goto value_nest_cancel;
  2380. break;
  2381. case DEVLINK_PARAM_TYPE_U16:
  2382. if (nla_put_u16(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu16))
  2383. goto value_nest_cancel;
  2384. break;
  2385. case DEVLINK_PARAM_TYPE_U32:
  2386. if (nla_put_u32(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu32))
  2387. goto value_nest_cancel;
  2388. break;
  2389. case DEVLINK_PARAM_TYPE_STRING:
  2390. if (nla_put_string(msg, DEVLINK_ATTR_PARAM_VALUE_DATA,
  2391. val.vstr))
  2392. goto value_nest_cancel;
  2393. break;
  2394. case DEVLINK_PARAM_TYPE_BOOL:
  2395. if (val.vbool &&
  2396. nla_put_flag(msg, DEVLINK_ATTR_PARAM_VALUE_DATA))
  2397. goto value_nest_cancel;
  2398. break;
  2399. }
  2400. nla_nest_end(msg, param_value_attr);
  2401. return 0;
  2402. value_nest_cancel:
  2403. nla_nest_cancel(msg, param_value_attr);
  2404. nla_put_failure:
  2405. return -EMSGSIZE;
  2406. }
  2407. static int devlink_nl_param_fill(struct sk_buff *msg, struct devlink *devlink,
  2408. struct devlink_param_item *param_item,
  2409. enum devlink_command cmd,
  2410. u32 portid, u32 seq, int flags)
  2411. {
  2412. union devlink_param_value param_value[DEVLINK_PARAM_CMODE_MAX + 1];
  2413. const struct devlink_param *param = param_item->param;
  2414. struct devlink_param_gset_ctx ctx;
  2415. struct nlattr *param_values_list;
  2416. struct nlattr *param_attr;
  2417. int nla_type;
  2418. void *hdr;
  2419. int err;
  2420. int i;
  2421. /* Get value from driver part to driverinit configuration mode */
  2422. for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
  2423. if (!devlink_param_cmode_is_supported(param, i))
  2424. continue;
  2425. if (i == DEVLINK_PARAM_CMODE_DRIVERINIT) {
  2426. if (!param_item->driverinit_value_valid)
  2427. return -EOPNOTSUPP;
  2428. param_value[i] = param_item->driverinit_value;
  2429. } else {
  2430. ctx.cmode = i;
  2431. err = devlink_param_get(devlink, param, &ctx);
  2432. if (err)
  2433. return err;
  2434. param_value[i] = ctx.val;
  2435. }
  2436. }
  2437. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  2438. if (!hdr)
  2439. return -EMSGSIZE;
  2440. if (devlink_nl_put_handle(msg, devlink))
  2441. goto genlmsg_cancel;
  2442. param_attr = nla_nest_start(msg, DEVLINK_ATTR_PARAM);
  2443. if (!param_attr)
  2444. goto genlmsg_cancel;
  2445. if (nla_put_string(msg, DEVLINK_ATTR_PARAM_NAME, param->name))
  2446. goto param_nest_cancel;
  2447. if (param->generic && nla_put_flag(msg, DEVLINK_ATTR_PARAM_GENERIC))
  2448. goto param_nest_cancel;
  2449. nla_type = devlink_param_type_to_nla_type(param->type);
  2450. if (nla_type < 0)
  2451. goto param_nest_cancel;
  2452. if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_TYPE, nla_type))
  2453. goto param_nest_cancel;
  2454. param_values_list = nla_nest_start(msg, DEVLINK_ATTR_PARAM_VALUES_LIST);
  2455. if (!param_values_list)
  2456. goto param_nest_cancel;
  2457. for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
  2458. if (!devlink_param_cmode_is_supported(param, i))
  2459. continue;
  2460. err = devlink_nl_param_value_fill_one(msg, param->type,
  2461. i, param_value[i]);
  2462. if (err)
  2463. goto values_list_nest_cancel;
  2464. }
  2465. nla_nest_end(msg, param_values_list);
  2466. nla_nest_end(msg, param_attr);
  2467. genlmsg_end(msg, hdr);
  2468. return 0;
  2469. values_list_nest_cancel:
  2470. nla_nest_end(msg, param_values_list);
  2471. param_nest_cancel:
  2472. nla_nest_cancel(msg, param_attr);
  2473. genlmsg_cancel:
  2474. genlmsg_cancel(msg, hdr);
  2475. return -EMSGSIZE;
  2476. }
  2477. static void devlink_param_notify(struct devlink *devlink,
  2478. struct devlink_param_item *param_item,
  2479. enum devlink_command cmd)
  2480. {
  2481. struct sk_buff *msg;
  2482. int err;
  2483. WARN_ON(cmd != DEVLINK_CMD_PARAM_NEW && cmd != DEVLINK_CMD_PARAM_DEL);
  2484. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  2485. if (!msg)
  2486. return;
  2487. err = devlink_nl_param_fill(msg, devlink, param_item, cmd, 0, 0, 0);
  2488. if (err) {
  2489. nlmsg_free(msg);
  2490. return;
  2491. }
  2492. genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
  2493. msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
  2494. }
  2495. static int devlink_nl_cmd_param_get_dumpit(struct sk_buff *msg,
  2496. struct netlink_callback *cb)
  2497. {
  2498. struct devlink_param_item *param_item;
  2499. struct devlink *devlink;
  2500. int start = cb->args[0];
  2501. int idx = 0;
  2502. int err;
  2503. mutex_lock(&devlink_mutex);
  2504. list_for_each_entry(devlink, &devlink_list, list) {
  2505. if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
  2506. continue;
  2507. mutex_lock(&devlink->lock);
  2508. list_for_each_entry(param_item, &devlink->param_list, list) {
  2509. if (idx < start) {
  2510. idx++;
  2511. continue;
  2512. }
  2513. err = devlink_nl_param_fill(msg, devlink, param_item,
  2514. DEVLINK_CMD_PARAM_GET,
  2515. NETLINK_CB(cb->skb).portid,
  2516. cb->nlh->nlmsg_seq,
  2517. NLM_F_MULTI);
  2518. if (err) {
  2519. mutex_unlock(&devlink->lock);
  2520. goto out;
  2521. }
  2522. idx++;
  2523. }
  2524. mutex_unlock(&devlink->lock);
  2525. }
  2526. out:
  2527. mutex_unlock(&devlink_mutex);
  2528. cb->args[0] = idx;
  2529. return msg->len;
  2530. }
  2531. static int
  2532. devlink_param_type_get_from_info(struct genl_info *info,
  2533. enum devlink_param_type *param_type)
  2534. {
  2535. if (!info->attrs[DEVLINK_ATTR_PARAM_TYPE])
  2536. return -EINVAL;
  2537. switch (nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_TYPE])) {
  2538. case NLA_U8:
  2539. *param_type = DEVLINK_PARAM_TYPE_U8;
  2540. break;
  2541. case NLA_U16:
  2542. *param_type = DEVLINK_PARAM_TYPE_U16;
  2543. break;
  2544. case NLA_U32:
  2545. *param_type = DEVLINK_PARAM_TYPE_U32;
  2546. break;
  2547. case NLA_STRING:
  2548. *param_type = DEVLINK_PARAM_TYPE_STRING;
  2549. break;
  2550. case NLA_FLAG:
  2551. *param_type = DEVLINK_PARAM_TYPE_BOOL;
  2552. break;
  2553. default:
  2554. return -EINVAL;
  2555. }
  2556. return 0;
  2557. }
  2558. static int
  2559. devlink_param_value_get_from_info(const struct devlink_param *param,
  2560. struct genl_info *info,
  2561. union devlink_param_value *value)
  2562. {
  2563. struct nlattr *param_data;
  2564. int len;
  2565. param_data = info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA];
  2566. if (param->type != DEVLINK_PARAM_TYPE_BOOL && !param_data)
  2567. return -EINVAL;
  2568. switch (param->type) {
  2569. case DEVLINK_PARAM_TYPE_U8:
  2570. if (nla_len(param_data) != sizeof(u8))
  2571. return -EINVAL;
  2572. value->vu8 = nla_get_u8(param_data);
  2573. break;
  2574. case DEVLINK_PARAM_TYPE_U16:
  2575. if (nla_len(param_data) != sizeof(u16))
  2576. return -EINVAL;
  2577. value->vu16 = nla_get_u16(param_data);
  2578. break;
  2579. case DEVLINK_PARAM_TYPE_U32:
  2580. if (nla_len(param_data) != sizeof(u32))
  2581. return -EINVAL;
  2582. value->vu32 = nla_get_u32(param_data);
  2583. break;
  2584. case DEVLINK_PARAM_TYPE_STRING:
  2585. len = strnlen(nla_data(param_data), nla_len(param_data));
  2586. if (len == nla_len(param_data) ||
  2587. len >= __DEVLINK_PARAM_MAX_STRING_VALUE)
  2588. return -EINVAL;
  2589. strcpy(value->vstr, nla_data(param_data));
  2590. break;
  2591. case DEVLINK_PARAM_TYPE_BOOL:
  2592. if (param_data && nla_len(param_data))
  2593. return -EINVAL;
  2594. value->vbool = nla_get_flag(param_data);
  2595. break;
  2596. }
  2597. return 0;
  2598. }
  2599. static struct devlink_param_item *
  2600. devlink_param_get_from_info(struct devlink *devlink,
  2601. struct genl_info *info)
  2602. {
  2603. char *param_name;
  2604. if (!info->attrs[DEVLINK_ATTR_PARAM_NAME])
  2605. return NULL;
  2606. param_name = nla_data(info->attrs[DEVLINK_ATTR_PARAM_NAME]);
  2607. return devlink_param_find_by_name(&devlink->param_list, param_name);
  2608. }
  2609. static int devlink_nl_cmd_param_get_doit(struct sk_buff *skb,
  2610. struct genl_info *info)
  2611. {
  2612. struct devlink *devlink = info->user_ptr[0];
  2613. struct devlink_param_item *param_item;
  2614. struct sk_buff *msg;
  2615. int err;
  2616. param_item = devlink_param_get_from_info(devlink, info);
  2617. if (!param_item)
  2618. return -EINVAL;
  2619. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  2620. if (!msg)
  2621. return -ENOMEM;
  2622. err = devlink_nl_param_fill(msg, devlink, param_item,
  2623. DEVLINK_CMD_PARAM_GET,
  2624. info->snd_portid, info->snd_seq, 0);
  2625. if (err) {
  2626. nlmsg_free(msg);
  2627. return err;
  2628. }
  2629. return genlmsg_reply(msg, info);
  2630. }
  2631. static int devlink_nl_cmd_param_set_doit(struct sk_buff *skb,
  2632. struct genl_info *info)
  2633. {
  2634. struct devlink *devlink = info->user_ptr[0];
  2635. enum devlink_param_type param_type;
  2636. struct devlink_param_gset_ctx ctx;
  2637. enum devlink_param_cmode cmode;
  2638. struct devlink_param_item *param_item;
  2639. const struct devlink_param *param;
  2640. union devlink_param_value value;
  2641. int err = 0;
  2642. param_item = devlink_param_get_from_info(devlink, info);
  2643. if (!param_item)
  2644. return -EINVAL;
  2645. param = param_item->param;
  2646. err = devlink_param_type_get_from_info(info, &param_type);
  2647. if (err)
  2648. return err;
  2649. if (param_type != param->type)
  2650. return -EINVAL;
  2651. err = devlink_param_value_get_from_info(param, info, &value);
  2652. if (err)
  2653. return err;
  2654. if (param->validate) {
  2655. err = param->validate(devlink, param->id, value, info->extack);
  2656. if (err)
  2657. return err;
  2658. }
  2659. if (!info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE])
  2660. return -EINVAL;
  2661. cmode = nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE]);
  2662. if (!devlink_param_cmode_is_supported(param, cmode))
  2663. return -EOPNOTSUPP;
  2664. if (cmode == DEVLINK_PARAM_CMODE_DRIVERINIT) {
  2665. if (param->type == DEVLINK_PARAM_TYPE_STRING)
  2666. strcpy(param_item->driverinit_value.vstr, value.vstr);
  2667. else
  2668. param_item->driverinit_value = value;
  2669. param_item->driverinit_value_valid = true;
  2670. } else {
  2671. if (!param->set)
  2672. return -EOPNOTSUPP;
  2673. ctx.val = value;
  2674. ctx.cmode = cmode;
  2675. err = devlink_param_set(devlink, param, &ctx);
  2676. if (err)
  2677. return err;
  2678. }
  2679. devlink_param_notify(devlink, param_item, DEVLINK_CMD_PARAM_NEW);
  2680. return 0;
  2681. }
  2682. static int devlink_param_register_one(struct devlink *devlink,
  2683. const struct devlink_param *param)
  2684. {
  2685. struct devlink_param_item *param_item;
  2686. if (devlink_param_find_by_name(&devlink->param_list,
  2687. param->name))
  2688. return -EEXIST;
  2689. if (param->supported_cmodes == BIT(DEVLINK_PARAM_CMODE_DRIVERINIT))
  2690. WARN_ON(param->get || param->set);
  2691. else
  2692. WARN_ON(!param->get || !param->set);
  2693. param_item = kzalloc(sizeof(*param_item), GFP_KERNEL);
  2694. if (!param_item)
  2695. return -ENOMEM;
  2696. param_item->param = param;
  2697. list_add_tail(&param_item->list, &devlink->param_list);
  2698. devlink_param_notify(devlink, param_item, DEVLINK_CMD_PARAM_NEW);
  2699. return 0;
  2700. }
  2701. static void devlink_param_unregister_one(struct devlink *devlink,
  2702. const struct devlink_param *param)
  2703. {
  2704. struct devlink_param_item *param_item;
  2705. param_item = devlink_param_find_by_name(&devlink->param_list,
  2706. param->name);
  2707. WARN_ON(!param_item);
  2708. devlink_param_notify(devlink, param_item, DEVLINK_CMD_PARAM_DEL);
  2709. list_del(&param_item->list);
  2710. kfree(param_item);
  2711. }
  2712. static int devlink_nl_region_snapshot_id_put(struct sk_buff *msg,
  2713. struct devlink *devlink,
  2714. struct devlink_snapshot *snapshot)
  2715. {
  2716. struct nlattr *snap_attr;
  2717. int err;
  2718. snap_attr = nla_nest_start(msg, DEVLINK_ATTR_REGION_SNAPSHOT);
  2719. if (!snap_attr)
  2720. return -EINVAL;
  2721. err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID, snapshot->id);
  2722. if (err)
  2723. goto nla_put_failure;
  2724. nla_nest_end(msg, snap_attr);
  2725. return 0;
  2726. nla_put_failure:
  2727. nla_nest_cancel(msg, snap_attr);
  2728. return err;
  2729. }
  2730. static int devlink_nl_region_snapshots_id_put(struct sk_buff *msg,
  2731. struct devlink *devlink,
  2732. struct devlink_region *region)
  2733. {
  2734. struct devlink_snapshot *snapshot;
  2735. struct nlattr *snapshots_attr;
  2736. int err;
  2737. snapshots_attr = nla_nest_start(msg, DEVLINK_ATTR_REGION_SNAPSHOTS);
  2738. if (!snapshots_attr)
  2739. return -EINVAL;
  2740. list_for_each_entry(snapshot, &region->snapshot_list, list) {
  2741. err = devlink_nl_region_snapshot_id_put(msg, devlink, snapshot);
  2742. if (err)
  2743. goto nla_put_failure;
  2744. }
  2745. nla_nest_end(msg, snapshots_attr);
  2746. return 0;
  2747. nla_put_failure:
  2748. nla_nest_cancel(msg, snapshots_attr);
  2749. return err;
  2750. }
  2751. static int devlink_nl_region_fill(struct sk_buff *msg, struct devlink *devlink,
  2752. enum devlink_command cmd, u32 portid,
  2753. u32 seq, int flags,
  2754. struct devlink_region *region)
  2755. {
  2756. void *hdr;
  2757. int err;
  2758. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  2759. if (!hdr)
  2760. return -EMSGSIZE;
  2761. err = devlink_nl_put_handle(msg, devlink);
  2762. if (err)
  2763. goto nla_put_failure;
  2764. err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME, region->name);
  2765. if (err)
  2766. goto nla_put_failure;
  2767. err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE,
  2768. region->size,
  2769. DEVLINK_ATTR_PAD);
  2770. if (err)
  2771. goto nla_put_failure;
  2772. err = devlink_nl_region_snapshots_id_put(msg, devlink, region);
  2773. if (err)
  2774. goto nla_put_failure;
  2775. genlmsg_end(msg, hdr);
  2776. return 0;
  2777. nla_put_failure:
  2778. genlmsg_cancel(msg, hdr);
  2779. return err;
  2780. }
  2781. static void devlink_nl_region_notify(struct devlink_region *region,
  2782. struct devlink_snapshot *snapshot,
  2783. enum devlink_command cmd)
  2784. {
  2785. struct devlink *devlink = region->devlink;
  2786. struct sk_buff *msg;
  2787. void *hdr;
  2788. int err;
  2789. WARN_ON(cmd != DEVLINK_CMD_REGION_NEW && cmd != DEVLINK_CMD_REGION_DEL);
  2790. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  2791. if (!msg)
  2792. return;
  2793. hdr = genlmsg_put(msg, 0, 0, &devlink_nl_family, 0, cmd);
  2794. if (!hdr)
  2795. goto out_free_msg;
  2796. err = devlink_nl_put_handle(msg, devlink);
  2797. if (err)
  2798. goto out_cancel_msg;
  2799. err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME,
  2800. region->name);
  2801. if (err)
  2802. goto out_cancel_msg;
  2803. if (snapshot) {
  2804. err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID,
  2805. snapshot->id);
  2806. if (err)
  2807. goto out_cancel_msg;
  2808. } else {
  2809. err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE,
  2810. region->size, DEVLINK_ATTR_PAD);
  2811. if (err)
  2812. goto out_cancel_msg;
  2813. }
  2814. genlmsg_end(msg, hdr);
  2815. genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
  2816. msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
  2817. return;
  2818. out_cancel_msg:
  2819. genlmsg_cancel(msg, hdr);
  2820. out_free_msg:
  2821. nlmsg_free(msg);
  2822. }
  2823. static int devlink_nl_cmd_region_get_doit(struct sk_buff *skb,
  2824. struct genl_info *info)
  2825. {
  2826. struct devlink *devlink = info->user_ptr[0];
  2827. struct devlink_region *region;
  2828. const char *region_name;
  2829. struct sk_buff *msg;
  2830. int err;
  2831. if (!info->attrs[DEVLINK_ATTR_REGION_NAME])
  2832. return -EINVAL;
  2833. region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
  2834. region = devlink_region_get_by_name(devlink, region_name);
  2835. if (!region)
  2836. return -EINVAL;
  2837. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  2838. if (!msg)
  2839. return -ENOMEM;
  2840. err = devlink_nl_region_fill(msg, devlink, DEVLINK_CMD_REGION_GET,
  2841. info->snd_portid, info->snd_seq, 0,
  2842. region);
  2843. if (err) {
  2844. nlmsg_free(msg);
  2845. return err;
  2846. }
  2847. return genlmsg_reply(msg, info);
  2848. }
  2849. static int devlink_nl_cmd_region_get_dumpit(struct sk_buff *msg,
  2850. struct netlink_callback *cb)
  2851. {
  2852. struct devlink_region *region;
  2853. struct devlink *devlink;
  2854. int start = cb->args[0];
  2855. int idx = 0;
  2856. int err;
  2857. mutex_lock(&devlink_mutex);
  2858. list_for_each_entry(devlink, &devlink_list, list) {
  2859. if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
  2860. continue;
  2861. mutex_lock(&devlink->lock);
  2862. list_for_each_entry(region, &devlink->region_list, list) {
  2863. if (idx < start) {
  2864. idx++;
  2865. continue;
  2866. }
  2867. err = devlink_nl_region_fill(msg, devlink,
  2868. DEVLINK_CMD_REGION_GET,
  2869. NETLINK_CB(cb->skb).portid,
  2870. cb->nlh->nlmsg_seq,
  2871. NLM_F_MULTI, region);
  2872. if (err) {
  2873. mutex_unlock(&devlink->lock);
  2874. goto out;
  2875. }
  2876. idx++;
  2877. }
  2878. mutex_unlock(&devlink->lock);
  2879. }
  2880. out:
  2881. mutex_unlock(&devlink_mutex);
  2882. cb->args[0] = idx;
  2883. return msg->len;
  2884. }
  2885. static int devlink_nl_cmd_region_del(struct sk_buff *skb,
  2886. struct genl_info *info)
  2887. {
  2888. struct devlink *devlink = info->user_ptr[0];
  2889. struct devlink_snapshot *snapshot;
  2890. struct devlink_region *region;
  2891. const char *region_name;
  2892. u32 snapshot_id;
  2893. if (!info->attrs[DEVLINK_ATTR_REGION_NAME] ||
  2894. !info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID])
  2895. return -EINVAL;
  2896. region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
  2897. snapshot_id = nla_get_u32(info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]);
  2898. region = devlink_region_get_by_name(devlink, region_name);
  2899. if (!region)
  2900. return -EINVAL;
  2901. snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id);
  2902. if (!snapshot)
  2903. return -EINVAL;
  2904. devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_DEL);
  2905. devlink_region_snapshot_del(snapshot);
  2906. return 0;
  2907. }
  2908. static int devlink_nl_cmd_region_read_chunk_fill(struct sk_buff *msg,
  2909. struct devlink *devlink,
  2910. u8 *chunk, u32 chunk_size,
  2911. u64 addr)
  2912. {
  2913. struct nlattr *chunk_attr;
  2914. int err;
  2915. chunk_attr = nla_nest_start(msg, DEVLINK_ATTR_REGION_CHUNK);
  2916. if (!chunk_attr)
  2917. return -EINVAL;
  2918. err = nla_put(msg, DEVLINK_ATTR_REGION_CHUNK_DATA, chunk_size, chunk);
  2919. if (err)
  2920. goto nla_put_failure;
  2921. err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_CHUNK_ADDR, addr,
  2922. DEVLINK_ATTR_PAD);
  2923. if (err)
  2924. goto nla_put_failure;
  2925. nla_nest_end(msg, chunk_attr);
  2926. return 0;
  2927. nla_put_failure:
  2928. nla_nest_cancel(msg, chunk_attr);
  2929. return err;
  2930. }
  2931. #define DEVLINK_REGION_READ_CHUNK_SIZE 256
  2932. static int devlink_nl_region_read_snapshot_fill(struct sk_buff *skb,
  2933. struct devlink *devlink,
  2934. struct devlink_region *region,
  2935. struct nlattr **attrs,
  2936. u64 start_offset,
  2937. u64 end_offset,
  2938. bool dump,
  2939. u64 *new_offset)
  2940. {
  2941. struct devlink_snapshot *snapshot;
  2942. u64 curr_offset = start_offset;
  2943. u32 snapshot_id;
  2944. int err = 0;
  2945. *new_offset = start_offset;
  2946. snapshot_id = nla_get_u32(attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]);
  2947. snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id);
  2948. if (!snapshot)
  2949. return -EINVAL;
  2950. if (end_offset > snapshot->data_len || dump)
  2951. end_offset = snapshot->data_len;
  2952. while (curr_offset < end_offset) {
  2953. u32 data_size;
  2954. u8 *data;
  2955. if (end_offset - curr_offset < DEVLINK_REGION_READ_CHUNK_SIZE)
  2956. data_size = end_offset - curr_offset;
  2957. else
  2958. data_size = DEVLINK_REGION_READ_CHUNK_SIZE;
  2959. data = &snapshot->data[curr_offset];
  2960. err = devlink_nl_cmd_region_read_chunk_fill(skb, devlink,
  2961. data, data_size,
  2962. curr_offset);
  2963. if (err)
  2964. break;
  2965. curr_offset += data_size;
  2966. }
  2967. *new_offset = curr_offset;
  2968. return err;
  2969. }
  2970. static int devlink_nl_cmd_region_read_dumpit(struct sk_buff *skb,
  2971. struct netlink_callback *cb)
  2972. {
  2973. u64 ret_offset, start_offset, end_offset = 0;
  2974. struct nlattr *attrs[DEVLINK_ATTR_MAX + 1];
  2975. const struct genl_ops *ops = cb->data;
  2976. struct devlink_region *region;
  2977. struct nlattr *chunks_attr;
  2978. const char *region_name;
  2979. struct devlink *devlink;
  2980. bool dump = true;
  2981. void *hdr;
  2982. int err;
  2983. start_offset = *((u64 *)&cb->args[0]);
  2984. err = nlmsg_parse(cb->nlh, GENL_HDRLEN + devlink_nl_family.hdrsize,
  2985. attrs, DEVLINK_ATTR_MAX, ops->policy, NULL);
  2986. if (err)
  2987. goto out;
  2988. devlink = devlink_get_from_attrs(sock_net(cb->skb->sk), attrs);
  2989. if (IS_ERR(devlink))
  2990. goto out;
  2991. mutex_lock(&devlink_mutex);
  2992. mutex_lock(&devlink->lock);
  2993. if (!attrs[DEVLINK_ATTR_REGION_NAME] ||
  2994. !attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID])
  2995. goto out_unlock;
  2996. region_name = nla_data(attrs[DEVLINK_ATTR_REGION_NAME]);
  2997. region = devlink_region_get_by_name(devlink, region_name);
  2998. if (!region)
  2999. goto out_unlock;
  3000. hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  3001. &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI,
  3002. DEVLINK_CMD_REGION_READ);
  3003. if (!hdr)
  3004. goto out_unlock;
  3005. err = devlink_nl_put_handle(skb, devlink);
  3006. if (err)
  3007. goto nla_put_failure;
  3008. err = nla_put_string(skb, DEVLINK_ATTR_REGION_NAME, region_name);
  3009. if (err)
  3010. goto nla_put_failure;
  3011. chunks_attr = nla_nest_start(skb, DEVLINK_ATTR_REGION_CHUNKS);
  3012. if (!chunks_attr)
  3013. goto nla_put_failure;
  3014. if (attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR] &&
  3015. attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]) {
  3016. if (!start_offset)
  3017. start_offset =
  3018. nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
  3019. end_offset = nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
  3020. end_offset += nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]);
  3021. dump = false;
  3022. }
  3023. err = devlink_nl_region_read_snapshot_fill(skb, devlink,
  3024. region, attrs,
  3025. start_offset,
  3026. end_offset, dump,
  3027. &ret_offset);
  3028. if (err && err != -EMSGSIZE)
  3029. goto nla_put_failure;
  3030. /* Check if there was any progress done to prevent infinite loop */
  3031. if (ret_offset == start_offset)
  3032. goto nla_put_failure;
  3033. *((u64 *)&cb->args[0]) = ret_offset;
  3034. nla_nest_end(skb, chunks_attr);
  3035. genlmsg_end(skb, hdr);
  3036. mutex_unlock(&devlink->lock);
  3037. mutex_unlock(&devlink_mutex);
  3038. return skb->len;
  3039. nla_put_failure:
  3040. genlmsg_cancel(skb, hdr);
  3041. out_unlock:
  3042. mutex_unlock(&devlink->lock);
  3043. mutex_unlock(&devlink_mutex);
  3044. out:
  3045. return 0;
  3046. }
  3047. static const struct nla_policy devlink_nl_policy[DEVLINK_ATTR_MAX + 1] = {
  3048. [DEVLINK_ATTR_BUS_NAME] = { .type = NLA_NUL_STRING },
  3049. [DEVLINK_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING },
  3050. [DEVLINK_ATTR_PORT_INDEX] = { .type = NLA_U32 },
  3051. [DEVLINK_ATTR_PORT_TYPE] = { .type = NLA_U16 },
  3052. [DEVLINK_ATTR_PORT_SPLIT_COUNT] = { .type = NLA_U32 },
  3053. [DEVLINK_ATTR_SB_INDEX] = { .type = NLA_U32 },
  3054. [DEVLINK_ATTR_SB_POOL_INDEX] = { .type = NLA_U16 },
  3055. [DEVLINK_ATTR_SB_POOL_TYPE] = { .type = NLA_U8 },
  3056. [DEVLINK_ATTR_SB_POOL_SIZE] = { .type = NLA_U32 },
  3057. [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE] = { .type = NLA_U8 },
  3058. [DEVLINK_ATTR_SB_THRESHOLD] = { .type = NLA_U32 },
  3059. [DEVLINK_ATTR_SB_TC_INDEX] = { .type = NLA_U16 },
  3060. [DEVLINK_ATTR_ESWITCH_MODE] = { .type = NLA_U16 },
  3061. [DEVLINK_ATTR_ESWITCH_INLINE_MODE] = { .type = NLA_U8 },
  3062. [DEVLINK_ATTR_ESWITCH_ENCAP_MODE] = { .type = NLA_U8 },
  3063. [DEVLINK_ATTR_DPIPE_TABLE_NAME] = { .type = NLA_NUL_STRING },
  3064. [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED] = { .type = NLA_U8 },
  3065. [DEVLINK_ATTR_RESOURCE_ID] = { .type = NLA_U64},
  3066. [DEVLINK_ATTR_RESOURCE_SIZE] = { .type = NLA_U64},
  3067. [DEVLINK_ATTR_PARAM_NAME] = { .type = NLA_NUL_STRING },
  3068. [DEVLINK_ATTR_PARAM_TYPE] = { .type = NLA_U8 },
  3069. [DEVLINK_ATTR_PARAM_VALUE_CMODE] = { .type = NLA_U8 },
  3070. [DEVLINK_ATTR_REGION_NAME] = { .type = NLA_NUL_STRING },
  3071. [DEVLINK_ATTR_REGION_SNAPSHOT_ID] = { .type = NLA_U32 },
  3072. [DEVLINK_ATTR_REGION_CHUNK_ADDR] = { .type = NLA_U64 },
  3073. [DEVLINK_ATTR_REGION_CHUNK_LEN] = { .type = NLA_U64 },
  3074. };
  3075. static const struct genl_ops devlink_nl_ops[] = {
  3076. {
  3077. .cmd = DEVLINK_CMD_GET,
  3078. .doit = devlink_nl_cmd_get_doit,
  3079. .dumpit = devlink_nl_cmd_get_dumpit,
  3080. .policy = devlink_nl_policy,
  3081. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  3082. /* can be retrieved by unprivileged users */
  3083. },
  3084. {
  3085. .cmd = DEVLINK_CMD_PORT_GET,
  3086. .doit = devlink_nl_cmd_port_get_doit,
  3087. .dumpit = devlink_nl_cmd_port_get_dumpit,
  3088. .policy = devlink_nl_policy,
  3089. .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
  3090. /* can be retrieved by unprivileged users */
  3091. },
  3092. {
  3093. .cmd = DEVLINK_CMD_PORT_SET,
  3094. .doit = devlink_nl_cmd_port_set_doit,
  3095. .policy = devlink_nl_policy,
  3096. .flags = GENL_ADMIN_PERM,
  3097. .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
  3098. },
  3099. {
  3100. .cmd = DEVLINK_CMD_PORT_SPLIT,
  3101. .doit = devlink_nl_cmd_port_split_doit,
  3102. .policy = devlink_nl_policy,
  3103. .flags = GENL_ADMIN_PERM,
  3104. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  3105. DEVLINK_NL_FLAG_NO_LOCK,
  3106. },
  3107. {
  3108. .cmd = DEVLINK_CMD_PORT_UNSPLIT,
  3109. .doit = devlink_nl_cmd_port_unsplit_doit,
  3110. .policy = devlink_nl_policy,
  3111. .flags = GENL_ADMIN_PERM,
  3112. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  3113. DEVLINK_NL_FLAG_NO_LOCK,
  3114. },
  3115. {
  3116. .cmd = DEVLINK_CMD_SB_GET,
  3117. .doit = devlink_nl_cmd_sb_get_doit,
  3118. .dumpit = devlink_nl_cmd_sb_get_dumpit,
  3119. .policy = devlink_nl_policy,
  3120. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  3121. DEVLINK_NL_FLAG_NEED_SB,
  3122. /* can be retrieved by unprivileged users */
  3123. },
  3124. {
  3125. .cmd = DEVLINK_CMD_SB_POOL_GET,
  3126. .doit = devlink_nl_cmd_sb_pool_get_doit,
  3127. .dumpit = devlink_nl_cmd_sb_pool_get_dumpit,
  3128. .policy = devlink_nl_policy,
  3129. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  3130. DEVLINK_NL_FLAG_NEED_SB,
  3131. /* can be retrieved by unprivileged users */
  3132. },
  3133. {
  3134. .cmd = DEVLINK_CMD_SB_POOL_SET,
  3135. .doit = devlink_nl_cmd_sb_pool_set_doit,
  3136. .policy = devlink_nl_policy,
  3137. .flags = GENL_ADMIN_PERM,
  3138. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  3139. DEVLINK_NL_FLAG_NEED_SB,
  3140. },
  3141. {
  3142. .cmd = DEVLINK_CMD_SB_PORT_POOL_GET,
  3143. .doit = devlink_nl_cmd_sb_port_pool_get_doit,
  3144. .dumpit = devlink_nl_cmd_sb_port_pool_get_dumpit,
  3145. .policy = devlink_nl_policy,
  3146. .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
  3147. DEVLINK_NL_FLAG_NEED_SB,
  3148. /* can be retrieved by unprivileged users */
  3149. },
  3150. {
  3151. .cmd = DEVLINK_CMD_SB_PORT_POOL_SET,
  3152. .doit = devlink_nl_cmd_sb_port_pool_set_doit,
  3153. .policy = devlink_nl_policy,
  3154. .flags = GENL_ADMIN_PERM,
  3155. .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
  3156. DEVLINK_NL_FLAG_NEED_SB,
  3157. },
  3158. {
  3159. .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_GET,
  3160. .doit = devlink_nl_cmd_sb_tc_pool_bind_get_doit,
  3161. .dumpit = devlink_nl_cmd_sb_tc_pool_bind_get_dumpit,
  3162. .policy = devlink_nl_policy,
  3163. .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
  3164. DEVLINK_NL_FLAG_NEED_SB,
  3165. /* can be retrieved by unprivileged users */
  3166. },
  3167. {
  3168. .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_SET,
  3169. .doit = devlink_nl_cmd_sb_tc_pool_bind_set_doit,
  3170. .policy = devlink_nl_policy,
  3171. .flags = GENL_ADMIN_PERM,
  3172. .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
  3173. DEVLINK_NL_FLAG_NEED_SB,
  3174. },
  3175. {
  3176. .cmd = DEVLINK_CMD_SB_OCC_SNAPSHOT,
  3177. .doit = devlink_nl_cmd_sb_occ_snapshot_doit,
  3178. .policy = devlink_nl_policy,
  3179. .flags = GENL_ADMIN_PERM,
  3180. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  3181. DEVLINK_NL_FLAG_NEED_SB,
  3182. },
  3183. {
  3184. .cmd = DEVLINK_CMD_SB_OCC_MAX_CLEAR,
  3185. .doit = devlink_nl_cmd_sb_occ_max_clear_doit,
  3186. .policy = devlink_nl_policy,
  3187. .flags = GENL_ADMIN_PERM,
  3188. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  3189. DEVLINK_NL_FLAG_NEED_SB,
  3190. },
  3191. {
  3192. .cmd = DEVLINK_CMD_ESWITCH_GET,
  3193. .doit = devlink_nl_cmd_eswitch_get_doit,
  3194. .policy = devlink_nl_policy,
  3195. .flags = GENL_ADMIN_PERM,
  3196. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  3197. },
  3198. {
  3199. .cmd = DEVLINK_CMD_ESWITCH_SET,
  3200. .doit = devlink_nl_cmd_eswitch_set_doit,
  3201. .policy = devlink_nl_policy,
  3202. .flags = GENL_ADMIN_PERM,
  3203. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  3204. DEVLINK_NL_FLAG_NO_LOCK,
  3205. },
  3206. {
  3207. .cmd = DEVLINK_CMD_DPIPE_TABLE_GET,
  3208. .doit = devlink_nl_cmd_dpipe_table_get,
  3209. .policy = devlink_nl_policy,
  3210. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  3211. /* can be retrieved by unprivileged users */
  3212. },
  3213. {
  3214. .cmd = DEVLINK_CMD_DPIPE_ENTRIES_GET,
  3215. .doit = devlink_nl_cmd_dpipe_entries_get,
  3216. .policy = devlink_nl_policy,
  3217. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  3218. /* can be retrieved by unprivileged users */
  3219. },
  3220. {
  3221. .cmd = DEVLINK_CMD_DPIPE_HEADERS_GET,
  3222. .doit = devlink_nl_cmd_dpipe_headers_get,
  3223. .policy = devlink_nl_policy,
  3224. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  3225. /* can be retrieved by unprivileged users */
  3226. },
  3227. {
  3228. .cmd = DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET,
  3229. .doit = devlink_nl_cmd_dpipe_table_counters_set,
  3230. .policy = devlink_nl_policy,
  3231. .flags = GENL_ADMIN_PERM,
  3232. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  3233. },
  3234. {
  3235. .cmd = DEVLINK_CMD_RESOURCE_SET,
  3236. .doit = devlink_nl_cmd_resource_set,
  3237. .policy = devlink_nl_policy,
  3238. .flags = GENL_ADMIN_PERM,
  3239. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  3240. },
  3241. {
  3242. .cmd = DEVLINK_CMD_RESOURCE_DUMP,
  3243. .doit = devlink_nl_cmd_resource_dump,
  3244. .policy = devlink_nl_policy,
  3245. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  3246. /* can be retrieved by unprivileged users */
  3247. },
  3248. {
  3249. .cmd = DEVLINK_CMD_RELOAD,
  3250. .doit = devlink_nl_cmd_reload,
  3251. .policy = devlink_nl_policy,
  3252. .flags = GENL_ADMIN_PERM,
  3253. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  3254. DEVLINK_NL_FLAG_NO_LOCK,
  3255. },
  3256. {
  3257. .cmd = DEVLINK_CMD_PARAM_GET,
  3258. .doit = devlink_nl_cmd_param_get_doit,
  3259. .dumpit = devlink_nl_cmd_param_get_dumpit,
  3260. .policy = devlink_nl_policy,
  3261. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  3262. /* can be retrieved by unprivileged users */
  3263. },
  3264. {
  3265. .cmd = DEVLINK_CMD_PARAM_SET,
  3266. .doit = devlink_nl_cmd_param_set_doit,
  3267. .policy = devlink_nl_policy,
  3268. .flags = GENL_ADMIN_PERM,
  3269. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  3270. },
  3271. {
  3272. .cmd = DEVLINK_CMD_REGION_GET,
  3273. .doit = devlink_nl_cmd_region_get_doit,
  3274. .dumpit = devlink_nl_cmd_region_get_dumpit,
  3275. .policy = devlink_nl_policy,
  3276. .flags = GENL_ADMIN_PERM,
  3277. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  3278. },
  3279. {
  3280. .cmd = DEVLINK_CMD_REGION_DEL,
  3281. .doit = devlink_nl_cmd_region_del,
  3282. .policy = devlink_nl_policy,
  3283. .flags = GENL_ADMIN_PERM,
  3284. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  3285. },
  3286. {
  3287. .cmd = DEVLINK_CMD_REGION_READ,
  3288. .dumpit = devlink_nl_cmd_region_read_dumpit,
  3289. .policy = devlink_nl_policy,
  3290. .flags = GENL_ADMIN_PERM,
  3291. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  3292. },
  3293. };
  3294. static struct genl_family devlink_nl_family __ro_after_init = {
  3295. .name = DEVLINK_GENL_NAME,
  3296. .version = DEVLINK_GENL_VERSION,
  3297. .maxattr = DEVLINK_ATTR_MAX,
  3298. .netnsok = true,
  3299. .pre_doit = devlink_nl_pre_doit,
  3300. .post_doit = devlink_nl_post_doit,
  3301. .module = THIS_MODULE,
  3302. .ops = devlink_nl_ops,
  3303. .n_ops = ARRAY_SIZE(devlink_nl_ops),
  3304. .mcgrps = devlink_nl_mcgrps,
  3305. .n_mcgrps = ARRAY_SIZE(devlink_nl_mcgrps),
  3306. };
  3307. /**
  3308. * devlink_alloc - Allocate new devlink instance resources
  3309. *
  3310. * @ops: ops
  3311. * @priv_size: size of user private data
  3312. *
  3313. * Allocate new devlink instance resources, including devlink index
  3314. * and name.
  3315. */
  3316. struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size)
  3317. {
  3318. struct devlink *devlink;
  3319. devlink = kzalloc(sizeof(*devlink) + priv_size, GFP_KERNEL);
  3320. if (!devlink)
  3321. return NULL;
  3322. devlink->ops = ops;
  3323. devlink_net_set(devlink, &init_net);
  3324. INIT_LIST_HEAD(&devlink->port_list);
  3325. INIT_LIST_HEAD(&devlink->sb_list);
  3326. INIT_LIST_HEAD_RCU(&devlink->dpipe_table_list);
  3327. INIT_LIST_HEAD(&devlink->resource_list);
  3328. INIT_LIST_HEAD(&devlink->param_list);
  3329. INIT_LIST_HEAD(&devlink->region_list);
  3330. mutex_init(&devlink->lock);
  3331. return devlink;
  3332. }
  3333. EXPORT_SYMBOL_GPL(devlink_alloc);
  3334. /**
  3335. * devlink_register - Register devlink instance
  3336. *
  3337. * @devlink: devlink
  3338. */
  3339. int devlink_register(struct devlink *devlink, struct device *dev)
  3340. {
  3341. mutex_lock(&devlink_mutex);
  3342. devlink->dev = dev;
  3343. list_add_tail(&devlink->list, &devlink_list);
  3344. devlink_notify(devlink, DEVLINK_CMD_NEW);
  3345. mutex_unlock(&devlink_mutex);
  3346. return 0;
  3347. }
  3348. EXPORT_SYMBOL_GPL(devlink_register);
  3349. /**
  3350. * devlink_unregister - Unregister devlink instance
  3351. *
  3352. * @devlink: devlink
  3353. */
  3354. void devlink_unregister(struct devlink *devlink)
  3355. {
  3356. mutex_lock(&devlink_mutex);
  3357. devlink_notify(devlink, DEVLINK_CMD_DEL);
  3358. list_del(&devlink->list);
  3359. mutex_unlock(&devlink_mutex);
  3360. }
  3361. EXPORT_SYMBOL_GPL(devlink_unregister);
  3362. /**
  3363. * devlink_free - Free devlink instance resources
  3364. *
  3365. * @devlink: devlink
  3366. */
  3367. void devlink_free(struct devlink *devlink)
  3368. {
  3369. kfree(devlink);
  3370. }
  3371. EXPORT_SYMBOL_GPL(devlink_free);
  3372. /**
  3373. * devlink_port_register - Register devlink port
  3374. *
  3375. * @devlink: devlink
  3376. * @devlink_port: devlink port
  3377. * @port_index
  3378. *
  3379. * Register devlink port with provided port index. User can use
  3380. * any indexing, even hw-related one. devlink_port structure
  3381. * is convenient to be embedded inside user driver private structure.
  3382. * Note that the caller should take care of zeroing the devlink_port
  3383. * structure.
  3384. */
  3385. int devlink_port_register(struct devlink *devlink,
  3386. struct devlink_port *devlink_port,
  3387. unsigned int port_index)
  3388. {
  3389. mutex_lock(&devlink->lock);
  3390. if (devlink_port_index_exists(devlink, port_index)) {
  3391. mutex_unlock(&devlink->lock);
  3392. return -EEXIST;
  3393. }
  3394. devlink_port->devlink = devlink;
  3395. devlink_port->index = port_index;
  3396. devlink_port->registered = true;
  3397. list_add_tail(&devlink_port->list, &devlink->port_list);
  3398. mutex_unlock(&devlink->lock);
  3399. devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
  3400. return 0;
  3401. }
  3402. EXPORT_SYMBOL_GPL(devlink_port_register);
  3403. /**
  3404. * devlink_port_unregister - Unregister devlink port
  3405. *
  3406. * @devlink_port: devlink port
  3407. */
  3408. void devlink_port_unregister(struct devlink_port *devlink_port)
  3409. {
  3410. struct devlink *devlink = devlink_port->devlink;
  3411. devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL);
  3412. mutex_lock(&devlink->lock);
  3413. list_del(&devlink_port->list);
  3414. mutex_unlock(&devlink->lock);
  3415. }
  3416. EXPORT_SYMBOL_GPL(devlink_port_unregister);
  3417. static void __devlink_port_type_set(struct devlink_port *devlink_port,
  3418. enum devlink_port_type type,
  3419. void *type_dev)
  3420. {
  3421. devlink_port->type = type;
  3422. devlink_port->type_dev = type_dev;
  3423. devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
  3424. }
  3425. /**
  3426. * devlink_port_type_eth_set - Set port type to Ethernet
  3427. *
  3428. * @devlink_port: devlink port
  3429. * @netdev: related netdevice
  3430. */
  3431. void devlink_port_type_eth_set(struct devlink_port *devlink_port,
  3432. struct net_device *netdev)
  3433. {
  3434. return __devlink_port_type_set(devlink_port,
  3435. DEVLINK_PORT_TYPE_ETH, netdev);
  3436. }
  3437. EXPORT_SYMBOL_GPL(devlink_port_type_eth_set);
  3438. /**
  3439. * devlink_port_type_ib_set - Set port type to InfiniBand
  3440. *
  3441. * @devlink_port: devlink port
  3442. * @ibdev: related IB device
  3443. */
  3444. void devlink_port_type_ib_set(struct devlink_port *devlink_port,
  3445. struct ib_device *ibdev)
  3446. {
  3447. return __devlink_port_type_set(devlink_port,
  3448. DEVLINK_PORT_TYPE_IB, ibdev);
  3449. }
  3450. EXPORT_SYMBOL_GPL(devlink_port_type_ib_set);
  3451. /**
  3452. * devlink_port_type_clear - Clear port type
  3453. *
  3454. * @devlink_port: devlink port
  3455. */
  3456. void devlink_port_type_clear(struct devlink_port *devlink_port)
  3457. {
  3458. return __devlink_port_type_set(devlink_port,
  3459. DEVLINK_PORT_TYPE_NOTSET, NULL);
  3460. }
  3461. EXPORT_SYMBOL_GPL(devlink_port_type_clear);
  3462. /**
  3463. * devlink_port_attrs_set - Set port attributes
  3464. *
  3465. * @devlink_port: devlink port
  3466. * @flavour: flavour of the port
  3467. * @port_number: number of the port that is facing user, for example
  3468. * the front panel port number
  3469. * @split: indicates if this is split port
  3470. * @split_subport_number: if the port is split, this is the number
  3471. * of subport.
  3472. */
  3473. void devlink_port_attrs_set(struct devlink_port *devlink_port,
  3474. enum devlink_port_flavour flavour,
  3475. u32 port_number, bool split,
  3476. u32 split_subport_number)
  3477. {
  3478. struct devlink_port_attrs *attrs = &devlink_port->attrs;
  3479. attrs->set = true;
  3480. attrs->flavour = flavour;
  3481. attrs->port_number = port_number;
  3482. attrs->split = split;
  3483. attrs->split_subport_number = split_subport_number;
  3484. devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
  3485. }
  3486. EXPORT_SYMBOL_GPL(devlink_port_attrs_set);
  3487. int devlink_port_get_phys_port_name(struct devlink_port *devlink_port,
  3488. char *name, size_t len)
  3489. {
  3490. struct devlink_port_attrs *attrs = &devlink_port->attrs;
  3491. int n = 0;
  3492. if (!attrs->set)
  3493. return -EOPNOTSUPP;
  3494. switch (attrs->flavour) {
  3495. case DEVLINK_PORT_FLAVOUR_PHYSICAL:
  3496. if (!attrs->split)
  3497. n = snprintf(name, len, "p%u", attrs->port_number);
  3498. else
  3499. n = snprintf(name, len, "p%us%u", attrs->port_number,
  3500. attrs->split_subport_number);
  3501. break;
  3502. case DEVLINK_PORT_FLAVOUR_CPU:
  3503. case DEVLINK_PORT_FLAVOUR_DSA:
  3504. /* As CPU and DSA ports do not have a netdevice associated
  3505. * case should not ever happen.
  3506. */
  3507. WARN_ON(1);
  3508. return -EINVAL;
  3509. }
  3510. if (n >= len)
  3511. return -EINVAL;
  3512. return 0;
  3513. }
  3514. EXPORT_SYMBOL_GPL(devlink_port_get_phys_port_name);
  3515. int devlink_sb_register(struct devlink *devlink, unsigned int sb_index,
  3516. u32 size, u16 ingress_pools_count,
  3517. u16 egress_pools_count, u16 ingress_tc_count,
  3518. u16 egress_tc_count)
  3519. {
  3520. struct devlink_sb *devlink_sb;
  3521. int err = 0;
  3522. mutex_lock(&devlink->lock);
  3523. if (devlink_sb_index_exists(devlink, sb_index)) {
  3524. err = -EEXIST;
  3525. goto unlock;
  3526. }
  3527. devlink_sb = kzalloc(sizeof(*devlink_sb), GFP_KERNEL);
  3528. if (!devlink_sb) {
  3529. err = -ENOMEM;
  3530. goto unlock;
  3531. }
  3532. devlink_sb->index = sb_index;
  3533. devlink_sb->size = size;
  3534. devlink_sb->ingress_pools_count = ingress_pools_count;
  3535. devlink_sb->egress_pools_count = egress_pools_count;
  3536. devlink_sb->ingress_tc_count = ingress_tc_count;
  3537. devlink_sb->egress_tc_count = egress_tc_count;
  3538. list_add_tail(&devlink_sb->list, &devlink->sb_list);
  3539. unlock:
  3540. mutex_unlock(&devlink->lock);
  3541. return err;
  3542. }
  3543. EXPORT_SYMBOL_GPL(devlink_sb_register);
  3544. void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index)
  3545. {
  3546. struct devlink_sb *devlink_sb;
  3547. mutex_lock(&devlink->lock);
  3548. devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
  3549. WARN_ON(!devlink_sb);
  3550. list_del(&devlink_sb->list);
  3551. mutex_unlock(&devlink->lock);
  3552. kfree(devlink_sb);
  3553. }
  3554. EXPORT_SYMBOL_GPL(devlink_sb_unregister);
  3555. /**
  3556. * devlink_dpipe_headers_register - register dpipe headers
  3557. *
  3558. * @devlink: devlink
  3559. * @dpipe_headers: dpipe header array
  3560. *
  3561. * Register the headers supported by hardware.
  3562. */
  3563. int devlink_dpipe_headers_register(struct devlink *devlink,
  3564. struct devlink_dpipe_headers *dpipe_headers)
  3565. {
  3566. mutex_lock(&devlink->lock);
  3567. devlink->dpipe_headers = dpipe_headers;
  3568. mutex_unlock(&devlink->lock);
  3569. return 0;
  3570. }
  3571. EXPORT_SYMBOL_GPL(devlink_dpipe_headers_register);
  3572. /**
  3573. * devlink_dpipe_headers_unregister - unregister dpipe headers
  3574. *
  3575. * @devlink: devlink
  3576. *
  3577. * Unregister the headers supported by hardware.
  3578. */
  3579. void devlink_dpipe_headers_unregister(struct devlink *devlink)
  3580. {
  3581. mutex_lock(&devlink->lock);
  3582. devlink->dpipe_headers = NULL;
  3583. mutex_unlock(&devlink->lock);
  3584. }
  3585. EXPORT_SYMBOL_GPL(devlink_dpipe_headers_unregister);
  3586. /**
  3587. * devlink_dpipe_table_counter_enabled - check if counter allocation
  3588. * required
  3589. * @devlink: devlink
  3590. * @table_name: tables name
  3591. *
  3592. * Used by driver to check if counter allocation is required.
  3593. * After counter allocation is turned on the table entries
  3594. * are updated to include counter statistics.
  3595. *
  3596. * After that point on the driver must respect the counter
  3597. * state so that each entry added to the table is added
  3598. * with a counter.
  3599. */
  3600. bool devlink_dpipe_table_counter_enabled(struct devlink *devlink,
  3601. const char *table_name)
  3602. {
  3603. struct devlink_dpipe_table *table;
  3604. bool enabled;
  3605. rcu_read_lock();
  3606. table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
  3607. table_name);
  3608. enabled = false;
  3609. if (table)
  3610. enabled = table->counters_enabled;
  3611. rcu_read_unlock();
  3612. return enabled;
  3613. }
  3614. EXPORT_SYMBOL_GPL(devlink_dpipe_table_counter_enabled);
  3615. /**
  3616. * devlink_dpipe_table_register - register dpipe table
  3617. *
  3618. * @devlink: devlink
  3619. * @table_name: table name
  3620. * @table_ops: table ops
  3621. * @priv: priv
  3622. * @counter_control_extern: external control for counters
  3623. */
  3624. int devlink_dpipe_table_register(struct devlink *devlink,
  3625. const char *table_name,
  3626. struct devlink_dpipe_table_ops *table_ops,
  3627. void *priv, bool counter_control_extern)
  3628. {
  3629. struct devlink_dpipe_table *table;
  3630. if (devlink_dpipe_table_find(&devlink->dpipe_table_list, table_name))
  3631. return -EEXIST;
  3632. if (WARN_ON(!table_ops->size_get))
  3633. return -EINVAL;
  3634. table = kzalloc(sizeof(*table), GFP_KERNEL);
  3635. if (!table)
  3636. return -ENOMEM;
  3637. table->name = table_name;
  3638. table->table_ops = table_ops;
  3639. table->priv = priv;
  3640. table->counter_control_extern = counter_control_extern;
  3641. mutex_lock(&devlink->lock);
  3642. list_add_tail_rcu(&table->list, &devlink->dpipe_table_list);
  3643. mutex_unlock(&devlink->lock);
  3644. return 0;
  3645. }
  3646. EXPORT_SYMBOL_GPL(devlink_dpipe_table_register);
  3647. /**
  3648. * devlink_dpipe_table_unregister - unregister dpipe table
  3649. *
  3650. * @devlink: devlink
  3651. * @table_name: table name
  3652. */
  3653. void devlink_dpipe_table_unregister(struct devlink *devlink,
  3654. const char *table_name)
  3655. {
  3656. struct devlink_dpipe_table *table;
  3657. mutex_lock(&devlink->lock);
  3658. table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
  3659. table_name);
  3660. if (!table)
  3661. goto unlock;
  3662. list_del_rcu(&table->list);
  3663. mutex_unlock(&devlink->lock);
  3664. kfree_rcu(table, rcu);
  3665. return;
  3666. unlock:
  3667. mutex_unlock(&devlink->lock);
  3668. }
  3669. EXPORT_SYMBOL_GPL(devlink_dpipe_table_unregister);
  3670. /**
  3671. * devlink_resource_register - devlink resource register
  3672. *
  3673. * @devlink: devlink
  3674. * @resource_name: resource's name
  3675. * @top_hierarchy: top hierarchy
  3676. * @reload_required: reload is required for new configuration to
  3677. * apply
  3678. * @resource_size: resource's size
  3679. * @resource_id: resource's id
  3680. * @parent_reosurce_id: resource's parent id
  3681. * @size params: size parameters
  3682. */
  3683. int devlink_resource_register(struct devlink *devlink,
  3684. const char *resource_name,
  3685. u64 resource_size,
  3686. u64 resource_id,
  3687. u64 parent_resource_id,
  3688. const struct devlink_resource_size_params *size_params)
  3689. {
  3690. struct devlink_resource *resource;
  3691. struct list_head *resource_list;
  3692. bool top_hierarchy;
  3693. int err = 0;
  3694. top_hierarchy = parent_resource_id == DEVLINK_RESOURCE_ID_PARENT_TOP;
  3695. mutex_lock(&devlink->lock);
  3696. resource = devlink_resource_find(devlink, NULL, resource_id);
  3697. if (resource) {
  3698. err = -EINVAL;
  3699. goto out;
  3700. }
  3701. resource = kzalloc(sizeof(*resource), GFP_KERNEL);
  3702. if (!resource) {
  3703. err = -ENOMEM;
  3704. goto out;
  3705. }
  3706. if (top_hierarchy) {
  3707. resource_list = &devlink->resource_list;
  3708. } else {
  3709. struct devlink_resource *parent_resource;
  3710. parent_resource = devlink_resource_find(devlink, NULL,
  3711. parent_resource_id);
  3712. if (parent_resource) {
  3713. resource_list = &parent_resource->resource_list;
  3714. resource->parent = parent_resource;
  3715. } else {
  3716. kfree(resource);
  3717. err = -EINVAL;
  3718. goto out;
  3719. }
  3720. }
  3721. resource->name = resource_name;
  3722. resource->size = resource_size;
  3723. resource->size_new = resource_size;
  3724. resource->id = resource_id;
  3725. resource->size_valid = true;
  3726. memcpy(&resource->size_params, size_params,
  3727. sizeof(resource->size_params));
  3728. INIT_LIST_HEAD(&resource->resource_list);
  3729. list_add_tail(&resource->list, resource_list);
  3730. out:
  3731. mutex_unlock(&devlink->lock);
  3732. return err;
  3733. }
  3734. EXPORT_SYMBOL_GPL(devlink_resource_register);
  3735. /**
  3736. * devlink_resources_unregister - free all resources
  3737. *
  3738. * @devlink: devlink
  3739. * @resource: resource
  3740. */
  3741. void devlink_resources_unregister(struct devlink *devlink,
  3742. struct devlink_resource *resource)
  3743. {
  3744. struct devlink_resource *tmp, *child_resource;
  3745. struct list_head *resource_list;
  3746. if (resource)
  3747. resource_list = &resource->resource_list;
  3748. else
  3749. resource_list = &devlink->resource_list;
  3750. if (!resource)
  3751. mutex_lock(&devlink->lock);
  3752. list_for_each_entry_safe(child_resource, tmp, resource_list, list) {
  3753. devlink_resources_unregister(devlink, child_resource);
  3754. list_del(&child_resource->list);
  3755. kfree(child_resource);
  3756. }
  3757. if (!resource)
  3758. mutex_unlock(&devlink->lock);
  3759. }
  3760. EXPORT_SYMBOL_GPL(devlink_resources_unregister);
  3761. /**
  3762. * devlink_resource_size_get - get and update size
  3763. *
  3764. * @devlink: devlink
  3765. * @resource_id: the requested resource id
  3766. * @p_resource_size: ptr to update
  3767. */
  3768. int devlink_resource_size_get(struct devlink *devlink,
  3769. u64 resource_id,
  3770. u64 *p_resource_size)
  3771. {
  3772. struct devlink_resource *resource;
  3773. int err = 0;
  3774. mutex_lock(&devlink->lock);
  3775. resource = devlink_resource_find(devlink, NULL, resource_id);
  3776. if (!resource) {
  3777. err = -EINVAL;
  3778. goto out;
  3779. }
  3780. *p_resource_size = resource->size_new;
  3781. resource->size = resource->size_new;
  3782. out:
  3783. mutex_unlock(&devlink->lock);
  3784. return err;
  3785. }
  3786. EXPORT_SYMBOL_GPL(devlink_resource_size_get);
  3787. /**
  3788. * devlink_dpipe_table_resource_set - set the resource id
  3789. *
  3790. * @devlink: devlink
  3791. * @table_name: table name
  3792. * @resource_id: resource id
  3793. * @resource_units: number of resource's units consumed per table's entry
  3794. */
  3795. int devlink_dpipe_table_resource_set(struct devlink *devlink,
  3796. const char *table_name, u64 resource_id,
  3797. u64 resource_units)
  3798. {
  3799. struct devlink_dpipe_table *table;
  3800. int err = 0;
  3801. mutex_lock(&devlink->lock);
  3802. table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
  3803. table_name);
  3804. if (!table) {
  3805. err = -EINVAL;
  3806. goto out;
  3807. }
  3808. table->resource_id = resource_id;
  3809. table->resource_units = resource_units;
  3810. table->resource_valid = true;
  3811. out:
  3812. mutex_unlock(&devlink->lock);
  3813. return err;
  3814. }
  3815. EXPORT_SYMBOL_GPL(devlink_dpipe_table_resource_set);
  3816. /**
  3817. * devlink_resource_occ_get_register - register occupancy getter
  3818. *
  3819. * @devlink: devlink
  3820. * @resource_id: resource id
  3821. * @occ_get: occupancy getter callback
  3822. * @occ_get_priv: occupancy getter callback priv
  3823. */
  3824. void devlink_resource_occ_get_register(struct devlink *devlink,
  3825. u64 resource_id,
  3826. devlink_resource_occ_get_t *occ_get,
  3827. void *occ_get_priv)
  3828. {
  3829. struct devlink_resource *resource;
  3830. mutex_lock(&devlink->lock);
  3831. resource = devlink_resource_find(devlink, NULL, resource_id);
  3832. if (WARN_ON(!resource))
  3833. goto out;
  3834. WARN_ON(resource->occ_get);
  3835. resource->occ_get = occ_get;
  3836. resource->occ_get_priv = occ_get_priv;
  3837. out:
  3838. mutex_unlock(&devlink->lock);
  3839. }
  3840. EXPORT_SYMBOL_GPL(devlink_resource_occ_get_register);
  3841. /**
  3842. * devlink_resource_occ_get_unregister - unregister occupancy getter
  3843. *
  3844. * @devlink: devlink
  3845. * @resource_id: resource id
  3846. */
  3847. void devlink_resource_occ_get_unregister(struct devlink *devlink,
  3848. u64 resource_id)
  3849. {
  3850. struct devlink_resource *resource;
  3851. mutex_lock(&devlink->lock);
  3852. resource = devlink_resource_find(devlink, NULL, resource_id);
  3853. if (WARN_ON(!resource))
  3854. goto out;
  3855. WARN_ON(!resource->occ_get);
  3856. resource->occ_get = NULL;
  3857. resource->occ_get_priv = NULL;
  3858. out:
  3859. mutex_unlock(&devlink->lock);
  3860. }
  3861. EXPORT_SYMBOL_GPL(devlink_resource_occ_get_unregister);
  3862. /**
  3863. * devlink_params_register - register configuration parameters
  3864. *
  3865. * @devlink: devlink
  3866. * @params: configuration parameters array
  3867. * @params_count: number of parameters provided
  3868. *
  3869. * Register the configuration parameters supported by the driver.
  3870. */
  3871. int devlink_params_register(struct devlink *devlink,
  3872. const struct devlink_param *params,
  3873. size_t params_count)
  3874. {
  3875. const struct devlink_param *param = params;
  3876. int i;
  3877. int err;
  3878. mutex_lock(&devlink->lock);
  3879. for (i = 0; i < params_count; i++, param++) {
  3880. if (!param || !param->name || !param->supported_cmodes) {
  3881. err = -EINVAL;
  3882. goto rollback;
  3883. }
  3884. if (param->generic) {
  3885. err = devlink_param_generic_verify(param);
  3886. if (err)
  3887. goto rollback;
  3888. } else {
  3889. err = devlink_param_driver_verify(param);
  3890. if (err)
  3891. goto rollback;
  3892. }
  3893. err = devlink_param_register_one(devlink, param);
  3894. if (err)
  3895. goto rollback;
  3896. }
  3897. mutex_unlock(&devlink->lock);
  3898. return 0;
  3899. rollback:
  3900. if (!i)
  3901. goto unlock;
  3902. for (param--; i > 0; i--, param--)
  3903. devlink_param_unregister_one(devlink, param);
  3904. unlock:
  3905. mutex_unlock(&devlink->lock);
  3906. return err;
  3907. }
  3908. EXPORT_SYMBOL_GPL(devlink_params_register);
  3909. /**
  3910. * devlink_params_unregister - unregister configuration parameters
  3911. * @devlink: devlink
  3912. * @params: configuration parameters to unregister
  3913. * @params_count: number of parameters provided
  3914. */
  3915. void devlink_params_unregister(struct devlink *devlink,
  3916. const struct devlink_param *params,
  3917. size_t params_count)
  3918. {
  3919. const struct devlink_param *param = params;
  3920. int i;
  3921. mutex_lock(&devlink->lock);
  3922. for (i = 0; i < params_count; i++, param++)
  3923. devlink_param_unregister_one(devlink, param);
  3924. mutex_unlock(&devlink->lock);
  3925. }
  3926. EXPORT_SYMBOL_GPL(devlink_params_unregister);
  3927. /**
  3928. * devlink_param_driverinit_value_get - get configuration parameter
  3929. * value for driver initializing
  3930. *
  3931. * @devlink: devlink
  3932. * @param_id: parameter ID
  3933. * @init_val: value of parameter in driverinit configuration mode
  3934. *
  3935. * This function should be used by the driver to get driverinit
  3936. * configuration for initialization after reload command.
  3937. */
  3938. int devlink_param_driverinit_value_get(struct devlink *devlink, u32 param_id,
  3939. union devlink_param_value *init_val)
  3940. {
  3941. struct devlink_param_item *param_item;
  3942. if (!devlink->ops || !devlink->ops->reload)
  3943. return -EOPNOTSUPP;
  3944. param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
  3945. if (!param_item)
  3946. return -EINVAL;
  3947. if (!param_item->driverinit_value_valid ||
  3948. !devlink_param_cmode_is_supported(param_item->param,
  3949. DEVLINK_PARAM_CMODE_DRIVERINIT))
  3950. return -EOPNOTSUPP;
  3951. if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING)
  3952. strcpy(init_val->vstr, param_item->driverinit_value.vstr);
  3953. else
  3954. *init_val = param_item->driverinit_value;
  3955. return 0;
  3956. }
  3957. EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_get);
  3958. /**
  3959. * devlink_param_driverinit_value_set - set value of configuration
  3960. * parameter for driverinit
  3961. * configuration mode
  3962. *
  3963. * @devlink: devlink
  3964. * @param_id: parameter ID
  3965. * @init_val: value of parameter to set for driverinit configuration mode
  3966. *
  3967. * This function should be used by the driver to set driverinit
  3968. * configuration mode default value.
  3969. */
  3970. int devlink_param_driverinit_value_set(struct devlink *devlink, u32 param_id,
  3971. union devlink_param_value init_val)
  3972. {
  3973. struct devlink_param_item *param_item;
  3974. param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
  3975. if (!param_item)
  3976. return -EINVAL;
  3977. if (!devlink_param_cmode_is_supported(param_item->param,
  3978. DEVLINK_PARAM_CMODE_DRIVERINIT))
  3979. return -EOPNOTSUPP;
  3980. if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING)
  3981. strcpy(param_item->driverinit_value.vstr, init_val.vstr);
  3982. else
  3983. param_item->driverinit_value = init_val;
  3984. param_item->driverinit_value_valid = true;
  3985. devlink_param_notify(devlink, param_item, DEVLINK_CMD_PARAM_NEW);
  3986. return 0;
  3987. }
  3988. EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_set);
  3989. /**
  3990. * devlink_param_value_changed - notify devlink on a parameter's value
  3991. * change. Should be called by the driver
  3992. * right after the change.
  3993. *
  3994. * @devlink: devlink
  3995. * @param_id: parameter ID
  3996. *
  3997. * This function should be used by the driver to notify devlink on value
  3998. * change, excluding driverinit configuration mode.
  3999. * For driverinit configuration mode driver should use the function
  4000. * devlink_param_driverinit_value_set() instead.
  4001. */
  4002. void devlink_param_value_changed(struct devlink *devlink, u32 param_id)
  4003. {
  4004. struct devlink_param_item *param_item;
  4005. param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
  4006. WARN_ON(!param_item);
  4007. devlink_param_notify(devlink, param_item, DEVLINK_CMD_PARAM_NEW);
  4008. }
  4009. EXPORT_SYMBOL_GPL(devlink_param_value_changed);
  4010. /**
  4011. * devlink_param_value_str_fill - Safely fill-up the string preventing
  4012. * from overflow of the preallocated buffer
  4013. *
  4014. * @dst_val: destination devlink_param_value
  4015. * @src: source buffer
  4016. */
  4017. void devlink_param_value_str_fill(union devlink_param_value *dst_val,
  4018. const char *src)
  4019. {
  4020. size_t len;
  4021. len = strlcpy(dst_val->vstr, src, __DEVLINK_PARAM_MAX_STRING_VALUE);
  4022. WARN_ON(len >= __DEVLINK_PARAM_MAX_STRING_VALUE);
  4023. }
  4024. EXPORT_SYMBOL_GPL(devlink_param_value_str_fill);
  4025. /**
  4026. * devlink_region_create - create a new address region
  4027. *
  4028. * @devlink: devlink
  4029. * @region_name: region name
  4030. * @region_max_snapshots: Maximum supported number of snapshots for region
  4031. * @region_size: size of region
  4032. */
  4033. struct devlink_region *devlink_region_create(struct devlink *devlink,
  4034. const char *region_name,
  4035. u32 region_max_snapshots,
  4036. u64 region_size)
  4037. {
  4038. struct devlink_region *region;
  4039. int err = 0;
  4040. mutex_lock(&devlink->lock);
  4041. if (devlink_region_get_by_name(devlink, region_name)) {
  4042. err = -EEXIST;
  4043. goto unlock;
  4044. }
  4045. region = kzalloc(sizeof(*region), GFP_KERNEL);
  4046. if (!region) {
  4047. err = -ENOMEM;
  4048. goto unlock;
  4049. }
  4050. region->devlink = devlink;
  4051. region->max_snapshots = region_max_snapshots;
  4052. region->name = region_name;
  4053. region->size = region_size;
  4054. INIT_LIST_HEAD(&region->snapshot_list);
  4055. list_add_tail(&region->list, &devlink->region_list);
  4056. devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW);
  4057. mutex_unlock(&devlink->lock);
  4058. return region;
  4059. unlock:
  4060. mutex_unlock(&devlink->lock);
  4061. return ERR_PTR(err);
  4062. }
  4063. EXPORT_SYMBOL_GPL(devlink_region_create);
  4064. /**
  4065. * devlink_region_destroy - destroy address region
  4066. *
  4067. * @region: devlink region to destroy
  4068. */
  4069. void devlink_region_destroy(struct devlink_region *region)
  4070. {
  4071. struct devlink *devlink = region->devlink;
  4072. struct devlink_snapshot *snapshot, *ts;
  4073. mutex_lock(&devlink->lock);
  4074. /* Free all snapshots of region */
  4075. list_for_each_entry_safe(snapshot, ts, &region->snapshot_list, list)
  4076. devlink_region_snapshot_del(snapshot);
  4077. list_del(&region->list);
  4078. devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_DEL);
  4079. mutex_unlock(&devlink->lock);
  4080. kfree(region);
  4081. }
  4082. EXPORT_SYMBOL_GPL(devlink_region_destroy);
  4083. /**
  4084. * devlink_region_shapshot_id_get - get snapshot ID
  4085. *
  4086. * This callback should be called when adding a new snapshot,
  4087. * Driver should use the same id for multiple snapshots taken
  4088. * on multiple regions at the same time/by the same trigger.
  4089. *
  4090. * @devlink: devlink
  4091. */
  4092. u32 devlink_region_shapshot_id_get(struct devlink *devlink)
  4093. {
  4094. u32 id;
  4095. mutex_lock(&devlink->lock);
  4096. id = ++devlink->snapshot_id;
  4097. mutex_unlock(&devlink->lock);
  4098. return id;
  4099. }
  4100. EXPORT_SYMBOL_GPL(devlink_region_shapshot_id_get);
  4101. /**
  4102. * devlink_region_snapshot_create - create a new snapshot
  4103. * This will add a new snapshot of a region. The snapshot
  4104. * will be stored on the region struct and can be accessed
  4105. * from devlink. This is useful for future analyses of snapshots.
  4106. * Multiple snapshots can be created on a region.
  4107. * The @snapshot_id should be obtained using the getter function.
  4108. *
  4109. * @devlink_region: devlink region of the snapshot
  4110. * @data_len: size of snapshot data
  4111. * @data: snapshot data
  4112. * @snapshot_id: snapshot id to be created
  4113. * @data_destructor: pointer to destructor function to free data
  4114. */
  4115. int devlink_region_snapshot_create(struct devlink_region *region, u64 data_len,
  4116. u8 *data, u32 snapshot_id,
  4117. devlink_snapshot_data_dest_t *data_destructor)
  4118. {
  4119. struct devlink *devlink = region->devlink;
  4120. struct devlink_snapshot *snapshot;
  4121. int err;
  4122. mutex_lock(&devlink->lock);
  4123. /* check if region can hold one more snapshot */
  4124. if (region->cur_snapshots == region->max_snapshots) {
  4125. err = -ENOMEM;
  4126. goto unlock;
  4127. }
  4128. if (devlink_region_snapshot_get_by_id(region, snapshot_id)) {
  4129. err = -EEXIST;
  4130. goto unlock;
  4131. }
  4132. snapshot = kzalloc(sizeof(*snapshot), GFP_KERNEL);
  4133. if (!snapshot) {
  4134. err = -ENOMEM;
  4135. goto unlock;
  4136. }
  4137. snapshot->id = snapshot_id;
  4138. snapshot->region = region;
  4139. snapshot->data = data;
  4140. snapshot->data_len = data_len;
  4141. snapshot->data_destructor = data_destructor;
  4142. list_add_tail(&snapshot->list, &region->snapshot_list);
  4143. region->cur_snapshots++;
  4144. devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_NEW);
  4145. mutex_unlock(&devlink->lock);
  4146. return 0;
  4147. unlock:
  4148. mutex_unlock(&devlink->lock);
  4149. return err;
  4150. }
  4151. EXPORT_SYMBOL_GPL(devlink_region_snapshot_create);
  4152. static int __init devlink_module_init(void)
  4153. {
  4154. return genl_register_family(&devlink_nl_family);
  4155. }
  4156. static void __exit devlink_module_exit(void)
  4157. {
  4158. genl_unregister_family(&devlink_nl_family);
  4159. }
  4160. module_init(devlink_module_init);
  4161. module_exit(devlink_module_exit);
  4162. MODULE_LICENSE("GPL v2");
  4163. MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
  4164. MODULE_DESCRIPTION("Network physical device Netlink interface");
  4165. MODULE_ALIAS_GENL_FAMILY(DEVLINK_GENL_NAME);