FD.io VPP  v16.12-rc0-308-g931be3a
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 static u8 *
33 format_mpls_output_trace (u8 * s, va_list * args)
34 {
35  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
36  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
37  mpls_output_trace_t * t = va_arg (*args, mpls_output_trace_t *);
38  vnet_main_t * vnm = vnet_get_main();
39  uword indent = format_get_indent (s);
40 
41  s = format (s, "adj-idx %d : %U flow hash: 0x%08x",
42  t->adj_index,
44  t->flow_hash);
45  s = format (s, "\n%U%U",
46  format_white_space, indent,
48  vnm, t->adj_index,
49  t->packet_data, sizeof (t->packet_data));
50  return s;
51 }
52 
53 static inline uword
55  vlib_node_runtime_t * node,
56  vlib_frame_t * from_frame,
57  int is_midchain)
58 {
59  u32 n_left_from, next_index, * from, * to_next, cpu_index;
60  vlib_node_runtime_t * error_node;
61 
62  cpu_index = os_get_cpu_number();
63  error_node = vlib_node_get_runtime (vm, mpls_output_node.index);
64  from = vlib_frame_vector_args (from_frame);
65  n_left_from = from_frame->n_vectors;
66  next_index = node->cached_next_index;
67 
68  while (n_left_from > 0)
69  {
70  u32 n_left_to_next;
71 
72  vlib_get_next_frame (vm, node, next_index,
73  to_next, n_left_to_next);
74 
75  while (n_left_from > 0 && n_left_to_next > 0)
76  {
77  ip_adjacency_t * adj0;
79  vlib_buffer_t * p0;
80  u32 pi0, rw_len0, adj_index0, next0, error0;
81 
82  pi0 = to_next[0] = from[0];
83 
84  p0 = vlib_get_buffer (vm, pi0);
85 
86  adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
87 
88  /* We should never rewrite a pkt using the MISS adjacency */
89  ASSERT(adj_index0);
90 
91  adj0 = adj_get(adj_index0);
92  hdr0 = vlib_buffer_get_current (p0);
93 
94  /* Guess we are only writing on simple Ethernet header. */
95  vnet_rewrite_one_header (adj0[0], hdr0,
96  sizeof (ethernet_header_t));
97 
98  /* Update packet buffer attributes/set output interface. */
99  rw_len0 = adj0[0].rewrite_header.data_bytes;
100 
101  if (PREDICT_FALSE (rw_len0 > sizeof(ethernet_header_t)))
104  cpu_index, adj_index0,
105  /* packet increment */ 0,
106  /* byte increment */ rw_len0-sizeof(ethernet_header_t));
107 
108  /* Check MTU of outgoing interface. */
109  error0 = (vlib_buffer_length_in_chain (vm, p0)
110  > adj0[0].rewrite_header.max_l3_packet_bytes
111  ? IP4_ERROR_MTU_EXCEEDED
112  : IP4_ERROR_NONE);
113 
114  p0->error = error_node->errors[error0];
115 
116  /* Don't adjust the buffer for ttl issue; icmp-error node wants
117  * to see the IP headerr */
118  if (PREDICT_TRUE(error0 == IP4_ERROR_NONE))
119  {
120  p0->current_data -= rw_len0;
121  p0->current_length += rw_len0;
122 
123  vnet_buffer (p0)->sw_if_index[VLIB_TX] =
124  adj0[0].rewrite_header.sw_if_index;
125  next0 = adj0[0].rewrite_header.next_index;
126 
127  if (is_midchain)
128  {
129  adj0->sub_type.midchain.fixup_func(vm, adj0, p0);
130  }
131  }
132  else
133  {
134  next0 = MPLS_OUTPUT_NEXT_DROP;
135  }
136 
137  from += 1;
138  n_left_from -= 1;
139  to_next += 1;
140  n_left_to_next -= 1;
141 
143  {
144  mpls_output_trace_t *tr = vlib_add_trace (vm, node,
145  p0, sizeof (*tr));
146  tr->adj_index = vnet_buffer(p0)->ip.adj_index[VLIB_TX];
147  tr->flow_hash = vnet_buffer(p0)->ip.flow_hash;
148  }
149 
150  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
151  to_next, n_left_to_next,
152  pi0, next0);
153  }
154 
155  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
156  }
158  MPLS_ERROR_PKTS_ENCAP,
159  from_frame->n_vectors);
160 
161  return from_frame->n_vectors;
162 }
163 
164 static char * mpls_error_strings[] = {
165 #define mpls_error(n,s) s,
166 #include "error.def"
167 #undef mpls_error
168 };
169 
170 static inline uword
172  vlib_node_runtime_t * node,
173  vlib_frame_t * from_frame)
174 {
175  return (mpls_output_inline(vm, node, from_frame, /* is_midchain */ 0));
176 }
177 
179  .function = mpls_output,
180  .name = "mpls-output",
181  /* Takes a vector of packets. */
182  .vector_size = sizeof (u32),
183  .n_errors = MPLS_N_ERROR,
184  .error_strings = mpls_error_strings,
185 
186  .n_next_nodes = MPLS_OUTPUT_N_NEXT,
187  .next_nodes = {
188 #define _(s,n) [MPLS_OUTPUT_NEXT_##s] = n,
190 #undef _
191  },
192 
193  .format_trace = format_mpls_output_trace,
194 };
195 
197 
198 static inline uword
200  vlib_node_runtime_t * node,
201  vlib_frame_t * from_frame)
202 {
203  return (mpls_output_inline(vm, node, from_frame, /* is_midchain */ 1));
204 }
205 
207  .function = mpls_midchain,
208  .name = "mpls-midchain",
209  .vector_size = sizeof (u32),
210 
211  .format_trace = format_mpls_output_trace,
212 
213  .sibling_of = "mpls-output",
214 };
215 
217 
218 /**
219  * @brief Next index values from the MPLS incomplete adj node
220  */
221 #define foreach_mpls_adj_incomplete_next \
222 _(DROP, "error-drop") \
223 _(IP4, "ip4-arp") \
224 _(IP6, "ip6-discover-neighbor")
225 
226 typedef enum {
227 #define _(s,n) MPLS_ADJ_INCOMPLETE_NEXT_##s,
229 #undef _
232 
233 /**
234  * @brief A struct to hold tracing information for the MPLS label imposition
235  * node.
236  */
238 {
241 
242 
243 /**
244  * @brief Graph node for incomplete MPLS adjacency.
245  * This node will push traffic to either the v4-arp or v6-nd node
246  * based on the next-hop proto of the adj.
247  * We pay a cost for this 'routing' node, but an incomplete adj is the
248  * exception case.
249  */
250 static inline uword
252  vlib_node_runtime_t * node,
253  vlib_frame_t * from_frame)
254 {
255  u32 n_left_from, next_index, * from, * to_next;
256 
257  from = vlib_frame_vector_args (from_frame);
258  n_left_from = from_frame->n_vectors;
259  next_index = node->cached_next_index;
260 
261  while (n_left_from > 0)
262  {
263  u32 n_left_to_next;
264 
265  vlib_get_next_frame (vm, node, next_index,
266  to_next, n_left_to_next);
267 
268  while (n_left_from > 0 && n_left_to_next > 0)
269  {
270  u32 pi0, next0, adj_index0;
271  ip_adjacency_t * adj0;
272  vlib_buffer_t * p0;
273 
274  pi0 = to_next[0] = from[0];
275  p0 = vlib_get_buffer (vm, pi0);
276  from += 1;
277  n_left_from -= 1;
278  to_next += 1;
279  n_left_to_next -= 1;
280 
281  adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
282  ASSERT(adj_index0);
283 
284  adj0 = adj_get(adj_index0);
285 
287  {
288  next0 = MPLS_ADJ_INCOMPLETE_NEXT_IP4;
289  }
290  else
291  {
292  next0 = MPLS_ADJ_INCOMPLETE_NEXT_IP6;
293  }
294 
296  {
298  vlib_add_trace (vm, node, p0, sizeof (*tr));
299  tr->next = next0;
300  }
301 
302  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
303  to_next, n_left_to_next,
304  pi0, next0);
305  }
306 
307  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
308  }
309 
310  return from_frame->n_vectors;
311 }
312 
313 static u8 *
315 {
316  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
317  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
319  uword indent;
320 
321  t = va_arg (*args, mpls_adj_incomplete_trace_t *);
322  indent = format_get_indent (s);
323 
324  s = format (s, "%Unext:%d",
325  format_white_space, indent,
326  t->next);
327  return (s);
328 }
329 
331  .function = mpls_adj_incomplete,
332  .name = "mpls-adj-incomplete",
333  .format_trace = format_mpls_adj_incomplete_trace,
334  /* Takes a vector of packets. */
335  .vector_size = sizeof (u32),
336  .n_errors = MPLS_N_ERROR,
337  .error_strings = mpls_error_strings,
338 
339  .n_next_nodes = MPLS_ADJ_INCOMPLETE_N_NEXT,
340  .next_nodes = {
341 #define _(s,n) [MPLS_ADJ_INCOMPLETE_NEXT_##s] = n,
343 #undef _
344  },
345 
346  .format_trace = format_mpls_output_trace,
347 };
348 
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
#define vnet_rewrite_one_header(rw0, p0, most_likely_size)
Definition: rewrite.h:253
#define CLIB_UNUSED(x)
Definition: clib.h:79
#define PREDICT_TRUE(x)
Definition: clib.h:98
IP unicast adjacency.
Definition: lookup.h:174
A struct to hold tracing information for the MPLS label imposition node.
Definition: mpls_output.c:237
static u8 * format_mpls_adj_incomplete_trace(u8 *s, va_list *args)
Definition: mpls_output.c:314
vlib_error_t * errors
Definition: node.h:419
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:112
static u8 * format_mpls_output_trace(u8 *s, va_list *args)
Definition: mpls_output.c:33
vlib_node_registration_t mpls_midchain_node
(constructor) VLIB_REGISTER_NODE (mpls_midchain_node)
Definition: mpls_output.c:206
vlib_node_registration_t mpls_output_node
(constructor) VLIB_REGISTER_NODE (mpls_output_node)
Definition: mpls_output.c:178
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
static ip_adjacency_t * adj_get(adj_index_t adj_index)
Get a pointer to an adjacency object from its index.
Definition: adj.h:117
i16 current_data
signed offset in data[], pre_data[] that we are currently processing.
Definition: buffer.h:78
format_function_t format_ip_adjacency_packet_data
Definition: format.h:59
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:190
u8 * format_white_space(u8 *s, va_list *va)
Definition: std-formats.c:113
mpls_adj_incomplete_next_t
Definition: mpls_output.c:226
static uword format_get_indent(u8 *s)
Definition: format.h:72
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:82
uword os_get_cpu_number(void)
Definition: unix-misc.c:224
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:251
#define PREDICT_FALSE(x)
Definition: clib.h:97
#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:30
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
static uword mpls_output(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
Definition: mpls_output.c:171
union ip_adjacency_t_::@164 sub_type
u16 n_vectors
Definition: node.h:344
#define foreach_mpls_adj_incomplete_next
Next index values from the MPLS incomplete adj node.
Definition: mpls_output.c:221
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
u16 cached_next_index
Definition: node.h:463
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:54
static void vlib_increment_combined_counter(vlib_combined_counter_main_t *cm, u32 cpu_index, u32 index, u32 packet_increment, u32 byte_increment)
Increment a combined counter.
Definition: counter.h:241
#define ASSERT(truth)
#define foreach_mpls_output_next
Definition: mpls.h:237
unsigned int u32
Definition: types.h:88
#define vnet_buffer(b)
Definition: buffer.h:333
#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 char * mpls_error_strings[]
Definition: mpls_output.c:164
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 VLIB_NODE_FUNCTION_MULTIARCH(node, fn)
Definition: node.h:158
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:143
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:418
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:330
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
static uword mpls_midchain(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
Definition: mpls_output.c:199
struct ip_adjacency_t_::@164::@166 midchain
IP_LOOKUP_NEXT_MIDCHAIN.
format_function_t format_ip_adjacency
Definition: format.h:58