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