examples: add eal cleanup to examples
[dpdk.git] / examples / vhost_blk / vhost_blk.c
index 95a0508..fe2b4e4 100644 (file)
@@ -2,6 +2,12 @@
  * Copyright(c) 2010-2019 Intel Corporation
  */
 
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE
+#endif
+#include <pthread.h>
+#include <sched.h>
+
 #include <stdint.h>
 #include <unistd.h>
 #include <stdbool.h>
@@ -80,9 +86,9 @@ enqueue_task(struct vhost_blk_task *task)
         */
        used->ring[used->idx & (vq->vring.size - 1)].id = task->req_idx;
        used->ring[used->idx & (vq->vring.size - 1)].len = task->data_len;
-       rte_smp_mb();
+       rte_atomic_thread_fence(__ATOMIC_SEQ_CST);
        used->idx++;
-       rte_smp_mb();
+       rte_atomic_thread_fence(__ATOMIC_SEQ_CST);
 
        rte_vhost_clr_inflight_desc_split(task->ctrlr->vid,
                vq->id, used->idx, task->req_idx);
@@ -106,12 +112,12 @@ enqueue_task_packed(struct vhost_blk_task *task)
        desc->id = task->buffer_id;
        desc->addr = 0;
 
-       rte_smp_mb();
+       rte_atomic_thread_fence(__ATOMIC_SEQ_CST);
        if (vq->used_wrap_counter)
                desc->flags |= VIRTQ_DESC_F_AVAIL | VIRTQ_DESC_F_USED;
        else
                desc->flags &= ~(VIRTQ_DESC_F_AVAIL | VIRTQ_DESC_F_USED);
-       rte_smp_mb();
+       rte_atomic_thread_fence(__ATOMIC_SEQ_CST);
 
        rte_vhost_clr_inflight_desc_packed(task->ctrlr->vid, vq->id,
                                           task->inflight_idx);
@@ -597,10 +603,10 @@ new_device(int vid)
        struct vhost_blk_ctrlr *ctrlr;
        struct vhost_blk_queue *vq;
        char path[PATH_MAX];
-       uint64_t features;
+       uint64_t features, protocol_features;
        pthread_t tid;
        int i, ret;
-       bool packed_ring;
+       bool packed_ring, inflight_shmfd;
 
        ret = rte_vhost_get_ifname(vid, path, PATH_MAX);
        if (ret) {
@@ -625,6 +631,16 @@ new_device(int vid)
        }
        packed_ring = !!(features & (1ULL << VIRTIO_F_RING_PACKED));
 
+       ret = rte_vhost_get_negotiated_protocol_features(
+               vid, &protocol_features);
+       if (ret) {
+               fprintf(stderr,
+                       "Failed to get the negotiated protocol features\n");
+               return -1;
+       }
+       inflight_shmfd = !!(features &
+                           (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD));
+
        /* Disable Notifications and init last idx */
        for (i = 0; i < NUM_OF_BLK_QUEUES; i++) {
                vq = &ctrlr->queues[i];
@@ -635,10 +651,13 @@ new_device(int vid)
                assert(rte_vhost_get_vring_base(ctrlr->vid, i,
                                               &vq->last_avail_idx,
                                               &vq->last_used_idx) == 0);
-               assert(rte_vhost_get_vhost_ring_inflight(ctrlr->vid, i,
-                                               &vq->inflight_ring) == 0);
 
-               if (packed_ring) {
+               if (inflight_shmfd)
+                       assert(rte_vhost_get_vhost_ring_inflight(
+                                      ctrlr->vid, i,
+                                      &vq->inflight_ring) == 0);
+
+               if (packed_ring && inflight_shmfd) {
                        /* for the reconnection */
                        assert(rte_vhost_get_vring_base_from_inflight(
                                ctrlr->vid, i,
@@ -666,7 +685,8 @@ new_device(int vid)
        /* start polling vring */
        worker_thread_status = WORKER_STATE_START;
        fprintf(stdout, "New Device %s, Device ID %d\n", path, vid);
-       if (pthread_create(&tid, NULL, &ctrlr_worker, ctrlr) < 0) {
+       if (rte_ctrl_thread_create(&tid, "vhostblk-ctrlr", NULL,
+                                  &ctrlr_worker, ctrlr) != 0) {
                fprintf(stderr, "Worker Thread Started Failed\n");
                return -1;
        }
@@ -750,8 +770,9 @@ vhost_blk_bdev_construct(const char *bdev_name,
        if (!bdev)
                return NULL;
 
-       strncpy(bdev->name, bdev_name, sizeof(bdev->name));
-       strncpy(bdev->product_name, bdev_serial, sizeof(bdev->product_name));
+       snprintf(bdev->name, sizeof(bdev->name), "%s", bdev_name);
+       snprintf(bdev->product_name, sizeof(bdev->product_name), "%s",
+                bdev_serial);
        bdev->blocklen = blk_size;
        bdev->blockcnt = blk_cnt;
        bdev->write_cache = wce_enable;
@@ -876,11 +897,18 @@ int main(int argc, char *argv[])
 
        signal(SIGINT, signal_handler);
 
-       rte_vhost_driver_start(dev_pathname);
+       ret = rte_vhost_driver_start(dev_pathname);
+       if (ret < 0) {
+               fprintf(stderr, "Failed to start vhost driver.\n");
+               return -1;
+       }
 
        /* loop for exit the application */
        while (1)
                sleep(1);
 
+       /* clean up the EAL */
+       rte_eal_cleanup();
+
        return 0;
 }