naccyde / rpms / iproute

Forked from rpms/iproute 9 months ago
Clone

Blame SOURCES/0132-tc-actions-add-helpers-to-parse-and-print-control-ac.patch

99be8f
From 23f1822fa8129326de4709d643f41cf26b6bae88 Mon Sep 17 00:00:00 2001
99be8f
From: Andrea Claudi <aclaudi@redhat.com>
99be8f
Date: Wed, 5 Jun 2019 13:09:39 +0200
99be8f
Subject: [PATCH] tc: actions: add helpers to parse and print control actions
99be8f
99be8f
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1714660
99be8f
Upstream Status: iproute2.git commit e67aba5595811
99be8f
Conflicts: context change due to out-of-order cherry-pick of
99be8f
           commit 73aa988868e7e ("tc/m_gact: Drop dead code")
99be8f
99be8f
commit e67aba559581143f9bc34f0706b0c3feeeab08fa
99be8f
Author: Jiri Pirko <jiri@mellanox.com>
99be8f
Date:   Tue May 16 19:29:36 2017 +0200
99be8f
99be8f
    tc: actions: add helpers to parse and print control actions
99be8f
99be8f
    Each tc action is terminated by a control action. Each action parses and
99be8f
    prints then intividually. Introduce set of helpers and allow to share
99be8f
    this code.
99be8f
99be8f
    Signed-off-by: Jiri Pirko <jiri@mellanox.com>
99be8f
---
99be8f
 tc/m_bpf.c        |   8 +--
99be8f
 tc/m_connmark.c   |   4 +-
99be8f
 tc/m_csum.c       |   9 ++-
99be8f
 tc/m_gact.c       |  45 ++++-----------
99be8f
 tc/m_ife.c        |  10 ++--
99be8f
 tc/m_mirred.c     |  10 ++--
99be8f
 tc/m_nat.c        |  11 ++--
99be8f
 tc/m_pedit.c      |   8 +--
99be8f
 tc/m_police.c     |  50 +++++------------
99be8f
 tc/m_sample.c     |   4 +-
99be8f
 tc/m_simple.c     |   3 -
99be8f
 tc/m_skbedit.c    |   7 +--
99be8f
 tc/m_skbmod.c     |  30 +---------
99be8f
 tc/m_tunnel_key.c |   8 +--
99be8f
 tc/m_vlan.c       |   9 ++-
99be8f
 tc/tc_util.c      | 137 +++++++++++++++++++++++++++++++++++++++++++++-
99be8f
 tc/tc_util.h      |  11 +++-
99be8f
 17 files changed, 209 insertions(+), 155 deletions(-)
99be8f
99be8f
diff --git a/tc/m_bpf.c b/tc/m_bpf.c
99be8f
index 1ddc334f2f21b..57283030a35f5 100644
99be8f
--- a/tc/m_bpf.c
99be8f
+++ b/tc/m_bpf.c
99be8f
@@ -75,7 +75,7 @@ static int bpf_parse_opt(struct action_util *a, int *ptr_argc, char ***ptr_argv,
99be8f
 			 int tca_id, struct nlmsghdr *n)
99be8f
 {
99be8f
 	const char *bpf_obj = NULL, *bpf_uds_name = NULL;
99be8f
-	struct tc_act_bpf parm = { .action = TC_ACT_PIPE };
99be8f
+	struct tc_act_bpf parm = {};
99be8f
 	struct bpf_cfg_in cfg = {};
99be8f
 	bool seen_run = false;
99be8f
 	struct rtattr *tail;
99be8f
@@ -123,8 +123,8 @@ opt_bpf:
99be8f
 		NEXT_ARG_FWD();
99be8f
 	}
99be8f
 
99be8f
-	if (argc && !action_a2n(*argv, &parm.action, false))
99be8f
-		NEXT_ARG_FWD();
99be8f
+	parse_action_control_dflt(&argc, &argv, &parm.action,
99be8f
+				  false, TC_ACT_PIPE);
99be8f
 
99be8f
 	if (argc) {
99be8f
 		if (matches(*argv, "index") == 0) {
99be8f
@@ -186,7 +186,7 @@ static int bpf_print_opt(struct action_util *au, FILE *f, struct rtattr *arg)
99be8f
 				      b, sizeof(b)));
99be8f
 	}
99be8f
 
99be8f
-	fprintf(f, "default-action %s\n", action_n2a(parm->action));
99be8f
+	print_action_control(f, "default-action ", parm->action, "\n");
99be8f
 	fprintf(f, "\tindex %u ref %d bind %d", parm->index, parm->refcnt,
99be8f
 		parm->bindcnt);
99be8f
 
99be8f
diff --git a/tc/m_connmark.c b/tc/m_connmark.c
99be8f
index 295f90d52eefd..3c2274bc0d2af 100644
99be8f
--- a/tc/m_connmark.c
99be8f
+++ b/tc/m_connmark.c
99be8f
@@ -80,9 +80,7 @@ parse_connmark(struct action_util *a, int *argc_p, char ***argv_p, int tca_id,
99be8f
 		}
99be8f
 	}
99be8f
 
99be8f
-	sel.action = TC_ACT_PIPE;
99be8f
-	if (argc && !action_a2n(*argv, &sel.action, false))
99be8f
-		NEXT_ARG_FWD();
99be8f
+	parse_action_control_dflt(&argc, &argv, &sel.action, false, TC_ACT_PIPE);
99be8f
 
99be8f
 	if (argc) {
99be8f
 		if (matches(*argv, "index") == 0) {
99be8f
diff --git a/tc/m_csum.c b/tc/m_csum.c
99be8f
index 0ee8cad3fbe4c..7b156734f64c5 100644
99be8f
--- a/tc/m_csum.c
99be8f
+++ b/tc/m_csum.c
99be8f
@@ -123,8 +123,7 @@ parse_csum(struct action_util *a, int *argc_p,
99be8f
 		return -1;
99be8f
 	}
99be8f
 
99be8f
-	if (argc && !action_a2n(*argv, &sel.action, false))
99be8f
-		NEXT_ARG_FWD();
99be8f
+	parse_action_control_dflt(&argc, &argv, &sel.action, false, TC_ACT_OK);
99be8f
 
99be8f
 	if (argc) {
99be8f
 		if (matches(*argv, "index") == 0) {
99be8f
@@ -200,10 +199,10 @@ print_csum(struct action_util *au, FILE *f, struct rtattr *arg)
99be8f
 		uflag_1 = "?empty";
99be8f
 	}
99be8f
 
99be8f
-	fprintf(f, "csum (%s%s%s%s%s%s%s) action %s\n",
99be8f
+	fprintf(f, "csum (%s%s%s%s%s%s%s) ",
99be8f
 		uflag_1, uflag_2, uflag_3,
99be8f
-		uflag_4, uflag_5, uflag_6, uflag_7,
99be8f
-		action_n2a(sel->action));
99be8f
+		uflag_4, uflag_5, uflag_6, uflag_7);
99be8f
+	print_action_control(f, "action ", sel->action, "\n");
99be8f
 	fprintf(f, "\tindex %u ref %d bind %d", sel->index, sel->refcnt,
99be8f
 		sel->bindcnt);
99be8f
 
99be8f
diff --git a/tc/m_gact.c b/tc/m_gact.c
99be8f
index 0cb5222fd3817..bc3860bbe4441 100644
99be8f
--- a/tc/m_gact.c
99be8f
+++ b/tc/m_gact.c
99be8f
@@ -68,26 +68,13 @@ usage(void)
99be8f
 	exit(-1);
99be8f
 }
99be8f
 
99be8f
-static int
99be8f
-get_act(char ***argv_p)
99be8f
-{
99be8f
-	int n;
99be8f
-
99be8f
-	if (action_a2n(**argv_p, &n, false)) {
99be8f
-		fprintf(stderr, "bad action type %s\n", **argv_p);
99be8f
-		return -10;
99be8f
-	}
99be8f
-	return n;
99be8f
-}
99be8f
-
99be8f
 static int
99be8f
 parse_gact(struct action_util *a, int *argc_p, char ***argv_p,
99be8f
 	   int tca_id, struct nlmsghdr *n)
99be8f
 {
99be8f
 	int argc = *argc_p;
99be8f
 	char **argv = *argv_p;
99be8f
-	int action = TC_POLICE_RECLASSIFY;
99be8f
-	struct tc_gact p = { .action = TC_POLICE_RECLASSIFY };
99be8f
+	struct tc_gact p = { 0 };
99be8f
 #ifdef CONFIG_GACT_PROB
99be8f
 	int rd = 0;
99be8f
 	struct tc_gact_p pp;
99be8f
@@ -101,16 +88,8 @@ parse_gact(struct action_util *a, int *argc_p, char ***argv_p,
99be8f
 	if (matches(*argv, "gact") == 0) {
99be8f
 		argc--;
99be8f
 		argv++;
99be8f
-	} else {
99be8f
-		action = get_act(&argv);
99be8f
-		if (action != -10) {
99be8f
-			p.action = action;
99be8f
-			argc--;
99be8f
-			argv++;
99be8f
-		} else {
99be8f
-			explain();
99be8f
-			return action;
99be8f
-		}
99be8f
+	} else if (parse_action_control(&argc, &argv, &p.action, false) == -1) {
99be8f
+		usage();
99be8f
 	}
99be8f
 
99be8f
 #ifdef CONFIG_GACT_PROB
99be8f
@@ -129,13 +108,9 @@ parse_gact(struct action_util *a, int *argc_p, char ***argv_p,
99be8f
 				return -1;
99be8f
 			}
99be8f
 
99be8f
-			action = get_act(&argv);
99be8f
-			if (action != -10) { /* FIXME */
99be8f
-				pp.paction = action;
99be8f
-			} else {
99be8f
-				explain();
99be8f
-				return -1;
99be8f
-			}
99be8f
+			if (parse_action_control(&argc, &argv,
99be8f
+						 &pp.paction, false) == -1)
99be8f
+				usage();
99be8f
 			argc--;
99be8f
 			argv++;
99be8f
 			if (get_u16(&pp.pval, *argv, 10)) {
99be8f
@@ -204,7 +179,8 @@ print_gact(struct action_util *au, FILE * f, struct rtattr *arg)
99be8f
 	}
99be8f
 	p = RTA_DATA(tb[TCA_GACT_PARMS]);
99be8f
 
99be8f
-	fprintf(f, "gact action %s", action_n2a(p->action));
99be8f
+	fprintf(f, "gact ");
99be8f
+	print_action_control(f, "action ", p->action, "");
99be8f
 #ifdef CONFIG_GACT_PROB
99be8f
 	if (tb[TCA_GACT_PROB] != NULL) {
99be8f
 		pp = RTA_DATA(tb[TCA_GACT_PROB]);
99be8f
@@ -213,8 +189,9 @@ print_gact(struct action_util *au, FILE * f, struct rtattr *arg)
99be8f
 		memset(&pp_dummy, 0, sizeof(pp_dummy));
99be8f
 		pp = &pp_dummy;
99be8f
 	}
99be8f
-	fprintf(f, "\n\t random type %s %s val %d",
99be8f
-		prob_n2a(pp->ptype), action_n2a(pp->paction), pp->pval);
99be8f
+	fprintf(f, "\n\t random type %s", prob_n2a(pp->ptype));
99be8f
+	print_action_control(f, " ", pp->paction, " ");
99be8f
+	fprintf(f, "val %d", pp->pval);
99be8f
 #endif
99be8f
 	fprintf(f, "\n\t index %u ref %d bind %d", p->index, p->refcnt,
99be8f
 		p->bindcnt);
99be8f
diff --git a/tc/m_ife.c b/tc/m_ife.c
99be8f
index f6131b1332324..e3521e62c178c 100644
99be8f
--- a/tc/m_ife.c
99be8f
+++ b/tc/m_ife.c
99be8f
@@ -57,7 +57,7 @@ static int parse_ife(struct action_util *a, int *argc_p, char ***argv_p,
99be8f
 	int argc = *argc_p;
99be8f
 	char **argv = *argv_p;
99be8f
 	int ok = 0;
99be8f
-	struct tc_ife p = { .action = TC_ACT_PIPE };	/* good default */
99be8f
+	struct tc_ife p = { 0 };
99be8f
 	struct rtattr *tail;
99be8f
 	struct rtattr *tail2;
99be8f
 	char dbuf[ETH_ALEN];
99be8f
@@ -156,8 +156,7 @@ static int parse_ife(struct action_util *a, int *argc_p, char ***argv_p,
99be8f
 		argv++;
99be8f
 	}
99be8f
 
99be8f
-	if (argc && !action_a2n(*argv, &p.action, false))
99be8f
-		NEXT_ARG_FWD();
99be8f
+	parse_action_control_dflt(&argc, &argv, &p.action, false, TC_ACT_PIPE);
99be8f
 
99be8f
 	if (argc) {
99be8f
 		if (matches(*argv, "index") == 0) {
99be8f
@@ -245,9 +244,8 @@ static int print_ife(struct action_util *au, FILE *f, struct rtattr *arg)
99be8f
 	}
99be8f
 	p = RTA_DATA(tb[TCA_IFE_PARMS]);
99be8f
 
99be8f
-	fprintf(f, "ife %s action %s ",
99be8f
-		(p->flags & IFE_ENCODE) ? "encode" : "decode",
99be8f
-		action_n2a(p->action));
99be8f
+	fprintf(f, "ife %s ", p->flags & IFE_ENCODE ? "encode" : "decode");
99be8f
+	print_action_control(f, "action ", p->action, " ");
99be8f
 
99be8f
 	if (tb[TCA_IFE_TYPE]) {
99be8f
 		ife_type = rta_getattr_u16(tb[TCA_IFE_TYPE]);
99be8f
diff --git a/tc/m_mirred.c b/tc/m_mirred.c
99be8f
index e9438904fdf50..2384bda1ff045 100644
99be8f
--- a/tc/m_mirred.c
99be8f
+++ b/tc/m_mirred.c
99be8f
@@ -170,10 +170,8 @@ parse_direction(struct action_util *a, int *argc_p, char ***argv_p,
99be8f
 	}
99be8f
 
99be8f
 
99be8f
-	if (argc &&
99be8f
-	    (p.eaction == TCA_EGRESS_MIRROR || p.eaction == TCA_INGRESS_MIRROR)
99be8f
-	    && !action_a2n(*argv, &p.action, false))
99be8f
-		NEXT_ARG();
99be8f
+	if (p.eaction == TCA_EGRESS_MIRROR || p.eaction == TCA_INGRESS_MIRROR)
99be8f
+		parse_action_control(&argc, &argv, &p.action, false);
99be8f
 
99be8f
 	if (argc) {
99be8f
 		if (iok && matches(*argv, "index") == 0) {
99be8f
@@ -272,8 +270,8 @@ print_mirred(struct action_util *au, FILE * f, struct rtattr *arg)
99be8f
 		return -1;
99be8f
 	}
99be8f
 
99be8f
-	fprintf(f, "mirred (%s to device %s) %s",
99be8f
-		mirred_n2a(p->eaction), dev, action_n2a(p->action));
99be8f
+	fprintf(f, "mirred (%s to device %s)", mirred_n2a(p->eaction), dev);
99be8f
+	print_action_control(f, " ", p->action, "");
99be8f
 
99be8f
 	fprintf(f, "\n ");
99be8f
 	fprintf(f, "\tindex %u ref %d bind %d", p->index, p->refcnt,
99be8f
diff --git a/tc/m_nat.c b/tc/m_nat.c
99be8f
index 525f185e2c082..31b68fb6bd784 100644
99be8f
--- a/tc/m_nat.c
99be8f
+++ b/tc/m_nat.c
99be8f
@@ -115,8 +115,7 @@ parse_nat(struct action_util *a, int *argc_p, char ***argv_p, int tca_id, struct
99be8f
 		return -1;
99be8f
 	}
99be8f
 
99be8f
-	if (argc && !action_a2n(*argv, &sel.action, false))
99be8f
-		NEXT_ARG_FWD();
99be8f
+	parse_action_control_dflt(&argc, &argv, &sel.action, false, TC_ACT_OK);
99be8f
 
99be8f
 	if (argc) {
99be8f
 		if (matches(*argv, "index") == 0) {
99be8f
@@ -164,12 +163,12 @@ print_nat(struct action_util *au, FILE * f, struct rtattr *arg)
99be8f
 	len = ffs(sel->mask);
99be8f
 	len = len ? 33 - len : 0;
99be8f
 
99be8f
-	fprintf(f, " nat %s %s/%d %s %s", sel->flags & TCA_NAT_FLAG_EGRESS ?
99be8f
-					  "egress" : "ingress",
99be8f
+	fprintf(f, " nat %s %s/%d %s", sel->flags & TCA_NAT_FLAG_EGRESS ?
99be8f
+				       "egress" : "ingress",
99be8f
 		format_host_r(AF_INET, 4, &sel->old_addr, buf1, sizeof(buf1)),
99be8f
 		len,
99be8f
-		format_host_r(AF_INET, 4, &sel->new_addr, buf2, sizeof(buf2)),
99be8f
-		action_n2a(sel->action));
99be8f
+		format_host_r(AF_INET, 4, &sel->new_addr, buf2, sizeof(buf2)));
99be8f
+	print_action_control(f, " ", sel->action, "");
99be8f
 
99be8f
 	if (show_stats) {
99be8f
 		if (tb[TCA_NAT_TM]) {
99be8f
diff --git a/tc/m_pedit.c b/tc/m_pedit.c
99be8f
index dfa6b2c4835e9..b7d26b4540beb 100644
99be8f
--- a/tc/m_pedit.c
99be8f
+++ b/tc/m_pedit.c
99be8f
@@ -670,8 +670,7 @@ int parse_pedit(struct action_util *a, int *argc_p, char ***argv_p, int tca_id,
99be8f
 		return -1;
99be8f
 	}
99be8f
 
99be8f
-	if (argc && !action_a2n(*argv, &sel.sel.action, false))
99be8f
-		NEXT_ARG();
99be8f
+	parse_action_control_dflt(&argc, &argv, &sel.sel.action, false, TC_ACT_OK);
99be8f
 
99be8f
 	if (argc) {
99be8f
 		if (matches(*argv, "index") == 0) {
99be8f
@@ -776,8 +775,9 @@ int print_pedit(struct action_util *au, FILE *f, struct rtattr *arg)
99be8f
 		}
99be8f
 	}
99be8f
 
99be8f
-	fprintf(f, " pedit action %s keys %d\n ",
99be8f
-		action_n2a(sel->action), sel->nkeys);
99be8f
+	fprintf(f, " pedit ");
99be8f
+	print_action_control(f, "action ", sel->action, " ");
99be8f
+	fprintf(f,"keys %d\n ", sel->nkeys);
99be8f
 	fprintf(f, "\t index %u ref %d bind %d", sel->index, sel->refcnt,
99be8f
 		sel->bindcnt);
99be8f
 
99be8f
diff --git a/tc/m_police.c b/tc/m_police.c
99be8f
index 226e20e4e8005..2b73969de5daf 100644
99be8f
--- a/tc/m_police.c
99be8f
+++ b/tc/m_police.c
99be8f
@@ -50,27 +50,6 @@ static void explain1(char *arg)
99be8f
 	fprintf(stderr, "Illegal \"%s\"\n", arg);
99be8f
 }
99be8f
 
99be8f
-static int get_police_result(int *action, int *result, char *arg)
99be8f
-{
99be8f
-	char *p = strchr(arg, '/');
99be8f
-
99be8f
-	if (p)
99be8f
-		*p = 0;
99be8f
-
99be8f
-	if (action_a2n(arg, action, true)) {
99be8f
-		if (p)
99be8f
-			*p = '/';
99be8f
-		return -1;
99be8f
-	}
99be8f
-
99be8f
-	if (p) {
99be8f
-		*p = '/';
99be8f
-		if (action_a2n(p+1, result, true))
99be8f
-			return -1;
99be8f
-	}
99be8f
-	return 0;
99be8f
-}
99be8f
-
99be8f
 int act_parse_police(struct action_util *a, int *argc_p, char ***argv_p,
99be8f
 		     int tca_id, struct nlmsghdr *n)
99be8f
 {
99be8f
@@ -166,23 +145,19 @@ int act_parse_police(struct action_util *a, int *argc_p, char ***argv_p,
99be8f
 				explain1("peakrate");
99be8f
 				return -1;
99be8f
 			}
99be8f
-		} else if (matches(*argv, "reclassify") == 0) {
99be8f
-			p.action = TC_POLICE_RECLASSIFY;
99be8f
-		} else if (matches(*argv, "drop") == 0 ||
99be8f
-			   matches(*argv, "shot") == 0) {
99be8f
-			p.action = TC_POLICE_SHOT;
99be8f
-		} else if (matches(*argv, "continue") == 0) {
99be8f
-			p.action = TC_POLICE_UNSPEC;
99be8f
-		} else if (matches(*argv, "pass") == 0) {
99be8f
-			p.action = TC_POLICE_OK;
99be8f
-		} else if (matches(*argv, "pipe") == 0) {
99be8f
-			p.action = TC_POLICE_PIPE;
99be8f
+		} else if (matches(*argv, "reclassify") == 0 ||
99be8f
+			   matches(*argv, "drop") == 0 ||
99be8f
+			   matches(*argv, "shot") == 0 ||
99be8f
+			   matches(*argv, "continue") == 0 ||
99be8f
+			   matches(*argv, "pass") == 0 ||
99be8f
+			   matches(*argv, "pipe") == 0) {
99be8f
+			if (parse_action_control(&argc, &argv, &p.action, false))
99be8f
+				return -1;
99be8f
 		} else if (strcmp(*argv, "conform-exceed") == 0) {
99be8f
 			NEXT_ARG();
99be8f
-			if (get_police_result(&p.action, &presult, *argv)) {
99be8f
-				fprintf(stderr, "Illegal \"action\"\n");
99be8f
+			if (parse_action_control_slash(&argc, &argv, &p.action,
99be8f
+						       &presult, true))
99be8f
 				return -1;
99be8f
-			}
99be8f
 		} else if (matches(*argv, "overhead") == 0) {
99be8f
 			NEXT_ARG();
99be8f
 			if (get_u16(&overhead, *argv, 10)) {
99be8f
@@ -318,12 +293,13 @@ int print_police(struct action_util *a, FILE *f, struct rtattr *arg)
99be8f
 		fprintf(f, "avrate %s ",
99be8f
 			sprint_rate(rta_getattr_u32(tb[TCA_POLICE_AVRATE]),
99be8f
 				    b1));
99be8f
-	fprintf(f, "action %s", action_n2a(p->action));
99be8f
+
99be8f
+	print_action_control(f, "action ", p->action, "");
99be8f
 
99be8f
 	if (tb[TCA_POLICE_RESULT]) {
99be8f
 		__u32 action = rta_getattr_u32(tb[TCA_POLICE_RESULT]);
99be8f
 
99be8f
-		fprintf(f, "/%s ", action_n2a(action));
99be8f
+		print_action_control(f, "/", action, " ");
99be8f
 	} else
99be8f
 		fprintf(f, " ");
99be8f
 
99be8f
diff --git a/tc/m_sample.c b/tc/m_sample.c
99be8f
index 9291109071a89..ff5ee6bd1ef63 100644
99be8f
--- a/tc/m_sample.c
99be8f
+++ b/tc/m_sample.c
99be8f
@@ -98,9 +98,7 @@ static int parse_sample(struct action_util *a, int *argc_p, char ***argv_p,
99be8f
 		NEXT_ARG_FWD();
99be8f
 	}
99be8f
 
99be8f
-	p.action = TC_ACT_PIPE;
99be8f
-	if (argc && !action_a2n(*argv, &p.action, false))
99be8f
-		NEXT_ARG_FWD();
99be8f
+	parse_action_control_dflt(&argc, &argv, &p.action, false, TC_ACT_PIPE);
99be8f
 
99be8f
 	if (argc) {
99be8f
 		if (matches(*argv, "index") == 0) {
99be8f
diff --git a/tc/m_simple.c b/tc/m_simple.c
99be8f
index 65e48addf161b..f8937bcabb7ae 100644
99be8f
--- a/tc/m_simple.c
99be8f
+++ b/tc/m_simple.c
99be8f
@@ -120,9 +120,6 @@ parse_simple(struct action_util *a, int *argc_p, char ***argv_p, int tca_id,
99be8f
 		}
99be8f
 	}
99be8f
 
99be8f
-	if (argc && !action_a2n(*argv, &sel.action, false))
99be8f
-		NEXT_ARG_FWD();
99be8f
-
99be8f
 	if (argc) {
99be8f
 		if (matches(*argv, "index") == 0) {
99be8f
 			NEXT_ARG();
99be8f
diff --git a/tc/m_skbedit.c b/tc/m_skbedit.c
99be8f
index 638715f679d37..aa374fcb33ed9 100644
99be8f
--- a/tc/m_skbedit.c
99be8f
+++ b/tc/m_skbedit.c
99be8f
@@ -120,9 +120,8 @@ parse_skbedit(struct action_util *a, int *argc_p, char ***argv_p, int tca_id,
99be8f
 		argv++;
99be8f
 	}
99be8f
 
99be8f
-	sel.action = TC_ACT_PIPE;
99be8f
-	if (argc && !action_a2n(*argv, &sel.action, false))
99be8f
-		NEXT_ARG();
99be8f
+	parse_action_control_dflt(&argc, &argv, &sel.action,
99be8f
+				  false, TC_ACT_PIPE);
99be8f
 
99be8f
 	if (argc) {
99be8f
 		if (matches(*argv, "index") == 0) {
99be8f
@@ -214,7 +213,7 @@ static int print_skbedit(struct action_util *au, FILE *f, struct rtattr *arg)
99be8f
 			fprintf(f, " ptype %d", *ptype);
99be8f
 	}
99be8f
 
99be8f
-	fprintf(f, " %s", action_n2a(p->action));
99be8f
+	print_action_control(f, " ", p->action, "");
99be8f
 
99be8f
 	fprintf(f, "\n\t index %u ref %d bind %d",
99be8f
 		p->index, p->refcnt, p->bindcnt);
99be8f
diff --git a/tc/m_skbmod.c b/tc/m_skbmod.c
99be8f
index acb7771d2901b..1ccd474309348 100644
99be8f
--- a/tc/m_skbmod.c
99be8f
+++ b/tc/m_skbmod.c
99be8f
@@ -61,7 +61,6 @@ static int parse_skbmod(struct action_util *a, int *argc_p, char ***argv_p,
99be8f
 	char *saddr = NULL;
99be8f
 
99be8f
 	memset(&p, 0, sizeof(p));
99be8f
-	p.action = TC_ACT_PIPE;	/* good default */
99be8f
 
99be8f
 	if (argc <= 0)
99be8f
 		return -1;
99be8f
@@ -123,31 +122,7 @@ static int parse_skbmod(struct action_util *a, int *argc_p, char ***argv_p,
99be8f
 		argv++;
99be8f
 	}
99be8f
 
99be8f
-	if (argc) {
99be8f
-		if (matches(*argv, "reclassify") == 0) {
99be8f
-			p.action = TC_ACT_RECLASSIFY;
99be8f
-			argc--;
99be8f
-			argv++;
99be8f
-		} else if (matches(*argv, "pipe") == 0) {
99be8f
-			p.action = TC_ACT_PIPE;
99be8f
-			argc--;
99be8f
-			argv++;
99be8f
-		} else if (matches(*argv, "drop") == 0 ||
99be8f
-			   matches(*argv, "shot") == 0) {
99be8f
-			p.action = TC_ACT_SHOT;
99be8f
-			argc--;
99be8f
-			argv++;
99be8f
-		} else if (matches(*argv, "continue") == 0) {
99be8f
-			p.action = TC_ACT_UNSPEC;
99be8f
-			argc--;
99be8f
-			argv++;
99be8f
-		} else if (matches(*argv, "pass") == 0 ||
99be8f
-			   matches(*argv, "ok") == 0) {
99be8f
-			p.action = TC_ACT_OK;
99be8f
-			argc--;
99be8f
-			argv++;
99be8f
-		}
99be8f
-	}
99be8f
+	parse_action_control_dflt(&argc, &argv, &p.action, false, TC_ACT_PIPE);
99be8f
 
99be8f
 	if (argc) {
99be8f
 		if (matches(*argv, "index") == 0) {
99be8f
@@ -206,7 +181,8 @@ static int print_skbmod(struct action_util *au, FILE *f, struct rtattr *arg)
99be8f
 
99be8f
 	p = RTA_DATA(tb[TCA_SKBMOD_PARMS]);
99be8f
 
99be8f
-	fprintf(f, "skbmod action %s ", action_n2a(p->action));
99be8f
+	fprintf(f, "skbmod ");
99be8f
+	print_action_control(f, "", p->action, " ");
99be8f
 
99be8f
 	if (tb[TCA_SKBMOD_ETYPE]) {
99be8f
 		skbmod_etype = rta_getattr_u16(tb[TCA_SKBMOD_ETYPE]);
99be8f
diff --git a/tc/m_tunnel_key.c b/tc/m_tunnel_key.c
99be8f
index 60fd1c464e531..cdde64a15b929 100644
99be8f
--- a/tc/m_tunnel_key.c
99be8f
+++ b/tc/m_tunnel_key.c
99be8f
@@ -99,7 +99,7 @@ static int tunnel_key_parse_tos_ttl(char *str, int type, struct nlmsghdr *n)
99be8f
 static int parse_tunnel_key(struct action_util *a, int *argc_p, char ***argv_p,
99be8f
 			    int tca_id, struct nlmsghdr *n)
99be8f
 {
99be8f
-	struct tc_tunnel_key parm = { .action = TC_ACT_PIPE };
99be8f
+	struct tc_tunnel_key parm = {};
99be8f
 	char **argv = *argv_p;
99be8f
 	int argc = *argc_p;
99be8f
 	struct rtattr *tail;
99be8f
@@ -194,8 +194,8 @@ static int parse_tunnel_key(struct action_util *a, int *argc_p, char ***argv_p,
99be8f
 		NEXT_ARG_FWD();
99be8f
 	}
99be8f
 
99be8f
-	if (argc && !action_a2n(*argv, &parm.action, false))
99be8f
-		NEXT_ARG_FWD();
99be8f
+	parse_action_control_dflt(&argc, &argv, &parm.action,
99be8f
+				  false, TC_ACT_PIPE);
99be8f
 
99be8f
 	if (argc) {
99be8f
 		if (matches(*argv, "index") == 0) {
99be8f
@@ -318,7 +318,7 @@ static int print_tunnel_key(struct action_util *au, FILE *f, struct rtattr *arg)
99be8f
 					  tb[TCA_TUNNEL_KEY_ENC_TTL]);
99be8f
 		break;
99be8f
 	}
99be8f
-	fprintf(f, " %s", action_n2a(parm->action));
99be8f
+	print_action_control(f, " ", parm->action, "");
99be8f
 
99be8f
 	fprintf(f, "\n\tindex %d ref %d bind %d", parm->index, parm->refcnt,
99be8f
 		parm->bindcnt);
99be8f
diff --git a/tc/m_vlan.c b/tc/m_vlan.c
99be8f
index 44b9375966da3..2441b06847ecd 100644
99be8f
--- a/tc/m_vlan.c
99be8f
+++ b/tc/m_vlan.c
99be8f
@@ -59,7 +59,7 @@ static int parse_vlan(struct action_util *a, int *argc_p, char ***argv_p,
99be8f
 	int proto_set = 0;
99be8f
 	__u8 prio;
99be8f
 	int prio_set = 0;
99be8f
-	struct tc_vlan parm = { 0 };
99be8f
+	struct tc_vlan parm = {};
99be8f
 
99be8f
 	if (matches(*argv, "vlan") != 0)
99be8f
 		return -1;
99be8f
@@ -133,9 +133,8 @@ static int parse_vlan(struct action_util *a, int *argc_p, char ***argv_p,
99be8f
 		argv++;
99be8f
 	}
99be8f
 
99be8f
-	parm.action = TC_ACT_PIPE;
99be8f
-	if (argc && !action_a2n(*argv, &parm.action, false))
99be8f
-		NEXT_ARG_FWD();
99be8f
+	parse_action_control_dflt(&argc, &argv, &parm.action,
99be8f
+				  false, TC_ACT_PIPE);
99be8f
 
99be8f
 	if (argc) {
99be8f
 		if (matches(*argv, "index") == 0) {
99be8f
@@ -224,7 +223,7 @@ static int print_vlan(struct action_util *au, FILE *f, struct rtattr *arg)
99be8f
 		}
99be8f
 		break;
99be8f
 	}
99be8f
-	fprintf(f, " %s", action_n2a(parm->action));
99be8f
+	print_action_control(f, " ", parm->action, "");
99be8f
 
99be8f
 	fprintf(f, "\n\t index %u ref %d bind %d", parm->index, parm->refcnt,
99be8f
 		parm->bindcnt);
99be8f
diff --git a/tc/tc_util.c b/tc/tc_util.c
99be8f
index 296825ae174e0..840222832690b 100644
99be8f
--- a/tc/tc_util.c
99be8f
+++ b/tc/tc_util.c
99be8f
@@ -411,7 +411,7 @@ char *sprint_qdisc_handle(__u32 h, char *buf)
99be8f
 	return buf;
99be8f
 }
99be8f
 
99be8f
-const char *action_n2a(int action)
99be8f
+static const char *action_n2a(int action)
99be8f
 {
99be8f
 	static char buf[64];
99be8f
 
99be8f
@@ -443,7 +443,7 @@ const char *action_n2a(int action)
99be8f
  *
99be8f
  * In error case, returns -1 and does not touch @result. Otherwise returns 0.
99be8f
  */
99be8f
-int action_a2n(char *arg, int *result, bool allow_num)
99be8f
+static int action_a2n(char *arg, int *result, bool allow_num)
99be8f
 {
99be8f
 	int n;
99be8f
 	char dummy;
99be8f
@@ -474,6 +474,139 @@ int action_a2n(char *arg, int *result, bool allow_num)
99be8f
 	return 0;
99be8f
 }
99be8f
 
99be8f
+/* Parse action control including possible options.
99be8f
+ *
99be8f
+ * Parameters:
99be8f
+ * @argc_p - pointer to argc to parse
99be8f
+ * @argv_p - pointer to argv to parse
99be8f
+ * @result_p - pointer to output variable
99be8f
+ * @allow_num - whether action may be in numeric format already
99be8f
+ *
99be8f
+ * In error case, returns -1 and does not touch @result_1p. Otherwise returns 0.
99be8f
+ */
99be8f
+int parse_action_control(int *argc_p, char ***argv_p,
99be8f
+			 int *result_p, bool allow_num)
99be8f
+{
99be8f
+	int argc = *argc_p;
99be8f
+	char **argv = *argv_p;
99be8f
+	int result;
99be8f
+
99be8f
+	if (!argc)
99be8f
+		return -1;
99be8f
+	if (action_a2n(*argv, &result, allow_num) == -1) {
99be8f
+		fprintf(stderr, "Bad action type %s\n", *argv);
99be8f
+		return -1;
99be8f
+	}
99be8f
+	NEXT_ARG_FWD();
99be8f
+	*argc_p = argc;
99be8f
+	*argv_p = argv;
99be8f
+	*result_p = result;
99be8f
+	return 0;
99be8f
+}
99be8f
+
99be8f
+/* Parse action control including possible options.
99be8f
+ *
99be8f
+ * Parameters:
99be8f
+ * @argc_p - pointer to argc to parse
99be8f
+ * @argv_p - pointer to argv to parse
99be8f
+ * @result_p - pointer to output variable
99be8f
+ * @allow_num - whether action may be in numeric format already
99be8f
+ * @default_result - set as a result in case of parsing error
99be8f
+ *
99be8f
+ * In case there is an error during parsing, the default result is used.
99be8f
+ */
99be8f
+void parse_action_control_dflt(int *argc_p, char ***argv_p,
99be8f
+			       int *result_p, bool allow_num,
99be8f
+			       int default_result)
99be8f
+{
99be8f
+	if (parse_action_control(argc_p, argv_p, result_p, allow_num))
99be8f
+		*result_p = default_result;
99be8f
+}
99be8f
+
99be8f
+static int parse_action_control_slash_spaces(int *argc_p, char ***argv_p,
99be8f
+					     int *result1_p, int *result2_p,
99be8f
+					     bool allow_num)
99be8f
+{
99be8f
+	int argc = *argc_p;
99be8f
+	char **argv = *argv_p;
99be8f
+	int result1, result2;
99be8f
+	int *result_p = &result1;
99be8f
+	int ok = 0;
99be8f
+	int ret;
99be8f
+
99be8f
+	while (argc > 0) {
99be8f
+		switch (ok) {
99be8f
+		case 1:
99be8f
+			if (strcmp(*argv, "/") != 0)
99be8f
+				goto out;
99be8f
+			result_p = &result2;
99be8f
+			NEXT_ARG();
99be8f
+			/* fall-through */
99be8f
+		case 0: /* fall-through */
99be8f
+		case 2:
99be8f
+			ret = parse_action_control(&argc, &argv,
99be8f
+						   result_p, allow_num);
99be8f
+			if (ret)
99be8f
+				return ret;
99be8f
+			ok++;
99be8f
+			break;
99be8f
+		default:
99be8f
+			goto out;
99be8f
+		}
99be8f
+	}
99be8f
+out:
99be8f
+	*result1_p = result1;
99be8f
+	if (ok == 2)
99be8f
+		*result2_p = result2;
99be8f
+	*argc_p = argc;
99be8f
+	*argv_p = argv;
99be8f
+	return 0;
99be8f
+}
99be8f
+
99be8f
+/* Parse action control with slash including possible options.
99be8f
+ *
99be8f
+ * Parameters:
99be8f
+ * @argc_p - pointer to argc to parse
99be8f
+ * @argv_p - pointer to argv to parse
99be8f
+ * @result1_p - pointer to the first (before slash) output variable
99be8f
+ * @result2_p - pointer to the second (after slash) output variable
99be8f
+ * @allow_num - whether action may be in numeric format already
99be8f
+ *
99be8f
+ * In error case, returns -1 and does not touch @result*. Otherwise returns 0.
99be8f
+ */
99be8f
+int parse_action_control_slash(int *argc_p, char ***argv_p,
99be8f
+			       int *result1_p, int *result2_p, bool allow_num)
99be8f
+{
99be8f
+	char **argv = *argv_p;
99be8f
+	int result1, result2;
99be8f
+	char *p = strchr(*argv, '/');
99be8f
+
99be8f
+	if (!p)
99be8f
+		return parse_action_control_slash_spaces(argc_p, argv_p,
99be8f
+							 result1_p, result2_p,
99be8f
+							 allow_num);
99be8f
+	*p = 0;
99be8f
+	if (action_a2n(*argv, &result1, allow_num)) {
99be8f
+		if (p)
99be8f
+			*p = '/';
99be8f
+		return -1;
99be8f
+	}
99be8f
+
99be8f
+	*p = '/';
99be8f
+	if (action_a2n(p + 1, &result2, allow_num))
99be8f
+		return -1;
99be8f
+
99be8f
+	*result1_p = result1;
99be8f
+	*result2_p = result2;
99be8f
+	return 0;
99be8f
+}
99be8f
+
99be8f
+void print_action_control(FILE *f, const char *prefix,
99be8f
+			  int action, const char *suffix)
99be8f
+{
99be8f
+	fprintf(f, "%s%s%s", prefix, action_n2a(action), suffix);
99be8f
+}
99be8f
+
99be8f
 int get_linklayer(unsigned int *val, const char *arg)
99be8f
 {
99be8f
 	int res;
99be8f
diff --git a/tc/tc_util.h b/tc/tc_util.h
99be8f
index 4db26c6d5e25b..5c54ad384eae6 100644
99be8f
--- a/tc/tc_util.h
99be8f
+++ b/tc/tc_util.h
99be8f
@@ -100,8 +100,15 @@ char *sprint_tc_classid(__u32 h, char *buf);
99be8f
 int tc_print_police(FILE *f, struct rtattr *tb);
99be8f
 int parse_police(int *argc_p, char ***argv_p, int tca_id, struct nlmsghdr *n);
99be8f
 
99be8f
-const char *action_n2a(int action);
99be8f
-int action_a2n(char *arg, int *result, bool allow_num);
99be8f
+int parse_action_control(int *argc_p, char ***argv_p,
99be8f
+			 int *result_p, bool allow_num);
99be8f
+void parse_action_control_dflt(int *argc_p, char ***argv_p,
99be8f
+			       int *result_p, bool allow_num,
99be8f
+			       int default_result);
99be8f
+int parse_action_control_slash(int *argc_p, char ***argv_p,
99be8f
+			       int *result1_p, int *result2_p, bool allow_num);
99be8f
+void print_action_control(FILE *f, const char *prefix,
99be8f
+			  int action, const char *suffix);
99be8f
 int act_parse_police(struct action_util *a, int *argc_p,
99be8f
 		     char ***argv_p, int tca_id, struct nlmsghdr *n);
99be8f
 int print_police(struct action_util *a, FILE *f, struct rtattr *tb);
99be8f
-- 
d30c09
2.21.0
99be8f