]> git.proxmox.com Git - mirror_frr.git/blob - lib/vty.h
Merge pull request #13086 from donaldsharp/suppress_fib_pending
[mirror_frr.git] / lib / vty.h
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Virtual terminal [aka TeletYpe] interface routine
3 * Copyright (C) 1997 Kunihiro Ishiguro
4 */
5
6 #ifndef _ZEBRA_VTY_H
7 #define _ZEBRA_VTY_H
8
9 #include <sys/types.h>
10 #ifdef HAVE_LIBPCRE2_POSIX
11 #ifndef _FRR_PCRE2_POSIX
12 #define _FRR_PCRE2_POSIX
13 #include <pcre2posix.h>
14 #endif /* _FRR_PCRE2_POSIX */
15 #elif defined(HAVE_LIBPCREPOSIX)
16 #include <pcreposix.h>
17 #else
18 #include <regex.h>
19 #endif /* HAVE_LIBPCRE2_POSIX */
20
21 #include "frrevent.h"
22 #include "log.h"
23 #include "sockunion.h"
24 #include "qobj.h"
25 #include "compiler.h"
26 #include "northbound.h"
27 #include "zlog_live.h"
28 #include "libfrr.h"
29 #include "mgmt_fe_client.h"
30
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34
35 struct json_object;
36
37 #define VTY_BUFSIZ 4096
38 #define VTY_MAXHIST 20
39 #define VTY_MAXDEPTH 8
40
41 #define VTY_MAXCFGCHANGES 16
42
43 struct vty_error {
44 char error_buf[VTY_BUFSIZ];
45 uint32_t line_num;
46 };
47
48 struct vty_cfg_change {
49 char xpath[XPATH_MAXLEN];
50 enum nb_operation operation;
51 const char *value;
52 };
53
54 PREDECL_DLIST(vtys);
55
56 /* VTY struct. */
57 struct vty {
58 struct vtys_item itm;
59
60 /* File descripter of this vty. */
61 int fd;
62
63 /* output FD, to support stdin/stdout combination */
64 int wfd;
65
66 /* File output, used for VTYSH only */
67 FILE *of;
68 FILE *of_saved;
69
70 /* whether we are using pager or not */
71 bool is_paged;
72
73 /* Is this vty connect to file or not */
74 enum { VTY_TERM, VTY_FILE, VTY_SHELL, VTY_SHELL_SERV } type;
75
76 /* Node status of this vty */
77 int node;
78
79 /* Failure count */
80 int fail;
81
82 /* Output filer regex */
83 bool filter;
84 regex_t include;
85
86 /* Line buffer */
87 struct buffer *lbuf;
88
89 /* Output buffer. */
90 struct buffer *obuf;
91
92 /* Command input buffer */
93 char *buf;
94
95 /* Command input error buffer */
96 struct list *error;
97
98 /* Command cursor point */
99 int cp;
100
101 /* Command length */
102 int length;
103
104 /* Command max length. */
105 int max;
106
107 /* Histry of command */
108 char *hist[VTY_MAXHIST];
109
110 /* History lookup current point */
111 int hp;
112
113 /* History insert end point */
114 int hindex;
115
116 /* Changes enqueued to be applied in the candidate configuration. */
117 size_t num_cfg_changes;
118 struct nb_cfg_change cfg_changes[VTY_MAXCFGCHANGES];
119
120 /* XPath of the current node */
121 int xpath_index;
122 char xpath[VTY_MAXDEPTH][XPATH_MAXLEN];
123
124 /*
125 * Keep track of how many SET_CFG requests has been sent so far that
126 * has not been committed yet.
127 */
128 size_t mgmt_num_pending_setcfg;
129
130 /* In configure mode. */
131 bool config;
132
133 /* Private candidate configuration mode. */
134 bool private_config;
135
136 /* Candidate configuration. */
137 struct nb_config *candidate_config;
138
139 /* Base candidate configuration. */
140 struct nb_config *candidate_config_base;
141
142 /* Dynamic transaction information. */
143 bool pending_allowed;
144 bool pending_commit;
145 bool no_implicit_commit;
146 char *pending_cmds_buf;
147 size_t pending_cmds_buflen;
148 size_t pending_cmds_bufpos;
149
150 /* Confirmed-commit timeout and rollback configuration. */
151 struct event *t_confirmed_commit_timeout;
152 struct nb_config *confirmed_commit_rollback;
153
154 /* qobj object ID (replacement for "index") */
155 uint64_t qobj_index;
156
157 /* qobj second-level object ID (replacement for "index_sub") */
158 uint64_t qobj_index_sub;
159
160 /* For escape character. */
161 unsigned char escape;
162
163 /* Current vty status. */
164 enum {
165 VTY_NORMAL,
166 VTY_CLOSE,
167 VTY_MORE,
168 VTY_MORELINE,
169 VTY_PASSFD,
170 } status;
171
172 /* vtysh socket/fd passing (for terminal monitor) */
173 int pass_fd;
174
175 /* CLI command return value (likely CMD_SUCCESS) when pass_fd != -1 */
176 uint8_t pass_fd_status[4];
177
178 /* live logging target / terminal monitor */
179 struct zlog_live_cfg live_log;
180
181 /* IAC handling: was the last character received the
182 IAC (interpret-as-command) escape character (and therefore the next
183 character will be the command code)? Refer to Telnet RFC 854. */
184 unsigned char iac;
185
186 /* IAC SB (option subnegotiation) handling */
187 unsigned char iac_sb_in_progress;
188 /* At the moment, we care only about the NAWS (window size) negotiation,
189 and that requires just a 5-character buffer (RFC 1073):
190 <NAWS char> <16-bit width> <16-bit height> */
191 #define TELNET_NAWS_SB_LEN 5
192 unsigned char sb_buf[TELNET_NAWS_SB_LEN];
193 /* How many subnegotiation characters have we received? We just drop
194 those that do not fit in the buffer. */
195 size_t sb_len;
196
197 /* Window width/height. */
198 int width;
199 int height;
200
201 /* Configure lines. */
202 int lines;
203
204 /* Read and write thread. */
205 struct event *t_read;
206 struct event *t_write;
207
208 /* Timeout seconds and thread. */
209 unsigned long v_timeout;
210 struct event *t_timeout;
211
212 /* What address is this vty comming from. */
213 char address[SU_ADDRSTRLEN];
214
215 /* "frame" output. This is buffered and will be printed if some
216 * actual output follows, or will be discarded if the frame ends
217 * without any output. */
218 size_t frame_pos;
219 char frame[1024];
220
221 uintptr_t mgmt_session_id;
222 uint64_t mgmt_client_id;
223 uint64_t mgmt_req_id;
224 bool mgmt_req_pending;
225 bool mgmt_locked_candidate_ds;
226 };
227
228 static inline void vty_push_context(struct vty *vty, int node, uint64_t id)
229 {
230 vty->node = node;
231 vty->qobj_index = id;
232 }
233
234 /* note: VTY_PUSH_CONTEXT(..., NULL) doesn't work, since it will try to
235 * dereference "NULL->qobj_node.nid" */
236 #define VTY_PUSH_CONTEXT(nodeval, ptr) \
237 vty_push_context(vty, nodeval, QOBJ_ID_0SAFE(ptr))
238 #define VTY_PUSH_CONTEXT_NULL(nodeval) vty_push_context(vty, nodeval, 0ULL)
239 #define VTY_PUSH_CONTEXT_SUB(nodeval, ptr) \
240 do { \
241 vty->node = nodeval; \
242 /* qobj_index stays untouched */ \
243 vty->qobj_index_sub = QOBJ_ID_0SAFE(ptr); \
244 } while (0)
245
246 /* can return NULL if context is invalid! */
247 #define VTY_GET_CONTEXT(structname) \
248 QOBJ_GET_TYPESAFE(vty->qobj_index, structname)
249 #define VTY_GET_CONTEXT_SUB(structname) \
250 QOBJ_GET_TYPESAFE(vty->qobj_index_sub, structname)
251
252 /* will return if ptr is NULL. */
253 #define VTY_CHECK_CONTEXT(ptr) \
254 if (!ptr) { \
255 vty_out(vty, \
256 "Current configuration object was deleted " \
257 "by another process.\n"); \
258 return CMD_WARNING; \
259 }
260
261 /* struct structname *ptr = <context>; ptr will never be NULL. */
262 #define VTY_DECLVAR_CONTEXT(structname, ptr) \
263 struct structname *ptr = VTY_GET_CONTEXT(structname); \
264 VTY_CHECK_CONTEXT(ptr);
265 #define VTY_DECLVAR_CONTEXT_SUB(structname, ptr) \
266 struct structname *ptr = VTY_GET_CONTEXT_SUB(structname); \
267 VTY_CHECK_CONTEXT(ptr);
268 #define VTY_DECLVAR_INSTANCE_CONTEXT(structname, ptr) \
269 if (vty->qobj_index == 0) \
270 return CMD_NOT_MY_INSTANCE; \
271 struct structname *ptr = VTY_GET_CONTEXT(structname); \
272 VTY_CHECK_CONTEXT(ptr);
273
274 #define VTY_DECLVAR_CONTEXT_VRF(vrfptr) \
275 struct vrf *vrfptr; \
276 if (vty->node == CONFIG_NODE) \
277 vrfptr = vrf_lookup_by_id(VRF_DEFAULT); \
278 else \
279 vrfptr = VTY_GET_CONTEXT(vrf); \
280 VTY_CHECK_CONTEXT(vrfptr); \
281 MACRO_REQUIRE_SEMICOLON() /* end */
282
283 /* XPath macros. */
284 #define VTY_PUSH_XPATH(nodeval, value) \
285 do { \
286 if (vty->xpath_index >= VTY_MAXDEPTH) { \
287 vty_out(vty, "%% Reached maximum CLI depth (%u)\n", \
288 VTY_MAXDEPTH); \
289 return CMD_WARNING; \
290 } \
291 vty->node = nodeval; \
292 strlcpy(vty->xpath[vty->xpath_index], value, \
293 sizeof(vty->xpath[0])); \
294 vty->xpath_index++; \
295 } while (0)
296
297 #define VTY_CURR_XPATH vty->xpath[vty->xpath_index - 1]
298
299 #define VTY_CHECK_XPATH \
300 do { \
301 if (vty->type != VTY_FILE && !vty->private_config \
302 && vty->xpath_index > 0 \
303 && !yang_dnode_exists(vty->candidate_config->dnode, \
304 VTY_CURR_XPATH)) { \
305 vty_out(vty, \
306 "Current configuration object was deleted " \
307 "by another process.\n\n"); \
308 return CMD_WARNING; \
309 } \
310 } while (0)
311
312 struct vty_arg {
313 const char *name;
314 const char *value;
315 const char **argv;
316 int argc;
317 };
318
319 /* Integrated configuration file. */
320 #define INTEGRATE_DEFAULT_CONFIG "frr.conf"
321
322 /* Default time out value */
323 #define VTY_TIMEOUT_DEFAULT 600
324
325 /* Vty read buffer size. */
326 #define VTY_READ_BUFSIZ 512
327
328 /* Directory separator. */
329 #ifndef DIRECTORY_SEP
330 #define DIRECTORY_SEP '/'
331 #endif /* DIRECTORY_SEP */
332
333 #ifndef IS_DIRECTORY_SEP
334 #define IS_DIRECTORY_SEP(c) ((c) == DIRECTORY_SEP)
335 #endif
336
337 extern struct nb_config *vty_mgmt_candidate_config;
338
339 /* Prototypes. */
340 extern void vty_init(struct event_loop *m, bool do_command_logging);
341 extern void vty_init_vtysh(void);
342 extern void vty_terminate(void);
343 extern void vty_reset(void);
344 extern struct vty *vty_new(void);
345 extern struct vty *vty_stdio(void (*atclose)(int isexit));
346
347 /* - vty_frame() output goes to a buffer (for context-begin markers)
348 * - vty_out() will first print this buffer, and clear it
349 * - vty_endframe() clears the buffer without printing it, and prints an
350 * extra string if the buffer was empty before (for context-end markers)
351 */
352 extern int vty_out(struct vty *, const char *, ...) PRINTFRR(2, 3);
353 extern void vty_frame(struct vty *, const char *, ...) PRINTFRR(2, 3);
354 extern void vty_endframe(struct vty *, const char *);
355 extern bool vty_set_include(struct vty *vty, const char *regexp);
356 /* returns CMD_SUCCESS so you can do a one-line "return vty_json(...)"
357 * NULL check and json_object_free() is included.
358 *
359 * _no_pretty means do not add a bunch of newlines and dump the output
360 * as densely as possible.
361 */
362 extern int vty_json(struct vty *vty, struct json_object *json);
363 extern int vty_json_no_pretty(struct vty *vty, struct json_object *json);
364 extern void vty_json_empty(struct vty *vty);
365 /* post fd to be passed to the vtysh client
366 * fd is owned by the VTY code after this and will be closed when done
367 */
368 extern void vty_pass_fd(struct vty *vty, int fd);
369
370 extern bool vty_read_config(struct nb_config *config, const char *config_file,
371 char *config_default_dir);
372 extern void vty_time_print(struct vty *, int);
373 extern void vty_serv_sock(const char *, unsigned short, const char *);
374 extern void vty_close(struct vty *);
375 extern char *vty_get_cwd(void);
376 extern void vty_update_xpath(const char *oldpath, const char *newpath);
377 extern int vty_config_enter(struct vty *vty, bool private_config,
378 bool exclusive);
379 extern void vty_config_exit(struct vty *);
380 extern int vty_config_node_exit(struct vty *);
381 extern int vty_shell(struct vty *);
382 extern int vty_shell_serv(struct vty *);
383 extern void vty_hello(struct vty *);
384
385 /* ^Z / SIGTSTP handling */
386 extern void vty_stdio_suspend(void);
387 extern void vty_stdio_resume(void);
388 extern void vty_stdio_close(void);
389
390 extern void vty_init_mgmt_fe(void);
391 extern bool vty_mgmt_fe_enabled(void);
392 extern int vty_mgmt_send_config_data(struct vty *vty);
393 extern int vty_mgmt_send_commit_config(struct vty *vty, bool validate_only,
394 bool abort);
395 extern int vty_mgmt_send_get_config(struct vty *vty,
396 Mgmtd__DatastoreId datastore,
397 const char **xpath_list, int num_req);
398 extern int vty_mgmt_send_get_data(struct vty *vty, Mgmtd__DatastoreId datastore,
399 const char **xpath_list, int num_req);
400 extern int vty_mgmt_send_lockds_req(struct vty *vty, Mgmtd__DatastoreId ds_id,
401 bool lock);
402 extern void vty_mgmt_resume_response(struct vty *vty, bool success);
403
404 static inline bool vty_needs_implicit_commit(struct vty *vty)
405 {
406 return (frr_get_cli_mode() == FRR_CLI_CLASSIC
407 ? ((vty->pending_allowed || vty->no_implicit_commit)
408 ? false
409 : true)
410 : false);
411 }
412
413 #ifdef __cplusplus
414 }
415 #endif
416
417 #endif /* _ZEBRA_VTY_H */