hw/arm: QOM'ify strongarm.c
[qemu/kevin.git] / net / netmap.c
blob6cc0db5ee1cfbd124b9483e02cd45b320685e874
1 /*
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
22 * THE SOFTWARE.
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>
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"
43 typedef 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; /* Current virtio-net header length. */
53 } NetmapState;
55 #ifndef __FreeBSD__
56 #define pkt_copy bcopy
57 #else
58 /* A fast copy routine only for multiples of 64 bytes, non overlapped. */
59 static inline void
60 pkt_copy(const void *_src, void *_dst, int l)
62 const uint64_t *src = _src;
63 uint64_t *dst = _dst;
64 if (unlikely(l >= 1024)) {
65 bcopy(src, dst, l);
66 return;
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++;
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,
86 Error **errp)
88 struct nm_desc *nmd;
89 struct nmreq req;
91 memset(&req, 0, sizeof(req));
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;
101 return nmd;
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
148 * buffered packets.
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(NetClientState *nc,
159 const uint8_t *buf, size_t size)
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;
167 if (unlikely(!ring)) {
168 /* Drop. */
169 return size;
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;
178 if (nm_ring_empty(ring)) {
179 /* No available slots in the netmap TX ring. */
180 netmap_write_poll(s, true);
181 return 0;
184 i = ring->cur;
185 idx = ring->slot[i].buf_idx;
186 dst = (uint8_t *)NETMAP_BUF(ring, idx);
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);
194 return size;
197 static ssize_t netmap_receive_iov(NetClientState *nc,
198 const struct iovec *iov, int iovcnt)
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;
208 if (unlikely(!ring)) {
209 /* Drop the packet. */
210 return iov_size(iov, iovcnt);
213 last = i = ring->cur;
215 if (nm_ring_space(ring) < iovcnt) {
216 /* Not enough netmap slots. */
217 netmap_write_poll(s, true);
218 return 0;
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;
226 /* Split each iovec fragment over more netmap slots, if
227 necessary. */
228 while (iov_frag_size) {
229 nm_frag_size = MIN(iov_frag_size, ring->nr_buf_size);
231 if (unlikely(nm_ring_empty(ring))) {
232 /* We run out of netmap slots while splitting the
233 iovec fragments. */
234 netmap_write_poll(s, true);
235 return 0;
238 idx = ring->slot[i].buf_idx;
239 dst = (uint8_t *)NETMAP_BUF(ring, idx);
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);
245 last = i;
246 i = nm_ring_next(ring, i);
248 offset += nm_frag_size;
249 iov_frag_size -= nm_frag_size;
252 /* The last slot must not have NS_MOREFRAG set. */
253 ring->slot[last].flags &= ~NS_MOREFRAG;
255 /* Now update ring->cur and ring->head. */
256 ring->cur = ring->head = i;
258 ioctl(s->nmd->fd, NIOCTXSYNC, NULL);
260 return iov_size(iov, iovcnt);
263 /* Complete a previous send (backend --> guest) and enable the
264 fd_read callback. */
265 static void netmap_send_completed(NetClientState *nc, ssize_t len)
267 NetmapState *s = DO_UPCAST(NetmapState, nc, nc);
269 netmap_read_poll(s, true);
272 static void netmap_send(void *opaque)
274 NetmapState *s = opaque;
275 struct netmap_ring *ring = s->rx;
277 /* Keep sending while there are available packets into the netmap
278 RX ring and the forwarding path towards the peer is open. */
279 while (!nm_ring_empty(ring)) {
280 uint32_t i;
281 uint32_t idx;
282 bool morefrag;
283 int iovcnt = 0;
284 int iovsize;
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++;
294 ring->cur = ring->head = nm_ring_next(ring, i);
295 } while (!nm_ring_empty(ring) && morefrag);
297 if (unlikely(nm_ring_empty(ring) && morefrag)) {
298 RD(5, "[netmap_send] ran out of slots, with a pending"
299 "incomplete packet\n");
302 iovsize = qemu_sendv_packet_async(&s->nc, s->iov, iovcnt,
303 netmap_send_completed);
305 if (iovsize == 0) {
306 /* The peer does not receive anymore. Packet is queued, stop
307 * reading from the backend until netmap_send_completed()
309 netmap_read_poll(s, false);
310 break;
315 /* Flush and close. */
316 static void netmap_cleanup(NetClientState *nc)
318 NetmapState *s = DO_UPCAST(NetmapState, nc, nc);
320 qemu_purge_queued_packets(nc);
322 netmap_poll(nc, false);
323 nm_close(s->nmd);
324 s->nmd = NULL;
327 /* Offloading manipulation support callbacks. */
328 static int netmap_fd_set_vnet_hdr_len(NetmapState *s, int len)
330 struct nmreq req;
332 /* Issue a NETMAP_BDG_VNET_HDR command to change the virtio-net header
333 * length for the netmap adapter associated to 's->ifname'.
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;
341 return ioctl(s->nmd->fd, NIOCREGIF, &req);
344 static bool netmap_has_vnet_hdr_len(NetClientState *nc, int len)
346 NetmapState *s = DO_UPCAST(NetmapState, nc, nc);
347 int prev_len = s->vnet_hdr_len;
349 /* Check that we can set the new length. */
350 if (netmap_fd_set_vnet_hdr_len(s, len)) {
351 return false;
354 /* Restore the previous length. */
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();
361 return true;
364 /* A netmap interface that supports virtio-net headers always
365 * supports UFO, so we use this callback also for the has_ufo hook. */
366 static bool netmap_has_vnet_hdr(NetClientState *nc)
368 return netmap_has_vnet_hdr_len(nc, sizeof(struct virtio_net_hdr));
371 static void netmap_using_vnet_hdr(NetClientState *nc, bool enable)
375 static void netmap_set_vnet_hdr_len(NetClientState *nc, int len)
377 NetmapState *s = DO_UPCAST(NetmapState, nc, nc);
378 int err;
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 /* Keep track of the current length. */
386 s->vnet_hdr_len = len;
390 static void netmap_set_offload(NetClientState *nc, int csum, int tso4, int tso6,
391 int ecn, int ufo)
393 NetmapState *s = DO_UPCAST(NetmapState, nc, nc);
395 /* Setting a virtio-net header length greater than zero automatically
396 * enables the offloadings. */
397 if (!s->vnet_hdr_len) {
398 netmap_set_vnet_hdr_len(nc, sizeof(struct virtio_net_hdr));
402 /* NetClientInfo methods */
403 static 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,
418 /* The exported init function
420 * ... -net netmap,ifname="..."
422 int net_init_netmap(const NetClientOptions *opts,
423 const char *name, NetClientState *peer, Error **errp)
425 const NetdevNetmapOptions *netmap_opts = opts->u.netmap.data;
426 struct nm_desc *nmd;
427 NetClientState *nc;
428 Error *err = NULL;
429 NetmapState *s;
431 nmd = netmap_open(netmap_opts, &err);
432 if (err) {
433 error_propagate(errp, err);
434 return -1;
436 /* Create the object. */
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); /* Initially only poll for reads. */
446 return 0;