1ae0078fcSDexuan Cui /* 2ae0078fcSDexuan Cui * Hyper-V transport for vsock 3ae0078fcSDexuan Cui * 4ae0078fcSDexuan Cui * Hyper-V Sockets supplies a byte-stream based communication mechanism 5ae0078fcSDexuan Cui * between the host and the VM. This driver implements the necessary 6ae0078fcSDexuan Cui * support in the VM by introducing the new vsock transport. 7ae0078fcSDexuan Cui * 8ae0078fcSDexuan Cui * Copyright (c) 2017, Microsoft Corporation. 9ae0078fcSDexuan Cui * 10ae0078fcSDexuan Cui * This program is free software; you can redistribute it and/or modify it 11ae0078fcSDexuan Cui * under the terms and conditions of the GNU General Public License, 12ae0078fcSDexuan Cui * version 2, as published by the Free Software Foundation. 13ae0078fcSDexuan Cui * 14ae0078fcSDexuan Cui * This program is distributed in the hope it will be useful, but WITHOUT 15ae0078fcSDexuan Cui * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 16ae0078fcSDexuan Cui * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 17ae0078fcSDexuan Cui * more details. 18ae0078fcSDexuan Cui * 19ae0078fcSDexuan Cui */ 20ae0078fcSDexuan Cui #include <linux/module.h> 21ae0078fcSDexuan Cui #include <linux/vmalloc.h> 22ae0078fcSDexuan Cui #include <linux/hyperv.h> 23ae0078fcSDexuan Cui #include <net/sock.h> 24ae0078fcSDexuan Cui #include <net/af_vsock.h> 25ae0078fcSDexuan Cui 26ac383f58SSunil Muthuswamy /* Older (VMBUS version 'VERSION_WIN10' or before) Windows hosts have some 27ac383f58SSunil Muthuswamy * stricter requirements on the hv_sock ring buffer size of six 4K pages. Newer 28ac383f58SSunil Muthuswamy * hosts don't have this limitation; but, keep the defaults the same for compat. 29ae0078fcSDexuan Cui */ 30ae0078fcSDexuan Cui #define PAGE_SIZE_4K 4096 31ae0078fcSDexuan Cui #define RINGBUFFER_HVS_RCV_SIZE (PAGE_SIZE_4K * 6) 32ae0078fcSDexuan Cui #define RINGBUFFER_HVS_SND_SIZE (PAGE_SIZE_4K * 6) 33ac383f58SSunil Muthuswamy #define RINGBUFFER_HVS_MAX_SIZE (PAGE_SIZE_4K * 64) 34ae0078fcSDexuan Cui 35ae0078fcSDexuan Cui /* The MTU is 16KB per the host side's design */ 36ae0078fcSDexuan Cui #define HVS_MTU_SIZE (1024 * 16) 37ae0078fcSDexuan Cui 38a9eeb998SSunil Muthuswamy /* How long to wait for graceful shutdown of a connection */ 39a9eeb998SSunil Muthuswamy #define HVS_CLOSE_TIMEOUT (8 * HZ) 40a9eeb998SSunil Muthuswamy 41ae0078fcSDexuan Cui struct vmpipe_proto_header { 42ae0078fcSDexuan Cui u32 pkt_type; 43ae0078fcSDexuan Cui u32 data_size; 44ae0078fcSDexuan Cui }; 45ae0078fcSDexuan Cui 46ae0078fcSDexuan Cui /* For recv, we use the VMBus in-place packet iterator APIs to directly copy 47ae0078fcSDexuan Cui * data from the ringbuffer into the userspace buffer. 48ae0078fcSDexuan Cui */ 49ae0078fcSDexuan Cui struct hvs_recv_buf { 50ae0078fcSDexuan Cui /* The header before the payload data */ 51ae0078fcSDexuan Cui struct vmpipe_proto_header hdr; 52ae0078fcSDexuan Cui 53ae0078fcSDexuan Cui /* The payload */ 54ae0078fcSDexuan Cui u8 data[HVS_MTU_SIZE]; 55ae0078fcSDexuan Cui }; 56ae0078fcSDexuan Cui 57ae0078fcSDexuan Cui /* We can send up to HVS_MTU_SIZE bytes of payload to the host, but let's use 5814a1eaa8SSunil Muthuswamy * a smaller size, i.e. HVS_SEND_BUF_SIZE, to maximize concurrency between the 5914a1eaa8SSunil Muthuswamy * guest and the host processing as one VMBUS packet is the smallest processing 6014a1eaa8SSunil Muthuswamy * unit. 61ae0078fcSDexuan Cui * 62ae0078fcSDexuan Cui * Note: the buffer can be eliminated in the future when we add new VMBus 63ae0078fcSDexuan Cui * ringbuffer APIs that allow us to directly copy data from userspace buffer 64ae0078fcSDexuan Cui * to VMBus ringbuffer. 65ae0078fcSDexuan Cui */ 66ae0078fcSDexuan Cui #define HVS_SEND_BUF_SIZE (PAGE_SIZE_4K - sizeof(struct vmpipe_proto_header)) 67ae0078fcSDexuan Cui 68ae0078fcSDexuan Cui struct hvs_send_buf { 69ae0078fcSDexuan Cui /* The header before the payload data */ 70ae0078fcSDexuan Cui struct vmpipe_proto_header hdr; 71ae0078fcSDexuan Cui 72ae0078fcSDexuan Cui /* The payload */ 73ae0078fcSDexuan Cui u8 data[HVS_SEND_BUF_SIZE]; 74ae0078fcSDexuan Cui }; 75ae0078fcSDexuan Cui 76ae0078fcSDexuan Cui #define HVS_HEADER_LEN (sizeof(struct vmpacket_descriptor) + \ 77ae0078fcSDexuan Cui sizeof(struct vmpipe_proto_header)) 78ae0078fcSDexuan Cui 79ae0078fcSDexuan Cui /* See 'prev_indices' in hv_ringbuffer_read(), hv_ringbuffer_write(), and 80ae0078fcSDexuan Cui * __hv_pkt_iter_next(). 81ae0078fcSDexuan Cui */ 82ae0078fcSDexuan Cui #define VMBUS_PKT_TRAILER_SIZE (sizeof(u64)) 83ae0078fcSDexuan Cui 84ae0078fcSDexuan Cui #define HVS_PKT_LEN(payload_len) (HVS_HEADER_LEN + \ 85ae0078fcSDexuan Cui ALIGN((payload_len), 8) + \ 86ae0078fcSDexuan Cui VMBUS_PKT_TRAILER_SIZE) 87ae0078fcSDexuan Cui 88ae0078fcSDexuan Cui union hvs_service_id { 89ae0078fcSDexuan Cui uuid_le srv_id; 90ae0078fcSDexuan Cui 91ae0078fcSDexuan Cui struct { 92ae0078fcSDexuan Cui unsigned int svm_port; 93ae0078fcSDexuan Cui unsigned char b[sizeof(uuid_le) - sizeof(unsigned int)]; 94ae0078fcSDexuan Cui }; 95ae0078fcSDexuan Cui }; 96ae0078fcSDexuan Cui 97ae0078fcSDexuan Cui /* Per-socket state (accessed via vsk->trans) */ 98ae0078fcSDexuan Cui struct hvsock { 99ae0078fcSDexuan Cui struct vsock_sock *vsk; 100ae0078fcSDexuan Cui 101ae0078fcSDexuan Cui uuid_le vm_srv_id; 102ae0078fcSDexuan Cui uuid_le host_srv_id; 103ae0078fcSDexuan Cui 104ae0078fcSDexuan Cui struct vmbus_channel *chan; 105ae0078fcSDexuan Cui struct vmpacket_descriptor *recv_desc; 106ae0078fcSDexuan Cui 107ae0078fcSDexuan Cui /* The length of the payload not delivered to userland yet */ 108ae0078fcSDexuan Cui u32 recv_data_len; 109ae0078fcSDexuan Cui /* The offset of the payload */ 110ae0078fcSDexuan Cui u32 recv_data_off; 111ae0078fcSDexuan Cui 112ae0078fcSDexuan Cui /* Have we sent the zero-length packet (FIN)? */ 113ae0078fcSDexuan Cui bool fin_sent; 114ae0078fcSDexuan Cui }; 115ae0078fcSDexuan Cui 116ae0078fcSDexuan Cui /* In the VM, we support Hyper-V Sockets with AF_VSOCK, and the endpoint is 117ae0078fcSDexuan Cui * <cid, port> (see struct sockaddr_vm). Note: cid is not really used here: 118ae0078fcSDexuan Cui * when we write apps to connect to the host, we can only use VMADDR_CID_ANY 119ae0078fcSDexuan Cui * or VMADDR_CID_HOST (both are equivalent) as the remote cid, and when we 120ae0078fcSDexuan Cui * write apps to bind() & listen() in the VM, we can only use VMADDR_CID_ANY 121ae0078fcSDexuan Cui * as the local cid. 122ae0078fcSDexuan Cui * 123ae0078fcSDexuan Cui * On the host, Hyper-V Sockets are supported by Winsock AF_HYPERV: 124ae0078fcSDexuan Cui * https://docs.microsoft.com/en-us/virtualization/hyper-v-on-windows/user- 125ae0078fcSDexuan Cui * guide/make-integration-service, and the endpoint is <VmID, ServiceId> with 126ae0078fcSDexuan Cui * the below sockaddr: 127ae0078fcSDexuan Cui * 128ae0078fcSDexuan Cui * struct SOCKADDR_HV 129ae0078fcSDexuan Cui * { 130ae0078fcSDexuan Cui * ADDRESS_FAMILY Family; 131ae0078fcSDexuan Cui * USHORT Reserved; 132ae0078fcSDexuan Cui * GUID VmId; 133ae0078fcSDexuan Cui * GUID ServiceId; 134ae0078fcSDexuan Cui * }; 135ae0078fcSDexuan Cui * Note: VmID is not used by Linux VM and actually it isn't transmitted via 136ae0078fcSDexuan Cui * VMBus, because here it's obvious the host and the VM can easily identify 137ae0078fcSDexuan Cui * each other. Though the VmID is useful on the host, especially in the case 138ae0078fcSDexuan Cui * of Windows container, Linux VM doesn't need it at all. 139ae0078fcSDexuan Cui * 140ae0078fcSDexuan Cui * To make use of the AF_VSOCK infrastructure in Linux VM, we have to limit 141ae0078fcSDexuan Cui * the available GUID space of SOCKADDR_HV so that we can create a mapping 142ae0078fcSDexuan Cui * between AF_VSOCK port and SOCKADDR_HV Service GUID. The rule of writing 143ae0078fcSDexuan Cui * Hyper-V Sockets apps on the host and in Linux VM is: 144ae0078fcSDexuan Cui * 145ae0078fcSDexuan Cui **************************************************************************** 146ae0078fcSDexuan Cui * The only valid Service GUIDs, from the perspectives of both the host and * 147ae0078fcSDexuan Cui * Linux VM, that can be connected by the other end, must conform to this * 148ae0078fcSDexuan Cui * format: <port>-facb-11e6-bd58-64006a7986d3, and the "port" must be in * 149ae0078fcSDexuan Cui * this range [0, 0x7FFFFFFF]. * 150ae0078fcSDexuan Cui **************************************************************************** 151ae0078fcSDexuan Cui * 152ae0078fcSDexuan Cui * When we write apps on the host to connect(), the GUID ServiceID is used. 153ae0078fcSDexuan Cui * When we write apps in Linux VM to connect(), we only need to specify the 154ae0078fcSDexuan Cui * port and the driver will form the GUID and use that to request the host. 155ae0078fcSDexuan Cui * 156ae0078fcSDexuan Cui * From the perspective of Linux VM: 157ae0078fcSDexuan Cui * 1. the local ephemeral port (i.e. the local auto-bound port when we call 158ae0078fcSDexuan Cui * connect() without explicit bind()) is generated by __vsock_bind_stream(), 159ae0078fcSDexuan Cui * and the range is [1024, 0xFFFFFFFF). 160ae0078fcSDexuan Cui * 2. the remote ephemeral port (i.e. the auto-generated remote port for 161ae0078fcSDexuan Cui * a connect request initiated by the host's connect()) is generated by 162ae0078fcSDexuan Cui * hvs_remote_addr_init() and the range is [0x80000000, 0xFFFFFFFF). 163ae0078fcSDexuan Cui */ 164ae0078fcSDexuan Cui 165ae0078fcSDexuan Cui #define MAX_LISTEN_PORT ((u32)0x7FFFFFFF) 166ae0078fcSDexuan Cui #define MAX_VM_LISTEN_PORT MAX_LISTEN_PORT 167ae0078fcSDexuan Cui #define MAX_HOST_LISTEN_PORT MAX_LISTEN_PORT 168ae0078fcSDexuan Cui #define MIN_HOST_EPHEMERAL_PORT (MAX_HOST_LISTEN_PORT + 1) 169ae0078fcSDexuan Cui 170ae0078fcSDexuan Cui /* 00000000-facb-11e6-bd58-64006a7986d3 */ 171ae0078fcSDexuan Cui static const uuid_le srv_id_template = 172ae0078fcSDexuan Cui UUID_LE(0x00000000, 0xfacb, 0x11e6, 0xbd, 0x58, 173ae0078fcSDexuan Cui 0x64, 0x00, 0x6a, 0x79, 0x86, 0xd3); 174ae0078fcSDexuan Cui 175ae0078fcSDexuan Cui static bool is_valid_srv_id(const uuid_le *id) 176ae0078fcSDexuan Cui { 177ae0078fcSDexuan Cui return !memcmp(&id->b[4], &srv_id_template.b[4], sizeof(uuid_le) - 4); 178ae0078fcSDexuan Cui } 179ae0078fcSDexuan Cui 180ae0078fcSDexuan Cui static unsigned int get_port_by_srv_id(const uuid_le *svr_id) 181ae0078fcSDexuan Cui { 182ae0078fcSDexuan Cui return *((unsigned int *)svr_id); 183ae0078fcSDexuan Cui } 184ae0078fcSDexuan Cui 185ae0078fcSDexuan Cui static void hvs_addr_init(struct sockaddr_vm *addr, const uuid_le *svr_id) 186ae0078fcSDexuan Cui { 187ae0078fcSDexuan Cui unsigned int port = get_port_by_srv_id(svr_id); 188ae0078fcSDexuan Cui 189ae0078fcSDexuan Cui vsock_addr_init(addr, VMADDR_CID_ANY, port); 190ae0078fcSDexuan Cui } 191ae0078fcSDexuan Cui 192ae0078fcSDexuan Cui static void hvs_remote_addr_init(struct sockaddr_vm *remote, 193ae0078fcSDexuan Cui struct sockaddr_vm *local) 194ae0078fcSDexuan Cui { 195ae0078fcSDexuan Cui static u32 host_ephemeral_port = MIN_HOST_EPHEMERAL_PORT; 196ae0078fcSDexuan Cui struct sock *sk; 197ae0078fcSDexuan Cui 198ae0078fcSDexuan Cui vsock_addr_init(remote, VMADDR_CID_ANY, VMADDR_PORT_ANY); 199ae0078fcSDexuan Cui 200ae0078fcSDexuan Cui while (1) { 201ae0078fcSDexuan Cui /* Wrap around ? */ 202ae0078fcSDexuan Cui if (host_ephemeral_port < MIN_HOST_EPHEMERAL_PORT || 203ae0078fcSDexuan Cui host_ephemeral_port == VMADDR_PORT_ANY) 204ae0078fcSDexuan Cui host_ephemeral_port = MIN_HOST_EPHEMERAL_PORT; 205ae0078fcSDexuan Cui 206ae0078fcSDexuan Cui remote->svm_port = host_ephemeral_port++; 207ae0078fcSDexuan Cui 208ae0078fcSDexuan Cui sk = vsock_find_connected_socket(remote, local); 209ae0078fcSDexuan Cui if (!sk) { 210ae0078fcSDexuan Cui /* Found an available ephemeral port */ 211ae0078fcSDexuan Cui return; 212ae0078fcSDexuan Cui } 213ae0078fcSDexuan Cui 214ae0078fcSDexuan Cui /* Release refcnt got in vsock_find_connected_socket */ 215ae0078fcSDexuan Cui sock_put(sk); 216ae0078fcSDexuan Cui } 217ae0078fcSDexuan Cui } 218ae0078fcSDexuan Cui 219ae0078fcSDexuan Cui static void hvs_set_channel_pending_send_size(struct vmbus_channel *chan) 220ae0078fcSDexuan Cui { 221ae0078fcSDexuan Cui set_channel_pending_send_size(chan, 222ae0078fcSDexuan Cui HVS_PKT_LEN(HVS_SEND_BUF_SIZE)); 223ae0078fcSDexuan Cui 224ae0078fcSDexuan Cui /* See hvs_stream_has_space(): we must make sure the host has seen 225ae0078fcSDexuan Cui * the new pending send size, before we can re-check the writable 226ae0078fcSDexuan Cui * bytes. 227ae0078fcSDexuan Cui */ 228ae0078fcSDexuan Cui virt_mb(); 229ae0078fcSDexuan Cui } 230ae0078fcSDexuan Cui 231ae0078fcSDexuan Cui static void hvs_clear_channel_pending_send_size(struct vmbus_channel *chan) 232ae0078fcSDexuan Cui { 233ae0078fcSDexuan Cui set_channel_pending_send_size(chan, 0); 234ae0078fcSDexuan Cui 235ae0078fcSDexuan Cui /* Ditto */ 236ae0078fcSDexuan Cui virt_mb(); 237ae0078fcSDexuan Cui } 238ae0078fcSDexuan Cui 239ae0078fcSDexuan Cui static bool hvs_channel_readable(struct vmbus_channel *chan) 240ae0078fcSDexuan Cui { 241ae0078fcSDexuan Cui u32 readable = hv_get_bytes_to_read(&chan->inbound); 242ae0078fcSDexuan Cui 243ae0078fcSDexuan Cui /* 0-size payload means FIN */ 244ae0078fcSDexuan Cui return readable >= HVS_PKT_LEN(0); 245ae0078fcSDexuan Cui } 246ae0078fcSDexuan Cui 247ae0078fcSDexuan Cui static int hvs_channel_readable_payload(struct vmbus_channel *chan) 248ae0078fcSDexuan Cui { 249ae0078fcSDexuan Cui u32 readable = hv_get_bytes_to_read(&chan->inbound); 250ae0078fcSDexuan Cui 251ae0078fcSDexuan Cui if (readable > HVS_PKT_LEN(0)) { 252ae0078fcSDexuan Cui /* At least we have 1 byte to read. We don't need to return 253ae0078fcSDexuan Cui * the exact readable bytes: see vsock_stream_recvmsg() -> 254ae0078fcSDexuan Cui * vsock_stream_has_data(). 255ae0078fcSDexuan Cui */ 256ae0078fcSDexuan Cui return 1; 257ae0078fcSDexuan Cui } 258ae0078fcSDexuan Cui 259ae0078fcSDexuan Cui if (readable == HVS_PKT_LEN(0)) { 260ae0078fcSDexuan Cui /* 0-size payload means FIN */ 261ae0078fcSDexuan Cui return 0; 262ae0078fcSDexuan Cui } 263ae0078fcSDexuan Cui 264ae0078fcSDexuan Cui /* No payload or FIN */ 265ae0078fcSDexuan Cui return -1; 266ae0078fcSDexuan Cui } 267ae0078fcSDexuan Cui 268ae0078fcSDexuan Cui static size_t hvs_channel_writable_bytes(struct vmbus_channel *chan) 269ae0078fcSDexuan Cui { 270ae0078fcSDexuan Cui u32 writeable = hv_get_bytes_to_write(&chan->outbound); 271ae0078fcSDexuan Cui size_t ret; 272ae0078fcSDexuan Cui 273ae0078fcSDexuan Cui /* The ringbuffer mustn't be 100% full, and we should reserve a 274ae0078fcSDexuan Cui * zero-length-payload packet for the FIN: see hv_ringbuffer_write() 275ae0078fcSDexuan Cui * and hvs_shutdown(). 276ae0078fcSDexuan Cui */ 277ae0078fcSDexuan Cui if (writeable <= HVS_PKT_LEN(1) + HVS_PKT_LEN(0)) 278ae0078fcSDexuan Cui return 0; 279ae0078fcSDexuan Cui 280ae0078fcSDexuan Cui ret = writeable - HVS_PKT_LEN(1) - HVS_PKT_LEN(0); 281ae0078fcSDexuan Cui 282ae0078fcSDexuan Cui return round_down(ret, 8); 283ae0078fcSDexuan Cui } 284ae0078fcSDexuan Cui 285ae0078fcSDexuan Cui static int hvs_send_data(struct vmbus_channel *chan, 286ae0078fcSDexuan Cui struct hvs_send_buf *send_buf, size_t to_write) 287ae0078fcSDexuan Cui { 288ae0078fcSDexuan Cui send_buf->hdr.pkt_type = 1; 289ae0078fcSDexuan Cui send_buf->hdr.data_size = to_write; 290ae0078fcSDexuan Cui return vmbus_sendpacket(chan, &send_buf->hdr, 291ae0078fcSDexuan Cui sizeof(send_buf->hdr) + to_write, 292ae0078fcSDexuan Cui 0, VM_PKT_DATA_INBAND, 0); 293ae0078fcSDexuan Cui } 294ae0078fcSDexuan Cui 295ae0078fcSDexuan Cui static void hvs_channel_cb(void *ctx) 296ae0078fcSDexuan Cui { 297ae0078fcSDexuan Cui struct sock *sk = (struct sock *)ctx; 298ae0078fcSDexuan Cui struct vsock_sock *vsk = vsock_sk(sk); 299ae0078fcSDexuan Cui struct hvsock *hvs = vsk->trans; 300ae0078fcSDexuan Cui struct vmbus_channel *chan = hvs->chan; 301ae0078fcSDexuan Cui 302ae0078fcSDexuan Cui if (hvs_channel_readable(chan)) 303ae0078fcSDexuan Cui sk->sk_data_ready(sk); 304ae0078fcSDexuan Cui 305ae0078fcSDexuan Cui /* See hvs_stream_has_space(): when we reach here, the writable bytes 306ae0078fcSDexuan Cui * may be already less than HVS_PKT_LEN(HVS_SEND_BUF_SIZE). 307ae0078fcSDexuan Cui */ 308ae0078fcSDexuan Cui if (hv_get_bytes_to_write(&chan->outbound) > 0) 309ae0078fcSDexuan Cui sk->sk_write_space(sk); 310ae0078fcSDexuan Cui } 311ae0078fcSDexuan Cui 312a9eeb998SSunil Muthuswamy static void hvs_do_close_lock_held(struct vsock_sock *vsk, 313a9eeb998SSunil Muthuswamy bool cancel_timeout) 314a9eeb998SSunil Muthuswamy { 315a9eeb998SSunil Muthuswamy struct sock *sk = sk_vsock(vsk); 316a9eeb998SSunil Muthuswamy 317a9eeb998SSunil Muthuswamy sock_set_flag(sk, SOCK_DONE); 318a9eeb998SSunil Muthuswamy vsk->peer_shutdown = SHUTDOWN_MASK; 319a9eeb998SSunil Muthuswamy if (vsock_stream_has_data(vsk) <= 0) 320a9eeb998SSunil Muthuswamy sk->sk_state = TCP_CLOSING; 321a9eeb998SSunil Muthuswamy sk->sk_state_change(sk); 322a9eeb998SSunil Muthuswamy if (vsk->close_work_scheduled && 323a9eeb998SSunil Muthuswamy (!cancel_timeout || cancel_delayed_work(&vsk->close_work))) { 324a9eeb998SSunil Muthuswamy vsk->close_work_scheduled = false; 325a9eeb998SSunil Muthuswamy vsock_remove_sock(vsk); 326a9eeb998SSunil Muthuswamy 327a9eeb998SSunil Muthuswamy /* Release the reference taken while scheduling the timeout */ 328a9eeb998SSunil Muthuswamy sock_put(sk); 329a9eeb998SSunil Muthuswamy } 330a9eeb998SSunil Muthuswamy } 331a9eeb998SSunil Muthuswamy 332ae0078fcSDexuan Cui static void hvs_close_connection(struct vmbus_channel *chan) 333ae0078fcSDexuan Cui { 334ae0078fcSDexuan Cui struct sock *sk = get_per_channel_state(chan); 335ae0078fcSDexuan Cui 336b4562ca7SDexuan Cui lock_sock(sk); 337a9eeb998SSunil Muthuswamy hvs_do_close_lock_held(vsock_sk(sk), true); 338b4562ca7SDexuan Cui release_sock(sk); 339ae0078fcSDexuan Cui } 340ae0078fcSDexuan Cui 341ae0078fcSDexuan Cui static void hvs_open_connection(struct vmbus_channel *chan) 342ae0078fcSDexuan Cui { 343ae0078fcSDexuan Cui uuid_le *if_instance, *if_type; 344ae0078fcSDexuan Cui unsigned char conn_from_host; 345ae0078fcSDexuan Cui 346ae0078fcSDexuan Cui struct sockaddr_vm addr; 347ae0078fcSDexuan Cui struct sock *sk, *new = NULL; 348ac383f58SSunil Muthuswamy struct vsock_sock *vnew = NULL; 349ac383f58SSunil Muthuswamy struct hvsock *hvs = NULL; 350ac383f58SSunil Muthuswamy struct hvsock *hvs_new = NULL; 351ac383f58SSunil Muthuswamy int rcvbuf; 352ae0078fcSDexuan Cui int ret; 353ac383f58SSunil Muthuswamy int sndbuf; 354ae0078fcSDexuan Cui 355ae0078fcSDexuan Cui if_type = &chan->offermsg.offer.if_type; 356ae0078fcSDexuan Cui if_instance = &chan->offermsg.offer.if_instance; 357ae0078fcSDexuan Cui conn_from_host = chan->offermsg.offer.u.pipe.user_def[0]; 358ae0078fcSDexuan Cui 359ae0078fcSDexuan Cui /* The host or the VM should only listen on a port in 360ae0078fcSDexuan Cui * [0, MAX_LISTEN_PORT] 361ae0078fcSDexuan Cui */ 362ae0078fcSDexuan Cui if (!is_valid_srv_id(if_type) || 363ae0078fcSDexuan Cui get_port_by_srv_id(if_type) > MAX_LISTEN_PORT) 364ae0078fcSDexuan Cui return; 365ae0078fcSDexuan Cui 366ae0078fcSDexuan Cui hvs_addr_init(&addr, conn_from_host ? if_type : if_instance); 367ae0078fcSDexuan Cui sk = vsock_find_bound_socket(&addr); 368ae0078fcSDexuan Cui if (!sk) 369ae0078fcSDexuan Cui return; 370ae0078fcSDexuan Cui 371b4562ca7SDexuan Cui lock_sock(sk); 3723b4477d2SStefan Hajnoczi if ((conn_from_host && sk->sk_state != TCP_LISTEN) || 3733b4477d2SStefan Hajnoczi (!conn_from_host && sk->sk_state != TCP_SYN_SENT)) 374ae0078fcSDexuan Cui goto out; 375ae0078fcSDexuan Cui 376ae0078fcSDexuan Cui if (conn_from_host) { 377ae0078fcSDexuan Cui if (sk->sk_ack_backlog >= sk->sk_max_ack_backlog) 378ae0078fcSDexuan Cui goto out; 379ae0078fcSDexuan Cui 380ae0078fcSDexuan Cui new = __vsock_create(sock_net(sk), NULL, sk, GFP_KERNEL, 381ae0078fcSDexuan Cui sk->sk_type, 0); 382ae0078fcSDexuan Cui if (!new) 383ae0078fcSDexuan Cui goto out; 384ae0078fcSDexuan Cui 3853b4477d2SStefan Hajnoczi new->sk_state = TCP_SYN_SENT; 386ae0078fcSDexuan Cui vnew = vsock_sk(new); 387ae0078fcSDexuan Cui hvs_new = vnew->trans; 388ae0078fcSDexuan Cui hvs_new->chan = chan; 389ae0078fcSDexuan Cui } else { 390ae0078fcSDexuan Cui hvs = vsock_sk(sk)->trans; 391ae0078fcSDexuan Cui hvs->chan = chan; 392ae0078fcSDexuan Cui } 393ae0078fcSDexuan Cui 394ae0078fcSDexuan Cui set_channel_read_mode(chan, HV_CALL_DIRECT); 395ac383f58SSunil Muthuswamy 396ac383f58SSunil Muthuswamy /* Use the socket buffer sizes as hints for the VMBUS ring size. For 397ac383f58SSunil Muthuswamy * server side sockets, 'sk' is the parent socket and thus, this will 398ac383f58SSunil Muthuswamy * allow the child sockets to inherit the size from the parent. Keep 399ac383f58SSunil Muthuswamy * the mins to the default value and align to page size as per VMBUS 400ac383f58SSunil Muthuswamy * requirements. 401ac383f58SSunil Muthuswamy * For the max, the socket core library will limit the socket buffer 402ac383f58SSunil Muthuswamy * size that can be set by the user, but, since currently, the hv_sock 403ac383f58SSunil Muthuswamy * VMBUS ring buffer is physically contiguous allocation, restrict it 404ac383f58SSunil Muthuswamy * further. 405ac383f58SSunil Muthuswamy * Older versions of hv_sock host side code cannot handle bigger VMBUS 406ac383f58SSunil Muthuswamy * ring buffer size. Use the version number to limit the change to newer 407ac383f58SSunil Muthuswamy * versions. 408ac383f58SSunil Muthuswamy */ 409ac383f58SSunil Muthuswamy if (vmbus_proto_version < VERSION_WIN10_V5) { 410ac383f58SSunil Muthuswamy sndbuf = RINGBUFFER_HVS_SND_SIZE; 411ac383f58SSunil Muthuswamy rcvbuf = RINGBUFFER_HVS_RCV_SIZE; 412ac383f58SSunil Muthuswamy } else { 413ac383f58SSunil Muthuswamy sndbuf = max_t(int, sk->sk_sndbuf, RINGBUFFER_HVS_SND_SIZE); 414ac383f58SSunil Muthuswamy sndbuf = min_t(int, sndbuf, RINGBUFFER_HVS_MAX_SIZE); 415ac383f58SSunil Muthuswamy sndbuf = ALIGN(sndbuf, PAGE_SIZE); 416ac383f58SSunil Muthuswamy rcvbuf = max_t(int, sk->sk_rcvbuf, RINGBUFFER_HVS_RCV_SIZE); 417ac383f58SSunil Muthuswamy rcvbuf = min_t(int, rcvbuf, RINGBUFFER_HVS_MAX_SIZE); 418ac383f58SSunil Muthuswamy rcvbuf = ALIGN(rcvbuf, PAGE_SIZE); 419ac383f58SSunil Muthuswamy } 420ac383f58SSunil Muthuswamy 421ac383f58SSunil Muthuswamy ret = vmbus_open(chan, sndbuf, rcvbuf, NULL, 0, hvs_channel_cb, 422ac383f58SSunil Muthuswamy conn_from_host ? new : sk); 423ae0078fcSDexuan Cui if (ret != 0) { 424ae0078fcSDexuan Cui if (conn_from_host) { 425ae0078fcSDexuan Cui hvs_new->chan = NULL; 426ae0078fcSDexuan Cui sock_put(new); 427ae0078fcSDexuan Cui } else { 428ae0078fcSDexuan Cui hvs->chan = NULL; 429ae0078fcSDexuan Cui } 430ae0078fcSDexuan Cui goto out; 431ae0078fcSDexuan Cui } 432ae0078fcSDexuan Cui 433ae0078fcSDexuan Cui set_per_channel_state(chan, conn_from_host ? new : sk); 434ae0078fcSDexuan Cui vmbus_set_chn_rescind_callback(chan, hvs_close_connection); 435ae0078fcSDexuan Cui 436ae0078fcSDexuan Cui if (conn_from_host) { 4373b4477d2SStefan Hajnoczi new->sk_state = TCP_ESTABLISHED; 438ae0078fcSDexuan Cui sk->sk_ack_backlog++; 439ae0078fcSDexuan Cui 440ae0078fcSDexuan Cui hvs_addr_init(&vnew->local_addr, if_type); 441ae0078fcSDexuan Cui hvs_remote_addr_init(&vnew->remote_addr, &vnew->local_addr); 442ae0078fcSDexuan Cui 443ae0078fcSDexuan Cui hvs_new->vm_srv_id = *if_type; 444ae0078fcSDexuan Cui hvs_new->host_srv_id = *if_instance; 445ae0078fcSDexuan Cui 446ae0078fcSDexuan Cui vsock_insert_connected(vnew); 447ae0078fcSDexuan Cui 448ae0078fcSDexuan Cui vsock_enqueue_accept(sk, new); 449ae0078fcSDexuan Cui } else { 4503b4477d2SStefan Hajnoczi sk->sk_state = TCP_ESTABLISHED; 451ae0078fcSDexuan Cui sk->sk_socket->state = SS_CONNECTED; 452ae0078fcSDexuan Cui 453ae0078fcSDexuan Cui vsock_insert_connected(vsock_sk(sk)); 454ae0078fcSDexuan Cui } 455ae0078fcSDexuan Cui 456ae0078fcSDexuan Cui sk->sk_state_change(sk); 457ae0078fcSDexuan Cui 458ae0078fcSDexuan Cui out: 459ae0078fcSDexuan Cui /* Release refcnt obtained when we called vsock_find_bound_socket() */ 460ae0078fcSDexuan Cui sock_put(sk); 461b4562ca7SDexuan Cui 462b4562ca7SDexuan Cui release_sock(sk); 463ae0078fcSDexuan Cui } 464ae0078fcSDexuan Cui 465ae0078fcSDexuan Cui static u32 hvs_get_local_cid(void) 466ae0078fcSDexuan Cui { 467ae0078fcSDexuan Cui return VMADDR_CID_ANY; 468ae0078fcSDexuan Cui } 469ae0078fcSDexuan Cui 470ae0078fcSDexuan Cui static int hvs_sock_init(struct vsock_sock *vsk, struct vsock_sock *psk) 471ae0078fcSDexuan Cui { 472ae0078fcSDexuan Cui struct hvsock *hvs; 473ac383f58SSunil Muthuswamy struct sock *sk = sk_vsock(vsk); 474ae0078fcSDexuan Cui 475ae0078fcSDexuan Cui hvs = kzalloc(sizeof(*hvs), GFP_KERNEL); 476ae0078fcSDexuan Cui if (!hvs) 477ae0078fcSDexuan Cui return -ENOMEM; 478ae0078fcSDexuan Cui 479ae0078fcSDexuan Cui vsk->trans = hvs; 480ae0078fcSDexuan Cui hvs->vsk = vsk; 481ac383f58SSunil Muthuswamy sk->sk_sndbuf = RINGBUFFER_HVS_SND_SIZE; 482ac383f58SSunil Muthuswamy sk->sk_rcvbuf = RINGBUFFER_HVS_RCV_SIZE; 483ae0078fcSDexuan Cui return 0; 484ae0078fcSDexuan Cui } 485ae0078fcSDexuan Cui 486ae0078fcSDexuan Cui static int hvs_connect(struct vsock_sock *vsk) 487ae0078fcSDexuan Cui { 488ae0078fcSDexuan Cui union hvs_service_id vm, host; 489ae0078fcSDexuan Cui struct hvsock *h = vsk->trans; 490ae0078fcSDexuan Cui 491ae0078fcSDexuan Cui vm.srv_id = srv_id_template; 492ae0078fcSDexuan Cui vm.svm_port = vsk->local_addr.svm_port; 493ae0078fcSDexuan Cui h->vm_srv_id = vm.srv_id; 494ae0078fcSDexuan Cui 495ae0078fcSDexuan Cui host.srv_id = srv_id_template; 496ae0078fcSDexuan Cui host.svm_port = vsk->remote_addr.svm_port; 497ae0078fcSDexuan Cui h->host_srv_id = host.srv_id; 498ae0078fcSDexuan Cui 499ae0078fcSDexuan Cui return vmbus_send_tl_connect_request(&h->vm_srv_id, &h->host_srv_id); 500ae0078fcSDexuan Cui } 501ae0078fcSDexuan Cui 502a9eeb998SSunil Muthuswamy static void hvs_shutdown_lock_held(struct hvsock *hvs, int mode) 503a9eeb998SSunil Muthuswamy { 504a9eeb998SSunil Muthuswamy struct vmpipe_proto_header hdr; 505a9eeb998SSunil Muthuswamy 506a9eeb998SSunil Muthuswamy if (hvs->fin_sent || !hvs->chan) 507a9eeb998SSunil Muthuswamy return; 508a9eeb998SSunil Muthuswamy 509a9eeb998SSunil Muthuswamy /* It can't fail: see hvs_channel_writable_bytes(). */ 510a9eeb998SSunil Muthuswamy (void)hvs_send_data(hvs->chan, (struct hvs_send_buf *)&hdr, 0); 511a9eeb998SSunil Muthuswamy hvs->fin_sent = true; 512a9eeb998SSunil Muthuswamy } 513a9eeb998SSunil Muthuswamy 514ae0078fcSDexuan Cui static int hvs_shutdown(struct vsock_sock *vsk, int mode) 515ae0078fcSDexuan Cui { 516ae0078fcSDexuan Cui struct sock *sk = sk_vsock(vsk); 517ae0078fcSDexuan Cui 518ae0078fcSDexuan Cui if (!(mode & SEND_SHUTDOWN)) 519ae0078fcSDexuan Cui return 0; 520ae0078fcSDexuan Cui 521ae0078fcSDexuan Cui lock_sock(sk); 522a9eeb998SSunil Muthuswamy hvs_shutdown_lock_held(vsk->trans, mode); 523ae0078fcSDexuan Cui release_sock(sk); 524ae0078fcSDexuan Cui return 0; 525ae0078fcSDexuan Cui } 526ae0078fcSDexuan Cui 527a9eeb998SSunil Muthuswamy static void hvs_close_timeout(struct work_struct *work) 528a9eeb998SSunil Muthuswamy { 529a9eeb998SSunil Muthuswamy struct vsock_sock *vsk = 530a9eeb998SSunil Muthuswamy container_of(work, struct vsock_sock, close_work.work); 531a9eeb998SSunil Muthuswamy struct sock *sk = sk_vsock(vsk); 532a9eeb998SSunil Muthuswamy 533a9eeb998SSunil Muthuswamy sock_hold(sk); 534a9eeb998SSunil Muthuswamy lock_sock(sk); 535a9eeb998SSunil Muthuswamy if (!sock_flag(sk, SOCK_DONE)) 536a9eeb998SSunil Muthuswamy hvs_do_close_lock_held(vsk, false); 537a9eeb998SSunil Muthuswamy 538a9eeb998SSunil Muthuswamy vsk->close_work_scheduled = false; 539a9eeb998SSunil Muthuswamy release_sock(sk); 540a9eeb998SSunil Muthuswamy sock_put(sk); 541a9eeb998SSunil Muthuswamy } 542a9eeb998SSunil Muthuswamy 543a9eeb998SSunil Muthuswamy /* Returns true, if it is safe to remove socket; false otherwise */ 544a9eeb998SSunil Muthuswamy static bool hvs_close_lock_held(struct vsock_sock *vsk) 545a9eeb998SSunil Muthuswamy { 546a9eeb998SSunil Muthuswamy struct sock *sk = sk_vsock(vsk); 547a9eeb998SSunil Muthuswamy 548a9eeb998SSunil Muthuswamy if (!(sk->sk_state == TCP_ESTABLISHED || 549a9eeb998SSunil Muthuswamy sk->sk_state == TCP_CLOSING)) 550a9eeb998SSunil Muthuswamy return true; 551a9eeb998SSunil Muthuswamy 552a9eeb998SSunil Muthuswamy if ((sk->sk_shutdown & SHUTDOWN_MASK) != SHUTDOWN_MASK) 553a9eeb998SSunil Muthuswamy hvs_shutdown_lock_held(vsk->trans, SHUTDOWN_MASK); 554a9eeb998SSunil Muthuswamy 555a9eeb998SSunil Muthuswamy if (sock_flag(sk, SOCK_DONE)) 556a9eeb998SSunil Muthuswamy return true; 557a9eeb998SSunil Muthuswamy 558a9eeb998SSunil Muthuswamy /* This reference will be dropped by the delayed close routine */ 559a9eeb998SSunil Muthuswamy sock_hold(sk); 560a9eeb998SSunil Muthuswamy INIT_DELAYED_WORK(&vsk->close_work, hvs_close_timeout); 561a9eeb998SSunil Muthuswamy vsk->close_work_scheduled = true; 562a9eeb998SSunil Muthuswamy schedule_delayed_work(&vsk->close_work, HVS_CLOSE_TIMEOUT); 563a9eeb998SSunil Muthuswamy return false; 564a9eeb998SSunil Muthuswamy } 565a9eeb998SSunil Muthuswamy 566ae0078fcSDexuan Cui static void hvs_release(struct vsock_sock *vsk) 567ae0078fcSDexuan Cui { 568b4562ca7SDexuan Cui struct sock *sk = sk_vsock(vsk); 569a9eeb998SSunil Muthuswamy bool remove_sock; 570ae0078fcSDexuan Cui 571b4562ca7SDexuan Cui lock_sock(sk); 572a9eeb998SSunil Muthuswamy remove_sock = hvs_close_lock_held(vsk); 573b4562ca7SDexuan Cui release_sock(sk); 574a9eeb998SSunil Muthuswamy if (remove_sock) 575a9eeb998SSunil Muthuswamy vsock_remove_sock(vsk); 576ae0078fcSDexuan Cui } 577ae0078fcSDexuan Cui 578ae0078fcSDexuan Cui static void hvs_destruct(struct vsock_sock *vsk) 579ae0078fcSDexuan Cui { 580ae0078fcSDexuan Cui struct hvsock *hvs = vsk->trans; 581ae0078fcSDexuan Cui struct vmbus_channel *chan = hvs->chan; 582ae0078fcSDexuan Cui 583ae0078fcSDexuan Cui if (chan) 584ae0078fcSDexuan Cui vmbus_hvsock_device_unregister(chan); 585ae0078fcSDexuan Cui 586ae0078fcSDexuan Cui kfree(hvs); 587ae0078fcSDexuan Cui } 588ae0078fcSDexuan Cui 589ae0078fcSDexuan Cui static int hvs_dgram_bind(struct vsock_sock *vsk, struct sockaddr_vm *addr) 590ae0078fcSDexuan Cui { 591ae0078fcSDexuan Cui return -EOPNOTSUPP; 592ae0078fcSDexuan Cui } 593ae0078fcSDexuan Cui 594ae0078fcSDexuan Cui static int hvs_dgram_dequeue(struct vsock_sock *vsk, struct msghdr *msg, 595ae0078fcSDexuan Cui size_t len, int flags) 596ae0078fcSDexuan Cui { 597ae0078fcSDexuan Cui return -EOPNOTSUPP; 598ae0078fcSDexuan Cui } 599ae0078fcSDexuan Cui 600ae0078fcSDexuan Cui static int hvs_dgram_enqueue(struct vsock_sock *vsk, 601ae0078fcSDexuan Cui struct sockaddr_vm *remote, struct msghdr *msg, 602ae0078fcSDexuan Cui size_t dgram_len) 603ae0078fcSDexuan Cui { 604ae0078fcSDexuan Cui return -EOPNOTSUPP; 605ae0078fcSDexuan Cui } 606ae0078fcSDexuan Cui 607ae0078fcSDexuan Cui static bool hvs_dgram_allow(u32 cid, u32 port) 608ae0078fcSDexuan Cui { 609ae0078fcSDexuan Cui return false; 610ae0078fcSDexuan Cui } 611ae0078fcSDexuan Cui 612ae0078fcSDexuan Cui static int hvs_update_recv_data(struct hvsock *hvs) 613ae0078fcSDexuan Cui { 614ae0078fcSDexuan Cui struct hvs_recv_buf *recv_buf; 615ae0078fcSDexuan Cui u32 payload_len; 616ae0078fcSDexuan Cui 617ae0078fcSDexuan Cui recv_buf = (struct hvs_recv_buf *)(hvs->recv_desc + 1); 618ae0078fcSDexuan Cui payload_len = recv_buf->hdr.data_size; 619ae0078fcSDexuan Cui 620ae0078fcSDexuan Cui if (payload_len > HVS_MTU_SIZE) 621ae0078fcSDexuan Cui return -EIO; 622ae0078fcSDexuan Cui 623ae0078fcSDexuan Cui if (payload_len == 0) 624ae0078fcSDexuan Cui hvs->vsk->peer_shutdown |= SEND_SHUTDOWN; 625ae0078fcSDexuan Cui 626ae0078fcSDexuan Cui hvs->recv_data_len = payload_len; 627ae0078fcSDexuan Cui hvs->recv_data_off = 0; 628ae0078fcSDexuan Cui 629ae0078fcSDexuan Cui return 0; 630ae0078fcSDexuan Cui } 631ae0078fcSDexuan Cui 632ae0078fcSDexuan Cui static ssize_t hvs_stream_dequeue(struct vsock_sock *vsk, struct msghdr *msg, 633ae0078fcSDexuan Cui size_t len, int flags) 634ae0078fcSDexuan Cui { 635ae0078fcSDexuan Cui struct hvsock *hvs = vsk->trans; 636ae0078fcSDexuan Cui bool need_refill = !hvs->recv_desc; 637ae0078fcSDexuan Cui struct hvs_recv_buf *recv_buf; 638ae0078fcSDexuan Cui u32 to_read; 639ae0078fcSDexuan Cui int ret; 640ae0078fcSDexuan Cui 641ae0078fcSDexuan Cui if (flags & MSG_PEEK) 642ae0078fcSDexuan Cui return -EOPNOTSUPP; 643ae0078fcSDexuan Cui 644ae0078fcSDexuan Cui if (need_refill) { 645ae0078fcSDexuan Cui hvs->recv_desc = hv_pkt_iter_first(hvs->chan); 646ae0078fcSDexuan Cui ret = hvs_update_recv_data(hvs); 647ae0078fcSDexuan Cui if (ret) 648ae0078fcSDexuan Cui return ret; 649ae0078fcSDexuan Cui } 650ae0078fcSDexuan Cui 651ae0078fcSDexuan Cui recv_buf = (struct hvs_recv_buf *)(hvs->recv_desc + 1); 652ae0078fcSDexuan Cui to_read = min_t(u32, len, hvs->recv_data_len); 653ae0078fcSDexuan Cui ret = memcpy_to_msg(msg, recv_buf->data + hvs->recv_data_off, to_read); 654ae0078fcSDexuan Cui if (ret != 0) 655ae0078fcSDexuan Cui return ret; 656ae0078fcSDexuan Cui 657ae0078fcSDexuan Cui hvs->recv_data_len -= to_read; 658ae0078fcSDexuan Cui if (hvs->recv_data_len == 0) { 659ae0078fcSDexuan Cui hvs->recv_desc = hv_pkt_iter_next(hvs->chan, hvs->recv_desc); 660ae0078fcSDexuan Cui if (hvs->recv_desc) { 661ae0078fcSDexuan Cui ret = hvs_update_recv_data(hvs); 662ae0078fcSDexuan Cui if (ret) 663ae0078fcSDexuan Cui return ret; 664ae0078fcSDexuan Cui } 665ae0078fcSDexuan Cui } else { 666ae0078fcSDexuan Cui hvs->recv_data_off += to_read; 667ae0078fcSDexuan Cui } 668ae0078fcSDexuan Cui 669ae0078fcSDexuan Cui return to_read; 670ae0078fcSDexuan Cui } 671ae0078fcSDexuan Cui 672ae0078fcSDexuan Cui static ssize_t hvs_stream_enqueue(struct vsock_sock *vsk, struct msghdr *msg, 673ae0078fcSDexuan Cui size_t len) 674ae0078fcSDexuan Cui { 675ae0078fcSDexuan Cui struct hvsock *hvs = vsk->trans; 676ae0078fcSDexuan Cui struct vmbus_channel *chan = hvs->chan; 677ae0078fcSDexuan Cui struct hvs_send_buf *send_buf; 67814a1eaa8SSunil Muthuswamy ssize_t to_write, max_writable; 67914a1eaa8SSunil Muthuswamy ssize_t ret = 0; 68014a1eaa8SSunil Muthuswamy ssize_t bytes_written = 0; 681ae0078fcSDexuan Cui 682ae0078fcSDexuan Cui BUILD_BUG_ON(sizeof(*send_buf) != PAGE_SIZE_4K); 683ae0078fcSDexuan Cui 684ae0078fcSDexuan Cui send_buf = kmalloc(sizeof(*send_buf), GFP_KERNEL); 685ae0078fcSDexuan Cui if (!send_buf) 686ae0078fcSDexuan Cui return -ENOMEM; 687ae0078fcSDexuan Cui 68814a1eaa8SSunil Muthuswamy /* Reader(s) could be draining data from the channel as we write. 68914a1eaa8SSunil Muthuswamy * Maximize bandwidth, by iterating until the channel is found to be 69014a1eaa8SSunil Muthuswamy * full. 69114a1eaa8SSunil Muthuswamy */ 69214a1eaa8SSunil Muthuswamy while (len) { 693ae0078fcSDexuan Cui max_writable = hvs_channel_writable_bytes(chan); 69414a1eaa8SSunil Muthuswamy if (!max_writable) 69514a1eaa8SSunil Muthuswamy break; 696ae0078fcSDexuan Cui to_write = min_t(ssize_t, len, max_writable); 697ae0078fcSDexuan Cui to_write = min_t(ssize_t, to_write, HVS_SEND_BUF_SIZE); 69814a1eaa8SSunil Muthuswamy /* memcpy_from_msg is safe for loop as it advances the offsets 69914a1eaa8SSunil Muthuswamy * within the message iterator. 70014a1eaa8SSunil Muthuswamy */ 701ae0078fcSDexuan Cui ret = memcpy_from_msg(send_buf->data, msg, to_write); 702ae0078fcSDexuan Cui if (ret < 0) 703ae0078fcSDexuan Cui goto out; 704ae0078fcSDexuan Cui 705ae0078fcSDexuan Cui ret = hvs_send_data(hvs->chan, send_buf, to_write); 706ae0078fcSDexuan Cui if (ret < 0) 707ae0078fcSDexuan Cui goto out; 708ae0078fcSDexuan Cui 70914a1eaa8SSunil Muthuswamy bytes_written += to_write; 71014a1eaa8SSunil Muthuswamy len -= to_write; 71114a1eaa8SSunil Muthuswamy } 712ae0078fcSDexuan Cui out: 71314a1eaa8SSunil Muthuswamy /* If any data has been sent, return that */ 71414a1eaa8SSunil Muthuswamy if (bytes_written) 71514a1eaa8SSunil Muthuswamy ret = bytes_written; 716ae0078fcSDexuan Cui kfree(send_buf); 717ae0078fcSDexuan Cui return ret; 718ae0078fcSDexuan Cui } 719ae0078fcSDexuan Cui 720ae0078fcSDexuan Cui static s64 hvs_stream_has_data(struct vsock_sock *vsk) 721ae0078fcSDexuan Cui { 722ae0078fcSDexuan Cui struct hvsock *hvs = vsk->trans; 723ae0078fcSDexuan Cui s64 ret; 724ae0078fcSDexuan Cui 725ae0078fcSDexuan Cui if (hvs->recv_data_len > 0) 726ae0078fcSDexuan Cui return 1; 727ae0078fcSDexuan Cui 728ae0078fcSDexuan Cui switch (hvs_channel_readable_payload(hvs->chan)) { 729ae0078fcSDexuan Cui case 1: 730ae0078fcSDexuan Cui ret = 1; 731ae0078fcSDexuan Cui break; 732ae0078fcSDexuan Cui case 0: 733ae0078fcSDexuan Cui vsk->peer_shutdown |= SEND_SHUTDOWN; 734ae0078fcSDexuan Cui ret = 0; 735ae0078fcSDexuan Cui break; 736ae0078fcSDexuan Cui default: /* -1 */ 737ae0078fcSDexuan Cui ret = 0; 738ae0078fcSDexuan Cui break; 739ae0078fcSDexuan Cui } 740ae0078fcSDexuan Cui 741ae0078fcSDexuan Cui return ret; 742ae0078fcSDexuan Cui } 743ae0078fcSDexuan Cui 744ae0078fcSDexuan Cui static s64 hvs_stream_has_space(struct vsock_sock *vsk) 745ae0078fcSDexuan Cui { 746ae0078fcSDexuan Cui struct hvsock *hvs = vsk->trans; 747ae0078fcSDexuan Cui struct vmbus_channel *chan = hvs->chan; 748ae0078fcSDexuan Cui s64 ret; 749ae0078fcSDexuan Cui 750ae0078fcSDexuan Cui ret = hvs_channel_writable_bytes(chan); 751ae0078fcSDexuan Cui if (ret > 0) { 752ae0078fcSDexuan Cui hvs_clear_channel_pending_send_size(chan); 753ae0078fcSDexuan Cui } else { 754ae0078fcSDexuan Cui /* See hvs_channel_cb() */ 755ae0078fcSDexuan Cui hvs_set_channel_pending_send_size(chan); 756ae0078fcSDexuan Cui 757ae0078fcSDexuan Cui /* Re-check the writable bytes to avoid race */ 758ae0078fcSDexuan Cui ret = hvs_channel_writable_bytes(chan); 759ae0078fcSDexuan Cui if (ret > 0) 760ae0078fcSDexuan Cui hvs_clear_channel_pending_send_size(chan); 761ae0078fcSDexuan Cui } 762ae0078fcSDexuan Cui 763ae0078fcSDexuan Cui return ret; 764ae0078fcSDexuan Cui } 765ae0078fcSDexuan Cui 766ae0078fcSDexuan Cui static u64 hvs_stream_rcvhiwat(struct vsock_sock *vsk) 767ae0078fcSDexuan Cui { 768ae0078fcSDexuan Cui return HVS_MTU_SIZE + 1; 769ae0078fcSDexuan Cui } 770ae0078fcSDexuan Cui 771ae0078fcSDexuan Cui static bool hvs_stream_is_active(struct vsock_sock *vsk) 772ae0078fcSDexuan Cui { 773ae0078fcSDexuan Cui struct hvsock *hvs = vsk->trans; 774ae0078fcSDexuan Cui 775ae0078fcSDexuan Cui return hvs->chan != NULL; 776ae0078fcSDexuan Cui } 777ae0078fcSDexuan Cui 778ae0078fcSDexuan Cui static bool hvs_stream_allow(u32 cid, u32 port) 779ae0078fcSDexuan Cui { 780ae0078fcSDexuan Cui /* The host's port range [MIN_HOST_EPHEMERAL_PORT, 0xFFFFFFFF) is 781ae0078fcSDexuan Cui * reserved as ephemeral ports, which are used as the host's ports 782ae0078fcSDexuan Cui * when the host initiates connections. 783ae0078fcSDexuan Cui * 784ae0078fcSDexuan Cui * Perform this check in the guest so an immediate error is produced 785ae0078fcSDexuan Cui * instead of a timeout. 786ae0078fcSDexuan Cui */ 787ae0078fcSDexuan Cui if (port > MAX_HOST_LISTEN_PORT) 788ae0078fcSDexuan Cui return false; 789ae0078fcSDexuan Cui 790ae0078fcSDexuan Cui if (cid == VMADDR_CID_HOST) 791ae0078fcSDexuan Cui return true; 792ae0078fcSDexuan Cui 793ae0078fcSDexuan Cui return false; 794ae0078fcSDexuan Cui } 795ae0078fcSDexuan Cui 796ae0078fcSDexuan Cui static 797ae0078fcSDexuan Cui int hvs_notify_poll_in(struct vsock_sock *vsk, size_t target, bool *readable) 798ae0078fcSDexuan Cui { 799ae0078fcSDexuan Cui struct hvsock *hvs = vsk->trans; 800ae0078fcSDexuan Cui 801ae0078fcSDexuan Cui *readable = hvs_channel_readable(hvs->chan); 802ae0078fcSDexuan Cui return 0; 803ae0078fcSDexuan Cui } 804ae0078fcSDexuan Cui 805ae0078fcSDexuan Cui static 806ae0078fcSDexuan Cui int hvs_notify_poll_out(struct vsock_sock *vsk, size_t target, bool *writable) 807ae0078fcSDexuan Cui { 808ae0078fcSDexuan Cui *writable = hvs_stream_has_space(vsk) > 0; 809ae0078fcSDexuan Cui 810ae0078fcSDexuan Cui return 0; 811ae0078fcSDexuan Cui } 812ae0078fcSDexuan Cui 813ae0078fcSDexuan Cui static 814ae0078fcSDexuan Cui int hvs_notify_recv_init(struct vsock_sock *vsk, size_t target, 815ae0078fcSDexuan Cui struct vsock_transport_recv_notify_data *d) 816ae0078fcSDexuan Cui { 817ae0078fcSDexuan Cui return 0; 818ae0078fcSDexuan Cui } 819ae0078fcSDexuan Cui 820ae0078fcSDexuan Cui static 821ae0078fcSDexuan Cui int hvs_notify_recv_pre_block(struct vsock_sock *vsk, size_t target, 822ae0078fcSDexuan Cui struct vsock_transport_recv_notify_data *d) 823ae0078fcSDexuan Cui { 824ae0078fcSDexuan Cui return 0; 825ae0078fcSDexuan Cui } 826ae0078fcSDexuan Cui 827ae0078fcSDexuan Cui static 828ae0078fcSDexuan Cui int hvs_notify_recv_pre_dequeue(struct vsock_sock *vsk, size_t target, 829ae0078fcSDexuan Cui struct vsock_transport_recv_notify_data *d) 830ae0078fcSDexuan Cui { 831ae0078fcSDexuan Cui return 0; 832ae0078fcSDexuan Cui } 833ae0078fcSDexuan Cui 834ae0078fcSDexuan Cui static 835ae0078fcSDexuan Cui int hvs_notify_recv_post_dequeue(struct vsock_sock *vsk, size_t target, 836ae0078fcSDexuan Cui ssize_t copied, bool data_read, 837ae0078fcSDexuan Cui struct vsock_transport_recv_notify_data *d) 838ae0078fcSDexuan Cui { 839ae0078fcSDexuan Cui return 0; 840ae0078fcSDexuan Cui } 841ae0078fcSDexuan Cui 842ae0078fcSDexuan Cui static 843ae0078fcSDexuan Cui int hvs_notify_send_init(struct vsock_sock *vsk, 844ae0078fcSDexuan Cui struct vsock_transport_send_notify_data *d) 845ae0078fcSDexuan Cui { 846ae0078fcSDexuan Cui return 0; 847ae0078fcSDexuan Cui } 848ae0078fcSDexuan Cui 849ae0078fcSDexuan Cui static 850ae0078fcSDexuan Cui int hvs_notify_send_pre_block(struct vsock_sock *vsk, 851ae0078fcSDexuan Cui struct vsock_transport_send_notify_data *d) 852ae0078fcSDexuan Cui { 853ae0078fcSDexuan Cui return 0; 854ae0078fcSDexuan Cui } 855ae0078fcSDexuan Cui 856ae0078fcSDexuan Cui static 857ae0078fcSDexuan Cui int hvs_notify_send_pre_enqueue(struct vsock_sock *vsk, 858ae0078fcSDexuan Cui struct vsock_transport_send_notify_data *d) 859ae0078fcSDexuan Cui { 860ae0078fcSDexuan Cui return 0; 861ae0078fcSDexuan Cui } 862ae0078fcSDexuan Cui 863ae0078fcSDexuan Cui static 864ae0078fcSDexuan Cui int hvs_notify_send_post_enqueue(struct vsock_sock *vsk, ssize_t written, 865ae0078fcSDexuan Cui struct vsock_transport_send_notify_data *d) 866ae0078fcSDexuan Cui { 867ae0078fcSDexuan Cui return 0; 868ae0078fcSDexuan Cui } 869ae0078fcSDexuan Cui 870ae0078fcSDexuan Cui static void hvs_set_buffer_size(struct vsock_sock *vsk, u64 val) 871ae0078fcSDexuan Cui { 872ae0078fcSDexuan Cui /* Ignored. */ 873ae0078fcSDexuan Cui } 874ae0078fcSDexuan Cui 875ae0078fcSDexuan Cui static void hvs_set_min_buffer_size(struct vsock_sock *vsk, u64 val) 876ae0078fcSDexuan Cui { 877ae0078fcSDexuan Cui /* Ignored. */ 878ae0078fcSDexuan Cui } 879ae0078fcSDexuan Cui 880ae0078fcSDexuan Cui static void hvs_set_max_buffer_size(struct vsock_sock *vsk, u64 val) 881ae0078fcSDexuan Cui { 882ae0078fcSDexuan Cui /* Ignored. */ 883ae0078fcSDexuan Cui } 884ae0078fcSDexuan Cui 885ae0078fcSDexuan Cui static u64 hvs_get_buffer_size(struct vsock_sock *vsk) 886ae0078fcSDexuan Cui { 887ae0078fcSDexuan Cui return -ENOPROTOOPT; 888ae0078fcSDexuan Cui } 889ae0078fcSDexuan Cui 890ae0078fcSDexuan Cui static u64 hvs_get_min_buffer_size(struct vsock_sock *vsk) 891ae0078fcSDexuan Cui { 892ae0078fcSDexuan Cui return -ENOPROTOOPT; 893ae0078fcSDexuan Cui } 894ae0078fcSDexuan Cui 895ae0078fcSDexuan Cui static u64 hvs_get_max_buffer_size(struct vsock_sock *vsk) 896ae0078fcSDexuan Cui { 897ae0078fcSDexuan Cui return -ENOPROTOOPT; 898ae0078fcSDexuan Cui } 899ae0078fcSDexuan Cui 900ae0078fcSDexuan Cui static struct vsock_transport hvs_transport = { 901ae0078fcSDexuan Cui .get_local_cid = hvs_get_local_cid, 902ae0078fcSDexuan Cui 903ae0078fcSDexuan Cui .init = hvs_sock_init, 904ae0078fcSDexuan Cui .destruct = hvs_destruct, 905ae0078fcSDexuan Cui .release = hvs_release, 906ae0078fcSDexuan Cui .connect = hvs_connect, 907ae0078fcSDexuan Cui .shutdown = hvs_shutdown, 908ae0078fcSDexuan Cui 909ae0078fcSDexuan Cui .dgram_bind = hvs_dgram_bind, 910ae0078fcSDexuan Cui .dgram_dequeue = hvs_dgram_dequeue, 911ae0078fcSDexuan Cui .dgram_enqueue = hvs_dgram_enqueue, 912ae0078fcSDexuan Cui .dgram_allow = hvs_dgram_allow, 913ae0078fcSDexuan Cui 914ae0078fcSDexuan Cui .stream_dequeue = hvs_stream_dequeue, 915ae0078fcSDexuan Cui .stream_enqueue = hvs_stream_enqueue, 916ae0078fcSDexuan Cui .stream_has_data = hvs_stream_has_data, 917ae0078fcSDexuan Cui .stream_has_space = hvs_stream_has_space, 918ae0078fcSDexuan Cui .stream_rcvhiwat = hvs_stream_rcvhiwat, 919ae0078fcSDexuan Cui .stream_is_active = hvs_stream_is_active, 920ae0078fcSDexuan Cui .stream_allow = hvs_stream_allow, 921ae0078fcSDexuan Cui 922ae0078fcSDexuan Cui .notify_poll_in = hvs_notify_poll_in, 923ae0078fcSDexuan Cui .notify_poll_out = hvs_notify_poll_out, 924ae0078fcSDexuan Cui .notify_recv_init = hvs_notify_recv_init, 925ae0078fcSDexuan Cui .notify_recv_pre_block = hvs_notify_recv_pre_block, 926ae0078fcSDexuan Cui .notify_recv_pre_dequeue = hvs_notify_recv_pre_dequeue, 927ae0078fcSDexuan Cui .notify_recv_post_dequeue = hvs_notify_recv_post_dequeue, 928ae0078fcSDexuan Cui .notify_send_init = hvs_notify_send_init, 929ae0078fcSDexuan Cui .notify_send_pre_block = hvs_notify_send_pre_block, 930ae0078fcSDexuan Cui .notify_send_pre_enqueue = hvs_notify_send_pre_enqueue, 931ae0078fcSDexuan Cui .notify_send_post_enqueue = hvs_notify_send_post_enqueue, 932ae0078fcSDexuan Cui 933ae0078fcSDexuan Cui .set_buffer_size = hvs_set_buffer_size, 934ae0078fcSDexuan Cui .set_min_buffer_size = hvs_set_min_buffer_size, 935ae0078fcSDexuan Cui .set_max_buffer_size = hvs_set_max_buffer_size, 936ae0078fcSDexuan Cui .get_buffer_size = hvs_get_buffer_size, 937ae0078fcSDexuan Cui .get_min_buffer_size = hvs_get_min_buffer_size, 938ae0078fcSDexuan Cui .get_max_buffer_size = hvs_get_max_buffer_size, 939ae0078fcSDexuan Cui }; 940ae0078fcSDexuan Cui 941ae0078fcSDexuan Cui static int hvs_probe(struct hv_device *hdev, 942ae0078fcSDexuan Cui const struct hv_vmbus_device_id *dev_id) 943ae0078fcSDexuan Cui { 944ae0078fcSDexuan Cui struct vmbus_channel *chan = hdev->channel; 945ae0078fcSDexuan Cui 946ae0078fcSDexuan Cui hvs_open_connection(chan); 947ae0078fcSDexuan Cui 948ae0078fcSDexuan Cui /* Always return success to suppress the unnecessary error message 949ae0078fcSDexuan Cui * in vmbus_probe(): on error the host will rescind the device in 950ae0078fcSDexuan Cui * 30 seconds and we can do cleanup at that time in 951ae0078fcSDexuan Cui * vmbus_onoffer_rescind(). 952ae0078fcSDexuan Cui */ 953ae0078fcSDexuan Cui return 0; 954ae0078fcSDexuan Cui } 955ae0078fcSDexuan Cui 956ae0078fcSDexuan Cui static int hvs_remove(struct hv_device *hdev) 957ae0078fcSDexuan Cui { 958ae0078fcSDexuan Cui struct vmbus_channel *chan = hdev->channel; 959ae0078fcSDexuan Cui 960ae0078fcSDexuan Cui vmbus_close(chan); 961ae0078fcSDexuan Cui 962ae0078fcSDexuan Cui return 0; 963ae0078fcSDexuan Cui } 964ae0078fcSDexuan Cui 965ae0078fcSDexuan Cui /* This isn't really used. See vmbus_match() and vmbus_probe() */ 966ae0078fcSDexuan Cui static const struct hv_vmbus_device_id id_table[] = { 967ae0078fcSDexuan Cui {}, 968ae0078fcSDexuan Cui }; 969ae0078fcSDexuan Cui 970ae0078fcSDexuan Cui static struct hv_driver hvs_drv = { 971ae0078fcSDexuan Cui .name = "hv_sock", 972ae0078fcSDexuan Cui .hvsock = true, 973ae0078fcSDexuan Cui .id_table = id_table, 974ae0078fcSDexuan Cui .probe = hvs_probe, 975ae0078fcSDexuan Cui .remove = hvs_remove, 976ae0078fcSDexuan Cui }; 977ae0078fcSDexuan Cui 978ae0078fcSDexuan Cui static int __init hvs_init(void) 979ae0078fcSDexuan Cui { 980ae0078fcSDexuan Cui int ret; 981ae0078fcSDexuan Cui 982ae0078fcSDexuan Cui if (vmbus_proto_version < VERSION_WIN10) 983ae0078fcSDexuan Cui return -ENODEV; 984ae0078fcSDexuan Cui 985ae0078fcSDexuan Cui ret = vmbus_driver_register(&hvs_drv); 986ae0078fcSDexuan Cui if (ret != 0) 987ae0078fcSDexuan Cui return ret; 988ae0078fcSDexuan Cui 989ae0078fcSDexuan Cui ret = vsock_core_init(&hvs_transport); 990ae0078fcSDexuan Cui if (ret) { 991ae0078fcSDexuan Cui vmbus_driver_unregister(&hvs_drv); 992ae0078fcSDexuan Cui return ret; 993ae0078fcSDexuan Cui } 994ae0078fcSDexuan Cui 995ae0078fcSDexuan Cui return 0; 996ae0078fcSDexuan Cui } 997ae0078fcSDexuan Cui 998ae0078fcSDexuan Cui static void __exit hvs_exit(void) 999ae0078fcSDexuan Cui { 1000ae0078fcSDexuan Cui vsock_core_exit(); 1001ae0078fcSDexuan Cui vmbus_driver_unregister(&hvs_drv); 1002ae0078fcSDexuan Cui } 1003ae0078fcSDexuan Cui 1004ae0078fcSDexuan Cui module_init(hvs_init); 1005ae0078fcSDexuan Cui module_exit(hvs_exit); 1006ae0078fcSDexuan Cui 1007ae0078fcSDexuan Cui MODULE_DESCRIPTION("Hyper-V Sockets"); 1008ae0078fcSDexuan Cui MODULE_VERSION("1.0.0"); 1009ae0078fcSDexuan Cui MODULE_LICENSE("GPL"); 1010ae0078fcSDexuan Cui MODULE_ALIAS_NETPROTO(PF_VSOCK); 1011