diff mbox series

[v2] examples/vhost: add vhostpmd support

Message ID 20200331073811.54307-1-Sivaprasad.Tummala@intel.com (mailing list archive)
State Superseded, archived
Delegated to: Maxime Coquelin
Headers show
Series [v2] examples/vhost: add vhostpmd support | expand

Checks

Context Check Description
ci/Intel-compilation fail apply issues
ci/checkpatch success coding style OK

Commit Message

Sivaprasad Tummala March 31, 2020, 7:38 a.m. UTC
Added vHostPMD based configuration of vHost devices.
Currently vHost library calls are used for configuring the
vhost device.

vHostPMD is a pre-requisite for enabling future features
supported such as FPGA and possibly CBDMA. With the
vHostPMD integration, upstream features in PMD can be easily
supported and additional features such as queue stats for
vhost devices can be useful.

With the patch, user has an option to select
vHost PMD by passing "--use-vhost-pmd" option.
Disabled by default.

Note: vHost multi-queue is not enabled on this patch.

Signed-off-by: Sivaprasad Tummala <Sivaprasad.Tummala@intel.com>

---

V2:
 - Reverted unregister_device to unregister_drivers - Ye Xiaolong
 - Updated port_init to log and return error to main  - Ye Xiaolong
 - unregister_drivers for previous created vhost ports
   for failed vhost port configure - Ye Xialong
---
 examples/vhost/main.c | 153 ++++++++++++++++++++++++------------------
 1 file changed, 87 insertions(+), 66 deletions(-)
diff mbox series

Patch

diff --git a/examples/vhost/main.c b/examples/vhost/main.c
index b3e331045..0c3d93bbd 100644
--- a/examples/vhost/main.c
+++ b/examples/vhost/main.c
@@ -228,38 +228,44 @@  get_eth_conf(struct rte_eth_conf *eth_conf, uint32_t num_devices)
  * for the device to be re-used
  */
 static void
-unregister_device(int socket_num)
+unregister_drivers(int socket_num)
 {
-	int ret;
+	int i, ret;
 
-	if (vhost_pmd) {
-		char drv_name[RTE_ETH_NAME_MAX_LEN];
-		struct vhost_dev *vdev = NULL;
-		uint16_t port_id;
-
-		snprintf(drv_name, RTE_ETH_NAME_MAX_LEN,
-						"net_vhost%d", socket_num);
-		ret = rte_eth_dev_get_port_by_name(drv_name, &port_id);
-		if (ret != 0)
-			rte_exit(EXIT_FAILURE,
-				"vhost device port id get failed.\n");
+	for (i = 0; i < socket_num; i++) {
+		if (vhost_pmd) {
+			char drv_name[RTE_ETH_NAME_MAX_LEN];
+			struct vhost_dev *vdev = NULL;
+			uint16_t port_id;
 
-		TAILQ_FOREACH(vdev, &vhost_dev_list, global_vdev_entry) {
-			if (vdev->eth_dev_id == port_id) {
-				vdev->remove = 1;
-				break;
+			snprintf(drv_name, RTE_ETH_NAME_MAX_LEN,
+					"net_vhost%d", socket_num);
+			ret = rte_eth_dev_get_port_by_name(drv_name, &port_id);
+			if (ret != 0) {
+				RTE_LOG(ERR, VHOST_CONFIG,
+					"Fail to get vhost port id for %s\n",
+					drv_name);
+				continue;
 			}
-		}
 
-		/* Close the Ethernet device */
-		rte_eth_dev_close(port_id);
-	} else {
-		ret = rte_vhost_driver_unregister(socket_files +
-					socket_num * PATH_MAX);
-		if (ret != 0)
-			RTE_LOG(ERR, VHOST_CONFIG,
-				"Fail to unregister vhost driver for %s.\n",
-				socket_files + socket_num * PATH_MAX);
+			TAILQ_FOREACH(vdev, &vhost_dev_list,
+						global_vdev_entry) {
+				if (vdev->eth_dev_id == port_id) {
+					vdev->remove = 1;
+					break;
+				}
+			}
+
+			/* unregister the device */
+			rte_eth_dev_close(port_id);
+		} else {
+			ret = rte_vhost_driver_unregister(
+					socket_files + i * PATH_MAX);
+			if (ret != 0)
+				RTE_LOG(ERR, VHOST_CONFIG,
+					"Fail to unregister vhost driver for %s.\n",
+					socket_files + i * PATH_MAX);
+		}
 	}
 }
 
@@ -268,7 +274,7 @@  unregister_device(int socket_num)
  * coming from the mbuf_pool passed as parameter
  */
 static inline int
-port_init_v2(uint16_t portid)
+vhost_pmd_port_init(uint16_t portid)
 {
 	struct rte_eth_rxconf rxq_conf;
 	struct rte_eth_txconf txq_conf;
@@ -289,10 +295,12 @@  port_init_v2(uint16_t portid)
 	fflush(stdout);
 
 	ret = rte_eth_dev_info_get(portid, &dev_info);
-	if (ret != 0)
-		rte_exit(EXIT_FAILURE,
+	if (ret != 0) {
+		RTE_LOG(ERR, VHOST_CONFIG,
 			"Error during getting device (port %u) info: %s\n",
 			portid, strerror(-ret));
+		return ret;
+	}
 
 	if (strncmp(dev_info.driver_name, "net_vhost",
 				sizeof("net_vhost")) == 0)
@@ -301,26 +309,33 @@  port_init_v2(uint16_t portid)
 			vhost_device_event_callback, NULL);
 
 	ret = rte_eth_dev_configure(portid, 1, 1, &port_conf);
-	if (ret < 0)
-		rte_exit(EXIT_FAILURE, "Cannot configure device: err=%d, port=%u\n",
-			  ret, portid);
+	if (ret < 0) {
+		RTE_LOG(ERR, VHOST_CONFIG,
+			"Cannot configure device: err=%d, port=%u\n",
+			ret, portid);
+		return ret;
+	}
 
 	nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
 	nb_txd = RTE_TEST_TX_DESC_DEFAULT;
 
 	ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
 						   &nb_txd);
-	if (ret < 0)
-		rte_exit(EXIT_FAILURE,
-			 "Cannot adjust number of descriptors: err=%d, port=%u\n",
-			 ret, portid);
+	if (ret < 0) {
+		RTE_LOG(ERR, VHOST_CONFIG,
+			"Cannot adjust number of descriptors: err=%d, port=%u\n",
+			ret, portid);
+		return ret;
+	}
 
 	ret = rte_eth_macaddr_get(portid,
 				  &vmdq_ports_eth_addr[portid]);
-	if (ret < 0)
-		rte_exit(EXIT_FAILURE,
-			 "Cannot get MAC address: err=%d, port=%u\n",
-			 ret, portid);
+	if (ret < 0) {
+		RTE_LOG(ERR, VHOST_CONFIG,
+			"Cannot get MAC address: err=%d, port=%u\n",
+			ret, portid);
+		return ret;
+	}
 
 	/* init RX queue */
 	fflush(stdout);
@@ -330,9 +345,12 @@  port_init_v2(uint16_t portid)
 					 rte_eth_dev_socket_id(portid),
 					 &rxq_conf,
 					 mbuf_pool);
-	if (ret < 0)
-		rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup:err=%d, port=%u\n",
-			  ret, portid);
+	if (ret < 0) {
+		RTE_LOG(ERR, VHOST_CONFIG,
+			"rte_eth_rx_queue_setup: err=%d, port=%u\n",
+			ret, portid);
+		return ret;
+	}
 
 	/* init TX queue */
 	fflush(stdout);
@@ -341,27 +359,29 @@  port_init_v2(uint16_t portid)
 	ret = rte_eth_tx_queue_setup(portid, 0, nb_txd,
 			rte_eth_dev_socket_id(portid),
 			&txq_conf);
-	if (ret < 0)
-		rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup:err=%d, port=%u\n",
+	if (ret < 0) {
+		RTE_LOG(ERR, VHOST_CONFIG,
+			"rte_eth_rx_queue_setup: err=%d, port=%u\n",
 			ret, portid);
+		return ret;
+	}
 
 	/* Start device */
 	ret = rte_eth_dev_start(portid);
-	if (ret < 0)
-		rte_exit(EXIT_FAILURE, "rte_eth_dev_start:err=%d, port=%u\n",
-				ret, portid);
+	if (ret < 0) {
+		RTE_LOG(ERR, VHOST_CONFIG,
+			"rte_eth_dev_start: err=%d, port=%u\n",
+			ret, portid);
+		return ret;
+	}
 
 	if (promiscuous) {
 		ret = rte_eth_promiscuous_enable(portid);
 		if ((ret != 0) && (ret != -ENOTSUP)) {
-			uint8_t i;
-
-			for (i = 0; i < nb_sockets; i++)
-				unregister_device(i);
-
-			rte_exit(EXIT_FAILURE,
-				 "rte_eth_promiscuous_enable:err=%s, port=%u\n",
-				 rte_strerror(-ret), portid);
+			RTE_LOG(ERR, VHOST_CONFIG,
+				"rte_eth_promiscuous_enable: err=%d, port=%u\n",
+				ret, portid);
+			return ret;
 		}
 	}
 
@@ -1207,6 +1227,8 @@  drain_eth_rx(struct vhost_dev *vdev)
 		return;
 
 	if (vhost_pmd) {
+		uint32_t idx;
+
 		enqueue_count = rte_eth_tx_burst(vdev->eth_dev_id, 0,
 			pkts, rx_count);
 		if (unlikely(enqueue_count < rx_count)) {
@@ -1222,7 +1244,6 @@  drain_eth_rx(struct vhost_dev *vdev)
 					break;
 			}
 			/* Drop the remaining packets */
-			uint32_t idx;
 			for (idx = enqueue_count; idx < rx_count; idx++)
 				rte_pktmbuf_free(pkts[idx]);
 		}
@@ -1620,10 +1641,7 @@  static void
 sigint_handler(__rte_unused int signum)
 {
 	/* Unregister vhost driver. */
-	uint8_t i;
-
-	for (i = 0; i < nb_sockets; i++)
-		unregister_device(i);
+	unregister_drivers(nb_sockets);
 
 	exit(0);
 }
@@ -1806,25 +1824,26 @@  main(int argc, char *argv[])
 
 			ret = rte_dev_probe(dev_name);
 			if (ret != 0) {
+				unregister_drivers(i);
 				rte_exit(EXIT_FAILURE,
 					"vhost user device probe failed\n");
 			}
 			ret = rte_eth_dev_get_port_by_name(drv_name, &port);
 			if (ret != 0) {
-				unregister_device(i);
+				unregister_drivers(i+1);
 				rte_exit(EXIT_FAILURE,
 					"vhost device port id get failed.\n");
 			}
-			ret = port_init_v2(port);
+			ret = vhost_pmd_port_init(port);
 			if (ret != 0) {
-				unregister_device(i);
+				unregister_drivers(i+1);
 				rte_exit(EXIT_FAILURE,
 					"vhost device port initialization failed.\n");
 			}
 		} else {
 			ret = rte_vhost_driver_register(file, flags);
 			if (ret != 0) {
-				unregister_device(i);
+				unregister_drivers(i);
 				rte_exit(EXIT_FAILURE,
 					"vhost driver register failure.\n");
 			}
@@ -1862,11 +1881,13 @@  main(int argc, char *argv[])
 			ret = rte_vhost_driver_callback_register(file,
 				&virtio_net_device_ops);
 			if (ret != 0) {
+				unregister_drivers(i+1);
 				rte_exit(EXIT_FAILURE,
 					"failed to register vhost driver callbacks.\n");
 			}
 
 			if (rte_vhost_driver_start(file) < 0) {
+				unregister_drivers(i+1);
 				rte_exit(EXIT_FAILURE,
 					"failed to start vhost driver.\n");
 			}