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/logicalrouter.h"
27 #include "lib/prefix.h"
28 #include "lib/memory.h"
32 #include "zebra_vrf.h"
33 #include "zebra_memory.h"
35 #include "zebra_vxlan.h"
37 #include "zebra_netns_notify.h"
38 #include "zebra_netns_id.h"
39 #include "zebra_pbr.h"
41 #include "table_manager.h"
43 extern struct zebra_privs_t zserv_privs
;
45 DEFINE_MTYPE(ZEBRA
, ZEBRA_NS
, "Zebra Name Space")
47 static struct zebra_ns
*dzns
;
49 static int logicalrouter_config_write(struct vty
*vty
);
50 static int zebra_ns_disable_internal(struct zebra_ns
*zns
, bool complete
);
52 struct zebra_ns
*zebra_ns_lookup(ns_id_t ns_id
)
54 if (ns_id
== NS_DEFAULT
)
56 struct zebra_ns
*info
= (struct zebra_ns
*)ns_info_lookup(ns_id
);
58 return (info
== NULL
) ? dzns
: info
;
61 static struct zebra_ns
*zebra_ns_alloc(void)
63 return XCALLOC(MTYPE_ZEBRA_NS
, sizeof(struct zebra_ns
));
66 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();
77 /* Do any needed per-NS data structure allocation. */
78 zns
->if_table
= route_table_init();
83 static int zebra_ns_delete(struct ns
*ns
)
85 struct zebra_ns
*zns
= (struct zebra_ns
*)ns
->info
;
87 if (IS_ZEBRA_DEBUG_EVENT
)
88 zlog_info("ZNS %s with id %u (deleted)", ns
->name
, ns
->ns_id
);
91 XFREE(MTYPE_ZEBRA_NS
, zns
);
95 static int zebra_ns_enabled(struct ns
*ns
)
97 struct zebra_ns
*zns
= ns
->info
;
99 if (IS_ZEBRA_DEBUG_EVENT
)
100 zlog_info("ZNS %s with id %u (enabled)", ns
->name
, ns
->ns_id
);
103 return zebra_ns_enable(ns
->ns_id
, (void **)&zns
);
106 int zebra_ns_disabled(struct ns
*ns
)
108 struct zebra_ns
*zns
= ns
->info
;
110 if (IS_ZEBRA_DEBUG_EVENT
)
111 zlog_info("ZNS %s with id %u (disabled)", ns
->name
, ns
->ns_id
);
114 return zebra_ns_disable_internal(zns
, true);
117 /* Do global enable actions - open sockets, read kernel config etc. */
118 int zebra_ns_enable(ns_id_t ns_id
, void **info
)
120 struct zebra_ns
*zns
= (struct zebra_ns
*)(*info
);
124 #if defined(HAVE_RTADV)
132 /* Initiate Table Manager per ZNS */
133 table_manager_enable(ns_id
);
138 /* Common handler for ns disable - this can be called during ns config,
139 * or during zebra shutdown.
141 static int zebra_ns_disable_internal(struct zebra_ns
*zns
, bool complete
)
143 route_table_finish(zns
->if_table
);
144 #if defined(HAVE_RTADV)
145 rtadv_terminate(zns
);
148 kernel_terminate(zns
, complete
);
150 table_manager_disable(zns
->ns_id
);
152 zns
->ns_id
= NS_DEFAULT
;
157 /* During zebra shutdown, do partial cleanup while the async dataplane
160 int zebra_ns_early_shutdown(struct ns
*ns
)
162 struct zebra_ns
*zns
= ns
->info
;
167 return zebra_ns_disable_internal(zns
, false);
170 /* During zebra shutdown, do final cleanup
171 * after all dataplane work is complete.
173 int zebra_ns_final_shutdown(struct ns
*ns
)
175 struct zebra_ns
*zns
= ns
->info
;
180 kernel_terminate(zns
, true);
185 int zebra_ns_init(void)
188 ns_id_t ns_id_external
;
190 dzns
= zebra_ns_alloc();
192 frr_elevate_privs(&zserv_privs
) {
193 ns_id
= zebra_ns_id_get_default();
195 ns_id_external
= ns_map_nsid_with_external(ns_id
, true);
196 ns_init_management(ns_id_external
, ns_id
);
198 logicalrouter_init(logicalrouter_config_write
);
200 /* Do any needed per-NS data structure allocation. */
201 dzns
->if_table
= route_table_init();
203 /* Register zebra VRF callbacks, create and activate default VRF. */
206 /* Default NS is activated */
207 zebra_ns_enable(ns_id_external
, (void **)&dzns
);
209 if (vrf_is_backend_netns()) {
210 ns_add_hook(NS_NEW_HOOK
, zebra_ns_new
);
211 ns_add_hook(NS_ENABLE_HOOK
, zebra_ns_enabled
);
212 ns_add_hook(NS_DISABLE_HOOK
, zebra_ns_disabled
);
213 ns_add_hook(NS_DELETE_HOOK
, zebra_ns_delete
);
214 zebra_ns_notify_parse();
215 zebra_ns_notify_init();
221 static int logicalrouter_config_write(struct vty
*vty
)
226 RB_FOREACH (ns
, ns_head
, &ns_tree
) {
227 if (ns
->ns_id
== NS_DEFAULT
|| ns
->name
== NULL
)
229 vty_out(vty
, "logical-router %u netns %s\n", ns
->ns_id
,
236 int zebra_ns_config_write(struct vty
*vty
, struct ns
*ns
)
238 if (ns
&& ns
->name
!= NULL
)
239 vty_out(vty
, " netns %s\n", ns
->name
);