New upstream version 18.02
[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 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 virtio_user_start_device(struct virtio_user_dev *dev)
98 {
99         uint64_t features;
100         int ret;
101
102         /* Do not check return as already done in init, or reset in stop */
103         dev->ops->send_request(dev, VHOST_USER_SET_OWNER, NULL);
104
105         /* Step 0: tell vhost to create queues */
106         if (virtio_user_queue_setup(dev, virtio_user_create_queue) < 0)
107                 goto error;
108
109         /* Step 1: set features */
110         features = dev->features;
111         /* Strip VIRTIO_NET_F_MAC, as MAC address is handled in vdev init */
112         features &= ~(1ull << VIRTIO_NET_F_MAC);
113         /* Strip VIRTIO_NET_F_CTRL_VQ, as devices do not really need to know */
114         features &= ~(1ull << VIRTIO_NET_F_CTRL_VQ);
115         features &= ~(1ull << VIRTIO_NET_F_STATUS);
116         ret = dev->ops->send_request(dev, VHOST_USER_SET_FEATURES, &features);
117         if (ret < 0)
118                 goto error;
119         PMD_DRV_LOG(INFO, "set features: %" PRIx64, features);
120
121         /* Step 2: share memory regions */
122         ret = dev->ops->send_request(dev, VHOST_USER_SET_MEM_TABLE, NULL);
123         if (ret < 0)
124                 goto error;
125
126         /* Step 3: kick queues */
127         if (virtio_user_queue_setup(dev, virtio_user_kick_queue) < 0)
128                 goto error;
129
130         /* Step 4: enable queues
131          * we enable the 1st queue pair by default.
132          */
133         dev->ops->enable_qp(dev, 0, 1);
134
135         return 0;
136 error:
137         /* TODO: free resource here or caller to check */
138         return -1;
139 }
140
141 int virtio_user_stop_device(struct virtio_user_dev *dev)
142 {
143         uint32_t i;
144
145         for (i = 0; i < dev->max_queue_pairs; ++i)
146                 dev->ops->enable_qp(dev, i, 0);
147
148         if (dev->ops->send_request(dev, VHOST_USER_RESET_OWNER, NULL) < 0) {
149                 PMD_DRV_LOG(INFO, "Failed to reset the device\n");
150                 return -1;
151         }
152
153         return 0;
154 }
155
156 static inline void
157 parse_mac(struct virtio_user_dev *dev, const char *mac)
158 {
159         int i, r;
160         uint32_t tmp[ETHER_ADDR_LEN];
161
162         if (!mac)
163                 return;
164
165         r = sscanf(mac, "%x:%x:%x:%x:%x:%x", &tmp[0],
166                         &tmp[1], &tmp[2], &tmp[3], &tmp[4], &tmp[5]);
167         if (r == ETHER_ADDR_LEN) {
168                 for (i = 0; i < ETHER_ADDR_LEN; ++i)
169                         dev->mac_addr[i] = (uint8_t)tmp[i];
170                 dev->mac_specified = 1;
171         } else {
172                 /* ignore the wrong mac, use random mac */
173                 PMD_DRV_LOG(ERR, "wrong format of mac: %s", mac);
174         }
175 }
176
177 int
178 is_vhost_user_by_type(const char *path)
179 {
180         struct stat sb;
181
182         if (stat(path, &sb) == -1)
183                 return 0;
184
185         return S_ISSOCK(sb.st_mode);
186 }
187
188 static int
189 virtio_user_dev_init_notify(struct virtio_user_dev *dev)
190 {
191         uint32_t i, j;
192         int callfd;
193         int kickfd;
194
195         for (i = 0; i < VIRTIO_MAX_VIRTQUEUES; ++i) {
196                 if (i >= dev->max_queue_pairs * 2) {
197                         dev->kickfds[i] = -1;
198                         dev->callfds[i] = -1;
199                         continue;
200                 }
201
202                 /* May use invalid flag, but some backend uses kickfd and
203                  * callfd as criteria to judge if dev is alive. so finally we
204                  * use real event_fd.
205                  */
206                 callfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
207                 if (callfd < 0) {
208                         PMD_DRV_LOG(ERR, "callfd error, %s", strerror(errno));
209                         break;
210                 }
211                 kickfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
212                 if (kickfd < 0) {
213                         PMD_DRV_LOG(ERR, "kickfd error, %s", strerror(errno));
214                         break;
215                 }
216                 dev->callfds[i] = callfd;
217                 dev->kickfds[i] = kickfd;
218         }
219
220         if (i < VIRTIO_MAX_VIRTQUEUES) {
221                 for (j = 0; j <= i; ++j) {
222                         close(dev->callfds[j]);
223                         close(dev->kickfds[j]);
224                 }
225
226                 return -1;
227         }
228
229         return 0;
230 }
231
232 static int
233 virtio_user_fill_intr_handle(struct virtio_user_dev *dev)
234 {
235         uint32_t i;
236         struct rte_eth_dev *eth_dev = &rte_eth_devices[dev->port_id];
237
238         if (!eth_dev->intr_handle) {
239                 eth_dev->intr_handle = malloc(sizeof(*eth_dev->intr_handle));
240                 if (!eth_dev->intr_handle) {
241                         PMD_DRV_LOG(ERR, "fail to allocate intr_handle");
242                         return -1;
243                 }
244                 memset(eth_dev->intr_handle, 0, sizeof(*eth_dev->intr_handle));
245         }
246
247         for (i = 0; i < dev->max_queue_pairs; ++i)
248                 eth_dev->intr_handle->efds[i] = dev->callfds[i];
249         eth_dev->intr_handle->nb_efd = dev->max_queue_pairs;
250         eth_dev->intr_handle->max_intr = dev->max_queue_pairs + 1;
251         eth_dev->intr_handle->type = RTE_INTR_HANDLE_VDEV;
252         /* For virtio vdev, no need to read counter for clean */
253         eth_dev->intr_handle->efd_counter_size = 0;
254         eth_dev->intr_handle->fd = -1;
255         if (dev->vhostfd >= 0)
256                 eth_dev->intr_handle->fd = dev->vhostfd;
257
258         return 0;
259 }
260
261 static int
262 virtio_user_dev_setup(struct virtio_user_dev *dev)
263 {
264         uint32_t q;
265
266         dev->vhostfd = -1;
267         dev->vhostfds = NULL;
268         dev->tapfds = NULL;
269
270         if (is_vhost_user_by_type(dev->path)) {
271                 dev->ops = &ops_user;
272         } else {
273                 dev->ops = &ops_kernel;
274
275                 dev->vhostfds = malloc(dev->max_queue_pairs * sizeof(int));
276                 dev->tapfds = malloc(dev->max_queue_pairs * sizeof(int));
277                 if (!dev->vhostfds || !dev->tapfds) {
278                         PMD_INIT_LOG(ERR, "Failed to malloc");
279                         return -1;
280                 }
281
282                 for (q = 0; q < dev->max_queue_pairs; ++q) {
283                         dev->vhostfds[q] = -1;
284                         dev->tapfds[q] = -1;
285                 }
286         }
287
288         if (dev->ops->setup(dev) < 0)
289                 return -1;
290
291         if (virtio_user_dev_init_notify(dev) < 0)
292                 return -1;
293
294         if (virtio_user_fill_intr_handle(dev) < 0)
295                 return -1;
296
297         return 0;
298 }
299
300 /* Use below macro to filter features from vhost backend */
301 #define VIRTIO_USER_SUPPORTED_FEATURES                  \
302         (1ULL << VIRTIO_NET_F_MAC               |       \
303          1ULL << VIRTIO_NET_F_STATUS            |       \
304          1ULL << VIRTIO_NET_F_MQ                |       \
305          1ULL << VIRTIO_NET_F_CTRL_MAC_ADDR     |       \
306          1ULL << VIRTIO_NET_F_CTRL_VQ           |       \
307          1ULL << VIRTIO_NET_F_CTRL_RX           |       \
308          1ULL << VIRTIO_NET_F_CTRL_VLAN         |       \
309          1ULL << VIRTIO_NET_F_CSUM              |       \
310          1ULL << VIRTIO_NET_F_HOST_TSO4         |       \
311          1ULL << VIRTIO_NET_F_HOST_TSO6         |       \
312          1ULL << VIRTIO_NET_F_MRG_RXBUF         |       \
313          1ULL << VIRTIO_RING_F_INDIRECT_DESC    |       \
314          1ULL << VIRTIO_NET_F_GUEST_CSUM        |       \
315          1ULL << VIRTIO_NET_F_GUEST_TSO4        |       \
316          1ULL << VIRTIO_NET_F_GUEST_TSO6        |       \
317          1ULL << VIRTIO_F_VERSION_1)
318
319 int
320 virtio_user_dev_init(struct virtio_user_dev *dev, char *path, int queues,
321                      int cq, int queue_size, const char *mac, char **ifname)
322 {
323         snprintf(dev->path, PATH_MAX, "%s", path);
324         dev->max_queue_pairs = queues;
325         dev->queue_pairs = 1; /* mq disabled by default */
326         dev->queue_size = queue_size;
327         dev->mac_specified = 0;
328         parse_mac(dev, mac);
329
330         if (*ifname) {
331                 dev->ifname = *ifname;
332                 *ifname = NULL;
333         }
334
335         if (virtio_user_dev_setup(dev) < 0) {
336                 PMD_INIT_LOG(ERR, "backend set up fails");
337                 return -1;
338         }
339
340         if (dev->ops->send_request(dev, VHOST_USER_SET_OWNER, NULL) < 0) {
341                 PMD_INIT_LOG(ERR, "set_owner fails: %s", strerror(errno));
342                 return -1;
343         }
344
345         if (dev->ops->send_request(dev, VHOST_USER_GET_FEATURES,
346                             &dev->device_features) < 0) {
347                 PMD_INIT_LOG(ERR, "get_features failed: %s", strerror(errno));
348                 return -1;
349         }
350         if (dev->mac_specified)
351                 dev->device_features |= (1ull << VIRTIO_NET_F_MAC);
352
353         if (cq) {
354                 /* device does not really need to know anything about CQ,
355                  * so if necessary, we just claim to support CQ
356                  */
357                 dev->device_features |= (1ull << VIRTIO_NET_F_CTRL_VQ);
358         } else {
359                 dev->device_features &= ~(1ull << VIRTIO_NET_F_CTRL_VQ);
360                 /* Also disable features depends on VIRTIO_NET_F_CTRL_VQ */
361                 dev->device_features &= ~(1ull << VIRTIO_NET_F_CTRL_RX);
362                 dev->device_features &= ~(1ull << VIRTIO_NET_F_CTRL_VLAN);
363                 dev->device_features &= ~(1ull << VIRTIO_NET_F_GUEST_ANNOUNCE);
364                 dev->device_features &= ~(1ull << VIRTIO_NET_F_MQ);
365                 dev->device_features &= ~(1ull << VIRTIO_NET_F_CTRL_MAC_ADDR);
366         }
367
368         /* The backend will not report this feature, we add it explicitly */
369         if (is_vhost_user_by_type(dev->path))
370                 dev->device_features |= (1ull << VIRTIO_NET_F_STATUS);
371
372         dev->device_features &= VIRTIO_USER_SUPPORTED_FEATURES;
373
374         return 0;
375 }
376
377 void
378 virtio_user_dev_uninit(struct virtio_user_dev *dev)
379 {
380         uint32_t i;
381
382         virtio_user_stop_device(dev);
383
384         for (i = 0; i < dev->max_queue_pairs * 2; ++i) {
385                 close(dev->callfds[i]);
386                 close(dev->kickfds[i]);
387         }
388
389         close(dev->vhostfd);
390
391         if (dev->vhostfds) {
392                 for (i = 0; i < dev->max_queue_pairs; ++i)
393                         close(dev->vhostfds[i]);
394                 free(dev->vhostfds);
395                 free(dev->tapfds);
396         }
397
398         free(dev->ifname);
399 }
400
401 static uint8_t
402 virtio_user_handle_mq(struct virtio_user_dev *dev, uint16_t q_pairs)
403 {
404         uint16_t i;
405         uint8_t ret = 0;
406
407         if (q_pairs > dev->max_queue_pairs) {
408                 PMD_INIT_LOG(ERR, "multi-q config %u, but only %u supported",
409                              q_pairs, dev->max_queue_pairs);
410                 return -1;
411         }
412
413         for (i = 0; i < q_pairs; ++i)
414                 ret |= dev->ops->enable_qp(dev, i, 1);
415         for (i = q_pairs; i < dev->max_queue_pairs; ++i)
416                 ret |= dev->ops->enable_qp(dev, i, 0);
417
418         dev->queue_pairs = q_pairs;
419
420         return ret;
421 }
422
423 static uint32_t
424 virtio_user_handle_ctrl_msg(struct virtio_user_dev *dev, struct vring *vring,
425                             uint16_t idx_hdr)
426 {
427         struct virtio_net_ctrl_hdr *hdr;
428         virtio_net_ctrl_ack status = ~0;
429         uint16_t i, idx_data, idx_status;
430         uint32_t n_descs = 0;
431
432         /* locate desc for header, data, and status */
433         idx_data = vring->desc[idx_hdr].next;
434         n_descs++;
435
436         i = idx_data;
437         while (vring->desc[i].flags == VRING_DESC_F_NEXT) {
438                 i = vring->desc[i].next;
439                 n_descs++;
440         }
441
442         /* locate desc for status */
443         idx_status = i;
444         n_descs++;
445
446         hdr = (void *)(uintptr_t)vring->desc[idx_hdr].addr;
447         if (hdr->class == VIRTIO_NET_CTRL_MQ &&
448             hdr->cmd == VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) {
449                 uint16_t queues;
450
451                 queues = *(uint16_t *)(uintptr_t)vring->desc[idx_data].addr;
452                 status = virtio_user_handle_mq(dev, queues);
453         }
454
455         /* Update status */
456         *(virtio_net_ctrl_ack *)(uintptr_t)vring->desc[idx_status].addr = status;
457
458         return n_descs;
459 }
460
461 void
462 virtio_user_handle_cq(struct virtio_user_dev *dev, uint16_t queue_idx)
463 {
464         uint16_t avail_idx, desc_idx;
465         struct vring_used_elem *uep;
466         uint32_t n_descs;
467         struct vring *vring = &dev->vrings[queue_idx];
468
469         /* Consume avail ring, using used ring idx as first one */
470         while (vring->used->idx != vring->avail->idx) {
471                 avail_idx = (vring->used->idx) & (vring->num - 1);
472                 desc_idx = vring->avail->ring[avail_idx];
473
474                 n_descs = virtio_user_handle_ctrl_msg(dev, vring, desc_idx);
475
476                 /* Update used ring */
477                 uep = &vring->used->ring[avail_idx];
478                 uep->id = avail_idx;
479                 uep->len = n_descs;
480
481                 vring->used->idx++;
482         }
483 }