]>
Commit | Line | Data |
---|---|---|
fe18ee2d DS |
1 | /* zebra NS Routines |
2 | * Copyright (C) 2016 Cumulus Networks, Inc. | |
3 | * Donald Sharp | |
b95c1883 | 4 | * Copyright (C) 2017/2018 6WIND |
fe18ee2d DS |
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 | * | |
896014f4 DL |
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 | |
fe18ee2d DS |
21 | */ |
22 | #include "zebra.h" | |
23 | ||
13460c44 | 24 | #include "lib/ns.h" |
fe18ee2d | 25 | #include "lib/vrf.h" |
736d41ad | 26 | #include "lib/logicalrouter.h" |
fe18ee2d DS |
27 | #include "lib/prefix.h" |
28 | #include "lib/memory.h" | |
29 | ||
30 | #include "rtadv.h" | |
31 | #include "zebra_ns.h" | |
7c551956 | 32 | #include "zebra_vrf.h" |
4a1ab8e4 | 33 | #include "zebra_memory.h" |
05f7f5db | 34 | #include "rt.h" |
b7cfce93 | 35 | #include "zebra_vxlan.h" |
3347430b | 36 | #include "debug.h" |
e27dec3c | 37 | #include "zebra_netns_notify.h" |
ec31f30d | 38 | #include "zebra_netns_id.h" |
43fe6a2a | 39 | #include "zebra_pbr.h" |
47a08aa9 | 40 | #include "rib.h" |
ec31f30d PG |
41 | |
42 | extern struct zebra_privs_t zserv_privs; | |
4a1ab8e4 | 43 | |
d62a17ae | 44 | DEFINE_MTYPE(ZEBRA, ZEBRA_NS, "Zebra Name Space") |
fe18ee2d | 45 | |
996c9314 LB |
46 | static inline int zebra_ns_table_entry_compare(const struct zebra_ns_table *e1, |
47 | const struct zebra_ns_table *e2); | |
5335613b DS |
48 | |
49 | RB_GENERATE(zebra_ns_table_head, zebra_ns_table, zebra_ns_table_entry, | |
50 | zebra_ns_table_entry_compare); | |
51 | ||
337960dd | 52 | static struct zebra_ns *dzns; |
fe18ee2d | 53 | |
996c9314 LB |
54 | static inline int zebra_ns_table_entry_compare(const struct zebra_ns_table *e1, |
55 | const struct zebra_ns_table *e2) | |
5335613b DS |
56 | { |
57 | if (e1->tableid == e2->tableid) | |
58 | return (e1->afi - e2->afi); | |
59 | ||
60 | return e1->tableid - e2->tableid; | |
61 | } | |
62 | ||
736d41ad PG |
63 | static int logicalrouter_config_write(struct vty *vty); |
64 | ||
d62a17ae | 65 | struct zebra_ns *zebra_ns_lookup(ns_id_t ns_id) |
fe18ee2d | 66 | { |
ff705b15 PG |
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; | |
fe18ee2d DS |
72 | } |
73 | ||
3347430b PG |
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; | |
ff705b15 PG |
89 | |
90 | /* Do any needed per-NS data structure allocation. */ | |
91 | zns->if_table = route_table_init(); | |
92 | zebra_vxlan_ns_init(zns); | |
93 | ||
3347430b PG |
94 | return 0; |
95 | } | |
96 | ||
97 | static int zebra_ns_delete(struct ns *ns) | |
98 | { | |
996c9314 | 99 | struct zebra_ns *zns = (struct zebra_ns *)ns->info; |
3347430b PG |
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 | ||
ff705b15 | 120 | int zebra_ns_disabled(struct ns *ns) |
3347430b PG |
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 | ||
84915b0a | 131 | /* Do global enable actions - open sockets, read kernel config etc. */ |
d62a17ae | 132 | int zebra_ns_enable(ns_id_t ns_id, void **info) |
fe18ee2d | 133 | { |
d62a17ae | 134 | struct zebra_ns *zns = (struct zebra_ns *)(*info); |
fe18ee2d | 135 | |
ff705b15 PG |
136 | zns->ns_id = ns_id; |
137 | ||
5e280e56 PG |
138 | zns->rules_hash = |
139 | hash_create_size(8, zebra_pbr_rules_hash_key, | |
140 | zebra_pbr_rules_hash_equal, "Rules Hash"); | |
141 | ||
d62a17ae | 142 | #if defined(HAVE_RTADV) |
143 | rtadv_init(zns); | |
fe18ee2d DS |
144 | #endif |
145 | ||
d62a17ae | 146 | kernel_init(zns); |
147 | interface_list(zns); | |
148 | route_read(zns); | |
fe18ee2d | 149 | |
d62a17ae | 150 | return 0; |
fe18ee2d DS |
151 | } |
152 | ||
996c9314 LB |
153 | struct route_table *zebra_ns_find_table(struct zebra_ns *zns, uint32_t tableid, |
154 | afi_t afi) | |
ae825b8b | 155 | { |
55cd0f61 DS |
156 | struct zebra_ns_table finder; |
157 | struct zebra_ns_table *znst; | |
ae825b8b DS |
158 | |
159 | memset(&finder, 0, sizeof(finder)); | |
160 | finder.afi = afi; | |
161 | finder.tableid = tableid; | |
55cd0f61 | 162 | znst = RB_FIND(zebra_ns_table_head, &zns->ns_tables, &finder); |
ae825b8b DS |
163 | |
164 | if (znst) | |
165 | return znst->table; | |
166 | else | |
167 | return NULL; | |
168 | } | |
169 | ||
47a08aa9 DS |
170 | unsigned long zebra_ns_score_proto(u_char proto, u_short instance) |
171 | { | |
172 | struct zebra_ns *zns; | |
173 | struct zebra_ns_table *znst; | |
174 | unsigned long cnt = 0; | |
175 | ||
176 | zns = zebra_ns_lookup(NS_DEFAULT); | |
177 | ||
178 | RB_FOREACH (znst, zebra_ns_table_head, &zns->ns_tables) | |
179 | cnt += rib_score_proto_table(proto, instance, znst->table); | |
180 | ||
181 | return cnt; | |
182 | } | |
183 | ||
95a29032 DS |
184 | void zebra_ns_sweep_route(void) |
185 | { | |
186 | struct zebra_ns_table *znst; | |
187 | struct zebra_ns *zns; | |
188 | ||
189 | zns = zebra_ns_lookup(NS_DEFAULT); | |
190 | ||
191 | RB_FOREACH (znst, zebra_ns_table_head, &zns->ns_tables) | |
192 | rib_sweep_table(znst->table); | |
193 | } | |
194 | ||
5335613b DS |
195 | struct route_table *zebra_ns_get_table(struct zebra_ns *zns, |
196 | struct zebra_vrf *zvrf, uint32_t tableid, | |
197 | afi_t afi) | |
198 | { | |
199 | struct zebra_ns_table finder; | |
200 | struct zebra_ns_table *znst; | |
201 | rib_table_info_t *info; | |
202 | ||
203 | memset(&finder, 0, sizeof(finder)); | |
204 | finder.afi = afi; | |
205 | finder.tableid = tableid; | |
206 | znst = RB_FIND(zebra_ns_table_head, &zns->ns_tables, &finder); | |
207 | ||
208 | if (znst) | |
209 | return znst->table; | |
210 | ||
211 | znst = XCALLOC(MTYPE_ZEBRA_NS, sizeof(*znst)); | |
212 | znst->tableid = tableid; | |
213 | znst->afi = afi; | |
214 | znst->table = | |
215 | (afi == AFI_IP6) ? srcdest_table_init() : route_table_init(); | |
216 | ||
217 | info = XCALLOC(MTYPE_RIB_TABLE_INFO, sizeof(*info)); | |
218 | info->zvrf = zvrf; | |
219 | info->afi = afi; | |
220 | info->safi = SAFI_UNICAST; | |
221 | znst->table->info = info; | |
222 | znst->table->cleanup = zebra_rtable_node_cleanup; | |
223 | ||
224 | RB_INSERT(zebra_ns_table_head, &zns->ns_tables, znst); | |
225 | return znst->table; | |
226 | } | |
227 | ||
783fc3cd | 228 | static void zebra_ns_free_table(struct zebra_ns_table *znst) |
5335613b DS |
229 | { |
230 | void *table_info; | |
27b136bd | 231 | |
5335613b DS |
232 | rib_close_table(znst->table); |
233 | ||
234 | table_info = znst->table->info; | |
235 | route_table_finish(znst->table); | |
236 | XFREE(MTYPE_RIB_TABLE_INFO, table_info); | |
237 | XFREE(MTYPE_ZEBRA_NS, znst); | |
5335613b DS |
238 | } |
239 | ||
d62a17ae | 240 | int zebra_ns_disable(ns_id_t ns_id, void **info) |
fe18ee2d | 241 | { |
5335613b | 242 | struct zebra_ns_table *znst; |
d62a17ae | 243 | struct zebra_ns *zns = (struct zebra_ns *)(*info); |
fe18ee2d | 244 | |
43fe6a2a DS |
245 | hash_clean(zns->rules_hash, zebra_pbr_rules_free); |
246 | hash_free(zns->rules_hash); | |
55cd0f61 DS |
247 | while (!RB_EMPTY(zebra_ns_table_head, &zns->ns_tables)) { |
248 | znst = RB_ROOT(zebra_ns_table_head, &zns->ns_tables); | |
249 | ||
5335613b | 250 | RB_REMOVE(zebra_ns_table_head, &zns->ns_tables, znst); |
783fc3cd | 251 | zebra_ns_free_table(znst); |
5335613b | 252 | } |
43fe6a2a | 253 | |
d62a17ae | 254 | route_table_finish(zns->if_table); |
b7cfce93 | 255 | zebra_vxlan_ns_disable(zns); |
d62a17ae | 256 | #if defined(HAVE_RTADV) |
257 | rtadv_terminate(zns); | |
fe18ee2d DS |
258 | #endif |
259 | ||
d62a17ae | 260 | kernel_terminate(zns); |
fe18ee2d | 261 | |
ff705b15 PG |
262 | zns->ns_id = NS_DEFAULT; |
263 | ||
d62a17ae | 264 | return 0; |
fe18ee2d DS |
265 | } |
266 | ||
5335613b | 267 | |
d62a17ae | 268 | int zebra_ns_init(void) |
fe18ee2d | 269 | { |
ec31f30d PG |
270 | ns_id_t ns_id; |
271 | ||
3347430b PG |
272 | dzns = zebra_ns_alloc(); |
273 | ||
ec31f30d PG |
274 | if (zserv_privs.change(ZPRIVS_RAISE)) |
275 | zlog_err("Can't raise privileges"); | |
276 | ns_id = zebra_ns_id_get_default(); | |
277 | if (zserv_privs.change(ZPRIVS_LOWER)) | |
278 | zlog_err("Can't lower privileges"); | |
fe18ee2d | 279 | |
736d41ad PG |
280 | ns_init_management(ns_id); |
281 | ||
282 | logicalrouter_init(logicalrouter_config_write); | |
13460c44 | 283 | |
84915b0a | 284 | /* Do any needed per-NS data structure allocation. */ |
285 | dzns->if_table = route_table_init(); | |
286 | zebra_vxlan_ns_init(dzns); | |
287 | ||
288 | /* Register zebra VRF callbacks, create and activate default VRF. */ | |
d62a17ae | 289 | zebra_vrf_init(); |
fe18ee2d | 290 | |
84915b0a | 291 | /* Default NS is activated */ |
736d41ad | 292 | zebra_ns_enable(ns_id, (void **)&dzns); |
fe18ee2d | 293 | |
3347430b PG |
294 | if (vrf_is_backend_netns()) { |
295 | ns_add_hook(NS_NEW_HOOK, zebra_ns_new); | |
296 | ns_add_hook(NS_ENABLE_HOOK, zebra_ns_enabled); | |
297 | ns_add_hook(NS_DISABLE_HOOK, zebra_ns_disabled); | |
298 | ns_add_hook(NS_DELETE_HOOK, zebra_ns_delete); | |
e27dec3c PG |
299 | zebra_ns_notify_parse(); |
300 | zebra_ns_notify_init(); | |
3347430b | 301 | } |
d62a17ae | 302 | return 0; |
fe18ee2d | 303 | } |
b95c1883 | 304 | |
736d41ad PG |
305 | static int logicalrouter_config_write(struct vty *vty) |
306 | { | |
307 | struct ns *ns; | |
308 | int write = 0; | |
309 | ||
996c9314 | 310 | RB_FOREACH (ns, ns_head, &ns_tree) { |
736d41ad PG |
311 | if (ns->ns_id == NS_DEFAULT || ns->name == NULL) |
312 | continue; | |
313 | vty_out(vty, "logical-router %u netns %s\n", ns->ns_id, | |
314 | ns->name); | |
315 | write = 1; | |
316 | } | |
317 | return write; | |
318 | } | |
319 | ||
b95c1883 PG |
320 | int zebra_ns_config_write(struct vty *vty, struct ns *ns) |
321 | { | |
322 | if (ns && ns->name != NULL) | |
323 | vty_out(vty, " netns %s\n", ns->name); | |
324 | return 0; | |
325 | } |