blob: 09a76b530f69248cfca45f569c4cf3c23237824e [file] [log] [blame]
Neale Ranns810086d2017-11-05 16:26:46 -08001/*
2 * Copyright (c) 2017 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <vnet/udp/udp_encap.h>
17
18typedef struct udp4_encap_trace_t_
19{
20 udp_header_t udp;
21 ip4_header_t ip;
22} udp4_encap_trace_t;
23
24typedef struct udp6_encap_trace_t_
25{
26 udp_header_t udp;
27 ip6_header_t ip;
28} udp6_encap_trace_t;
29
30static u8 *
31format_udp4_encap_trace (u8 * s, va_list * args)
32{
33 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
34 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
35 udp4_encap_trace_t *t;
36
37 t = va_arg (*args, udp4_encap_trace_t *);
38
39 s = format (s, "%U\n %U",
40 format_ip4_header, &t->ip, sizeof (t->ip),
41 format_udp_header, &t->udp, sizeof (t->udp));
42 return (s);
43}
44
45static u8 *
46format_udp6_encap_trace (u8 * s, va_list * args)
47{
48 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
49 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
50 udp6_encap_trace_t *t;
51
52 t = va_arg (*args, udp6_encap_trace_t *);
53
54 s = format (s, "%U\n %U",
55 format_ip6_header, &t->ip, sizeof (t->ip),
56 format_udp_header, &t->udp, sizeof (t->udp));
57 return (s);
58}
59
60always_inline uword
61udp_encap_inline (vlib_main_t * vm,
62 vlib_node_runtime_t * node,
63 vlib_frame_t * frame, int is_encap_v6)
64{
65 u32 *from = vlib_frame_vector_args (frame);
66 u32 n_left_from, n_left_to_next, *to_next, next_index;
67
68 n_left_from = frame->n_vectors;
69 next_index = node->cached_next_index;
70
71 while (n_left_from > 0)
72 {
73 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
74
75 while (n_left_from >= 4 && n_left_to_next >= 2)
76 {
77 vlib_buffer_t *b0, *b1;
78 udp_encap_t *ue0, *ue1;
79 u32 bi0, next0, uei0;
80 u32 bi1, next1, uei1;
81
82 /* Prefetch next iteration. */
83 {
84 vlib_buffer_t *p2, *p3;
85
86 p2 = vlib_get_buffer (vm, from[2]);
87 p3 = vlib_get_buffer (vm, from[3]);
88
89 vlib_prefetch_buffer_header (p2, STORE);
90 vlib_prefetch_buffer_header (p3, STORE);
91 }
92
93 bi0 = to_next[0] = from[0];
94 bi1 = to_next[1] = from[1];
95
96 from += 2;
97 n_left_from -= 2;
98 to_next += 2;
99 n_left_to_next -= 2;
100
101 b0 = vlib_get_buffer (vm, bi0);
102 b1 = vlib_get_buffer (vm, bi1);
103
104 uei0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
105 uei1 = vnet_buffer (b1)->ip.adj_index[VLIB_TX];
106
107 /* Rewrite packet header and updates lengths. */
108 ue0 = udp_encap_get (uei0);
109 ue1 = udp_encap_get (uei1);
110
111 /* Paint */
112 if (is_encap_v6)
113 {
114 const u8 n_bytes =
115 sizeof (udp_header_t) + sizeof (ip6_header_t);
116 ip_udp_encap_two (vm, b0, b1, (u8 *) & ue0->ue_hdrs,
117 (u8 *) & ue1->ue_hdrs, n_bytes, 0);
118 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
119 {
120 udp6_encap_trace_t *tr =
121 vlib_add_trace (vm, node, b0, sizeof (*tr));
122 tr->udp = ue0->ue_hdrs.ip6.ue_udp;
123 tr->ip = ue0->ue_hdrs.ip6.ue_ip6;
124 }
125 if (PREDICT_FALSE (b1->flags & VLIB_BUFFER_IS_TRACED))
126 {
127 udp6_encap_trace_t *tr =
128 vlib_add_trace (vm, node, b1, sizeof (*tr));
129 tr->udp = ue1->ue_hdrs.ip6.ue_udp;
130 tr->ip = ue1->ue_hdrs.ip6.ue_ip6;
131 }
132 }
133 else
134 {
135 const u8 n_bytes =
136 sizeof (udp_header_t) + sizeof (ip4_header_t);
137
138 ip_udp_encap_two (vm, b0, b1,
139 (u8 *) & ue0->ue_hdrs,
140 (u8 *) & ue1->ue_hdrs, n_bytes, 1);
141
142 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
143 {
144 udp4_encap_trace_t *tr =
145 vlib_add_trace (vm, node, b0, sizeof (*tr));
146 tr->udp = ue0->ue_hdrs.ip4.ue_udp;
147 tr->ip = ue0->ue_hdrs.ip4.ue_ip4;
148 }
149 if (PREDICT_FALSE (b1->flags & VLIB_BUFFER_IS_TRACED))
150 {
151 udp4_encap_trace_t *tr =
152 vlib_add_trace (vm, node, b1, sizeof (*tr));
153 tr->udp = ue1->ue_hdrs.ip4.ue_udp;
154 tr->ip = ue1->ue_hdrs.ip4.ue_ip4;
155 }
156 }
157
158 next0 = ue0->ue_dpo.dpoi_next_node;
159 next1 = ue1->ue_dpo.dpoi_next_node;
160 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = ue0->ue_dpo.dpoi_index;
161 vnet_buffer (b1)->ip.adj_index[VLIB_TX] = ue1->ue_dpo.dpoi_index;
162
163 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
164 to_next, n_left_to_next,
165 bi0, bi1, next0, next1);
166 }
167
168 while (n_left_from > 0 && n_left_to_next > 0)
169 {
170 u32 bi0, next0, uei0;
171 vlib_buffer_t *b0;
172 udp_encap_t *ue0;
173
174 bi0 = to_next[0] = from[0];
175
176 from += 1;
177 n_left_from -= 1;
178 to_next += 1;
179 n_left_to_next -= 1;
180
181 b0 = vlib_get_buffer (vm, bi0);
182
183 uei0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
184
185 /* Rewrite packet header and updates lengths. */
186 ue0 = udp_encap_get (uei0);
187
188 /* Paint */
189 if (is_encap_v6)
190 {
191 const u8 n_bytes =
192 sizeof (udp_header_t) + sizeof (ip6_header_t);
193 ip_udp_encap_one (vm, b0, (u8 *) & ue0->ue_hdrs.ip6, n_bytes,
194 0);
195
196 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
197 {
198 udp6_encap_trace_t *tr =
199 vlib_add_trace (vm, node, b0, sizeof (*tr));
200 tr->udp = ue0->ue_hdrs.ip6.ue_udp;
201 tr->ip = ue0->ue_hdrs.ip6.ue_ip6;
202 }
203 }
204 else
205 {
206 const u8 n_bytes =
207 sizeof (udp_header_t) + sizeof (ip4_header_t);
208
209 ip_udp_encap_one (vm, b0, (u8 *) & ue0->ue_hdrs.ip4, n_bytes,
210 1);
211
212 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
213 {
214 udp4_encap_trace_t *tr =
215 vlib_add_trace (vm, node, b0, sizeof (*tr));
216 tr->udp = ue0->ue_hdrs.ip4.ue_udp;
217 tr->ip = ue0->ue_hdrs.ip4.ue_ip4;
218 }
219 }
220
221 next0 = ue0->ue_dpo.dpoi_next_node;
222 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = ue0->ue_dpo.dpoi_index;
223
224 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
225 to_next, n_left_to_next,
226 bi0, next0);
227 }
228
229 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
230 }
231
232 return frame->n_vectors;
233}
234
235static uword
236udp4_encap (vlib_main_t * vm,
237 vlib_node_runtime_t * node, vlib_frame_t * frame)
238{
239 return udp_encap_inline (vm, node, frame, 0);
240}
241
242static uword
243udp6_encap (vlib_main_t * vm,
244 vlib_node_runtime_t * node, vlib_frame_t * frame)
245{
246 return udp_encap_inline (vm, node, frame, 1);
247}
248
249/* *INDENT-OFF* */
250VLIB_REGISTER_NODE (udp4_encap_node) = {
251 .function = udp4_encap,
252 .name = "udp4-encap",
253 .vector_size = sizeof (u32),
254
255 .format_trace = format_udp4_encap_trace,
256
257 .n_next_nodes = 0,
258};
259VLIB_NODE_FUNCTION_MULTIARCH (udp4_encap_node, udp4_encap);
260
261VLIB_REGISTER_NODE (udp6_encap_node) = {
262 .function = udp6_encap,
263 .name = "udp6-encap",
264 .vector_size = sizeof (u32),
265
266 .format_trace = format_udp6_encap_trace,
267
268 .n_next_nodes = 0,
269};
270VLIB_NODE_FUNCTION_MULTIARCH (udp6_encap_node, udp6_encap);
271/* *INDENT-ON* */
272
273
274/*
275 * fd.io coding-style-patch-verification: ON
276 *
277 * Local Variables:
278 * eval: (c-set-style "gnu")
279 * End:
280 */