Blame SOURCES/0026-devlink-Add-support-for-pipeline-debug-dpipe.patch

cd1737
From 120f9c488ba7d291f899f1ec2f77e0ae33efcd88 Mon Sep 17 00:00:00 2001
cd1737
From: Kamal Heib <kheib@redhat.com>
cd1737
Date: Thu, 9 Nov 2017 04:44:32 -0500
cd1737
Subject: [PATCH] devlink: Add support for pipeline debug (dpipe)
cd1737
cd1737
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1456539
cd1737
cd1737
commit 153c1a9b21e5b7b78e066de2b93a4edb8c3dc498
cd1737
Author: Arkadi Sharshevsky <arkadis@mellanox.com>
cd1737
Date:   Wed May 3 13:25:23 2017 +0200
cd1737
cd1737
    devlink: Add support for pipeline debug (dpipe)
cd1737
cd1737
    Add support for pipeline debug (dpipe). The headers are used both the
cd1737
    gain visibillity into the headers supported by the hardware, and to
cd1737
    build the headers/field database which is used by other commands.
cd1737
cd1737
    Examples:
cd1737
cd1737
    First we can see the headers supported by the hardware:
cd1737
cd1737
    $devlink dpipe header show pci/0000:03:00.0
cd1737
cd1737
    pci/0000:03:00.0:
cd1737
      name mlxsw_meta
cd1737
      field:
cd1737
        name erif_port bitwidth 32 mapping_type ifindex
cd1737
        name l3_forward bitwidth 1
cd1737
        name l3_drop bitwidth 1
cd1737
cd1737
    Note that mapping_type is presented only if relevant. Also the header/
cd1737
    field id's are reported by the kernel they are not shown by default.
cd1737
    They can be observed by using the -v option. Also the headers scope
cd1737
    (global/local) is specified.
cd1737
cd1737
    $devlink -v dpipe header show pci/0000:03:00.0
cd1737
cd1737
    pci/0000:03:00.0:
cd1737
      name mlxsw_meta id 0 global false
cd1737
      field:
cd1737
        name erif_port id 0 bitwidth 32 mapping_type ifindex
cd1737
        name l3_forward id 1 bitwidth 1
cd1737
        name l3_drop id 2 bitwidth 1
cd1737
cd1737
    Second we can examine the tables supported by the hardware. In order
cd1737
    to dump all the tables no table name should be provided:
cd1737
    $devlink dpipe table show pci/0000:03:00.0
cd1737
cd1737
    In order to examine specific table its name have to be specified:
cd1737
    $devlink dpipe table show pci/0000:03:00.0 name erif
cd1737
cd1737
    pci/0000:03:00.0:
cd1737
      name mlxsw_erif size 800 counters_enabled true
cd1737
      match:
cd1737
        type field_exact header mlxsw_meta field erif_port mapping ifindex
cd1737
      action:
cd1737
        type field_modify header mlxsw_meta field l3_forward
cd1737
        type field_modify header mlxsw_meta field l3_drop
cd1737
cd1737
    To enable/disable counters on the table:
cd1737
    $devlink dpipe table set pci/0000:03:00.0 name erif counters enable
cd1737
    $devlink dpipe table set pci/0000:03:00.0 name erif counters disable
cd1737
cd1737
    In order to see the current entries in the hardware for specific table:
cd1737
    $devlink dpipe table dump pci/0000:03:00.0 name erif
cd1737
cd1737
    pci/0000:03:00.0:
cd1737
      index 0 counter 0
cd1737
      match_value:
cd1737
        type field_exact header mlxsw_meta field erif_port mapping ifindex mapping_value 383 value 0
cd1737
      action_value:
cd1737
        type field_modify header mlxsw_meta field l3_forward value 1
cd1737
cd1737
      index 1 counter 0
cd1737
      match_value:
cd1737
        type field_exact header mlxsw_meta field erif_port mapping ifindex mapping_value 381 value 1
cd1737
      action_value:
cd1737
        type field_modify header mlxsw_meta field l3_forward value 1
cd1737
cd1737
    In the above example the table contains two entries which does match
cd1737
    on erif port and forwards the packet or drop it (currently only the
cd1737
    forward count is implemented). The counter values are provided for
cd1737
    example. In case the counting is not enabled on the table the counters
cd1737
    will not be available.
cd1737
cd1737
    Signed-off-by: Arkadi Sharshevsky <arkadis@mellanox.com>
cd1737
    Signed-off-by: Jiri Pirko <jiri@mellanox.com>
cd1737
cd1737
Signed-off-by: Kamal Heib <kheib@redhat.com>
cd1737
---
cd1737
 devlink/devlink.c | 1353 +++++++++++++++++++++++++++++++++++++++++++++++++----
cd1737
 1 file changed, 1254 insertions(+), 99 deletions(-)
cd1737
cd1737
diff --git a/devlink/devlink.c b/devlink/devlink.c
cd1737
index 35220d8..e22ee0a 100644
cd1737
--- a/devlink/devlink.c
cd1737
+++ b/devlink/devlink.c
cd1737
@@ -34,7 +34,15 @@
cd1737
 #define ESWITCH_INLINE_MODE_TRANSPORT "transport"
cd1737
 
cd1737
 #define pr_err(args...) fprintf(stderr, ##args)
cd1737
-#define pr_out(args...) fprintf(stdout, ##args)
cd1737
+#define pr_out(args...)						\
cd1737
+	do {							\
cd1737
+		if (g_indent_newline) {				\
cd1737
+			fprintf(stdout, "%s", g_indent_str);	\
cd1737
+			g_indent_newline = false;		\
cd1737
+		}						\
cd1737
+		fprintf(stdout, ##args);			\
cd1737
+	} while (0)
cd1737
+
cd1737
 #define pr_out_sp(num, args...)					\
cd1737
 	do {							\
cd1737
 		int ret = fprintf(stdout, ##args);		\
cd1737
@@ -42,6 +50,35 @@
cd1737
 			fprintf(stdout, "%*s", num - ret, "");	\
cd1737
 	} while (0)
cd1737
 
cd1737
+static int g_indent_level;
cd1737
+static bool g_indent_newline;
cd1737
+#define INDENT_STR_STEP 2
cd1737
+#define INDENT_STR_MAXLEN 32
cd1737
+static char g_indent_str[INDENT_STR_MAXLEN + 1] = "";
cd1737
+
cd1737
+static void __pr_out_indent_inc(void)
cd1737
+{
cd1737
+	if (g_indent_level + INDENT_STR_STEP > INDENT_STR_MAXLEN)
cd1737
+		return;
cd1737
+	g_indent_level += INDENT_STR_STEP;
cd1737
+	memset(g_indent_str, ' ', sizeof(g_indent_str));
cd1737
+	g_indent_str[g_indent_level] = '\0';
cd1737
+}
cd1737
+
cd1737
+static void __pr_out_indent_dec(void)
cd1737
+{
cd1737
+	if (g_indent_level - INDENT_STR_STEP < 0)
cd1737
+		return;
cd1737
+	g_indent_level -= INDENT_STR_STEP;
cd1737
+	g_indent_str[g_indent_level] = '\0';
cd1737
+}
cd1737
+
cd1737
+static void __pr_out_newline(void)
cd1737
+{
cd1737
+	pr_out("\n");
cd1737
+	g_indent_newline = true;
cd1737
+}
cd1737
+
cd1737
 static int _mnlg_socket_recv_run(struct mnlg_socket *nlg,
cd1737
 				 mnl_cb_t data_cb, void *data)
cd1737
 {
cd1737
@@ -137,6 +174,8 @@ static void ifname_map_free(struct ifname_map *ifname_map)
cd1737
 #define DL_OPT_SB_TC		BIT(10)
cd1737
 #define DL_OPT_ESWITCH_MODE	BIT(11)
cd1737
 #define DL_OPT_ESWITCH_INLINE_MODE	BIT(12)
cd1737
+#define DL_OPT_DPIPE_TABLE_NAME	BIT(13)
cd1737
+#define DL_OPT_DPIPE_TABLE_COUNTERS	BIT(14)
cd1737
 
cd1737
 struct dl_opts {
cd1737
 	uint32_t present; /* flags of present items */
cd1737
@@ -154,6 +193,8 @@ struct dl_opts {
cd1737
 	uint16_t sb_tc_index;
cd1737
 	enum devlink_eswitch_mode eswitch_mode;
cd1737
 	enum devlink_eswitch_inline_mode eswitch_inline_mode;
cd1737
+	const char *dpipe_table_name;
cd1737
+	bool dpipe_counters_enable;
cd1737
 };
cd1737
 
cd1737
 struct dl {
cd1737
@@ -166,6 +207,7 @@ struct dl {
cd1737
 	json_writer_t *jw;
cd1737
 	bool json_output;
cd1737
 	bool pretty_output;
cd1737
+	bool verbose;
cd1737
 	struct {
cd1737
 		bool present;
cd1737
 		char *bus_name;
cd1737
@@ -257,6 +299,38 @@ static const enum mnl_attr_data_type devlink_policy[DEVLINK_ATTR_MAX + 1] = {
cd1737
 	[DEVLINK_ATTR_SB_OCC_MAX] = MNL_TYPE_U32,
cd1737
 	[DEVLINK_ATTR_ESWITCH_MODE] = MNL_TYPE_U16,
cd1737
 	[DEVLINK_ATTR_ESWITCH_INLINE_MODE] = MNL_TYPE_U8,
cd1737
+	[DEVLINK_ATTR_DPIPE_TABLES] = MNL_TYPE_NESTED,
cd1737
+	[DEVLINK_ATTR_DPIPE_TABLE] = MNL_TYPE_NESTED,
cd1737
+	[DEVLINK_ATTR_DPIPE_TABLE_NAME] = MNL_TYPE_STRING,
cd1737
+	[DEVLINK_ATTR_DPIPE_TABLE_SIZE] = MNL_TYPE_U64,
cd1737
+	[DEVLINK_ATTR_DPIPE_TABLE_MATCHES] = MNL_TYPE_NESTED,
cd1737
+	[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS] = MNL_TYPE_NESTED,
cd1737
+	[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED] =  MNL_TYPE_U8,
cd1737
+	[DEVLINK_ATTR_DPIPE_ENTRIES] = MNL_TYPE_NESTED,
cd1737
+	[DEVLINK_ATTR_DPIPE_ENTRY] = MNL_TYPE_NESTED,
cd1737
+	[DEVLINK_ATTR_DPIPE_ENTRY_INDEX] = MNL_TYPE_U64,
cd1737
+	[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES] = MNL_TYPE_NESTED,
cd1737
+	[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES] = MNL_TYPE_NESTED,
cd1737
+	[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER] = MNL_TYPE_U64,
cd1737
+	[DEVLINK_ATTR_DPIPE_MATCH] = MNL_TYPE_NESTED,
cd1737
+	[DEVLINK_ATTR_DPIPE_MATCH_VALUE] = MNL_TYPE_NESTED,
cd1737
+	[DEVLINK_ATTR_DPIPE_MATCH_TYPE] = MNL_TYPE_U32,
cd1737
+	[DEVLINK_ATTR_DPIPE_ACTION] = MNL_TYPE_NESTED,
cd1737
+	[DEVLINK_ATTR_DPIPE_ACTION_VALUE] = MNL_TYPE_NESTED,
cd1737
+	[DEVLINK_ATTR_DPIPE_ACTION_TYPE] = MNL_TYPE_U32,
cd1737
+	[DEVLINK_ATTR_DPIPE_VALUE_MAPPING] = MNL_TYPE_U32,
cd1737
+	[DEVLINK_ATTR_DPIPE_HEADERS] = MNL_TYPE_NESTED,
cd1737
+	[DEVLINK_ATTR_DPIPE_HEADER] = MNL_TYPE_NESTED,
cd1737
+	[DEVLINK_ATTR_DPIPE_HEADER_NAME] = MNL_TYPE_STRING,
cd1737
+	[DEVLINK_ATTR_DPIPE_HEADER_ID] = MNL_TYPE_U32,
cd1737
+	[DEVLINK_ATTR_DPIPE_HEADER_FIELDS] = MNL_TYPE_NESTED,
cd1737
+	[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL] = MNL_TYPE_U8,
cd1737
+	[DEVLINK_ATTR_DPIPE_HEADER_INDEX] = MNL_TYPE_U32,
cd1737
+	[DEVLINK_ATTR_DPIPE_FIELD] = MNL_TYPE_NESTED,
cd1737
+	[DEVLINK_ATTR_DPIPE_FIELD_NAME] = MNL_TYPE_STRING,
cd1737
+	[DEVLINK_ATTR_DPIPE_FIELD_ID] = MNL_TYPE_U32,
cd1737
+	[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH] = MNL_TYPE_U32,
cd1737
+	[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE] = MNL_TYPE_U32,
cd1737
 };
cd1737
 
cd1737
 static int attr_cb(const struct nlattr *attr, void *data)
cd1737
@@ -666,6 +740,20 @@ static int eswitch_inline_mode_get(const char *typestr,
cd1737
 	return 0;
cd1737
 }
cd1737
 
cd1737
+static int dpipe_counters_enable_get(const char *typestr,
cd1737
+				     bool *counters_enable)
cd1737
+{
cd1737
+	if (strcmp(typestr, "enable") == 0) {
cd1737
+		*counters_enable = 1;
cd1737
+	} else if (strcmp(typestr, "disable") == 0) {
cd1737
+		*counters_enable = 0;
cd1737
+	} else {
cd1737
+		pr_err("Unknown counter_state \"%s\"\n", typestr);
cd1737
+		return -EINVAL;
cd1737
+	}
cd1737
+	return 0;
cd1737
+}
cd1737
+
cd1737
 static int dl_argv_parse(struct dl *dl, uint32_t o_required,
cd1737
 			 uint32_t o_optional)
cd1737
 {
cd1737
@@ -800,6 +888,27 @@ static int dl_argv_parse(struct dl *dl, uint32_t o_required,
cd1737
 			if (err)
cd1737
 				return err;
cd1737
 			o_found |= DL_OPT_ESWITCH_INLINE_MODE;
cd1737
+		} else if (dl_argv_match(dl, "name") &&
cd1737
+			   (o_all & DL_OPT_DPIPE_TABLE_NAME)) {
cd1737
+			dl_arg_inc(dl);
cd1737
+			err = dl_argv_str(dl, &opts->dpipe_table_name);
cd1737
+			if (err)
cd1737
+				return err;
cd1737
+			o_found |= DL_OPT_DPIPE_TABLE_NAME;
cd1737
+		} else if (dl_argv_match(dl, "counters") &&
cd1737
+			   (o_all & DL_OPT_DPIPE_TABLE_COUNTERS)) {
cd1737
+			const char *typestr;
cd1737
+
cd1737
+			dl_arg_inc(dl);
cd1737
+			err = dl_argv_str(dl, &typestr);
cd1737
+			if (err)
cd1737
+				return err;
cd1737
+			err = dpipe_counters_enable_get(typestr,
cd1737
+							&opts->dpipe_counters_enable);
cd1737
+			if (err)
cd1737
+				return err;
cd1737
+			o_found |= DL_OPT_DPIPE_TABLE_COUNTERS;
cd1737
+
cd1737
 		} else {
cd1737
 			pr_err("Unknown option \"%s\"\n", dl_argv(dl));
cd1737
 			return -EINVAL;
cd1737
@@ -866,6 +975,17 @@ static int dl_argv_parse(struct dl *dl, uint32_t o_required,
cd1737
 		return -EINVAL;
cd1737
 	}
cd1737
 
cd1737
+	if ((o_required & DL_OPT_DPIPE_TABLE_NAME) &&
cd1737
+	    !(o_found & DL_OPT_DPIPE_TABLE_NAME)) {
cd1737
+		pr_err("Dpipe table name expected\n");
cd1737
+		return -EINVAL;
cd1737
+	}
cd1737
+
cd1737
+	if ((o_required & DL_OPT_DPIPE_TABLE_COUNTERS) &&
cd1737
+	    !(o_found & DL_OPT_DPIPE_TABLE_COUNTERS)) {
cd1737
+		pr_err("Dpipe table counter state expected\n");
cd1737
+		return -EINVAL;
cd1737
+	}
cd1737
 	return 0;
cd1737
 }
cd1737
 
cd1737
@@ -915,6 +1035,12 @@ static void dl_opts_put(struct nlmsghdr *nlh, struct dl *dl)
cd1737
 	if (opts->present & DL_OPT_ESWITCH_INLINE_MODE)
cd1737
 		mnl_attr_put_u8(nlh, DEVLINK_ATTR_ESWITCH_INLINE_MODE,
cd1737
 				opts->eswitch_inline_mode);
cd1737
+	if (opts->present & DL_OPT_DPIPE_TABLE_NAME)
cd1737
+		mnl_attr_put_strz(nlh, DEVLINK_ATTR_DPIPE_TABLE_NAME,
cd1737
+				  opts->dpipe_table_name);
cd1737
+	if (opts->present & DL_OPT_DPIPE_TABLE_COUNTERS)
cd1737
+		mnl_attr_put_u8(nlh, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED,
cd1737
+				opts->dpipe_counters_enable);
cd1737
 }
cd1737
 
cd1737
 static int dl_argv_parse_put(struct nlmsghdr *nlh, struct dl *dl,
cd1737
@@ -1033,7 +1159,19 @@ static void __pr_out_handle_start(struct dl *dl, struct nlattr **tb,
cd1737
 			jsonw_start_object(dl->jw);
cd1737
 		}
cd1737
 	} else {
cd1737
-		pr_out("%s%s", buf, content ? ":" : "");
cd1737
+		if (array) {
cd1737
+			if (should_arr_last_handle_end(dl, bus_name, dev_name))
cd1737
+				__pr_out_indent_dec();
cd1737
+			if (should_arr_last_handle_start(dl, bus_name,
cd1737
+							 dev_name)) {
cd1737
+				pr_out("%s%s", buf, content ? ":" : "");
cd1737
+				__pr_out_newline();
cd1737
+				__pr_out_indent_inc();
cd1737
+				arr_last_handle_set(dl, bus_name, dev_name);
cd1737
+			}
cd1737
+		} else {
cd1737
+			pr_out("%s%s", buf, content ? ":" : "");
cd1737
+		}
cd1737
 	}
cd1737
 }
cd1737
 
cd1737
@@ -1047,7 +1185,7 @@ static void pr_out_handle_end(struct dl *dl)
cd1737
 	if (dl->json_output)
cd1737
 		jsonw_end_object(dl->jw);
cd1737
 	else
cd1737
-		pr_out("\n");
cd1737
+		__pr_out_newline();
cd1737
 }
cd1737
 
cd1737
 static void pr_out_handle(struct dl *dl, struct nlattr **tb)
cd1737
@@ -1163,18 +1301,26 @@ static void pr_out_port_handle_end(struct dl *dl)
cd1737
 
cd1737
 static void pr_out_str(struct dl *dl, const char *name, const char *val)
cd1737
 {
cd1737
-	if (dl->json_output)
cd1737
+	if (dl->json_output) {
cd1737
 		jsonw_string_field(dl->jw, name, val);
cd1737
-	else
cd1737
-		pr_out(" %s %s", name, val);
cd1737
+	} else {
cd1737
+		if (g_indent_newline)
cd1737
+			pr_out("%s %s", name, val);
cd1737
+		else
cd1737
+			pr_out(" %s %s", name, val);
cd1737
+	}
cd1737
 }
cd1737
 
cd1737
 static void pr_out_uint(struct dl *dl, const char *name, unsigned int val)
cd1737
 {
cd1737
-	if (dl->json_output)
cd1737
+	if (dl->json_output) {
cd1737
 		jsonw_uint_field(dl->jw, name, val);
cd1737
-	else
cd1737
-		pr_out(" %s %u", name, val);
cd1737
+	} else {
cd1737
+		if (g_indent_newline)
cd1737
+			pr_out("%s %u", name, val);
cd1737
+		else
cd1737
+			pr_out(" %s %u", name, val);
cd1737
+	}
cd1737
 }
cd1737
 
cd1737
 static void pr_out_dev(struct dl *dl, struct nlattr **tb)
cd1737
@@ -1201,6 +1347,42 @@ static void pr_out_section_end(struct dl *dl)
cd1737
 	}
cd1737
 }
cd1737
 
cd1737
+static void pr_out_array_start(struct dl *dl, const char *name)
cd1737
+{
cd1737
+	if (dl->json_output) {
cd1737
+		jsonw_name(dl->jw, name);
cd1737
+		jsonw_start_array(dl->jw);
cd1737
+	} else {
cd1737
+		if (!g_indent_newline)
cd1737
+			__pr_out_newline();
cd1737
+		pr_out("%s:", name);
cd1737
+		__pr_out_newline();
cd1737
+		__pr_out_indent_inc();
cd1737
+	}
cd1737
+}
cd1737
+
cd1737
+static void pr_out_array_end(struct dl *dl)
cd1737
+{
cd1737
+	if (dl->json_output)
cd1737
+		jsonw_end_array(dl->jw);
cd1737
+	else
cd1737
+		__pr_out_indent_dec();
cd1737
+}
cd1737
+
cd1737
+static void pr_out_entry_start(struct dl *dl)
cd1737
+{
cd1737
+	if (dl->json_output)
cd1737
+		jsonw_start_object(dl->jw);
cd1737
+}
cd1737
+
cd1737
+static void pr_out_entry_end(struct dl *dl)
cd1737
+{
cd1737
+	if (dl->json_output)
cd1737
+		jsonw_end_object(dl->jw);
cd1737
+	else
cd1737
+		__pr_out_newline();
cd1737
+}
cd1737
+
cd1737
 static const char *eswitch_mode_name(uint32_t mode)
cd1737
 {
cd1737
 	switch (mode) {
cd1737
@@ -2423,129 +2605,1102 @@ static int cmd_mon(struct dl *dl)
cd1737
 	return -ENOENT;
cd1737
 }
cd1737
 
cd1737
-static void help(void)
cd1737
+struct dpipe_field {
cd1737
+	char *name;
cd1737
+	unsigned int id;
cd1737
+	unsigned int bitwidth;
cd1737
+	enum devlink_dpipe_field_mapping_type mapping_type;
cd1737
+};
cd1737
+
cd1737
+struct dpipe_header {
cd1737
+	struct list_head list;
cd1737
+	char *name;
cd1737
+	unsigned int id;
cd1737
+	struct dpipe_field *fields;
cd1737
+	unsigned int fields_count;
cd1737
+};
cd1737
+
cd1737
+struct dpipe_ctx {
cd1737
+	struct dl *dl;
cd1737
+	int err;
cd1737
+	struct list_head global_headers;
cd1737
+	struct list_head local_headers;
cd1737
+	bool print_headers;
cd1737
+};
cd1737
+
cd1737
+static struct dpipe_header *dpipe_header_alloc(unsigned int fields_count)
cd1737
 {
cd1737
-	pr_err("Usage: devlink [ OPTIONS ] OBJECT { COMMAND | help }\n"
cd1737
-	       "where  OBJECT := { dev | port | sb | monitor }\n"
cd1737
-	       "       OPTIONS := { -V[ersion] | -n[no-nice-names] | -j[json] | -p[pretty] }\n");
cd1737
+	struct dpipe_header *header;
cd1737
+
cd1737
+	header = calloc(1, sizeof(struct dpipe_header));
cd1737
+	if (!header)
cd1737
+		return NULL;
cd1737
+	header->fields = calloc(fields_count, sizeof(struct dpipe_field));
cd1737
+	if (!header->fields)
cd1737
+		goto err_fields_alloc;
cd1737
+	header->fields_count = fields_count;
cd1737
+	return header;
cd1737
+
cd1737
+err_fields_alloc:
cd1737
+	free(header);
cd1737
+	return NULL;
cd1737
 }
cd1737
 
cd1737
-static int dl_cmd(struct dl *dl)
cd1737
+static void dpipe_header_free(struct dpipe_header *header)
cd1737
 {
cd1737
-	if (dl_argv_match(dl, "help") || dl_no_arg(dl)) {
cd1737
-		help();
cd1737
-		return 0;
cd1737
-	} else if (dl_argv_match(dl, "dev")) {
cd1737
-		dl_arg_inc(dl);
cd1737
-		return cmd_dev(dl);
cd1737
-	} else if (dl_argv_match(dl, "port")) {
cd1737
-		dl_arg_inc(dl);
cd1737
-		return cmd_port(dl);
cd1737
-	} else if (dl_argv_match(dl, "sb")) {
cd1737
-		dl_arg_inc(dl);
cd1737
-		return cmd_sb(dl);
cd1737
-	} else if (dl_argv_match(dl, "monitor")) {
cd1737
-		dl_arg_inc(dl);
cd1737
-		return cmd_mon(dl);
cd1737
+	free(header->fields);
cd1737
+	free(header);
cd1737
+}
cd1737
+
cd1737
+static void dpipe_header_clear(struct dpipe_header *header)
cd1737
+{
cd1737
+	struct dpipe_field *field;
cd1737
+	int i;
cd1737
+
cd1737
+	for (i = 0; i < header->fields_count; i++) {
cd1737
+		field = &header->fields[i];
cd1737
+		free(field->name);
cd1737
 	}
cd1737
-	pr_err("Object \"%s\" not found\n", dl_argv(dl));
cd1737
-	return -ENOENT;
cd1737
+	free(header->name);
cd1737
 }
cd1737
 
cd1737
-static int dl_init(struct dl *dl, int argc, char **argv)
cd1737
+static void dpipe_header_add(struct dpipe_ctx *ctx,
cd1737
+			     struct dpipe_header *header, bool global)
cd1737
 {
cd1737
-	int err;
cd1737
+	if (global)
cd1737
+		list_add(&header->list, &ctx->global_headers);
cd1737
+	else
cd1737
+		list_add(&header->list, &ctx->local_headers);
cd1737
+}
cd1737
 
cd1737
-	dl->argc = argc;
cd1737
-	dl->argv = argv;
cd1737
+static void dpipe_header_del(struct dpipe_header *header)
cd1737
+{
cd1737
+	list_del(&header->list);
cd1737
+}
cd1737
 
cd1737
-	dl->nlg = mnlg_socket_open(DEVLINK_GENL_NAME, DEVLINK_GENL_VERSION);
cd1737
-	if (!dl->nlg) {
cd1737
-		pr_err("Failed to connect to devlink Netlink\n");
cd1737
-		return -errno;
cd1737
+static struct dpipe_ctx *dpipe_ctx_alloc(struct dl *dl)
cd1737
+{
cd1737
+	struct dpipe_ctx *ctx;
cd1737
+
cd1737
+	ctx = calloc(1, sizeof(struct dpipe_ctx));
cd1737
+	if (!ctx)
cd1737
+		return NULL;
cd1737
+	ctx->dl = dl;
cd1737
+	INIT_LIST_HEAD(&ctx->global_headers);
cd1737
+	INIT_LIST_HEAD(&ctx->local_headers);
cd1737
+	return ctx;
cd1737
+}
cd1737
+
cd1737
+static void dpipe_ctx_free(struct dpipe_ctx *ctx)
cd1737
+{
cd1737
+	free(ctx);
cd1737
+}
cd1737
+
cd1737
+static void dpipe_ctx_clear(struct dpipe_ctx *ctx)
cd1737
+{
cd1737
+	struct dpipe_header *header, *tmp;
cd1737
+
cd1737
+	list_for_each_entry_safe(header, tmp, &ctx->global_headers,
cd1737
+				 list) {
cd1737
+		dpipe_header_del(header);
cd1737
+		dpipe_header_clear(header);
cd1737
+		dpipe_header_free(header);
cd1737
+	}
cd1737
+	list_for_each_entry_safe(header, tmp, &ctx->local_headers,
cd1737
+				 list) {
cd1737
+		dpipe_header_del(header);
cd1737
+		dpipe_header_clear(header);
cd1737
+		dpipe_header_free(header);
cd1737
 	}
cd1737
+}
cd1737
 
cd1737
-	err = ifname_map_init(dl);
cd1737
-	if (err) {
cd1737
-		pr_err("Failed to create index map\n");
cd1737
-		goto err_ifname_map_create;
cd1737
+static const char *dpipe_header_id2s(struct dpipe_ctx *ctx,
cd1737
+				     uint32_t header_id, bool global)
cd1737
+{
cd1737
+	struct list_head *header_list;
cd1737
+	struct dpipe_header *header;
cd1737
+
cd1737
+	if (global)
cd1737
+		header_list = &ctx->global_headers;
cd1737
+	else
cd1737
+		header_list = &ctx->local_headers;
cd1737
+	list_for_each_entry(header, header_list, list) {
cd1737
+		if (header->id != header_id)
cd1737
+			continue;
cd1737
+		return header->name;
cd1737
 	}
cd1737
-	if (dl->json_output) {
cd1737
-		dl->jw = jsonw_new(stdout);
cd1737
-		if (!dl->jw) {
cd1737
-			pr_err("Failed to create JSON writer\n");
cd1737
-			goto err_json_new;
cd1737
-		}
cd1737
-		jsonw_pretty(dl->jw, dl->pretty_output);
cd1737
+	return NULL;
cd1737
+}
cd1737
+
cd1737
+static const char *dpipe_field_id2s(struct dpipe_ctx *ctx,
cd1737
+				    uint32_t header_id,
cd1737
+				    uint32_t field_id, bool global)
cd1737
+{
cd1737
+	struct list_head *header_list;
cd1737
+	struct dpipe_header *header;
cd1737
+
cd1737
+	if (global)
cd1737
+		header_list = &ctx->global_headers;
cd1737
+	else
cd1737
+		header_list = &ctx->local_headers;
cd1737
+	list_for_each_entry(header, header_list, list) {
cd1737
+		if (header->id != header_id)
cd1737
+			continue;
cd1737
+		return header->fields[field_id].name;
cd1737
 	}
cd1737
-	return 0;
cd1737
+	return NULL;
cd1737
+}
cd1737
 
cd1737
-err_json_new:
cd1737
-	ifname_map_fini(dl);
cd1737
-err_ifname_map_create:
cd1737
-	mnlg_socket_close(dl->nlg);
cd1737
-	return err;
cd1737
+static const char *
cd1737
+dpipe_field_mapping_e2s(enum devlink_dpipe_field_mapping_type mapping_type)
cd1737
+{
cd1737
+	switch (mapping_type) {
cd1737
+	case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_NONE:
cd1737
+		return NULL;
cd1737
+	case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_IFINDEX:
cd1737
+		return "ifindex";
cd1737
+	default:
cd1737
+		return "<unknown>";
cd1737
+	}
cd1737
 }
cd1737
 
cd1737
-static void dl_fini(struct dl *dl)
cd1737
+static const char *
cd1737
+dpipe_mapping_get(struct dpipe_ctx *ctx, uint32_t header_id,
cd1737
+		  uint32_t field_id, bool global)
cd1737
 {
cd1737
-	if (dl->json_output)
cd1737
-		jsonw_destroy(&dl->jw);
cd1737
-	ifname_map_fini(dl);
cd1737
-	mnlg_socket_close(dl->nlg);
cd1737
+	enum devlink_dpipe_field_mapping_type mapping_type;
cd1737
+	struct list_head *header_list;
cd1737
+	struct dpipe_header *header;
cd1737
+
cd1737
+	if (global)
cd1737
+		header_list = &ctx->global_headers;
cd1737
+	else
cd1737
+		header_list = &ctx->local_headers;
cd1737
+	list_for_each_entry(header, header_list, list) {
cd1737
+		if (header->id != header_id)
cd1737
+			continue;
cd1737
+		mapping_type = header->fields[field_id].mapping_type;
cd1737
+		return dpipe_field_mapping_e2s(mapping_type);
cd1737
+	}
cd1737
+	return NULL;
cd1737
 }
cd1737
 
cd1737
-static struct dl *dl_alloc(void)
cd1737
+static void pr_out_dpipe_fields(struct dpipe_ctx *ctx,
cd1737
+				struct dpipe_field *fields,
cd1737
+				unsigned int field_count)
cd1737
 {
cd1737
-	struct dl *dl;
cd1737
+	struct dpipe_field *field;
cd1737
+	int i;
cd1737
 
cd1737
-	dl = calloc(1, sizeof(*dl));
cd1737
-	if (!dl)
cd1737
-		return NULL;
cd1737
-	return dl;
cd1737
+	for (i = 0; i < field_count; i++) {
cd1737
+		field = &fields[i];
cd1737
+		pr_out_entry_start(ctx->dl);
cd1737
+		pr_out_str(ctx->dl, "name", field->name);
cd1737
+		if (ctx->dl->verbose)
cd1737
+			pr_out_uint(ctx->dl, "id", field->id);
cd1737
+		pr_out_uint(ctx->dl, "bitwidth", field->bitwidth);
cd1737
+		if (field->mapping_type)
cd1737
+			pr_out_str(ctx->dl, "mapping_type",
cd1737
+				   dpipe_field_mapping_e2s(field->mapping_type));
cd1737
+		pr_out_entry_end(ctx->dl);
cd1737
+	}
cd1737
 }
cd1737
 
cd1737
-static void dl_free(struct dl *dl)
cd1737
+static void
cd1737
+pr_out_dpipe_header(struct dpipe_ctx *ctx, struct nlattr **tb,
cd1737
+		    struct dpipe_header *header, bool global)
cd1737
 {
cd1737
-	free(dl);
cd1737
+	pr_out_handle_start_arr(ctx->dl, tb);
cd1737
+	pr_out_str(ctx->dl, "name", header->name);
cd1737
+	if (ctx->dl->verbose) {
cd1737
+		pr_out_uint(ctx->dl, "id", header->id);
cd1737
+		pr_out_str(ctx->dl, "global",
cd1737
+			   global ? "true" : "false");
cd1737
+	}
cd1737
+	pr_out_array_start(ctx->dl, "field");
cd1737
+	pr_out_dpipe_fields(ctx, header->fields,
cd1737
+			    header->fields_count);
cd1737
+	pr_out_array_end(ctx->dl);
cd1737
+	pr_out_handle_end(ctx->dl);
cd1737
 }
cd1737
 
cd1737
-int main(int argc, char **argv)
cd1737
+static void pr_out_dpipe_headers(struct dpipe_ctx *ctx,
cd1737
+				 struct nlattr **tb)
cd1737
 {
cd1737
-	static const struct option long_options[] = {
cd1737
-		{ "Version",		no_argument,		NULL, 'V' },
cd1737
-		{ "no-nice-names",	no_argument,		NULL, 'n' },
cd1737
-		{ "json",		no_argument,		NULL, 'j' },
cd1737
-		{ "pretty",		no_argument,		NULL, 'p' },
cd1737
-		{ NULL, 0, NULL, 0 }
cd1737
-	};
cd1737
-	struct dl *dl;
cd1737
-	int opt;
cd1737
+	struct dpipe_header *header;
cd1737
+
cd1737
+	list_for_each_entry(header, &ctx->local_headers, list)
cd1737
+		pr_out_dpipe_header(ctx, tb, header, false);
cd1737
+
cd1737
+	list_for_each_entry(header, &ctx->global_headers, list)
cd1737
+		pr_out_dpipe_header(ctx, tb, header, true);
cd1737
+}
cd1737
+
cd1737
+static int dpipe_header_field_get(struct nlattr *nl, struct dpipe_field *field)
cd1737
+{
cd1737
+	struct nlattr *nla_field[DEVLINK_ATTR_MAX + 1] = {};
cd1737
+	const char *name;
cd1737
 	int err;
cd1737
-	int ret;
cd1737
 
cd1737
-	dl = dl_alloc();
cd1737
-	if (!dl) {
cd1737
-		pr_err("Failed to allocate memory for devlink\n");
cd1737
-		return EXIT_FAILURE;
cd1737
+	err = mnl_attr_parse_nested(nl, attr_cb, nla_field);
cd1737
+	if (err != MNL_CB_OK)
cd1737
+		return -EINVAL;
cd1737
+	if (!nla_field[DEVLINK_ATTR_DPIPE_FIELD_ID] ||
cd1737
+	    !nla_field[DEVLINK_ATTR_DPIPE_FIELD_NAME] ||
cd1737
+	    !nla_field[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH] ||
cd1737
+	    !nla_field[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE])
cd1737
+		return -EINVAL;
cd1737
+
cd1737
+	name = mnl_attr_get_str(nla_field[DEVLINK_ATTR_DPIPE_FIELD_NAME]);
cd1737
+	field->id = mnl_attr_get_u32(nla_field[DEVLINK_ATTR_DPIPE_FIELD_ID]);
cd1737
+	field->bitwidth = mnl_attr_get_u32(nla_field[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH]);
cd1737
+	field->name = strdup(name);
cd1737
+	if (!field->name)
cd1737
+		return -ENOMEM;
cd1737
+	field->mapping_type = mnl_attr_get_u32(nla_field[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE]);
cd1737
+	return 0;
cd1737
+}
cd1737
+
cd1737
+static int dpipe_header_fields_get(struct nlattr *nla_fields,
cd1737
+				   struct dpipe_field *fields)
cd1737
+{
cd1737
+	struct nlattr *nla_field;
cd1737
+	int count = 0;
cd1737
+	int err;
cd1737
+
cd1737
+	mnl_attr_for_each_nested(nla_field, nla_fields) {
cd1737
+		err = dpipe_header_field_get(nla_field, &fields[count]);
cd1737
+		if (err)
cd1737
+			return err;
cd1737
+		count++;
cd1737
 	}
cd1737
+	return 0;
cd1737
+}
cd1737
 
cd1737
-	while ((opt = getopt_long(argc, argv, "Vnjp",
cd1737
-				  long_options, NULL)) >= 0) {
cd1737
+static unsigned int dpipe_header_field_count_get(struct nlattr *nla_fields)
cd1737
+{
cd1737
+	struct nlattr *nla_field;
cd1737
+	unsigned int count = 0;
cd1737
 
cd1737
-		switch (opt) {
cd1737
-		case 'V':
cd1737
-			printf("devlink utility, iproute2-ss%s\n", SNAPSHOT);
cd1737
-			ret = EXIT_SUCCESS;
cd1737
-			goto dl_free;
cd1737
-		case 'n':
cd1737
-			dl->no_nice_names = true;
cd1737
-			break;
cd1737
-		case 'j':
cd1737
-			dl->json_output = true;
cd1737
-			break;
cd1737
-		case 'p':
cd1737
-			dl->pretty_output = true;
cd1737
+	mnl_attr_for_each_nested(nla_field, nla_fields)
cd1737
+		count++;
cd1737
+	return count;
cd1737
+}
cd1737
+
cd1737
+static int dpipe_header_get(struct dpipe_ctx *ctx, struct nlattr *nl)
cd1737
+{
cd1737
+	struct nlattr *nla_header[DEVLINK_ATTR_MAX + 1] = {};
cd1737
+	struct dpipe_header *header;
cd1737
+	unsigned int fields_count;
cd1737
+	const char *header_name;
cd1737
+	bool global;
cd1737
+	int err;
cd1737
+
cd1737
+	err = mnl_attr_parse_nested(nl, attr_cb, nla_header);
cd1737
+	if (err != MNL_CB_OK)
cd1737
+		return -EINVAL;
cd1737
+
cd1737
+	if (!nla_header[DEVLINK_ATTR_DPIPE_HEADER_NAME] ||
cd1737
+	    !nla_header[DEVLINK_ATTR_DPIPE_HEADER_ID] ||
cd1737
+	    !nla_header[DEVLINK_ATTR_DPIPE_HEADER_FIELDS])
cd1737
+		return -EINVAL;
cd1737
+
cd1737
+	fields_count = dpipe_header_field_count_get(nla_header[DEVLINK_ATTR_DPIPE_HEADER_FIELDS]);
cd1737
+	header = dpipe_header_alloc(fields_count);
cd1737
+	if (!header)
cd1737
+		return -ENOMEM;
cd1737
+
cd1737
+	header_name = mnl_attr_get_str(nla_header[DEVLINK_ATTR_DPIPE_HEADER_NAME]);
cd1737
+	header->name = strdup(header_name);
cd1737
+	header->id = mnl_attr_get_u32(nla_header[DEVLINK_ATTR_DPIPE_HEADER_ID]);
cd1737
+	header->fields_count = fields_count;
cd1737
+	global = !!mnl_attr_get_u8(nla_header[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL]);
cd1737
+
cd1737
+	err = dpipe_header_fields_get(nla_header[DEVLINK_ATTR_DPIPE_HEADER_FIELDS],
cd1737
+				      header->fields);
cd1737
+	if (err)
cd1737
+		goto err_field_get;
cd1737
+	dpipe_header_add(ctx, header, global);
cd1737
+	return 0;
cd1737
+
cd1737
+err_field_get:
cd1737
+	dpipe_header_free(header);
cd1737
+	return err;
cd1737
+}
cd1737
+
cd1737
+static int dpipe_headers_get(struct dpipe_ctx *ctx, struct nlattr **tb)
cd1737
+{
cd1737
+	struct nlattr *nla_headers = tb[DEVLINK_ATTR_DPIPE_HEADERS];
cd1737
+	struct nlattr *nla_header;
cd1737
+	int err;
cd1737
+
cd1737
+	mnl_attr_for_each_nested(nla_header, nla_headers) {
cd1737
+		err = dpipe_header_get(ctx, nla_header);
cd1737
+		if (err)
cd1737
+			return err;
cd1737
+	}
cd1737
+	return 0;
cd1737
+}
cd1737
+
cd1737
+static int cmd_dpipe_header_cb(const struct nlmsghdr *nlh, void *data)
cd1737
+{
cd1737
+	struct dpipe_ctx *ctx = data;
cd1737
+	struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
cd1737
+	struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
cd1737
+	int err;
cd1737
+
cd1737
+	mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
cd1737
+	if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
cd1737
+	    !tb[DEVLINK_ATTR_DPIPE_HEADERS])
cd1737
+		return MNL_CB_ERROR;
cd1737
+	err = dpipe_headers_get(ctx, tb);
cd1737
+	if (err) {
cd1737
+		ctx->err = err;
cd1737
+		return MNL_CB_ERROR;
cd1737
+	}
cd1737
+
cd1737
+	if (ctx->print_headers)
cd1737
+		pr_out_dpipe_headers(ctx, tb);
cd1737
+	return MNL_CB_OK;
cd1737
+}
cd1737
+
cd1737
+static int cmd_dpipe_headers_show(struct dl *dl)
cd1737
+{
cd1737
+	struct nlmsghdr *nlh;
cd1737
+	struct dpipe_ctx *ctx;
cd1737
+	uint16_t flags = NLM_F_REQUEST | NLM_F_ACK;
cd1737
+	int err;
cd1737
+
cd1737
+	nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_DPIPE_HEADERS_GET, flags);
cd1737
+
cd1737
+	err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE, 0);
cd1737
+	if (err)
cd1737
+		return err;
cd1737
+
cd1737
+	ctx = dpipe_ctx_alloc(dl);
cd1737
+	if (!ctx)
cd1737
+		return -ENOMEM;
cd1737
+
cd1737
+	ctx->print_headers = true;
cd1737
+
cd1737
+	pr_out_section_start(dl, "header");
cd1737
+	err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_dpipe_header_cb, ctx);
cd1737
+	if (err)
cd1737
+		pr_err("error get headers %s\n", strerror(ctx->err));
cd1737
+	pr_out_section_end(dl);
cd1737
+
cd1737
+	dpipe_ctx_clear(ctx);
cd1737
+	dpipe_ctx_free(ctx);
cd1737
+	return err;
cd1737
+}
cd1737
+
cd1737
+static void cmd_dpipe_header_help(void)
cd1737
+{
cd1737
+	pr_err("Usage: devlink dpipe headers show DEV\n");
cd1737
+}
cd1737
+
cd1737
+static int cmd_dpipe_header(struct dl *dl)
cd1737
+{
cd1737
+	if (dl_argv_match(dl, "help") || dl_no_arg(dl)) {
cd1737
+		cmd_dpipe_header_help();
cd1737
+		return 0;
cd1737
+	} else if (dl_argv_match(dl, "show")) {
cd1737
+		dl_arg_inc(dl);
cd1737
+		return cmd_dpipe_headers_show(dl);
cd1737
+	}
cd1737
+	pr_err("Command \"%s\" not found\n", dl_argv(dl));
cd1737
+	return -ENOENT;
cd1737
+}
cd1737
+
cd1737
+static const char
cd1737
+*dpipe_action_type_e2s(enum devlink_dpipe_action_type action_type)
cd1737
+{
cd1737
+	switch (action_type) {
cd1737
+	case DEVLINK_DPIPE_ACTION_TYPE_FIELD_MODIFY:
cd1737
+		return "field_modify";
cd1737
+	default:
cd1737
+		return "<unknown>";
cd1737
+	}
cd1737
+}
cd1737
+
cd1737
+static void pr_out_dpipe_action(struct dpipe_ctx *ctx,
cd1737
+				uint32_t header_id, uint32_t field_id,
cd1737
+				uint32_t action_type, bool global)
cd1737
+{
cd1737
+	const char *mapping;
cd1737
+
cd1737
+	pr_out_str(ctx->dl, "type", dpipe_action_type_e2s(action_type));
cd1737
+	pr_out_str(ctx->dl, "header", dpipe_header_id2s(ctx, header_id,
cd1737
+							global));
cd1737
+	pr_out_str(ctx->dl, "field", dpipe_field_id2s(ctx, header_id, field_id,
cd1737
+						      global));
cd1737
+	mapping = dpipe_mapping_get(ctx, header_id, field_id, global);
cd1737
+	if (mapping)
cd1737
+		pr_out_str(ctx->dl, "mapping", mapping);
cd1737
+}
cd1737
+
cd1737
+static int dpipe_action_show(struct dpipe_ctx *ctx, struct nlattr *nl)
cd1737
+{
cd1737
+	struct nlattr *nla_action[DEVLINK_ATTR_MAX + 1] = {};
cd1737
+	uint32_t header_id, field_id, action_type;
cd1737
+	bool global;
cd1737
+	int err;
cd1737
+
cd1737
+	err = mnl_attr_parse_nested(nl, attr_cb, nla_action);
cd1737
+	if (err != MNL_CB_OK)
cd1737
+		return -EINVAL;
cd1737
+
cd1737
+	if (!nla_action[DEVLINK_ATTR_DPIPE_ACTION_TYPE] ||
cd1737
+	    !nla_action[DEVLINK_ATTR_DPIPE_HEADER_INDEX] ||
cd1737
+	    !nla_action[DEVLINK_ATTR_DPIPE_HEADER_ID] ||
cd1737
+	    !nla_action[DEVLINK_ATTR_DPIPE_FIELD_ID]) {
cd1737
+		return -EINVAL;
cd1737
+	}
cd1737
+
cd1737
+	header_id = mnl_attr_get_u32(nla_action[DEVLINK_ATTR_DPIPE_HEADER_ID]);
cd1737
+	field_id = mnl_attr_get_u32(nla_action[DEVLINK_ATTR_DPIPE_FIELD_ID]);
cd1737
+	action_type = mnl_attr_get_u32(nla_action[DEVLINK_ATTR_DPIPE_ACTION_TYPE]);
cd1737
+	global = !!mnl_attr_get_u8(nla_action[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL]);
cd1737
+
cd1737
+	pr_out_dpipe_action(ctx, header_id, field_id, action_type, global);
cd1737
+	return 0;
cd1737
+}
cd1737
+
cd1737
+static int dpipe_table_actions_show(struct dpipe_ctx *ctx,
cd1737
+				    struct nlattr *nla_actions)
cd1737
+{
cd1737
+	struct nlattr *nla_action;
cd1737
+
cd1737
+	mnl_attr_for_each_nested(nla_action, nla_actions) {
cd1737
+		pr_out_entry_start(ctx->dl);
cd1737
+		if (dpipe_action_show(ctx, nla_action))
cd1737
+			goto err_action_show;
cd1737
+		pr_out_entry_end(ctx->dl);
cd1737
+	}
cd1737
+	return 0;
cd1737
+
cd1737
+err_action_show:
cd1737
+	pr_out_entry_end(ctx->dl);
cd1737
+	return -EINVAL;
cd1737
+}
cd1737
+
cd1737
+static const char *
cd1737
+dpipe_match_type_e2s(enum devlink_dpipe_match_type match_type)
cd1737
+{
cd1737
+	switch (match_type) {
cd1737
+	case DEVLINK_DPIPE_MATCH_TYPE_FIELD_EXACT:
cd1737
+		return "field_exact";
cd1737
+	default:
cd1737
+		return "<unknown>";
cd1737
+	}
cd1737
+}
cd1737
+
cd1737
+static void pr_out_dpipe_match(struct dpipe_ctx *ctx,
cd1737
+			       uint32_t header_id, uint32_t field_id,
cd1737
+			       uint32_t match_type, bool global)
cd1737
+{
cd1737
+	const char *mapping;
cd1737
+
cd1737
+	pr_out_str(ctx->dl, "type", dpipe_match_type_e2s(match_type));
cd1737
+	pr_out_str(ctx->dl, "header", dpipe_header_id2s(ctx, header_id,
cd1737
+							global));
cd1737
+	pr_out_str(ctx->dl, "field", dpipe_field_id2s(ctx, header_id, field_id,
cd1737
+						      global));
cd1737
+	mapping = dpipe_mapping_get(ctx, header_id, field_id, global);
cd1737
+	if (mapping)
cd1737
+		pr_out_str(ctx->dl, "mapping", mapping);
cd1737
+
cd1737
+}
cd1737
+
cd1737
+static int dpipe_match_show(struct dpipe_ctx *ctx, struct nlattr *nl)
cd1737
+{
cd1737
+	struct nlattr *nla_match[DEVLINK_ATTR_MAX + 1] = {};
cd1737
+	uint32_t header_id, field_id, match_type;
cd1737
+	bool global;
cd1737
+	int err;
cd1737
+
cd1737
+	err = mnl_attr_parse_nested(nl, attr_cb, nla_match);
cd1737
+	if (err != MNL_CB_OK)
cd1737
+		return -EINVAL;
cd1737
+
cd1737
+	if (!nla_match[DEVLINK_ATTR_DPIPE_MATCH_TYPE] ||
cd1737
+	    !nla_match[DEVLINK_ATTR_DPIPE_HEADER_INDEX] ||
cd1737
+	    !nla_match[DEVLINK_ATTR_DPIPE_HEADER_ID] ||
cd1737
+	    !nla_match[DEVLINK_ATTR_DPIPE_FIELD_ID]) {
cd1737
+		return -EINVAL;
cd1737
+	}
cd1737
+
cd1737
+	match_type = mnl_attr_get_u32(nla_match[DEVLINK_ATTR_DPIPE_MATCH_TYPE]);
cd1737
+	header_id = mnl_attr_get_u32(nla_match[DEVLINK_ATTR_DPIPE_HEADER_ID]);
cd1737
+	field_id = mnl_attr_get_u32(nla_match[DEVLINK_ATTR_DPIPE_FIELD_ID]);
cd1737
+	global = !!mnl_attr_get_u8(nla_match[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL]);
cd1737
+
cd1737
+	pr_out_dpipe_match(ctx, header_id, field_id, match_type, global);
cd1737
+	return 0;
cd1737
+}
cd1737
+
cd1737
+static int dpipe_table_matches_show(struct dpipe_ctx *ctx,
cd1737
+				    struct nlattr *nla_matches)
cd1737
+{
cd1737
+	struct nlattr *nla_match;
cd1737
+
cd1737
+	mnl_attr_for_each_nested(nla_match, nla_matches) {
cd1737
+		pr_out_entry_start(ctx->dl);
cd1737
+		if (dpipe_match_show(ctx, nla_match))
cd1737
+			goto err_match_show;
cd1737
+		pr_out_entry_end(ctx->dl);
cd1737
+	}
cd1737
+	return 0;
cd1737
+
cd1737
+err_match_show:
cd1737
+	pr_out_entry_end(ctx->dl);
cd1737
+	return -EINVAL;
cd1737
+}
cd1737
+
cd1737
+static int dpipe_table_show(struct dpipe_ctx *ctx, struct nlattr *nl)
cd1737
+{
cd1737
+	struct nlattr *nla_table[DEVLINK_ATTR_MAX + 1] = {};
cd1737
+	bool counters_enabled;
cd1737
+	const char *name;
cd1737
+	uint32_t size;
cd1737
+	int err;
cd1737
+
cd1737
+	err = mnl_attr_parse_nested(nl, attr_cb, nla_table);
cd1737
+	if (err != MNL_CB_OK)
cd1737
+		return -EINVAL;
cd1737
+
cd1737
+	if (!nla_table[DEVLINK_ATTR_DPIPE_TABLE_NAME] ||
cd1737
+	    !nla_table[DEVLINK_ATTR_DPIPE_TABLE_SIZE] ||
cd1737
+	    !nla_table[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS] ||
cd1737
+	    !nla_table[DEVLINK_ATTR_DPIPE_TABLE_MATCHES] ||
cd1737
+	    !nla_table[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED]) {
cd1737
+		return -EINVAL;
cd1737
+	}
cd1737
+
cd1737
+	name = mnl_attr_get_str(nla_table[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
cd1737
+	size = mnl_attr_get_u32(nla_table[DEVLINK_ATTR_DPIPE_TABLE_SIZE]);
cd1737
+	counters_enabled = !!mnl_attr_get_u8(nla_table[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED]);
cd1737
+
cd1737
+	pr_out_str(ctx->dl, "name", name);
cd1737
+	pr_out_uint(ctx->dl, "size", size);
cd1737
+	pr_out_str(ctx->dl, "counters_enabled",
cd1737
+		   counters_enabled ? "true" : "false");
cd1737
+
cd1737
+	pr_out_array_start(ctx->dl, "match");
cd1737
+	if (dpipe_table_matches_show(ctx, nla_table[DEVLINK_ATTR_DPIPE_TABLE_MATCHES]))
cd1737
+		goto err_matches_show;
cd1737
+	pr_out_array_end(ctx->dl);
cd1737
+
cd1737
+	pr_out_array_start(ctx->dl, "action");
cd1737
+	if (dpipe_table_actions_show(ctx, nla_table[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS]))
cd1737
+		goto err_actions_show;
cd1737
+	pr_out_array_end(ctx->dl);
cd1737
+
cd1737
+	return 0;
cd1737
+
cd1737
+err_actions_show:
cd1737
+err_matches_show:
cd1737
+	pr_out_array_end(ctx->dl);
cd1737
+	return -EINVAL;
cd1737
+}
cd1737
+
cd1737
+static int dpipe_tables_show(struct dpipe_ctx *ctx, struct nlattr **tb)
cd1737
+{
cd1737
+	struct nlattr *nla_tables = tb[DEVLINK_ATTR_DPIPE_TABLES];
cd1737
+	struct nlattr *nla_table;
cd1737
+
cd1737
+	mnl_attr_for_each_nested(nla_table, nla_tables) {
cd1737
+		pr_out_handle_start_arr(ctx->dl, tb);
cd1737
+		if (dpipe_table_show(ctx, nla_table))
cd1737
+			goto err_table_show;
cd1737
+		pr_out_handle_end(ctx->dl);
cd1737
+	}
cd1737
+	return 0;
cd1737
+
cd1737
+err_table_show:
cd1737
+	pr_out_handle_end(ctx->dl);
cd1737
+	return -EINVAL;
cd1737
+}
cd1737
+
cd1737
+static int cmd_dpipe_table_show_cb(const struct nlmsghdr *nlh, void *data)
cd1737
+{
cd1737
+	struct dpipe_ctx *ctx = data;
cd1737
+	struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
cd1737
+	struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
cd1737
+
cd1737
+	mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
cd1737
+	if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
cd1737
+	    !tb[DEVLINK_ATTR_DPIPE_TABLES])
cd1737
+		return MNL_CB_ERROR;
cd1737
+
cd1737
+	if (dpipe_tables_show(ctx, tb))
cd1737
+		return MNL_CB_ERROR;
cd1737
+	return MNL_CB_OK;
cd1737
+}
cd1737
+
cd1737
+static int cmd_dpipe_table_show(struct dl *dl)
cd1737
+{
cd1737
+	struct nlmsghdr *nlh;
cd1737
+	struct dpipe_ctx *ctx;
cd1737
+	uint16_t flags = NLM_F_REQUEST;
cd1737
+	int err;
cd1737
+
cd1737
+	ctx = dpipe_ctx_alloc(dl);
cd1737
+	if (!ctx)
cd1737
+		return -ENOMEM;
cd1737
+
cd1737
+	err = dl_argv_parse(dl, DL_OPT_HANDLE, DL_OPT_DPIPE_TABLE_NAME);
cd1737
+	if (err)
cd1737
+		goto out;
cd1737
+
cd1737
+	nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_DPIPE_HEADERS_GET, flags);
cd1737
+	dl_opts_put(nlh, dl);
cd1737
+	err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_dpipe_header_cb, ctx);
cd1737
+	if (err) {
cd1737
+		pr_err("error get headers %s\n", strerror(ctx->err));
cd1737
+		goto out;
cd1737
+	}
cd1737
+
cd1737
+	flags = NLM_F_REQUEST | NLM_F_ACK;
cd1737
+	nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_DPIPE_TABLE_GET, flags);
cd1737
+	dl_opts_put(nlh, dl);
cd1737
+
cd1737
+	pr_out_section_start(dl, "table");
cd1737
+	_mnlg_socket_sndrcv(dl->nlg, nlh, cmd_dpipe_table_show_cb, ctx);
cd1737
+	pr_out_section_end(dl);
cd1737
+out:
cd1737
+	dpipe_ctx_clear(ctx);
cd1737
+	dpipe_ctx_free(ctx);
cd1737
+	return err;
cd1737
+}
cd1737
+
cd1737
+static int cmd_dpipe_table_set(struct dl *dl)
cd1737
+{
cd1737
+	struct nlmsghdr *nlh;
cd1737
+	int err;
cd1737
+
cd1737
+	nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET,
cd1737
+			       NLM_F_REQUEST | NLM_F_ACK);
cd1737
+
cd1737
+	err = dl_argv_parse_put(nlh, dl,
cd1737
+				DL_OPT_HANDLE | DL_OPT_DPIPE_TABLE_NAME |
cd1737
+				DL_OPT_DPIPE_TABLE_COUNTERS, 0);
cd1737
+	if (err)
cd1737
+		return err;
cd1737
+
cd1737
+	return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL);
cd1737
+}
cd1737
+
cd1737
+static int dpipe_entry_value_show(struct dpipe_ctx *ctx,
cd1737
+				  struct nlattr **nla_match_value)
cd1737
+{
cd1737
+	uint16_t value_len;
cd1737
+	bool mask, mapping;
cd1737
+
cd1737
+	mask = !!nla_match_value[DEVLINK_ATTR_DPIPE_VALUE_MASK];
cd1737
+	mapping = !!nla_match_value[DEVLINK_ATTR_DPIPE_VALUE_MAPPING];
cd1737
+
cd1737
+	value_len = mnl_attr_get_payload_len(nla_match_value[DEVLINK_ATTR_DPIPE_VALUE]);
cd1737
+	if (value_len == sizeof(uint32_t)) {
cd1737
+		uint32_t value, value_mask, value_mapping;
cd1737
+
cd1737
+		if (mapping) {
cd1737
+			value_mapping = mnl_attr_get_u32(nla_match_value[DEVLINK_ATTR_DPIPE_VALUE_MAPPING]);
cd1737
+			pr_out_uint(ctx->dl, "mapping_value", value_mapping);
cd1737
+		}
cd1737
+
cd1737
+		if (mask) {
cd1737
+			value_mask = mnl_attr_get_u32(nla_match_value[DEVLINK_ATTR_DPIPE_VALUE_MASK]);
cd1737
+			pr_out_uint(ctx->dl, "mask_value", value_mask);
cd1737
+		}
cd1737
+
cd1737
+		value = mnl_attr_get_u32(nla_match_value[DEVLINK_ATTR_DPIPE_VALUE]);
cd1737
+		pr_out_uint(ctx->dl, "value", value);
cd1737
+
cd1737
+	} else {
cd1737
+		return -EINVAL;
cd1737
+	}
cd1737
+
cd1737
+	return 0;
cd1737
+}
cd1737
+
cd1737
+static int dpipe_entry_match_value_show(struct dpipe_ctx *ctx,
cd1737
+					struct nlattr *nl)
cd1737
+{
cd1737
+	struct nlattr *nla_match_value[DEVLINK_ATTR_MAX + 1] = {};
cd1737
+	int err;
cd1737
+
cd1737
+	err = mnl_attr_parse_nested(nl, attr_cb, nla_match_value);
cd1737
+	if (err != MNL_CB_OK)
cd1737
+		return -EINVAL;
cd1737
+
cd1737
+	if (!nla_match_value[DEVLINK_ATTR_DPIPE_MATCH] ||
cd1737
+	    !nla_match_value[DEVLINK_ATTR_DPIPE_VALUE]) {
cd1737
+		return -EINVAL;
cd1737
+	}
cd1737
+
cd1737
+	pr_out_entry_start(ctx->dl);
cd1737
+	if (dpipe_match_show(ctx, nla_match_value[DEVLINK_ATTR_DPIPE_MATCH]))
cd1737
+		goto err_match_show;
cd1737
+	if (dpipe_entry_value_show(ctx, nla_match_value))
cd1737
+		goto err_value_show;
cd1737
+	pr_out_entry_end(ctx->dl);
cd1737
+
cd1737
+	return 0;
cd1737
+
cd1737
+err_match_show:
cd1737
+err_value_show:
cd1737
+	pr_out_entry_end(ctx->dl);
cd1737
+	return -EINVAL;
cd1737
+}
cd1737
+
cd1737
+static int dpipe_entry_action_value_show(struct dpipe_ctx *ctx,
cd1737
+					 struct nlattr *nl)
cd1737
+{
cd1737
+	struct nlattr *nla_action_value[DEVLINK_ATTR_MAX + 1] = {};
cd1737
+	int err;
cd1737
+
cd1737
+	err = mnl_attr_parse_nested(nl, attr_cb, nla_action_value);
cd1737
+	if (err != MNL_CB_OK)
cd1737
+		return -EINVAL;
cd1737
+
cd1737
+	if (!nla_action_value[DEVLINK_ATTR_DPIPE_ACTION] ||
cd1737
+	    !nla_action_value[DEVLINK_ATTR_DPIPE_VALUE]) {
cd1737
+		return -EINVAL;
cd1737
+	}
cd1737
+
cd1737
+	pr_out_entry_start(ctx->dl);
cd1737
+	if (dpipe_action_show(ctx, nla_action_value[DEVLINK_ATTR_DPIPE_ACTION]))
cd1737
+		goto err_action_show;
cd1737
+	if (dpipe_entry_value_show(ctx, nla_action_value))
cd1737
+		goto err_value_show;
cd1737
+	pr_out_entry_end(ctx->dl);
cd1737
+
cd1737
+	return 0;
cd1737
+
cd1737
+err_action_show:
cd1737
+err_value_show:
cd1737
+	pr_out_entry_end(ctx->dl);
cd1737
+	return -EINVAL;
cd1737
+}
cd1737
+
cd1737
+static int
cd1737
+dpipe_tables_action_values_show(struct dpipe_ctx *ctx,
cd1737
+				struct nlattr *nla_action_values)
cd1737
+{
cd1737
+	struct nlattr *nla_action_value;
cd1737
+
cd1737
+	mnl_attr_for_each_nested(nla_action_value, nla_action_values) {
cd1737
+		if (dpipe_entry_action_value_show(ctx, nla_action_value))
cd1737
+			return -EINVAL;
cd1737
+	}
cd1737
+	return 0;
cd1737
+}
cd1737
+
cd1737
+static int
cd1737
+dpipe_tables_match_values_show(struct dpipe_ctx *ctx,
cd1737
+			       struct nlattr *nla_match_values)
cd1737
+{
cd1737
+	struct nlattr *nla_match_value;
cd1737
+
cd1737
+	mnl_attr_for_each_nested(nla_match_value, nla_match_values) {
cd1737
+		if (dpipe_entry_match_value_show(ctx, nla_match_value))
cd1737
+			return -EINVAL;
cd1737
+	}
cd1737
+	return 0;
cd1737
+}
cd1737
+
cd1737
+static int dpipe_entry_show(struct dpipe_ctx *ctx, struct nlattr *nl)
cd1737
+{
cd1737
+	struct nlattr *nla_entry[DEVLINK_ATTR_MAX + 1] = {};
cd1737
+	uint32_t entry_index;
cd1737
+	uint64_t counter;
cd1737
+	int err;
cd1737
+
cd1737
+	err = mnl_attr_parse_nested(nl, attr_cb, nla_entry);
cd1737
+	if (err != MNL_CB_OK)
cd1737
+		return -EINVAL;
cd1737
+
cd1737
+	if (!nla_entry[DEVLINK_ATTR_DPIPE_ENTRY_INDEX] ||
cd1737
+	    !nla_entry[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES] ||
cd1737
+	    !nla_entry[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES]) {
cd1737
+		return -EINVAL;
cd1737
+	}
cd1737
+
cd1737
+	entry_index = mnl_attr_get_u32(nla_entry[DEVLINK_ATTR_DPIPE_ENTRY_INDEX]);
cd1737
+	pr_out_uint(ctx->dl, "index", entry_index);
cd1737
+
cd1737
+	if (nla_entry[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER]) {
cd1737
+		counter = mnl_attr_get_u64(nla_entry[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER]);
cd1737
+		pr_out_uint(ctx->dl, "counter", counter);
cd1737
+	}
cd1737
+
cd1737
+	pr_out_array_start(ctx->dl, "match_value");
cd1737
+	if (dpipe_tables_match_values_show(ctx,
cd1737
+					   nla_entry[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES]))
cd1737
+		goto err_match_values_show;
cd1737
+	pr_out_array_end(ctx->dl);
cd1737
+
cd1737
+	pr_out_array_start(ctx->dl, "action_value");
cd1737
+	if (dpipe_tables_action_values_show(ctx,
cd1737
+					    nla_entry[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES]))
cd1737
+		goto err_action_values_show;
cd1737
+	pr_out_array_end(ctx->dl);
cd1737
+	return 0;
cd1737
+
cd1737
+err_action_values_show:
cd1737
+err_match_values_show:
cd1737
+	pr_out_array_end(ctx->dl);
cd1737
+	return -EINVAL;
cd1737
+}
cd1737
+
cd1737
+static int dpipe_table_entries_show(struct dpipe_ctx *ctx, struct nlattr **tb)
cd1737
+{
cd1737
+	struct nlattr *nla_entries = tb[DEVLINK_ATTR_DPIPE_ENTRIES];
cd1737
+	struct nlattr *nla_entry;
cd1737
+
cd1737
+	mnl_attr_for_each_nested(nla_entry, nla_entries) {
cd1737
+		pr_out_handle_start_arr(ctx->dl, tb);
cd1737
+		if (dpipe_entry_show(ctx, nla_entry))
cd1737
+			goto err_entry_show;
cd1737
+		pr_out_handle_end(ctx->dl);
cd1737
+	}
cd1737
+	return 0;
cd1737
+
cd1737
+err_entry_show:
cd1737
+	pr_out_handle_end(ctx->dl);
cd1737
+	return -EINVAL;
cd1737
+}
cd1737
+
cd1737
+static int cmd_dpipe_table_entry_dump_cb(const struct nlmsghdr *nlh, void *data)
cd1737
+{
cd1737
+	struct dpipe_ctx *ctx = data;
cd1737
+	struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
cd1737
+	struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
cd1737
+
cd1737
+	mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
cd1737
+	if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
cd1737
+	    !tb[DEVLINK_ATTR_DPIPE_ENTRIES])
cd1737
+		return MNL_CB_ERROR;
cd1737
+
cd1737
+	if (dpipe_table_entries_show(ctx, tb))
cd1737
+		return MNL_CB_ERROR;
cd1737
+	return MNL_CB_OK;
cd1737
+}
cd1737
+
cd1737
+static int cmd_dpipe_table_dump(struct dl *dl)
cd1737
+{
cd1737
+	struct nlmsghdr *nlh;
cd1737
+	struct dpipe_ctx *ctx;
cd1737
+	uint16_t flags = NLM_F_REQUEST;
cd1737
+	int err;
cd1737
+
cd1737
+	ctx = dpipe_ctx_alloc(dl);
cd1737
+	if (!ctx)
cd1737
+		return -ENOMEM;
cd1737
+
cd1737
+	err = dl_argv_parse(dl, DL_OPT_HANDLE | DL_OPT_DPIPE_TABLE_NAME, 0);
cd1737
+	if (err)
cd1737
+		goto out;
cd1737
+
cd1737
+	nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_DPIPE_HEADERS_GET, flags);
cd1737
+	dl_opts_put(nlh, dl);
cd1737
+	err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_dpipe_header_cb, ctx);
cd1737
+	if (err) {
cd1737
+		pr_err("error get headers %s\n", strerror(ctx->err));
cd1737
+		goto out;
cd1737
+	}
cd1737
+
cd1737
+	flags = NLM_F_REQUEST | NLM_F_ACK;
cd1737
+	nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_DPIPE_ENTRIES_GET, flags);
cd1737
+	dl_opts_put(nlh, dl);
cd1737
+
cd1737
+	pr_out_section_start(dl, "table_entry");
cd1737
+	_mnlg_socket_sndrcv(dl->nlg, nlh, cmd_dpipe_table_entry_dump_cb, ctx);
cd1737
+	pr_out_section_end(dl);
cd1737
+out:
cd1737
+	dpipe_ctx_clear(ctx);
cd1737
+	dpipe_ctx_free(ctx);
cd1737
+	return err;
cd1737
+}
cd1737
+
cd1737
+static void cmd_dpipe_table_help(void)
cd1737
+{
cd1737
+	pr_err("Usage: devlink dpipe table [ OBJECT-LIST ]\n"
cd1737
+	       "where  OBJECT-LIST := { show | set | dump }\n");
cd1737
+}
cd1737
+
cd1737
+static int cmd_dpipe_table(struct dl *dl)
cd1737
+{
cd1737
+	if (dl_argv_match(dl, "help") || dl_no_arg(dl)) {
cd1737
+		cmd_dpipe_table_help();
cd1737
+		return 0;
cd1737
+	} else if (dl_argv_match(dl, "show")) {
cd1737
+		dl_arg_inc(dl);
cd1737
+		return cmd_dpipe_table_show(dl);
cd1737
+	} else if (dl_argv_match(dl, "set")) {
cd1737
+		dl_arg_inc(dl);
cd1737
+		return cmd_dpipe_table_set(dl);
cd1737
+	}  else if (dl_argv_match(dl, "dump")) {
cd1737
+		dl_arg_inc(dl);
cd1737
+		return cmd_dpipe_table_dump(dl);
cd1737
+	}
cd1737
+	pr_err("Command \"%s\" not found\n", dl_argv(dl));
cd1737
+	return -ENOENT;
cd1737
+}
cd1737
+
cd1737
+static void cmd_dpipe_help(void)
cd1737
+{
cd1737
+	pr_err("Usage: devlink dpipe [ OBJECT-LIST ]\n"
cd1737
+	       "where  OBJECT-LIST := { header | table }\n");
cd1737
+}
cd1737
+
cd1737
+static int cmd_dpipe(struct dl *dl)
cd1737
+{
cd1737
+	if (dl_argv_match(dl, "help") || dl_no_arg(dl)) {
cd1737
+		cmd_dpipe_help();
cd1737
+		return 0;
cd1737
+	} else if (dl_argv_match(dl, "header")) {
cd1737
+		dl_arg_inc(dl);
cd1737
+		return cmd_dpipe_header(dl);
cd1737
+	} else if (dl_argv_match(dl, "table")) {
cd1737
+		dl_arg_inc(dl);
cd1737
+		return cmd_dpipe_table(dl);
cd1737
+	}
cd1737
+	pr_err("Command \"%s\" not found\n", dl_argv(dl));
cd1737
+	return -ENOENT;
cd1737
+}
cd1737
+
cd1737
+static void help(void)
cd1737
+{
cd1737
+	pr_err("Usage: devlink [ OPTIONS ] OBJECT { COMMAND | help }\n"
cd1737
+	       "where  OBJECT := { dev | port | sb | monitor | dpipe }\n"
cd1737
+	       "       OPTIONS := { -V[ersion] | -n[no-nice-names] | -j[json] | -p[pretty] | -v[verbose] }\n");
cd1737
+}
cd1737
+
cd1737
+static int dl_cmd(struct dl *dl)
cd1737
+{
cd1737
+	if (dl_argv_match(dl, "help") || dl_no_arg(dl)) {
cd1737
+		help();
cd1737
+		return 0;
cd1737
+	} else if (dl_argv_match(dl, "dev")) {
cd1737
+		dl_arg_inc(dl);
cd1737
+		return cmd_dev(dl);
cd1737
+	} else if (dl_argv_match(dl, "port")) {
cd1737
+		dl_arg_inc(dl);
cd1737
+		return cmd_port(dl);
cd1737
+	} else if (dl_argv_match(dl, "sb")) {
cd1737
+		dl_arg_inc(dl);
cd1737
+		return cmd_sb(dl);
cd1737
+	} else if (dl_argv_match(dl, "monitor")) {
cd1737
+		dl_arg_inc(dl);
cd1737
+		return cmd_mon(dl);
cd1737
+	} else if (dl_argv_match(dl, "dpipe")) {
cd1737
+		dl_arg_inc(dl);
cd1737
+		return cmd_dpipe(dl);
cd1737
+	}
cd1737
+	pr_err("Object \"%s\" not found\n", dl_argv(dl));
cd1737
+	return -ENOENT;
cd1737
+}
cd1737
+
cd1737
+static int dl_init(struct dl *dl, int argc, char **argv)
cd1737
+{
cd1737
+	int err;
cd1737
+
cd1737
+	dl->argc = argc;
cd1737
+	dl->argv = argv;
cd1737
+
cd1737
+	dl->nlg = mnlg_socket_open(DEVLINK_GENL_NAME, DEVLINK_GENL_VERSION);
cd1737
+	if (!dl->nlg) {
cd1737
+		pr_err("Failed to connect to devlink Netlink\n");
cd1737
+		return -errno;
cd1737
+	}
cd1737
+
cd1737
+	err = ifname_map_init(dl);
cd1737
+	if (err) {
cd1737
+		pr_err("Failed to create index map\n");
cd1737
+		goto err_ifname_map_create;
cd1737
+	}
cd1737
+	if (dl->json_output) {
cd1737
+		dl->jw = jsonw_new(stdout);
cd1737
+		if (!dl->jw) {
cd1737
+			pr_err("Failed to create JSON writer\n");
cd1737
+			goto err_json_new;
cd1737
+		}
cd1737
+		jsonw_pretty(dl->jw, dl->pretty_output);
cd1737
+	}
cd1737
+	return 0;
cd1737
+
cd1737
+err_json_new:
cd1737
+	ifname_map_fini(dl);
cd1737
+err_ifname_map_create:
cd1737
+	mnlg_socket_close(dl->nlg);
cd1737
+	return err;
cd1737
+}
cd1737
+
cd1737
+static void dl_fini(struct dl *dl)
cd1737
+{
cd1737
+	if (dl->json_output)
cd1737
+		jsonw_destroy(&dl->jw);
cd1737
+	ifname_map_fini(dl);
cd1737
+	mnlg_socket_close(dl->nlg);
cd1737
+}
cd1737
+
cd1737
+static struct dl *dl_alloc(void)
cd1737
+{
cd1737
+	struct dl *dl;
cd1737
+
cd1737
+	dl = calloc(1, sizeof(*dl));
cd1737
+	if (!dl)
cd1737
+		return NULL;
cd1737
+	return dl;
cd1737
+}
cd1737
+
cd1737
+static void dl_free(struct dl *dl)
cd1737
+{
cd1737
+	free(dl);
cd1737
+}
cd1737
+
cd1737
+int main(int argc, char **argv)
cd1737
+{
cd1737
+	static const struct option long_options[] = {
cd1737
+		{ "Version",		no_argument,		NULL, 'V' },
cd1737
+		{ "no-nice-names",	no_argument,		NULL, 'n' },
cd1737
+		{ "json",		no_argument,		NULL, 'j' },
cd1737
+		{ "pretty",		no_argument,		NULL, 'p' },
cd1737
+		{ "verbose",		no_argument,		NULL, 'v' },
cd1737
+		{ NULL, 0, NULL, 0 }
cd1737
+	};
cd1737
+	struct dl *dl;
cd1737
+	int opt;
cd1737
+	int err;
cd1737
+	int ret;
cd1737
+
cd1737
+	dl = dl_alloc();
cd1737
+	if (!dl) {
cd1737
+		pr_err("Failed to allocate memory for devlink\n");
cd1737
+		return EXIT_FAILURE;
cd1737
+	}
cd1737
+
cd1737
+	while ((opt = getopt_long(argc, argv, "Vnjpv",
cd1737
+				  long_options, NULL)) >= 0) {
cd1737
+
cd1737
+		switch (opt) {
cd1737
+		case 'V':
cd1737
+			printf("devlink utility, iproute2-ss%s\n", SNAPSHOT);
cd1737
+			ret = EXIT_SUCCESS;
cd1737
+			goto dl_free;
cd1737
+		case 'n':
cd1737
+			dl->no_nice_names = true;
cd1737
+			break;
cd1737
+		case 'j':
cd1737
+			dl->json_output = true;
cd1737
+			break;
cd1737
+		case 'p':
cd1737
+			dl->pretty_output = true;
cd1737
+			break;
cd1737
+		case 'v':
cd1737
+			dl->verbose = true;
cd1737
 			break;
cd1737
 		default:
cd1737
 			pr_err("Unknown option.\n");
cd1737
-- 
cd1737
1.8.3.1
cd1737