Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * gre.c: gre |
| 3 | * |
| 4 | * Copyright (c) 2012 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 <vnet/vnet.h> |
| 19 | #include <vnet/gre/gre.h> |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 20 | #include <vnet/adj/adj_midchain.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 21 | |
Benoît Ganne | 8e22054 | 2019-03-01 14:19:55 +0100 | [diff] [blame] | 22 | extern gre_main_t gre_main; |
| 23 | |
Filip Tehlar | 0fce11f | 2019-03-04 09:21:59 -0800 | [diff] [blame] | 24 | #ifndef CLIB_MARCH_VARIANT |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 25 | gre_main_t gre_main; |
| 26 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 27 | typedef struct |
| 28 | { |
| 29 | union |
| 30 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 31 | ip4_and_gre_header_t ip4_and_gre; |
| 32 | u64 as_u64[3]; |
| 33 | }; |
| 34 | } ip4_and_gre_union_t; |
| 35 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 36 | typedef struct |
| 37 | { |
| 38 | union |
| 39 | { |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 40 | ip6_and_gre_header_t ip6_and_gre; |
| 41 | u64 as_u64[3]; |
| 42 | }; |
| 43 | } ip6_and_gre_union_t; |
Filip Tehlar | 0fce11f | 2019-03-04 09:21:59 -0800 | [diff] [blame] | 44 | #endif /* CLIB_MARCH_VARIANT */ |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 45 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 46 | |
| 47 | /* Packet trace structure */ |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 48 | typedef struct |
| 49 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 50 | /* Tunnel-id / index in tunnel vector */ |
| 51 | u32 tunnel_id; |
| 52 | |
| 53 | /* pkt length */ |
| 54 | u32 length; |
| 55 | |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 56 | /* tunnel ip addresses */ |
| 57 | ip46_address_t src; |
| 58 | ip46_address_t dst; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 59 | } gre_tx_trace_t; |
| 60 | |
Benoît Ganne | 8e22054 | 2019-03-01 14:19:55 +0100 | [diff] [blame] | 61 | extern u8 *format_gre_tx_trace (u8 * s, va_list * args); |
| 62 | |
| 63 | #ifndef CLIB_MARCH_VARIANT |
| 64 | u8 * |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 65 | format_gre_tx_trace (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 66 | { |
| 67 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 68 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 69 | gre_tx_trace_t *t = va_arg (*args, gre_tx_trace_t *); |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 70 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 71 | s = format (s, "GRE: tunnel %d len %d src %U dst %U", |
John Lo | a43ccae | 2018-02-13 17:15:23 -0500 | [diff] [blame] | 72 | t->tunnel_id, t->length, |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 73 | format_ip46_address, &t->src, IP46_TYPE_ANY, |
| 74 | format_ip46_address, &t->dst, IP46_TYPE_ANY); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 75 | return s; |
| 76 | } |
| 77 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 78 | u8 * |
| 79 | format_gre_protocol (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 80 | { |
| 81 | gre_protocol_t p = va_arg (*args, u32); |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 82 | gre_main_t *gm = &gre_main; |
| 83 | gre_protocol_info_t *pi = gre_get_protocol_info (gm, p); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 84 | |
| 85 | if (pi) |
| 86 | s = format (s, "%s", pi->name); |
| 87 | else |
| 88 | s = format (s, "0x%04x", p); |
| 89 | |
| 90 | return s; |
| 91 | } |
| 92 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 93 | u8 * |
| 94 | format_gre_header_with_length (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 95 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 96 | gre_main_t *gm = &gre_main; |
| 97 | gre_header_t *h = va_arg (*args, gre_header_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 98 | u32 max_header_bytes = va_arg (*args, u32); |
| 99 | gre_protocol_t p = clib_net_to_host_u16 (h->protocol); |
Christophe Fontaine | d3c008d | 2017-10-02 18:10:54 +0200 | [diff] [blame] | 100 | u32 indent, header_bytes; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 101 | |
| 102 | header_bytes = sizeof (h[0]); |
| 103 | if (max_header_bytes != 0 && header_bytes > max_header_bytes) |
| 104 | return format (s, "gre header truncated"); |
| 105 | |
| 106 | indent = format_get_indent (s); |
| 107 | |
| 108 | s = format (s, "GRE %U", format_gre_protocol, p); |
| 109 | |
John Lo | a43ccae | 2018-02-13 17:15:23 -0500 | [diff] [blame] | 110 | if (max_header_bytes != 0 && header_bytes < max_header_bytes) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 111 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 112 | gre_protocol_info_t *pi = gre_get_protocol_info (gm, p); |
| 113 | vlib_node_t *node = vlib_get_node (gm->vlib_main, pi->node_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 114 | if (node->format_buffer) |
| 115 | s = format (s, "\n%U%U", |
| 116 | format_white_space, indent, |
| 117 | node->format_buffer, (void *) (h + 1), |
| 118 | max_header_bytes - header_bytes); |
| 119 | } |
| 120 | |
| 121 | return s; |
| 122 | } |
| 123 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 124 | u8 * |
| 125 | format_gre_header (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 126 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 127 | gre_header_t *h = va_arg (*args, gre_header_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 128 | return format (s, "%U", format_gre_header_with_length, h, 0); |
| 129 | } |
| 130 | |
| 131 | /* Returns gre protocol as an int in host byte order. */ |
| 132 | uword |
| 133 | unformat_gre_protocol_host_byte_order (unformat_input_t * input, |
| 134 | va_list * args) |
| 135 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 136 | u16 *result = va_arg (*args, u16 *); |
| 137 | gre_main_t *gm = &gre_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 138 | int i; |
| 139 | |
| 140 | /* Named type. */ |
| 141 | if (unformat_user (input, unformat_vlib_number_by_name, |
| 142 | gm->protocol_info_by_name, &i)) |
| 143 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 144 | gre_protocol_info_t *pi = vec_elt_at_index (gm->protocol_infos, i); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 145 | *result = pi->protocol; |
| 146 | return 1; |
| 147 | } |
| 148 | |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | uword |
| 153 | unformat_gre_protocol_net_byte_order (unformat_input_t * input, |
| 154 | va_list * args) |
| 155 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 156 | u16 *result = va_arg (*args, u16 *); |
| 157 | if (!unformat_user (input, unformat_gre_protocol_host_byte_order, result)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 158 | return 0; |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 159 | *result = clib_host_to_net_u16 ((u16) * result); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 160 | return 1; |
| 161 | } |
| 162 | |
| 163 | uword |
| 164 | unformat_gre_header (unformat_input_t * input, va_list * args) |
| 165 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 166 | u8 **result = va_arg (*args, u8 **); |
| 167 | gre_header_t _h, *h = &_h; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 168 | u16 p; |
| 169 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 170 | if (!unformat (input, "%U", unformat_gre_protocol_host_byte_order, &p)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 171 | return 0; |
| 172 | |
| 173 | h->protocol = clib_host_to_net_u16 (p); |
| 174 | |
| 175 | /* Add header to result. */ |
| 176 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 177 | void *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 178 | u32 n_bytes = sizeof (h[0]); |
| 179 | |
| 180 | vec_add2 (*result, p, n_bytes); |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 181 | clib_memcpy (p, h, n_bytes); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 182 | } |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 183 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 184 | return 1; |
| 185 | } |
| 186 | |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 187 | static int |
| 188 | gre_proto_from_vnet_link (vnet_link_t link) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 189 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 190 | switch (link) |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 191 | { |
| 192 | case VNET_LINK_IP4: |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 193 | return (GRE_PROTOCOL_ip4); |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 194 | case VNET_LINK_IP6: |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 195 | return (GRE_PROTOCOL_ip6); |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 196 | case VNET_LINK_MPLS: |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 197 | return (GRE_PROTOCOL_mpls_unicast); |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 198 | case VNET_LINK_ETHERNET: |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 199 | return (GRE_PROTOCOL_teb); |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 200 | case VNET_LINK_ARP: |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 201 | return (GRE_PROTOCOL_arp); |
Florin Coras | ce1b4c7 | 2017-01-26 14:25:34 -0800 | [diff] [blame] | 202 | case VNET_LINK_NSH: |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 203 | ASSERT (0); |
| 204 | break; |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 205 | } |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 206 | ASSERT (0); |
| 207 | return (GRE_PROTOCOL_ip4); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 210 | static u8 * |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 211 | gre_build_rewrite (vnet_main_t * vnm, |
| 212 | u32 sw_if_index, |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 213 | vnet_link_t link_type, const void *dst_address) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 214 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 215 | gre_main_t *gm = &gre_main; |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 216 | const ip46_address_t *dst; |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 217 | ip4_and_gre_header_t *h4; |
| 218 | ip6_and_gre_header_t *h6; |
John Lo | a43ccae | 2018-02-13 17:15:23 -0500 | [diff] [blame] | 219 | gre_header_t *gre; |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 220 | u8 *rewrite = NULL; |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 221 | gre_tunnel_t *t; |
| 222 | u32 ti; |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 223 | u8 is_ipv6; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 224 | |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 225 | dst = dst_address; |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 226 | ti = gm->tunnel_index_by_sw_if_index[sw_if_index]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 227 | |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 228 | if (~0 == ti) |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 229 | /* not one of ours */ |
| 230 | return (0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 231 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 232 | t = pool_elt_at_index (gm->tunnels, ti); |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 233 | |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 234 | is_ipv6 = t->tunnel_dst.fp_proto == FIB_PROTOCOL_IP6 ? 1 : 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 235 | |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 236 | if (!is_ipv6) |
| 237 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 238 | vec_validate (rewrite, sizeof (*h4) - 1); |
| 239 | h4 = (ip4_and_gre_header_t *) rewrite; |
John Lo | a43ccae | 2018-02-13 17:15:23 -0500 | [diff] [blame] | 240 | gre = &h4->gre; |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 241 | h4->ip4.ip_version_and_header_length = 0x45; |
| 242 | h4->ip4.ttl = 254; |
| 243 | h4->ip4.protocol = IP_PROTOCOL_GRE; |
| 244 | /* fixup ip4 header length and checksum after-the-fact */ |
| 245 | h4->ip4.src_address.as_u32 = t->tunnel_src.ip4.as_u32; |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 246 | h4->ip4.dst_address.as_u32 = dst->ip4.as_u32; |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 247 | h4->ip4.checksum = ip4_header_checksum (&h4->ip4); |
| 248 | } |
| 249 | else |
| 250 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 251 | vec_validate (rewrite, sizeof (*h6) - 1); |
| 252 | h6 = (ip6_and_gre_header_t *) rewrite; |
John Lo | a43ccae | 2018-02-13 17:15:23 -0500 | [diff] [blame] | 253 | gre = &h6->gre; |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 254 | h6->ip6.ip_version_traffic_class_and_flow_label = |
| 255 | clib_host_to_net_u32 (6 << 28); |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 256 | h6->ip6.hop_limit = 255; |
| 257 | h6->ip6.protocol = IP_PROTOCOL_GRE; |
| 258 | /* fixup ip6 header length and checksum after-the-fact */ |
| 259 | h6->ip6.src_address.as_u64[0] = t->tunnel_src.ip6.as_u64[0]; |
| 260 | h6->ip6.src_address.as_u64[1] = t->tunnel_src.ip6.as_u64[1]; |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 261 | h6->ip6.dst_address.as_u64[0] = dst->ip6.as_u64[0]; |
| 262 | h6->ip6.dst_address.as_u64[1] = dst->ip6.as_u64[1]; |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 263 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 264 | |
John Lo | a43ccae | 2018-02-13 17:15:23 -0500 | [diff] [blame] | 265 | if (PREDICT_FALSE (t->type == GRE_TUNNEL_TYPE_ERSPAN)) |
| 266 | { |
| 267 | gre->protocol = clib_host_to_net_u16 (GRE_PROTOCOL_erspan); |
| 268 | gre->flags_and_version = clib_host_to_net_u16 (GRE_FLAGS_SEQUENCE); |
| 269 | } |
| 270 | else |
| 271 | gre->protocol = |
| 272 | clib_host_to_net_u16 (gre_proto_from_vnet_link (link_type)); |
| 273 | |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 274 | return (rewrite); |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 275 | } |
| 276 | |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 277 | #define is_v4_packet(_h) ((*(u8*) _h) & 0xF0) == 0x40 |
| 278 | |
Neale Ranns | db14f5a | 2018-01-29 10:43:33 -0800 | [diff] [blame] | 279 | static void |
| 280 | gre4_fixup (vlib_main_t * vm, |
Neale Ranns | 960eeea | 2019-12-02 23:28:50 +0000 | [diff] [blame] | 281 | const ip_adjacency_t * adj, vlib_buffer_t * b0, const void *data) |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 282 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 283 | ip4_header_t *ip0; |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 284 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 285 | ip0 = vlib_buffer_get_current (b0); |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 286 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 287 | /* Fixup the checksum and len fields in the GRE tunnel encap |
| 288 | * that was applied at the midchain node */ |
| 289 | ip0->length = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)); |
| 290 | ip0->checksum = ip4_header_checksum (ip0); |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 291 | } |
| 292 | |
Neale Ranns | db14f5a | 2018-01-29 10:43:33 -0800 | [diff] [blame] | 293 | static void |
| 294 | gre6_fixup (vlib_main_t * vm, |
Neale Ranns | 960eeea | 2019-12-02 23:28:50 +0000 | [diff] [blame] | 295 | const ip_adjacency_t * adj, vlib_buffer_t * b0, const void *data) |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 296 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 297 | ip6_header_t *ip0; |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 298 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 299 | ip0 = vlib_buffer_get_current (b0); |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 300 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 301 | /* Fixup the payload length field in the GRE tunnel encap that was applied |
| 302 | * at the midchain node */ |
| 303 | ip0->payload_length = |
Ole Troan | da6e11b | 2018-05-23 11:21:42 +0200 | [diff] [blame] | 304 | clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0) - |
| 305 | sizeof (*ip0)); |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | void |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 309 | gre_update_adj (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai) |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 310 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 311 | gre_main_t *gm = &gre_main; |
| 312 | gre_tunnel_t *t; |
Neale Ranns | 521a8d7 | 2018-12-06 13:46:49 +0000 | [diff] [blame] | 313 | adj_flags_t af; |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 314 | u8 is_ipv6; |
Neale Ranns | 521a8d7 | 2018-12-06 13:46:49 +0000 | [diff] [blame] | 315 | u32 ti; |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 316 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 317 | ti = gm->tunnel_index_by_sw_if_index[sw_if_index]; |
| 318 | t = pool_elt_at_index (gm->tunnels, ti); |
| 319 | is_ipv6 = t->tunnel_dst.fp_proto == FIB_PROTOCOL_IP6 ? 1 : 0; |
Neale Ranns | 521a8d7 | 2018-12-06 13:46:49 +0000 | [diff] [blame] | 320 | af = ADJ_FLAG_MIDCHAIN_IP_STACK; |
| 321 | |
| 322 | if (VNET_LINK_ETHERNET == adj_get_link_type (ai)) |
| 323 | af |= ADJ_FLAG_MIDCHAIN_NO_COUNT; |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 324 | |
John Lo | a43ccae | 2018-02-13 17:15:23 -0500 | [diff] [blame] | 325 | adj_nbr_midchain_update_rewrite |
Neale Ranns | 521a8d7 | 2018-12-06 13:46:49 +0000 | [diff] [blame] | 326 | (ai, !is_ipv6 ? gre4_fixup : gre6_fixup, NULL, af, |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 327 | gre_build_rewrite (vnm, sw_if_index, adj_get_link_type (ai), |
| 328 | &t->tunnel_dst.fp_addr)); |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 329 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 330 | gre_tunnel_stack (ai); |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 331 | } |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 332 | |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 333 | adj_walk_rc_t |
| 334 | mgre_mk_complete_walk (adj_index_t ai, void *data) |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 335 | { |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 336 | mgre_walk_ctx_t *ctx = data; |
| 337 | adj_midchain_fixup_t f; |
| 338 | |
| 339 | f = (ctx->t->tunnel_dst.fp_proto == FIB_PROTOCOL_IP4 ? |
| 340 | gre4_fixup : gre6_fixup); |
| 341 | |
| 342 | adj_nbr_midchain_update_rewrite |
| 343 | (ai, f, NULL, ADJ_FLAG_MIDCHAIN_IP_STACK, |
| 344 | gre_build_rewrite (vnet_get_main (), |
| 345 | ctx->t->sw_if_index, |
| 346 | adj_get_link_type (ai), |
| 347 | &nhrp_entry_get_nh (ctx->ne)->fp_addr)); |
| 348 | |
| 349 | nhrp_entry_adj_stack (ctx->ne, ai); |
| 350 | |
| 351 | return (ADJ_WALK_RC_CONTINUE); |
| 352 | } |
| 353 | |
| 354 | adj_walk_rc_t |
| 355 | mgre_mk_incomplete_walk (adj_index_t ai, void *data) |
| 356 | { |
| 357 | gre_tunnel_t *t = data; |
| 358 | adj_midchain_fixup_t f; |
| 359 | |
| 360 | f = (t->tunnel_dst.fp_proto == FIB_PROTOCOL_IP4 ? gre4_fixup : gre6_fixup); |
| 361 | |
| 362 | adj_nbr_midchain_update_rewrite (ai, f, NULL, ADJ_FLAG_NONE, NULL); |
| 363 | |
| 364 | adj_midchain_delegate_unstack (ai); |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 365 | |
| 366 | return (ADJ_WALK_RC_CONTINUE); |
| 367 | } |
| 368 | |
| 369 | void |
| 370 | mgre_update_adj (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai) |
| 371 | { |
| 372 | gre_main_t *gm = &gre_main; |
| 373 | ip_adjacency_t *adj; |
| 374 | nhrp_entry_t *ne; |
| 375 | gre_tunnel_t *t; |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 376 | u32 ti; |
| 377 | |
| 378 | adj = adj_get (ai); |
| 379 | ti = gm->tunnel_index_by_sw_if_index[sw_if_index]; |
| 380 | t = pool_elt_at_index (gm->tunnels, ti); |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 381 | |
| 382 | ne = nhrp_entry_find (sw_if_index, &adj->sub_type.nbr.next_hop); |
| 383 | |
| 384 | if (NULL == ne) |
| 385 | // no NHRP entry to provide the next-hop |
| 386 | return; |
| 387 | |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 388 | mgre_walk_ctx_t ctx = { |
| 389 | .t = t, |
| 390 | .ne = ne |
| 391 | }; |
| 392 | adj_nbr_walk_nh (sw_if_index, |
| 393 | adj->ia_nh_proto, |
| 394 | &adj->sub_type.nbr.next_hop, mgre_mk_complete_walk, &ctx); |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 395 | } |
Filip Tehlar | 0fce11f | 2019-03-04 09:21:59 -0800 | [diff] [blame] | 396 | #endif /* CLIB_MARCH_VARIANT */ |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 397 | |
John Lo | a43ccae | 2018-02-13 17:15:23 -0500 | [diff] [blame] | 398 | typedef enum |
| 399 | { |
John Lo | a43ccae | 2018-02-13 17:15:23 -0500 | [diff] [blame] | 400 | GRE_ENCAP_NEXT_L2_MIDCHAIN, |
| 401 | GRE_ENCAP_N_NEXT, |
| 402 | } gre_encap_next_t; |
| 403 | |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 404 | /** |
John Lo | a43ccae | 2018-02-13 17:15:23 -0500 | [diff] [blame] | 405 | * @brief TX function. Only called for L2 payload including TEB or ERSPAN. |
| 406 | * L3 traffic uses the adj-midchains. |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 407 | */ |
Benoît Ganne | 8e22054 | 2019-03-01 14:19:55 +0100 | [diff] [blame] | 408 | VLIB_NODE_FN (gre_encap_node) (vlib_main_t * vm, vlib_node_runtime_t * node, |
Filip Tehlar | 0fce11f | 2019-03-04 09:21:59 -0800 | [diff] [blame] | 409 | vlib_frame_t * frame) |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 410 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 411 | gre_main_t *gm = &gre_main; |
Benoît Ganne | 8e22054 | 2019-03-01 14:19:55 +0100 | [diff] [blame] | 412 | u32 *from, n_left_from; |
| 413 | vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs; |
| 414 | u32 sw_if_index[2] = { ~0, ~0 }; |
| 415 | const gre_tunnel_t *gt[2] = { 0 }; |
| 416 | adj_index_t adj_index[2] = { ADJ_INDEX_INVALID, ADJ_INDEX_INVALID }; |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 417 | |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 418 | from = vlib_frame_vector_args (frame); |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 419 | n_left_from = frame->n_vectors; |
Benoît Ganne | 8e22054 | 2019-03-01 14:19:55 +0100 | [diff] [blame] | 420 | vlib_get_buffers (vm, from, bufs, n_left_from); |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 421 | |
Benoît Ganne | 8e22054 | 2019-03-01 14:19:55 +0100 | [diff] [blame] | 422 | while (n_left_from >= 2) |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 423 | { |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 424 | |
Benoît Ganne | 8e22054 | 2019-03-01 14:19:55 +0100 | [diff] [blame] | 425 | if (PREDICT_FALSE |
| 426 | (sw_if_index[0] != vnet_buffer (b[0])->sw_if_index[VLIB_TX])) |
John Lo | a43ccae | 2018-02-13 17:15:23 -0500 | [diff] [blame] | 427 | { |
Benoît Ganne | 8e22054 | 2019-03-01 14:19:55 +0100 | [diff] [blame] | 428 | const vnet_hw_interface_t *hi; |
| 429 | sw_if_index[0] = vnet_buffer (b[0])->sw_if_index[VLIB_TX]; |
| 430 | hi = vnet_get_sup_hw_interface (gm->vnet_main, sw_if_index[0]); |
| 431 | gt[0] = &gm->tunnels[hi->dev_instance]; |
| 432 | adj_index[0] = gt[0]->l2_adj_index; |
| 433 | } |
| 434 | if (PREDICT_FALSE |
| 435 | (sw_if_index[1] != vnet_buffer (b[1])->sw_if_index[VLIB_TX])) |
| 436 | { |
| 437 | const vnet_hw_interface_t *hi; |
| 438 | sw_if_index[1] = vnet_buffer (b[1])->sw_if_index[VLIB_TX]; |
| 439 | hi = vnet_get_sup_hw_interface (gm->vnet_main, sw_if_index[1]); |
| 440 | gt[1] = &gm->tunnels[hi->dev_instance]; |
| 441 | adj_index[1] = gt[1]->l2_adj_index; |
John Lo | a43ccae | 2018-02-13 17:15:23 -0500 | [diff] [blame] | 442 | } |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 443 | |
Benoît Ganne | 8e22054 | 2019-03-01 14:19:55 +0100 | [diff] [blame] | 444 | vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = adj_index[0]; |
| 445 | vnet_buffer (b[1])->ip.adj_index[VLIB_TX] = adj_index[1]; |
| 446 | |
| 447 | if (PREDICT_FALSE (gt[0]->type == GRE_TUNNEL_TYPE_ERSPAN)) |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 448 | { |
Benoît Ganne | 8e22054 | 2019-03-01 14:19:55 +0100 | [diff] [blame] | 449 | /* Encap GRE seq# and ERSPAN type II header */ |
| 450 | erspan_t2_t *h0; |
| 451 | u32 seq_num; |
| 452 | u64 hdr; |
| 453 | vlib_buffer_advance (b[0], -sizeof (erspan_t2_t)); |
| 454 | h0 = vlib_buffer_get_current (b[0]); |
| 455 | seq_num = clib_atomic_fetch_add (>[0]->gre_sn->seq_num, 1); |
| 456 | hdr = clib_host_to_net_u64 (ERSPAN_HDR2); |
| 457 | h0->seq_num = clib_host_to_net_u32 (seq_num); |
| 458 | h0->t2_u64 = hdr; |
| 459 | h0->t2.cos_en_t_session |= clib_host_to_net_u16 (gt[0]->session_id); |
| 460 | } |
| 461 | if (PREDICT_FALSE (gt[1]->type == GRE_TUNNEL_TYPE_ERSPAN)) |
| 462 | { |
| 463 | /* Encap GRE seq# and ERSPAN type II header */ |
| 464 | erspan_t2_t *h0; |
| 465 | u32 seq_num; |
| 466 | u64 hdr; |
| 467 | vlib_buffer_advance (b[1], -sizeof (erspan_t2_t)); |
| 468 | h0 = vlib_buffer_get_current (b[1]); |
| 469 | seq_num = clib_atomic_fetch_add (>[1]->gre_sn->seq_num, 1); |
| 470 | hdr = clib_host_to_net_u64 (ERSPAN_HDR2); |
| 471 | h0->seq_num = clib_host_to_net_u32 (seq_num); |
| 472 | h0->t2_u64 = hdr; |
| 473 | h0->t2.cos_en_t_session |= clib_host_to_net_u16 (gt[1]->session_id); |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 474 | } |
| 475 | |
Benoît Ganne | 8e22054 | 2019-03-01 14:19:55 +0100 | [diff] [blame] | 476 | if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED)) |
| 477 | { |
| 478 | gre_tx_trace_t *tr = vlib_add_trace (vm, node, |
| 479 | b[0], sizeof (*tr)); |
| 480 | tr->tunnel_id = gt[0] - gm->tunnels; |
| 481 | tr->src = gt[0]->tunnel_src; |
| 482 | tr->dst = gt[0]->tunnel_dst.fp_addr; |
| 483 | tr->length = vlib_buffer_length_in_chain (vm, b[0]); |
| 484 | } |
| 485 | if (PREDICT_FALSE (b[1]->flags & VLIB_BUFFER_IS_TRACED)) |
| 486 | { |
| 487 | gre_tx_trace_t *tr = vlib_add_trace (vm, node, |
| 488 | b[1], sizeof (*tr)); |
| 489 | tr->tunnel_id = gt[1] - gm->tunnels; |
| 490 | tr->src = gt[1]->tunnel_src; |
| 491 | tr->dst = gt[1]->tunnel_dst.fp_addr; |
| 492 | tr->length = vlib_buffer_length_in_chain (vm, b[1]); |
| 493 | } |
| 494 | |
| 495 | b += 2; |
| 496 | n_left_from -= 2; |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 497 | } |
| 498 | |
Benoît Ganne | 8e22054 | 2019-03-01 14:19:55 +0100 | [diff] [blame] | 499 | while (n_left_from >= 1) |
| 500 | { |
| 501 | |
| 502 | if (PREDICT_FALSE |
| 503 | (sw_if_index[0] != vnet_buffer (b[0])->sw_if_index[VLIB_TX])) |
| 504 | { |
| 505 | const vnet_hw_interface_t *hi; |
| 506 | sw_if_index[0] = vnet_buffer (b[0])->sw_if_index[VLIB_TX]; |
| 507 | hi = vnet_get_sup_hw_interface (gm->vnet_main, sw_if_index[0]); |
| 508 | gt[0] = &gm->tunnels[hi->dev_instance]; |
| 509 | adj_index[0] = gt[0]->l2_adj_index; |
| 510 | } |
| 511 | |
| 512 | vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = adj_index[0]; |
| 513 | |
| 514 | if (PREDICT_FALSE (gt[0]->type == GRE_TUNNEL_TYPE_ERSPAN)) |
| 515 | { |
| 516 | /* Encap GRE seq# and ERSPAN type II header */ |
| 517 | erspan_t2_t *h0; |
| 518 | u32 seq_num; |
| 519 | u64 hdr; |
| 520 | vlib_buffer_advance (b[0], -sizeof (erspan_t2_t)); |
| 521 | h0 = vlib_buffer_get_current (b[0]); |
| 522 | seq_num = clib_atomic_fetch_add (>[0]->gre_sn->seq_num, 1); |
| 523 | hdr = clib_host_to_net_u64 (ERSPAN_HDR2); |
| 524 | h0->seq_num = clib_host_to_net_u32 (seq_num); |
| 525 | h0->t2_u64 = hdr; |
| 526 | h0->t2.cos_en_t_session |= clib_host_to_net_u16 (gt[0]->session_id); |
| 527 | } |
| 528 | |
| 529 | if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED)) |
| 530 | { |
| 531 | gre_tx_trace_t *tr = vlib_add_trace (vm, node, |
| 532 | b[0], sizeof (*tr)); |
| 533 | tr->tunnel_id = gt[0] - gm->tunnels; |
| 534 | tr->src = gt[0]->tunnel_src; |
| 535 | tr->dst = gt[0]->tunnel_dst.fp_addr; |
| 536 | tr->length = vlib_buffer_length_in_chain (vm, b[0]); |
| 537 | } |
| 538 | |
| 539 | b += 1; |
| 540 | n_left_from -= 1; |
| 541 | } |
| 542 | |
| 543 | vlib_buffer_enqueue_to_single_next (vm, node, from, |
| 544 | GRE_ENCAP_NEXT_L2_MIDCHAIN, |
| 545 | frame->n_vectors); |
| 546 | |
John Lo | a43ccae | 2018-02-13 17:15:23 -0500 | [diff] [blame] | 547 | vlib_node_increment_counter (vm, node->node_index, |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 548 | GRE_ERROR_PKTS_ENCAP, frame->n_vectors); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 549 | |
| 550 | return frame->n_vectors; |
| 551 | } |
| 552 | |
John Lo | a43ccae | 2018-02-13 17:15:23 -0500 | [diff] [blame] | 553 | static char *gre_error_strings[] = { |
| 554 | #define gre_error(n,s) s, |
| 555 | #include "error.def" |
| 556 | #undef gre_error |
| 557 | }; |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 558 | |
John Lo | a43ccae | 2018-02-13 17:15:23 -0500 | [diff] [blame] | 559 | /* *INDENT-OFF* */ |
| 560 | VLIB_REGISTER_NODE (gre_encap_node) = |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 561 | { |
John Lo | a43ccae | 2018-02-13 17:15:23 -0500 | [diff] [blame] | 562 | .name = "gre-encap", |
| 563 | .vector_size = sizeof (u32), |
| 564 | .format_trace = format_gre_tx_trace, |
| 565 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 566 | .n_errors = GRE_N_ERROR, |
| 567 | .error_strings = gre_error_strings, |
| 568 | .n_next_nodes = GRE_ENCAP_N_NEXT, |
| 569 | .next_nodes = { |
John Lo | a43ccae | 2018-02-13 17:15:23 -0500 | [diff] [blame] | 570 | [GRE_ENCAP_NEXT_L2_MIDCHAIN] = "adj-l2-midchain", |
| 571 | }, |
| 572 | }; |
John Lo | a43ccae | 2018-02-13 17:15:23 -0500 | [diff] [blame] | 573 | /* *INDENT-ON* */ |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 574 | |
Benoît Ganne | 8e22054 | 2019-03-01 14:19:55 +0100 | [diff] [blame] | 575 | #ifndef CLIB_MARCH_VARIANT |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 576 | static u8 * |
| 577 | format_gre_tunnel_name (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 578 | { |
| 579 | u32 dev_instance = va_arg (*args, u32); |
John Lo | a43ccae | 2018-02-13 17:15:23 -0500 | [diff] [blame] | 580 | gre_main_t *gm = &gre_main; |
| 581 | gre_tunnel_t *t; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 582 | |
John Lo | a43ccae | 2018-02-13 17:15:23 -0500 | [diff] [blame] | 583 | if (dev_instance >= vec_len (gm->tunnels)) |
| 584 | return format (s, "<improperly-referenced>"); |
| 585 | |
| 586 | t = pool_elt_at_index (gm->tunnels, dev_instance); |
| 587 | return format (s, "gre%d", t->user_instance); |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 588 | } |
| 589 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 590 | static u8 * |
| 591 | format_gre_device (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 592 | { |
| 593 | u32 dev_instance = va_arg (*args, u32); |
| 594 | CLIB_UNUSED (int verbose) = va_arg (*args, int); |
| 595 | |
| 596 | s = format (s, "GRE tunnel: id %d\n", dev_instance); |
| 597 | return s; |
| 598 | } |
| 599 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 600 | static int |
| 601 | gre_tunnel_desc (u32 sw_if_index, |
| 602 | ip46_address_t * src, ip46_address_t * dst, u8 * is_l2) |
| 603 | { |
| 604 | gre_main_t *gm = &gre_main; |
| 605 | gre_tunnel_t *t; |
| 606 | u32 ti; |
| 607 | |
| 608 | ti = gm->tunnel_index_by_sw_if_index[sw_if_index]; |
| 609 | |
| 610 | if (~0 == ti) |
| 611 | /* not one of ours */ |
| 612 | return -1; |
| 613 | |
| 614 | t = pool_elt_at_index (gm->tunnels, ti); |
| 615 | |
| 616 | *src = t->tunnel_src; |
| 617 | *dst = t->tunnel_dst.fp_addr; |
| 618 | *is_l2 = t->type == GRE_TUNNEL_TYPE_TEB; |
| 619 | |
| 620 | return (0); |
| 621 | } |
| 622 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 623 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 624 | VNET_DEVICE_CLASS (gre_device_class) = { |
| 625 | .name = "GRE tunnel device", |
| 626 | .format_device_name = format_gre_tunnel_name, |
| 627 | .format_device = format_gre_device, |
| 628 | .format_tx_trace = format_gre_tx_trace, |
Chris Luke | 393490e | 2016-05-06 17:09:09 -0400 | [diff] [blame] | 629 | .admin_up_down_function = gre_interface_admin_up_down, |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 630 | .ip_tun_desc = gre_tunnel_desc, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 631 | #ifdef SOON |
| 632 | .clear counter = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 633 | #endif |
| 634 | }; |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 635 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 636 | VNET_HW_INTERFACE_CLASS (gre_hw_interface_class) = { |
| 637 | .name = "GRE", |
| 638 | .format_header = format_gre_header_with_length, |
| 639 | .unformat_header = unformat_gre_header, |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 640 | .build_rewrite = gre_build_rewrite, |
| 641 | .update_adjacency = gre_update_adj, |
| 642 | .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 643 | }; |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 644 | |
| 645 | VNET_HW_INTERFACE_CLASS (mgre_hw_interface_class) = { |
| 646 | .name = "mGRE", |
| 647 | .format_header = format_gre_header_with_length, |
| 648 | .unformat_header = unformat_gre_header, |
| 649 | .build_rewrite = gre_build_rewrite, |
| 650 | .update_adjacency = mgre_update_adj, |
| 651 | .flags = VNET_HW_INTERFACE_CLASS_FLAG_NBMA, |
| 652 | }; |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 653 | /* *INDENT-ON* */ |
Benoît Ganne | 8e22054 | 2019-03-01 14:19:55 +0100 | [diff] [blame] | 654 | #endif /* CLIB_MARCH_VARIANT */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 655 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 656 | static void |
| 657 | add_protocol (gre_main_t * gm, gre_protocol_t protocol, char *protocol_name) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 658 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 659 | gre_protocol_info_t *pi; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 660 | u32 i; |
| 661 | |
| 662 | vec_add2 (gm->protocol_infos, pi, 1); |
| 663 | i = pi - gm->protocol_infos; |
| 664 | |
| 665 | pi->name = protocol_name; |
| 666 | pi->protocol = protocol; |
| 667 | pi->next_index = pi->node_index = ~0; |
| 668 | |
| 669 | hash_set (gm->protocol_info_by_protocol, protocol, i); |
| 670 | hash_set_mem (gm->protocol_info_by_name, pi->name, i); |
| 671 | } |
| 672 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 673 | static clib_error_t * |
| 674 | gre_init (vlib_main_t * vm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 675 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 676 | gre_main_t *gm = &gre_main; |
| 677 | clib_error_t *error; |
| 678 | ip_main_t *im = &ip_main; |
| 679 | ip_protocol_info_t *pi; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 680 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 681 | clib_memset (gm, 0, sizeof (gm[0])); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 682 | gm->vlib_main = vm; |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 683 | gm->vnet_main = vnet_get_main (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 684 | |
| 685 | if ((error = vlib_call_init_function (vm, ip_main_init))) |
| 686 | return error; |
| 687 | |
| 688 | if ((error = vlib_call_init_function (vm, ip4_lookup_init))) |
| 689 | return error; |
| 690 | |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 691 | if ((error = vlib_call_init_function (vm, ip6_lookup_init))) |
| 692 | return error; |
| 693 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 694 | /* Set up the ip packet generator */ |
| 695 | pi = ip_get_protocol_info (im, IP_PROTOCOL_GRE); |
| 696 | pi->format_header = format_gre_header; |
| 697 | pi->unformat_pg_edit = unformat_pg_gre_header; |
| 698 | |
| 699 | gm->protocol_info_by_name = hash_create_string (0, sizeof (uword)); |
| 700 | gm->protocol_info_by_protocol = hash_create (0, sizeof (uword)); |
Neale Ranns | 33ce60d | 2017-12-14 08:51:32 -0800 | [diff] [blame] | 701 | gm->tunnel_by_key4 = |
| 702 | hash_create_mem (0, sizeof (gre_tunnel_key4_t), sizeof (uword)); |
| 703 | gm->tunnel_by_key6 = |
| 704 | hash_create_mem (0, sizeof (gre_tunnel_key6_t), sizeof (uword)); |
John Lo | a43ccae | 2018-02-13 17:15:23 -0500 | [diff] [blame] | 705 | gm->seq_num_by_key = |
| 706 | hash_create_mem (0, sizeof (gre_sn_key_t), sizeof (uword)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 707 | |
| 708 | #define _(n,s) add_protocol (gm, GRE_PROTOCOL_##s, #s); |
| 709 | foreach_gre_protocol |
| 710 | #undef _ |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 711 | return vlib_call_init_function (vm, gre_input_init); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | VLIB_INIT_FUNCTION (gre_init); |
| 715 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 716 | /* |
| 717 | * fd.io coding-style-patch-verification: ON |
| 718 | * |
| 719 | * Local Variables: |
| 720 | * eval: (c-set-style "gnu") |
| 721 | * End: |
| 722 | */ |