📄 zebra_vty.c
字号:
/* Zebra VTY functions * Copyright (C) 2002 Kunihiro Ishiguro * * This file is part of GNU Zebra. * * GNU Zebra is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation; either version 2, or (at your option) any * later version. * * GNU Zebra is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with GNU Zebra; see the file COPYING. If not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. */#include <zebra.h>#include "if.h"#include "prefix.h"#include "command.h"#include "table.h"#include "rib.h"/* Return route type string for VTY output. */const char *route_type_str (u_char type){ switch (type) { case ZEBRA_ROUTE_SYSTEM: return "system"; case ZEBRA_ROUTE_KERNEL: return "kernel"; case ZEBRA_ROUTE_CONNECT: return "connected"; case ZEBRA_ROUTE_STATIC: return "static"; case ZEBRA_ROUTE_RIP: return "rip"; case ZEBRA_ROUTE_RIPNG: return "rip"; case ZEBRA_ROUTE_OSPF: return "ospf"; case ZEBRA_ROUTE_OSPF6: return "ospf"; case ZEBRA_ROUTE_BGP: return "bgp"; default: return "unknown"; }};/* Return route type string for VTY output. */const charroute_type_char (u_char type){ switch (type) { case ZEBRA_ROUTE_SYSTEM: return 'S'; case ZEBRA_ROUTE_KERNEL: return 'K'; case ZEBRA_ROUTE_CONNECT: return 'C'; case ZEBRA_ROUTE_STATIC: return 'S'; case ZEBRA_ROUTE_RIP: return 'R'; case ZEBRA_ROUTE_RIPNG: return 'R'; case ZEBRA_ROUTE_OSPF: return 'O'; case ZEBRA_ROUTE_OSPF6: return 'O'; case ZEBRA_ROUTE_BGP: return 'B'; default: return '?'; }};/* General fucntion for static route. */intzebra_static_ipv4 (struct vty *vty, int add_cmd, char *dest_str, char *mask_str, char *gate_str, char *distance_str){ int ret; u_char distance; struct prefix p; struct in_addr gate; struct in_addr mask; char *ifname; ret = str2prefix (dest_str, &p); if (ret <= 0) { vty_out (vty, "%% Malformed address%s", VTY_NEWLINE); return CMD_WARNING; } /* Cisco like mask notation. */ if (mask_str) { ret = inet_aton (mask_str, &mask); if (ret == 0) { vty_out (vty, "%% Malformed address%s", VTY_NEWLINE); return CMD_WARNING; } p.prefixlen = ip_masklen (mask); } /* Apply mask for given prefix. */ apply_mask (&p); /* Administrative distance. */ if (distance_str) distance = atoi (distance_str); else distance = ZEBRA_STATIC_DISTANCE_DEFAULT; /* Null0 static route. */ if (strncasecmp (gate_str, "Null0", strlen (gate_str)) == 0) { if (add_cmd) static_add_ipv4 (&p, NULL, NULL, distance, 0); else static_delete_ipv4 (&p, NULL, NULL, distance, 0); return CMD_SUCCESS; } /* When gateway is A.B.C.D format, gate is treated as nexthop address other case gate is treated as interface name. */ ret = inet_aton (gate_str, &gate); if (ret) ifname = NULL; else ifname = gate_str; if (add_cmd) static_add_ipv4 (&p, ifname ? NULL : &gate, ifname, distance, 0); else static_delete_ipv4 (&p, ifname ? NULL : &gate, ifname, distance, 0); return CMD_SUCCESS;}/* Static route configuration. */DEFUN (ip_route, ip_route_cmd, "ip route A.B.C.D/M (A.B.C.D|INTERFACE|null0)", IP_STR "Establish static routes\n" "IP destination prefix (e.g. 10.0.0.0/8)\n" "IP gateway address\n" "IP gateway interface name\n" "Null interface\n"){ return zebra_static_ipv4 (vty, 1, argv[0], NULL, argv[1], NULL);}/* Mask as A.B.C.D format. */DEFUN (ip_route_mask, ip_route_mask_cmd, "ip route A.B.C.D A.B.C.D (A.B.C.D|INTERFACE|null0)", IP_STR "Establish static routes\n" "IP destination prefix\n" "IP destination prefix mask\n" "IP gateway address\n" "IP gateway interface name\n" "Null interface\n"){ return zebra_static_ipv4 (vty, 1, argv[0], argv[1], argv[2], NULL);}/* Distance option value. */DEFUN (ip_route_distance, ip_route_distance_cmd, "ip route A.B.C.D/M (A.B.C.D|INTERFACE|null0) <1-255>", IP_STR "Establish static routes\n" "IP destination prefix (e.g. 10.0.0.0/8)\n" "IP gateway address\n" "IP gateway interface name\n" "Null interface\n" "Distance value for this route\n"){ return zebra_static_ipv4 (vty, 1, argv[0], NULL, argv[1], argv[2]);}DEFUN (ip_route_mask_distance, ip_route_mask_distance_cmd, "ip route A.B.C.D A.B.C.D (A.B.C.D|INTERFACE|null0) <1-255>", IP_STR "Establish static routes\n" "IP destination prefix\n" "IP destination prefix mask\n" "IP gateway address\n" "IP gateway interface name\n" "Null interface\n" "Distance value for this route\n"){ return zebra_static_ipv4 (vty, 1, argv[0], argv[1], argv[2], argv[3]);}DEFUN (no_ip_route, no_ip_route_cmd, "no ip route A.B.C.D/M (A.B.C.D|INTERFACE|null0)", NO_STR IP_STR "Establish static routes\n" "IP destination prefix (e.g. 10.0.0.0/8)\n" "IP gateway address\n" "IP gateway interface name\n" "Null interface\n"){ return zebra_static_ipv4 (vty, 0, argv[0], NULL, argv[1], NULL);}DEFUN (no_ip_route_mask, no_ip_route_mask_cmd, "no ip route A.B.C.D A.B.C.D (A.B.C.D|INTERFACE|null0)", NO_STR IP_STR "Establish static routes\n" "IP destination prefix\n" "IP destination prefix mask\n" "IP gateway address\n" "IP gateway interface name\n" "Null interface\n"){ return zebra_static_ipv4 (vty, 0, argv[0], argv[1], argv[2], NULL);}DEFUN (no_ip_route_distance, no_ip_route_distance_cmd, "no ip route A.B.C.D/M (A.B.C.D|INTERFACE|null0) <1-255>", NO_STR IP_STR "Establish static routes\n" "IP destination prefix (e.g. 10.0.0.0/8)\n" "IP gateway address\n" "IP gateway interface name\n" "Null interface\n" "Distance value for this route\n"){ return zebra_static_ipv4 (vty, 0, argv[0], NULL, argv[1], argv[2]);}DEFUN (no_ip_route_mask_distance, no_ip_route_mask_distance_cmd, "no ip route A.B.C.D A.B.C.D (A.B.C.D|INTERFACE|null0) <1-255>", NO_STR IP_STR "Establish static routes\n" "IP destination prefix\n" "IP destination prefix mask\n" "IP gateway address\n" "IP gateway interface name\n" "Null interface\n" "Distance value for this route\n"){ return zebra_static_ipv4 (vty, 0, argv[0], argv[1], argv[2], argv[3]);}/* New RIB. Detailed information for IPv4 route. */voidvty_show_ip_route_detail (struct vty *vty, struct route_node *rn){ struct rib *rib; struct nexthop *nexthop; for (rib = rn->info; rib; rib = rib->next) { vty_out (vty, "Routing entry for %s/%d%s", inet_ntoa (rn->p.u.prefix4), rn->p.prefixlen, VTY_NEWLINE); vty_out (vty, " Known via \"%s\"", route_type_str (rib->type)); vty_out (vty, ", distance %d, metric %d", rib->distance, rib->metric); if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED)) vty_out (vty, ", best"); if (rib->refcnt) vty_out (vty, ", refcnt %ld", rib->refcnt); vty_out (vty, "%s", VTY_NEWLINE);#define ONE_DAY_SECOND 60*60*24#define ONE_WEEK_SECOND 60*60*24*7 if (rib->type == ZEBRA_ROUTE_RIP || rib->type == ZEBRA_ROUTE_OSPF || rib->type == ZEBRA_ROUTE_BGP) { time_t uptime; struct tm *tm; uptime = time (NULL); uptime -= rib->uptime; tm = gmtime (&uptime); vty_out (vty, " Last update "); if (uptime < ONE_DAY_SECOND) vty_out (vty, "%02d:%02d:%02d", tm->tm_hour, tm->tm_min, tm->tm_sec); else if (uptime < ONE_WEEK_SECOND) vty_out (vty, "%dd%02dh%02dm", tm->tm_yday, tm->tm_hour, tm->tm_min); else vty_out (vty, "%02dw%dd%02dh", tm->tm_yday/7, tm->tm_yday - ((tm->tm_yday/7) * 7), tm->tm_hour); vty_out (vty, " ago%s", VTY_NEWLINE); } for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) { vty_out (vty, " %c", CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB) ? '*' : ' '); switch (nexthop->type) { case NEXTHOP_TYPE_IPV4: case NEXTHOP_TYPE_IPV4_IFINDEX: vty_out (vty, " %s", inet_ntoa (nexthop->gate.ipv4)); if (nexthop->ifindex) vty_out (vty, ", via %s", ifindex2ifname (nexthop->ifindex)); break; case NEXTHOP_TYPE_IFINDEX: vty_out (vty, " directly connected, %s", ifindex2ifname (nexthop->ifindex)); break; case NEXTHOP_TYPE_IFNAME: vty_out (vty, " directly connected, %s", nexthop->ifname); break; case NEXTHOP_TYPE_BLACKHOLE: vty_out (vty, " directly connected, via Null0"); break; default: break; } if (! CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE)) vty_out (vty, " inactive"); if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE)) { vty_out (vty, " (recursive"); switch (nexthop->rtype) { case NEXTHOP_TYPE_IPV4: case NEXTHOP_TYPE_IPV4_IFINDEX: vty_out (vty, " via %s)", inet_ntoa (nexthop->rgate.ipv4)); break; case NEXTHOP_TYPE_IFINDEX: case NEXTHOP_TYPE_IFNAME: vty_out (vty, " is directly connected, %s)", ifindex2ifname (nexthop->rifindex)); break; default: break; } } vty_out (vty, "%s", VTY_NEWLINE); } vty_out (vty, "%s", VTY_NEWLINE); }}voidvty_show_ip_route (struct vty *vty, struct route_node *rn, struct rib *rib){ struct nexthop *nexthop; int len = 0; char buf[BUFSIZ]; /* Nexthop information. */ for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) { if (nexthop == rib->nexthop) { /* Prefix information. */ len = vty_out (vty, "%c%c%c %s/%d", route_type_char (rib->type), CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED) ? '>' : ' ', CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB) ? '*' : ' ', inet_ntop (AF_INET, &rn->p.u.prefix, buf, BUFSIZ), rn->p.prefixlen); /* Distance and metric display. */ if (rib->type != ZEBRA_ROUTE_CONNECT && rib->type != ZEBRA_ROUTE_KERNEL) len += vty_out (vty, " [%d/%d]", rib->distance, rib->metric); } else vty_out (vty, " %c%*c", CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB) ? '*' : ' ', len - 3, ' '); switch (nexthop->type) { case NEXTHOP_TYPE_IPV4: case NEXTHOP_TYPE_IPV4_IFINDEX: vty_out (vty, " via %s", inet_ntoa (nexthop->gate.ipv4)); if (nexthop->ifindex) vty_out (vty, ", %s", ifindex2ifname (nexthop->ifindex)); break; case NEXTHOP_TYPE_IFINDEX: vty_out (vty, " is directly connected, %s", ifindex2ifname (nexthop->ifindex)); break; case NEXTHOP_TYPE_IFNAME: vty_out (vty, " is directly connected, %s", nexthop->ifname); break; case NEXTHOP_TYPE_BLACKHOLE: vty_out (vty, " is directly connected, Null0"); default: break; } if (! CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE)) vty_out (vty, " inactive"); if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE)) { vty_out (vty, " (recursive"); switch (nexthop->rtype) { case NEXTHOP_TYPE_IPV4: case NEXTHOP_TYPE_IPV4_IFINDEX: vty_out (vty, " via %s)", inet_ntoa (nexthop->rgate.ipv4)); break; case NEXTHOP_TYPE_IFINDEX: case NEXTHOP_TYPE_IFNAME: vty_out (vty, " is directly connected, %s)", ifindex2ifname (nexthop->rifindex)); break; default: break; } } if (rib->type == ZEBRA_ROUTE_RIP || rib->type == ZEBRA_ROUTE_OSPF || rib->type == ZEBRA_ROUTE_BGP) { time_t uptime; struct tm *tm; uptime = time (NULL); uptime -= rib->uptime; tm = gmtime (&uptime);#define ONE_DAY_SECOND 60*60*24#define ONE_WEEK_SECOND 60*60*24*7 if (uptime < ONE_DAY_SECOND) vty_out (vty, ", %02d:%02d:%02d", tm->tm_hour, tm->tm_min, tm->tm_sec); else if (uptime < ONE_WEEK_SECOND) vty_out (vty, ", %dd%02dh%02dm", tm->tm_yday, tm->tm_hour, tm->tm_min); else vty_out (vty, ", %02dw%dd%02dh", tm->tm_yday/7, tm->tm_yday - ((tm->tm_yday/7) * 7), tm->tm_hour); } vty_out (vty, "%s", VTY_NEWLINE); }}#define SHOW_ROUTE_V4_HEADER "Codes: K - kernel route, C - connected, S - static, R - RIP, O - OSPF,%s B - BGP, > - selected route, * - FIB route%s%s"DEFUN (show_ip_route, show_ip_route_cmd, "show ip route", SHOW_STR IP_STR "IP routing table\n"){ struct route_table *table; struct route_node *rn; struct rib *rib; int first = 1; table = vrf_table (AFI_IP, SAFI_UNICAST, 0); if (! table) return CMD_SUCCESS; /* Show all IPv4 routes. */ for (rn = route_top (table); rn; rn = route_next (rn)) for (rib = rn->info; rib; rib = rib->next) { if (first) { vty_out (vty, SHOW_ROUTE_V4_HEADER, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE); first = 0; } vty_show_ip_route (vty, rn, rib); } return CMD_SUCCESS;}DEFUN (show_ip_route_prefix_longer, show_ip_route_prefix_longer_cmd, "show ip route A.B.C.D/M longer-prefixes", SHOW_STR IP_STR
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -