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>
25 #include <vnet/ethernet/arp_packet.h>
27 #define GBP_LEARN_DBG(...) \
28 vlib_log_debug (gbp_learn_main.gl_logger, __VA_ARGS__);
30 #define foreach_gbp_learn \
35 #define _(sym,str) GBP_LEARN_ERROR_##sym,
41 static char *gbp_learn_error_strings[] = {
42 #define _(sym,string) string,
49 #define _(sym,str) GBP_LEARN_NEXT_##sym,
55 typedef struct gbp_learn_l2_t_
62 ip46_address_t outer_src;
63 ip46_address_t outer_dst;
68 gbp_learn_l2_cp (const gbp_learn_l2_t * gl2)
70 ip46_address_t *ips = NULL;
72 GBP_LEARN_DBG ("L2 EP: %U %U, %d",
73 format_mac_address_t, &gl2->mac,
74 format_ip46_address, &gl2->ip, IP46_TYPE_ANY, gl2->sclass);
76 if (!ip46_address_is_zero (&gl2->ip))
77 vec_add1 (ips, gl2->ip);
80 * flip the source and dst, since that's how it was received, this API
83 gbp_endpoint_update_and_lock (GBP_ENDPOINT_SRC_DP,
84 gl2->sw_if_index, ips,
85 &gl2->mac, INDEX_INVALID,
86 INDEX_INVALID, gl2->sclass,
87 (GBP_ENDPOINT_FLAG_LEARNT |
88 GBP_ENDPOINT_FLAG_REMOTE),
89 &gl2->outer_dst, &gl2->outer_src, NULL);
94 gbp_learn_l2_ip4_dp (const u8 * mac, const ip4_address_t * ip,
95 u32 bd_index, u32 sw_if_index, sclass_t sclass,
96 const ip4_address_t * outer_src,
97 const ip4_address_t * outer_dst)
99 gbp_learn_l2_t gl2 = {
100 .sw_if_index = sw_if_index,
101 .bd_index = bd_index,
104 .outer_src.ip4 = *outer_src,
105 .outer_dst.ip4 = *outer_dst,
107 mac_address_from_bytes (&gl2.mac, mac);
109 vl_api_rpc_call_main_thread (gbp_learn_l2_cp, (u8 *) & gl2, sizeof (gl2));
113 gbp_learn_l2_ip6_dp (const u8 * mac, const ip6_address_t * ip,
114 u32 bd_index, u32 sw_if_index, sclass_t sclass,
115 const ip4_address_t * outer_src,
116 const ip4_address_t * outer_dst)
118 gbp_learn_l2_t gl2 = {
119 .sw_if_index = sw_if_index,
120 .bd_index = bd_index,
123 .outer_src.ip4 = *outer_src,
124 .outer_dst.ip4 = *outer_dst,
126 mac_address_from_bytes (&gl2.mac, mac);
128 vl_api_rpc_call_main_thread (gbp_learn_l2_cp, (u8 *) & gl2, sizeof (gl2));
132 gbp_learn_l2_dp (const u8 * mac, u32 bd_index, u32 sw_if_index,
134 const ip4_address_t * outer_src,
135 const ip4_address_t * outer_dst)
137 gbp_learn_l2_t gl2 = {
138 .sw_if_index = sw_if_index,
139 .bd_index = bd_index,
141 .outer_src.ip4 = *outer_src,
142 .outer_dst.ip4 = *outer_dst,
144 mac_address_from_bytes (&gl2.mac, mac);
146 vl_api_rpc_call_main_thread (gbp_learn_l2_cp, (u8 *) & gl2, sizeof (gl2));
150 * per-packet trace data
152 typedef struct gbp_learn_l2_trace_t_
154 /* per-pkt trace data */
161 gbp_bridge_domain_flags_t gb_flags;
162 } gbp_learn_l2_trace_t;
165 gbp_learn_get_outer (const ethernet_header_t * eh0,
166 ip4_address_t * outer_src, ip4_address_t * outer_dst)
171 /* rewind back to the ivxlan header */
173 buff -= (sizeof (vxlan_gbp_header_t) +
174 sizeof (udp_header_t) + sizeof (ip4_header_t));
176 ip0 = (ip4_header_t *) buff;
178 *outer_src = ip0->src_address;
179 *outer_dst = ip0->dst_address;
183 gbp_endpoint_update_required (const gbp_endpoint_t * ge0,
184 u32 rx_sw_if_index, sclass_t sclass)
186 /* Conditions for [re]learning this EP */
188 /* 1. it doesn't have a dataplane source */
189 if (!gbp_endpoint_is_learnt (ge0))
192 /* 2. has the input interface changed */
193 if (gbp_itf_get_sw_if_index (ge0->ge_fwd.gef_itf) != rx_sw_if_index)
196 /* 3. has the sclass changed */
197 if (sclass != ge0->ge_fwd.gef_sclass)
200 /* otherwise it's unchanged */
204 VLIB_NODE_FN (gbp_learn_l2_node) (vlib_main_t * vm,
205 vlib_node_runtime_t * node,
206 vlib_frame_t * frame)
208 u32 n_left_from, *from, *to_next, next_index, thread_index, seed;
209 gbp_learn_main_t *glm;
212 glm = &gbp_learn_main;
214 n_left_from = frame->n_vectors;
215 from = vlib_frame_vector_args (frame);
216 time_now = vlib_time_now (vm);
217 thread_index = vm->thread_index;
219 seed = throttle_seed (&glm->gl_l2_throttle, thread_index, time_now);
221 while (n_left_from > 0)
225 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
227 while (n_left_from > 0 && n_left_to_next > 0)
229 ip4_address_t outer_src, outer_dst;
230 const ethernet_header_t *eh0;
231 u32 bi0, sw_if_index0, t0;
232 gbp_bridge_domain_t *gb0;
233 gbp_learn_next_t next0;
238 next0 = GBP_LEARN_NEXT_DROP;
246 b0 = vlib_get_buffer (vm, bi0);
247 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
249 eh0 = vlib_buffer_get_current (b0);
250 sclass0 = vnet_buffer2 (b0)->gbp.sclass;
252 next0 = vnet_l2_feature_next (b0, glm->gl_l2_input_feat_next,
253 L2INPUT_FEAT_GBP_LEARN);
255 ge0 = gbp_endpoint_find_mac (eh0->src_address,
256 vnet_buffer (b0)->l2.bd_index);
258 gbp_bridge_domain_get_by_bd_index (vnet_buffer (b0)->l2.bd_index);
260 if ((vnet_buffer2 (b0)->gbp.flags & VXLAN_GBP_GPFLAGS_D) ||
261 (gb0->gb_flags & GBP_BD_FLAG_DO_NOT_LEARN))
268 * check for new EP or a moved EP
271 gbp_endpoint_update_required (ge0, sw_if_index0, sclass0))
274 * use the last 4 bytes of the mac address as the hash for the EP
276 t0 = throttle_check (&glm->gl_l2_throttle, thread_index,
277 *((u32 *) (eh0->src_address + 2)), seed);
280 gbp_learn_get_outer (eh0, &outer_src, &outer_dst);
282 if (outer_src.as_u32 == 0 || outer_dst.as_u32 == 0)
288 switch (clib_net_to_host_u16 (eh0->type))
290 case ETHERNET_TYPE_IP4:
292 const ip4_header_t *ip0;
294 ip0 = (ip4_header_t *) (eh0 + 1);
296 gbp_learn_l2_ip4_dp (eh0->src_address,
298 vnet_buffer (b0)->l2.bd_index,
299 sw_if_index0, sclass0,
300 &outer_src, &outer_dst);
304 case ETHERNET_TYPE_IP6:
306 const ip6_header_t *ip0;
308 ip0 = (ip6_header_t *) (eh0 + 1);
310 gbp_learn_l2_ip6_dp (eh0->src_address,
312 vnet_buffer (b0)->l2.bd_index,
313 sw_if_index0, sclass0,
314 &outer_src, &outer_dst);
318 case ETHERNET_TYPE_ARP:
320 const ethernet_arp_header_t *arp0;
322 arp0 = (ethernet_arp_header_t *) (eh0 + 1);
324 gbp_learn_l2_ip4_dp (eh0->src_address,
325 &arp0->ip4_over_ethernet[0].ip4,
326 vnet_buffer (b0)->l2.bd_index,
327 sw_if_index0, sclass0,
328 &outer_src, &outer_dst);
332 gbp_learn_l2_dp (eh0->src_address,
333 vnet_buffer (b0)->l2.bd_index,
334 sw_if_index0, sclass0,
335 &outer_src, &outer_dst);
343 * this update could happen simultaneoulsy from multiple workers
344 * but that's ok we are not interested in being very accurate.
347 ge0->ge_last_time = time_now;
350 if (PREDICT_FALSE ((b0->flags & VLIB_BUFFER_IS_TRACED)))
352 gbp_learn_l2_trace_t *t =
353 vlib_add_trace (vm, node, b0, sizeof (*t));
354 clib_memcpy_fast (t->mac.bytes, eh0->src_address, 6);
355 t->new = (NULL == ge0);
357 t->sw_if_index = sw_if_index0;
359 t->gb_flags = gb0->gb_flags;
360 t->d_bit = ! !(vnet_buffer2 (b0)->gbp.flags &
361 VXLAN_GBP_GPFLAGS_D);
364 /* verify speculative enqueue, maybe switch current next frame */
365 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
366 to_next, n_left_to_next,
370 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
373 return frame->n_vectors;
376 /* packet trace format function */
378 format_gbp_learn_l2_trace (u8 * s, va_list * args)
380 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
381 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
382 gbp_learn_l2_trace_t *t = va_arg (*args, gbp_learn_l2_trace_t *);
384 s = format (s, "new:%d throttled:%d d-bit:%d mac:%U itf:%d sclass:%d"
386 t->new, t->throttled, t->d_bit,
387 format_mac_address_t, &t->mac, t->sw_if_index, t->sclass,
388 format_gbp_bridge_domain_flags, t->gb_flags);
394 VLIB_REGISTER_NODE (gbp_learn_l2_node) = {
395 .name = "gbp-learn-l2",
396 .vector_size = sizeof (u32),
397 .format_trace = format_gbp_learn_l2_trace,
398 .type = VLIB_NODE_TYPE_INTERNAL,
400 .n_errors = ARRAY_LEN(gbp_learn_error_strings),
401 .error_strings = gbp_learn_error_strings,
403 .n_next_nodes = GBP_LEARN_N_NEXT,
406 [GBP_LEARN_NEXT_DROP] = "error-drop",
411 typedef struct gbp_learn_l3_t_
417 ip46_address_t outer_src;
418 ip46_address_t outer_dst;
422 gbp_learn_l3_cp (const gbp_learn_l3_t * gl3)
424 ip46_address_t *ips = NULL;
426 GBP_LEARN_DBG ("L3 EP: %U, %d", format_ip46_address, &gl3->ip,
427 IP46_TYPE_ANY, gl3->sclass);
429 vec_add1 (ips, gl3->ip);
431 gbp_endpoint_update_and_lock (GBP_ENDPOINT_SRC_DP,
432 gl3->sw_if_index, ips, NULL,
433 INDEX_INVALID, INDEX_INVALID, gl3->sclass,
434 (GBP_ENDPOINT_FLAG_REMOTE |
435 GBP_ENDPOINT_FLAG_LEARNT),
436 &gl3->outer_dst, &gl3->outer_src, NULL);
441 gbp_learn_ip4_dp (const ip4_address_t * ip,
442 u32 fib_index, u32 sw_if_index, sclass_t sclass,
443 const ip4_address_t * outer_src,
444 const ip4_address_t * outer_dst)
447 gbp_learn_l3_t gl3 = {
451 .sw_if_index = sw_if_index,
452 .fib_index = fib_index,
454 .outer_src.ip4 = *outer_src,
455 .outer_dst.ip4 = *outer_dst,
459 vl_api_rpc_call_main_thread (gbp_learn_l3_cp, (u8 *) & gl3, sizeof (gl3));
463 gbp_learn_ip6_dp (const ip6_address_t * ip,
464 u32 fib_index, u32 sw_if_index, sclass_t sclass,
465 const ip4_address_t * outer_src,
466 const ip4_address_t * outer_dst)
469 gbp_learn_l3_t gl3 = {
473 .sw_if_index = sw_if_index,
474 .fib_index = fib_index,
476 .outer_src.ip4 = *outer_src,
477 .outer_dst.ip4 = *outer_dst,
481 vl_api_rpc_call_main_thread (gbp_learn_l3_cp, (u8 *) & gl3, sizeof (gl3));
485 * per-packet trace data
487 typedef struct gbp_learn_l3_trace_t_
489 /* per-pkt trace data */
495 } gbp_learn_l3_trace_t;
498 gbp_learn_l3 (vlib_main_t * vm,
499 vlib_node_runtime_t * node, vlib_frame_t * frame,
500 fib_protocol_t fproto)
502 u32 n_left_from, *from, *to_next, next_index, thread_index, seed;
503 gbp_learn_main_t *glm;
506 glm = &gbp_learn_main;
508 n_left_from = frame->n_vectors;
509 from = vlib_frame_vector_args (frame);
510 time_now = vlib_time_now (vm);
511 thread_index = vm->thread_index;
513 seed = throttle_seed (&glm->gl_l3_throttle, thread_index, time_now);
515 while (n_left_from > 0)
519 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
521 while (n_left_from > 0 && n_left_to_next > 0)
523 CLIB_UNUSED (const ip4_header_t *) ip4_0;
524 CLIB_UNUSED (const ip6_header_t *) ip6_0;
525 u32 bi0, sw_if_index0, t0, fib_index0;
526 ip4_address_t outer_src, outer_dst;
527 ethernet_header_t *eth0;
528 gbp_learn_next_t next0;
533 next0 = GBP_LEARN_NEXT_DROP;
541 b0 = vlib_get_buffer (vm, bi0);
542 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
543 sclass0 = vnet_buffer2 (b0)->gbp.sclass;
547 vnet_feature_next (&next0, b0);
549 if (vnet_buffer2 (b0)->gbp.flags & VXLAN_GBP_GPFLAGS_D)
556 fib_index0 = fib_table_get_index_for_sw_if_index (fproto,
559 if (FIB_PROTOCOL_IP6 == fproto)
561 ip6_0 = vlib_buffer_get_current (b0);
562 eth0 = (ethernet_header_t *) (((u8 *) ip6_0) - sizeof (*eth0));
564 gbp_learn_get_outer (eth0, &outer_src, &outer_dst);
566 ge0 = gbp_endpoint_find_ip6 (&ip6_0->src_address, fib_index0);
569 gbp_endpoint_update_required (ge0, sw_if_index0, sclass0))
571 t0 = throttle_check (&glm->gl_l3_throttle,
573 ip6_address_hash_to_u32
574 (&ip6_0->src_address), seed);
578 gbp_learn_ip6_dp (&ip6_0->src_address,
579 fib_index0, sw_if_index0, sclass0,
580 &outer_src, &outer_dst);
586 * this update could happen simultaneoulsy from multiple
587 * workers but that's ok we are not interested in being
591 ge0->ge_last_time = time_now;
596 ip4_0 = vlib_buffer_get_current (b0);
597 eth0 = (ethernet_header_t *) (((u8 *) ip4_0) - sizeof (*eth0));
599 gbp_learn_get_outer (eth0, &outer_src, &outer_dst);
600 ge0 = gbp_endpoint_find_ip4 (&ip4_0->src_address, fib_index0);
603 gbp_endpoint_update_required (ge0, sw_if_index0, sclass0))
605 t0 = throttle_check (&glm->gl_l3_throttle, thread_index,
606 ip4_0->src_address.as_u32, seed);
610 gbp_learn_ip4_dp (&ip4_0->src_address,
611 fib_index0, sw_if_index0, sclass0,
612 &outer_src, &outer_dst);
618 * this update could happen simultaneoulsy from multiple
619 * workers but that's ok we are not interested in being
623 ge0->ge_last_time = time_now;
627 if (PREDICT_FALSE ((b0->flags & VLIB_BUFFER_IS_TRACED)))
629 gbp_learn_l3_trace_t *t;
631 t = vlib_add_trace (vm, node, b0, sizeof (*t));
632 if (FIB_PROTOCOL_IP6 == fproto && ip6_0)
633 ip46_address_set_ip6 (&t->ip, &ip6_0->src_address);
634 if (FIB_PROTOCOL_IP4 == fproto && ip4_0)
635 ip46_address_set_ip4 (&t->ip, &ip4_0->src_address);
636 t->new = (NULL == ge0);
638 t->sw_if_index = sw_if_index0;
642 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
643 to_next, n_left_to_next,
647 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
650 return frame->n_vectors;
653 /* packet trace format function */
655 format_gbp_learn_l3_trace (u8 * s, va_list * args)
657 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
658 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
659 gbp_learn_l3_trace_t *t = va_arg (*args, gbp_learn_l3_trace_t *);
661 s = format (s, "new:%d throttled:%d ip:%U itf:%d sclass:%d",
662 t->new, t->throttled,
663 format_ip46_address, &t->ip, IP46_TYPE_ANY, t->sw_if_index,
669 VLIB_NODE_FN (gbp_learn_ip4_node) (vlib_main_t * vm,
670 vlib_node_runtime_t * node,
671 vlib_frame_t * frame)
673 return (gbp_learn_l3 (vm, node, frame, FIB_PROTOCOL_IP4));
676 VLIB_NODE_FN (gbp_learn_ip6_node) (vlib_main_t * vm,
677 vlib_node_runtime_t * node,
678 vlib_frame_t * frame)
680 return (gbp_learn_l3 (vm, node, frame, FIB_PROTOCOL_IP6));
684 VLIB_REGISTER_NODE (gbp_learn_ip4_node) = {
685 .name = "gbp-learn-ip4",
686 .vector_size = sizeof (u32),
687 .format_trace = format_gbp_learn_l3_trace,
688 .type = VLIB_NODE_TYPE_INTERNAL,
691 VNET_FEATURE_INIT (gbp_learn_ip4, static) =
693 .arc_name = "ip4-unicast",
694 .node_name = "gbp-learn-ip4",
697 VLIB_REGISTER_NODE (gbp_learn_ip6_node) = {
698 .name = "gbp-learn-ip6",
699 .vector_size = sizeof (u32),
700 .format_trace = format_gbp_learn_l3_trace,
701 .type = VLIB_NODE_TYPE_INTERNAL,
704 VNET_FEATURE_INIT (gbp_learn_ip6, static) =
706 .arc_name = "ip6-unicast",
707 .node_name = "gbp-learn-ip6",
713 * fd.io coding-style-patch-verification: ON
716 * eval: (c-set-style "gnu")