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/condvar.h>
35 #include <sys/eventhandler.h>
36 #include <sys/kernel.h>
37 #include <sys/kthread.h>
38 #include <sys/limits.h>
40 #include <sys/malloc.h>
42 #include <sys/module.h>
43 #include <sys/mutex.h>
45 #include <sys/queue.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.h>
48 #include <sys/sysctl.h>
49 #include <sys/systm.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>
75 struct ha_dt_msg_wire {
76 ctl_ha_dt_cmd command;
83 struct ctl_softc *ha_ctl_softc;
84 ctl_evt_handler ha_handler[CTL_HA_CHAN_MAX];
86 struct sockaddr_in ha_peer_in;
87 struct socket *ha_lso;
89 struct mbufq ha_sendq;
90 struct mbuf *ha_sending;
99 eventhandler_tag ha_shutdown_eh;
100 TAILQ_HEAD(, ctl_ha_dt_req) ha_dts;
104 ctl_ha_conn_wake(struct ha_softc *softc)
107 mtx_lock(&softc->ha_lock);
108 softc->ha_wakeup = 1;
109 mtx_unlock(&softc->ha_lock);
110 wakeup(&softc->ha_wakeup);
114 ctl_ha_lupcall(struct socket *so, void *arg, int waitflag)
116 struct ha_softc *softc = arg;
118 ctl_ha_conn_wake(softc);
123 ctl_ha_rupcall(struct socket *so, void *arg, int waitflag)
125 struct ha_softc *softc = arg;
127 wakeup(&softc->ha_receiving);
132 ctl_ha_supcall(struct socket *so, void *arg, int waitflag)
134 struct ha_softc *softc = arg;
136 ctl_ha_conn_wake(softc);
141 ctl_ha_evt(struct ha_softc *softc, ctl_ha_channel ch, ctl_ha_event evt,
146 if (ch < CTL_HA_CHAN_MAX) {
147 if (softc->ha_handler[ch])
148 softc->ha_handler[ch](ch, evt, param);
151 for (i = 0; i < CTL_HA_CHAN_MAX; i++) {
152 if (softc->ha_handler[i])
153 softc->ha_handler[i](i, evt, param);
158 ctl_ha_close(struct ha_softc *softc)
160 struct socket *so = softc->ha_so;
163 if (softc->ha_connected || softc->ha_disconnect) {
164 softc->ha_connected = 0;
165 mbufq_drain(&softc->ha_sendq);
166 m_freem(softc->ha_sending);
167 softc->ha_sending = NULL;
171 SOCKBUF_LOCK(&so->so_rcv);
172 soupcall_clear(so, SO_RCV);
173 while (softc->ha_receiving) {
174 wakeup(&softc->ha_receiving);
175 msleep(&softc->ha_receiving, SOCKBUF_MTX(&so->so_rcv),
178 SOCKBUF_UNLOCK(&so->so_rcv);
179 SOCKBUF_LOCK(&so->so_snd);
180 soupcall_clear(so, SO_SND);
181 SOCKBUF_UNLOCK(&so->so_snd);
183 if (softc->ha_connect)
184 pause("reconnect", hz / 2);
188 ctl_ha_evt(softc, CTL_HA_CHAN_MAX, CTL_HA_EVT_LINK_CHANGE,
189 (softc->ha_connect || softc->ha_listen) ?
190 CTL_HA_LINK_UNKNOWN : CTL_HA_LINK_OFFLINE);
195 ctl_ha_lclose(struct ha_softc *softc)
199 SOCKBUF_LOCK(&softc->ha_lso->so_rcv);
200 soupcall_clear(softc->ha_lso, SO_RCV);
201 SOCKBUF_UNLOCK(&softc->ha_lso->so_rcv);
202 soclose(softc->ha_lso);
203 softc->ha_lso = NULL;
208 ctl_ha_rx_thread(void *arg)
210 struct ha_softc *softc = arg;
211 struct socket *so = softc->ha_so;
212 struct ha_msg_wire wire_hdr;
215 int error, flags, next;
217 bzero(&wire_hdr, sizeof(wire_hdr));
219 if (wire_hdr.length > 0)
220 next = wire_hdr.length;
222 next = sizeof(wire_hdr);
223 SOCKBUF_LOCK(&so->so_rcv);
224 while (sbavail(&so->so_rcv) < next || softc->ha_disconnect) {
225 if (softc->ha_connected == 0 || softc->ha_disconnect ||
227 (so->so_rcv.sb_state & SBS_CANTRCVMORE)) {
230 so->so_rcv.sb_lowat = next;
231 msleep(&softc->ha_receiving, SOCKBUF_MTX(&so->so_rcv),
234 SOCKBUF_UNLOCK(&so->so_rcv);
236 if (wire_hdr.length == 0) {
237 iov.iov_base = &wire_hdr;
238 iov.iov_len = sizeof(wire_hdr);
241 uio.uio_rw = UIO_READ;
242 uio.uio_segflg = UIO_SYSSPACE;
243 uio.uio_td = curthread;
244 uio.uio_resid = sizeof(wire_hdr);
245 flags = MSG_DONTWAIT;
246 error = soreceive(softc->ha_so, NULL, &uio, NULL,
249 printf("%s: header receive error %d\n",
251 SOCKBUF_LOCK(&so->so_rcv);
255 ctl_ha_evt(softc, wire_hdr.channel,
256 CTL_HA_EVT_MSG_RECV, wire_hdr.length);
262 softc->ha_receiving = 0;
263 wakeup(&softc->ha_receiving);
264 SOCKBUF_UNLOCK(&so->so_rcv);
265 ctl_ha_conn_wake(softc);
270 ctl_ha_send(struct ha_softc *softc)
272 struct socket *so = softc->ha_so;
276 if (softc->ha_sending == NULL) {
277 mtx_lock(&softc->ha_lock);
278 softc->ha_sending = mbufq_dequeue(&softc->ha_sendq);
279 mtx_unlock(&softc->ha_lock);
280 if (softc->ha_sending == NULL) {
281 so->so_snd.sb_lowat = so->so_snd.sb_hiwat + 1;
285 SOCKBUF_LOCK(&so->so_snd);
286 if (sbspace(&so->so_snd) < softc->ha_sending->m_pkthdr.len) {
287 so->so_snd.sb_lowat = softc->ha_sending->m_pkthdr.len;
288 SOCKBUF_UNLOCK(&so->so_snd);
291 SOCKBUF_UNLOCK(&so->so_snd);
292 error = sosend(softc->ha_so, NULL, NULL, softc->ha_sending,
293 NULL, MSG_DONTWAIT, curthread);
294 softc->ha_sending = NULL;
296 printf("%s: sosend() error %d\n", __func__, error);
303 ctl_ha_sock_setup(struct ha_softc *softc)
306 struct socket *so = softc->ha_so;
310 error = soreserve(so, val, val);
312 printf("%s: soreserve failed %d\n", __func__, error);
314 SOCKBUF_LOCK(&so->so_rcv);
315 so->so_rcv.sb_lowat = sizeof(struct ha_msg_wire);
316 soupcall_set(so, SO_RCV, ctl_ha_rupcall, softc);
317 SOCKBUF_UNLOCK(&so->so_rcv);
318 SOCKBUF_LOCK(&so->so_snd);
319 so->so_snd.sb_lowat = sizeof(struct ha_msg_wire);
320 soupcall_set(so, SO_SND, ctl_ha_supcall, softc);
321 SOCKBUF_UNLOCK(&so->so_snd);
323 bzero(&opt, sizeof(struct sockopt));
324 opt.sopt_dir = SOPT_SET;
325 opt.sopt_level = SOL_SOCKET;
326 opt.sopt_name = SO_KEEPALIVE;
328 opt.sopt_valsize = sizeof(val);
330 error = sosetopt(so, &opt);
332 printf("%s: KEEPALIVE setting failed %d\n", __func__, error);
334 opt.sopt_level = IPPROTO_TCP;
335 opt.sopt_name = TCP_NODELAY;
337 error = sosetopt(so, &opt);
339 printf("%s: NODELAY setting failed %d\n", __func__, error);
341 opt.sopt_name = TCP_KEEPINIT;
343 error = sosetopt(so, &opt);
345 printf("%s: KEEPINIT setting failed %d\n", __func__, error);
347 opt.sopt_name = TCP_KEEPIDLE;
349 error = sosetopt(so, &opt);
351 printf("%s: KEEPIDLE setting failed %d\n", __func__, error);
353 opt.sopt_name = TCP_KEEPINTVL;
355 error = sosetopt(so, &opt);
357 printf("%s: KEEPINTVL setting failed %d\n", __func__, error);
359 opt.sopt_name = TCP_KEEPCNT;
361 error = sosetopt(so, &opt);
363 printf("%s: KEEPCNT setting failed %d\n", __func__, error);
367 ctl_ha_connect(struct ha_softc *softc)
369 struct thread *td = curthread;
370 struct sockaddr_in sa;
374 /* Create the socket */
375 error = socreate(PF_INET, &so, SOCK_STREAM,
376 IPPROTO_TCP, td->td_ucred, td);
378 printf("%s: socreate() error %d\n", __func__, error);
382 ctl_ha_sock_setup(softc);
384 memcpy(&sa, &softc->ha_peer_in, sizeof(sa));
385 error = soconnect(so, (struct sockaddr *)&sa, td);
388 printf("%s: soconnect() error %d\n", __func__, error);
399 ctl_ha_accept(struct ha_softc *softc)
401 struct socket *lso, *so;
402 struct sockaddr *sap;
407 error = solisten_dequeue(lso, &so, 0);
408 if (error == EWOULDBLOCK)
411 printf("%s: socket error %d\n", __func__, error);
416 error = soaccept(so, &sap);
418 printf("%s: soaccept() error %d\n", __func__, error);
426 ctl_ha_sock_setup(softc);
430 ctl_ha_lclose(softc);
435 ctl_ha_listen(struct ha_softc *softc)
437 struct thread *td = curthread;
438 struct sockaddr_in sa;
442 /* Create the socket */
443 if (softc->ha_lso == NULL) {
444 error = socreate(PF_INET, &softc->ha_lso, SOCK_STREAM,
445 IPPROTO_TCP, td->td_ucred, td);
447 printf("%s: socreate() error %d\n", __func__, error);
450 bzero(&opt, sizeof(struct sockopt));
451 opt.sopt_dir = SOPT_SET;
452 opt.sopt_level = SOL_SOCKET;
453 opt.sopt_name = SO_REUSEADDR;
455 opt.sopt_valsize = sizeof(val);
457 error = sosetopt(softc->ha_lso, &opt);
459 printf("%s: REUSEADDR setting failed %d\n",
462 bzero(&opt, sizeof(struct sockopt));
463 opt.sopt_dir = SOPT_SET;
464 opt.sopt_level = SOL_SOCKET;
465 opt.sopt_name = SO_REUSEPORT;
467 opt.sopt_valsize = sizeof(val);
469 error = sosetopt(softc->ha_lso, &opt);
471 printf("%s: REUSEPORT setting failed %d\n",
476 memcpy(&sa, &softc->ha_peer_in, sizeof(sa));
477 error = sobind(softc->ha_lso, (struct sockaddr *)&sa, td);
479 printf("%s: sobind() error %d\n", __func__, error);
482 error = solisten(softc->ha_lso, 1, td);
484 printf("%s: solisten() error %d\n", __func__, error);
487 SOLISTEN_LOCK(softc->ha_lso);
488 softc->ha_lso->so_state |= SS_NBIO;
489 solisten_upcall_set(softc->ha_lso, ctl_ha_lupcall, softc);
490 SOLISTEN_UNLOCK(softc->ha_lso);
494 ctl_ha_lclose(softc);
499 ctl_ha_conn_thread(void *arg)
501 struct ha_softc *softc = arg;
505 if (softc->ha_disconnect || softc->ha_shutdown) {
507 if (softc->ha_disconnect == 2 || softc->ha_shutdown)
508 ctl_ha_lclose(softc);
509 softc->ha_disconnect = 0;
510 if (softc->ha_shutdown)
512 } else if (softc->ha_so != NULL &&
513 (softc->ha_so->so_error ||
514 softc->ha_so->so_rcv.sb_state & SBS_CANTRCVMORE))
516 if (softc->ha_so == NULL) {
517 if (softc->ha_lso != NULL)
518 ctl_ha_accept(softc);
519 else if (softc->ha_listen)
520 ctl_ha_listen(softc);
521 else if (softc->ha_connect)
522 ctl_ha_connect(softc);
524 if (softc->ha_so != NULL) {
525 if (softc->ha_connected == 0 &&
526 softc->ha_so->so_error == 0 &&
527 (softc->ha_so->so_state & SS_ISCONNECTING) == 0) {
528 softc->ha_connected = 1;
529 ctl_ha_evt(softc, CTL_HA_CHAN_MAX,
530 CTL_HA_EVT_LINK_CHANGE,
532 softc->ha_receiving = 1;
533 error = kproc_kthread_add(ctl_ha_rx_thread,
534 softc, &softc->ha_ctl_softc->ctl_proc,
535 NULL, 0, 0, "ctl", "ha_rx");
537 printf("Error creating CTL HA rx thread!\n");
538 softc->ha_receiving = 0;
539 softc->ha_disconnect = 1;
544 mtx_lock(&softc->ha_lock);
545 if (softc->ha_so != NULL &&
546 (softc->ha_so->so_error ||
547 softc->ha_so->so_rcv.sb_state & SBS_CANTRCVMORE))
549 else if (!softc->ha_wakeup)
550 msleep(&softc->ha_wakeup, &softc->ha_lock, 0, "-", hz);
551 softc->ha_wakeup = 0;
552 mtx_unlock(&softc->ha_lock);
554 mtx_lock(&softc->ha_lock);
555 softc->ha_shutdown = 2;
556 wakeup(&softc->ha_wakeup);
557 mtx_unlock(&softc->ha_lock);
562 ctl_ha_peer_sysctl(SYSCTL_HANDLER_ARGS)
564 struct ha_softc *softc = (struct ha_softc *)arg1;
565 struct sockaddr_in *sa;
566 int error, b1, b2, b3, b4, p, num;
569 strlcpy(buf, softc->ha_peer, sizeof(buf));
570 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
571 if ((error != 0) || (req->newptr == NULL) ||
572 strncmp(buf, softc->ha_peer, sizeof(buf)) == 0)
575 sa = &softc->ha_peer_in;
576 mtx_lock(&softc->ha_lock);
577 if ((num = sscanf(buf, "connect %d.%d.%d.%d:%d",
578 &b1, &b2, &b3, &b4, &p)) >= 4) {
579 softc->ha_connect = 1;
580 softc->ha_listen = 0;
581 } else if ((num = sscanf(buf, "listen %d.%d.%d.%d:%d",
582 &b1, &b2, &b3, &b4, &p)) >= 4) {
583 softc->ha_connect = 0;
584 softc->ha_listen = 1;
586 softc->ha_connect = 0;
587 softc->ha_listen = 0;
593 strlcpy(softc->ha_peer, buf, sizeof(softc->ha_peer));
594 if (softc->ha_connect || softc->ha_listen) {
595 memset(sa, 0, sizeof(*sa));
596 sa->sin_len = sizeof(struct sockaddr_in);
597 sa->sin_family = AF_INET;
598 sa->sin_port = htons((num >= 5) ? p : 999);
599 sa->sin_addr.s_addr =
600 htonl((b1 << 24) + (b2 << 16) + (b3 << 8) + b4);
602 softc->ha_disconnect = 2;
603 softc->ha_wakeup = 1;
604 mtx_unlock(&softc->ha_lock);
605 wakeup(&softc->ha_wakeup);
610 ctl_ha_msg_register(ctl_ha_channel channel, ctl_evt_handler handler)
612 struct ha_softc *softc = &ha_softc;
614 KASSERT(channel < CTL_HA_CHAN_MAX,
615 ("Wrong CTL HA channel %d", channel));
616 softc->ha_handler[channel] = handler;
617 return (CTL_HA_STATUS_SUCCESS);
621 ctl_ha_msg_deregister(ctl_ha_channel channel)
623 struct ha_softc *softc = &ha_softc;
625 KASSERT(channel < CTL_HA_CHAN_MAX,
626 ("Wrong CTL HA channel %d", channel));
627 softc->ha_handler[channel] = NULL;
628 return (CTL_HA_STATUS_SUCCESS);
632 * Receive a message of the specified size.
635 ctl_ha_msg_recv(ctl_ha_channel channel, void *addr, size_t len,
638 struct ha_softc *softc = &ha_softc;
643 if (!softc->ha_connected)
644 return (CTL_HA_STATUS_DISCONNECT);
650 uio.uio_rw = UIO_READ;
651 uio.uio_segflg = UIO_SYSSPACE;
652 uio.uio_td = curthread;
654 flags = wait ? 0 : MSG_DONTWAIT;
655 error = soreceive(softc->ha_so, NULL, &uio, NULL, NULL, &flags);
657 return (CTL_HA_STATUS_SUCCESS);
659 /* Consider all errors fatal for HA sanity. */
660 mtx_lock(&softc->ha_lock);
661 if (softc->ha_connected) {
662 softc->ha_disconnect = 1;
663 softc->ha_wakeup = 1;
664 wakeup(&softc->ha_wakeup);
666 mtx_unlock(&softc->ha_lock);
667 return (CTL_HA_STATUS_ERROR);
671 * Send a message of the specified size.
674 ctl_ha_msg_send2(ctl_ha_channel channel, const void *addr, size_t len,
675 const void *addr2, size_t len2, int wait)
677 struct ha_softc *softc = &ha_softc;
678 struct mbuf *mb, *newmb;
679 struct ha_msg_wire hdr;
682 if (!softc->ha_connected)
683 return (CTL_HA_STATUS_DISCONNECT);
685 newmb = m_getm2(NULL, sizeof(hdr) + len + len2, wait, MT_DATA,
688 /* Consider all errors fatal for HA sanity. */
689 mtx_lock(&softc->ha_lock);
690 if (softc->ha_connected) {
691 softc->ha_disconnect = 1;
692 softc->ha_wakeup = 1;
693 wakeup(&softc->ha_wakeup);
695 mtx_unlock(&softc->ha_lock);
696 printf("%s: Can't allocate mbuf chain\n", __func__);
697 return (CTL_HA_STATUS_ERROR);
699 hdr.channel = channel;
700 hdr.length = len + len2;
702 memcpy(mtodo(mb, 0), &hdr, sizeof(hdr));
703 mb->m_len += sizeof(hdr);
705 for (; mb != NULL && off < len; mb = mb->m_next) {
706 copylen = min(M_TRAILINGSPACE(mb), len - off);
707 memcpy(mtodo(mb, mb->m_len), (const char *)addr + off, copylen);
708 mb->m_len += copylen;
713 KASSERT(off == len, ("%s: off (%zu) != len (%zu)", __func__,
716 for (; mb != NULL && off < len2; mb = mb->m_next) {
717 copylen = min(M_TRAILINGSPACE(mb), len2 - off);
718 memcpy(mtodo(mb, mb->m_len), (const char *)addr2 + off, copylen);
719 mb->m_len += copylen;
722 KASSERT(off == len2, ("%s: off (%zu) != len2 (%zu)", __func__,
724 newmb->m_pkthdr.len = sizeof(hdr) + len + len2;
726 mtx_lock(&softc->ha_lock);
727 if (!softc->ha_connected) {
728 mtx_unlock(&softc->ha_lock);
730 return (CTL_HA_STATUS_DISCONNECT);
732 mbufq_enqueue(&softc->ha_sendq, newmb);
733 softc->ha_wakeup = 1;
734 mtx_unlock(&softc->ha_lock);
735 wakeup(&softc->ha_wakeup);
736 return (CTL_HA_STATUS_SUCCESS);
740 ctl_ha_msg_send(ctl_ha_channel channel, const void *addr, size_t len,
744 return (ctl_ha_msg_send2(channel, addr, len, NULL, 0, wait));
748 ctl_ha_msg_abort(ctl_ha_channel channel)
750 struct ha_softc *softc = &ha_softc;
752 mtx_lock(&softc->ha_lock);
753 softc->ha_disconnect = 1;
754 softc->ha_wakeup = 1;
755 mtx_unlock(&softc->ha_lock);
756 wakeup(&softc->ha_wakeup);
757 return (CTL_HA_STATUS_SUCCESS);
761 * Allocate a data transfer request structure.
763 struct ctl_ha_dt_req *
764 ctl_dt_req_alloc(void)
767 return (malloc(sizeof(struct ctl_ha_dt_req), M_CTL, M_WAITOK | M_ZERO));
771 * Free a data transfer request structure.
774 ctl_dt_req_free(struct ctl_ha_dt_req *req)
781 * Issue a DMA request for a single buffer.
784 ctl_dt_single(struct ctl_ha_dt_req *req)
786 struct ha_softc *softc = &ha_softc;
787 struct ha_dt_msg_wire wire_dt;
788 ctl_ha_status status;
790 wire_dt.command = req->command;
791 wire_dt.size = req->size;
792 wire_dt.local = req->local;
793 wire_dt.remote = req->remote;
794 if (req->command == CTL_HA_DT_CMD_READ && req->callback != NULL) {
795 mtx_lock(&softc->ha_lock);
796 TAILQ_INSERT_TAIL(&softc->ha_dts, req, links);
797 mtx_unlock(&softc->ha_lock);
798 ctl_ha_msg_send(CTL_HA_CHAN_DATA, &wire_dt, sizeof(wire_dt),
800 return (CTL_HA_STATUS_WAIT);
802 if (req->command == CTL_HA_DT_CMD_READ) {
803 status = ctl_ha_msg_send(CTL_HA_CHAN_DATA, &wire_dt,
804 sizeof(wire_dt), M_WAITOK);
806 status = ctl_ha_msg_send2(CTL_HA_CHAN_DATA, &wire_dt,
807 sizeof(wire_dt), req->local, req->size, M_WAITOK);
813 ctl_dt_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
815 struct ha_softc *softc = &ha_softc;
816 struct ctl_ha_dt_req *req;
817 ctl_ha_status isc_status;
819 if (event == CTL_HA_EVT_MSG_RECV) {
820 struct ha_dt_msg_wire wire_dt;
824 size = min(sizeof(wire_dt), param);
825 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_DATA, &wire_dt,
827 if (isc_status != CTL_HA_STATUS_SUCCESS) {
828 printf("%s: Error receiving message: %d\n",
829 __func__, isc_status);
833 if (wire_dt.command == CTL_HA_DT_CMD_READ) {
834 wire_dt.command = CTL_HA_DT_CMD_WRITE;
836 wire_dt.local = wire_dt.remote;
837 wire_dt.remote = tmp;
838 ctl_ha_msg_send2(CTL_HA_CHAN_DATA, &wire_dt,
839 sizeof(wire_dt), wire_dt.local, wire_dt.size,
841 } else if (wire_dt.command == CTL_HA_DT_CMD_WRITE) {
842 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_DATA,
843 wire_dt.remote, wire_dt.size, M_WAITOK);
844 mtx_lock(&softc->ha_lock);
845 TAILQ_FOREACH(req, &softc->ha_dts, links) {
846 if (req->local == wire_dt.remote) {
847 TAILQ_REMOVE(&softc->ha_dts, req, links);
851 mtx_unlock(&softc->ha_lock);
853 req->ret = isc_status;
857 } else if (event == CTL_HA_EVT_LINK_CHANGE) {
858 CTL_DEBUG_PRINT(("%s: Link state change to %d\n", __func__,
860 if (param != CTL_HA_LINK_ONLINE) {
861 mtx_lock(&softc->ha_lock);
862 while ((req = TAILQ_FIRST(&softc->ha_dts)) != NULL) {
863 TAILQ_REMOVE(&softc->ha_dts, req, links);
864 mtx_unlock(&softc->ha_lock);
865 req->ret = CTL_HA_STATUS_DISCONNECT;
867 mtx_lock(&softc->ha_lock);
869 mtx_unlock(&softc->ha_lock);
872 printf("%s: Unknown event %d\n", __func__, event);
878 ctl_ha_msg_init(struct ctl_softc *ctl_softc)
880 struct ha_softc *softc = &ha_softc;
883 softc->ha_ctl_softc = ctl_softc;
884 mtx_init(&softc->ha_lock, "CTL HA mutex", NULL, MTX_DEF);
885 mbufq_init(&softc->ha_sendq, INT_MAX);
886 TAILQ_INIT(&softc->ha_dts);
887 error = kproc_kthread_add(ctl_ha_conn_thread, softc,
888 &ctl_softc->ctl_proc, NULL, 0, 0, "ctl", "ha_tx");
890 printf("error creating CTL HA connection thread!\n");
891 mtx_destroy(&softc->ha_lock);
892 return (CTL_HA_STATUS_ERROR);
894 softc->ha_shutdown_eh = EVENTHANDLER_REGISTER(shutdown_pre_sync,
895 ctl_ha_msg_shutdown, ctl_softc, SHUTDOWN_PRI_FIRST);
896 SYSCTL_ADD_PROC(&ctl_softc->sysctl_ctx,
897 SYSCTL_CHILDREN(ctl_softc->sysctl_tree),
899 CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT,
900 softc, 0, ctl_ha_peer_sysctl, "A", "HA peer connection method");
902 if (ctl_ha_msg_register(CTL_HA_CHAN_DATA, ctl_dt_event_handler)
903 != CTL_HA_STATUS_SUCCESS) {
904 printf("%s: ctl_ha_msg_register failed.\n", __func__);
907 return (CTL_HA_STATUS_SUCCESS);
911 ctl_ha_msg_shutdown(struct ctl_softc *ctl_softc)
913 struct ha_softc *softc = &ha_softc;
915 /* Disconnect and shutdown threads. */
916 mtx_lock(&softc->ha_lock);
917 if (softc->ha_shutdown < 2) {
918 softc->ha_shutdown = 1;
919 softc->ha_wakeup = 1;
920 wakeup(&softc->ha_wakeup);
921 while (softc->ha_shutdown < 2 && !SCHEDULER_STOPPED()) {
922 msleep(&softc->ha_wakeup, &softc->ha_lock, 0,
926 mtx_unlock(&softc->ha_lock);
930 ctl_ha_msg_destroy(struct ctl_softc *ctl_softc)
932 struct ha_softc *softc = &ha_softc;
934 if (softc->ha_shutdown_eh != NULL) {
935 EVENTHANDLER_DEREGISTER(shutdown_pre_sync,
936 softc->ha_shutdown_eh);
937 softc->ha_shutdown_eh = NULL;
940 ctl_ha_msg_shutdown(ctl_softc); /* Just in case. */
942 if (ctl_ha_msg_deregister(CTL_HA_CHAN_DATA) != CTL_HA_STATUS_SUCCESS)
943 printf("%s: ctl_ha_msg_deregister failed.\n", __func__);
945 mtx_destroy(&softc->ha_lock);
946 return (CTL_HA_STATUS_SUCCESS);