1 ;;; 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:
6 ;;; http://www.apache.org/licenses/LICENSE-2.0
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.
14 ;;; tunnel-encap-skel.el - tunnel interface output skeleton
18 (define-skeleton skel-tunnel-encap
19 "Insert a tunnel encap implementation"
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))
26 #include <vppinfra/error.h>
27 #include <vppinfra/hash.h>
28 #include <vnet/vnet.h>
29 #include <vnet/ip/ip.h>
30 #include <vnet/ethernet/ethernet.h>
31 #include <vnet/" encap-stack "/" encap_stack ".h>
33 /* Statistics (not really errors) */
34 #define foreach_" encap_stack "_encap_error \\
35 _(ENCAPSULATED, \"good packets encapsulated\")
37 static char * " encap_stack "_encap_error_strings[] = {
38 #define _(sym,string) string,
39 foreach_" encap_stack "_encap_error
44 #define _(sym,str) " ENCAP_STACK "_ENCAP_ERROR_##sym,
45 foreach_" encap_stack "_encap_error
47 " ENCAP_STACK "_ENCAP_N_ERROR,
48 } " encap_stack "_encap_error_t;
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;
58 } " encap_stack "_encap_trace_t;
60 u8 * format_" encap_stack "_encap_trace (u8 * s, va_list * args)
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 *);
67 s = format (s, \"" ENCAP-STACK ": tunnel %d\", t->tunnel_index);
71 /* $$$$ FIXME adjust to match the rewrite string */
72 #define foreach_fixed_header_offset \\
73 _(0) _(1) _(2) _(3) _(FIXME)
76 " encap_stack "_encap (vlib_main_t * vm,
77 vlib_node_runtime_t * node,
78 vlib_frame_t * from_frame)
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;
86 from = vlib_frame_vector_args (from_frame);
87 n_left_from = from_frame->n_vectors;
89 next_index = node->cached_next_index;
91 while (n_left_from > 0)
95 vlib_get_next_frame (vm, node, next_index,
96 to_next, n_left_to_next);
98 #if 0 /* $$$ dual loop when the single loop works */
99 while (n_left_from >= 4 && n_left_to_next >= 2)
102 vlib_buffer_t * b0, * b1;
103 nsh_unicast_header_t * h0, * h1;
108 /* Prefetch next iteration. */
110 vlib_buffer_t * p2, * p3;
112 p2 = vlib_get_buffer (vm, from[2]);
113 p3 = vlib_get_buffer (vm, from[3]);
115 vlib_prefetch_buffer_header (p2, LOAD);
116 vlib_prefetch_buffer_header (p3, LOAD);
118 CLIB_PREFETCH (p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
119 CLIB_PREFETCH (p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
131 b0 = vlib_get_buffer (vm, bi0);
132 b1 = vlib_get_buffer (vm, bi1);
134 h0 = vlib_buffer_get_current (b0);
135 h1 = vlib_buffer_get_current (b1);
137 next0 = next1 = " ENCAP_STACK "_ENCAP_NEXT_IP4_LOOKUP;
139 vlib_buffer_advance (b0, sizeof (*h0));
140 vlib_buffer_advance (b1, sizeof (*h1));
142 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
143 to_next, n_left_to_next,
144 bi0, bi1, next0, next1);
148 while (n_left_from > 0 && n_left_to_next > 0)
152 u32 next0 = " ENCAP_STACK "_ENCAP_NEXT_IP4_LOOKUP;
153 vnet_hw_interface_t * hi0;
156 u64 * copy_src0, * copy_dst0;
157 u32 * copy_src_last0, * copy_dst_last0;
158 " encap_stack "_tunnel_t * t0;
169 b0 = vlib_get_buffer (vm, bi0);
172 hi0 = vnet_get_sup_hw_interface
173 (vnm, vnet_buffer(b0)->sw_if_index[VLIB_TX]);
175 t0 = pool_elt_at_index (ngm->tunnels, hi0->dev_instance);
177 ASSERT(vec_len(t0->rewrite) >= 24);
179 /* Apply the rewrite string. $$$$ vnet_rewrite? */
180 vlib_buffer_advance (b0, -(word)_vec_len(t0->rewrite));
182 ip0 = vlib_buffer_get_current(b0);
183 /* Copy the fixed header */
184 copy_dst0 = (u64 *) ip0;
185 copy_src0 = (u64 *) t0->rewrite;
187 ASSERT (sizeof (ip4_udp_" encap_stack "_header_t) == FIXME);
189 /* Copy first N octets 8-bytes at a time */
190 #define _(offs) copy_dst0[offs] = copy_src0[offs];
191 foreach_fixed_header_offset;
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 *)(©_dst0[FIXME]);
196 copy_src_last0 = (u32 *)(©_src0[FIXME]);
197 copy_dst_last0[0] = copy_src_last0[0];
200 /* fix the <bleep>ing outer-IP checksum */
201 sum0 = ip0->checksum;
202 /* old_l0 always 0, see the rewrite setup */
204 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
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;
212 udp0 = (udp_header_t *)(ip0+1);
213 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
216 udp0->length = new_l0;
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 ++;
222 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
224 " encap_stack "_encap_trace_t *tr =
225 vlib_add_trace (vm, node, b0, sizeof (*tr));
226 tr->tunnel_index = t0 - ngm->tunnels;
228 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
229 to_next, n_left_to_next,
233 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
235 vlib_node_increment_counter (vm, node->node_index,
236 " ENCAP_STACK "_ENCAP_ERROR_ENCAPSULATED,
238 return from_frame->n_vectors;
241 VLIB_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,
248 .n_errors = ARRAY_LEN(" encap_stack "_encap_error_strings),
249 .error_strings = " encap_stack "_encap_error_strings,
251 .n_next_nodes = " ENCAP_STACK "_ENCAP_N_NEXT,
254 [" ENCAP_STACK "_ENCAP_NEXT_IP4_LOOKUP] = \"ip4-lookup\",
255 [" ENCAP_STACK "_ENCAP_NEXT_DROP] = \"error-drop\",