1/*-
2 * Copyright (c) 2015 Alexander Motin <mav@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer,
10 *    without modification, immediately at the beginning of the file.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: stable/10/sys/cam/ctl/ctl_ha.c 312587 2017-01-21 08:43:41Z mav $");
29
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/kernel.h>
33#include <sys/kthread.h>
34#include <sys/types.h>
35#include <sys/limits.h>
36#include <sys/lock.h>
37#include <sys/module.h>
38#include <sys/mutex.h>
39#include <sys/condvar.h>
40#include <sys/malloc.h>
41#include <sys/mbuf.h>
42#include <sys/proc.h>
43#include <sys/conf.h>
44#include <sys/queue.h>
45#include <sys/sysctl.h>
46#include <sys/socket.h>
47#include <sys/socketvar.h>
48#include <sys/uio.h>
49#include <netinet/in.h>
50#include <netinet/tcp.h>
51#include <vm/uma.h>
52
53#include <cam/cam.h>
54#include <cam/scsi/scsi_all.h>
55#include <cam/scsi/scsi_da.h>
56#include <cam/ctl/ctl_io.h>
57#include <cam/ctl/ctl.h>
58#include <cam/ctl/ctl_frontend.h>
59#include <cam/ctl/ctl_util.h>
60#include <cam/ctl/ctl_backend.h>
61#include <cam/ctl/ctl_ioctl.h>
62#include <cam/ctl/ctl_ha.h>
63#include <cam/ctl/ctl_private.h>
64#include <cam/ctl/ctl_debug.h>
65#include <cam/ctl/ctl_error.h>
66
67#if (__FreeBSD_version < 1100000)
68struct mbufq {
69	struct mbuf *head;
70	struct mbuf *tail;
71};
72
73static void
74mbufq_init(struct mbufq *q, int limit)
75{
76
77	q->head = q->tail = NULL;
78}
79
80static void
81mbufq_drain(struct mbufq *q)
82{
83	struct mbuf *m;
84
85	while ((m = q->head) != NULL) {
86		q->head = m->m_nextpkt;
87		m_freem(m);
88	}
89	q->tail = NULL;
90}
91
92static struct mbuf *
93mbufq_dequeue(struct mbufq *q)
94{
95	struct mbuf *m;
96
97	m = q->head;
98	if (m) {
99		if (q->tail == m)
100			q->tail = NULL;
101		q->head = m->m_nextpkt;
102		m->m_nextpkt = NULL;
103	}
104	return (m);
105}
106
107static void
108mbufq_enqueue(struct mbufq *q, struct mbuf *m)
109{
110
111	m->m_nextpkt = NULL;
112	if (q->tail)
113		q->tail->m_nextpkt = m;
114	else
115		q->head = m;
116	q->tail = m;
117}
118
119static u_int
120sbavail(struct sockbuf *sb)
121{
122	return (sb->sb_cc);
123}
124
125#if (__FreeBSD_version < 1000000)
126#define	mtodo(m, o)	((void *)(((m)->m_data) + (o)))
127#endif
128#endif
129
130struct ha_msg_wire {
131	uint32_t	 channel;
132	uint32_t	 length;
133};
134
135struct ha_dt_msg_wire {
136	ctl_ha_dt_cmd	command;
137	uint32_t	size;
138	uint8_t		*local;
139	uint8_t		*remote;
140};
141
142struct ha_softc {
143	struct ctl_softc *ha_ctl_softc;
144	ctl_evt_handler	 ha_handler[CTL_HA_CHAN_MAX];
145	char		 ha_peer[128];
146	struct sockaddr_in  ha_peer_in;
147	struct socket	*ha_lso;
148	struct socket	*ha_so;
149	struct mbufq	 ha_sendq;
150	struct mbuf	*ha_sending;
151	struct mtx	 ha_lock;
152	int		 ha_connect;
153	int		 ha_listen;
154	int		 ha_connected;
155	int		 ha_receiving;
156	int		 ha_wakeup;
157	int		 ha_disconnect;
158	int		 ha_shutdown;
159	eventhandler_tag ha_shutdown_eh;
160	TAILQ_HEAD(, ctl_ha_dt_req) ha_dts;
161} ha_softc;
162
163static void
164ctl_ha_conn_wake(struct ha_softc *softc)
165{
166
167	mtx_lock(&softc->ha_lock);
168	softc->ha_wakeup = 1;
169	mtx_unlock(&softc->ha_lock);
170	wakeup(&softc->ha_wakeup);
171}
172
173static int
174ctl_ha_lupcall(struct socket *so, void *arg, int waitflag)
175{
176	struct ha_softc *softc = arg;
177
178	ctl_ha_conn_wake(softc);
179	return (SU_OK);
180}
181
182static int
183ctl_ha_rupcall(struct socket *so, void *arg, int waitflag)
184{
185	struct ha_softc *softc = arg;
186
187	wakeup(&softc->ha_receiving);
188	return (SU_OK);
189}
190
191static int
192ctl_ha_supcall(struct socket *so, void *arg, int waitflag)
193{
194	struct ha_softc *softc = arg;
195
196	ctl_ha_conn_wake(softc);
197	return (SU_OK);
198}
199
200static void
201ctl_ha_evt(struct ha_softc *softc, ctl_ha_channel ch, ctl_ha_event evt,
202    int param)
203{
204	int i;
205
206	if (ch < CTL_HA_CHAN_MAX) {
207		if (softc->ha_handler[ch])
208			softc->ha_handler[ch](ch, evt, param);
209		return;
210	}
211	for (i = 0; i < CTL_HA_CHAN_MAX; i++) {
212		if (softc->ha_handler[i])
213			softc->ha_handler[i](i, evt, param);
214	}
215}
216
217static void
218ctl_ha_close(struct ha_softc *softc)
219{
220	struct socket *so = softc->ha_so;
221	int report = 0;
222
223	if (softc->ha_connected || softc->ha_disconnect) {
224		softc->ha_connected = 0;
225		mbufq_drain(&softc->ha_sendq);
226		m_freem(softc->ha_sending);
227		softc->ha_sending = NULL;
228		report = 1;
229	}
230	if (so) {
231		SOCKBUF_LOCK(&so->so_rcv);
232		soupcall_clear(so, SO_RCV);
233		while (softc->ha_receiving) {
234			wakeup(&softc->ha_receiving);
235			msleep(&softc->ha_receiving, SOCKBUF_MTX(&so->so_rcv),
236			    0, "ha_rx exit", 0);
237		}
238		SOCKBUF_UNLOCK(&so->so_rcv);
239		SOCKBUF_LOCK(&so->so_snd);
240		soupcall_clear(so, SO_SND);
241		SOCKBUF_UNLOCK(&so->so_snd);
242		softc->ha_so = NULL;
243		if (softc->ha_connect)
244			pause("reconnect", hz / 2);
245		soclose(so);
246	}
247	if (report) {
248		ctl_ha_evt(softc, CTL_HA_CHAN_MAX, CTL_HA_EVT_LINK_CHANGE,
249		    (softc->ha_connect || softc->ha_listen) ?
250		    CTL_HA_LINK_UNKNOWN : CTL_HA_LINK_OFFLINE);
251	}
252}
253
254static void
255ctl_ha_lclose(struct ha_softc *softc)
256{
257
258	if (softc->ha_lso) {
259		SOCKBUF_LOCK(&softc->ha_lso->so_rcv);
260		soupcall_clear(softc->ha_lso, SO_RCV);
261		SOCKBUF_UNLOCK(&softc->ha_lso->so_rcv);
262		soclose(softc->ha_lso);
263		softc->ha_lso = NULL;
264	}
265}
266
267static void
268ctl_ha_rx_thread(void *arg)
269{
270	struct ha_softc *softc = arg;
271	struct socket *so = softc->ha_so;
272	struct ha_msg_wire wire_hdr;
273	struct uio uio;
274	struct iovec iov;
275	int error, flags, next;
276
277	bzero(&wire_hdr, sizeof(wire_hdr));
278	while (1) {
279		if (wire_hdr.length > 0)
280			next = wire_hdr.length;
281		else
282			next = sizeof(wire_hdr);
283		SOCKBUF_LOCK(&so->so_rcv);
284		while (sbavail(&so->so_rcv) < next || softc->ha_disconnect) {
285			if (softc->ha_connected == 0 || softc->ha_disconnect ||
286			    so->so_error ||
287			    (so->so_rcv.sb_state & SBS_CANTRCVMORE)) {
288				goto errout;
289			}
290			so->so_rcv.sb_lowat = next;
291			msleep(&softc->ha_receiving, SOCKBUF_MTX(&so->so_rcv),
292			    0, "-", 0);
293		}
294		SOCKBUF_UNLOCK(&so->so_rcv);
295
296		if (wire_hdr.length == 0) {
297			iov.iov_base = &wire_hdr;
298			iov.iov_len = sizeof(wire_hdr);
299			uio.uio_iov = &iov;
300			uio.uio_iovcnt = 1;
301			uio.uio_rw = UIO_READ;
302			uio.uio_segflg = UIO_SYSSPACE;
303			uio.uio_td = curthread;
304			uio.uio_resid = sizeof(wire_hdr);
305			flags = MSG_DONTWAIT;
306			error = soreceive(softc->ha_so, NULL, &uio, NULL,
307			    NULL, &flags);
308			if (error != 0) {
309				printf("%s: header receive error %d\n",
310				    __func__, error);
311				SOCKBUF_LOCK(&so->so_rcv);
312				goto errout;
313			}
314		} else {
315			ctl_ha_evt(softc, wire_hdr.channel,
316			    CTL_HA_EVT_MSG_RECV, wire_hdr.length);
317			wire_hdr.length = 0;
318		}
319	}
320
321errout:
322	softc->ha_receiving = 0;
323	wakeup(&softc->ha_receiving);
324	SOCKBUF_UNLOCK(&so->so_rcv);
325	ctl_ha_conn_wake(softc);
326	kthread_exit();
327}
328
329static void
330ctl_ha_send(struct ha_softc *softc)
331{
332	struct socket *so = softc->ha_so;
333	int error;
334
335	while (1) {
336		if (softc->ha_sending == NULL) {
337			mtx_lock(&softc->ha_lock);
338			softc->ha_sending = mbufq_dequeue(&softc->ha_sendq);
339			mtx_unlock(&softc->ha_lock);
340			if (softc->ha_sending == NULL) {
341				so->so_snd.sb_lowat = so->so_snd.sb_hiwat + 1;
342				break;
343			}
344		}
345		SOCKBUF_LOCK(&so->so_snd);
346		if (sbspace(&so->so_snd) < softc->ha_sending->m_pkthdr.len) {
347			so->so_snd.sb_lowat = softc->ha_sending->m_pkthdr.len;
348			SOCKBUF_UNLOCK(&so->so_snd);
349			break;
350		}
351		SOCKBUF_UNLOCK(&so->so_snd);
352		error = sosend(softc->ha_so, NULL, NULL, softc->ha_sending,
353		    NULL, MSG_DONTWAIT, curthread);
354		softc->ha_sending = NULL;
355		if (error != 0) {
356			printf("%s: sosend() error %d\n", __func__, error);
357			return;
358		}
359	};
360}
361
362static void
363ctl_ha_sock_setup(struct ha_softc *softc)
364{
365	struct sockopt opt;
366	struct socket *so = softc->ha_so;
367	int error, val;
368
369	val = 1024 * 1024;
370	error = soreserve(so, val, val);
371	if (error)
372		printf("%s: soreserve failed %d\n", __func__, error);
373
374	SOCKBUF_LOCK(&so->so_rcv);
375	so->so_rcv.sb_lowat = sizeof(struct ha_msg_wire);
376	soupcall_set(so, SO_RCV, ctl_ha_rupcall, softc);
377	SOCKBUF_UNLOCK(&so->so_rcv);
378	SOCKBUF_LOCK(&so->so_snd);
379	so->so_snd.sb_lowat = sizeof(struct ha_msg_wire);
380	soupcall_set(so, SO_SND, ctl_ha_supcall, softc);
381	SOCKBUF_UNLOCK(&so->so_snd);
382
383	bzero(&opt, sizeof(struct sockopt));
384	opt.sopt_dir = SOPT_SET;
385	opt.sopt_level = SOL_SOCKET;
386	opt.sopt_name = SO_KEEPALIVE;
387	opt.sopt_val = &val;
388	opt.sopt_valsize = sizeof(val);
389	val = 1;
390	error = sosetopt(so, &opt);
391	if (error)
392		printf("%s: KEEPALIVE setting failed %d\n", __func__, error);
393
394	opt.sopt_level = IPPROTO_TCP;
395	opt.sopt_name = TCP_NODELAY;
396	val = 1;
397	error = sosetopt(so, &opt);
398	if (error)
399		printf("%s: NODELAY setting failed %d\n", __func__, error);
400
401	opt.sopt_name = TCP_KEEPINIT;
402	val = 3;
403	error = sosetopt(so, &opt);
404	if (error)
405		printf("%s: KEEPINIT setting failed %d\n", __func__, error);
406
407	opt.sopt_name = TCP_KEEPIDLE;
408	val = 1;
409	error = sosetopt(so, &opt);
410	if (error)
411		printf("%s: KEEPIDLE setting failed %d\n", __func__, error);
412
413	opt.sopt_name = TCP_KEEPINTVL;
414	val = 1;
415	error = sosetopt(so, &opt);
416	if (error)
417		printf("%s: KEEPINTVL setting failed %d\n", __func__, error);
418
419	opt.sopt_name = TCP_KEEPCNT;
420	val = 5;
421	error = sosetopt(so, &opt);
422	if (error)
423		printf("%s: KEEPCNT setting failed %d\n", __func__, error);
424}
425
426static int
427ctl_ha_connect(struct ha_softc *softc)
428{
429	struct thread *td = curthread;
430	struct sockaddr_in sa;
431	struct socket *so;
432	int error;
433
434	/* Create the socket */
435	error = socreate(PF_INET, &so, SOCK_STREAM,
436	    IPPROTO_TCP, td->td_ucred, td);
437	if (error != 0) {
438		printf("%s: socreate() error %d\n", __func__, error);
439		return (error);
440	}
441	softc->ha_so = so;
442	ctl_ha_sock_setup(softc);
443
444	memcpy(&sa, &softc->ha_peer_in, sizeof(sa));
445	error = soconnect(so, (struct sockaddr *)&sa, td);
446	if (error != 0) {
447		if (bootverbose)
448			printf("%s: soconnect() error %d\n", __func__, error);
449		goto out;
450	}
451	return (0);
452
453out:
454	ctl_ha_close(softc);
455	return (error);
456}
457
458static int
459ctl_ha_accept(struct ha_softc *softc)
460{
461	struct socket *so;
462	struct sockaddr *sap;
463	int error;
464
465	ACCEPT_LOCK();
466	if (softc->ha_lso->so_rcv.sb_state & SBS_CANTRCVMORE)
467		softc->ha_lso->so_error = ECONNABORTED;
468	if (softc->ha_lso->so_error) {
469		error = softc->ha_lso->so_error;
470		softc->ha_lso->so_error = 0;
471		ACCEPT_UNLOCK();
472		printf("%s: socket error %d\n", __func__, error);
473		goto out;
474	}
475	so = TAILQ_FIRST(&softc->ha_lso->so_comp);
476	if (so == NULL) {
477		ACCEPT_UNLOCK();
478		return (EWOULDBLOCK);
479	}
480	KASSERT(!(so->so_qstate & SQ_INCOMP), ("accept1: so SQ_INCOMP"));
481	KASSERT(so->so_qstate & SQ_COMP, ("accept1: so not SQ_COMP"));
482
483	/*
484	 * Before changing the flags on the socket, we have to bump the
485	 * reference count.  Otherwise, if the protocol calls sofree(),
486	 * the socket will be released due to a zero refcount.
487	 */
488	SOCK_LOCK(so);			/* soref() and so_state update */
489	soref(so);			/* file descriptor reference */
490
491	TAILQ_REMOVE(&softc->ha_lso->so_comp, so, so_list);
492	softc->ha_lso->so_qlen--;
493	so->so_state |= SS_NBIO;
494	so->so_qstate &= ~SQ_COMP;
495	so->so_head = NULL;
496
497	SOCK_UNLOCK(so);
498	ACCEPT_UNLOCK();
499
500	sap = NULL;
501	error = soaccept(so, &sap);
502	if (error != 0) {
503		printf("%s: soaccept() error %d\n", __func__, error);
504		if (sap != NULL)
505			free(sap, M_SONAME);
506		goto out;
507	}
508	if (sap != NULL)
509		free(sap, M_SONAME);
510	softc->ha_so = so;
511	ctl_ha_sock_setup(softc);
512	return (0);
513
514out:
515	ctl_ha_lclose(softc);
516	return (error);
517}
518
519static int
520ctl_ha_listen(struct ha_softc *softc)
521{
522	struct thread *td = curthread;
523	struct sockaddr_in sa;
524	struct sockopt opt;
525	int error, val;
526
527	/* Create the socket */
528	if (softc->ha_lso == NULL) {
529		error = socreate(PF_INET, &softc->ha_lso, SOCK_STREAM,
530		    IPPROTO_TCP, td->td_ucred, td);
531		if (error != 0) {
532			printf("%s: socreate() error %d\n", __func__, error);
533			return (error);
534		}
535		bzero(&opt, sizeof(struct sockopt));
536		opt.sopt_dir = SOPT_SET;
537		opt.sopt_level = SOL_SOCKET;
538		opt.sopt_name = SO_REUSEADDR;
539		opt.sopt_val = &val;
540		opt.sopt_valsize = sizeof(val);
541		val = 1;
542		error = sosetopt(softc->ha_lso, &opt);
543		if (error) {
544			printf("%s: REUSEADDR setting failed %d\n",
545			    __func__, error);
546		}
547		bzero(&opt, sizeof(struct sockopt));
548		opt.sopt_dir = SOPT_SET;
549		opt.sopt_level = SOL_SOCKET;
550		opt.sopt_name = SO_REUSEPORT;
551		opt.sopt_val = &val;
552		opt.sopt_valsize = sizeof(val);
553		val = 1;
554		error = sosetopt(softc->ha_lso, &opt);
555		if (error) {
556			printf("%s: REUSEPORT setting failed %d\n",
557			    __func__, error);
558		}
559		SOCKBUF_LOCK(&softc->ha_lso->so_rcv);
560		soupcall_set(softc->ha_lso, SO_RCV, ctl_ha_lupcall, softc);
561		SOCKBUF_UNLOCK(&softc->ha_lso->so_rcv);
562	}
563
564	memcpy(&sa, &softc->ha_peer_in, sizeof(sa));
565	error = sobind(softc->ha_lso, (struct sockaddr *)&sa, td);
566	if (error != 0) {
567		printf("%s: sobind() error %d\n", __func__, error);
568		goto out;
569	}
570	error = solisten(softc->ha_lso, 1, td);
571	if (error != 0) {
572		printf("%s: solisten() error %d\n", __func__, error);
573		goto out;
574	}
575	return (0);
576
577out:
578	ctl_ha_lclose(softc);
579	return (error);
580}
581
582static void
583ctl_ha_conn_thread(void *arg)
584{
585	struct ha_softc *softc = arg;
586	int error;
587
588	while (1) {
589		if (softc->ha_disconnect || softc->ha_shutdown) {
590			ctl_ha_close(softc);
591			if (softc->ha_disconnect == 2 || softc->ha_shutdown)
592				ctl_ha_lclose(softc);
593			softc->ha_disconnect = 0;
594			if (softc->ha_shutdown)
595				break;
596		} else if (softc->ha_so != NULL &&
597		    (softc->ha_so->so_error ||
598		     softc->ha_so->so_rcv.sb_state & SBS_CANTRCVMORE))
599			ctl_ha_close(softc);
600		if (softc->ha_so == NULL) {
601			if (softc->ha_lso != NULL)
602				ctl_ha_accept(softc);
603			else if (softc->ha_listen)
604				ctl_ha_listen(softc);
605			else if (softc->ha_connect)
606				ctl_ha_connect(softc);
607		}
608		if (softc->ha_so != NULL) {
609			if (softc->ha_connected == 0 &&
610			    softc->ha_so->so_error == 0 &&
611			    (softc->ha_so->so_state & SS_ISCONNECTING) == 0) {
612				softc->ha_connected = 1;
613				ctl_ha_evt(softc, CTL_HA_CHAN_MAX,
614				    CTL_HA_EVT_LINK_CHANGE,
615				    CTL_HA_LINK_ONLINE);
616				softc->ha_receiving = 1;
617				error = kproc_kthread_add(ctl_ha_rx_thread,
618				    softc, &softc->ha_ctl_softc->ctl_proc,
619				    NULL, 0, 0, "ctl", "ha_rx");
620				if (error != 0) {
621					printf("Error creating CTL HA rx thread!\n");
622					softc->ha_receiving = 0;
623					softc->ha_disconnect = 1;
624				}
625			}
626			ctl_ha_send(softc);
627		}
628		mtx_lock(&softc->ha_lock);
629		if (softc->ha_so != NULL &&
630		    (softc->ha_so->so_error ||
631		     softc->ha_so->so_rcv.sb_state & SBS_CANTRCVMORE))
632			;
633		else if (!softc->ha_wakeup)
634			msleep(&softc->ha_wakeup, &softc->ha_lock, 0, "-", hz);
635		softc->ha_wakeup = 0;
636		mtx_unlock(&softc->ha_lock);
637	}
638	mtx_lock(&softc->ha_lock);
639	softc->ha_shutdown = 2;
640	wakeup(&softc->ha_wakeup);
641	mtx_unlock(&softc->ha_lock);
642	kthread_exit();
643}
644
645static int
646ctl_ha_peer_sysctl(SYSCTL_HANDLER_ARGS)
647{
648	struct ha_softc *softc = (struct ha_softc *)arg1;
649	struct sockaddr_in *sa;
650	int error, b1, b2, b3, b4, p, num;
651	char buf[128];
652
653	strlcpy(buf, softc->ha_peer, sizeof(buf));
654	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
655	if ((error != 0) || (req->newptr == NULL) ||
656	    strncmp(buf, softc->ha_peer, sizeof(buf)) == 0)
657		return (error);
658
659	sa = &softc->ha_peer_in;
660	mtx_lock(&softc->ha_lock);
661	if ((num = sscanf(buf, "connect %d.%d.%d.%d:%d",
662	    &b1, &b2, &b3, &b4, &p)) >= 4) {
663		softc->ha_connect = 1;
664		softc->ha_listen = 0;
665	} else if ((num = sscanf(buf, "listen %d.%d.%d.%d:%d",
666	    &b1, &b2, &b3, &b4, &p)) >= 4) {
667		softc->ha_connect = 0;
668		softc->ha_listen = 1;
669	} else {
670		softc->ha_connect = 0;
671		softc->ha_listen = 0;
672		if (buf[0] != 0) {
673			buf[0] = 0;
674			error = EINVAL;
675		}
676	}
677	strlcpy(softc->ha_peer, buf, sizeof(softc->ha_peer));
678	if (softc->ha_connect || softc->ha_listen) {
679		memset(sa, 0, sizeof(*sa));
680		sa->sin_len = sizeof(struct sockaddr_in);
681		sa->sin_family = AF_INET;
682		sa->sin_port = htons((num >= 5) ? p : 999);
683		sa->sin_addr.s_addr =
684		    htonl((b1 << 24) + (b2 << 16) + (b3 << 8) + b4);
685	}
686	softc->ha_disconnect = 2;
687	softc->ha_wakeup = 1;
688	mtx_unlock(&softc->ha_lock);
689	wakeup(&softc->ha_wakeup);
690	return (error);
691}
692
693ctl_ha_status
694ctl_ha_msg_register(ctl_ha_channel channel, ctl_evt_handler handler)
695{
696	struct ha_softc *softc = &ha_softc;
697
698	KASSERT(channel < CTL_HA_CHAN_MAX,
699	    ("Wrong CTL HA channel %d", channel));
700	softc->ha_handler[channel] = handler;
701	return (CTL_HA_STATUS_SUCCESS);
702}
703
704ctl_ha_status
705ctl_ha_msg_deregister(ctl_ha_channel channel)
706{
707	struct ha_softc *softc = &ha_softc;
708
709	KASSERT(channel < CTL_HA_CHAN_MAX,
710	    ("Wrong CTL HA channel %d", channel));
711	softc->ha_handler[channel] = NULL;
712	return (CTL_HA_STATUS_SUCCESS);
713}
714
715/*
716 * Receive a message of the specified size.
717 */
718ctl_ha_status
719ctl_ha_msg_recv(ctl_ha_channel channel, void *addr, size_t len,
720		int wait)
721{
722	struct ha_softc *softc = &ha_softc;
723	struct uio uio;
724	struct iovec iov;
725	int error, flags;
726
727	if (!softc->ha_connected)
728		return (CTL_HA_STATUS_DISCONNECT);
729
730	iov.iov_base = addr;
731	iov.iov_len = len;
732	uio.uio_iov = &iov;
733	uio.uio_iovcnt = 1;
734	uio.uio_rw = UIO_READ;
735	uio.uio_segflg = UIO_SYSSPACE;
736	uio.uio_td = curthread;
737	uio.uio_resid = len;
738	flags = wait ? 0 : MSG_DONTWAIT;
739	error = soreceive(softc->ha_so, NULL, &uio, NULL, NULL, &flags);
740	if (error == 0)
741		return (CTL_HA_STATUS_SUCCESS);
742
743	/* Consider all errors fatal for HA sanity. */
744	mtx_lock(&softc->ha_lock);
745	if (softc->ha_connected) {
746		softc->ha_disconnect = 1;
747		softc->ha_wakeup = 1;
748		wakeup(&softc->ha_wakeup);
749	}
750	mtx_unlock(&softc->ha_lock);
751	return (CTL_HA_STATUS_ERROR);
752}
753
754/*
755 * Send a message of the specified size.
756 */
757ctl_ha_status
758ctl_ha_msg_send2(ctl_ha_channel channel, const void *addr, size_t len,
759    const void *addr2, size_t len2, int wait)
760{
761	struct ha_softc *softc = &ha_softc;
762	struct mbuf *mb, *newmb;
763	struct ha_msg_wire hdr;
764	size_t copylen, off;
765
766	if (!softc->ha_connected)
767		return (CTL_HA_STATUS_DISCONNECT);
768
769	newmb = m_getm2(NULL, sizeof(hdr) + len + len2, wait, MT_DATA,
770	    M_PKTHDR);
771	if (newmb == NULL) {
772		/* Consider all errors fatal for HA sanity. */
773		mtx_lock(&softc->ha_lock);
774		if (softc->ha_connected) {
775			softc->ha_disconnect = 1;
776			softc->ha_wakeup = 1;
777			wakeup(&softc->ha_wakeup);
778		}
779		mtx_unlock(&softc->ha_lock);
780		printf("%s: Can't allocate mbuf chain\n", __func__);
781		return (CTL_HA_STATUS_ERROR);
782	}
783	hdr.channel = channel;
784	hdr.length = len + len2;
785	mb = newmb;
786	memcpy(mtodo(mb, 0), &hdr, sizeof(hdr));
787	mb->m_len += sizeof(hdr);
788	off = 0;
789	for (; mb != NULL && off < len; mb = mb->m_next) {
790		copylen = min(M_TRAILINGSPACE(mb), len - off);
791		memcpy(mtodo(mb, mb->m_len), (const char *)addr + off, copylen);
792		mb->m_len += copylen;
793		off += copylen;
794		if (off == len)
795			break;
796	}
797	KASSERT(off == len, ("%s: off (%zu) != len (%zu)", __func__,
798	    off, len));
799	off = 0;
800	for (; mb != NULL && off < len2; mb = mb->m_next) {
801		copylen = min(M_TRAILINGSPACE(mb), len2 - off);
802		memcpy(mtodo(mb, mb->m_len), (const char *)addr2 + off, copylen);
803		mb->m_len += copylen;
804		off += copylen;
805	}
806	KASSERT(off == len2, ("%s: off (%zu) != len2 (%zu)", __func__,
807	    off, len2));
808	newmb->m_pkthdr.len = sizeof(hdr) + len + len2;
809
810	mtx_lock(&softc->ha_lock);
811	if (!softc->ha_connected) {
812		mtx_unlock(&softc->ha_lock);
813		m_freem(newmb);
814		return (CTL_HA_STATUS_DISCONNECT);
815	}
816	mbufq_enqueue(&softc->ha_sendq, newmb);
817	softc->ha_wakeup = 1;
818	mtx_unlock(&softc->ha_lock);
819	wakeup(&softc->ha_wakeup);
820	return (CTL_HA_STATUS_SUCCESS);
821}
822
823ctl_ha_status
824ctl_ha_msg_send(ctl_ha_channel channel, const void *addr, size_t len,
825    int wait)
826{
827
828	return (ctl_ha_msg_send2(channel, addr, len, NULL, 0, wait));
829}
830
831ctl_ha_status
832ctl_ha_msg_abort(ctl_ha_channel channel)
833{
834	struct ha_softc *softc = &ha_softc;
835
836	mtx_lock(&softc->ha_lock);
837	softc->ha_disconnect = 1;
838	softc->ha_wakeup = 1;
839	mtx_unlock(&softc->ha_lock);
840	wakeup(&softc->ha_wakeup);
841	return (CTL_HA_STATUS_SUCCESS);
842}
843
844/*
845 * Allocate a data transfer request structure.
846 */
847struct ctl_ha_dt_req *
848ctl_dt_req_alloc(void)
849{
850
851	return (malloc(sizeof(struct ctl_ha_dt_req), M_CTL, M_WAITOK | M_ZERO));
852}
853
854/*
855 * Free a data transfer request structure.
856 */
857void
858ctl_dt_req_free(struct ctl_ha_dt_req *req)
859{
860
861	free(req, M_CTL);
862}
863
864/*
865 * Issue a DMA request for a single buffer.
866 */
867ctl_ha_status
868ctl_dt_single(struct ctl_ha_dt_req *req)
869{
870	struct ha_softc *softc = &ha_softc;
871	struct ha_dt_msg_wire wire_dt;
872	ctl_ha_status status;
873
874	wire_dt.command = req->command;
875	wire_dt.size = req->size;
876	wire_dt.local = req->local;
877	wire_dt.remote = req->remote;
878	if (req->command == CTL_HA_DT_CMD_READ && req->callback != NULL) {
879		mtx_lock(&softc->ha_lock);
880		TAILQ_INSERT_TAIL(&softc->ha_dts, req, links);
881		mtx_unlock(&softc->ha_lock);
882		ctl_ha_msg_send(CTL_HA_CHAN_DATA, &wire_dt, sizeof(wire_dt),
883		    M_WAITOK);
884		return (CTL_HA_STATUS_WAIT);
885	}
886	if (req->command == CTL_HA_DT_CMD_READ) {
887		status = ctl_ha_msg_send(CTL_HA_CHAN_DATA, &wire_dt,
888		    sizeof(wire_dt), M_WAITOK);
889	} else {
890		status = ctl_ha_msg_send2(CTL_HA_CHAN_DATA, &wire_dt,
891		    sizeof(wire_dt), req->local, req->size, M_WAITOK);
892	}
893	return (status);
894}
895
896static void
897ctl_dt_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
898{
899	struct ha_softc *softc = &ha_softc;
900	struct ctl_ha_dt_req *req;
901	ctl_ha_status isc_status;
902
903	if (event == CTL_HA_EVT_MSG_RECV) {
904		struct ha_dt_msg_wire wire_dt;
905		uint8_t *tmp;
906		int size;
907
908		size = min(sizeof(wire_dt), param);
909		isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_DATA, &wire_dt,
910					     size, M_WAITOK);
911		if (isc_status != CTL_HA_STATUS_SUCCESS) {
912			printf("%s: Error receiving message: %d\n",
913			    __func__, isc_status);
914			return;
915		}
916
917		if (wire_dt.command == CTL_HA_DT_CMD_READ) {
918			wire_dt.command = CTL_HA_DT_CMD_WRITE;
919			tmp = wire_dt.local;
920			wire_dt.local = wire_dt.remote;
921			wire_dt.remote = tmp;
922			ctl_ha_msg_send2(CTL_HA_CHAN_DATA, &wire_dt,
923			    sizeof(wire_dt), wire_dt.local, wire_dt.size,
924			    M_WAITOK);
925		} else if (wire_dt.command == CTL_HA_DT_CMD_WRITE) {
926			isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_DATA,
927			    wire_dt.remote, wire_dt.size, M_WAITOK);
928			mtx_lock(&softc->ha_lock);
929			TAILQ_FOREACH(req, &softc->ha_dts, links) {
930				if (req->local == wire_dt.remote) {
931					TAILQ_REMOVE(&softc->ha_dts, req, links);
932					break;
933				}
934			}
935			mtx_unlock(&softc->ha_lock);
936			if (req) {
937				req->ret = isc_status;
938				req->callback(req);
939			}
940		}
941	} else if (event == CTL_HA_EVT_LINK_CHANGE) {
942		CTL_DEBUG_PRINT(("%s: Link state change to %d\n", __func__,
943		    param));
944		if (param != CTL_HA_LINK_ONLINE) {
945			mtx_lock(&softc->ha_lock);
946			while ((req = TAILQ_FIRST(&softc->ha_dts)) != NULL) {
947				TAILQ_REMOVE(&softc->ha_dts, req, links);
948				mtx_unlock(&softc->ha_lock);
949				req->ret = CTL_HA_STATUS_DISCONNECT;
950				req->callback(req);
951				mtx_lock(&softc->ha_lock);
952			}
953			mtx_unlock(&softc->ha_lock);
954		}
955	} else {
956		printf("%s: Unknown event %d\n", __func__, event);
957	}
958}
959
960
961ctl_ha_status
962ctl_ha_msg_init(struct ctl_softc *ctl_softc)
963{
964	struct ha_softc *softc = &ha_softc;
965	int error;
966
967	softc->ha_ctl_softc = ctl_softc;
968	mtx_init(&softc->ha_lock, "CTL HA mutex", NULL, MTX_DEF);
969	mbufq_init(&softc->ha_sendq, INT_MAX);
970	TAILQ_INIT(&softc->ha_dts);
971	error = kproc_kthread_add(ctl_ha_conn_thread, softc,
972	    &ctl_softc->ctl_proc, NULL, 0, 0, "ctl", "ha_tx");
973	if (error != 0) {
974		printf("error creating CTL HA connection thread!\n");
975		mtx_destroy(&softc->ha_lock);
976		return (CTL_HA_STATUS_ERROR);
977	}
978	softc->ha_shutdown_eh = EVENTHANDLER_REGISTER(shutdown_pre_sync,
979	    ctl_ha_msg_shutdown, ctl_softc, SHUTDOWN_PRI_FIRST);
980	SYSCTL_ADD_PROC(&ctl_softc->sysctl_ctx,
981	    SYSCTL_CHILDREN(ctl_softc->sysctl_tree),
982	    OID_AUTO, "ha_peer", CTLTYPE_STRING | CTLFLAG_RWTUN,
983	    softc, 0, ctl_ha_peer_sysctl, "A", "HA peer connection method");
984
985	if (ctl_ha_msg_register(CTL_HA_CHAN_DATA, ctl_dt_event_handler)
986	    != CTL_HA_STATUS_SUCCESS) {
987		printf("%s: ctl_ha_msg_register failed.\n", __func__);
988	}
989
990	return (CTL_HA_STATUS_SUCCESS);
991};
992
993void
994ctl_ha_msg_shutdown(struct ctl_softc *ctl_softc)
995{
996	struct ha_softc *softc = &ha_softc;
997
998	/* Disconnect and shutdown threads. */
999	mtx_lock(&softc->ha_lock);
1000	if (softc->ha_shutdown < 2) {
1001		softc->ha_shutdown = 1;
1002		softc->ha_wakeup = 1;
1003		wakeup(&softc->ha_wakeup);
1004		while (softc->ha_shutdown < 2 && !SCHEDULER_STOPPED()) {
1005			msleep(&softc->ha_wakeup, &softc->ha_lock, 0,
1006			    "shutdown", hz);
1007		}
1008	}
1009	mtx_unlock(&softc->ha_lock);
1010};
1011
1012ctl_ha_status
1013ctl_ha_msg_destroy(struct ctl_softc *ctl_softc)
1014{
1015	struct ha_softc *softc = &ha_softc;
1016
1017	if (softc->ha_shutdown_eh != NULL) {
1018		EVENTHANDLER_DEREGISTER(shutdown_pre_sync,
1019		    softc->ha_shutdown_eh);
1020		softc->ha_shutdown_eh = NULL;
1021	}
1022
1023	ctl_ha_msg_shutdown(ctl_softc);	/* Just in case. */
1024
1025	if (ctl_ha_msg_deregister(CTL_HA_CHAN_DATA) != CTL_HA_STATUS_SUCCESS)
1026		printf("%s: ctl_ha_msg_deregister failed.\n", __func__);
1027
1028	mtx_destroy(&softc->ha_lock);
1029	return (CTL_HA_STATUS_SUCCESS);
1030};
1031