examples/vhost: add branch hints
[dpdk.git] / examples / vhost / main.c
index 8ee0c16..cc945f1 100644 (file)
@@ -99,7 +99,6 @@
 #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 */
@@ -174,6 +173,7 @@ uint32_t num_devices = 0;
  * 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,9 +218,6 @@ static uint32_t burst_rx_retry_num = BURST_RX_RETRIES;
 static char dev_basename[MAX_BASENAME_SZ] = "vhost-net";
 
 
-/* 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 = {
        .rx_thresh = {
@@ -673,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);
                                        }
                                }
                        }
@@ -1016,7 +1013,7 @@ 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 {
@@ -1047,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;
@@ -1060,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) {
@@ -1077,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;
@@ -1103,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++;
@@ -1259,7 +1211,7 @@ switch_worker(__attribute__((unused)) void *arg)
                        vdev = dev_ll->vdev;
                        dev = vdev->dev;
 
-                       if (vdev->remove) {
+                       if (unlikely(vdev->remove)) {
                                dev_ll = dev_ll->next;
                                unlink_vmdq(vdev);
                                vdev->ready = DEVICE_SAFE_REMOVE;
@@ -1298,7 +1250,7 @@ switch_worker(__attribute__((unused)) void *arg)
                                }
                        }
 
-                       if (!vdev->remove) {
+                       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 */
@@ -1309,7 +1261,7 @@ switch_worker(__attribute__((unused)) void *arg)
                                        }
                                }
                                while (tx_count)
-                                       virtio_tx_route(vdev, pkts_burst[--tx_count], mbuf_pool, (uint16_t)dev->device_fh);
+                                       virtio_tx_route(vdev, pkts_burst[--tx_count], (uint16_t)dev->device_fh);
                        }
 
                        /*move to the next device in the list*/
@@ -2319,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
@@ -2780,7 +2722,8 @@ new_device (struct virtio_net *dev)
        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;
 
@@ -2921,6 +2864,13 @@ MAIN(int argc, char *argv[])
        ret = us_vhost_parse_args(argc, argv);
        if (ret < 0)
                rte_exit(EXIT_FAILURE, "Invalid argument\n");
+#ifdef RTE_IXGBE_INC_VECTOR
+       if (mergeable == 1) {
+               rte_exit(EXIT_FAILURE,
+                       "sorry, mergeable feature doesn't work with vec sg recv, " \
+                       "please disable it in cfg as a workaround\n");
+       }
+#endif
 
        for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id ++)
                if (rte_lcore_is_enabled(lcore_id))
@@ -3085,6 +3035,9 @@ 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 = rte_vhost_driver_register((char *)&dev_basename);
        if (ret != 0)