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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <plugins/gbp/gbp_recirc.h>
17 #include <plugins/gbp/gbp_endpoint_group.h>
18 #include <plugins/gbp/gbp_endpoint.h>
19 #include <plugins/gbp/gbp_itf.h>
21 #include <vnet/dpo/dvr_dpo.h>
22 #include <vnet/fib/fib_table.h>
27 gbp_recirc_t *gbp_recirc_pool;
30 * Recirc configs keyed by sw_if_index
32 index_t *gbp_recirc_db;
37 vlib_log_class_t gr_logger;
39 #define GBP_RECIRC_DBG(...) \
40 vlib_log_debug (gr_logger, __VA_ARGS__);
43 format_gbp_recirc (u8 * s, va_list * args)
45 gbp_recirc_t *gr = va_arg (*args, gbp_recirc_t *);
46 vnet_main_t *vnm = vnet_get_main ();
48 return format (s, " %U, epg:%d, ext:%d",
49 format_vnet_sw_if_index_name, vnm,
50 gr->gr_sw_if_index, gr->gr_epg, gr->gr_is_ext);
54 gbp_recirc_add (u32 sw_if_index, epg_id_t epg_id, u8 is_ext)
59 vec_validate_init_empty (gbp_recirc_db, sw_if_index, INDEX_INVALID);
61 gri = gbp_recirc_db[sw_if_index];
63 if (INDEX_INVALID == gri)
65 gbp_endpoint_group_t *gg;
66 fib_protocol_t fproto;
69 ggi = gbp_endpoint_group_find (epg_id);
71 if (INDEX_INVALID == ggi)
72 return (VNET_API_ERROR_NO_SUCH_ENTRY);
74 gbp_endpoint_group_lock (ggi);
75 pool_get (gbp_recirc_pool, gr);
76 clib_memset (gr, 0, sizeof (*gr));
77 gri = gr - gbp_recirc_pool;
80 gr->gr_is_ext = is_ext;
81 gr->gr_sw_if_index = sw_if_index;
84 * IP enable the recirc interface
86 ip4_sw_interface_enable_disable (gr->gr_sw_if_index, 1);
87 ip6_sw_interface_enable_disable (gr->gr_sw_if_index, 1);
90 * cache the FIB indicies of the EPG
94 gg = gbp_endpoint_group_get (gr->gr_epgi);
95 FOR_EACH_FIB_IP_PROTOCOL (fproto)
97 gr->gr_fib_index[fproto] =
98 gbp_endpoint_group_get_fib_index (gg, fproto);
102 * bind to the bridge-domain of the EPG
104 gr->gr_itf = gbp_itf_add_and_lock (gr->gr_sw_if_index, gg->gg_bd_index);
107 * Packets on the recirculation interface are subject to src-EPG
108 * classification. Recirc interfaces are L2-emulation mode.
109 * for internal EPGs this is via an LPM on all external subnets.
110 * for external EPGs this is via a port mapping.
116 * recirc is for post-NAT translation packets going into
117 * the external EPG, these are classified to the NAT EPG
120 mac_address_from_bytes (&mac,
121 vnet_sw_interface_get_hw_address
122 (vnet_get_main (), gr->gr_sw_if_index));
123 gbp_endpoint_update_and_lock (GBP_ENDPOINT_SRC_CP,
125 NULL, &mac, INDEX_INVALID,
126 INDEX_INVALID, gr->gr_epg,
127 GBP_ENDPOINT_FLAG_NONE,
128 NULL, NULL, &gr->gr_ep);
129 vnet_feature_enable_disable ("ip4-unicast",
130 "ip4-gbp-src-classify",
131 gr->gr_sw_if_index, 1, 0, 0);
132 vnet_feature_enable_disable ("ip6-unicast",
133 "ip6-gbp-src-classify",
134 gr->gr_sw_if_index, 1, 0, 0);
139 * recirc is for pre-NAT translation packets coming from
140 * the external EPG, these are classified based on a LPM
141 * in the EPG's route-domain
143 vnet_feature_enable_disable ("ip4-unicast",
144 "ip4-gbp-lpm-classify",
145 gr->gr_sw_if_index, 1, 0, 0);
146 vnet_feature_enable_disable ("ip6-unicast",
147 "ip6-gbp-lpm-classify",
148 gr->gr_sw_if_index, 1, 0, 0);
151 gbp_recirc_db[sw_if_index] = gri;
155 gr = gbp_recirc_get (gri);
158 GBP_RECIRC_DBG ("add: %U", format_gbp_recirc, gr);
163 gbp_recirc_delete (u32 sw_if_index)
168 gri = gbp_recirc_db[sw_if_index];
170 if (INDEX_INVALID != gri)
172 gr = pool_elt_at_index (gbp_recirc_pool, gri);
174 GBP_RECIRC_DBG ("del: %U", format_gbp_recirc, gr);
178 gbp_endpoint_unlock (GBP_ENDPOINT_SRC_CP, gr->gr_ep);
179 vnet_feature_enable_disable ("ip4-unicast",
180 "ip4-gbp-src-classify",
181 gr->gr_sw_if_index, 0, 0, 0);
182 vnet_feature_enable_disable ("ip6-unicast",
183 "ip6-gbp-src-classify",
184 gr->gr_sw_if_index, 0, 0, 0);
188 vnet_feature_enable_disable ("ip4-unicast",
189 "ip4-gbp-lpm-classify",
190 gr->gr_sw_if_index, 0, 0, 0);
191 vnet_feature_enable_disable ("ip6-unicast",
192 "ip6-gbp-lpm-classify",
193 gr->gr_sw_if_index, 0, 0, 0);
196 ip4_sw_interface_enable_disable (gr->gr_sw_if_index, 0);
197 ip6_sw_interface_enable_disable (gr->gr_sw_if_index, 0);
199 gbp_itf_unlock (gr->gr_itf);
201 gbp_endpoint_group_unlock (gr->gr_epgi);
202 gbp_recirc_db[sw_if_index] = INDEX_INVALID;
203 pool_put (gbp_recirc_pool, gr);
208 gbp_recirc_walk (gbp_recirc_cb_t cb, void *ctx)
213 pool_foreach(ge, gbp_recirc_pool,
222 gbp_recirc_show_one (gbp_recirc_t * gr, void *ctx)
224 vlib_cli_output (ctx, " %U", format_gbp_recirc, gr);
229 static clib_error_t *
230 gbp_recirc_show (vlib_main_t * vm,
231 unformat_input_t * input, vlib_cli_command_t * cmd)
233 vlib_cli_output (vm, "Recirculation-Interfaces:");
234 gbp_recirc_walk (gbp_recirc_show_one, vm);
240 * Show Group Based Policy Recircs and derived information
243 * @cliexstart{show gbp recirc}
247 VLIB_CLI_COMMAND (gbp_recirc_show_node, static) = {
248 .path = "show gbp recirc",
249 .short_help = "show gbp recirc\n",
250 .function = gbp_recirc_show,
254 static clib_error_t *
255 gbp_recirc_init (vlib_main_t * vm)
257 gr_logger = vlib_log_register_class ("gbp", "recirc");
262 VLIB_INIT_FUNCTION (gbp_recirc_init);
265 * fd.io coding-style-patch-verification: ON
268 * eval: (c-set-style "gnu")