]> git.proxmox.com Git - mirror_frr.git/blame - staticd/static_vrf.c
Merge pull request #8556 from donaldsharp/bgp_pbr_weird
[mirror_frr.git] / staticd / static_vrf.c
CommitLineData
7e24fdf3
DS
1/*
2 * STATICd - vrf code
3 * Copyright (C) 2018 Cumulus Networks, Inc.
4 * Donald Sharp
5 *
8d5cbee9
DS
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.
7e24fdf3 10 *
8d5cbee9
DS
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.
7e24fdf3
DS
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
7e24fdf3
DS
27#include "static_vrf.h"
28#include "static_routes.h"
fceb6174 29#include "static_zebra.h"
7e24fdf3
DS
30#include "static_vty.h"
31
88fa5104 32DEFINE_MTYPE_STATIC(STATIC, STATIC_RTABLE_INFO, "Static Route Table Info");
33
7e24fdf3
DS
34static struct static_vrf *static_vrf_alloc(void)
35{
36 struct route_table *table;
37 struct static_vrf *svrf;
88fa5104 38 struct stable_info *info;
7e24fdf3
DS
39 safi_t safi;
40 afi_t afi;
41
88fa5104 42 svrf = XCALLOC(MTYPE_STATIC_RTABLE_INFO, sizeof(struct static_vrf));
7e24fdf3
DS
43
44 for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
45 for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++) {
46 if (afi == AFI_IP6)
47 table = srcdest_table_init();
48 else
49 table = route_table_init();
88fa5104 50
51 info = XCALLOC(MTYPE_STATIC_RTABLE_INFO,
52 sizeof(struct stable_info));
53 info->svrf = svrf;
54 info->afi = afi;
55 info->safi = safi;
56 route_table_set_info(table, info);
57
7e24fdf3
DS
58 table->cleanup = zebra_stable_node_cleanup;
59 svrf->stable[afi][safi] = table;
60 }
61 }
62 return svrf;
63}
64
65static int static_vrf_new(struct vrf *vrf)
66{
67 struct static_vrf *svrf;
68
69 svrf = static_vrf_alloc();
70 vrf->info = svrf;
71 svrf->vrf = vrf;
72
73 return 0;
74}
75
76static int static_vrf_enable(struct vrf *vrf)
77{
fceb6174
PG
78 static_zebra_vrf_register(vrf);
79
7e24fdf3
DS
80 static_fixup_vrf_ids(vrf->info);
81
7e24fdf3
DS
82 return 0;
83}
84
85static int static_vrf_disable(struct vrf *vrf)
86{
fceb6174 87 static_zebra_vrf_unregister(vrf);
7e24fdf3
DS
88 return 0;
89}
90
91static int static_vrf_delete(struct vrf *vrf)
92{
93 struct route_table *table;
94 struct static_vrf *svrf;
95 safi_t safi;
96 afi_t afi;
88fa5104 97 void *info;
7e24fdf3
DS
98
99 svrf = vrf->info;
100 for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
101 for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++) {
102 table = svrf->stable[afi][safi];
88fa5104 103 info = route_table_get_info(table);
7e24fdf3 104 route_table_finish(table);
88fa5104 105 XFREE(MTYPE_STATIC_RTABLE_INFO, info);
7e24fdf3
DS
106 svrf->stable[afi][safi] = NULL;
107 }
108 }
88fa5104 109 XFREE(MTYPE_STATIC_RTABLE_INFO, svrf);
7e24fdf3
DS
110 return 0;
111}
112
113/* Lookup the static routing table in a VRF. */
114struct route_table *static_vrf_static_table(afi_t afi, safi_t safi,
115 struct static_vrf *svrf)
116{
117 if (!svrf)
118 return NULL;
119
120 if (afi >= AFI_MAX || safi >= SAFI_MAX)
121 return NULL;
122
123 return svrf->stable[afi][safi];
124}
125
126struct static_vrf *static_vrf_lookup_by_id(vrf_id_t vrf_id)
127{
128 struct vrf *vrf;
129
130 vrf = vrf_lookup_by_id(vrf_id);
131 if (vrf)
132 return ((struct static_vrf *)vrf->info);
133
134 return NULL;
135}
136
137struct static_vrf *static_vrf_lookup_by_name(const char *name)
138{
139 struct vrf *vrf;
140
141 if (!name)
142 name = VRF_DEFAULT_NAME;
143
144 vrf = vrf_lookup_by_name(name);
145 if (vrf)
146 return ((struct static_vrf *)vrf->info);
147
148 return NULL;
149}
150
151static int static_vrf_config_write(struct vty *vty)
152{
153 struct vrf *vrf;
154
155 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
0e20f68b
DS
156 if (vrf->vrf_id != VRF_DEFAULT)
157 vty_frame(vty, "vrf %s\n", vrf->name);
158
7e24fdf3
DS
159 static_config(vty, vrf->info, AFI_IP,
160 SAFI_UNICAST, "ip route");
161 static_config(vty, vrf->info, AFI_IP,
162 SAFI_MULTICAST, "ip mroute");
163 static_config(vty, vrf->info, AFI_IP6,
164 SAFI_UNICAST, "ipv6 route");
0e20f68b
DS
165
166 if (vrf->vrf_id != VRF_DEFAULT)
e8be380a 167 vty_endframe(vty, " exit-vrf\n!\n");
7e24fdf3
DS
168 }
169
170 return 0;
171}
172
7e24fdf3
DS
173void static_vrf_init(void)
174{
175 vrf_init(static_vrf_new, static_vrf_enable,
ecbc5a37 176 static_vrf_disable, static_vrf_delete, NULL);
7e24fdf3
DS
177
178 vrf_cmd_init(static_vrf_config_write, &static_privs);
179}
a4155a1b 180
181void static_vrf_terminate(void)
182{
183 vrf_terminate();
184}