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