Skip to contentSkip to conversation

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

PATCH In review Nguyễn Thái Ngọc Duy 3 participants 6 messages Apr 22, 2011 – May 16, 2011

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

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

Also mention the default value 4096.

---
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(-)
net.c +4 -0
index 4f777c3..74dffa3 100644
@@ -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 */ }
         },
net/socket.c +38 -16

197 lines not on this page · raw message

qemu-options.hx +7 -4
index 677c550..abf6045 100644
@@ -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
Signature · 1 line
1.7.3.1
#1 lore raw

2011/4/22 Nguyễn Thái Ngọc Duy <[email protected]>:

net/socket.c static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan, · line 296

Quoted 7 lines
net/socket.c +3 -0
@@ -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

#2 lore raw

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

Also mention the default value 4096.

---
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(-)
net.c +4 -0
index 8d6a555..6746bc7 100644
@@ -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 */ }
         },
net/socket.c +36 -16

201 lines not on this page · raw message

qemu-options.hx +6 -5
index 66fffe3..4d5af96 100644
@@ -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
 
-@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
 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.
+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
 
-@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
Signature · 1 line
1.7.4.74.g639db
Reviewed-by on v1 1/1
#3 lore raw

2011/4/22 Nguyễn Thái Ngọc Duy <[email protected]>:

Quoted 10 lines

Also mention the default value 4096.

---
 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(-)
#4 lore raw
In reply to Nguyễn Thái Ngọc Duy
Quoted 3 lines · Nguyễn Thái Ngọc Duy

Also mention the default value 4096.

IS the option really necessary? Why not just allocate 128K or so and be done with it? That should be big enough even with GSO etc.

Quoted 301 lines

---
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(-)
net.c +4 -0
index 8d6a555..6746bc7 100644
@@ -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 */ }
         },
net/socket.c +36 -16
index 7337f4f..e932064 100644
@@ -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 {
qemu-options.hx +6 -5
index 66fffe3..4d5af96 100644
@@ -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
 
-@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
 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.
+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
 
-@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
Signature · 1 line
1.7.4.74.g639db
#5 lore raw

2011/5/16 Michael S. Tsirkin <[email protected]>:

Quoted 7 lines · Nguyễn Thái Ngọc Duy
Quoted 3 lines

Also mention the default value 4096.

IS the option really necessary? Why not just allocate 128K or so and be done with it? That should be big enough even with GSO etc.

If it's big enough for practically everything, then yes, that way would be simpler.

Signature · 1 line
Duy