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