]>
Commit | Line | Data |
---|---|---|
bf094f69 QY |
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. | |
718e3744 | 7 | * |
bf094f69 QY |
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. | |
718e3744 | 12 | * |
bf094f69 QY |
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. | |
718e3744 | 17 | * |
896014f4 DL |
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 | |
718e3744 | 21 | */ |
22 | ||
23 | #include <zebra.h> | |
d8647095 QY |
24 | |
25 | /* clang-format off */ | |
bf094f69 QY |
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... */ | |
453844ab | 38 | #include "lib/hook.h" /* for DEFINE_HOOK, DEFINE_KOOH, hook_call */ |
bf094f69 QY |
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... */ | |
52f6868d | 55 | #include "lib/frr_pthread.h" /* for frr_pthread_new, frr_pthread_stop... */ |
ae6670d0 | 56 | #include "lib/frratomic.h" /* for atomic_load_explicit, atomic_stor... */ |
174482ef | 57 | #include "lib/lib_errors.h" /* for generic ferr ids */ |
bf094f69 QY |
58 | |
59 | #include "zebra/debug.h" /* for various debugging macros */ | |
bf094f69 | 60 | #include "zebra/rib.h" /* for rib_score_proto */ |
bf094f69 | 61 | #include "zebra/zapi_msg.h" /* for zserv_handle_commands */ |
bf094f69 | 62 | #include "zebra/zebra_vrf.h" /* for zebra_vrf_lookup_by_id, zvrf */ |
d8647095 | 63 | #include "zebra/zserv.h" /* for zserv */ |
3801e764 | 64 | #include "zebra/zebra_router.h" |
364fed6b | 65 | #include "zebra/zebra_errors.h" /* for error messages */ |
d8647095 | 66 | /* clang-format on */ |
6b0655a2 | 67 | |
1002497a | 68 | /* privileges */ |
edd7c245 | 69 | extern struct zebra_privs_t zserv_privs; |
453844ab | 70 | |
cc3d8834 DS |
71 | /* The listener socket for clients connecting to us */ |
72 | static int zsock; | |
73 | ||
329e35da | 74 | /* |
f2efe6a3 | 75 | * Client thread events. |
329e35da | 76 | * |
f2efe6a3 | 77 | * These are used almost exclusively by client threads to drive their own event |
24f8f979 | 78 | * loops. The only exception is in zserv_client_create(), which pushes an |
21ccc0cf | 79 | * initial ZSERV_CLIENT_READ event to start the API handler loop. |
329e35da | 80 | */ |
21ccc0cf QY |
81 | enum zserv_client_event { |
82 | /* Schedule a socket read */ | |
83 | ZSERV_CLIENT_READ, | |
84 | /* Schedule a buffer write */ | |
85 | ZSERV_CLIENT_WRITE, | |
21ccc0cf | 86 | }; |
453844ab | 87 | |
21ccc0cf QY |
88 | /* |
89 | * Main thread events. | |
90 | * | |
91 | * These are used by client threads to notify the main thread about various | |
92 | * events and to make processing requests. | |
93 | */ | |
94 | enum zserv_event { | |
95 | /* Schedule listen job on Zebra API socket */ | |
96 | ZSERV_ACCEPT, | |
97 | /* The calling client has packets on its input buffer */ | |
98 | ZSERV_PROCESS_MESSAGES, | |
99 | /* The calling client wishes to be killed */ | |
f3e33b69 | 100 | ZSERV_HANDLE_CLIENT_FAIL, |
21ccc0cf QY |
101 | }; |
102 | ||
103 | /* | |
104 | * Zebra server event driver for all client threads. | |
105 | * | |
106 | * This is essentially a wrapper around thread_add_event() that centralizes | |
107 | * those scheduling calls into one place. | |
108 | * | |
109 | * All calls to this function schedule an event on the pthread running the | |
110 | * provided client. | |
111 | * | |
112 | * client | |
113 | * the client in question, and thread target | |
114 | * | |
115 | * event | |
116 | * the event to notify them about | |
117 | */ | |
118 | static void zserv_client_event(struct zserv *client, | |
119 | enum zserv_client_event event); | |
120 | ||
121 | /* | |
122 | * Zebra server event driver for the main thread. | |
123 | * | |
124 | * This is essentially a wrapper around thread_add_event() that centralizes | |
125 | * those scheduling calls into one place. | |
126 | * | |
127 | * All calls to this function schedule an event on Zebra's main pthread. | |
128 | * | |
129 | * client | |
130 | * the client in question | |
131 | * | |
132 | * event | |
133 | * the event to notify the main thread about | |
134 | */ | |
135 | static void zserv_event(struct zserv *client, enum zserv_event event); | |
e16abbb3 | 136 | |
e16abbb3 | 137 | |
f2efe6a3 | 138 | /* Client thread lifecycle -------------------------------------------------- */ |
e16abbb3 | 139 | |
9bcbcae2 | 140 | /* |
1002497a QY |
141 | * Log zapi message to zlog. |
142 | * | |
143 | * errmsg (optional) | |
144 | * Debugging message | |
9bcbcae2 | 145 | * |
1002497a QY |
146 | * msg |
147 | * The message | |
148 | * | |
149 | * hdr (optional) | |
150 | * The message header | |
9bcbcae2 | 151 | */ |
49b3b01f QY |
152 | void zserv_log_message(const char *errmsg, struct stream *msg, |
153 | struct zmsghdr *hdr) | |
1002497a QY |
154 | { |
155 | zlog_debug("Rx'd ZAPI message"); | |
156 | if (errmsg) | |
157 | zlog_debug("%s", errmsg); | |
158 | if (hdr) { | |
159 | zlog_debug(" Length: %d", hdr->length); | |
160 | zlog_debug("Command: %s", zserv_command_string(hdr->command)); | |
161 | zlog_debug(" VRF: %u", hdr->vrf_id); | |
162 | } | |
163 | zlog_hexdump(msg->data, STREAM_READABLE(msg)); | |
9bcbcae2 QY |
164 | } |
165 | ||
f2efe6a3 QY |
166 | /* |
167 | * Gracefully shut down a client connection. | |
168 | * | |
f3e33b69 QY |
169 | * Cancel any pending tasks for the client's thread. Then schedule a task on |
170 | * the main thread to shut down the calling thread. | |
f2efe6a3 | 171 | * |
c0226378 QY |
172 | * It is not safe to close the client socket in this function. The socket is |
173 | * owned by the main thread. | |
174 | * | |
f2efe6a3 QY |
175 | * Must be called from the client pthread, never the main thread. |
176 | */ | |
f3e33b69 | 177 | static void zserv_client_fail(struct zserv *client) |
f2efe6a3 | 178 | { |
e914ccbe | 179 | flog_warn(EC_ZEBRA_CLIENT_IO_ERROR, |
9df414fe | 180 | "Client '%s' encountered an error and is shutting down.", |
f3e33b69 QY |
181 | zebra_route_string(client->proto)); |
182 | ||
c2ca5ee6 | 183 | atomic_store_explicit(&client->pthread->running, false, |
f3e33b69 | 184 | memory_order_relaxed); |
c0226378 | 185 | |
f2efe6a3 QY |
186 | THREAD_OFF(client->t_read); |
187 | THREAD_OFF(client->t_write); | |
f3e33b69 | 188 | zserv_event(client, ZSERV_HANDLE_CLIENT_FAIL); |
f2efe6a3 QY |
189 | } |
190 | ||
1002497a | 191 | /* |
370d8dad QY |
192 | * Write all pending messages to client socket. |
193 | * | |
29bed51b QY |
194 | * This function first attempts to flush any buffered data. If unsuccessful, |
195 | * the function reschedules itself and returns. If successful, it pops all | |
196 | * available messages from the output queue and continues to write data | |
197 | * directly to the socket until the socket would block. If the socket never | |
198 | * blocks and all data is written, the function returns without rescheduling | |
199 | * itself. If the socket ends up throwing EWOULDBLOCK, the remaining data is | |
200 | * buffered and the function reschedules itself. | |
370d8dad | 201 | * |
29bed51b QY |
202 | * The utility of the buffer is that it allows us to vastly reduce lock |
203 | * contention by allowing us to pop *all* messages off the output queue at once | |
204 | * instead of locking and unlocking each time we want to pop a single message | |
205 | * off the queue. The same thing could arguably be accomplished faster by | |
206 | * allowing the main thread to write directly into the buffer instead of | |
207 | * enqueuing packets onto an intermediary queue, but the intermediary queue | |
208 | * allows us to expose information about input and output queues to the user in | |
209 | * terms of number of packets rather than size of data. | |
1002497a QY |
210 | */ |
211 | static int zserv_write(struct thread *thread) | |
d62a17ae | 212 | { |
1002497a QY |
213 | struct zserv *client = THREAD_ARG(thread); |
214 | struct stream *msg; | |
ce4f1050 | 215 | uint32_t wcmd = 0; |
29bed51b QY |
216 | struct stream_fifo *cache; |
217 | ||
218 | /* If we have any data pending, try to flush it first */ | |
ccd51bd2 | 219 | switch (buffer_flush_all(client->wb, client->sock)) { |
29bed51b QY |
220 | case BUFFER_ERROR: |
221 | goto zwrite_fail; | |
222 | case BUFFER_PENDING: | |
ccd51bd2 QY |
223 | atomic_store_explicit(&client->last_write_time, |
224 | (uint32_t)monotime(NULL), | |
225 | memory_order_relaxed); | |
29bed51b QY |
226 | zserv_client_event(client, ZSERV_CLIENT_WRITE); |
227 | return 0; | |
228 | case BUFFER_EMPTY: | |
229 | break; | |
230 | } | |
231 | ||
232 | cache = stream_fifo_new(); | |
89f4e507 | 233 | |
329e35da QY |
234 | pthread_mutex_lock(&client->obuf_mtx); |
235 | { | |
c2ca5ee6 | 236 | while (stream_fifo_head(client->obuf_fifo)) |
370d8dad QY |
237 | stream_fifo_push(cache, |
238 | stream_fifo_pop(client->obuf_fifo)); | |
329e35da QY |
239 | } |
240 | pthread_mutex_unlock(&client->obuf_mtx); | |
241 | ||
ccd51bd2 QY |
242 | if (cache->tail) { |
243 | msg = cache->tail; | |
370d8dad | 244 | stream_set_getp(msg, 0); |
370d8dad | 245 | wcmd = stream_getw_from(msg, 6); |
ccd51bd2 | 246 | } |
822167e7 | 247 | |
ccd51bd2 QY |
248 | while (stream_fifo_head(cache)) { |
249 | msg = stream_fifo_pop(cache); | |
250 | buffer_put(client->wb, STREAM_DATA(msg), stream_get_endp(msg)); | |
370d8dad QY |
251 | stream_free(msg); |
252 | } | |
1002497a | 253 | |
822167e7 | 254 | stream_fifo_free(cache); |
1002497a | 255 | |
ccd51bd2 QY |
256 | /* If we have any data pending, try to flush it first */ |
257 | switch (buffer_flush_all(client->wb, client->sock)) { | |
258 | case BUFFER_ERROR: | |
259 | goto zwrite_fail; | |
260 | case BUFFER_PENDING: | |
261 | atomic_store_explicit(&client->last_write_time, | |
262 | (uint32_t)monotime(NULL), | |
263 | memory_order_relaxed); | |
264 | zserv_client_event(client, ZSERV_CLIENT_WRITE); | |
265 | return 0; | |
ccd51bd2 QY |
266 | case BUFFER_EMPTY: |
267 | break; | |
268 | } | |
269 | ||
370d8dad QY |
270 | atomic_store_explicit(&client->last_write_cmd, wcmd, |
271 | memory_order_relaxed); | |
1002497a | 272 | |
52f6868d | 273 | atomic_store_explicit(&client->last_write_time, |
370d8dad | 274 | (uint32_t)monotime(NULL), memory_order_relaxed); |
52f6868d | 275 | |
1002497a | 276 | return 0; |
29bed51b QY |
277 | |
278 | zwrite_fail: | |
e914ccbe | 279 | flog_warn(EC_ZEBRA_CLIENT_WRITE_FAILED, |
9df414fe | 280 | "%s: could not write to %s [fd = %d], closing.", __func__, |
29bed51b | 281 | zebra_route_string(client->proto), client->sock); |
f3e33b69 | 282 | zserv_client_fail(client); |
29bed51b | 283 | return 0; |
0c5e7be5 DS |
284 | } |
285 | ||
329e35da QY |
286 | /* |
287 | * Read and process data from a client socket. | |
288 | * | |
289 | * The responsibilities here are to read raw data from the client socket, | |
290 | * validate the header, encapsulate it into a single stream object, push it | |
291 | * onto the input queue and then notify the main thread that there is new data | |
292 | * available. | |
293 | * | |
294 | * This function first looks for any data in the client structure's working | |
295 | * input buffer. If data is present, it is assumed that reading stopped in a | |
296 | * previous invocation of this task and needs to be resumed to finish a message. | |
297 | * Otherwise, the socket data stream is assumed to be at the beginning of a new | |
298 | * ZAPI message (specifically at the header). The header is read and validated. | |
299 | * If the header passed validation then the length field found in the header is | |
300 | * used to compute the total length of the message. That much data is read (but | |
301 | * not inspected), appended to the header, placed into a stream and pushed onto | |
302 | * the client's input queue. A task is then scheduled on the main thread to | |
303 | * process the client's input queue. Finally, if all of this was successful, | |
304 | * this task reschedules itself. | |
305 | * | |
306 | * Any failure in any of these actions is handled by terminating the client. | |
307 | */ | |
1002497a | 308 | static int zserv_read(struct thread *thread) |
0c5e7be5 | 309 | { |
ae6670d0 | 310 | struct zserv *client = THREAD_ARG(thread); |
0c5e7be5 | 311 | int sock; |
0c5e7be5 | 312 | size_t already; |
ae6670d0 QY |
313 | struct stream_fifo *cache; |
314 | uint32_t p2p_orig; | |
315 | ||
1572d9af QY |
316 | uint32_t p2p; |
317 | struct zmsghdr hdr; | |
318 | ||
5ec5a716 | 319 | p2p_orig = atomic_load_explicit(&zrouter.packets_to_process, |
ae6670d0 QY |
320 | memory_order_relaxed); |
321 | cache = stream_fifo_new(); | |
370d8dad | 322 | p2p = p2p_orig; |
0c5e7be5 | 323 | sock = THREAD_FD(thread); |
0c5e7be5 | 324 | |
43ea2c76 | 325 | while (p2p) { |
107afcd1 QY |
326 | ssize_t nb; |
327 | bool hdrvalid; | |
328 | char errmsg[256]; | |
329 | ||
1002497a QY |
330 | already = stream_get_endp(client->ibuf_work); |
331 | ||
5a762c8a | 332 | /* Read length and command (if we don't have it already). */ |
1002497a QY |
333 | if (already < ZEBRA_HEADER_SIZE) { |
334 | nb = stream_read_try(client->ibuf_work, sock, | |
335 | ZEBRA_HEADER_SIZE - already); | |
03f29018 DS |
336 | if ((nb == 0 || nb == -1)) { |
337 | if (IS_ZEBRA_DEBUG_EVENT) | |
338 | zlog_debug("connection closed socket [%d]", | |
339 | sock); | |
1002497a | 340 | goto zread_fail; |
03f29018 | 341 | } |
1002497a | 342 | if (nb != (ssize_t)(ZEBRA_HEADER_SIZE - already)) { |
5a762c8a | 343 | /* Try again later. */ |
1002497a | 344 | break; |
5a762c8a DS |
345 | } |
346 | already = ZEBRA_HEADER_SIZE; | |
0c5e7be5 | 347 | } |
0c5e7be5 | 348 | |
5a762c8a | 349 | /* Reset to read from the beginning of the incoming packet. */ |
1002497a | 350 | stream_set_getp(client->ibuf_work, 0); |
0c5e7be5 | 351 | |
5a762c8a | 352 | /* Fetch header values */ |
1002497a | 353 | hdrvalid = zapi_parse_header(client->ibuf_work, &hdr); |
0c5e7be5 | 354 | |
1002497a QY |
355 | if (!hdrvalid) { |
356 | snprintf(errmsg, sizeof(errmsg), | |
357 | "%s: Message has corrupt header", __func__); | |
358 | zserv_log_message(errmsg, client->ibuf_work, NULL); | |
359 | goto zread_fail; | |
0c5e7be5 | 360 | } |
1002497a QY |
361 | |
362 | /* Validate header */ | |
363 | if (hdr.marker != ZEBRA_HEADER_MARKER | |
364 | || hdr.version != ZSERV_VERSION) { | |
365 | snprintf( | |
366 | errmsg, sizeof(errmsg), | |
367 | "Message has corrupt header\n%s: socket %d version mismatch, marker %d, version %d", | |
368 | __func__, sock, hdr.marker, hdr.version); | |
369 | zserv_log_message(errmsg, client->ibuf_work, &hdr); | |
370 | goto zread_fail; | |
5a762c8a | 371 | } |
1002497a QY |
372 | if (hdr.length < ZEBRA_HEADER_SIZE) { |
373 | snprintf( | |
374 | errmsg, sizeof(errmsg), | |
375 | "Message has corrupt header\n%s: socket %d message length %u is less than header size %d", | |
376 | __func__, sock, hdr.length, ZEBRA_HEADER_SIZE); | |
377 | zserv_log_message(errmsg, client->ibuf_work, &hdr); | |
378 | goto zread_fail; | |
379 | } | |
380 | if (hdr.length > STREAM_SIZE(client->ibuf_work)) { | |
381 | snprintf( | |
382 | errmsg, sizeof(errmsg), | |
383 | "Message has corrupt header\n%s: socket %d message length %u exceeds buffer size %lu", | |
384 | __func__, sock, hdr.length, | |
385 | (unsigned long)STREAM_SIZE(client->ibuf_work)); | |
1572d9af | 386 | zserv_log_message(errmsg, client->ibuf_work, &hdr); |
1002497a | 387 | goto zread_fail; |
0c5e7be5 | 388 | } |
0c5e7be5 | 389 | |
5a762c8a | 390 | /* Read rest of data. */ |
1002497a QY |
391 | if (already < hdr.length) { |
392 | nb = stream_read_try(client->ibuf_work, sock, | |
393 | hdr.length - already); | |
03f29018 DS |
394 | if ((nb == 0 || nb == -1)) { |
395 | if (IS_ZEBRA_DEBUG_EVENT) | |
396 | zlog_debug( | |
397 | "connection closed [%d] when reading zebra data", | |
398 | sock); | |
1002497a | 399 | goto zread_fail; |
03f29018 | 400 | } |
1002497a | 401 | if (nb != (ssize_t)(hdr.length - already)) { |
5a762c8a | 402 | /* Try again later. */ |
1002497a | 403 | break; |
5a762c8a DS |
404 | } |
405 | } | |
0c5e7be5 | 406 | |
5a762c8a | 407 | /* Debug packet information. */ |
494247b5 DS |
408 | if (IS_ZEBRA_DEBUG_PACKET) |
409 | zlog_debug("zebra message[%s:%u:%u] comes from socket [%d]", | |
410 | zserv_command_string(hdr.command), | |
411 | hdr.vrf_id, hdr.length, | |
996c9314 | 412 | sock); |
0c5e7be5 | 413 | |
1572d9af QY |
414 | stream_set_getp(client->ibuf_work, 0); |
415 | struct stream *msg = stream_dup(client->ibuf_work); | |
416 | ||
417 | stream_fifo_push(cache, msg); | |
418 | stream_reset(client->ibuf_work); | |
43ea2c76 | 419 | p2p--; |
1572d9af QY |
420 | } |
421 | ||
422 | if (p2p < p2p_orig) { | |
423 | /* update session statistics */ | |
52f6868d QY |
424 | atomic_store_explicit(&client->last_read_time, monotime(NULL), |
425 | memory_order_relaxed); | |
426 | atomic_store_explicit(&client->last_read_cmd, hdr.command, | |
427 | memory_order_relaxed); | |
5a762c8a | 428 | |
1572d9af | 429 | /* publish read packets on client's input queue */ |
329e35da QY |
430 | pthread_mutex_lock(&client->ibuf_mtx); |
431 | { | |
1572d9af QY |
432 | while (cache->head) |
433 | stream_fifo_push(client->ibuf_fifo, | |
434 | stream_fifo_pop(cache)); | |
329e35da QY |
435 | } |
436 | pthread_mutex_unlock(&client->ibuf_mtx); | |
822167e7 QY |
437 | |
438 | /* Schedule job to process those packets */ | |
439 | zserv_event(client, ZSERV_PROCESS_MESSAGES); | |
440 | ||
d62a17ae | 441 | } |
442 | ||
1002497a | 443 | if (IS_ZEBRA_DEBUG_PACKET) |
494247b5 DS |
444 | zlog_debug("Read %d packets from client: %s", p2p_orig - p2p, |
445 | zebra_route_string(client->proto)); | |
1002497a | 446 | |
1002497a | 447 | /* Reschedule ourselves */ |
21ccc0cf | 448 | zserv_client_event(client, ZSERV_CLIENT_READ); |
1002497a | 449 | |
1572d9af QY |
450 | stream_fifo_free(cache); |
451 | ||
d62a17ae | 452 | return 0; |
1002497a QY |
453 | |
454 | zread_fail: | |
1572d9af | 455 | stream_fifo_free(cache); |
f3e33b69 | 456 | zserv_client_fail(client); |
1002497a | 457 | return -1; |
718e3744 | 458 | } |
459 | ||
21ccc0cf QY |
460 | static void zserv_client_event(struct zserv *client, |
461 | enum zserv_client_event event) | |
1002497a QY |
462 | { |
463 | switch (event) { | |
21ccc0cf | 464 | case ZSERV_CLIENT_READ: |
329e35da QY |
465 | thread_add_read(client->pthread->master, zserv_read, client, |
466 | client->sock, &client->t_read); | |
1002497a | 467 | break; |
21ccc0cf | 468 | case ZSERV_CLIENT_WRITE: |
329e35da | 469 | thread_add_write(client->pthread->master, zserv_write, client, |
1002497a QY |
470 | client->sock, &client->t_write); |
471 | break; | |
472 | } | |
473 | } | |
718e3744 | 474 | |
f2efe6a3 QY |
475 | /* Main thread lifecycle ---------------------------------------------------- */ |
476 | ||
f2efe6a3 QY |
477 | /* |
478 | * Read and process messages from a client. | |
479 | * | |
480 | * This task runs on the main pthread. It is scheduled by client pthreads when | |
481 | * they have new messages available on their input queues. The client is passed | |
482 | * as the task argument. | |
483 | * | |
484 | * Each message is popped off the client's input queue and the action associated | |
485 | * with the message is executed. This proceeds until there are no more messages, | |
904e0d88 QY |
486 | * an error occurs, or the processing limit is reached. |
487 | * | |
2561d12e | 488 | * The client's I/O thread can push at most zrouter.packets_to_process messages |
822167e7 | 489 | * onto the input buffer before notifying us there are packets to read. As long |
2561d12e | 490 | * as we always process zrouter.packets_to_process messages here, then we can |
822167e7 QY |
491 | * rely on the read thread to handle queuing this task enough times to process |
492 | * everything on the input queue. | |
f2efe6a3 QY |
493 | */ |
494 | static int zserv_process_messages(struct thread *thread) | |
495 | { | |
496 | struct zserv *client = THREAD_ARG(thread); | |
f2efe6a3 | 497 | struct stream *msg; |
904e0d88 | 498 | struct stream_fifo *cache = stream_fifo_new(); |
5ec5a716 | 499 | uint32_t p2p = zrouter.packets_to_process; |
dded2aba | 500 | bool need_resched = false; |
f2efe6a3 | 501 | |
f2efe6a3 QY |
502 | pthread_mutex_lock(&client->ibuf_mtx); |
503 | { | |
822167e7 QY |
504 | uint32_t i; |
505 | for (i = 0; i < p2p && stream_fifo_head(client->ibuf_fifo); | |
506 | ++i) { | |
507 | msg = stream_fifo_pop(client->ibuf_fifo); | |
508 | stream_fifo_push(cache, msg); | |
509 | } | |
904e0d88 | 510 | |
822167e7 | 511 | msg = NULL; |
dded2aba MS |
512 | |
513 | /* Need to reschedule processing work if there are still | |
514 | * packets in the fifo. | |
515 | */ | |
516 | if (stream_fifo_head(client->ibuf_fifo)) | |
517 | need_resched = true; | |
f2efe6a3 QY |
518 | } |
519 | pthread_mutex_unlock(&client->ibuf_mtx); | |
520 | ||
822167e7 | 521 | while (stream_fifo_head(cache)) { |
904e0d88 | 522 | msg = stream_fifo_pop(cache); |
904e0d88 QY |
523 | zserv_handle_commands(client, msg); |
524 | stream_free(msg); | |
525 | } | |
526 | ||
527 | stream_fifo_free(cache); | |
528 | ||
dded2aba MS |
529 | /* Reschedule ourselves if necessary */ |
530 | if (need_resched) | |
531 | zserv_event(client, ZSERV_PROCESS_MESSAGES); | |
532 | ||
f2efe6a3 QY |
533 | return 0; |
534 | } | |
535 | ||
21ccc0cf | 536 | int zserv_send_message(struct zserv *client, struct stream *msg) |
f2efe6a3 QY |
537 | { |
538 | pthread_mutex_lock(&client->obuf_mtx); | |
539 | { | |
540 | stream_fifo_push(client->obuf_fifo, msg); | |
f2efe6a3 QY |
541 | } |
542 | pthread_mutex_unlock(&client->obuf_mtx); | |
ccd51bd2 QY |
543 | |
544 | zserv_client_event(client, ZSERV_CLIENT_WRITE); | |
545 | ||
f2efe6a3 QY |
546 | return 0; |
547 | } | |
548 | ||
549 | ||
550 | /* Hooks for client connect / disconnect */ | |
21ccc0cf QY |
551 | DEFINE_HOOK(zserv_client_connect, (struct zserv *client), (client)); |
552 | DEFINE_KOOH(zserv_client_close, (struct zserv *client), (client)); | |
f2efe6a3 QY |
553 | |
554 | /* | |
555 | * Deinitialize zebra client. | |
556 | * | |
557 | * - Deregister and deinitialize related internal resources | |
558 | * - Gracefully close socket | |
559 | * - Free associated resources | |
560 | * - Free client structure | |
561 | * | |
562 | * This does *not* take any action on the struct thread * fields. These are | |
563 | * managed by the owning pthread and any tasks associated with them must have | |
564 | * been stopped prior to invoking this function. | |
565 | */ | |
21ccc0cf | 566 | static void zserv_client_free(struct zserv *client) |
f2efe6a3 | 567 | { |
21ccc0cf | 568 | hook_call(zserv_client_close, client); |
f2efe6a3 QY |
569 | |
570 | /* Close file descriptor. */ | |
571 | if (client->sock) { | |
572 | unsigned long nroutes; | |
573 | ||
574 | close(client->sock); | |
a580357a | 575 | |
f2efe6a3 QY |
576 | nroutes = rib_score_proto(client->proto, client->instance); |
577 | zlog_notice( | |
578 | "client %d disconnected. %lu %s routes removed from the rib", | |
579 | client->sock, nroutes, | |
580 | zebra_route_string(client->proto)); | |
581 | client->sock = -1; | |
582 | } | |
583 | ||
584 | /* Free stream buffers. */ | |
585 | if (client->ibuf_work) | |
586 | stream_free(client->ibuf_work); | |
587 | if (client->obuf_work) | |
588 | stream_free(client->obuf_work); | |
589 | if (client->ibuf_fifo) | |
590 | stream_fifo_free(client->ibuf_fifo); | |
591 | if (client->obuf_fifo) | |
592 | stream_fifo_free(client->obuf_fifo); | |
593 | if (client->wb) | |
594 | buffer_free(client->wb); | |
595 | ||
596 | /* Free buffer mutexes */ | |
597 | pthread_mutex_destroy(&client->obuf_mtx); | |
598 | pthread_mutex_destroy(&client->ibuf_mtx); | |
599 | ||
600 | /* Free bitmaps. */ | |
49db7a7b | 601 | for (afi_t afi = AFI_IP; afi < AFI_MAX; afi++) { |
f2efe6a3 QY |
602 | for (int i = 0; i < ZEBRA_ROUTE_MAX; i++) |
603 | vrf_bitmap_free(client->redist[afi][i]); | |
604 | ||
49db7a7b RW |
605 | vrf_bitmap_free(client->redist_default[afi]); |
606 | } | |
f2efe6a3 QY |
607 | vrf_bitmap_free(client->ridinfo); |
608 | ||
609 | XFREE(MTYPE_TMP, client); | |
610 | } | |
611 | ||
f3e33b69 | 612 | void zserv_close_client(struct zserv *client) |
f2efe6a3 | 613 | { |
f3e33b69 | 614 | /* synchronously stop and join pthread */ |
f2efe6a3 QY |
615 | frr_pthread_stop(client->pthread, NULL); |
616 | ||
f3e33b69 QY |
617 | if (IS_ZEBRA_DEBUG_EVENT) |
618 | zlog_debug("Closing client '%s'", | |
619 | zebra_route_string(client->proto)); | |
620 | ||
3801e764 | 621 | thread_cancel_event(zrouter.master, client); |
f3e33b69 | 622 | THREAD_OFF(client->t_cleanup); |
dded2aba | 623 | THREAD_OFF(client->t_process); |
f3e33b69 QY |
624 | |
625 | /* destroy pthread */ | |
f2efe6a3 QY |
626 | frr_pthread_destroy(client->pthread); |
627 | client->pthread = NULL; | |
628 | ||
f3e33b69 | 629 | /* remove from client list */ |
161e9ab7 | 630 | listnode_delete(zrouter.client_list, client); |
f3e33b69 QY |
631 | |
632 | /* delete client */ | |
21ccc0cf | 633 | zserv_client_free(client); |
f3e33b69 QY |
634 | } |
635 | ||
636 | /* | |
637 | * This task is scheduled by a ZAPI client pthread on the main pthread when it | |
638 | * wants to stop itself. When this executes, the client connection should | |
639 | * already have been closed and the thread will most likely have died, but its | |
640 | * resources still need to be cleaned up. | |
641 | */ | |
642 | static int zserv_handle_client_fail(struct thread *thread) | |
643 | { | |
644 | struct zserv *client = THREAD_ARG(thread); | |
645 | ||
646 | zserv_close_client(client); | |
f2efe6a3 QY |
647 | return 0; |
648 | } | |
649 | ||
650 | /* | |
651 | * Create a new client. | |
652 | * | |
653 | * This is called when a new connection is accept()'d on the ZAPI socket. It | |
654 | * initializes new client structure, notifies any subscribers of the connection | |
655 | * event and spawns the client's thread. | |
656 | * | |
657 | * sock | |
658 | * client's socket file descriptor | |
659 | */ | |
2875801f | 660 | static struct zserv *zserv_client_create(int sock) |
f2efe6a3 QY |
661 | { |
662 | struct zserv *client; | |
f3f45626 DS |
663 | size_t stream_size = |
664 | MAX(ZEBRA_MAX_PACKET_SIZ, sizeof(struct zapi_route)); | |
f2efe6a3 QY |
665 | int i; |
666 | afi_t afi; | |
667 | ||
668 | client = XCALLOC(MTYPE_TMP, sizeof(struct zserv)); | |
669 | ||
670 | /* Make client input/output buffer. */ | |
671 | client->sock = sock; | |
672 | client->ibuf_fifo = stream_fifo_new(); | |
673 | client->obuf_fifo = stream_fifo_new(); | |
f3f45626 DS |
674 | client->ibuf_work = stream_new(stream_size); |
675 | client->obuf_work = stream_new(stream_size); | |
f2efe6a3 QY |
676 | pthread_mutex_init(&client->ibuf_mtx, NULL); |
677 | pthread_mutex_init(&client->obuf_mtx, NULL); | |
678 | client->wb = buffer_new(0); | |
679 | ||
f2efe6a3 QY |
680 | atomic_store_explicit(&client->connect_time, (uint32_t) monotime(NULL), |
681 | memory_order_relaxed); | |
682 | ||
683 | /* Initialize flags */ | |
49db7a7b | 684 | for (afi = AFI_IP; afi < AFI_MAX; afi++) { |
f2efe6a3 QY |
685 | for (i = 0; i < ZEBRA_ROUTE_MAX; i++) |
686 | client->redist[afi][i] = vrf_bitmap_init(); | |
49db7a7b RW |
687 | client->redist_default[afi] = vrf_bitmap_init(); |
688 | } | |
f2efe6a3 QY |
689 | client->ridinfo = vrf_bitmap_init(); |
690 | ||
f2efe6a3 | 691 | /* Add this client to linked list. */ |
161e9ab7 | 692 | listnode_add(zrouter.client_list, client); |
f2efe6a3 QY |
693 | |
694 | struct frr_pthread_attr zclient_pthr_attrs = { | |
f2efe6a3 QY |
695 | .start = frr_pthread_attr_default.start, |
696 | .stop = frr_pthread_attr_default.stop | |
697 | }; | |
698 | client->pthread = | |
57019528 CS |
699 | frr_pthread_new(&zclient_pthr_attrs, "Zebra API client thread", |
700 | "zebra_apic"); | |
f2efe6a3 | 701 | |
f2efe6a3 | 702 | /* start read loop */ |
21ccc0cf | 703 | zserv_client_event(client, ZSERV_CLIENT_READ); |
f2efe6a3 QY |
704 | |
705 | /* call callbacks */ | |
21ccc0cf | 706 | hook_call(zserv_client_connect, client); |
f2efe6a3 QY |
707 | |
708 | /* start pthread */ | |
709 | frr_pthread_run(client->pthread, NULL); | |
2875801f QY |
710 | |
711 | return client; | |
f2efe6a3 | 712 | } |
329e35da | 713 | |
21ccc0cf QY |
714 | /* |
715 | * Accept socket connection. | |
716 | */ | |
717 | static int zserv_accept(struct thread *thread) | |
718e3744 | 718 | { |
d62a17ae | 719 | int accept_sock; |
720 | int client_sock; | |
721 | struct sockaddr_in client; | |
722 | socklen_t len; | |
723 | ||
724 | accept_sock = THREAD_FD(thread); | |
718e3744 | 725 | |
d62a17ae | 726 | /* Reregister myself. */ |
21ccc0cf | 727 | zserv_event(NULL, ZSERV_ACCEPT); |
718e3744 | 728 | |
d62a17ae | 729 | len = sizeof(struct sockaddr_in); |
730 | client_sock = accept(accept_sock, (struct sockaddr *)&client, &len); | |
719e9741 | 731 | |
d62a17ae | 732 | if (client_sock < 0) { |
450971aa | 733 | flog_err_sys(EC_LIB_SOCKET, "Can't accept zebra socket: %s", |
9df414fe | 734 | safe_strerror(errno)); |
d62a17ae | 735 | return -1; |
736 | } | |
718e3744 | 737 | |
d62a17ae | 738 | /* Make client socket non-blocking. */ |
739 | set_nonblocking(client_sock); | |
718e3744 | 740 | |
d62a17ae | 741 | /* Create new zebra client. */ |
21ccc0cf | 742 | zserv_client_create(client_sock); |
718e3744 | 743 | |
d62a17ae | 744 | return 0; |
718e3744 | 745 | } |
746 | ||
41674562 DS |
747 | void zserv_close(void) |
748 | { | |
749 | /* | |
750 | * On shutdown, let's close the socket down | |
751 | * so that long running processes of killing the | |
752 | * routing table doesn't leave us in a bad | |
753 | * state where a client tries to reconnect | |
754 | */ | |
755 | close(zsock); | |
756 | zsock = -1; | |
757 | } | |
758 | ||
21ccc0cf | 759 | void zserv_start(char *path) |
d62a17ae | 760 | { |
761 | int ret; | |
d62a17ae | 762 | mode_t old_mask; |
689f5a8c DL |
763 | struct sockaddr_storage sa; |
764 | socklen_t sa_len; | |
d62a17ae | 765 | |
689f5a8c DL |
766 | if (!frr_zclient_addr(&sa, &sa_len, path)) |
767 | /* should be caught in zebra main() */ | |
768 | return; | |
d62a17ae | 769 | |
770 | /* Set umask */ | |
771 | old_mask = umask(0077); | |
772 | ||
773 | /* Make UNIX domain socket. */ | |
cc3d8834 DS |
774 | zsock = socket(sa.ss_family, SOCK_STREAM, 0); |
775 | if (zsock < 0) { | |
450971aa | 776 | flog_err_sys(EC_LIB_SOCKET, "Can't create zserv socket: %s", |
9df414fe | 777 | safe_strerror(errno)); |
d62a17ae | 778 | return; |
779 | } | |
780 | ||
689f5a8c | 781 | if (sa.ss_family != AF_UNIX) { |
cc3d8834 DS |
782 | sockopt_reuseaddr(zsock); |
783 | sockopt_reuseport(zsock); | |
689f5a8c DL |
784 | } else { |
785 | struct sockaddr_un *suna = (struct sockaddr_un *)&sa; | |
786 | if (suna->sun_path[0]) | |
787 | unlink(suna->sun_path); | |
788 | } | |
789 | ||
338b8e91 RW |
790 | setsockopt_so_recvbuf(zsock, 1048576); |
791 | setsockopt_so_sendbuf(zsock, 1048576); | |
689f5a8c | 792 | |
6bb30c2c | 793 | frr_elevate_privs((sa.ss_family != AF_UNIX) ? &zserv_privs : NULL) { |
cc3d8834 | 794 | ret = bind(zsock, (struct sockaddr *)&sa, sa_len); |
6bb30c2c | 795 | } |
d62a17ae | 796 | if (ret < 0) { |
1c50c1c0 QY |
797 | flog_err_sys(EC_LIB_SOCKET, "Can't bind zserv socket on %s: %s", |
798 | path, safe_strerror(errno)); | |
cc3d8834 DS |
799 | close(zsock); |
800 | zsock = -1; | |
d62a17ae | 801 | return; |
802 | } | |
803 | ||
cc3d8834 | 804 | ret = listen(zsock, 5); |
d62a17ae | 805 | if (ret < 0) { |
450971aa | 806 | flog_err_sys(EC_LIB_SOCKET, |
9df414fe QY |
807 | "Can't listen to zserv socket %s: %s", path, |
808 | safe_strerror(errno)); | |
cc3d8834 DS |
809 | close(zsock); |
810 | zsock = -1; | |
d62a17ae | 811 | return; |
812 | } | |
813 | ||
814 | umask(old_mask); | |
815 | ||
21ccc0cf | 816 | zserv_event(NULL, ZSERV_ACCEPT); |
718e3744 | 817 | } |
6b0655a2 | 818 | |
21ccc0cf QY |
819 | void zserv_event(struct zserv *client, enum zserv_event event) |
820 | { | |
821 | switch (event) { | |
822 | case ZSERV_ACCEPT: | |
cc3d8834 | 823 | thread_add_read(zrouter.master, zserv_accept, NULL, zsock, |
21ccc0cf QY |
824 | NULL); |
825 | break; | |
826 | case ZSERV_PROCESS_MESSAGES: | |
3801e764 | 827 | thread_add_event(zrouter.master, zserv_process_messages, client, |
dded2aba | 828 | 0, &client->t_process); |
21ccc0cf | 829 | break; |
f3e33b69 | 830 | case ZSERV_HANDLE_CLIENT_FAIL: |
3801e764 | 831 | thread_add_event(zrouter.master, zserv_handle_client_fail, |
f3e33b69 | 832 | client, 0, &client->t_cleanup); |
21ccc0cf QY |
833 | } |
834 | } | |
835 | ||
836 | ||
f2efe6a3 QY |
837 | /* General purpose ---------------------------------------------------------- */ |
838 | ||
04b02fda | 839 | #define ZEBRA_TIME_BUF 32 |
d62a17ae | 840 | static char *zserv_time_buf(time_t *time1, char *buf, int buflen) |
04b02fda | 841 | { |
d62a17ae | 842 | struct tm *tm; |
843 | time_t now; | |
04b02fda | 844 | |
d62a17ae | 845 | assert(buf != NULL); |
846 | assert(buflen >= ZEBRA_TIME_BUF); | |
847 | assert(time1 != NULL); | |
04b02fda | 848 | |
d62a17ae | 849 | if (!*time1) { |
850 | snprintf(buf, buflen, "never "); | |
851 | return (buf); | |
852 | } | |
04b02fda | 853 | |
d62a17ae | 854 | now = monotime(NULL); |
855 | now -= *time1; | |
856 | tm = gmtime(&now); | |
04b02fda | 857 | |
d62a17ae | 858 | if (now < ONE_DAY_SECOND) |
859 | snprintf(buf, buflen, "%02d:%02d:%02d", tm->tm_hour, tm->tm_min, | |
860 | tm->tm_sec); | |
861 | else if (now < ONE_WEEK_SECOND) | |
862 | snprintf(buf, buflen, "%dd%02dh%02dm", tm->tm_yday, tm->tm_hour, | |
863 | tm->tm_min); | |
96ade3ed | 864 | else |
d62a17ae | 865 | snprintf(buf, buflen, "%02dw%dd%02dh", tm->tm_yday / 7, |
866 | tm->tm_yday - ((tm->tm_yday / 7) * 7), tm->tm_hour); | |
867 | return buf; | |
868 | } | |
869 | ||
870 | static void zebra_show_client_detail(struct vty *vty, struct zserv *client) | |
871 | { | |
872 | char cbuf[ZEBRA_TIME_BUF], rbuf[ZEBRA_TIME_BUF]; | |
873 | char wbuf[ZEBRA_TIME_BUF], nhbuf[ZEBRA_TIME_BUF], mbuf[ZEBRA_TIME_BUF]; | |
52f6868d | 874 | time_t connect_time, last_read_time, last_write_time; |
0545c373 | 875 | uint32_t last_read_cmd, last_write_cmd; |
d62a17ae | 876 | |
877 | vty_out(vty, "Client: %s", zebra_route_string(client->proto)); | |
878 | if (client->instance) | |
879 | vty_out(vty, " Instance: %d", client->instance); | |
880 | vty_out(vty, "\n"); | |
881 | ||
882 | vty_out(vty, "------------------------ \n"); | |
883 | vty_out(vty, "FD: %d \n", client->sock); | |
d62a17ae | 884 | |
52f6868d QY |
885 | connect_time = (time_t) atomic_load_explicit(&client->connect_time, |
886 | memory_order_relaxed); | |
887 | ||
d62a17ae | 888 | vty_out(vty, "Connect Time: %s \n", |
52f6868d | 889 | zserv_time_buf(&connect_time, cbuf, ZEBRA_TIME_BUF)); |
d62a17ae | 890 | if (client->nh_reg_time) { |
891 | vty_out(vty, "Nexthop Registry Time: %s \n", | |
892 | zserv_time_buf(&client->nh_reg_time, nhbuf, | |
893 | ZEBRA_TIME_BUF)); | |
894 | if (client->nh_last_upd_time) | |
895 | vty_out(vty, "Nexthop Last Update Time: %s \n", | |
896 | zserv_time_buf(&client->nh_last_upd_time, mbuf, | |
897 | ZEBRA_TIME_BUF)); | |
898 | else | |
899 | vty_out(vty, "No Nexthop Update sent\n"); | |
900 | } else | |
901 | vty_out(vty, "Not registered for Nexthop Updates\n"); | |
902 | ||
1f312c84 QY |
903 | last_read_time = (time_t)atomic_load_explicit(&client->last_read_time, |
904 | memory_order_relaxed); | |
905 | last_write_time = (time_t)atomic_load_explicit(&client->last_write_time, | |
52f6868d QY |
906 | memory_order_relaxed); |
907 | ||
908 | last_read_cmd = atomic_load_explicit(&client->last_read_cmd, | |
909 | memory_order_relaxed); | |
910 | last_write_cmd = atomic_load_explicit(&client->last_write_cmd, | |
911 | memory_order_relaxed); | |
912 | ||
d62a17ae | 913 | vty_out(vty, "Last Msg Rx Time: %s \n", |
52f6868d | 914 | zserv_time_buf(&last_read_time, rbuf, ZEBRA_TIME_BUF)); |
d62a17ae | 915 | vty_out(vty, "Last Msg Tx Time: %s \n", |
52f6868d QY |
916 | zserv_time_buf(&last_write_time, wbuf, ZEBRA_TIME_BUF)); |
917 | if (last_read_cmd) | |
d62a17ae | 918 | vty_out(vty, "Last Rcvd Cmd: %s \n", |
52f6868d QY |
919 | zserv_command_string(last_read_cmd)); |
920 | if (last_write_cmd) | |
d62a17ae | 921 | vty_out(vty, "Last Sent Cmd: %s \n", |
52f6868d | 922 | zserv_command_string(last_write_cmd)); |
d62a17ae | 923 | vty_out(vty, "\n"); |
924 | ||
925 | vty_out(vty, "Type Add Update Del \n"); | |
926 | vty_out(vty, "================================================== \n"); | |
927 | vty_out(vty, "IPv4 %-12d%-12d%-12d\n", client->v4_route_add_cnt, | |
928 | client->v4_route_upd8_cnt, client->v4_route_del_cnt); | |
929 | vty_out(vty, "IPv6 %-12d%-12d%-12d\n", client->v6_route_add_cnt, | |
930 | client->v6_route_upd8_cnt, client->v6_route_del_cnt); | |
931 | vty_out(vty, "Redist:v4 %-12d%-12d%-12d\n", client->redist_v4_add_cnt, | |
932 | 0, client->redist_v4_del_cnt); | |
933 | vty_out(vty, "Redist:v6 %-12d%-12d%-12d\n", client->redist_v6_add_cnt, | |
934 | 0, client->redist_v6_del_cnt); | |
935 | vty_out(vty, "Connected %-12d%-12d%-12d\n", client->ifadd_cnt, 0, | |
936 | client->ifdel_cnt); | |
937 | vty_out(vty, "BFD peer %-12d%-12d%-12d\n", client->bfd_peer_add_cnt, | |
938 | client->bfd_peer_upd8_cnt, client->bfd_peer_del_cnt); | |
ab5990d8 DS |
939 | vty_out(vty, "NHT v4 %-12d%-12d%-12d\n", |
940 | client->v4_nh_watch_add_cnt, 0, client->v4_nh_watch_rem_cnt); | |
941 | vty_out(vty, "NHT v6 %-12d%-12d%-12d\n", | |
942 | client->v6_nh_watch_add_cnt, 0, client->v6_nh_watch_rem_cnt); | |
4ab3321f AK |
943 | vty_out(vty, "VxLAN SG %-12d%-12d%-12d\n", client->vxlan_sg_add_cnt, |
944 | 0, client->vxlan_sg_del_cnt); | |
d62a17ae | 945 | vty_out(vty, "Interface Up Notifications: %d\n", client->ifup_cnt); |
946 | vty_out(vty, "Interface Down Notifications: %d\n", client->ifdown_cnt); | |
947 | vty_out(vty, "VNI add notifications: %d\n", client->vniadd_cnt); | |
948 | vty_out(vty, "VNI delete notifications: %d\n", client->vnidel_cnt); | |
b7cfce93 MK |
949 | vty_out(vty, "L3-VNI add notifications: %d\n", client->l3vniadd_cnt); |
950 | vty_out(vty, "L3-VNI delete notifications: %d\n", client->l3vnidel_cnt); | |
d62a17ae | 951 | vty_out(vty, "MAC-IP add notifications: %d\n", client->macipadd_cnt); |
952 | vty_out(vty, "MAC-IP delete notifications: %d\n", client->macipdel_cnt); | |
953 | ||
03ed85a6 DS |
954 | #if defined DEV_BUILD |
955 | vty_out(vty, "Input Fifo: %zu:%zu Output Fifo: %zu:%zu\n", | |
956 | client->ibuf_fifo->count, client->ibuf_fifo->max_count, | |
957 | client->obuf_fifo->count, client->obuf_fifo->max_count); | |
958 | #endif | |
d62a17ae | 959 | vty_out(vty, "\n"); |
960 | return; | |
961 | } | |
962 | ||
963 | static void zebra_show_client_brief(struct vty *vty, struct zserv *client) | |
964 | { | |
965 | char cbuf[ZEBRA_TIME_BUF], rbuf[ZEBRA_TIME_BUF]; | |
966 | char wbuf[ZEBRA_TIME_BUF]; | |
52f6868d QY |
967 | time_t connect_time, last_read_time, last_write_time; |
968 | ||
e1de21d7 QY |
969 | connect_time = (time_t)atomic_load_explicit(&client->connect_time, |
970 | memory_order_relaxed); | |
971 | last_read_time = (time_t)atomic_load_explicit(&client->last_read_time, | |
972 | memory_order_relaxed); | |
973 | last_write_time = (time_t)atomic_load_explicit(&client->last_write_time, | |
52f6868d | 974 | memory_order_relaxed); |
d62a17ae | 975 | |
976 | vty_out(vty, "%-8s%12s %12s%12s%8d/%-8d%8d/%-8d\n", | |
977 | zebra_route_string(client->proto), | |
52f6868d QY |
978 | zserv_time_buf(&connect_time, cbuf, ZEBRA_TIME_BUF), |
979 | zserv_time_buf(&last_read_time, rbuf, ZEBRA_TIME_BUF), | |
980 | zserv_time_buf(&last_write_time, wbuf, ZEBRA_TIME_BUF), | |
d62a17ae | 981 | client->v4_route_add_cnt + client->v4_route_upd8_cnt, |
982 | client->v4_route_del_cnt, | |
983 | client->v6_route_add_cnt + client->v6_route_upd8_cnt, | |
984 | client->v6_route_del_cnt); | |
985 | } | |
986 | ||
21ccc0cf | 987 | struct zserv *zserv_find_client(uint8_t proto, unsigned short instance) |
d62a17ae | 988 | { |
989 | struct listnode *node, *nnode; | |
990 | struct zserv *client; | |
991 | ||
161e9ab7 | 992 | for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) { |
996c9314 | 993 | if (client->proto == proto && client->instance == instance) |
d62a17ae | 994 | return client; |
995 | } | |
996 | ||
997 | return NULL; | |
8ed6821e | 998 | } |
999 | ||
718e3744 | 1000 | /* This command is for debugging purpose. */ |
1001 | DEFUN (show_zebra_client, | |
1002 | show_zebra_client_cmd, | |
1003 | "show zebra client", | |
1004 | SHOW_STR | |
41e7fb80 | 1005 | ZEBRA_STR |
b9ee4999 | 1006 | "Client information\n") |
718e3744 | 1007 | { |
d62a17ae | 1008 | struct listnode *node; |
1009 | struct zserv *client; | |
718e3744 | 1010 | |
161e9ab7 | 1011 | for (ALL_LIST_ELEMENTS_RO(zrouter.client_list, node, client)) |
d62a17ae | 1012 | zebra_show_client_detail(vty, client); |
04b02fda | 1013 | |
d62a17ae | 1014 | return CMD_SUCCESS; |
04b02fda DS |
1015 | } |
1016 | ||
1017 | /* This command is for debugging purpose. */ | |
1018 | DEFUN (show_zebra_client_summary, | |
1019 | show_zebra_client_summary_cmd, | |
1020 | "show zebra client summary", | |
1021 | SHOW_STR | |
41e7fb80 | 1022 | ZEBRA_STR |
b9ee4999 DS |
1023 | "Client information brief\n" |
1024 | "Brief Summary\n") | |
04b02fda | 1025 | { |
d62a17ae | 1026 | struct listnode *node; |
1027 | struct zserv *client; | |
04b02fda | 1028 | |
d62a17ae | 1029 | vty_out(vty, |
1030 | "Name Connect Time Last Read Last Write IPv4 Routes IPv6 Routes \n"); | |
1031 | vty_out(vty, | |
1032 | "--------------------------------------------------------------------------------\n"); | |
04b02fda | 1033 | |
161e9ab7 | 1034 | for (ALL_LIST_ELEMENTS_RO(zrouter.client_list, node, client)) |
d62a17ae | 1035 | zebra_show_client_brief(vty, client); |
fb018d25 | 1036 | |
d62a17ae | 1037 | vty_out(vty, "Routes column shows (added+updated)/deleted\n"); |
1038 | return CMD_SUCCESS; | |
718e3744 | 1039 | } |
1040 | ||
411314ed DS |
1041 | #if defined(HANDLE_ZAPI_FUZZING) |
1042 | void zserv_read_file(char *input) | |
1043 | { | |
1044 | int fd; | |
411314ed | 1045 | |
996c9314 | 1046 | fd = open(input, O_RDONLY | O_NONBLOCK); |
411314ed | 1047 | |
2875801f | 1048 | zserv_client_create(fd); |
411314ed DS |
1049 | } |
1050 | #endif | |
1051 | ||
5f145fb8 | 1052 | void zserv_init(void) |
718e3744 | 1053 | { |
d62a17ae | 1054 | /* Client list init. */ |
161e9ab7 | 1055 | zrouter.client_list = list_new(); |
21ccc0cf QY |
1056 | |
1057 | /* Misc init. */ | |
cc3d8834 | 1058 | zsock = -1; |
718e3744 | 1059 | |
d62a17ae | 1060 | install_element(ENABLE_NODE, &show_zebra_client_cmd); |
1061 | install_element(ENABLE_NODE, &show_zebra_client_summary_cmd); | |
718e3744 | 1062 | } |