xref: /openbmc/qemu/net/vhost-vdpa.c (revision 40f962ffeba627cd65121a7b2883f145e79c9c4c)
1 /*
2  * vhost-vdpa.c
3  *
4  * Copyright(c) 2017-2018 Intel Corporation.
5  * Copyright(c) 2020 Red Hat, Inc.
6  *
7  * This work is licensed under the terms of the GNU GPL, version 2 or later.
8  * See the COPYING file in the top-level directory.
9  *
10  */
11 
12 #include "qemu/osdep.h"
13 #include "clients.h"
14 #include "net/vhost_net.h"
15 #include "net/vhost-vdpa.h"
16 #include "hw/virtio/vhost-vdpa.h"
17 #include "qemu/config-file.h"
18 #include "qemu/error-report.h"
19 #include "qemu/option.h"
20 #include "qapi/error.h"
21 #include <sys/ioctl.h>
22 #include <err.h>
23 #include "standard-headers/linux/virtio_net.h"
24 #include "monitor/monitor.h"
25 #include "hw/virtio/vhost.h"
26 
27 /* Todo:need to add the multiqueue support here */
28 typedef struct VhostVDPAState {
29     NetClientState nc;
30     struct vhost_vdpa vhost_vdpa;
31     VHostNetState *vhost_net;
32     bool started;
33 } VhostVDPAState;
34 
35 const int vdpa_feature_bits[] = {
36     VIRTIO_F_NOTIFY_ON_EMPTY,
37     VIRTIO_RING_F_INDIRECT_DESC,
38     VIRTIO_RING_F_EVENT_IDX,
39     VIRTIO_F_ANY_LAYOUT,
40     VIRTIO_F_VERSION_1,
41     VIRTIO_NET_F_CSUM,
42     VIRTIO_NET_F_GUEST_CSUM,
43     VIRTIO_NET_F_GSO,
44     VIRTIO_NET_F_GUEST_TSO4,
45     VIRTIO_NET_F_GUEST_TSO6,
46     VIRTIO_NET_F_GUEST_ECN,
47     VIRTIO_NET_F_GUEST_UFO,
48     VIRTIO_NET_F_HOST_TSO4,
49     VIRTIO_NET_F_HOST_TSO6,
50     VIRTIO_NET_F_HOST_ECN,
51     VIRTIO_NET_F_HOST_UFO,
52     VIRTIO_NET_F_MRG_RXBUF,
53     VIRTIO_NET_F_MTU,
54     VIRTIO_F_IOMMU_PLATFORM,
55     VIRTIO_F_RING_PACKED,
56     VIRTIO_NET_F_RSS,
57     VIRTIO_NET_F_HASH_REPORT,
58     VIRTIO_NET_F_GUEST_ANNOUNCE,
59     VIRTIO_NET_F_STATUS,
60     VHOST_INVALID_FEATURE_BIT
61 };
62 
63 VHostNetState *vhost_vdpa_get_vhost_net(NetClientState *nc)
64 {
65     VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc);
66     assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
67     return s->vhost_net;
68 }
69 
70 static int vhost_vdpa_net_check_device_id(struct vhost_net *net)
71 {
72     uint32_t device_id;
73     int ret;
74     struct vhost_dev *hdev;
75 
76     hdev = (struct vhost_dev *)&net->dev;
77     ret = hdev->vhost_ops->vhost_get_device_id(hdev, &device_id);
78     if (device_id != VIRTIO_ID_NET) {
79         return -ENOTSUP;
80     }
81     return ret;
82 }
83 
84 static void vhost_vdpa_del(NetClientState *ncs)
85 {
86     VhostVDPAState *s;
87     assert(ncs->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
88     s = DO_UPCAST(VhostVDPAState, nc, ncs);
89     if (s->vhost_net) {
90         vhost_net_cleanup(s->vhost_net);
91     }
92 }
93 
94 static int vhost_vdpa_add(NetClientState *ncs, void *be)
95 {
96     VhostNetOptions options;
97     struct vhost_net *net = NULL;
98     VhostVDPAState *s;
99     int ret;
100 
101     options.backend_type = VHOST_BACKEND_TYPE_VDPA;
102     assert(ncs->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
103     s = DO_UPCAST(VhostVDPAState, nc, ncs);
104     options.net_backend = ncs;
105     options.opaque      = be;
106     options.busyloop_timeout = 0;
107 
108     net = vhost_net_init(&options);
109     if (!net) {
110         error_report("failed to init vhost_net for queue");
111         goto err;
112     }
113     if (s->vhost_net) {
114         vhost_net_cleanup(s->vhost_net);
115         g_free(s->vhost_net);
116     }
117     s->vhost_net = net;
118     ret = vhost_vdpa_net_check_device_id(net);
119     if (ret) {
120         goto err;
121     }
122     return 0;
123 err:
124     if (net) {
125         vhost_net_cleanup(net);
126     }
127     vhost_vdpa_del(ncs);
128     return -1;
129 }
130 
131 static void vhost_vdpa_cleanup(NetClientState *nc)
132 {
133     VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc);
134 
135     if (s->vhost_net) {
136         vhost_net_cleanup(s->vhost_net);
137         g_free(s->vhost_net);
138         s->vhost_net = NULL;
139     }
140      if (s->vhost_vdpa.device_fd >= 0) {
141         qemu_close(s->vhost_vdpa.device_fd);
142         s->vhost_vdpa.device_fd = -1;
143     }
144 }
145 
146 static bool vhost_vdpa_has_vnet_hdr(NetClientState *nc)
147 {
148     assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
149 
150     return true;
151 }
152 
153 static bool vhost_vdpa_has_ufo(NetClientState *nc)
154 {
155     assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
156     VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc);
157     uint64_t features = 0;
158     features |= (1ULL << VIRTIO_NET_F_HOST_UFO);
159     features = vhost_net_get_features(s->vhost_net, features);
160     return !!(features & (1ULL << VIRTIO_NET_F_HOST_UFO));
161 
162 }
163 
164 static NetClientInfo net_vhost_vdpa_info = {
165         .type = NET_CLIENT_DRIVER_VHOST_VDPA,
166         .size = sizeof(VhostVDPAState),
167         .cleanup = vhost_vdpa_cleanup,
168         .has_vnet_hdr = vhost_vdpa_has_vnet_hdr,
169         .has_ufo = vhost_vdpa_has_ufo,
170 };
171 
172 static int net_vhost_vdpa_init(NetClientState *peer, const char *device,
173                                const char *name, const char *vhostdev)
174 {
175     NetClientState *nc = NULL;
176     VhostVDPAState *s;
177     int vdpa_device_fd = -1;
178     int ret = 0;
179     assert(name);
180     nc = qemu_new_net_client(&net_vhost_vdpa_info, peer, device, name);
181     snprintf(nc->info_str, sizeof(nc->info_str), TYPE_VHOST_VDPA);
182     nc->queue_index = 0;
183     s = DO_UPCAST(VhostVDPAState, nc, nc);
184     vdpa_device_fd = qemu_open_old(vhostdev, O_RDWR);
185     if (vdpa_device_fd == -1) {
186         return -errno;
187     }
188     s->vhost_vdpa.device_fd = vdpa_device_fd;
189     ret = vhost_vdpa_add(nc, (void *)&s->vhost_vdpa);
190     assert(s->vhost_net);
191     return ret;
192 }
193 
194 static int net_vhost_check_net(void *opaque, QemuOpts *opts, Error **errp)
195 {
196     const char *name = opaque;
197     const char *driver, *netdev;
198 
199     driver = qemu_opt_get(opts, "driver");
200     netdev = qemu_opt_get(opts, "netdev");
201     if (!driver || !netdev) {
202         return 0;
203     }
204     if (strcmp(netdev, name) == 0 &&
205         !g_str_has_prefix(driver, "virtio-net-")) {
206         error_setg(errp, "vhost-vdpa requires frontend driver virtio-net-*");
207         return -1;
208     }
209     return 0;
210 }
211 
212 int net_init_vhost_vdpa(const Netdev *netdev, const char *name,
213                         NetClientState *peer, Error **errp)
214 {
215     const NetdevVhostVDPAOptions *opts;
216 
217     assert(netdev->type == NET_CLIENT_DRIVER_VHOST_VDPA);
218     opts = &netdev->u.vhost_vdpa;
219     /* verify net frontend */
220     if (qemu_opts_foreach(qemu_find_opts("device"), net_vhost_check_net,
221                           (char *)name, errp)) {
222         return -1;
223     }
224     return net_vhost_vdpa_init(peer, TYPE_VHOST_VDPA, name, opts->vhostdev);
225 }
226