17a58be3whu/*-
27a58be3whu * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
37a58be3whu *
47a58be3whu * Copyright (c) 2020 Microsoft Corp.
57a58be3whu * All rights reserved.
67a58be3whu *
77a58be3whu * Redistribution and use in source and binary forms, with or without
87a58be3whu * modification, are permitted provided that the following conditions
97a58be3whu * are met:
107a58be3whu * 1. Redistributions of source code must retain the above copyright
117a58be3whu *    notice unmodified, this list of conditions, and the following
127a58be3whu *    disclaimer.
137a58be3whu * 2. Redistributions in binary form must reproduce the above copyright
147a58be3whu *    notice, this list of conditions and the following disclaimer in the
157a58be3whu *    documentation and/or other materials provided with the distribution.
167a58be3whu *
177a58be3whu * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
187a58be3whu * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
197a58be3whu * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
207a58be3whu * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
217a58be3whu * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
227a58be3whu * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
237a58be3whu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
247a58be3whu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
257a58be3whu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
267a58be3whu * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
277a58be3whu *
287a58be3whu * $FreeBSD$
297a58be3whu */
307a58be3whu
317a58be3whu#ifndef _HVSOCK_H
327a58be3whu#define _HVSOCK_H
337a58be3whu#include <sys/socket.h>
347a58be3whu#include <sys/socketvar.h>
357a58be3whu#include <sys/queue.h>
367a58be3whu
377a58be3whu#include <dev/hyperv/include/hyperv.h>
387a58be3whu#include <dev/hyperv/include/vmbus.h>
397a58be3whu
407a58be3whu/*
417a58be3whu * HyperV Socket Protocols
427a58be3whu */
437a58be3whu#define	HYPERV_SOCK_PROTO_TRANS		1	/* Transport protocol */
447a58be3whu
457a58be3whu#define	HVADDR_PORT_ANY			-1U
467a58be3whu#define	HVADDR_PORT_UNKNOWN		-1U
477a58be3whu
487a58be3whu#define HVS_LIST_BOUND			0x01
497a58be3whu#define HVS_LIST_CONNECTED		0x02
507a58be3whu#define HVS_LIST_ALL			(HVS_LIST_BOUND | HVS_LIST_CONNECTED)
517a58be3whu
527a58be3whustruct sockaddr_hvs {
537a58be3whu	unsigned char	sa_len;
547a58be3whu	sa_family_t	sa_family;
557a58be3whu	unsigned int	hvs_port;
567a58be3whu	unsigned char	hvs_zero[sizeof(struct sockaddr) -
577a58be3whu				 sizeof(sa_family_t) -
587a58be3whu				 sizeof(unsigned char) -
597a58be3whu				 sizeof(unsigned int)];
607a58be3whu};
617a58be3whu
627a58be3whustruct vmpipe_proto_header {
637a58be3whu	uint32_t			vmpipe_pkt_type;
647a58be3whu	uint32_t			vmpipe_data_size;
657a58be3whu} __packed;
667a58be3whu
677a58be3whustruct hvs_pkt_header {
687a58be3whu	struct vmbus_chanpkt_hdr	chan_pkt_hdr;
697a58be3whu	struct vmpipe_proto_header	vmpipe_pkt_hdr;
707a58be3whu} __packed;
717a58be3whu
727a58be3whustruct hvs_pcb {
737a58be3whu	struct socket			*so;		/* Pointer to socket */
747a58be3whu	struct sockaddr_hvs		local_addr;
757a58be3whu	struct sockaddr_hvs		remote_addr;
767a58be3whu
777a58be3whu	struct hyperv_guid		vm_srv_id;
787a58be3whu	struct hyperv_guid		host_srv_id;
797a58be3whu
807a58be3whu	struct vmbus_channel		*chan;
817a58be3whu	/* Current packet header on rx ring */
827a58be3whu	struct hvs_pkt_header		hvs_pkt;
837a58be3whu	/* Available data in receive br in current packet */
847a58be3whu	uint32_t			recv_data_len;
857a58be3whu	/* offset in the packet */
867a58be3whu	uint32_t			recv_data_off;
877a58be3whu	bool				rb_init;
887a58be3whu	/* Link lists for global bound and connected sockets */
897a58be3whu	LIST_ENTRY(hvs_pcb)		bound_next;
907a58be3whu	LIST_ENTRY(hvs_pcb)		connected_next;
917a58be3whu};
927a58be3whu
937a58be3whu#define so2hvspcb(so) \
947a58be3whu	((struct hvs_pcb *)((so)->so_pcb))
957a58be3whu#define hsvpcb2so(hvspcb) \
967a58be3whu	((struct socket *)((hvspcb)->so))
977a58be3whu
987a58be3whuvoid	hvs_addr_init(struct sockaddr_hvs *, const struct hyperv_guid *);
997a58be3whuvoid	hvs_trans_init(void);
1007a58be3whuvoid	hvs_trans_close(struct socket *);
1017a58be3whuvoid	hvs_trans_detach(struct socket *);
1027a58be3whuvoid	hvs_trans_abort(struct socket *);
1037a58be3whuint	hvs_trans_attach(struct socket *, int, struct thread *);
1047a58be3whuint	hvs_trans_bind(struct socket *, struct sockaddr *, struct thread *);
1057a58be3whuint	hvs_trans_listen(struct socket *, int, struct thread *);
1067a58be3whuint	hvs_trans_accept(struct socket *, struct sockaddr **);
1077a58be3whuint	hvs_trans_connect(struct socket *,
1087a58be3whu	    struct sockaddr *, struct thread *);
1097a58be3whuint	hvs_trans_peeraddr(struct socket *, struct sockaddr **);
1107a58be3whuint	hvs_trans_sockaddr(struct socket *, struct sockaddr **);
1117a58be3whuint	hvs_trans_soreceive(struct socket *, struct sockaddr **,
1127a58be3whu	    struct uio *, struct mbuf **, struct mbuf **, int *);
1137a58be3whuint	hvs_trans_sosend(struct socket *, struct sockaddr *, struct uio *,
1147a58be3whu	     struct mbuf *, struct mbuf *, int, struct thread *);
1157a58be3whuint	hvs_trans_disconnect(struct socket *);
1167a58be3whuint	hvs_trans_shutdown(struct socket *);
1177a58be3whu
1187a58be3whuint	hvs_trans_lock(void);
1197a58be3whuvoid	hvs_trans_unlock(void);
1207a58be3whu
1217a58be3whuvoid	hvs_remove_socket_from_list(struct socket *, unsigned char);
1227a58be3whu#endif /* _HVSOCK_H */
123