]> git.proxmox.com Git - mirror_qemu.git/blame - net/netmap.c
net: simplify net_init_tap_one logic
[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
2744d920 26#include "qemu/osdep.h"
58952137
VM
27#include <sys/ioctl.h>
28#include <net/if.h>
29#include <sys/mman.h>
0a985b37 30#define NETMAP_WITH_LIBS
58952137
VM
31#include <net/netmap.h>
32#include <net/netmap_user.h>
33
34#include "net/net.h"
f6c65bfb 35#include "net/tap.h"
58952137
VM
36#include "clients.h"
37#include "sysemu/sysemu.h"
38#include "qemu/error-report.h"
39#include "qemu/iov.h"
40
58952137
VM
41typedef struct NetmapState {
42 NetClientState nc;
ab685220
VM
43 struct nm_desc *nmd;
44 char ifname[IFNAMSIZ];
45 struct netmap_ring *tx;
46 struct netmap_ring *rx;
58952137
VM
47 bool read_poll;
48 bool write_poll;
49 struct iovec iov[IOV_MAX];
f6c65bfb 50 int vnet_hdr_len; /* Current virtio-net header length. */
58952137
VM
51} NetmapState;
52
58952137
VM
53#ifndef __FreeBSD__
54#define pkt_copy bcopy
55#else
56/* A fast copy routine only for multiples of 64 bytes, non overlapped. */
57static inline void
58pkt_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 */
ab685220
VM
83static struct nm_desc *netmap_open(const NetdevNetmapOptions *nm_opts,
84 Error **errp)
58952137 85{
ab685220 86 struct nm_desc *nmd;
58952137
VM
87 struct nmreq req;
88
58952137 89 memset(&req, 0, sizeof(req));
58952137 90
ab685220
VM
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;
58952137
VM
97 }
98
ab685220 99 return nmd;
58952137
VM
100}
101
58952137
VM
102static void netmap_send(void *opaque);
103static void netmap_writable(void *opaque);
104
105/* Set the event-loop handlers for the netmap backend. */
106static void netmap_update_fd_handler(NetmapState *s)
107{
ab685220 108 qemu_set_fd_handler(s->nmd->fd,
82e1cc4b
FZ
109 s->read_poll ? netmap_send : NULL,
110 s->write_poll ? netmap_writable : NULL,
111 s);
58952137
VM
112}
113
114/* Update the read handler. */
115static 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. */
124static 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
132static 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) {
131e744a
PJ
137 s->write_poll = enable;
138 s->read_poll = enable;
58952137
VM
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 */
148static 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
156static ssize_t netmap_receive(NetClientState *nc,
157 const uint8_t *buf, size_t size)
158{
159 NetmapState *s = DO_UPCAST(NetmapState, nc, nc);
ab685220 160 struct netmap_ring *ring = s->tx;
58952137
VM
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
0a985b37 176 if (nm_ring_empty(ring)) {
58952137
VM
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);
0a985b37 189 ring->cur = ring->head = nm_ring_next(ring, i);
ab685220 190 ioctl(s->nmd->fd, NIOCTXSYNC, NULL);
58952137
VM
191
192 return size;
193}
194
195static ssize_t netmap_receive_iov(NetClientState *nc,
196 const struct iovec *iov, int iovcnt)
197{
198 NetmapState *s = DO_UPCAST(NetmapState, nc, nc);
ab685220 199 struct netmap_ring *ring = s->tx;
58952137
VM
200 uint32_t last;
201 uint32_t idx;
202 uint8_t *dst;
203 int j;
204 uint32_t i;
58952137
VM
205
206 if (unlikely(!ring)) {
207 /* Drop the packet. */
208 return iov_size(iov, iovcnt);
209 }
210
f6c65bfb 211 last = i = ring->cur;
58952137 212
0a985b37 213 if (nm_ring_space(ring) < iovcnt) {
58952137
VM
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
0a985b37 229 if (unlikely(nm_ring_empty(ring))) {
58952137
VM
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;
0a985b37 244 i = nm_ring_next(ring, i);
58952137
VM
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
0a985b37
VM
253 /* Now update ring->cur and ring->head. */
254 ring->cur = ring->head = i;
58952137 255
ab685220 256 ioctl(s->nmd->fd, NIOCTXSYNC, NULL);
58952137
VM
257
258 return iov_size(iov, iovcnt);
259}
260
261/* Complete a previous send (backend --> guest) and enable the
262 fd_read callback. */
263static 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
270static void netmap_send(void *opaque)
271{
272 NetmapState *s = opaque;
ab685220 273 struct netmap_ring *ring = s->rx;
58952137
VM
274
275 /* Keep sending while there are available packets into the netmap
276 RX ring and the forwarding path towards the peer is open. */
e8dd1d9c 277 while (!nm_ring_empty(ring)) {
58952137
VM
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
0a985b37
VM
292 ring->cur = ring->head = nm_ring_next(ring, i);
293 } while (!nm_ring_empty(ring) && morefrag);
58952137 294
0a985b37 295 if (unlikely(nm_ring_empty(ring) && morefrag)) {
58952137
VM
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. */
314static 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);
ab685220
VM
321 nm_close(s->nmd);
322 s->nmd = NULL;
58952137
VM
323}
324
f6c65bfb
VM
325/* Offloading manipulation support callbacks. */
326static bool netmap_has_ufo(NetClientState *nc)
327{
328 return true;
329}
330
331static bool netmap_has_vnet_hdr(NetClientState *nc)
332{
333 return true;
334}
335
336static 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
342static void netmap_using_vnet_hdr(NetClientState *nc, bool enable)
343{
344}
345
346static 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
ab685220 353 * length for the netmap adapter associated to 's->ifname'.
f6c65bfb
VM
354 */
355 memset(&req, 0, sizeof(req));
ab685220 356 pstrcpy(req.nr_name, sizeof(req.nr_name), s->ifname);
f6c65bfb
VM
357 req.nr_version = NETMAP_API;
358 req.nr_cmd = NETMAP_BDG_VNET_HDR;
359 req.nr_arg1 = len;
ab685220 360 err = ioctl(s->nmd->fd, NIOCREGIF, &req);
f6c65bfb
VM
361 if (err) {
362 error_report("Unable to execute NETMAP_BDG_VNET_HDR on %s: %s",
ab685220 363 s->ifname, strerror(errno));
f6c65bfb
VM
364 } else {
365 /* Keep track of the current length. */
366 s->vnet_hdr_len = len;
367 }
368}
369
370static 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}
58952137
VM
382
383/* NetClientInfo methods */
384static 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,
f6c65bfb
VM
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,
58952137
VM
397};
398
399/* The exported init function
400 *
401 * ... -net netmap,ifname="..."
402 */
403int net_init_netmap(const NetClientOptions *opts,
a30ecde6 404 const char *name, NetClientState *peer, Error **errp)
58952137 405{
54c59b4d 406 const NetdevNetmapOptions *netmap_opts = opts->u.netmap;
ab685220 407 struct nm_desc *nmd;
58952137 408 NetClientState *nc;
39bec4f3 409 Error *err = NULL;
58952137
VM
410 NetmapState *s;
411
ab685220 412 nmd = netmap_open(netmap_opts, &err);
39bec4f3
VM
413 if (err) {
414 error_propagate(errp, err);
58952137
VM
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);
ab685220
VM
420 s->nmd = nmd;
421 s->tx = NETMAP_TXRING(nmd->nifp, 0);
422 s->rx = NETMAP_RXRING(nmd->nifp, 0);
f6c65bfb 423 s->vnet_hdr_len = 0;
ab685220 424 pstrcpy(s->ifname, sizeof(s->ifname), netmap_opts->ifname);
58952137
VM
425 netmap_read_poll(s, true); /* Initially only poll for reads. */
426
427 return 0;
428}
429