]>
Commit | Line | Data |
---|---|---|
be3b67b5 SW |
1 | /* |
2 | * PBR - vrf code | |
3 | * Copyright (C) 2019 Cumulus Networks, Inc. | |
4 | * Stephen Worley | |
5 | * | |
6 | * FRR 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 | |
8 | * Free Software Foundation; either version 2, or (at your option) any | |
9 | * later version. | |
10 | * | |
11 | * FRR is distributed in the hope that it will be useful, but | |
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * General Public License for 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 | ||
24 | #include "pbr_vrf.h" | |
25 | #include "pbr_memory.h" | |
26 | #include "pbr_map.h" | |
27 | #include "pbr_debug.h" | |
fcf29c69 | 28 | #include "pbr_nht.h" |
0e7d7358 | 29 | #include "pbr_zebra.h" |
be3b67b5 | 30 | |
bf8d3d6a | 31 | DEFINE_MTYPE_STATIC(PBRD, PBR_MAP_VRF, "PBR Map VRF"); |
be3b67b5 SW |
32 | |
33 | static struct pbr_vrf *pbr_vrf_alloc(void) | |
34 | { | |
35 | struct pbr_vrf *pbr_vrf; | |
36 | ||
37 | pbr_vrf = XCALLOC(MTYPE_PBR_MAP_VRF, sizeof(struct pbr_vrf)); | |
38 | ||
39 | return pbr_vrf; | |
40 | } | |
41 | ||
42 | static void pbr_vrf_free(struct pbr_vrf *pbr_vrf) | |
43 | { | |
44 | XFREE(MTYPE_PBR_MAP_VRF, pbr_vrf); | |
45 | } | |
46 | ||
47 | static int pbr_vrf_new(struct vrf *vrf) | |
48 | { | |
49 | struct pbr_vrf *pbr_vrf; | |
50 | ||
51 | DEBUGD(&pbr_dbg_event, "%s: %u (%s)", __func__, vrf->vrf_id, vrf->name); | |
52 | ||
53 | pbr_vrf = pbr_vrf_alloc(); | |
54 | vrf->info = pbr_vrf; | |
55 | pbr_vrf->vrf = vrf; | |
56 | ||
57 | return 0; | |
58 | } | |
59 | ||
60 | static int pbr_vrf_enable(struct vrf *vrf) | |
61 | { | |
62 | DEBUGD(&pbr_dbg_event, "%s: %u (%s)", __func__, vrf->vrf_id, vrf->name); | |
63 | ||
fcf29c69 | 64 | pbr_nht_vrf_update(vrf->info); |
be3b67b5 SW |
65 | pbr_map_vrf_update(vrf->info); |
66 | ||
67 | return 0; | |
68 | } | |
69 | ||
70 | static int pbr_vrf_disable(struct vrf *vrf) | |
71 | { | |
72 | DEBUGD(&pbr_dbg_event, "%s: %u (%s)", __func__, vrf->vrf_id, vrf->name); | |
73 | ||
74 | pbr_map_vrf_update(vrf->info); | |
75 | ||
76 | return 0; | |
77 | } | |
78 | ||
79 | static int pbr_vrf_delete(struct vrf *vrf) | |
80 | { | |
81 | DEBUGD(&pbr_dbg_event, "%s: %u (%s)", __func__, vrf->vrf_id, vrf->name); | |
82 | ||
83 | /* | |
84 | * Make sure vrf is always marked disabled first so we handle | |
85 | * pbr rules using it. | |
86 | */ | |
87 | assert(!vrf_is_enabled(vrf)); | |
88 | ||
89 | pbr_vrf_free(vrf->info); | |
90 | vrf->info = NULL; | |
91 | ||
92 | return 0; | |
93 | } | |
94 | ||
be3b67b5 SW |
95 | struct pbr_vrf *pbr_vrf_lookup_by_name(const char *name) |
96 | { | |
97 | struct vrf *vrf; | |
98 | ||
99 | if (!name) | |
100 | name = VRF_DEFAULT_NAME; | |
101 | ||
102 | vrf = vrf_lookup_by_name(name); | |
103 | if (vrf) | |
104 | return ((struct pbr_vrf *)vrf->info); | |
105 | ||
106 | return NULL; | |
107 | } | |
108 | ||
109 | bool pbr_vrf_is_enabled(const struct pbr_vrf *pbr_vrf) | |
110 | { | |
111 | return vrf_is_enabled(pbr_vrf->vrf) ? true : false; | |
112 | } | |
113 | ||
114 | bool pbr_vrf_is_valid(const struct pbr_vrf *pbr_vrf) | |
115 | { | |
116 | if (vrf_is_backend_netns()) | |
117 | return false; | |
118 | ||
119 | if (!pbr_vrf->vrf) | |
120 | return false; | |
121 | ||
122 | return pbr_vrf_is_enabled(pbr_vrf); | |
123 | } | |
124 | ||
125 | void pbr_vrf_init(void) | |
126 | { | |
ac2cb9bf | 127 | vrf_init(pbr_vrf_new, pbr_vrf_enable, pbr_vrf_disable, pbr_vrf_delete); |
be3b67b5 | 128 | } |
0e7d7358 DS |
129 | |
130 | void pbr_vrf_terminate(void) | |
131 | { | |
132 | struct vrf *vrf; | |
133 | struct interface *ifp; | |
134 | ||
135 | RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) { | |
136 | FOR_ALL_INTERFACES (vrf, ifp) | |
137 | pbr_if_del(ifp); | |
138 | } | |
139 | } |