devlink.c 48 KB

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