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