Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * decap.c: vxlan tunnel decap packet processing |
| 3 | * |
| 4 | * Copyright (c) 2013 Cisco and/or its affiliates. |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at: |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include <vlib/vlib.h> |
| 19 | #include <vnet/pg/pg.h> |
| 20 | #include <vnet/vxlan/vxlan.h> |
| 21 | |
John Lo | 13e3d45 | 2016-08-09 19:20:51 -0400 | [diff] [blame] | 22 | vlib_node_registration_t vxlan4_input_node; |
| 23 | vlib_node_registration_t vxlan6_input_node; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 24 | |
| 25 | typedef struct { |
| 26 | u32 next_index; |
| 27 | u32 tunnel_index; |
| 28 | u32 error; |
| 29 | u32 vni; |
| 30 | } vxlan_rx_trace_t; |
| 31 | |
| 32 | static u8 * format_vxlan_rx_trace (u8 * s, va_list * args) |
| 33 | { |
| 34 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 35 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
| 36 | vxlan_rx_trace_t * t = va_arg (*args, vxlan_rx_trace_t *); |
| 37 | |
| 38 | if (t->tunnel_index != ~0) |
| 39 | { |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 40 | s = format (s, "VXLAN decap from vxlan_tunnel%d vni %d next %d error %d", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 41 | t->tunnel_index, t->vni, t->next_index, t->error); |
| 42 | } |
| 43 | else |
| 44 | { |
Eyal Bari | 0f4b184 | 2018-04-12 12:39:51 +0300 | [diff] [blame] | 45 | s = format (s, "VXLAN decap error - tunnel for vni %d does not exist", |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 46 | t->vni); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 47 | } |
| 48 | return s; |
| 49 | } |
| 50 | |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 51 | always_inline u32 |
Eyal Bari | 0fa5678 | 2018-06-04 12:25:05 +0300 | [diff] [blame] | 52 | buf_fib_index (vlib_buffer_t *b, u32 is_ip4) |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 53 | { |
Eyal Bari | 0fa5678 | 2018-06-04 12:25:05 +0300 | [diff] [blame] | 54 | u32 sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_TX]; |
| 55 | if (sw_if_index != (u32) ~ 0) |
| 56 | return sw_if_index; |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 57 | |
Eyal Bari | fb66301 | 2017-10-19 15:27:51 +0300 | [diff] [blame] | 58 | u32 * fib_index_by_sw_if_index = is_ip4 ? |
| 59 | ip4_main.fib_index_by_sw_if_index : ip6_main.fib_index_by_sw_if_index; |
Eyal Bari | 0fa5678 | 2018-06-04 12:25:05 +0300 | [diff] [blame] | 60 | sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX]; |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 61 | |
Eyal Bari | 0fa5678 | 2018-06-04 12:25:05 +0300 | [diff] [blame] | 62 | return vec_elt (fib_index_by_sw_if_index, sw_if_index); |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 63 | } |
| 64 | |
Eyal Bari | dd47eca | 2018-07-08 08:15:56 +0300 | [diff] [blame] | 65 | typedef vxlan4_tunnel_key_t last_tunnel_cache4; |
Eyal Bari | 0f4b184 | 2018-04-12 12:39:51 +0300 | [diff] [blame] | 66 | |
Eyal Bari | 0f4b184 | 2018-04-12 12:39:51 +0300 | [diff] [blame] | 67 | always_inline vxlan_tunnel_t * |
| 68 | vxlan4_find_tunnel (vxlan_main_t * vxm, last_tunnel_cache4 * cache, |
Eyal Bari | 0fa5678 | 2018-06-04 12:25:05 +0300 | [diff] [blame] | 69 | u32 fib_index, ip4_header_t * ip4_0, vxlan_header_t * vxlan0, |
Eyal Bari | 0f4b184 | 2018-04-12 12:39:51 +0300 | [diff] [blame] | 70 | vxlan_tunnel_t ** stats_t0) |
| 71 | { |
| 72 | /* Make sure VXLAN tunnel exist according to packet SIP and VNI */ |
Eyal Bari | dd47eca | 2018-07-08 08:15:56 +0300 | [diff] [blame] | 73 | vxlan4_tunnel_key_t key4 = { |
| 74 | .key = { |
| 75 | [0] = ip4_0->src_address.as_u32, |
| 76 | [1] = (((u64) fib_index) << 32) | vxlan0->vni_reserved, |
| 77 | } |
Eyal Bari | 0f4b184 | 2018-04-12 12:39:51 +0300 | [diff] [blame] | 78 | }; |
| 79 | |
Eyal Bari | dd47eca | 2018-07-08 08:15:56 +0300 | [diff] [blame] | 80 | if (PREDICT_FALSE (clib_bihash_key_compare_16_8 (key4.key, cache->key) == 0)) |
Eyal Bari | 0f4b184 | 2018-04-12 12:39:51 +0300 | [diff] [blame] | 81 | { |
Eyal Bari | dd47eca | 2018-07-08 08:15:56 +0300 | [diff] [blame] | 82 | int rv = clib_bihash_search_inline_16_8 (&vxm->vxlan4_tunnel_by_key, &key4); |
| 83 | if (PREDICT_FALSE (rv != 0)) |
Eyal Bari | 0f4b184 | 2018-04-12 12:39:51 +0300 | [diff] [blame] | 84 | return 0; |
| 85 | |
Eyal Bari | dd47eca | 2018-07-08 08:15:56 +0300 | [diff] [blame] | 86 | *cache = key4; |
Eyal Bari | 0f4b184 | 2018-04-12 12:39:51 +0300 | [diff] [blame] | 87 | } |
Eyal Bari | dd47eca | 2018-07-08 08:15:56 +0300 | [diff] [blame] | 88 | vxlan_tunnel_t * t0 = pool_elt_at_index (vxm->tunnels, cache->value); |
Eyal Bari | 0fa5678 | 2018-06-04 12:25:05 +0300 | [diff] [blame] | 89 | |
Eyal Bari | 0f4b184 | 2018-04-12 12:39:51 +0300 | [diff] [blame] | 90 | /* Validate VXLAN tunnel SIP against packet DIP */ |
| 91 | if (PREDICT_TRUE (ip4_0->dst_address.as_u32 == t0->src.ip4.as_u32)) |
| 92 | *stats_t0 = t0; |
| 93 | else |
| 94 | { |
| 95 | /* try multicast */ |
| 96 | if (PREDICT_TRUE (!ip4_address_is_multicast (&ip4_0->dst_address))) |
| 97 | return 0; |
| 98 | |
Eyal Bari | dd47eca | 2018-07-08 08:15:56 +0300 | [diff] [blame] | 99 | key4.key[0] = ip4_0->dst_address.as_u32; |
Eyal Bari | 0f4b184 | 2018-04-12 12:39:51 +0300 | [diff] [blame] | 100 | /* Make sure mcast VXLAN tunnel exist by packet DIP and VNI */ |
Eyal Bari | dd47eca | 2018-07-08 08:15:56 +0300 | [diff] [blame] | 101 | int rv = clib_bihash_search_inline_16_8 (&vxm->vxlan4_tunnel_by_key, &key4); |
| 102 | if (PREDICT_FALSE (rv != 0)) |
Eyal Bari | 0f4b184 | 2018-04-12 12:39:51 +0300 | [diff] [blame] | 103 | return 0; |
Eyal Bari | dd47eca | 2018-07-08 08:15:56 +0300 | [diff] [blame] | 104 | |
| 105 | *stats_t0 = pool_elt_at_index (vxm->tunnels, key4.value); |
Eyal Bari | 0f4b184 | 2018-04-12 12:39:51 +0300 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | return t0; |
| 109 | } |
| 110 | |
Eyal Bari | 0fa5678 | 2018-06-04 12:25:05 +0300 | [diff] [blame] | 111 | typedef vxlan6_tunnel_key_t last_tunnel_cache6; |
| 112 | |
Eyal Bari | 0f4b184 | 2018-04-12 12:39:51 +0300 | [diff] [blame] | 113 | always_inline vxlan_tunnel_t * |
| 114 | vxlan6_find_tunnel (vxlan_main_t * vxm, last_tunnel_cache6 * cache, |
Eyal Bari | 0fa5678 | 2018-06-04 12:25:05 +0300 | [diff] [blame] | 115 | u32 fib_index, ip6_header_t * ip6_0, vxlan_header_t * vxlan0, |
Eyal Bari | 0f4b184 | 2018-04-12 12:39:51 +0300 | [diff] [blame] | 116 | vxlan_tunnel_t ** stats_t0) |
| 117 | { |
| 118 | /* Make sure VXLAN tunnel exist according to packet SIP and VNI */ |
Eyal Bari | 0fa5678 | 2018-06-04 12:25:05 +0300 | [diff] [blame] | 119 | |
| 120 | vxlan6_tunnel_key_t key6 = { |
| 121 | .key = { |
| 122 | [0] = ip6_0->src_address.as_u64[0], |
| 123 | [1] = ip6_0->src_address.as_u64[1], |
Eyal Bari | dd47eca | 2018-07-08 08:15:56 +0300 | [diff] [blame] | 124 | [2] = (((u64) fib_index) << 32) | vxlan0->vni_reserved, |
Eyal Bari | 0fa5678 | 2018-06-04 12:25:05 +0300 | [diff] [blame] | 125 | } |
Eyal Bari | 0f4b184 | 2018-04-12 12:39:51 +0300 | [diff] [blame] | 126 | }; |
| 127 | |
Eyal Bari | dd47eca | 2018-07-08 08:15:56 +0300 | [diff] [blame] | 128 | if (PREDICT_FALSE (clib_bihash_key_compare_24_8 (key6.key, cache->key) == 0)) |
Eyal Bari | 0f4b184 | 2018-04-12 12:39:51 +0300 | [diff] [blame] | 129 | { |
Eyal Bari | dd47eca | 2018-07-08 08:15:56 +0300 | [diff] [blame] | 130 | int rv = clib_bihash_search_inline_24_8 (&vxm->vxlan6_tunnel_by_key, &key6); |
Eyal Bari | 0fa5678 | 2018-06-04 12:25:05 +0300 | [diff] [blame] | 131 | if (PREDICT_FALSE (rv != 0)) |
Eyal Bari | 0f4b184 | 2018-04-12 12:39:51 +0300 | [diff] [blame] | 132 | return 0; |
| 133 | |
Eyal Bari | 0fa5678 | 2018-06-04 12:25:05 +0300 | [diff] [blame] | 134 | *cache = key6; |
Eyal Bari | 0f4b184 | 2018-04-12 12:39:51 +0300 | [diff] [blame] | 135 | } |
Eyal Bari | 0fa5678 | 2018-06-04 12:25:05 +0300 | [diff] [blame] | 136 | vxlan_tunnel_t * t0 = pool_elt_at_index (vxm->tunnels, cache->value); |
Eyal Bari | 0f4b184 | 2018-04-12 12:39:51 +0300 | [diff] [blame] | 137 | |
| 138 | /* Validate VXLAN tunnel SIP against packet DIP */ |
| 139 | if (PREDICT_TRUE (ip6_address_is_equal (&ip6_0->dst_address, &t0->src.ip6))) |
| 140 | *stats_t0 = t0; |
| 141 | else |
| 142 | { |
| 143 | /* try multicast */ |
| 144 | if (PREDICT_TRUE (!ip6_address_is_multicast (&ip6_0->dst_address))) |
| 145 | return 0; |
| 146 | |
Eyal Bari | 0f4b184 | 2018-04-12 12:39:51 +0300 | [diff] [blame] | 147 | /* Make sure mcast VXLAN tunnel exist by packet DIP and VNI */ |
Eyal Bari | 0fa5678 | 2018-06-04 12:25:05 +0300 | [diff] [blame] | 148 | key6.key[0] = ip6_0->dst_address.as_u64[0]; |
| 149 | key6.key[1] = ip6_0->dst_address.as_u64[1]; |
Eyal Bari | dd47eca | 2018-07-08 08:15:56 +0300 | [diff] [blame] | 150 | int rv = clib_bihash_search_inline_24_8 (&vxm->vxlan6_tunnel_by_key, &key6); |
Eyal Bari | 0fa5678 | 2018-06-04 12:25:05 +0300 | [diff] [blame] | 151 | if (PREDICT_FALSE (rv != 0)) |
Eyal Bari | 0f4b184 | 2018-04-12 12:39:51 +0300 | [diff] [blame] | 152 | return 0; |
Eyal Bari | 0fa5678 | 2018-06-04 12:25:05 +0300 | [diff] [blame] | 153 | |
| 154 | *stats_t0 = pool_elt_at_index (vxm->tunnels, key6.value); |
Eyal Bari | 0f4b184 | 2018-04-12 12:39:51 +0300 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | return t0; |
| 158 | } |
| 159 | |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 160 | always_inline uword |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 161 | vxlan_input (vlib_main_t * vm, |
| 162 | vlib_node_runtime_t * node, |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 163 | vlib_frame_t * from_frame, |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 164 | u32 is_ip4) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 165 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 166 | vxlan_main_t * vxm = &vxlan_main; |
| 167 | vnet_main_t * vnm = vxm->vnet_main; |
| 168 | vnet_interface_main_t * im = &vnm->interface_main; |
Eyal Bari | 0f4b184 | 2018-04-12 12:39:51 +0300 | [diff] [blame] | 169 | vlib_combined_counter_main_t * rx_counter = im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX; |
| 170 | vlib_combined_counter_main_t * drop_counter = im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_DROP; |
Eyal Bari | dd47eca | 2018-07-08 08:15:56 +0300 | [diff] [blame] | 171 | last_tunnel_cache4 last4; |
Eyal Bari | 0fa5678 | 2018-06-04 12:25:05 +0300 | [diff] [blame] | 172 | last_tunnel_cache6 last6; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 173 | u32 pkts_decapsulated = 0; |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 174 | u32 thread_index = vlib_get_thread_index(); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 175 | |
Dave Barach | f9c231e | 2016-08-05 10:10:18 -0400 | [diff] [blame] | 176 | if (is_ip4) |
Eyal Bari | dd47eca | 2018-07-08 08:15:56 +0300 | [diff] [blame] | 177 | memset (&last4, 0xff, sizeof last4); |
Dave Barach | f9c231e | 2016-08-05 10:10:18 -0400 | [diff] [blame] | 178 | else |
Eyal Bari | 0fa5678 | 2018-06-04 12:25:05 +0300 | [diff] [blame] | 179 | memset (&last6, 0xff, sizeof last6); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 180 | |
Eyal Bari | fb66301 | 2017-10-19 15:27:51 +0300 | [diff] [blame] | 181 | u32 next_index = node->cached_next_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 182 | |
Eyal Bari | fb66301 | 2017-10-19 15:27:51 +0300 | [diff] [blame] | 183 | u32 * from = vlib_frame_vector_args (from_frame); |
| 184 | u32 n_left_from = from_frame->n_vectors; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 185 | |
| 186 | while (n_left_from > 0) |
| 187 | { |
Eyal Bari | fb66301 | 2017-10-19 15:27:51 +0300 | [diff] [blame] | 188 | u32 * to_next, n_left_to_next; |
Eyal Bari | 0f4b184 | 2018-04-12 12:39:51 +0300 | [diff] [blame] | 189 | vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); |
Eyal Bari | fb66301 | 2017-10-19 15:27:51 +0300 | [diff] [blame] | 190 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 191 | while (n_left_from >= 4 && n_left_to_next >= 2) |
| 192 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 193 | /* Prefetch next iteration. */ |
| 194 | { |
| 195 | vlib_buffer_t * p2, * p3; |
| 196 | |
| 197 | p2 = vlib_get_buffer (vm, from[2]); |
| 198 | p3 = vlib_get_buffer (vm, from[3]); |
| 199 | |
| 200 | vlib_prefetch_buffer_header (p2, LOAD); |
| 201 | vlib_prefetch_buffer_header (p3, LOAD); |
| 202 | |
| 203 | CLIB_PREFETCH (p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD); |
| 204 | CLIB_PREFETCH (p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD); |
| 205 | } |
| 206 | |
Eyal Bari | 0f4b184 | 2018-04-12 12:39:51 +0300 | [diff] [blame] | 207 | u32 bi0 = to_next[0] = from[0]; |
| 208 | u32 bi1 = to_next[1] = from[1]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 209 | from += 2; |
| 210 | to_next += 2; |
| 211 | n_left_to_next -= 2; |
| 212 | n_left_from -= 2; |
| 213 | |
Eyal Bari | 0f4b184 | 2018-04-12 12:39:51 +0300 | [diff] [blame] | 214 | vlib_buffer_t * b0, * b1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 215 | b0 = vlib_get_buffer (vm, bi0); |
| 216 | b1 = vlib_get_buffer (vm, bi1); |
| 217 | |
| 218 | /* udp leaves current_data pointing at the vxlan header */ |
Eyal Bari | fb66301 | 2017-10-19 15:27:51 +0300 | [diff] [blame] | 219 | void * cur0 = vlib_buffer_get_current (b0); |
| 220 | void * cur1 = vlib_buffer_get_current (b1); |
| 221 | vxlan_header_t * vxlan0 = cur0; |
| 222 | vxlan_header_t * vxlan1 = cur1; |
| 223 | |
| 224 | ip4_header_t * ip4_0, * ip4_1; |
| 225 | ip6_header_t * ip6_0, * ip6_1; |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 226 | if (is_ip4) { |
Eyal Bari | fb66301 | 2017-10-19 15:27:51 +0300 | [diff] [blame] | 227 | ip4_0 = cur0 - sizeof(udp_header_t) - sizeof(ip4_header_t); |
| 228 | ip4_1 = cur1 - sizeof(udp_header_t) - sizeof(ip4_header_t); |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 229 | } else { |
Eyal Bari | fb66301 | 2017-10-19 15:27:51 +0300 | [diff] [blame] | 230 | ip6_0 = cur0 - sizeof(udp_header_t) - sizeof(ip6_header_t); |
| 231 | ip6_1 = cur1 - sizeof(udp_header_t) - sizeof(ip6_header_t); |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 232 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 233 | |
Eyal Bari | 0f4b184 | 2018-04-12 12:39:51 +0300 | [diff] [blame] | 234 | /* pop vxlan */ |
Eyal Bari | fb66301 | 2017-10-19 15:27:51 +0300 | [diff] [blame] | 235 | vlib_buffer_advance (b0, sizeof *vxlan0); |
| 236 | vlib_buffer_advance (b1, sizeof *vxlan1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 237 | |
Eyal Bari | 0fa5678 | 2018-06-04 12:25:05 +0300 | [diff] [blame] | 238 | u32 fi0 = buf_fib_index(b0, is_ip4); |
| 239 | u32 fi1 = buf_fib_index(b1, is_ip4); |
| 240 | |
Eyal Bari | 0f4b184 | 2018-04-12 12:39:51 +0300 | [diff] [blame] | 241 | vxlan_tunnel_t * t0, * stats_t0; |
| 242 | vxlan_tunnel_t * t1, * stats_t1; |
| 243 | if (is_ip4) |
| 244 | { |
Eyal Bari | 0fa5678 | 2018-06-04 12:25:05 +0300 | [diff] [blame] | 245 | t0 = vxlan4_find_tunnel (vxm, &last4, fi0, ip4_0, vxlan0, &stats_t0); |
| 246 | t1 = vxlan4_find_tunnel (vxm, &last4, fi1, ip4_1, vxlan1, &stats_t1); |
Eyal Bari | 0f4b184 | 2018-04-12 12:39:51 +0300 | [diff] [blame] | 247 | } |
| 248 | else |
| 249 | { |
Eyal Bari | 0fa5678 | 2018-06-04 12:25:05 +0300 | [diff] [blame] | 250 | t0 = vxlan6_find_tunnel (vxm, &last6, fi0, ip6_0, vxlan0, &stats_t0); |
| 251 | t1 = vxlan6_find_tunnel (vxm, &last6, fi1, ip6_1, vxlan1, &stats_t1); |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 252 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 253 | |
Eyal Bari | fb66301 | 2017-10-19 15:27:51 +0300 | [diff] [blame] | 254 | u32 len0 = vlib_buffer_length_in_chain (vm, b0); |
Eyal Bari | fb66301 | 2017-10-19 15:27:51 +0300 | [diff] [blame] | 255 | u32 len1 = vlib_buffer_length_in_chain (vm, b1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 256 | |
Eyal Bari | 0f4b184 | 2018-04-12 12:39:51 +0300 | [diff] [blame] | 257 | u32 next0, next1; |
| 258 | u8 error0 = 0, error1 = 0; |
| 259 | /* Validate VXLAN tunnel encap-fib index agaist packet */ |
Eyal Bari | 0fa5678 | 2018-06-04 12:25:05 +0300 | [diff] [blame] | 260 | if (PREDICT_FALSE (t0 == 0 || vxlan0->flags != VXLAN_FLAGS_I)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 261 | { |
Eyal Bari | 0f4b184 | 2018-04-12 12:39:51 +0300 | [diff] [blame] | 262 | next0 = VXLAN_INPUT_NEXT_DROP; |
| 263 | |
| 264 | if (t0 != 0 && vxlan0->flags != VXLAN_FLAGS_I) |
| 265 | { |
| 266 | error0 = VXLAN_ERROR_BAD_FLAGS; |
| 267 | vlib_increment_combined_counter |
| 268 | (drop_counter, thread_index, stats_t0->sw_if_index, 1, len0); |
| 269 | } |
| 270 | else |
| 271 | error0 = VXLAN_ERROR_NO_SUCH_TUNNEL; |
| 272 | b0->error = node->errors[error0]; |
| 273 | } |
| 274 | else |
| 275 | { |
| 276 | next0 = t0->decap_next_index; |
| 277 | |
| 278 | /* Required to make the l2 tag push / pop code work on l2 subifs */ |
| 279 | if (PREDICT_TRUE(next0 == VXLAN_INPUT_NEXT_L2_INPUT)) |
| 280 | vnet_update_l2_len (b0); |
| 281 | |
| 282 | /* Set packet input sw_if_index to unicast VXLAN tunnel for learning */ |
| 283 | vnet_buffer(b0)->sw_if_index[VLIB_RX] = t0->sw_if_index; |
| 284 | vlib_increment_combined_counter |
| 285 | (rx_counter, thread_index, stats_t0->sw_if_index, 1, len0); |
| 286 | pkts_decapsulated++; |
| 287 | } |
| 288 | |
| 289 | /* Validate VXLAN tunnel encap-fib index agaist packet */ |
Eyal Bari | 0fa5678 | 2018-06-04 12:25:05 +0300 | [diff] [blame] | 290 | if (PREDICT_FALSE (t1 == 0 || vxlan1->flags != VXLAN_FLAGS_I)) |
Eyal Bari | 0f4b184 | 2018-04-12 12:39:51 +0300 | [diff] [blame] | 291 | { |
| 292 | next1 = VXLAN_INPUT_NEXT_DROP; |
| 293 | |
| 294 | if (t1 != 0 && vxlan1->flags != VXLAN_FLAGS_I) |
| 295 | { |
| 296 | error1 = VXLAN_ERROR_BAD_FLAGS; |
| 297 | vlib_increment_combined_counter |
| 298 | (drop_counter, thread_index, stats_t1->sw_if_index, 1, len1); |
| 299 | } |
| 300 | else |
| 301 | error1 = VXLAN_ERROR_NO_SUCH_TUNNEL; |
| 302 | b1->error = node->errors[error1]; |
| 303 | } |
| 304 | else |
| 305 | { |
| 306 | next1 = t1->decap_next_index; |
| 307 | |
| 308 | /* Required to make the l2 tag push / pop code work on l2 subifs */ |
| 309 | if (PREDICT_TRUE(next1 == VXLAN_INPUT_NEXT_L2_INPUT)) |
| 310 | vnet_update_l2_len (b1); |
| 311 | |
| 312 | /* Set packet input sw_if_index to unicast VXLAN tunnel for learning */ |
| 313 | vnet_buffer(b1)->sw_if_index[VLIB_RX] = t1->sw_if_index; |
| 314 | pkts_decapsulated++; |
| 315 | |
| 316 | vlib_increment_combined_counter |
| 317 | (rx_counter, thread_index, stats_t1->sw_if_index, 1, len1); |
| 318 | } |
| 319 | |
| 320 | if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) |
| 321 | { |
| 322 | vxlan_rx_trace_t *tr = vlib_add_trace (vm, node, b0, sizeof (*tr)); |
| 323 | tr->next_index = next0; |
| 324 | tr->error = error0; |
| 325 | tr->tunnel_index = t0 == 0 ? ~0 : t0 - vxm->tunnels; |
| 326 | tr->vni = vnet_get_vni (vxlan0); |
| 327 | } |
| 328 | if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED)) |
| 329 | { |
| 330 | vxlan_rx_trace_t *tr = vlib_add_trace (vm, node, b1, sizeof (*tr)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 331 | tr->next_index = next1; |
| 332 | tr->error = error1; |
Eyal Bari | 0f4b184 | 2018-04-12 12:39:51 +0300 | [diff] [blame] | 333 | tr->tunnel_index = t1 == 0 ? ~0 : t1 - vxm->tunnels; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 334 | tr->vni = vnet_get_vni (vxlan1); |
| 335 | } |
| 336 | |
| 337 | vlib_validate_buffer_enqueue_x2 (vm, node, next_index, |
| 338 | to_next, n_left_to_next, |
| 339 | bi0, bi1, next0, next1); |
| 340 | } |
| 341 | |
| 342 | while (n_left_from > 0 && n_left_to_next > 0) |
| 343 | { |
Eyal Bari | 0f4b184 | 2018-04-12 12:39:51 +0300 | [diff] [blame] | 344 | u32 bi0 = to_next[0] = from[0]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 345 | from += 1; |
| 346 | to_next += 1; |
| 347 | n_left_from -= 1; |
| 348 | n_left_to_next -= 1; |
| 349 | |
Eyal Bari | 0f4b184 | 2018-04-12 12:39:51 +0300 | [diff] [blame] | 350 | vlib_buffer_t * b0 = vlib_get_buffer (vm, bi0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 351 | |
| 352 | /* udp leaves current_data pointing at the vxlan header */ |
Eyal Bari | fb66301 | 2017-10-19 15:27:51 +0300 | [diff] [blame] | 353 | void * cur0 = vlib_buffer_get_current (b0); |
| 354 | vxlan_header_t * vxlan0 = cur0; |
Eyal Bari | 0f4b184 | 2018-04-12 12:39:51 +0300 | [diff] [blame] | 355 | ip4_header_t * ip4_0; |
| 356 | ip6_header_t * ip6_0; |
Eyal Bari | fb66301 | 2017-10-19 15:27:51 +0300 | [diff] [blame] | 357 | if (is_ip4) |
| 358 | ip4_0 = cur0 -sizeof(udp_header_t) - sizeof(ip4_header_t); |
| 359 | else |
| 360 | ip6_0 = cur0 -sizeof(udp_header_t) - sizeof(ip6_header_t); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 361 | |
| 362 | /* pop (ip, udp, vxlan) */ |
Eyal Bari | fb66301 | 2017-10-19 15:27:51 +0300 | [diff] [blame] | 363 | vlib_buffer_advance (b0, sizeof(*vxlan0)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 364 | |
Eyal Bari | 0fa5678 | 2018-06-04 12:25:05 +0300 | [diff] [blame] | 365 | u32 fi0 = buf_fib_index(b0, is_ip4); |
| 366 | |
Eyal Bari | 0f4b184 | 2018-04-12 12:39:51 +0300 | [diff] [blame] | 367 | vxlan_tunnel_t * t0, * stats_t0; |
| 368 | if (is_ip4) |
Eyal Bari | 0fa5678 | 2018-06-04 12:25:05 +0300 | [diff] [blame] | 369 | t0 = vxlan4_find_tunnel (vxm, &last4, fi0, ip4_0, vxlan0, &stats_t0); |
Eyal Bari | 0f4b184 | 2018-04-12 12:39:51 +0300 | [diff] [blame] | 370 | else |
Eyal Bari | 0fa5678 | 2018-06-04 12:25:05 +0300 | [diff] [blame] | 371 | t0 = vxlan6_find_tunnel (vxm, &last6, fi0, ip6_0, vxlan0, &stats_t0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 372 | |
Eyal Bari | 0f4b184 | 2018-04-12 12:39:51 +0300 | [diff] [blame] | 373 | uword len0 = vlib_buffer_length_in_chain (vm, b0); |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 374 | |
Eyal Bari | 0f4b184 | 2018-04-12 12:39:51 +0300 | [diff] [blame] | 375 | u32 next0; |
| 376 | u8 error0 = 0; |
| 377 | /* Validate VXLAN tunnel encap-fib index agaist packet */ |
Eyal Bari | 0fa5678 | 2018-06-04 12:25:05 +0300 | [diff] [blame] | 378 | if (PREDICT_FALSE (t0 == 0 || vxlan0->flags != VXLAN_FLAGS_I)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 379 | { |
Eyal Bari | 0f4b184 | 2018-04-12 12:39:51 +0300 | [diff] [blame] | 380 | next0 = VXLAN_INPUT_NEXT_DROP; |
| 381 | |
| 382 | if (t0 != 0 && vxlan0->flags != VXLAN_FLAGS_I) |
| 383 | { |
| 384 | error0 = VXLAN_ERROR_BAD_FLAGS; |
| 385 | vlib_increment_combined_counter |
| 386 | (drop_counter, thread_index, stats_t0->sw_if_index, 1, len0); |
| 387 | } |
| 388 | else |
| 389 | error0 = VXLAN_ERROR_NO_SUCH_TUNNEL; |
| 390 | b0->error = node->errors[error0]; |
| 391 | } |
| 392 | else |
| 393 | { |
| 394 | next0 = t0->decap_next_index; |
| 395 | |
| 396 | /* Required to make the l2 tag push / pop code work on l2 subifs */ |
| 397 | if (PREDICT_TRUE(next0 == VXLAN_INPUT_NEXT_L2_INPUT)) |
| 398 | vnet_update_l2_len (b0); |
| 399 | |
| 400 | /* Set packet input sw_if_index to unicast VXLAN tunnel for learning */ |
| 401 | vnet_buffer(b0)->sw_if_index[VLIB_RX] = t0->sw_if_index; |
| 402 | pkts_decapsulated++; |
| 403 | |
| 404 | vlib_increment_combined_counter |
| 405 | (rx_counter, thread_index, stats_t0->sw_if_index, 1, len0); |
| 406 | } |
| 407 | |
| 408 | if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) |
| 409 | { |
| 410 | vxlan_rx_trace_t *tr |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 411 | = vlib_add_trace (vm, node, b0, sizeof (*tr)); |
| 412 | tr->next_index = next0; |
| 413 | tr->error = error0; |
Eyal Bari | 0f4b184 | 2018-04-12 12:39:51 +0300 | [diff] [blame] | 414 | tr->tunnel_index = t0 == 0 ? ~0 : t0 - vxm->tunnels; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 415 | tr->vni = vnet_get_vni (vxlan0); |
| 416 | } |
| 417 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, |
| 418 | to_next, n_left_to_next, |
| 419 | bi0, next0); |
| 420 | } |
| 421 | |
| 422 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 423 | } |
Eyal Bari | fb66301 | 2017-10-19 15:27:51 +0300 | [diff] [blame] | 424 | /* Do we still need this now that tunnel tx stats is kept? */ |
| 425 | u32 node_idx = is_ip4 ? vxlan4_input_node.index : vxlan6_input_node.index; |
| 426 | vlib_node_increment_counter (vm, node_idx, VXLAN_ERROR_DECAPSULATED, |
| 427 | pkts_decapsulated); |
| 428 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 429 | return from_frame->n_vectors; |
| 430 | } |
| 431 | |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 432 | static uword |
| 433 | vxlan4_input (vlib_main_t * vm, |
| 434 | vlib_node_runtime_t * node, |
| 435 | vlib_frame_t * from_frame) |
| 436 | { |
| 437 | return vxlan_input(vm, node, from_frame, /* is_ip4 */ 1); |
| 438 | } |
| 439 | |
| 440 | static uword |
| 441 | vxlan6_input (vlib_main_t * vm, |
| 442 | vlib_node_runtime_t * node, |
| 443 | vlib_frame_t * from_frame) |
| 444 | { |
| 445 | return vxlan_input(vm, node, from_frame, /* is_ip4 */ 0); |
| 446 | } |
| 447 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 448 | static char * vxlan_error_strings[] = { |
| 449 | #define vxlan_error(n,s) s, |
| 450 | #include <vnet/vxlan/vxlan_error.def> |
| 451 | #undef vxlan_error |
| 452 | #undef _ |
| 453 | }; |
| 454 | |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 455 | VLIB_REGISTER_NODE (vxlan4_input_node) = { |
| 456 | .function = vxlan4_input, |
| 457 | .name = "vxlan4-input", |
| 458 | /* Takes a vector of packets. */ |
| 459 | .vector_size = sizeof (u32), |
| 460 | |
| 461 | .n_errors = VXLAN_N_ERROR, |
| 462 | .error_strings = vxlan_error_strings, |
| 463 | |
| 464 | .n_next_nodes = VXLAN_INPUT_N_NEXT, |
| 465 | .next_nodes = { |
| 466 | #define _(s,n) [VXLAN_INPUT_NEXT_##s] = n, |
| 467 | foreach_vxlan_input_next |
| 468 | #undef _ |
| 469 | }, |
| 470 | |
| 471 | //temp .format_buffer = format_vxlan_header, |
| 472 | .format_trace = format_vxlan_rx_trace, |
| 473 | // $$$$ .unformat_buffer = unformat_vxlan_header, |
| 474 | }; |
| 475 | |
Damjan Marion | 1c80e83 | 2016-05-11 23:07:18 +0200 | [diff] [blame] | 476 | VLIB_NODE_FUNCTION_MULTIARCH (vxlan4_input_node, vxlan4_input) |
| 477 | |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 478 | VLIB_REGISTER_NODE (vxlan6_input_node) = { |
| 479 | .function = vxlan6_input, |
| 480 | .name = "vxlan6-input", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 481 | /* Takes a vector of packets. */ |
| 482 | .vector_size = sizeof (u32), |
| 483 | |
| 484 | .n_errors = VXLAN_N_ERROR, |
| 485 | .error_strings = vxlan_error_strings, |
| 486 | |
| 487 | .n_next_nodes = VXLAN_INPUT_N_NEXT, |
| 488 | .next_nodes = { |
| 489 | #define _(s,n) [VXLAN_INPUT_NEXT_##s] = n, |
| 490 | foreach_vxlan_input_next |
| 491 | #undef _ |
| 492 | }, |
| 493 | |
| 494 | //temp .format_buffer = format_vxlan_header, |
| 495 | .format_trace = format_vxlan_rx_trace, |
| 496 | // $$$$ .unformat_buffer = unformat_vxlan_header, |
| 497 | }; |
Damjan Marion | 1c80e83 | 2016-05-11 23:07:18 +0200 | [diff] [blame] | 498 | |
| 499 | VLIB_NODE_FUNCTION_MULTIARCH (vxlan6_input_node, vxlan6_input) |
| 500 | |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 501 | |
| 502 | typedef enum { |
| 503 | IP_VXLAN_BYPASS_NEXT_DROP, |
| 504 | IP_VXLAN_BYPASS_NEXT_VXLAN, |
| 505 | IP_VXLAN_BYPASS_N_NEXT, |
| 506 | } ip_vxan_bypass_next_t; |
| 507 | |
| 508 | always_inline uword |
| 509 | ip_vxlan_bypass_inline (vlib_main_t * vm, |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 510 | vlib_node_runtime_t * node, |
| 511 | vlib_frame_t * frame, |
| 512 | u32 is_ip4) |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 513 | { |
| 514 | vxlan_main_t * vxm = &vxlan_main; |
| 515 | u32 * from, * to_next, n_left_from, n_left_to_next, next_index; |
| 516 | vlib_node_runtime_t * error_node = vlib_node_get_runtime (vm, ip4_input_node.index); |
| 517 | ip4_address_t addr4; /* last IPv4 address matching a local VTEP address */ |
| 518 | ip6_address_t addr6; /* last IPv6 address matching a local VTEP address */ |
| 519 | |
| 520 | from = vlib_frame_vector_args (frame); |
| 521 | n_left_from = frame->n_vectors; |
| 522 | next_index = node->cached_next_index; |
| 523 | |
| 524 | if (node->flags & VLIB_NODE_FLAG_TRACE) |
| 525 | ip4_forward_next_trace (vm, node, frame, VLIB_TX); |
| 526 | |
| 527 | if (is_ip4) addr4.data_u32 = ~0; |
| 528 | else ip6_address_set_zero (&addr6); |
| 529 | |
| 530 | while (n_left_from > 0) |
| 531 | { |
| 532 | vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); |
| 533 | |
| 534 | while (n_left_from >= 4 && n_left_to_next >= 2) |
| 535 | { |
| 536 | vlib_buffer_t * b0, * b1; |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 537 | ip4_header_t * ip40, * ip41; |
| 538 | ip6_header_t * ip60, * ip61; |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 539 | udp_header_t * udp0, * udp1; |
| 540 | u32 bi0, ip_len0, udp_len0, flags0, next0; |
| 541 | u32 bi1, ip_len1, udp_len1, flags1, next1; |
| 542 | i32 len_diff0, len_diff1; |
| 543 | u8 error0, good_udp0, proto0; |
| 544 | u8 error1, good_udp1, proto1; |
| 545 | |
| 546 | /* Prefetch next iteration. */ |
| 547 | { |
| 548 | vlib_buffer_t * p2, * p3; |
| 549 | |
| 550 | p2 = vlib_get_buffer (vm, from[2]); |
| 551 | p3 = vlib_get_buffer (vm, from[3]); |
| 552 | |
| 553 | vlib_prefetch_buffer_header (p2, LOAD); |
| 554 | vlib_prefetch_buffer_header (p3, LOAD); |
| 555 | |
| 556 | CLIB_PREFETCH (p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD); |
| 557 | CLIB_PREFETCH (p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD); |
| 558 | } |
| 559 | |
| 560 | bi0 = to_next[0] = from[0]; |
| 561 | bi1 = to_next[1] = from[1]; |
| 562 | from += 2; |
| 563 | n_left_from -= 2; |
| 564 | to_next += 2; |
| 565 | n_left_to_next -= 2; |
| 566 | |
| 567 | b0 = vlib_get_buffer (vm, bi0); |
| 568 | b1 = vlib_get_buffer (vm, bi1); |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 569 | if (is_ip4) |
| 570 | { |
| 571 | ip40 = vlib_buffer_get_current (b0); |
| 572 | ip41 = vlib_buffer_get_current (b1); |
| 573 | } |
| 574 | else |
| 575 | { |
| 576 | ip60 = vlib_buffer_get_current (b0); |
| 577 | ip61 = vlib_buffer_get_current (b1); |
| 578 | } |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 579 | |
| 580 | /* Setup packet for next IP feature */ |
Damjan Marion | 7d98a12 | 2018-07-19 20:42:08 +0200 | [diff] [blame] | 581 | vnet_feature_next(&next0, b0); |
| 582 | vnet_feature_next(&next1, b1); |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 583 | |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 584 | if (is_ip4) |
| 585 | { |
| 586 | /* Treat IP frag packets as "experimental" protocol for now |
| 587 | until support of IP frag reassembly is implemented */ |
| 588 | proto0 = ip4_is_fragment(ip40) ? 0xfe : ip40->protocol; |
| 589 | proto1 = ip4_is_fragment(ip41) ? 0xfe : ip41->protocol; |
| 590 | } |
| 591 | else |
| 592 | { |
| 593 | proto0 = ip60->protocol; |
| 594 | proto1 = ip61->protocol; |
| 595 | } |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 596 | |
| 597 | /* Process packet 0 */ |
| 598 | if (proto0 != IP_PROTOCOL_UDP) |
| 599 | goto exit0; /* not UDP packet */ |
| 600 | |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 601 | if (is_ip4) |
| 602 | udp0 = ip4_next_header (ip40); |
| 603 | else |
| 604 | udp0 = ip6_next_header (ip60); |
| 605 | |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 606 | if (udp0->dst_port != clib_host_to_net_u16 (UDP_DST_PORT_vxlan)) |
| 607 | goto exit0; /* not VXLAN packet */ |
| 608 | |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 609 | /* Validate DIP against VTEPs*/ |
Eyal Bari | 0f4b184 | 2018-04-12 12:39:51 +0300 | [diff] [blame] | 610 | if (is_ip4) |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 611 | { |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 612 | if (addr4.as_u32 != ip40->dst_address.as_u32) |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 613 | { |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 614 | if (!hash_get (vxm->vtep4, ip40->dst_address.as_u32)) |
| 615 | goto exit0; /* no local VTEP for VXLAN packet */ |
| 616 | addr4 = ip40->dst_address; |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 617 | } |
| 618 | } |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 619 | else |
| 620 | { |
| 621 | if (!ip6_address_is_equal (&addr6, &ip60->dst_address)) |
| 622 | { |
| 623 | if (!hash_get_mem (vxm->vtep6, &ip60->dst_address)) |
| 624 | goto exit0; /* no local VTEP for VXLAN packet */ |
| 625 | addr6 = ip60->dst_address; |
| 626 | } |
| 627 | } |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 628 | |
| 629 | flags0 = b0->flags; |
Damjan Marion | 213b5aa | 2017-07-13 21:19:27 +0200 | [diff] [blame] | 630 | good_udp0 = (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0; |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 631 | |
| 632 | /* Don't verify UDP checksum for packets with explicit zero checksum. */ |
| 633 | good_udp0 |= udp0->checksum == 0; |
| 634 | |
| 635 | /* Verify UDP length */ |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 636 | if (is_ip4) |
| 637 | ip_len0 = clib_net_to_host_u16 (ip40->length); |
| 638 | else |
| 639 | ip_len0 = clib_net_to_host_u16 (ip60->payload_length); |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 640 | udp_len0 = clib_net_to_host_u16 (udp0->length); |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 641 | len_diff0 = ip_len0 - udp_len0; |
| 642 | |
| 643 | /* Verify UDP checksum */ |
| 644 | if (PREDICT_FALSE (!good_udp0)) |
| 645 | { |
Damjan Marion | 213b5aa | 2017-07-13 21:19:27 +0200 | [diff] [blame] | 646 | if ((flags0 & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED) == 0) |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 647 | { |
| 648 | if (is_ip4) |
| 649 | flags0 = ip4_tcp_udp_validate_checksum (vm, b0); |
| 650 | else |
| 651 | flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, b0); |
| 652 | good_udp0 = |
Damjan Marion | 213b5aa | 2017-07-13 21:19:27 +0200 | [diff] [blame] | 653 | (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0; |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 654 | } |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 655 | } |
| 656 | |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 657 | if (is_ip4) |
| 658 | { |
| 659 | error0 = good_udp0 ? 0 : IP4_ERROR_UDP_CHECKSUM; |
| 660 | error0 = (len_diff0 >= 0) ? error0 : IP4_ERROR_UDP_LENGTH; |
| 661 | } |
| 662 | else |
| 663 | { |
| 664 | error0 = good_udp0 ? 0 : IP6_ERROR_UDP_CHECKSUM; |
| 665 | error0 = (len_diff0 >= 0) ? error0 : IP6_ERROR_UDP_LENGTH; |
| 666 | } |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 667 | |
Eyal Bari | 0f4b184 | 2018-04-12 12:39:51 +0300 | [diff] [blame] | 668 | next0 = error0 ? |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 669 | IP_VXLAN_BYPASS_NEXT_DROP : IP_VXLAN_BYPASS_NEXT_VXLAN; |
| 670 | b0->error = error0 ? error_node->errors[error0] : 0; |
| 671 | |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 672 | /* vxlan-input node expect current at VXLAN header */ |
| 673 | if (is_ip4) |
| 674 | vlib_buffer_advance (b0, sizeof(ip4_header_t)+sizeof(udp_header_t)); |
| 675 | else |
| 676 | vlib_buffer_advance (b0, sizeof(ip6_header_t)+sizeof(udp_header_t)); |
| 677 | |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 678 | exit0: |
| 679 | /* Process packet 1 */ |
| 680 | if (proto1 != IP_PROTOCOL_UDP) |
| 681 | goto exit1; /* not UDP packet */ |
| 682 | |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 683 | if (is_ip4) |
| 684 | udp1 = ip4_next_header (ip41); |
| 685 | else |
| 686 | udp1 = ip6_next_header (ip61); |
| 687 | |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 688 | if (udp1->dst_port != clib_host_to_net_u16 (UDP_DST_PORT_vxlan)) |
| 689 | goto exit1; /* not VXLAN packet */ |
| 690 | |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 691 | /* Validate DIP against VTEPs*/ |
Eyal Bari | 0f4b184 | 2018-04-12 12:39:51 +0300 | [diff] [blame] | 692 | if (is_ip4) |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 693 | { |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 694 | if (addr4.as_u32 != ip41->dst_address.as_u32) |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 695 | { |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 696 | if (!hash_get (vxm->vtep4, ip41->dst_address.as_u32)) |
| 697 | goto exit1; /* no local VTEP for VXLAN packet */ |
| 698 | addr4 = ip41->dst_address; |
| 699 | } |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 700 | } |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 701 | else |
| 702 | { |
| 703 | if (!ip6_address_is_equal (&addr6, &ip61->dst_address)) |
| 704 | { |
| 705 | if (!hash_get_mem (vxm->vtep6, &ip61->dst_address)) |
| 706 | goto exit1; /* no local VTEP for VXLAN packet */ |
| 707 | addr6 = ip61->dst_address; |
| 708 | } |
| 709 | } |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 710 | |
| 711 | flags1 = b1->flags; |
Damjan Marion | 213b5aa | 2017-07-13 21:19:27 +0200 | [diff] [blame] | 712 | good_udp1 = (flags1 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0; |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 713 | |
| 714 | /* Don't verify UDP checksum for packets with explicit zero checksum. */ |
| 715 | good_udp1 |= udp1->checksum == 0; |
| 716 | |
| 717 | /* Verify UDP length */ |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 718 | if (is_ip4) |
| 719 | ip_len1 = clib_net_to_host_u16 (ip41->length); |
| 720 | else |
| 721 | ip_len1 = clib_net_to_host_u16 (ip61->payload_length); |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 722 | udp_len1 = clib_net_to_host_u16 (udp1->length); |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 723 | len_diff1 = ip_len1 - udp_len1; |
| 724 | |
| 725 | /* Verify UDP checksum */ |
| 726 | if (PREDICT_FALSE (!good_udp1)) |
| 727 | { |
Damjan Marion | 213b5aa | 2017-07-13 21:19:27 +0200 | [diff] [blame] | 728 | if ((flags1 & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED) == 0) |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 729 | { |
| 730 | if (is_ip4) |
| 731 | flags1 = ip4_tcp_udp_validate_checksum (vm, b1); |
| 732 | else |
| 733 | flags1 = ip6_tcp_udp_icmp_validate_checksum (vm, b1); |
| 734 | good_udp1 = |
Damjan Marion | 213b5aa | 2017-07-13 21:19:27 +0200 | [diff] [blame] | 735 | (flags1 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0; |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 736 | } |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 737 | } |
| 738 | |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 739 | if (is_ip4) |
| 740 | { |
| 741 | error1 = good_udp1 ? 0 : IP4_ERROR_UDP_CHECKSUM; |
| 742 | error1 = (len_diff1 >= 0) ? error1 : IP4_ERROR_UDP_LENGTH; |
| 743 | } |
| 744 | else |
| 745 | { |
Eyal Bari | a93ea42 | 2017-02-01 13:36:15 +0200 | [diff] [blame] | 746 | error1 = good_udp1 ? 0 : IP6_ERROR_UDP_CHECKSUM; |
| 747 | error1 = (len_diff1 >= 0) ? error1 : IP6_ERROR_UDP_LENGTH; |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 748 | } |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 749 | |
Eyal Bari | 0f4b184 | 2018-04-12 12:39:51 +0300 | [diff] [blame] | 750 | next1 = error1 ? |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 751 | IP_VXLAN_BYPASS_NEXT_DROP : IP_VXLAN_BYPASS_NEXT_VXLAN; |
| 752 | b1->error = error1 ? error_node->errors[error1] : 0; |
Eyal Bari | 0f4b184 | 2018-04-12 12:39:51 +0300 | [diff] [blame] | 753 | |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 754 | /* vxlan-input node expect current at VXLAN header */ |
| 755 | if (is_ip4) |
| 756 | vlib_buffer_advance (b1, sizeof(ip4_header_t)+sizeof(udp_header_t)); |
| 757 | else |
| 758 | vlib_buffer_advance (b1, sizeof(ip6_header_t)+sizeof(udp_header_t)); |
| 759 | |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 760 | exit1: |
| 761 | vlib_validate_buffer_enqueue_x2 (vm, node, next_index, |
| 762 | to_next, n_left_to_next, |
| 763 | bi0, bi1, next0, next1); |
| 764 | } |
| 765 | |
| 766 | while (n_left_from > 0 && n_left_to_next > 0) |
| 767 | { |
| 768 | vlib_buffer_t * b0; |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 769 | ip4_header_t * ip40; |
| 770 | ip6_header_t * ip60; |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 771 | udp_header_t * udp0; |
| 772 | u32 bi0, ip_len0, udp_len0, flags0, next0; |
| 773 | i32 len_diff0; |
| 774 | u8 error0, good_udp0, proto0; |
| 775 | |
| 776 | bi0 = to_next[0] = from[0]; |
| 777 | from += 1; |
| 778 | n_left_from -= 1; |
| 779 | to_next += 1; |
| 780 | n_left_to_next -= 1; |
| 781 | |
| 782 | b0 = vlib_get_buffer (vm, bi0); |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 783 | if (is_ip4) |
| 784 | ip40 = vlib_buffer_get_current (b0); |
| 785 | else |
| 786 | ip60 = vlib_buffer_get_current (b0); |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 787 | |
| 788 | /* Setup packet for next IP feature */ |
Damjan Marion | 7d98a12 | 2018-07-19 20:42:08 +0200 | [diff] [blame] | 789 | vnet_feature_next(&next0, b0); |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 790 | |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 791 | if (is_ip4) |
| 792 | /* Treat IP4 frag packets as "experimental" protocol for now |
| 793 | until support of IP frag reassembly is implemented */ |
| 794 | proto0 = ip4_is_fragment(ip40) ? 0xfe : ip40->protocol; |
| 795 | else |
| 796 | proto0 = ip60->protocol; |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 797 | |
| 798 | if (proto0 != IP_PROTOCOL_UDP) |
| 799 | goto exit; /* not UDP packet */ |
| 800 | |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 801 | if (is_ip4) |
| 802 | udp0 = ip4_next_header (ip40); |
| 803 | else |
| 804 | udp0 = ip6_next_header (ip60); |
| 805 | |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 806 | if (udp0->dst_port != clib_host_to_net_u16 (UDP_DST_PORT_vxlan)) |
| 807 | goto exit; /* not VXLAN packet */ |
| 808 | |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 809 | /* Validate DIP against VTEPs*/ |
Eyal Bari | 0f4b184 | 2018-04-12 12:39:51 +0300 | [diff] [blame] | 810 | if (is_ip4) |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 811 | { |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 812 | if (addr4.as_u32 != ip40->dst_address.as_u32) |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 813 | { |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 814 | if (!hash_get (vxm->vtep4, ip40->dst_address.as_u32)) |
| 815 | goto exit; /* no local VTEP for VXLAN packet */ |
| 816 | addr4 = ip40->dst_address; |
| 817 | } |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 818 | } |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 819 | else |
| 820 | { |
| 821 | if (!ip6_address_is_equal (&addr6, &ip60->dst_address)) |
| 822 | { |
| 823 | if (!hash_get_mem (vxm->vtep6, &ip60->dst_address)) |
| 824 | goto exit; /* no local VTEP for VXLAN packet */ |
| 825 | addr6 = ip60->dst_address; |
| 826 | } |
| 827 | } |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 828 | |
| 829 | flags0 = b0->flags; |
Damjan Marion | 213b5aa | 2017-07-13 21:19:27 +0200 | [diff] [blame] | 830 | good_udp0 = (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0; |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 831 | |
| 832 | /* Don't verify UDP checksum for packets with explicit zero checksum. */ |
| 833 | good_udp0 |= udp0->checksum == 0; |
| 834 | |
| 835 | /* Verify UDP length */ |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 836 | if (is_ip4) |
| 837 | ip_len0 = clib_net_to_host_u16 (ip40->length); |
| 838 | else |
| 839 | ip_len0 = clib_net_to_host_u16 (ip60->payload_length); |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 840 | udp_len0 = clib_net_to_host_u16 (udp0->length); |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 841 | len_diff0 = ip_len0 - udp_len0; |
| 842 | |
| 843 | /* Verify UDP checksum */ |
| 844 | if (PREDICT_FALSE (!good_udp0)) |
| 845 | { |
Damjan Marion | 213b5aa | 2017-07-13 21:19:27 +0200 | [diff] [blame] | 846 | if ((flags0 & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED) == 0) |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 847 | { |
| 848 | if (is_ip4) |
| 849 | flags0 = ip4_tcp_udp_validate_checksum (vm, b0); |
| 850 | else |
| 851 | flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, b0); |
| 852 | good_udp0 = |
Damjan Marion | 213b5aa | 2017-07-13 21:19:27 +0200 | [diff] [blame] | 853 | (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0; |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 854 | } |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 855 | } |
| 856 | |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 857 | if (is_ip4) |
| 858 | { |
| 859 | error0 = good_udp0 ? 0 : IP4_ERROR_UDP_CHECKSUM; |
| 860 | error0 = (len_diff0 >= 0) ? error0 : IP4_ERROR_UDP_LENGTH; |
| 861 | } |
| 862 | else |
| 863 | { |
| 864 | error0 = good_udp0 ? 0 : IP6_ERROR_UDP_CHECKSUM; |
| 865 | error0 = (len_diff0 >= 0) ? error0 : IP6_ERROR_UDP_LENGTH; |
| 866 | } |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 867 | |
Eyal Bari | 0f4b184 | 2018-04-12 12:39:51 +0300 | [diff] [blame] | 868 | next0 = error0 ? |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 869 | IP_VXLAN_BYPASS_NEXT_DROP : IP_VXLAN_BYPASS_NEXT_VXLAN; |
| 870 | b0->error = error0 ? error_node->errors[error0] : 0; |
| 871 | |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 872 | /* vxlan-input node expect current at VXLAN header */ |
| 873 | if (is_ip4) |
| 874 | vlib_buffer_advance (b0, sizeof(ip4_header_t)+sizeof(udp_header_t)); |
| 875 | else |
| 876 | vlib_buffer_advance (b0, sizeof(ip6_header_t)+sizeof(udp_header_t)); |
| 877 | |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 878 | exit: |
| 879 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, |
| 880 | to_next, n_left_to_next, |
| 881 | bi0, next0); |
| 882 | } |
| 883 | |
| 884 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 885 | } |
| 886 | |
| 887 | return frame->n_vectors; |
| 888 | } |
| 889 | |
| 890 | static uword |
| 891 | ip4_vxlan_bypass (vlib_main_t * vm, |
| 892 | vlib_node_runtime_t * node, |
| 893 | vlib_frame_t * frame) |
| 894 | { |
| 895 | return ip_vxlan_bypass_inline (vm, node, frame, /* is_ip4 */ 1); |
| 896 | } |
| 897 | |
| 898 | VLIB_REGISTER_NODE (ip4_vxlan_bypass_node) = { |
| 899 | .function = ip4_vxlan_bypass, |
| 900 | .name = "ip4-vxlan-bypass", |
| 901 | .vector_size = sizeof (u32), |
| 902 | |
| 903 | .n_next_nodes = IP_VXLAN_BYPASS_N_NEXT, |
| 904 | .next_nodes = { |
| 905 | [IP_VXLAN_BYPASS_NEXT_DROP] = "error-drop", |
| 906 | [IP_VXLAN_BYPASS_NEXT_VXLAN] = "vxlan4-input", |
| 907 | }, |
| 908 | |
| 909 | .format_buffer = format_ip4_header, |
| 910 | .format_trace = format_ip4_forward_next_trace, |
| 911 | }; |
| 912 | |
| 913 | VLIB_NODE_FUNCTION_MULTIARCH (ip4_vxlan_bypass_node,ip4_vxlan_bypass) |
| 914 | |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 915 | /* Dummy init function to get us linked in. */ |
| 916 | clib_error_t * ip4_vxlan_bypass_init (vlib_main_t * vm) |
| 917 | { return 0; } |
| 918 | |
| 919 | VLIB_INIT_FUNCTION (ip4_vxlan_bypass_init); |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 920 | |
| 921 | static uword |
| 922 | ip6_vxlan_bypass (vlib_main_t * vm, |
| 923 | vlib_node_runtime_t * node, |
| 924 | vlib_frame_t * frame) |
| 925 | { |
| 926 | return ip_vxlan_bypass_inline (vm, node, frame, /* is_ip4 */ 0); |
| 927 | } |
| 928 | |
| 929 | VLIB_REGISTER_NODE (ip6_vxlan_bypass_node) = { |
| 930 | .function = ip6_vxlan_bypass, |
| 931 | .name = "ip6-vxlan-bypass", |
| 932 | .vector_size = sizeof (u32), |
| 933 | |
| 934 | .n_next_nodes = IP_VXLAN_BYPASS_N_NEXT, |
| 935 | .next_nodes = { |
| 936 | [IP_VXLAN_BYPASS_NEXT_DROP] = "error-drop", |
| 937 | [IP_VXLAN_BYPASS_NEXT_VXLAN] = "vxlan6-input", |
| 938 | }, |
| 939 | |
| 940 | .format_buffer = format_ip6_header, |
| 941 | .format_trace = format_ip6_forward_next_trace, |
| 942 | }; |
| 943 | |
| 944 | VLIB_NODE_FUNCTION_MULTIARCH (ip6_vxlan_bypass_node,ip6_vxlan_bypass) |
| 945 | |
| 946 | /* Dummy init function to get us linked in. */ |
| 947 | clib_error_t * ip6_vxlan_bypass_init (vlib_main_t * vm) |
| 948 | { return 0; } |
| 949 | |
| 950 | VLIB_INIT_FUNCTION (ip6_vxlan_bypass_init); |
eyal bari | af86a48 | 2018-04-17 11:20:27 +0300 | [diff] [blame] | 951 | |
| 952 | #define foreach_vxlan_flow_input_next \ |
| 953 | _(DROP, "error-drop") \ |
| 954 | _(L2_INPUT, "l2-input") |
| 955 | |
| 956 | typedef enum |
| 957 | { |
| 958 | #define _(s,n) VXLAN_FLOW_NEXT_##s, |
| 959 | foreach_vxlan_flow_input_next |
| 960 | #undef _ |
| 961 | VXLAN_FLOW_N_NEXT, |
| 962 | } vxlan_flow_input_next_t; |
| 963 | |
| 964 | #define foreach_vxlan_flow_error \ |
| 965 | _(NONE, "no error") \ |
| 966 | _(IP_CHECKSUM_ERROR, "Rx ip checksum errors") \ |
| 967 | _(IP_HEADER_ERROR, "Rx ip header errors") \ |
| 968 | _(UDP_CHECKSUM_ERROR, "Rx udp checksum errors") \ |
| 969 | _(UDP_LENGTH_ERROR, "Rx udp length errors") |
| 970 | |
| 971 | typedef enum |
| 972 | { |
| 973 | #define _(f,s) VXLAN_FLOW_ERROR_##f, |
| 974 | foreach_vxlan_flow_error |
| 975 | #undef _ |
| 976 | VXLAN_FLOW_N_ERROR, |
| 977 | } vxlan_flow_error_t; |
| 978 | |
| 979 | static char *vxlan_flow_error_strings[] = { |
| 980 | #define _(n,s) s, |
| 981 | foreach_vxlan_flow_error |
| 982 | #undef _ |
| 983 | }; |
| 984 | |
| 985 | |
| 986 | static_always_inline u8 |
| 987 | vxlan_validate_udp_csum (vlib_main_t * vm, vlib_buffer_t *b) |
| 988 | { |
| 989 | u32 flags = b->flags; |
| 990 | enum { offset = sizeof(ip4_header_t) + sizeof(udp_header_t) + sizeof(vxlan_header_t), }; |
| 991 | |
| 992 | /* Verify UDP checksum */ |
| 993 | if ((flags & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED) == 0) |
| 994 | { |
| 995 | vlib_buffer_advance (b, -offset); |
| 996 | flags = ip4_tcp_udp_validate_checksum (vm, b); |
| 997 | vlib_buffer_advance (b, offset); |
| 998 | } |
| 999 | |
| 1000 | return (flags & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0; |
| 1001 | } |
| 1002 | |
| 1003 | static_always_inline u8 |
| 1004 | vxlan_check_udp_csum (vlib_main_t * vm, vlib_buffer_t *b) |
| 1005 | { |
| 1006 | ip4_vxlan_header_t * hdr = vlib_buffer_get_current(b) - sizeof *hdr; |
| 1007 | udp_header_t * udp = &hdr->udp; |
| 1008 | /* Don't verify UDP checksum for packets with explicit zero checksum. */ |
| 1009 | u8 good_csum = (b->flags & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0 || |
| 1010 | udp->checksum == 0; |
| 1011 | |
| 1012 | return !good_csum; |
| 1013 | } |
| 1014 | |
| 1015 | static_always_inline u8 |
| 1016 | vxlan_check_ip (vlib_buffer_t *b, u16 payload_len) |
| 1017 | { |
| 1018 | ip4_vxlan_header_t * hdr = vlib_buffer_get_current(b) - sizeof *hdr; |
| 1019 | u16 ip_len = clib_net_to_host_u16 (hdr->ip4.length); |
| 1020 | u16 expected = payload_len + sizeof *hdr; |
| 1021 | return ip_len > expected || hdr->ip4.ttl == 0 || hdr->ip4.ip_version_and_header_length != 0x45; |
| 1022 | } |
| 1023 | |
| 1024 | static_always_inline u8 |
| 1025 | vxlan_check_ip_udp_len (vlib_buffer_t *b) |
| 1026 | { |
| 1027 | ip4_vxlan_header_t * hdr = vlib_buffer_get_current(b) - sizeof *hdr; |
| 1028 | u16 ip_len = clib_net_to_host_u16 (hdr->ip4.length); |
| 1029 | u16 udp_len = clib_net_to_host_u16 (hdr->udp.length); |
| 1030 | return udp_len > ip_len; |
| 1031 | } |
| 1032 | |
| 1033 | static_always_inline u8 |
| 1034 | vxlan_err_code (u8 ip_err0, u8 udp_err0, u8 csum_err0) |
| 1035 | { |
| 1036 | u8 error0 = VXLAN_FLOW_ERROR_NONE; |
| 1037 | if (ip_err0) |
| 1038 | error0 = VXLAN_FLOW_ERROR_IP_HEADER_ERROR; |
| 1039 | if (udp_err0) |
| 1040 | error0 = VXLAN_FLOW_ERROR_UDP_LENGTH_ERROR; |
| 1041 | if (csum_err0) |
| 1042 | error0 = VXLAN_FLOW_ERROR_UDP_CHECKSUM_ERROR; |
| 1043 | return error0; |
| 1044 | } |
| 1045 | |
Eyal Bari | 93a6f25 | 2018-06-14 08:57:39 +0300 | [diff] [blame] | 1046 | VLIB_NODE_FN (vxlan4_flow_input_node) (vlib_main_t * vm, |
eyal bari | af86a48 | 2018-04-17 11:20:27 +0300 | [diff] [blame] | 1047 | vlib_node_runtime_t * node, |
| 1048 | vlib_frame_t * f) |
| 1049 | { |
| 1050 | enum { payload_offset = sizeof(ip4_vxlan_header_t) }; |
| 1051 | |
| 1052 | vxlan_main_t * vxm = &vxlan_main; |
| 1053 | vnet_interface_main_t * im = &vnet_main.interface_main; |
| 1054 | vlib_combined_counter_main_t * rx_counter[VXLAN_FLOW_N_NEXT] = { |
| 1055 | [VXLAN_FLOW_NEXT_DROP] = im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_DROP, |
| 1056 | [VXLAN_FLOW_NEXT_L2_INPUT] = im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX, |
| 1057 | }; |
| 1058 | u32 thread_index = vlib_get_thread_index(); |
| 1059 | |
| 1060 | u32 * from = vlib_frame_vector_args (f); |
| 1061 | u32 n_left_from = f->n_vectors; |
| 1062 | u32 next_index = VXLAN_FLOW_NEXT_L2_INPUT; |
| 1063 | |
| 1064 | while (n_left_from > 0) |
| 1065 | { |
| 1066 | u32 n_left_to_next, *to_next; |
| 1067 | |
| 1068 | vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); |
| 1069 | |
| 1070 | while (n_left_from > 3 && n_left_to_next > 3) |
| 1071 | { |
| 1072 | u32 bi0 = to_next[0] = from[0]; |
| 1073 | u32 bi1 = to_next[1] = from[1]; |
| 1074 | u32 bi2 = to_next[2] = from[2]; |
| 1075 | u32 bi3 = to_next[3] = from[3]; |
| 1076 | from+=4; |
| 1077 | n_left_from-=4; |
| 1078 | to_next+=4; |
| 1079 | n_left_to_next-=4; |
| 1080 | |
| 1081 | vlib_buffer_t * b0 = vlib_get_buffer (vm, bi0); |
| 1082 | vlib_buffer_t * b1 = vlib_get_buffer (vm, bi1); |
| 1083 | vlib_buffer_t * b2 = vlib_get_buffer (vm, bi2); |
| 1084 | vlib_buffer_t * b3 = vlib_get_buffer (vm, bi3); |
| 1085 | |
| 1086 | vlib_buffer_advance (b0, payload_offset); |
| 1087 | vlib_buffer_advance (b1, payload_offset); |
| 1088 | vlib_buffer_advance (b2, payload_offset); |
| 1089 | vlib_buffer_advance (b3, payload_offset); |
| 1090 | |
| 1091 | u16 len0 = vlib_buffer_length_in_chain (vm, b0); |
| 1092 | u16 len1 = vlib_buffer_length_in_chain (vm, b1); |
| 1093 | u16 len2 = vlib_buffer_length_in_chain (vm, b2); |
| 1094 | u16 len3 = vlib_buffer_length_in_chain (vm, b3); |
| 1095 | |
| 1096 | u32 next0 = VXLAN_FLOW_NEXT_L2_INPUT, next1 = VXLAN_FLOW_NEXT_L2_INPUT, |
| 1097 | next2 = VXLAN_FLOW_NEXT_L2_INPUT, next3 = VXLAN_FLOW_NEXT_L2_INPUT; |
| 1098 | |
| 1099 | u8 ip_err0 = vxlan_check_ip (b0, len0); |
| 1100 | u8 ip_err1 = vxlan_check_ip (b1, len1); |
| 1101 | u8 ip_err2 = vxlan_check_ip (b2, len2); |
| 1102 | u8 ip_err3 = vxlan_check_ip (b3, len3); |
| 1103 | u8 ip_err = ip_err0 | ip_err1 | ip_err2 | ip_err3; |
| 1104 | |
| 1105 | u8 udp_err0 = vxlan_check_ip_udp_len (b0); |
| 1106 | u8 udp_err1 = vxlan_check_ip_udp_len (b1); |
| 1107 | u8 udp_err2 = vxlan_check_ip_udp_len (b2); |
| 1108 | u8 udp_err3 = vxlan_check_ip_udp_len (b3); |
| 1109 | u8 udp_err = udp_err0 | udp_err1 | udp_err2 | udp_err3; |
| 1110 | |
| 1111 | u8 csum_err0 = vxlan_check_udp_csum (vm, b0); |
| 1112 | u8 csum_err1 = vxlan_check_udp_csum (vm, b1); |
| 1113 | u8 csum_err2 = vxlan_check_udp_csum (vm, b2); |
| 1114 | u8 csum_err3 = vxlan_check_udp_csum (vm, b3); |
| 1115 | u8 csum_err = csum_err0 | csum_err1 | csum_err2 | csum_err3; |
| 1116 | |
| 1117 | if (PREDICT_FALSE(csum_err)) |
| 1118 | { |
| 1119 | if (csum_err0) |
| 1120 | csum_err0 = !vxlan_validate_udp_csum (vm, b0); |
| 1121 | if (csum_err1) |
| 1122 | csum_err1 = !vxlan_validate_udp_csum (vm, b1); |
| 1123 | if (csum_err2) |
| 1124 | csum_err2 = !vxlan_validate_udp_csum (vm, b2); |
| 1125 | if (csum_err3) |
| 1126 | csum_err3 = !vxlan_validate_udp_csum (vm, b3); |
| 1127 | csum_err = csum_err0 | csum_err1 | csum_err2 | csum_err3; |
| 1128 | } |
| 1129 | |
| 1130 | if (PREDICT_FALSE(ip_err || udp_err || csum_err)) |
| 1131 | { |
| 1132 | if (ip_err0 || udp_err0 || csum_err0) |
| 1133 | { |
| 1134 | next0 = VXLAN_FLOW_NEXT_DROP; |
| 1135 | u8 error0 = vxlan_err_code (ip_err0, udp_err0, csum_err0); |
| 1136 | b0->error = node->errors[error0]; |
| 1137 | } |
| 1138 | if (ip_err1 || udp_err1 || csum_err1) |
| 1139 | { |
| 1140 | next1 = VXLAN_FLOW_NEXT_DROP; |
| 1141 | u8 error1 = vxlan_err_code (ip_err1, udp_err1, csum_err1); |
| 1142 | b1->error = node->errors[error1]; |
| 1143 | } |
| 1144 | if (ip_err2 || udp_err2 || csum_err2) |
| 1145 | { |
| 1146 | next2 = VXLAN_FLOW_NEXT_DROP; |
| 1147 | u8 error2 = vxlan_err_code (ip_err2, udp_err2, csum_err2); |
| 1148 | b2->error = node->errors[error2]; |
| 1149 | } |
| 1150 | if (ip_err3 || udp_err3 || csum_err3) |
| 1151 | { |
| 1152 | next3 = VXLAN_FLOW_NEXT_DROP; |
| 1153 | u8 error3 = vxlan_err_code (ip_err3, udp_err3, csum_err3); |
| 1154 | b3->error = node->errors[error3]; |
| 1155 | } |
| 1156 | } |
| 1157 | |
| 1158 | vnet_update_l2_len (b0); |
| 1159 | vnet_update_l2_len (b1); |
| 1160 | vnet_update_l2_len (b2); |
| 1161 | vnet_update_l2_len (b3); |
| 1162 | |
| 1163 | ASSERT (b0->flow_id != 0); |
| 1164 | ASSERT (b1->flow_id != 0); |
| 1165 | ASSERT (b2->flow_id != 0); |
| 1166 | ASSERT (b3->flow_id != 0); |
| 1167 | |
| 1168 | u32 t_index0 = b0->flow_id - vxm->flow_id_start; |
| 1169 | u32 t_index1 = b1->flow_id - vxm->flow_id_start; |
| 1170 | u32 t_index2 = b2->flow_id - vxm->flow_id_start; |
| 1171 | u32 t_index3 = b3->flow_id - vxm->flow_id_start; |
| 1172 | |
| 1173 | vxlan_tunnel_t * t0 = &vxm->tunnels[t_index0]; |
| 1174 | vxlan_tunnel_t * t1 = &vxm->tunnels[t_index1]; |
| 1175 | vxlan_tunnel_t * t2 = &vxm->tunnels[t_index2]; |
| 1176 | vxlan_tunnel_t * t3 = &vxm->tunnels[t_index3]; |
| 1177 | |
| 1178 | /* flow id consumed */ |
| 1179 | b0->flow_id = 0; |
| 1180 | b1->flow_id = 0; |
| 1181 | b2->flow_id = 0; |
| 1182 | b3->flow_id = 0; |
| 1183 | |
| 1184 | u32 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX] = t0->sw_if_index; |
| 1185 | u32 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX] = t1->sw_if_index; |
| 1186 | u32 sw_if_index2 = vnet_buffer (b2)->sw_if_index[VLIB_RX] = t2->sw_if_index; |
| 1187 | u32 sw_if_index3 = vnet_buffer (b3)->sw_if_index[VLIB_RX] = t3->sw_if_index; |
| 1188 | |
| 1189 | vlib_increment_combined_counter (rx_counter[next0], thread_index, sw_if_index0, 1, len0); |
| 1190 | vlib_increment_combined_counter (rx_counter[next1], thread_index, sw_if_index1, 1, len1); |
| 1191 | vlib_increment_combined_counter (rx_counter[next2], thread_index, sw_if_index2, 1, len2); |
| 1192 | vlib_increment_combined_counter (rx_counter[next3], thread_index, sw_if_index3, 1, len3); |
| 1193 | |
| 1194 | u32 flags = b0->flags | b1->flags | b2->flags | b3->flags; |
| 1195 | |
| 1196 | if (PREDICT_FALSE(flags & VLIB_BUFFER_IS_TRACED)) |
| 1197 | { |
| 1198 | if (b0->flags & VLIB_BUFFER_IS_TRACED) |
| 1199 | { |
| 1200 | vxlan_rx_trace_t *tr = vlib_add_trace (vm, node, b0, sizeof *tr); |
| 1201 | u8 error0 = vxlan_err_code (ip_err0, udp_err0, csum_err0); |
| 1202 | *tr = (vxlan_rx_trace_t) { |
| 1203 | .next_index = next0, .error = error0, .tunnel_index = t_index0, .vni = t0->vni }; |
| 1204 | } |
| 1205 | if (b1->flags & VLIB_BUFFER_IS_TRACED) |
| 1206 | { |
| 1207 | vxlan_rx_trace_t *tr = vlib_add_trace (vm, node, b1, sizeof *tr); |
| 1208 | u8 error1 = vxlan_err_code (ip_err1, udp_err1, csum_err1); |
| 1209 | *tr = (vxlan_rx_trace_t) { |
| 1210 | .next_index = next1, .error = error1, .tunnel_index = t_index1, .vni = t1->vni }; |
| 1211 | } |
| 1212 | if (b2->flags & VLIB_BUFFER_IS_TRACED) |
| 1213 | { |
| 1214 | vxlan_rx_trace_t *tr = vlib_add_trace (vm, node, b2, sizeof *tr); |
| 1215 | u8 error2 = vxlan_err_code (ip_err2, udp_err2, csum_err2); |
| 1216 | *tr = (vxlan_rx_trace_t) { |
| 1217 | .next_index = next2, .error = error2, .tunnel_index = t_index2, .vni = t2->vni }; |
| 1218 | } |
| 1219 | if (b3->flags & VLIB_BUFFER_IS_TRACED) |
| 1220 | { |
| 1221 | vxlan_rx_trace_t *tr = vlib_add_trace (vm, node, b3, sizeof *tr); |
| 1222 | u8 error3 = vxlan_err_code (ip_err3, udp_err3, csum_err3); |
| 1223 | *tr = (vxlan_rx_trace_t) { |
| 1224 | .next_index = next3, .error = error3, .tunnel_index = t_index3, .vni = t3->vni }; |
| 1225 | } |
| 1226 | } |
| 1227 | vlib_validate_buffer_enqueue_x4 |
| 1228 | (vm, node, next_index, to_next, n_left_to_next, |
| 1229 | bi0, bi1, bi2, bi3, next0, next1, next2, next3); |
| 1230 | } |
| 1231 | while (n_left_from > 0 && n_left_to_next > 0) |
| 1232 | { |
| 1233 | u32 bi0 = to_next[0] = from[0]; |
| 1234 | from++; |
| 1235 | n_left_from--; |
| 1236 | to_next++; |
| 1237 | n_left_to_next--; |
| 1238 | |
| 1239 | vlib_buffer_t * b0 = vlib_get_buffer (vm, bi0); |
| 1240 | vlib_buffer_advance (b0, payload_offset); |
| 1241 | |
| 1242 | u16 len0 = vlib_buffer_length_in_chain (vm, b0); |
| 1243 | u32 next0 = VXLAN_FLOW_NEXT_L2_INPUT; |
| 1244 | |
| 1245 | u8 ip_err0 = vxlan_check_ip (b0, len0); |
| 1246 | u8 udp_err0 = vxlan_check_ip_udp_len (b0); |
| 1247 | u8 csum_err0 = vxlan_check_udp_csum (vm, b0); |
| 1248 | |
| 1249 | if (csum_err0) |
| 1250 | csum_err0 = !vxlan_validate_udp_csum (vm, b0); |
| 1251 | if (ip_err0 || udp_err0 || csum_err0) |
| 1252 | { |
| 1253 | next0 = VXLAN_FLOW_NEXT_DROP; |
| 1254 | u8 error0 = vxlan_err_code (ip_err0, udp_err0, csum_err0); |
| 1255 | b0->error = node->errors[error0]; |
| 1256 | } |
| 1257 | |
| 1258 | vnet_update_l2_len (b0); |
| 1259 | |
| 1260 | ASSERT (b0->flow_id != 0); |
| 1261 | u32 t_index0 = b0->flow_id - vxm->flow_id_start; |
| 1262 | vxlan_tunnel_t * t0 = &vxm->tunnels[t_index0]; |
| 1263 | b0->flow_id = 0; |
| 1264 | |
| 1265 | u32 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX] = t0->sw_if_index; |
| 1266 | vlib_increment_combined_counter (rx_counter[next0], thread_index, sw_if_index0, 1, len0); |
| 1267 | |
| 1268 | if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) |
| 1269 | { |
| 1270 | vxlan_rx_trace_t *tr = vlib_add_trace (vm, node, b0, sizeof *tr); |
| 1271 | u8 error0 = vxlan_err_code (ip_err0, udp_err0, csum_err0); |
| 1272 | *tr = (vxlan_rx_trace_t) { |
| 1273 | .next_index = next0, .error = error0, .tunnel_index = t_index0, .vni = t0->vni }; |
| 1274 | } |
| 1275 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, |
| 1276 | to_next, n_left_to_next, |
| 1277 | bi0, next0); |
| 1278 | } |
| 1279 | |
| 1280 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 1281 | } |
| 1282 | |
| 1283 | return f->n_vectors; |
| 1284 | } |
| 1285 | |
| 1286 | /* *INDENT-OFF* */ |
| 1287 | #ifndef CLIB_MULTIARCH_VARIANT |
| 1288 | VLIB_REGISTER_NODE (vxlan4_flow_input_node) = { |
| 1289 | .name = "vxlan-flow-input", |
eyal bari | af86a48 | 2018-04-17 11:20:27 +0300 | [diff] [blame] | 1290 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 1291 | .vector_size = sizeof (u32), |
| 1292 | |
| 1293 | .format_trace = format_vxlan_rx_trace, |
| 1294 | |
| 1295 | .n_errors = VXLAN_FLOW_N_ERROR, |
| 1296 | .error_strings = vxlan_flow_error_strings, |
| 1297 | |
| 1298 | .n_next_nodes = VXLAN_FLOW_N_NEXT, |
| 1299 | .next_nodes = { |
| 1300 | #define _(s,n) [VXLAN_FLOW_NEXT_##s] = n, |
| 1301 | foreach_vxlan_flow_input_next |
| 1302 | #undef _ |
| 1303 | }, |
| 1304 | }; |
| 1305 | #endif |
| 1306 | /* *INDENT-ON* */ |