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