]> git.proxmox.com Git - mirror_frr.git/blame - vtysh/vtysh.c
Merge pull request #2823 from opensourcerouting/snap-staticd
[mirror_frr.git] / vtysh / vtysh.c
CommitLineData
718e3744 1/* Virtual terminal interface shell.
2 * Copyright (C) 2000 Kunihiro Ishiguro
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
896014f4
DL
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
718e3744 19 */
20
21#include <zebra.h>
22
23#include <sys/un.h>
24#include <setjmp.h>
25#include <sys/wait.h>
26#include <sys/resource.h>
27#include <sys/stat.h>
28
29#include <readline/readline.h>
30#include <readline/history.h>
31
7c8ff89e
DS
32#include <dirent.h>
33#include <stdio.h>
34#include <string.h>
35
4201dd11 36#include "linklist.h"
718e3744 37#include "command.h"
38#include "memory.h"
039f3a34 39#include "filter.h"
718e3744 40#include "vtysh/vtysh.h"
6099b3b5 41#include "log.h"
320da874 42#include "bgpd/bgp_vty.h"
13460c44 43#include "ns.h"
cd2a8a42 44#include "vrf.h"
eb05883f 45#include "libfrr.h"
26fbe472 46#include "command_graph.h"
2cddf2ff 47#include "frrstr.h"
718e3744 48
4a1ab8e4
DL
49DEFINE_MTYPE_STATIC(MVTYSH, VTYSH_CMD, "Vtysh cmd copy")
50
718e3744 51/* Struct VTY. */
52struct vty *vty;
53
54/* VTY shell pager name. */
55char *vtysh_pager_name = NULL;
56
2cddf2ff 57/* VTY shell client structure */
d62a17ae 58struct vtysh_client {
59 int fd;
60 const char *name;
61 int flag;
62 char path[MAXPATHLEN];
63 struct vtysh_client *next;
7c8ff89e
DS
64};
65
2cddf2ff
QY
66/* Some utility functions for working on vtysh-specific vty tasks */
67
68static FILE *vty_open_pager(struct vty *vty)
69{
70 if (vty->is_paged)
71 return vty->of;
72
5d806ec6
QY
73 if (!vtysh_pager_name)
74 return NULL;
75
2cddf2ff
QY
76 vty->of_saved = vty->of;
77 vty->of = popen(vtysh_pager_name, "w");
78 if (vty->of == NULL) {
79 vty->of = vty->of_saved;
80 perror("popen");
81 exit(1);
82 }
83
84 vty->is_paged = true;
85
86 return vty->of;
87}
88
89static int vty_close_pager(struct vty *vty)
90{
91 if (!vty->is_paged)
92 return 0;
93
94 fflush(vty->of);
95 if (pclose(vty->of) == -1) {
96 perror("pclose");
97 exit(1);
98 }
99
100 vty->of = vty->of_saved;
101 vty->is_paged = false;
102
103 return 0;
104}
105
106void vtysh_pager_init(void)
107{
108 char *pager_defined;
109
110 pager_defined = getenv("VTYSH_PAGER");
111
112 if (pager_defined)
113 vtysh_pager_name = strdup(pager_defined);
114 else
115 vtysh_pager_name = strdup(VTYSH_PAGER);
116}
117
118/* --- */
119
d62a17ae 120struct vtysh_client vtysh_client[] = {
121 {.fd = -1, .name = "zebra", .flag = VTYSH_ZEBRA, .next = NULL},
122 {.fd = -1, .name = "ripd", .flag = VTYSH_RIPD, .next = NULL},
123 {.fd = -1, .name = "ripngd", .flag = VTYSH_RIPNGD, .next = NULL},
124 {.fd = -1, .name = "ospfd", .flag = VTYSH_OSPFD, .next = NULL},
125 {.fd = -1, .name = "ospf6d", .flag = VTYSH_OSPF6D, .next = NULL},
126 {.fd = -1, .name = "ldpd", .flag = VTYSH_LDPD, .next = NULL},
127 {.fd = -1, .name = "bgpd", .flag = VTYSH_BGPD, .next = NULL},
128 {.fd = -1, .name = "isisd", .flag = VTYSH_ISISD, .next = NULL},
129 {.fd = -1, .name = "pimd", .flag = VTYSH_PIMD, .next = NULL},
130 {.fd = -1, .name = "nhrpd", .flag = VTYSH_NHRPD, .next = NULL},
131 {.fd = -1, .name = "eigrpd", .flag = VTYSH_EIGRPD, .next = NULL},
132 {.fd = -1, .name = "babeld", .flag = VTYSH_BABELD, .next = NULL},
996c9314 133 {.fd = -1, .name = "sharpd", .flag = VTYSH_SHARPD, .next = NULL},
d62a17ae 134 {.fd = -1, .name = "watchfrr", .flag = VTYSH_WATCHFRR, .next = NULL},
e5c83d9b 135 {.fd = -1, .name = "pbrd", .flag = VTYSH_PBRD, .next = NULL},
7e24fdf3 136 {.fd = -1, .name = "staticd", .flag = VTYSH_STATICD, .next = NULL},
b1aa147d 137};
138
d62a17ae 139enum vtysh_write_integrated vtysh_write_integrated =
140 WRITE_INTEGRATED_UNSPECIFIED;
e7168df4 141
67736451
MS
142static int vtysh_reconnect(struct vtysh_client *vclient);
143
d62a17ae 144static void vclient_close(struct vtysh_client *vclient)
718e3744 145{
d62a17ae 146 if (vclient->fd >= 0) {
2cddf2ff 147 vty_out(vty,
d62a17ae 148 "Warning: closing connection to %s because of an I/O error!\n",
149 vclient->name);
150 close(vclient->fd);
67736451
MS
151 /* indicate as candidate for reconnect */
152 vclient->fd = VTYSH_WAS_ACTIVE;
d62a17ae 153 }
718e3744 154}
155
2cddf2ff
QY
156/*
157 * Send a CLI command to a client and read the response.
158 *
159 * Output will be printed to vty->of. If you want to suppress output, set that
160 * to NULL.
161 *
162 * vclient
163 * the client to send the command to
164 *
165 * line
166 * the command to send
167 *
168 * callback
169 * if non-null, this will be called with each line of output received from
170 * the client passed in the second parameter
171 *
172 * cbarg
173 * optional first argument to pass to callback
174 *
175 * Returns:
176 * a status code
177 */
d62a17ae 178static int vtysh_client_run(struct vtysh_client *vclient, const char *line,
2cddf2ff 179 void (*callback)(void *, const char *), void *cbarg)
d62a17ae 180{
181 int ret;
182 char stackbuf[4096];
183 char *buf = stackbuf;
184 size_t bufsz = sizeof(stackbuf);
185 char *bufvalid, *end = NULL;
186 char terminator[3] = {0, 0, 0};
187
67736451
MS
188 /* vclinet was previously active, try to reconnect */
189 if (vclient->fd == VTYSH_WAS_ACTIVE) {
190 ret = vtysh_reconnect(vclient);
191 if (ret < 0)
192 goto out_err;
193 }
194
d62a17ae 195 if (vclient->fd < 0)
196 return CMD_SUCCESS;
197
198 ret = write(vclient->fd, line, strlen(line) + 1);
67736451
MS
199 if (ret <= 0) {
200 /* close connection and try to reconnect */
201 vclient_close(vclient);
202 ret = vtysh_reconnect(vclient);
203 if (ret < 0)
204 goto out_err;
205 /* retry line */
206 ret = write(vclient->fd, line, strlen(line) + 1);
207 if (ret <= 0)
208 goto out_err;
209 }
d62a17ae 210
211 bufvalid = buf;
212 do {
213 ssize_t nread =
acf59d7a 214 read(vclient->fd, bufvalid, buf + bufsz - bufvalid - 1);
d62a17ae 215
216 if (nread < 0 && (errno == EINTR || errno == EAGAIN))
217 continue;
218
219 if (nread <= 0) {
2cddf2ff 220 vty_out(vty, "vtysh: error reading from %s: %s (%d)",
d62a17ae 221 vclient->name, safe_strerror(errno), errno);
222 goto out_err;
223 }
224
225 bufvalid += nread;
226
acf59d7a
QY
227 /* Null terminate so we may pass this to *printf later. */
228 bufvalid[0] = '\0';
229
fe53c680
QY
230 /*
231 * We expect string output from daemons, so instead of looking
232 * for the full 3 null bytes of the terminator, we check for
233 * just one instead and assume it is the first byte of the
234 * terminator. The presence of the full terminator is checked
235 * later.
236 */
08ee8e22 237 if (bufvalid - buf >= 4)
fe53c680 238 end = memmem(bufvalid - 4, 4, "\0", 1);
d62a17ae 239
08ee8e22
QY
240 /*
241 * calculate # bytes we have, up to & not including the
242 * terminator if present
243 */
244 size_t textlen = (end ? end : bufvalid) - buf;
fe53c680 245 bool b = false;
08ee8e22
QY
246
247 /* feed line processing callback if present */
248 while (callback && bufvalid > buf && (end > buf || !end)) {
249 textlen = (end ? end : bufvalid) - buf;
d62a17ae 250 char *eol = memchr(buf, '\n', textlen);
251 if (eol)
252 /* line break */
253 *eol++ = '\0';
254 else if (end == buf)
fe53c680
QY
255 /*
256 * no line break, end of input, no text left
257 * before end; nothing to write
258 */
259 b = true;
d62a17ae 260 else if (end)
fe53c680 261 /* no nl, end of input, but some text left */
d62a17ae 262 eol = end;
acf59d7a 263 else if (bufvalid == buf + bufsz - 1) {
fe53c680
QY
264 /*
265 * no nl, no end of input, no buffer space;
266 * realloc
267 */
268 char *new;
269
270 bufsz *= 2;
271 if (buf == stackbuf) {
272 new = XMALLOC(MTYPE_TMP, bufsz);
273 memcpy(new, stackbuf, sizeof(stackbuf));
274 } else
275 new = XREALLOC(MTYPE_TMP, buf, bufsz);
276
277 bufvalid = bufvalid - buf + new;
278 buf = new;
279 /* if end != NULL, we won't be reading more
280 * data... */
281 assert(end == NULL);
282 b = true;
283 } else
284 b = true;
285
286 if (b)
d62a17ae 287 break;
288
08ee8e22 289 /* eol is at line end now, either \n => \0 or \0\0\0 */
d62a17ae 290 assert(eol && eol <= bufvalid);
291
2cddf2ff
QY
292 if (vty->of)
293 vty_out(vty, "%s\n", buf);
294
295 callback(cbarg, buf);
d62a17ae 296
fe53c680 297 /* shift back data and adjust bufvalid */
d62a17ae 298 memmove(buf, eol, bufvalid - eol);
299 bufvalid -= eol - buf;
300 if (end)
301 end -= eol - buf;
302 }
303
08ee8e22
QY
304 /* else if no callback, dump raw */
305 if (!callback) {
2cddf2ff
QY
306 if (vty->of)
307 vty_out(vty, "%s", buf);
08ee8e22
QY
308 memmove(buf, buf + textlen, bufvalid - buf - textlen);
309 bufvalid -= textlen;
fe53c680
QY
310 if (end)
311 end -= textlen;
312
313 /*
314 * ----------------------------------------------------
315 * At this point `buf` should be in one of two states:
316 * - Empty (i.e. buf == bufvalid)
317 * - Contains up to 4 bytes of the terminator
318 * ----------------------------------------------------
319 */
320 assert(((buf == bufvalid)
321 || (bufvalid - buf <= 4 && buf[0] == 0x00)));
08ee8e22
QY
322 }
323
fe53c680
QY
324 /* if we have the terminator, break */
325 if (end && bufvalid - buf == 4) {
326 assert(!memcmp(buf, terminator, 3));
327 ret = buf[3];
328 break;
d62a17ae 329 }
fe53c680
QY
330
331 } while (true);
d62a17ae 332 goto out;
7526a182
DL
333
334out_err:
d62a17ae 335 vclient_close(vclient);
336 ret = CMD_SUCCESS;
7526a182 337out:
d62a17ae 338 if (buf != stackbuf)
339 XFREE(MTYPE_TMP, buf);
340 return ret;
718e3744 341}
342
d62a17ae 343static int vtysh_client_run_all(struct vtysh_client *head_client,
2cddf2ff 344 const char *line, int continue_on_err,
d62a17ae 345 void (*callback)(void *, const char *),
346 void *cbarg)
7c8ff89e 347{
d62a17ae 348 struct vtysh_client *client;
349 int rc, rc_all = CMD_SUCCESS;
a3d826f0 350 int correct_instance = 0, wrong_instance = 0;
7c8ff89e 351
d62a17ae 352 for (client = head_client; client; client = client->next) {
2cddf2ff 353 rc = vtysh_client_run(client, line, callback, cbarg);
a3d826f0
CS
354 if (rc == CMD_NOT_MY_INSTANCE) {
355 wrong_instance++;
356 continue;
357 }
91fd1b8d
QY
358 if (client->fd > 0)
359 correct_instance++;
d62a17ae 360 if (rc != CMD_SUCCESS) {
361 if (!continue_on_err)
362 return rc;
363 rc_all = rc;
364 }
365 }
2cddf2ff
QY
366 if (wrong_instance && !correct_instance) {
367 vty_out(vty,
b4e197b5 368 "%% [%s]: command ignored as it targets an instance that is not running\n",
a3d826f0 369 head_client->name);
ac28e4ec 370 rc_all = CMD_WARNING_CONFIG_FAILED;
a3d826f0 371 }
d62a17ae 372 return rc_all;
7526a182
DL
373}
374
2cddf2ff
QY
375/*
376 * Execute command against all daemons.
377 *
378 * head_client
379 * where to start walking in the daemon list
380 *
381 * line
382 * the specific command to execute
383 *
384 * Returns:
385 * a status code
386 */
d62a17ae 387static int vtysh_client_execute(struct vtysh_client *head_client,
2cddf2ff 388 const char *line)
7526a182 389{
2cddf2ff 390 return vtysh_client_run_all(head_client, line, 0, NULL, NULL);
7526a182
DL
391}
392
2cddf2ff
QY
393/*
394 * Retrieve all running config from daemons and parse it with the vtysh config
395 * parser. Returned output is not displayed to the user.
396 *
397 * head_client
398 * where to start walking in the daemon list
399 *
400 * line
401 * the specific command to execute
402 */
d62a17ae 403static void vtysh_client_config(struct vtysh_client *head_client, char *line)
7526a182 404{
a23587fa
DL
405 /* watchfrr currently doesn't load any config, and has some hardcoded
406 * settings that show up in "show run". skip it here (for now at
407 * least) so we don't get that mangled up in config-write.
408 */
409 if (head_client->flag == VTYSH_WATCHFRR)
410 return;
411
2cddf2ff
QY
412 /* suppress output to user */
413 vty->of_saved = vty->of;
414 vty->of = NULL;
415 vtysh_client_run_all(head_client, line, 1, vtysh_config_parse_line,
416 NULL);
417 vty->of = vty->of_saved;
718e3744 418}
419
420/* Command execution over the vty interface. */
d62a17ae 421static int vtysh_execute_func(const char *line, int pager)
422{
423 int ret, cmd_stat;
d7c0a89a 424 unsigned int i;
d62a17ae 425 vector vline;
426 const struct cmd_element *cmd;
d62a17ae 427 int tried = 0;
428 int saved_ret, saved_node;
429
430 /* Split readline string up into the vector. */
431 vline = cmd_make_strvec(line);
432
433 if (vline == NULL)
434 return CMD_SUCCESS;
435
86b28610
LB
436 if (user_mode) {
437 if (strncmp("en", vector_slot(vline, 0), 2) == 0) {
9d144835 438 cmd_free_strvec(vline);
2cddf2ff 439 vty_out(vty, "%% Command not allowed: enable\n");
86b28610
LB
440 return CMD_WARNING;
441 }
442 }
443
2cddf2ff 444 saved_ret = ret = cmd_execute(vty, line, &cmd, 1);
d62a17ae 445 saved_node = vty->node;
446
76015847
QY
447 /*
448 * If command doesn't succeeded in current node, try to walk up in node
449 * tree. Changing vty->node is enough to try it just out without actual
450 * walkup in the vtysh.
451 */
d62a17ae 452 while (ret != CMD_SUCCESS && ret != CMD_SUCCESS_DAEMON
453 && ret != CMD_WARNING && ret != CMD_WARNING_CONFIG_FAILED
454 && vty->node > CONFIG_NODE) {
455 vty->node = node_parent(vty->node);
2cddf2ff 456 ret = cmd_execute(vty, line, &cmd, 1);
d62a17ae 457 tried++;
65efcfce 458 }
d62a17ae 459
460 vty->node = saved_node;
461
76015847
QY
462 /*
463 * If command succeeded in any other node than current (tried > 0) we
464 * have to move into node in the vtysh where it succeeded.
465 */
d62a17ae 466 if (ret == CMD_SUCCESS || ret == CMD_SUCCESS_DAEMON
467 || ret == CMD_WARNING) {
468 if ((saved_node == BGP_VPNV4_NODE
469 || saved_node == BGP_VPNV6_NODE
470 || saved_node == BGP_IPV4_NODE
471 || saved_node == BGP_IPV6_NODE
e7d78d0f
PG
472 || saved_node == BGP_FLOWSPECV4_NODE
473 || saved_node == BGP_FLOWSPECV6_NODE
d62a17ae 474 || saved_node == BGP_IPV4M_NODE
475 || saved_node == BGP_IPV4L_NODE
476 || saved_node == BGP_IPV6L_NODE
477 || saved_node == BGP_IPV6M_NODE
983bd6f7
RW
478 || saved_node == BGP_EVPN_NODE
479 || saved_node == LDP_IPV4_NODE
480 || saved_node == LDP_IPV6_NODE)
d62a17ae 481 && (tried == 1)) {
482 vtysh_execute("exit-address-family");
483 } else if ((saved_node == BGP_EVPN_VNI_NODE) && (tried == 1)) {
484 vtysh_execute("exit-vni");
485 } else if (saved_node == BGP_VRF_POLICY_NODE && (tried == 1)) {
486 vtysh_execute("exit-vrf-policy");
487 } else if ((saved_node == BGP_VNC_DEFAULTS_NODE
488 || saved_node == BGP_VNC_NVE_GROUP_NODE
489 || saved_node == BGP_VNC_L2_GROUP_NODE)
490 && (tried == 1)) {
491 vtysh_execute("exit-vnc");
16d6ea59
QY
492 } else if (saved_node == VRF_NODE && (tried == 1)) {
493 vtysh_execute("exit-vrf");
d62a17ae 494 } else if ((saved_node == KEYCHAIN_KEY_NODE
495 || saved_node == LDP_PSEUDOWIRE_NODE
496 || saved_node == LDP_IPV4_IFACE_NODE
b9c7bc5a 497 || saved_node == LDP_IPV6_IFACE_NODE)
d62a17ae 498 && (tried == 1)) {
499 vtysh_execute("exit");
500 } else if (tried) {
501 vtysh_execute("end");
502 vtysh_execute("configure terminal");
503 }
13bfca7a 504 }
76015847
QY
505 /*
506 * If command didn't succeed in any node, continue with return value
507 * from first try.
508 */
d62a17ae 509 else if (tried) {
510 ret = saved_ret;
13bfca7a 511 }
718e3744 512
d62a17ae 513 cmd_free_strvec(vline);
514
515 cmd_stat = ret;
516 switch (ret) {
517 case CMD_WARNING:
518 case CMD_WARNING_CONFIG_FAILED:
519 if (vty->type == VTY_FILE)
2cddf2ff 520 vty_out(vty, "Warning...\n");
d62a17ae 521 break;
522 case CMD_ERR_AMBIGUOUS:
2cddf2ff 523 vty_out(vty, "%% Ambiguous command: %s\n", line);
d62a17ae 524 break;
525 case CMD_ERR_NO_MATCH:
2cddf2ff 526 vty_out(vty, "%% Unknown command: %s\n", line);
d62a17ae 527 break;
528 case CMD_ERR_INCOMPLETE:
2cddf2ff 529 vty_out(vty, "%% Command incomplete: %s\n", line);
d62a17ae 530 break;
531 case CMD_SUCCESS_DAEMON: {
193a5a95
QY
532 /*
533 * FIXME: Don't open pager for exit commands. popen() causes
534 * problems if exited from vtysh at all. This hack shouldn't
535 * cause any problem but is really ugly.
536 */
2cddf2ff
QY
537 if (pager && strncmp(line, "exit", 4))
538 vty_open_pager(vty);
d62a17ae 539
540 if (!strcmp(cmd->string, "configure terminal")) {
541 for (i = 0; i < array_size(vtysh_client); i++) {
542 cmd_stat = vtysh_client_execute(
2cddf2ff 543 &vtysh_client[i], line);
d62a17ae 544 if (cmd_stat == CMD_WARNING)
545 break;
546 }
547
548 if (cmd_stat) {
549 line = "end";
550 vline = cmd_make_strvec(line);
551
a0c14c54 552
553 if (vline == NULL) {
554 if (vty->is_paged)
555 vty_close_pager(vty);
d62a17ae 556 return CMD_SUCCESS;
557 }
558
559 ret = cmd_execute_command(vline, vty, &cmd, 1);
560 cmd_free_strvec(vline);
561 if (ret != CMD_SUCCESS_DAEMON)
562 break;
563 } else if (cmd->func) {
564 (*cmd->func)(cmd, vty, 0, NULL);
565 break;
566 }
b094d260 567 }
718e3744 568
d62a17ae 569 cmd_stat = CMD_SUCCESS;
2a191994 570 struct vtysh_client *vc;
d62a17ae 571 for (i = 0; i < array_size(vtysh_client); i++) {
572 if (cmd->daemon & vtysh_client[i].flag) {
67736451
MS
573 if (vtysh_client[i].fd < 0
574 && (cmd->daemon == vtysh_client[i].flag)) {
575 for (vc = &vtysh_client[i]; vc;
576 vc = vc->next)
577 if (vc->fd < 0)
578 vtysh_reconnect(vc);
579 }
cde69cc2
QY
580 if (vtysh_client[i].fd < 0
581 && (cmd->daemon == vtysh_client[i].flag)) {
2a191994
QY
582 bool any_inst = false;
583 for (vc = &vtysh_client[i]; vc;
584 vc = vc->next)
585 any_inst = any_inst
586 || (vc->fd > 0);
91fd1b8d 587 if (!any_inst) {
2a191994
QY
588 fprintf(stderr,
589 "%s is not running\n",
590 vtysh_client[i].name);
91fd1b8d
QY
591 continue;
592 }
cde69cc2 593 }
d62a17ae 594 cmd_stat = vtysh_client_execute(
2cddf2ff 595 &vtysh_client[i], line);
d62a17ae 596 if (cmd_stat != CMD_SUCCESS)
597 break;
598 }
599 }
b1aa147d 600 if (cmd_stat != CMD_SUCCESS)
d62a17ae 601 break;
602
603 if (cmd->func)
604 (*cmd->func)(cmd, vty, 0, NULL);
605 }
718e3744 606 }
2cddf2ff
QY
607 if (vty->is_paged)
608 vty_close_pager(vty);
609
d62a17ae 610 return cmd_stat;
718e3744 611}
612
d62a17ae 613int vtysh_execute_no_pager(const char *line)
718e3744 614{
d62a17ae 615 return vtysh_execute_func(line, 0);
718e3744 616}
617
d62a17ae 618int vtysh_execute(const char *line)
718e3744 619{
d62a17ae 620 return vtysh_execute_func(line, 1);
718e3744 621}
622
d62a17ae 623static char *trim(char *s)
a5b89524 624{
d62a17ae 625 size_t size;
626 char *end;
a5b89524 627
d62a17ae 628 size = strlen(s);
a5b89524 629
d62a17ae 630 if (!size)
631 return s;
a5b89524 632
d62a17ae 633 end = s + size - 1;
a7ce0ad1 634 while (end >= s && isspace((int)*end))
d62a17ae 635 end--;
636 *(end + 1) = '\0';
a5b89524 637
a7ce0ad1 638 while (*s && isspace((int)*s))
d62a17ae 639 s++;
a5b89524 640
d62a17ae 641 return s;
a5b89524
DW
642}
643
d62a17ae 644int vtysh_mark_file(const char *filename)
0846286b 645{
d62a17ae 646 struct vty *vty;
647 FILE *confp = NULL;
648 int ret;
649 vector vline;
650 int tried = 0;
651 const struct cmd_element *cmd;
652 int saved_ret, prev_node;
653 int lineno = 0;
654 char *vty_buf_copy = NULL;
655 char *vty_buf_trimmed = NULL;
0846286b 656
d62a17ae 657 if (strncmp("-", filename, 1) == 0)
658 confp = stdin;
659 else
660 confp = fopen(filename, "r");
0846286b 661
d62a17ae 662 if (confp == NULL) {
663 fprintf(stderr, "%% Can't open config file %s due to '%s'.\n",
664 filename, safe_strerror(errno));
665 return (CMD_ERR_NO_FILE);
666 }
0846286b 667
d62a17ae 668 vty = vty_new();
4a9746fd 669 vty->wfd = STDERR_FILENO;
d62a17ae 670 vty->type = VTY_TERM;
671 vty->node = CONFIG_NODE;
0846286b 672
d62a17ae 673 vtysh_execute_no_pager("enable");
674 vtysh_execute_no_pager("configure terminal");
675 vty_buf_copy = XCALLOC(MTYPE_VTYSH_CMD, VTY_BUFSIZ);
0846286b 676
d62a17ae 677 while (fgets(vty->buf, VTY_BUFSIZ, confp)) {
678 lineno++;
679 tried = 0;
680 strcpy(vty_buf_copy, vty->buf);
681 vty_buf_trimmed = trim(vty_buf_copy);
0846286b 682
a701f7ea
RW
683 switch (vty->node) {
684 case LDP_IPV4_IFACE_NODE:
685 if (strncmp(vty_buf_copy, " ", 3)) {
2cddf2ff 686 vty_out(vty, " end\n");
a701f7ea
RW
687 vty->node = LDP_IPV4_NODE;
688 }
689 break;
690 case LDP_IPV6_IFACE_NODE:
691 if (strncmp(vty_buf_copy, " ", 3)) {
2cddf2ff 692 vty_out(vty, " end\n");
a701f7ea
RW
693 vty->node = LDP_IPV6_NODE;
694 }
695 break;
696 case LDP_PSEUDOWIRE_NODE:
697 if (strncmp(vty_buf_copy, " ", 2)) {
2cddf2ff 698 vty_out(vty, " end\n");
a701f7ea
RW
699 vty->node = LDP_L2VPN_NODE;
700 }
701 break;
702 default:
703 break;
704 }
705
d62a17ae 706 if (vty_buf_trimmed[0] == '!' || vty_buf_trimmed[0] == '#') {
2cddf2ff 707 vty_out(vty, "%s", vty->buf);
d62a17ae 708 continue;
709 }
0846286b 710
d62a17ae 711 /* Split readline string up into the vector. */
712 vline = cmd_make_strvec(vty->buf);
0846286b 713
d62a17ae 714 if (vline == NULL) {
2cddf2ff 715 vty_out(vty, "%s", vty->buf);
d62a17ae 716 continue;
717 }
0846286b 718
76015847
QY
719 /*
720 * Ignore the "end" lines, we will generate these where
721 * appropriate
722 */
d62a17ae 723 if (strlen(vty_buf_trimmed) == 3
724 && strncmp("end", vty_buf_trimmed, 3) == 0) {
44f12f20 725 cmd_free_strvec(vline);
d62a17ae 726 continue;
727 }
0846286b 728
d62a17ae 729 prev_node = vty->node;
730 saved_ret = ret = cmd_execute_command_strict(vline, vty, &cmd);
731
76015847
QY
732 /*
733 * If command doesn't succeeded in current node, try to walk up
734 * in node tree. Changing vty->node is enough to try it just
735 * out without actual walkup in the vtysh.
736 */
d62a17ae 737 while (ret != CMD_SUCCESS && ret != CMD_SUCCESS_DAEMON
738 && ret != CMD_WARNING && ret != CMD_WARNING_CONFIG_FAILED
739 && vty->node > CONFIG_NODE) {
740 vty->node = node_parent(vty->node);
741 ret = cmd_execute_command_strict(vline, vty, &cmd);
742 tried++;
743 }
0846286b 744
76015847
QY
745 /*
746 * If command succeeded in any other node than current (tried >
747 * 0) we have to move into node in the vtysh where it
748 * succeeded.
749 */
d62a17ae 750 if (ret == CMD_SUCCESS || ret == CMD_SUCCESS_DAEMON
751 || ret == CMD_WARNING) {
752 if ((prev_node == BGP_VPNV4_NODE
753 || prev_node == BGP_VPNV6_NODE
754 || prev_node == BGP_IPV4_NODE
755 || prev_node == BGP_IPV6_NODE
e7d78d0f
PG
756 || prev_node == BGP_FLOWSPECV4_NODE
757 || prev_node == BGP_FLOWSPECV6_NODE
d62a17ae 758 || prev_node == BGP_IPV4L_NODE
759 || prev_node == BGP_IPV6L_NODE
760 || prev_node == BGP_IPV4M_NODE
761 || prev_node == BGP_IPV6M_NODE
762 || prev_node == BGP_EVPN_NODE)
763 && (tried == 1)) {
2cddf2ff 764 vty_out(vty, "exit-address-family\n");
d62a17ae 765 } else if ((prev_node == BGP_EVPN_VNI_NODE)
766 && (tried == 1)) {
2cddf2ff 767 vty_out(vty, "exit-vni\n");
b9c7bc5a 768 } else if ((prev_node == KEYCHAIN_KEY_NODE)
d62a17ae 769 && (tried == 1)) {
2cddf2ff 770 vty_out(vty, "exit\n");
d62a17ae 771 } else if (tried) {
2cddf2ff 772 vty_out(vty, "end\n");
d62a17ae 773 }
774 }
76015847
QY
775 /*
776 * If command didn't succeed in any node, continue with return
777 * value from first try.
778 */
d62a17ae 779 else if (tried) {
780 ret = saved_ret;
781 vty->node = prev_node;
782 }
783
784 cmd_free_strvec(vline);
785 switch (ret) {
786 case CMD_WARNING:
787 case CMD_WARNING_CONFIG_FAILED:
788 if (vty->type == VTY_FILE)
789 fprintf(stderr, "line %d: Warning...: %s\n",
790 lineno, vty->buf);
791 fclose(confp);
792 vty_close(vty);
793 XFREE(MTYPE_VTYSH_CMD, vty_buf_copy);
794 return ret;
795 case CMD_ERR_AMBIGUOUS:
796 fprintf(stderr, "line %d: %% Ambiguous command: %s\n",
797 lineno, vty->buf);
798 fclose(confp);
799 vty_close(vty);
800 XFREE(MTYPE_VTYSH_CMD, vty_buf_copy);
801 return CMD_ERR_AMBIGUOUS;
802 case CMD_ERR_NO_MATCH:
803 fprintf(stderr, "line %d: %% Unknown command: %s\n",
804 lineno, vty->buf);
805 fclose(confp);
806 vty_close(vty);
807 XFREE(MTYPE_VTYSH_CMD, vty_buf_copy);
808 return CMD_ERR_NO_MATCH;
809 case CMD_ERR_INCOMPLETE:
810 fprintf(stderr, "line %d: %% Command incomplete: %s\n",
811 lineno, vty->buf);
812 fclose(confp);
813 vty_close(vty);
814 XFREE(MTYPE_VTYSH_CMD, vty_buf_copy);
815 return CMD_ERR_INCOMPLETE;
816 case CMD_SUCCESS:
2cddf2ff 817 vty_out(vty, "%s", vty->buf);
d62a17ae 818 break;
819 case CMD_SUCCESS_DAEMON: {
6d10727a 820 int cmd_stat;
d62a17ae 821
2cddf2ff
QY
822 vty_out(vty, "%s", vty->buf);
823 cmd_stat = vtysh_client_execute(&vtysh_client[0],
824 vty->buf);
d62a17ae 825 if (cmd_stat != CMD_SUCCESS)
826 break;
827
828 if (cmd->func)
829 (*cmd->func)(cmd, vty, 0, NULL);
830 }
831 }
0846286b 832 }
d62a17ae 833 /* This is the end */
2cddf2ff 834 vty_out(vty, "\nend\n");
d62a17ae 835 vty_close(vty);
836 XFREE(MTYPE_VTYSH_CMD, vty_buf_copy);
0846286b 837
d62a17ae 838 if (confp != stdin)
839 fclose(confp);
0846286b 840
d62a17ae 841 return (0);
0846286b
DS
842}
843
718e3744 844/* Configration make from file. */
d62a17ae 845int vtysh_config_from_file(struct vty *vty, FILE *fp)
846{
847 int ret;
848 const struct cmd_element *cmd;
849 int lineno = 0;
76015847 850 /* once we have an error, we remember & return that */
d62a17ae 851 int retcode = CMD_SUCCESS;
852
853 while (fgets(vty->buf, VTY_BUFSIZ, fp)) {
854 lineno++;
855
856 ret = command_config_read_one_line(vty, &cmd, 1);
857
858 switch (ret) {
859 case CMD_WARNING:
860 case CMD_WARNING_CONFIG_FAILED:
861 if (vty->type == VTY_FILE)
862 fprintf(stderr, "line %d: Warning[%d]...: %s\n",
863 lineno, vty->node, vty->buf);
76015847
QY
864 retcode = ret;
865
d62a17ae 866 break;
867 case CMD_ERR_AMBIGUOUS:
868 fprintf(stderr,
869 "line %d: %% Ambiguous command[%d]: %s\n",
870 lineno, vty->node, vty->buf);
76015847 871 retcode = CMD_ERR_AMBIGUOUS;
d62a17ae 872 break;
873 case CMD_ERR_NO_MATCH:
874 fprintf(stderr, "line %d: %% Unknown command[%d]: %s",
875 lineno, vty->node, vty->buf);
76015847 876 retcode = CMD_ERR_NO_MATCH;
d62a17ae 877 break;
878 case CMD_ERR_INCOMPLETE:
879 fprintf(stderr,
880 "line %d: %% Command incomplete[%d]: %s\n",
881 lineno, vty->node, vty->buf);
76015847 882 retcode = CMD_ERR_INCOMPLETE;
d62a17ae 883 break;
884 case CMD_SUCCESS_DAEMON: {
d7c0a89a 885 unsigned int i;
d62a17ae 886 int cmd_stat = CMD_SUCCESS;
887
888 for (i = 0; i < array_size(vtysh_client); i++) {
889 if (cmd->daemon & vtysh_client[i].flag) {
890 cmd_stat = vtysh_client_execute(
2cddf2ff 891 &vtysh_client[i], vty->buf);
d62a17ae 892 /*
893 * CMD_WARNING - Can mean that the
76015847
QY
894 * command was parsed successfully but
895 * it was already entered in a few
896 * spots. As such if we receive a
d62a17ae 897 * CMD_WARNING from a daemon we
76015847
QY
898 * shouldn't stop talking to the other
899 * daemons for the particular command.
d62a17ae 900 */
901 if (cmd_stat != CMD_SUCCESS
902 && cmd_stat != CMD_WARNING) {
903 fprintf(stderr,
904 "line %d: Failure to communicate[%d] to %s, line: %s\n",
905 lineno, cmd_stat,
906 vtysh_client[i].name,
907 vty->buf);
596074af 908 retcode = cmd_stat;
d62a17ae 909 break;
910 }
911 }
912 }
913 if (cmd_stat != CMD_SUCCESS)
914 break;
915
916 if (cmd->func)
917 (*cmd->func)(cmd, vty, 0, NULL);
918 }
919 }
920 }
921
922 return (retcode);
923}
924
a4364a44
RM
925/*
926 * Function processes cli commands terminated with '?' character when entered
927 * through either 'vtysh' or 'vtysh -c' interfaces.
928 */
929static int vtysh_process_questionmark(const char *input, int input_len)
718e3744 930{
a4364a44 931 int ret, width = 0;
d62a17ae 932 unsigned int i;
a4364a44 933 vector vline, describe;
d62a17ae 934 struct cmd_token *token;
718e3744 935
a4364a44
RM
936 if (!input)
937 return 1;
938
939 vline = cmd_make_strvec(input);
718e3744 940
d62a17ae 941 /* In case of '> ?'. */
942 if (vline == NULL) {
943 vline = vector_init(1);
944 vector_set(vline, NULL);
a4364a44 945 } else if (input_len && isspace((int)input[input_len - 1]))
d62a17ae 946 vector_set(vline, NULL);
718e3744 947
80c872f6
RW
948 describe = cmd_describe_command(vline, vty, &ret);
949
d62a17ae 950 /* Ambiguous and no match error. */
951 switch (ret) {
718e3744 952 case CMD_ERR_AMBIGUOUS:
d62a17ae 953 cmd_free_strvec(vline);
80c872f6 954 vector_free(describe);
2cddf2ff
QY
955 vty_out(vty, "%% Ambiguous command.\n");
956 rl_on_new_line();
d62a17ae 957 return 0;
958 break;
718e3744 959 case CMD_ERR_NO_MATCH:
d62a17ae 960 cmd_free_strvec(vline);
9320658c
QY
961 if (describe)
962 vector_free(describe);
2cddf2ff
QY
963 vty_out(vty, "%% There is no matched command.\n");
964 rl_on_new_line();
d62a17ae 965 return 0;
966 break;
718e3744 967 }
7f059ea6 968
d62a17ae 969 /* Get width of command string. */
970 width = 0;
971 for (i = 0; i < vector_active(describe); i++)
972 if ((token = vector_slot(describe, i)) != NULL) {
973 if (token->text[0] == '\0')
974 continue;
7f059ea6 975
d62a17ae 976 int len = strlen(token->text);
1a0f614d 977
d62a17ae 978 if (width < len)
979 width = len;
980 }
1a0f614d 981
d62a17ae 982 for (i = 0; i < vector_active(describe); i++)
983 if ((token = vector_slot(describe, i)) != NULL) {
984 if (!token->desc)
2cddf2ff 985 vty_out(vty, " %-s\n", token->text);
d62a17ae 986 else
2cddf2ff
QY
987 vty_out(vty, " %-*s %s\n", width, token->text,
988 token->desc);
d62a17ae 989
990 if (IS_VARYING_TOKEN(token->type)) {
991 const char *ref = vector_slot(
992 vline, vector_active(vline) - 1);
993
994 vector varcomps = vector_init(VECTOR_MIN_SIZE);
995 cmd_variable_complete(token, ref, varcomps);
996
997 if (vector_active(varcomps) > 0) {
998 int rows, cols;
999 rl_get_screen_size(&rows, &cols);
1000
1001 char *ac = cmd_variable_comp2str(
1002 varcomps, cols);
2cddf2ff 1003 vty_out(vty, "%s\n", ac);
d62a17ae 1004 XFREE(MTYPE_TMP, ac);
1005 }
1006
1007 vector_free(varcomps);
1008 }
1009 }
718e3744 1010
d62a17ae 1011 cmd_free_strvec(vline);
1012 vector_free(describe);
718e3744 1013
a4364a44
RM
1014 return 0;
1015}
1016
1017/*
1018 * Entry point for user commands terminated with '?' character and typed through
1019 * the usual vtysh's stdin interface. This is the function being registered with
1020 * readline() api's.
1021 */
b1599bb6 1022static int vtysh_rl_describe(int a, int b)
a4364a44
RM
1023{
1024 int ret;
1025
2cddf2ff 1026 vty_out(vty, "\n");
a4364a44
RM
1027
1028 ret = vtysh_process_questionmark(rl_line_buffer, rl_end);
d62a17ae 1029 rl_on_new_line();
718e3744 1030
a4364a44
RM
1031 return ret;
1032}
1033
1034/*
1035 * Function in charged of processing vtysh instructions terminating with '?'
1036 * character and received through the 'vtysh -c' interface. If user's
1037 * instruction is well-formatted, we will call the same processing routine
1038 * utilized by the traditional vtysh's stdin interface.
1039 */
1040int vtysh_execute_command_questionmark(char *input)
1041{
1042 int input_len, qmark_count = 0;
1043 const char *str;
1044
1045 if (!(input && *input))
1046 return 1;
1047
1048 /* Finding out question_mark count and strlen */
1049 for (str = input; *str; ++str) {
1050 if (*str == '?')
1051 qmark_count++;
1052 }
1053 input_len = str - input;
1054
1055 /*
1056 * Verify that user's input terminates in '?' and that patterns such as
1057 * 'cmd ? subcmd ?' are prevented.
1058 */
1059 if (qmark_count != 1 || input[input_len - 1] != '?')
1060 return 1;
1061
1062 /*
1063 * Questionmark-processing function is not expecting to receive '?'
1064 * character in input string.
1065 */
1066 input[input_len - 1] = '\0';
1067
1068 return vtysh_process_questionmark(input, input_len - 1);
718e3744 1069}
1070
95e735b5 1071/* Result of cmd_complete_command() call will be stored here
1072 * and used in new_completion() in order to put the space in
1073 * correct places only. */
718e3744 1074int complete_status;
1075
d62a17ae 1076static char *command_generator(const char *text, int state)
718e3744 1077{
d62a17ae 1078 vector vline;
1079 static char **matched = NULL;
1080 static int index = 0;
718e3744 1081
d62a17ae 1082 /* First call. */
1083 if (!state) {
1084 index = 0;
718e3744 1085
d62a17ae 1086 if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
1087 return NULL;
718e3744 1088
d62a17ae 1089 vline = cmd_make_strvec(rl_line_buffer);
1090 if (vline == NULL)
1091 return NULL;
718e3744 1092
d62a17ae 1093 if (rl_end && isspace((int)rl_line_buffer[rl_end - 1]))
1094 vector_set(vline, NULL);
718e3744 1095
d62a17ae 1096 matched = cmd_complete_command(vline, vty, &complete_status);
1097 cmd_free_strvec(vline);
1098 }
718e3744 1099
d62a17ae 1100 if (matched && matched[index])
76015847
QY
1101 /*
1102 * this is free()'d by readline, but we leak 1 count of
1103 * MTYPE_COMPLETION
1104 */
d62a17ae 1105 return matched[index++];
718e3744 1106
d62a17ae 1107 XFREE(MTYPE_TMP, matched);
1108 matched = NULL;
66d29a54 1109
d62a17ae 1110 return NULL;
718e3744 1111}
1112
d62a17ae 1113static char **new_completion(char *text, int start, int end)
718e3744 1114{
d62a17ae 1115 char **matches;
718e3744 1116
d62a17ae 1117 matches = rl_completion_matches(text, command_generator);
718e3744 1118
d62a17ae 1119 if (matches) {
1120 rl_point = rl_end;
1121 if (complete_status != CMD_COMPLETE_FULL_MATCH)
1122 /* only append a space on full match */
1123 rl_completion_append_character = '\0';
1124 }
718e3744 1125
d62a17ae 1126 return matches;
718e3744 1127}
1128
95e735b5 1129/* Vty node structures. */
d62a17ae 1130static struct cmd_node bgp_node = {
9d303b37 1131 BGP_NODE, "%s(config-router)# ",
718e3744 1132};
1133
d62a17ae 1134static struct cmd_node rip_node = {
9d303b37 1135 RIP_NODE, "%s(config-router)# ",
718e3744 1136};
1137
d62a17ae 1138static struct cmd_node isis_node = {
9d303b37 1139 ISIS_NODE, "%s(config-router)# ",
c25e458a 1140};
1141
d62a17ae 1142static struct cmd_node interface_node = {
9d303b37 1143 INTERFACE_NODE, "%s(config-if)# ",
718e3744 1144};
1145
2dd0d726
RW
1146static struct cmd_node pw_node = {
1147 PW_NODE, "%s(config-pw)# ",
1148};
1149
f5d20fdb
PG
1150static struct cmd_node logicalrouter_node = {
1151 LOGICALROUTER_NODE, "%s(config-logical-router)# ",
13460c44
FL
1152};
1153
d62a17ae 1154static struct cmd_node vrf_node = {
9d303b37 1155 VRF_NODE, "%s(config-vrf)# ",
e9d94ea7
DS
1156};
1157
e5c83d9b
DS
1158static struct cmd_node nh_group_node = {
1159 NH_GROUP_NODE,
1160 "%s(config-nh-group)# ",
1161};
1162
d62a17ae 1163static struct cmd_node rmap_node = {RMAP_NODE, "%s(config-route-map)# "};
95e735b5 1164
e5c83d9b
DS
1165static struct cmd_node pbr_map_node = {PBRMAP_NODE, "%s(config-pbr-map)# "};
1166
d62a17ae 1167static struct cmd_node zebra_node = {ZEBRA_NODE, "%s(config-router)# "};
95e735b5 1168
d62a17ae 1169static struct cmd_node bgp_vpnv4_node = {BGP_VPNV4_NODE,
1170 "%s(config-router-af)# "};
95e735b5 1171
d62a17ae 1172static struct cmd_node bgp_vpnv6_node = {BGP_VPNV6_NODE,
1173 "%s(config-router-af)# "};
8ecd3266 1174
e7d78d0f
PG
1175static struct cmd_node bgp_flowspecv4_node = {BGP_FLOWSPECV4_NODE,
1176 "%s(config-router-af)# "};
1177
1178static struct cmd_node bgp_flowspecv6_node = {BGP_FLOWSPECV6_NODE,
1179 "%s(config-router-af)# "};
1180
d62a17ae 1181static struct cmd_node bgp_ipv4_node = {BGP_IPV4_NODE,
1182 "%s(config-router-af)# "};
95e735b5 1183
d62a17ae 1184static struct cmd_node bgp_ipv4m_node = {BGP_IPV4M_NODE,
1185 "%s(config-router-af)# "};
95e735b5 1186
d62a17ae 1187static struct cmd_node bgp_ipv4l_node = {BGP_IPV4L_NODE,
1188 "%s(config-router-af)# "};
f51bae9c 1189
d62a17ae 1190static struct cmd_node bgp_ipv6_node = {BGP_IPV6_NODE,
1191 "%s(config-router-af)# "};
95e735b5 1192
d62a17ae 1193static struct cmd_node bgp_ipv6m_node = {BGP_IPV6M_NODE,
1194 "%s(config-router-af)# "};
57b5b7ed 1195
d62a17ae 1196static struct cmd_node bgp_evpn_node = {BGP_EVPN_NODE,
1197 "%s(config-router-af)# "};
14a227b8 1198
d62a17ae 1199static struct cmd_node bgp_evpn_vni_node = {BGP_EVPN_VNI_NODE,
1200 "%s(config-router-af-vni)# "};
90e60aa7 1201
d62a17ae 1202static struct cmd_node bgp_ipv6l_node = {BGP_IPV6L_NODE,
1203 "%s(config-router-af)# "};
f51bae9c 1204
d62a17ae 1205static struct cmd_node bgp_vnc_defaults_node = {
1206 BGP_VNC_DEFAULTS_NODE, "%s(config-router-vnc-defaults)# "};
65efcfce 1207
d62a17ae 1208static struct cmd_node bgp_vnc_nve_group_node = {
1209 BGP_VNC_NVE_GROUP_NODE, "%s(config-router-vnc-nve-group)# "};
65efcfce 1210
d62a17ae 1211static struct cmd_node bgp_vrf_policy_node = {BGP_VRF_POLICY_NODE,
1212 "%s(config-router-vrf-policy)# "};
5ff06872 1213
d62a17ae 1214static struct cmd_node bgp_vnc_l2_group_node = {
1215 BGP_VNC_L2_GROUP_NODE, "%s(config-router-vnc-l2-group)# "};
65efcfce 1216
d62a17ae 1217static struct cmd_node ospf_node = {OSPF_NODE, "%s(config-router)# "};
1218
1219static struct cmd_node eigrp_node = {EIGRP_NODE, "%s(config-router)# "};
1220
1221static struct cmd_node babel_node = {BABEL_NODE, "%s(config-router)# "};
1222
1223static struct cmd_node ripng_node = {RIPNG_NODE, "%s(config-router)# "};
1224
1225static struct cmd_node ospf6_node = {OSPF6_NODE, "%s(config-ospf6)# "};
1226
1227static struct cmd_node ldp_node = {LDP_NODE, "%s(config-ldp)# "};
1228
1229static struct cmd_node ldp_ipv4_node = {LDP_IPV4_NODE, "%s(config-ldp-af)# "};
1230
1231static struct cmd_node ldp_ipv6_node = {LDP_IPV6_NODE, "%s(config-ldp-af)# "};
1232
1233static struct cmd_node ldp_ipv4_iface_node = {LDP_IPV4_IFACE_NODE,
1234 "%s(config-ldp-af-if)# "};
1235
1236static struct cmd_node ldp_ipv6_iface_node = {LDP_IPV6_IFACE_NODE,
1237 "%s(config-ldp-af-if)# "};
1238
1239static struct cmd_node ldp_l2vpn_node = {LDP_L2VPN_NODE, "%s(config-l2vpn)# "};
1240
1241static struct cmd_node ldp_pseudowire_node = {LDP_PSEUDOWIRE_NODE,
1242 "%s(config-l2vpn-pw)# "};
1243
1244static struct cmd_node keychain_node = {KEYCHAIN_NODE, "%s(config-keychain)# "};
1245
1246static struct cmd_node keychain_key_node = {KEYCHAIN_KEY_NODE,
1247 "%s(config-keychain-key)# "};
1248
1249struct cmd_node link_params_node = {
9d303b37 1250 LINK_PARAMS_NODE, "%s(config-link-params)# ",
95e735b5 1251};
1252
23489cb0 1253#if defined(HAVE_RPKI)
dabecd7c 1254static struct cmd_node rpki_node = {RPKI_NODE, "%s(config-rpki)# ", 1};
23489cb0 1255#endif
dabecd7c 1256
d62a17ae 1257/* Defined in lib/vty.c */
1258extern struct cmd_node vty_node;
1259
1260/* When '^Z' is received from vty, move down to the enable mode. */
1261static int vtysh_end(void)
1262{
1263 switch (vty->node) {
1264 case VIEW_NODE:
1265 case ENABLE_NODE:
1266 /* Nothing to do. */
1267 break;
1268 default:
1269 vty->node = ENABLE_NODE;
1270 break;
1271 }
1272 return CMD_SUCCESS;
1273}
1274
1275DEFUNSH(VTYSH_REALLYALL, vtysh_end_all, vtysh_end_all_cmd, "end",
1276 "End current mode and change to enable mode\n")
7f57883e 1277{
d62a17ae 1278 return vtysh_end();
1279}
7f57883e 1280
d62a17ae 1281DEFUNSH(VTYSH_BGPD, router_bgp, router_bgp_cmd,
1282 "router bgp [(1-4294967295) [<view|vrf> WORD]]",
1283 ROUTER_STR BGP_STR AS_STR
1284 "BGP view\nBGP VRF\n"
1285 "View/VRF name\n")
a616ea5f 1286{
d62a17ae 1287 vty->node = BGP_NODE;
1288 return CMD_SUCCESS;
1289}
a616ea5f 1290
d62a17ae 1291DEFUNSH(VTYSH_BGPD, address_family_vpnv4, address_family_vpnv4_cmd,
1292 "address-family vpnv4 [unicast]",
1293 "Enter Address Family command mode\n"
1294 "Address Family\n"
1295 "Address Family modifier\n")
95e735b5 1296{
d62a17ae 1297 vty->node = BGP_VPNV4_NODE;
1298 return CMD_SUCCESS;
1299}
95e735b5 1300
d62a17ae 1301DEFUNSH(VTYSH_BGPD, address_family_vpnv6, address_family_vpnv6_cmd,
1302 "address-family vpnv6 [unicast]",
1303 "Enter Address Family command mode\n"
1304 "Address Family\n"
1305 "Address Family modifier\n")
95e735b5 1306{
d62a17ae 1307 vty->node = BGP_VPNV6_NODE;
1308 return CMD_SUCCESS;
1309}
95e735b5 1310
d62a17ae 1311DEFUNSH(VTYSH_BGPD, address_family_ipv4, address_family_ipv4_cmd,
1312 "address-family ipv4 [unicast]",
1313 "Enter Address Family command mode\n"
1314 "Address Family\n"
1315 "Address Family Modifier\n")
4fcbf6e2 1316{
d62a17ae 1317 vty->node = BGP_IPV4_NODE;
1318 return CMD_SUCCESS;
1319}
4fcbf6e2 1320
e7d78d0f
PG
1321DEFUNSH(VTYSH_BGPD, address_family_flowspecv4, address_family_flowspecv4_cmd,
1322 "address-family ipv4 flowspec",
1323 "Enter Address Family command mode\n"
1324 "Address Family\n"
1325 "Address Family Modifier\n")
1326{
1327 vty->node = BGP_FLOWSPECV4_NODE;
1328 return CMD_SUCCESS;
1329}
1330
1331DEFUNSH(VTYSH_BGPD, address_family_flowspecv6, address_family_flowspecv6_cmd,
1332 "address-family ipv6 flowspec",
1333 "Enter Address Family command mode\n"
1334 "Address Family\n"
1335 "Address Family Modifier\n")
1336{
1337 vty->node = BGP_FLOWSPECV6_NODE;
1338 return CMD_SUCCESS;
1339}
1340
d62a17ae 1341DEFUNSH(VTYSH_BGPD, address_family_ipv4_multicast,
1342 address_family_ipv4_multicast_cmd, "address-family ipv4 multicast",
1343 "Enter Address Family command mode\n"
1344 "Address Family\n"
1345 "Address Family modifier\n")
4fcbf6e2 1346{
d62a17ae 1347 vty->node = BGP_IPV4M_NODE;
1348 return CMD_SUCCESS;
1349}
4fcbf6e2 1350
d62a17ae 1351DEFUNSH(VTYSH_BGPD, address_family_ipv4_vpn, address_family_ipv4_vpn_cmd,
1352 "address-family ipv4 vpn",
1353 "Enter Address Family command mode\n"
1354 "Address Family\n"
1355 "Address Family modifier\n")
4fcbf6e2 1356{
d62a17ae 1357 vty->node = BGP_VPNV4_NODE;
1358 return CMD_SUCCESS;
1359}
4fcbf6e2 1360
d62a17ae 1361DEFUNSH(VTYSH_BGPD, address_family_ipv4_labeled_unicast,
1362 address_family_ipv4_labeled_unicast_cmd,
1363 "address-family ipv4 labeled-unicast",
1364 "Enter Address Family command mode\n"
1365 "Address Family\n"
1366 "Address Family modifier\n")
4fcbf6e2 1367{
d62a17ae 1368 vty->node = BGP_IPV4L_NODE;
1369 return CMD_SUCCESS;
1370}
4fcbf6e2 1371
d62a17ae 1372DEFUNSH(VTYSH_BGPD, address_family_ipv6, address_family_ipv6_cmd,
1373 "address-family ipv6 [unicast]",
1374 "Enter Address Family command mode\n"
1375 "Address Family\n"
1376 "Address Family modifier\n")
4fcbf6e2 1377{
d62a17ae 1378 vty->node = BGP_IPV6_NODE;
1379 return CMD_SUCCESS;
1380}
4fcbf6e2 1381
d62a17ae 1382DEFUNSH(VTYSH_BGPD, address_family_ipv6_multicast,
1383 address_family_ipv6_multicast_cmd, "address-family ipv6 multicast",
1384 "Enter Address Family command mode\n"
1385 "Address Family\n"
1386 "Address Family modifier\n")
4fcbf6e2 1387{
d62a17ae 1388 vty->node = BGP_IPV6M_NODE;
1389 return CMD_SUCCESS;
1390}
4fcbf6e2 1391
d62a17ae 1392DEFUNSH(VTYSH_BGPD, address_family_ipv6_vpn, address_family_ipv6_vpn_cmd,
1393 "address-family ipv6 vpn",
1394 "Enter Address Family command mode\n"
1395 "Address Family\n"
1396 "Address Family modifier\n")
4fcbf6e2 1397{
d62a17ae 1398 vty->node = BGP_VPNV6_NODE;
1399 return CMD_SUCCESS;
1400}
4fcbf6e2 1401
d62a17ae 1402DEFUNSH(VTYSH_BGPD, address_family_ipv6_labeled_unicast,
1403 address_family_ipv6_labeled_unicast_cmd,
1404 "address-family ipv6 labeled-unicast",
1405 "Enter Address Family command mode\n"
1406 "Address Family\n"
1407 "Address Family modifier\n")
95e735b5 1408{
d62a17ae 1409 vty->node = BGP_IPV6L_NODE;
1410 return CMD_SUCCESS;
1411}
95e735b5 1412
23489cb0 1413#if defined(HAVE_RPKI)
b86543b8
LB
1414DEFUNSH(VTYSH_BGPD,
1415 rpki,
1416 rpki_cmd,
1417 "rpki",
dabecd7c
MR
1418 "Enable rpki and enter rpki configuration mode\n")
1419{
1420 vty->node = RPKI_NODE;
1421 return CMD_SUCCESS;
1422}
1423
b86543b8
LB
1424DEFUNSH(VTYSH_BGPD,
1425 rpki_exit,
1426 rpki_exit_cmd,
1427 "exit",
dabecd7c
MR
1428 "Exit current mode and down to previous mode\n")
1429{
1430 vty->node = CONFIG_NODE;
1431 return CMD_SUCCESS;
1432}
1433
b86543b8
LB
1434DEFUNSH(VTYSH_BGPD,
1435 rpki_quit,
1436 rpki_quit_cmd,
1437 "quit",
dabecd7c
MR
1438 "Exit current mode and down to previous mode\n")
1439{
1440 return rpki_exit(self, vty, argc, argv);
1441}
23489cb0 1442#endif
dabecd7c 1443
d62a17ae 1444DEFUNSH(VTYSH_BGPD, address_family_evpn, address_family_evpn_cmd,
1445 "address-family <l2vpn evpn>",
1446 "Enter Address Family command mode\n"
1447 "Address Family\n"
1448 "Address Family modifier\n")
95e735b5 1449{
d62a17ae 1450 vty->node = BGP_EVPN_NODE;
1451 return CMD_SUCCESS;
1452}
95e735b5 1453
d62a17ae 1454#if defined(HAVE_CUMULUS)
5014d96f
DW
1455DEFUNSH_HIDDEN(VTYSH_BGPD, address_family_evpn2, address_family_evpn2_cmd,
1456 "address-family evpn",
1457 "Enter Address Family command mode\n"
1458 "EVPN Address family\n")
16f1b9ee 1459{
d62a17ae 1460 vty->node = BGP_EVPN_NODE;
1461 return CMD_SUCCESS;
1462}
1463#endif
16f1b9ee 1464
d62a17ae 1465DEFUNSH(VTYSH_BGPD, bgp_evpn_vni, bgp_evpn_vni_cmd, "vni (1-16777215)",
1466 "VXLAN Network Identifier\n"
1467 "VNI number\n")
1468{
1469 vty->node = BGP_EVPN_VNI_NODE;
1470 return CMD_SUCCESS;
1471}
e7168df4 1472
d62a17ae 1473#if defined(ENABLE_BGP_VNC)
1474DEFUNSH(VTYSH_BGPD, vnc_defaults, vnc_defaults_cmd, "vnc defaults",
1475 "VNC/RFP related configuration\n"
1476 "Configure default NVE group\n")
718e3744 1477{
d62a17ae 1478 vty->node = BGP_VNC_DEFAULTS_NODE;
1479 return CMD_SUCCESS;
1480}
1481
1482DEFUNSH(VTYSH_BGPD, vnc_nve_group, vnc_nve_group_cmd, "vnc nve-group NAME",
1483 "VNC/RFP related configuration\n"
1484 "Configure a NVE group\n"
1485 "Group name\n")
718e3744 1486{
d62a17ae 1487 vty->node = BGP_VNC_NVE_GROUP_NODE;
1488 return CMD_SUCCESS;
1489}
1490
1491DEFUNSH(VTYSH_BGPD, vnc_vrf_policy, vnc_vrf_policy_cmd, "vrf-policy NAME",
1492 "Configure a VRF policy group\n"
1493 "Group name\n")
8ecd3266 1494{
d62a17ae 1495 vty->node = BGP_VRF_POLICY_NODE;
1496 return CMD_SUCCESS;
1497}
1498
1499DEFUNSH(VTYSH_BGPD, vnc_l2_group, vnc_l2_group_cmd, "vnc l2-group NAME",
1500 "VNC/RFP related configuration\n"
1501 "Configure a L2 group\n"
1502 "Group name\n")
718e3744 1503{
d62a17ae 1504 vty->node = BGP_VNC_L2_GROUP_NODE;
1505 return CMD_SUCCESS;
718e3744 1506}
d62a17ae 1507#endif
718e3744 1508
d62a17ae 1509DEFUNSH(VTYSH_RIPD, key_chain, key_chain_cmd, "key chain WORD",
1510 "Authentication key management\n"
1511 "Key-chain management\n"
1512 "Key-chain name\n")
718e3744 1513{
d62a17ae 1514 vty->node = KEYCHAIN_NODE;
1515 return CMD_SUCCESS;
39530dfe 1516}
c7f1274b 1517
d62a17ae 1518DEFUNSH(VTYSH_RIPD, key, key_cmd, "key (0-2147483647)",
1519 "Configure a key\n"
1520 "Key identifier number\n")
39530dfe 1521{
d62a17ae 1522 vty->node = KEYCHAIN_KEY_NODE;
1523 return CMD_SUCCESS;
39530dfe 1524}
f51bae9c 1525
d62a17ae 1526DEFUNSH(VTYSH_RIPD, router_rip, router_rip_cmd, "router rip",
1527 ROUTER_STR "RIP\n")
39530dfe 1528{
d62a17ae 1529 vty->node = RIP_NODE;
1530 return CMD_SUCCESS;
57b5b7ed 1531}
718e3744 1532
d62a17ae 1533DEFUNSH(VTYSH_RIPNGD, router_ripng, router_ripng_cmd, "router ripng",
1534 ROUTER_STR "RIPng\n")
05ba625a 1535{
d62a17ae 1536 vty->node = RIPNG_NODE;
1537 return CMD_SUCCESS;
57b5b7ed 1538}
1539
b5a8894d
CS
1540DEFUNSH(VTYSH_OSPFD, router_ospf, router_ospf_cmd,
1541 "router ospf [(1-65535)] [vrf NAME]",
d62a17ae 1542 "Enable a routing process\n"
1543 "Start OSPF configuration\n"
b86543b8
LB
1544 "Instance ID\n"
1545 VRF_CMD_HELP_STR)
90e60aa7 1546{
d62a17ae 1547 vty->node = OSPF_NODE;
1548 return CMD_SUCCESS;
90e60aa7 1549}
1550
d62a17ae 1551DEFUNSH(VTYSH_EIGRPD, router_eigrp, router_eigrp_cmd, "router eigrp (1-65535)",
1552 "Enable a routing process\n"
1553 "Start EIGRP configuration\n"
1554 "AS number to use\n")
14a227b8 1555{
d62a17ae 1556 vty->node = EIGRP_NODE;
1557 return CMD_SUCCESS;
14a227b8 1558}
90e60aa7 1559
d62a17ae 1560DEFUNSH(VTYSH_BABELD, router_babel, router_babel_cmd, "router babel",
1561 "Enable a routing process\n"
1562 "Make Babel instance command\n")
90e60aa7 1563{
d62a17ae 1564 vty->node = BABEL_NODE;
1565 return CMD_SUCCESS;
90e60aa7 1566}
14a227b8 1567
d62a17ae 1568DEFUNSH(VTYSH_OSPF6D, router_ospf6, router_ospf6_cmd, "router ospf6",
1569 ROUTER_STR OSPF6_STR)
65efcfce 1570{
d62a17ae 1571 vty->node = OSPF6_NODE;
1572 return CMD_SUCCESS;
65efcfce
LB
1573}
1574
d62a17ae 1575#if defined(HAVE_LDPD)
1576DEFUNSH(VTYSH_LDPD, ldp_mpls_ldp, ldp_mpls_ldp_cmd, "mpls ldp",
1577 "Global MPLS configuration subcommands\n"
1578 "Label Distribution Protocol\n")
65efcfce 1579{
d62a17ae 1580 vty->node = LDP_NODE;
1581 return CMD_SUCCESS;
65efcfce
LB
1582}
1583
d62a17ae 1584DEFUNSH(VTYSH_LDPD, ldp_address_family_ipv4, ldp_address_family_ipv4_cmd,
1585 "address-family ipv4",
1586 "Configure Address Family and its parameters\n"
1587 "IPv4\n")
5ff06872 1588{
d62a17ae 1589 vty->node = LDP_IPV4_NODE;
1590 return CMD_SUCCESS;
5ff06872
LB
1591}
1592
d62a17ae 1593DEFUNSH(VTYSH_LDPD, ldp_address_family_ipv6, ldp_address_family_ipv6_cmd,
1594 "address-family ipv6",
1595 "Configure Address Family and its parameters\n"
1596 "IPv6\n")
65efcfce 1597{
d62a17ae 1598 vty->node = LDP_IPV6_NODE;
1599 return CMD_SUCCESS;
65efcfce
LB
1600}
1601
983bd6f7
RW
1602DEFUNSH(VTYSH_LDPD, ldp_exit_address_family, ldp_exit_address_family_cmd,
1603 "exit-address-family", "Exit from Address Family configuration mode\n")
1604{
1605 if (vty->node == LDP_IPV4_NODE || vty->node == LDP_IPV6_NODE)
1606 vty->node = LDP_NODE;
1607 return CMD_SUCCESS;
1608}
1609
d62a17ae 1610DEFUNSH(VTYSH_LDPD, ldp_interface_ifname, ldp_interface_ifname_cmd,
1611 "interface IFNAME",
1612 "Enable LDP on an interface and enter interface submode\n"
1613 "Interface's name\n")
718e3744 1614{
d62a17ae 1615 switch (vty->node) {
1616 case LDP_IPV4_NODE:
1617 vty->node = LDP_IPV4_IFACE_NODE;
1618 break;
1619 case LDP_IPV6_NODE:
1620 vty->node = LDP_IPV6_IFACE_NODE;
1621 break;
1622 default:
1623 break;
1624 }
1625
1626 return CMD_SUCCESS;
1627}
718e3744 1628
d62a17ae 1629DEFUNSH(VTYSH_LDPD, ldp_l2vpn_word_type_vpls, ldp_l2vpn_word_type_vpls_cmd,
1630 "l2vpn WORD type vpls",
1631 "Configure l2vpn commands\n"
1632 "L2VPN name\n"
1633 "L2VPN type\n"
1634 "Virtual Private LAN Service\n")
718e3744 1635{
d62a17ae 1636 vty->node = LDP_L2VPN_NODE;
1637 return CMD_SUCCESS;
718e3744 1638}
1639
d62a17ae 1640DEFUNSH(VTYSH_LDPD, ldp_member_pseudowire_ifname,
1641 ldp_member_pseudowire_ifname_cmd, "member pseudowire IFNAME",
1642 "L2VPN member configuration\n"
1643 "Pseudowire interface\n"
1644 "Interface's name\n")
718e3744 1645{
d62a17ae 1646 vty->node = LDP_PSEUDOWIRE_NODE;
1647 return CMD_SUCCESS;
718e3744 1648}
d62a17ae 1649#endif
718e3744 1650
d62a17ae 1651DEFUNSH(VTYSH_ISISD, router_isis, router_isis_cmd, "router isis WORD",
1652 ROUTER_STR
1653 "ISO IS-IS\n"
546067df 1654 "ISO Routing area tag\n")
d62a17ae 1655{
1656 vty->node = ISIS_NODE;
1657 return CMD_SUCCESS;
1658}
1659
1660DEFUNSH(VTYSH_RMAP, vtysh_route_map, vtysh_route_map_cmd,
1661 "route-map WORD <deny|permit> (1-65535)",
1662 "Create route-map or enter route-map command mode\n"
1663 "Route map tag\n"
1664 "Route map denies set operations\n"
1665 "Route map permits set operations\n"
1666 "Sequence to insert to/delete from existing route-map entry\n")
1667{
1668 vty->node = RMAP_NODE;
1669 return CMD_SUCCESS;
1670}
1671
e5c83d9b 1672DEFUNSH(VTYSH_PBRD, vtysh_pbr_map, vtysh_pbr_map_cmd,
9a55f79a 1673 "pbr-map NAME seq (1-700)",
e5c83d9b
DS
1674 "Create pbr-map or enter pbr-map command mode\n"
1675 "The name of the PBR MAP\n"
1676 "Sequence to insert to/delete from existing pbr-map entry\n"
1677 "Sequence number\n")
1678{
1679 vty->node = PBRMAP_NODE;
1680 return CMD_SUCCESS;
1681}
1682
9a55f79a 1683DEFSH(VTYSH_PBRD, vtysh_no_pbr_map_cmd, "no pbr-map WORD [seq (1-700)]",
e5c83d9b
DS
1684 NO_STR
1685 "Delete pbr-map\n"
1686 "The name of the PBR MAP\n"
1687 "Sequence to delete from existing pbr-map entry\n"
1688 "Sequence number\n")
1689
d62a17ae 1690DEFUNSH(VTYSH_ALL, vtysh_line_vty, vtysh_line_vty_cmd, "line vty",
1691 "Configure a terminal line\n"
1692 "Virtual terminal\n")
1693{
1694 vty->node = VTY_NODE;
1695 return CMD_SUCCESS;
1696}
1697
1698DEFUNSH(VTYSH_REALLYALL, vtysh_enable, vtysh_enable_cmd, "enable",
1699 "Turn on privileged mode command\n")
1700{
1701 vty->node = ENABLE_NODE;
1702 return CMD_SUCCESS;
1703}
1704
1705DEFUNSH(VTYSH_REALLYALL, vtysh_disable, vtysh_disable_cmd, "disable",
1706 "Turn off privileged mode command\n")
1707{
1708 if (vty->node == ENABLE_NODE)
1709 vty->node = VIEW_NODE;
1710 return CMD_SUCCESS;
1711}
1712
1713DEFUNSH(VTYSH_REALLYALL, vtysh_config_terminal, vtysh_config_terminal_cmd,
1714 "configure terminal",
1715 "Configuration from vty interface\n"
1716 "Configuration terminal\n")
1717{
1718 vty->node = CONFIG_NODE;
1719 return CMD_SUCCESS;
1720}
1721
1722static int vtysh_exit(struct vty *vty)
1723{
1724 switch (vty->node) {
1725 case VIEW_NODE:
1726 case ENABLE_NODE:
1727 exit(0);
1728 break;
1729 case CONFIG_NODE:
1730 vty->node = ENABLE_NODE;
1731 break;
1732 case INTERFACE_NODE:
2dd0d726 1733 case PW_NODE:
f5d20fdb 1734 case LOGICALROUTER_NODE:
d62a17ae 1735 case VRF_NODE:
dba32923 1736 case NH_GROUP_NODE:
d62a17ae 1737 case ZEBRA_NODE:
1738 case BGP_NODE:
1739 case RIP_NODE:
1740 case RIPNG_NODE:
1741 case OSPF_NODE:
1742 case OSPF6_NODE:
1743 case EIGRP_NODE:
1744 case BABEL_NODE:
1745 case LDP_NODE:
1746 case LDP_L2VPN_NODE:
1747 case ISIS_NODE:
d62a17ae 1748 case RMAP_NODE:
e5c83d9b 1749 case PBRMAP_NODE:
d62a17ae 1750 case VTY_NODE:
1751 case KEYCHAIN_NODE:
1752 vtysh_execute("end");
1753 vtysh_execute("configure terminal");
1754 vty->node = CONFIG_NODE;
1755 break;
1756 case BGP_VPNV4_NODE:
1757 case BGP_VPNV6_NODE:
1758 case BGP_IPV4_NODE:
1759 case BGP_IPV4M_NODE:
1760 case BGP_IPV4L_NODE:
1761 case BGP_IPV6_NODE:
1762 case BGP_IPV6M_NODE:
1763 case BGP_IPV6L_NODE:
e7d78d0f
PG
1764 case BGP_FLOWSPECV4_NODE:
1765 case BGP_FLOWSPECV6_NODE:
d62a17ae 1766 case BGP_VRF_POLICY_NODE:
1767 case BGP_EVPN_NODE:
1768 case BGP_VNC_DEFAULTS_NODE:
1769 case BGP_VNC_NVE_GROUP_NODE:
1770 case BGP_VNC_L2_GROUP_NODE:
1771 vty->node = BGP_NODE;
1772 break;
1773 case BGP_EVPN_VNI_NODE:
1774 vty->node = BGP_EVPN_NODE;
1775 break;
1776 case LDP_IPV4_NODE:
1777 case LDP_IPV6_NODE:
1778 vty->node = LDP_NODE;
1779 break;
1780 case LDP_IPV4_IFACE_NODE:
1781 vty->node = LDP_IPV4_NODE;
1782 break;
1783 case LDP_IPV6_IFACE_NODE:
1784 vty->node = LDP_IPV6_NODE;
1785 break;
1786 case LDP_PSEUDOWIRE_NODE:
1787 vty->node = LDP_L2VPN_NODE;
1788 break;
1789 case KEYCHAIN_KEY_NODE:
1790 vty->node = KEYCHAIN_NODE;
1791 break;
1792 case LINK_PARAMS_NODE:
1793 vty->node = INTERFACE_NODE;
1794 break;
1795 default:
1796 break;
1797 }
1798 return CMD_SUCCESS;
1799}
1800
1801DEFUNSH(VTYSH_REALLYALL, vtysh_exit_all, vtysh_exit_all_cmd, "exit",
1802 "Exit current mode and down to previous mode\n")
4fcbf6e2 1803{
d62a17ae 1804 return vtysh_exit(vty);
4fcbf6e2
RW
1805}
1806
d62a17ae 1807DEFUNSH(VTYSH_ALL, vtysh_quit_all, vtysh_quit_all_cmd, "quit",
1808 "Exit current mode and down to previous mode\n")
4fcbf6e2 1809{
d62a17ae 1810 return vtysh_exit_all(self, vty, argc, argv);
1811}
4fcbf6e2 1812
d62a17ae 1813DEFUNSH(VTYSH_BGPD, exit_address_family, exit_address_family_cmd,
1814 "exit-address-family", "Exit from Address Family configuration mode\n")
4fcbf6e2 1815{
d62a17ae 1816 if (vty->node == BGP_IPV4_NODE || vty->node == BGP_IPV4M_NODE
1817 || vty->node == BGP_IPV4L_NODE || vty->node == BGP_VPNV4_NODE
1818 || vty->node == BGP_VPNV6_NODE || vty->node == BGP_IPV6_NODE
1819 || vty->node == BGP_IPV6L_NODE || vty->node == BGP_IPV6M_NODE
e7d78d0f
PG
1820 || vty->node == BGP_EVPN_NODE
1821 || vty->node == BGP_FLOWSPECV4_NODE
1822 || vty->node == BGP_FLOWSPECV6_NODE)
d62a17ae 1823 vty->node = BGP_NODE;
1824 return CMD_SUCCESS;
1825}
4fcbf6e2 1826
d62a17ae 1827DEFUNSH(VTYSH_BGPD, exit_vni, exit_vni_cmd, "exit-vni", "Exit from VNI mode\n")
4fcbf6e2 1828{
d62a17ae 1829 if (vty->node == BGP_EVPN_VNI_NODE)
1830 vty->node = BGP_EVPN_NODE;
1831 return CMD_SUCCESS;
1832}
1833
1834DEFUNSH(VTYSH_BGPD, exit_vnc_config, exit_vnc_config_cmd, "exit-vnc",
1835 "Exit from VNC configuration mode\n")
4fcbf6e2 1836{
d62a17ae 1837 if (vty->node == BGP_VNC_DEFAULTS_NODE
1838 || vty->node == BGP_VNC_NVE_GROUP_NODE
1839 || vty->node == BGP_VNC_L2_GROUP_NODE)
1840 vty->node = BGP_NODE;
1841 return CMD_SUCCESS;
4fcbf6e2
RW
1842}
1843
16d6ea59
QY
1844DEFUNSH(VTYSH_PIMD|VTYSH_ZEBRA, exit_vrf_config, exit_vrf_config_cmd, "exit-vrf",
1845 "Exit from VRF configuration mode\n")
1846{
1847 if (vty->node == VRF_NODE)
1848 vty->node = CONFIG_NODE;
1849 return CMD_SUCCESS;
1850}
1851
d62a17ae 1852DEFUNSH(VTYSH_BGPD, exit_vrf_policy, exit_vrf_policy_cmd, "exit-vrf-policy",
16d6ea59 1853 "Exit from VRF policy configuration mode\n")
5ff06872 1854{
d62a17ae 1855 if (vty->node == BGP_VRF_POLICY_NODE)
1856 vty->node = BGP_NODE;
1857 return CMD_SUCCESS;
5ff06872
LB
1858}
1859
d62a17ae 1860DEFUNSH(VTYSH_RIPD, vtysh_exit_ripd, vtysh_exit_ripd_cmd, "exit",
1861 "Exit current mode and down to previous mode\n")
718e3744 1862{
d62a17ae 1863 return vtysh_exit(vty);
718e3744 1864}
1865
d62a17ae 1866DEFUNSH(VTYSH_RIPD, vtysh_quit_ripd, vtysh_quit_ripd_cmd, "quit",
1867 "Exit current mode and down to previous mode\n")
a98d33ab 1868{
d62a17ae 1869 return vtysh_exit_ripd(self, vty, argc, argv);
a98d33ab 1870}
718e3744 1871
d62a17ae 1872DEFUNSH(VTYSH_RIPNGD, vtysh_exit_ripngd, vtysh_exit_ripngd_cmd, "exit",
1873 "Exit current mode and down to previous mode\n")
68980084 1874{
d62a17ae 1875 return vtysh_exit(vty);
68980084 1876}
1877
d62a17ae 1878DEFUNSH(VTYSH_RIPNGD, vtysh_quit_ripngd, vtysh_quit_ripngd_cmd, "quit",
1879 "Exit current mode and down to previous mode\n")
a98d33ab 1880{
d62a17ae 1881 return vtysh_exit_ripngd(self, vty, argc, argv);
a98d33ab 1882}
68980084 1883
d62a17ae 1884DEFUNSH(VTYSH_RMAP, vtysh_exit_rmap, vtysh_exit_rmap_cmd, "exit",
1885 "Exit current mode and down to previous mode\n")
718e3744 1886{
d62a17ae 1887 return vtysh_exit(vty);
718e3744 1888}
1889
d62a17ae 1890DEFUNSH(VTYSH_RMAP, vtysh_quit_rmap, vtysh_quit_rmap_cmd, "quit",
1891 "Exit current mode and down to previous mode\n")
a98d33ab 1892{
d62a17ae 1893 return vtysh_exit_rmap(self, vty, argc, argv);
a98d33ab 1894}
718e3744 1895
e5c83d9b
DS
1896DEFUNSH(VTYSH_PBRD, vtysh_exit_pbr_map, vtysh_exit_pbr_map_cmd, "exit",
1897 "Exit current mode and down to previous mode\n")
1898{
1899 return vtysh_exit(vty);
1900}
1901
1902DEFUNSH(VTYSH_PBRD, vtysh_quit_pbr_map, vtysh_quit_pbr_map_cmd, "quit",
1903 "Exit current mode and down to previous mode\n")
1904{
1905 return vtysh_exit_rmap(self, vty, argc, argv);
1906}
1907
d62a17ae 1908DEFUNSH(VTYSH_BGPD, vtysh_exit_bgpd, vtysh_exit_bgpd_cmd, "exit",
1909 "Exit current mode and down to previous mode\n")
718e3744 1910{
d62a17ae 1911 return vtysh_exit(vty);
718e3744 1912}
1913
d62a17ae 1914DEFUNSH(VTYSH_BGPD, vtysh_quit_bgpd, vtysh_quit_bgpd_cmd, "quit",
1915 "Exit current mode and down to previous mode\n")
a98d33ab 1916{
d62a17ae 1917 return vtysh_exit_bgpd(self, vty, argc, argv);
a98d33ab 1918}
718e3744 1919
d62a17ae 1920DEFUNSH(VTYSH_OSPFD, vtysh_exit_ospfd, vtysh_exit_ospfd_cmd, "exit",
1921 "Exit current mode and down to previous mode\n")
718e3744 1922{
d62a17ae 1923 return vtysh_exit(vty);
718e3744 1924}
1925
d62a17ae 1926DEFUNSH(VTYSH_OSPFD, vtysh_quit_ospfd, vtysh_quit_ospfd_cmd, "quit",
1927 "Exit current mode and down to previous mode\n")
a98d33ab 1928{
d62a17ae 1929 return vtysh_exit_ospfd(self, vty, argc, argv);
a98d33ab 1930}
718e3744 1931
d62a17ae 1932DEFUNSH(VTYSH_EIGRPD, vtysh_exit_eigrpd, vtysh_exit_eigrpd_cmd, "exit",
1933 "Exit current mode and down to previous mode\n")
7f57883e 1934{
d62a17ae 1935 return vtysh_exit(vty);
7f57883e
DS
1936}
1937
d62a17ae 1938DEFUNSH(VTYSH_EIGRPD, vtysh_quit_eigrpd, vtysh_quit_eigrpd_cmd, "quit",
1939 "Exit current mode and down to previous mode\n")
7f57883e 1940{
d62a17ae 1941 return vtysh_exit(vty);
7f57883e
DS
1942}
1943
d62a17ae 1944DEFUNSH(VTYSH_EIGRPD, vtysh_exit_babeld, vtysh_exit_babeld_cmd, "exit",
1945 "Exit current mode and down to previous mode\n")
a616ea5f 1946{
d62a17ae 1947 return vtysh_exit(vty);
a616ea5f
DS
1948}
1949
d62a17ae 1950DEFUNSH(VTYSH_BABELD, vtysh_quit_babeld, vtysh_quit_babeld_cmd, "quit",
1951 "Exit current mode and down to previous mode\n")
a616ea5f 1952{
d62a17ae 1953 return vtysh_exit(vty);
a616ea5f
DS
1954}
1955
d62a17ae 1956DEFUNSH(VTYSH_OSPF6D, vtysh_exit_ospf6d, vtysh_exit_ospf6d_cmd, "exit",
1957 "Exit current mode and down to previous mode\n")
68980084 1958{
d62a17ae 1959 return vtysh_exit(vty);
68980084 1960}
1961
d62a17ae 1962DEFUNSH(VTYSH_OSPF6D, vtysh_quit_ospf6d, vtysh_quit_ospf6d_cmd, "quit",
1963 "Exit current mode and down to previous mode\n")
a98d33ab 1964{
d62a17ae 1965 return vtysh_exit_ospf6d(self, vty, argc, argv);
a98d33ab 1966}
68980084 1967
d62a17ae 1968#if defined(HAVE_LDPD)
1969DEFUNSH(VTYSH_LDPD, vtysh_exit_ldpd, vtysh_exit_ldpd_cmd, "exit",
1970 "Exit current mode and down to previous mode\n")
4fcbf6e2 1971{
d62a17ae 1972 return vtysh_exit(vty);
4fcbf6e2
RW
1973}
1974
d62a17ae 1975ALIAS(vtysh_exit_ldpd, vtysh_quit_ldpd_cmd, "quit",
1976 "Exit current mode and down to previous mode\n")
87ab4aec 1977#endif
4fcbf6e2 1978
d62a17ae 1979DEFUNSH(VTYSH_ISISD, vtysh_exit_isisd, vtysh_exit_isisd_cmd, "exit",
1980 "Exit current mode and down to previous mode\n")
c25e458a 1981{
d62a17ae 1982 return vtysh_exit(vty);
c25e458a 1983}
1984
d62a17ae 1985DEFUNSH(VTYSH_ISISD, vtysh_quit_isisd, vtysh_quit_isisd_cmd, "quit",
1986 "Exit current mode and down to previous mode\n")
a98d33ab 1987{
d62a17ae 1988 return vtysh_exit_isisd(self, vty, argc, argv);
a98d33ab 1989}
c25e458a 1990
d62a17ae 1991DEFUNSH(VTYSH_ALL, vtysh_exit_line_vty, vtysh_exit_line_vty_cmd, "exit",
1992 "Exit current mode and down to previous mode\n")
e7168df4 1993{
d62a17ae 1994 return vtysh_exit(vty);
e7168df4 1995}
1996
d62a17ae 1997DEFUNSH(VTYSH_ALL, vtysh_quit_line_vty, vtysh_quit_line_vty_cmd, "quit",
1998 "Exit current mode and down to previous mode\n")
a98d33ab 1999{
d62a17ae 2000 return vtysh_exit_line_vty(self, vty, argc, argv);
a98d33ab 2001}
e7168df4 2002
d62a17ae 2003DEFUNSH(VTYSH_INTERFACE, vtysh_interface, vtysh_interface_cmd,
2004 "interface IFNAME [vrf NAME]",
2005 "Select an interface to configure\n"
2006 "Interface's name\n" VRF_CMD_HELP_STR)
718e3744 2007{
d62a17ae 2008 vty->node = INTERFACE_NODE;
2009 return CMD_SUCCESS;
718e3744 2010}
2011
2dd0d726
RW
2012DEFUNSH(VTYSH_ZEBRA, vtysh_pseudowire, vtysh_pseudowire_cmd,
2013 "pseudowire IFNAME",
2014 "Static pseudowire configuration\n"
2015 "Pseudowire name\n")
2016{
2017 vty->node = PW_NODE;
2018 return CMD_SUCCESS;
2019}
2020
f5d20fdb
PG
2021DEFUNSH(VTYSH_ZEBRA, vtysh_logicalrouter, vtysh_logicalrouter_cmd,
2022 "logical-router (1-65535) ns NAME",
d62a17ae 2023 "Enable a logical-router\n"
2024 "Specify the logical-router indentifier\n"
2025 "The Name Space\n"
2026 "The file name in " NS_RUN_DIR ", or a full pathname\n")
13460c44 2027{
f5d20fdb 2028 vty->node = LOGICALROUTER_NODE;
d62a17ae 2029 return CMD_SUCCESS;
13460c44
FL
2030}
2031
f5d20fdb 2032DEFSH(VTYSH_ZEBRA, vtysh_no_logicalrouter_cmd,
996c9314
LB
2033 "no logical-router (1-65535) ns NAME", NO_STR
2034 "Enable a Logical-Router\n"
2035 "Specify the Logical-Router identifier\n"
2036 "The Name Space\n"
2037 "The file name in " NS_RUN_DIR ", or a full pathname\n")
f5d20fdb 2038
e5c83d9b
DS
2039DEFUNSH(VTYSH_PBRD, vtysh_nexthop_group, vtysh_nexthop_group_cmd,
2040 "nexthop-group NAME",
2041 "Nexthop Group configuration\n"
2042 "Name of the Nexthop Group\n")
2043{
2044 vty->node = NH_GROUP_NODE;
2045 return CMD_SUCCESS;
2046}
2047
2048DEFSH(VTYSH_PBRD, vtysh_no_nexthop_group_cmd, "no nexthop-group NAME",
2049 NO_STR
2050 "Nexthop Group Configuration\n"
2051 "Name of the Nexthop Group\n")
2052
d62a17ae 2053DEFUNSH(VTYSH_VRF, vtysh_vrf, vtysh_vrf_cmd, "vrf NAME",
2054 "Select a VRF to configure\n"
2055 "VRF's name\n")
e9d94ea7 2056{
d62a17ae 2057 vty->node = VRF_NODE;
2058 return CMD_SUCCESS;
e9d94ea7
DS
2059}
2060
34c46274
RW
2061DEFSH(VTYSH_ZEBRA, vtysh_vrf_netns_cmd,
2062 "netns NAME",
2063 "Attach VRF to a Namespace\n"
2064 "The file name in " NS_RUN_DIR ", or a full pathname\n")
2065
2066DEFSH(VTYSH_ZEBRA, vtysh_no_vrf_netns_cmd,
2067 "no netns [NAME]",
2068 NO_STR
2069 "Detach VRF from a Namespace\n"
2070 "The file name in " NS_RUN_DIR ", or a full pathname\n")
e9d94ea7 2071
b86543b8
LB
2072DEFUNSH(VTYSH_NS, vtysh_exit_logicalrouter,
2073 vtysh_exit_logicalrouter_cmd, "exit",
2074 "Exit current mode and down to previous mode\n")
13460c44 2075{
d62a17ae 2076 return vtysh_exit(vty);
13460c44
FL
2077}
2078
b86543b8
LB
2079DEFUNSH(VTYSH_NS, vtysh_quit_logicalrouter,
2080 vtysh_quit_logicalrouter_cmd, "quit",
2081 "Exit current mode and down to previous mode\n")
a98d33ab 2082{
f5d20fdb 2083 return vtysh_exit_logicalrouter(self, vty, argc, argv);
a98d33ab 2084}
13460c44 2085
d62a17ae 2086DEFUNSH(VTYSH_VRF, vtysh_exit_vrf, vtysh_exit_vrf_cmd, "exit",
2087 "Exit current mode and down to previous mode\n")
e9d94ea7 2088{
d62a17ae 2089 return vtysh_exit(vty);
e9d94ea7
DS
2090}
2091
d62a17ae 2092DEFUNSH(VTYSH_VRF, vtysh_quit_vrf, vtysh_quit_vrf_cmd, "quit",
2093 "Exit current mode and down to previous mode\n")
a98d33ab 2094{
d62a17ae 2095 return vtysh_exit_vrf(self, vty, argc, argv);
a98d33ab 2096}
e9d94ea7 2097
e5c83d9b
DS
2098DEFUNSH(VTYSH_PBRD, vtysh_exit_nexthop_group, vtysh_exit_nexthop_group_cmd,
2099 "exit", "Exit current mode and down to previous mode\n")
2100{
2101 return vtysh_exit(vty);
2102}
2103
2104DEFUNSH(VTYSH_VRF, vtysh_quit_nexthop_group, vtysh_quit_nexthop_group_cmd,
2105 "quit", "Exit current mode and down to previous mode\n")
2106{
2107 return vtysh_exit_nexthop_group(self, vty, argc, argv);
2108}
2109
d62a17ae 2110DEFUNSH(VTYSH_INTERFACE, vtysh_exit_interface, vtysh_exit_interface_cmd, "exit",
2111 "Exit current mode and down to previous mode\n")
718e3744 2112{
d62a17ae 2113 return vtysh_exit(vty);
718e3744 2114}
2115
d62a17ae 2116DEFUNSH(VTYSH_INTERFACE, vtysh_quit_interface, vtysh_quit_interface_cmd, "quit",
2117 "Exit current mode and down to previous mode\n")
a98d33ab 2118{
d62a17ae 2119 return vtysh_exit_interface(self, vty, argc, argv);
a98d33ab 2120}
718e3744 2121
8872626b
DS
2122DEFUN (vtysh_show_poll,
2123 vtysh_show_poll_cmd,
2124 "show thread poll",
2125 SHOW_STR
2126 "Thread information\n"
2127 "Thread Poll Information\n")
2128{
2129 unsigned int i;
8872626b
DS
2130 int ret = CMD_SUCCESS;
2131 char line[100];
2132
2133 snprintf(line, sizeof(line), "do show thread poll\n");
2134 for (i = 0; i < array_size(vtysh_client); i++)
2135 if (vtysh_client[i].fd >= 0) {
2136 vty_out(vty, "Thread statistics for %s:\n",
2137 vtysh_client[i].name);
2138 ret = vtysh_client_execute(&vtysh_client[i], line);
2139 vty_out(vty, "\n");
2140 }
2141 return ret;
2142}
2143
0f69b58c
DS
2144DEFUN (vtysh_show_thread,
2145 vtysh_show_thread_cmd,
2146 "show thread cpu [FILTER]",
c25c6137
QY
2147 SHOW_STR
2148 "Thread information\n"
2149 "Thread CPU usage\n"
2150 "Display filter (rwtexb)\n")
0f69b58c 2151{
d62a17ae 2152 unsigned int i;
2153 int idx = 0;
2154 int ret = CMD_SUCCESS;
2155 char line[100];
0f69b58c 2156
d62a17ae 2157 const char *filter =
2158 argv_find(argv, argc, "FILTER", &idx) ? argv[idx]->arg : "";
c25c6137 2159
d62a17ae 2160 snprintf(line, sizeof(line), "do show thread cpu %s\n", filter);
2161 for (i = 0; i < array_size(vtysh_client); i++)
2162 if (vtysh_client[i].fd >= 0) {
2cddf2ff 2163 vty_out(vty, "Thread statistics for %s:\n",
d62a17ae 2164 vtysh_client[i].name);
2cddf2ff
QY
2165 ret = vtysh_client_execute(&vtysh_client[i], line);
2166 vty_out(vty, "\n");
d62a17ae 2167 }
2168 return ret;
0f69b58c
DS
2169}
2170
2171DEFUN (vtysh_show_work_queues,
2172 vtysh_show_work_queues_cmd,
2173 "show work-queues",
2174 SHOW_STR
2175 "Work Queue information\n")
2176{
d62a17ae 2177 unsigned int i;
2178 int ret = CMD_SUCCESS;
2179 char line[] = "do show work-queues\n";
0f69b58c 2180
d62a17ae 2181 for (i = 0; i < array_size(vtysh_client); i++)
2182 if (vtysh_client[i].fd >= 0) {
2cddf2ff 2183 vty_out(vty, "Work queue statistics for %s:\n",
d62a17ae 2184 vtysh_client[i].name);
2cddf2ff
QY
2185 ret = vtysh_client_execute(&vtysh_client[i], line);
2186 vty_out(vty, "\n");
d62a17ae 2187 }
0f69b58c 2188
d62a17ae 2189 return ret;
0f69b58c
DS
2190}
2191
b47b0a84
DS
2192DEFUN (vtysh_show_work_queues_daemon,
2193 vtysh_show_work_queues_daemon_cmd,
e5c83d9b 2194 "show work-queues <zebra|ripd|ripngd|ospfd|ospf6d|bgpd|isisd|pbrd>",
b47b0a84
DS
2195 SHOW_STR
2196 "Work Queue information\n"
2197 "For the zebra daemon\n"
2198 "For the rip daemon\n"
2199 "For the ripng daemon\n"
2200 "For the ospf daemon\n"
2201 "For the ospfv6 daemon\n"
2202 "For the bgp daemon\n"
e5c83d9b
DS
2203 "For the isis daemon\n"
2204 "For the pbr daemon\n")
b47b0a84 2205{
d62a17ae 2206 int idx_protocol = 2;
2207 unsigned int i;
2208 int ret = CMD_SUCCESS;
b47b0a84 2209
d62a17ae 2210 for (i = 0; i < array_size(vtysh_client); i++) {
2211 if (strmatch(vtysh_client[i].name, argv[idx_protocol]->text))
2212 break;
2213 }
b47b0a84 2214
2cddf2ff 2215 ret = vtysh_client_execute(&vtysh_client[i], "show work-queues\n");
b47b0a84 2216
d62a17ae 2217 return ret;
b47b0a84
DS
2218}
2219
d62a17ae 2220DEFUNSH(VTYSH_ZEBRA, vtysh_link_params, vtysh_link_params_cmd, "link-params",
2221 LINK_PARAMS_STR)
16f1b9ee 2222{
d62a17ae 2223 vty->node = LINK_PARAMS_NODE;
2224 return CMD_SUCCESS;
16f1b9ee
OD
2225}
2226
d62a17ae 2227DEFUNSH(VTYSH_ZEBRA, exit_link_params, exit_link_params_cmd, "exit-link-params",
2228 "Exit from Link Params configuration node\n")
03f99d9a 2229{
d62a17ae 2230 if (vty->node == LINK_PARAMS_NODE)
2231 vty->node = INTERFACE_NODE;
2232 return CMD_SUCCESS;
03f99d9a
DS
2233}
2234
d62a17ae 2235static int show_per_daemon(const char *line, const char *headline)
362b4031 2236{
d62a17ae 2237 unsigned int i;
2238 int ret = CMD_SUCCESS;
2a8e27af 2239
d62a17ae 2240 for (i = 0; i < array_size(vtysh_client); i++)
2241 if (vtysh_client[i].fd >= 0) {
2cddf2ff
QY
2242 vty_out(vty, headline, vtysh_client[i].name);
2243 ret = vtysh_client_execute(&vtysh_client[i], line);
2244 vty_out(vty, "\n");
d62a17ae 2245 }
2a8e27af 2246
d62a17ae 2247 return ret;
362b4031
PJ
2248}
2249
aea03ad6
QY
2250DEFUNSH_HIDDEN (0x00,
2251 vtysh_debug_all,
2252 vtysh_debug_all_cmd,
2253 "[no] debug all",
2254 NO_STR
2255 DEBUG_STR
2256 "Toggle all debugs on or off\n")
2257{
2258 return CMD_SUCCESS;
2259}
2260
87f6dc50
DS
2261DEFUN (vtysh_show_debugging,
2262 vtysh_show_debugging_cmd,
2263 "show debugging",
2264 SHOW_STR
2265 DEBUG_STR)
2266{
996c9314 2267 return show_per_daemon("do show debugging\n", "");
87f6dc50
DS
2268}
2269
40818cec
DL
2270DEFUN (vtysh_show_debugging_hashtable,
2271 vtysh_show_debugging_hashtable_cmd,
2272 "show debugging hashtable [statistics]",
2273 SHOW_STR
2274 DEBUG_STR
2275 "Statistics about hash tables\n"
2276 "Statistics about hash tables\n")
2277{
2cddf2ff
QY
2278 vty_out(vty, "\n");
2279 vty_out(vty,
44deef01 2280 "Load factor (LF) - average number of elements across all buckets\n");
2cddf2ff 2281 vty_out(vty,
44deef01 2282 "Full load factor (FLF) - average number of elements across full buckets\n\n");
2cddf2ff 2283 vty_out(vty,
44deef01 2284 "Standard deviation (SD) is calculated for both the LF and FLF\n");
2cddf2ff 2285 vty_out(vty,
44deef01 2286 "and indicates the typical deviation of bucket chain length\n");
2cddf2ff 2287 vty_out(vty, "from the value in the corresponding load factor.\n\n");
44deef01 2288
40818cec
DL
2289 return show_per_daemon("do show debugging hashtable\n",
2290 "Hashtable statistics for %s:\n");
2291}
2292
2a8e27af
DL
2293/* Memory */
2294DEFUN (vtysh_show_memory,
2295 vtysh_show_memory_cmd,
2296 "show memory",
2297 SHOW_STR
2298 "Memory statistics\n")
2299{
d991a3ea 2300 return show_per_daemon("do show memory\n", "Memory statistics for %s:\n");
2a8e27af
DL
2301}
2302
2303DEFUN (vtysh_show_modules,
2304 vtysh_show_modules_cmd,
2305 "show modules",
2306 SHOW_STR
2307 "Loaded modules\n")
2308{
d991a3ea 2309 return show_per_daemon("do show modules\n",
d62a17ae 2310 "Module information for %s:\n");
2a8e27af
DL
2311}
2312
95e735b5 2313/* Logging commands. */
dbf7d13d
PJ
2314DEFUN (vtysh_show_logging,
2315 vtysh_show_logging_cmd,
2316 "show logging",
2317 SHOW_STR
2318 "Show current logging configuration\n")
2319{
7292d851
DS
2320 return show_per_daemon("do show logging\n",
2321 "Logging configuration for %s:\n");
d62a17ae 2322}
2323
2324DEFUNSH(VTYSH_ALL, vtysh_log_stdout, vtysh_log_stdout_cmd, "log stdout",
2325 "Logging control\n"
2326 "Set stdout logging level\n")
95e735b5 2327{
d62a17ae 2328 return CMD_SUCCESS;
2329}
2330
2331DEFUNSH(VTYSH_ALL, vtysh_log_stdout_level, vtysh_log_stdout_level_cmd,
2332 "log stdout <emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>",
2333 "Logging control\n"
2334 "Set stdout logging level\n" LOG_LEVEL_DESC)
95e735b5 2335{
d62a17ae 2336 return CMD_SUCCESS;
95e735b5 2337}
2338
d62a17ae 2339DEFUNSH(VTYSH_ALL, no_vtysh_log_stdout, no_vtysh_log_stdout_cmd,
9d303b37 2340 "no log stdout [LEVEL]", NO_STR
d62a17ae 2341 "Logging control\n"
2342 "Cancel logging to stdout\n"
2343 "Logging level\n")
95e735b5 2344{
d62a17ae 2345 return CMD_SUCCESS;
95e735b5 2346}
2347
d62a17ae 2348DEFUNSH(VTYSH_ALL, vtysh_log_file, vtysh_log_file_cmd, "log file FILENAME",
2349 "Logging control\n"
2350 "Logging to file\n"
2351 "Logging filename\n")
274a4a44 2352{
d62a17ae 2353 return CMD_SUCCESS;
274a4a44 2354}
2355
d62a17ae 2356DEFUNSH(VTYSH_ALL, vtysh_log_file_level, vtysh_log_file_level_cmd,
2357 "log file FILENAME <emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>",
2358 "Logging control\n"
2359 "Logging to file\n"
2360 "Logging filename\n" LOG_LEVEL_DESC)
95e735b5 2361{
d62a17ae 2362 return CMD_SUCCESS;
95e735b5 2363}
2364
d62a17ae 2365DEFUNSH(VTYSH_ALL, no_vtysh_log_file, no_vtysh_log_file_cmd,
9d303b37 2366 "no log file [FILENAME [LEVEL]]", NO_STR
d62a17ae 2367 "Logging control\n"
2368 "Cancel logging to file\n"
2369 "Logging file name\n"
2370 "Logging level\n")
274a4a44 2371{
d62a17ae 2372 return CMD_SUCCESS;
274a4a44 2373}
2374
d62a17ae 2375DEFUNSH(VTYSH_ALL, vtysh_log_monitor, vtysh_log_monitor_cmd,
2376 "log monitor [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2377 "Logging control\n"
2378 "Set terminal line (monitor) logging level\n" LOG_LEVEL_DESC)
274a4a44 2379{
d62a17ae 2380 return CMD_SUCCESS;
274a4a44 2381}
2382
d62a17ae 2383DEFUNSH(VTYSH_ALL, no_vtysh_log_monitor, no_vtysh_log_monitor_cmd,
9d303b37 2384 "no log monitor [LEVEL]", NO_STR
d62a17ae 2385 "Logging control\n"
2386 "Disable terminal line (monitor) logging\n"
2387 "Logging level\n")
95e735b5 2388{
d62a17ae 2389 return CMD_SUCCESS;
95e735b5 2390}
2391
d62a17ae 2392DEFUNSH(VTYSH_ALL, vtysh_log_syslog, vtysh_log_syslog_cmd,
2393 "log syslog [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2394 "Logging control\n"
2395 "Set syslog logging level\n" LOG_LEVEL_DESC)
95e735b5 2396{
d62a17ae 2397 return CMD_SUCCESS;
95e735b5 2398}
2399
d62a17ae 2400DEFUNSH(VTYSH_ALL, no_vtysh_log_syslog, no_vtysh_log_syslog_cmd,
1f143236
DS
2401 "no log syslog [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2402 NO_STR
d62a17ae 2403 "Logging control\n"
2404 "Cancel logging to syslog\n"
1f143236 2405 LOG_LEVEL_DESC)
d62a17ae 2406{
2407 return CMD_SUCCESS;
2408}
2409
2410DEFUNSH(VTYSH_ALL, vtysh_log_facility, vtysh_log_facility_cmd,
2411 "log facility <kern|user|mail|daemon|auth|syslog|lpr|news|uucp|cron|local0|local1|local2|local3|local4|local5|local6|local7>",
2412 "Logging control\n"
2413 "Facility parameter for syslog messages\n" LOG_FACILITY_DESC)
95e735b5 2414{
d62a17ae 2415 return CMD_SUCCESS;
95e735b5 2416}
2417
d62a17ae 2418DEFUNSH(VTYSH_ALL, no_vtysh_log_facility, no_vtysh_log_facility_cmd,
9d303b37 2419 "no log facility [FACILITY]", NO_STR
d62a17ae 2420 "Logging control\n"
2421 "Reset syslog facility to default (daemon)\n"
2422 "Syslog facility\n")
274a4a44 2423{
d62a17ae 2424 return CMD_SUCCESS;
274a4a44 2425}
2426
d62a17ae 2427DEFUNSH(VTYSH_ALL, vtysh_log_record_priority, vtysh_log_record_priority_cmd,
2428 "log record-priority",
2429 "Logging control\n"
2430 "Log the priority of the message within the message\n")
95e735b5 2431{
d62a17ae 2432 return CMD_SUCCESS;
95e735b5 2433}
2434
d62a17ae 2435DEFUNSH(VTYSH_ALL, no_vtysh_log_record_priority,
9d303b37 2436 no_vtysh_log_record_priority_cmd, "no log record-priority", NO_STR
d62a17ae 2437 "Logging control\n"
2438 "Do not log the priority of the message within the message\n")
95e735b5 2439{
d62a17ae 2440 return CMD_SUCCESS;
95e735b5 2441}
2442
d62a17ae 2443DEFUNSH(VTYSH_ALL, vtysh_log_timestamp_precision,
2444 vtysh_log_timestamp_precision_cmd, "log timestamp precision (0-6)",
2445 "Logging control\n"
2446 "Timestamp configuration\n"
2447 "Set the timestamp precision\n"
2448 "Number of subsecond digits\n")
c749b722 2449{
d62a17ae 2450 return CMD_SUCCESS;
c749b722
AS
2451}
2452
d62a17ae 2453DEFUNSH(VTYSH_ALL, no_vtysh_log_timestamp_precision,
2454 no_vtysh_log_timestamp_precision_cmd, "no log timestamp precision",
2455 NO_STR
2456 "Logging control\n"
2457 "Timestamp configuration\n"
2458 "Reset the timestamp precision to the default value of 0\n")
c749b722 2459{
d62a17ae 2460 return CMD_SUCCESS;
c749b722
AS
2461}
2462
d62a17ae 2463DEFUNSH(VTYSH_ALL, vtysh_service_password_encrypt,
2464 vtysh_service_password_encrypt_cmd, "service password-encryption",
2465 "Set up miscellaneous service\n"
2466 "Enable encrypted passwords\n")
e7168df4 2467{
d62a17ae 2468 return CMD_SUCCESS;
e7168df4 2469}
2470
d62a17ae 2471DEFUNSH(VTYSH_ALL, no_vtysh_service_password_encrypt,
2472 no_vtysh_service_password_encrypt_cmd, "no service password-encryption",
2473 NO_STR
2474 "Set up miscellaneous service\n"
2475 "Enable encrypted passwords\n")
e7168df4 2476{
d62a17ae 2477 return CMD_SUCCESS;
e7168df4 2478}
2479
d62a17ae 2480DEFUNSH(VTYSH_ALL, vtysh_config_password, vtysh_password_cmd,
2481 "password [(8-8)] LINE",
322e2d5c 2482 "Modify the terminal connection password\n"
d62a17ae 2483 "Specifies a HIDDEN password will follow\n"
2484 "The password string\n")
e7168df4 2485{
d62a17ae 2486 return CMD_SUCCESS;
e7168df4 2487}
2488
322e2d5c
PM
2489DEFUNSH(VTYSH_ALL, no_vtysh_config_password, no_vtysh_password_cmd,
2490 "no password", NO_STR
2491 "Modify the terminal connection password\n")
2492{
4911ca9c 2493 vty_out(vty, NO_PASSWD_CMD_WARNING);
eb83f7ce 2494
322e2d5c
PM
2495 return CMD_SUCCESS;
2496}
2497
d62a17ae 2498DEFUNSH(VTYSH_ALL, vtysh_config_enable_password, vtysh_enable_password_cmd,
2499 "enable password [(8-8)] LINE",
2500 "Modify enable password parameters\n"
2501 "Assign the privileged level password\n"
2502 "Specifies a HIDDEN password will follow\n"
2503 "The 'enable' password string\n")
e7168df4 2504{
d62a17ae 2505 return CMD_SUCCESS;
e7168df4 2506}
2507
d62a17ae 2508DEFUNSH(VTYSH_ALL, no_vtysh_config_enable_password,
9d303b37 2509 no_vtysh_enable_password_cmd, "no enable password", NO_STR
d62a17ae 2510 "Modify enable password parameters\n"
2511 "Assign the privileged level password\n")
e7168df4 2512{
4911ca9c 2513 vty_out(vty, NO_PASSWD_CMD_WARNING);
eb83f7ce 2514
d62a17ae 2515 return CMD_SUCCESS;
e7168df4 2516}
2517
718e3744 2518DEFUN (vtysh_write_terminal,
2519 vtysh_write_terminal_cmd,
e52702f2 2520 "write terminal [<zebra|ripd|ripngd|ospfd|ospf6d|ldpd|bgpd|isisd|pimd>]",
718e3744 2521 "Write running configuration to memory, network, or terminal\n"
066242b5
QY
2522 "Write to terminal\n"
2523 "For the zebra daemon\n"
2524 "For the rip daemon\n"
2525 "For the ripng daemon\n"
2526 "For the ospf daemon\n"
2527 "For the ospfv6 daemon\n"
16cedbb0 2528 "For the ldpd daemon\n"
066242b5
QY
2529 "For the bgp daemon\n"
2530 "For the isis daemon\n"
2531 "For the pim daemon\n")
718e3744 2532{
d7c0a89a 2533 unsigned int i;
d62a17ae 2534 char line[] = "do write terminal\n";
d62a17ae 2535
2cddf2ff
QY
2536 vty_out(vty, "Building configuration...\n");
2537 vty_out(vty, "\nCurrent configuration:\n");
2538 vty_out(vty, "!\n");
d62a17ae 2539
2540 for (i = 0; i < array_size(vtysh_client); i++)
2541 if ((argc < 3)
2542 || (strmatch(vtysh_client[i].name, argv[2]->text)))
2543 vtysh_client_config(&vtysh_client[i], line);
2544
2545 /* Integrate vtysh specific configuration. */
2cddf2ff 2546 vty_open_pager(vty);
d62a17ae 2547 vtysh_config_write();
2cddf2ff
QY
2548 vtysh_config_dump();
2549 vty_close_pager(vty);
2550 vty_out(vty, "end\n");
d62a17ae 2551
d62a17ae 2552 return CMD_SUCCESS;
718e3744 2553}
2554
a98d33ab
QY
2555DEFUN (vtysh_show_running_config,
2556 vtysh_show_running_config_cmd,
e52702f2 2557 "show running-config [<zebra|ripd|ripngd|ospfd|ospf6d|ldpd|bgpd|isisd|pimd>]",
a98d33ab
QY
2558 SHOW_STR
2559 "Current operating configuration\n"
c006e89e
DS
2560 "For the zebra daemon\n"
2561 "For the rip daemon\n"
2562 "For the ripng daemon\n"
2563 "For the ospf daemon\n"
2564 "For the ospfv6 daemon\n"
4fcbf6e2 2565 "For the ldp daemon\n"
c006e89e 2566 "For the bgp daemon\n"
0fab7646
DS
2567 "For the isis daemon\n"
2568 "For the pim daemon\n")
c006e89e 2569{
d62a17ae 2570 return vtysh_write_terminal(self, vty, argc, argv);
c006e89e
DS
2571}
2572
e7168df4 2573DEFUN (vtysh_integrated_config,
2574 vtysh_integrated_config_cmd,
2575 "service integrated-vtysh-config",
2576 "Set up miscellaneous service\n"
2577 "Write configuration into integrated file\n")
4fc01e67 2578{
d62a17ae 2579 vtysh_write_integrated = WRITE_INTEGRATED_YES;
2580 return CMD_SUCCESS;
4fc01e67 2581}
2582
e7168df4 2583DEFUN (no_vtysh_integrated_config,
2584 no_vtysh_integrated_config_cmd,
2585 "no service integrated-vtysh-config",
2586 NO_STR
2587 "Set up miscellaneous service\n"
2588 "Write configuration into integrated file\n")
4fc01e67 2589{
d62a17ae 2590 vtysh_write_integrated = WRITE_INTEGRATED_NO;
2591 return CMD_SUCCESS;
4fc01e67 2592}
2593
d62a17ae 2594static void backup_config_file(const char *fbackup)
718e3744 2595{
d62a17ae 2596 char *integrate_sav = NULL;
718e3744 2597
d62a17ae 2598 integrate_sav = malloc(strlen(fbackup) + strlen(CONF_BACKUP_EXT) + 1);
2599 strcpy(integrate_sav, fbackup);
2600 strcat(integrate_sav, CONF_BACKUP_EXT);
718e3744 2601
d62a17ae 2602 /* Move current configuration file to backup config file. */
1a40fad5 2603 if (unlink(integrate_sav) != 0) {
2604 vty_out(vty, "Warning: %s unlink failed\n", integrate_sav);
2605 }
2606 if (rename(fbackup, integrate_sav) != 0) {
2607 vty_out(vty, "Error renaming %s to %s\n", fbackup,
2608 integrate_sav);
2609 }
d62a17ae 2610 free(integrate_sav);
a7222276
DS
2611}
2612
d62a17ae 2613int vtysh_write_config_integrated(void)
a7222276 2614{
d7c0a89a 2615 unsigned int i;
d62a17ae 2616 char line[] = "do write terminal\n";
2617 FILE *fp;
2618 int fd;
029a775e 2619#ifdef FRR_USER
d62a17ae 2620 struct passwd *pwentry;
029a775e 2621#endif
2622#ifdef FRR_GROUP
d62a17ae 2623 struct group *grentry;
029a775e 2624#endif
d62a17ae 2625 uid_t uid = -1;
2626 gid_t gid = -1;
2627 struct stat st;
2628 int err = 0;
a7222276 2629
2cddf2ff 2630 vty_out(vty, "Building Configuration...\n");
a7222276 2631
9b8a8249
DL
2632 backup_config_file(frr_config);
2633 fp = fopen(frr_config, "w");
d62a17ae 2634 if (fp == NULL) {
2cddf2ff 2635 vty_out(vty,
d62a17ae 2636 "%% Error: failed to open configuration file %s: %s\n",
9b8a8249 2637 frr_config, safe_strerror(errno));
d62a17ae 2638 return CMD_WARNING_CONFIG_FAILED;
2639 }
2640 fd = fileno(fp);
a7222276 2641
d62a17ae 2642 for (i = 0; i < array_size(vtysh_client); i++)
2643 vtysh_client_config(&vtysh_client[i], line);
718e3744 2644
d62a17ae 2645 vtysh_config_write();
b7ae6ac4
QY
2646 vty->of_saved = vty->of;
2647 vty->of = fp;
2cddf2ff 2648 vtysh_config_dump();
b7ae6ac4 2649 vty->of = vty->of_saved;
718e3744 2650
d62a17ae 2651 if (fchmod(fd, CONFIGFILE_MASK) != 0) {
2652 printf("%% Warning: can't chmod configuration file %s: %s\n",
9b8a8249 2653 frr_config, safe_strerror(errno));
d62a17ae 2654 err++;
2655 }
718e3744 2656
9e8df988 2657#ifdef FRR_USER
d62a17ae 2658 pwentry = getpwnam(FRR_USER);
2659 if (pwentry)
2660 uid = pwentry->pw_uid;
2661 else {
2662 printf("%% Warning: could not look up user \"%s\"\n", FRR_USER);
2663 err++;
2664 }
9e8df988
JAG
2665#endif
2666#ifdef FRR_GROUP
d62a17ae 2667 grentry = getgrnam(FRR_GROUP);
2668 if (grentry)
2669 gid = grentry->gr_gid;
2670 else {
2671 printf("%% Warning: could not look up group \"%s\"\n",
2672 FRR_GROUP);
2673 err++;
2674 }
9e8df988 2675#endif
4fc01e67 2676
d62a17ae 2677 if (!fstat(fd, &st)) {
2678 if (st.st_uid == uid)
2679 uid = -1;
2680 if (st.st_gid == gid)
2681 gid = -1;
2682 if ((uid != (uid_t)-1 || gid != (gid_t)-1)
2683 && fchown(fd, uid, gid)) {
2684 printf("%% Warning: can't chown configuration file %s: %s\n",
9b8a8249 2685 frr_config, safe_strerror(errno));
d62a17ae 2686 err++;
2687 }
2688 } else {
9b8a8249 2689 printf("%% Warning: stat() failed on %s: %s\n", frr_config,
d62a17ae 2690 safe_strerror(errno));
2691 err++;
2692 }
2693
2694 fclose(fp);
2695
9b8a8249 2696 printf("Integrated configuration saved to %s\n", frr_config);
d62a17ae 2697 if (err)
2698 return CMD_WARNING;
2699
2700 printf("[OK]\n");
2701 return CMD_SUCCESS;
718e3744 2702}
2703
a68f8616 2704static bool want_config_integrated(void)
039eaca3 2705{
d62a17ae 2706 struct stat s;
2707
2708 switch (vtysh_write_integrated) {
2709 case WRITE_INTEGRATED_UNSPECIFIED:
9b8a8249 2710 if (stat(frr_config, &s) && errno == ENOENT)
d62a17ae 2711 return false;
2712 return true;
2713 case WRITE_INTEGRATED_NO:
2714 return false;
2715 case WRITE_INTEGRATED_YES:
2716 return true;
2717 }
2718 return true;
039eaca3
CF
2719}
2720
4fc01e67 2721DEFUN (vtysh_write_memory,
2722 vtysh_write_memory_cmd,
a98d33ab 2723 "write [<memory|file>]",
4fc01e67 2724 "Write running configuration to memory, network, or terminal\n"
a98d33ab
QY
2725 "Write configuration to the file (same as write file)\n"
2726 "Write configuration to the file (same as write memory)\n")
4fc01e67 2727{
d62a17ae 2728 int ret = CMD_SUCCESS;
2729 char line[] = "do write memory\n";
d7c0a89a 2730 unsigned int i;
d62a17ae 2731
2cddf2ff 2732 vty_out(vty, "Note: this version of vtysh never writes vtysh.conf\n");
d62a17ae 2733
2734 /* If integrated frr.conf explicitely set. */
2735 if (want_config_integrated()) {
2736 ret = CMD_WARNING_CONFIG_FAILED;
869f5586
QY
2737
2738 /* first attempt to use watchfrr if it's available */
2739 bool used_watchfrr = false;
2740
d62a17ae 2741 for (i = 0; i < array_size(vtysh_client); i++)
2742 if (vtysh_client[i].flag == VTYSH_WATCHFRR)
2743 break;
869f5586
QY
2744 if (i < array_size(vtysh_client) && vtysh_client[i].fd != -1) {
2745 used_watchfrr = true;
d62a17ae 2746 ret = vtysh_client_execute(&vtysh_client[i],
2cddf2ff 2747 "do write integrated");
869f5586 2748 }
d62a17ae 2749
470bc619 2750 /*
869f5586
QY
2751 * If we didn't use watchfrr, fallback to writing the config
2752 * ourselves
470bc619 2753 */
869f5586 2754 if (!used_watchfrr) {
d62a17ae 2755 printf("\nWarning: attempting direct configuration write without "
2756 "watchfrr.\nFile permissions and ownership may be "
2757 "incorrect, or write may fail.\n\n");
2758 ret = vtysh_write_config_integrated();
2759 }
2760 return ret;
2761 }
a7222276 2762
2cddf2ff 2763 vty_out(vty, "Building Configuration...\n");
a7222276 2764
d62a17ae 2765 for (i = 0; i < array_size(vtysh_client); i++)
2cddf2ff 2766 ret = vtysh_client_execute(&vtysh_client[i], line);
4fc01e67 2767
d62a17ae 2768 return ret;
4fc01e67 2769}
2770
a98d33ab
QY
2771DEFUN (vtysh_copy_running_config,
2772 vtysh_copy_running_config_cmd,
2773 "copy running-config startup-config",
718e3744 2774 "Copy from one file to another\n"
2775 "Copy from current system configuration\n"
2776 "Copy to startup configuration\n")
a98d33ab 2777{
d62a17ae 2778 return vtysh_write_memory(self, vty, argc, argv);
a98d33ab 2779}
718e3744 2780
34553cc3 2781DEFUN (vtysh_terminal_length,
2782 vtysh_terminal_length_cmd,
6147e2c6 2783 "terminal length (0-512)",
34553cc3 2784 "Set terminal line parameters\n"
2785 "Set number of lines on a screen\n"
2786 "Number of lines on screen (0 for no pausing)\n")
2787{
d62a17ae 2788 int idx_number = 2;
2789 int lines;
2790 char *endptr = NULL;
2791 char default_pager[10];
34553cc3 2792
d62a17ae 2793 lines = strtol(argv[idx_number]->arg, &endptr, 10);
2794 if (lines < 0 || lines > 512 || *endptr != '\0') {
2cddf2ff 2795 vty_out(vty, "length is malformed\n");
d62a17ae 2796 return CMD_WARNING;
2797 }
34553cc3 2798
d62a17ae 2799 if (vtysh_pager_name) {
2800 free(vtysh_pager_name);
2801 vtysh_pager_name = NULL;
2802 }
34553cc3 2803
d62a17ae 2804 if (lines != 0) {
2805 snprintf(default_pager, 10, "more -%i", lines);
2806 vtysh_pager_name = strdup(default_pager);
2807 }
34553cc3 2808
d62a17ae 2809 return CMD_SUCCESS;
34553cc3 2810}
2811
2812DEFUN (vtysh_terminal_no_length,
2813 vtysh_terminal_no_length_cmd,
2814 "terminal no length",
2815 "Set terminal line parameters\n"
2816 NO_STR
2817 "Set number of lines on a screen\n")
2818{
d62a17ae 2819 if (vtysh_pager_name) {
2820 free(vtysh_pager_name);
2821 vtysh_pager_name = NULL;
2822 }
34553cc3 2823
d62a17ae 2824 vtysh_pager_init();
2825 return CMD_SUCCESS;
34553cc3 2826}
2827
f2799e69 2828DEFUN (vtysh_show_daemons,
2829 vtysh_show_daemons_cmd,
2830 "show daemons",
e7168df4 2831 SHOW_STR
2832 "Show list of running daemons\n")
2833{
d7c0a89a 2834 unsigned int i;
b1aa147d 2835
d62a17ae 2836 for (i = 0; i < array_size(vtysh_client); i++)
2837 if (vtysh_client[i].fd >= 0)
2cddf2ff
QY
2838 vty_out(vty, " %s", vtysh_client[i].name);
2839 vty_out(vty, "\n");
e7168df4 2840
d62a17ae 2841 return CMD_SUCCESS;
e7168df4 2842}
2843
718e3744 2844/* Execute command in child process. */
996c9314
LB
2845static void execute_command(const char *command, int argc, const char *arg1,
2846 const char *arg2)
d62a17ae 2847{
2848 pid_t pid;
2849 int status;
2850
2851 /* Call fork(). */
2852 pid = fork();
2853
2854 if (pid < 0) {
2855 /* Failure of fork(). */
2856 fprintf(stderr, "Can't fork: %s\n", safe_strerror(errno));
2857 exit(1);
2858 } else if (pid == 0) {
2859 /* This is child process. */
2860 switch (argc) {
2861 case 0:
2862 execlp(command, command, (const char *)NULL);
2863 break;
2864 case 1:
2865 execlp(command, command, arg1, (const char *)NULL);
2866 break;
2867 case 2:
2868 execlp(command, command, arg1, arg2,
2869 (const char *)NULL);
2870 break;
2871 }
718e3744 2872
d62a17ae 2873 /* When execlp suceed, this part is not executed. */
2874 fprintf(stderr, "Can't execute %s: %s\n", command,
2875 safe_strerror(errno));
2876 exit(1);
2877 } else {
2878 /* This is parent. */
2879 execute_flag = 1;
2880 wait4(pid, &status, 0, NULL);
2881 execute_flag = 0;
2882 }
718e3744 2883}
2884
2885DEFUN (vtysh_ping,
2886 vtysh_ping_cmd,
2887 "ping WORD",
4eeccf18 2888 "Send echo messages\n"
718e3744 2889 "Ping destination address or hostname\n")
2890{
137a1684
DS
2891 int idx = 1;
2892
2893 argv_find(argv, argc, "WORD", &idx);
2894 execute_command("ping", 1, argv[idx]->arg, NULL);
d62a17ae 2895 return CMD_SUCCESS;
718e3744 2896}
2897
d62a17ae 2898ALIAS(vtysh_ping, vtysh_ping_ip_cmd, "ping ip WORD",
2899 "Send echo messages\n"
2900 "IP echo\n"
2901 "Ping destination address or hostname\n")
4eeccf18 2902
718e3744 2903DEFUN (vtysh_traceroute,
2904 vtysh_traceroute_cmd,
2905 "traceroute WORD",
2906 "Trace route to destination\n"
2907 "Trace route to destination address or hostname\n")
2908{
137a1684
DS
2909 int idx = 1;
2910
2911 argv_find(argv, argc, "WORD", &idx);
2912 execute_command("traceroute", 1, argv[idx]->arg, NULL);
d62a17ae 2913 return CMD_SUCCESS;
718e3744 2914}
2915
d62a17ae 2916ALIAS(vtysh_traceroute, vtysh_traceroute_ip_cmd, "traceroute ip WORD",
2917 "Trace route to destination\n"
2918 "IP trace\n"
2919 "Trace route to destination address or hostname\n")
4eeccf18 2920
4d9ad5dc
MS
2921DEFUN (vtysh_mtrace,
2922 vtysh_mtrace_cmd,
71e55fb2 2923 "mtrace WORD [WORD]",
4d9ad5dc 2924 "Multicast trace route to multicast source\n"
71e55fb2
MS
2925 "Multicast trace route to multicast source address\n"
2926 "Multicast trace route for multicast group address\n")
4d9ad5dc 2927{
71e55fb2
MS
2928 if (argc == 2)
2929 execute_command("mtracebis", 1, argv[1]->arg, NULL);
2930 else
2931 execute_command("mtracebis", 2, argv[1]->arg, argv[2]->arg);
4d9ad5dc
MS
2932 return CMD_SUCCESS;
2933}
2934
4eeccf18 2935DEFUN (vtysh_ping6,
2936 vtysh_ping6_cmd,
2937 "ping ipv6 WORD",
2938 "Send echo messages\n"
2939 "IPv6 echo\n"
2940 "Ping destination address or hostname\n")
2941{
137a1684 2942 execute_command("ping6", 1, argv[2]->arg, NULL);
d62a17ae 2943 return CMD_SUCCESS;
4eeccf18 2944}
2945
2946DEFUN (vtysh_traceroute6,
2947 vtysh_traceroute6_cmd,
2948 "traceroute ipv6 WORD",
2949 "Trace route to destination\n"
2950 "IPv6 trace\n"
2951 "Trace route to destination address or hostname\n")
2952{
137a1684 2953 execute_command("traceroute6", 1, argv[2]->arg, NULL);
d62a17ae 2954 return CMD_SUCCESS;
4eeccf18 2955}
4eeccf18 2956
576b6b5d 2957#if defined(HAVE_SHELL_ACCESS)
718e3744 2958DEFUN (vtysh_telnet,
2959 vtysh_telnet_cmd,
2960 "telnet WORD",
2961 "Open a telnet connection\n"
2962 "IP address or hostname of a remote system\n")
2963{
137a1684 2964 execute_command("telnet", 1, argv[1]->arg, NULL);
d62a17ae 2965 return CMD_SUCCESS;
718e3744 2966}
2967
2968DEFUN (vtysh_telnet_port,
2969 vtysh_telnet_port_cmd,
2970 "telnet WORD PORT",
2971 "Open a telnet connection\n"
2972 "IP address or hostname of a remote system\n"
2973 "TCP Port number\n")
2974{
137a1684 2975 execute_command("telnet", 2, argv[1]->arg, argv[2]->arg);
d62a17ae 2976 return CMD_SUCCESS;
718e3744 2977}
2978
5087df56 2979DEFUN (vtysh_ssh,
2980 vtysh_ssh_cmd,
2981 "ssh WORD",
2982 "Open an ssh connection\n"
2983 "[user@]host\n")
2984{
137a1684 2985 execute_command("ssh", 1, argv[1]->arg, NULL);
d62a17ae 2986 return CMD_SUCCESS;
5087df56 2987}
2988
718e3744 2989DEFUN (vtysh_start_shell,
2990 vtysh_start_shell_cmd,
2991 "start-shell",
2992 "Start UNIX shell\n")
2993{
d62a17ae 2994 execute_command("sh", 0, NULL, NULL);
2995 return CMD_SUCCESS;
718e3744 2996}
2997
2998DEFUN (vtysh_start_bash,
2999 vtysh_start_bash_cmd,
3000 "start-shell bash",
3001 "Start UNIX shell\n"
3002 "Start bash\n")
3003{
d62a17ae 3004 execute_command("bash", 0, NULL, NULL);
3005 return CMD_SUCCESS;
718e3744 3006}
3007
3008DEFUN (vtysh_start_zsh,
3009 vtysh_start_zsh_cmd,
3010 "start-shell zsh",
3011 "Start UNIX shell\n"
3012 "Start Z shell\n")
3013{
d62a17ae 3014 execute_command("zsh", 0, NULL, NULL);
3015 return CMD_SUCCESS;
718e3744 3016}
576b6b5d 3017#endif
b094d260 3018
0b84f294
DL
3019DEFUN (config_list,
3020 config_list_cmd,
3021 "list [permutations]",
3022 "Print command list\n"
3023 "Print all possible command permutations\n")
3024{
d62a17ae 3025 return cmd_list_cmds(vty, argc == 2);
0b84f294
DL
3026}
3027
193a5a95
QY
3028DEFUN (vtysh_output_file,
3029 vtysh_output_file_cmd,
3030 "output file FILE",
3031 "Direct vtysh output to file\n"
3032 "Direct vtysh output to file\n"
3033 "Path to dump output to\n")
3034{
3035 const char *path = argv[argc - 1]->arg;
2cddf2ff
QY
3036 vty->of = fopen(path, "a");
3037 if (!vty->of) {
3038 vty_out(vty, "Failed to open file '%s': %s\n", path,
193a5a95 3039 safe_strerror(errno));
2cddf2ff 3040 vty->of = stdout;
193a5a95
QY
3041 }
3042 return CMD_SUCCESS;
3043}
3044
3045DEFUN (no_vtysh_output_file,
3046 no_vtysh_output_file_cmd,
3047 "no output file [FILE]",
3048 NO_STR
3049 "Direct vtysh output to file\n"
3050 "Direct vtysh output to file\n"
3051 "Path to dump output to\n")
3052{
2cddf2ff
QY
3053 if (vty->of != stdout) {
3054 fclose(vty->of);
3055 vty->of = stdout;
193a5a95
QY
3056 }
3057 return CMD_SUCCESS;
3058}
3059
cf6c83e7
QY
3060DEFUN(find,
3061 find_cmd,
3062 "find COMMAND...",
3063 "Find CLI command containing text\n"
3064 "Text to search for\n")
3065{
3066 char *text = argv_concat(argv, argc, 1);
3067 const struct cmd_node *node;
3068 const struct cmd_element *cli;
3069 vector clis;
3070
3071 for (unsigned int i = 0; i < vector_active(cmdvec); i++) {
3072 node = vector_slot(cmdvec, i);
3073 if (!node)
3074 continue;
3075 clis = node->cmd_vector;
3076 for (unsigned int j = 0; j < vector_active(clis); j++) {
3077 cli = vector_slot(clis, j);
3078 if (strcasestr(cli->string, text))
2cddf2ff 3079 vty_out(vty, " (%s) %s\n",
cf6c83e7
QY
3080 node_names[node->node], cli->string);
3081 }
3082 }
3083
3084 XFREE(MTYPE_TMP, text);
3085
3086 return CMD_SUCCESS;
3087}
3088
26fbe472
QY
3089DEFUN_HIDDEN(show_cli_graph_vtysh,
3090 show_cli_graph_vtysh_cmd,
3091 "show cli graph",
3092 SHOW_STR
3093 "CLI reflection\n"
3094 "Dump current command space as DOT graph\n")
3095{
3096 struct cmd_node *cn = vector_slot(cmdvec, vty->node);
3097 char *dot = cmd_graph_dump_dot(cn->cmdgraph);
3098
3099 vty_out(vty, "%s\n", dot);
3100 XFREE(MTYPE_TMP, dot);
3101 return CMD_SUCCESS;
3102}
3103
d62a17ae 3104static void vtysh_install_default(enum node_type node)
718e3744 3105{
d62a17ae 3106 install_element(node, &config_list_cmd);
cf6c83e7 3107 install_element(node, &find_cmd);
26fbe472 3108 install_element(node, &show_cli_graph_vtysh_cmd);
193a5a95
QY
3109 install_element(node, &vtysh_output_file_cmd);
3110 install_element(node, &no_vtysh_output_file_cmd);
718e3744 3111}
3112
3113/* Making connection to protocol daemon. */
d62a17ae 3114static int vtysh_connect(struct vtysh_client *vclient)
3115{
3116 int ret;
3117 int sock, len;
3118 struct sockaddr_un addr;
3119 struct stat s_stat;
3120 const char *path;
3121
3122 if (!vclient->path[0])
3123 snprintf(vclient->path, sizeof(vclient->path), "%s/%s.vty",
9b8a8249 3124 vtydir, vclient->name);
d62a17ae 3125 path = vclient->path;
3126
3127 /* Stat socket to see if we have permission to access it. */
3128 ret = stat(path, &s_stat);
3129 if (ret < 0 && errno != ENOENT) {
3130 fprintf(stderr, "vtysh_connect(%s): stat = %s\n", path,
3131 safe_strerror(errno));
3132 exit(1);
3133 }
3134
3135 if (ret >= 0) {
3136 if (!S_ISSOCK(s_stat.st_mode)) {
3137 fprintf(stderr, "vtysh_connect(%s): Not a socket\n",
3138 path);
3139 exit(1);
3140 }
718e3744 3141 }
718e3744 3142
d62a17ae 3143 sock = socket(AF_UNIX, SOCK_STREAM, 0);
3144 if (sock < 0) {
718e3744 3145#ifdef DEBUG
d62a17ae 3146 fprintf(stderr, "vtysh_connect(%s): socket = %s\n", path,
3147 safe_strerror(errno));
718e3744 3148#endif /* DEBUG */
d62a17ae 3149 return -1;
3150 }
718e3744 3151
d62a17ae 3152 memset(&addr, 0, sizeof(struct sockaddr_un));
3153 addr.sun_family = AF_UNIX;
3154 strlcpy(addr.sun_path, path, sizeof(addr.sun_path));
6f0e3f6e 3155#ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
d62a17ae 3156 len = addr.sun_len = SUN_LEN(&addr);
718e3744 3157#else
d62a17ae 3158 len = sizeof(addr.sun_family) + strlen(addr.sun_path);
6f0e3f6e 3159#endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
718e3744 3160
d62a17ae 3161 ret = connect(sock, (struct sockaddr *)&addr, len);
3162 if (ret < 0) {
718e3744 3163#ifdef DEBUG
d62a17ae 3164 fprintf(stderr, "vtysh_connect(%s): connect = %s\n", path,
3165 safe_strerror(errno));
718e3744 3166#endif /* DEBUG */
d62a17ae 3167 close(sock);
3168 return -1;
3169 }
3170 vclient->fd = sock;
718e3744 3171
d62a17ae 3172 return 0;
718e3744 3173}
3174
67736451
MS
3175static int vtysh_reconnect(struct vtysh_client *vclient)
3176{
3177 int ret;
3178
3179 fprintf(stderr, "Warning: connecting to %s...", vclient->name);
3180 ret = vtysh_connect(vclient);
3181 if (ret < 0) {
3182 fprintf(stderr, "failed!\n");
3183 return ret;
3184 }
3185 fprintf(stderr, "success!\n");
2cddf2ff 3186 if (vtysh_client_execute(vclient, "enable") < 0)
67736451
MS
3187 return -1;
3188 return vtysh_execute_no_pager("end");
3189}
3190
7c8ff89e 3191/* Return true if str ends with suffix, else return false */
d62a17ae 3192static int ends_with(const char *str, const char *suffix)
7c8ff89e 3193{
d62a17ae 3194 if (!str || !suffix)
3195 return 0;
3196 size_t lenstr = strlen(str);
3197 size_t lensuffix = strlen(suffix);
3198 if (lensuffix > lenstr)
3199 return 0;
3200 return strncmp(str + lenstr - lensuffix, suffix, lensuffix) == 0;
7c8ff89e
DS
3201}
3202
d62a17ae 3203static void vtysh_client_sorted_insert(struct vtysh_client *head_client,
3204 struct vtysh_client *client)
7c8ff89e 3205{
d62a17ae 3206 struct vtysh_client *prev_node, *current_node;
7c8ff89e 3207
d62a17ae 3208 prev_node = head_client;
3209 current_node = head_client->next;
3210 while (current_node) {
3211 if (strcmp(current_node->path, client->path) > 0)
3212 break;
7c8ff89e 3213
d62a17ae 3214 prev_node = current_node;
3215 current_node = current_node->next;
3216 }
3217 client->next = current_node;
3218 prev_node->next = client;
7c8ff89e
DS
3219}
3220
3221#define MAXIMUM_INSTANCES 10
3222
d62a17ae 3223static void vtysh_update_all_insances(struct vtysh_client *head_client)
3224{
3225 struct vtysh_client *client;
3226 DIR *dir;
3227 struct dirent *file;
3228 int n = 0;
3229
3230 if (head_client->flag != VTYSH_OSPFD)
3231 return;
3232
3233 /* ls vty_sock_dir and look for all files ending in .vty */
9b8a8249 3234 dir = opendir(vtydir);
d62a17ae 3235 if (dir) {
3236 while ((file = readdir(dir)) != NULL) {
3237 if (begins_with(file->d_name, "ospfd-")
3238 && ends_with(file->d_name, ".vty")) {
3239 if (n == MAXIMUM_INSTANCES) {
3240 fprintf(stderr,
3241 "Parsing %s, client limit(%d) reached!\n",
9b8a8249 3242 vtydir, n);
d62a17ae 3243 break;
3244 }
3245 client = (struct vtysh_client *)malloc(
3246 sizeof(struct vtysh_client));
3247 client->fd = -1;
3248 client->name = "ospfd";
3249 client->flag = VTYSH_OSPFD;
3250 snprintf(client->path, sizeof(client->path),
9b8a8249 3251 "%s/%s", vtydir, file->d_name);
d62a17ae 3252 client->next = NULL;
3253 vtysh_client_sorted_insert(head_client, client);
3254 n++;
3255 }
3256 }
3257 closedir(dir);
3258 }
3259}
3260
3261static int vtysh_connect_all_instances(struct vtysh_client *head_client)
3262{
3263 struct vtysh_client *client;
3264 int rc = 0;
3265
3266 vtysh_update_all_insances(head_client);
3267
3268 client = head_client->next;
3269 while (client) {
3270 if (vtysh_connect(client) == 0)
3271 rc++;
3272 client = client->next;
3273 }
3274
3275 return rc;
3276}
3277
3278int vtysh_connect_all(const char *daemon_name)
3279{
d7c0a89a 3280 unsigned int i;
d62a17ae 3281 int rc = 0;
3282 int matches = 0;
3283
3284 for (i = 0; i < array_size(vtysh_client); i++) {
3285 if (!daemon_name
3286 || !strcmp(daemon_name, vtysh_client[i].name)) {
3287 matches++;
3288 if (vtysh_connect(&vtysh_client[i]) == 0)
3289 rc++;
3290
3291 rc += vtysh_connect_all_instances(&vtysh_client[i]);
3292 }
3293 }
3294 if (!matches)
3295 fprintf(stderr, "Error: no daemons match name %s!\n",
3296 daemon_name);
3297 return rc;
718e3744 3298}
3299
95e735b5 3300/* To disable readline's filename completion. */
d62a17ae 3301static char *vtysh_completion_entry_function(const char *ignore,
3302 int invoking_key)
718e3744 3303{
d62a17ae 3304 return NULL;
718e3744 3305}
3306
d62a17ae 3307void vtysh_readline_init(void)
718e3744 3308{
d62a17ae 3309 /* readline related settings. */
3310 rl_initialize();
3311 rl_bind_key('?', (rl_command_func_t *)vtysh_rl_describe);
3312 rl_completion_entry_function = vtysh_completion_entry_function;
3313 rl_attempted_completion_function =
3314 (rl_completion_func_t *)new_completion;
718e3744 3315}
3316
d62a17ae 3317char *vtysh_prompt(void)
718e3744 3318{
d62a17ae 3319 static char buf[100];
718e3744 3320
6b3ee3a0 3321 snprintf(buf, sizeof buf, cmd_prompt(vty->node), cmd_hostname_get());
d62a17ae 3322 return buf;
718e3744 3323}
3324
7f059ea6
DL
3325static void vtysh_ac_line(void *arg, const char *line)
3326{
d62a17ae 3327 vector comps = arg;
3328 size_t i;
3329 for (i = 0; i < vector_active(comps); i++)
3330 if (!strcmp(line, (char *)vector_slot(comps, i)))
3331 return;
3332 vector_set(comps, XSTRDUP(MTYPE_COMPLETION, line));
7f059ea6
DL
3333}
3334
3335static void vtysh_autocomplete(vector comps, struct cmd_token *token)
3336{
d62a17ae 3337 char accmd[256];
3338 size_t i;
7f059ea6 3339
d62a17ae 3340 snprintf(accmd, sizeof(accmd), "autocomplete %d %s %s", token->type,
3341 token->text, token->varname ? token->varname : "-");
7f059ea6 3342
8d70e7fe
QY
3343 vty->of_saved = vty->of;
3344 vty->of = NULL;
d62a17ae 3345 for (i = 0; i < array_size(vtysh_client); i++)
2cddf2ff
QY
3346 vtysh_client_run_all(&vtysh_client[i], accmd, 1, vtysh_ac_line,
3347 comps);
8d70e7fe 3348 vty->of = vty->of_saved;
7f059ea6
DL
3349}
3350
1d6664e0 3351static const struct cmd_variable_handler vtysh_var_handler[] = {
d62a17ae 3352 {/* match all */
3353 .tokenname = NULL,
3354 .varname = NULL,
3355 .completions = vtysh_autocomplete},
3356 {.completions = NULL}};
3357
193a5a95
QY
3358void vtysh_uninit()
3359{
2cddf2ff
QY
3360 if (vty->of != stdout)
3361 fclose(vty->of);
193a5a95
QY
3362}
3363
d62a17ae 3364void vtysh_init_vty(void)
3365{
3366 /* Make vty structure. */
3367 vty = vty_new();
3368 vty->type = VTY_SHELL;
3369 vty->node = VIEW_NODE;
3370
193a5a95 3371 /* set default output */
2cddf2ff 3372 vty->of = stdout;
193a5a95 3373
d62a17ae 3374 /* Initialize commands. */
3375 cmd_init(0);
3376 cmd_variable_handler_register(vtysh_var_handler);
3377
3378 /* Install nodes. */
3379 install_node(&bgp_node, NULL);
3380 install_node(&rip_node, NULL);
3381 install_node(&interface_node, NULL);
2dd0d726 3382 install_node(&pw_node, NULL);
d62a17ae 3383 install_node(&link_params_node, NULL);
f5d20fdb 3384 install_node(&logicalrouter_node, NULL);
d62a17ae 3385 install_node(&vrf_node, NULL);
e5c83d9b 3386 install_node(&nh_group_node, NULL);
d62a17ae 3387 install_node(&rmap_node, NULL);
e5c83d9b 3388 install_node(&pbr_map_node, NULL);
d62a17ae 3389 install_node(&zebra_node, NULL);
3390 install_node(&bgp_vpnv4_node, NULL);
3391 install_node(&bgp_vpnv6_node, NULL);
e7d78d0f
PG
3392 install_node(&bgp_flowspecv4_node, NULL);
3393 install_node(&bgp_flowspecv6_node, NULL);
d62a17ae 3394 install_node(&bgp_ipv4_node, NULL);
3395 install_node(&bgp_ipv4m_node, NULL);
3396 install_node(&bgp_ipv4l_node, NULL);
3397 install_node(&bgp_ipv6_node, NULL);
3398 install_node(&bgp_ipv6m_node, NULL);
3399 install_node(&bgp_ipv6l_node, NULL);
3400 install_node(&bgp_vrf_policy_node, NULL);
3401 install_node(&bgp_evpn_node, NULL);
3402 install_node(&bgp_evpn_vni_node, NULL);
3403 install_node(&bgp_vnc_defaults_node, NULL);
3404 install_node(&bgp_vnc_nve_group_node, NULL);
3405 install_node(&bgp_vnc_l2_group_node, NULL);
3406 install_node(&ospf_node, NULL);
3407 install_node(&eigrp_node, NULL);
3408 install_node(&babel_node, NULL);
3409 install_node(&ripng_node, NULL);
3410 install_node(&ospf6_node, NULL);
3411 install_node(&ldp_node, NULL);
3412 install_node(&ldp_ipv4_node, NULL);
3413 install_node(&ldp_ipv6_node, NULL);
3414 install_node(&ldp_ipv4_iface_node, NULL);
3415 install_node(&ldp_ipv6_iface_node, NULL);
3416 install_node(&ldp_l2vpn_node, NULL);
3417 install_node(&ldp_pseudowire_node, NULL);
3418 install_node(&keychain_node, NULL);
3419 install_node(&keychain_key_node, NULL);
3420 install_node(&isis_node, NULL);
3421 install_node(&vty_node, NULL);
23489cb0 3422#if defined(HAVE_RPKI)
dabecd7c 3423 install_node(&rpki_node, NULL);
23489cb0 3424#endif
d62a17ae 3425
cf6c83e7
QY
3426 struct cmd_node *node;
3427 for (unsigned int i = 0; i < vector_active(cmdvec); i++) {
3428 node = vector_slot(cmdvec, i);
3429 if (!node || node->node == VIEW_NODE)
3430 continue;
3431 vtysh_install_default(node->node);
3432 }
d62a17ae 3433
3434 install_element(VIEW_NODE, &vtysh_enable_cmd);
3435 install_element(ENABLE_NODE, &vtysh_config_terminal_cmd);
3436 install_element(ENABLE_NODE, &vtysh_disable_cmd);
3437
3438 /* "exit" command. */
3439 install_element(VIEW_NODE, &vtysh_exit_all_cmd);
3440 install_element(CONFIG_NODE, &vtysh_exit_all_cmd);
3441 install_element(VIEW_NODE, &vtysh_quit_all_cmd);
3442 install_element(CONFIG_NODE, &vtysh_quit_all_cmd);
3443 install_element(RIP_NODE, &vtysh_exit_ripd_cmd);
3444 install_element(RIP_NODE, &vtysh_quit_ripd_cmd);
3445 install_element(RIPNG_NODE, &vtysh_exit_ripngd_cmd);
3446 install_element(RIPNG_NODE, &vtysh_quit_ripngd_cmd);
3447 install_element(OSPF_NODE, &vtysh_exit_ospfd_cmd);
3448 install_element(OSPF_NODE, &vtysh_quit_ospfd_cmd);
3449 install_element(EIGRP_NODE, &vtysh_exit_eigrpd_cmd);
3450 install_element(EIGRP_NODE, &vtysh_quit_eigrpd_cmd);
3451 install_element(BABEL_NODE, &vtysh_exit_babeld_cmd);
3452 install_element(BABEL_NODE, &vtysh_quit_babeld_cmd);
3453 install_element(OSPF6_NODE, &vtysh_exit_ospf6d_cmd);
3454 install_element(OSPF6_NODE, &vtysh_quit_ospf6d_cmd);
3455#if defined(HAVE_LDPD)
3456 install_element(LDP_NODE, &vtysh_exit_ldpd_cmd);
3457 install_element(LDP_NODE, &vtysh_quit_ldpd_cmd);
3458 install_element(LDP_IPV4_NODE, &vtysh_exit_ldpd_cmd);
3459 install_element(LDP_IPV4_NODE, &vtysh_quit_ldpd_cmd);
983bd6f7 3460 install_element(LDP_IPV4_NODE, &ldp_exit_address_family_cmd);
d62a17ae 3461 install_element(LDP_IPV6_NODE, &vtysh_exit_ldpd_cmd);
3462 install_element(LDP_IPV6_NODE, &vtysh_quit_ldpd_cmd);
983bd6f7 3463 install_element(LDP_IPV6_NODE, &ldp_exit_address_family_cmd);
d62a17ae 3464 install_element(LDP_IPV4_IFACE_NODE, &vtysh_exit_ldpd_cmd);
3465 install_element(LDP_IPV4_IFACE_NODE, &vtysh_quit_ldpd_cmd);
3466 install_element(LDP_IPV6_IFACE_NODE, &vtysh_exit_ldpd_cmd);
3467 install_element(LDP_IPV6_IFACE_NODE, &vtysh_quit_ldpd_cmd);
3468 install_element(LDP_L2VPN_NODE, &vtysh_exit_ldpd_cmd);
3469 install_element(LDP_L2VPN_NODE, &vtysh_quit_ldpd_cmd);
3470 install_element(LDP_PSEUDOWIRE_NODE, &vtysh_exit_ldpd_cmd);
3471 install_element(LDP_PSEUDOWIRE_NODE, &vtysh_quit_ldpd_cmd);
87ab4aec 3472#endif
d62a17ae 3473 install_element(BGP_NODE, &vtysh_exit_bgpd_cmd);
3474 install_element(BGP_NODE, &vtysh_quit_bgpd_cmd);
3475 install_element(BGP_VPNV4_NODE, &vtysh_exit_bgpd_cmd);
3476 install_element(BGP_VPNV4_NODE, &vtysh_quit_bgpd_cmd);
3477 install_element(BGP_VPNV6_NODE, &vtysh_exit_bgpd_cmd);
3478 install_element(BGP_VPNV6_NODE, &vtysh_quit_bgpd_cmd);
e7d78d0f
PG
3479 install_element(BGP_FLOWSPECV4_NODE, &vtysh_exit_bgpd_cmd);
3480 install_element(BGP_FLOWSPECV4_NODE, &vtysh_quit_bgpd_cmd);
3481 install_element(BGP_FLOWSPECV6_NODE, &vtysh_exit_bgpd_cmd);
3482 install_element(BGP_FLOWSPECV6_NODE, &vtysh_quit_bgpd_cmd);
d62a17ae 3483 install_element(BGP_IPV4_NODE, &vtysh_exit_bgpd_cmd);
3484 install_element(BGP_IPV4_NODE, &vtysh_quit_bgpd_cmd);
3485 install_element(BGP_IPV4M_NODE, &vtysh_exit_bgpd_cmd);
3486 install_element(BGP_IPV4M_NODE, &vtysh_quit_bgpd_cmd);
3487 install_element(BGP_IPV4L_NODE, &vtysh_exit_bgpd_cmd);
3488 install_element(BGP_IPV4L_NODE, &vtysh_quit_bgpd_cmd);
3489 install_element(BGP_IPV6_NODE, &vtysh_exit_bgpd_cmd);
3490 install_element(BGP_IPV6_NODE, &vtysh_quit_bgpd_cmd);
3491 install_element(BGP_IPV6M_NODE, &vtysh_exit_bgpd_cmd);
3492 install_element(BGP_IPV6M_NODE, &vtysh_quit_bgpd_cmd);
3493 install_element(BGP_EVPN_NODE, &vtysh_quit_bgpd_cmd);
3494 install_element(BGP_EVPN_NODE, &vtysh_exit_bgpd_cmd);
3495 install_element(BGP_EVPN_VNI_NODE, &vtysh_exit_bgpd_cmd);
3496 install_element(BGP_EVPN_VNI_NODE, &vtysh_quit_bgpd_cmd);
3497 install_element(BGP_IPV6L_NODE, &vtysh_exit_bgpd_cmd);
3498 install_element(BGP_IPV6L_NODE, &vtysh_quit_bgpd_cmd);
3499#if defined(ENABLE_BGP_VNC)
3500 install_element(BGP_VRF_POLICY_NODE, &vtysh_exit_bgpd_cmd);
3501 install_element(BGP_VRF_POLICY_NODE, &vtysh_quit_bgpd_cmd);
3502 install_element(BGP_VNC_DEFAULTS_NODE, &vtysh_exit_bgpd_cmd);
3503 install_element(BGP_VNC_DEFAULTS_NODE, &vtysh_quit_bgpd_cmd);
3504 install_element(BGP_VNC_NVE_GROUP_NODE, &vtysh_exit_bgpd_cmd);
3505 install_element(BGP_VNC_NVE_GROUP_NODE, &vtysh_quit_bgpd_cmd);
3506 install_element(BGP_VNC_L2_GROUP_NODE, &vtysh_exit_bgpd_cmd);
3507 install_element(BGP_VNC_L2_GROUP_NODE, &vtysh_quit_bgpd_cmd);
87ab4aec 3508#endif
d62a17ae 3509 install_element(ISIS_NODE, &vtysh_exit_isisd_cmd);
3510 install_element(ISIS_NODE, &vtysh_quit_isisd_cmd);
3511 install_element(KEYCHAIN_NODE, &vtysh_exit_ripd_cmd);
3512 install_element(KEYCHAIN_NODE, &vtysh_quit_ripd_cmd);
3513 install_element(KEYCHAIN_KEY_NODE, &vtysh_exit_ripd_cmd);
3514 install_element(KEYCHAIN_KEY_NODE, &vtysh_quit_ripd_cmd);
3515 install_element(RMAP_NODE, &vtysh_exit_rmap_cmd);
3516 install_element(RMAP_NODE, &vtysh_quit_rmap_cmd);
e5c83d9b
DS
3517 install_element(PBRMAP_NODE, &vtysh_exit_pbr_map_cmd);
3518 install_element(PBRMAP_NODE, &vtysh_quit_pbr_map_cmd);
d62a17ae 3519 install_element(VTY_NODE, &vtysh_exit_line_vty_cmd);
3520 install_element(VTY_NODE, &vtysh_quit_line_vty_cmd);
3521
3522 /* "end" command. */
3523 install_element(CONFIG_NODE, &vtysh_end_all_cmd);
3524 install_element(ENABLE_NODE, &vtysh_end_all_cmd);
3525 install_element(RIP_NODE, &vtysh_end_all_cmd);
3526 install_element(RIPNG_NODE, &vtysh_end_all_cmd);
3527 install_element(OSPF_NODE, &vtysh_end_all_cmd);
3528 install_element(EIGRP_NODE, &vtysh_end_all_cmd);
3529 install_element(BABEL_NODE, &vtysh_end_all_cmd);
3530 install_element(OSPF6_NODE, &vtysh_end_all_cmd);
3531 install_element(LDP_NODE, &vtysh_end_all_cmd);
3532 install_element(LDP_IPV4_NODE, &vtysh_end_all_cmd);
3533 install_element(LDP_IPV6_NODE, &vtysh_end_all_cmd);
3534 install_element(LDP_IPV4_IFACE_NODE, &vtysh_end_all_cmd);
3535 install_element(LDP_IPV6_IFACE_NODE, &vtysh_end_all_cmd);
3536 install_element(LDP_L2VPN_NODE, &vtysh_end_all_cmd);
3537 install_element(LDP_PSEUDOWIRE_NODE, &vtysh_end_all_cmd);
3538 install_element(BGP_NODE, &vtysh_end_all_cmd);
3539 install_element(BGP_IPV4_NODE, &vtysh_end_all_cmd);
3540 install_element(BGP_IPV4M_NODE, &vtysh_end_all_cmd);
3541 install_element(BGP_IPV4L_NODE, &vtysh_end_all_cmd);
3542 install_element(BGP_VPNV4_NODE, &vtysh_end_all_cmd);
3543 install_element(BGP_VPNV6_NODE, &vtysh_end_all_cmd);
e7d78d0f
PG
3544 install_element(BGP_FLOWSPECV4_NODE, &vtysh_end_all_cmd);
3545 install_element(BGP_FLOWSPECV6_NODE, &vtysh_end_all_cmd);
d62a17ae 3546 install_element(BGP_IPV6_NODE, &vtysh_end_all_cmd);
3547 install_element(BGP_IPV6M_NODE, &vtysh_end_all_cmd);
3548 install_element(BGP_IPV6L_NODE, &vtysh_end_all_cmd);
3549 install_element(BGP_VRF_POLICY_NODE, &vtysh_end_all_cmd);
3550 install_element(BGP_EVPN_NODE, &vtysh_end_all_cmd);
3551 install_element(BGP_EVPN_VNI_NODE, &vtysh_end_all_cmd);
3552 install_element(BGP_VNC_DEFAULTS_NODE, &vtysh_end_all_cmd);
3553 install_element(BGP_VNC_NVE_GROUP_NODE, &vtysh_end_all_cmd);
3554 install_element(BGP_VNC_L2_GROUP_NODE, &vtysh_end_all_cmd);
3555 install_element(ISIS_NODE, &vtysh_end_all_cmd);
3556 install_element(KEYCHAIN_NODE, &vtysh_end_all_cmd);
3557 install_element(KEYCHAIN_KEY_NODE, &vtysh_end_all_cmd);
3558 install_element(RMAP_NODE, &vtysh_end_all_cmd);
e5c83d9b 3559 install_element(PBRMAP_NODE, &vtysh_end_all_cmd);
d62a17ae 3560 install_element(VTY_NODE, &vtysh_end_all_cmd);
3561
d62a17ae 3562 install_element(INTERFACE_NODE, &vtysh_end_all_cmd);
3563 install_element(INTERFACE_NODE, &vtysh_exit_interface_cmd);
3564 install_element(LINK_PARAMS_NODE, &exit_link_params_cmd);
3565 install_element(LINK_PARAMS_NODE, &vtysh_end_all_cmd);
3566 install_element(LINK_PARAMS_NODE, &vtysh_exit_interface_cmd);
3567 install_element(INTERFACE_NODE, &vtysh_quit_interface_cmd);
3568
2dd0d726
RW
3569 install_element(PW_NODE, &vtysh_end_all_cmd);
3570 install_element(PW_NODE, &vtysh_exit_interface_cmd);
3571 install_element(PW_NODE, &vtysh_quit_interface_cmd);
3572
f5d20fdb 3573 install_element(LOGICALROUTER_NODE, &vtysh_end_all_cmd);
d62a17ae 3574
f5d20fdb
PG
3575 install_element(CONFIG_NODE, &vtysh_logicalrouter_cmd);
3576 install_element(CONFIG_NODE, &vtysh_no_logicalrouter_cmd);
996c9314
LB
3577 install_element(LOGICALROUTER_NODE, &vtysh_exit_logicalrouter_cmd);
3578 install_element(LOGICALROUTER_NODE, &vtysh_quit_logicalrouter_cmd);
d62a17ae 3579
e5c83d9b
DS
3580 install_element(CONFIG_NODE, &vtysh_nexthop_group_cmd);
3581 install_element(NH_GROUP_NODE, &vtysh_end_all_cmd);
3582 install_element(NH_GROUP_NODE, &vtysh_exit_nexthop_group_cmd);
3583 install_element(NH_GROUP_NODE, &vtysh_quit_nexthop_group_cmd);
3584
d62a17ae 3585 install_element(VRF_NODE, &vtysh_end_all_cmd);
3586 install_element(VRF_NODE, &vtysh_exit_vrf_cmd);
3587 install_element(VRF_NODE, &vtysh_quit_vrf_cmd);
3588
3589 install_element(CONFIG_NODE, &router_eigrp_cmd);
3590 install_element(CONFIG_NODE, &router_babel_cmd);
3591 install_element(CONFIG_NODE, &router_rip_cmd);
3592 install_element(CONFIG_NODE, &router_ripng_cmd);
3593 install_element(CONFIG_NODE, &router_ospf_cmd);
3594 install_element(CONFIG_NODE, &router_ospf6_cmd);
3595#if defined(HAVE_LDPD)
3596 install_element(CONFIG_NODE, &ldp_mpls_ldp_cmd);
3597 install_element(LDP_NODE, &ldp_address_family_ipv4_cmd);
3598 install_element(LDP_NODE, &ldp_address_family_ipv6_cmd);
3599 install_element(LDP_IPV4_NODE, &ldp_interface_ifname_cmd);
3600 install_element(LDP_IPV6_NODE, &ldp_interface_ifname_cmd);
3601 install_element(CONFIG_NODE, &ldp_l2vpn_word_type_vpls_cmd);
3602 install_element(LDP_L2VPN_NODE, &ldp_member_pseudowire_ifname_cmd);
87ab4aec 3603#endif
d62a17ae 3604 install_element(CONFIG_NODE, &router_isis_cmd);
3605 install_element(CONFIG_NODE, &router_bgp_cmd);
3606 install_element(BGP_NODE, &address_family_vpnv4_cmd);
3607 install_element(BGP_NODE, &address_family_vpnv6_cmd);
87ab4aec 3608#if defined(ENABLE_BGP_VNC)
d62a17ae 3609 install_element(BGP_NODE, &vnc_vrf_policy_cmd);
3610 install_element(BGP_NODE, &vnc_defaults_cmd);
3611 install_element(BGP_NODE, &vnc_nve_group_cmd);
3612 install_element(BGP_NODE, &vnc_l2_group_cmd);
87ab4aec 3613#endif
d62a17ae 3614 install_element(BGP_NODE, &address_family_ipv4_cmd);
3615 install_element(BGP_NODE, &address_family_ipv4_multicast_cmd);
3616 install_element(BGP_NODE, &address_family_ipv4_vpn_cmd);
3617 install_element(BGP_NODE, &address_family_ipv4_labeled_unicast_cmd);
3618 install_element(BGP_NODE, &address_family_ipv6_cmd);
3619 install_element(BGP_NODE, &address_family_ipv6_multicast_cmd);
3620 install_element(BGP_NODE, &address_family_ipv6_vpn_cmd);
3621 install_element(BGP_NODE, &address_family_ipv6_labeled_unicast_cmd);
3622 install_element(BGP_NODE, &address_family_evpn_cmd);
e7d78d0f
PG
3623 install_element(BGP_NODE, &address_family_flowspecv4_cmd);
3624 install_element(BGP_NODE, &address_family_flowspecv6_cmd);
d62a17ae 3625#if defined(HAVE_CUMULUS)
3626 install_element(BGP_NODE, &address_family_evpn2_cmd);
90e60aa7 3627#endif
d62a17ae 3628 install_element(BGP_VPNV4_NODE, &exit_address_family_cmd);
3629 install_element(BGP_VPNV6_NODE, &exit_address_family_cmd);
3630 install_element(BGP_IPV4_NODE, &exit_address_family_cmd);
3631 install_element(BGP_IPV4M_NODE, &exit_address_family_cmd);
3632 install_element(BGP_IPV4L_NODE, &exit_address_family_cmd);
3633 install_element(BGP_IPV6_NODE, &exit_address_family_cmd);
3634 install_element(BGP_IPV6M_NODE, &exit_address_family_cmd);
3635 install_element(BGP_EVPN_NODE, &exit_address_family_cmd);
3636 install_element(BGP_IPV6L_NODE, &exit_address_family_cmd);
e7d78d0f
PG
3637 install_element(BGP_FLOWSPECV4_NODE, &exit_address_family_cmd);
3638 install_element(BGP_FLOWSPECV6_NODE, &exit_address_family_cmd);
d62a17ae 3639
23489cb0 3640#if defined(HAVE_RPKI)
dabecd7c
MR
3641 install_element(CONFIG_NODE, &rpki_cmd);
3642 install_element(RPKI_NODE, &rpki_exit_cmd);
3643 install_element(RPKI_NODE, &rpki_quit_cmd);
3644 install_element(RPKI_NODE, &vtysh_end_all_cmd);
23489cb0 3645#endif
dabecd7c 3646
d62a17ae 3647 /* EVPN commands */
3648 install_element(BGP_EVPN_NODE, &bgp_evpn_vni_cmd);
3649 install_element(BGP_EVPN_VNI_NODE, &exit_vni_cmd);
3650
3651 install_element(BGP_VRF_POLICY_NODE, &exit_vrf_policy_cmd);
3652 install_element(BGP_VNC_DEFAULTS_NODE, &exit_vnc_config_cmd);
3653 install_element(BGP_VNC_NVE_GROUP_NODE, &exit_vnc_config_cmd);
3654 install_element(BGP_VNC_L2_GROUP_NODE, &exit_vnc_config_cmd);
3655
3656 install_element(CONFIG_NODE, &key_chain_cmd);
3657 install_element(CONFIG_NODE, &vtysh_route_map_cmd);
e5c83d9b
DS
3658 install_element(CONFIG_NODE, &vtysh_pbr_map_cmd);
3659 install_element(CONFIG_NODE, &vtysh_no_pbr_map_cmd);
d62a17ae 3660 install_element(CONFIG_NODE, &vtysh_line_vty_cmd);
3661 install_element(KEYCHAIN_NODE, &key_cmd);
3662 install_element(KEYCHAIN_NODE, &key_chain_cmd);
3663 install_element(KEYCHAIN_KEY_NODE, &key_chain_cmd);
3664 install_element(CONFIG_NODE, &vtysh_interface_cmd);
2dd0d726 3665 install_element(CONFIG_NODE, &vtysh_pseudowire_cmd);
d62a17ae 3666 install_element(INTERFACE_NODE, &vtysh_link_params_cmd);
3667 install_element(ENABLE_NODE, &vtysh_show_running_config_cmd);
3668 install_element(ENABLE_NODE, &vtysh_copy_running_config_cmd);
3669
34c46274
RW
3670 install_element(CONFIG_NODE, &vtysh_vrf_cmd);
3671 install_element(VRF_NODE, &vtysh_vrf_netns_cmd);
3672 install_element(VRF_NODE, &vtysh_no_vrf_netns_cmd);
16d6ea59
QY
3673 install_element(VRF_NODE, &exit_vrf_config_cmd);
3674
e5c83d9b 3675 install_element(CONFIG_NODE, &vtysh_no_nexthop_group_cmd);
d62a17ae 3676
3677 /* "write terminal" command. */
3678 install_element(ENABLE_NODE, &vtysh_write_terminal_cmd);
3679
3680 install_element(CONFIG_NODE, &vtysh_integrated_config_cmd);
3681 install_element(CONFIG_NODE, &no_vtysh_integrated_config_cmd);
3682
3683 /* "write memory" command. */
3684 install_element(ENABLE_NODE, &vtysh_write_memory_cmd);
3685
3686 install_element(VIEW_NODE, &vtysh_terminal_length_cmd);
3687 install_element(VIEW_NODE, &vtysh_terminal_no_length_cmd);
3688 install_element(VIEW_NODE, &vtysh_show_daemons_cmd);
3689
3690 install_element(VIEW_NODE, &vtysh_ping_cmd);
3691 install_element(VIEW_NODE, &vtysh_ping_ip_cmd);
3692 install_element(VIEW_NODE, &vtysh_traceroute_cmd);
3693 install_element(VIEW_NODE, &vtysh_traceroute_ip_cmd);
4d9ad5dc 3694 install_element(VIEW_NODE, &vtysh_mtrace_cmd);
d62a17ae 3695 install_element(VIEW_NODE, &vtysh_ping6_cmd);
3696 install_element(VIEW_NODE, &vtysh_traceroute6_cmd);
576b6b5d 3697#if defined(HAVE_SHELL_ACCESS)
d62a17ae 3698 install_element(VIEW_NODE, &vtysh_telnet_cmd);
3699 install_element(VIEW_NODE, &vtysh_telnet_port_cmd);
3700 install_element(VIEW_NODE, &vtysh_ssh_cmd);
4eeccf18 3701#endif
576b6b5d 3702#if defined(HAVE_SHELL_ACCESS)
d62a17ae 3703 install_element(ENABLE_NODE, &vtysh_start_shell_cmd);
3704 install_element(ENABLE_NODE, &vtysh_start_bash_cmd);
3705 install_element(ENABLE_NODE, &vtysh_start_zsh_cmd);
576b6b5d
DS
3706#endif
3707
aea03ad6 3708 /* debugging */
87f6dc50 3709 install_element(VIEW_NODE, &vtysh_show_debugging_cmd);
40818cec 3710 install_element(VIEW_NODE, &vtysh_show_debugging_hashtable_cmd);
eb68fbc6 3711 install_element(ENABLE_NODE, &vtysh_debug_all_cmd);
aea03ad6
QY
3712 install_element(CONFIG_NODE, &vtysh_debug_all_cmd);
3713
3714 /* misc lib show commands */
d62a17ae 3715 install_element(VIEW_NODE, &vtysh_show_memory_cmd);
3716 install_element(VIEW_NODE, &vtysh_show_modules_cmd);
d62a17ae 3717 install_element(VIEW_NODE, &vtysh_show_work_queues_cmd);
3718 install_element(VIEW_NODE, &vtysh_show_work_queues_daemon_cmd);
d62a17ae 3719 install_element(VIEW_NODE, &vtysh_show_thread_cmd);
8872626b 3720 install_element(VIEW_NODE, &vtysh_show_poll_cmd);
d62a17ae 3721
3722 /* Logging */
3723 install_element(VIEW_NODE, &vtysh_show_logging_cmd);
3724 install_element(CONFIG_NODE, &vtysh_log_stdout_cmd);
3725 install_element(CONFIG_NODE, &vtysh_log_stdout_level_cmd);
3726 install_element(CONFIG_NODE, &no_vtysh_log_stdout_cmd);
3727 install_element(CONFIG_NODE, &vtysh_log_file_cmd);
3728 install_element(CONFIG_NODE, &vtysh_log_file_level_cmd);
3729 install_element(CONFIG_NODE, &no_vtysh_log_file_cmd);
3730 install_element(CONFIG_NODE, &vtysh_log_monitor_cmd);
3731 install_element(CONFIG_NODE, &no_vtysh_log_monitor_cmd);
3732 install_element(CONFIG_NODE, &vtysh_log_syslog_cmd);
3733 install_element(CONFIG_NODE, &no_vtysh_log_syslog_cmd);
d62a17ae 3734 install_element(CONFIG_NODE, &vtysh_log_facility_cmd);
3735 install_element(CONFIG_NODE, &no_vtysh_log_facility_cmd);
3736 install_element(CONFIG_NODE, &vtysh_log_record_priority_cmd);
3737 install_element(CONFIG_NODE, &no_vtysh_log_record_priority_cmd);
3738 install_element(CONFIG_NODE, &vtysh_log_timestamp_precision_cmd);
3739 install_element(CONFIG_NODE, &no_vtysh_log_timestamp_precision_cmd);
3740
3741 install_element(CONFIG_NODE, &vtysh_service_password_encrypt_cmd);
3742 install_element(CONFIG_NODE, &no_vtysh_service_password_encrypt_cmd);
3743
3744 install_element(CONFIG_NODE, &vtysh_password_cmd);
322e2d5c 3745 install_element(CONFIG_NODE, &no_vtysh_password_cmd);
d62a17ae 3746 install_element(CONFIG_NODE, &vtysh_enable_password_cmd);
3747 install_element(CONFIG_NODE, &no_vtysh_enable_password_cmd);
718e3744 3748}