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