dpipe.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 2016 Mellanox Technologies. All rights reserved.
  4. * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com>
  5. */
  6. #include "devl_internal.h"
  7. static struct devlink_dpipe_field devlink_dpipe_fields_ethernet[] = {
  8. {
  9. .name = "destination mac",
  10. .id = DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC,
  11. .bitwidth = 48,
  12. },
  13. };
  14. struct devlink_dpipe_header devlink_dpipe_header_ethernet = {
  15. .name = "ethernet",
  16. .id = DEVLINK_DPIPE_HEADER_ETHERNET,
  17. .fields = devlink_dpipe_fields_ethernet,
  18. .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ethernet),
  19. .global = true,
  20. };
  21. EXPORT_SYMBOL_GPL(devlink_dpipe_header_ethernet);
  22. static struct devlink_dpipe_field devlink_dpipe_fields_ipv4[] = {
  23. {
  24. .name = "destination ip",
  25. .id = DEVLINK_DPIPE_FIELD_IPV4_DST_IP,
  26. .bitwidth = 32,
  27. },
  28. };
  29. struct devlink_dpipe_header devlink_dpipe_header_ipv4 = {
  30. .name = "ipv4",
  31. .id = DEVLINK_DPIPE_HEADER_IPV4,
  32. .fields = devlink_dpipe_fields_ipv4,
  33. .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv4),
  34. .global = true,
  35. };
  36. EXPORT_SYMBOL_GPL(devlink_dpipe_header_ipv4);
  37. static struct devlink_dpipe_field devlink_dpipe_fields_ipv6[] = {
  38. {
  39. .name = "destination ip",
  40. .id = DEVLINK_DPIPE_FIELD_IPV6_DST_IP,
  41. .bitwidth = 128,
  42. },
  43. };
  44. struct devlink_dpipe_header devlink_dpipe_header_ipv6 = {
  45. .name = "ipv6",
  46. .id = DEVLINK_DPIPE_HEADER_IPV6,
  47. .fields = devlink_dpipe_fields_ipv6,
  48. .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv6),
  49. .global = true,
  50. };
  51. EXPORT_SYMBOL_GPL(devlink_dpipe_header_ipv6);
  52. int devlink_dpipe_match_put(struct sk_buff *skb,
  53. struct devlink_dpipe_match *match)
  54. {
  55. struct devlink_dpipe_header *header = match->header;
  56. struct devlink_dpipe_field *field = &header->fields[match->field_id];
  57. struct nlattr *match_attr;
  58. match_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_MATCH);
  59. if (!match_attr)
  60. return -EMSGSIZE;
  61. if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_MATCH_TYPE, match->type) ||
  62. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, match->header_index) ||
  63. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
  64. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
  65. nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
  66. goto nla_put_failure;
  67. nla_nest_end(skb, match_attr);
  68. return 0;
  69. nla_put_failure:
  70. nla_nest_cancel(skb, match_attr);
  71. return -EMSGSIZE;
  72. }
  73. EXPORT_SYMBOL_GPL(devlink_dpipe_match_put);
  74. static int devlink_dpipe_matches_put(struct devlink_dpipe_table *table,
  75. struct sk_buff *skb)
  76. {
  77. struct nlattr *matches_attr;
  78. matches_attr = nla_nest_start_noflag(skb,
  79. DEVLINK_ATTR_DPIPE_TABLE_MATCHES);
  80. if (!matches_attr)
  81. return -EMSGSIZE;
  82. if (table->table_ops->matches_dump(table->priv, skb))
  83. goto nla_put_failure;
  84. nla_nest_end(skb, matches_attr);
  85. return 0;
  86. nla_put_failure:
  87. nla_nest_cancel(skb, matches_attr);
  88. return -EMSGSIZE;
  89. }
  90. int devlink_dpipe_action_put(struct sk_buff *skb,
  91. struct devlink_dpipe_action *action)
  92. {
  93. struct devlink_dpipe_header *header = action->header;
  94. struct devlink_dpipe_field *field = &header->fields[action->field_id];
  95. struct nlattr *action_attr;
  96. action_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_ACTION);
  97. if (!action_attr)
  98. return -EMSGSIZE;
  99. if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_ACTION_TYPE, action->type) ||
  100. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, action->header_index) ||
  101. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
  102. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
  103. nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
  104. goto nla_put_failure;
  105. nla_nest_end(skb, action_attr);
  106. return 0;
  107. nla_put_failure:
  108. nla_nest_cancel(skb, action_attr);
  109. return -EMSGSIZE;
  110. }
  111. EXPORT_SYMBOL_GPL(devlink_dpipe_action_put);
  112. static int devlink_dpipe_actions_put(struct devlink_dpipe_table *table,
  113. struct sk_buff *skb)
  114. {
  115. struct nlattr *actions_attr;
  116. actions_attr = nla_nest_start_noflag(skb,
  117. DEVLINK_ATTR_DPIPE_TABLE_ACTIONS);
  118. if (!actions_attr)
  119. return -EMSGSIZE;
  120. if (table->table_ops->actions_dump(table->priv, skb))
  121. goto nla_put_failure;
  122. nla_nest_end(skb, actions_attr);
  123. return 0;
  124. nla_put_failure:
  125. nla_nest_cancel(skb, actions_attr);
  126. return -EMSGSIZE;
  127. }
  128. static int devlink_dpipe_table_put(struct sk_buff *skb,
  129. struct devlink_dpipe_table *table)
  130. {
  131. struct nlattr *table_attr;
  132. u64 table_size;
  133. table_size = table->table_ops->size_get(table->priv);
  134. table_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_TABLE);
  135. if (!table_attr)
  136. return -EMSGSIZE;
  137. if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_TABLE_NAME, table->name) ||
  138. nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_SIZE, table_size,
  139. DEVLINK_ATTR_PAD))
  140. goto nla_put_failure;
  141. if (nla_put_u8(skb, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED,
  142. table->counters_enabled))
  143. goto nla_put_failure;
  144. if (table->resource_valid) {
  145. if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID,
  146. table->resource_id, DEVLINK_ATTR_PAD) ||
  147. nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS,
  148. table->resource_units, DEVLINK_ATTR_PAD))
  149. goto nla_put_failure;
  150. }
  151. if (devlink_dpipe_matches_put(table, skb))
  152. goto nla_put_failure;
  153. if (devlink_dpipe_actions_put(table, skb))
  154. goto nla_put_failure;
  155. nla_nest_end(skb, table_attr);
  156. return 0;
  157. nla_put_failure:
  158. nla_nest_cancel(skb, table_attr);
  159. return -EMSGSIZE;
  160. }
  161. static int devlink_dpipe_send_and_alloc_skb(struct sk_buff **pskb,
  162. struct genl_info *info)
  163. {
  164. int err;
  165. if (*pskb) {
  166. err = genlmsg_reply(*pskb, info);
  167. if (err)
  168. return err;
  169. }
  170. *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
  171. if (!*pskb)
  172. return -ENOMEM;
  173. return 0;
  174. }
  175. static int devlink_dpipe_tables_fill(struct genl_info *info,
  176. enum devlink_command cmd, int flags,
  177. struct list_head *dpipe_tables,
  178. const char *table_name)
  179. {
  180. struct devlink *devlink = info->user_ptr[0];
  181. struct devlink_dpipe_table *table;
  182. struct nlattr *tables_attr;
  183. struct sk_buff *skb = NULL;
  184. struct nlmsghdr *nlh;
  185. bool incomplete;
  186. void *hdr;
  187. int i;
  188. int err;
  189. table = list_first_entry(dpipe_tables,
  190. struct devlink_dpipe_table, list);
  191. start_again:
  192. err = devlink_dpipe_send_and_alloc_skb(&skb, info);
  193. if (err)
  194. return err;
  195. hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
  196. &devlink_nl_family, NLM_F_MULTI, cmd);
  197. if (!hdr) {
  198. nlmsg_free(skb);
  199. return -EMSGSIZE;
  200. }
  201. if (devlink_nl_put_handle(skb, devlink))
  202. goto nla_put_failure;
  203. tables_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_TABLES);
  204. if (!tables_attr)
  205. goto nla_put_failure;
  206. i = 0;
  207. incomplete = false;
  208. list_for_each_entry_from(table, dpipe_tables, list) {
  209. if (!table_name) {
  210. err = devlink_dpipe_table_put(skb, table);
  211. if (err) {
  212. if (!i)
  213. goto err_table_put;
  214. incomplete = true;
  215. break;
  216. }
  217. } else {
  218. if (!strcmp(table->name, table_name)) {
  219. err = devlink_dpipe_table_put(skb, table);
  220. if (err)
  221. break;
  222. }
  223. }
  224. i++;
  225. }
  226. nla_nest_end(skb, tables_attr);
  227. genlmsg_end(skb, hdr);
  228. if (incomplete)
  229. goto start_again;
  230. send_done:
  231. nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
  232. NLMSG_DONE, 0, flags | NLM_F_MULTI);
  233. if (!nlh) {
  234. err = devlink_dpipe_send_and_alloc_skb(&skb, info);
  235. if (err)
  236. return err;
  237. goto send_done;
  238. }
  239. return genlmsg_reply(skb, info);
  240. nla_put_failure:
  241. err = -EMSGSIZE;
  242. err_table_put:
  243. nlmsg_free(skb);
  244. return err;
  245. }
  246. int devlink_nl_dpipe_table_get_doit(struct sk_buff *skb, struct genl_info *info)
  247. {
  248. struct devlink *devlink = info->user_ptr[0];
  249. const char *table_name = NULL;
  250. if (info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
  251. table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
  252. return devlink_dpipe_tables_fill(info, DEVLINK_CMD_DPIPE_TABLE_GET, 0,
  253. &devlink->dpipe_table_list,
  254. table_name);
  255. }
  256. static int devlink_dpipe_value_put(struct sk_buff *skb,
  257. struct devlink_dpipe_value *value)
  258. {
  259. if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE,
  260. value->value_size, value->value))
  261. return -EMSGSIZE;
  262. if (value->mask)
  263. if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE_MASK,
  264. value->value_size, value->mask))
  265. return -EMSGSIZE;
  266. if (value->mapping_valid)
  267. if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_VALUE_MAPPING,
  268. value->mapping_value))
  269. return -EMSGSIZE;
  270. return 0;
  271. }
  272. static int devlink_dpipe_action_value_put(struct sk_buff *skb,
  273. struct devlink_dpipe_value *value)
  274. {
  275. if (!value->action)
  276. return -EINVAL;
  277. if (devlink_dpipe_action_put(skb, value->action))
  278. return -EMSGSIZE;
  279. if (devlink_dpipe_value_put(skb, value))
  280. return -EMSGSIZE;
  281. return 0;
  282. }
  283. static int devlink_dpipe_action_values_put(struct sk_buff *skb,
  284. struct devlink_dpipe_value *values,
  285. unsigned int values_count)
  286. {
  287. struct nlattr *action_attr;
  288. int i;
  289. int err;
  290. for (i = 0; i < values_count; i++) {
  291. action_attr = nla_nest_start_noflag(skb,
  292. DEVLINK_ATTR_DPIPE_ACTION_VALUE);
  293. if (!action_attr)
  294. return -EMSGSIZE;
  295. err = devlink_dpipe_action_value_put(skb, &values[i]);
  296. if (err)
  297. goto err_action_value_put;
  298. nla_nest_end(skb, action_attr);
  299. }
  300. return 0;
  301. err_action_value_put:
  302. nla_nest_cancel(skb, action_attr);
  303. return err;
  304. }
  305. static int devlink_dpipe_match_value_put(struct sk_buff *skb,
  306. struct devlink_dpipe_value *value)
  307. {
  308. if (!value->match)
  309. return -EINVAL;
  310. if (devlink_dpipe_match_put(skb, value->match))
  311. return -EMSGSIZE;
  312. if (devlink_dpipe_value_put(skb, value))
  313. return -EMSGSIZE;
  314. return 0;
  315. }
  316. static int devlink_dpipe_match_values_put(struct sk_buff *skb,
  317. struct devlink_dpipe_value *values,
  318. unsigned int values_count)
  319. {
  320. struct nlattr *match_attr;
  321. int i;
  322. int err;
  323. for (i = 0; i < values_count; i++) {
  324. match_attr = nla_nest_start_noflag(skb,
  325. DEVLINK_ATTR_DPIPE_MATCH_VALUE);
  326. if (!match_attr)
  327. return -EMSGSIZE;
  328. err = devlink_dpipe_match_value_put(skb, &values[i]);
  329. if (err)
  330. goto err_match_value_put;
  331. nla_nest_end(skb, match_attr);
  332. }
  333. return 0;
  334. err_match_value_put:
  335. nla_nest_cancel(skb, match_attr);
  336. return err;
  337. }
  338. static int devlink_dpipe_entry_put(struct sk_buff *skb,
  339. struct devlink_dpipe_entry *entry)
  340. {
  341. struct nlattr *entry_attr, *matches_attr, *actions_attr;
  342. int err;
  343. entry_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_ENTRY);
  344. if (!entry_attr)
  345. return -EMSGSIZE;
  346. if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_INDEX, entry->index,
  347. DEVLINK_ATTR_PAD))
  348. goto nla_put_failure;
  349. if (entry->counter_valid)
  350. if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_COUNTER,
  351. entry->counter, DEVLINK_ATTR_PAD))
  352. goto nla_put_failure;
  353. matches_attr = nla_nest_start_noflag(skb,
  354. DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES);
  355. if (!matches_attr)
  356. goto nla_put_failure;
  357. err = devlink_dpipe_match_values_put(skb, entry->match_values,
  358. entry->match_values_count);
  359. if (err) {
  360. nla_nest_cancel(skb, matches_attr);
  361. goto err_match_values_put;
  362. }
  363. nla_nest_end(skb, matches_attr);
  364. actions_attr = nla_nest_start_noflag(skb,
  365. DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES);
  366. if (!actions_attr)
  367. goto nla_put_failure;
  368. err = devlink_dpipe_action_values_put(skb, entry->action_values,
  369. entry->action_values_count);
  370. if (err) {
  371. nla_nest_cancel(skb, actions_attr);
  372. goto err_action_values_put;
  373. }
  374. nla_nest_end(skb, actions_attr);
  375. nla_nest_end(skb, entry_attr);
  376. return 0;
  377. nla_put_failure:
  378. err = -EMSGSIZE;
  379. err_match_values_put:
  380. err_action_values_put:
  381. nla_nest_cancel(skb, entry_attr);
  382. return err;
  383. }
  384. static struct devlink_dpipe_table *
  385. devlink_dpipe_table_find(struct list_head *dpipe_tables,
  386. const char *table_name, struct devlink *devlink)
  387. {
  388. struct devlink_dpipe_table *table;
  389. list_for_each_entry_rcu(table, dpipe_tables, list,
  390. lockdep_is_held(&devlink->lock)) {
  391. if (!strcmp(table->name, table_name))
  392. return table;
  393. }
  394. return NULL;
  395. }
  396. int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx)
  397. {
  398. struct devlink *devlink;
  399. int err;
  400. err = devlink_dpipe_send_and_alloc_skb(&dump_ctx->skb,
  401. dump_ctx->info);
  402. if (err)
  403. return err;
  404. dump_ctx->hdr = genlmsg_put(dump_ctx->skb,
  405. dump_ctx->info->snd_portid,
  406. dump_ctx->info->snd_seq,
  407. &devlink_nl_family, NLM_F_MULTI,
  408. dump_ctx->cmd);
  409. if (!dump_ctx->hdr)
  410. goto nla_put_failure;
  411. devlink = dump_ctx->info->user_ptr[0];
  412. if (devlink_nl_put_handle(dump_ctx->skb, devlink))
  413. goto nla_put_failure;
  414. dump_ctx->nest = nla_nest_start_noflag(dump_ctx->skb,
  415. DEVLINK_ATTR_DPIPE_ENTRIES);
  416. if (!dump_ctx->nest)
  417. goto nla_put_failure;
  418. return 0;
  419. nla_put_failure:
  420. nlmsg_free(dump_ctx->skb);
  421. return -EMSGSIZE;
  422. }
  423. EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_prepare);
  424. int devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx,
  425. struct devlink_dpipe_entry *entry)
  426. {
  427. return devlink_dpipe_entry_put(dump_ctx->skb, entry);
  428. }
  429. EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_append);
  430. int devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx)
  431. {
  432. nla_nest_end(dump_ctx->skb, dump_ctx->nest);
  433. genlmsg_end(dump_ctx->skb, dump_ctx->hdr);
  434. return 0;
  435. }
  436. EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_close);
  437. void devlink_dpipe_entry_clear(struct devlink_dpipe_entry *entry)
  438. {
  439. unsigned int value_count, value_index;
  440. struct devlink_dpipe_value *value;
  441. value = entry->action_values;
  442. value_count = entry->action_values_count;
  443. for (value_index = 0; value_index < value_count; value_index++) {
  444. kfree(value[value_index].value);
  445. kfree(value[value_index].mask);
  446. }
  447. value = entry->match_values;
  448. value_count = entry->match_values_count;
  449. for (value_index = 0; value_index < value_count; value_index++) {
  450. kfree(value[value_index].value);
  451. kfree(value[value_index].mask);
  452. }
  453. }
  454. EXPORT_SYMBOL_GPL(devlink_dpipe_entry_clear);
  455. static int devlink_dpipe_entries_fill(struct genl_info *info,
  456. enum devlink_command cmd, int flags,
  457. struct devlink_dpipe_table *table)
  458. {
  459. struct devlink_dpipe_dump_ctx dump_ctx;
  460. struct nlmsghdr *nlh;
  461. int err;
  462. dump_ctx.skb = NULL;
  463. dump_ctx.cmd = cmd;
  464. dump_ctx.info = info;
  465. err = table->table_ops->entries_dump(table->priv,
  466. table->counters_enabled,
  467. &dump_ctx);
  468. if (err)
  469. return err;
  470. send_done:
  471. nlh = nlmsg_put(dump_ctx.skb, info->snd_portid, info->snd_seq,
  472. NLMSG_DONE, 0, flags | NLM_F_MULTI);
  473. if (!nlh) {
  474. err = devlink_dpipe_send_and_alloc_skb(&dump_ctx.skb, info);
  475. if (err)
  476. return err;
  477. goto send_done;
  478. }
  479. return genlmsg_reply(dump_ctx.skb, info);
  480. }
  481. int devlink_nl_dpipe_entries_get_doit(struct sk_buff *skb,
  482. struct genl_info *info)
  483. {
  484. struct devlink *devlink = info->user_ptr[0];
  485. struct devlink_dpipe_table *table;
  486. const char *table_name;
  487. if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_DPIPE_TABLE_NAME))
  488. return -EINVAL;
  489. table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
  490. table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
  491. table_name, devlink);
  492. if (!table)
  493. return -EINVAL;
  494. if (!table->table_ops->entries_dump)
  495. return -EINVAL;
  496. return devlink_dpipe_entries_fill(info, DEVLINK_CMD_DPIPE_ENTRIES_GET,
  497. 0, table);
  498. }
  499. static int devlink_dpipe_fields_put(struct sk_buff *skb,
  500. const struct devlink_dpipe_header *header)
  501. {
  502. struct devlink_dpipe_field *field;
  503. struct nlattr *field_attr;
  504. int i;
  505. for (i = 0; i < header->fields_count; i++) {
  506. field = &header->fields[i];
  507. field_attr = nla_nest_start_noflag(skb,
  508. DEVLINK_ATTR_DPIPE_FIELD);
  509. if (!field_attr)
  510. return -EMSGSIZE;
  511. if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_FIELD_NAME, field->name) ||
  512. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
  513. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH, field->bitwidth) ||
  514. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE, field->mapping_type))
  515. goto nla_put_failure;
  516. nla_nest_end(skb, field_attr);
  517. }
  518. return 0;
  519. nla_put_failure:
  520. nla_nest_cancel(skb, field_attr);
  521. return -EMSGSIZE;
  522. }
  523. static int devlink_dpipe_header_put(struct sk_buff *skb,
  524. struct devlink_dpipe_header *header)
  525. {
  526. struct nlattr *fields_attr, *header_attr;
  527. int err;
  528. header_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_HEADER);
  529. if (!header_attr)
  530. return -EMSGSIZE;
  531. if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_HEADER_NAME, header->name) ||
  532. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
  533. nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
  534. goto nla_put_failure;
  535. fields_attr = nla_nest_start_noflag(skb,
  536. DEVLINK_ATTR_DPIPE_HEADER_FIELDS);
  537. if (!fields_attr)
  538. goto nla_put_failure;
  539. err = devlink_dpipe_fields_put(skb, header);
  540. if (err) {
  541. nla_nest_cancel(skb, fields_attr);
  542. goto nla_put_failure;
  543. }
  544. nla_nest_end(skb, fields_attr);
  545. nla_nest_end(skb, header_attr);
  546. return 0;
  547. nla_put_failure:
  548. err = -EMSGSIZE;
  549. nla_nest_cancel(skb, header_attr);
  550. return err;
  551. }
  552. static int devlink_dpipe_headers_fill(struct genl_info *info,
  553. enum devlink_command cmd, int flags,
  554. struct devlink_dpipe_headers *
  555. dpipe_headers)
  556. {
  557. struct devlink *devlink = info->user_ptr[0];
  558. struct nlattr *headers_attr;
  559. struct sk_buff *skb = NULL;
  560. struct nlmsghdr *nlh;
  561. void *hdr;
  562. int i, j;
  563. int err;
  564. i = 0;
  565. start_again:
  566. err = devlink_dpipe_send_and_alloc_skb(&skb, info);
  567. if (err)
  568. return err;
  569. hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
  570. &devlink_nl_family, NLM_F_MULTI, cmd);
  571. if (!hdr) {
  572. nlmsg_free(skb);
  573. return -EMSGSIZE;
  574. }
  575. if (devlink_nl_put_handle(skb, devlink))
  576. goto nla_put_failure;
  577. headers_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_HEADERS);
  578. if (!headers_attr)
  579. goto nla_put_failure;
  580. j = 0;
  581. for (; i < dpipe_headers->headers_count; i++) {
  582. err = devlink_dpipe_header_put(skb, dpipe_headers->headers[i]);
  583. if (err) {
  584. if (!j)
  585. goto err_table_put;
  586. break;
  587. }
  588. j++;
  589. }
  590. nla_nest_end(skb, headers_attr);
  591. genlmsg_end(skb, hdr);
  592. if (i != dpipe_headers->headers_count)
  593. goto start_again;
  594. send_done:
  595. nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
  596. NLMSG_DONE, 0, flags | NLM_F_MULTI);
  597. if (!nlh) {
  598. err = devlink_dpipe_send_and_alloc_skb(&skb, info);
  599. if (err)
  600. return err;
  601. goto send_done;
  602. }
  603. return genlmsg_reply(skb, info);
  604. nla_put_failure:
  605. err = -EMSGSIZE;
  606. err_table_put:
  607. nlmsg_free(skb);
  608. return err;
  609. }
  610. int devlink_nl_dpipe_headers_get_doit(struct sk_buff *skb,
  611. struct genl_info *info)
  612. {
  613. struct devlink *devlink = info->user_ptr[0];
  614. if (!devlink->dpipe_headers)
  615. return -EOPNOTSUPP;
  616. return devlink_dpipe_headers_fill(info, DEVLINK_CMD_DPIPE_HEADERS_GET,
  617. 0, devlink->dpipe_headers);
  618. }
  619. static int devlink_dpipe_table_counters_set(struct devlink *devlink,
  620. const char *table_name,
  621. bool enable)
  622. {
  623. struct devlink_dpipe_table *table;
  624. table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
  625. table_name, devlink);
  626. if (!table)
  627. return -EINVAL;
  628. if (table->counter_control_extern)
  629. return -EOPNOTSUPP;
  630. if (!(table->counters_enabled ^ enable))
  631. return 0;
  632. table->counters_enabled = enable;
  633. if (table->table_ops->counters_set_update)
  634. table->table_ops->counters_set_update(table->priv, enable);
  635. return 0;
  636. }
  637. int devlink_nl_dpipe_table_counters_set_doit(struct sk_buff *skb,
  638. struct genl_info *info)
  639. {
  640. struct devlink *devlink = info->user_ptr[0];
  641. const char *table_name;
  642. bool counters_enable;
  643. if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_DPIPE_TABLE_NAME) ||
  644. GENL_REQ_ATTR_CHECK(info,
  645. DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED))
  646. return -EINVAL;
  647. table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
  648. counters_enable = !!nla_get_u8(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED]);
  649. return devlink_dpipe_table_counters_set(devlink, table_name,
  650. counters_enable);
  651. }
  652. /**
  653. * devl_dpipe_headers_register - register dpipe headers
  654. *
  655. * @devlink: devlink
  656. * @dpipe_headers: dpipe header array
  657. *
  658. * Register the headers supported by hardware.
  659. */
  660. void devl_dpipe_headers_register(struct devlink *devlink,
  661. struct devlink_dpipe_headers *dpipe_headers)
  662. {
  663. lockdep_assert_held(&devlink->lock);
  664. devlink->dpipe_headers = dpipe_headers;
  665. }
  666. EXPORT_SYMBOL_GPL(devl_dpipe_headers_register);
  667. /**
  668. * devl_dpipe_headers_unregister - unregister dpipe headers
  669. *
  670. * @devlink: devlink
  671. *
  672. * Unregister the headers supported by hardware.
  673. */
  674. void devl_dpipe_headers_unregister(struct devlink *devlink)
  675. {
  676. lockdep_assert_held(&devlink->lock);
  677. devlink->dpipe_headers = NULL;
  678. }
  679. EXPORT_SYMBOL_GPL(devl_dpipe_headers_unregister);
  680. /**
  681. * devlink_dpipe_table_counter_enabled - check if counter allocation
  682. * required
  683. * @devlink: devlink
  684. * @table_name: tables name
  685. *
  686. * Used by driver to check if counter allocation is required.
  687. * After counter allocation is turned on the table entries
  688. * are updated to include counter statistics.
  689. *
  690. * After that point on the driver must respect the counter
  691. * state so that each entry added to the table is added
  692. * with a counter.
  693. */
  694. bool devlink_dpipe_table_counter_enabled(struct devlink *devlink,
  695. const char *table_name)
  696. {
  697. struct devlink_dpipe_table *table;
  698. bool enabled;
  699. rcu_read_lock();
  700. table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
  701. table_name, devlink);
  702. enabled = false;
  703. if (table)
  704. enabled = table->counters_enabled;
  705. rcu_read_unlock();
  706. return enabled;
  707. }
  708. EXPORT_SYMBOL_GPL(devlink_dpipe_table_counter_enabled);
  709. /**
  710. * devl_dpipe_table_register - register dpipe table
  711. *
  712. * @devlink: devlink
  713. * @table_name: table name
  714. * @table_ops: table ops
  715. * @priv: priv
  716. * @counter_control_extern: external control for counters
  717. */
  718. int devl_dpipe_table_register(struct devlink *devlink,
  719. const char *table_name,
  720. const struct devlink_dpipe_table_ops *table_ops,
  721. void *priv, bool counter_control_extern)
  722. {
  723. struct devlink_dpipe_table *table;
  724. lockdep_assert_held(&devlink->lock);
  725. if (WARN_ON(!table_ops->size_get))
  726. return -EINVAL;
  727. if (devlink_dpipe_table_find(&devlink->dpipe_table_list, table_name,
  728. devlink))
  729. return -EEXIST;
  730. table = kzalloc(sizeof(*table), GFP_KERNEL);
  731. if (!table)
  732. return -ENOMEM;
  733. table->name = table_name;
  734. table->table_ops = table_ops;
  735. table->priv = priv;
  736. table->counter_control_extern = counter_control_extern;
  737. list_add_tail_rcu(&table->list, &devlink->dpipe_table_list);
  738. return 0;
  739. }
  740. EXPORT_SYMBOL_GPL(devl_dpipe_table_register);
  741. /**
  742. * devl_dpipe_table_unregister - unregister dpipe table
  743. *
  744. * @devlink: devlink
  745. * @table_name: table name
  746. */
  747. void devl_dpipe_table_unregister(struct devlink *devlink,
  748. const char *table_name)
  749. {
  750. struct devlink_dpipe_table *table;
  751. lockdep_assert_held(&devlink->lock);
  752. table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
  753. table_name, devlink);
  754. if (!table)
  755. return;
  756. list_del_rcu(&table->list);
  757. kfree_rcu(table, rcu);
  758. }
  759. EXPORT_SYMBOL_GPL(devl_dpipe_table_unregister);
  760. /**
  761. * devl_dpipe_table_resource_set - set the resource id
  762. *
  763. * @devlink: devlink
  764. * @table_name: table name
  765. * @resource_id: resource id
  766. * @resource_units: number of resource's units consumed per table's entry
  767. */
  768. int devl_dpipe_table_resource_set(struct devlink *devlink,
  769. const char *table_name, u64 resource_id,
  770. u64 resource_units)
  771. {
  772. struct devlink_dpipe_table *table;
  773. table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
  774. table_name, devlink);
  775. if (!table)
  776. return -EINVAL;
  777. table->resource_id = resource_id;
  778. table->resource_units = resource_units;
  779. table->resource_valid = true;
  780. return 0;
  781. }
  782. EXPORT_SYMBOL_GPL(devl_dpipe_table_resource_set);