2023-02-08 13:17:09 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2017-12-13 11:04:31 +01:00
|
|
|
/*
|
|
|
|
* Zebra NS collector and notifier for Network NameSpaces
|
|
|
|
* Copyright (C) 2017 6WIND
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <zebra.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_NETLINK
|
|
|
|
#ifdef HAVE_NETNS
|
|
|
|
#undef _GNU_SOURCE
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
|
|
|
|
#include <sched.h>
|
|
|
|
#endif
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <sys/inotify.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include "thread.h"
|
|
|
|
#include "ns.h"
|
|
|
|
#include "command.h"
|
|
|
|
#include "memory.h"
|
2018-06-14 16:38:40 +02:00
|
|
|
#include "lib_errors.h"
|
2017-12-13 11:04:31 +01:00
|
|
|
|
2019-01-11 19:31:46 +01:00
|
|
|
#include "zebra_router.h"
|
2017-12-13 11:04:31 +01:00
|
|
|
#endif /* defined(HAVE_NETLINK) */
|
|
|
|
|
|
|
|
#include "zebra_netns_notify.h"
|
|
|
|
#include "zebra_netns_id.h"
|
2018-08-16 22:10:32 +02:00
|
|
|
#include "zebra_errors.h"
|
2021-11-06 01:36:48 +01:00
|
|
|
#include "interface.h"
|
2017-12-13 11:04:31 +01:00
|
|
|
|
|
|
|
#ifdef HAVE_NETLINK
|
|
|
|
|
|
|
|
/* upon creation of folder under /var/run/netns,
|
|
|
|
* wait that netns context is bound to
|
|
|
|
* that folder 10 seconds
|
|
|
|
*/
|
|
|
|
#define ZEBRA_NS_POLLING_INTERVAL_MSEC 1000
|
|
|
|
#define ZEBRA_NS_POLLING_MAX_RETRIES 200
|
|
|
|
|
|
|
|
DEFINE_MTYPE_STATIC(ZEBRA, NETNS_MISC, "ZebraNetNSInfo");
|
|
|
|
static struct thread *zebra_netns_notify_current;
|
|
|
|
|
|
|
|
struct zebra_netns_info {
|
|
|
|
const char *netnspath;
|
|
|
|
unsigned int retries;
|
|
|
|
};
|
|
|
|
|
2022-02-23 01:04:25 +01:00
|
|
|
static void zebra_ns_ready_read(struct thread *t);
|
2017-12-13 11:04:31 +01:00
|
|
|
static void zebra_ns_notify_create_context_from_entry_name(const char *name);
|
|
|
|
static int zebra_ns_continue_read(struct zebra_netns_info *zns_info,
|
|
|
|
int stop_retry);
|
2022-02-23 01:04:25 +01:00
|
|
|
static void zebra_ns_notify_read(struct thread *t);
|
2017-12-13 11:04:31 +01:00
|
|
|
|
2021-10-18 22:34:02 +02:00
|
|
|
static struct vrf *vrf_handler_create(struct vty *vty, const char *vrfname)
|
|
|
|
{
|
|
|
|
if (strlen(vrfname) > VRF_NAMSIZ) {
|
|
|
|
flog_warn(EC_LIB_VRF_LENGTH,
|
|
|
|
"%% VRF name %s invalid: length exceeds %d bytes",
|
|
|
|
vrfname, VRF_NAMSIZ);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return vrf_get(VRF_UNKNOWN, vrfname);
|
|
|
|
}
|
|
|
|
|
2017-12-13 11:04:31 +01:00
|
|
|
static void zebra_ns_notify_create_context_from_entry_name(const char *name)
|
|
|
|
{
|
|
|
|
char *netnspath = ns_netns_pathname(NULL, name);
|
|
|
|
struct vrf *vrf;
|
|
|
|
int ret;
|
zebra, lib: add an internal API to get relative default nsid in other ns
as remind, the netns identifiers are local to a namespace. that is to
say that for instance, a vrf <vrfx> will have a netns id value in one
netns, and have an other netns id value in one other netns.
There is a need for zebra daemon to collect some cross information, like
the LINK_NETNSID information from interfaces having link layer in an
other network namespace. For that, it is needed to have a global
overview instead of a relative overview per namespace.
The first brick of this change is an API that sticks to netlink API,
that uses NETNSA_TARGET_NSID. from a given vrf vrfX, and a new vrf
created vrfY, the API returns the value of nsID from vrfX, inside the
new vrf vrfY.
The brick also gets the ns id value of default namespace in each other
namespace. An additional value in ns.h is offered, that permits to
retrieve the default namespace context.
Signed-off-by: Philippe Guibert <philippe.guibert@6wind.com>
2019-10-02 12:14:13 +02:00
|
|
|
ns_id_t ns_id, ns_id_external, ns_id_relative = NS_UNKNOWN;
|
|
|
|
struct ns *default_ns;
|
2017-12-13 11:04:31 +01:00
|
|
|
|
|
|
|
if (netnspath == NULL)
|
|
|
|
return;
|
|
|
|
|
2019-08-13 15:47:23 +02:00
|
|
|
frr_with_privs(&zserv_privs) {
|
2020-01-06 17:39:17 +01:00
|
|
|
ns_id = zebra_ns_id_get(netnspath, -1);
|
2018-08-10 18:36:43 +02:00
|
|
|
}
|
2018-07-18 17:58:45 +02:00
|
|
|
if (ns_id == NS_UNKNOWN)
|
|
|
|
return;
|
2018-03-26 12:22:18 +02:00
|
|
|
ns_id_external = ns_map_nsid_with_external(ns_id, true);
|
2018-02-16 18:22:34 +01:00
|
|
|
/* if VRF with NS ID already present */
|
2018-03-26 12:22:18 +02:00
|
|
|
vrf = vrf_lookup_by_id((vrf_id_t)ns_id_external);
|
2018-02-16 18:22:34 +01:00
|
|
|
if (vrf) {
|
2018-08-16 22:10:32 +02:00
|
|
|
zlog_debug(
|
2018-02-16 18:22:34 +01:00
|
|
|
"NS notify : same NSID used by VRF %s. Ignore NS %s creation",
|
|
|
|
vrf->name, netnspath);
|
|
|
|
return;
|
|
|
|
}
|
2021-10-18 22:34:02 +02:00
|
|
|
vrf = vrf_handler_create(NULL, name);
|
|
|
|
if (!vrf) {
|
2018-09-13 21:21:05 +02:00
|
|
|
flog_warn(EC_ZEBRA_NS_VRF_CREATION_FAILED,
|
2018-08-16 22:10:32 +02:00
|
|
|
"NS notify : failed to create VRF %s", name);
|
2018-03-26 12:22:18 +02:00
|
|
|
ns_map_nsid_with_external(ns_id, false);
|
2018-02-16 18:22:34 +01:00
|
|
|
return;
|
|
|
|
}
|
2020-01-06 17:39:17 +01:00
|
|
|
|
|
|
|
default_ns = ns_get_default();
|
|
|
|
|
|
|
|
/* force kernel ns_id creation in that new vrf */
|
|
|
|
frr_with_privs(&zserv_privs) {
|
|
|
|
ns_switch_to_netns(netnspath);
|
|
|
|
ns_id_relative = zebra_ns_id_get(NULL, default_ns->fd);
|
|
|
|
ns_switchback_to_initial();
|
|
|
|
}
|
|
|
|
|
2019-08-13 15:47:23 +02:00
|
|
|
frr_with_privs(&zserv_privs) {
|
2021-10-18 22:34:02 +02:00
|
|
|
ret = zebra_vrf_netns_handler_create(NULL, vrf, netnspath,
|
|
|
|
ns_id_external, ns_id,
|
|
|
|
ns_id_relative);
|
2018-08-10 18:36:43 +02:00
|
|
|
}
|
2017-12-13 11:04:31 +01:00
|
|
|
if (ret != CMD_SUCCESS) {
|
2018-09-13 21:21:05 +02:00
|
|
|
flog_warn(EC_ZEBRA_NS_VRF_CREATION_FAILED,
|
2018-08-16 22:10:32 +02:00
|
|
|
"NS notify : failed to create NS %s", netnspath);
|
2018-03-26 12:22:18 +02:00
|
|
|
ns_map_nsid_with_external(ns_id, false);
|
2018-07-19 17:51:41 +02:00
|
|
|
vrf_delete(vrf);
|
2017-12-13 11:04:31 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
zlog_info("NS notify : created VRF %s NS %s", name, netnspath);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int zebra_ns_continue_read(struct zebra_netns_info *zns_info,
|
|
|
|
int stop_retry)
|
|
|
|
{
|
|
|
|
void *ns_path_ptr = (void *)zns_info->netnspath;
|
|
|
|
|
|
|
|
if (stop_retry) {
|
|
|
|
XFREE(MTYPE_NETNS_MISC, ns_path_ptr);
|
|
|
|
XFREE(MTYPE_NETNS_MISC, zns_info);
|
|
|
|
return 0;
|
|
|
|
}
|
2019-01-11 19:31:46 +01:00
|
|
|
thread_add_timer_msec(zrouter.master, zebra_ns_ready_read,
|
2017-12-13 11:04:31 +01:00
|
|
|
(void *)zns_info, ZEBRA_NS_POLLING_INTERVAL_MSEC,
|
|
|
|
NULL);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-03-22 18:02:00 +01:00
|
|
|
static int zebra_ns_delete(char *name)
|
|
|
|
{
|
|
|
|
struct vrf *vrf = vrf_lookup_by_name(name);
|
2021-11-06 01:36:48 +01:00
|
|
|
struct interface *ifp, *tmp;
|
2018-03-22 18:02:00 +01:00
|
|
|
struct ns *ns;
|
|
|
|
|
|
|
|
if (!vrf) {
|
2018-09-13 21:21:05 +02:00
|
|
|
flog_warn(EC_ZEBRA_NS_DELETION_FAILED_NO_VRF,
|
2018-08-16 22:10:32 +02:00
|
|
|
"NS notify : no VRF found using NS %s", name);
|
2018-03-22 18:02:00 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2021-11-06 01:36:48 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We don't receive interface down/delete notifications from kernel
|
|
|
|
* when a netns is deleted. Therefore we have to manually replicate
|
|
|
|
* the necessary actions here.
|
|
|
|
*/
|
|
|
|
RB_FOREACH_SAFE (ifp, if_name_head, &vrf->ifaces_by_name, tmp) {
|
|
|
|
if (!CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (if_is_no_ptm_operative(ifp)) {
|
|
|
|
UNSET_FLAG(ifp->flags, IFF_RUNNING);
|
|
|
|
if_down(ifp);
|
|
|
|
}
|
|
|
|
|
|
|
|
UNSET_FLAG(ifp->flags, IFF_UP);
|
2022-03-25 01:02:33 +01:00
|
|
|
if_delete_update(&ifp);
|
2021-11-06 01:36:48 +01:00
|
|
|
}
|
|
|
|
|
2018-03-22 18:02:00 +01:00
|
|
|
ns = (struct ns *)vrf->ns_ctxt;
|
|
|
|
/* the deletion order is the same
|
|
|
|
* as the one used when siging signal is received
|
|
|
|
*/
|
2021-11-02 21:54:43 +01:00
|
|
|
vrf->ns_ctxt = NULL;
|
2018-03-22 18:02:00 +01:00
|
|
|
vrf_delete(vrf);
|
|
|
|
if (ns)
|
|
|
|
ns_delete(ns);
|
|
|
|
|
|
|
|
zlog_info("NS notify : deleted VRF %s", name);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-06-22 13:55:55 +02:00
|
|
|
static int zebra_ns_notify_self_identify(struct stat *netst)
|
|
|
|
{
|
2020-04-20 20:28:39 +02:00
|
|
|
char net_path[PATH_MAX];
|
2018-06-22 13:55:55 +02:00
|
|
|
int netns;
|
|
|
|
|
2020-04-20 20:12:38 +02:00
|
|
|
snprintf(net_path, sizeof(net_path), "/proc/self/ns/net");
|
2018-06-22 13:55:55 +02:00
|
|
|
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;
|
2020-07-18 15:45:08 +02:00
|
|
|
char netnspath[PATH_MAX];
|
2018-06-22 13:55:55 +02:00
|
|
|
|
|
|
|
if (zebra_ns_notify_self_identify(&default_netns_stat))
|
|
|
|
return false;
|
|
|
|
|
2022-05-11 12:16:44 +02:00
|
|
|
memset(&st, 0, sizeof(st));
|
2020-04-20 20:12:38 +02:00
|
|
|
snprintf(netnspath, sizeof(netnspath), "%s/%s", NS_RUN_DIR, name);
|
2018-06-22 13:55:55 +02:00
|
|
|
/* 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;
|
|
|
|
}
|
2018-03-22 18:02:00 +01:00
|
|
|
|
2022-02-23 01:04:25 +01:00
|
|
|
static void zebra_ns_ready_read(struct thread *t)
|
2017-12-13 11:04:31 +01:00
|
|
|
{
|
|
|
|
struct zebra_netns_info *zns_info = THREAD_ARG(t);
|
|
|
|
const char *netnspath;
|
|
|
|
int err, stop_retry = 0;
|
|
|
|
|
|
|
|
if (!zns_info)
|
2022-02-23 01:04:25 +01:00
|
|
|
return;
|
2017-12-13 11:04:31 +01:00
|
|
|
if (!zns_info->netnspath) {
|
|
|
|
XFREE(MTYPE_NETNS_MISC, zns_info);
|
2022-02-23 01:04:25 +01:00
|
|
|
return;
|
2017-12-13 11:04:31 +01:00
|
|
|
}
|
|
|
|
netnspath = zns_info->netnspath;
|
|
|
|
if (--zns_info->retries == 0)
|
|
|
|
stop_retry = 1;
|
2019-08-13 15:47:23 +02:00
|
|
|
frr_with_privs(&zserv_privs) {
|
2018-08-10 18:36:43 +02:00
|
|
|
err = ns_switch_to_netns(netnspath);
|
|
|
|
}
|
2022-02-23 01:04:25 +01:00
|
|
|
if (err < 0) {
|
|
|
|
zebra_ns_continue_read(zns_info, stop_retry);
|
|
|
|
return;
|
|
|
|
}
|
2017-12-13 11:04:31 +01:00
|
|
|
|
|
|
|
/* go back to default ns */
|
2019-08-13 15:47:23 +02:00
|
|
|
frr_with_privs(&zserv_privs) {
|
2018-08-10 18:36:43 +02:00
|
|
|
err = ns_switchback_to_initial();
|
|
|
|
}
|
2022-02-23 01:04:25 +01:00
|
|
|
if (err < 0) {
|
|
|
|
zebra_ns_continue_read(zns_info, stop_retry);
|
|
|
|
return;
|
|
|
|
}
|
2017-12-13 11:04:31 +01:00
|
|
|
|
2018-12-28 14:27:45 +01:00
|
|
|
/* check default name is not already set */
|
|
|
|
if (strmatch(VRF_DEFAULT_NAME, basename(netnspath))) {
|
2020-03-27 12:35:23 +01:00
|
|
|
zlog_warn("NS notify : NS %s is already default VRF.Cancel VRF Creation", basename(netnspath));
|
2022-02-23 01:04:25 +01:00
|
|
|
zebra_ns_continue_read(zns_info, 1);
|
|
|
|
return;
|
2018-12-28 14:27:45 +01:00
|
|
|
}
|
2018-06-22 13:55:55 +02:00
|
|
|
if (zebra_ns_notify_is_default_netns(basename(netnspath))) {
|
|
|
|
zlog_warn(
|
*: rework renaming the default VRF
Currently, it is possible to rename the default VRF either by passing
`-o` option to zebra or by creating a file in `/var/run/netns` and
binding it to `/proc/self/ns/net`.
In both cases, only zebra knows about the rename and other daemons learn
about it only after they connect to zebra. This is a problem, because
daemons may read their config before they connect to zebra. To handle
this rename after the config is read, we have some special code in every
single daemon, which is not very bad but not desirable in my opinion.
But things are getting worse when we need to handle this in northbound
layer as we have to manually rewrite the config nodes. This approach is
already hacky, but still works as every daemon handles its own NB
structures. But it is completely incompatible with the central
management daemon architecture we are aiming for, as mgmtd doesn't even
have a connection with zebra to learn from it. And it shouldn't have it,
because operational state changes should never affect configuration.
To solve the problem and simplify the code, I propose to expand the `-o`
option to all daemons. By using the startup option, we let daemons know
about the rename before they read their configs so we don't need any
special code to deal with it. There's an easy way to pass the option to
all daemons by using `frr_global_options` variable.
Unfortunately, the second way of renaming by creating a file in
`/var/run/netns` is incompatible with the new mgmtd architecture.
Theoretically, we could force daemons to read their configs only after
they connect to zebra, but it means adding even more code to handle a
very specific use-case. And anyway this won't work for mgmtd as it
doesn't have a connection with zebra. So I had to remove this option.
Signed-off-by: Igor Ryzhov <iryzhov@nfware.com>
2021-12-03 23:22:55 +01:00
|
|
|
"NS notify : NS %s is default VRF. Ignore VRF creation",
|
|
|
|
basename(netnspath));
|
2022-02-23 01:04:25 +01:00
|
|
|
zebra_ns_continue_read(zns_info, 1);
|
|
|
|
return;
|
2018-06-22 13:55:55 +02:00
|
|
|
}
|
|
|
|
|
2017-12-13 11:04:31 +01:00
|
|
|
/* success : close fd and create zns context */
|
|
|
|
zebra_ns_notify_create_context_from_entry_name(basename(netnspath));
|
2022-02-23 01:04:25 +01:00
|
|
|
zebra_ns_continue_read(zns_info, 1);
|
2017-12-13 11:04:31 +01:00
|
|
|
}
|
|
|
|
|
2022-02-23 01:04:25 +01:00
|
|
|
static void zebra_ns_notify_read(struct thread *t)
|
2017-12-13 11:04:31 +01:00
|
|
|
{
|
|
|
|
int fd_monitor = THREAD_FD(t);
|
|
|
|
struct inotify_event *event;
|
|
|
|
char buf[BUFSIZ];
|
|
|
|
ssize_t len;
|
2023-01-17 20:32:08 +01:00
|
|
|
char event_name[NAME_MAX + 1];
|
2017-12-13 11:04:31 +01:00
|
|
|
|
2021-10-06 21:06:23 +02:00
|
|
|
thread_add_read(zrouter.master, zebra_ns_notify_read, NULL, fd_monitor,
|
|
|
|
&zebra_netns_notify_current);
|
2017-12-13 11:04:31 +01:00
|
|
|
len = read(fd_monitor, buf, sizeof(buf));
|
|
|
|
if (len < 0) {
|
2018-09-13 21:21:05 +02:00
|
|
|
flog_err_sys(EC_ZEBRA_NS_NOTIFY_READ,
|
2018-08-16 22:10:32 +02:00
|
|
|
"NS notify read: failed to read (%s)",
|
|
|
|
safe_strerror(errno));
|
2022-02-23 01:04:25 +01:00
|
|
|
return;
|
2017-12-13 11:04:31 +01:00
|
|
|
}
|
|
|
|
for (event = (struct inotify_event *)buf; (char *)event < &buf[len];
|
|
|
|
event = (struct inotify_event *)((char *)event + sizeof(*event)
|
|
|
|
+ event->len)) {
|
|
|
|
char *netnspath;
|
|
|
|
struct zebra_netns_info *netnsinfo;
|
|
|
|
|
2018-03-22 18:02:00 +01:00
|
|
|
if (!(event->mask & (IN_CREATE | IN_DELETE)))
|
2017-12-13 11:04:31 +01:00
|
|
|
continue;
|
2018-06-22 17:14:57 +02:00
|
|
|
|
|
|
|
if (offsetof(struct inotify_event, name) + event->len
|
|
|
|
>= sizeof(buf)) {
|
2018-09-13 21:21:05 +02:00
|
|
|
flog_err(EC_ZEBRA_NS_NOTIFY_READ,
|
2018-08-16 22:10:32 +02:00
|
|
|
"NS notify read: buffer underflow");
|
2018-06-15 16:16:11 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-06-27 12:43:43 +02:00
|
|
|
|
|
|
|
if (strnlen(event->name, event->len) == event->len) {
|
2018-09-13 21:21:05 +02:00
|
|
|
flog_err(EC_ZEBRA_NS_NOTIFY_READ,
|
2018-08-16 22:10:32 +02:00
|
|
|
"NS notify error: bad event name");
|
2018-06-27 12:43:43 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-01-17 20:32:08 +01:00
|
|
|
/*
|
|
|
|
* Coverity Scan extra steps to satisfy `STRING_NULL` warning:
|
|
|
|
* - Make sure event name is present by checking `len != 0`
|
|
|
|
* - Event name length must be at most `NAME_MAX + 1`
|
|
|
|
* (null byte inclusive)
|
|
|
|
* - Copy event name to a stack buffer to make sure it
|
|
|
|
* includes the null byte. `event->name` includes at least
|
|
|
|
* one null byte and `event->len` accounts the null bytes,
|
|
|
|
* so the operation after `memcpy` will look like a
|
|
|
|
* truncation to satisfy Coverity Scan null byte ending.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* if `event->name` is `abc\0` and `event->len` is 4,
|
|
|
|
* `memcpy` will copy the 4 bytes and then we set the
|
|
|
|
* null byte again at the position 4.
|
|
|
|
*
|
|
|
|
* For more information please read inotify(7) man page.
|
|
|
|
*/
|
|
|
|
if (event->len == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (event->len > sizeof(event_name)) {
|
|
|
|
flog_err(EC_ZEBRA_NS_NOTIFY_READ,
|
|
|
|
"NS notify error: unexpected big event name");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(event_name, event->name, event->len);
|
|
|
|
event_name[event->len - 1] = 0;
|
|
|
|
|
2018-12-18 18:00:31 +01:00
|
|
|
if (event->mask & IN_DELETE) {
|
2023-01-17 20:32:08 +01:00
|
|
|
zebra_ns_delete(event_name);
|
2018-12-18 18:00:31 +01:00
|
|
|
continue;
|
|
|
|
}
|
2023-01-17 20:32:08 +01:00
|
|
|
netnspath = ns_netns_pathname(NULL, event_name);
|
2017-12-13 11:04:31 +01:00
|
|
|
if (!netnspath)
|
|
|
|
continue;
|
|
|
|
netnspath = XSTRDUP(MTYPE_NETNS_MISC, netnspath);
|
|
|
|
netnsinfo = XCALLOC(MTYPE_NETNS_MISC,
|
|
|
|
sizeof(struct zebra_netns_info));
|
|
|
|
netnsinfo->retries = ZEBRA_NS_POLLING_MAX_RETRIES;
|
|
|
|
netnsinfo->netnspath = netnspath;
|
2019-01-11 19:31:46 +01:00
|
|
|
thread_add_timer_msec(zrouter.master, zebra_ns_ready_read,
|
2017-12-13 11:04:31 +01:00
|
|
|
(void *)netnsinfo, 0, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void zebra_ns_notify_parse(void)
|
|
|
|
{
|
|
|
|
struct dirent *dent;
|
|
|
|
DIR *srcdir = opendir(NS_RUN_DIR);
|
|
|
|
|
|
|
|
if (srcdir == NULL) {
|
2018-09-13 21:34:28 +02:00
|
|
|
flog_err_sys(EC_LIB_SYSTEM_CALL,
|
2018-08-16 22:10:32 +02:00
|
|
|
"NS parsing init: failed to parse %s", NS_RUN_DIR);
|
2017-12-13 11:04:31 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
while ((dent = readdir(srcdir)) != NULL) {
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (strcmp(dent->d_name, ".") == 0
|
|
|
|
|| strcmp(dent->d_name, "..") == 0)
|
|
|
|
continue;
|
|
|
|
if (fstatat(dirfd(srcdir), dent->d_name, &st, 0) < 0) {
|
2018-08-16 22:10:32 +02:00
|
|
|
flog_err_sys(
|
2018-09-13 21:34:28 +02:00
|
|
|
EC_LIB_SYSTEM_CALL,
|
2018-08-16 22:10:32 +02:00
|
|
|
"NS parsing init: failed to parse entry %s",
|
|
|
|
dent->d_name);
|
2017-12-13 11:04:31 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (S_ISDIR(st.st_mode)) {
|
2018-08-16 22:10:32 +02:00
|
|
|
zlog_debug("NS parsing init: %s is not a NS",
|
|
|
|
dent->d_name);
|
2017-12-13 11:04:31 +01:00
|
|
|
continue;
|
|
|
|
}
|
2018-12-28 14:27:45 +01:00
|
|
|
/* check default name is not already set */
|
|
|
|
if (strmatch(VRF_DEFAULT_NAME, basename(dent->d_name))) {
|
2020-03-27 12:35:23 +01:00
|
|
|
zlog_warn("NS notify : NS %s is already default VRF.Cancel VRF Creation", dent->d_name);
|
2018-12-28 14:27:45 +01:00
|
|
|
continue;
|
|
|
|
}
|
2018-06-22 13:55:55 +02:00
|
|
|
if (zebra_ns_notify_is_default_netns(dent->d_name)) {
|
|
|
|
zlog_warn(
|
*: rework renaming the default VRF
Currently, it is possible to rename the default VRF either by passing
`-o` option to zebra or by creating a file in `/var/run/netns` and
binding it to `/proc/self/ns/net`.
In both cases, only zebra knows about the rename and other daemons learn
about it only after they connect to zebra. This is a problem, because
daemons may read their config before they connect to zebra. To handle
this rename after the config is read, we have some special code in every
single daemon, which is not very bad but not desirable in my opinion.
But things are getting worse when we need to handle this in northbound
layer as we have to manually rewrite the config nodes. This approach is
already hacky, but still works as every daemon handles its own NB
structures. But it is completely incompatible with the central
management daemon architecture we are aiming for, as mgmtd doesn't even
have a connection with zebra to learn from it. And it shouldn't have it,
because operational state changes should never affect configuration.
To solve the problem and simplify the code, I propose to expand the `-o`
option to all daemons. By using the startup option, we let daemons know
about the rename before they read their configs so we don't need any
special code to deal with it. There's an easy way to pass the option to
all daemons by using `frr_global_options` variable.
Unfortunately, the second way of renaming by creating a file in
`/var/run/netns` is incompatible with the new mgmtd architecture.
Theoretically, we could force daemons to read their configs only after
they connect to zebra, but it means adding even more code to handle a
very specific use-case. And anyway this won't work for mgmtd as it
doesn't have a connection with zebra. So I had to remove this option.
Signed-off-by: Igor Ryzhov <iryzhov@nfware.com>
2021-12-03 23:22:55 +01:00
|
|
|
"NS notify : NS %s is default VRF. Ignore VRF creation",
|
|
|
|
dent->d_name);
|
2018-06-22 13:55:55 +02:00
|
|
|
continue;
|
|
|
|
}
|
2017-12-13 11:04:31 +01:00
|
|
|
zebra_ns_notify_create_context_from_entry_name(dent->d_name);
|
|
|
|
}
|
|
|
|
closedir(srcdir);
|
|
|
|
}
|
|
|
|
|
|
|
|
void zebra_ns_notify_init(void)
|
|
|
|
{
|
|
|
|
int fd_monitor;
|
|
|
|
|
|
|
|
fd_monitor = inotify_init();
|
|
|
|
if (fd_monitor < 0) {
|
2018-08-16 22:10:32 +02:00
|
|
|
flog_err_sys(
|
2018-09-13 21:34:28 +02:00
|
|
|
EC_LIB_SYSTEM_CALL,
|
2018-08-16 22:10:32 +02:00
|
|
|
"NS notify init: failed to initialize inotify (%s)",
|
|
|
|
safe_strerror(errno));
|
2017-12-13 11:04:31 +01:00
|
|
|
}
|
2018-03-22 18:02:00 +01:00
|
|
|
if (inotify_add_watch(fd_monitor, NS_RUN_DIR,
|
|
|
|
IN_CREATE | IN_DELETE) < 0) {
|
2018-09-13 21:34:28 +02:00
|
|
|
flog_err_sys(EC_LIB_SYSTEM_CALL,
|
2018-08-16 22:10:32 +02:00
|
|
|
"NS notify watch: failed to add watch (%s)",
|
|
|
|
safe_strerror(errno));
|
2017-12-13 11:04:31 +01:00
|
|
|
}
|
2021-10-06 21:06:23 +02:00
|
|
|
thread_add_read(zrouter.master, zebra_ns_notify_read, NULL, fd_monitor,
|
|
|
|
&zebra_netns_notify_current);
|
2017-12-13 11:04:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void zebra_ns_notify_close(void)
|
|
|
|
{
|
|
|
|
if (zebra_netns_notify_current == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
int fd = 0;
|
|
|
|
|
|
|
|
if (zebra_netns_notify_current->u.fd > 0)
|
|
|
|
fd = zebra_netns_notify_current->u.fd;
|
2018-09-21 22:21:15 +02:00
|
|
|
|
|
|
|
if (zebra_netns_notify_current->master != NULL)
|
2022-06-03 18:47:04 +02:00
|
|
|
THREAD_OFF(zebra_netns_notify_current);
|
2018-09-21 22:21:15 +02:00
|
|
|
|
|
|
|
/* auto-removal of notify items */
|
2017-12-13 11:04:31 +01:00
|
|
|
if (fd > 0)
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
void zebra_ns_notify_parse(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void zebra_ns_notify_init(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void zebra_ns_notify_close(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif /* !HAVE_NETLINK */
|