]> git.puffer.fish Git - matthieu/frr.git/commitdiff
zebra: detect if a netns is the default netns
authorPhilippe Guibert <philippe.guibert@6wind.com>
Fri, 22 Jun 2018 11:55:55 +0000 (13:55 +0200)
committerPhilippe Guibert <philippe.guibert@6wind.com>
Tue, 28 Aug 2018 14:23:50 +0000 (16:23 +0200)
In the case the default netns has a netns path, then a new NETNS
creation will be bypassed.

Signed-off-by: Philippe Guibert <philippe.guibert@6wind.com>
zebra/zebra_netns_notify.c

index 2b7bf04ec3894e227399c347bcef9939f44adf67..12207805d95231fc0193fd2fcc9587e41fda6a99 100644 (file)
@@ -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);