1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26#include "qemu/osdep.h"
27#include <sys/ioctl.h>
28#include <net/if.h>
29#include <sys/mman.h>
30#define NETMAP_WITH_LIBS
31#include <net/netmap.h>
32#include <net/netmap_user.h>
33
34#include "net/net.h"
35#include "net/tap.h"
36#include "clients.h"
37#include "sysemu/sysemu.h"
38#include "qemu/error-report.h"
39#include "qapi/error.h"
40#include "qemu/iov.h"
41#include "qemu/cutils.h"
42
43typedef struct NetmapState {
44 NetClientState nc;
45 struct nm_desc *nmd;
46 char ifname[IFNAMSIZ];
47 struct netmap_ring *tx;
48 struct netmap_ring *rx;
49 bool read_poll;
50 bool write_poll;
51 struct iovec iov[IOV_MAX];
52 int vnet_hdr_len;
53} NetmapState;
54
55#ifndef __FreeBSD__
56#define pkt_copy bcopy
57#else
58
59static inline void
60pkt_copy(const void *_src, void *_dst, int l)
61{
62 const uint64_t *src = _src;
63 uint64_t *dst = _dst;
64 if (unlikely(l >= 1024)) {
65 bcopy(src, dst, l);
66 return;
67 }
68 for (; l > 0; l -= 64) {
69 *dst++ = *src++;
70 *dst++ = *src++;
71 *dst++ = *src++;
72 *dst++ = *src++;
73 *dst++ = *src++;
74 *dst++ = *src++;
75 *dst++ = *src++;
76 *dst++ = *src++;
77 }
78}
79#endif
80
81
82
83
84
85static struct nm_desc *netmap_open(const NetdevNetmapOptions *nm_opts,
86 Error **errp)
87{
88 struct nm_desc *nmd;
89 struct nmreq req;
90
91 memset(&req, 0, sizeof(req));
92
93 nmd = nm_open(nm_opts->ifname, &req, NETMAP_NO_TX_POLL,
94 NULL);
95 if (nmd == NULL) {
96 error_setg_errno(errp, errno, "Failed to nm_open() %s",
97 nm_opts->ifname);
98 return NULL;
99 }
100
101 return nmd;
102}
103
104static void netmap_send(void *opaque);
105static void netmap_writable(void *opaque);
106
107
108static void netmap_update_fd_handler(NetmapState *s)
109{
110 qemu_set_fd_handler(s->nmd->fd,
111 s->read_poll ? netmap_send : NULL,
112 s->write_poll ? netmap_writable : NULL,
113 s);
114}
115
116
117static void netmap_read_poll(NetmapState *s, bool enable)
118{
119 if (s->read_poll != enable) {
120 s->read_poll = enable;
121 netmap_update_fd_handler(s);
122 }
123}
124
125
126static void netmap_write_poll(NetmapState *s, bool enable)
127{
128 if (s->write_poll != enable) {
129 s->write_poll = enable;
130 netmap_update_fd_handler(s);
131 }
132}
133
134static void netmap_poll(NetClientState *nc, bool enable)
135{
136 NetmapState *s = DO_UPCAST(NetmapState, nc, nc);
137
138 if (s->read_poll != enable || s->write_poll != enable) {
139 s->write_poll = enable;
140 s->read_poll = enable;
141 netmap_update_fd_handler(s);
142 }
143}
144
145
146
147
148
149
150static void netmap_writable(void *opaque)
151{
152 NetmapState *s = opaque;
153
154 netmap_write_poll(s, false);
155 qemu_flush_queued_packets(&s->nc);
156}
157
158static ssize_t netmap_receive(NetClientState *nc,
159 const uint8_t *buf, size_t size)
160{
161 NetmapState *s = DO_UPCAST(NetmapState, nc, nc);
162 struct netmap_ring *ring = s->tx;
163 uint32_t i;
164 uint32_t idx;
165 uint8_t *dst;
166
167 if (unlikely(!ring)) {
168
169 return size;
170 }
171
172 if (unlikely(size > ring->nr_buf_size)) {
173 RD(5, "[netmap_receive] drop packet of size %d > %d\n",
174 (int)size, ring->nr_buf_size);
175 return size;
176 }
177
178 if (nm_ring_empty(ring)) {
179
180 netmap_write_poll(s, true);
181 return 0;
182 }
183
184 i = ring->cur;
185 idx = ring->slot[i].buf_idx;
186 dst = (uint8_t *)NETMAP_BUF(ring, idx);
187
188 ring->slot[i].len = size;
189 ring->slot[i].flags = 0;
190 pkt_copy(buf, dst, size);
191 ring->cur = ring->head = nm_ring_next(ring, i);
192 ioctl(s->nmd->fd, NIOCTXSYNC, NULL);
193
194 return size;
195}
196
197static ssize_t netmap_receive_iov(NetClientState *nc,
198 const struct iovec *iov, int iovcnt)
199{
200 NetmapState *s = DO_UPCAST(NetmapState, nc, nc);
201 struct netmap_ring *ring = s->tx;
202 uint32_t last;
203 uint32_t idx;
204 uint8_t *dst;
205 int j;
206 uint32_t i;
207
208 if (unlikely(!ring)) {
209
210 return iov_size(iov, iovcnt);
211 }
212
213 last = i = ring->cur;
214
215 if (nm_ring_space(ring) < iovcnt) {
216
217 netmap_write_poll(s, true);
218 return 0;
219 }
220
221 for (j = 0; j < iovcnt; j++) {
222 int iov_frag_size = iov[j].iov_len;
223 int offset = 0;
224 int nm_frag_size;
225
226
227
228 while (iov_frag_size) {
229 nm_frag_size = MIN(iov_frag_size, ring->nr_buf_size);
230
231 if (unlikely(nm_ring_empty(ring))) {
232
233
234 netmap_write_poll(s, true);
235 return 0;
236 }
237
238 idx = ring->slot[i].buf_idx;
239 dst = (uint8_t *)NETMAP_BUF(ring, idx);
240
241 ring->slot[i].len = nm_frag_size;
242 ring->slot[i].flags = NS_MOREFRAG;
243 pkt_copy(iov[j].iov_base + offset, dst, nm_frag_size);
244
245 last = i;
246 i = nm_ring_next(ring, i);
247
248 offset += nm_frag_size;
249 iov_frag_size -= nm_frag_size;
250 }
251 }
252
253 ring->slot[last].flags &= ~NS_MOREFRAG;
254
255
256 ring->cur = ring->head = i;
257
258 ioctl(s->nmd->fd, NIOCTXSYNC, NULL);
259
260 return iov_size(iov, iovcnt);
261}
262
263
264
265static void netmap_send_completed(NetClientState *nc, ssize_t len)
266{
267 NetmapState *s = DO_UPCAST(NetmapState, nc, nc);
268
269 netmap_read_poll(s, true);
270}
271
272static void netmap_send(void *opaque)
273{
274 NetmapState *s = opaque;
275 struct netmap_ring *ring = s->rx;
276
277
278
279 while (!nm_ring_empty(ring)) {
280 uint32_t i;
281 uint32_t idx;
282 bool morefrag;
283 int iovcnt = 0;
284 int iovsize;
285
286 do {
287 i = ring->cur;
288 idx = ring->slot[i].buf_idx;
289 morefrag = (ring->slot[i].flags & NS_MOREFRAG);
290 s->iov[iovcnt].iov_base = (u_char *)NETMAP_BUF(ring, idx);
291 s->iov[iovcnt].iov_len = ring->slot[i].len;
292 iovcnt++;
293
294 ring->cur = ring->head = nm_ring_next(ring, i);
295 } while (!nm_ring_empty(ring) && morefrag);
296
297 if (unlikely(nm_ring_empty(ring) && morefrag)) {
298 RD(5, "[netmap_send] ran out of slots, with a pending"
299 "incomplete packet\n");
300 }
301
302 iovsize = qemu_sendv_packet_async(&s->nc, s->iov, iovcnt,
303 netmap_send_completed);
304
305 if (iovsize == 0) {
306
307
308
309 netmap_read_poll(s, false);
310 break;
311 }
312 }
313}
314
315
316static void netmap_cleanup(NetClientState *nc)
317{
318 NetmapState *s = DO_UPCAST(NetmapState, nc, nc);
319
320 qemu_purge_queued_packets(nc);
321
322 netmap_poll(nc, false);
323 nm_close(s->nmd);
324 s->nmd = NULL;
325}
326
327
328static int netmap_fd_set_vnet_hdr_len(NetmapState *s, int len)
329{
330 struct nmreq req;
331
332
333
334
335 memset(&req, 0, sizeof(req));
336 pstrcpy(req.nr_name, sizeof(req.nr_name), s->ifname);
337 req.nr_version = NETMAP_API;
338 req.nr_cmd = NETMAP_BDG_VNET_HDR;
339 req.nr_arg1 = len;
340
341 return ioctl(s->nmd->fd, NIOCREGIF, &req);
342}
343
344static bool netmap_has_vnet_hdr_len(NetClientState *nc, int len)
345{
346 NetmapState *s = DO_UPCAST(NetmapState, nc, nc);
347 int prev_len = s->vnet_hdr_len;
348
349
350 if (netmap_fd_set_vnet_hdr_len(s, len)) {
351 return false;
352 }
353
354
355 if (netmap_fd_set_vnet_hdr_len(s, prev_len)) {
356 error_report("Failed to restore vnet-hdr length %d on %s: %s",
357 prev_len, s->ifname, strerror(errno));
358 abort();
359 }
360
361 return true;
362}
363
364
365
366static bool netmap_has_vnet_hdr(NetClientState *nc)
367{
368 return netmap_has_vnet_hdr_len(nc, sizeof(struct virtio_net_hdr));
369}
370
371static void netmap_using_vnet_hdr(NetClientState *nc, bool enable)
372{
373}
374
375static void netmap_set_vnet_hdr_len(NetClientState *nc, int len)
376{
377 NetmapState *s = DO_UPCAST(NetmapState, nc, nc);
378 int err;
379
380 err = netmap_fd_set_vnet_hdr_len(s, len);
381 if (err) {
382 error_report("Unable to set vnet-hdr length %d on %s: %s",
383 len, s->ifname, strerror(errno));
384 } else {
385
386 s->vnet_hdr_len = len;
387 }
388}
389
390static void netmap_set_offload(NetClientState *nc, int csum, int tso4, int tso6,
391 int ecn, int ufo)
392{
393 NetmapState *s = DO_UPCAST(NetmapState, nc, nc);
394
395
396
397 if (!s->vnet_hdr_len) {
398 netmap_set_vnet_hdr_len(nc, sizeof(struct virtio_net_hdr));
399 }
400}
401
402
403static NetClientInfo net_netmap_info = {
404 .type = NET_CLIENT_OPTIONS_KIND_NETMAP,
405 .size = sizeof(NetmapState),
406 .receive = netmap_receive,
407 .receive_iov = netmap_receive_iov,
408 .poll = netmap_poll,
409 .cleanup = netmap_cleanup,
410 .has_ufo = netmap_has_vnet_hdr,
411 .has_vnet_hdr = netmap_has_vnet_hdr,
412 .has_vnet_hdr_len = netmap_has_vnet_hdr_len,
413 .using_vnet_hdr = netmap_using_vnet_hdr,
414 .set_offload = netmap_set_offload,
415 .set_vnet_hdr_len = netmap_set_vnet_hdr_len,
416};
417
418
419
420
421
422int net_init_netmap(const NetClientOptions *opts,
423 const char *name, NetClientState *peer, Error **errp)
424{
425 const NetdevNetmapOptions *netmap_opts = opts->u.netmap.data;
426 struct nm_desc *nmd;
427 NetClientState *nc;
428 Error *err = NULL;
429 NetmapState *s;
430
431 nmd = netmap_open(netmap_opts, &err);
432 if (err) {
433 error_propagate(errp, err);
434 return -1;
435 }
436
437 nc = qemu_new_net_client(&net_netmap_info, peer, "netmap", name);
438 s = DO_UPCAST(NetmapState, nc, nc);
439 s->nmd = nmd;
440 s->tx = NETMAP_TXRING(nmd->nifp, 0);
441 s->rx = NETMAP_RXRING(nmd->nifp, 0);
442 s->vnet_hdr_len = 0;
443 pstrcpy(s->ifname, sizeof(s->ifname), netmap_opts->ifname);
444 netmap_read_poll(s, true);
445
446 return 0;
447}
448
449