From: Philippe Guibert Date: Fri, 22 Jun 2018 11:55:55 +0000 (+0200) Subject: zebra: detect if a netns is the default netns X-Git-Tag: frr-6.1-dev~10^2~9 X-Git-Url: https://git.puffer.fish/?a=commitdiff_plain;h=3ed78e8cff9d141ccd1f917566637d7bf3e4bb0d;p=matthieu%2Ffrr.git zebra: detect if a netns is the default netns In the case the default netns has a netns path, then a new NETNS creation will be bypassed. Signed-off-by: Philippe Guibert --- diff --git a/zebra/zebra_netns_notify.c b/zebra/zebra_netns_notify.c index 2b7bf04ec3..12207805d9 100644 --- a/zebra/zebra_netns_notify.c +++ b/zebra/zebra_netns_notify.c @@ -149,6 +149,41 @@ static int zebra_ns_delete(char *name) return 0; } +static int zebra_ns_notify_self_identify(struct stat *netst) +{ + char net_path[64]; + int netns; + + sprintf(net_path, "/proc/self/ns/net"); + netns = open(net_path, O_RDONLY); + if (netns < 0) + return -1; + if (fstat(netns, netst) < 0) { + close(netns); + return -1; + } + close(netns); + return 0; +} + +static bool zebra_ns_notify_is_default_netns(const char *name) +{ + struct stat default_netns_stat; + struct stat st; + char netnspath[64]; + + if (zebra_ns_notify_self_identify(&default_netns_stat)) + return false; + + memset(&st, 0, sizeof(struct stat)); + snprintf(netnspath, 64, "%s/%s", NS_RUN_DIR, name); + /* compare with local stat */ + if (stat(netnspath, &st) == 0 && + (st.st_dev == default_netns_stat.st_dev) && + (st.st_ino == default_netns_stat.st_ino)) + return true; + return false; +} static int zebra_ns_ready_read(struct thread *t) { @@ -178,6 +213,14 @@ static int zebra_ns_ready_read(struct thread *t) if (err < 0) return zebra_ns_continue_read(zns_info, stop_retry); + if (zebra_ns_notify_is_default_netns(basename(netnspath))) { + zlog_warn( + "NS notify : NS %s is default VRF." + " Updating VRF Name", basename(netnspath)); + vrf_set_default_name(basename(netnspath)); + return zebra_ns_continue_read(zns_info, 1); + } + /* success : close fd and create zns context */ zebra_ns_notify_create_context_from_entry_name(basename(netnspath)); return zebra_ns_continue_read(zns_info, 1); @@ -259,6 +302,13 @@ void zebra_ns_notify_parse(void) dent->d_name); continue; } + if (zebra_ns_notify_is_default_netns(dent->d_name)) { + zlog_warn( + "NS notify : NS %s is default VRF." + " Updating VRF Name", dent->d_name); + vrf_set_default_name(dent->d_name); + continue; + } zebra_ns_notify_create_context_from_entry_name(dent->d_name); } closedir(srcdir);