xref: /freebsd/sys/cam/ctl/ctl_ha.c (revision c9dbb1cc52b063bbd9ab078a7afc89a8696da659)
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$");
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)
68 struct mbufq {
69 	struct mbuf *head;
70 	struct mbuf *tail;
71 };
72 
73 static void
74 mbufq_init(struct mbufq *q, int limit)
75 {
76 
77 	q->head = q->tail = NULL;
78 }
79 
80 static void
81 mbufq_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 
92 static struct mbuf *
93 mbufq_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 
107 static void
108 mbufq_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 
119 static u_int
120 sbavail(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 
130 struct ha_msg_wire {
131 	uint32_t	 channel;
132 	uint32_t	 length;
133 };
134 
135 struct ha_dt_msg_wire {
136 	ctl_ha_dt_cmd	command;
137 	uint32_t	size;
138 	uint8_t		*local;
139 	uint8_t		*remote;
140 };
141 
142 struct 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 
163 extern struct ctl_softc *control_softc;
164 
165 static void
166 ctl_ha_conn_wake(struct ha_softc *softc)
167 {
168 
169 	mtx_lock(&softc->ha_lock);
170 	softc->ha_wakeup = 1;
171 	mtx_unlock(&softc->ha_lock);
172 	wakeup(&softc->ha_wakeup);
173 }
174 
175 static int
176 ctl_ha_lupcall(struct socket *so, void *arg, int waitflag)
177 {
178 	struct ha_softc *softc = arg;
179 
180 	ctl_ha_conn_wake(softc);
181 	return (SU_OK);
182 }
183 
184 static int
185 ctl_ha_rupcall(struct socket *so, void *arg, int waitflag)
186 {
187 	struct ha_softc *softc = arg;
188 
189 	wakeup(&softc->ha_receiving);
190 	return (SU_OK);
191 }
192 
193 static int
194 ctl_ha_supcall(struct socket *so, void *arg, int waitflag)
195 {
196 	struct ha_softc *softc = arg;
197 
198 	ctl_ha_conn_wake(softc);
199 	return (SU_OK);
200 }
201 
202 static void
203 ctl_ha_evt(struct ha_softc *softc, ctl_ha_channel ch, ctl_ha_event evt,
204     int param)
205 {
206 	int i;
207 
208 	if (ch < CTL_HA_CHAN_MAX) {
209 		if (softc->ha_handler[ch])
210 			softc->ha_handler[ch](ch, evt, param);
211 		return;
212 	}
213 	for (i = 0; i < CTL_HA_CHAN_MAX; i++) {
214 		if (softc->ha_handler[i])
215 			softc->ha_handler[i](i, evt, param);
216 	}
217 }
218 
219 static void
220 ctl_ha_close(struct ha_softc *softc)
221 {
222 	struct socket *so = softc->ha_so;
223 	int report = 0;
224 
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;
230 		report = 1;
231 	}
232 	if (so) {
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),
238 			    0, "ha_rx exit", 0);
239 		}
240 		SOCKBUF_UNLOCK(&so->so_rcv);
241 		SOCKBUF_LOCK(&so->so_snd);
242 		soupcall_clear(so, SO_SND);
243 		SOCKBUF_UNLOCK(&so->so_snd);
244 		softc->ha_so = NULL;
245 		if (softc->ha_connect)
246 			pause("reconnect", hz / 2);
247 		soclose(so);
248 	}
249 	if (report) {
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);
253 	}
254 }
255 
256 static void
257 ctl_ha_lclose(struct ha_softc *softc)
258 {
259 
260 	if (softc->ha_lso) {
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;
266 	}
267 }
268 
269 static void
270 ctl_ha_rx_thread(void *arg)
271 {
272 	struct ha_softc *softc = arg;
273 	struct socket *so = softc->ha_so;
274 	struct ha_msg_wire wire_hdr;
275 	struct uio uio;
276 	struct iovec iov;
277 	int error, flags, next;
278 
279 	bzero(&wire_hdr, sizeof(wire_hdr));
280 	while (1) {
281 		if (wire_hdr.length > 0)
282 			next = wire_hdr.length;
283 		else
284 			next = sizeof(wire_hdr);
285 		SOCKBUF_LOCK(&so->so_rcv);
286 		while (sbavail(&so->so_rcv) < next) {
287 			if (softc->ha_connected == 0 || so->so_error ||
288 			    (so->so_rcv.sb_state & SBS_CANTRCVMORE)) {
289 				goto errout;
290 			}
291 			so->so_rcv.sb_lowat = next;
292 			msleep(&softc->ha_receiving, SOCKBUF_MTX(&so->so_rcv),
293 			    0, "-", 0);
294 		}
295 		SOCKBUF_UNLOCK(&so->so_rcv);
296 
297 		if (wire_hdr.length == 0) {
298 			iov.iov_base = &wire_hdr;
299 			iov.iov_len = sizeof(wire_hdr);
300 			uio.uio_iov = &iov;
301 			uio.uio_iovcnt = 1;
302 			uio.uio_rw = UIO_READ;
303 			uio.uio_segflg = UIO_SYSSPACE;
304 			uio.uio_td = curthread;
305 			uio.uio_resid = sizeof(wire_hdr);
306 			flags = MSG_DONTWAIT;
307 			error = soreceive(softc->ha_so, NULL, &uio, NULL,
308 			    NULL, &flags);
309 			if (error != 0) {
310 				printf("%s: header receive error %d\n",
311 				    __func__, error);
312 				SOCKBUF_LOCK(&so->so_rcv);
313 				goto errout;
314 			}
315 		} else {
316 			ctl_ha_evt(softc, wire_hdr.channel,
317 			    CTL_HA_EVT_MSG_RECV, wire_hdr.length);
318 			wire_hdr.length = 0;
319 		}
320 	}
321 
322 errout:
323 	softc->ha_receiving = 0;
324 	wakeup(&softc->ha_receiving);
325 	SOCKBUF_UNLOCK(&so->so_rcv);
326 	ctl_ha_conn_wake(softc);
327 	kthread_exit();
328 }
329 
330 static void
331 ctl_ha_send(struct ha_softc *softc)
332 {
333 	struct socket *so = softc->ha_so;
334 	int error;
335 
336 	while (1) {
337 		if (softc->ha_sending == NULL) {
338 			mtx_lock(&softc->ha_lock);
339 			softc->ha_sending = mbufq_dequeue(&softc->ha_sendq);
340 			mtx_unlock(&softc->ha_lock);
341 			if (softc->ha_sending == NULL) {
342 				so->so_snd.sb_lowat = so->so_snd.sb_hiwat + 1;
343 				break;
344 			}
345 		}
346 		SOCKBUF_LOCK(&so->so_snd);
347 		if (sbspace(&so->so_snd) < softc->ha_sending->m_pkthdr.len) {
348 			so->so_snd.sb_lowat = softc->ha_sending->m_pkthdr.len;
349 			SOCKBUF_UNLOCK(&so->so_snd);
350 			break;
351 		}
352 		SOCKBUF_UNLOCK(&so->so_snd);
353 		error = sosend(softc->ha_so, NULL, NULL, softc->ha_sending,
354 		    NULL, MSG_DONTWAIT, curthread);
355 		softc->ha_sending = NULL;
356 		if (error != 0) {
357 			printf("%s: sosend() error %d\n", __func__, error);
358 			return;
359 		}
360 	};
361 }
362 
363 static void
364 ctl_ha_sock_setup(struct ha_softc *softc)
365 {
366 	struct sockopt opt;
367 	struct socket *so = softc->ha_so;
368 	int error, val;
369 
370 	val = 1024 * 1024;
371 	error = soreserve(so, val, val);
372 	if (error)
373 		printf("%s: soreserve failed %d\n", __func__, error);
374 
375 	SOCKBUF_LOCK(&so->so_rcv);
376 	so->so_rcv.sb_lowat = sizeof(struct ha_msg_wire);
377 	soupcall_set(so, SO_RCV, ctl_ha_rupcall, softc);
378 	SOCKBUF_UNLOCK(&so->so_rcv);
379 	SOCKBUF_LOCK(&so->so_snd);
380 	so->so_snd.sb_lowat = sizeof(struct ha_msg_wire);
381 	soupcall_set(so, SO_SND, ctl_ha_supcall, softc);
382 	SOCKBUF_UNLOCK(&so->so_snd);
383 
384 	bzero(&opt, sizeof(struct sockopt));
385 	opt.sopt_dir = SOPT_SET;
386 	opt.sopt_level = SOL_SOCKET;
387 	opt.sopt_name = SO_KEEPALIVE;
388 	opt.sopt_val = &val;
389 	opt.sopt_valsize = sizeof(val);
390 	val = 1;
391 	error = sosetopt(so, &opt);
392 	if (error)
393 		printf("%s: KEEPALIVE setting failed %d\n", __func__, error);
394 
395 	opt.sopt_level = IPPROTO_TCP;
396 	opt.sopt_name = TCP_NODELAY;
397 	val = 1;
398 	error = sosetopt(so, &opt);
399 	if (error)
400 		printf("%s: NODELAY setting failed %d\n", __func__, error);
401 
402 	opt.sopt_name = TCP_KEEPINIT;
403 	val = 3;
404 	error = sosetopt(so, &opt);
405 	if (error)
406 		printf("%s: KEEPINIT setting failed %d\n", __func__, error);
407 
408 	opt.sopt_name = TCP_KEEPIDLE;
409 	val = 1;
410 	error = sosetopt(so, &opt);
411 	if (error)
412 		printf("%s: KEEPIDLE setting failed %d\n", __func__, error);
413 
414 	opt.sopt_name = TCP_KEEPINTVL;
415 	val = 1;
416 	error = sosetopt(so, &opt);
417 	if (error)
418 		printf("%s: KEEPINTVL setting failed %d\n", __func__, error);
419 
420 	opt.sopt_name = TCP_KEEPCNT;
421 	val = 5;
422 	error = sosetopt(so, &opt);
423 	if (error)
424 		printf("%s: KEEPCNT setting failed %d\n", __func__, error);
425 }
426 
427 static int
428 ctl_ha_connect(struct ha_softc *softc)
429 {
430 	struct thread *td = curthread;
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 	error = soconnect(so, (struct sockaddr *)&softc->ha_peer_in, td);
445 	if (error != 0) {
446 		printf("%s: soconnect() error %d\n", __func__, error);
447 		goto out;
448 	}
449 	return (0);
450 
451 out:
452 	ctl_ha_close(softc);
453 	return (error);
454 }
455 
456 static int
457 ctl_ha_accept(struct ha_softc *softc)
458 {
459 	struct socket *so;
460 	struct sockaddr *sap;
461 	int error;
462 
463 	ACCEPT_LOCK();
464 	if (softc->ha_lso->so_rcv.sb_state & SBS_CANTRCVMORE)
465 		softc->ha_lso->so_error = ECONNABORTED;
466 	if (softc->ha_lso->so_error) {
467 		error = softc->ha_lso->so_error;
468 		softc->ha_lso->so_error = 0;
469 		ACCEPT_UNLOCK();
470 		printf("%s: socket error %d\n", __func__, error);
471 		goto out;
472 	}
473 	so = TAILQ_FIRST(&softc->ha_lso->so_comp);
474 	if (so == NULL) {
475 		ACCEPT_UNLOCK();
476 		return (EWOULDBLOCK);
477 	}
478 	KASSERT(!(so->so_qstate & SQ_INCOMP), ("accept1: so SQ_INCOMP"));
479 	KASSERT(so->so_qstate & SQ_COMP, ("accept1: so not SQ_COMP"));
480 
481 	/*
482 	 * Before changing the flags on the socket, we have to bump the
483 	 * reference count.  Otherwise, if the protocol calls sofree(),
484 	 * the socket will be released due to a zero refcount.
485 	 */
486 	SOCK_LOCK(so);			/* soref() and so_state update */
487 	soref(so);			/* file descriptor reference */
488 
489 	TAILQ_REMOVE(&softc->ha_lso->so_comp, so, so_list);
490 	softc->ha_lso->so_qlen--;
491 	so->so_state |= SS_NBIO;
492 	so->so_qstate &= ~SQ_COMP;
493 	so->so_head = NULL;
494 
495 	SOCK_UNLOCK(so);
496 	ACCEPT_UNLOCK();
497 
498 	sap = NULL;
499 	error = soaccept(so, &sap);
500 	if (error != 0) {
501 		printf("%s: soaccept() error %d\n", __func__, error);
502 		if (sap != NULL)
503 			free(sap, M_SONAME);
504 		goto out;
505 	}
506 	if (sap != NULL)
507 		free(sap, M_SONAME);
508 	softc->ha_so = so;
509 	ctl_ha_sock_setup(softc);
510 	return (0);
511 
512 out:
513 	ctl_ha_lclose(softc);
514 	return (error);
515 }
516 
517 static int
518 ctl_ha_listen(struct ha_softc *softc)
519 {
520 	struct thread *td = curthread;
521 	struct sockopt opt;
522 	int error, val;
523 
524 	/* Create the socket */
525 	if (softc->ha_lso == NULL) {
526 		error = socreate(PF_INET, &softc->ha_lso, SOCK_STREAM,
527 		    IPPROTO_TCP, td->td_ucred, td);
528 		if (error != 0) {
529 			printf("%s: socreate() error %d\n", __func__, error);
530 			return (error);
531 		}
532 		bzero(&opt, sizeof(struct sockopt));
533 		opt.sopt_dir = SOPT_SET;
534 		opt.sopt_level = SOL_SOCKET;
535 		opt.sopt_name = SO_REUSEADDR;
536 		opt.sopt_val = &val;
537 		opt.sopt_valsize = sizeof(val);
538 		val = 1;
539 		error = sosetopt(softc->ha_lso, &opt);
540 		if (error) {
541 			printf("%s: REUSEADDR setting failed %d\n",
542 			    __func__, error);
543 		}
544 		SOCKBUF_LOCK(&softc->ha_lso->so_rcv);
545 		soupcall_set(softc->ha_lso, SO_RCV, ctl_ha_lupcall, softc);
546 		SOCKBUF_UNLOCK(&softc->ha_lso->so_rcv);
547 	}
548 
549 	error = sobind(softc->ha_lso, (struct sockaddr *)&softc->ha_peer_in, td);
550 	if (error != 0) {
551 		printf("%s: sobind() error %d\n", __func__, error);
552 		goto out;
553 	}
554 	error = solisten(softc->ha_lso, 1, td);
555 	if (error != 0) {
556 		printf("%s: solisten() error %d\n", __func__, error);
557 		goto out;
558 	}
559 	return (0);
560 
561 out:
562 	ctl_ha_lclose(softc);
563 	return (error);
564 }
565 
566 static void
567 ctl_ha_conn_thread(void *arg)
568 {
569 	struct ha_softc *softc = arg;
570 	int error;
571 
572 	while (1) {
573 		if (softc->ha_disconnect || softc->ha_shutdown) {
574 			ctl_ha_close(softc);
575 			ctl_ha_lclose(softc);
576 			softc->ha_disconnect = 0;
577 			if (softc->ha_shutdown)
578 				break;
579 		} else if (softc->ha_so != NULL &&
580 		    (softc->ha_so->so_error ||
581 		     softc->ha_so->so_rcv.sb_state & SBS_CANTRCVMORE))
582 			ctl_ha_close(softc);
583 		if (softc->ha_so == NULL) {
584 			if (softc->ha_lso != NULL)
585 				ctl_ha_accept(softc);
586 			else if (softc->ha_listen)
587 				ctl_ha_listen(softc);
588 			else if (softc->ha_connect)
589 				ctl_ha_connect(softc);
590 		}
591 		if (softc->ha_so != NULL) {
592 			if (softc->ha_connected == 0 &&
593 			    softc->ha_so->so_error == 0 &&
594 			    (softc->ha_so->so_state & SS_ISCONNECTING) == 0) {
595 				softc->ha_connected = 1;
596 				ctl_ha_evt(softc, CTL_HA_CHAN_MAX,
597 				    CTL_HA_EVT_LINK_CHANGE,
598 				    CTL_HA_LINK_ONLINE);
599 				softc->ha_receiving = 1;
600 				error = kproc_kthread_add(ctl_ha_rx_thread,
601 				    softc, &softc->ha_ctl_softc->ctl_proc,
602 				    NULL, 0, 0, "ctl", "ha_rx");
603 				if (error != 0) {
604 					printf("Error creating CTL HA rx thread!\n");
605 					softc->ha_receiving = 0;
606 					softc->ha_disconnect = 1;
607 				}
608 			}
609 			ctl_ha_send(softc);
610 		}
611 		mtx_lock(&softc->ha_lock);
612 		if (softc->ha_so != NULL &&
613 		    (softc->ha_so->so_error ||
614 		     softc->ha_so->so_rcv.sb_state & SBS_CANTRCVMORE))
615 			;
616 		else if (!softc->ha_wakeup)
617 			msleep(&softc->ha_wakeup, &softc->ha_lock, 0, "-", hz);
618 		softc->ha_wakeup = 0;
619 		mtx_unlock(&softc->ha_lock);
620 	}
621 	mtx_lock(&softc->ha_lock);
622 	softc->ha_shutdown = 2;
623 	wakeup(&softc->ha_wakeup);
624 	mtx_unlock(&softc->ha_lock);
625 	kthread_exit();
626 }
627 
628 static int
629 ctl_ha_peer_sysctl(SYSCTL_HANDLER_ARGS)
630 {
631 	struct ha_softc *softc = (struct ha_softc *)arg1;
632 	struct sockaddr_in *sa;
633 	int error, b1, b2, b3, b4, p, num;
634 	char buf[128];
635 
636 	strlcpy(buf, softc->ha_peer, sizeof(buf));
637 	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
638 	if ((error != 0) || (req->newptr == NULL) ||
639 	    strncmp(buf, softc->ha_peer, sizeof(buf)) == 0)
640 		return (error);
641 
642 	sa = &softc->ha_peer_in;
643 	mtx_lock(&softc->ha_lock);
644 	if ((num = sscanf(buf, "connect %d.%d.%d.%d:%d",
645 	    &b1, &b2, &b3, &b4, &p)) >= 4) {
646 		softc->ha_connect = 1;
647 		softc->ha_listen = 0;
648 	} else if ((num = sscanf(buf, "listen %d.%d.%d.%d:%d",
649 	    &b1, &b2, &b3, &b4, &p)) >= 4) {
650 		softc->ha_connect = 0;
651 		softc->ha_listen = 1;
652 	} else {
653 		softc->ha_connect = 0;
654 		softc->ha_listen = 0;
655 		if (buf[0] != 0) {
656 			buf[0] = 0;
657 			error = EINVAL;
658 		}
659 	}
660 	strlcpy(softc->ha_peer, buf, sizeof(softc->ha_peer));
661 	if (softc->ha_connect || softc->ha_listen) {
662 		memset(sa, 0, sizeof(*sa));
663 		sa->sin_len = sizeof(struct sockaddr_in);
664 		sa->sin_family = AF_INET;
665 		sa->sin_port = htons((num >= 5) ? p : 999);
666 		sa->sin_addr.s_addr =
667 		    htonl((b1 << 24) + (b2 << 16) + (b3 << 8) + b4);
668 	}
669 	softc->ha_disconnect = 1;
670 	softc->ha_wakeup = 1;
671 	mtx_unlock(&softc->ha_lock);
672 	wakeup(&softc->ha_wakeup);
673 	return (error);
674 }
675 
676 ctl_ha_status
677 ctl_ha_msg_register(ctl_ha_channel channel, ctl_evt_handler handler)
678 {
679 	struct ha_softc *softc = &ha_softc;
680 
681 	KASSERT(channel < CTL_HA_CHAN_MAX,
682 	    ("Wrong CTL HA channel %d", channel));
683 	softc->ha_handler[channel] = handler;
684 	return (CTL_HA_STATUS_SUCCESS);
685 }
686 
687 ctl_ha_status
688 ctl_ha_msg_deregister(ctl_ha_channel channel)
689 {
690 	struct ha_softc *softc = &ha_softc;
691 
692 	KASSERT(channel < CTL_HA_CHAN_MAX,
693 	    ("Wrong CTL HA channel %d", channel));
694 	softc->ha_handler[channel] = NULL;
695 	return (CTL_HA_STATUS_SUCCESS);
696 }
697 
698 /*
699  * Receive a message of the specified size.
700  */
701 ctl_ha_status
702 ctl_ha_msg_recv(ctl_ha_channel channel, void *addr, size_t len,
703 		int wait)
704 {
705 	struct ha_softc *softc = &ha_softc;
706 	struct uio uio;
707 	struct iovec iov;
708 	int error, flags;
709 
710 	if (!softc->ha_connected)
711 		return (CTL_HA_STATUS_DISCONNECT);
712 
713 	iov.iov_base = addr;
714 	iov.iov_len = len;
715 	uio.uio_iov = &iov;
716 	uio.uio_iovcnt = 1;
717 	uio.uio_rw = UIO_READ;
718 	uio.uio_segflg = UIO_SYSSPACE;
719 	uio.uio_td = curthread;
720 	uio.uio_resid = len;
721 	flags = wait ? 0 : MSG_DONTWAIT;
722 	error = soreceive(softc->ha_so, NULL, &uio, NULL, NULL, &flags);
723 	if (error == 0)
724 		return (CTL_HA_STATUS_SUCCESS);
725 
726 	/* Consider all errors fatal for HA sanity. */
727 	mtx_lock(&softc->ha_lock);
728 	if (softc->ha_connected) {
729 		softc->ha_disconnect = 1;
730 		softc->ha_wakeup = 1;
731 		wakeup(&softc->ha_wakeup);
732 	}
733 	mtx_unlock(&softc->ha_lock);
734 	return (CTL_HA_STATUS_ERROR);
735 }
736 
737 /*
738  * Send a message of the specified size.
739  */
740 ctl_ha_status
741 ctl_ha_msg_send2(ctl_ha_channel channel, const void *addr, size_t len,
742     const void *addr2, size_t len2, int wait)
743 {
744 	struct ha_softc *softc = &ha_softc;
745 	struct mbuf *mb, *newmb;
746 	struct ha_msg_wire hdr;
747 	size_t copylen, off;
748 
749 	if (!softc->ha_connected)
750 		return (CTL_HA_STATUS_DISCONNECT);
751 
752 	newmb = m_getm2(NULL, sizeof(hdr) + len + len2, wait, MT_DATA,
753 	    M_PKTHDR);
754 	if (newmb == NULL) {
755 		/* Consider all errors fatal for HA sanity. */
756 		mtx_lock(&softc->ha_lock);
757 		if (softc->ha_connected) {
758 			softc->ha_disconnect = 1;
759 			softc->ha_wakeup = 1;
760 			wakeup(&softc->ha_wakeup);
761 		}
762 		mtx_unlock(&softc->ha_lock);
763 		printf("%s: Can't allocate mbuf chain\n", __func__);
764 		return (CTL_HA_STATUS_ERROR);
765 	}
766 	hdr.channel = channel;
767 	hdr.length = len + len2;
768 	mb = newmb;
769 	memcpy(mtodo(mb, 0), &hdr, sizeof(hdr));
770 	mb->m_len += sizeof(hdr);
771 	off = 0;
772 	for (; mb != NULL && off < len; mb = mb->m_next) {
773 		copylen = min(M_TRAILINGSPACE(mb), len - off);
774 		memcpy(mtodo(mb, mb->m_len), (const char *)addr + off, copylen);
775 		mb->m_len += copylen;
776 		off += copylen;
777 		if (off == len)
778 			break;
779 	}
780 	KASSERT(off == len, ("%s: off (%zu) != len (%zu)", __func__,
781 	    off, len));
782 	off = 0;
783 	for (; mb != NULL && off < len2; mb = mb->m_next) {
784 		copylen = min(M_TRAILINGSPACE(mb), len2 - off);
785 		memcpy(mtodo(mb, mb->m_len), (const char *)addr2 + off, copylen);
786 		mb->m_len += copylen;
787 		off += copylen;
788 	}
789 	KASSERT(off == len2, ("%s: off (%zu) != len2 (%zu)", __func__,
790 	    off, len2));
791 	newmb->m_pkthdr.len = sizeof(hdr) + len + len2;
792 
793 	mtx_lock(&softc->ha_lock);
794 	if (!softc->ha_connected) {
795 		mtx_unlock(&softc->ha_lock);
796 		m_freem(newmb);
797 		return (CTL_HA_STATUS_DISCONNECT);
798 	}
799 	mbufq_enqueue(&softc->ha_sendq, newmb);
800 	softc->ha_wakeup = 1;
801 	mtx_unlock(&softc->ha_lock);
802 	wakeup(&softc->ha_wakeup);
803 	return (CTL_HA_STATUS_SUCCESS);
804 }
805 
806 ctl_ha_status
807 ctl_ha_msg_send(ctl_ha_channel channel, const void *addr, size_t len,
808     int wait)
809 {
810 
811 	return (ctl_ha_msg_send2(channel, addr, len, NULL, 0, wait));
812 }
813 
814 /*
815  * Allocate a data transfer request structure.
816  */
817 struct ctl_ha_dt_req *
818 ctl_dt_req_alloc(void)
819 {
820 
821 	return (malloc(sizeof(struct ctl_ha_dt_req), M_CTL, M_WAITOK | M_ZERO));
822 }
823 
824 /*
825  * Free a data transfer request structure.
826  */
827 void
828 ctl_dt_req_free(struct ctl_ha_dt_req *req)
829 {
830 
831 	free(req, M_CTL);
832 }
833 
834 /*
835  * Issue a DMA request for a single buffer.
836  */
837 ctl_ha_status
838 ctl_dt_single(struct ctl_ha_dt_req *req)
839 {
840 	struct ha_softc *softc = &ha_softc;
841 	struct ha_dt_msg_wire wire_dt;
842 	ctl_ha_status status;
843 
844 	wire_dt.command = req->command;
845 	wire_dt.size = req->size;
846 	wire_dt.local = req->local;
847 	wire_dt.remote = req->remote;
848 	if (req->command == CTL_HA_DT_CMD_READ && req->callback != NULL) {
849 		mtx_lock(&softc->ha_lock);
850 		TAILQ_INSERT_TAIL(&softc->ha_dts, req, links);
851 		mtx_unlock(&softc->ha_lock);
852 		ctl_ha_msg_send(CTL_HA_CHAN_DATA, &wire_dt, sizeof(wire_dt),
853 		    M_WAITOK);
854 		return (CTL_HA_STATUS_WAIT);
855 	}
856 	if (req->command == CTL_HA_DT_CMD_READ) {
857 		status = ctl_ha_msg_send(CTL_HA_CHAN_DATA, &wire_dt,
858 		    sizeof(wire_dt), M_WAITOK);
859 	} else {
860 		status = ctl_ha_msg_send2(CTL_HA_CHAN_DATA, &wire_dt,
861 		    sizeof(wire_dt), req->local, req->size, M_WAITOK);
862 	}
863 	return (status);
864 }
865 
866 static void
867 ctl_dt_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
868 {
869 	struct ha_softc *softc = &ha_softc;
870 	struct ctl_ha_dt_req *req;
871 	ctl_ha_status isc_status;
872 
873 	if (event == CTL_HA_EVT_MSG_RECV) {
874 		struct ha_dt_msg_wire wire_dt;
875 		uint8_t *tmp;
876 		int size;
877 
878 		size = min(sizeof(wire_dt), param);
879 		isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_DATA, &wire_dt,
880 					     size, M_WAITOK);
881 		if (isc_status != CTL_HA_STATUS_SUCCESS) {
882 			printf("%s: Error receiving message: %d\n",
883 			    __func__, isc_status);
884 			return;
885 		}
886 
887 		if (wire_dt.command == CTL_HA_DT_CMD_READ) {
888 			wire_dt.command = CTL_HA_DT_CMD_WRITE;
889 			tmp = wire_dt.local;
890 			wire_dt.local = wire_dt.remote;
891 			wire_dt.remote = tmp;
892 			ctl_ha_msg_send2(CTL_HA_CHAN_DATA, &wire_dt,
893 			    sizeof(wire_dt), wire_dt.local, wire_dt.size,
894 			    M_WAITOK);
895 		} else if (wire_dt.command == CTL_HA_DT_CMD_WRITE) {
896 			isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_DATA,
897 			    wire_dt.remote, wire_dt.size, M_WAITOK);
898 			mtx_lock(&softc->ha_lock);
899 			TAILQ_FOREACH(req, &softc->ha_dts, links) {
900 				if (req->local == wire_dt.remote) {
901 					TAILQ_REMOVE(&softc->ha_dts, req, links);
902 					break;
903 				}
904 			}
905 			mtx_unlock(&softc->ha_lock);
906 			if (req) {
907 				req->ret = isc_status;
908 				req->callback(req);
909 			}
910 		}
911 	} else if (event == CTL_HA_EVT_LINK_CHANGE) {
912 		CTL_DEBUG_PRINT(("%s: Link state change to %d\n", __func__,
913 		    param));
914 		if (param != CTL_HA_LINK_ONLINE) {
915 			mtx_lock(&softc->ha_lock);
916 			while ((req = TAILQ_FIRST(&softc->ha_dts)) != NULL) {
917 				TAILQ_REMOVE(&softc->ha_dts, req, links);
918 				mtx_unlock(&softc->ha_lock);
919 				req->ret = CTL_HA_STATUS_DISCONNECT;
920 				req->callback(req);
921 				mtx_lock(&softc->ha_lock);
922 			}
923 			mtx_unlock(&softc->ha_lock);
924 		}
925 	} else {
926 		printf("%s: Unknown event %d\n", __func__, event);
927 	}
928 }
929 
930 
931 ctl_ha_status
932 ctl_ha_msg_init(struct ctl_softc *ctl_softc)
933 {
934 	struct ha_softc *softc = &ha_softc;
935 	int error;
936 
937 	softc->ha_ctl_softc = ctl_softc;
938 	mtx_init(&softc->ha_lock, "CTL HA mutex", NULL, MTX_DEF);
939 	mbufq_init(&softc->ha_sendq, INT_MAX);
940 	TAILQ_INIT(&softc->ha_dts);
941 	error = kproc_kthread_add(ctl_ha_conn_thread, softc,
942 	    &ctl_softc->ctl_proc, NULL, 0, 0, "ctl", "ha_tx");
943 	if (error != 0) {
944 		printf("error creating CTL HA connection thread!\n");
945 		mtx_destroy(&softc->ha_lock);
946 		return (CTL_HA_STATUS_ERROR);
947 	}
948 	softc->ha_shutdown_eh = EVENTHANDLER_REGISTER(shutdown_pre_sync,
949 	    ctl_ha_msg_shutdown, ctl_softc, SHUTDOWN_PRI_FIRST);
950 	SYSCTL_ADD_PROC(&ctl_softc->sysctl_ctx,
951 	    SYSCTL_CHILDREN(ctl_softc->sysctl_tree),
952 	    OID_AUTO, "ha_peer", CTLTYPE_STRING | CTLFLAG_RWTUN,
953 	    softc, 0, ctl_ha_peer_sysctl, "A", "HA peer connection method");
954 
955 	if (ctl_ha_msg_register(CTL_HA_CHAN_DATA, ctl_dt_event_handler)
956 	    != CTL_HA_STATUS_SUCCESS) {
957 		printf("%s: ctl_ha_msg_register failed.\n", __func__);
958 	}
959 
960 	return (CTL_HA_STATUS_SUCCESS);
961 };
962 
963 void
964 ctl_ha_msg_shutdown(struct ctl_softc *ctl_softc)
965 {
966 	struct ha_softc *softc = &ha_softc;
967 
968 	/* Disconnect and shutdown threads. */
969 	mtx_lock(&softc->ha_lock);
970 	if (softc->ha_shutdown < 2) {
971 		softc->ha_shutdown = 1;
972 		softc->ha_wakeup = 1;
973 		wakeup(&softc->ha_wakeup);
974 		while (softc->ha_shutdown < 2) {
975 			msleep(&softc->ha_wakeup, &softc->ha_lock, 0,
976 			    "shutdown", hz);
977 		}
978 	}
979 	mtx_unlock(&softc->ha_lock);
980 };
981 
982 ctl_ha_status
983 ctl_ha_msg_destroy(struct ctl_softc *ctl_softc)
984 {
985 	struct ha_softc *softc = &ha_softc;
986 
987 	if (softc->ha_shutdown_eh != NULL) {
988 		EVENTHANDLER_DEREGISTER(shutdown_pre_sync,
989 		    softc->ha_shutdown_eh);
990 		softc->ha_shutdown_eh = NULL;
991 	}
992 
993 	ctl_ha_msg_shutdown(ctl_softc);	/* Just in case. */
994 
995 	if (ctl_ha_msg_deregister(CTL_HA_CHAN_DATA) != CTL_HA_STATUS_SUCCESS)
996 		printf("%s: ctl_ha_msg_deregister failed.\n", __func__);
997 
998 	mtx_destroy(&softc->ha_lock);
999 	return (CTL_HA_STATUS_SUCCESS);
1000 };
1001