17ac58230SAlexander Motin /*- 24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 3f24882ecSPedro F. Giffuni * 47ac58230SAlexander Motin * Copyright (c) 2015 Alexander Motin <mav@FreeBSD.org> 57ac58230SAlexander Motin * All rights reserved. 67ac58230SAlexander Motin * 77ac58230SAlexander Motin * Redistribution and use in source and binary forms, with or without 87ac58230SAlexander Motin * modification, are permitted provided that the following conditions 97ac58230SAlexander Motin * are met: 107ac58230SAlexander Motin * 1. Redistributions of source code must retain the above copyright 117ac58230SAlexander Motin * notice, this list of conditions and the following disclaimer, 127ac58230SAlexander Motin * without modification, immediately at the beginning of the file. 137ac58230SAlexander Motin * 2. Redistributions in binary form must reproduce the above copyright 147ac58230SAlexander Motin * notice, this list of conditions and the following disclaimer in the 157ac58230SAlexander Motin * documentation and/or other materials provided with the distribution. 167ac58230SAlexander Motin * 177ac58230SAlexander Motin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 187ac58230SAlexander Motin * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 197ac58230SAlexander Motin * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 207ac58230SAlexander Motin * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 217ac58230SAlexander Motin * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 227ac58230SAlexander Motin * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 237ac58230SAlexander Motin * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 247ac58230SAlexander Motin * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 257ac58230SAlexander Motin * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 267ac58230SAlexander Motin * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 277ac58230SAlexander Motin */ 287ac58230SAlexander Motin 297ac58230SAlexander Motin #include <sys/param.h> 30e2e050c8SConrad Meyer #include <sys/condvar.h> 31e2e050c8SConrad Meyer #include <sys/conf.h> 32e2e050c8SConrad Meyer #include <sys/eventhandler.h> 337ac58230SAlexander Motin #include <sys/kernel.h> 347ac58230SAlexander Motin #include <sys/kthread.h> 357ac58230SAlexander Motin #include <sys/limits.h> 367ac58230SAlexander Motin #include <sys/lock.h> 377ac58230SAlexander Motin #include <sys/malloc.h> 387ac58230SAlexander Motin #include <sys/mbuf.h> 39e2e050c8SConrad Meyer #include <sys/module.h> 40e2e050c8SConrad Meyer #include <sys/mutex.h> 417ac58230SAlexander Motin #include <sys/proc.h> 427ac58230SAlexander Motin #include <sys/queue.h> 437ac58230SAlexander Motin #include <sys/socket.h> 447ac58230SAlexander Motin #include <sys/socketvar.h> 45e2e050c8SConrad Meyer #include <sys/sysctl.h> 46e2e050c8SConrad Meyer #include <sys/systm.h> 477ac58230SAlexander Motin #include <sys/uio.h> 487ac58230SAlexander Motin #include <netinet/in.h> 497ac58230SAlexander Motin #include <netinet/tcp.h> 507ac58230SAlexander Motin #include <vm/uma.h> 517ac58230SAlexander Motin 527ac58230SAlexander Motin #include <cam/cam.h> 537ac58230SAlexander Motin #include <cam/scsi/scsi_all.h> 547ac58230SAlexander Motin #include <cam/scsi/scsi_da.h> 557ac58230SAlexander Motin #include <cam/ctl/ctl_io.h> 567ac58230SAlexander Motin #include <cam/ctl/ctl.h> 577ac58230SAlexander Motin #include <cam/ctl/ctl_frontend.h> 587ac58230SAlexander Motin #include <cam/ctl/ctl_util.h> 597ac58230SAlexander Motin #include <cam/ctl/ctl_backend.h> 607ac58230SAlexander Motin #include <cam/ctl/ctl_ioctl.h> 617ac58230SAlexander Motin #include <cam/ctl/ctl_ha.h> 627ac58230SAlexander Motin #include <cam/ctl/ctl_private.h> 637ac58230SAlexander Motin #include <cam/ctl/ctl_debug.h> 647ac58230SAlexander Motin #include <cam/ctl/ctl_error.h> 657ac58230SAlexander Motin 667ac58230SAlexander Motin struct ha_msg_wire { 677ac58230SAlexander Motin uint32_t channel; 687ac58230SAlexander Motin uint32_t length; 697ac58230SAlexander Motin }; 707ac58230SAlexander Motin 717ac58230SAlexander Motin struct ha_dt_msg_wire { 727ac58230SAlexander Motin ctl_ha_dt_cmd command; 737ac58230SAlexander Motin uint32_t size; 747ac58230SAlexander Motin uint8_t *local; 757ac58230SAlexander Motin uint8_t *remote; 767ac58230SAlexander Motin }; 777ac58230SAlexander Motin 787ac58230SAlexander Motin struct ha_softc { 797ac58230SAlexander Motin struct ctl_softc *ha_ctl_softc; 807ac58230SAlexander Motin ctl_evt_handler ha_handler[CTL_HA_CHAN_MAX]; 817ac58230SAlexander Motin char ha_peer[128]; 827ac58230SAlexander Motin struct sockaddr_in ha_peer_in; 837ac58230SAlexander Motin struct socket *ha_lso; 847ac58230SAlexander Motin struct socket *ha_so; 857ac58230SAlexander Motin struct mbufq ha_sendq; 867ac58230SAlexander Motin struct mbuf *ha_sending; 877ac58230SAlexander Motin struct mtx ha_lock; 887ac58230SAlexander Motin int ha_connect; 897ac58230SAlexander Motin int ha_listen; 907ac58230SAlexander Motin int ha_connected; 917ac58230SAlexander Motin int ha_receiving; 927ac58230SAlexander Motin int ha_wakeup; 937ac58230SAlexander Motin int ha_disconnect; 9459bb97a9SAlexander Motin int ha_shutdown; 9559bb97a9SAlexander Motin eventhandler_tag ha_shutdown_eh; 967ac58230SAlexander Motin TAILQ_HEAD(, ctl_ha_dt_req) ha_dts; 977ac58230SAlexander Motin } ha_softc; 987ac58230SAlexander Motin 997ac58230SAlexander Motin static void 1007ac58230SAlexander Motin ctl_ha_conn_wake(struct ha_softc *softc) 1017ac58230SAlexander Motin { 1027ac58230SAlexander Motin 1037ac58230SAlexander Motin mtx_lock(&softc->ha_lock); 1047ac58230SAlexander Motin softc->ha_wakeup = 1; 1057ac58230SAlexander Motin mtx_unlock(&softc->ha_lock); 1067ac58230SAlexander Motin wakeup(&softc->ha_wakeup); 1077ac58230SAlexander Motin } 1087ac58230SAlexander Motin 1097ac58230SAlexander Motin static int 1107ac58230SAlexander Motin ctl_ha_lupcall(struct socket *so, void *arg, int waitflag) 1117ac58230SAlexander Motin { 1127ac58230SAlexander Motin struct ha_softc *softc = arg; 1137ac58230SAlexander Motin 1147ac58230SAlexander Motin ctl_ha_conn_wake(softc); 1157ac58230SAlexander Motin return (SU_OK); 1167ac58230SAlexander Motin } 1177ac58230SAlexander Motin 1187ac58230SAlexander Motin static int 1197ac58230SAlexander Motin ctl_ha_rupcall(struct socket *so, void *arg, int waitflag) 1207ac58230SAlexander Motin { 1217ac58230SAlexander Motin struct ha_softc *softc = arg; 1227ac58230SAlexander Motin 1237ac58230SAlexander Motin wakeup(&softc->ha_receiving); 1247ac58230SAlexander Motin return (SU_OK); 1257ac58230SAlexander Motin } 1267ac58230SAlexander Motin 1277ac58230SAlexander Motin static int 1287ac58230SAlexander Motin ctl_ha_supcall(struct socket *so, void *arg, int waitflag) 1297ac58230SAlexander Motin { 1307ac58230SAlexander Motin struct ha_softc *softc = arg; 1317ac58230SAlexander Motin 1327ac58230SAlexander Motin ctl_ha_conn_wake(softc); 1337ac58230SAlexander Motin return (SU_OK); 1347ac58230SAlexander Motin } 1357ac58230SAlexander Motin 1367ac58230SAlexander Motin static void 1377ac58230SAlexander Motin ctl_ha_evt(struct ha_softc *softc, ctl_ha_channel ch, ctl_ha_event evt, 1387ac58230SAlexander Motin int param) 1397ac58230SAlexander Motin { 1407ac58230SAlexander Motin int i; 1417ac58230SAlexander Motin 1427ac58230SAlexander Motin if (ch < CTL_HA_CHAN_MAX) { 1437ac58230SAlexander Motin if (softc->ha_handler[ch]) 1447ac58230SAlexander Motin softc->ha_handler[ch](ch, evt, param); 1457ac58230SAlexander Motin return; 1467ac58230SAlexander Motin } 1477ac58230SAlexander Motin for (i = 0; i < CTL_HA_CHAN_MAX; i++) { 1487ac58230SAlexander Motin if (softc->ha_handler[i]) 1497ac58230SAlexander Motin softc->ha_handler[i](i, evt, param); 1507ac58230SAlexander Motin } 1517ac58230SAlexander Motin } 1527ac58230SAlexander Motin 1537ac58230SAlexander Motin static void 1547ac58230SAlexander Motin ctl_ha_close(struct ha_softc *softc) 1557ac58230SAlexander Motin { 1567ac58230SAlexander Motin struct socket *so = softc->ha_so; 1577ac58230SAlexander Motin int report = 0; 1587ac58230SAlexander Motin 1597ac58230SAlexander Motin if (softc->ha_connected || softc->ha_disconnect) { 1607ac58230SAlexander Motin softc->ha_connected = 0; 1617ac58230SAlexander Motin mbufq_drain(&softc->ha_sendq); 1627ac58230SAlexander Motin m_freem(softc->ha_sending); 1637ac58230SAlexander Motin softc->ha_sending = NULL; 1647ac58230SAlexander Motin report = 1; 1657ac58230SAlexander Motin } 1667ac58230SAlexander Motin if (so) { 167*0d378958SGleb Smirnoff SOCK_RECVBUF_LOCK(so); 1687ac58230SAlexander Motin soupcall_clear(so, SO_RCV); 1697ac58230SAlexander Motin while (softc->ha_receiving) { 1707ac58230SAlexander Motin wakeup(&softc->ha_receiving); 171*0d378958SGleb Smirnoff msleep(&softc->ha_receiving, SOCK_RECVBUF_MTX(so), 1727ac58230SAlexander Motin 0, "ha_rx exit", 0); 1737ac58230SAlexander Motin } 174*0d378958SGleb Smirnoff SOCK_RECVBUF_UNLOCK(so); 175*0d378958SGleb Smirnoff SOCK_SENDBUF_LOCK(so); 1767ac58230SAlexander Motin soupcall_clear(so, SO_SND); 177*0d378958SGleb Smirnoff SOCK_SENDBUF_UNLOCK(so); 1787ac58230SAlexander Motin softc->ha_so = NULL; 1797ac58230SAlexander Motin if (softc->ha_connect) 1807ac58230SAlexander Motin pause("reconnect", hz / 2); 1817ac58230SAlexander Motin soclose(so); 1827ac58230SAlexander Motin } 1837ac58230SAlexander Motin if (report) { 1847ac58230SAlexander Motin ctl_ha_evt(softc, CTL_HA_CHAN_MAX, CTL_HA_EVT_LINK_CHANGE, 1857ac58230SAlexander Motin (softc->ha_connect || softc->ha_listen) ? 1867ac58230SAlexander Motin CTL_HA_LINK_UNKNOWN : CTL_HA_LINK_OFFLINE); 1877ac58230SAlexander Motin } 1887ac58230SAlexander Motin } 1897ac58230SAlexander Motin 1907ac58230SAlexander Motin static void 1917ac58230SAlexander Motin ctl_ha_lclose(struct ha_softc *softc) 1927ac58230SAlexander Motin { 1937ac58230SAlexander Motin 1947ac58230SAlexander Motin if (softc->ha_lso) { 1953873b149SAlexander Motin if (SOLISTENING(softc->ha_lso)) { 1963873b149SAlexander Motin SOLISTEN_LOCK(softc->ha_lso); 1973873b149SAlexander Motin solisten_upcall_set(softc->ha_lso, NULL, NULL); 1983873b149SAlexander Motin SOLISTEN_UNLOCK(softc->ha_lso); 1993873b149SAlexander Motin } 2007ac58230SAlexander Motin soclose(softc->ha_lso); 2017ac58230SAlexander Motin softc->ha_lso = NULL; 2027ac58230SAlexander Motin } 2037ac58230SAlexander Motin } 2047ac58230SAlexander Motin 2057ac58230SAlexander Motin static void 2067ac58230SAlexander Motin ctl_ha_rx_thread(void *arg) 2077ac58230SAlexander Motin { 2087ac58230SAlexander Motin struct ha_softc *softc = arg; 2097ac58230SAlexander Motin struct socket *so = softc->ha_so; 2107ac58230SAlexander Motin struct ha_msg_wire wire_hdr; 2117ac58230SAlexander Motin struct uio uio; 2127ac58230SAlexander Motin struct iovec iov; 2137ac58230SAlexander Motin int error, flags, next; 2147ac58230SAlexander Motin 2157ac58230SAlexander Motin bzero(&wire_hdr, sizeof(wire_hdr)); 2167ac58230SAlexander Motin while (1) { 2177ac58230SAlexander Motin if (wire_hdr.length > 0) 2187ac58230SAlexander Motin next = wire_hdr.length; 2197ac58230SAlexander Motin else 2207ac58230SAlexander Motin next = sizeof(wire_hdr); 221*0d378958SGleb Smirnoff SOCK_RECVBUF_LOCK(so); 222a85700a9SAlexander Motin while (sbavail(&so->so_rcv) < next || softc->ha_disconnect) { 223a85700a9SAlexander Motin if (softc->ha_connected == 0 || softc->ha_disconnect || 224a85700a9SAlexander Motin so->so_error || 2257ac58230SAlexander Motin (so->so_rcv.sb_state & SBS_CANTRCVMORE)) { 2267ac58230SAlexander Motin goto errout; 2277ac58230SAlexander Motin } 2287ac58230SAlexander Motin so->so_rcv.sb_lowat = next; 229*0d378958SGleb Smirnoff msleep(&softc->ha_receiving, SOCK_RECVBUF_MTX(so), 2307ac58230SAlexander Motin 0, "-", 0); 2317ac58230SAlexander Motin } 232*0d378958SGleb Smirnoff SOCK_RECVBUF_UNLOCK(so); 2337ac58230SAlexander Motin 2347ac58230SAlexander Motin if (wire_hdr.length == 0) { 2357ac58230SAlexander Motin iov.iov_base = &wire_hdr; 2367ac58230SAlexander Motin iov.iov_len = sizeof(wire_hdr); 2377ac58230SAlexander Motin uio.uio_iov = &iov; 2387ac58230SAlexander Motin uio.uio_iovcnt = 1; 2397ac58230SAlexander Motin uio.uio_rw = UIO_READ; 2407ac58230SAlexander Motin uio.uio_segflg = UIO_SYSSPACE; 2417ac58230SAlexander Motin uio.uio_td = curthread; 2427ac58230SAlexander Motin uio.uio_resid = sizeof(wire_hdr); 2437ac58230SAlexander Motin flags = MSG_DONTWAIT; 2447ac58230SAlexander Motin error = soreceive(softc->ha_so, NULL, &uio, NULL, 2457ac58230SAlexander Motin NULL, &flags); 2467ac58230SAlexander Motin if (error != 0) { 2477ac58230SAlexander Motin printf("%s: header receive error %d\n", 2487ac58230SAlexander Motin __func__, error); 249*0d378958SGleb Smirnoff SOCK_RECVBUF_LOCK(so); 2507ac58230SAlexander Motin goto errout; 2517ac58230SAlexander Motin } 2527ac58230SAlexander Motin } else { 2537ac58230SAlexander Motin ctl_ha_evt(softc, wire_hdr.channel, 2547ac58230SAlexander Motin CTL_HA_EVT_MSG_RECV, wire_hdr.length); 2557ac58230SAlexander Motin wire_hdr.length = 0; 2567ac58230SAlexander Motin } 2577ac58230SAlexander Motin } 2587ac58230SAlexander Motin 2597ac58230SAlexander Motin errout: 2607ac58230SAlexander Motin softc->ha_receiving = 0; 2617ac58230SAlexander Motin wakeup(&softc->ha_receiving); 262*0d378958SGleb Smirnoff SOCK_RECVBUF_UNLOCK(so); 2637ac58230SAlexander Motin ctl_ha_conn_wake(softc); 2647ac58230SAlexander Motin kthread_exit(); 2657ac58230SAlexander Motin } 2667ac58230SAlexander Motin 2677ac58230SAlexander Motin static void 2687ac58230SAlexander Motin ctl_ha_send(struct ha_softc *softc) 2697ac58230SAlexander Motin { 2707ac58230SAlexander Motin struct socket *so = softc->ha_so; 2717ac58230SAlexander Motin int error; 2727ac58230SAlexander Motin 2737ac58230SAlexander Motin while (1) { 2747ac58230SAlexander Motin if (softc->ha_sending == NULL) { 2757ac58230SAlexander Motin mtx_lock(&softc->ha_lock); 2767ac58230SAlexander Motin softc->ha_sending = mbufq_dequeue(&softc->ha_sendq); 2777ac58230SAlexander Motin mtx_unlock(&softc->ha_lock); 2787ac58230SAlexander Motin if (softc->ha_sending == NULL) { 2797ac58230SAlexander Motin so->so_snd.sb_lowat = so->so_snd.sb_hiwat + 1; 2807ac58230SAlexander Motin break; 2817ac58230SAlexander Motin } 2827ac58230SAlexander Motin } 283*0d378958SGleb Smirnoff SOCK_SENDBUF_LOCK(so); 2847ac58230SAlexander Motin if (sbspace(&so->so_snd) < softc->ha_sending->m_pkthdr.len) { 2857ac58230SAlexander Motin so->so_snd.sb_lowat = softc->ha_sending->m_pkthdr.len; 286*0d378958SGleb Smirnoff SOCK_SENDBUF_UNLOCK(so); 2877ac58230SAlexander Motin break; 2887ac58230SAlexander Motin } 289*0d378958SGleb Smirnoff SOCK_SENDBUF_UNLOCK(so); 2907ac58230SAlexander Motin error = sosend(softc->ha_so, NULL, NULL, softc->ha_sending, 2917ac58230SAlexander Motin NULL, MSG_DONTWAIT, curthread); 2927ac58230SAlexander Motin softc->ha_sending = NULL; 2937ac58230SAlexander Motin if (error != 0) { 2947ac58230SAlexander Motin printf("%s: sosend() error %d\n", __func__, error); 2957ac58230SAlexander Motin return; 2967ac58230SAlexander Motin } 29774b8d63dSPedro F. Giffuni } 2987ac58230SAlexander Motin } 2997ac58230SAlexander Motin 3007ac58230SAlexander Motin static void 3017ac58230SAlexander Motin ctl_ha_sock_setup(struct ha_softc *softc) 3027ac58230SAlexander Motin { 3037ac58230SAlexander Motin struct sockopt opt; 3047ac58230SAlexander Motin struct socket *so = softc->ha_so; 3057ac58230SAlexander Motin int error, val; 3067ac58230SAlexander Motin 3077ac58230SAlexander Motin val = 1024 * 1024; 3087ac58230SAlexander Motin error = soreserve(so, val, val); 3097ac58230SAlexander Motin if (error) 3107ac58230SAlexander Motin printf("%s: soreserve failed %d\n", __func__, error); 3117ac58230SAlexander Motin 312*0d378958SGleb Smirnoff SOCK_RECVBUF_LOCK(so); 3137ac58230SAlexander Motin so->so_rcv.sb_lowat = sizeof(struct ha_msg_wire); 3147ac58230SAlexander Motin soupcall_set(so, SO_RCV, ctl_ha_rupcall, softc); 315*0d378958SGleb Smirnoff SOCK_RECVBUF_UNLOCK(so); 316*0d378958SGleb Smirnoff SOCK_SENDBUF_LOCK(so); 3177ac58230SAlexander Motin so->so_snd.sb_lowat = sizeof(struct ha_msg_wire); 3187ac58230SAlexander Motin soupcall_set(so, SO_SND, ctl_ha_supcall, softc); 319*0d378958SGleb Smirnoff SOCK_SENDBUF_UNLOCK(so); 3207ac58230SAlexander Motin 3217ac58230SAlexander Motin bzero(&opt, sizeof(struct sockopt)); 3227ac58230SAlexander Motin opt.sopt_dir = SOPT_SET; 3237ac58230SAlexander Motin opt.sopt_level = SOL_SOCKET; 3247ac58230SAlexander Motin opt.sopt_name = SO_KEEPALIVE; 3257ac58230SAlexander Motin opt.sopt_val = &val; 3267ac58230SAlexander Motin opt.sopt_valsize = sizeof(val); 3277ac58230SAlexander Motin val = 1; 3287ac58230SAlexander Motin error = sosetopt(so, &opt); 3297ac58230SAlexander Motin if (error) 3307ac58230SAlexander Motin printf("%s: KEEPALIVE setting failed %d\n", __func__, error); 3317ac58230SAlexander Motin 3327ac58230SAlexander Motin opt.sopt_level = IPPROTO_TCP; 3337ac58230SAlexander Motin opt.sopt_name = TCP_NODELAY; 3347ac58230SAlexander Motin val = 1; 3357ac58230SAlexander Motin error = sosetopt(so, &opt); 3367ac58230SAlexander Motin if (error) 3377ac58230SAlexander Motin printf("%s: NODELAY setting failed %d\n", __func__, error); 3387ac58230SAlexander Motin 3397ac58230SAlexander Motin opt.sopt_name = TCP_KEEPINIT; 3407ac58230SAlexander Motin val = 3; 3417ac58230SAlexander Motin error = sosetopt(so, &opt); 3427ac58230SAlexander Motin if (error) 3437ac58230SAlexander Motin printf("%s: KEEPINIT setting failed %d\n", __func__, error); 3447ac58230SAlexander Motin 3457ac58230SAlexander Motin opt.sopt_name = TCP_KEEPIDLE; 3467ac58230SAlexander Motin val = 1; 3477ac58230SAlexander Motin error = sosetopt(so, &opt); 3487ac58230SAlexander Motin if (error) 3497ac58230SAlexander Motin printf("%s: KEEPIDLE setting failed %d\n", __func__, error); 3507ac58230SAlexander Motin 3517ac58230SAlexander Motin opt.sopt_name = TCP_KEEPINTVL; 3527ac58230SAlexander Motin val = 1; 3537ac58230SAlexander Motin error = sosetopt(so, &opt); 3547ac58230SAlexander Motin if (error) 3557ac58230SAlexander Motin printf("%s: KEEPINTVL setting failed %d\n", __func__, error); 3567ac58230SAlexander Motin 3577ac58230SAlexander Motin opt.sopt_name = TCP_KEEPCNT; 3587ac58230SAlexander Motin val = 5; 3597ac58230SAlexander Motin error = sosetopt(so, &opt); 3607ac58230SAlexander Motin if (error) 3617ac58230SAlexander Motin printf("%s: KEEPCNT setting failed %d\n", __func__, error); 3627ac58230SAlexander Motin } 3637ac58230SAlexander Motin 3647ac58230SAlexander Motin static int 3657ac58230SAlexander Motin ctl_ha_connect(struct ha_softc *softc) 3667ac58230SAlexander Motin { 3677ac58230SAlexander Motin struct thread *td = curthread; 3680bb9989cSAlexander Motin struct sockaddr_in sa; 3697ac58230SAlexander Motin struct socket *so; 3707ac58230SAlexander Motin int error; 3717ac58230SAlexander Motin 3727ac58230SAlexander Motin /* Create the socket */ 3737ac58230SAlexander Motin error = socreate(PF_INET, &so, SOCK_STREAM, 3747ac58230SAlexander Motin IPPROTO_TCP, td->td_ucred, td); 3757ac58230SAlexander Motin if (error != 0) { 3767ac58230SAlexander Motin printf("%s: socreate() error %d\n", __func__, error); 3777ac58230SAlexander Motin return (error); 3787ac58230SAlexander Motin } 3797ac58230SAlexander Motin softc->ha_so = so; 3807ac58230SAlexander Motin ctl_ha_sock_setup(softc); 3817ac58230SAlexander Motin 3820bb9989cSAlexander Motin memcpy(&sa, &softc->ha_peer_in, sizeof(sa)); 3830bb9989cSAlexander Motin error = soconnect(so, (struct sockaddr *)&sa, td); 384f9b66e4cSAlexander Motin if (error != 0) { 385f9b66e4cSAlexander Motin if (bootverbose) 3867ac58230SAlexander Motin printf("%s: soconnect() error %d\n", __func__, error); 3877ac58230SAlexander Motin goto out; 3887ac58230SAlexander Motin } 3897ac58230SAlexander Motin return (0); 3907ac58230SAlexander Motin 3917ac58230SAlexander Motin out: 3927ac58230SAlexander Motin ctl_ha_close(softc); 3937ac58230SAlexander Motin return (error); 3947ac58230SAlexander Motin } 3957ac58230SAlexander Motin 3967ac58230SAlexander Motin static int 3977ac58230SAlexander Motin ctl_ha_accept(struct ha_softc *softc) 3987ac58230SAlexander Motin { 399779f106aSGleb Smirnoff struct socket *lso, *so; 400cfb1e929SGleb Smirnoff struct sockaddr_in sin = { .sin_len = sizeof(sin) }; 4017ac58230SAlexander Motin int error; 4027ac58230SAlexander Motin 403779f106aSGleb Smirnoff lso = softc->ha_lso; 404779f106aSGleb Smirnoff SOLISTEN_LOCK(lso); 405779f106aSGleb Smirnoff error = solisten_dequeue(lso, &so, 0); 406779f106aSGleb Smirnoff if (error == EWOULDBLOCK) 407779f106aSGleb Smirnoff return (error); 408779f106aSGleb Smirnoff if (error) { 4097ac58230SAlexander Motin printf("%s: socket error %d\n", __func__, error); 4107ac58230SAlexander Motin goto out; 4117ac58230SAlexander Motin } 4127ac58230SAlexander Motin 413cfb1e929SGleb Smirnoff error = soaccept(so, (struct sockaddr *)&sin); 4147ac58230SAlexander Motin if (error != 0) { 4157ac58230SAlexander Motin printf("%s: soaccept() error %d\n", __func__, error); 4167ac58230SAlexander Motin goto out; 4177ac58230SAlexander Motin } 4187ac58230SAlexander Motin softc->ha_so = so; 4197ac58230SAlexander Motin ctl_ha_sock_setup(softc); 4207ac58230SAlexander Motin return (0); 4217ac58230SAlexander Motin 4227ac58230SAlexander Motin out: 4237ac58230SAlexander Motin ctl_ha_lclose(softc); 4247ac58230SAlexander Motin return (error); 4257ac58230SAlexander Motin } 4267ac58230SAlexander Motin 4277ac58230SAlexander Motin static int 4287ac58230SAlexander Motin ctl_ha_listen(struct ha_softc *softc) 4297ac58230SAlexander Motin { 4307ac58230SAlexander Motin struct thread *td = curthread; 4310bb9989cSAlexander Motin struct sockaddr_in sa; 4327ac58230SAlexander Motin struct sockopt opt; 4337ac58230SAlexander Motin int error, val; 4347ac58230SAlexander Motin 4357ac58230SAlexander Motin /* Create the socket */ 4367ac58230SAlexander Motin if (softc->ha_lso == NULL) { 4377ac58230SAlexander Motin error = socreate(PF_INET, &softc->ha_lso, SOCK_STREAM, 4387ac58230SAlexander Motin IPPROTO_TCP, td->td_ucred, td); 4397ac58230SAlexander Motin if (error != 0) { 4407ac58230SAlexander Motin printf("%s: socreate() error %d\n", __func__, error); 4417ac58230SAlexander Motin return (error); 4427ac58230SAlexander Motin } 4437ac58230SAlexander Motin bzero(&opt, sizeof(struct sockopt)); 4447ac58230SAlexander Motin opt.sopt_dir = SOPT_SET; 4457ac58230SAlexander Motin opt.sopt_level = SOL_SOCKET; 4467ac58230SAlexander Motin opt.sopt_name = SO_REUSEADDR; 4477ac58230SAlexander Motin opt.sopt_val = &val; 4487ac58230SAlexander Motin opt.sopt_valsize = sizeof(val); 4497ac58230SAlexander Motin val = 1; 4507ac58230SAlexander Motin error = sosetopt(softc->ha_lso, &opt); 4517ac58230SAlexander Motin if (error) { 4527ac58230SAlexander Motin printf("%s: REUSEADDR setting failed %d\n", 4537ac58230SAlexander Motin __func__, error); 4547ac58230SAlexander Motin } 455a85700a9SAlexander Motin bzero(&opt, sizeof(struct sockopt)); 456a85700a9SAlexander Motin opt.sopt_dir = SOPT_SET; 457a85700a9SAlexander Motin opt.sopt_level = SOL_SOCKET; 458a85700a9SAlexander Motin opt.sopt_name = SO_REUSEPORT; 459a85700a9SAlexander Motin opt.sopt_val = &val; 460a85700a9SAlexander Motin opt.sopt_valsize = sizeof(val); 461a85700a9SAlexander Motin val = 1; 462a85700a9SAlexander Motin error = sosetopt(softc->ha_lso, &opt); 463a85700a9SAlexander Motin if (error) { 464a85700a9SAlexander Motin printf("%s: REUSEPORT setting failed %d\n", 465a85700a9SAlexander Motin __func__, error); 466a85700a9SAlexander Motin } 4677ac58230SAlexander Motin } 4687ac58230SAlexander Motin 4690bb9989cSAlexander Motin memcpy(&sa, &softc->ha_peer_in, sizeof(sa)); 4700bb9989cSAlexander Motin error = sobind(softc->ha_lso, (struct sockaddr *)&sa, td); 4717ac58230SAlexander Motin if (error != 0) { 4727ac58230SAlexander Motin printf("%s: sobind() error %d\n", __func__, error); 4737ac58230SAlexander Motin goto out; 4747ac58230SAlexander Motin } 4757ac58230SAlexander Motin error = solisten(softc->ha_lso, 1, td); 4767ac58230SAlexander Motin if (error != 0) { 4777ac58230SAlexander Motin printf("%s: solisten() error %d\n", __func__, error); 4787ac58230SAlexander Motin goto out; 4797ac58230SAlexander Motin } 480779f106aSGleb Smirnoff SOLISTEN_LOCK(softc->ha_lso); 481779f106aSGleb Smirnoff softc->ha_lso->so_state |= SS_NBIO; 482779f106aSGleb Smirnoff solisten_upcall_set(softc->ha_lso, ctl_ha_lupcall, softc); 483779f106aSGleb Smirnoff SOLISTEN_UNLOCK(softc->ha_lso); 4847ac58230SAlexander Motin return (0); 4857ac58230SAlexander Motin 4867ac58230SAlexander Motin out: 4877ac58230SAlexander Motin ctl_ha_lclose(softc); 4887ac58230SAlexander Motin return (error); 4897ac58230SAlexander Motin } 4907ac58230SAlexander Motin 4917ac58230SAlexander Motin static void 4927ac58230SAlexander Motin ctl_ha_conn_thread(void *arg) 4937ac58230SAlexander Motin { 4947ac58230SAlexander Motin struct ha_softc *softc = arg; 4957ac58230SAlexander Motin int error; 4967ac58230SAlexander Motin 4977ac58230SAlexander Motin while (1) { 49859bb97a9SAlexander Motin if (softc->ha_disconnect || softc->ha_shutdown) { 4997ac58230SAlexander Motin ctl_ha_close(softc); 500a85700a9SAlexander Motin if (softc->ha_disconnect == 2 || softc->ha_shutdown) 5017ac58230SAlexander Motin ctl_ha_lclose(softc); 5027ac58230SAlexander Motin softc->ha_disconnect = 0; 50359bb97a9SAlexander Motin if (softc->ha_shutdown) 50459bb97a9SAlexander Motin break; 5057ac58230SAlexander Motin } else if (softc->ha_so != NULL && 5067ac58230SAlexander Motin (softc->ha_so->so_error || 5077ac58230SAlexander Motin softc->ha_so->so_rcv.sb_state & SBS_CANTRCVMORE)) 5087ac58230SAlexander Motin ctl_ha_close(softc); 5097ac58230SAlexander Motin if (softc->ha_so == NULL) { 5107ac58230SAlexander Motin if (softc->ha_lso != NULL) 5117ac58230SAlexander Motin ctl_ha_accept(softc); 5127ac58230SAlexander Motin else if (softc->ha_listen) 5137ac58230SAlexander Motin ctl_ha_listen(softc); 5147ac58230SAlexander Motin else if (softc->ha_connect) 5157ac58230SAlexander Motin ctl_ha_connect(softc); 5167ac58230SAlexander Motin } 5177ac58230SAlexander Motin if (softc->ha_so != NULL) { 5187ac58230SAlexander Motin if (softc->ha_connected == 0 && 5197ac58230SAlexander Motin softc->ha_so->so_error == 0 && 5207ac58230SAlexander Motin (softc->ha_so->so_state & SS_ISCONNECTING) == 0) { 5217ac58230SAlexander Motin softc->ha_connected = 1; 5227ac58230SAlexander Motin ctl_ha_evt(softc, CTL_HA_CHAN_MAX, 5237ac58230SAlexander Motin CTL_HA_EVT_LINK_CHANGE, 5247ac58230SAlexander Motin CTL_HA_LINK_ONLINE); 5257ac58230SAlexander Motin softc->ha_receiving = 1; 5267ac58230SAlexander Motin error = kproc_kthread_add(ctl_ha_rx_thread, 5277ac58230SAlexander Motin softc, &softc->ha_ctl_softc->ctl_proc, 5287ac58230SAlexander Motin NULL, 0, 0, "ctl", "ha_rx"); 5297ac58230SAlexander Motin if (error != 0) { 5307ac58230SAlexander Motin printf("Error creating CTL HA rx thread!\n"); 5317ac58230SAlexander Motin softc->ha_receiving = 0; 5327ac58230SAlexander Motin softc->ha_disconnect = 1; 5337ac58230SAlexander Motin } 5347ac58230SAlexander Motin } 5357ac58230SAlexander Motin ctl_ha_send(softc); 5367ac58230SAlexander Motin } 5377ac58230SAlexander Motin mtx_lock(&softc->ha_lock); 5387ac58230SAlexander Motin if (softc->ha_so != NULL && 5397ac58230SAlexander Motin (softc->ha_so->so_error || 5407ac58230SAlexander Motin softc->ha_so->so_rcv.sb_state & SBS_CANTRCVMORE)) 5417ac58230SAlexander Motin ; 5427ac58230SAlexander Motin else if (!softc->ha_wakeup) 5437ac58230SAlexander Motin msleep(&softc->ha_wakeup, &softc->ha_lock, 0, "-", hz); 5447ac58230SAlexander Motin softc->ha_wakeup = 0; 5457ac58230SAlexander Motin mtx_unlock(&softc->ha_lock); 5467ac58230SAlexander Motin } 54759bb97a9SAlexander Motin mtx_lock(&softc->ha_lock); 54859bb97a9SAlexander Motin softc->ha_shutdown = 2; 54959bb97a9SAlexander Motin wakeup(&softc->ha_wakeup); 55059bb97a9SAlexander Motin mtx_unlock(&softc->ha_lock); 55159bb97a9SAlexander Motin kthread_exit(); 5527ac58230SAlexander Motin } 5537ac58230SAlexander Motin 5547ac58230SAlexander Motin static int 5557ac58230SAlexander Motin ctl_ha_peer_sysctl(SYSCTL_HANDLER_ARGS) 5567ac58230SAlexander Motin { 5577ac58230SAlexander Motin struct ha_softc *softc = (struct ha_softc *)arg1; 5587ac58230SAlexander Motin struct sockaddr_in *sa; 5597ac58230SAlexander Motin int error, b1, b2, b3, b4, p, num; 560e2c3044bSAlexander Motin char buf[128]; 5617ac58230SAlexander Motin 562e2c3044bSAlexander Motin strlcpy(buf, softc->ha_peer, sizeof(buf)); 563e2c3044bSAlexander Motin error = sysctl_handle_string(oidp, buf, sizeof(buf), req); 564e2c3044bSAlexander Motin if ((error != 0) || (req->newptr == NULL) || 565e2c3044bSAlexander Motin strncmp(buf, softc->ha_peer, sizeof(buf)) == 0) 5667ac58230SAlexander Motin return (error); 5677ac58230SAlexander Motin 5687ac58230SAlexander Motin sa = &softc->ha_peer_in; 5697ac58230SAlexander Motin mtx_lock(&softc->ha_lock); 570e2c3044bSAlexander Motin if ((num = sscanf(buf, "connect %d.%d.%d.%d:%d", 5717ac58230SAlexander Motin &b1, &b2, &b3, &b4, &p)) >= 4) { 5727ac58230SAlexander Motin softc->ha_connect = 1; 5737ac58230SAlexander Motin softc->ha_listen = 0; 574e2c3044bSAlexander Motin } else if ((num = sscanf(buf, "listen %d.%d.%d.%d:%d", 5757ac58230SAlexander Motin &b1, &b2, &b3, &b4, &p)) >= 4) { 5767ac58230SAlexander Motin softc->ha_connect = 0; 5777ac58230SAlexander Motin softc->ha_listen = 1; 5787ac58230SAlexander Motin } else { 5797ac58230SAlexander Motin softc->ha_connect = 0; 5807ac58230SAlexander Motin softc->ha_listen = 0; 581e2c3044bSAlexander Motin if (buf[0] != 0) { 582e2c3044bSAlexander Motin buf[0] = 0; 5837ac58230SAlexander Motin error = EINVAL; 5847ac58230SAlexander Motin } 585e2c3044bSAlexander Motin } 586e2c3044bSAlexander Motin strlcpy(softc->ha_peer, buf, sizeof(softc->ha_peer)); 5877ac58230SAlexander Motin if (softc->ha_connect || softc->ha_listen) { 5887ac58230SAlexander Motin memset(sa, 0, sizeof(*sa)); 5897ac58230SAlexander Motin sa->sin_len = sizeof(struct sockaddr_in); 5907ac58230SAlexander Motin sa->sin_family = AF_INET; 5917ac58230SAlexander Motin sa->sin_port = htons((num >= 5) ? p : 999); 5927ac58230SAlexander Motin sa->sin_addr.s_addr = 5937ac58230SAlexander Motin htonl((b1 << 24) + (b2 << 16) + (b3 << 8) + b4); 5947ac58230SAlexander Motin } 595a85700a9SAlexander Motin softc->ha_disconnect = 2; 5967ac58230SAlexander Motin softc->ha_wakeup = 1; 5977ac58230SAlexander Motin mtx_unlock(&softc->ha_lock); 5987ac58230SAlexander Motin wakeup(&softc->ha_wakeup); 5997ac58230SAlexander Motin return (error); 6007ac58230SAlexander Motin } 6017ac58230SAlexander Motin 6027ac58230SAlexander Motin ctl_ha_status 6037ac58230SAlexander Motin ctl_ha_msg_register(ctl_ha_channel channel, ctl_evt_handler handler) 6047ac58230SAlexander Motin { 6057ac58230SAlexander Motin struct ha_softc *softc = &ha_softc; 6067ac58230SAlexander Motin 6077ac58230SAlexander Motin KASSERT(channel < CTL_HA_CHAN_MAX, 6087ac58230SAlexander Motin ("Wrong CTL HA channel %d", channel)); 6097ac58230SAlexander Motin softc->ha_handler[channel] = handler; 6107ac58230SAlexander Motin return (CTL_HA_STATUS_SUCCESS); 6117ac58230SAlexander Motin } 6127ac58230SAlexander Motin 6137ac58230SAlexander Motin ctl_ha_status 6147ac58230SAlexander Motin ctl_ha_msg_deregister(ctl_ha_channel channel) 6157ac58230SAlexander Motin { 6167ac58230SAlexander Motin struct ha_softc *softc = &ha_softc; 6177ac58230SAlexander Motin 6187ac58230SAlexander Motin KASSERT(channel < CTL_HA_CHAN_MAX, 6197ac58230SAlexander Motin ("Wrong CTL HA channel %d", channel)); 6207ac58230SAlexander Motin softc->ha_handler[channel] = NULL; 6217ac58230SAlexander Motin return (CTL_HA_STATUS_SUCCESS); 6227ac58230SAlexander Motin } 6237ac58230SAlexander Motin 6247ac58230SAlexander Motin /* 6257ac58230SAlexander Motin * Receive a message of the specified size. 6267ac58230SAlexander Motin */ 6277ac58230SAlexander Motin ctl_ha_status 6287ac58230SAlexander Motin ctl_ha_msg_recv(ctl_ha_channel channel, void *addr, size_t len, 6297ac58230SAlexander Motin int wait) 6307ac58230SAlexander Motin { 6317ac58230SAlexander Motin struct ha_softc *softc = &ha_softc; 6327ac58230SAlexander Motin struct uio uio; 6337ac58230SAlexander Motin struct iovec iov; 6347ac58230SAlexander Motin int error, flags; 6357ac58230SAlexander Motin 6367ac58230SAlexander Motin if (!softc->ha_connected) 6377ac58230SAlexander Motin return (CTL_HA_STATUS_DISCONNECT); 6387ac58230SAlexander Motin 6397ac58230SAlexander Motin iov.iov_base = addr; 6407ac58230SAlexander Motin iov.iov_len = len; 6417ac58230SAlexander Motin uio.uio_iov = &iov; 6427ac58230SAlexander Motin uio.uio_iovcnt = 1; 6437ac58230SAlexander Motin uio.uio_rw = UIO_READ; 6447ac58230SAlexander Motin uio.uio_segflg = UIO_SYSSPACE; 6457ac58230SAlexander Motin uio.uio_td = curthread; 6467ac58230SAlexander Motin uio.uio_resid = len; 6477ac58230SAlexander Motin flags = wait ? 0 : MSG_DONTWAIT; 6487ac58230SAlexander Motin error = soreceive(softc->ha_so, NULL, &uio, NULL, NULL, &flags); 6497ac58230SAlexander Motin if (error == 0) 6507ac58230SAlexander Motin return (CTL_HA_STATUS_SUCCESS); 6517ac58230SAlexander Motin 6527ac58230SAlexander Motin /* Consider all errors fatal for HA sanity. */ 6537ac58230SAlexander Motin mtx_lock(&softc->ha_lock); 6547ac58230SAlexander Motin if (softc->ha_connected) { 6557ac58230SAlexander Motin softc->ha_disconnect = 1; 6567ac58230SAlexander Motin softc->ha_wakeup = 1; 6577ac58230SAlexander Motin wakeup(&softc->ha_wakeup); 6587ac58230SAlexander Motin } 6597ac58230SAlexander Motin mtx_unlock(&softc->ha_lock); 6607ac58230SAlexander Motin return (CTL_HA_STATUS_ERROR); 6617ac58230SAlexander Motin } 6627ac58230SAlexander Motin 6637ac58230SAlexander Motin /* 6647ac58230SAlexander Motin * Send a message of the specified size. 6657ac58230SAlexander Motin */ 6667ac58230SAlexander Motin ctl_ha_status 6677ac58230SAlexander Motin ctl_ha_msg_send2(ctl_ha_channel channel, const void *addr, size_t len, 6687ac58230SAlexander Motin const void *addr2, size_t len2, int wait) 6697ac58230SAlexander Motin { 6707ac58230SAlexander Motin struct ha_softc *softc = &ha_softc; 6717ac58230SAlexander Motin struct mbuf *mb, *newmb; 6727ac58230SAlexander Motin struct ha_msg_wire hdr; 6737ac58230SAlexander Motin size_t copylen, off; 6747ac58230SAlexander Motin 6757ac58230SAlexander Motin if (!softc->ha_connected) 6767ac58230SAlexander Motin return (CTL_HA_STATUS_DISCONNECT); 6777ac58230SAlexander Motin 6787ac58230SAlexander Motin newmb = m_getm2(NULL, sizeof(hdr) + len + len2, wait, MT_DATA, 6797ac58230SAlexander Motin M_PKTHDR); 6807ac58230SAlexander Motin if (newmb == NULL) { 6817ac58230SAlexander Motin /* Consider all errors fatal for HA sanity. */ 6827ac58230SAlexander Motin mtx_lock(&softc->ha_lock); 6837ac58230SAlexander Motin if (softc->ha_connected) { 6847ac58230SAlexander Motin softc->ha_disconnect = 1; 6857ac58230SAlexander Motin softc->ha_wakeup = 1; 6867ac58230SAlexander Motin wakeup(&softc->ha_wakeup); 6877ac58230SAlexander Motin } 6887ac58230SAlexander Motin mtx_unlock(&softc->ha_lock); 6897ac58230SAlexander Motin printf("%s: Can't allocate mbuf chain\n", __func__); 6907ac58230SAlexander Motin return (CTL_HA_STATUS_ERROR); 6917ac58230SAlexander Motin } 6927ac58230SAlexander Motin hdr.channel = channel; 6937ac58230SAlexander Motin hdr.length = len + len2; 6947ac58230SAlexander Motin mb = newmb; 6957ac58230SAlexander Motin memcpy(mtodo(mb, 0), &hdr, sizeof(hdr)); 6967ac58230SAlexander Motin mb->m_len += sizeof(hdr); 6977ac58230SAlexander Motin off = 0; 6987ac58230SAlexander Motin for (; mb != NULL && off < len; mb = mb->m_next) { 6997ac58230SAlexander Motin copylen = min(M_TRAILINGSPACE(mb), len - off); 7007ac58230SAlexander Motin memcpy(mtodo(mb, mb->m_len), (const char *)addr + off, copylen); 7017ac58230SAlexander Motin mb->m_len += copylen; 7027ac58230SAlexander Motin off += copylen; 7037ac58230SAlexander Motin if (off == len) 7047ac58230SAlexander Motin break; 7057ac58230SAlexander Motin } 7067ac58230SAlexander Motin KASSERT(off == len, ("%s: off (%zu) != len (%zu)", __func__, 7077ac58230SAlexander Motin off, len)); 7087ac58230SAlexander Motin off = 0; 7097ac58230SAlexander Motin for (; mb != NULL && off < len2; mb = mb->m_next) { 7107ac58230SAlexander Motin copylen = min(M_TRAILINGSPACE(mb), len2 - off); 7117ac58230SAlexander Motin memcpy(mtodo(mb, mb->m_len), (const char *)addr2 + off, copylen); 7127ac58230SAlexander Motin mb->m_len += copylen; 7137ac58230SAlexander Motin off += copylen; 7147ac58230SAlexander Motin } 7157ac58230SAlexander Motin KASSERT(off == len2, ("%s: off (%zu) != len2 (%zu)", __func__, 7167ac58230SAlexander Motin off, len2)); 7177ac58230SAlexander Motin newmb->m_pkthdr.len = sizeof(hdr) + len + len2; 7187ac58230SAlexander Motin 7197ac58230SAlexander Motin mtx_lock(&softc->ha_lock); 7207ac58230SAlexander Motin if (!softc->ha_connected) { 7217ac58230SAlexander Motin mtx_unlock(&softc->ha_lock); 7227ac58230SAlexander Motin m_freem(newmb); 7237ac58230SAlexander Motin return (CTL_HA_STATUS_DISCONNECT); 7247ac58230SAlexander Motin } 7257ac58230SAlexander Motin mbufq_enqueue(&softc->ha_sendq, newmb); 7267ac58230SAlexander Motin softc->ha_wakeup = 1; 7277ac58230SAlexander Motin mtx_unlock(&softc->ha_lock); 7287ac58230SAlexander Motin wakeup(&softc->ha_wakeup); 7297ac58230SAlexander Motin return (CTL_HA_STATUS_SUCCESS); 7307ac58230SAlexander Motin } 7317ac58230SAlexander Motin 7327ac58230SAlexander Motin ctl_ha_status 7337ac58230SAlexander Motin ctl_ha_msg_send(ctl_ha_channel channel, const void *addr, size_t len, 7347ac58230SAlexander Motin int wait) 7357ac58230SAlexander Motin { 7367ac58230SAlexander Motin 7377ac58230SAlexander Motin return (ctl_ha_msg_send2(channel, addr, len, NULL, 0, wait)); 7387ac58230SAlexander Motin } 7397ac58230SAlexander Motin 740a85700a9SAlexander Motin ctl_ha_status 741a85700a9SAlexander Motin ctl_ha_msg_abort(ctl_ha_channel channel) 742a85700a9SAlexander Motin { 743a85700a9SAlexander Motin struct ha_softc *softc = &ha_softc; 744a85700a9SAlexander Motin 745a85700a9SAlexander Motin mtx_lock(&softc->ha_lock); 746a85700a9SAlexander Motin softc->ha_disconnect = 1; 747a85700a9SAlexander Motin softc->ha_wakeup = 1; 748a85700a9SAlexander Motin mtx_unlock(&softc->ha_lock); 749a85700a9SAlexander Motin wakeup(&softc->ha_wakeup); 750a85700a9SAlexander Motin return (CTL_HA_STATUS_SUCCESS); 751a85700a9SAlexander Motin } 752a85700a9SAlexander Motin 7537ac58230SAlexander Motin /* 7547ac58230SAlexander Motin * Allocate a data transfer request structure. 7557ac58230SAlexander Motin */ 7567ac58230SAlexander Motin struct ctl_ha_dt_req * 7577ac58230SAlexander Motin ctl_dt_req_alloc(void) 7587ac58230SAlexander Motin { 7597ac58230SAlexander Motin 7607ac58230SAlexander Motin return (malloc(sizeof(struct ctl_ha_dt_req), M_CTL, M_WAITOK | M_ZERO)); 7617ac58230SAlexander Motin } 7627ac58230SAlexander Motin 7637ac58230SAlexander Motin /* 7647ac58230SAlexander Motin * Free a data transfer request structure. 7657ac58230SAlexander Motin */ 7667ac58230SAlexander Motin void 7677ac58230SAlexander Motin ctl_dt_req_free(struct ctl_ha_dt_req *req) 7687ac58230SAlexander Motin { 7697ac58230SAlexander Motin 7707ac58230SAlexander Motin free(req, M_CTL); 7717ac58230SAlexander Motin } 7727ac58230SAlexander Motin 7737ac58230SAlexander Motin /* 7747ac58230SAlexander Motin * Issue a DMA request for a single buffer. 7757ac58230SAlexander Motin */ 7767ac58230SAlexander Motin ctl_ha_status 7777ac58230SAlexander Motin ctl_dt_single(struct ctl_ha_dt_req *req) 7787ac58230SAlexander Motin { 7797ac58230SAlexander Motin struct ha_softc *softc = &ha_softc; 7807ac58230SAlexander Motin struct ha_dt_msg_wire wire_dt; 7817ac58230SAlexander Motin ctl_ha_status status; 7827ac58230SAlexander Motin 7837ac58230SAlexander Motin wire_dt.command = req->command; 7847ac58230SAlexander Motin wire_dt.size = req->size; 7857ac58230SAlexander Motin wire_dt.local = req->local; 7867ac58230SAlexander Motin wire_dt.remote = req->remote; 7877ac58230SAlexander Motin if (req->command == CTL_HA_DT_CMD_READ && req->callback != NULL) { 7887ac58230SAlexander Motin mtx_lock(&softc->ha_lock); 7897ac58230SAlexander Motin TAILQ_INSERT_TAIL(&softc->ha_dts, req, links); 7907ac58230SAlexander Motin mtx_unlock(&softc->ha_lock); 7917ac58230SAlexander Motin ctl_ha_msg_send(CTL_HA_CHAN_DATA, &wire_dt, sizeof(wire_dt), 7927ac58230SAlexander Motin M_WAITOK); 7937ac58230SAlexander Motin return (CTL_HA_STATUS_WAIT); 7947ac58230SAlexander Motin } 7957ac58230SAlexander Motin if (req->command == CTL_HA_DT_CMD_READ) { 7967ac58230SAlexander Motin status = ctl_ha_msg_send(CTL_HA_CHAN_DATA, &wire_dt, 7977ac58230SAlexander Motin sizeof(wire_dt), M_WAITOK); 7987ac58230SAlexander Motin } else { 7997ac58230SAlexander Motin status = ctl_ha_msg_send2(CTL_HA_CHAN_DATA, &wire_dt, 8007ac58230SAlexander Motin sizeof(wire_dt), req->local, req->size, M_WAITOK); 8017ac58230SAlexander Motin } 8027ac58230SAlexander Motin return (status); 8037ac58230SAlexander Motin } 8047ac58230SAlexander Motin 8057ac58230SAlexander Motin static void 8067ac58230SAlexander Motin ctl_dt_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param) 8077ac58230SAlexander Motin { 8087ac58230SAlexander Motin struct ha_softc *softc = &ha_softc; 8097ac58230SAlexander Motin struct ctl_ha_dt_req *req; 8107ac58230SAlexander Motin ctl_ha_status isc_status; 8117ac58230SAlexander Motin 8127ac58230SAlexander Motin if (event == CTL_HA_EVT_MSG_RECV) { 8137ac58230SAlexander Motin struct ha_dt_msg_wire wire_dt; 8147ac58230SAlexander Motin uint8_t *tmp; 8157ac58230SAlexander Motin int size; 8167ac58230SAlexander Motin 8177ac58230SAlexander Motin size = min(sizeof(wire_dt), param); 8187ac58230SAlexander Motin isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_DATA, &wire_dt, 8197ac58230SAlexander Motin size, M_WAITOK); 8207ac58230SAlexander Motin if (isc_status != CTL_HA_STATUS_SUCCESS) { 8217ac58230SAlexander Motin printf("%s: Error receiving message: %d\n", 8227ac58230SAlexander Motin __func__, isc_status); 8237ac58230SAlexander Motin return; 8247ac58230SAlexander Motin } 8257ac58230SAlexander Motin 8267ac58230SAlexander Motin if (wire_dt.command == CTL_HA_DT_CMD_READ) { 8277ac58230SAlexander Motin wire_dt.command = CTL_HA_DT_CMD_WRITE; 8287ac58230SAlexander Motin tmp = wire_dt.local; 8297ac58230SAlexander Motin wire_dt.local = wire_dt.remote; 8307ac58230SAlexander Motin wire_dt.remote = tmp; 8317ac58230SAlexander Motin ctl_ha_msg_send2(CTL_HA_CHAN_DATA, &wire_dt, 8327ac58230SAlexander Motin sizeof(wire_dt), wire_dt.local, wire_dt.size, 8337ac58230SAlexander Motin M_WAITOK); 8347ac58230SAlexander Motin } else if (wire_dt.command == CTL_HA_DT_CMD_WRITE) { 8357ac58230SAlexander Motin isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_DATA, 8367ac58230SAlexander Motin wire_dt.remote, wire_dt.size, M_WAITOK); 8377ac58230SAlexander Motin mtx_lock(&softc->ha_lock); 8387ac58230SAlexander Motin TAILQ_FOREACH(req, &softc->ha_dts, links) { 8397ac58230SAlexander Motin if (req->local == wire_dt.remote) { 8407ac58230SAlexander Motin TAILQ_REMOVE(&softc->ha_dts, req, links); 8417ac58230SAlexander Motin break; 8427ac58230SAlexander Motin } 8437ac58230SAlexander Motin } 8447ac58230SAlexander Motin mtx_unlock(&softc->ha_lock); 8457ac58230SAlexander Motin if (req) { 8467ac58230SAlexander Motin req->ret = isc_status; 8477ac58230SAlexander Motin req->callback(req); 8487ac58230SAlexander Motin } 8497ac58230SAlexander Motin } 8507ac58230SAlexander Motin } else if (event == CTL_HA_EVT_LINK_CHANGE) { 8517ac58230SAlexander Motin CTL_DEBUG_PRINT(("%s: Link state change to %d\n", __func__, 8527ac58230SAlexander Motin param)); 8537ac58230SAlexander Motin if (param != CTL_HA_LINK_ONLINE) { 8547ac58230SAlexander Motin mtx_lock(&softc->ha_lock); 8557ac58230SAlexander Motin while ((req = TAILQ_FIRST(&softc->ha_dts)) != NULL) { 8567ac58230SAlexander Motin TAILQ_REMOVE(&softc->ha_dts, req, links); 8577ac58230SAlexander Motin mtx_unlock(&softc->ha_lock); 8587ac58230SAlexander Motin req->ret = CTL_HA_STATUS_DISCONNECT; 8597ac58230SAlexander Motin req->callback(req); 8607ac58230SAlexander Motin mtx_lock(&softc->ha_lock); 8617ac58230SAlexander Motin } 8627ac58230SAlexander Motin mtx_unlock(&softc->ha_lock); 8637ac58230SAlexander Motin } 8647ac58230SAlexander Motin } else { 8657ac58230SAlexander Motin printf("%s: Unknown event %d\n", __func__, event); 8667ac58230SAlexander Motin } 8677ac58230SAlexander Motin } 8687ac58230SAlexander Motin 8697ac58230SAlexander Motin ctl_ha_status 8707ac58230SAlexander Motin ctl_ha_msg_init(struct ctl_softc *ctl_softc) 8717ac58230SAlexander Motin { 8727ac58230SAlexander Motin struct ha_softc *softc = &ha_softc; 8737ac58230SAlexander Motin int error; 8747ac58230SAlexander Motin 8757ac58230SAlexander Motin softc->ha_ctl_softc = ctl_softc; 8767ac58230SAlexander Motin mtx_init(&softc->ha_lock, "CTL HA mutex", NULL, MTX_DEF); 8777ac58230SAlexander Motin mbufq_init(&softc->ha_sendq, INT_MAX); 8787ac58230SAlexander Motin TAILQ_INIT(&softc->ha_dts); 8797ac58230SAlexander Motin error = kproc_kthread_add(ctl_ha_conn_thread, softc, 8807ac58230SAlexander Motin &ctl_softc->ctl_proc, NULL, 0, 0, "ctl", "ha_tx"); 8817ac58230SAlexander Motin if (error != 0) { 8827ac58230SAlexander Motin printf("error creating CTL HA connection thread!\n"); 8837ac58230SAlexander Motin mtx_destroy(&softc->ha_lock); 8847ac58230SAlexander Motin return (CTL_HA_STATUS_ERROR); 8857ac58230SAlexander Motin } 88659bb97a9SAlexander Motin softc->ha_shutdown_eh = EVENTHANDLER_REGISTER(shutdown_pre_sync, 88759bb97a9SAlexander Motin ctl_ha_msg_shutdown, ctl_softc, SHUTDOWN_PRI_FIRST); 8887ac58230SAlexander Motin SYSCTL_ADD_PROC(&ctl_softc->sysctl_ctx, 8897ac58230SAlexander Motin SYSCTL_CHILDREN(ctl_softc->sysctl_tree), 8907029da5cSPawel Biernacki OID_AUTO, "ha_peer", 891303477d3SAlexander Motin CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, 8927ac58230SAlexander Motin softc, 0, ctl_ha_peer_sysctl, "A", "HA peer connection method"); 8937ac58230SAlexander Motin 8947ac58230SAlexander Motin if (ctl_ha_msg_register(CTL_HA_CHAN_DATA, ctl_dt_event_handler) 8957ac58230SAlexander Motin != CTL_HA_STATUS_SUCCESS) { 8967ac58230SAlexander Motin printf("%s: ctl_ha_msg_register failed.\n", __func__); 8977ac58230SAlexander Motin } 8987ac58230SAlexander Motin 8997ac58230SAlexander Motin return (CTL_HA_STATUS_SUCCESS); 9007ac58230SAlexander Motin }; 9017ac58230SAlexander Motin 90259bb97a9SAlexander Motin void 9037ac58230SAlexander Motin ctl_ha_msg_shutdown(struct ctl_softc *ctl_softc) 9047ac58230SAlexander Motin { 9057ac58230SAlexander Motin struct ha_softc *softc = &ha_softc; 9067ac58230SAlexander Motin 9079d61fd08SMitchell Horne if (SCHEDULER_STOPPED()) 9089d61fd08SMitchell Horne return; 9099d61fd08SMitchell Horne 91059bb97a9SAlexander Motin /* Disconnect and shutdown threads. */ 91159bb97a9SAlexander Motin mtx_lock(&softc->ha_lock); 91259bb97a9SAlexander Motin if (softc->ha_shutdown < 2) { 91359bb97a9SAlexander Motin softc->ha_shutdown = 1; 91459bb97a9SAlexander Motin softc->ha_wakeup = 1; 91559bb97a9SAlexander Motin wakeup(&softc->ha_wakeup); 9169d61fd08SMitchell Horne while (softc->ha_shutdown < 2) { 91759bb97a9SAlexander Motin msleep(&softc->ha_wakeup, &softc->ha_lock, 0, 91859bb97a9SAlexander Motin "shutdown", hz); 9197ac58230SAlexander Motin } 92059bb97a9SAlexander Motin } 92159bb97a9SAlexander Motin mtx_unlock(&softc->ha_lock); 92259bb97a9SAlexander Motin }; 92359bb97a9SAlexander Motin 92459bb97a9SAlexander Motin ctl_ha_status 92559bb97a9SAlexander Motin ctl_ha_msg_destroy(struct ctl_softc *ctl_softc) 92659bb97a9SAlexander Motin { 92759bb97a9SAlexander Motin struct ha_softc *softc = &ha_softc; 92859bb97a9SAlexander Motin 92959bb97a9SAlexander Motin if (softc->ha_shutdown_eh != NULL) { 93059bb97a9SAlexander Motin EVENTHANDLER_DEREGISTER(shutdown_pre_sync, 93159bb97a9SAlexander Motin softc->ha_shutdown_eh); 93259bb97a9SAlexander Motin softc->ha_shutdown_eh = NULL; 93359bb97a9SAlexander Motin } 93459bb97a9SAlexander Motin 93559bb97a9SAlexander Motin ctl_ha_msg_shutdown(ctl_softc); /* Just in case. */ 93659bb97a9SAlexander Motin 93759bb97a9SAlexander Motin if (ctl_ha_msg_deregister(CTL_HA_CHAN_DATA) != CTL_HA_STATUS_SUCCESS) 93859bb97a9SAlexander Motin printf("%s: ctl_ha_msg_deregister failed.\n", __func__); 9397ac58230SAlexander Motin 9407ac58230SAlexander Motin mtx_destroy(&softc->ha_lock); 9417ac58230SAlexander Motin return (CTL_HA_STATUS_SUCCESS); 9427ac58230SAlexander Motin }; 943