nsh_output.c revision 9b3c3af8
1/*
2 * nsh_output.c: NSH Adj rewrite
3 *
4 * Copyright (c) 2017-2019 Intel 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 <nsh/nsh.h>
22
23typedef struct {
24  /* Adjacency taken. */
25  u32 adj_index;
26  u32 flow_hash;
27
28  /* Packet data, possibly *after* rewrite. */
29  u8 packet_data[64 - 1*sizeof(u32)];
30} nsh_output_trace_t;
31
32#define foreach_nsh_output_next        	\
33_(DROP, "error-drop")            \
34_(INTERFACE, "interface-output" )
35
36typedef enum {
37#define _(s,n) NSH_OUTPUT_NEXT_##s,
38  foreach_nsh_output_next
39#undef _
40  NSH_OUTPUT_N_NEXT,
41} nsh_output_next_t;
42
43static u8 *
44format_nsh_output_trace (u8 * s, va_list * args)
45{
46  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
47  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
48  nsh_output_trace_t * t = va_arg (*args, nsh_output_trace_t *);
49  uword indent = format_get_indent (s);
50
51  s = format (s, "adj-idx %d : %U flow hash: 0x%08x",
52              t->adj_index,
53              format_ip_adjacency, t->adj_index, FORMAT_IP_ADJACENCY_NONE,
54              t->flow_hash);
55  s = format (s, "\n%U%U",
56              format_white_space, indent,
57              format_ip_adjacency_packet_data,
58              t->adj_index, t->packet_data, sizeof (t->packet_data));
59  return s;
60}
61
62static inline uword
63nsh_output_inline (vlib_main_t * vm,
64                   vlib_node_runtime_t * node,
65                   vlib_frame_t * from_frame,
66                   int is_midchain)
67{
68  u32 n_left_from, next_index, * from, * to_next, thread_index;
69  vlib_node_runtime_t * error_node;
70  u32 n_left_to_next;
71  nsh_main_t *nm;
72
73  thread_index = vlib_get_thread_index();
74  error_node = vlib_node_get_runtime (vm, nsh_eth_output_node.index);
75  from = vlib_frame_vector_args (from_frame);
76  n_left_from = from_frame->n_vectors;
77  next_index = node->cached_next_index;
78  nm = &nsh_main;
79
80  while (n_left_from > 0)
81    {
82      vlib_get_next_frame (vm, node, next_index,
83                           to_next, n_left_to_next);
84
85      while (n_left_from >= 4 && n_left_to_next >= 2)
86        {
87          ip_adjacency_t * adj0;
88          nsh_base_header_t *hdr0;
89          ethernet_header_t * eth_hdr0;
90          vlib_buffer_t * p0;
91          u32 pi0, rw_len0, adj_index0, next0, error0;
92
93          ip_adjacency_t * adj1;
94          nsh_base_header_t *hdr1;
95          ethernet_header_t * eth_hdr1;
96          vlib_buffer_t * p1;
97          u32 pi1, rw_len1, adj_index1, next1, error1;
98
99          /* Prefetch next iteration. */
100          {
101            vlib_buffer_t * p2, * p3;
102
103            p2 = vlib_get_buffer (vm, from[2]);
104            p3 = vlib_get_buffer (vm, from[3]);
105
106            vlib_prefetch_buffer_header (p2, STORE);
107            vlib_prefetch_buffer_header (p3, STORE);
108
109            CLIB_PREFETCH (p2->data, sizeof (hdr0[0]), STORE);
110            CLIB_PREFETCH (p3->data, sizeof (hdr1[0]), STORE);
111          }
112
113          pi0 = to_next[0] = from[0];
114          pi1 = to_next[1] = from[1];
115
116          from += 2;
117          n_left_from -= 2;
118          to_next += 2;
119          n_left_to_next -= 2;
120
121          p0 = vlib_get_buffer (vm, pi0);
122          p1 = vlib_get_buffer (vm, pi1);
123
124          adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
125          adj_index1 = vnet_buffer (p1)->ip.adj_index[VLIB_TX];
126
127          adj0 = adj_get(adj_index0);
128          adj1 = adj_get(adj_index1);
129          hdr0 = vlib_buffer_get_current (p0);
130          hdr1 = vlib_buffer_get_current (p1);
131
132          /* Guess we are only writing on simple Ethernet header. */
133          vnet_rewrite_two_headers (adj0[0], adj1[0], hdr0, hdr1,
134                                   sizeof (ethernet_header_t));
135
136          eth_hdr0 = (ethernet_header_t*)((u8 *)hdr0-sizeof(ethernet_header_t));
137          eth_hdr0->type = clib_host_to_net_u16(ETHERNET_TYPE_NSH);
138          eth_hdr1 = (ethernet_header_t*)((u8 *)hdr1-sizeof(ethernet_header_t));
139          eth_hdr1->type = clib_host_to_net_u16(ETHERNET_TYPE_NSH);
140
141          /* Update packet buffer attributes/set output interface. */
142          rw_len0 = adj0[0].rewrite_header.data_bytes;
143          rw_len1 = adj1[0].rewrite_header.data_bytes;
144
145          /* Bump the adj counters for packet and bytes */
146          vlib_increment_combined_counter
147              (&adjacency_counters,
148               thread_index,
149               adj_index0,
150               1,
151               vlib_buffer_length_in_chain (vm, p0) + rw_len0);
152          vlib_increment_combined_counter
153              (&adjacency_counters,
154               thread_index,
155               adj_index1,
156               1,
157               vlib_buffer_length_in_chain (vm, p1) + rw_len1);
158
159          /* Check MTU of outgoing interface. */
160          if (PREDICT_TRUE(vlib_buffer_length_in_chain (vm, p0) <=
161                           adj0[0].rewrite_header.max_l3_packet_bytes))
162            {
163              p0->current_data -= rw_len0;
164              p0->current_length += rw_len0;
165
166              vnet_buffer (p0)->sw_if_index[VLIB_TX] =
167                  adj0[0].rewrite_header.sw_if_index;
168              next0 = NSH_OUTPUT_NEXT_INTERFACE;
169              error0 = IP4_ERROR_NONE;
170
171              if (PREDICT_FALSE(adj0[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
172                vnet_feature_arc_start (nm->output_feature_arc_index,
173                                        adj0[0].rewrite_header.sw_if_index,
174                                        &next0, p0);
175            }
176          else
177            {
178              error0 = IP4_ERROR_MTU_EXCEEDED;
179              next0 = NSH_OUTPUT_NEXT_DROP;
180            }
181          if (PREDICT_TRUE(vlib_buffer_length_in_chain (vm, p1) <=
182                           adj1[0].rewrite_header.max_l3_packet_bytes))
183            {
184              p1->current_data -= rw_len1;
185              p1->current_length += rw_len1;
186
187              vnet_buffer (p1)->sw_if_index[VLIB_TX] =
188                  adj1[0].rewrite_header.sw_if_index;
189              next1 = NSH_OUTPUT_NEXT_INTERFACE;
190              error1 = IP4_ERROR_NONE;
191
192              if (PREDICT_FALSE(adj1[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
193                vnet_feature_arc_start (nm->output_feature_arc_index,
194                                        adj1[0].rewrite_header.sw_if_index,
195                                        &next1, p1);
196            }
197          else
198            {
199              error1 = IP4_ERROR_MTU_EXCEEDED;
200              next1 = NSH_OUTPUT_NEXT_DROP;
201            }
202          if (is_midchain)
203          {
204              adj0->sub_type.midchain.fixup_func(vm, adj0, p0);
205              adj1->sub_type.midchain.fixup_func(vm, adj1, p1);
206          }
207
208          p0->error = error_node->errors[error0];
209          p1->error = error_node->errors[error1];
210
211          if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED))
212            {
213              nsh_output_trace_t *tr = vlib_add_trace (vm, node,
214                                                        p0, sizeof (*tr));
215              tr->adj_index = vnet_buffer(p0)->ip.adj_index[VLIB_TX];
216              tr->flow_hash = vnet_buffer(p0)->ip.flow_hash;
217            }
218          if (PREDICT_FALSE(p1->flags & VLIB_BUFFER_IS_TRACED))
219            {
220              nsh_output_trace_t *tr = vlib_add_trace (vm, node,
221                                                        p1, sizeof (*tr));
222              tr->adj_index = vnet_buffer(p1)->ip.adj_index[VLIB_TX];
223              tr->flow_hash = vnet_buffer(p1)->ip.flow_hash;
224            }
225
226          vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
227                                           to_next, n_left_to_next,
228                                           pi0, pi1, next0, next1);
229        }
230
231      while (n_left_from > 0 && n_left_to_next > 0)
232        {
233          ip_adjacency_t * adj0;
234          nsh_base_header_t *hdr0;
235          ethernet_header_t * eth_hdr0;
236          vlib_buffer_t * p0;
237          u32 pi0, rw_len0, adj_index0, next0, error0;
238
239          pi0 = to_next[0] = from[0];
240
241          p0 = vlib_get_buffer (vm, pi0);
242
243          adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
244
245          adj0 = adj_get(adj_index0);
246          hdr0 = vlib_buffer_get_current (p0);
247
248          /* Guess we are only writing on simple Ethernet header. */
249          vnet_rewrite_one_header (adj0[0], hdr0,
250                                   sizeof (ethernet_header_t));
251
252          eth_hdr0 = (ethernet_header_t*)((u8 *)hdr0-sizeof(ethernet_header_t));
253          eth_hdr0->type = clib_host_to_net_u16(ETHERNET_TYPE_NSH);
254
255          /* Update packet buffer attributes/set output interface. */
256          rw_len0 = adj0[0].rewrite_header.data_bytes;
257
258          vlib_increment_combined_counter
259              (&adjacency_counters,
260               thread_index,
261               adj_index0,
262               1,
263               vlib_buffer_length_in_chain (vm, p0) + rw_len0);
264
265          /* Check MTU of outgoing interface. */
266          if (PREDICT_TRUE(vlib_buffer_length_in_chain (vm, p0) <=
267                           adj0[0].rewrite_header.max_l3_packet_bytes))
268            {
269              p0->current_data -= rw_len0;
270              p0->current_length += rw_len0;
271
272              vnet_buffer (p0)->sw_if_index[VLIB_TX] =
273                  adj0[0].rewrite_header.sw_if_index;
274              next0 = NSH_OUTPUT_NEXT_INTERFACE;
275              error0 = IP4_ERROR_NONE;
276
277              if (PREDICT_FALSE(adj0[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
278                vnet_feature_arc_start (nm->output_feature_arc_index,
279                                        adj0[0].rewrite_header.sw_if_index,
280                                        &next0, p0);
281            }
282          else
283            {
284              error0 = IP4_ERROR_MTU_EXCEEDED;
285              next0 = NSH_OUTPUT_NEXT_DROP;
286            }
287          if (is_midchain)
288          {
289              adj0->sub_type.midchain.fixup_func(vm, adj0, p0);
290          }
291
292          p0->error = error_node->errors[error0];
293
294          from += 1;
295          n_left_from -= 1;
296          to_next += 1;
297          n_left_to_next -= 1;
298
299          if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED))
300            {
301              nsh_output_trace_t *tr = vlib_add_trace (vm, node,
302                                                        p0, sizeof (*tr));
303              tr->adj_index = vnet_buffer(p0)->ip.adj_index[VLIB_TX];
304              tr->flow_hash = vnet_buffer(p0)->ip.flow_hash;
305            }
306
307          vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
308                                           to_next, n_left_to_next,
309                                           pi0, next0);
310        }
311
312      vlib_put_next_frame (vm, node, next_index, n_left_to_next);
313    }
314
315  return from_frame->n_vectors;
316}
317
318typedef enum nsh_midchain_next_t_
319{
320    NSH_MIDCHAIN_NEXT_DROP,
321} nsh_midchain_next_t;
322
323static inline uword
324nsh_eth_output (vlib_main_t * vm,
325                vlib_node_runtime_t * node,
326                vlib_frame_t * from_frame)
327{
328    return (nsh_output_inline(vm, node, from_frame, /* is_midchain */ 0));
329}
330
331VLIB_REGISTER_NODE (nsh_eth_output_node) = {
332  .function = nsh_eth_output,
333  .name = "nsh-eth-output",
334  /* Takes a vector of packets. */
335  .vector_size = sizeof (u32),
336  .n_next_nodes = NSH_OUTPUT_N_NEXT,
337  .next_nodes = {
338#define _(s,n) [NSH_OUTPUT_NEXT_##s] = n,
339    foreach_nsh_output_next
340#undef _
341  },
342
343  .format_trace = format_nsh_output_trace,
344};
345
346VLIB_NODE_FUNCTION_MULTIARCH (nsh_eth_output_node, nsh_eth_output)
347
348static inline uword
349nsh_midchain (vlib_main_t * vm,
350               vlib_node_runtime_t * node,
351               vlib_frame_t * from_frame)
352{
353    return (nsh_output_inline(vm, node, from_frame, /* is_midchain */ 1));
354}
355
356VLIB_REGISTER_NODE (nsh_midchain_node) = {
357  .function = nsh_midchain,
358  .name = "nsh-midchain",
359  .vector_size = sizeof (u32),
360  .format_trace = format_nsh_output_trace,
361  .n_next_nodes = 1,
362  .next_nodes = {
363      [NSH_MIDCHAIN_NEXT_DROP] = "error-drop",
364  },
365};
366
367VLIB_NODE_FUNCTION_MULTIARCH (nsh_midchain_node, nsh_midchain)
368
369/* Built-in nsh tx feature path definition */
370VNET_FEATURE_INIT (nsh_interface_output, static) = {
371  .arc_name = "nsh-eth-output",
372  .node_name = "interface-output",
373  .runs_before = 0, /* not before any other features */
374};
375
376/* Built-in ip4 tx feature path definition */
377/* *INDENT-OFF* */
378VNET_FEATURE_ARC_INIT (nsh_eth_output, static) =
379{
380  .arc_name  = "nsh-eth-output",
381  .start_nodes = VNET_FEATURES ("nsh-midchain"),
382};
383
384VNET_FEATURE_INIT (nsh_eth_tx_drop, static) =
385{
386  .arc_name = "nsh-eth-output",
387  .node_name = "error-drop",
388  .runs_before = 0,     /* not before any other features */
389};
390/* *INDENT-ON* */
391/**
392 * @brief Next index values from the NSH incomplete adj node
393 */
394#define foreach_nsh_adj_incomplete_next       	\
395_(DROP, "error-drop")                   \
396_(IP4,  "ip4-arp")                      \
397_(IP6,  "ip6-discover-neighbor")
398
399typedef enum {
400#define _(s,n) NSH_ADJ_INCOMPLETE_NEXT_##s,
401  foreach_nsh_adj_incomplete_next
402#undef _
403  NSH_ADJ_INCOMPLETE_N_NEXT,
404} nsh_adj_incomplete_next_t;
405
406/**
407 * @brief A struct to hold tracing information for the NSH label imposition
408 * node.
409 */
410typedef struct nsh_adj_incomplete_trace_t_
411{
412    u32 next;
413} nsh_adj_incomplete_trace_t;
414
415
416/**
417 * @brief Graph node for incomplete NSH adjacency.
418 * This node will push traffic to either the v4-arp or v6-nd node
419 * based on the next-hop proto of the adj.
420 * We pay a cost for this 'routing' node, but an incomplete adj is the
421 * exception case.
422 */
423static inline uword
424nsh_adj_incomplete (vlib_main_t * vm,
425                     vlib_node_runtime_t * node,
426                     vlib_frame_t * from_frame)
427{
428  u32 n_left_from, next_index, * from, * to_next;
429
430  from = vlib_frame_vector_args (from_frame);
431  n_left_from = from_frame->n_vectors;
432  next_index = node->cached_next_index;
433
434  while (n_left_from > 0)
435    {
436      u32 n_left_to_next;
437
438      vlib_get_next_frame (vm, node, next_index,
439                           to_next, n_left_to_next);
440
441      while (n_left_from > 0 && n_left_to_next > 0)
442        {
443          u32 pi0, next0, adj_index0;
444          ip_adjacency_t * adj0;
445          vlib_buffer_t * p0;
446
447          pi0 = to_next[0] = from[0];
448          p0 = vlib_get_buffer (vm, pi0);
449          from += 1;
450          n_left_from -= 1;
451          to_next += 1;
452          n_left_to_next -= 1;
453
454          adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
455
456          adj0 = adj_get(adj_index0);
457
458          if (PREDICT_TRUE(FIB_PROTOCOL_IP4 == adj0->ia_nh_proto))
459          {
460              next0 = NSH_ADJ_INCOMPLETE_NEXT_IP4;
461          }
462          else
463          {
464              next0 = NSH_ADJ_INCOMPLETE_NEXT_IP6;
465          }
466
467          if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED))
468          {
469              nsh_adj_incomplete_trace_t *tr =
470                 vlib_add_trace (vm, node, p0, sizeof (*tr));
471              tr->next = next0;
472          }
473
474          vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
475                                           to_next, n_left_to_next,
476                                           pi0, next0);
477        }
478
479      vlib_put_next_frame (vm, node, next_index, n_left_to_next);
480    }
481
482  return from_frame->n_vectors;
483}
484
485static u8 *
486format_nsh_adj_incomplete_trace (u8 * s, va_list * args)
487{
488    CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
489    CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
490    nsh_adj_incomplete_trace_t * t;
491    uword indent;
492
493    t = va_arg (*args, nsh_adj_incomplete_trace_t *);
494    indent = format_get_indent (s);
495
496    s = format (s, "%Unext:%d",
497                format_white_space, indent,
498                t->next);
499    return (s);
500}
501
502VLIB_REGISTER_NODE (nsh_adj_incomplete_node) = {
503  .function = nsh_adj_incomplete,
504  .name = "nsh-adj-incomplete",
505  .format_trace = format_nsh_adj_incomplete_trace,
506  /* Takes a vector of packets. */
507  .vector_size = sizeof (u32),
508  .n_next_nodes = NSH_ADJ_INCOMPLETE_N_NEXT,
509  .next_nodes = {
510#define _(s,n) [NSH_ADJ_INCOMPLETE_NEXT_##s] = n,
511    foreach_nsh_adj_incomplete_next
512#undef _
513  },
514};
515
516VLIB_NODE_FUNCTION_MULTIARCH (nsh_adj_incomplete_node,
517                              nsh_adj_incomplete)
518