net/virtio-user: support to report net status
[dpdk.git] / drivers / net / virtio / virtio_user / virtio_user_dev.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <stdint.h>
35 #include <stdio.h>
36 #include <fcntl.h>
37 #include <string.h>
38 #include <errno.h>
39 #include <sys/mman.h>
40 #include <unistd.h>
41 #include <sys/eventfd.h>
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <unistd.h>
45
46 #include "vhost.h"
47 #include "virtio_user_dev.h"
48 #include "../virtio_ethdev.h"
49
50 static int
51 virtio_user_create_queue(struct virtio_user_dev *dev, uint32_t queue_sel)
52 {
53         /* Of all per virtqueue MSGs, make sure VHOST_SET_VRING_CALL come
54          * firstly because vhost depends on this msg to allocate virtqueue
55          * pair.
56          */
57         struct vhost_vring_file file;
58
59         file.index = queue_sel;
60         file.fd = dev->callfds[queue_sel];
61         dev->ops->send_request(dev, VHOST_USER_SET_VRING_CALL, &file);
62
63         return 0;
64 }
65
66 static int
67 virtio_user_kick_queue(struct virtio_user_dev *dev, uint32_t queue_sel)
68 {
69         struct vhost_vring_file file;
70         struct vhost_vring_state state;
71         struct vring *vring = &dev->vrings[queue_sel];
72         struct vhost_vring_addr addr = {
73                 .index = queue_sel,
74                 .desc_user_addr = (uint64_t)(uintptr_t)vring->desc,
75                 .avail_user_addr = (uint64_t)(uintptr_t)vring->avail,
76                 .used_user_addr = (uint64_t)(uintptr_t)vring->used,
77                 .log_guest_addr = 0,
78                 .flags = 0, /* disable log */
79         };
80
81         state.index = queue_sel;
82         state.num = vring->num;
83         dev->ops->send_request(dev, VHOST_USER_SET_VRING_NUM, &state);
84
85         state.index = queue_sel;
86         state.num = 0; /* no reservation */
87         dev->ops->send_request(dev, VHOST_USER_SET_VRING_BASE, &state);
88
89         dev->ops->send_request(dev, VHOST_USER_SET_VRING_ADDR, &addr);
90
91         /* Of all per virtqueue MSGs, make sure VHOST_USER_SET_VRING_KICK comes
92          * lastly because vhost depends on this msg to judge if
93          * virtio is ready.
94          */
95         file.index = queue_sel;
96         file.fd = dev->kickfds[queue_sel];
97         dev->ops->send_request(dev, VHOST_USER_SET_VRING_KICK, &file);
98
99         return 0;
100 }
101
102 static int
103 virtio_user_queue_setup(struct virtio_user_dev *dev,
104                         int (*fn)(struct virtio_user_dev *, uint32_t))
105 {
106         uint32_t i, queue_sel;
107
108         for (i = 0; i < dev->max_queue_pairs; ++i) {
109                 queue_sel = 2 * i + VTNET_SQ_RQ_QUEUE_IDX;
110                 if (fn(dev, queue_sel) < 0) {
111                         PMD_DRV_LOG(INFO, "setup rx vq fails: %u", i);
112                         return -1;
113                 }
114         }
115         for (i = 0; i < dev->max_queue_pairs; ++i) {
116                 queue_sel = 2 * i + VTNET_SQ_TQ_QUEUE_IDX;
117                 if (fn(dev, queue_sel) < 0) {
118                         PMD_DRV_LOG(INFO, "setup tx vq fails: %u", i);
119                         return -1;
120                 }
121         }
122
123         return 0;
124 }
125
126 int
127 virtio_user_start_device(struct virtio_user_dev *dev)
128 {
129         uint64_t features;
130         int ret;
131
132         /* Step 0: tell vhost to create queues */
133         if (virtio_user_queue_setup(dev, virtio_user_create_queue) < 0)
134                 goto error;
135
136         /* Step 1: set features */
137         features = dev->features;
138         /* Strip VIRTIO_NET_F_MAC, as MAC address is handled in vdev init */
139         features &= ~(1ull << VIRTIO_NET_F_MAC);
140         /* Strip VIRTIO_NET_F_CTRL_VQ, as devices do not really need to know */
141         features &= ~(1ull << VIRTIO_NET_F_CTRL_VQ);
142         features &= ~(1ull << VIRTIO_NET_F_STATUS);
143         ret = dev->ops->send_request(dev, VHOST_USER_SET_FEATURES, &features);
144         if (ret < 0)
145                 goto error;
146         PMD_DRV_LOG(INFO, "set features: %" PRIx64, features);
147
148         /* Step 2: share memory regions */
149         ret = dev->ops->send_request(dev, VHOST_USER_SET_MEM_TABLE, NULL);
150         if (ret < 0)
151                 goto error;
152
153         /* Step 3: kick queues */
154         if (virtio_user_queue_setup(dev, virtio_user_kick_queue) < 0)
155                 goto error;
156
157         /* Step 4: enable queues
158          * we enable the 1st queue pair by default.
159          */
160         dev->ops->enable_qp(dev, 0, 1);
161
162         return 0;
163 error:
164         /* TODO: free resource here or caller to check */
165         return -1;
166 }
167
168 int virtio_user_stop_device(struct virtio_user_dev *dev)
169 {
170         uint32_t i;
171
172         for (i = 0; i < dev->max_queue_pairs; ++i)
173                 dev->ops->enable_qp(dev, i, 0);
174
175         return 0;
176 }
177
178 static inline void
179 parse_mac(struct virtio_user_dev *dev, const char *mac)
180 {
181         int i, r;
182         uint32_t tmp[ETHER_ADDR_LEN];
183
184         if (!mac)
185                 return;
186
187         r = sscanf(mac, "%x:%x:%x:%x:%x:%x", &tmp[0],
188                         &tmp[1], &tmp[2], &tmp[3], &tmp[4], &tmp[5]);
189         if (r == ETHER_ADDR_LEN) {
190                 for (i = 0; i < ETHER_ADDR_LEN; ++i)
191                         dev->mac_addr[i] = (uint8_t)tmp[i];
192                 dev->mac_specified = 1;
193         } else {
194                 /* ignore the wrong mac, use random mac */
195                 PMD_DRV_LOG(ERR, "wrong format of mac: %s", mac);
196         }
197 }
198
199 int
200 is_vhost_user_by_type(const char *path)
201 {
202         struct stat sb;
203
204         if (stat(path, &sb) == -1)
205                 return 0;
206
207         return S_ISSOCK(sb.st_mode);
208 }
209
210 static int
211 virtio_user_dev_init_notify(struct virtio_user_dev *dev)
212 {
213         uint32_t i, j;
214         int callfd;
215         int kickfd;
216
217         for (i = 0; i < VIRTIO_MAX_VIRTQUEUES; ++i) {
218                 if (i >= dev->max_queue_pairs * 2) {
219                         dev->kickfds[i] = -1;
220                         dev->callfds[i] = -1;
221                         continue;
222                 }
223
224                 /* May use invalid flag, but some backend uses kickfd and
225                  * callfd as criteria to judge if dev is alive. so finally we
226                  * use real event_fd.
227                  */
228                 callfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
229                 if (callfd < 0) {
230                         PMD_DRV_LOG(ERR, "callfd error, %s", strerror(errno));
231                         break;
232                 }
233                 kickfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
234                 if (kickfd < 0) {
235                         PMD_DRV_LOG(ERR, "kickfd error, %s", strerror(errno));
236                         break;
237                 }
238                 dev->callfds[i] = callfd;
239                 dev->kickfds[i] = kickfd;
240         }
241
242         if (i < VIRTIO_MAX_VIRTQUEUES) {
243                 for (j = 0; j <= i; ++j) {
244                         close(dev->callfds[j]);
245                         close(dev->kickfds[j]);
246                 }
247
248                 return -1;
249         }
250
251         return 0;
252 }
253
254 static int
255 virtio_user_fill_intr_handle(struct virtio_user_dev *dev)
256 {
257         uint32_t i;
258         struct rte_eth_dev *eth_dev = &rte_eth_devices[dev->port_id];
259
260         if (!eth_dev->intr_handle) {
261                 eth_dev->intr_handle = malloc(sizeof(*eth_dev->intr_handle));
262                 if (!eth_dev->intr_handle) {
263                         PMD_DRV_LOG(ERR, "fail to allocate intr_handle");
264                         return -1;
265                 }
266                 memset(eth_dev->intr_handle, 0, sizeof(*eth_dev->intr_handle));
267         }
268
269         for (i = 0; i < dev->max_queue_pairs; ++i)
270                 eth_dev->intr_handle->efds[i] = dev->callfds[i];
271         eth_dev->intr_handle->nb_efd = dev->max_queue_pairs;
272         eth_dev->intr_handle->max_intr = dev->max_queue_pairs + 1;
273         eth_dev->intr_handle->type = RTE_INTR_HANDLE_VDEV;
274
275         return 0;
276 }
277
278 static int
279 virtio_user_dev_setup(struct virtio_user_dev *dev)
280 {
281         uint32_t q;
282
283         dev->vhostfd = -1;
284         dev->vhostfds = NULL;
285         dev->tapfds = NULL;
286
287         if (virtio_user_dev_init_notify(dev) < 0)
288                 return -1;
289
290         if (virtio_user_fill_intr_handle(dev) < 0)
291                 return -1;
292
293         if (is_vhost_user_by_type(dev->path)) {
294                 dev->ops = &ops_user;
295         } else {
296                 dev->ops = &ops_kernel;
297
298                 dev->vhostfds = malloc(dev->max_queue_pairs * sizeof(int));
299                 dev->tapfds = malloc(dev->max_queue_pairs * sizeof(int));
300                 if (!dev->vhostfds || !dev->tapfds) {
301                         PMD_INIT_LOG(ERR, "Failed to malloc");
302                         return -1;
303                 }
304
305                 for (q = 0; q < dev->max_queue_pairs; ++q) {
306                         dev->vhostfds[q] = -1;
307                         dev->tapfds[q] = -1;
308                 }
309         }
310
311         return dev->ops->setup(dev);
312 }
313
314 int
315 virtio_user_dev_init(struct virtio_user_dev *dev, char *path, int queues,
316                      int cq, int queue_size, const char *mac, char **ifname)
317 {
318         snprintf(dev->path, PATH_MAX, "%s", path);
319         dev->max_queue_pairs = queues;
320         dev->queue_pairs = 1; /* mq disabled by default */
321         dev->queue_size = queue_size;
322         dev->mac_specified = 0;
323         parse_mac(dev, mac);
324
325         if (*ifname) {
326                 dev->ifname = *ifname;
327                 *ifname = NULL;
328         }
329
330         if (virtio_user_dev_setup(dev) < 0) {
331                 PMD_INIT_LOG(ERR, "backend set up fails");
332                 return -1;
333         }
334         if (dev->ops->send_request(dev, VHOST_USER_SET_OWNER, NULL) < 0) {
335                 PMD_INIT_LOG(ERR, "set_owner fails: %s", strerror(errno));
336                 return -1;
337         }
338
339         if (dev->ops->send_request(dev, VHOST_USER_GET_FEATURES,
340                             &dev->device_features) < 0) {
341                 PMD_INIT_LOG(ERR, "get_features failed: %s", strerror(errno));
342                 return -1;
343         }
344         if (dev->mac_specified)
345                 dev->device_features |= (1ull << VIRTIO_NET_F_MAC);
346
347         if (cq) {
348                 /* device does not really need to know anything about CQ,
349                  * so if necessary, we just claim to support CQ
350                  */
351                 dev->device_features |= (1ull << VIRTIO_NET_F_CTRL_VQ);
352         } else {
353                 dev->device_features &= ~(1ull << VIRTIO_NET_F_CTRL_VQ);
354                 /* Also disable features depends on VIRTIO_NET_F_CTRL_VQ */
355                 dev->device_features &= ~(1ull << VIRTIO_NET_F_CTRL_RX);
356                 dev->device_features &= ~(1ull << VIRTIO_NET_F_CTRL_VLAN);
357                 dev->device_features &= ~(1ull << VIRTIO_NET_F_GUEST_ANNOUNCE);
358                 dev->device_features &= ~(1ull << VIRTIO_NET_F_MQ);
359                 dev->device_features &= ~(1ull << VIRTIO_NET_F_CTRL_MAC_ADDR);
360         }
361
362         /* The backend will not report this feature, we add it explicitly */
363         dev->device_features |= (1ull << VIRTIO_NET_F_STATUS);
364
365         return 0;
366 }
367
368 void
369 virtio_user_dev_uninit(struct virtio_user_dev *dev)
370 {
371         uint32_t i;
372
373         virtio_user_stop_device(dev);
374
375         for (i = 0; i < dev->max_queue_pairs * 2; ++i) {
376                 close(dev->callfds[i]);
377                 close(dev->kickfds[i]);
378         }
379
380         close(dev->vhostfd);
381
382         if (dev->vhostfds) {
383                 for (i = 0; i < dev->max_queue_pairs; ++i)
384                         close(dev->vhostfds[i]);
385                 free(dev->vhostfds);
386                 free(dev->tapfds);
387         }
388
389         free(dev->ifname);
390 }
391
392 static uint8_t
393 virtio_user_handle_mq(struct virtio_user_dev *dev, uint16_t q_pairs)
394 {
395         uint16_t i;
396         uint8_t ret = 0;
397
398         if (q_pairs > dev->max_queue_pairs) {
399                 PMD_INIT_LOG(ERR, "multi-q config %u, but only %u supported",
400                              q_pairs, dev->max_queue_pairs);
401                 return -1;
402         }
403
404         for (i = 0; i < q_pairs; ++i)
405                 ret |= dev->ops->enable_qp(dev, i, 1);
406         for (i = q_pairs; i < dev->max_queue_pairs; ++i)
407                 ret |= dev->ops->enable_qp(dev, i, 0);
408
409         dev->queue_pairs = q_pairs;
410
411         return ret;
412 }
413
414 static uint32_t
415 virtio_user_handle_ctrl_msg(struct virtio_user_dev *dev, struct vring *vring,
416                             uint16_t idx_hdr)
417 {
418         struct virtio_net_ctrl_hdr *hdr;
419         virtio_net_ctrl_ack status = ~0;
420         uint16_t i, idx_data, idx_status;
421         uint32_t n_descs = 0;
422
423         /* locate desc for header, data, and status */
424         idx_data = vring->desc[idx_hdr].next;
425         n_descs++;
426
427         i = idx_data;
428         while (vring->desc[i].flags == VRING_DESC_F_NEXT) {
429                 i = vring->desc[i].next;
430                 n_descs++;
431         }
432
433         /* locate desc for status */
434         idx_status = i;
435         n_descs++;
436
437         hdr = (void *)(uintptr_t)vring->desc[idx_hdr].addr;
438         if (hdr->class == VIRTIO_NET_CTRL_MQ &&
439             hdr->cmd == VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) {
440                 uint16_t queues;
441
442                 queues = *(uint16_t *)(uintptr_t)vring->desc[idx_data].addr;
443                 status = virtio_user_handle_mq(dev, queues);
444         }
445
446         /* Update status */
447         *(virtio_net_ctrl_ack *)(uintptr_t)vring->desc[idx_status].addr = status;
448
449         return n_descs;
450 }
451
452 void
453 virtio_user_handle_cq(struct virtio_user_dev *dev, uint16_t queue_idx)
454 {
455         uint16_t avail_idx, desc_idx;
456         struct vring_used_elem *uep;
457         uint32_t n_descs;
458         struct vring *vring = &dev->vrings[queue_idx];
459
460         /* Consume avail ring, using used ring idx as first one */
461         while (vring->used->idx != vring->avail->idx) {
462                 avail_idx = (vring->used->idx) & (vring->num - 1);
463                 desc_idx = vring->avail->ring[avail_idx];
464
465                 n_descs = virtio_user_handle_ctrl_msg(dev, vring, desc_idx);
466
467                 /* Update used ring */
468                 uep = &vring->used->ring[avail_idx];
469                 uep->id = avail_idx;
470                 uep->len = n_descs;
471
472                 vring->used->idx++;
473         }
474 }