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