blob: 301f8e48b63a17c7b292d5e01f9b577b14dd9f63 [file] [log] [blame]
Dave Barach8d0f2f02018-03-12 09:31:36 -04001;;; Copyright (c) 2016 Cisco and/or its affiliates.
2;;; Licensed under the Apache License, Version 2.0 (the "License");
3;;; you may not use this file except in compliance with the License.
4;;; You may obtain a copy of the License at:
5;;;
6;;; http://www.apache.org/licenses/LICENSE-2.0
7;;;
8;;; Unless required by applicable law or agreed to in writing, software
9;;; distributed under the License is distributed on an "AS IS" BASIS,
10;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11;;; See the License for the specific language governing permissions and
12;;; limitations under the License.
13
Ed Warnickecb9cada2015-12-08 15:45:58 -070014;;; tunnel-encap-skel.el - tunnel interface output skeleton
15
16(require 'skeleton)
17
Keith Burns (alagalah)ca46d8c2016-03-18 07:22:15 -070018(define-skeleton skel-tunnel-encap
Ed Warnickecb9cada2015-12-08 15:45:58 -070019"Insert a tunnel encap implementation"
20nil
21'(setq encap_stack (skeleton-read "encap_stack (e.g ip4_udp_lisp): "))
22'(setq ENCAP_STACK (upcase encap_stack))
23'(setq encap-stack (replace-regexp-in-string "_" "-" encap_stack))
24'(setq ENCAP-STACK (upcase encap-stack))
25"
Dave Wallace526b5e82016-03-14 00:10:24 -040026#include <vppinfra/error.h>
27#include <vppinfra/hash.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070028#include <vnet/vnet.h>
29#include <vnet/ip/ip.h>
30#include <vnet/ethernet/ethernet.h>
31#include <vnet/" encap-stack "/" encap_stack ".h>
32
33/* Statistics (not really errors) */
34#define foreach_" encap_stack "_encap_error \\
35_(ENCAPSULATED, \"good packets encapsulated\")
36
37static char * " encap_stack "_encap_error_strings[] = {
38#define _(sym,string) string,
39 foreach_" encap_stack "_encap_error
40#undef _
41};
42
43typedef enum {
44#define _(sym,str) " ENCAP_STACK "_ENCAP_ERROR_##sym,
45 foreach_" encap_stack "_encap_error
46#undef _
47 " ENCAP_STACK "_ENCAP_N_ERROR,
48} " encap_stack "_encap_error_t;
49
50typedef enum {
51 " ENCAP_STACK "_ENCAP_NEXT_IP4_LOOKUP,
52 " ENCAP_STACK "_ENCAP_NEXT_DROP,
53 " ENCAP_STACK "_ENCAP_N_NEXT,
54} " encap_stack "_encap_next_t;
55
56typedef struct {
57 u32 tunnel_index;
58} " encap_stack "_encap_trace_t;
59
60u8 * format_" encap_stack "_encap_trace (u8 * s, va_list * args)
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 *);
64 " encap_stack "_encap_trace_t * t
65 = va_arg (*args, " encap_stack "_encap_trace_t *);
66
67 s = format (s, \"" ENCAP-STACK ": tunnel %d\", t->tunnel_index);
68 return s;
69}
70
71/* $$$$ FIXME adjust to match the rewrite string */
72#define foreach_fixed_header_offset \\
73_(0) _(1) _(2) _(3) _(FIXME)
74
75static uword
76" encap_stack "_encap (vlib_main_t * vm,
77 vlib_node_runtime_t * node,
78 vlib_frame_t * from_frame)
79{
80 u32 n_left_from, next_index, * from, * to_next;
81 " encap_stack "_main_t * ngm = &" encap_stack "_main;
82 vnet_main_t * vnm = ngm->vnet_main;
83 u32 pkts_encapsulated = 0;
84 u16 old_l0 = 0, old_l1 = 0;
85
86 from = vlib_frame_vector_args (from_frame);
87 n_left_from = from_frame->n_vectors;
88
89 next_index = node->cached_next_index;
90
91 while (n_left_from > 0)
92 {
93 u32 n_left_to_next;
94
95 vlib_get_next_frame (vm, node, next_index,
96 to_next, n_left_to_next);
97
98#if 0 /* $$$ dual loop when the single loop works */
99 while (n_left_from >= 4 && n_left_to_next >= 2)
100 {
101 u32 bi0, bi1;
102 vlib_buffer_t * b0, * b1;
103 nsh_unicast_header_t * h0, * h1;
104 u32 label0, label1;
105 u32 next0, next1;
106 uword * p0, * p1;
107
108 /* Prefetch next iteration. */
109 {
110 vlib_buffer_t * p2, * p3;
111
112 p2 = vlib_get_buffer (vm, from[2]);
113 p3 = vlib_get_buffer (vm, from[3]);
114
115 vlib_prefetch_buffer_header (p2, LOAD);
116 vlib_prefetch_buffer_header (p3, LOAD);
117
118 CLIB_PREFETCH (p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
119 CLIB_PREFETCH (p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
120 }
121
122 bi0 = from[0];
123 bi1 = from[1];
124 to_next[0] = bi0;
125 to_next[1] = bi1;
126 from += 2;
127 to_next += 2;
128 n_left_to_next -= 2;
129 n_left_from -= 2;
130
131 b0 = vlib_get_buffer (vm, bi0);
132 b1 = vlib_get_buffer (vm, bi1);
133
134 h0 = vlib_buffer_get_current (b0);
135 h1 = vlib_buffer_get_current (b1);
136
137 next0 = next1 = " ENCAP_STACK "_ENCAP_NEXT_IP4_LOOKUP;
138
139 vlib_buffer_advance (b0, sizeof (*h0));
140 vlib_buffer_advance (b1, sizeof (*h1));
141
142 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
143 to_next, n_left_to_next,
144 bi0, bi1, next0, next1);
145 }
146#endif
147
148 while (n_left_from > 0 && n_left_to_next > 0)
149 {
150 u32 bi0;
151 vlib_buffer_t * b0;
152 u32 next0 = " ENCAP_STACK "_ENCAP_NEXT_IP4_LOOKUP;
153 vnet_hw_interface_t * hi0;
154 ip4_header_t * ip0;
155 udp_header_t * udp0;
156 u64 * copy_src0, * copy_dst0;
157 u32 * copy_src_last0, * copy_dst_last0;
158 " encap_stack "_tunnel_t * t0;
159 u16 new_l0;
160 ip_csum_t sum0;
161
162 bi0 = from[0];
163 to_next[0] = bi0;
164 from += 1;
165 to_next += 1;
166 n_left_from -= 1;
167 n_left_to_next -= 1;
168
169 b0 = vlib_get_buffer (vm, bi0);
170
171 /* 1-wide cache? */
172 hi0 = vnet_get_sup_hw_interface
173 (vnm, vnet_buffer(b0)->sw_if_index[VLIB_TX]);
174
175 t0 = pool_elt_at_index (ngm->tunnels, hi0->dev_instance);
176
177 ASSERT(vec_len(t0->rewrite) >= 24);
178
179 /* Apply the rewrite string. $$$$ vnet_rewrite? */
180 vlib_buffer_advance (b0, -(word)_vec_len(t0->rewrite));
181
182 ip0 = vlib_buffer_get_current(b0);
183 /* Copy the fixed header */
184 copy_dst0 = (u64 *) ip0;
185 copy_src0 = (u64 *) t0->rewrite;
186
187 ASSERT (sizeof (ip4_udp_" encap_stack "_header_t) == FIXME);
188
189 /* Copy first N octets 8-bytes at a time */
190#define _(offs) copy_dst0[offs] = copy_src0[offs];
191 foreach_fixed_header_offset;
192#undef _
193#if 0 /* needed if encap not a multiple of 8 bytes */
194 /* Last 4 octets. Hopefully gcc will be our friend */
195 copy_dst_last0 = (u32 *)(&copy_dst0[FIXME]);
196 copy_src_last0 = (u32 *)(&copy_src0[FIXME]);
197 copy_dst_last0[0] = copy_src_last0[0];
198
199#endif
200 /* fix the <bleep>ing outer-IP checksum */
201 sum0 = ip0->checksum;
202 /* old_l0 always 0, see the rewrite setup */
203 new_l0 =
204 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
205
206 sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
207 length /* changed member */);
208 ip0->checksum = ip_csum_fold (sum0);
209 ip0->length = new_l0;
210
211 /* Fix UDP length */
212 udp0 = (udp_header_t *)(ip0+1);
213 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
214 - sizeof (*ip0));
215
216 udp0->length = new_l0;
217
218 /* Reset to look up tunnel partner in the configured FIB */
219 vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->encap_fib_index;
220 pkts_encapsulated ++;
221
222 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
223 {
224 " encap_stack "_encap_trace_t *tr =
225 vlib_add_trace (vm, node, b0, sizeof (*tr));
226 tr->tunnel_index = t0 - ngm->tunnels;
227 }
228 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
229 to_next, n_left_to_next,
230 bi0, next0);
231 }
232
233 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
234 }
235 vlib_node_increment_counter (vm, node->node_index,
236 " ENCAP_STACK "_ENCAP_ERROR_ENCAPSULATED,
237 pkts_encapsulated);
238 return from_frame->n_vectors;
239}
240
241VLIB_REGISTER_NODE (" encap_stack "_encap_node) = {
242 .function = " encap_stack "_encap,
243 .name = \"" encap-stack "-encap\",
244 .vector_size = sizeof (u32),
245 .format_trace = format_" encap_stack "_encap_trace,
246 .type = VLIB_NODE_TYPE_INTERNAL,
247
248 .n_errors = ARRAY_LEN(" encap_stack "_encap_error_strings),
249 .error_strings = " encap_stack "_encap_error_strings,
250
251 .n_next_nodes = " ENCAP_STACK "_ENCAP_N_NEXT,
252
253 .next_nodes = {
254 [" ENCAP_STACK "_ENCAP_NEXT_IP4_LOOKUP] = \"ip4-lookup\",
255 [" ENCAP_STACK "_ENCAP_NEXT_DROP] = \"error-drop\",
256 },
257};
258")