Soft Patch Panel
 help / color / mirror / Atom feed
From: ogawa.yasufumi@lab.ntt.co.jp
To: spp@dpdk.org, ferruh.yigit@intel.com, ogawa.yasufumi@lab.ntt.co.jp
Subject: [spp] [PATCH 1/4] shared: change sec status to JSON format
Date: Thu,  4 Oct 2018 14:59:15 +0900	[thread overview]
Message-ID: <20181004055918.5922-2-ogawa.yasufumi@lab.ntt.co.jp> (raw)
In-Reply-To: <20181004055918.5922-1-ogawa.yasufumi@lab.ntt.co.jp>

From: Yasufumi Ogawa <ogawa.yasufumi@lab.ntt.co.jp>

Spp_nfv or spp_vm returns its status as a YAML-like format for
simplicity and requires clients to parse the response. It is better the
format to parse if it is standardized format.

This update is to change the response to JSON format.

  {
    "status": "running",
    "ports": [
      {"src":"phy:0","dst": "ring:0"},
      {"src":"ring:0","dst": "null"}
    ]
  }

Function retrieving the status is changed from print_active_port() to
get_sec_status_json() to return not only ports info but also running
status.

Signed-off-by: Yasufumi Ogawa <ogawa.yasufumi@lab.ntt.co.jp>
---
 src/shared/common.c | 84 +++++++++++++++++++++++++++++++----------------------
 src/shared/common.h |  2 +-
 2 files changed, 51 insertions(+), 35 deletions(-)

diff --git a/src/shared/common.c b/src/shared/common.c
index fa33fcc..56f89df 100644
--- a/src/shared/common.c
+++ b/src/shared/common.c
@@ -233,21 +233,33 @@ spp_atoi(const char *str, int *val)
 }
 
 /*
- * Print port status in forward array
+ * Get status of spp_nfv or spp_vm as JSON format. It consists of running
+ * status and patch info of ports.
  *
- * Each of port status is formatted as
- * "port_id:[PORT_ID],[IN_PORT_STAT],[TYPE],output:[OUTPORT_STAT]"
+ * Here is an example of well-formatted JSON status to better understand.
+ * Actual status has no spaces and new lines inserted as
+ * '{"status":"running","ports":[{"src":"phy:0","dst":"ring:0"},...]}'
+ *
+ *   {
+ *     "status": "running",
+ *     "ports": [
+ *       {"src":"phy:0","dst": "ring:0"},
+ *       {"src":"ring:0","dst": "null"}
+ *     ]
+ *   }
  */
 void
-print_active_ports(char *str,
+get_sec_stats_json(char *str, const char *running_stat,
 		struct port *ports_fwd_array,
 		struct port_map *port_map)
 {
 	unsigned int i;
-	const char *port_prefix = "ports: '";
+	unsigned int has_ports = 0;  // for checking having port at last
+
+	sprintf(str, "%s",  "{\"status\":");
+	sprintf(str + strlen(str), "\"%s\",", running_stat);
+	sprintf(str + strlen(str), "\"ports\":[");
 
-	/* Every elements value */
-	sprintf(str, "%s",  port_prefix);
 	for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
 		if (ports_fwd_array[i].in_port_id == PORT_RESET)
 			continue;
@@ -256,80 +268,79 @@ print_active_ports(char *str,
 		RTE_LOG(INFO, APP, "Status %d\n",
 			ports_fwd_array[i].in_port_id);
 
-		/* in_port_id is same value as port_id */
-		/**
-		 * NOTE(yasuufm)
-		 * in_port_id cannot be PORT_RESET currently and it is
-		 * meaningless, but not remove for future possible change
-		 */
-		// if (ports_fwd_array[i].in_port_id != PORT_RESET)
-		// 	sprintf(str + strlen(str), "on,");
-		// else
-		// 	sprintf(str + strlen(str), "off,");
+		sprintf(str + strlen(str), "{\"src\":");
 
 		switch (port_map[i].port_type) {
 		case PHY:
+			has_ports = 1;
 			RTE_LOG(INFO, APP, "Type: PHY\n");
-			sprintf(str + strlen(str), "phy:%u-",
+			sprintf(str + strlen(str), "\"phy:%u\",",
 					port_map[i].id);
 			break;
 		case RING:
+			has_ports = 1;
 			RTE_LOG(INFO, APP, "Type: RING\n");
-			sprintf(str + strlen(str), "ring:%u-",
+			sprintf(str + strlen(str), "\"ring:%u\",",
 				port_map[i].id);
 			break;
 		case VHOST:
+			has_ports = 1;
 			RTE_LOG(INFO, APP, "Type: VHOST\n");
-			sprintf(str + strlen(str), "vhost:%u-",
+			sprintf(str + strlen(str), "{\"vhost:%u\",",
 				port_map[i].id);
 			break;
 		case PCAP:
+			has_ports = 1;
 			RTE_LOG(INFO, APP, "Type: PCAP\n");
-			sprintf(str + strlen(str), "pcap:%u-",
+			sprintf(str + strlen(str), "\"pcap:%u\",",
 					port_map[i].id);
 			break;
 		case NULLPMD:
+			has_ports = 1;
 			RTE_LOG(INFO, APP, "Type: NULLPMD\n");
-			sprintf(str + strlen(str), "nullpmd:%u-",
+			sprintf(str + strlen(str), "\"nullpmd:%u\",",
 					port_map[i].id);
 			break;
 		case UNDEF:
+			has_ports = 1;
 			RTE_LOG(INFO, APP, "Type: UDF\n");
 			/* TODO(yasufum) Need to remove print for undefined ? */
-			sprintf(str + strlen(str), "udf-");
+			sprintf(str + strlen(str), "\"udf\",");
 			break;
 		}
 
+		sprintf(str + strlen(str), "\"dst\":");
+
 		RTE_LOG(INFO, APP, "Out Port ID %d\n",
 				ports_fwd_array[i].out_port_id);
 		if (ports_fwd_array[i].out_port_id == PORT_RESET) {
-			sprintf(str + strlen(str), "%s", "null,");
+			sprintf(str + strlen(str), "%s", "\"null\"");
 		} else {
 			unsigned int j = ports_fwd_array[i].out_port_id;
 			switch (port_map[j].port_type) {
 			case PHY:
 				RTE_LOG(INFO, APP, "Type: PHY\n");
-				sprintf(str + strlen(str), "phy:%u,",
+				sprintf(str + strlen(str), "\"phy:%u\"",
 						port_map[j].id);
 				break;
 			case RING:
 				RTE_LOG(INFO, APP, "Type: RING\n");
-				sprintf(str + strlen(str), "ring:%u,",
+				sprintf(str + strlen(str), "\"ring:%u\"",
 					port_map[j].id);
 				break;
 			case VHOST:
 				RTE_LOG(INFO, APP, "Type: VHOST\n");
-				sprintf(str + strlen(str), "vhost:%u,",
+				sprintf(str + strlen(str), "\"vhost:%u\"",
 						port_map[j].id);
 				break;
 			case PCAP:
 				RTE_LOG(INFO, APP, "Type: PCAP\n");
-				sprintf(str + strlen(str), "pcap:%u,",
+				sprintf(str + strlen(str), "\"pcap:%u\"",
 						port_map[j].id);
 				break;
 			case NULLPMD:
 				RTE_LOG(INFO, APP, "Type: NULLPMD\n");
-				sprintf(str + strlen(str), "nullpmd:%u,",
+				sprintf(str + strlen(str), "\"nullpmd:%u\"",
 						port_map[j].id);
 				break;
 			case UNDEF:
@@ -338,18 +349,23 @@ print_active_ports(char *str,
 				 * TODO(yasufum) Need to remove print for
 				 * undefined ?
 				 */
-				sprintf(str + strlen(str), "udf,");
+				sprintf(str + strlen(str), "\"udf\"");
 				break;
 			}
 		}
+
+		sprintf(str + strlen(str), "},");
 	}
 
-	// If there are no ports, it's formatted as "ports: ''"
-	if (strcmp(str, port_prefix) == 0) {
-		sprintf(str + strlen(str), "'");
+	// Check the number of ports to remove "," if it has one or more ports.
+	if (has_ports == 0) {
+		sprintf(str + strlen(str), "]");
 	} else {  // Remove last ','
-		sprintf(str + strlen(str) - 1, "'");
+		sprintf(str + strlen(str) - 1, "]");
 	}
+
+	sprintf(str + strlen(str), "}");
+
 	// make sure to be terminated with null character
 	sprintf(str + strlen(str), "%c", '\0');
 }
diff --git a/src/shared/common.h b/src/shared/common.h
index f030ecd..52a9a65 100644
--- a/src/shared/common.h
+++ b/src/shared/common.h
@@ -192,7 +192,7 @@ int parse_portmask(struct port_info *ports, uint16_t max_ports,
 int parse_num_clients(uint16_t *num_clients, const char *clients);
 int parse_server(char **server_ip, int *server_port, char *server_addr);
 
-void print_active_ports(char *str,
+void get_sec_stats_json(char *str, const char *running_stat,
 		struct port *ports_fwd_array,
 		struct port_map *port_map);
 
-- 
2.7.4

  reply	other threads:[~2018-10-04  5:59 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-10-04  5:59 [spp] [PATCH 0/4] Change format of sec status to JSON ogawa.yasufumi
2018-10-04  5:59 ` ogawa.yasufumi [this message]
2018-10-04  5:59 ` [spp] [PATCH 2/4] spp_nfv: update retrieving status ogawa.yasufumi
2018-10-04  5:59 ` [spp] [PATCH 3/4] spp_vm: " ogawa.yasufumi
2018-10-04  5:59 ` [spp] [PATCH 4/4] controller: update for parsing JSON status ogawa.yasufumi

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=20181004055918.5922-2-ogawa.yasufumi@lab.ntt.co.jp \
    --to=ogawa.yasufumi@lab.ntt.co.jp \
    --cc=ferruh.yigit@intel.com \
    --cc=spp@dpdk.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;
as well as URLs for NNTP newsgroup(s).