]>
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 | ||
d62a17ae | 138 | #if defined(HAVE_RTADV) |
139 | rtadv_init(zns); | |
fe18ee2d DS |
140 | #endif |
141 | ||
d62a17ae | 142 | kernel_init(zns); |
143 | interface_list(zns); | |
144 | route_read(zns); | |
fe18ee2d | 145 | |
d62a17ae | 146 | return 0; |
fe18ee2d DS |
147 | } |
148 | ||
996c9314 LB |
149 | struct route_table *zebra_ns_find_table(struct zebra_ns *zns, uint32_t tableid, |
150 | afi_t afi) | |
ae825b8b | 151 | { |
55cd0f61 DS |
152 | struct zebra_ns_table finder; |
153 | struct zebra_ns_table *znst; | |
ae825b8b DS |
154 | |
155 | memset(&finder, 0, sizeof(finder)); | |
156 | finder.afi = afi; | |
157 | finder.tableid = tableid; | |
55cd0f61 | 158 | znst = RB_FIND(zebra_ns_table_head, &zns->ns_tables, &finder); |
ae825b8b DS |
159 | |
160 | if (znst) | |
161 | return znst->table; | |
162 | else | |
163 | return NULL; | |
164 | } | |
165 | ||
47a08aa9 DS |
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 | ||
95a29032 DS |
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 | ||
5335613b DS |
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 | ||
783fc3cd | 224 | static void zebra_ns_free_table(struct zebra_ns_table *znst) |
5335613b DS |
225 | { |
226 | void *table_info; | |
27b136bd | 227 | |
5335613b DS |
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); | |
5335613b DS |
234 | } |
235 | ||
d62a17ae | 236 | int zebra_ns_disable(ns_id_t ns_id, void **info) |
fe18ee2d | 237 | { |
5335613b | 238 | struct zebra_ns_table *znst; |
d62a17ae | 239 | struct zebra_ns *zns = (struct zebra_ns *)(*info); |
fe18ee2d | 240 | |
43fe6a2a DS |
241 | hash_clean(zns->rules_hash, zebra_pbr_rules_free); |
242 | hash_free(zns->rules_hash); | |
55cd0f61 DS |
243 | while (!RB_EMPTY(zebra_ns_table_head, &zns->ns_tables)) { |
244 | znst = RB_ROOT(zebra_ns_table_head, &zns->ns_tables); | |
245 | ||
5335613b | 246 | RB_REMOVE(zebra_ns_table_head, &zns->ns_tables, znst); |
783fc3cd | 247 | zebra_ns_free_table(znst); |
5335613b | 248 | } |
43fe6a2a | 249 | |
d62a17ae | 250 | route_table_finish(zns->if_table); |
b7cfce93 | 251 | zebra_vxlan_ns_disable(zns); |
d62a17ae | 252 | #if defined(HAVE_RTADV) |
253 | rtadv_terminate(zns); | |
fe18ee2d DS |
254 | #endif |
255 | ||
d62a17ae | 256 | kernel_terminate(zns); |
fe18ee2d | 257 | |
ff705b15 PG |
258 | zns->ns_id = NS_DEFAULT; |
259 | ||
d62a17ae | 260 | return 0; |
fe18ee2d DS |
261 | } |
262 | ||
5335613b | 263 | |
d62a17ae | 264 | int zebra_ns_init(void) |
fe18ee2d | 265 | { |
ec31f30d PG |
266 | ns_id_t ns_id; |
267 | ||
3347430b PG |
268 | dzns = zebra_ns_alloc(); |
269 | ||
ec31f30d PG |
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"); | |
fe18ee2d | 275 | |
736d41ad PG |
276 | ns_init_management(ns_id); |
277 | ||
278 | logicalrouter_init(logicalrouter_config_write); | |
13460c44 | 279 | |
84915b0a | 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. */ | |
d62a17ae | 285 | zebra_vrf_init(); |
fe18ee2d | 286 | |
84915b0a | 287 | /* Default NS is activated */ |
736d41ad | 288 | zebra_ns_enable(ns_id, (void **)&dzns); |
fe18ee2d | 289 | |
43fe6a2a DS |
290 | dzns->rules_hash = |
291 | hash_create_size(8, zebra_pbr_rules_hash_key, | |
292 | zebra_pbr_rules_hash_equal, "Rules Hash"); | |
3347430b PG |
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); | |
e27dec3c PG |
298 | zebra_ns_notify_parse(); |
299 | zebra_ns_notify_init(); | |
3347430b | 300 | } |
d62a17ae | 301 | return 0; |
fe18ee2d | 302 | } |
b95c1883 | 303 | |
736d41ad PG |
304 | static int logicalrouter_config_write(struct vty *vty) |
305 | { | |
306 | struct ns *ns; | |
307 | int write = 0; | |
308 | ||
996c9314 | 309 | RB_FOREACH (ns, ns_head, &ns_tree) { |
736d41ad PG |
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 | ||
b95c1883 PG |
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 | } |