Soft Patch Panel
 help / color / mirror / Atom feed
* [spp] [PATCH 0/2] Add REST APIs for add or del port to spp_primary
@ 2019-07-16  7:30 yasufum.o
  2019-07-16  7:30 ` [spp] [PATCH 1/2] spp_primary: add commands to add and del port yasufum.o
  2019-07-16  7:30 ` [spp] [PATCH 2/2] spp-ctl: add port add and del APIs for spp_primary yasufum.o
  0 siblings, 2 replies; 3+ messages in thread
From: yasufum.o @ 2019-07-16  7:30 UTC (permalink / raw)
  To: spp, ferruh.yigit, yasufum.o

From: Yasufumi Ogawa <yasufum.o@gmail.com>

Spp_primary is designed to only have physical ports and initialize them
at the process is started. It is because development of spp_primary was
started before vdev is introduced.

However, Spp_primary can support adding or deleting port dynamically by
using hotplug features currently as same as secondary processes. This
update is to add the features for spp_primary, and add REST API for.
SPP CLI does not support to add or delete for spp_primary yet.

Yasufumi Ogawa (2):
  spp_primary: add commands to add and del port
  spp-ctl: add port add and del APIs for spp_primary

 src/nfv/commands.h        |   2 +-
 src/primary/Makefile      |   6 ++
 src/primary/main.c        | 179 ++++++++++++++++++++++++++++++++++++--
 src/spp-ctl/spp_proc.py   |   8 ++
 src/spp-ctl/spp_webapi.py |  20 +++++
 5 files changed, 209 insertions(+), 6 deletions(-)

-- 
2.17.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [spp] [PATCH 1/2] spp_primary: add commands to add and del port
  2019-07-16  7:30 [spp] [PATCH 0/2] Add REST APIs for add or del port to spp_primary yasufum.o
@ 2019-07-16  7:30 ` yasufum.o
  2019-07-16  7:30 ` [spp] [PATCH 2/2] spp-ctl: add port add and del APIs for spp_primary yasufum.o
  1 sibling, 0 replies; 3+ messages in thread
From: yasufum.o @ 2019-07-16  7:30 UTC (permalink / raw)
  To: spp, ferruh.yigit, yasufum.o

From: Yasufumi Ogawa <yasufum.o@gmail.com>

This patch is to add commands for adding or deleting a port to
spp_primary. REST APIs for the commands are added in the next patch.

Signed-off-by: Yasufumi Ogawa <yasufum.o@gmail.com>
---
 src/nfv/commands.h   |   2 +-
 src/primary/Makefile |   6 ++
 src/primary/main.c   | 179 +++++++++++++++++++++++++++++++++++++++++--
 3 files changed, 181 insertions(+), 6 deletions(-)

diff --git a/src/nfv/commands.h b/src/nfv/commands.h
index 8f19f4e..7bdbdff 100644
--- a/src/nfv/commands.h
+++ b/src/nfv/commands.h
@@ -188,7 +188,7 @@ parse_command(char *str)
 		RTE_ETH_FOREACH_DEV(dev_id) {
 			rte_eth_dev_get_name_by_port(dev_id, dev_name);
 			if (strlen(dev_name) > 0)
-				RTE_LOG(DEBUG, SPP_NFV, "Eth devs: %d\t%s\n",
+				RTE_LOG(DEBUG, SPP_NFV, "ethdevs:%2d %s\n",
 						dev_id, dev_name);
 		}
 
diff --git a/src/primary/Makefile b/src/primary/Makefile
index f348146..e524ef1 100644
--- a/src/primary/Makefile
+++ b/src/primary/Makefile
@@ -18,8 +18,14 @@ endif
 # binary name
 APP = spp_primary
 
+# TODO: move add_port.c and parse_resource uid() in utils.c to shared
+#       dir and remove it.
+SPP_SEC_DIR = ../shared/secondary
+
 # all source are stored in SRCS-y
 SRCS-y := main.c init.c args.c ../shared/common.c
+SRCS-y += $(SPP_SEC_DIR)/add_port.c
+SRCS-y += $(SPP_SEC_DIR)/utils.c
 
 INC := $(wildcard *.h)
 
diff --git a/src/primary/main.c b/src/primary/main.c
index fc9ca9b..635cd71 100644
--- a/src/primary/main.c
+++ b/src/primary/main.c
@@ -17,6 +17,9 @@
 #include "init.h"
 #include "primary.h"
 
+#include "shared/secondary/add_port.h"
+#include "shared/secondary/utils.h"
+
 /*
  * Buffer sizes of status message of primary. Total number of size
  * must be equal to MSG_SIZE 2048 defined in `shared/common.h`.
@@ -38,10 +41,19 @@
 
 #define POLL_TIMEOUT_MS 100
 
-static sig_atomic_t on = 1;
+/**
+ * Set of port id and type of resource UID, such as `vhost:1`. It is intended
+ * to be used for mapping to ethdev ID. as port_id_list.
+ */
+struct port_id_map {
+	int port_id;
+	enum port_type type;
+};
 
-static enum cmd_type cmd = STOP;
+struct port_id_map port_id_list[RTE_MAX_ETHPORTS];
 
+static sig_atomic_t on = 1;
+static enum cmd_type cmd = STOP;
 static struct pollfd pfd;
 
 /* global var - extern in header */
@@ -170,7 +182,7 @@ do_send(int *connected, int *sock, char *str)
 
 	ret = send(*sock, str, MSG_SIZE, 0);
 	if (ret == -1) {
-		RTE_LOG(ERR, PRIMARY, "send failed");
+		RTE_LOG(ERR, PRIMARY, "Failed to send\n");
 		*connected = 0;
 		return -1;
 	}
@@ -221,8 +233,6 @@ launch_sec_proc(char *sec_name, int sec_id, char **sec_args)
 		/* Tokenize path of spp_primary */
 		token_list[i] = strtok(path_spp_pri, "/");
 		while (token_list[i] != NULL) {
-			// RTE_LOG(DEBUG, PRIMARY, "token: %s\n",
-			//		token_list[i]);
 			i++;
 			num_token++;
 			token_list[i] = strtok(NULL, "/");
@@ -437,6 +447,128 @@ get_status_json(char *str)
 	return 0;
 }
 
+/**
+ * Add a port to spp_primary. Port is given as a resource UID which is a
+ * combination of port type and ID like as 'ring:0'.
+ */
+static int
+add_port(char *res_uid)
+{
+	uint16_t dev_id;
+	char *p_type;
+	int p_id;
+	int res;
+	uint16_t cnt = 0;
+
+	res = parse_resource_uid(res_uid, &p_type, &p_id);
+	if (res < 0)
+		return -1;
+
+	for (dev_id = 0; dev_id < RTE_MAX_ETHPORTS; dev_id++) {
+		if (port_id_list[dev_id].port_id == PORT_RESET)
+			break;
+		cnt += 1;
+	}
+
+	if (!strcmp(p_type, "vhost")) {
+		res = add_vhost_pmd(p_id);
+		port_id_list[cnt].port_id = p_id;
+		port_id_list[cnt].type = VHOST;
+
+	} else if (!strcmp(p_type, "ring")) {
+		res = add_ring_pmd(p_id);
+		port_id_list[cnt].port_id = p_id;
+		port_id_list[cnt].type = RING;
+
+	} else if (!strcmp(p_type, "pcap")) {
+		res = add_pcap_pmd(p_id);
+		port_id_list[cnt].port_id = p_id;
+		port_id_list[cnt].type = PCAP;
+
+	} else if (!strcmp(p_type, "nullpmd")) {
+		res = add_null_pmd(p_id);
+		port_id_list[cnt].port_id = p_id;
+		port_id_list[cnt].type = NULLPMD;
+	}
+
+	if (res < 0)
+		return -1;
+
+	return 0;
+}
+
+/* Find ethdev ID from resource UID to delete. */
+static uint16_t
+find_ethdev_id(int p_id, enum port_type ptype)
+{
+	int cnt;
+	struct port_id_map *p_map;
+
+	RTE_LOG(DEBUG, PRIMARY, "Finding ethdev_id, p_id: %d, ptype: %d\n",
+			p_id, ptype);
+
+	for (cnt = 0; cnt < RTE_MAX_ETHPORTS; cnt++) {
+		p_map = &port_id_list[cnt];
+		if (p_map == NULL)
+			RTE_LOG(ERR, PRIMARY, "Failed to find port_id.\n");
+		else {
+			if (p_map->port_id == p_id &&
+					p_map->type == ptype)
+				return cnt;
+		}
+	}
+
+	return PORT_RESET;
+}
+
+/* Delete port. */
+static int
+del_port(char *res_uid)
+{
+	uint16_t dev_id = 0;
+	char *p_type;
+	int p_id;
+	int res;
+
+	res = parse_resource_uid(res_uid, &p_type, &p_id);
+	if (res < 0) {
+		RTE_LOG(ERR, PRIMARY,
+			"Failed to parse resource UID\n");
+		return -1;
+	}
+
+	if (!strcmp(p_type, "vhost")) {
+		dev_id = find_ethdev_id(p_id, VHOST);
+		if (dev_id == PORT_RESET)
+			return -1;
+		dev_detach_by_port_id(dev_id);
+
+	} else if (!strcmp(p_type, "ring")) {
+		dev_id = find_ethdev_id(p_id, RING);
+		if (dev_id == PORT_RESET)
+			return -1;
+		rte_eth_dev_stop(dev_id);
+		rte_eth_dev_close(dev_id);
+
+	} else if (!strcmp(p_type, "pcap")) {
+		dev_id = find_ethdev_id(p_id, PCAP);
+		if (dev_id == PORT_RESET)
+			return -1;
+		dev_detach_by_port_id(dev_id);
+
+	} else if (!strcmp(p_type, "nullpmd")) {
+		dev_id = find_ethdev_id(p_id, NULLPMD);
+		if (dev_id == PORT_RESET)
+			return -1;
+		dev_detach_by_port_id(dev_id);
+	}
+
+	port_id_list[dev_id].port_id = PORT_RESET;
+	port_id_list[dev_id].type = UNDEF;
+
+	return 0;
+}
+
 static int
 parse_command(char *str)
 {
@@ -445,6 +577,8 @@ parse_command(char *str)
 	char *sec_args[NOF_TOKENS] = {NULL};
 	int ret = 0;
 	int i = 0;
+	uint16_t dev_id;
+	char dev_name[RTE_DEV_NAME_MAX_LEN] = { 0 };
 
 	memset(sec_name, '\0', 16);
 
@@ -469,6 +603,13 @@ parse_command(char *str)
 		memset(str, '\0', MSG_SIZE);
 		ret = get_status_json(str);
 
+		RTE_ETH_FOREACH_DEV(dev_id) {
+			rte_eth_dev_get_name_by_port(dev_id, dev_name);
+			if (strlen(dev_name) > 0)
+				RTE_LOG(DEBUG, PRIMARY, "Port list: %d\t%s\n",
+						dev_id, dev_name);
+		}
+
 	} else if (!strcmp(token_list[0], "launch")) {
 		RTE_LOG(DEBUG, PRIMARY, "'%s' command received.\n",
 				token_list[0]);
@@ -476,6 +617,19 @@ parse_command(char *str)
 		ret = launch_sec_proc(sec_name,
 				strtod(token_list[1], NULL), sec_args);
 
+	} else if (!strcmp(token_list[0], "add")) {
+		RTE_LOG(DEBUG, PRIMARY, "'%s' command received.\n",
+				token_list[0]);
+
+		if (add_port(token_list[1]) < 0)
+			RTE_LOG(ERR, PRIMARY, "Failed to add_port()\n");
+
+	} else if (!strcmp(token_list[0], "del")) {
+		RTE_LOG(DEBUG, PRIMARY, "Received del command\n");
+		cmd = STOP;
+		if (del_port(token_list[1]) < 0)
+			RTE_LOG(ERR, PRIMARY, "Failed to del_port()\n");
+
 	} else if (!strcmp(token_list[0], "exit")) {
 		RTE_LOG(DEBUG, PRIMARY, "'exit' command received.\n");
 		cmd = STOP;
@@ -568,6 +722,8 @@ int
 main(int argc, char *argv[])
 {
 	int sock = SOCK_RESET;
+	uint16_t dev_id;
+	char dev_name[RTE_DEV_NAME_MAX_LEN] = { 0 };
 	int connected = 0;
 	char str[MSG_SIZE];
 	int flg_exit;  // used as res of parse_command() to exit if -1
@@ -587,6 +743,19 @@ main(int argc, char *argv[])
 	/* clear statistics */
 	clear_stats();
 
+	memset(port_id_list, 0x00,
+			sizeof(struct port_id_map) * RTE_MAX_ETHPORTS);
+	for (dev_id = 0; dev_id < RTE_MAX_ETHPORTS; dev_id++) {
+		ret = rte_eth_dev_get_name_by_port(dev_id, dev_name);
+		if (ret > -1) {
+			port_id_list[dev_id].port_id = dev_id;
+			port_id_list[dev_id].type = PHY;
+		} else {
+			port_id_list[dev_id].port_id = PORT_RESET;
+			port_id_list[dev_id].type = UNDEF;
+		}
+	}
+
 	/* put all other cores to sleep bar master */
 	rte_eal_mp_remote_launch(sleep_lcore, NULL, SKIP_MASTER);
 
-- 
2.17.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [spp] [PATCH 2/2] spp-ctl: add port add and del APIs for spp_primary
  2019-07-16  7:30 [spp] [PATCH 0/2] Add REST APIs for add or del port to spp_primary yasufum.o
  2019-07-16  7:30 ` [spp] [PATCH 1/2] spp_primary: add commands to add and del port yasufum.o
@ 2019-07-16  7:30 ` yasufum.o
  1 sibling, 0 replies; 3+ messages in thread
From: yasufum.o @ 2019-07-16  7:30 UTC (permalink / raw)
  To: spp, ferruh.yigit, yasufum.o

From: Yasufumi Ogawa <yasufum.o@gmail.com>

This update is to add REST APIs for adding ro deleting to spp_primary.
Here is an example for adding `vhost:1`.

    $ curl -X PUT -H 'application/json' -d \
      '{"action": "add", "port": "vhost:1"}' \
      http://127.0.0.1:7777/v1/primary/ports

Signed-off-by: Yasufumi Ogawa <yasufum.o@gmail.com>
---
 src/spp-ctl/spp_proc.py   |  8 ++++++++
 src/spp-ctl/spp_webapi.py | 20 ++++++++++++++++++++
 2 files changed, 28 insertions(+)

diff --git a/src/spp-ctl/spp_proc.py b/src/spp-ctl/spp_proc.py
index fe7ecea..2bc14e6 100644
--- a/src/spp-ctl/spp_proc.py
+++ b/src/spp-ctl/spp_proc.py
@@ -292,6 +292,14 @@ class PrimaryProc(SppProc):
     def clear(self):
         return "clear"
 
+    @exec_command
+    def port_add(self, port):
+        return "add {port}".format(**locals())
+
+    @exec_command
+    def port_del(self, port):
+        return "del {port}".format(**locals())
+
     @exec_command
     def do_launch_sec_proc(self, args):
         proc_name = args['proc_name']
diff --git a/src/spp-ctl/spp_webapi.py b/src/spp-ctl/spp_webapi.py
index 7f2baad..89b188a 100644
--- a/src/spp-ctl/spp_webapi.py
+++ b/src/spp-ctl/spp_webapi.py
@@ -452,6 +452,8 @@ class V1PrimaryHandler(BaseHandler):
     def set_route(self):
         self.route('/status', 'GET', callback=self.get_status)
         self.route('/status', 'DELETE', callback=self.clear_status)
+        self.route('/ports', 'PUT',
+                   callback=self.primary_port)
         self.route('/launch', 'PUT',
                    callback=self.launch_sec_proc)
         self.route('/', 'DELETE', callback=self.pri_exit)
@@ -478,6 +480,24 @@ class V1PrimaryHandler(BaseHandler):
         proc = self._get_proc()
         proc.clear()
 
+    # TODO(yasufum) change name and make it to shared method
+    def _validate_nfv_port(self, body):
+        for key in ['action', 'port']:
+            if key not in body:
+                raise KeyRequired(key)
+        if body['action'] not in ["add", "del"]:
+            raise KeyInvalid('action', body['action'])
+        self._validate_port(body['port'])
+
+    def primary_port(self, body):
+        self._validate_nfv_port(body)
+        proc = self._get_proc()
+
+        if body['action'] == "add":
+            proc.port_add(body['port'])
+        else:
+            proc.port_del(body['port'])
+
     def launch_sec_proc(self, body):  # the arg should be "body"
         for key in ['client_id', 'proc_name', 'eal', 'app']:
             if key not in body:
-- 
2.17.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2019-07-16  7:31 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-07-16  7:30 [spp] [PATCH 0/2] Add REST APIs for add or del port to spp_primary yasufum.o
2019-07-16  7:30 ` [spp] [PATCH 1/2] spp_primary: add commands to add and del port yasufum.o
2019-07-16  7:30 ` [spp] [PATCH 2/2] spp-ctl: add port add and del APIs for spp_primary yasufum.o

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).