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"
48 /* upon creation of folder under /var/run/netns,
49 * wait that netns context is bound to
50 * that folder 10 seconds
52 #define ZEBRA_NS_POLLING_INTERVAL_MSEC 1000
53 #define ZEBRA_NS_POLLING_MAX_RETRIES 200
55 DEFINE_MTYPE_STATIC(ZEBRA
, NETNS_MISC
, "ZebraNetNSInfo")
56 static struct thread
*zebra_netns_notify_current
;
58 struct zebra_netns_info
{
59 const char *netnspath
;
63 static int zebra_ns_ready_read(struct thread
*t
);
64 static void zebra_ns_notify_create_context_from_entry_name(const char *name
);
65 static int zebra_ns_continue_read(struct zebra_netns_info
*zns_info
,
67 static int zebra_ns_notify_read(struct thread
*t
);
69 static void zebra_ns_notify_create_context_from_entry_name(const char *name
)
71 char *netnspath
= ns_netns_pathname(NULL
, name
);
74 ns_id_t ns_id
, ns_id_external
;
76 if (netnspath
== NULL
)
79 frr_elevate_privs(&zserv_privs
) {
80 ns_id
= zebra_ns_id_get(netnspath
);
82 if (ns_id
== NS_UNKNOWN
)
84 ns_id_external
= ns_map_nsid_with_external(ns_id
, true);
85 /* if VRF with NS ID already present */
86 vrf
= vrf_lookup_by_id((vrf_id_t
)ns_id_external
);
89 "NS notify : same NSID used by VRF %s. Ignore NS %s creation",
90 vrf
->name
, netnspath
);
93 if (vrf_handler_create(NULL
, name
, &vrf
) != CMD_SUCCESS
) {
94 zlog_warn("NS notify : failed to create VRF %s", name
);
95 ns_map_nsid_with_external(ns_id
, false);
98 frr_elevate_privs(&zserv_privs
) {
99 ret
= vrf_netns_handler_create(NULL
, vrf
, netnspath
,
100 ns_id_external
, ns_id
);
102 if (ret
!= CMD_SUCCESS
) {
103 zlog_warn("NS notify : failed to create NS %s", netnspath
);
104 ns_map_nsid_with_external(ns_id
, false);
108 zlog_info("NS notify : created VRF %s NS %s", name
, netnspath
);
111 static int zebra_ns_continue_read(struct zebra_netns_info
*zns_info
,
114 void *ns_path_ptr
= (void *)zns_info
->netnspath
;
117 XFREE(MTYPE_NETNS_MISC
, ns_path_ptr
);
118 XFREE(MTYPE_NETNS_MISC
, zns_info
);
121 thread_add_timer_msec(zebrad
.master
, zebra_ns_ready_read
,
122 (void *)zns_info
, ZEBRA_NS_POLLING_INTERVAL_MSEC
,
127 static int zebra_ns_delete(char *name
)
129 struct vrf
*vrf
= vrf_lookup_by_name(name
);
134 "NS notify : no VRF found using NS %s",
138 /* Clear configured flag and invoke delete. */
139 UNSET_FLAG(vrf
->status
, VRF_CONFIGURED
);
140 ns
= (struct ns
*)vrf
->ns_ctxt
;
141 /* the deletion order is the same
142 * as the one used when siging signal is received
148 zlog_info("NS notify : deleted VRF %s", name
);
153 static int zebra_ns_ready_read(struct thread
*t
)
155 struct zebra_netns_info
*zns_info
= THREAD_ARG(t
);
156 const char *netnspath
;
157 int err
, stop_retry
= 0;
161 if (!zns_info
->netnspath
) {
162 XFREE(MTYPE_NETNS_MISC
, zns_info
);
165 netnspath
= zns_info
->netnspath
;
166 if (--zns_info
->retries
== 0)
168 frr_elevate_privs(&zserv_privs
) {
169 err
= ns_switch_to_netns(netnspath
);
172 return zebra_ns_continue_read(zns_info
, stop_retry
);
174 /* go back to default ns */
175 frr_elevate_privs(&zserv_privs
) {
176 err
= ns_switchback_to_initial();
179 return zebra_ns_continue_read(zns_info
, stop_retry
);
181 /* success : close fd and create zns context */
182 zebra_ns_notify_create_context_from_entry_name(basename(netnspath
));
183 return zebra_ns_continue_read(zns_info
, 1);
186 static int zebra_ns_notify_read(struct thread
*t
)
188 int fd_monitor
= THREAD_FD(t
);
189 struct inotify_event
*event
;
193 zebra_netns_notify_current
= thread_add_read(
194 zebrad
.master
, zebra_ns_notify_read
, NULL
, fd_monitor
, NULL
);
195 len
= read(fd_monitor
, buf
, sizeof(buf
));
197 zlog_warn("NS notify read: failed to read (%s)",
198 safe_strerror(errno
));
201 for (event
= (struct inotify_event
*)buf
; (char *)event
< &buf
[len
];
202 event
= (struct inotify_event
*)((char *)event
+ sizeof(*event
)
205 struct zebra_netns_info
*netnsinfo
;
207 if (!(event
->mask
& (IN_CREATE
| IN_DELETE
)))
209 if (event
->mask
& IN_DELETE
)
210 return zebra_ns_delete(event
->name
);
212 if (offsetof(struct inotify_event
, name
) + event
->len
214 zlog_err("NS notify read: buffer underflow");
218 if (strnlen(event
->name
, event
->len
) == event
->len
) {
219 zlog_err("NS notify error: bad event name");
223 netnspath
= ns_netns_pathname(NULL
, event
->name
);
226 netnspath
= XSTRDUP(MTYPE_NETNS_MISC
, netnspath
);
227 netnsinfo
= XCALLOC(MTYPE_NETNS_MISC
,
228 sizeof(struct zebra_netns_info
));
229 netnsinfo
->retries
= ZEBRA_NS_POLLING_MAX_RETRIES
;
230 netnsinfo
->netnspath
= netnspath
;
231 thread_add_timer_msec(zebrad
.master
, zebra_ns_ready_read
,
232 (void *)netnsinfo
, 0, NULL
);
237 void zebra_ns_notify_parse(void)
240 DIR *srcdir
= opendir(NS_RUN_DIR
);
242 if (srcdir
== NULL
) {
243 zlog_warn("NS parsing init: failed to parse %s", NS_RUN_DIR
);
246 while ((dent
= readdir(srcdir
)) != NULL
) {
249 if (strcmp(dent
->d_name
, ".") == 0
250 || strcmp(dent
->d_name
, "..") == 0)
252 if (fstatat(dirfd(srcdir
), dent
->d_name
, &st
, 0) < 0) {
253 zlog_warn("NS parsing init: failed to parse entry %s",
257 if (S_ISDIR(st
.st_mode
)) {
258 zlog_warn("NS parsing init: %s is not a NS",
262 zebra_ns_notify_create_context_from_entry_name(dent
->d_name
);
267 void zebra_ns_notify_init(void)
271 zebra_netns_notify_current
= NULL
;
272 fd_monitor
= inotify_init();
273 if (fd_monitor
< 0) {
274 zlog_warn("NS notify init: failed to initialize inotify (%s)",
275 safe_strerror(errno
));
277 if (inotify_add_watch(fd_monitor
, NS_RUN_DIR
,
278 IN_CREATE
| IN_DELETE
) < 0) {
279 zlog_warn("NS notify watch: failed to add watch (%s)",
280 safe_strerror(errno
));
282 zebra_netns_notify_current
= thread_add_read(
283 zebrad
.master
, zebra_ns_notify_read
, NULL
, fd_monitor
, NULL
);
286 void zebra_ns_notify_close(void)
288 if (zebra_netns_notify_current
== NULL
)
293 if (zebra_netns_notify_current
->u
.fd
> 0)
294 fd
= zebra_netns_notify_current
->u
.fd
;
295 thread_cancel(zebra_netns_notify_current
);
296 /* auto-removal of inotify items */
302 void zebra_ns_notify_parse(void)
306 void zebra_ns_notify_init(void)
310 void zebra_ns_notify_close(void)
313 #endif /* !HAVE_NETLINK */