Re: [RFC 2/5] VSOCK: Introduce virtio-vsock-common.ko

2013-06-28 Thread Asias He
On Thu, Jun 27, 2013 at 01:34:30PM +0300, Michael S. Tsirkin wrote:
 On Thu, Jun 27, 2013 at 04:00:01PM +0800, Asias He wrote:
  This module contains the common code and header files for the following
  virtio-vsock and virtio-vhost kernel modules.
  
  Signed-off-by: Asias He as...@redhat.com
  ---
   include/linux/virtio_vsock.h| 200 +++
   include/uapi/linux/virtio_ids.h |   1 +
   include/uapi/linux/virtio_vsock.h   |  70 +++
   net/vmw_vsock/virtio_transport_common.c | 992 
  
   4 files changed, 1263 insertions(+)
   create mode 100644 include/linux/virtio_vsock.h
   create mode 100644 include/uapi/linux/virtio_vsock.h
   create mode 100644 net/vmw_vsock/virtio_transport_common.c
  
  diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h
  new file mode 100644
  index 000..cd8ed95
  --- /dev/null
  +++ b/include/linux/virtio_vsock.h
  @@ -0,0 +1,200 @@
  +/*
  + * This header, excluding the #ifdef __KERNEL__ part, is BSD licensed so
  + * anyone can use the definitions to implement compatible drivers/servers:
  + *
  + *
  + * Redistribution and use in source and binary forms, with or without
  + * modification, are permitted provided that the following conditions
  + * are met:
  + * 1. Redistributions of source code must retain the above copyright
  + *notice, this list of conditions and the following disclaimer.
  + * 2. Redistributions in binary form must reproduce the above copyright
  + *notice, this list of conditions and the following disclaimer in the
  + *documentation and/or other materials provided with the distribution.
  + * 3. Neither the name of IBM nor the names of its contributors
  + *may be used to endorse or promote products derived from this software
  + *without specific prior written permission.
  + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
  ``AS IS''
  + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
  THE
  + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
  PURPOSE
  + * ARE DISCLAIMED.  IN NO EVENT SHALL IBM OR CONTRIBUTORS BE LIABLE
  + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
  CONSEQUENTIAL
  + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
  STRICT
  + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY 
  WAY
  + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  + * SUCH DAMAGE.
  + *
  + * Copyright (C) Red Hat, Inc., 2013
  + * Copyright (C) Asias He as...@redhat.com, 2013
  + */
  +
  +#ifndef _LINUX_VIRTIO_VSOCK_H
  +#define _LINUX_VIRTIO_VSOCK_H
  +
  +#include uapi/linux/virtio_vsock.h
  +#include linux/socket.h
  +#include net/sock.h
  +
  +#define VIRTIO_VSOCK_DEFAULT_MIN_BUF_SIZE  128
  +#define VIRTIO_VSOCK_DEFAULT_BUF_SIZE  (1024 * 256)
  +#define VIRTIO_VSOCK_DEFAULT_MAX_BUF_SIZE  (1024 * 256)
  +#define VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE   (1024 * 4)
  +#define VIRTIO_VSOCK_MAX_PKT_BUF_SIZE  (1024 * 64)
  +
  +struct vsock_transport_recv_notify_data;
  +struct vsock_transport_send_notify_data;
  +struct sockaddr_vm;
  +struct vsock_sock;
  +
  +enum {
  +   VSOCK_VQ_CTRL   = 0,
  +   VSOCK_VQ_RX = 1, /* for host to guest data */
  +   VSOCK_VQ_TX = 2, /* for guest to host data */
  +   VSOCK_VQ_MAX= 3,
  +};
  +
  +/* virtio transport socket state */
  +struct virtio_transport {
  +   struct virtio_transport_pkt_ops *ops;
  +   struct vsock_sock *vsk;
  +
  +   u64 buf_size;
  +   u64 buf_size_min;
  +   u64 buf_size_max;
  +
  +   struct mutex tx_lock;
  +   struct mutex rx_lock;
  +
  +   struct list_head rx_queue;
  +   u64 rx_bytes;
  +
  +   /* Protected by trans-tx_lock */
  +   u64 tx_cnt;
  +   u64 buf_alloc;
  +   u64 peer_fwd_cnt;
  +   u64 peer_buf_alloc;
  +   /* Protected by trans-rx_lock */
  +   u64 fwd_cnt;
  +};
  +
  +struct virtio_vsock_pkt {
  +   struct virtio_vsock_hdr hdr;
  +   struct virtio_transport *trans;
  +   struct work_struct work;
  +   struct list_head list;
  +   void *buf;
  +   u32 len;
  +   u32 off;
  +};
  +
  +struct virtio_vsock_pkt_info {
  +   struct sockaddr_vm *src;
  +   struct sockaddr_vm *dst;
  +   struct iovec *iov;
  +   u32 len;
  +   u8 type;
  +   u8 op;
  +   u8 shut;
  +};
  +
  +struct virtio_transport_pkt_ops {
  +   int (*send_pkt)(struct vsock_sock *vsk,
  +   struct virtio_vsock_pkt_info *info);
  +};
  +
  +void virtio_vsock_dumppkt(const char *func,
  + const struct virtio_vsock_pkt *pkt);
  +
  +struct sock *
  +virtio_transport_get_pending(struct sock *listener,
  +struct virtio_vsock_pkt *pkt);
  +struct virtio_vsock_pkt *
  +virtio_transport_alloc_pkt(struct vsock_sock *vsk,
  

Re: [RFC 2/5] VSOCK: Introduce virtio-vsock-common.ko

2013-06-28 Thread David Miller
From: Asias He as...@redhat.com
Date: Thu, 27 Jun 2013 16:00:01 +0800

 +static void
 +virtio_transport_recv_dgram(struct sock *sk,
 + struct virtio_vsock_pkt *pkt)
 ...
 + memcpy(skb-data, pkt, sizeof(*pkt));
 + memcpy(skb-data + sizeof(*pkt), pkt-buf, pkt-len);

Are you sure this is right?

Shouldn't you be using sizeof(struct virtio_vsock_hdr) instead of
sizeof(*pkt).  'pkt' is struct virtio_vsock_pkt and has all kinds
of meta-data you probably don't mean to include in the SKB.
--
To unsubscribe from this list: send the line unsubscribe kvm in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [RFC 2/5] VSOCK: Introduce virtio-vsock-common.ko

2013-06-27 Thread Michael S. Tsirkin
On Thu, Jun 27, 2013 at 04:00:01PM +0800, Asias He wrote:
 This module contains the common code and header files for the following
 virtio-vsock and virtio-vhost kernel modules.
 
 Signed-off-by: Asias He as...@redhat.com
 ---
  include/linux/virtio_vsock.h| 200 +++
  include/uapi/linux/virtio_ids.h |   1 +
  include/uapi/linux/virtio_vsock.h   |  70 +++
  net/vmw_vsock/virtio_transport_common.c | 992 
 
  4 files changed, 1263 insertions(+)
  create mode 100644 include/linux/virtio_vsock.h
  create mode 100644 include/uapi/linux/virtio_vsock.h
  create mode 100644 net/vmw_vsock/virtio_transport_common.c
 
 diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h
 new file mode 100644
 index 000..cd8ed95
 --- /dev/null
 +++ b/include/linux/virtio_vsock.h
 @@ -0,0 +1,200 @@
 +/*
 + * This header, excluding the #ifdef __KERNEL__ part, is BSD licensed so
 + * anyone can use the definitions to implement compatible drivers/servers:
 + *
 + *
 + * Redistribution and use in source and binary forms, with or without
 + * modification, are permitted provided that the following conditions
 + * are met:
 + * 1. Redistributions of source code must retain the above copyright
 + *notice, this list of conditions and the following disclaimer.
 + * 2. Redistributions in binary form must reproduce the above copyright
 + *notice, this list of conditions and the following disclaimer in the
 + *documentation and/or other materials provided with the distribution.
 + * 3. Neither the name of IBM nor the names of its contributors
 + *may be used to endorse or promote products derived from this software
 + *without specific prior written permission.
 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS 
 IS''
 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 + * ARE DISCLAIMED.  IN NO EVENT SHALL IBM OR CONTRIBUTORS BE LIABLE
 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 + * SUCH DAMAGE.
 + *
 + * Copyright (C) Red Hat, Inc., 2013
 + * Copyright (C) Asias He as...@redhat.com, 2013
 + */
 +
 +#ifndef _LINUX_VIRTIO_VSOCK_H
 +#define _LINUX_VIRTIO_VSOCK_H
 +
 +#include uapi/linux/virtio_vsock.h
 +#include linux/socket.h
 +#include net/sock.h
 +
 +#define VIRTIO_VSOCK_DEFAULT_MIN_BUF_SIZE128
 +#define VIRTIO_VSOCK_DEFAULT_BUF_SIZE(1024 * 256)
 +#define VIRTIO_VSOCK_DEFAULT_MAX_BUF_SIZE(1024 * 256)
 +#define VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE (1024 * 4)
 +#define VIRTIO_VSOCK_MAX_PKT_BUF_SIZE(1024 * 64)
 +
 +struct vsock_transport_recv_notify_data;
 +struct vsock_transport_send_notify_data;
 +struct sockaddr_vm;
 +struct vsock_sock;
 +
 +enum {
 + VSOCK_VQ_CTRL   = 0,
 + VSOCK_VQ_RX = 1, /* for host to guest data */
 + VSOCK_VQ_TX = 2, /* for guest to host data */
 + VSOCK_VQ_MAX= 3,
 +};
 +
 +/* virtio transport socket state */
 +struct virtio_transport {
 + struct virtio_transport_pkt_ops *ops;
 + struct vsock_sock *vsk;
 +
 + u64 buf_size;
 + u64 buf_size_min;
 + u64 buf_size_max;
 +
 + struct mutex tx_lock;
 + struct mutex rx_lock;
 +
 + struct list_head rx_queue;
 + u64 rx_bytes;
 +
 + /* Protected by trans-tx_lock */
 + u64 tx_cnt;
 + u64 buf_alloc;
 + u64 peer_fwd_cnt;
 + u64 peer_buf_alloc;
 + /* Protected by trans-rx_lock */
 + u64 fwd_cnt;
 +};
 +
 +struct virtio_vsock_pkt {
 + struct virtio_vsock_hdr hdr;
 + struct virtio_transport *trans;
 + struct work_struct work;
 + struct list_head list;
 + void *buf;
 + u32 len;
 + u32 off;
 +};
 +
 +struct virtio_vsock_pkt_info {
 + struct sockaddr_vm *src;
 + struct sockaddr_vm *dst;
 + struct iovec *iov;
 + u32 len;
 + u8 type;
 + u8 op;
 + u8 shut;
 +};
 +
 +struct virtio_transport_pkt_ops {
 + int (*send_pkt)(struct vsock_sock *vsk,
 + struct virtio_vsock_pkt_info *info);
 +};
 +
 +void virtio_vsock_dumppkt(const char *func,
 +   const struct virtio_vsock_pkt *pkt);
 +
 +struct sock *
 +virtio_transport_get_pending(struct sock *listener,
 +  struct virtio_vsock_pkt *pkt);
 +struct virtio_vsock_pkt *
 +virtio_transport_alloc_pkt(struct vsock_sock *vsk,
 +struct virtio_vsock_pkt_info *info,
 +size_t len,
 +u32