]>
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" |
8288a24f | 41 | #include "table_manager.h" |
ec31f30d PG |
42 | |
43 | extern struct zebra_privs_t zserv_privs; | |
4a1ab8e4 | 44 | |
d62a17ae | 45 | DEFINE_MTYPE(ZEBRA, ZEBRA_NS, "Zebra Name Space") |
fe18ee2d | 46 | |
337960dd | 47 | static struct zebra_ns *dzns; |
fe18ee2d | 48 | |
736d41ad | 49 | static int logicalrouter_config_write(struct vty *vty); |
62b8bb7a | 50 | static int zebra_ns_disable_internal(struct zebra_ns *zns, bool complete); |
736d41ad | 51 | |
d62a17ae | 52 | struct zebra_ns *zebra_ns_lookup(ns_id_t ns_id) |
fe18ee2d | 53 | { |
ff705b15 PG |
54 | if (ns_id == NS_DEFAULT) |
55 | return dzns; | |
56 | struct zebra_ns *info = (struct zebra_ns *)ns_info_lookup(ns_id); | |
57 | ||
58 | return (info == NULL) ? dzns : info; | |
fe18ee2d DS |
59 | } |
60 | ||
3347430b PG |
61 | static struct zebra_ns *zebra_ns_alloc(void) |
62 | { | |
63 | return XCALLOC(MTYPE_ZEBRA_NS, sizeof(struct zebra_ns)); | |
64 | } | |
65 | ||
66 | static int zebra_ns_new(struct ns *ns) | |
67 | { | |
68 | struct zebra_ns *zns; | |
69 | ||
70 | if (IS_ZEBRA_DEBUG_EVENT) | |
71 | zlog_info("ZNS %s with id %u (created)", ns->name, ns->ns_id); | |
72 | ||
73 | zns = zebra_ns_alloc(); | |
74 | ns->info = zns; | |
75 | zns->ns = ns; | |
ff705b15 PG |
76 | |
77 | /* Do any needed per-NS data structure allocation. */ | |
78 | zns->if_table = route_table_init(); | |
79 | zebra_vxlan_ns_init(zns); | |
80 | ||
3347430b PG |
81 | return 0; |
82 | } | |
83 | ||
84 | static int zebra_ns_delete(struct ns *ns) | |
85 | { | |
996c9314 | 86 | struct zebra_ns *zns = (struct zebra_ns *)ns->info; |
3347430b PG |
87 | |
88 | if (IS_ZEBRA_DEBUG_EVENT) | |
89 | zlog_info("ZNS %s with id %u (deleted)", ns->name, ns->ns_id); | |
90 | if (!zns) | |
91 | return 0; | |
92 | XFREE(MTYPE_ZEBRA_NS, zns); | |
93 | return 0; | |
94 | } | |
95 | ||
96 | static int zebra_ns_enabled(struct ns *ns) | |
97 | { | |
98 | struct zebra_ns *zns = ns->info; | |
99 | ||
100 | if (IS_ZEBRA_DEBUG_EVENT) | |
101 | zlog_info("ZNS %s with id %u (enabled)", ns->name, ns->ns_id); | |
102 | if (!zns) | |
103 | return 0; | |
104 | return zebra_ns_enable(ns->ns_id, (void **)&zns); | |
105 | } | |
106 | ||
ff705b15 | 107 | int zebra_ns_disabled(struct ns *ns) |
3347430b PG |
108 | { |
109 | struct zebra_ns *zns = ns->info; | |
110 | ||
111 | if (IS_ZEBRA_DEBUG_EVENT) | |
112 | zlog_info("ZNS %s with id %u (disabled)", ns->name, ns->ns_id); | |
113 | if (!zns) | |
114 | return 0; | |
62b8bb7a | 115 | return zebra_ns_disable_internal(zns, true); |
3347430b PG |
116 | } |
117 | ||
84915b0a | 118 | /* Do global enable actions - open sockets, read kernel config etc. */ |
d62a17ae | 119 | int zebra_ns_enable(ns_id_t ns_id, void **info) |
fe18ee2d | 120 | { |
d62a17ae | 121 | struct zebra_ns *zns = (struct zebra_ns *)(*info); |
fe18ee2d | 122 | |
ff705b15 PG |
123 | zns->ns_id = ns_id; |
124 | ||
d62a17ae | 125 | #if defined(HAVE_RTADV) |
126 | rtadv_init(zns); | |
fe18ee2d DS |
127 | #endif |
128 | ||
d62a17ae | 129 | kernel_init(zns); |
130 | interface_list(zns); | |
131 | route_read(zns); | |
fe18ee2d | 132 | |
8288a24f PG |
133 | /* Initiate Table Manager per ZNS */ |
134 | table_manager_enable(ns_id); | |
135 | ||
d62a17ae | 136 | return 0; |
fe18ee2d DS |
137 | } |
138 | ||
62b8bb7a MS |
139 | /* Common handler for ns disable - this can be called during ns config, |
140 | * or during zebra shutdown. | |
141 | */ | |
142 | static int zebra_ns_disable_internal(struct zebra_ns *zns, bool complete) | |
fe18ee2d | 143 | { |
d62a17ae | 144 | route_table_finish(zns->if_table); |
b7cfce93 | 145 | zebra_vxlan_ns_disable(zns); |
d62a17ae | 146 | #if defined(HAVE_RTADV) |
147 | rtadv_terminate(zns); | |
fe18ee2d DS |
148 | #endif |
149 | ||
62b8bb7a | 150 | kernel_terminate(zns, complete); |
fe18ee2d | 151 | |
8288a24f PG |
152 | table_manager_disable(zns->ns_id); |
153 | ||
ff705b15 PG |
154 | zns->ns_id = NS_DEFAULT; |
155 | ||
d62a17ae | 156 | return 0; |
fe18ee2d DS |
157 | } |
158 | ||
62b8bb7a MS |
159 | /* During zebra shutdown, do partial cleanup while the async dataplane |
160 | * is still running. | |
161 | */ | |
162 | int zebra_ns_early_shutdown(struct ns *ns) | |
163 | { | |
164 | struct zebra_ns *zns = ns->info; | |
165 | ||
166 | if (zns == NULL) | |
167 | return 0; | |
168 | ||
169 | return zebra_ns_disable_internal(zns, false); | |
170 | } | |
171 | ||
172 | /* During zebra shutdown, do final cleanup | |
173 | * after all dataplane work is complete. | |
174 | */ | |
175 | int zebra_ns_final_shutdown(struct ns *ns) | |
176 | { | |
177 | struct zebra_ns *zns = ns->info; | |
178 | ||
179 | if (zns == NULL) | |
180 | return 0; | |
181 | ||
182 | kernel_terminate(zns, true); | |
183 | ||
184 | return 0; | |
185 | } | |
5335613b | 186 | |
d62a17ae | 187 | int zebra_ns_init(void) |
fe18ee2d | 188 | { |
ec31f30d | 189 | ns_id_t ns_id; |
03aff2d8 | 190 | ns_id_t ns_id_external; |
ec31f30d | 191 | |
3347430b PG |
192 | dzns = zebra_ns_alloc(); |
193 | ||
01b9e3fd DL |
194 | frr_elevate_privs(&zserv_privs) { |
195 | ns_id = zebra_ns_id_get_default(); | |
196 | } | |
03aff2d8 PG |
197 | ns_id_external = ns_map_nsid_with_external(ns_id, true); |
198 | ns_init_management(ns_id_external, ns_id); | |
736d41ad PG |
199 | |
200 | logicalrouter_init(logicalrouter_config_write); | |
13460c44 | 201 | |
84915b0a | 202 | /* Do any needed per-NS data structure allocation. */ |
203 | dzns->if_table = route_table_init(); | |
204 | zebra_vxlan_ns_init(dzns); | |
205 | ||
206 | /* Register zebra VRF callbacks, create and activate default VRF. */ | |
d62a17ae | 207 | zebra_vrf_init(); |
fe18ee2d | 208 | |
84915b0a | 209 | /* Default NS is activated */ |
03aff2d8 | 210 | zebra_ns_enable(ns_id_external, (void **)&dzns); |
fe18ee2d | 211 | |
3347430b PG |
212 | if (vrf_is_backend_netns()) { |
213 | ns_add_hook(NS_NEW_HOOK, zebra_ns_new); | |
214 | ns_add_hook(NS_ENABLE_HOOK, zebra_ns_enabled); | |
215 | ns_add_hook(NS_DISABLE_HOOK, zebra_ns_disabled); | |
216 | ns_add_hook(NS_DELETE_HOOK, zebra_ns_delete); | |
e27dec3c PG |
217 | zebra_ns_notify_parse(); |
218 | zebra_ns_notify_init(); | |
3347430b | 219 | } |
7f0ea8a4 | 220 | |
d62a17ae | 221 | return 0; |
fe18ee2d | 222 | } |
b95c1883 | 223 | |
736d41ad PG |
224 | static int logicalrouter_config_write(struct vty *vty) |
225 | { | |
226 | struct ns *ns; | |
227 | int write = 0; | |
228 | ||
996c9314 | 229 | RB_FOREACH (ns, ns_head, &ns_tree) { |
736d41ad PG |
230 | if (ns->ns_id == NS_DEFAULT || ns->name == NULL) |
231 | continue; | |
232 | vty_out(vty, "logical-router %u netns %s\n", ns->ns_id, | |
233 | ns->name); | |
234 | write = 1; | |
235 | } | |
236 | return write; | |
237 | } | |
238 | ||
b95c1883 PG |
239 | int zebra_ns_config_write(struct vty *vty, struct ns *ns) |
240 | { | |
241 | if (ns && ns->name != NULL) | |
242 | vty_out(vty, " netns %s\n", ns->name); | |
243 | return 0; | |
244 | } |