drm/tests: hdmi: Fix memory leaks in drm_display_mode_from_cea_vic()
[drm/drm-misc.git] / net / devlink / dpipe.c
blob55009b377447fe7734a1533828185e8e2e162bd8
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 */
7 #include "devl_internal.h"
9 static struct devlink_dpipe_field devlink_dpipe_fields_ethernet[] = {
11 .name = "destination mac",
12 .id = DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC,
13 .bitwidth = 48,
17 struct devlink_dpipe_header devlink_dpipe_header_ethernet = {
18 .name = "ethernet",
19 .id = DEVLINK_DPIPE_HEADER_ETHERNET,
20 .fields = devlink_dpipe_fields_ethernet,
21 .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ethernet),
22 .global = true,
24 EXPORT_SYMBOL_GPL(devlink_dpipe_header_ethernet);
26 static struct devlink_dpipe_field devlink_dpipe_fields_ipv4[] = {
28 .name = "destination ip",
29 .id = DEVLINK_DPIPE_FIELD_IPV4_DST_IP,
30 .bitwidth = 32,
34 struct devlink_dpipe_header devlink_dpipe_header_ipv4 = {
35 .name = "ipv4",
36 .id = DEVLINK_DPIPE_HEADER_IPV4,
37 .fields = devlink_dpipe_fields_ipv4,
38 .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv4),
39 .global = true,
41 EXPORT_SYMBOL_GPL(devlink_dpipe_header_ipv4);
43 static struct devlink_dpipe_field devlink_dpipe_fields_ipv6[] = {
45 .name = "destination ip",
46 .id = DEVLINK_DPIPE_FIELD_IPV6_DST_IP,
47 .bitwidth = 128,
51 struct devlink_dpipe_header devlink_dpipe_header_ipv6 = {
52 .name = "ipv6",
53 .id = DEVLINK_DPIPE_HEADER_IPV6,
54 .fields = devlink_dpipe_fields_ipv6,
55 .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv6),
56 .global = true,
58 EXPORT_SYMBOL_GPL(devlink_dpipe_header_ipv6);
60 int devlink_dpipe_match_put(struct sk_buff *skb,
61 struct devlink_dpipe_match *match)
63 struct devlink_dpipe_header *header = match->header;
64 struct devlink_dpipe_field *field = &header->fields[match->field_id];
65 struct nlattr *match_attr;
67 match_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_MATCH);
68 if (!match_attr)
69 return -EMSGSIZE;
71 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_MATCH_TYPE, match->type) ||
72 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, match->header_index) ||
73 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
74 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
75 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
76 goto nla_put_failure;
78 nla_nest_end(skb, match_attr);
79 return 0;
81 nla_put_failure:
82 nla_nest_cancel(skb, match_attr);
83 return -EMSGSIZE;
85 EXPORT_SYMBOL_GPL(devlink_dpipe_match_put);
87 static int devlink_dpipe_matches_put(struct devlink_dpipe_table *table,
88 struct sk_buff *skb)
90 struct nlattr *matches_attr;
92 matches_attr = nla_nest_start_noflag(skb,
93 DEVLINK_ATTR_DPIPE_TABLE_MATCHES);
94 if (!matches_attr)
95 return -EMSGSIZE;
97 if (table->table_ops->matches_dump(table->priv, skb))
98 goto nla_put_failure;
100 nla_nest_end(skb, matches_attr);
101 return 0;
103 nla_put_failure:
104 nla_nest_cancel(skb, matches_attr);
105 return -EMSGSIZE;
108 int devlink_dpipe_action_put(struct sk_buff *skb,
109 struct devlink_dpipe_action *action)
111 struct devlink_dpipe_header *header = action->header;
112 struct devlink_dpipe_field *field = &header->fields[action->field_id];
113 struct nlattr *action_attr;
115 action_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_ACTION);
116 if (!action_attr)
117 return -EMSGSIZE;
119 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_ACTION_TYPE, action->type) ||
120 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, action->header_index) ||
121 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
122 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
123 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
124 goto nla_put_failure;
126 nla_nest_end(skb, action_attr);
127 return 0;
129 nla_put_failure:
130 nla_nest_cancel(skb, action_attr);
131 return -EMSGSIZE;
133 EXPORT_SYMBOL_GPL(devlink_dpipe_action_put);
135 static int devlink_dpipe_actions_put(struct devlink_dpipe_table *table,
136 struct sk_buff *skb)
138 struct nlattr *actions_attr;
140 actions_attr = nla_nest_start_noflag(skb,
141 DEVLINK_ATTR_DPIPE_TABLE_ACTIONS);
142 if (!actions_attr)
143 return -EMSGSIZE;
145 if (table->table_ops->actions_dump(table->priv, skb))
146 goto nla_put_failure;
148 nla_nest_end(skb, actions_attr);
149 return 0;
151 nla_put_failure:
152 nla_nest_cancel(skb, actions_attr);
153 return -EMSGSIZE;
156 static int devlink_dpipe_table_put(struct sk_buff *skb,
157 struct devlink_dpipe_table *table)
159 struct nlattr *table_attr;
160 u64 table_size;
162 table_size = table->table_ops->size_get(table->priv);
163 table_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_TABLE);
164 if (!table_attr)
165 return -EMSGSIZE;
167 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_TABLE_NAME, table->name) ||
168 nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_SIZE, table_size,
169 DEVLINK_ATTR_PAD))
170 goto nla_put_failure;
171 if (nla_put_u8(skb, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED,
172 table->counters_enabled))
173 goto nla_put_failure;
175 if (table->resource_valid) {
176 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID,
177 table->resource_id, DEVLINK_ATTR_PAD) ||
178 nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS,
179 table->resource_units, DEVLINK_ATTR_PAD))
180 goto nla_put_failure;
182 if (devlink_dpipe_matches_put(table, skb))
183 goto nla_put_failure;
185 if (devlink_dpipe_actions_put(table, skb))
186 goto nla_put_failure;
188 nla_nest_end(skb, table_attr);
189 return 0;
191 nla_put_failure:
192 nla_nest_cancel(skb, table_attr);
193 return -EMSGSIZE;
196 static int devlink_dpipe_send_and_alloc_skb(struct sk_buff **pskb,
197 struct genl_info *info)
199 int err;
201 if (*pskb) {
202 err = genlmsg_reply(*pskb, info);
203 if (err)
204 return err;
206 *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
207 if (!*pskb)
208 return -ENOMEM;
209 return 0;
212 static int devlink_dpipe_tables_fill(struct genl_info *info,
213 enum devlink_command cmd, int flags,
214 struct list_head *dpipe_tables,
215 const char *table_name)
217 struct devlink *devlink = info->user_ptr[0];
218 struct devlink_dpipe_table *table;
219 struct nlattr *tables_attr;
220 struct sk_buff *skb = NULL;
221 struct nlmsghdr *nlh;
222 bool incomplete;
223 void *hdr;
224 int i;
225 int err;
227 table = list_first_entry(dpipe_tables,
228 struct devlink_dpipe_table, list);
229 start_again:
230 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
231 if (err)
232 return err;
234 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
235 &devlink_nl_family, NLM_F_MULTI, cmd);
236 if (!hdr) {
237 nlmsg_free(skb);
238 return -EMSGSIZE;
241 if (devlink_nl_put_handle(skb, devlink))
242 goto nla_put_failure;
243 tables_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_TABLES);
244 if (!tables_attr)
245 goto nla_put_failure;
247 i = 0;
248 incomplete = false;
249 list_for_each_entry_from(table, dpipe_tables, list) {
250 if (!table_name) {
251 err = devlink_dpipe_table_put(skb, table);
252 if (err) {
253 if (!i)
254 goto err_table_put;
255 incomplete = true;
256 break;
258 } else {
259 if (!strcmp(table->name, table_name)) {
260 err = devlink_dpipe_table_put(skb, table);
261 if (err)
262 break;
265 i++;
268 nla_nest_end(skb, tables_attr);
269 genlmsg_end(skb, hdr);
270 if (incomplete)
271 goto start_again;
273 send_done:
274 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
275 NLMSG_DONE, 0, flags | NLM_F_MULTI);
276 if (!nlh) {
277 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
278 if (err)
279 return err;
280 goto send_done;
283 return genlmsg_reply(skb, info);
285 nla_put_failure:
286 err = -EMSGSIZE;
287 err_table_put:
288 nlmsg_free(skb);
289 return err;
292 int devlink_nl_dpipe_table_get_doit(struct sk_buff *skb, struct genl_info *info)
294 struct devlink *devlink = info->user_ptr[0];
295 const char *table_name = NULL;
297 if (info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
298 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
300 return devlink_dpipe_tables_fill(info, DEVLINK_CMD_DPIPE_TABLE_GET, 0,
301 &devlink->dpipe_table_list,
302 table_name);
305 static int devlink_dpipe_value_put(struct sk_buff *skb,
306 struct devlink_dpipe_value *value)
308 if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE,
309 value->value_size, value->value))
310 return -EMSGSIZE;
311 if (value->mask)
312 if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE_MASK,
313 value->value_size, value->mask))
314 return -EMSGSIZE;
315 if (value->mapping_valid)
316 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_VALUE_MAPPING,
317 value->mapping_value))
318 return -EMSGSIZE;
319 return 0;
322 static int devlink_dpipe_action_value_put(struct sk_buff *skb,
323 struct devlink_dpipe_value *value)
325 if (!value->action)
326 return -EINVAL;
327 if (devlink_dpipe_action_put(skb, value->action))
328 return -EMSGSIZE;
329 if (devlink_dpipe_value_put(skb, value))
330 return -EMSGSIZE;
331 return 0;
334 static int devlink_dpipe_action_values_put(struct sk_buff *skb,
335 struct devlink_dpipe_value *values,
336 unsigned int values_count)
338 struct nlattr *action_attr;
339 int i;
340 int err;
342 for (i = 0; i < values_count; i++) {
343 action_attr = nla_nest_start_noflag(skb,
344 DEVLINK_ATTR_DPIPE_ACTION_VALUE);
345 if (!action_attr)
346 return -EMSGSIZE;
347 err = devlink_dpipe_action_value_put(skb, &values[i]);
348 if (err)
349 goto err_action_value_put;
350 nla_nest_end(skb, action_attr);
352 return 0;
354 err_action_value_put:
355 nla_nest_cancel(skb, action_attr);
356 return err;
359 static int devlink_dpipe_match_value_put(struct sk_buff *skb,
360 struct devlink_dpipe_value *value)
362 if (!value->match)
363 return -EINVAL;
364 if (devlink_dpipe_match_put(skb, value->match))
365 return -EMSGSIZE;
366 if (devlink_dpipe_value_put(skb, value))
367 return -EMSGSIZE;
368 return 0;
371 static int devlink_dpipe_match_values_put(struct sk_buff *skb,
372 struct devlink_dpipe_value *values,
373 unsigned int values_count)
375 struct nlattr *match_attr;
376 int i;
377 int err;
379 for (i = 0; i < values_count; i++) {
380 match_attr = nla_nest_start_noflag(skb,
381 DEVLINK_ATTR_DPIPE_MATCH_VALUE);
382 if (!match_attr)
383 return -EMSGSIZE;
384 err = devlink_dpipe_match_value_put(skb, &values[i]);
385 if (err)
386 goto err_match_value_put;
387 nla_nest_end(skb, match_attr);
389 return 0;
391 err_match_value_put:
392 nla_nest_cancel(skb, match_attr);
393 return err;
396 static int devlink_dpipe_entry_put(struct sk_buff *skb,
397 struct devlink_dpipe_entry *entry)
399 struct nlattr *entry_attr, *matches_attr, *actions_attr;
400 int err;
402 entry_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_ENTRY);
403 if (!entry_attr)
404 return -EMSGSIZE;
406 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_INDEX, entry->index,
407 DEVLINK_ATTR_PAD))
408 goto nla_put_failure;
409 if (entry->counter_valid)
410 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_COUNTER,
411 entry->counter, DEVLINK_ATTR_PAD))
412 goto nla_put_failure;
414 matches_attr = nla_nest_start_noflag(skb,
415 DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES);
416 if (!matches_attr)
417 goto nla_put_failure;
419 err = devlink_dpipe_match_values_put(skb, entry->match_values,
420 entry->match_values_count);
421 if (err) {
422 nla_nest_cancel(skb, matches_attr);
423 goto err_match_values_put;
425 nla_nest_end(skb, matches_attr);
427 actions_attr = nla_nest_start_noflag(skb,
428 DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES);
429 if (!actions_attr)
430 goto nla_put_failure;
432 err = devlink_dpipe_action_values_put(skb, entry->action_values,
433 entry->action_values_count);
434 if (err) {
435 nla_nest_cancel(skb, actions_attr);
436 goto err_action_values_put;
438 nla_nest_end(skb, actions_attr);
440 nla_nest_end(skb, entry_attr);
441 return 0;
443 nla_put_failure:
444 err = -EMSGSIZE;
445 err_match_values_put:
446 err_action_values_put:
447 nla_nest_cancel(skb, entry_attr);
448 return err;
451 static struct devlink_dpipe_table *
452 devlink_dpipe_table_find(struct list_head *dpipe_tables,
453 const char *table_name, struct devlink *devlink)
455 struct devlink_dpipe_table *table;
457 list_for_each_entry_rcu(table, dpipe_tables, list,
458 lockdep_is_held(&devlink->lock)) {
459 if (!strcmp(table->name, table_name))
460 return table;
462 return NULL;
465 int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx)
467 struct devlink *devlink;
468 int err;
470 err = devlink_dpipe_send_and_alloc_skb(&dump_ctx->skb,
471 dump_ctx->info);
472 if (err)
473 return err;
475 dump_ctx->hdr = genlmsg_put(dump_ctx->skb,
476 dump_ctx->info->snd_portid,
477 dump_ctx->info->snd_seq,
478 &devlink_nl_family, NLM_F_MULTI,
479 dump_ctx->cmd);
480 if (!dump_ctx->hdr)
481 goto nla_put_failure;
483 devlink = dump_ctx->info->user_ptr[0];
484 if (devlink_nl_put_handle(dump_ctx->skb, devlink))
485 goto nla_put_failure;
486 dump_ctx->nest = nla_nest_start_noflag(dump_ctx->skb,
487 DEVLINK_ATTR_DPIPE_ENTRIES);
488 if (!dump_ctx->nest)
489 goto nla_put_failure;
490 return 0;
492 nla_put_failure:
493 nlmsg_free(dump_ctx->skb);
494 return -EMSGSIZE;
496 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_prepare);
498 int devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx,
499 struct devlink_dpipe_entry *entry)
501 return devlink_dpipe_entry_put(dump_ctx->skb, entry);
503 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_append);
505 int devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx)
507 nla_nest_end(dump_ctx->skb, dump_ctx->nest);
508 genlmsg_end(dump_ctx->skb, dump_ctx->hdr);
509 return 0;
511 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_close);
513 void devlink_dpipe_entry_clear(struct devlink_dpipe_entry *entry)
516 unsigned int value_count, value_index;
517 struct devlink_dpipe_value *value;
519 value = entry->action_values;
520 value_count = entry->action_values_count;
521 for (value_index = 0; value_index < value_count; value_index++) {
522 kfree(value[value_index].value);
523 kfree(value[value_index].mask);
526 value = entry->match_values;
527 value_count = entry->match_values_count;
528 for (value_index = 0; value_index < value_count; value_index++) {
529 kfree(value[value_index].value);
530 kfree(value[value_index].mask);
533 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_clear);
535 static int devlink_dpipe_entries_fill(struct genl_info *info,
536 enum devlink_command cmd, int flags,
537 struct devlink_dpipe_table *table)
539 struct devlink_dpipe_dump_ctx dump_ctx;
540 struct nlmsghdr *nlh;
541 int err;
543 dump_ctx.skb = NULL;
544 dump_ctx.cmd = cmd;
545 dump_ctx.info = info;
547 err = table->table_ops->entries_dump(table->priv,
548 table->counters_enabled,
549 &dump_ctx);
550 if (err)
551 return err;
553 send_done:
554 nlh = nlmsg_put(dump_ctx.skb, info->snd_portid, info->snd_seq,
555 NLMSG_DONE, 0, flags | NLM_F_MULTI);
556 if (!nlh) {
557 err = devlink_dpipe_send_and_alloc_skb(&dump_ctx.skb, info);
558 if (err)
559 return err;
560 goto send_done;
562 return genlmsg_reply(dump_ctx.skb, info);
565 int devlink_nl_dpipe_entries_get_doit(struct sk_buff *skb,
566 struct genl_info *info)
568 struct devlink *devlink = info->user_ptr[0];
569 struct devlink_dpipe_table *table;
570 const char *table_name;
572 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_DPIPE_TABLE_NAME))
573 return -EINVAL;
575 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
576 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
577 table_name, devlink);
578 if (!table)
579 return -EINVAL;
581 if (!table->table_ops->entries_dump)
582 return -EINVAL;
584 return devlink_dpipe_entries_fill(info, DEVLINK_CMD_DPIPE_ENTRIES_GET,
585 0, table);
588 static int devlink_dpipe_fields_put(struct sk_buff *skb,
589 const struct devlink_dpipe_header *header)
591 struct devlink_dpipe_field *field;
592 struct nlattr *field_attr;
593 int i;
595 for (i = 0; i < header->fields_count; i++) {
596 field = &header->fields[i];
597 field_attr = nla_nest_start_noflag(skb,
598 DEVLINK_ATTR_DPIPE_FIELD);
599 if (!field_attr)
600 return -EMSGSIZE;
601 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_FIELD_NAME, field->name) ||
602 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
603 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH, field->bitwidth) ||
604 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE, field->mapping_type))
605 goto nla_put_failure;
606 nla_nest_end(skb, field_attr);
608 return 0;
610 nla_put_failure:
611 nla_nest_cancel(skb, field_attr);
612 return -EMSGSIZE;
615 static int devlink_dpipe_header_put(struct sk_buff *skb,
616 struct devlink_dpipe_header *header)
618 struct nlattr *fields_attr, *header_attr;
619 int err;
621 header_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_HEADER);
622 if (!header_attr)
623 return -EMSGSIZE;
625 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_HEADER_NAME, header->name) ||
626 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
627 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
628 goto nla_put_failure;
630 fields_attr = nla_nest_start_noflag(skb,
631 DEVLINK_ATTR_DPIPE_HEADER_FIELDS);
632 if (!fields_attr)
633 goto nla_put_failure;
635 err = devlink_dpipe_fields_put(skb, header);
636 if (err) {
637 nla_nest_cancel(skb, fields_attr);
638 goto nla_put_failure;
640 nla_nest_end(skb, fields_attr);
641 nla_nest_end(skb, header_attr);
642 return 0;
644 nla_put_failure:
645 err = -EMSGSIZE;
646 nla_nest_cancel(skb, header_attr);
647 return err;
650 static int devlink_dpipe_headers_fill(struct genl_info *info,
651 enum devlink_command cmd, int flags,
652 struct devlink_dpipe_headers *
653 dpipe_headers)
655 struct devlink *devlink = info->user_ptr[0];
656 struct nlattr *headers_attr;
657 struct sk_buff *skb = NULL;
658 struct nlmsghdr *nlh;
659 void *hdr;
660 int i, j;
661 int err;
663 i = 0;
664 start_again:
665 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
666 if (err)
667 return err;
669 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
670 &devlink_nl_family, NLM_F_MULTI, cmd);
671 if (!hdr) {
672 nlmsg_free(skb);
673 return -EMSGSIZE;
676 if (devlink_nl_put_handle(skb, devlink))
677 goto nla_put_failure;
678 headers_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_HEADERS);
679 if (!headers_attr)
680 goto nla_put_failure;
682 j = 0;
683 for (; i < dpipe_headers->headers_count; i++) {
684 err = devlink_dpipe_header_put(skb, dpipe_headers->headers[i]);
685 if (err) {
686 if (!j)
687 goto err_table_put;
688 break;
690 j++;
692 nla_nest_end(skb, headers_attr);
693 genlmsg_end(skb, hdr);
694 if (i != dpipe_headers->headers_count)
695 goto start_again;
697 send_done:
698 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
699 NLMSG_DONE, 0, flags | NLM_F_MULTI);
700 if (!nlh) {
701 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
702 if (err)
703 return err;
704 goto send_done;
706 return genlmsg_reply(skb, info);
708 nla_put_failure:
709 err = -EMSGSIZE;
710 err_table_put:
711 nlmsg_free(skb);
712 return err;
715 int devlink_nl_dpipe_headers_get_doit(struct sk_buff *skb,
716 struct genl_info *info)
718 struct devlink *devlink = info->user_ptr[0];
720 if (!devlink->dpipe_headers)
721 return -EOPNOTSUPP;
722 return devlink_dpipe_headers_fill(info, DEVLINK_CMD_DPIPE_HEADERS_GET,
723 0, devlink->dpipe_headers);
726 static int devlink_dpipe_table_counters_set(struct devlink *devlink,
727 const char *table_name,
728 bool enable)
730 struct devlink_dpipe_table *table;
732 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
733 table_name, devlink);
734 if (!table)
735 return -EINVAL;
737 if (table->counter_control_extern)
738 return -EOPNOTSUPP;
740 if (!(table->counters_enabled ^ enable))
741 return 0;
743 table->counters_enabled = enable;
744 if (table->table_ops->counters_set_update)
745 table->table_ops->counters_set_update(table->priv, enable);
746 return 0;
749 int devlink_nl_dpipe_table_counters_set_doit(struct sk_buff *skb,
750 struct genl_info *info)
752 struct devlink *devlink = info->user_ptr[0];
753 const char *table_name;
754 bool counters_enable;
756 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_DPIPE_TABLE_NAME) ||
757 GENL_REQ_ATTR_CHECK(info,
758 DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED))
759 return -EINVAL;
761 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
762 counters_enable = !!nla_get_u8(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED]);
764 return devlink_dpipe_table_counters_set(devlink, table_name,
765 counters_enable);
769 * devl_dpipe_headers_register - register dpipe headers
771 * @devlink: devlink
772 * @dpipe_headers: dpipe header array
774 * Register the headers supported by hardware.
776 void devl_dpipe_headers_register(struct devlink *devlink,
777 struct devlink_dpipe_headers *dpipe_headers)
779 lockdep_assert_held(&devlink->lock);
781 devlink->dpipe_headers = dpipe_headers;
783 EXPORT_SYMBOL_GPL(devl_dpipe_headers_register);
786 * devl_dpipe_headers_unregister - unregister dpipe headers
788 * @devlink: devlink
790 * Unregister the headers supported by hardware.
792 void devl_dpipe_headers_unregister(struct devlink *devlink)
794 lockdep_assert_held(&devlink->lock);
796 devlink->dpipe_headers = NULL;
798 EXPORT_SYMBOL_GPL(devl_dpipe_headers_unregister);
801 * devlink_dpipe_table_counter_enabled - check if counter allocation
802 * required
803 * @devlink: devlink
804 * @table_name: tables name
806 * Used by driver to check if counter allocation is required.
807 * After counter allocation is turned on the table entries
808 * are updated to include counter statistics.
810 * After that point on the driver must respect the counter
811 * state so that each entry added to the table is added
812 * with a counter.
814 bool devlink_dpipe_table_counter_enabled(struct devlink *devlink,
815 const char *table_name)
817 struct devlink_dpipe_table *table;
818 bool enabled;
820 rcu_read_lock();
821 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
822 table_name, devlink);
823 enabled = false;
824 if (table)
825 enabled = table->counters_enabled;
826 rcu_read_unlock();
827 return enabled;
829 EXPORT_SYMBOL_GPL(devlink_dpipe_table_counter_enabled);
832 * devl_dpipe_table_register - register dpipe table
834 * @devlink: devlink
835 * @table_name: table name
836 * @table_ops: table ops
837 * @priv: priv
838 * @counter_control_extern: external control for counters
840 int devl_dpipe_table_register(struct devlink *devlink,
841 const char *table_name,
842 const struct devlink_dpipe_table_ops *table_ops,
843 void *priv, bool counter_control_extern)
845 struct devlink_dpipe_table *table;
847 lockdep_assert_held(&devlink->lock);
849 if (WARN_ON(!table_ops->size_get))
850 return -EINVAL;
852 if (devlink_dpipe_table_find(&devlink->dpipe_table_list, table_name,
853 devlink))
854 return -EEXIST;
856 table = kzalloc(sizeof(*table), GFP_KERNEL);
857 if (!table)
858 return -ENOMEM;
860 table->name = table_name;
861 table->table_ops = table_ops;
862 table->priv = priv;
863 table->counter_control_extern = counter_control_extern;
865 list_add_tail_rcu(&table->list, &devlink->dpipe_table_list);
867 return 0;
869 EXPORT_SYMBOL_GPL(devl_dpipe_table_register);
872 * devl_dpipe_table_unregister - unregister dpipe table
874 * @devlink: devlink
875 * @table_name: table name
877 void devl_dpipe_table_unregister(struct devlink *devlink,
878 const char *table_name)
880 struct devlink_dpipe_table *table;
882 lockdep_assert_held(&devlink->lock);
884 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
885 table_name, devlink);
886 if (!table)
887 return;
888 list_del_rcu(&table->list);
889 kfree_rcu(table, rcu);
891 EXPORT_SYMBOL_GPL(devl_dpipe_table_unregister);
894 * devl_dpipe_table_resource_set - set the resource id
896 * @devlink: devlink
897 * @table_name: table name
898 * @resource_id: resource id
899 * @resource_units: number of resource's units consumed per table's entry
901 int devl_dpipe_table_resource_set(struct devlink *devlink,
902 const char *table_name, u64 resource_id,
903 u64 resource_units)
905 struct devlink_dpipe_table *table;
907 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
908 table_name, devlink);
909 if (!table)
910 return -EINVAL;
912 table->resource_id = resource_id;
913 table->resource_units = resource_units;
914 table->resource_valid = true;
915 return 0;
917 EXPORT_SYMBOL_GPL(devl_dpipe_table_resource_set);