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 inline int zebra_ns_table_entry_compare(const struct zebra_ns_table
*e1
,
48 const struct zebra_ns_table
*e2
);
50 RB_GENERATE(zebra_ns_table_head
, zebra_ns_table
, zebra_ns_table_entry
,
51 zebra_ns_table_entry_compare
);
53 static struct zebra_ns
*dzns
;
55 static inline int zebra_ns_table_entry_compare(const struct zebra_ns_table
*e1
,
56 const struct zebra_ns_table
*e2
)
58 if (e1
->tableid
< e2
->tableid
)
60 if (e1
->tableid
> e2
->tableid
)
62 if (e1
->ns_id
< e2
->ns_id
)
64 if (e1
->ns_id
> e2
->ns_id
)
66 return (e1
->afi
- e2
->afi
);
69 static int logicalrouter_config_write(struct vty
*vty
);
71 struct zebra_ns
*zebra_ns_lookup(ns_id_t ns_id
)
73 if (ns_id
== NS_DEFAULT
)
75 struct zebra_ns
*info
= (struct zebra_ns
*)ns_info_lookup(ns_id
);
77 return (info
== NULL
) ? dzns
: info
;
80 static struct zebra_ns
*zebra_ns_alloc(void)
82 return XCALLOC(MTYPE_ZEBRA_NS
, sizeof(struct zebra_ns
));
85 static int zebra_ns_new(struct ns
*ns
)
89 if (IS_ZEBRA_DEBUG_EVENT
)
90 zlog_info("ZNS %s with id %u (created)", ns
->name
, ns
->ns_id
);
92 zns
= zebra_ns_alloc();
96 /* Do any needed per-NS data structure allocation. */
97 zns
->if_table
= route_table_init();
98 zebra_vxlan_ns_init(zns
);
103 static int zebra_ns_delete(struct ns
*ns
)
105 struct zebra_ns
*zns
= (struct zebra_ns
*)ns
->info
;
107 if (IS_ZEBRA_DEBUG_EVENT
)
108 zlog_info("ZNS %s with id %u (deleted)", ns
->name
, ns
->ns_id
);
111 XFREE(MTYPE_ZEBRA_NS
, zns
);
115 static int zebra_ns_enabled(struct ns
*ns
)
117 struct zebra_ns
*zns
= ns
->info
;
119 if (IS_ZEBRA_DEBUG_EVENT
)
120 zlog_info("ZNS %s with id %u (enabled)", ns
->name
, ns
->ns_id
);
123 return zebra_ns_enable(ns
->ns_id
, (void **)&zns
);
126 int zebra_ns_disabled(struct ns
*ns
)
128 struct zebra_ns
*zns
= ns
->info
;
130 if (IS_ZEBRA_DEBUG_EVENT
)
131 zlog_info("ZNS %s with id %u (disabled)", ns
->name
, ns
->ns_id
);
134 return zebra_ns_disable(ns
->ns_id
, (void **)&zns
);
137 /* Do global enable actions - open sockets, read kernel config etc. */
138 int zebra_ns_enable(ns_id_t ns_id
, void **info
)
140 struct zebra_ns
*zns
= (struct zebra_ns
*)(*info
);
145 hash_create_size(8, zebra_pbr_rules_hash_key
,
146 zebra_pbr_rules_hash_equal
, "Rules Hash");
149 hash_create_size(8, zebra_pbr_ipset_hash_key
,
150 zebra_pbr_ipset_hash_equal
, "IPset Hash");
152 zns
->ipset_entry_hash
=
153 hash_create_size(8, zebra_pbr_ipset_entry_hash_key
,
154 zebra_pbr_ipset_entry_hash_equal
,
158 hash_create_size(8, zebra_pbr_iptable_hash_key
,
159 zebra_pbr_iptable_hash_equal
,
160 "IPtable Hash Entry");
162 #if defined(HAVE_RTADV)
170 /* Initiate Table Manager per ZNS */
171 table_manager_enable(ns_id
);
176 struct route_table
*zebra_ns_find_table(struct zebra_ns
*zns
, uint32_t tableid
,
179 struct zebra_ns_table finder
;
180 struct zebra_ns_table
*znst
;
182 memset(&finder
, 0, sizeof(finder
));
184 finder
.tableid
= tableid
;
185 finder
.ns_id
= zns
->ns_id
;
186 znst
= RB_FIND(zebra_ns_table_head
, &zns
->ns_tables
, &finder
);
194 unsigned long zebra_ns_score_proto(uint8_t proto
, unsigned short instance
)
196 struct zebra_ns
*zns
;
197 struct zebra_ns_table
*znst
;
198 unsigned long cnt
= 0;
200 zns
= zebra_ns_lookup(NS_DEFAULT
);
202 RB_FOREACH (znst
, zebra_ns_table_head
, &zns
->ns_tables
) {
203 if (znst
->ns_id
!= NS_DEFAULT
)
205 cnt
+= rib_score_proto_table(proto
, instance
, znst
->table
);
210 void zebra_ns_sweep_route(void)
212 struct zebra_ns_table
*znst
;
213 struct zebra_ns
*zns
;
215 zns
= zebra_ns_lookup(NS_DEFAULT
);
217 RB_FOREACH (znst
, zebra_ns_table_head
, &zns
->ns_tables
) {
218 if (znst
->ns_id
!= NS_DEFAULT
)
220 rib_sweep_table(znst
->table
);
224 struct route_table
*zebra_ns_get_table(struct zebra_ns
*zns
,
225 struct zebra_vrf
*zvrf
, uint32_t tableid
,
228 struct zebra_ns_table finder
;
229 struct zebra_ns_table
*znst
;
230 rib_table_info_t
*info
;
232 memset(&finder
, 0, sizeof(finder
));
234 finder
.tableid
= tableid
;
235 finder
.ns_id
= zns
->ns_id
;
236 znst
= RB_FIND(zebra_ns_table_head
, &zns
->ns_tables
, &finder
);
241 znst
= XCALLOC(MTYPE_ZEBRA_NS
, sizeof(*znst
));
242 znst
->tableid
= tableid
;
244 znst
->ns_id
= zns
->ns_id
;
246 (afi
== AFI_IP6
) ? srcdest_table_init() : route_table_init();
248 info
= XCALLOC(MTYPE_RIB_TABLE_INFO
, sizeof(*info
));
251 info
->safi
= SAFI_UNICAST
;
252 znst
->table
->info
= info
;
253 znst
->table
->cleanup
= zebra_rtable_node_cleanup
;
255 RB_INSERT(zebra_ns_table_head
, &zns
->ns_tables
, znst
);
259 static void zebra_ns_free_table(struct zebra_ns_table
*znst
)
263 rib_close_table(znst
->table
);
265 table_info
= znst
->table
->info
;
266 route_table_finish(znst
->table
);
267 XFREE(MTYPE_RIB_TABLE_INFO
, table_info
);
268 XFREE(MTYPE_ZEBRA_NS
, znst
);
271 int zebra_ns_disable(ns_id_t ns_id
, void **info
)
273 struct zebra_ns_table
*znst
, *tmp
;
274 struct zebra_ns
*zns
= (struct zebra_ns
*)(*info
);
276 hash_clean(zns
->rules_hash
, zebra_pbr_rules_free
);
277 hash_free(zns
->rules_hash
);
278 hash_clean(zns
->ipset_entry_hash
, zebra_pbr_ipset_entry_free
);
279 hash_clean(zns
->ipset_hash
, zebra_pbr_ipset_free
);
280 hash_free(zns
->ipset_hash
);
281 hash_free(zns
->ipset_entry_hash
);
282 hash_clean(zns
->iptable_hash
,
283 zebra_pbr_iptable_free
);
284 hash_free(zns
->iptable_hash
);
286 RB_FOREACH_SAFE (znst
, zebra_ns_table_head
, &zns
->ns_tables
, tmp
) {
287 if (znst
->ns_id
!= ns_id
)
289 RB_REMOVE(zebra_ns_table_head
, &zns
->ns_tables
, znst
);
290 zebra_ns_free_table(znst
);
293 route_table_finish(zns
->if_table
);
294 zebra_vxlan_ns_disable(zns
);
295 #if defined(HAVE_RTADV)
296 rtadv_terminate(zns
);
299 kernel_terminate(zns
);
301 table_manager_disable(zns
->ns_id
);
303 zns
->ns_id
= NS_DEFAULT
;
309 int zebra_ns_init(void)
312 ns_id_t ns_id_external
;
314 dzns
= zebra_ns_alloc();
316 frr_elevate_privs(&zserv_privs
) {
317 ns_id
= zebra_ns_id_get_default();
319 ns_id_external
= ns_map_nsid_with_external(ns_id
, true);
320 ns_init_management(ns_id_external
, ns_id
);
322 logicalrouter_init(logicalrouter_config_write
);
324 /* Do any needed per-NS data structure allocation. */
325 dzns
->if_table
= route_table_init();
326 zebra_vxlan_ns_init(dzns
);
328 /* Register zebra VRF callbacks, create and activate default VRF. */
331 /* Default NS is activated */
332 zebra_ns_enable(ns_id_external
, (void **)&dzns
);
334 if (vrf_is_backend_netns()) {
335 ns_add_hook(NS_NEW_HOOK
, zebra_ns_new
);
336 ns_add_hook(NS_ENABLE_HOOK
, zebra_ns_enabled
);
337 ns_add_hook(NS_DISABLE_HOOK
, zebra_ns_disabled
);
338 ns_add_hook(NS_DELETE_HOOK
, zebra_ns_delete
);
339 zebra_ns_notify_parse();
340 zebra_ns_notify_init();
345 static int logicalrouter_config_write(struct vty
*vty
)
350 RB_FOREACH (ns
, ns_head
, &ns_tree
) {
351 if (ns
->ns_id
== NS_DEFAULT
|| ns
->name
== NULL
)
353 vty_out(vty
, "logical-router %u netns %s\n", ns
->ns_id
,
360 int zebra_ns_config_write(struct vty
*vty
, struct ns
*ns
)
362 if (ns
&& ns
->name
!= NULL
)
363 vty_out(vty
, " netns %s\n", ns
->name
);