back to topotato report
topotato coverage report
Current view: top level - zebra - zserv.c (source / functions) Hit Total Coverage
Test: test_pim_cbsr.py::PIMCandidateBSRTest Lines: 295 538 54.8 %
Date: 2023-02-16 02:09:14 Functions: 22 34 64.7 %

          Line data    Source code
       1             : /*
       2             :  * Zebra API server.
       3             :  * Portions:
       4             :  *   Copyright (C) 1997-1999  Kunihiro Ishiguro
       5             :  *   Copyright (C) 2015-2018  Cumulus Networks, Inc.
       6             :  *   et al.
       7             :  *
       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)
      11             :  * any later version.
      12             :  *
      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
      16             :  * more details.
      17             :  *
      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
      21             :  */
      22             : 
      23             : #include <zebra.h>
      24             : 
      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 */
      35             : 
      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/zclient.h"          /* for zmsghdr, ZEBRA_HEADER_SIZE, ZEBRA... */
      54             : #include "lib/frr_pthread.h"      /* for frr_pthread_new, frr_pthread_stop... */
      55             : #include "lib/frratomic.h"        /* for atomic_load_explicit, atomic_stor... */
      56             : #include "lib/lib_errors.h"       /* for generic ferr ids */
      57             : #include "lib/printfrr.h"         /* for string functions */
      58             : 
      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 */
      66             : /* clang-format on */
      67             : 
      68             : /* privileges */
      69             : extern struct zebra_privs_t zserv_privs;
      70             : 
      71             : /* The listener socket for clients connecting to us */
      72             : static int zsock;
      73             : 
      74             : /* The lock that protects access to zapi client objects */
      75             : static pthread_mutex_t client_mutex;
      76             : 
      77             : static struct zserv *find_client_internal(uint8_t proto,
      78             :                                           unsigned short instance,
      79             :                                           uint32_t session_id);
      80             : 
      81             : /* Mem type for zclients. */
      82           9 : DEFINE_MTYPE_STATIC(ZEBRA, ZSERV_CLIENT, "ZClients");
      83             : 
      84             : /*
      85             :  * Client thread events.
      86             :  *
      87             :  * These are used almost exclusively by client threads to drive their own event
      88             :  * loops. The only exception is in zserv_client_create(), which pushes an
      89             :  * initial ZSERV_CLIENT_READ event to start the API handler loop.
      90             :  */
      91             : enum zserv_client_event {
      92             :         /* Schedule a socket read */
      93             :         ZSERV_CLIENT_READ,
      94             :         /* Schedule a buffer write */
      95             :         ZSERV_CLIENT_WRITE,
      96             : };
      97             : 
      98             : /*
      99             :  * Main thread events.
     100             :  *
     101             :  * These are used by client threads to notify the main thread about various
     102             :  * events and to make processing requests.
     103             :  */
     104             : enum zserv_event {
     105             :         /* Schedule listen job on Zebra API socket */
     106             :         ZSERV_ACCEPT,
     107             :         /* The calling client has packets on its input buffer */
     108             :         ZSERV_PROCESS_MESSAGES,
     109             :         /* The calling client wishes to be killed */
     110             :         ZSERV_HANDLE_CLIENT_FAIL,
     111             : };
     112             : 
     113             : /*
     114             :  * Zebra server event driver for all client threads.
     115             :  *
     116             :  * This is essentially a wrapper around thread_add_event() that centralizes
     117             :  * those scheduling calls into one place.
     118             :  *
     119             :  * All calls to this function schedule an event on the pthread running the
     120             :  * provided client.
     121             :  *
     122             :  * client
     123             :  *    the client in question, and thread target
     124             :  *
     125             :  * event
     126             :  *    the event to notify them about
     127             :  */
     128             : static void zserv_client_event(struct zserv *client,
     129             :                                enum zserv_client_event event);
     130             : 
     131             : /*
     132             :  * Zebra server event driver for the main thread.
     133             :  *
     134             :  * This is essentially a wrapper around thread_add_event() that centralizes
     135             :  * those scheduling calls into one place.
     136             :  *
     137             :  * All calls to this function schedule an event on Zebra's main pthread.
     138             :  *
     139             :  * client
     140             :  *    the client in question
     141             :  *
     142             :  * event
     143             :  *    the event to notify the main thread about
     144             :  */
     145             : static void zserv_event(struct zserv *client, enum zserv_event event);
     146             : 
     147             : 
     148             : /* Client thread lifecycle -------------------------------------------------- */
     149             : 
     150             : /*
     151             :  * Free a zserv client object.
     152             :  */
     153           9 : void zserv_client_delete(struct zserv *client)
     154             : {
     155           0 :         XFREE(MTYPE_ZSERV_CLIENT, client);
     156           9 : }
     157             : 
     158             : /*
     159             :  * Log zapi message to zlog.
     160             :  *
     161             :  * errmsg (optional)
     162             :  *    Debugging message
     163             :  *
     164             :  * msg
     165             :  *    The message
     166             :  *
     167             :  * hdr (optional)
     168             :  *    The message header
     169             :  */
     170           0 : void zserv_log_message(const char *errmsg, struct stream *msg,
     171             :                        struct zmsghdr *hdr)
     172             : {
     173           0 :         zlog_debug("Rx'd ZAPI message");
     174           0 :         if (errmsg)
     175           0 :                 zlog_debug("%s", errmsg);
     176           0 :         if (hdr) {
     177           0 :                 zlog_debug(" Length: %d", hdr->length);
     178           0 :                 zlog_debug("Command: %s", zserv_command_string(hdr->command));
     179           0 :                 zlog_debug("    VRF: %u", hdr->vrf_id);
     180             :         }
     181           0 :         stream_hexdump(msg);
     182           0 : }
     183             : 
     184             : /*
     185             :  * Gracefuly shut down a client connection.
     186             :  *
     187             :  * Cancel any pending tasks for the client's thread. Then schedule a task on
     188             :  * the main thread to shut down the calling thread.
     189             :  *
     190             :  * It is not safe to close the client socket in this function. The socket is
     191             :  * owned by the main thread.
     192             :  *
     193             :  * Must be called from the client pthread, never the main thread.
     194             :  */
     195           5 : static void zserv_client_fail(struct zserv *client)
     196             : {
     197           5 :         flog_warn(EC_ZEBRA_CLIENT_IO_ERROR,
     198             :                   "Client '%s' encountered an error and is shutting down.",
     199             :                   zebra_route_string(client->proto));
     200             : 
     201           5 :         atomic_store_explicit(&client->pthread->running, false,
     202             :                               memory_order_relaxed);
     203             : 
     204           5 :         THREAD_OFF(client->t_read);
     205           5 :         THREAD_OFF(client->t_write);
     206           5 :         zserv_event(client, ZSERV_HANDLE_CLIENT_FAIL);
     207           5 : }
     208             : 
     209             : /*
     210             :  * Write all pending messages to client socket.
     211             :  *
     212             :  * This function first attempts to flush any buffered data. If unsuccessful,
     213             :  * the function reschedules itself and returns. If successful, it pops all
     214             :  * available messages from the output queue and continues to write data
     215             :  * directly to the socket until the socket would block. If the socket never
     216             :  * blocks and all data is written, the function returns without rescheduling
     217             :  * itself. If the socket ends up throwing EWOULDBLOCK, the remaining data is
     218             :  * buffered and the function reschedules itself.
     219             :  *
     220             :  * The utility of the buffer is that it allows us to vastly reduce lock
     221             :  * contention by allowing us to pop *all* messages off the output queue at once
     222             :  * instead of locking and unlocking each time we want to pop a single message
     223             :  * off the queue. The same thing could arguably be accomplished faster by
     224             :  * allowing the main thread to write directly into the buffer instead of
     225             :  * enqueuing packets onto an intermediary queue, but the intermediary queue
     226             :  * allows us to expose information about input and output queues to the user in
     227             :  * terms of number of packets rather than size of data.
     228             :  */
     229          75 : static void zserv_write(struct thread *thread)
     230             : {
     231          75 :         struct zserv *client = THREAD_ARG(thread);
     232          75 :         struct stream *msg;
     233          75 :         uint32_t wcmd = 0;
     234          75 :         struct stream_fifo *cache;
     235          75 :         uint64_t time_now = monotime(NULL);
     236             : 
     237             :         /* If we have any data pending, try to flush it first */
     238          75 :         switch (buffer_flush_all(client->wb, client->sock)) {
     239           0 :         case BUFFER_ERROR:
     240           0 :                 goto zwrite_fail;
     241           0 :         case BUFFER_PENDING:
     242           0 :                 frr_with_mutex (&client->stats_mtx) {
     243           0 :                         client->last_write_time = time_now;
     244             :                 }
     245           0 :                 zserv_client_event(client, ZSERV_CLIENT_WRITE);
     246           0 :                 return;
     247             :         case BUFFER_EMPTY:
     248             :                 break;
     249             :         }
     250             : 
     251          75 :         cache = stream_fifo_new();
     252             : 
     253         150 :         frr_with_mutex (&client->obuf_mtx) {
     254         299 :                 while (stream_fifo_head(client->obuf_fifo))
     255         224 :                         stream_fifo_push(cache,
     256             :                                          stream_fifo_pop(client->obuf_fifo));
     257             :         }
     258             : 
     259          75 :         if (cache->tail) {
     260          73 :                 msg = cache->tail;
     261          73 :                 stream_set_getp(msg, 0);
     262          73 :                 wcmd = stream_getw_from(msg, ZAPI_HEADER_CMD_LOCATION);
     263             :         }
     264             : 
     265         299 :         while (stream_fifo_head(cache)) {
     266         224 :                 msg = stream_fifo_pop(cache);
     267         224 :                 buffer_put(client->wb, STREAM_DATA(msg), stream_get_endp(msg));
     268         224 :                 stream_free(msg);
     269             :         }
     270             : 
     271          75 :         stream_fifo_free(cache);
     272             : 
     273             :         /* If we have any data pending, try to flush it first */
     274          75 :         switch (buffer_flush_all(client->wb, client->sock)) {
     275           0 :         case BUFFER_ERROR:
     276           0 :                 goto zwrite_fail;
     277           0 :         case BUFFER_PENDING:
     278           0 :                 frr_with_mutex (&client->stats_mtx) {
     279           0 :                         client->last_write_time = time_now;
     280             :                 }
     281           0 :                 zserv_client_event(client, ZSERV_CLIENT_WRITE);
     282           0 :                 return;
     283             :         case BUFFER_EMPTY:
     284             :                 break;
     285             :         }
     286             : 
     287          75 :         frr_with_mutex (&client->stats_mtx) {
     288          75 :                 client->last_write_cmd = wcmd;
     289          75 :                 client->last_write_time = time_now;
     290             :         }
     291          75 :         return;
     292             : 
     293           0 : zwrite_fail:
     294           0 :         flog_warn(EC_ZEBRA_CLIENT_WRITE_FAILED,
     295             :                   "%s: could not write to %s [fd = %d], closing.", __func__,
     296             :                   zebra_route_string(client->proto), client->sock);
     297           0 :         zserv_client_fail(client);
     298             : }
     299             : 
     300             : /*
     301             :  * Read and process data from a client socket.
     302             :  *
     303             :  * The responsibilities here are to read raw data from the client socket,
     304             :  * validate the header, encapsulate it into a single stream object, push it
     305             :  * onto the input queue and then notify the main thread that there is new data
     306             :  * available.
     307             :  *
     308             :  * This function first looks for any data in the client structure's working
     309             :  * input buffer. If data is present, it is assumed that reading stopped in a
     310             :  * previous invocation of this task and needs to be resumed to finish a message.
     311             :  * Otherwise, the socket data stream is assumed to be at the beginning of a new
     312             :  * ZAPI message (specifically at the header). The header is read and validated.
     313             :  * If the header passed validation then the length field found in the header is
     314             :  * used to compute the total length of the message. That much data is read (but
     315             :  * not inspected), appended to the header, placed into a stream and pushed onto
     316             :  * the client's input queue. A task is then scheduled on the main thread to
     317             :  * process the client's input queue. Finally, if all of this was successful,
     318             :  * this task reschedules itself.
     319             :  *
     320             :  * Any failure in any of these actions is handled by terminating the client.
     321             :  */
     322          33 : static void zserv_read(struct thread *thread)
     323             : {
     324          33 :         struct zserv *client = THREAD_ARG(thread);
     325          33 :         int sock;
     326          33 :         size_t already;
     327          33 :         struct stream_fifo *cache;
     328          33 :         uint32_t p2p_orig;
     329             : 
     330          33 :         uint32_t p2p;
     331          33 :         struct zmsghdr hdr;
     332             : 
     333          33 :         p2p_orig = atomic_load_explicit(&zrouter.packets_to_process,
     334             :                                         memory_order_relaxed);
     335          33 :         cache = stream_fifo_new();
     336          33 :         p2p = p2p_orig;
     337          33 :         sock = THREAD_FD(thread);
     338             : 
     339          93 :         while (p2p) {
     340          93 :                 ssize_t nb;
     341          93 :                 bool hdrvalid;
     342          93 :                 char errmsg[256];
     343             : 
     344          93 :                 already = stream_get_endp(client->ibuf_work);
     345             : 
     346             :                 /* Read length and command (if we don't have it already). */
     347          93 :                 if (already < ZEBRA_HEADER_SIZE) {
     348          93 :                         nb = stream_read_try(client->ibuf_work, sock,
     349             :                                              ZEBRA_HEADER_SIZE - already);
     350          93 :                         if ((nb == 0 || nb == -1)) {
     351           5 :                                 if (IS_ZEBRA_DEBUG_EVENT)
     352           0 :                                         zlog_debug("connection closed socket [%d]",
     353             :                                                    sock);
     354           5 :                                 goto zread_fail;
     355             :                         }
     356          88 :                         if (nb != (ssize_t)(ZEBRA_HEADER_SIZE - already)) {
     357             :                                 /* Try again later. */
     358             :                                 break;
     359             :                         }
     360             :                         already = ZEBRA_HEADER_SIZE;
     361             :                 }
     362             : 
     363             :                 /* Reset to read from the beginning of the incoming packet. */
     364          60 :                 stream_set_getp(client->ibuf_work, 0);
     365             : 
     366             :                 /* Fetch header values */
     367          60 :                 hdrvalid = zapi_parse_header(client->ibuf_work, &hdr);
     368             : 
     369          60 :                 if (!hdrvalid) {
     370           0 :                         snprintf(errmsg, sizeof(errmsg),
     371             :                                  "%s: Message has corrupt header", __func__);
     372           0 :                         zserv_log_message(errmsg, client->ibuf_work, NULL);
     373           0 :                         goto zread_fail;
     374             :                 }
     375             : 
     376             :                 /* Validate header */
     377          60 :                 if (hdr.marker != ZEBRA_HEADER_MARKER
     378          60 :                     || hdr.version != ZSERV_VERSION) {
     379           0 :                         snprintf(
     380             :                                 errmsg, sizeof(errmsg),
     381             :                                 "Message has corrupt header\n%s: socket %d version mismatch, marker %d, version %d",
     382           0 :                                 __func__, sock, hdr.marker, hdr.version);
     383           0 :                         zserv_log_message(errmsg, client->ibuf_work, &hdr);
     384           0 :                         goto zread_fail;
     385             :                 }
     386          60 :                 if (hdr.length < ZEBRA_HEADER_SIZE) {
     387           0 :                         snprintf(
     388             :                                 errmsg, sizeof(errmsg),
     389             :                                 "Message has corrupt header\n%s: socket %d message length %u is less than header size %d",
     390             :                                 __func__, sock, hdr.length, ZEBRA_HEADER_SIZE);
     391           0 :                         zserv_log_message(errmsg, client->ibuf_work, &hdr);
     392           0 :                         goto zread_fail;
     393             :                 }
     394          60 :                 if (hdr.length > STREAM_SIZE(client->ibuf_work)) {
     395           0 :                         snprintf(
     396             :                                 errmsg, sizeof(errmsg),
     397             :                                 "Message has corrupt header\n%s: socket %d message length %u exceeds buffer size %lu",
     398             :                                 __func__, sock, hdr.length,
     399             :                                 (unsigned long)STREAM_SIZE(client->ibuf_work));
     400           0 :                         zserv_log_message(errmsg, client->ibuf_work, &hdr);
     401           0 :                         goto zread_fail;
     402             :                 }
     403             : 
     404             :                 /* Read rest of data. */
     405          60 :                 if (already < hdr.length) {
     406          45 :                         nb = stream_read_try(client->ibuf_work, sock,
     407             :                                              hdr.length - already);
     408          45 :                         if ((nb == 0 || nb == -1)) {
     409           0 :                                 if (IS_ZEBRA_DEBUG_EVENT)
     410           0 :                                         zlog_debug(
     411             :                                                    "connection closed [%d] when reading zebra data",
     412             :                                                    sock);
     413           0 :                                 goto zread_fail;
     414             :                         }
     415          45 :                         if (nb != (ssize_t)(hdr.length - already)) {
     416             :                                 /* Try again later. */
     417             :                                 break;
     418             :                         }
     419             :                 }
     420             : 
     421             :                 /* Debug packet information. */
     422          60 :                 if (IS_ZEBRA_DEBUG_PACKET)
     423           0 :                         zlog_debug("zebra message[%s:%u:%u] comes from socket [%d]",
     424             :                                    zserv_command_string(hdr.command),
     425             :                                    hdr.vrf_id, hdr.length,
     426             :                                    sock);
     427             : 
     428          60 :                 stream_set_getp(client->ibuf_work, 0);
     429          60 :                 struct stream *msg = stream_dup(client->ibuf_work);
     430             : 
     431          60 :                 stream_fifo_push(cache, msg);
     432          60 :                 stream_reset(client->ibuf_work);
     433          60 :                 p2p--;
     434             :         }
     435             : 
     436          28 :         if (p2p < p2p_orig) {
     437          28 :                 uint64_t time_now = monotime(NULL);
     438             : 
     439             :                 /* update session statistics */
     440          28 :                 frr_with_mutex (&client->stats_mtx) {
     441          28 :                         client->last_read_time = time_now;
     442          28 :                         client->last_read_cmd = hdr.command;
     443             :                 }
     444             : 
     445             :                 /* publish read packets on client's input queue */
     446          56 :                 frr_with_mutex (&client->ibuf_mtx) {
     447          88 :                         while (cache->head)
     448          60 :                                 stream_fifo_push(client->ibuf_fifo,
     449             :                                                  stream_fifo_pop(cache));
     450             :                 }
     451             : 
     452             :                 /* Schedule job to process those packets */
     453          28 :                 zserv_event(client, ZSERV_PROCESS_MESSAGES);
     454             : 
     455             :         }
     456             : 
     457          28 :         if (IS_ZEBRA_DEBUG_PACKET)
     458           0 :                 zlog_debug("Read %d packets from client: %s", p2p_orig - p2p,
     459             :                            zebra_route_string(client->proto));
     460             : 
     461             :         /* Reschedule ourselves */
     462          28 :         zserv_client_event(client, ZSERV_CLIENT_READ);
     463             : 
     464          28 :         stream_fifo_free(cache);
     465             : 
     466          28 :         return;
     467             : 
     468           5 : zread_fail:
     469           5 :         stream_fifo_free(cache);
     470           5 :         zserv_client_fail(client);
     471             : }
     472             : 
     473         262 : static void zserv_client_event(struct zserv *client,
     474             :                                enum zserv_client_event event)
     475             : {
     476         262 :         switch (event) {
     477          37 :         case ZSERV_CLIENT_READ:
     478          37 :                 thread_add_read(client->pthread->master, zserv_read, client,
     479             :                                 client->sock, &client->t_read);
     480          37 :                 break;
     481         225 :         case ZSERV_CLIENT_WRITE:
     482         225 :                 thread_add_write(client->pthread->master, zserv_write, client,
     483             :                                  client->sock, &client->t_write);
     484         225 :                 break;
     485             :         }
     486         262 : }
     487             : 
     488             : /* Main thread lifecycle ---------------------------------------------------- */
     489             : 
     490             : /*
     491             :  * Read and process messages from a client.
     492             :  *
     493             :  * This task runs on the main pthread. It is scheduled by client pthreads when
     494             :  * they have new messages available on their input queues. The client is passed
     495             :  * as the task argument.
     496             :  *
     497             :  * Each message is popped off the client's input queue and the action associated
     498             :  * with the message is executed. This proceeds until there are no more messages,
     499             :  * an error occurs, or the processing limit is reached.
     500             :  *
     501             :  * The client's I/O thread can push at most zrouter.packets_to_process messages
     502             :  * onto the input buffer before notifying us there are packets to read. As long
     503             :  * as we always process zrouter.packets_to_process messages here, then we can
     504             :  * rely on the read thread to handle queuing this task enough times to process
     505             :  * everything on the input queue.
     506             :  */
     507          28 : static void zserv_process_messages(struct thread *thread)
     508             : {
     509          28 :         struct zserv *client = THREAD_ARG(thread);
     510          28 :         struct stream *msg;
     511          28 :         struct stream_fifo *cache = stream_fifo_new();
     512          28 :         uint32_t p2p = zrouter.packets_to_process;
     513          28 :         bool need_resched = false;
     514             : 
     515          56 :         frr_with_mutex (&client->ibuf_mtx) {
     516             :                 uint32_t i;
     517          88 :                 for (i = 0; i < p2p && stream_fifo_head(client->ibuf_fifo);
     518          60 :                      ++i) {
     519          60 :                         msg = stream_fifo_pop(client->ibuf_fifo);
     520          60 :                         stream_fifo_push(cache, msg);
     521             :                 }
     522             : 
     523          28 :                 msg = NULL;
     524             : 
     525             :                 /* Need to reschedule processing work if there are still
     526             :                  * packets in the fifo.
     527             :                  */
     528          28 :                 if (stream_fifo_head(client->ibuf_fifo))
     529           0 :                         need_resched = true;
     530             :         }
     531             : 
     532             :         /* Process the batch of messages */
     533          28 :         if (stream_fifo_head(cache))
     534          28 :                 zserv_handle_commands(client, cache);
     535             : 
     536          28 :         stream_fifo_free(cache);
     537             : 
     538             :         /* Reschedule ourselves if necessary */
     539          28 :         if (need_resched)
     540           0 :                 zserv_event(client, ZSERV_PROCESS_MESSAGES);
     541          28 : }
     542             : 
     543         225 : int zserv_send_message(struct zserv *client, struct stream *msg)
     544             : {
     545         450 :         frr_with_mutex (&client->obuf_mtx) {
     546         225 :                 stream_fifo_push(client->obuf_fifo, msg);
     547             :         }
     548             : 
     549         225 :         zserv_client_event(client, ZSERV_CLIENT_WRITE);
     550             : 
     551         225 :         return 0;
     552             : }
     553             : 
     554             : /*
     555             :  * Send a batch of messages to a connected Zebra API client.
     556             :  */
     557           0 : int zserv_send_batch(struct zserv *client, struct stream_fifo *fifo)
     558             : {
     559           0 :         struct stream *msg;
     560             : 
     561           0 :         frr_with_mutex (&client->obuf_mtx) {
     562           0 :                 msg = stream_fifo_pop(fifo);
     563           0 :                 while (msg) {
     564           0 :                         stream_fifo_push(client->obuf_fifo, msg);
     565           0 :                         msg = stream_fifo_pop(fifo);
     566             :                 }
     567             :         }
     568             : 
     569           0 :         zserv_client_event(client, ZSERV_CLIENT_WRITE);
     570             : 
     571           0 :         return 0;
     572             : }
     573             : 
     574             : /* Hooks for client connect / disconnect */
     575           9 : DEFINE_HOOK(zserv_client_connect, (struct zserv *client), (client));
     576          99 : DEFINE_KOOH(zserv_client_close, (struct zserv *client), (client));
     577             : 
     578             : /*
     579             :  * Deinitialize zebra client.
     580             :  *
     581             :  * - Deregister and deinitialize related internal resources
     582             :  * - Gracefuly close socket
     583             :  * - Free associated resources
     584             :  * - Free client structure
     585             :  *
     586             :  * This does *not* take any action on the struct thread * fields. These are
     587             :  * managed by the owning pthread and any tasks associated with them must have
     588             :  * been stopped prior to invoking this function.
     589             :  */
     590           9 : static void zserv_client_free(struct zserv *client)
     591             : {
     592           9 :         if (client == NULL)
     593             :                 return;
     594             : 
     595           9 :         hook_call(zserv_client_close, client);
     596             : 
     597             :         /* Close file descriptor. */
     598           9 :         if (client->sock) {
     599           9 :                 unsigned long nroutes;
     600           9 :                 unsigned long nnhgs;
     601             : 
     602           9 :                 close(client->sock);
     603             : 
     604           9 :                 if (DYNAMIC_CLIENT_GR_DISABLED(client)) {
     605           9 :                         zebra_mpls_client_cleanup_vrf_label(client->proto);
     606             : 
     607          18 :                         nroutes = rib_score_proto(client->proto,
     608           9 :                                                   client->instance);
     609           9 :                         zlog_notice(
     610             :                                 "client %d disconnected %lu %s routes removed from the rib",
     611             :                                 client->sock, nroutes,
     612             :                                 zebra_route_string(client->proto));
     613             : 
     614             :                         /* Not worrying about instance for now */
     615           9 :                         nnhgs = zebra_nhg_score_proto(client->proto);
     616           9 :                         zlog_notice(
     617             :                                 "client %d disconnected %lu %s nhgs removed from the rib",
     618             :                                 client->sock, nnhgs,
     619             :                                 zebra_route_string(client->proto));
     620             :                 }
     621           9 :                 client->sock = -1;
     622             :         }
     623             : 
     624             :         /* Free stream buffers. */
     625           9 :         if (client->ibuf_work)
     626           9 :                 stream_free(client->ibuf_work);
     627           9 :         if (client->obuf_work)
     628           9 :                 stream_free(client->obuf_work);
     629           9 :         if (client->ibuf_fifo)
     630           9 :                 stream_fifo_free(client->ibuf_fifo);
     631           9 :         if (client->obuf_fifo)
     632           9 :                 stream_fifo_free(client->obuf_fifo);
     633           9 :         if (client->wb)
     634           9 :                 buffer_free(client->wb);
     635             : 
     636             :         /* Free buffer mutexes */
     637           9 :         pthread_mutex_destroy(&client->stats_mtx);
     638           9 :         pthread_mutex_destroy(&client->obuf_mtx);
     639           9 :         pthread_mutex_destroy(&client->ibuf_mtx);
     640             : 
     641             :         /* Free bitmaps. */
     642          36 :         for (afi_t afi = AFI_IP; afi < AFI_MAX; afi++) {
     643         864 :                 for (int i = 0; i < ZEBRA_ROUTE_MAX; i++) {
     644         837 :                         vrf_bitmap_free(client->redist[afi][i]);
     645         837 :                         redist_del_all_instances(&client->mi_redist[afi][i]);
     646             :                 }
     647             : 
     648          27 :                 vrf_bitmap_free(client->redist_default[afi]);
     649          27 :                 vrf_bitmap_free(client->ridinfo[afi]);
     650          27 :                 vrf_bitmap_free(client->nhrp_neighinfo[afi]);
     651             :         }
     652             : 
     653             :         /*
     654             :          * If any instance are graceful restart enabled,
     655             :          * client is not deleted
     656             :          */
     657           9 :         if (DYNAMIC_CLIENT_GR_DISABLED(client)) {
     658           9 :                 if (IS_ZEBRA_DEBUG_EVENT)
     659           0 :                         zlog_debug("%s: Deleting client %s", __func__,
     660             :                                    zebra_route_string(client->proto));
     661           9 :                 zserv_client_delete(client);
     662             :         } else {
     663             :                 /* Handle cases where client has GR instance. */
     664           0 :                 if (IS_ZEBRA_DEBUG_EVENT)
     665           0 :                         zlog_debug("%s: client %s restart enabled", __func__,
     666             :                                    zebra_route_string(client->proto));
     667           0 :                 if (zebra_gr_client_disconnect(client) < 0)
     668           0 :                         zlog_err(
     669             :                                 "%s: GR enabled but could not handle disconnect event",
     670             :                                 __func__);
     671             :         }
     672             : }
     673             : 
     674           9 : void zserv_close_client(struct zserv *client)
     675             : {
     676           9 :         bool free_p = true;
     677             : 
     678           9 :         if (client->pthread) {
     679             :                 /* synchronously stop and join pthread */
     680           9 :                 frr_pthread_stop(client->pthread, NULL);
     681             : 
     682           9 :                 if (IS_ZEBRA_DEBUG_EVENT)
     683           0 :                         zlog_debug("Closing client '%s'",
     684             :                                    zebra_route_string(client->proto));
     685             : 
     686           9 :                 thread_cancel_event(zrouter.master, client);
     687           9 :                 THREAD_OFF(client->t_cleanup);
     688           9 :                 THREAD_OFF(client->t_process);
     689             : 
     690             :                 /* destroy pthread */
     691           9 :                 frr_pthread_destroy(client->pthread);
     692           9 :                 client->pthread = NULL;
     693             :         }
     694             : 
     695             :         /*
     696             :          * Final check in case the client struct is in use in another
     697             :          * pthread: if not in-use, continue and free the client
     698             :          */
     699          18 :         frr_with_mutex (&client_mutex) {
     700           9 :                 if (client->busy_count <= 0) {
     701             :                         /* remove from client list */
     702           9 :                         listnode_delete(zrouter.client_list, client);
     703             :                 } else {
     704             :                         /*
     705             :                          * The client session object may be in use, although
     706             :                          * the associated pthread is gone. Defer final
     707             :                          * cleanup.
     708             :                          */
     709           0 :                         client->is_closed = true;
     710           0 :                         free_p = false;
     711             :                 }
     712             :         }
     713             : 
     714             :         /* delete client */
     715           9 :         if (free_p)
     716           9 :                 zserv_client_free(client);
     717           9 : }
     718             : 
     719             : /*
     720             :  * This task is scheduled by a ZAPI client pthread on the main pthread when it
     721             :  * wants to stop itself. When this executes, the client connection should
     722             :  * already have been closed and the thread will most likely have died, but its
     723             :  * resources still need to be cleaned up.
     724             :  */
     725           5 : static void zserv_handle_client_fail(struct thread *thread)
     726             : {
     727           5 :         struct zserv *client = THREAD_ARG(thread);
     728             : 
     729           5 :         zserv_close_client(client);
     730           5 : }
     731             : 
     732             : /*
     733             :  * Create a new client.
     734             :  *
     735             :  * This is called when a new connection is accept()'d on the ZAPI socket. It
     736             :  * initializes new client structure, notifies any subscribers of the connection
     737             :  * event and spawns the client's thread.
     738             :  *
     739             :  * sock
     740             :  *    client's socket file descriptor
     741             :  */
     742           9 : static struct zserv *zserv_client_create(int sock)
     743             : {
     744           9 :         struct zserv *client;
     745           9 :         size_t stream_size =
     746             :                 MAX(ZEBRA_MAX_PACKET_SIZ, sizeof(struct zapi_route));
     747           9 :         int i;
     748           9 :         afi_t afi;
     749             : 
     750           9 :         client = XCALLOC(MTYPE_ZSERV_CLIENT, sizeof(struct zserv));
     751             : 
     752             :         /* Make client input/output buffer. */
     753           9 :         client->sock = sock;
     754           9 :         client->ibuf_fifo = stream_fifo_new();
     755           9 :         client->obuf_fifo = stream_fifo_new();
     756           9 :         client->ibuf_work = stream_new(stream_size);
     757           9 :         client->obuf_work = stream_new(stream_size);
     758           9 :         client->connect_time = monotime(NULL);
     759           9 :         pthread_mutex_init(&client->ibuf_mtx, NULL);
     760           9 :         pthread_mutex_init(&client->obuf_mtx, NULL);
     761           9 :         pthread_mutex_init(&client->stats_mtx, NULL);
     762           9 :         client->wb = buffer_new(0);
     763           9 :         TAILQ_INIT(&(client->gr_info_queue));
     764             : 
     765             :         /* Initialize flags */
     766          36 :         for (afi = AFI_IP; afi < AFI_MAX; afi++) {
     767         864 :                 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
     768         837 :                         client->redist[afi][i] = vrf_bitmap_init();
     769          27 :                 client->redist_default[afi] = vrf_bitmap_init();
     770          27 :                 client->ridinfo[afi] = vrf_bitmap_init();
     771          27 :                 client->nhrp_neighinfo[afi] = vrf_bitmap_init();
     772             :         }
     773             : 
     774             :         /* Add this client to linked list. */
     775          18 :         frr_with_mutex (&client_mutex) {
     776           9 :                 listnode_add(zrouter.client_list, client);
     777             :         }
     778             : 
     779           9 :         struct frr_pthread_attr zclient_pthr_attrs = {
     780           9 :                 .start = frr_pthread_attr_default.start,
     781           9 :                 .stop = frr_pthread_attr_default.stop
     782             :         };
     783          18 :         client->pthread =
     784           9 :                 frr_pthread_new(&zclient_pthr_attrs, "Zebra API client thread",
     785             :                                 "zebra_apic");
     786             : 
     787             :         /* start read loop */
     788           9 :         zserv_client_event(client, ZSERV_CLIENT_READ);
     789             : 
     790             :         /* call callbacks */
     791           9 :         hook_call(zserv_client_connect, client);
     792             : 
     793             :         /* start pthread */
     794           9 :         frr_pthread_run(client->pthread, NULL);
     795             : 
     796           9 :         return client;
     797             : }
     798             : 
     799             : /*
     800             :  * Retrieve a client object by the complete tuple of
     801             :  * {protocol, instance, session}. This version supports use
     802             :  * from a different pthread: the object will be returned marked
     803             :  * in-use. The caller *must* release the client object with the
     804             :  * release_client() api, to ensure that the in-use marker is cleared properly.
     805             :  */
     806           0 : struct zserv *zserv_acquire_client(uint8_t proto, unsigned short instance,
     807             :                                    uint32_t session_id)
     808             : {
     809           0 :         struct zserv *client = NULL;
     810             : 
     811           0 :         frr_with_mutex (&client_mutex) {
     812           0 :                 client = find_client_internal(proto, instance, session_id);
     813           0 :                 if (client) {
     814             :                         /* Don't return a dead/closed client object */
     815           0 :                         if (client->is_closed)
     816             :                                 client = NULL;
     817             :                         else
     818           0 :                                 client->busy_count++;
     819             :                 }
     820             :         }
     821             : 
     822           0 :         return client;
     823             : }
     824             : 
     825             : /*
     826             :  * Release a client object that was acquired with the acquire_client() api.
     827             :  * After this has been called, the caller must not use the client pointer -
     828             :  * it may be freed if the client has closed.
     829             :  */
     830           0 : void zserv_release_client(struct zserv *client)
     831             : {
     832             :         /*
     833             :          * Once we've decremented the client object's refcount, it's possible
     834             :          * for it to be deleted as soon as we release the lock, so we won't
     835             :          * touch the object again.
     836             :          */
     837           0 :         frr_with_mutex (&client_mutex) {
     838           0 :                 client->busy_count--;
     839             : 
     840           0 :                 if (client->busy_count <= 0) {
     841             :                         /*
     842             :                          * No more users of the client object. If the client
     843             :                          * session is closed, schedule cleanup on the zebra
     844             :                          * main pthread.
     845             :                          */
     846           0 :                         if (client->is_closed)
     847           0 :                                 thread_add_event(zrouter.master,
     848             :                                                  zserv_handle_client_fail,
     849             :                                                  client, 0, &client->t_cleanup);
     850             :                 }
     851             :         }
     852             : 
     853             :         /*
     854             :          * Cleanup must take place on the zebra main pthread, so we've
     855             :          * scheduled an event.
     856             :          */
     857           0 : }
     858             : 
     859             : /*
     860             :  * Accept socket connection.
     861             :  */
     862           9 : static void zserv_accept(struct thread *thread)
     863             : {
     864           9 :         int accept_sock;
     865           9 :         int client_sock;
     866           9 :         struct sockaddr_in client;
     867           9 :         socklen_t len;
     868             : 
     869           9 :         accept_sock = THREAD_FD(thread);
     870             : 
     871             :         /* Reregister myself. */
     872           9 :         zserv_event(NULL, ZSERV_ACCEPT);
     873             : 
     874           9 :         len = sizeof(struct sockaddr_in);
     875           9 :         client_sock = accept(accept_sock, (struct sockaddr *)&client, &len);
     876             : 
     877           9 :         if (client_sock < 0) {
     878           0 :                 flog_err_sys(EC_LIB_SOCKET, "Can't accept zebra socket: %s",
     879             :                              safe_strerror(errno));
     880           0 :                 return;
     881             :         }
     882             : 
     883             :         /* Make client socket non-blocking.  */
     884           9 :         set_nonblocking(client_sock);
     885             : 
     886             :         /* Create new zebra client. */
     887           9 :         zserv_client_create(client_sock);
     888             : }
     889             : 
     890           3 : void zserv_close(void)
     891             : {
     892             :         /*
     893             :          * On shutdown, let's close the socket down
     894             :          * so that long running processes of killing the
     895             :          * routing table doesn't leave us in a bad
     896             :          * state where a client tries to reconnect
     897             :          */
     898           3 :         close(zsock);
     899           3 :         zsock = -1;
     900             : 
     901             :         /* Free client list's mutex */
     902           3 :         pthread_mutex_destroy(&client_mutex);
     903           3 : }
     904             : 
     905           3 : void zserv_start(char *path)
     906             : {
     907           3 :         int ret;
     908           3 :         mode_t old_mask;
     909           3 :         struct sockaddr_storage sa;
     910           3 :         socklen_t sa_len;
     911             : 
     912           3 :         if (!frr_zclient_addr(&sa, &sa_len, path))
     913             :                 /* should be caught in zebra main() */
     914           0 :                 return;
     915             : 
     916             :         /* Set umask */
     917           3 :         old_mask = umask(0077);
     918             : 
     919             :         /* Make UNIX domain socket. */
     920           3 :         zsock = socket(sa.ss_family, SOCK_STREAM, 0);
     921           3 :         if (zsock < 0) {
     922           0 :                 flog_err_sys(EC_LIB_SOCKET, "Can't create zserv socket: %s",
     923             :                              safe_strerror(errno));
     924           0 :                 return;
     925             :         }
     926             : 
     927           3 :         if (sa.ss_family != AF_UNIX) {
     928           0 :                 sockopt_reuseaddr(zsock);
     929           0 :                 sockopt_reuseport(zsock);
     930             :         } else {
     931           3 :                 struct sockaddr_un *suna = (struct sockaddr_un *)&sa;
     932           3 :                 if (suna->sun_path[0])
     933           3 :                         unlink(suna->sun_path);
     934             :         }
     935             : 
     936           3 :         setsockopt_so_recvbuf(zsock, 1048576);
     937           3 :         setsockopt_so_sendbuf(zsock, 1048576);
     938             : 
     939           6 :         frr_with_privs((sa.ss_family != AF_UNIX) ? &zserv_privs : NULL) {
     940           3 :                 ret = bind(zsock, (struct sockaddr *)&sa, sa_len);
     941             :         }
     942           3 :         if (ret < 0) {
     943           0 :                 flog_err_sys(EC_LIB_SOCKET, "Can't bind zserv socket on %s: %s",
     944             :                              path, safe_strerror(errno));
     945           0 :                 close(zsock);
     946           0 :                 zsock = -1;
     947           0 :                 return;
     948             :         }
     949             : 
     950           3 :         ret = listen(zsock, 5);
     951           3 :         if (ret < 0) {
     952           0 :                 flog_err_sys(EC_LIB_SOCKET,
     953             :                              "Can't listen to zserv socket %s: %s", path,
     954             :                              safe_strerror(errno));
     955           0 :                 close(zsock);
     956           0 :                 zsock = -1;
     957           0 :                 return;
     958             :         }
     959             : 
     960           3 :         umask(old_mask);
     961             : 
     962           3 :         zserv_event(NULL, ZSERV_ACCEPT);
     963             : }
     964             : 
     965          45 : void zserv_event(struct zserv *client, enum zserv_event event)
     966             : {
     967          45 :         switch (event) {
     968          12 :         case ZSERV_ACCEPT:
     969          12 :                 thread_add_read(zrouter.master, zserv_accept, NULL, zsock,
     970             :                                 NULL);
     971          12 :                 break;
     972          28 :         case ZSERV_PROCESS_MESSAGES:
     973          28 :                 thread_add_event(zrouter.master, zserv_process_messages, client,
     974             :                                  0, &client->t_process);
     975          28 :                 break;
     976           5 :         case ZSERV_HANDLE_CLIENT_FAIL:
     977           5 :                 thread_add_event(zrouter.master, zserv_handle_client_fail,
     978             :                                  client, 0, &client->t_cleanup);
     979             :         }
     980          45 : }
     981             : 
     982             : 
     983             : /* General purpose ---------------------------------------------------------- */
     984             : 
     985             : #define ZEBRA_TIME_BUF 32
     986           0 : static char *zserv_time_buf(time_t *time1, char *buf, int buflen)
     987             : {
     988           0 :         time_t now;
     989             : 
     990           0 :         assert(buf != NULL);
     991           0 :         assert(buflen >= ZEBRA_TIME_BUF);
     992           0 :         assert(time1 != NULL);
     993             : 
     994           0 :         if (!*time1) {
     995           0 :                 snprintf(buf, buflen, "never   ");
     996           0 :                 return (buf);
     997             :         }
     998             : 
     999           0 :         now = monotime(NULL);
    1000           0 :         now -= *time1;
    1001             : 
    1002           0 :         frrtime_to_interval(now, buf, buflen);
    1003             : 
    1004           0 :         return buf;
    1005             : }
    1006             : 
    1007             : /* Display client info details */
    1008           0 : static void zebra_show_client_detail(struct vty *vty, struct zserv *client)
    1009             : {
    1010           0 :         char cbuf[ZEBRA_TIME_BUF], rbuf[ZEBRA_TIME_BUF];
    1011           0 :         char wbuf[ZEBRA_TIME_BUF], nhbuf[ZEBRA_TIME_BUF], mbuf[ZEBRA_TIME_BUF];
    1012           0 :         time_t connect_time, last_read_time, last_write_time;
    1013           0 :         uint32_t last_read_cmd, last_write_cmd;
    1014             : 
    1015           0 :         vty_out(vty, "Client: %s", zebra_route_string(client->proto));
    1016           0 :         if (client->instance)
    1017           0 :                 vty_out(vty, " Instance: %u", client->instance);
    1018           0 :         if (client->session_id)
    1019           0 :                 vty_out(vty, " [%u]", client->session_id);
    1020           0 :         vty_out(vty, "\n");
    1021             : 
    1022           0 :         vty_out(vty, "------------------------ \n");
    1023           0 :         vty_out(vty, "FD: %d \n", client->sock);
    1024             : 
    1025           0 :         frr_with_mutex (&client->stats_mtx) {
    1026           0 :                 connect_time = client->connect_time;
    1027           0 :                 last_read_time = client->last_read_time;
    1028           0 :                 last_write_time = client->last_write_time;
    1029             : 
    1030           0 :                 last_read_cmd = client->last_read_cmd;
    1031           0 :                 last_write_cmd = client->last_write_cmd;
    1032             :         }
    1033             : 
    1034           0 :         vty_out(vty, "Connect Time: %s \n",
    1035             :                 zserv_time_buf(&connect_time, cbuf, ZEBRA_TIME_BUF));
    1036           0 :         if (client->nh_reg_time) {
    1037           0 :                 vty_out(vty, "Nexthop Registry Time: %s \n",
    1038             :                         zserv_time_buf(&client->nh_reg_time, nhbuf,
    1039             :                                        ZEBRA_TIME_BUF));
    1040           0 :                 if (client->nh_last_upd_time)
    1041           0 :                         vty_out(vty, "Nexthop Last Update Time: %s \n",
    1042             :                                 zserv_time_buf(&client->nh_last_upd_time, mbuf,
    1043             :                                                ZEBRA_TIME_BUF));
    1044             :                 else
    1045           0 :                         vty_out(vty, "No Nexthop Update sent\n");
    1046             :         } else
    1047           0 :                 vty_out(vty, "Not registered for Nexthop Updates\n");
    1048             : 
    1049           0 :         vty_out(vty,
    1050             :                 "Client will %sbe notified about the status of its routes.\n",
    1051           0 :                 client->notify_owner ? "" : "Not ");
    1052             : 
    1053           0 :         vty_out(vty, "Last Msg Rx Time: %s \n",
    1054             :                 zserv_time_buf(&last_read_time, rbuf, ZEBRA_TIME_BUF));
    1055           0 :         vty_out(vty, "Last Msg Tx Time: %s \n",
    1056             :                 zserv_time_buf(&last_write_time, wbuf, ZEBRA_TIME_BUF));
    1057           0 :         if (last_read_cmd)
    1058           0 :                 vty_out(vty, "Last Rcvd Cmd: %s \n",
    1059             :                         zserv_command_string(last_read_cmd));
    1060           0 :         if (last_write_cmd)
    1061           0 :                 vty_out(vty, "Last Sent Cmd: %s \n",
    1062             :                         zserv_command_string(last_write_cmd));
    1063           0 :         vty_out(vty, "\n");
    1064             : 
    1065           0 :         vty_out(vty, "Type        Add         Update      Del \n");
    1066           0 :         vty_out(vty, "================================================== \n");
    1067           0 :         vty_out(vty, "IPv4        %-12u%-12u%-12u\n", client->v4_route_add_cnt,
    1068             :                 client->v4_route_upd8_cnt, client->v4_route_del_cnt);
    1069           0 :         vty_out(vty, "IPv6        %-12u%-12u%-12u\n", client->v6_route_add_cnt,
    1070             :                 client->v6_route_upd8_cnt, client->v6_route_del_cnt);
    1071           0 :         vty_out(vty, "Redist:v4   %-12u%-12u%-12u\n", client->redist_v4_add_cnt,
    1072             :                 0, client->redist_v4_del_cnt);
    1073           0 :         vty_out(vty, "Redist:v6   %-12u%-12u%-12u\n", client->redist_v6_add_cnt,
    1074             :                 0, client->redist_v6_del_cnt);
    1075           0 :         vty_out(vty, "VRF         %-12u%-12u%-12u\n", client->vrfadd_cnt, 0,
    1076             :                 client->vrfdel_cnt);
    1077           0 :         vty_out(vty, "Connected   %-12u%-12u%-12u\n", client->ifadd_cnt, 0,
    1078             :                 client->ifdel_cnt);
    1079           0 :         vty_out(vty, "Interface   %-12u%-12u%-12u\n", client->ifup_cnt, 0,
    1080             :                 client->ifdown_cnt);
    1081           0 :         vty_out(vty, "Intf Addr   %-12u%-12u%-12u\n",
    1082             :                 client->connected_rt_add_cnt, 0, client->connected_rt_del_cnt);
    1083           0 :         vty_out(vty, "BFD peer    %-12u%-12u%-12u\n", client->bfd_peer_add_cnt,
    1084             :                 client->bfd_peer_upd8_cnt, client->bfd_peer_del_cnt);
    1085           0 :         vty_out(vty, "NHT v4      %-12u%-12u%-12u\n",
    1086             :                 client->v4_nh_watch_add_cnt, 0, client->v4_nh_watch_rem_cnt);
    1087           0 :         vty_out(vty, "NHT v6      %-12u%-12u%-12u\n",
    1088             :                 client->v6_nh_watch_add_cnt, 0, client->v6_nh_watch_rem_cnt);
    1089           0 :         vty_out(vty, "VxLAN SG    %-12u%-12u%-12u\n", client->vxlan_sg_add_cnt,
    1090             :                 0, client->vxlan_sg_del_cnt);
    1091           0 :         vty_out(vty, "VNI         %-12u%-12u%-12u\n", client->vniadd_cnt, 0,
    1092             :                 client->vnidel_cnt);
    1093           0 :         vty_out(vty, "L3-VNI      %-12u%-12u%-12u\n", client->l3vniadd_cnt, 0,
    1094             :                 client->l3vnidel_cnt);
    1095           0 :         vty_out(vty, "MAC-IP      %-12u%-12u%-12u\n", client->macipadd_cnt, 0,
    1096             :                 client->macipdel_cnt);
    1097           0 :         vty_out(vty, "ES          %-12u%-12u%-12u\n", client->local_es_add_cnt,
    1098             :                 0, client->local_es_del_cnt);
    1099           0 :         vty_out(vty, "ES-EVI      %-12u%-12u%-12u\n",
    1100             :                 client->local_es_evi_add_cnt, 0, client->local_es_evi_del_cnt);
    1101           0 :         vty_out(vty, "Errors: %u\n", client->error_cnt);
    1102             : 
    1103             : #if defined DEV_BUILD
    1104             :         vty_out(vty, "Input Fifo: %zu:%zu Output Fifo: %zu:%zu\n",
    1105             :                 client->ibuf_fifo->count, client->ibuf_fifo->max_count,
    1106             :                 client->obuf_fifo->count, client->obuf_fifo->max_count);
    1107             : #endif
    1108           0 :         vty_out(vty, "\n");
    1109           0 : }
    1110             : 
    1111             : /* Display stale client information */
    1112           0 : static void zebra_show_stale_client_detail(struct vty *vty,
    1113             :                                            struct zserv *client)
    1114             : {
    1115           0 :         char buf[PREFIX2STR_BUFFER];
    1116           0 :         time_t uptime;
    1117           0 :         struct client_gr_info *info = NULL;
    1118           0 :         struct zserv *s = NULL;
    1119           0 :         bool first_p = true;
    1120             : 
    1121           0 :         TAILQ_FOREACH (info, &client->gr_info_queue, gr_info) {
    1122           0 :                 if (first_p) {
    1123           0 :                         vty_out(vty, "Stale Client Information\n");
    1124           0 :                         vty_out(vty, "------------------------\n");
    1125             : 
    1126           0 :                         if (client->instance)
    1127           0 :                                 vty_out(vty, " Instance: %u", client->instance);
    1128           0 :                         if (client->session_id)
    1129           0 :                                 vty_out(vty, " [%u]", client->session_id);
    1130             : 
    1131             :                         first_p = false;
    1132             :                 }
    1133             : 
    1134           0 :                 vty_out(vty, "VRF : %s\n", vrf_id_to_name(info->vrf_id));
    1135           0 :                 vty_out(vty, "Capabilities : ");
    1136           0 :                 switch (info->capabilities) {
    1137           0 :                 case ZEBRA_CLIENT_GR_CAPABILITIES:
    1138           0 :                         vty_out(vty, "Graceful Restart(%u seconds)\n",
    1139             :                                 info->stale_removal_time);
    1140           0 :                         break;
    1141           0 :                 case ZEBRA_CLIENT_ROUTE_UPDATE_COMPLETE:
    1142             :                 case ZEBRA_CLIENT_ROUTE_UPDATE_PENDING:
    1143             :                 case ZEBRA_CLIENT_GR_DISABLE:
    1144             :                 case ZEBRA_CLIENT_RIB_STALE_TIME:
    1145           0 :                         vty_out(vty, "None\n");
    1146           0 :                         break;
    1147             :                 }
    1148             : 
    1149           0 :                 if (ZEBRA_CLIENT_GR_ENABLED(info->capabilities)) {
    1150           0 :                         if (info->stale_client_ptr) {
    1151           0 :                                 s = (struct zserv *)(info->stale_client_ptr);
    1152           0 :                                 uptime = monotime(NULL);
    1153           0 :                                 uptime -= s->restart_time;
    1154             : 
    1155           0 :                                 frrtime_to_interval(uptime, buf, sizeof(buf));
    1156             : 
    1157           0 :                                 vty_out(vty, "Last restart time : %s ago\n",
    1158             :                                         buf);
    1159             : 
    1160           0 :                                 vty_out(vty, "Stalepath removal time: %d sec\n",
    1161             :                                         info->stale_removal_time);
    1162           0 :                                 if (info->t_stale_removal) {
    1163           0 :                                         vty_out(vty,
    1164             :                                                 "Stale delete timer: %ld sec\n",
    1165             :                                                 thread_timer_remain_second(
    1166             :                                                         info->t_stale_removal));
    1167             :                                 }
    1168             :                         }
    1169           0 :                         vty_out(vty, "Current AFI : %d\n", info->current_afi);
    1170           0 :                         if (info->current_prefix)
    1171           0 :                                 vty_out(vty, "Current prefix : %pFX\n",
    1172             :                                         info->current_prefix);
    1173             :                 }
    1174             :         }
    1175           0 :         vty_out(vty, "\n");
    1176           0 :         return;
    1177             : }
    1178             : 
    1179           0 : static void zebra_show_client_brief(struct vty *vty, struct zserv *client)
    1180             : {
    1181           0 :         char client_string[80];
    1182           0 :         char cbuf[ZEBRA_TIME_BUF], rbuf[ZEBRA_TIME_BUF];
    1183           0 :         char wbuf[ZEBRA_TIME_BUF];
    1184           0 :         time_t connect_time, last_read_time, last_write_time;
    1185             : 
    1186           0 :         frr_with_mutex (&client->stats_mtx) {
    1187           0 :                 connect_time = client->connect_time;
    1188           0 :                 last_read_time = client->last_read_time;
    1189           0 :                 last_write_time = client->last_write_time;
    1190             :         }
    1191             : 
    1192           0 :         if (client->instance || client->session_id)
    1193           0 :                 snprintfrr(client_string, sizeof(client_string), "%s[%u:%u]",
    1194           0 :                            zebra_route_string(client->proto), client->instance,
    1195             :                            client->session_id);
    1196             :         else
    1197           0 :                 snprintfrr(client_string, sizeof(client_string), "%s",
    1198           0 :                            zebra_route_string(client->proto));
    1199             : 
    1200           0 :         vty_out(vty, "%-10s%12s %12s%12s %10d/%-10d %10d/%-10d\n",
    1201             :                 client_string,
    1202             :                 zserv_time_buf(&connect_time, cbuf, ZEBRA_TIME_BUF),
    1203             :                 zserv_time_buf(&last_read_time, rbuf, ZEBRA_TIME_BUF),
    1204             :                 zserv_time_buf(&last_write_time, wbuf, ZEBRA_TIME_BUF),
    1205           0 :                 client->v4_route_add_cnt + client->v4_route_upd8_cnt,
    1206             :                 client->v4_route_del_cnt,
    1207           0 :                 client->v6_route_add_cnt + client->v6_route_upd8_cnt,
    1208             :                 client->v6_route_del_cnt);
    1209           0 : }
    1210             : 
    1211             : /*
    1212             :  * Common logic that searches the client list for a zapi client; this
    1213             :  * MUST be called holding the client list mutex.
    1214             :  */
    1215          25 : static struct zserv *find_client_internal(uint8_t proto,
    1216             :                                           unsigned short instance,
    1217             :                                           uint32_t session_id)
    1218             : {
    1219          25 :         struct listnode *node, *nnode;
    1220          25 :         struct zserv *client = NULL;
    1221             : 
    1222          77 :         for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
    1223          33 :                 if (client->proto == proto && client->instance == instance &&
    1224           6 :                     client->session_id == session_id)
    1225             :                         break;
    1226             :         }
    1227             : 
    1228          25 :         return client;
    1229             : }
    1230             : 
    1231             : /*
    1232             :  * Public api that searches for a client session; this version is
    1233             :  * used from the zebra main pthread.
    1234             :  */
    1235          25 : struct zserv *zserv_find_client(uint8_t proto, unsigned short instance)
    1236             : {
    1237          25 :         struct zserv *client;
    1238             : 
    1239          25 :         frr_with_mutex (&client_mutex) {
    1240          25 :                 client = find_client_internal(proto, instance, 0);
    1241             :         }
    1242             : 
    1243          25 :         return client;
    1244             : }
    1245             : 
    1246             : /*
    1247             :  * Retrieve a client by its protocol, instance number, and session id.
    1248             :  */
    1249           0 : struct zserv *zserv_find_client_session(uint8_t proto, unsigned short instance,
    1250             :                                         uint32_t session_id)
    1251             : {
    1252           0 :         struct zserv *client;
    1253             : 
    1254           0 :         frr_with_mutex (&client_mutex) {
    1255           0 :                 client = find_client_internal(proto, instance, session_id);
    1256             :         }
    1257             : 
    1258           0 :         return client;
    1259             : 
    1260             : }
    1261             : 
    1262             : /* This command is for debugging purpose. */
    1263           0 : DEFUN (show_zebra_client,
    1264             :        show_zebra_client_cmd,
    1265             :        "show zebra client",
    1266             :        SHOW_STR
    1267             :        ZEBRA_STR
    1268             :        "Client information\n")
    1269             : {
    1270           0 :         struct listnode *node;
    1271           0 :         struct zserv *client;
    1272             : 
    1273           0 :         for (ALL_LIST_ELEMENTS_RO(zrouter.client_list, node, client)) {
    1274           0 :                 zebra_show_client_detail(vty, client);
    1275             :                 /* Show GR info if present */
    1276           0 :                 zebra_show_stale_client_detail(vty, client);
    1277             :         }
    1278             : 
    1279           0 :         return CMD_SUCCESS;
    1280             : }
    1281             : 
    1282             : /* This command is for debugging purpose. */
    1283           0 : DEFUN (show_zebra_client_summary,
    1284             :        show_zebra_client_summary_cmd,
    1285             :        "show zebra client summary",
    1286             :        SHOW_STR
    1287             :        ZEBRA_STR
    1288             :        "Client information brief\n"
    1289             :        "Brief Summary\n")
    1290             : {
    1291           0 :         struct listnode *node;
    1292           0 :         struct zserv *client;
    1293             : 
    1294           0 :         vty_out(vty,
    1295             :                 "Name      Connect Time    Last Read  Last Write      IPv4 Routes           IPv6 Routes\n");
    1296           0 :         vty_out(vty,
    1297             :                 "------------------------------------------------------------------------------------------\n");
    1298             : 
    1299           0 :         for (ALL_LIST_ELEMENTS_RO(zrouter.client_list, node, client))
    1300           0 :                 zebra_show_client_brief(vty, client);
    1301             : 
    1302           0 :         vty_out(vty, "Routes column shows (added+updated)/deleted\n");
    1303           0 :         return CMD_SUCCESS;
    1304             : }
    1305             : 
    1306           9 : static int zserv_client_close_cb(struct zserv *closed_client)
    1307             : {
    1308           9 :         struct listnode *node, *nnode;
    1309           9 :         struct zserv *client = NULL;
    1310             : 
    1311          27 :         for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
    1312           9 :                 if (client->proto == closed_client->proto)
    1313           0 :                         continue;
    1314             : 
    1315           9 :                 zsend_client_close_notify(client, closed_client);
    1316             :         }
    1317             : 
    1318           9 :         return 0;
    1319             : }
    1320             : 
    1321           3 : void zserv_init(void)
    1322             : {
    1323             :         /* Client list init. */
    1324           3 :         zrouter.client_list = list_new();
    1325           3 :         zrouter.stale_client_list = list_new();
    1326             : 
    1327             :         /* Misc init. */
    1328           3 :         zsock = -1;
    1329           3 :         pthread_mutex_init(&client_mutex, NULL);
    1330             : 
    1331           3 :         install_element(ENABLE_NODE, &show_zebra_client_cmd);
    1332           3 :         install_element(ENABLE_NODE, &show_zebra_client_summary_cmd);
    1333             : 
    1334           3 :         hook_register(zserv_client_close, zserv_client_close_cb);
    1335           3 : }

Generated by: LCOV version v1.16-topotato