From 89ce8012d71f5689074dc4cbe3db102cbdf76460 Mon Sep 17 00:00:00 2001 From: Andrea Claudi Date: Mon, 8 Jul 2019 11:36:42 +0200 Subject: [PATCH 1/3] ip-route: fix json formatting for metrics Setting metrics for routes currently lead to non-parsable json output. For example: $ ip link add type dummy $ ip route add 192.168.2.0 dev dummy0 metric 100 mtu 1000 rto_min 3 $ ip -j route | jq parse error: ':' not as part of an object at line 1, column 319 Fixing this opening a json object in the metrics array and using print_string() instead of fprintf(). This is the output for the above commands applying this patch: $ ip -j route | jq [ { "dst": "192.168.2.0", "dev": "dummy0", "scope": "link", "metric": 100, "flags": [], "metrics": [ { "mtu": 1000, "rto_min": 3 } ] } ] Fixes: 663c3cb23103f ("iproute: implement JSON and color output") Fixes: 968272e791710 ("iproute: refactor metrics print") Signed-off-by: Andrea Claudi Reported-by: Frank Hofmann Signed-off-by: Stephen Hemminger --- ip/iproute.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ip/iproute.c b/ip/iproute.c index 1669e013..2f9b612b 100644 --- a/ip/iproute.c +++ b/ip/iproute.c @@ -578,6 +578,7 @@ static void print_rta_metrics(FILE *fp, const struct rtattr *rta) int i; open_json_array(PRINT_JSON, "metrics"); + open_json_object(NULL); parse_rtattr(mxrta, RTAX_MAX, RTA_DATA(rta), RTA_PAYLOAD(rta)); @@ -611,7 +612,7 @@ static void print_rta_metrics(FILE *fp, const struct rtattr *rta) print_rtax_features(fp, val); break; default: - fprintf(fp, "%u ", val); + print_uint(PRINT_ANY, mx_names[i], "%u ", val); break; case RTAX_RTT: @@ -639,6 +640,7 @@ static void print_rta_metrics(FILE *fp, const struct rtattr *rta) } } + close_json_object(); close_json_array(PRINT_JSON, NULL); } From 82f3df2028320b83491036b07291223e95d35cef Mon Sep 17 00:00:00 2001 From: Roman Mashak Date: Mon, 8 Jul 2019 12:06:17 -0400 Subject: [PATCH 2/3] tc: added mask parameter in skbedit action Add 32-bit missing mask attribute in iproute2/tc, which has been long supported by the kernel side. v2: print value in hex with print_hex() as suggested by Stephen Hemminger. Signed-off-by: Roman Mashak Signed-off-by: Stephen Hemminger --- tc/m_skbedit.c | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/tc/m_skbedit.c b/tc/m_skbedit.c index b6b839f8..70e3a2e4 100644 --- a/tc/m_skbedit.c +++ b/tc/m_skbedit.c @@ -33,7 +33,7 @@ static void explain(void) fprintf(stderr, "Usage: ... skbedit <[QM] [PM] [MM] [PT] [IF]>\n" "QM = queue_mapping QUEUE_MAPPING\n" "PM = priority PRIORITY\n" - "MM = mark MARK\n" + "MM = mark MARK[/MASK]\n" "PT = ptype PACKETYPE\n" "IF = inheritdsfield\n" "PACKETYPE = is one of:\n" @@ -41,6 +41,7 @@ static void explain(void) "QUEUE_MAPPING = device transmit queue to use\n" "PRIORITY = classID to assign to priority field\n" "MARK = firewall mark to set\n" + "MASK = mask applied to firewall mark (0xffffffff by default)\n" "note: inheritdsfield maps DS field to skb->priority\n"); } @@ -61,7 +62,7 @@ parse_skbedit(struct action_util *a, int *argc_p, char ***argv_p, int tca_id, struct rtattr *tail; unsigned int tmp; __u16 queue_mapping, ptype; - __u32 flags = 0, priority, mark; + __u32 flags = 0, priority, mark, mask; __u64 pure_flags = 0; struct tc_skbedit sel = { 0 }; @@ -89,12 +90,26 @@ parse_skbedit(struct action_util *a, int *argc_p, char ***argv_p, int tca_id, } ok++; } else if (matches(*argv, "mark") == 0) { - flags |= SKBEDIT_F_MARK; + char *slash; + NEXT_ARG(); + slash = strchr(*argv, '/'); + if (slash) + *slash = '\0'; + + flags |= SKBEDIT_F_MARK; if (get_u32(&mark, *argv, 0)) { fprintf(stderr, "Illegal mark\n"); return -1; } + + if (slash) { + if (get_u32(&mask, slash + 1, 0)) { + fprintf(stderr, "Illegal mask\n"); + return -1; + } + flags |= SKBEDIT_F_MASK; + } ok++; } else if (matches(*argv, "ptype") == 0) { @@ -133,7 +148,7 @@ parse_skbedit(struct action_util *a, int *argc_p, char ***argv_p, int tca_id, if (matches(*argv, "index") == 0) { NEXT_ARG(); if (get_u32(&sel.index, *argv, 10)) { - fprintf(stderr, "Pedit: Illegal \"index\"\n"); + fprintf(stderr, "skbedit: Illegal \"index\"\n"); return -1; } argc--; @@ -159,6 +174,9 @@ parse_skbedit(struct action_util *a, int *argc_p, char ***argv_p, int tca_id, if (flags & SKBEDIT_F_MARK) addattr_l(n, MAX_MSG, TCA_SKBEDIT_MARK, &mark, sizeof(mark)); + if (flags & SKBEDIT_F_MASK) + addattr_l(n, MAX_MSG, TCA_SKBEDIT_MASK, + &mask, sizeof(mask)); if (flags & SKBEDIT_F_PTYPE) addattr_l(n, MAX_MSG, TCA_SKBEDIT_PTYPE, &ptype, sizeof(ptype)); @@ -206,6 +224,10 @@ static int print_skbedit(struct action_util *au, FILE *f, struct rtattr *arg) print_uint(PRINT_ANY, "mark", " mark %u", rta_getattr_u32(tb[TCA_SKBEDIT_MARK])); } + if (tb[TCA_SKBEDIT_MASK]) { + print_hex(PRINT_ANY, "mask", "/%#x", + rta_getattr_u32(tb[TCA_SKBEDIT_MASK])); + } if (tb[TCA_SKBEDIT_PTYPE] != NULL) { ptype = rta_getattr_u16(tb[TCA_SKBEDIT_PTYPE]); if (ptype == PACKET_HOST) From 26a49de4dbb6b73e251c9256460ba6b0a8ac5201 Mon Sep 17 00:00:00 2001 From: Roman Mashak Date: Mon, 8 Jul 2019 12:06:18 -0400 Subject: [PATCH 3/3] tc: document 'mask' parameter in skbedit man page Signed-off-by: Roman Mashak Signed-off-by: Stephen Hemminger --- man/man8/tc-skbedit.8 | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/man/man8/tc-skbedit.8 b/man/man8/tc-skbedit.8 index 24591982..704f63bd 100644 --- a/man/man8/tc-skbedit.8 +++ b/man/man8/tc-skbedit.8 @@ -9,8 +9,7 @@ skbedit - SKB editing action .IR QUEUE_MAPPING " ] [" .B priority .IR PRIORITY " ] [" -.B mark -.IR MARK " ] [" +.BI mark " MARK\fR[\fB/\fIMASK] ] [" .B ptype .IR PTYPE " ] [" .BR inheritdsfield " ]" @@ -49,12 +48,14 @@ or a hexadecimal major class ID optionally followed by a colon .RB ( : ) and a hexadecimal minor class ID. .TP -.BI mark " MARK" +.BI mark " MARK\fR[\fB/\fIMASK]" Change the packet's firewall mark value. .I MARK is an unsigned 32bit value in automatically detected format (i.e., prefix with .RB ' 0x ' for hexadecimal interpretation, etc.). +.I MASK +defines the 32-bit mask selecting bits of mark value. Default is 0xffffffff. .TP .BI ptype " PTYPE" Override the packet's type. Useful for setting packet type to host when