]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zebra_ns.c
zebra: importation of bgp evpn rt5 from vni with other netns
[mirror_frr.git] / zebra / zebra_ns.c
CommitLineData
fe18ee2d
DS
1/* zebra NS Routines
2 * Copyright (C) 2016 Cumulus Networks, Inc.
3 * Donald Sharp
b95c1883 4 * Copyright (C) 2017/2018 6WIND
fe18ee2d
DS
5 *
6 * This file is part of Quagga.
7 *
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
11 * later version.
12 *
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.
17 *
896014f4
DL
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
fe18ee2d
DS
21 */
22#include "zebra.h"
23
13460c44 24#include "lib/ns.h"
fe18ee2d
DS
25#include "lib/vrf.h"
26#include "lib/prefix.h"
27#include "lib/memory.h"
28
fe18ee2d 29#include "zebra_ns.h"
7c551956 30#include "zebra_vrf.h"
4a1ab8e4 31#include "zebra_memory.h"
05f7f5db 32#include "rt.h"
b7cfce93 33#include "zebra_vxlan.h"
3347430b 34#include "debug.h"
e27dec3c 35#include "zebra_netns_notify.h"
ec31f30d 36#include "zebra_netns_id.h"
43fe6a2a 37#include "zebra_pbr.h"
47a08aa9 38#include "rib.h"
8288a24f 39#include "table_manager.h"
257b245c 40#include "zebra_errors.h"
ec31f30d
PG
41
42extern struct zebra_privs_t zserv_privs;
4a1ab8e4 43
d62a17ae 44DEFINE_MTYPE(ZEBRA, ZEBRA_NS, "Zebra Name Space")
fe18ee2d 45
337960dd 46static struct zebra_ns *dzns;
fe18ee2d 47
62b8bb7a 48static int zebra_ns_disable_internal(struct zebra_ns *zns, bool complete);
736d41ad 49
d62a17ae 50struct zebra_ns *zebra_ns_lookup(ns_id_t ns_id)
fe18ee2d 51{
ff705b15
PG
52 if (ns_id == NS_DEFAULT)
53 return dzns;
54 struct zebra_ns *info = (struct zebra_ns *)ns_info_lookup(ns_id);
55
56 return (info == NULL) ? dzns : info;
fe18ee2d
DS
57}
58
3347430b
PG
59static struct zebra_ns *zebra_ns_alloc(void)
60{
61 return XCALLOC(MTYPE_ZEBRA_NS, sizeof(struct zebra_ns));
62}
63
64static int zebra_ns_new(struct ns *ns)
65{
66 struct zebra_ns *zns;
67
257b245c
SW
68 if (!ns)
69 return -1;
70
3347430b
PG
71 if (IS_ZEBRA_DEBUG_EVENT)
72 zlog_info("ZNS %s with id %u (created)", ns->name, ns->ns_id);
73
74 zns = zebra_ns_alloc();
75 ns->info = zns;
76 zns->ns = ns;
3f6e9ae5 77 zns->ns_id = ns->ns_id;
ff705b15
PG
78
79 /* Do any needed per-NS data structure allocation. */
80 zns->if_table = route_table_init();
ff705b15 81
3347430b
PG
82 return 0;
83}
84
85static int zebra_ns_delete(struct ns *ns)
86{
996c9314 87 struct zebra_ns *zns = (struct zebra_ns *)ns->info;
3347430b
PG
88
89 if (IS_ZEBRA_DEBUG_EVENT)
90 zlog_info("ZNS %s with id %u (deleted)", ns->name, ns->ns_id);
91 if (!zns)
92 return 0;
fd4c24f5 93 XFREE(MTYPE_ZEBRA_NS, ns->info);
3347430b
PG
94 return 0;
95}
96
97static int zebra_ns_enabled(struct ns *ns)
98{
99 struct zebra_ns *zns = ns->info;
100
101 if (IS_ZEBRA_DEBUG_EVENT)
102 zlog_info("ZNS %s with id %u (enabled)", ns->name, ns->ns_id);
103 if (!zns)
104 return 0;
105 return zebra_ns_enable(ns->ns_id, (void **)&zns);
106}
107
ff705b15 108int zebra_ns_disabled(struct ns *ns)
3347430b
PG
109{
110 struct zebra_ns *zns = ns->info;
111
112 if (IS_ZEBRA_DEBUG_EVENT)
113 zlog_info("ZNS %s with id %u (disabled)", ns->name, ns->ns_id);
114 if (!zns)
115 return 0;
62b8bb7a 116 return zebra_ns_disable_internal(zns, true);
3347430b
PG
117}
118
84915b0a 119/* Do global enable actions - open sockets, read kernel config etc. */
d62a17ae 120int zebra_ns_enable(ns_id_t ns_id, void **info)
fe18ee2d 121{
d62a17ae 122 struct zebra_ns *zns = (struct zebra_ns *)(*info);
fe18ee2d 123
ff705b15
PG
124 zns->ns_id = ns_id;
125
d62a17ae 126 kernel_init(zns);
127 interface_list(zns);
128 route_read(zns);
ab35be75 129 kernel_read_pbr_rules(zns);
fe18ee2d 130
8288a24f
PG
131 /* Initiate Table Manager per ZNS */
132 table_manager_enable(ns_id);
133
d62a17ae 134 return 0;
fe18ee2d
DS
135}
136
62b8bb7a
MS
137/* Common handler for ns disable - this can be called during ns config,
138 * or during zebra shutdown.
139 */
140static int zebra_ns_disable_internal(struct zebra_ns *zns, bool complete)
fe18ee2d 141{
d62a17ae 142 route_table_finish(zns->if_table);
fe18ee2d 143
62b8bb7a 144 kernel_terminate(zns, complete);
fe18ee2d 145
8288a24f
PG
146 table_manager_disable(zns->ns_id);
147
ff705b15
PG
148 zns->ns_id = NS_DEFAULT;
149
d62a17ae 150 return 0;
fe18ee2d
DS
151}
152
62b8bb7a
MS
153/* During zebra shutdown, do partial cleanup while the async dataplane
154 * is still running.
155 */
bd23c840 156int zebra_ns_early_shutdown(struct ns *ns)
62b8bb7a
MS
157{
158 struct zebra_ns *zns = ns->info;
159
160 if (zns == NULL)
161 return 0;
162
bd23c840 163 return zebra_ns_disable_internal(zns, false);
62b8bb7a
MS
164}
165
166/* During zebra shutdown, do final cleanup
167 * after all dataplane work is complete.
168 */
bd23c840 169int zebra_ns_final_shutdown(struct ns *ns)
62b8bb7a
MS
170{
171 struct zebra_ns *zns = ns->info;
172
173 if (zns == NULL)
174 return 0;
175
176 kernel_terminate(zns, true);
177
bd23c840 178 return 0;
62b8bb7a 179}
5335613b 180
edbc3322 181int zebra_ns_init(const char *optional_default_name)
fe18ee2d 182{
257b245c 183 struct ns *default_ns;
ec31f30d 184 ns_id_t ns_id;
03aff2d8 185 ns_id_t ns_id_external;
ec31f30d 186
0cf6db21 187 frr_with_privs(&zserv_privs) {
01b9e3fd
DL
188 ns_id = zebra_ns_id_get_default();
189 }
03aff2d8
PG
190 ns_id_external = ns_map_nsid_with_external(ns_id, true);
191 ns_init_management(ns_id_external, ns_id);
736d41ad 192
257b245c
SW
193 default_ns = ns_lookup(ns_get_default_id());
194 if (!default_ns) {
195 flog_err(EC_ZEBRA_NS_NO_DEFAULT,
196 "%s: failed to find default ns", __func__);
197 exit(EXIT_FAILURE); /* This is non-recoverable */
198 }
199
84915b0a 200 /* Do any needed per-NS data structure allocation. */
257b245c
SW
201 zebra_ns_new(default_ns);
202 dzns = default_ns->info;
84915b0a 203
204 /* Register zebra VRF callbacks, create and activate default VRF. */
d62a17ae 205 zebra_vrf_init();
fe18ee2d 206
84915b0a 207 /* Default NS is activated */
03aff2d8 208 zebra_ns_enable(ns_id_external, (void **)&dzns);
fe18ee2d 209
edbc3322
PG
210 if (optional_default_name)
211 vrf_set_default_name(optional_default_name,
212 true);
213
3347430b
PG
214 if (vrf_is_backend_netns()) {
215 ns_add_hook(NS_NEW_HOOK, zebra_ns_new);
216 ns_add_hook(NS_ENABLE_HOOK, zebra_ns_enabled);
217 ns_add_hook(NS_DISABLE_HOOK, zebra_ns_disabled);
218 ns_add_hook(NS_DELETE_HOOK, zebra_ns_delete);
e27dec3c
PG
219 zebra_ns_notify_parse();
220 zebra_ns_notify_init();
3347430b 221 }
7f0ea8a4 222
d62a17ae 223 return 0;
fe18ee2d 224}
b95c1883
PG
225
226int zebra_ns_config_write(struct vty *vty, struct ns *ns)
227{
228 if (ns && ns->name != NULL)
229 vty_out(vty, " netns %s\n", ns->name);
230 return 0;
231}
07509878
PG
232
233void zebra_ns_list_walk(int (*exec_for_each_zns)(struct zebra_ns *zns,
234 void *param_in,
235 void **param_out),
236 void *param_in,
237 void **param_out)
238{
239 struct ns *ns;
240 struct zebra_ns *zns;
241 int ret;
242
243 RB_FOREACH (ns, ns_head, &ns_tree) {
244 zns = (struct zebra_ns *)ns->info;
245 if (!zns && ns->ns_id == NS_DEFAULT)
246 zns = zebra_ns_lookup(ns->ns_id);
247 if (!zns)
248 continue;
249 ret = exec_for_each_zns(zns, param_in, param_out);
250 if (ret == ZNS_WALK_STOP)
251 return;
252 }
253}