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