]> git.proxmox.com Git - mirror_frr.git/blame - lib/vty.h
*: Convert `struct event_master` to `struct event_loop`
[mirror_frr.git] / lib / vty.h
CommitLineData
acddc0ed 1// SPDX-License-Identifier: GPL-2.0-or-later
718e3744 2/* Virtual terminal [aka TeletYpe] interface routine
896014f4 3 * Copyright (C) 1997 Kunihiro Ishiguro
896014f4 4 */
718e3744 5
6#ifndef _ZEBRA_VTY_H
7#define _ZEBRA_VTY_H
8
fe6b47b9 9#include <sys/types.h>
061f5d1c
DA
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)
beee9b4a
DS
16#include <pcreposix.h>
17#else
fe6b47b9 18#include <regex.h>
061f5d1c 19#endif /* HAVE_LIBPCRE2_POSIX */
fe6b47b9 20
cb37cb33 21#include "event.h"
1ed72e0b 22#include "log.h"
d227617a 23#include "sockunion.h"
0878c8d4 24#include "qobj.h"
de1a880c 25#include "compiler.h"
1c2facd1 26#include "northbound.h"
0798d276 27#include "zlog_live.h"
1401ee8b 28#include "libfrr.h"
ef43a632 29#include "mgmt_fe_client.h"
b21b19c5 30
5e244469
RW
31#ifdef __cplusplus
32extern "C" {
33#endif
34
a8dfd147
DL
35struct json_object;
36
2af38873 37#define VTY_BUFSIZ 4096
718e3744 38#define VTY_MAXHIST 20
1c2facd1 39#define VTY_MAXDEPTH 8
718e3744 40
0b3eed38 41#define VTY_MAXCFGCHANGES 16
a6233bfc 42
7ab57d19
DS
43struct vty_error {
44 char error_buf[VTY_BUFSIZ];
45 uint32_t line_num;
46};
47
a6233bfc 48struct vty_cfg_change {
8427e0e6 49 char xpath[XPATH_MAXLEN];
a6233bfc
RW
50 enum nb_operation operation;
51 const char *value;
52};
53
43dd8caf
DL
54PREDECL_DLIST(vtys);
55
718e3744 56/* VTY struct. */
d62a17ae 57struct vty {
43dd8caf
DL
58 struct vtys_item itm;
59
d62a17ae 60 /* File descripter of this vty. */
61 int fd;
718e3744 62
d62a17ae 63 /* output FD, to support stdin/stdout combination */
64 int wfd;
c5e69a02 65
2cddf2ff
QY
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
d62a17ae 73 /* Is this vty connect to file or not */
74 enum { VTY_TERM, VTY_FILE, VTY_SHELL, VTY_SHELL_SERV } type;
718e3744 75
d62a17ae 76 /* Node status of this vty */
77 int node;
718e3744 78
d62a17ae 79 /* Failure count */
80 int fail;
718e3744 81
fe6b47b9
QY
82 /* Output filer regex */
83 bool filter;
84 regex_t include;
85
0b42d81a
QY
86 /* Line buffer */
87 struct buffer *lbuf;
88
d62a17ae 89 /* Output buffer. */
90 struct buffer *obuf;
718e3744 91
d62a17ae 92 /* Command input buffer */
93 char *buf;
718e3744 94
d62a17ae 95 /* Command input error buffer */
7ab57d19 96 struct list *error;
5689fe5f 97
d62a17ae 98 /* Command cursor point */
99 int cp;
718e3744 100
d62a17ae 101 /* Command length */
102 int length;
718e3744 103
d62a17ae 104 /* Command max length. */
105 int max;
718e3744 106
d62a17ae 107 /* Histry of command */
108 char *hist[VTY_MAXHIST];
718e3744 109
d62a17ae 110 /* History lookup current point */
111 int hp;
718e3744 112
d62a17ae 113 /* History insert end point */
114 int hindex;
718e3744 115
a6233bfc
RW
116 /* Changes enqueued to be applied in the candidate configuration. */
117 size_t num_cfg_changes;
ef43a632 118 struct nb_cfg_change cfg_changes[VTY_MAXCFGCHANGES];
a6233bfc 119
1c2facd1
RW
120 /* XPath of the current node */
121 int xpath_index;
122 char xpath[VTY_MAXDEPTH][XPATH_MAXLEN];
123
1401ee8b
PS
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
f344c66e
RW
130 /* In configure mode. */
131 bool config;
132
1c2facd1
RW
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
b855e95f 142 /* Dynamic transaction information. */
fd396924
CH
143 bool pending_allowed;
144 bool pending_commit;
ef43a632 145 bool no_implicit_commit;
b855e95f
RW
146 char *pending_cmds_buf;
147 size_t pending_cmds_buflen;
148 size_t pending_cmds_bufpos;
149
fbdc1c0a 150 /* Confirmed-commit timeout and rollback configuration. */
e6685141 151 struct event *t_confirmed_commit_timeout;
fbdc1c0a
RW
152 struct nb_config *confirmed_commit_rollback;
153
d62a17ae 154 /* qobj object ID (replacement for "index") */
155 uint64_t qobj_index;
718e3744 156
d62a17ae 157 /* qobj second-level object ID (replacement for "index_sub") */
158 uint64_t qobj_index_sub;
718e3744 159
d62a17ae 160 /* For escape character. */
161 unsigned char escape;
718e3744 162
d62a17ae 163 /* Current vty status. */
b2dde56b
DL
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];
718e3744 177
0798d276
DL
178 /* live logging target / terminal monitor */
179 struct zlog_live_cfg live_log;
180
d62a17ae 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;
718e3744 185
d62a17ae 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> */
9fc7ebf1 191#define TELNET_NAWS_SB_LEN 5
d62a17ae 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;
718e3744 196
d62a17ae 197 /* Window width/height. */
198 int width;
199 int height;
718e3744 200
d62a17ae 201 /* Configure lines. */
202 int lines;
718e3744 203
d62a17ae 204 /* Read and write thread. */
e6685141
DS
205 struct event *t_read;
206 struct event *t_write;
718e3744 207
d62a17ae 208 /* Timeout seconds and thread. */
209 unsigned long v_timeout;
e6685141 210 struct event *t_timeout;
d227617a 211
d62a17ae 212 /* What address is this vty comming from. */
213 char address[SU_ADDRSTRLEN];
2071aa0e
DL
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];
ef43a632
CH
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;
718e3744 226};
227
d62a17ae 228static inline void vty_push_context(struct vty *vty, int node, uint64_t id)
0878c8d4 229{
d62a17ae 230 vty->node = node;
231 vty->qobj_index = id;
0878c8d4
DL
232}
233
3c5070be
DL
234/* note: VTY_PUSH_CONTEXT(..., NULL) doesn't work, since it will try to
235 * dereference "NULL->qobj_node.nid" */
d62a17ae 236#define VTY_PUSH_CONTEXT(nodeval, ptr) \
a50b7ceb 237 vty_push_context(vty, nodeval, QOBJ_ID_0SAFE(ptr))
d62a17ae 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); \
6a098b3a 244 } while (0)
0878c8d4
DL
245
246/* can return NULL if context is invalid! */
d62a17ae 247#define VTY_GET_CONTEXT(structname) \
0878c8d4 248 QOBJ_GET_TYPESAFE(vty->qobj_index, structname)
d62a17ae 249#define VTY_GET_CONTEXT_SUB(structname) \
6a098b3a 250 QOBJ_GET_TYPESAFE(vty->qobj_index_sub, structname)
0878c8d4
DL
251
252/* will return if ptr is NULL. */
d62a17ae 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; \
0878c8d4
DL
259 }
260
261/* struct structname *ptr = <context>; ptr will never be NULL. */
d62a17ae 262#define VTY_DECLVAR_CONTEXT(structname, ptr) \
263 struct structname *ptr = VTY_GET_CONTEXT(structname); \
0878c8d4 264 VTY_CHECK_CONTEXT(ptr);
d62a17ae 265#define VTY_DECLVAR_CONTEXT_SUB(structname, ptr) \
266 struct structname *ptr = VTY_GET_CONTEXT_SUB(structname); \
6a098b3a 267 VTY_CHECK_CONTEXT(ptr);
a3d826f0 268#define VTY_DECLVAR_INSTANCE_CONTEXT(structname, ptr) \
996c9314
LB
269 if (vty->qobj_index == 0) \
270 return CMD_NOT_MY_INSTANCE; \
a3d826f0
CS
271 struct structname *ptr = VTY_GET_CONTEXT(structname); \
272 VTY_CHECK_CONTEXT(ptr);
0878c8d4 273
0cbed951
DL
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
1c2facd1
RW
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 { \
caaa8b9c
RW
301 if (vty->type != VTY_FILE && !vty->private_config \
302 && vty->xpath_index > 0 \
1c2facd1
RW
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
d62a17ae 312struct vty_arg {
313 const char *name;
314 const char *value;
315 const char **argv;
316 int argc;
eac6e3f0
RW
317};
318
718e3744 319/* Integrated configuration file. */
e20dc2ba 320#define INTEGRATE_DEFAULT_CONFIG "frr.conf"
718e3744 321
718e3744 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
ef43a632
CH
337extern struct nb_config *vty_mgmt_candidate_config;
338
718e3744 339/* Prototypes. */
cd9d0537 340extern void vty_init(struct event_loop *m, bool do_command_logging);
d62a17ae 341extern void vty_init_vtysh(void);
342extern void vty_terminate(void);
343extern void vty_reset(void);
344extern struct vty *vty_new(void);
154b9e8f 345extern struct vty *vty_stdio(void (*atclose)(int isexit));
2071aa0e
DL
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 */
afb35622
DL
352extern int vty_out(struct vty *, const char *, ...) PRINTFRR(2, 3);
353extern void vty_frame(struct vty *, const char *, ...) PRINTFRR(2, 3);
2071aa0e 354extern void vty_endframe(struct vty *, const char *);
30f0195d 355extern bool vty_set_include(struct vty *vty, const char *regexp);
a8dfd147
DL
356/* returns CMD_SUCCESS so you can do a one-line "return vty_json(...)"
357 * NULL check and json_object_free() is included.
d7c6467b
DS
358 *
359 * _no_pretty means do not add a bunch of newlines and dump the output
360 * as densely as possible.
a8dfd147
DL
361 */
362extern int vty_json(struct vty *vty, struct json_object *json);
d7c6467b 363extern int vty_json_no_pretty(struct vty *vty, struct json_object *json);
6333c548 364extern void vty_json_empty(struct vty *vty);
b2dde56b
DL
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 */
368extern void vty_pass_fd(struct vty *vty, int fd);
369
1c2facd1
RW
370extern bool vty_read_config(struct nb_config *config, const char *config_file,
371 char *config_default_dir);
d62a17ae 372extern void vty_time_print(struct vty *, int);
373extern void vty_serv_sock(const char *, unsigned short, const char *);
374extern void vty_close(struct vty *);
375extern char *vty_get_cwd(void);
763725cd 376extern void vty_update_xpath(const char *oldpath, const char *newpath);
f344c66e
RW
377extern int vty_config_enter(struct vty *vty, bool private_config,
378 bool exclusive);
379extern void vty_config_exit(struct vty *);
791ded4a 380extern int vty_config_node_exit(struct vty *);
d62a17ae 381extern int vty_shell(struct vty *);
382extern int vty_shell_serv(struct vty *);
383extern void vty_hello(struct vty *);
718e3744 384
154b9e8f
DL
385/* ^Z / SIGTSTP handling */
386extern void vty_stdio_suspend(void);
387extern void vty_stdio_resume(void);
388extern void vty_stdio_close(void);
389
ef43a632
CH
390extern void vty_init_mgmt_fe(void);
391extern bool vty_mgmt_fe_enabled(void);
392extern int vty_mgmt_send_config_data(struct vty *vty);
393extern int vty_mgmt_send_commit_config(struct vty *vty, bool validate_only,
394 bool abort);
39c329bb
CH
395extern int vty_mgmt_send_get_config(struct vty *vty,
396 Mgmtd__DatastoreId datastore,
ef43a632
CH
397 const char **xpath_list, int num_req);
398extern int vty_mgmt_send_get_data(struct vty *vty, Mgmtd__DatastoreId datastore,
399 const char **xpath_list, int num_req);
400extern int vty_mgmt_send_lockds_req(struct vty *vty, Mgmtd__DatastoreId ds_id,
401 bool lock);
1401ee8b
PS
402extern void vty_mgmt_resume_response(struct vty *vty, bool success);
403
404static inline bool vty_needs_implicit_commit(struct vty *vty)
405{
406 return (frr_get_cli_mode() == FRR_CLI_CLASSIC
39c329bb
CH
407 ? ((vty->pending_allowed || vty->no_implicit_commit)
408 ? false
409 : true)
410 : false);
1401ee8b 411}
ef43a632 412
5e244469
RW
413#ifdef __cplusplus
414}
415#endif
416
718e3744 417#endif /* _ZEBRA_VTY_H */