]> git.proxmox.com Git - mirror_frr.git/blob - staticd/static_vrf.c
Merge pull request #7782 from kuldeepkash/multicast_pim_sm_topo2
[mirror_frr.git] / staticd / static_vrf.c
1 /*
2 * STATICd - vrf code
3 * Copyright (C) 2018 Cumulus Networks, Inc.
4 * Donald Sharp
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 #include <zebra.h>
21
22 #include "vrf.h"
23 #include "nexthop.h"
24 #include "table.h"
25 #include "srcdest_table.h"
26
27 #include "static_memory.h"
28 #include "static_vrf.h"
29 #include "static_routes.h"
30 #include "static_zebra.h"
31 #include "static_vty.h"
32
33 DEFINE_MTYPE_STATIC(STATIC, STATIC_RTABLE_INFO, "Static Route Table Info");
34
35 static void zebra_stable_node_cleanup(struct route_table *table,
36 struct route_node *node)
37 {
38 struct static_nexthop *nh;
39 struct static_path *pn;
40 struct static_route_info *si;
41 struct route_table *src_table;
42 struct route_node *src_node;
43 struct static_path *src_pn;
44 struct static_route_info *src_si;
45
46 si = node->info;
47
48 if (si) {
49 frr_each_safe(static_path_list, &si->path_list, pn) {
50 frr_each_safe(static_nexthop_list, &pn->nexthop_list,
51 nh) {
52 static_nexthop_list_del(&pn->nexthop_list, nh);
53 XFREE(MTYPE_STATIC_NEXTHOP, nh);
54 }
55 static_path_list_del(&si->path_list, pn);
56 XFREE(MTYPE_STATIC_PATH, pn);
57 }
58
59 /* clean up for dst table */
60 src_table = srcdest_srcnode_table(node);
61 if (src_table) {
62 /* This means the route_node is part of the top
63 * hierarchy and refers to a destination prefix.
64 */
65 for (src_node = route_top(src_table); src_node;
66 src_node = route_next(src_node)) {
67 src_si = src_node->info;
68
69 frr_each_safe(static_path_list,
70 &src_si->path_list, src_pn) {
71 frr_each_safe(static_nexthop_list,
72 &src_pn->nexthop_list,
73 nh) {
74 static_nexthop_list_del(
75 &src_pn->nexthop_list,
76 nh);
77 XFREE(MTYPE_STATIC_NEXTHOP, nh);
78 }
79 static_path_list_del(&src_si->path_list,
80 src_pn);
81 XFREE(MTYPE_STATIC_PATH, src_pn);
82 }
83
84 XFREE(MTYPE_STATIC_ROUTE, src_node->info);
85 }
86 }
87
88 XFREE(MTYPE_STATIC_ROUTE, node->info);
89 }
90 }
91
92 static struct static_vrf *static_vrf_alloc(void)
93 {
94 struct route_table *table;
95 struct static_vrf *svrf;
96 struct stable_info *info;
97 safi_t safi;
98 afi_t afi;
99
100 svrf = XCALLOC(MTYPE_STATIC_RTABLE_INFO, sizeof(struct static_vrf));
101
102 for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
103 for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++) {
104 if (afi == AFI_IP6)
105 table = srcdest_table_init();
106 else
107 table = route_table_init();
108
109 info = XCALLOC(MTYPE_STATIC_RTABLE_INFO,
110 sizeof(struct stable_info));
111 info->svrf = svrf;
112 info->afi = afi;
113 info->safi = safi;
114 route_table_set_info(table, info);
115
116 table->cleanup = zebra_stable_node_cleanup;
117 svrf->stable[afi][safi] = table;
118 }
119 }
120 return svrf;
121 }
122
123 static int static_vrf_new(struct vrf *vrf)
124 {
125 struct static_vrf *svrf;
126
127 svrf = static_vrf_alloc();
128 vrf->info = svrf;
129 svrf->vrf = vrf;
130
131 return 0;
132 }
133
134 static int static_vrf_enable(struct vrf *vrf)
135 {
136 static_zebra_vrf_register(vrf);
137
138 static_fixup_vrf_ids(vrf->info);
139
140 return 0;
141 }
142
143 static int static_vrf_disable(struct vrf *vrf)
144 {
145 static_zebra_vrf_unregister(vrf);
146 return 0;
147 }
148
149 static int static_vrf_delete(struct vrf *vrf)
150 {
151 struct route_table *table;
152 struct static_vrf *svrf;
153 safi_t safi;
154 afi_t afi;
155 void *info;
156
157 svrf = vrf->info;
158 for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
159 for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++) {
160 table = svrf->stable[afi][safi];
161 info = route_table_get_info(table);
162 route_table_finish(table);
163 XFREE(MTYPE_STATIC_RTABLE_INFO, info);
164 svrf->stable[afi][safi] = NULL;
165 }
166 }
167 XFREE(MTYPE_STATIC_RTABLE_INFO, svrf);
168 return 0;
169 }
170
171 /* Lookup the static routing table in a VRF. */
172 struct route_table *static_vrf_static_table(afi_t afi, safi_t safi,
173 struct static_vrf *svrf)
174 {
175 if (!svrf)
176 return NULL;
177
178 if (afi >= AFI_MAX || safi >= SAFI_MAX)
179 return NULL;
180
181 return svrf->stable[afi][safi];
182 }
183
184 struct static_vrf *static_vrf_lookup_by_id(vrf_id_t vrf_id)
185 {
186 struct vrf *vrf;
187
188 vrf = vrf_lookup_by_id(vrf_id);
189 if (vrf)
190 return ((struct static_vrf *)vrf->info);
191
192 return NULL;
193 }
194
195 struct static_vrf *static_vrf_lookup_by_name(const char *name)
196 {
197 struct vrf *vrf;
198
199 if (!name)
200 name = VRF_DEFAULT_NAME;
201
202 vrf = vrf_lookup_by_name(name);
203 if (vrf)
204 return ((struct static_vrf *)vrf->info);
205
206 return NULL;
207 }
208
209 static int static_vrf_config_write(struct vty *vty)
210 {
211 struct vrf *vrf;
212
213 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
214 if (vrf->vrf_id != VRF_DEFAULT)
215 vty_frame(vty, "vrf %s\n", vrf->name);
216
217 static_config(vty, vrf->info, AFI_IP,
218 SAFI_UNICAST, "ip route");
219 static_config(vty, vrf->info, AFI_IP,
220 SAFI_MULTICAST, "ip mroute");
221 static_config(vty, vrf->info, AFI_IP6,
222 SAFI_UNICAST, "ipv6 route");
223
224 if (vrf->vrf_id != VRF_DEFAULT)
225 vty_endframe(vty, " exit-vrf\n!\n");
226 }
227
228 return 0;
229 }
230
231 int static_vrf_has_config(struct static_vrf *svrf)
232 {
233 struct route_table *table;
234 safi_t safi;
235 afi_t afi;
236
237 /*
238 * NOTE: This is a don't care for the default VRF, but we go through
239 * the motions to keep things consistent.
240 */
241 FOREACH_AFI_SAFI (afi, safi) {
242 table = svrf->stable[afi][safi];
243 if (!table)
244 continue;
245 if (route_table_count(table))
246 return 1;
247 }
248
249 return 0;
250 }
251
252 void static_vrf_init(void)
253 {
254 vrf_init(static_vrf_new, static_vrf_enable,
255 static_vrf_disable, static_vrf_delete, NULL);
256
257 vrf_cmd_init(static_vrf_config_write, &static_privs);
258 }
259
260 void static_vrf_terminate(void)
261 {
262 vrf_terminate();
263 }
264
265 struct static_vrf *static_vty_get_unknown_vrf(const char *vrf_name)
266 {
267 struct static_vrf *svrf;
268 struct vrf *vrf;
269
270 svrf = static_vrf_lookup_by_name(vrf_name);
271
272 if (svrf)
273 return svrf;
274
275 vrf = vrf_get(VRF_UNKNOWN, vrf_name);
276 if (!vrf)
277 return NULL;
278 svrf = vrf->info;
279 if (!svrf)
280 return NULL;
281 /* Mark as having FRR configuration */
282 vrf_set_user_cfged(vrf);
283
284 return svrf;
285 }