devlink.c 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310
  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_fill(struct sk_buff *msg, struct devlink *devlink,
  385. struct devlink_port *devlink_port,
  386. enum devlink_command cmd, u32 portid,
  387. u32 seq, int flags)
  388. {
  389. void *hdr;
  390. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  391. if (!hdr)
  392. return -EMSGSIZE;
  393. if (devlink_nl_put_handle(msg, devlink))
  394. goto nla_put_failure;
  395. if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
  396. goto nla_put_failure;
  397. if (nla_put_u16(msg, DEVLINK_ATTR_PORT_TYPE, devlink_port->type))
  398. goto nla_put_failure;
  399. if (devlink_port->desired_type != DEVLINK_PORT_TYPE_NOTSET &&
  400. nla_put_u16(msg, DEVLINK_ATTR_PORT_DESIRED_TYPE,
  401. devlink_port->desired_type))
  402. goto nla_put_failure;
  403. if (devlink_port->type == DEVLINK_PORT_TYPE_ETH) {
  404. struct net_device *netdev = devlink_port->type_dev;
  405. if (netdev &&
  406. (nla_put_u32(msg, DEVLINK_ATTR_PORT_NETDEV_IFINDEX,
  407. netdev->ifindex) ||
  408. nla_put_string(msg, DEVLINK_ATTR_PORT_NETDEV_NAME,
  409. netdev->name)))
  410. goto nla_put_failure;
  411. }
  412. if (devlink_port->type == DEVLINK_PORT_TYPE_IB) {
  413. struct ib_device *ibdev = devlink_port->type_dev;
  414. if (ibdev &&
  415. nla_put_string(msg, DEVLINK_ATTR_PORT_IBDEV_NAME,
  416. ibdev->name))
  417. goto nla_put_failure;
  418. }
  419. if (devlink_port->split &&
  420. nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_GROUP,
  421. devlink_port->split_group))
  422. goto nla_put_failure;
  423. genlmsg_end(msg, hdr);
  424. return 0;
  425. nla_put_failure:
  426. genlmsg_cancel(msg, hdr);
  427. return -EMSGSIZE;
  428. }
  429. static void devlink_port_notify(struct devlink_port *devlink_port,
  430. enum devlink_command cmd)
  431. {
  432. struct devlink *devlink = devlink_port->devlink;
  433. struct sk_buff *msg;
  434. int err;
  435. if (!devlink_port->registered)
  436. return;
  437. WARN_ON(cmd != DEVLINK_CMD_PORT_NEW && cmd != DEVLINK_CMD_PORT_DEL);
  438. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  439. if (!msg)
  440. return;
  441. err = devlink_nl_port_fill(msg, devlink, devlink_port, cmd, 0, 0, 0);
  442. if (err) {
  443. nlmsg_free(msg);
  444. return;
  445. }
  446. genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
  447. msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
  448. }
  449. static int devlink_nl_cmd_get_doit(struct sk_buff *skb, struct genl_info *info)
  450. {
  451. struct devlink *devlink = info->user_ptr[0];
  452. struct sk_buff *msg;
  453. int err;
  454. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  455. if (!msg)
  456. return -ENOMEM;
  457. err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
  458. info->snd_portid, info->snd_seq, 0);
  459. if (err) {
  460. nlmsg_free(msg);
  461. return err;
  462. }
  463. return genlmsg_reply(msg, info);
  464. }
  465. static int devlink_nl_cmd_get_dumpit(struct sk_buff *msg,
  466. struct netlink_callback *cb)
  467. {
  468. struct devlink *devlink;
  469. int start = cb->args[0];
  470. int idx = 0;
  471. int err;
  472. mutex_lock(&devlink_mutex);
  473. list_for_each_entry(devlink, &devlink_list, list) {
  474. if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
  475. continue;
  476. if (idx < start) {
  477. idx++;
  478. continue;
  479. }
  480. err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
  481. NETLINK_CB(cb->skb).portid,
  482. cb->nlh->nlmsg_seq, NLM_F_MULTI);
  483. if (err)
  484. goto out;
  485. idx++;
  486. }
  487. out:
  488. mutex_unlock(&devlink_mutex);
  489. cb->args[0] = idx;
  490. return msg->len;
  491. }
  492. static int devlink_nl_cmd_port_get_doit(struct sk_buff *skb,
  493. struct genl_info *info)
  494. {
  495. struct devlink_port *devlink_port = info->user_ptr[0];
  496. struct devlink *devlink = devlink_port->devlink;
  497. struct sk_buff *msg;
  498. int err;
  499. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  500. if (!msg)
  501. return -ENOMEM;
  502. err = devlink_nl_port_fill(msg, devlink, devlink_port,
  503. DEVLINK_CMD_PORT_NEW,
  504. info->snd_portid, info->snd_seq, 0);
  505. if (err) {
  506. nlmsg_free(msg);
  507. return err;
  508. }
  509. return genlmsg_reply(msg, info);
  510. }
  511. static int devlink_nl_cmd_port_get_dumpit(struct sk_buff *msg,
  512. struct netlink_callback *cb)
  513. {
  514. struct devlink *devlink;
  515. struct devlink_port *devlink_port;
  516. int start = cb->args[0];
  517. int idx = 0;
  518. int err;
  519. mutex_lock(&devlink_mutex);
  520. list_for_each_entry(devlink, &devlink_list, list) {
  521. if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
  522. continue;
  523. mutex_lock(&devlink->lock);
  524. list_for_each_entry(devlink_port, &devlink->port_list, list) {
  525. if (idx < start) {
  526. idx++;
  527. continue;
  528. }
  529. err = devlink_nl_port_fill(msg, devlink, devlink_port,
  530. DEVLINK_CMD_NEW,
  531. NETLINK_CB(cb->skb).portid,
  532. cb->nlh->nlmsg_seq,
  533. NLM_F_MULTI);
  534. if (err) {
  535. mutex_unlock(&devlink->lock);
  536. goto out;
  537. }
  538. idx++;
  539. }
  540. mutex_unlock(&devlink->lock);
  541. }
  542. out:
  543. mutex_unlock(&devlink_mutex);
  544. cb->args[0] = idx;
  545. return msg->len;
  546. }
  547. static int devlink_port_type_set(struct devlink *devlink,
  548. struct devlink_port *devlink_port,
  549. enum devlink_port_type port_type)
  550. {
  551. int err;
  552. if (devlink->ops && devlink->ops->port_type_set) {
  553. if (port_type == DEVLINK_PORT_TYPE_NOTSET)
  554. return -EINVAL;
  555. if (port_type == devlink_port->type)
  556. return 0;
  557. err = devlink->ops->port_type_set(devlink_port, port_type);
  558. if (err)
  559. return err;
  560. devlink_port->desired_type = port_type;
  561. devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
  562. return 0;
  563. }
  564. return -EOPNOTSUPP;
  565. }
  566. static int devlink_nl_cmd_port_set_doit(struct sk_buff *skb,
  567. struct genl_info *info)
  568. {
  569. struct devlink_port *devlink_port = info->user_ptr[0];
  570. struct devlink *devlink = devlink_port->devlink;
  571. int err;
  572. if (info->attrs[DEVLINK_ATTR_PORT_TYPE]) {
  573. enum devlink_port_type port_type;
  574. port_type = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_TYPE]);
  575. err = devlink_port_type_set(devlink, devlink_port, port_type);
  576. if (err)
  577. return err;
  578. }
  579. return 0;
  580. }
  581. static int devlink_port_split(struct devlink *devlink,
  582. u32 port_index, u32 count)
  583. {
  584. if (devlink->ops && devlink->ops->port_split)
  585. return devlink->ops->port_split(devlink, port_index, count);
  586. return -EOPNOTSUPP;
  587. }
  588. static int devlink_nl_cmd_port_split_doit(struct sk_buff *skb,
  589. struct genl_info *info)
  590. {
  591. struct devlink *devlink = info->user_ptr[0];
  592. u32 port_index;
  593. u32 count;
  594. if (!info->attrs[DEVLINK_ATTR_PORT_INDEX] ||
  595. !info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT])
  596. return -EINVAL;
  597. port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
  598. count = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT]);
  599. return devlink_port_split(devlink, port_index, count);
  600. }
  601. static int devlink_port_unsplit(struct devlink *devlink, u32 port_index)
  602. {
  603. if (devlink->ops && devlink->ops->port_unsplit)
  604. return devlink->ops->port_unsplit(devlink, port_index);
  605. return -EOPNOTSUPP;
  606. }
  607. static int devlink_nl_cmd_port_unsplit_doit(struct sk_buff *skb,
  608. struct genl_info *info)
  609. {
  610. struct devlink *devlink = info->user_ptr[0];
  611. u32 port_index;
  612. if (!info->attrs[DEVLINK_ATTR_PORT_INDEX])
  613. return -EINVAL;
  614. port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
  615. return devlink_port_unsplit(devlink, port_index);
  616. }
  617. static int devlink_nl_sb_fill(struct sk_buff *msg, struct devlink *devlink,
  618. struct devlink_sb *devlink_sb,
  619. enum devlink_command cmd, u32 portid,
  620. u32 seq, int flags)
  621. {
  622. void *hdr;
  623. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  624. if (!hdr)
  625. return -EMSGSIZE;
  626. if (devlink_nl_put_handle(msg, devlink))
  627. goto nla_put_failure;
  628. if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
  629. goto nla_put_failure;
  630. if (nla_put_u32(msg, DEVLINK_ATTR_SB_SIZE, devlink_sb->size))
  631. goto nla_put_failure;
  632. if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_POOL_COUNT,
  633. devlink_sb->ingress_pools_count))
  634. goto nla_put_failure;
  635. if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_POOL_COUNT,
  636. devlink_sb->egress_pools_count))
  637. goto nla_put_failure;
  638. if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_TC_COUNT,
  639. devlink_sb->ingress_tc_count))
  640. goto nla_put_failure;
  641. if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_TC_COUNT,
  642. devlink_sb->egress_tc_count))
  643. goto nla_put_failure;
  644. genlmsg_end(msg, hdr);
  645. return 0;
  646. nla_put_failure:
  647. genlmsg_cancel(msg, hdr);
  648. return -EMSGSIZE;
  649. }
  650. static int devlink_nl_cmd_sb_get_doit(struct sk_buff *skb,
  651. struct genl_info *info)
  652. {
  653. struct devlink *devlink = info->user_ptr[0];
  654. struct devlink_sb *devlink_sb = info->user_ptr[1];
  655. struct sk_buff *msg;
  656. int err;
  657. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  658. if (!msg)
  659. return -ENOMEM;
  660. err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
  661. DEVLINK_CMD_SB_NEW,
  662. info->snd_portid, info->snd_seq, 0);
  663. if (err) {
  664. nlmsg_free(msg);
  665. return err;
  666. }
  667. return genlmsg_reply(msg, info);
  668. }
  669. static int devlink_nl_cmd_sb_get_dumpit(struct sk_buff *msg,
  670. struct netlink_callback *cb)
  671. {
  672. struct devlink *devlink;
  673. struct devlink_sb *devlink_sb;
  674. int start = cb->args[0];
  675. int idx = 0;
  676. int err;
  677. mutex_lock(&devlink_mutex);
  678. list_for_each_entry(devlink, &devlink_list, list) {
  679. if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
  680. continue;
  681. mutex_lock(&devlink->lock);
  682. list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
  683. if (idx < start) {
  684. idx++;
  685. continue;
  686. }
  687. err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
  688. DEVLINK_CMD_SB_NEW,
  689. NETLINK_CB(cb->skb).portid,
  690. cb->nlh->nlmsg_seq,
  691. NLM_F_MULTI);
  692. if (err) {
  693. mutex_unlock(&devlink->lock);
  694. goto out;
  695. }
  696. idx++;
  697. }
  698. mutex_unlock(&devlink->lock);
  699. }
  700. out:
  701. mutex_unlock(&devlink_mutex);
  702. cb->args[0] = idx;
  703. return msg->len;
  704. }
  705. static int devlink_nl_sb_pool_fill(struct sk_buff *msg, struct devlink *devlink,
  706. struct devlink_sb *devlink_sb,
  707. u16 pool_index, enum devlink_command cmd,
  708. u32 portid, u32 seq, int flags)
  709. {
  710. struct devlink_sb_pool_info pool_info;
  711. void *hdr;
  712. int err;
  713. err = devlink->ops->sb_pool_get(devlink, devlink_sb->index,
  714. pool_index, &pool_info);
  715. if (err)
  716. return err;
  717. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  718. if (!hdr)
  719. return -EMSGSIZE;
  720. if (devlink_nl_put_handle(msg, devlink))
  721. goto nla_put_failure;
  722. if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
  723. goto nla_put_failure;
  724. if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
  725. goto nla_put_failure;
  726. if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_info.pool_type))
  727. goto nla_put_failure;
  728. if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_SIZE, pool_info.size))
  729. goto nla_put_failure;
  730. if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE,
  731. pool_info.threshold_type))
  732. goto nla_put_failure;
  733. genlmsg_end(msg, hdr);
  734. return 0;
  735. nla_put_failure:
  736. genlmsg_cancel(msg, hdr);
  737. return -EMSGSIZE;
  738. }
  739. static int devlink_nl_cmd_sb_pool_get_doit(struct sk_buff *skb,
  740. struct genl_info *info)
  741. {
  742. struct devlink *devlink = info->user_ptr[0];
  743. struct devlink_sb *devlink_sb = info->user_ptr[1];
  744. struct sk_buff *msg;
  745. u16 pool_index;
  746. int err;
  747. err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
  748. &pool_index);
  749. if (err)
  750. return err;
  751. if (!devlink->ops || !devlink->ops->sb_pool_get)
  752. return -EOPNOTSUPP;
  753. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  754. if (!msg)
  755. return -ENOMEM;
  756. err = devlink_nl_sb_pool_fill(msg, devlink, devlink_sb, pool_index,
  757. DEVLINK_CMD_SB_POOL_NEW,
  758. info->snd_portid, info->snd_seq, 0);
  759. if (err) {
  760. nlmsg_free(msg);
  761. return err;
  762. }
  763. return genlmsg_reply(msg, info);
  764. }
  765. static int __sb_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
  766. struct devlink *devlink,
  767. struct devlink_sb *devlink_sb,
  768. u32 portid, u32 seq)
  769. {
  770. u16 pool_count = devlink_sb_pool_count(devlink_sb);
  771. u16 pool_index;
  772. int err;
  773. for (pool_index = 0; pool_index < pool_count; pool_index++) {
  774. if (*p_idx < start) {
  775. (*p_idx)++;
  776. continue;
  777. }
  778. err = devlink_nl_sb_pool_fill(msg, devlink,
  779. devlink_sb,
  780. pool_index,
  781. DEVLINK_CMD_SB_POOL_NEW,
  782. portid, seq, NLM_F_MULTI);
  783. if (err)
  784. return err;
  785. (*p_idx)++;
  786. }
  787. return 0;
  788. }
  789. static int devlink_nl_cmd_sb_pool_get_dumpit(struct sk_buff *msg,
  790. struct netlink_callback *cb)
  791. {
  792. struct devlink *devlink;
  793. struct devlink_sb *devlink_sb;
  794. int start = cb->args[0];
  795. int idx = 0;
  796. int err;
  797. mutex_lock(&devlink_mutex);
  798. list_for_each_entry(devlink, &devlink_list, list) {
  799. if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
  800. !devlink->ops || !devlink->ops->sb_pool_get)
  801. continue;
  802. mutex_lock(&devlink->lock);
  803. list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
  804. err = __sb_pool_get_dumpit(msg, start, &idx, devlink,
  805. devlink_sb,
  806. NETLINK_CB(cb->skb).portid,
  807. cb->nlh->nlmsg_seq);
  808. if (err && err != -EOPNOTSUPP) {
  809. mutex_unlock(&devlink->lock);
  810. goto out;
  811. }
  812. }
  813. mutex_unlock(&devlink->lock);
  814. }
  815. out:
  816. mutex_unlock(&devlink_mutex);
  817. cb->args[0] = idx;
  818. return msg->len;
  819. }
  820. static int devlink_sb_pool_set(struct devlink *devlink, unsigned int sb_index,
  821. u16 pool_index, u32 size,
  822. enum devlink_sb_threshold_type threshold_type)
  823. {
  824. const struct devlink_ops *ops = devlink->ops;
  825. if (ops && ops->sb_pool_set)
  826. return ops->sb_pool_set(devlink, sb_index, pool_index,
  827. size, threshold_type);
  828. return -EOPNOTSUPP;
  829. }
  830. static int devlink_nl_cmd_sb_pool_set_doit(struct sk_buff *skb,
  831. struct genl_info *info)
  832. {
  833. struct devlink *devlink = info->user_ptr[0];
  834. struct devlink_sb *devlink_sb = info->user_ptr[1];
  835. enum devlink_sb_threshold_type threshold_type;
  836. u16 pool_index;
  837. u32 size;
  838. int err;
  839. err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
  840. &pool_index);
  841. if (err)
  842. return err;
  843. err = devlink_sb_th_type_get_from_info(info, &threshold_type);
  844. if (err)
  845. return err;
  846. if (!info->attrs[DEVLINK_ATTR_SB_POOL_SIZE])
  847. return -EINVAL;
  848. size = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_POOL_SIZE]);
  849. return devlink_sb_pool_set(devlink, devlink_sb->index,
  850. pool_index, size, threshold_type);
  851. }
  852. static int devlink_nl_sb_port_pool_fill(struct sk_buff *msg,
  853. struct devlink *devlink,
  854. struct devlink_port *devlink_port,
  855. struct devlink_sb *devlink_sb,
  856. u16 pool_index,
  857. enum devlink_command cmd,
  858. u32 portid, u32 seq, int flags)
  859. {
  860. const struct devlink_ops *ops = devlink->ops;
  861. u32 threshold;
  862. void *hdr;
  863. int err;
  864. err = ops->sb_port_pool_get(devlink_port, devlink_sb->index,
  865. pool_index, &threshold);
  866. if (err)
  867. return err;
  868. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  869. if (!hdr)
  870. return -EMSGSIZE;
  871. if (devlink_nl_put_handle(msg, devlink))
  872. goto nla_put_failure;
  873. if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
  874. goto nla_put_failure;
  875. if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
  876. goto nla_put_failure;
  877. if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
  878. goto nla_put_failure;
  879. if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
  880. goto nla_put_failure;
  881. if (ops->sb_occ_port_pool_get) {
  882. u32 cur;
  883. u32 max;
  884. err = ops->sb_occ_port_pool_get(devlink_port, devlink_sb->index,
  885. pool_index, &cur, &max);
  886. if (err && err != -EOPNOTSUPP)
  887. return err;
  888. if (!err) {
  889. if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
  890. goto nla_put_failure;
  891. if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
  892. goto nla_put_failure;
  893. }
  894. }
  895. genlmsg_end(msg, hdr);
  896. return 0;
  897. nla_put_failure:
  898. genlmsg_cancel(msg, hdr);
  899. return -EMSGSIZE;
  900. }
  901. static int devlink_nl_cmd_sb_port_pool_get_doit(struct sk_buff *skb,
  902. struct genl_info *info)
  903. {
  904. struct devlink_port *devlink_port = info->user_ptr[0];
  905. struct devlink *devlink = devlink_port->devlink;
  906. struct devlink_sb *devlink_sb = info->user_ptr[1];
  907. struct sk_buff *msg;
  908. u16 pool_index;
  909. int err;
  910. err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
  911. &pool_index);
  912. if (err)
  913. return err;
  914. if (!devlink->ops || !devlink->ops->sb_port_pool_get)
  915. return -EOPNOTSUPP;
  916. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  917. if (!msg)
  918. return -ENOMEM;
  919. err = devlink_nl_sb_port_pool_fill(msg, devlink, devlink_port,
  920. devlink_sb, pool_index,
  921. DEVLINK_CMD_SB_PORT_POOL_NEW,
  922. info->snd_portid, info->snd_seq, 0);
  923. if (err) {
  924. nlmsg_free(msg);
  925. return err;
  926. }
  927. return genlmsg_reply(msg, info);
  928. }
  929. static int __sb_port_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
  930. struct devlink *devlink,
  931. struct devlink_sb *devlink_sb,
  932. u32 portid, u32 seq)
  933. {
  934. struct devlink_port *devlink_port;
  935. u16 pool_count = devlink_sb_pool_count(devlink_sb);
  936. u16 pool_index;
  937. int err;
  938. list_for_each_entry(devlink_port, &devlink->port_list, list) {
  939. for (pool_index = 0; pool_index < pool_count; pool_index++) {
  940. if (*p_idx < start) {
  941. (*p_idx)++;
  942. continue;
  943. }
  944. err = devlink_nl_sb_port_pool_fill(msg, devlink,
  945. devlink_port,
  946. devlink_sb,
  947. pool_index,
  948. DEVLINK_CMD_SB_PORT_POOL_NEW,
  949. portid, seq,
  950. NLM_F_MULTI);
  951. if (err)
  952. return err;
  953. (*p_idx)++;
  954. }
  955. }
  956. return 0;
  957. }
  958. static int devlink_nl_cmd_sb_port_pool_get_dumpit(struct sk_buff *msg,
  959. struct netlink_callback *cb)
  960. {
  961. struct devlink *devlink;
  962. struct devlink_sb *devlink_sb;
  963. int start = cb->args[0];
  964. int idx = 0;
  965. int err;
  966. mutex_lock(&devlink_mutex);
  967. list_for_each_entry(devlink, &devlink_list, list) {
  968. if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
  969. !devlink->ops || !devlink->ops->sb_port_pool_get)
  970. continue;
  971. mutex_lock(&devlink->lock);
  972. list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
  973. err = __sb_port_pool_get_dumpit(msg, start, &idx,
  974. devlink, devlink_sb,
  975. NETLINK_CB(cb->skb).portid,
  976. cb->nlh->nlmsg_seq);
  977. if (err && err != -EOPNOTSUPP) {
  978. mutex_unlock(&devlink->lock);
  979. goto out;
  980. }
  981. }
  982. mutex_unlock(&devlink->lock);
  983. }
  984. out:
  985. mutex_unlock(&devlink_mutex);
  986. cb->args[0] = idx;
  987. return msg->len;
  988. }
  989. static int devlink_sb_port_pool_set(struct devlink_port *devlink_port,
  990. unsigned int sb_index, u16 pool_index,
  991. u32 threshold)
  992. {
  993. const struct devlink_ops *ops = devlink_port->devlink->ops;
  994. if (ops && ops->sb_port_pool_set)
  995. return ops->sb_port_pool_set(devlink_port, sb_index,
  996. pool_index, threshold);
  997. return -EOPNOTSUPP;
  998. }
  999. static int devlink_nl_cmd_sb_port_pool_set_doit(struct sk_buff *skb,
  1000. struct genl_info *info)
  1001. {
  1002. struct devlink_port *devlink_port = info->user_ptr[0];
  1003. struct devlink_sb *devlink_sb = info->user_ptr[1];
  1004. u16 pool_index;
  1005. u32 threshold;
  1006. int err;
  1007. err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
  1008. &pool_index);
  1009. if (err)
  1010. return err;
  1011. if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
  1012. return -EINVAL;
  1013. threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
  1014. return devlink_sb_port_pool_set(devlink_port, devlink_sb->index,
  1015. pool_index, threshold);
  1016. }
  1017. static int
  1018. devlink_nl_sb_tc_pool_bind_fill(struct sk_buff *msg, struct devlink *devlink,
  1019. struct devlink_port *devlink_port,
  1020. struct devlink_sb *devlink_sb, u16 tc_index,
  1021. enum devlink_sb_pool_type pool_type,
  1022. enum devlink_command cmd,
  1023. u32 portid, u32 seq, int flags)
  1024. {
  1025. const struct devlink_ops *ops = devlink->ops;
  1026. u16 pool_index;
  1027. u32 threshold;
  1028. void *hdr;
  1029. int err;
  1030. err = ops->sb_tc_pool_bind_get(devlink_port, devlink_sb->index,
  1031. tc_index, pool_type,
  1032. &pool_index, &threshold);
  1033. if (err)
  1034. return err;
  1035. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  1036. if (!hdr)
  1037. return -EMSGSIZE;
  1038. if (devlink_nl_put_handle(msg, devlink))
  1039. goto nla_put_failure;
  1040. if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
  1041. goto nla_put_failure;
  1042. if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
  1043. goto nla_put_failure;
  1044. if (nla_put_u16(msg, DEVLINK_ATTR_SB_TC_INDEX, tc_index))
  1045. goto nla_put_failure;
  1046. if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_type))
  1047. goto nla_put_failure;
  1048. if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
  1049. goto nla_put_failure;
  1050. if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
  1051. goto nla_put_failure;
  1052. if (ops->sb_occ_tc_port_bind_get) {
  1053. u32 cur;
  1054. u32 max;
  1055. err = ops->sb_occ_tc_port_bind_get(devlink_port,
  1056. devlink_sb->index,
  1057. tc_index, pool_type,
  1058. &cur, &max);
  1059. if (err && err != -EOPNOTSUPP)
  1060. return err;
  1061. if (!err) {
  1062. if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
  1063. goto nla_put_failure;
  1064. if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
  1065. goto nla_put_failure;
  1066. }
  1067. }
  1068. genlmsg_end(msg, hdr);
  1069. return 0;
  1070. nla_put_failure:
  1071. genlmsg_cancel(msg, hdr);
  1072. return -EMSGSIZE;
  1073. }
  1074. static int devlink_nl_cmd_sb_tc_pool_bind_get_doit(struct sk_buff *skb,
  1075. struct genl_info *info)
  1076. {
  1077. struct devlink_port *devlink_port = info->user_ptr[0];
  1078. struct devlink *devlink = devlink_port->devlink;
  1079. struct devlink_sb *devlink_sb = info->user_ptr[1];
  1080. struct sk_buff *msg;
  1081. enum devlink_sb_pool_type pool_type;
  1082. u16 tc_index;
  1083. int err;
  1084. err = devlink_sb_pool_type_get_from_info(info, &pool_type);
  1085. if (err)
  1086. return err;
  1087. err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
  1088. pool_type, &tc_index);
  1089. if (err)
  1090. return err;
  1091. if (!devlink->ops || !devlink->ops->sb_tc_pool_bind_get)
  1092. return -EOPNOTSUPP;
  1093. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1094. if (!msg)
  1095. return -ENOMEM;
  1096. err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink, devlink_port,
  1097. devlink_sb, tc_index, pool_type,
  1098. DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
  1099. info->snd_portid,
  1100. info->snd_seq, 0);
  1101. if (err) {
  1102. nlmsg_free(msg);
  1103. return err;
  1104. }
  1105. return genlmsg_reply(msg, info);
  1106. }
  1107. static int __sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
  1108. int start, int *p_idx,
  1109. struct devlink *devlink,
  1110. struct devlink_sb *devlink_sb,
  1111. u32 portid, u32 seq)
  1112. {
  1113. struct devlink_port *devlink_port;
  1114. u16 tc_index;
  1115. int err;
  1116. list_for_each_entry(devlink_port, &devlink->port_list, list) {
  1117. for (tc_index = 0;
  1118. tc_index < devlink_sb->ingress_tc_count; tc_index++) {
  1119. if (*p_idx < start) {
  1120. (*p_idx)++;
  1121. continue;
  1122. }
  1123. err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
  1124. devlink_port,
  1125. devlink_sb,
  1126. tc_index,
  1127. DEVLINK_SB_POOL_TYPE_INGRESS,
  1128. DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
  1129. portid, seq,
  1130. NLM_F_MULTI);
  1131. if (err)
  1132. return err;
  1133. (*p_idx)++;
  1134. }
  1135. for (tc_index = 0;
  1136. tc_index < devlink_sb->egress_tc_count; tc_index++) {
  1137. if (*p_idx < start) {
  1138. (*p_idx)++;
  1139. continue;
  1140. }
  1141. err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
  1142. devlink_port,
  1143. devlink_sb,
  1144. tc_index,
  1145. DEVLINK_SB_POOL_TYPE_EGRESS,
  1146. DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
  1147. portid, seq,
  1148. NLM_F_MULTI);
  1149. if (err)
  1150. return err;
  1151. (*p_idx)++;
  1152. }
  1153. }
  1154. return 0;
  1155. }
  1156. static int
  1157. devlink_nl_cmd_sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
  1158. struct netlink_callback *cb)
  1159. {
  1160. struct devlink *devlink;
  1161. struct devlink_sb *devlink_sb;
  1162. int start = cb->args[0];
  1163. int idx = 0;
  1164. int err;
  1165. mutex_lock(&devlink_mutex);
  1166. list_for_each_entry(devlink, &devlink_list, list) {
  1167. if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
  1168. !devlink->ops || !devlink->ops->sb_tc_pool_bind_get)
  1169. continue;
  1170. mutex_lock(&devlink->lock);
  1171. list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
  1172. err = __sb_tc_pool_bind_get_dumpit(msg, start, &idx,
  1173. devlink,
  1174. devlink_sb,
  1175. NETLINK_CB(cb->skb).portid,
  1176. cb->nlh->nlmsg_seq);
  1177. if (err && err != -EOPNOTSUPP) {
  1178. mutex_unlock(&devlink->lock);
  1179. goto out;
  1180. }
  1181. }
  1182. mutex_unlock(&devlink->lock);
  1183. }
  1184. out:
  1185. mutex_unlock(&devlink_mutex);
  1186. cb->args[0] = idx;
  1187. return msg->len;
  1188. }
  1189. static int devlink_sb_tc_pool_bind_set(struct devlink_port *devlink_port,
  1190. unsigned int sb_index, u16 tc_index,
  1191. enum devlink_sb_pool_type pool_type,
  1192. u16 pool_index, u32 threshold)
  1193. {
  1194. const struct devlink_ops *ops = devlink_port->devlink->ops;
  1195. if (ops && ops->sb_tc_pool_bind_set)
  1196. return ops->sb_tc_pool_bind_set(devlink_port, sb_index,
  1197. tc_index, pool_type,
  1198. pool_index, threshold);
  1199. return -EOPNOTSUPP;
  1200. }
  1201. static int devlink_nl_cmd_sb_tc_pool_bind_set_doit(struct sk_buff *skb,
  1202. struct genl_info *info)
  1203. {
  1204. struct devlink_port *devlink_port = info->user_ptr[0];
  1205. struct devlink_sb *devlink_sb = info->user_ptr[1];
  1206. enum devlink_sb_pool_type pool_type;
  1207. u16 tc_index;
  1208. u16 pool_index;
  1209. u32 threshold;
  1210. int err;
  1211. err = devlink_sb_pool_type_get_from_info(info, &pool_type);
  1212. if (err)
  1213. return err;
  1214. err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
  1215. pool_type, &tc_index);
  1216. if (err)
  1217. return err;
  1218. err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
  1219. &pool_index);
  1220. if (err)
  1221. return err;
  1222. if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
  1223. return -EINVAL;
  1224. threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
  1225. return devlink_sb_tc_pool_bind_set(devlink_port, devlink_sb->index,
  1226. tc_index, pool_type,
  1227. pool_index, threshold);
  1228. }
  1229. static int devlink_nl_cmd_sb_occ_snapshot_doit(struct sk_buff *skb,
  1230. struct genl_info *info)
  1231. {
  1232. struct devlink *devlink = info->user_ptr[0];
  1233. struct devlink_sb *devlink_sb = info->user_ptr[1];
  1234. const struct devlink_ops *ops = devlink->ops;
  1235. if (ops && ops->sb_occ_snapshot)
  1236. return ops->sb_occ_snapshot(devlink, devlink_sb->index);
  1237. return -EOPNOTSUPP;
  1238. }
  1239. static int devlink_nl_cmd_sb_occ_max_clear_doit(struct sk_buff *skb,
  1240. struct genl_info *info)
  1241. {
  1242. struct devlink *devlink = info->user_ptr[0];
  1243. struct devlink_sb *devlink_sb = info->user_ptr[1];
  1244. const struct devlink_ops *ops = devlink->ops;
  1245. if (ops && ops->sb_occ_max_clear)
  1246. return ops->sb_occ_max_clear(devlink, devlink_sb->index);
  1247. return -EOPNOTSUPP;
  1248. }
  1249. static int devlink_nl_eswitch_fill(struct sk_buff *msg, struct devlink *devlink,
  1250. enum devlink_command cmd, u32 portid,
  1251. u32 seq, int flags)
  1252. {
  1253. const struct devlink_ops *ops = devlink->ops;
  1254. u8 inline_mode, encap_mode;
  1255. void *hdr;
  1256. int err = 0;
  1257. u16 mode;
  1258. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  1259. if (!hdr)
  1260. return -EMSGSIZE;
  1261. err = devlink_nl_put_handle(msg, devlink);
  1262. if (err)
  1263. goto nla_put_failure;
  1264. if (ops->eswitch_mode_get) {
  1265. err = ops->eswitch_mode_get(devlink, &mode);
  1266. if (err)
  1267. goto nla_put_failure;
  1268. err = nla_put_u16(msg, DEVLINK_ATTR_ESWITCH_MODE, mode);
  1269. if (err)
  1270. goto nla_put_failure;
  1271. }
  1272. if (ops->eswitch_inline_mode_get) {
  1273. err = ops->eswitch_inline_mode_get(devlink, &inline_mode);
  1274. if (err)
  1275. goto nla_put_failure;
  1276. err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_INLINE_MODE,
  1277. inline_mode);
  1278. if (err)
  1279. goto nla_put_failure;
  1280. }
  1281. if (ops->eswitch_encap_mode_get) {
  1282. err = ops->eswitch_encap_mode_get(devlink, &encap_mode);
  1283. if (err)
  1284. goto nla_put_failure;
  1285. err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_ENCAP_MODE, encap_mode);
  1286. if (err)
  1287. goto nla_put_failure;
  1288. }
  1289. genlmsg_end(msg, hdr);
  1290. return 0;
  1291. nla_put_failure:
  1292. genlmsg_cancel(msg, hdr);
  1293. return err;
  1294. }
  1295. static int devlink_nl_cmd_eswitch_get_doit(struct sk_buff *skb,
  1296. struct genl_info *info)
  1297. {
  1298. struct devlink *devlink = info->user_ptr[0];
  1299. const struct devlink_ops *ops = devlink->ops;
  1300. struct sk_buff *msg;
  1301. int err;
  1302. if (!ops)
  1303. return -EOPNOTSUPP;
  1304. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1305. if (!msg)
  1306. return -ENOMEM;
  1307. err = devlink_nl_eswitch_fill(msg, devlink, DEVLINK_CMD_ESWITCH_GET,
  1308. info->snd_portid, info->snd_seq, 0);
  1309. if (err) {
  1310. nlmsg_free(msg);
  1311. return err;
  1312. }
  1313. return genlmsg_reply(msg, info);
  1314. }
  1315. static int devlink_nl_cmd_eswitch_set_doit(struct sk_buff *skb,
  1316. struct genl_info *info)
  1317. {
  1318. struct devlink *devlink = info->user_ptr[0];
  1319. const struct devlink_ops *ops = devlink->ops;
  1320. u8 inline_mode, encap_mode;
  1321. int err = 0;
  1322. u16 mode;
  1323. if (!ops)
  1324. return -EOPNOTSUPP;
  1325. if (info->attrs[DEVLINK_ATTR_ESWITCH_MODE]) {
  1326. if (!ops->eswitch_mode_set)
  1327. return -EOPNOTSUPP;
  1328. mode = nla_get_u16(info->attrs[DEVLINK_ATTR_ESWITCH_MODE]);
  1329. err = ops->eswitch_mode_set(devlink, mode);
  1330. if (err)
  1331. return err;
  1332. }
  1333. if (info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]) {
  1334. if (!ops->eswitch_inline_mode_set)
  1335. return -EOPNOTSUPP;
  1336. inline_mode = nla_get_u8(
  1337. info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]);
  1338. err = ops->eswitch_inline_mode_set(devlink, inline_mode);
  1339. if (err)
  1340. return err;
  1341. }
  1342. if (info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]) {
  1343. if (!ops->eswitch_encap_mode_set)
  1344. return -EOPNOTSUPP;
  1345. encap_mode = nla_get_u8(info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]);
  1346. err = ops->eswitch_encap_mode_set(devlink, encap_mode);
  1347. if (err)
  1348. return err;
  1349. }
  1350. return 0;
  1351. }
  1352. int devlink_dpipe_match_put(struct sk_buff *skb,
  1353. struct devlink_dpipe_match *match)
  1354. {
  1355. struct devlink_dpipe_header *header = match->header;
  1356. struct devlink_dpipe_field *field = &header->fields[match->field_id];
  1357. struct nlattr *match_attr;
  1358. match_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_MATCH);
  1359. if (!match_attr)
  1360. return -EMSGSIZE;
  1361. if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_MATCH_TYPE, match->type) ||
  1362. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, match->header_index) ||
  1363. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
  1364. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
  1365. nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
  1366. goto nla_put_failure;
  1367. nla_nest_end(skb, match_attr);
  1368. return 0;
  1369. nla_put_failure:
  1370. nla_nest_cancel(skb, match_attr);
  1371. return -EMSGSIZE;
  1372. }
  1373. EXPORT_SYMBOL_GPL(devlink_dpipe_match_put);
  1374. static int devlink_dpipe_matches_put(struct devlink_dpipe_table *table,
  1375. struct sk_buff *skb)
  1376. {
  1377. struct nlattr *matches_attr;
  1378. matches_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLE_MATCHES);
  1379. if (!matches_attr)
  1380. return -EMSGSIZE;
  1381. if (table->table_ops->matches_dump(table->priv, skb))
  1382. goto nla_put_failure;
  1383. nla_nest_end(skb, matches_attr);
  1384. return 0;
  1385. nla_put_failure:
  1386. nla_nest_cancel(skb, matches_attr);
  1387. return -EMSGSIZE;
  1388. }
  1389. int devlink_dpipe_action_put(struct sk_buff *skb,
  1390. struct devlink_dpipe_action *action)
  1391. {
  1392. struct devlink_dpipe_header *header = action->header;
  1393. struct devlink_dpipe_field *field = &header->fields[action->field_id];
  1394. struct nlattr *action_attr;
  1395. action_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_ACTION);
  1396. if (!action_attr)
  1397. return -EMSGSIZE;
  1398. if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_ACTION_TYPE, action->type) ||
  1399. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, action->header_index) ||
  1400. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
  1401. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
  1402. nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
  1403. goto nla_put_failure;
  1404. nla_nest_end(skb, action_attr);
  1405. return 0;
  1406. nla_put_failure:
  1407. nla_nest_cancel(skb, action_attr);
  1408. return -EMSGSIZE;
  1409. }
  1410. EXPORT_SYMBOL_GPL(devlink_dpipe_action_put);
  1411. static int devlink_dpipe_actions_put(struct devlink_dpipe_table *table,
  1412. struct sk_buff *skb)
  1413. {
  1414. struct nlattr *actions_attr;
  1415. actions_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLE_ACTIONS);
  1416. if (!actions_attr)
  1417. return -EMSGSIZE;
  1418. if (table->table_ops->actions_dump(table->priv, skb))
  1419. goto nla_put_failure;
  1420. nla_nest_end(skb, actions_attr);
  1421. return 0;
  1422. nla_put_failure:
  1423. nla_nest_cancel(skb, actions_attr);
  1424. return -EMSGSIZE;
  1425. }
  1426. static int devlink_dpipe_table_put(struct sk_buff *skb,
  1427. struct devlink_dpipe_table *table)
  1428. {
  1429. struct nlattr *table_attr;
  1430. u64 table_size;
  1431. table_size = table->table_ops->size_get(table->priv);
  1432. table_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLE);
  1433. if (!table_attr)
  1434. return -EMSGSIZE;
  1435. if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_TABLE_NAME, table->name) ||
  1436. nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_SIZE, table_size,
  1437. DEVLINK_ATTR_PAD))
  1438. goto nla_put_failure;
  1439. if (nla_put_u8(skb, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED,
  1440. table->counters_enabled))
  1441. goto nla_put_failure;
  1442. if (table->resource_valid) {
  1443. if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID,
  1444. table->resource_id, DEVLINK_ATTR_PAD) ||
  1445. nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS,
  1446. table->resource_units, DEVLINK_ATTR_PAD))
  1447. goto nla_put_failure;
  1448. }
  1449. if (devlink_dpipe_matches_put(table, skb))
  1450. goto nla_put_failure;
  1451. if (devlink_dpipe_actions_put(table, skb))
  1452. goto nla_put_failure;
  1453. nla_nest_end(skb, table_attr);
  1454. return 0;
  1455. nla_put_failure:
  1456. nla_nest_cancel(skb, table_attr);
  1457. return -EMSGSIZE;
  1458. }
  1459. static int devlink_dpipe_send_and_alloc_skb(struct sk_buff **pskb,
  1460. struct genl_info *info)
  1461. {
  1462. int err;
  1463. if (*pskb) {
  1464. err = genlmsg_reply(*pskb, info);
  1465. if (err)
  1466. return err;
  1467. }
  1468. *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1469. if (!*pskb)
  1470. return -ENOMEM;
  1471. return 0;
  1472. }
  1473. static int devlink_dpipe_tables_fill(struct genl_info *info,
  1474. enum devlink_command cmd, int flags,
  1475. struct list_head *dpipe_tables,
  1476. const char *table_name)
  1477. {
  1478. struct devlink *devlink = info->user_ptr[0];
  1479. struct devlink_dpipe_table *table;
  1480. struct nlattr *tables_attr;
  1481. struct sk_buff *skb = NULL;
  1482. struct nlmsghdr *nlh;
  1483. bool incomplete;
  1484. void *hdr;
  1485. int i;
  1486. int err;
  1487. table = list_first_entry(dpipe_tables,
  1488. struct devlink_dpipe_table, list);
  1489. start_again:
  1490. err = devlink_dpipe_send_and_alloc_skb(&skb, info);
  1491. if (err)
  1492. return err;
  1493. hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
  1494. &devlink_nl_family, NLM_F_MULTI, cmd);
  1495. if (!hdr) {
  1496. nlmsg_free(skb);
  1497. return -EMSGSIZE;
  1498. }
  1499. if (devlink_nl_put_handle(skb, devlink))
  1500. goto nla_put_failure;
  1501. tables_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLES);
  1502. if (!tables_attr)
  1503. goto nla_put_failure;
  1504. i = 0;
  1505. incomplete = false;
  1506. list_for_each_entry_from(table, dpipe_tables, list) {
  1507. if (!table_name) {
  1508. err = devlink_dpipe_table_put(skb, table);
  1509. if (err) {
  1510. if (!i)
  1511. goto err_table_put;
  1512. incomplete = true;
  1513. break;
  1514. }
  1515. } else {
  1516. if (!strcmp(table->name, table_name)) {
  1517. err = devlink_dpipe_table_put(skb, table);
  1518. if (err)
  1519. break;
  1520. }
  1521. }
  1522. i++;
  1523. }
  1524. nla_nest_end(skb, tables_attr);
  1525. genlmsg_end(skb, hdr);
  1526. if (incomplete)
  1527. goto start_again;
  1528. send_done:
  1529. nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
  1530. NLMSG_DONE, 0, flags | NLM_F_MULTI);
  1531. if (!nlh) {
  1532. err = devlink_dpipe_send_and_alloc_skb(&skb, info);
  1533. if (err)
  1534. return err;
  1535. goto send_done;
  1536. }
  1537. return genlmsg_reply(skb, info);
  1538. nla_put_failure:
  1539. err = -EMSGSIZE;
  1540. err_table_put:
  1541. genlmsg_cancel(skb, hdr);
  1542. nlmsg_free(skb);
  1543. return err;
  1544. }
  1545. static int devlink_nl_cmd_dpipe_table_get(struct sk_buff *skb,
  1546. struct genl_info *info)
  1547. {
  1548. struct devlink *devlink = info->user_ptr[0];
  1549. const char *table_name = NULL;
  1550. if (info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
  1551. table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
  1552. return devlink_dpipe_tables_fill(info, DEVLINK_CMD_DPIPE_TABLE_GET, 0,
  1553. &devlink->dpipe_table_list,
  1554. table_name);
  1555. }
  1556. static int devlink_dpipe_value_put(struct sk_buff *skb,
  1557. struct devlink_dpipe_value *value)
  1558. {
  1559. if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE,
  1560. value->value_size, value->value))
  1561. return -EMSGSIZE;
  1562. if (value->mask)
  1563. if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE_MASK,
  1564. value->value_size, value->mask))
  1565. return -EMSGSIZE;
  1566. if (value->mapping_valid)
  1567. if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_VALUE_MAPPING,
  1568. value->mapping_value))
  1569. return -EMSGSIZE;
  1570. return 0;
  1571. }
  1572. static int devlink_dpipe_action_value_put(struct sk_buff *skb,
  1573. struct devlink_dpipe_value *value)
  1574. {
  1575. if (!value->action)
  1576. return -EINVAL;
  1577. if (devlink_dpipe_action_put(skb, value->action))
  1578. return -EMSGSIZE;
  1579. if (devlink_dpipe_value_put(skb, value))
  1580. return -EMSGSIZE;
  1581. return 0;
  1582. }
  1583. static int devlink_dpipe_action_values_put(struct sk_buff *skb,
  1584. struct devlink_dpipe_value *values,
  1585. unsigned int values_count)
  1586. {
  1587. struct nlattr *action_attr;
  1588. int i;
  1589. int err;
  1590. for (i = 0; i < values_count; i++) {
  1591. action_attr = nla_nest_start(skb,
  1592. DEVLINK_ATTR_DPIPE_ACTION_VALUE);
  1593. if (!action_attr)
  1594. return -EMSGSIZE;
  1595. err = devlink_dpipe_action_value_put(skb, &values[i]);
  1596. if (err)
  1597. goto err_action_value_put;
  1598. nla_nest_end(skb, action_attr);
  1599. }
  1600. return 0;
  1601. err_action_value_put:
  1602. nla_nest_cancel(skb, action_attr);
  1603. return err;
  1604. }
  1605. static int devlink_dpipe_match_value_put(struct sk_buff *skb,
  1606. struct devlink_dpipe_value *value)
  1607. {
  1608. if (!value->match)
  1609. return -EINVAL;
  1610. if (devlink_dpipe_match_put(skb, value->match))
  1611. return -EMSGSIZE;
  1612. if (devlink_dpipe_value_put(skb, value))
  1613. return -EMSGSIZE;
  1614. return 0;
  1615. }
  1616. static int devlink_dpipe_match_values_put(struct sk_buff *skb,
  1617. struct devlink_dpipe_value *values,
  1618. unsigned int values_count)
  1619. {
  1620. struct nlattr *match_attr;
  1621. int i;
  1622. int err;
  1623. for (i = 0; i < values_count; i++) {
  1624. match_attr = nla_nest_start(skb,
  1625. DEVLINK_ATTR_DPIPE_MATCH_VALUE);
  1626. if (!match_attr)
  1627. return -EMSGSIZE;
  1628. err = devlink_dpipe_match_value_put(skb, &values[i]);
  1629. if (err)
  1630. goto err_match_value_put;
  1631. nla_nest_end(skb, match_attr);
  1632. }
  1633. return 0;
  1634. err_match_value_put:
  1635. nla_nest_cancel(skb, match_attr);
  1636. return err;
  1637. }
  1638. static int devlink_dpipe_entry_put(struct sk_buff *skb,
  1639. struct devlink_dpipe_entry *entry)
  1640. {
  1641. struct nlattr *entry_attr, *matches_attr, *actions_attr;
  1642. int err;
  1643. entry_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_ENTRY);
  1644. if (!entry_attr)
  1645. return -EMSGSIZE;
  1646. if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_INDEX, entry->index,
  1647. DEVLINK_ATTR_PAD))
  1648. goto nla_put_failure;
  1649. if (entry->counter_valid)
  1650. if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_COUNTER,
  1651. entry->counter, DEVLINK_ATTR_PAD))
  1652. goto nla_put_failure;
  1653. matches_attr = nla_nest_start(skb,
  1654. DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES);
  1655. if (!matches_attr)
  1656. goto nla_put_failure;
  1657. err = devlink_dpipe_match_values_put(skb, entry->match_values,
  1658. entry->match_values_count);
  1659. if (err) {
  1660. nla_nest_cancel(skb, matches_attr);
  1661. goto err_match_values_put;
  1662. }
  1663. nla_nest_end(skb, matches_attr);
  1664. actions_attr = nla_nest_start(skb,
  1665. DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES);
  1666. if (!actions_attr)
  1667. goto nla_put_failure;
  1668. err = devlink_dpipe_action_values_put(skb, entry->action_values,
  1669. entry->action_values_count);
  1670. if (err) {
  1671. nla_nest_cancel(skb, actions_attr);
  1672. goto err_action_values_put;
  1673. }
  1674. nla_nest_end(skb, actions_attr);
  1675. nla_nest_end(skb, entry_attr);
  1676. return 0;
  1677. nla_put_failure:
  1678. err = -EMSGSIZE;
  1679. err_match_values_put:
  1680. err_action_values_put:
  1681. nla_nest_cancel(skb, entry_attr);
  1682. return err;
  1683. }
  1684. static struct devlink_dpipe_table *
  1685. devlink_dpipe_table_find(struct list_head *dpipe_tables,
  1686. const char *table_name)
  1687. {
  1688. struct devlink_dpipe_table *table;
  1689. list_for_each_entry_rcu(table, dpipe_tables, list) {
  1690. if (!strcmp(table->name, table_name))
  1691. return table;
  1692. }
  1693. return NULL;
  1694. }
  1695. int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx)
  1696. {
  1697. struct devlink *devlink;
  1698. int err;
  1699. err = devlink_dpipe_send_and_alloc_skb(&dump_ctx->skb,
  1700. dump_ctx->info);
  1701. if (err)
  1702. return err;
  1703. dump_ctx->hdr = genlmsg_put(dump_ctx->skb,
  1704. dump_ctx->info->snd_portid,
  1705. dump_ctx->info->snd_seq,
  1706. &devlink_nl_family, NLM_F_MULTI,
  1707. dump_ctx->cmd);
  1708. if (!dump_ctx->hdr)
  1709. goto nla_put_failure;
  1710. devlink = dump_ctx->info->user_ptr[0];
  1711. if (devlink_nl_put_handle(dump_ctx->skb, devlink))
  1712. goto nla_put_failure;
  1713. dump_ctx->nest = nla_nest_start(dump_ctx->skb,
  1714. DEVLINK_ATTR_DPIPE_ENTRIES);
  1715. if (!dump_ctx->nest)
  1716. goto nla_put_failure;
  1717. return 0;
  1718. nla_put_failure:
  1719. genlmsg_cancel(dump_ctx->skb, dump_ctx->hdr);
  1720. nlmsg_free(dump_ctx->skb);
  1721. return -EMSGSIZE;
  1722. }
  1723. EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_prepare);
  1724. int devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx,
  1725. struct devlink_dpipe_entry *entry)
  1726. {
  1727. return devlink_dpipe_entry_put(dump_ctx->skb, entry);
  1728. }
  1729. EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_append);
  1730. int devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx)
  1731. {
  1732. nla_nest_end(dump_ctx->skb, dump_ctx->nest);
  1733. genlmsg_end(dump_ctx->skb, dump_ctx->hdr);
  1734. return 0;
  1735. }
  1736. EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_close);
  1737. void devlink_dpipe_entry_clear(struct devlink_dpipe_entry *entry)
  1738. {
  1739. unsigned int value_count, value_index;
  1740. struct devlink_dpipe_value *value;
  1741. value = entry->action_values;
  1742. value_count = entry->action_values_count;
  1743. for (value_index = 0; value_index < value_count; value_index++) {
  1744. kfree(value[value_index].value);
  1745. kfree(value[value_index].mask);
  1746. }
  1747. value = entry->match_values;
  1748. value_count = entry->match_values_count;
  1749. for (value_index = 0; value_index < value_count; value_index++) {
  1750. kfree(value[value_index].value);
  1751. kfree(value[value_index].mask);
  1752. }
  1753. }
  1754. EXPORT_SYMBOL(devlink_dpipe_entry_clear);
  1755. static int devlink_dpipe_entries_fill(struct genl_info *info,
  1756. enum devlink_command cmd, int flags,
  1757. struct devlink_dpipe_table *table)
  1758. {
  1759. struct devlink_dpipe_dump_ctx dump_ctx;
  1760. struct nlmsghdr *nlh;
  1761. int err;
  1762. dump_ctx.skb = NULL;
  1763. dump_ctx.cmd = cmd;
  1764. dump_ctx.info = info;
  1765. err = table->table_ops->entries_dump(table->priv,
  1766. table->counters_enabled,
  1767. &dump_ctx);
  1768. if (err)
  1769. return err;
  1770. send_done:
  1771. nlh = nlmsg_put(dump_ctx.skb, info->snd_portid, info->snd_seq,
  1772. NLMSG_DONE, 0, flags | NLM_F_MULTI);
  1773. if (!nlh) {
  1774. err = devlink_dpipe_send_and_alloc_skb(&dump_ctx.skb, info);
  1775. if (err)
  1776. return err;
  1777. goto send_done;
  1778. }
  1779. return genlmsg_reply(dump_ctx.skb, info);
  1780. }
  1781. static int devlink_nl_cmd_dpipe_entries_get(struct sk_buff *skb,
  1782. struct genl_info *info)
  1783. {
  1784. struct devlink *devlink = info->user_ptr[0];
  1785. struct devlink_dpipe_table *table;
  1786. const char *table_name;
  1787. if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
  1788. return -EINVAL;
  1789. table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
  1790. table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
  1791. table_name);
  1792. if (!table)
  1793. return -EINVAL;
  1794. if (!table->table_ops->entries_dump)
  1795. return -EINVAL;
  1796. return devlink_dpipe_entries_fill(info, DEVLINK_CMD_DPIPE_ENTRIES_GET,
  1797. 0, table);
  1798. }
  1799. static int devlink_dpipe_fields_put(struct sk_buff *skb,
  1800. const struct devlink_dpipe_header *header)
  1801. {
  1802. struct devlink_dpipe_field *field;
  1803. struct nlattr *field_attr;
  1804. int i;
  1805. for (i = 0; i < header->fields_count; i++) {
  1806. field = &header->fields[i];
  1807. field_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_FIELD);
  1808. if (!field_attr)
  1809. return -EMSGSIZE;
  1810. if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_FIELD_NAME, field->name) ||
  1811. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
  1812. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH, field->bitwidth) ||
  1813. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE, field->mapping_type))
  1814. goto nla_put_failure;
  1815. nla_nest_end(skb, field_attr);
  1816. }
  1817. return 0;
  1818. nla_put_failure:
  1819. nla_nest_cancel(skb, field_attr);
  1820. return -EMSGSIZE;
  1821. }
  1822. static int devlink_dpipe_header_put(struct sk_buff *skb,
  1823. struct devlink_dpipe_header *header)
  1824. {
  1825. struct nlattr *fields_attr, *header_attr;
  1826. int err;
  1827. header_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_HEADER);
  1828. if (!header_attr)
  1829. return -EMSGSIZE;
  1830. if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_HEADER_NAME, header->name) ||
  1831. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
  1832. nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
  1833. goto nla_put_failure;
  1834. fields_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_HEADER_FIELDS);
  1835. if (!fields_attr)
  1836. goto nla_put_failure;
  1837. err = devlink_dpipe_fields_put(skb, header);
  1838. if (err) {
  1839. nla_nest_cancel(skb, fields_attr);
  1840. goto nla_put_failure;
  1841. }
  1842. nla_nest_end(skb, fields_attr);
  1843. nla_nest_end(skb, header_attr);
  1844. return 0;
  1845. nla_put_failure:
  1846. err = -EMSGSIZE;
  1847. nla_nest_cancel(skb, header_attr);
  1848. return err;
  1849. }
  1850. static int devlink_dpipe_headers_fill(struct genl_info *info,
  1851. enum devlink_command cmd, int flags,
  1852. struct devlink_dpipe_headers *
  1853. dpipe_headers)
  1854. {
  1855. struct devlink *devlink = info->user_ptr[0];
  1856. struct nlattr *headers_attr;
  1857. struct sk_buff *skb = NULL;
  1858. struct nlmsghdr *nlh;
  1859. void *hdr;
  1860. int i, j;
  1861. int err;
  1862. i = 0;
  1863. start_again:
  1864. err = devlink_dpipe_send_and_alloc_skb(&skb, info);
  1865. if (err)
  1866. return err;
  1867. hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
  1868. &devlink_nl_family, NLM_F_MULTI, cmd);
  1869. if (!hdr) {
  1870. nlmsg_free(skb);
  1871. return -EMSGSIZE;
  1872. }
  1873. if (devlink_nl_put_handle(skb, devlink))
  1874. goto nla_put_failure;
  1875. headers_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_HEADERS);
  1876. if (!headers_attr)
  1877. goto nla_put_failure;
  1878. j = 0;
  1879. for (; i < dpipe_headers->headers_count; i++) {
  1880. err = devlink_dpipe_header_put(skb, dpipe_headers->headers[i]);
  1881. if (err) {
  1882. if (!j)
  1883. goto err_table_put;
  1884. break;
  1885. }
  1886. j++;
  1887. }
  1888. nla_nest_end(skb, headers_attr);
  1889. genlmsg_end(skb, hdr);
  1890. if (i != dpipe_headers->headers_count)
  1891. goto start_again;
  1892. send_done:
  1893. nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
  1894. NLMSG_DONE, 0, flags | NLM_F_MULTI);
  1895. if (!nlh) {
  1896. err = devlink_dpipe_send_and_alloc_skb(&skb, info);
  1897. if (err)
  1898. return err;
  1899. goto send_done;
  1900. }
  1901. return genlmsg_reply(skb, info);
  1902. nla_put_failure:
  1903. err = -EMSGSIZE;
  1904. err_table_put:
  1905. genlmsg_cancel(skb, hdr);
  1906. nlmsg_free(skb);
  1907. return err;
  1908. }
  1909. static int devlink_nl_cmd_dpipe_headers_get(struct sk_buff *skb,
  1910. struct genl_info *info)
  1911. {
  1912. struct devlink *devlink = info->user_ptr[0];
  1913. if (!devlink->dpipe_headers)
  1914. return -EOPNOTSUPP;
  1915. return devlink_dpipe_headers_fill(info, DEVLINK_CMD_DPIPE_HEADERS_GET,
  1916. 0, devlink->dpipe_headers);
  1917. }
  1918. static int devlink_dpipe_table_counters_set(struct devlink *devlink,
  1919. const char *table_name,
  1920. bool enable)
  1921. {
  1922. struct devlink_dpipe_table *table;
  1923. table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
  1924. table_name);
  1925. if (!table)
  1926. return -EINVAL;
  1927. if (table->counter_control_extern)
  1928. return -EOPNOTSUPP;
  1929. if (!(table->counters_enabled ^ enable))
  1930. return 0;
  1931. table->counters_enabled = enable;
  1932. if (table->table_ops->counters_set_update)
  1933. table->table_ops->counters_set_update(table->priv, enable);
  1934. return 0;
  1935. }
  1936. static int devlink_nl_cmd_dpipe_table_counters_set(struct sk_buff *skb,
  1937. struct genl_info *info)
  1938. {
  1939. struct devlink *devlink = info->user_ptr[0];
  1940. const char *table_name;
  1941. bool counters_enable;
  1942. if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME] ||
  1943. !info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED])
  1944. return -EINVAL;
  1945. table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
  1946. counters_enable = !!nla_get_u8(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED]);
  1947. return devlink_dpipe_table_counters_set(devlink, table_name,
  1948. counters_enable);
  1949. }
  1950. static struct devlink_resource *
  1951. devlink_resource_find(struct devlink *devlink,
  1952. struct devlink_resource *resource, u64 resource_id)
  1953. {
  1954. struct list_head *resource_list;
  1955. if (resource)
  1956. resource_list = &resource->resource_list;
  1957. else
  1958. resource_list = &devlink->resource_list;
  1959. list_for_each_entry(resource, resource_list, list) {
  1960. struct devlink_resource *child_resource;
  1961. if (resource->id == resource_id)
  1962. return resource;
  1963. child_resource = devlink_resource_find(devlink, resource,
  1964. resource_id);
  1965. if (child_resource)
  1966. return child_resource;
  1967. }
  1968. return NULL;
  1969. }
  1970. static void
  1971. devlink_resource_validate_children(struct devlink_resource *resource)
  1972. {
  1973. struct devlink_resource *child_resource;
  1974. bool size_valid = true;
  1975. u64 parts_size = 0;
  1976. if (list_empty(&resource->resource_list))
  1977. goto out;
  1978. list_for_each_entry(child_resource, &resource->resource_list, list)
  1979. parts_size += child_resource->size_new;
  1980. if (parts_size > resource->size_new)
  1981. size_valid = false;
  1982. out:
  1983. resource->size_valid = size_valid;
  1984. }
  1985. static int devlink_nl_cmd_resource_set(struct sk_buff *skb,
  1986. struct genl_info *info)
  1987. {
  1988. struct devlink *devlink = info->user_ptr[0];
  1989. struct devlink_resource *resource;
  1990. u64 resource_id;
  1991. u64 size;
  1992. int err;
  1993. if (!info->attrs[DEVLINK_ATTR_RESOURCE_ID] ||
  1994. !info->attrs[DEVLINK_ATTR_RESOURCE_SIZE])
  1995. return -EINVAL;
  1996. resource_id = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_ID]);
  1997. resource = devlink_resource_find(devlink, NULL, resource_id);
  1998. if (!resource)
  1999. return -EINVAL;
  2000. if (!resource->resource_ops->size_validate)
  2001. return -EINVAL;
  2002. size = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_SIZE]);
  2003. err = resource->resource_ops->size_validate(devlink, size,
  2004. info->extack);
  2005. if (err)
  2006. return err;
  2007. resource->size_new = size;
  2008. devlink_resource_validate_children(resource);
  2009. if (resource->parent)
  2010. devlink_resource_validate_children(resource->parent);
  2011. return 0;
  2012. }
  2013. static int
  2014. devlink_resource_size_params_put(struct devlink_resource *resource,
  2015. struct sk_buff *skb)
  2016. {
  2017. struct devlink_resource_size_params *size_params;
  2018. size_params = &resource->size_params;
  2019. if (nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_GRAN,
  2020. size_params->size_granularity, DEVLINK_ATTR_PAD) ||
  2021. nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MAX,
  2022. size_params->size_max, DEVLINK_ATTR_PAD) ||
  2023. nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MIN,
  2024. size_params->size_min, DEVLINK_ATTR_PAD) ||
  2025. nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_UNIT, size_params->unit))
  2026. return -EMSGSIZE;
  2027. return 0;
  2028. }
  2029. static int devlink_resource_put(struct devlink *devlink, struct sk_buff *skb,
  2030. struct devlink_resource *resource)
  2031. {
  2032. struct devlink_resource *child_resource;
  2033. struct nlattr *child_resource_attr;
  2034. struct nlattr *resource_attr;
  2035. resource_attr = nla_nest_start(skb, DEVLINK_ATTR_RESOURCE);
  2036. if (!resource_attr)
  2037. return -EMSGSIZE;
  2038. if (nla_put_string(skb, DEVLINK_ATTR_RESOURCE_NAME, resource->name) ||
  2039. nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE, resource->size,
  2040. DEVLINK_ATTR_PAD) ||
  2041. nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_ID, resource->id,
  2042. DEVLINK_ATTR_PAD))
  2043. goto nla_put_failure;
  2044. if (resource->size != resource->size_new)
  2045. nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_NEW,
  2046. resource->size_new, DEVLINK_ATTR_PAD);
  2047. if (resource->resource_ops && resource->resource_ops->occ_get)
  2048. if (nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_OCC,
  2049. resource->resource_ops->occ_get(devlink),
  2050. DEVLINK_ATTR_PAD))
  2051. goto nla_put_failure;
  2052. if (devlink_resource_size_params_put(resource, skb))
  2053. goto nla_put_failure;
  2054. if (list_empty(&resource->resource_list))
  2055. goto out;
  2056. if (nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_SIZE_VALID,
  2057. resource->size_valid))
  2058. goto nla_put_failure;
  2059. child_resource_attr = nla_nest_start(skb, DEVLINK_ATTR_RESOURCE_LIST);
  2060. if (!child_resource_attr)
  2061. goto nla_put_failure;
  2062. list_for_each_entry(child_resource, &resource->resource_list, list) {
  2063. if (devlink_resource_put(devlink, skb, child_resource))
  2064. goto resource_put_failure;
  2065. }
  2066. nla_nest_end(skb, child_resource_attr);
  2067. out:
  2068. nla_nest_end(skb, resource_attr);
  2069. return 0;
  2070. resource_put_failure:
  2071. nla_nest_cancel(skb, child_resource_attr);
  2072. nla_put_failure:
  2073. nla_nest_cancel(skb, resource_attr);
  2074. return -EMSGSIZE;
  2075. }
  2076. static int devlink_resource_fill(struct genl_info *info,
  2077. enum devlink_command cmd, int flags)
  2078. {
  2079. struct devlink *devlink = info->user_ptr[0];
  2080. struct devlink_resource *resource;
  2081. struct nlattr *resources_attr;
  2082. struct sk_buff *skb = NULL;
  2083. struct nlmsghdr *nlh;
  2084. bool incomplete;
  2085. void *hdr;
  2086. int i;
  2087. int err;
  2088. resource = list_first_entry(&devlink->resource_list,
  2089. struct devlink_resource, list);
  2090. start_again:
  2091. err = devlink_dpipe_send_and_alloc_skb(&skb, info);
  2092. if (err)
  2093. return err;
  2094. hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
  2095. &devlink_nl_family, NLM_F_MULTI, cmd);
  2096. if (!hdr) {
  2097. nlmsg_free(skb);
  2098. return -EMSGSIZE;
  2099. }
  2100. if (devlink_nl_put_handle(skb, devlink))
  2101. goto nla_put_failure;
  2102. resources_attr = nla_nest_start(skb, DEVLINK_ATTR_RESOURCE_LIST);
  2103. if (!resources_attr)
  2104. goto nla_put_failure;
  2105. incomplete = false;
  2106. i = 0;
  2107. list_for_each_entry_from(resource, &devlink->resource_list, list) {
  2108. err = devlink_resource_put(devlink, skb, resource);
  2109. if (err) {
  2110. if (!i)
  2111. goto err_resource_put;
  2112. incomplete = true;
  2113. break;
  2114. }
  2115. i++;
  2116. }
  2117. nla_nest_end(skb, resources_attr);
  2118. genlmsg_end(skb, hdr);
  2119. if (incomplete)
  2120. goto start_again;
  2121. send_done:
  2122. nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
  2123. NLMSG_DONE, 0, flags | NLM_F_MULTI);
  2124. if (!nlh) {
  2125. err = devlink_dpipe_send_and_alloc_skb(&skb, info);
  2126. if (err)
  2127. goto err_skb_send_alloc;
  2128. goto send_done;
  2129. }
  2130. return genlmsg_reply(skb, info);
  2131. nla_put_failure:
  2132. err = -EMSGSIZE;
  2133. err_resource_put:
  2134. err_skb_send_alloc:
  2135. genlmsg_cancel(skb, hdr);
  2136. nlmsg_free(skb);
  2137. return err;
  2138. }
  2139. static int devlink_nl_cmd_resource_dump(struct sk_buff *skb,
  2140. struct genl_info *info)
  2141. {
  2142. struct devlink *devlink = info->user_ptr[0];
  2143. if (list_empty(&devlink->resource_list))
  2144. return -EOPNOTSUPP;
  2145. return devlink_resource_fill(info, DEVLINK_CMD_RESOURCE_DUMP, 0);
  2146. }
  2147. static int
  2148. devlink_resources_validate(struct devlink *devlink,
  2149. struct devlink_resource *resource,
  2150. struct genl_info *info)
  2151. {
  2152. struct list_head *resource_list;
  2153. int err = 0;
  2154. if (resource)
  2155. resource_list = &resource->resource_list;
  2156. else
  2157. resource_list = &devlink->resource_list;
  2158. list_for_each_entry(resource, resource_list, list) {
  2159. if (!resource->size_valid)
  2160. return -EINVAL;
  2161. err = devlink_resources_validate(devlink, resource, info);
  2162. if (err)
  2163. return err;
  2164. }
  2165. return err;
  2166. }
  2167. static int devlink_nl_cmd_reload(struct sk_buff *skb, struct genl_info *info)
  2168. {
  2169. struct devlink *devlink = info->user_ptr[0];
  2170. int err;
  2171. if (!devlink->ops->reload)
  2172. return -EOPNOTSUPP;
  2173. err = devlink_resources_validate(devlink, NULL, info);
  2174. if (err) {
  2175. NL_SET_ERR_MSG_MOD(info->extack, "resources size validation failed");
  2176. return err;
  2177. }
  2178. return devlink->ops->reload(devlink);
  2179. }
  2180. static const struct nla_policy devlink_nl_policy[DEVLINK_ATTR_MAX + 1] = {
  2181. [DEVLINK_ATTR_BUS_NAME] = { .type = NLA_NUL_STRING },
  2182. [DEVLINK_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING },
  2183. [DEVLINK_ATTR_PORT_INDEX] = { .type = NLA_U32 },
  2184. [DEVLINK_ATTR_PORT_TYPE] = { .type = NLA_U16 },
  2185. [DEVLINK_ATTR_PORT_SPLIT_COUNT] = { .type = NLA_U32 },
  2186. [DEVLINK_ATTR_SB_INDEX] = { .type = NLA_U32 },
  2187. [DEVLINK_ATTR_SB_POOL_INDEX] = { .type = NLA_U16 },
  2188. [DEVLINK_ATTR_SB_POOL_TYPE] = { .type = NLA_U8 },
  2189. [DEVLINK_ATTR_SB_POOL_SIZE] = { .type = NLA_U32 },
  2190. [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE] = { .type = NLA_U8 },
  2191. [DEVLINK_ATTR_SB_THRESHOLD] = { .type = NLA_U32 },
  2192. [DEVLINK_ATTR_SB_TC_INDEX] = { .type = NLA_U16 },
  2193. [DEVLINK_ATTR_ESWITCH_MODE] = { .type = NLA_U16 },
  2194. [DEVLINK_ATTR_ESWITCH_INLINE_MODE] = { .type = NLA_U8 },
  2195. [DEVLINK_ATTR_ESWITCH_ENCAP_MODE] = { .type = NLA_U8 },
  2196. [DEVLINK_ATTR_DPIPE_TABLE_NAME] = { .type = NLA_NUL_STRING },
  2197. [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED] = { .type = NLA_U8 },
  2198. [DEVLINK_ATTR_RESOURCE_ID] = { .type = NLA_U64},
  2199. [DEVLINK_ATTR_RESOURCE_SIZE] = { .type = NLA_U64},
  2200. };
  2201. static const struct genl_ops devlink_nl_ops[] = {
  2202. {
  2203. .cmd = DEVLINK_CMD_GET,
  2204. .doit = devlink_nl_cmd_get_doit,
  2205. .dumpit = devlink_nl_cmd_get_dumpit,
  2206. .policy = devlink_nl_policy,
  2207. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  2208. /* can be retrieved by unprivileged users */
  2209. },
  2210. {
  2211. .cmd = DEVLINK_CMD_PORT_GET,
  2212. .doit = devlink_nl_cmd_port_get_doit,
  2213. .dumpit = devlink_nl_cmd_port_get_dumpit,
  2214. .policy = devlink_nl_policy,
  2215. .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
  2216. /* can be retrieved by unprivileged users */
  2217. },
  2218. {
  2219. .cmd = DEVLINK_CMD_PORT_SET,
  2220. .doit = devlink_nl_cmd_port_set_doit,
  2221. .policy = devlink_nl_policy,
  2222. .flags = GENL_ADMIN_PERM,
  2223. .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
  2224. },
  2225. {
  2226. .cmd = DEVLINK_CMD_PORT_SPLIT,
  2227. .doit = devlink_nl_cmd_port_split_doit,
  2228. .policy = devlink_nl_policy,
  2229. .flags = GENL_ADMIN_PERM,
  2230. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  2231. DEVLINK_NL_FLAG_NO_LOCK,
  2232. },
  2233. {
  2234. .cmd = DEVLINK_CMD_PORT_UNSPLIT,
  2235. .doit = devlink_nl_cmd_port_unsplit_doit,
  2236. .policy = devlink_nl_policy,
  2237. .flags = GENL_ADMIN_PERM,
  2238. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  2239. DEVLINK_NL_FLAG_NO_LOCK,
  2240. },
  2241. {
  2242. .cmd = DEVLINK_CMD_SB_GET,
  2243. .doit = devlink_nl_cmd_sb_get_doit,
  2244. .dumpit = devlink_nl_cmd_sb_get_dumpit,
  2245. .policy = devlink_nl_policy,
  2246. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  2247. DEVLINK_NL_FLAG_NEED_SB,
  2248. /* can be retrieved by unprivileged users */
  2249. },
  2250. {
  2251. .cmd = DEVLINK_CMD_SB_POOL_GET,
  2252. .doit = devlink_nl_cmd_sb_pool_get_doit,
  2253. .dumpit = devlink_nl_cmd_sb_pool_get_dumpit,
  2254. .policy = devlink_nl_policy,
  2255. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  2256. DEVLINK_NL_FLAG_NEED_SB,
  2257. /* can be retrieved by unprivileged users */
  2258. },
  2259. {
  2260. .cmd = DEVLINK_CMD_SB_POOL_SET,
  2261. .doit = devlink_nl_cmd_sb_pool_set_doit,
  2262. .policy = devlink_nl_policy,
  2263. .flags = GENL_ADMIN_PERM,
  2264. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  2265. DEVLINK_NL_FLAG_NEED_SB,
  2266. },
  2267. {
  2268. .cmd = DEVLINK_CMD_SB_PORT_POOL_GET,
  2269. .doit = devlink_nl_cmd_sb_port_pool_get_doit,
  2270. .dumpit = devlink_nl_cmd_sb_port_pool_get_dumpit,
  2271. .policy = devlink_nl_policy,
  2272. .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
  2273. DEVLINK_NL_FLAG_NEED_SB,
  2274. /* can be retrieved by unprivileged users */
  2275. },
  2276. {
  2277. .cmd = DEVLINK_CMD_SB_PORT_POOL_SET,
  2278. .doit = devlink_nl_cmd_sb_port_pool_set_doit,
  2279. .policy = devlink_nl_policy,
  2280. .flags = GENL_ADMIN_PERM,
  2281. .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
  2282. DEVLINK_NL_FLAG_NEED_SB,
  2283. },
  2284. {
  2285. .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_GET,
  2286. .doit = devlink_nl_cmd_sb_tc_pool_bind_get_doit,
  2287. .dumpit = devlink_nl_cmd_sb_tc_pool_bind_get_dumpit,
  2288. .policy = devlink_nl_policy,
  2289. .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
  2290. DEVLINK_NL_FLAG_NEED_SB,
  2291. /* can be retrieved by unprivileged users */
  2292. },
  2293. {
  2294. .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_SET,
  2295. .doit = devlink_nl_cmd_sb_tc_pool_bind_set_doit,
  2296. .policy = devlink_nl_policy,
  2297. .flags = GENL_ADMIN_PERM,
  2298. .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
  2299. DEVLINK_NL_FLAG_NEED_SB,
  2300. },
  2301. {
  2302. .cmd = DEVLINK_CMD_SB_OCC_SNAPSHOT,
  2303. .doit = devlink_nl_cmd_sb_occ_snapshot_doit,
  2304. .policy = devlink_nl_policy,
  2305. .flags = GENL_ADMIN_PERM,
  2306. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  2307. DEVLINK_NL_FLAG_NEED_SB,
  2308. },
  2309. {
  2310. .cmd = DEVLINK_CMD_SB_OCC_MAX_CLEAR,
  2311. .doit = devlink_nl_cmd_sb_occ_max_clear_doit,
  2312. .policy = devlink_nl_policy,
  2313. .flags = GENL_ADMIN_PERM,
  2314. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  2315. DEVLINK_NL_FLAG_NEED_SB,
  2316. },
  2317. {
  2318. .cmd = DEVLINK_CMD_ESWITCH_GET,
  2319. .doit = devlink_nl_cmd_eswitch_get_doit,
  2320. .policy = devlink_nl_policy,
  2321. .flags = GENL_ADMIN_PERM,
  2322. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  2323. },
  2324. {
  2325. .cmd = DEVLINK_CMD_ESWITCH_SET,
  2326. .doit = devlink_nl_cmd_eswitch_set_doit,
  2327. .policy = devlink_nl_policy,
  2328. .flags = GENL_ADMIN_PERM,
  2329. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  2330. },
  2331. {
  2332. .cmd = DEVLINK_CMD_DPIPE_TABLE_GET,
  2333. .doit = devlink_nl_cmd_dpipe_table_get,
  2334. .policy = devlink_nl_policy,
  2335. .flags = GENL_ADMIN_PERM,
  2336. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  2337. },
  2338. {
  2339. .cmd = DEVLINK_CMD_DPIPE_ENTRIES_GET,
  2340. .doit = devlink_nl_cmd_dpipe_entries_get,
  2341. .policy = devlink_nl_policy,
  2342. .flags = GENL_ADMIN_PERM,
  2343. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  2344. },
  2345. {
  2346. .cmd = DEVLINK_CMD_DPIPE_HEADERS_GET,
  2347. .doit = devlink_nl_cmd_dpipe_headers_get,
  2348. .policy = devlink_nl_policy,
  2349. .flags = GENL_ADMIN_PERM,
  2350. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  2351. },
  2352. {
  2353. .cmd = DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET,
  2354. .doit = devlink_nl_cmd_dpipe_table_counters_set,
  2355. .policy = devlink_nl_policy,
  2356. .flags = GENL_ADMIN_PERM,
  2357. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  2358. },
  2359. {
  2360. .cmd = DEVLINK_CMD_RESOURCE_SET,
  2361. .doit = devlink_nl_cmd_resource_set,
  2362. .policy = devlink_nl_policy,
  2363. .flags = GENL_ADMIN_PERM,
  2364. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  2365. },
  2366. {
  2367. .cmd = DEVLINK_CMD_RESOURCE_DUMP,
  2368. .doit = devlink_nl_cmd_resource_dump,
  2369. .policy = devlink_nl_policy,
  2370. .flags = GENL_ADMIN_PERM,
  2371. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  2372. },
  2373. {
  2374. .cmd = DEVLINK_CMD_RELOAD,
  2375. .doit = devlink_nl_cmd_reload,
  2376. .policy = devlink_nl_policy,
  2377. .flags = GENL_ADMIN_PERM,
  2378. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  2379. DEVLINK_NL_FLAG_NO_LOCK,
  2380. },
  2381. };
  2382. static struct genl_family devlink_nl_family __ro_after_init = {
  2383. .name = DEVLINK_GENL_NAME,
  2384. .version = DEVLINK_GENL_VERSION,
  2385. .maxattr = DEVLINK_ATTR_MAX,
  2386. .netnsok = true,
  2387. .pre_doit = devlink_nl_pre_doit,
  2388. .post_doit = devlink_nl_post_doit,
  2389. .module = THIS_MODULE,
  2390. .ops = devlink_nl_ops,
  2391. .n_ops = ARRAY_SIZE(devlink_nl_ops),
  2392. .mcgrps = devlink_nl_mcgrps,
  2393. .n_mcgrps = ARRAY_SIZE(devlink_nl_mcgrps),
  2394. };
  2395. /**
  2396. * devlink_alloc - Allocate new devlink instance resources
  2397. *
  2398. * @ops: ops
  2399. * @priv_size: size of user private data
  2400. *
  2401. * Allocate new devlink instance resources, including devlink index
  2402. * and name.
  2403. */
  2404. struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size)
  2405. {
  2406. struct devlink *devlink;
  2407. devlink = kzalloc(sizeof(*devlink) + priv_size, GFP_KERNEL);
  2408. if (!devlink)
  2409. return NULL;
  2410. devlink->ops = ops;
  2411. devlink_net_set(devlink, &init_net);
  2412. INIT_LIST_HEAD(&devlink->port_list);
  2413. INIT_LIST_HEAD(&devlink->sb_list);
  2414. INIT_LIST_HEAD_RCU(&devlink->dpipe_table_list);
  2415. INIT_LIST_HEAD(&devlink->resource_list);
  2416. mutex_init(&devlink->lock);
  2417. return devlink;
  2418. }
  2419. EXPORT_SYMBOL_GPL(devlink_alloc);
  2420. /**
  2421. * devlink_register - Register devlink instance
  2422. *
  2423. * @devlink: devlink
  2424. */
  2425. int devlink_register(struct devlink *devlink, struct device *dev)
  2426. {
  2427. mutex_lock(&devlink_mutex);
  2428. devlink->dev = dev;
  2429. list_add_tail(&devlink->list, &devlink_list);
  2430. devlink_notify(devlink, DEVLINK_CMD_NEW);
  2431. mutex_unlock(&devlink_mutex);
  2432. return 0;
  2433. }
  2434. EXPORT_SYMBOL_GPL(devlink_register);
  2435. /**
  2436. * devlink_unregister - Unregister devlink instance
  2437. *
  2438. * @devlink: devlink
  2439. */
  2440. void devlink_unregister(struct devlink *devlink)
  2441. {
  2442. mutex_lock(&devlink_mutex);
  2443. devlink_notify(devlink, DEVLINK_CMD_DEL);
  2444. list_del(&devlink->list);
  2445. mutex_unlock(&devlink_mutex);
  2446. }
  2447. EXPORT_SYMBOL_GPL(devlink_unregister);
  2448. /**
  2449. * devlink_free - Free devlink instance resources
  2450. *
  2451. * @devlink: devlink
  2452. */
  2453. void devlink_free(struct devlink *devlink)
  2454. {
  2455. kfree(devlink);
  2456. }
  2457. EXPORT_SYMBOL_GPL(devlink_free);
  2458. /**
  2459. * devlink_port_register - Register devlink port
  2460. *
  2461. * @devlink: devlink
  2462. * @devlink_port: devlink port
  2463. * @port_index
  2464. *
  2465. * Register devlink port with provided port index. User can use
  2466. * any indexing, even hw-related one. devlink_port structure
  2467. * is convenient to be embedded inside user driver private structure.
  2468. * Note that the caller should take care of zeroing the devlink_port
  2469. * structure.
  2470. */
  2471. int devlink_port_register(struct devlink *devlink,
  2472. struct devlink_port *devlink_port,
  2473. unsigned int port_index)
  2474. {
  2475. mutex_lock(&devlink->lock);
  2476. if (devlink_port_index_exists(devlink, port_index)) {
  2477. mutex_unlock(&devlink->lock);
  2478. return -EEXIST;
  2479. }
  2480. devlink_port->devlink = devlink;
  2481. devlink_port->index = port_index;
  2482. devlink_port->registered = true;
  2483. list_add_tail(&devlink_port->list, &devlink->port_list);
  2484. mutex_unlock(&devlink->lock);
  2485. devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
  2486. return 0;
  2487. }
  2488. EXPORT_SYMBOL_GPL(devlink_port_register);
  2489. /**
  2490. * devlink_port_unregister - Unregister devlink port
  2491. *
  2492. * @devlink_port: devlink port
  2493. */
  2494. void devlink_port_unregister(struct devlink_port *devlink_port)
  2495. {
  2496. struct devlink *devlink = devlink_port->devlink;
  2497. devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL);
  2498. mutex_lock(&devlink->lock);
  2499. list_del(&devlink_port->list);
  2500. mutex_unlock(&devlink->lock);
  2501. }
  2502. EXPORT_SYMBOL_GPL(devlink_port_unregister);
  2503. static void __devlink_port_type_set(struct devlink_port *devlink_port,
  2504. enum devlink_port_type type,
  2505. void *type_dev)
  2506. {
  2507. devlink_port->type = type;
  2508. devlink_port->type_dev = type_dev;
  2509. devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
  2510. }
  2511. /**
  2512. * devlink_port_type_eth_set - Set port type to Ethernet
  2513. *
  2514. * @devlink_port: devlink port
  2515. * @netdev: related netdevice
  2516. */
  2517. void devlink_port_type_eth_set(struct devlink_port *devlink_port,
  2518. struct net_device *netdev)
  2519. {
  2520. return __devlink_port_type_set(devlink_port,
  2521. DEVLINK_PORT_TYPE_ETH, netdev);
  2522. }
  2523. EXPORT_SYMBOL_GPL(devlink_port_type_eth_set);
  2524. /**
  2525. * devlink_port_type_ib_set - Set port type to InfiniBand
  2526. *
  2527. * @devlink_port: devlink port
  2528. * @ibdev: related IB device
  2529. */
  2530. void devlink_port_type_ib_set(struct devlink_port *devlink_port,
  2531. struct ib_device *ibdev)
  2532. {
  2533. return __devlink_port_type_set(devlink_port,
  2534. DEVLINK_PORT_TYPE_IB, ibdev);
  2535. }
  2536. EXPORT_SYMBOL_GPL(devlink_port_type_ib_set);
  2537. /**
  2538. * devlink_port_type_clear - Clear port type
  2539. *
  2540. * @devlink_port: devlink port
  2541. */
  2542. void devlink_port_type_clear(struct devlink_port *devlink_port)
  2543. {
  2544. return __devlink_port_type_set(devlink_port,
  2545. DEVLINK_PORT_TYPE_NOTSET, NULL);
  2546. }
  2547. EXPORT_SYMBOL_GPL(devlink_port_type_clear);
  2548. /**
  2549. * devlink_port_split_set - Set port is split
  2550. *
  2551. * @devlink_port: devlink port
  2552. * @split_group: split group - identifies group split port is part of
  2553. */
  2554. void devlink_port_split_set(struct devlink_port *devlink_port,
  2555. u32 split_group)
  2556. {
  2557. devlink_port->split = true;
  2558. devlink_port->split_group = split_group;
  2559. devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
  2560. }
  2561. EXPORT_SYMBOL_GPL(devlink_port_split_set);
  2562. int devlink_sb_register(struct devlink *devlink, unsigned int sb_index,
  2563. u32 size, u16 ingress_pools_count,
  2564. u16 egress_pools_count, u16 ingress_tc_count,
  2565. u16 egress_tc_count)
  2566. {
  2567. struct devlink_sb *devlink_sb;
  2568. int err = 0;
  2569. mutex_lock(&devlink->lock);
  2570. if (devlink_sb_index_exists(devlink, sb_index)) {
  2571. err = -EEXIST;
  2572. goto unlock;
  2573. }
  2574. devlink_sb = kzalloc(sizeof(*devlink_sb), GFP_KERNEL);
  2575. if (!devlink_sb) {
  2576. err = -ENOMEM;
  2577. goto unlock;
  2578. }
  2579. devlink_sb->index = sb_index;
  2580. devlink_sb->size = size;
  2581. devlink_sb->ingress_pools_count = ingress_pools_count;
  2582. devlink_sb->egress_pools_count = egress_pools_count;
  2583. devlink_sb->ingress_tc_count = ingress_tc_count;
  2584. devlink_sb->egress_tc_count = egress_tc_count;
  2585. list_add_tail(&devlink_sb->list, &devlink->sb_list);
  2586. unlock:
  2587. mutex_unlock(&devlink->lock);
  2588. return err;
  2589. }
  2590. EXPORT_SYMBOL_GPL(devlink_sb_register);
  2591. void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index)
  2592. {
  2593. struct devlink_sb *devlink_sb;
  2594. mutex_lock(&devlink->lock);
  2595. devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
  2596. WARN_ON(!devlink_sb);
  2597. list_del(&devlink_sb->list);
  2598. mutex_unlock(&devlink->lock);
  2599. kfree(devlink_sb);
  2600. }
  2601. EXPORT_SYMBOL_GPL(devlink_sb_unregister);
  2602. /**
  2603. * devlink_dpipe_headers_register - register dpipe headers
  2604. *
  2605. * @devlink: devlink
  2606. * @dpipe_headers: dpipe header array
  2607. *
  2608. * Register the headers supported by hardware.
  2609. */
  2610. int devlink_dpipe_headers_register(struct devlink *devlink,
  2611. struct devlink_dpipe_headers *dpipe_headers)
  2612. {
  2613. mutex_lock(&devlink->lock);
  2614. devlink->dpipe_headers = dpipe_headers;
  2615. mutex_unlock(&devlink->lock);
  2616. return 0;
  2617. }
  2618. EXPORT_SYMBOL_GPL(devlink_dpipe_headers_register);
  2619. /**
  2620. * devlink_dpipe_headers_unregister - unregister dpipe headers
  2621. *
  2622. * @devlink: devlink
  2623. *
  2624. * Unregister the headers supported by hardware.
  2625. */
  2626. void devlink_dpipe_headers_unregister(struct devlink *devlink)
  2627. {
  2628. mutex_lock(&devlink->lock);
  2629. devlink->dpipe_headers = NULL;
  2630. mutex_unlock(&devlink->lock);
  2631. }
  2632. EXPORT_SYMBOL_GPL(devlink_dpipe_headers_unregister);
  2633. /**
  2634. * devlink_dpipe_table_counter_enabled - check if counter allocation
  2635. * required
  2636. * @devlink: devlink
  2637. * @table_name: tables name
  2638. *
  2639. * Used by driver to check if counter allocation is required.
  2640. * After counter allocation is turned on the table entries
  2641. * are updated to include counter statistics.
  2642. *
  2643. * After that point on the driver must respect the counter
  2644. * state so that each entry added to the table is added
  2645. * with a counter.
  2646. */
  2647. bool devlink_dpipe_table_counter_enabled(struct devlink *devlink,
  2648. const char *table_name)
  2649. {
  2650. struct devlink_dpipe_table *table;
  2651. bool enabled;
  2652. rcu_read_lock();
  2653. table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
  2654. table_name);
  2655. enabled = false;
  2656. if (table)
  2657. enabled = table->counters_enabled;
  2658. rcu_read_unlock();
  2659. return enabled;
  2660. }
  2661. EXPORT_SYMBOL_GPL(devlink_dpipe_table_counter_enabled);
  2662. /**
  2663. * devlink_dpipe_table_register - register dpipe table
  2664. *
  2665. * @devlink: devlink
  2666. * @table_name: table name
  2667. * @table_ops: table ops
  2668. * @priv: priv
  2669. * @counter_control_extern: external control for counters
  2670. */
  2671. int devlink_dpipe_table_register(struct devlink *devlink,
  2672. const char *table_name,
  2673. struct devlink_dpipe_table_ops *table_ops,
  2674. void *priv, bool counter_control_extern)
  2675. {
  2676. struct devlink_dpipe_table *table;
  2677. if (devlink_dpipe_table_find(&devlink->dpipe_table_list, table_name))
  2678. return -EEXIST;
  2679. if (WARN_ON(!table_ops->size_get))
  2680. return -EINVAL;
  2681. table = kzalloc(sizeof(*table), GFP_KERNEL);
  2682. if (!table)
  2683. return -ENOMEM;
  2684. table->name = table_name;
  2685. table->table_ops = table_ops;
  2686. table->priv = priv;
  2687. table->counter_control_extern = counter_control_extern;
  2688. mutex_lock(&devlink->lock);
  2689. list_add_tail_rcu(&table->list, &devlink->dpipe_table_list);
  2690. mutex_unlock(&devlink->lock);
  2691. return 0;
  2692. }
  2693. EXPORT_SYMBOL_GPL(devlink_dpipe_table_register);
  2694. /**
  2695. * devlink_dpipe_table_unregister - unregister dpipe table
  2696. *
  2697. * @devlink: devlink
  2698. * @table_name: table name
  2699. */
  2700. void devlink_dpipe_table_unregister(struct devlink *devlink,
  2701. const char *table_name)
  2702. {
  2703. struct devlink_dpipe_table *table;
  2704. mutex_lock(&devlink->lock);
  2705. table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
  2706. table_name);
  2707. if (!table)
  2708. goto unlock;
  2709. list_del_rcu(&table->list);
  2710. mutex_unlock(&devlink->lock);
  2711. kfree_rcu(table, rcu);
  2712. return;
  2713. unlock:
  2714. mutex_unlock(&devlink->lock);
  2715. }
  2716. EXPORT_SYMBOL_GPL(devlink_dpipe_table_unregister);
  2717. /**
  2718. * devlink_resource_register - devlink resource register
  2719. *
  2720. * @devlink: devlink
  2721. * @resource_name: resource's name
  2722. * @top_hierarchy: top hierarchy
  2723. * @reload_required: reload is required for new configuration to
  2724. * apply
  2725. * @resource_size: resource's size
  2726. * @resource_id: resource's id
  2727. * @parent_reosurce_id: resource's parent id
  2728. * @size params: size parameters
  2729. * @resource_ops: resource ops
  2730. */
  2731. int devlink_resource_register(struct devlink *devlink,
  2732. const char *resource_name,
  2733. bool top_hierarchy,
  2734. u64 resource_size,
  2735. u64 resource_id,
  2736. u64 parent_resource_id,
  2737. const struct devlink_resource_size_params *size_params,
  2738. const struct devlink_resource_ops *resource_ops)
  2739. {
  2740. struct devlink_resource *resource;
  2741. struct list_head *resource_list;
  2742. int err = 0;
  2743. mutex_lock(&devlink->lock);
  2744. resource = devlink_resource_find(devlink, NULL, resource_id);
  2745. if (resource) {
  2746. err = -EINVAL;
  2747. goto out;
  2748. }
  2749. resource = kzalloc(sizeof(*resource), GFP_KERNEL);
  2750. if (!resource) {
  2751. err = -ENOMEM;
  2752. goto out;
  2753. }
  2754. if (top_hierarchy) {
  2755. resource_list = &devlink->resource_list;
  2756. } else {
  2757. struct devlink_resource *parent_resource;
  2758. parent_resource = devlink_resource_find(devlink, NULL,
  2759. parent_resource_id);
  2760. if (parent_resource) {
  2761. resource_list = &parent_resource->resource_list;
  2762. resource->parent = parent_resource;
  2763. } else {
  2764. kfree(resource);
  2765. err = -EINVAL;
  2766. goto out;
  2767. }
  2768. }
  2769. resource->name = resource_name;
  2770. resource->size = resource_size;
  2771. resource->size_new = resource_size;
  2772. resource->id = resource_id;
  2773. resource->resource_ops = resource_ops;
  2774. resource->size_valid = true;
  2775. memcpy(&resource->size_params, size_params,
  2776. sizeof(resource->size_params));
  2777. INIT_LIST_HEAD(&resource->resource_list);
  2778. list_add_tail(&resource->list, resource_list);
  2779. out:
  2780. mutex_unlock(&devlink->lock);
  2781. return err;
  2782. }
  2783. EXPORT_SYMBOL_GPL(devlink_resource_register);
  2784. /**
  2785. * devlink_resources_unregister - free all resources
  2786. *
  2787. * @devlink: devlink
  2788. * @resource: resource
  2789. */
  2790. void devlink_resources_unregister(struct devlink *devlink,
  2791. struct devlink_resource *resource)
  2792. {
  2793. struct devlink_resource *tmp, *child_resource;
  2794. struct list_head *resource_list;
  2795. if (resource)
  2796. resource_list = &resource->resource_list;
  2797. else
  2798. resource_list = &devlink->resource_list;
  2799. if (!resource)
  2800. mutex_lock(&devlink->lock);
  2801. list_for_each_entry_safe(child_resource, tmp, resource_list, list) {
  2802. devlink_resources_unregister(devlink, child_resource);
  2803. list_del(&child_resource->list);
  2804. kfree(child_resource);
  2805. }
  2806. if (!resource)
  2807. mutex_unlock(&devlink->lock);
  2808. }
  2809. EXPORT_SYMBOL_GPL(devlink_resources_unregister);
  2810. /**
  2811. * devlink_resource_size_get - get and update size
  2812. *
  2813. * @devlink: devlink
  2814. * @resource_id: the requested resource id
  2815. * @p_resource_size: ptr to update
  2816. */
  2817. int devlink_resource_size_get(struct devlink *devlink,
  2818. u64 resource_id,
  2819. u64 *p_resource_size)
  2820. {
  2821. struct devlink_resource *resource;
  2822. int err = 0;
  2823. mutex_lock(&devlink->lock);
  2824. resource = devlink_resource_find(devlink, NULL, resource_id);
  2825. if (!resource) {
  2826. err = -EINVAL;
  2827. goto out;
  2828. }
  2829. *p_resource_size = resource->size_new;
  2830. resource->size = resource->size_new;
  2831. out:
  2832. mutex_unlock(&devlink->lock);
  2833. return err;
  2834. }
  2835. EXPORT_SYMBOL_GPL(devlink_resource_size_get);
  2836. /**
  2837. * devlink_dpipe_table_resource_set - set the resource id
  2838. *
  2839. * @devlink: devlink
  2840. * @table_name: table name
  2841. * @resource_id: resource id
  2842. * @resource_units: number of resource's units consumed per table's entry
  2843. */
  2844. int devlink_dpipe_table_resource_set(struct devlink *devlink,
  2845. const char *table_name, u64 resource_id,
  2846. u64 resource_units)
  2847. {
  2848. struct devlink_dpipe_table *table;
  2849. int err = 0;
  2850. mutex_lock(&devlink->lock);
  2851. table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
  2852. table_name);
  2853. if (!table) {
  2854. err = -EINVAL;
  2855. goto out;
  2856. }
  2857. table->resource_id = resource_id;
  2858. table->resource_units = resource_units;
  2859. table->resource_valid = true;
  2860. out:
  2861. mutex_unlock(&devlink->lock);
  2862. return err;
  2863. }
  2864. EXPORT_SYMBOL_GPL(devlink_dpipe_table_resource_set);
  2865. static int __init devlink_module_init(void)
  2866. {
  2867. return genl_register_family(&devlink_nl_family);
  2868. }
  2869. static void __exit devlink_module_exit(void)
  2870. {
  2871. genl_unregister_family(&devlink_nl_family);
  2872. }
  2873. module_init(devlink_module_init);
  2874. module_exit(devlink_module_exit);
  2875. MODULE_LICENSE("GPL v2");
  2876. MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
  2877. MODULE_DESCRIPTION("Network physical device Netlink interface");
  2878. MODULE_ALIAS_GENL_FAMILY(DEVLINK_GENL_NAME);