]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_ns.c
Merge pull request #2849 from patrasar/memory_leak_nht
[mirror_frr.git] / zebra / zebra_ns.c
1 /* zebra NS Routines
2 * Copyright (C) 2016 Cumulus Networks, Inc.
3 * Donald Sharp
4 * Copyright (C) 2017/2018 6WIND
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 *
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
21 */
22 #include "zebra.h"
23
24 #include "lib/ns.h"
25 #include "lib/vrf.h"
26 #include "lib/logicalrouter.h"
27 #include "lib/prefix.h"
28 #include "lib/memory.h"
29 #include "lib/lib_errors.h"
30
31 #include "rtadv.h"
32 #include "zebra_ns.h"
33 #include "zebra_vrf.h"
34 #include "zebra_memory.h"
35 #include "rt.h"
36 #include "zebra_vxlan.h"
37 #include "debug.h"
38 #include "zebra_netns_notify.h"
39 #include "zebra_netns_id.h"
40 #include "zebra_pbr.h"
41 #include "rib.h"
42 #include "table_manager.h"
43
44 extern struct zebra_privs_t zserv_privs;
45
46 DEFINE_MTYPE(ZEBRA, ZEBRA_NS, "Zebra Name Space")
47
48 static inline int zebra_ns_table_entry_compare(const struct zebra_ns_table *e1,
49 const struct zebra_ns_table *e2);
50
51 RB_GENERATE(zebra_ns_table_head, zebra_ns_table, zebra_ns_table_entry,
52 zebra_ns_table_entry_compare);
53
54 static struct zebra_ns *dzns;
55
56 static inline int zebra_ns_table_entry_compare(const struct zebra_ns_table *e1,
57 const struct zebra_ns_table *e2)
58 {
59 if (e1->tableid < e2->tableid)
60 return -1;
61 if (e1->tableid > e2->tableid)
62 return 1;
63 if (e1->ns_id < e2->ns_id)
64 return -1;
65 if (e1->ns_id > e2->ns_id)
66 return 1;
67 return (e1->afi - e2->afi);
68 }
69
70 static int logicalrouter_config_write(struct vty *vty);
71
72 struct zebra_ns *zebra_ns_lookup(ns_id_t ns_id)
73 {
74 if (ns_id == NS_DEFAULT)
75 return dzns;
76 struct zebra_ns *info = (struct zebra_ns *)ns_info_lookup(ns_id);
77
78 return (info == NULL) ? dzns : info;
79 }
80
81 static struct zebra_ns *zebra_ns_alloc(void)
82 {
83 return XCALLOC(MTYPE_ZEBRA_NS, sizeof(struct zebra_ns));
84 }
85
86 static int zebra_ns_new(struct ns *ns)
87 {
88 struct zebra_ns *zns;
89
90 if (IS_ZEBRA_DEBUG_EVENT)
91 zlog_info("ZNS %s with id %u (created)", ns->name, ns->ns_id);
92
93 zns = zebra_ns_alloc();
94 ns->info = zns;
95 zns->ns = ns;
96
97 /* Do any needed per-NS data structure allocation. */
98 zns->if_table = route_table_init();
99 zebra_vxlan_ns_init(zns);
100
101 return 0;
102 }
103
104 static int zebra_ns_delete(struct ns *ns)
105 {
106 struct zebra_ns *zns = (struct zebra_ns *)ns->info;
107
108 if (IS_ZEBRA_DEBUG_EVENT)
109 zlog_info("ZNS %s with id %u (deleted)", ns->name, ns->ns_id);
110 if (!zns)
111 return 0;
112 XFREE(MTYPE_ZEBRA_NS, zns);
113 return 0;
114 }
115
116 static int zebra_ns_enabled(struct ns *ns)
117 {
118 struct zebra_ns *zns = ns->info;
119
120 if (IS_ZEBRA_DEBUG_EVENT)
121 zlog_info("ZNS %s with id %u (enabled)", ns->name, ns->ns_id);
122 if (!zns)
123 return 0;
124 return zebra_ns_enable(ns->ns_id, (void **)&zns);
125 }
126
127 int zebra_ns_disabled(struct ns *ns)
128 {
129 struct zebra_ns *zns = ns->info;
130
131 if (IS_ZEBRA_DEBUG_EVENT)
132 zlog_info("ZNS %s with id %u (disabled)", ns->name, ns->ns_id);
133 if (!zns)
134 return 0;
135 return zebra_ns_disable(ns->ns_id, (void **)&zns);
136 }
137
138 /* Do global enable actions - open sockets, read kernel config etc. */
139 int zebra_ns_enable(ns_id_t ns_id, void **info)
140 {
141 struct zebra_ns *zns = (struct zebra_ns *)(*info);
142
143 zns->ns_id = ns_id;
144
145 zns->rules_hash =
146 hash_create_size(8, zebra_pbr_rules_hash_key,
147 zebra_pbr_rules_hash_equal, "Rules Hash");
148
149 zns->ipset_hash =
150 hash_create_size(8, zebra_pbr_ipset_hash_key,
151 zebra_pbr_ipset_hash_equal, "IPset Hash");
152
153 zns->ipset_entry_hash =
154 hash_create_size(8, zebra_pbr_ipset_entry_hash_key,
155 zebra_pbr_ipset_entry_hash_equal,
156 "IPset Hash Entry");
157
158 zns->iptable_hash =
159 hash_create_size(8, zebra_pbr_iptable_hash_key,
160 zebra_pbr_iptable_hash_equal,
161 "IPtable Hash Entry");
162
163 #if defined(HAVE_RTADV)
164 rtadv_init(zns);
165 #endif
166
167 kernel_init(zns);
168 interface_list(zns);
169 route_read(zns);
170
171 /* Initiate Table Manager per ZNS */
172 table_manager_enable(ns_id);
173
174 return 0;
175 }
176
177 struct route_table *zebra_ns_find_table(struct zebra_ns *zns, uint32_t tableid,
178 afi_t afi)
179 {
180 struct zebra_ns_table finder;
181 struct zebra_ns_table *znst;
182
183 memset(&finder, 0, sizeof(finder));
184 finder.afi = afi;
185 finder.tableid = tableid;
186 finder.ns_id = zns->ns_id;
187 znst = RB_FIND(zebra_ns_table_head, &zns->ns_tables, &finder);
188
189 if (znst)
190 return znst->table;
191 else
192 return NULL;
193 }
194
195 unsigned long zebra_ns_score_proto(uint8_t proto, unsigned short instance)
196 {
197 struct zebra_ns *zns;
198 struct zebra_ns_table *znst;
199 unsigned long cnt = 0;
200
201 zns = zebra_ns_lookup(NS_DEFAULT);
202
203 RB_FOREACH (znst, zebra_ns_table_head, &zns->ns_tables) {
204 if (znst->ns_id != NS_DEFAULT)
205 continue;
206 cnt += rib_score_proto_table(proto, instance, znst->table);
207 }
208 return cnt;
209 }
210
211 void zebra_ns_sweep_route(void)
212 {
213 struct zebra_ns_table *znst;
214 struct zebra_ns *zns;
215
216 zns = zebra_ns_lookup(NS_DEFAULT);
217
218 RB_FOREACH (znst, zebra_ns_table_head, &zns->ns_tables) {
219 if (znst->ns_id != NS_DEFAULT)
220 continue;
221 rib_sweep_table(znst->table);
222 }
223 }
224
225 struct route_table *zebra_ns_get_table(struct zebra_ns *zns,
226 struct zebra_vrf *zvrf, uint32_t tableid,
227 afi_t afi)
228 {
229 struct zebra_ns_table finder;
230 struct zebra_ns_table *znst;
231 rib_table_info_t *info;
232
233 memset(&finder, 0, sizeof(finder));
234 finder.afi = afi;
235 finder.tableid = tableid;
236 finder.ns_id = zns->ns_id;
237 znst = RB_FIND(zebra_ns_table_head, &zns->ns_tables, &finder);
238
239 if (znst)
240 return znst->table;
241
242 znst = XCALLOC(MTYPE_ZEBRA_NS, sizeof(*znst));
243 znst->tableid = tableid;
244 znst->afi = afi;
245 znst->ns_id = zns->ns_id;
246 znst->table =
247 (afi == AFI_IP6) ? srcdest_table_init() : route_table_init();
248
249 info = XCALLOC(MTYPE_RIB_TABLE_INFO, sizeof(*info));
250 info->zvrf = zvrf;
251 info->afi = afi;
252 info->safi = SAFI_UNICAST;
253 znst->table->info = info;
254 znst->table->cleanup = zebra_rtable_node_cleanup;
255
256 RB_INSERT(zebra_ns_table_head, &zns->ns_tables, znst);
257 return znst->table;
258 }
259
260 static void zebra_ns_free_table(struct zebra_ns_table *znst)
261 {
262 void *table_info;
263
264 rib_close_table(znst->table);
265
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);
270 }
271
272 int zebra_ns_disable(ns_id_t ns_id, void **info)
273 {
274 struct zebra_ns_table *znst, *tmp;
275 struct zebra_ns *zns = (struct zebra_ns *)(*info);
276
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);
287
288 RB_FOREACH_SAFE (znst, zebra_ns_table_head, &zns->ns_tables, tmp) {
289 if (znst->ns_id != ns_id)
290 continue;
291 RB_REMOVE(zebra_ns_table_head, &zns->ns_tables, znst);
292 zebra_ns_free_table(znst);
293 }
294
295 route_table_finish(zns->if_table);
296 zebra_vxlan_ns_disable(zns);
297 #if defined(HAVE_RTADV)
298 rtadv_terminate(zns);
299 #endif
300
301 kernel_terminate(zns);
302
303 table_manager_disable(zns->ns_id);
304
305 zns->ns_id = NS_DEFAULT;
306
307 return 0;
308 }
309
310
311 int zebra_ns_init(void)
312 {
313 ns_id_t ns_id;
314 ns_id_t ns_id_external;
315
316 dzns = zebra_ns_alloc();
317
318 frr_elevate_privs(&zserv_privs) {
319 ns_id = zebra_ns_id_get_default();
320 }
321 ns_id_external = ns_map_nsid_with_external(ns_id, true);
322 ns_init_management(ns_id_external, ns_id);
323
324 logicalrouter_init(logicalrouter_config_write);
325
326 /* Do any needed per-NS data structure allocation. */
327 dzns->if_table = route_table_init();
328 zebra_vxlan_ns_init(dzns);
329
330 /* Register zebra VRF callbacks, create and activate default VRF. */
331 zebra_vrf_init();
332
333 /* Default NS is activated */
334 zebra_ns_enable(ns_id_external, (void **)&dzns);
335
336 if (vrf_is_backend_netns()) {
337 ns_add_hook(NS_NEW_HOOK, zebra_ns_new);
338 ns_add_hook(NS_ENABLE_HOOK, zebra_ns_enabled);
339 ns_add_hook(NS_DISABLE_HOOK, zebra_ns_disabled);
340 ns_add_hook(NS_DELETE_HOOK, zebra_ns_delete);
341 zebra_ns_notify_parse();
342 zebra_ns_notify_init();
343 }
344 return 0;
345 }
346
347 static int logicalrouter_config_write(struct vty *vty)
348 {
349 struct ns *ns;
350 int write = 0;
351
352 RB_FOREACH (ns, ns_head, &ns_tree) {
353 if (ns->ns_id == NS_DEFAULT || ns->name == NULL)
354 continue;
355 vty_out(vty, "logical-router %u netns %s\n", ns->ns_id,
356 ns->name);
357 write = 1;
358 }
359 return write;
360 }
361
362 int zebra_ns_config_write(struct vty *vty, struct ns *ns)
363 {
364 if (ns && ns->name != NULL)
365 vty_out(vty, " netns %s\n", ns->name);
366 return 0;
367 }