dpipe.c 23 KB

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