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