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

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