devlink.c 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755
  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. EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwmsg);
  31. static LIST_HEAD(devlink_list);
  32. /* devlink_mutex
  33. *
  34. * An overall lock guarding every operation coming from userspace.
  35. * It also guards devlink devices list and it is taken when
  36. * driver registers/unregisters it.
  37. */
  38. static DEFINE_MUTEX(devlink_mutex);
  39. /* devlink_port_mutex
  40. *
  41. * Shared lock to guard lists of ports in all devlink devices.
  42. */
  43. static DEFINE_MUTEX(devlink_port_mutex);
  44. static struct net *devlink_net(const struct devlink *devlink)
  45. {
  46. return read_pnet(&devlink->_net);
  47. }
  48. static void devlink_net_set(struct devlink *devlink, struct net *net)
  49. {
  50. write_pnet(&devlink->_net, net);
  51. }
  52. static struct devlink *devlink_get_from_attrs(struct net *net,
  53. struct nlattr **attrs)
  54. {
  55. struct devlink *devlink;
  56. char *busname;
  57. char *devname;
  58. if (!attrs[DEVLINK_ATTR_BUS_NAME] || !attrs[DEVLINK_ATTR_DEV_NAME])
  59. return ERR_PTR(-EINVAL);
  60. busname = nla_data(attrs[DEVLINK_ATTR_BUS_NAME]);
  61. devname = nla_data(attrs[DEVLINK_ATTR_DEV_NAME]);
  62. list_for_each_entry(devlink, &devlink_list, list) {
  63. if (strcmp(devlink->dev->bus->name, busname) == 0 &&
  64. strcmp(dev_name(devlink->dev), devname) == 0 &&
  65. net_eq(devlink_net(devlink), net))
  66. return devlink;
  67. }
  68. return ERR_PTR(-ENODEV);
  69. }
  70. static struct devlink *devlink_get_from_info(struct genl_info *info)
  71. {
  72. return devlink_get_from_attrs(genl_info_net(info), info->attrs);
  73. }
  74. static struct devlink_port *devlink_port_get_by_index(struct devlink *devlink,
  75. int port_index)
  76. {
  77. struct devlink_port *devlink_port;
  78. list_for_each_entry(devlink_port, &devlink->port_list, list) {
  79. if (devlink_port->index == port_index)
  80. return devlink_port;
  81. }
  82. return NULL;
  83. }
  84. static bool devlink_port_index_exists(struct devlink *devlink, int port_index)
  85. {
  86. return devlink_port_get_by_index(devlink, port_index);
  87. }
  88. static struct devlink_port *devlink_port_get_from_attrs(struct devlink *devlink,
  89. struct nlattr **attrs)
  90. {
  91. if (attrs[DEVLINK_ATTR_PORT_INDEX]) {
  92. u32 port_index = nla_get_u32(attrs[DEVLINK_ATTR_PORT_INDEX]);
  93. struct devlink_port *devlink_port;
  94. devlink_port = devlink_port_get_by_index(devlink, port_index);
  95. if (!devlink_port)
  96. return ERR_PTR(-ENODEV);
  97. return devlink_port;
  98. }
  99. return ERR_PTR(-EINVAL);
  100. }
  101. static struct devlink_port *devlink_port_get_from_info(struct devlink *devlink,
  102. struct genl_info *info)
  103. {
  104. return devlink_port_get_from_attrs(devlink, info->attrs);
  105. }
  106. struct devlink_sb {
  107. struct list_head list;
  108. unsigned int index;
  109. u32 size;
  110. u16 ingress_pools_count;
  111. u16 egress_pools_count;
  112. u16 ingress_tc_count;
  113. u16 egress_tc_count;
  114. };
  115. static u16 devlink_sb_pool_count(struct devlink_sb *devlink_sb)
  116. {
  117. return devlink_sb->ingress_pools_count + devlink_sb->egress_pools_count;
  118. }
  119. static struct devlink_sb *devlink_sb_get_by_index(struct devlink *devlink,
  120. unsigned int sb_index)
  121. {
  122. struct devlink_sb *devlink_sb;
  123. list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
  124. if (devlink_sb->index == sb_index)
  125. return devlink_sb;
  126. }
  127. return NULL;
  128. }
  129. static bool devlink_sb_index_exists(struct devlink *devlink,
  130. unsigned int sb_index)
  131. {
  132. return devlink_sb_get_by_index(devlink, sb_index);
  133. }
  134. static struct devlink_sb *devlink_sb_get_from_attrs(struct devlink *devlink,
  135. struct nlattr **attrs)
  136. {
  137. if (attrs[DEVLINK_ATTR_SB_INDEX]) {
  138. u32 sb_index = nla_get_u32(attrs[DEVLINK_ATTR_SB_INDEX]);
  139. struct devlink_sb *devlink_sb;
  140. devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
  141. if (!devlink_sb)
  142. return ERR_PTR(-ENODEV);
  143. return devlink_sb;
  144. }
  145. return ERR_PTR(-EINVAL);
  146. }
  147. static struct devlink_sb *devlink_sb_get_from_info(struct devlink *devlink,
  148. struct genl_info *info)
  149. {
  150. return devlink_sb_get_from_attrs(devlink, info->attrs);
  151. }
  152. static int devlink_sb_pool_index_get_from_attrs(struct devlink_sb *devlink_sb,
  153. struct nlattr **attrs,
  154. u16 *p_pool_index)
  155. {
  156. u16 val;
  157. if (!attrs[DEVLINK_ATTR_SB_POOL_INDEX])
  158. return -EINVAL;
  159. val = nla_get_u16(attrs[DEVLINK_ATTR_SB_POOL_INDEX]);
  160. if (val >= devlink_sb_pool_count(devlink_sb))
  161. return -EINVAL;
  162. *p_pool_index = val;
  163. return 0;
  164. }
  165. static int devlink_sb_pool_index_get_from_info(struct devlink_sb *devlink_sb,
  166. struct genl_info *info,
  167. u16 *p_pool_index)
  168. {
  169. return devlink_sb_pool_index_get_from_attrs(devlink_sb, info->attrs,
  170. p_pool_index);
  171. }
  172. static int
  173. devlink_sb_pool_type_get_from_attrs(struct nlattr **attrs,
  174. enum devlink_sb_pool_type *p_pool_type)
  175. {
  176. u8 val;
  177. if (!attrs[DEVLINK_ATTR_SB_POOL_TYPE])
  178. return -EINVAL;
  179. val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_TYPE]);
  180. if (val != DEVLINK_SB_POOL_TYPE_INGRESS &&
  181. val != DEVLINK_SB_POOL_TYPE_EGRESS)
  182. return -EINVAL;
  183. *p_pool_type = val;
  184. return 0;
  185. }
  186. static int
  187. devlink_sb_pool_type_get_from_info(struct genl_info *info,
  188. enum devlink_sb_pool_type *p_pool_type)
  189. {
  190. return devlink_sb_pool_type_get_from_attrs(info->attrs, p_pool_type);
  191. }
  192. static int
  193. devlink_sb_th_type_get_from_attrs(struct nlattr **attrs,
  194. enum devlink_sb_threshold_type *p_th_type)
  195. {
  196. u8 val;
  197. if (!attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE])
  198. return -EINVAL;
  199. val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE]);
  200. if (val != DEVLINK_SB_THRESHOLD_TYPE_STATIC &&
  201. val != DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC)
  202. return -EINVAL;
  203. *p_th_type = val;
  204. return 0;
  205. }
  206. static int
  207. devlink_sb_th_type_get_from_info(struct genl_info *info,
  208. enum devlink_sb_threshold_type *p_th_type)
  209. {
  210. return devlink_sb_th_type_get_from_attrs(info->attrs, p_th_type);
  211. }
  212. static int
  213. devlink_sb_tc_index_get_from_attrs(struct devlink_sb *devlink_sb,
  214. struct nlattr **attrs,
  215. enum devlink_sb_pool_type pool_type,
  216. u16 *p_tc_index)
  217. {
  218. u16 val;
  219. if (!attrs[DEVLINK_ATTR_SB_TC_INDEX])
  220. return -EINVAL;
  221. val = nla_get_u16(attrs[DEVLINK_ATTR_SB_TC_INDEX]);
  222. if (pool_type == DEVLINK_SB_POOL_TYPE_INGRESS &&
  223. val >= devlink_sb->ingress_tc_count)
  224. return -EINVAL;
  225. if (pool_type == DEVLINK_SB_POOL_TYPE_EGRESS &&
  226. val >= devlink_sb->egress_tc_count)
  227. return -EINVAL;
  228. *p_tc_index = val;
  229. return 0;
  230. }
  231. static int
  232. devlink_sb_tc_index_get_from_info(struct devlink_sb *devlink_sb,
  233. struct genl_info *info,
  234. enum devlink_sb_pool_type pool_type,
  235. u16 *p_tc_index)
  236. {
  237. return devlink_sb_tc_index_get_from_attrs(devlink_sb, info->attrs,
  238. pool_type, p_tc_index);
  239. }
  240. #define DEVLINK_NL_FLAG_NEED_DEVLINK BIT(0)
  241. #define DEVLINK_NL_FLAG_NEED_PORT BIT(1)
  242. #define DEVLINK_NL_FLAG_NEED_SB BIT(2)
  243. #define DEVLINK_NL_FLAG_LOCK_PORTS BIT(3)
  244. /* port is not needed but we need to ensure they don't
  245. * change in the middle of command
  246. */
  247. static int devlink_nl_pre_doit(const struct genl_ops *ops,
  248. struct sk_buff *skb, struct genl_info *info)
  249. {
  250. struct devlink *devlink;
  251. mutex_lock(&devlink_mutex);
  252. devlink = devlink_get_from_info(info);
  253. if (IS_ERR(devlink)) {
  254. mutex_unlock(&devlink_mutex);
  255. return PTR_ERR(devlink);
  256. }
  257. if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_DEVLINK) {
  258. info->user_ptr[0] = devlink;
  259. } else if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_PORT) {
  260. struct devlink_port *devlink_port;
  261. mutex_lock(&devlink_port_mutex);
  262. devlink_port = devlink_port_get_from_info(devlink, info);
  263. if (IS_ERR(devlink_port)) {
  264. mutex_unlock(&devlink_port_mutex);
  265. mutex_unlock(&devlink_mutex);
  266. return PTR_ERR(devlink_port);
  267. }
  268. info->user_ptr[0] = devlink_port;
  269. }
  270. if (ops->internal_flags & DEVLINK_NL_FLAG_LOCK_PORTS) {
  271. mutex_lock(&devlink_port_mutex);
  272. }
  273. if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_SB) {
  274. struct devlink_sb *devlink_sb;
  275. devlink_sb = devlink_sb_get_from_info(devlink, info);
  276. if (IS_ERR(devlink_sb)) {
  277. if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_PORT)
  278. mutex_unlock(&devlink_port_mutex);
  279. mutex_unlock(&devlink_mutex);
  280. return PTR_ERR(devlink_sb);
  281. }
  282. info->user_ptr[1] = devlink_sb;
  283. }
  284. return 0;
  285. }
  286. static void devlink_nl_post_doit(const struct genl_ops *ops,
  287. struct sk_buff *skb, struct genl_info *info)
  288. {
  289. if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_PORT ||
  290. ops->internal_flags & DEVLINK_NL_FLAG_LOCK_PORTS)
  291. mutex_unlock(&devlink_port_mutex);
  292. mutex_unlock(&devlink_mutex);
  293. }
  294. static struct genl_family devlink_nl_family;
  295. enum devlink_multicast_groups {
  296. DEVLINK_MCGRP_CONFIG,
  297. };
  298. static const struct genl_multicast_group devlink_nl_mcgrps[] = {
  299. [DEVLINK_MCGRP_CONFIG] = { .name = DEVLINK_GENL_MCGRP_CONFIG_NAME },
  300. };
  301. static int devlink_nl_put_handle(struct sk_buff *msg, struct devlink *devlink)
  302. {
  303. if (nla_put_string(msg, DEVLINK_ATTR_BUS_NAME, devlink->dev->bus->name))
  304. return -EMSGSIZE;
  305. if (nla_put_string(msg, DEVLINK_ATTR_DEV_NAME, dev_name(devlink->dev)))
  306. return -EMSGSIZE;
  307. return 0;
  308. }
  309. static int devlink_nl_fill(struct sk_buff *msg, struct devlink *devlink,
  310. enum devlink_command cmd, u32 portid,
  311. u32 seq, int flags)
  312. {
  313. void *hdr;
  314. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  315. if (!hdr)
  316. return -EMSGSIZE;
  317. if (devlink_nl_put_handle(msg, devlink))
  318. goto nla_put_failure;
  319. genlmsg_end(msg, hdr);
  320. return 0;
  321. nla_put_failure:
  322. genlmsg_cancel(msg, hdr);
  323. return -EMSGSIZE;
  324. }
  325. static void devlink_notify(struct devlink *devlink, enum devlink_command cmd)
  326. {
  327. struct sk_buff *msg;
  328. int err;
  329. WARN_ON(cmd != DEVLINK_CMD_NEW && cmd != DEVLINK_CMD_DEL);
  330. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  331. if (!msg)
  332. return;
  333. err = devlink_nl_fill(msg, devlink, cmd, 0, 0, 0);
  334. if (err) {
  335. nlmsg_free(msg);
  336. return;
  337. }
  338. genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
  339. msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
  340. }
  341. static int devlink_nl_port_fill(struct sk_buff *msg, struct devlink *devlink,
  342. struct devlink_port *devlink_port,
  343. enum devlink_command cmd, u32 portid,
  344. u32 seq, int flags)
  345. {
  346. void *hdr;
  347. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  348. if (!hdr)
  349. return -EMSGSIZE;
  350. if (devlink_nl_put_handle(msg, devlink))
  351. goto nla_put_failure;
  352. if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
  353. goto nla_put_failure;
  354. if (nla_put_u16(msg, DEVLINK_ATTR_PORT_TYPE, devlink_port->type))
  355. goto nla_put_failure;
  356. if (devlink_port->desired_type != DEVLINK_PORT_TYPE_NOTSET &&
  357. nla_put_u16(msg, DEVLINK_ATTR_PORT_DESIRED_TYPE,
  358. devlink_port->desired_type))
  359. goto nla_put_failure;
  360. if (devlink_port->type == DEVLINK_PORT_TYPE_ETH) {
  361. struct net_device *netdev = devlink_port->type_dev;
  362. if (netdev &&
  363. (nla_put_u32(msg, DEVLINK_ATTR_PORT_NETDEV_IFINDEX,
  364. netdev->ifindex) ||
  365. nla_put_string(msg, DEVLINK_ATTR_PORT_NETDEV_NAME,
  366. netdev->name)))
  367. goto nla_put_failure;
  368. }
  369. if (devlink_port->type == DEVLINK_PORT_TYPE_IB) {
  370. struct ib_device *ibdev = devlink_port->type_dev;
  371. if (ibdev &&
  372. nla_put_string(msg, DEVLINK_ATTR_PORT_IBDEV_NAME,
  373. ibdev->name))
  374. goto nla_put_failure;
  375. }
  376. if (devlink_port->split &&
  377. nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_GROUP,
  378. devlink_port->split_group))
  379. goto nla_put_failure;
  380. genlmsg_end(msg, hdr);
  381. return 0;
  382. nla_put_failure:
  383. genlmsg_cancel(msg, hdr);
  384. return -EMSGSIZE;
  385. }
  386. static void devlink_port_notify(struct devlink_port *devlink_port,
  387. enum devlink_command cmd)
  388. {
  389. struct devlink *devlink = devlink_port->devlink;
  390. struct sk_buff *msg;
  391. int err;
  392. if (!devlink_port->registered)
  393. return;
  394. WARN_ON(cmd != DEVLINK_CMD_PORT_NEW && cmd != DEVLINK_CMD_PORT_DEL);
  395. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  396. if (!msg)
  397. return;
  398. err = devlink_nl_port_fill(msg, devlink, devlink_port, cmd, 0, 0, 0);
  399. if (err) {
  400. nlmsg_free(msg);
  401. return;
  402. }
  403. genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
  404. msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
  405. }
  406. static int devlink_nl_cmd_get_doit(struct sk_buff *skb, struct genl_info *info)
  407. {
  408. struct devlink *devlink = info->user_ptr[0];
  409. struct sk_buff *msg;
  410. int err;
  411. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  412. if (!msg)
  413. return -ENOMEM;
  414. err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
  415. info->snd_portid, info->snd_seq, 0);
  416. if (err) {
  417. nlmsg_free(msg);
  418. return err;
  419. }
  420. return genlmsg_reply(msg, info);
  421. }
  422. static int devlink_nl_cmd_get_dumpit(struct sk_buff *msg,
  423. struct netlink_callback *cb)
  424. {
  425. struct devlink *devlink;
  426. int start = cb->args[0];
  427. int idx = 0;
  428. int err;
  429. mutex_lock(&devlink_mutex);
  430. list_for_each_entry(devlink, &devlink_list, list) {
  431. if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
  432. continue;
  433. if (idx < start) {
  434. idx++;
  435. continue;
  436. }
  437. err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
  438. NETLINK_CB(cb->skb).portid,
  439. cb->nlh->nlmsg_seq, NLM_F_MULTI);
  440. if (err)
  441. goto out;
  442. idx++;
  443. }
  444. out:
  445. mutex_unlock(&devlink_mutex);
  446. cb->args[0] = idx;
  447. return msg->len;
  448. }
  449. static int devlink_nl_cmd_port_get_doit(struct sk_buff *skb,
  450. struct genl_info *info)
  451. {
  452. struct devlink_port *devlink_port = info->user_ptr[0];
  453. struct devlink *devlink = devlink_port->devlink;
  454. struct sk_buff *msg;
  455. int err;
  456. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  457. if (!msg)
  458. return -ENOMEM;
  459. err = devlink_nl_port_fill(msg, devlink, devlink_port,
  460. DEVLINK_CMD_PORT_NEW,
  461. info->snd_portid, info->snd_seq, 0);
  462. if (err) {
  463. nlmsg_free(msg);
  464. return err;
  465. }
  466. return genlmsg_reply(msg, info);
  467. }
  468. static int devlink_nl_cmd_port_get_dumpit(struct sk_buff *msg,
  469. struct netlink_callback *cb)
  470. {
  471. struct devlink *devlink;
  472. struct devlink_port *devlink_port;
  473. int start = cb->args[0];
  474. int idx = 0;
  475. int err;
  476. mutex_lock(&devlink_mutex);
  477. mutex_lock(&devlink_port_mutex);
  478. list_for_each_entry(devlink, &devlink_list, list) {
  479. if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
  480. continue;
  481. list_for_each_entry(devlink_port, &devlink->port_list, list) {
  482. if (idx < start) {
  483. idx++;
  484. continue;
  485. }
  486. err = devlink_nl_port_fill(msg, devlink, devlink_port,
  487. DEVLINK_CMD_NEW,
  488. NETLINK_CB(cb->skb).portid,
  489. cb->nlh->nlmsg_seq,
  490. NLM_F_MULTI);
  491. if (err)
  492. goto out;
  493. idx++;
  494. }
  495. }
  496. out:
  497. mutex_unlock(&devlink_port_mutex);
  498. mutex_unlock(&devlink_mutex);
  499. cb->args[0] = idx;
  500. return msg->len;
  501. }
  502. static int devlink_port_type_set(struct devlink *devlink,
  503. struct devlink_port *devlink_port,
  504. enum devlink_port_type port_type)
  505. {
  506. int err;
  507. if (devlink->ops && devlink->ops->port_type_set) {
  508. if (port_type == DEVLINK_PORT_TYPE_NOTSET)
  509. return -EINVAL;
  510. if (port_type == devlink_port->type)
  511. return 0;
  512. err = devlink->ops->port_type_set(devlink_port, port_type);
  513. if (err)
  514. return err;
  515. devlink_port->desired_type = port_type;
  516. devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
  517. return 0;
  518. }
  519. return -EOPNOTSUPP;
  520. }
  521. static int devlink_nl_cmd_port_set_doit(struct sk_buff *skb,
  522. struct genl_info *info)
  523. {
  524. struct devlink_port *devlink_port = info->user_ptr[0];
  525. struct devlink *devlink = devlink_port->devlink;
  526. int err;
  527. if (info->attrs[DEVLINK_ATTR_PORT_TYPE]) {
  528. enum devlink_port_type port_type;
  529. port_type = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_TYPE]);
  530. err = devlink_port_type_set(devlink, devlink_port, port_type);
  531. if (err)
  532. return err;
  533. }
  534. return 0;
  535. }
  536. static int devlink_port_split(struct devlink *devlink,
  537. u32 port_index, u32 count)
  538. {
  539. if (devlink->ops && devlink->ops->port_split)
  540. return devlink->ops->port_split(devlink, port_index, count);
  541. return -EOPNOTSUPP;
  542. }
  543. static int devlink_nl_cmd_port_split_doit(struct sk_buff *skb,
  544. struct genl_info *info)
  545. {
  546. struct devlink *devlink = info->user_ptr[0];
  547. u32 port_index;
  548. u32 count;
  549. if (!info->attrs[DEVLINK_ATTR_PORT_INDEX] ||
  550. !info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT])
  551. return -EINVAL;
  552. port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
  553. count = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT]);
  554. return devlink_port_split(devlink, port_index, count);
  555. }
  556. static int devlink_port_unsplit(struct devlink *devlink, u32 port_index)
  557. {
  558. if (devlink->ops && devlink->ops->port_unsplit)
  559. return devlink->ops->port_unsplit(devlink, port_index);
  560. return -EOPNOTSUPP;
  561. }
  562. static int devlink_nl_cmd_port_unsplit_doit(struct sk_buff *skb,
  563. struct genl_info *info)
  564. {
  565. struct devlink *devlink = info->user_ptr[0];
  566. u32 port_index;
  567. if (!info->attrs[DEVLINK_ATTR_PORT_INDEX])
  568. return -EINVAL;
  569. port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
  570. return devlink_port_unsplit(devlink, port_index);
  571. }
  572. static int devlink_nl_sb_fill(struct sk_buff *msg, struct devlink *devlink,
  573. struct devlink_sb *devlink_sb,
  574. enum devlink_command cmd, u32 portid,
  575. u32 seq, int flags)
  576. {
  577. void *hdr;
  578. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  579. if (!hdr)
  580. return -EMSGSIZE;
  581. if (devlink_nl_put_handle(msg, devlink))
  582. goto nla_put_failure;
  583. if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
  584. goto nla_put_failure;
  585. if (nla_put_u32(msg, DEVLINK_ATTR_SB_SIZE, devlink_sb->size))
  586. goto nla_put_failure;
  587. if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_POOL_COUNT,
  588. devlink_sb->ingress_pools_count))
  589. goto nla_put_failure;
  590. if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_POOL_COUNT,
  591. devlink_sb->egress_pools_count))
  592. goto nla_put_failure;
  593. if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_TC_COUNT,
  594. devlink_sb->ingress_tc_count))
  595. goto nla_put_failure;
  596. if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_TC_COUNT,
  597. devlink_sb->egress_tc_count))
  598. goto nla_put_failure;
  599. genlmsg_end(msg, hdr);
  600. return 0;
  601. nla_put_failure:
  602. genlmsg_cancel(msg, hdr);
  603. return -EMSGSIZE;
  604. }
  605. static int devlink_nl_cmd_sb_get_doit(struct sk_buff *skb,
  606. struct genl_info *info)
  607. {
  608. struct devlink *devlink = info->user_ptr[0];
  609. struct devlink_sb *devlink_sb = info->user_ptr[1];
  610. struct sk_buff *msg;
  611. int err;
  612. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  613. if (!msg)
  614. return -ENOMEM;
  615. err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
  616. DEVLINK_CMD_SB_NEW,
  617. info->snd_portid, info->snd_seq, 0);
  618. if (err) {
  619. nlmsg_free(msg);
  620. return err;
  621. }
  622. return genlmsg_reply(msg, info);
  623. }
  624. static int devlink_nl_cmd_sb_get_dumpit(struct sk_buff *msg,
  625. struct netlink_callback *cb)
  626. {
  627. struct devlink *devlink;
  628. struct devlink_sb *devlink_sb;
  629. int start = cb->args[0];
  630. int idx = 0;
  631. int err;
  632. mutex_lock(&devlink_mutex);
  633. list_for_each_entry(devlink, &devlink_list, list) {
  634. if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
  635. continue;
  636. list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
  637. if (idx < start) {
  638. idx++;
  639. continue;
  640. }
  641. err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
  642. DEVLINK_CMD_SB_NEW,
  643. NETLINK_CB(cb->skb).portid,
  644. cb->nlh->nlmsg_seq,
  645. NLM_F_MULTI);
  646. if (err)
  647. goto out;
  648. idx++;
  649. }
  650. }
  651. out:
  652. mutex_unlock(&devlink_mutex);
  653. cb->args[0] = idx;
  654. return msg->len;
  655. }
  656. static int devlink_nl_sb_pool_fill(struct sk_buff *msg, struct devlink *devlink,
  657. struct devlink_sb *devlink_sb,
  658. u16 pool_index, enum devlink_command cmd,
  659. u32 portid, u32 seq, int flags)
  660. {
  661. struct devlink_sb_pool_info pool_info;
  662. void *hdr;
  663. int err;
  664. err = devlink->ops->sb_pool_get(devlink, devlink_sb->index,
  665. pool_index, &pool_info);
  666. if (err)
  667. return err;
  668. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  669. if (!hdr)
  670. return -EMSGSIZE;
  671. if (devlink_nl_put_handle(msg, devlink))
  672. goto nla_put_failure;
  673. if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
  674. goto nla_put_failure;
  675. if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
  676. goto nla_put_failure;
  677. if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_info.pool_type))
  678. goto nla_put_failure;
  679. if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_SIZE, pool_info.size))
  680. goto nla_put_failure;
  681. if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE,
  682. pool_info.threshold_type))
  683. goto nla_put_failure;
  684. genlmsg_end(msg, hdr);
  685. return 0;
  686. nla_put_failure:
  687. genlmsg_cancel(msg, hdr);
  688. return -EMSGSIZE;
  689. }
  690. static int devlink_nl_cmd_sb_pool_get_doit(struct sk_buff *skb,
  691. struct genl_info *info)
  692. {
  693. struct devlink *devlink = info->user_ptr[0];
  694. struct devlink_sb *devlink_sb = info->user_ptr[1];
  695. struct sk_buff *msg;
  696. u16 pool_index;
  697. int err;
  698. err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
  699. &pool_index);
  700. if (err)
  701. return err;
  702. if (!devlink->ops || !devlink->ops->sb_pool_get)
  703. return -EOPNOTSUPP;
  704. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  705. if (!msg)
  706. return -ENOMEM;
  707. err = devlink_nl_sb_pool_fill(msg, devlink, devlink_sb, pool_index,
  708. DEVLINK_CMD_SB_POOL_NEW,
  709. info->snd_portid, info->snd_seq, 0);
  710. if (err) {
  711. nlmsg_free(msg);
  712. return err;
  713. }
  714. return genlmsg_reply(msg, info);
  715. }
  716. static int __sb_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
  717. struct devlink *devlink,
  718. struct devlink_sb *devlink_sb,
  719. u32 portid, u32 seq)
  720. {
  721. u16 pool_count = devlink_sb_pool_count(devlink_sb);
  722. u16 pool_index;
  723. int err;
  724. for (pool_index = 0; pool_index < pool_count; pool_index++) {
  725. if (*p_idx < start) {
  726. (*p_idx)++;
  727. continue;
  728. }
  729. err = devlink_nl_sb_pool_fill(msg, devlink,
  730. devlink_sb,
  731. pool_index,
  732. DEVLINK_CMD_SB_POOL_NEW,
  733. portid, seq, NLM_F_MULTI);
  734. if (err)
  735. return err;
  736. (*p_idx)++;
  737. }
  738. return 0;
  739. }
  740. static int devlink_nl_cmd_sb_pool_get_dumpit(struct sk_buff *msg,
  741. struct netlink_callback *cb)
  742. {
  743. struct devlink *devlink;
  744. struct devlink_sb *devlink_sb;
  745. int start = cb->args[0];
  746. int idx = 0;
  747. int err;
  748. mutex_lock(&devlink_mutex);
  749. list_for_each_entry(devlink, &devlink_list, list) {
  750. if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
  751. !devlink->ops || !devlink->ops->sb_pool_get)
  752. continue;
  753. list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
  754. err = __sb_pool_get_dumpit(msg, start, &idx, devlink,
  755. devlink_sb,
  756. NETLINK_CB(cb->skb).portid,
  757. cb->nlh->nlmsg_seq);
  758. if (err && err != -EOPNOTSUPP)
  759. goto out;
  760. }
  761. }
  762. out:
  763. mutex_unlock(&devlink_mutex);
  764. cb->args[0] = idx;
  765. return msg->len;
  766. }
  767. static int devlink_sb_pool_set(struct devlink *devlink, unsigned int sb_index,
  768. u16 pool_index, u32 size,
  769. enum devlink_sb_threshold_type threshold_type)
  770. {
  771. const struct devlink_ops *ops = devlink->ops;
  772. if (ops && ops->sb_pool_set)
  773. return ops->sb_pool_set(devlink, sb_index, pool_index,
  774. size, threshold_type);
  775. return -EOPNOTSUPP;
  776. }
  777. static int devlink_nl_cmd_sb_pool_set_doit(struct sk_buff *skb,
  778. struct genl_info *info)
  779. {
  780. struct devlink *devlink = info->user_ptr[0];
  781. struct devlink_sb *devlink_sb = info->user_ptr[1];
  782. enum devlink_sb_threshold_type threshold_type;
  783. u16 pool_index;
  784. u32 size;
  785. int err;
  786. err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
  787. &pool_index);
  788. if (err)
  789. return err;
  790. err = devlink_sb_th_type_get_from_info(info, &threshold_type);
  791. if (err)
  792. return err;
  793. if (!info->attrs[DEVLINK_ATTR_SB_POOL_SIZE])
  794. return -EINVAL;
  795. size = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_POOL_SIZE]);
  796. return devlink_sb_pool_set(devlink, devlink_sb->index,
  797. pool_index, size, threshold_type);
  798. }
  799. static int devlink_nl_sb_port_pool_fill(struct sk_buff *msg,
  800. struct devlink *devlink,
  801. struct devlink_port *devlink_port,
  802. struct devlink_sb *devlink_sb,
  803. u16 pool_index,
  804. enum devlink_command cmd,
  805. u32 portid, u32 seq, int flags)
  806. {
  807. const struct devlink_ops *ops = devlink->ops;
  808. u32 threshold;
  809. void *hdr;
  810. int err;
  811. err = ops->sb_port_pool_get(devlink_port, devlink_sb->index,
  812. pool_index, &threshold);
  813. if (err)
  814. return err;
  815. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  816. if (!hdr)
  817. return -EMSGSIZE;
  818. if (devlink_nl_put_handle(msg, devlink))
  819. goto nla_put_failure;
  820. if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
  821. goto nla_put_failure;
  822. if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
  823. goto nla_put_failure;
  824. if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
  825. goto nla_put_failure;
  826. if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
  827. goto nla_put_failure;
  828. if (ops->sb_occ_port_pool_get) {
  829. u32 cur;
  830. u32 max;
  831. err = ops->sb_occ_port_pool_get(devlink_port, devlink_sb->index,
  832. pool_index, &cur, &max);
  833. if (err && err != -EOPNOTSUPP)
  834. return err;
  835. if (!err) {
  836. if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
  837. goto nla_put_failure;
  838. if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
  839. goto nla_put_failure;
  840. }
  841. }
  842. genlmsg_end(msg, hdr);
  843. return 0;
  844. nla_put_failure:
  845. genlmsg_cancel(msg, hdr);
  846. return -EMSGSIZE;
  847. }
  848. static int devlink_nl_cmd_sb_port_pool_get_doit(struct sk_buff *skb,
  849. struct genl_info *info)
  850. {
  851. struct devlink_port *devlink_port = info->user_ptr[0];
  852. struct devlink *devlink = devlink_port->devlink;
  853. struct devlink_sb *devlink_sb = info->user_ptr[1];
  854. struct sk_buff *msg;
  855. u16 pool_index;
  856. int err;
  857. err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
  858. &pool_index);
  859. if (err)
  860. return err;
  861. if (!devlink->ops || !devlink->ops->sb_port_pool_get)
  862. return -EOPNOTSUPP;
  863. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  864. if (!msg)
  865. return -ENOMEM;
  866. err = devlink_nl_sb_port_pool_fill(msg, devlink, devlink_port,
  867. devlink_sb, pool_index,
  868. DEVLINK_CMD_SB_PORT_POOL_NEW,
  869. info->snd_portid, info->snd_seq, 0);
  870. if (err) {
  871. nlmsg_free(msg);
  872. return err;
  873. }
  874. return genlmsg_reply(msg, info);
  875. }
  876. static int __sb_port_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
  877. struct devlink *devlink,
  878. struct devlink_sb *devlink_sb,
  879. u32 portid, u32 seq)
  880. {
  881. struct devlink_port *devlink_port;
  882. u16 pool_count = devlink_sb_pool_count(devlink_sb);
  883. u16 pool_index;
  884. int err;
  885. list_for_each_entry(devlink_port, &devlink->port_list, list) {
  886. for (pool_index = 0; pool_index < pool_count; pool_index++) {
  887. if (*p_idx < start) {
  888. (*p_idx)++;
  889. continue;
  890. }
  891. err = devlink_nl_sb_port_pool_fill(msg, devlink,
  892. devlink_port,
  893. devlink_sb,
  894. pool_index,
  895. DEVLINK_CMD_SB_PORT_POOL_NEW,
  896. portid, seq,
  897. NLM_F_MULTI);
  898. if (err)
  899. return err;
  900. (*p_idx)++;
  901. }
  902. }
  903. return 0;
  904. }
  905. static int devlink_nl_cmd_sb_port_pool_get_dumpit(struct sk_buff *msg,
  906. struct netlink_callback *cb)
  907. {
  908. struct devlink *devlink;
  909. struct devlink_sb *devlink_sb;
  910. int start = cb->args[0];
  911. int idx = 0;
  912. int err;
  913. mutex_lock(&devlink_mutex);
  914. mutex_lock(&devlink_port_mutex);
  915. list_for_each_entry(devlink, &devlink_list, list) {
  916. if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
  917. !devlink->ops || !devlink->ops->sb_port_pool_get)
  918. continue;
  919. list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
  920. err = __sb_port_pool_get_dumpit(msg, start, &idx,
  921. devlink, devlink_sb,
  922. NETLINK_CB(cb->skb).portid,
  923. cb->nlh->nlmsg_seq);
  924. if (err && err != -EOPNOTSUPP)
  925. goto out;
  926. }
  927. }
  928. out:
  929. mutex_unlock(&devlink_port_mutex);
  930. mutex_unlock(&devlink_mutex);
  931. cb->args[0] = idx;
  932. return msg->len;
  933. }
  934. static int devlink_sb_port_pool_set(struct devlink_port *devlink_port,
  935. unsigned int sb_index, u16 pool_index,
  936. u32 threshold)
  937. {
  938. const struct devlink_ops *ops = devlink_port->devlink->ops;
  939. if (ops && ops->sb_port_pool_set)
  940. return ops->sb_port_pool_set(devlink_port, sb_index,
  941. pool_index, threshold);
  942. return -EOPNOTSUPP;
  943. }
  944. static int devlink_nl_cmd_sb_port_pool_set_doit(struct sk_buff *skb,
  945. struct genl_info *info)
  946. {
  947. struct devlink_port *devlink_port = info->user_ptr[0];
  948. struct devlink_sb *devlink_sb = info->user_ptr[1];
  949. u16 pool_index;
  950. u32 threshold;
  951. int err;
  952. err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
  953. &pool_index);
  954. if (err)
  955. return err;
  956. if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
  957. return -EINVAL;
  958. threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
  959. return devlink_sb_port_pool_set(devlink_port, devlink_sb->index,
  960. pool_index, threshold);
  961. }
  962. static int
  963. devlink_nl_sb_tc_pool_bind_fill(struct sk_buff *msg, struct devlink *devlink,
  964. struct devlink_port *devlink_port,
  965. struct devlink_sb *devlink_sb, u16 tc_index,
  966. enum devlink_sb_pool_type pool_type,
  967. enum devlink_command cmd,
  968. u32 portid, u32 seq, int flags)
  969. {
  970. const struct devlink_ops *ops = devlink->ops;
  971. u16 pool_index;
  972. u32 threshold;
  973. void *hdr;
  974. int err;
  975. err = ops->sb_tc_pool_bind_get(devlink_port, devlink_sb->index,
  976. tc_index, pool_type,
  977. &pool_index, &threshold);
  978. if (err)
  979. return err;
  980. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  981. if (!hdr)
  982. return -EMSGSIZE;
  983. if (devlink_nl_put_handle(msg, devlink))
  984. goto nla_put_failure;
  985. if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
  986. goto nla_put_failure;
  987. if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
  988. goto nla_put_failure;
  989. if (nla_put_u16(msg, DEVLINK_ATTR_SB_TC_INDEX, tc_index))
  990. goto nla_put_failure;
  991. if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_type))
  992. goto nla_put_failure;
  993. if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
  994. goto nla_put_failure;
  995. if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
  996. goto nla_put_failure;
  997. if (ops->sb_occ_tc_port_bind_get) {
  998. u32 cur;
  999. u32 max;
  1000. err = ops->sb_occ_tc_port_bind_get(devlink_port,
  1001. devlink_sb->index,
  1002. tc_index, pool_type,
  1003. &cur, &max);
  1004. if (err && err != -EOPNOTSUPP)
  1005. return err;
  1006. if (!err) {
  1007. if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
  1008. goto nla_put_failure;
  1009. if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
  1010. goto nla_put_failure;
  1011. }
  1012. }
  1013. genlmsg_end(msg, hdr);
  1014. return 0;
  1015. nla_put_failure:
  1016. genlmsg_cancel(msg, hdr);
  1017. return -EMSGSIZE;
  1018. }
  1019. static int devlink_nl_cmd_sb_tc_pool_bind_get_doit(struct sk_buff *skb,
  1020. struct genl_info *info)
  1021. {
  1022. struct devlink_port *devlink_port = info->user_ptr[0];
  1023. struct devlink *devlink = devlink_port->devlink;
  1024. struct devlink_sb *devlink_sb = info->user_ptr[1];
  1025. struct sk_buff *msg;
  1026. enum devlink_sb_pool_type pool_type;
  1027. u16 tc_index;
  1028. int err;
  1029. err = devlink_sb_pool_type_get_from_info(info, &pool_type);
  1030. if (err)
  1031. return err;
  1032. err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
  1033. pool_type, &tc_index);
  1034. if (err)
  1035. return err;
  1036. if (!devlink->ops || !devlink->ops->sb_tc_pool_bind_get)
  1037. return -EOPNOTSUPP;
  1038. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1039. if (!msg)
  1040. return -ENOMEM;
  1041. err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink, devlink_port,
  1042. devlink_sb, tc_index, pool_type,
  1043. DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
  1044. info->snd_portid,
  1045. info->snd_seq, 0);
  1046. if (err) {
  1047. nlmsg_free(msg);
  1048. return err;
  1049. }
  1050. return genlmsg_reply(msg, info);
  1051. }
  1052. static int __sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
  1053. int start, int *p_idx,
  1054. struct devlink *devlink,
  1055. struct devlink_sb *devlink_sb,
  1056. u32 portid, u32 seq)
  1057. {
  1058. struct devlink_port *devlink_port;
  1059. u16 tc_index;
  1060. int err;
  1061. list_for_each_entry(devlink_port, &devlink->port_list, list) {
  1062. for (tc_index = 0;
  1063. tc_index < devlink_sb->ingress_tc_count; tc_index++) {
  1064. if (*p_idx < start) {
  1065. (*p_idx)++;
  1066. continue;
  1067. }
  1068. err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
  1069. devlink_port,
  1070. devlink_sb,
  1071. tc_index,
  1072. DEVLINK_SB_POOL_TYPE_INGRESS,
  1073. DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
  1074. portid, seq,
  1075. NLM_F_MULTI);
  1076. if (err)
  1077. return err;
  1078. (*p_idx)++;
  1079. }
  1080. for (tc_index = 0;
  1081. tc_index < devlink_sb->egress_tc_count; tc_index++) {
  1082. if (*p_idx < start) {
  1083. (*p_idx)++;
  1084. continue;
  1085. }
  1086. err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
  1087. devlink_port,
  1088. devlink_sb,
  1089. tc_index,
  1090. DEVLINK_SB_POOL_TYPE_EGRESS,
  1091. DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
  1092. portid, seq,
  1093. NLM_F_MULTI);
  1094. if (err)
  1095. return err;
  1096. (*p_idx)++;
  1097. }
  1098. }
  1099. return 0;
  1100. }
  1101. static int
  1102. devlink_nl_cmd_sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
  1103. struct netlink_callback *cb)
  1104. {
  1105. struct devlink *devlink;
  1106. struct devlink_sb *devlink_sb;
  1107. int start = cb->args[0];
  1108. int idx = 0;
  1109. int err;
  1110. mutex_lock(&devlink_mutex);
  1111. mutex_lock(&devlink_port_mutex);
  1112. list_for_each_entry(devlink, &devlink_list, list) {
  1113. if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
  1114. !devlink->ops || !devlink->ops->sb_tc_pool_bind_get)
  1115. continue;
  1116. list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
  1117. err = __sb_tc_pool_bind_get_dumpit(msg, start, &idx,
  1118. devlink,
  1119. devlink_sb,
  1120. NETLINK_CB(cb->skb).portid,
  1121. cb->nlh->nlmsg_seq);
  1122. if (err && err != -EOPNOTSUPP)
  1123. goto out;
  1124. }
  1125. }
  1126. out:
  1127. mutex_unlock(&devlink_port_mutex);
  1128. mutex_unlock(&devlink_mutex);
  1129. cb->args[0] = idx;
  1130. return msg->len;
  1131. }
  1132. static int devlink_sb_tc_pool_bind_set(struct devlink_port *devlink_port,
  1133. unsigned int sb_index, u16 tc_index,
  1134. enum devlink_sb_pool_type pool_type,
  1135. u16 pool_index, u32 threshold)
  1136. {
  1137. const struct devlink_ops *ops = devlink_port->devlink->ops;
  1138. if (ops && ops->sb_tc_pool_bind_set)
  1139. return ops->sb_tc_pool_bind_set(devlink_port, sb_index,
  1140. tc_index, pool_type,
  1141. pool_index, threshold);
  1142. return -EOPNOTSUPP;
  1143. }
  1144. static int devlink_nl_cmd_sb_tc_pool_bind_set_doit(struct sk_buff *skb,
  1145. struct genl_info *info)
  1146. {
  1147. struct devlink_port *devlink_port = info->user_ptr[0];
  1148. struct devlink_sb *devlink_sb = info->user_ptr[1];
  1149. enum devlink_sb_pool_type pool_type;
  1150. u16 tc_index;
  1151. u16 pool_index;
  1152. u32 threshold;
  1153. int err;
  1154. err = devlink_sb_pool_type_get_from_info(info, &pool_type);
  1155. if (err)
  1156. return err;
  1157. err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
  1158. pool_type, &tc_index);
  1159. if (err)
  1160. return err;
  1161. err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
  1162. &pool_index);
  1163. if (err)
  1164. return err;
  1165. if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
  1166. return -EINVAL;
  1167. threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
  1168. return devlink_sb_tc_pool_bind_set(devlink_port, devlink_sb->index,
  1169. tc_index, pool_type,
  1170. pool_index, threshold);
  1171. }
  1172. static int devlink_nl_cmd_sb_occ_snapshot_doit(struct sk_buff *skb,
  1173. struct genl_info *info)
  1174. {
  1175. struct devlink *devlink = info->user_ptr[0];
  1176. struct devlink_sb *devlink_sb = info->user_ptr[1];
  1177. const struct devlink_ops *ops = devlink->ops;
  1178. if (ops && ops->sb_occ_snapshot)
  1179. return ops->sb_occ_snapshot(devlink, devlink_sb->index);
  1180. return -EOPNOTSUPP;
  1181. }
  1182. static int devlink_nl_cmd_sb_occ_max_clear_doit(struct sk_buff *skb,
  1183. struct genl_info *info)
  1184. {
  1185. struct devlink *devlink = info->user_ptr[0];
  1186. struct devlink_sb *devlink_sb = info->user_ptr[1];
  1187. const struct devlink_ops *ops = devlink->ops;
  1188. if (ops && ops->sb_occ_max_clear)
  1189. return ops->sb_occ_max_clear(devlink, devlink_sb->index);
  1190. return -EOPNOTSUPP;
  1191. }
  1192. static int devlink_nl_eswitch_fill(struct sk_buff *msg, struct devlink *devlink,
  1193. enum devlink_command cmd, u32 portid,
  1194. u32 seq, int flags)
  1195. {
  1196. const struct devlink_ops *ops = devlink->ops;
  1197. u8 inline_mode, encap_mode;
  1198. void *hdr;
  1199. int err = 0;
  1200. u16 mode;
  1201. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  1202. if (!hdr)
  1203. return -EMSGSIZE;
  1204. err = devlink_nl_put_handle(msg, devlink);
  1205. if (err)
  1206. goto nla_put_failure;
  1207. if (ops->eswitch_mode_get) {
  1208. err = ops->eswitch_mode_get(devlink, &mode);
  1209. if (err)
  1210. goto nla_put_failure;
  1211. err = nla_put_u16(msg, DEVLINK_ATTR_ESWITCH_MODE, mode);
  1212. if (err)
  1213. goto nla_put_failure;
  1214. }
  1215. if (ops->eswitch_inline_mode_get) {
  1216. err = ops->eswitch_inline_mode_get(devlink, &inline_mode);
  1217. if (err)
  1218. goto nla_put_failure;
  1219. err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_INLINE_MODE,
  1220. inline_mode);
  1221. if (err)
  1222. goto nla_put_failure;
  1223. }
  1224. if (ops->eswitch_encap_mode_get) {
  1225. err = ops->eswitch_encap_mode_get(devlink, &encap_mode);
  1226. if (err)
  1227. goto nla_put_failure;
  1228. err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_ENCAP_MODE, encap_mode);
  1229. if (err)
  1230. goto nla_put_failure;
  1231. }
  1232. genlmsg_end(msg, hdr);
  1233. return 0;
  1234. nla_put_failure:
  1235. genlmsg_cancel(msg, hdr);
  1236. return err;
  1237. }
  1238. static int devlink_nl_cmd_eswitch_get_doit(struct sk_buff *skb,
  1239. struct genl_info *info)
  1240. {
  1241. struct devlink *devlink = info->user_ptr[0];
  1242. const struct devlink_ops *ops = devlink->ops;
  1243. struct sk_buff *msg;
  1244. int err;
  1245. if (!ops)
  1246. return -EOPNOTSUPP;
  1247. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1248. if (!msg)
  1249. return -ENOMEM;
  1250. err = devlink_nl_eswitch_fill(msg, devlink, DEVLINK_CMD_ESWITCH_GET,
  1251. info->snd_portid, info->snd_seq, 0);
  1252. if (err) {
  1253. nlmsg_free(msg);
  1254. return err;
  1255. }
  1256. return genlmsg_reply(msg, info);
  1257. }
  1258. static int devlink_nl_cmd_eswitch_set_doit(struct sk_buff *skb,
  1259. struct genl_info *info)
  1260. {
  1261. struct devlink *devlink = info->user_ptr[0];
  1262. const struct devlink_ops *ops = devlink->ops;
  1263. u8 inline_mode, encap_mode;
  1264. int err = 0;
  1265. u16 mode;
  1266. if (!ops)
  1267. return -EOPNOTSUPP;
  1268. if (info->attrs[DEVLINK_ATTR_ESWITCH_MODE]) {
  1269. if (!ops->eswitch_mode_set)
  1270. return -EOPNOTSUPP;
  1271. mode = nla_get_u16(info->attrs[DEVLINK_ATTR_ESWITCH_MODE]);
  1272. err = ops->eswitch_mode_set(devlink, mode);
  1273. if (err)
  1274. return err;
  1275. }
  1276. if (info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]) {
  1277. if (!ops->eswitch_inline_mode_set)
  1278. return -EOPNOTSUPP;
  1279. inline_mode = nla_get_u8(
  1280. info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]);
  1281. err = ops->eswitch_inline_mode_set(devlink, inline_mode);
  1282. if (err)
  1283. return err;
  1284. }
  1285. if (info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]) {
  1286. if (!ops->eswitch_encap_mode_set)
  1287. return -EOPNOTSUPP;
  1288. encap_mode = nla_get_u8(info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]);
  1289. err = ops->eswitch_encap_mode_set(devlink, encap_mode);
  1290. if (err)
  1291. return err;
  1292. }
  1293. return 0;
  1294. }
  1295. int devlink_dpipe_match_put(struct sk_buff *skb,
  1296. struct devlink_dpipe_match *match)
  1297. {
  1298. struct devlink_dpipe_header *header = match->header;
  1299. struct devlink_dpipe_field *field = &header->fields[match->field_id];
  1300. struct nlattr *match_attr;
  1301. match_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_MATCH);
  1302. if (!match_attr)
  1303. return -EMSGSIZE;
  1304. if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_MATCH_TYPE, match->type) ||
  1305. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, match->header_index) ||
  1306. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
  1307. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
  1308. nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
  1309. goto nla_put_failure;
  1310. nla_nest_end(skb, match_attr);
  1311. return 0;
  1312. nla_put_failure:
  1313. nla_nest_cancel(skb, match_attr);
  1314. return -EMSGSIZE;
  1315. }
  1316. EXPORT_SYMBOL_GPL(devlink_dpipe_match_put);
  1317. static int devlink_dpipe_matches_put(struct devlink_dpipe_table *table,
  1318. struct sk_buff *skb)
  1319. {
  1320. struct nlattr *matches_attr;
  1321. matches_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLE_MATCHES);
  1322. if (!matches_attr)
  1323. return -EMSGSIZE;
  1324. if (table->table_ops->matches_dump(table->priv, skb))
  1325. goto nla_put_failure;
  1326. nla_nest_end(skb, matches_attr);
  1327. return 0;
  1328. nla_put_failure:
  1329. nla_nest_cancel(skb, matches_attr);
  1330. return -EMSGSIZE;
  1331. }
  1332. int devlink_dpipe_action_put(struct sk_buff *skb,
  1333. struct devlink_dpipe_action *action)
  1334. {
  1335. struct devlink_dpipe_header *header = action->header;
  1336. struct devlink_dpipe_field *field = &header->fields[action->field_id];
  1337. struct nlattr *action_attr;
  1338. action_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_ACTION);
  1339. if (!action_attr)
  1340. return -EMSGSIZE;
  1341. if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_ACTION_TYPE, action->type) ||
  1342. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, action->header_index) ||
  1343. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
  1344. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
  1345. nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
  1346. goto nla_put_failure;
  1347. nla_nest_end(skb, action_attr);
  1348. return 0;
  1349. nla_put_failure:
  1350. nla_nest_cancel(skb, action_attr);
  1351. return -EMSGSIZE;
  1352. }
  1353. EXPORT_SYMBOL_GPL(devlink_dpipe_action_put);
  1354. static int devlink_dpipe_actions_put(struct devlink_dpipe_table *table,
  1355. struct sk_buff *skb)
  1356. {
  1357. struct nlattr *actions_attr;
  1358. actions_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLE_ACTIONS);
  1359. if (!actions_attr)
  1360. return -EMSGSIZE;
  1361. if (table->table_ops->actions_dump(table->priv, skb))
  1362. goto nla_put_failure;
  1363. nla_nest_end(skb, actions_attr);
  1364. return 0;
  1365. nla_put_failure:
  1366. nla_nest_cancel(skb, actions_attr);
  1367. return -EMSGSIZE;
  1368. }
  1369. static int devlink_dpipe_table_put(struct sk_buff *skb,
  1370. struct devlink_dpipe_table *table)
  1371. {
  1372. struct nlattr *table_attr;
  1373. table_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLE);
  1374. if (!table_attr)
  1375. return -EMSGSIZE;
  1376. if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_TABLE_NAME, table->name) ||
  1377. nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_SIZE, table->size,
  1378. DEVLINK_ATTR_PAD))
  1379. goto nla_put_failure;
  1380. if (nla_put_u8(skb, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED,
  1381. table->counters_enabled))
  1382. goto nla_put_failure;
  1383. if (devlink_dpipe_matches_put(table, skb))
  1384. goto nla_put_failure;
  1385. if (devlink_dpipe_actions_put(table, skb))
  1386. goto nla_put_failure;
  1387. nla_nest_end(skb, table_attr);
  1388. return 0;
  1389. nla_put_failure:
  1390. nla_nest_cancel(skb, table_attr);
  1391. return -EMSGSIZE;
  1392. }
  1393. static int devlink_dpipe_send_and_alloc_skb(struct sk_buff **pskb,
  1394. struct genl_info *info)
  1395. {
  1396. int err;
  1397. if (*pskb) {
  1398. err = genlmsg_reply(*pskb, info);
  1399. if (err)
  1400. return err;
  1401. }
  1402. *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1403. if (!*pskb)
  1404. return -ENOMEM;
  1405. return 0;
  1406. }
  1407. static int devlink_dpipe_tables_fill(struct genl_info *info,
  1408. enum devlink_command cmd, int flags,
  1409. struct list_head *dpipe_tables,
  1410. const char *table_name)
  1411. {
  1412. struct devlink *devlink = info->user_ptr[0];
  1413. struct devlink_dpipe_table *table;
  1414. struct nlattr *tables_attr;
  1415. struct sk_buff *skb = NULL;
  1416. struct nlmsghdr *nlh;
  1417. bool incomplete;
  1418. void *hdr;
  1419. int i;
  1420. int err;
  1421. table = list_first_entry(dpipe_tables,
  1422. struct devlink_dpipe_table, list);
  1423. start_again:
  1424. err = devlink_dpipe_send_and_alloc_skb(&skb, info);
  1425. if (err)
  1426. return err;
  1427. hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
  1428. &devlink_nl_family, NLM_F_MULTI, cmd);
  1429. if (!hdr)
  1430. return -EMSGSIZE;
  1431. if (devlink_nl_put_handle(skb, devlink))
  1432. goto nla_put_failure;
  1433. tables_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLES);
  1434. if (!tables_attr)
  1435. goto nla_put_failure;
  1436. i = 0;
  1437. incomplete = false;
  1438. list_for_each_entry_from(table, dpipe_tables, list) {
  1439. if (!table_name) {
  1440. err = devlink_dpipe_table_put(skb, table);
  1441. if (err) {
  1442. if (!i)
  1443. goto err_table_put;
  1444. incomplete = true;
  1445. break;
  1446. }
  1447. } else {
  1448. if (!strcmp(table->name, table_name)) {
  1449. err = devlink_dpipe_table_put(skb, table);
  1450. if (err)
  1451. break;
  1452. }
  1453. }
  1454. i++;
  1455. }
  1456. nla_nest_end(skb, tables_attr);
  1457. genlmsg_end(skb, hdr);
  1458. if (incomplete)
  1459. goto start_again;
  1460. send_done:
  1461. nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
  1462. NLMSG_DONE, 0, flags | NLM_F_MULTI);
  1463. if (!nlh) {
  1464. err = devlink_dpipe_send_and_alloc_skb(&skb, info);
  1465. if (err)
  1466. goto err_skb_send_alloc;
  1467. goto send_done;
  1468. }
  1469. return genlmsg_reply(skb, info);
  1470. nla_put_failure:
  1471. err = -EMSGSIZE;
  1472. err_table_put:
  1473. err_skb_send_alloc:
  1474. genlmsg_cancel(skb, hdr);
  1475. nlmsg_free(skb);
  1476. return err;
  1477. }
  1478. static int devlink_nl_cmd_dpipe_table_get(struct sk_buff *skb,
  1479. struct genl_info *info)
  1480. {
  1481. struct devlink *devlink = info->user_ptr[0];
  1482. const char *table_name = NULL;
  1483. if (info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
  1484. table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
  1485. return devlink_dpipe_tables_fill(info, DEVLINK_CMD_DPIPE_TABLE_GET, 0,
  1486. &devlink->dpipe_table_list,
  1487. table_name);
  1488. }
  1489. static int devlink_dpipe_value_put(struct sk_buff *skb,
  1490. struct devlink_dpipe_value *value)
  1491. {
  1492. if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE,
  1493. value->value_size, value->value))
  1494. return -EMSGSIZE;
  1495. if (value->mask)
  1496. if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE_MASK,
  1497. value->value_size, value->mask))
  1498. return -EMSGSIZE;
  1499. if (value->mapping_valid)
  1500. if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_VALUE_MAPPING,
  1501. value->mapping_value))
  1502. return -EMSGSIZE;
  1503. return 0;
  1504. }
  1505. static int devlink_dpipe_action_value_put(struct sk_buff *skb,
  1506. struct devlink_dpipe_value *value)
  1507. {
  1508. if (!value->action)
  1509. return -EINVAL;
  1510. if (devlink_dpipe_action_put(skb, value->action))
  1511. return -EMSGSIZE;
  1512. if (devlink_dpipe_value_put(skb, value))
  1513. return -EMSGSIZE;
  1514. return 0;
  1515. }
  1516. static int devlink_dpipe_action_values_put(struct sk_buff *skb,
  1517. struct devlink_dpipe_value *values,
  1518. unsigned int values_count)
  1519. {
  1520. struct nlattr *action_attr;
  1521. int i;
  1522. int err;
  1523. for (i = 0; i < values_count; i++) {
  1524. action_attr = nla_nest_start(skb,
  1525. DEVLINK_ATTR_DPIPE_ACTION_VALUE);
  1526. if (!action_attr)
  1527. return -EMSGSIZE;
  1528. err = devlink_dpipe_action_value_put(skb, &values[i]);
  1529. if (err)
  1530. goto err_action_value_put;
  1531. nla_nest_end(skb, action_attr);
  1532. }
  1533. return 0;
  1534. err_action_value_put:
  1535. nla_nest_cancel(skb, action_attr);
  1536. return err;
  1537. }
  1538. static int devlink_dpipe_match_value_put(struct sk_buff *skb,
  1539. struct devlink_dpipe_value *value)
  1540. {
  1541. if (!value->match)
  1542. return -EINVAL;
  1543. if (devlink_dpipe_match_put(skb, value->match))
  1544. return -EMSGSIZE;
  1545. if (devlink_dpipe_value_put(skb, value))
  1546. return -EMSGSIZE;
  1547. return 0;
  1548. }
  1549. static int devlink_dpipe_match_values_put(struct sk_buff *skb,
  1550. struct devlink_dpipe_value *values,
  1551. unsigned int values_count)
  1552. {
  1553. struct nlattr *match_attr;
  1554. int i;
  1555. int err;
  1556. for (i = 0; i < values_count; i++) {
  1557. match_attr = nla_nest_start(skb,
  1558. DEVLINK_ATTR_DPIPE_MATCH_VALUE);
  1559. if (!match_attr)
  1560. return -EMSGSIZE;
  1561. err = devlink_dpipe_match_value_put(skb, &values[i]);
  1562. if (err)
  1563. goto err_match_value_put;
  1564. nla_nest_end(skb, match_attr);
  1565. }
  1566. return 0;
  1567. err_match_value_put:
  1568. nla_nest_cancel(skb, match_attr);
  1569. return err;
  1570. }
  1571. static int devlink_dpipe_entry_put(struct sk_buff *skb,
  1572. struct devlink_dpipe_entry *entry)
  1573. {
  1574. struct nlattr *entry_attr, *matches_attr, *actions_attr;
  1575. int err;
  1576. entry_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_ENTRY);
  1577. if (!entry_attr)
  1578. return -EMSGSIZE;
  1579. if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_INDEX, entry->index,
  1580. DEVLINK_ATTR_PAD))
  1581. goto nla_put_failure;
  1582. if (entry->counter_valid)
  1583. if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_COUNTER,
  1584. entry->counter, DEVLINK_ATTR_PAD))
  1585. goto nla_put_failure;
  1586. matches_attr = nla_nest_start(skb,
  1587. DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES);
  1588. if (!matches_attr)
  1589. goto nla_put_failure;
  1590. err = devlink_dpipe_match_values_put(skb, entry->match_values,
  1591. entry->match_values_count);
  1592. if (err) {
  1593. nla_nest_cancel(skb, matches_attr);
  1594. goto err_match_values_put;
  1595. }
  1596. nla_nest_end(skb, matches_attr);
  1597. actions_attr = nla_nest_start(skb,
  1598. DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES);
  1599. if (!actions_attr)
  1600. goto nla_put_failure;
  1601. err = devlink_dpipe_action_values_put(skb, entry->action_values,
  1602. entry->action_values_count);
  1603. if (err) {
  1604. nla_nest_cancel(skb, actions_attr);
  1605. goto err_action_values_put;
  1606. }
  1607. nla_nest_end(skb, actions_attr);
  1608. nla_nest_end(skb, entry_attr);
  1609. return 0;
  1610. nla_put_failure:
  1611. err = -EMSGSIZE;
  1612. err_match_values_put:
  1613. err_action_values_put:
  1614. nla_nest_cancel(skb, entry_attr);
  1615. return err;
  1616. }
  1617. static struct devlink_dpipe_table *
  1618. devlink_dpipe_table_find(struct list_head *dpipe_tables,
  1619. const char *table_name)
  1620. {
  1621. struct devlink_dpipe_table *table;
  1622. list_for_each_entry_rcu(table, dpipe_tables, list) {
  1623. if (!strcmp(table->name, table_name))
  1624. return table;
  1625. }
  1626. return NULL;
  1627. }
  1628. int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx)
  1629. {
  1630. struct devlink *devlink;
  1631. int err;
  1632. err = devlink_dpipe_send_and_alloc_skb(&dump_ctx->skb,
  1633. dump_ctx->info);
  1634. if (err)
  1635. return err;
  1636. dump_ctx->hdr = genlmsg_put(dump_ctx->skb,
  1637. dump_ctx->info->snd_portid,
  1638. dump_ctx->info->snd_seq,
  1639. &devlink_nl_family, NLM_F_MULTI,
  1640. dump_ctx->cmd);
  1641. if (!dump_ctx->hdr)
  1642. goto nla_put_failure;
  1643. devlink = dump_ctx->info->user_ptr[0];
  1644. if (devlink_nl_put_handle(dump_ctx->skb, devlink))
  1645. goto nla_put_failure;
  1646. dump_ctx->nest = nla_nest_start(dump_ctx->skb,
  1647. DEVLINK_ATTR_DPIPE_ENTRIES);
  1648. if (!dump_ctx->nest)
  1649. goto nla_put_failure;
  1650. return 0;
  1651. nla_put_failure:
  1652. genlmsg_cancel(dump_ctx->skb, dump_ctx->hdr);
  1653. nlmsg_free(dump_ctx->skb);
  1654. return -EMSGSIZE;
  1655. }
  1656. EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_prepare);
  1657. int devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx,
  1658. struct devlink_dpipe_entry *entry)
  1659. {
  1660. return devlink_dpipe_entry_put(dump_ctx->skb, entry);
  1661. }
  1662. EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_append);
  1663. int devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx)
  1664. {
  1665. nla_nest_end(dump_ctx->skb, dump_ctx->nest);
  1666. genlmsg_end(dump_ctx->skb, dump_ctx->hdr);
  1667. return 0;
  1668. }
  1669. EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_close);
  1670. static int devlink_dpipe_entries_fill(struct genl_info *info,
  1671. enum devlink_command cmd, int flags,
  1672. struct devlink_dpipe_table *table)
  1673. {
  1674. struct devlink_dpipe_dump_ctx dump_ctx;
  1675. struct nlmsghdr *nlh;
  1676. int err;
  1677. dump_ctx.skb = NULL;
  1678. dump_ctx.cmd = cmd;
  1679. dump_ctx.info = info;
  1680. err = table->table_ops->entries_dump(table->priv,
  1681. table->counters_enabled,
  1682. &dump_ctx);
  1683. if (err)
  1684. goto err_entries_dump;
  1685. send_done:
  1686. nlh = nlmsg_put(dump_ctx.skb, info->snd_portid, info->snd_seq,
  1687. NLMSG_DONE, 0, flags | NLM_F_MULTI);
  1688. if (!nlh) {
  1689. err = devlink_dpipe_send_and_alloc_skb(&dump_ctx.skb, info);
  1690. if (err)
  1691. goto err_skb_send_alloc;
  1692. goto send_done;
  1693. }
  1694. return genlmsg_reply(dump_ctx.skb, info);
  1695. err_entries_dump:
  1696. err_skb_send_alloc:
  1697. genlmsg_cancel(dump_ctx.skb, dump_ctx.hdr);
  1698. nlmsg_free(dump_ctx.skb);
  1699. return err;
  1700. }
  1701. static int devlink_nl_cmd_dpipe_entries_get(struct sk_buff *skb,
  1702. struct genl_info *info)
  1703. {
  1704. struct devlink *devlink = info->user_ptr[0];
  1705. struct devlink_dpipe_table *table;
  1706. const char *table_name;
  1707. if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
  1708. return -EINVAL;
  1709. table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
  1710. table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
  1711. table_name);
  1712. if (!table)
  1713. return -EINVAL;
  1714. if (!table->table_ops->entries_dump)
  1715. return -EINVAL;
  1716. return devlink_dpipe_entries_fill(info, DEVLINK_CMD_DPIPE_ENTRIES_GET,
  1717. 0, table);
  1718. }
  1719. static int devlink_dpipe_fields_put(struct sk_buff *skb,
  1720. const struct devlink_dpipe_header *header)
  1721. {
  1722. struct devlink_dpipe_field *field;
  1723. struct nlattr *field_attr;
  1724. int i;
  1725. for (i = 0; i < header->fields_count; i++) {
  1726. field = &header->fields[i];
  1727. field_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_FIELD);
  1728. if (!field_attr)
  1729. return -EMSGSIZE;
  1730. if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_FIELD_NAME, field->name) ||
  1731. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
  1732. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH, field->bitwidth) ||
  1733. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE, field->mapping_type))
  1734. goto nla_put_failure;
  1735. nla_nest_end(skb, field_attr);
  1736. }
  1737. return 0;
  1738. nla_put_failure:
  1739. nla_nest_cancel(skb, field_attr);
  1740. return -EMSGSIZE;
  1741. }
  1742. static int devlink_dpipe_header_put(struct sk_buff *skb,
  1743. struct devlink_dpipe_header *header)
  1744. {
  1745. struct nlattr *fields_attr, *header_attr;
  1746. int err;
  1747. header_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_HEADER);
  1748. if (!header_attr)
  1749. return -EMSGSIZE;
  1750. if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_HEADER_NAME, header->name) ||
  1751. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
  1752. nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
  1753. goto nla_put_failure;
  1754. fields_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_HEADER_FIELDS);
  1755. if (!fields_attr)
  1756. goto nla_put_failure;
  1757. err = devlink_dpipe_fields_put(skb, header);
  1758. if (err) {
  1759. nla_nest_cancel(skb, fields_attr);
  1760. goto nla_put_failure;
  1761. }
  1762. nla_nest_end(skb, fields_attr);
  1763. nla_nest_end(skb, header_attr);
  1764. return 0;
  1765. nla_put_failure:
  1766. err = -EMSGSIZE;
  1767. nla_nest_cancel(skb, header_attr);
  1768. return err;
  1769. }
  1770. static int devlink_dpipe_headers_fill(struct genl_info *info,
  1771. enum devlink_command cmd, int flags,
  1772. struct devlink_dpipe_headers *
  1773. dpipe_headers)
  1774. {
  1775. struct devlink *devlink = info->user_ptr[0];
  1776. struct nlattr *headers_attr;
  1777. struct sk_buff *skb = NULL;
  1778. struct nlmsghdr *nlh;
  1779. void *hdr;
  1780. int i, j;
  1781. int err;
  1782. i = 0;
  1783. start_again:
  1784. err = devlink_dpipe_send_and_alloc_skb(&skb, info);
  1785. if (err)
  1786. return err;
  1787. hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
  1788. &devlink_nl_family, NLM_F_MULTI, cmd);
  1789. if (!hdr)
  1790. return -EMSGSIZE;
  1791. if (devlink_nl_put_handle(skb, devlink))
  1792. goto nla_put_failure;
  1793. headers_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_HEADERS);
  1794. if (!headers_attr)
  1795. goto nla_put_failure;
  1796. j = 0;
  1797. for (; i < dpipe_headers->headers_count; i++) {
  1798. err = devlink_dpipe_header_put(skb, dpipe_headers->headers[i]);
  1799. if (err) {
  1800. if (!j)
  1801. goto err_table_put;
  1802. break;
  1803. }
  1804. j++;
  1805. }
  1806. nla_nest_end(skb, headers_attr);
  1807. genlmsg_end(skb, hdr);
  1808. if (i != dpipe_headers->headers_count)
  1809. goto start_again;
  1810. send_done:
  1811. nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
  1812. NLMSG_DONE, 0, flags | NLM_F_MULTI);
  1813. if (!nlh) {
  1814. err = devlink_dpipe_send_and_alloc_skb(&skb, info);
  1815. if (err)
  1816. goto err_skb_send_alloc;
  1817. goto send_done;
  1818. }
  1819. return genlmsg_reply(skb, info);
  1820. nla_put_failure:
  1821. err = -EMSGSIZE;
  1822. err_table_put:
  1823. err_skb_send_alloc:
  1824. genlmsg_cancel(skb, hdr);
  1825. nlmsg_free(skb);
  1826. return err;
  1827. }
  1828. static int devlink_nl_cmd_dpipe_headers_get(struct sk_buff *skb,
  1829. struct genl_info *info)
  1830. {
  1831. struct devlink *devlink = info->user_ptr[0];
  1832. if (!devlink->dpipe_headers)
  1833. return -EOPNOTSUPP;
  1834. return devlink_dpipe_headers_fill(info, DEVLINK_CMD_DPIPE_HEADERS_GET,
  1835. 0, devlink->dpipe_headers);
  1836. }
  1837. static int devlink_dpipe_table_counters_set(struct devlink *devlink,
  1838. const char *table_name,
  1839. bool enable)
  1840. {
  1841. struct devlink_dpipe_table *table;
  1842. table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
  1843. table_name);
  1844. if (!table)
  1845. return -EINVAL;
  1846. if (table->counter_control_extern)
  1847. return -EOPNOTSUPP;
  1848. if (!(table->counters_enabled ^ enable))
  1849. return 0;
  1850. table->counters_enabled = enable;
  1851. if (table->table_ops->counters_set_update)
  1852. table->table_ops->counters_set_update(table->priv, enable);
  1853. return 0;
  1854. }
  1855. static int devlink_nl_cmd_dpipe_table_counters_set(struct sk_buff *skb,
  1856. struct genl_info *info)
  1857. {
  1858. struct devlink *devlink = info->user_ptr[0];
  1859. const char *table_name;
  1860. bool counters_enable;
  1861. if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME] ||
  1862. !info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED])
  1863. return -EINVAL;
  1864. table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
  1865. counters_enable = !!nla_get_u8(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED]);
  1866. return devlink_dpipe_table_counters_set(devlink, table_name,
  1867. counters_enable);
  1868. }
  1869. static const struct nla_policy devlink_nl_policy[DEVLINK_ATTR_MAX + 1] = {
  1870. [DEVLINK_ATTR_BUS_NAME] = { .type = NLA_NUL_STRING },
  1871. [DEVLINK_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING },
  1872. [DEVLINK_ATTR_PORT_INDEX] = { .type = NLA_U32 },
  1873. [DEVLINK_ATTR_PORT_TYPE] = { .type = NLA_U16 },
  1874. [DEVLINK_ATTR_PORT_SPLIT_COUNT] = { .type = NLA_U32 },
  1875. [DEVLINK_ATTR_SB_INDEX] = { .type = NLA_U32 },
  1876. [DEVLINK_ATTR_SB_POOL_INDEX] = { .type = NLA_U16 },
  1877. [DEVLINK_ATTR_SB_POOL_TYPE] = { .type = NLA_U8 },
  1878. [DEVLINK_ATTR_SB_POOL_SIZE] = { .type = NLA_U32 },
  1879. [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE] = { .type = NLA_U8 },
  1880. [DEVLINK_ATTR_SB_THRESHOLD] = { .type = NLA_U32 },
  1881. [DEVLINK_ATTR_SB_TC_INDEX] = { .type = NLA_U16 },
  1882. [DEVLINK_ATTR_ESWITCH_MODE] = { .type = NLA_U16 },
  1883. [DEVLINK_ATTR_ESWITCH_INLINE_MODE] = { .type = NLA_U8 },
  1884. [DEVLINK_ATTR_ESWITCH_ENCAP_MODE] = { .type = NLA_U8 },
  1885. [DEVLINK_ATTR_DPIPE_TABLE_NAME] = { .type = NLA_NUL_STRING },
  1886. [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED] = { .type = NLA_U8 },
  1887. };
  1888. static const struct genl_ops devlink_nl_ops[] = {
  1889. {
  1890. .cmd = DEVLINK_CMD_GET,
  1891. .doit = devlink_nl_cmd_get_doit,
  1892. .dumpit = devlink_nl_cmd_get_dumpit,
  1893. .policy = devlink_nl_policy,
  1894. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  1895. /* can be retrieved by unprivileged users */
  1896. },
  1897. {
  1898. .cmd = DEVLINK_CMD_PORT_GET,
  1899. .doit = devlink_nl_cmd_port_get_doit,
  1900. .dumpit = devlink_nl_cmd_port_get_dumpit,
  1901. .policy = devlink_nl_policy,
  1902. .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
  1903. /* can be retrieved by unprivileged users */
  1904. },
  1905. {
  1906. .cmd = DEVLINK_CMD_PORT_SET,
  1907. .doit = devlink_nl_cmd_port_set_doit,
  1908. .policy = devlink_nl_policy,
  1909. .flags = GENL_ADMIN_PERM,
  1910. .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
  1911. },
  1912. {
  1913. .cmd = DEVLINK_CMD_PORT_SPLIT,
  1914. .doit = devlink_nl_cmd_port_split_doit,
  1915. .policy = devlink_nl_policy,
  1916. .flags = GENL_ADMIN_PERM,
  1917. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  1918. },
  1919. {
  1920. .cmd = DEVLINK_CMD_PORT_UNSPLIT,
  1921. .doit = devlink_nl_cmd_port_unsplit_doit,
  1922. .policy = devlink_nl_policy,
  1923. .flags = GENL_ADMIN_PERM,
  1924. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  1925. },
  1926. {
  1927. .cmd = DEVLINK_CMD_SB_GET,
  1928. .doit = devlink_nl_cmd_sb_get_doit,
  1929. .dumpit = devlink_nl_cmd_sb_get_dumpit,
  1930. .policy = devlink_nl_policy,
  1931. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  1932. DEVLINK_NL_FLAG_NEED_SB,
  1933. /* can be retrieved by unprivileged users */
  1934. },
  1935. {
  1936. .cmd = DEVLINK_CMD_SB_POOL_GET,
  1937. .doit = devlink_nl_cmd_sb_pool_get_doit,
  1938. .dumpit = devlink_nl_cmd_sb_pool_get_dumpit,
  1939. .policy = devlink_nl_policy,
  1940. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  1941. DEVLINK_NL_FLAG_NEED_SB,
  1942. /* can be retrieved by unprivileged users */
  1943. },
  1944. {
  1945. .cmd = DEVLINK_CMD_SB_POOL_SET,
  1946. .doit = devlink_nl_cmd_sb_pool_set_doit,
  1947. .policy = devlink_nl_policy,
  1948. .flags = GENL_ADMIN_PERM,
  1949. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  1950. DEVLINK_NL_FLAG_NEED_SB,
  1951. },
  1952. {
  1953. .cmd = DEVLINK_CMD_SB_PORT_POOL_GET,
  1954. .doit = devlink_nl_cmd_sb_port_pool_get_doit,
  1955. .dumpit = devlink_nl_cmd_sb_port_pool_get_dumpit,
  1956. .policy = devlink_nl_policy,
  1957. .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
  1958. DEVLINK_NL_FLAG_NEED_SB,
  1959. /* can be retrieved by unprivileged users */
  1960. },
  1961. {
  1962. .cmd = DEVLINK_CMD_SB_PORT_POOL_SET,
  1963. .doit = devlink_nl_cmd_sb_port_pool_set_doit,
  1964. .policy = devlink_nl_policy,
  1965. .flags = GENL_ADMIN_PERM,
  1966. .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
  1967. DEVLINK_NL_FLAG_NEED_SB,
  1968. },
  1969. {
  1970. .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_GET,
  1971. .doit = devlink_nl_cmd_sb_tc_pool_bind_get_doit,
  1972. .dumpit = devlink_nl_cmd_sb_tc_pool_bind_get_dumpit,
  1973. .policy = devlink_nl_policy,
  1974. .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
  1975. DEVLINK_NL_FLAG_NEED_SB,
  1976. /* can be retrieved by unprivileged users */
  1977. },
  1978. {
  1979. .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_SET,
  1980. .doit = devlink_nl_cmd_sb_tc_pool_bind_set_doit,
  1981. .policy = devlink_nl_policy,
  1982. .flags = GENL_ADMIN_PERM,
  1983. .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
  1984. DEVLINK_NL_FLAG_NEED_SB,
  1985. },
  1986. {
  1987. .cmd = DEVLINK_CMD_SB_OCC_SNAPSHOT,
  1988. .doit = devlink_nl_cmd_sb_occ_snapshot_doit,
  1989. .policy = devlink_nl_policy,
  1990. .flags = GENL_ADMIN_PERM,
  1991. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  1992. DEVLINK_NL_FLAG_NEED_SB |
  1993. DEVLINK_NL_FLAG_LOCK_PORTS,
  1994. },
  1995. {
  1996. .cmd = DEVLINK_CMD_SB_OCC_MAX_CLEAR,
  1997. .doit = devlink_nl_cmd_sb_occ_max_clear_doit,
  1998. .policy = devlink_nl_policy,
  1999. .flags = GENL_ADMIN_PERM,
  2000. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  2001. DEVLINK_NL_FLAG_NEED_SB |
  2002. DEVLINK_NL_FLAG_LOCK_PORTS,
  2003. },
  2004. {
  2005. .cmd = DEVLINK_CMD_ESWITCH_GET,
  2006. .doit = devlink_nl_cmd_eswitch_get_doit,
  2007. .policy = devlink_nl_policy,
  2008. .flags = GENL_ADMIN_PERM,
  2009. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  2010. },
  2011. {
  2012. .cmd = DEVLINK_CMD_ESWITCH_SET,
  2013. .doit = devlink_nl_cmd_eswitch_set_doit,
  2014. .policy = devlink_nl_policy,
  2015. .flags = GENL_ADMIN_PERM,
  2016. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  2017. },
  2018. {
  2019. .cmd = DEVLINK_CMD_DPIPE_TABLE_GET,
  2020. .doit = devlink_nl_cmd_dpipe_table_get,
  2021. .policy = devlink_nl_policy,
  2022. .flags = GENL_ADMIN_PERM,
  2023. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  2024. },
  2025. {
  2026. .cmd = DEVLINK_CMD_DPIPE_ENTRIES_GET,
  2027. .doit = devlink_nl_cmd_dpipe_entries_get,
  2028. .policy = devlink_nl_policy,
  2029. .flags = GENL_ADMIN_PERM,
  2030. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  2031. },
  2032. {
  2033. .cmd = DEVLINK_CMD_DPIPE_HEADERS_GET,
  2034. .doit = devlink_nl_cmd_dpipe_headers_get,
  2035. .policy = devlink_nl_policy,
  2036. .flags = GENL_ADMIN_PERM,
  2037. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  2038. },
  2039. {
  2040. .cmd = DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET,
  2041. .doit = devlink_nl_cmd_dpipe_table_counters_set,
  2042. .policy = devlink_nl_policy,
  2043. .flags = GENL_ADMIN_PERM,
  2044. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  2045. },
  2046. };
  2047. static struct genl_family devlink_nl_family __ro_after_init = {
  2048. .name = DEVLINK_GENL_NAME,
  2049. .version = DEVLINK_GENL_VERSION,
  2050. .maxattr = DEVLINK_ATTR_MAX,
  2051. .netnsok = true,
  2052. .pre_doit = devlink_nl_pre_doit,
  2053. .post_doit = devlink_nl_post_doit,
  2054. .module = THIS_MODULE,
  2055. .ops = devlink_nl_ops,
  2056. .n_ops = ARRAY_SIZE(devlink_nl_ops),
  2057. .mcgrps = devlink_nl_mcgrps,
  2058. .n_mcgrps = ARRAY_SIZE(devlink_nl_mcgrps),
  2059. };
  2060. /**
  2061. * devlink_alloc - Allocate new devlink instance resources
  2062. *
  2063. * @ops: ops
  2064. * @priv_size: size of user private data
  2065. *
  2066. * Allocate new devlink instance resources, including devlink index
  2067. * and name.
  2068. */
  2069. struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size)
  2070. {
  2071. struct devlink *devlink;
  2072. devlink = kzalloc(sizeof(*devlink) + priv_size, GFP_KERNEL);
  2073. if (!devlink)
  2074. return NULL;
  2075. devlink->ops = ops;
  2076. devlink_net_set(devlink, &init_net);
  2077. INIT_LIST_HEAD(&devlink->port_list);
  2078. INIT_LIST_HEAD(&devlink->sb_list);
  2079. INIT_LIST_HEAD_RCU(&devlink->dpipe_table_list);
  2080. return devlink;
  2081. }
  2082. EXPORT_SYMBOL_GPL(devlink_alloc);
  2083. /**
  2084. * devlink_register - Register devlink instance
  2085. *
  2086. * @devlink: devlink
  2087. */
  2088. int devlink_register(struct devlink *devlink, struct device *dev)
  2089. {
  2090. mutex_lock(&devlink_mutex);
  2091. devlink->dev = dev;
  2092. list_add_tail(&devlink->list, &devlink_list);
  2093. devlink_notify(devlink, DEVLINK_CMD_NEW);
  2094. mutex_unlock(&devlink_mutex);
  2095. return 0;
  2096. }
  2097. EXPORT_SYMBOL_GPL(devlink_register);
  2098. /**
  2099. * devlink_unregister - Unregister devlink instance
  2100. *
  2101. * @devlink: devlink
  2102. */
  2103. void devlink_unregister(struct devlink *devlink)
  2104. {
  2105. mutex_lock(&devlink_mutex);
  2106. devlink_notify(devlink, DEVLINK_CMD_DEL);
  2107. list_del(&devlink->list);
  2108. mutex_unlock(&devlink_mutex);
  2109. }
  2110. EXPORT_SYMBOL_GPL(devlink_unregister);
  2111. /**
  2112. * devlink_free - Free devlink instance resources
  2113. *
  2114. * @devlink: devlink
  2115. */
  2116. void devlink_free(struct devlink *devlink)
  2117. {
  2118. kfree(devlink);
  2119. }
  2120. EXPORT_SYMBOL_GPL(devlink_free);
  2121. /**
  2122. * devlink_port_register - Register devlink port
  2123. *
  2124. * @devlink: devlink
  2125. * @devlink_port: devlink port
  2126. * @port_index
  2127. *
  2128. * Register devlink port with provided port index. User can use
  2129. * any indexing, even hw-related one. devlink_port structure
  2130. * is convenient to be embedded inside user driver private structure.
  2131. * Note that the caller should take care of zeroing the devlink_port
  2132. * structure.
  2133. */
  2134. int devlink_port_register(struct devlink *devlink,
  2135. struct devlink_port *devlink_port,
  2136. unsigned int port_index)
  2137. {
  2138. mutex_lock(&devlink_port_mutex);
  2139. if (devlink_port_index_exists(devlink, port_index)) {
  2140. mutex_unlock(&devlink_port_mutex);
  2141. return -EEXIST;
  2142. }
  2143. devlink_port->devlink = devlink;
  2144. devlink_port->index = port_index;
  2145. devlink_port->registered = true;
  2146. list_add_tail(&devlink_port->list, &devlink->port_list);
  2147. mutex_unlock(&devlink_port_mutex);
  2148. devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
  2149. return 0;
  2150. }
  2151. EXPORT_SYMBOL_GPL(devlink_port_register);
  2152. /**
  2153. * devlink_port_unregister - Unregister devlink port
  2154. *
  2155. * @devlink_port: devlink port
  2156. */
  2157. void devlink_port_unregister(struct devlink_port *devlink_port)
  2158. {
  2159. devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL);
  2160. mutex_lock(&devlink_port_mutex);
  2161. list_del(&devlink_port->list);
  2162. mutex_unlock(&devlink_port_mutex);
  2163. }
  2164. EXPORT_SYMBOL_GPL(devlink_port_unregister);
  2165. static void __devlink_port_type_set(struct devlink_port *devlink_port,
  2166. enum devlink_port_type type,
  2167. void *type_dev)
  2168. {
  2169. devlink_port->type = type;
  2170. devlink_port->type_dev = type_dev;
  2171. devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
  2172. }
  2173. /**
  2174. * devlink_port_type_eth_set - Set port type to Ethernet
  2175. *
  2176. * @devlink_port: devlink port
  2177. * @netdev: related netdevice
  2178. */
  2179. void devlink_port_type_eth_set(struct devlink_port *devlink_port,
  2180. struct net_device *netdev)
  2181. {
  2182. return __devlink_port_type_set(devlink_port,
  2183. DEVLINK_PORT_TYPE_ETH, netdev);
  2184. }
  2185. EXPORT_SYMBOL_GPL(devlink_port_type_eth_set);
  2186. /**
  2187. * devlink_port_type_ib_set - Set port type to InfiniBand
  2188. *
  2189. * @devlink_port: devlink port
  2190. * @ibdev: related IB device
  2191. */
  2192. void devlink_port_type_ib_set(struct devlink_port *devlink_port,
  2193. struct ib_device *ibdev)
  2194. {
  2195. return __devlink_port_type_set(devlink_port,
  2196. DEVLINK_PORT_TYPE_IB, ibdev);
  2197. }
  2198. EXPORT_SYMBOL_GPL(devlink_port_type_ib_set);
  2199. /**
  2200. * devlink_port_type_clear - Clear port type
  2201. *
  2202. * @devlink_port: devlink port
  2203. */
  2204. void devlink_port_type_clear(struct devlink_port *devlink_port)
  2205. {
  2206. return __devlink_port_type_set(devlink_port,
  2207. DEVLINK_PORT_TYPE_NOTSET, NULL);
  2208. }
  2209. EXPORT_SYMBOL_GPL(devlink_port_type_clear);
  2210. /**
  2211. * devlink_port_split_set - Set port is split
  2212. *
  2213. * @devlink_port: devlink port
  2214. * @split_group: split group - identifies group split port is part of
  2215. */
  2216. void devlink_port_split_set(struct devlink_port *devlink_port,
  2217. u32 split_group)
  2218. {
  2219. devlink_port->split = true;
  2220. devlink_port->split_group = split_group;
  2221. devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
  2222. }
  2223. EXPORT_SYMBOL_GPL(devlink_port_split_set);
  2224. int devlink_sb_register(struct devlink *devlink, unsigned int sb_index,
  2225. u32 size, u16 ingress_pools_count,
  2226. u16 egress_pools_count, u16 ingress_tc_count,
  2227. u16 egress_tc_count)
  2228. {
  2229. struct devlink_sb *devlink_sb;
  2230. int err = 0;
  2231. mutex_lock(&devlink_mutex);
  2232. if (devlink_sb_index_exists(devlink, sb_index)) {
  2233. err = -EEXIST;
  2234. goto unlock;
  2235. }
  2236. devlink_sb = kzalloc(sizeof(*devlink_sb), GFP_KERNEL);
  2237. if (!devlink_sb) {
  2238. err = -ENOMEM;
  2239. goto unlock;
  2240. }
  2241. devlink_sb->index = sb_index;
  2242. devlink_sb->size = size;
  2243. devlink_sb->ingress_pools_count = ingress_pools_count;
  2244. devlink_sb->egress_pools_count = egress_pools_count;
  2245. devlink_sb->ingress_tc_count = ingress_tc_count;
  2246. devlink_sb->egress_tc_count = egress_tc_count;
  2247. list_add_tail(&devlink_sb->list, &devlink->sb_list);
  2248. unlock:
  2249. mutex_unlock(&devlink_mutex);
  2250. return err;
  2251. }
  2252. EXPORT_SYMBOL_GPL(devlink_sb_register);
  2253. void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index)
  2254. {
  2255. struct devlink_sb *devlink_sb;
  2256. mutex_lock(&devlink_mutex);
  2257. devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
  2258. WARN_ON(!devlink_sb);
  2259. list_del(&devlink_sb->list);
  2260. mutex_unlock(&devlink_mutex);
  2261. kfree(devlink_sb);
  2262. }
  2263. EXPORT_SYMBOL_GPL(devlink_sb_unregister);
  2264. /**
  2265. * devlink_dpipe_headers_register - register dpipe headers
  2266. *
  2267. * @devlink: devlink
  2268. * @dpipe_headers: dpipe header array
  2269. *
  2270. * Register the headers supported by hardware.
  2271. */
  2272. int devlink_dpipe_headers_register(struct devlink *devlink,
  2273. struct devlink_dpipe_headers *dpipe_headers)
  2274. {
  2275. mutex_lock(&devlink_mutex);
  2276. devlink->dpipe_headers = dpipe_headers;
  2277. mutex_unlock(&devlink_mutex);
  2278. return 0;
  2279. }
  2280. EXPORT_SYMBOL_GPL(devlink_dpipe_headers_register);
  2281. /**
  2282. * devlink_dpipe_headers_unregister - unregister dpipe headers
  2283. *
  2284. * @devlink: devlink
  2285. *
  2286. * Unregister the headers supported by hardware.
  2287. */
  2288. void devlink_dpipe_headers_unregister(struct devlink *devlink)
  2289. {
  2290. mutex_lock(&devlink_mutex);
  2291. devlink->dpipe_headers = NULL;
  2292. mutex_unlock(&devlink_mutex);
  2293. }
  2294. EXPORT_SYMBOL_GPL(devlink_dpipe_headers_unregister);
  2295. /**
  2296. * devlink_dpipe_table_counter_enabled - check if counter allocation
  2297. * required
  2298. * @devlink: devlink
  2299. * @table_name: tables name
  2300. *
  2301. * Used by driver to check if counter allocation is required.
  2302. * After counter allocation is turned on the table entries
  2303. * are updated to include counter statistics.
  2304. *
  2305. * After that point on the driver must respect the counter
  2306. * state so that each entry added to the table is added
  2307. * with a counter.
  2308. */
  2309. bool devlink_dpipe_table_counter_enabled(struct devlink *devlink,
  2310. const char *table_name)
  2311. {
  2312. struct devlink_dpipe_table *table;
  2313. bool enabled;
  2314. rcu_read_lock();
  2315. table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
  2316. table_name);
  2317. enabled = false;
  2318. if (table)
  2319. enabled = table->counters_enabled;
  2320. rcu_read_unlock();
  2321. return enabled;
  2322. }
  2323. EXPORT_SYMBOL_GPL(devlink_dpipe_table_counter_enabled);
  2324. /**
  2325. * devlink_dpipe_table_register - register dpipe table
  2326. *
  2327. * @devlink: devlink
  2328. * @table_name: table name
  2329. * @table_ops: table ops
  2330. * @priv: priv
  2331. * @size: size
  2332. * @counter_control_extern: external control for counters
  2333. */
  2334. int devlink_dpipe_table_register(struct devlink *devlink,
  2335. const char *table_name,
  2336. struct devlink_dpipe_table_ops *table_ops,
  2337. void *priv, u64 size,
  2338. bool counter_control_extern)
  2339. {
  2340. struct devlink_dpipe_table *table;
  2341. if (devlink_dpipe_table_find(&devlink->dpipe_table_list, table_name))
  2342. return -EEXIST;
  2343. table = kzalloc(sizeof(*table), GFP_KERNEL);
  2344. if (!table)
  2345. return -ENOMEM;
  2346. table->name = table_name;
  2347. table->table_ops = table_ops;
  2348. table->priv = priv;
  2349. table->size = size;
  2350. table->counter_control_extern = counter_control_extern;
  2351. mutex_lock(&devlink_mutex);
  2352. list_add_tail_rcu(&table->list, &devlink->dpipe_table_list);
  2353. mutex_unlock(&devlink_mutex);
  2354. return 0;
  2355. }
  2356. EXPORT_SYMBOL_GPL(devlink_dpipe_table_register);
  2357. /**
  2358. * devlink_dpipe_table_unregister - unregister dpipe table
  2359. *
  2360. * @devlink: devlink
  2361. * @table_name: table name
  2362. */
  2363. void devlink_dpipe_table_unregister(struct devlink *devlink,
  2364. const char *table_name)
  2365. {
  2366. struct devlink_dpipe_table *table;
  2367. mutex_lock(&devlink_mutex);
  2368. table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
  2369. table_name);
  2370. if (!table)
  2371. goto unlock;
  2372. list_del_rcu(&table->list);
  2373. mutex_unlock(&devlink_mutex);
  2374. kfree_rcu(table, rcu);
  2375. return;
  2376. unlock:
  2377. mutex_unlock(&devlink_mutex);
  2378. }
  2379. EXPORT_SYMBOL_GPL(devlink_dpipe_table_unregister);
  2380. static int __init devlink_module_init(void)
  2381. {
  2382. return genl_register_family(&devlink_nl_family);
  2383. }
  2384. static void __exit devlink_module_exit(void)
  2385. {
  2386. genl_unregister_family(&devlink_nl_family);
  2387. }
  2388. module_init(devlink_module_init);
  2389. module_exit(devlink_module_exit);
  2390. MODULE_LICENSE("GPL v2");
  2391. MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
  2392. MODULE_DESCRIPTION("Network physical device Netlink interface");
  2393. MODULE_ALIAS_GENL_FAMILY(DEVLINK_GENL_NAME);