diff options
| author | Donatas Abraitis <donatas.abraitis@gmail.com> | 2020-09-23 08:45:09 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-09-23 08:45:09 +0300 |
| commit | 0ce5baaab1cefb0cd89cd526ab5225cd3a58a921 (patch) | |
| tree | b2107db6725811ab8e04468adc7317d22c631c0d | |
| parent | efdd997dad76b534826701bbc2145abcd1945f83 (diff) | |
| parent | 1eb92f06c4e39a47a82ae585cfb276497843de6c (diff) | |
Merge pull request #7018 from gouault6wind/show_ip_route
Clean up in vrf management
| -rw-r--r-- | lib/netns_linux.c | 14 | ||||
| -rw-r--r-- | lib/netns_other.c | 7 | ||||
| -rw-r--r-- | lib/ns.h | 5 | ||||
| -rw-r--r-- | lib/vrf.c | 19 | ||||
| -rw-r--r-- | lib/vrf.h | 6 | ||||
| -rw-r--r-- | zebra/zebra_netns_id.c | 9 | ||||
| -rw-r--r-- | zebra/zebra_ns.c | 2 | ||||
| -rw-r--r-- | zebra/zebra_vty.c | 151 |
8 files changed, 91 insertions, 122 deletions
diff --git a/lib/netns_linux.c b/lib/netns_linux.c index e8d549b4e0..c688433983 100644 --- a/lib/netns_linux.c +++ b/lib/netns_linux.c @@ -43,9 +43,6 @@ DEFINE_MTYPE_STATIC(LIB, NS, "NetNS Context") DEFINE_MTYPE_STATIC(LIB, NS_NAME, "NetNS Name") -/* default NS ID value used when VRF backend is not NETNS */ -#define NS_DEFAULT_INTERNAL 0 - static inline int ns_compare(const struct ns *ns, const struct ns *ns2); static struct ns *ns_lookup_name_internal(const char *name); @@ -101,9 +98,6 @@ static inline int setns(int fd, int nstype) static int have_netns_enabled = -1; #endif /* HAVE_NETNS */ -/* default NS ID value used when VRF backend is not NETNS */ -#define NS_DEFAULT_INTERNAL 0 - static int have_netns(void) { #ifdef HAVE_NETNS @@ -612,15 +606,7 @@ ns_id_t ns_id_get_absolute(ns_id_t ns_id_reference, ns_id_t link_nsid) return ns->ns_id; } -ns_id_t ns_get_default_id(void) -{ - if (default_ns) - return default_ns->ns_id; - return NS_DEFAULT_INTERNAL; -} - struct ns *ns_get_default(void) { return default_ns; } - diff --git a/lib/netns_other.c b/lib/netns_other.c index 9cf7f1f33a..3fc4b8df4b 100644 --- a/lib/netns_other.c +++ b/lib/netns_other.c @@ -110,13 +110,6 @@ void ns_init(void) { } -/* API to retrieve default NS */ -ns_id_t ns_get_default_id(void) -{ - return NS_UNKNOWN; -} - - /* API that can be used to change from NS */ int ns_switchback_to_initial(void) { @@ -161,10 +161,7 @@ extern ns_id_t ns_map_nsid_with_external(ns_id_t ns_id, bool map); */ extern void ns_init(void); -/* API to retrieve default NS */ -extern ns_id_t ns_get_default_id(void); - -#define NS_DEFAULT ns_get_default_id() +#define NS_DEFAULT 0 /* API that can be used to change from NS */ extern int ns_switchback_to_initial(void); @@ -39,8 +39,7 @@ #include "northbound.h" #include "northbound_cli.h" -/* default VRF ID value used when VRF backend is not NETNS */ -#define VRF_DEFAULT_INTERNAL 0 +/* default VRF name value used when VRF backend is not NETNS */ #define VRF_DEFAULT_NAME_INTERNAL "default" DEFINE_MTYPE_STATIC(LIB, VRF, "VRF") @@ -331,6 +330,9 @@ const char *vrf_id_to_name(vrf_id_t vrf_id) { struct vrf *vrf; + if (vrf_id == VRF_DEFAULT) + return VRF_DEFAULT_NAME; + vrf = vrf_lookup_by_id(vrf_id); return VRF_LOGNAME(vrf); } @@ -518,7 +520,7 @@ void vrf_init(int (*create)(struct vrf *), int (*enable)(struct vrf *), strlcpy(default_vrf->data.l.netns_name, VRF_DEFAULT_NAME, NS_NAMSIZ); - ns = ns_lookup(ns_get_default_id()); + ns = ns_lookup(NS_DEFAULT); ns->vrf_ctxt = default_vrf; default_vrf->ns_ctxt = ns; } @@ -946,17 +948,6 @@ const char *vrf_get_default_name(void) return vrf_default_name; } -vrf_id_t vrf_get_default_id(void) -{ - /* backend netns is only known by zebra - * for other daemons, we return VRF_DEFAULT_INTERNAL - */ - if (vrf_is_backend_netns()) - return ns_get_default_id(); - else - return VRF_DEFAULT_INTERNAL; -} - int vrf_bind(vrf_id_t vrf_id, int fd, const char *name) { int ret = 0; @@ -262,12 +262,8 @@ extern int vrf_getaddrinfo(const char *node, const char *service, extern int vrf_ioctl(vrf_id_t vrf_id, int d, unsigned long request, char *args); -/* function called by macro VRF_DEFAULT - * to get the default VRF_ID - */ -extern vrf_id_t vrf_get_default_id(void); /* The default VRF ID */ -#define VRF_DEFAULT vrf_get_default_id() +#define VRF_DEFAULT 0 extern void vrf_set_default_name(const char *default_name, bool force); extern const char *vrf_get_default_name(void); diff --git a/zebra/zebra_netns_id.c b/zebra/zebra_netns_id.c index 79121bb086..3201c438cd 100644 --- a/zebra/zebra_netns_id.c +++ b/zebra/zebra_netns_id.c @@ -39,9 +39,6 @@ #include "zebra/zebra_netns_id.h" #include "zebra/zebra_errors.h" -/* default NS ID value used when VRF backend is not NETNS */ -#define NS_DEFAULT_INTERNAL 0 - /* in case NEWNSID not available, the NSID will be locally obtained */ #define NS_BASE_NSID 0 @@ -362,14 +359,14 @@ ns_id_t zebra_ns_id_get_default(void) fd = open(NS_DEFAULT_NAME, O_RDONLY); if (fd == -1) - return NS_DEFAULT_INTERNAL; + return NS_DEFAULT; if (!vrf_is_backend_netns()) { close(fd); - return NS_DEFAULT_INTERNAL; + return NS_DEFAULT; } close(fd); return zebra_ns_id_get((char *)NS_DEFAULT_NAME, -1); #else /* HAVE_NETNS */ - return NS_DEFAULT_INTERNAL; + return NS_DEFAULT; #endif /* !HAVE_NETNS */ } diff --git a/zebra/zebra_ns.c b/zebra/zebra_ns.c index 6462daf687..e9ff3fcc08 100644 --- a/zebra/zebra_ns.c +++ b/zebra/zebra_ns.c @@ -199,7 +199,7 @@ int zebra_ns_init(const char *optional_default_name) if (ns) ns->relative_default_ns = ns_id; - default_ns = ns_lookup(ns_get_default_id()); + default_ns = ns_lookup(NS_DEFAULT); if (!default_ns) { flog_err(EC_ZEBRA_NS_NO_DEFAULT, "%s: failed to find default ns", __func__); diff --git a/zebra/zebra_vty.c b/zebra/zebra_vty.c index b6d0b26125..fc0df09df8 100644 --- a/zebra/zebra_vty.c +++ b/zebra/zebra_vty.c @@ -61,12 +61,19 @@ extern int allow_delete; +/* context to manage dumps in multiple tables or vrfs */ +struct route_show_ctx { + bool multi; /* dump multiple tables or vrf */ + bool header_done; /* common header already displayed */ +}; + static int do_show_ip_route(struct vty *vty, const char *vrf_name, afi_t afi, safi_t safi, bool use_fib, bool use_json, route_tag_t tag, const struct prefix *longer_prefix_p, bool supernets_only, int type, - unsigned short ospf_instance_id, uint32_t tableid); + unsigned short ospf_instance_id, uint32_t tableid, + struct route_show_ctx *ctx); static void vty_show_ip_route_detail(struct vty *vty, struct route_node *rn, int mcast, bool use_fib, bool show_ng); static void vty_show_ip_route_summary(struct vty *vty, @@ -140,8 +147,12 @@ DEFUN (show_ip_rpf, JSON_STR) { bool uj = use_json(argc, argv); + struct route_show_ctx ctx = { + .multi = false, + }; + return do_show_ip_route(vty, VRF_DEFAULT_NAME, AFI_IP, SAFI_MULTICAST, - false, uj, 0, NULL, false, 0, 0, 0); + false, uj, 0, NULL, false, 0, 0, 0, &ctx); } DEFUN (show_ip_rpf_addr, @@ -331,15 +342,8 @@ static void show_nexthop_detail_helper(struct vty *vty, } if ((re->vrf_id != nexthop->vrf_id) - && (nexthop->type != NEXTHOP_TYPE_BLACKHOLE)) { - struct vrf *vrf = - vrf_lookup_by_id(nexthop->vrf_id); - - if (vrf) - vty_out(vty, "(vrf %s)", vrf->name); - else - vty_out(vty, "(vrf UNKNOWN)"); - } + && (nexthop->type != NEXTHOP_TYPE_BLACKHOLE)) + vty_out(vty, "(vrf %s)", vrf_id_to_name(nexthop->vrf_id)); if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_DUPLICATE)) vty_out(vty, " (duplicate nexthop removed)"); @@ -537,15 +541,9 @@ static void show_route_nexthop_helper(struct vty *vty, break; } - if ((re == NULL || (nexthop->vrf_id != re->vrf_id)) && - (nexthop->type != NEXTHOP_TYPE_BLACKHOLE)) { - struct vrf *vrf = vrf_lookup_by_id(nexthop->vrf_id); - - if (vrf) - vty_out(vty, " (vrf %s)", vrf->name); - else - vty_out(vty, " (vrf UNKNOWN)"); - } + if ((re == NULL || (nexthop->vrf_id != re->vrf_id)) + && (nexthop->type != NEXTHOP_TYPE_BLACKHOLE)) + vty_out(vty, " (vrf %s)", vrf_id_to_name(nexthop->vrf_id)); if (!CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)) vty_out(vty, " inactive"); @@ -609,7 +607,6 @@ static void show_nexthop_json_helper(json_object *json_nexthop, const struct route_entry *re) { char buf[SRCDEST2STR_BUFFER]; - struct vrf *vrf = NULL; json_object *json_labels = NULL; json_object *json_backups = NULL; int i; @@ -703,11 +700,10 @@ static void show_nexthop_json_helper(json_object *json_nexthop, } if ((nexthop->vrf_id != re->vrf_id) - && (nexthop->type != NEXTHOP_TYPE_BLACKHOLE)) { - vrf = vrf_lookup_by_id(nexthop->vrf_id); + && (nexthop->type != NEXTHOP_TYPE_BLACKHOLE)) json_object_string_add(json_nexthop, "vrf", - vrf->name); - } + vrf_id_to_name(nexthop->vrf_id)); + if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_DUPLICATE)) json_object_boolean_true_add(json_nexthop, "duplicate"); @@ -802,7 +798,6 @@ static void vty_show_ip_route(struct vty *vty, struct route_node *rn, json_object *json_nexthop = NULL; json_object *json_route = NULL; time_t uptime; - const struct vrf *vrf = NULL; const rib_dest_t *dest = rib_dest_from_rnode(rn); const struct nexthop_group *nhg; char up_str[MONOTIME_STRLEN]; @@ -835,13 +830,10 @@ static void vty_show_ip_route(struct vty *vty, struct route_node *rn, json_object_int_add(json_route, "instance", re->instance); - if (re->vrf_id) { - json_object_int_add(json_route, "vrfId", re->vrf_id); - vrf = vrf_lookup_by_id(re->vrf_id); - json_object_string_add(json_route, "vrfName", - vrf->name); + json_object_int_add(json_route, "vrfId", re->vrf_id); + json_object_string_add(json_route, "vrfName", + vrf_id_to_name(re->vrf_id)); - } if (CHECK_FLAG(re->flags, ZEBRA_FLAG_SELECTED)) json_object_boolean_true_add(json_route, "selected"); @@ -1038,7 +1030,7 @@ static void do_show_route_helper(struct vty *vty, struct zebra_vrf *zvrf, const struct prefix *longer_prefix_p, bool supernets_only, int type, unsigned short ospf_instance_id, bool use_json, - uint32_t tableid) + uint32_t tableid, struct route_show_ctx *ctx) { struct route_node *rn; struct route_entry *re; @@ -1049,6 +1041,17 @@ static void do_show_route_helper(struct vty *vty, struct zebra_vrf *zvrf, uint32_t addr; char buf[BUFSIZ]; + /* + * ctx->multi indicates if we are dumping multiple tables or vrfs. + * if set: + * => display the common header at most once + * => add newline at each call except first + * => always display the VRF and table + * else: + * => display the common header if at least one entry is found + * => display the VRF and table if specific + */ + if (use_json) json = json_object_new_object(); @@ -1092,23 +1095,30 @@ static void do_show_route_helper(struct vty *vty, struct zebra_vrf *zvrf, if (use_json) { if (!json_prefix) json_prefix = json_object_new_array(); - } else { - if (first) { + } else if (first) { + if (!ctx->header_done) { if (afi == AFI_IP) vty_out(vty, SHOW_ROUTE_V4_HEADER); else vty_out(vty, SHOW_ROUTE_V6_HEADER); - - if (tableid && tableid != RT_TABLE_MAIN) - vty_out(vty, "\nVRF %s table %u:\n", - zvrf_name(zvrf), tableid); - else if (zvrf_id(zvrf) != VRF_DEFAULT) - vty_out(vty, "\nVRF %s:\n", + } + if (ctx->multi && ctx->header_done) + vty_out(vty, "\n"); + if (ctx->multi || zvrf_id(zvrf) != VRF_DEFAULT + || tableid) { + if (!tableid) + vty_out(vty, "VRF %s:\n", zvrf_name(zvrf)); - first = 0; + else + vty_out(vty, + "VRF %s table %u:\n", + zvrf_name(zvrf), + tableid); } + ctx->header_done = true; + first = 0; } vty_show_ip_route(vty, rn, re, json_prefix, use_fib); @@ -1133,7 +1143,8 @@ static void do_show_ip_route_all(struct vty *vty, struct zebra_vrf *zvrf, route_tag_t tag, const struct prefix *longer_prefix_p, bool supernets_only, int type, - unsigned short ospf_instance_id) + unsigned short ospf_instance_id, + struct route_show_ctx *ctx) { struct zebra_router_table *zrt; struct rib_table_info *info; @@ -1148,11 +1159,10 @@ static void do_show_ip_route_all(struct vty *vty, struct zebra_vrf *zvrf, zrt->safi != SAFI_UNICAST) continue; - do_show_ip_route(vty, zvrf_name(zvrf), afi, - SAFI_UNICAST, use_fib, use_json, - tag, longer_prefix_p, - supernets_only, type, - ospf_instance_id, zrt->tableid); + do_show_ip_route(vty, zvrf_name(zvrf), afi, SAFI_UNICAST, + use_fib, use_json, tag, longer_prefix_p, + supernets_only, type, ospf_instance_id, + zrt->tableid, ctx); } } @@ -1161,7 +1171,8 @@ static int do_show_ip_route(struct vty *vty, const char *vrf_name, afi_t afi, route_tag_t tag, const struct prefix *longer_prefix_p, bool supernets_only, int type, - unsigned short ospf_instance_id, uint32_t tableid) + unsigned short ospf_instance_id, uint32_t tableid, + struct route_show_ctx *ctx) { struct route_table *table; struct zebra_vrf *zvrf = NULL; @@ -1194,7 +1205,7 @@ static int do_show_ip_route(struct vty *vty, const char *vrf_name, afi_t afi, do_show_route_helper(vty, zvrf, table, afi, use_fib, tag, longer_prefix_p, supernets_only, type, - ospf_instance_id, use_json, tableid); + ospf_instance_id, use_json, tableid, ctx); return CMD_SUCCESS; } @@ -1272,18 +1283,11 @@ static void show_nexthop_group_out(struct vty *vty, struct nhg_hash_entry *nhe) { struct nexthop *nexthop = NULL; struct nhg_connected *rb_node_dep = NULL; - struct vrf *nhe_vrf = vrf_lookup_by_id(nhe->vrf_id); struct nexthop_group *backup_nhg; vty_out(vty, "ID: %u\n", nhe->id); vty_out(vty, " RefCnt: %d\n", nhe->refcnt); - - if (nhe_vrf) - vty_out(vty, " VRF: %s AFI: %s\n", nhe_vrf->name, - afi2str(nhe->afi)); - else - vty_out(vty, " VRF: UNKNOWN AFI: %s\n", - afi2str(nhe->afi)); + vty_out(vty, " VRF: %s\n", vrf_id_to_name(nhe->vrf_id)); if (CHECK_FLAG(nhe->flags, NEXTHOP_GROUP_UNHASHABLE)) vty_out(vty, " Duplicate - from kernel not hashable\n"); @@ -1671,6 +1675,9 @@ DEFPY (show_route, struct vrf *vrf; int type = 0; struct zebra_vrf *zvrf; + struct route_show_ctx ctx = { + .multi = vrf_all || table_all, + }; if (!vrf_is_backend_netns()) { if ((vrf_all || vrf_name) && (table || table_all)) { @@ -1698,17 +1705,18 @@ DEFPY (show_route, continue; if (table_all) - do_show_ip_route_all(vty, zvrf, afi, - !!fib, !!json, - tag, prefix_str ? prefix : NULL, + do_show_ip_route_all(vty, zvrf, afi, !!fib, + !!json, tag, + prefix_str ? prefix : NULL, !!supernets_only, type, - ospf_instance_id); + ospf_instance_id, &ctx); else do_show_ip_route(vty, zvrf_name(zvrf), afi, - SAFI_UNICAST, !!fib, !!json, tag, + SAFI_UNICAST, !!fib, !!json, + tag, prefix_str ? prefix : NULL, !!supernets_only, type, - ospf_instance_id, table); + ospf_instance_id, table, &ctx); } } else { vrf_id_t vrf_id = VRF_DEFAULT; @@ -1722,15 +1730,16 @@ DEFPY (show_route, return CMD_SUCCESS; if (table_all) - do_show_ip_route_all(vty, zvrf, afi, - !!fib, !!json, - tag, prefix_str ? prefix : NULL, + do_show_ip_route_all(vty, zvrf, afi, !!fib, !!json, tag, + prefix_str ? prefix : NULL, !!supernets_only, type, - ospf_instance_id); + ospf_instance_id, &ctx); else - do_show_ip_route(vty, vrf->name, afi, SAFI_UNICAST, !!fib, - !!json, tag, prefix_str ? prefix : NULL, - !!supernets_only, type, ospf_instance_id, table); + do_show_ip_route(vty, vrf->name, afi, SAFI_UNICAST, + !!fib, !!json, tag, + prefix_str ? prefix : NULL, + !!supernets_only, type, + ospf_instance_id, table, &ctx); } return CMD_SUCCESS; |
