aboutsummaryrefslogtreecommitdiff
path: root/net/netmap.c
diff options
context:
space:
mode:
Diffstat (limited to 'net/netmap.c')
-rw-r--r--net/netmap.c123
1 files changed, 80 insertions, 43 deletions
diff --git a/net/netmap.c b/net/netmap.c
index 0ccc4976b5..8213304a5b 100644
--- a/net/netmap.c
+++ b/net/netmap.c
@@ -27,10 +27,13 @@
#include <net/if.h>
#include <sys/mman.h>
#include <stdint.h>
+#include <stdio.h>
+#define NETMAP_WITH_LIBS
#include <net/netmap.h>
#include <net/netmap_user.h>
#include "net/net.h"
+#include "net/tap.h"
#include "clients.h"
#include "sysemu/sysemu.h"
#include "qemu/error-report.h"
@@ -54,33 +57,9 @@ typedef struct NetmapState {
bool read_poll;
bool write_poll;
struct iovec iov[IOV_MAX];
+ int vnet_hdr_len; /* Current virtio-net header length. */
} NetmapState;
-#define D(format, ...) \
- do { \
- struct timeval __xxts; \
- gettimeofday(&__xxts, NULL); \
- printf("%03d.%06d %s [%d] " format "\n", \
- (int)__xxts.tv_sec % 1000, (int)__xxts.tv_usec, \
- __func__, __LINE__, ##__VA_ARGS__); \
- } while (0)
-
-/* Rate limited version of "D", lps indicates how many per second */
-#define RD(lps, format, ...) \
- do { \
- static int t0, __cnt; \
- struct timeval __xxts; \
- gettimeofday(&__xxts, NULL); \
- if (t0 != __xxts.tv_sec) { \
- t0 = __xxts.tv_sec; \
- __cnt = 0; \
- } \
- if (__cnt++ < lps) { \
- D(format, ##__VA_ARGS__); \
- } \
- } while (0)
-
-
#ifndef __FreeBSD__
#define pkt_copy bcopy
#else
@@ -237,7 +216,7 @@ static ssize_t netmap_receive(NetClientState *nc,
return size;
}
- if (ring->avail == 0) {
+ if (nm_ring_empty(ring)) {
/* No available slots in the netmap TX ring. */
netmap_write_poll(s, true);
return 0;
@@ -250,8 +229,7 @@ static ssize_t netmap_receive(NetClientState *nc,
ring->slot[i].len = size;
ring->slot[i].flags = 0;
pkt_copy(buf, dst, size);
- ring->cur = NETMAP_RING_NEXT(ring, i);
- ring->avail--;
+ ring->cur = ring->head = nm_ring_next(ring, i);
ioctl(s->me.fd, NIOCTXSYNC, NULL);
return size;
@@ -267,17 +245,15 @@ static ssize_t netmap_receive_iov(NetClientState *nc,
uint8_t *dst;
int j;
uint32_t i;
- uint32_t avail;
if (unlikely(!ring)) {
/* Drop the packet. */
return iov_size(iov, iovcnt);
}
- i = ring->cur;
- avail = ring->avail;
+ last = i = ring->cur;
- if (avail < iovcnt) {
+ if (nm_ring_space(ring) < iovcnt) {
/* Not enough netmap slots. */
netmap_write_poll(s, true);
return 0;
@@ -293,7 +269,7 @@ static ssize_t netmap_receive_iov(NetClientState *nc,
while (iov_frag_size) {
nm_frag_size = MIN(iov_frag_size, ring->nr_buf_size);
- if (unlikely(avail == 0)) {
+ if (unlikely(nm_ring_empty(ring))) {
/* We run out of netmap slots while splitting the
iovec fragments. */
netmap_write_poll(s, true);
@@ -308,8 +284,7 @@ static ssize_t netmap_receive_iov(NetClientState *nc,
pkt_copy(iov[j].iov_base + offset, dst, nm_frag_size);
last = i;
- i = NETMAP_RING_NEXT(ring, i);
- avail--;
+ i = nm_ring_next(ring, i);
offset += nm_frag_size;
iov_frag_size -= nm_frag_size;
@@ -318,9 +293,8 @@ static ssize_t netmap_receive_iov(NetClientState *nc,
/* The last slot must not have NS_MOREFRAG set. */
ring->slot[last].flags &= ~NS_MOREFRAG;
- /* Now update ring->cur and ring->avail. */
- ring->cur = i;
- ring->avail = avail;
+ /* Now update ring->cur and ring->head. */
+ ring->cur = ring->head = i;
ioctl(s->me.fd, NIOCTXSYNC, NULL);
@@ -343,7 +317,7 @@ static void netmap_send(void *opaque)
/* Keep sending while there are available packets into the netmap
RX ring and the forwarding path towards the peer is open. */
- while (ring->avail > 0 && qemu_can_send_packet(&s->nc)) {
+ while (!nm_ring_empty(ring) && qemu_can_send_packet(&s->nc)) {
uint32_t i;
uint32_t idx;
bool morefrag;
@@ -358,11 +332,10 @@ static void netmap_send(void *opaque)
s->iov[iovcnt].iov_len = ring->slot[i].len;
iovcnt++;
- ring->cur = NETMAP_RING_NEXT(ring, i);
- ring->avail--;
- } while (ring->avail && morefrag);
+ ring->cur = ring->head = nm_ring_next(ring, i);
+ } while (!nm_ring_empty(ring) && morefrag);
- if (unlikely(!ring->avail && morefrag)) {
+ if (unlikely(nm_ring_empty(ring) && morefrag)) {
RD(5, "[netmap_send] ran out of slots, with a pending"
"incomplete packet\n");
}
@@ -394,6 +367,63 @@ static void netmap_cleanup(NetClientState *nc)
s->me.fd = -1;
}
+/* Offloading manipulation support callbacks. */
+static bool netmap_has_ufo(NetClientState *nc)
+{
+ return true;
+}
+
+static bool netmap_has_vnet_hdr(NetClientState *nc)
+{
+ return true;
+}
+
+static bool netmap_has_vnet_hdr_len(NetClientState *nc, int len)
+{
+ return len == 0 || len == sizeof(struct virtio_net_hdr) ||
+ len == sizeof(struct virtio_net_hdr_mrg_rxbuf);
+}
+
+static void netmap_using_vnet_hdr(NetClientState *nc, bool enable)
+{
+}
+
+static void netmap_set_vnet_hdr_len(NetClientState *nc, int len)
+{
+ NetmapState *s = DO_UPCAST(NetmapState, nc, nc);
+ int err;
+ struct nmreq req;
+
+ /* Issue a NETMAP_BDG_VNET_HDR command to change the virtio-net header
+ * length for the netmap adapter associated to 'me->ifname'.
+ */
+ memset(&req, 0, sizeof(req));
+ pstrcpy(req.nr_name, sizeof(req.nr_name), s->me.ifname);
+ req.nr_version = NETMAP_API;
+ req.nr_cmd = NETMAP_BDG_VNET_HDR;
+ req.nr_arg1 = len;
+ err = ioctl(s->me.fd, NIOCREGIF, &req);
+ if (err) {
+ error_report("Unable to execute NETMAP_BDG_VNET_HDR on %s: %s",
+ s->me.ifname, strerror(errno));
+ } else {
+ /* Keep track of the current length. */
+ s->vnet_hdr_len = len;
+ }
+}
+
+static void netmap_set_offload(NetClientState *nc, int csum, int tso4, int tso6,
+ int ecn, int ufo)
+{
+ NetmapState *s = DO_UPCAST(NetmapState, nc, nc);
+
+ /* Setting a virtio-net header length greater than zero automatically
+ * enables the offloadings.
+ */
+ if (!s->vnet_hdr_len) {
+ netmap_set_vnet_hdr_len(nc, sizeof(struct virtio_net_hdr));
+ }
+}
/* NetClientInfo methods */
static NetClientInfo net_netmap_info = {
@@ -403,6 +433,12 @@ static NetClientInfo net_netmap_info = {
.receive_iov = netmap_receive_iov,
.poll = netmap_poll,
.cleanup = netmap_cleanup,
+ .has_ufo = netmap_has_ufo,
+ .has_vnet_hdr = netmap_has_vnet_hdr,
+ .has_vnet_hdr_len = netmap_has_vnet_hdr_len,
+ .using_vnet_hdr = netmap_using_vnet_hdr,
+ .set_offload = netmap_set_offload,
+ .set_vnet_hdr_len = netmap_set_vnet_hdr_len,
};
/* The exported init function
@@ -428,6 +464,7 @@ int net_init_netmap(const NetClientOptions *opts,
nc = qemu_new_net_client(&net_netmap_info, peer, "netmap", name);
s = DO_UPCAST(NetmapState, nc, nc);
s->me = me;
+ s->vnet_hdr_len = 0;
netmap_read_poll(s, true); /* Initially only poll for reads. */
return 0;