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 "qemu/osdep.h"
27 #include <sys/ioctl.h>
29 #define NETMAP_WITH_LIBS
30 #include <net/netmap.h>
31 #include <net/netmap_user.h>
36 #include "sysemu/sysemu.h"
37 #include "qemu/error-report.h"
38 #include "qapi/error.h"
40 #include "qemu/cutils.h"
41 #include "qemu/main-loop.h"
43 typedef struct NetmapState
{
46 char ifname
[IFNAMSIZ
];
47 struct netmap_ring
*tx
;
48 struct netmap_ring
*rx
;
51 struct iovec iov
[IOV_MAX
];
52 int vnet_hdr_len
; /* Current virtio-net header length. */
56 #define pkt_copy bcopy
58 /* A fast copy routine only for multiples of 64 bytes, non overlapped. */
60 pkt_copy(const void *_src
, void *_dst
, int l
)
62 const uint64_t *src
= _src
;
64 if (unlikely(l
>= 1024)) {
68 for (; l
> 0; l
-= 64) {
79 #endif /* __FreeBSD__ */
82 * Open a netmap device. We assume there is only one queue
83 * (which is the case for the VALE bridge).
85 static struct nm_desc
*netmap_open(const NetdevNetmapOptions
*nm_opts
,
91 memset(&req
, 0, sizeof(req
));
93 nmd
= nm_open(nm_opts
->ifname
, &req
, NETMAP_NO_TX_POLL
,
96 error_setg_errno(errp
, errno
, "Failed to nm_open() %s",
104 static void netmap_send(void *opaque
);
105 static void netmap_writable(void *opaque
);
107 /* Set the event-loop handlers for the netmap backend. */
108 static void netmap_update_fd_handler(NetmapState
*s
)
110 qemu_set_fd_handler(s
->nmd
->fd
,
111 s
->read_poll
? netmap_send
: NULL
,
112 s
->write_poll
? netmap_writable
: NULL
,
116 /* Update the read handler. */
117 static void netmap_read_poll(NetmapState
*s
, bool enable
)
119 if (s
->read_poll
!= enable
) { /* Do nothing if not changed. */
120 s
->read_poll
= enable
;
121 netmap_update_fd_handler(s
);
125 /* Update the write handler. */
126 static void netmap_write_poll(NetmapState
*s
, bool enable
)
128 if (s
->write_poll
!= enable
) {
129 s
->write_poll
= enable
;
130 netmap_update_fd_handler(s
);
134 static void netmap_poll(NetClientState
*nc
, bool enable
)
136 NetmapState
*s
= DO_UPCAST(NetmapState
, nc
, nc
);
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
);
146 * The fd_write() callback, invoked if the fd is marked as
147 * writable after a poll. Unregister the handler and flush any
150 static void netmap_writable(void *opaque
)
152 NetmapState
*s
= opaque
;
154 netmap_write_poll(s
, false);
155 qemu_flush_queued_packets(&s
->nc
);
158 static ssize_t
netmap_receive_iov(NetClientState
*nc
,
159 const struct iovec
*iov
, int iovcnt
)
161 NetmapState
*s
= DO_UPCAST(NetmapState
, nc
, nc
);
162 struct netmap_ring
*ring
= s
->tx
;
163 unsigned int tail
= ring
->tail
;
171 last
= i
= ring
->head
;
173 if (nm_ring_space(ring
) < iovcnt
) {
174 /* Not enough netmap slots. Tell the kernel that we have seen the new
175 * available slots (so that it notifies us again when it has more
176 * ones), but without publishing any new slots to be processed
177 * (e.g., we don't advance ring->head). */
179 netmap_write_poll(s
, true);
183 for (j
= 0; j
< iovcnt
; j
++) {
184 int iov_frag_size
= iov
[j
].iov_len
;
188 totlen
+= iov_frag_size
;
190 /* Split each iovec fragment over more netmap slots, if
192 while (iov_frag_size
) {
193 nm_frag_size
= MIN(iov_frag_size
, ring
->nr_buf_size
);
195 if (unlikely(i
== tail
)) {
196 /* We ran out of netmap slots while splitting the
199 netmap_write_poll(s
, true);
203 idx
= ring
->slot
[i
].buf_idx
;
204 dst
= (uint8_t *)NETMAP_BUF(ring
, idx
);
206 ring
->slot
[i
].len
= nm_frag_size
;
207 ring
->slot
[i
].flags
= NS_MOREFRAG
;
208 pkt_copy(iov
[j
].iov_base
+ offset
, dst
, nm_frag_size
);
211 i
= nm_ring_next(ring
, i
);
213 offset
+= nm_frag_size
;
214 iov_frag_size
-= nm_frag_size
;
217 /* The last slot must not have NS_MOREFRAG set. */
218 ring
->slot
[last
].flags
&= ~NS_MOREFRAG
;
220 /* Now update ring->head and ring->cur to publish the new slots and
221 * the new wakeup point. */
222 ring
->head
= ring
->cur
= i
;
224 ioctl(s
->nmd
->fd
, NIOCTXSYNC
, NULL
);
229 static ssize_t
netmap_receive(NetClientState
*nc
,
230 const uint8_t *buf
, size_t size
)
234 iov
.iov_base
= (void *)buf
;
237 return netmap_receive_iov(nc
, &iov
, 1);
240 /* Complete a previous send (backend --> guest) and enable the
242 static void netmap_send_completed(NetClientState
*nc
, ssize_t len
)
244 NetmapState
*s
= DO_UPCAST(NetmapState
, nc
, nc
);
246 netmap_read_poll(s
, true);
249 static void netmap_send(void *opaque
)
251 NetmapState
*s
= opaque
;
252 struct netmap_ring
*ring
= s
->rx
;
253 unsigned int tail
= ring
->tail
;
255 /* Keep sending while there are available slots in the netmap
256 RX ring and the forwarding path towards the peer is open. */
257 while (ring
->head
!= tail
) {
258 uint32_t i
= ring
->head
;
264 /* Get a (possibly multi-slot) packet. */
266 idx
= ring
->slot
[i
].buf_idx
;
267 morefrag
= (ring
->slot
[i
].flags
& NS_MOREFRAG
);
268 s
->iov
[iovcnt
].iov_base
= (void *)NETMAP_BUF(ring
, idx
);
269 s
->iov
[iovcnt
].iov_len
= ring
->slot
[i
].len
;
271 i
= nm_ring_next(ring
, i
);
272 } while (i
!= tail
&& morefrag
);
274 /* Advance ring->cur to tell the kernel that we have seen the slots. */
277 if (unlikely(morefrag
)) {
278 /* This is a truncated packet, so we can stop without releasing the
279 * incomplete slots by updating ring->head. We will hopefully
280 * re-read the complete packet the next time we are called. */
284 iovsize
= qemu_sendv_packet_async(&s
->nc
, s
->iov
, iovcnt
,
285 netmap_send_completed
);
287 /* Release the slots to the kernel. */
291 /* The peer does not receive anymore. Packet is queued, stop
292 * reading from the backend until netmap_send_completed(). */
293 netmap_read_poll(s
, false);
299 /* Flush and close. */
300 static void netmap_cleanup(NetClientState
*nc
)
302 NetmapState
*s
= DO_UPCAST(NetmapState
, nc
, nc
);
304 qemu_purge_queued_packets(nc
);
306 netmap_poll(nc
, false);
311 /* Offloading manipulation support callbacks. */
312 static int netmap_fd_set_vnet_hdr_len(NetmapState
*s
, int len
)
316 /* Issue a NETMAP_BDG_VNET_HDR command to change the virtio-net header
317 * length for the netmap adapter associated to 's->ifname'.
319 memset(&req
, 0, sizeof(req
));
320 pstrcpy(req
.nr_name
, sizeof(req
.nr_name
), s
->ifname
);
321 req
.nr_version
= NETMAP_API
;
322 req
.nr_cmd
= NETMAP_BDG_VNET_HDR
;
325 return ioctl(s
->nmd
->fd
, NIOCREGIF
, &req
);
328 static bool netmap_has_vnet_hdr_len(NetClientState
*nc
, int len
)
330 NetmapState
*s
= DO_UPCAST(NetmapState
, nc
, nc
);
331 int prev_len
= s
->vnet_hdr_len
;
333 /* Check that we can set the new length. */
334 if (netmap_fd_set_vnet_hdr_len(s
, len
)) {
338 /* Restore the previous length. */
339 if (netmap_fd_set_vnet_hdr_len(s
, prev_len
)) {
340 error_report("Failed to restore vnet-hdr length %d on %s: %s",
341 prev_len
, s
->ifname
, strerror(errno
));
348 /* A netmap interface that supports virtio-net headers always
349 * supports UFO, so we use this callback also for the has_ufo hook. */
350 static bool netmap_has_vnet_hdr(NetClientState
*nc
)
352 return netmap_has_vnet_hdr_len(nc
, sizeof(struct virtio_net_hdr
));
355 static void netmap_using_vnet_hdr(NetClientState
*nc
, bool enable
)
359 static void netmap_set_vnet_hdr_len(NetClientState
*nc
, int len
)
361 NetmapState
*s
= DO_UPCAST(NetmapState
, nc
, nc
);
364 err
= netmap_fd_set_vnet_hdr_len(s
, len
);
366 error_report("Unable to set vnet-hdr length %d on %s: %s",
367 len
, s
->ifname
, strerror(errno
));
369 /* Keep track of the current length. */
370 s
->vnet_hdr_len
= len
;
374 static void netmap_set_offload(NetClientState
*nc
, int csum
, int tso4
, int tso6
,
377 NetmapState
*s
= DO_UPCAST(NetmapState
, nc
, nc
);
379 /* Setting a virtio-net header length greater than zero automatically
380 * enables the offloadings. */
381 if (!s
->vnet_hdr_len
) {
382 netmap_set_vnet_hdr_len(nc
, sizeof(struct virtio_net_hdr
));
386 /* NetClientInfo methods */
387 static NetClientInfo net_netmap_info
= {
388 .type
= NET_CLIENT_DRIVER_NETMAP
,
389 .size
= sizeof(NetmapState
),
390 .receive
= netmap_receive
,
391 .receive_iov
= netmap_receive_iov
,
393 .cleanup
= netmap_cleanup
,
394 .has_ufo
= netmap_has_vnet_hdr
,
395 .has_vnet_hdr
= netmap_has_vnet_hdr
,
396 .has_vnet_hdr_len
= netmap_has_vnet_hdr_len
,
397 .using_vnet_hdr
= netmap_using_vnet_hdr
,
398 .set_offload
= netmap_set_offload
,
399 .set_vnet_hdr_len
= netmap_set_vnet_hdr_len
,
402 /* The exported init function
404 * ... -net netmap,ifname="..."
406 int net_init_netmap(const Netdev
*netdev
,
407 const char *name
, NetClientState
*peer
, Error
**errp
)
409 const NetdevNetmapOptions
*netmap_opts
= &netdev
->u
.netmap
;
415 nmd
= netmap_open(netmap_opts
, &err
);
417 error_propagate(errp
, err
);
420 /* Create the object. */
421 nc
= qemu_new_net_client(&net_netmap_info
, peer
, "netmap", name
);
422 s
= DO_UPCAST(NetmapState
, nc
, nc
);
424 s
->tx
= NETMAP_TXRING(nmd
->nifp
, 0);
425 s
->rx
= NETMAP_RXRING(nmd
->nifp
, 0);
427 pstrcpy(s
->ifname
, sizeof(s
->ifname
), netmap_opts
->ifname
);
428 netmap_read_poll(s
, true); /* Initially only poll for reads. */