blob: 7660bbe1d4ba79f4fa18cfbcb34980ab9ee8aa69 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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 Rannsb80c5362016-10-08 13:03:40 +010020#include <vnet/adj/adj_midchain.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070021
22gre_main_t gre_main;
23
Swarup Nayak9ff647a2017-11-27 10:27:43 +053024typedef struct
25{
26 union
27 {
Ed Warnickecb9cada2015-12-08 15:45:58 -070028 ip4_and_gre_header_t ip4_and_gre;
29 u64 as_u64[3];
30 };
31} ip4_and_gre_union_t;
32
Swarup Nayak9ff647a2017-11-27 10:27:43 +053033typedef struct
34{
35 union
36 {
Ciara Loftus7eac9162016-09-30 15:47:03 +010037 ip6_and_gre_header_t ip6_and_gre;
38 u64 as_u64[3];
39 };
40} ip6_and_gre_union_t;
41
Ed Warnickecb9cada2015-12-08 15:45:58 -070042
43/* Packet trace structure */
Swarup Nayak9ff647a2017-11-27 10:27:43 +053044typedef struct
45{
Ed Warnickecb9cada2015-12-08 15:45:58 -070046 /* Tunnel-id / index in tunnel vector */
47 u32 tunnel_id;
48
49 /* pkt length */
50 u32 length;
51
Ciara Loftus7eac9162016-09-30 15:47:03 +010052 /* tunnel ip addresses */
53 ip46_address_t src;
54 ip46_address_t dst;
Ed Warnickecb9cada2015-12-08 15:45:58 -070055} gre_tx_trace_t;
56
Swarup Nayak9ff647a2017-11-27 10:27:43 +053057u8 *
58format_gre_tx_trace (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070059{
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 Nayak9ff647a2017-11-27 10:27:43 +053062 gre_tx_trace_t *t = va_arg (*args, gre_tx_trace_t *);
Neale Ranns5e575b12016-10-03 09:40:25 +010063
Ed Warnickecb9cada2015-12-08 15:45:58 -070064 s = format (s, "GRE: tunnel %d len %d src %U dst %U",
Neale Ranns5e575b12016-10-03 09:40:25 +010065 t->tunnel_id, clib_net_to_host_u16 (t->length),
Ciara Loftus7eac9162016-09-30 15:47:03 +010066 format_ip46_address, &t->src, IP46_TYPE_ANY,
67 format_ip46_address, &t->dst, IP46_TYPE_ANY);
Ed Warnickecb9cada2015-12-08 15:45:58 -070068 return s;
69}
70
Swarup Nayak9ff647a2017-11-27 10:27:43 +053071u8 *
72format_gre_protocol (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070073{
74 gre_protocol_t p = va_arg (*args, u32);
Swarup Nayak9ff647a2017-11-27 10:27:43 +053075 gre_main_t *gm = &gre_main;
76 gre_protocol_info_t *pi = gre_get_protocol_info (gm, p);
Ed Warnickecb9cada2015-12-08 15:45:58 -070077
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 Nayak9ff647a2017-11-27 10:27:43 +053086u8 *
87format_gre_header_with_length (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070088{
Swarup Nayak9ff647a2017-11-27 10:27:43 +053089 gre_main_t *gm = &gre_main;
90 gre_header_t *h = va_arg (*args, gre_header_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070091 u32 max_header_bytes = va_arg (*args, u32);
92 gre_protocol_t p = clib_net_to_host_u16 (h->protocol);
Christophe Fontained3c008d2017-10-02 18:10:54 +020093 u32 indent, header_bytes;
Ed Warnickecb9cada2015-12-08 15:45:58 -070094
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
103 if (max_header_bytes != 0 && header_bytes > max_header_bytes)
104 {
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530105 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 Warnickecb9cada2015-12-08 15:45:58 -0700107 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 Nayak9ff647a2017-11-27 10:27:43 +0530117u8 *
118format_gre_header (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700119{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530120 gre_header_t *h = va_arg (*args, gre_header_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700121 return format (s, "%U", format_gre_header_with_length, h, 0);
122}
123
124/* Returns gre protocol as an int in host byte order. */
125uword
126unformat_gre_protocol_host_byte_order (unformat_input_t * input,
127 va_list * args)
128{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530129 u16 *result = va_arg (*args, u16 *);
130 gre_main_t *gm = &gre_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700131 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 Nayak9ff647a2017-11-27 10:27:43 +0530137 gre_protocol_info_t *pi = vec_elt_at_index (gm->protocol_infos, i);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700138 *result = pi->protocol;
139 return 1;
140 }
141
142 return 0;
143}
144
145uword
146unformat_gre_protocol_net_byte_order (unformat_input_t * input,
147 va_list * args)
148{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530149 u16 *result = va_arg (*args, u16 *);
150 if (!unformat_user (input, unformat_gre_protocol_host_byte_order, result))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700151 return 0;
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530152 *result = clib_host_to_net_u16 ((u16) * result);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700153 return 1;
154}
155
156uword
157unformat_gre_header (unformat_input_t * input, va_list * args)
158{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530159 u8 **result = va_arg (*args, u8 **);
160 gre_header_t _h, *h = &_h;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700161 u16 p;
162
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530163 if (!unformat (input, "%U", unformat_gre_protocol_host_byte_order, &p))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700164 return 0;
165
166 h->protocol = clib_host_to_net_u16 (p);
167
168 /* Add header to result. */
169 {
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530170 void *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700171 u32 n_bytes = sizeof (h[0]);
172
173 vec_add2 (*result, p, n_bytes);
Damjan Marionf1213b82016-03-13 02:22:06 +0100174 clib_memcpy (p, h, n_bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700175 }
Neale Ranns5e575b12016-10-03 09:40:25 +0100176
Ed Warnickecb9cada2015-12-08 15:45:58 -0700177 return 1;
178}
179
Neale Rannsb80c5362016-10-08 13:03:40 +0100180static int
181gre_proto_from_vnet_link (vnet_link_t link)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700182{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530183 switch (link)
Neale Rannsb80c5362016-10-08 13:03:40 +0100184 {
185 case VNET_LINK_IP4:
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530186 return (GRE_PROTOCOL_ip4);
Neale Rannsb80c5362016-10-08 13:03:40 +0100187 case VNET_LINK_IP6:
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530188 return (GRE_PROTOCOL_ip6);
Neale Rannsb80c5362016-10-08 13:03:40 +0100189 case VNET_LINK_MPLS:
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530190 return (GRE_PROTOCOL_mpls_unicast);
Neale Rannsb80c5362016-10-08 13:03:40 +0100191 case VNET_LINK_ETHERNET:
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530192 return (GRE_PROTOCOL_teb);
Neale Rannsb80c5362016-10-08 13:03:40 +0100193 case VNET_LINK_ARP:
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530194 return (GRE_PROTOCOL_arp);
Florin Corasce1b4c72017-01-26 14:25:34 -0800195 case VNET_LINK_NSH:
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530196 ASSERT (0);
197 break;
Neale Rannsb80c5362016-10-08 13:03:40 +0100198 }
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530199 ASSERT (0);
200 return (GRE_PROTOCOL_ip4);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700201}
202
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530203static u8 *
Neale Rannsb80c5362016-10-08 13:03:40 +0100204gre_build_rewrite (vnet_main_t * vnm,
205 u32 sw_if_index,
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530206 vnet_link_t link_type, const void *dst_address)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700207{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530208 gre_main_t *gm = &gre_main;
209 ip4_and_gre_header_t *h4;
210 ip6_and_gre_header_t *h6;
211 u8 *rewrite = NULL;
Neale Rannsb80c5362016-10-08 13:03:40 +0100212 gre_tunnel_t *t;
213 u32 ti;
Ciara Loftus7eac9162016-09-30 15:47:03 +0100214 u8 is_ipv6;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700215
Neale Rannsb80c5362016-10-08 13:03:40 +0100216 ti = gm->tunnel_index_by_sw_if_index[sw_if_index];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700217
Neale Rannsb80c5362016-10-08 13:03:40 +0100218 if (~0 == ti)
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530219 /* not one of ours */
220 return (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700221
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530222 t = pool_elt_at_index (gm->tunnels, ti);
Neale Ranns5e575b12016-10-03 09:40:25 +0100223
Ciara Loftus7eac9162016-09-30 15:47:03 +0100224 is_ipv6 = t->tunnel_dst.fp_proto == FIB_PROTOCOL_IP6 ? 1 : 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700225
Ciara Loftus7eac9162016-09-30 15:47:03 +0100226 if (!is_ipv6)
227 {
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530228 vec_validate (rewrite, sizeof (*h4) - 1);
229 h4 = (ip4_and_gre_header_t *) rewrite;
230 h4->gre.protocol =
231 clib_host_to_net_u16 (gre_proto_from_vnet_link (link_type));
Ciara Loftus7eac9162016-09-30 15:47:03 +0100232
233 h4->ip4.ip_version_and_header_length = 0x45;
234 h4->ip4.ttl = 254;
235 h4->ip4.protocol = IP_PROTOCOL_GRE;
236 /* fixup ip4 header length and checksum after-the-fact */
237 h4->ip4.src_address.as_u32 = t->tunnel_src.ip4.as_u32;
238 h4->ip4.dst_address.as_u32 = t->tunnel_dst.fp_addr.ip4.as_u32;
239 h4->ip4.checksum = ip4_header_checksum (&h4->ip4);
240 }
241 else
242 {
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530243 vec_validate (rewrite, sizeof (*h6) - 1);
244 h6 = (ip6_and_gre_header_t *) rewrite;
245 h6->gre.protocol =
246 clib_host_to_net_u16 (gre_proto_from_vnet_link (link_type));
Ciara Loftus7eac9162016-09-30 15:47:03 +0100247
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530248 h6->ip6.ip_version_traffic_class_and_flow_label =
249 clib_host_to_net_u32 (6 << 28);
Ciara Loftus7eac9162016-09-30 15:47:03 +0100250 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 Warnickecb9cada2015-12-08 15:45:58 -0700258
Neale Rannsb80c5362016-10-08 13:03:40 +0100259 return (rewrite);
Neale Ranns5e575b12016-10-03 09:40:25 +0100260}
261
Ciara Loftus7eac9162016-09-30 15:47:03 +0100262#define is_v4_packet(_h) ((*(u8*) _h) & 0xF0) == 0x40
263
Neale Rannsb80c5362016-10-08 13:03:40 +0100264void
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530265gre4_fixup (vlib_main_t * vm, ip_adjacency_t * adj, vlib_buffer_t * b0)
Neale Rannsb80c5362016-10-08 13:03:40 +0100266{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530267 ip4_header_t *ip0;
Neale Rannsb80c5362016-10-08 13:03:40 +0100268
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530269 ip0 = vlib_buffer_get_current (b0);
Neale Rannsb80c5362016-10-08 13:03:40 +0100270
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530271 /* Fixup the checksum and len fields in the GRE tunnel encap
272 * that was applied at the midchain node */
273 ip0->length = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
274 ip0->checksum = ip4_header_checksum (ip0);
Neale Rannsb80c5362016-10-08 13:03:40 +0100275}
276
277void
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530278gre6_fixup (vlib_main_t * vm, ip_adjacency_t * adj, vlib_buffer_t * b0)
Ciara Loftus7eac9162016-09-30 15:47:03 +0100279{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530280 ip6_header_t *ip0;
Ciara Loftus7eac9162016-09-30 15:47:03 +0100281
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530282 ip0 = vlib_buffer_get_current (b0);
Ciara Loftus7eac9162016-09-30 15:47:03 +0100283
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530284 /* Fixup the payload length field in the GRE tunnel encap that was applied
285 * at the midchain node */
286 ip0->payload_length =
287 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0))
288 - sizeof (*ip0);
Ciara Loftus7eac9162016-09-30 15:47:03 +0100289}
290
291void
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530292gre_update_adj (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai)
Neale Rannsb80c5362016-10-08 13:03:40 +0100293{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530294 gre_main_t *gm = &gre_main;
295 gre_tunnel_t *t;
296 u32 ti;
297 u8 is_ipv6;
Ciara Loftus7eac9162016-09-30 15:47:03 +0100298
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530299 ti = gm->tunnel_index_by_sw_if_index[sw_if_index];
300 t = pool_elt_at_index (gm->tunnels, ti);
301 is_ipv6 = t->tunnel_dst.fp_proto == FIB_PROTOCOL_IP6 ? 1 : 0;
Ciara Loftus7eac9162016-09-30 15:47:03 +0100302
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530303 adj_nbr_midchain_update_rewrite (ai, !is_ipv6 ? gre4_fixup : gre6_fixup,
304 (VNET_LINK_ETHERNET ==
305 adj_get_link_type (ai) ?
306 ADJ_FLAG_MIDCHAIN_NO_COUNT :
307 ADJ_FLAG_NONE), gre_build_rewrite (vnm,
308 sw_if_index,
309 adj_get_link_type
310 (ai),
311 NULL));
Neale Rannsb80c5362016-10-08 13:03:40 +0100312
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530313 gre_tunnel_stack (ai);
Neale Rannsb80c5362016-10-08 13:03:40 +0100314}
315
316/**
317 * @brief TX function. Only called L2. L3 traffic uses the adj-midchains
318 */
Neale Ranns5e575b12016-10-03 09:40:25 +0100319static uword
Neale Ranns177bbdc2016-11-15 09:46:51 +0000320gre_interface_tx_inline (vlib_main_t * vm,
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530321 vlib_node_runtime_t * node, vlib_frame_t * frame)
Neale Ranns5e575b12016-10-03 09:40:25 +0100322{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530323 gre_main_t *gm = &gre_main;
Neale Ranns5e575b12016-10-03 09:40:25 +0100324 u32 next_index;
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530325 u32 *from, *to_next, n_left_from, n_left_to_next;
326 vnet_interface_output_runtime_t *rd = (void *) node->runtime_data;
Neale Ranns5e575b12016-10-03 09:40:25 +0100327 const gre_tunnel_t *gt = pool_elt_at_index (gm->tunnels, rd->dev_instance);
Ciara Loftus7eac9162016-09-30 15:47:03 +0100328 u8 is_ipv6 = gt->tunnel_dst.fp_proto == FIB_PROTOCOL_IP6 ? 1 : 0;
Neale Ranns5e575b12016-10-03 09:40:25 +0100329
330 /* Vector of buffer / pkt indices we're supposed to process */
331 from = vlib_frame_vector_args (frame);
332
333 /* Number of buffers / pkts */
334 n_left_from = frame->n_vectors;
335
336 /* Speculatively send the first buffer to the last disposition we used */
337 next_index = node->cached_next_index;
338
339 while (n_left_from > 0)
340 {
341 /* set up to enqueue to our disposition with index = next_index */
342 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
343
344 /*
345 * FIXME DUAL LOOP
346 */
347
348 while (n_left_from > 0 && n_left_to_next > 0)
349 {
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530350 vlib_buffer_t *b0;
Neale Ranns5e575b12016-10-03 09:40:25 +0100351 u32 bi0;
352
353 bi0 = from[0];
354 to_next[0] = bi0;
355 from += 1;
356 to_next += 1;
357 n_left_from -= 1;
358 n_left_to_next -= 1;
359
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530360 b0 = vlib_get_buffer (vm, bi0);
Neale Ranns5e575b12016-10-03 09:40:25 +0100361
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530362 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = gt->l2_adj_index;
Neale Ranns5e575b12016-10-03 09:40:25 +0100363
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530364 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
Neale Ranns5e575b12016-10-03 09:40:25 +0100365 {
366 gre_tx_trace_t *tr = vlib_add_trace (vm, node,
367 b0, sizeof (*tr));
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530368 tr->tunnel_id = gt - gm->tunnels;
369 tr->src = gt->tunnel_src;
370 tr->dst = gt->tunnel_src;
371 tr->length = vlib_buffer_length_in_chain (vm, b0);
Neale Ranns5e575b12016-10-03 09:40:25 +0100372 }
373
374 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
375 to_next, n_left_to_next,
376 bi0, gt->l2_tx_arc);
377 }
378
379 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
380 }
381
Ciara Loftus7eac9162016-09-30 15:47:03 +0100382 vlib_node_increment_counter (vm, !is_ipv6 ? gre4_input_node.index :
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530383 gre6_input_node.index,
Neale Ranns5e575b12016-10-03 09:40:25 +0100384 GRE_ERROR_PKTS_ENCAP, frame->n_vectors);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700385
386 return frame->n_vectors;
387}
388
Neale Ranns177bbdc2016-11-15 09:46:51 +0000389static uword
390gre_interface_tx (vlib_main_t * vm,
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530391 vlib_node_runtime_t * node, vlib_frame_t * frame)
Neale Ranns177bbdc2016-11-15 09:46:51 +0000392{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530393 return (gre_interface_tx_inline (vm, node, frame));
Neale Ranns177bbdc2016-11-15 09:46:51 +0000394}
395
396static uword
397gre_teb_interface_tx (vlib_main_t * vm,
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530398 vlib_node_runtime_t * node, vlib_frame_t * frame)
Neale Ranns177bbdc2016-11-15 09:46:51 +0000399{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530400 return (gre_interface_tx_inline (vm, node, frame));
Neale Ranns177bbdc2016-11-15 09:46:51 +0000401}
402
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530403static u8 *
404format_gre_tunnel_name (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700405{
406 u32 dev_instance = va_arg (*args, u32);
407 return format (s, "gre%d", dev_instance);
408}
409
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530410static u8 *
411format_gre_tunnel_teb_name (u8 * s, va_list * args)
Neale Ranns177bbdc2016-11-15 09:46:51 +0000412{
413 u32 dev_instance = va_arg (*args, u32);
414 return format (s, "teb-gre%d", dev_instance);
415}
416
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530417static u8 *
418format_gre_device (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700419{
420 u32 dev_instance = va_arg (*args, u32);
421 CLIB_UNUSED (int verbose) = va_arg (*args, int);
422
423 s = format (s, "GRE tunnel: id %d\n", dev_instance);
424 return s;
425}
426
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530427/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700428VNET_DEVICE_CLASS (gre_device_class) = {
429 .name = "GRE tunnel device",
430 .format_device_name = format_gre_tunnel_name,
431 .format_device = format_gre_device,
432 .format_tx_trace = format_gre_tx_trace,
433 .tx_function = gre_interface_tx,
Chris Luke393490e2016-05-06 17:09:09 -0400434 .admin_up_down_function = gre_interface_admin_up_down,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700435#ifdef SOON
436 .clear counter = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700437#endif
438};
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530439/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700440
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530441
442/* *INDENT-OFF* */
Damjan Marion1c80e832016-05-11 23:07:18 +0200443VLIB_DEVICE_TX_FUNCTION_MULTIARCH (gre_device_class,
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530444 gre_interface_tx)
Damjan Marion1c80e832016-05-11 23:07:18 +0200445
Neale Ranns177bbdc2016-11-15 09:46:51 +0000446VNET_DEVICE_CLASS (gre_device_teb_class) = {
447 .name = "GRE TEB tunnel device",
448 .format_device_name = format_gre_tunnel_teb_name,
449 .format_device = format_gre_device,
450 .format_tx_trace = format_gre_tx_trace,
451 .tx_function = gre_teb_interface_tx,
452 .admin_up_down_function = gre_interface_admin_up_down,
453#ifdef SOON
454 .clear counter = 0;
455#endif
456};
457
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530458/* *INDENT-ON* */
459
460/* *INDENT-OFF* */
Neale Ranns177bbdc2016-11-15 09:46:51 +0000461VLIB_DEVICE_TX_FUNCTION_MULTIARCH (gre_device_teb_class,
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530462 gre_teb_interface_tx)
Neale Ranns177bbdc2016-11-15 09:46:51 +0000463
Ed Warnickecb9cada2015-12-08 15:45:58 -0700464VNET_HW_INTERFACE_CLASS (gre_hw_interface_class) = {
465 .name = "GRE",
466 .format_header = format_gre_header_with_length,
467 .unformat_header = unformat_gre_header,
Neale Rannsb80c5362016-10-08 13:03:40 +0100468 .build_rewrite = gre_build_rewrite,
469 .update_adjacency = gre_update_adj,
470 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700471};
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530472/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700473
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530474static void
475add_protocol (gre_main_t * gm, gre_protocol_t protocol, char *protocol_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700476{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530477 gre_protocol_info_t *pi;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700478 u32 i;
479
480 vec_add2 (gm->protocol_infos, pi, 1);
481 i = pi - gm->protocol_infos;
482
483 pi->name = protocol_name;
484 pi->protocol = protocol;
485 pi->next_index = pi->node_index = ~0;
486
487 hash_set (gm->protocol_info_by_protocol, protocol, i);
488 hash_set_mem (gm->protocol_info_by_name, pi->name, i);
489}
490
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530491static clib_error_t *
492gre_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700493{
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530494 gre_main_t *gm = &gre_main;
495 clib_error_t *error;
496 ip_main_t *im = &ip_main;
497 ip_protocol_info_t *pi;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700498
499 memset (gm, 0, sizeof (gm[0]));
500 gm->vlib_main = vm;
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530501 gm->vnet_main = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700502
503 if ((error = vlib_call_init_function (vm, ip_main_init)))
504 return error;
505
506 if ((error = vlib_call_init_function (vm, ip4_lookup_init)))
507 return error;
508
Ciara Loftus7eac9162016-09-30 15:47:03 +0100509 if ((error = vlib_call_init_function (vm, ip6_lookup_init)))
510 return error;
511
Ed Warnickecb9cada2015-12-08 15:45:58 -0700512 /* Set up the ip packet generator */
513 pi = ip_get_protocol_info (im, IP_PROTOCOL_GRE);
514 pi->format_header = format_gre_header;
515 pi->unformat_pg_edit = unformat_pg_gre_header;
516
517 gm->protocol_info_by_name = hash_create_string (0, sizeof (uword));
518 gm->protocol_info_by_protocol = hash_create (0, sizeof (uword));
Neale Ranns33ce60d2017-12-14 08:51:32 -0800519 gm->tunnel_by_key4 =
520 hash_create_mem (0, sizeof (gre_tunnel_key4_t), sizeof (uword));
521 gm->tunnel_by_key6 =
522 hash_create_mem (0, sizeof (gre_tunnel_key6_t), sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700523
524#define _(n,s) add_protocol (gm, GRE_PROTOCOL_##s, #s);
525 foreach_gre_protocol
526#undef _
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530527 return vlib_call_init_function (vm, gre_input_init);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700528}
529
530VLIB_INIT_FUNCTION (gre_init);
531
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530532gre_main_t *
533gre_get_main (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700534{
535 vlib_call_init_function (vm, gre_init);
536 return &gre_main;
537}
538
Swarup Nayak9ff647a2017-11-27 10:27:43 +0530539
540/*
541 * fd.io coding-style-patch-verification: ON
542 *
543 * Local Variables:
544 * eval: (c-set-style "gnu")
545 * End:
546 */