FD.io VPP  v18.04-17-g3a0d853
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,
57  vnet_mpls_uc_get_ttl(label),
58  vnet_mpls_uc_get_exp(label));
59 
60  return s;
61 }
62 
64 
65 typedef struct {
71 
72 static inline uword
74  vlib_node_runtime_t * node,
75  vlib_frame_t * from_frame)
76 {
77  u32 n_left_from, next_index, * from, * to_next;
78  mpls_main_t * mm = &mpls_main;
79  u32 thread_index = vlib_get_thread_index();
81  vnet_main_t * vnm = vnet_get_main();
82 
83  from = vlib_frame_vector_args (from_frame);
84  n_left_from = from_frame->n_vectors;
85 
86  next_index = node->cached_next_index;
87 
90 
91  while (n_left_from > 0)
92  {
93  u32 n_left_to_next;
94 
95  vlib_get_next_frame (vm, node, next_index,
96  to_next, n_left_to_next);
97 
98  while (n_left_from >= 4 && n_left_to_next >= 2)
99  {
100  u32 bi0, next0, sw_if_index0;
101  u32 bi1, next1, sw_if_index1;
102  vlib_buffer_t *b0, *b1;
103  char *h0, *h1;
104 
105  /* Prefetch next iteration. */
106  {
107  vlib_buffer_t * p2, * p3;
108 
109  p2 = vlib_get_buffer (vm, from[2]);
110  p3 = vlib_get_buffer (vm, from[3]);
111 
112  vlib_prefetch_buffer_header (p2, STORE);
113  vlib_prefetch_buffer_header (p3, STORE);
114 
115  CLIB_PREFETCH (p2->data, sizeof (h0[0]), STORE);
116  CLIB_PREFETCH (p3->data, sizeof (h1[0]), STORE);
117  }
118 
119  bi0 = to_next[0] = from[0];
120  bi1 = to_next[1] = from[1];
121 
122  from += 2;
123  to_next += 2;
124  n_left_from -= 2;
125  n_left_to_next -= 2;
126 
127  b0 = vlib_get_buffer (vm, bi0);
128  b1 = vlib_get_buffer (vm, bi1);
129 
130  h0 = vlib_buffer_get_current (b0);
131  h1 = vlib_buffer_get_current (b1);
132 
133  sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
134  sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
135 
136  /* TTL expired? */
137  if (PREDICT_FALSE(h0[3] == 0))
138  {
139  next0 = MPLS_INPUT_NEXT_DROP;
140  b0->error = node->errors[MPLS_ERROR_TTL_EXPIRED];
141  }
142  else
143  {
144  next0 = MPLS_INPUT_NEXT_LOOKUP;
146  sw_if_index0, &next0, b0);
147  vlib_increment_simple_counter (cm, thread_index, sw_if_index0, 1);
148  }
149 
150  if (PREDICT_FALSE(h1[3] == 0))
151  {
152  next1 = MPLS_INPUT_NEXT_DROP;
153  b1->error = node->errors[MPLS_ERROR_TTL_EXPIRED];
154  }
155  else
156  {
157  next1 = MPLS_INPUT_NEXT_LOOKUP;
159  sw_if_index1, &next1, b1);
160  vlib_increment_simple_counter (cm, thread_index, sw_if_index1, 1);
161  }
162 
163  if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
164  {
165  mpls_input_trace_t *tr = vlib_add_trace (vm, node,
166  b0, sizeof (*tr));
167  tr->next_index = next0;
168  tr->label_net_byte_order = *((u32*)h0);
169  }
170  if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
171  {
172  mpls_input_trace_t *tr = vlib_add_trace (vm, node,
173  b1, sizeof (*tr));
174  tr->next_index = next1;
175  tr->label_net_byte_order = *((u32*)h1);
176  }
177 
178  vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
179  to_next, n_left_to_next,
180  bi0, bi1,
181  next0, next1);
182  }
183 
184  while (n_left_from > 0 && n_left_to_next > 0)
185  {
186  u32 sw_if_index0, next0, bi0;
187  vlib_buffer_t * b0;
188  char * h0;
189 
190  bi0 = from[0];
191  to_next[0] = bi0;
192  from += 1;
193  to_next += 1;
194  n_left_from -= 1;
195  n_left_to_next -= 1;
196 
197  b0 = vlib_get_buffer (vm, bi0);
198  h0 = vlib_buffer_get_current (b0);
199  sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
200 
201  /* TTL expired? */
202  if (PREDICT_FALSE(h0[3] == 0))
203  {
204  next0 = MPLS_INPUT_NEXT_DROP;
205  b0->error = node->errors[MPLS_ERROR_TTL_EXPIRED];
206  }
207  else
208  {
209  next0 = MPLS_INPUT_NEXT_LOOKUP;
210  vnet_feature_arc_start(mm->input_feature_arc_index, sw_if_index0, &next0, b0);
211  vlib_increment_simple_counter (cm, thread_index, sw_if_index0, 1);
212  }
213 
214  if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
215  {
216  mpls_input_trace_t *tr = vlib_add_trace (vm, node,
217  b0, sizeof (*tr));
218  tr->next_index = next0;
219  tr->label_net_byte_order = *(u32*)h0;
220  }
221 
222  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
223  to_next, n_left_to_next,
224  bi0, next0);
225  }
226 
227  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
228  }
230  MPLS_ERROR_PKTS_DECAP, from_frame->n_vectors);
231  return from_frame->n_vectors;
232 }
233 
234 static uword
236  vlib_node_runtime_t * node,
237  vlib_frame_t * from_frame)
238 {
239  return mpls_input_inline (vm, node, from_frame);
240 }
241 
242 static char * mpls_error_strings[] = {
243 #define mpls_error(n,s) s,
244 #include "error.def"
245 #undef mpls_error
246 };
247 
249  .function = mpls_input,
250  .name = "mpls-input",
251  /* Takes a vector of packets. */
252  .vector_size = sizeof (u32),
253 
254  .runtime_data_bytes = sizeof(mpls_input_runtime_t),
255 
256  .n_errors = MPLS_N_ERROR,
257  .error_strings = mpls_error_strings,
258 
259  .n_next_nodes = MPLS_INPUT_N_NEXT,
260  .next_nodes = {
261 #define _(s,n) [MPLS_INPUT_NEXT_##s] = n,
263 #undef _
264  },
265 
267  .format_trace = format_mpls_input_trace,
268 };
269 
271 
272 static void
274 {
275  pg_node_t * pn;
276 
277  pn = pg_get_node (mpls_input_node.index);
279 
280  ethernet_register_input_type (vm, ETHERNET_TYPE_MPLS,
281  mpls_input_node.index);
282 }
283 
285 {
286  clib_error_t * error;
287 
288  error = vlib_call_init_function (vm, mpls_init);
289  if (error)
290  clib_error_report (error);
291 
292  mpls_setup_nodes (vm);
293 
294  return 0;
295 }
296 
#define CLIB_UNUSED(x)
Definition: clib.h:79
vnet_main_t * vnet_get_main(void)
Definition: misc.c:47
vnet_interface_main_t interface_main
Definition: vnet.h:56
u8 input_feature_arc_index
Definition: mpls.h:56
static char * mpls_error_strings[]
Definition: mpls_input.c:242
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
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:78
vlib_error_t * errors
Vector of errors for this node.
Definition: node.h:415
static pg_node_t * pg_get_node(uword node_index)
Definition: pg.h:350
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:111
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:191
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
format_function_t format_mpls_unicast_header_net_byte_order
Definition: mpls.h:68
static u32 vnet_mpls_uc_get_ttl(mpls_label_t label_exp_s_ttl)
Definition: packet.h:118
A collection of simple counters.
Definition: counter.h:58
#define vlib_call_init_function(vm, x)
Definition: init.h:162
#define VLIB_NODE_FUNCTION_MULTIARCH(node, fn)
Definition: node.h:158
vlib_node_registration_t mpls_input_node
(constructor) VLIB_REGISTER_NODE (mpls_input_node)
Definition: mpls_input.c:63
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:209
#define PREDICT_FALSE(x)
Definition: clib.h:105
static u32 vnet_mpls_uc_get_label(mpls_label_t label_exp_s_ttl)
Definition: packet.h:103
vlib_simple_counter_main_t * sw_if_counters
Definition: interface.h:717
mpls_input_next_t
Definition: mpls_input.c:32
#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
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:130
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1166
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:143
u16 n_vectors
Definition: node.h:344
mpls_main_t mpls_main
Definition: mpls.c:25
static_always_inline uword vlib_get_thread_index(void)
Definition: threads.h:221
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:74
vlib_main_t * vm
Definition: buffer.c:294
unformat_function_t unformat_pg_mpls_header
Definition: mpls.h:83
unformat_function_t * unformat_edit
Definition: pg.h:307
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
#define foreach_mpls_input_next
Definition: mpls_input.c:28
static u8 * format_mpls_input_trace(u8 *s, va_list *args)
Definition: mpls_input.c:40
static void mpls_setup_nodes(vlib_main_t *vm)
Definition: mpls_input.c:273
static clib_error_t * mpls_input_init(vlib_main_t *vm)
Definition: mpls_input.c:284
u16 cached_next_index
Next frame index that vector arguments were last enqueued to last time this node ran.
Definition: node.h:456
unsigned int u32
Definition: types.h:88
#define clib_error_report(e)
Definition: error.h:113
void ethernet_register_input_type(vlib_main_t *vm, ethernet_type_t type, u32 node_index)
Definition: node.c:1333
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 clib_error_t * mpls_init(vlib_main_t *vm)
Definition: mpls.c:473
struct _vlib_node_registration vlib_node_registration_t
unsigned char u8
Definition: types.h:56
static uword mpls_input(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
Definition: mpls_input.c:235
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:267
#define vnet_buffer(b)
Definition: buffer.h:372
static uword mpls_input_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
Definition: mpls_input.c:73
u8 data[0]
Packet data.
Definition: buffer.h:179
static u32 vnet_mpls_uc_get_exp(mpls_label_t label_exp_s_ttl)
Definition: packet.h:108
mpls_main_t * mpls_main
Definition: mpls_input.c:69
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:111
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
Definition: pg.h:304
Definition: defs.h:46
static_always_inline void vnet_feature_arc_start(u8 arc, u32 sw_if_index, u32 *next0, vlib_buffer_t *b0)
Definition: feature.h:201