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.h>
19 * Grouping of global data for the GBP source EPG classification feature
21 typedef struct gbp_policy_main_t_
24 * Next nodes for L2 output features
26 u32 l2_output_feat_next[32];
29 static gbp_policy_main_t gbp_policy_main;
31 #define foreach_gbp_policy \
36 #define _(sym,str) GBP_ERROR_##sym,
42 static char *gbp_policy_error_strings[] = {
43 #define _(sym,string) string,
50 #define _(sym,str) GBP_POLICY_NEXT_##sym,
57 * per-packet trace data
59 typedef struct gbp_policy_trace_t_
61 /* per-pkt trace data */
68 gbp_policy (vlib_main_t * vm,
69 vlib_node_runtime_t * node, vlib_frame_t * frame)
71 gbp_main_t *gm = &gbp_main;
72 gbp_policy_main_t *gpm = &gbp_policy_main;
73 u32 n_left_from, *from, *to_next;
77 n_left_from = frame->n_vectors;
78 from = vlib_frame_vector_args (frame);
80 while (n_left_from > 0)
84 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
86 while (n_left_from > 0 && n_left_to_next > 0)
88 const gbp_endpoint_t *gep0;
89 gbp_policy_next_t next0;
90 gbp_contract_key_t key0;
91 gbp_contract_value_t value0 = {
94 u32 bi0, sw_if_index0;
97 next0 = GBP_POLICY_NEXT_DENY;
105 b0 = vlib_get_buffer (vm, bi0);
108 * determine the src and dst EPG
110 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
111 gep0 = gbp_endpoint_get_itf (sw_if_index0);
112 key0.gck_dst = gep0->ge_epg_id;
113 key0.gck_src = vnet_buffer2 (b0)->gbp.src_epg;
115 if (EPG_INVALID != key0.gck_src)
117 if (PREDICT_FALSE (key0.gck_src == key0.gck_dst))
122 next0 = vnet_l2_feature_next (b0, gpm->l2_output_feat_next,
123 L2OUTPUT_FEAT_GBP_POLICY);
127 value0.as_u64 = gbp_acl_lookup (&key0);
129 if (~0 != value0.gc_lc_index)
131 fa_5tuple_opaque_t pkt_5tuple0;
133 u32 acl_pos_p0, acl_match_p0;
134 u32 rule_match_p0, trace_bitmap0;
139 l2_len0 = vnet_buffer (b0)->l2.l2_len;
140 h0 = vlib_buffer_get_current (b0);
143 clib_net_to_host_u16 (*(u16 *) (h0 + l2_len0 - 2));
145 is_ip60 = (ether_type0 == ETHERNET_TYPE_IP6) ? 1 : 0;
147 * tests against the ACL
149 acl_plugin_fill_5tuple_inline (gm->
150 acl_plugin.p_acl_main,
151 value0.gc_lc_index, b0,
156 acl_plugin_match_5tuple_inline (gm->
157 acl_plugin.p_acl_main,
159 &pkt_5tuple0, is_ip60,
160 &action0, &acl_pos_p0,
167 vnet_l2_feature_next (b0, gpm->l2_output_feat_next,
168 L2OUTPUT_FEAT_GBP_POLICY);
175 * the src EPG is not set when the packet arrives on an EPG
176 * uplink interface and we do not need to apply policy
178 next0 = vnet_l2_feature_next (b0, gpm->l2_output_feat_next,
179 L2OUTPUT_FEAT_GBP_POLICY);
182 if (PREDICT_FALSE ((b0->flags & VLIB_BUFFER_IS_TRACED)))
184 gbp_policy_trace_t *t =
185 vlib_add_trace (vm, node, b0, sizeof (*t));
186 t->src_epg = key0.gck_src;
187 t->dst_epg = key0.gck_dst;
188 t->acl_index = value0.gc_acl_index;
191 /* verify speculative enqueue, maybe switch current next frame */
192 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
193 to_next, n_left_to_next,
197 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
200 return frame->n_vectors;
203 /* packet trace format function */
205 format_gbp_policy_trace (u8 * s, va_list * args)
207 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
208 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
209 gbp_policy_trace_t *t = va_arg (*args, gbp_policy_trace_t *);
212 format (s, "src:%d, dst:%d, acl:%d", t->src_epg, t->dst_epg,
219 VLIB_REGISTER_NODE (gbp_policy_node) = {
220 .function = gbp_policy,
221 .name = "gbp-policy",
222 .vector_size = sizeof (u32),
223 .format_trace = format_gbp_policy_trace,
224 .type = VLIB_NODE_TYPE_INTERNAL,
226 .n_errors = ARRAY_LEN(gbp_policy_error_strings),
227 .error_strings = gbp_policy_error_strings,
229 .n_next_nodes = GBP_POLICY_N_NEXT,
232 [GBP_POLICY_NEXT_DENY] = "error-drop",
236 VLIB_NODE_FUNCTION_MULTIARCH (gbp_policy_node, gbp_policy);
240 static clib_error_t *
241 gbp_policy_init (vlib_main_t * vm)
243 gbp_policy_main_t *gpm = &gbp_policy_main;
244 clib_error_t *error = 0;
246 /* Initialize the feature next-node indexes */
247 feat_bitmap_init_next_nodes (vm,
248 gbp_policy_node.index,
250 l2output_get_feat_names (),
251 gpm->l2_output_feat_next);
256 VLIB_INIT_FUNCTION (gbp_policy_init);
259 * fd.io coding-style-patch-verification: ON
262 * eval: (c-set-style "gnu")