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