qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] [PATCH] net/socket: remove hardcoded packet size in favor o


From: Nguyễn Thái Ngọc Duy
Subject: [Qemu-devel] [PATCH] net/socket: remove hardcoded packet size in favor of new mtu parameter
Date: Fri, 22 Apr 2011 19:59:28 +0700

Also mention the default value 4096.

Signed-off-by: Nguyễn Thái Ngọc Duy <address@hidden>
---
 free new buffers in net_socket_cleanup()

 net.c           |    4 ++++
 net/socket.c    |   52 ++++++++++++++++++++++++++++++++++++----------------
 qemu-options.hx |   11 ++++++-----
 3 files changed, 46 insertions(+), 21 deletions(-)

diff --git a/net.c b/net.c
index 8d6a555..6746bc7 100644
--- a/net.c
+++ b/net.c
@@ -1001,6 +1001,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..e932064 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) {
@@ -228,6 +230,8 @@ static void net_socket_cleanup(VLANClientState *nc)
     NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc);
     qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
     close(s->fd);
+    qemu_free(s->buf);
+    qemu_free(s->buf1);
 }
 
 static NetClientInfo net_dgram_socket_info = {
@@ -238,6 +242,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 +293,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 +321,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 +335,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 +349,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 +362,13 @@ 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 +390,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 +401,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 +440,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 +487,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 +497,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 +523,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 +541,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 +559,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 +576,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 +592,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 +608,7 @@ 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 66fffe3..4d5af96 100644
--- a/qemu-options.hx
+++ b/qemu-options.hx
@@ -1095,9 +1095,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
@@ -1273,14 +1273,14 @@ 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
 
address@hidden -net socket[,address@hidden,address@hidden,address@hidden 
[,address@hidden:@var{port}][,address@hidden:@var{port}]
address@hidden -net socket[,address@hidden,address@hidden,address@hidden 
[,address@hidden:@var{port}][,address@hidden:@var{port}][,address@hidden
 
 Connect the VLAN @var{n} to a remote VLAN in another QEMU virtual
 machine using a TCP socket connection. If @option{listen} is
 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. @address@hidden
-specifies an already opened TCP socket.
+specifies an already opened TCP socket. MTU is 4096 by default.
 
 Example:
 @example
@@ -1293,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
 
address@hidden -net 
socket[,address@hidden,address@hidden,address@hidden,address@hidden:@var{port}[,address@hidden
address@hidden -net 
socket[,address@hidden,address@hidden,address@hidden,address@hidden:@var{port}[,address@hidden,address@hidden
 
 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
-- 
1.7.4.74.g639db




reply via email to

[Prev in Thread] Current Thread [Next in Thread]