17ac58230SAlexander Motin /*- 2f24882ecSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 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/cdefs.h> 307ac58230SAlexander Motin __FBSDID("$FreeBSD$"); 317ac58230SAlexander Motin 327ac58230SAlexander Motin #include <sys/param.h> 33e2e050c8SConrad Meyer #include <sys/condvar.h> 34e2e050c8SConrad Meyer #include <sys/conf.h> 35e2e050c8SConrad Meyer #include <sys/eventhandler.h> 367ac58230SAlexander Motin #include <sys/kernel.h> 377ac58230SAlexander Motin #include <sys/kthread.h> 387ac58230SAlexander Motin #include <sys/limits.h> 397ac58230SAlexander Motin #include <sys/lock.h> 407ac58230SAlexander Motin #include <sys/malloc.h> 417ac58230SAlexander Motin #include <sys/mbuf.h> 42e2e050c8SConrad Meyer #include <sys/module.h> 43e2e050c8SConrad Meyer #include <sys/mutex.h> 447ac58230SAlexander Motin #include <sys/proc.h> 457ac58230SAlexander Motin #include <sys/queue.h> 467ac58230SAlexander Motin #include <sys/socket.h> 477ac58230SAlexander Motin #include <sys/socketvar.h> 48e2e050c8SConrad Meyer #include <sys/sysctl.h> 49e2e050c8SConrad Meyer #include <sys/systm.h> 507ac58230SAlexander Motin #include <sys/uio.h> 517ac58230SAlexander Motin #include <netinet/in.h> 527ac58230SAlexander Motin #include <netinet/tcp.h> 537ac58230SAlexander Motin #include <vm/uma.h> 547ac58230SAlexander Motin 557ac58230SAlexander Motin #include <cam/cam.h> 567ac58230SAlexander Motin #include <cam/scsi/scsi_all.h> 577ac58230SAlexander Motin #include <cam/scsi/scsi_da.h> 587ac58230SAlexander Motin #include <cam/ctl/ctl_io.h> 597ac58230SAlexander Motin #include <cam/ctl/ctl.h> 607ac58230SAlexander Motin #include <cam/ctl/ctl_frontend.h> 617ac58230SAlexander Motin #include <cam/ctl/ctl_util.h> 627ac58230SAlexander Motin #include <cam/ctl/ctl_backend.h> 637ac58230SAlexander Motin #include <cam/ctl/ctl_ioctl.h> 647ac58230SAlexander Motin #include <cam/ctl/ctl_ha.h> 657ac58230SAlexander Motin #include <cam/ctl/ctl_private.h> 667ac58230SAlexander Motin #include <cam/ctl/ctl_debug.h> 677ac58230SAlexander Motin #include <cam/ctl/ctl_error.h> 687ac58230SAlexander Motin 697ac58230SAlexander Motin 707ac58230SAlexander Motin struct ha_msg_wire { 717ac58230SAlexander Motin uint32_t channel; 727ac58230SAlexander Motin uint32_t length; 737ac58230SAlexander Motin }; 747ac58230SAlexander Motin 757ac58230SAlexander Motin struct ha_dt_msg_wire { 767ac58230SAlexander Motin ctl_ha_dt_cmd command; 777ac58230SAlexander Motin uint32_t size; 787ac58230SAlexander Motin uint8_t *local; 797ac58230SAlexander Motin uint8_t *remote; 807ac58230SAlexander Motin }; 817ac58230SAlexander Motin 827ac58230SAlexander Motin struct ha_softc { 837ac58230SAlexander Motin struct ctl_softc *ha_ctl_softc; 847ac58230SAlexander Motin ctl_evt_handler ha_handler[CTL_HA_CHAN_MAX]; 857ac58230SAlexander Motin char ha_peer[128]; 867ac58230SAlexander Motin struct sockaddr_in ha_peer_in; 877ac58230SAlexander Motin struct socket *ha_lso; 887ac58230SAlexander Motin struct socket *ha_so; 897ac58230SAlexander Motin struct mbufq ha_sendq; 907ac58230SAlexander Motin struct mbuf *ha_sending; 917ac58230SAlexander Motin struct mtx ha_lock; 927ac58230SAlexander Motin int ha_connect; 937ac58230SAlexander Motin int ha_listen; 947ac58230SAlexander Motin int ha_connected; 957ac58230SAlexander Motin int ha_receiving; 967ac58230SAlexander Motin int ha_wakeup; 977ac58230SAlexander Motin int ha_disconnect; 9859bb97a9SAlexander Motin int ha_shutdown; 9959bb97a9SAlexander Motin eventhandler_tag ha_shutdown_eh; 1007ac58230SAlexander Motin TAILQ_HEAD(, ctl_ha_dt_req) ha_dts; 1017ac58230SAlexander Motin } ha_softc; 1027ac58230SAlexander Motin 1037ac58230SAlexander Motin static void 1047ac58230SAlexander Motin ctl_ha_conn_wake(struct ha_softc *softc) 1057ac58230SAlexander Motin { 1067ac58230SAlexander Motin 1077ac58230SAlexander Motin mtx_lock(&softc->ha_lock); 1087ac58230SAlexander Motin softc->ha_wakeup = 1; 1097ac58230SAlexander Motin mtx_unlock(&softc->ha_lock); 1107ac58230SAlexander Motin wakeup(&softc->ha_wakeup); 1117ac58230SAlexander Motin } 1127ac58230SAlexander Motin 1137ac58230SAlexander Motin static int 1147ac58230SAlexander Motin ctl_ha_lupcall(struct socket *so, void *arg, int waitflag) 1157ac58230SAlexander Motin { 1167ac58230SAlexander Motin struct ha_softc *softc = arg; 1177ac58230SAlexander Motin 1187ac58230SAlexander Motin ctl_ha_conn_wake(softc); 1197ac58230SAlexander Motin return (SU_OK); 1207ac58230SAlexander Motin } 1217ac58230SAlexander Motin 1227ac58230SAlexander Motin static int 1237ac58230SAlexander Motin ctl_ha_rupcall(struct socket *so, void *arg, int waitflag) 1247ac58230SAlexander Motin { 1257ac58230SAlexander Motin struct ha_softc *softc = arg; 1267ac58230SAlexander Motin 1277ac58230SAlexander Motin wakeup(&softc->ha_receiving); 1287ac58230SAlexander Motin return (SU_OK); 1297ac58230SAlexander Motin } 1307ac58230SAlexander Motin 1317ac58230SAlexander Motin static int 1327ac58230SAlexander Motin ctl_ha_supcall(struct socket *so, void *arg, int waitflag) 1337ac58230SAlexander Motin { 1347ac58230SAlexander Motin struct ha_softc *softc = arg; 1357ac58230SAlexander Motin 1367ac58230SAlexander Motin ctl_ha_conn_wake(softc); 1377ac58230SAlexander Motin return (SU_OK); 1387ac58230SAlexander Motin } 1397ac58230SAlexander Motin 1407ac58230SAlexander Motin static void 1417ac58230SAlexander Motin ctl_ha_evt(struct ha_softc *softc, ctl_ha_channel ch, ctl_ha_event evt, 1427ac58230SAlexander Motin int param) 1437ac58230SAlexander Motin { 1447ac58230SAlexander Motin int i; 1457ac58230SAlexander Motin 1467ac58230SAlexander Motin if (ch < CTL_HA_CHAN_MAX) { 1477ac58230SAlexander Motin if (softc->ha_handler[ch]) 1487ac58230SAlexander Motin softc->ha_handler[ch](ch, evt, param); 1497ac58230SAlexander Motin return; 1507ac58230SAlexander Motin } 1517ac58230SAlexander Motin for (i = 0; i < CTL_HA_CHAN_MAX; i++) { 1527ac58230SAlexander Motin if (softc->ha_handler[i]) 1537ac58230SAlexander Motin softc->ha_handler[i](i, evt, param); 1547ac58230SAlexander Motin } 1557ac58230SAlexander Motin } 1567ac58230SAlexander Motin 1577ac58230SAlexander Motin static void 1587ac58230SAlexander Motin ctl_ha_close(struct ha_softc *softc) 1597ac58230SAlexander Motin { 1607ac58230SAlexander Motin struct socket *so = softc->ha_so; 1617ac58230SAlexander Motin int report = 0; 1627ac58230SAlexander Motin 1637ac58230SAlexander Motin if (softc->ha_connected || softc->ha_disconnect) { 1647ac58230SAlexander Motin softc->ha_connected = 0; 1657ac58230SAlexander Motin mbufq_drain(&softc->ha_sendq); 1667ac58230SAlexander Motin m_freem(softc->ha_sending); 1677ac58230SAlexander Motin softc->ha_sending = NULL; 1687ac58230SAlexander Motin report = 1; 1697ac58230SAlexander Motin } 1707ac58230SAlexander Motin if (so) { 1717ac58230SAlexander Motin SOCKBUF_LOCK(&so->so_rcv); 1727ac58230SAlexander Motin soupcall_clear(so, SO_RCV); 1737ac58230SAlexander Motin while (softc->ha_receiving) { 1747ac58230SAlexander Motin wakeup(&softc->ha_receiving); 1757ac58230SAlexander Motin msleep(&softc->ha_receiving, SOCKBUF_MTX(&so->so_rcv), 1767ac58230SAlexander Motin 0, "ha_rx exit", 0); 1777ac58230SAlexander Motin } 1787ac58230SAlexander Motin SOCKBUF_UNLOCK(&so->so_rcv); 1797ac58230SAlexander Motin SOCKBUF_LOCK(&so->so_snd); 1807ac58230SAlexander Motin soupcall_clear(so, SO_SND); 1817ac58230SAlexander Motin SOCKBUF_UNLOCK(&so->so_snd); 1827ac58230SAlexander Motin softc->ha_so = NULL; 1837ac58230SAlexander Motin if (softc->ha_connect) 1847ac58230SAlexander Motin pause("reconnect", hz / 2); 1857ac58230SAlexander Motin soclose(so); 1867ac58230SAlexander Motin } 1877ac58230SAlexander Motin if (report) { 1887ac58230SAlexander Motin ctl_ha_evt(softc, CTL_HA_CHAN_MAX, CTL_HA_EVT_LINK_CHANGE, 1897ac58230SAlexander Motin (softc->ha_connect || softc->ha_listen) ? 1907ac58230SAlexander Motin CTL_HA_LINK_UNKNOWN : CTL_HA_LINK_OFFLINE); 1917ac58230SAlexander Motin } 1927ac58230SAlexander Motin } 1937ac58230SAlexander Motin 1947ac58230SAlexander Motin static void 1957ac58230SAlexander Motin ctl_ha_lclose(struct ha_softc *softc) 1967ac58230SAlexander Motin { 1977ac58230SAlexander Motin 1987ac58230SAlexander Motin if (softc->ha_lso) { 199*3873b149SAlexander Motin if (SOLISTENING(softc->ha_lso)) { 200*3873b149SAlexander Motin SOLISTEN_LOCK(softc->ha_lso); 201*3873b149SAlexander Motin solisten_upcall_set(softc->ha_lso, NULL, NULL); 202*3873b149SAlexander Motin SOLISTEN_UNLOCK(softc->ha_lso); 203*3873b149SAlexander Motin } 2047ac58230SAlexander Motin soclose(softc->ha_lso); 2057ac58230SAlexander Motin softc->ha_lso = NULL; 2067ac58230SAlexander Motin } 2077ac58230SAlexander Motin } 2087ac58230SAlexander Motin 2097ac58230SAlexander Motin static void 2107ac58230SAlexander Motin ctl_ha_rx_thread(void *arg) 2117ac58230SAlexander Motin { 2127ac58230SAlexander Motin struct ha_softc *softc = arg; 2137ac58230SAlexander Motin struct socket *so = softc->ha_so; 2147ac58230SAlexander Motin struct ha_msg_wire wire_hdr; 2157ac58230SAlexander Motin struct uio uio; 2167ac58230SAlexander Motin struct iovec iov; 2177ac58230SAlexander Motin int error, flags, next; 2187ac58230SAlexander Motin 2197ac58230SAlexander Motin bzero(&wire_hdr, sizeof(wire_hdr)); 2207ac58230SAlexander Motin while (1) { 2217ac58230SAlexander Motin if (wire_hdr.length > 0) 2227ac58230SAlexander Motin next = wire_hdr.length; 2237ac58230SAlexander Motin else 2247ac58230SAlexander Motin next = sizeof(wire_hdr); 2257ac58230SAlexander Motin SOCKBUF_LOCK(&so->so_rcv); 226a85700a9SAlexander Motin while (sbavail(&so->so_rcv) < next || softc->ha_disconnect) { 227a85700a9SAlexander Motin if (softc->ha_connected == 0 || softc->ha_disconnect || 228a85700a9SAlexander Motin so->so_error || 2297ac58230SAlexander Motin (so->so_rcv.sb_state & SBS_CANTRCVMORE)) { 2307ac58230SAlexander Motin goto errout; 2317ac58230SAlexander Motin } 2327ac58230SAlexander Motin so->so_rcv.sb_lowat = next; 2337ac58230SAlexander Motin msleep(&softc->ha_receiving, SOCKBUF_MTX(&so->so_rcv), 2347ac58230SAlexander Motin 0, "-", 0); 2357ac58230SAlexander Motin } 2367ac58230SAlexander Motin SOCKBUF_UNLOCK(&so->so_rcv); 2377ac58230SAlexander Motin 2387ac58230SAlexander Motin if (wire_hdr.length == 0) { 2397ac58230SAlexander Motin iov.iov_base = &wire_hdr; 2407ac58230SAlexander Motin iov.iov_len = sizeof(wire_hdr); 2417ac58230SAlexander Motin uio.uio_iov = &iov; 2427ac58230SAlexander Motin uio.uio_iovcnt = 1; 2437ac58230SAlexander Motin uio.uio_rw = UIO_READ; 2447ac58230SAlexander Motin uio.uio_segflg = UIO_SYSSPACE; 2457ac58230SAlexander Motin uio.uio_td = curthread; 2467ac58230SAlexander Motin uio.uio_resid = sizeof(wire_hdr); 2477ac58230SAlexander Motin flags = MSG_DONTWAIT; 2487ac58230SAlexander Motin error = soreceive(softc->ha_so, NULL, &uio, NULL, 2497ac58230SAlexander Motin NULL, &flags); 2507ac58230SAlexander Motin if (error != 0) { 2517ac58230SAlexander Motin printf("%s: header receive error %d\n", 2527ac58230SAlexander Motin __func__, error); 2537ac58230SAlexander Motin SOCKBUF_LOCK(&so->so_rcv); 2547ac58230SAlexander Motin goto errout; 2557ac58230SAlexander Motin } 2567ac58230SAlexander Motin } else { 2577ac58230SAlexander Motin ctl_ha_evt(softc, wire_hdr.channel, 2587ac58230SAlexander Motin CTL_HA_EVT_MSG_RECV, wire_hdr.length); 2597ac58230SAlexander Motin wire_hdr.length = 0; 2607ac58230SAlexander Motin } 2617ac58230SAlexander Motin } 2627ac58230SAlexander Motin 2637ac58230SAlexander Motin errout: 2647ac58230SAlexander Motin softc->ha_receiving = 0; 2657ac58230SAlexander Motin wakeup(&softc->ha_receiving); 2667ac58230SAlexander Motin SOCKBUF_UNLOCK(&so->so_rcv); 2677ac58230SAlexander Motin ctl_ha_conn_wake(softc); 2687ac58230SAlexander Motin kthread_exit(); 2697ac58230SAlexander Motin } 2707ac58230SAlexander Motin 2717ac58230SAlexander Motin static void 2727ac58230SAlexander Motin ctl_ha_send(struct ha_softc *softc) 2737ac58230SAlexander Motin { 2747ac58230SAlexander Motin struct socket *so = softc->ha_so; 2757ac58230SAlexander Motin int error; 2767ac58230SAlexander Motin 2777ac58230SAlexander Motin while (1) { 2787ac58230SAlexander Motin if (softc->ha_sending == NULL) { 2797ac58230SAlexander Motin mtx_lock(&softc->ha_lock); 2807ac58230SAlexander Motin softc->ha_sending = mbufq_dequeue(&softc->ha_sendq); 2817ac58230SAlexander Motin mtx_unlock(&softc->ha_lock); 2827ac58230SAlexander Motin if (softc->ha_sending == NULL) { 2837ac58230SAlexander Motin so->so_snd.sb_lowat = so->so_snd.sb_hiwat + 1; 2847ac58230SAlexander Motin break; 2857ac58230SAlexander Motin } 2867ac58230SAlexander Motin } 2877ac58230SAlexander Motin SOCKBUF_LOCK(&so->so_snd); 2887ac58230SAlexander Motin if (sbspace(&so->so_snd) < softc->ha_sending->m_pkthdr.len) { 2897ac58230SAlexander Motin so->so_snd.sb_lowat = softc->ha_sending->m_pkthdr.len; 2907ac58230SAlexander Motin SOCKBUF_UNLOCK(&so->so_snd); 2917ac58230SAlexander Motin break; 2927ac58230SAlexander Motin } 2937ac58230SAlexander Motin SOCKBUF_UNLOCK(&so->so_snd); 2947ac58230SAlexander Motin error = sosend(softc->ha_so, NULL, NULL, softc->ha_sending, 2957ac58230SAlexander Motin NULL, MSG_DONTWAIT, curthread); 2967ac58230SAlexander Motin softc->ha_sending = NULL; 2977ac58230SAlexander Motin if (error != 0) { 2987ac58230SAlexander Motin printf("%s: sosend() error %d\n", __func__, error); 2997ac58230SAlexander Motin return; 3007ac58230SAlexander Motin } 30174b8d63dSPedro F. Giffuni } 3027ac58230SAlexander Motin } 3037ac58230SAlexander Motin 3047ac58230SAlexander Motin static void 3057ac58230SAlexander Motin ctl_ha_sock_setup(struct ha_softc *softc) 3067ac58230SAlexander Motin { 3077ac58230SAlexander Motin struct sockopt opt; 3087ac58230SAlexander Motin struct socket *so = softc->ha_so; 3097ac58230SAlexander Motin int error, val; 3107ac58230SAlexander Motin 3117ac58230SAlexander Motin val = 1024 * 1024; 3127ac58230SAlexander Motin error = soreserve(so, val, val); 3137ac58230SAlexander Motin if (error) 3147ac58230SAlexander Motin printf("%s: soreserve failed %d\n", __func__, error); 3157ac58230SAlexander Motin 3167ac58230SAlexander Motin SOCKBUF_LOCK(&so->so_rcv); 3177ac58230SAlexander Motin so->so_rcv.sb_lowat = sizeof(struct ha_msg_wire); 3187ac58230SAlexander Motin soupcall_set(so, SO_RCV, ctl_ha_rupcall, softc); 3197ac58230SAlexander Motin SOCKBUF_UNLOCK(&so->so_rcv); 3207ac58230SAlexander Motin SOCKBUF_LOCK(&so->so_snd); 3217ac58230SAlexander Motin so->so_snd.sb_lowat = sizeof(struct ha_msg_wire); 3227ac58230SAlexander Motin soupcall_set(so, SO_SND, ctl_ha_supcall, softc); 3237ac58230SAlexander Motin SOCKBUF_UNLOCK(&so->so_snd); 3247ac58230SAlexander Motin 3257ac58230SAlexander Motin bzero(&opt, sizeof(struct sockopt)); 3267ac58230SAlexander Motin opt.sopt_dir = SOPT_SET; 3277ac58230SAlexander Motin opt.sopt_level = SOL_SOCKET; 3287ac58230SAlexander Motin opt.sopt_name = SO_KEEPALIVE; 3297ac58230SAlexander Motin opt.sopt_val = &val; 3307ac58230SAlexander Motin opt.sopt_valsize = sizeof(val); 3317ac58230SAlexander Motin val = 1; 3327ac58230SAlexander Motin error = sosetopt(so, &opt); 3337ac58230SAlexander Motin if (error) 3347ac58230SAlexander Motin printf("%s: KEEPALIVE setting failed %d\n", __func__, error); 3357ac58230SAlexander Motin 3367ac58230SAlexander Motin opt.sopt_level = IPPROTO_TCP; 3377ac58230SAlexander Motin opt.sopt_name = TCP_NODELAY; 3387ac58230SAlexander Motin val = 1; 3397ac58230SAlexander Motin error = sosetopt(so, &opt); 3407ac58230SAlexander Motin if (error) 3417ac58230SAlexander Motin printf("%s: NODELAY setting failed %d\n", __func__, error); 3427ac58230SAlexander Motin 3437ac58230SAlexander Motin opt.sopt_name = TCP_KEEPINIT; 3447ac58230SAlexander Motin val = 3; 3457ac58230SAlexander Motin error = sosetopt(so, &opt); 3467ac58230SAlexander Motin if (error) 3477ac58230SAlexander Motin printf("%s: KEEPINIT setting failed %d\n", __func__, error); 3487ac58230SAlexander Motin 3497ac58230SAlexander Motin opt.sopt_name = TCP_KEEPIDLE; 3507ac58230SAlexander Motin val = 1; 3517ac58230SAlexander Motin error = sosetopt(so, &opt); 3527ac58230SAlexander Motin if (error) 3537ac58230SAlexander Motin printf("%s: KEEPIDLE setting failed %d\n", __func__, error); 3547ac58230SAlexander Motin 3557ac58230SAlexander Motin opt.sopt_name = TCP_KEEPINTVL; 3567ac58230SAlexander Motin val = 1; 3577ac58230SAlexander Motin error = sosetopt(so, &opt); 3587ac58230SAlexander Motin if (error) 3597ac58230SAlexander Motin printf("%s: KEEPINTVL setting failed %d\n", __func__, error); 3607ac58230SAlexander Motin 3617ac58230SAlexander Motin opt.sopt_name = TCP_KEEPCNT; 3627ac58230SAlexander Motin val = 5; 3637ac58230SAlexander Motin error = sosetopt(so, &opt); 3647ac58230SAlexander Motin if (error) 3657ac58230SAlexander Motin printf("%s: KEEPCNT setting failed %d\n", __func__, error); 3667ac58230SAlexander Motin } 3677ac58230SAlexander Motin 3687ac58230SAlexander Motin static int 3697ac58230SAlexander Motin ctl_ha_connect(struct ha_softc *softc) 3707ac58230SAlexander Motin { 3717ac58230SAlexander Motin struct thread *td = curthread; 3720bb9989cSAlexander Motin struct sockaddr_in sa; 3737ac58230SAlexander Motin struct socket *so; 3747ac58230SAlexander Motin int error; 3757ac58230SAlexander Motin 3767ac58230SAlexander Motin /* Create the socket */ 3777ac58230SAlexander Motin error = socreate(PF_INET, &so, SOCK_STREAM, 3787ac58230SAlexander Motin IPPROTO_TCP, td->td_ucred, td); 3797ac58230SAlexander Motin if (error != 0) { 3807ac58230SAlexander Motin printf("%s: socreate() error %d\n", __func__, error); 3817ac58230SAlexander Motin return (error); 3827ac58230SAlexander Motin } 3837ac58230SAlexander Motin softc->ha_so = so; 3847ac58230SAlexander Motin ctl_ha_sock_setup(softc); 3857ac58230SAlexander Motin 3860bb9989cSAlexander Motin memcpy(&sa, &softc->ha_peer_in, sizeof(sa)); 3870bb9989cSAlexander Motin error = soconnect(so, (struct sockaddr *)&sa, td); 388f9b66e4cSAlexander Motin if (error != 0) { 389f9b66e4cSAlexander Motin if (bootverbose) 3907ac58230SAlexander Motin printf("%s: soconnect() error %d\n", __func__, error); 3917ac58230SAlexander Motin goto out; 3927ac58230SAlexander Motin } 3937ac58230SAlexander Motin return (0); 3947ac58230SAlexander Motin 3957ac58230SAlexander Motin out: 3967ac58230SAlexander Motin ctl_ha_close(softc); 3977ac58230SAlexander Motin return (error); 3987ac58230SAlexander Motin } 3997ac58230SAlexander Motin 4007ac58230SAlexander Motin static int 4017ac58230SAlexander Motin ctl_ha_accept(struct ha_softc *softc) 4027ac58230SAlexander Motin { 403779f106aSGleb Smirnoff struct socket *lso, *so; 4047ac58230SAlexander Motin struct sockaddr *sap; 4057ac58230SAlexander Motin int error; 4067ac58230SAlexander Motin 407779f106aSGleb Smirnoff lso = softc->ha_lso; 408779f106aSGleb Smirnoff SOLISTEN_LOCK(lso); 409779f106aSGleb Smirnoff error = solisten_dequeue(lso, &so, 0); 410779f106aSGleb Smirnoff if (error == EWOULDBLOCK) 411779f106aSGleb Smirnoff return (error); 412779f106aSGleb Smirnoff if (error) { 4137ac58230SAlexander Motin printf("%s: socket error %d\n", __func__, error); 4147ac58230SAlexander Motin goto out; 4157ac58230SAlexander Motin } 4167ac58230SAlexander Motin 4177ac58230SAlexander Motin sap = NULL; 4187ac58230SAlexander Motin error = soaccept(so, &sap); 4197ac58230SAlexander Motin if (error != 0) { 4207ac58230SAlexander Motin printf("%s: soaccept() error %d\n", __func__, error); 4217ac58230SAlexander Motin if (sap != NULL) 4227ac58230SAlexander Motin free(sap, M_SONAME); 4237ac58230SAlexander Motin goto out; 4247ac58230SAlexander Motin } 4257ac58230SAlexander Motin if (sap != NULL) 4267ac58230SAlexander Motin free(sap, M_SONAME); 4277ac58230SAlexander Motin softc->ha_so = so; 4287ac58230SAlexander Motin ctl_ha_sock_setup(softc); 4297ac58230SAlexander Motin return (0); 4307ac58230SAlexander Motin 4317ac58230SAlexander Motin out: 4327ac58230SAlexander Motin ctl_ha_lclose(softc); 4337ac58230SAlexander Motin return (error); 4347ac58230SAlexander Motin } 4357ac58230SAlexander Motin 4367ac58230SAlexander Motin static int 4377ac58230SAlexander Motin ctl_ha_listen(struct ha_softc *softc) 4387ac58230SAlexander Motin { 4397ac58230SAlexander Motin struct thread *td = curthread; 4400bb9989cSAlexander Motin struct sockaddr_in sa; 4417ac58230SAlexander Motin struct sockopt opt; 4427ac58230SAlexander Motin int error, val; 4437ac58230SAlexander Motin 4447ac58230SAlexander Motin /* Create the socket */ 4457ac58230SAlexander Motin if (softc->ha_lso == NULL) { 4467ac58230SAlexander Motin error = socreate(PF_INET, &softc->ha_lso, SOCK_STREAM, 4477ac58230SAlexander Motin IPPROTO_TCP, td->td_ucred, td); 4487ac58230SAlexander Motin if (error != 0) { 4497ac58230SAlexander Motin printf("%s: socreate() error %d\n", __func__, error); 4507ac58230SAlexander Motin return (error); 4517ac58230SAlexander Motin } 4527ac58230SAlexander Motin bzero(&opt, sizeof(struct sockopt)); 4537ac58230SAlexander Motin opt.sopt_dir = SOPT_SET; 4547ac58230SAlexander Motin opt.sopt_level = SOL_SOCKET; 4557ac58230SAlexander Motin opt.sopt_name = SO_REUSEADDR; 4567ac58230SAlexander Motin opt.sopt_val = &val; 4577ac58230SAlexander Motin opt.sopt_valsize = sizeof(val); 4587ac58230SAlexander Motin val = 1; 4597ac58230SAlexander Motin error = sosetopt(softc->ha_lso, &opt); 4607ac58230SAlexander Motin if (error) { 4617ac58230SAlexander Motin printf("%s: REUSEADDR setting failed %d\n", 4627ac58230SAlexander Motin __func__, error); 4637ac58230SAlexander Motin } 464a85700a9SAlexander Motin bzero(&opt, sizeof(struct sockopt)); 465a85700a9SAlexander Motin opt.sopt_dir = SOPT_SET; 466a85700a9SAlexander Motin opt.sopt_level = SOL_SOCKET; 467a85700a9SAlexander Motin opt.sopt_name = SO_REUSEPORT; 468a85700a9SAlexander Motin opt.sopt_val = &val; 469a85700a9SAlexander Motin opt.sopt_valsize = sizeof(val); 470a85700a9SAlexander Motin val = 1; 471a85700a9SAlexander Motin error = sosetopt(softc->ha_lso, &opt); 472a85700a9SAlexander Motin if (error) { 473a85700a9SAlexander Motin printf("%s: REUSEPORT setting failed %d\n", 474a85700a9SAlexander Motin __func__, error); 475a85700a9SAlexander Motin } 4767ac58230SAlexander Motin } 4777ac58230SAlexander Motin 4780bb9989cSAlexander Motin memcpy(&sa, &softc->ha_peer_in, sizeof(sa)); 4790bb9989cSAlexander Motin error = sobind(softc->ha_lso, (struct sockaddr *)&sa, td); 4807ac58230SAlexander Motin if (error != 0) { 4817ac58230SAlexander Motin printf("%s: sobind() error %d\n", __func__, error); 4827ac58230SAlexander Motin goto out; 4837ac58230SAlexander Motin } 4847ac58230SAlexander Motin error = solisten(softc->ha_lso, 1, td); 4857ac58230SAlexander Motin if (error != 0) { 4867ac58230SAlexander Motin printf("%s: solisten() error %d\n", __func__, error); 4877ac58230SAlexander Motin goto out; 4887ac58230SAlexander Motin } 489779f106aSGleb Smirnoff SOLISTEN_LOCK(softc->ha_lso); 490779f106aSGleb Smirnoff softc->ha_lso->so_state |= SS_NBIO; 491779f106aSGleb Smirnoff solisten_upcall_set(softc->ha_lso, ctl_ha_lupcall, softc); 492779f106aSGleb Smirnoff SOLISTEN_UNLOCK(softc->ha_lso); 4937ac58230SAlexander Motin return (0); 4947ac58230SAlexander Motin 4957ac58230SAlexander Motin out: 4967ac58230SAlexander Motin ctl_ha_lclose(softc); 4977ac58230SAlexander Motin return (error); 4987ac58230SAlexander Motin } 4997ac58230SAlexander Motin 5007ac58230SAlexander Motin static void 5017ac58230SAlexander Motin ctl_ha_conn_thread(void *arg) 5027ac58230SAlexander Motin { 5037ac58230SAlexander Motin struct ha_softc *softc = arg; 5047ac58230SAlexander Motin int error; 5057ac58230SAlexander Motin 5067ac58230SAlexander Motin while (1) { 50759bb97a9SAlexander Motin if (softc->ha_disconnect || softc->ha_shutdown) { 5087ac58230SAlexander Motin ctl_ha_close(softc); 509a85700a9SAlexander Motin if (softc->ha_disconnect == 2 || softc->ha_shutdown) 5107ac58230SAlexander Motin ctl_ha_lclose(softc); 5117ac58230SAlexander Motin softc->ha_disconnect = 0; 51259bb97a9SAlexander Motin if (softc->ha_shutdown) 51359bb97a9SAlexander Motin break; 5147ac58230SAlexander Motin } else if (softc->ha_so != NULL && 5157ac58230SAlexander Motin (softc->ha_so->so_error || 5167ac58230SAlexander Motin softc->ha_so->so_rcv.sb_state & SBS_CANTRCVMORE)) 5177ac58230SAlexander Motin ctl_ha_close(softc); 5187ac58230SAlexander Motin if (softc->ha_so == NULL) { 5197ac58230SAlexander Motin if (softc->ha_lso != NULL) 5207ac58230SAlexander Motin ctl_ha_accept(softc); 5217ac58230SAlexander Motin else if (softc->ha_listen) 5227ac58230SAlexander Motin ctl_ha_listen(softc); 5237ac58230SAlexander Motin else if (softc->ha_connect) 5247ac58230SAlexander Motin ctl_ha_connect(softc); 5257ac58230SAlexander Motin } 5267ac58230SAlexander Motin if (softc->ha_so != NULL) { 5277ac58230SAlexander Motin if (softc->ha_connected == 0 && 5287ac58230SAlexander Motin softc->ha_so->so_error == 0 && 5297ac58230SAlexander Motin (softc->ha_so->so_state & SS_ISCONNECTING) == 0) { 5307ac58230SAlexander Motin softc->ha_connected = 1; 5317ac58230SAlexander Motin ctl_ha_evt(softc, CTL_HA_CHAN_MAX, 5327ac58230SAlexander Motin CTL_HA_EVT_LINK_CHANGE, 5337ac58230SAlexander Motin CTL_HA_LINK_ONLINE); 5347ac58230SAlexander Motin softc->ha_receiving = 1; 5357ac58230SAlexander Motin error = kproc_kthread_add(ctl_ha_rx_thread, 5367ac58230SAlexander Motin softc, &softc->ha_ctl_softc->ctl_proc, 5377ac58230SAlexander Motin NULL, 0, 0, "ctl", "ha_rx"); 5387ac58230SAlexander Motin if (error != 0) { 5397ac58230SAlexander Motin printf("Error creating CTL HA rx thread!\n"); 5407ac58230SAlexander Motin softc->ha_receiving = 0; 5417ac58230SAlexander Motin softc->ha_disconnect = 1; 5427ac58230SAlexander Motin } 5437ac58230SAlexander Motin } 5447ac58230SAlexander Motin ctl_ha_send(softc); 5457ac58230SAlexander Motin } 5467ac58230SAlexander Motin mtx_lock(&softc->ha_lock); 5477ac58230SAlexander Motin if (softc->ha_so != NULL && 5487ac58230SAlexander Motin (softc->ha_so->so_error || 5497ac58230SAlexander Motin softc->ha_so->so_rcv.sb_state & SBS_CANTRCVMORE)) 5507ac58230SAlexander Motin ; 5517ac58230SAlexander Motin else if (!softc->ha_wakeup) 5527ac58230SAlexander Motin msleep(&softc->ha_wakeup, &softc->ha_lock, 0, "-", hz); 5537ac58230SAlexander Motin softc->ha_wakeup = 0; 5547ac58230SAlexander Motin mtx_unlock(&softc->ha_lock); 5557ac58230SAlexander Motin } 55659bb97a9SAlexander Motin mtx_lock(&softc->ha_lock); 55759bb97a9SAlexander Motin softc->ha_shutdown = 2; 55859bb97a9SAlexander Motin wakeup(&softc->ha_wakeup); 55959bb97a9SAlexander Motin mtx_unlock(&softc->ha_lock); 56059bb97a9SAlexander Motin kthread_exit(); 5617ac58230SAlexander Motin } 5627ac58230SAlexander Motin 5637ac58230SAlexander Motin static int 5647ac58230SAlexander Motin ctl_ha_peer_sysctl(SYSCTL_HANDLER_ARGS) 5657ac58230SAlexander Motin { 5667ac58230SAlexander Motin struct ha_softc *softc = (struct ha_softc *)arg1; 5677ac58230SAlexander Motin struct sockaddr_in *sa; 5687ac58230SAlexander Motin int error, b1, b2, b3, b4, p, num; 569e2c3044bSAlexander Motin char buf[128]; 5707ac58230SAlexander Motin 571e2c3044bSAlexander Motin strlcpy(buf, softc->ha_peer, sizeof(buf)); 572e2c3044bSAlexander Motin error = sysctl_handle_string(oidp, buf, sizeof(buf), req); 573e2c3044bSAlexander Motin if ((error != 0) || (req->newptr == NULL) || 574e2c3044bSAlexander Motin strncmp(buf, softc->ha_peer, sizeof(buf)) == 0) 5757ac58230SAlexander Motin return (error); 5767ac58230SAlexander Motin 5777ac58230SAlexander Motin sa = &softc->ha_peer_in; 5787ac58230SAlexander Motin mtx_lock(&softc->ha_lock); 579e2c3044bSAlexander Motin if ((num = sscanf(buf, "connect %d.%d.%d.%d:%d", 5807ac58230SAlexander Motin &b1, &b2, &b3, &b4, &p)) >= 4) { 5817ac58230SAlexander Motin softc->ha_connect = 1; 5827ac58230SAlexander Motin softc->ha_listen = 0; 583e2c3044bSAlexander Motin } else if ((num = sscanf(buf, "listen %d.%d.%d.%d:%d", 5847ac58230SAlexander Motin &b1, &b2, &b3, &b4, &p)) >= 4) { 5857ac58230SAlexander Motin softc->ha_connect = 0; 5867ac58230SAlexander Motin softc->ha_listen = 1; 5877ac58230SAlexander Motin } else { 5887ac58230SAlexander Motin softc->ha_connect = 0; 5897ac58230SAlexander Motin softc->ha_listen = 0; 590e2c3044bSAlexander Motin if (buf[0] != 0) { 591e2c3044bSAlexander Motin buf[0] = 0; 5927ac58230SAlexander Motin error = EINVAL; 5937ac58230SAlexander Motin } 594e2c3044bSAlexander Motin } 595e2c3044bSAlexander Motin strlcpy(softc->ha_peer, buf, sizeof(softc->ha_peer)); 5967ac58230SAlexander Motin if (softc->ha_connect || softc->ha_listen) { 5977ac58230SAlexander Motin memset(sa, 0, sizeof(*sa)); 5987ac58230SAlexander Motin sa->sin_len = sizeof(struct sockaddr_in); 5997ac58230SAlexander Motin sa->sin_family = AF_INET; 6007ac58230SAlexander Motin sa->sin_port = htons((num >= 5) ? p : 999); 6017ac58230SAlexander Motin sa->sin_addr.s_addr = 6027ac58230SAlexander Motin htonl((b1 << 24) + (b2 << 16) + (b3 << 8) + b4); 6037ac58230SAlexander Motin } 604a85700a9SAlexander Motin softc->ha_disconnect = 2; 6057ac58230SAlexander Motin softc->ha_wakeup = 1; 6067ac58230SAlexander Motin mtx_unlock(&softc->ha_lock); 6077ac58230SAlexander Motin wakeup(&softc->ha_wakeup); 6087ac58230SAlexander Motin return (error); 6097ac58230SAlexander Motin } 6107ac58230SAlexander Motin 6117ac58230SAlexander Motin ctl_ha_status 6127ac58230SAlexander Motin ctl_ha_msg_register(ctl_ha_channel channel, ctl_evt_handler handler) 6137ac58230SAlexander Motin { 6147ac58230SAlexander Motin struct ha_softc *softc = &ha_softc; 6157ac58230SAlexander Motin 6167ac58230SAlexander Motin KASSERT(channel < CTL_HA_CHAN_MAX, 6177ac58230SAlexander Motin ("Wrong CTL HA channel %d", channel)); 6187ac58230SAlexander Motin softc->ha_handler[channel] = handler; 6197ac58230SAlexander Motin return (CTL_HA_STATUS_SUCCESS); 6207ac58230SAlexander Motin } 6217ac58230SAlexander Motin 6227ac58230SAlexander Motin ctl_ha_status 6237ac58230SAlexander Motin ctl_ha_msg_deregister(ctl_ha_channel channel) 6247ac58230SAlexander Motin { 6257ac58230SAlexander Motin struct ha_softc *softc = &ha_softc; 6267ac58230SAlexander Motin 6277ac58230SAlexander Motin KASSERT(channel < CTL_HA_CHAN_MAX, 6287ac58230SAlexander Motin ("Wrong CTL HA channel %d", channel)); 6297ac58230SAlexander Motin softc->ha_handler[channel] = NULL; 6307ac58230SAlexander Motin return (CTL_HA_STATUS_SUCCESS); 6317ac58230SAlexander Motin } 6327ac58230SAlexander Motin 6337ac58230SAlexander Motin /* 6347ac58230SAlexander Motin * Receive a message of the specified size. 6357ac58230SAlexander Motin */ 6367ac58230SAlexander Motin ctl_ha_status 6377ac58230SAlexander Motin ctl_ha_msg_recv(ctl_ha_channel channel, void *addr, size_t len, 6387ac58230SAlexander Motin int wait) 6397ac58230SAlexander Motin { 6407ac58230SAlexander Motin struct ha_softc *softc = &ha_softc; 6417ac58230SAlexander Motin struct uio uio; 6427ac58230SAlexander Motin struct iovec iov; 6437ac58230SAlexander Motin int error, flags; 6447ac58230SAlexander Motin 6457ac58230SAlexander Motin if (!softc->ha_connected) 6467ac58230SAlexander Motin return (CTL_HA_STATUS_DISCONNECT); 6477ac58230SAlexander Motin 6487ac58230SAlexander Motin iov.iov_base = addr; 6497ac58230SAlexander Motin iov.iov_len = len; 6507ac58230SAlexander Motin uio.uio_iov = &iov; 6517ac58230SAlexander Motin uio.uio_iovcnt = 1; 6527ac58230SAlexander Motin uio.uio_rw = UIO_READ; 6537ac58230SAlexander Motin uio.uio_segflg = UIO_SYSSPACE; 6547ac58230SAlexander Motin uio.uio_td = curthread; 6557ac58230SAlexander Motin uio.uio_resid = len; 6567ac58230SAlexander Motin flags = wait ? 0 : MSG_DONTWAIT; 6577ac58230SAlexander Motin error = soreceive(softc->ha_so, NULL, &uio, NULL, NULL, &flags); 6587ac58230SAlexander Motin if (error == 0) 6597ac58230SAlexander Motin return (CTL_HA_STATUS_SUCCESS); 6607ac58230SAlexander Motin 6617ac58230SAlexander Motin /* Consider all errors fatal for HA sanity. */ 6627ac58230SAlexander Motin mtx_lock(&softc->ha_lock); 6637ac58230SAlexander Motin if (softc->ha_connected) { 6647ac58230SAlexander Motin softc->ha_disconnect = 1; 6657ac58230SAlexander Motin softc->ha_wakeup = 1; 6667ac58230SAlexander Motin wakeup(&softc->ha_wakeup); 6677ac58230SAlexander Motin } 6687ac58230SAlexander Motin mtx_unlock(&softc->ha_lock); 6697ac58230SAlexander Motin return (CTL_HA_STATUS_ERROR); 6707ac58230SAlexander Motin } 6717ac58230SAlexander Motin 6727ac58230SAlexander Motin /* 6737ac58230SAlexander Motin * Send a message of the specified size. 6747ac58230SAlexander Motin */ 6757ac58230SAlexander Motin ctl_ha_status 6767ac58230SAlexander Motin ctl_ha_msg_send2(ctl_ha_channel channel, const void *addr, size_t len, 6777ac58230SAlexander Motin const void *addr2, size_t len2, int wait) 6787ac58230SAlexander Motin { 6797ac58230SAlexander Motin struct ha_softc *softc = &ha_softc; 6807ac58230SAlexander Motin struct mbuf *mb, *newmb; 6817ac58230SAlexander Motin struct ha_msg_wire hdr; 6827ac58230SAlexander Motin size_t copylen, off; 6837ac58230SAlexander Motin 6847ac58230SAlexander Motin if (!softc->ha_connected) 6857ac58230SAlexander Motin return (CTL_HA_STATUS_DISCONNECT); 6867ac58230SAlexander Motin 6877ac58230SAlexander Motin newmb = m_getm2(NULL, sizeof(hdr) + len + len2, wait, MT_DATA, 6887ac58230SAlexander Motin M_PKTHDR); 6897ac58230SAlexander Motin if (newmb == NULL) { 6907ac58230SAlexander Motin /* Consider all errors fatal for HA sanity. */ 6917ac58230SAlexander Motin mtx_lock(&softc->ha_lock); 6927ac58230SAlexander Motin if (softc->ha_connected) { 6937ac58230SAlexander Motin softc->ha_disconnect = 1; 6947ac58230SAlexander Motin softc->ha_wakeup = 1; 6957ac58230SAlexander Motin wakeup(&softc->ha_wakeup); 6967ac58230SAlexander Motin } 6977ac58230SAlexander Motin mtx_unlock(&softc->ha_lock); 6987ac58230SAlexander Motin printf("%s: Can't allocate mbuf chain\n", __func__); 6997ac58230SAlexander Motin return (CTL_HA_STATUS_ERROR); 7007ac58230SAlexander Motin } 7017ac58230SAlexander Motin hdr.channel = channel; 7027ac58230SAlexander Motin hdr.length = len + len2; 7037ac58230SAlexander Motin mb = newmb; 7047ac58230SAlexander Motin memcpy(mtodo(mb, 0), &hdr, sizeof(hdr)); 7057ac58230SAlexander Motin mb->m_len += sizeof(hdr); 7067ac58230SAlexander Motin off = 0; 7077ac58230SAlexander Motin for (; mb != NULL && off < len; mb = mb->m_next) { 7087ac58230SAlexander Motin copylen = min(M_TRAILINGSPACE(mb), len - off); 7097ac58230SAlexander Motin memcpy(mtodo(mb, mb->m_len), (const char *)addr + off, copylen); 7107ac58230SAlexander Motin mb->m_len += copylen; 7117ac58230SAlexander Motin off += copylen; 7127ac58230SAlexander Motin if (off == len) 7137ac58230SAlexander Motin break; 7147ac58230SAlexander Motin } 7157ac58230SAlexander Motin KASSERT(off == len, ("%s: off (%zu) != len (%zu)", __func__, 7167ac58230SAlexander Motin off, len)); 7177ac58230SAlexander Motin off = 0; 7187ac58230SAlexander Motin for (; mb != NULL && off < len2; mb = mb->m_next) { 7197ac58230SAlexander Motin copylen = min(M_TRAILINGSPACE(mb), len2 - off); 7207ac58230SAlexander Motin memcpy(mtodo(mb, mb->m_len), (const char *)addr2 + off, copylen); 7217ac58230SAlexander Motin mb->m_len += copylen; 7227ac58230SAlexander Motin off += copylen; 7237ac58230SAlexander Motin } 7247ac58230SAlexander Motin KASSERT(off == len2, ("%s: off (%zu) != len2 (%zu)", __func__, 7257ac58230SAlexander Motin off, len2)); 7267ac58230SAlexander Motin newmb->m_pkthdr.len = sizeof(hdr) + len + len2; 7277ac58230SAlexander Motin 7287ac58230SAlexander Motin mtx_lock(&softc->ha_lock); 7297ac58230SAlexander Motin if (!softc->ha_connected) { 7307ac58230SAlexander Motin mtx_unlock(&softc->ha_lock); 7317ac58230SAlexander Motin m_freem(newmb); 7327ac58230SAlexander Motin return (CTL_HA_STATUS_DISCONNECT); 7337ac58230SAlexander Motin } 7347ac58230SAlexander Motin mbufq_enqueue(&softc->ha_sendq, newmb); 7357ac58230SAlexander Motin softc->ha_wakeup = 1; 7367ac58230SAlexander Motin mtx_unlock(&softc->ha_lock); 7377ac58230SAlexander Motin wakeup(&softc->ha_wakeup); 7387ac58230SAlexander Motin return (CTL_HA_STATUS_SUCCESS); 7397ac58230SAlexander Motin } 7407ac58230SAlexander Motin 7417ac58230SAlexander Motin ctl_ha_status 7427ac58230SAlexander Motin ctl_ha_msg_send(ctl_ha_channel channel, const void *addr, size_t len, 7437ac58230SAlexander Motin int wait) 7447ac58230SAlexander Motin { 7457ac58230SAlexander Motin 7467ac58230SAlexander Motin return (ctl_ha_msg_send2(channel, addr, len, NULL, 0, wait)); 7477ac58230SAlexander Motin } 7487ac58230SAlexander Motin 749a85700a9SAlexander Motin ctl_ha_status 750a85700a9SAlexander Motin ctl_ha_msg_abort(ctl_ha_channel channel) 751a85700a9SAlexander Motin { 752a85700a9SAlexander Motin struct ha_softc *softc = &ha_softc; 753a85700a9SAlexander Motin 754a85700a9SAlexander Motin mtx_lock(&softc->ha_lock); 755a85700a9SAlexander Motin softc->ha_disconnect = 1; 756a85700a9SAlexander Motin softc->ha_wakeup = 1; 757a85700a9SAlexander Motin mtx_unlock(&softc->ha_lock); 758a85700a9SAlexander Motin wakeup(&softc->ha_wakeup); 759a85700a9SAlexander Motin return (CTL_HA_STATUS_SUCCESS); 760a85700a9SAlexander Motin } 761a85700a9SAlexander Motin 7627ac58230SAlexander Motin /* 7637ac58230SAlexander Motin * Allocate a data transfer request structure. 7647ac58230SAlexander Motin */ 7657ac58230SAlexander Motin struct ctl_ha_dt_req * 7667ac58230SAlexander Motin ctl_dt_req_alloc(void) 7677ac58230SAlexander Motin { 7687ac58230SAlexander Motin 7697ac58230SAlexander Motin return (malloc(sizeof(struct ctl_ha_dt_req), M_CTL, M_WAITOK | M_ZERO)); 7707ac58230SAlexander Motin } 7717ac58230SAlexander Motin 7727ac58230SAlexander Motin /* 7737ac58230SAlexander Motin * Free a data transfer request structure. 7747ac58230SAlexander Motin */ 7757ac58230SAlexander Motin void 7767ac58230SAlexander Motin ctl_dt_req_free(struct ctl_ha_dt_req *req) 7777ac58230SAlexander Motin { 7787ac58230SAlexander Motin 7797ac58230SAlexander Motin free(req, M_CTL); 7807ac58230SAlexander Motin } 7817ac58230SAlexander Motin 7827ac58230SAlexander Motin /* 7837ac58230SAlexander Motin * Issue a DMA request for a single buffer. 7847ac58230SAlexander Motin */ 7857ac58230SAlexander Motin ctl_ha_status 7867ac58230SAlexander Motin ctl_dt_single(struct ctl_ha_dt_req *req) 7877ac58230SAlexander Motin { 7887ac58230SAlexander Motin struct ha_softc *softc = &ha_softc; 7897ac58230SAlexander Motin struct ha_dt_msg_wire wire_dt; 7907ac58230SAlexander Motin ctl_ha_status status; 7917ac58230SAlexander Motin 7927ac58230SAlexander Motin wire_dt.command = req->command; 7937ac58230SAlexander Motin wire_dt.size = req->size; 7947ac58230SAlexander Motin wire_dt.local = req->local; 7957ac58230SAlexander Motin wire_dt.remote = req->remote; 7967ac58230SAlexander Motin if (req->command == CTL_HA_DT_CMD_READ && req->callback != NULL) { 7977ac58230SAlexander Motin mtx_lock(&softc->ha_lock); 7987ac58230SAlexander Motin TAILQ_INSERT_TAIL(&softc->ha_dts, req, links); 7997ac58230SAlexander Motin mtx_unlock(&softc->ha_lock); 8007ac58230SAlexander Motin ctl_ha_msg_send(CTL_HA_CHAN_DATA, &wire_dt, sizeof(wire_dt), 8017ac58230SAlexander Motin M_WAITOK); 8027ac58230SAlexander Motin return (CTL_HA_STATUS_WAIT); 8037ac58230SAlexander Motin } 8047ac58230SAlexander Motin if (req->command == CTL_HA_DT_CMD_READ) { 8057ac58230SAlexander Motin status = ctl_ha_msg_send(CTL_HA_CHAN_DATA, &wire_dt, 8067ac58230SAlexander Motin sizeof(wire_dt), M_WAITOK); 8077ac58230SAlexander Motin } else { 8087ac58230SAlexander Motin status = ctl_ha_msg_send2(CTL_HA_CHAN_DATA, &wire_dt, 8097ac58230SAlexander Motin sizeof(wire_dt), req->local, req->size, M_WAITOK); 8107ac58230SAlexander Motin } 8117ac58230SAlexander Motin return (status); 8127ac58230SAlexander Motin } 8137ac58230SAlexander Motin 8147ac58230SAlexander Motin static void 8157ac58230SAlexander Motin ctl_dt_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param) 8167ac58230SAlexander Motin { 8177ac58230SAlexander Motin struct ha_softc *softc = &ha_softc; 8187ac58230SAlexander Motin struct ctl_ha_dt_req *req; 8197ac58230SAlexander Motin ctl_ha_status isc_status; 8207ac58230SAlexander Motin 8217ac58230SAlexander Motin if (event == CTL_HA_EVT_MSG_RECV) { 8227ac58230SAlexander Motin struct ha_dt_msg_wire wire_dt; 8237ac58230SAlexander Motin uint8_t *tmp; 8247ac58230SAlexander Motin int size; 8257ac58230SAlexander Motin 8267ac58230SAlexander Motin size = min(sizeof(wire_dt), param); 8277ac58230SAlexander Motin isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_DATA, &wire_dt, 8287ac58230SAlexander Motin size, M_WAITOK); 8297ac58230SAlexander Motin if (isc_status != CTL_HA_STATUS_SUCCESS) { 8307ac58230SAlexander Motin printf("%s: Error receiving message: %d\n", 8317ac58230SAlexander Motin __func__, isc_status); 8327ac58230SAlexander Motin return; 8337ac58230SAlexander Motin } 8347ac58230SAlexander Motin 8357ac58230SAlexander Motin if (wire_dt.command == CTL_HA_DT_CMD_READ) { 8367ac58230SAlexander Motin wire_dt.command = CTL_HA_DT_CMD_WRITE; 8377ac58230SAlexander Motin tmp = wire_dt.local; 8387ac58230SAlexander Motin wire_dt.local = wire_dt.remote; 8397ac58230SAlexander Motin wire_dt.remote = tmp; 8407ac58230SAlexander Motin ctl_ha_msg_send2(CTL_HA_CHAN_DATA, &wire_dt, 8417ac58230SAlexander Motin sizeof(wire_dt), wire_dt.local, wire_dt.size, 8427ac58230SAlexander Motin M_WAITOK); 8437ac58230SAlexander Motin } else if (wire_dt.command == CTL_HA_DT_CMD_WRITE) { 8447ac58230SAlexander Motin isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_DATA, 8457ac58230SAlexander Motin wire_dt.remote, wire_dt.size, M_WAITOK); 8467ac58230SAlexander Motin mtx_lock(&softc->ha_lock); 8477ac58230SAlexander Motin TAILQ_FOREACH(req, &softc->ha_dts, links) { 8487ac58230SAlexander Motin if (req->local == wire_dt.remote) { 8497ac58230SAlexander Motin TAILQ_REMOVE(&softc->ha_dts, req, links); 8507ac58230SAlexander Motin break; 8517ac58230SAlexander Motin } 8527ac58230SAlexander Motin } 8537ac58230SAlexander Motin mtx_unlock(&softc->ha_lock); 8547ac58230SAlexander Motin if (req) { 8557ac58230SAlexander Motin req->ret = isc_status; 8567ac58230SAlexander Motin req->callback(req); 8577ac58230SAlexander Motin } 8587ac58230SAlexander Motin } 8597ac58230SAlexander Motin } else if (event == CTL_HA_EVT_LINK_CHANGE) { 8607ac58230SAlexander Motin CTL_DEBUG_PRINT(("%s: Link state change to %d\n", __func__, 8617ac58230SAlexander Motin param)); 8627ac58230SAlexander Motin if (param != CTL_HA_LINK_ONLINE) { 8637ac58230SAlexander Motin mtx_lock(&softc->ha_lock); 8647ac58230SAlexander Motin while ((req = TAILQ_FIRST(&softc->ha_dts)) != NULL) { 8657ac58230SAlexander Motin TAILQ_REMOVE(&softc->ha_dts, req, links); 8667ac58230SAlexander Motin mtx_unlock(&softc->ha_lock); 8677ac58230SAlexander Motin req->ret = CTL_HA_STATUS_DISCONNECT; 8687ac58230SAlexander Motin req->callback(req); 8697ac58230SAlexander Motin mtx_lock(&softc->ha_lock); 8707ac58230SAlexander Motin } 8717ac58230SAlexander Motin mtx_unlock(&softc->ha_lock); 8727ac58230SAlexander Motin } 8737ac58230SAlexander Motin } else { 8747ac58230SAlexander Motin printf("%s: Unknown event %d\n", __func__, event); 8757ac58230SAlexander Motin } 8767ac58230SAlexander Motin } 8777ac58230SAlexander Motin 8787ac58230SAlexander Motin 8797ac58230SAlexander Motin ctl_ha_status 8807ac58230SAlexander Motin ctl_ha_msg_init(struct ctl_softc *ctl_softc) 8817ac58230SAlexander Motin { 8827ac58230SAlexander Motin struct ha_softc *softc = &ha_softc; 8837ac58230SAlexander Motin int error; 8847ac58230SAlexander Motin 8857ac58230SAlexander Motin softc->ha_ctl_softc = ctl_softc; 8867ac58230SAlexander Motin mtx_init(&softc->ha_lock, "CTL HA mutex", NULL, MTX_DEF); 8877ac58230SAlexander Motin mbufq_init(&softc->ha_sendq, INT_MAX); 8887ac58230SAlexander Motin TAILQ_INIT(&softc->ha_dts); 8897ac58230SAlexander Motin error = kproc_kthread_add(ctl_ha_conn_thread, softc, 8907ac58230SAlexander Motin &ctl_softc->ctl_proc, NULL, 0, 0, "ctl", "ha_tx"); 8917ac58230SAlexander Motin if (error != 0) { 8927ac58230SAlexander Motin printf("error creating CTL HA connection thread!\n"); 8937ac58230SAlexander Motin mtx_destroy(&softc->ha_lock); 8947ac58230SAlexander Motin return (CTL_HA_STATUS_ERROR); 8957ac58230SAlexander Motin } 89659bb97a9SAlexander Motin softc->ha_shutdown_eh = EVENTHANDLER_REGISTER(shutdown_pre_sync, 89759bb97a9SAlexander Motin ctl_ha_msg_shutdown, ctl_softc, SHUTDOWN_PRI_FIRST); 8987ac58230SAlexander Motin SYSCTL_ADD_PROC(&ctl_softc->sysctl_ctx, 8997ac58230SAlexander Motin SYSCTL_CHILDREN(ctl_softc->sysctl_tree), 9007029da5cSPawel Biernacki OID_AUTO, "ha_peer", 9017029da5cSPawel Biernacki CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, 9027ac58230SAlexander Motin softc, 0, ctl_ha_peer_sysctl, "A", "HA peer connection method"); 9037ac58230SAlexander Motin 9047ac58230SAlexander Motin if (ctl_ha_msg_register(CTL_HA_CHAN_DATA, ctl_dt_event_handler) 9057ac58230SAlexander Motin != CTL_HA_STATUS_SUCCESS) { 9067ac58230SAlexander Motin printf("%s: ctl_ha_msg_register failed.\n", __func__); 9077ac58230SAlexander Motin } 9087ac58230SAlexander Motin 9097ac58230SAlexander Motin return (CTL_HA_STATUS_SUCCESS); 9107ac58230SAlexander Motin }; 9117ac58230SAlexander Motin 91259bb97a9SAlexander Motin void 9137ac58230SAlexander Motin ctl_ha_msg_shutdown(struct ctl_softc *ctl_softc) 9147ac58230SAlexander Motin { 9157ac58230SAlexander Motin struct ha_softc *softc = &ha_softc; 9167ac58230SAlexander Motin 91759bb97a9SAlexander Motin /* Disconnect and shutdown threads. */ 91859bb97a9SAlexander Motin mtx_lock(&softc->ha_lock); 91959bb97a9SAlexander Motin if (softc->ha_shutdown < 2) { 92059bb97a9SAlexander Motin softc->ha_shutdown = 1; 92159bb97a9SAlexander Motin softc->ha_wakeup = 1; 92259bb97a9SAlexander Motin wakeup(&softc->ha_wakeup); 9234982dca1SAlexander Motin while (softc->ha_shutdown < 2 && !SCHEDULER_STOPPED()) { 92459bb97a9SAlexander Motin msleep(&softc->ha_wakeup, &softc->ha_lock, 0, 92559bb97a9SAlexander Motin "shutdown", hz); 9267ac58230SAlexander Motin } 92759bb97a9SAlexander Motin } 92859bb97a9SAlexander Motin mtx_unlock(&softc->ha_lock); 92959bb97a9SAlexander Motin }; 93059bb97a9SAlexander Motin 93159bb97a9SAlexander Motin ctl_ha_status 93259bb97a9SAlexander Motin ctl_ha_msg_destroy(struct ctl_softc *ctl_softc) 93359bb97a9SAlexander Motin { 93459bb97a9SAlexander Motin struct ha_softc *softc = &ha_softc; 93559bb97a9SAlexander Motin 93659bb97a9SAlexander Motin if (softc->ha_shutdown_eh != NULL) { 93759bb97a9SAlexander Motin EVENTHANDLER_DEREGISTER(shutdown_pre_sync, 93859bb97a9SAlexander Motin softc->ha_shutdown_eh); 93959bb97a9SAlexander Motin softc->ha_shutdown_eh = NULL; 94059bb97a9SAlexander Motin } 94159bb97a9SAlexander Motin 94259bb97a9SAlexander Motin ctl_ha_msg_shutdown(ctl_softc); /* Just in case. */ 94359bb97a9SAlexander Motin 94459bb97a9SAlexander Motin if (ctl_ha_msg_deregister(CTL_HA_CHAN_DATA) != CTL_HA_STATUS_SUCCESS) 94559bb97a9SAlexander Motin printf("%s: ctl_ha_msg_deregister failed.\n", __func__); 9467ac58230SAlexander Motin 9477ac58230SAlexander Motin mtx_destroy(&softc->ha_lock); 9487ac58230SAlexander Motin return (CTL_HA_STATUS_SUCCESS); 9497ac58230SAlexander Motin }; 950