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>
74 struct ha_dt_msg_wire {
75 ctl_ha_dt_cmd command;
82 struct ctl_softc *ha_ctl_softc;
83 ctl_evt_handler ha_handler[CTL_HA_CHAN_MAX];
85 struct sockaddr_in ha_peer_in;
86 struct socket *ha_lso;
88 struct mbufq ha_sendq;
89 struct mbuf *ha_sending;
98 eventhandler_tag ha_shutdown_eh;
99 TAILQ_HEAD(, ctl_ha_dt_req) ha_dts;
103 ctl_ha_conn_wake(struct ha_softc *softc)
106 mtx_lock(&softc->ha_lock);
107 softc->ha_wakeup = 1;
108 mtx_unlock(&softc->ha_lock);
109 wakeup(&softc->ha_wakeup);
113 ctl_ha_lupcall(struct socket *so, void *arg, int waitflag)
115 struct ha_softc *softc = arg;
117 ctl_ha_conn_wake(softc);
122 ctl_ha_rupcall(struct socket *so, void *arg, int waitflag)
124 struct ha_softc *softc = arg;
126 wakeup(&softc->ha_receiving);
131 ctl_ha_supcall(struct socket *so, void *arg, int waitflag)
133 struct ha_softc *softc = arg;
135 ctl_ha_conn_wake(softc);
140 ctl_ha_evt(struct ha_softc *softc, ctl_ha_channel ch, ctl_ha_event evt,
145 if (ch < CTL_HA_CHAN_MAX) {
146 if (softc->ha_handler[ch])
147 softc->ha_handler[ch](ch, evt, param);
150 for (i = 0; i < CTL_HA_CHAN_MAX; i++) {
151 if (softc->ha_handler[i])
152 softc->ha_handler[i](i, evt, param);
157 ctl_ha_close(struct ha_softc *softc)
159 struct socket *so = softc->ha_so;
162 if (softc->ha_connected || softc->ha_disconnect) {
163 softc->ha_connected = 0;
164 mbufq_drain(&softc->ha_sendq);
165 m_freem(softc->ha_sending);
166 softc->ha_sending = NULL;
170 SOCKBUF_LOCK(&so->so_rcv);
171 soupcall_clear(so, SO_RCV);
172 while (softc->ha_receiving) {
173 wakeup(&softc->ha_receiving);
174 msleep(&softc->ha_receiving, SOCKBUF_MTX(&so->so_rcv),
177 SOCKBUF_UNLOCK(&so->so_rcv);
178 SOCKBUF_LOCK(&so->so_snd);
179 soupcall_clear(so, SO_SND);
180 SOCKBUF_UNLOCK(&so->so_snd);
182 if (softc->ha_connect)
183 pause("reconnect", hz / 2);
187 ctl_ha_evt(softc, CTL_HA_CHAN_MAX, CTL_HA_EVT_LINK_CHANGE,
188 (softc->ha_connect || softc->ha_listen) ?
189 CTL_HA_LINK_UNKNOWN : CTL_HA_LINK_OFFLINE);
194 ctl_ha_lclose(struct ha_softc *softc)
198 if (SOLISTENING(softc->ha_lso)) {
199 SOLISTEN_LOCK(softc->ha_lso);
200 solisten_upcall_set(softc->ha_lso, NULL, NULL);
201 SOLISTEN_UNLOCK(softc->ha_lso);
203 soclose(softc->ha_lso);
204 softc->ha_lso = NULL;
209 ctl_ha_rx_thread(void *arg)
211 struct ha_softc *softc = arg;
212 struct socket *so = softc->ha_so;
213 struct ha_msg_wire wire_hdr;
216 int error, flags, next;
218 bzero(&wire_hdr, sizeof(wire_hdr));
220 if (wire_hdr.length > 0)
221 next = wire_hdr.length;
223 next = sizeof(wire_hdr);
224 SOCKBUF_LOCK(&so->so_rcv);
225 while (sbavail(&so->so_rcv) < next || softc->ha_disconnect) {
226 if (softc->ha_connected == 0 || softc->ha_disconnect ||
228 (so->so_rcv.sb_state & SBS_CANTRCVMORE)) {
231 so->so_rcv.sb_lowat = next;
232 msleep(&softc->ha_receiving, SOCKBUF_MTX(&so->so_rcv),
235 SOCKBUF_UNLOCK(&so->so_rcv);
237 if (wire_hdr.length == 0) {
238 iov.iov_base = &wire_hdr;
239 iov.iov_len = sizeof(wire_hdr);
242 uio.uio_rw = UIO_READ;
243 uio.uio_segflg = UIO_SYSSPACE;
244 uio.uio_td = curthread;
245 uio.uio_resid = sizeof(wire_hdr);
246 flags = MSG_DONTWAIT;
247 error = soreceive(softc->ha_so, NULL, &uio, NULL,
250 printf("%s: header receive error %d\n",
252 SOCKBUF_LOCK(&so->so_rcv);
256 ctl_ha_evt(softc, wire_hdr.channel,
257 CTL_HA_EVT_MSG_RECV, wire_hdr.length);
263 softc->ha_receiving = 0;
264 wakeup(&softc->ha_receiving);
265 SOCKBUF_UNLOCK(&so->so_rcv);
266 ctl_ha_conn_wake(softc);
271 ctl_ha_send(struct ha_softc *softc)
273 struct socket *so = softc->ha_so;
277 if (softc->ha_sending == NULL) {
278 mtx_lock(&softc->ha_lock);
279 softc->ha_sending = mbufq_dequeue(&softc->ha_sendq);
280 mtx_unlock(&softc->ha_lock);
281 if (softc->ha_sending == NULL) {
282 so->so_snd.sb_lowat = so->so_snd.sb_hiwat + 1;
286 SOCKBUF_LOCK(&so->so_snd);
287 if (sbspace(&so->so_snd) < softc->ha_sending->m_pkthdr.len) {
288 so->so_snd.sb_lowat = softc->ha_sending->m_pkthdr.len;
289 SOCKBUF_UNLOCK(&so->so_snd);
292 SOCKBUF_UNLOCK(&so->so_snd);
293 error = sosend(softc->ha_so, NULL, NULL, softc->ha_sending,
294 NULL, MSG_DONTWAIT, curthread);
295 softc->ha_sending = NULL;
297 printf("%s: sosend() error %d\n", __func__, error);
304 ctl_ha_sock_setup(struct ha_softc *softc)
307 struct socket *so = softc->ha_so;
311 error = soreserve(so, val, val);
313 printf("%s: soreserve failed %d\n", __func__, error);
315 SOCKBUF_LOCK(&so->so_rcv);
316 so->so_rcv.sb_lowat = sizeof(struct ha_msg_wire);
317 soupcall_set(so, SO_RCV, ctl_ha_rupcall, softc);
318 SOCKBUF_UNLOCK(&so->so_rcv);
319 SOCKBUF_LOCK(&so->so_snd);
320 so->so_snd.sb_lowat = sizeof(struct ha_msg_wire);
321 soupcall_set(so, SO_SND, ctl_ha_supcall, softc);
322 SOCKBUF_UNLOCK(&so->so_snd);
324 bzero(&opt, sizeof(struct sockopt));
325 opt.sopt_dir = SOPT_SET;
326 opt.sopt_level = SOL_SOCKET;
327 opt.sopt_name = SO_KEEPALIVE;
329 opt.sopt_valsize = sizeof(val);
331 error = sosetopt(so, &opt);
333 printf("%s: KEEPALIVE setting failed %d\n", __func__, error);
335 opt.sopt_level = IPPROTO_TCP;
336 opt.sopt_name = TCP_NODELAY;
338 error = sosetopt(so, &opt);
340 printf("%s: NODELAY setting failed %d\n", __func__, error);
342 opt.sopt_name = TCP_KEEPINIT;
344 error = sosetopt(so, &opt);
346 printf("%s: KEEPINIT setting failed %d\n", __func__, error);
348 opt.sopt_name = TCP_KEEPIDLE;
350 error = sosetopt(so, &opt);
352 printf("%s: KEEPIDLE setting failed %d\n", __func__, error);
354 opt.sopt_name = TCP_KEEPINTVL;
356 error = sosetopt(so, &opt);
358 printf("%s: KEEPINTVL setting failed %d\n", __func__, error);
360 opt.sopt_name = TCP_KEEPCNT;
362 error = sosetopt(so, &opt);
364 printf("%s: KEEPCNT setting failed %d\n", __func__, error);
368 ctl_ha_connect(struct ha_softc *softc)
370 struct thread *td = curthread;
371 struct sockaddr_in sa;
375 /* Create the socket */
376 error = socreate(PF_INET, &so, SOCK_STREAM,
377 IPPROTO_TCP, td->td_ucred, td);
379 printf("%s: socreate() error %d\n", __func__, error);
383 ctl_ha_sock_setup(softc);
385 memcpy(&sa, &softc->ha_peer_in, sizeof(sa));
386 error = soconnect(so, (struct sockaddr *)&sa, td);
389 printf("%s: soconnect() error %d\n", __func__, error);
400 ctl_ha_accept(struct ha_softc *softc)
402 struct socket *lso, *so;
403 struct sockaddr *sap;
408 error = solisten_dequeue(lso, &so, 0);
409 if (error == EWOULDBLOCK)
412 printf("%s: socket error %d\n", __func__, error);
417 error = soaccept(so, &sap);
419 printf("%s: soaccept() error %d\n", __func__, error);
427 ctl_ha_sock_setup(softc);
431 ctl_ha_lclose(softc);
436 ctl_ha_listen(struct ha_softc *softc)
438 struct thread *td = curthread;
439 struct sockaddr_in sa;
443 /* Create the socket */
444 if (softc->ha_lso == NULL) {
445 error = socreate(PF_INET, &softc->ha_lso, SOCK_STREAM,
446 IPPROTO_TCP, td->td_ucred, td);
448 printf("%s: socreate() error %d\n", __func__, error);
451 bzero(&opt, sizeof(struct sockopt));
452 opt.sopt_dir = SOPT_SET;
453 opt.sopt_level = SOL_SOCKET;
454 opt.sopt_name = SO_REUSEADDR;
456 opt.sopt_valsize = sizeof(val);
458 error = sosetopt(softc->ha_lso, &opt);
460 printf("%s: REUSEADDR setting failed %d\n",
463 bzero(&opt, sizeof(struct sockopt));
464 opt.sopt_dir = SOPT_SET;
465 opt.sopt_level = SOL_SOCKET;
466 opt.sopt_name = SO_REUSEPORT;
468 opt.sopt_valsize = sizeof(val);
470 error = sosetopt(softc->ha_lso, &opt);
472 printf("%s: REUSEPORT setting failed %d\n",
477 memcpy(&sa, &softc->ha_peer_in, sizeof(sa));
478 error = sobind(softc->ha_lso, (struct sockaddr *)&sa, td);
480 printf("%s: sobind() error %d\n", __func__, error);
483 error = solisten(softc->ha_lso, 1, td);
485 printf("%s: solisten() error %d\n", __func__, error);
488 SOLISTEN_LOCK(softc->ha_lso);
489 softc->ha_lso->so_state |= SS_NBIO;
490 solisten_upcall_set(softc->ha_lso, ctl_ha_lupcall, softc);
491 SOLISTEN_UNLOCK(softc->ha_lso);
495 ctl_ha_lclose(softc);
500 ctl_ha_conn_thread(void *arg)
502 struct ha_softc *softc = arg;
506 if (softc->ha_disconnect || softc->ha_shutdown) {
508 if (softc->ha_disconnect == 2 || softc->ha_shutdown)
509 ctl_ha_lclose(softc);
510 softc->ha_disconnect = 0;
511 if (softc->ha_shutdown)
513 } else if (softc->ha_so != NULL &&
514 (softc->ha_so->so_error ||
515 softc->ha_so->so_rcv.sb_state & SBS_CANTRCVMORE))
517 if (softc->ha_so == NULL) {
518 if (softc->ha_lso != NULL)
519 ctl_ha_accept(softc);
520 else if (softc->ha_listen)
521 ctl_ha_listen(softc);
522 else if (softc->ha_connect)
523 ctl_ha_connect(softc);
525 if (softc->ha_so != NULL) {
526 if (softc->ha_connected == 0 &&
527 softc->ha_so->so_error == 0 &&
528 (softc->ha_so->so_state & SS_ISCONNECTING) == 0) {
529 softc->ha_connected = 1;
530 ctl_ha_evt(softc, CTL_HA_CHAN_MAX,
531 CTL_HA_EVT_LINK_CHANGE,
533 softc->ha_receiving = 1;
534 error = kproc_kthread_add(ctl_ha_rx_thread,
535 softc, &softc->ha_ctl_softc->ctl_proc,
536 NULL, 0, 0, "ctl", "ha_rx");
538 printf("Error creating CTL HA rx thread!\n");
539 softc->ha_receiving = 0;
540 softc->ha_disconnect = 1;
545 mtx_lock(&softc->ha_lock);
546 if (softc->ha_so != NULL &&
547 (softc->ha_so->so_error ||
548 softc->ha_so->so_rcv.sb_state & SBS_CANTRCVMORE))
550 else if (!softc->ha_wakeup)
551 msleep(&softc->ha_wakeup, &softc->ha_lock, 0, "-", hz);
552 softc->ha_wakeup = 0;
553 mtx_unlock(&softc->ha_lock);
555 mtx_lock(&softc->ha_lock);
556 softc->ha_shutdown = 2;
557 wakeup(&softc->ha_wakeup);
558 mtx_unlock(&softc->ha_lock);
563 ctl_ha_peer_sysctl(SYSCTL_HANDLER_ARGS)
565 struct ha_softc *softc = (struct ha_softc *)arg1;
566 struct sockaddr_in *sa;
567 int error, b1, b2, b3, b4, p, num;
570 strlcpy(buf, softc->ha_peer, sizeof(buf));
571 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
572 if ((error != 0) || (req->newptr == NULL) ||
573 strncmp(buf, softc->ha_peer, sizeof(buf)) == 0)
576 sa = &softc->ha_peer_in;
577 mtx_lock(&softc->ha_lock);
578 if ((num = sscanf(buf, "connect %d.%d.%d.%d:%d",
579 &b1, &b2, &b3, &b4, &p)) >= 4) {
580 softc->ha_connect = 1;
581 softc->ha_listen = 0;
582 } else if ((num = sscanf(buf, "listen %d.%d.%d.%d:%d",
583 &b1, &b2, &b3, &b4, &p)) >= 4) {
584 softc->ha_connect = 0;
585 softc->ha_listen = 1;
587 softc->ha_connect = 0;
588 softc->ha_listen = 0;
594 strlcpy(softc->ha_peer, buf, sizeof(softc->ha_peer));
595 if (softc->ha_connect || softc->ha_listen) {
596 memset(sa, 0, sizeof(*sa));
597 sa->sin_len = sizeof(struct sockaddr_in);
598 sa->sin_family = AF_INET;
599 sa->sin_port = htons((num >= 5) ? p : 999);
600 sa->sin_addr.s_addr =
601 htonl((b1 << 24) + (b2 << 16) + (b3 << 8) + b4);
603 softc->ha_disconnect = 2;
604 softc->ha_wakeup = 1;
605 mtx_unlock(&softc->ha_lock);
606 wakeup(&softc->ha_wakeup);
611 ctl_ha_msg_register(ctl_ha_channel channel, ctl_evt_handler handler)
613 struct ha_softc *softc = &ha_softc;
615 KASSERT(channel < CTL_HA_CHAN_MAX,
616 ("Wrong CTL HA channel %d", channel));
617 softc->ha_handler[channel] = handler;
618 return (CTL_HA_STATUS_SUCCESS);
622 ctl_ha_msg_deregister(ctl_ha_channel channel)
624 struct ha_softc *softc = &ha_softc;
626 KASSERT(channel < CTL_HA_CHAN_MAX,
627 ("Wrong CTL HA channel %d", channel));
628 softc->ha_handler[channel] = NULL;
629 return (CTL_HA_STATUS_SUCCESS);
633 * Receive a message of the specified size.
636 ctl_ha_msg_recv(ctl_ha_channel channel, void *addr, size_t len,
639 struct ha_softc *softc = &ha_softc;
644 if (!softc->ha_connected)
645 return (CTL_HA_STATUS_DISCONNECT);
651 uio.uio_rw = UIO_READ;
652 uio.uio_segflg = UIO_SYSSPACE;
653 uio.uio_td = curthread;
655 flags = wait ? 0 : MSG_DONTWAIT;
656 error = soreceive(softc->ha_so, NULL, &uio, NULL, NULL, &flags);
658 return (CTL_HA_STATUS_SUCCESS);
660 /* Consider all errors fatal for HA sanity. */
661 mtx_lock(&softc->ha_lock);
662 if (softc->ha_connected) {
663 softc->ha_disconnect = 1;
664 softc->ha_wakeup = 1;
665 wakeup(&softc->ha_wakeup);
667 mtx_unlock(&softc->ha_lock);
668 return (CTL_HA_STATUS_ERROR);
672 * Send a message of the specified size.
675 ctl_ha_msg_send2(ctl_ha_channel channel, const void *addr, size_t len,
676 const void *addr2, size_t len2, int wait)
678 struct ha_softc *softc = &ha_softc;
679 struct mbuf *mb, *newmb;
680 struct ha_msg_wire hdr;
683 if (!softc->ha_connected)
684 return (CTL_HA_STATUS_DISCONNECT);
686 newmb = m_getm2(NULL, sizeof(hdr) + len + len2, wait, MT_DATA,
689 /* Consider all errors fatal for HA sanity. */
690 mtx_lock(&softc->ha_lock);
691 if (softc->ha_connected) {
692 softc->ha_disconnect = 1;
693 softc->ha_wakeup = 1;
694 wakeup(&softc->ha_wakeup);
696 mtx_unlock(&softc->ha_lock);
697 printf("%s: Can't allocate mbuf chain\n", __func__);
698 return (CTL_HA_STATUS_ERROR);
700 hdr.channel = channel;
701 hdr.length = len + len2;
703 memcpy(mtodo(mb, 0), &hdr, sizeof(hdr));
704 mb->m_len += sizeof(hdr);
706 for (; mb != NULL && off < len; mb = mb->m_next) {
707 copylen = min(M_TRAILINGSPACE(mb), len - off);
708 memcpy(mtodo(mb, mb->m_len), (const char *)addr + off, copylen);
709 mb->m_len += copylen;
714 KASSERT(off == len, ("%s: off (%zu) != len (%zu)", __func__,
717 for (; mb != NULL && off < len2; mb = mb->m_next) {
718 copylen = min(M_TRAILINGSPACE(mb), len2 - off);
719 memcpy(mtodo(mb, mb->m_len), (const char *)addr2 + off, copylen);
720 mb->m_len += copylen;
723 KASSERT(off == len2, ("%s: off (%zu) != len2 (%zu)", __func__,
725 newmb->m_pkthdr.len = sizeof(hdr) + len + len2;
727 mtx_lock(&softc->ha_lock);
728 if (!softc->ha_connected) {
729 mtx_unlock(&softc->ha_lock);
731 return (CTL_HA_STATUS_DISCONNECT);
733 mbufq_enqueue(&softc->ha_sendq, newmb);
734 softc->ha_wakeup = 1;
735 mtx_unlock(&softc->ha_lock);
736 wakeup(&softc->ha_wakeup);
737 return (CTL_HA_STATUS_SUCCESS);
741 ctl_ha_msg_send(ctl_ha_channel channel, const void *addr, size_t len,
745 return (ctl_ha_msg_send2(channel, addr, len, NULL, 0, wait));
749 ctl_ha_msg_abort(ctl_ha_channel channel)
751 struct ha_softc *softc = &ha_softc;
753 mtx_lock(&softc->ha_lock);
754 softc->ha_disconnect = 1;
755 softc->ha_wakeup = 1;
756 mtx_unlock(&softc->ha_lock);
757 wakeup(&softc->ha_wakeup);
758 return (CTL_HA_STATUS_SUCCESS);
762 * Allocate a data transfer request structure.
764 struct ctl_ha_dt_req *
765 ctl_dt_req_alloc(void)
768 return (malloc(sizeof(struct ctl_ha_dt_req), M_CTL, M_WAITOK | M_ZERO));
772 * Free a data transfer request structure.
775 ctl_dt_req_free(struct ctl_ha_dt_req *req)
782 * Issue a DMA request for a single buffer.
785 ctl_dt_single(struct ctl_ha_dt_req *req)
787 struct ha_softc *softc = &ha_softc;
788 struct ha_dt_msg_wire wire_dt;
789 ctl_ha_status status;
791 wire_dt.command = req->command;
792 wire_dt.size = req->size;
793 wire_dt.local = req->local;
794 wire_dt.remote = req->remote;
795 if (req->command == CTL_HA_DT_CMD_READ && req->callback != NULL) {
796 mtx_lock(&softc->ha_lock);
797 TAILQ_INSERT_TAIL(&softc->ha_dts, req, links);
798 mtx_unlock(&softc->ha_lock);
799 ctl_ha_msg_send(CTL_HA_CHAN_DATA, &wire_dt, sizeof(wire_dt),
801 return (CTL_HA_STATUS_WAIT);
803 if (req->command == CTL_HA_DT_CMD_READ) {
804 status = ctl_ha_msg_send(CTL_HA_CHAN_DATA, &wire_dt,
805 sizeof(wire_dt), M_WAITOK);
807 status = ctl_ha_msg_send2(CTL_HA_CHAN_DATA, &wire_dt,
808 sizeof(wire_dt), req->local, req->size, M_WAITOK);
814 ctl_dt_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
816 struct ha_softc *softc = &ha_softc;
817 struct ctl_ha_dt_req *req;
818 ctl_ha_status isc_status;
820 if (event == CTL_HA_EVT_MSG_RECV) {
821 struct ha_dt_msg_wire wire_dt;
825 size = min(sizeof(wire_dt), param);
826 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_DATA, &wire_dt,
828 if (isc_status != CTL_HA_STATUS_SUCCESS) {
829 printf("%s: Error receiving message: %d\n",
830 __func__, isc_status);
834 if (wire_dt.command == CTL_HA_DT_CMD_READ) {
835 wire_dt.command = CTL_HA_DT_CMD_WRITE;
837 wire_dt.local = wire_dt.remote;
838 wire_dt.remote = tmp;
839 ctl_ha_msg_send2(CTL_HA_CHAN_DATA, &wire_dt,
840 sizeof(wire_dt), wire_dt.local, wire_dt.size,
842 } else if (wire_dt.command == CTL_HA_DT_CMD_WRITE) {
843 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_DATA,
844 wire_dt.remote, wire_dt.size, M_WAITOK);
845 mtx_lock(&softc->ha_lock);
846 TAILQ_FOREACH(req, &softc->ha_dts, links) {
847 if (req->local == wire_dt.remote) {
848 TAILQ_REMOVE(&softc->ha_dts, req, links);
852 mtx_unlock(&softc->ha_lock);
854 req->ret = isc_status;
858 } else if (event == CTL_HA_EVT_LINK_CHANGE) {
859 CTL_DEBUG_PRINT(("%s: Link state change to %d\n", __func__,
861 if (param != CTL_HA_LINK_ONLINE) {
862 mtx_lock(&softc->ha_lock);
863 while ((req = TAILQ_FIRST(&softc->ha_dts)) != NULL) {
864 TAILQ_REMOVE(&softc->ha_dts, req, links);
865 mtx_unlock(&softc->ha_lock);
866 req->ret = CTL_HA_STATUS_DISCONNECT;
868 mtx_lock(&softc->ha_lock);
870 mtx_unlock(&softc->ha_lock);
873 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);