devlink.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768
  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. static LIST_HEAD(devlink_list);
  29. /* devlink_mutex
  30. *
  31. * An overall lock guarding every operation coming from userspace.
  32. * It also guards devlink devices list and it is taken when
  33. * driver registers/unregisters it.
  34. */
  35. static DEFINE_MUTEX(devlink_mutex);
  36. /* devlink_port_mutex
  37. *
  38. * Shared lock to guard lists of ports in all devlink devices.
  39. */
  40. static DEFINE_MUTEX(devlink_port_mutex);
  41. static struct net *devlink_net(const struct devlink *devlink)
  42. {
  43. return read_pnet(&devlink->_net);
  44. }
  45. static void devlink_net_set(struct devlink *devlink, struct net *net)
  46. {
  47. write_pnet(&devlink->_net, net);
  48. }
  49. static struct devlink *devlink_get_from_attrs(struct net *net,
  50. struct nlattr **attrs)
  51. {
  52. struct devlink *devlink;
  53. char *busname;
  54. char *devname;
  55. if (!attrs[DEVLINK_ATTR_BUS_NAME] || !attrs[DEVLINK_ATTR_DEV_NAME])
  56. return ERR_PTR(-EINVAL);
  57. busname = nla_data(attrs[DEVLINK_ATTR_BUS_NAME]);
  58. devname = nla_data(attrs[DEVLINK_ATTR_DEV_NAME]);
  59. list_for_each_entry(devlink, &devlink_list, list) {
  60. if (strcmp(devlink->dev->bus->name, busname) == 0 &&
  61. strcmp(dev_name(devlink->dev), devname) == 0 &&
  62. net_eq(devlink_net(devlink), net))
  63. return devlink;
  64. }
  65. return ERR_PTR(-ENODEV);
  66. }
  67. static struct devlink *devlink_get_from_info(struct genl_info *info)
  68. {
  69. return devlink_get_from_attrs(genl_info_net(info), info->attrs);
  70. }
  71. static struct devlink_port *devlink_port_get_by_index(struct devlink *devlink,
  72. int port_index)
  73. {
  74. struct devlink_port *devlink_port;
  75. list_for_each_entry(devlink_port, &devlink->port_list, list) {
  76. if (devlink_port->index == port_index)
  77. return devlink_port;
  78. }
  79. return NULL;
  80. }
  81. static bool devlink_port_index_exists(struct devlink *devlink, int port_index)
  82. {
  83. return devlink_port_get_by_index(devlink, port_index);
  84. }
  85. static struct devlink_port *devlink_port_get_from_attrs(struct devlink *devlink,
  86. struct nlattr **attrs)
  87. {
  88. if (attrs[DEVLINK_ATTR_PORT_INDEX]) {
  89. u32 port_index = nla_get_u32(attrs[DEVLINK_ATTR_PORT_INDEX]);
  90. struct devlink_port *devlink_port;
  91. devlink_port = devlink_port_get_by_index(devlink, port_index);
  92. if (!devlink_port)
  93. return ERR_PTR(-ENODEV);
  94. return devlink_port;
  95. }
  96. return ERR_PTR(-EINVAL);
  97. }
  98. static struct devlink_port *devlink_port_get_from_info(struct devlink *devlink,
  99. struct genl_info *info)
  100. {
  101. return devlink_port_get_from_attrs(devlink, info->attrs);
  102. }
  103. struct devlink_sb {
  104. struct list_head list;
  105. unsigned int index;
  106. u32 size;
  107. u16 ingress_pools_count;
  108. u16 egress_pools_count;
  109. u16 ingress_tc_count;
  110. u16 egress_tc_count;
  111. };
  112. static u16 devlink_sb_pool_count(struct devlink_sb *devlink_sb)
  113. {
  114. return devlink_sb->ingress_pools_count + devlink_sb->egress_pools_count;
  115. }
  116. static struct devlink_sb *devlink_sb_get_by_index(struct devlink *devlink,
  117. unsigned int sb_index)
  118. {
  119. struct devlink_sb *devlink_sb;
  120. list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
  121. if (devlink_sb->index == sb_index)
  122. return devlink_sb;
  123. }
  124. return NULL;
  125. }
  126. static bool devlink_sb_index_exists(struct devlink *devlink,
  127. unsigned int sb_index)
  128. {
  129. return devlink_sb_get_by_index(devlink, sb_index);
  130. }
  131. static struct devlink_sb *devlink_sb_get_from_attrs(struct devlink *devlink,
  132. struct nlattr **attrs)
  133. {
  134. if (attrs[DEVLINK_ATTR_SB_INDEX]) {
  135. u32 sb_index = nla_get_u32(attrs[DEVLINK_ATTR_SB_INDEX]);
  136. struct devlink_sb *devlink_sb;
  137. devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
  138. if (!devlink_sb)
  139. return ERR_PTR(-ENODEV);
  140. return devlink_sb;
  141. }
  142. return ERR_PTR(-EINVAL);
  143. }
  144. static struct devlink_sb *devlink_sb_get_from_info(struct devlink *devlink,
  145. struct genl_info *info)
  146. {
  147. return devlink_sb_get_from_attrs(devlink, info->attrs);
  148. }
  149. static int devlink_sb_pool_index_get_from_attrs(struct devlink_sb *devlink_sb,
  150. struct nlattr **attrs,
  151. u16 *p_pool_index)
  152. {
  153. u16 val;
  154. if (!attrs[DEVLINK_ATTR_SB_POOL_INDEX])
  155. return -EINVAL;
  156. val = nla_get_u16(attrs[DEVLINK_ATTR_SB_POOL_INDEX]);
  157. if (val >= devlink_sb_pool_count(devlink_sb))
  158. return -EINVAL;
  159. *p_pool_index = val;
  160. return 0;
  161. }
  162. static int devlink_sb_pool_index_get_from_info(struct devlink_sb *devlink_sb,
  163. struct genl_info *info,
  164. u16 *p_pool_index)
  165. {
  166. return devlink_sb_pool_index_get_from_attrs(devlink_sb, info->attrs,
  167. p_pool_index);
  168. }
  169. static int
  170. devlink_sb_pool_type_get_from_attrs(struct nlattr **attrs,
  171. enum devlink_sb_pool_type *p_pool_type)
  172. {
  173. u8 val;
  174. if (!attrs[DEVLINK_ATTR_SB_POOL_TYPE])
  175. return -EINVAL;
  176. val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_TYPE]);
  177. if (val != DEVLINK_SB_POOL_TYPE_INGRESS &&
  178. val != DEVLINK_SB_POOL_TYPE_EGRESS)
  179. return -EINVAL;
  180. *p_pool_type = val;
  181. return 0;
  182. }
  183. static int
  184. devlink_sb_pool_type_get_from_info(struct genl_info *info,
  185. enum devlink_sb_pool_type *p_pool_type)
  186. {
  187. return devlink_sb_pool_type_get_from_attrs(info->attrs, p_pool_type);
  188. }
  189. static int
  190. devlink_sb_th_type_get_from_attrs(struct nlattr **attrs,
  191. enum devlink_sb_threshold_type *p_th_type)
  192. {
  193. u8 val;
  194. if (!attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE])
  195. return -EINVAL;
  196. val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE]);
  197. if (val != DEVLINK_SB_THRESHOLD_TYPE_STATIC &&
  198. val != DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC)
  199. return -EINVAL;
  200. *p_th_type = val;
  201. return 0;
  202. }
  203. static int
  204. devlink_sb_th_type_get_from_info(struct genl_info *info,
  205. enum devlink_sb_threshold_type *p_th_type)
  206. {
  207. return devlink_sb_th_type_get_from_attrs(info->attrs, p_th_type);
  208. }
  209. static int
  210. devlink_sb_tc_index_get_from_attrs(struct devlink_sb *devlink_sb,
  211. struct nlattr **attrs,
  212. enum devlink_sb_pool_type pool_type,
  213. u16 *p_tc_index)
  214. {
  215. u16 val;
  216. if (!attrs[DEVLINK_ATTR_SB_TC_INDEX])
  217. return -EINVAL;
  218. val = nla_get_u16(attrs[DEVLINK_ATTR_SB_TC_INDEX]);
  219. if (pool_type == DEVLINK_SB_POOL_TYPE_INGRESS &&
  220. val >= devlink_sb->ingress_tc_count)
  221. return -EINVAL;
  222. if (pool_type == DEVLINK_SB_POOL_TYPE_EGRESS &&
  223. val >= devlink_sb->egress_tc_count)
  224. return -EINVAL;
  225. *p_tc_index = val;
  226. return 0;
  227. }
  228. static int
  229. devlink_sb_tc_index_get_from_info(struct devlink_sb *devlink_sb,
  230. struct genl_info *info,
  231. enum devlink_sb_pool_type pool_type,
  232. u16 *p_tc_index)
  233. {
  234. return devlink_sb_tc_index_get_from_attrs(devlink_sb, info->attrs,
  235. pool_type, p_tc_index);
  236. }
  237. #define DEVLINK_NL_FLAG_NEED_DEVLINK BIT(0)
  238. #define DEVLINK_NL_FLAG_NEED_PORT BIT(1)
  239. #define DEVLINK_NL_FLAG_NEED_SB BIT(2)
  240. #define DEVLINK_NL_FLAG_LOCK_PORTS BIT(3)
  241. /* port is not needed but we need to ensure they don't
  242. * change in the middle of command
  243. */
  244. static int devlink_nl_pre_doit(const struct genl_ops *ops,
  245. struct sk_buff *skb, struct genl_info *info)
  246. {
  247. struct devlink *devlink;
  248. mutex_lock(&devlink_mutex);
  249. devlink = devlink_get_from_info(info);
  250. if (IS_ERR(devlink)) {
  251. mutex_unlock(&devlink_mutex);
  252. return PTR_ERR(devlink);
  253. }
  254. if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_DEVLINK) {
  255. info->user_ptr[0] = devlink;
  256. } else if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_PORT) {
  257. struct devlink_port *devlink_port;
  258. mutex_lock(&devlink_port_mutex);
  259. devlink_port = devlink_port_get_from_info(devlink, info);
  260. if (IS_ERR(devlink_port)) {
  261. mutex_unlock(&devlink_port_mutex);
  262. mutex_unlock(&devlink_mutex);
  263. return PTR_ERR(devlink_port);
  264. }
  265. info->user_ptr[0] = devlink_port;
  266. }
  267. if (ops->internal_flags & DEVLINK_NL_FLAG_LOCK_PORTS) {
  268. mutex_lock(&devlink_port_mutex);
  269. }
  270. if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_SB) {
  271. struct devlink_sb *devlink_sb;
  272. devlink_sb = devlink_sb_get_from_info(devlink, info);
  273. if (IS_ERR(devlink_sb)) {
  274. if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_PORT)
  275. mutex_unlock(&devlink_port_mutex);
  276. mutex_unlock(&devlink_mutex);
  277. return PTR_ERR(devlink_sb);
  278. }
  279. info->user_ptr[1] = devlink_sb;
  280. }
  281. return 0;
  282. }
  283. static void devlink_nl_post_doit(const struct genl_ops *ops,
  284. struct sk_buff *skb, struct genl_info *info)
  285. {
  286. if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_PORT ||
  287. ops->internal_flags & DEVLINK_NL_FLAG_LOCK_PORTS)
  288. mutex_unlock(&devlink_port_mutex);
  289. mutex_unlock(&devlink_mutex);
  290. }
  291. static struct genl_family devlink_nl_family = {
  292. .id = GENL_ID_GENERATE,
  293. .name = DEVLINK_GENL_NAME,
  294. .version = DEVLINK_GENL_VERSION,
  295. .maxattr = DEVLINK_ATTR_MAX,
  296. .netnsok = true,
  297. .pre_doit = devlink_nl_pre_doit,
  298. .post_doit = devlink_nl_post_doit,
  299. };
  300. enum devlink_multicast_groups {
  301. DEVLINK_MCGRP_CONFIG,
  302. };
  303. static const struct genl_multicast_group devlink_nl_mcgrps[] = {
  304. [DEVLINK_MCGRP_CONFIG] = { .name = DEVLINK_GENL_MCGRP_CONFIG_NAME },
  305. };
  306. static int devlink_nl_put_handle(struct sk_buff *msg, struct devlink *devlink)
  307. {
  308. if (nla_put_string(msg, DEVLINK_ATTR_BUS_NAME, devlink->dev->bus->name))
  309. return -EMSGSIZE;
  310. if (nla_put_string(msg, DEVLINK_ATTR_DEV_NAME, dev_name(devlink->dev)))
  311. return -EMSGSIZE;
  312. return 0;
  313. }
  314. static int devlink_nl_fill(struct sk_buff *msg, struct devlink *devlink,
  315. enum devlink_command cmd, u32 portid,
  316. u32 seq, int flags)
  317. {
  318. void *hdr;
  319. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  320. if (!hdr)
  321. return -EMSGSIZE;
  322. if (devlink_nl_put_handle(msg, devlink))
  323. goto nla_put_failure;
  324. genlmsg_end(msg, hdr);
  325. return 0;
  326. nla_put_failure:
  327. genlmsg_cancel(msg, hdr);
  328. return -EMSGSIZE;
  329. }
  330. static void devlink_notify(struct devlink *devlink, enum devlink_command cmd)
  331. {
  332. struct sk_buff *msg;
  333. int err;
  334. WARN_ON(cmd != DEVLINK_CMD_NEW && cmd != DEVLINK_CMD_DEL);
  335. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  336. if (!msg)
  337. return;
  338. err = devlink_nl_fill(msg, devlink, cmd, 0, 0, 0);
  339. if (err) {
  340. nlmsg_free(msg);
  341. return;
  342. }
  343. genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
  344. msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
  345. }
  346. static int devlink_nl_port_fill(struct sk_buff *msg, struct devlink *devlink,
  347. struct devlink_port *devlink_port,
  348. enum devlink_command cmd, u32 portid,
  349. u32 seq, int flags)
  350. {
  351. void *hdr;
  352. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  353. if (!hdr)
  354. return -EMSGSIZE;
  355. if (devlink_nl_put_handle(msg, devlink))
  356. goto nla_put_failure;
  357. if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
  358. goto nla_put_failure;
  359. if (nla_put_u16(msg, DEVLINK_ATTR_PORT_TYPE, devlink_port->type))
  360. goto nla_put_failure;
  361. if (devlink_port->desired_type != DEVLINK_PORT_TYPE_NOTSET &&
  362. nla_put_u16(msg, DEVLINK_ATTR_PORT_DESIRED_TYPE,
  363. devlink_port->desired_type))
  364. goto nla_put_failure;
  365. if (devlink_port->type == DEVLINK_PORT_TYPE_ETH) {
  366. struct net_device *netdev = devlink_port->type_dev;
  367. if (netdev &&
  368. (nla_put_u32(msg, DEVLINK_ATTR_PORT_NETDEV_IFINDEX,
  369. netdev->ifindex) ||
  370. nla_put_string(msg, DEVLINK_ATTR_PORT_NETDEV_NAME,
  371. netdev->name)))
  372. goto nla_put_failure;
  373. }
  374. if (devlink_port->type == DEVLINK_PORT_TYPE_IB) {
  375. struct ib_device *ibdev = devlink_port->type_dev;
  376. if (ibdev &&
  377. nla_put_string(msg, DEVLINK_ATTR_PORT_IBDEV_NAME,
  378. ibdev->name))
  379. goto nla_put_failure;
  380. }
  381. if (devlink_port->split &&
  382. nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_GROUP,
  383. devlink_port->split_group))
  384. goto nla_put_failure;
  385. genlmsg_end(msg, hdr);
  386. return 0;
  387. nla_put_failure:
  388. genlmsg_cancel(msg, hdr);
  389. return -EMSGSIZE;
  390. }
  391. static void devlink_port_notify(struct devlink_port *devlink_port,
  392. enum devlink_command cmd)
  393. {
  394. struct devlink *devlink = devlink_port->devlink;
  395. struct sk_buff *msg;
  396. int err;
  397. if (!devlink_port->registered)
  398. return;
  399. WARN_ON(cmd != DEVLINK_CMD_PORT_NEW && cmd != DEVLINK_CMD_PORT_DEL);
  400. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  401. if (!msg)
  402. return;
  403. err = devlink_nl_port_fill(msg, devlink, devlink_port, cmd, 0, 0, 0);
  404. if (err) {
  405. nlmsg_free(msg);
  406. return;
  407. }
  408. genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
  409. msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
  410. }
  411. static int devlink_nl_cmd_get_doit(struct sk_buff *skb, struct genl_info *info)
  412. {
  413. struct devlink *devlink = info->user_ptr[0];
  414. struct sk_buff *msg;
  415. int err;
  416. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  417. if (!msg)
  418. return -ENOMEM;
  419. err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
  420. info->snd_portid, info->snd_seq, 0);
  421. if (err) {
  422. nlmsg_free(msg);
  423. return err;
  424. }
  425. return genlmsg_reply(msg, info);
  426. }
  427. static int devlink_nl_cmd_get_dumpit(struct sk_buff *msg,
  428. struct netlink_callback *cb)
  429. {
  430. struct devlink *devlink;
  431. int start = cb->args[0];
  432. int idx = 0;
  433. int err;
  434. mutex_lock(&devlink_mutex);
  435. list_for_each_entry(devlink, &devlink_list, list) {
  436. if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
  437. continue;
  438. if (idx < start) {
  439. idx++;
  440. continue;
  441. }
  442. err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
  443. NETLINK_CB(cb->skb).portid,
  444. cb->nlh->nlmsg_seq, NLM_F_MULTI);
  445. if (err)
  446. goto out;
  447. idx++;
  448. }
  449. out:
  450. mutex_unlock(&devlink_mutex);
  451. cb->args[0] = idx;
  452. return msg->len;
  453. }
  454. static int devlink_nl_cmd_port_get_doit(struct sk_buff *skb,
  455. struct genl_info *info)
  456. {
  457. struct devlink_port *devlink_port = info->user_ptr[0];
  458. struct devlink *devlink = devlink_port->devlink;
  459. struct sk_buff *msg;
  460. int err;
  461. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  462. if (!msg)
  463. return -ENOMEM;
  464. err = devlink_nl_port_fill(msg, devlink, devlink_port,
  465. DEVLINK_CMD_PORT_NEW,
  466. info->snd_portid, info->snd_seq, 0);
  467. if (err) {
  468. nlmsg_free(msg);
  469. return err;
  470. }
  471. return genlmsg_reply(msg, info);
  472. }
  473. static int devlink_nl_cmd_port_get_dumpit(struct sk_buff *msg,
  474. struct netlink_callback *cb)
  475. {
  476. struct devlink *devlink;
  477. struct devlink_port *devlink_port;
  478. int start = cb->args[0];
  479. int idx = 0;
  480. int err;
  481. mutex_lock(&devlink_mutex);
  482. mutex_lock(&devlink_port_mutex);
  483. list_for_each_entry(devlink, &devlink_list, list) {
  484. if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
  485. continue;
  486. list_for_each_entry(devlink_port, &devlink->port_list, list) {
  487. if (idx < start) {
  488. idx++;
  489. continue;
  490. }
  491. err = devlink_nl_port_fill(msg, devlink, devlink_port,
  492. DEVLINK_CMD_NEW,
  493. NETLINK_CB(cb->skb).portid,
  494. cb->nlh->nlmsg_seq,
  495. NLM_F_MULTI);
  496. if (err)
  497. goto out;
  498. idx++;
  499. }
  500. }
  501. out:
  502. mutex_unlock(&devlink_port_mutex);
  503. mutex_unlock(&devlink_mutex);
  504. cb->args[0] = idx;
  505. return msg->len;
  506. }
  507. static int devlink_port_type_set(struct devlink *devlink,
  508. struct devlink_port *devlink_port,
  509. enum devlink_port_type port_type)
  510. {
  511. int err;
  512. if (devlink->ops && devlink->ops->port_type_set) {
  513. if (port_type == DEVLINK_PORT_TYPE_NOTSET)
  514. return -EINVAL;
  515. err = devlink->ops->port_type_set(devlink_port, port_type);
  516. if (err)
  517. return err;
  518. devlink_port->desired_type = port_type;
  519. devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
  520. return 0;
  521. }
  522. return -EOPNOTSUPP;
  523. }
  524. static int devlink_nl_cmd_port_set_doit(struct sk_buff *skb,
  525. struct genl_info *info)
  526. {
  527. struct devlink_port *devlink_port = info->user_ptr[0];
  528. struct devlink *devlink = devlink_port->devlink;
  529. int err;
  530. if (info->attrs[DEVLINK_ATTR_PORT_TYPE]) {
  531. enum devlink_port_type port_type;
  532. port_type = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_TYPE]);
  533. err = devlink_port_type_set(devlink, devlink_port, port_type);
  534. if (err)
  535. return err;
  536. }
  537. return 0;
  538. }
  539. static int devlink_port_split(struct devlink *devlink,
  540. u32 port_index, u32 count)
  541. {
  542. if (devlink->ops && devlink->ops->port_split)
  543. return devlink->ops->port_split(devlink, port_index, count);
  544. return -EOPNOTSUPP;
  545. }
  546. static int devlink_nl_cmd_port_split_doit(struct sk_buff *skb,
  547. struct genl_info *info)
  548. {
  549. struct devlink *devlink = info->user_ptr[0];
  550. u32 port_index;
  551. u32 count;
  552. if (!info->attrs[DEVLINK_ATTR_PORT_INDEX] ||
  553. !info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT])
  554. return -EINVAL;
  555. port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
  556. count = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT]);
  557. return devlink_port_split(devlink, port_index, count);
  558. }
  559. static int devlink_port_unsplit(struct devlink *devlink, u32 port_index)
  560. {
  561. if (devlink->ops && devlink->ops->port_unsplit)
  562. return devlink->ops->port_unsplit(devlink, port_index);
  563. return -EOPNOTSUPP;
  564. }
  565. static int devlink_nl_cmd_port_unsplit_doit(struct sk_buff *skb,
  566. struct genl_info *info)
  567. {
  568. struct devlink *devlink = info->user_ptr[0];
  569. u32 port_index;
  570. if (!info->attrs[DEVLINK_ATTR_PORT_INDEX])
  571. return -EINVAL;
  572. port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
  573. return devlink_port_unsplit(devlink, port_index);
  574. }
  575. static int devlink_nl_sb_fill(struct sk_buff *msg, struct devlink *devlink,
  576. struct devlink_sb *devlink_sb,
  577. enum devlink_command cmd, u32 portid,
  578. u32 seq, int flags)
  579. {
  580. void *hdr;
  581. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  582. if (!hdr)
  583. return -EMSGSIZE;
  584. if (devlink_nl_put_handle(msg, devlink))
  585. goto nla_put_failure;
  586. if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
  587. goto nla_put_failure;
  588. if (nla_put_u32(msg, DEVLINK_ATTR_SB_SIZE, devlink_sb->size))
  589. goto nla_put_failure;
  590. if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_POOL_COUNT,
  591. devlink_sb->ingress_pools_count))
  592. goto nla_put_failure;
  593. if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_POOL_COUNT,
  594. devlink_sb->egress_pools_count))
  595. goto nla_put_failure;
  596. if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_TC_COUNT,
  597. devlink_sb->ingress_tc_count))
  598. goto nla_put_failure;
  599. if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_TC_COUNT,
  600. devlink_sb->egress_tc_count))
  601. goto nla_put_failure;
  602. genlmsg_end(msg, hdr);
  603. return 0;
  604. nla_put_failure:
  605. genlmsg_cancel(msg, hdr);
  606. return -EMSGSIZE;
  607. }
  608. static int devlink_nl_cmd_sb_get_doit(struct sk_buff *skb,
  609. struct genl_info *info)
  610. {
  611. struct devlink *devlink = info->user_ptr[0];
  612. struct devlink_sb *devlink_sb = info->user_ptr[1];
  613. struct sk_buff *msg;
  614. int err;
  615. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  616. if (!msg)
  617. return -ENOMEM;
  618. err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
  619. DEVLINK_CMD_SB_NEW,
  620. info->snd_portid, info->snd_seq, 0);
  621. if (err) {
  622. nlmsg_free(msg);
  623. return err;
  624. }
  625. return genlmsg_reply(msg, info);
  626. }
  627. static int devlink_nl_cmd_sb_get_dumpit(struct sk_buff *msg,
  628. struct netlink_callback *cb)
  629. {
  630. struct devlink *devlink;
  631. struct devlink_sb *devlink_sb;
  632. int start = cb->args[0];
  633. int idx = 0;
  634. int err;
  635. mutex_lock(&devlink_mutex);
  636. list_for_each_entry(devlink, &devlink_list, list) {
  637. if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
  638. continue;
  639. list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
  640. if (idx < start) {
  641. idx++;
  642. continue;
  643. }
  644. err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
  645. DEVLINK_CMD_SB_NEW,
  646. NETLINK_CB(cb->skb).portid,
  647. cb->nlh->nlmsg_seq,
  648. NLM_F_MULTI);
  649. if (err)
  650. goto out;
  651. idx++;
  652. }
  653. }
  654. out:
  655. mutex_unlock(&devlink_mutex);
  656. cb->args[0] = idx;
  657. return msg->len;
  658. }
  659. static int devlink_nl_sb_pool_fill(struct sk_buff *msg, struct devlink *devlink,
  660. struct devlink_sb *devlink_sb,
  661. u16 pool_index, enum devlink_command cmd,
  662. u32 portid, u32 seq, int flags)
  663. {
  664. struct devlink_sb_pool_info pool_info;
  665. void *hdr;
  666. int err;
  667. err = devlink->ops->sb_pool_get(devlink, devlink_sb->index,
  668. pool_index, &pool_info);
  669. if (err)
  670. return err;
  671. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  672. if (!hdr)
  673. return -EMSGSIZE;
  674. if (devlink_nl_put_handle(msg, devlink))
  675. goto nla_put_failure;
  676. if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
  677. goto nla_put_failure;
  678. if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
  679. goto nla_put_failure;
  680. if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_info.pool_type))
  681. goto nla_put_failure;
  682. if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_SIZE, pool_info.size))
  683. goto nla_put_failure;
  684. if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE,
  685. pool_info.threshold_type))
  686. goto nla_put_failure;
  687. genlmsg_end(msg, hdr);
  688. return 0;
  689. nla_put_failure:
  690. genlmsg_cancel(msg, hdr);
  691. return -EMSGSIZE;
  692. }
  693. static int devlink_nl_cmd_sb_pool_get_doit(struct sk_buff *skb,
  694. struct genl_info *info)
  695. {
  696. struct devlink *devlink = info->user_ptr[0];
  697. struct devlink_sb *devlink_sb = info->user_ptr[1];
  698. struct sk_buff *msg;
  699. u16 pool_index;
  700. int err;
  701. err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
  702. &pool_index);
  703. if (err)
  704. return err;
  705. if (!devlink->ops || !devlink->ops->sb_pool_get)
  706. return -EOPNOTSUPP;
  707. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  708. if (!msg)
  709. return -ENOMEM;
  710. err = devlink_nl_sb_pool_fill(msg, devlink, devlink_sb, pool_index,
  711. DEVLINK_CMD_SB_POOL_NEW,
  712. info->snd_portid, info->snd_seq, 0);
  713. if (err) {
  714. nlmsg_free(msg);
  715. return err;
  716. }
  717. return genlmsg_reply(msg, info);
  718. }
  719. static int __sb_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
  720. struct devlink *devlink,
  721. struct devlink_sb *devlink_sb,
  722. u32 portid, u32 seq)
  723. {
  724. u16 pool_count = devlink_sb_pool_count(devlink_sb);
  725. u16 pool_index;
  726. int err;
  727. for (pool_index = 0; pool_index < pool_count; pool_index++) {
  728. if (*p_idx < start) {
  729. (*p_idx)++;
  730. continue;
  731. }
  732. err = devlink_nl_sb_pool_fill(msg, devlink,
  733. devlink_sb,
  734. pool_index,
  735. DEVLINK_CMD_SB_POOL_NEW,
  736. portid, seq, NLM_F_MULTI);
  737. if (err)
  738. return err;
  739. (*p_idx)++;
  740. }
  741. return 0;
  742. }
  743. static int devlink_nl_cmd_sb_pool_get_dumpit(struct sk_buff *msg,
  744. struct netlink_callback *cb)
  745. {
  746. struct devlink *devlink;
  747. struct devlink_sb *devlink_sb;
  748. int start = cb->args[0];
  749. int idx = 0;
  750. int err;
  751. mutex_lock(&devlink_mutex);
  752. list_for_each_entry(devlink, &devlink_list, list) {
  753. if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
  754. !devlink->ops || !devlink->ops->sb_pool_get)
  755. continue;
  756. list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
  757. err = __sb_pool_get_dumpit(msg, start, &idx, devlink,
  758. devlink_sb,
  759. NETLINK_CB(cb->skb).portid,
  760. cb->nlh->nlmsg_seq);
  761. if (err && err != -EOPNOTSUPP)
  762. goto out;
  763. }
  764. }
  765. out:
  766. mutex_unlock(&devlink_mutex);
  767. cb->args[0] = idx;
  768. return msg->len;
  769. }
  770. static int devlink_sb_pool_set(struct devlink *devlink, unsigned int sb_index,
  771. u16 pool_index, u32 size,
  772. enum devlink_sb_threshold_type threshold_type)
  773. {
  774. const struct devlink_ops *ops = devlink->ops;
  775. if (ops && ops->sb_pool_set)
  776. return ops->sb_pool_set(devlink, sb_index, pool_index,
  777. size, threshold_type);
  778. return -EOPNOTSUPP;
  779. }
  780. static int devlink_nl_cmd_sb_pool_set_doit(struct sk_buff *skb,
  781. struct genl_info *info)
  782. {
  783. struct devlink *devlink = info->user_ptr[0];
  784. struct devlink_sb *devlink_sb = info->user_ptr[1];
  785. enum devlink_sb_threshold_type threshold_type;
  786. u16 pool_index;
  787. u32 size;
  788. int err;
  789. err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
  790. &pool_index);
  791. if (err)
  792. return err;
  793. err = devlink_sb_th_type_get_from_info(info, &threshold_type);
  794. if (err)
  795. return err;
  796. if (!info->attrs[DEVLINK_ATTR_SB_POOL_SIZE])
  797. return -EINVAL;
  798. size = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_POOL_SIZE]);
  799. return devlink_sb_pool_set(devlink, devlink_sb->index,
  800. pool_index, size, threshold_type);
  801. }
  802. static int devlink_nl_sb_port_pool_fill(struct sk_buff *msg,
  803. struct devlink *devlink,
  804. struct devlink_port *devlink_port,
  805. struct devlink_sb *devlink_sb,
  806. u16 pool_index,
  807. enum devlink_command cmd,
  808. u32 portid, u32 seq, int flags)
  809. {
  810. const struct devlink_ops *ops = devlink->ops;
  811. u32 threshold;
  812. void *hdr;
  813. int err;
  814. err = ops->sb_port_pool_get(devlink_port, devlink_sb->index,
  815. pool_index, &threshold);
  816. if (err)
  817. return err;
  818. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  819. if (!hdr)
  820. return -EMSGSIZE;
  821. if (devlink_nl_put_handle(msg, devlink))
  822. goto nla_put_failure;
  823. if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
  824. goto nla_put_failure;
  825. if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
  826. goto nla_put_failure;
  827. if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
  828. goto nla_put_failure;
  829. if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
  830. goto nla_put_failure;
  831. if (ops->sb_occ_port_pool_get) {
  832. u32 cur;
  833. u32 max;
  834. err = ops->sb_occ_port_pool_get(devlink_port, devlink_sb->index,
  835. pool_index, &cur, &max);
  836. if (err && err != -EOPNOTSUPP)
  837. return err;
  838. if (!err) {
  839. if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
  840. goto nla_put_failure;
  841. if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
  842. goto nla_put_failure;
  843. }
  844. }
  845. genlmsg_end(msg, hdr);
  846. return 0;
  847. nla_put_failure:
  848. genlmsg_cancel(msg, hdr);
  849. return -EMSGSIZE;
  850. }
  851. static int devlink_nl_cmd_sb_port_pool_get_doit(struct sk_buff *skb,
  852. struct genl_info *info)
  853. {
  854. struct devlink_port *devlink_port = info->user_ptr[0];
  855. struct devlink *devlink = devlink_port->devlink;
  856. struct devlink_sb *devlink_sb = info->user_ptr[1];
  857. struct sk_buff *msg;
  858. u16 pool_index;
  859. int err;
  860. err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
  861. &pool_index);
  862. if (err)
  863. return err;
  864. if (!devlink->ops || !devlink->ops->sb_port_pool_get)
  865. return -EOPNOTSUPP;
  866. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  867. if (!msg)
  868. return -ENOMEM;
  869. err = devlink_nl_sb_port_pool_fill(msg, devlink, devlink_port,
  870. devlink_sb, pool_index,
  871. DEVLINK_CMD_SB_PORT_POOL_NEW,
  872. info->snd_portid, info->snd_seq, 0);
  873. if (err) {
  874. nlmsg_free(msg);
  875. return err;
  876. }
  877. return genlmsg_reply(msg, info);
  878. }
  879. static int __sb_port_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
  880. struct devlink *devlink,
  881. struct devlink_sb *devlink_sb,
  882. u32 portid, u32 seq)
  883. {
  884. struct devlink_port *devlink_port;
  885. u16 pool_count = devlink_sb_pool_count(devlink_sb);
  886. u16 pool_index;
  887. int err;
  888. list_for_each_entry(devlink_port, &devlink->port_list, list) {
  889. for (pool_index = 0; pool_index < pool_count; pool_index++) {
  890. if (*p_idx < start) {
  891. (*p_idx)++;
  892. continue;
  893. }
  894. err = devlink_nl_sb_port_pool_fill(msg, devlink,
  895. devlink_port,
  896. devlink_sb,
  897. pool_index,
  898. DEVLINK_CMD_SB_PORT_POOL_NEW,
  899. portid, seq,
  900. NLM_F_MULTI);
  901. if (err)
  902. return err;
  903. (*p_idx)++;
  904. }
  905. }
  906. return 0;
  907. }
  908. static int devlink_nl_cmd_sb_port_pool_get_dumpit(struct sk_buff *msg,
  909. struct netlink_callback *cb)
  910. {
  911. struct devlink *devlink;
  912. struct devlink_sb *devlink_sb;
  913. int start = cb->args[0];
  914. int idx = 0;
  915. int err;
  916. mutex_lock(&devlink_mutex);
  917. mutex_lock(&devlink_port_mutex);
  918. list_for_each_entry(devlink, &devlink_list, list) {
  919. if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
  920. !devlink->ops || !devlink->ops->sb_port_pool_get)
  921. continue;
  922. list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
  923. err = __sb_port_pool_get_dumpit(msg, start, &idx,
  924. devlink, devlink_sb,
  925. NETLINK_CB(cb->skb).portid,
  926. cb->nlh->nlmsg_seq);
  927. if (err && err != -EOPNOTSUPP)
  928. goto out;
  929. }
  930. }
  931. out:
  932. mutex_unlock(&devlink_port_mutex);
  933. mutex_unlock(&devlink_mutex);
  934. cb->args[0] = idx;
  935. return msg->len;
  936. }
  937. static int devlink_sb_port_pool_set(struct devlink_port *devlink_port,
  938. unsigned int sb_index, u16 pool_index,
  939. u32 threshold)
  940. {
  941. const struct devlink_ops *ops = devlink_port->devlink->ops;
  942. if (ops && ops->sb_port_pool_set)
  943. return ops->sb_port_pool_set(devlink_port, sb_index,
  944. pool_index, threshold);
  945. return -EOPNOTSUPP;
  946. }
  947. static int devlink_nl_cmd_sb_port_pool_set_doit(struct sk_buff *skb,
  948. struct genl_info *info)
  949. {
  950. struct devlink_port *devlink_port = info->user_ptr[0];
  951. struct devlink_sb *devlink_sb = info->user_ptr[1];
  952. u16 pool_index;
  953. u32 threshold;
  954. int err;
  955. err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
  956. &pool_index);
  957. if (err)
  958. return err;
  959. if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
  960. return -EINVAL;
  961. threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
  962. return devlink_sb_port_pool_set(devlink_port, devlink_sb->index,
  963. pool_index, threshold);
  964. }
  965. static int
  966. devlink_nl_sb_tc_pool_bind_fill(struct sk_buff *msg, struct devlink *devlink,
  967. struct devlink_port *devlink_port,
  968. struct devlink_sb *devlink_sb, u16 tc_index,
  969. enum devlink_sb_pool_type pool_type,
  970. enum devlink_command cmd,
  971. u32 portid, u32 seq, int flags)
  972. {
  973. const struct devlink_ops *ops = devlink->ops;
  974. u16 pool_index;
  975. u32 threshold;
  976. void *hdr;
  977. int err;
  978. err = ops->sb_tc_pool_bind_get(devlink_port, devlink_sb->index,
  979. tc_index, pool_type,
  980. &pool_index, &threshold);
  981. if (err)
  982. return err;
  983. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  984. if (!hdr)
  985. return -EMSGSIZE;
  986. if (devlink_nl_put_handle(msg, devlink))
  987. goto nla_put_failure;
  988. if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
  989. goto nla_put_failure;
  990. if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
  991. goto nla_put_failure;
  992. if (nla_put_u16(msg, DEVLINK_ATTR_SB_TC_INDEX, tc_index))
  993. goto nla_put_failure;
  994. if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_type))
  995. goto nla_put_failure;
  996. if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
  997. goto nla_put_failure;
  998. if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
  999. goto nla_put_failure;
  1000. if (ops->sb_occ_tc_port_bind_get) {
  1001. u32 cur;
  1002. u32 max;
  1003. err = ops->sb_occ_tc_port_bind_get(devlink_port,
  1004. devlink_sb->index,
  1005. tc_index, pool_type,
  1006. &cur, &max);
  1007. if (err && err != -EOPNOTSUPP)
  1008. return err;
  1009. if (!err) {
  1010. if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
  1011. goto nla_put_failure;
  1012. if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
  1013. goto nla_put_failure;
  1014. }
  1015. }
  1016. genlmsg_end(msg, hdr);
  1017. return 0;
  1018. nla_put_failure:
  1019. genlmsg_cancel(msg, hdr);
  1020. return -EMSGSIZE;
  1021. }
  1022. static int devlink_nl_cmd_sb_tc_pool_bind_get_doit(struct sk_buff *skb,
  1023. struct genl_info *info)
  1024. {
  1025. struct devlink_port *devlink_port = info->user_ptr[0];
  1026. struct devlink *devlink = devlink_port->devlink;
  1027. struct devlink_sb *devlink_sb = info->user_ptr[1];
  1028. struct sk_buff *msg;
  1029. enum devlink_sb_pool_type pool_type;
  1030. u16 tc_index;
  1031. int err;
  1032. err = devlink_sb_pool_type_get_from_info(info, &pool_type);
  1033. if (err)
  1034. return err;
  1035. err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
  1036. pool_type, &tc_index);
  1037. if (err)
  1038. return err;
  1039. if (!devlink->ops || !devlink->ops->sb_tc_pool_bind_get)
  1040. return -EOPNOTSUPP;
  1041. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1042. if (!msg)
  1043. return -ENOMEM;
  1044. err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink, devlink_port,
  1045. devlink_sb, tc_index, pool_type,
  1046. DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
  1047. info->snd_portid,
  1048. info->snd_seq, 0);
  1049. if (err) {
  1050. nlmsg_free(msg);
  1051. return err;
  1052. }
  1053. return genlmsg_reply(msg, info);
  1054. }
  1055. static int __sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
  1056. int start, int *p_idx,
  1057. struct devlink *devlink,
  1058. struct devlink_sb *devlink_sb,
  1059. u32 portid, u32 seq)
  1060. {
  1061. struct devlink_port *devlink_port;
  1062. u16 tc_index;
  1063. int err;
  1064. list_for_each_entry(devlink_port, &devlink->port_list, list) {
  1065. for (tc_index = 0;
  1066. tc_index < devlink_sb->ingress_tc_count; tc_index++) {
  1067. if (*p_idx < start) {
  1068. (*p_idx)++;
  1069. continue;
  1070. }
  1071. err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
  1072. devlink_port,
  1073. devlink_sb,
  1074. tc_index,
  1075. DEVLINK_SB_POOL_TYPE_INGRESS,
  1076. DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
  1077. portid, seq,
  1078. NLM_F_MULTI);
  1079. if (err)
  1080. return err;
  1081. (*p_idx)++;
  1082. }
  1083. for (tc_index = 0;
  1084. tc_index < devlink_sb->egress_tc_count; tc_index++) {
  1085. if (*p_idx < start) {
  1086. (*p_idx)++;
  1087. continue;
  1088. }
  1089. err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
  1090. devlink_port,
  1091. devlink_sb,
  1092. tc_index,
  1093. DEVLINK_SB_POOL_TYPE_EGRESS,
  1094. DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
  1095. portid, seq,
  1096. NLM_F_MULTI);
  1097. if (err)
  1098. return err;
  1099. (*p_idx)++;
  1100. }
  1101. }
  1102. return 0;
  1103. }
  1104. static int
  1105. devlink_nl_cmd_sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
  1106. struct netlink_callback *cb)
  1107. {
  1108. struct devlink *devlink;
  1109. struct devlink_sb *devlink_sb;
  1110. int start = cb->args[0];
  1111. int idx = 0;
  1112. int err;
  1113. mutex_lock(&devlink_mutex);
  1114. mutex_lock(&devlink_port_mutex);
  1115. list_for_each_entry(devlink, &devlink_list, list) {
  1116. if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
  1117. !devlink->ops || !devlink->ops->sb_tc_pool_bind_get)
  1118. continue;
  1119. list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
  1120. err = __sb_tc_pool_bind_get_dumpit(msg, start, &idx,
  1121. devlink,
  1122. devlink_sb,
  1123. NETLINK_CB(cb->skb).portid,
  1124. cb->nlh->nlmsg_seq);
  1125. if (err && err != -EOPNOTSUPP)
  1126. goto out;
  1127. }
  1128. }
  1129. out:
  1130. mutex_unlock(&devlink_port_mutex);
  1131. mutex_unlock(&devlink_mutex);
  1132. cb->args[0] = idx;
  1133. return msg->len;
  1134. }
  1135. static int devlink_sb_tc_pool_bind_set(struct devlink_port *devlink_port,
  1136. unsigned int sb_index, u16 tc_index,
  1137. enum devlink_sb_pool_type pool_type,
  1138. u16 pool_index, u32 threshold)
  1139. {
  1140. const struct devlink_ops *ops = devlink_port->devlink->ops;
  1141. if (ops && ops->sb_tc_pool_bind_set)
  1142. return ops->sb_tc_pool_bind_set(devlink_port, sb_index,
  1143. tc_index, pool_type,
  1144. pool_index, threshold);
  1145. return -EOPNOTSUPP;
  1146. }
  1147. static int devlink_nl_cmd_sb_tc_pool_bind_set_doit(struct sk_buff *skb,
  1148. struct genl_info *info)
  1149. {
  1150. struct devlink_port *devlink_port = info->user_ptr[0];
  1151. struct devlink_sb *devlink_sb = info->user_ptr[1];
  1152. enum devlink_sb_pool_type pool_type;
  1153. u16 tc_index;
  1154. u16 pool_index;
  1155. u32 threshold;
  1156. int err;
  1157. err = devlink_sb_pool_type_get_from_info(info, &pool_type);
  1158. if (err)
  1159. return err;
  1160. err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
  1161. pool_type, &tc_index);
  1162. if (err)
  1163. return err;
  1164. err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
  1165. &pool_index);
  1166. if (err)
  1167. return err;
  1168. if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
  1169. return -EINVAL;
  1170. threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
  1171. return devlink_sb_tc_pool_bind_set(devlink_port, devlink_sb->index,
  1172. tc_index, pool_type,
  1173. pool_index, threshold);
  1174. }
  1175. static int devlink_nl_cmd_sb_occ_snapshot_doit(struct sk_buff *skb,
  1176. struct genl_info *info)
  1177. {
  1178. struct devlink *devlink = info->user_ptr[0];
  1179. struct devlink_sb *devlink_sb = info->user_ptr[1];
  1180. const struct devlink_ops *ops = devlink->ops;
  1181. if (ops && ops->sb_occ_snapshot)
  1182. return ops->sb_occ_snapshot(devlink, devlink_sb->index);
  1183. return -EOPNOTSUPP;
  1184. }
  1185. static int devlink_nl_cmd_sb_occ_max_clear_doit(struct sk_buff *skb,
  1186. struct genl_info *info)
  1187. {
  1188. struct devlink *devlink = info->user_ptr[0];
  1189. struct devlink_sb *devlink_sb = info->user_ptr[1];
  1190. const struct devlink_ops *ops = devlink->ops;
  1191. if (ops && ops->sb_occ_max_clear)
  1192. return ops->sb_occ_max_clear(devlink, devlink_sb->index);
  1193. return -EOPNOTSUPP;
  1194. }
  1195. static const struct nla_policy devlink_nl_policy[DEVLINK_ATTR_MAX + 1] = {
  1196. [DEVLINK_ATTR_BUS_NAME] = { .type = NLA_NUL_STRING },
  1197. [DEVLINK_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING },
  1198. [DEVLINK_ATTR_PORT_INDEX] = { .type = NLA_U32 },
  1199. [DEVLINK_ATTR_PORT_TYPE] = { .type = NLA_U16 },
  1200. [DEVLINK_ATTR_PORT_SPLIT_COUNT] = { .type = NLA_U32 },
  1201. [DEVLINK_ATTR_SB_INDEX] = { .type = NLA_U32 },
  1202. [DEVLINK_ATTR_SB_POOL_INDEX] = { .type = NLA_U16 },
  1203. [DEVLINK_ATTR_SB_POOL_TYPE] = { .type = NLA_U8 },
  1204. [DEVLINK_ATTR_SB_POOL_SIZE] = { .type = NLA_U32 },
  1205. [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE] = { .type = NLA_U8 },
  1206. [DEVLINK_ATTR_SB_THRESHOLD] = { .type = NLA_U32 },
  1207. [DEVLINK_ATTR_SB_TC_INDEX] = { .type = NLA_U16 },
  1208. };
  1209. static const struct genl_ops devlink_nl_ops[] = {
  1210. {
  1211. .cmd = DEVLINK_CMD_GET,
  1212. .doit = devlink_nl_cmd_get_doit,
  1213. .dumpit = devlink_nl_cmd_get_dumpit,
  1214. .policy = devlink_nl_policy,
  1215. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  1216. /* can be retrieved by unprivileged users */
  1217. },
  1218. {
  1219. .cmd = DEVLINK_CMD_PORT_GET,
  1220. .doit = devlink_nl_cmd_port_get_doit,
  1221. .dumpit = devlink_nl_cmd_port_get_dumpit,
  1222. .policy = devlink_nl_policy,
  1223. .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
  1224. /* can be retrieved by unprivileged users */
  1225. },
  1226. {
  1227. .cmd = DEVLINK_CMD_PORT_SET,
  1228. .doit = devlink_nl_cmd_port_set_doit,
  1229. .policy = devlink_nl_policy,
  1230. .flags = GENL_ADMIN_PERM,
  1231. .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
  1232. },
  1233. {
  1234. .cmd = DEVLINK_CMD_PORT_SPLIT,
  1235. .doit = devlink_nl_cmd_port_split_doit,
  1236. .policy = devlink_nl_policy,
  1237. .flags = GENL_ADMIN_PERM,
  1238. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  1239. },
  1240. {
  1241. .cmd = DEVLINK_CMD_PORT_UNSPLIT,
  1242. .doit = devlink_nl_cmd_port_unsplit_doit,
  1243. .policy = devlink_nl_policy,
  1244. .flags = GENL_ADMIN_PERM,
  1245. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  1246. },
  1247. {
  1248. .cmd = DEVLINK_CMD_SB_GET,
  1249. .doit = devlink_nl_cmd_sb_get_doit,
  1250. .dumpit = devlink_nl_cmd_sb_get_dumpit,
  1251. .policy = devlink_nl_policy,
  1252. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  1253. DEVLINK_NL_FLAG_NEED_SB,
  1254. /* can be retrieved by unprivileged users */
  1255. },
  1256. {
  1257. .cmd = DEVLINK_CMD_SB_POOL_GET,
  1258. .doit = devlink_nl_cmd_sb_pool_get_doit,
  1259. .dumpit = devlink_nl_cmd_sb_pool_get_dumpit,
  1260. .policy = devlink_nl_policy,
  1261. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  1262. DEVLINK_NL_FLAG_NEED_SB,
  1263. /* can be retrieved by unprivileged users */
  1264. },
  1265. {
  1266. .cmd = DEVLINK_CMD_SB_POOL_SET,
  1267. .doit = devlink_nl_cmd_sb_pool_set_doit,
  1268. .policy = devlink_nl_policy,
  1269. .flags = GENL_ADMIN_PERM,
  1270. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  1271. DEVLINK_NL_FLAG_NEED_SB,
  1272. },
  1273. {
  1274. .cmd = DEVLINK_CMD_SB_PORT_POOL_GET,
  1275. .doit = devlink_nl_cmd_sb_port_pool_get_doit,
  1276. .dumpit = devlink_nl_cmd_sb_port_pool_get_dumpit,
  1277. .policy = devlink_nl_policy,
  1278. .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
  1279. DEVLINK_NL_FLAG_NEED_SB,
  1280. /* can be retrieved by unprivileged users */
  1281. },
  1282. {
  1283. .cmd = DEVLINK_CMD_SB_PORT_POOL_SET,
  1284. .doit = devlink_nl_cmd_sb_port_pool_set_doit,
  1285. .policy = devlink_nl_policy,
  1286. .flags = GENL_ADMIN_PERM,
  1287. .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
  1288. DEVLINK_NL_FLAG_NEED_SB,
  1289. },
  1290. {
  1291. .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_GET,
  1292. .doit = devlink_nl_cmd_sb_tc_pool_bind_get_doit,
  1293. .dumpit = devlink_nl_cmd_sb_tc_pool_bind_get_dumpit,
  1294. .policy = devlink_nl_policy,
  1295. .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
  1296. DEVLINK_NL_FLAG_NEED_SB,
  1297. /* can be retrieved by unprivileged users */
  1298. },
  1299. {
  1300. .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_SET,
  1301. .doit = devlink_nl_cmd_sb_tc_pool_bind_set_doit,
  1302. .policy = devlink_nl_policy,
  1303. .flags = GENL_ADMIN_PERM,
  1304. .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
  1305. DEVLINK_NL_FLAG_NEED_SB,
  1306. },
  1307. {
  1308. .cmd = DEVLINK_CMD_SB_OCC_SNAPSHOT,
  1309. .doit = devlink_nl_cmd_sb_occ_snapshot_doit,
  1310. .policy = devlink_nl_policy,
  1311. .flags = GENL_ADMIN_PERM,
  1312. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  1313. DEVLINK_NL_FLAG_NEED_SB |
  1314. DEVLINK_NL_FLAG_LOCK_PORTS,
  1315. },
  1316. {
  1317. .cmd = DEVLINK_CMD_SB_OCC_MAX_CLEAR,
  1318. .doit = devlink_nl_cmd_sb_occ_max_clear_doit,
  1319. .policy = devlink_nl_policy,
  1320. .flags = GENL_ADMIN_PERM,
  1321. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  1322. DEVLINK_NL_FLAG_NEED_SB |
  1323. DEVLINK_NL_FLAG_LOCK_PORTS,
  1324. },
  1325. };
  1326. /**
  1327. * devlink_alloc - Allocate new devlink instance resources
  1328. *
  1329. * @ops: ops
  1330. * @priv_size: size of user private data
  1331. *
  1332. * Allocate new devlink instance resources, including devlink index
  1333. * and name.
  1334. */
  1335. struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size)
  1336. {
  1337. struct devlink *devlink;
  1338. devlink = kzalloc(sizeof(*devlink) + priv_size, GFP_KERNEL);
  1339. if (!devlink)
  1340. return NULL;
  1341. devlink->ops = ops;
  1342. devlink_net_set(devlink, &init_net);
  1343. INIT_LIST_HEAD(&devlink->port_list);
  1344. INIT_LIST_HEAD(&devlink->sb_list);
  1345. return devlink;
  1346. }
  1347. EXPORT_SYMBOL_GPL(devlink_alloc);
  1348. /**
  1349. * devlink_register - Register devlink instance
  1350. *
  1351. * @devlink: devlink
  1352. */
  1353. int devlink_register(struct devlink *devlink, struct device *dev)
  1354. {
  1355. mutex_lock(&devlink_mutex);
  1356. devlink->dev = dev;
  1357. list_add_tail(&devlink->list, &devlink_list);
  1358. devlink_notify(devlink, DEVLINK_CMD_NEW);
  1359. mutex_unlock(&devlink_mutex);
  1360. return 0;
  1361. }
  1362. EXPORT_SYMBOL_GPL(devlink_register);
  1363. /**
  1364. * devlink_unregister - Unregister devlink instance
  1365. *
  1366. * @devlink: devlink
  1367. */
  1368. void devlink_unregister(struct devlink *devlink)
  1369. {
  1370. mutex_lock(&devlink_mutex);
  1371. devlink_notify(devlink, DEVLINK_CMD_DEL);
  1372. list_del(&devlink->list);
  1373. mutex_unlock(&devlink_mutex);
  1374. }
  1375. EXPORT_SYMBOL_GPL(devlink_unregister);
  1376. /**
  1377. * devlink_free - Free devlink instance resources
  1378. *
  1379. * @devlink: devlink
  1380. */
  1381. void devlink_free(struct devlink *devlink)
  1382. {
  1383. kfree(devlink);
  1384. }
  1385. EXPORT_SYMBOL_GPL(devlink_free);
  1386. /**
  1387. * devlink_port_register - Register devlink port
  1388. *
  1389. * @devlink: devlink
  1390. * @devlink_port: devlink port
  1391. * @port_index
  1392. *
  1393. * Register devlink port with provided port index. User can use
  1394. * any indexing, even hw-related one. devlink_port structure
  1395. * is convenient to be embedded inside user driver private structure.
  1396. * Note that the caller should take care of zeroing the devlink_port
  1397. * structure.
  1398. */
  1399. int devlink_port_register(struct devlink *devlink,
  1400. struct devlink_port *devlink_port,
  1401. unsigned int port_index)
  1402. {
  1403. mutex_lock(&devlink_port_mutex);
  1404. if (devlink_port_index_exists(devlink, port_index)) {
  1405. mutex_unlock(&devlink_port_mutex);
  1406. return -EEXIST;
  1407. }
  1408. devlink_port->devlink = devlink;
  1409. devlink_port->index = port_index;
  1410. devlink_port->registered = true;
  1411. list_add_tail(&devlink_port->list, &devlink->port_list);
  1412. mutex_unlock(&devlink_port_mutex);
  1413. devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
  1414. return 0;
  1415. }
  1416. EXPORT_SYMBOL_GPL(devlink_port_register);
  1417. /**
  1418. * devlink_port_unregister - Unregister devlink port
  1419. *
  1420. * @devlink_port: devlink port
  1421. */
  1422. void devlink_port_unregister(struct devlink_port *devlink_port)
  1423. {
  1424. devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL);
  1425. mutex_lock(&devlink_port_mutex);
  1426. list_del(&devlink_port->list);
  1427. mutex_unlock(&devlink_port_mutex);
  1428. }
  1429. EXPORT_SYMBOL_GPL(devlink_port_unregister);
  1430. static void __devlink_port_type_set(struct devlink_port *devlink_port,
  1431. enum devlink_port_type type,
  1432. void *type_dev)
  1433. {
  1434. devlink_port->type = type;
  1435. devlink_port->type_dev = type_dev;
  1436. devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
  1437. }
  1438. /**
  1439. * devlink_port_type_eth_set - Set port type to Ethernet
  1440. *
  1441. * @devlink_port: devlink port
  1442. * @netdev: related netdevice
  1443. */
  1444. void devlink_port_type_eth_set(struct devlink_port *devlink_port,
  1445. struct net_device *netdev)
  1446. {
  1447. return __devlink_port_type_set(devlink_port,
  1448. DEVLINK_PORT_TYPE_ETH, netdev);
  1449. }
  1450. EXPORT_SYMBOL_GPL(devlink_port_type_eth_set);
  1451. /**
  1452. * devlink_port_type_ib_set - Set port type to InfiniBand
  1453. *
  1454. * @devlink_port: devlink port
  1455. * @ibdev: related IB device
  1456. */
  1457. void devlink_port_type_ib_set(struct devlink_port *devlink_port,
  1458. struct ib_device *ibdev)
  1459. {
  1460. return __devlink_port_type_set(devlink_port,
  1461. DEVLINK_PORT_TYPE_IB, ibdev);
  1462. }
  1463. EXPORT_SYMBOL_GPL(devlink_port_type_ib_set);
  1464. /**
  1465. * devlink_port_type_clear - Clear port type
  1466. *
  1467. * @devlink_port: devlink port
  1468. */
  1469. void devlink_port_type_clear(struct devlink_port *devlink_port)
  1470. {
  1471. return __devlink_port_type_set(devlink_port,
  1472. DEVLINK_PORT_TYPE_NOTSET, NULL);
  1473. }
  1474. EXPORT_SYMBOL_GPL(devlink_port_type_clear);
  1475. /**
  1476. * devlink_port_split_set - Set port is split
  1477. *
  1478. * @devlink_port: devlink port
  1479. * @split_group: split group - identifies group split port is part of
  1480. */
  1481. void devlink_port_split_set(struct devlink_port *devlink_port,
  1482. u32 split_group)
  1483. {
  1484. devlink_port->split = true;
  1485. devlink_port->split_group = split_group;
  1486. devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
  1487. }
  1488. EXPORT_SYMBOL_GPL(devlink_port_split_set);
  1489. int devlink_sb_register(struct devlink *devlink, unsigned int sb_index,
  1490. u32 size, u16 ingress_pools_count,
  1491. u16 egress_pools_count, u16 ingress_tc_count,
  1492. u16 egress_tc_count)
  1493. {
  1494. struct devlink_sb *devlink_sb;
  1495. int err = 0;
  1496. mutex_lock(&devlink_mutex);
  1497. if (devlink_sb_index_exists(devlink, sb_index)) {
  1498. err = -EEXIST;
  1499. goto unlock;
  1500. }
  1501. devlink_sb = kzalloc(sizeof(*devlink_sb), GFP_KERNEL);
  1502. if (!devlink_sb) {
  1503. err = -ENOMEM;
  1504. goto unlock;
  1505. }
  1506. devlink_sb->index = sb_index;
  1507. devlink_sb->size = size;
  1508. devlink_sb->ingress_pools_count = ingress_pools_count;
  1509. devlink_sb->egress_pools_count = egress_pools_count;
  1510. devlink_sb->ingress_tc_count = ingress_tc_count;
  1511. devlink_sb->egress_tc_count = egress_tc_count;
  1512. list_add_tail(&devlink_sb->list, &devlink->sb_list);
  1513. unlock:
  1514. mutex_unlock(&devlink_mutex);
  1515. return err;
  1516. }
  1517. EXPORT_SYMBOL_GPL(devlink_sb_register);
  1518. void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index)
  1519. {
  1520. struct devlink_sb *devlink_sb;
  1521. mutex_lock(&devlink_mutex);
  1522. devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
  1523. WARN_ON(!devlink_sb);
  1524. list_del(&devlink_sb->list);
  1525. mutex_unlock(&devlink_mutex);
  1526. kfree(devlink_sb);
  1527. }
  1528. EXPORT_SYMBOL_GPL(devlink_sb_unregister);
  1529. static int __init devlink_module_init(void)
  1530. {
  1531. return genl_register_family_with_ops_groups(&devlink_nl_family,
  1532. devlink_nl_ops,
  1533. devlink_nl_mcgrps);
  1534. }
  1535. static void __exit devlink_module_exit(void)
  1536. {
  1537. genl_unregister_family(&devlink_nl_family);
  1538. }
  1539. module_init(devlink_module_init);
  1540. module_exit(devlink_module_exit);
  1541. MODULE_LICENSE("GPL v2");
  1542. MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
  1543. MODULE_DESCRIPTION("Network physical device Netlink interface");
  1544. MODULE_ALIAS_GENL_FAMILY(DEVLINK_GENL_NAME);