]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_ns.c
Merge pull request #1973 from donaldsharp/static_nh_vrf
[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 #if defined(HAVE_RTADV)
144 rtadv_init(zns);
145 #endif
146
147 kernel_init(zns);
148 interface_list(zns);
149 route_read(zns);
150
151 /* Initiate Table Manager per ZNS */
152 table_manager_enable(ns_id);
153
154 return 0;
155 }
156
157 struct route_table *zebra_ns_find_table(struct zebra_ns *zns, uint32_t tableid,
158 afi_t afi)
159 {
160 struct zebra_ns_table finder;
161 struct zebra_ns_table *znst;
162
163 memset(&finder, 0, sizeof(finder));
164 finder.afi = afi;
165 finder.tableid = tableid;
166 znst = RB_FIND(zebra_ns_table_head, &zns->ns_tables, &finder);
167
168 if (znst)
169 return znst->table;
170 else
171 return NULL;
172 }
173
174 unsigned long zebra_ns_score_proto(uint8_t proto, unsigned short instance)
175 {
176 struct zebra_ns *zns;
177 struct zebra_ns_table *znst;
178 unsigned long cnt = 0;
179
180 zns = zebra_ns_lookup(NS_DEFAULT);
181
182 RB_FOREACH (znst, zebra_ns_table_head, &zns->ns_tables)
183 cnt += rib_score_proto_table(proto, instance, znst->table);
184
185 return cnt;
186 }
187
188 void zebra_ns_sweep_route(void)
189 {
190 struct zebra_ns_table *znst;
191 struct zebra_ns *zns;
192
193 zns = zebra_ns_lookup(NS_DEFAULT);
194
195 RB_FOREACH (znst, zebra_ns_table_head, &zns->ns_tables)
196 rib_sweep_table(znst->table);
197 }
198
199 struct route_table *zebra_ns_get_table(struct zebra_ns *zns,
200 struct zebra_vrf *zvrf, uint32_t tableid,
201 afi_t afi)
202 {
203 struct zebra_ns_table finder;
204 struct zebra_ns_table *znst;
205 rib_table_info_t *info;
206
207 memset(&finder, 0, sizeof(finder));
208 finder.afi = afi;
209 finder.tableid = tableid;
210 znst = RB_FIND(zebra_ns_table_head, &zns->ns_tables, &finder);
211
212 if (znst)
213 return znst->table;
214
215 znst = XCALLOC(MTYPE_ZEBRA_NS, sizeof(*znst));
216 znst->tableid = tableid;
217 znst->afi = afi;
218 znst->table =
219 (afi == AFI_IP6) ? srcdest_table_init() : route_table_init();
220
221 info = XCALLOC(MTYPE_RIB_TABLE_INFO, sizeof(*info));
222 info->zvrf = zvrf;
223 info->afi = afi;
224 info->safi = SAFI_UNICAST;
225 znst->table->info = info;
226 znst->table->cleanup = zebra_rtable_node_cleanup;
227
228 RB_INSERT(zebra_ns_table_head, &zns->ns_tables, znst);
229 return znst->table;
230 }
231
232 static void zebra_ns_free_table(struct zebra_ns_table *znst)
233 {
234 void *table_info;
235
236 rib_close_table(znst->table);
237
238 table_info = znst->table->info;
239 route_table_finish(znst->table);
240 XFREE(MTYPE_RIB_TABLE_INFO, table_info);
241 XFREE(MTYPE_ZEBRA_NS, znst);
242 }
243
244 int zebra_ns_disable(ns_id_t ns_id, void **info)
245 {
246 struct zebra_ns_table *znst;
247 struct zebra_ns *zns = (struct zebra_ns *)(*info);
248
249 hash_clean(zns->rules_hash, zebra_pbr_rules_free);
250 hash_free(zns->rules_hash);
251 while (!RB_EMPTY(zebra_ns_table_head, &zns->ns_tables)) {
252 znst = RB_ROOT(zebra_ns_table_head, &zns->ns_tables);
253
254 RB_REMOVE(zebra_ns_table_head, &zns->ns_tables, znst);
255 zebra_ns_free_table(znst);
256 }
257
258 route_table_finish(zns->if_table);
259 zebra_vxlan_ns_disable(zns);
260 #if defined(HAVE_RTADV)
261 rtadv_terminate(zns);
262 #endif
263
264 kernel_terminate(zns);
265
266 table_manager_disable(zns->ns_id);
267
268 zns->ns_id = NS_DEFAULT;
269
270 return 0;
271 }
272
273
274 int zebra_ns_init(void)
275 {
276 ns_id_t ns_id;
277
278 dzns = zebra_ns_alloc();
279
280 if (zserv_privs.change(ZPRIVS_RAISE))
281 zlog_err("Can't raise privileges");
282 ns_id = zebra_ns_id_get_default();
283 if (zserv_privs.change(ZPRIVS_LOWER))
284 zlog_err("Can't lower privileges");
285
286 ns_init_management(ns_id);
287
288 logicalrouter_init(logicalrouter_config_write);
289
290 /* Do any needed per-NS data structure allocation. */
291 dzns->if_table = route_table_init();
292 zebra_vxlan_ns_init(dzns);
293
294 /* Register zebra VRF callbacks, create and activate default VRF. */
295 zebra_vrf_init();
296
297 /* Default NS is activated */
298 zebra_ns_enable(ns_id, (void **)&dzns);
299
300 if (vrf_is_backend_netns()) {
301 ns_add_hook(NS_NEW_HOOK, zebra_ns_new);
302 ns_add_hook(NS_ENABLE_HOOK, zebra_ns_enabled);
303 ns_add_hook(NS_DISABLE_HOOK, zebra_ns_disabled);
304 ns_add_hook(NS_DELETE_HOOK, zebra_ns_delete);
305 zebra_ns_notify_parse();
306 zebra_ns_notify_init();
307 }
308 return 0;
309 }
310
311 static int logicalrouter_config_write(struct vty *vty)
312 {
313 struct ns *ns;
314 int write = 0;
315
316 RB_FOREACH (ns, ns_head, &ns_tree) {
317 if (ns->ns_id == NS_DEFAULT || ns->name == NULL)
318 continue;
319 vty_out(vty, "logical-router %u netns %s\n", ns->ns_id,
320 ns->name);
321 write = 1;
322 }
323 return write;
324 }
325
326 int zebra_ns_config_write(struct vty *vty, struct ns *ns)
327 {
328 if (ns && ns->name != NULL)
329 vty_out(vty, " netns %s\n", ns->name);
330 return 0;
331 }