devlink.c 101 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985
  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. #define DEVLINK_NL_FLAG_NEED_DEVLINK BIT(0)
  281. #define DEVLINK_NL_FLAG_NEED_PORT BIT(1)
  282. #define DEVLINK_NL_FLAG_NEED_SB BIT(2)
  283. /* The per devlink instance lock is taken by default in the pre-doit
  284. * operation, yet several commands do not require this. The global
  285. * devlink lock is taken and protects from disruption by user-calls.
  286. */
  287. #define DEVLINK_NL_FLAG_NO_LOCK BIT(3)
  288. static int devlink_nl_pre_doit(const struct genl_ops *ops,
  289. struct sk_buff *skb, struct genl_info *info)
  290. {
  291. struct devlink *devlink;
  292. int err;
  293. mutex_lock(&devlink_mutex);
  294. devlink = devlink_get_from_info(info);
  295. if (IS_ERR(devlink)) {
  296. mutex_unlock(&devlink_mutex);
  297. return PTR_ERR(devlink);
  298. }
  299. if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
  300. mutex_lock(&devlink->lock);
  301. if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_DEVLINK) {
  302. info->user_ptr[0] = devlink;
  303. } else if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_PORT) {
  304. struct devlink_port *devlink_port;
  305. devlink_port = devlink_port_get_from_info(devlink, info);
  306. if (IS_ERR(devlink_port)) {
  307. err = PTR_ERR(devlink_port);
  308. goto unlock;
  309. }
  310. info->user_ptr[0] = devlink_port;
  311. }
  312. if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_SB) {
  313. struct devlink_sb *devlink_sb;
  314. devlink_sb = devlink_sb_get_from_info(devlink, info);
  315. if (IS_ERR(devlink_sb)) {
  316. err = PTR_ERR(devlink_sb);
  317. goto unlock;
  318. }
  319. info->user_ptr[1] = devlink_sb;
  320. }
  321. return 0;
  322. unlock:
  323. if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
  324. mutex_unlock(&devlink->lock);
  325. mutex_unlock(&devlink_mutex);
  326. return err;
  327. }
  328. static void devlink_nl_post_doit(const struct genl_ops *ops,
  329. struct sk_buff *skb, struct genl_info *info)
  330. {
  331. struct devlink *devlink;
  332. devlink = devlink_get_from_info(info);
  333. if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
  334. mutex_unlock(&devlink->lock);
  335. mutex_unlock(&devlink_mutex);
  336. }
  337. static struct genl_family devlink_nl_family;
  338. enum devlink_multicast_groups {
  339. DEVLINK_MCGRP_CONFIG,
  340. };
  341. static const struct genl_multicast_group devlink_nl_mcgrps[] = {
  342. [DEVLINK_MCGRP_CONFIG] = { .name = DEVLINK_GENL_MCGRP_CONFIG_NAME },
  343. };
  344. static int devlink_nl_put_handle(struct sk_buff *msg, struct devlink *devlink)
  345. {
  346. if (nla_put_string(msg, DEVLINK_ATTR_BUS_NAME, devlink->dev->bus->name))
  347. return -EMSGSIZE;
  348. if (nla_put_string(msg, DEVLINK_ATTR_DEV_NAME, dev_name(devlink->dev)))
  349. return -EMSGSIZE;
  350. return 0;
  351. }
  352. static int devlink_nl_fill(struct sk_buff *msg, struct devlink *devlink,
  353. enum devlink_command cmd, u32 portid,
  354. u32 seq, int flags)
  355. {
  356. void *hdr;
  357. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  358. if (!hdr)
  359. return -EMSGSIZE;
  360. if (devlink_nl_put_handle(msg, devlink))
  361. goto nla_put_failure;
  362. genlmsg_end(msg, hdr);
  363. return 0;
  364. nla_put_failure:
  365. genlmsg_cancel(msg, hdr);
  366. return -EMSGSIZE;
  367. }
  368. static void devlink_notify(struct devlink *devlink, enum devlink_command cmd)
  369. {
  370. struct sk_buff *msg;
  371. int err;
  372. WARN_ON(cmd != DEVLINK_CMD_NEW && cmd != DEVLINK_CMD_DEL);
  373. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  374. if (!msg)
  375. return;
  376. err = devlink_nl_fill(msg, devlink, cmd, 0, 0, 0);
  377. if (err) {
  378. nlmsg_free(msg);
  379. return;
  380. }
  381. genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
  382. msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
  383. }
  384. static int devlink_nl_port_attrs_put(struct sk_buff *msg,
  385. struct devlink_port *devlink_port)
  386. {
  387. struct devlink_port_attrs *attrs = &devlink_port->attrs;
  388. if (!attrs->set)
  389. return 0;
  390. if (nla_put_u16(msg, DEVLINK_ATTR_PORT_FLAVOUR, attrs->flavour))
  391. return -EMSGSIZE;
  392. if (nla_put_u32(msg, DEVLINK_ATTR_PORT_NUMBER, attrs->port_number))
  393. return -EMSGSIZE;
  394. if (!attrs->split)
  395. return 0;
  396. if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_GROUP, attrs->port_number))
  397. return -EMSGSIZE;
  398. if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_SUBPORT_NUMBER,
  399. attrs->split_subport_number))
  400. return -EMSGSIZE;
  401. return 0;
  402. }
  403. static int devlink_nl_port_fill(struct sk_buff *msg, struct devlink *devlink,
  404. struct devlink_port *devlink_port,
  405. enum devlink_command cmd, u32 portid,
  406. u32 seq, int flags)
  407. {
  408. void *hdr;
  409. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  410. if (!hdr)
  411. return -EMSGSIZE;
  412. if (devlink_nl_put_handle(msg, devlink))
  413. goto nla_put_failure;
  414. if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
  415. goto nla_put_failure;
  416. if (nla_put_u16(msg, DEVLINK_ATTR_PORT_TYPE, devlink_port->type))
  417. goto nla_put_failure;
  418. if (devlink_port->desired_type != DEVLINK_PORT_TYPE_NOTSET &&
  419. nla_put_u16(msg, DEVLINK_ATTR_PORT_DESIRED_TYPE,
  420. devlink_port->desired_type))
  421. goto nla_put_failure;
  422. if (devlink_port->type == DEVLINK_PORT_TYPE_ETH) {
  423. struct net_device *netdev = devlink_port->type_dev;
  424. if (netdev &&
  425. (nla_put_u32(msg, DEVLINK_ATTR_PORT_NETDEV_IFINDEX,
  426. netdev->ifindex) ||
  427. nla_put_string(msg, DEVLINK_ATTR_PORT_NETDEV_NAME,
  428. netdev->name)))
  429. goto nla_put_failure;
  430. }
  431. if (devlink_port->type == DEVLINK_PORT_TYPE_IB) {
  432. struct ib_device *ibdev = devlink_port->type_dev;
  433. if (ibdev &&
  434. nla_put_string(msg, DEVLINK_ATTR_PORT_IBDEV_NAME,
  435. ibdev->name))
  436. goto nla_put_failure;
  437. }
  438. if (devlink_nl_port_attrs_put(msg, devlink_port))
  439. goto nla_put_failure;
  440. genlmsg_end(msg, hdr);
  441. return 0;
  442. nla_put_failure:
  443. genlmsg_cancel(msg, hdr);
  444. return -EMSGSIZE;
  445. }
  446. static void devlink_port_notify(struct devlink_port *devlink_port,
  447. enum devlink_command cmd)
  448. {
  449. struct devlink *devlink = devlink_port->devlink;
  450. struct sk_buff *msg;
  451. int err;
  452. if (!devlink_port->registered)
  453. return;
  454. WARN_ON(cmd != DEVLINK_CMD_PORT_NEW && cmd != DEVLINK_CMD_PORT_DEL);
  455. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  456. if (!msg)
  457. return;
  458. err = devlink_nl_port_fill(msg, devlink, devlink_port, cmd, 0, 0, 0);
  459. if (err) {
  460. nlmsg_free(msg);
  461. return;
  462. }
  463. genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
  464. msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
  465. }
  466. static int devlink_nl_cmd_get_doit(struct sk_buff *skb, struct genl_info *info)
  467. {
  468. struct devlink *devlink = info->user_ptr[0];
  469. struct sk_buff *msg;
  470. int err;
  471. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  472. if (!msg)
  473. return -ENOMEM;
  474. err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
  475. info->snd_portid, info->snd_seq, 0);
  476. if (err) {
  477. nlmsg_free(msg);
  478. return err;
  479. }
  480. return genlmsg_reply(msg, info);
  481. }
  482. static int devlink_nl_cmd_get_dumpit(struct sk_buff *msg,
  483. struct netlink_callback *cb)
  484. {
  485. struct devlink *devlink;
  486. int start = cb->args[0];
  487. int idx = 0;
  488. int err;
  489. mutex_lock(&devlink_mutex);
  490. list_for_each_entry(devlink, &devlink_list, list) {
  491. if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
  492. continue;
  493. if (idx < start) {
  494. idx++;
  495. continue;
  496. }
  497. err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
  498. NETLINK_CB(cb->skb).portid,
  499. cb->nlh->nlmsg_seq, NLM_F_MULTI);
  500. if (err)
  501. goto out;
  502. idx++;
  503. }
  504. out:
  505. mutex_unlock(&devlink_mutex);
  506. cb->args[0] = idx;
  507. return msg->len;
  508. }
  509. static int devlink_nl_cmd_port_get_doit(struct sk_buff *skb,
  510. struct genl_info *info)
  511. {
  512. struct devlink_port *devlink_port = info->user_ptr[0];
  513. struct devlink *devlink = devlink_port->devlink;
  514. struct sk_buff *msg;
  515. int err;
  516. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  517. if (!msg)
  518. return -ENOMEM;
  519. err = devlink_nl_port_fill(msg, devlink, devlink_port,
  520. DEVLINK_CMD_PORT_NEW,
  521. info->snd_portid, info->snd_seq, 0);
  522. if (err) {
  523. nlmsg_free(msg);
  524. return err;
  525. }
  526. return genlmsg_reply(msg, info);
  527. }
  528. static int devlink_nl_cmd_port_get_dumpit(struct sk_buff *msg,
  529. struct netlink_callback *cb)
  530. {
  531. struct devlink *devlink;
  532. struct devlink_port *devlink_port;
  533. int start = cb->args[0];
  534. int idx = 0;
  535. int err;
  536. mutex_lock(&devlink_mutex);
  537. list_for_each_entry(devlink, &devlink_list, list) {
  538. if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
  539. continue;
  540. mutex_lock(&devlink->lock);
  541. list_for_each_entry(devlink_port, &devlink->port_list, list) {
  542. if (idx < start) {
  543. idx++;
  544. continue;
  545. }
  546. err = devlink_nl_port_fill(msg, devlink, devlink_port,
  547. DEVLINK_CMD_NEW,
  548. NETLINK_CB(cb->skb).portid,
  549. cb->nlh->nlmsg_seq,
  550. NLM_F_MULTI);
  551. if (err) {
  552. mutex_unlock(&devlink->lock);
  553. goto out;
  554. }
  555. idx++;
  556. }
  557. mutex_unlock(&devlink->lock);
  558. }
  559. out:
  560. mutex_unlock(&devlink_mutex);
  561. cb->args[0] = idx;
  562. return msg->len;
  563. }
  564. static int devlink_port_type_set(struct devlink *devlink,
  565. struct devlink_port *devlink_port,
  566. enum devlink_port_type port_type)
  567. {
  568. int err;
  569. if (devlink->ops && devlink->ops->port_type_set) {
  570. if (port_type == DEVLINK_PORT_TYPE_NOTSET)
  571. return -EINVAL;
  572. if (port_type == devlink_port->type)
  573. return 0;
  574. err = devlink->ops->port_type_set(devlink_port, port_type);
  575. if (err)
  576. return err;
  577. devlink_port->desired_type = port_type;
  578. devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
  579. return 0;
  580. }
  581. return -EOPNOTSUPP;
  582. }
  583. static int devlink_nl_cmd_port_set_doit(struct sk_buff *skb,
  584. struct genl_info *info)
  585. {
  586. struct devlink_port *devlink_port = info->user_ptr[0];
  587. struct devlink *devlink = devlink_port->devlink;
  588. int err;
  589. if (info->attrs[DEVLINK_ATTR_PORT_TYPE]) {
  590. enum devlink_port_type port_type;
  591. port_type = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_TYPE]);
  592. err = devlink_port_type_set(devlink, devlink_port, port_type);
  593. if (err)
  594. return err;
  595. }
  596. return 0;
  597. }
  598. static int devlink_port_split(struct devlink *devlink, u32 port_index,
  599. u32 count, struct netlink_ext_ack *extack)
  600. {
  601. if (devlink->ops && devlink->ops->port_split)
  602. return devlink->ops->port_split(devlink, port_index, count,
  603. extack);
  604. return -EOPNOTSUPP;
  605. }
  606. static int devlink_nl_cmd_port_split_doit(struct sk_buff *skb,
  607. struct genl_info *info)
  608. {
  609. struct devlink *devlink = info->user_ptr[0];
  610. u32 port_index;
  611. u32 count;
  612. if (!info->attrs[DEVLINK_ATTR_PORT_INDEX] ||
  613. !info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT])
  614. return -EINVAL;
  615. port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
  616. count = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT]);
  617. return devlink_port_split(devlink, port_index, count, info->extack);
  618. }
  619. static int devlink_port_unsplit(struct devlink *devlink, u32 port_index,
  620. struct netlink_ext_ack *extack)
  621. {
  622. if (devlink->ops && devlink->ops->port_unsplit)
  623. return devlink->ops->port_unsplit(devlink, port_index, extack);
  624. return -EOPNOTSUPP;
  625. }
  626. static int devlink_nl_cmd_port_unsplit_doit(struct sk_buff *skb,
  627. struct genl_info *info)
  628. {
  629. struct devlink *devlink = info->user_ptr[0];
  630. u32 port_index;
  631. if (!info->attrs[DEVLINK_ATTR_PORT_INDEX])
  632. return -EINVAL;
  633. port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
  634. return devlink_port_unsplit(devlink, port_index, info->extack);
  635. }
  636. static int devlink_nl_sb_fill(struct sk_buff *msg, struct devlink *devlink,
  637. struct devlink_sb *devlink_sb,
  638. enum devlink_command cmd, u32 portid,
  639. u32 seq, int flags)
  640. {
  641. void *hdr;
  642. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  643. if (!hdr)
  644. return -EMSGSIZE;
  645. if (devlink_nl_put_handle(msg, devlink))
  646. goto nla_put_failure;
  647. if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
  648. goto nla_put_failure;
  649. if (nla_put_u32(msg, DEVLINK_ATTR_SB_SIZE, devlink_sb->size))
  650. goto nla_put_failure;
  651. if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_POOL_COUNT,
  652. devlink_sb->ingress_pools_count))
  653. goto nla_put_failure;
  654. if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_POOL_COUNT,
  655. devlink_sb->egress_pools_count))
  656. goto nla_put_failure;
  657. if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_TC_COUNT,
  658. devlink_sb->ingress_tc_count))
  659. goto nla_put_failure;
  660. if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_TC_COUNT,
  661. devlink_sb->egress_tc_count))
  662. goto nla_put_failure;
  663. genlmsg_end(msg, hdr);
  664. return 0;
  665. nla_put_failure:
  666. genlmsg_cancel(msg, hdr);
  667. return -EMSGSIZE;
  668. }
  669. static int devlink_nl_cmd_sb_get_doit(struct sk_buff *skb,
  670. struct genl_info *info)
  671. {
  672. struct devlink *devlink = info->user_ptr[0];
  673. struct devlink_sb *devlink_sb = info->user_ptr[1];
  674. struct sk_buff *msg;
  675. int err;
  676. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  677. if (!msg)
  678. return -ENOMEM;
  679. err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
  680. DEVLINK_CMD_SB_NEW,
  681. info->snd_portid, info->snd_seq, 0);
  682. if (err) {
  683. nlmsg_free(msg);
  684. return err;
  685. }
  686. return genlmsg_reply(msg, info);
  687. }
  688. static int devlink_nl_cmd_sb_get_dumpit(struct sk_buff *msg,
  689. struct netlink_callback *cb)
  690. {
  691. struct devlink *devlink;
  692. struct devlink_sb *devlink_sb;
  693. int start = cb->args[0];
  694. int idx = 0;
  695. int err;
  696. mutex_lock(&devlink_mutex);
  697. list_for_each_entry(devlink, &devlink_list, list) {
  698. if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
  699. continue;
  700. mutex_lock(&devlink->lock);
  701. list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
  702. if (idx < start) {
  703. idx++;
  704. continue;
  705. }
  706. err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
  707. DEVLINK_CMD_SB_NEW,
  708. NETLINK_CB(cb->skb).portid,
  709. cb->nlh->nlmsg_seq,
  710. NLM_F_MULTI);
  711. if (err) {
  712. mutex_unlock(&devlink->lock);
  713. goto out;
  714. }
  715. idx++;
  716. }
  717. mutex_unlock(&devlink->lock);
  718. }
  719. out:
  720. mutex_unlock(&devlink_mutex);
  721. cb->args[0] = idx;
  722. return msg->len;
  723. }
  724. static int devlink_nl_sb_pool_fill(struct sk_buff *msg, struct devlink *devlink,
  725. struct devlink_sb *devlink_sb,
  726. u16 pool_index, enum devlink_command cmd,
  727. u32 portid, u32 seq, int flags)
  728. {
  729. struct devlink_sb_pool_info pool_info;
  730. void *hdr;
  731. int err;
  732. err = devlink->ops->sb_pool_get(devlink, devlink_sb->index,
  733. pool_index, &pool_info);
  734. if (err)
  735. return err;
  736. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  737. if (!hdr)
  738. return -EMSGSIZE;
  739. if (devlink_nl_put_handle(msg, devlink))
  740. goto nla_put_failure;
  741. if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
  742. goto nla_put_failure;
  743. if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
  744. goto nla_put_failure;
  745. if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_info.pool_type))
  746. goto nla_put_failure;
  747. if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_SIZE, pool_info.size))
  748. goto nla_put_failure;
  749. if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE,
  750. pool_info.threshold_type))
  751. goto nla_put_failure;
  752. genlmsg_end(msg, hdr);
  753. return 0;
  754. nla_put_failure:
  755. genlmsg_cancel(msg, hdr);
  756. return -EMSGSIZE;
  757. }
  758. static int devlink_nl_cmd_sb_pool_get_doit(struct sk_buff *skb,
  759. struct genl_info *info)
  760. {
  761. struct devlink *devlink = info->user_ptr[0];
  762. struct devlink_sb *devlink_sb = info->user_ptr[1];
  763. struct sk_buff *msg;
  764. u16 pool_index;
  765. int err;
  766. err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
  767. &pool_index);
  768. if (err)
  769. return err;
  770. if (!devlink->ops || !devlink->ops->sb_pool_get)
  771. return -EOPNOTSUPP;
  772. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  773. if (!msg)
  774. return -ENOMEM;
  775. err = devlink_nl_sb_pool_fill(msg, devlink, devlink_sb, pool_index,
  776. DEVLINK_CMD_SB_POOL_NEW,
  777. info->snd_portid, info->snd_seq, 0);
  778. if (err) {
  779. nlmsg_free(msg);
  780. return err;
  781. }
  782. return genlmsg_reply(msg, info);
  783. }
  784. static int __sb_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
  785. struct devlink *devlink,
  786. struct devlink_sb *devlink_sb,
  787. u32 portid, u32 seq)
  788. {
  789. u16 pool_count = devlink_sb_pool_count(devlink_sb);
  790. u16 pool_index;
  791. int err;
  792. for (pool_index = 0; pool_index < pool_count; pool_index++) {
  793. if (*p_idx < start) {
  794. (*p_idx)++;
  795. continue;
  796. }
  797. err = devlink_nl_sb_pool_fill(msg, devlink,
  798. devlink_sb,
  799. pool_index,
  800. DEVLINK_CMD_SB_POOL_NEW,
  801. portid, seq, NLM_F_MULTI);
  802. if (err)
  803. return err;
  804. (*p_idx)++;
  805. }
  806. return 0;
  807. }
  808. static int devlink_nl_cmd_sb_pool_get_dumpit(struct sk_buff *msg,
  809. struct netlink_callback *cb)
  810. {
  811. struct devlink *devlink;
  812. struct devlink_sb *devlink_sb;
  813. int start = cb->args[0];
  814. int idx = 0;
  815. int err;
  816. mutex_lock(&devlink_mutex);
  817. list_for_each_entry(devlink, &devlink_list, list) {
  818. if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
  819. !devlink->ops || !devlink->ops->sb_pool_get)
  820. continue;
  821. mutex_lock(&devlink->lock);
  822. list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
  823. err = __sb_pool_get_dumpit(msg, start, &idx, devlink,
  824. devlink_sb,
  825. NETLINK_CB(cb->skb).portid,
  826. cb->nlh->nlmsg_seq);
  827. if (err && err != -EOPNOTSUPP) {
  828. mutex_unlock(&devlink->lock);
  829. goto out;
  830. }
  831. }
  832. mutex_unlock(&devlink->lock);
  833. }
  834. out:
  835. mutex_unlock(&devlink_mutex);
  836. cb->args[0] = idx;
  837. return msg->len;
  838. }
  839. static int devlink_sb_pool_set(struct devlink *devlink, unsigned int sb_index,
  840. u16 pool_index, u32 size,
  841. enum devlink_sb_threshold_type threshold_type)
  842. {
  843. const struct devlink_ops *ops = devlink->ops;
  844. if (ops && ops->sb_pool_set)
  845. return ops->sb_pool_set(devlink, sb_index, pool_index,
  846. size, threshold_type);
  847. return -EOPNOTSUPP;
  848. }
  849. static int devlink_nl_cmd_sb_pool_set_doit(struct sk_buff *skb,
  850. struct genl_info *info)
  851. {
  852. struct devlink *devlink = info->user_ptr[0];
  853. struct devlink_sb *devlink_sb = info->user_ptr[1];
  854. enum devlink_sb_threshold_type threshold_type;
  855. u16 pool_index;
  856. u32 size;
  857. int err;
  858. err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
  859. &pool_index);
  860. if (err)
  861. return err;
  862. err = devlink_sb_th_type_get_from_info(info, &threshold_type);
  863. if (err)
  864. return err;
  865. if (!info->attrs[DEVLINK_ATTR_SB_POOL_SIZE])
  866. return -EINVAL;
  867. size = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_POOL_SIZE]);
  868. return devlink_sb_pool_set(devlink, devlink_sb->index,
  869. pool_index, size, threshold_type);
  870. }
  871. static int devlink_nl_sb_port_pool_fill(struct sk_buff *msg,
  872. struct devlink *devlink,
  873. struct devlink_port *devlink_port,
  874. struct devlink_sb *devlink_sb,
  875. u16 pool_index,
  876. enum devlink_command cmd,
  877. u32 portid, u32 seq, int flags)
  878. {
  879. const struct devlink_ops *ops = devlink->ops;
  880. u32 threshold;
  881. void *hdr;
  882. int err;
  883. err = ops->sb_port_pool_get(devlink_port, devlink_sb->index,
  884. pool_index, &threshold);
  885. if (err)
  886. return err;
  887. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  888. if (!hdr)
  889. return -EMSGSIZE;
  890. if (devlink_nl_put_handle(msg, devlink))
  891. goto nla_put_failure;
  892. if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
  893. goto nla_put_failure;
  894. if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
  895. goto nla_put_failure;
  896. if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
  897. goto nla_put_failure;
  898. if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
  899. goto nla_put_failure;
  900. if (ops->sb_occ_port_pool_get) {
  901. u32 cur;
  902. u32 max;
  903. err = ops->sb_occ_port_pool_get(devlink_port, devlink_sb->index,
  904. pool_index, &cur, &max);
  905. if (err && err != -EOPNOTSUPP)
  906. return err;
  907. if (!err) {
  908. if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
  909. goto nla_put_failure;
  910. if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
  911. goto nla_put_failure;
  912. }
  913. }
  914. genlmsg_end(msg, hdr);
  915. return 0;
  916. nla_put_failure:
  917. genlmsg_cancel(msg, hdr);
  918. return -EMSGSIZE;
  919. }
  920. static int devlink_nl_cmd_sb_port_pool_get_doit(struct sk_buff *skb,
  921. struct genl_info *info)
  922. {
  923. struct devlink_port *devlink_port = info->user_ptr[0];
  924. struct devlink *devlink = devlink_port->devlink;
  925. struct devlink_sb *devlink_sb = info->user_ptr[1];
  926. struct sk_buff *msg;
  927. u16 pool_index;
  928. int err;
  929. err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
  930. &pool_index);
  931. if (err)
  932. return err;
  933. if (!devlink->ops || !devlink->ops->sb_port_pool_get)
  934. return -EOPNOTSUPP;
  935. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  936. if (!msg)
  937. return -ENOMEM;
  938. err = devlink_nl_sb_port_pool_fill(msg, devlink, devlink_port,
  939. devlink_sb, pool_index,
  940. DEVLINK_CMD_SB_PORT_POOL_NEW,
  941. info->snd_portid, info->snd_seq, 0);
  942. if (err) {
  943. nlmsg_free(msg);
  944. return err;
  945. }
  946. return genlmsg_reply(msg, info);
  947. }
  948. static int __sb_port_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
  949. struct devlink *devlink,
  950. struct devlink_sb *devlink_sb,
  951. u32 portid, u32 seq)
  952. {
  953. struct devlink_port *devlink_port;
  954. u16 pool_count = devlink_sb_pool_count(devlink_sb);
  955. u16 pool_index;
  956. int err;
  957. list_for_each_entry(devlink_port, &devlink->port_list, list) {
  958. for (pool_index = 0; pool_index < pool_count; pool_index++) {
  959. if (*p_idx < start) {
  960. (*p_idx)++;
  961. continue;
  962. }
  963. err = devlink_nl_sb_port_pool_fill(msg, devlink,
  964. devlink_port,
  965. devlink_sb,
  966. pool_index,
  967. DEVLINK_CMD_SB_PORT_POOL_NEW,
  968. portid, seq,
  969. NLM_F_MULTI);
  970. if (err)
  971. return err;
  972. (*p_idx)++;
  973. }
  974. }
  975. return 0;
  976. }
  977. static int devlink_nl_cmd_sb_port_pool_get_dumpit(struct sk_buff *msg,
  978. struct netlink_callback *cb)
  979. {
  980. struct devlink *devlink;
  981. struct devlink_sb *devlink_sb;
  982. int start = cb->args[0];
  983. int idx = 0;
  984. int err;
  985. mutex_lock(&devlink_mutex);
  986. list_for_each_entry(devlink, &devlink_list, list) {
  987. if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
  988. !devlink->ops || !devlink->ops->sb_port_pool_get)
  989. continue;
  990. mutex_lock(&devlink->lock);
  991. list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
  992. err = __sb_port_pool_get_dumpit(msg, start, &idx,
  993. devlink, devlink_sb,
  994. NETLINK_CB(cb->skb).portid,
  995. cb->nlh->nlmsg_seq);
  996. if (err && err != -EOPNOTSUPP) {
  997. mutex_unlock(&devlink->lock);
  998. goto out;
  999. }
  1000. }
  1001. mutex_unlock(&devlink->lock);
  1002. }
  1003. out:
  1004. mutex_unlock(&devlink_mutex);
  1005. cb->args[0] = idx;
  1006. return msg->len;
  1007. }
  1008. static int devlink_sb_port_pool_set(struct devlink_port *devlink_port,
  1009. unsigned int sb_index, u16 pool_index,
  1010. u32 threshold)
  1011. {
  1012. const struct devlink_ops *ops = devlink_port->devlink->ops;
  1013. if (ops && ops->sb_port_pool_set)
  1014. return ops->sb_port_pool_set(devlink_port, sb_index,
  1015. pool_index, threshold);
  1016. return -EOPNOTSUPP;
  1017. }
  1018. static int devlink_nl_cmd_sb_port_pool_set_doit(struct sk_buff *skb,
  1019. struct genl_info *info)
  1020. {
  1021. struct devlink_port *devlink_port = info->user_ptr[0];
  1022. struct devlink_sb *devlink_sb = info->user_ptr[1];
  1023. u16 pool_index;
  1024. u32 threshold;
  1025. int err;
  1026. err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
  1027. &pool_index);
  1028. if (err)
  1029. return err;
  1030. if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
  1031. return -EINVAL;
  1032. threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
  1033. return devlink_sb_port_pool_set(devlink_port, devlink_sb->index,
  1034. pool_index, threshold);
  1035. }
  1036. static int
  1037. devlink_nl_sb_tc_pool_bind_fill(struct sk_buff *msg, struct devlink *devlink,
  1038. struct devlink_port *devlink_port,
  1039. struct devlink_sb *devlink_sb, u16 tc_index,
  1040. enum devlink_sb_pool_type pool_type,
  1041. enum devlink_command cmd,
  1042. u32 portid, u32 seq, int flags)
  1043. {
  1044. const struct devlink_ops *ops = devlink->ops;
  1045. u16 pool_index;
  1046. u32 threshold;
  1047. void *hdr;
  1048. int err;
  1049. err = ops->sb_tc_pool_bind_get(devlink_port, devlink_sb->index,
  1050. tc_index, pool_type,
  1051. &pool_index, &threshold);
  1052. if (err)
  1053. return err;
  1054. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  1055. if (!hdr)
  1056. return -EMSGSIZE;
  1057. if (devlink_nl_put_handle(msg, devlink))
  1058. goto nla_put_failure;
  1059. if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
  1060. goto nla_put_failure;
  1061. if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
  1062. goto nla_put_failure;
  1063. if (nla_put_u16(msg, DEVLINK_ATTR_SB_TC_INDEX, tc_index))
  1064. goto nla_put_failure;
  1065. if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_type))
  1066. goto nla_put_failure;
  1067. if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
  1068. goto nla_put_failure;
  1069. if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
  1070. goto nla_put_failure;
  1071. if (ops->sb_occ_tc_port_bind_get) {
  1072. u32 cur;
  1073. u32 max;
  1074. err = ops->sb_occ_tc_port_bind_get(devlink_port,
  1075. devlink_sb->index,
  1076. tc_index, pool_type,
  1077. &cur, &max);
  1078. if (err && err != -EOPNOTSUPP)
  1079. return err;
  1080. if (!err) {
  1081. if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
  1082. goto nla_put_failure;
  1083. if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
  1084. goto nla_put_failure;
  1085. }
  1086. }
  1087. genlmsg_end(msg, hdr);
  1088. return 0;
  1089. nla_put_failure:
  1090. genlmsg_cancel(msg, hdr);
  1091. return -EMSGSIZE;
  1092. }
  1093. static int devlink_nl_cmd_sb_tc_pool_bind_get_doit(struct sk_buff *skb,
  1094. struct genl_info *info)
  1095. {
  1096. struct devlink_port *devlink_port = info->user_ptr[0];
  1097. struct devlink *devlink = devlink_port->devlink;
  1098. struct devlink_sb *devlink_sb = info->user_ptr[1];
  1099. struct sk_buff *msg;
  1100. enum devlink_sb_pool_type pool_type;
  1101. u16 tc_index;
  1102. int err;
  1103. err = devlink_sb_pool_type_get_from_info(info, &pool_type);
  1104. if (err)
  1105. return err;
  1106. err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
  1107. pool_type, &tc_index);
  1108. if (err)
  1109. return err;
  1110. if (!devlink->ops || !devlink->ops->sb_tc_pool_bind_get)
  1111. return -EOPNOTSUPP;
  1112. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1113. if (!msg)
  1114. return -ENOMEM;
  1115. err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink, devlink_port,
  1116. devlink_sb, tc_index, pool_type,
  1117. DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
  1118. info->snd_portid,
  1119. info->snd_seq, 0);
  1120. if (err) {
  1121. nlmsg_free(msg);
  1122. return err;
  1123. }
  1124. return genlmsg_reply(msg, info);
  1125. }
  1126. static int __sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
  1127. int start, int *p_idx,
  1128. struct devlink *devlink,
  1129. struct devlink_sb *devlink_sb,
  1130. u32 portid, u32 seq)
  1131. {
  1132. struct devlink_port *devlink_port;
  1133. u16 tc_index;
  1134. int err;
  1135. list_for_each_entry(devlink_port, &devlink->port_list, list) {
  1136. for (tc_index = 0;
  1137. tc_index < devlink_sb->ingress_tc_count; tc_index++) {
  1138. if (*p_idx < start) {
  1139. (*p_idx)++;
  1140. continue;
  1141. }
  1142. err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
  1143. devlink_port,
  1144. devlink_sb,
  1145. tc_index,
  1146. DEVLINK_SB_POOL_TYPE_INGRESS,
  1147. DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
  1148. portid, seq,
  1149. NLM_F_MULTI);
  1150. if (err)
  1151. return err;
  1152. (*p_idx)++;
  1153. }
  1154. for (tc_index = 0;
  1155. tc_index < devlink_sb->egress_tc_count; tc_index++) {
  1156. if (*p_idx < start) {
  1157. (*p_idx)++;
  1158. continue;
  1159. }
  1160. err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
  1161. devlink_port,
  1162. devlink_sb,
  1163. tc_index,
  1164. DEVLINK_SB_POOL_TYPE_EGRESS,
  1165. DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
  1166. portid, seq,
  1167. NLM_F_MULTI);
  1168. if (err)
  1169. return err;
  1170. (*p_idx)++;
  1171. }
  1172. }
  1173. return 0;
  1174. }
  1175. static int
  1176. devlink_nl_cmd_sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
  1177. struct netlink_callback *cb)
  1178. {
  1179. struct devlink *devlink;
  1180. struct devlink_sb *devlink_sb;
  1181. int start = cb->args[0];
  1182. int idx = 0;
  1183. int err;
  1184. mutex_lock(&devlink_mutex);
  1185. list_for_each_entry(devlink, &devlink_list, list) {
  1186. if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
  1187. !devlink->ops || !devlink->ops->sb_tc_pool_bind_get)
  1188. continue;
  1189. mutex_lock(&devlink->lock);
  1190. list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
  1191. err = __sb_tc_pool_bind_get_dumpit(msg, start, &idx,
  1192. devlink,
  1193. devlink_sb,
  1194. NETLINK_CB(cb->skb).portid,
  1195. cb->nlh->nlmsg_seq);
  1196. if (err && err != -EOPNOTSUPP) {
  1197. mutex_unlock(&devlink->lock);
  1198. goto out;
  1199. }
  1200. }
  1201. mutex_unlock(&devlink->lock);
  1202. }
  1203. out:
  1204. mutex_unlock(&devlink_mutex);
  1205. cb->args[0] = idx;
  1206. return msg->len;
  1207. }
  1208. static int devlink_sb_tc_pool_bind_set(struct devlink_port *devlink_port,
  1209. unsigned int sb_index, u16 tc_index,
  1210. enum devlink_sb_pool_type pool_type,
  1211. u16 pool_index, u32 threshold)
  1212. {
  1213. const struct devlink_ops *ops = devlink_port->devlink->ops;
  1214. if (ops && ops->sb_tc_pool_bind_set)
  1215. return ops->sb_tc_pool_bind_set(devlink_port, sb_index,
  1216. tc_index, pool_type,
  1217. pool_index, threshold);
  1218. return -EOPNOTSUPP;
  1219. }
  1220. static int devlink_nl_cmd_sb_tc_pool_bind_set_doit(struct sk_buff *skb,
  1221. struct genl_info *info)
  1222. {
  1223. struct devlink_port *devlink_port = info->user_ptr[0];
  1224. struct devlink_sb *devlink_sb = info->user_ptr[1];
  1225. enum devlink_sb_pool_type pool_type;
  1226. u16 tc_index;
  1227. u16 pool_index;
  1228. u32 threshold;
  1229. int err;
  1230. err = devlink_sb_pool_type_get_from_info(info, &pool_type);
  1231. if (err)
  1232. return err;
  1233. err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
  1234. pool_type, &tc_index);
  1235. if (err)
  1236. return err;
  1237. err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
  1238. &pool_index);
  1239. if (err)
  1240. return err;
  1241. if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
  1242. return -EINVAL;
  1243. threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
  1244. return devlink_sb_tc_pool_bind_set(devlink_port, devlink_sb->index,
  1245. tc_index, pool_type,
  1246. pool_index, threshold);
  1247. }
  1248. static int devlink_nl_cmd_sb_occ_snapshot_doit(struct sk_buff *skb,
  1249. struct genl_info *info)
  1250. {
  1251. struct devlink *devlink = info->user_ptr[0];
  1252. struct devlink_sb *devlink_sb = info->user_ptr[1];
  1253. const struct devlink_ops *ops = devlink->ops;
  1254. if (ops && ops->sb_occ_snapshot)
  1255. return ops->sb_occ_snapshot(devlink, devlink_sb->index);
  1256. return -EOPNOTSUPP;
  1257. }
  1258. static int devlink_nl_cmd_sb_occ_max_clear_doit(struct sk_buff *skb,
  1259. struct genl_info *info)
  1260. {
  1261. struct devlink *devlink = info->user_ptr[0];
  1262. struct devlink_sb *devlink_sb = info->user_ptr[1];
  1263. const struct devlink_ops *ops = devlink->ops;
  1264. if (ops && ops->sb_occ_max_clear)
  1265. return ops->sb_occ_max_clear(devlink, devlink_sb->index);
  1266. return -EOPNOTSUPP;
  1267. }
  1268. static int devlink_nl_eswitch_fill(struct sk_buff *msg, struct devlink *devlink,
  1269. enum devlink_command cmd, u32 portid,
  1270. u32 seq, int flags)
  1271. {
  1272. const struct devlink_ops *ops = devlink->ops;
  1273. u8 inline_mode, encap_mode;
  1274. void *hdr;
  1275. int err = 0;
  1276. u16 mode;
  1277. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  1278. if (!hdr)
  1279. return -EMSGSIZE;
  1280. err = devlink_nl_put_handle(msg, devlink);
  1281. if (err)
  1282. goto nla_put_failure;
  1283. if (ops->eswitch_mode_get) {
  1284. err = ops->eswitch_mode_get(devlink, &mode);
  1285. if (err)
  1286. goto nla_put_failure;
  1287. err = nla_put_u16(msg, DEVLINK_ATTR_ESWITCH_MODE, mode);
  1288. if (err)
  1289. goto nla_put_failure;
  1290. }
  1291. if (ops->eswitch_inline_mode_get) {
  1292. err = ops->eswitch_inline_mode_get(devlink, &inline_mode);
  1293. if (err)
  1294. goto nla_put_failure;
  1295. err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_INLINE_MODE,
  1296. inline_mode);
  1297. if (err)
  1298. goto nla_put_failure;
  1299. }
  1300. if (ops->eswitch_encap_mode_get) {
  1301. err = ops->eswitch_encap_mode_get(devlink, &encap_mode);
  1302. if (err)
  1303. goto nla_put_failure;
  1304. err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_ENCAP_MODE, encap_mode);
  1305. if (err)
  1306. goto nla_put_failure;
  1307. }
  1308. genlmsg_end(msg, hdr);
  1309. return 0;
  1310. nla_put_failure:
  1311. genlmsg_cancel(msg, hdr);
  1312. return err;
  1313. }
  1314. static int devlink_nl_cmd_eswitch_get_doit(struct sk_buff *skb,
  1315. struct genl_info *info)
  1316. {
  1317. struct devlink *devlink = info->user_ptr[0];
  1318. const struct devlink_ops *ops = devlink->ops;
  1319. struct sk_buff *msg;
  1320. int err;
  1321. if (!ops)
  1322. return -EOPNOTSUPP;
  1323. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1324. if (!msg)
  1325. return -ENOMEM;
  1326. err = devlink_nl_eswitch_fill(msg, devlink, DEVLINK_CMD_ESWITCH_GET,
  1327. info->snd_portid, info->snd_seq, 0);
  1328. if (err) {
  1329. nlmsg_free(msg);
  1330. return err;
  1331. }
  1332. return genlmsg_reply(msg, info);
  1333. }
  1334. static int devlink_nl_cmd_eswitch_set_doit(struct sk_buff *skb,
  1335. struct genl_info *info)
  1336. {
  1337. struct devlink *devlink = info->user_ptr[0];
  1338. const struct devlink_ops *ops = devlink->ops;
  1339. u8 inline_mode, encap_mode;
  1340. int err = 0;
  1341. u16 mode;
  1342. if (!ops)
  1343. return -EOPNOTSUPP;
  1344. if (info->attrs[DEVLINK_ATTR_ESWITCH_MODE]) {
  1345. if (!ops->eswitch_mode_set)
  1346. return -EOPNOTSUPP;
  1347. mode = nla_get_u16(info->attrs[DEVLINK_ATTR_ESWITCH_MODE]);
  1348. err = ops->eswitch_mode_set(devlink, mode);
  1349. if (err)
  1350. return err;
  1351. }
  1352. if (info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]) {
  1353. if (!ops->eswitch_inline_mode_set)
  1354. return -EOPNOTSUPP;
  1355. inline_mode = nla_get_u8(
  1356. info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]);
  1357. err = ops->eswitch_inline_mode_set(devlink, inline_mode);
  1358. if (err)
  1359. return err;
  1360. }
  1361. if (info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]) {
  1362. if (!ops->eswitch_encap_mode_set)
  1363. return -EOPNOTSUPP;
  1364. encap_mode = nla_get_u8(info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]);
  1365. err = ops->eswitch_encap_mode_set(devlink, encap_mode);
  1366. if (err)
  1367. return err;
  1368. }
  1369. return 0;
  1370. }
  1371. int devlink_dpipe_match_put(struct sk_buff *skb,
  1372. struct devlink_dpipe_match *match)
  1373. {
  1374. struct devlink_dpipe_header *header = match->header;
  1375. struct devlink_dpipe_field *field = &header->fields[match->field_id];
  1376. struct nlattr *match_attr;
  1377. match_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_MATCH);
  1378. if (!match_attr)
  1379. return -EMSGSIZE;
  1380. if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_MATCH_TYPE, match->type) ||
  1381. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, match->header_index) ||
  1382. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
  1383. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
  1384. nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
  1385. goto nla_put_failure;
  1386. nla_nest_end(skb, match_attr);
  1387. return 0;
  1388. nla_put_failure:
  1389. nla_nest_cancel(skb, match_attr);
  1390. return -EMSGSIZE;
  1391. }
  1392. EXPORT_SYMBOL_GPL(devlink_dpipe_match_put);
  1393. static int devlink_dpipe_matches_put(struct devlink_dpipe_table *table,
  1394. struct sk_buff *skb)
  1395. {
  1396. struct nlattr *matches_attr;
  1397. matches_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLE_MATCHES);
  1398. if (!matches_attr)
  1399. return -EMSGSIZE;
  1400. if (table->table_ops->matches_dump(table->priv, skb))
  1401. goto nla_put_failure;
  1402. nla_nest_end(skb, matches_attr);
  1403. return 0;
  1404. nla_put_failure:
  1405. nla_nest_cancel(skb, matches_attr);
  1406. return -EMSGSIZE;
  1407. }
  1408. int devlink_dpipe_action_put(struct sk_buff *skb,
  1409. struct devlink_dpipe_action *action)
  1410. {
  1411. struct devlink_dpipe_header *header = action->header;
  1412. struct devlink_dpipe_field *field = &header->fields[action->field_id];
  1413. struct nlattr *action_attr;
  1414. action_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_ACTION);
  1415. if (!action_attr)
  1416. return -EMSGSIZE;
  1417. if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_ACTION_TYPE, action->type) ||
  1418. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, action->header_index) ||
  1419. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
  1420. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
  1421. nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
  1422. goto nla_put_failure;
  1423. nla_nest_end(skb, action_attr);
  1424. return 0;
  1425. nla_put_failure:
  1426. nla_nest_cancel(skb, action_attr);
  1427. return -EMSGSIZE;
  1428. }
  1429. EXPORT_SYMBOL_GPL(devlink_dpipe_action_put);
  1430. static int devlink_dpipe_actions_put(struct devlink_dpipe_table *table,
  1431. struct sk_buff *skb)
  1432. {
  1433. struct nlattr *actions_attr;
  1434. actions_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLE_ACTIONS);
  1435. if (!actions_attr)
  1436. return -EMSGSIZE;
  1437. if (table->table_ops->actions_dump(table->priv, skb))
  1438. goto nla_put_failure;
  1439. nla_nest_end(skb, actions_attr);
  1440. return 0;
  1441. nla_put_failure:
  1442. nla_nest_cancel(skb, actions_attr);
  1443. return -EMSGSIZE;
  1444. }
  1445. static int devlink_dpipe_table_put(struct sk_buff *skb,
  1446. struct devlink_dpipe_table *table)
  1447. {
  1448. struct nlattr *table_attr;
  1449. u64 table_size;
  1450. table_size = table->table_ops->size_get(table->priv);
  1451. table_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLE);
  1452. if (!table_attr)
  1453. return -EMSGSIZE;
  1454. if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_TABLE_NAME, table->name) ||
  1455. nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_SIZE, table_size,
  1456. DEVLINK_ATTR_PAD))
  1457. goto nla_put_failure;
  1458. if (nla_put_u8(skb, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED,
  1459. table->counters_enabled))
  1460. goto nla_put_failure;
  1461. if (table->resource_valid) {
  1462. if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID,
  1463. table->resource_id, DEVLINK_ATTR_PAD) ||
  1464. nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS,
  1465. table->resource_units, DEVLINK_ATTR_PAD))
  1466. goto nla_put_failure;
  1467. }
  1468. if (devlink_dpipe_matches_put(table, skb))
  1469. goto nla_put_failure;
  1470. if (devlink_dpipe_actions_put(table, skb))
  1471. goto nla_put_failure;
  1472. nla_nest_end(skb, table_attr);
  1473. return 0;
  1474. nla_put_failure:
  1475. nla_nest_cancel(skb, table_attr);
  1476. return -EMSGSIZE;
  1477. }
  1478. static int devlink_dpipe_send_and_alloc_skb(struct sk_buff **pskb,
  1479. struct genl_info *info)
  1480. {
  1481. int err;
  1482. if (*pskb) {
  1483. err = genlmsg_reply(*pskb, info);
  1484. if (err)
  1485. return err;
  1486. }
  1487. *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1488. if (!*pskb)
  1489. return -ENOMEM;
  1490. return 0;
  1491. }
  1492. static int devlink_dpipe_tables_fill(struct genl_info *info,
  1493. enum devlink_command cmd, int flags,
  1494. struct list_head *dpipe_tables,
  1495. const char *table_name)
  1496. {
  1497. struct devlink *devlink = info->user_ptr[0];
  1498. struct devlink_dpipe_table *table;
  1499. struct nlattr *tables_attr;
  1500. struct sk_buff *skb = NULL;
  1501. struct nlmsghdr *nlh;
  1502. bool incomplete;
  1503. void *hdr;
  1504. int i;
  1505. int err;
  1506. table = list_first_entry(dpipe_tables,
  1507. struct devlink_dpipe_table, list);
  1508. start_again:
  1509. err = devlink_dpipe_send_and_alloc_skb(&skb, info);
  1510. if (err)
  1511. return err;
  1512. hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
  1513. &devlink_nl_family, NLM_F_MULTI, cmd);
  1514. if (!hdr) {
  1515. nlmsg_free(skb);
  1516. return -EMSGSIZE;
  1517. }
  1518. if (devlink_nl_put_handle(skb, devlink))
  1519. goto nla_put_failure;
  1520. tables_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLES);
  1521. if (!tables_attr)
  1522. goto nla_put_failure;
  1523. i = 0;
  1524. incomplete = false;
  1525. list_for_each_entry_from(table, dpipe_tables, list) {
  1526. if (!table_name) {
  1527. err = devlink_dpipe_table_put(skb, table);
  1528. if (err) {
  1529. if (!i)
  1530. goto err_table_put;
  1531. incomplete = true;
  1532. break;
  1533. }
  1534. } else {
  1535. if (!strcmp(table->name, table_name)) {
  1536. err = devlink_dpipe_table_put(skb, table);
  1537. if (err)
  1538. break;
  1539. }
  1540. }
  1541. i++;
  1542. }
  1543. nla_nest_end(skb, tables_attr);
  1544. genlmsg_end(skb, hdr);
  1545. if (incomplete)
  1546. goto start_again;
  1547. send_done:
  1548. nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
  1549. NLMSG_DONE, 0, flags | NLM_F_MULTI);
  1550. if (!nlh) {
  1551. err = devlink_dpipe_send_and_alloc_skb(&skb, info);
  1552. if (err)
  1553. return err;
  1554. goto send_done;
  1555. }
  1556. return genlmsg_reply(skb, info);
  1557. nla_put_failure:
  1558. err = -EMSGSIZE;
  1559. err_table_put:
  1560. nlmsg_free(skb);
  1561. return err;
  1562. }
  1563. static int devlink_nl_cmd_dpipe_table_get(struct sk_buff *skb,
  1564. struct genl_info *info)
  1565. {
  1566. struct devlink *devlink = info->user_ptr[0];
  1567. const char *table_name = NULL;
  1568. if (info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
  1569. table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
  1570. return devlink_dpipe_tables_fill(info, DEVLINK_CMD_DPIPE_TABLE_GET, 0,
  1571. &devlink->dpipe_table_list,
  1572. table_name);
  1573. }
  1574. static int devlink_dpipe_value_put(struct sk_buff *skb,
  1575. struct devlink_dpipe_value *value)
  1576. {
  1577. if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE,
  1578. value->value_size, value->value))
  1579. return -EMSGSIZE;
  1580. if (value->mask)
  1581. if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE_MASK,
  1582. value->value_size, value->mask))
  1583. return -EMSGSIZE;
  1584. if (value->mapping_valid)
  1585. if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_VALUE_MAPPING,
  1586. value->mapping_value))
  1587. return -EMSGSIZE;
  1588. return 0;
  1589. }
  1590. static int devlink_dpipe_action_value_put(struct sk_buff *skb,
  1591. struct devlink_dpipe_value *value)
  1592. {
  1593. if (!value->action)
  1594. return -EINVAL;
  1595. if (devlink_dpipe_action_put(skb, value->action))
  1596. return -EMSGSIZE;
  1597. if (devlink_dpipe_value_put(skb, value))
  1598. return -EMSGSIZE;
  1599. return 0;
  1600. }
  1601. static int devlink_dpipe_action_values_put(struct sk_buff *skb,
  1602. struct devlink_dpipe_value *values,
  1603. unsigned int values_count)
  1604. {
  1605. struct nlattr *action_attr;
  1606. int i;
  1607. int err;
  1608. for (i = 0; i < values_count; i++) {
  1609. action_attr = nla_nest_start(skb,
  1610. DEVLINK_ATTR_DPIPE_ACTION_VALUE);
  1611. if (!action_attr)
  1612. return -EMSGSIZE;
  1613. err = devlink_dpipe_action_value_put(skb, &values[i]);
  1614. if (err)
  1615. goto err_action_value_put;
  1616. nla_nest_end(skb, action_attr);
  1617. }
  1618. return 0;
  1619. err_action_value_put:
  1620. nla_nest_cancel(skb, action_attr);
  1621. return err;
  1622. }
  1623. static int devlink_dpipe_match_value_put(struct sk_buff *skb,
  1624. struct devlink_dpipe_value *value)
  1625. {
  1626. if (!value->match)
  1627. return -EINVAL;
  1628. if (devlink_dpipe_match_put(skb, value->match))
  1629. return -EMSGSIZE;
  1630. if (devlink_dpipe_value_put(skb, value))
  1631. return -EMSGSIZE;
  1632. return 0;
  1633. }
  1634. static int devlink_dpipe_match_values_put(struct sk_buff *skb,
  1635. struct devlink_dpipe_value *values,
  1636. unsigned int values_count)
  1637. {
  1638. struct nlattr *match_attr;
  1639. int i;
  1640. int err;
  1641. for (i = 0; i < values_count; i++) {
  1642. match_attr = nla_nest_start(skb,
  1643. DEVLINK_ATTR_DPIPE_MATCH_VALUE);
  1644. if (!match_attr)
  1645. return -EMSGSIZE;
  1646. err = devlink_dpipe_match_value_put(skb, &values[i]);
  1647. if (err)
  1648. goto err_match_value_put;
  1649. nla_nest_end(skb, match_attr);
  1650. }
  1651. return 0;
  1652. err_match_value_put:
  1653. nla_nest_cancel(skb, match_attr);
  1654. return err;
  1655. }
  1656. static int devlink_dpipe_entry_put(struct sk_buff *skb,
  1657. struct devlink_dpipe_entry *entry)
  1658. {
  1659. struct nlattr *entry_attr, *matches_attr, *actions_attr;
  1660. int err;
  1661. entry_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_ENTRY);
  1662. if (!entry_attr)
  1663. return -EMSGSIZE;
  1664. if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_INDEX, entry->index,
  1665. DEVLINK_ATTR_PAD))
  1666. goto nla_put_failure;
  1667. if (entry->counter_valid)
  1668. if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_COUNTER,
  1669. entry->counter, DEVLINK_ATTR_PAD))
  1670. goto nla_put_failure;
  1671. matches_attr = nla_nest_start(skb,
  1672. DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES);
  1673. if (!matches_attr)
  1674. goto nla_put_failure;
  1675. err = devlink_dpipe_match_values_put(skb, entry->match_values,
  1676. entry->match_values_count);
  1677. if (err) {
  1678. nla_nest_cancel(skb, matches_attr);
  1679. goto err_match_values_put;
  1680. }
  1681. nla_nest_end(skb, matches_attr);
  1682. actions_attr = nla_nest_start(skb,
  1683. DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES);
  1684. if (!actions_attr)
  1685. goto nla_put_failure;
  1686. err = devlink_dpipe_action_values_put(skb, entry->action_values,
  1687. entry->action_values_count);
  1688. if (err) {
  1689. nla_nest_cancel(skb, actions_attr);
  1690. goto err_action_values_put;
  1691. }
  1692. nla_nest_end(skb, actions_attr);
  1693. nla_nest_end(skb, entry_attr);
  1694. return 0;
  1695. nla_put_failure:
  1696. err = -EMSGSIZE;
  1697. err_match_values_put:
  1698. err_action_values_put:
  1699. nla_nest_cancel(skb, entry_attr);
  1700. return err;
  1701. }
  1702. static struct devlink_dpipe_table *
  1703. devlink_dpipe_table_find(struct list_head *dpipe_tables,
  1704. const char *table_name)
  1705. {
  1706. struct devlink_dpipe_table *table;
  1707. list_for_each_entry_rcu(table, dpipe_tables, list) {
  1708. if (!strcmp(table->name, table_name))
  1709. return table;
  1710. }
  1711. return NULL;
  1712. }
  1713. int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx)
  1714. {
  1715. struct devlink *devlink;
  1716. int err;
  1717. err = devlink_dpipe_send_and_alloc_skb(&dump_ctx->skb,
  1718. dump_ctx->info);
  1719. if (err)
  1720. return err;
  1721. dump_ctx->hdr = genlmsg_put(dump_ctx->skb,
  1722. dump_ctx->info->snd_portid,
  1723. dump_ctx->info->snd_seq,
  1724. &devlink_nl_family, NLM_F_MULTI,
  1725. dump_ctx->cmd);
  1726. if (!dump_ctx->hdr)
  1727. goto nla_put_failure;
  1728. devlink = dump_ctx->info->user_ptr[0];
  1729. if (devlink_nl_put_handle(dump_ctx->skb, devlink))
  1730. goto nla_put_failure;
  1731. dump_ctx->nest = nla_nest_start(dump_ctx->skb,
  1732. DEVLINK_ATTR_DPIPE_ENTRIES);
  1733. if (!dump_ctx->nest)
  1734. goto nla_put_failure;
  1735. return 0;
  1736. nla_put_failure:
  1737. nlmsg_free(dump_ctx->skb);
  1738. return -EMSGSIZE;
  1739. }
  1740. EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_prepare);
  1741. int devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx,
  1742. struct devlink_dpipe_entry *entry)
  1743. {
  1744. return devlink_dpipe_entry_put(dump_ctx->skb, entry);
  1745. }
  1746. EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_append);
  1747. int devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx)
  1748. {
  1749. nla_nest_end(dump_ctx->skb, dump_ctx->nest);
  1750. genlmsg_end(dump_ctx->skb, dump_ctx->hdr);
  1751. return 0;
  1752. }
  1753. EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_close);
  1754. void devlink_dpipe_entry_clear(struct devlink_dpipe_entry *entry)
  1755. {
  1756. unsigned int value_count, value_index;
  1757. struct devlink_dpipe_value *value;
  1758. value = entry->action_values;
  1759. value_count = entry->action_values_count;
  1760. for (value_index = 0; value_index < value_count; value_index++) {
  1761. kfree(value[value_index].value);
  1762. kfree(value[value_index].mask);
  1763. }
  1764. value = entry->match_values;
  1765. value_count = entry->match_values_count;
  1766. for (value_index = 0; value_index < value_count; value_index++) {
  1767. kfree(value[value_index].value);
  1768. kfree(value[value_index].mask);
  1769. }
  1770. }
  1771. EXPORT_SYMBOL(devlink_dpipe_entry_clear);
  1772. static int devlink_dpipe_entries_fill(struct genl_info *info,
  1773. enum devlink_command cmd, int flags,
  1774. struct devlink_dpipe_table *table)
  1775. {
  1776. struct devlink_dpipe_dump_ctx dump_ctx;
  1777. struct nlmsghdr *nlh;
  1778. int err;
  1779. dump_ctx.skb = NULL;
  1780. dump_ctx.cmd = cmd;
  1781. dump_ctx.info = info;
  1782. err = table->table_ops->entries_dump(table->priv,
  1783. table->counters_enabled,
  1784. &dump_ctx);
  1785. if (err)
  1786. return err;
  1787. send_done:
  1788. nlh = nlmsg_put(dump_ctx.skb, info->snd_portid, info->snd_seq,
  1789. NLMSG_DONE, 0, flags | NLM_F_MULTI);
  1790. if (!nlh) {
  1791. err = devlink_dpipe_send_and_alloc_skb(&dump_ctx.skb, info);
  1792. if (err)
  1793. return err;
  1794. goto send_done;
  1795. }
  1796. return genlmsg_reply(dump_ctx.skb, info);
  1797. }
  1798. static int devlink_nl_cmd_dpipe_entries_get(struct sk_buff *skb,
  1799. struct genl_info *info)
  1800. {
  1801. struct devlink *devlink = info->user_ptr[0];
  1802. struct devlink_dpipe_table *table;
  1803. const char *table_name;
  1804. if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
  1805. return -EINVAL;
  1806. table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
  1807. table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
  1808. table_name);
  1809. if (!table)
  1810. return -EINVAL;
  1811. if (!table->table_ops->entries_dump)
  1812. return -EINVAL;
  1813. return devlink_dpipe_entries_fill(info, DEVLINK_CMD_DPIPE_ENTRIES_GET,
  1814. 0, table);
  1815. }
  1816. static int devlink_dpipe_fields_put(struct sk_buff *skb,
  1817. const struct devlink_dpipe_header *header)
  1818. {
  1819. struct devlink_dpipe_field *field;
  1820. struct nlattr *field_attr;
  1821. int i;
  1822. for (i = 0; i < header->fields_count; i++) {
  1823. field = &header->fields[i];
  1824. field_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_FIELD);
  1825. if (!field_attr)
  1826. return -EMSGSIZE;
  1827. if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_FIELD_NAME, field->name) ||
  1828. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
  1829. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH, field->bitwidth) ||
  1830. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE, field->mapping_type))
  1831. goto nla_put_failure;
  1832. nla_nest_end(skb, field_attr);
  1833. }
  1834. return 0;
  1835. nla_put_failure:
  1836. nla_nest_cancel(skb, field_attr);
  1837. return -EMSGSIZE;
  1838. }
  1839. static int devlink_dpipe_header_put(struct sk_buff *skb,
  1840. struct devlink_dpipe_header *header)
  1841. {
  1842. struct nlattr *fields_attr, *header_attr;
  1843. int err;
  1844. header_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_HEADER);
  1845. if (!header_attr)
  1846. return -EMSGSIZE;
  1847. if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_HEADER_NAME, header->name) ||
  1848. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
  1849. nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
  1850. goto nla_put_failure;
  1851. fields_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_HEADER_FIELDS);
  1852. if (!fields_attr)
  1853. goto nla_put_failure;
  1854. err = devlink_dpipe_fields_put(skb, header);
  1855. if (err) {
  1856. nla_nest_cancel(skb, fields_attr);
  1857. goto nla_put_failure;
  1858. }
  1859. nla_nest_end(skb, fields_attr);
  1860. nla_nest_end(skb, header_attr);
  1861. return 0;
  1862. nla_put_failure:
  1863. err = -EMSGSIZE;
  1864. nla_nest_cancel(skb, header_attr);
  1865. return err;
  1866. }
  1867. static int devlink_dpipe_headers_fill(struct genl_info *info,
  1868. enum devlink_command cmd, int flags,
  1869. struct devlink_dpipe_headers *
  1870. dpipe_headers)
  1871. {
  1872. struct devlink *devlink = info->user_ptr[0];
  1873. struct nlattr *headers_attr;
  1874. struct sk_buff *skb = NULL;
  1875. struct nlmsghdr *nlh;
  1876. void *hdr;
  1877. int i, j;
  1878. int err;
  1879. i = 0;
  1880. start_again:
  1881. err = devlink_dpipe_send_and_alloc_skb(&skb, info);
  1882. if (err)
  1883. return err;
  1884. hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
  1885. &devlink_nl_family, NLM_F_MULTI, cmd);
  1886. if (!hdr) {
  1887. nlmsg_free(skb);
  1888. return -EMSGSIZE;
  1889. }
  1890. if (devlink_nl_put_handle(skb, devlink))
  1891. goto nla_put_failure;
  1892. headers_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_HEADERS);
  1893. if (!headers_attr)
  1894. goto nla_put_failure;
  1895. j = 0;
  1896. for (; i < dpipe_headers->headers_count; i++) {
  1897. err = devlink_dpipe_header_put(skb, dpipe_headers->headers[i]);
  1898. if (err) {
  1899. if (!j)
  1900. goto err_table_put;
  1901. break;
  1902. }
  1903. j++;
  1904. }
  1905. nla_nest_end(skb, headers_attr);
  1906. genlmsg_end(skb, hdr);
  1907. if (i != dpipe_headers->headers_count)
  1908. goto start_again;
  1909. send_done:
  1910. nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
  1911. NLMSG_DONE, 0, flags | NLM_F_MULTI);
  1912. if (!nlh) {
  1913. err = devlink_dpipe_send_and_alloc_skb(&skb, info);
  1914. if (err)
  1915. return err;
  1916. goto send_done;
  1917. }
  1918. return genlmsg_reply(skb, info);
  1919. nla_put_failure:
  1920. err = -EMSGSIZE;
  1921. err_table_put:
  1922. nlmsg_free(skb);
  1923. return err;
  1924. }
  1925. static int devlink_nl_cmd_dpipe_headers_get(struct sk_buff *skb,
  1926. struct genl_info *info)
  1927. {
  1928. struct devlink *devlink = info->user_ptr[0];
  1929. if (!devlink->dpipe_headers)
  1930. return -EOPNOTSUPP;
  1931. return devlink_dpipe_headers_fill(info, DEVLINK_CMD_DPIPE_HEADERS_GET,
  1932. 0, devlink->dpipe_headers);
  1933. }
  1934. static int devlink_dpipe_table_counters_set(struct devlink *devlink,
  1935. const char *table_name,
  1936. bool enable)
  1937. {
  1938. struct devlink_dpipe_table *table;
  1939. table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
  1940. table_name);
  1941. if (!table)
  1942. return -EINVAL;
  1943. if (table->counter_control_extern)
  1944. return -EOPNOTSUPP;
  1945. if (!(table->counters_enabled ^ enable))
  1946. return 0;
  1947. table->counters_enabled = enable;
  1948. if (table->table_ops->counters_set_update)
  1949. table->table_ops->counters_set_update(table->priv, enable);
  1950. return 0;
  1951. }
  1952. static int devlink_nl_cmd_dpipe_table_counters_set(struct sk_buff *skb,
  1953. struct genl_info *info)
  1954. {
  1955. struct devlink *devlink = info->user_ptr[0];
  1956. const char *table_name;
  1957. bool counters_enable;
  1958. if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME] ||
  1959. !info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED])
  1960. return -EINVAL;
  1961. table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
  1962. counters_enable = !!nla_get_u8(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED]);
  1963. return devlink_dpipe_table_counters_set(devlink, table_name,
  1964. counters_enable);
  1965. }
  1966. static struct devlink_resource *
  1967. devlink_resource_find(struct devlink *devlink,
  1968. struct devlink_resource *resource, u64 resource_id)
  1969. {
  1970. struct list_head *resource_list;
  1971. if (resource)
  1972. resource_list = &resource->resource_list;
  1973. else
  1974. resource_list = &devlink->resource_list;
  1975. list_for_each_entry(resource, resource_list, list) {
  1976. struct devlink_resource *child_resource;
  1977. if (resource->id == resource_id)
  1978. return resource;
  1979. child_resource = devlink_resource_find(devlink, resource,
  1980. resource_id);
  1981. if (child_resource)
  1982. return child_resource;
  1983. }
  1984. return NULL;
  1985. }
  1986. static void
  1987. devlink_resource_validate_children(struct devlink_resource *resource)
  1988. {
  1989. struct devlink_resource *child_resource;
  1990. bool size_valid = true;
  1991. u64 parts_size = 0;
  1992. if (list_empty(&resource->resource_list))
  1993. goto out;
  1994. list_for_each_entry(child_resource, &resource->resource_list, list)
  1995. parts_size += child_resource->size_new;
  1996. if (parts_size > resource->size_new)
  1997. size_valid = false;
  1998. out:
  1999. resource->size_valid = size_valid;
  2000. }
  2001. static int
  2002. devlink_resource_validate_size(struct devlink_resource *resource, u64 size,
  2003. struct netlink_ext_ack *extack)
  2004. {
  2005. u64 reminder;
  2006. int err = 0;
  2007. if (size > resource->size_params.size_max) {
  2008. NL_SET_ERR_MSG_MOD(extack, "Size larger than maximum");
  2009. err = -EINVAL;
  2010. }
  2011. if (size < resource->size_params.size_min) {
  2012. NL_SET_ERR_MSG_MOD(extack, "Size smaller than minimum");
  2013. err = -EINVAL;
  2014. }
  2015. div64_u64_rem(size, resource->size_params.size_granularity, &reminder);
  2016. if (reminder) {
  2017. NL_SET_ERR_MSG_MOD(extack, "Wrong granularity");
  2018. err = -EINVAL;
  2019. }
  2020. return err;
  2021. }
  2022. static int devlink_nl_cmd_resource_set(struct sk_buff *skb,
  2023. struct genl_info *info)
  2024. {
  2025. struct devlink *devlink = info->user_ptr[0];
  2026. struct devlink_resource *resource;
  2027. u64 resource_id;
  2028. u64 size;
  2029. int err;
  2030. if (!info->attrs[DEVLINK_ATTR_RESOURCE_ID] ||
  2031. !info->attrs[DEVLINK_ATTR_RESOURCE_SIZE])
  2032. return -EINVAL;
  2033. resource_id = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_ID]);
  2034. resource = devlink_resource_find(devlink, NULL, resource_id);
  2035. if (!resource)
  2036. return -EINVAL;
  2037. size = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_SIZE]);
  2038. err = devlink_resource_validate_size(resource, size, info->extack);
  2039. if (err)
  2040. return err;
  2041. resource->size_new = size;
  2042. devlink_resource_validate_children(resource);
  2043. if (resource->parent)
  2044. devlink_resource_validate_children(resource->parent);
  2045. return 0;
  2046. }
  2047. static int
  2048. devlink_resource_size_params_put(struct devlink_resource *resource,
  2049. struct sk_buff *skb)
  2050. {
  2051. struct devlink_resource_size_params *size_params;
  2052. size_params = &resource->size_params;
  2053. if (nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_GRAN,
  2054. size_params->size_granularity, DEVLINK_ATTR_PAD) ||
  2055. nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MAX,
  2056. size_params->size_max, DEVLINK_ATTR_PAD) ||
  2057. nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MIN,
  2058. size_params->size_min, DEVLINK_ATTR_PAD) ||
  2059. nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_UNIT, size_params->unit))
  2060. return -EMSGSIZE;
  2061. return 0;
  2062. }
  2063. static int devlink_resource_occ_put(struct devlink_resource *resource,
  2064. struct sk_buff *skb)
  2065. {
  2066. if (!resource->occ_get)
  2067. return 0;
  2068. return nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_OCC,
  2069. resource->occ_get(resource->occ_get_priv),
  2070. DEVLINK_ATTR_PAD);
  2071. }
  2072. static int devlink_resource_put(struct devlink *devlink, struct sk_buff *skb,
  2073. struct devlink_resource *resource)
  2074. {
  2075. struct devlink_resource *child_resource;
  2076. struct nlattr *child_resource_attr;
  2077. struct nlattr *resource_attr;
  2078. resource_attr = nla_nest_start(skb, DEVLINK_ATTR_RESOURCE);
  2079. if (!resource_attr)
  2080. return -EMSGSIZE;
  2081. if (nla_put_string(skb, DEVLINK_ATTR_RESOURCE_NAME, resource->name) ||
  2082. nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE, resource->size,
  2083. DEVLINK_ATTR_PAD) ||
  2084. nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_ID, resource->id,
  2085. DEVLINK_ATTR_PAD))
  2086. goto nla_put_failure;
  2087. if (resource->size != resource->size_new)
  2088. nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_NEW,
  2089. resource->size_new, DEVLINK_ATTR_PAD);
  2090. if (devlink_resource_occ_put(resource, skb))
  2091. goto nla_put_failure;
  2092. if (devlink_resource_size_params_put(resource, skb))
  2093. goto nla_put_failure;
  2094. if (list_empty(&resource->resource_list))
  2095. goto out;
  2096. if (nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_SIZE_VALID,
  2097. resource->size_valid))
  2098. goto nla_put_failure;
  2099. child_resource_attr = nla_nest_start(skb, DEVLINK_ATTR_RESOURCE_LIST);
  2100. if (!child_resource_attr)
  2101. goto nla_put_failure;
  2102. list_for_each_entry(child_resource, &resource->resource_list, list) {
  2103. if (devlink_resource_put(devlink, skb, child_resource))
  2104. goto resource_put_failure;
  2105. }
  2106. nla_nest_end(skb, child_resource_attr);
  2107. out:
  2108. nla_nest_end(skb, resource_attr);
  2109. return 0;
  2110. resource_put_failure:
  2111. nla_nest_cancel(skb, child_resource_attr);
  2112. nla_put_failure:
  2113. nla_nest_cancel(skb, resource_attr);
  2114. return -EMSGSIZE;
  2115. }
  2116. static int devlink_resource_fill(struct genl_info *info,
  2117. enum devlink_command cmd, int flags)
  2118. {
  2119. struct devlink *devlink = info->user_ptr[0];
  2120. struct devlink_resource *resource;
  2121. struct nlattr *resources_attr;
  2122. struct sk_buff *skb = NULL;
  2123. struct nlmsghdr *nlh;
  2124. bool incomplete;
  2125. void *hdr;
  2126. int i;
  2127. int err;
  2128. resource = list_first_entry(&devlink->resource_list,
  2129. struct devlink_resource, list);
  2130. start_again:
  2131. err = devlink_dpipe_send_and_alloc_skb(&skb, info);
  2132. if (err)
  2133. return err;
  2134. hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
  2135. &devlink_nl_family, NLM_F_MULTI, cmd);
  2136. if (!hdr) {
  2137. nlmsg_free(skb);
  2138. return -EMSGSIZE;
  2139. }
  2140. if (devlink_nl_put_handle(skb, devlink))
  2141. goto nla_put_failure;
  2142. resources_attr = nla_nest_start(skb, DEVLINK_ATTR_RESOURCE_LIST);
  2143. if (!resources_attr)
  2144. goto nla_put_failure;
  2145. incomplete = false;
  2146. i = 0;
  2147. list_for_each_entry_from(resource, &devlink->resource_list, list) {
  2148. err = devlink_resource_put(devlink, skb, resource);
  2149. if (err) {
  2150. if (!i)
  2151. goto err_resource_put;
  2152. incomplete = true;
  2153. break;
  2154. }
  2155. i++;
  2156. }
  2157. nla_nest_end(skb, resources_attr);
  2158. genlmsg_end(skb, hdr);
  2159. if (incomplete)
  2160. goto start_again;
  2161. send_done:
  2162. nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
  2163. NLMSG_DONE, 0, flags | NLM_F_MULTI);
  2164. if (!nlh) {
  2165. err = devlink_dpipe_send_and_alloc_skb(&skb, info);
  2166. if (err)
  2167. goto err_skb_send_alloc;
  2168. goto send_done;
  2169. }
  2170. return genlmsg_reply(skb, info);
  2171. nla_put_failure:
  2172. err = -EMSGSIZE;
  2173. err_resource_put:
  2174. err_skb_send_alloc:
  2175. nlmsg_free(skb);
  2176. return err;
  2177. }
  2178. static int devlink_nl_cmd_resource_dump(struct sk_buff *skb,
  2179. struct genl_info *info)
  2180. {
  2181. struct devlink *devlink = info->user_ptr[0];
  2182. if (list_empty(&devlink->resource_list))
  2183. return -EOPNOTSUPP;
  2184. return devlink_resource_fill(info, DEVLINK_CMD_RESOURCE_DUMP, 0);
  2185. }
  2186. static int
  2187. devlink_resources_validate(struct devlink *devlink,
  2188. struct devlink_resource *resource,
  2189. struct genl_info *info)
  2190. {
  2191. struct list_head *resource_list;
  2192. int err = 0;
  2193. if (resource)
  2194. resource_list = &resource->resource_list;
  2195. else
  2196. resource_list = &devlink->resource_list;
  2197. list_for_each_entry(resource, resource_list, list) {
  2198. if (!resource->size_valid)
  2199. return -EINVAL;
  2200. err = devlink_resources_validate(devlink, resource, info);
  2201. if (err)
  2202. return err;
  2203. }
  2204. return err;
  2205. }
  2206. static int devlink_nl_cmd_reload(struct sk_buff *skb, struct genl_info *info)
  2207. {
  2208. struct devlink *devlink = info->user_ptr[0];
  2209. int err;
  2210. if (!devlink->ops->reload)
  2211. return -EOPNOTSUPP;
  2212. err = devlink_resources_validate(devlink, NULL, info);
  2213. if (err) {
  2214. NL_SET_ERR_MSG_MOD(info->extack, "resources size validation failed");
  2215. return err;
  2216. }
  2217. return devlink->ops->reload(devlink, info->extack);
  2218. }
  2219. static const struct devlink_param devlink_param_generic[] = {};
  2220. static int devlink_param_generic_verify(const struct devlink_param *param)
  2221. {
  2222. /* verify it match generic parameter by id and name */
  2223. if (param->id > DEVLINK_PARAM_GENERIC_ID_MAX)
  2224. return -EINVAL;
  2225. if (strcmp(param->name, devlink_param_generic[param->id].name))
  2226. return -ENOENT;
  2227. WARN_ON(param->type != devlink_param_generic[param->id].type);
  2228. return 0;
  2229. }
  2230. static int devlink_param_driver_verify(const struct devlink_param *param)
  2231. {
  2232. int i;
  2233. if (param->id <= DEVLINK_PARAM_GENERIC_ID_MAX)
  2234. return -EINVAL;
  2235. /* verify no such name in generic params */
  2236. for (i = 0; i <= DEVLINK_PARAM_GENERIC_ID_MAX; i++)
  2237. if (!strcmp(param->name, devlink_param_generic[i].name))
  2238. return -EEXIST;
  2239. return 0;
  2240. }
  2241. static struct devlink_param_item *
  2242. devlink_param_find_by_name(struct list_head *param_list,
  2243. const char *param_name)
  2244. {
  2245. struct devlink_param_item *param_item;
  2246. list_for_each_entry(param_item, param_list, list)
  2247. if (!strcmp(param_item->param->name, param_name))
  2248. return param_item;
  2249. return NULL;
  2250. }
  2251. static bool
  2252. devlink_param_cmode_is_supported(const struct devlink_param *param,
  2253. enum devlink_param_cmode cmode)
  2254. {
  2255. return test_bit(cmode, &param->supported_cmodes);
  2256. }
  2257. static int devlink_param_get(struct devlink *devlink,
  2258. const struct devlink_param *param,
  2259. struct devlink_param_gset_ctx *ctx)
  2260. {
  2261. if (!param->get)
  2262. return -EOPNOTSUPP;
  2263. return param->get(devlink, param->id, ctx);
  2264. }
  2265. static int devlink_param_set(struct devlink *devlink,
  2266. const struct devlink_param *param,
  2267. struct devlink_param_gset_ctx *ctx)
  2268. {
  2269. if (!param->set)
  2270. return -EOPNOTSUPP;
  2271. return param->set(devlink, param->id, ctx);
  2272. }
  2273. static int
  2274. devlink_param_type_to_nla_type(enum devlink_param_type param_type)
  2275. {
  2276. switch (param_type) {
  2277. case DEVLINK_PARAM_TYPE_U8:
  2278. return NLA_U8;
  2279. case DEVLINK_PARAM_TYPE_U16:
  2280. return NLA_U16;
  2281. case DEVLINK_PARAM_TYPE_U32:
  2282. return NLA_U32;
  2283. case DEVLINK_PARAM_TYPE_STRING:
  2284. return NLA_STRING;
  2285. case DEVLINK_PARAM_TYPE_BOOL:
  2286. return NLA_FLAG;
  2287. default:
  2288. return -EINVAL;
  2289. }
  2290. }
  2291. static int
  2292. devlink_nl_param_value_fill_one(struct sk_buff *msg,
  2293. enum devlink_param_type type,
  2294. enum devlink_param_cmode cmode,
  2295. union devlink_param_value val)
  2296. {
  2297. struct nlattr *param_value_attr;
  2298. param_value_attr = nla_nest_start(msg, DEVLINK_ATTR_PARAM_VALUE);
  2299. if (!param_value_attr)
  2300. goto nla_put_failure;
  2301. if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_CMODE, cmode))
  2302. goto value_nest_cancel;
  2303. switch (type) {
  2304. case DEVLINK_PARAM_TYPE_U8:
  2305. if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu8))
  2306. goto value_nest_cancel;
  2307. break;
  2308. case DEVLINK_PARAM_TYPE_U16:
  2309. if (nla_put_u16(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu16))
  2310. goto value_nest_cancel;
  2311. break;
  2312. case DEVLINK_PARAM_TYPE_U32:
  2313. if (nla_put_u32(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu32))
  2314. goto value_nest_cancel;
  2315. break;
  2316. case DEVLINK_PARAM_TYPE_STRING:
  2317. if (nla_put_string(msg, DEVLINK_ATTR_PARAM_VALUE_DATA,
  2318. val.vstr))
  2319. goto value_nest_cancel;
  2320. break;
  2321. case DEVLINK_PARAM_TYPE_BOOL:
  2322. if (val.vbool &&
  2323. nla_put_flag(msg, DEVLINK_ATTR_PARAM_VALUE_DATA))
  2324. goto value_nest_cancel;
  2325. break;
  2326. }
  2327. nla_nest_end(msg, param_value_attr);
  2328. return 0;
  2329. value_nest_cancel:
  2330. nla_nest_cancel(msg, param_value_attr);
  2331. nla_put_failure:
  2332. return -EMSGSIZE;
  2333. }
  2334. static int devlink_nl_param_fill(struct sk_buff *msg, struct devlink *devlink,
  2335. struct devlink_param_item *param_item,
  2336. enum devlink_command cmd,
  2337. u32 portid, u32 seq, int flags)
  2338. {
  2339. union devlink_param_value param_value[DEVLINK_PARAM_CMODE_MAX + 1];
  2340. const struct devlink_param *param = param_item->param;
  2341. struct devlink_param_gset_ctx ctx;
  2342. struct nlattr *param_values_list;
  2343. struct nlattr *param_attr;
  2344. int nla_type;
  2345. void *hdr;
  2346. int err;
  2347. int i;
  2348. /* Get value from driver part to driverinit configuration mode */
  2349. for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
  2350. if (!devlink_param_cmode_is_supported(param, i))
  2351. continue;
  2352. if (i == DEVLINK_PARAM_CMODE_DRIVERINIT) {
  2353. if (!param_item->driverinit_value_valid)
  2354. return -EOPNOTSUPP;
  2355. param_value[i] = param_item->driverinit_value;
  2356. } else {
  2357. ctx.cmode = i;
  2358. err = devlink_param_get(devlink, param, &ctx);
  2359. if (err)
  2360. return err;
  2361. param_value[i] = ctx.val;
  2362. }
  2363. }
  2364. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  2365. if (!hdr)
  2366. return -EMSGSIZE;
  2367. if (devlink_nl_put_handle(msg, devlink))
  2368. goto genlmsg_cancel;
  2369. param_attr = nla_nest_start(msg, DEVLINK_ATTR_PARAM);
  2370. if (!param_attr)
  2371. goto genlmsg_cancel;
  2372. if (nla_put_string(msg, DEVLINK_ATTR_PARAM_NAME, param->name))
  2373. goto param_nest_cancel;
  2374. if (param->generic && nla_put_flag(msg, DEVLINK_ATTR_PARAM_GENERIC))
  2375. goto param_nest_cancel;
  2376. nla_type = devlink_param_type_to_nla_type(param->type);
  2377. if (nla_type < 0)
  2378. goto param_nest_cancel;
  2379. if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_TYPE, nla_type))
  2380. goto param_nest_cancel;
  2381. param_values_list = nla_nest_start(msg, DEVLINK_ATTR_PARAM_VALUES_LIST);
  2382. if (!param_values_list)
  2383. goto param_nest_cancel;
  2384. for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
  2385. if (!devlink_param_cmode_is_supported(param, i))
  2386. continue;
  2387. err = devlink_nl_param_value_fill_one(msg, param->type,
  2388. i, param_value[i]);
  2389. if (err)
  2390. goto values_list_nest_cancel;
  2391. }
  2392. nla_nest_end(msg, param_values_list);
  2393. nla_nest_end(msg, param_attr);
  2394. genlmsg_end(msg, hdr);
  2395. return 0;
  2396. values_list_nest_cancel:
  2397. nla_nest_end(msg, param_values_list);
  2398. param_nest_cancel:
  2399. nla_nest_cancel(msg, param_attr);
  2400. genlmsg_cancel:
  2401. genlmsg_cancel(msg, hdr);
  2402. return -EMSGSIZE;
  2403. }
  2404. static int devlink_nl_cmd_param_get_dumpit(struct sk_buff *msg,
  2405. struct netlink_callback *cb)
  2406. {
  2407. struct devlink_param_item *param_item;
  2408. struct devlink *devlink;
  2409. int start = cb->args[0];
  2410. int idx = 0;
  2411. int err;
  2412. mutex_lock(&devlink_mutex);
  2413. list_for_each_entry(devlink, &devlink_list, list) {
  2414. if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
  2415. continue;
  2416. mutex_lock(&devlink->lock);
  2417. list_for_each_entry(param_item, &devlink->param_list, list) {
  2418. if (idx < start) {
  2419. idx++;
  2420. continue;
  2421. }
  2422. err = devlink_nl_param_fill(msg, devlink, param_item,
  2423. DEVLINK_CMD_PARAM_GET,
  2424. NETLINK_CB(cb->skb).portid,
  2425. cb->nlh->nlmsg_seq,
  2426. NLM_F_MULTI);
  2427. if (err) {
  2428. mutex_unlock(&devlink->lock);
  2429. goto out;
  2430. }
  2431. idx++;
  2432. }
  2433. mutex_unlock(&devlink->lock);
  2434. }
  2435. out:
  2436. mutex_unlock(&devlink_mutex);
  2437. cb->args[0] = idx;
  2438. return msg->len;
  2439. }
  2440. static int
  2441. devlink_param_type_get_from_info(struct genl_info *info,
  2442. enum devlink_param_type *param_type)
  2443. {
  2444. if (!info->attrs[DEVLINK_ATTR_PARAM_TYPE])
  2445. return -EINVAL;
  2446. switch (nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_TYPE])) {
  2447. case NLA_U8:
  2448. *param_type = DEVLINK_PARAM_TYPE_U8;
  2449. break;
  2450. case NLA_U16:
  2451. *param_type = DEVLINK_PARAM_TYPE_U16;
  2452. break;
  2453. case NLA_U32:
  2454. *param_type = DEVLINK_PARAM_TYPE_U32;
  2455. break;
  2456. case NLA_STRING:
  2457. *param_type = DEVLINK_PARAM_TYPE_STRING;
  2458. break;
  2459. case NLA_FLAG:
  2460. *param_type = DEVLINK_PARAM_TYPE_BOOL;
  2461. break;
  2462. default:
  2463. return -EINVAL;
  2464. }
  2465. return 0;
  2466. }
  2467. static int
  2468. devlink_param_value_get_from_info(const struct devlink_param *param,
  2469. struct genl_info *info,
  2470. union devlink_param_value *value)
  2471. {
  2472. if (param->type != DEVLINK_PARAM_TYPE_BOOL &&
  2473. !info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA])
  2474. return -EINVAL;
  2475. switch (param->type) {
  2476. case DEVLINK_PARAM_TYPE_U8:
  2477. value->vu8 = nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]);
  2478. break;
  2479. case DEVLINK_PARAM_TYPE_U16:
  2480. value->vu16 = nla_get_u16(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]);
  2481. break;
  2482. case DEVLINK_PARAM_TYPE_U32:
  2483. value->vu32 = nla_get_u32(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]);
  2484. break;
  2485. case DEVLINK_PARAM_TYPE_STRING:
  2486. if (nla_len(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]) >
  2487. DEVLINK_PARAM_MAX_STRING_VALUE)
  2488. return -EINVAL;
  2489. value->vstr = nla_data(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]);
  2490. break;
  2491. case DEVLINK_PARAM_TYPE_BOOL:
  2492. value->vbool = info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA] ?
  2493. true : false;
  2494. break;
  2495. }
  2496. return 0;
  2497. }
  2498. static struct devlink_param_item *
  2499. devlink_param_get_from_info(struct devlink *devlink,
  2500. struct genl_info *info)
  2501. {
  2502. char *param_name;
  2503. if (!info->attrs[DEVLINK_ATTR_PARAM_NAME])
  2504. return NULL;
  2505. param_name = nla_data(info->attrs[DEVLINK_ATTR_PARAM_NAME]);
  2506. return devlink_param_find_by_name(&devlink->param_list, param_name);
  2507. }
  2508. static int devlink_nl_cmd_param_get_doit(struct sk_buff *skb,
  2509. struct genl_info *info)
  2510. {
  2511. struct devlink *devlink = info->user_ptr[0];
  2512. struct devlink_param_item *param_item;
  2513. struct sk_buff *msg;
  2514. int err;
  2515. param_item = devlink_param_get_from_info(devlink, info);
  2516. if (!param_item)
  2517. return -EINVAL;
  2518. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  2519. if (!msg)
  2520. return -ENOMEM;
  2521. err = devlink_nl_param_fill(msg, devlink, param_item,
  2522. DEVLINK_CMD_PARAM_GET,
  2523. info->snd_portid, info->snd_seq, 0);
  2524. if (err) {
  2525. nlmsg_free(msg);
  2526. return err;
  2527. }
  2528. return genlmsg_reply(msg, info);
  2529. }
  2530. static int devlink_nl_cmd_param_set_doit(struct sk_buff *skb,
  2531. struct genl_info *info)
  2532. {
  2533. struct devlink *devlink = info->user_ptr[0];
  2534. enum devlink_param_type param_type;
  2535. struct devlink_param_gset_ctx ctx;
  2536. enum devlink_param_cmode cmode;
  2537. struct devlink_param_item *param_item;
  2538. const struct devlink_param *param;
  2539. union devlink_param_value value;
  2540. int err = 0;
  2541. param_item = devlink_param_get_from_info(devlink, info);
  2542. if (!param_item)
  2543. return -EINVAL;
  2544. param = param_item->param;
  2545. err = devlink_param_type_get_from_info(info, &param_type);
  2546. if (err)
  2547. return err;
  2548. if (param_type != param->type)
  2549. return -EINVAL;
  2550. err = devlink_param_value_get_from_info(param, info, &value);
  2551. if (err)
  2552. return err;
  2553. if (param->validate) {
  2554. err = param->validate(devlink, param->id, value, info->extack);
  2555. if (err)
  2556. return err;
  2557. }
  2558. if (!info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE])
  2559. return -EINVAL;
  2560. cmode = nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE]);
  2561. if (!devlink_param_cmode_is_supported(param, cmode))
  2562. return -EOPNOTSUPP;
  2563. if (cmode == DEVLINK_PARAM_CMODE_DRIVERINIT) {
  2564. param_item->driverinit_value = value;
  2565. param_item->driverinit_value_valid = true;
  2566. } else {
  2567. if (!param->set)
  2568. return -EOPNOTSUPP;
  2569. ctx.val = value;
  2570. ctx.cmode = cmode;
  2571. err = devlink_param_set(devlink, param, &ctx);
  2572. if (err)
  2573. return err;
  2574. }
  2575. return 0;
  2576. }
  2577. static int devlink_param_register_one(struct devlink *devlink,
  2578. const struct devlink_param *param)
  2579. {
  2580. struct devlink_param_item *param_item;
  2581. if (devlink_param_find_by_name(&devlink->param_list,
  2582. param->name))
  2583. return -EEXIST;
  2584. if (param->supported_cmodes == BIT(DEVLINK_PARAM_CMODE_DRIVERINIT))
  2585. WARN_ON(param->get || param->set);
  2586. else
  2587. WARN_ON(!param->get || !param->set);
  2588. param_item = kzalloc(sizeof(*param_item), GFP_KERNEL);
  2589. if (!param_item)
  2590. return -ENOMEM;
  2591. param_item->param = param;
  2592. list_add_tail(&param_item->list, &devlink->param_list);
  2593. return 0;
  2594. }
  2595. static void devlink_param_unregister_one(struct devlink *devlink,
  2596. const struct devlink_param *param)
  2597. {
  2598. struct devlink_param_item *param_item;
  2599. param_item = devlink_param_find_by_name(&devlink->param_list,
  2600. param->name);
  2601. WARN_ON(!param_item);
  2602. list_del(&param_item->list);
  2603. kfree(param_item);
  2604. }
  2605. static const struct nla_policy devlink_nl_policy[DEVLINK_ATTR_MAX + 1] = {
  2606. [DEVLINK_ATTR_BUS_NAME] = { .type = NLA_NUL_STRING },
  2607. [DEVLINK_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING },
  2608. [DEVLINK_ATTR_PORT_INDEX] = { .type = NLA_U32 },
  2609. [DEVLINK_ATTR_PORT_TYPE] = { .type = NLA_U16 },
  2610. [DEVLINK_ATTR_PORT_SPLIT_COUNT] = { .type = NLA_U32 },
  2611. [DEVLINK_ATTR_SB_INDEX] = { .type = NLA_U32 },
  2612. [DEVLINK_ATTR_SB_POOL_INDEX] = { .type = NLA_U16 },
  2613. [DEVLINK_ATTR_SB_POOL_TYPE] = { .type = NLA_U8 },
  2614. [DEVLINK_ATTR_SB_POOL_SIZE] = { .type = NLA_U32 },
  2615. [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE] = { .type = NLA_U8 },
  2616. [DEVLINK_ATTR_SB_THRESHOLD] = { .type = NLA_U32 },
  2617. [DEVLINK_ATTR_SB_TC_INDEX] = { .type = NLA_U16 },
  2618. [DEVLINK_ATTR_ESWITCH_MODE] = { .type = NLA_U16 },
  2619. [DEVLINK_ATTR_ESWITCH_INLINE_MODE] = { .type = NLA_U8 },
  2620. [DEVLINK_ATTR_ESWITCH_ENCAP_MODE] = { .type = NLA_U8 },
  2621. [DEVLINK_ATTR_DPIPE_TABLE_NAME] = { .type = NLA_NUL_STRING },
  2622. [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED] = { .type = NLA_U8 },
  2623. [DEVLINK_ATTR_RESOURCE_ID] = { .type = NLA_U64},
  2624. [DEVLINK_ATTR_RESOURCE_SIZE] = { .type = NLA_U64},
  2625. [DEVLINK_ATTR_PARAM_NAME] = { .type = NLA_NUL_STRING },
  2626. [DEVLINK_ATTR_PARAM_TYPE] = { .type = NLA_U8 },
  2627. [DEVLINK_ATTR_PARAM_VALUE_CMODE] = { .type = NLA_U8 },
  2628. };
  2629. static const struct genl_ops devlink_nl_ops[] = {
  2630. {
  2631. .cmd = DEVLINK_CMD_GET,
  2632. .doit = devlink_nl_cmd_get_doit,
  2633. .dumpit = devlink_nl_cmd_get_dumpit,
  2634. .policy = devlink_nl_policy,
  2635. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  2636. /* can be retrieved by unprivileged users */
  2637. },
  2638. {
  2639. .cmd = DEVLINK_CMD_PORT_GET,
  2640. .doit = devlink_nl_cmd_port_get_doit,
  2641. .dumpit = devlink_nl_cmd_port_get_dumpit,
  2642. .policy = devlink_nl_policy,
  2643. .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
  2644. /* can be retrieved by unprivileged users */
  2645. },
  2646. {
  2647. .cmd = DEVLINK_CMD_PORT_SET,
  2648. .doit = devlink_nl_cmd_port_set_doit,
  2649. .policy = devlink_nl_policy,
  2650. .flags = GENL_ADMIN_PERM,
  2651. .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
  2652. },
  2653. {
  2654. .cmd = DEVLINK_CMD_PORT_SPLIT,
  2655. .doit = devlink_nl_cmd_port_split_doit,
  2656. .policy = devlink_nl_policy,
  2657. .flags = GENL_ADMIN_PERM,
  2658. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  2659. DEVLINK_NL_FLAG_NO_LOCK,
  2660. },
  2661. {
  2662. .cmd = DEVLINK_CMD_PORT_UNSPLIT,
  2663. .doit = devlink_nl_cmd_port_unsplit_doit,
  2664. .policy = devlink_nl_policy,
  2665. .flags = GENL_ADMIN_PERM,
  2666. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  2667. DEVLINK_NL_FLAG_NO_LOCK,
  2668. },
  2669. {
  2670. .cmd = DEVLINK_CMD_SB_GET,
  2671. .doit = devlink_nl_cmd_sb_get_doit,
  2672. .dumpit = devlink_nl_cmd_sb_get_dumpit,
  2673. .policy = devlink_nl_policy,
  2674. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  2675. DEVLINK_NL_FLAG_NEED_SB,
  2676. /* can be retrieved by unprivileged users */
  2677. },
  2678. {
  2679. .cmd = DEVLINK_CMD_SB_POOL_GET,
  2680. .doit = devlink_nl_cmd_sb_pool_get_doit,
  2681. .dumpit = devlink_nl_cmd_sb_pool_get_dumpit,
  2682. .policy = devlink_nl_policy,
  2683. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  2684. DEVLINK_NL_FLAG_NEED_SB,
  2685. /* can be retrieved by unprivileged users */
  2686. },
  2687. {
  2688. .cmd = DEVLINK_CMD_SB_POOL_SET,
  2689. .doit = devlink_nl_cmd_sb_pool_set_doit,
  2690. .policy = devlink_nl_policy,
  2691. .flags = GENL_ADMIN_PERM,
  2692. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  2693. DEVLINK_NL_FLAG_NEED_SB,
  2694. },
  2695. {
  2696. .cmd = DEVLINK_CMD_SB_PORT_POOL_GET,
  2697. .doit = devlink_nl_cmd_sb_port_pool_get_doit,
  2698. .dumpit = devlink_nl_cmd_sb_port_pool_get_dumpit,
  2699. .policy = devlink_nl_policy,
  2700. .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
  2701. DEVLINK_NL_FLAG_NEED_SB,
  2702. /* can be retrieved by unprivileged users */
  2703. },
  2704. {
  2705. .cmd = DEVLINK_CMD_SB_PORT_POOL_SET,
  2706. .doit = devlink_nl_cmd_sb_port_pool_set_doit,
  2707. .policy = devlink_nl_policy,
  2708. .flags = GENL_ADMIN_PERM,
  2709. .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
  2710. DEVLINK_NL_FLAG_NEED_SB,
  2711. },
  2712. {
  2713. .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_GET,
  2714. .doit = devlink_nl_cmd_sb_tc_pool_bind_get_doit,
  2715. .dumpit = devlink_nl_cmd_sb_tc_pool_bind_get_dumpit,
  2716. .policy = devlink_nl_policy,
  2717. .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
  2718. DEVLINK_NL_FLAG_NEED_SB,
  2719. /* can be retrieved by unprivileged users */
  2720. },
  2721. {
  2722. .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_SET,
  2723. .doit = devlink_nl_cmd_sb_tc_pool_bind_set_doit,
  2724. .policy = devlink_nl_policy,
  2725. .flags = GENL_ADMIN_PERM,
  2726. .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
  2727. DEVLINK_NL_FLAG_NEED_SB,
  2728. },
  2729. {
  2730. .cmd = DEVLINK_CMD_SB_OCC_SNAPSHOT,
  2731. .doit = devlink_nl_cmd_sb_occ_snapshot_doit,
  2732. .policy = devlink_nl_policy,
  2733. .flags = GENL_ADMIN_PERM,
  2734. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  2735. DEVLINK_NL_FLAG_NEED_SB,
  2736. },
  2737. {
  2738. .cmd = DEVLINK_CMD_SB_OCC_MAX_CLEAR,
  2739. .doit = devlink_nl_cmd_sb_occ_max_clear_doit,
  2740. .policy = devlink_nl_policy,
  2741. .flags = GENL_ADMIN_PERM,
  2742. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  2743. DEVLINK_NL_FLAG_NEED_SB,
  2744. },
  2745. {
  2746. .cmd = DEVLINK_CMD_ESWITCH_GET,
  2747. .doit = devlink_nl_cmd_eswitch_get_doit,
  2748. .policy = devlink_nl_policy,
  2749. .flags = GENL_ADMIN_PERM,
  2750. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  2751. },
  2752. {
  2753. .cmd = DEVLINK_CMD_ESWITCH_SET,
  2754. .doit = devlink_nl_cmd_eswitch_set_doit,
  2755. .policy = devlink_nl_policy,
  2756. .flags = GENL_ADMIN_PERM,
  2757. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  2758. DEVLINK_NL_FLAG_NO_LOCK,
  2759. },
  2760. {
  2761. .cmd = DEVLINK_CMD_DPIPE_TABLE_GET,
  2762. .doit = devlink_nl_cmd_dpipe_table_get,
  2763. .policy = devlink_nl_policy,
  2764. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  2765. /* can be retrieved by unprivileged users */
  2766. },
  2767. {
  2768. .cmd = DEVLINK_CMD_DPIPE_ENTRIES_GET,
  2769. .doit = devlink_nl_cmd_dpipe_entries_get,
  2770. .policy = devlink_nl_policy,
  2771. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  2772. /* can be retrieved by unprivileged users */
  2773. },
  2774. {
  2775. .cmd = DEVLINK_CMD_DPIPE_HEADERS_GET,
  2776. .doit = devlink_nl_cmd_dpipe_headers_get,
  2777. .policy = devlink_nl_policy,
  2778. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  2779. /* can be retrieved by unprivileged users */
  2780. },
  2781. {
  2782. .cmd = DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET,
  2783. .doit = devlink_nl_cmd_dpipe_table_counters_set,
  2784. .policy = devlink_nl_policy,
  2785. .flags = GENL_ADMIN_PERM,
  2786. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  2787. },
  2788. {
  2789. .cmd = DEVLINK_CMD_RESOURCE_SET,
  2790. .doit = devlink_nl_cmd_resource_set,
  2791. .policy = devlink_nl_policy,
  2792. .flags = GENL_ADMIN_PERM,
  2793. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  2794. },
  2795. {
  2796. .cmd = DEVLINK_CMD_RESOURCE_DUMP,
  2797. .doit = devlink_nl_cmd_resource_dump,
  2798. .policy = devlink_nl_policy,
  2799. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  2800. /* can be retrieved by unprivileged users */
  2801. },
  2802. {
  2803. .cmd = DEVLINK_CMD_RELOAD,
  2804. .doit = devlink_nl_cmd_reload,
  2805. .policy = devlink_nl_policy,
  2806. .flags = GENL_ADMIN_PERM,
  2807. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  2808. DEVLINK_NL_FLAG_NO_LOCK,
  2809. },
  2810. {
  2811. .cmd = DEVLINK_CMD_PARAM_GET,
  2812. .doit = devlink_nl_cmd_param_get_doit,
  2813. .dumpit = devlink_nl_cmd_param_get_dumpit,
  2814. .policy = devlink_nl_policy,
  2815. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  2816. /* can be retrieved by unprivileged users */
  2817. },
  2818. {
  2819. .cmd = DEVLINK_CMD_PARAM_SET,
  2820. .doit = devlink_nl_cmd_param_set_doit,
  2821. .policy = devlink_nl_policy,
  2822. .flags = GENL_ADMIN_PERM,
  2823. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  2824. },
  2825. };
  2826. static struct genl_family devlink_nl_family __ro_after_init = {
  2827. .name = DEVLINK_GENL_NAME,
  2828. .version = DEVLINK_GENL_VERSION,
  2829. .maxattr = DEVLINK_ATTR_MAX,
  2830. .netnsok = true,
  2831. .pre_doit = devlink_nl_pre_doit,
  2832. .post_doit = devlink_nl_post_doit,
  2833. .module = THIS_MODULE,
  2834. .ops = devlink_nl_ops,
  2835. .n_ops = ARRAY_SIZE(devlink_nl_ops),
  2836. .mcgrps = devlink_nl_mcgrps,
  2837. .n_mcgrps = ARRAY_SIZE(devlink_nl_mcgrps),
  2838. };
  2839. /**
  2840. * devlink_alloc - Allocate new devlink instance resources
  2841. *
  2842. * @ops: ops
  2843. * @priv_size: size of user private data
  2844. *
  2845. * Allocate new devlink instance resources, including devlink index
  2846. * and name.
  2847. */
  2848. struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size)
  2849. {
  2850. struct devlink *devlink;
  2851. devlink = kzalloc(sizeof(*devlink) + priv_size, GFP_KERNEL);
  2852. if (!devlink)
  2853. return NULL;
  2854. devlink->ops = ops;
  2855. devlink_net_set(devlink, &init_net);
  2856. INIT_LIST_HEAD(&devlink->port_list);
  2857. INIT_LIST_HEAD(&devlink->sb_list);
  2858. INIT_LIST_HEAD_RCU(&devlink->dpipe_table_list);
  2859. INIT_LIST_HEAD(&devlink->resource_list);
  2860. INIT_LIST_HEAD(&devlink->param_list);
  2861. mutex_init(&devlink->lock);
  2862. return devlink;
  2863. }
  2864. EXPORT_SYMBOL_GPL(devlink_alloc);
  2865. /**
  2866. * devlink_register - Register devlink instance
  2867. *
  2868. * @devlink: devlink
  2869. */
  2870. int devlink_register(struct devlink *devlink, struct device *dev)
  2871. {
  2872. mutex_lock(&devlink_mutex);
  2873. devlink->dev = dev;
  2874. list_add_tail(&devlink->list, &devlink_list);
  2875. devlink_notify(devlink, DEVLINK_CMD_NEW);
  2876. mutex_unlock(&devlink_mutex);
  2877. return 0;
  2878. }
  2879. EXPORT_SYMBOL_GPL(devlink_register);
  2880. /**
  2881. * devlink_unregister - Unregister devlink instance
  2882. *
  2883. * @devlink: devlink
  2884. */
  2885. void devlink_unregister(struct devlink *devlink)
  2886. {
  2887. mutex_lock(&devlink_mutex);
  2888. devlink_notify(devlink, DEVLINK_CMD_DEL);
  2889. list_del(&devlink->list);
  2890. mutex_unlock(&devlink_mutex);
  2891. }
  2892. EXPORT_SYMBOL_GPL(devlink_unregister);
  2893. /**
  2894. * devlink_free - Free devlink instance resources
  2895. *
  2896. * @devlink: devlink
  2897. */
  2898. void devlink_free(struct devlink *devlink)
  2899. {
  2900. kfree(devlink);
  2901. }
  2902. EXPORT_SYMBOL_GPL(devlink_free);
  2903. /**
  2904. * devlink_port_register - Register devlink port
  2905. *
  2906. * @devlink: devlink
  2907. * @devlink_port: devlink port
  2908. * @port_index
  2909. *
  2910. * Register devlink port with provided port index. User can use
  2911. * any indexing, even hw-related one. devlink_port structure
  2912. * is convenient to be embedded inside user driver private structure.
  2913. * Note that the caller should take care of zeroing the devlink_port
  2914. * structure.
  2915. */
  2916. int devlink_port_register(struct devlink *devlink,
  2917. struct devlink_port *devlink_port,
  2918. unsigned int port_index)
  2919. {
  2920. mutex_lock(&devlink->lock);
  2921. if (devlink_port_index_exists(devlink, port_index)) {
  2922. mutex_unlock(&devlink->lock);
  2923. return -EEXIST;
  2924. }
  2925. devlink_port->devlink = devlink;
  2926. devlink_port->index = port_index;
  2927. devlink_port->registered = true;
  2928. list_add_tail(&devlink_port->list, &devlink->port_list);
  2929. mutex_unlock(&devlink->lock);
  2930. devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
  2931. return 0;
  2932. }
  2933. EXPORT_SYMBOL_GPL(devlink_port_register);
  2934. /**
  2935. * devlink_port_unregister - Unregister devlink port
  2936. *
  2937. * @devlink_port: devlink port
  2938. */
  2939. void devlink_port_unregister(struct devlink_port *devlink_port)
  2940. {
  2941. struct devlink *devlink = devlink_port->devlink;
  2942. devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL);
  2943. mutex_lock(&devlink->lock);
  2944. list_del(&devlink_port->list);
  2945. mutex_unlock(&devlink->lock);
  2946. }
  2947. EXPORT_SYMBOL_GPL(devlink_port_unregister);
  2948. static void __devlink_port_type_set(struct devlink_port *devlink_port,
  2949. enum devlink_port_type type,
  2950. void *type_dev)
  2951. {
  2952. devlink_port->type = type;
  2953. devlink_port->type_dev = type_dev;
  2954. devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
  2955. }
  2956. /**
  2957. * devlink_port_type_eth_set - Set port type to Ethernet
  2958. *
  2959. * @devlink_port: devlink port
  2960. * @netdev: related netdevice
  2961. */
  2962. void devlink_port_type_eth_set(struct devlink_port *devlink_port,
  2963. struct net_device *netdev)
  2964. {
  2965. return __devlink_port_type_set(devlink_port,
  2966. DEVLINK_PORT_TYPE_ETH, netdev);
  2967. }
  2968. EXPORT_SYMBOL_GPL(devlink_port_type_eth_set);
  2969. /**
  2970. * devlink_port_type_ib_set - Set port type to InfiniBand
  2971. *
  2972. * @devlink_port: devlink port
  2973. * @ibdev: related IB device
  2974. */
  2975. void devlink_port_type_ib_set(struct devlink_port *devlink_port,
  2976. struct ib_device *ibdev)
  2977. {
  2978. return __devlink_port_type_set(devlink_port,
  2979. DEVLINK_PORT_TYPE_IB, ibdev);
  2980. }
  2981. EXPORT_SYMBOL_GPL(devlink_port_type_ib_set);
  2982. /**
  2983. * devlink_port_type_clear - Clear port type
  2984. *
  2985. * @devlink_port: devlink port
  2986. */
  2987. void devlink_port_type_clear(struct devlink_port *devlink_port)
  2988. {
  2989. return __devlink_port_type_set(devlink_port,
  2990. DEVLINK_PORT_TYPE_NOTSET, NULL);
  2991. }
  2992. EXPORT_SYMBOL_GPL(devlink_port_type_clear);
  2993. /**
  2994. * devlink_port_attrs_set - Set port attributes
  2995. *
  2996. * @devlink_port: devlink port
  2997. * @flavour: flavour of the port
  2998. * @port_number: number of the port that is facing user, for example
  2999. * the front panel port number
  3000. * @split: indicates if this is split port
  3001. * @split_subport_number: if the port is split, this is the number
  3002. * of subport.
  3003. */
  3004. void devlink_port_attrs_set(struct devlink_port *devlink_port,
  3005. enum devlink_port_flavour flavour,
  3006. u32 port_number, bool split,
  3007. u32 split_subport_number)
  3008. {
  3009. struct devlink_port_attrs *attrs = &devlink_port->attrs;
  3010. attrs->set = true;
  3011. attrs->flavour = flavour;
  3012. attrs->port_number = port_number;
  3013. attrs->split = split;
  3014. attrs->split_subport_number = split_subport_number;
  3015. devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
  3016. }
  3017. EXPORT_SYMBOL_GPL(devlink_port_attrs_set);
  3018. int devlink_port_get_phys_port_name(struct devlink_port *devlink_port,
  3019. char *name, size_t len)
  3020. {
  3021. struct devlink_port_attrs *attrs = &devlink_port->attrs;
  3022. int n = 0;
  3023. if (!attrs->set)
  3024. return -EOPNOTSUPP;
  3025. switch (attrs->flavour) {
  3026. case DEVLINK_PORT_FLAVOUR_PHYSICAL:
  3027. if (!attrs->split)
  3028. n = snprintf(name, len, "p%u", attrs->port_number);
  3029. else
  3030. n = snprintf(name, len, "p%us%u", attrs->port_number,
  3031. attrs->split_subport_number);
  3032. break;
  3033. case DEVLINK_PORT_FLAVOUR_CPU:
  3034. case DEVLINK_PORT_FLAVOUR_DSA:
  3035. /* As CPU and DSA ports do not have a netdevice associated
  3036. * case should not ever happen.
  3037. */
  3038. WARN_ON(1);
  3039. return -EINVAL;
  3040. }
  3041. if (n >= len)
  3042. return -EINVAL;
  3043. return 0;
  3044. }
  3045. EXPORT_SYMBOL_GPL(devlink_port_get_phys_port_name);
  3046. int devlink_sb_register(struct devlink *devlink, unsigned int sb_index,
  3047. u32 size, u16 ingress_pools_count,
  3048. u16 egress_pools_count, u16 ingress_tc_count,
  3049. u16 egress_tc_count)
  3050. {
  3051. struct devlink_sb *devlink_sb;
  3052. int err = 0;
  3053. mutex_lock(&devlink->lock);
  3054. if (devlink_sb_index_exists(devlink, sb_index)) {
  3055. err = -EEXIST;
  3056. goto unlock;
  3057. }
  3058. devlink_sb = kzalloc(sizeof(*devlink_sb), GFP_KERNEL);
  3059. if (!devlink_sb) {
  3060. err = -ENOMEM;
  3061. goto unlock;
  3062. }
  3063. devlink_sb->index = sb_index;
  3064. devlink_sb->size = size;
  3065. devlink_sb->ingress_pools_count = ingress_pools_count;
  3066. devlink_sb->egress_pools_count = egress_pools_count;
  3067. devlink_sb->ingress_tc_count = ingress_tc_count;
  3068. devlink_sb->egress_tc_count = egress_tc_count;
  3069. list_add_tail(&devlink_sb->list, &devlink->sb_list);
  3070. unlock:
  3071. mutex_unlock(&devlink->lock);
  3072. return err;
  3073. }
  3074. EXPORT_SYMBOL_GPL(devlink_sb_register);
  3075. void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index)
  3076. {
  3077. struct devlink_sb *devlink_sb;
  3078. mutex_lock(&devlink->lock);
  3079. devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
  3080. WARN_ON(!devlink_sb);
  3081. list_del(&devlink_sb->list);
  3082. mutex_unlock(&devlink->lock);
  3083. kfree(devlink_sb);
  3084. }
  3085. EXPORT_SYMBOL_GPL(devlink_sb_unregister);
  3086. /**
  3087. * devlink_dpipe_headers_register - register dpipe headers
  3088. *
  3089. * @devlink: devlink
  3090. * @dpipe_headers: dpipe header array
  3091. *
  3092. * Register the headers supported by hardware.
  3093. */
  3094. int devlink_dpipe_headers_register(struct devlink *devlink,
  3095. struct devlink_dpipe_headers *dpipe_headers)
  3096. {
  3097. mutex_lock(&devlink->lock);
  3098. devlink->dpipe_headers = dpipe_headers;
  3099. mutex_unlock(&devlink->lock);
  3100. return 0;
  3101. }
  3102. EXPORT_SYMBOL_GPL(devlink_dpipe_headers_register);
  3103. /**
  3104. * devlink_dpipe_headers_unregister - unregister dpipe headers
  3105. *
  3106. * @devlink: devlink
  3107. *
  3108. * Unregister the headers supported by hardware.
  3109. */
  3110. void devlink_dpipe_headers_unregister(struct devlink *devlink)
  3111. {
  3112. mutex_lock(&devlink->lock);
  3113. devlink->dpipe_headers = NULL;
  3114. mutex_unlock(&devlink->lock);
  3115. }
  3116. EXPORT_SYMBOL_GPL(devlink_dpipe_headers_unregister);
  3117. /**
  3118. * devlink_dpipe_table_counter_enabled - check if counter allocation
  3119. * required
  3120. * @devlink: devlink
  3121. * @table_name: tables name
  3122. *
  3123. * Used by driver to check if counter allocation is required.
  3124. * After counter allocation is turned on the table entries
  3125. * are updated to include counter statistics.
  3126. *
  3127. * After that point on the driver must respect the counter
  3128. * state so that each entry added to the table is added
  3129. * with a counter.
  3130. */
  3131. bool devlink_dpipe_table_counter_enabled(struct devlink *devlink,
  3132. const char *table_name)
  3133. {
  3134. struct devlink_dpipe_table *table;
  3135. bool enabled;
  3136. rcu_read_lock();
  3137. table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
  3138. table_name);
  3139. enabled = false;
  3140. if (table)
  3141. enabled = table->counters_enabled;
  3142. rcu_read_unlock();
  3143. return enabled;
  3144. }
  3145. EXPORT_SYMBOL_GPL(devlink_dpipe_table_counter_enabled);
  3146. /**
  3147. * devlink_dpipe_table_register - register dpipe table
  3148. *
  3149. * @devlink: devlink
  3150. * @table_name: table name
  3151. * @table_ops: table ops
  3152. * @priv: priv
  3153. * @counter_control_extern: external control for counters
  3154. */
  3155. int devlink_dpipe_table_register(struct devlink *devlink,
  3156. const char *table_name,
  3157. struct devlink_dpipe_table_ops *table_ops,
  3158. void *priv, bool counter_control_extern)
  3159. {
  3160. struct devlink_dpipe_table *table;
  3161. if (devlink_dpipe_table_find(&devlink->dpipe_table_list, table_name))
  3162. return -EEXIST;
  3163. if (WARN_ON(!table_ops->size_get))
  3164. return -EINVAL;
  3165. table = kzalloc(sizeof(*table), GFP_KERNEL);
  3166. if (!table)
  3167. return -ENOMEM;
  3168. table->name = table_name;
  3169. table->table_ops = table_ops;
  3170. table->priv = priv;
  3171. table->counter_control_extern = counter_control_extern;
  3172. mutex_lock(&devlink->lock);
  3173. list_add_tail_rcu(&table->list, &devlink->dpipe_table_list);
  3174. mutex_unlock(&devlink->lock);
  3175. return 0;
  3176. }
  3177. EXPORT_SYMBOL_GPL(devlink_dpipe_table_register);
  3178. /**
  3179. * devlink_dpipe_table_unregister - unregister dpipe table
  3180. *
  3181. * @devlink: devlink
  3182. * @table_name: table name
  3183. */
  3184. void devlink_dpipe_table_unregister(struct devlink *devlink,
  3185. const char *table_name)
  3186. {
  3187. struct devlink_dpipe_table *table;
  3188. mutex_lock(&devlink->lock);
  3189. table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
  3190. table_name);
  3191. if (!table)
  3192. goto unlock;
  3193. list_del_rcu(&table->list);
  3194. mutex_unlock(&devlink->lock);
  3195. kfree_rcu(table, rcu);
  3196. return;
  3197. unlock:
  3198. mutex_unlock(&devlink->lock);
  3199. }
  3200. EXPORT_SYMBOL_GPL(devlink_dpipe_table_unregister);
  3201. /**
  3202. * devlink_resource_register - devlink resource register
  3203. *
  3204. * @devlink: devlink
  3205. * @resource_name: resource's name
  3206. * @top_hierarchy: top hierarchy
  3207. * @reload_required: reload is required for new configuration to
  3208. * apply
  3209. * @resource_size: resource's size
  3210. * @resource_id: resource's id
  3211. * @parent_reosurce_id: resource's parent id
  3212. * @size params: size parameters
  3213. */
  3214. int devlink_resource_register(struct devlink *devlink,
  3215. const char *resource_name,
  3216. u64 resource_size,
  3217. u64 resource_id,
  3218. u64 parent_resource_id,
  3219. const struct devlink_resource_size_params *size_params)
  3220. {
  3221. struct devlink_resource *resource;
  3222. struct list_head *resource_list;
  3223. bool top_hierarchy;
  3224. int err = 0;
  3225. top_hierarchy = parent_resource_id == DEVLINK_RESOURCE_ID_PARENT_TOP;
  3226. mutex_lock(&devlink->lock);
  3227. resource = devlink_resource_find(devlink, NULL, resource_id);
  3228. if (resource) {
  3229. err = -EINVAL;
  3230. goto out;
  3231. }
  3232. resource = kzalloc(sizeof(*resource), GFP_KERNEL);
  3233. if (!resource) {
  3234. err = -ENOMEM;
  3235. goto out;
  3236. }
  3237. if (top_hierarchy) {
  3238. resource_list = &devlink->resource_list;
  3239. } else {
  3240. struct devlink_resource *parent_resource;
  3241. parent_resource = devlink_resource_find(devlink, NULL,
  3242. parent_resource_id);
  3243. if (parent_resource) {
  3244. resource_list = &parent_resource->resource_list;
  3245. resource->parent = parent_resource;
  3246. } else {
  3247. kfree(resource);
  3248. err = -EINVAL;
  3249. goto out;
  3250. }
  3251. }
  3252. resource->name = resource_name;
  3253. resource->size = resource_size;
  3254. resource->size_new = resource_size;
  3255. resource->id = resource_id;
  3256. resource->size_valid = true;
  3257. memcpy(&resource->size_params, size_params,
  3258. sizeof(resource->size_params));
  3259. INIT_LIST_HEAD(&resource->resource_list);
  3260. list_add_tail(&resource->list, resource_list);
  3261. out:
  3262. mutex_unlock(&devlink->lock);
  3263. return err;
  3264. }
  3265. EXPORT_SYMBOL_GPL(devlink_resource_register);
  3266. /**
  3267. * devlink_resources_unregister - free all resources
  3268. *
  3269. * @devlink: devlink
  3270. * @resource: resource
  3271. */
  3272. void devlink_resources_unregister(struct devlink *devlink,
  3273. struct devlink_resource *resource)
  3274. {
  3275. struct devlink_resource *tmp, *child_resource;
  3276. struct list_head *resource_list;
  3277. if (resource)
  3278. resource_list = &resource->resource_list;
  3279. else
  3280. resource_list = &devlink->resource_list;
  3281. if (!resource)
  3282. mutex_lock(&devlink->lock);
  3283. list_for_each_entry_safe(child_resource, tmp, resource_list, list) {
  3284. devlink_resources_unregister(devlink, child_resource);
  3285. list_del(&child_resource->list);
  3286. kfree(child_resource);
  3287. }
  3288. if (!resource)
  3289. mutex_unlock(&devlink->lock);
  3290. }
  3291. EXPORT_SYMBOL_GPL(devlink_resources_unregister);
  3292. /**
  3293. * devlink_resource_size_get - get and update size
  3294. *
  3295. * @devlink: devlink
  3296. * @resource_id: the requested resource id
  3297. * @p_resource_size: ptr to update
  3298. */
  3299. int devlink_resource_size_get(struct devlink *devlink,
  3300. u64 resource_id,
  3301. u64 *p_resource_size)
  3302. {
  3303. struct devlink_resource *resource;
  3304. int err = 0;
  3305. mutex_lock(&devlink->lock);
  3306. resource = devlink_resource_find(devlink, NULL, resource_id);
  3307. if (!resource) {
  3308. err = -EINVAL;
  3309. goto out;
  3310. }
  3311. *p_resource_size = resource->size_new;
  3312. resource->size = resource->size_new;
  3313. out:
  3314. mutex_unlock(&devlink->lock);
  3315. return err;
  3316. }
  3317. EXPORT_SYMBOL_GPL(devlink_resource_size_get);
  3318. /**
  3319. * devlink_dpipe_table_resource_set - set the resource id
  3320. *
  3321. * @devlink: devlink
  3322. * @table_name: table name
  3323. * @resource_id: resource id
  3324. * @resource_units: number of resource's units consumed per table's entry
  3325. */
  3326. int devlink_dpipe_table_resource_set(struct devlink *devlink,
  3327. const char *table_name, u64 resource_id,
  3328. u64 resource_units)
  3329. {
  3330. struct devlink_dpipe_table *table;
  3331. int err = 0;
  3332. mutex_lock(&devlink->lock);
  3333. table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
  3334. table_name);
  3335. if (!table) {
  3336. err = -EINVAL;
  3337. goto out;
  3338. }
  3339. table->resource_id = resource_id;
  3340. table->resource_units = resource_units;
  3341. table->resource_valid = true;
  3342. out:
  3343. mutex_unlock(&devlink->lock);
  3344. return err;
  3345. }
  3346. EXPORT_SYMBOL_GPL(devlink_dpipe_table_resource_set);
  3347. /**
  3348. * devlink_resource_occ_get_register - register occupancy getter
  3349. *
  3350. * @devlink: devlink
  3351. * @resource_id: resource id
  3352. * @occ_get: occupancy getter callback
  3353. * @occ_get_priv: occupancy getter callback priv
  3354. */
  3355. void devlink_resource_occ_get_register(struct devlink *devlink,
  3356. u64 resource_id,
  3357. devlink_resource_occ_get_t *occ_get,
  3358. void *occ_get_priv)
  3359. {
  3360. struct devlink_resource *resource;
  3361. mutex_lock(&devlink->lock);
  3362. resource = devlink_resource_find(devlink, NULL, resource_id);
  3363. if (WARN_ON(!resource))
  3364. goto out;
  3365. WARN_ON(resource->occ_get);
  3366. resource->occ_get = occ_get;
  3367. resource->occ_get_priv = occ_get_priv;
  3368. out:
  3369. mutex_unlock(&devlink->lock);
  3370. }
  3371. EXPORT_SYMBOL_GPL(devlink_resource_occ_get_register);
  3372. /**
  3373. * devlink_resource_occ_get_unregister - unregister occupancy getter
  3374. *
  3375. * @devlink: devlink
  3376. * @resource_id: resource id
  3377. */
  3378. void devlink_resource_occ_get_unregister(struct devlink *devlink,
  3379. u64 resource_id)
  3380. {
  3381. struct devlink_resource *resource;
  3382. mutex_lock(&devlink->lock);
  3383. resource = devlink_resource_find(devlink, NULL, resource_id);
  3384. if (WARN_ON(!resource))
  3385. goto out;
  3386. WARN_ON(!resource->occ_get);
  3387. resource->occ_get = NULL;
  3388. resource->occ_get_priv = NULL;
  3389. out:
  3390. mutex_unlock(&devlink->lock);
  3391. }
  3392. EXPORT_SYMBOL_GPL(devlink_resource_occ_get_unregister);
  3393. /**
  3394. * devlink_params_register - register configuration parameters
  3395. *
  3396. * @devlink: devlink
  3397. * @params: configuration parameters array
  3398. * @params_count: number of parameters provided
  3399. *
  3400. * Register the configuration parameters supported by the driver.
  3401. */
  3402. int devlink_params_register(struct devlink *devlink,
  3403. const struct devlink_param *params,
  3404. size_t params_count)
  3405. {
  3406. const struct devlink_param *param = params;
  3407. int i;
  3408. int err;
  3409. mutex_lock(&devlink->lock);
  3410. for (i = 0; i < params_count; i++, param++) {
  3411. if (!param || !param->name || !param->supported_cmodes) {
  3412. err = -EINVAL;
  3413. goto rollback;
  3414. }
  3415. if (param->generic) {
  3416. err = devlink_param_generic_verify(param);
  3417. if (err)
  3418. goto rollback;
  3419. } else {
  3420. err = devlink_param_driver_verify(param);
  3421. if (err)
  3422. goto rollback;
  3423. }
  3424. err = devlink_param_register_one(devlink, param);
  3425. if (err)
  3426. goto rollback;
  3427. }
  3428. mutex_unlock(&devlink->lock);
  3429. return 0;
  3430. rollback:
  3431. if (!i)
  3432. goto unlock;
  3433. for (param--; i > 0; i--, param--)
  3434. devlink_param_unregister_one(devlink, param);
  3435. unlock:
  3436. mutex_unlock(&devlink->lock);
  3437. return err;
  3438. }
  3439. EXPORT_SYMBOL_GPL(devlink_params_register);
  3440. /**
  3441. * devlink_params_unregister - unregister configuration parameters
  3442. * @devlink: devlink
  3443. * @params: configuration parameters to unregister
  3444. * @params_count: number of parameters provided
  3445. */
  3446. void devlink_params_unregister(struct devlink *devlink,
  3447. const struct devlink_param *params,
  3448. size_t params_count)
  3449. {
  3450. const struct devlink_param *param = params;
  3451. int i;
  3452. mutex_lock(&devlink->lock);
  3453. for (i = 0; i < params_count; i++, param++)
  3454. devlink_param_unregister_one(devlink, param);
  3455. mutex_unlock(&devlink->lock);
  3456. }
  3457. EXPORT_SYMBOL_GPL(devlink_params_unregister);
  3458. static int __init devlink_module_init(void)
  3459. {
  3460. return genl_register_family(&devlink_nl_family);
  3461. }
  3462. static void __exit devlink_module_exit(void)
  3463. {
  3464. genl_unregister_family(&devlink_nl_family);
  3465. }
  3466. module_init(devlink_module_init);
  3467. module_exit(devlink_module_exit);
  3468. MODULE_LICENSE("GPL v2");
  3469. MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
  3470. MODULE_DESCRIPTION("Network physical device Netlink interface");
  3471. MODULE_ALIAS_GENL_FAMILY(DEVLINK_GENL_NAME);