]> git.proxmox.com Git - mirror_frr.git/blob - staticd/static_vrf.c
Merge pull request #5005 from Frankkkkk/dockerfile
[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 static void zebra_stable_node_cleanup(struct route_table *table,
34 struct route_node *node)
35 {
36 struct static_route *si, *next;
37
38 if (node->info)
39 for (si = node->info; si; si = next) {
40 next = si->next;
41 XFREE(MTYPE_STATIC_ROUTE, si);
42 }
43 }
44
45 static struct static_vrf *static_vrf_alloc(void)
46 {
47 struct route_table *table;
48 struct static_vrf *svrf;
49 safi_t safi;
50 afi_t afi;
51
52 svrf = XCALLOC(MTYPE_TMP, sizeof(struct static_vrf));
53
54 for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
55 for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++) {
56 if (afi == AFI_IP6)
57 table = srcdest_table_init();
58 else
59 table = route_table_init();
60 table->cleanup = zebra_stable_node_cleanup;
61 svrf->stable[afi][safi] = table;
62 }
63 }
64 return svrf;
65 }
66
67 static int static_vrf_new(struct vrf *vrf)
68 {
69 struct static_vrf *svrf;
70
71 svrf = static_vrf_alloc();
72 vrf->info = svrf;
73 svrf->vrf = vrf;
74
75 return 0;
76 }
77
78 static int static_vrf_enable(struct vrf *vrf)
79 {
80 static_zebra_vrf_register(vrf);
81
82 static_fixup_vrf_ids(vrf->info);
83
84 /*
85 * We may have static routes that are now possible to
86 * insert into the appropriate tables
87 */
88 static_config_install_delayed_routes(vrf->info);
89
90 return 0;
91 }
92
93 static int static_vrf_disable(struct vrf *vrf)
94 {
95 static_zebra_vrf_unregister(vrf);
96 return 0;
97 }
98
99 static int static_vrf_delete(struct vrf *vrf)
100 {
101 struct route_table *table;
102 struct static_vrf *svrf;
103 safi_t safi;
104 afi_t afi;
105
106 svrf = vrf->info;
107 for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
108 for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++) {
109 table = svrf->stable[afi][safi];
110 route_table_finish(table);
111 svrf->stable[afi][safi] = NULL;
112 }
113 }
114 return 0;
115 }
116
117 /* Lookup the static routing table in a VRF. */
118 struct route_table *static_vrf_static_table(afi_t afi, safi_t safi,
119 struct static_vrf *svrf)
120 {
121 if (!svrf)
122 return NULL;
123
124 if (afi >= AFI_MAX || safi >= SAFI_MAX)
125 return NULL;
126
127 return svrf->stable[afi][safi];
128 }
129
130 struct static_vrf *static_vrf_lookup_by_id(vrf_id_t vrf_id)
131 {
132 struct vrf *vrf;
133
134 vrf = vrf_lookup_by_id(vrf_id);
135 if (vrf)
136 return ((struct static_vrf *)vrf->info);
137
138 return NULL;
139 }
140
141 struct static_vrf *static_vrf_lookup_by_name(const char *name)
142 {
143 struct vrf *vrf;
144
145 if (!name)
146 name = VRF_DEFAULT_NAME;
147
148 vrf = vrf_lookup_by_name(name);
149 if (vrf)
150 return ((struct static_vrf *)vrf->info);
151
152 return NULL;
153 }
154
155 static int static_vrf_config_write(struct vty *vty)
156 {
157 struct vrf *vrf;
158
159 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
160 if (vrf->vrf_id != VRF_DEFAULT)
161 vty_frame(vty, "vrf %s\n", vrf->name);
162
163 static_config(vty, vrf->info, AFI_IP,
164 SAFI_UNICAST, "ip route");
165 static_config(vty, vrf->info, AFI_IP,
166 SAFI_MULTICAST, "ip mroute");
167 static_config(vty, vrf->info, AFI_IP6,
168 SAFI_UNICAST, "ipv6 route");
169
170 if (vrf->vrf_id != VRF_DEFAULT)
171 vty_endframe(vty, " exit-vrf\n!\n");
172 }
173
174 return 0;
175 }
176
177 int static_vrf_has_config(struct static_vrf *svrf)
178 {
179 struct route_table *table;
180 safi_t safi;
181 afi_t afi;
182
183 /*
184 * NOTE: This is a don't care for the default VRF, but we go through
185 * the motions to keep things consistent.
186 */
187 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
188 for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++) {
189 table = svrf->stable[afi][safi];
190 if (!table)
191 continue;
192 if (route_table_count(table))
193 return 1;
194 }
195 }
196
197 return 0;
198 }
199
200 void static_vrf_init(void)
201 {
202 vrf_init(static_vrf_new, static_vrf_enable,
203 static_vrf_disable, static_vrf_delete, NULL);
204
205 vrf_cmd_init(static_vrf_config_write, &static_privs);
206 }