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