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