From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from tama50.ecl.ntt.co.jp (tama50.ecl.ntt.co.jp [129.60.39.147]) by dpdk.org (Postfix) with ESMTP id 918495F0D for ; Fri, 5 Oct 2018 07:06:39 +0200 (CEST) Received: from vc1.ecl.ntt.co.jp (vc1.ecl.ntt.co.jp [129.60.86.153]) by tama50.ecl.ntt.co.jp (8.13.8/8.13.8) with ESMTP id w9556cwA027394; Fri, 5 Oct 2018 14:06:38 +0900 Received: from vc1.ecl.ntt.co.jp (localhost [127.0.0.1]) by vc1.ecl.ntt.co.jp (Postfix) with ESMTP id 471F6EA8812; Fri, 5 Oct 2018 14:06:38 +0900 (JST) Received: from localhost.localdomain (unknown [129.60.13.51]) by vc1.ecl.ntt.co.jp (Postfix) with ESMTP id 255F8EA8811; Fri, 5 Oct 2018 14:06:38 +0900 (JST) From: ogawa.yasufumi@lab.ntt.co.jp To: ferruh.yigit@intel.com, spp@dpdk.org, ogawa.yasufumi@lab.ntt.co.jp Date: Fri, 5 Oct 2018 14:06:27 +0900 Message-Id: <20181005050630.10661-2-ogawa.yasufumi@lab.ntt.co.jp> X-Mailer: git-send-email 2.13.1 In-Reply-To: <20181005050630.10661-1-ogawa.yasufumi@lab.ntt.co.jp> References: <20181004055918.5922-1-ogawa.yasufumi@lab.ntt.co.j> <20181005050630.10661-1-ogawa.yasufumi@lab.ntt.co.jp> X-TM-AS-MML: disable Subject: [spp] [PATCH v2 1/4] shared: change sec status to JSON format X-BeenThere: spp@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: Soft Patch Panel List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 05 Oct 2018 05:06:40 -0000 From: Yasufumi Ogawa 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 --- 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