FD.io VPP  v18.01.2-1-g9b554f3
Vector Packet Processing
udp_encap_node.c
Go to the documentation of this file.
1 /*
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 
18 typedef struct udp4_encap_trace_t_
19 {
23 
24 typedef struct udp6_encap_trace_t_
25 {
29 
30 static u8 *
31 format_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 *);
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 
45 static u8 *
46 format_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 *);
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 
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);
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  }
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 
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  }
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 
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 
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 
235 static uword
237  vlib_node_runtime_t * node, vlib_frame_t * frame)
238 {
239  return udp_encap_inline (vm, node, frame, 0);
240 }
241 
242 static uword
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* */
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 };
260 
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 };
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  */
The UDP encap represenation.
Definition: udp_encap.h:46
#define CLIB_UNUSED(x)
Definition: clib.h:79
vlib_node_registration_t udp6_encap_node
(constructor) VLIB_REGISTER_NODE (udp6_encap_node)
format_function_t format_udp_header
Definition: format.h:102
struct udp6_encap_trace_t_ udp6_encap_trace_t
format_function_t format_ip4_header
Definition: format.h:86
dpo_id_t ue_dpo
The DPO used to forward to the next node in the VLIB graph.
Definition: udp_encap.h:78
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
static uword udp6_encap(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
struct udp_encap_t_::@293::@295 ip6
#define always_inline
Definition: clib.h:92
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:171
static uword udp4_encap(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
#define PREDICT_FALSE(x)
Definition: clib.h:105
static uword udp_encap_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame, int is_encap_v6)
static void ip_udp_encap_one(vlib_main_t *vm, vlib_buffer_t *b0, u8 *ec0, word ec_len, u8 is_ip4)
Definition: udp.h:323
#define vlib_validate_buffer_enqueue_x2(vm, node, next_index, to_next, n_left_to_next, bi0, bi1, next0, next1)
Finish enqueueing two buffers forward in the graph.
Definition: buffer_node.h:70
#define vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next, n_left_to_next, bi0, next0)
Finish enqueueing one buffer forward in the graph.
Definition: buffer_node.h:218
#define vlib_get_next_frame(vm, node, next_index, vectors, n_vectors_left)
Get pointer to next frame vector data by (vlib_node_runtime_t, next_index).
Definition: node_funcs.h:364
static void ip_udp_encap_two(vlib_main_t *vm, vlib_buffer_t *b0, vlib_buffer_t *b1, u8 *ec0, u8 *ec1, word ec_len, u8 is_v4)
Definition: udp.h:351
u16 n_vectors
Definition: node.h:344
vlib_main_t * vm
Definition: buffer.c:283
#define VLIB_BUFFER_IS_TRACED
Definition: buffer.h:93
void vlib_put_next_frame(vlib_main_t *vm, vlib_node_runtime_t *r, u32 next_index, u32 n_vectors_left)
Release pointer to next frame vector data.
Definition: main.c:454
u16 cached_next_index
Next frame index that vector arguments were last enqueued to last time this node ran.
Definition: node.h:456
struct udp4_encap_trace_t_ udp4_encap_trace_t
unsigned int u32
Definition: types.h:88
format_function_t format_ip6_header
Definition: format.h:98
u64 uword
Definition: types.h:112
static void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace_funcs.h:55
struct udp_encap_t_::@293::@294 ip4
Definition: defs.h:47
index_t dpoi_index
the index of objects of that type
Definition: dpo.h:182
unsigned char u8
Definition: types.h:56
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:267
union udp_encap_t_::@293 ue_hdrs
The headers to paint, in packet painting order.
#define vnet_buffer(b)
Definition: buffer.h:326
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:143
VLIB_NODE_FUNCTION_MULTIARCH(udp4_encap_node, udp4_encap)
u16 dpoi_next_node
The next VLIB node to follow.
Definition: dpo.h:178
static u8 * format_udp6_encap_trace(u8 *s, va_list *args)
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index, VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:75
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:57
vlib_node_registration_t udp4_encap_node
(constructor) VLIB_REGISTER_NODE (udp4_encap_node)
static udp_encap_t * udp_encap_get(index_t uei)
Definition: udp_encap.h:134
static u8 * format_udp4_encap_trace(u8 *s, va_list *args)