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"
39 #include "zebra_router.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_with_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(EC_ZEBRA_NS_VRF_CREATION_FAILED
,
96 "NS notify : failed to create VRF %s", name
);
97 ns_map_nsid_with_external(ns_id
, false);
100 frr_with_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(EC_ZEBRA_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(zrouter
.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(EC_ZEBRA_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_with_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_with_privs(&zserv_privs
) {
213 err
= ns_switchback_to_initial();
216 return zebra_ns_continue_read(zns_info
, stop_retry
);
218 /* check default name is not already set */
219 if (strmatch(VRF_DEFAULT_NAME
, basename(netnspath
))) {
220 zlog_warn("NS notify : NS %s is already default VRF."
221 "Cancel VRF Creation", basename(netnspath
));
222 return zebra_ns_continue_read(zns_info
, 1);
224 if (zebra_ns_notify_is_default_netns(basename(netnspath
))) {
226 "NS notify : NS %s is default VRF."
227 " Updating VRF Name", basename(netnspath
));
228 vrf_set_default_name(basename(netnspath
), false);
229 return zebra_ns_continue_read(zns_info
, 1);
232 /* success : close fd and create zns context */
233 zebra_ns_notify_create_context_from_entry_name(basename(netnspath
));
234 return zebra_ns_continue_read(zns_info
, 1);
237 static int zebra_ns_notify_read(struct thread
*t
)
239 int fd_monitor
= THREAD_FD(t
);
240 struct inotify_event
*event
;
244 zebra_netns_notify_current
= thread_add_read(
245 zrouter
.master
, zebra_ns_notify_read
, NULL
, fd_monitor
, NULL
);
246 len
= read(fd_monitor
, buf
, sizeof(buf
));
248 flog_err_sys(EC_ZEBRA_NS_NOTIFY_READ
,
249 "NS notify read: failed to read (%s)",
250 safe_strerror(errno
));
253 for (event
= (struct inotify_event
*)buf
; (char *)event
< &buf
[len
];
254 event
= (struct inotify_event
*)((char *)event
+ sizeof(*event
)
257 struct zebra_netns_info
*netnsinfo
;
259 if (!(event
->mask
& (IN_CREATE
| IN_DELETE
)))
262 if (offsetof(struct inotify_event
, name
) + event
->len
264 flog_err(EC_ZEBRA_NS_NOTIFY_READ
,
265 "NS notify read: buffer underflow");
269 if (strnlen(event
->name
, event
->len
) == event
->len
) {
270 flog_err(EC_ZEBRA_NS_NOTIFY_READ
,
271 "NS notify error: bad event name");
275 if (event
->mask
& IN_DELETE
) {
276 zebra_ns_delete(event
->name
);
279 netnspath
= ns_netns_pathname(NULL
, event
->name
);
282 netnspath
= XSTRDUP(MTYPE_NETNS_MISC
, netnspath
);
283 netnsinfo
= XCALLOC(MTYPE_NETNS_MISC
,
284 sizeof(struct zebra_netns_info
));
285 netnsinfo
->retries
= ZEBRA_NS_POLLING_MAX_RETRIES
;
286 netnsinfo
->netnspath
= netnspath
;
287 thread_add_timer_msec(zrouter
.master
, zebra_ns_ready_read
,
288 (void *)netnsinfo
, 0, NULL
);
293 void zebra_ns_notify_parse(void)
296 DIR *srcdir
= opendir(NS_RUN_DIR
);
298 if (srcdir
== NULL
) {
299 flog_err_sys(EC_LIB_SYSTEM_CALL
,
300 "NS parsing init: failed to parse %s", NS_RUN_DIR
);
303 while ((dent
= readdir(srcdir
)) != NULL
) {
306 if (strcmp(dent
->d_name
, ".") == 0
307 || strcmp(dent
->d_name
, "..") == 0)
309 if (fstatat(dirfd(srcdir
), dent
->d_name
, &st
, 0) < 0) {
312 "NS parsing init: failed to parse entry %s",
316 if (S_ISDIR(st
.st_mode
)) {
317 zlog_debug("NS parsing init: %s is not a NS",
321 /* check default name is not already set */
322 if (strmatch(VRF_DEFAULT_NAME
, basename(dent
->d_name
))) {
323 zlog_warn("NS notify : NS %s is already default VRF."
324 "Cancel VRF Creation", dent
->d_name
);
327 if (zebra_ns_notify_is_default_netns(dent
->d_name
)) {
329 "NS notify : NS %s is default VRF."
330 " Updating VRF Name", dent
->d_name
);
331 vrf_set_default_name(dent
->d_name
, false);
334 zebra_ns_notify_create_context_from_entry_name(dent
->d_name
);
339 void zebra_ns_notify_init(void)
343 zebra_netns_notify_current
= NULL
;
344 fd_monitor
= inotify_init();
345 if (fd_monitor
< 0) {
348 "NS notify init: failed to initialize inotify (%s)",
349 safe_strerror(errno
));
351 if (inotify_add_watch(fd_monitor
, NS_RUN_DIR
,
352 IN_CREATE
| IN_DELETE
) < 0) {
353 flog_err_sys(EC_LIB_SYSTEM_CALL
,
354 "NS notify watch: failed to add watch (%s)",
355 safe_strerror(errno
));
357 zebra_netns_notify_current
= thread_add_read(
358 zrouter
.master
, zebra_ns_notify_read
, NULL
, fd_monitor
, NULL
);
361 void zebra_ns_notify_close(void)
363 if (zebra_netns_notify_current
== NULL
)
368 if (zebra_netns_notify_current
->u
.fd
> 0)
369 fd
= zebra_netns_notify_current
->u
.fd
;
371 if (zebra_netns_notify_current
->master
!= NULL
)
372 thread_cancel(zebra_netns_notify_current
);
374 /* auto-removal of notify items */
380 void zebra_ns_notify_parse(void)
384 void zebra_ns_notify_init(void)
388 void zebra_ns_notify_close(void)
391 #endif /* !HAVE_NETLINK */