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