FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
mpls_input.c
Go to the documentation of this file.
1 /*
2  * node.c: MPLS input
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/mpls/mpls.h>
21 #include <vnet/feature/feature.h>
22 
23 typedef struct {
27 
28 #define foreach_mpls_input_next \
29 _(DROP, "error-drop") \
30 _(LOOKUP, "mpls-lookup")
31 
32 typedef enum {
33 #define _(s,n) MPLS_INPUT_NEXT_##s,
35 #undef _
38 
39 static u8 *
40 format_mpls_input_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_input_trace_t * t = va_arg (*args, mpls_input_trace_t *);
45  char * next_name;
46  u32 label;
47  next_name = "BUG!";
48  label = clib_net_to_host_u32(t->label_net_byte_order);
49 
50 #define _(a,b) if (t->next_index == MPLS_INPUT_NEXT_##a) next_name = b;
52 #undef _
53 
54  s = format (s, "MPLS: next %s[%d] label %d ttl %d exp %d",
55  next_name, t->next_index,
59 
60  return s;
61 }
62 
63 typedef struct {
69 
70 static inline uword
74 {
75  u32 n_left_from, next_index, * from, * to_next;
76  mpls_main_t * mm = &mpls_main;
79  vnet_main_t * vnm = vnet_get_main();
80 
82  n_left_from = from_frame->n_vectors;
83 
84  next_index = node->cached_next_index;
85 
88 
89  while (n_left_from > 0)
90  {
91  u32 n_left_to_next;
92 
94  to_next, n_left_to_next);
95 
96  while (n_left_from >= 4 && n_left_to_next >= 2)
97  {
98  u32 bi0, next0, sw_if_index0;
99  u32 bi1, next1, sw_if_index1;
100  vlib_buffer_t *b0, *b1;
101  char *h0, *h1;
102 
103  /* Prefetch next iteration. */
104  {
105  vlib_buffer_t * p2, * p3;
106 
107  p2 = vlib_get_buffer (vm, from[2]);
108  p3 = vlib_get_buffer (vm, from[3]);
109 
110  vlib_prefetch_buffer_header (p2, LOAD);
111  vlib_prefetch_buffer_header (p3, LOAD);
112 
113  CLIB_PREFETCH (p2->data, sizeof (h0[0]), LOAD);
114  CLIB_PREFETCH (p3->data, sizeof (h1[0]), LOAD);
115  }
116 
117  bi0 = to_next[0] = from[0];
118  bi1 = to_next[1] = from[1];
119 
120  from += 2;
121  to_next += 2;
122  n_left_from -= 2;
123  n_left_to_next -= 2;
124 
125  b0 = vlib_get_buffer (vm, bi0);
126  b1 = vlib_get_buffer (vm, bi1);
127 
128  h0 = vlib_buffer_get_current (b0);
129  h1 = vlib_buffer_get_current (b1);
130 
131  sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
132  sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
133 
134  /* TTL expired? */
135  if (PREDICT_FALSE(h0[3] == 0))
136  {
137  next0 = MPLS_INPUT_NEXT_DROP;
138  b0->error = node->errors[MPLS_ERROR_TTL_EXPIRED];
139  }
140  else
141  {
142  next0 = MPLS_INPUT_NEXT_LOOKUP;
144  sw_if_index0, &next0, b0);
145  vlib_increment_simple_counter (cm, thread_index, sw_if_index0, 1);
146  }
147 
148  if (PREDICT_FALSE(h1[3] == 0))
149  {
150  next1 = MPLS_INPUT_NEXT_DROP;
151  b1->error = node->errors[MPLS_ERROR_TTL_EXPIRED];
152  }
153  else
154  {
155  next1 = MPLS_INPUT_NEXT_LOOKUP;
157  sw_if_index1, &next1, b1);
158  vlib_increment_simple_counter (cm, thread_index, sw_if_index1, 1);
159  }
160 
161  if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
162  {
164  b0, sizeof (*tr));
165  tr->next_index = next0;
166  tr->label_net_byte_order = *((u32*)h0);
167  }
168  if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
169  {
171  b1, sizeof (*tr));
172  tr->next_index = next1;
173  tr->label_net_byte_order = *((u32*)h1);
174  }
175 
177  to_next, n_left_to_next,
178  bi0, bi1,
179  next0, next1);
180  }
181 
182  while (n_left_from > 0 && n_left_to_next > 0)
183  {
184  u32 sw_if_index0, next0, bi0;
185  vlib_buffer_t * b0;
186  char * h0;
187 
188  bi0 = from[0];
189  to_next[0] = bi0;
190  from += 1;
191  to_next += 1;
192  n_left_from -= 1;
193  n_left_to_next -= 1;
194 
195  b0 = vlib_get_buffer (vm, bi0);
196  h0 = vlib_buffer_get_current (b0);
197  sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
198 
199  /* TTL expired? */
200  if (PREDICT_FALSE(h0[3] == 0))
201  {
202  next0 = MPLS_INPUT_NEXT_DROP;
203  b0->error = node->errors[MPLS_ERROR_TTL_EXPIRED];
204  }
205  else
206  {
207  next0 = MPLS_INPUT_NEXT_LOOKUP;
208  vnet_feature_arc_start(mm->input_feature_arc_index, sw_if_index0, &next0, b0);
209  vlib_increment_simple_counter (cm, thread_index, sw_if_index0, 1);
210  }
211 
212  if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
213  {
215  b0, sizeof (*tr));
216  tr->next_index = next0;
217  tr->label_net_byte_order = *(u32*)h0;
218  }
219 
221  to_next, n_left_to_next,
222  bi0, next0);
223  }
224 
225  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
226  }
228  MPLS_ERROR_PKTS_DECAP, from_frame->n_vectors);
229  return from_frame->n_vectors;
230 }
231 
235 {
236  return mpls_input_inline (vm, node, from_frame);
237 }
238 
239 static char * mpls_error_strings[] = {
240 #define mpls_error(n,s) s,
241 #include "error.def"
242 #undef mpls_error
243 };
244 
246  .name = "mpls-input",
247  /* Takes a vector of packets. */
248  .vector_size = sizeof (u32),
249 
250  .runtime_data_bytes = sizeof(mpls_input_runtime_t),
251 
252  .n_errors = MPLS_N_ERROR,
253  .error_strings = mpls_error_strings,
254 
255  .n_next_nodes = MPLS_INPUT_N_NEXT,
256  .next_nodes = {
257 #define _(s,n) [MPLS_INPUT_NEXT_##s] = n,
259 #undef _
260  },
261 
263  .format_trace = format_mpls_input_trace,
264 };
265 
266 #ifndef CLIB_MARCH_VARIANT
267 static void
269 {
270  pg_node_t * pn;
271 
272  pn = pg_get_node (mpls_input_node.index);
274 
275  ethernet_register_input_type (vm, ETHERNET_TYPE_MPLS,
276  mpls_input_node.index);
277 }
278 
280 {
282 
283  return 0;
284 }
285 
286 /* *INDENT-OFF* */
288 {
289  .runs_after = VLIB_INITS("mpls_init"),
290 };
291 /* *INDENT-ON* */
292 #endif /* CLIB_MARCH_VARIANT */
vlib.h
label
u32 label
Definition: fib_types.api:25
thread_index
u32 thread_index
Definition: nat44_ei_hairpinning.c:492
vlib_prefetch_buffer_header
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:231
mpls_input_next_t
mpls_input_next_t
Definition: mpls_input.c:32
next_index
nat44_ei_hairpin_src_next_t next_index
Definition: nat44_ei_hairpinning.c:412
VNET_INTERFACE_COUNTER_MPLS
@ VNET_INTERFACE_COUNTER_MPLS
Definition: interface.h:911
pg.h
vlib_get_buffer
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:111
mpls_input_init
static clib_error_t * mpls_input_init(vlib_main_t *vm)
Definition: mpls_input.c:279
mpls_input_runtime_t
Definition: mpls_input.c:63
node
vlib_main_t vlib_node_runtime_t * node
Definition: nat44_ei.c:3047
mpls_main
mpls_main_t mpls_main
Definition: mpls.c:25
mpls_input_runtime_t::last_inner_fib_index
u32 last_inner_fib_index
Definition: mpls_input.c:65
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
mpls_input_runtime_t::last_outer_fib_index
u32 last_outer_fib_index
Definition: mpls_input.c:66
VLIB_RX
@ VLIB_RX
Definition: defs.h:46
vnet_mpls_uc_get_label
static u32 vnet_mpls_uc_get_label(mpls_label_t label_exp_s_ttl)
Definition: packet.h:105
vnet_interface_main_t::sw_if_counters
vlib_simple_counter_main_t * sw_if_counters
Definition: interface.h:1022
from_frame
vlib_main_t vlib_node_runtime_t vlib_frame_t * from_frame
Definition: esp_encrypt.c:1328
mpls.h
vlib_frame_t
Definition: node.h:372
mpls_input_runtime_t::mpls_main
mpls_main_t * mpls_main
Definition: mpls_input.c:67
format_mpls_input_trace
static u8 * format_mpls_input_trace(u8 *s, va_list *args)
Definition: mpls_input.c:40
mpls_setup_nodes
static void mpls_setup_nodes(vlib_main_t *vm)
Definition: mpls_input.c:268
pg_node_t
Definition: pg.h:324
CLIB_PREFETCH
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:80
vlib_increment_simple_counter
static void vlib_increment_simple_counter(vlib_simple_counter_main_t *cm, u32 thread_index, u32 index, u64 increment)
Increment a simple counter.
Definition: counter.h:74
vlib_buffer_t::error
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:145
mpls_input_trace_t::label_net_byte_order
u32 label_net_byte_order
Definition: mpls_input.c:25
mpls_main_t::input_feature_arc_index
u8 input_feature_arc_index
Definition: mpls.h:56
VLIB_NODE_FN
#define VLIB_NODE_FN(node)
Definition: node.h:202
feature.h
pg_node_t::unformat_edit
unformat_function_t * unformat_edit
Definition: pg.h:327
CLIB_UNUSED
#define CLIB_UNUSED(x)
Definition: clib.h:90
vnet_buffer
#define vnet_buffer(b)
Definition: buffer.h:437
vec_elt_at_index
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
Definition: vec_bootstrap.h:203
vnet_get_main
vnet_main_t * vnet_get_main(void)
Definition: pnat_test_stubs.h:56
PREDICT_FALSE
#define PREDICT_FALSE(x)
Definition: clib.h:124
vlib_get_thread_index
static_always_inline uword vlib_get_thread_index(void)
Definition: threads.h:208
vlib_frame_vector_args
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:301
mpls_input_node
vlib_node_registration_t mpls_input_node
(constructor) VLIB_REGISTER_NODE (mpls_input_node)
Definition: mpls_input.c:245
unformat_pg_mpls_header
unformat_function_t unformat_pg_mpls_header
Definition: mpls.h:85
mpls_input_inline
static uword mpls_input_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
Definition: mpls_input.c:71
uword
u64 uword
Definition: types.h:112
pg_get_node
static pg_node_t * pg_get_node(uword node_index)
Definition: pg.h:381
vlib_node_increment_counter
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1244
cm
vnet_feature_config_main_t * cm
Definition: nat44_ei_hairpinning.c:591
vnet_mpls_uc_get_exp
static u32 vnet_mpls_uc_get_exp(mpls_label_t label_exp_s_ttl)
Definition: packet.h:110
mpls_input_trace_t::next_index
u32 next_index
Definition: mpls_input.c:24
vlib_node_registration_t
struct _vlib_node_registration vlib_node_registration_t
foreach_mpls_input_next
#define foreach_mpls_input_next
Definition: mpls_input.c:28
vnet_main_t
Definition: vnet.h:76
vlib_validate_buffer_enqueue_x1
#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:224
format
description fragment has unexpected format
Definition: map.api:433
vlib_put_next_frame
vlib_put_next_frame(vm, node, next_index, 0)
mpls_error_strings
static char * mpls_error_strings[]
Definition: mpls_input.c:239
u32
unsigned int u32
Definition: types.h:88
VLIB_INIT_FUNCTION
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:172
format_mpls_unicast_header_net_byte_order
format_function_t format_mpls_unicast_header_net_byte_order
Definition: mpls.h:70
MPLS_N_ERROR
@ MPLS_N_ERROR
Definition: mpls.h:31
vlib_main_t
Definition: main.h:102
vlib_node_t
Definition: node.h:247
vlib_simple_counter_main_t
A collection of simple counters.
Definition: counter.h:57
vlib_add_trace
void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace.c:628
VLIB_INITS
#define VLIB_INITS(...)
Definition: init.h:352
u8
unsigned char u8
Definition: types.h:56
clib_error_t
Definition: clib_error.h:21
vlib_buffer_get_current
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:257
vlib_init_function_t
clib_error_t *() vlib_init_function_t(struct vlib_main_t *vm)
Definition: init.h:51
vlib_buffer_t::data
u8 data[]
Packet data.
Definition: buffer.h:204
mpls_main_t
Definition: mpls.h:41
vnet_feature_arc_start
static_always_inline void vnet_feature_arc_start(u8 arc, u32 sw_if_index, u32 *next0, vlib_buffer_t *b0)
Definition: feature.h:302
MPLS_INPUT_N_NEXT
@ MPLS_INPUT_N_NEXT
Definition: mpls_input.c:36
mpls_input_trace_t
Definition: mpls_input.c:23
vnet_mpls_uc_get_ttl
static u32 vnet_mpls_uc_get_ttl(mpls_label_t label_exp_s_ttl)
Definition: packet.h:120
vlib_validate_buffer_enqueue_x2
#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
mpls_input_runtime_t::last_label
u32 last_label
Definition: mpls_input.c:64
vlib_node_runtime_t
Definition: node.h:454
from
from
Definition: nat44_ei_hairpinning.c:415
vlib_get_next_frame
#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:395
n_left_from
n_left_from
Definition: nat44_ei_hairpinning.c:416
ethernet_register_input_type
void ethernet_register_input_type(vlib_main_t *vm, ethernet_type_t type, u32 node_index)
Definition: node.c:2263
vnet_main_t::interface_main
vnet_interface_main_t interface_main
Definition: vnet.h:81
error.def
vlib_buffer_t::flags
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index,...
Definition: buffer.h:133
vlib_buffer_t
VLIB buffer representation.
Definition: buffer.h:111
VLIB_REGISTER_NODE
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169