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