2 * Copyright (C) 2016 Cumulus Networks, Inc.
4 * Copyright (C) 2017/2018 6WIND
6 * This file is part of Quagga.
8 * Quagga is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any
13 * Quagga is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; see the file COPYING; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "lib/prefix.h"
27 #include "lib/memory.h"
30 #include "zebra_vrf.h"
32 #include "zebra_vxlan.h"
34 #include "zebra_netns_notify.h"
35 #include "zebra_netns_id.h"
36 #include "zebra_pbr.h"
38 #include "table_manager.h"
39 #include "zebra_errors.h"
41 extern struct zebra_privs_t zserv_privs
;
43 DEFINE_MTYPE_STATIC(ZEBRA
, ZEBRA_NS
, "Zebra Name Space");
45 static struct zebra_ns
*dzns
;
47 static int zebra_ns_disable_internal(struct zebra_ns
*zns
, bool complete
);
49 struct zebra_ns
*zebra_ns_lookup(ns_id_t ns_id
)
51 if (ns_id
== NS_DEFAULT
)
53 struct zebra_ns
*info
= (struct zebra_ns
*)ns_info_lookup(ns_id
);
55 return (info
== NULL
) ? dzns
: info
;
58 static struct zebra_ns
*zebra_ns_alloc(void)
60 return XCALLOC(MTYPE_ZEBRA_NS
, sizeof(struct zebra_ns
));
63 static int zebra_ns_new(struct ns
*ns
)
70 if (IS_ZEBRA_DEBUG_EVENT
)
71 zlog_info("ZNS %s with id %u (created)", ns
->name
, ns
->ns_id
);
73 zns
= zebra_ns_alloc();
76 zns
->ns_id
= ns
->ns_id
;
78 /* Do any needed per-NS data structure allocation. */
79 zns
->if_table
= route_table_init();
84 static int zebra_ns_delete(struct ns
*ns
)
86 struct zebra_ns
*zns
= (struct zebra_ns
*)ns
->info
;
88 if (IS_ZEBRA_DEBUG_EVENT
)
89 zlog_info("ZNS %s with id %u (deleted)", ns
->name
, ns
->ns_id
);
92 XFREE(MTYPE_ZEBRA_NS
, ns
->info
);
96 static int zebra_ns_enabled(struct ns
*ns
)
98 struct zebra_ns
*zns
= ns
->info
;
100 if (IS_ZEBRA_DEBUG_EVENT
)
101 zlog_info("ZNS %s with id %u (enabled)", ns
->name
, ns
->ns_id
);
104 return zebra_ns_enable(ns
->ns_id
, (void **)&zns
);
107 int zebra_ns_disabled(struct ns
*ns
)
109 struct zebra_ns
*zns
= ns
->info
;
111 if (IS_ZEBRA_DEBUG_EVENT
)
112 zlog_info("ZNS %s with id %u (disabled)", ns
->name
, ns
->ns_id
);
115 return zebra_ns_disable_internal(zns
, true);
118 /* Do global enable actions - open sockets, read kernel config etc. */
119 int zebra_ns_enable(ns_id_t ns_id
, void **info
)
121 struct zebra_ns
*zns
= (struct zebra_ns
*)(*info
);
126 zebra_dplane_ns_enable(zns
, true);
129 kernel_read_pbr_rules(zns
);
134 /* Common handler for ns disable - this can be called during ns config,
135 * or during zebra shutdown.
137 static int zebra_ns_disable_internal(struct zebra_ns
*zns
, bool complete
)
139 route_table_finish(zns
->if_table
);
141 zebra_dplane_ns_enable(zns
, false /*Disable*/);
143 kernel_terminate(zns
, complete
);
145 zns
->ns_id
= NS_DEFAULT
;
150 /* During zebra shutdown, do partial cleanup while the async dataplane
153 int zebra_ns_early_shutdown(struct ns
*ns
,
154 void *param_in
__attribute__((unused
)),
155 void **param_out
__attribute__((unused
)))
157 struct zebra_ns
*zns
= ns
->info
;
162 zebra_ns_disable_internal(zns
, false);
163 return NS_WALK_CONTINUE
;
166 /* During zebra shutdown, do final cleanup
167 * after all dataplane work is complete.
169 int zebra_ns_final_shutdown(struct ns
*ns
,
170 void *param_in
__attribute__((unused
)),
171 void **param_out
__attribute__((unused
)))
173 struct zebra_ns
*zns
= ns
->info
;
178 kernel_terminate(zns
, true);
180 return NS_WALK_CONTINUE
;
183 int zebra_ns_init(const char *optional_default_name
)
185 struct ns
*default_ns
;
187 ns_id_t ns_id_external
;
190 frr_with_privs(&zserv_privs
) {
191 ns_id
= zebra_ns_id_get_default();
193 ns_id_external
= ns_map_nsid_with_external(ns_id
, true);
194 ns_init_management(ns_id_external
, ns_id
);
195 ns
= ns_get_default();
197 ns
->relative_default_ns
= ns_id
;
199 default_ns
= ns_lookup(NS_DEFAULT
);
201 flog_err(EC_ZEBRA_NS_NO_DEFAULT
,
202 "%s: failed to find default ns", __func__
);
203 exit(EXIT_FAILURE
); /* This is non-recoverable */
206 /* Do any needed per-NS data structure allocation. */
207 zebra_ns_new(default_ns
);
208 dzns
= default_ns
->info
;
210 /* Register zebra VRF callbacks, create and activate default VRF. */
213 /* Default NS is activated */
214 zebra_ns_enable(ns_id_external
, (void **)&dzns
);
216 if (optional_default_name
)
217 vrf_set_default_name(optional_default_name
,
220 if (vrf_is_backend_netns()) {
221 ns_add_hook(NS_NEW_HOOK
, zebra_ns_new
);
222 ns_add_hook(NS_ENABLE_HOOK
, zebra_ns_enabled
);
223 ns_add_hook(NS_DISABLE_HOOK
, zebra_ns_disabled
);
224 ns_add_hook(NS_DELETE_HOOK
, zebra_ns_delete
);
225 zebra_ns_notify_parse();
226 zebra_ns_notify_init();
232 int zebra_ns_config_write(struct vty
*vty
, struct ns
*ns
)
234 if (ns
&& ns
->name
!= NULL
)
235 vty_out(vty
, " netns %s\n", ns
->name
);