Patchwork net/socket: remove hardcoded packet size in favor of new mtu parameter

login
register
mail settings
Submitter Nguyễn Thái Ngọc Duy
Date April 22, 2011, 1:39 a.m.
Message ID <1303436374-23606-1-git-send-email-pclouds@gmail.com>
Download mbox | patch
Permalink /patch/92496/
State New
Headers show

Comments

Nguyễn Thái Ngọc Duy - April 22, 2011, 1:39 a.m.
Also mention the default value 4096.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 I have a driver that sends 4352 byte packets. Tested with tcp socket only.
 There's also 4096 byte buffers in vde, but I don't use/test it.

 net.c           |    4 ++++
 net/socket.c    |   54 ++++++++++++++++++++++++++++++++++++++----------------
 qemu-options.hx |   11 +++++++----
 3 files changed, 49 insertions(+), 20 deletions(-)
Stefan Hajnoczi - April 22, 2011, 6:29 a.m.
2011/4/22 Nguyễn Thái Ngọc Duy <pclouds@gmail.com>:
> @@ -288,6 +291,10 @@ static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan,
>
>     s = DO_UPCAST(NetSocketState, nc, nc);
>
> +    s->mtu = mtu;
> +    s->buf = qemu_malloc(s->mtu);
> +    s->buf1 = qemu_malloc(s->mtu);

These need to be freed in net_socket_cleanup().

Stefan

Patch

diff --git a/net.c b/net.c
index 4f777c3..74dffa3 100644
--- a/net.c
+++ b/net.c
@@ -1000,6 +1000,10 @@  static const struct {
                 .name = "localaddr",
                 .type = QEMU_OPT_STRING,
                 .help = "source address for multicast packets",
+            }, {
+                .name = "mtu",
+                .type = QEMU_OPT_NUMBER,
+                .help = "MTU size",
             },
             { /* end of list */ }
         },
diff --git a/net/socket.c b/net/socket.c
index 7337f4f..99a9b39 100644
--- a/net/socket.c
+++ b/net/socket.c
@@ -38,7 +38,8 @@  typedef struct NetSocketState {
     int state; /* 0 = getting length, 1 = getting data */
     unsigned int index;
     unsigned int packet_len;
-    uint8_t buf[4096];
+    unsigned int mtu;
+    uint8_t *buf, *buf1;
     struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
 } NetSocketState;
 
@@ -47,6 +48,7 @@  typedef struct NetSocketListenState {
     char *model;
     char *name;
     int fd;
+    unsigned int mtu;
 } NetSocketListenState;
 
 /* XXX: we consider we can send the whole packet without blocking */
@@ -73,10 +75,10 @@  static void net_socket_send(void *opaque)
     NetSocketState *s = opaque;
     int size, err;
     unsigned l;
-    uint8_t buf1[4096];
+    uint8_t *buf1 = s->buf1;
     const uint8_t *buf;
 
-    size = recv(s->fd, (void *)buf1, sizeof(buf1), 0);
+    size = recv(s->fd, (void *)buf1, s->mtu, 0);
     if (size < 0) {
         err = socket_error();
         if (err != EWOULDBLOCK)
@@ -111,7 +113,7 @@  static void net_socket_send(void *opaque)
             l = s->packet_len - s->index;
             if (l > size)
                 l = size;
-            if (s->index + l <= sizeof(s->buf)) {
+            if (s->index + l <= s->mtu) {
                 memcpy(s->buf + s->index, buf, l);
             } else {
                 fprintf(stderr, "serious error: oversized packet received,"
@@ -138,7 +140,7 @@  static void net_socket_send_dgram(void *opaque)
     NetSocketState *s = opaque;
     int size;
 
-    size = recv(s->fd, (void *)s->buf, sizeof(s->buf), 0);
+    size = recv(s->fd, (void *)s->buf, s->mtu, 0);
     if (size < 0)
         return;
     if (size == 0) {
@@ -238,6 +240,7 @@  static NetClientInfo net_dgram_socket_info = {
 };
 
 static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan,
+                                                unsigned int mtu,
                                                 const char *model,
                                                 const char *name,
                                                 int fd, int is_connected)
@@ -288,6 +291,10 @@  static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan,
 
     s = DO_UPCAST(NetSocketState, nc, nc);
 
+    s->mtu = mtu;
+    s->buf = qemu_malloc(s->mtu);
+    s->buf1 = qemu_malloc(s->mtu);
+
     s->fd = fd;
 
     qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s);
@@ -312,6 +319,7 @@  static NetClientInfo net_socket_info = {
 };
 
 static NetSocketState *net_socket_fd_init_stream(VLANState *vlan,
+                                                 unsigned int mtu,
                                                  const char *model,
                                                  const char *name,
                                                  int fd, int is_connected)
@@ -325,6 +333,10 @@  static NetSocketState *net_socket_fd_init_stream(VLANState *vlan,
 
     s = DO_UPCAST(NetSocketState, nc, nc);
 
+    s->mtu = mtu;
+    s->buf = qemu_malloc(s->mtu);
+    s->buf1 = qemu_malloc(s->mtu);
+
     s->fd = fd;
 
     if (is_connected) {
@@ -335,7 +347,7 @@  static NetSocketState *net_socket_fd_init_stream(VLANState *vlan,
     return s;
 }
 
-static NetSocketState *net_socket_fd_init(VLANState *vlan,
+static NetSocketState *net_socket_fd_init(VLANState *vlan, unsigned int mtu,
                                           const char *model, const char *name,
                                           int fd, int is_connected)
 {
@@ -348,13 +360,16 @@  static NetSocketState *net_socket_fd_init(VLANState *vlan,
     }
     switch(so_type) {
     case SOCK_DGRAM:
-        return net_socket_fd_init_dgram(vlan, model, name, fd, is_connected);
+        return net_socket_fd_init_dgram(vlan, mtu, model, name,
+                                        fd, is_connected);
     case SOCK_STREAM:
-        return net_socket_fd_init_stream(vlan, model, name, fd, is_connected);
+        return net_socket_fd_init_stream(vlan, mtu, model, name,
+                                         fd, is_connected);
     default:
         /* who knows ... this could be a eg. a pty, do warn and continue as stream */
         fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd);
-        return net_socket_fd_init_stream(vlan, model, name, fd, is_connected);
+        return net_socket_fd_init_stream(vlan, mtu, model, name,
+                                         fd, is_connected);
     }
     return NULL;
 }
@@ -376,7 +391,7 @@  static void net_socket_accept(void *opaque)
             break;
         }
     }
-    s1 = net_socket_fd_init(s->vlan, s->model, s->name, fd, 1);
+    s1 = net_socket_fd_init(s->vlan, s->mtu, s->model, s->name, fd, 1);
     if (!s1) {
         closesocket(fd);
     } else {
@@ -387,6 +402,7 @@  static void net_socket_accept(void *opaque)
 }
 
 static int net_socket_listen_init(VLANState *vlan,
+                                  unsigned int mtu,
                                   const char *model,
                                   const char *name,
                                   const char *host_str)
@@ -425,11 +441,13 @@  static int net_socket_listen_init(VLANState *vlan,
     s->model = qemu_strdup(model);
     s->name = name ? qemu_strdup(name) : NULL;
     s->fd = fd;
+    s->mtu = mtu;
     qemu_set_fd_handler(fd, net_socket_accept, NULL, s);
     return 0;
 }
 
 static int net_socket_connect_init(VLANState *vlan,
+                                   unsigned int mtu,
                                    const char *model,
                                    const char *name,
                                    const char *host_str)
@@ -470,7 +488,7 @@  static int net_socket_connect_init(VLANState *vlan,
             break;
         }
     }
-    s = net_socket_fd_init(vlan, model, name, fd, connected);
+    s = net_socket_fd_init(vlan, mtu, model, name, fd, connected);
     if (!s)
         return -1;
     snprintf(s->nc.info_str, sizeof(s->nc.info_str),
@@ -480,6 +498,7 @@  static int net_socket_connect_init(VLANState *vlan,
 }
 
 static int net_socket_mcast_init(VLANState *vlan,
+                                 unsigned int mtu,
                                  const char *model,
                                  const char *name,
                                  const char *host_str,
@@ -505,7 +524,7 @@  static int net_socket_mcast_init(VLANState *vlan,
     if (fd < 0)
 	return -1;
 
-    s = net_socket_fd_init(vlan, model, name, fd, 0);
+    s = net_socket_fd_init(vlan, mtu, model, name, fd, 0);
     if (!s)
         return -1;
 
@@ -523,6 +542,8 @@  int net_init_socket(QemuOpts *opts,
                     const char *name,
                     VLANState *vlan)
 {
+    unsigned int mtu = qemu_opt_get_number(opts, "mtu", 4096);
+
     if (qemu_opt_get(opts, "fd")) {
         int fd;
 
@@ -539,7 +560,7 @@  int net_init_socket(QemuOpts *opts,
             return -1;
         }
 
-        if (!net_socket_fd_init(vlan, "socket", name, fd, 1)) {
+        if (!net_socket_fd_init(vlan, mtu, "socket", name, fd, 1)) {
             close(fd);
             return -1;
         }
@@ -556,7 +577,7 @@  int net_init_socket(QemuOpts *opts,
 
         listen = qemu_opt_get(opts, "listen");
 
-        if (net_socket_listen_init(vlan, "socket", name, listen) == -1) {
+        if (net_socket_listen_init(vlan, mtu, "socket", name, listen) == -1) {
             return -1;
         }
     } else if (qemu_opt_get(opts, "connect")) {
@@ -572,7 +593,7 @@  int net_init_socket(QemuOpts *opts,
 
         connect = qemu_opt_get(opts, "connect");
 
-        if (net_socket_connect_init(vlan, "socket", name, connect) == -1) {
+        if (net_socket_connect_init(vlan, mtu, "socket", name, connect) == -1) {
             return -1;
         }
     } else if (qemu_opt_get(opts, "mcast")) {
@@ -588,7 +609,8 @@  int net_init_socket(QemuOpts *opts,
         mcast = qemu_opt_get(opts, "mcast");
         localaddr = qemu_opt_get(opts, "localaddr");
 
-        if (net_socket_mcast_init(vlan, "socket", name, mcast, localaddr) == -1) {
+        if (net_socket_mcast_init(vlan, mtu, "socket", name,
+                                  mcast, localaddr) == -1) {
             return -1;
         }
     } else {
diff --git a/qemu-options.hx b/qemu-options.hx
index 677c550..abf6045 100644
--- a/qemu-options.hx
+++ b/qemu-options.hx
@@ -1093,9 +1093,9 @@  DEF("net", HAS_ARG, QEMU_OPTION_net,
     "                use vhostforce=on to force vhost on for non-MSIX virtio guests\n"
     "                use 'vhostfd=h' to connect to an already opened vhost net device\n"
 #endif
-    "-net socket[,vlan=n][,name=str][,fd=h][,listen=[host]:port][,connect=host:port]\n"
+    "-net socket[,vlan=n][,name=str][,fd=h][,listen=[host]:port][,connect=host:port][,mtu=mtu]\n"
     "                connect the vlan 'n' to another VLAN using a socket connection\n"
-    "-net socket[,vlan=n][,name=str][,fd=h][,mcast=maddr:port[,localaddr=addr]]\n"
+    "-net socket[,vlan=n][,name=str][,fd=h][,mcast=maddr:port[,localaddr=addr]][,mtu=mtu]\n"
     "                connect the vlan 'n' to multicast maddr and port\n"
     "                use 'localaddr=addr' to specify the host address to send packets from\n"
 #ifdef CONFIG_VDE
@@ -1271,7 +1271,7 @@  qemu linux.img -net nic,vlan=0 -net tap,vlan=0,ifname=tap0 \
                -net nic,vlan=1 -net tap,vlan=1,ifname=tap1
 @end example
 
-@item -net socket[,vlan=@var{n}][,name=@var{name}][,fd=@var{h}] [,listen=[@var{host}]:@var{port}][,connect=@var{host}:@var{port}]
+@item -net socket[,vlan=@var{n}][,name=@var{name}][,fd=@var{h}] [,listen=[@var{host}]:@var{port}][,connect=@var{host}:@var{port}][,mtu=@var{mtu}]
 
 Connect the VLAN @var{n} to a remote VLAN in another QEMU virtual
 machine using a TCP socket connection. If @option{listen} is
@@ -1279,6 +1279,8 @@  specified, QEMU waits for incoming connections on @var{port}
 (@var{host} is optional). @option{connect} is used to connect to
 another QEMU instance using the @option{listen} option. @option{fd}=@var{h}
 specifies an already opened TCP socket.
+MTU is 4096 by default. If a packet larger than MTU is received, the
+connection will be disconnected.
 
 Example:
 @example
@@ -1291,11 +1293,12 @@  qemu linux.img -net nic,macaddr=52:54:00:12:34:57 \
                -net socket,connect=127.0.0.1:1234
 @end example
 
-@item -net socket[,vlan=@var{n}][,name=@var{name}][,fd=@var{h}][,mcast=@var{maddr}:@var{port}[,localaddr=@var{addr}]]
+@item -net socket[,vlan=@var{n}][,name=@var{name}][,fd=@var{h}][,mcast=@var{maddr}:@var{port}[,localaddr=@var{addr}]][,mtu=@var{mtu}]
 
 Create a VLAN @var{n} shared with another QEMU virtual
 machines using a UDP multicast socket, effectively making a bus for
 every QEMU with same multicast address @var{maddr} and @var{port}.
+MTU is 4096 by default.
 NOTES:
 @enumerate
 @item