devlink.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897
  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_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. void *hdr;
  1198. int err = 0;
  1199. u16 mode;
  1200. u8 inline_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 out;
  1207. err = ops->eswitch_mode_get(devlink, &mode);
  1208. if (err)
  1209. goto out;
  1210. err = nla_put_u16(msg, DEVLINK_ATTR_ESWITCH_MODE, mode);
  1211. if (err)
  1212. goto out;
  1213. if (ops->eswitch_inline_mode_get) {
  1214. err = ops->eswitch_inline_mode_get(devlink, &inline_mode);
  1215. if (err)
  1216. goto out;
  1217. err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_INLINE_MODE,
  1218. inline_mode);
  1219. if (err)
  1220. goto out;
  1221. }
  1222. genlmsg_end(msg, hdr);
  1223. return 0;
  1224. out:
  1225. genlmsg_cancel(msg, hdr);
  1226. return err;
  1227. }
  1228. static int devlink_nl_cmd_eswitch_mode_get_doit(struct sk_buff *skb,
  1229. struct genl_info *info)
  1230. {
  1231. struct devlink *devlink = info->user_ptr[0];
  1232. const struct devlink_ops *ops = devlink->ops;
  1233. struct sk_buff *msg;
  1234. int err;
  1235. if (!ops || !ops->eswitch_mode_get)
  1236. return -EOPNOTSUPP;
  1237. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1238. if (!msg)
  1239. return -ENOMEM;
  1240. err = devlink_eswitch_fill(msg, devlink, DEVLINK_CMD_ESWITCH_MODE_GET,
  1241. info->snd_portid, info->snd_seq, 0);
  1242. if (err) {
  1243. nlmsg_free(msg);
  1244. return err;
  1245. }
  1246. return genlmsg_reply(msg, info);
  1247. }
  1248. static int devlink_nl_cmd_eswitch_mode_set_doit(struct sk_buff *skb,
  1249. struct genl_info *info)
  1250. {
  1251. struct devlink *devlink = info->user_ptr[0];
  1252. const struct devlink_ops *ops = devlink->ops;
  1253. u16 mode;
  1254. u8 inline_mode;
  1255. int err = 0;
  1256. if (!ops)
  1257. return -EOPNOTSUPP;
  1258. if (info->attrs[DEVLINK_ATTR_ESWITCH_MODE]) {
  1259. if (!ops->eswitch_mode_set)
  1260. return -EOPNOTSUPP;
  1261. mode = nla_get_u16(info->attrs[DEVLINK_ATTR_ESWITCH_MODE]);
  1262. err = ops->eswitch_mode_set(devlink, mode);
  1263. if (err)
  1264. return err;
  1265. }
  1266. if (info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]) {
  1267. if (!ops->eswitch_inline_mode_set)
  1268. return -EOPNOTSUPP;
  1269. inline_mode = nla_get_u8(
  1270. info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]);
  1271. err = ops->eswitch_inline_mode_set(devlink, inline_mode);
  1272. if (err)
  1273. return err;
  1274. }
  1275. return 0;
  1276. }
  1277. static const struct nla_policy devlink_nl_policy[DEVLINK_ATTR_MAX + 1] = {
  1278. [DEVLINK_ATTR_BUS_NAME] = { .type = NLA_NUL_STRING },
  1279. [DEVLINK_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING },
  1280. [DEVLINK_ATTR_PORT_INDEX] = { .type = NLA_U32 },
  1281. [DEVLINK_ATTR_PORT_TYPE] = { .type = NLA_U16 },
  1282. [DEVLINK_ATTR_PORT_SPLIT_COUNT] = { .type = NLA_U32 },
  1283. [DEVLINK_ATTR_SB_INDEX] = { .type = NLA_U32 },
  1284. [DEVLINK_ATTR_SB_POOL_INDEX] = { .type = NLA_U16 },
  1285. [DEVLINK_ATTR_SB_POOL_TYPE] = { .type = NLA_U8 },
  1286. [DEVLINK_ATTR_SB_POOL_SIZE] = { .type = NLA_U32 },
  1287. [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE] = { .type = NLA_U8 },
  1288. [DEVLINK_ATTR_SB_THRESHOLD] = { .type = NLA_U32 },
  1289. [DEVLINK_ATTR_SB_TC_INDEX] = { .type = NLA_U16 },
  1290. [DEVLINK_ATTR_ESWITCH_MODE] = { .type = NLA_U16 },
  1291. [DEVLINK_ATTR_ESWITCH_INLINE_MODE] = { .type = NLA_U8 },
  1292. };
  1293. static const struct genl_ops devlink_nl_ops[] = {
  1294. {
  1295. .cmd = DEVLINK_CMD_GET,
  1296. .doit = devlink_nl_cmd_get_doit,
  1297. .dumpit = devlink_nl_cmd_get_dumpit,
  1298. .policy = devlink_nl_policy,
  1299. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  1300. /* can be retrieved by unprivileged users */
  1301. },
  1302. {
  1303. .cmd = DEVLINK_CMD_PORT_GET,
  1304. .doit = devlink_nl_cmd_port_get_doit,
  1305. .dumpit = devlink_nl_cmd_port_get_dumpit,
  1306. .policy = devlink_nl_policy,
  1307. .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
  1308. /* can be retrieved by unprivileged users */
  1309. },
  1310. {
  1311. .cmd = DEVLINK_CMD_PORT_SET,
  1312. .doit = devlink_nl_cmd_port_set_doit,
  1313. .policy = devlink_nl_policy,
  1314. .flags = GENL_ADMIN_PERM,
  1315. .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
  1316. },
  1317. {
  1318. .cmd = DEVLINK_CMD_PORT_SPLIT,
  1319. .doit = devlink_nl_cmd_port_split_doit,
  1320. .policy = devlink_nl_policy,
  1321. .flags = GENL_ADMIN_PERM,
  1322. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  1323. },
  1324. {
  1325. .cmd = DEVLINK_CMD_PORT_UNSPLIT,
  1326. .doit = devlink_nl_cmd_port_unsplit_doit,
  1327. .policy = devlink_nl_policy,
  1328. .flags = GENL_ADMIN_PERM,
  1329. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  1330. },
  1331. {
  1332. .cmd = DEVLINK_CMD_SB_GET,
  1333. .doit = devlink_nl_cmd_sb_get_doit,
  1334. .dumpit = devlink_nl_cmd_sb_get_dumpit,
  1335. .policy = devlink_nl_policy,
  1336. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  1337. DEVLINK_NL_FLAG_NEED_SB,
  1338. /* can be retrieved by unprivileged users */
  1339. },
  1340. {
  1341. .cmd = DEVLINK_CMD_SB_POOL_GET,
  1342. .doit = devlink_nl_cmd_sb_pool_get_doit,
  1343. .dumpit = devlink_nl_cmd_sb_pool_get_dumpit,
  1344. .policy = devlink_nl_policy,
  1345. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  1346. DEVLINK_NL_FLAG_NEED_SB,
  1347. /* can be retrieved by unprivileged users */
  1348. },
  1349. {
  1350. .cmd = DEVLINK_CMD_SB_POOL_SET,
  1351. .doit = devlink_nl_cmd_sb_pool_set_doit,
  1352. .policy = devlink_nl_policy,
  1353. .flags = GENL_ADMIN_PERM,
  1354. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  1355. DEVLINK_NL_FLAG_NEED_SB,
  1356. },
  1357. {
  1358. .cmd = DEVLINK_CMD_SB_PORT_POOL_GET,
  1359. .doit = devlink_nl_cmd_sb_port_pool_get_doit,
  1360. .dumpit = devlink_nl_cmd_sb_port_pool_get_dumpit,
  1361. .policy = devlink_nl_policy,
  1362. .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
  1363. DEVLINK_NL_FLAG_NEED_SB,
  1364. /* can be retrieved by unprivileged users */
  1365. },
  1366. {
  1367. .cmd = DEVLINK_CMD_SB_PORT_POOL_SET,
  1368. .doit = devlink_nl_cmd_sb_port_pool_set_doit,
  1369. .policy = devlink_nl_policy,
  1370. .flags = GENL_ADMIN_PERM,
  1371. .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
  1372. DEVLINK_NL_FLAG_NEED_SB,
  1373. },
  1374. {
  1375. .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_GET,
  1376. .doit = devlink_nl_cmd_sb_tc_pool_bind_get_doit,
  1377. .dumpit = devlink_nl_cmd_sb_tc_pool_bind_get_dumpit,
  1378. .policy = devlink_nl_policy,
  1379. .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
  1380. DEVLINK_NL_FLAG_NEED_SB,
  1381. /* can be retrieved by unprivileged users */
  1382. },
  1383. {
  1384. .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_SET,
  1385. .doit = devlink_nl_cmd_sb_tc_pool_bind_set_doit,
  1386. .policy = devlink_nl_policy,
  1387. .flags = GENL_ADMIN_PERM,
  1388. .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
  1389. DEVLINK_NL_FLAG_NEED_SB,
  1390. },
  1391. {
  1392. .cmd = DEVLINK_CMD_SB_OCC_SNAPSHOT,
  1393. .doit = devlink_nl_cmd_sb_occ_snapshot_doit,
  1394. .policy = devlink_nl_policy,
  1395. .flags = GENL_ADMIN_PERM,
  1396. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  1397. DEVLINK_NL_FLAG_NEED_SB |
  1398. DEVLINK_NL_FLAG_LOCK_PORTS,
  1399. },
  1400. {
  1401. .cmd = DEVLINK_CMD_SB_OCC_MAX_CLEAR,
  1402. .doit = devlink_nl_cmd_sb_occ_max_clear_doit,
  1403. .policy = devlink_nl_policy,
  1404. .flags = GENL_ADMIN_PERM,
  1405. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  1406. DEVLINK_NL_FLAG_NEED_SB |
  1407. DEVLINK_NL_FLAG_LOCK_PORTS,
  1408. },
  1409. {
  1410. .cmd = DEVLINK_CMD_ESWITCH_MODE_GET,
  1411. .doit = devlink_nl_cmd_eswitch_mode_get_doit,
  1412. .policy = devlink_nl_policy,
  1413. .flags = GENL_ADMIN_PERM,
  1414. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  1415. },
  1416. {
  1417. .cmd = DEVLINK_CMD_ESWITCH_MODE_SET,
  1418. .doit = devlink_nl_cmd_eswitch_mode_set_doit,
  1419. .policy = devlink_nl_policy,
  1420. .flags = GENL_ADMIN_PERM,
  1421. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  1422. },
  1423. };
  1424. static struct genl_family devlink_nl_family __ro_after_init = {
  1425. .name = DEVLINK_GENL_NAME,
  1426. .version = DEVLINK_GENL_VERSION,
  1427. .maxattr = DEVLINK_ATTR_MAX,
  1428. .netnsok = true,
  1429. .pre_doit = devlink_nl_pre_doit,
  1430. .post_doit = devlink_nl_post_doit,
  1431. .module = THIS_MODULE,
  1432. .ops = devlink_nl_ops,
  1433. .n_ops = ARRAY_SIZE(devlink_nl_ops),
  1434. .mcgrps = devlink_nl_mcgrps,
  1435. .n_mcgrps = ARRAY_SIZE(devlink_nl_mcgrps),
  1436. };
  1437. /**
  1438. * devlink_alloc - Allocate new devlink instance resources
  1439. *
  1440. * @ops: ops
  1441. * @priv_size: size of user private data
  1442. *
  1443. * Allocate new devlink instance resources, including devlink index
  1444. * and name.
  1445. */
  1446. struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size)
  1447. {
  1448. struct devlink *devlink;
  1449. devlink = kzalloc(sizeof(*devlink) + priv_size, GFP_KERNEL);
  1450. if (!devlink)
  1451. return NULL;
  1452. devlink->ops = ops;
  1453. devlink_net_set(devlink, &init_net);
  1454. INIT_LIST_HEAD(&devlink->port_list);
  1455. INIT_LIST_HEAD(&devlink->sb_list);
  1456. return devlink;
  1457. }
  1458. EXPORT_SYMBOL_GPL(devlink_alloc);
  1459. /**
  1460. * devlink_register - Register devlink instance
  1461. *
  1462. * @devlink: devlink
  1463. */
  1464. int devlink_register(struct devlink *devlink, struct device *dev)
  1465. {
  1466. mutex_lock(&devlink_mutex);
  1467. devlink->dev = dev;
  1468. list_add_tail(&devlink->list, &devlink_list);
  1469. devlink_notify(devlink, DEVLINK_CMD_NEW);
  1470. mutex_unlock(&devlink_mutex);
  1471. return 0;
  1472. }
  1473. EXPORT_SYMBOL_GPL(devlink_register);
  1474. /**
  1475. * devlink_unregister - Unregister devlink instance
  1476. *
  1477. * @devlink: devlink
  1478. */
  1479. void devlink_unregister(struct devlink *devlink)
  1480. {
  1481. mutex_lock(&devlink_mutex);
  1482. devlink_notify(devlink, DEVLINK_CMD_DEL);
  1483. list_del(&devlink->list);
  1484. mutex_unlock(&devlink_mutex);
  1485. }
  1486. EXPORT_SYMBOL_GPL(devlink_unregister);
  1487. /**
  1488. * devlink_free - Free devlink instance resources
  1489. *
  1490. * @devlink: devlink
  1491. */
  1492. void devlink_free(struct devlink *devlink)
  1493. {
  1494. kfree(devlink);
  1495. }
  1496. EXPORT_SYMBOL_GPL(devlink_free);
  1497. /**
  1498. * devlink_port_register - Register devlink port
  1499. *
  1500. * @devlink: devlink
  1501. * @devlink_port: devlink port
  1502. * @port_index
  1503. *
  1504. * Register devlink port with provided port index. User can use
  1505. * any indexing, even hw-related one. devlink_port structure
  1506. * is convenient to be embedded inside user driver private structure.
  1507. * Note that the caller should take care of zeroing the devlink_port
  1508. * structure.
  1509. */
  1510. int devlink_port_register(struct devlink *devlink,
  1511. struct devlink_port *devlink_port,
  1512. unsigned int port_index)
  1513. {
  1514. mutex_lock(&devlink_port_mutex);
  1515. if (devlink_port_index_exists(devlink, port_index)) {
  1516. mutex_unlock(&devlink_port_mutex);
  1517. return -EEXIST;
  1518. }
  1519. devlink_port->devlink = devlink;
  1520. devlink_port->index = port_index;
  1521. devlink_port->registered = true;
  1522. list_add_tail(&devlink_port->list, &devlink->port_list);
  1523. mutex_unlock(&devlink_port_mutex);
  1524. devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
  1525. return 0;
  1526. }
  1527. EXPORT_SYMBOL_GPL(devlink_port_register);
  1528. /**
  1529. * devlink_port_unregister - Unregister devlink port
  1530. *
  1531. * @devlink_port: devlink port
  1532. */
  1533. void devlink_port_unregister(struct devlink_port *devlink_port)
  1534. {
  1535. devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL);
  1536. mutex_lock(&devlink_port_mutex);
  1537. list_del(&devlink_port->list);
  1538. mutex_unlock(&devlink_port_mutex);
  1539. }
  1540. EXPORT_SYMBOL_GPL(devlink_port_unregister);
  1541. static void __devlink_port_type_set(struct devlink_port *devlink_port,
  1542. enum devlink_port_type type,
  1543. void *type_dev)
  1544. {
  1545. devlink_port->type = type;
  1546. devlink_port->type_dev = type_dev;
  1547. devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
  1548. }
  1549. /**
  1550. * devlink_port_type_eth_set - Set port type to Ethernet
  1551. *
  1552. * @devlink_port: devlink port
  1553. * @netdev: related netdevice
  1554. */
  1555. void devlink_port_type_eth_set(struct devlink_port *devlink_port,
  1556. struct net_device *netdev)
  1557. {
  1558. return __devlink_port_type_set(devlink_port,
  1559. DEVLINK_PORT_TYPE_ETH, netdev);
  1560. }
  1561. EXPORT_SYMBOL_GPL(devlink_port_type_eth_set);
  1562. /**
  1563. * devlink_port_type_ib_set - Set port type to InfiniBand
  1564. *
  1565. * @devlink_port: devlink port
  1566. * @ibdev: related IB device
  1567. */
  1568. void devlink_port_type_ib_set(struct devlink_port *devlink_port,
  1569. struct ib_device *ibdev)
  1570. {
  1571. return __devlink_port_type_set(devlink_port,
  1572. DEVLINK_PORT_TYPE_IB, ibdev);
  1573. }
  1574. EXPORT_SYMBOL_GPL(devlink_port_type_ib_set);
  1575. /**
  1576. * devlink_port_type_clear - Clear port type
  1577. *
  1578. * @devlink_port: devlink port
  1579. */
  1580. void devlink_port_type_clear(struct devlink_port *devlink_port)
  1581. {
  1582. return __devlink_port_type_set(devlink_port,
  1583. DEVLINK_PORT_TYPE_NOTSET, NULL);
  1584. }
  1585. EXPORT_SYMBOL_GPL(devlink_port_type_clear);
  1586. /**
  1587. * devlink_port_split_set - Set port is split
  1588. *
  1589. * @devlink_port: devlink port
  1590. * @split_group: split group - identifies group split port is part of
  1591. */
  1592. void devlink_port_split_set(struct devlink_port *devlink_port,
  1593. u32 split_group)
  1594. {
  1595. devlink_port->split = true;
  1596. devlink_port->split_group = split_group;
  1597. devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
  1598. }
  1599. EXPORT_SYMBOL_GPL(devlink_port_split_set);
  1600. int devlink_sb_register(struct devlink *devlink, unsigned int sb_index,
  1601. u32 size, u16 ingress_pools_count,
  1602. u16 egress_pools_count, u16 ingress_tc_count,
  1603. u16 egress_tc_count)
  1604. {
  1605. struct devlink_sb *devlink_sb;
  1606. int err = 0;
  1607. mutex_lock(&devlink_mutex);
  1608. if (devlink_sb_index_exists(devlink, sb_index)) {
  1609. err = -EEXIST;
  1610. goto unlock;
  1611. }
  1612. devlink_sb = kzalloc(sizeof(*devlink_sb), GFP_KERNEL);
  1613. if (!devlink_sb) {
  1614. err = -ENOMEM;
  1615. goto unlock;
  1616. }
  1617. devlink_sb->index = sb_index;
  1618. devlink_sb->size = size;
  1619. devlink_sb->ingress_pools_count = ingress_pools_count;
  1620. devlink_sb->egress_pools_count = egress_pools_count;
  1621. devlink_sb->ingress_tc_count = ingress_tc_count;
  1622. devlink_sb->egress_tc_count = egress_tc_count;
  1623. list_add_tail(&devlink_sb->list, &devlink->sb_list);
  1624. unlock:
  1625. mutex_unlock(&devlink_mutex);
  1626. return err;
  1627. }
  1628. EXPORT_SYMBOL_GPL(devlink_sb_register);
  1629. void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index)
  1630. {
  1631. struct devlink_sb *devlink_sb;
  1632. mutex_lock(&devlink_mutex);
  1633. devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
  1634. WARN_ON(!devlink_sb);
  1635. list_del(&devlink_sb->list);
  1636. mutex_unlock(&devlink_mutex);
  1637. kfree(devlink_sb);
  1638. }
  1639. EXPORT_SYMBOL_GPL(devlink_sb_unregister);
  1640. static int __init devlink_module_init(void)
  1641. {
  1642. return genl_register_family(&devlink_nl_family);
  1643. }
  1644. static void __exit devlink_module_exit(void)
  1645. {
  1646. genl_unregister_family(&devlink_nl_family);
  1647. }
  1648. module_init(devlink_module_init);
  1649. module_exit(devlink_module_exit);
  1650. MODULE_LICENSE("GPL v2");
  1651. MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
  1652. MODULE_DESCRIPTION("Network physical device Netlink interface");
  1653. MODULE_ALIAS_GENL_FAMILY(DEVLINK_GENL_NAME);