2 * Zebra NS collector and notifier for Network NameSpaces
3 * Copyright (C) 2017 6WIND
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along
16 * with this program; see the file COPYING; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 #include <sys/inotify.h>
37 #include "lib_errors.h"
40 #include "zebra_memory.h"
41 #endif /* defined(HAVE_NETLINK) */
43 #include "zebra_netns_notify.h"
44 #include "zebra_netns_id.h"
45 #include "zebra_errors.h"
49 /* upon creation of folder under /var/run/netns,
50 * wait that netns context is bound to
51 * that folder 10 seconds
53 #define ZEBRA_NS_POLLING_INTERVAL_MSEC 1000
54 #define ZEBRA_NS_POLLING_MAX_RETRIES 200
56 DEFINE_MTYPE_STATIC(ZEBRA
, NETNS_MISC
, "ZebraNetNSInfo")
57 static struct thread
*zebra_netns_notify_current
;
59 struct zebra_netns_info
{
60 const char *netnspath
;
64 static int zebra_ns_ready_read(struct thread
*t
);
65 static void zebra_ns_notify_create_context_from_entry_name(const char *name
);
66 static int zebra_ns_continue_read(struct zebra_netns_info
*zns_info
,
68 static int zebra_ns_notify_read(struct thread
*t
);
70 static void zebra_ns_notify_create_context_from_entry_name(const char *name
)
72 char *netnspath
= ns_netns_pathname(NULL
, name
);
75 ns_id_t ns_id
, ns_id_external
;
77 if (netnspath
== NULL
)
80 frr_elevate_privs(&zserv_privs
) {
81 ns_id
= zebra_ns_id_get(netnspath
);
83 if (ns_id
== NS_UNKNOWN
)
85 ns_id_external
= ns_map_nsid_with_external(ns_id
, true);
86 /* if VRF with NS ID already present */
87 vrf
= vrf_lookup_by_id((vrf_id_t
)ns_id_external
);
90 "NS notify : same NSID used by VRF %s. Ignore NS %s creation",
91 vrf
->name
, netnspath
);
94 if (vrf_handler_create(NULL
, name
, &vrf
) != CMD_SUCCESS
) {
95 flog_warn(ZEBRA_ERR_NS_VRF_CREATION_FAILED
,
96 "NS notify : failed to create VRF %s", name
);
97 ns_map_nsid_with_external(ns_id
, false);
100 frr_elevate_privs(&zserv_privs
) {
101 ret
= vrf_netns_handler_create(NULL
, vrf
, netnspath
,
102 ns_id_external
, ns_id
);
104 if (ret
!= CMD_SUCCESS
) {
105 flog_warn(ZEBRA_ERR_NS_VRF_CREATION_FAILED
,
106 "NS notify : failed to create NS %s", netnspath
);
107 ns_map_nsid_with_external(ns_id
, false);
111 zlog_info("NS notify : created VRF %s NS %s", name
, netnspath
);
114 static int zebra_ns_continue_read(struct zebra_netns_info
*zns_info
,
117 void *ns_path_ptr
= (void *)zns_info
->netnspath
;
120 XFREE(MTYPE_NETNS_MISC
, ns_path_ptr
);
121 XFREE(MTYPE_NETNS_MISC
, zns_info
);
124 thread_add_timer_msec(zebrad
.master
, zebra_ns_ready_read
,
125 (void *)zns_info
, ZEBRA_NS_POLLING_INTERVAL_MSEC
,
130 static int zebra_ns_delete(char *name
)
132 struct vrf
*vrf
= vrf_lookup_by_name(name
);
136 flog_warn(ZEBRA_ERR_NS_DELETION_FAILED_NO_VRF
,
137 "NS notify : no VRF found using NS %s", name
);
140 /* Clear configured flag and invoke delete. */
141 UNSET_FLAG(vrf
->status
, VRF_CONFIGURED
);
142 ns
= (struct ns
*)vrf
->ns_ctxt
;
143 /* the deletion order is the same
144 * as the one used when siging signal is received
150 zlog_info("NS notify : deleted VRF %s", name
);
154 static int zebra_ns_notify_self_identify(struct stat
*netst
)
159 sprintf(net_path
, "/proc/self/ns/net");
160 netns
= open(net_path
, O_RDONLY
);
163 if (fstat(netns
, netst
) < 0) {
171 static bool zebra_ns_notify_is_default_netns(const char *name
)
173 struct stat default_netns_stat
;
177 if (zebra_ns_notify_self_identify(&default_netns_stat
))
180 memset(&st
, 0, sizeof(struct stat
));
181 snprintf(netnspath
, 64, "%s/%s", NS_RUN_DIR
, name
);
182 /* compare with local stat */
183 if (stat(netnspath
, &st
) == 0 &&
184 (st
.st_dev
== default_netns_stat
.st_dev
) &&
185 (st
.st_ino
== default_netns_stat
.st_ino
))
190 static int zebra_ns_ready_read(struct thread
*t
)
192 struct zebra_netns_info
*zns_info
= THREAD_ARG(t
);
193 const char *netnspath
;
194 int err
, stop_retry
= 0;
198 if (!zns_info
->netnspath
) {
199 XFREE(MTYPE_NETNS_MISC
, zns_info
);
202 netnspath
= zns_info
->netnspath
;
203 if (--zns_info
->retries
== 0)
205 frr_elevate_privs(&zserv_privs
) {
206 err
= ns_switch_to_netns(netnspath
);
209 return zebra_ns_continue_read(zns_info
, stop_retry
);
211 /* go back to default ns */
212 frr_elevate_privs(&zserv_privs
) {
213 err
= ns_switchback_to_initial();
216 return zebra_ns_continue_read(zns_info
, stop_retry
);
218 if (zebra_ns_notify_is_default_netns(basename(netnspath
))) {
220 "NS notify : NS %s is default VRF."
221 " Updating VRF Name", basename(netnspath
));
222 vrf_set_default_name(basename(netnspath
));
223 return zebra_ns_continue_read(zns_info
, 1);
226 /* success : close fd and create zns context */
227 zebra_ns_notify_create_context_from_entry_name(basename(netnspath
));
228 return zebra_ns_continue_read(zns_info
, 1);
231 static int zebra_ns_notify_read(struct thread
*t
)
233 int fd_monitor
= THREAD_FD(t
);
234 struct inotify_event
*event
;
238 zebra_netns_notify_current
= thread_add_read(
239 zebrad
.master
, zebra_ns_notify_read
, NULL
, fd_monitor
, NULL
);
240 len
= read(fd_monitor
, buf
, sizeof(buf
));
242 flog_err_sys(ZEBRA_ERR_NS_NOTIFY_READ
,
243 "NS notify read: failed to read (%s)",
244 safe_strerror(errno
));
247 for (event
= (struct inotify_event
*)buf
; (char *)event
< &buf
[len
];
248 event
= (struct inotify_event
*)((char *)event
+ sizeof(*event
)
251 struct zebra_netns_info
*netnsinfo
;
253 if (!(event
->mask
& (IN_CREATE
| IN_DELETE
)))
255 if (event
->mask
& IN_DELETE
)
256 return zebra_ns_delete(event
->name
);
258 if (offsetof(struct inotify_event
, name
) + event
->len
260 flog_err(ZEBRA_ERR_NS_NOTIFY_READ
,
261 "NS notify read: buffer underflow");
265 if (strnlen(event
->name
, event
->len
) == event
->len
) {
266 flog_err(ZEBRA_ERR_NS_NOTIFY_READ
,
267 "NS notify error: bad event name");
271 netnspath
= ns_netns_pathname(NULL
, event
->name
);
274 netnspath
= XSTRDUP(MTYPE_NETNS_MISC
, netnspath
);
275 netnsinfo
= XCALLOC(MTYPE_NETNS_MISC
,
276 sizeof(struct zebra_netns_info
));
277 netnsinfo
->retries
= ZEBRA_NS_POLLING_MAX_RETRIES
;
278 netnsinfo
->netnspath
= netnspath
;
279 thread_add_timer_msec(zebrad
.master
, zebra_ns_ready_read
,
280 (void *)netnsinfo
, 0, NULL
);
285 void zebra_ns_notify_parse(void)
288 DIR *srcdir
= opendir(NS_RUN_DIR
);
290 if (srcdir
== NULL
) {
291 flog_err_sys(LIB_ERR_SYSTEM_CALL
,
292 "NS parsing init: failed to parse %s", NS_RUN_DIR
);
295 while ((dent
= readdir(srcdir
)) != NULL
) {
298 if (strcmp(dent
->d_name
, ".") == 0
299 || strcmp(dent
->d_name
, "..") == 0)
301 if (fstatat(dirfd(srcdir
), dent
->d_name
, &st
, 0) < 0) {
304 "NS parsing init: failed to parse entry %s",
308 if (S_ISDIR(st
.st_mode
)) {
309 zlog_debug("NS parsing init: %s is not a NS",
313 if (zebra_ns_notify_is_default_netns(dent
->d_name
)) {
315 "NS notify : NS %s is default VRF."
316 " Updating VRF Name", dent
->d_name
);
317 vrf_set_default_name(dent
->d_name
);
320 zebra_ns_notify_create_context_from_entry_name(dent
->d_name
);
325 void zebra_ns_notify_init(void)
329 zebra_netns_notify_current
= NULL
;
330 fd_monitor
= inotify_init();
331 if (fd_monitor
< 0) {
334 "NS notify init: failed to initialize inotify (%s)",
335 safe_strerror(errno
));
337 if (inotify_add_watch(fd_monitor
, NS_RUN_DIR
,
338 IN_CREATE
| IN_DELETE
) < 0) {
339 flog_err_sys(LIB_ERR_SYSTEM_CALL
,
340 "NS notify watch: failed to add watch (%s)",
341 safe_strerror(errno
));
343 zebra_netns_notify_current
= thread_add_read(
344 zebrad
.master
, zebra_ns_notify_read
, NULL
, fd_monitor
, NULL
);
347 void zebra_ns_notify_close(void)
349 if (zebra_netns_notify_current
== NULL
)
354 if (zebra_netns_notify_current
->u
.fd
> 0)
355 fd
= zebra_netns_notify_current
->u
.fd
;
356 thread_cancel(zebra_netns_notify_current
);
357 /* auto-removal of inotify items */
363 void zebra_ns_notify_parse(void)
367 void zebra_ns_notify_init(void)
371 void zebra_ns_notify_close(void)
374 #endif /* !HAVE_NETLINK */