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