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