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
mbufq_init(struct mbufq * q,int limit)74 mbufq_init(struct mbufq *q, int limit)
75 {
76
77 q->head = q->tail = NULL;
78 }
79
80 static void
mbufq_drain(struct mbufq * q)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 *
mbufq_dequeue(struct mbufq * q)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
mbufq_enqueue(struct mbufq * q,struct mbuf * m)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
sbavail(struct sockbuf * sb)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 static void
ctl_ha_conn_wake(struct ha_softc * softc)164 ctl_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
173 static int
ctl_ha_lupcall(struct socket * so,void * arg,int waitflag)174 ctl_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
182 static int
ctl_ha_rupcall(struct socket * so,void * arg,int waitflag)183 ctl_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
191 static int
ctl_ha_supcall(struct socket * so,void * arg,int waitflag)192 ctl_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
200 static void
ctl_ha_evt(struct ha_softc * softc,ctl_ha_channel ch,ctl_ha_event evt,int param)201 ctl_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
217 static void
ctl_ha_close(struct ha_softc * softc)218 ctl_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
254 static void
ctl_ha_lclose(struct ha_softc * softc)255 ctl_ha_lclose(struct ha_softc *softc)
256 {
257
258 if (softc->ha_lso) {
259 SOCKBUF_LOCK(&softc->ha_lso->so_rcv);
260 if (softc->ha_lso->so_rcv.sb_upcall != NULL)
261 soupcall_clear(softc->ha_lso, SO_RCV);
262 SOCKBUF_UNLOCK(&softc->ha_lso->so_rcv);
263 soclose(softc->ha_lso);
264 softc->ha_lso = NULL;
265 }
266 }
267
268 static void
ctl_ha_rx_thread(void * arg)269 ctl_ha_rx_thread(void *arg)
270 {
271 struct ha_softc *softc = arg;
272 struct socket *so = softc->ha_so;
273 struct ha_msg_wire wire_hdr;
274 struct uio uio;
275 struct iovec iov;
276 int error, flags, next;
277
278 bzero(&wire_hdr, sizeof(wire_hdr));
279 while (1) {
280 if (wire_hdr.length > 0)
281 next = wire_hdr.length;
282 else
283 next = sizeof(wire_hdr);
284 SOCKBUF_LOCK(&so->so_rcv);
285 while (sbavail(&so->so_rcv) < next || softc->ha_disconnect) {
286 if (softc->ha_connected == 0 || softc->ha_disconnect ||
287 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
ctl_ha_send(struct ha_softc * softc)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
ctl_ha_sock_setup(struct ha_softc * softc)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
ctl_ha_connect(struct ha_softc * softc)428 ctl_ha_connect(struct ha_softc *softc)
429 {
430 struct thread *td = curthread;
431 struct sockaddr_in sa;
432 struct socket *so;
433 int error;
434
435 /* Create the socket */
436 error = socreate(PF_INET, &so, SOCK_STREAM,
437 IPPROTO_TCP, td->td_ucred, td);
438 if (error != 0) {
439 printf("%s: socreate() error %d\n", __func__, error);
440 return (error);
441 }
442 softc->ha_so = so;
443 ctl_ha_sock_setup(softc);
444
445 memcpy(&sa, &softc->ha_peer_in, sizeof(sa));
446 error = soconnect(so, (struct sockaddr *)&sa, td);
447 if (error != 0) {
448 if (bootverbose)
449 printf("%s: soconnect() error %d\n", __func__, error);
450 goto out;
451 }
452 return (0);
453
454 out:
455 ctl_ha_close(softc);
456 return (error);
457 }
458
459 static int
ctl_ha_accept(struct ha_softc * softc)460 ctl_ha_accept(struct ha_softc *softc)
461 {
462 struct socket *so;
463 struct sockaddr *sap;
464 int error;
465
466 ACCEPT_LOCK();
467 if (softc->ha_lso->so_rcv.sb_state & SBS_CANTRCVMORE)
468 softc->ha_lso->so_error = ECONNABORTED;
469 if (softc->ha_lso->so_error) {
470 error = softc->ha_lso->so_error;
471 softc->ha_lso->so_error = 0;
472 ACCEPT_UNLOCK();
473 printf("%s: socket error %d\n", __func__, error);
474 goto out;
475 }
476 so = TAILQ_FIRST(&softc->ha_lso->so_comp);
477 if (so == NULL) {
478 ACCEPT_UNLOCK();
479 return (EWOULDBLOCK);
480 }
481 KASSERT(!(so->so_qstate & SQ_INCOMP), ("accept1: so SQ_INCOMP"));
482 KASSERT(so->so_qstate & SQ_COMP, ("accept1: so not SQ_COMP"));
483
484 /*
485 * Before changing the flags on the socket, we have to bump the
486 * reference count. Otherwise, if the protocol calls sofree(),
487 * the socket will be released due to a zero refcount.
488 */
489 SOCK_LOCK(so); /* soref() and so_state update */
490 soref(so); /* file descriptor reference */
491
492 TAILQ_REMOVE(&softc->ha_lso->so_comp, so, so_list);
493 softc->ha_lso->so_qlen--;
494 so->so_state |= SS_NBIO;
495 so->so_qstate &= ~SQ_COMP;
496 so->so_head = NULL;
497
498 SOCK_UNLOCK(so);
499 ACCEPT_UNLOCK();
500
501 sap = NULL;
502 error = soaccept(so, &sap);
503 if (error != 0) {
504 printf("%s: soaccept() error %d\n", __func__, error);
505 if (sap != NULL)
506 free(sap, M_SONAME);
507 goto out;
508 }
509 if (sap != NULL)
510 free(sap, M_SONAME);
511 softc->ha_so = so;
512 ctl_ha_sock_setup(softc);
513 return (0);
514
515 out:
516 ctl_ha_lclose(softc);
517 return (error);
518 }
519
520 static int
ctl_ha_listen(struct ha_softc * softc)521 ctl_ha_listen(struct ha_softc *softc)
522 {
523 struct thread *td = curthread;
524 struct sockaddr_in sa;
525 struct sockopt opt;
526 int error, val;
527
528 /* Create the socket */
529 if (softc->ha_lso == NULL) {
530 error = socreate(PF_INET, &softc->ha_lso, SOCK_STREAM,
531 IPPROTO_TCP, td->td_ucred, td);
532 if (error != 0) {
533 printf("%s: socreate() error %d\n", __func__, error);
534 return (error);
535 }
536 bzero(&opt, sizeof(struct sockopt));
537 opt.sopt_dir = SOPT_SET;
538 opt.sopt_level = SOL_SOCKET;
539 opt.sopt_name = SO_REUSEADDR;
540 opt.sopt_val = &val;
541 opt.sopt_valsize = sizeof(val);
542 val = 1;
543 error = sosetopt(softc->ha_lso, &opt);
544 if (error) {
545 printf("%s: REUSEADDR setting failed %d\n",
546 __func__, error);
547 }
548 bzero(&opt, sizeof(struct sockopt));
549 opt.sopt_dir = SOPT_SET;
550 opt.sopt_level = SOL_SOCKET;
551 opt.sopt_name = SO_REUSEPORT;
552 opt.sopt_val = &val;
553 opt.sopt_valsize = sizeof(val);
554 val = 1;
555 error = sosetopt(softc->ha_lso, &opt);
556 if (error) {
557 printf("%s: REUSEPORT setting failed %d\n",
558 __func__, error);
559 }
560 SOCKBUF_LOCK(&softc->ha_lso->so_rcv);
561 soupcall_set(softc->ha_lso, SO_RCV, ctl_ha_lupcall, softc);
562 SOCKBUF_UNLOCK(&softc->ha_lso->so_rcv);
563 }
564
565 memcpy(&sa, &softc->ha_peer_in, sizeof(sa));
566 error = sobind(softc->ha_lso, (struct sockaddr *)&sa, td);
567 if (error != 0) {
568 printf("%s: sobind() error %d\n", __func__, error);
569 goto out;
570 }
571 error = solisten(softc->ha_lso, 1, td);
572 if (error != 0) {
573 printf("%s: solisten() error %d\n", __func__, error);
574 goto out;
575 }
576 return (0);
577
578 out:
579 ctl_ha_lclose(softc);
580 return (error);
581 }
582
583 static void
ctl_ha_conn_thread(void * arg)584 ctl_ha_conn_thread(void *arg)
585 {
586 struct ha_softc *softc = arg;
587 int error;
588
589 while (1) {
590 if (softc->ha_disconnect || softc->ha_shutdown) {
591 ctl_ha_close(softc);
592 if (softc->ha_disconnect == 2 || softc->ha_shutdown)
593 ctl_ha_lclose(softc);
594 softc->ha_disconnect = 0;
595 if (softc->ha_shutdown)
596 break;
597 } else if (softc->ha_so != NULL &&
598 (softc->ha_so->so_error ||
599 softc->ha_so->so_rcv.sb_state & SBS_CANTRCVMORE))
600 ctl_ha_close(softc);
601 if (softc->ha_so == NULL) {
602 if (softc->ha_lso != NULL)
603 ctl_ha_accept(softc);
604 else if (softc->ha_listen)
605 ctl_ha_listen(softc);
606 else if (softc->ha_connect)
607 ctl_ha_connect(softc);
608 }
609 if (softc->ha_so != NULL) {
610 if (softc->ha_connected == 0 &&
611 softc->ha_so->so_error == 0 &&
612 (softc->ha_so->so_state & SS_ISCONNECTING) == 0) {
613 softc->ha_connected = 1;
614 ctl_ha_evt(softc, CTL_HA_CHAN_MAX,
615 CTL_HA_EVT_LINK_CHANGE,
616 CTL_HA_LINK_ONLINE);
617 softc->ha_receiving = 1;
618 error = kproc_kthread_add(ctl_ha_rx_thread,
619 softc, &softc->ha_ctl_softc->ctl_proc,
620 NULL, 0, 0, "ctl", "ha_rx");
621 if (error != 0) {
622 printf("Error creating CTL HA rx thread!\n");
623 softc->ha_receiving = 0;
624 softc->ha_disconnect = 1;
625 }
626 }
627 ctl_ha_send(softc);
628 }
629 mtx_lock(&softc->ha_lock);
630 if (softc->ha_so != NULL &&
631 (softc->ha_so->so_error ||
632 softc->ha_so->so_rcv.sb_state & SBS_CANTRCVMORE))
633 ;
634 else if (!softc->ha_wakeup)
635 msleep(&softc->ha_wakeup, &softc->ha_lock, 0, "-", hz);
636 softc->ha_wakeup = 0;
637 mtx_unlock(&softc->ha_lock);
638 }
639 mtx_lock(&softc->ha_lock);
640 softc->ha_shutdown = 2;
641 wakeup(&softc->ha_wakeup);
642 mtx_unlock(&softc->ha_lock);
643 kthread_exit();
644 }
645
646 static int
ctl_ha_peer_sysctl(SYSCTL_HANDLER_ARGS)647 ctl_ha_peer_sysctl(SYSCTL_HANDLER_ARGS)
648 {
649 struct ha_softc *softc = (struct ha_softc *)arg1;
650 struct sockaddr_in *sa;
651 int error, b1, b2, b3, b4, p, num;
652 char buf[128];
653
654 strlcpy(buf, softc->ha_peer, sizeof(buf));
655 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
656 if ((error != 0) || (req->newptr == NULL) ||
657 strncmp(buf, softc->ha_peer, sizeof(buf)) == 0)
658 return (error);
659
660 sa = &softc->ha_peer_in;
661 mtx_lock(&softc->ha_lock);
662 if ((num = sscanf(buf, "connect %d.%d.%d.%d:%d",
663 &b1, &b2, &b3, &b4, &p)) >= 4) {
664 softc->ha_connect = 1;
665 softc->ha_listen = 0;
666 } else if ((num = sscanf(buf, "listen %d.%d.%d.%d:%d",
667 &b1, &b2, &b3, &b4, &p)) >= 4) {
668 softc->ha_connect = 0;
669 softc->ha_listen = 1;
670 } else {
671 softc->ha_connect = 0;
672 softc->ha_listen = 0;
673 if (buf[0] != 0) {
674 buf[0] = 0;
675 error = EINVAL;
676 }
677 }
678 strlcpy(softc->ha_peer, buf, sizeof(softc->ha_peer));
679 if (softc->ha_connect || softc->ha_listen) {
680 memset(sa, 0, sizeof(*sa));
681 sa->sin_len = sizeof(struct sockaddr_in);
682 sa->sin_family = AF_INET;
683 sa->sin_port = htons((num >= 5) ? p : 999);
684 sa->sin_addr.s_addr =
685 htonl((b1 << 24) + (b2 << 16) + (b3 << 8) + b4);
686 }
687 softc->ha_disconnect = 2;
688 softc->ha_wakeup = 1;
689 mtx_unlock(&softc->ha_lock);
690 wakeup(&softc->ha_wakeup);
691 return (error);
692 }
693
694 ctl_ha_status
ctl_ha_msg_register(ctl_ha_channel channel,ctl_evt_handler handler)695 ctl_ha_msg_register(ctl_ha_channel channel, ctl_evt_handler handler)
696 {
697 struct ha_softc *softc = &ha_softc;
698
699 KASSERT(channel < CTL_HA_CHAN_MAX,
700 ("Wrong CTL HA channel %d", channel));
701 softc->ha_handler[channel] = handler;
702 return (CTL_HA_STATUS_SUCCESS);
703 }
704
705 ctl_ha_status
ctl_ha_msg_deregister(ctl_ha_channel channel)706 ctl_ha_msg_deregister(ctl_ha_channel channel)
707 {
708 struct ha_softc *softc = &ha_softc;
709
710 KASSERT(channel < CTL_HA_CHAN_MAX,
711 ("Wrong CTL HA channel %d", channel));
712 softc->ha_handler[channel] = NULL;
713 return (CTL_HA_STATUS_SUCCESS);
714 }
715
716 /*
717 * Receive a message of the specified size.
718 */
719 ctl_ha_status
ctl_ha_msg_recv(ctl_ha_channel channel,void * addr,size_t len,int wait)720 ctl_ha_msg_recv(ctl_ha_channel channel, void *addr, size_t len,
721 int wait)
722 {
723 struct ha_softc *softc = &ha_softc;
724 struct uio uio;
725 struct iovec iov;
726 int error, flags;
727
728 if (!softc->ha_connected)
729 return (CTL_HA_STATUS_DISCONNECT);
730
731 iov.iov_base = addr;
732 iov.iov_len = len;
733 uio.uio_iov = &iov;
734 uio.uio_iovcnt = 1;
735 uio.uio_rw = UIO_READ;
736 uio.uio_segflg = UIO_SYSSPACE;
737 uio.uio_td = curthread;
738 uio.uio_resid = len;
739 flags = wait ? 0 : MSG_DONTWAIT;
740 error = soreceive(softc->ha_so, NULL, &uio, NULL, NULL, &flags);
741 if (error == 0)
742 return (CTL_HA_STATUS_SUCCESS);
743
744 /* Consider all errors fatal for HA sanity. */
745 mtx_lock(&softc->ha_lock);
746 if (softc->ha_connected) {
747 softc->ha_disconnect = 1;
748 softc->ha_wakeup = 1;
749 wakeup(&softc->ha_wakeup);
750 }
751 mtx_unlock(&softc->ha_lock);
752 return (CTL_HA_STATUS_ERROR);
753 }
754
755 /*
756 * Send a message of the specified size.
757 */
758 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)759 ctl_ha_msg_send2(ctl_ha_channel channel, const void *addr, size_t len,
760 const void *addr2, size_t len2, int wait)
761 {
762 struct ha_softc *softc = &ha_softc;
763 struct mbuf *mb, *newmb;
764 struct ha_msg_wire hdr;
765 size_t copylen, off;
766
767 if (!softc->ha_connected)
768 return (CTL_HA_STATUS_DISCONNECT);
769
770 newmb = m_getm2(NULL, sizeof(hdr) + len + len2, wait, MT_DATA,
771 M_PKTHDR);
772 if (newmb == NULL) {
773 /* Consider all errors fatal for HA sanity. */
774 mtx_lock(&softc->ha_lock);
775 if (softc->ha_connected) {
776 softc->ha_disconnect = 1;
777 softc->ha_wakeup = 1;
778 wakeup(&softc->ha_wakeup);
779 }
780 mtx_unlock(&softc->ha_lock);
781 printf("%s: Can't allocate mbuf chain\n", __func__);
782 return (CTL_HA_STATUS_ERROR);
783 }
784 hdr.channel = channel;
785 hdr.length = len + len2;
786 mb = newmb;
787 memcpy(mtodo(mb, 0), &hdr, sizeof(hdr));
788 mb->m_len += sizeof(hdr);
789 off = 0;
790 for (; mb != NULL && off < len; mb = mb->m_next) {
791 copylen = min(M_TRAILINGSPACE(mb), len - off);
792 memcpy(mtodo(mb, mb->m_len), (const char *)addr + off, copylen);
793 mb->m_len += copylen;
794 off += copylen;
795 if (off == len)
796 break;
797 }
798 KASSERT(off == len, ("%s: off (%zu) != len (%zu)", __func__,
799 off, len));
800 off = 0;
801 for (; mb != NULL && off < len2; mb = mb->m_next) {
802 copylen = min(M_TRAILINGSPACE(mb), len2 - off);
803 memcpy(mtodo(mb, mb->m_len), (const char *)addr2 + off, copylen);
804 mb->m_len += copylen;
805 off += copylen;
806 }
807 KASSERT(off == len2, ("%s: off (%zu) != len2 (%zu)", __func__,
808 off, len2));
809 newmb->m_pkthdr.len = sizeof(hdr) + len + len2;
810
811 mtx_lock(&softc->ha_lock);
812 if (!softc->ha_connected) {
813 mtx_unlock(&softc->ha_lock);
814 m_freem(newmb);
815 return (CTL_HA_STATUS_DISCONNECT);
816 }
817 mbufq_enqueue(&softc->ha_sendq, newmb);
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 ctl_ha_status
ctl_ha_msg_send(ctl_ha_channel channel,const void * addr,size_t len,int wait)825 ctl_ha_msg_send(ctl_ha_channel channel, const void *addr, size_t len,
826 int wait)
827 {
828
829 return (ctl_ha_msg_send2(channel, addr, len, NULL, 0, wait));
830 }
831
832 ctl_ha_status
ctl_ha_msg_abort(ctl_ha_channel channel)833 ctl_ha_msg_abort(ctl_ha_channel channel)
834 {
835 struct ha_softc *softc = &ha_softc;
836
837 mtx_lock(&softc->ha_lock);
838 softc->ha_disconnect = 1;
839 softc->ha_wakeup = 1;
840 mtx_unlock(&softc->ha_lock);
841 wakeup(&softc->ha_wakeup);
842 return (CTL_HA_STATUS_SUCCESS);
843 }
844
845 /*
846 * Allocate a data transfer request structure.
847 */
848 struct ctl_ha_dt_req *
ctl_dt_req_alloc(void)849 ctl_dt_req_alloc(void)
850 {
851
852 return (malloc(sizeof(struct ctl_ha_dt_req), M_CTL, M_WAITOK | M_ZERO));
853 }
854
855 /*
856 * Free a data transfer request structure.
857 */
858 void
ctl_dt_req_free(struct ctl_ha_dt_req * req)859 ctl_dt_req_free(struct ctl_ha_dt_req *req)
860 {
861
862 free(req, M_CTL);
863 }
864
865 /*
866 * Issue a DMA request for a single buffer.
867 */
868 ctl_ha_status
ctl_dt_single(struct ctl_ha_dt_req * req)869 ctl_dt_single(struct ctl_ha_dt_req *req)
870 {
871 struct ha_softc *softc = &ha_softc;
872 struct ha_dt_msg_wire wire_dt;
873 ctl_ha_status status;
874
875 wire_dt.command = req->command;
876 wire_dt.size = req->size;
877 wire_dt.local = req->local;
878 wire_dt.remote = req->remote;
879 if (req->command == CTL_HA_DT_CMD_READ && req->callback != NULL) {
880 mtx_lock(&softc->ha_lock);
881 TAILQ_INSERT_TAIL(&softc->ha_dts, req, links);
882 mtx_unlock(&softc->ha_lock);
883 ctl_ha_msg_send(CTL_HA_CHAN_DATA, &wire_dt, sizeof(wire_dt),
884 M_WAITOK);
885 return (CTL_HA_STATUS_WAIT);
886 }
887 if (req->command == CTL_HA_DT_CMD_READ) {
888 status = ctl_ha_msg_send(CTL_HA_CHAN_DATA, &wire_dt,
889 sizeof(wire_dt), M_WAITOK);
890 } else {
891 status = ctl_ha_msg_send2(CTL_HA_CHAN_DATA, &wire_dt,
892 sizeof(wire_dt), req->local, req->size, M_WAITOK);
893 }
894 return (status);
895 }
896
897 static void
ctl_dt_event_handler(ctl_ha_channel channel,ctl_ha_event event,int param)898 ctl_dt_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
899 {
900 struct ha_softc *softc = &ha_softc;
901 struct ctl_ha_dt_req *req;
902 ctl_ha_status isc_status;
903
904 if (event == CTL_HA_EVT_MSG_RECV) {
905 struct ha_dt_msg_wire wire_dt;
906 uint8_t *tmp;
907 int size;
908
909 size = min(sizeof(wire_dt), param);
910 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_DATA, &wire_dt,
911 size, M_WAITOK);
912 if (isc_status != CTL_HA_STATUS_SUCCESS) {
913 printf("%s: Error receiving message: %d\n",
914 __func__, isc_status);
915 return;
916 }
917
918 if (wire_dt.command == CTL_HA_DT_CMD_READ) {
919 wire_dt.command = CTL_HA_DT_CMD_WRITE;
920 tmp = wire_dt.local;
921 wire_dt.local = wire_dt.remote;
922 wire_dt.remote = tmp;
923 ctl_ha_msg_send2(CTL_HA_CHAN_DATA, &wire_dt,
924 sizeof(wire_dt), wire_dt.local, wire_dt.size,
925 M_WAITOK);
926 } else if (wire_dt.command == CTL_HA_DT_CMD_WRITE) {
927 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_DATA,
928 wire_dt.remote, wire_dt.size, M_WAITOK);
929 mtx_lock(&softc->ha_lock);
930 TAILQ_FOREACH(req, &softc->ha_dts, links) {
931 if (req->local == wire_dt.remote) {
932 TAILQ_REMOVE(&softc->ha_dts, req, links);
933 break;
934 }
935 }
936 mtx_unlock(&softc->ha_lock);
937 if (req) {
938 req->ret = isc_status;
939 req->callback(req);
940 }
941 }
942 } else if (event == CTL_HA_EVT_LINK_CHANGE) {
943 CTL_DEBUG_PRINT(("%s: Link state change to %d\n", __func__,
944 param));
945 if (param != CTL_HA_LINK_ONLINE) {
946 mtx_lock(&softc->ha_lock);
947 while ((req = TAILQ_FIRST(&softc->ha_dts)) != NULL) {
948 TAILQ_REMOVE(&softc->ha_dts, req, links);
949 mtx_unlock(&softc->ha_lock);
950 req->ret = CTL_HA_STATUS_DISCONNECT;
951 req->callback(req);
952 mtx_lock(&softc->ha_lock);
953 }
954 mtx_unlock(&softc->ha_lock);
955 }
956 } else {
957 printf("%s: Unknown event %d\n", __func__, event);
958 }
959 }
960
961
962 ctl_ha_status
ctl_ha_msg_init(struct ctl_softc * ctl_softc)963 ctl_ha_msg_init(struct ctl_softc *ctl_softc)
964 {
965 struct ha_softc *softc = &ha_softc;
966 int error;
967
968 softc->ha_ctl_softc = ctl_softc;
969 mtx_init(&softc->ha_lock, "CTL HA mutex", NULL, MTX_DEF);
970 mbufq_init(&softc->ha_sendq, INT_MAX);
971 TAILQ_INIT(&softc->ha_dts);
972 error = kproc_kthread_add(ctl_ha_conn_thread, softc,
973 &ctl_softc->ctl_proc, NULL, 0, 0, "ctl", "ha_tx");
974 if (error != 0) {
975 printf("error creating CTL HA connection thread!\n");
976 mtx_destroy(&softc->ha_lock);
977 return (CTL_HA_STATUS_ERROR);
978 }
979 softc->ha_shutdown_eh = EVENTHANDLER_REGISTER(shutdown_pre_sync,
980 ctl_ha_msg_shutdown, ctl_softc, SHUTDOWN_PRI_FIRST);
981 SYSCTL_ADD_PROC(&ctl_softc->sysctl_ctx,
982 SYSCTL_CHILDREN(ctl_softc->sysctl_tree),
983 OID_AUTO, "ha_peer", CTLTYPE_STRING | CTLFLAG_RWTUN,
984 softc, 0, ctl_ha_peer_sysctl, "A", "HA peer connection method");
985
986 if (ctl_ha_msg_register(CTL_HA_CHAN_DATA, ctl_dt_event_handler)
987 != CTL_HA_STATUS_SUCCESS) {
988 printf("%s: ctl_ha_msg_register failed.\n", __func__);
989 }
990
991 return (CTL_HA_STATUS_SUCCESS);
992 };
993
994 void
ctl_ha_msg_shutdown(struct ctl_softc * ctl_softc)995 ctl_ha_msg_shutdown(struct ctl_softc *ctl_softc)
996 {
997 struct ha_softc *softc = &ha_softc;
998
999 /* Disconnect and shutdown threads. */
1000 mtx_lock(&softc->ha_lock);
1001 if (softc->ha_shutdown < 2) {
1002 softc->ha_shutdown = 1;
1003 softc->ha_wakeup = 1;
1004 wakeup(&softc->ha_wakeup);
1005 while (softc->ha_shutdown < 2 && !SCHEDULER_STOPPED()) {
1006 msleep(&softc->ha_wakeup, &softc->ha_lock, 0,
1007 "shutdown", hz);
1008 }
1009 }
1010 mtx_unlock(&softc->ha_lock);
1011 };
1012
1013 ctl_ha_status
ctl_ha_msg_destroy(struct ctl_softc * ctl_softc)1014 ctl_ha_msg_destroy(struct ctl_softc *ctl_softc)
1015 {
1016 struct ha_softc *softc = &ha_softc;
1017
1018 if (softc->ha_shutdown_eh != NULL) {
1019 EVENTHANDLER_DEREGISTER(shutdown_pre_sync,
1020 softc->ha_shutdown_eh);
1021 softc->ha_shutdown_eh = NULL;
1022 }
1023
1024 ctl_ha_msg_shutdown(ctl_softc); /* Just in case. */
1025
1026 if (ctl_ha_msg_deregister(CTL_HA_CHAN_DATA) != CTL_HA_STATUS_SUCCESS)
1027 printf("%s: ctl_ha_msg_deregister failed.\n", __func__);
1028
1029 mtx_destroy(&softc->ha_lock);
1030 return (CTL_HA_STATUS_SUCCESS);
1031 };
1032