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