2 * gbp.h : Group Based Policy
4 * Copyright (c) 2018 Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #include <plugins/gbp/gbp.h>
19 #include <plugins/gbp/gbp_classify.h>
20 #include <plugins/gbp/gbp_policy_dpo.h>
21 #include <plugins/gbp/gbp_ext_itf.h>
22 #include <vnet/fib/ip4_fib.h>
23 #include <vnet/fib/ip6_fib.h>
24 #include <vnet/dpo/load_balance.h>
25 #include <vnet/l2/l2_input.h>
26 #include <vnet/l2/feat_bitmap.h>
27 #include <vnet/fib/fib_table.h>
28 #include <vnet/vxlan-gbp/vxlan_gbp_packet.h>
29 #include <vnet/ethernet/arp_packet.h>
32 * per-packet trace data
34 typedef struct gbp_classify_trace_t_
36 /* per-pkt trace data */
38 } gbp_classify_trace_t;
41 * determine the SRC EPG form the input port
44 gbp_classify_inline (vlib_main_t * vm,
45 vlib_node_runtime_t * node,
47 gbp_src_classify_type_t type, dpo_proto_t dproto)
49 gbp_src_classify_main_t *gscm = &gbp_src_classify_main;
50 u32 n_left_from, *from, *to_next;
54 n_left_from = frame->n_vectors;
55 from = vlib_frame_vector_args (frame);
57 while (n_left_from > 0)
61 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
63 while (n_left_from > 0 && n_left_to_next > 0)
65 u32 next0, bi0, sw_if_index0;
66 const gbp_endpoint_t *ge0;
77 b0 = vlib_get_buffer (vm, bi0);
79 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
80 vnet_buffer2 (b0)->gbp.flags = VXLAN_GBP_GPFLAGS_NONE;
82 if (GBP_SRC_CLASSIFY_NULL == type)
84 sclass0 = SCLASS_INVALID;
86 vnet_l2_feature_next (b0, gscm->l2_input_feat_next[type],
87 L2INPUT_FEAT_GBP_NULL_CLASSIFY);
91 if (DPO_PROTO_ETHERNET == dproto)
93 const ethernet_header_t *h0;
95 h0 = vlib_buffer_get_current (b0);
97 vnet_l2_feature_next (b0, gscm->l2_input_feat_next[type],
98 L2INPUT_FEAT_GBP_SRC_CLASSIFY);
99 ge0 = gbp_endpoint_find_mac (h0->src_address,
100 vnet_buffer (b0)->l2.bd_index);
102 else if (DPO_PROTO_IP4 == dproto)
104 const ip4_header_t *h0;
106 h0 = vlib_buffer_get_current (b0);
108 ge0 = gbp_endpoint_find_ip4
110 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
115 * Go straight to looukp, do not pass go, do not collect $200
119 else if (DPO_PROTO_IP6 == dproto)
121 const ip6_header_t *h0;
123 h0 = vlib_buffer_get_current (b0);
125 ge0 = gbp_endpoint_find_ip6
127 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP6,
132 * Go straight to lookup, do not pass go, do not collect $200
143 if (PREDICT_TRUE (NULL != ge0))
144 sclass0 = ge0->ge_fwd.gef_sclass;
146 sclass0 = SCLASS_INVALID;
149 vnet_buffer2 (b0)->gbp.sclass = sclass0;
151 if (PREDICT_FALSE ((b0->flags & VLIB_BUFFER_IS_TRACED)))
153 gbp_classify_trace_t *t =
154 vlib_add_trace (vm, node, b0, sizeof (*t));
158 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
159 to_next, n_left_to_next,
163 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
166 return frame->n_vectors;
169 VLIB_NODE_FN (gbp_src_classify_node) (vlib_main_t * vm,
170 vlib_node_runtime_t * node,
171 vlib_frame_t * frame)
173 return (gbp_classify_inline (vm, node, frame,
174 GBP_SRC_CLASSIFY_PORT, DPO_PROTO_ETHERNET));
177 VLIB_NODE_FN (gbp_null_classify_node) (vlib_main_t * vm,
178 vlib_node_runtime_t * node,
179 vlib_frame_t * frame)
181 return (gbp_classify_inline (vm, node, frame,
182 GBP_SRC_CLASSIFY_NULL, DPO_PROTO_ETHERNET));
185 VLIB_NODE_FN (gbp_ip4_src_classify_node) (vlib_main_t * vm,
186 vlib_node_runtime_t * node,
187 vlib_frame_t * frame)
189 return (gbp_classify_inline (vm, node, frame,
190 GBP_SRC_CLASSIFY_PORT, DPO_PROTO_IP4));
193 VLIB_NODE_FN (gbp_ip6_src_classify_node) (vlib_main_t * vm,
194 vlib_node_runtime_t * node,
195 vlib_frame_t * frame)
197 return (gbp_classify_inline (vm, node, frame,
198 GBP_SRC_CLASSIFY_PORT, DPO_PROTO_IP6));
202 /* packet trace format function */
204 format_gbp_classify_trace (u8 * s, va_list * args)
206 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
207 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
208 gbp_classify_trace_t *t = va_arg (*args, gbp_classify_trace_t *);
210 s = format (s, "sclass:%d", t->sclass);
216 VLIB_REGISTER_NODE (gbp_null_classify_node) = {
217 .name = "gbp-null-classify",
218 .vector_size = sizeof (u32),
219 .format_trace = format_gbp_classify_trace,
220 .type = VLIB_NODE_TYPE_INTERNAL,
226 VLIB_REGISTER_NODE (gbp_src_classify_node) = {
227 .name = "gbp-src-classify",
228 .vector_size = sizeof (u32),
229 .format_trace = format_gbp_classify_trace,
230 .type = VLIB_NODE_TYPE_INTERNAL,
236 VLIB_REGISTER_NODE (gbp_ip4_src_classify_node) = {
237 .name = "ip4-gbp-src-classify",
238 .vector_size = sizeof (u32),
239 .format_trace = format_gbp_classify_trace,
240 .type = VLIB_NODE_TYPE_INTERNAL,
249 VLIB_REGISTER_NODE (gbp_ip6_src_classify_node) = {
250 .name = "ip6-gbp-src-classify",
251 .vector_size = sizeof (u32),
252 .format_trace = format_gbp_classify_trace,
253 .type = VLIB_NODE_TYPE_INTERNAL,
262 VNET_FEATURE_INIT (gbp_ip4_src_classify_feat_node, static) =
264 .arc_name = "ip4-unicast",
265 .node_name = "ip4-gbp-src-classify",
266 .runs_before = VNET_FEATURES ("nat44-out2in"),
268 VNET_FEATURE_INIT (gbp_ip6_src_classify_feat_node, static) =
270 .arc_name = "ip6-unicast",
271 .node_name = "ip6-gbp-src-classify",
272 .runs_before = VNET_FEATURES ("nat66-out2in"),
277 typedef enum gbp_lpm_classify_next_t_
279 GPB_LPM_CLASSIFY_DROP,
280 } gbp_lpm_classify_next_t;
283 gbp_classify_get_src_ip4_address (const ethernet_header_t * eh0,
284 const ip4_address_t ** ip4)
286 const ip4_header_t *iph4;
288 iph4 = (ip4_header_t *) (eh0 + 1);
289 *ip4 = &iph4->src_address;
293 gbp_classify_get_src_ip6_address (const ethernet_header_t * eh0,
294 const ip6_address_t ** ip6)
296 const ip6_header_t *iph6;
298 iph6 = (ip6_header_t *) (eh0 + 1);
299 *ip6 = &iph6->src_address;
303 gbp_classify_get_src_ip_address (const ethernet_header_t * eh0,
304 const ip4_address_t ** ip4,
305 const ip6_address_t ** ip6)
307 u16 etype = clib_net_to_host_u16 (eh0->type);
311 case ETHERNET_TYPE_IP4:
312 gbp_classify_get_src_ip4_address (eh0, ip4);
314 case ETHERNET_TYPE_IP6:
315 gbp_classify_get_src_ip6_address (eh0, ip6);
317 case ETHERNET_TYPE_VLAN:
319 ethernet_vlan_header_t *vh0;
321 vh0 = (ethernet_vlan_header_t *) (eh0 + 1);
323 switch (clib_net_to_host_u16 (vh0->type))
325 case ETHERNET_TYPE_IP4:
327 gbp_classify_get_src_ip4_address (eh0, ip4);
329 case ETHERNET_TYPE_IP6:
330 gbp_classify_get_src_ip6_address (eh0, ip6);
336 case ETHERNET_TYPE_ARP:
338 const ethernet_arp_header_t *ea0;
340 ea0 = (ethernet_arp_header_t *) (eh0 + 1);
342 *ip4 = &ea0->ip4_over_ethernet[0].ip4;
351 * per-packet trace data
353 typedef struct gbp_lpm_classify_trace_t_
358 } gbp_lpm_classify_trace_t;
360 /* packet trace format function */
362 format_gbp_lpm_classify_trace (u8 * s, va_list * args)
364 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
365 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
366 gbp_lpm_classify_trace_t *t = va_arg (*args, gbp_lpm_classify_trace_t *);
368 s = format (s, "sclass:%d lb:%d src:%U",
369 t->sclass, t->lbi, format_ip46_address, &t->src, IP46_TYPE_ANY);
375 * Determine the SRC EPG from a LPM
378 gbp_lpm_classify_inline (vlib_main_t * vm,
379 vlib_node_runtime_t * node,
380 vlib_frame_t * frame,
381 dpo_proto_t dproto, u8 is_recirc)
383 gbp_src_classify_main_t *gscm = &gbp_src_classify_main;
384 u32 n_left_from, *from, *to_next;
388 n_left_from = frame->n_vectors;
389 from = vlib_frame_vector_args (frame);
391 while (n_left_from > 0)
395 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
397 while (n_left_from > 0 && n_left_to_next > 0)
399 u32 bi0, sw_if_index0, fib_index0, lbi0;
400 gbp_lpm_classify_next_t next0;
401 const ethernet_header_t *eh0;
402 const gbp_policy_dpo_t *gpd0;
403 const ip4_address_t *ip4_0;
404 const ip6_address_t *ip6_0;
405 const gbp_endpoint_t *ge0;
406 const gbp_recirc_t *gr0;
407 const dpo_id_t *dpo0;
420 next0 = GPB_LPM_CLASSIFY_DROP;
424 b0 = vlib_get_buffer (vm, bi0);
426 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
427 vnet_buffer2 (b0)->gbp.flags = VXLAN_GBP_GPFLAGS_NONE;
429 if (DPO_PROTO_IP4 == dproto)
431 &((ip4_header_t *) vlib_buffer_get_current (b0))->src_address;
432 else if (DPO_PROTO_IP6 == dproto)
434 &((ip6_header_t *) vlib_buffer_get_current (b0))->src_address;
435 else if (DPO_PROTO_ETHERNET == dproto)
437 eh0 = vlib_buffer_get_current (b0);
438 gbp_classify_get_src_ip_address (eh0, &ip4_0, &ip6_0);
443 gr0 = gbp_recirc_get (sw_if_index0);
444 fib_index0 = gr0->gr_fib_index[dproto];
447 vnet_feature_next (&next0, b0);
453 /* packet should be l2 */
454 sclass0 = SCLASS_INVALID;
458 ge0 = gbp_endpoint_find_mac (eh0->src_address,
459 vnet_buffer (b0)->l2.bd_index);
463 /* packet must have come from an EP's mac */
464 sclass0 = SCLASS_INVALID;
468 fib_index0 = ge0->ge_fwd.gef_fib_index;
470 if (~0 == fib_index0)
472 sclass0 = SCLASS_INVALID;
478 ge0 = gbp_endpoint_find_ip4 (ip4_0, fib_index0);
482 ge0 = gbp_endpoint_find_ip6 (ip6_0, fib_index0);
485 next0 = vnet_l2_feature_next
486 (b0, gscm->l2_input_feat_next[GBP_SRC_CLASSIFY_LPM],
487 L2INPUT_FEAT_GBP_LPM_CLASSIFY);
490 * if we found the EP by IP lookup, it must be from the EP
491 * not a network behind it
495 sclass0 = ge0->ge_fwd.gef_sclass;
502 lbi0 = ip4_fib_forwarding_lookup (fib_index0, ip4_0);
507 ip6_fib_table_fwding_lookup (&ip6_main, fib_index0, ip6_0);
511 /* not IP so no LPM classify possible */
512 sclass0 = SCLASS_INVALID;
513 next0 = GPB_LPM_CLASSIFY_DROP;
516 lb0 = load_balance_get (lbi0);
517 dpo0 = load_balance_get_bucket_i (lb0, 0);
519 /* all packets from an external network should not be learned by the
520 * reciever. so set the Do-not-learn bit here */
521 vnet_buffer2 (b0)->gbp.flags = VXLAN_GBP_GPFLAGS_D;
523 if (gbp_policy_dpo_type == dpo0->dpoi_type)
525 gpd0 = gbp_policy_dpo_get (dpo0->dpoi_index);
526 sclass0 = gpd0->gpd_sclass;
530 /* could not classify => drop */
531 sclass0 = SCLASS_INVALID;
536 vnet_buffer2 (b0)->gbp.sclass = sclass0;
538 if (PREDICT_FALSE ((b0->flags & VLIB_BUFFER_IS_TRACED)))
540 gbp_lpm_classify_trace_t *t =
541 vlib_add_trace (vm, node, b0, sizeof (*t));
550 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
551 to_next, n_left_to_next,
555 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
558 return frame->n_vectors;
561 VLIB_NODE_FN (gbp_ip4_lpm_classify_node) (vlib_main_t * vm,
562 vlib_node_runtime_t * node,
563 vlib_frame_t * frame)
565 return (gbp_lpm_classify_inline (vm, node, frame, DPO_PROTO_IP4, 1));
568 VLIB_NODE_FN (gbp_ip6_lpm_classify_node) (vlib_main_t * vm,
569 vlib_node_runtime_t * node,
570 vlib_frame_t * frame)
572 return (gbp_lpm_classify_inline (vm, node, frame, DPO_PROTO_IP6, 1));
575 VLIB_NODE_FN (gbp_l2_lpm_classify_node) (vlib_main_t * vm,
576 vlib_node_runtime_t * node,
577 vlib_frame_t * frame)
579 return (gbp_lpm_classify_inline (vm, node, frame, DPO_PROTO_ETHERNET, 0));
583 VLIB_REGISTER_NODE (gbp_ip4_lpm_classify_node) = {
584 .name = "ip4-gbp-lpm-classify",
585 .vector_size = sizeof (u32),
586 .format_trace = format_gbp_lpm_classify_trace,
587 .type = VLIB_NODE_TYPE_INTERNAL,
592 [GPB_LPM_CLASSIFY_DROP] = "ip4-drop"
596 VLIB_REGISTER_NODE (gbp_ip6_lpm_classify_node) = {
597 .name = "ip6-gbp-lpm-classify",
598 .vector_size = sizeof (u32),
599 .format_trace = format_gbp_lpm_classify_trace,
600 .type = VLIB_NODE_TYPE_INTERNAL,
605 [GPB_LPM_CLASSIFY_DROP] = "ip6-drop"
609 VLIB_REGISTER_NODE (gbp_l2_lpm_classify_node) = {
610 .name = "l2-gbp-lpm-classify",
611 .vector_size = sizeof (u32),
612 .format_trace = format_gbp_lpm_classify_trace,
613 .type = VLIB_NODE_TYPE_INTERNAL,
618 [GPB_LPM_CLASSIFY_DROP] = "error-drop"
622 VNET_FEATURE_INIT (gbp_ip4_lpm_classify_feat_node, static) =
624 .arc_name = "ip4-unicast",
625 .node_name = "ip4-gbp-lpm-classify",
626 .runs_before = VNET_FEATURES ("nat44-out2in"),
628 VNET_FEATURE_INIT (gbp_ip6_lpm_classify_feat_node, static) =
630 .arc_name = "ip6-unicast",
631 .node_name = "ip6-gbp-lpm-classify",
632 .runs_before = VNET_FEATURES ("nat66-out2in"),
638 * fd.io coding-style-patch-verification: ON
641 * eval: (c-set-style "gnu")