]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_ns.c
Merge pull request #2035 from vincentbernat/fix/no-etag-esi-ignore
[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 (e1->afi - e2->afi);
60
61 return e1->tableid - e2->tableid;
62 }
63
64 static int logicalrouter_config_write(struct vty *vty);
65
66 struct zebra_ns *zebra_ns_lookup(ns_id_t ns_id)
67 {
68 if (ns_id == NS_DEFAULT)
69 return dzns;
70 struct zebra_ns *info = (struct zebra_ns *)ns_info_lookup(ns_id);
71
72 return (info == NULL) ? dzns : info;
73 }
74
75 static struct zebra_ns *zebra_ns_alloc(void)
76 {
77 return XCALLOC(MTYPE_ZEBRA_NS, sizeof(struct zebra_ns));
78 }
79
80 static int zebra_ns_new(struct ns *ns)
81 {
82 struct zebra_ns *zns;
83
84 if (IS_ZEBRA_DEBUG_EVENT)
85 zlog_info("ZNS %s with id %u (created)", ns->name, ns->ns_id);
86
87 zns = zebra_ns_alloc();
88 ns->info = zns;
89 zns->ns = ns;
90
91 /* Do any needed per-NS data structure allocation. */
92 zns->if_table = route_table_init();
93 zebra_vxlan_ns_init(zns);
94
95 return 0;
96 }
97
98 static int zebra_ns_delete(struct ns *ns)
99 {
100 struct zebra_ns *zns = (struct zebra_ns *)ns->info;
101
102 if (IS_ZEBRA_DEBUG_EVENT)
103 zlog_info("ZNS %s with id %u (deleted)", ns->name, ns->ns_id);
104 if (!zns)
105 return 0;
106 XFREE(MTYPE_ZEBRA_NS, zns);
107 return 0;
108 }
109
110 static int zebra_ns_enabled(struct ns *ns)
111 {
112 struct zebra_ns *zns = ns->info;
113
114 if (IS_ZEBRA_DEBUG_EVENT)
115 zlog_info("ZNS %s with id %u (enabled)", ns->name, ns->ns_id);
116 if (!zns)
117 return 0;
118 return zebra_ns_enable(ns->ns_id, (void **)&zns);
119 }
120
121 int zebra_ns_disabled(struct ns *ns)
122 {
123 struct zebra_ns *zns = ns->info;
124
125 if (IS_ZEBRA_DEBUG_EVENT)
126 zlog_info("ZNS %s with id %u (disabled)", ns->name, ns->ns_id);
127 if (!zns)
128 return 0;
129 return zebra_ns_disable(ns->ns_id, (void **)&zns);
130 }
131
132 /* Do global enable actions - open sockets, read kernel config etc. */
133 int zebra_ns_enable(ns_id_t ns_id, void **info)
134 {
135 struct zebra_ns *zns = (struct zebra_ns *)(*info);
136
137 zns->ns_id = ns_id;
138
139 zns->rules_hash =
140 hash_create_size(8, zebra_pbr_rules_hash_key,
141 zebra_pbr_rules_hash_equal, "Rules Hash");
142
143 zns->ipset_hash =
144 hash_create_size(8, zebra_pbr_ipset_hash_key,
145 zebra_pbr_ipset_hash_equal, "IPset Hash");
146
147 zns->ipset_entry_hash =
148 hash_create_size(8, zebra_pbr_ipset_entry_hash_key,
149 zebra_pbr_ipset_entry_hash_equal,
150 "IPset Hash Entry");
151
152 zns->iptable_hash =
153 hash_create_size(8, zebra_pbr_iptable_hash_key,
154 zebra_pbr_iptable_hash_equal,
155 "IPtable Hash Entry");
156
157 #if defined(HAVE_RTADV)
158 rtadv_init(zns);
159 #endif
160
161 kernel_init(zns);
162 interface_list(zns);
163 route_read(zns);
164
165 /* Initiate Table Manager per ZNS */
166 table_manager_enable(ns_id);
167
168 return 0;
169 }
170
171 struct route_table *zebra_ns_find_table(struct zebra_ns *zns, uint32_t tableid,
172 afi_t afi)
173 {
174 struct zebra_ns_table finder;
175 struct zebra_ns_table *znst;
176
177 memset(&finder, 0, sizeof(finder));
178 finder.afi = afi;
179 finder.tableid = tableid;
180 znst = RB_FIND(zebra_ns_table_head, &zns->ns_tables, &finder);
181
182 if (znst)
183 return znst->table;
184 else
185 return NULL;
186 }
187
188 unsigned long zebra_ns_score_proto(uint8_t proto, unsigned short instance)
189 {
190 struct zebra_ns *zns;
191 struct zebra_ns_table *znst;
192 unsigned long cnt = 0;
193
194 zns = zebra_ns_lookup(NS_DEFAULT);
195
196 RB_FOREACH (znst, zebra_ns_table_head, &zns->ns_tables)
197 cnt += rib_score_proto_table(proto, instance, znst->table);
198
199 return cnt;
200 }
201
202 void zebra_ns_sweep_route(void)
203 {
204 struct zebra_ns_table *znst;
205 struct zebra_ns *zns;
206
207 zns = zebra_ns_lookup(NS_DEFAULT);
208
209 RB_FOREACH (znst, zebra_ns_table_head, &zns->ns_tables)
210 rib_sweep_table(znst->table);
211 }
212
213 struct route_table *zebra_ns_get_table(struct zebra_ns *zns,
214 struct zebra_vrf *zvrf, uint32_t tableid,
215 afi_t afi)
216 {
217 struct zebra_ns_table finder;
218 struct zebra_ns_table *znst;
219 rib_table_info_t *info;
220
221 memset(&finder, 0, sizeof(finder));
222 finder.afi = afi;
223 finder.tableid = tableid;
224 znst = RB_FIND(zebra_ns_table_head, &zns->ns_tables, &finder);
225
226 if (znst)
227 return znst->table;
228
229 znst = XCALLOC(MTYPE_ZEBRA_NS, sizeof(*znst));
230 znst->tableid = tableid;
231 znst->afi = afi;
232 znst->table =
233 (afi == AFI_IP6) ? srcdest_table_init() : route_table_init();
234
235 info = XCALLOC(MTYPE_RIB_TABLE_INFO, sizeof(*info));
236 info->zvrf = zvrf;
237 info->afi = afi;
238 info->safi = SAFI_UNICAST;
239 znst->table->info = info;
240 znst->table->cleanup = zebra_rtable_node_cleanup;
241
242 RB_INSERT(zebra_ns_table_head, &zns->ns_tables, znst);
243 return znst->table;
244 }
245
246 static void zebra_ns_free_table(struct zebra_ns_table *znst)
247 {
248 void *table_info;
249
250 rib_close_table(znst->table);
251
252 table_info = znst->table->info;
253 route_table_finish(znst->table);
254 XFREE(MTYPE_RIB_TABLE_INFO, table_info);
255 XFREE(MTYPE_ZEBRA_NS, znst);
256 }
257
258 int zebra_ns_disable(ns_id_t ns_id, void **info)
259 {
260 struct zebra_ns_table *znst;
261 struct zebra_ns *zns = (struct zebra_ns *)(*info);
262
263 hash_clean(zns->rules_hash, zebra_pbr_rules_free);
264 hash_free(zns->rules_hash);
265 hash_clean(zns->ipset_hash, zebra_pbr_ipset_free);
266 hash_free(zns->ipset_hash);
267 hash_clean(zns->ipset_entry_hash,
268 zebra_pbr_ipset_entry_free),
269 hash_free(zns->ipset_entry_hash);
270 hash_clean(zns->iptable_hash,
271 zebra_pbr_iptable_free);
272 hash_free(zns->iptable_hash);
273
274 while (!RB_EMPTY(zebra_ns_table_head, &zns->ns_tables)) {
275 znst = RB_ROOT(zebra_ns_table_head, &zns->ns_tables);
276
277 RB_REMOVE(zebra_ns_table_head, &zns->ns_tables, znst);
278 zebra_ns_free_table(znst);
279 }
280
281 route_table_finish(zns->if_table);
282 zebra_vxlan_ns_disable(zns);
283 #if defined(HAVE_RTADV)
284 rtadv_terminate(zns);
285 #endif
286
287 kernel_terminate(zns);
288
289 table_manager_disable(zns->ns_id);
290
291 zns->ns_id = NS_DEFAULT;
292
293 return 0;
294 }
295
296
297 int zebra_ns_init(void)
298 {
299 ns_id_t ns_id;
300 ns_id_t ns_id_external;
301
302 dzns = zebra_ns_alloc();
303
304 if (zserv_privs.change(ZPRIVS_RAISE))
305 zlog_err("Can't raise privileges");
306 ns_id = zebra_ns_id_get_default();
307 if (zserv_privs.change(ZPRIVS_LOWER))
308 zlog_err("Can't lower privileges");
309 ns_id_external = ns_map_nsid_with_external(ns_id, true);
310 ns_init_management(ns_id_external, ns_id);
311
312 logicalrouter_init(logicalrouter_config_write);
313
314 /* Do any needed per-NS data structure allocation. */
315 dzns->if_table = route_table_init();
316 zebra_vxlan_ns_init(dzns);
317
318 /* Register zebra VRF callbacks, create and activate default VRF. */
319 zebra_vrf_init();
320
321 /* Default NS is activated */
322 zebra_ns_enable(ns_id_external, (void **)&dzns);
323
324 if (vrf_is_backend_netns()) {
325 ns_add_hook(NS_NEW_HOOK, zebra_ns_new);
326 ns_add_hook(NS_ENABLE_HOOK, zebra_ns_enabled);
327 ns_add_hook(NS_DISABLE_HOOK, zebra_ns_disabled);
328 ns_add_hook(NS_DELETE_HOOK, zebra_ns_delete);
329 zebra_ns_notify_parse();
330 zebra_ns_notify_init();
331 }
332 return 0;
333 }
334
335 static int logicalrouter_config_write(struct vty *vty)
336 {
337 struct ns *ns;
338 int write = 0;
339
340 RB_FOREACH (ns, ns_head, &ns_tree) {
341 if (ns->ns_id == NS_DEFAULT || ns->name == NULL)
342 continue;
343 vty_out(vty, "logical-router %u netns %s\n", ns->ns_id,
344 ns->name);
345 write = 1;
346 }
347 return write;
348 }
349
350 int zebra_ns_config_write(struct vty *vty, struct ns *ns)
351 {
352 if (ns && ns->name != NULL)
353 vty_out(vty, " netns %s\n", ns->name);
354 return 0;
355 }