2 * SPDX-License-Identifier: BSD-2-Clause
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/param.h>
30 #include <sys/condvar.h>
32 #include <sys/eventhandler.h>
33 #include <sys/kernel.h>
34 #include <sys/kthread.h>
35 #include <sys/limits.h>
37 #include <sys/malloc.h>
39 #include <sys/module.h>
40 #include <sys/mutex.h>
42 #include <sys/queue.h>
43 #include <sys/socket.h>
44 #include <sys/socketvar.h>
45 #include <sys/sysctl.h>
46 #include <sys/systm.h>
48 #include <netinet/in.h>
49 #include <netinet/tcp.h>
53 #include <cam/scsi/scsi_all.h>
54 #include <cam/scsi/scsi_da.h>
55 #include <cam/ctl/ctl_io.h>
56 #include <cam/ctl/ctl.h>
57 #include <cam/ctl/ctl_frontend.h>
58 #include <cam/ctl/ctl_util.h>
59 #include <cam/ctl/ctl_backend.h>
60 #include <cam/ctl/ctl_ioctl.h>
61 #include <cam/ctl/ctl_ha.h>
62 #include <cam/ctl/ctl_private.h>
63 #include <cam/ctl/ctl_debug.h>
64 #include <cam/ctl/ctl_error.h>
71 struct ha_dt_msg_wire {
72 ctl_ha_dt_cmd command;
79 struct ctl_softc *ha_ctl_softc;
80 ctl_evt_handler ha_handler[CTL_HA_CHAN_MAX];
82 struct sockaddr_in ha_peer_in;
83 struct socket *ha_lso;
85 struct mbufq ha_sendq;
86 struct mbuf *ha_sending;
95 eventhandler_tag ha_shutdown_eh;
96 TAILQ_HEAD(, ctl_ha_dt_req) ha_dts;
100 ctl_ha_conn_wake(struct ha_softc *softc)
103 mtx_lock(&softc->ha_lock);
104 softc->ha_wakeup = 1;
105 mtx_unlock(&softc->ha_lock);
106 wakeup(&softc->ha_wakeup);
110 ctl_ha_lupcall(struct socket *so, void *arg, int waitflag)
112 struct ha_softc *softc = arg;
114 ctl_ha_conn_wake(softc);
119 ctl_ha_rupcall(struct socket *so, void *arg, int waitflag)
121 struct ha_softc *softc = arg;
123 wakeup(&softc->ha_receiving);
128 ctl_ha_supcall(struct socket *so, void *arg, int waitflag)
130 struct ha_softc *softc = arg;
132 ctl_ha_conn_wake(softc);
137 ctl_ha_evt(struct ha_softc *softc, ctl_ha_channel ch, ctl_ha_event evt,
142 if (ch < CTL_HA_CHAN_MAX) {
143 if (softc->ha_handler[ch])
144 softc->ha_handler[ch](ch, evt, param);
147 for (i = 0; i < CTL_HA_CHAN_MAX; i++) {
148 if (softc->ha_handler[i])
149 softc->ha_handler[i](i, evt, param);
154 ctl_ha_close(struct ha_softc *softc)
156 struct socket *so = softc->ha_so;
159 if (softc->ha_connected || softc->ha_disconnect) {
160 softc->ha_connected = 0;
161 mbufq_drain(&softc->ha_sendq);
162 m_freem(softc->ha_sending);
163 softc->ha_sending = NULL;
167 SOCKBUF_LOCK(&so->so_rcv);
168 soupcall_clear(so, SO_RCV);
169 while (softc->ha_receiving) {
170 wakeup(&softc->ha_receiving);
171 msleep(&softc->ha_receiving, SOCKBUF_MTX(&so->so_rcv),
174 SOCKBUF_UNLOCK(&so->so_rcv);
175 SOCKBUF_LOCK(&so->so_snd);
176 soupcall_clear(so, SO_SND);
177 SOCKBUF_UNLOCK(&so->so_snd);
179 if (softc->ha_connect)
180 pause("reconnect", hz / 2);
184 ctl_ha_evt(softc, CTL_HA_CHAN_MAX, CTL_HA_EVT_LINK_CHANGE,
185 (softc->ha_connect || softc->ha_listen) ?
186 CTL_HA_LINK_UNKNOWN : CTL_HA_LINK_OFFLINE);
191 ctl_ha_lclose(struct ha_softc *softc)
195 if (SOLISTENING(softc->ha_lso)) {
196 SOLISTEN_LOCK(softc->ha_lso);
197 solisten_upcall_set(softc->ha_lso, NULL, NULL);
198 SOLISTEN_UNLOCK(softc->ha_lso);
200 soclose(softc->ha_lso);
201 softc->ha_lso = NULL;
206 ctl_ha_rx_thread(void *arg)
208 struct ha_softc *softc = arg;
209 struct socket *so = softc->ha_so;
210 struct ha_msg_wire wire_hdr;
213 int error, flags, next;
215 bzero(&wire_hdr, sizeof(wire_hdr));
217 if (wire_hdr.length > 0)
218 next = wire_hdr.length;
220 next = sizeof(wire_hdr);
221 SOCKBUF_LOCK(&so->so_rcv);
222 while (sbavail(&so->so_rcv) < next || softc->ha_disconnect) {
223 if (softc->ha_connected == 0 || softc->ha_disconnect ||
225 (so->so_rcv.sb_state & SBS_CANTRCVMORE)) {
228 so->so_rcv.sb_lowat = next;
229 msleep(&softc->ha_receiving, SOCKBUF_MTX(&so->so_rcv),
232 SOCKBUF_UNLOCK(&so->so_rcv);
234 if (wire_hdr.length == 0) {
235 iov.iov_base = &wire_hdr;
236 iov.iov_len = sizeof(wire_hdr);
239 uio.uio_rw = UIO_READ;
240 uio.uio_segflg = UIO_SYSSPACE;
241 uio.uio_td = curthread;
242 uio.uio_resid = sizeof(wire_hdr);
243 flags = MSG_DONTWAIT;
244 error = soreceive(softc->ha_so, NULL, &uio, NULL,
247 printf("%s: header receive error %d\n",
249 SOCKBUF_LOCK(&so->so_rcv);
253 ctl_ha_evt(softc, wire_hdr.channel,
254 CTL_HA_EVT_MSG_RECV, wire_hdr.length);
260 softc->ha_receiving = 0;
261 wakeup(&softc->ha_receiving);
262 SOCKBUF_UNLOCK(&so->so_rcv);
263 ctl_ha_conn_wake(softc);
268 ctl_ha_send(struct ha_softc *softc)
270 struct socket *so = softc->ha_so;
274 if (softc->ha_sending == NULL) {
275 mtx_lock(&softc->ha_lock);
276 softc->ha_sending = mbufq_dequeue(&softc->ha_sendq);
277 mtx_unlock(&softc->ha_lock);
278 if (softc->ha_sending == NULL) {
279 so->so_snd.sb_lowat = so->so_snd.sb_hiwat + 1;
283 SOCKBUF_LOCK(&so->so_snd);
284 if (sbspace(&so->so_snd) < softc->ha_sending->m_pkthdr.len) {
285 so->so_snd.sb_lowat = softc->ha_sending->m_pkthdr.len;
286 SOCKBUF_UNLOCK(&so->so_snd);
289 SOCKBUF_UNLOCK(&so->so_snd);
290 error = sosend(softc->ha_so, NULL, NULL, softc->ha_sending,
291 NULL, MSG_DONTWAIT, curthread);
292 softc->ha_sending = NULL;
294 printf("%s: sosend() error %d\n", __func__, error);
301 ctl_ha_sock_setup(struct ha_softc *softc)
304 struct socket *so = softc->ha_so;
308 error = soreserve(so, val, val);
310 printf("%s: soreserve failed %d\n", __func__, error);
312 SOCKBUF_LOCK(&so->so_rcv);
313 so->so_rcv.sb_lowat = sizeof(struct ha_msg_wire);
314 soupcall_set(so, SO_RCV, ctl_ha_rupcall, softc);
315 SOCKBUF_UNLOCK(&so->so_rcv);
316 SOCKBUF_LOCK(&so->so_snd);
317 so->so_snd.sb_lowat = sizeof(struct ha_msg_wire);
318 soupcall_set(so, SO_SND, ctl_ha_supcall, softc);
319 SOCKBUF_UNLOCK(&so->so_snd);
321 bzero(&opt, sizeof(struct sockopt));
322 opt.sopt_dir = SOPT_SET;
323 opt.sopt_level = SOL_SOCKET;
324 opt.sopt_name = SO_KEEPALIVE;
326 opt.sopt_valsize = sizeof(val);
328 error = sosetopt(so, &opt);
330 printf("%s: KEEPALIVE setting failed %d\n", __func__, error);
332 opt.sopt_level = IPPROTO_TCP;
333 opt.sopt_name = TCP_NODELAY;
335 error = sosetopt(so, &opt);
337 printf("%s: NODELAY setting failed %d\n", __func__, error);
339 opt.sopt_name = TCP_KEEPINIT;
341 error = sosetopt(so, &opt);
343 printf("%s: KEEPINIT setting failed %d\n", __func__, error);
345 opt.sopt_name = TCP_KEEPIDLE;
347 error = sosetopt(so, &opt);
349 printf("%s: KEEPIDLE setting failed %d\n", __func__, error);
351 opt.sopt_name = TCP_KEEPINTVL;
353 error = sosetopt(so, &opt);
355 printf("%s: KEEPINTVL setting failed %d\n", __func__, error);
357 opt.sopt_name = TCP_KEEPCNT;
359 error = sosetopt(so, &opt);
361 printf("%s: KEEPCNT setting failed %d\n", __func__, error);
365 ctl_ha_connect(struct ha_softc *softc)
367 struct thread *td = curthread;
368 struct sockaddr_in sa;
372 /* Create the socket */
373 error = socreate(PF_INET, &so, SOCK_STREAM,
374 IPPROTO_TCP, td->td_ucred, td);
376 printf("%s: socreate() error %d\n", __func__, error);
380 ctl_ha_sock_setup(softc);
382 memcpy(&sa, &softc->ha_peer_in, sizeof(sa));
383 error = soconnect(so, (struct sockaddr *)&sa, td);
386 printf("%s: soconnect() error %d\n", __func__, error);
397 ctl_ha_accept(struct ha_softc *softc)
399 struct socket *lso, *so;
400 struct sockaddr_in sin = { .sin_len = sizeof(sin) };
405 error = solisten_dequeue(lso, &so, 0);
406 if (error == EWOULDBLOCK)
409 printf("%s: socket error %d\n", __func__, error);
413 error = soaccept(so, (struct sockaddr *)&sin);
415 printf("%s: soaccept() error %d\n", __func__, error);
419 ctl_ha_sock_setup(softc);
423 ctl_ha_lclose(softc);
428 ctl_ha_listen(struct ha_softc *softc)
430 struct thread *td = curthread;
431 struct sockaddr_in sa;
435 /* Create the socket */
436 if (softc->ha_lso == NULL) {
437 error = socreate(PF_INET, &softc->ha_lso, SOCK_STREAM,
438 IPPROTO_TCP, td->td_ucred, td);
440 printf("%s: socreate() error %d\n", __func__, error);
443 bzero(&opt, sizeof(struct sockopt));
444 opt.sopt_dir = SOPT_SET;
445 opt.sopt_level = SOL_SOCKET;
446 opt.sopt_name = SO_REUSEADDR;
448 opt.sopt_valsize = sizeof(val);
450 error = sosetopt(softc->ha_lso, &opt);
452 printf("%s: REUSEADDR setting failed %d\n",
455 bzero(&opt, sizeof(struct sockopt));
456 opt.sopt_dir = SOPT_SET;
457 opt.sopt_level = SOL_SOCKET;
458 opt.sopt_name = SO_REUSEPORT;
460 opt.sopt_valsize = sizeof(val);
462 error = sosetopt(softc->ha_lso, &opt);
464 printf("%s: REUSEPORT setting failed %d\n",
469 memcpy(&sa, &softc->ha_peer_in, sizeof(sa));
470 error = sobind(softc->ha_lso, (struct sockaddr *)&sa, td);
472 printf("%s: sobind() error %d\n", __func__, error);
475 error = solisten(softc->ha_lso, 1, td);
477 printf("%s: solisten() error %d\n", __func__, error);
480 SOLISTEN_LOCK(softc->ha_lso);
481 softc->ha_lso->so_state |= SS_NBIO;
482 solisten_upcall_set(softc->ha_lso, ctl_ha_lupcall, softc);
483 SOLISTEN_UNLOCK(softc->ha_lso);
487 ctl_ha_lclose(softc);
492 ctl_ha_conn_thread(void *arg)
494 struct ha_softc *softc = arg;
498 if (softc->ha_disconnect || softc->ha_shutdown) {
500 if (softc->ha_disconnect == 2 || softc->ha_shutdown)
501 ctl_ha_lclose(softc);
502 softc->ha_disconnect = 0;
503 if (softc->ha_shutdown)
505 } else if (softc->ha_so != NULL &&
506 (softc->ha_so->so_error ||
507 softc->ha_so->so_rcv.sb_state & SBS_CANTRCVMORE))
509 if (softc->ha_so == NULL) {
510 if (softc->ha_lso != NULL)
511 ctl_ha_accept(softc);
512 else if (softc->ha_listen)
513 ctl_ha_listen(softc);
514 else if (softc->ha_connect)
515 ctl_ha_connect(softc);
517 if (softc->ha_so != NULL) {
518 if (softc->ha_connected == 0 &&
519 softc->ha_so->so_error == 0 &&
520 (softc->ha_so->so_state & SS_ISCONNECTING) == 0) {
521 softc->ha_connected = 1;
522 ctl_ha_evt(softc, CTL_HA_CHAN_MAX,
523 CTL_HA_EVT_LINK_CHANGE,
525 softc->ha_receiving = 1;
526 error = kproc_kthread_add(ctl_ha_rx_thread,
527 softc, &softc->ha_ctl_softc->ctl_proc,
528 NULL, 0, 0, "ctl", "ha_rx");
530 printf("Error creating CTL HA rx thread!\n");
531 softc->ha_receiving = 0;
532 softc->ha_disconnect = 1;
537 mtx_lock(&softc->ha_lock);
538 if (softc->ha_so != NULL &&
539 (softc->ha_so->so_error ||
540 softc->ha_so->so_rcv.sb_state & SBS_CANTRCVMORE))
542 else if (!softc->ha_wakeup)
543 msleep(&softc->ha_wakeup, &softc->ha_lock, 0, "-", hz);
544 softc->ha_wakeup = 0;
545 mtx_unlock(&softc->ha_lock);
547 mtx_lock(&softc->ha_lock);
548 softc->ha_shutdown = 2;
549 wakeup(&softc->ha_wakeup);
550 mtx_unlock(&softc->ha_lock);
555 ctl_ha_peer_sysctl(SYSCTL_HANDLER_ARGS)
557 struct ha_softc *softc = (struct ha_softc *)arg1;
558 struct sockaddr_in *sa;
559 int error, b1, b2, b3, b4, p, num;
562 strlcpy(buf, softc->ha_peer, sizeof(buf));
563 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
564 if ((error != 0) || (req->newptr == NULL) ||
565 strncmp(buf, softc->ha_peer, sizeof(buf)) == 0)
568 sa = &softc->ha_peer_in;
569 mtx_lock(&softc->ha_lock);
570 if ((num = sscanf(buf, "connect %d.%d.%d.%d:%d",
571 &b1, &b2, &b3, &b4, &p)) >= 4) {
572 softc->ha_connect = 1;
573 softc->ha_listen = 0;
574 } else if ((num = sscanf(buf, "listen %d.%d.%d.%d:%d",
575 &b1, &b2, &b3, &b4, &p)) >= 4) {
576 softc->ha_connect = 0;
577 softc->ha_listen = 1;
579 softc->ha_connect = 0;
580 softc->ha_listen = 0;
586 strlcpy(softc->ha_peer, buf, sizeof(softc->ha_peer));
587 if (softc->ha_connect || softc->ha_listen) {
588 memset(sa, 0, sizeof(*sa));
589 sa->sin_len = sizeof(struct sockaddr_in);
590 sa->sin_family = AF_INET;
591 sa->sin_port = htons((num >= 5) ? p : 999);
592 sa->sin_addr.s_addr =
593 htonl((b1 << 24) + (b2 << 16) + (b3 << 8) + b4);
595 softc->ha_disconnect = 2;
596 softc->ha_wakeup = 1;
597 mtx_unlock(&softc->ha_lock);
598 wakeup(&softc->ha_wakeup);
603 ctl_ha_msg_register(ctl_ha_channel channel, ctl_evt_handler handler)
605 struct ha_softc *softc = &ha_softc;
607 KASSERT(channel < CTL_HA_CHAN_MAX,
608 ("Wrong CTL HA channel %d", channel));
609 softc->ha_handler[channel] = handler;
610 return (CTL_HA_STATUS_SUCCESS);
614 ctl_ha_msg_deregister(ctl_ha_channel channel)
616 struct ha_softc *softc = &ha_softc;
618 KASSERT(channel < CTL_HA_CHAN_MAX,
619 ("Wrong CTL HA channel %d", channel));
620 softc->ha_handler[channel] = NULL;
621 return (CTL_HA_STATUS_SUCCESS);
625 * Receive a message of the specified size.
628 ctl_ha_msg_recv(ctl_ha_channel channel, void *addr, size_t len,
631 struct ha_softc *softc = &ha_softc;
636 if (!softc->ha_connected)
637 return (CTL_HA_STATUS_DISCONNECT);
643 uio.uio_rw = UIO_READ;
644 uio.uio_segflg = UIO_SYSSPACE;
645 uio.uio_td = curthread;
647 flags = wait ? 0 : MSG_DONTWAIT;
648 error = soreceive(softc->ha_so, NULL, &uio, NULL, NULL, &flags);
650 return (CTL_HA_STATUS_SUCCESS);
652 /* Consider all errors fatal for HA sanity. */
653 mtx_lock(&softc->ha_lock);
654 if (softc->ha_connected) {
655 softc->ha_disconnect = 1;
656 softc->ha_wakeup = 1;
657 wakeup(&softc->ha_wakeup);
659 mtx_unlock(&softc->ha_lock);
660 return (CTL_HA_STATUS_ERROR);
664 * Send a message of the specified size.
667 ctl_ha_msg_send2(ctl_ha_channel channel, const void *addr, size_t len,
668 const void *addr2, size_t len2, int wait)
670 struct ha_softc *softc = &ha_softc;
671 struct mbuf *mb, *newmb;
672 struct ha_msg_wire hdr;
675 if (!softc->ha_connected)
676 return (CTL_HA_STATUS_DISCONNECT);
678 newmb = m_getm2(NULL, sizeof(hdr) + len + len2, wait, MT_DATA,
681 /* Consider all errors fatal for HA sanity. */
682 mtx_lock(&softc->ha_lock);
683 if (softc->ha_connected) {
684 softc->ha_disconnect = 1;
685 softc->ha_wakeup = 1;
686 wakeup(&softc->ha_wakeup);
688 mtx_unlock(&softc->ha_lock);
689 printf("%s: Can't allocate mbuf chain\n", __func__);
690 return (CTL_HA_STATUS_ERROR);
692 hdr.channel = channel;
693 hdr.length = len + len2;
695 memcpy(mtodo(mb, 0), &hdr, sizeof(hdr));
696 mb->m_len += sizeof(hdr);
698 for (; mb != NULL && off < len; mb = mb->m_next) {
699 copylen = min(M_TRAILINGSPACE(mb), len - off);
700 memcpy(mtodo(mb, mb->m_len), (const char *)addr + off, copylen);
701 mb->m_len += copylen;
706 KASSERT(off == len, ("%s: off (%zu) != len (%zu)", __func__,
709 for (; mb != NULL && off < len2; mb = mb->m_next) {
710 copylen = min(M_TRAILINGSPACE(mb), len2 - off);
711 memcpy(mtodo(mb, mb->m_len), (const char *)addr2 + off, copylen);
712 mb->m_len += copylen;
715 KASSERT(off == len2, ("%s: off (%zu) != len2 (%zu)", __func__,
717 newmb->m_pkthdr.len = sizeof(hdr) + len + len2;
719 mtx_lock(&softc->ha_lock);
720 if (!softc->ha_connected) {
721 mtx_unlock(&softc->ha_lock);
723 return (CTL_HA_STATUS_DISCONNECT);
725 mbufq_enqueue(&softc->ha_sendq, newmb);
726 softc->ha_wakeup = 1;
727 mtx_unlock(&softc->ha_lock);
728 wakeup(&softc->ha_wakeup);
729 return (CTL_HA_STATUS_SUCCESS);
733 ctl_ha_msg_send(ctl_ha_channel channel, const void *addr, size_t len,
737 return (ctl_ha_msg_send2(channel, addr, len, NULL, 0, wait));
741 ctl_ha_msg_abort(ctl_ha_channel channel)
743 struct ha_softc *softc = &ha_softc;
745 mtx_lock(&softc->ha_lock);
746 softc->ha_disconnect = 1;
747 softc->ha_wakeup = 1;
748 mtx_unlock(&softc->ha_lock);
749 wakeup(&softc->ha_wakeup);
750 return (CTL_HA_STATUS_SUCCESS);
754 * Allocate a data transfer request structure.
756 struct ctl_ha_dt_req *
757 ctl_dt_req_alloc(void)
760 return (malloc(sizeof(struct ctl_ha_dt_req), M_CTL, M_WAITOK | M_ZERO));
764 * Free a data transfer request structure.
767 ctl_dt_req_free(struct ctl_ha_dt_req *req)
774 * Issue a DMA request for a single buffer.
777 ctl_dt_single(struct ctl_ha_dt_req *req)
779 struct ha_softc *softc = &ha_softc;
780 struct ha_dt_msg_wire wire_dt;
781 ctl_ha_status status;
783 wire_dt.command = req->command;
784 wire_dt.size = req->size;
785 wire_dt.local = req->local;
786 wire_dt.remote = req->remote;
787 if (req->command == CTL_HA_DT_CMD_READ && req->callback != NULL) {
788 mtx_lock(&softc->ha_lock);
789 TAILQ_INSERT_TAIL(&softc->ha_dts, req, links);
790 mtx_unlock(&softc->ha_lock);
791 ctl_ha_msg_send(CTL_HA_CHAN_DATA, &wire_dt, sizeof(wire_dt),
793 return (CTL_HA_STATUS_WAIT);
795 if (req->command == CTL_HA_DT_CMD_READ) {
796 status = ctl_ha_msg_send(CTL_HA_CHAN_DATA, &wire_dt,
797 sizeof(wire_dt), M_WAITOK);
799 status = ctl_ha_msg_send2(CTL_HA_CHAN_DATA, &wire_dt,
800 sizeof(wire_dt), req->local, req->size, M_WAITOK);
806 ctl_dt_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
808 struct ha_softc *softc = &ha_softc;
809 struct ctl_ha_dt_req *req;
810 ctl_ha_status isc_status;
812 if (event == CTL_HA_EVT_MSG_RECV) {
813 struct ha_dt_msg_wire wire_dt;
817 size = min(sizeof(wire_dt), param);
818 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_DATA, &wire_dt,
820 if (isc_status != CTL_HA_STATUS_SUCCESS) {
821 printf("%s: Error receiving message: %d\n",
822 __func__, isc_status);
826 if (wire_dt.command == CTL_HA_DT_CMD_READ) {
827 wire_dt.command = CTL_HA_DT_CMD_WRITE;
829 wire_dt.local = wire_dt.remote;
830 wire_dt.remote = tmp;
831 ctl_ha_msg_send2(CTL_HA_CHAN_DATA, &wire_dt,
832 sizeof(wire_dt), wire_dt.local, wire_dt.size,
834 } else if (wire_dt.command == CTL_HA_DT_CMD_WRITE) {
835 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_DATA,
836 wire_dt.remote, wire_dt.size, M_WAITOK);
837 mtx_lock(&softc->ha_lock);
838 TAILQ_FOREACH(req, &softc->ha_dts, links) {
839 if (req->local == wire_dt.remote) {
840 TAILQ_REMOVE(&softc->ha_dts, req, links);
844 mtx_unlock(&softc->ha_lock);
846 req->ret = isc_status;
850 } else if (event == CTL_HA_EVT_LINK_CHANGE) {
851 CTL_DEBUG_PRINT(("%s: Link state change to %d\n", __func__,
853 if (param != CTL_HA_LINK_ONLINE) {
854 mtx_lock(&softc->ha_lock);
855 while ((req = TAILQ_FIRST(&softc->ha_dts)) != NULL) {
856 TAILQ_REMOVE(&softc->ha_dts, req, links);
857 mtx_unlock(&softc->ha_lock);
858 req->ret = CTL_HA_STATUS_DISCONNECT;
860 mtx_lock(&softc->ha_lock);
862 mtx_unlock(&softc->ha_lock);
865 printf("%s: Unknown event %d\n", __func__, event);
870 ctl_ha_msg_init(struct ctl_softc *ctl_softc)
872 struct ha_softc *softc = &ha_softc;
875 softc->ha_ctl_softc = ctl_softc;
876 mtx_init(&softc->ha_lock, "CTL HA mutex", NULL, MTX_DEF);
877 mbufq_init(&softc->ha_sendq, INT_MAX);
878 TAILQ_INIT(&softc->ha_dts);
879 error = kproc_kthread_add(ctl_ha_conn_thread, softc,
880 &ctl_softc->ctl_proc, NULL, 0, 0, "ctl", "ha_tx");
882 printf("error creating CTL HA connection thread!\n");
883 mtx_destroy(&softc->ha_lock);
884 return (CTL_HA_STATUS_ERROR);
886 softc->ha_shutdown_eh = EVENTHANDLER_REGISTER(shutdown_pre_sync,
887 ctl_ha_msg_shutdown, ctl_softc, SHUTDOWN_PRI_FIRST);
888 SYSCTL_ADD_PROC(&ctl_softc->sysctl_ctx,
889 SYSCTL_CHILDREN(ctl_softc->sysctl_tree),
891 CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
892 softc, 0, ctl_ha_peer_sysctl, "A", "HA peer connection method");
894 if (ctl_ha_msg_register(CTL_HA_CHAN_DATA, ctl_dt_event_handler)
895 != CTL_HA_STATUS_SUCCESS) {
896 printf("%s: ctl_ha_msg_register failed.\n", __func__);
899 return (CTL_HA_STATUS_SUCCESS);
903 ctl_ha_msg_shutdown(struct ctl_softc *ctl_softc)
905 struct ha_softc *softc = &ha_softc;
907 if (SCHEDULER_STOPPED())
910 /* Disconnect and shutdown threads. */
911 mtx_lock(&softc->ha_lock);
912 if (softc->ha_shutdown < 2) {
913 softc->ha_shutdown = 1;
914 softc->ha_wakeup = 1;
915 wakeup(&softc->ha_wakeup);
916 while (softc->ha_shutdown < 2) {
917 msleep(&softc->ha_wakeup, &softc->ha_lock, 0,
921 mtx_unlock(&softc->ha_lock);
925 ctl_ha_msg_destroy(struct ctl_softc *ctl_softc)
927 struct ha_softc *softc = &ha_softc;
929 if (softc->ha_shutdown_eh != NULL) {
930 EVENTHANDLER_DEREGISTER(shutdown_pre_sync,
931 softc->ha_shutdown_eh);
932 softc->ha_shutdown_eh = NULL;
935 ctl_ha_msg_shutdown(ctl_softc); /* Just in case. */
937 if (ctl_ha_msg_deregister(CTL_HA_CHAN_DATA) != CTL_HA_STATUS_SUCCESS)
938 printf("%s: ctl_ha_msg_deregister failed.\n", __func__);
940 mtx_destroy(&softc->ha_lock);
941 return (CTL_HA_STATUS_SUCCESS);