devlink.c 48 KB

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