Netdev List
 help / color / mirror / Atom feed
From: Keara Leibovitz <kleib@mojatatu.com>
To: dsahern@gmail.com
Cc: stephen@networkplumber.org, netdev@vger.kernel.org,
	kernel@mojatatu.com, Keara Leibovitz <kleib@mojatatu.com>
Subject: [PATCH iproute2-next 1/1] tc: jsonify nat action
Date: Mon, 18 Jun 2018 14:57:49 -0400	[thread overview]
Message-ID: <1529348269-30039-1-git-send-email-kleib@mojatatu.com> (raw)

Add json output support for nat action

Example output:

~$ $TC actions add action nat egress 10.10.10.1 20.20.20.2 index 2
~$ $TC actions add action nat ingress 100.100.100.1/32 200.200.200.2 \
	continue index 99
~$ $TC -j actions ls action nat

[{
	"total acts": 2
}, {
	"actions": [{
		"order": 0,
		"type": "nat",
		"direction": "egress",
		"old_addr": "10.10.10.1/32",
		"new_addr": "20.20.20.2",
		"control_action": {
			"type": "pass"
		},
		"index": 2,
		"ref": 1,
		"bind": 0
	}, {
		"order": 1,
		"type": "nat",
		"direction": "ingress",
		"old_addr": "100.100.100.1/32",
		"new_addr": "200.200.200.2",
		"control_action": {
			"type": "continue"
		},
		"index": 99,
		"ref": 1,
		"bind": 0
	}]
}]

Signed-off-by: Keara Leibovitz <kleib@mojatatu.com>
---
 tc/m_nat.c | 32 +++++++++++++++++++-------------
 1 file changed, 19 insertions(+), 13 deletions(-)

diff --git a/tc/m_nat.c b/tc/m_nat.c
index 653792da91c0..ee0b7520a605 100644
--- a/tc/m_nat.c
+++ b/tc/m_nat.c
@@ -142,9 +142,8 @@ print_nat(struct action_util *au, FILE * f, struct rtattr *arg)
 {
 	struct tc_nat *sel;
 	struct rtattr *tb[TCA_NAT_MAX + 1];
-	char buf1[256];
-	char buf2[256];
-
+	SPRINT_BUF(buf1);
+	SPRINT_BUF(buf2);
 	int len;
 
 	if (arg == NULL)
@@ -153,7 +152,7 @@ print_nat(struct action_util *au, FILE * f, struct rtattr *arg)
 	parse_rtattr_nested(tb, TCA_NAT_MAX, arg);
 
 	if (tb[TCA_NAT_PARMS] == NULL) {
-		fprintf(f, "[NULL nat parameters]");
+		print_string(PRINT_FP, NULL, "%s", "[NULL nat parameters]");
 		return -1;
 	}
 	sel = RTA_DATA(tb[TCA_NAT_PARMS]);
@@ -161,15 +160,22 @@ print_nat(struct action_util *au, FILE * f, struct rtattr *arg)
 	len = ffs(sel->mask);
 	len = len ? 33 - len : 0;
 
-	fprintf(f, " nat %s %s/%d %s", sel->flags & TCA_NAT_FLAG_EGRESS ?
-				       "egress" : "ingress",
-		format_host_r(AF_INET, 4, &sel->old_addr, buf1, sizeof(buf1)),
-		len,
-		format_host_r(AF_INET, 4, &sel->new_addr, buf2, sizeof(buf2)));
-	print_action_control(f, " ", sel->action, "");
+	print_string(PRINT_ANY, "type", " %s ", "nat");
+	print_string(PRINT_ANY, "direction", "%s",
+		     sel->flags & TCA_NAT_FLAG_EGRESS ? "egress" : "ingress");
 
-	fprintf(f, "\n\t index %u ref %d bind %d",
-		sel->index, sel->refcnt, sel->bindcnt);
+	snprintf(buf2, sizeof(buf2), "%s/%d",
+		 format_host_r(AF_INET, 4, &sel->old_addr, buf1, sizeof(buf1)),
+		 len);
+	print_string(PRINT_ANY, "old_addr", " %s", buf2);
+	print_string(PRINT_ANY, "new_addr", " %s",
+		     format_host_r(AF_INET, 4, &sel->new_addr, buf1, sizeof(buf1)));
+
+	print_action_control(f, " ", sel->action, "");
+	print_string(PRINT_FP, NULL, "%s", _SL_);
+	print_uint(PRINT_ANY, "index", "\t index %u", sel->index);
+	print_int(PRINT_ANY, "ref", " ref %d", sel->refcnt);
+	print_int(PRINT_ANY, "bind", " bind %d", sel->bindcnt);
 
 	if (show_stats) {
 		if (tb[TCA_NAT_TM]) {
@@ -179,7 +185,7 @@ print_nat(struct action_util *au, FILE * f, struct rtattr *arg)
 		}
 	}
 
-	fprintf(f, "\n");
+	print_string(PRINT_FP, NULL, "%s", _SL_);
 
 	return 0;
 }
-- 
2.7.4

             reply	other threads:[~2018-06-18 18:58 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-06-18 18:57 Keara Leibovitz [this message]
2018-06-20 17:24 ` [PATCH iproute2-next 1/1] tc: jsonify nat action David Ahern

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1529348269-30039-1-git-send-email-kleib@mojatatu.com \
    --to=kleib@mojatatu.com \
    --cc=dsahern@gmail.com \
    --cc=kernel@mojatatu.com \
    --cc=netdev@vger.kernel.org \
    --cc=stephen@networkplumber.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox