3 * Copyright (C) 2018 Cumulus Networks, Inc.
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)
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
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
25 #include "srcdest_table.h"
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"
33 static void zebra_stable_node_cleanup(struct route_table
*table
,
34 struct route_node
*node
)
36 struct static_route
*si
, *next
;
39 for (si
= node
->info
; si
; si
= next
) {
41 XFREE(MTYPE_STATIC_ROUTE
, si
);
45 static struct static_vrf
*static_vrf_alloc(void)
47 struct route_table
*table
;
48 struct static_vrf
*svrf
;
52 svrf
= XCALLOC(MTYPE_TMP
, sizeof(struct static_vrf
));
54 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++) {
55 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MULTICAST
; safi
++) {
57 table
= srcdest_table_init();
59 table
= route_table_init();
60 table
->cleanup
= zebra_stable_node_cleanup
;
61 svrf
->stable
[afi
][safi
] = table
;
67 static int static_vrf_new(struct vrf
*vrf
)
69 struct static_vrf
*svrf
;
71 svrf
= static_vrf_alloc();
78 static int static_vrf_enable(struct vrf
*vrf
)
80 static_zebra_vrf_register(vrf
);
82 static_fixup_vrf_ids(vrf
->info
);
85 * We may have static routes that are now possible to
86 * insert into the appropriate tables
88 static_config_install_delayed_routes(vrf
->info
);
93 static int static_vrf_disable(struct vrf
*vrf
)
95 static_zebra_vrf_unregister(vrf
);
99 static int static_vrf_delete(struct vrf
*vrf
)
101 struct route_table
*table
;
102 struct static_vrf
*svrf
;
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
;
114 XFREE(MTYPE_TMP
, svrf
);
118 /* Lookup the static routing table in a VRF. */
119 struct route_table
*static_vrf_static_table(afi_t afi
, safi_t safi
,
120 struct static_vrf
*svrf
)
125 if (afi
>= AFI_MAX
|| safi
>= SAFI_MAX
)
128 return svrf
->stable
[afi
][safi
];
131 struct static_vrf
*static_vrf_lookup_by_id(vrf_id_t vrf_id
)
135 vrf
= vrf_lookup_by_id(vrf_id
);
137 return ((struct static_vrf
*)vrf
->info
);
142 struct static_vrf
*static_vrf_lookup_by_name(const char *name
)
147 name
= VRF_DEFAULT_NAME
;
149 vrf
= vrf_lookup_by_name(name
);
151 return ((struct static_vrf
*)vrf
->info
);
156 static int static_vrf_config_write(struct vty
*vty
)
160 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
161 if (vrf
->vrf_id
!= VRF_DEFAULT
)
162 vty_frame(vty
, "vrf %s\n", vrf
->name
);
164 static_config(vty
, vrf
->info
, AFI_IP
,
165 SAFI_UNICAST
, "ip route");
166 static_config(vty
, vrf
->info
, AFI_IP
,
167 SAFI_MULTICAST
, "ip mroute");
168 static_config(vty
, vrf
->info
, AFI_IP6
,
169 SAFI_UNICAST
, "ipv6 route");
171 if (vrf
->vrf_id
!= VRF_DEFAULT
)
172 vty_endframe(vty
, " exit-vrf\n!\n");
178 int static_vrf_has_config(struct static_vrf
*svrf
)
180 struct route_table
*table
;
185 * NOTE: This is a don't care for the default VRF, but we go through
186 * the motions to keep things consistent.
188 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
189 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++) {
190 table
= svrf
->stable
[afi
][safi
];
193 if (route_table_count(table
))
201 void static_vrf_init(void)
203 vrf_init(static_vrf_new
, static_vrf_enable
,
204 static_vrf_disable
, static_vrf_delete
, NULL
);
206 vrf_cmd_init(static_vrf_config_write
, &static_privs
);
209 void static_vrf_terminate(void)