2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2015 Alexander Motin <mav@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer,
12 * without modification, immediately at the beginning of the file.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/kthread.h>
36 #include <sys/types.h>
37 #include <sys/limits.h>
39 #include <sys/module.h>
40 #include <sys/mutex.h>
41 #include <sys/condvar.h>
42 #include <sys/malloc.h>
46 #include <sys/queue.h>
47 #include <sys/sysctl.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
51 #include <netinet/in.h>
52 #include <netinet/tcp.h>
56 #include <cam/scsi/scsi_all.h>
57 #include <cam/scsi/scsi_da.h>
58 #include <cam/ctl/ctl_io.h>
59 #include <cam/ctl/ctl.h>
60 #include <cam/ctl/ctl_frontend.h>
61 #include <cam/ctl/ctl_util.h>
62 #include <cam/ctl/ctl_backend.h>
63 #include <cam/ctl/ctl_ioctl.h>
64 #include <cam/ctl/ctl_ha.h>
65 #include <cam/ctl/ctl_private.h>
66 #include <cam/ctl/ctl_debug.h>
67 #include <cam/ctl/ctl_error.h>
69 #if (__FreeBSD_version < 1100000)
76 mbufq_init(struct mbufq *q, int limit)
79 q->head = q->tail = NULL;
83 mbufq_drain(struct mbufq *q)
87 while ((m = q->head) != NULL) {
88 q->head = m->m_nextpkt;
95 mbufq_dequeue(struct mbufq *q)
103 q->head = m->m_nextpkt;
110 mbufq_enqueue(struct mbufq *q, struct mbuf *m)
115 q->tail->m_nextpkt = m;
122 sbavail(struct sockbuf *sb)
127 #if (__FreeBSD_version < 1000000)
128 #define mtodo(m, o) ((void *)(((m)->m_data) + (o)))
137 struct ha_dt_msg_wire {
138 ctl_ha_dt_cmd command;
145 struct ctl_softc *ha_ctl_softc;
146 ctl_evt_handler ha_handler[CTL_HA_CHAN_MAX];
148 struct sockaddr_in ha_peer_in;
149 struct socket *ha_lso;
150 struct socket *ha_so;
151 struct mbufq ha_sendq;
152 struct mbuf *ha_sending;
161 eventhandler_tag ha_shutdown_eh;
162 TAILQ_HEAD(, ctl_ha_dt_req) ha_dts;
166 ctl_ha_conn_wake(struct ha_softc *softc)
169 mtx_lock(&softc->ha_lock);
170 softc->ha_wakeup = 1;
171 mtx_unlock(&softc->ha_lock);
172 wakeup(&softc->ha_wakeup);
176 ctl_ha_lupcall(struct socket *so, void *arg, int waitflag)
178 struct ha_softc *softc = arg;
180 ctl_ha_conn_wake(softc);
185 ctl_ha_rupcall(struct socket *so, void *arg, int waitflag)
187 struct ha_softc *softc = arg;
189 wakeup(&softc->ha_receiving);
194 ctl_ha_supcall(struct socket *so, void *arg, int waitflag)
196 struct ha_softc *softc = arg;
198 ctl_ha_conn_wake(softc);
203 ctl_ha_evt(struct ha_softc *softc, ctl_ha_channel ch, ctl_ha_event evt,
208 if (ch < CTL_HA_CHAN_MAX) {
209 if (softc->ha_handler[ch])
210 softc->ha_handler[ch](ch, evt, param);
213 for (i = 0; i < CTL_HA_CHAN_MAX; i++) {
214 if (softc->ha_handler[i])
215 softc->ha_handler[i](i, evt, param);
220 ctl_ha_close(struct ha_softc *softc)
222 struct socket *so = softc->ha_so;
225 if (softc->ha_connected || softc->ha_disconnect) {
226 softc->ha_connected = 0;
227 mbufq_drain(&softc->ha_sendq);
228 m_freem(softc->ha_sending);
229 softc->ha_sending = NULL;
233 SOCKBUF_LOCK(&so->so_rcv);
234 soupcall_clear(so, SO_RCV);
235 while (softc->ha_receiving) {
236 wakeup(&softc->ha_receiving);
237 msleep(&softc->ha_receiving, SOCKBUF_MTX(&so->so_rcv),
240 SOCKBUF_UNLOCK(&so->so_rcv);
241 SOCKBUF_LOCK(&so->so_snd);
242 soupcall_clear(so, SO_SND);
243 SOCKBUF_UNLOCK(&so->so_snd);
245 if (softc->ha_connect)
246 pause("reconnect", hz / 2);
250 ctl_ha_evt(softc, CTL_HA_CHAN_MAX, CTL_HA_EVT_LINK_CHANGE,
251 (softc->ha_connect || softc->ha_listen) ?
252 CTL_HA_LINK_UNKNOWN : CTL_HA_LINK_OFFLINE);
257 ctl_ha_lclose(struct ha_softc *softc)
261 SOCKBUF_LOCK(&softc->ha_lso->so_rcv);
262 soupcall_clear(softc->ha_lso, SO_RCV);
263 SOCKBUF_UNLOCK(&softc->ha_lso->so_rcv);
264 soclose(softc->ha_lso);
265 softc->ha_lso = NULL;
270 ctl_ha_rx_thread(void *arg)
272 struct ha_softc *softc = arg;
273 struct socket *so = softc->ha_so;
274 struct ha_msg_wire wire_hdr;
277 int error, flags, next;
279 bzero(&wire_hdr, sizeof(wire_hdr));
281 if (wire_hdr.length > 0)
282 next = wire_hdr.length;
284 next = sizeof(wire_hdr);
285 SOCKBUF_LOCK(&so->so_rcv);
286 while (sbavail(&so->so_rcv) < next || softc->ha_disconnect) {
287 if (softc->ha_connected == 0 || softc->ha_disconnect ||
289 (so->so_rcv.sb_state & SBS_CANTRCVMORE)) {
292 so->so_rcv.sb_lowat = next;
293 msleep(&softc->ha_receiving, SOCKBUF_MTX(&so->so_rcv),
296 SOCKBUF_UNLOCK(&so->so_rcv);
298 if (wire_hdr.length == 0) {
299 iov.iov_base = &wire_hdr;
300 iov.iov_len = sizeof(wire_hdr);
303 uio.uio_rw = UIO_READ;
304 uio.uio_segflg = UIO_SYSSPACE;
305 uio.uio_td = curthread;
306 uio.uio_resid = sizeof(wire_hdr);
307 flags = MSG_DONTWAIT;
308 error = soreceive(softc->ha_so, NULL, &uio, NULL,
311 printf("%s: header receive error %d\n",
313 SOCKBUF_LOCK(&so->so_rcv);
317 ctl_ha_evt(softc, wire_hdr.channel,
318 CTL_HA_EVT_MSG_RECV, wire_hdr.length);
324 softc->ha_receiving = 0;
325 wakeup(&softc->ha_receiving);
326 SOCKBUF_UNLOCK(&so->so_rcv);
327 ctl_ha_conn_wake(softc);
332 ctl_ha_send(struct ha_softc *softc)
334 struct socket *so = softc->ha_so;
338 if (softc->ha_sending == NULL) {
339 mtx_lock(&softc->ha_lock);
340 softc->ha_sending = mbufq_dequeue(&softc->ha_sendq);
341 mtx_unlock(&softc->ha_lock);
342 if (softc->ha_sending == NULL) {
343 so->so_snd.sb_lowat = so->so_snd.sb_hiwat + 1;
347 SOCKBUF_LOCK(&so->so_snd);
348 if (sbspace(&so->so_snd) < softc->ha_sending->m_pkthdr.len) {
349 so->so_snd.sb_lowat = softc->ha_sending->m_pkthdr.len;
350 SOCKBUF_UNLOCK(&so->so_snd);
353 SOCKBUF_UNLOCK(&so->so_snd);
354 error = sosend(softc->ha_so, NULL, NULL, softc->ha_sending,
355 NULL, MSG_DONTWAIT, curthread);
356 softc->ha_sending = NULL;
358 printf("%s: sosend() error %d\n", __func__, error);
365 ctl_ha_sock_setup(struct ha_softc *softc)
368 struct socket *so = softc->ha_so;
372 error = soreserve(so, val, val);
374 printf("%s: soreserve failed %d\n", __func__, error);
376 SOCKBUF_LOCK(&so->so_rcv);
377 so->so_rcv.sb_lowat = sizeof(struct ha_msg_wire);
378 soupcall_set(so, SO_RCV, ctl_ha_rupcall, softc);
379 SOCKBUF_UNLOCK(&so->so_rcv);
380 SOCKBUF_LOCK(&so->so_snd);
381 so->so_snd.sb_lowat = sizeof(struct ha_msg_wire);
382 soupcall_set(so, SO_SND, ctl_ha_supcall, softc);
383 SOCKBUF_UNLOCK(&so->so_snd);
385 bzero(&opt, sizeof(struct sockopt));
386 opt.sopt_dir = SOPT_SET;
387 opt.sopt_level = SOL_SOCKET;
388 opt.sopt_name = SO_KEEPALIVE;
390 opt.sopt_valsize = sizeof(val);
392 error = sosetopt(so, &opt);
394 printf("%s: KEEPALIVE setting failed %d\n", __func__, error);
396 opt.sopt_level = IPPROTO_TCP;
397 opt.sopt_name = TCP_NODELAY;
399 error = sosetopt(so, &opt);
401 printf("%s: NODELAY setting failed %d\n", __func__, error);
403 opt.sopt_name = TCP_KEEPINIT;
405 error = sosetopt(so, &opt);
407 printf("%s: KEEPINIT setting failed %d\n", __func__, error);
409 opt.sopt_name = TCP_KEEPIDLE;
411 error = sosetopt(so, &opt);
413 printf("%s: KEEPIDLE setting failed %d\n", __func__, error);
415 opt.sopt_name = TCP_KEEPINTVL;
417 error = sosetopt(so, &opt);
419 printf("%s: KEEPINTVL setting failed %d\n", __func__, error);
421 opt.sopt_name = TCP_KEEPCNT;
423 error = sosetopt(so, &opt);
425 printf("%s: KEEPCNT setting failed %d\n", __func__, error);
429 ctl_ha_connect(struct ha_softc *softc)
431 struct thread *td = curthread;
432 struct sockaddr_in sa;
436 /* Create the socket */
437 error = socreate(PF_INET, &so, SOCK_STREAM,
438 IPPROTO_TCP, td->td_ucred, td);
440 printf("%s: socreate() error %d\n", __func__, error);
444 ctl_ha_sock_setup(softc);
446 memcpy(&sa, &softc->ha_peer_in, sizeof(sa));
447 error = soconnect(so, (struct sockaddr *)&sa, td);
450 printf("%s: soconnect() error %d\n", __func__, error);
461 ctl_ha_accept(struct ha_softc *softc)
463 struct socket *lso, *so;
464 struct sockaddr *sap;
469 error = solisten_dequeue(lso, &so, 0);
470 if (error == EWOULDBLOCK)
473 printf("%s: socket error %d\n", __func__, error);
478 error = soaccept(so, &sap);
480 printf("%s: soaccept() error %d\n", __func__, error);
488 ctl_ha_sock_setup(softc);
492 ctl_ha_lclose(softc);
497 ctl_ha_listen(struct ha_softc *softc)
499 struct thread *td = curthread;
500 struct sockaddr_in sa;
504 /* Create the socket */
505 if (softc->ha_lso == NULL) {
506 error = socreate(PF_INET, &softc->ha_lso, SOCK_STREAM,
507 IPPROTO_TCP, td->td_ucred, td);
509 printf("%s: socreate() error %d\n", __func__, error);
512 bzero(&opt, sizeof(struct sockopt));
513 opt.sopt_dir = SOPT_SET;
514 opt.sopt_level = SOL_SOCKET;
515 opt.sopt_name = SO_REUSEADDR;
517 opt.sopt_valsize = sizeof(val);
519 error = sosetopt(softc->ha_lso, &opt);
521 printf("%s: REUSEADDR setting failed %d\n",
524 bzero(&opt, sizeof(struct sockopt));
525 opt.sopt_dir = SOPT_SET;
526 opt.sopt_level = SOL_SOCKET;
527 opt.sopt_name = SO_REUSEPORT;
529 opt.sopt_valsize = sizeof(val);
531 error = sosetopt(softc->ha_lso, &opt);
533 printf("%s: REUSEPORT setting failed %d\n",
538 memcpy(&sa, &softc->ha_peer_in, sizeof(sa));
539 error = sobind(softc->ha_lso, (struct sockaddr *)&sa, td);
541 printf("%s: sobind() error %d\n", __func__, error);
544 error = solisten(softc->ha_lso, 1, td);
546 printf("%s: solisten() error %d\n", __func__, error);
549 SOLISTEN_LOCK(softc->ha_lso);
550 softc->ha_lso->so_state |= SS_NBIO;
551 solisten_upcall_set(softc->ha_lso, ctl_ha_lupcall, softc);
552 SOLISTEN_UNLOCK(softc->ha_lso);
556 ctl_ha_lclose(softc);
561 ctl_ha_conn_thread(void *arg)
563 struct ha_softc *softc = arg;
567 if (softc->ha_disconnect || softc->ha_shutdown) {
569 if (softc->ha_disconnect == 2 || softc->ha_shutdown)
570 ctl_ha_lclose(softc);
571 softc->ha_disconnect = 0;
572 if (softc->ha_shutdown)
574 } else if (softc->ha_so != NULL &&
575 (softc->ha_so->so_error ||
576 softc->ha_so->so_rcv.sb_state & SBS_CANTRCVMORE))
578 if (softc->ha_so == NULL) {
579 if (softc->ha_lso != NULL)
580 ctl_ha_accept(softc);
581 else if (softc->ha_listen)
582 ctl_ha_listen(softc);
583 else if (softc->ha_connect)
584 ctl_ha_connect(softc);
586 if (softc->ha_so != NULL) {
587 if (softc->ha_connected == 0 &&
588 softc->ha_so->so_error == 0 &&
589 (softc->ha_so->so_state & SS_ISCONNECTING) == 0) {
590 softc->ha_connected = 1;
591 ctl_ha_evt(softc, CTL_HA_CHAN_MAX,
592 CTL_HA_EVT_LINK_CHANGE,
594 softc->ha_receiving = 1;
595 error = kproc_kthread_add(ctl_ha_rx_thread,
596 softc, &softc->ha_ctl_softc->ctl_proc,
597 NULL, 0, 0, "ctl", "ha_rx");
599 printf("Error creating CTL HA rx thread!\n");
600 softc->ha_receiving = 0;
601 softc->ha_disconnect = 1;
606 mtx_lock(&softc->ha_lock);
607 if (softc->ha_so != NULL &&
608 (softc->ha_so->so_error ||
609 softc->ha_so->so_rcv.sb_state & SBS_CANTRCVMORE))
611 else if (!softc->ha_wakeup)
612 msleep(&softc->ha_wakeup, &softc->ha_lock, 0, "-", hz);
613 softc->ha_wakeup = 0;
614 mtx_unlock(&softc->ha_lock);
616 mtx_lock(&softc->ha_lock);
617 softc->ha_shutdown = 2;
618 wakeup(&softc->ha_wakeup);
619 mtx_unlock(&softc->ha_lock);
624 ctl_ha_peer_sysctl(SYSCTL_HANDLER_ARGS)
626 struct ha_softc *softc = (struct ha_softc *)arg1;
627 struct sockaddr_in *sa;
628 int error, b1, b2, b3, b4, p, num;
631 strlcpy(buf, softc->ha_peer, sizeof(buf));
632 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
633 if ((error != 0) || (req->newptr == NULL) ||
634 strncmp(buf, softc->ha_peer, sizeof(buf)) == 0)
637 sa = &softc->ha_peer_in;
638 mtx_lock(&softc->ha_lock);
639 if ((num = sscanf(buf, "connect %d.%d.%d.%d:%d",
640 &b1, &b2, &b3, &b4, &p)) >= 4) {
641 softc->ha_connect = 1;
642 softc->ha_listen = 0;
643 } else if ((num = sscanf(buf, "listen %d.%d.%d.%d:%d",
644 &b1, &b2, &b3, &b4, &p)) >= 4) {
645 softc->ha_connect = 0;
646 softc->ha_listen = 1;
648 softc->ha_connect = 0;
649 softc->ha_listen = 0;
655 strlcpy(softc->ha_peer, buf, sizeof(softc->ha_peer));
656 if (softc->ha_connect || softc->ha_listen) {
657 memset(sa, 0, sizeof(*sa));
658 sa->sin_len = sizeof(struct sockaddr_in);
659 sa->sin_family = AF_INET;
660 sa->sin_port = htons((num >= 5) ? p : 999);
661 sa->sin_addr.s_addr =
662 htonl((b1 << 24) + (b2 << 16) + (b3 << 8) + b4);
664 softc->ha_disconnect = 2;
665 softc->ha_wakeup = 1;
666 mtx_unlock(&softc->ha_lock);
667 wakeup(&softc->ha_wakeup);
672 ctl_ha_msg_register(ctl_ha_channel channel, ctl_evt_handler handler)
674 struct ha_softc *softc = &ha_softc;
676 KASSERT(channel < CTL_HA_CHAN_MAX,
677 ("Wrong CTL HA channel %d", channel));
678 softc->ha_handler[channel] = handler;
679 return (CTL_HA_STATUS_SUCCESS);
683 ctl_ha_msg_deregister(ctl_ha_channel channel)
685 struct ha_softc *softc = &ha_softc;
687 KASSERT(channel < CTL_HA_CHAN_MAX,
688 ("Wrong CTL HA channel %d", channel));
689 softc->ha_handler[channel] = NULL;
690 return (CTL_HA_STATUS_SUCCESS);
694 * Receive a message of the specified size.
697 ctl_ha_msg_recv(ctl_ha_channel channel, void *addr, size_t len,
700 struct ha_softc *softc = &ha_softc;
705 if (!softc->ha_connected)
706 return (CTL_HA_STATUS_DISCONNECT);
712 uio.uio_rw = UIO_READ;
713 uio.uio_segflg = UIO_SYSSPACE;
714 uio.uio_td = curthread;
716 flags = wait ? 0 : MSG_DONTWAIT;
717 error = soreceive(softc->ha_so, NULL, &uio, NULL, NULL, &flags);
719 return (CTL_HA_STATUS_SUCCESS);
721 /* Consider all errors fatal for HA sanity. */
722 mtx_lock(&softc->ha_lock);
723 if (softc->ha_connected) {
724 softc->ha_disconnect = 1;
725 softc->ha_wakeup = 1;
726 wakeup(&softc->ha_wakeup);
728 mtx_unlock(&softc->ha_lock);
729 return (CTL_HA_STATUS_ERROR);
733 * Send a message of the specified size.
736 ctl_ha_msg_send2(ctl_ha_channel channel, const void *addr, size_t len,
737 const void *addr2, size_t len2, int wait)
739 struct ha_softc *softc = &ha_softc;
740 struct mbuf *mb, *newmb;
741 struct ha_msg_wire hdr;
744 if (!softc->ha_connected)
745 return (CTL_HA_STATUS_DISCONNECT);
747 newmb = m_getm2(NULL, sizeof(hdr) + len + len2, wait, MT_DATA,
750 /* Consider all errors fatal for HA sanity. */
751 mtx_lock(&softc->ha_lock);
752 if (softc->ha_connected) {
753 softc->ha_disconnect = 1;
754 softc->ha_wakeup = 1;
755 wakeup(&softc->ha_wakeup);
757 mtx_unlock(&softc->ha_lock);
758 printf("%s: Can't allocate mbuf chain\n", __func__);
759 return (CTL_HA_STATUS_ERROR);
761 hdr.channel = channel;
762 hdr.length = len + len2;
764 memcpy(mtodo(mb, 0), &hdr, sizeof(hdr));
765 mb->m_len += sizeof(hdr);
767 for (; mb != NULL && off < len; mb = mb->m_next) {
768 copylen = min(M_TRAILINGSPACE(mb), len - off);
769 memcpy(mtodo(mb, mb->m_len), (const char *)addr + off, copylen);
770 mb->m_len += copylen;
775 KASSERT(off == len, ("%s: off (%zu) != len (%zu)", __func__,
778 for (; mb != NULL && off < len2; mb = mb->m_next) {
779 copylen = min(M_TRAILINGSPACE(mb), len2 - off);
780 memcpy(mtodo(mb, mb->m_len), (const char *)addr2 + off, copylen);
781 mb->m_len += copylen;
784 KASSERT(off == len2, ("%s: off (%zu) != len2 (%zu)", __func__,
786 newmb->m_pkthdr.len = sizeof(hdr) + len + len2;
788 mtx_lock(&softc->ha_lock);
789 if (!softc->ha_connected) {
790 mtx_unlock(&softc->ha_lock);
792 return (CTL_HA_STATUS_DISCONNECT);
794 mbufq_enqueue(&softc->ha_sendq, newmb);
795 softc->ha_wakeup = 1;
796 mtx_unlock(&softc->ha_lock);
797 wakeup(&softc->ha_wakeup);
798 return (CTL_HA_STATUS_SUCCESS);
802 ctl_ha_msg_send(ctl_ha_channel channel, const void *addr, size_t len,
806 return (ctl_ha_msg_send2(channel, addr, len, NULL, 0, wait));
810 ctl_ha_msg_abort(ctl_ha_channel channel)
812 struct ha_softc *softc = &ha_softc;
814 mtx_lock(&softc->ha_lock);
815 softc->ha_disconnect = 1;
816 softc->ha_wakeup = 1;
817 mtx_unlock(&softc->ha_lock);
818 wakeup(&softc->ha_wakeup);
819 return (CTL_HA_STATUS_SUCCESS);
823 * Allocate a data transfer request structure.
825 struct ctl_ha_dt_req *
826 ctl_dt_req_alloc(void)
829 return (malloc(sizeof(struct ctl_ha_dt_req), M_CTL, M_WAITOK | M_ZERO));
833 * Free a data transfer request structure.
836 ctl_dt_req_free(struct ctl_ha_dt_req *req)
843 * Issue a DMA request for a single buffer.
846 ctl_dt_single(struct ctl_ha_dt_req *req)
848 struct ha_softc *softc = &ha_softc;
849 struct ha_dt_msg_wire wire_dt;
850 ctl_ha_status status;
852 wire_dt.command = req->command;
853 wire_dt.size = req->size;
854 wire_dt.local = req->local;
855 wire_dt.remote = req->remote;
856 if (req->command == CTL_HA_DT_CMD_READ && req->callback != NULL) {
857 mtx_lock(&softc->ha_lock);
858 TAILQ_INSERT_TAIL(&softc->ha_dts, req, links);
859 mtx_unlock(&softc->ha_lock);
860 ctl_ha_msg_send(CTL_HA_CHAN_DATA, &wire_dt, sizeof(wire_dt),
862 return (CTL_HA_STATUS_WAIT);
864 if (req->command == CTL_HA_DT_CMD_READ) {
865 status = ctl_ha_msg_send(CTL_HA_CHAN_DATA, &wire_dt,
866 sizeof(wire_dt), M_WAITOK);
868 status = ctl_ha_msg_send2(CTL_HA_CHAN_DATA, &wire_dt,
869 sizeof(wire_dt), req->local, req->size, M_WAITOK);
875 ctl_dt_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
877 struct ha_softc *softc = &ha_softc;
878 struct ctl_ha_dt_req *req;
879 ctl_ha_status isc_status;
881 if (event == CTL_HA_EVT_MSG_RECV) {
882 struct ha_dt_msg_wire wire_dt;
886 size = min(sizeof(wire_dt), param);
887 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_DATA, &wire_dt,
889 if (isc_status != CTL_HA_STATUS_SUCCESS) {
890 printf("%s: Error receiving message: %d\n",
891 __func__, isc_status);
895 if (wire_dt.command == CTL_HA_DT_CMD_READ) {
896 wire_dt.command = CTL_HA_DT_CMD_WRITE;
898 wire_dt.local = wire_dt.remote;
899 wire_dt.remote = tmp;
900 ctl_ha_msg_send2(CTL_HA_CHAN_DATA, &wire_dt,
901 sizeof(wire_dt), wire_dt.local, wire_dt.size,
903 } else if (wire_dt.command == CTL_HA_DT_CMD_WRITE) {
904 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_DATA,
905 wire_dt.remote, wire_dt.size, M_WAITOK);
906 mtx_lock(&softc->ha_lock);
907 TAILQ_FOREACH(req, &softc->ha_dts, links) {
908 if (req->local == wire_dt.remote) {
909 TAILQ_REMOVE(&softc->ha_dts, req, links);
913 mtx_unlock(&softc->ha_lock);
915 req->ret = isc_status;
919 } else if (event == CTL_HA_EVT_LINK_CHANGE) {
920 CTL_DEBUG_PRINT(("%s: Link state change to %d\n", __func__,
922 if (param != CTL_HA_LINK_ONLINE) {
923 mtx_lock(&softc->ha_lock);
924 while ((req = TAILQ_FIRST(&softc->ha_dts)) != NULL) {
925 TAILQ_REMOVE(&softc->ha_dts, req, links);
926 mtx_unlock(&softc->ha_lock);
927 req->ret = CTL_HA_STATUS_DISCONNECT;
929 mtx_lock(&softc->ha_lock);
931 mtx_unlock(&softc->ha_lock);
934 printf("%s: Unknown event %d\n", __func__, event);
940 ctl_ha_msg_init(struct ctl_softc *ctl_softc)
942 struct ha_softc *softc = &ha_softc;
945 softc->ha_ctl_softc = ctl_softc;
946 mtx_init(&softc->ha_lock, "CTL HA mutex", NULL, MTX_DEF);
947 mbufq_init(&softc->ha_sendq, INT_MAX);
948 TAILQ_INIT(&softc->ha_dts);
949 error = kproc_kthread_add(ctl_ha_conn_thread, softc,
950 &ctl_softc->ctl_proc, NULL, 0, 0, "ctl", "ha_tx");
952 printf("error creating CTL HA connection thread!\n");
953 mtx_destroy(&softc->ha_lock);
954 return (CTL_HA_STATUS_ERROR);
956 softc->ha_shutdown_eh = EVENTHANDLER_REGISTER(shutdown_pre_sync,
957 ctl_ha_msg_shutdown, ctl_softc, SHUTDOWN_PRI_FIRST);
958 SYSCTL_ADD_PROC(&ctl_softc->sysctl_ctx,
959 SYSCTL_CHILDREN(ctl_softc->sysctl_tree),
960 OID_AUTO, "ha_peer", CTLTYPE_STRING | CTLFLAG_RWTUN,
961 softc, 0, ctl_ha_peer_sysctl, "A", "HA peer connection method");
963 if (ctl_ha_msg_register(CTL_HA_CHAN_DATA, ctl_dt_event_handler)
964 != CTL_HA_STATUS_SUCCESS) {
965 printf("%s: ctl_ha_msg_register failed.\n", __func__);
968 return (CTL_HA_STATUS_SUCCESS);
972 ctl_ha_msg_shutdown(struct ctl_softc *ctl_softc)
974 struct ha_softc *softc = &ha_softc;
976 /* Disconnect and shutdown threads. */
977 mtx_lock(&softc->ha_lock);
978 if (softc->ha_shutdown < 2) {
979 softc->ha_shutdown = 1;
980 softc->ha_wakeup = 1;
981 wakeup(&softc->ha_wakeup);
982 while (softc->ha_shutdown < 2 && !SCHEDULER_STOPPED()) {
983 msleep(&softc->ha_wakeup, &softc->ha_lock, 0,
987 mtx_unlock(&softc->ha_lock);
991 ctl_ha_msg_destroy(struct ctl_softc *ctl_softc)
993 struct ha_softc *softc = &ha_softc;
995 if (softc->ha_shutdown_eh != NULL) {
996 EVENTHANDLER_DEREGISTER(shutdown_pre_sync,
997 softc->ha_shutdown_eh);
998 softc->ha_shutdown_eh = NULL;
1001 ctl_ha_msg_shutdown(ctl_softc); /* Just in case. */
1003 if (ctl_ha_msg_deregister(CTL_HA_CHAN_DATA) != CTL_HA_STATUS_SUCCESS)
1004 printf("%s: ctl_ha_msg_deregister failed.\n", __func__);
1006 mtx_destroy(&softc->ha_lock);
1007 return (CTL_HA_STATUS_SUCCESS);