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_learn_l2_trace_t;
168 gbp_learn_get_outer (const ethernet_header_t * eh0,
169 ip4_address_t * outer_src, ip4_address_t * outer_dst)
174 /* rewind back to the ivxlan header */
176 buff -= (sizeof (vxlan_gbp_header_t) +
177 sizeof (udp_header_t) + sizeof (ip4_header_t));
179 ip0 = (ip4_header_t *) buff;
181 *outer_src = ip0->src_address;
182 *outer_dst = ip0->dst_address;
185 VLIB_NODE_FN (gbp_learn_l2_node) (vlib_main_t * vm,
186 vlib_node_runtime_t * node,
187 vlib_frame_t * frame)
189 u32 n_left_from, *from, *to_next, next_index, thread_index, seed;
190 gbp_learn_main_t *glm;
193 glm = &gbp_learn_main;
195 n_left_from = frame->n_vectors;
196 from = vlib_frame_vector_args (frame);
197 time_now = vlib_time_now (vm);
198 thread_index = vm->thread_index;
200 seed = throttle_seed (&glm->gl_l2_throttle, thread_index, time_now);
202 while (n_left_from > 0)
206 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
208 while (n_left_from > 0 && n_left_to_next > 0)
210 ip4_address_t outer_src, outer_dst;
211 u32 bi0, sw_if_index0, t0, epg0;
212 const ethernet_header_t *eh0;
213 gbp_bridge_domain_t *gb0;
214 gbp_learn_next_t next0;
218 next0 = GBP_LEARN_NEXT_DROP;
226 b0 = vlib_get_buffer (vm, bi0);
227 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
229 eh0 = vlib_buffer_get_current (b0);
230 epg0 = vnet_buffer2 (b0)->gbp.src_epg;
232 next0 = vnet_l2_feature_next (b0, glm->gl_l2_input_feat_next,
233 L2INPUT_FEAT_GBP_LEARN);
235 ge0 = gbp_endpoint_find_mac (eh0->src_address,
236 vnet_buffer (b0)->l2.bd_index);
238 gbp_bridge_domain_get_by_bd_index (vnet_buffer (b0)->l2.bd_index);
240 if ((vnet_buffer2 (b0)->gbp.flags & VXLAN_GBP_GPFLAGS_D) ||
241 (gb0->gb_flags & GBP_BD_FLAG_DO_NOT_LEARN))
248 * check for new EP or a moved EP
250 if (NULL == ge0 || ge0->ge_fwd.gef_itf != sw_if_index0)
254 * use the last 4 bytes of the mac address as the hash for the EP
256 t0 = throttle_check (&glm->gl_l2_throttle, thread_index,
257 *((u32 *) (eh0->src_address + 2)), seed);
260 gbp_learn_get_outer (eh0, &outer_src, &outer_dst);
262 switch (clib_net_to_host_u16 (eh0->type))
264 case ETHERNET_TYPE_IP4:
266 const ip4_header_t *ip0;
268 ip0 = (ip4_header_t *) (eh0 + 1);
270 gbp_learn_l2_ip4_dp (eh0->src_address,
272 vnet_buffer (b0)->l2.bd_index,
274 &outer_src, &outer_dst);
278 case ETHERNET_TYPE_IP6:
280 const ip6_header_t *ip0;
282 ip0 = (ip6_header_t *) (eh0 + 1);
284 gbp_learn_l2_ip6_dp (eh0->src_address,
286 vnet_buffer (b0)->l2.bd_index,
288 &outer_src, &outer_dst);
293 gbp_learn_l2_dp (eh0->src_address,
294 vnet_buffer (b0)->l2.bd_index,
296 &outer_src, &outer_dst);
304 * this update could happen simultaneoulsy from multiple workers
305 * but that's ok we are not interested in being very accurate.
308 ge0->ge_last_time = time_now;
311 if (PREDICT_FALSE ((b0->flags & VLIB_BUFFER_IS_TRACED)))
313 gbp_learn_l2_trace_t *t =
314 vlib_add_trace (vm, node, b0, sizeof (*t));
315 clib_memcpy_fast (t->mac.bytes, eh0->src_address, 6);
316 t->new = (NULL == ge0);
318 t->sw_if_index = sw_if_index0;
320 t->d_bit = ! !(vnet_buffer2 (b0)->gbp.flags &
321 VXLAN_GBP_GPFLAGS_D);
324 /* verify speculative enqueue, maybe switch current next frame */
325 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
326 to_next, n_left_to_next,
330 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
333 return frame->n_vectors;
336 /* packet trace format function */
338 format_gbp_learn_l2_trace (u8 * s, va_list * args)
340 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
341 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
342 gbp_learn_l2_trace_t *t = va_arg (*args, gbp_learn_l2_trace_t *);
344 s = format (s, "new:%d throttled:%d d-bit:%d mac:%U itf:%d epg:%d",
345 t->new, t->throttled, t->d_bit,
346 format_mac_address_t, &t->mac, t->sw_if_index, t->epg);
352 VLIB_REGISTER_NODE (gbp_learn_l2_node) = {
353 .name = "gbp-learn-l2",
354 .vector_size = sizeof (u32),
355 .format_trace = format_gbp_learn_l2_trace,
356 .type = VLIB_NODE_TYPE_INTERNAL,
358 .n_errors = ARRAY_LEN(gbp_learn_error_strings),
359 .error_strings = gbp_learn_error_strings,
361 .n_next_nodes = GBP_LEARN_N_NEXT,
364 [GBP_LEARN_NEXT_DROP] = "error-drop",
369 typedef struct gbp_learn_l3_t_
375 ip46_address_t outer_src;
376 ip46_address_t outer_dst;
380 gbp_learn_l3_cp (const gbp_learn_l3_t * gl3)
382 ip46_address_t *ips = NULL;
384 GBP_LEARN_DBG ("L3 EP: %U, %d", format_ip46_address, &gl3->ip,
385 IP46_TYPE_ANY, gl3->epg);
387 vec_add1 (ips, gl3->ip);
389 gbp_endpoint_update_and_lock (GBP_ENDPOINT_SRC_DP,
390 gl3->sw_if_index, ips, NULL,
391 INDEX_INVALID, INDEX_INVALID, gl3->epg,
392 (GBP_ENDPOINT_FLAG_REMOTE |
393 GBP_ENDPOINT_FLAG_LEARNT),
394 &gl3->outer_dst, &gl3->outer_src, NULL);
399 gbp_learn_ip4_dp (const ip4_address_t * ip,
400 u32 fib_index, u32 sw_if_index, epg_id_t epg,
401 const ip4_address_t * outer_src,
402 const ip4_address_t * outer_dst)
405 gbp_learn_l3_t gl3 = {
409 .sw_if_index = sw_if_index,
410 .fib_index = fib_index,
412 .outer_src.ip4 = *outer_src,
413 .outer_dst.ip4 = *outer_dst,
417 vl_api_rpc_call_main_thread (gbp_learn_l3_cp, (u8 *) & gl3, sizeof (gl3));
421 gbp_learn_ip6_dp (const ip6_address_t * ip,
422 u32 fib_index, u32 sw_if_index, epg_id_t epg,
423 const ip4_address_t * outer_src,
424 const ip4_address_t * outer_dst)
427 gbp_learn_l3_t gl3 = {
431 .sw_if_index = sw_if_index,
432 .fib_index = fib_index,
434 .outer_src.ip4 = *outer_src,
435 .outer_dst.ip4 = *outer_dst,
439 vl_api_rpc_call_main_thread (gbp_learn_l3_cp, (u8 *) & gl3, sizeof (gl3));
443 * per-packet trace data
445 typedef struct gbp_learn_l3_trace_t_
447 /* per-pkt trace data */
453 } gbp_learn_l3_trace_t;
456 gbp_learn_l3 (vlib_main_t * vm,
457 vlib_node_runtime_t * node, vlib_frame_t * frame,
458 fib_protocol_t fproto)
460 u32 n_left_from, *from, *to_next, next_index, thread_index, seed;
461 gbp_learn_main_t *glm;
464 glm = &gbp_learn_main;
466 n_left_from = frame->n_vectors;
467 from = vlib_frame_vector_args (frame);
468 time_now = vlib_time_now (vm);
469 thread_index = vm->thread_index;
471 seed = throttle_seed (&glm->gl_l3_throttle, thread_index, time_now);
473 while (n_left_from > 0)
477 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
479 while (n_left_from > 0 && n_left_to_next > 0)
481 u32 bi0, sw_if_index0, t0, epg0, fib_index0;
482 CLIB_UNUSED (const ip4_header_t *) ip4_0;
483 CLIB_UNUSED (const ip6_header_t *) ip6_0;
484 ip4_address_t outer_src, outer_dst;
485 ethernet_header_t *eth0;
486 gbp_learn_next_t next0;
490 next0 = GBP_LEARN_NEXT_DROP;
498 b0 = vlib_get_buffer (vm, bi0);
499 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
500 epg0 = vnet_buffer2 (b0)->gbp.src_epg;
504 vnet_feature_next (&next0, b0);
506 if (vnet_buffer2 (b0)->gbp.flags & VXLAN_GBP_GPFLAGS_D)
513 fib_index0 = fib_table_get_index_for_sw_if_index (fproto,
516 if (FIB_PROTOCOL_IP6 == fproto)
518 ip6_0 = vlib_buffer_get_current (b0);
519 eth0 = (ethernet_header_t *) (((u8 *) ip6_0) - sizeof (*eth0));
521 gbp_learn_get_outer (eth0, &outer_src, &outer_dst);
523 ge0 = gbp_endpoint_find_ip6 (&ip6_0->src_address, fib_index0);
527 t0 = throttle_check (&glm->gl_l3_throttle,
529 ip6_address_hash_to_u32
530 (&ip6_0->src_address), seed);
534 gbp_learn_ip6_dp (&ip6_0->src_address,
535 fib_index0, sw_if_index0, epg0,
536 &outer_src, &outer_dst);
542 * this update could happen simultaneoulsy from multiple
543 * workers but that's ok we are not interested in being
547 ge0->ge_last_time = time_now;
552 ip4_0 = vlib_buffer_get_current (b0);
553 eth0 = (ethernet_header_t *) (((u8 *) ip4_0) - sizeof (*eth0));
555 gbp_learn_get_outer (eth0, &outer_src, &outer_dst);
556 ge0 = gbp_endpoint_find_ip4 (&ip4_0->src_address, fib_index0);
560 t0 = throttle_check (&glm->gl_l3_throttle, thread_index,
561 ip4_0->src_address.as_u32, seed);
565 gbp_learn_ip4_dp (&ip4_0->src_address,
566 fib_index0, sw_if_index0, epg0,
567 &outer_src, &outer_dst);
573 * this update could happen simultaneoulsy from multiple
574 * workers but that's ok we are not interested in being
578 ge0->ge_last_time = time_now;
582 if (PREDICT_FALSE ((b0->flags & VLIB_BUFFER_IS_TRACED)))
584 gbp_learn_l3_trace_t *t;
586 t = vlib_add_trace (vm, node, b0, sizeof (*t));
587 if (FIB_PROTOCOL_IP6 == fproto && ip6_0)
588 ip46_address_set_ip6 (&t->ip, &ip6_0->src_address);
589 if (FIB_PROTOCOL_IP4 == fproto && ip4_0)
590 ip46_address_set_ip4 (&t->ip, &ip4_0->src_address);
591 t->new = (NULL == ge0);
593 t->sw_if_index = sw_if_index0;
597 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
598 to_next, n_left_to_next,
602 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
605 return frame->n_vectors;
608 /* packet trace format function */
610 format_gbp_learn_l3_trace (u8 * s, va_list * args)
612 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
613 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
614 gbp_learn_l3_trace_t *t = va_arg (*args, gbp_learn_l3_trace_t *);
616 s = format (s, "new:%d throttled:%d ip:%U itf:%d epg:%d",
617 t->new, t->throttled,
618 format_ip46_address, &t->ip, IP46_TYPE_ANY, t->sw_if_index,
624 VLIB_NODE_FN (gbp_learn_ip4_node) (vlib_main_t * vm,
625 vlib_node_runtime_t * node,
626 vlib_frame_t * frame)
628 return (gbp_learn_l3 (vm, node, frame, FIB_PROTOCOL_IP4));
631 VLIB_NODE_FN (gbp_learn_ip6_node) (vlib_main_t * vm,
632 vlib_node_runtime_t * node,
633 vlib_frame_t * frame)
635 return (gbp_learn_l3 (vm, node, frame, FIB_PROTOCOL_IP6));
639 VLIB_REGISTER_NODE (gbp_learn_ip4_node) = {
640 .name = "gbp-learn-ip4",
641 .vector_size = sizeof (u32),
642 .format_trace = format_gbp_learn_l3_trace,
643 .type = VLIB_NODE_TYPE_INTERNAL,
646 VNET_FEATURE_INIT (gbp_learn_ip4, static) =
648 .arc_name = "ip4-unicast",
649 .node_name = "gbp-learn-ip4",
652 VLIB_REGISTER_NODE (gbp_learn_ip6_node) = {
653 .name = "gbp-learn-ip6",
654 .vector_size = sizeof (u32),
655 .format_trace = format_gbp_learn_l3_trace,
656 .type = VLIB_NODE_TYPE_INTERNAL,
659 VNET_FEATURE_INIT (gbp_learn_ip6, static) =
661 .arc_name = "ip6-unicast",
662 .node_name = "gbp-learn-ip6",
668 * fd.io coding-style-patch-verification: ON
671 * eval: (c-set-style "gnu")