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