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_vty.h"
32 static void zebra_stable_node_cleanup(struct route_table
*table
,
33 struct route_node
*node
)
35 struct static_route
*si
, *next
;
38 for (si
= node
->info
; si
; si
= next
) {
40 XFREE(MTYPE_STATIC_ROUTE
, si
);
44 static struct static_vrf
*static_vrf_alloc(void)
46 struct route_table
*table
;
47 struct static_vrf
*svrf
;
51 svrf
= XCALLOC(MTYPE_TMP
, sizeof(struct static_vrf
));
53 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++) {
54 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MULTICAST
; safi
++) {
56 table
= srcdest_table_init();
58 table
= route_table_init();
59 table
->cleanup
= zebra_stable_node_cleanup
;
60 svrf
->stable
[afi
][safi
] = table
;
66 static int static_vrf_new(struct vrf
*vrf
)
68 struct static_vrf
*svrf
;
70 svrf
= static_vrf_alloc();
77 static int static_vrf_enable(struct vrf
*vrf
)
79 static_fixup_vrf_ids(vrf
->info
);
82 * We may have static routes that are now possible to
83 * insert into the appropriate tables
85 static_config_install_delayed_routes(vrf
->info
);
90 static int static_vrf_disable(struct vrf
*vrf
)
95 static int static_vrf_delete(struct vrf
*vrf
)
97 struct route_table
*table
;
98 struct static_vrf
*svrf
;
103 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++) {
104 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MULTICAST
; safi
++) {
105 table
= svrf
->stable
[afi
][safi
];
106 route_table_finish(table
);
107 svrf
->stable
[afi
][safi
] = NULL
;
113 /* Lookup the static routing table in a VRF. */
114 struct route_table
*static_vrf_static_table(afi_t afi
, safi_t safi
,
115 struct static_vrf
*svrf
)
120 if (afi
>= AFI_MAX
|| safi
>= SAFI_MAX
)
123 return svrf
->stable
[afi
][safi
];
126 struct static_vrf
*static_vrf_lookup_by_id(vrf_id_t vrf_id
)
130 vrf
= vrf_lookup_by_id(vrf_id
);
132 return ((struct static_vrf
*)vrf
->info
);
137 struct static_vrf
*static_vrf_lookup_by_name(const char *name
)
142 name
= VRF_DEFAULT_NAME
;
144 vrf
= vrf_lookup_by_name(name
);
146 return ((struct static_vrf
*)vrf
->info
);
151 static int static_vrf_config_write(struct vty
*vty
)
155 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
156 if (vrf
->vrf_id
!= VRF_DEFAULT
)
157 vty_frame(vty
, "vrf %s\n", vrf
->name
);
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");
166 if (vrf
->vrf_id
!= VRF_DEFAULT
)
167 vty_endframe(vty
, "!\n");
173 int static_vrf_has_config(struct static_vrf
*svrf
)
175 struct route_table
*table
;
180 * NOTE: This is a don't care for the default VRF, but we go through
181 * the motions to keep things consistent.
183 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
184 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++) {
185 table
= svrf
->stable
[afi
][safi
];
188 if (route_table_count(table
))
196 void static_vrf_init(void)
198 vrf_init(static_vrf_new
, static_vrf_enable
,
199 static_vrf_disable
, static_vrf_delete
, NULL
);
201 vrf_cmd_init(static_vrf_config_write
, &static_privs
);