xref: /qemu/net/netmap.c (revision 7a4e543d)
1 /*
2  * netmap access for qemu
3  *
4  * Copyright (c) 2012-2013 Luigi Rizzo
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
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.
23  */
24 
25 
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>
33 
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 "qemu/iov.h"
40 
41 typedef struct NetmapState {
42     NetClientState      nc;
43     struct nm_desc      *nmd;
44     char                ifname[IFNAMSIZ];
45     struct netmap_ring  *tx;
46     struct netmap_ring  *rx;
47     bool                read_poll;
48     bool                write_poll;
49     struct iovec        iov[IOV_MAX];
50     int                 vnet_hdr_len;  /* Current virtio-net header length. */
51 } NetmapState;
52 
53 #ifndef __FreeBSD__
54 #define pkt_copy bcopy
55 #else
56 /* A fast copy routine only for multiples of 64 bytes, non overlapped. */
57 static inline void
58 pkt_copy(const void *_src, void *_dst, int l)
59 {
60     const uint64_t *src = _src;
61     uint64_t *dst = _dst;
62     if (unlikely(l >= 1024)) {
63         bcopy(src, dst, l);
64         return;
65     }
66     for (; l > 0; l -= 64) {
67         *dst++ = *src++;
68         *dst++ = *src++;
69         *dst++ = *src++;
70         *dst++ = *src++;
71         *dst++ = *src++;
72         *dst++ = *src++;
73         *dst++ = *src++;
74         *dst++ = *src++;
75     }
76 }
77 #endif /* __FreeBSD__ */
78 
79 /*
80  * Open a netmap device. We assume there is only one queue
81  * (which is the case for the VALE bridge).
82  */
83 static struct nm_desc *netmap_open(const NetdevNetmapOptions *nm_opts,
84                                    Error **errp)
85 {
86     struct nm_desc *nmd;
87     struct nmreq req;
88 
89     memset(&req, 0, sizeof(req));
90 
91     nmd = nm_open(nm_opts->ifname, &req, NETMAP_NO_TX_POLL,
92                   NULL);
93     if (nmd == NULL) {
94         error_setg_errno(errp, errno, "Failed to nm_open() %s",
95                          nm_opts->ifname);
96         return NULL;
97     }
98 
99     return nmd;
100 }
101 
102 static void netmap_send(void *opaque);
103 static void netmap_writable(void *opaque);
104 
105 /* Set the event-loop handlers for the netmap backend. */
106 static void netmap_update_fd_handler(NetmapState *s)
107 {
108     qemu_set_fd_handler(s->nmd->fd,
109                         s->read_poll ? netmap_send : NULL,
110                         s->write_poll ? netmap_writable : NULL,
111                         s);
112 }
113 
114 /* Update the read handler. */
115 static void netmap_read_poll(NetmapState *s, bool enable)
116 {
117     if (s->read_poll != enable) { /* Do nothing if not changed. */
118         s->read_poll = enable;
119         netmap_update_fd_handler(s);
120     }
121 }
122 
123 /* Update the write handler. */
124 static void netmap_write_poll(NetmapState *s, bool enable)
125 {
126     if (s->write_poll != enable) {
127         s->write_poll = enable;
128         netmap_update_fd_handler(s);
129     }
130 }
131 
132 static void netmap_poll(NetClientState *nc, bool enable)
133 {
134     NetmapState *s = DO_UPCAST(NetmapState, nc, nc);
135 
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);
140     }
141 }
142 
143 /*
144  * The fd_write() callback, invoked if the fd is marked as
145  * writable after a poll. Unregister the handler and flush any
146  * buffered packets.
147  */
148 static void netmap_writable(void *opaque)
149 {
150     NetmapState *s = opaque;
151 
152     netmap_write_poll(s, false);
153     qemu_flush_queued_packets(&s->nc);
154 }
155 
156 static ssize_t netmap_receive(NetClientState *nc,
157       const uint8_t *buf, size_t size)
158 {
159     NetmapState *s = DO_UPCAST(NetmapState, nc, nc);
160     struct netmap_ring *ring = s->tx;
161     uint32_t i;
162     uint32_t idx;
163     uint8_t *dst;
164 
165     if (unlikely(!ring)) {
166         /* Drop. */
167         return size;
168     }
169 
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);
173         return size;
174     }
175 
176     if (nm_ring_empty(ring)) {
177         /* No available slots in the netmap TX ring. */
178         netmap_write_poll(s, true);
179         return 0;
180     }
181 
182     i = ring->cur;
183     idx = ring->slot[i].buf_idx;
184     dst = (uint8_t *)NETMAP_BUF(ring, idx);
185 
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);
191 
192     return size;
193 }
194 
195 static ssize_t netmap_receive_iov(NetClientState *nc,
196                     const struct iovec *iov, int iovcnt)
197 {
198     NetmapState *s = DO_UPCAST(NetmapState, nc, nc);
199     struct netmap_ring *ring = s->tx;
200     uint32_t last;
201     uint32_t idx;
202     uint8_t *dst;
203     int j;
204     uint32_t i;
205 
206     if (unlikely(!ring)) {
207         /* Drop the packet. */
208         return iov_size(iov, iovcnt);
209     }
210 
211     last = i = ring->cur;
212 
213     if (nm_ring_space(ring) < iovcnt) {
214         /* Not enough netmap slots. */
215         netmap_write_poll(s, true);
216         return 0;
217     }
218 
219     for (j = 0; j < iovcnt; j++) {
220         int iov_frag_size = iov[j].iov_len;
221         int offset = 0;
222         int nm_frag_size;
223 
224         /* Split each iovec fragment over more netmap slots, if
225            necessary. */
226         while (iov_frag_size) {
227             nm_frag_size = MIN(iov_frag_size, ring->nr_buf_size);
228 
229             if (unlikely(nm_ring_empty(ring))) {
230                 /* We run out of netmap slots while splitting the
231                    iovec fragments. */
232                 netmap_write_poll(s, true);
233                 return 0;
234             }
235 
236             idx = ring->slot[i].buf_idx;
237             dst = (uint8_t *)NETMAP_BUF(ring, idx);
238 
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);
242 
243             last = i;
244             i = nm_ring_next(ring, i);
245 
246             offset += nm_frag_size;
247             iov_frag_size -= nm_frag_size;
248         }
249     }
250     /* The last slot must not have NS_MOREFRAG set. */
251     ring->slot[last].flags &= ~NS_MOREFRAG;
252 
253     /* Now update ring->cur and ring->head. */
254     ring->cur = ring->head = i;
255 
256     ioctl(s->nmd->fd, NIOCTXSYNC, NULL);
257 
258     return iov_size(iov, iovcnt);
259 }
260 
261 /* Complete a previous send (backend --> guest) and enable the
262    fd_read callback. */
263 static void netmap_send_completed(NetClientState *nc, ssize_t len)
264 {
265     NetmapState *s = DO_UPCAST(NetmapState, nc, nc);
266 
267     netmap_read_poll(s, true);
268 }
269 
270 static void netmap_send(void *opaque)
271 {
272     NetmapState *s = opaque;
273     struct netmap_ring *ring = s->rx;
274 
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)) {
278         uint32_t i;
279         uint32_t idx;
280         bool morefrag;
281         int iovcnt = 0;
282         int iovsize;
283 
284         do {
285             i = ring->cur;
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;
290             iovcnt++;
291 
292             ring->cur = ring->head = nm_ring_next(ring, i);
293         } while (!nm_ring_empty(ring) && morefrag);
294 
295         if (unlikely(nm_ring_empty(ring) && morefrag)) {
296             RD(5, "[netmap_send] ran out of slots, with a pending"
297                    "incomplete packet\n");
298         }
299 
300         iovsize = qemu_sendv_packet_async(&s->nc, s->iov, iovcnt,
301                                             netmap_send_completed);
302 
303         if (iovsize == 0) {
304             /* The peer does not receive anymore. Packet is queued, stop
305              * reading from the backend until netmap_send_completed()
306              */
307             netmap_read_poll(s, false);
308             break;
309         }
310     }
311 }
312 
313 /* Flush and close. */
314 static void netmap_cleanup(NetClientState *nc)
315 {
316     NetmapState *s = DO_UPCAST(NetmapState, nc, nc);
317 
318     qemu_purge_queued_packets(nc);
319 
320     netmap_poll(nc, false);
321     nm_close(s->nmd);
322     s->nmd = NULL;
323 }
324 
325 /* Offloading manipulation support callbacks. */
326 static bool netmap_has_ufo(NetClientState *nc)
327 {
328     return true;
329 }
330 
331 static bool netmap_has_vnet_hdr(NetClientState *nc)
332 {
333     return true;
334 }
335 
336 static bool netmap_has_vnet_hdr_len(NetClientState *nc, int len)
337 {
338     return len == 0 || len == sizeof(struct virtio_net_hdr) ||
339                 len == sizeof(struct virtio_net_hdr_mrg_rxbuf);
340 }
341 
342 static void netmap_using_vnet_hdr(NetClientState *nc, bool enable)
343 {
344 }
345 
346 static void netmap_set_vnet_hdr_len(NetClientState *nc, int len)
347 {
348     NetmapState *s = DO_UPCAST(NetmapState, nc, nc);
349     int err;
350     struct nmreq req;
351 
352     /* Issue a NETMAP_BDG_VNET_HDR command to change the virtio-net header
353      * length for the netmap adapter associated to 's->ifname'.
354      */
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;
359     req.nr_arg1 = len;
360     err = ioctl(s->nmd->fd, NIOCREGIF, &req);
361     if (err) {
362         error_report("Unable to execute NETMAP_BDG_VNET_HDR on %s: %s",
363                      s->ifname, strerror(errno));
364     } else {
365         /* Keep track of the current length. */
366         s->vnet_hdr_len = len;
367     }
368 }
369 
370 static void netmap_set_offload(NetClientState *nc, int csum, int tso4, int tso6,
371                                int ecn, int ufo)
372 {
373     NetmapState *s = DO_UPCAST(NetmapState, nc, nc);
374 
375     /* Setting a virtio-net header length greater than zero automatically
376      * enables the offloadings.
377      */
378     if (!s->vnet_hdr_len) {
379         netmap_set_vnet_hdr_len(nc, sizeof(struct virtio_net_hdr));
380     }
381 }
382 
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,
389     .poll = netmap_poll,
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,
397 };
398 
399 /* The exported init function
400  *
401  * ... -net netmap,ifname="..."
402  */
403 int net_init_netmap(const NetClientOptions *opts,
404                     const char *name, NetClientState *peer, Error **errp)
405 {
406     const NetdevNetmapOptions *netmap_opts = opts->u.netmap;
407     struct nm_desc *nmd;
408     NetClientState *nc;
409     Error *err = NULL;
410     NetmapState *s;
411 
412     nmd = netmap_open(netmap_opts, &err);
413     if (err) {
414         error_propagate(errp, err);
415         return -1;
416     }
417     /* Create the object. */
418     nc = qemu_new_net_client(&net_netmap_info, peer, "netmap", name);
419     s = DO_UPCAST(NetmapState, nc, nc);
420     s->nmd = nmd;
421     s->tx = NETMAP_TXRING(nmd->nifp, 0);
422     s->rx = NETMAP_RXRING(nmd->nifp, 0);
423     s->vnet_hdr_len = 0;
424     pstrcpy(s->ifname, sizeof(s->ifname), netmap_opts->ifname);
425     netmap_read_poll(s, true); /* Initially only poll for reads. */
426 
427     return 0;
428 }
429 
430