]> asedeno.scripts.mit.edu Git - linux.git/blobdiff - net/vmw_vsock/hyperv_transport.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[linux.git] / net / vmw_vsock / hyperv_transport.c
index c443db7af8d4af073bb6a7b81df2eed36320f80a..3492c021925f4b2163ff5aff46d334ae64bbdc41 100644 (file)
 #include <linux/hyperv.h>
 #include <net/sock.h>
 #include <net/af_vsock.h>
+#include <asm/hyperv-tlfs.h>
 
 /* Older (VMBUS version 'VERSION_WIN10' or before) Windows hosts have some
- * stricter requirements on the hv_sock ring buffer size of six 4K pages. Newer
- * hosts don't have this limitation; but, keep the defaults the same for compat.
+ * stricter requirements on the hv_sock ring buffer size of six 4K pages.
+ * hyperv-tlfs defines HV_HYP_PAGE_SIZE as 4K. Newer hosts don't have this
+ * limitation; but, keep the defaults the same for compat.
  */
-#define PAGE_SIZE_4K           4096
-#define RINGBUFFER_HVS_RCV_SIZE (PAGE_SIZE_4K * 6)
-#define RINGBUFFER_HVS_SND_SIZE (PAGE_SIZE_4K * 6)
-#define RINGBUFFER_HVS_MAX_SIZE (PAGE_SIZE_4K * 64)
+#define RINGBUFFER_HVS_RCV_SIZE (HV_HYP_PAGE_SIZE * 6)
+#define RINGBUFFER_HVS_SND_SIZE (HV_HYP_PAGE_SIZE * 6)
+#define RINGBUFFER_HVS_MAX_SIZE (HV_HYP_PAGE_SIZE * 64)
 
 /* The MTU is 16KB per the host side's design */
 #define HVS_MTU_SIZE           (1024 * 16)
@@ -54,7 +55,8 @@ struct hvs_recv_buf {
  * ringbuffer APIs that allow us to directly copy data from userspace buffer
  * to VMBus ringbuffer.
  */
-#define HVS_SEND_BUF_SIZE (PAGE_SIZE_4K - sizeof(struct vmpipe_proto_header))
+#define HVS_SEND_BUF_SIZE \
+               (HV_HYP_PAGE_SIZE - sizeof(struct vmpipe_proto_header))
 
 struct hvs_send_buf {
        /* The header before the payload data */
@@ -136,33 +138,22 @@ struct hvsock {
  ****************************************************************************
  * The only valid Service GUIDs, from the perspectives of both the host and *
  * Linux VM, that can be connected by the other end, must conform to this   *
- * format: <port>-facb-11e6-bd58-64006a7986d3, and the "port" must be in    *
- * this range [0, 0x7FFFFFFF].                                              *
+ * format: <port>-facb-11e6-bd58-64006a7986d3.                              *
  ****************************************************************************
  *
  * When we write apps on the host to connect(), the GUID ServiceID is used.
  * When we write apps in Linux VM to connect(), we only need to specify the
  * port and the driver will form the GUID and use that to request the host.
  *
- * From the perspective of Linux VM:
- * 1. the local ephemeral port (i.e. the local auto-bound port when we call
- * connect() without explicit bind()) is generated by __vsock_bind_stream(),
- * and the range is [1024, 0xFFFFFFFF).
- * 2. the remote ephemeral port (i.e. the auto-generated remote port for
- * a connect request initiated by the host's connect()) is generated by
- * hvs_remote_addr_init() and the range is [0x80000000, 0xFFFFFFFF).
  */
 
-#define MAX_LISTEN_PORT                        ((u32)0x7FFFFFFF)
-#define MAX_VM_LISTEN_PORT             MAX_LISTEN_PORT
-#define MAX_HOST_LISTEN_PORT           MAX_LISTEN_PORT
-#define MIN_HOST_EPHEMERAL_PORT                (MAX_HOST_LISTEN_PORT + 1)
-
 /* 00000000-facb-11e6-bd58-64006a7986d3 */
 static const guid_t srv_id_template =
        GUID_INIT(0x00000000, 0xfacb, 0x11e6, 0xbd, 0x58,
                  0x64, 0x00, 0x6a, 0x79, 0x86, 0xd3);
 
+static bool hvs_check_transport(struct vsock_sock *vsk);
+
 static bool is_valid_srv_id(const guid_t *id)
 {
        return !memcmp(&id->b[4], &srv_id_template.b[4], sizeof(guid_t) - 4);
@@ -180,33 +171,6 @@ static void hvs_addr_init(struct sockaddr_vm *addr, const guid_t *svr_id)
        vsock_addr_init(addr, VMADDR_CID_ANY, port);
 }
 
-static void hvs_remote_addr_init(struct sockaddr_vm *remote,
-                                struct sockaddr_vm *local)
-{
-       static u32 host_ephemeral_port = MIN_HOST_EPHEMERAL_PORT;
-       struct sock *sk;
-
-       vsock_addr_init(remote, VMADDR_CID_ANY, VMADDR_PORT_ANY);
-
-       while (1) {
-               /* Wrap around ? */
-               if (host_ephemeral_port < MIN_HOST_EPHEMERAL_PORT ||
-                   host_ephemeral_port == VMADDR_PORT_ANY)
-                       host_ephemeral_port = MIN_HOST_EPHEMERAL_PORT;
-
-               remote->svm_port = host_ephemeral_port++;
-
-               sk = vsock_find_connected_socket(remote, local);
-               if (!sk) {
-                       /* Found an available ephemeral port */
-                       return;
-               }
-
-               /* Release refcnt got in vsock_find_connected_socket */
-               sock_put(sk);
-       }
-}
-
 static void hvs_set_channel_pending_send_size(struct vmbus_channel *chan)
 {
        set_channel_pending_send_size(chan,
@@ -336,12 +300,7 @@ static void hvs_open_connection(struct vmbus_channel *chan)
        if_type = &chan->offermsg.offer.if_type;
        if_instance = &chan->offermsg.offer.if_instance;
        conn_from_host = chan->offermsg.offer.u.pipe.user_def[0];
-
-       /* The host or the VM should only listen on a port in
-        * [0, MAX_LISTEN_PORT]
-        */
-       if (!is_valid_srv_id(if_type) ||
-           get_port_by_srv_id(if_type) > MAX_LISTEN_PORT)
+       if (!is_valid_srv_id(if_type))
                return;
 
        hvs_addr_init(&addr, conn_from_host ? if_type : if_instance);
@@ -358,13 +317,27 @@ static void hvs_open_connection(struct vmbus_channel *chan)
                if (sk->sk_ack_backlog >= sk->sk_max_ack_backlog)
                        goto out;
 
-               new = __vsock_create(sock_net(sk), NULL, sk, GFP_KERNEL,
-                                    sk->sk_type, 0);
+               new = vsock_create_connected(sk);
                if (!new)
                        goto out;
 
                new->sk_state = TCP_SYN_SENT;
                vnew = vsock_sk(new);
+
+               hvs_addr_init(&vnew->local_addr, if_type);
+
+               /* Remote peer is always the host */
+               vsock_addr_init(&vnew->remote_addr,
+                               VMADDR_CID_HOST, VMADDR_PORT_ANY);
+               vnew->remote_addr.svm_port = get_port_by_srv_id(if_instance);
+               ret = vsock_assign_transport(vnew, vsock_sk(sk));
+               /* Transport assigned (looking at remote_addr) must be the
+                * same where we received the request.
+                */
+               if (ret || !hvs_check_transport(vnew)) {
+                       sock_put(new);
+                       goto out;
+               }
                hvs_new = vnew->trans;
                hvs_new->chan = chan;
        } else {
@@ -393,10 +366,10 @@ static void hvs_open_connection(struct vmbus_channel *chan)
        } else {
                sndbuf = max_t(int, sk->sk_sndbuf, RINGBUFFER_HVS_SND_SIZE);
                sndbuf = min_t(int, sndbuf, RINGBUFFER_HVS_MAX_SIZE);
-               sndbuf = ALIGN(sndbuf, PAGE_SIZE);
+               sndbuf = ALIGN(sndbuf, HV_HYP_PAGE_SIZE);
                rcvbuf = max_t(int, sk->sk_rcvbuf, RINGBUFFER_HVS_RCV_SIZE);
                rcvbuf = min_t(int, rcvbuf, RINGBUFFER_HVS_MAX_SIZE);
-               rcvbuf = ALIGN(rcvbuf, PAGE_SIZE);
+               rcvbuf = ALIGN(rcvbuf, HV_HYP_PAGE_SIZE);
        }
 
        ret = vmbus_open(chan, sndbuf, rcvbuf, NULL, 0, hvs_channel_cb,
@@ -426,10 +399,7 @@ static void hvs_open_connection(struct vmbus_channel *chan)
 
        if (conn_from_host) {
                new->sk_state = TCP_ESTABLISHED;
-               sk->sk_ack_backlog++;
-
-               hvs_addr_init(&vnew->local_addr, if_type);
-               hvs_remote_addr_init(&vnew->remote_addr, &vnew->local_addr);
+               sk_acceptq_added(sk);
 
                hvs_new->vm_srv_id = *if_type;
                hvs_new->host_srv_id = *if_instance;
@@ -670,7 +640,7 @@ static ssize_t hvs_stream_enqueue(struct vsock_sock *vsk, struct msghdr *msg,
        ssize_t ret = 0;
        ssize_t bytes_written = 0;
 
-       BUILD_BUG_ON(sizeof(*send_buf) != PAGE_SIZE_4K);
+       BUILD_BUG_ON(sizeof(*send_buf) != HV_HYP_PAGE_SIZE);
 
        send_buf = kmalloc(sizeof(*send_buf), GFP_KERNEL);
        if (!send_buf)
@@ -753,16 +723,6 @@ static bool hvs_stream_is_active(struct vsock_sock *vsk)
 
 static bool hvs_stream_allow(u32 cid, u32 port)
 {
-       /* The host's port range [MIN_HOST_EPHEMERAL_PORT, 0xFFFFFFFF) is
-        * reserved as ephemeral ports, which are used as the host's ports
-        * when the host initiates connections.
-        *
-        * Perform this check in the guest so an immediate error is produced
-        * instead of a timeout.
-        */
-       if (port > MAX_HOST_LISTEN_PORT)
-               return false;
-
        if (cid == VMADDR_CID_HOST)
                return true;
 
@@ -843,37 +803,9 @@ int hvs_notify_send_post_enqueue(struct vsock_sock *vsk, ssize_t written,
        return 0;
 }
 
-static void hvs_set_buffer_size(struct vsock_sock *vsk, u64 val)
-{
-       /* Ignored. */
-}
-
-static void hvs_set_min_buffer_size(struct vsock_sock *vsk, u64 val)
-{
-       /* Ignored. */
-}
-
-static void hvs_set_max_buffer_size(struct vsock_sock *vsk, u64 val)
-{
-       /* Ignored. */
-}
-
-static u64 hvs_get_buffer_size(struct vsock_sock *vsk)
-{
-       return -ENOPROTOOPT;
-}
-
-static u64 hvs_get_min_buffer_size(struct vsock_sock *vsk)
-{
-       return -ENOPROTOOPT;
-}
-
-static u64 hvs_get_max_buffer_size(struct vsock_sock *vsk)
-{
-       return -ENOPROTOOPT;
-}
-
 static struct vsock_transport hvs_transport = {
+       .module                   = THIS_MODULE,
+
        .get_local_cid            = hvs_get_local_cid,
 
        .init                     = hvs_sock_init,
@@ -906,14 +838,13 @@ static struct vsock_transport hvs_transport = {
        .notify_send_pre_enqueue  = hvs_notify_send_pre_enqueue,
        .notify_send_post_enqueue = hvs_notify_send_post_enqueue,
 
-       .set_buffer_size          = hvs_set_buffer_size,
-       .set_min_buffer_size      = hvs_set_min_buffer_size,
-       .set_max_buffer_size      = hvs_set_max_buffer_size,
-       .get_buffer_size          = hvs_get_buffer_size,
-       .get_min_buffer_size      = hvs_get_min_buffer_size,
-       .get_max_buffer_size      = hvs_get_max_buffer_size,
 };
 
+static bool hvs_check_transport(struct vsock_sock *vsk)
+{
+       return vsk->transport == &hvs_transport;
+}
+
 static int hvs_probe(struct hv_device *hdev,
                     const struct hv_vmbus_device_id *dev_id)
 {
@@ -938,6 +869,24 @@ static int hvs_remove(struct hv_device *hdev)
        return 0;
 }
 
+/* hv_sock connections can not persist across hibernation, and all the hv_sock
+ * channels are forced to be rescinded before hibernation: see
+ * vmbus_bus_suspend(). Here the dummy hvs_suspend() and hvs_resume()
+ * are only needed because hibernation requires that every vmbus device's
+ * driver should have a .suspend and .resume callback: see vmbus_suspend().
+ */
+static int hvs_suspend(struct hv_device *hv_dev)
+{
+       /* Dummy */
+       return 0;
+}
+
+static int hvs_resume(struct hv_device *dev)
+{
+       /* Dummy */
+       return 0;
+}
+
 /* This isn't really used. See vmbus_match() and vmbus_probe() */
 static const struct hv_vmbus_device_id id_table[] = {
        {},
@@ -949,6 +898,8 @@ static struct hv_driver hvs_drv = {
        .id_table       = id_table,
        .probe          = hvs_probe,
        .remove         = hvs_remove,
+       .suspend        = hvs_suspend,
+       .resume         = hvs_resume,
 };
 
 static int __init hvs_init(void)
@@ -962,7 +913,7 @@ static int __init hvs_init(void)
        if (ret != 0)
                return ret;
 
-       ret = vsock_core_init(&hvs_transport);
+       ret = vsock_core_register(&hvs_transport, VSOCK_TRANSPORT_F_G2H);
        if (ret) {
                vmbus_driver_unregister(&hvs_drv);
                return ret;
@@ -973,7 +924,7 @@ static int __init hvs_init(void)
 
 static void __exit hvs_exit(void)
 {
-       vsock_core_exit();
+       vsock_core_unregister(&hvs_transport);
        vmbus_driver_unregister(&hvs_drv);
 }