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>
31 #define NETMAP_WITH_LIBS
32 #include <net/netmap.h>
33 #include <net/netmap_user.h>
38 #include "sysemu/sysemu.h"
39 #include "qemu/error-report.h"
42 /* Private netmap device info. */
43 typedef struct NetmapPriv
{
47 struct netmap_if
*nifp
;
48 struct netmap_ring
*rx
;
49 struct netmap_ring
*tx
;
50 char fdname
[PATH_MAX
]; /* Normally "/dev/netmap". */
51 char ifname
[IFNAMSIZ
];
54 typedef struct NetmapState
{
59 struct iovec iov
[IOV_MAX
];
60 int vnet_hdr_len
; /* Current virtio-net header length. */
64 #define pkt_copy bcopy
66 /* A fast copy routine only for multiples of 64 bytes, non overlapped. */
68 pkt_copy(const void *_src
, void *_dst
, int l
)
70 const uint64_t *src
= _src
;
72 if (unlikely(l
>= 1024)) {
76 for (; l
> 0; l
-= 64) {
87 #endif /* __FreeBSD__ */
90 * Open a netmap device. We assume there is only one queue
91 * (which is the case for the VALE bridge).
93 static int netmap_open(NetmapPriv
*me
)
100 me
->fd
= fd
= open(me
->fdname
, O_RDWR
);
102 error_report("Unable to open netmap device '%s' (%s)",
103 me
->fdname
, strerror(errno
));
106 memset(&req
, 0, sizeof(req
));
107 pstrcpy(req
.nr_name
, sizeof(req
.nr_name
), me
->ifname
);
108 req
.nr_ringid
= NETMAP_NO_TX_POLL
;
109 req
.nr_version
= NETMAP_API
;
110 err
= ioctl(fd
, NIOCREGIF
, &req
);
112 error_report("Unable to register %s: %s", me
->ifname
, strerror(errno
));
115 l
= me
->memsize
= req
.nr_memsize
;
117 me
->mem
= mmap(0, l
, PROT_WRITE
| PROT_READ
, MAP_SHARED
, fd
, 0);
118 if (me
->mem
== MAP_FAILED
) {
119 error_report("Unable to mmap netmap shared memory: %s",
125 me
->nifp
= NETMAP_IF(me
->mem
, req
.nr_offset
);
126 me
->tx
= NETMAP_TXRING(me
->nifp
, 0);
127 me
->rx
= NETMAP_RXRING(me
->nifp
, 0);
135 /* Tell the event-loop if the netmap backend can send packets
137 static int netmap_can_send(void *opaque
)
139 NetmapState
*s
= opaque
;
141 return qemu_can_send_packet(&s
->nc
);
144 static void netmap_send(void *opaque
);
145 static void netmap_writable(void *opaque
);
147 /* Set the event-loop handlers for the netmap backend. */
148 static void netmap_update_fd_handler(NetmapState
*s
)
150 qemu_set_fd_handler2(s
->me
.fd
,
151 s
->read_poll
? netmap_can_send
: NULL
,
152 s
->read_poll
? netmap_send
: NULL
,
153 s
->write_poll
? netmap_writable
: NULL
,
157 /* Update the read handler. */
158 static void netmap_read_poll(NetmapState
*s
, bool enable
)
160 if (s
->read_poll
!= enable
) { /* Do nothing if not changed. */
161 s
->read_poll
= enable
;
162 netmap_update_fd_handler(s
);
166 /* Update the write handler. */
167 static void netmap_write_poll(NetmapState
*s
, bool enable
)
169 if (s
->write_poll
!= enable
) {
170 s
->write_poll
= enable
;
171 netmap_update_fd_handler(s
);
175 static void netmap_poll(NetClientState
*nc
, bool enable
)
177 NetmapState
*s
= DO_UPCAST(NetmapState
, nc
, nc
);
179 if (s
->read_poll
!= enable
|| s
->write_poll
!= enable
) {
180 s
->read_poll
= enable
;
181 s
->read_poll
= enable
;
182 netmap_update_fd_handler(s
);
187 * The fd_write() callback, invoked if the fd is marked as
188 * writable after a poll. Unregister the handler and flush any
191 static void netmap_writable(void *opaque
)
193 NetmapState
*s
= opaque
;
195 netmap_write_poll(s
, false);
196 qemu_flush_queued_packets(&s
->nc
);
199 static ssize_t
netmap_receive(NetClientState
*nc
,
200 const uint8_t *buf
, size_t size
)
202 NetmapState
*s
= DO_UPCAST(NetmapState
, nc
, nc
);
203 struct netmap_ring
*ring
= s
->me
.tx
;
208 if (unlikely(!ring
)) {
213 if (unlikely(size
> ring
->nr_buf_size
)) {
214 RD(5, "[netmap_receive] drop packet of size %d > %d\n",
215 (int)size
, ring
->nr_buf_size
);
219 if (nm_ring_empty(ring
)) {
220 /* No available slots in the netmap TX ring. */
221 netmap_write_poll(s
, true);
226 idx
= ring
->slot
[i
].buf_idx
;
227 dst
= (uint8_t *)NETMAP_BUF(ring
, idx
);
229 ring
->slot
[i
].len
= size
;
230 ring
->slot
[i
].flags
= 0;
231 pkt_copy(buf
, dst
, size
);
232 ring
->cur
= ring
->head
= nm_ring_next(ring
, i
);
233 ioctl(s
->me
.fd
, NIOCTXSYNC
, NULL
);
238 static ssize_t
netmap_receive_iov(NetClientState
*nc
,
239 const struct iovec
*iov
, int iovcnt
)
241 NetmapState
*s
= DO_UPCAST(NetmapState
, nc
, nc
);
242 struct netmap_ring
*ring
= s
->me
.tx
;
249 if (unlikely(!ring
)) {
250 /* Drop the packet. */
251 return iov_size(iov
, iovcnt
);
254 last
= i
= ring
->cur
;
256 if (nm_ring_space(ring
) < iovcnt
) {
257 /* Not enough netmap slots. */
258 netmap_write_poll(s
, true);
262 for (j
= 0; j
< iovcnt
; j
++) {
263 int iov_frag_size
= iov
[j
].iov_len
;
267 /* Split each iovec fragment over more netmap slots, if
269 while (iov_frag_size
) {
270 nm_frag_size
= MIN(iov_frag_size
, ring
->nr_buf_size
);
272 if (unlikely(nm_ring_empty(ring
))) {
273 /* We run out of netmap slots while splitting the
275 netmap_write_poll(s
, true);
279 idx
= ring
->slot
[i
].buf_idx
;
280 dst
= (uint8_t *)NETMAP_BUF(ring
, idx
);
282 ring
->slot
[i
].len
= nm_frag_size
;
283 ring
->slot
[i
].flags
= NS_MOREFRAG
;
284 pkt_copy(iov
[j
].iov_base
+ offset
, dst
, nm_frag_size
);
287 i
= nm_ring_next(ring
, i
);
289 offset
+= nm_frag_size
;
290 iov_frag_size
-= nm_frag_size
;
293 /* The last slot must not have NS_MOREFRAG set. */
294 ring
->slot
[last
].flags
&= ~NS_MOREFRAG
;
296 /* Now update ring->cur and ring->head. */
297 ring
->cur
= ring
->head
= i
;
299 ioctl(s
->me
.fd
, NIOCTXSYNC
, NULL
);
301 return iov_size(iov
, iovcnt
);
304 /* Complete a previous send (backend --> guest) and enable the
306 static void netmap_send_completed(NetClientState
*nc
, ssize_t len
)
308 NetmapState
*s
= DO_UPCAST(NetmapState
, nc
, nc
);
310 netmap_read_poll(s
, true);
313 static void netmap_send(void *opaque
)
315 NetmapState
*s
= opaque
;
316 struct netmap_ring
*ring
= s
->me
.rx
;
318 /* Keep sending while there are available packets into the netmap
319 RX ring and the forwarding path towards the peer is open. */
320 while (!nm_ring_empty(ring
) && qemu_can_send_packet(&s
->nc
)) {
329 idx
= ring
->slot
[i
].buf_idx
;
330 morefrag
= (ring
->slot
[i
].flags
& NS_MOREFRAG
);
331 s
->iov
[iovcnt
].iov_base
= (u_char
*)NETMAP_BUF(ring
, idx
);
332 s
->iov
[iovcnt
].iov_len
= ring
->slot
[i
].len
;
335 ring
->cur
= ring
->head
= nm_ring_next(ring
, i
);
336 } while (!nm_ring_empty(ring
) && morefrag
);
338 if (unlikely(nm_ring_empty(ring
) && morefrag
)) {
339 RD(5, "[netmap_send] ran out of slots, with a pending"
340 "incomplete packet\n");
343 iovsize
= qemu_sendv_packet_async(&s
->nc
, s
->iov
, iovcnt
,
344 netmap_send_completed
);
347 /* The peer does not receive anymore. Packet is queued, stop
348 * reading from the backend until netmap_send_completed()
350 netmap_read_poll(s
, false);
356 /* Flush and close. */
357 static void netmap_cleanup(NetClientState
*nc
)
359 NetmapState
*s
= DO_UPCAST(NetmapState
, nc
, nc
);
361 qemu_purge_queued_packets(nc
);
363 netmap_poll(nc
, false);
364 munmap(s
->me
.mem
, s
->me
.memsize
);
370 /* Offloading manipulation support callbacks. */
371 static bool netmap_has_ufo(NetClientState
*nc
)
376 static bool netmap_has_vnet_hdr(NetClientState
*nc
)
381 static bool netmap_has_vnet_hdr_len(NetClientState
*nc
, int len
)
383 return len
== 0 || len
== sizeof(struct virtio_net_hdr
) ||
384 len
== sizeof(struct virtio_net_hdr_mrg_rxbuf
);
387 static void netmap_using_vnet_hdr(NetClientState
*nc
, bool enable
)
391 static void netmap_set_vnet_hdr_len(NetClientState
*nc
, int len
)
393 NetmapState
*s
= DO_UPCAST(NetmapState
, nc
, nc
);
397 /* Issue a NETMAP_BDG_VNET_HDR command to change the virtio-net header
398 * length for the netmap adapter associated to 'me->ifname'.
400 memset(&req
, 0, sizeof(req
));
401 pstrcpy(req
.nr_name
, sizeof(req
.nr_name
), s
->me
.ifname
);
402 req
.nr_version
= NETMAP_API
;
403 req
.nr_cmd
= NETMAP_BDG_VNET_HDR
;
405 err
= ioctl(s
->me
.fd
, NIOCREGIF
, &req
);
407 error_report("Unable to execute NETMAP_BDG_VNET_HDR on %s: %s",
408 s
->me
.ifname
, strerror(errno
));
410 /* Keep track of the current length. */
411 s
->vnet_hdr_len
= len
;
415 static void netmap_set_offload(NetClientState
*nc
, int csum
, int tso4
, int tso6
,
418 NetmapState
*s
= DO_UPCAST(NetmapState
, nc
, nc
);
420 /* Setting a virtio-net header length greater than zero automatically
421 * enables the offloadings.
423 if (!s
->vnet_hdr_len
) {
424 netmap_set_vnet_hdr_len(nc
, sizeof(struct virtio_net_hdr
));
428 /* NetClientInfo methods */
429 static NetClientInfo net_netmap_info
= {
430 .type
= NET_CLIENT_OPTIONS_KIND_NETMAP
,
431 .size
= sizeof(NetmapState
),
432 .receive
= netmap_receive
,
433 .receive_iov
= netmap_receive_iov
,
435 .cleanup
= netmap_cleanup
,
436 .has_ufo
= netmap_has_ufo
,
437 .has_vnet_hdr
= netmap_has_vnet_hdr
,
438 .has_vnet_hdr_len
= netmap_has_vnet_hdr_len
,
439 .using_vnet_hdr
= netmap_using_vnet_hdr
,
440 .set_offload
= netmap_set_offload
,
441 .set_vnet_hdr_len
= netmap_set_vnet_hdr_len
,
444 /* The exported init function
446 * ... -net netmap,ifname="..."
448 int net_init_netmap(const NetClientOptions
*opts
,
449 const char *name
, NetClientState
*peer
)
451 const NetdevNetmapOptions
*netmap_opts
= opts
->netmap
;
456 pstrcpy(me
.fdname
, sizeof(me
.fdname
),
457 netmap_opts
->has_devname
? netmap_opts
->devname
: "/dev/netmap");
458 /* Set default name for the port if not supplied. */
459 pstrcpy(me
.ifname
, sizeof(me
.ifname
), netmap_opts
->ifname
);
460 if (netmap_open(&me
)) {
463 /* Create the object. */
464 nc
= qemu_new_net_client(&net_netmap_info
, peer
, "netmap", name
);
465 s
= DO_UPCAST(NetmapState
, nc
, nc
);
468 netmap_read_poll(s
, true); /* Initially only poll for reads. */