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"
29 #include "lib/lib_errors.h"
33 #include "zebra_vrf.h"
34 #include "zebra_memory.h"
36 #include "zebra_vxlan.h"
38 #include "zebra_netns_notify.h"
39 #include "zebra_netns_id.h"
40 #include "zebra_pbr.h"
42 #include "table_manager.h"
44 extern struct zebra_privs_t zserv_privs
;
46 DEFINE_MTYPE(ZEBRA
, ZEBRA_NS
, "Zebra Name Space")
48 static inline int zebra_ns_table_entry_compare(const struct zebra_ns_table
*e1
,
49 const struct zebra_ns_table
*e2
);
51 RB_GENERATE(zebra_ns_table_head
, zebra_ns_table
, zebra_ns_table_entry
,
52 zebra_ns_table_entry_compare
);
54 static struct zebra_ns
*dzns
;
56 static inline int zebra_ns_table_entry_compare(const struct zebra_ns_table
*e1
,
57 const struct zebra_ns_table
*e2
)
59 if (e1
->tableid
< e2
->tableid
)
61 if (e1
->tableid
> e2
->tableid
)
63 if (e1
->ns_id
< e2
->ns_id
)
65 if (e1
->ns_id
> e2
->ns_id
)
67 return (e1
->afi
- e2
->afi
);
70 static int logicalrouter_config_write(struct vty
*vty
);
72 struct zebra_ns
*zebra_ns_lookup(ns_id_t ns_id
)
74 if (ns_id
== NS_DEFAULT
)
76 struct zebra_ns
*info
= (struct zebra_ns
*)ns_info_lookup(ns_id
);
78 return (info
== NULL
) ? dzns
: info
;
81 static struct zebra_ns
*zebra_ns_alloc(void)
83 return XCALLOC(MTYPE_ZEBRA_NS
, sizeof(struct zebra_ns
));
86 static int zebra_ns_new(struct ns
*ns
)
90 if (IS_ZEBRA_DEBUG_EVENT
)
91 zlog_info("ZNS %s with id %u (created)", ns
->name
, ns
->ns_id
);
93 zns
= zebra_ns_alloc();
97 /* Do any needed per-NS data structure allocation. */
98 zns
->if_table
= route_table_init();
99 zebra_vxlan_ns_init(zns
);
104 static int zebra_ns_delete(struct ns
*ns
)
106 struct zebra_ns
*zns
= (struct zebra_ns
*)ns
->info
;
108 if (IS_ZEBRA_DEBUG_EVENT
)
109 zlog_info("ZNS %s with id %u (deleted)", ns
->name
, ns
->ns_id
);
112 XFREE(MTYPE_ZEBRA_NS
, zns
);
116 static int zebra_ns_enabled(struct ns
*ns
)
118 struct zebra_ns
*zns
= ns
->info
;
120 if (IS_ZEBRA_DEBUG_EVENT
)
121 zlog_info("ZNS %s with id %u (enabled)", ns
->name
, ns
->ns_id
);
124 return zebra_ns_enable(ns
->ns_id
, (void **)&zns
);
127 int zebra_ns_disabled(struct ns
*ns
)
129 struct zebra_ns
*zns
= ns
->info
;
131 if (IS_ZEBRA_DEBUG_EVENT
)
132 zlog_info("ZNS %s with id %u (disabled)", ns
->name
, ns
->ns_id
);
135 return zebra_ns_disable(ns
->ns_id
, (void **)&zns
);
138 /* Do global enable actions - open sockets, read kernel config etc. */
139 int zebra_ns_enable(ns_id_t ns_id
, void **info
)
141 struct zebra_ns
*zns
= (struct zebra_ns
*)(*info
);
146 hash_create_size(8, zebra_pbr_rules_hash_key
,
147 zebra_pbr_rules_hash_equal
, "Rules Hash");
150 hash_create_size(8, zebra_pbr_ipset_hash_key
,
151 zebra_pbr_ipset_hash_equal
, "IPset Hash");
153 zns
->ipset_entry_hash
=
154 hash_create_size(8, zebra_pbr_ipset_entry_hash_key
,
155 zebra_pbr_ipset_entry_hash_equal
,
159 hash_create_size(8, zebra_pbr_iptable_hash_key
,
160 zebra_pbr_iptable_hash_equal
,
161 "IPtable Hash Entry");
163 #if defined(HAVE_RTADV)
171 /* Initiate Table Manager per ZNS */
172 table_manager_enable(ns_id
);
177 struct route_table
*zebra_ns_find_table(struct zebra_ns
*zns
, uint32_t tableid
,
180 struct zebra_ns_table finder
;
181 struct zebra_ns_table
*znst
;
183 memset(&finder
, 0, sizeof(finder
));
185 finder
.tableid
= tableid
;
186 finder
.ns_id
= zns
->ns_id
;
187 znst
= RB_FIND(zebra_ns_table_head
, &zns
->ns_tables
, &finder
);
195 unsigned long zebra_ns_score_proto(uint8_t proto
, unsigned short instance
)
197 struct zebra_ns
*zns
;
198 struct zebra_ns_table
*znst
;
199 unsigned long cnt
= 0;
201 zns
= zebra_ns_lookup(NS_DEFAULT
);
203 RB_FOREACH (znst
, zebra_ns_table_head
, &zns
->ns_tables
) {
204 if (znst
->ns_id
!= NS_DEFAULT
)
206 cnt
+= rib_score_proto_table(proto
, instance
, znst
->table
);
211 void zebra_ns_sweep_route(void)
213 struct zebra_ns_table
*znst
;
214 struct zebra_ns
*zns
;
216 zns
= zebra_ns_lookup(NS_DEFAULT
);
218 RB_FOREACH (znst
, zebra_ns_table_head
, &zns
->ns_tables
) {
219 if (znst
->ns_id
!= NS_DEFAULT
)
221 rib_sweep_table(znst
->table
);
225 struct route_table
*zebra_ns_get_table(struct zebra_ns
*zns
,
226 struct zebra_vrf
*zvrf
, uint32_t tableid
,
229 struct zebra_ns_table finder
;
230 struct zebra_ns_table
*znst
;
231 rib_table_info_t
*info
;
233 memset(&finder
, 0, sizeof(finder
));
235 finder
.tableid
= tableid
;
236 finder
.ns_id
= zns
->ns_id
;
237 znst
= RB_FIND(zebra_ns_table_head
, &zns
->ns_tables
, &finder
);
242 znst
= XCALLOC(MTYPE_ZEBRA_NS
, sizeof(*znst
));
243 znst
->tableid
= tableid
;
245 znst
->ns_id
= zns
->ns_id
;
247 (afi
== AFI_IP6
) ? srcdest_table_init() : route_table_init();
249 info
= XCALLOC(MTYPE_RIB_TABLE_INFO
, sizeof(*info
));
252 info
->safi
= SAFI_UNICAST
;
253 znst
->table
->info
= info
;
254 znst
->table
->cleanup
= zebra_rtable_node_cleanup
;
256 RB_INSERT(zebra_ns_table_head
, &zns
->ns_tables
, znst
);
260 static void zebra_ns_free_table(struct zebra_ns_table
*znst
)
264 rib_close_table(znst
->table
);
266 table_info
= znst
->table
->info
;
267 route_table_finish(znst
->table
);
268 XFREE(MTYPE_RIB_TABLE_INFO
, table_info
);
269 XFREE(MTYPE_ZEBRA_NS
, znst
);
272 int zebra_ns_disable(ns_id_t ns_id
, void **info
)
274 struct zebra_ns_table
*znst
, *tmp
;
275 struct zebra_ns
*zns
= (struct zebra_ns
*)(*info
);
277 hash_clean(zns
->rules_hash
, zebra_pbr_rules_free
);
278 hash_free(zns
->rules_hash
);
279 hash_clean(zns
->ipset_entry_hash
,
280 zebra_pbr_ipset_entry_free
),
281 hash_clean(zns
->ipset_hash
, zebra_pbr_ipset_free
);
282 hash_free(zns
->ipset_hash
);
283 hash_free(zns
->ipset_entry_hash
);
284 hash_clean(zns
->iptable_hash
,
285 zebra_pbr_iptable_free
);
286 hash_free(zns
->iptable_hash
);
288 RB_FOREACH_SAFE (znst
, zebra_ns_table_head
, &zns
->ns_tables
, tmp
) {
289 if (znst
->ns_id
!= ns_id
)
291 RB_REMOVE(zebra_ns_table_head
, &zns
->ns_tables
, znst
);
292 zebra_ns_free_table(znst
);
295 route_table_finish(zns
->if_table
);
296 zebra_vxlan_ns_disable(zns
);
297 #if defined(HAVE_RTADV)
298 rtadv_terminate(zns
);
301 kernel_terminate(zns
);
303 table_manager_disable(zns
->ns_id
);
305 zns
->ns_id
= NS_DEFAULT
;
311 int zebra_ns_init(void)
314 ns_id_t ns_id_external
;
316 dzns
= zebra_ns_alloc();
318 if (zserv_privs
.change(ZPRIVS_RAISE
))
319 flog_err(LIB_ERR_PRIVILEGES
, "Can't raise privileges");
320 ns_id
= zebra_ns_id_get_default();
321 if (zserv_privs
.change(ZPRIVS_LOWER
))
322 flog_err(LIB_ERR_PRIVILEGES
, "Can't lower privileges");
323 ns_id_external
= ns_map_nsid_with_external(ns_id
, true);
324 ns_init_management(ns_id_external
, ns_id
);
326 logicalrouter_init(logicalrouter_config_write
);
328 /* Do any needed per-NS data structure allocation. */
329 dzns
->if_table
= route_table_init();
330 zebra_vxlan_ns_init(dzns
);
332 /* Register zebra VRF callbacks, create and activate default VRF. */
335 /* Default NS is activated */
336 zebra_ns_enable(ns_id_external
, (void **)&dzns
);
338 if (vrf_is_backend_netns()) {
339 ns_add_hook(NS_NEW_HOOK
, zebra_ns_new
);
340 ns_add_hook(NS_ENABLE_HOOK
, zebra_ns_enabled
);
341 ns_add_hook(NS_DISABLE_HOOK
, zebra_ns_disabled
);
342 ns_add_hook(NS_DELETE_HOOK
, zebra_ns_delete
);
343 zebra_ns_notify_parse();
344 zebra_ns_notify_init();
349 static int logicalrouter_config_write(struct vty
*vty
)
354 RB_FOREACH (ns
, ns_head
, &ns_tree
) {
355 if (ns
->ns_id
== NS_DEFAULT
|| ns
->name
== NULL
)
357 vty_out(vty
, "logical-router %u netns %s\n", ns
->ns_id
,
364 int zebra_ns_config_write(struct vty
*vty
, struct ns
*ns
)
366 if (ns
&& ns
->name
!= NULL
)
367 vty_out(vty
, " netns %s\n", ns
->name
);