diff --git a/sys/dev/hyperv/hvsock/hv_sock.c b/sys/dev/hyperv/hvsock/hv_sock.c --- a/sys/dev/hyperv/hvsock/hv_sock.c +++ b/sys/dev/hyperv/hvsock/hv_sock.c @@ -83,11 +83,6 @@ roundup2(payload_len, 8) + \ sizeof(uint64_t)) -#define so2hvspcb(so) \ - ((struct hvs_pcb *)((so)->so_pcb)) -#define hsvpcb2so(hvspcb) \ - ((struct socket *)((hvspcb)->so)) - /* * HyperV Transport socket functions */ @@ -176,7 +171,7 @@ static void __hvs_remove_socket_from_list(struct socket *so, unsigned char list) { - struct hvs_pcb *pcb = so2hvspcb(so); + struct hvs_pcb *pcb = so->so_pcb; HVSOCK_DBG(HVSOCK_DBG_VERBOSE, "%s: pcb is %p\n", __func__, pcb); @@ -186,7 +181,7 @@ static void __hvs_insert_socket_on_list(struct socket *so, unsigned char list) { - struct hvs_pcb *pcb = so2hvspcb(so); + struct hvs_pcb *pcb = so->so_pcb; if (list & HVS_LIST_BOUND) LIST_INSERT_HEAD(&hvs_trans_bound_socks, @@ -197,20 +192,6 @@ pcb, connected_next); } -static void -hvs_remove_socket_from_list(struct socket *so, unsigned char list) -{ - if (!so || !so->so_pcb) { - HVSOCK_DBG(HVSOCK_DBG_VERBOSE, - "%s: socket or so_pcb is null\n", __func__); - return; - } - - mtx_lock(&hvs_trans_socks_mtx); - __hvs_remove_socket_from_list(so, list); - mtx_unlock(&hvs_trans_socks_mtx); -} - static void hvs_insert_socket_on_list(struct socket *so, unsigned char list) { @@ -272,14 +253,13 @@ hvs_addr_set(addr, get_port_by_srv_id(svr_id)); } -static int +static inline void hvs_trans_lock(void) { sx_xlock(&hvs_trans_socks_sx); - return (0); } -static void +static inline void hvs_trans_unlock(void) { sx_xunlock(&hvs_trans_socks_sx); @@ -321,25 +301,20 @@ static int hvs_trans_attach(struct socket *so, int proto, struct thread *td) { - struct hvs_pcb *pcb = so2hvspcb(so); + struct hvs_pcb *pcb = so->so_pcb; HVSOCK_DBG(HVSOCK_DBG_VERBOSE, "%s: HyperV Socket hvs_trans_attach called\n", __func__); - if (so->so_type != SOCK_STREAM) - return (ESOCKTNOSUPPORT); - if (proto != 0 && proto != HYPERV_SOCK_PROTO_TRANS) return (EPROTONOSUPPORT); - if (pcb != NULL) - return (EISCONN); pcb = malloc(sizeof(struct hvs_pcb), M_HVSOCK, M_NOWAIT | M_ZERO); if (pcb == NULL) return (ENOMEM); pcb->so = so; - so->so_pcb = (void *)pcb; + so->so_pcb = pcb; return (0); } @@ -352,12 +327,8 @@ HVSOCK_DBG(HVSOCK_DBG_VERBOSE, "%s: HyperV Socket hvs_trans_detach called\n", __func__); - (void) hvs_trans_lock(); - pcb = so2hvspcb(so); - if (pcb == NULL) { - hvs_trans_unlock(); - return; - } + hvs_trans_lock(); + pcb = so->so_pcb; if (SOLISTENING(so)) { zfree(pcb, M_HVSOCK); @@ -371,7 +342,7 @@ static int hvs_trans_bind(struct socket *so, struct sockaddr *addr, struct thread *td) { - struct hvs_pcb *pcb = so2hvspcb(so); + struct hvs_pcb *pcb = so->so_pcb; struct sockaddr_hvs *sa = (struct sockaddr_hvs *) addr; int error = 0; @@ -382,10 +353,6 @@ return (EINVAL); } - if (pcb == NULL) { - return (EINVAL); - } - if (sa->sa_family != AF_HYPERV) { HVSOCK_DBG(HVSOCK_DBG_ERR, "%s: Not supported, sa_family is %u\n", @@ -423,16 +390,13 @@ static int hvs_trans_listen(struct socket *so, int backlog, struct thread *td) { - struct hvs_pcb *pcb = so2hvspcb(so); + struct hvs_pcb *pcb = so->so_pcb; struct socket *bound_so; int error; HVSOCK_DBG(HVSOCK_DBG_VERBOSE, "%s: HyperV Socket hvs_trans_listen called\n", __func__); - if (pcb == NULL) - return (EINVAL); - /* Check if the address is already bound and it was by us. */ bound_so = hvs_find_socket_on_list(&pcb->local_addr, HVS_LIST_BOUND); if (bound_so == NULL || bound_so != so) { @@ -455,14 +419,11 @@ static int hvs_trans_accept(struct socket *so, struct sockaddr *sa) { - struct hvs_pcb *pcb = so2hvspcb(so); + struct hvs_pcb *pcb = so->so_pcb; HVSOCK_DBG(HVSOCK_DBG_VERBOSE, "%s: HyperV Socket hvs_trans_accept called\n", __func__); - if (pcb == NULL) - return (EINVAL); - memcpy(sa, &pcb->remote_addr, pcb->remote_addr.sa_len); return (0); @@ -471,7 +432,7 @@ static int hvs_trans_connect(struct socket *so, struct sockaddr *nam, struct thread *td) { - struct hvs_pcb *pcb = so2hvspcb(so); + struct hvs_pcb *pcb = so->so_pcb; struct sockaddr_hvs *raddr = (struct sockaddr_hvs *)nam; bool found_auto_bound_port = false; int i, error = 0; @@ -480,9 +441,6 @@ "%s: HyperV Socket hvs_trans_connect called, remote port is %x\n", __func__, raddr->hvs_port); - if (pcb == NULL) - return (EINVAL); - /* Verify the remote address */ if (raddr == NULL) return (EINVAL); @@ -562,17 +520,10 @@ static int hvs_trans_disconnect(struct socket *so) { - struct hvs_pcb *pcb; - HVSOCK_DBG(HVSOCK_DBG_VERBOSE, "%s: HyperV Socket hvs_trans_disconnect called\n", __func__); - (void) hvs_trans_lock(); - pcb = so2hvspcb(so); - if (pcb == NULL) { - hvs_trans_unlock(); - return (EINVAL); - } + hvs_trans_lock(); /* If socket is already disconnected, skip this */ if ((so->so_state & SS_ISDISCONNECTED) == 0) @@ -592,7 +543,7 @@ hvs_trans_soreceive(struct socket *so, struct sockaddr **paddr, struct uio *uio, struct mbuf **mp0, struct mbuf **controlp, int *flagsp) { - struct hvs_pcb *pcb = so2hvspcb(so); + struct hvs_pcb *pcb = so->so_pcb; struct sockbuf *sb; ssize_t orig_resid; uint32_t canread, to_read; @@ -602,11 +553,6 @@ HVSOCK_DBG(HVSOCK_DBG_VERBOSE, "%s: HyperV Socket hvs_trans_soreceive called\n", __func__); - if (so->so_type != SOCK_STREAM) - return (EINVAL); - if (pcb == NULL) - return (EINVAL); - if (flagsp != NULL) flags = *flagsp &~ MSG_EOR; else @@ -757,7 +703,7 @@ hvs_trans_sosend(struct socket *so, struct sockaddr *addr, struct uio *uio, struct mbuf *top, struct mbuf *controlp, int flags, struct thread *td) { - struct hvs_pcb *pcb = so2hvspcb(so); + struct hvs_pcb *pcb = so->so_pcb; struct sockbuf *sb; ssize_t orig_resid; uint32_t canwrite, to_write; @@ -767,11 +713,6 @@ "%s: HyperV Socket hvs_trans_sosend called, uio_resid = %zd\n", __func__, uio->uio_resid); - if (so->so_type != SOCK_STREAM) - return (EINVAL); - if (pcb == NULL) - return (EINVAL); - /* If nothing to send */ if (uio->uio_resid == 0 || uio->uio_rw != UIO_WRITE) return (EINVAL); @@ -853,14 +794,11 @@ static int hvs_trans_peeraddr(struct socket *so, struct sockaddr *sa) { - struct hvs_pcb *pcb = so2hvspcb(so); + struct hvs_pcb *pcb = so->so_pcb; HVSOCK_DBG(HVSOCK_DBG_VERBOSE, "%s: HyperV Socket hvs_trans_peeraddr called\n", __func__); - if (pcb == NULL) - return (EINVAL); - memcpy(sa, &pcb->remote_addr, pcb->remote_addr.sa_len); return (0); @@ -869,14 +807,11 @@ static int hvs_trans_sockaddr(struct socket *so, struct sockaddr *sa) { - struct hvs_pcb *pcb = so2hvspcb(so); + struct hvs_pcb *pcb = so->so_pcb; HVSOCK_DBG(HVSOCK_DBG_VERBOSE, "%s: HyperV Socket hvs_trans_sockaddr called\n", __func__); - if (pcb == NULL) - return (EINVAL); - memcpy(sa, &pcb->local_addr, pcb->local_addr.sa_len); return (0); @@ -890,8 +825,8 @@ HVSOCK_DBG(HVSOCK_DBG_VERBOSE, "%s: HyperV Socket hvs_trans_close called\n", __func__); - (void) hvs_trans_lock(); - pcb = so2hvspcb(so); + hvs_trans_lock(); + pcb = so->so_pcb; if (!pcb) { hvs_trans_unlock(); return; @@ -926,16 +861,10 @@ static void hvs_trans_abort(struct socket *so) { - struct hvs_pcb *pcb = so2hvspcb(so); - HVSOCK_DBG(HVSOCK_DBG_VERBOSE, "%s: HyperV Socket hvs_trans_abort called\n", __func__); - (void) hvs_trans_lock(); - if (pcb == NULL) { - hvs_trans_unlock(); - return; - } + hvs_trans_lock(); if (SOLISTENING(so)) { mtx_lock(&hvs_trans_socks_mtx); @@ -955,7 +884,7 @@ static int hvs_trans_shutdown(struct socket *so, enum shutdown_how how) { - struct hvs_pcb *pcb = so2hvspcb(so); + struct hvs_pcb *pcb = so->so_pcb; HVSOCK_DBG(HVSOCK_DBG_VERBOSE, "%s: HyperV Socket hvs_trans_shutdown called\n", __func__); @@ -968,9 +897,6 @@ } SOCK_UNLOCK(so); - if (pcb == NULL) - return (EINVAL); - switch (how) { case SHUT_RD: socantrcvmore(so); @@ -1108,8 +1034,8 @@ * Here we know channel is still open. Need to make * sure the socket has not been closed or freed. */ - (void) hvs_trans_lock(); - so = hsvpcb2so(pcb); + hvs_trans_lock(); + so = pcb->so; if (pcb->chan != NULL && so != NULL) { /* @@ -1407,7 +1333,7 @@ hvsock_open_channel(struct vmbus_channel *chan, struct socket *so) { unsigned int rcvbuf, sndbuf; - struct hvs_pcb *pcb = so2hvspcb(so); + struct hvs_pcb *pcb = so->so_pcb; int ret; if (vmbus_current_version < VMBUS_VERSION_WIN10_V5) { @@ -1603,10 +1529,10 @@ if (conn_from_host) { hvsock_open_conn_passive(chan, so, sc); } else { - (void) hvs_trans_lock(); + hvs_trans_lock(); pcb = so->so_pcb; if (pcb && pcb->so) { - sc->pcb = so2hvspcb(so); + sc->pcb = so->so_pcb; hvsock_open_conn_active(chan, so); } else { HVSOCK_DBG(HVSOCK_DBG_VERBOSE, @@ -1667,9 +1593,9 @@ HVSOCK_DBG(HVSOCK_DBG_VERBOSE, "hvsock_detach called.\n"); if (sc->pcb != NULL) { - (void) hvs_trans_lock(); + hvs_trans_lock(); - so = hsvpcb2so(sc->pcb); + so = sc->pcb->so; if (so) { /* Close the connection */ if (so->so_state &