GBP Endpoint Updates
[vpp.git] / src / plugins / gbp / gbp_recirc.c
1 /*
2  * Copyright (c) 2018 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <plugins/gbp/gbp_recirc.h>
17 #include <plugins/gbp/gbp_endpoint_group.h>
18 #include <plugins/gbp/gbp_endpoint.h>
19
20 #include <vnet/dpo/dvr_dpo.h>
21 #include <vnet/fib/fib_table.h>
22
23 /**
24  * Pool of GBP recircs
25  */
26 gbp_recirc_t *gbp_recirc_pool;
27
28 /**
29  * Recirc configs keyed by sw_if_index
30  */
31 index_t *gbp_recirc_db;
32
33 int
34 gbp_recirc_add (u32 sw_if_index, epg_id_t epg_id, u8 is_ext)
35 {
36   gbp_recirc_t *gr;
37   index_t gri;
38
39   vec_validate_init_empty (gbp_recirc_db, sw_if_index, INDEX_INVALID);
40
41   gri = gbp_recirc_db[sw_if_index];
42
43   if (INDEX_INVALID == gri)
44     {
45       gbp_endpoint_group_t *gepg;
46       fib_protocol_t fproto;
47
48       pool_get (gbp_recirc_pool, gr);
49       memset (gr, 0, sizeof (*gr));
50       gri = gr - gbp_recirc_pool;
51
52       gr->gr_epg = epg_id;
53       gr->gr_is_ext = is_ext;
54       gr->gr_sw_if_index = sw_if_index;
55
56       /*
57        * IP enable the recirc interface
58        */
59       ip4_sw_interface_enable_disable (gr->gr_sw_if_index, 1);
60       ip6_sw_interface_enable_disable (gr->gr_sw_if_index, 1);
61
62       /*
63        * cache the FIB indicies of the EPG
64        */
65       gepg = gbp_endpoint_group_find (gr->gr_epg);
66
67       if (NULL == gepg)
68         return (VNET_API_ERROR_NO_SUCH_ENTRY);
69
70       FOR_EACH_FIB_IP_PROTOCOL (fproto)
71       {
72         gr->gr_fib_index[fproto] = gepg->gepg_fib_index[fproto];
73       }
74
75       /*
76        * Packets on the recirculation interface are subjet to src-EPG
77        * classification. Recirc interfaces are L2-emulation mode.
78        *   for internal EPGs this is via an LPM on all external subnets.
79        *   for external EPGs this is via a port mapping.
80        */
81       if (gr->gr_is_ext)
82         {
83           /*
84            * recirc is for post-NAT translation packets going into
85            * the external EPG, these are classified to the NAT EPG
86            * based on its port
87            */
88           gbp_endpoint_update (gr->gr_sw_if_index,
89                                NULL, NULL, gr->gr_epg, &gr->gr_ep);
90           vnet_feature_enable_disable ("ip4-unicast",
91                                        "ip4-gbp-src-classify",
92                                        gr->gr_sw_if_index, 1, 0, 0);
93           vnet_feature_enable_disable ("ip6-unicast",
94                                        "ip6-gbp-src-classify",
95                                        gr->gr_sw_if_index, 1, 0, 0);
96         }
97       else
98         {
99           /*
100            * recirc is for pre-NAT translation packets coming from
101            * the external EPG, these are classified based on a LPM
102            * in the EPG's route-domain
103            */
104           vnet_feature_enable_disable ("ip4-unicast",
105                                        "ip4-gbp-lpm-classify",
106                                        gr->gr_sw_if_index, 1, 0, 0);
107           vnet_feature_enable_disable ("ip6-unicast",
108                                        "ip6-gbp-lpm-classify",
109                                        gr->gr_sw_if_index, 1, 0, 0);
110         }
111
112       gbp_recirc_db[sw_if_index] = gri;
113     }
114
115   return (0);
116 }
117
118 void
119 gbp_recirc_delete (u32 sw_if_index)
120 {
121   gbp_recirc_t *gr;
122   index_t gri;
123
124   gri = gbp_recirc_db[sw_if_index];
125
126   if (INDEX_INVALID != gri)
127     {
128       gr = pool_elt_at_index (gbp_recirc_pool, gri);
129
130       if (gr->gr_is_ext)
131         {
132           gbp_endpoint_delete (gr->gr_ep);
133           vnet_feature_enable_disable ("ip4-unicast",
134                                        "ip4-gbp-src-classify",
135                                        gr->gr_sw_if_index, 0, 0, 0);
136           vnet_feature_enable_disable ("ip6-unicast",
137                                        "ip6-gbp-src-classify",
138                                        gr->gr_sw_if_index, 0, 0, 0);
139         }
140       else
141         {
142           vnet_feature_enable_disable ("ip4-unicast",
143                                        "ip4-gbp-lpm-classify",
144                                        gr->gr_sw_if_index, 0, 0, 0);
145           vnet_feature_enable_disable ("ip6-unicast",
146                                        "ip6-gbp-lpm-classify",
147                                        gr->gr_sw_if_index, 0, 0, 0);
148         }
149
150       ip4_sw_interface_enable_disable (gr->gr_sw_if_index, 0);
151       ip6_sw_interface_enable_disable (gr->gr_sw_if_index, 0);
152
153       gbp_recirc_db[sw_if_index] = INDEX_INVALID;
154       pool_put (gbp_recirc_pool, gr);
155     }
156 }
157
158 void
159 gbp_recirc_walk (gbp_recirc_cb_t cb, void *ctx)
160 {
161   gbp_recirc_t *gbpe;
162
163   /* *INDENT-OFF* */
164   pool_foreach(gbpe, gbp_recirc_pool,
165   {
166     if (!cb(gbpe, ctx))
167       break;
168   });
169   /* *INDENT-ON* */
170 }
171
172 static int
173 gbp_recirc_show_one (gbp_recirc_t * gr, void *ctx)
174 {
175   vnet_main_t *vnm = vnet_get_main ();
176   vlib_main_t *vm;
177
178   vm = ctx;
179   vlib_cli_output (vm, "  %U, epg:%d, ext:%d",
180                    format_vnet_sw_if_index_name, vnm,
181                    gr->gr_sw_if_index, gr->gr_epg, gr->gr_is_ext);
182
183   return (1);
184 }
185
186 static clib_error_t *
187 gbp_recirc_show (vlib_main_t * vm,
188                  unformat_input_t * input, vlib_cli_command_t * cmd)
189 {
190   vlib_cli_output (vm, "Recirculation-Interfaces:");
191   gbp_recirc_walk (gbp_recirc_show_one, vm);
192
193   return (NULL);
194 }
195
196
197 /*?
198  * Show Group Based Policy Recircs and derived information
199  *
200  * @cliexpar
201  * @cliexstart{show gbp recirc}
202  * @cliexend
203  ?*/
204 /* *INDENT-OFF* */
205 VLIB_CLI_COMMAND (gbp_recirc_show_node, static) = {
206   .path = "show gbp recirc",
207   .short_help = "show gbp recirc\n",
208   .function = gbp_recirc_show,
209 };
210 /* *INDENT-ON* */
211
212 /*
213  * fd.io coding-style-patch-verification: ON
214  *
215  * Local Variables:
216  * eval: (c-set-style "gnu")
217  * End:
218  */