Line data Source code
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/prefix.h"
27 : #include "lib/memory.h"
28 :
29 : #include "zebra_ns.h"
30 : #include "zebra_vrf.h"
31 : #include "rt.h"
32 : #include "zebra_vxlan.h"
33 : #include "debug.h"
34 : #include "zebra_netns_notify.h"
35 : #include "zebra_netns_id.h"
36 : #include "zebra_pbr.h"
37 : #include "zebra_tc.h"
38 : #include "rib.h"
39 : #include "table_manager.h"
40 : #include "zebra_errors.h"
41 :
42 : extern struct zebra_privs_t zserv_privs;
43 :
44 6 : DEFINE_MTYPE_STATIC(ZEBRA, ZEBRA_NS, "Zebra Name Space");
45 :
46 : static struct zebra_ns *dzns;
47 :
48 : static int zebra_ns_disable_internal(struct zebra_ns *zns, bool complete);
49 :
50 98 : struct zebra_ns *zebra_ns_lookup(ns_id_t ns_id)
51 : {
52 98 : if (ns_id == NS_DEFAULT)
53 98 : return dzns;
54 0 : struct zebra_ns *info = (struct zebra_ns *)ns_info_lookup(ns_id);
55 :
56 0 : return (info == NULL) ? dzns : info;
57 : }
58 :
59 2 : static struct zebra_ns *zebra_ns_alloc(void)
60 : {
61 2 : return XCALLOC(MTYPE_ZEBRA_NS, sizeof(struct zebra_ns));
62 : }
63 :
64 2 : static int zebra_ns_new(struct ns *ns)
65 : {
66 2 : struct zebra_ns *zns;
67 :
68 2 : if (!ns)
69 : return -1;
70 :
71 2 : if (IS_ZEBRA_DEBUG_EVENT)
72 0 : zlog_info("ZNS %s with id %u (created)", ns->name, ns->ns_id);
73 :
74 2 : zns = zebra_ns_alloc();
75 2 : ns->info = zns;
76 2 : zns->ns = ns;
77 2 : zns->ns_id = ns->ns_id;
78 :
79 : /* Do any needed per-NS data structure allocation. */
80 2 : zns->if_table = route_table_init();
81 :
82 2 : return 0;
83 : }
84 :
85 0 : static int zebra_ns_delete(struct ns *ns)
86 : {
87 0 : struct zebra_ns *zns = (struct zebra_ns *)ns->info;
88 :
89 0 : if (IS_ZEBRA_DEBUG_EVENT)
90 0 : zlog_info("ZNS %s with id %u (deleted)", ns->name, ns->ns_id);
91 0 : if (!zns)
92 : return 0;
93 0 : XFREE(MTYPE_ZEBRA_NS, ns->info);
94 0 : return 0;
95 : }
96 :
97 0 : static int zebra_ns_enabled(struct ns *ns)
98 : {
99 0 : struct zebra_ns *zns = ns->info;
100 :
101 0 : if (IS_ZEBRA_DEBUG_EVENT)
102 0 : zlog_info("ZNS %s with id %u (enabled)", ns->name, ns->ns_id);
103 0 : if (!zns)
104 : return 0;
105 0 : return zebra_ns_enable(ns->ns_id, (void **)&zns);
106 : }
107 :
108 0 : int zebra_ns_disabled(struct ns *ns)
109 : {
110 0 : struct zebra_ns *zns = ns->info;
111 :
112 0 : if (IS_ZEBRA_DEBUG_EVENT)
113 0 : zlog_info("ZNS %s with id %u (disabled)", ns->name, ns->ns_id);
114 0 : if (!zns)
115 : return 0;
116 0 : return zebra_ns_disable_internal(zns, true);
117 : }
118 :
119 : /* Do global enable actions - open sockets, read kernel config etc. */
120 2 : int zebra_ns_enable(ns_id_t ns_id, void **info)
121 : {
122 2 : struct zebra_ns *zns = (struct zebra_ns *)(*info);
123 :
124 2 : zns->ns_id = ns_id;
125 :
126 2 : kernel_init(zns);
127 2 : zebra_dplane_ns_enable(zns, true);
128 2 : interface_list(zns);
129 2 : route_read(zns);
130 2 : kernel_read_pbr_rules(zns);
131 2 : kernel_read_tc_qdisc(zns);
132 :
133 2 : return 0;
134 : }
135 :
136 : /* Common handler for ns disable - this can be called during ns config,
137 : * or during zebra shutdown.
138 : */
139 2 : static int zebra_ns_disable_internal(struct zebra_ns *zns, bool complete)
140 : {
141 2 : if (zns->if_table)
142 2 : route_table_finish(zns->if_table);
143 2 : zns->if_table = NULL;
144 :
145 2 : zebra_dplane_ns_enable(zns, false /*Disable*/);
146 :
147 2 : kernel_terminate(zns, complete);
148 :
149 2 : zns->ns_id = NS_DEFAULT;
150 :
151 2 : return 0;
152 : }
153 :
154 : /* During zebra shutdown, do partial cleanup while the async dataplane
155 : * is still running.
156 : */
157 2 : int zebra_ns_early_shutdown(struct ns *ns,
158 : void *param_in __attribute__((unused)),
159 : void **param_out __attribute__((unused)))
160 : {
161 2 : struct zebra_ns *zns = ns->info;
162 :
163 2 : if (zns == NULL)
164 : return 0;
165 :
166 2 : zebra_ns_disable_internal(zns, false);
167 2 : return NS_WALK_CONTINUE;
168 : }
169 :
170 : /* During zebra shutdown, do final cleanup
171 : * after all dataplane work is complete.
172 : */
173 2 : int zebra_ns_final_shutdown(struct ns *ns,
174 : void *param_in __attribute__((unused)),
175 : void **param_out __attribute__((unused)))
176 : {
177 2 : struct zebra_ns *zns = ns->info;
178 :
179 2 : if (zns == NULL)
180 : return 0;
181 :
182 2 : kernel_terminate(zns, true);
183 :
184 2 : return NS_WALK_CONTINUE;
185 : }
186 :
187 2 : int zebra_ns_init(void)
188 : {
189 2 : struct ns *default_ns;
190 2 : ns_id_t ns_id;
191 2 : ns_id_t ns_id_external;
192 2 : struct ns *ns;
193 :
194 2 : frr_with_privs(&zserv_privs) {
195 2 : ns_id = zebra_ns_id_get_default();
196 : }
197 2 : ns_id_external = ns_map_nsid_with_external(ns_id, true);
198 2 : ns_init_management(ns_id_external, ns_id);
199 2 : ns = ns_get_default();
200 2 : if (ns)
201 2 : ns->relative_default_ns = ns_id;
202 :
203 2 : default_ns = ns_lookup(NS_DEFAULT);
204 2 : if (!default_ns) {
205 0 : flog_err(EC_ZEBRA_NS_NO_DEFAULT,
206 : "%s: failed to find default ns", __func__);
207 0 : exit(EXIT_FAILURE); /* This is non-recoverable */
208 : }
209 :
210 : /* Do any needed per-NS data structure allocation. */
211 2 : zebra_ns_new(default_ns);
212 2 : dzns = default_ns->info;
213 :
214 : /* Register zebra VRF callbacks, create and activate default VRF. */
215 2 : zebra_vrf_init();
216 :
217 : /* Default NS is activated */
218 2 : zebra_ns_enable(ns_id_external, (void **)&dzns);
219 :
220 2 : if (vrf_is_backend_netns()) {
221 0 : ns_add_hook(NS_NEW_HOOK, zebra_ns_new);
222 0 : ns_add_hook(NS_ENABLE_HOOK, zebra_ns_enabled);
223 0 : ns_add_hook(NS_DISABLE_HOOK, zebra_ns_disabled);
224 0 : ns_add_hook(NS_DELETE_HOOK, zebra_ns_delete);
225 0 : zebra_ns_notify_parse();
226 0 : zebra_ns_notify_init();
227 : }
228 :
229 2 : return 0;
230 : }
231 :
232 0 : int zebra_ns_config_write(struct vty *vty, struct ns *ns)
233 : {
234 0 : if (ns && ns->name != NULL)
235 0 : vty_out(vty, " netns %s\n", ns->name);
236 0 : return 0;
237 : }
|