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