diff options
author | Marco Varlese <marco.varlese@suse.com> | 2017-09-19 14:25:28 +0200 |
---|---|---|
committer | Damjan Marion <dmarion.lists@gmail.com> | 2017-10-06 08:51:09 +0000 |
commit | b598f1d3d7d4ace9a29c01d93a8d1ba616a91e15 (patch) | |
tree | 25f408f1255c61138499d146f66da27d390c7cbd | |
parent | 4e5ceefb5522cfde1e916d84b56c318ec1ea614e (diff) |
Initial GENEVE TUNNEL implementation and tests.
Notes on this first implementation:
* First version of the implementation does NOT support GENEVE OPTIONS
HEADER: it isn't well understood what the purpose of the OPTIONS will be and/or
what content would be placed in the variable option data;
Once the IETF work will evolve and further information will be available
it could be possible to modify the frame rewrite to contemplate the
actual GENEVE OPTIONS.
Change-Id: Iddfe6f408cc45bb0800f00ce6a3e302e48a4ed52
Signed-off-by: Marco Varlese <marco.varlese@suse.com>
-rw-r--r-- | src/vat/api_format.c | 377 | ||||
-rw-r--r-- | src/vnet.am | 17 | ||||
-rw-r--r-- | src/vnet/fib/fib_node.h | 1 | ||||
-rw-r--r-- | src/vnet/geneve/decap.c | 1323 | ||||
-rw-r--r-- | src/vnet/geneve/encap.c | 594 | ||||
-rw-r--r-- | src/vnet/geneve/geneve.api | 71 | ||||
-rw-r--r-- | src/vnet/geneve/geneve.c | 1158 | ||||
-rw-r--r-- | src/vnet/geneve/geneve.h | 213 | ||||
-rw-r--r-- | src/vnet/geneve/geneve_api.c | 237 | ||||
-rw-r--r-- | src/vnet/geneve/geneve_error.def | 18 | ||||
-rw-r--r-- | src/vnet/geneve/geneve_packet.h | 180 | ||||
-rw-r--r-- | src/vnet/mfib/mfib_types.h | 2 | ||||
-rw-r--r-- | src/vnet/udp/udp.h | 6 | ||||
-rw-r--r-- | src/vnet/vnet_all_api_h.h | 1 | ||||
-rw-r--r-- | src/vpp/api/custom_dump.c | 68 | ||||
-rw-r--r-- | src/vpp/api/vpe.api | 1 | ||||
-rw-r--r-- | test/patches/scapy-2.3.3/enable_geneve.patch | 13 | ||||
-rw-r--r-- | test/patches/scapy-2.3.3/geneve.patch | 56 | ||||
-rw-r--r-- | test/test_geneve.py | 234 | ||||
-rw-r--r-- | test/vpp_papi_provider.py | 32 |
20 files changed, 4600 insertions, 2 deletions
diff --git a/src/vat/api_format.c b/src/vat/api_format.c index 99bef05a8dc..ba1a791afec 100644 --- a/src/vat/api_format.c +++ b/src/vat/api_format.c @@ -26,6 +26,7 @@ #include <vnet/l2/l2_input.h> #include <vnet/l2tp/l2tp.h> #include <vnet/vxlan/vxlan.h> +#include <vnet/geneve/geneve.h> #include <vnet/gre/gre.h> #include <vnet/vxlan-gpe/vxlan_gpe.h> #include <vnet/lisp-gpe/lisp_gpe.h> @@ -1919,6 +1920,40 @@ static void vl_api_vxlan_add_del_tunnel_reply_t_handler_json vam->result_ready = 1; } +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_tunnel_reply_t_handler_json + (vl_api_geneve_add_del_tunnel_reply_t * mp) +{ + vat_main_t *vam = &vat_main; + vat_json_node_t node; + + vat_json_init_object (&node); + vat_json_object_add_int (&node, "retval", ntohl (mp->retval)); + vat_json_object_add_uint (&node, "sw_if_index", ntohl (mp->sw_if_index)); + + vat_json_print (vam->ofp, &node); + vat_json_free (&node); + + vam->retval = ntohl (mp->retval); + vam->result_ready = 1; +} + static void vl_api_vxlan_gpe_add_del_tunnel_reply_t_handler (vl_api_vxlan_gpe_add_del_tunnel_reply_t * mp) { @@ -4951,6 +4986,7 @@ _(sw_interface_set_table_reply) \ _(sw_interface_set_mpls_enable_reply) \ _(sw_interface_set_vpath_reply) \ _(sw_interface_set_vxlan_bypass_reply) \ +_(sw_interface_set_geneve_bypass_reply) \ _(sw_interface_set_vxlan_gpe_bypass_reply) \ _(sw_interface_set_l2_bridge_reply) \ _(bridge_domain_add_del_reply) \ @@ -5132,6 +5168,7 @@ _(SW_INTERFACE_SET_TABLE_REPLY, sw_interface_set_table_reply) \ _(SW_INTERFACE_SET_MPLS_ENABLE_REPLY, sw_interface_set_mpls_enable_reply) \ _(SW_INTERFACE_SET_VPATH_REPLY, sw_interface_set_vpath_reply) \ _(SW_INTERFACE_SET_VXLAN_BYPASS_REPLY, sw_interface_set_vxlan_bypass_reply) \ +_(SW_INTERFACE_SET_GENEVE_BYPASS_REPLY, sw_interface_set_geneve_bypass_reply) \ _(SW_INTERFACE_SET_VXLAN_GPE_BYPASS_REPLY, sw_interface_set_vxlan_gpe_bypass_reply) \ _(SW_INTERFACE_SET_L2_XCONNECT_REPLY, \ sw_interface_set_l2_xconnect_reply) \ @@ -5204,7 +5241,9 @@ _(L2TPV3_INTERFACE_ENABLE_DISABLE_REPLY, \ _(L2TPV3_SET_LOOKUP_KEY_REPLY, l2tpv3_set_lookup_key_reply) \ _(SW_IF_L2TPV3_TUNNEL_DETAILS, sw_if_l2tpv3_tunnel_details) \ _(VXLAN_ADD_DEL_TUNNEL_REPLY, vxlan_add_del_tunnel_reply) \ +_(GENEVE_ADD_DEL_TUNNEL_REPLY, geneve_add_del_tunnel_reply) \ _(VXLAN_TUNNEL_DETAILS, vxlan_tunnel_details) \ +_(GENEVE_TUNNEL_DETAILS, geneve_tunnel_details) \ _(GRE_ADD_DEL_TUNNEL_REPLY, gre_add_del_tunnel_reply) \ _(GRE_TUNNEL_DETAILS, gre_tunnel_details) \ _(L2_FIB_CLEAR_TABLE_REPLY, l2_fib_clear_table_reply) \ @@ -6026,6 +6065,12 @@ api_sw_interface_dump (vat_main_t * vam) strncpy ((char *) mp->name_filter, "vxlan", sizeof (mp->name_filter) - 1); S (mp); + /* and geneve tunnel interfaces */ + M (SW_INTERFACE_DUMP, mp); + mp->name_filter_valid = 1; + strncpy ((char *) mp->name_filter, "geneve", sizeof (mp->name_filter) - 1); + S (mp); + /* and host (af_packet) interfaces */ M (SW_INTERFACE_DUMP, mp); mp->name_filter_valid = 1; @@ -6478,6 +6523,56 @@ api_sw_interface_set_vxlan_bypass (vat_main_t * vam) return ret; } +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", api_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 int api_sw_interface_set_l2_xconnect (vat_main_t * vam) @@ -11992,6 +12087,281 @@ api_vxlan_tunnel_dump (vat_main_t * vam) 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 */ + memset (&src, 0, sizeof src); + 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, + api_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, + api_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, &src.ip6, sizeof (src.ip6)); + clib_memcpy (mp->remote_address, &dst.ip6, sizeof (dst.ip6)); + } + else + { + clib_memcpy (mp->local_address, &src.ip4, sizeof (src.ip4)); + clib_memcpy (mp->remote_address, &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; + mp->is_ipv6 = ipv6_set; + + S (mp); + W (ret); + return ret; +} + +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 = to_ip46 (mp->is_ipv6, mp->dst_address); + ip46_address_t dst = to_ip46 (mp->is_ipv6, mp->src_address); + + 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 void vl_api_geneve_tunnel_details_t_handler_json + (vl_api_geneve_tunnel_details_t * mp) +{ + vat_main_t *vam = &vat_main; + vat_json_node_t *node = NULL; + + if (VAT_JSON_ARRAY != vam->json_tree.type) + { + ASSERT (VAT_JSON_NONE == vam->json_tree.type); + vat_json_init_array (&vam->json_tree); + } + node = vat_json_array_add (&vam->json_tree); + + vat_json_init_object (node); + vat_json_object_add_uint (node, "sw_if_index", ntohl (mp->sw_if_index)); + if (mp->is_ipv6) + { + struct in6_addr ip6; + + clib_memcpy (&ip6, mp->src_address, sizeof (ip6)); + vat_json_object_add_ip6 (node, "src_address", ip6); + clib_memcpy (&ip6, mp->dst_address, sizeof (ip6)); + vat_json_object_add_ip6 (node, "dst_address", ip6); + } + else + { + struct in_addr ip4; + + clib_memcpy (&ip4, mp->src_address, sizeof (ip4)); + vat_json_object_add_ip4 (node, "src_address", ip4); + clib_memcpy (&ip4, mp->dst_address, sizeof (ip4)); + vat_json_object_add_ip4 (node, "dst_address", ip4); + } + vat_json_object_add_uint (node, "encap_vrf_id", ntohl (mp->encap_vrf_id)); + vat_json_object_add_uint (node, "decap_next_index", + ntohl (mp->decap_next_index)); + vat_json_object_add_uint (node, "vni", ntohl (mp->vni)); + vat_json_object_add_uint (node, "is_ipv6", mp->is_ipv6 ? 1 : 0); + vat_json_object_add_uint (node, "mcast_sw_if_index", + 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 */ + M (CONTROL_PING, mp_ping); + S (mp_ping); + + W (ret); + return ret; +} + static int api_gre_add_del_tunnel (vat_main_t * vam) { @@ -20773,6 +21143,8 @@ _(sw_interface_set_vpath, \ "<intfc> | sw_if_index <id> enable | disable") \ _(sw_interface_set_vxlan_bypass, \ "<intfc> | sw_if_index <id> [ip4 | ip6] [enable | disable]") \ +_(sw_interface_set_geneve_bypass, \ + "<intfc> | sw_if_index <id> [ip4 | ip6] [enable | disable]") \ _(sw_interface_set_l2_xconnect, \ "rx <intfc> | rx_sw_if_index <id> tx <intfc> | tx_sw_if_index <id>\n" \ "enable | disable") \ @@ -20903,7 +21275,12 @@ _(vxlan_add_del_tunnel, \ "src <ip-addr> { dst <ip-addr> | group <mcast-ip-addr>\n" \ "{ <intfc> | mcast_sw_if_index <nn> } }\n" \ "vni <vni> [encap-vrf-id <nn>] [decap-next <l2|nn>] [del]") \ +_(geneve_add_del_tunnel, \ + "src <ip-addr> { dst <ip-addr> | group <mcast-ip-addr>\n" \ + "{ <intfc> | mcast_sw_if_index <nn> } }\n" \ + "vni <vni> [encap-vrf-id <nn>] [decap-next <l2|nn>] [del]") \ _(vxlan_tunnel_dump, "[<intfc> | sw_if_index <nn>]") \ +_(geneve_tunnel_dump, "[<intfc> | sw_if_index <nn>]") \ _(gre_add_del_tunnel, \ "src <ip-addr> dst <ip-addr> [outer-fib-id <nn>] [teb] [del]\n") \ _(gre_tunnel_dump, "[<intfc> | sw_if_index <nn>]") \ diff --git a/src/vnet.am b/src/vnet.am index 36d16c0453e..15b3467c772 100644 --- a/src/vnet.am +++ b/src/vnet.am @@ -253,6 +253,23 @@ nobase_include_HEADERS += \ API_FILES += vnet/vxlan/vxlan.api ######################################## +# Layer 2 / Geneve +######################################## +libvnet_la_SOURCES += \ + vnet/geneve/geneve.c \ + vnet/geneve/encap.c \ + vnet/geneve/decap.c \ + vnet/geneve/geneve_api.c + +nobase_include_HEADERS += \ + vnet/geneve/geneve.h \ + vnet/geneve/geneve_packet.h \ + vnet/geneve/geneve_error.def \ + vnet/geneve/geneve.api.h + +API_FILES += vnet/geneve/geneve.api + +######################################## # Layer 2 / CDP ######################################## libvnet_la_SOURCES += \ diff --git a/src/vnet/fib/fib_node.h b/src/vnet/fib/fib_node.h index ec517e157db..762e0cf01af 100644 --- a/src/vnet/fib/fib_node.h +++ b/src/vnet/fib/fib_node.h @@ -43,6 +43,7 @@ typedef enum fib_node_type_t_ { FIB_NODE_TYPE_VXLAN_TUNNEL, FIB_NODE_TYPE_MAP_E, FIB_NODE_TYPE_VXLAN_GPE_TUNNEL, + FIB_NODE_TYPE_GENEVE_TUNNEL, /** * Marker. New types before this one. leave the test last. */ diff --git a/src/vnet/geneve/decap.c b/src/vnet/geneve/decap.c new file mode 100644 index 00000000000..db052145ce2 --- /dev/null +++ b/src/vnet/geneve/decap.c @@ -0,0 +1,1323 @@ +/* + * 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 <vlib/vlib.h> +#include <vnet/pg/pg.h> +#include <vnet/geneve/geneve.h> + +vlib_node_registration_t geneve4_input_node; +vlib_node_registration_t geneve6_input_node; + +typedef struct +{ + u32 next_index; + u32 tunnel_index; + u32 error; + u32 vni; +} 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, t->next_index, t->error); + } + else + { + s = format (s, "GENEVE decap error - tunnel for vni %d does not exist", + t->vni); + } + 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 = vlib_get_thread_index (); + u32 stats_sw_if_index, stats_n_packets, stats_n_bytes; + + if (is_ip4) + last_key4.as_u64 = ~0; + else + memset (&last_key6, 0xff, sizeof (last_key6)); + + from = vlib_frame_vector_args (from_frame); + n_left_from = from_frame->n_vectors; + + 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_buffer_t *p2, *p3; + + p2 = vlib_get_buffer (vm, from[2]); + p3 = vlib_get_buffer (vm, from[3]); + + vlib_prefetch_buffer_header (p2, LOAD); + vlib_prefetch_buffer_header (p3, LOAD); + + CLIB_PREFETCH (p2->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD); + CLIB_PREFETCH (p3->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 = vlib_get_buffer (vm, bi0); + b1 = vlib_get_buffer (vm, bi1); + + /* udp leaves current_data pointing at the geneve header */ + geneve0 = vlib_buffer_get_current (b0); + geneve1 = vlib_buffer_get_current (b1); + 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 (b0, + sizeof (*ip6_0) + 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 (geneve0->ver != GENEVE_VERSION)) + { + error0 = GENEVE_ERROR_BAD_FLAGS; + next0 = GENEVE_INPUT_NEXT_DROP; + goto trace0; + } +#if SUPPORT_OPTIONS_HEADER==0 + 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 = geneve0->vni; + + /* 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 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 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 = geneve0->vni; + /* 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 = geneve0->vni; + + /* 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 (&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 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 = geneve0->vni; + 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 = vnet_get_geneve_vni (geneve0); + } + + if (PREDICT_FALSE (geneve1->ver != GENEVE_VERSION)) + { + error1 = GENEVE_ERROR_BAD_FLAGS; + next1 = GENEVE_INPUT_NEXT_DROP; + goto trace1; + } +#if SUPPORT_OPTIONS_HEADER==0 + 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 = geneve1->vni; + + /* 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 agaist 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 = geneve1->vni; + /* 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 = geneve1->vni; + + /* 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 (&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 agaist 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 = geneve1->vni; + 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 = 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 = vlib_get_buffer (vm, bi0); + + /* udp leaves current_data pointing at the geneve header */ + geneve0 = vlib_buffer_get_current (b0); + 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 (geneve0->ver != GENEVE_VERSION)) + { + error0 = GENEVE_ERROR_BAD_FLAGS; + next0 = GENEVE_INPUT_NEXT_DROP; + goto trace00; + } +#if SUPPORT_OPTIONS_HEADER==0 + 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 = geneve0->vni; + + /* 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 = geneve0->vni; + /* 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 = geneve0->vni; + + /* 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 (&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 = geneve0->vni; + 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 = 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; +} + +static uword +geneve4_input (vlib_main_t * vm, + vlib_node_runtime_t * node, vlib_frame_t * from_frame) +{ + return geneve_input (vm, node, from_frame, /* is_ip4 */ 1); +} + +static uword +geneve6_input (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 <vnet/geneve/geneve_error.def> +#undef geneve_error +#undef _ +}; + +/* *INDENT-OFF* */ +VLIB_REGISTER_NODE (geneve4_input_node) = { + .function = geneve4_input, + .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_NODE_FUNCTION_MULTIARCH (geneve4_input_node, geneve4_input) + +VLIB_REGISTER_NODE (geneve6_input_node) = { + .function = geneve6_input, + .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, +}; + +VLIB_NODE_FUNCTION_MULTIARCH (geneve6_input_node, geneve6_input) +/* *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); + ip4_address_t addr4; /* last IPv4 address matching a local VTEP address */ + ip6_address_t addr6; /* last IPv6 address matching a local VTEP address */ + + from = vlib_frame_vector_args (frame); + n_left_from = frame->n_vectors; + next_index = node->cached_next_index; + + if (node->flags & VLIB_NODE_FLAG_TRACE) + ip4_forward_next_trace (vm, node, frame, VLIB_TX); + + if (is_ip4) + addr4.data_u32 = ~0; + else + ip6_address_set_zero (&addr6); + + 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_buffer_t *p2, *p3; + + p2 = vlib_get_buffer (vm, from[2]); + p3 = vlib_get_buffer (vm, from[3]); + + vlib_prefetch_buffer_header (p2, LOAD); + vlib_prefetch_buffer_header (p3, LOAD); + + CLIB_PREFETCH (p2->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD); + CLIB_PREFETCH (p3->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 = vlib_get_buffer (vm, bi0); + b1 = vlib_get_buffer (vm, bi1); + 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 (vnet_buffer (b0)->sw_if_index[VLIB_RX], &next0, + b0); + vnet_feature_next (vnet_buffer (b1)->sw_if_index[VLIB_RX], &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) + { + if (addr4.as_u32 != ip40->dst_address.as_u32) + { + if (!hash_get (vxm->vtep4, ip40->dst_address.as_u32)) + goto exit0; /* no local VTEP for GENEVE packet */ + addr4 = ip40->dst_address; + } + } + else + { + if (!ip6_address_is_equal (&addr6, &ip60->dst_address)) + { + if (!hash_get_mem (vxm->vtep6, &ip60->dst_address)) + goto exit0; /* no local VTEP for GENEVE packet */ + addr6 = ip60->dst_address; + } + } + + 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) + { + if (addr4.as_u32 != ip41->dst_address.as_u32) + { + if (!hash_get (vxm->vtep4, ip41->dst_address.as_u32)) + goto exit1; /* no local VTEP for GENEVE packet */ + addr4 = ip41->dst_address; + } + } + else + { + if (!ip6_address_is_equal (&addr6, &ip61->dst_address)) + { + if (!hash_get_mem (vxm->vtep6, &ip61->dst_address)) + goto exit1; /* no local VTEP for GENEVE packet */ + addr6 = ip61->dst_address; + } + } + + 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 = vlib_get_buffer (vm, bi0); + 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 (vnet_buffer (b0)->sw_if_index[VLIB_RX], &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) + { + if (addr4.as_u32 != ip40->dst_address.as_u32) + { + if (!hash_get (vxm->vtep4, ip40->dst_address.as_u32)) + goto exit; /* no local VTEP for GENEVE packet */ + addr4 = ip40->dst_address; + } + } + else + { + if (!ip6_address_is_equal (&addr6, &ip60->dst_address)) + { + if (!hash_get_mem (vxm->vtep6, &ip60->dst_address)) + goto exit; /* no local VTEP for GENEVE packet */ + addr6 = ip60->dst_address; + } + } + + 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; +} + +static uword +ip4_geneve_bypass (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) = +{ + .function = ip4_geneve_bypass,.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,}; + +VLIB_NODE_FUNCTION_MULTIARCH (ip4_geneve_bypass_node, ip4_geneve_bypass) +/* Dummy init function to get us linked in. */ + clib_error_t *ip4_geneve_bypass_init (vlib_main_t * vm) +{ + return 0; +} + +VLIB_INIT_FUNCTION (ip4_geneve_bypass_init); +/* *INDENT-ON* */ + +static uword +ip6_geneve_bypass (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) = +{ + .function = ip6_geneve_bypass,.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* */ + +VLIB_NODE_FUNCTION_MULTIARCH (ip6_geneve_bypass_node, ip6_geneve_bypass) +/* Dummy init function to get us linked in. */ + clib_error_t *ip6_geneve_bypass_init (vlib_main_t * vm) +{ + return 0; +} + +VLIB_INIT_FUNCTION (ip6_geneve_bypass_init); + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/vnet/geneve/encap.c b/src/vnet/geneve/encap.c new file mode 100644 index 00000000000..59d5c96c974 --- /dev/null +++ b/src/vnet/geneve/encap.c @@ -0,0 +1,594 @@ +/* + * 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 <vppinfra/error.h> +#include <vppinfra/hash.h> +#include <vnet/vnet.h> +#include <vnet/ip/ip.h> +#include <vnet/ethernet/ethernet.h> +#include <vnet/geneve/geneve.h> + +/* 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; + +typedef struct +{ + u32 tunnel_index; + u32 vni; +} geneve_encap_trace_t; + +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; +} + + +#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 = vlib_get_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; + + from = vlib_frame_vector_args (from_frame); + n_left_from = from_frame->n_vectors; + + 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 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_buffer_t *p2, *p3; + + p2 = vlib_get_buffer (vm, from[2]); + p3 = vlib_get_buffer (vm, from[3]); + + vlib_prefetch_buffer_header (p2, LOAD); + vlib_prefetch_buffer_header (p3, LOAD); + + CLIB_PREFETCH (p2->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD); + CLIB_PREFETCH (p3->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 = vlib_get_buffer (vm, bi0); + b1 = vlib_get_buffer (vm, bi1); + + flow_hash0 = vnet_l2_compute_flow_hash (b0); + flow_hash1 = vnet_l2_compute_flow_hash (b1); + + /* Get next node index and adj index from tunnel next_dpo */ + if (sw_if_index0 != vnet_buffer (b0)->sw_if_index[VLIB_TX]) + { + sw_if_index0 = vnet_buffer (b0)->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; + } + vnet_buffer (b0)->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 (b1)->sw_if_index[VLIB_TX]) + { + sw_if_index1 = vnet_buffer (b1)->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; + } + vnet_buffer (b1)->ip.adj_index[VLIB_TX] = t1->next_dpo.dpoi_index; + + /* Apply the rewrite string. $$$$ vnet_rewrite? */ + vlib_buffer_advance (b0, -(word) _vec_len (t0->rewrite)); + vlib_buffer_advance (b1, -(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 (b0); + ip4_1 = vlib_buffer_get_current (b1); + + /* 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, b0)); + 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, b1)); + 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, b0) - + 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, b1) - + 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 (b0); + ip6_1 = vlib_buffer_get_current (b1); + + /* 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, b0) + - sizeof (*ip6_0)); + ip6_0->payload_length = new_l0; + new_l1 = + clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1) + - 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, b0, + ip6_0, + &bogus); + ASSERT (bogus == 0); + if (udp0->checksum == 0) + udp0->checksum = 0xffff; + udp1->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b1, + ip6_1, + &bogus); + ASSERT (bogus == 0); + if (udp1->checksum == 0) + udp1->checksum = 0xffff; + } + + pkts_encapsulated += 2; + len0 = vlib_buffer_length_in_chain (vm, b0); + len1 = vlib_buffer_length_in_chain (vm, b1); + stats_n_packets += 2; + stats_n_bytes += len0 + len1; + + /* 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 (b0->flags & VLIB_BUFFER_IS_TRACED)) + { + geneve_encap_trace_t *tr = + vlib_add_trace (vm, node, b0, sizeof (*tr)); + tr->tunnel_index = t0 - vxm->tunnels; + tr->vni = t0->vni; + } + + if (PREDICT_FALSE (b1->flags & VLIB_BUFFER_IS_TRACED)) + { + geneve_encap_trace_t *tr = + vlib_add_trace (vm, node, b1, sizeof (*tr)); + tr->tunnel_index = t1 - vxm->tunnels; + tr->vni = t1->vni; + } + + 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 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; + + b0 = vlib_get_buffer (vm, bi0); + + flow_hash0 = vnet_l2_compute_flow_hash (b0); + + /* Get next node index and adj index from tunnel next_dpo */ + if (sw_if_index0 != vnet_buffer (b0)->sw_if_index[VLIB_TX]) + { + sw_if_index0 = vnet_buffer (b0)->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; + } + vnet_buffer (b0)->ip.adj_index[VLIB_TX] = t0->next_dpo.dpoi_index; + + /* Apply the rewrite string. $$$$ vnet_rewrite? */ + vlib_buffer_advance (b0, -(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 (b0); + + /* 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, b0)); + 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, b0) - + 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 (b0); + /* 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, b0) + - 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, b0, + ip6_0, + &bogus); + ASSERT (bogus == 0); + if (udp0->checksum == 0) + udp0->checksum = 0xffff; + } + + pkts_encapsulated++; + len0 = vlib_buffer_length_in_chain (vm, b0); + stats_n_packets += 1; + stats_n_bytes += len0; + + /* 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 (b0->flags & VLIB_BUFFER_IS_TRACED)) + { + geneve_encap_trace_t *tr = + vlib_add_trace (vm, node, b0, sizeof (*tr)); + tr->tunnel_index = t0 - vxm->tunnels; + tr->vni = t0->vni; + } + 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; +} + +static uword +geneve4_encap (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); +} + +static uword +geneve6_encap (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) = { + .function = geneve4_encap, + .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_NODE_FUNCTION_MULTIARCH (geneve4_encap_node, geneve4_encap) + +VLIB_REGISTER_NODE (geneve6_encap_node) = { + .function = geneve6_encap, + .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", + }, +}; + +VLIB_NODE_FUNCTION_MULTIARCH (geneve6_encap_node, geneve6_encap) +/* *INDENT-ON* */ + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/vnet/geneve/geneve.api b/src/vnet/geneve/geneve.api new file mode 100644 index 00000000000..eb391055b8e --- /dev/null +++ b/src/vnet/geneve/geneve.api @@ -0,0 +1,71 @@ +/* + * 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. + */ + + define geneve_add_del_tunnel +{ + u32 client_index; + u32 context; + u8 is_add; + u8 is_ipv6; + u8 local_address[16]; + u8 remote_address[16]; + u32 mcast_sw_if_index; + u32 encap_vrf_id; + u32 decap_next_index; + u32 vni; +}; + +define geneve_add_del_tunnel_reply +{ + u32 context; + i32 retval; + u32 sw_if_index; +}; + +define geneve_tunnel_dump +{ + u32 client_index; + u32 context; + u32 sw_if_index; +}; + +define geneve_tunnel_details +{ + u32 context; + u32 sw_if_index; + u8 src_address[16]; + u8 dst_address[16]; + u32 mcast_sw_if_index; + u32 encap_vrf_id; + u32 decap_next_index; + u32 vni; + u8 is_ipv6; +}; + +/** \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; + u32 sw_if_index; + u8 is_ipv6; + u8 enable; +}; diff --git a/src/vnet/geneve/geneve.c b/src/vnet/geneve/geneve.c new file mode 100644 index 00000000000..86250e9430b --- /dev/null +++ b/src/vnet/geneve/geneve.c @@ -0,0 +1,1158 @@ +/* + * 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 <vnet/geneve/geneve.h> +#include <vnet/ip/format.h> +#include <vnet/fib/fib_entry.h> +#include <vnet/fib/fib_table.h> +#include <vnet/mfib/mfib_table.h> +#include <vnet/adj/adj_mcast.h> +#include <vnet/interface.h> +#include <vlib/vlib.h> + +/** + * @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. + * + * You can refer to this kind of L2 overlay bridge domain as a GENEVE + * (Virtual eXtensible VLAN) segment. + */ + + +geneve_main_t geneve_main; + +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] local %U remote %U vni %d sw_if_index %d ", + t - ngm->tunnels, + format_ip46_address, &t->local, IP46_TYPE_ANY, + format_ip46_address, &t->remote, IP46_TYPE_ANY, + t->vni, t->sw_if_index); + + if (ip46_address_is_multicast (&t->remote)) + s = format (s, "mcast_sw_if_index %d ", t->mcast_sw_if_index); + + s = format (s, "encap_fib_index %d fib_entry_index %d decap_next %U\n", + t->encap_fib_index, t->fib_entry_index, + format_decap_next, t->decap_next_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 uword +dummy_interface_tx (vlib_main_t * vm, + vlib_node_runtime_t * node, vlib_frame_t * frame) +{ + clib_warning ("you shouldn't be here, leaking buffers..."); + return frame->n_vectors; +} + +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; +} + +/* *INDENT-OFF* */ +VNET_DEVICE_CLASS (geneve_device_class, static) = { + .name = "GENEVE", + .format_device_name = format_geneve_name, + .format_tx_trace = format_geneve_encap_trace, + .tx_function = dummy_interface_tx, + .admin_up_down_function = geneve_interface_admin_up_down, +}; +/* *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) +{ +#if (CLIB_DEBUG > 0) + ASSERT (FIB_NODE_TYPE_GENEVE_TUNNEL == node->fn_type); +#endif + 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) + +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_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; +} + +static void +hash_set_key_copy (uword ** h, void *key, uword v) +{ + size_t ksz = hash_header (*h)->user; + void *copy = clib_mem_alloc (ksz); + clib_memcpy (copy, key, ksz); + hash_set_mem (*h, copy, v); +} + +static void +hash_unset_key_free (uword ** h, void *key) +{ + hash_pair_t *hp = hash_get_pair_mem (*h, key); + ASSERT (hp); + key = uword_to_pointer (hp->key, void *); + hash_unset_mem (*h, key); + clib_mem_free (key); +} + +static uword +vtep_addr_ref (ip46_address_t * ip) +{ + uword *vtep = ip46_address_is_ip4 (ip) ? + hash_get (geneve_main.vtep4, ip->ip4.as_u32) : + hash_get_mem (geneve_main.vtep6, &ip->ip6); + if (vtep) + return ++(*vtep); + ip46_address_is_ip4 (ip) ? + hash_set (geneve_main.vtep4, ip->ip4.as_u32, 1) : + hash_set_key_copy (&geneve_main.vtep6, &ip->ip6, 1); + return 1; +} + +static uword +vtep_addr_unref (ip46_address_t * ip) +{ + uword *vtep = ip46_address_is_ip4 (ip) ? + hash_get (geneve_main.vtep4, ip->ip4.as_u32) : + hash_get_mem (geneve_main.vtep6, &ip->ip6); + ASSERT (vtep); + if (--(*vtep) != 0) + return *vtep; + ip46_address_is_ip4 (ip) ? + hash_unset (geneve_main.vtep4, ip->ip4.as_u32) : + hash_unset_key_free (&geneve_main.vtep6, &ip->ip6); + return 0; +} + +typedef CLIB_PACKED (union + { + struct + { + fib_node_index_t mfib_entry_index; + adj_index_t mcast_adj_index; + }; u64 as_u64; + }) 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); + 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_key_copy (&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_key_free (&geneve_main.mcast_shared, remote); +} + +static inline fib_protocol_t +fib_ip_proto (bool is_ip6) +{ + return (is_ip6) ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4; +} + +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 << 8); + 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 << 8); + 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); + 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_key_copy (&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 (vec_len (vxm->free_geneve_tunnel_hw_if_indices) > 0) + { + vnet_interface_main_t *im = &vnm->interface_main; + hw_if_index = vxm->free_geneve_tunnel_hw_if_indices + [vec_len (vxm->free_geneve_tunnel_hw_if_indices) - 1]; + _vec_len (vxm->free_geneve_tunnel_hw_if_indices) -= 1; + + hi = vnet_get_hw_interface (vnm, hw_if_index); + hi->dev_instance = t - vxm->tunnels; + hi->hw_instance = hi->dev_instance; + + /* clear old stats of freed tunnel before reuse */ + sw_if_index = hi->sw_if_index; + vnet_interface_counter_lock (im); + vlib_zero_combined_counter + (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_TX], + sw_if_index); + vlib_zero_combined_counter (&im->combined_sw_if_counters + [VNET_INTERFACE_COUNTER_RX], + sw_if_index); + vlib_zero_simple_counter (&im->sw_if_counters + [VNET_INTERFACE_COUNTER_DROP], + sw_if_index); + vnet_interface_counter_unlock (im); + } + 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); + } + + 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; + u32 encap_index = !is_ip6 ? + geneve4_encap_node.index : geneve6_encap_node.index; + 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 (&t->local); + t->fib_entry_index = fib_table_entry_special_add + (t->encap_fib_index, &tun_remote_pfx, FIB_SOURCE_RR, + FIB_ENTRY_FLAG_NONE); + t->sibling_index = fib_entry_child_add + (t->fib_entry_index, FIB_NODE_TYPE_GENEVE_TUNNEL, + t - vxm->tunnels); + 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 (&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 = 0, + .frp_flags = FIB_ROUTE_PATH_LOCAL, + }; + 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, MFIB_ITF_FLAG_FORWARD); + + path.frp_sw_if_index = a->mcast_sw_if_index; + path.frp_flags = FIB_ROUTE_PATH_FLAG_NONE; + mfei = mfib_table_entry_path_update (t->encap_fib_index, + &mpfx, + MFIB_SOURCE_GENEVE, + &path, + MFIB_ITF_FLAG_ACCEPT); + + /* + * 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; + } + + /* Set geneve tunnel output node */ + hi->output_node_index = encap_index; + + 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, 0, 0, + 0); + vec_add1 (vxm->free_geneve_tunnel_hw_if_indices, 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_key_free (&vxm->geneve6_tunnel_by_key, &key6); + + if (!ip46_address_is_multicast (&t->remote)) + { + vtep_addr_unref (&t->local); + fib_entry_child_remove (t->fib_entry_index, t->sibling_index); + fib_table_entry_delete_index (t->fib_entry_index, FIB_SOURCE_RR); + } + else if (vtep_addr_unref (&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; + + 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; + 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 */ + memset (&local, 0, sizeof local); + 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 + { + 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; + } + + 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 <local-vtep-addr>" + " {remote <remote-vtep-addr>|group <mcast-vtep-addr> <intf-name>} vni <nn>" + " [encap-vrf-id <nn>] [decap-next [l2|node <name>]] [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 <interface> [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 ip geneve-bypass <interface> [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->vtep6 = hash_create_mem (0, sizeof (ip6_address_t), sizeof (uword)); + vxm->mcast_shared = hash_create_mem (0, + sizeof (ip46_address_t), + sizeof (mcast_shared_t)); + + udp_register_dst_port (vm, UDP_DST_PORT_geneve, + geneve4_input_node.index, /* is_ip4 */ 1); + udp_register_dst_port (vm, UDP_DST_PORT_geneve6, + geneve6_input_node.index, /* is_ip4 */ 0); + + 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/vnet/geneve/geneve.h b/src/vnet/geneve/geneve.h new file mode 100644 index 00000000000..2976043ed0f --- /dev/null +++ b/src/vnet/geneve/geneve.h @@ -0,0 +1,213 @@ +/* + * 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 <vppinfra/error.h> +#include <vppinfra/hash.h> +#include <vnet/vnet.h> +#include <vnet/ip/ip.h> +#include <vnet/l2/l2_input.h> +#include <vnet/l2/l2_output.h> +#include <vnet/l2/l2_bd.h> +#include <vnet/ethernet/ethernet.h> +#include <vnet/geneve/geneve_packet.h> +#include <vnet/ip/ip4_packet.h> +#include <vnet/ip/ip6_packet.h> +#include <vnet/udp/udp.h> +#include <vnet/dpo/dpo.h> +#include <vnet/adj/adj_types.h> + +#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 +{ + /* 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; +} 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 <vnet/geneve/geneve_error.def> +#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 */ + uword *vtep4; /* local ip4 VTEPs keyed on their ip4 addr */ + uword *vtep6; /* local ip6 VTEPs keyed on their ip6 addr */ + + /* mcast shared info */ + uword *mcast_shared; /* keyed on mcast ip46 addr */ + + /* Free vlib hw_if_indices */ + u32 *free_geneve_tunnel_hw_if_indices; + + /* 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; +} geneve_main_t; + +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; +} 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/vnet/geneve/geneve_api.c b/src/vnet/geneve/geneve_api.c new file mode 100644 index 00000000000..1239bfc24c0 --- /dev/null +++ b/src/vnet/geneve/geneve_api.c @@ -0,0 +1,237 @@ +/* + * 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 <vnet/vnet.h> +#include <vlibmemory/api.h> + +#include <vnet/interface.h> +#include <vnet/api_errno.h> +#include <vnet/feature/feature.h> +#include <vnet/geneve/geneve.h> +#include <vnet/fib/fib_table.h> + +#include <vnet/vnet_msg_enum.h> + +#define vl_typedefs /* define message structures */ +#include <vnet/vnet_all_api_h.h> +#undef vl_typedefs + +#define vl_endianfun /* define message structures */ +#include <vnet/vnet_all_api_h.h> +#undef vl_endianfun + +/* instantiate all the print functions we know about */ +#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__) +#define vl_printfun +#include <vnet/vnet_all_api_h.h> +#undef vl_printfun + +#include <vlibapi/api_helper_macros.h> + +#define foreach_vpe_api_msg \ +_(SW_INTERFACE_SET_GENEVE_BYPASS, sw_interface_set_geneve_bypass) \ +_(GENEVE_ADD_DEL_TUNNEL, geneve_add_del_tunnel) \ +_(GENEVE_TUNNEL_DUMP, geneve_tunnel_dump) + +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->is_ipv6, + .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), + .remote = to_ip46 (mp->is_ipv6, mp->remote_address), + .local = to_ip46 (mp->is_ipv6, mp->local_address), + }; + + /* 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 send_geneve_tunnel_details + (geneve_tunnel_t * t, unix_shared_memory_queue_t * q, 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)); + memset (rmp, 0, sizeof (*rmp)); + rmp->_vl_msg_id = ntohs (VL_API_GENEVE_TUNNEL_DETAILS); + if (is_ipv6) + { + memcpy (rmp->src_address, t->local.ip6.as_u8, 16); + memcpy (rmp->dst_address, t->remote.ip6.as_u8, 16); + rmp->encap_vrf_id = htonl (im6->fibs[t->encap_fib_index].ft_table_id); + } + else + { + memcpy (rmp->src_address, t->local.ip4.as_u8, 4); + memcpy (rmp->dst_address, t->remote.ip4.as_u8, 4); + rmp->encap_vrf_id = htonl (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->is_ipv6 = is_ipv6; + rmp->context = context; + + vl_msg_api_send_shmem (q, (u8 *) & rmp); +} + +static void vl_api_geneve_tunnel_dump_t_handler + (vl_api_geneve_tunnel_dump_t * mp) +{ + unix_shared_memory_queue_t *q; + geneve_main_t *vxm = &geneve_main; + geneve_tunnel_t *t; + u32 sw_if_index; + + q = vl_api_client_index_to_input_queue (mp->client_index); + if (q == 0) + { + 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, q, 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, q, mp->context); + } +} + +/* + * vpe_api_hookup + * Add vpe's API message handlers to the table. + * vlib has alread mapped shared memory and + * added the client registration handlers. + * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process() + */ +#define vl_msg_name_crc_list +#include <vnet/vnet_all_api_h.h> +#undef vl_msg_name_crc_list + +static void +setup_message_id_table (api_main_t * am) +{ +#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id); + foreach_vl_msg_name_crc_geneve; +#undef _ +} + +static clib_error_t * +geneve_api_hookup (vlib_main_t * vm) +{ + api_main_t *am = &api_main; + +#define _(N,n) \ + vl_msg_api_set_handlers(VL_API_##N, #n, \ + vl_api_##n##_t_handler, \ + vl_noop_handler, \ + vl_api_##n##_t_endian, \ + vl_api_##n##_t_print, \ + sizeof(vl_api_##n##_t), 1); + foreach_vpe_api_msg; +#undef _ + + am->api_trace_cfg[VL_API_GENEVE_ADD_DEL_TUNNEL].size += 16 * sizeof (u32); + + /* + * Set up the (msg_name, crc, message-id) table + */ + setup_message_id_table (am); + + return 0; +} + +VLIB_API_INIT_FUNCTION (geneve_api_hookup); + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/vnet/geneve/geneve_error.def b/src/vnet/geneve/geneve_error.def new file mode 100644 index 00000000000..73abc8ddbf1 --- /dev/null +++ b/src/vnet/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/vnet/geneve/geneve_packet.h b/src/vnet/geneve/geneve_packet.h new file mode 100644 index 00000000000..87e5225f755 --- /dev/null +++ b/src/vnet/geneve/geneve_packet.h @@ -0,0 +1,180 @@ +/* + * 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 + +typedef struct +{ + u16 opt_class; + u8 type; + u8 res:3; + u8 length:5; + 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 +{ + u8 ver:2; + u8 opt_len:6; + u8 oam_frame:1; + u8 critical_options:1; + u8 res1:6; + u16 protocol; + u32 vni:24; + u8 res2; + geneve_options_t opts[]; +} geneve_header_t; + +static inline u32 +vnet_get_geneve_vni (geneve_header_t * h) +{ + return (clib_net_to_host_u32 (h->vni) >> 8); +} + +static inline void +vnet_set_geneve_vni (geneve_header_t * h, u32 vni) +{ + h->vni = clib_host_to_net_u32 (vni << 8); +} + +static inline u8 +vnet_get_geneve_version (geneve_header_t * h) +{ + return (clib_net_to_host_u32 (h->ver) >> 30); +} + +static inline void +vnet_set_geneve_version (geneve_header_t * h, u8 version) +{ + h->ver = clib_host_to_net_u32 (version << 30); +} + +static inline u8 +vnet_get_geneve_options_len (geneve_header_t * h) +{ + return (clib_net_to_host_u32 (h->opt_len) >> 24); +} + +static inline void +vnet_set_geneve_options_len (geneve_header_t * h, u8 len) +{ + h->opt_len = clib_host_to_net_u32 (len << 24); +} + +static inline u8 +vnet_get_geneve_oamframe_bit (geneve_header_t * h) +{ + return (clib_net_to_host_u32 (h->oam_frame) >> 23); +} + +static inline void +vnet_set_geneve_oamframe_bit (geneve_header_t * h, u8 oam) +{ + h->oam_frame = clib_host_to_net_u32 (oam << 23); +} + +static inline u8 +vnet_get_geneve_critical_bit (geneve_header_t * h) +{ + return (clib_net_to_host_u32 (h->critical_options) >> 22); +} + +static inline void +vnet_set_geneve_critical_bit (geneve_header_t * h, u8 critical_opts) +{ + h->critical_options = clib_host_to_net_u32 (critical_opts << 22); +} + +static inline u16 +vnet_get_geneve_protocol (geneve_header_t * h) +{ + return clib_net_to_host_u32 (h->protocol); +} + +static inline void +vnet_set_geneve_protocol (geneve_header_t * h, u16 protocol) +{ + h->protocol = clib_host_to_net_u32 (protocol); +} +#endif + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/vnet/mfib/mfib_types.h b/src/vnet/mfib/mfib_types.h index 50aede044ec..ec68c1a5873 100644 --- a/src/vnet/mfib/mfib_types.h +++ b/src/vnet/mfib/mfib_types.h @@ -169,6 +169,7 @@ typedef enum mfib_source_t_ MFIB_SOURCE_GTPU, MFIB_SOURCE_VXLAN_GPE, MFIB_SOURCE_RR, + MFIB_SOURCE_GENEVE, MFIB_SOURCE_DEFAULT_ROUTE, } mfib_source_t; @@ -182,6 +183,7 @@ typedef enum mfib_source_t_ [MFIB_SOURCE_GTPU] = "GTPU", \ [MFIB_SOURCE_VXLAN_GPE] = "VXLAN-GPE", \ [MFIB_SOURCE_RR] = "Recursive-resolution", \ + [MFIB_SOURCE_GENEVE] = "Geneve", \ [MFIB_SOURCE_DEFAULT_ROUTE] = "Default Route", \ } diff --git a/src/vnet/udp/udp.h b/src/vnet/udp/udp.h index a9a62c27bb3..dae611cc418 100644 --- a/src/vnet/udp/udp.h +++ b/src/vnet/udp/udp.h @@ -90,7 +90,8 @@ _ (4739, ipfix) \ _ (4789, vxlan) \ _ (4789, vxlan6) \ _ (4790, VXLAN_GPE) \ -_ (6633, vpath_3) +_ (6633, vpath_3) \ +_ (6081, geneve) #define foreach_udp6_dst_port \ @@ -102,7 +103,8 @@ _ (3785, bfd_echo6) \ _ (4341, lisp_gpe6) \ _ (4342, lisp_cp6) \ _ (4790, VXLAN6_GPE) \ -_ (6633, vpath6_3) +_ (6633, vpath6_3) \ +_ (6081, geneve6) typedef enum { diff --git a/src/vnet/vnet_all_api_h.h b/src/vnet/vnet_all_api_h.h index 0b225340460..c926d0e0408 100644 --- a/src/vnet/vnet_all_api_h.h +++ b/src/vnet/vnet_all_api_h.h @@ -41,6 +41,7 @@ #include <vnet/ip/ip.api.h> #include <vnet/unix/tap.api.h> #include <vnet/vxlan/vxlan.api.h> +#include <vnet/geneve/geneve.api.h> #include <vnet/lldp/lldp.api.h> #include <vnet/vxlan-gpe/vxlan_gpe.api.h> #include <vnet/bfd/bfd.api.h> diff --git a/src/vpp/api/custom_dump.c b/src/vpp/api/custom_dump.c index 8063d680d6e..ecb0ab172f7 100644 --- a/src/vpp/api/custom_dump.c +++ b/src/vpp/api/custom_dump.c @@ -26,6 +26,7 @@ #include <vnet/l2/l2_input.h> #include <vnet/srv6/sr.h> #include <vnet/vxlan-gpe/vxlan_gpe.h> +#include <vnet/geneve/geneve.h> #include <vnet/classify/policer_classify.h> #include <vnet/policer/xlate.h> #include <vnet/policer/policer.h> @@ -229,6 +230,26 @@ static void *vl_api_sw_interface_set_vxlan_bypass_t_print FINISH; } +static void *vl_api_sw_interface_set_geneve_bypass_t_print + (vl_api_sw_interface_set_geneve_bypass_t * mp, void *handle) +{ + u8 *s; + + s = format (0, "SCRIPT: sw_interface_set_geneve_bypass "); + + s = format (s, "sw_if_index %d ", ntohl (mp->sw_if_index)); + + if (mp->is_ipv6) + s = format (s, "ip6 "); + + if (mp->enable) + s = format (s, "enable "); + else + s = format (s, "disable "); + + FINISH; +} + static void *vl_api_sw_interface_set_l2_xconnect_t_print (vl_api_sw_interface_set_l2_xconnect_t * mp, void *handle) { @@ -1495,6 +1516,50 @@ static void *vl_api_vxlan_tunnel_dump_t_print FINISH; } +static void *vl_api_geneve_add_del_tunnel_t_print + (vl_api_geneve_add_del_tunnel_t * mp, void *handle) +{ + u8 *s; + s = format (0, "SCRIPT: geneve_add_del_tunnel "); + + ip46_address_t local = to_ip46 (mp->is_ipv6, mp->local_address); + ip46_address_t remote = to_ip46 (mp->is_ipv6, mp->remote_address); + + u8 is_grp = ip46_address_is_multicast (&remote); + char *remote_name = is_grp ? "group" : "dst"; + + s = format (s, "src %U ", format_ip46_address, &local, IP46_TYPE_ANY); + s = format (s, "%s %U ", remote_name, format_ip46_address, + &remote, IP46_TYPE_ANY); + + if (is_grp) + s = format (s, "mcast_sw_if_index %d ", ntohl (mp->mcast_sw_if_index)); + + if (mp->encap_vrf_id) + s = format (s, "encap-vrf-id %d ", ntohl (mp->encap_vrf_id)); + + s = format (s, "decap-next %d ", ntohl (mp->decap_next_index)); + + s = format (s, "vni %d ", ntohl (mp->vni)); + + if (mp->is_add == 0) + s = format (s, "del "); + + FINISH; +} + +static void *vl_api_geneve_tunnel_dump_t_print + (vl_api_geneve_tunnel_dump_t * mp, void *handle) +{ + u8 *s; + + s = format (0, "SCRIPT: geneve_tunnel_dump "); + + s = format (s, "sw_if_index %d ", ntohl (mp->sw_if_index)); + + FINISH; +} + static void *vl_api_gre_add_del_tunnel_t_print (vl_api_gre_add_del_tunnel_t * mp, void *handle) { @@ -3065,6 +3130,7 @@ _(SW_INTERFACE_SET_TABLE, sw_interface_set_table) \ _(SW_INTERFACE_SET_MPLS_ENABLE, sw_interface_set_mpls_enable) \ _(SW_INTERFACE_SET_VPATH, sw_interface_set_vpath) \ _(SW_INTERFACE_SET_VXLAN_BYPASS, sw_interface_set_vxlan_bypass) \ +_(SW_INTERFACE_SET_GENEVE_BYPASS, sw_interface_set_geneve_bypass) \ _(TAP_CONNECT, tap_connect) \ _(TAP_MODIFY, tap_modify) \ _(TAP_DELETE, tap_delete) \ @@ -3118,6 +3184,8 @@ _(L2TPV3_SET_LOOKUP_KEY, l2tpv3_set_lookup_key) \ _(SW_IF_L2TPV3_TUNNEL_DUMP, sw_if_l2tpv3_tunnel_dump) \ _(VXLAN_ADD_DEL_TUNNEL, vxlan_add_del_tunnel) \ _(VXLAN_TUNNEL_DUMP, vxlan_tunnel_dump) \ +_(GENEVE_ADD_DEL_TUNNEL, geneve_add_del_tunnel) \ +_(GENEVE_TUNNEL_DUMP, geneve_tunnel_dump) \ _(GRE_ADD_DEL_TUNNEL, gre_add_del_tunnel) \ _(GRE_TUNNEL_DUMP, gre_tunnel_dump) \ _(L2_FIB_CLEAR_TABLE, l2_fib_clear_table) \ diff --git a/src/vpp/api/vpe.api b/src/vpp/api/vpe.api index d68beae13d3..b8e581891df 100644 --- a/src/vpp/api/vpe.api +++ b/src/vpp/api/vpe.api @@ -26,6 +26,7 @@ * IP APIs: see .../src/vnet/ip/{ip.api, ip_api.c} * TAP APIs: see .../src/vnet/unix/{tap.api, tap_api.c} * VXLAN APIs: see .../src/vnet/vxlan/{vxlan.api, vxlan_api.c} + * GENEVE APIs: see .../src/vnet/geneve/{geneve.api, geneve_api.c} * LLDP APIs: see .../src/vnet/lldp/{lldp.api, lldp_api.c} * AF-PACKET APIs: see ... /vnet/devices/af_packet/{af_packet.api, af_packet_api.c} * NETMAP APIs: see ... /src/vnet/devices/netmap/{netmap.api, netmap_api.c} diff --git a/test/patches/scapy-2.3.3/enable_geneve.patch b/test/patches/scapy-2.3.3/enable_geneve.patch new file mode 100644 index 00000000000..4e91b219d3e --- /dev/null +++ b/test/patches/scapy-2.3.3/enable_geneve.patch @@ -0,0 +1,13 @@ +diff --git a/scapy/config.py b/scapy/config.py +index cc97c6d..887b639 100755 +--- a/scapy/config.py ++++ b/scapy/config.py +@@ -439,7 +439,7 @@ debug_tls:When 1, print some TLS session secrets when they are computed. + "mobileip", "netbios", "netflow", "ntp", "ppp", "pptp", + "radius", "rip", "rtp", "skinny", "smb", "snmp", + "tftp", "x509", "bluetooth", "dhcp6", "llmnr", +- "sctp", "vrrp", "ipsec", "lltd", "vxlan", "eap"] ++ "sctp", "vrrp", "ipsec", "lltd", "vxlan", "eap", "geneve"] + contribs = dict() + crypto_valid = isCryptographyValid() + crypto_valid_advanced = isCryptographyAdvanced() diff --git a/test/patches/scapy-2.3.3/geneve.patch b/test/patches/scapy-2.3.3/geneve.patch new file mode 100644 index 00000000000..92752f1196e --- /dev/null +++ b/test/patches/scapy-2.3.3/geneve.patch @@ -0,0 +1,56 @@ +diff --git a/scapy/layers/geneve.py b/scapy/layers/geneve.py +new file mode 100644 +index 0000000..e2ca888 +--- /dev/null ++++ b/scapy/layers/geneve.py +@@ -0,0 +1,50 @@ ++#! /usr/bin/env python ++# (GENEVE): ++# A Framework for Overlaying Virtualized Layer 2 Networks over Layer 3 Networks ++ ++from scapy.packet import Packet, bind_layers ++from scapy.layers.l2 import Ether ++from scapy.layers.inet import IP, UDP ++from scapy.layers.inet6 import IPv6 ++from scapy.fields import FlagsField, XByteField, ThreeBytesField, \ ++ ConditionalField, ShortField, ByteEnumField, X3BytesField ++ ++# ++# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ ++# |Ver| Opt Len |O|C| Rsvd. | Protocol Type | ++# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ ++# | Virtual Network Identifier (VNI) | Reserved | ++# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ ++# | Variable Length Options | ++# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ ++# ++ ++class GENEVE(Packet): ++ name = "GENEVE" ++ ++ fields_desc = [ ++ # INIT = ver + optlen + o + c + rsvd (all zeros) ++ ShortField("init", 0x0), ++ # PROTOCOL is a 2-bytes field ++ ShortField("protocol", 0x6558), ++ ThreeBytesField("vni", 0), ++ XByteField("reserved2", 0), ++ ] ++ ++ def mysummary(self): ++ return self.sprintf("GENEVE (vni=%GENEVE.vni%)") ++ ++bind_layers(UDP, GENEVE, dport=6081) # RFC standard port ++bind_layers(UDP, GENEVE, dport=6081) # New IANA assigned port for use with NSH ++bind_layers(UDP, GENEVE, dport=8472) # Linux implementation port ++# By default, set both ports to the RFC standard ++bind_layers(UDP, GENEVE, sport=6081, dport=6081) ++ ++bind_layers(GENEVE, Ether) ++bind_layers(GENEVE, IP, NextProtocol=1) ++bind_layers(GENEVE, IPv6, NextProtocol=2) ++bind_layers(GENEVE, Ether, flags=4, NextProtocol=0) ++bind_layers(GENEVE, IP, flags=4, NextProtocol=1) ++bind_layers(GENEVE, IPv6, flags=4, NextProtocol=2) ++bind_layers(GENEVE, Ether, flags=4, NextProtocol=3) ++ diff --git a/test/test_geneve.py b/test/test_geneve.py new file mode 100644 index 00000000000..c32302c7384 --- /dev/null +++ b/test/test_geneve.py @@ -0,0 +1,234 @@ +#!/usr/bin/env python + +import socket +from util import ip4n_range +import unittest +from framework import VppTestCase, VppTestRunner +from template_bd import BridgeDomain + +from scapy.layers.l2 import Ether +from scapy.layers.inet import IP, UDP +from scapy.layers.geneve import GENEVE +from scapy.utils import atol + + +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 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_ip4n + for dest_ip4n in ip4n_range(next_hop_address, ip_range_start, + ip_range_end): + # add host route so dest_ip4n will not be resolved + cls.vapi.ip_add_del_route(dest_ip4n, 32, next_hop_address) + r = cls.vapi.geneve_add_del_tunnel( + local_addr=cls.pg0.local_ip4n, + remote_addr=dest_ip4n, + vni=vni) + cls.vapi.sw_interface_set_l2_bridge(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 = 2000 + 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_addr=cls.pg0.local_ip4n, + remote_addr=cls.mcast_ip4n, + mcast_sw_if_index=1, + vni=vni, + is_add=is_add) + if r.sw_if_index == 0xffffffff: + raise "bad sw_if_index" + + @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 = 200 + ip_range_start = 10 + ip_range_end = ip_range_start + n_distinct_dst_tunnels + for dest_ip4n in ip4n_range(cls.mcast_ip4n, ip_range_start, + ip_range_end): + vni = bytearray(dest_ip4n)[3] + cls.vapi.geneve_add_del_tunnel( + local_addr=cls.pg0.local_ip4n, + remote_addr=dest_ip4n, + mcast_sw_if_index=1, + vni=vni, + is_add=is_add) + + @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_ip4n = socket.inet_pton(socket.AF_INET, cls.mcast_ip4) + iplong = atol(cls.mcast_ip4) + cls.mcast_mac = "01:00:5e:%02x:%02x:%02x" % ( + (iplong >> 16) & 0x7F, (iplong >> 8) & 0xFF, iplong & 0xFF) + + # Create GENEVE VTEP on VPP pg0, and put geneve_tunnel0 and pg1 + # into BD. + cls.single_tunnel_bd = 1 + r = cls.vapi.geneve_add_del_tunnel( + local_addr=cls.pg0.local_ip4n, + remote_addr=cls.pg0.remote_ip4n, + vni=cls.single_tunnel_bd) + cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index, + bd_id=cls.single_tunnel_bd) + cls.vapi.sw_interface_set_l2_bridge(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_addr=cls.pg0.local_ip4n, + remote_addr=cls.mcast_ip4n, + mcast_sw_if_index=1, + vni=cls.mcast_flood_bd) + cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index, + bd_id=cls.mcast_flood_bd) + cls.vapi.sw_interface_set_l2_bridge(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(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() + if not self.vpp_dead: + 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")) + + +if __name__ == '__main__': + unittest.main(testRunner=VppTestRunner) diff --git a/test/vpp_papi_provider.py b/test/vpp_papi_provider.py index f9f54da2606..c3127f84022 100644 --- a/test/vpp_papi_provider.py +++ b/test/vpp_papi_provider.py @@ -385,6 +385,38 @@ class VppPapiProvider(object): 'decap_next_index': decap_next_index, 'vni': vni}) + def geneve_add_del_tunnel( + self, + local_addr, + remote_addr, + mcast_sw_if_index=0xFFFFFFFF, + is_add=1, + is_ipv6=0, + encap_vrf_id=0, + decap_next_index=0xFFFFFFFF, + vni=0): + """ + + :param remote_addr: + :param local_addr: + :param is_add: (Default value = 1) + :param is_ipv6: (Default value = 0) + :param encap_vrf_id: (Default value = 0) + :param decap_next_index: (Default value = 0xFFFFFFFF) + :param mcast_sw_if_index: (Default value = 0xFFFFFFFF) + :param vni: (Default value = 0) + + """ + return self.api(self.papi.geneve_add_del_tunnel, + {'is_add': is_add, + 'is_ipv6': is_ipv6, + 'local_address': local_addr, + 'remote_address': remote_addr, + 'mcast_sw_if_index': mcast_sw_if_index, + 'encap_vrf_id': encap_vrf_id, + 'decap_next_index': decap_next_index, + 'vni': vni}) + def bridge_domain_add_del(self, bd_id, flood=1, uu_flood=1, forward=1, learn=1, arp_term=0, is_add=1): """Create/delete bridge domain. |