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