net/mlx5: handle Rx CQE compression
[dpdk.git] / lib / librte_vhost / vhost_user / virtio-net-user.c
index ffce0d6..a6a48dc 100644 (file)
 #include <sys/mman.h>
 #include <sys/types.h>
 #include <sys/stat.h>
-#include <sys/ioctl.h>
-#include <sys/socket.h>
-#include <net/ethernet.h>
-#include <netinet/in.h>
-#include <netinet/if_ether.h>
-#include <linux/if_packet.h>
 
 #include <rte_common.h>
 #include <rte_log.h>
 
-#include "virtio-net.h"
 #include "virtio-net-user.h"
 #include "vhost-net-user.h"
 #include "vhost-net.h"
@@ -70,9 +63,10 @@ static uint64_t
 get_blk_size(int fd)
 {
        struct stat stat;
+       int ret;
 
-       fstat(fd, &stat);
-       return (uint64_t)stat.st_blksize;
+       ret = fstat(fd, &stat);
+       return ret == -1 ? (uint64_t)-1 : (uint64_t)stat.st_blksize;
 }
 
 static void
@@ -102,10 +96,14 @@ vhost_backend_cleanup(struct virtio_net *dev)
                free(dev->mem);
                dev->mem = NULL;
        }
+       if (dev->log_addr) {
+               munmap((void *)(uintptr_t)dev->log_addr, dev->log_size);
+               dev->log_addr = 0;
+       }
 }
 
 int
-user_set_mem_table(struct vhost_device_ctx ctx, struct VhostUserMsg *pmsg)
+user_set_mem_table(int vid, struct VhostUserMsg *pmsg)
 {
        struct VhostUserMemory memory = pmsg->payload.memory;
        struct virtio_memory_regions *pregion;
@@ -116,13 +114,15 @@ user_set_mem_table(struct vhost_device_ctx ctx, struct VhostUserMsg *pmsg)
        uint64_t alignment;
 
        /* unmap old memory regions one by one*/
-       dev = get_device(ctx);
+       dev = get_device(vid);
        if (dev == NULL)
                return -1;
 
        /* Remove from the data plane. */
-       if (dev->flags & VIRTIO_DEV_RUNNING)
-               notify_ops->destroy_device(dev);
+       if (dev->flags & VIRTIO_DEV_RUNNING) {
+               dev->flags &= ~VIRTIO_DEV_RUNNING;
+               notify_ops->destroy_device(vid);
+       }
 
        if (dev->mem) {
                free_mem_region(dev);
@@ -136,8 +136,8 @@ user_set_mem_table(struct vhost_device_ctx ctx, struct VhostUserMsg *pmsg)
                sizeof(struct orig_region_map) * memory.nregions);
        if (dev->mem == NULL) {
                RTE_LOG(ERR, VHOST_CONFIG,
-                       "(%"PRIu64") Failed to allocate memory for dev->mem\n",
-                       dev->device_fh);
+                       "(%d) failed to allocate memory for dev->mem\n",
+                       dev->vid);
                return -1;
        }
        dev->mem->nregions = memory.nregions;
@@ -168,6 +168,11 @@ user_set_mem_table(struct vhost_device_ctx ctx, struct VhostUserMsg *pmsg)
                 * aligned.
                 */
                alignment = get_blk_size(pmsg->fds[idx]);
+               if (alignment == (uint64_t)-1) {
+                       RTE_LOG(ERR, VHOST_CONFIG,
+                               "couldn't get hugepage size through fstat\n");
+                       goto err_mmap;
+               }
                mapped_size = RTE_ALIGN_CEIL(mapped_size, alignment);
 
                mapped_address = (uint64_t)(uintptr_t)mmap(NULL,
@@ -231,8 +236,8 @@ static int
 vq_is_ready(struct vhost_virtqueue *vq)
 {
        return vq && vq->desc   &&
-              vq->kickfd != -1 &&
-              vq->callfd != -1;
+              vq->kickfd != VIRTIO_UNINITIALIZED_EVENTFD &&
+              vq->callfd != VIRTIO_UNINITIALIZED_EVENTFD;
 }
 
 static int
@@ -258,18 +263,18 @@ virtio_is_ready(struct virtio_net *dev)
 }
 
 void
-user_set_vring_call(struct vhost_device_ctx ctx, struct VhostUserMsg *pmsg)
+user_set_vring_call(int vid, struct VhostUserMsg *pmsg)
 {
        struct vhost_vring_file file;
 
        file.index = pmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
        if (pmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
-               file.fd = -1;
+               file.fd = VIRTIO_INVALID_EVENTFD;
        else
                file.fd = pmsg->fds[0];
        RTE_LOG(INFO, VHOST_CONFIG,
                "vring call idx:%d file:%d\n", file.index, file.fd);
-       ops->set_vring_call(ctx, &file);
+       vhost_set_vring_call(vid, &file);
 }
 
 
@@ -278,42 +283,45 @@ user_set_vring_call(struct vhost_device_ctx ctx, struct VhostUserMsg *pmsg)
  *  device is ready for packet processing.
  */
 void
-user_set_vring_kick(struct vhost_device_ctx ctx, struct VhostUserMsg *pmsg)
+user_set_vring_kick(int vid, struct VhostUserMsg *pmsg)
 {
        struct vhost_vring_file file;
-       struct virtio_net *dev = get_device(ctx);
+       struct virtio_net *dev = get_device(vid);
+
+       if (!dev)
+               return;
 
        file.index = pmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
        if (pmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
-               file.fd = -1;
+               file.fd = VIRTIO_INVALID_EVENTFD;
        else
                file.fd = pmsg->fds[0];
        RTE_LOG(INFO, VHOST_CONFIG,
                "vring kick idx:%d file:%d\n", file.index, file.fd);
-       ops->set_vring_kick(ctx, &file);
+       vhost_set_vring_kick(vid, &file);
 
-       if (virtio_is_ready(dev) &&
-               !(dev->flags & VIRTIO_DEV_RUNNING))
-                       notify_ops->new_device(dev);
+       if (virtio_is_ready(dev) && !(dev->flags & VIRTIO_DEV_RUNNING)) {
+               if (notify_ops->new_device(vid) == 0)
+                       dev->flags |= VIRTIO_DEV_RUNNING;
+       }
 }
 
 /*
  * when virtio is stopped, qemu will send us the GET_VRING_BASE message.
  */
 int
-user_get_vring_base(struct vhost_device_ctx ctx,
-       struct vhost_vring_state *state)
+user_get_vring_base(int vid, struct vhost_vring_state *state)
 {
-       struct virtio_net *dev = get_device(ctx);
+       struct virtio_net *dev = get_device(vid);
 
        if (dev == NULL)
                return -1;
        /* We have to stop the queue (virtio) if it is running. */
        if (dev->flags & VIRTIO_DEV_RUNNING)
-               notify_ops->destroy_device(dev);
+               notify_ops->destroy_device(vid);
 
        /* Here we are safe to get the last used index */
-       ops->get_vring_base(ctx, state->index, state);
+       vhost_get_vring_base(vid, state->index, state);
 
        RTE_LOG(INFO, VHOST_CONFIG,
                "vring base idx:%d file:%d\n", state->index, state->num);
@@ -322,10 +330,10 @@ user_get_vring_base(struct vhost_device_ctx ctx,
         * sent and only sent in vhost_vring_stop.
         * TODO: cleanup the vring, it isn't usable since here.
         */
-       if (dev->virtqueue[state->index]->kickfd >= 0) {
+       if (dev->virtqueue[state->index]->kickfd >= 0)
                close(dev->virtqueue[state->index]->kickfd);
-               dev->virtqueue[state->index]->kickfd = -1;
-       }
+
+       dev->virtqueue[state->index]->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
 
        return 0;
 }
@@ -335,19 +343,21 @@ user_get_vring_base(struct vhost_device_ctx ctx,
  * enable the virtio queue pair.
  */
 int
-user_set_vring_enable(struct vhost_device_ctx ctx,
-                     struct vhost_vring_state *state)
+user_set_vring_enable(int vid, struct vhost_vring_state *state)
 {
-       struct virtio_net *dev = get_device(ctx);
+       struct virtio_net *dev;
        int enable = (int)state->num;
 
+       dev = get_device(vid);
+       if (dev == NULL)
+               return -1;
+
        RTE_LOG(INFO, VHOST_CONFIG,
                "set queue enable: %d to qp idx: %d\n",
                enable, state->index);
 
-       if (notify_ops->vring_state_changed) {
-               notify_ops->vring_state_changed(dev, state->index, enable);
-       }
+       if (notify_ops->vring_state_changed)
+               notify_ops->vring_state_changed(vid, state->index, enable);
 
        dev->virtqueue[state->index]->enabled = enable;
 
@@ -355,12 +365,11 @@ user_set_vring_enable(struct vhost_device_ctx ctx,
 }
 
 void
-user_set_protocol_features(struct vhost_device_ctx ctx,
-                          uint64_t protocol_features)
+user_set_protocol_features(int vid, uint64_t protocol_features)
 {
        struct virtio_net *dev;
 
-       dev = get_device(ctx);
+       dev = get_device(vid);
        if (dev == NULL || protocol_features & ~VHOST_USER_PROTOCOL_FEATURES)
                return;
 
@@ -368,15 +377,14 @@ user_set_protocol_features(struct vhost_device_ctx ctx,
 }
 
 int
-user_set_log_base(struct vhost_device_ctx ctx,
-                struct VhostUserMsg *msg)
+user_set_log_base(int vid, struct VhostUserMsg *msg)
 {
        struct virtio_net *dev;
        int fd = msg->fds[0];
        uint64_t size, off;
        void *addr;
 
-       dev = get_device(ctx);
+       dev = get_device(vid);
        if (!dev)
                return -1;
 
@@ -403,132 +411,58 @@ user_set_log_base(struct vhost_device_ctx ctx,
         * fail when offset is not page size aligned.
         */
        addr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+       close(fd);
        if (addr == MAP_FAILED) {
                RTE_LOG(ERR, VHOST_CONFIG, "mmap log base failed!\n");
                return -1;
        }
 
-       /* TODO: unmap on stop */
-       dev->log_base = (uint64_t)(uintptr_t)addr + off;
+       /*
+        * Free previously mapped log memory on occasionally
+        * multiple VHOST_USER_SET_LOG_BASE.
+        */
+       if (dev->log_addr) {
+               munmap((void *)(uintptr_t)dev->log_addr, dev->log_size);
+       }
+       dev->log_addr = (uint64_t)(uintptr_t)addr;
+       dev->log_base = dev->log_addr + off;
        dev->log_size = size;
 
        return 0;
 }
 
-#define RARP_BUF_SIZE  64
-
-static void
-make_rarp_packet(uint8_t *buf, uint8_t *mac)
-{
-       struct ether_header *eth_hdr;
-       struct ether_arp *rarp;
-
-       /* Ethernet header. */
-       eth_hdr = (struct ether_header *)buf;
-       memset(&eth_hdr->ether_dhost, 0xff, ETH_ALEN);
-       memcpy(&eth_hdr->ether_shost, mac,  ETH_ALEN);
-       eth_hdr->ether_type = htons(ETH_P_RARP);
-
-       /* RARP header. */
-       rarp = (struct ether_arp *)(eth_hdr + 1);
-       rarp->ea_hdr.ar_hrd = htons(ARPHRD_ETHER);
-       rarp->ea_hdr.ar_pro = htons(ETHERTYPE_IP);
-       rarp->ea_hdr.ar_hln = ETH_ALEN;
-       rarp->ea_hdr.ar_pln = 4;
-       rarp->ea_hdr.ar_op  = htons(ARPOP_RREQUEST);
-
-       memcpy(&rarp->arp_sha, mac, ETH_ALEN);
-       memset(&rarp->arp_spa, 0x00, 4);
-       memcpy(&rarp->arp_tha, mac, 6);
-       memset(&rarp->arp_tpa, 0x00, 4);
-}
-
-
-static void
-send_rarp(const char *ifname, uint8_t *rarp)
-{
-       int fd;
-       struct ifreq ifr;
-       struct sockaddr_ll addr;
-
-       fd = socket(AF_PACKET, SOCK_RAW, 0);
-       if (fd < 0) {
-               perror("socket failed");
-               return;
-       }
-
-       memset(&ifr, 0, sizeof(struct ifreq));
-       strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
-       if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) {
-               perror("failed to get interface index");
-               close(fd);
-               return;
-       }
-
-       addr.sll_ifindex = ifr.ifr_ifindex;
-       addr.sll_halen   = ETH_ALEN;
-
-       if (sendto(fd, rarp, RARP_BUF_SIZE, 0,
-                  (const struct sockaddr*)&addr, sizeof(addr)) < 0) {
-               perror("send rarp packet failed");
-       }
-}
-
-
 /*
- * Broadcast a RARP message to all interfaces, to update
- * switch's mac table
+ * An rarp packet is constructed and broadcasted to notify switches about
+ * the new location of the migrated VM, so that packets from outside will
+ * not be lost after migration.
+ *
+ * However, we don't actually "send" a rarp packet here, instead, we set
+ * a flag 'broadcast_rarp' to let rte_vhost_dequeue_burst() inject it.
  */
 int
-user_send_rarp(struct VhostUserMsg *msg)
+user_send_rarp(int vid, struct VhostUserMsg *msg)
 {
+       struct virtio_net *dev;
        uint8_t *mac = (uint8_t *)&msg->payload.u64;
-       uint8_t rarp[RARP_BUF_SIZE];
-       struct ifconf ifc = {0, };
-       struct ifreq *ifr;
-       int nr = 16;
-       int fd;
-       uint32_t i;
+
+       dev = get_device(vid);
+       if (!dev)
+               return -1;
 
        RTE_LOG(DEBUG, VHOST_CONFIG,
                ":: mac: %02x:%02x:%02x:%02x:%02x:%02x\n",
                mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
-
-       make_rarp_packet(rarp, mac);
+       memcpy(dev->mac.addr_bytes, mac, 6);
 
        /*
-        * Get all interfaces
+        * Set the flag to inject a RARP broadcast packet at
+        * rte_vhost_dequeue_burst().
+        *
+        * rte_smp_wmb() is for making sure the mac is copied
+        * before the flag is set.
         */
-       fd = socket(AF_INET, SOCK_DGRAM, 0);
-       if (fd < 0) {
-               perror("failed to create AF_INET socket");
-               return -1;
-       }
-
-again:
-       ifc.ifc_len = sizeof(*ifr) * nr;
-       ifc.ifc_buf = realloc(ifc.ifc_buf, ifc.ifc_len);
-
-       if (ioctl(fd, SIOCGIFCONF, &ifc) < 0) {
-               perror("failed at SIOCGIFCONF");
-               close(fd);
-               return -1;
-       }
-
-       if (ifc.ifc_len == (int)sizeof(struct ifreq) * nr) {
-               /*
-                * current ifc_buf is not big enough to hold
-                * all interfaces; double it and try again.
-                */
-               nr *= 2;
-               goto again;
-       }
-
-       ifr = (struct ifreq *)ifc.ifc_buf;
-       for (i = 0; i < ifc.ifc_len / sizeof(struct ifreq); i++)
-               send_rarp(ifr[i].ifr_name, rarp);
-
-       close(fd);
+       rte_smp_wmb();
+       rte_atomic16_set(&dev->broadcast_rarp, 1);
 
        return 0;
 }