]>
Commit | Line | Data |
---|---|---|
acddc0ed | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
bf094f69 QY |
2 | /* |
3 | * Zebra API server. | |
4 | * Portions: | |
5 | * Copyright (C) 1997-1999 Kunihiro Ishiguro | |
6 | * Copyright (C) 2015-2018 Cumulus Networks, Inc. | |
7 | * et al. | |
718e3744 | 8 | */ |
9 | ||
10 | #ifndef _ZEBRA_ZSERV_H | |
11 | #define _ZEBRA_ZSERV_H | |
12 | ||
d8647095 | 13 | /* clang-format off */ |
bf094f69 QY |
14 | #include <stdint.h> /* for uint32_t, uint8_t */ |
15 | #include <time.h> /* for time_t */ | |
16 | ||
17 | #include "lib/route_types.h" /* for ZEBRA_ROUTE_MAX */ | |
18 | #include "lib/zebra.h" /* for AFI_MAX */ | |
19 | #include "lib/vrf.h" /* for vrf_bitmap_t */ | |
20 | #include "lib/zclient.h" /* for redist_proto */ | |
21 | #include "lib/stream.h" /* for stream, stream_fifo */ | |
24a58196 | 22 | #include "frrevent.h" /* for thread, thread_master */ |
bf094f69 QY |
23 | #include "lib/linklist.h" /* for list */ |
24 | #include "lib/workqueue.h" /* for work_queue */ | |
453844ab | 25 | #include "lib/hook.h" /* for DECLARE_HOOK, DECLARE_KOOH */ |
d8647095 | 26 | /* clang-format on */ |
6833ae01 | 27 | |
51e94aa7 EDP |
28 | #ifdef __cplusplus |
29 | extern "C" { | |
30 | #endif | |
31 | ||
7ca9c407 DL |
32 | struct zebra_vrf; |
33 | ||
718e3744 | 34 | /* Default port information. */ |
718e3744 | 35 | #define ZEBRA_VTY_PORT 2601 |
718e3744 | 36 | |
37 | /* Default configuration filename. */ | |
38 | #define DEFAULT_CONFIG_FILE "zebra.conf" | |
39 | ||
518f0eb1 DS |
40 | #define ZEBRA_RMAP_DEFAULT_UPDATE_TIMER 5 /* disabled by default */ |
41 | ||
8062cbe2 S |
42 | |
43 | /* Stale route marker timer */ | |
44 | #define ZEBRA_DEFAULT_STALE_UPDATE_DELAY 1 | |
45 | ||
46 | /* Count of stale routes processed in timer context */ | |
47 | #define ZEBRA_MAX_STALE_ROUTE_COUNT 50000 | |
48 | ||
49 | /* Graceful Restart information */ | |
50 | struct client_gr_info { | |
51 | /* VRF for which GR enabled */ | |
52 | vrf_id_t vrf_id; | |
53 | ||
8062cbe2 S |
54 | /* Stale time and GR cap */ |
55 | uint32_t stale_removal_time; | |
56 | enum zserv_client_capabilities capabilities; | |
57 | ||
58 | /* GR commands */ | |
cd7108ba | 59 | bool do_delete; |
8062cbe2 S |
60 | bool gr_enable; |
61 | bool stale_client; | |
62 | ||
63 | /* Route sync and enable flags for AFI/SAFI */ | |
0f5ef7f9 DS |
64 | bool af_enabled[AFI_MAX]; |
65 | bool route_sync[AFI_MAX]; | |
8062cbe2 S |
66 | |
67 | /* Book keeping */ | |
8062cbe2 | 68 | void *stale_client_ptr; |
e6685141 | 69 | struct event *t_stale_removal; |
8062cbe2 S |
70 | |
71 | TAILQ_ENTRY(client_gr_info) gr_info; | |
72 | }; | |
73 | ||
718e3744 | 74 | /* Client structure. */ |
d62a17ae | 75 | struct zserv { |
329e35da QY |
76 | /* Client pthread */ |
77 | struct frr_pthread *pthread; | |
78 | ||
d62a17ae | 79 | /* Client file descriptor. */ |
80 | int sock; | |
81 | ||
aa9002a5 MS |
82 | /* Attributes used to permit access to zapi clients from |
83 | * other pthreads: the client has a busy counter, and a | |
84 | * 'closed' flag. These attributes are managed using a | |
85 | * lock, via the acquire_client() and release_client() apis. | |
86 | */ | |
87 | int busy_count; | |
88 | bool is_closed; | |
89 | ||
d62a17ae | 90 | /* Input/output buffer to the client. */ |
329e35da | 91 | pthread_mutex_t ibuf_mtx; |
1002497a | 92 | struct stream_fifo *ibuf_fifo; |
329e35da | 93 | pthread_mutex_t obuf_mtx; |
1002497a QY |
94 | struct stream_fifo *obuf_fifo; |
95 | ||
96 | /* Private I/O buffers */ | |
97 | struct stream *ibuf_work; | |
98 | struct stream *obuf_work; | |
d62a17ae | 99 | |
100 | /* Buffer of data waiting to be written to client. */ | |
101 | struct buffer *wb; | |
102 | ||
103 | /* Threads for read/write. */ | |
e6685141 DS |
104 | struct event *t_read; |
105 | struct event *t_write; | |
d62a17ae | 106 | |
dded2aba | 107 | /* Event for message processing, for the main pthread */ |
e6685141 | 108 | struct event *t_process; |
dded2aba | 109 | |
aa9002a5 | 110 | /* Event for the main pthread */ |
e6685141 | 111 | struct event *t_cleanup; |
f3e33b69 | 112 | |
d62a17ae | 113 | /* This client's redistribute flag. */ |
114 | struct redist_proto mi_redist[AFI_MAX][ZEBRA_ROUTE_MAX]; | |
115 | vrf_bitmap_t redist[AFI_MAX][ZEBRA_ROUTE_MAX]; | |
116 | ||
117 | /* Redistribute default route flag. */ | |
49db7a7b | 118 | vrf_bitmap_t redist_default[AFI_MAX]; |
d62a17ae | 119 | |
d62a17ae | 120 | /* Router-id information. */ |
98a3fb0a | 121 | vrf_bitmap_t ridinfo[AFI_MAX]; |
d62a17ae | 122 | |
7723e8d3 PG |
123 | /* Router-id information. */ |
124 | vrf_bitmap_t nhrp_neighinfo[AFI_MAX]; | |
125 | ||
e1a1880d DS |
126 | bool notify_owner; |
127 | ||
17da84a4 KS |
128 | /* Indicates if client is synchronous. */ |
129 | bool synchronous; | |
130 | ||
4e0b5b31 | 131 | /* client's protocol and session info */ |
d7c0a89a | 132 | uint8_t proto; |
453844ab | 133 | uint16_t instance; |
4e0b5b31 | 134 | uint32_t session_id; |
d62a17ae | 135 | |
ee235396 SK |
136 | /* |
137 | * Interested for MLAG Updates, and also stores the client | |
138 | * interested message mask | |
139 | */ | |
140 | bool mlag_updates_interested; | |
141 | uint32_t mlag_reg_mask1; | |
142 | ||
d62a17ae | 143 | /* Statistics */ |
d7c0a89a QY |
144 | uint32_t redist_v4_add_cnt; |
145 | uint32_t redist_v4_del_cnt; | |
146 | uint32_t redist_v6_add_cnt; | |
147 | uint32_t redist_v6_del_cnt; | |
148 | uint32_t v4_route_add_cnt; | |
149 | uint32_t v4_route_upd8_cnt; | |
150 | uint32_t v4_route_del_cnt; | |
151 | uint32_t v6_route_add_cnt; | |
152 | uint32_t v6_route_del_cnt; | |
153 | uint32_t v6_route_upd8_cnt; | |
154 | uint32_t connected_rt_add_cnt; | |
155 | uint32_t connected_rt_del_cnt; | |
156 | uint32_t ifup_cnt; | |
157 | uint32_t ifdown_cnt; | |
158 | uint32_t ifadd_cnt; | |
159 | uint32_t ifdel_cnt; | |
160 | uint32_t if_bfd_cnt; | |
161 | uint32_t bfd_peer_add_cnt; | |
162 | uint32_t bfd_peer_upd8_cnt; | |
163 | uint32_t bfd_peer_del_cnt; | |
164 | uint32_t bfd_peer_replay_cnt; | |
165 | uint32_t vrfadd_cnt; | |
166 | uint32_t vrfdel_cnt; | |
167 | uint32_t if_vrfchg_cnt; | |
168 | uint32_t bfd_client_reg_cnt; | |
169 | uint32_t vniadd_cnt; | |
170 | uint32_t vnidel_cnt; | |
171 | uint32_t l3vniadd_cnt; | |
172 | uint32_t l3vnidel_cnt; | |
173 | uint32_t macipadd_cnt; | |
174 | uint32_t macipdel_cnt; | |
175 | uint32_t prefixadd_cnt; | |
176 | uint32_t prefixdel_cnt; | |
ab5990d8 DS |
177 | uint32_t v4_nh_watch_add_cnt; |
178 | uint32_t v4_nh_watch_rem_cnt; | |
179 | uint32_t v6_nh_watch_add_cnt; | |
180 | uint32_t v6_nh_watch_rem_cnt; | |
4ab3321f AK |
181 | uint32_t vxlan_sg_add_cnt; |
182 | uint32_t vxlan_sg_del_cnt; | |
ce5160c0 AK |
183 | uint32_t local_es_add_cnt; |
184 | uint32_t local_es_del_cnt; | |
185 | uint32_t local_es_evi_add_cnt; | |
186 | uint32_t local_es_evi_del_cnt; | |
9ab0b2a3 | 187 | uint32_t error_cnt; |
d62a17ae | 188 | |
d62a17ae | 189 | time_t nh_reg_time; |
190 | time_t nh_dereg_time; | |
191 | time_t nh_last_upd_time; | |
192 | ||
52f6868d QY |
193 | /* |
194 | * Session information. | |
195 | * | |
196 | * These are not synchronous with respect to each other. For instance, | |
197 | * last_read_cmd may contain a value that has been read in the future | |
198 | * relative to last_read_time. | |
199 | */ | |
200 | ||
ccac1109 DL |
201 | pthread_mutex_t stats_mtx; |
202 | /* BEGIN covered by stats_mtx */ | |
203 | ||
52f6868d | 204 | /* monotime of client creation */ |
ccac1109 | 205 | uint64_t connect_time; |
52f6868d | 206 | /* monotime of last message received */ |
ccac1109 | 207 | uint64_t last_read_time; |
52f6868d | 208 | /* monotime of last message sent */ |
ccac1109 | 209 | uint64_t last_write_time; |
52f6868d | 210 | /* command code of last message read */ |
ccac1109 | 211 | uint64_t last_read_cmd; |
52f6868d | 212 | /* command code of last message written */ |
ccac1109 DL |
213 | uint64_t last_write_cmd; |
214 | ||
215 | /* END covered by stats_mtx */ | |
8062cbe2 S |
216 | |
217 | /* | |
218 | * Number of instances configured with | |
219 | * graceful restart | |
220 | */ | |
221 | uint32_t gr_instance_count; | |
222 | time_t restart_time; | |
223 | ||
224 | /* | |
225 | * Graceful restart information for | |
226 | * each instance | |
227 | */ | |
228 | TAILQ_HEAD(info_list, client_gr_info) gr_info_queue; | |
718e3744 | 229 | }; |
230 | ||
89f4e507 | 231 | #define ZAPI_HANDLER_ARGS \ |
1002497a QY |
232 | struct zserv *client, struct zmsghdr *hdr, struct stream *msg, \ |
233 | struct zebra_vrf *zvrf | |
89f4e507 | 234 | |
453844ab | 235 | /* Hooks for client connect / disconnect */ |
21ccc0cf QY |
236 | DECLARE_HOOK(zserv_client_connect, (struct zserv *client), (client)); |
237 | DECLARE_KOOH(zserv_client_close, (struct zserv *client), (client)); | |
453844ab | 238 | |
6f4aee61 S |
239 | #define DYNAMIC_CLIENT_GR_DISABLED(_client) \ |
240 | ((_client->proto <= ZEBRA_ROUTE_CONNECT) \ | |
241 | || !(_client->gr_instance_count)) | |
242 | ||
21ccc0cf QY |
243 | /* |
244 | * Initialize Zebra API server. | |
245 | * | |
246 | * Installs CLI commands and creates the client list. | |
247 | */ | |
5f145fb8 | 248 | extern void zserv_init(void); |
fb018d25 | 249 | |
41674562 DS |
250 | /* |
251 | * Stop the Zebra API server. | |
252 | * | |
253 | * closes the socket | |
254 | */ | |
255 | extern void zserv_close(void); | |
256 | ||
21ccc0cf QY |
257 | /* |
258 | * Start Zebra API server. | |
259 | * | |
260 | * Allocates resources, creates the server socket and begins listening on the | |
261 | * socket. | |
262 | * | |
263 | * path | |
264 | * where to place the Unix domain socket | |
265 | */ | |
266 | extern void zserv_start(char *path); | |
267 | ||
268 | /* | |
269 | * Send a message to a connected Zebra API client. | |
270 | * | |
271 | * client | |
272 | * the client to send to | |
273 | * | |
274 | * msg | |
275 | * the message to send | |
276 | */ | |
277 | extern int zserv_send_message(struct zserv *client, struct stream *msg); | |
278 | ||
6e2a33a8 MS |
279 | /* |
280 | * Send a batch of messages to a connected Zebra API client. | |
281 | * | |
282 | * client | |
283 | * the client to send to | |
284 | * | |
285 | * fifo | |
286 | * the list of messages to send | |
287 | */ | |
288 | extern int zserv_send_batch(struct zserv *client, struct stream_fifo *fifo); | |
289 | ||
21ccc0cf QY |
290 | /* |
291 | * Retrieve a client by its protocol and instance number. | |
292 | * | |
293 | * proto | |
294 | * protocol number | |
295 | * | |
296 | * instance | |
297 | * instance number | |
298 | * | |
299 | * Returns: | |
300 | * The Zebra API client. | |
301 | */ | |
302 | extern struct zserv *zserv_find_client(uint8_t proto, unsigned short instance); | |
8ed6821e | 303 | |
4e0b5b31 MS |
304 | /* |
305 | * Retrieve a client by its protocol, instance number, and session id. | |
306 | * | |
307 | * proto | |
308 | * protocol number | |
309 | * | |
310 | * instance | |
311 | * instance number | |
312 | * | |
313 | * session_id | |
314 | * session id | |
315 | * | |
316 | * Returns: | |
317 | * The Zebra API client. | |
318 | */ | |
319 | struct zserv *zserv_find_client_session(uint8_t proto, unsigned short instance, | |
320 | uint32_t session_id); | |
321 | ||
aa9002a5 MS |
322 | /* |
323 | * Retrieve a client object by the complete tuple of | |
324 | * {protocol, instance, session}. This version supports use | |
325 | * from a different pthread: the object will be returned marked | |
326 | * in-use. The caller *must* release the client object with the | |
327 | * release_client() api, to ensure that the in-use marker is cleared properly. | |
328 | * | |
329 | * Returns: | |
330 | * The Zebra API client. | |
331 | */ | |
332 | extern struct zserv *zserv_acquire_client(uint8_t proto, | |
333 | unsigned short instance, | |
334 | uint32_t session_id); | |
335 | ||
336 | /* | |
337 | * Release a client object that was acquired with the acquire_client() api. | |
338 | * After this has been called, the pointer must not be used - it may be freed | |
339 | * in another pthread if the client has closed. | |
340 | */ | |
341 | extern void zserv_release_client(struct zserv *client); | |
342 | ||
f3e33b69 QY |
343 | /* |
344 | * Close a client. | |
345 | * | |
346 | * Kills a client's thread, removes the client from the client list and cleans | |
347 | * up its resources. | |
348 | * | |
349 | * client | |
350 | * the client to close | |
351 | */ | |
352 | extern void zserv_close_client(struct zserv *client); | |
353 | ||
c0ce4875 MS |
354 | /* |
355 | * Free memory for a zserv client object - note that this does not | |
356 | * clean up the internal allocations associated with the zserv client, | |
357 | * this just free the struct's memory. | |
358 | */ | |
359 | void zserv_client_delete(struct zserv *client); | |
360 | ||
49b3b01f QY |
361 | /* |
362 | * Log a ZAPI message hexdump. | |
363 | * | |
364 | * errmsg | |
365 | * Error message to include with packet hexdump | |
366 | * | |
367 | * msg | |
368 | * Message to log | |
369 | * | |
370 | * hdr | |
371 | * Message header | |
372 | */ | |
373 | void zserv_log_message(const char *errmsg, struct stream *msg, | |
374 | struct zmsghdr *hdr); | |
375 | ||
4dfd7a02 | 376 | /* TODO */ |
e6685141 | 377 | __attribute__((__noreturn__)) void zebra_finalize(struct event *event); |
4dfd7a02 | 378 | |
851140a7 S |
379 | /* |
380 | * Graceful restart functions. | |
381 | */ | |
382 | extern int zebra_gr_client_disconnect(struct zserv *client); | |
383 | extern void zebra_gr_client_reconnect(struct zserv *client); | |
b9e6727a S |
384 | extern void zebra_gr_stale_client_cleanup(struct list *client_list); |
385 | extern void zread_client_capabilities(struct zserv *client, struct zmsghdr *hdr, | |
386 | struct stream *msg, | |
387 | struct zebra_vrf *zvrf); | |
851140a7 | 388 | |
51e94aa7 EDP |
389 | #ifdef __cplusplus |
390 | } | |
391 | #endif | |
392 | ||
718e3744 | 393 | #endif /* _ZEBRA_ZEBRA_H */ |