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>
17 #include <plugins/gbp/gbp_learn.h>
18 #include <plugins/gbp/gbp_bridge_domain.h>
19 #include <vlibmemory/api.h>
21 #include <vnet/util/throttle.h>
22 #include <vnet/l2/l2_input.h>
23 #include <vnet/fib/fib_table.h>
24 #include <vnet/vxlan-gbp/vxlan_gbp_packet.h>
27 * Grouping of global data for the GBP source EPG classification feature
29 typedef struct gbp_learn_main_t_
32 * Next nodes for L2 output features
34 u32 gl_l2_input_feat_next[32];
37 * logger - VLIB log class
39 vlib_log_class_t gl_logger;
42 * throttles for the DP leanring
44 throttle_t gl_l2_throttle;
45 throttle_t gl_l3_throttle;
49 * The maximum learning rate per-hashed EP
51 #define GBP_ENDPOINT_HASH_LEARN_RATE (1e-2)
53 static gbp_learn_main_t gbp_learn_main;
55 #define GBP_LEARN_DBG(...) \
56 vlib_log_debug (gbp_learn_main.gl_logger, __VA_ARGS__);
58 #define foreach_gbp_learn \
63 #define _(sym,str) GBP_LEARN_ERROR_##sym,
69 static char *gbp_learn_error_strings[] = {
70 #define _(sym,string) string,
77 #define _(sym,str) GBP_LEARN_NEXT_##sym,
83 typedef struct gbp_learn_l2_t_
90 ip46_address_t outer_src;
91 ip46_address_t outer_dst;
96 gbp_learn_l2_cp (const gbp_learn_l2_t * gl2)
98 ip46_address_t *ips = NULL;
100 GBP_LEARN_DBG ("L2 EP: %U %U, %d",
101 format_mac_address_t, &gl2->mac,
102 format_ip46_address, &gl2->ip, IP46_TYPE_ANY, gl2->epg);
104 vec_add1 (ips, gl2->ip);
106 ASSERT (!ip46_address_is_zero (&gl2->outer_src));
107 ASSERT (!ip46_address_is_zero (&gl2->outer_dst));
110 * flip the source and dst, since that's how it was received, this API
111 * takes how it's sent
113 gbp_endpoint_update_and_lock (GBP_ENDPOINT_SRC_DP,
114 gl2->sw_if_index, ips,
115 &gl2->mac, INDEX_INVALID,
116 INDEX_INVALID, gl2->epg,
117 (GBP_ENDPOINT_FLAG_LEARNT |
118 GBP_ENDPOINT_FLAG_REMOTE),
119 &gl2->outer_dst, &gl2->outer_src, NULL);
124 gbp_learn_l2_ip4_dp (const u8 * mac, const ip4_address_t * ip,
125 u32 bd_index, u32 sw_if_index, epg_id_t epg,
126 const ip4_address_t * outer_src,
127 const ip4_address_t * outer_dst)
129 gbp_learn_l2_t gl2 = {
130 .sw_if_index = sw_if_index,
131 .bd_index = bd_index,
134 .outer_src.ip4 = *outer_src,
135 .outer_dst.ip4 = *outer_dst,
137 mac_address_from_bytes (&gl2.mac, mac);
139 ASSERT (!ip46_address_is_zero (&gl2.outer_src));
140 ASSERT (!ip46_address_is_zero (&gl2.outer_dst));
142 vl_api_rpc_call_main_thread (gbp_learn_l2_cp, (u8 *) & gl2, sizeof (gl2));
146 gbp_learn_l2_ip6_dp (const u8 * mac, const ip6_address_t * ip,
147 u32 bd_index, u32 sw_if_index, epg_id_t epg,
148 const ip4_address_t * outer_src,
149 const ip4_address_t * outer_dst)
151 gbp_learn_l2_t gl2 = {
152 .sw_if_index = sw_if_index,
153 .bd_index = bd_index,
156 .outer_src.ip4 = *outer_src,
157 .outer_dst.ip4 = *outer_dst,
159 mac_address_from_bytes (&gl2.mac, mac);
161 vl_api_rpc_call_main_thread (gbp_learn_l2_cp, (u8 *) & gl2, sizeof (gl2));
165 gbp_learn_l2_dp (const u8 * mac, u32 bd_index, u32 sw_if_index,
167 const ip4_address_t * outer_src,
168 const ip4_address_t * outer_dst)
170 gbp_learn_l2_t gl2 = {
171 .sw_if_index = sw_if_index,
172 .bd_index = bd_index,
174 .outer_src.ip4 = *outer_src,
175 .outer_dst.ip4 = *outer_dst,
177 mac_address_from_bytes (&gl2.mac, mac);
179 vl_api_rpc_call_main_thread (gbp_learn_l2_cp, (u8 *) & gl2, sizeof (gl2));
183 * per-packet trace data
185 typedef struct gbp_learn_l2_trace_t_
187 /* per-pkt trace data */
194 } gbp_learn_l2_trace_t;
197 gbp_learn_get_outer (const ethernet_header_t * eh0,
198 ip4_address_t * outer_src, ip4_address_t * outer_dst)
203 /* rewind back to the ivxlan header */
205 buff -= (sizeof (vxlan_gbp_header_t) +
206 sizeof (udp_header_t) + sizeof (ip4_header_t));
208 ip0 = (ip4_header_t *) buff;
210 *outer_src = ip0->src_address;
211 *outer_dst = ip0->dst_address;
215 gbp_learn_l2 (vlib_main_t * vm,
216 vlib_node_runtime_t * node, vlib_frame_t * frame)
218 u32 n_left_from, *from, *to_next, next_index, thread_index, seed;
219 gbp_learn_main_t *glm;
222 glm = &gbp_learn_main;
224 n_left_from = frame->n_vectors;
225 from = vlib_frame_vector_args (frame);
226 time_now = vlib_time_now (vm);
227 thread_index = vm->thread_index;
229 seed = throttle_seed (&glm->gl_l2_throttle, thread_index, time_now);
231 while (n_left_from > 0)
235 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
237 while (n_left_from > 0 && n_left_to_next > 0)
239 ip4_address_t outer_src, outer_dst;
240 u32 bi0, sw_if_index0, t0, epg0;
241 const ethernet_header_t *eh0;
242 gbp_bridge_domain_t *gb0;
243 gbp_learn_next_t next0;
247 next0 = GBP_LEARN_NEXT_DROP;
255 b0 = vlib_get_buffer (vm, bi0);
256 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
258 eh0 = vlib_buffer_get_current (b0);
259 epg0 = vnet_buffer2 (b0)->gbp.src_epg;
261 next0 = vnet_l2_feature_next (b0, glm->gl_l2_input_feat_next,
262 L2INPUT_FEAT_GBP_LEARN);
264 ge0 = gbp_endpoint_find_mac (eh0->src_address,
265 vnet_buffer (b0)->l2.bd_index);
267 gbp_bridge_domain_get_by_bd_index (vnet_buffer (b0)->l2.bd_index);
269 if ((vnet_buffer2 (b0)->gbp.flags & VXLAN_GBP_GPFLAGS_D) ||
270 (gb0->gb_flags & GBP_BD_FLAG_DO_NOT_LEARN))
277 * check for new EP or a moved EP
279 if (NULL == ge0 || ge0->ge_fwd.gef_itf != sw_if_index0)
283 * use the last 4 bytes of the mac address as the hash for the EP
285 t0 = throttle_check (&glm->gl_l2_throttle, thread_index,
286 *((u32 *) (eh0->src_address + 2)), seed);
289 gbp_learn_get_outer (eh0, &outer_src, &outer_dst);
291 switch (clib_net_to_host_u16 (eh0->type))
293 case ETHERNET_TYPE_IP4:
295 const ip4_header_t *ip0;
297 ip0 = (ip4_header_t *) (eh0 + 1);
299 gbp_learn_l2_ip4_dp (eh0->src_address,
301 vnet_buffer (b0)->l2.bd_index,
303 &outer_src, &outer_dst);
307 case ETHERNET_TYPE_IP6:
309 const ip6_header_t *ip0;
311 ip0 = (ip6_header_t *) (eh0 + 1);
313 gbp_learn_l2_ip6_dp (eh0->src_address,
315 vnet_buffer (b0)->l2.bd_index,
317 &outer_src, &outer_dst);
322 gbp_learn_l2_dp (eh0->src_address,
323 vnet_buffer (b0)->l2.bd_index,
325 &outer_src, &outer_dst);
333 * this update could happen simultaneoulsy from multiple workers
334 * but that's ok we are not interested in being very accurate.
337 ge0->ge_last_time = time_now;
340 if (PREDICT_FALSE ((b0->flags & VLIB_BUFFER_IS_TRACED)))
342 gbp_learn_l2_trace_t *t =
343 vlib_add_trace (vm, node, b0, sizeof (*t));
344 clib_memcpy_fast (t->mac.bytes, eh0->src_address, 6);
345 t->new = (NULL == ge0);
347 t->sw_if_index = sw_if_index0;
349 t->d_bit = ! !(vnet_buffer2 (b0)->gbp.flags &
350 VXLAN_GBP_GPFLAGS_D);
353 /* verify speculative enqueue, maybe switch current next frame */
354 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
355 to_next, n_left_to_next,
359 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
362 return frame->n_vectors;
365 /* packet trace format function */
367 format_gbp_learn_l2_trace (u8 * s, va_list * args)
369 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
370 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
371 gbp_learn_l2_trace_t *t = va_arg (*args, gbp_learn_l2_trace_t *);
373 s = format (s, "new:%d throttled:%d d-bit:%d mac:%U itf:%d epg:%d",
374 t->new, t->throttled, t->d_bit,
375 format_mac_address_t, &t->mac, t->sw_if_index, t->epg);
381 VLIB_REGISTER_NODE (gbp_learn_l2_node) = {
382 .function = gbp_learn_l2,
383 .name = "gbp-learn-l2",
384 .vector_size = sizeof (u32),
385 .format_trace = format_gbp_learn_l2_trace,
386 .type = VLIB_NODE_TYPE_INTERNAL,
388 .n_errors = ARRAY_LEN(gbp_learn_error_strings),
389 .error_strings = gbp_learn_error_strings,
391 .n_next_nodes = GBP_LEARN_N_NEXT,
394 [GBP_LEARN_NEXT_DROP] = "error-drop",
398 VLIB_NODE_FUNCTION_MULTIARCH (gbp_learn_l2_node, gbp_learn_l2);
401 typedef struct gbp_learn_l3_t_
407 ip46_address_t outer_src;
408 ip46_address_t outer_dst;
412 gbp_learn_l3_cp (const gbp_learn_l3_t * gl3)
414 ip46_address_t *ips = NULL;
416 GBP_LEARN_DBG ("L3 EP: %U, %d", format_ip46_address, &gl3->ip,
417 IP46_TYPE_ANY, gl3->epg);
419 vec_add1 (ips, gl3->ip);
421 gbp_endpoint_update_and_lock (GBP_ENDPOINT_SRC_DP,
422 gl3->sw_if_index, ips, NULL,
423 INDEX_INVALID, INDEX_INVALID, gl3->epg,
424 (GBP_ENDPOINT_FLAG_REMOTE |
425 GBP_ENDPOINT_FLAG_LEARNT),
426 &gl3->outer_dst, &gl3->outer_src, NULL);
431 gbp_learn_ip4_dp (const ip4_address_t * ip,
432 u32 fib_index, u32 sw_if_index, epg_id_t epg,
433 const ip4_address_t * outer_src,
434 const ip4_address_t * outer_dst)
437 gbp_learn_l3_t gl3 = {
441 .sw_if_index = sw_if_index,
442 .fib_index = fib_index,
444 .outer_src.ip4 = *outer_src,
445 .outer_dst.ip4 = *outer_dst,
449 vl_api_rpc_call_main_thread (gbp_learn_l3_cp, (u8 *) & gl3, sizeof (gl3));
453 gbp_learn_ip6_dp (const ip6_address_t * ip,
454 u32 fib_index, u32 sw_if_index, epg_id_t epg,
455 const ip4_address_t * outer_src,
456 const ip4_address_t * outer_dst)
459 gbp_learn_l3_t gl3 = {
463 .sw_if_index = sw_if_index,
464 .fib_index = fib_index,
466 .outer_src.ip4 = *outer_src,
467 .outer_dst.ip4 = *outer_dst,
471 vl_api_rpc_call_main_thread (gbp_learn_l3_cp, (u8 *) & gl3, sizeof (gl3));
475 * per-packet trace data
477 typedef struct gbp_learn_l3_trace_t_
479 /* per-pkt trace data */
485 } gbp_learn_l3_trace_t;
488 gbp_learn_l3 (vlib_main_t * vm,
489 vlib_node_runtime_t * node, vlib_frame_t * frame,
490 fib_protocol_t fproto)
492 u32 n_left_from, *from, *to_next, next_index, thread_index, seed;
493 gbp_learn_main_t *glm;
496 glm = &gbp_learn_main;
498 n_left_from = frame->n_vectors;
499 from = vlib_frame_vector_args (frame);
500 time_now = vlib_time_now (vm);
501 thread_index = vm->thread_index;
503 seed = throttle_seed (&glm->gl_l3_throttle, thread_index, time_now);
505 while (n_left_from > 0)
509 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
511 while (n_left_from > 0 && n_left_to_next > 0)
513 u32 bi0, sw_if_index0, t0, epg0, fib_index0;
514 CLIB_UNUSED (const ip4_header_t *) ip4_0;
515 CLIB_UNUSED (const ip6_header_t *) ip6_0;
516 ip4_address_t outer_src, outer_dst;
517 ethernet_header_t *eth0;
518 gbp_learn_next_t next0;
522 next0 = GBP_LEARN_NEXT_DROP;
530 b0 = vlib_get_buffer (vm, bi0);
531 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
532 epg0 = vnet_buffer2 (b0)->gbp.src_epg;
536 vnet_feature_next (&next0, b0);
538 if (vnet_buffer2 (b0)->gbp.flags & VXLAN_GBP_GPFLAGS_D)
545 fib_index0 = fib_table_get_index_for_sw_if_index (fproto,
548 if (FIB_PROTOCOL_IP6 == fproto)
550 ip6_0 = vlib_buffer_get_current (b0);
551 eth0 = (ethernet_header_t *) (((u8 *) ip6_0) - sizeof (*eth0));
553 gbp_learn_get_outer (eth0, &outer_src, &outer_dst);
555 ge0 = gbp_endpoint_find_ip6 (&ip6_0->src_address, fib_index0);
559 t0 = throttle_check (&glm->gl_l3_throttle,
561 ip6_address_hash_to_u32
562 (&ip6_0->src_address), seed);
566 gbp_learn_ip6_dp (&ip6_0->src_address,
567 fib_index0, sw_if_index0, epg0,
568 &outer_src, &outer_dst);
574 * this update could happen simultaneoulsy from multiple
575 * workers but that's ok we are not interested in being
579 ge0->ge_last_time = time_now;
584 ip4_0 = vlib_buffer_get_current (b0);
585 eth0 = (ethernet_header_t *) (((u8 *) ip4_0) - sizeof (*eth0));
587 gbp_learn_get_outer (eth0, &outer_src, &outer_dst);
588 ge0 = gbp_endpoint_find_ip4 (&ip4_0->src_address, fib_index0);
592 t0 = throttle_check (&glm->gl_l3_throttle, thread_index,
593 ip4_0->src_address.as_u32, seed);
597 gbp_learn_ip4_dp (&ip4_0->src_address,
598 fib_index0, sw_if_index0, epg0,
599 &outer_src, &outer_dst);
605 * this update could happen simultaneoulsy from multiple
606 * workers but that's ok we are not interested in being
610 ge0->ge_last_time = time_now;
614 if (PREDICT_FALSE ((b0->flags & VLIB_BUFFER_IS_TRACED)))
616 gbp_learn_l3_trace_t *t;
618 t = vlib_add_trace (vm, node, b0, sizeof (*t));
619 if (FIB_PROTOCOL_IP6 == fproto && ip6_0)
620 ip46_address_set_ip6 (&t->ip, &ip6_0->src_address);
621 if (FIB_PROTOCOL_IP4 == fproto && ip4_0)
622 ip46_address_set_ip4 (&t->ip, &ip4_0->src_address);
623 t->new = (NULL == ge0);
625 t->sw_if_index = sw_if_index0;
629 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
630 to_next, n_left_to_next,
634 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
637 return frame->n_vectors;
640 /* packet trace format function */
642 format_gbp_learn_l3_trace (u8 * s, va_list * args)
644 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
645 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
646 gbp_learn_l3_trace_t *t = va_arg (*args, gbp_learn_l3_trace_t *);
648 s = format (s, "new:%d throttled:%d ip:%U itf:%d epg:%d",
649 t->new, t->throttled,
650 format_ip46_address, &t->ip, IP46_TYPE_ANY, t->sw_if_index,
657 gbp_learn_ip4 (vlib_main_t * vm,
658 vlib_node_runtime_t * node, vlib_frame_t * frame)
660 return (gbp_learn_l3 (vm, node, frame, FIB_PROTOCOL_IP4));
664 gbp_learn_ip6 (vlib_main_t * vm,
665 vlib_node_runtime_t * node, vlib_frame_t * frame)
667 return (gbp_learn_l3 (vm, node, frame, FIB_PROTOCOL_IP6));
671 VLIB_REGISTER_NODE (gbp_learn_ip4_node) = {
672 .function = gbp_learn_ip4,
673 .name = "gbp-learn-ip4",
674 .vector_size = sizeof (u32),
675 .format_trace = format_gbp_learn_l3_trace,
676 .type = VLIB_NODE_TYPE_INTERNAL,
679 VLIB_NODE_FUNCTION_MULTIARCH (gbp_learn_ip4_node, gbp_learn_ip4);
681 VNET_FEATURE_INIT (gbp_learn_ip4, static) =
683 .arc_name = "ip4-unicast",
684 .node_name = "gbp-learn-ip4",
687 VLIB_REGISTER_NODE (gbp_learn_ip6_node) = {
688 .function = gbp_learn_ip6,
689 .name = "gbp-learn-ip6",
690 .vector_size = sizeof (u32),
691 .format_trace = format_gbp_learn_l3_trace,
692 .type = VLIB_NODE_TYPE_INTERNAL,
695 VLIB_NODE_FUNCTION_MULTIARCH (gbp_learn_ip6_node, gbp_learn_ip6);
697 VNET_FEATURE_INIT (gbp_learn_ip6, static) =
699 .arc_name = "ip6-unicast",
700 .node_name = "gbp-learn-ip6",
706 gbp_learn_enable (u32 sw_if_index, gbb_learn_mode_t mode)
708 if (GBP_LEARN_MODE_L2 == mode)
709 l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_GBP_LEARN, 1);
712 vnet_feature_enable_disable ("ip4-unicast",
713 "gbp-learn-ip4", sw_if_index, 1, 0, 0);
714 vnet_feature_enable_disable ("ip6-unicast",
715 "gbp-learn-ip6", sw_if_index, 1, 0, 0);
720 gbp_learn_disable (u32 sw_if_index, gbb_learn_mode_t mode)
722 if (GBP_LEARN_MODE_L2 == mode)
723 l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_GBP_LEARN, 0);
726 vnet_feature_enable_disable ("ip4-unicast",
727 "gbp-learn-ip4", sw_if_index, 0, 0, 0);
728 vnet_feature_enable_disable ("ip6-unicast",
729 "gbp-learn-ip6", sw_if_index, 0, 0, 0);
733 static clib_error_t *
734 gbp_learn_init (vlib_main_t * vm)
736 gbp_learn_main_t *glm = &gbp_learn_main;
737 vlib_thread_main_t *tm = &vlib_thread_main;
739 /* Initialize the feature next-node indices */
740 feat_bitmap_init_next_nodes (vm,
741 gbp_learn_l2_node.index,
743 l2input_get_feat_names (),
744 glm->gl_l2_input_feat_next);
746 throttle_init (&glm->gl_l2_throttle,
747 tm->n_vlib_mains, GBP_ENDPOINT_HASH_LEARN_RATE);
749 throttle_init (&glm->gl_l3_throttle,
750 tm->n_vlib_mains, GBP_ENDPOINT_HASH_LEARN_RATE);
752 glm->gl_logger = vlib_log_register_class ("gbp", "learn");
757 VLIB_INIT_FUNCTION (gbp_learn_init);
760 * fd.io coding-style-patch-verification: ON
763 * eval: (c-set-style "gnu")