4 * Copyright (C) 1997-1999 Kunihiro Ishiguro
5 * Copyright (C) 2015-2018 Cumulus Networks, Inc.
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 * You should have received a copy of the GNU General Public License along
19 * with this program; see the file COPYING; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 /* clang-format off */
26 #include <errno.h> /* for errno */
27 #include <netinet/in.h> /* for sockaddr_in */
28 #include <stdint.h> /* for uint8_t */
29 #include <stdio.h> /* for snprintf */
30 #include <sys/socket.h> /* for sockaddr_storage, AF_UNIX, accept... */
31 #include <sys/stat.h> /* for umask, mode_t */
32 #include <sys/un.h> /* for sockaddr_un */
33 #include <time.h> /* for NULL, tm, gmtime, time_t */
34 #include <unistd.h> /* for close, unlink, ssize_t */
36 #include "lib/buffer.h" /* for BUFFER_EMPTY, BUFFER_ERROR, BUFFE... */
37 #include "lib/command.h" /* for vty, install_element, CMD_SUCCESS... */
38 #include "lib/hook.h" /* for DEFINE_HOOK, DEFINE_KOOH, hook_call */
39 #include "lib/linklist.h" /* for ALL_LIST_ELEMENTS_RO, ALL_LIST_EL... */
40 #include "lib/libfrr.h" /* for frr_zclient_addr */
41 #include "lib/log.h" /* for zlog_warn, zlog_debug, safe_strerror */
42 #include "lib/memory.h" /* for MTYPE_TMP, XCALLOC, XFREE */
43 #include "lib/monotime.h" /* for monotime, ONE_DAY_SECOND, ONE_WEE... */
44 #include "lib/network.h" /* for set_nonblocking */
45 #include "lib/privs.h" /* for zebra_privs_t, ZPRIVS_LOWER, ZPRI... */
46 #include "lib/route_types.h" /* for ZEBRA_ROUTE_MAX */
47 #include "lib/sockopt.h" /* for setsockopt_so_recvbuf, setsockopt... */
48 #include "lib/sockunion.h" /* for sockopt_reuseaddr, sockopt_reuseport */
49 #include "lib/stream.h" /* for STREAM_SIZE, stream (ptr only), ... */
50 #include "lib/thread.h" /* for thread (ptr only), THREAD_ARG, ... */
51 #include "lib/vrf.h" /* for vrf_info_lookup, VRF_DEFAULT */
52 #include "lib/vty.h" /* for vty_out, vty (ptr only) */
53 #include "lib/zassert.h" /* for assert */
54 #include "lib/zclient.h" /* for zmsghdr, ZEBRA_HEADER_SIZE, ZEBRA... */
55 #include "lib/frr_pthread.h" /* for frr_pthread_new, frr_pthread_stop... */
56 #include "lib/frratomic.h" /* for atomic_load_explicit, atomic_stor... */
57 #include "lib/lib_errors.h" /* for generic ferr ids */
59 #include "zebra/debug.h" /* for various debugging macros */
60 #include "zebra/rib.h" /* for rib_score_proto */
61 #include "zebra/zapi_msg.h" /* for zserv_handle_commands */
62 #include "zebra/zebra_vrf.h" /* for zebra_vrf_lookup_by_id, zvrf */
63 #include "zebra/zserv.h" /* for zserv */
64 #include "zebra/zebra_router.h"
65 #include "zebra/zebra_errors.h" /* for error messages */
69 extern struct zebra_privs_t zserv_privs
;
71 /* The listener socket for clients connecting to us */
75 * Client thread events.
77 * These are used almost exclusively by client threads to drive their own event
78 * loops. The only exception is in zserv_client_create(), which pushes an
79 * initial ZSERV_CLIENT_READ event to start the API handler loop.
81 enum zserv_client_event
{
82 /* Schedule a socket read */
84 /* Schedule a buffer write */
91 * These are used by client threads to notify the main thread about various
92 * events and to make processing requests.
95 /* Schedule listen job on Zebra API socket */
97 /* The calling client has packets on its input buffer */
98 ZSERV_PROCESS_MESSAGES
,
99 /* The calling client wishes to be killed */
100 ZSERV_HANDLE_CLIENT_FAIL
,
104 * Zebra server event driver for all client threads.
106 * This is essentially a wrapper around thread_add_event() that centralizes
107 * those scheduling calls into one place.
109 * All calls to this function schedule an event on the pthread running the
113 * the client in question, and thread target
116 * the event to notify them about
118 static void zserv_client_event(struct zserv
*client
,
119 enum zserv_client_event event
);
122 * Zebra server event driver for the main thread.
124 * This is essentially a wrapper around thread_add_event() that centralizes
125 * those scheduling calls into one place.
127 * All calls to this function schedule an event on Zebra's main pthread.
130 * the client in question
133 * the event to notify the main thread about
135 static void zserv_event(struct zserv
*client
, enum zserv_event event
);
138 /* Client thread lifecycle -------------------------------------------------- */
141 * Log zapi message to zlog.
152 static void zserv_log_message(const char *errmsg
, struct stream
*msg
,
155 zlog_debug("Rx'd ZAPI message");
157 zlog_debug("%s", errmsg
);
159 zlog_debug(" Length: %d", hdr
->length
);
160 zlog_debug("Command: %s", zserv_command_string(hdr
->command
));
161 zlog_debug(" VRF: %u", hdr
->vrf_id
);
163 zlog_hexdump(msg
->data
, STREAM_READABLE(msg
));
167 * Gracefully shut down a client connection.
169 * Cancel any pending tasks for the client's thread. Then schedule a task on
170 * the main thread to shut down the calling thread.
172 * It is not safe to close the client socket in this function. The socket is
173 * owned by the main thread.
175 * Must be called from the client pthread, never the main thread.
177 static void zserv_client_fail(struct zserv
*client
)
179 flog_warn(EC_ZEBRA_CLIENT_IO_ERROR
,
180 "Client '%s' encountered an error and is shutting down.",
181 zebra_route_string(client
->proto
));
183 atomic_store_explicit(&client
->pthread
->running
, false,
184 memory_order_relaxed
);
186 THREAD_OFF(client
->t_read
);
187 THREAD_OFF(client
->t_write
);
188 zserv_event(client
, ZSERV_HANDLE_CLIENT_FAIL
);
192 * Write all pending messages to client socket.
194 * This function first attempts to flush any buffered data. If unsuccessful,
195 * the function reschedules itself and returns. If successful, it pops all
196 * available messages from the output queue and continues to write data
197 * directly to the socket until the socket would block. If the socket never
198 * blocks and all data is written, the function returns without rescheduling
199 * itself. If the socket ends up throwing EWOULDBLOCK, the remaining data is
200 * buffered and the function reschedules itself.
202 * The utility of the buffer is that it allows us to vastly reduce lock
203 * contention by allowing us to pop *all* messages off the output queue at once
204 * instead of locking and unlocking each time we want to pop a single message
205 * off the queue. The same thing could arguably be accomplished faster by
206 * allowing the main thread to write directly into the buffer instead of
207 * enqueuing packets onto an intermediary queue, but the intermediary queue
208 * allows us to expose information about input and output queues to the user in
209 * terms of number of packets rather than size of data.
211 static int zserv_write(struct thread
*thread
)
213 struct zserv
*client
= THREAD_ARG(thread
);
216 struct stream_fifo
*cache
;
218 /* If we have any data pending, try to flush it first */
219 switch (buffer_flush_all(client
->wb
, client
->sock
)) {
223 atomic_store_explicit(&client
->last_write_time
,
224 (uint32_t)monotime(NULL
),
225 memory_order_relaxed
);
226 zserv_client_event(client
, ZSERV_CLIENT_WRITE
);
232 cache
= stream_fifo_new();
234 pthread_mutex_lock(&client
->obuf_mtx
);
236 while (stream_fifo_head(client
->obuf_fifo
))
237 stream_fifo_push(cache
,
238 stream_fifo_pop(client
->obuf_fifo
));
240 pthread_mutex_unlock(&client
->obuf_mtx
);
244 stream_set_getp(msg
, 0);
245 wcmd
= stream_getw_from(msg
, 6);
248 while (stream_fifo_head(cache
)) {
249 msg
= stream_fifo_pop(cache
);
250 buffer_put(client
->wb
, STREAM_DATA(msg
), stream_get_endp(msg
));
254 stream_fifo_free(cache
);
256 /* If we have any data pending, try to flush it first */
257 switch (buffer_flush_all(client
->wb
, client
->sock
)) {
261 atomic_store_explicit(&client
->last_write_time
,
262 (uint32_t)monotime(NULL
),
263 memory_order_relaxed
);
264 zserv_client_event(client
, ZSERV_CLIENT_WRITE
);
270 atomic_store_explicit(&client
->last_write_cmd
, wcmd
,
271 memory_order_relaxed
);
273 atomic_store_explicit(&client
->last_write_time
,
274 (uint32_t)monotime(NULL
), memory_order_relaxed
);
279 flog_warn(EC_ZEBRA_CLIENT_WRITE_FAILED
,
280 "%s: could not write to %s [fd = %d], closing.", __func__
,
281 zebra_route_string(client
->proto
), client
->sock
);
282 zserv_client_fail(client
);
287 * Read and process data from a client socket.
289 * The responsibilities here are to read raw data from the client socket,
290 * validate the header, encapsulate it into a single stream object, push it
291 * onto the input queue and then notify the main thread that there is new data
294 * This function first looks for any data in the client structure's working
295 * input buffer. If data is present, it is assumed that reading stopped in a
296 * previous invocation of this task and needs to be resumed to finish a message.
297 * Otherwise, the socket data stream is assumed to be at the beginning of a new
298 * ZAPI message (specifically at the header). The header is read and validated.
299 * If the header passed validation then the length field found in the header is
300 * used to compute the total length of the message. That much data is read (but
301 * not inspected), appended to the header, placed into a stream and pushed onto
302 * the client's input queue. A task is then scheduled on the main thread to
303 * process the client's input queue. Finally, if all of this was successful,
304 * this task reschedules itself.
306 * Any failure in any of these actions is handled by terminating the client.
308 static int zserv_read(struct thread
*thread
)
310 struct zserv
*client
= THREAD_ARG(thread
);
313 struct stream_fifo
*cache
;
319 p2p_orig
= atomic_load_explicit(&zrouter
.packets_to_process
,
320 memory_order_relaxed
);
321 cache
= stream_fifo_new();
323 sock
= THREAD_FD(thread
);
330 already
= stream_get_endp(client
->ibuf_work
);
332 /* Read length and command (if we don't have it already). */
333 if (already
< ZEBRA_HEADER_SIZE
) {
334 nb
= stream_read_try(client
->ibuf_work
, sock
,
335 ZEBRA_HEADER_SIZE
- already
);
336 if ((nb
== 0 || nb
== -1)) {
337 if (IS_ZEBRA_DEBUG_EVENT
)
338 zlog_debug("connection closed socket [%d]",
342 if (nb
!= (ssize_t
)(ZEBRA_HEADER_SIZE
- already
)) {
343 /* Try again later. */
346 already
= ZEBRA_HEADER_SIZE
;
349 /* Reset to read from the beginning of the incoming packet. */
350 stream_set_getp(client
->ibuf_work
, 0);
352 /* Fetch header values */
353 hdrvalid
= zapi_parse_header(client
->ibuf_work
, &hdr
);
356 snprintf(errmsg
, sizeof(errmsg
),
357 "%s: Message has corrupt header", __func__
);
358 zserv_log_message(errmsg
, client
->ibuf_work
, NULL
);
362 /* Validate header */
363 if (hdr
.marker
!= ZEBRA_HEADER_MARKER
364 || hdr
.version
!= ZSERV_VERSION
) {
366 errmsg
, sizeof(errmsg
),
367 "Message has corrupt header\n%s: socket %d version mismatch, marker %d, version %d",
368 __func__
, sock
, hdr
.marker
, hdr
.version
);
369 zserv_log_message(errmsg
, client
->ibuf_work
, &hdr
);
372 if (hdr
.length
< ZEBRA_HEADER_SIZE
) {
374 errmsg
, sizeof(errmsg
),
375 "Message has corrupt header\n%s: socket %d message length %u is less than header size %d",
376 __func__
, sock
, hdr
.length
, ZEBRA_HEADER_SIZE
);
377 zserv_log_message(errmsg
, client
->ibuf_work
, &hdr
);
380 if (hdr
.length
> STREAM_SIZE(client
->ibuf_work
)) {
382 errmsg
, sizeof(errmsg
),
383 "Message has corrupt header\n%s: socket %d message length %u exceeds buffer size %lu",
384 __func__
, sock
, hdr
.length
,
385 (unsigned long)STREAM_SIZE(client
->ibuf_work
));
386 zserv_log_message(errmsg
, client
->ibuf_work
, &hdr
);
390 /* Read rest of data. */
391 if (already
< hdr
.length
) {
392 nb
= stream_read_try(client
->ibuf_work
, sock
,
393 hdr
.length
- already
);
394 if ((nb
== 0 || nb
== -1)) {
395 if (IS_ZEBRA_DEBUG_EVENT
)
397 "connection closed [%d] when reading zebra data",
401 if (nb
!= (ssize_t
)(hdr
.length
- already
)) {
402 /* Try again later. */
407 /* Debug packet information. */
408 if (IS_ZEBRA_DEBUG_EVENT
)
409 zlog_debug("zebra message comes from socket [%d]",
412 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
413 zserv_log_message(NULL
, client
->ibuf_work
, &hdr
);
415 stream_set_getp(client
->ibuf_work
, 0);
416 struct stream
*msg
= stream_dup(client
->ibuf_work
);
418 stream_fifo_push(cache
, msg
);
419 stream_reset(client
->ibuf_work
);
423 if (p2p
< p2p_orig
) {
424 /* update session statistics */
425 atomic_store_explicit(&client
->last_read_time
, monotime(NULL
),
426 memory_order_relaxed
);
427 atomic_store_explicit(&client
->last_read_cmd
, hdr
.command
,
428 memory_order_relaxed
);
430 /* publish read packets on client's input queue */
431 pthread_mutex_lock(&client
->ibuf_mtx
);
434 stream_fifo_push(client
->ibuf_fifo
,
435 stream_fifo_pop(cache
));
437 pthread_mutex_unlock(&client
->ibuf_mtx
);
439 /* Schedule job to process those packets */
440 zserv_event(client
, ZSERV_PROCESS_MESSAGES
);
444 if (IS_ZEBRA_DEBUG_PACKET
)
445 zlog_debug("Read %d packets", p2p_orig
- p2p
);
447 /* Reschedule ourselves */
448 zserv_client_event(client
, ZSERV_CLIENT_READ
);
450 stream_fifo_free(cache
);
455 stream_fifo_free(cache
);
456 zserv_client_fail(client
);
460 static void zserv_client_event(struct zserv
*client
,
461 enum zserv_client_event event
)
464 case ZSERV_CLIENT_READ
:
465 thread_add_read(client
->pthread
->master
, zserv_read
, client
,
466 client
->sock
, &client
->t_read
);
468 case ZSERV_CLIENT_WRITE
:
469 thread_add_write(client
->pthread
->master
, zserv_write
, client
,
470 client
->sock
, &client
->t_write
);
475 /* Main thread lifecycle ---------------------------------------------------- */
478 * Read and process messages from a client.
480 * This task runs on the main pthread. It is scheduled by client pthreads when
481 * they have new messages available on their input queues. The client is passed
482 * as the task argument.
484 * Each message is popped off the client's input queue and the action associated
485 * with the message is executed. This proceeds until there are no more messages,
486 * an error occurs, or the processing limit is reached.
488 * The client's I/O thread can push at most zrouter.packets_to_process messages
489 * onto the input buffer before notifying us there are packets to read. As long
490 * as we always process zrouter.packets_to_process messages here, then we can
491 * rely on the read thread to handle queuing this task enough times to process
492 * everything on the input queue.
494 static int zserv_process_messages(struct thread
*thread
)
496 struct zserv
*client
= THREAD_ARG(thread
);
498 struct stream_fifo
*cache
= stream_fifo_new();
499 uint32_t p2p
= zrouter
.packets_to_process
;
500 bool need_resched
= false;
502 pthread_mutex_lock(&client
->ibuf_mtx
);
505 for (i
= 0; i
< p2p
&& stream_fifo_head(client
->ibuf_fifo
);
507 msg
= stream_fifo_pop(client
->ibuf_fifo
);
508 stream_fifo_push(cache
, msg
);
513 /* Need to reschedule processing work if there are still
514 * packets in the fifo.
516 if (stream_fifo_head(client
->ibuf_fifo
))
519 pthread_mutex_unlock(&client
->ibuf_mtx
);
521 while (stream_fifo_head(cache
)) {
522 msg
= stream_fifo_pop(cache
);
523 zserv_handle_commands(client
, msg
);
527 stream_fifo_free(cache
);
529 /* Reschedule ourselves if necessary */
531 zserv_event(client
, ZSERV_PROCESS_MESSAGES
);
536 int zserv_send_message(struct zserv
*client
, struct stream
*msg
)
539 * This is a somewhat poorly named variable added with Zebra's portion
540 * of the label manager. That component does not use the regular
541 * zserv/zapi_msg interface for handling its messages, as the client
542 * itself runs in-process. Instead it uses synchronous writes on the
543 * zserv client's socket directly in the zread* handlers for its
544 * message types. Furthermore, it cannot handle the usual messages
545 * Zebra sends (such as those for interface changes) and so has added
546 * this flag and check here as a hack to suppress all messages that it
547 * does not explicitly know about.
549 * In any case this needs to be cleaned up at some point.
552 * zread_label_manager_request
553 * zsend_label_manager_connect_response
554 * zsend_assign_label_chunk_response
557 if (client
->is_synchronous
)
560 pthread_mutex_lock(&client
->obuf_mtx
);
562 stream_fifo_push(client
->obuf_fifo
, msg
);
564 pthread_mutex_unlock(&client
->obuf_mtx
);
566 zserv_client_event(client
, ZSERV_CLIENT_WRITE
);
572 /* Hooks for client connect / disconnect */
573 DEFINE_HOOK(zserv_client_connect
, (struct zserv
*client
), (client
));
574 DEFINE_KOOH(zserv_client_close
, (struct zserv
*client
), (client
));
577 * Deinitialize zebra client.
579 * - Deregister and deinitialize related internal resources
580 * - Gracefully close socket
581 * - Free associated resources
582 * - Free client structure
584 * This does *not* take any action on the struct thread * fields. These are
585 * managed by the owning pthread and any tasks associated with them must have
586 * been stopped prior to invoking this function.
588 static void zserv_client_free(struct zserv
*client
)
590 hook_call(zserv_client_close
, client
);
592 /* Close file descriptor. */
594 unsigned long nroutes
;
598 nroutes
= rib_score_proto(client
->proto
, client
->instance
);
600 "client %d disconnected. %lu %s routes removed from the rib",
601 client
->sock
, nroutes
,
602 zebra_route_string(client
->proto
));
606 /* Free stream buffers. */
607 if (client
->ibuf_work
)
608 stream_free(client
->ibuf_work
);
609 if (client
->obuf_work
)
610 stream_free(client
->obuf_work
);
611 if (client
->ibuf_fifo
)
612 stream_fifo_free(client
->ibuf_fifo
);
613 if (client
->obuf_fifo
)
614 stream_fifo_free(client
->obuf_fifo
);
616 buffer_free(client
->wb
);
618 /* Free buffer mutexes */
619 pthread_mutex_destroy(&client
->obuf_mtx
);
620 pthread_mutex_destroy(&client
->ibuf_mtx
);
623 for (afi_t afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
624 for (int i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
625 vrf_bitmap_free(client
->redist
[afi
][i
]);
627 vrf_bitmap_free(client
->redist_default
[afi
]);
629 vrf_bitmap_free(client
->ifinfo
);
630 vrf_bitmap_free(client
->ridinfo
);
632 XFREE(MTYPE_TMP
, client
);
635 void zserv_close_client(struct zserv
*client
)
637 /* synchronously stop and join pthread */
638 frr_pthread_stop(client
->pthread
, NULL
);
640 if (IS_ZEBRA_DEBUG_EVENT
)
641 zlog_debug("Closing client '%s'",
642 zebra_route_string(client
->proto
));
644 thread_cancel_event(zrouter
.master
, client
);
645 THREAD_OFF(client
->t_cleanup
);
646 THREAD_OFF(client
->t_process
);
648 /* destroy pthread */
649 frr_pthread_destroy(client
->pthread
);
650 client
->pthread
= NULL
;
652 /* remove from client list */
653 listnode_delete(zrouter
.client_list
, client
);
656 zserv_client_free(client
);
660 * This task is scheduled by a ZAPI client pthread on the main pthread when it
661 * wants to stop itself. When this executes, the client connection should
662 * already have been closed and the thread will most likely have died, but its
663 * resources still need to be cleaned up.
665 static int zserv_handle_client_fail(struct thread
*thread
)
667 struct zserv
*client
= THREAD_ARG(thread
);
669 zserv_close_client(client
);
674 * Create a new client.
676 * This is called when a new connection is accept()'d on the ZAPI socket. It
677 * initializes new client structure, notifies any subscribers of the connection
678 * event and spawns the client's thread.
681 * client's socket file descriptor
683 static struct zserv
*zserv_client_create(int sock
)
685 struct zserv
*client
;
689 client
= XCALLOC(MTYPE_TMP
, sizeof(struct zserv
));
691 /* Make client input/output buffer. */
693 client
->ibuf_fifo
= stream_fifo_new();
694 client
->obuf_fifo
= stream_fifo_new();
695 client
->ibuf_work
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
696 client
->obuf_work
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
697 pthread_mutex_init(&client
->ibuf_mtx
, NULL
);
698 pthread_mutex_init(&client
->obuf_mtx
, NULL
);
699 client
->wb
= buffer_new(0);
701 /* Set table number. */
702 client
->rtm_table
= zrouter
.rtm_table_default
;
704 atomic_store_explicit(&client
->connect_time
, (uint32_t) monotime(NULL
),
705 memory_order_relaxed
);
707 /* Initialize flags */
708 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
709 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
710 client
->redist
[afi
][i
] = vrf_bitmap_init();
711 client
->redist_default
[afi
] = vrf_bitmap_init();
713 client
->ifinfo
= vrf_bitmap_init();
714 client
->ridinfo
= vrf_bitmap_init();
716 /* by default, it's not a synchronous client */
717 client
->is_synchronous
= 0;
719 /* Add this client to linked list. */
720 listnode_add(zrouter
.client_list
, client
);
722 struct frr_pthread_attr zclient_pthr_attrs
= {
723 .start
= frr_pthread_attr_default
.start
,
724 .stop
= frr_pthread_attr_default
.stop
727 frr_pthread_new(&zclient_pthr_attrs
, "Zebra API client thread",
730 /* start read loop */
731 zserv_client_event(client
, ZSERV_CLIENT_READ
);
734 hook_call(zserv_client_connect
, client
);
737 frr_pthread_run(client
->pthread
, NULL
);
743 * Accept socket connection.
745 static int zserv_accept(struct thread
*thread
)
749 struct sockaddr_in client
;
752 accept_sock
= THREAD_FD(thread
);
754 /* Reregister myself. */
755 zserv_event(NULL
, ZSERV_ACCEPT
);
757 len
= sizeof(struct sockaddr_in
);
758 client_sock
= accept(accept_sock
, (struct sockaddr
*)&client
, &len
);
760 if (client_sock
< 0) {
761 flog_err_sys(EC_LIB_SOCKET
, "Can't accept zebra socket: %s",
762 safe_strerror(errno
));
766 /* Make client socket non-blocking. */
767 set_nonblocking(client_sock
);
769 /* Create new zebra client. */
770 zserv_client_create(client_sock
);
775 void zserv_start(char *path
)
779 struct sockaddr_storage sa
;
782 if (!frr_zclient_addr(&sa
, &sa_len
, path
))
783 /* should be caught in zebra main() */
787 old_mask
= umask(0077);
789 /* Make UNIX domain socket. */
790 zsock
= socket(sa
.ss_family
, SOCK_STREAM
, 0);
792 flog_err_sys(EC_LIB_SOCKET
, "Can't create zserv socket: %s",
793 safe_strerror(errno
));
797 if (sa
.ss_family
!= AF_UNIX
) {
798 sockopt_reuseaddr(zsock
);
799 sockopt_reuseport(zsock
);
801 struct sockaddr_un
*suna
= (struct sockaddr_un
*)&sa
;
802 if (suna
->sun_path
[0])
803 unlink(suna
->sun_path
);
806 frr_elevate_privs(&zserv_privs
) {
807 setsockopt_so_recvbuf(zsock
, 1048576);
808 setsockopt_so_sendbuf(zsock
, 1048576);
811 frr_elevate_privs((sa
.ss_family
!= AF_UNIX
) ? &zserv_privs
: NULL
) {
812 ret
= bind(zsock
, (struct sockaddr
*)&sa
, sa_len
);
815 flog_err_sys(EC_LIB_SOCKET
, "Can't bind zserv socket on %s: %s",
816 path
, safe_strerror(errno
));
822 ret
= listen(zsock
, 5);
824 flog_err_sys(EC_LIB_SOCKET
,
825 "Can't listen to zserv socket %s: %s", path
,
826 safe_strerror(errno
));
834 zserv_event(NULL
, ZSERV_ACCEPT
);
837 void zserv_event(struct zserv
*client
, enum zserv_event event
)
841 thread_add_read(zrouter
.master
, zserv_accept
, NULL
, zsock
,
844 case ZSERV_PROCESS_MESSAGES
:
845 thread_add_event(zrouter
.master
, zserv_process_messages
, client
,
846 0, &client
->t_process
);
848 case ZSERV_HANDLE_CLIENT_FAIL
:
849 thread_add_event(zrouter
.master
, zserv_handle_client_fail
,
850 client
, 0, &client
->t_cleanup
);
855 /* General purpose ---------------------------------------------------------- */
857 #define ZEBRA_TIME_BUF 32
858 static char *zserv_time_buf(time_t *time1
, char *buf
, int buflen
)
864 assert(buflen
>= ZEBRA_TIME_BUF
);
865 assert(time1
!= NULL
);
868 snprintf(buf
, buflen
, "never ");
872 now
= monotime(NULL
);
876 if (now
< ONE_DAY_SECOND
)
877 snprintf(buf
, buflen
, "%02d:%02d:%02d", tm
->tm_hour
, tm
->tm_min
,
879 else if (now
< ONE_WEEK_SECOND
)
880 snprintf(buf
, buflen
, "%dd%02dh%02dm", tm
->tm_yday
, tm
->tm_hour
,
883 snprintf(buf
, buflen
, "%02dw%dd%02dh", tm
->tm_yday
/ 7,
884 tm
->tm_yday
- ((tm
->tm_yday
/ 7) * 7), tm
->tm_hour
);
888 static void zebra_show_client_detail(struct vty
*vty
, struct zserv
*client
)
890 char cbuf
[ZEBRA_TIME_BUF
], rbuf
[ZEBRA_TIME_BUF
];
891 char wbuf
[ZEBRA_TIME_BUF
], nhbuf
[ZEBRA_TIME_BUF
], mbuf
[ZEBRA_TIME_BUF
];
892 time_t connect_time
, last_read_time
, last_write_time
;
893 uint32_t last_read_cmd
, last_write_cmd
;
895 vty_out(vty
, "Client: %s", zebra_route_string(client
->proto
));
896 if (client
->instance
)
897 vty_out(vty
, " Instance: %d", client
->instance
);
900 vty_out(vty
, "------------------------ \n");
901 vty_out(vty
, "FD: %d \n", client
->sock
);
902 vty_out(vty
, "Route Table ID: %d \n", client
->rtm_table
);
904 connect_time
= (time_t) atomic_load_explicit(&client
->connect_time
,
905 memory_order_relaxed
);
907 vty_out(vty
, "Connect Time: %s \n",
908 zserv_time_buf(&connect_time
, cbuf
, ZEBRA_TIME_BUF
));
909 if (client
->nh_reg_time
) {
910 vty_out(vty
, "Nexthop Registry Time: %s \n",
911 zserv_time_buf(&client
->nh_reg_time
, nhbuf
,
913 if (client
->nh_last_upd_time
)
914 vty_out(vty
, "Nexthop Last Update Time: %s \n",
915 zserv_time_buf(&client
->nh_last_upd_time
, mbuf
,
918 vty_out(vty
, "No Nexthop Update sent\n");
920 vty_out(vty
, "Not registered for Nexthop Updates\n");
922 last_read_time
= (time_t)atomic_load_explicit(&client
->last_read_time
,
923 memory_order_relaxed
);
924 last_write_time
= (time_t)atomic_load_explicit(&client
->last_write_time
,
925 memory_order_relaxed
);
927 last_read_cmd
= atomic_load_explicit(&client
->last_read_cmd
,
928 memory_order_relaxed
);
929 last_write_cmd
= atomic_load_explicit(&client
->last_write_cmd
,
930 memory_order_relaxed
);
932 vty_out(vty
, "Last Msg Rx Time: %s \n",
933 zserv_time_buf(&last_read_time
, rbuf
, ZEBRA_TIME_BUF
));
934 vty_out(vty
, "Last Msg Tx Time: %s \n",
935 zserv_time_buf(&last_write_time
, wbuf
, ZEBRA_TIME_BUF
));
937 vty_out(vty
, "Last Rcvd Cmd: %s \n",
938 zserv_command_string(last_read_cmd
));
940 vty_out(vty
, "Last Sent Cmd: %s \n",
941 zserv_command_string(last_write_cmd
));
944 vty_out(vty
, "Type Add Update Del \n");
945 vty_out(vty
, "================================================== \n");
946 vty_out(vty
, "IPv4 %-12d%-12d%-12d\n", client
->v4_route_add_cnt
,
947 client
->v4_route_upd8_cnt
, client
->v4_route_del_cnt
);
948 vty_out(vty
, "IPv6 %-12d%-12d%-12d\n", client
->v6_route_add_cnt
,
949 client
->v6_route_upd8_cnt
, client
->v6_route_del_cnt
);
950 vty_out(vty
, "Redist:v4 %-12d%-12d%-12d\n", client
->redist_v4_add_cnt
,
951 0, client
->redist_v4_del_cnt
);
952 vty_out(vty
, "Redist:v6 %-12d%-12d%-12d\n", client
->redist_v6_add_cnt
,
953 0, client
->redist_v6_del_cnt
);
954 vty_out(vty
, "Connected %-12d%-12d%-12d\n", client
->ifadd_cnt
, 0,
956 vty_out(vty
, "BFD peer %-12d%-12d%-12d\n", client
->bfd_peer_add_cnt
,
957 client
->bfd_peer_upd8_cnt
, client
->bfd_peer_del_cnt
);
958 vty_out(vty
, "NHT v4 %-12d%-12d%-12d\n",
959 client
->v4_nh_watch_add_cnt
, 0, client
->v4_nh_watch_rem_cnt
);
960 vty_out(vty
, "NHT v6 %-12d%-12d%-12d\n",
961 client
->v6_nh_watch_add_cnt
, 0, client
->v6_nh_watch_rem_cnt
);
962 vty_out(vty
, "Interface Up Notifications: %d\n", client
->ifup_cnt
);
963 vty_out(vty
, "Interface Down Notifications: %d\n", client
->ifdown_cnt
);
964 vty_out(vty
, "VNI add notifications: %d\n", client
->vniadd_cnt
);
965 vty_out(vty
, "VNI delete notifications: %d\n", client
->vnidel_cnt
);
966 vty_out(vty
, "L3-VNI add notifications: %d\n", client
->l3vniadd_cnt
);
967 vty_out(vty
, "L3-VNI delete notifications: %d\n", client
->l3vnidel_cnt
);
968 vty_out(vty
, "MAC-IP add notifications: %d\n", client
->macipadd_cnt
);
969 vty_out(vty
, "MAC-IP delete notifications: %d\n", client
->macipdel_cnt
);
971 #if defined DEV_BUILD
972 vty_out(vty
, "Input Fifo: %zu:%zu Output Fifo: %zu:%zu\n",
973 client
->ibuf_fifo
->count
, client
->ibuf_fifo
->max_count
,
974 client
->obuf_fifo
->count
, client
->obuf_fifo
->max_count
);
980 static void zebra_show_client_brief(struct vty
*vty
, struct zserv
*client
)
982 char cbuf
[ZEBRA_TIME_BUF
], rbuf
[ZEBRA_TIME_BUF
];
983 char wbuf
[ZEBRA_TIME_BUF
];
984 time_t connect_time
, last_read_time
, last_write_time
;
986 connect_time
= (time_t)atomic_load_explicit(&client
->connect_time
,
987 memory_order_relaxed
);
988 last_read_time
= (time_t)atomic_load_explicit(&client
->last_read_time
,
989 memory_order_relaxed
);
990 last_write_time
= (time_t)atomic_load_explicit(&client
->last_write_time
,
991 memory_order_relaxed
);
993 vty_out(vty
, "%-8s%12s %12s%12s%8d/%-8d%8d/%-8d\n",
994 zebra_route_string(client
->proto
),
995 zserv_time_buf(&connect_time
, cbuf
, ZEBRA_TIME_BUF
),
996 zserv_time_buf(&last_read_time
, rbuf
, ZEBRA_TIME_BUF
),
997 zserv_time_buf(&last_write_time
, wbuf
, ZEBRA_TIME_BUF
),
998 client
->v4_route_add_cnt
+ client
->v4_route_upd8_cnt
,
999 client
->v4_route_del_cnt
,
1000 client
->v6_route_add_cnt
+ client
->v6_route_upd8_cnt
,
1001 client
->v6_route_del_cnt
);
1004 struct zserv
*zserv_find_client(uint8_t proto
, unsigned short instance
)
1006 struct listnode
*node
, *nnode
;
1007 struct zserv
*client
;
1009 for (ALL_LIST_ELEMENTS(zrouter
.client_list
, node
, nnode
, client
)) {
1010 if (client
->proto
== proto
&& client
->instance
== instance
)
1017 /* This command is for debugging purpose. */
1018 DEFUN (show_zebra_client
,
1019 show_zebra_client_cmd
,
1020 "show zebra client",
1023 "Client information\n")
1025 struct listnode
*node
;
1026 struct zserv
*client
;
1028 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
))
1029 zebra_show_client_detail(vty
, client
);
1034 /* This command is for debugging purpose. */
1035 DEFUN (show_zebra_client_summary
,
1036 show_zebra_client_summary_cmd
,
1037 "show zebra client summary",
1040 "Client information brief\n"
1043 struct listnode
*node
;
1044 struct zserv
*client
;
1047 "Name Connect Time Last Read Last Write IPv4 Routes IPv6 Routes \n");
1049 "--------------------------------------------------------------------------------\n");
1051 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
))
1052 zebra_show_client_brief(vty
, client
);
1054 vty_out(vty
, "Routes column shows (added+updated)/deleted\n");
1058 #if defined(HANDLE_ZAPI_FUZZING)
1059 void zserv_read_file(char *input
)
1064 fd
= open(input
, O_RDONLY
| O_NONBLOCK
);
1067 zserv_client_create(fd
);
1071 void zserv_init(void)
1073 /* Client list init. */
1074 zrouter
.client_list
= list_new();
1079 install_element(ENABLE_NODE
, &show_zebra_client_cmd
);
1080 install_element(ENABLE_NODE
, &show_zebra_client_summary_cmd
);