1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Copyright (C) 1997-1999 Kunihiro Ishiguro
6 * Copyright (C) 2015-2018 Cumulus Networks, Inc.
12 /* clang-format off */
13 #include <errno.h> /* for errno */
14 #include <netinet/in.h> /* for sockaddr_in */
15 #include <stdint.h> /* for uint8_t */
16 #include <stdio.h> /* for snprintf */
17 #include <sys/socket.h> /* for sockaddr_storage, AF_UNIX, accept... */
18 #include <sys/stat.h> /* for umask, mode_t */
19 #include <sys/un.h> /* for sockaddr_un */
20 #include <time.h> /* for NULL, tm, gmtime, time_t */
21 #include <unistd.h> /* for close, unlink, ssize_t */
23 #include "lib/buffer.h" /* for BUFFER_EMPTY, BUFFER_ERROR, BUFFE... */
24 #include "lib/command.h" /* for vty, install_element, CMD_SUCCESS... */
25 #include "lib/hook.h" /* for DEFINE_HOOK, DEFINE_KOOH, hook_call */
26 #include "lib/linklist.h" /* for ALL_LIST_ELEMENTS_RO, ALL_LIST_EL... */
27 #include "lib/libfrr.h" /* for frr_zclient_addr */
28 #include "lib/log.h" /* for zlog_warn, zlog_debug, safe_strerror */
29 #include "lib/memory.h" /* for MTYPE_TMP, XCALLOC, XFREE */
30 #include "lib/monotime.h" /* for monotime, ONE_DAY_SECOND, ONE_WEE... */
31 #include "lib/network.h" /* for set_nonblocking */
32 #include "lib/privs.h" /* for zebra_privs_t, ZPRIVS_LOWER, ZPRI... */
33 #include "lib/route_types.h" /* for ZEBRA_ROUTE_MAX */
34 #include "lib/sockopt.h" /* for setsockopt_so_recvbuf, setsockopt... */
35 #include "lib/sockunion.h" /* for sockopt_reuseaddr, sockopt_reuseport */
36 #include "lib/stream.h" /* for STREAM_SIZE, stream (ptr only), ... */
37 #include "frrevent.h" /* for thread (ptr only), EVENT_ARG, ... */
38 #include "lib/vrf.h" /* for vrf_info_lookup, VRF_DEFAULT */
39 #include "lib/vty.h" /* for vty_out, vty (ptr only) */
40 #include "lib/zclient.h" /* for zmsghdr, ZEBRA_HEADER_SIZE, ZEBRA... */
41 #include "lib/frr_pthread.h" /* for frr_pthread_new, frr_pthread_stop... */
42 #include "lib/frratomic.h" /* for atomic_load_explicit, atomic_stor... */
43 #include "lib/lib_errors.h" /* for generic ferr ids */
44 #include "lib/printfrr.h" /* for string functions */
46 #include "zebra/debug.h" /* for various debugging macros */
47 #include "zebra/rib.h" /* for rib_score_proto */
48 #include "zebra/zapi_msg.h" /* for zserv_handle_commands */
49 #include "zebra/zebra_vrf.h" /* for zebra_vrf_lookup_by_id, zvrf */
50 #include "zebra/zserv.h" /* for zserv */
51 #include "zebra/zebra_router.h"
52 #include "zebra/zebra_errors.h" /* for error messages */
56 extern struct zebra_privs_t zserv_privs
;
58 /* The listener socket for clients connecting to us */
61 /* The lock that protects access to zapi client objects */
62 static pthread_mutex_t client_mutex
;
64 static struct zserv
*find_client_internal(uint8_t proto
,
65 unsigned short instance
,
68 /* Mem type for zclients. */
69 DEFINE_MTYPE_STATIC(ZEBRA
, ZSERV_CLIENT
, "ZClients");
72 * Client thread events.
74 * These are used almost exclusively by client threads to drive their own event
75 * loops. The only exception is in zserv_client_create(), which pushes an
76 * initial ZSERV_CLIENT_READ event to start the API handler loop.
78 enum zserv_client_event
{
79 /* Schedule a socket read */
81 /* Schedule a buffer write */
88 * These are used by client threads to notify the main thread about various
89 * events and to make processing requests.
92 /* Schedule listen job on Zebra API socket */
94 /* The calling client has packets on its input buffer */
95 ZSERV_PROCESS_MESSAGES
,
96 /* The calling client wishes to be killed */
97 ZSERV_HANDLE_CLIENT_FAIL
,
101 * Zebra server event driver for all client threads.
103 * This is essentially a wrapper around event_add_event() that centralizes
104 * those scheduling calls into one place.
106 * All calls to this function schedule an event on the pthread running the
110 * the client in question, and thread target
113 * the event to notify them about
115 static void zserv_client_event(struct zserv
*client
,
116 enum zserv_client_event event
);
119 * Zebra server event driver for the main thread.
121 * This is essentially a wrapper around event_add_event() that centralizes
122 * those scheduling calls into one place.
124 * All calls to this function schedule an event on Zebra's main pthread.
127 * the client in question
130 * the event to notify the main thread about
132 static void zserv_event(struct zserv
*client
, enum zserv_event event
);
135 /* Client thread lifecycle -------------------------------------------------- */
138 * Free a zserv client object.
140 void zserv_client_delete(struct zserv
*client
)
142 XFREE(MTYPE_ZSERV_CLIENT
, client
);
146 * Log zapi message to zlog.
157 void zserv_log_message(const char *errmsg
, struct stream
*msg
,
160 zlog_debug("Rx'd ZAPI message");
162 zlog_debug("%s", errmsg
);
164 zlog_debug(" Length: %d", hdr
->length
);
165 zlog_debug("Command: %s", zserv_command_string(hdr
->command
));
166 zlog_debug(" VRF: %u", hdr
->vrf_id
);
172 * Gracefuly shut down a client connection.
174 * Cancel any pending tasks for the client's thread. Then schedule a task on
175 * the main thread to shut down the calling thread.
177 * It is not safe to close the client socket in this function. The socket is
178 * owned by the main thread.
180 * Must be called from the client pthread, never the main thread.
182 static void zserv_client_fail(struct zserv
*client
)
184 flog_warn(EC_ZEBRA_CLIENT_IO_ERROR
,
185 "Client '%s' encountered an error and is shutting down.",
186 zebra_route_string(client
->proto
));
188 atomic_store_explicit(&client
->pthread
->running
, false,
189 memory_order_relaxed
);
191 EVENT_OFF(client
->t_read
);
192 EVENT_OFF(client
->t_write
);
193 zserv_event(client
, ZSERV_HANDLE_CLIENT_FAIL
);
197 * Write all pending messages to client socket.
199 * This function first attempts to flush any buffered data. If unsuccessful,
200 * the function reschedules itself and returns. If successful, it pops all
201 * available messages from the output queue and continues to write data
202 * directly to the socket until the socket would block. If the socket never
203 * blocks and all data is written, the function returns without rescheduling
204 * itself. If the socket ends up throwing EWOULDBLOCK, the remaining data is
205 * buffered and the function reschedules itself.
207 * The utility of the buffer is that it allows us to vastly reduce lock
208 * contention by allowing us to pop *all* messages off the output queue at once
209 * instead of locking and unlocking each time we want to pop a single message
210 * off the queue. The same thing could arguably be accomplished faster by
211 * allowing the main thread to write directly into the buffer instead of
212 * enqueuing packets onto an intermediary queue, but the intermediary queue
213 * allows us to expose information about input and output queues to the user in
214 * terms of number of packets rather than size of data.
216 static void zserv_write(struct event
*thread
)
218 struct zserv
*client
= EVENT_ARG(thread
);
221 struct stream_fifo
*cache
;
222 uint64_t time_now
= monotime(NULL
);
224 /* If we have any data pending, try to flush it first */
225 switch (buffer_flush_all(client
->wb
, client
->sock
)) {
229 frr_with_mutex (&client
->stats_mtx
) {
230 client
->last_write_time
= time_now
;
232 zserv_client_event(client
, ZSERV_CLIENT_WRITE
);
238 cache
= stream_fifo_new();
240 frr_with_mutex (&client
->obuf_mtx
) {
241 while (stream_fifo_head(client
->obuf_fifo
))
242 stream_fifo_push(cache
,
243 stream_fifo_pop(client
->obuf_fifo
));
248 stream_set_getp(msg
, 0);
249 wcmd
= stream_getw_from(msg
, ZAPI_HEADER_CMD_LOCATION
);
252 while (stream_fifo_head(cache
)) {
253 msg
= stream_fifo_pop(cache
);
254 buffer_put(client
->wb
, STREAM_DATA(msg
), stream_get_endp(msg
));
258 stream_fifo_free(cache
);
260 /* If we have any data pending, try to flush it first */
261 switch (buffer_flush_all(client
->wb
, client
->sock
)) {
265 frr_with_mutex (&client
->stats_mtx
) {
266 client
->last_write_time
= time_now
;
268 zserv_client_event(client
, ZSERV_CLIENT_WRITE
);
274 frr_with_mutex (&client
->stats_mtx
) {
275 client
->last_write_cmd
= wcmd
;
276 client
->last_write_time
= time_now
;
281 flog_warn(EC_ZEBRA_CLIENT_WRITE_FAILED
,
282 "%s: could not write to %s [fd = %d], closing.", __func__
,
283 zebra_route_string(client
->proto
), client
->sock
);
284 zserv_client_fail(client
);
288 * Read and process data from a client socket.
290 * The responsibilities here are to read raw data from the client socket,
291 * validate the header, encapsulate it into a single stream object, push it
292 * onto the input queue and then notify the main thread that there is new data
295 * This function first looks for any data in the client structure's working
296 * input buffer. If data is present, it is assumed that reading stopped in a
297 * previous invocation of this task and needs to be resumed to finish a message.
298 * Otherwise, the socket data stream is assumed to be at the beginning of a new
299 * ZAPI message (specifically at the header). The header is read and validated.
300 * If the header passed validation then the length field found in the header is
301 * used to compute the total length of the message. That much data is read (but
302 * not inspected), appended to the header, placed into a stream and pushed onto
303 * the client's input queue. A task is then scheduled on the main thread to
304 * process the client's input queue. Finally, if all of this was successful,
305 * this task reschedules itself.
307 * Any failure in any of these actions is handled by terminating the client.
309 static void zserv_read(struct event
*thread
)
311 struct zserv
*client
= EVENT_ARG(thread
);
314 struct stream_fifo
*cache
;
320 p2p_orig
= atomic_load_explicit(&zrouter
.packets_to_process
,
321 memory_order_relaxed
);
322 cache
= stream_fifo_new();
324 sock
= EVENT_FD(thread
);
331 already
= stream_get_endp(client
->ibuf_work
);
333 /* Read length and command (if we don't have it already). */
334 if (already
< ZEBRA_HEADER_SIZE
) {
335 nb
= stream_read_try(client
->ibuf_work
, sock
,
336 ZEBRA_HEADER_SIZE
- already
);
337 if ((nb
== 0 || nb
== -1)) {
338 if (IS_ZEBRA_DEBUG_EVENT
)
339 zlog_debug("connection closed socket [%d]",
343 if (nb
!= (ssize_t
)(ZEBRA_HEADER_SIZE
- already
)) {
344 /* Try again later. */
347 already
= ZEBRA_HEADER_SIZE
;
350 /* Reset to read from the beginning of the incoming packet. */
351 stream_set_getp(client
->ibuf_work
, 0);
353 /* Fetch header values */
354 hdrvalid
= zapi_parse_header(client
->ibuf_work
, &hdr
);
357 snprintf(errmsg
, sizeof(errmsg
),
358 "%s: Message has corrupt header", __func__
);
359 zserv_log_message(errmsg
, client
->ibuf_work
, NULL
);
363 /* Validate header */
364 if (hdr
.marker
!= ZEBRA_HEADER_MARKER
365 || hdr
.version
!= ZSERV_VERSION
) {
367 errmsg
, sizeof(errmsg
),
368 "Message has corrupt header\n%s: socket %d version mismatch, marker %d, version %d",
369 __func__
, sock
, hdr
.marker
, hdr
.version
);
370 zserv_log_message(errmsg
, client
->ibuf_work
, &hdr
);
373 if (hdr
.length
< ZEBRA_HEADER_SIZE
) {
375 errmsg
, sizeof(errmsg
),
376 "Message has corrupt header\n%s: socket %d message length %u is less than header size %d",
377 __func__
, sock
, hdr
.length
, ZEBRA_HEADER_SIZE
);
378 zserv_log_message(errmsg
, client
->ibuf_work
, &hdr
);
381 if (hdr
.length
> STREAM_SIZE(client
->ibuf_work
)) {
383 errmsg
, sizeof(errmsg
),
384 "Message has corrupt header\n%s: socket %d message length %u exceeds buffer size %lu",
385 __func__
, sock
, hdr
.length
,
386 (unsigned long)STREAM_SIZE(client
->ibuf_work
));
387 zserv_log_message(errmsg
, client
->ibuf_work
, &hdr
);
391 /* Read rest of data. */
392 if (already
< hdr
.length
) {
393 nb
= stream_read_try(client
->ibuf_work
, sock
,
394 hdr
.length
- already
);
395 if ((nb
== 0 || nb
== -1)) {
396 if (IS_ZEBRA_DEBUG_EVENT
)
398 "connection closed [%d] when reading zebra data",
402 if (nb
!= (ssize_t
)(hdr
.length
- already
)) {
403 /* Try again later. */
408 /* Debug packet information. */
409 if (IS_ZEBRA_DEBUG_PACKET
)
410 zlog_debug("zebra message[%s:%u:%u] comes from socket [%d]",
411 zserv_command_string(hdr
.command
),
412 hdr
.vrf_id
, hdr
.length
,
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 uint64_t time_now
= monotime(NULL
);
426 /* update session statistics */
427 frr_with_mutex (&client
->stats_mtx
) {
428 client
->last_read_time
= time_now
;
429 client
->last_read_cmd
= hdr
.command
;
432 /* publish read packets on client's input queue */
433 frr_with_mutex (&client
->ibuf_mtx
) {
435 stream_fifo_push(client
->ibuf_fifo
,
436 stream_fifo_pop(cache
));
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 from client: %s", p2p_orig
- p2p
,
446 zebra_route_string(client
->proto
));
448 /* Reschedule ourselves */
449 zserv_client_event(client
, ZSERV_CLIENT_READ
);
451 stream_fifo_free(cache
);
456 stream_fifo_free(cache
);
457 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 event_add_read(client
->pthread
->master
, zserv_read
, client
,
466 client
->sock
, &client
->t_read
);
468 case ZSERV_CLIENT_WRITE
:
469 event_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 void zserv_process_messages(struct event
*thread
)
496 struct zserv
*client
= EVENT_ARG(thread
);
498 struct stream_fifo
*cache
= stream_fifo_new();
499 uint32_t p2p
= zrouter
.packets_to_process
;
500 bool need_resched
= false;
502 frr_with_mutex (&client
->ibuf_mtx
) {
504 for (i
= 0; i
< p2p
&& stream_fifo_head(client
->ibuf_fifo
);
506 msg
= stream_fifo_pop(client
->ibuf_fifo
);
507 stream_fifo_push(cache
, msg
);
512 /* Need to reschedule processing work if there are still
513 * packets in the fifo.
515 if (stream_fifo_head(client
->ibuf_fifo
))
519 /* Process the batch of messages */
520 if (stream_fifo_head(cache
))
521 zserv_handle_commands(client
, cache
);
523 stream_fifo_free(cache
);
525 /* Reschedule ourselves if necessary */
527 zserv_event(client
, ZSERV_PROCESS_MESSAGES
);
530 int zserv_send_message(struct zserv
*client
, struct stream
*msg
)
532 frr_with_mutex (&client
->obuf_mtx
) {
533 stream_fifo_push(client
->obuf_fifo
, msg
);
536 zserv_client_event(client
, ZSERV_CLIENT_WRITE
);
542 * Send a batch of messages to a connected Zebra API client.
544 int zserv_send_batch(struct zserv
*client
, struct stream_fifo
*fifo
)
548 frr_with_mutex (&client
->obuf_mtx
) {
549 msg
= stream_fifo_pop(fifo
);
551 stream_fifo_push(client
->obuf_fifo
, msg
);
552 msg
= stream_fifo_pop(fifo
);
556 zserv_client_event(client
, ZSERV_CLIENT_WRITE
);
561 /* Hooks for client connect / disconnect */
562 DEFINE_HOOK(zserv_client_connect
, (struct zserv
*client
), (client
));
563 DEFINE_KOOH(zserv_client_close
, (struct zserv
*client
), (client
));
566 * Deinitialize zebra client.
568 * - Deregister and deinitialize related internal resources
569 * - Gracefuly close socket
570 * - Free associated resources
571 * - Free client structure
573 * This does *not* take any action on the struct event * fields. These are
574 * managed by the owning pthread and any tasks associated with them must have
575 * been stopped prior to invoking this function.
577 static void zserv_client_free(struct zserv
*client
)
582 hook_call(zserv_client_close
, client
);
584 /* Close file descriptor. */
586 unsigned long nroutes
;
591 if (DYNAMIC_CLIENT_GR_DISABLED(client
)) {
592 zebra_mpls_client_cleanup_vrf_label(client
->proto
);
594 nroutes
= rib_score_proto(client
->proto
,
597 "client %d disconnected %lu %s routes removed from the rib",
598 client
->sock
, nroutes
,
599 zebra_route_string(client
->proto
));
601 /* Not worrying about instance for now */
602 nnhgs
= zebra_nhg_score_proto(client
->proto
);
604 "client %d disconnected %lu %s nhgs removed from the rib",
606 zebra_route_string(client
->proto
));
611 /* Free stream buffers. */
612 if (client
->ibuf_work
)
613 stream_free(client
->ibuf_work
);
614 if (client
->obuf_work
)
615 stream_free(client
->obuf_work
);
616 if (client
->ibuf_fifo
)
617 stream_fifo_free(client
->ibuf_fifo
);
618 if (client
->obuf_fifo
)
619 stream_fifo_free(client
->obuf_fifo
);
621 buffer_free(client
->wb
);
623 /* Free buffer mutexes */
624 pthread_mutex_destroy(&client
->stats_mtx
);
625 pthread_mutex_destroy(&client
->obuf_mtx
);
626 pthread_mutex_destroy(&client
->ibuf_mtx
);
629 for (afi_t afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
630 for (int i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
631 vrf_bitmap_free(client
->redist
[afi
][i
]);
632 redist_del_all_instances(&client
->mi_redist
[afi
][i
]);
635 vrf_bitmap_free(client
->redist_default
[afi
]);
636 vrf_bitmap_free(client
->ridinfo
[afi
]);
637 vrf_bitmap_free(client
->nhrp_neighinfo
[afi
]);
641 * If any instance are graceful restart enabled,
642 * client is not deleted
644 if (DYNAMIC_CLIENT_GR_DISABLED(client
)) {
645 if (IS_ZEBRA_DEBUG_EVENT
)
646 zlog_debug("%s: Deleting client %s", __func__
,
647 zebra_route_string(client
->proto
));
648 zserv_client_delete(client
);
650 /* Handle cases where client has GR instance. */
651 if (IS_ZEBRA_DEBUG_EVENT
)
652 zlog_debug("%s: client %s restart enabled", __func__
,
653 zebra_route_string(client
->proto
));
654 if (zebra_gr_client_disconnect(client
) < 0)
656 "%s: GR enabled but could not handle disconnect event",
661 void zserv_close_client(struct zserv
*client
)
665 if (client
->pthread
) {
666 /* synchronously stop and join pthread */
667 frr_pthread_stop(client
->pthread
, NULL
);
669 if (IS_ZEBRA_DEBUG_EVENT
)
670 zlog_debug("Closing client '%s'",
671 zebra_route_string(client
->proto
));
673 event_cancel_event(zrouter
.master
, client
);
674 EVENT_OFF(client
->t_cleanup
);
675 EVENT_OFF(client
->t_process
);
677 /* destroy pthread */
678 frr_pthread_destroy(client
->pthread
);
679 client
->pthread
= NULL
;
683 * Final check in case the client struct is in use in another
684 * pthread: if not in-use, continue and free the client
686 frr_with_mutex (&client_mutex
) {
687 if (client
->busy_count
<= 0) {
688 /* remove from client list */
689 listnode_delete(zrouter
.client_list
, client
);
692 * The client session object may be in use, although
693 * the associated pthread is gone. Defer final
696 client
->is_closed
= true;
703 zserv_client_free(client
);
707 * This task is scheduled by a ZAPI client pthread on the main pthread when it
708 * wants to stop itself. When this executes, the client connection should
709 * already have been closed and the thread will most likely have died, but its
710 * resources still need to be cleaned up.
712 static void zserv_handle_client_fail(struct event
*thread
)
714 struct zserv
*client
= EVENT_ARG(thread
);
716 zserv_close_client(client
);
720 * Create a new client.
722 * This is called when a new connection is accept()'d on the ZAPI socket. It
723 * initializes new client structure, notifies any subscribers of the connection
724 * event and spawns the client's thread.
727 * client's socket file descriptor
729 static struct zserv
*zserv_client_create(int sock
)
731 struct zserv
*client
;
733 MAX(ZEBRA_MAX_PACKET_SIZ
, sizeof(struct zapi_route
));
737 client
= XCALLOC(MTYPE_ZSERV_CLIENT
, sizeof(struct zserv
));
739 /* Make client input/output buffer. */
741 client
->ibuf_fifo
= stream_fifo_new();
742 client
->obuf_fifo
= stream_fifo_new();
743 client
->ibuf_work
= stream_new(stream_size
);
744 client
->obuf_work
= stream_new(stream_size
);
745 client
->connect_time
= monotime(NULL
);
746 pthread_mutex_init(&client
->ibuf_mtx
, NULL
);
747 pthread_mutex_init(&client
->obuf_mtx
, NULL
);
748 pthread_mutex_init(&client
->stats_mtx
, NULL
);
749 client
->wb
= buffer_new(0);
750 TAILQ_INIT(&(client
->gr_info_queue
));
752 /* Initialize flags */
753 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
754 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
755 client
->redist
[afi
][i
] = vrf_bitmap_init();
756 client
->redist_default
[afi
] = vrf_bitmap_init();
757 client
->ridinfo
[afi
] = vrf_bitmap_init();
758 client
->nhrp_neighinfo
[afi
] = vrf_bitmap_init();
761 /* Add this client to linked list. */
762 frr_with_mutex (&client_mutex
) {
763 listnode_add(zrouter
.client_list
, client
);
766 struct frr_pthread_attr zclient_pthr_attrs
= {
767 .start
= frr_pthread_attr_default
.start
,
768 .stop
= frr_pthread_attr_default
.stop
771 frr_pthread_new(&zclient_pthr_attrs
, "Zebra API client thread",
774 /* start read loop */
775 zserv_client_event(client
, ZSERV_CLIENT_READ
);
778 hook_call(zserv_client_connect
, client
);
781 frr_pthread_run(client
->pthread
, NULL
);
787 * Retrieve a client object by the complete tuple of
788 * {protocol, instance, session}. This version supports use
789 * from a different pthread: the object will be returned marked
790 * in-use. The caller *must* release the client object with the
791 * release_client() api, to ensure that the in-use marker is cleared properly.
793 struct zserv
*zserv_acquire_client(uint8_t proto
, unsigned short instance
,
796 struct zserv
*client
= NULL
;
798 frr_with_mutex (&client_mutex
) {
799 client
= find_client_internal(proto
, instance
, session_id
);
801 /* Don't return a dead/closed client object */
802 if (client
->is_closed
)
805 client
->busy_count
++;
813 * Release a client object that was acquired with the acquire_client() api.
814 * After this has been called, the caller must not use the client pointer -
815 * it may be freed if the client has closed.
817 void zserv_release_client(struct zserv
*client
)
820 * Once we've decremented the client object's refcount, it's possible
821 * for it to be deleted as soon as we release the lock, so we won't
822 * touch the object again.
824 frr_with_mutex (&client_mutex
) {
825 client
->busy_count
--;
827 if (client
->busy_count
<= 0) {
829 * No more users of the client object. If the client
830 * session is closed, schedule cleanup on the zebra
833 if (client
->is_closed
)
834 event_add_event(zrouter
.master
,
835 zserv_handle_client_fail
,
836 client
, 0, &client
->t_cleanup
);
841 * Cleanup must take place on the zebra main pthread, so we've
842 * scheduled an event.
847 * Accept socket connection.
849 static void zserv_accept(struct event
*thread
)
853 struct sockaddr_in client
;
856 accept_sock
= EVENT_FD(thread
);
858 /* Reregister myself. */
859 zserv_event(NULL
, ZSERV_ACCEPT
);
861 len
= sizeof(struct sockaddr_in
);
862 client_sock
= accept(accept_sock
, (struct sockaddr
*)&client
, &len
);
864 if (client_sock
< 0) {
865 flog_err_sys(EC_LIB_SOCKET
, "Can't accept zebra socket: %s",
866 safe_strerror(errno
));
870 /* Make client socket non-blocking. */
871 set_nonblocking(client_sock
);
873 /* Create new zebra client. */
874 zserv_client_create(client_sock
);
877 void zserv_close(void)
880 * On shutdown, let's close the socket down
881 * so that long running processes of killing the
882 * routing table doesn't leave us in a bad
883 * state where a client tries to reconnect
888 /* Free client list's mutex */
889 pthread_mutex_destroy(&client_mutex
);
892 void zserv_start(char *path
)
896 struct sockaddr_storage sa
;
899 if (!frr_zclient_addr(&sa
, &sa_len
, path
))
900 /* should be caught in zebra main() */
904 old_mask
= umask(0077);
906 /* Make UNIX domain socket. */
907 zsock
= socket(sa
.ss_family
, SOCK_STREAM
, 0);
909 flog_err_sys(EC_LIB_SOCKET
, "Can't create zserv socket: %s",
910 safe_strerror(errno
));
914 if (sa
.ss_family
!= AF_UNIX
) {
915 sockopt_reuseaddr(zsock
);
916 sockopt_reuseport(zsock
);
918 struct sockaddr_un
*suna
= (struct sockaddr_un
*)&sa
;
919 if (suna
->sun_path
[0])
920 unlink(suna
->sun_path
);
923 setsockopt_so_recvbuf(zsock
, 1048576);
924 setsockopt_so_sendbuf(zsock
, 1048576);
926 frr_with_privs((sa
.ss_family
!= AF_UNIX
) ? &zserv_privs
: NULL
) {
927 ret
= bind(zsock
, (struct sockaddr
*)&sa
, sa_len
);
930 flog_err_sys(EC_LIB_SOCKET
, "Can't bind zserv socket on %s: %s",
931 path
, safe_strerror(errno
));
937 ret
= listen(zsock
, 5);
939 flog_err_sys(EC_LIB_SOCKET
,
940 "Can't listen to zserv socket %s: %s", path
,
941 safe_strerror(errno
));
949 zserv_event(NULL
, ZSERV_ACCEPT
);
952 void zserv_event(struct zserv
*client
, enum zserv_event event
)
956 event_add_read(zrouter
.master
, zserv_accept
, NULL
, zsock
, NULL
);
958 case ZSERV_PROCESS_MESSAGES
:
959 event_add_event(zrouter
.master
, zserv_process_messages
, client
,
960 0, &client
->t_process
);
962 case ZSERV_HANDLE_CLIENT_FAIL
:
963 event_add_event(zrouter
.master
, zserv_handle_client_fail
,
964 client
, 0, &client
->t_cleanup
);
969 /* General purpose ---------------------------------------------------------- */
971 #define ZEBRA_TIME_BUF 32
972 static char *zserv_time_buf(time_t *time1
, char *buf
, int buflen
)
977 assert(buflen
>= ZEBRA_TIME_BUF
);
978 assert(time1
!= NULL
);
981 snprintf(buf
, buflen
, "never ");
985 now
= monotime(NULL
);
988 frrtime_to_interval(now
, buf
, buflen
);
993 /* Display client info details */
994 static void zebra_show_client_detail(struct vty
*vty
, struct zserv
*client
)
996 char cbuf
[ZEBRA_TIME_BUF
], rbuf
[ZEBRA_TIME_BUF
];
997 char wbuf
[ZEBRA_TIME_BUF
], nhbuf
[ZEBRA_TIME_BUF
], mbuf
[ZEBRA_TIME_BUF
];
998 time_t connect_time
, last_read_time
, last_write_time
;
999 uint32_t last_read_cmd
, last_write_cmd
;
1001 vty_out(vty
, "Client: %s", zebra_route_string(client
->proto
));
1002 if (client
->instance
)
1003 vty_out(vty
, " Instance: %u", client
->instance
);
1004 if (client
->session_id
)
1005 vty_out(vty
, " [%u]", client
->session_id
);
1008 vty_out(vty
, "------------------------ \n");
1009 vty_out(vty
, "FD: %d \n", client
->sock
);
1011 frr_with_mutex (&client
->stats_mtx
) {
1012 connect_time
= client
->connect_time
;
1013 last_read_time
= client
->last_read_time
;
1014 last_write_time
= client
->last_write_time
;
1016 last_read_cmd
= client
->last_read_cmd
;
1017 last_write_cmd
= client
->last_write_cmd
;
1020 vty_out(vty
, "Connect Time: %s \n",
1021 zserv_time_buf(&connect_time
, cbuf
, ZEBRA_TIME_BUF
));
1022 if (client
->nh_reg_time
) {
1023 vty_out(vty
, "Nexthop Registry Time: %s \n",
1024 zserv_time_buf(&client
->nh_reg_time
, nhbuf
,
1026 if (client
->nh_last_upd_time
)
1027 vty_out(vty
, "Nexthop Last Update Time: %s \n",
1028 zserv_time_buf(&client
->nh_last_upd_time
, mbuf
,
1031 vty_out(vty
, "No Nexthop Update sent\n");
1033 vty_out(vty
, "Not registered for Nexthop Updates\n");
1036 "Client will %sbe notified about the status of its routes.\n",
1037 client
->notify_owner
? "" : "Not ");
1039 vty_out(vty
, "Last Msg Rx Time: %s \n",
1040 zserv_time_buf(&last_read_time
, rbuf
, ZEBRA_TIME_BUF
));
1041 vty_out(vty
, "Last Msg Tx Time: %s \n",
1042 zserv_time_buf(&last_write_time
, wbuf
, ZEBRA_TIME_BUF
));
1044 vty_out(vty
, "Last Rcvd Cmd: %s \n",
1045 zserv_command_string(last_read_cmd
));
1047 vty_out(vty
, "Last Sent Cmd: %s \n",
1048 zserv_command_string(last_write_cmd
));
1051 vty_out(vty
, "Type Add Update Del \n");
1052 vty_out(vty
, "================================================== \n");
1053 vty_out(vty
, "IPv4 %-12u%-12u%-12u\n", client
->v4_route_add_cnt
,
1054 client
->v4_route_upd8_cnt
, client
->v4_route_del_cnt
);
1055 vty_out(vty
, "IPv6 %-12u%-12u%-12u\n", client
->v6_route_add_cnt
,
1056 client
->v6_route_upd8_cnt
, client
->v6_route_del_cnt
);
1057 vty_out(vty
, "Redist:v4 %-12u%-12u%-12u\n", client
->redist_v4_add_cnt
,
1058 0, client
->redist_v4_del_cnt
);
1059 vty_out(vty
, "Redist:v6 %-12u%-12u%-12u\n", client
->redist_v6_add_cnt
,
1060 0, client
->redist_v6_del_cnt
);
1061 vty_out(vty
, "VRF %-12u%-12u%-12u\n", client
->vrfadd_cnt
, 0,
1062 client
->vrfdel_cnt
);
1063 vty_out(vty
, "Connected %-12u%-12u%-12u\n", client
->ifadd_cnt
, 0,
1065 vty_out(vty
, "Interface %-12u%-12u%-12u\n", client
->ifup_cnt
, 0,
1066 client
->ifdown_cnt
);
1067 vty_out(vty
, "Intf Addr %-12u%-12u%-12u\n",
1068 client
->connected_rt_add_cnt
, 0, client
->connected_rt_del_cnt
);
1069 vty_out(vty
, "BFD peer %-12u%-12u%-12u\n", client
->bfd_peer_add_cnt
,
1070 client
->bfd_peer_upd8_cnt
, client
->bfd_peer_del_cnt
);
1071 vty_out(vty
, "NHT v4 %-12u%-12u%-12u\n",
1072 client
->v4_nh_watch_add_cnt
, 0, client
->v4_nh_watch_rem_cnt
);
1073 vty_out(vty
, "NHT v6 %-12u%-12u%-12u\n",
1074 client
->v6_nh_watch_add_cnt
, 0, client
->v6_nh_watch_rem_cnt
);
1075 vty_out(vty
, "VxLAN SG %-12u%-12u%-12u\n", client
->vxlan_sg_add_cnt
,
1076 0, client
->vxlan_sg_del_cnt
);
1077 vty_out(vty
, "VNI %-12u%-12u%-12u\n", client
->vniadd_cnt
, 0,
1078 client
->vnidel_cnt
);
1079 vty_out(vty
, "L3-VNI %-12u%-12u%-12u\n", client
->l3vniadd_cnt
, 0,
1080 client
->l3vnidel_cnt
);
1081 vty_out(vty
, "MAC-IP %-12u%-12u%-12u\n", client
->macipadd_cnt
, 0,
1082 client
->macipdel_cnt
);
1083 vty_out(vty
, "ES %-12u%-12u%-12u\n", client
->local_es_add_cnt
,
1084 0, client
->local_es_del_cnt
);
1085 vty_out(vty
, "ES-EVI %-12u%-12u%-12u\n",
1086 client
->local_es_evi_add_cnt
, 0, client
->local_es_evi_del_cnt
);
1087 vty_out(vty
, "Errors: %u\n", client
->error_cnt
);
1089 #if defined DEV_BUILD
1090 vty_out(vty
, "Input Fifo: %zu:%zu Output Fifo: %zu:%zu\n",
1091 client
->ibuf_fifo
->count
, client
->ibuf_fifo
->max_count
,
1092 client
->obuf_fifo
->count
, client
->obuf_fifo
->max_count
);
1097 /* Display stale client information */
1098 static void zebra_show_stale_client_detail(struct vty
*vty
,
1099 struct zserv
*client
)
1101 char buf
[PREFIX2STR_BUFFER
];
1103 struct client_gr_info
*info
= NULL
;
1104 struct zserv
*s
= NULL
;
1105 bool first_p
= true;
1107 TAILQ_FOREACH (info
, &client
->gr_info_queue
, gr_info
) {
1109 vty_out(vty
, "Stale Client Information\n");
1110 vty_out(vty
, "------------------------\n");
1112 if (client
->instance
)
1113 vty_out(vty
, " Instance: %u", client
->instance
);
1114 if (client
->session_id
)
1115 vty_out(vty
, " [%u]", client
->session_id
);
1120 vty_out(vty
, "VRF : %s\n", vrf_id_to_name(info
->vrf_id
));
1121 vty_out(vty
, "Capabilities : ");
1122 switch (info
->capabilities
) {
1123 case ZEBRA_CLIENT_GR_CAPABILITIES
:
1124 vty_out(vty
, "Graceful Restart(%u seconds)\n",
1125 info
->stale_removal_time
);
1127 case ZEBRA_CLIENT_ROUTE_UPDATE_COMPLETE
:
1128 case ZEBRA_CLIENT_ROUTE_UPDATE_PENDING
:
1129 case ZEBRA_CLIENT_GR_DISABLE
:
1130 case ZEBRA_CLIENT_RIB_STALE_TIME
:
1131 vty_out(vty
, "None\n");
1135 if (ZEBRA_CLIENT_GR_ENABLED(info
->capabilities
)) {
1136 if (info
->stale_client_ptr
) {
1137 s
= (struct zserv
*)(info
->stale_client_ptr
);
1138 uptime
= monotime(NULL
);
1139 uptime
-= s
->restart_time
;
1141 frrtime_to_interval(uptime
, buf
, sizeof(buf
));
1143 vty_out(vty
, "Last restart time : %s ago\n",
1146 vty_out(vty
, "Stalepath removal time: %d sec\n",
1147 info
->stale_removal_time
);
1148 if (info
->t_stale_removal
) {
1150 "Stale delete timer: %ld sec\n",
1151 event_timer_remain_second(
1152 info
->t_stale_removal
));
1161 static void zebra_show_client_brief(struct vty
*vty
, struct zserv
*client
)
1163 char client_string
[80];
1164 char cbuf
[ZEBRA_TIME_BUF
], rbuf
[ZEBRA_TIME_BUF
];
1165 char wbuf
[ZEBRA_TIME_BUF
];
1166 time_t connect_time
, last_read_time
, last_write_time
;
1168 frr_with_mutex (&client
->stats_mtx
) {
1169 connect_time
= client
->connect_time
;
1170 last_read_time
= client
->last_read_time
;
1171 last_write_time
= client
->last_write_time
;
1174 if (client
->instance
|| client
->session_id
)
1175 snprintfrr(client_string
, sizeof(client_string
), "%s[%u:%u]",
1176 zebra_route_string(client
->proto
), client
->instance
,
1177 client
->session_id
);
1179 snprintfrr(client_string
, sizeof(client_string
), "%s",
1180 zebra_route_string(client
->proto
));
1182 vty_out(vty
, "%-10s%12s %12s%12s %10d/%-10d %10d/%-10d\n",
1184 zserv_time_buf(&connect_time
, cbuf
, ZEBRA_TIME_BUF
),
1185 zserv_time_buf(&last_read_time
, rbuf
, ZEBRA_TIME_BUF
),
1186 zserv_time_buf(&last_write_time
, wbuf
, ZEBRA_TIME_BUF
),
1187 client
->v4_route_add_cnt
+ client
->v4_route_upd8_cnt
,
1188 client
->v4_route_del_cnt
,
1189 client
->v6_route_add_cnt
+ client
->v6_route_upd8_cnt
,
1190 client
->v6_route_del_cnt
);
1194 * Common logic that searches the client list for a zapi client; this
1195 * MUST be called holding the client list mutex.
1197 static struct zserv
*find_client_internal(uint8_t proto
,
1198 unsigned short instance
,
1199 uint32_t session_id
)
1201 struct listnode
*node
, *nnode
;
1202 struct zserv
*client
= NULL
;
1204 for (ALL_LIST_ELEMENTS(zrouter
.client_list
, node
, nnode
, client
)) {
1205 if (client
->proto
== proto
&& client
->instance
== instance
&&
1206 client
->session_id
== session_id
)
1214 * Public api that searches for a client session; this version is
1215 * used from the zebra main pthread.
1217 struct zserv
*zserv_find_client(uint8_t proto
, unsigned short instance
)
1219 struct zserv
*client
;
1221 frr_with_mutex (&client_mutex
) {
1222 client
= find_client_internal(proto
, instance
, 0);
1229 * Retrieve a client by its protocol, instance number, and session id.
1231 struct zserv
*zserv_find_client_session(uint8_t proto
, unsigned short instance
,
1232 uint32_t session_id
)
1234 struct zserv
*client
;
1236 frr_with_mutex (&client_mutex
) {
1237 client
= find_client_internal(proto
, instance
, session_id
);
1244 /* This command is for debugging purpose. */
1245 DEFUN (show_zebra_client
,
1246 show_zebra_client_cmd
,
1247 "show zebra client",
1250 "Client information\n")
1252 struct listnode
*node
;
1253 struct zserv
*client
;
1255 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
)) {
1256 zebra_show_client_detail(vty
, client
);
1257 /* Show GR info if present */
1258 zebra_show_stale_client_detail(vty
, client
);
1264 /* This command is for debugging purpose. */
1265 DEFUN (show_zebra_client_summary
,
1266 show_zebra_client_summary_cmd
,
1267 "show zebra client summary",
1270 "Client information brief\n"
1273 struct listnode
*node
;
1274 struct zserv
*client
;
1277 "Name Connect Time Last Read Last Write IPv4 Routes IPv6 Routes\n");
1279 "------------------------------------------------------------------------------------------\n");
1281 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
))
1282 zebra_show_client_brief(vty
, client
);
1284 vty_out(vty
, "Routes column shows (added+updated)/deleted\n");
1288 static int zserv_client_close_cb(struct zserv
*closed_client
)
1290 struct listnode
*node
, *nnode
;
1291 struct zserv
*client
= NULL
;
1293 for (ALL_LIST_ELEMENTS(zrouter
.client_list
, node
, nnode
, client
)) {
1294 if (client
->proto
== closed_client
->proto
)
1297 zsend_client_close_notify(client
, closed_client
);
1303 void zserv_init(void)
1305 /* Client list init. */
1306 zrouter
.client_list
= list_new();
1307 zrouter
.stale_client_list
= list_new();
1311 pthread_mutex_init(&client_mutex
, NULL
);
1313 install_element(ENABLE_NODE
, &show_zebra_client_cmd
);
1314 install_element(ENABLE_NODE
, &show_zebra_client_summary_cmd
);
1316 hook_register(zserv_client_close
, zserv_client_close_cb
);