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