From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([140.186.70.92]:49776) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QLhkh-0005pX-RS for qemu-devel@nongnu.org; Sun, 15 May 2011 16:17:25 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1QLhkg-0000fI-8G for qemu-devel@nongnu.org; Sun, 15 May 2011 16:17:23 -0400 Received: from mx1.redhat.com ([209.132.183.28]:21685) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QLhkf-0000fA-NY for qemu-devel@nongnu.org; Sun, 15 May 2011 16:17:22 -0400 Date: Sun, 15 May 2011 23:17:27 +0300 From: "Michael S. Tsirkin" Message-ID: <20110515201727.GB29768@redhat.com> References: <1303477168-4663-1-git-send-email-pclouds@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <1303477168-4663-1-git-send-email-pclouds@gmail.com> Content-Transfer-Encoding: quoted-printable Subject: Re: [Qemu-devel] [PATCH] net/socket: remove hardcoded packet size in favor of new mtu parameter List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: =?utf-8?B?Tmd1eeG7hW4gVGjDoWkgTmfhu41j?= Duy Cc: Stefan Hajnoczi , Anthony Liguori , qemu-devel@nongnu.org, Mark McLoughlin On Fri, Apr 22, 2011 at 07:59:28PM +0700, Nguy=E1=BB=85n Th=C3=A1i Ng=E1=BB= =8Dc Duy wrote: > Also mention the default value 4096. >=20 > Signed-off-by: Nguy=E1=BB=85n Th=C3=A1i Ng=E1=BB=8Dc Duy 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. > --- > free new buffers in net_socket_cleanup() >=20 > net.c | 4 ++++ > net/socket.c | 52 ++++++++++++++++++++++++++++++++++++-----------= ----- > qemu-options.hx | 11 ++++++----- > 3 files changed, 46 insertions(+), 21 deletions(-) >=20 > 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 =3D "localaddr", > .type =3D QEMU_OPT_STRING, > .help =3D "source address for multicast packets", > + }, { > + .name =3D "mtu", > + .type =3D QEMU_OPT_NUMBER, > + .help =3D "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 =3D getting length, 1 =3D 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 desti= nation iff connectionless (SOCK_DGRAM) */ > } NetSocketState; > =20 > @@ -47,6 +48,7 @@ typedef struct NetSocketListenState { > char *model; > char *name; > int fd; > + unsigned int mtu; > } NetSocketListenState; > =20 > /* XXX: we consider we can send the whole packet without blocking */ > @@ -73,10 +75,10 @@ static void net_socket_send(void *opaque) > NetSocketState *s =3D opaque; > int size, err; > unsigned l; > - uint8_t buf1[4096]; > + uint8_t *buf1 =3D s->buf1; > const uint8_t *buf; > =20 > - size =3D recv(s->fd, (void *)buf1, sizeof(buf1), 0); > + size =3D recv(s->fd, (void *)buf1, s->mtu, 0); > if (size < 0) { > err =3D socket_error(); > if (err !=3D EWOULDBLOCK) > @@ -111,7 +113,7 @@ static void net_socket_send(void *opaque) > l =3D s->packet_len - s->index; > if (l > size) > l =3D size; > - if (s->index + l <=3D sizeof(s->buf)) { > + if (s->index + l <=3D s->mtu) { > memcpy(s->buf + s->index, buf, l); > } else { > fprintf(stderr, "serious error: oversized packet recei= ved," > @@ -138,7 +140,7 @@ static void net_socket_send_dgram(void *opaque) > NetSocketState *s =3D opaque; > int size; > =20 > - size =3D recv(s->fd, (void *)s->buf, sizeof(s->buf), 0); > + size =3D recv(s->fd, (void *)s->buf, s->mtu, 0); > if (size < 0) > return; > if (size =3D=3D 0) { > @@ -228,6 +230,8 @@ static void net_socket_cleanup(VLANClientState *nc) > NetSocketState *s =3D 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); > } > =20 > static NetClientInfo net_dgram_socket_info =3D { > @@ -238,6 +242,7 @@ static NetClientInfo net_dgram_socket_info =3D { > }; > =20 > static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan, > + unsigned int mtu, > const char *model, > const char *name, > int fd, int is_connect= ed) > @@ -288,6 +293,10 @@ static NetSocketState *net_socket_fd_init_dgram(VL= ANState *vlan, > =20 > s =3D DO_UPCAST(NetSocketState, nc, nc); > =20 > + s->mtu =3D mtu; > + s->buf =3D qemu_malloc(s->mtu); > + s->buf1 =3D qemu_malloc(s->mtu); > + > s->fd =3D fd; > =20 > qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s); > @@ -312,6 +321,7 @@ static NetClientInfo net_socket_info =3D { > }; > =20 > static NetSocketState *net_socket_fd_init_stream(VLANState *vlan, > + unsigned int mtu, > const char *model, > const char *name, > int fd, int is_connec= ted) > @@ -325,6 +335,10 @@ static NetSocketState *net_socket_fd_init_stream(V= LANState *vlan, > =20 > s =3D DO_UPCAST(NetSocketState, nc, nc); > =20 > + s->mtu =3D mtu; > + s->buf =3D qemu_malloc(s->mtu); > + s->buf1 =3D qemu_malloc(s->mtu); > + > s->fd =3D fd; > =20 > if (is_connected) { > @@ -335,7 +349,7 @@ static NetSocketState *net_socket_fd_init_stream(VL= ANState *vlan, > return s; > } > =20 > -static NetSocketState *net_socket_fd_init(VLANState *vlan, > +static NetSocketState *net_socket_fd_init(VLANState *vlan, unsigned in= t mtu, > const char *model, const cha= r *name, > int fd, int is_connected) > { > @@ -348,13 +362,13 @@ static NetSocketState *net_socket_fd_init(VLANSta= te *vlan, > } > switch(so_type) { > case SOCK_DGRAM: > - return net_socket_fd_init_dgram(vlan, model, name, fd, is_conn= ected); > + 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_con= nected); > + return net_socket_fd_init_stream(vlan, mtu, model, name, fd, i= s_connected); > default: > /* who knows ... this could be a eg. a pty, do warn and contin= ue as stream */ > fprintf(stderr, "qemu: warning: socket type=3D%d for fd=3D%d i= s not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd); > - return net_socket_fd_init_stream(vlan, model, name, fd, is_con= nected); > + return net_socket_fd_init_stream(vlan, mtu, model, name, fd, i= s_connected); > } > return NULL; > } > @@ -376,7 +390,7 @@ static void net_socket_accept(void *opaque) > break; > } > } > - s1 =3D net_socket_fd_init(s->vlan, s->model, s->name, fd, 1); > + s1 =3D 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) > } > =20 > 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 =3D qemu_strdup(model); > s->name =3D name ? qemu_strdup(name) : NULL; > s->fd =3D fd; > + s->mtu =3D mtu; > qemu_set_fd_handler(fd, net_socket_accept, NULL, s); > return 0; > } > =20 > 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 =3D net_socket_fd_init(vlan, model, name, fd, connected); > + s =3D 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, > } > =20 > 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; > =20 > - s =3D net_socket_fd_init(vlan, model, name, fd, 0); > + s =3D net_socket_fd_init(vlan, mtu, model, name, fd, 0); > if (!s) > return -1; > =20 > @@ -523,6 +541,8 @@ int net_init_socket(QemuOpts *opts, > const char *name, > VLANState *vlan) > { > + unsigned int mtu =3D qemu_opt_get_number(opts, "mtu", 4096); > + > if (qemu_opt_get(opts, "fd")) { > int fd; > =20 > @@ -539,7 +559,7 @@ int net_init_socket(QemuOpts *opts, > return -1; > } > =20 > - 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, > =20 > listen =3D qemu_opt_get(opts, "listen"); > =20 > - if (net_socket_listen_init(vlan, "socket", name, listen) =3D=3D= -1) { > + if (net_socket_listen_init(vlan, mtu, "socket", name, listen) = =3D=3D -1) { > return -1; > } > } else if (qemu_opt_get(opts, "connect")) { > @@ -572,7 +592,7 @@ int net_init_socket(QemuOpts *opts, > =20 > connect =3D qemu_opt_get(opts, "connect"); > =20 > - if (net_socket_connect_init(vlan, "socket", name, connect) =3D= =3D -1) { > + if (net_socket_connect_init(vlan, mtu, "socket", name, connect= ) =3D=3D -1) { > return -1; > } > } else if (qemu_opt_get(opts, "mcast")) { > @@ -588,7 +608,7 @@ int net_init_socket(QemuOpts *opts, > mcast =3D qemu_opt_get(opts, "mcast"); > localaddr =3D qemu_opt_get(opts, "localaddr"); > =20 > - if (net_socket_mcast_init(vlan, "socket", name, mcast, localad= dr) =3D=3D -1) { > + if (net_socket_mcast_init(vlan, mtu, "socket", name, mcast, lo= caladdr) =3D=3D -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=3Don to force vhost on for non-MSI= X virtio guests\n" > " use 'vhostfd=3Dh' to connect to an already opened= vhost net device\n" > #endif > - "-net socket[,vlan=3Dn][,name=3Dstr][,fd=3Dh][,listen=3D[host]:por= t][,connect=3Dhost:port]\n" > + "-net socket[,vlan=3Dn][,name=3Dstr][,fd=3Dh][,listen=3D[host]:por= t][,connect=3Dhost:port][,mtu=3Dmtu]\n" > " connect the vlan 'n' to another VLAN using a sock= et connection\n" > - "-net socket[,vlan=3Dn][,name=3Dstr][,fd=3Dh][,mcast=3Dmaddr:port[= ,localaddr=3Daddr]]\n" > + "-net socket[,vlan=3Dn][,name=3Dstr][,fd=3Dh][,mcast=3Dmaddr:port[= ,localaddr=3Daddr]][,mtu=3Dmtu]\n" > " connect the vlan 'n' to multicast maddr and port\= n" > " use 'localaddr=3Daddr' to specify the host addres= s to send packets from\n" > #ifdef CONFIG_VDE > @@ -1273,14 +1273,14 @@ qemu linux.img -net nic,vlan=3D0 -net tap,vlan=3D= 0,ifname=3Dtap0 \ > -net nic,vlan=3D1 -net tap,vlan=3D1,ifname=3Dtap1 > @end example > =20 > -@item -net socket[,vlan=3D@var{n}][,name=3D@var{name}][,fd=3D@var{h}] = [,listen=3D[@var{host}]:@var{port}][,connect=3D@var{host}:@var{port}] > +@item -net socket[,vlan=3D@var{n}][,name=3D@var{name}][,fd=3D@var{h}] = [,listen=3D[@var{host}]:@var{port}][,connect=3D@var{host}:@var{port}][,mt= u=3D@var{mtu}] > =20 > 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}=3D= @var{h} > -specifies an already opened TCP socket. > +specifies an already opened TCP socket. MTU is 4096 by default. > =20 > Example: > @example > @@ -1293,11 +1293,12 @@ qemu linux.img -net nic,macaddr=3D52:54:00:12:3= 4:57 \ > -net socket,connect=3D127.0.0.1:1234 > @end example > =20 > -@item -net socket[,vlan=3D@var{n}][,name=3D@var{name}][,fd=3D@var{h}][= ,mcast=3D@var{maddr}:@var{port}[,localaddr=3D@var{addr}]] > +@item -net socket[,vlan=3D@var{n}][,name=3D@var{name}][,fd=3D@var{h}][= ,mcast=3D@var{maddr}:@var{port}[,localaddr=3D@var{addr}]][,mtu=3D@var{mtu= }] > =20 > 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 > --=20 > 1.7.4.74.g639db >=20