FD.io VPP  v16.12-rc0-308-g931be3a
Vector Packet Processing
node.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 static u8 *
29 format_mpls_input_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_input_trace_t * t = va_arg (*args, mpls_input_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[%d] label %d ttl %d",
43  next_name, t->next_index,
46 
47  return s;
48 }
49 
51 
52 typedef struct {
58 
59 static inline uword
61  vlib_node_runtime_t * node,
62  vlib_frame_t * from_frame)
63 {
64  u32 n_left_from, next_index, * from, * to_next;
66  mpls_main_t * mm;
67  u32 cpu_index = os_get_cpu_number();
69  vnet_main_t * vnm = vnet_get_main();
70 
71  from = vlib_frame_vector_args (from_frame);
72  n_left_from = from_frame->n_vectors;
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 
85 
86  while (n_left_from > 0)
87  {
88  u32 n_left_to_next;
89 
90  vlib_get_next_frame (vm, node, next_index,
91  to_next, n_left_to_next);
92 
93  while (n_left_from > 0 && n_left_to_next > 0)
94  {
95  u32 bi0;
96  vlib_buffer_t * b0;
98  u32 label0;
99  u32 next0;
101  u32 sw_if_index0;
102 
103  bi0 = from[0];
104  to_next[0] = bi0;
105  from += 1;
106  to_next += 1;
107  n_left_from -= 1;
108  n_left_to_next -= 1;
109 
110  b0 = vlib_get_buffer (vm, bi0);
111  h0 = vlib_buffer_get_current (b0);
112  sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
113 
116  sw_if_index0);
117 
118  label0 = clib_net_to_host_u32 (h0->label_exp_s_ttl);
119  /* TTL expired? */
120  if (PREDICT_FALSE(vnet_mpls_uc_get_ttl (label0) == 0))
121  {
122  next0 = MPLS_INPUT_NEXT_DROP;
123  b0->error = node->errors[MPLS_ERROR_TTL_EXPIRED];
124  }
125  else
126  {
129  &next0,
130  /* # bytes of config data */ 0);
131  vlib_increment_simple_counter (cm, cpu_index, sw_if_index0, 1);
132  }
133 
135  {
136  mpls_input_trace_t *tr = vlib_add_trace (vm, node,
137  b0, sizeof (*tr));
138  tr->next_index = next0;
139  tr->label_host_byte_order = label0;
140  }
141 
142  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
143  to_next, n_left_to_next,
144  bi0, next0);
145  }
146 
147  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
148  }
150  MPLS_ERROR_PKTS_DECAP, from_frame->n_vectors);
151  return from_frame->n_vectors;
152 }
153 
154 static uword
156  vlib_node_runtime_t * node,
157  vlib_frame_t * from_frame)
158 {
159  return mpls_input_inline (vm, node, from_frame);
160 }
161 
162 static char * mpls_error_strings[] = {
163 #define mpls_error(n,s) s,
164 #include "error.def"
165 #undef mpls_error
166 };
167 
169  .function = mpls_input,
170  .name = "mpls-input",
171  /* Takes a vector of packets. */
172  .vector_size = sizeof (u32),
173 
174  .runtime_data_bytes = sizeof(mpls_input_runtime_t),
175 
176  .n_errors = MPLS_N_ERROR,
177  .error_strings = mpls_error_strings,
178 
179  .n_next_nodes = MPLS_INPUT_N_NEXT,
180  .next_nodes = {
181 #define _(s,n) [MPLS_INPUT_NEXT_##s] = n,
183 #undef _
184  },
185 
187  .format_trace = format_mpls_input_trace,
188 };
189 
191 
192 static void
194 {
196  pg_node_t * pn;
197 
198  pn = pg_get_node (mpls_input_node.index);
200 
202  rt->last_label = (u32) ~0;
203  rt->last_inner_fib_index = 0;
204  rt->last_outer_fib_index = 0;
205  rt->mpls_main = &mpls_main;
206 
207  ethernet_register_input_type (vm, ETHERNET_TYPE_MPLS_UNICAST,
208  mpls_input_node.index);
209 }
210 
212 {
213  clib_error_t * error;
214 
215  error = vlib_call_init_function (vm, mpls_init);
216  if (error)
217  clib_error_report (error);
218 
219  mpls_setup_nodes (vm);
220 
221  return (mpls_feature_init(vm));
222 }
223 
vnet_config_main_t config_main
Definition: feature.h:55
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
static void vlib_increment_simple_counter(vlib_simple_counter_main_t *cm, u32 cpu_index, u32 index, u32 increment)
Increment a simple counter.
Definition: counter.h:78
VLIB_NODE_FUNCTION_MULTIARCH(ethernet_input_not_l2_node, ethernet_input_not_l2)
Definition: node.c:1054
#define CLIB_UNUSED(x)
Definition: clib.h:79
u32 last_inner_fib_index
Definition: node.c:54
u32 current_config_index
Used by feature subgraph arcs to visit enabled feature nodes.
Definition: buffer.h:124
vnet_interface_main_t interface_main
Definition: vnet.h:72
static uword mpls_input(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
Definition: node.c:155
struct _vlib_node_registration vlib_node_registration_t
static void mpls_setup_nodes(vlib_main_t *vm)
Definition: node.c:193
#define clib_error_report(e)
Definition: error.h:125
vlib_error_t * errors
Definition: node.h:419
u32 last_outer_fib_index
Definition: node.c:55
#define foreach_mpls_input_next
Definition: mpls.h:213
static clib_error_t * mpls_input_init(vlib_main_t *vm)
Definition: node.c:211
vnet_feature_config_main_t feature_config_mains[VNET_N_IP_FEAT]
Definition: mpls.h:98
static pg_node_t * pg_get_node(uword node_index)
Definition: pg.h:347
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
#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:190
#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:164
static u32 vnet_mpls_uc_get_ttl(mpls_label_t label_exp_s_ttl)
Definition: packet.h:92
A collection of simple counters.
Definition: counter.h:59
#define vlib_call_init_function(vm, x)
Definition: init.h:161
static u8 * format_mpls_input_trace(u8 *s, va_list *args)
Definition: node.c:29
uword os_get_cpu_number(void)
Definition: unix-misc.c:224
static uword mpls_input_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
Definition: node.c:60
static void * vnet_get_config_data(vnet_config_main_t *cm, u32 *config_index, u32 *next_index, u32 n_data_bytes)
Definition: config.h:122
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
static u32 vnet_mpls_uc_get_label(mpls_label_t label_exp_s_ttl)
Definition: packet.h:77
vlib_simple_counter_main_t * sw_if_counters
Definition: interface.h:576
#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_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:121
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1113
void ethernet_register_input_type(vlib_main_t *vm, ethernet_type_t type, u32 node_index)
Definition: node.c:1180
u16 n_vectors
Definition: node.h:344
mpls_main_t mpls_main
Definition: mpls.c:25
unformat_function_t unformat_pg_mpls_header
Definition: mpls.h:182
unformat_function_t * unformat_edit
Definition: pg.h:301
clib_error_t * mpls_feature_init(vlib_main_t *vm)
u16 cached_next_index
Definition: node.h:463
unsigned int u32
Definition: types.h:88
#define vnet_buffer(b)
Definition: buffer.h:333
mpls_label_t label_exp_s_ttl
Definition: packet.h:31
#define VLIB_BUFFER_IS_TRACED
Definition: buffer.h:95
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:693
#define vec_elt(v, i)
Get vector value at index i.
u32 label_host_byte_order
Definition: node.c:25
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 VLIB_REGISTER_NODE(x,...)
Definition: node.h:143
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:418
u32 next_index
Definition: node.c:24
static char * mpls_error_strings[]
Definition: node.c:162
mpls_main_t * mpls_main
Definition: node.c:56
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:298
Definition: defs.h:46
vlib_node_registration_t mpls_input_node
(constructor) VLIB_REGISTER_NODE (mpls_input_node)
Definition: node.c:50