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 <vnet/dpo/dvr_dpo.h>
17 #include <vnet/dpo/drop_dpo.h>
18 #include <vnet/vxlan-gbp/vxlan_gbp_packet.h>
19 #include <vnet/vxlan-gbp/vxlan_gbp.h>
21 #include <plugins/gbp/gbp.h>
22 #include <plugins/gbp/gbp_policy_dpo.h>
23 #include <plugins/gbp/gbp_recirc.h>
25 #ifndef CLIB_MARCH_VARIANT
29 gbp_policy_dpo_t *gbp_policy_dpo_pool;
32 * DPO type registered for these GBP FWD
34 dpo_type_t gbp_policy_dpo_type;
36 static gbp_policy_dpo_t *
37 gbp_policy_dpo_alloc (void)
39 gbp_policy_dpo_t *gpd;
41 pool_get_aligned_zero (gbp_policy_dpo_pool, gpd, CLIB_CACHE_LINE_BYTES);
46 static inline gbp_policy_dpo_t *
47 gbp_policy_dpo_get_from_dpo (const dpo_id_t * dpo)
49 ASSERT (gbp_policy_dpo_type == dpo->dpoi_type);
51 return (gbp_policy_dpo_get (dpo->dpoi_index));
55 gbp_policy_dpo_get_index (gbp_policy_dpo_t * gpd)
57 return (gpd - gbp_policy_dpo_pool);
61 gbp_policy_dpo_lock (dpo_id_t * dpo)
63 gbp_policy_dpo_t *gpd;
65 gpd = gbp_policy_dpo_get_from_dpo (dpo);
70 gbp_policy_dpo_unlock (dpo_id_t * dpo)
72 gbp_policy_dpo_t *gpd;
74 gpd = gbp_policy_dpo_get_from_dpo (dpo);
77 if (0 == gpd->gpd_locks)
79 dpo_reset (&gpd->gpd_dpo);
80 pool_put (gbp_policy_dpo_pool, gpd);
85 gbp_policy_dpo_get_urpf (const dpo_id_t * dpo)
87 gbp_policy_dpo_t *gpd;
89 gpd = gbp_policy_dpo_get_from_dpo (dpo);
91 return (gpd->gpd_sw_if_index);
95 gbp_policy_dpo_add_or_lock (dpo_proto_t dproto,
96 sclass_t sclass, u32 sw_if_index, dpo_id_t * dpo)
98 gbp_policy_dpo_t *gpd;
99 dpo_id_t parent = DPO_INVALID;
101 gpd = gbp_policy_dpo_alloc ();
103 gpd->gpd_proto = dproto;
104 gpd->gpd_sw_if_index = sw_if_index;
105 gpd->gpd_sclass = sclass;
107 if (~0 != sw_if_index)
110 * stack on the DVR DPO for the output interface
112 dvr_dpo_add_or_lock (sw_if_index, dproto, &parent);
116 dpo_copy (&parent, drop_dpo_get (dproto));
119 dpo_stack (gbp_policy_dpo_type, dproto, &gpd->gpd_dpo, &parent);
120 dpo_set (dpo, gbp_policy_dpo_type, dproto, gbp_policy_dpo_get_index (gpd));
124 format_gbp_policy_dpo (u8 * s, va_list * ap)
126 index_t index = va_arg (*ap, index_t);
127 u32 indent = va_arg (*ap, u32);
128 gbp_policy_dpo_t *gpd = gbp_policy_dpo_get (index);
129 vnet_main_t *vnm = vnet_get_main ();
131 s = format (s, "gbp-policy-dpo: %U, sclass:%d out:%U",
132 format_dpo_proto, gpd->gpd_proto,
133 (int) gpd->gpd_sclass,
134 format_vnet_sw_if_index_name, vnm, gpd->gpd_sw_if_index);
135 s = format (s, "\n%U", format_white_space, indent + 2);
136 s = format (s, "%U", format_dpo_id, &gpd->gpd_dpo, indent + 4);
142 * Interpose a policy DPO
145 gbp_policy_dpo_interpose (const dpo_id_t * original,
146 const dpo_id_t * parent, dpo_id_t * clone)
148 gbp_policy_dpo_t *gpd, *gpd_clone;
150 gpd_clone = gbp_policy_dpo_alloc ();
151 gpd = gbp_policy_dpo_get (original->dpoi_index);
153 gpd_clone->gpd_proto = gpd->gpd_proto;
154 gpd_clone->gpd_sclass = gpd->gpd_sclass;
155 gpd_clone->gpd_sw_if_index = gpd->gpd_sw_if_index;
158 * if no interface is provided, grab one from the parent
161 if (~0 == gpd_clone->gpd_sw_if_index)
162 gpd_clone->gpd_sw_if_index = dpo_get_urpf (parent);
164 dpo_stack (gbp_policy_dpo_type,
165 gpd_clone->gpd_proto, &gpd_clone->gpd_dpo, parent);
169 gpd_clone->gpd_proto, gbp_policy_dpo_get_index (gpd_clone));
172 const static dpo_vft_t gbp_policy_dpo_vft = {
173 .dv_lock = gbp_policy_dpo_lock,
174 .dv_unlock = gbp_policy_dpo_unlock,
175 .dv_format = format_gbp_policy_dpo,
176 .dv_get_urpf = gbp_policy_dpo_get_urpf,
177 .dv_mk_interpose = gbp_policy_dpo_interpose,
181 * @brief The per-protocol VLIB graph nodes that are assigned to a glean
184 * this means that these graph nodes are ones from which a glean is the
185 * parent object in the DPO-graph.
187 const static char *const gbp_policy_dpo_ip4_nodes[] = {
188 "ip4-gbp-policy-dpo",
192 const static char *const gbp_policy_dpo_ip6_nodes[] = {
193 "ip6-gbp-policy-dpo",
197 const static char *const *const gbp_policy_dpo_nodes[DPO_PROTO_NUM] = {
198 [DPO_PROTO_IP4] = gbp_policy_dpo_ip4_nodes,
199 [DPO_PROTO_IP6] = gbp_policy_dpo_ip6_nodes,
203 gbp_policy_dpo_get_type (void)
205 return (gbp_policy_dpo_type);
208 static clib_error_t *
209 gbp_policy_dpo_module_init (vlib_main_t * vm)
211 gbp_policy_dpo_type = dpo_register_new_type (&gbp_policy_dpo_vft,
212 gbp_policy_dpo_nodes);
217 VLIB_INIT_FUNCTION (gbp_policy_dpo_module_init);
218 #endif /* CLIB_MARCH_VARIANT */
220 typedef struct gbp_policy_dpo_trace_t_
227 } gbp_policy_dpo_trace_t;
236 gbp_rule_l3_redirect (const gbp_rule_t * gu, vlib_buffer_t * b0, int is_ip6)
238 gbp_policy_node_t pnode;
242 pnode = (is_ip6 ? GBP_POLICY_NODE_IP6 : GBP_POLICY_NODE_IP4);
243 dproto = (is_ip6 ? DPO_PROTO_IP6 : DPO_PROTO_IP4);
244 dpo = &gu->gu_dpo[pnode][dproto];
246 /* The flow hash is still valid as this is a IP packet being switched */
247 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = dpo->dpoi_index;
249 return (dpo->dpoi_next_node);
253 gbp_policy_dpo_inline (vlib_main_t * vm,
254 vlib_node_runtime_t * node,
255 vlib_frame_t * from_frame, u8 is_ip6)
257 gbp_main_t *gm = &gbp_main;
258 u32 n_left_from, next_index, *from, *to_next;
261 from = vlib_frame_vector_args (from_frame);
262 n_left_from = from_frame->n_vectors;
264 next_index = node->cached_next_index;
266 while (n_left_from > 0)
270 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
272 while (n_left_from > 0 && n_left_to_next > 0)
274 const gbp_policy_dpo_t *gpd0;
276 gbp_contract_key_t key0;
289 next0 = GBP_POLICY_DROP;
291 b0 = vlib_get_buffer (vm, bi0);
294 gpd0 = gbp_policy_dpo_get (vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
295 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = gpd0->gpd_dpo.dpoi_index;
298 * Reflection check; in and out on an ivxlan tunnel
300 if ((~0 != vxlan_gbp_tunnel_by_sw_if_index (gpd0->gpd_sw_if_index))
301 && (vnet_buffer2 (b0)->gbp.flags & VXLAN_GBP_GPFLAGS_R))
306 if (vnet_buffer2 (b0)->gbp.flags & VXLAN_GBP_GPFLAGS_A)
308 next0 = gpd0->gpd_dpo.dpoi_next_node;
313 key0.gck_src = vnet_buffer2 (b0)->gbp.sclass;
314 key0.gck_dst = gpd0->gpd_sclass;
316 if (SCLASS_INVALID != key0.gck_src)
318 if (PREDICT_FALSE (key0.gck_src == key0.gck_dst))
323 next0 = gpd0->gpd_dpo.dpoi_next_node;
324 vnet_buffer2 (b0)->gbp.flags |= VXLAN_GBP_GPFLAGS_A;
329 gci0 = gbp_contract_find (&key0);
331 if (INDEX_INVALID != gci0)
333 fa_5tuple_opaque_t pkt_5tuple0;
334 u32 acl_pos_p0, acl_match_p0;
335 u32 rule_match_p0, trace_bitmap0;
337 * tests against the ACL
339 gc0 = gbp_contract_get (gci0);
340 acl_plugin_fill_5tuple_inline (gm->
341 acl_plugin.p_acl_main,
342 gc0->gc_lc_index, b0,
347 acl_plugin_match_5tuple_inline (gm->
348 acl_plugin.p_acl_main,
350 &pkt_5tuple0, is_ip6,
351 &action0, &acl_pos_p0,
358 vnet_buffer2 (b0)->gbp.flags |= VXLAN_GBP_GPFLAGS_A;
359 gu = gbp_rule_get (gc0->gc_rules[rule_match_p0]);
360 action0 = gu->gu_action;
362 switch (gu->gu_action)
364 case GBP_RULE_PERMIT:
365 next0 = gpd0->gpd_dpo.dpoi_next_node;
370 case GBP_RULE_REDIRECT:
371 next0 = gbp_rule_l3_redirect (gu, b0, is_ip6);
381 * the src EPG is not set when the packet arrives on an EPG
382 * uplink interface and we do not need to apply policy
384 next0 = gpd0->gpd_dpo.dpoi_next_node;
387 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
389 gbp_policy_dpo_trace_t *tr;
391 tr = vlib_add_trace (vm, node, b0, sizeof (*tr));
392 tr->sclass = key0.gck_src;
393 tr->dclass = key0.gck_dst;
394 tr->acl_index = (gc0 ? gc0->gc_acl_index : ~0);
395 tr->a_bit = vnet_buffer2 (b0)->gbp.flags & VXLAN_GBP_GPFLAGS_A;
396 tr->action = action0;
399 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
400 n_left_to_next, bi0, next0);
402 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
404 return from_frame->n_vectors;
408 format_gbp_policy_dpo_trace (u8 * s, va_list * args)
410 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
411 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
412 gbp_policy_dpo_trace_t *t = va_arg (*args, gbp_policy_dpo_trace_t *);
414 s = format (s, " sclass:%d dclass:%d acl-index:%d a-bit:%d action:%d",
415 t->sclass, t->dclass, t->acl_index, t->a_bit, t->action);
420 VLIB_NODE_FN (ip4_gbp_policy_dpo_node) (vlib_main_t * vm,
421 vlib_node_runtime_t * node,
422 vlib_frame_t * from_frame)
424 return (gbp_policy_dpo_inline (vm, node, from_frame, 0));
427 VLIB_NODE_FN (ip6_gbp_policy_dpo_node) (vlib_main_t * vm,
428 vlib_node_runtime_t * node,
429 vlib_frame_t * from_frame)
431 return (gbp_policy_dpo_inline (vm, node, from_frame, 1));
435 VLIB_REGISTER_NODE (ip4_gbp_policy_dpo_node) = {
436 .name = "ip4-gbp-policy-dpo",
437 .vector_size = sizeof (u32),
438 .format_trace = format_gbp_policy_dpo_trace,
439 .n_next_nodes = GBP_POLICY_N_NEXT,
442 [GBP_POLICY_DROP] = "ip4-drop",
445 VLIB_REGISTER_NODE (ip6_gbp_policy_dpo_node) = {
446 .name = "ip6-gbp-policy-dpo",
447 .vector_size = sizeof (u32),
448 .format_trace = format_gbp_policy_dpo_trace,
449 .n_next_nodes = GBP_POLICY_N_NEXT,
452 [GBP_POLICY_DROP] = "ip6-drop",
458 * fd.io coding-style-patch-verification: ON
461 * eval: (c-set-style "gnu")