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