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>
30 #define NETMAP_WITH_LIBS
31 #include <net/netmap.h>
32 #include <net/netmap_user.h>
37 #include "sysemu/sysemu.h"
38 #include "qemu/error-report.h"
41 typedef struct NetmapState
{
44 char ifname
[IFNAMSIZ
];
45 struct netmap_ring
*tx
;
46 struct netmap_ring
*rx
;
49 struct iovec iov
[IOV_MAX
];
50 int vnet_hdr_len
; /* Current virtio-net header length. */
54 #define pkt_copy bcopy
56 /* A fast copy routine only for multiples of 64 bytes, non overlapped. */
58 pkt_copy(const void *_src
, void *_dst
, int l
)
60 const uint64_t *src
= _src
;
62 if (unlikely(l
>= 1024)) {
66 for (; l
> 0; l
-= 64) {
77 #endif /* __FreeBSD__ */
80 * Open a netmap device. We assume there is only one queue
81 * (which is the case for the VALE bridge).
83 static struct nm_desc
*netmap_open(const NetdevNetmapOptions
*nm_opts
,
89 memset(&req
, 0, sizeof(req
));
91 nmd
= nm_open(nm_opts
->ifname
, &req
, NETMAP_NO_TX_POLL
,
94 error_setg_errno(errp
, errno
, "Failed to nm_open() %s",
102 static void netmap_send(void *opaque
);
103 static void netmap_writable(void *opaque
);
105 /* Set the event-loop handlers for the netmap backend. */
106 static void netmap_update_fd_handler(NetmapState
*s
)
108 qemu_set_fd_handler(s
->nmd
->fd
,
109 s
->read_poll
? netmap_send
: NULL
,
110 s
->write_poll
? netmap_writable
: NULL
,
114 /* Update the read handler. */
115 static void netmap_read_poll(NetmapState
*s
, bool enable
)
117 if (s
->read_poll
!= enable
) { /* Do nothing if not changed. */
118 s
->read_poll
= enable
;
119 netmap_update_fd_handler(s
);
123 /* Update the write handler. */
124 static void netmap_write_poll(NetmapState
*s
, bool enable
)
126 if (s
->write_poll
!= enable
) {
127 s
->write_poll
= enable
;
128 netmap_update_fd_handler(s
);
132 static void netmap_poll(NetClientState
*nc
, bool enable
)
134 NetmapState
*s
= DO_UPCAST(NetmapState
, nc
, nc
);
136 if (s
->read_poll
!= enable
|| s
->write_poll
!= enable
) {
137 s
->write_poll
= enable
;
138 s
->read_poll
= enable
;
139 netmap_update_fd_handler(s
);
144 * The fd_write() callback, invoked if the fd is marked as
145 * writable after a poll. Unregister the handler and flush any
148 static void netmap_writable(void *opaque
)
150 NetmapState
*s
= opaque
;
152 netmap_write_poll(s
, false);
153 qemu_flush_queued_packets(&s
->nc
);
156 static ssize_t
netmap_receive(NetClientState
*nc
,
157 const uint8_t *buf
, size_t size
)
159 NetmapState
*s
= DO_UPCAST(NetmapState
, nc
, nc
);
160 struct netmap_ring
*ring
= s
->tx
;
165 if (unlikely(!ring
)) {
170 if (unlikely(size
> ring
->nr_buf_size
)) {
171 RD(5, "[netmap_receive] drop packet of size %d > %d\n",
172 (int)size
, ring
->nr_buf_size
);
176 if (nm_ring_empty(ring
)) {
177 /* No available slots in the netmap TX ring. */
178 netmap_write_poll(s
, true);
183 idx
= ring
->slot
[i
].buf_idx
;
184 dst
= (uint8_t *)NETMAP_BUF(ring
, idx
);
186 ring
->slot
[i
].len
= size
;
187 ring
->slot
[i
].flags
= 0;
188 pkt_copy(buf
, dst
, size
);
189 ring
->cur
= ring
->head
= nm_ring_next(ring
, i
);
190 ioctl(s
->nmd
->fd
, NIOCTXSYNC
, NULL
);
195 static ssize_t
netmap_receive_iov(NetClientState
*nc
,
196 const struct iovec
*iov
, int iovcnt
)
198 NetmapState
*s
= DO_UPCAST(NetmapState
, nc
, nc
);
199 struct netmap_ring
*ring
= s
->tx
;
206 if (unlikely(!ring
)) {
207 /* Drop the packet. */
208 return iov_size(iov
, iovcnt
);
211 last
= i
= ring
->cur
;
213 if (nm_ring_space(ring
) < iovcnt
) {
214 /* Not enough netmap slots. */
215 netmap_write_poll(s
, true);
219 for (j
= 0; j
< iovcnt
; j
++) {
220 int iov_frag_size
= iov
[j
].iov_len
;
224 /* Split each iovec fragment over more netmap slots, if
226 while (iov_frag_size
) {
227 nm_frag_size
= MIN(iov_frag_size
, ring
->nr_buf_size
);
229 if (unlikely(nm_ring_empty(ring
))) {
230 /* We run out of netmap slots while splitting the
232 netmap_write_poll(s
, true);
236 idx
= ring
->slot
[i
].buf_idx
;
237 dst
= (uint8_t *)NETMAP_BUF(ring
, idx
);
239 ring
->slot
[i
].len
= nm_frag_size
;
240 ring
->slot
[i
].flags
= NS_MOREFRAG
;
241 pkt_copy(iov
[j
].iov_base
+ offset
, dst
, nm_frag_size
);
244 i
= nm_ring_next(ring
, i
);
246 offset
+= nm_frag_size
;
247 iov_frag_size
-= nm_frag_size
;
250 /* The last slot must not have NS_MOREFRAG set. */
251 ring
->slot
[last
].flags
&= ~NS_MOREFRAG
;
253 /* Now update ring->cur and ring->head. */
254 ring
->cur
= ring
->head
= i
;
256 ioctl(s
->nmd
->fd
, NIOCTXSYNC
, NULL
);
258 return iov_size(iov
, iovcnt
);
261 /* Complete a previous send (backend --> guest) and enable the
263 static void netmap_send_completed(NetClientState
*nc
, ssize_t len
)
265 NetmapState
*s
= DO_UPCAST(NetmapState
, nc
, nc
);
267 netmap_read_poll(s
, true);
270 static void netmap_send(void *opaque
)
272 NetmapState
*s
= opaque
;
273 struct netmap_ring
*ring
= s
->rx
;
275 /* Keep sending while there are available packets into the netmap
276 RX ring and the forwarding path towards the peer is open. */
277 while (!nm_ring_empty(ring
)) {
286 idx
= ring
->slot
[i
].buf_idx
;
287 morefrag
= (ring
->slot
[i
].flags
& NS_MOREFRAG
);
288 s
->iov
[iovcnt
].iov_base
= (u_char
*)NETMAP_BUF(ring
, idx
);
289 s
->iov
[iovcnt
].iov_len
= ring
->slot
[i
].len
;
292 ring
->cur
= ring
->head
= nm_ring_next(ring
, i
);
293 } while (!nm_ring_empty(ring
) && morefrag
);
295 if (unlikely(nm_ring_empty(ring
) && morefrag
)) {
296 RD(5, "[netmap_send] ran out of slots, with a pending"
297 "incomplete packet\n");
300 iovsize
= qemu_sendv_packet_async(&s
->nc
, s
->iov
, iovcnt
,
301 netmap_send_completed
);
304 /* The peer does not receive anymore. Packet is queued, stop
305 * reading from the backend until netmap_send_completed()
307 netmap_read_poll(s
, false);
313 /* Flush and close. */
314 static void netmap_cleanup(NetClientState
*nc
)
316 NetmapState
*s
= DO_UPCAST(NetmapState
, nc
, nc
);
318 qemu_purge_queued_packets(nc
);
320 netmap_poll(nc
, false);
325 /* Offloading manipulation support callbacks. */
326 static bool netmap_has_ufo(NetClientState
*nc
)
331 static bool netmap_has_vnet_hdr(NetClientState
*nc
)
336 static bool netmap_has_vnet_hdr_len(NetClientState
*nc
, int len
)
338 return len
== 0 || len
== sizeof(struct virtio_net_hdr
) ||
339 len
== sizeof(struct virtio_net_hdr_mrg_rxbuf
);
342 static void netmap_using_vnet_hdr(NetClientState
*nc
, bool enable
)
346 static void netmap_set_vnet_hdr_len(NetClientState
*nc
, int len
)
348 NetmapState
*s
= DO_UPCAST(NetmapState
, nc
, nc
);
352 /* Issue a NETMAP_BDG_VNET_HDR command to change the virtio-net header
353 * length for the netmap adapter associated to 's->ifname'.
355 memset(&req
, 0, sizeof(req
));
356 pstrcpy(req
.nr_name
, sizeof(req
.nr_name
), s
->ifname
);
357 req
.nr_version
= NETMAP_API
;
358 req
.nr_cmd
= NETMAP_BDG_VNET_HDR
;
360 err
= ioctl(s
->nmd
->fd
, NIOCREGIF
, &req
);
362 error_report("Unable to execute NETMAP_BDG_VNET_HDR on %s: %s",
363 s
->ifname
, strerror(errno
));
365 /* Keep track of the current length. */
366 s
->vnet_hdr_len
= len
;
370 static void netmap_set_offload(NetClientState
*nc
, int csum
, int tso4
, int tso6
,
373 NetmapState
*s
= DO_UPCAST(NetmapState
, nc
, nc
);
375 /* Setting a virtio-net header length greater than zero automatically
376 * enables the offloadings.
378 if (!s
->vnet_hdr_len
) {
379 netmap_set_vnet_hdr_len(nc
, sizeof(struct virtio_net_hdr
));
383 /* NetClientInfo methods */
384 static NetClientInfo net_netmap_info
= {
385 .type
= NET_CLIENT_OPTIONS_KIND_NETMAP
,
386 .size
= sizeof(NetmapState
),
387 .receive
= netmap_receive
,
388 .receive_iov
= netmap_receive_iov
,
390 .cleanup
= netmap_cleanup
,
391 .has_ufo
= netmap_has_ufo
,
392 .has_vnet_hdr
= netmap_has_vnet_hdr
,
393 .has_vnet_hdr_len
= netmap_has_vnet_hdr_len
,
394 .using_vnet_hdr
= netmap_using_vnet_hdr
,
395 .set_offload
= netmap_set_offload
,
396 .set_vnet_hdr_len
= netmap_set_vnet_hdr_len
,
399 /* The exported init function
401 * ... -net netmap,ifname="..."
403 int net_init_netmap(const NetClientOptions
*opts
,
404 const char *name
, NetClientState
*peer
, Error
**errp
)
406 const NetdevNetmapOptions
*netmap_opts
= opts
->u
.netmap
;
412 nmd
= netmap_open(netmap_opts
, &err
);
414 error_propagate(errp
, err
);
417 /* Create the object. */
418 nc
= qemu_new_net_client(&net_netmap_info
, peer
, "netmap", name
);
419 s
= DO_UPCAST(NetmapState
, nc
, nc
);
421 s
->tx
= NETMAP_TXRING(nmd
->nifp
, 0);
422 s
->rx
= NETMAP_RXRING(nmd
->nifp
, 0);
424 pstrcpy(s
->ifname
, sizeof(s
->ifname
), netmap_opts
->ifname
);
425 netmap_read_poll(s
, true); /* Initially only poll for reads. */