New upstream version 18.08
[deb_dpdk.git] / drivers / net / virtio / virtio_user / virtio_user_dev.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2016 Intel Corporation
3  */
4
5 #include <stdint.h>
6 #include <stdio.h>
7 #include <fcntl.h>
8 #include <string.h>
9 #include <errno.h>
10 #include <sys/mman.h>
11 #include <unistd.h>
12 #include <sys/eventfd.h>
13 #include <sys/types.h>
14 #include <sys/stat.h>
15
16 #include "vhost.h"
17 #include "virtio_user_dev.h"
18 #include "../virtio_ethdev.h"
19
20 #define VIRTIO_USER_MEM_EVENT_CLB_NAME "virtio_user_mem_event_clb"
21
22 static int
23 virtio_user_create_queue(struct virtio_user_dev *dev, uint32_t queue_sel)
24 {
25         /* Of all per virtqueue MSGs, make sure VHOST_SET_VRING_CALL come
26          * firstly because vhost depends on this msg to allocate virtqueue
27          * pair.
28          */
29         struct vhost_vring_file file;
30
31         file.index = queue_sel;
32         file.fd = dev->callfds[queue_sel];
33         dev->ops->send_request(dev, VHOST_USER_SET_VRING_CALL, &file);
34
35         return 0;
36 }
37
38 static int
39 virtio_user_kick_queue(struct virtio_user_dev *dev, uint32_t queue_sel)
40 {
41         struct vhost_vring_file file;
42         struct vhost_vring_state state;
43         struct vring *vring = &dev->vrings[queue_sel];
44         struct vhost_vring_addr addr = {
45                 .index = queue_sel,
46                 .desc_user_addr = (uint64_t)(uintptr_t)vring->desc,
47                 .avail_user_addr = (uint64_t)(uintptr_t)vring->avail,
48                 .used_user_addr = (uint64_t)(uintptr_t)vring->used,
49                 .log_guest_addr = 0,
50                 .flags = 0, /* disable log */
51         };
52
53         state.index = queue_sel;
54         state.num = vring->num;
55         dev->ops->send_request(dev, VHOST_USER_SET_VRING_NUM, &state);
56
57         state.index = queue_sel;
58         state.num = 0; /* no reservation */
59         dev->ops->send_request(dev, VHOST_USER_SET_VRING_BASE, &state);
60
61         dev->ops->send_request(dev, VHOST_USER_SET_VRING_ADDR, &addr);
62
63         /* Of all per virtqueue MSGs, make sure VHOST_USER_SET_VRING_KICK comes
64          * lastly because vhost depends on this msg to judge if
65          * virtio is ready.
66          */
67         file.index = queue_sel;
68         file.fd = dev->kickfds[queue_sel];
69         dev->ops->send_request(dev, VHOST_USER_SET_VRING_KICK, &file);
70
71         return 0;
72 }
73
74 static int
75 virtio_user_queue_setup(struct virtio_user_dev *dev,
76                         int (*fn)(struct virtio_user_dev *, uint32_t))
77 {
78         uint32_t i, queue_sel;
79
80         for (i = 0; i < dev->max_queue_pairs; ++i) {
81                 queue_sel = 2 * i + VTNET_SQ_RQ_QUEUE_IDX;
82                 if (fn(dev, queue_sel) < 0) {
83                         PMD_DRV_LOG(INFO, "setup rx vq fails: %u", i);
84                         return -1;
85                 }
86         }
87         for (i = 0; i < dev->max_queue_pairs; ++i) {
88                 queue_sel = 2 * i + VTNET_SQ_TQ_QUEUE_IDX;
89                 if (fn(dev, queue_sel) < 0) {
90                         PMD_DRV_LOG(INFO, "setup tx vq fails: %u", i);
91                         return -1;
92                 }
93         }
94
95         return 0;
96 }
97
98 int
99 is_vhost_user_by_type(const char *path)
100 {
101         struct stat sb;
102
103         if (stat(path, &sb) == -1)
104                 return 0;
105
106         return S_ISSOCK(sb.st_mode);
107 }
108
109 int
110 virtio_user_start_device(struct virtio_user_dev *dev)
111 {
112         uint64_t features;
113         int ret;
114
115         pthread_mutex_lock(&dev->mutex);
116
117         if (is_vhost_user_by_type(dev->path) && dev->vhostfd < 0)
118                 goto error;
119
120         /* Do not check return as already done in init, or reset in stop */
121         dev->ops->send_request(dev, VHOST_USER_SET_OWNER, NULL);
122
123         /* Step 0: tell vhost to create queues */
124         if (virtio_user_queue_setup(dev, virtio_user_create_queue) < 0)
125                 goto error;
126
127         /* Step 1: set features */
128         features = dev->features;
129         /* Strip VIRTIO_NET_F_MAC, as MAC address is handled in vdev init */
130         features &= ~(1ull << VIRTIO_NET_F_MAC);
131         /* Strip VIRTIO_NET_F_CTRL_VQ, as devices do not really need to know */
132         features &= ~(1ull << VIRTIO_NET_F_CTRL_VQ);
133         features &= ~(1ull << VIRTIO_NET_F_STATUS);
134         ret = dev->ops->send_request(dev, VHOST_USER_SET_FEATURES, &features);
135         if (ret < 0)
136                 goto error;
137         PMD_DRV_LOG(INFO, "set features: %" PRIx64, features);
138
139         /* Step 2: share memory regions */
140         ret = dev->ops->send_request(dev, VHOST_USER_SET_MEM_TABLE, NULL);
141         if (ret < 0)
142                 goto error;
143
144         /* Step 3: kick queues */
145         if (virtio_user_queue_setup(dev, virtio_user_kick_queue) < 0)
146                 goto error;
147
148         /* Step 4: enable queues
149          * we enable the 1st queue pair by default.
150          */
151         dev->ops->enable_qp(dev, 0, 1);
152
153         dev->started = true;
154         pthread_mutex_unlock(&dev->mutex);
155
156         return 0;
157 error:
158         pthread_mutex_unlock(&dev->mutex);
159         /* TODO: free resource here or caller to check */
160         return -1;
161 }
162
163 int virtio_user_stop_device(struct virtio_user_dev *dev)
164 {
165         uint32_t i;
166
167         pthread_mutex_lock(&dev->mutex);
168         for (i = 0; i < dev->max_queue_pairs; ++i)
169                 dev->ops->enable_qp(dev, i, 0);
170
171         if (dev->ops->send_request(dev, VHOST_USER_RESET_OWNER, NULL) < 0) {
172                 PMD_DRV_LOG(INFO, "Failed to reset the device\n");
173                 pthread_mutex_unlock(&dev->mutex);
174                 return -1;
175         }
176         dev->started = false;
177         pthread_mutex_unlock(&dev->mutex);
178
179         return 0;
180 }
181
182 static inline void
183 parse_mac(struct virtio_user_dev *dev, const char *mac)
184 {
185         int i, r;
186         uint32_t tmp[ETHER_ADDR_LEN];
187
188         if (!mac)
189                 return;
190
191         r = sscanf(mac, "%x:%x:%x:%x:%x:%x", &tmp[0],
192                         &tmp[1], &tmp[2], &tmp[3], &tmp[4], &tmp[5]);
193         if (r == ETHER_ADDR_LEN) {
194                 for (i = 0; i < ETHER_ADDR_LEN; ++i)
195                         dev->mac_addr[i] = (uint8_t)tmp[i];
196                 dev->mac_specified = 1;
197         } else {
198                 /* ignore the wrong mac, use random mac */
199                 PMD_DRV_LOG(ERR, "wrong format of mac: %s", mac);
200         }
201 }
202
203 static int
204 virtio_user_dev_init_notify(struct virtio_user_dev *dev)
205 {
206         uint32_t i, j;
207         int callfd;
208         int kickfd;
209
210         for (i = 0; i < VIRTIO_MAX_VIRTQUEUES; ++i) {
211                 if (i >= dev->max_queue_pairs * 2) {
212                         dev->kickfds[i] = -1;
213                         dev->callfds[i] = -1;
214                         continue;
215                 }
216
217                 /* May use invalid flag, but some backend uses kickfd and
218                  * callfd as criteria to judge if dev is alive. so finally we
219                  * use real event_fd.
220                  */
221                 callfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
222                 if (callfd < 0) {
223                         PMD_DRV_LOG(ERR, "callfd error, %s", strerror(errno));
224                         break;
225                 }
226                 kickfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
227                 if (kickfd < 0) {
228                         PMD_DRV_LOG(ERR, "kickfd error, %s", strerror(errno));
229                         break;
230                 }
231                 dev->callfds[i] = callfd;
232                 dev->kickfds[i] = kickfd;
233         }
234
235         if (i < VIRTIO_MAX_VIRTQUEUES) {
236                 for (j = 0; j <= i; ++j) {
237                         close(dev->callfds[j]);
238                         close(dev->kickfds[j]);
239                 }
240
241                 return -1;
242         }
243
244         return 0;
245 }
246
247 static int
248 virtio_user_fill_intr_handle(struct virtio_user_dev *dev)
249 {
250         uint32_t i;
251         struct rte_eth_dev *eth_dev = &rte_eth_devices[dev->port_id];
252
253         if (!eth_dev->intr_handle) {
254                 eth_dev->intr_handle = malloc(sizeof(*eth_dev->intr_handle));
255                 if (!eth_dev->intr_handle) {
256                         PMD_DRV_LOG(ERR, "fail to allocate intr_handle");
257                         return -1;
258                 }
259                 memset(eth_dev->intr_handle, 0, sizeof(*eth_dev->intr_handle));
260         }
261
262         for (i = 0; i < dev->max_queue_pairs; ++i)
263                 eth_dev->intr_handle->efds[i] = dev->callfds[i];
264         eth_dev->intr_handle->nb_efd = dev->max_queue_pairs;
265         eth_dev->intr_handle->max_intr = dev->max_queue_pairs + 1;
266         eth_dev->intr_handle->type = RTE_INTR_HANDLE_VDEV;
267         /* For virtio vdev, no need to read counter for clean */
268         eth_dev->intr_handle->efd_counter_size = 0;
269         eth_dev->intr_handle->fd = -1;
270         if (dev->vhostfd >= 0)
271                 eth_dev->intr_handle->fd = dev->vhostfd;
272         else if (dev->is_server)
273                 eth_dev->intr_handle->fd = dev->listenfd;
274
275         return 0;
276 }
277
278 static void
279 virtio_user_mem_event_cb(enum rte_mem_event type __rte_unused,
280                                                  const void *addr __rte_unused,
281                                                  size_t len __rte_unused,
282                                                  void *arg)
283 {
284         struct virtio_user_dev *dev = arg;
285         uint16_t i;
286
287         pthread_mutex_lock(&dev->mutex);
288
289         if (dev->started == false)
290                 goto exit;
291
292         /* Step 1: pause the active queues */
293         for (i = 0; i < dev->queue_pairs; i++)
294                 dev->ops->enable_qp(dev, i, 0);
295
296         /* Step 2: update memory regions */
297         dev->ops->send_request(dev, VHOST_USER_SET_MEM_TABLE, NULL);
298
299         /* Step 3: resume the active queues */
300         for (i = 0; i < dev->queue_pairs; i++)
301                 dev->ops->enable_qp(dev, i, 1);
302
303 exit:
304         pthread_mutex_unlock(&dev->mutex);
305 }
306
307 static int
308 virtio_user_dev_setup(struct virtio_user_dev *dev)
309 {
310         uint32_t q;
311
312         dev->vhostfd = -1;
313         dev->vhostfds = NULL;
314         dev->tapfds = NULL;
315
316         if (dev->is_server) {
317                 if (access(dev->path, F_OK) == 0 &&
318                     !is_vhost_user_by_type(dev->path)) {
319                         PMD_DRV_LOG(ERR, "Server mode doesn't support vhost-kernel!");
320                         return -1;
321                 }
322                 dev->ops = &ops_user;
323         } else {
324                 if (is_vhost_user_by_type(dev->path)) {
325                         dev->ops = &ops_user;
326                 } else {
327                         dev->ops = &ops_kernel;
328
329                         dev->vhostfds = malloc(dev->max_queue_pairs *
330                                                sizeof(int));
331                         dev->tapfds = malloc(dev->max_queue_pairs *
332                                              sizeof(int));
333                         if (!dev->vhostfds || !dev->tapfds) {
334                                 PMD_INIT_LOG(ERR, "Failed to malloc");
335                                 return -1;
336                         }
337
338                         for (q = 0; q < dev->max_queue_pairs; ++q) {
339                                 dev->vhostfds[q] = -1;
340                                 dev->tapfds[q] = -1;
341                         }
342                 }
343         }
344
345         if (dev->ops->setup(dev) < 0)
346                 return -1;
347
348         if (virtio_user_dev_init_notify(dev) < 0)
349                 return -1;
350
351         if (virtio_user_fill_intr_handle(dev) < 0)
352                 return -1;
353
354         return 0;
355 }
356
357 /* Use below macro to filter features from vhost backend */
358 #define VIRTIO_USER_SUPPORTED_FEATURES                  \
359         (1ULL << VIRTIO_NET_F_MAC               |       \
360          1ULL << VIRTIO_NET_F_STATUS            |       \
361          1ULL << VIRTIO_NET_F_MQ                |       \
362          1ULL << VIRTIO_NET_F_CTRL_MAC_ADDR     |       \
363          1ULL << VIRTIO_NET_F_CTRL_VQ           |       \
364          1ULL << VIRTIO_NET_F_CTRL_RX           |       \
365          1ULL << VIRTIO_NET_F_CTRL_VLAN         |       \
366          1ULL << VIRTIO_NET_F_CSUM              |       \
367          1ULL << VIRTIO_NET_F_HOST_TSO4         |       \
368          1ULL << VIRTIO_NET_F_HOST_TSO6         |       \
369          1ULL << VIRTIO_NET_F_MRG_RXBUF         |       \
370          1ULL << VIRTIO_RING_F_INDIRECT_DESC    |       \
371          1ULL << VIRTIO_NET_F_GUEST_CSUM        |       \
372          1ULL << VIRTIO_NET_F_GUEST_TSO4        |       \
373          1ULL << VIRTIO_NET_F_GUEST_TSO6        |       \
374          1ULL << VIRTIO_F_IN_ORDER              |       \
375          1ULL << VIRTIO_F_VERSION_1)
376
377 int
378 virtio_user_dev_init(struct virtio_user_dev *dev, char *path, int queues,
379                      int cq, int queue_size, const char *mac, char **ifname,
380                      int mrg_rxbuf, int in_order)
381 {
382         pthread_mutex_init(&dev->mutex, NULL);
383         snprintf(dev->path, PATH_MAX, "%s", path);
384         dev->started = 0;
385         dev->max_queue_pairs = queues;
386         dev->queue_pairs = 1; /* mq disabled by default */
387         dev->queue_size = queue_size;
388         dev->mac_specified = 0;
389         dev->unsupported_features = 0;
390         parse_mac(dev, mac);
391
392         if (*ifname) {
393                 dev->ifname = *ifname;
394                 *ifname = NULL;
395         }
396
397         if (virtio_user_dev_setup(dev) < 0) {
398                 PMD_INIT_LOG(ERR, "backend set up fails");
399                 return -1;
400         }
401
402         if (!dev->is_server) {
403                 if (dev->ops->send_request(dev, VHOST_USER_SET_OWNER,
404                                            NULL) < 0) {
405                         PMD_INIT_LOG(ERR, "set_owner fails: %s",
406                                      strerror(errno));
407                         return -1;
408                 }
409
410                 if (dev->ops->send_request(dev, VHOST_USER_GET_FEATURES,
411                                            &dev->device_features) < 0) {
412                         PMD_INIT_LOG(ERR, "get_features failed: %s",
413                                      strerror(errno));
414                         return -1;
415                 }
416         } else {
417                 /* We just pretend vhost-user can support all these features.
418                  * Note that this could be problematic that if some feature is
419                  * negotiated but not supported by the vhost-user which comes
420                  * later.
421                  */
422                 dev->device_features = VIRTIO_USER_SUPPORTED_FEATURES;
423         }
424
425         if (!mrg_rxbuf) {
426                 dev->device_features &= ~(1ull << VIRTIO_NET_F_MRG_RXBUF);
427                 dev->unsupported_features |= (1ull << VIRTIO_NET_F_MRG_RXBUF);
428         }
429
430         if (!in_order) {
431                 dev->device_features &= ~(1ull << VIRTIO_F_IN_ORDER);
432                 dev->unsupported_features |= (1ull << VIRTIO_F_IN_ORDER);
433         }
434
435         if (dev->mac_specified) {
436                 dev->device_features |= (1ull << VIRTIO_NET_F_MAC);
437         } else {
438                 dev->device_features &= ~(1ull << VIRTIO_NET_F_MAC);
439                 dev->unsupported_features |= (1ull << VIRTIO_NET_F_MAC);
440         }
441
442         if (cq) {
443                 /* device does not really need to know anything about CQ,
444                  * so if necessary, we just claim to support CQ
445                  */
446                 dev->device_features |= (1ull << VIRTIO_NET_F_CTRL_VQ);
447         } else {
448                 dev->device_features &= ~(1ull << VIRTIO_NET_F_CTRL_VQ);
449                 /* Also disable features depends on VIRTIO_NET_F_CTRL_VQ */
450                 dev->device_features &= ~(1ull << VIRTIO_NET_F_CTRL_RX);
451                 dev->device_features &= ~(1ull << VIRTIO_NET_F_CTRL_VLAN);
452                 dev->device_features &= ~(1ull << VIRTIO_NET_F_GUEST_ANNOUNCE);
453                 dev->device_features &= ~(1ull << VIRTIO_NET_F_MQ);
454                 dev->device_features &= ~(1ull << VIRTIO_NET_F_CTRL_MAC_ADDR);
455                 dev->unsupported_features |= (1ull << VIRTIO_NET_F_CTRL_VQ);
456                 dev->unsupported_features |= (1ull << VIRTIO_NET_F_CTRL_RX);
457                 dev->unsupported_features |= (1ull << VIRTIO_NET_F_CTRL_VLAN);
458                 dev->unsupported_features |=
459                         (1ull << VIRTIO_NET_F_GUEST_ANNOUNCE);
460                 dev->unsupported_features |= (1ull << VIRTIO_NET_F_MQ);
461                 dev->unsupported_features |=
462                         (1ull << VIRTIO_NET_F_CTRL_MAC_ADDR);
463         }
464
465         /* The backend will not report this feature, we add it explicitly */
466         if (is_vhost_user_by_type(dev->path))
467                 dev->device_features |= (1ull << VIRTIO_NET_F_STATUS);
468
469         dev->device_features &= VIRTIO_USER_SUPPORTED_FEATURES;
470         dev->unsupported_features |= ~VIRTIO_USER_SUPPORTED_FEATURES;
471
472         if (rte_mem_event_callback_register(VIRTIO_USER_MEM_EVENT_CLB_NAME,
473                                 virtio_user_mem_event_cb, dev)) {
474                 if (rte_errno != ENOTSUP) {
475                         PMD_INIT_LOG(ERR, "Failed to register mem event"
476                                         " callback\n");
477                         return -1;
478                 }
479         }
480
481         return 0;
482 }
483
484 void
485 virtio_user_dev_uninit(struct virtio_user_dev *dev)
486 {
487         uint32_t i;
488
489         virtio_user_stop_device(dev);
490
491         rte_mem_event_callback_unregister(VIRTIO_USER_MEM_EVENT_CLB_NAME, dev);
492
493         for (i = 0; i < dev->max_queue_pairs * 2; ++i) {
494                 close(dev->callfds[i]);
495                 close(dev->kickfds[i]);
496         }
497
498         close(dev->vhostfd);
499
500         if (dev->is_server && dev->listenfd >= 0) {
501                 close(dev->listenfd);
502                 dev->listenfd = -1;
503         }
504
505         if (dev->vhostfds) {
506                 for (i = 0; i < dev->max_queue_pairs; ++i)
507                         close(dev->vhostfds[i]);
508                 free(dev->vhostfds);
509                 free(dev->tapfds);
510         }
511
512         free(dev->ifname);
513
514         if (dev->is_server)
515                 unlink(dev->path);
516 }
517
518 uint8_t
519 virtio_user_handle_mq(struct virtio_user_dev *dev, uint16_t q_pairs)
520 {
521         uint16_t i;
522         uint8_t ret = 0;
523
524         if (q_pairs > dev->max_queue_pairs) {
525                 PMD_INIT_LOG(ERR, "multi-q config %u, but only %u supported",
526                              q_pairs, dev->max_queue_pairs);
527                 return -1;
528         }
529
530         /* Server mode can't enable queue pairs if vhostfd is invalid,
531          * always return 0 in this case.
532          */
533         if (dev->vhostfd >= 0) {
534                 for (i = 0; i < q_pairs; ++i)
535                         ret |= dev->ops->enable_qp(dev, i, 1);
536                 for (i = q_pairs; i < dev->max_queue_pairs; ++i)
537                         ret |= dev->ops->enable_qp(dev, i, 0);
538         } else if (!dev->is_server) {
539                 ret = ~0;
540         }
541         dev->queue_pairs = q_pairs;
542
543         return ret;
544 }
545
546 static uint32_t
547 virtio_user_handle_ctrl_msg(struct virtio_user_dev *dev, struct vring *vring,
548                             uint16_t idx_hdr)
549 {
550         struct virtio_net_ctrl_hdr *hdr;
551         virtio_net_ctrl_ack status = ~0;
552         uint16_t i, idx_data, idx_status;
553         uint32_t n_descs = 0;
554
555         /* locate desc for header, data, and status */
556         idx_data = vring->desc[idx_hdr].next;
557         n_descs++;
558
559         i = idx_data;
560         while (vring->desc[i].flags == VRING_DESC_F_NEXT) {
561                 i = vring->desc[i].next;
562                 n_descs++;
563         }
564
565         /* locate desc for status */
566         idx_status = i;
567         n_descs++;
568
569         hdr = (void *)(uintptr_t)vring->desc[idx_hdr].addr;
570         if (hdr->class == VIRTIO_NET_CTRL_MQ &&
571             hdr->cmd == VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) {
572                 uint16_t queues;
573
574                 queues = *(uint16_t *)(uintptr_t)vring->desc[idx_data].addr;
575                 status = virtio_user_handle_mq(dev, queues);
576         }
577
578         /* Update status */
579         *(virtio_net_ctrl_ack *)(uintptr_t)vring->desc[idx_status].addr = status;
580
581         return n_descs;
582 }
583
584 void
585 virtio_user_handle_cq(struct virtio_user_dev *dev, uint16_t queue_idx)
586 {
587         uint16_t avail_idx, desc_idx;
588         struct vring_used_elem *uep;
589         uint32_t n_descs;
590         struct vring *vring = &dev->vrings[queue_idx];
591
592         /* Consume avail ring, using used ring idx as first one */
593         while (vring->used->idx != vring->avail->idx) {
594                 avail_idx = (vring->used->idx) & (vring->num - 1);
595                 desc_idx = vring->avail->ring[avail_idx];
596
597                 n_descs = virtio_user_handle_ctrl_msg(dev, vring, desc_idx);
598
599                 /* Update used ring */
600                 uep = &vring->used->ring[avail_idx];
601                 uep->id = avail_idx;
602                 uep->len = n_descs;
603
604                 vring->used->idx++;
605         }
606 }