From 3a6adc52fe4cad83883c00ad47aefdfb995fb441 Mon Sep 17 00:00:00 2001 From: Neale Ranns Date: Fri, 18 Sep 2020 07:35:06 +0000 Subject: geneve: Move to plugin Type: refactor Change-Id: I613bf4d6517591351b212bfe6c8d93abf235f5dc Signed-off-by: Neale Ranns --- src/plugins/geneve/CMakeLists.txt | 35 + src/plugins/geneve/decap.c | 1303 ++++++++++++++++++++++++++++++++ src/plugins/geneve/encap.c | 581 ++++++++++++++ src/plugins/geneve/geneve.api | 100 +++ src/plugins/geneve/geneve.c | 1122 +++++++++++++++++++++++++++ src/plugins/geneve/geneve.h | 226 ++++++ src/plugins/geneve/geneve_api.c | 265 +++++++ src/plugins/geneve/geneve_error.def | 18 + src/plugins/geneve/geneve_packet.h | 244 ++++++ src/plugins/geneve/geneve_test.c | 412 ++++++++++ src/plugins/geneve/test/test_geneve.py | 307 ++++++++ 11 files changed, 4613 insertions(+) create mode 100644 src/plugins/geneve/CMakeLists.txt create mode 100644 src/plugins/geneve/decap.c create mode 100644 src/plugins/geneve/encap.c create mode 100644 src/plugins/geneve/geneve.api create mode 100644 src/plugins/geneve/geneve.c create mode 100644 src/plugins/geneve/geneve.h create mode 100644 src/plugins/geneve/geneve_api.c create mode 100644 src/plugins/geneve/geneve_error.def create mode 100644 src/plugins/geneve/geneve_packet.h create mode 100644 src/plugins/geneve/geneve_test.c create mode 100644 src/plugins/geneve/test/test_geneve.py (limited to 'src/plugins') diff --git a/src/plugins/geneve/CMakeLists.txt b/src/plugins/geneve/CMakeLists.txt new file mode 100644 index 00000000000..43eb0172097 --- /dev/null +++ b/src/plugins/geneve/CMakeLists.txt @@ -0,0 +1,35 @@ +# Copyright (c) 2020 Cisco and/or its affiliates. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at: +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +add_vpp_plugin(geneve + SOURCES + geneve.c + encap.c + decap.c + geneve_api.c + + MULTIARCH_SOURCES + encap.c + decap.c + + API_FILES + geneve.api + + INSTALL_HEADERS + geneve.h + geneve_packet.h + geneve_error.def + + API_TEST_SOURCES + geneve_test.c +) diff --git a/src/plugins/geneve/decap.c b/src/plugins/geneve/decap.c new file mode 100644 index 00000000000..f42c0a88a5a --- /dev/null +++ b/src/plugins/geneve/decap.c @@ -0,0 +1,1303 @@ +/* + * Copyright (c) 2017 SUSE LLC. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include + +typedef struct +{ + u32 next_index; + u32 tunnel_index; + u32 error; + u32 vni_rsvd; +} geneve_rx_trace_t; + +static u8 * +format_geneve_rx_trace (u8 * s, va_list * args) +{ + CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); + CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); + geneve_rx_trace_t *t = va_arg (*args, geneve_rx_trace_t *); + + if (t->tunnel_index != ~0) + { + s = + format (s, + "GENEVE decap from geneve_tunnel%d vni %d next %d error %d", + t->tunnel_index, t->vni_rsvd, t->next_index, t->error); + } + else + { + s = format (s, "GENEVE decap error - tunnel for vni %d does not exist", + t->vni_rsvd); + } + return s; +} + +always_inline u32 +validate_geneve_fib (vlib_buffer_t * b, geneve_tunnel_t * t, u32 is_ip4) +{ + u32 fib_index, sw_if_index; + + sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX]; + + if (is_ip4) + fib_index = (vnet_buffer (b)->sw_if_index[VLIB_TX] == (u32) ~ 0) ? + vec_elt (ip4_main.fib_index_by_sw_if_index, sw_if_index) : + vnet_buffer (b)->sw_if_index[VLIB_TX]; + else + fib_index = (vnet_buffer (b)->sw_if_index[VLIB_TX] == (u32) ~ 0) ? + vec_elt (ip6_main.fib_index_by_sw_if_index, sw_if_index) : + vnet_buffer (b)->sw_if_index[VLIB_TX]; + + return (fib_index == t->encap_fib_index); +} + +always_inline uword +geneve_input (vlib_main_t * vm, + vlib_node_runtime_t * node, + vlib_frame_t * from_frame, u32 is_ip4) +{ + u32 n_left_from, next_index, *from, *to_next; + geneve_main_t *vxm = &geneve_main; + vnet_main_t *vnm = vxm->vnet_main; + vnet_interface_main_t *im = &vnm->interface_main; + u32 last_tunnel_index = ~0; + geneve4_tunnel_key_t last_key4; + geneve6_tunnel_key_t last_key6; + u32 pkts_decapsulated = 0; + u32 thread_index = vm->thread_index; + u32 stats_sw_if_index, stats_n_packets, stats_n_bytes; + vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs; + + if (is_ip4) + last_key4.as_u64 = ~0; + else + clib_memset (&last_key6, 0xff, sizeof (last_key6)); + + from = vlib_frame_vector_args (from_frame); + n_left_from = from_frame->n_vectors; + vlib_get_buffers (vm, from, bufs, n_left_from); + + next_index = node->cached_next_index; + stats_sw_if_index = node->runtime_data[0]; + stats_n_packets = stats_n_bytes = 0; + + while (n_left_from > 0) + { + u32 n_left_to_next; + + vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); + while (n_left_from >= 4 && n_left_to_next >= 2) + { + u32 bi0, bi1; + vlib_buffer_t *b0, *b1; + u32 next0, next1; + ip4_header_t *ip4_0, *ip4_1; + ip6_header_t *ip6_0, *ip6_1; + geneve_header_t *geneve0, *geneve1; + uword *p0, *p1; + u32 tunnel_index0, tunnel_index1; + geneve_tunnel_t *t0, *t1, *mt0 = NULL, *mt1 = NULL; + geneve4_tunnel_key_t key4_0, key4_1; + geneve6_tunnel_key_t key6_0, key6_1; + u32 error0, error1; + u32 sw_if_index0, sw_if_index1, len0, len1; + + /* Prefetch next iteration. */ + { + vlib_prefetch_buffer_header (b[2], LOAD); + vlib_prefetch_buffer_header (b[3], LOAD); + + CLIB_PREFETCH (b[2]->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD); + CLIB_PREFETCH (b[3]->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD); + } + + bi0 = from[0]; + bi1 = from[1]; + to_next[0] = bi0; + to_next[1] = bi1; + from += 2; + to_next += 2; + n_left_to_next -= 2; + n_left_from -= 2; + + b0 = b[0]; + b1 = b[1]; + b += 2; + + /* udp leaves current_data pointing at the geneve header */ + geneve0 = vlib_buffer_get_current (b0); + geneve1 = vlib_buffer_get_current (b1); + + vnet_geneve_hdr_1word_ntoh (geneve0); + vnet_geneve_hdr_1word_ntoh (geneve1); + + if (is_ip4) + { + vlib_buffer_advance + (b0, -(word) (sizeof (udp_header_t) + sizeof (ip4_header_t))); + vlib_buffer_advance + (b1, -(word) (sizeof (udp_header_t) + sizeof (ip4_header_t))); + ip4_0 = vlib_buffer_get_current (b0); + ip4_1 = vlib_buffer_get_current (b1); + } + else + { + vlib_buffer_advance + (b0, -(word) (sizeof (udp_header_t) + sizeof (ip6_header_t))); + vlib_buffer_advance + (b1, -(word) (sizeof (udp_header_t) + sizeof (ip6_header_t))); + ip6_0 = vlib_buffer_get_current (b0); + ip6_1 = vlib_buffer_get_current (b1); + } + + /* pop (ip, udp, geneve) */ + if (is_ip4) + { + vlib_buffer_advance (b0, + sizeof (*ip4_0) + sizeof (udp_header_t) + + GENEVE_BASE_HEADER_LENGTH + + vnet_get_geneve_options_len (geneve0)); + vlib_buffer_advance (b1, + sizeof (*ip4_1) + sizeof (udp_header_t) + + GENEVE_BASE_HEADER_LENGTH + + vnet_get_geneve_options_len (geneve1)); + } + else + { + vlib_buffer_advance (b0, + sizeof (*ip6_0) + sizeof (udp_header_t) + + GENEVE_BASE_HEADER_LENGTH + + vnet_get_geneve_options_len (geneve0)); + vlib_buffer_advance (b1, + sizeof (*ip6_1) + sizeof (udp_header_t) + + GENEVE_BASE_HEADER_LENGTH + + vnet_get_geneve_options_len (geneve1)); + } + + tunnel_index0 = ~0; + error0 = 0; + + tunnel_index1 = ~0; + error1 = 0; + + if (PREDICT_FALSE + (vnet_get_geneve_version (geneve0) != GENEVE_VERSION)) + { + error0 = GENEVE_ERROR_BAD_FLAGS; + next0 = GENEVE_INPUT_NEXT_DROP; + goto trace0; + } +#if SUPPORT_OPTIONS_HEADER==1 + if (PREDICT_FALSE (vnet_get_geneve_critical_bit (geneve0) == 1)) + { + error0 = GENEVE_ERROR_BAD_FLAGS; + next0 = GENEVE_INPUT_NEXT_DROP; + goto trace0; + } +#endif + if (is_ip4) + { + key4_0.remote = ip4_0->src_address.as_u32; + key4_0.vni = vnet_get_geneve_vni_network_order (geneve0); + + /* Make sure GENEVE tunnel exist according to packet SIP and VNI */ + if (PREDICT_FALSE (key4_0.as_u64 != last_key4.as_u64)) + { + p0 = hash_get (vxm->geneve4_tunnel_by_key, key4_0.as_u64); + if (PREDICT_FALSE (p0 == NULL)) + { + error0 = GENEVE_ERROR_NO_SUCH_TUNNEL; + next0 = GENEVE_INPUT_NEXT_DROP; + goto trace0; + } + last_key4.as_u64 = key4_0.as_u64; + tunnel_index0 = last_tunnel_index = p0[0]; + } + else + tunnel_index0 = last_tunnel_index; + t0 = pool_elt_at_index (vxm->tunnels, tunnel_index0); + + /* Validate GENEVE tunnel encap-fib index against packet */ + if (PREDICT_FALSE (validate_geneve_fib (b0, t0, is_ip4) == 0)) + { + error0 = GENEVE_ERROR_NO_SUCH_TUNNEL; + next0 = GENEVE_INPUT_NEXT_DROP; + goto trace0; + } + + /* Validate GENEVE tunnel SIP against packet DIP */ + if (PREDICT_TRUE + (ip4_0->dst_address.as_u32 == t0->local.ip4.as_u32)) + goto next0; /* valid packet */ + if (PREDICT_FALSE + (ip4_address_is_multicast (&ip4_0->dst_address))) + { + key4_0.remote = ip4_0->dst_address.as_u32; + key4_0.vni = vnet_get_geneve_vni_network_order (geneve0); + /* Make sure mcast GENEVE tunnel exist by packet DIP and VNI */ + p0 = hash_get (vxm->geneve4_tunnel_by_key, key4_0.as_u64); + if (PREDICT_TRUE (p0 != NULL)) + { + mt0 = pool_elt_at_index (vxm->tunnels, p0[0]); + goto next0; /* valid packet */ + } + } + error0 = GENEVE_ERROR_NO_SUCH_TUNNEL; + next0 = GENEVE_INPUT_NEXT_DROP; + goto trace0; + + } + else /* !is_ip4 */ + { + key6_0.remote.as_u64[0] = ip6_0->src_address.as_u64[0]; + key6_0.remote.as_u64[1] = ip6_0->src_address.as_u64[1]; + key6_0.vni = vnet_get_geneve_vni_network_order (geneve0); + + /* Make sure GENEVE tunnel exist according to packet SIP and VNI */ + if (PREDICT_FALSE + (memcmp (&key6_0, &last_key6, sizeof (last_key6)) != 0)) + { + p0 = hash_get_mem (vxm->geneve6_tunnel_by_key, &key6_0); + if (PREDICT_FALSE (p0 == NULL)) + { + error0 = GENEVE_ERROR_NO_SUCH_TUNNEL; + next0 = GENEVE_INPUT_NEXT_DROP; + goto trace0; + } + clib_memcpy_fast (&last_key6, &key6_0, sizeof (key6_0)); + tunnel_index0 = last_tunnel_index = p0[0]; + } + else + tunnel_index0 = last_tunnel_index; + t0 = pool_elt_at_index (vxm->tunnels, tunnel_index0); + + /* Validate GENEVE tunnel encap-fib index against packet */ + if (PREDICT_FALSE (validate_geneve_fib (b0, t0, is_ip4) == 0)) + { + error0 = GENEVE_ERROR_NO_SUCH_TUNNEL; + next0 = GENEVE_INPUT_NEXT_DROP; + goto trace0; + } + + /* Validate GENEVE tunnel SIP against packet DIP */ + if (PREDICT_TRUE (ip6_address_is_equal (&ip6_0->dst_address, + &t0->local.ip6))) + goto next0; /* valid packet */ + if (PREDICT_FALSE + (ip6_address_is_multicast (&ip6_0->dst_address))) + { + key6_0.remote.as_u64[0] = ip6_0->dst_address.as_u64[0]; + key6_0.remote.as_u64[1] = ip6_0->dst_address.as_u64[1]; + key6_0.vni = vnet_get_geneve_vni_network_order (geneve0); + p0 = hash_get_mem (vxm->geneve6_tunnel_by_key, &key6_0); + if (PREDICT_TRUE (p0 != NULL)) + { + mt0 = pool_elt_at_index (vxm->tunnels, p0[0]); + goto next0; /* valid packet */ + } + } + error0 = GENEVE_ERROR_NO_SUCH_TUNNEL; + next0 = GENEVE_INPUT_NEXT_DROP; + goto trace0; + } + + next0: + next0 = t0->decap_next_index; + sw_if_index0 = t0->sw_if_index; + len0 = vlib_buffer_length_in_chain (vm, b0); + + /* Required to make the l2 tag push / pop code work on l2 subifs */ + if (PREDICT_TRUE (next0 == GENEVE_INPUT_NEXT_L2_INPUT)) + vnet_update_l2_len (b0); + + /* Set packet input sw_if_index to unicast GENEVE tunnel for learning */ + vnet_buffer (b0)->sw_if_index[VLIB_RX] = sw_if_index0; + sw_if_index0 = (mt0) ? mt0->sw_if_index : sw_if_index0; + + pkts_decapsulated++; + stats_n_packets += 1; + stats_n_bytes += len0; + + /* Batch stats increment on the same geneve tunnel so counter + is not incremented per packet */ + if (PREDICT_FALSE (sw_if_index0 != stats_sw_if_index)) + { + stats_n_packets -= 1; + stats_n_bytes -= len0; + if (stats_n_packets) + vlib_increment_combined_counter + (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX, + thread_index, stats_sw_if_index, + stats_n_packets, stats_n_bytes); + stats_n_packets = 1; + stats_n_bytes = len0; + stats_sw_if_index = sw_if_index0; + } + + trace0: + b0->error = error0 ? node->errors[error0] : 0; + + if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED)) + { + geneve_rx_trace_t *tr + = vlib_add_trace (vm, node, b0, sizeof (*tr)); + tr->next_index = next0; + tr->error = error0; + tr->tunnel_index = tunnel_index0; + tr->vni_rsvd = vnet_get_geneve_vni (geneve0); + } + + if (PREDICT_FALSE + (vnet_get_geneve_version (geneve1) != GENEVE_VERSION)) + { + error1 = GENEVE_ERROR_BAD_FLAGS; + next1 = GENEVE_INPUT_NEXT_DROP; + goto trace1; + } +#if SUPPORT_OPTIONS_HEADER==1 + if (PREDICT_FALSE (vnet_get_geneve_critical_bit (geneve1) == 1)) + { + error1 = GENEVE_ERROR_BAD_FLAGS; + next1 = GENEVE_INPUT_NEXT_DROP; + goto trace1; + } +#endif + if (is_ip4) + { + key4_1.remote = ip4_1->src_address.as_u32; + key4_1.vni = vnet_get_geneve_vni_network_order (geneve1); + + /* Make sure unicast GENEVE tunnel exist by packet SIP and VNI */ + if (PREDICT_FALSE (key4_1.as_u64 != last_key4.as_u64)) + { + p1 = hash_get (vxm->geneve4_tunnel_by_key, key4_1.as_u64); + if (PREDICT_FALSE (p1 == NULL)) + { + error1 = GENEVE_ERROR_NO_SUCH_TUNNEL; + next1 = GENEVE_INPUT_NEXT_DROP; + goto trace1; + } + last_key4.as_u64 = key4_1.as_u64; + tunnel_index1 = last_tunnel_index = p1[0]; + } + else + tunnel_index1 = last_tunnel_index; + t1 = pool_elt_at_index (vxm->tunnels, tunnel_index1); + + /* Validate GENEVE tunnel encap-fib index against packet */ + if (PREDICT_FALSE (validate_geneve_fib (b1, t1, is_ip4) == 0)) + { + error1 = GENEVE_ERROR_NO_SUCH_TUNNEL; + next1 = GENEVE_INPUT_NEXT_DROP; + goto trace1; + } + + /* Validate GENEVE tunnel SIP against packet DIP */ + if (PREDICT_TRUE + (ip4_1->dst_address.as_u32 == t1->local.ip4.as_u32)) + goto next1; /* valid packet */ + if (PREDICT_FALSE + (ip4_address_is_multicast (&ip4_1->dst_address))) + { + key4_1.remote = ip4_1->dst_address.as_u32; + key4_1.vni = vnet_get_geneve_vni_network_order (geneve1); + /* Make sure mcast GENEVE tunnel exist by packet DIP and VNI */ + p1 = hash_get (vxm->geneve4_tunnel_by_key, key4_1.as_u64); + if (PREDICT_TRUE (p1 != NULL)) + { + mt1 = pool_elt_at_index (vxm->tunnels, p1[0]); + goto next1; /* valid packet */ + } + } + error1 = GENEVE_ERROR_NO_SUCH_TUNNEL; + next1 = GENEVE_INPUT_NEXT_DROP; + goto trace1; + + } + else /* !is_ip4 */ + { + key6_1.remote.as_u64[0] = ip6_1->src_address.as_u64[0]; + key6_1.remote.as_u64[1] = ip6_1->src_address.as_u64[1]; + key6_1.vni = vnet_get_geneve_vni_network_order (geneve1); + + /* Make sure GENEVE tunnel exist according to packet SIP and VNI */ + if (PREDICT_FALSE + (memcmp (&key6_1, &last_key6, sizeof (last_key6)) != 0)) + { + p1 = hash_get_mem (vxm->geneve6_tunnel_by_key, &key6_1); + + if (PREDICT_FALSE (p1 == NULL)) + { + error1 = GENEVE_ERROR_NO_SUCH_TUNNEL; + next1 = GENEVE_INPUT_NEXT_DROP; + goto trace1; + } + + clib_memcpy_fast (&last_key6, &key6_1, sizeof (key6_1)); + tunnel_index1 = last_tunnel_index = p1[0]; + } + else + tunnel_index1 = last_tunnel_index; + t1 = pool_elt_at_index (vxm->tunnels, tunnel_index1); + + /* Validate GENEVE tunnel encap-fib index against packet */ + if (PREDICT_FALSE (validate_geneve_fib (b1, t1, is_ip4) == 0)) + { + error1 = GENEVE_ERROR_NO_SUCH_TUNNEL; + next1 = GENEVE_INPUT_NEXT_DROP; + goto trace1; + } + + /* Validate GENEVE tunnel SIP against packet DIP */ + if (PREDICT_TRUE (ip6_address_is_equal (&ip6_1->dst_address, + &t1->local.ip6))) + goto next1; /* valid packet */ + if (PREDICT_FALSE + (ip6_address_is_multicast (&ip6_1->dst_address))) + { + key6_1.remote.as_u64[0] = ip6_1->dst_address.as_u64[0]; + key6_1.remote.as_u64[1] = ip6_1->dst_address.as_u64[1]; + key6_1.vni = vnet_get_geneve_vni_network_order (geneve1); + p1 = hash_get_mem (vxm->geneve6_tunnel_by_key, &key6_1); + if (PREDICT_TRUE (p1 != NULL)) + { + mt1 = pool_elt_at_index (vxm->tunnels, p1[0]); + goto next1; /* valid packet */ + } + } + error1 = GENEVE_ERROR_NO_SUCH_TUNNEL; + next1 = GENEVE_INPUT_NEXT_DROP; + goto trace1; + } + + next1: + next1 = t1->decap_next_index; + sw_if_index1 = t1->sw_if_index; + len1 = vlib_buffer_length_in_chain (vm, b1); + + /* Required to make the l2 tag push / pop code work on l2 subifs */ + if (PREDICT_TRUE (next1 == GENEVE_INPUT_NEXT_L2_INPUT)) + vnet_update_l2_len (b1); + + /* Set packet input sw_if_index to unicast GENEVE tunnel for learning */ + vnet_buffer (b1)->sw_if_index[VLIB_RX] = sw_if_index1; + sw_if_index1 = (mt1) ? mt1->sw_if_index : sw_if_index1; + + pkts_decapsulated++; + stats_n_packets += 1; + stats_n_bytes += len1; + + /* Batch stats increment on the same geneve tunnel so counter + is not incremented per packet */ + if (PREDICT_FALSE (sw_if_index1 != stats_sw_if_index)) + { + stats_n_packets -= 1; + stats_n_bytes -= len1; + if (stats_n_packets) + vlib_increment_combined_counter + (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX, + thread_index, stats_sw_if_index, + stats_n_packets, stats_n_bytes); + stats_n_packets = 1; + stats_n_bytes = len1; + stats_sw_if_index = sw_if_index1; + } + + trace1: + b1->error = error1 ? node->errors[error1] : 0; + + if (PREDICT_FALSE (b1->flags & VLIB_BUFFER_IS_TRACED)) + { + geneve_rx_trace_t *tr + = vlib_add_trace (vm, node, b1, sizeof (*tr)); + tr->next_index = next1; + tr->error = error1; + tr->tunnel_index = tunnel_index1; + tr->vni_rsvd = vnet_get_geneve_vni (geneve1); + } + + vlib_validate_buffer_enqueue_x2 (vm, node, next_index, + to_next, n_left_to_next, + bi0, bi1, next0, next1); + } + + while (n_left_from > 0 && n_left_to_next > 0) + { + u32 bi0; + vlib_buffer_t *b0; + u32 next0; + ip4_header_t *ip4_0; + ip6_header_t *ip6_0; + geneve_header_t *geneve0; + uword *p0; + u32 tunnel_index0; + geneve_tunnel_t *t0, *mt0 = NULL; + geneve4_tunnel_key_t key4_0; + geneve6_tunnel_key_t key6_0; + u32 error0; + u32 sw_if_index0, len0; + + bi0 = from[0]; + to_next[0] = bi0; + from += 1; + to_next += 1; + n_left_from -= 1; + n_left_to_next -= 1; + + b0 = b[0]; + b += 1; + + /* udp leaves current_data pointing at the geneve header */ + geneve0 = vlib_buffer_get_current (b0); + vnet_geneve_hdr_1word_ntoh (geneve0); + + if (is_ip4) + { + vlib_buffer_advance + (b0, -(word) (sizeof (udp_header_t) + sizeof (ip4_header_t))); + ip4_0 = vlib_buffer_get_current (b0); + } + else + { + vlib_buffer_advance + (b0, -(word) (sizeof (udp_header_t) + sizeof (ip6_header_t))); + ip6_0 = vlib_buffer_get_current (b0); + } + + /* pop (ip, udp, geneve) */ + if (is_ip4) + { + vlib_buffer_advance + (b0, + sizeof (*ip4_0) + sizeof (udp_header_t) + + GENEVE_BASE_HEADER_LENGTH + + vnet_get_geneve_options_len (geneve0)); + } + else + { + vlib_buffer_advance + (b0, + sizeof (*ip6_0) + sizeof (udp_header_t) + + GENEVE_BASE_HEADER_LENGTH + + vnet_get_geneve_options_len (geneve0)); + } + + tunnel_index0 = ~0; + error0 = 0; + + if (PREDICT_FALSE + (vnet_get_geneve_version (geneve0) != GENEVE_VERSION)) + { + error0 = GENEVE_ERROR_BAD_FLAGS; + next0 = GENEVE_INPUT_NEXT_DROP; + goto trace00; + } +#if SUPPORT_OPTIONS_HEADER==1 + if (PREDICT_FALSE (vnet_get_geneve_critical_bit (geneve0) == 1)) + { + error0 = GENEVE_ERROR_BAD_FLAGS; + next0 = GENEVE_INPUT_NEXT_DROP; + goto trace00; + } +#endif + if (is_ip4) + { + key4_0.remote = ip4_0->src_address.as_u32; + key4_0.vni = vnet_get_geneve_vni_network_order (geneve0); + + /* Make sure unicast GENEVE tunnel exist by packet SIP and VNI */ + if (PREDICT_FALSE (key4_0.as_u64 != last_key4.as_u64)) + { + p0 = hash_get (vxm->geneve4_tunnel_by_key, key4_0.as_u64); + if (PREDICT_FALSE (p0 == NULL)) + { + error0 = GENEVE_ERROR_NO_SUCH_TUNNEL; + next0 = GENEVE_INPUT_NEXT_DROP; + goto trace00; + } + last_key4.as_u64 = key4_0.as_u64; + tunnel_index0 = last_tunnel_index = p0[0]; + } + else + tunnel_index0 = last_tunnel_index; + t0 = pool_elt_at_index (vxm->tunnels, tunnel_index0); + + /* Validate GENEVE tunnel encap-fib index agaist packet */ + if (PREDICT_FALSE (validate_geneve_fib (b0, t0, is_ip4) == 0)) + { + error0 = GENEVE_ERROR_NO_SUCH_TUNNEL; + next0 = GENEVE_INPUT_NEXT_DROP; + goto trace00; + } + + /* Validate GENEVE tunnel SIP against packet DIP */ + if (PREDICT_TRUE + (ip4_0->dst_address.as_u32 == t0->local.ip4.as_u32)) + goto next00; /* valid packet */ + if (PREDICT_FALSE + (ip4_address_is_multicast (&ip4_0->dst_address))) + { + key4_0.remote = ip4_0->dst_address.as_u32; + key4_0.vni = vnet_get_geneve_vni_network_order (geneve0); + /* Make sure mcast GENEVE tunnel exist by packet DIP and VNI */ + p0 = hash_get (vxm->geneve4_tunnel_by_key, key4_0.as_u64); + if (PREDICT_TRUE (p0 != NULL)) + { + mt0 = pool_elt_at_index (vxm->tunnels, p0[0]); + goto next00; /* valid packet */ + } + } + error0 = GENEVE_ERROR_NO_SUCH_TUNNEL; + next0 = GENEVE_INPUT_NEXT_DROP; + goto trace00; + + } + else /* !is_ip4 */ + { + key6_0.remote.as_u64[0] = ip6_0->src_address.as_u64[0]; + key6_0.remote.as_u64[1] = ip6_0->src_address.as_u64[1]; + key6_0.vni = vnet_get_geneve_vni_network_order (geneve0); + + /* Make sure GENEVE tunnel exist according to packet SIP and VNI */ + if (PREDICT_FALSE + (memcmp (&key6_0, &last_key6, sizeof (last_key6)) != 0)) + { + p0 = hash_get_mem (vxm->geneve6_tunnel_by_key, &key6_0); + if (PREDICT_FALSE (p0 == NULL)) + { + error0 = GENEVE_ERROR_NO_SUCH_TUNNEL; + next0 = GENEVE_INPUT_NEXT_DROP; + goto trace00; + } + clib_memcpy_fast (&last_key6, &key6_0, sizeof (key6_0)); + tunnel_index0 = last_tunnel_index = p0[0]; + } + else + tunnel_index0 = last_tunnel_index; + t0 = pool_elt_at_index (vxm->tunnels, tunnel_index0); + + /* Validate GENEVE tunnel encap-fib index agaist packet */ + if (PREDICT_FALSE (validate_geneve_fib (b0, t0, is_ip4) == 0)) + { + error0 = GENEVE_ERROR_NO_SUCH_TUNNEL; + next0 = GENEVE_INPUT_NEXT_DROP; + goto trace00; + } + + /* Validate GENEVE tunnel SIP against packet DIP */ + if (PREDICT_TRUE (ip6_address_is_equal (&ip6_0->dst_address, + &t0->local.ip6))) + goto next00; /* valid packet */ + if (PREDICT_FALSE + (ip6_address_is_multicast (&ip6_0->dst_address))) + { + key6_0.remote.as_u64[0] = ip6_0->dst_address.as_u64[0]; + key6_0.remote.as_u64[1] = ip6_0->dst_address.as_u64[1]; + key6_0.vni = vnet_get_geneve_vni_network_order (geneve0); + p0 = hash_get_mem (vxm->geneve6_tunnel_by_key, &key6_0); + if (PREDICT_TRUE (p0 != NULL)) + { + mt0 = pool_elt_at_index (vxm->tunnels, p0[0]); + goto next00; /* valid packet */ + } + } + error0 = GENEVE_ERROR_NO_SUCH_TUNNEL; + next0 = GENEVE_INPUT_NEXT_DROP; + goto trace00; + } + + next00: + next0 = t0->decap_next_index; + sw_if_index0 = t0->sw_if_index; + len0 = vlib_buffer_length_in_chain (vm, b0); + + /* Required to make the l2 tag push / pop code work on l2 subifs */ + if (PREDICT_TRUE (next0 == GENEVE_INPUT_NEXT_L2_INPUT)) + vnet_update_l2_len (b0); + + /* Set packet input sw_if_index to unicast GENEVE tunnel for learning */ + vnet_buffer (b0)->sw_if_index[VLIB_RX] = sw_if_index0; + sw_if_index0 = (mt0) ? mt0->sw_if_index : sw_if_index0; + + pkts_decapsulated++; + stats_n_packets += 1; + stats_n_bytes += len0; + + /* Batch stats increment on the same geneve tunnel so counter + is not incremented per packet */ + if (PREDICT_FALSE (sw_if_index0 != stats_sw_if_index)) + { + stats_n_packets -= 1; + stats_n_bytes -= len0; + if (stats_n_packets) + vlib_increment_combined_counter + (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX, + thread_index, stats_sw_if_index, + stats_n_packets, stats_n_bytes); + stats_n_packets = 1; + stats_n_bytes = len0; + stats_sw_if_index = sw_if_index0; + } + + trace00: + b0->error = error0 ? node->errors[error0] : 0; + + if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED)) + { + geneve_rx_trace_t *tr + = vlib_add_trace (vm, node, b0, sizeof (*tr)); + tr->next_index = next0; + tr->error = error0; + tr->tunnel_index = tunnel_index0; + tr->vni_rsvd = vnet_get_geneve_vni (geneve0); + } + vlib_validate_buffer_enqueue_x1 (vm, node, next_index, + to_next, n_left_to_next, + bi0, next0); + } + + vlib_put_next_frame (vm, node, next_index, n_left_to_next); + } + /* Do we still need this now that tunnel tx stats is kept? */ + vlib_node_increment_counter (vm, is_ip4 ? + geneve4_input_node. + index : geneve6_input_node.index, + GENEVE_ERROR_DECAPSULATED, pkts_decapsulated); + + /* Increment any remaining batch stats */ + if (stats_n_packets) + { + vlib_increment_combined_counter + (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX, + thread_index, stats_sw_if_index, stats_n_packets, stats_n_bytes); + node->runtime_data[0] = stats_sw_if_index; + } + + return from_frame->n_vectors; +} + +VLIB_NODE_FN (geneve4_input_node) (vlib_main_t * vm, + vlib_node_runtime_t * node, + vlib_frame_t * from_frame) +{ + return geneve_input (vm, node, from_frame, /* is_ip4 */ 1); +} + +VLIB_NODE_FN (geneve6_input_node) (vlib_main_t * vm, + vlib_node_runtime_t * node, + vlib_frame_t * from_frame) +{ + return geneve_input (vm, node, from_frame, /* is_ip4 */ 0); +} + +static char *geneve_error_strings[] = { +#define geneve_error(n,s) s, +#include +#undef geneve_error +#undef _ +}; + +/* *INDENT-OFF* */ +VLIB_REGISTER_NODE (geneve4_input_node) = { + .name = "geneve4-input", + /* Takes a vector of packets. */ + .vector_size = sizeof (u32), + .n_errors = GENEVE_N_ERROR, + .error_strings = geneve_error_strings, + .n_next_nodes = GENEVE_INPUT_N_NEXT, + .next_nodes = { +#define _(s,n) [GENEVE_INPUT_NEXT_##s] = n, + foreach_geneve_input_next +#undef _ + }, + +//temp .format_buffer = format_geneve_header, + .format_trace = format_geneve_rx_trace, + // $$$$ .unformat_buffer = unformat_geneve_header, +}; + +VLIB_REGISTER_NODE (geneve6_input_node) = { + .name = "geneve6-input", + /* Takes a vector of packets. */ + .vector_size = sizeof (u32), + .n_errors = GENEVE_N_ERROR, + .error_strings = geneve_error_strings, + .n_next_nodes = GENEVE_INPUT_N_NEXT, + .next_nodes = { +#define _(s,n) [GENEVE_INPUT_NEXT_##s] = n, + foreach_geneve_input_next +#undef _ + }, +//temp .format_buffer = format_geneve_header, + .format_trace = format_geneve_rx_trace, + // $$$$ .unformat_buffer = unformat_geneve_header, +}; +/* *INDENT-ON* */ + +typedef enum +{ + IP_GENEVE_BYPASS_NEXT_DROP, + IP_GENEVE_BYPASS_NEXT_GENEVE, + IP_GENEVE_BYPASS_N_NEXT, +} ip_vxan_bypass_next_t; + +always_inline uword +ip_geneve_bypass_inline (vlib_main_t * vm, + vlib_node_runtime_t * node, + vlib_frame_t * frame, u32 is_ip4) +{ + geneve_main_t *vxm = &geneve_main; + u32 *from, *to_next, n_left_from, n_left_to_next, next_index; + vlib_node_runtime_t *error_node = + vlib_node_get_runtime (vm, ip4_input_node.index); + vtep4_key_t last_vtep4; /* last IPv4 address / fib index + matching a local VTEP address */ + vtep6_key_t last_vtep6; /* last IPv6 address / fib index + matching a local VTEP address */ + vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs; +#ifdef CLIB_HAVE_VEC512 + vtep4_cache_t vtep4_u512; + clib_memset (&vtep4_u512, 0, sizeof (vtep4_u512)); +#endif + + from = vlib_frame_vector_args (frame); + n_left_from = frame->n_vectors; + next_index = node->cached_next_index; + + vlib_get_buffers (vm, from, bufs, n_left_from); + + if (node->flags & VLIB_NODE_FLAG_TRACE) + ip4_forward_next_trace (vm, node, frame, VLIB_TX); + + if (is_ip4) + vtep4_key_init (&last_vtep4); + else + vtep6_key_init (&last_vtep6); + + while (n_left_from > 0) + { + vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); + + while (n_left_from >= 4 && n_left_to_next >= 2) + { + vlib_buffer_t *b0, *b1; + ip4_header_t *ip40, *ip41; + ip6_header_t *ip60, *ip61; + udp_header_t *udp0, *udp1; + u32 bi0, ip_len0, udp_len0, flags0, next0; + u32 bi1, ip_len1, udp_len1, flags1, next1; + i32 len_diff0, len_diff1; + u8 error0, good_udp0, proto0; + u8 error1, good_udp1, proto1; + + /* Prefetch next iteration. */ + { + vlib_prefetch_buffer_header (b[2], LOAD); + vlib_prefetch_buffer_header (b[3], LOAD); + + CLIB_PREFETCH (b[2]->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD); + CLIB_PREFETCH (b[3]->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD); + } + + bi0 = to_next[0] = from[0]; + bi1 = to_next[1] = from[1]; + from += 2; + n_left_from -= 2; + to_next += 2; + n_left_to_next -= 2; + + b0 = b[0]; + b1 = b[1]; + b += 2; + if (is_ip4) + { + ip40 = vlib_buffer_get_current (b0); + ip41 = vlib_buffer_get_current (b1); + } + else + { + ip60 = vlib_buffer_get_current (b0); + ip61 = vlib_buffer_get_current (b1); + } + + /* Setup packet for next IP feature */ + vnet_feature_next (&next0, b0); + vnet_feature_next (&next1, b1); + + if (is_ip4) + { + /* Treat IP frag packets as "experimental" protocol for now + until support of IP frag reassembly is implemented */ + proto0 = ip4_is_fragment (ip40) ? 0xfe : ip40->protocol; + proto1 = ip4_is_fragment (ip41) ? 0xfe : ip41->protocol; + } + else + { + proto0 = ip60->protocol; + proto1 = ip61->protocol; + } + + /* Process packet 0 */ + if (proto0 != IP_PROTOCOL_UDP) + goto exit0; /* not UDP packet */ + + if (is_ip4) + udp0 = ip4_next_header (ip40); + else + udp0 = ip6_next_header (ip60); + + if (udp0->dst_port != clib_host_to_net_u16 (UDP_DST_PORT_geneve)) + goto exit0; /* not GENEVE packet */ + + /* Validate DIP against VTEPs */ + if (is_ip4) + { +#ifdef CLIB_HAVE_VEC512 + if (!vtep4_check_vector + (&vxm->vtep_table, b0, ip40, &last_vtep4, &vtep4_u512)) +#else + if (!vtep4_check (&vxm->vtep_table, b0, ip40, &last_vtep4)) +#endif + goto exit0; /* no local VTEP for GENEVE packet */ + } + else + { + if (!vtep6_check (&vxm->vtep_table, b0, ip60, &last_vtep6)) + goto exit0; /* no local VTEP for GENEVE packet */ + } + + flags0 = b0->flags; + good_udp0 = (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0; + + /* Don't verify UDP checksum for packets with explicit zero checksum. */ + good_udp0 |= udp0->checksum == 0; + + /* Verify UDP length */ + if (is_ip4) + ip_len0 = clib_net_to_host_u16 (ip40->length); + else + ip_len0 = clib_net_to_host_u16 (ip60->payload_length); + udp_len0 = clib_net_to_host_u16 (udp0->length); + len_diff0 = ip_len0 - udp_len0; + + /* Verify UDP checksum */ + if (PREDICT_FALSE (!good_udp0)) + { + if ((flags0 & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED) == 0) + { + if (is_ip4) + flags0 = ip4_tcp_udp_validate_checksum (vm, b0); + else + flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, b0); + good_udp0 = + (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0; + } + } + + if (is_ip4) + { + error0 = good_udp0 ? 0 : IP4_ERROR_UDP_CHECKSUM; + error0 = (len_diff0 >= 0) ? error0 : IP4_ERROR_UDP_LENGTH; + } + else + { + error0 = good_udp0 ? 0 : IP6_ERROR_UDP_CHECKSUM; + error0 = (len_diff0 >= 0) ? error0 : IP6_ERROR_UDP_LENGTH; + } + + next0 = error0 ? + IP_GENEVE_BYPASS_NEXT_DROP : IP_GENEVE_BYPASS_NEXT_GENEVE; + b0->error = error0 ? error_node->errors[error0] : 0; + + /* geneve-input node expect current at GENEVE header */ + if (is_ip4) + vlib_buffer_advance (b0, + sizeof (ip4_header_t) + + sizeof (udp_header_t)); + else + vlib_buffer_advance (b0, + sizeof (ip6_header_t) + + sizeof (udp_header_t)); + + exit0: + /* Process packet 1 */ + if (proto1 != IP_PROTOCOL_UDP) + goto exit1; /* not UDP packet */ + + if (is_ip4) + udp1 = ip4_next_header (ip41); + else + udp1 = ip6_next_header (ip61); + + if (udp1->dst_port != clib_host_to_net_u16 (UDP_DST_PORT_geneve)) + goto exit1; /* not GENEVE packet */ + + /* Validate DIP against VTEPs */ + if (is_ip4) + { +#ifdef CLIB_HAVE_VEC512 + if (!vtep4_check_vector + (&vxm->vtep_table, b1, ip41, &last_vtep4, &vtep4_u512)) +#else + if (!vtep4_check (&vxm->vtep_table, b1, ip41, &last_vtep4)) +#endif + goto exit1; /* no local VTEP for GENEVE packet */ + } + else + { + if (!vtep6_check (&vxm->vtep_table, b1, ip61, &last_vtep6)) + goto exit1; /* no local VTEP for GENEVE packet */ + } + + flags1 = b1->flags; + good_udp1 = (flags1 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0; + + /* Don't verify UDP checksum for packets with explicit zero checksum. */ + good_udp1 |= udp1->checksum == 0; + + /* Verify UDP length */ + if (is_ip4) + ip_len1 = clib_net_to_host_u16 (ip41->length); + else + ip_len1 = clib_net_to_host_u16 (ip61->payload_length); + udp_len1 = clib_net_to_host_u16 (udp1->length); + len_diff1 = ip_len1 - udp_len1; + + /* Verify UDP checksum */ + if (PREDICT_FALSE (!good_udp1)) + { + if ((flags1 & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED) == 0) + { + if (is_ip4) + flags1 = ip4_tcp_udp_validate_checksum (vm, b1); + else + flags1 = ip6_tcp_udp_icmp_validate_checksum (vm, b1); + good_udp1 = + (flags1 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0; + } + } + + if (is_ip4) + { + error1 = good_udp1 ? 0 : IP4_ERROR_UDP_CHECKSUM; + error1 = (len_diff1 >= 0) ? error1 : IP4_ERROR_UDP_LENGTH; + } + else + { + error1 = good_udp1 ? 0 : IP6_ERROR_UDP_CHECKSUM; + error1 = (len_diff1 >= 0) ? error1 : IP6_ERROR_UDP_LENGTH; + } + + next1 = error1 ? + IP_GENEVE_BYPASS_NEXT_DROP : IP_GENEVE_BYPASS_NEXT_GENEVE; + b1->error = error1 ? error_node->errors[error1] : 0; + + /* geneve-input node expect current at GENEVE header */ + if (is_ip4) + vlib_buffer_advance (b1, + sizeof (ip4_header_t) + + sizeof (udp_header_t)); + else + vlib_buffer_advance (b1, + sizeof (ip6_header_t) + + sizeof (udp_header_t)); + + exit1: + vlib_validate_buffer_enqueue_x2 (vm, node, next_index, + to_next, n_left_to_next, + bi0, bi1, next0, next1); + } + + while (n_left_from > 0 && n_left_to_next > 0) + { + vlib_buffer_t *b0; + ip4_header_t *ip40; + ip6_header_t *ip60; + udp_header_t *udp0; + u32 bi0, ip_len0, udp_len0, flags0, next0; + i32 len_diff0; + u8 error0, good_udp0, proto0; + + bi0 = to_next[0] = from[0]; + from += 1; + n_left_from -= 1; + to_next += 1; + n_left_to_next -= 1; + + b0 = b[0]; + b++; + if (is_ip4) + ip40 = vlib_buffer_get_current (b0); + else + ip60 = vlib_buffer_get_current (b0); + + /* Setup packet for next IP feature */ + vnet_feature_next (&next0, b0); + + if (is_ip4) + /* Treat IP4 frag packets as "experimental" protocol for now + until support of IP frag reassembly is implemented */ + proto0 = ip4_is_fragment (ip40) ? 0xfe : ip40->protocol; + else + proto0 = ip60->protocol; + + if (proto0 != IP_PROTOCOL_UDP) + goto exit; /* not UDP packet */ + + if (is_ip4) + udp0 = ip4_next_header (ip40); + else + udp0 = ip6_next_header (ip60); + + if (udp0->dst_port != clib_host_to_net_u16 (UDP_DST_PORT_geneve)) + goto exit; /* not GENEVE packet */ + + /* Validate DIP against VTEPs */ + if (is_ip4) + { +#ifdef CLIB_HAVE_VEC512 + if (!vtep4_check_vector + (&vxm->vtep_table, b0, ip40, &last_vtep4, &vtep4_u512)) +#else + if (!vtep4_check (&vxm->vtep_table, b0, ip40, &last_vtep4)) +#endif + goto exit; /* no local VTEP for GENEVE packet */ + } + else + { + if (!vtep6_check (&vxm->vtep_table, b0, ip60, &last_vtep6)) + goto exit; /* no local VTEP for GENEVE packet */ + } + + flags0 = b0->flags; + good_udp0 = (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0; + + /* Don't verify UDP checksum for packets with explicit zero checksum. */ + good_udp0 |= udp0->checksum == 0; + + /* Verify UDP length */ + if (is_ip4) + ip_len0 = clib_net_to_host_u16 (ip40->length); + else + ip_len0 = clib_net_to_host_u16 (ip60->payload_length); + udp_len0 = clib_net_to_host_u16 (udp0->length); + len_diff0 = ip_len0 - udp_len0; + + /* Verify UDP checksum */ + if (PREDICT_FALSE (!good_udp0)) + { + if ((flags0 & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED) == 0) + { + if (is_ip4) + flags0 = ip4_tcp_udp_validate_checksum (vm, b0); + else + flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, b0); + good_udp0 = + (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0; + } + } + + if (is_ip4) + { + error0 = good_udp0 ? 0 : IP4_ERROR_UDP_CHECKSUM; + error0 = (len_diff0 >= 0) ? error0 : IP4_ERROR_UDP_LENGTH; + } + else + { + error0 = good_udp0 ? 0 : IP6_ERROR_UDP_CHECKSUM; + error0 = (len_diff0 >= 0) ? error0 : IP6_ERROR_UDP_LENGTH; + } + + next0 = error0 ? + IP_GENEVE_BYPASS_NEXT_DROP : IP_GENEVE_BYPASS_NEXT_GENEVE; + b0->error = error0 ? error_node->errors[error0] : 0; + + /* geneve-input node expect current at GENEVE header */ + if (is_ip4) + vlib_buffer_advance (b0, + sizeof (ip4_header_t) + + sizeof (udp_header_t)); + else + vlib_buffer_advance (b0, + sizeof (ip6_header_t) + + sizeof (udp_header_t)); + + exit: + vlib_validate_buffer_enqueue_x1 (vm, node, next_index, + to_next, n_left_to_next, + bi0, next0); + } + + vlib_put_next_frame (vm, node, next_index, n_left_to_next); + } + + return frame->n_vectors; +} + +VLIB_NODE_FN (ip4_geneve_bypass_node) (vlib_main_t * vm, + vlib_node_runtime_t * node, + vlib_frame_t * frame) +{ + return ip_geneve_bypass_inline (vm, node, frame, /* is_ip4 */ 1); +} + +/* *INDENT-OFF* */ +VLIB_REGISTER_NODE (ip4_geneve_bypass_node) = +{ + .name = "ip4-geneve-bypass", + .vector_size = sizeof (u32), + .n_next_nodes = IP_GENEVE_BYPASS_N_NEXT,.next_nodes = + { + [IP_GENEVE_BYPASS_NEXT_DROP] = "error-drop", + [IP_GENEVE_BYPASS_NEXT_GENEVE] = "geneve4-input", + }, + .format_buffer = format_ip4_header, + .format_trace = format_ip4_forward_next_trace, +}; +/* *INDENT-ON* */ + +VLIB_NODE_FN (ip6_geneve_bypass_node) (vlib_main_t * vm, + vlib_node_runtime_t * node, + vlib_frame_t * frame) +{ + return ip_geneve_bypass_inline (vm, node, frame, /* is_ip4 */ 0); +} + +/* *INDENT-OFF* */ +VLIB_REGISTER_NODE (ip6_geneve_bypass_node) = +{ + .name = "ip6-geneve-bypass", + .vector_size = sizeof (u32), + .n_next_nodes = IP_GENEVE_BYPASS_N_NEXT, + .next_nodes = + { + [IP_GENEVE_BYPASS_NEXT_DROP] = "error-drop", + [IP_GENEVE_BYPASS_NEXT_GENEVE] = "geneve6-input", + }, + .format_buffer = format_ip6_header, + .format_trace = format_ip6_forward_next_trace, +}; +/* *INDENT-ON* */ + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/plugins/geneve/encap.c b/src/plugins/geneve/encap.c new file mode 100644 index 00000000000..6a84d0c5490 --- /dev/null +++ b/src/plugins/geneve/encap.c @@ -0,0 +1,581 @@ +/* + * Copyright (c) 2017 SUSE LLC. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include +#include +#include +#include + +/* Statistics (not all errors) */ +#define foreach_geneve_encap_error \ +_(ENCAPSULATED, "good packets encapsulated") + +static char *geneve_encap_error_strings[] = { +#define _(sym,string) string, + foreach_geneve_encap_error +#undef _ +}; + +typedef enum +{ +#define _(sym,str) GENEVE_ENCAP_ERROR_##sym, + foreach_geneve_encap_error +#undef _ + GENEVE_ENCAP_N_ERROR, +} geneve_encap_error_t; + +typedef enum +{ + GENEVE_ENCAP_NEXT_DROP, + GENEVE_ENCAP_N_NEXT, +} geneve_encap_next_t; + +#define foreach_fixed_header4_offset \ + _(0) _(1) _(2) _(3) + +#define foreach_fixed_header6_offset \ + _(0) _(1) _(2) _(3) _(4) _(5) _(6) + +always_inline uword +geneve_encap_inline (vlib_main_t * vm, + vlib_node_runtime_t * node, + vlib_frame_t * from_frame, u32 is_ip4) +{ + u32 n_left_from, next_index, *from, *to_next; + geneve_main_t *vxm = &geneve_main; + vnet_main_t *vnm = vxm->vnet_main; + vnet_interface_main_t *im = &vnm->interface_main; + u32 pkts_encapsulated = 0; + u16 old_l0 = 0, old_l1 = 0; + u32 thread_index = vm->thread_index; + u32 stats_sw_if_index, stats_n_packets, stats_n_bytes; + u32 sw_if_index0 = ~0, sw_if_index1 = ~0; + u32 next0 = 0, next1 = 0; + vnet_hw_interface_t *hi0, *hi1; + geneve_tunnel_t *t0 = NULL, *t1 = NULL; + vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs; + + from = vlib_frame_vector_args (from_frame); + n_left_from = from_frame->n_vectors; + vlib_get_buffers (vm, from, bufs, n_left_from); + + next_index = node->cached_next_index; + stats_sw_if_index = node->runtime_data[0]; + stats_n_packets = stats_n_bytes = 0; + + while (n_left_from > 0) + { + u32 n_left_to_next; + + vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); + + while (n_left_from >= 4 && n_left_to_next >= 2) + { + u32 bi0, bi1; + u32 flow_hash0, flow_hash1; + u32 len0, len1; + ip4_header_t *ip4_0, *ip4_1; + ip6_header_t *ip6_0, *ip6_1; + udp_header_t *udp0, *udp1; + u64 *copy_src0, *copy_dst0; + u64 *copy_src1, *copy_dst1; + u32 *copy_src_last0, *copy_dst_last0; + u32 *copy_src_last1, *copy_dst_last1; + u16 new_l0, new_l1; + ip_csum_t sum0, sum1; + + /* Prefetch next iteration. */ + { + vlib_prefetch_buffer_header (b[2], LOAD); + vlib_prefetch_buffer_header (b[3], LOAD); + + CLIB_PREFETCH (b[2]->data - CLIB_CACHE_LINE_BYTES, + 2 * CLIB_CACHE_LINE_BYTES, LOAD); + CLIB_PREFETCH (b[3]->data - CLIB_CACHE_LINE_BYTES, + 2 * CLIB_CACHE_LINE_BYTES, LOAD); + } + + bi0 = from[0]; + bi1 = from[1]; + to_next[0] = bi0; + to_next[1] = bi1; + from += 2; + to_next += 2; + n_left_to_next -= 2; + n_left_from -= 2; + + flow_hash0 = vnet_l2_compute_flow_hash (b[0]); + flow_hash1 = vnet_l2_compute_flow_hash (b[1]); + + + /* Get next node index and adj index from tunnel next_dpo */ + if (sw_if_index0 != vnet_buffer (b[0])->sw_if_index[VLIB_TX]) + { + sw_if_index0 = vnet_buffer (b[0])->sw_if_index[VLIB_TX]; + hi0 = vnet_get_sup_hw_interface (vnm, sw_if_index0); + t0 = &vxm->tunnels[hi0->dev_instance]; + /* Note: change to always set next0 if it may be set to drop */ + next0 = t0->next_dpo.dpoi_next_node; + } + + ALWAYS_ASSERT (t0 != NULL); + + vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = t0->next_dpo.dpoi_index; + + /* Get next node index and adj index from tunnel next_dpo */ + if (sw_if_index1 != vnet_buffer (b[1])->sw_if_index[VLIB_TX]) + { + sw_if_index1 = vnet_buffer (b[1])->sw_if_index[VLIB_TX]; + hi1 = vnet_get_sup_hw_interface (vnm, sw_if_index1); + t1 = &vxm->tunnels[hi1->dev_instance]; + /* Note: change to always set next1 if it may be set to drop */ + next1 = t1->next_dpo.dpoi_next_node; + } + + ALWAYS_ASSERT (t1 != NULL); + + vnet_buffer (b[1])->ip.adj_index[VLIB_TX] = t1->next_dpo.dpoi_index; + + /* Apply the rewrite string. $$$$ vnet_rewrite? */ + vlib_buffer_advance (b[0], -(word) _vec_len (t0->rewrite)); + vlib_buffer_advance (b[1], -(word) _vec_len (t1->rewrite)); + + if (is_ip4) + { + u8 ip4_geneve_base_header_len = + sizeof (ip4_header_t) + sizeof (udp_header_t) + + GENEVE_BASE_HEADER_LENGTH; + u8 ip4_geneve_header_total_len0 = ip4_geneve_base_header_len; + u8 ip4_geneve_header_total_len1 = ip4_geneve_base_header_len; +#if SUPPORT_OPTIONS_HEADER==1 + ip4_geneve_header_total_len0 += t0->options_len; + ip4_geneve_header_total_len1 += t1->options_len; +#endif + ASSERT (vec_len (t0->rewrite) == ip4_geneve_header_total_len0); + ASSERT (vec_len (t1->rewrite) == ip4_geneve_header_total_len1); + + ip4_0 = vlib_buffer_get_current (b[0]); + ip4_1 = vlib_buffer_get_current (b[1]); + + /* Copy the fixed header */ + copy_dst0 = (u64 *) ip4_0; + copy_src0 = (u64 *) t0->rewrite; + copy_dst1 = (u64 *) ip4_1; + copy_src1 = (u64 *) t1->rewrite; + /* Copy first 32 octets 8-bytes at a time */ +#define _(offs) copy_dst0[offs] = copy_src0[offs]; + foreach_fixed_header4_offset; +#undef _ +#define _(offs) copy_dst1[offs] = copy_src1[offs]; + foreach_fixed_header4_offset; +#undef _ + /* Last 4 octets. Hopefully gcc will be our friend */ + copy_dst_last0 = (u32 *) (©_dst0[4]); + copy_src_last0 = (u32 *) (©_src0[4]); + copy_dst_last0[0] = copy_src_last0[0]; + copy_dst_last1 = (u32 *) (©_dst1[4]); + copy_src_last1 = (u32 *) (©_src1[4]); + copy_dst_last1[0] = copy_src_last1[0]; + + /* Fix the IP4 checksum and length */ + sum0 = ip4_0->checksum; + new_l0 = /* old_l0 always 0, see the rewrite setup */ + clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b[0])); + sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t, + length /* changed member */ ); + ip4_0->checksum = ip_csum_fold (sum0); + ip4_0->length = new_l0; + sum1 = ip4_1->checksum; + new_l1 = /* old_l1 always 0, see the rewrite setup */ + clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b[1])); + sum1 = ip_csum_update (sum1, old_l1, new_l1, ip4_header_t, + length /* changed member */ ); + ip4_1->checksum = ip_csum_fold (sum1); + ip4_1->length = new_l1; + + /* Fix UDP length and set source port */ + udp0 = (udp_header_t *) (ip4_0 + 1); + new_l0 = + clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b[0]) - + sizeof (*ip4_0)); + udp0->length = new_l0; + udp0->src_port = flow_hash0; + udp1 = (udp_header_t *) (ip4_1 + 1); + new_l1 = + clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b[1]) - + sizeof (*ip4_1)); + udp1->length = new_l1; + udp1->src_port = flow_hash1; + } + else /* ipv6 */ + { + int bogus = 0; + + u8 ip6_geneve_base_header_len = + sizeof (ip6_header_t) + sizeof (udp_header_t) + + GENEVE_BASE_HEADER_LENGTH; + u8 ip6_geneve_header_total_len0 = ip6_geneve_base_header_len; + u8 ip6_geneve_header_total_len1 = ip6_geneve_base_header_len; +#if SUPPORT_OPTIONS_HEADER==1 + ip6_geneve_header_total_len0 += t0->options_len; + ip6_geneve_header_total_len1 += t1->options_len; +#endif + ASSERT (vec_len (t0->rewrite) == ip6_geneve_header_total_len0); + ASSERT (vec_len (t1->rewrite) == ip6_geneve_header_total_len1); + + ip6_0 = vlib_buffer_get_current (b[0]); + ip6_1 = vlib_buffer_get_current (b[1]); + + /* Copy the fixed header */ + copy_dst0 = (u64 *) ip6_0; + copy_src0 = (u64 *) t0->rewrite; + copy_dst1 = (u64 *) ip6_1; + copy_src1 = (u64 *) t1->rewrite; + /* Copy first 56 (ip6) octets 8-bytes at a time */ +#define _(offs) copy_dst0[offs] = copy_src0[offs]; + foreach_fixed_header6_offset; +#undef _ +#define _(offs) copy_dst1[offs] = copy_src1[offs]; + foreach_fixed_header6_offset; +#undef _ + /* Fix IP6 payload length */ + new_l0 = + clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b[0]) + - sizeof (*ip6_0)); + ip6_0->payload_length = new_l0; + new_l1 = + clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b[1]) + - sizeof (*ip6_1)); + ip6_1->payload_length = new_l1; + + /* Fix UDP length and set source port */ + udp0 = (udp_header_t *) (ip6_0 + 1); + udp0->length = new_l0; + udp0->src_port = flow_hash0; + udp1 = (udp_header_t *) (ip6_1 + 1); + udp1->length = new_l1; + udp1->src_port = flow_hash1; + + /* IPv6 UDP checksum is mandatory */ + udp0->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b[0], + ip6_0, + &bogus); + ASSERT (bogus == 0); + if (udp0->checksum == 0) + udp0->checksum = 0xffff; + udp1->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b[1], + ip6_1, + &bogus); + ASSERT (bogus == 0); + if (udp1->checksum == 0) + udp1->checksum = 0xffff; + } + + pkts_encapsulated += 2; + len0 = vlib_buffer_length_in_chain (vm, b[0]); + len1 = vlib_buffer_length_in_chain (vm, b[1]); + stats_n_packets += 2; + stats_n_bytes += len0 + len1; + + /* save inner packet flow_hash for load-balance node */ + vnet_buffer (b[0])->ip.flow_hash = flow_hash0; + vnet_buffer (b[1])->ip.flow_hash = flow_hash1; + + /* Batch stats increment on the same geneve tunnel so counter is not + incremented per packet. Note stats are still incremented for deleted + and admin-down tunnel where packets are dropped. It is not worthwhile + to check for this rare case and affect normal path performance. */ + if (PREDICT_FALSE ((sw_if_index0 != stats_sw_if_index) || + (sw_if_index1 != stats_sw_if_index))) + { + stats_n_packets -= 2; + stats_n_bytes -= len0 + len1; + if (sw_if_index0 == sw_if_index1) + { + if (stats_n_packets) + vlib_increment_combined_counter + (im->combined_sw_if_counters + + VNET_INTERFACE_COUNTER_TX, thread_index, + stats_sw_if_index, stats_n_packets, stats_n_bytes); + stats_sw_if_index = sw_if_index0; + stats_n_packets = 2; + stats_n_bytes = len0 + len1; + } + else + { + vlib_increment_combined_counter + (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX, + thread_index, sw_if_index0, 1, len0); + vlib_increment_combined_counter + (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX, + thread_index, sw_if_index1, 1, len1); + } + } + + if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED)) + { + geneve_encap_trace_t *tr = + vlib_add_trace (vm, node, b[0], sizeof (*tr)); + tr->tunnel_index = t0 - vxm->tunnels; + tr->vni = t0->vni; + } + + if (PREDICT_FALSE (b[1]->flags & VLIB_BUFFER_IS_TRACED)) + { + geneve_encap_trace_t *tr = + vlib_add_trace (vm, node, b[1], sizeof (*tr)); + tr->tunnel_index = t1 - vxm->tunnels; + tr->vni = t1->vni; + } + b += 2; + + vlib_validate_buffer_enqueue_x2 (vm, node, next_index, + to_next, n_left_to_next, + bi0, bi1, next0, next1); + } + + while (n_left_from > 0 && n_left_to_next > 0) + { + u32 bi0; + u32 flow_hash0; + u32 len0; + ip4_header_t *ip4_0; + ip6_header_t *ip6_0; + udp_header_t *udp0; + u64 *copy_src0, *copy_dst0; + u32 *copy_src_last0, *copy_dst_last0; + u16 new_l0; + ip_csum_t sum0; + + bi0 = from[0]; + to_next[0] = bi0; + from += 1; + to_next += 1; + n_left_from -= 1; + n_left_to_next -= 1; + + flow_hash0 = vnet_l2_compute_flow_hash (b[0]); + + /* Get next node index and adj index from tunnel next_dpo */ + if (sw_if_index0 != vnet_buffer (b[0])->sw_if_index[VLIB_TX]) + { + sw_if_index0 = vnet_buffer (b[0])->sw_if_index[VLIB_TX]; + hi0 = vnet_get_sup_hw_interface (vnm, sw_if_index0); + t0 = &vxm->tunnels[hi0->dev_instance]; + /* Note: change to always set next0 if it may be set to drop */ + next0 = t0->next_dpo.dpoi_next_node; + } + + ALWAYS_ASSERT (t0 != NULL); + + vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = t0->next_dpo.dpoi_index; + + /* Apply the rewrite string. $$$$ vnet_rewrite? */ + vlib_buffer_advance (b[0], -(word) _vec_len (t0->rewrite)); + + if (is_ip4) + { + u8 ip4_geneve_base_header_len = + sizeof (ip4_header_t) + sizeof (udp_header_t) + + GENEVE_BASE_HEADER_LENGTH; + u8 ip4_geneve_header_total_len0 = ip4_geneve_base_header_len; +#if SUPPORT_OPTIONS_HEADER==1 + ip4_geneve_header_total_len0 += t0->options_len; +#endif + ASSERT (vec_len (t0->rewrite) == ip4_geneve_header_total_len0); + + ip4_0 = vlib_buffer_get_current (b[0]); + + /* Copy the fixed header */ + copy_dst0 = (u64 *) ip4_0; + copy_src0 = (u64 *) t0->rewrite; + /* Copy first 32 octets 8-bytes at a time */ +#define _(offs) copy_dst0[offs] = copy_src0[offs]; + foreach_fixed_header4_offset; +#undef _ + /* Last 4 octets. Hopefully gcc will be our friend */ + copy_dst_last0 = (u32 *) (©_dst0[4]); + copy_src_last0 = (u32 *) (©_src0[4]); + copy_dst_last0[0] = copy_src_last0[0]; + + /* Fix the IP4 checksum and length */ + sum0 = ip4_0->checksum; + new_l0 = /* old_l0 always 0, see the rewrite setup */ + clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b[0])); + sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t, + length /* changed member */ ); + ip4_0->checksum = ip_csum_fold (sum0); + ip4_0->length = new_l0; + + /* Fix UDP length and set source port */ + udp0 = (udp_header_t *) (ip4_0 + 1); + new_l0 = + clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b[0]) - + sizeof (*ip4_0)); + udp0->length = new_l0; + udp0->src_port = flow_hash0; + } + + else /* ip6 path */ + { + int bogus = 0; + + u8 ip6_geneve_base_header_len = + sizeof (ip6_header_t) + sizeof (udp_header_t) + + GENEVE_BASE_HEADER_LENGTH; + u8 ip6_geneve_header_total_len0 = ip6_geneve_base_header_len; +#if SUPPORT_OPTIONS_HEADER==1 + ip6_geneve_header_total_len0 += t0->options_len; +#endif + ASSERT (vec_len (t0->rewrite) == ip6_geneve_header_total_len0); + + ip6_0 = vlib_buffer_get_current (b[0]); + /* Copy the fixed header */ + copy_dst0 = (u64 *) ip6_0; + copy_src0 = (u64 *) t0->rewrite; + /* Copy first 56 (ip6) octets 8-bytes at a time */ +#define _(offs) copy_dst0[offs] = copy_src0[offs]; + foreach_fixed_header6_offset; +#undef _ + /* Fix IP6 payload length */ + new_l0 = + clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b[0]) + - sizeof (*ip6_0)); + ip6_0->payload_length = new_l0; + + /* Fix UDP length and set source port */ + udp0 = (udp_header_t *) (ip6_0 + 1); + udp0->length = new_l0; + udp0->src_port = flow_hash0; + + /* IPv6 UDP checksum is mandatory */ + udp0->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b[0], + ip6_0, + &bogus); + ASSERT (bogus == 0); + if (udp0->checksum == 0) + udp0->checksum = 0xffff; + } + + pkts_encapsulated++; + len0 = vlib_buffer_length_in_chain (vm, b[0]); + stats_n_packets += 1; + stats_n_bytes += len0; + + /* save inner packet flow_hash for load-balance node */ + vnet_buffer (b[0])->ip.flow_hash = flow_hash0; + + /* Batch stats increment on the same geneve tunnel so counter is not + incremented per packet. Note stats are still incremented for deleted + and admin-down tunnel where packets are dropped. It is not worthwhile + to check for this rare case and affect normal path performance. */ + if (PREDICT_FALSE (sw_if_index0 != stats_sw_if_index)) + { + stats_n_packets -= 1; + stats_n_bytes -= len0; + if (stats_n_packets) + vlib_increment_combined_counter + (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX, + thread_index, stats_sw_if_index, + stats_n_packets, stats_n_bytes); + stats_n_packets = 1; + stats_n_bytes = len0; + stats_sw_if_index = sw_if_index0; + } + + if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED)) + { + geneve_encap_trace_t *tr = + vlib_add_trace (vm, node, b[0], sizeof (*tr)); + tr->tunnel_index = t0 - vxm->tunnels; + tr->vni = t0->vni; + } + b += 1; + + vlib_validate_buffer_enqueue_x1 (vm, node, next_index, + to_next, n_left_to_next, + bi0, next0); + } + + vlib_put_next_frame (vm, node, next_index, n_left_to_next); + } + + /* Do we still need this now that tunnel tx stats is kept? */ + vlib_node_increment_counter (vm, node->node_index, + GENEVE_ENCAP_ERROR_ENCAPSULATED, + pkts_encapsulated); + + /* Increment any remaining batch stats */ + if (stats_n_packets) + { + vlib_increment_combined_counter + (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX, + thread_index, stats_sw_if_index, stats_n_packets, stats_n_bytes); + node->runtime_data[0] = stats_sw_if_index; + } + + return from_frame->n_vectors; +} + +VLIB_NODE_FN (geneve4_encap_node) (vlib_main_t * vm, + vlib_node_runtime_t * node, + vlib_frame_t * from_frame) +{ + return geneve_encap_inline (vm, node, from_frame, /* is_ip4 */ 1); +} + +VLIB_NODE_FN (geneve6_encap_node) (vlib_main_t * vm, + vlib_node_runtime_t * node, + vlib_frame_t * from_frame) +{ + return geneve_encap_inline (vm, node, from_frame, /* is_ip4 */ 0); +} + +/* *INDENT-OFF* */ +VLIB_REGISTER_NODE (geneve4_encap_node) = { + .name = "geneve4-encap", + .vector_size = sizeof (u32), + .format_trace = format_geneve_encap_trace, + .type = VLIB_NODE_TYPE_INTERNAL, + .n_errors = ARRAY_LEN (geneve_encap_error_strings), + .error_strings = geneve_encap_error_strings, + .n_next_nodes = GENEVE_ENCAP_N_NEXT, + .next_nodes = { + [GENEVE_ENCAP_NEXT_DROP] = "error-drop", + }, +}; + +VLIB_REGISTER_NODE (geneve6_encap_node) = { + .name = "geneve6-encap", + .vector_size = sizeof (u32), + .format_trace = format_geneve_encap_trace, + .type = VLIB_NODE_TYPE_INTERNAL, + .n_errors = ARRAY_LEN (geneve_encap_error_strings), + .error_strings = geneve_encap_error_strings, + .n_next_nodes = GENEVE_ENCAP_N_NEXT, + .next_nodes = { + [GENEVE_ENCAP_NEXT_DROP] = "error-drop", + }, +}; +/* *INDENT-ON* */ + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/plugins/geneve/geneve.api b/src/plugins/geneve/geneve.api new file mode 100644 index 00000000000..fd871733fb9 --- /dev/null +++ b/src/plugins/geneve/geneve.api @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2017 SUSE LLC. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +option version = "2.1.0"; + +import "vnet/interface_types.api"; +import "vnet/ethernet/ethernet_types.api"; +import "vnet/ip/ip_types.api"; + +define geneve_add_del_tunnel +{ + option deprecated; + u32 client_index; + u32 context; + bool is_add; + vl_api_address_t local_address; + vl_api_address_t remote_address; + vl_api_interface_index_t mcast_sw_if_index; + u32 encap_vrf_id; + u32 decap_next_index; + u32 vni; + option vat_help="src { dst | group { | mcast_sw_if_index } } vni [encap-vrf-id ] [decap-next ] [del]"; +}; + +define geneve_add_del_tunnel_reply +{ + u32 context; + i32 retval; + vl_api_interface_index_t sw_if_index; +}; + +define geneve_add_del_tunnel2 +{ + u32 client_index; + u32 context; + bool is_add; + vl_api_address_t local_address; + vl_api_address_t remote_address; + vl_api_interface_index_t mcast_sw_if_index; + u32 encap_vrf_id; + u32 decap_next_index; + u32 vni; + bool l3_mode; +}; + +define geneve_add_del_tunnel2_reply +{ + u32 context; + i32 retval; + vl_api_interface_index_t sw_if_index; +}; + +define geneve_tunnel_dump +{ + u32 client_index; + u32 context; + vl_api_interface_index_t sw_if_index; + option vat_help="[ | sw_if_index ]"; +}; + +define geneve_tunnel_details +{ + u32 context; + vl_api_interface_index_t sw_if_index; + vl_api_address_t src_address; + vl_api_address_t dst_address; + vl_api_interface_index_t mcast_sw_if_index; + u32 encap_vrf_id; + u32 decap_next_index; + u32 vni; +}; + +/** \brief Interface set geneve-bypass request + @param client_index - opaque cookie to identify the sender + @param context - sender context, to match reply w/ request + @param sw_if_index - interface used to reach neighbor + @param is_ipv6 - if non-zero, enable ipv6-geneve-bypass, else ipv4-geneve-bypass + @param enable - if non-zero enable, else disable +*/ +autoreply define sw_interface_set_geneve_bypass +{ + u32 client_index; + u32 context; + vl_api_interface_index_t sw_if_index; + bool is_ipv6; + bool enable; + option vat_help=" | sw_if_index [ip4 | ip6] [enable | disable]"; +}; diff --git a/src/plugins/geneve/geneve.c b/src/plugins/geneve/geneve.c new file mode 100644 index 00000000000..f991af97571 --- /dev/null +++ b/src/plugins/geneve/geneve.c @@ -0,0 +1,1122 @@ +/* + * Copyright (c) 2017 SUSE LLC. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/** + * @file + * @brief GENEVE. + * + * GENEVE provides the features needed to allow L2 bridge domains (BDs) + * to span multiple servers. This is done by building an L2 overlay on + * top of an L3 network underlay using GENEVE tunnels. + * + * This makes it possible for servers to be co-located in the same data + * center or be separated geographically as long as they are reachable + * through the underlay L3 network. + */ + + +geneve_main_t geneve_main; + +u8 * +format_geneve_encap_trace (u8 * s, va_list * args) +{ + CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); + CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); + geneve_encap_trace_t *t = va_arg (*args, geneve_encap_trace_t *); + + s = format (s, "GENEVE encap to geneve_tunnel%d vni %d", + t->tunnel_index, t->vni); + return s; +} + +static u8 * +format_decap_next (u8 * s, va_list * args) +{ + u32 next_index = va_arg (*args, u32); + + switch (next_index) + { + case GENEVE_INPUT_NEXT_DROP: + return format (s, "drop"); + case GENEVE_INPUT_NEXT_L2_INPUT: + return format (s, "l2"); + default: + return format (s, "index %d", next_index); + } + return s; +} + +u8 * +format_geneve_tunnel (u8 * s, va_list * args) +{ + geneve_tunnel_t *t = va_arg (*args, geneve_tunnel_t *); + geneve_main_t *ngm = &geneve_main; + + s = format (s, "[%d] lcl %U rmt %U vni %d fib-idx %d sw-if-idx %d ", + t - ngm->tunnels, + format_ip46_address, &t->local, IP46_TYPE_ANY, + format_ip46_address, &t->remote, IP46_TYPE_ANY, + t->vni, t->encap_fib_index, t->sw_if_index); + + s = format (s, "encap-dpo-idx %d ", t->next_dpo.dpoi_index); + s = format (s, "decap-next-%U ", format_decap_next, t->decap_next_index); + s = format (s, "l3-mode %u ", t->l3_mode); + + if (PREDICT_FALSE (ip46_address_is_multicast (&t->remote))) + s = format (s, "mcast-sw-if-idx %d ", t->mcast_sw_if_index); + + return s; +} + +static u8 * +format_geneve_name (u8 * s, va_list * args) +{ + u32 dev_instance = va_arg (*args, u32); + return format (s, "geneve_tunnel%d", dev_instance); +} + +static clib_error_t * +geneve_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags) +{ + u32 hw_flags = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? + VNET_HW_INTERFACE_FLAG_LINK_UP : 0; + vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags); + + return /* no error */ 0; +} + +static clib_error_t * +geneve_mac_change (vnet_hw_interface_t * hi, + const u8 * old_address, const u8 * mac_address) +{ + l2input_interface_mac_change (hi->sw_if_index, old_address, mac_address); + return (NULL); +} + +/* *INDENT-OFF* */ +VNET_DEVICE_CLASS (geneve_device_class, static) = { + .name = "GENEVE", + .format_device_name = format_geneve_name, + .format_tx_trace = format_geneve_encap_trace, + .admin_up_down_function = geneve_interface_admin_up_down, + .mac_addr_change_function = geneve_mac_change, +}; +/* *INDENT-ON* */ + +static u8 * +format_geneve_header_with_length (u8 * s, va_list * args) +{ + u32 dev_instance = va_arg (*args, u32); + s = format (s, "unimplemented dev %u", dev_instance); + return s; +} + +/* *INDENT-OFF* */ +VNET_HW_INTERFACE_CLASS (geneve_hw_class) = { + .name = "GENEVE", + .format_header = format_geneve_header_with_length, + .build_rewrite = default_build_rewrite, +}; +/* *INDENT-ON* */ + +static void +geneve_tunnel_restack_dpo (geneve_tunnel_t * t) +{ + dpo_id_t dpo = DPO_INVALID; + u32 encap_index = ip46_address_is_ip4 (&t->remote) ? + geneve4_encap_node.index : geneve6_encap_node.index; + fib_forward_chain_type_t forw_type = ip46_address_is_ip4 (&t->remote) ? + FIB_FORW_CHAIN_TYPE_UNICAST_IP4 : FIB_FORW_CHAIN_TYPE_UNICAST_IP6; + + fib_entry_contribute_forwarding (t->fib_entry_index, forw_type, &dpo); + dpo_stack_from_node (encap_index, &t->next_dpo, &dpo); + dpo_reset (&dpo); +} + +static geneve_tunnel_t * +geneve_tunnel_from_fib_node (fib_node_t * node) +{ + ASSERT (FIB_NODE_TYPE_GENEVE_TUNNEL == node->fn_type); + return ((geneve_tunnel_t *) (((char *) node) - + STRUCT_OFFSET_OF (geneve_tunnel_t, node))); +} + +/** + * Function definition to backwalk a FIB node - + * Here we will restack the new dpo of GENEVE DIP to encap node. + */ +static fib_node_back_walk_rc_t +geneve_tunnel_back_walk (fib_node_t * node, fib_node_back_walk_ctx_t * ctx) +{ + geneve_tunnel_restack_dpo (geneve_tunnel_from_fib_node (node)); + return (FIB_NODE_BACK_WALK_CONTINUE); +} + +/** + * Function definition to get a FIB node from its index + */ +static fib_node_t * +geneve_tunnel_fib_node_get (fib_node_index_t index) +{ + geneve_tunnel_t *t; + geneve_main_t *vxm = &geneve_main; + + t = pool_elt_at_index (vxm->tunnels, index); + + return (&t->node); +} + +/** + * Function definition to inform the FIB node that its last lock has gone. + */ +static void +geneve_tunnel_last_lock_gone (fib_node_t * node) +{ + /* + * The GENEVE tunnel is a root of the graph. As such + * it never has children and thus is never locked. + */ + ASSERT (0); +} + +/* + * Virtual function table registered by GENEVE tunnels + * for participation in the FIB object graph. + */ +const static fib_node_vft_t geneve_vft = { + .fnv_get = geneve_tunnel_fib_node_get, + .fnv_last_lock = geneve_tunnel_last_lock_gone, + .fnv_back_walk = geneve_tunnel_back_walk, +}; + + +#define foreach_copy_field \ +_(vni) \ +_(mcast_sw_if_index) \ +_(encap_fib_index) \ +_(decap_next_index) \ +_(local) \ +_(remote) \ +_(l3_mode) + +static int +geneve_rewrite (geneve_tunnel_t * t, bool is_ip6) +{ + union + { + ip4_geneve_header_t *h4; + ip6_geneve_header_t *h6; + u8 *rw; + } r = + { + .rw = 0}; + int len = is_ip6 ? sizeof *r.h6 : sizeof *r.h4; +#if SUPPORT_OPTIONS_HEADER==1 + len += t->options_len; +#endif + + vec_validate_aligned (r.rw, len - 1, CLIB_CACHE_LINE_BYTES); + + udp_header_t *udp; + geneve_header_t *geneve; + /* Fixed portion of the (outer) ip header */ + if (!is_ip6) + { + ip4_header_t *ip = &r.h4->ip4; + udp = &r.h4->udp, geneve = &r.h4->geneve; + ip->ip_version_and_header_length = 0x45; + ip->ttl = 254; + ip->protocol = IP_PROTOCOL_UDP; + + ip->src_address = t->local.ip4; + ip->dst_address = t->remote.ip4; + + /* we fix up the ip4 header length and checksum after-the-fact */ + ip->checksum = ip4_header_checksum (ip); + } + else + { + ip6_header_t *ip = &r.h6->ip6; + udp = &r.h6->udp, geneve = &r.h6->geneve; + ip->ip_version_traffic_class_and_flow_label = + clib_host_to_net_u32 (6 << 28); + ip->hop_limit = 255; + ip->protocol = IP_PROTOCOL_UDP; + + ip->src_address = t->local.ip6; + ip->dst_address = t->remote.ip6; + } + + /* UDP header, randomize local port on something, maybe? */ + udp->src_port = clib_host_to_net_u16 (5251); + udp->dst_port = clib_host_to_net_u16 (UDP_DST_PORT_geneve); + + /* GENEVE header */ + vnet_set_geneve_version (geneve, GENEVE_VERSION); +#if SUPPORT_OPTIONS_HEADER==1 + vnet_set_geneve_options_len (geneve, t->options_len); +#else + vnet_set_geneve_options_len (geneve, 0); +#endif + vnet_set_geneve_oamframe_bit (geneve, 0); + vnet_set_geneve_critical_bit (geneve, 0); + vnet_set_geneve_protocol (geneve, GENEVE_ETH_PROTOCOL); + + vnet_geneve_hdr_1word_hton (geneve); + + vnet_set_geneve_vni (geneve, t->vni); + + t->rewrite = r.rw; + return (0); +} + +static bool +geneve_decap_next_is_valid (geneve_main_t * vxm, u32 is_ip6, + u32 decap_next_index) +{ + vlib_main_t *vm = vxm->vlib_main; + u32 input_idx = + (!is_ip6) ? geneve4_input_node.index : geneve6_input_node.index; + vlib_node_runtime_t *r = vlib_node_get_runtime (vm, input_idx); + + return decap_next_index < r->n_next_nodes; +} + +typedef union +{ + struct + { + fib_node_index_t mfib_entry_index; + adj_index_t mcast_adj_index; + }; + u64 as_u64; +} __clib_packed mcast_shared_t; + +static inline mcast_shared_t +mcast_shared_get (ip46_address_t * ip) +{ + ASSERT (ip46_address_is_multicast (ip)); + uword *p = hash_get_mem (geneve_main.mcast_shared, ip); + ALWAYS_ASSERT (p); + return (mcast_shared_t) + { + .as_u64 = *p}; +} + +static inline void +mcast_shared_add (ip46_address_t * remote, + fib_node_index_t mfei, adj_index_t ai) +{ + mcast_shared_t new_ep = { + .mcast_adj_index = ai, + .mfib_entry_index = mfei, + }; + + hash_set_mem_alloc (&geneve_main.mcast_shared, remote, new_ep.as_u64); +} + +static inline void +mcast_shared_remove (ip46_address_t * remote) +{ + mcast_shared_t ep = mcast_shared_get (remote); + + adj_unlock (ep.mcast_adj_index); + mfib_table_entry_delete_index (ep.mfib_entry_index, MFIB_SOURCE_GENEVE); + + hash_unset_mem_free (&geneve_main.mcast_shared, remote); +} + +int vnet_geneve_add_del_tunnel + (vnet_geneve_add_del_tunnel_args_t * a, u32 * sw_if_indexp) +{ + geneve_main_t *vxm = &geneve_main; + geneve_tunnel_t *t = 0; + vnet_main_t *vnm = vxm->vnet_main; + uword *p; + u32 hw_if_index = ~0; + u32 sw_if_index = ~0; + int rv; + geneve4_tunnel_key_t key4; + geneve6_tunnel_key_t key6; + u32 is_ip6 = a->is_ip6; + + if (!is_ip6) + { + key4.remote = a->remote.ip4.as_u32; + key4.vni = clib_host_to_net_u32 (a->vni << GENEVE_VNI_SHIFT); + p = hash_get (vxm->geneve4_tunnel_by_key, key4.as_u64); + } + else + { + key6.remote = a->remote.ip6; + key6.vni = clib_host_to_net_u32 (a->vni << GENEVE_VNI_SHIFT); + p = hash_get_mem (vxm->geneve6_tunnel_by_key, &key6); + } + + if (a->is_add) + { + l2input_main_t *l2im = &l2input_main; + + /* adding a tunnel: tunnel must not already exist */ + if (p) + return VNET_API_ERROR_TUNNEL_EXIST; + + /*if not set explicitly, default to l2 */ + if (a->decap_next_index == ~0) + a->decap_next_index = GENEVE_INPUT_NEXT_L2_INPUT; + if (!geneve_decap_next_is_valid (vxm, is_ip6, a->decap_next_index)) + return VNET_API_ERROR_INVALID_DECAP_NEXT; + + pool_get_aligned (vxm->tunnels, t, CLIB_CACHE_LINE_BYTES); + clib_memset (t, 0, sizeof (*t)); + + /* copy from arg structure */ +#define _(x) t->x = a->x; + foreach_copy_field; +#undef _ + + rv = geneve_rewrite (t, is_ip6); + if (rv) + { + pool_put (vxm->tunnels, t); + return rv; + } + + /* copy the key */ + if (is_ip6) + hash_set_mem_alloc (&vxm->geneve6_tunnel_by_key, &key6, + t - vxm->tunnels); + else + hash_set (vxm->geneve4_tunnel_by_key, key4.as_u64, t - vxm->tunnels); + + vnet_hw_interface_t *hi; + if (a->l3_mode) + { + u32 t_idx = t - vxm->tunnels; + u8 address[6] = + { 0xd0, 0x0b, 0xee, 0xd0, (u8) (t_idx >> 8), (u8) t_idx }; + clib_error_t *error = + ethernet_register_interface (vnm, geneve_device_class.index, + t_idx, + address, &hw_if_index, 0); + if (error) + { + clib_error_report (error); + return VNET_API_ERROR_INVALID_REGISTRATION; + } + } + else + { + hw_if_index = vnet_register_interface + (vnm, geneve_device_class.index, t - vxm->tunnels, + geneve_hw_class.index, t - vxm->tunnels); + } + + hi = vnet_get_hw_interface (vnm, hw_if_index); + + /* Set geneve tunnel output node */ + u32 encap_index = !is_ip6 ? + geneve4_encap_node.index : geneve6_encap_node.index; + vnet_set_interface_output_node (vnm, hw_if_index, encap_index); + + t->hw_if_index = hw_if_index; + t->sw_if_index = sw_if_index = hi->sw_if_index; + + vec_validate_init_empty (vxm->tunnel_index_by_sw_if_index, sw_if_index, + ~0); + vxm->tunnel_index_by_sw_if_index[sw_if_index] = t - vxm->tunnels; + + /* setup l2 input config with l2 feature and bd 0 to drop packet */ + vec_validate (l2im->configs, sw_if_index); + l2im->configs[sw_if_index].feature_bitmap = L2INPUT_FEAT_DROP; + l2im->configs[sw_if_index].bd_index = 0; + + vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, sw_if_index); + si->flags &= ~VNET_SW_INTERFACE_FLAG_HIDDEN; + vnet_sw_interface_set_flags (vnm, sw_if_index, + VNET_SW_INTERFACE_FLAG_ADMIN_UP); + + fib_node_init (&t->node, FIB_NODE_TYPE_GENEVE_TUNNEL); + fib_prefix_t tun_remote_pfx; + vnet_flood_class_t flood_class = VNET_FLOOD_CLASS_TUNNEL_NORMAL; + + fib_prefix_from_ip46_addr (&t->remote, &tun_remote_pfx); + if (!ip46_address_is_multicast (&t->remote)) + { + /* Unicast tunnel - + * source the FIB entry for the tunnel's destination + * and become a child thereof. The tunnel will then get poked + * when the forwarding for the entry updates, and the tunnel can + * re-stack accordingly + */ + vtep_addr_ref (&vxm->vtep_table, t->encap_fib_index, &t->local); + t->fib_entry_index = fib_entry_track (t->encap_fib_index, + &tun_remote_pfx, + FIB_NODE_TYPE_GENEVE_TUNNEL, + t - vxm->tunnels, + &t->sibling_index); + geneve_tunnel_restack_dpo (t); + } + else + { + /* Multicast tunnel - + * as the same mcast group can be used for mutiple mcast tunnels + * with different VNIs, create the output fib adjecency only if + * it does not already exist + */ + fib_protocol_t fp = fib_ip_proto (is_ip6); + + if (vtep_addr_ref (&vxm->vtep_table, + t->encap_fib_index, &t->remote) == 1) + { + fib_node_index_t mfei; + adj_index_t ai; + fib_route_path_t path = { + .frp_proto = fib_proto_to_dpo (fp), + .frp_addr = zero_addr, + .frp_sw_if_index = 0xffffffff, + .frp_fib_index = ~0, + .frp_weight = 1, + .frp_flags = FIB_ROUTE_PATH_LOCAL, + .frp_mitf_flags = MFIB_ITF_FLAG_FORWARD, + }; + const mfib_prefix_t mpfx = { + .fp_proto = fp, + .fp_len = (is_ip6 ? 128 : 32), + .fp_grp_addr = tun_remote_pfx.fp_addr, + }; + + /* + * Setup the (*,G) to receive traffic on the mcast group + * - the forwarding interface is for-us + * - the accepting interface is that from the API + */ + mfib_table_entry_path_update (t->encap_fib_index, + &mpfx, MFIB_SOURCE_GENEVE, &path); + + path.frp_sw_if_index = a->mcast_sw_if_index; + path.frp_flags = FIB_ROUTE_PATH_FLAG_NONE; + path.frp_mitf_flags = MFIB_ITF_FLAG_ACCEPT; + mfei = mfib_table_entry_path_update (t->encap_fib_index, + &mpfx, + MFIB_SOURCE_GENEVE, &path); + + /* + * Create the mcast adjacency to send traffic to the group + */ + ai = adj_mcast_add_or_lock (fp, + fib_proto_to_link (fp), + a->mcast_sw_if_index); + + /* + * create a new end-point + */ + mcast_shared_add (&t->remote, mfei, ai); + } + + dpo_id_t dpo = DPO_INVALID; + mcast_shared_t ep = mcast_shared_get (&t->remote); + + /* Stack shared mcast remote mac addr rewrite on encap */ + dpo_set (&dpo, DPO_ADJACENCY_MCAST, + fib_proto_to_dpo (fp), ep.mcast_adj_index); + + dpo_stack_from_node (encap_index, &t->next_dpo, &dpo); + dpo_reset (&dpo); + flood_class = VNET_FLOOD_CLASS_TUNNEL_MASTER; + } + + vnet_get_sw_interface (vnet_get_main (), sw_if_index)->flood_class = + flood_class; + } + else + { + /* deleting a tunnel: tunnel must exist */ + if (!p) + return VNET_API_ERROR_NO_SUCH_ENTRY; + + t = pool_elt_at_index (vxm->tunnels, p[0]); + + sw_if_index = t->sw_if_index; + vnet_sw_interface_set_flags (vnm, t->sw_if_index, 0 /* down */ ); + vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, t->sw_if_index); + si->flags |= VNET_SW_INTERFACE_FLAG_HIDDEN; + + /* make sure tunnel is removed from l2 bd or xconnect */ + set_int_l2_mode (vxm->vlib_main, vnm, MODE_L3, t->sw_if_index, 0, + L2_BD_PORT_TYPE_NORMAL, 0, 0); + + if (t->l3_mode) + ethernet_delete_interface (vnm, t->hw_if_index); + else + vnet_delete_hw_interface (vnm, t->hw_if_index); + + vxm->tunnel_index_by_sw_if_index[t->sw_if_index] = ~0; + + if (!is_ip6) + hash_unset (vxm->geneve4_tunnel_by_key, key4.as_u64); + else + hash_unset_mem_free (&vxm->geneve6_tunnel_by_key, &key6); + + if (!ip46_address_is_multicast (&t->remote)) + { + vtep_addr_unref (&vxm->vtep_table, t->encap_fib_index, &t->local); + fib_entry_untrack (t->fib_entry_index, t->sibling_index); + } + else if (vtep_addr_unref (&vxm->vtep_table, + t->encap_fib_index, &t->remote) == 0) + { + mcast_shared_remove (&t->remote); + } + + fib_node_deinit (&t->node); + vec_free (t->rewrite); + pool_put (vxm->tunnels, t); + } + + if (sw_if_indexp) + *sw_if_indexp = sw_if_index; + + if (a->is_add) + { + /* register udp ports */ + if (!is_ip6 && !udp_is_valid_dst_port (UDP_DST_PORT_geneve, 1)) + udp_register_dst_port (vxm->vlib_main, UDP_DST_PORT_geneve, + geneve4_input_node.index, 1); + if (is_ip6 && !udp_is_valid_dst_port (UDP_DST_PORT_geneve6, 0)) + udp_register_dst_port (vxm->vlib_main, UDP_DST_PORT_geneve6, + geneve6_input_node.index, 0); + } + + return 0; +} + +static uword +get_decap_next_for_node (u32 node_index, u32 ipv4_set) +{ + geneve_main_t *vxm = &geneve_main; + vlib_main_t *vm = vxm->vlib_main; + uword input_node = (ipv4_set) ? geneve4_input_node.index : + geneve6_input_node.index; + + return vlib_node_add_next (vm, input_node, node_index); +} + +static uword +unformat_decap_next (unformat_input_t * input, va_list * args) +{ + u32 *result = va_arg (*args, u32 *); + u32 ipv4_set = va_arg (*args, int); + geneve_main_t *vxm = &geneve_main; + vlib_main_t *vm = vxm->vlib_main; + u32 node_index; + u32 tmp; + + if (unformat (input, "l2")) + *result = GENEVE_INPUT_NEXT_L2_INPUT; + else if (unformat (input, "node %U", unformat_vlib_node, vm, &node_index)) + *result = get_decap_next_for_node (node_index, ipv4_set); + else if (unformat (input, "%d", &tmp)) + *result = tmp; + else + return 0; + return 1; +} + +static clib_error_t * +geneve_add_del_tunnel_command_fn (vlib_main_t * vm, + unformat_input_t * input, + vlib_cli_command_t * cmd) +{ + unformat_input_t _line_input, *line_input = &_line_input; + ip46_address_t local, remote; + u8 is_add = 1; + u8 local_set = 0; + u8 remote_set = 0; + u8 grp_set = 0; + u8 ipv4_set = 0; + u8 ipv6_set = 0; + u8 l3_mode = 0; + u32 encap_fib_index = 0; + u32 mcast_sw_if_index = ~0; + u32 decap_next_index = GENEVE_INPUT_NEXT_L2_INPUT; + u32 vni = 0; + u32 tmp; + int rv; + vnet_geneve_add_del_tunnel_args_t _a, *a = &_a; + u32 tunnel_sw_if_index; + clib_error_t *error = NULL; + + /* Cant "universally zero init" (={0}) due to GCC bug 53119 */ + clib_memset (&local, 0, sizeof local); + clib_memset (&remote, 0, sizeof remote); + + /* Get a line of input. */ + if (!unformat_user (input, unformat_line_input, line_input)) + return 0; + + while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) + { + if (unformat (line_input, "del")) + { + is_add = 0; + } + else if (unformat (line_input, "local %U", + unformat_ip4_address, &local.ip4)) + { + local_set = 1; + ipv4_set = 1; + } + else if (unformat (line_input, "remote %U", + unformat_ip4_address, &remote.ip4)) + { + remote_set = 1; + ipv4_set = 1; + } + else if (unformat (line_input, "local %U", + unformat_ip6_address, &local.ip6)) + { + local_set = 1; + ipv6_set = 1; + } + else if (unformat (line_input, "remote %U", + unformat_ip6_address, &remote.ip6)) + { + remote_set = 1; + ipv6_set = 1; + } + else if (unformat (line_input, "group %U %U", + unformat_ip4_address, &remote.ip4, + unformat_vnet_sw_interface, + vnet_get_main (), &mcast_sw_if_index)) + { + grp_set = remote_set = 1; + ipv4_set = 1; + } + else if (unformat (line_input, "group %U %U", + unformat_ip6_address, &remote.ip6, + unformat_vnet_sw_interface, + vnet_get_main (), &mcast_sw_if_index)) + { + grp_set = remote_set = 1; + ipv6_set = 1; + } + else if (unformat (line_input, "encap-vrf-id %d", &tmp)) + { + encap_fib_index = fib_table_find (fib_ip_proto (ipv6_set), tmp); + if (encap_fib_index == ~0) + { + error = + clib_error_return (0, "nonexistent encap-vrf-id %d", tmp); + goto done; + } + } + else if (unformat (line_input, "decap-next %U", unformat_decap_next, + &decap_next_index, ipv4_set)) + ; + else if (unformat (line_input, "vni %d", &vni)) + { + if (vni >> 24) + { + error = clib_error_return (0, "vni %d out of range", vni); + goto done; + } + } + else if (unformat (line_input, "l3-mode")) + { + l3_mode = 1; + } + else + { + error = clib_error_return (0, "parse error: '%U'", + format_unformat_error, line_input); + goto done; + } + } + + if (local_set == 0) + { + error = clib_error_return (0, "tunnel local address not specified"); + goto done; + } + + if (remote_set == 0) + { + error = clib_error_return (0, "tunnel remote address not specified"); + goto done; + } + + if (grp_set && !ip46_address_is_multicast (&remote)) + { + error = clib_error_return (0, "tunnel group address not multicast"); + goto done; + } + + if (grp_set == 0 && ip46_address_is_multicast (&remote)) + { + error = clib_error_return (0, "remote address must be unicast"); + goto done; + } + + if (grp_set && mcast_sw_if_index == ~0) + { + error = clib_error_return (0, "tunnel nonexistent multicast device"); + goto done; + } + + if (ipv4_set && ipv6_set) + { + error = clib_error_return (0, "both IPv4 and IPv6 addresses specified"); + goto done; + } + + if (ip46_address_cmp (&local, &remote) == 0) + { + error = + clib_error_return (0, "local and remote addresses are identical"); + goto done; + } + + if (decap_next_index == ~0) + { + error = clib_error_return (0, "next node not found"); + goto done; + } + + if (vni == 0) + { + error = clib_error_return (0, "vni not specified"); + goto done; + } + + clib_memset (a, 0, sizeof (*a)); + + a->is_add = is_add; + a->is_ip6 = ipv6_set; + +#define _(x) a->x = x; + foreach_copy_field; +#undef _ + + rv = vnet_geneve_add_del_tunnel (a, &tunnel_sw_if_index); + + switch (rv) + { + case 0: + if (is_add) + vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, + vnet_get_main (), tunnel_sw_if_index); + break; + + case VNET_API_ERROR_TUNNEL_EXIST: + error = clib_error_return (0, "tunnel already exists..."); + goto done; + + case VNET_API_ERROR_NO_SUCH_ENTRY: + error = clib_error_return (0, "tunnel does not exist..."); + goto done; + + default: + error = clib_error_return + (0, "vnet_geneve_add_del_tunnel returned %d", rv); + goto done; + } + +done: + unformat_free (line_input); + + return error; +} + +/*? + * Add or delete a GENEVE Tunnel. + * + * GENEVE provides the features needed to allow L2 bridge domains (BDs) + * to span multiple servers. This is done by building an L2 overlay on + * top of an L3 network underlay using GENEVE tunnels. + * + * This makes it possible for servers to be co-located in the same data + * center or be separated geographically as long as they are reachable + * through the underlay L3 network. + * + * You can refer to this kind of L2 overlay bridge domain as a GENEVE + * segment. + * + * @cliexpar + * Example of how to create a GENEVE Tunnel: + * @cliexcmd{create geneve tunnel local 10.0.3.1 remote 10.0.3.3 vni 13 encap-vrf-id 7} + * Example of how to delete a GENEVE Tunnel: + * @cliexcmd{create geneve tunnel local 10.0.3.1 remote 10.0.3.3 vni 13 del} + ?*/ +/* *INDENT-OFF* */ +VLIB_CLI_COMMAND (create_geneve_tunnel_command, static) = { + .path = "create geneve tunnel", + .short_help = + "create geneve tunnel local " + " {remote |group } vni " + " [encap-vrf-id ] [decap-next [l2|node ]] [l3-mode] [del]", + .function = geneve_add_del_tunnel_command_fn, +}; +/* *INDENT-ON* */ + +static clib_error_t * +show_geneve_tunnel_command_fn (vlib_main_t * vm, + unformat_input_t * input, + vlib_cli_command_t * cmd) +{ + geneve_main_t *vxm = &geneve_main; + geneve_tunnel_t *t; + + if (pool_elts (vxm->tunnels) == 0) + vlib_cli_output (vm, "No geneve tunnels configured..."); + + pool_foreach (t, vxm->tunnels, ( + { + vlib_cli_output (vm, "%U", + format_geneve_tunnel, t); + } + )); + + return 0; +} + +/*? + * Display all the GENEVE Tunnel entries. + * + * @cliexpar + * Example of how to display the GENEVE Tunnel entries: + * @cliexstart{show geneve tunnel} + * [0] local 10.0.3.1 remote 10.0.3.3 vni 13 encap_fib_index 0 sw_if_index 5 decap_next l2 + * @cliexend + ?*/ +/* *INDENT-OFF* */ +VLIB_CLI_COMMAND (show_geneve_tunnel_command, static) = { + .path = "show geneve tunnel", + .short_help = "show geneve tunnel", + .function = show_geneve_tunnel_command_fn, +}; +/* *INDENT-ON* */ + + +void +vnet_int_geneve_bypass_mode (u32 sw_if_index, u8 is_ip6, u8 is_enable) +{ + if (is_ip6) + vnet_feature_enable_disable ("ip6-unicast", "ip6-geneve-bypass", + sw_if_index, is_enable, 0, 0); + else + vnet_feature_enable_disable ("ip4-unicast", "ip4-geneve-bypass", + sw_if_index, is_enable, 0, 0); +} + + +static clib_error_t * +set_ip_geneve_bypass (u32 is_ip6, + unformat_input_t * input, vlib_cli_command_t * cmd) +{ + unformat_input_t _line_input, *line_input = &_line_input; + vnet_main_t *vnm = vnet_get_main (); + clib_error_t *error = 0; + u32 sw_if_index, is_enable; + + sw_if_index = ~0; + is_enable = 1; + + if (!unformat_user (input, unformat_line_input, line_input)) + return 0; + + while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) + { + if (unformat_user + (line_input, unformat_vnet_sw_interface, vnm, &sw_if_index)) + ; + else if (unformat (line_input, "del")) + is_enable = 0; + else + { + error = unformat_parse_error (line_input); + goto done; + } + } + + if (~0 == sw_if_index) + { + error = clib_error_return (0, "unknown interface `%U'", + format_unformat_error, line_input); + goto done; + } + + vnet_int_geneve_bypass_mode (sw_if_index, is_ip6, is_enable); + +done: + unformat_free (line_input); + + return error; +} + +static clib_error_t * +set_ip4_geneve_bypass (vlib_main_t * vm, + unformat_input_t * input, vlib_cli_command_t * cmd) +{ + return set_ip_geneve_bypass (0, input, cmd); +} + +/*? + * This command adds the 'ip4-geneve-bypass' graph node for a given interface. + * By adding the IPv4 geneve-bypass graph node to an interface, the node checks + * for and validate input geneve packet and bypass ip4-lookup, ip4-local, + * ip4-udp-lookup nodes to speedup geneve packet forwarding. This node will + * cause extra overhead to for non-geneve packets which is kept at a minimum. + * + * @cliexpar + * @parblock + * Example of graph node before ip4-geneve-bypass is enabled: + * @cliexstart{show vlib graph ip4-geneve-bypass} + * Name Next Previous + * ip4-geneve-bypass error-drop [0] + * geneve4-input [1] + * ip4-lookup [2] + * @cliexend + * + * Example of how to enable ip4-geneve-bypass on an interface: + * @cliexcmd{set interface ip geneve-bypass GigabitEthernet2/0/0} + * + * Example of graph node after ip4-geneve-bypass is enabled: + * @cliexstart{show vlib graph ip4-geneve-bypass} + * Name Next Previous + * ip4-geneve-bypass error-drop [0] ip4-input + * geneve4-input [1] ip4-input-no-checksum + * ip4-lookup [2] + * @cliexend + * + * Example of how to display the feature enabed on an interface: + * @cliexstart{show ip interface features GigabitEthernet2/0/0} + * IP feature paths configured on GigabitEthernet2/0/0... + * ... + * ipv4 unicast: + * ip4-geneve-bypass + * ip4-lookup + * ... + * @cliexend + * + * Example of how to disable ip4-geneve-bypass on an interface: + * @cliexcmd{set interface ip geneve-bypass GigabitEthernet2/0/0 del} + * @endparblock +?*/ +/* *INDENT-OFF* */ +VLIB_CLI_COMMAND (set_interface_ip_geneve_bypass_command, static) = { + .path = "set interface ip geneve-bypass", + .function = set_ip4_geneve_bypass, + .short_help = "set interface ip geneve-bypass [del]", +}; +/* *INDENT-ON* */ + +static clib_error_t * +set_ip6_geneve_bypass (vlib_main_t * vm, + unformat_input_t * input, vlib_cli_command_t * cmd) +{ + return set_ip_geneve_bypass (1, input, cmd); +} + +/*? + * This command adds the 'ip6-geneve-bypass' graph node for a given interface. + * By adding the IPv6 geneve-bypass graph node to an interface, the node checks + * for and validate input geneve packet and bypass ip6-lookup, ip6-local, + * ip6-udp-lookup nodes to speedup geneve packet forwarding. This node will + * cause extra overhead to for non-geneve packets which is kept at a minimum. + * + * @cliexpar + * @parblock + * Example of graph node before ip6-geneve-bypass is enabled: + * @cliexstart{show vlib graph ip6-geneve-bypass} + * Name Next Previous + * ip6-geneve-bypass error-drop [0] + * geneve6-input [1] + * ip6-lookup [2] + * @cliexend + * + * Example of how to enable ip6-geneve-bypass on an interface: + * @cliexcmd{set interface ip6 geneve-bypass GigabitEthernet2/0/0} + * + * Example of graph node after ip6-geneve-bypass is enabled: + * @cliexstart{show vlib graph ip6-geneve-bypass} + * Name Next Previous + * ip6-geneve-bypass error-drop [0] ip6-input + * geneve6-input [1] ip4-input-no-checksum + * ip6-lookup [2] + * @cliexend + * + * Example of how to display the feature enabed on an interface: + * @cliexstart{show ip interface features GigabitEthernet2/0/0} + * IP feature paths configured on GigabitEthernet2/0/0... + * ... + * ipv6 unicast: + * ip6-geneve-bypass + * ip6-lookup + * ... + * @cliexend + * + * Example of how to disable ip6-geneve-bypass on an interface: + * @cliexcmd{set interface ip6 geneve-bypass GigabitEthernet2/0/0 del} + * @endparblock +?*/ +/* *INDENT-OFF* */ +VLIB_CLI_COMMAND (set_interface_ip6_geneve_bypass_command, static) = { + .path = "set interface ip6 geneve-bypass", + .function = set_ip6_geneve_bypass, + .short_help = "set interface ip6 geneve-bypass [del]", +}; +/* *INDENT-ON* */ + +clib_error_t * +geneve_init (vlib_main_t * vm) +{ + geneve_main_t *vxm = &geneve_main; + + vxm->vnet_main = vnet_get_main (); + vxm->vlib_main = vm; + + /* initialize the ip6 hash */ + vxm->geneve6_tunnel_by_key = hash_create_mem (0, + sizeof (geneve6_tunnel_key_t), + sizeof (uword)); + vxm->vtep_table = vtep_table_create (); + vxm->mcast_shared = hash_create_mem (0, + sizeof (ip46_address_t), + sizeof (mcast_shared_t)); + + fib_node_register_type (FIB_NODE_TYPE_GENEVE_TUNNEL, &geneve_vft); + + return 0; +} + +VLIB_INIT_FUNCTION (geneve_init); + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/plugins/geneve/geneve.h b/src/plugins/geneve/geneve.h new file mode 100644 index 00000000000..029f7534497 --- /dev/null +++ b/src/plugins/geneve/geneve.h @@ -0,0 +1,226 @@ +/* + * Copyright (c) 2017 SUSE LLC. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef included_vnet_geneve_h +#define included_vnet_geneve_h + +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define SUPPORT_OPTIONS_HEADER 0 + +typedef CLIB_PACKED (struct + { + ip4_header_t ip4; /* 20 bytes */ + udp_header_t udp; /* 8 bytes */ + geneve_header_t geneve; /* Min 8 bytes, Max 260 bytes */ + }) ip4_geneve_header_t; + +typedef CLIB_PACKED (struct + { + ip6_header_t ip6; /* 40 bytes */ + udp_header_t udp; /* 8 bytes */ + geneve_header_t geneve; /* Min 8 bytes, Max 260 bytes */ + }) ip6_geneve_header_t; + +typedef CLIB_PACKED (struct + { + /* + * Key fields: ip source and geneve vni on incoming GENEVE packet + * all fields in NET byte order + */ + union + { + struct + { + u32 remote; + u32 vni; /* shifted left 8 bits */ + }; + u64 as_u64; + }; + }) geneve4_tunnel_key_t; + +typedef CLIB_PACKED (struct + { + /* + * Key fields: ip source and geneve vni on incoming GENEVE packet + * all fields in NET byte order + */ + ip6_address_t remote; + u32 vni; /* shifted left 8 bits */ + }) geneve6_tunnel_key_t; + +typedef struct +{ + u32 tunnel_index; + u32 vni; +} geneve_encap_trace_t; + +typedef struct +{ + /* Required for pool_get_aligned */ + CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); + + /* Rewrite string. $$$$ embed vnet_rewrite header */ + u8 *rewrite; + + /* FIB DPO for IP forwarding of GENEVE encap packet */ + dpo_id_t next_dpo; + + /* geneve VNI in HOST byte order */ + u32 vni; + + /* geneve OPTIONS LEN in HOST byte order */ +#if SUPPORT_OPTIONS_HEADER==1 + u8 options_len; +#endif + + /* tunnel local and remote addresses */ + ip46_address_t local; + ip46_address_t remote; + + /* mcast packet output intfc index (used only if remote is mcast) */ + u32 mcast_sw_if_index; + + /* decap next index */ + u32 decap_next_index; + + /* The FIB index for local/remote addresses */ + u32 encap_fib_index; + + /* vnet intfc index */ + u32 sw_if_index; + u32 hw_if_index; + + /** + * Linkage into the FIB object graph + */ + fib_node_t node; + + /* + * The FIB entry for (depending on GENEVE tunnel is unicast or mcast) + * sending unicast GENEVE encap packets or receiving mcast GENEVE packets + */ + fib_node_index_t fib_entry_index; + adj_index_t mcast_adj_index; + + /** + * The tunnel is a child of the FIB entry for its desintion. This is + * so it receives updates when the forwarding information for that entry + * changes. + * The tunnels sibling index on the FIB entry's dependency list. + */ + u32 sibling_index; + + u8 l3_mode; +} geneve_tunnel_t; + +#define foreach_geneve_input_next \ +_(DROP, "error-drop") \ +_(L2_INPUT, "l2-input") + +typedef enum +{ +#define _(s,n) GENEVE_INPUT_NEXT_##s, + foreach_geneve_input_next +#undef _ + GENEVE_INPUT_N_NEXT, +} geneve_input_next_t; + +typedef enum +{ +#define geneve_error(n,s) GENEVE_ERROR_##n, +#include +#undef geneve_error + GENEVE_N_ERROR, +} geneve_input_error_t; + +typedef struct +{ + /* vector of encap tunnel instances */ + geneve_tunnel_t *tunnels; + + /* lookup tunnel by key */ + uword *geneve4_tunnel_by_key; /* keyed on ipv4.remote + vni */ + uword *geneve6_tunnel_by_key; /* keyed on ipv6.remote + vni */ + + /* local VTEP IPs ref count used by geneve-bypass node to check if + received GENEVE packet DIP matches any local VTEP address */ + vtep_table_t vtep_table; + + /* mcast shared info */ + uword *mcast_shared; /* keyed on mcast ip46 addr */ + + /* Mapping from sw_if_index to tunnel index */ + u32 *tunnel_index_by_sw_if_index; + + /* convenience */ + vlib_main_t *vlib_main; + vnet_main_t *vnet_main; + + u16 msg_id_base; +} geneve_main_t; + +extern geneve_main_t geneve_main; + +extern vlib_node_registration_t geneve4_input_node; +extern vlib_node_registration_t geneve6_input_node; +extern vlib_node_registration_t geneve4_encap_node; +extern vlib_node_registration_t geneve6_encap_node; + +u8 *format_geneve_encap_trace (u8 * s, va_list * args); + +typedef struct +{ + u8 is_add; + + /* we normally use is_ip4, but since this adds to the + * structure, this seems less of abreaking change */ + u8 is_ip6; + ip46_address_t local, remote; + u32 mcast_sw_if_index; + u32 encap_fib_index; + u32 decap_next_index; + u32 vni; + u8 l3_mode; +} vnet_geneve_add_del_tunnel_args_t; + +int vnet_geneve_add_del_tunnel + (vnet_geneve_add_del_tunnel_args_t * a, u32 * sw_if_indexp); + +void vnet_int_geneve_bypass_mode (u32 sw_if_index, u8 is_ip6, u8 is_enable); +#endif /* included_vnet_geneve_h */ + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/plugins/geneve/geneve_api.c b/src/plugins/geneve/geneve_api.c new file mode 100644 index 00000000000..f6b9afda7d5 --- /dev/null +++ b/src/plugins/geneve/geneve_api.c @@ -0,0 +1,265 @@ +/* + * Copyright (c) 2017 SUSE LLC. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *------------------------------------------------------------------ + */ + +#include +#include + +#include +#include +#include +#include +#include + +#include + +/* define message IDs */ +#include +#include +#include + +/** + * Base message ID fot the plugin + */ +static u32 geneve_base_msg_id; +#define REPLY_MSG_ID_BASE geneve_base_msg_id + +#include + +static void + vl_api_sw_interface_set_geneve_bypass_t_handler + (vl_api_sw_interface_set_geneve_bypass_t * mp) +{ + vl_api_sw_interface_set_geneve_bypass_reply_t *rmp; + int rv = 0; + u32 sw_if_index = ntohl (mp->sw_if_index); + + VALIDATE_SW_IF_INDEX (mp); + + vnet_int_geneve_bypass_mode (sw_if_index, mp->is_ipv6, mp->enable); + BAD_SW_IF_INDEX_LABEL; + + REPLY_MACRO (VL_API_SW_INTERFACE_SET_GENEVE_BYPASS_REPLY); +} + +static void vl_api_geneve_add_del_tunnel_t_handler + (vl_api_geneve_add_del_tunnel_t * mp) +{ + vl_api_geneve_add_del_tunnel_reply_t *rmp; + int rv = 0; + ip4_main_t *im = &ip4_main; + + uword *p = hash_get (im->fib_index_by_table_id, ntohl (mp->encap_vrf_id)); + if (!p) + { + rv = VNET_API_ERROR_NO_SUCH_FIB; + goto out; + } + + vnet_geneve_add_del_tunnel_args_t a = { + .is_add = mp->is_add, + .is_ip6 = mp->remote_address.af, + .mcast_sw_if_index = ntohl (mp->mcast_sw_if_index), + .encap_fib_index = p[0], + .decap_next_index = ntohl (mp->decap_next_index), + .vni = ntohl (mp->vni), + }; + + ip_address_decode (&mp->remote_address, &a.remote); + ip_address_decode (&mp->local_address, &a.local); + + /* Check src & dst are different */ + if (ip46_address_cmp (&a.remote, &a.local) == 0) + { + rv = VNET_API_ERROR_SAME_SRC_DST; + goto out; + } + if (ip46_address_is_multicast (&a.remote) && + !vnet_sw_if_index_is_api_valid (a.mcast_sw_if_index)) + { + rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; + goto out; + } + + u32 sw_if_index = ~0; + rv = vnet_geneve_add_del_tunnel (&a, &sw_if_index); + +out: + /* *INDENT-OFF* */ + REPLY_MACRO2(VL_API_GENEVE_ADD_DEL_TUNNEL_REPLY, + ({ + rmp->sw_if_index = ntohl (sw_if_index); + })); + /* *INDENT-ON* */ +} + +static void vl_api_geneve_add_del_tunnel2_t_handler + (vl_api_geneve_add_del_tunnel2_t * mp) +{ + vl_api_geneve_add_del_tunnel2_reply_t *rmp; + int rv = 0; + ip4_main_t *im = &ip4_main; + + uword *p = hash_get (im->fib_index_by_table_id, ntohl (mp->encap_vrf_id)); + if (!p) + { + rv = VNET_API_ERROR_NO_SUCH_FIB; + goto out; + } + + vnet_geneve_add_del_tunnel_args_t a = { + .is_add = mp->is_add, + .is_ip6 = mp->remote_address.af, + .mcast_sw_if_index = ntohl (mp->mcast_sw_if_index), + .encap_fib_index = p[0], + .decap_next_index = ntohl (mp->decap_next_index), + .vni = ntohl (mp->vni), + .l3_mode = mp->l3_mode, + }; + + ip_address_decode (&mp->remote_address, &a.remote); + ip_address_decode (&mp->local_address, &a.local); + + /* Check src & dst are different */ + if (ip46_address_cmp (&a.remote, &a.local) == 0) + { + rv = VNET_API_ERROR_SAME_SRC_DST; + goto out; + } + if (ip46_address_is_multicast (&a.remote) && + !vnet_sw_if_index_is_api_valid (a.mcast_sw_if_index)) + { + rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; + goto out; + } + + u32 sw_if_index = ~0; + rv = vnet_geneve_add_del_tunnel (&a, &sw_if_index); + +out: + /* *INDENT-OFF* */ + REPLY_MACRO2(VL_API_GENEVE_ADD_DEL_TUNNEL2_REPLY, + ({ + rmp->sw_if_index = ntohl (sw_if_index); + })); + /* *INDENT-ON* */ +} + +static void send_geneve_tunnel_details + (geneve_tunnel_t * t, vl_api_registration_t * reg, u32 context) +{ + vl_api_geneve_tunnel_details_t *rmp; + ip4_main_t *im4 = &ip4_main; + ip6_main_t *im6 = &ip6_main; + u8 is_ipv6 = !ip46_address_is_ip4 (&t->remote); + + rmp = vl_msg_api_alloc (sizeof (*rmp)); + clib_memset (rmp, 0, sizeof (*rmp)); + rmp->_vl_msg_id = ntohs (VL_API_GENEVE_TUNNEL_DETAILS + REPLY_MSG_ID_BASE); + ip_address_encode (&t->local, is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4, + &rmp->src_address); + ip_address_encode (&t->remote, is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4, + &rmp->dst_address); + rmp->encap_vrf_id = + htonl (is_ipv6 ? im6->fibs[t->encap_fib_index]. + ft_table_id : im4->fibs[t->encap_fib_index].ft_table_id); + + rmp->mcast_sw_if_index = htonl (t->mcast_sw_if_index); + rmp->vni = htonl (t->vni); + rmp->decap_next_index = htonl (t->decap_next_index); + rmp->sw_if_index = htonl (t->sw_if_index); + rmp->context = context; + + vl_api_send_msg (reg, (u8 *) rmp); +} + +static void vl_api_geneve_tunnel_dump_t_handler + (vl_api_geneve_tunnel_dump_t * mp) +{ + vl_api_registration_t *reg; + geneve_main_t *vxm = &geneve_main; + geneve_tunnel_t *t; + u32 sw_if_index; + + reg = vl_api_client_index_to_registration (mp->client_index); + if (!reg) + return; + + sw_if_index = ntohl (mp->sw_if_index); + + if (~0 == sw_if_index) + { + /* *INDENT-OFF* */ + pool_foreach (t, vxm->tunnels, + ({ + send_geneve_tunnel_details(t, reg, mp->context); + })); + /* *INDENT-ON* */ + } + else + { + if ((sw_if_index >= vec_len (vxm->tunnel_index_by_sw_if_index)) || + (~0 == vxm->tunnel_index_by_sw_if_index[sw_if_index])) + { + return; + } + t = &vxm->tunnels[vxm->tunnel_index_by_sw_if_index[sw_if_index]]; + send_geneve_tunnel_details (t, reg, mp->context); + } +} + +/* + * geneve_api_hookup + * Add geneve's API message handlers to the table. + */ +/* API definitions */ +#include +#include + +static clib_error_t * +geneve_api_hookup (vlib_main_t * vm) +{ + api_main_t *am = vlibapi_get_main (); + + /* + * Set up the (msg_name, crc, message-id) table + */ + geneve_base_msg_id = setup_message_id_table (); + + am->api_trace_cfg[VL_API_GENEVE_ADD_DEL_TUNNEL + REPLY_MSG_ID_BASE].size += + 16 * sizeof (u32); + + return 0; +} + +VLIB_API_INIT_FUNCTION (geneve_api_hookup); + +#include +#include + +/* *INDENT-OFF* */ +VLIB_PLUGIN_REGISTER () = { + .version = VPP_BUILD_VER, + .description = "GENEVE Tunnels", +}; +/* *INDENT-ON* */ + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/plugins/geneve/geneve_error.def b/src/plugins/geneve/geneve_error.def new file mode 100644 index 00000000000..73abc8ddbf1 --- /dev/null +++ b/src/plugins/geneve/geneve_error.def @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2017 SUSE LLC. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +geneve_error (DECAPSULATED, "good packets decapsulated") +geneve_error (NO_SUCH_TUNNEL, "no such tunnel packets") +geneve_error (BAD_FLAGS, "packets with bad flags field in geneve header") diff --git a/src/plugins/geneve/geneve_packet.h b/src/plugins/geneve/geneve_packet.h new file mode 100644 index 00000000000..a0d0687434c --- /dev/null +++ b/src/plugins/geneve/geneve_packet.h @@ -0,0 +1,244 @@ +/* + * Copyright (c) 2017 SUSE LLC. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef included_vnet_geneve_packet_h +#define included_vnet_geneve_packet_h + +/* + * + * As per draft https://tools.ietf.org/html/draft-ietf-nvo3-geneve-05 + * + * Section 3.5 + * + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * | Option Class | Type |R|R|R| Length | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * | Variable Option Data | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + */ +#define GENEVE_MAX_OPT_LENGTH 128 + +/* + * + * As per draft https://tools.ietf.org/html/draft-ietf-nvo3-geneve-05 + * + * Section 7 + * + * +----------------+--------------------------------------+ + * | Option Class | Description | + * +----------------+--------------------------------------+ + * | 0x0000..0x00FF | Unassigned - IETF Review | + * | 0x0100 | Linux | + * | 0x0101 | Open vSwitch | + * | 0x0102 | Open Virtual Networking (OVN) | + * | 0x0103 | In-band Network Telemetry (INT) | + * | 0x0104 | VMware | + * | 0x0105..0xFFEF | Unassigned - First Come First Served | + * | 0xFFF0..FFFF | Experimental | + * +----------------+--------------------------------------+ +*/ +#define LINUX_OPT_CLASS 0x0100 +#define OVS_OPT_CLASS 0x0101 +#define OVN_OPT_CLASS 0x0102 +#define INT_OPT_CLASS 0x0103 +#define VMWARE_OPT_CLASS 0x0104 + +/* + * 0 1 2 3 + * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * | Option Class | Type |R|R|R| Length | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * | Variable Option Data | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + */ +typedef struct +{ + u16 opt_class; + u8 type; + /* The 3 reserved bits are for future use; + * Need to be 0 on sending and ignored on receipt. + */ + u8 res; + /* Length is expressed in 4-bytes multiples excluding the options header. */ + u8 length; + u32 opt_data[]; +} geneve_options_t; + +/* + * + * As per draft https://tools.ietf.org/html/draft-ietf-nvo3-geneve-05 + * + * Section 3/3.4 + * + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * |Ver| Opt Len |O|C| Rsvd. | Protocol Type | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * | Virtual Network Identifier (VNI) | Reserved | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * | Variable Length Options | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * + */ +#define GENEVE_BASE_HEADER_LENGTH 8 // GENEVE BASE HEADER in bytes +#define GENEVE_MAX_TOTAL_HDR_LENGTH 260 + +#define GENEVE_VERSION 0 +#define GENEVE_ETH_PROTOCOL 0x6558 + +typedef struct +{ + /* + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * |Ver| Opt Len |O|C| Rsvd. | Protocol Type | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + */ + u32 first_word; + + /* + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * | Virtual Network Identifier (VNI) | Reserved | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + */ + u32 vni_rsvd; + geneve_options_t opts[]; +} geneve_header_t; + +#define GENEVE_VERSION_SHIFT 30 +#define GENEVE_OPTLEN_SHIFT 24 +#define GENEVE_O_BIT_SHIFT 23 +#define GENEVE_C_BIT_SHIFT 22 +#define GENEVE_6_RESERVED_SHIFT 16 +#define GENEVE_VNI_SHIFT 8 + +#define GENEVE_VERSION_MASK 0xC0000000 +#define GENEVE_OPTLEN_MASK 0x3F000000 +#define GENEVE_O_BIT_MASK 0x00800000 +#define GENEVE_C_BIT_MASK 0x00400000 +#define GENEVE_6_RESERVED_MASK 0x003F0000 +#define GENEVE_PROTOCOL_MASK 0x0000FFFF +#define GENEVE_VNI_MASK 0xFFFFFF00 + +/* + * Return the VNI in host-byte order + */ +static inline u32 +vnet_get_geneve_vni (geneve_header_t * h) +{ + return ((clib_net_to_host_u32 (h->vni_rsvd) & GENEVE_VNI_MASK) >> + GENEVE_VNI_SHIFT); +} + +static inline u32 +vnet_get_geneve_vni_network_order (geneve_header_t * h) +{ + return (h->vni_rsvd & clib_net_to_host_u32 (GENEVE_VNI_MASK)); +} + +static inline void +vnet_set_geneve_vni (geneve_header_t * h, u32 vni) +{ + h->vni_rsvd &= ~(GENEVE_VNI_MASK); + h->vni_rsvd |= + clib_host_to_net_u32 ((vni << GENEVE_VNI_SHIFT) & GENEVE_VNI_MASK); +} + +static inline u8 +vnet_get_geneve_version (geneve_header_t * h) +{ + return ((h->first_word & GENEVE_VERSION_MASK) >> GENEVE_VERSION_SHIFT); +} + +static inline void +vnet_set_geneve_version (geneve_header_t * h, u8 version) +{ + h->first_word &= ~(GENEVE_VERSION_MASK); + h->first_word |= ((version << GENEVE_VERSION_SHIFT) & GENEVE_VERSION_MASK); +} + +static inline u8 +vnet_get_geneve_options_len (geneve_header_t * h) +{ + return ((h->first_word & GENEVE_OPTLEN_MASK) >> GENEVE_OPTLEN_SHIFT); +} + +static inline void +vnet_set_geneve_options_len (geneve_header_t * h, u8 len) +{ + h->first_word &= ~(GENEVE_OPTLEN_MASK); + h->first_word |= ((len << GENEVE_OPTLEN_SHIFT) & GENEVE_OPTLEN_MASK); +} + +static inline u8 +vnet_get_geneve_oamframe_bit (geneve_header_t * h) +{ + return ((h->first_word & GENEVE_O_BIT_MASK) >> GENEVE_O_BIT_SHIFT); +} + +static inline void +vnet_set_geneve_oamframe_bit (geneve_header_t * h, u8 oam) +{ + h->first_word &= ~(GENEVE_O_BIT_MASK); + h->first_word |= ((oam << GENEVE_O_BIT_SHIFT) & GENEVE_O_BIT_MASK); +} + +static inline u8 +vnet_get_geneve_critical_bit (geneve_header_t * h) +{ + return ((h->first_word & GENEVE_C_BIT_MASK) >> GENEVE_C_BIT_SHIFT); +} + +static inline void +vnet_set_geneve_critical_bit (geneve_header_t * h, u8 critical_opts) +{ + h->first_word &= ~(GENEVE_C_BIT_MASK); + h->first_word |= + ((critical_opts << GENEVE_C_BIT_SHIFT) & GENEVE_C_BIT_MASK); +} + +static inline u16 +vnet_get_geneve_protocol (geneve_header_t * h) +{ + return (h->first_word & GENEVE_PROTOCOL_MASK); +} + +static inline void +vnet_set_geneve_protocol (geneve_header_t * h, u16 protocol) +{ + h->first_word &= ~(GENEVE_PROTOCOL_MASK); + h->first_word |= (protocol & GENEVE_PROTOCOL_MASK); +} + +static inline void +vnet_geneve_hdr_1word_ntoh (geneve_header_t * h) +{ + h->first_word = clib_net_to_host_u32 (h->first_word); +} + +static inline void +vnet_geneve_hdr_1word_hton (geneve_header_t * h) +{ + h->first_word = clib_host_to_net_u32 (h->first_word); +} + +#endif + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/plugins/geneve/geneve_test.c b/src/plugins/geneve/geneve_test.c new file mode 100644 index 00000000000..ad6d3296ef6 --- /dev/null +++ b/src/plugins/geneve/geneve_test.c @@ -0,0 +1,412 @@ +/* + * Copyright (c) 2020 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include + +#include +#include +#include + +/* define message IDs */ +#include +#include +#include +#include + +typedef struct +{ + /* API message ID base */ + u16 msg_id_base; + u32 ping_id; + vat_main_t *vat_main; +} geneve_test_main_t; + +geneve_test_main_t geneve_test_main; + +#define __plugin_msg_base geneve_test_main.msg_id_base +#include + +/* Macro to finish up custom dump fns */ +#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__) +#define FINISH \ + vec_add1 (s, 0); \ + vl_print (handle, (char *)s); \ + vec_free (s); \ + return handle; + +static void vl_api_geneve_add_del_tunnel_reply_t_handler + (vl_api_geneve_add_del_tunnel_reply_t * mp) +{ + vat_main_t *vam = &vat_main; + i32 retval = ntohl (mp->retval); + if (vam->async_mode) + { + vam->async_errors += (retval < 0); + } + else + { + vam->retval = retval; + vam->sw_if_index = ntohl (mp->sw_if_index); + vam->result_ready = 1; + } +} + +static void vl_api_geneve_add_del_tunnel2_reply_t_handler + (vl_api_geneve_add_del_tunnel2_reply_t * mp) +{ + vat_main_t *vam = &vat_main; + i32 retval = ntohl (mp->retval); + if (vam->async_mode) + { + vam->async_errors += (retval < 0); + } + else + { + vam->retval = retval; + vam->sw_if_index = ntohl (mp->sw_if_index); + vam->result_ready = 1; + } +} + +static int +api_sw_interface_set_geneve_bypass (vat_main_t * vam) +{ + unformat_input_t *i = vam->input; + vl_api_sw_interface_set_geneve_bypass_t *mp; + u32 sw_if_index = 0; + u8 sw_if_index_set = 0; + u8 is_enable = 1; + u8 is_ipv6 = 0; + int ret; + + /* Parse args required to build the message */ + while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) + { + if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index)) + sw_if_index_set = 1; + else if (unformat (i, "sw_if_index %d", &sw_if_index)) + sw_if_index_set = 1; + else if (unformat (i, "enable")) + is_enable = 1; + else if (unformat (i, "disable")) + is_enable = 0; + else if (unformat (i, "ip4")) + is_ipv6 = 0; + else if (unformat (i, "ip6")) + is_ipv6 = 1; + else + break; + } + + if (sw_if_index_set == 0) + { + errmsg ("missing interface name or sw_if_index"); + return -99; + } + + /* Construct the API message */ + M (SW_INTERFACE_SET_GENEVE_BYPASS, mp); + + mp->sw_if_index = ntohl (sw_if_index); + mp->enable = is_enable; + mp->is_ipv6 = is_ipv6; + + /* send it... */ + S (mp); + + /* Wait for a reply... */ + W (ret); + return ret; +} + +static uword unformat_geneve_decap_next + (unformat_input_t * input, va_list * args) +{ + u32 *result = va_arg (*args, u32 *); + u32 tmp; + + if (unformat (input, "l2")) + *result = GENEVE_INPUT_NEXT_L2_INPUT; + else if (unformat (input, "%d", &tmp)) + *result = tmp; + else + return 0; + return 1; +} + +static int +api_geneve_add_del_tunnel (vat_main_t * vam) +{ + unformat_input_t *line_input = vam->input; + vl_api_geneve_add_del_tunnel_t *mp; + ip46_address_t src, dst; + u8 is_add = 1; + u8 ipv4_set = 0, ipv6_set = 0; + u8 src_set = 0; + u8 dst_set = 0; + u8 grp_set = 0; + u32 mcast_sw_if_index = ~0; + u32 encap_vrf_id = 0; + u32 decap_next_index = ~0; + u32 vni = 0; + int ret; + + /* Can't "universally zero init" (={0}) due to GCC bug 53119 */ + clib_memset (&src, 0, sizeof src); + clib_memset (&dst, 0, sizeof dst); + + while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) + { + if (unformat (line_input, "del")) + is_add = 0; + else + if (unformat (line_input, "src %U", unformat_ip4_address, &src.ip4)) + { + ipv4_set = 1; + src_set = 1; + } + else + if (unformat (line_input, "dst %U", unformat_ip4_address, &dst.ip4)) + { + ipv4_set = 1; + dst_set = 1; + } + else + if (unformat (line_input, "src %U", unformat_ip6_address, &src.ip6)) + { + ipv6_set = 1; + src_set = 1; + } + else + if (unformat (line_input, "dst %U", unformat_ip6_address, &dst.ip6)) + { + ipv6_set = 1; + dst_set = 1; + } + else if (unformat (line_input, "group %U %U", + unformat_ip4_address, &dst.ip4, + unformat_sw_if_index, vam, &mcast_sw_if_index)) + { + grp_set = dst_set = 1; + ipv4_set = 1; + } + else if (unformat (line_input, "group %U", + unformat_ip4_address, &dst.ip4)) + { + grp_set = dst_set = 1; + ipv4_set = 1; + } + else if (unformat (line_input, "group %U %U", + unformat_ip6_address, &dst.ip6, + unformat_sw_if_index, vam, &mcast_sw_if_index)) + { + grp_set = dst_set = 1; + ipv6_set = 1; + } + else if (unformat (line_input, "group %U", + unformat_ip6_address, &dst.ip6)) + { + grp_set = dst_set = 1; + ipv6_set = 1; + } + else + if (unformat (line_input, "mcast_sw_if_index %u", &mcast_sw_if_index)) + ; + else if (unformat (line_input, "encap-vrf-id %d", &encap_vrf_id)) + ; + else if (unformat (line_input, "decap-next %U", + unformat_geneve_decap_next, &decap_next_index)) + ; + else if (unformat (line_input, "vni %d", &vni)) + ; + else + { + errmsg ("parse error '%U'", format_unformat_error, line_input); + return -99; + } + } + + if (src_set == 0) + { + errmsg ("tunnel src address not specified"); + return -99; + } + if (dst_set == 0) + { + errmsg ("tunnel dst address not specified"); + return -99; + } + + if (grp_set && !ip46_address_is_multicast (&dst)) + { + errmsg ("tunnel group address not multicast"); + return -99; + } + if (grp_set && mcast_sw_if_index == ~0) + { + errmsg ("tunnel nonexistent multicast device"); + return -99; + } + if (grp_set == 0 && ip46_address_is_multicast (&dst)) + { + errmsg ("tunnel dst address must be unicast"); + return -99; + } + + + if (ipv4_set && ipv6_set) + { + errmsg ("both IPv4 and IPv6 addresses specified"); + return -99; + } + + if ((vni == 0) || (vni >> 24)) + { + errmsg ("vni not specified or out of range"); + return -99; + } + + M (GENEVE_ADD_DEL_TUNNEL, mp); + + if (ipv6_set) + { + clib_memcpy (&mp->local_address.un.ip6, &src.ip6, sizeof (src.ip6)); + clib_memcpy (&mp->remote_address.un.ip6, &dst.ip6, sizeof (dst.ip6)); + } + else + { + clib_memcpy (&mp->local_address.un.ip4, &src.ip4, sizeof (src.ip4)); + clib_memcpy (&mp->remote_address.un.ip4, &dst.ip4, sizeof (dst.ip4)); + } + mp->encap_vrf_id = ntohl (encap_vrf_id); + mp->decap_next_index = ntohl (decap_next_index); + mp->mcast_sw_if_index = ntohl (mcast_sw_if_index); + mp->vni = ntohl (vni); + mp->is_add = is_add; + + S (mp); + W (ret); + return ret; +} + +static int +api_geneve_add_del_tunnel2 (vat_main_t * vam) +{ + return api_geneve_add_del_tunnel (vam); +} + +static void vl_api_geneve_tunnel_details_t_handler + (vl_api_geneve_tunnel_details_t * mp) +{ + vat_main_t *vam = &vat_main; + ip46_address_t src = {.as_u64[0] = 0,.as_u64[1] = 0 }; + ip46_address_t dst = {.as_u64[0] = 0,.as_u64[1] = 0 }; + + if (mp->src_address.af == ADDRESS_IP6) + { + clib_memcpy (&src.ip6, &mp->src_address.un.ip6, sizeof (ip6_address_t)); + clib_memcpy (&dst.ip6, &mp->dst_address.un.ip6, sizeof (ip6_address_t)); + } + else + { + clib_memcpy (&src.ip4, &mp->src_address.un.ip4, sizeof (ip4_address_t)); + clib_memcpy (&dst.ip4, &mp->dst_address.un.ip4, sizeof (ip4_address_t)); + } + + print (vam->ofp, "%11d%24U%24U%14d%18d%13d%19d", + ntohl (mp->sw_if_index), + format_ip46_address, &src, IP46_TYPE_ANY, + format_ip46_address, &dst, IP46_TYPE_ANY, + ntohl (mp->encap_vrf_id), + ntohl (mp->decap_next_index), ntohl (mp->vni), + ntohl (mp->mcast_sw_if_index)); +} + +static int +api_geneve_tunnel_dump (vat_main_t * vam) +{ + unformat_input_t *i = vam->input; + vl_api_geneve_tunnel_dump_t *mp; + vl_api_control_ping_t *mp_ping; + u32 sw_if_index; + u8 sw_if_index_set = 0; + int ret; + + /* Parse args required to build the message */ + while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) + { + if (unformat (i, "sw_if_index %d", &sw_if_index)) + sw_if_index_set = 1; + else + break; + } + + if (sw_if_index_set == 0) + { + sw_if_index = ~0; + } + + if (!vam->json_output) + { + print (vam->ofp, "%11s%24s%24s%14s%18s%13s%19s", + "sw_if_index", "local_address", "remote_address", + "encap_vrf_id", "decap_next_index", "vni", "mcast_sw_if_index"); + } + + /* Get list of geneve-tunnel interfaces */ + M (GENEVE_TUNNEL_DUMP, mp); + + mp->sw_if_index = htonl (sw_if_index); + + S (mp); + + /* Use a control ping for synchronization */ + if (!geneve_test_main.ping_id) + geneve_test_main.ping_id = + vl_msg_api_get_msg_index ((u8 *) (VL_API_CONTROL_PING_CRC)); + mp_ping = vl_msg_api_alloc_as_if_client (sizeof (*mp_ping)); + mp_ping->_vl_msg_id = htons (geneve_test_main.ping_id); + mp_ping->client_index = vam->my_client_index; + + fformat (vam->ofp, "Sending ping id=%d\n", geneve_test_main.ping_id); + + vam->result_ready = 0; + S (mp_ping); + + W (ret); + return ret; +} + +/* _(sw_interface_set_geneve_bypass, */ +/* " | sw_if_index [ip4 | ip6] [enable | disable]") */ +/* _(geneve_add_del_tunnel, */ +/* "src { dst | group \n" */ +/* "{ | mcast_sw_if_index } }\n" */ +/* "vni [encap-vrf-id ] [decap-next ] [del]") */ +/* _(geneve_tunnel_dump, "[ | sw_if_index ]") */ + + +#include + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/plugins/geneve/test/test_geneve.py b/src/plugins/geneve/test/test_geneve.py new file mode 100644 index 00000000000..9ce1f8ff643 --- /dev/null +++ b/src/plugins/geneve/test/test_geneve.py @@ -0,0 +1,307 @@ +#!/usr/bin/env python3 + +import socket +from util import ip4_range +import unittest +from framework import VppTestCase, VppTestRunner +from template_bd import BridgeDomain + +from scapy.layers.l2 import Ether, ARP +from scapy.layers.inet import IP, UDP, ICMP +from scapy.contrib.geneve import GENEVE + +import util +from vpp_ip_route import VppIpRoute, VppRoutePath +from vpp_ip import INVALID_INDEX + + +class TestGeneve(BridgeDomain, VppTestCase): + """ GENEVE Test Case """ + + def __init__(self, *args): + BridgeDomain.__init__(self) + VppTestCase.__init__(self, *args) + + def encapsulate(self, pkt, vni): + + """ + Encapsulate the original payload frame by adding GENEVE header with its + UDP, IP and Ethernet fields + """ + return (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / + IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) / + UDP(sport=self.dport, dport=self.dport, chksum=0) / + GENEVE(vni=vni) / + pkt) + + def ip_range(self, start, end): + """ range of remote ip's """ + return ip4_range(self.pg0.remote_ip4, start, end) + + def encap_mcast(self, pkt, src_ip, src_mac, vni): + """ + Encapsulate the original payload frame by adding GENEVE header with its + UDP, IP and Ethernet fields + """ + return (Ether(src=src_mac, dst=self.mcast_mac) / + IP(src=src_ip, dst=self.mcast_ip4) / + UDP(sport=self.dport, dport=self.dport, chksum=0) / + GENEVE(vni=vni) / + pkt) + + def decapsulate(self, pkt): + """ + Decapsulate the original payload frame by removing GENEVE header + """ + # check if is set I flag + # self.assertEqual(pkt[GENEVE].flags, int('0x8', 16)) + return pkt[GENEVE].payload + + # Method for checking GENEVE encapsulation. + # + def check_encapsulation(self, pkt, vni, local_only=False, mcast_pkt=False): + # TODO: add error messages + # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved + # by VPP using ARP. + self.assertEqual(pkt[Ether].src, self.pg0.local_mac) + if not local_only: + if not mcast_pkt: + self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac) + else: + self.assertEqual(pkt[Ether].dst, type(self).mcast_mac) + # Verify GENEVE tunnel source IP is VPP_IP and destination IP is MY_IP. + self.assertEqual(pkt[IP].src, self.pg0.local_ip4) + if not local_only: + if not mcast_pkt: + self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4) + else: + self.assertEqual(pkt[IP].dst, type(self).mcast_ip4) + # Verify UDP destination port is GENEVE 4789, source UDP port could be + # arbitrary. + self.assertEqual(pkt[UDP].dport, type(self).dport) + # TODO: checksum check + # Verify VNI + self.assertEqual(pkt[GENEVE].vni, vni) + + @classmethod + def create_geneve_flood_test_bd(cls, vni, n_ucast_tunnels): + # Create 10 ucast geneve tunnels under bd + ip_range_start = 10 + ip_range_end = ip_range_start + n_ucast_tunnels + next_hop_address = cls.pg0.remote_ip4 + for dest_ip4 in ip4_range(next_hop_address, ip_range_start, + ip_range_end): + # add host route so dest_ip4 will not be resolved + rip = VppIpRoute(cls, dest_ip4, 32, + [VppRoutePath(next_hop_address, + INVALID_INDEX)], + register=False) + rip.add_vpp_config() + r = cls.vapi.geneve_add_del_tunnel( + local_address=cls.pg0.local_ip4, remote_address=dest_ip4, + vni=vni) + cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index, + bd_id=vni) + + @classmethod + def add_del_shared_mcast_dst_load(cls, is_add): + """ + add or del tunnels sharing the same mcast dst + to test geneve ref_count mechanism + """ + n_shared_dst_tunnels = 10 + vni_start = 10000 + vni_end = vni_start + n_shared_dst_tunnels + for vni in range(vni_start, vni_end): + r = cls.vapi.geneve_add_del_tunnel( + local_address=cls.pg0.local_ip4, + remote_address=cls.mcast_ip4, mcast_sw_if_index=1, + is_add=is_add, vni=vni) + if r.sw_if_index == 0xffffffff: + raise ValueError("bad sw_if_index: ~0") + + @classmethod + def add_shared_mcast_dst_load(cls): + cls.add_del_shared_mcast_dst_load(is_add=1) + + @classmethod + def del_shared_mcast_dst_load(cls): + cls.add_del_shared_mcast_dst_load(is_add=0) + + @classmethod + def add_del_mcast_tunnels_load(cls, is_add): + """ + add or del tunnels to test geneve stability + """ + n_distinct_dst_tunnels = 10 + ip_range_start = 10 + ip_range_end = ip_range_start + n_distinct_dst_tunnels + for dest_ip4 in ip4_range(cls.mcast_ip4, ip_range_start, + ip_range_end): + vni = int(dest_ip4.split('.')[3]) + cls.vapi.geneve_add_del_tunnel(local_address=cls.pg0.local_ip4, + remote_address=dest_ip4, + mcast_sw_if_index=1, is_add=is_add, + vni=vni) + + @classmethod + def add_mcast_tunnels_load(cls): + cls.add_del_mcast_tunnels_load(is_add=1) + + @classmethod + def del_mcast_tunnels_load(cls): + cls.add_del_mcast_tunnels_load(is_add=0) + + # Class method to start the GENEVE test case. + # Overrides setUpClass method in VppTestCase class. + # Python try..except statement is used to ensure that the tear down of + # the class will be executed even if exception is raised. + # @param cls The class pointer. + @classmethod + def setUpClass(cls): + super(TestGeneve, cls).setUpClass() + + try: + cls.dport = 6081 + + # Create 2 pg interfaces. + cls.create_pg_interfaces(range(4)) + for pg in cls.pg_interfaces: + pg.admin_up() + + # Configure IPv4 addresses on VPP pg0. + cls.pg0.config_ip4() + + # Resolve MAC address for VPP's IP address on pg0. + cls.pg0.resolve_arp() + + # Our Multicast address + cls.mcast_ip4 = '239.1.1.1' + cls.mcast_mac = util.mcast_ip_to_mac(cls.mcast_ip4) + + # Create GENEVE VTEP on VPP pg0, and put geneve_tunnel0 and pg1 + # into BD. + cls.single_tunnel_vni = 0xabcde + cls.single_tunnel_bd = 1 + r = cls.vapi.geneve_add_del_tunnel( + local_address=cls.pg0.local_ip4, + remote_address=cls.pg0.remote_ip4, vni=cls.single_tunnel_vni) + cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index, + bd_id=cls.single_tunnel_bd) + cls.vapi.sw_interface_set_l2_bridge( + rx_sw_if_index=cls.pg1.sw_if_index, bd_id=cls.single_tunnel_bd) + + # Setup vni 2 to test multicast flooding + cls.n_ucast_tunnels = 10 + cls.mcast_flood_bd = 2 + cls.create_geneve_flood_test_bd(cls.mcast_flood_bd, + cls.n_ucast_tunnels) + r = cls.vapi.geneve_add_del_tunnel( + local_address=cls.pg0.local_ip4, + remote_address=cls.mcast_ip4, mcast_sw_if_index=1, + vni=cls.mcast_flood_bd) + cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index, + bd_id=cls.mcast_flood_bd) + cls.vapi.sw_interface_set_l2_bridge( + rx_sw_if_index=cls.pg2.sw_if_index, bd_id=cls.mcast_flood_bd) + + # Add and delete mcast tunnels to check stability + cls.add_shared_mcast_dst_load() + cls.add_mcast_tunnels_load() + cls.del_shared_mcast_dst_load() + cls.del_mcast_tunnels_load() + + # Setup vni 3 to test unicast flooding + cls.ucast_flood_bd = 3 + cls.create_geneve_flood_test_bd(cls.ucast_flood_bd, + cls.n_ucast_tunnels) + cls.vapi.sw_interface_set_l2_bridge( + rx_sw_if_index=cls.pg3.sw_if_index, bd_id=cls.ucast_flood_bd) + except Exception: + super(TestGeneve, cls).tearDownClass() + raise + + # Method to define VPP actions before tear down of the test case. + # Overrides tearDown method in VppTestCase class. + # @param self The object pointer. + def tearDown(self): + super(TestGeneve, self).tearDown() + + def show_commands_at_teardown(self): + self.logger.info(self.vapi.cli("show bridge-domain 1 detail")) + self.logger.info(self.vapi.cli("show bridge-domain 2 detail")) + self.logger.info(self.vapi.cli("show bridge-domain 3 detail")) + self.logger.info(self.vapi.cli("show geneve tunnel")) + + +class TestGeneveL3(VppTestCase): + """ GENEVE L3 Test Case """ + + @classmethod + def setUpClass(cls): + super(TestGeneveL3, cls).setUpClass() + try: + cls.create_pg_interfaces(range(2)) + cls.interfaces = list(cls.pg_interfaces) + + for i in cls.interfaces: + i.admin_up() + i.config_ip4() + i.resolve_arp() + except Exception: + super(TestGeneveL3, cls).tearDownClass() + raise + + @classmethod + def tearDownClass(cls): + super(TestGeneveL3, cls).tearDownClass() + + def tearDown(self): + super(TestGeneveL3, self).tearDown() + + def show_commands_at_teardown(self): + self.logger.info(self.vapi.cli("show geneve tunnel")) + self.logger.info(self.vapi.cli("show ip neighbor")) + + def test_l3_packet(self): + vni = 1234 + r = self.vapi.add_node_next(node_name="geneve4-input", + next_name="ethernet-input") + r = self.vapi.geneve_add_del_tunnel2( + is_add=1, + local_address=self.pg0.local_ip4, + remote_address=self.pg0.remote_ip4, + vni=vni, + l3_mode=1, + decap_next_index=r.next_index) + + self.vapi.sw_interface_add_del_address( + sw_if_index=r.sw_if_index, prefix="10.0.0.1/24") + + pkt = (Ether(src=self.pg0.remote_mac, dst="d0:0b:ee:d0:00:00") / + IP(src='10.0.0.2', dst='10.0.0.1') / + ICMP()) + + encap = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / + IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) / + UDP(sport=6081, dport=6081, chksum=0) / + GENEVE(vni=vni)) + + arp = (Ether(src=self.pg0.remote_mac, dst="d0:0b:ee:d0:00:00") / + ARP(op="is-at", hwsrc=self.pg0.remote_mac, + hwdst="d0:0b:ee:d0:00:00", psrc="10.0.0.2", + pdst="10.0.0.1")) + + rx = self.send_and_expect(self.pg0, encap/pkt*1, self.pg0) + rx = self.send_and_assert_no_replies(self.pg0, encap/arp*1, self.pg0) + rx = self.send_and_expect(self.pg0, encap/pkt*1, self.pg0) + self.assertEqual(rx[0][ICMP].type, 0) # echo reply + + r = self.vapi.geneve_add_del_tunnel2( + is_add=0, + local_address=self.pg0.local_ip4, + remote_address=self.pg0.remote_ip4, + vni=vni) + +if __name__ == '__main__': + unittest.main(testRunner=VppTestRunner) -- cgit 1.2.3-korg