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