FD.io VPP  v19.08-27-gf4dcae4
Vector Packet Processing
mpls_output.c
Go to the documentation of this file.
1 /*
2  * mpls_output.c: MPLS Adj rewrite
3  *
4  * Copyright (c) 2012-2014 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 <vlib/vlib.h>
19 #include <vnet/pg/pg.h>
20 #include <vnet/ip/ip.h>
21 #include <vnet/mpls/mpls.h>
22 
23 typedef struct {
24  /* Adjacency taken. */
28 
29 #define foreach_mpls_output_next \
30 _(DROP, "error-drop")
31 
32 typedef enum {
33 #define _(s,n) MPLS_OUTPUT_NEXT_##s,
35 #undef _
38 
39 static u8 *
40 format_mpls_output_trace (u8 * s, va_list * args)
41 {
42  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
43  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
44  mpls_output_trace_t * t = va_arg (*args, mpls_output_trace_t *);
45 
46  s = format (s, "adj-idx %d : %U flow hash: 0x%08x",
47  t->adj_index,
49  t->flow_hash);
50  return s;
51 }
52 
53 static inline uword
55  vlib_node_runtime_t * node,
56  vlib_frame_t * from_frame,
57  int is_midchain)
58 {
59  u32 n_left_from, next_index, * from, * to_next, thread_index;
60  vlib_node_runtime_t * error_node;
61  u32 n_left_to_next;
62  mpls_main_t *mm;
63 
64  thread_index = vlib_get_thread_index();
65  error_node = vlib_node_get_runtime (vm, mpls_output_node.index);
66  from = vlib_frame_vector_args (from_frame);
67  n_left_from = from_frame->n_vectors;
68  next_index = node->cached_next_index;
69  mm = &mpls_main;
70 
71  while (n_left_from > 0)
72  {
73  vlib_get_next_frame (vm, node, next_index,
74  to_next, n_left_to_next);
75 
76  while (n_left_from >= 4 && n_left_to_next >= 2)
77  {
78  ip_adjacency_t * adj0;
80  vlib_buffer_t * p0;
81  u32 pi0, adj_index0, next0, error0;
82  word rw_len0;
83 
84  ip_adjacency_t * adj1;
86  vlib_buffer_t * p1;
87  u32 pi1, adj_index1, next1, error1;
88  word rw_len1;
89 
90  /* Prefetch next iteration. */
91  {
92  vlib_buffer_t * p2, * p3;
93 
94  p2 = vlib_get_buffer (vm, from[2]);
95  p3 = vlib_get_buffer (vm, from[3]);
96 
97  vlib_prefetch_buffer_header (p2, STORE);
98  vlib_prefetch_buffer_header (p3, STORE);
99 
100  CLIB_PREFETCH (p2->data, sizeof (hdr0[0]), STORE);
101  CLIB_PREFETCH (p3->data, sizeof (hdr1[0]), STORE);
102  }
103 
104  pi0 = to_next[0] = from[0];
105  pi1 = to_next[1] = from[1];
106 
107  from += 2;
108  n_left_from -= 2;
109  to_next += 2;
110  n_left_to_next -= 2;
111 
112  p0 = vlib_get_buffer (vm, pi0);
113  p1 = vlib_get_buffer (vm, pi1);
114 
115  adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
116  adj_index1 = vnet_buffer (p1)->ip.adj_index[VLIB_TX];
117 
118  adj0 = adj_get(adj_index0);
119  adj1 = adj_get(adj_index1);
120  hdr0 = vlib_buffer_get_current (p0);
121  hdr1 = vlib_buffer_get_current (p1);
122 
123  /* Guess we are only writing on simple Ethernet header. */
124  vnet_rewrite_two_headers (adj0[0], adj1[0], hdr0, hdr1,
125  sizeof (ethernet_header_t));
126 
127  /* Update packet buffer attributes/set output interface. */
128  rw_len0 = adj0[0].rewrite_header.data_bytes;
129  rw_len1 = adj1[0].rewrite_header.data_bytes;
130  vnet_buffer (p0)->mpls.save_rewrite_length = rw_len0;
131  vnet_buffer (p1)->mpls.save_rewrite_length = rw_len1;
132 
133  /* Bump the adj counters for packet and bytes */
136  thread_index,
137  adj_index0,
138  1,
139  vlib_buffer_length_in_chain (vm, p0) + rw_len0);
142  thread_index,
143  adj_index1,
144  1,
145  vlib_buffer_length_in_chain (vm, p1) + rw_len1);
146 
147  /* Check MTU of outgoing interface. */
149  adj0[0].rewrite_header.max_l3_packet_bytes))
150  {
151  vlib_buffer_advance(p0, -rw_len0);
152 
153  vnet_buffer (p0)->sw_if_index[VLIB_TX] =
154  adj0[0].rewrite_header.sw_if_index;
155  next0 = adj0[0].rewrite_header.next_index;
156  error0 = IP4_ERROR_NONE;
157 
158  if (PREDICT_FALSE(adj0[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
160  adj0[0].rewrite_header.sw_if_index,
161  &next0, p0);
162  }
163  else
164  {
165  error0 = IP4_ERROR_MTU_EXCEEDED;
166  next0 = MPLS_OUTPUT_NEXT_DROP;
167  }
169  adj1[0].rewrite_header.max_l3_packet_bytes))
170  {
171  vlib_buffer_advance(p1, -rw_len1);
172 
173  vnet_buffer (p1)->sw_if_index[VLIB_TX] =
174  adj1[0].rewrite_header.sw_if_index;
175  next1 = adj1[0].rewrite_header.next_index;
176  error1 = IP4_ERROR_NONE;
177 
178  if (PREDICT_FALSE(adj1[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
180  adj1[0].rewrite_header.sw_if_index,
181  &next1, p1);
182  }
183  else
184  {
185  error1 = IP4_ERROR_MTU_EXCEEDED;
186  next1 = MPLS_OUTPUT_NEXT_DROP;
187  }
188  if (is_midchain)
189  {
190  adj0->sub_type.midchain.fixup_func
191  (vm, adj0, p0,
192  adj0->sub_type.midchain.fixup_data);
193  adj1->sub_type.midchain.fixup_func
194  (vm, adj1, p1,
195  adj1->sub_type.midchain.fixup_data);
196  }
197 
198  p0->error = error_node->errors[error0];
199  p1->error = error_node->errors[error1];
200 
201  if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED))
202  {
203  mpls_output_trace_t *tr = vlib_add_trace (vm, node,
204  p0, sizeof (*tr));
205  tr->adj_index = vnet_buffer(p0)->ip.adj_index[VLIB_TX];
206  tr->flow_hash = vnet_buffer(p0)->ip.flow_hash;
207  }
208  if (PREDICT_FALSE(p1->flags & VLIB_BUFFER_IS_TRACED))
209  {
210  mpls_output_trace_t *tr = vlib_add_trace (vm, node,
211  p1, sizeof (*tr));
212  tr->adj_index = vnet_buffer(p1)->ip.adj_index[VLIB_TX];
213  tr->flow_hash = vnet_buffer(p1)->ip.flow_hash;
214  }
215 
216  vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
217  to_next, n_left_to_next,
218  pi0, pi1, next0, next1);
219  }
220 
221  while (n_left_from > 0 && n_left_to_next > 0)
222  {
223  ip_adjacency_t * adj0;
224  mpls_unicast_header_t *hdr0;
225  vlib_buffer_t * p0;
226  u32 pi0, adj_index0, next0, error0;
227  word rw_len0;
228 
229  pi0 = to_next[0] = from[0];
230 
231  p0 = vlib_get_buffer (vm, pi0);
232 
233  adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
234 
235  adj0 = adj_get(adj_index0);
236  hdr0 = vlib_buffer_get_current (p0);
237 
238  /* Guess we are only writing on simple Ethernet header. */
239  vnet_rewrite_one_header (adj0[0], hdr0,
240  sizeof (ethernet_header_t));
241 
242  /* Update packet buffer attributes/set output interface. */
243  rw_len0 = adj0[0].rewrite_header.data_bytes;
244  vnet_buffer (p0)->mpls.save_rewrite_length = rw_len0;
245 
248  thread_index,
249  adj_index0,
250  1,
251  vlib_buffer_length_in_chain (vm, p0) + rw_len0);
252 
253  /* Check MTU of outgoing interface. */
255  adj0[0].rewrite_header.max_l3_packet_bytes))
256  {
257  vlib_buffer_advance(p0, -rw_len0);
258 
259  vnet_buffer (p0)->sw_if_index[VLIB_TX] =
260  adj0[0].rewrite_header.sw_if_index;
261  next0 = adj0[0].rewrite_header.next_index;
262  error0 = IP4_ERROR_NONE;
263 
264  if (PREDICT_FALSE(adj0[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
266  adj0[0].rewrite_header.sw_if_index,
267  &next0, p0);
268  }
269  else
270  {
271  error0 = IP4_ERROR_MTU_EXCEEDED;
272  next0 = MPLS_OUTPUT_NEXT_DROP;
273  }
274  if (is_midchain)
275  {
276  adj0->sub_type.midchain.fixup_func
277  (vm, adj0, p0,
278  adj0->sub_type.midchain.fixup_data);
279  }
280 
281  p0->error = error_node->errors[error0];
282 
283  from += 1;
284  n_left_from -= 1;
285  to_next += 1;
286  n_left_to_next -= 1;
287 
288  if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED))
289  {
290  mpls_output_trace_t *tr = vlib_add_trace (vm, node,
291  p0, sizeof (*tr));
292  tr->adj_index = vnet_buffer(p0)->ip.adj_index[VLIB_TX];
293  tr->flow_hash = vnet_buffer(p0)->ip.flow_hash;
294  }
295 
296  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
297  to_next, n_left_to_next,
298  pi0, next0);
299  }
300 
301  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
302  }
304  MPLS_ERROR_PKTS_ENCAP,
305  from_frame->n_vectors);
306 
307  return from_frame->n_vectors;
308 }
309 
310 static char * mpls_error_strings[] = {
311 #define mpls_error(n,s) s,
312 #include "error.def"
313 #undef mpls_error
314 };
315 
317  vlib_node_runtime_t * node,
318  vlib_frame_t * from_frame)
319 {
320  return (mpls_output_inline(vm, node, from_frame, /* is_midchain */ 0));
321 }
322 
324  .name = "mpls-output",
325  /* Takes a vector of packets. */
326  .vector_size = sizeof (u32),
327  .n_errors = MPLS_N_ERROR,
328  .error_strings = mpls_error_strings,
329 
330  .n_next_nodes = MPLS_OUTPUT_N_NEXT,
331  .next_nodes = {
332 #define _(s,n) [MPLS_OUTPUT_NEXT_##s] = n,
334 #undef _
335  },
336 
337  .format_trace = format_mpls_output_trace,
338 };
339 
341  vlib_node_runtime_t * node,
342  vlib_frame_t * from_frame)
343 {
344  return (mpls_output_inline(vm, node, from_frame, /* is_midchain */ 1));
345 }
346 
348  .name = "mpls-midchain",
349  .vector_size = sizeof (u32),
350 
351  .format_trace = format_mpls_output_trace,
352 
353  .sibling_of = "mpls-output",
354 };
355 
356 /**
357  * @brief Next index values from the MPLS incomplete adj node
358  */
359 #define foreach_mpls_adj_incomplete_next \
360 _(DROP, "error-drop") \
361 _(IP4, "ip4-arp") \
362 _(IP6, "ip6-discover-neighbor")
363 
364 typedef enum {
365 #define _(s,n) MPLS_ADJ_INCOMPLETE_NEXT_##s,
367 #undef _
370 
371 /**
372  * @brief A struct to hold tracing information for the MPLS label imposition
373  * node.
374  */
376 {
379 
380 
381 /**
382  * @brief Graph node for incomplete MPLS adjacency.
383  * This node will push traffic to either the v4-arp or v6-nd node
384  * based on the next-hop proto of the adj.
385  * We pay a cost for this 'routing' node, but an incomplete adj is the
386  * exception case.
387  */
389  vlib_node_runtime_t * node,
390  vlib_frame_t * from_frame)
391 {
392  u32 n_left_from, next_index, * from, * to_next;
393 
394  from = vlib_frame_vector_args (from_frame);
395  n_left_from = from_frame->n_vectors;
396  next_index = node->cached_next_index;
397 
398  while (n_left_from > 0)
399  {
400  u32 n_left_to_next;
401 
402  vlib_get_next_frame (vm, node, next_index,
403  to_next, n_left_to_next);
404 
405  while (n_left_from > 0 && n_left_to_next > 0)
406  {
407  u32 pi0, next0, adj_index0;
408  ip_adjacency_t * adj0;
409  vlib_buffer_t * p0;
410 
411  pi0 = to_next[0] = from[0];
412  p0 = vlib_get_buffer (vm, pi0);
413  from += 1;
414  n_left_from -= 1;
415  to_next += 1;
416  n_left_to_next -= 1;
417 
418  adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
419 
420  adj0 = adj_get(adj_index0);
421 
423  {
424  next0 = MPLS_ADJ_INCOMPLETE_NEXT_IP4;
425  }
426  else
427  {
428  next0 = MPLS_ADJ_INCOMPLETE_NEXT_IP6;
429  }
430 
431  if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED))
432  {
434  vlib_add_trace (vm, node, p0, sizeof (*tr));
435  tr->next = next0;
436  }
437 
438  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
439  to_next, n_left_to_next,
440  pi0, next0);
441  }
442 
443  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
444  }
445 
446  return from_frame->n_vectors;
447 }
448 
449 static u8 *
451 {
452  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
453  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
455  u32 indent;
456 
457  t = va_arg (*args, mpls_adj_incomplete_trace_t *);
458  indent = format_get_indent (s);
459 
460  s = format (s, "%Unext:%d",
461  format_white_space, indent,
462  t->next);
463  return (s);
464 }
465 
467  .name = "mpls-adj-incomplete",
468  .format_trace = format_mpls_adj_incomplete_trace,
469  /* Takes a vector of packets. */
470  .vector_size = sizeof (u32),
471  .n_errors = MPLS_N_ERROR,
472  .error_strings = mpls_error_strings,
473 
474  .n_next_nodes = MPLS_ADJ_INCOMPLETE_N_NEXT,
475  .next_nodes = {
476 #define _(s,n) [MPLS_ADJ_INCOMPLETE_NEXT_##s] = n,
478 #undef _
479  },
480 };
481 
#define vnet_rewrite_one_header(rw0, p0, most_likely_size)
Definition: rewrite.h:198
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:124
#define foreach_mpls_output_next
Definition: mpls_output.c:29
#define CLIB_UNUSED(x)
Definition: clib.h:82
static void vlib_increment_combined_counter(vlib_combined_counter_main_t *cm, u32 thread_index, u32 index, u64 n_packets, u64 n_bytes)
Increment a combined counter.
Definition: counter.h:220
#define PREDICT_TRUE(x)
Definition: clib.h:112
IP unicast adjacency.
Definition: adj.h:221
A struct to hold tracing information for the MPLS label imposition node.
Definition: mpls_output.c:375
u8 data[0]
Packet data.
Definition: buffer.h:181
static u8 * format_mpls_adj_incomplete_trace(u8 *s, va_list *args)
Definition: mpls_output.c:450
static u32 format_get_indent(u8 *s)
Definition: format.h:72
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
#define VLIB_NODE_FN(node)
Definition: node.h:201
vlib_error_t * errors
Vector of errors for this node.
Definition: node.h:468
static uword vlib_buffer_length_in_chain(vlib_main_t *vm, vlib_buffer_t *b)
Get length in bytes of the buffer chain.
Definition: buffer_funcs.h:366
unsigned char u8
Definition: types.h:56
static u8 * format_mpls_output_trace(u8 *s, va_list *args)
Definition: mpls_output.c:40
vlib_node_registration_t mpls_midchain_node
(constructor) VLIB_REGISTER_NODE (mpls_midchain_node)
Definition: mpls_output.c:347
union ip_adjacency_t_::@48 sub_type
vlib_node_registration_t mpls_output_node
(constructor) VLIB_REGISTER_NODE (mpls_output_node)
Definition: mpls_output.c:323
static ip_adjacency_t * adj_get(adj_index_t adj_index)
Get a pointer to an adjacency object from its index.
Definition: adj.h:433
i64 word
Definition: types.h:111
format_function_t format_ip_adjacency
Definition: format.h:58
u8 * format_white_space(u8 *s, va_list *va)
Definition: std-formats.c:129
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:203
mpls_adj_incomplete_next_t
Definition: mpls_output.c:364
mpls_output_next_t
Definition: mpls_output.c:32
unsigned int u32
Definition: types.h:88
struct ip_adjacency_t_::@48::@50 midchain
IP_LOOKUP_NEXT_MIDCHAIN.
u8 output_feature_arc_index
Definition: mpls.h:57
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:136
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:229
#define PREDICT_FALSE(x)
Definition: clib.h:111
#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:338
vlib_combined_counter_main_t adjacency_counters
Adjacency packet counters.
Definition: adj.c:25
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1150
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169
u16 n_vectors
Definition: node.h:395
mpls_main_t mpls_main
Definition: mpls.c:25
static_always_inline uword vlib_get_thread_index(void)
Definition: threads.h:213
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:80
vlib_main_t * vm
Definition: buffer.c:312
#define foreach_mpls_adj_incomplete_next
Next index values from the MPLS incomplete adj node.
Definition: mpls_output.c:359
static vlib_node_runtime_t * vlib_node_get_runtime(vlib_main_t *vm, u32 node_index)
Get node runtime by node index.
Definition: node_funcs.h:89
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:458
u16 cached_next_index
Next frame index that vector arguments were last enqueued to last time this node ran.
Definition: node.h:513
static uword mpls_output_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame, int is_midchain)
Definition: mpls_output.c:54
static void vlib_buffer_advance(vlib_buffer_t *b, word l)
Advance current data pointer by the supplied (signed!) amount.
Definition: buffer.h:248
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
static char * mpls_error_strings[]
Definition: mpls_output.c:310
fib_protocol_t ia_nh_proto
The protocol of the neighbor/peer.
Definition: adj.h:249
Definition: defs.h:47
VLIB buffer representation.
Definition: buffer.h:102
u64 uword
Definition: types.h:112
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:244
#define vnet_buffer(b)
Definition: buffer.h:361
struct mpls_adj_incomplete_trace_t_ mpls_adj_incomplete_trace_t
A struct to hold tracing information for the MPLS label imposition node.
vlib_node_registration_t mpls_adj_incomplete_node
(constructor) VLIB_REGISTER_NODE (mpls_adj_incomplete_node)
Definition: mpls_output.c:466
#define vnet_rewrite_two_headers(rw0, rw1, p0, p1, most_likely_size)
Definition: rewrite.h:202
This adjacency/interface has output features configured.
Definition: rewrite.h:57
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:85
static_always_inline void vnet_feature_arc_start(u8 arc, u32 sw_if_index, u32 *next0, vlib_buffer_t *b0)
Definition: feature.h:275