2 * netmap access for qemu
4 * Copyright (c) 2012-2013 Luigi Rizzo
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 #include <sys/ioctl.h>
30 #include <net/netmap.h>
31 #include <net/netmap_user.h>
36 #include "sysemu/sysemu.h"
37 #include "qemu/error-report.h"
40 /* Private netmap device info. */
41 typedef struct NetmapPriv
{
45 struct netmap_if
*nifp
;
46 struct netmap_ring
*rx
;
47 struct netmap_ring
*tx
;
48 char fdname
[PATH_MAX
]; /* Normally "/dev/netmap". */
49 char ifname
[IFNAMSIZ
];
52 typedef struct NetmapState
{
57 struct iovec iov
[IOV_MAX
];
58 int vnet_hdr_len
; /* Current virtio-net header length. */
61 #define D(format, ...) \
63 struct timeval __xxts; \
64 gettimeofday(&__xxts, NULL); \
65 printf("%03d.%06d %s [%d] " format "\n", \
66 (int)__xxts.tv_sec % 1000, (int)__xxts.tv_usec, \
67 __func__, __LINE__, ##__VA_ARGS__); \
70 /* Rate limited version of "D", lps indicates how many per second */
71 #define RD(lps, format, ...) \
73 static int t0, __cnt; \
74 struct timeval __xxts; \
75 gettimeofday(&__xxts, NULL); \
76 if (t0 != __xxts.tv_sec) { \
80 if (__cnt++ < lps) { \
81 D(format, ##__VA_ARGS__); \
87 #define pkt_copy bcopy
89 /* A fast copy routine only for multiples of 64 bytes, non overlapped. */
91 pkt_copy(const void *_src
, void *_dst
, int l
)
93 const uint64_t *src
= _src
;
95 if (unlikely(l
>= 1024)) {
99 for (; l
> 0; l
-= 64) {
110 #endif /* __FreeBSD__ */
113 * Open a netmap device. We assume there is only one queue
114 * (which is the case for the VALE bridge).
116 static int netmap_open(NetmapPriv
*me
)
123 me
->fd
= fd
= open(me
->fdname
, O_RDWR
);
125 error_report("Unable to open netmap device '%s' (%s)",
126 me
->fdname
, strerror(errno
));
129 memset(&req
, 0, sizeof(req
));
130 pstrcpy(req
.nr_name
, sizeof(req
.nr_name
), me
->ifname
);
131 req
.nr_ringid
= NETMAP_NO_TX_POLL
;
132 req
.nr_version
= NETMAP_API
;
133 err
= ioctl(fd
, NIOCREGIF
, &req
);
135 error_report("Unable to register %s: %s", me
->ifname
, strerror(errno
));
138 l
= me
->memsize
= req
.nr_memsize
;
140 me
->mem
= mmap(0, l
, PROT_WRITE
| PROT_READ
, MAP_SHARED
, fd
, 0);
141 if (me
->mem
== MAP_FAILED
) {
142 error_report("Unable to mmap netmap shared memory: %s",
148 me
->nifp
= NETMAP_IF(me
->mem
, req
.nr_offset
);
149 me
->tx
= NETMAP_TXRING(me
->nifp
, 0);
150 me
->rx
= NETMAP_RXRING(me
->nifp
, 0);
158 /* Tell the event-loop if the netmap backend can send packets
160 static int netmap_can_send(void *opaque
)
162 NetmapState
*s
= opaque
;
164 return qemu_can_send_packet(&s
->nc
);
167 static void netmap_send(void *opaque
);
168 static void netmap_writable(void *opaque
);
170 /* Set the event-loop handlers for the netmap backend. */
171 static void netmap_update_fd_handler(NetmapState
*s
)
173 qemu_set_fd_handler2(s
->me
.fd
,
174 s
->read_poll
? netmap_can_send
: NULL
,
175 s
->read_poll
? netmap_send
: NULL
,
176 s
->write_poll
? netmap_writable
: NULL
,
180 /* Update the read handler. */
181 static void netmap_read_poll(NetmapState
*s
, bool enable
)
183 if (s
->read_poll
!= enable
) { /* Do nothing if not changed. */
184 s
->read_poll
= enable
;
185 netmap_update_fd_handler(s
);
189 /* Update the write handler. */
190 static void netmap_write_poll(NetmapState
*s
, bool enable
)
192 if (s
->write_poll
!= enable
) {
193 s
->write_poll
= enable
;
194 netmap_update_fd_handler(s
);
198 static void netmap_poll(NetClientState
*nc
, bool enable
)
200 NetmapState
*s
= DO_UPCAST(NetmapState
, nc
, nc
);
202 if (s
->read_poll
!= enable
|| s
->write_poll
!= enable
) {
203 s
->read_poll
= enable
;
204 s
->read_poll
= enable
;
205 netmap_update_fd_handler(s
);
210 * The fd_write() callback, invoked if the fd is marked as
211 * writable after a poll. Unregister the handler and flush any
214 static void netmap_writable(void *opaque
)
216 NetmapState
*s
= opaque
;
218 netmap_write_poll(s
, false);
219 qemu_flush_queued_packets(&s
->nc
);
222 static ssize_t
netmap_receive(NetClientState
*nc
,
223 const uint8_t *buf
, size_t size
)
225 NetmapState
*s
= DO_UPCAST(NetmapState
, nc
, nc
);
226 struct netmap_ring
*ring
= s
->me
.tx
;
231 if (unlikely(!ring
)) {
236 if (unlikely(size
> ring
->nr_buf_size
)) {
237 RD(5, "[netmap_receive] drop packet of size %d > %d\n",
238 (int)size
, ring
->nr_buf_size
);
242 if (ring
->avail
== 0) {
243 /* No available slots in the netmap TX ring. */
244 netmap_write_poll(s
, true);
249 idx
= ring
->slot
[i
].buf_idx
;
250 dst
= (uint8_t *)NETMAP_BUF(ring
, idx
);
252 ring
->slot
[i
].len
= size
;
253 ring
->slot
[i
].flags
= 0;
254 pkt_copy(buf
, dst
, size
);
255 ring
->cur
= NETMAP_RING_NEXT(ring
, i
);
257 ioctl(s
->me
.fd
, NIOCTXSYNC
, NULL
);
262 static ssize_t
netmap_receive_iov(NetClientState
*nc
,
263 const struct iovec
*iov
, int iovcnt
)
265 NetmapState
*s
= DO_UPCAST(NetmapState
, nc
, nc
);
266 struct netmap_ring
*ring
= s
->me
.tx
;
274 if (unlikely(!ring
)) {
275 /* Drop the packet. */
276 return iov_size(iov
, iovcnt
);
279 last
= i
= ring
->cur
;
282 if (avail
< iovcnt
) {
283 /* Not enough netmap slots. */
284 netmap_write_poll(s
, true);
288 for (j
= 0; j
< iovcnt
; j
++) {
289 int iov_frag_size
= iov
[j
].iov_len
;
293 /* Split each iovec fragment over more netmap slots, if
295 while (iov_frag_size
) {
296 nm_frag_size
= MIN(iov_frag_size
, ring
->nr_buf_size
);
298 if (unlikely(avail
== 0)) {
299 /* We run out of netmap slots while splitting the
301 netmap_write_poll(s
, true);
305 idx
= ring
->slot
[i
].buf_idx
;
306 dst
= (uint8_t *)NETMAP_BUF(ring
, idx
);
308 ring
->slot
[i
].len
= nm_frag_size
;
309 ring
->slot
[i
].flags
= NS_MOREFRAG
;
310 pkt_copy(iov
[j
].iov_base
+ offset
, dst
, nm_frag_size
);
313 i
= NETMAP_RING_NEXT(ring
, i
);
316 offset
+= nm_frag_size
;
317 iov_frag_size
-= nm_frag_size
;
320 /* The last slot must not have NS_MOREFRAG set. */
321 ring
->slot
[last
].flags
&= ~NS_MOREFRAG
;
323 /* Now update ring->cur and ring->avail. */
327 ioctl(s
->me
.fd
, NIOCTXSYNC
, NULL
);
329 return iov_size(iov
, iovcnt
);
332 /* Complete a previous send (backend --> guest) and enable the
334 static void netmap_send_completed(NetClientState
*nc
, ssize_t len
)
336 NetmapState
*s
= DO_UPCAST(NetmapState
, nc
, nc
);
338 netmap_read_poll(s
, true);
341 static void netmap_send(void *opaque
)
343 NetmapState
*s
= opaque
;
344 struct netmap_ring
*ring
= s
->me
.rx
;
346 /* Keep sending while there are available packets into the netmap
347 RX ring and the forwarding path towards the peer is open. */
348 while (ring
->avail
> 0 && qemu_can_send_packet(&s
->nc
)) {
357 idx
= ring
->slot
[i
].buf_idx
;
358 morefrag
= (ring
->slot
[i
].flags
& NS_MOREFRAG
);
359 s
->iov
[iovcnt
].iov_base
= (u_char
*)NETMAP_BUF(ring
, idx
);
360 s
->iov
[iovcnt
].iov_len
= ring
->slot
[i
].len
;
363 ring
->cur
= NETMAP_RING_NEXT(ring
, i
);
365 } while (ring
->avail
&& morefrag
);
367 if (unlikely(!ring
->avail
&& morefrag
)) {
368 RD(5, "[netmap_send] ran out of slots, with a pending"
369 "incomplete packet\n");
372 iovsize
= qemu_sendv_packet_async(&s
->nc
, s
->iov
, iovcnt
,
373 netmap_send_completed
);
376 /* The peer does not receive anymore. Packet is queued, stop
377 * reading from the backend until netmap_send_completed()
379 netmap_read_poll(s
, false);
385 /* Flush and close. */
386 static void netmap_cleanup(NetClientState
*nc
)
388 NetmapState
*s
= DO_UPCAST(NetmapState
, nc
, nc
);
390 qemu_purge_queued_packets(nc
);
392 netmap_poll(nc
, false);
393 munmap(s
->me
.mem
, s
->me
.memsize
);
399 /* Offloading manipulation support callbacks. */
400 static bool netmap_has_ufo(NetClientState
*nc
)
405 static bool netmap_has_vnet_hdr(NetClientState
*nc
)
410 static bool netmap_has_vnet_hdr_len(NetClientState
*nc
, int len
)
412 return len
== 0 || len
== sizeof(struct virtio_net_hdr
) ||
413 len
== sizeof(struct virtio_net_hdr_mrg_rxbuf
);
416 static void netmap_using_vnet_hdr(NetClientState
*nc
, bool enable
)
420 static void netmap_set_vnet_hdr_len(NetClientState
*nc
, int len
)
422 NetmapState
*s
= DO_UPCAST(NetmapState
, nc
, nc
);
426 /* Issue a NETMAP_BDG_VNET_HDR command to change the virtio-net header
427 * length for the netmap adapter associated to 'me->ifname'.
429 memset(&req
, 0, sizeof(req
));
430 pstrcpy(req
.nr_name
, sizeof(req
.nr_name
), s
->me
.ifname
);
431 req
.nr_version
= NETMAP_API
;
432 req
.nr_cmd
= NETMAP_BDG_VNET_HDR
;
434 err
= ioctl(s
->me
.fd
, NIOCREGIF
, &req
);
436 error_report("Unable to execute NETMAP_BDG_VNET_HDR on %s: %s",
437 s
->me
.ifname
, strerror(errno
));
439 /* Keep track of the current length. */
440 s
->vnet_hdr_len
= len
;
444 static void netmap_set_offload(NetClientState
*nc
, int csum
, int tso4
, int tso6
,
447 NetmapState
*s
= DO_UPCAST(NetmapState
, nc
, nc
);
449 /* Setting a virtio-net header length greater than zero automatically
450 * enables the offloadings.
452 if (!s
->vnet_hdr_len
) {
453 netmap_set_vnet_hdr_len(nc
, sizeof(struct virtio_net_hdr
));
457 /* NetClientInfo methods */
458 static NetClientInfo net_netmap_info
= {
459 .type
= NET_CLIENT_OPTIONS_KIND_NETMAP
,
460 .size
= sizeof(NetmapState
),
461 .receive
= netmap_receive
,
462 .receive_iov
= netmap_receive_iov
,
464 .cleanup
= netmap_cleanup
,
465 .has_ufo
= netmap_has_ufo
,
466 .has_vnet_hdr
= netmap_has_vnet_hdr
,
467 .has_vnet_hdr_len
= netmap_has_vnet_hdr_len
,
468 .using_vnet_hdr
= netmap_using_vnet_hdr
,
469 .set_offload
= netmap_set_offload
,
470 .set_vnet_hdr_len
= netmap_set_vnet_hdr_len
,
473 /* The exported init function
475 * ... -net netmap,ifname="..."
477 int net_init_netmap(const NetClientOptions
*opts
,
478 const char *name
, NetClientState
*peer
)
480 const NetdevNetmapOptions
*netmap_opts
= opts
->netmap
;
485 pstrcpy(me
.fdname
, sizeof(me
.fdname
),
486 netmap_opts
->has_devname
? netmap_opts
->devname
: "/dev/netmap");
487 /* Set default name for the port if not supplied. */
488 pstrcpy(me
.ifname
, sizeof(me
.ifname
), netmap_opts
->ifname
);
489 if (netmap_open(&me
)) {
492 /* Create the object. */
493 nc
= qemu_new_net_client(&net_netmap_info
, peer
, "netmap", name
);
494 s
= DO_UPCAST(NetmapState
, nc
, nc
);
497 netmap_read_poll(s
, true); /* Initially only poll for reads. */