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>
26 #define GBP_LEARN_DBG(...) \
27 vlib_log_debug (gbp_learn_main.gl_logger, __VA_ARGS__);
29 #define foreach_gbp_learn \
34 #define _(sym,str) GBP_LEARN_ERROR_##sym,
40 static char *gbp_learn_error_strings[] = {
41 #define _(sym,string) string,
48 #define _(sym,str) GBP_LEARN_NEXT_##sym,
54 typedef struct gbp_learn_l2_t_
61 ip46_address_t outer_src;
62 ip46_address_t outer_dst;
67 gbp_learn_l2_cp (const gbp_learn_l2_t * gl2)
69 ip46_address_t *ips = NULL;
71 GBP_LEARN_DBG ("L2 EP: %U %U, %d",
72 format_mac_address_t, &gl2->mac,
73 format_ip46_address, &gl2->ip, IP46_TYPE_ANY, gl2->epg);
75 vec_add1 (ips, gl2->ip);
77 ASSERT (!ip46_address_is_zero (&gl2->outer_src));
78 ASSERT (!ip46_address_is_zero (&gl2->outer_dst));
81 * flip the source and dst, since that's how it was received, this API
84 gbp_endpoint_update_and_lock (GBP_ENDPOINT_SRC_DP,
85 gl2->sw_if_index, ips,
86 &gl2->mac, INDEX_INVALID,
87 INDEX_INVALID, gl2->epg,
88 (GBP_ENDPOINT_FLAG_LEARNT |
89 GBP_ENDPOINT_FLAG_REMOTE),
90 &gl2->outer_dst, &gl2->outer_src, NULL);
95 gbp_learn_l2_ip4_dp (const u8 * mac, const ip4_address_t * ip,
96 u32 bd_index, u32 sw_if_index, epg_id_t epg,
97 const ip4_address_t * outer_src,
98 const ip4_address_t * outer_dst)
100 gbp_learn_l2_t gl2 = {
101 .sw_if_index = sw_if_index,
102 .bd_index = bd_index,
105 .outer_src.ip4 = *outer_src,
106 .outer_dst.ip4 = *outer_dst,
108 mac_address_from_bytes (&gl2.mac, mac);
110 ASSERT (!ip46_address_is_zero (&gl2.outer_src));
111 ASSERT (!ip46_address_is_zero (&gl2.outer_dst));
113 vl_api_rpc_call_main_thread (gbp_learn_l2_cp, (u8 *) & gl2, sizeof (gl2));
117 gbp_learn_l2_ip6_dp (const u8 * mac, const ip6_address_t * ip,
118 u32 bd_index, u32 sw_if_index, epg_id_t epg,
119 const ip4_address_t * outer_src,
120 const ip4_address_t * outer_dst)
122 gbp_learn_l2_t gl2 = {
123 .sw_if_index = sw_if_index,
124 .bd_index = bd_index,
127 .outer_src.ip4 = *outer_src,
128 .outer_dst.ip4 = *outer_dst,
130 mac_address_from_bytes (&gl2.mac, mac);
132 vl_api_rpc_call_main_thread (gbp_learn_l2_cp, (u8 *) & gl2, sizeof (gl2));
136 gbp_learn_l2_dp (const u8 * mac, u32 bd_index, u32 sw_if_index,
138 const ip4_address_t * outer_src,
139 const ip4_address_t * outer_dst)
141 gbp_learn_l2_t gl2 = {
142 .sw_if_index = sw_if_index,
143 .bd_index = bd_index,
145 .outer_src.ip4 = *outer_src,
146 .outer_dst.ip4 = *outer_dst,
148 mac_address_from_bytes (&gl2.mac, mac);
150 vl_api_rpc_call_main_thread (gbp_learn_l2_cp, (u8 *) & gl2, sizeof (gl2));
154 * per-packet trace data
156 typedef struct gbp_learn_l2_trace_t_
158 /* per-pkt trace data */
165 gbp_bridge_domain_flags_t gb_flags;
166 } gbp_learn_l2_trace_t;
169 gbp_learn_get_outer (const ethernet_header_t * eh0,
170 ip4_address_t * outer_src, ip4_address_t * outer_dst)
175 /* rewind back to the ivxlan header */
177 buff -= (sizeof (vxlan_gbp_header_t) +
178 sizeof (udp_header_t) + sizeof (ip4_header_t));
180 ip0 = (ip4_header_t *) buff;
182 *outer_src = ip0->src_address;
183 *outer_dst = ip0->dst_address;
186 VLIB_NODE_FN (gbp_learn_l2_node) (vlib_main_t * vm,
187 vlib_node_runtime_t * node,
188 vlib_frame_t * frame)
190 u32 n_left_from, *from, *to_next, next_index, thread_index, seed;
191 gbp_learn_main_t *glm;
194 glm = &gbp_learn_main;
196 n_left_from = frame->n_vectors;
197 from = vlib_frame_vector_args (frame);
198 time_now = vlib_time_now (vm);
199 thread_index = vm->thread_index;
201 seed = throttle_seed (&glm->gl_l2_throttle, thread_index, time_now);
203 while (n_left_from > 0)
207 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
209 while (n_left_from > 0 && n_left_to_next > 0)
211 ip4_address_t outer_src, outer_dst;
212 u32 bi0, sw_if_index0, t0, epg0;
213 const ethernet_header_t *eh0;
214 gbp_bridge_domain_t *gb0;
215 gbp_learn_next_t next0;
219 next0 = GBP_LEARN_NEXT_DROP;
227 b0 = vlib_get_buffer (vm, bi0);
228 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
230 eh0 = vlib_buffer_get_current (b0);
231 epg0 = vnet_buffer2 (b0)->gbp.src_epg;
233 next0 = vnet_l2_feature_next (b0, glm->gl_l2_input_feat_next,
234 L2INPUT_FEAT_GBP_LEARN);
236 ge0 = gbp_endpoint_find_mac (eh0->src_address,
237 vnet_buffer (b0)->l2.bd_index);
239 gbp_bridge_domain_get_by_bd_index (vnet_buffer (b0)->l2.bd_index);
241 if ((vnet_buffer2 (b0)->gbp.flags & VXLAN_GBP_GPFLAGS_D) ||
242 (gb0->gb_flags & GBP_BD_FLAG_DO_NOT_LEARN))
249 * check for new EP or a moved EP
251 if (NULL == ge0 || ge0->ge_fwd.gef_itf != sw_if_index0)
255 * use the last 4 bytes of the mac address as the hash for the EP
257 t0 = throttle_check (&glm->gl_l2_throttle, thread_index,
258 *((u32 *) (eh0->src_address + 2)), seed);
261 gbp_learn_get_outer (eh0, &outer_src, &outer_dst);
263 switch (clib_net_to_host_u16 (eh0->type))
265 case ETHERNET_TYPE_IP4:
267 const ip4_header_t *ip0;
269 ip0 = (ip4_header_t *) (eh0 + 1);
271 gbp_learn_l2_ip4_dp (eh0->src_address,
273 vnet_buffer (b0)->l2.bd_index,
275 &outer_src, &outer_dst);
279 case ETHERNET_TYPE_IP6:
281 const ip6_header_t *ip0;
283 ip0 = (ip6_header_t *) (eh0 + 1);
285 gbp_learn_l2_ip6_dp (eh0->src_address,
287 vnet_buffer (b0)->l2.bd_index,
289 &outer_src, &outer_dst);
294 gbp_learn_l2_dp (eh0->src_address,
295 vnet_buffer (b0)->l2.bd_index,
297 &outer_src, &outer_dst);
305 * this update could happen simultaneoulsy from multiple workers
306 * but that's ok we are not interested in being very accurate.
309 ge0->ge_last_time = time_now;
312 if (PREDICT_FALSE ((b0->flags & VLIB_BUFFER_IS_TRACED)))
314 gbp_learn_l2_trace_t *t =
315 vlib_add_trace (vm, node, b0, sizeof (*t));
316 clib_memcpy_fast (t->mac.bytes, eh0->src_address, 6);
317 t->new = (NULL == ge0);
319 t->sw_if_index = sw_if_index0;
321 t->gb_flags = gb0->gb_flags;
322 t->d_bit = ! !(vnet_buffer2 (b0)->gbp.flags &
323 VXLAN_GBP_GPFLAGS_D);
326 /* verify speculative enqueue, maybe switch current next frame */
327 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
328 to_next, n_left_to_next,
332 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
335 return frame->n_vectors;
338 /* packet trace format function */
340 format_gbp_learn_l2_trace (u8 * s, va_list * args)
342 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
343 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
344 gbp_learn_l2_trace_t *t = va_arg (*args, gbp_learn_l2_trace_t *);
346 s = format (s, "new:%d throttled:%d d-bit:%d mac:%U itf:%d epg:%d"
348 t->new, t->throttled, t->d_bit,
349 format_mac_address_t, &t->mac, t->sw_if_index, t->epg,
350 format_gbp_bridge_domain_flags, t->gb_flags);
356 VLIB_REGISTER_NODE (gbp_learn_l2_node) = {
357 .name = "gbp-learn-l2",
358 .vector_size = sizeof (u32),
359 .format_trace = format_gbp_learn_l2_trace,
360 .type = VLIB_NODE_TYPE_INTERNAL,
362 .n_errors = ARRAY_LEN(gbp_learn_error_strings),
363 .error_strings = gbp_learn_error_strings,
365 .n_next_nodes = GBP_LEARN_N_NEXT,
368 [GBP_LEARN_NEXT_DROP] = "error-drop",
373 typedef struct gbp_learn_l3_t_
379 ip46_address_t outer_src;
380 ip46_address_t outer_dst;
384 gbp_learn_l3_cp (const gbp_learn_l3_t * gl3)
386 ip46_address_t *ips = NULL;
388 GBP_LEARN_DBG ("L3 EP: %U, %d", format_ip46_address, &gl3->ip,
389 IP46_TYPE_ANY, gl3->epg);
391 vec_add1 (ips, gl3->ip);
393 gbp_endpoint_update_and_lock (GBP_ENDPOINT_SRC_DP,
394 gl3->sw_if_index, ips, NULL,
395 INDEX_INVALID, INDEX_INVALID, gl3->epg,
396 (GBP_ENDPOINT_FLAG_REMOTE |
397 GBP_ENDPOINT_FLAG_LEARNT),
398 &gl3->outer_dst, &gl3->outer_src, NULL);
403 gbp_learn_ip4_dp (const ip4_address_t * ip,
404 u32 fib_index, u32 sw_if_index, epg_id_t epg,
405 const ip4_address_t * outer_src,
406 const ip4_address_t * outer_dst)
409 gbp_learn_l3_t gl3 = {
413 .sw_if_index = sw_if_index,
414 .fib_index = fib_index,
416 .outer_src.ip4 = *outer_src,
417 .outer_dst.ip4 = *outer_dst,
421 vl_api_rpc_call_main_thread (gbp_learn_l3_cp, (u8 *) & gl3, sizeof (gl3));
425 gbp_learn_ip6_dp (const ip6_address_t * ip,
426 u32 fib_index, u32 sw_if_index, epg_id_t epg,
427 const ip4_address_t * outer_src,
428 const ip4_address_t * outer_dst)
431 gbp_learn_l3_t gl3 = {
435 .sw_if_index = sw_if_index,
436 .fib_index = fib_index,
438 .outer_src.ip4 = *outer_src,
439 .outer_dst.ip4 = *outer_dst,
443 vl_api_rpc_call_main_thread (gbp_learn_l3_cp, (u8 *) & gl3, sizeof (gl3));
447 * per-packet trace data
449 typedef struct gbp_learn_l3_trace_t_
451 /* per-pkt trace data */
457 } gbp_learn_l3_trace_t;
460 gbp_learn_l3 (vlib_main_t * vm,
461 vlib_node_runtime_t * node, vlib_frame_t * frame,
462 fib_protocol_t fproto)
464 u32 n_left_from, *from, *to_next, next_index, thread_index, seed;
465 gbp_learn_main_t *glm;
468 glm = &gbp_learn_main;
470 n_left_from = frame->n_vectors;
471 from = vlib_frame_vector_args (frame);
472 time_now = vlib_time_now (vm);
473 thread_index = vm->thread_index;
475 seed = throttle_seed (&glm->gl_l3_throttle, thread_index, time_now);
477 while (n_left_from > 0)
481 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
483 while (n_left_from > 0 && n_left_to_next > 0)
485 u32 bi0, sw_if_index0, t0, epg0, fib_index0;
486 CLIB_UNUSED (const ip4_header_t *) ip4_0;
487 CLIB_UNUSED (const ip6_header_t *) ip6_0;
488 ip4_address_t outer_src, outer_dst;
489 ethernet_header_t *eth0;
490 gbp_learn_next_t next0;
494 next0 = GBP_LEARN_NEXT_DROP;
502 b0 = vlib_get_buffer (vm, bi0);
503 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
504 epg0 = vnet_buffer2 (b0)->gbp.src_epg;
508 vnet_feature_next (&next0, b0);
510 if (vnet_buffer2 (b0)->gbp.flags & VXLAN_GBP_GPFLAGS_D)
517 fib_index0 = fib_table_get_index_for_sw_if_index (fproto,
520 if (FIB_PROTOCOL_IP6 == fproto)
522 ip6_0 = vlib_buffer_get_current (b0);
523 eth0 = (ethernet_header_t *) (((u8 *) ip6_0) - sizeof (*eth0));
525 gbp_learn_get_outer (eth0, &outer_src, &outer_dst);
527 ge0 = gbp_endpoint_find_ip6 (&ip6_0->src_address, fib_index0);
531 t0 = throttle_check (&glm->gl_l3_throttle,
533 ip6_address_hash_to_u32
534 (&ip6_0->src_address), seed);
538 gbp_learn_ip6_dp (&ip6_0->src_address,
539 fib_index0, sw_if_index0, epg0,
540 &outer_src, &outer_dst);
546 * this update could happen simultaneoulsy from multiple
547 * workers but that's ok we are not interested in being
551 ge0->ge_last_time = time_now;
556 ip4_0 = vlib_buffer_get_current (b0);
557 eth0 = (ethernet_header_t *) (((u8 *) ip4_0) - sizeof (*eth0));
559 gbp_learn_get_outer (eth0, &outer_src, &outer_dst);
560 ge0 = gbp_endpoint_find_ip4 (&ip4_0->src_address, fib_index0);
564 t0 = throttle_check (&glm->gl_l3_throttle, thread_index,
565 ip4_0->src_address.as_u32, seed);
569 gbp_learn_ip4_dp (&ip4_0->src_address,
570 fib_index0, sw_if_index0, epg0,
571 &outer_src, &outer_dst);
577 * this update could happen simultaneoulsy from multiple
578 * workers but that's ok we are not interested in being
582 ge0->ge_last_time = time_now;
586 if (PREDICT_FALSE ((b0->flags & VLIB_BUFFER_IS_TRACED)))
588 gbp_learn_l3_trace_t *t;
590 t = vlib_add_trace (vm, node, b0, sizeof (*t));
591 if (FIB_PROTOCOL_IP6 == fproto && ip6_0)
592 ip46_address_set_ip6 (&t->ip, &ip6_0->src_address);
593 if (FIB_PROTOCOL_IP4 == fproto && ip4_0)
594 ip46_address_set_ip4 (&t->ip, &ip4_0->src_address);
595 t->new = (NULL == ge0);
597 t->sw_if_index = sw_if_index0;
601 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
602 to_next, n_left_to_next,
606 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
609 return frame->n_vectors;
612 /* packet trace format function */
614 format_gbp_learn_l3_trace (u8 * s, va_list * args)
616 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
617 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
618 gbp_learn_l3_trace_t *t = va_arg (*args, gbp_learn_l3_trace_t *);
620 s = format (s, "new:%d throttled:%d ip:%U itf:%d epg:%d",
621 t->new, t->throttled,
622 format_ip46_address, &t->ip, IP46_TYPE_ANY, t->sw_if_index,
628 VLIB_NODE_FN (gbp_learn_ip4_node) (vlib_main_t * vm,
629 vlib_node_runtime_t * node,
630 vlib_frame_t * frame)
632 return (gbp_learn_l3 (vm, node, frame, FIB_PROTOCOL_IP4));
635 VLIB_NODE_FN (gbp_learn_ip6_node) (vlib_main_t * vm,
636 vlib_node_runtime_t * node,
637 vlib_frame_t * frame)
639 return (gbp_learn_l3 (vm, node, frame, FIB_PROTOCOL_IP6));
643 VLIB_REGISTER_NODE (gbp_learn_ip4_node) = {
644 .name = "gbp-learn-ip4",
645 .vector_size = sizeof (u32),
646 .format_trace = format_gbp_learn_l3_trace,
647 .type = VLIB_NODE_TYPE_INTERNAL,
650 VNET_FEATURE_INIT (gbp_learn_ip4, static) =
652 .arc_name = "ip4-unicast",
653 .node_name = "gbp-learn-ip4",
656 VLIB_REGISTER_NODE (gbp_learn_ip6_node) = {
657 .name = "gbp-learn-ip6",
658 .vector_size = sizeof (u32),
659 .format_trace = format_gbp_learn_l3_trace,
660 .type = VLIB_NODE_TYPE_INTERNAL,
663 VNET_FEATURE_INIT (gbp_learn_ip6, static) =
665 .arc_name = "ip6-unicast",
666 .node_name = "gbp-learn-ip6",
672 * fd.io coding-style-patch-verification: ON
675 * eval: (c-set-style "gnu")