examples/vhost: allow mergeable packets with vector ixgbe
[dpdk.git] / examples / vhost / main.c
index 0c9ba56..57ef464 100644 (file)
 #include <rte_log.h>
 #include <rte_string_fns.h>
 #include <rte_malloc.h>
+#include <rte_virtio_net.h>
 
 #include "main.h"
-#include "virtio-net.h"
-#include "vhost-net-cdev.h"
 
 #define MAX_QUEUES 128
 
 #define TX_WTHRESH 0  /* Default values of TX write-back threshold reg. */
 
 #define MAX_PKT_BURST 32               /* Max burst size for RX/TX */
-#define MAX_MRG_PKT_BURST 16   /* Max burst for merge buffers. Set to 1 due to performance issue. */
 #define BURST_TX_DRAIN_US 100  /* TX drain every ~100us */
 
 #define BURST_RX_WAIT_US 15    /* Defines how long we wait between retries on RX */
@@ -168,13 +166,14 @@ static uint32_t num_switching_cores = 0;
 
 /* number of devices/queues to support*/
 static uint32_t num_queues = 0;
-uint32_t num_devices = 0;
+static uint32_t num_devices;
 
 /*
  * Enable zero copy, pkts buffer will directly dma to hw descriptor,
  * disabled on default.
  */
 static uint32_t zero_copy;
+static int mergeable;
 
 /* number of descriptors to apply*/
 static uint32_t num_rx_descriptor = RTE_TEST_RX_DESC_DEFAULT_ZCP;
@@ -218,11 +217,6 @@ static uint32_t burst_rx_retry_num = BURST_RX_RETRIES;
 /* Character device basename. Can be set by user. */
 static char dev_basename[MAX_BASENAME_SZ] = "vhost-net";
 
-/* Charater device index. Can be set by user. */
-static uint32_t dev_index = 0;
-
-/* This can be set by the user so it is made available here. */
-extern uint64_t VHOST_FEATURES;
 
 /* Default configuration for rx and tx thresholds etc. */
 static struct rte_eth_rxconf rx_conf_default = {
@@ -560,7 +554,7 @@ us_vhost_usage(const char *prgname)
        RTE_LOG(INFO, VHOST_CONFIG, "%s [EAL options] -- -p PORTMASK\n"
        "               --vm2vm [0|1|2]\n"
        "               --rx_retry [0|1] --mergeable [0|1] --stats [0-N]\n"
-       "               --dev-basename <name> --dev-index [0-N]\n"
+       "               --dev-basename <name>\n"
        "               --nb-devices ND\n"
        "               -p PORTMASK: Set mask for ports to be used by application\n"
        "               --vm2vm [0|1|2]: disable/software(default)/hardware vm2vm comms\n"
@@ -570,7 +564,6 @@ us_vhost_usage(const char *prgname)
        "               --mergeable [0|1]: disable(default)/enable RX mergeable buffers\n"
        "               --stats [0-N]: 0: Disable stats, N: Time in seconds to print stats\n"
        "               --dev-basename: The basename to be used for the character device.\n"
-       "               --dev-index [0-N]: Defaults to zero if not used. Index is appended to basename.\n"
        "               --zero-copy [0|1]: disable(default)/enable rx/tx "
                        "zero copy\n"
        "               --rx-desc-num [0-N]: the number of descriptors on rx, "
@@ -598,7 +591,6 @@ us_vhost_parse_args(int argc, char **argv)
                {"mergeable", required_argument, NULL, 0},
                {"stats", required_argument, NULL, 0},
                {"dev-basename", required_argument, NULL, 0},
-               {"dev-index", required_argument, NULL, 0},
                {"zero-copy", required_argument, NULL, 0},
                {"rx-desc-num", required_argument, NULL, 0},
                {"tx-desc-num", required_argument, NULL, 0},
@@ -678,11 +670,11 @@ us_vhost_parse_args(int argc, char **argv)
                                        us_vhost_usage(prgname);
                                        return -1;
                                } else {
+                                       mergeable = !!ret;
                                        if (ret) {
                                                vmdq_conf_default.rxmode.jumbo_frame = 1;
                                                vmdq_conf_default.rxmode.max_rx_pkt_len
                                                        = JUMBO_FRAME_MAX_SIZE;
-                                               VHOST_FEATURES = (1ULL << VIRTIO_NET_F_MRG_RXBUF);
                                        }
                                }
                        }
@@ -708,17 +700,6 @@ us_vhost_parse_args(int argc, char **argv)
                                }
                        }
 
-                       /* Set character device index. */
-                       if (!strncmp(long_option[option_index].name, "dev-index", MAX_LONG_OPT_SZ)) {
-                               ret = parse_num_opt(optarg, INT32_MAX);
-                               if (ret == -1) {
-                                       RTE_LOG(INFO, VHOST_CONFIG, "Invalid argument for character device index [0..N]\n");
-                                       us_vhost_usage(prgname);
-                                       return -1;
-                               } else
-                                       dev_index = ret;
-                       }
-
                        /* Enable/disable rx/tx zero copy. */
                        if (!strncmp(long_option[option_index].name,
                                "zero-copy", MAX_LONG_OPT_SZ)) {
@@ -1003,7 +984,7 @@ unlink_vmdq(struct vhost_dev *vdev)
  * Check if the packet destination MAC address is for a local device. If so then put
  * the packet on that devices RX queue. If not then return.
  */
-static inline unsigned __attribute__((always_inline))
+static inline int __attribute__((always_inline))
 virtio_tx_local(struct vhost_dev *vdev, struct rte_mbuf *m)
 {
        struct virtio_net_data_ll *dev_ll;
@@ -1032,21 +1013,12 @@ virtio_tx_local(struct vhost_dev *vdev, struct rte_mbuf *m)
 
                        LOG_DEBUG(VHOST_DATA, "(%"PRIu64") TX: MAC address is local\n", tdev->device_fh);
 
-                       if (dev_ll->vdev->remove) {
+                       if (unlikely(dev_ll->vdev->remove)) {
                                /*drop the packet if the device is marked for removal*/
                                LOG_DEBUG(VHOST_DATA, "(%"PRIu64") Device is marked for removal\n", tdev->device_fh);
                        } else {
-                               uint32_t mergeable =
-                                       dev_ll->dev->features &
-                                       (1 << VIRTIO_NET_F_MRG_RXBUF);
-
                                /*send the packet to the local virtio device*/
-                               if (likely(mergeable == 0))
-                                       ret = virtio_dev_rx(dev_ll->dev, &m, 1);
-                               else
-                                       ret = virtio_dev_merge_rx(dev_ll->dev,
-                                               &m, 1);
-
+                               ret = rte_vhost_enqueue_burst(tdev, VIRTIO_RXQ, &m, 1);
                                if (enable_stats) {
                                        rte_atomic64_add(
                                        &dev_statistics[tdev->device_fh].rx_total_atomic,
@@ -1072,12 +1044,10 @@ virtio_tx_local(struct vhost_dev *vdev, struct rte_mbuf *m)
  * or the physical port.
  */
 static inline void __attribute__((always_inline))
-virtio_tx_route(struct vhost_dev *vdev, struct rte_mbuf *m, struct rte_mempool *mbuf_pool, uint16_t vlan_tag)
+virtio_tx_route(struct vhost_dev *vdev, struct rte_mbuf *m, uint16_t vlan_tag)
 {
        struct mbuf_table *tx_q;
-       struct vlan_ethhdr *vlan_hdr;
        struct rte_mbuf **m_table;
-       struct rte_mbuf *mbuf, *prev;
        unsigned len, ret, offset = 0;
        const uint16_t lcore_id = rte_lcore_id();
        struct virtio_net_data_ll *dev_ll = ll_root_used;
@@ -1085,8 +1055,10 @@ virtio_tx_route(struct vhost_dev *vdev, struct rte_mbuf *m, struct rte_mempool *
        struct virtio_net *dev = vdev->dev;
 
        /*check if destination is local VM*/
-       if ((vm2vm_mode == VM2VM_SOFTWARE) && (virtio_tx_local(vdev, m) == 0))
+       if ((vm2vm_mode == VM2VM_SOFTWARE) && (virtio_tx_local(vdev, m) == 0)) {
+               rte_pktmbuf_free(m);
                return;
+       }
 
        if (vm2vm_mode == VM2VM_HARDWARE) {
                while (dev_ll != NULL) {
@@ -1102,7 +1074,8 @@ virtio_tx_route(struct vhost_dev *vdev, struct rte_mbuf *m, struct rte_mempool *
                                        "(%"PRIu64") TX: Source and destination"
                                        " MAC addresses are the same. Dropping "
                                        "packet.\n",
-                                       dev_ll->vdev->device_fh);
+                                       dev_ll->vdev->dev->device_fh);
+                                       rte_pktmbuf_free(m);
                                        return;
                                }
                                offset = 4;
@@ -1128,58 +1101,12 @@ virtio_tx_route(struct vhost_dev *vdev, struct rte_mbuf *m, struct rte_mempool *
        tx_q = &lcore_tx_queue[lcore_id];
        len = tx_q->len;
 
-       /* Allocate an mbuf and populate the structure. */
-       mbuf = rte_pktmbuf_alloc(mbuf_pool);
-       if (unlikely(mbuf == NULL)) {
-               RTE_LOG(ERR, VHOST_DATA,
-                       "Failed to allocate memory for mbuf.\n");
-               return;
-       }
-
-       mbuf->data_len = m->data_len + VLAN_HLEN + offset;
-       mbuf->pkt_len = m->pkt_len + VLAN_HLEN + offset;
-       mbuf->nb_segs = m->nb_segs;
-
-       /* Copy ethernet header to mbuf. */
-       rte_memcpy(rte_pktmbuf_mtod(mbuf, void *),
-               rte_pktmbuf_mtod(m, const void *),
-               ETH_HLEN);
-
-
-       /* Setup vlan header. Bytes need to be re-ordered for network with htons()*/
-       vlan_hdr = rte_pktmbuf_mtod(mbuf, struct vlan_ethhdr *);
-       vlan_hdr->h_vlan_encapsulated_proto = vlan_hdr->h_vlan_proto;
-       vlan_hdr->h_vlan_proto = htons(ETH_P_8021Q);
-       vlan_hdr->h_vlan_TCI = htons(vlan_tag);
-
-       /* Copy the remaining packet contents to the mbuf. */
-       rte_memcpy((void *)(rte_pktmbuf_mtod(mbuf, uint8_t *) + VLAN_ETH_HLEN),
-               (const void *)(rte_pktmbuf_mtod(m, uint8_t *) + ETH_HLEN),
-               (m->data_len - ETH_HLEN));
-
-       /* Copy the remaining segments for the whole packet. */
-       prev = mbuf;
-       while (m->next) {
-               /* Allocate an mbuf and populate the structure. */
-               struct rte_mbuf *next_mbuf = rte_pktmbuf_alloc(mbuf_pool);
-               if (unlikely(next_mbuf == NULL)) {
-                       rte_pktmbuf_free(mbuf);
-                       RTE_LOG(ERR, VHOST_DATA,
-                               "Failed to allocate memory for mbuf.\n");
-                       return;
-               }
-
-               m = m->next;
-               prev->next = next_mbuf;
-               prev = next_mbuf;
-               next_mbuf->data_len = m->data_len;
-
-               /* Copy data to next mbuf. */
-               rte_memcpy(rte_pktmbuf_mtod(next_mbuf, void *),
-                       rte_pktmbuf_mtod(m, const void *), m->data_len);
-       }
+       m->ol_flags = PKT_TX_VLAN_PKT;
+       /*FIXME: offset*/
+       m->data_len += offset;
+       m->vlan_tci = vlan_tag;
 
-       tx_q->m_table[len] = mbuf;
+       tx_q->m_table[len] = m;
        len++;
        if (enable_stats) {
                dev_statistics[dev->device_fh].tx_total++;
@@ -1223,7 +1150,8 @@ switch_worker(__attribute__((unused)) void *arg)
        const uint16_t lcore_id = rte_lcore_id();
        const uint16_t num_cores = (uint16_t)rte_lcore_count();
        uint16_t rx_count = 0;
-       uint32_t mergeable = 0;
+       uint16_t tx_count;
+       uint32_t retry = 0;
 
        RTE_LOG(INFO, VHOST_DATA, "Procesing on Core %u started\n", lcore_id);
        lcore_ll = lcore_info[lcore_id].lcore_ll;
@@ -1282,10 +1210,8 @@ switch_worker(__attribute__((unused)) void *arg)
                        /*get virtio device ID*/
                        vdev = dev_ll->vdev;
                        dev = vdev->dev;
-                       mergeable =
-                               dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF);
 
-                       if (vdev->remove) {
+                       if (unlikely(vdev->remove)) {
                                dev_ll = dev_ll->next;
                                unlink_vmdq(vdev);
                                vdev->ready = DEVICE_SAFE_REMOVE;
@@ -1297,15 +1223,18 @@ switch_worker(__attribute__((unused)) void *arg)
                                        vdev->vmdq_rx_q, pkts_burst, MAX_PKT_BURST);
 
                                if (rx_count) {
-                                       if (likely(mergeable == 0))
-                                               ret_count =
-                                                       virtio_dev_rx(dev,
-                                                       pkts_burst, rx_count);
-                                       else
-                                               ret_count =
-                                                       virtio_dev_merge_rx(dev,
-                                                       pkts_burst, rx_count);
-
+                                       /*
+                                       * Retry is enabled and the queue is full then we wait and retry to avoid packet loss
+                                       * Here MAX_PKT_BURST must be less than virtio queue size
+                                       */
+                                       if (enable_retry && unlikely(rx_count > rte_vring_available_entries(dev, VIRTIO_RXQ))) {
+                                               for (retry = 0; retry < burst_rx_retry_num; retry++) {
+                                                       rte_delay_us(burst_rx_delay_time);
+                                                       if (rx_count <= rte_vring_available_entries(dev, VIRTIO_RXQ))
+                                                               break;
+                                               }
+                                       }
+                                       ret_count = rte_vhost_enqueue_burst(dev, VIRTIO_RXQ, pkts_burst, rx_count);
                                        if (enable_stats) {
                                                rte_atomic64_add(
                                                &dev_statistics[dev_ll->vdev->dev->device_fh].rx_total_atomic,
@@ -1321,12 +1250,18 @@ switch_worker(__attribute__((unused)) void *arg)
                                }
                        }
 
-                       if (!vdev->remove) {
-                               /*Handle guest TX*/
-                               if (likely(mergeable == 0))
-                                       virtio_dev_tx(dev, mbuf_pool);
-                               else
-                                       virtio_dev_merge_tx(dev, mbuf_pool);
+                       if (likely(!vdev->remove)) {
+                               /* Handle guest TX*/
+                               tx_count = rte_vhost_dequeue_burst(dev, VIRTIO_TXQ, mbuf_pool, pkts_burst, MAX_PKT_BURST);
+                               /* If this is the first received packet we need to learn the MAC and setup VMDQ */
+                               if (unlikely(vdev->ready == DEVICE_MAC_LEARNING) && tx_count) {
+                                       if (vdev->remove || (link_vmdq(vdev, pkts_burst[0]) == -1)) {
+                                               while (tx_count--)
+                                                       rte_pktmbuf_free(pkts_burst[tx_count]);
+                                       }
+                               }
+                               while (tx_count)
+                                       virtio_tx_route(vdev, pkts_burst[--tx_count], (uint16_t)dev->device_fh);
                        }
 
                        /*move to the next device in the list*/
@@ -2336,16 +2271,6 @@ init_data_ll (void)
        return 0;
 }
 
-/*
- * Set virtqueue flags so that we do not receive interrupts.
- */
-static void
-set_irq_status (struct virtio_net *dev)
-{
-       dev->virtqueue[VIRTIO_RXQ]->used->flags = VRING_USED_F_NO_NOTIFY;
-       dev->virtqueue[VIRTIO_TXQ]->used->flags = VRING_USED_F_NO_NOTIFY;
-}
-
 /*
  * Remove a device from the specific data core linked list and from the main linked list. Synchonization
  * occurs through the use of the lcore dev_removal_flag. Device is made volatile here to avoid re-ordering
@@ -2465,11 +2390,159 @@ destroy_device (volatile struct virtio_net *dev)
                        dev->device_fh);
 
                mbuf_destroy_zcp(vpool);
+               rte_free(vdev->regions_hpa);
        }
        rte_free(vdev);
 
 }
 
+/*
+ * Calculate the region count of physical continous regions for one particular
+ * region of whose vhost virtual address is continous. The particular region
+ * start from vva_start, with size of 'size' in argument.
+ */
+static uint32_t
+check_hpa_regions(uint64_t vva_start, uint64_t size)
+{
+       uint32_t i, nregions = 0, page_size = getpagesize();
+       uint64_t cur_phys_addr = 0, next_phys_addr = 0;
+       if (vva_start % page_size) {
+               LOG_DEBUG(VHOST_CONFIG,
+                       "in check_countinous: vva start(%p) mod page_size(%d) "
+                       "has remainder\n",
+                       (void *)(uintptr_t)vva_start, page_size);
+               return 0;
+       }
+       if (size % page_size) {
+               LOG_DEBUG(VHOST_CONFIG,
+                       "in check_countinous: "
+                       "size((%"PRIu64")) mod page_size(%d) has remainder\n",
+                       size, page_size);
+               return 0;
+       }
+       for (i = 0; i < size - page_size; i = i + page_size) {
+               cur_phys_addr
+                       = rte_mem_virt2phy((void *)(uintptr_t)(vva_start + i));
+               next_phys_addr = rte_mem_virt2phy(
+                       (void *)(uintptr_t)(vva_start + i + page_size));
+               if ((cur_phys_addr + page_size) != next_phys_addr) {
+                       ++nregions;
+                       LOG_DEBUG(VHOST_CONFIG,
+                               "in check_continuous: hva addr:(%p) is not "
+                               "continuous with hva addr:(%p), diff:%d\n",
+                               (void *)(uintptr_t)(vva_start + (uint64_t)i),
+                               (void *)(uintptr_t)(vva_start + (uint64_t)i
+                               + page_size), page_size);
+                       LOG_DEBUG(VHOST_CONFIG,
+                               "in check_continuous: hpa addr:(%p) is not "
+                               "continuous with hpa addr:(%p), "
+                               "diff:(%"PRIu64")\n",
+                               (void *)(uintptr_t)cur_phys_addr,
+                               (void *)(uintptr_t)next_phys_addr,
+                               (next_phys_addr-cur_phys_addr));
+               }
+       }
+       return nregions;
+}
+
+/*
+ * Divide each region whose vhost virtual address is continous into a few
+ * sub-regions, make sure the physical address within each sub-region are
+ * continous. And fill offset(to GPA) and size etc. information of each
+ * sub-region into regions_hpa.
+ */
+static uint32_t
+fill_hpa_memory_regions(struct virtio_memory_regions_hpa *mem_region_hpa, struct virtio_memory *virtio_memory)
+{
+       uint32_t regionidx, regionidx_hpa = 0, i, k, page_size = getpagesize();
+       uint64_t cur_phys_addr = 0, next_phys_addr = 0, vva_start;
+
+       if (mem_region_hpa == NULL)
+               return 0;
+
+       for (regionidx = 0; regionidx < virtio_memory->nregions; regionidx++) {
+               vva_start = virtio_memory->regions[regionidx].guest_phys_address +
+                       virtio_memory->regions[regionidx].address_offset;
+               mem_region_hpa[regionidx_hpa].guest_phys_address
+                       = virtio_memory->regions[regionidx].guest_phys_address;
+               mem_region_hpa[regionidx_hpa].host_phys_addr_offset =
+                       rte_mem_virt2phy((void *)(uintptr_t)(vva_start)) -
+                       mem_region_hpa[regionidx_hpa].guest_phys_address;
+               LOG_DEBUG(VHOST_CONFIG,
+                       "in fill_hpa_regions: guest phys addr start[%d]:(%p)\n",
+                       regionidx_hpa,
+                       (void *)(uintptr_t)
+                       (mem_region_hpa[regionidx_hpa].guest_phys_address));
+               LOG_DEBUG(VHOST_CONFIG,
+                       "in fill_hpa_regions: host  phys addr start[%d]:(%p)\n",
+                       regionidx_hpa,
+                       (void *)(uintptr_t)
+                       (mem_region_hpa[regionidx_hpa].host_phys_addr_offset));
+               for (i = 0, k = 0;
+                       i < virtio_memory->regions[regionidx].memory_size -
+                               page_size;
+                       i += page_size) {
+                       cur_phys_addr = rte_mem_virt2phy(
+                                       (void *)(uintptr_t)(vva_start + i));
+                       next_phys_addr = rte_mem_virt2phy(
+                                       (void *)(uintptr_t)(vva_start +
+                                       i + page_size));
+                       if ((cur_phys_addr + page_size) != next_phys_addr) {
+                               mem_region_hpa[regionidx_hpa].guest_phys_address_end =
+                                       mem_region_hpa[regionidx_hpa].guest_phys_address +
+                                       k + page_size;
+                               mem_region_hpa[regionidx_hpa].memory_size
+                                       = k + page_size;
+                               LOG_DEBUG(VHOST_CONFIG, "in fill_hpa_regions: guest "
+                                       "phys addr end  [%d]:(%p)\n",
+                                       regionidx_hpa,
+                                       (void *)(uintptr_t)
+                                       (mem_region_hpa[regionidx_hpa].guest_phys_address_end));
+                               LOG_DEBUG(VHOST_CONFIG,
+                                       "in fill_hpa_regions: guest phys addr "
+                                       "size [%d]:(%p)\n",
+                                       regionidx_hpa,
+                                       (void *)(uintptr_t)
+                                       (mem_region_hpa[regionidx_hpa].memory_size));
+                               mem_region_hpa[regionidx_hpa + 1].guest_phys_address
+                                       = mem_region_hpa[regionidx_hpa].guest_phys_address_end;
+                               ++regionidx_hpa;
+                               mem_region_hpa[regionidx_hpa].host_phys_addr_offset =
+                                       next_phys_addr -
+                                       mem_region_hpa[regionidx_hpa].guest_phys_address;
+                               LOG_DEBUG(VHOST_CONFIG, "in fill_hpa_regions: guest"
+                                       " phys addr start[%d]:(%p)\n",
+                                       regionidx_hpa,
+                                       (void *)(uintptr_t)
+                                       (mem_region_hpa[regionidx_hpa].guest_phys_address));
+                               LOG_DEBUG(VHOST_CONFIG,
+                                       "in fill_hpa_regions: host  phys addr "
+                                       "start[%d]:(%p)\n",
+                                       regionidx_hpa,
+                                       (void *)(uintptr_t)
+                                       (mem_region_hpa[regionidx_hpa].host_phys_addr_offset));
+                               k = 0;
+                       } else {
+                               k += page_size;
+                       }
+               }
+               mem_region_hpa[regionidx_hpa].guest_phys_address_end
+                       = mem_region_hpa[regionidx_hpa].guest_phys_address
+                       + k + page_size;
+               mem_region_hpa[regionidx_hpa].memory_size = k + page_size;
+               LOG_DEBUG(VHOST_CONFIG, "in fill_hpa_regions: guest phys addr end  "
+                       "[%d]:(%p)\n", regionidx_hpa,
+                       (void *)(uintptr_t)
+                       (mem_region_hpa[regionidx_hpa].guest_phys_address_end));
+               LOG_DEBUG(VHOST_CONFIG, "in fill_hpa_regions: guest phys addr size "
+                       "[%d]:(%p)\n", regionidx_hpa,
+                       (void *)(uintptr_t)
+                       (mem_region_hpa[regionidx_hpa].memory_size));
+               ++regionidx_hpa;
+       }
+       return regionidx_hpa;
+}
+
 /*
  * A new device is added to a data core. First the device is added to the main linked list
  * and the allocated to a specific data core.
@@ -2481,6 +2554,7 @@ new_device (struct virtio_net *dev)
        int lcore, core_add = 0;
        uint32_t device_num_min = num_devices;
        struct vhost_dev *vdev;
+       uint32_t regionidx;
 
        vdev = rte_zmalloc("vhost device", sizeof(*vdev), CACHE_LINE_SIZE);
        if (vdev == NULL) {
@@ -2491,12 +2565,49 @@ new_device (struct virtio_net *dev)
        vdev->dev = dev;
        dev->priv = vdev;
 
+       if (zero_copy) {
+               vdev->nregions_hpa = dev->mem->nregions;
+               for (regionidx = 0; regionidx < dev->mem->nregions; regionidx++) {
+                       vdev->nregions_hpa
+                               += check_hpa_regions(
+                                       dev->mem->regions[regionidx].guest_phys_address
+                                       + dev->mem->regions[regionidx].address_offset,
+                                       dev->mem->regions[regionidx].memory_size);
+
+               }
+
+               vdev->regions_hpa = (struct virtio_memory_regions_hpa *) rte_zmalloc("vhost hpa region",
+                       sizeof(struct virtio_memory_regions_hpa) * vdev->nregions_hpa,
+                       CACHE_LINE_SIZE);
+               if (vdev->regions_hpa == NULL) {
+                       RTE_LOG(ERR, VHOST_CONFIG, "Cannot allocate memory for hpa region\n");
+                       rte_free(vdev);
+                       return -1;
+               }
+
+
+               if (fill_hpa_memory_regions(
+                       vdev->regions_hpa, dev->mem
+                       ) != vdev->nregions_hpa) {
+
+                       RTE_LOG(ERR, VHOST_CONFIG,
+                               "hpa memory regions number mismatch: "
+                               "[%d]\n", vdev->nregions_hpa);
+                       rte_free(vdev->regions_hpa);
+                       rte_free(vdev);
+                       return -1;
+               }
+       }
+
+
        /* Add device to main ll */
        ll_dev = get_data_ll_free_entry(&ll_root_free);
        if (ll_dev == NULL) {
                RTE_LOG(INFO, VHOST_DATA, "(%"PRIu64") No free entry found in linked list. Device limit "
                        "of %d devices per core has been reached\n",
                        dev->device_fh, num_devices);
+               if (vdev->regions_hpa)
+                       rte_free(vdev->regions_hpa);
                rte_free(vdev);
                return -1;
        }
@@ -2549,6 +2660,7 @@ new_device (struct virtio_net *dev)
                                dev->device_fh, vdev->vmdq_rx_q);
 
                        mbuf_destroy_zcp(vpool);
+                       rte_free(vdev->regions_hpa);
                        rte_free(vdev);
                        return -1;
                }
@@ -2571,6 +2683,7 @@ new_device (struct virtio_net *dev)
                        }
 
                        mbuf_destroy_zcp(vpool);
+                       rte_free(vdev->regions_hpa);
                        rte_free(vdev);
                        return -1;
                }
@@ -2589,25 +2702,27 @@ new_device (struct virtio_net *dev)
                }
        }
        /* Add device to lcore ll */
-       ll_dev->dev->coreid = core_add;
-       ll_dev = get_data_ll_free_entry(&lcore_info[ll_dev->dev->coreid].lcore_ll->ll_root_free);
+       ll_dev = get_data_ll_free_entry(&lcore_info[core_add].lcore_ll->ll_root_free);
        if (ll_dev == NULL) {
                RTE_LOG(INFO, VHOST_DATA, "(%"PRIu64") Failed to add device to data core\n", dev->device_fh);
                vdev->ready = DEVICE_SAFE_REMOVE;
                destroy_device(dev);
+               if (vdev->regions_hpa)
+                       rte_free(vdev->regions_hpa);
                rte_free(vdev);
                return -1;
        }
        ll_dev->vdev = vdev;
        vdev->coreid = core_add;
 
-       add_data_ll_entry(&lcore_info[ll_dev->dev->coreid].lcore_ll->ll_root_used, ll_dev);
+       add_data_ll_entry(&lcore_info[vdev->coreid].lcore_ll->ll_root_used, ll_dev);
 
        /* Initialize device stats */
        memset(&dev_statistics[dev->device_fh], 0, sizeof(struct device_statistics));
 
        /* Disable notifications. */
-       set_irq_status(dev);
+       rte_vhost_enable_guest_notification(dev, VIRTIO_RXQ, 0);
+       rte_vhost_enable_guest_notification(dev, VIRTIO_TXQ, 0);
        lcore_info[vdev->coreid].lcore_ll->device_num++;
        dev->flags |= VIRTIO_DEV_RUNNING;
 
@@ -2912,15 +3027,18 @@ MAIN(int argc, char *argv[])
                                lcore_id);
        }
 
+       if (mergeable == 0)
+               rte_vhost_feature_disable(1ULL << VIRTIO_NET_F_MRG_RXBUF);
+
        /* Register CUSE device to handle IOCTLs. */
-       ret = register_cuse_device((char*)&dev_basename, dev_index, get_virtio_net_callbacks());
+       ret = rte_vhost_driver_register((char *)&dev_basename);
        if (ret != 0)
                rte_exit(EXIT_FAILURE,"CUSE device setup failure.\n");
 
-       init_virtio_net(&virtio_net_device_ops);
+       rte_vhost_driver_callback_register(&virtio_net_device_ops);
 
        /* Start CUSE session. */
-       start_cuse_session_loop();
+       rte_vhost_driver_session_start();
        return 0;
 
 }