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"
42 extern struct zebra_privs_t zserv_privs
;
44 DEFINE_MTYPE(ZEBRA
, ZEBRA_NS
, "Zebra Name Space")
46 static inline int zebra_ns_table_entry_compare(const struct zebra_ns_table
*e1
,
47 const struct zebra_ns_table
*e2
);
49 RB_GENERATE(zebra_ns_table_head
, zebra_ns_table
, zebra_ns_table_entry
,
50 zebra_ns_table_entry_compare
);
52 static struct zebra_ns
*dzns
;
54 static inline int zebra_ns_table_entry_compare(const struct zebra_ns_table
*e1
,
55 const struct zebra_ns_table
*e2
)
57 if (e1
->tableid
== e2
->tableid
)
58 return (e1
->afi
- e2
->afi
);
60 return e1
->tableid
- e2
->tableid
;
63 static int logicalrouter_config_write(struct vty
*vty
);
65 struct zebra_ns
*zebra_ns_lookup(ns_id_t ns_id
)
67 if (ns_id
== NS_DEFAULT
)
69 struct zebra_ns
*info
= (struct zebra_ns
*)ns_info_lookup(ns_id
);
71 return (info
== NULL
) ? dzns
: info
;
74 static struct zebra_ns
*zebra_ns_alloc(void)
76 return XCALLOC(MTYPE_ZEBRA_NS
, sizeof(struct zebra_ns
));
79 static int zebra_ns_new(struct ns
*ns
)
83 if (IS_ZEBRA_DEBUG_EVENT
)
84 zlog_info("ZNS %s with id %u (created)", ns
->name
, ns
->ns_id
);
86 zns
= zebra_ns_alloc();
90 /* Do any needed per-NS data structure allocation. */
91 zns
->if_table
= route_table_init();
92 zebra_vxlan_ns_init(zns
);
97 static int zebra_ns_delete(struct ns
*ns
)
99 struct zebra_ns
*zns
= (struct zebra_ns
*)ns
->info
;
101 if (IS_ZEBRA_DEBUG_EVENT
)
102 zlog_info("ZNS %s with id %u (deleted)", ns
->name
, ns
->ns_id
);
105 XFREE(MTYPE_ZEBRA_NS
, zns
);
109 static int zebra_ns_enabled(struct ns
*ns
)
111 struct zebra_ns
*zns
= ns
->info
;
113 if (IS_ZEBRA_DEBUG_EVENT
)
114 zlog_info("ZNS %s with id %u (enabled)", ns
->name
, ns
->ns_id
);
117 return zebra_ns_enable(ns
->ns_id
, (void **)&zns
);
120 int zebra_ns_disabled(struct ns
*ns
)
122 struct zebra_ns
*zns
= ns
->info
;
124 if (IS_ZEBRA_DEBUG_EVENT
)
125 zlog_info("ZNS %s with id %u (disabled)", ns
->name
, ns
->ns_id
);
128 return zebra_ns_disable(ns
->ns_id
, (void **)&zns
);
131 /* Do global enable actions - open sockets, read kernel config etc. */
132 int zebra_ns_enable(ns_id_t ns_id
, void **info
)
134 struct zebra_ns
*zns
= (struct zebra_ns
*)(*info
);
138 #if defined(HAVE_RTADV)
149 struct route_table
*zebra_ns_find_table(struct zebra_ns
*zns
, uint32_t tableid
,
152 struct zebra_ns_table finder
;
153 struct zebra_ns_table
*znst
;
155 memset(&finder
, 0, sizeof(finder
));
157 finder
.tableid
= tableid
;
158 znst
= RB_FIND(zebra_ns_table_head
, &zns
->ns_tables
, &finder
);
166 unsigned long zebra_ns_score_proto(u_char proto
, u_short instance
)
168 struct zebra_ns
*zns
;
169 struct zebra_ns_table
*znst
;
170 unsigned long cnt
= 0;
172 zns
= zebra_ns_lookup(NS_DEFAULT
);
174 RB_FOREACH (znst
, zebra_ns_table_head
, &zns
->ns_tables
)
175 cnt
+= rib_score_proto_table(proto
, instance
, znst
->table
);
180 void zebra_ns_sweep_route(void)
182 struct zebra_ns_table
*znst
;
183 struct zebra_ns
*zns
;
185 zns
= zebra_ns_lookup(NS_DEFAULT
);
187 RB_FOREACH (znst
, zebra_ns_table_head
, &zns
->ns_tables
)
188 rib_sweep_table(znst
->table
);
191 struct route_table
*zebra_ns_get_table(struct zebra_ns
*zns
,
192 struct zebra_vrf
*zvrf
, uint32_t tableid
,
195 struct zebra_ns_table finder
;
196 struct zebra_ns_table
*znst
;
197 rib_table_info_t
*info
;
199 memset(&finder
, 0, sizeof(finder
));
201 finder
.tableid
= tableid
;
202 znst
= RB_FIND(zebra_ns_table_head
, &zns
->ns_tables
, &finder
);
207 znst
= XCALLOC(MTYPE_ZEBRA_NS
, sizeof(*znst
));
208 znst
->tableid
= tableid
;
211 (afi
== AFI_IP6
) ? srcdest_table_init() : route_table_init();
213 info
= XCALLOC(MTYPE_RIB_TABLE_INFO
, sizeof(*info
));
216 info
->safi
= SAFI_UNICAST
;
217 znst
->table
->info
= info
;
218 znst
->table
->cleanup
= zebra_rtable_node_cleanup
;
220 RB_INSERT(zebra_ns_table_head
, &zns
->ns_tables
, znst
);
224 static void zebra_ns_free_table(struct zebra_ns_table
*znst
)
228 rib_close_table(znst
->table
);
230 table_info
= znst
->table
->info
;
231 route_table_finish(znst
->table
);
232 XFREE(MTYPE_RIB_TABLE_INFO
, table_info
);
233 XFREE(MTYPE_ZEBRA_NS
, znst
);
236 int zebra_ns_disable(ns_id_t ns_id
, void **info
)
238 struct zebra_ns_table
*znst
;
239 struct zebra_ns
*zns
= (struct zebra_ns
*)(*info
);
241 hash_clean(zns
->rules_hash
, zebra_pbr_rules_free
);
242 hash_free(zns
->rules_hash
);
243 while (!RB_EMPTY(zebra_ns_table_head
, &zns
->ns_tables
)) {
244 znst
= RB_ROOT(zebra_ns_table_head
, &zns
->ns_tables
);
246 RB_REMOVE(zebra_ns_table_head
, &zns
->ns_tables
, znst
);
247 zebra_ns_free_table(znst
);
250 route_table_finish(zns
->if_table
);
251 zebra_vxlan_ns_disable(zns
);
252 #if defined(HAVE_RTADV)
253 rtadv_terminate(zns
);
256 kernel_terminate(zns
);
258 zns
->ns_id
= NS_DEFAULT
;
264 int zebra_ns_init(void)
268 dzns
= zebra_ns_alloc();
270 if (zserv_privs
.change(ZPRIVS_RAISE
))
271 zlog_err("Can't raise privileges");
272 ns_id
= zebra_ns_id_get_default();
273 if (zserv_privs
.change(ZPRIVS_LOWER
))
274 zlog_err("Can't lower privileges");
276 ns_init_management(ns_id
);
278 logicalrouter_init(logicalrouter_config_write
);
280 /* Do any needed per-NS data structure allocation. */
281 dzns
->if_table
= route_table_init();
282 zebra_vxlan_ns_init(dzns
);
284 /* Register zebra VRF callbacks, create and activate default VRF. */
287 /* Default NS is activated */
288 zebra_ns_enable(ns_id
, (void **)&dzns
);
291 hash_create_size(8, zebra_pbr_rules_hash_key
,
292 zebra_pbr_rules_hash_equal
, "Rules Hash");
293 if (vrf_is_backend_netns()) {
294 ns_add_hook(NS_NEW_HOOK
, zebra_ns_new
);
295 ns_add_hook(NS_ENABLE_HOOK
, zebra_ns_enabled
);
296 ns_add_hook(NS_DISABLE_HOOK
, zebra_ns_disabled
);
297 ns_add_hook(NS_DELETE_HOOK
, zebra_ns_delete
);
298 zebra_ns_notify_parse();
299 zebra_ns_notify_init();
304 static int logicalrouter_config_write(struct vty
*vty
)
309 RB_FOREACH (ns
, ns_head
, &ns_tree
) {
310 if (ns
->ns_id
== NS_DEFAULT
|| ns
->name
== NULL
)
312 vty_out(vty
, "logical-router %u netns %s\n", ns
->ns_id
,
319 int zebra_ns_config_write(struct vty
*vty
, struct ns
*ns
)
321 if (ns
&& ns
->name
!= NULL
)
322 vty_out(vty
, " netns %s\n", ns
->name
);