4 * Copyright (C) 1997-1999 Kunihiro Ishiguro
5 * Copyright (C) 2015-2018 Cumulus Networks, Inc.
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 * You should have received a copy of the GNU General Public License along
19 * with this program; see the file COPYING; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 /* clang-format off */
26 #include <errno.h> /* for errno */
27 #include <netinet/in.h> /* for sockaddr_in */
28 #include <stdint.h> /* for uint8_t */
29 #include <stdio.h> /* for snprintf */
30 #include <sys/socket.h> /* for sockaddr_storage, AF_UNIX, accept... */
31 #include <sys/stat.h> /* for umask, mode_t */
32 #include <sys/un.h> /* for sockaddr_un */
33 #include <time.h> /* for NULL, tm, gmtime, time_t */
34 #include <unistd.h> /* for close, unlink, ssize_t */
36 #include "lib/buffer.h" /* for BUFFER_EMPTY, BUFFER_ERROR, BUFFE... */
37 #include "lib/command.h" /* for vty, install_element, CMD_SUCCESS... */
38 #include "lib/hook.h" /* for DEFINE_HOOK, DEFINE_KOOH, hook_call */
39 #include "lib/linklist.h" /* for ALL_LIST_ELEMENTS_RO, ALL_LIST_EL... */
40 #include "lib/libfrr.h" /* for frr_zclient_addr */
41 #include "lib/log.h" /* for zlog_warn, zlog_debug, safe_strerror */
42 #include "lib/memory.h" /* for MTYPE_TMP, XCALLOC, XFREE */
43 #include "lib/monotime.h" /* for monotime, ONE_DAY_SECOND, ONE_WEE... */
44 #include "lib/network.h" /* for set_nonblocking */
45 #include "lib/privs.h" /* for zebra_privs_t, ZPRIVS_LOWER, ZPRI... */
46 #include "lib/route_types.h" /* for ZEBRA_ROUTE_MAX */
47 #include "lib/sockopt.h" /* for setsockopt_so_recvbuf, setsockopt... */
48 #include "lib/sockunion.h" /* for sockopt_reuseaddr, sockopt_reuseport */
49 #include "lib/stream.h" /* for STREAM_SIZE, stream (ptr only), ... */
50 #include "lib/thread.h" /* for thread (ptr only), THREAD_ARG, ... */
51 #include "lib/vrf.h" /* for vrf_info_lookup, VRF_DEFAULT */
52 #include "lib/vty.h" /* for vty_out, vty (ptr only) */
53 #include "lib/zassert.h" /* for assert */
54 #include "lib/zclient.h" /* for zmsghdr, ZEBRA_HEADER_SIZE, ZEBRA... */
56 #include "zebra/debug.h" /* for various debugging macros */
57 #include "zebra/rib.h" /* for rib_score_proto */
58 #include "zebra/zapi_msg.h" /* for zserv_handle_commands */
59 #include "zebra/zebra_vrf.h" /* for zebra_vrf_lookup_by_id, zvrf */
60 #include "zebra/zserv.h" /* for zserv */
63 /* Event list of zebra. */
64 enum event
{ ZEBRA_READ
, ZEBRA_WRITE
};
66 extern struct zebra_privs_t zserv_privs
;
67 /* post event into client */
68 static void zebra_event(struct zserv
*client
, enum event event
);
71 /* Public interface --------------------------------------------------------- */
73 int zebra_server_send_message(struct zserv
*client
, struct stream
*msg
)
75 pthread_mutex_lock(&client
->obuf_mtx
);
77 stream_fifo_push(client
->obuf_fifo
, msg
);
78 zebra_event(client
, ZEBRA_WRITE
);
80 pthread_mutex_unlock(&client
->obuf_mtx
);
84 /* Hooks for client connect / disconnect */
85 DEFINE_HOOK(zapi_client_connect
, (struct zserv
*client
), (client
));
86 DEFINE_KOOH(zapi_client_close
, (struct zserv
*client
), (client
));
89 * Deinitialize zebra client.
91 * - Deregister and deinitialize related internal resources
92 * - Gracefully close socket
93 * - Free associated resources
94 * - Free client structure
96 * This does *not* take any action on the struct thread * fields. These are
97 * managed by the owning pthread and any tasks associated with them must have
98 * been stopped prior to invoking this function.
100 static void zebra_client_free(struct zserv
*client
)
102 hook_call(zapi_client_close
, client
);
105 * Ensure these have been nulled. This does not equate to the
106 * associated task(s) being scheduled or unscheduled on the client
107 * pthread's threadmaster.
109 assert(!client
->t_read
);
110 assert(!client
->t_write
);
113 * Ensure these have been nulled. This does not equate to the
114 * associated task(s) being scheduled or unscheduled on the client
115 * pthread's threadmaster.
117 assert(!client
->t_read
);
118 assert(!client
->t_write
);
120 /* Close file descriptor. */
122 unsigned long nroutes
;
125 nroutes
= rib_score_proto(client
->proto
, client
->instance
);
127 "client %d disconnected. %lu %s routes removed from the rib",
128 client
->sock
, nroutes
,
129 zebra_route_string(client
->proto
));
133 /* Free stream buffers. */
134 if (client
->ibuf_work
)
135 stream_free(client
->ibuf_work
);
136 if (client
->obuf_work
)
137 stream_free(client
->obuf_work
);
138 if (client
->ibuf_fifo
)
139 stream_fifo_free(client
->ibuf_fifo
);
140 if (client
->obuf_fifo
)
141 stream_fifo_free(client
->obuf_fifo
);
143 buffer_free(client
->wb
);
145 /* Free buffer mutexes */
146 pthread_mutex_destroy(&client
->obuf_mtx
);
147 pthread_mutex_destroy(&client
->ibuf_mtx
);
150 for (afi_t afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
151 for (int i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
152 vrf_bitmap_free(client
->redist
[afi
][i
]);
154 vrf_bitmap_free(client
->redist_default
);
155 vrf_bitmap_free(client
->ifinfo
);
156 vrf_bitmap_free(client
->ridinfo
);
158 XFREE(MTYPE_TMP
, client
);
162 * Finish closing a client.
164 * This task is scheduled by a ZAPI client pthread on the main pthread when it
165 * wants to stop itself. When this executes, the client connection should
166 * already have been closed. This task's responsibility is to gracefully
167 * terminate the client thread, update relevant internal datastructures and
168 * free any resources allocated by the main thread.
170 static int zebra_client_handle_close(struct thread
*thread
)
172 struct zserv
*client
= THREAD_ARG(thread
);
173 frr_pthread_stop(client
->pthread
, NULL
);
174 listnode_delete(zebrad
.client_list
, client
);
175 zebra_client_free(client
);
180 * Gracefully shut down a client connection.
182 * Cancel any pending tasks for the client's thread. Then schedule a task on the
183 * main thread to shut down the calling thread.
185 * Must be called from the client pthread, never the main thread.
187 static void zebra_client_close(struct zserv
*client
)
189 THREAD_OFF(client
->t_read
);
190 THREAD_OFF(client
->t_write
);
191 thread_add_event(zebrad
.master
, zebra_client_handle_close
, client
, 0,
195 /* Make new client. */
196 static void zebra_client_create(int sock
)
198 struct zserv
*client
;
202 client
= XCALLOC(MTYPE_TMP
, sizeof(struct zserv
));
204 /* Make client input/output buffer. */
206 client
->ibuf_fifo
= stream_fifo_new();
207 client
->obuf_fifo
= stream_fifo_new();
208 client
->ibuf_work
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
209 client
->obuf_work
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
210 pthread_mutex_init(&client
->ibuf_mtx
, NULL
);
211 pthread_mutex_init(&client
->obuf_mtx
, NULL
);
212 client
->wb
= buffer_new(0);
214 /* Set table number. */
215 client
->rtm_table
= zebrad
.rtm_table_default
;
217 client
->connect_time
= monotime(NULL
);
218 /* Initialize flags */
219 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
220 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
221 client
->redist
[afi
][i
] = vrf_bitmap_init();
222 client
->redist_default
= vrf_bitmap_init();
223 client
->ifinfo
= vrf_bitmap_init();
224 client
->ridinfo
= vrf_bitmap_init();
226 /* by default, it's not a synchronous client */
227 client
->is_synchronous
= 0;
229 /* Add this client to linked list. */
230 listnode_add(zebrad
.client_list
, client
);
232 struct frr_pthread_attr zclient_pthr_attrs
= {
233 .id
= frr_pthread_get_id(),
234 .start
= frr_pthread_attr_default
.start
,
235 .stop
= frr_pthread_attr_default
.stop
237 client
->pthread
= frr_pthread_new(&zclient_pthr_attrs
, "Zebra API client thread");
239 zebra_vrf_update_all(client
);
241 /* start read loop */
242 zebra_event(client
, ZEBRA_READ
);
245 hook_call(zapi_client_connect
, client
);
248 frr_pthread_run(client
->pthread
, NULL
);
252 * Log zapi message to zlog.
263 static void zserv_log_message(const char *errmsg
, struct stream
*msg
,
266 zlog_debug("Rx'd ZAPI message");
268 zlog_debug("%s", errmsg
);
270 zlog_debug(" Length: %d", hdr
->length
);
271 zlog_debug("Command: %s", zserv_command_string(hdr
->command
));
272 zlog_debug(" VRF: %u", hdr
->vrf_id
);
274 zlog_hexdump(msg
->data
, STREAM_READABLE(msg
));
277 static int zserv_flush_data(struct thread
*thread
)
279 struct zserv
*client
= THREAD_ARG(thread
);
281 client
->t_write
= NULL
;
282 switch (buffer_flush_available(client
->wb
, client
->sock
)) {
285 "%s: buffer_flush_available failed on zserv client fd %d, closing",
286 __func__
, client
->sock
);
287 zebra_client_close(client
);
291 client
->t_write
= NULL
;
292 thread_add_write(client
->pthread
->master
, zserv_flush_data
, client
,
293 client
->sock
, &client
->t_write
);
300 client
->last_write_time
= monotime(NULL
);
305 * Write a single packet.
307 static int zserv_write(struct thread
*thread
)
309 struct zserv
*client
= THREAD_ARG(thread
);
313 if (client
->is_synchronous
)
316 pthread_mutex_lock(&client
->obuf_mtx
);
318 msg
= stream_fifo_pop(client
->obuf_fifo
);
320 pthread_mutex_unlock(&client
->obuf_mtx
);
322 stream_set_getp(msg
, 0);
323 client
->last_write_cmd
= stream_getw_from(msg
, 6);
325 writerv
= buffer_write(client
->wb
, client
->sock
, STREAM_DATA(msg
),
326 stream_get_endp(msg
));
332 zlog_warn("%s: buffer_write failed to ZAPI client %s [fd = %d]",
333 __func__
, zebra_route_string(client
->proto
),
335 zlog_warn("%s: closing connection to %s", __func__
,
336 zebra_route_string(client
->proto
));
337 zebra_client_close(client
);
340 thread_add_write(client
->pthread
->master
, zserv_flush_data
,
341 client
, client
->sock
, &client
->t_write
);
347 pthread_mutex_lock(&client
->obuf_mtx
);
349 if (client
->obuf_fifo
->count
)
350 zebra_event(client
, ZEBRA_WRITE
);
352 pthread_mutex_unlock(&client
->obuf_mtx
);
354 client
->last_write_time
= monotime(NULL
);
358 #if defined(HANDLE_ZAPI_FUZZING)
359 static void zserv_write_incoming(struct stream
*orig
, uint16_t command
)
361 char fname
[MAXPATHLEN
];
365 copy
= stream_dup(orig
);
366 stream_set_getp(copy
, 0);
368 zserv_privs
.change(ZPRIVS_RAISE
);
369 snprintf(fname
, MAXPATHLEN
, "%s/%u", DAEMON_VTY_DIR
, command
);
370 fd
= open(fname
, O_CREAT
| O_WRONLY
| O_EXCL
, 0644);
371 stream_flush(copy
, fd
);
373 zserv_privs
.change(ZPRIVS_LOWER
);
379 * Read and process messages from a client.
381 * This task runs on the main pthread. It is scheduled by client pthreads when
382 * they have new messages available on their input queues. The client is passed
383 * as the task argument.
385 * Each message is popped off the client's input queue and the action associated
386 * with the message is executed. This proceeds until there are no more messages,
387 * an error occurs, or the processing limit is reached. In the last case, this
388 * task reschedules itself.
390 static int zserv_process_messages(struct thread
*thread
)
392 struct zserv
*client
= THREAD_ARG(thread
);
393 struct zebra_vrf
*zvrf
;
398 int p2p
= zebrad
.packets_to_process
;
401 pthread_mutex_lock(&client
->ibuf_mtx
);
403 msg
= stream_fifo_pop(client
->ibuf_fifo
);
405 pthread_mutex_unlock(&client
->ibuf_mtx
);
407 /* break if out of messages */
411 /* read & check header */
412 hdrvalid
= zapi_parse_header(msg
, &hdr
);
413 if (!hdrvalid
&& IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
) {
414 const char *emsg
= "Message has corrupt header";
415 zserv_log_message(emsg
, msg
, NULL
);
420 hdr
.length
-= ZEBRA_HEADER_SIZE
;
422 zvrf
= zebra_vrf_lookup_by_id(hdr
.vrf_id
);
423 if (!zvrf
&& IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
) {
424 const char *emsg
= "Message specifies unknown VRF";
425 zserv_log_message(emsg
, msg
, &hdr
);
430 /* process commands */
431 zserv_handle_commands(client
, &hdr
, msg
, zvrf
);
433 } while (msg
&& --p2p
);
435 /* reschedule self if necessary */
436 pthread_mutex_lock(&client
->ibuf_mtx
);
438 if (client
->ibuf_fifo
->count
)
439 thread_add_event(zebrad
.master
, &zserv_process_messages
,
442 pthread_mutex_unlock(&client
->ibuf_mtx
);
448 * Read and process data from a client socket.
450 * The responsibilities here are to read raw data from the client socket,
451 * validate the header, encapsulate it into a single stream object, push it
452 * onto the input queue and then notify the main thread that there is new data
455 * This function first looks for any data in the client structure's working
456 * input buffer. If data is present, it is assumed that reading stopped in a
457 * previous invocation of this task and needs to be resumed to finish a message.
458 * Otherwise, the socket data stream is assumed to be at the beginning of a new
459 * ZAPI message (specifically at the header). The header is read and validated.
460 * If the header passed validation then the length field found in the header is
461 * used to compute the total length of the message. That much data is read (but
462 * not inspected), appended to the header, placed into a stream and pushed onto
463 * the client's input queue. A task is then scheduled on the main thread to
464 * process the client's input queue. Finally, if all of this was successful,
465 * this task reschedules itself.
467 * Any failure in any of these actions is handled by terminating the client.
469 static int zserv_read(struct thread
*thread
)
472 struct zserv
*client
;
474 #if defined(HANDLE_ZAPI_FUZZING)
477 int p2p
= zebrad
.packets_to_process
;
479 sock
= THREAD_FD(thread
);
480 client
= THREAD_ARG(thread
);
488 already
= stream_get_endp(client
->ibuf_work
);
490 /* Read length and command (if we don't have it already). */
491 if (already
< ZEBRA_HEADER_SIZE
) {
492 nb
= stream_read_try(client
->ibuf_work
, sock
,
493 ZEBRA_HEADER_SIZE
- already
);
494 if ((nb
== 0 || nb
== -1) && IS_ZEBRA_DEBUG_EVENT
)
495 zlog_debug("connection closed socket [%d]",
497 if ((nb
== 0 || nb
== -1))
499 if (nb
!= (ssize_t
)(ZEBRA_HEADER_SIZE
- already
)) {
500 /* Try again later. */
503 already
= ZEBRA_HEADER_SIZE
;
506 /* Reset to read from the beginning of the incoming packet. */
507 stream_set_getp(client
->ibuf_work
, 0);
509 /* Fetch header values */
510 hdrvalid
= zapi_parse_header(client
->ibuf_work
, &hdr
);
513 snprintf(errmsg
, sizeof(errmsg
),
514 "%s: Message has corrupt header", __func__
);
515 zserv_log_message(errmsg
, client
->ibuf_work
, NULL
);
519 /* Validate header */
520 if (hdr
.marker
!= ZEBRA_HEADER_MARKER
521 || hdr
.version
!= ZSERV_VERSION
) {
523 errmsg
, sizeof(errmsg
),
524 "Message has corrupt header\n%s: socket %d version mismatch, marker %d, version %d",
525 __func__
, sock
, hdr
.marker
, hdr
.version
);
526 zserv_log_message(errmsg
, client
->ibuf_work
, &hdr
);
529 if (hdr
.length
< ZEBRA_HEADER_SIZE
) {
531 errmsg
, sizeof(errmsg
),
532 "Message has corrupt header\n%s: socket %d message length %u is less than header size %d",
533 __func__
, sock
, hdr
.length
, ZEBRA_HEADER_SIZE
);
534 zserv_log_message(errmsg
, client
->ibuf_work
, &hdr
);
537 if (hdr
.length
> STREAM_SIZE(client
->ibuf_work
)) {
539 errmsg
, sizeof(errmsg
),
540 "Message has corrupt header\n%s: socket %d message length %u exceeds buffer size %lu",
541 __func__
, sock
, hdr
.length
,
542 (unsigned long)STREAM_SIZE(client
->ibuf_work
));
546 /* Read rest of data. */
547 if (already
< hdr
.length
) {
548 nb
= stream_read_try(client
->ibuf_work
, sock
,
549 hdr
.length
- already
);
550 if ((nb
== 0 || nb
== -1) && IS_ZEBRA_DEBUG_EVENT
)
552 "connection closed [%d] when reading zebra data",
554 if ((nb
== 0 || nb
== -1))
556 if (nb
!= (ssize_t
)(hdr
.length
- already
)) {
557 /* Try again later. */
562 #if defined(HANDLE_ZAPI_FUZZING)
563 zserv_write_incoming(client
->ibuf_work
, command
);
566 /* Debug packet information. */
567 if (IS_ZEBRA_DEBUG_EVENT
)
568 zlog_debug("zebra message comes from socket [%d]",
571 if (IS_ZEBRA_DEBUG_PACKET
&& IS_ZEBRA_DEBUG_RECV
)
572 zserv_log_message(NULL
, client
->ibuf_work
, &hdr
);
574 client
->last_read_time
= monotime(NULL
);
575 client
->last_read_cmd
= hdr
.command
;
577 stream_set_getp(client
->ibuf_work
, 0);
578 struct stream
*msg
= stream_dup(client
->ibuf_work
);
580 pthread_mutex_lock(&client
->ibuf_mtx
);
582 stream_fifo_push(client
->ibuf_fifo
, msg
);
584 pthread_mutex_unlock(&client
->ibuf_mtx
);
586 stream_reset(client
->ibuf_work
);
589 if (IS_ZEBRA_DEBUG_PACKET
)
590 zlog_debug("Read %d packets",
591 zebrad
.packets_to_process
- p2p
);
593 /* Schedule job to process those packets */
594 thread_add_event(zebrad
.master
, &zserv_process_messages
, client
, 0,
597 /* Reschedule ourselves */
598 zebra_event(client
, ZEBRA_READ
);
603 zebra_client_close(client
);
607 static void zebra_event(struct zserv
*client
, enum event event
)
611 thread_add_read(client
->pthread
->master
, zserv_read
, client
,
612 client
->sock
, &client
->t_read
);
615 thread_add_write(client
->pthread
->master
, zserv_write
, client
,
616 client
->sock
, &client
->t_write
);
621 /* Main thread lifecycle ----------------------------------------------------*/
623 /* Accept code of zebra server socket. */
624 static int zebra_accept(struct thread
*thread
)
628 struct sockaddr_in client
;
631 accept_sock
= THREAD_FD(thread
);
633 /* Reregister myself. */
634 thread_add_read(zebrad
.master
, zebra_accept
, NULL
, accept_sock
, NULL
);
636 len
= sizeof(struct sockaddr_in
);
637 client_sock
= accept(accept_sock
, (struct sockaddr
*)&client
, &len
);
639 if (client_sock
< 0) {
640 zlog_warn("Can't accept zebra socket: %s",
641 safe_strerror(errno
));
645 /* Make client socket non-blocking. */
646 set_nonblocking(client_sock
);
648 /* Create new zebra client. */
649 zebra_client_create(client_sock
);
654 /* Make zebra server socket, wiping any existing one (see bug #403). */
655 void zebra_zserv_socket_init(char *path
)
660 struct sockaddr_storage sa
;
663 if (!frr_zclient_addr(&sa
, &sa_len
, path
))
664 /* should be caught in zebra main() */
668 old_mask
= umask(0077);
670 /* Make UNIX domain socket. */
671 sock
= socket(sa
.ss_family
, SOCK_STREAM
, 0);
673 zlog_warn("Can't create zserv socket: %s",
674 safe_strerror(errno
));
676 "zebra can't provide full functionality due to above error");
680 if (sa
.ss_family
!= AF_UNIX
) {
681 sockopt_reuseaddr(sock
);
682 sockopt_reuseport(sock
);
684 struct sockaddr_un
*suna
= (struct sockaddr_un
*)&sa
;
685 if (suna
->sun_path
[0])
686 unlink(suna
->sun_path
);
689 zserv_privs
.change(ZPRIVS_RAISE
);
690 setsockopt_so_recvbuf(sock
, 1048576);
691 setsockopt_so_sendbuf(sock
, 1048576);
692 zserv_privs
.change(ZPRIVS_LOWER
);
694 if (sa
.ss_family
!= AF_UNIX
&& zserv_privs
.change(ZPRIVS_RAISE
))
695 zlog_err("Can't raise privileges");
697 ret
= bind(sock
, (struct sockaddr
*)&sa
, sa_len
);
699 zlog_warn("Can't bind zserv socket on %s: %s", path
,
700 safe_strerror(errno
));
702 "zebra can't provide full functionality due to above error");
706 if (sa
.ss_family
!= AF_UNIX
&& zserv_privs
.change(ZPRIVS_LOWER
))
707 zlog_err("Can't lower privileges");
709 ret
= listen(sock
, 5);
711 zlog_warn("Can't listen to zserv socket %s: %s", path
,
712 safe_strerror(errno
));
714 "zebra can't provide full functionality due to above error");
721 thread_add_read(zebrad
.master
, zebra_accept
, NULL
, sock
, NULL
);
724 #define ZEBRA_TIME_BUF 32
725 static char *zserv_time_buf(time_t *time1
, char *buf
, int buflen
)
731 assert(buflen
>= ZEBRA_TIME_BUF
);
732 assert(time1
!= NULL
);
735 snprintf(buf
, buflen
, "never ");
739 now
= monotime(NULL
);
743 if (now
< ONE_DAY_SECOND
)
744 snprintf(buf
, buflen
, "%02d:%02d:%02d", tm
->tm_hour
, tm
->tm_min
,
746 else if (now
< ONE_WEEK_SECOND
)
747 snprintf(buf
, buflen
, "%dd%02dh%02dm", tm
->tm_yday
, tm
->tm_hour
,
750 snprintf(buf
, buflen
, "%02dw%dd%02dh", tm
->tm_yday
/ 7,
751 tm
->tm_yday
- ((tm
->tm_yday
/ 7) * 7), tm
->tm_hour
);
755 static void zebra_show_client_detail(struct vty
*vty
, struct zserv
*client
)
757 char cbuf
[ZEBRA_TIME_BUF
], rbuf
[ZEBRA_TIME_BUF
];
758 char wbuf
[ZEBRA_TIME_BUF
], nhbuf
[ZEBRA_TIME_BUF
], mbuf
[ZEBRA_TIME_BUF
];
760 vty_out(vty
, "Client: %s", zebra_route_string(client
->proto
));
761 if (client
->instance
)
762 vty_out(vty
, " Instance: %d", client
->instance
);
765 vty_out(vty
, "------------------------ \n");
766 vty_out(vty
, "FD: %d \n", client
->sock
);
767 vty_out(vty
, "Route Table ID: %d \n", client
->rtm_table
);
769 vty_out(vty
, "Connect Time: %s \n",
770 zserv_time_buf(&client
->connect_time
, cbuf
, ZEBRA_TIME_BUF
));
771 if (client
->nh_reg_time
) {
772 vty_out(vty
, "Nexthop Registry Time: %s \n",
773 zserv_time_buf(&client
->nh_reg_time
, nhbuf
,
775 if (client
->nh_last_upd_time
)
776 vty_out(vty
, "Nexthop Last Update Time: %s \n",
777 zserv_time_buf(&client
->nh_last_upd_time
, mbuf
,
780 vty_out(vty
, "No Nexthop Update sent\n");
782 vty_out(vty
, "Not registered for Nexthop Updates\n");
784 vty_out(vty
, "Last Msg Rx Time: %s \n",
785 zserv_time_buf(&client
->last_read_time
, rbuf
, ZEBRA_TIME_BUF
));
786 vty_out(vty
, "Last Msg Tx Time: %s \n",
787 zserv_time_buf(&client
->last_write_time
, wbuf
, ZEBRA_TIME_BUF
));
788 if (client
->last_read_time
)
789 vty_out(vty
, "Last Rcvd Cmd: %s \n",
790 zserv_command_string(client
->last_read_cmd
));
791 if (client
->last_write_time
)
792 vty_out(vty
, "Last Sent Cmd: %s \n",
793 zserv_command_string(client
->last_write_cmd
));
796 vty_out(vty
, "Type Add Update Del \n");
797 vty_out(vty
, "================================================== \n");
798 vty_out(vty
, "IPv4 %-12d%-12d%-12d\n", client
->v4_route_add_cnt
,
799 client
->v4_route_upd8_cnt
, client
->v4_route_del_cnt
);
800 vty_out(vty
, "IPv6 %-12d%-12d%-12d\n", client
->v6_route_add_cnt
,
801 client
->v6_route_upd8_cnt
, client
->v6_route_del_cnt
);
802 vty_out(vty
, "Redist:v4 %-12d%-12d%-12d\n", client
->redist_v4_add_cnt
,
803 0, client
->redist_v4_del_cnt
);
804 vty_out(vty
, "Redist:v6 %-12d%-12d%-12d\n", client
->redist_v6_add_cnt
,
805 0, client
->redist_v6_del_cnt
);
806 vty_out(vty
, "Connected %-12d%-12d%-12d\n", client
->ifadd_cnt
, 0,
808 vty_out(vty
, "BFD peer %-12d%-12d%-12d\n", client
->bfd_peer_add_cnt
,
809 client
->bfd_peer_upd8_cnt
, client
->bfd_peer_del_cnt
);
810 vty_out(vty
, "Interface Up Notifications: %d\n", client
->ifup_cnt
);
811 vty_out(vty
, "Interface Down Notifications: %d\n", client
->ifdown_cnt
);
812 vty_out(vty
, "VNI add notifications: %d\n", client
->vniadd_cnt
);
813 vty_out(vty
, "VNI delete notifications: %d\n", client
->vnidel_cnt
);
814 vty_out(vty
, "L3-VNI add notifications: %d\n", client
->l3vniadd_cnt
);
815 vty_out(vty
, "L3-VNI delete notifications: %d\n", client
->l3vnidel_cnt
);
816 vty_out(vty
, "MAC-IP add notifications: %d\n", client
->macipadd_cnt
);
817 vty_out(vty
, "MAC-IP delete notifications: %d\n", client
->macipdel_cnt
);
823 static void zebra_show_client_brief(struct vty
*vty
, struct zserv
*client
)
825 char cbuf
[ZEBRA_TIME_BUF
], rbuf
[ZEBRA_TIME_BUF
];
826 char wbuf
[ZEBRA_TIME_BUF
];
828 vty_out(vty
, "%-8s%12s %12s%12s%8d/%-8d%8d/%-8d\n",
829 zebra_route_string(client
->proto
),
830 zserv_time_buf(&client
->connect_time
, cbuf
, ZEBRA_TIME_BUF
),
831 zserv_time_buf(&client
->last_read_time
, rbuf
, ZEBRA_TIME_BUF
),
832 zserv_time_buf(&client
->last_write_time
, wbuf
, ZEBRA_TIME_BUF
),
833 client
->v4_route_add_cnt
+ client
->v4_route_upd8_cnt
,
834 client
->v4_route_del_cnt
,
835 client
->v6_route_add_cnt
+ client
->v6_route_upd8_cnt
,
836 client
->v6_route_del_cnt
);
839 struct zserv
*zebra_find_client(uint8_t proto
, unsigned short instance
)
841 struct listnode
*node
, *nnode
;
842 struct zserv
*client
;
844 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
)) {
845 if (client
->proto
== proto
&& client
->instance
== instance
)
852 /* This command is for debugging purpose. */
853 DEFUN (show_zebra_client
,
854 show_zebra_client_cmd
,
858 "Client information\n")
860 struct listnode
*node
;
861 struct zserv
*client
;
863 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
))
864 zebra_show_client_detail(vty
, client
);
869 /* This command is for debugging purpose. */
870 DEFUN (show_zebra_client_summary
,
871 show_zebra_client_summary_cmd
,
872 "show zebra client summary",
875 "Client information brief\n"
878 struct listnode
*node
;
879 struct zserv
*client
;
882 "Name Connect Time Last Read Last Write IPv4 Routes IPv6 Routes \n");
884 "--------------------------------------------------------------------------------\n");
886 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
))
887 zebra_show_client_brief(vty
, client
);
889 vty_out(vty
, "Routes column shows (added+updated)/deleted\n");
893 #if defined(HANDLE_ZAPI_FUZZING)
894 void zserv_read_file(char *input
)
897 struct zserv
*client
= NULL
;
900 zebra_client_create(-1);
901 client
= zebrad
.client_list
->head
->data
;
904 fd
= open(input
, O_RDONLY
| O_NONBLOCK
);
907 zebra_client_read(&t
);
913 void zserv_init(void)
915 /* Client list init. */
916 zebrad
.client_list
= list_new();
917 zebrad
.client_list
->del
= (void (*)(void *))zebra_client_free
;
919 install_element(ENABLE_NODE
, &show_zebra_client_cmd
);
920 install_element(ENABLE_NODE
, &show_zebra_client_summary_cmd
);