FD.io VPP  v16.09
Vector Packet Processing
node.c
Go to the documentation of this file.
1 /*
2  * node.c: mpls-o-gre decap processing
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-gre/mpls.h>
21 
22 typedef struct {
28 
29 u8 * format_mpls_rx_trace (u8 * s, va_list * args)
30 {
31  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
32  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
33  mpls_rx_trace_t * t = va_arg (*args, mpls_rx_trace_t *);
34  char * next_name;
35 
36  next_name = "BUG!";
37 
38 #define _(a,b) if (t->next_index == MPLS_INPUT_NEXT_##a) next_name = b;
40 #undef _
41 
42  s = format (s, "MPLS: next %s, lookup fib index %d, decap index %d\n",
43  next_name, t->next_index, t->tx_fib_index, t->decap_index);
44  if (t->decap_index != ~0)
45  {
46  s = format (s, " label %d",
48  }
49  return s;
50 }
51 
53 
54 typedef struct {
60 
61 static inline uword
63  vlib_node_runtime_t * node,
64  vlib_frame_t * from_frame, int is_mpls_o_gre)
65 {
66  u32 n_left_from, next_index, * from, * to_next;
67  ip4_main_t * im = &ip4_main;
68  from = vlib_frame_vector_args (from_frame);
69  n_left_from = from_frame->n_vectors;
71  mpls_main_t * mm;
72 
74  mm = rt->mpls_main;
75  /*
76  * Force an initial lookup every time, in case the control-plane
77  * changed the label->FIB mapping.
78  */
79  rt->last_label = ~0;
80 
81  next_index = node->cached_next_index;
82 
83  while (n_left_from > 0)
84  {
85  u32 n_left_to_next;
86 
87  vlib_get_next_frame (vm, node, next_index,
88  to_next, n_left_to_next);
89 
90 #if 0
91  while (n_left_from >= 4 && n_left_to_next >= 2)
92  {
93  u32 bi0, bi1;
94  vlib_buffer_t * b0, * b1;
95  mpls_unicast_header_t * h0, * h1;
96  int li0, li1;
97  u64 key0, key1;
98  u32 label0, label1;
99  u32 next0, next1;
100  uword * p0, * p1;
101  u32 fib_index0, fib_index1;
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 
115  }
116 
117  bi0 = from[0];
118  bi1 = from[1];
119  to_next[0] = bi0;
120  to_next[1] = bi1;
121  from += 2;
122  to_next += 2;
123  n_left_to_next -= 2;
124  n_left_from -= 2;
125 
126  b0 = vlib_get_buffer (vm, bi0);
127  b1 = vlib_get_buffer (vm, bi1);
128 
129  /* $$$$$ dual loop me */
130 
131  vlib_buffer_advance (b0, sizeof (*h0));
132  vlib_buffer_advance (b1, sizeof (*h1));
133 
134  vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
135  to_next, n_left_to_next,
136  bi0, bi1, next0, next1);
137  }
138 
139 #endif
140 
141  while (n_left_from > 0 && n_left_to_next > 0)
142  {
143  u32 bi0;
144  vlib_buffer_t * b0;
146  u32 label0;
147  u32 next0;
148  u64 key0;
149  uword * p0;
150  u32 rx_fib_index0;
151  mpls_decap_t *d0;
152 
153  bi0 = from[0];
154  to_next[0] = bi0;
155  from += 1;
156  to_next += 1;
157  n_left_from -= 1;
158  n_left_to_next -= 1;
159 
160  b0 = vlib_get_buffer (vm, bi0);
161  h0 = vlib_buffer_get_current (b0);
162 
163  if (is_mpls_o_gre)
164  {
165  rx_fib_index0 = vec_elt (im->fib_index_by_sw_if_index,
166  vnet_buffer(b0)->sw_if_index[VLIB_RX]);
167  }
168  else
169  {
170 #if 0
171  /* If separate RX numbering spaces are required... */
172  rx_fib_index0 = vec_elt (mm->fib_index_by_sw_if_index,
173  vnet_buffer(b0)->sw_if_index[VLIB_RX]);
174 #endif
175  rx_fib_index0 = 0;
176  }
177 
178  next0 = ~0;
179  d0 = 0;
180 
181  /*
182  * Expect the control-plane team to squeal like pigs.
183  * If they don't program a decap label entry for each
184  * and every label in the stack, packets go into the trash...
185  */
186 
187  do
188  {
189  label0 = clib_net_to_host_u32 (h0->label_exp_s_ttl);
190  /* TTL expired? */
191  if (PREDICT_FALSE(vnet_mpls_uc_get_ttl (label0) == 0))
192  {
193  next0 = MPLS_INPUT_NEXT_DROP;
194  b0->error = node->errors[MPLS_ERROR_TTL_EXPIRED];
195  break;
196  }
197 
198  key0 = ((u64)rx_fib_index0<<32)
199  | ((u64)vnet_mpls_uc_get_label (label0)<<12)
200  | ((u64)vnet_mpls_uc_get_s (label0)<<8);
201 
202  /*
203  * The architecture crew claims that we won't need
204  * separate ip4, ip6, mpls-o-ethernet label numbering
205  * spaces. Use the low 8 key bits as a discriminator.
206  */
207 
208  p0 = hash_get (mm->mpls_decap_by_rx_fib_and_label, key0);
209  if (p0 == 0)
210  {
211  next0 = MPLS_INPUT_NEXT_DROP;
212  b0->error = node->errors[MPLS_ERROR_BAD_LABEL];
213  break;
214  }
215  d0 = pool_elt_at_index (mm->decaps, p0[0]);
216  next0 = d0->next_index;
217  vnet_buffer(b0)->sw_if_index[VLIB_TX] = d0->tx_fib_index;
218  vlib_buffer_advance (b0, sizeof (*h0));
219  h0 = vlib_buffer_get_current (b0);
220  } while (!vnet_mpls_uc_get_s(label0));
221 
223  {
224  mpls_rx_trace_t *tr = vlib_add_trace (vm, node,
225  b0, sizeof (*tr));
226  tr->next_index = next0;
227  tr->decap_index = d0 ? d0 - mm->decaps : ~0;
228  tr->tx_fib_index = vnet_buffer(b0)->sw_if_index[VLIB_TX];
229  tr->label_host_byte_order = label0;
230  }
231 
232  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
233  to_next, n_left_to_next,
234  bi0, next0);
235  }
236 
237  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
238  }
240  MPLS_ERROR_PKTS_DECAP, from_frame->n_vectors);
241  return from_frame->n_vectors;
242 }
243 
244 static uword
246  vlib_node_runtime_t * node,
247  vlib_frame_t * from_frame)
248 {
249  return mpls_input_inline (vm, node, from_frame, 1 /* is mpls-o-gre */);
250 }
251 
252 static char * mpls_error_strings[] = {
253 #define mpls_error(n,s) s,
254 #include "error.def"
255 #undef mpls_error
256 };
257 
259  .function = mpls_input,
260  .name = "mpls-gre-input",
261  /* Takes a vector of packets. */
262  .vector_size = sizeof (u32),
263 
264  .runtime_data_bytes = sizeof(mpls_input_runtime_t),
265 
266  .n_errors = MPLS_N_ERROR,
267  .error_strings = mpls_error_strings,
268 
269  .n_next_nodes = MPLS_INPUT_N_NEXT,
270  .next_nodes = {
271 #define _(s,n) [MPLS_INPUT_NEXT_##s] = n,
273 #undef _
274  },
275 
276  .format_buffer = format_mpls_gre_header_with_length,
277  .format_trace = format_mpls_rx_trace,
278  .unformat_buffer = unformat_mpls_gre_header,
279 };
280 
282 
283 static uword
285  vlib_node_runtime_t * node,
286  vlib_frame_t * from_frame)
287 {
288  return mpls_input_inline (vm, node, from_frame, 0 /* is mpls-o-gre */);
289 }
290 
291 
293  .function = mpls_ethernet_input,
294  .name = "mpls-ethernet-input",
295  /* Takes a vector of packets. */
296  .vector_size = sizeof (u32),
297 
298  .runtime_data_bytes = sizeof(mpls_input_runtime_t),
299 
300  .n_errors = MPLS_N_ERROR,
301  .error_strings = mpls_error_strings,
302 
303  .n_next_nodes = MPLS_INPUT_N_NEXT,
304  .next_nodes = {
305 #define _(s,n) [MPLS_INPUT_NEXT_##s] = n,
307 #undef _
308  },
309 
310  .format_buffer = format_mpls_eth_header_with_length,
311  .format_trace = format_mpls_rx_trace,
312  .unformat_buffer = unformat_mpls_gre_header,
313 };
314 
316 
317 static void
319 {
320  vlib_node_t * n = vlib_get_node (vm, mpls_input_node.index);
321  pg_node_t * pn = pg_get_node (mpls_input_node.index);
323 
327 
329  rt->last_label = (u32) ~0;
330  rt->last_inner_fib_index = 0;
331  rt->last_outer_fib_index = 0;
332  rt->mpls_main = &mpls_main;
333 
334  n = vlib_get_node (vm, mpls_ethernet_input_node.index);
335 
337 
338  n->unformat_buffer = 0; /* unformat_mpls_ethernet_header; */
339 
341  rt->last_label = (u32) ~0;
342  rt->last_inner_fib_index = 0;
343  rt->last_outer_fib_index = 0;
344  rt->mpls_main = &mpls_main;
345 
346  ethernet_register_input_type (vm, ETHERNET_TYPE_MPLS_UNICAST,
348 }
349 
351 {
352  clib_error_t * error;
353 
354  error = vlib_call_init_function (vm, mpls_init);
355  if (error)
356  clib_error_report (error);
357 
358  mpls_setup_nodes (vm);
359 
360  return 0;
361 }
362 
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:457
VLIB_NODE_FUNCTION_MULTIARCH(ethernet_input_not_l2_node, ethernet_input_not_l2)
Definition: node.c:1056
#define CLIB_UNUSED(x)
Definition: clib.h:79
u32 last_inner_fib_index
Definition: node.c:56
static u32 vnet_mpls_uc_get_label(u32 label_exp_s_ttl)
Definition: packet.h:29
format_function_t format_mpls_eth_header_with_length
Definition: mpls.h:113
u32 next_index
Definition: mpls.h:78
static clib_error_t * mpls_input_init(vlib_main_t *vm)
Definition: node.c:350
struct _vlib_node_registration vlib_node_registration_t
static void mpls_setup_nodes(vlib_main_t *vm)
Definition: node.c:318
u32 * fib_index_by_sw_if_index
Table index indexed by software interface.
Definition: ip4.h:123
#define clib_error_report(e)
Definition: error.h:125
vlib_error_t * errors
Definition: node.h:418
u32 last_outer_fib_index
Definition: node.c:57
#define foreach_mpls_input_next
Definition: mpls.h:199
static u32 vnet_mpls_uc_get_s(u32 label_exp_s_ttl)
Definition: packet.h:39
static pg_node_t * pg_get_node(uword node_index)
Definition: pg.h:343
mpls_decap_t * decaps
Definition: mpls.h:95
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:111
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:187
unsigned long u64
Definition: types.h:89
#define vlib_call_init_function(vm, x)
Definition: init.h:161
#define hash_get(h, key)
Definition: hash.h:248
static uword mpls_input(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
Definition: node.c:245
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:369
format_function_t format_mpls_gre_header_with_length
Definition: mpls.h:112
static uword mpls_input_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame, int is_mpls_o_gre)
Definition: node.c:62
static uword mpls_ethernet_input(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
Definition: node.c:284
u32 tx_fib_index
Definition: node.c:25
u8 * format_mpls_rx_trace(u8 *s, va_list *args)
Definition: node.c:29
static u32 vnet_mpls_uc_get_ttl(u32 label_exp_s_ttl)
Definition: packet.h:44
u32 next_index
Definition: node.c:23
static void * vlib_node_get_runtime_data(vlib_main_t *vm, u32 node_index)
Get node runtime private data by node index.
Definition: node_funcs.h:109
#define PREDICT_FALSE(x)
Definition: clib.h:97
format_function_t * format_buffer
Definition: node.h:311
#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:130
#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:348
u32 label_host_byte_order
Definition: node.c:26
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:118
vlib_node_registration_t mpls_input_node
(constructor) VLIB_REGISTER_NODE (mpls_input_node)
Definition: node.c:52
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1111
void ethernet_register_input_type(vlib_main_t *vm, ethernet_type_t type, u32 node_index)
Definition: node.c:1182
u16 n_vectors
Definition: node.h:344
mpls_main_t mpls_main
Definition: mpls.c:21
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:82
static char * mpls_error_strings[]
Definition: node.c:252
unformat_function_t unformat_pg_mpls_header
Definition: mpls.h:132
static void vlib_buffer_advance(vlib_buffer_t *b, word l)
Advance current data pointer by the supplied (signed!) amount.
Definition: buffer.h:200
unformat_function_t * unformat_buffer
Definition: node.h:312
vlib_node_registration_t mpls_ethernet_input_node
(constructor) VLIB_REGISTER_NODE (mpls_ethernet_input_node)
Definition: node.c:292
unformat_function_t * unformat_edit
Definition: pg.h:299
u16 cached_next_index
Definition: node.h:462
unsigned int u32
Definition: types.h:88
#define vnet_buffer(b)
Definition: buffer.h:335
u32 tx_fib_index
Definition: mpls.h:77
u8 * format(u8 *s, char *fmt,...)
Definition: format.c:418
IPv4 main type.
Definition: ip4.h:114
uword * mpls_decap_by_rx_fib_and_label
Definition: mpls.h:96
#define VLIB_BUFFER_IS_TRACED
Definition: buffer.h:93
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:762
#define vec_elt(v, i)
Get vector value at index i.
Definition: defs.h:47
unsigned char u8
Definition: types.h:56
unformat_function_t unformat_mpls_gre_header
Definition: mpls.h:127
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:251
u32 decap_index
Definition: node.c:24
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:163
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:143
ip4_main_t ip4_main
Global ip4 main structure.
Definition: ip4_forward.c:1578
u8 data[0]
Packet data.
Definition: buffer.h:151
static vlib_node_t * vlib_get_node(vlib_main_t *vm, u32 i)
Get vlib node by index.
Definition: node_funcs.h:58
mpls_main_t * mpls_main
Definition: node.c:58
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:67
u32 flags
buffer flags: VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:85
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:69
Definition: pg.h:297
Definition: defs.h:46