tap: add support for persistance
[vpp.git] / src / vnet / interface_output.c
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 /*
16  * interface_output.c: interface output node
17  *
18  * Copyright (c) 2008 Eliot Dresselhaus
19  *
20  * Permission is hereby granted, free of charge, to any person obtaining
21  * a copy of this software and associated documentation files (the
22  * "Software"), to deal in the Software without restriction, including
23  * without limitation the rights to use, copy, modify, merge, publish,
24  * distribute, sublicense, and/or sell copies of the Software, and to
25  * permit persons to whom the Software is furnished to do so, subject to
26  * the following conditions:
27  *
28  * The above copyright notice and this permission notice shall be
29  * included in all copies or substantial portions of the Software.
30  *
31  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38  */
39
40 #include <vnet/vnet.h>
41 #include <vnet/gso/gso.h>
42 #include <vnet/ip/icmp46_packet.h>
43 #include <vnet/ip/ip4.h>
44 #include <vnet/ip/ip6.h>
45 #include <vnet/udp/udp_packet.h>
46 #include <vnet/feature/feature.h>
47 #include <vnet/classify/trace_classify.h>
48
49 typedef struct
50 {
51   u32 sw_if_index;
52   u32 flags;
53   u8 data[128 - 2 * sizeof (u32)];
54 }
55 interface_output_trace_t;
56
57 #ifndef CLIB_MARCH_VARIANT
58 u8 *
59 format_vnet_interface_output_trace (u8 * s, va_list * va)
60 {
61   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
62   vlib_node_t *node = va_arg (*va, vlib_node_t *);
63   interface_output_trace_t *t = va_arg (*va, interface_output_trace_t *);
64   vnet_main_t *vnm = vnet_get_main ();
65   vnet_sw_interface_t *si;
66   u32 indent;
67
68   if (t->sw_if_index != (u32) ~ 0)
69     {
70       indent = format_get_indent (s);
71
72       if (pool_is_free_index
73           (vnm->interface_main.sw_interfaces, t->sw_if_index))
74         {
75           /* the interface may have been deleted by the time the trace is printed */
76           s = format (s, "sw_if_index: %d ", t->sw_if_index);
77         }
78       else
79         {
80           si = vnet_get_sw_interface (vnm, t->sw_if_index);
81           s =
82             format (s, "%U ", format_vnet_sw_interface_name, vnm, si,
83                     t->flags);
84         }
85       s =
86         format (s, "\n%U%U", format_white_space, indent,
87                 node->format_buffer ? node->format_buffer : format_hex_bytes,
88                 t->data, sizeof (t->data));
89     }
90   return s;
91 }
92
93 static void
94 vnet_interface_output_trace (vlib_main_t * vm,
95                              vlib_node_runtime_t * node,
96                              vlib_frame_t * frame, uword n_buffers)
97 {
98   u32 n_left, *from;
99
100   n_left = n_buffers;
101   from = vlib_frame_vector_args (frame);
102
103   while (n_left >= 4)
104     {
105       u32 bi0, bi1;
106       vlib_buffer_t *b0, *b1;
107       interface_output_trace_t *t0, *t1;
108
109       /* Prefetch next iteration. */
110       vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
111       vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
112
113       bi0 = from[0];
114       bi1 = from[1];
115
116       b0 = vlib_get_buffer (vm, bi0);
117       b1 = vlib_get_buffer (vm, bi1);
118
119       if (b0->flags & VLIB_BUFFER_IS_TRACED)
120         {
121           t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
122           t0->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX];
123           t0->flags = b0->flags;
124           clib_memcpy_fast (t0->data, vlib_buffer_get_current (b0),
125                             sizeof (t0->data));
126         }
127       if (b1->flags & VLIB_BUFFER_IS_TRACED)
128         {
129           t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0]));
130           t1->sw_if_index = vnet_buffer (b1)->sw_if_index[VLIB_TX];
131           t1->flags = b1->flags;
132           clib_memcpy_fast (t1->data, vlib_buffer_get_current (b1),
133                             sizeof (t1->data));
134         }
135       from += 2;
136       n_left -= 2;
137     }
138
139   while (n_left >= 1)
140     {
141       u32 bi0;
142       vlib_buffer_t *b0;
143       interface_output_trace_t *t0;
144
145       bi0 = from[0];
146
147       b0 = vlib_get_buffer (vm, bi0);
148
149       if (b0->flags & VLIB_BUFFER_IS_TRACED)
150         {
151           t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
152           t0->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX];
153           t0->flags = b0->flags;
154           clib_memcpy_fast (t0->data, vlib_buffer_get_current (b0),
155                             sizeof (t0->data));
156         }
157       from += 1;
158       n_left -= 1;
159     }
160 }
161
162 static_always_inline void
163 calc_checksums (vlib_main_t * vm, vlib_buffer_t * b)
164 {
165   tcp_header_t *th;
166   udp_header_t *uh;
167   gso_header_offset_t gho = { 0 };
168
169   int is_ip4 = (b->flags & VNET_BUFFER_F_IS_IP4) != 0;
170   int is_ip6 = (b->flags & VNET_BUFFER_F_IS_IP6) != 0;
171
172   ASSERT (!(is_ip4 && is_ip6));
173
174   gho = vnet_gso_header_offset_parser (b, is_ip6);
175   th = (tcp_header_t *) (vlib_buffer_get_current (b) + gho.l4_hdr_offset);
176   uh = (udp_header_t *) (vlib_buffer_get_current (b) + gho.l4_hdr_offset);
177
178   if (is_ip4)
179     {
180       ip4_header_t *ip4;
181
182       ip4 =
183         (ip4_header_t *) (vlib_buffer_get_current (b) + gho.l3_hdr_offset);
184       if (b->flags & VNET_BUFFER_F_OFFLOAD_IP_CKSUM)
185         ip4->checksum = ip4_header_checksum (ip4);
186       if (b->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM)
187         {
188           th->checksum = 0;
189           th->checksum = ip4_tcp_udp_compute_checksum (vm, b, ip4);
190         }
191       else if (b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM)
192         {
193           uh->checksum = 0;
194           uh->checksum = ip4_tcp_udp_compute_checksum (vm, b, ip4);
195         }
196     }
197   else if (is_ip6)
198     {
199       int bogus;
200       ip6_header_t *ip6;
201
202       ip6 =
203         (ip6_header_t *) (vlib_buffer_get_current (b) + gho.l3_hdr_offset);
204       if (b->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM)
205         {
206           th->checksum = 0;
207           th->checksum =
208             ip6_tcp_udp_icmp_compute_checksum (vm, b, ip6, &bogus);
209         }
210       else if (b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM)
211         {
212           uh->checksum = 0;
213           uh->checksum =
214             ip6_tcp_udp_icmp_compute_checksum (vm, b, ip6, &bogus);
215         }
216     }
217   b->flags &= ~VNET_BUFFER_F_OFFLOAD_TCP_CKSUM;
218   b->flags &= ~VNET_BUFFER_F_OFFLOAD_UDP_CKSUM;
219   b->flags &= ~VNET_BUFFER_F_OFFLOAD_IP_CKSUM;
220 }
221
222 static_always_inline uword
223 vnet_interface_output_node_inline (vlib_main_t * vm,
224                                    vlib_node_runtime_t * node,
225                                    vlib_frame_t * frame,
226                                    vnet_main_t * vnm,
227                                    vnet_hw_interface_t * hi,
228                                    int do_tx_offloads)
229 {
230   vnet_interface_output_runtime_t *rt = (void *) node->runtime_data;
231   vnet_sw_interface_t *si;
232   u32 n_left_to_tx, *from, *from_end, *to_tx;
233   u32 n_bytes, n_buffers, n_packets;
234   u32 n_bytes_b0, n_bytes_b1, n_bytes_b2, n_bytes_b3;
235   u32 thread_index = vm->thread_index;
236   vnet_interface_main_t *im = &vnm->interface_main;
237   u32 next_index = VNET_INTERFACE_OUTPUT_NEXT_TX;
238   u32 current_config_index = ~0;
239   u8 arc = im->output_feature_arc_index;
240   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
241
242   n_buffers = frame->n_vectors;
243
244   if (node->flags & VLIB_NODE_FLAG_TRACE)
245     vnet_interface_output_trace (vm, node, frame, n_buffers);
246
247   from = vlib_frame_vector_args (frame);
248   vlib_get_buffers (vm, from, b, n_buffers);
249
250   if (rt->is_deleted)
251     return vlib_error_drop_buffers (vm, node, from,
252                                     /* buffer stride */ 1,
253                                     n_buffers,
254                                     VNET_INTERFACE_OUTPUT_NEXT_DROP,
255                                     node->node_index,
256                                     VNET_INTERFACE_OUTPUT_ERROR_INTERFACE_DELETED);
257
258   si = vnet_get_sw_interface (vnm, rt->sw_if_index);
259   hi = vnet_get_sup_hw_interface (vnm, rt->sw_if_index);
260   if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ||
261       !(hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
262     {
263       vlib_simple_counter_main_t *cm;
264
265       cm = vec_elt_at_index (vnm->interface_main.sw_if_counters,
266                              VNET_INTERFACE_COUNTER_TX_ERROR);
267       vlib_increment_simple_counter (cm, thread_index,
268                                      rt->sw_if_index, n_buffers);
269
270       return vlib_error_drop_buffers (vm, node, from,
271                                       /* buffer stride */ 1,
272                                       n_buffers,
273                                       VNET_INTERFACE_OUTPUT_NEXT_DROP,
274                                       node->node_index,
275                                       VNET_INTERFACE_OUTPUT_ERROR_INTERFACE_DOWN);
276     }
277
278   from_end = from + n_buffers;
279
280   /* Total byte count of all buffers. */
281   n_bytes = 0;
282   n_packets = 0;
283
284   /* interface-output feature arc handling */
285   if (PREDICT_FALSE (vnet_have_features (arc, rt->sw_if_index)))
286     {
287       vnet_feature_config_main_t *fcm;
288       fcm = vnet_feature_get_config_main (arc);
289       current_config_index = vnet_get_feature_config_index (arc,
290                                                             rt->sw_if_index);
291       vnet_get_config_data (&fcm->config_main, &current_config_index,
292                             &next_index, 0);
293     }
294
295   while (from < from_end)
296     {
297       /* Get new next frame since previous incomplete frame may have less
298          than VNET_FRAME_SIZE vectors in it. */
299       vlib_get_new_next_frame (vm, node, next_index, to_tx, n_left_to_tx);
300
301       while (from + 8 <= from_end && n_left_to_tx >= 4)
302         {
303           u32 bi0, bi1, bi2, bi3;
304           u32 tx_swif0, tx_swif1, tx_swif2, tx_swif3;
305           u32 or_flags;
306
307           /* Prefetch next iteration. */
308           vlib_prefetch_buffer_header (b[4], LOAD);
309           vlib_prefetch_buffer_header (b[5], LOAD);
310           vlib_prefetch_buffer_header (b[6], LOAD);
311           vlib_prefetch_buffer_header (b[7], LOAD);
312
313           bi0 = from[0];
314           bi1 = from[1];
315           bi2 = from[2];
316           bi3 = from[3];
317           to_tx[0] = bi0;
318           to_tx[1] = bi1;
319           to_tx[2] = bi2;
320           to_tx[3] = bi3;
321
322           or_flags = b[0]->flags | b[1]->flags | b[2]->flags | b[3]->flags;
323
324           from += 4;
325           to_tx += 4;
326           n_left_to_tx -= 4;
327
328           /* Be grumpy about zero length buffers for benefit of
329              driver tx function. */
330           ASSERT (b[0]->current_length > 0);
331           ASSERT (b[1]->current_length > 0);
332           ASSERT (b[2]->current_length > 0);
333           ASSERT (b[3]->current_length > 0);
334
335           n_bytes_b0 = vlib_buffer_length_in_chain (vm, b[0]);
336           n_bytes_b1 = vlib_buffer_length_in_chain (vm, b[1]);
337           n_bytes_b2 = vlib_buffer_length_in_chain (vm, b[2]);
338           n_bytes_b3 = vlib_buffer_length_in_chain (vm, b[3]);
339           tx_swif0 = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
340           tx_swif1 = vnet_buffer (b[1])->sw_if_index[VLIB_TX];
341           tx_swif2 = vnet_buffer (b[2])->sw_if_index[VLIB_TX];
342           tx_swif3 = vnet_buffer (b[3])->sw_if_index[VLIB_TX];
343
344           n_bytes += n_bytes_b0 + n_bytes_b1;
345           n_bytes += n_bytes_b2 + n_bytes_b3;
346           n_packets += 4;
347
348           if (PREDICT_FALSE (current_config_index != ~0))
349             {
350               vnet_buffer (b[0])->feature_arc_index = arc;
351               vnet_buffer (b[1])->feature_arc_index = arc;
352               vnet_buffer (b[2])->feature_arc_index = arc;
353               vnet_buffer (b[3])->feature_arc_index = arc;
354               b[0]->current_config_index = current_config_index;
355               b[1]->current_config_index = current_config_index;
356               b[2]->current_config_index = current_config_index;
357               b[3]->current_config_index = current_config_index;
358             }
359
360           /* update vlan subif tx counts, if required */
361           if (PREDICT_FALSE (tx_swif0 != rt->sw_if_index))
362             {
363               vlib_increment_combined_counter (im->combined_sw_if_counters +
364                                                VNET_INTERFACE_COUNTER_TX,
365                                                thread_index, tx_swif0, 1,
366                                                n_bytes_b0);
367             }
368
369           if (PREDICT_FALSE (tx_swif1 != rt->sw_if_index))
370             {
371
372               vlib_increment_combined_counter (im->combined_sw_if_counters +
373                                                VNET_INTERFACE_COUNTER_TX,
374                                                thread_index, tx_swif1, 1,
375                                                n_bytes_b1);
376             }
377
378           if (PREDICT_FALSE (tx_swif2 != rt->sw_if_index))
379             {
380
381               vlib_increment_combined_counter (im->combined_sw_if_counters +
382                                                VNET_INTERFACE_COUNTER_TX,
383                                                thread_index, tx_swif2, 1,
384                                                n_bytes_b2);
385             }
386           if (PREDICT_FALSE (tx_swif3 != rt->sw_if_index))
387             {
388
389               vlib_increment_combined_counter (im->combined_sw_if_counters +
390                                                VNET_INTERFACE_COUNTER_TX,
391                                                thread_index, tx_swif3, 1,
392                                                n_bytes_b3);
393             }
394
395           if (do_tx_offloads)
396             {
397               if (or_flags &
398                   (VNET_BUFFER_F_OFFLOAD_TCP_CKSUM |
399                    VNET_BUFFER_F_OFFLOAD_UDP_CKSUM |
400                    VNET_BUFFER_F_OFFLOAD_IP_CKSUM))
401                 {
402                   calc_checksums (vm, b[0]);
403                   calc_checksums (vm, b[1]);
404                   calc_checksums (vm, b[2]);
405                   calc_checksums (vm, b[3]);
406                 }
407             }
408           b += 4;
409
410         }
411
412       while (from + 1 <= from_end && n_left_to_tx >= 1)
413         {
414           u32 bi0;
415           u32 tx_swif0;
416
417           bi0 = from[0];
418           to_tx[0] = bi0;
419           from += 1;
420           to_tx += 1;
421           n_left_to_tx -= 1;
422
423           /* Be grumpy about zero length buffers for benefit of
424              driver tx function. */
425           ASSERT (b[0]->current_length > 0);
426
427           n_bytes_b0 = vlib_buffer_length_in_chain (vm, b[0]);
428           tx_swif0 = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
429           n_bytes += n_bytes_b0;
430           n_packets += 1;
431
432           if (PREDICT_FALSE (current_config_index != ~0))
433             {
434               vnet_buffer (b[0])->feature_arc_index = arc;
435               b[0]->current_config_index = current_config_index;
436             }
437
438           if (PREDICT_FALSE (tx_swif0 != rt->sw_if_index))
439             {
440
441               vlib_increment_combined_counter (im->combined_sw_if_counters +
442                                                VNET_INTERFACE_COUNTER_TX,
443                                                thread_index, tx_swif0, 1,
444                                                n_bytes_b0);
445             }
446
447           if (do_tx_offloads)
448             {
449               if (b[0]->flags &
450                   (VNET_BUFFER_F_OFFLOAD_TCP_CKSUM |
451                    VNET_BUFFER_F_OFFLOAD_UDP_CKSUM |
452                    VNET_BUFFER_F_OFFLOAD_IP_CKSUM))
453                 calc_checksums (vm, b[0]);
454             }
455           b += 1;
456         }
457
458       vlib_put_next_frame (vm, node, next_index, n_left_to_tx);
459     }
460
461   /* Update main interface stats. */
462   vlib_increment_combined_counter (im->combined_sw_if_counters
463                                    + VNET_INTERFACE_COUNTER_TX,
464                                    thread_index,
465                                    rt->sw_if_index, n_packets, n_bytes);
466   return n_buffers;
467 }
468 #endif /* CLIB_MARCH_VARIANT */
469
470 static_always_inline void vnet_interface_pcap_tx_trace
471   (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame,
472    int sw_if_index_from_buffer)
473 {
474   u32 n_left_from, *from;
475   u32 sw_if_index;
476   vnet_pcap_t *pp = &vlib_global_main.pcap;
477
478   if (PREDICT_TRUE (pp->pcap_tx_enable == 0))
479     return;
480
481   if (sw_if_index_from_buffer == 0)
482     {
483       vnet_interface_output_runtime_t *rt = (void *) node->runtime_data;
484       sw_if_index = rt->sw_if_index;
485     }
486   else
487     sw_if_index = ~0;
488
489   n_left_from = frame->n_vectors;
490   from = vlib_frame_vector_args (frame);
491
492   while (n_left_from > 0)
493     {
494       int classify_filter_result;
495       u32 bi0 = from[0];
496       vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
497       from++;
498       n_left_from--;
499
500       if (pp->filter_classify_table_index != ~0)
501         {
502           classify_filter_result =
503             vnet_is_packet_traced_inline
504             (b0, pp->filter_classify_table_index, 0 /* full classify */ );
505           if (classify_filter_result)
506             pcap_add_buffer (&pp->pcap_main, vm, bi0, pp->max_bytes_per_pkt);
507           continue;
508         }
509
510       if (sw_if_index_from_buffer)
511         sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX];
512
513       if (pp->pcap_sw_if_index == 0 || pp->pcap_sw_if_index == sw_if_index)
514         {
515           vnet_main_t *vnm = vnet_get_main ();
516           vnet_hw_interface_t *hi =
517             vnet_get_sup_hw_interface (vnm, sw_if_index);
518           /* Capture pkt if not filtered, or if filter hits */
519           if (hi->trace_classify_table_index == ~0 ||
520               vnet_is_packet_traced_inline
521               (b0, hi->trace_classify_table_index, 0 /* full classify */ ))
522             pcap_add_buffer (&pp->pcap_main, vm, bi0, pp->max_bytes_per_pkt);
523         }
524     }
525 }
526
527 #ifndef CLIB_MARCH_VARIANT
528
529 uword
530 vnet_interface_output_node (vlib_main_t * vm, vlib_node_runtime_t * node,
531                             vlib_frame_t * frame)
532 {
533   vnet_main_t *vnm = vnet_get_main ();
534   vnet_hw_interface_t *hi;
535   vnet_interface_output_runtime_t *rt = (void *) node->runtime_data;
536   hi = vnet_get_sup_hw_interface (vnm, rt->sw_if_index);
537
538   vnet_interface_pcap_tx_trace (vm, node, frame,
539                                 0 /* sw_if_index_from_buffer */ );
540
541   if (hi->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD)
542     return vnet_interface_output_node_inline (vm, node, frame, vnm, hi,
543                                               /* do_tx_offloads */ 0);
544   else
545     return vnet_interface_output_node_inline (vm, node, frame, vnm, hi,
546                                               /* do_tx_offloads */ 1);
547 }
548 #endif /* CLIB_MARCH_VARIANT */
549
550 /* Use buffer's sw_if_index[VNET_TX] to choose output interface. */
551 VLIB_NODE_FN (vnet_per_buffer_interface_output_node) (vlib_main_t * vm,
552                                                       vlib_node_runtime_t *
553                                                       node,
554                                                       vlib_frame_t * frame)
555 {
556   vnet_main_t *vnm = vnet_get_main ();
557   u32 n_left_to_next, *from, *to_next;
558   u32 n_left_from, next_index;
559
560   vnet_interface_pcap_tx_trace (vm, node, frame,
561                                 1 /* sw_if_index_from_buffer */ );
562
563   n_left_from = frame->n_vectors;
564
565   from = vlib_frame_vector_args (frame);
566   next_index = node->cached_next_index;
567
568   while (n_left_from > 0)
569     {
570       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
571
572       while (n_left_from >= 4 && n_left_to_next >= 2)
573         {
574           u32 bi0, bi1, next0, next1;
575           vlib_buffer_t *b0, *b1;
576           vnet_hw_interface_t *hi0, *hi1;
577
578           /* Prefetch next iteration. */
579           vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
580           vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
581
582           bi0 = from[0];
583           bi1 = from[1];
584           to_next[0] = bi0;
585           to_next[1] = bi1;
586           from += 2;
587           to_next += 2;
588           n_left_to_next -= 2;
589           n_left_from -= 2;
590
591           b0 = vlib_get_buffer (vm, bi0);
592           b1 = vlib_get_buffer (vm, bi1);
593
594           hi0 =
595             vnet_get_sup_hw_interface (vnm,
596                                        vnet_buffer (b0)->sw_if_index
597                                        [VLIB_TX]);
598           hi1 =
599             vnet_get_sup_hw_interface (vnm,
600                                        vnet_buffer (b1)->sw_if_index
601                                        [VLIB_TX]);
602
603           next0 = hi0->output_node_next_index;
604           next1 = hi1->output_node_next_index;
605
606           vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
607                                            n_left_to_next, bi0, bi1, next0,
608                                            next1);
609         }
610
611       while (n_left_from > 0 && n_left_to_next > 0)
612         {
613           u32 bi0, next0;
614           vlib_buffer_t *b0;
615           vnet_hw_interface_t *hi0;
616
617           bi0 = from[0];
618           to_next[0] = bi0;
619           from += 1;
620           to_next += 1;
621           n_left_to_next -= 1;
622           n_left_from -= 1;
623
624           b0 = vlib_get_buffer (vm, bi0);
625
626           hi0 =
627             vnet_get_sup_hw_interface (vnm,
628                                        vnet_buffer (b0)->sw_if_index
629                                        [VLIB_TX]);
630
631           next0 = hi0->output_node_next_index;
632
633           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
634                                            n_left_to_next, bi0, next0);
635         }
636
637       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
638     }
639
640   return frame->n_vectors;
641 }
642
643 typedef struct vnet_error_trace_t_
644 {
645   u32 sw_if_index;
646 } vnet_error_trace_t;
647
648
649 static u8 *
650 format_vnet_error_trace (u8 * s, va_list * va)
651 {
652   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
653   CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
654   vnet_error_trace_t *t = va_arg (*va, vnet_error_trace_t *);
655
656   s = format (s, "rx:%U", format_vnet_sw_if_index_name,
657               vnet_get_main (), t->sw_if_index);
658
659   return s;
660 }
661
662 static void
663 interface_trace_buffers (vlib_main_t * vm,
664                          vlib_node_runtime_t * node, vlib_frame_t * frame)
665 {
666   u32 n_left, *buffers;
667
668   buffers = vlib_frame_vector_args (frame);
669   n_left = frame->n_vectors;
670
671   while (n_left >= 4)
672     {
673       u32 bi0, bi1;
674       vlib_buffer_t *b0, *b1;
675       vnet_error_trace_t *t0, *t1;
676
677       /* Prefetch next iteration. */
678       vlib_prefetch_buffer_with_index (vm, buffers[2], LOAD);
679       vlib_prefetch_buffer_with_index (vm, buffers[3], LOAD);
680
681       bi0 = buffers[0];
682       bi1 = buffers[1];
683
684       b0 = vlib_get_buffer (vm, bi0);
685       b1 = vlib_get_buffer (vm, bi1);
686
687       if (b0->flags & VLIB_BUFFER_IS_TRACED)
688         {
689           t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
690           t0->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
691         }
692       if (b1->flags & VLIB_BUFFER_IS_TRACED)
693         {
694           t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0]));
695           t1->sw_if_index = vnet_buffer (b1)->sw_if_index[VLIB_RX];
696         }
697       buffers += 2;
698       n_left -= 2;
699     }
700
701   while (n_left >= 1)
702     {
703       u32 bi0;
704       vlib_buffer_t *b0;
705       vnet_error_trace_t *t0;
706
707       bi0 = buffers[0];
708
709       b0 = vlib_get_buffer (vm, bi0);
710
711       if (b0->flags & VLIB_BUFFER_IS_TRACED)
712         {
713           t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
714           t0->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
715         }
716       buffers += 1;
717       n_left -= 1;
718     }
719 }
720
721 typedef enum
722 {
723   VNET_ERROR_DISPOSITION_DROP,
724   VNET_ERROR_DISPOSITION_PUNT,
725   VNET_ERROR_N_DISPOSITION,
726 } vnet_error_disposition_t;
727
728 static_always_inline uword
729 interface_drop_punt (vlib_main_t * vm,
730                      vlib_node_runtime_t * node,
731                      vlib_frame_t * frame,
732                      vnet_error_disposition_t disposition)
733 {
734   u32 *from, n_left, thread_index, *sw_if_index;
735   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
736   u32 sw_if_indices[VLIB_FRAME_SIZE];
737   vlib_simple_counter_main_t *cm;
738   u16 nexts[VLIB_FRAME_SIZE];
739   vnet_main_t *vnm;
740
741   vnm = vnet_get_main ();
742   thread_index = vm->thread_index;
743   from = vlib_frame_vector_args (frame);
744   n_left = frame->n_vectors;
745   b = bufs;
746   sw_if_index = sw_if_indices;
747
748   vlib_get_buffers (vm, from, bufs, n_left);
749
750   if (node->flags & VLIB_NODE_FLAG_TRACE)
751     interface_trace_buffers (vm, node, frame);
752
753   /* All going to drop regardless, this is just a counting exercise */
754   clib_memset (nexts, 0, sizeof (nexts));
755
756   cm = vec_elt_at_index (vnm->interface_main.sw_if_counters,
757                          (disposition == VNET_ERROR_DISPOSITION_PUNT
758                           ? VNET_INTERFACE_COUNTER_PUNT
759                           : VNET_INTERFACE_COUNTER_DROP));
760
761   /* collect the array of interfaces first ... */
762   while (n_left >= 4)
763     {
764       if (n_left >= 12)
765         {
766           /* Prefetch 8 ahead - there's not much going on in each iteration */
767           vlib_prefetch_buffer_header (b[4], LOAD);
768           vlib_prefetch_buffer_header (b[5], LOAD);
769           vlib_prefetch_buffer_header (b[6], LOAD);
770           vlib_prefetch_buffer_header (b[7], LOAD);
771         }
772       sw_if_index[0] = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
773       sw_if_index[1] = vnet_buffer (b[1])->sw_if_index[VLIB_RX];
774       sw_if_index[2] = vnet_buffer (b[2])->sw_if_index[VLIB_RX];
775       sw_if_index[3] = vnet_buffer (b[3])->sw_if_index[VLIB_RX];
776
777       sw_if_index += 4;
778       n_left -= 4;
779       b += 4;
780     }
781   while (n_left)
782     {
783       sw_if_index[0] = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
784
785       sw_if_index += 1;
786       n_left -= 1;
787       b += 1;
788     }
789
790   /* ... then count against them in blocks */
791   n_left = frame->n_vectors;
792
793   while (n_left)
794     {
795       vnet_sw_interface_t *sw_if0;
796       u16 off, count;
797
798       off = frame->n_vectors - n_left;
799
800       sw_if_index = sw_if_indices + off;
801
802       count = clib_count_equal_u32 (sw_if_index, n_left);
803       n_left -= count;
804
805       vlib_increment_simple_counter (cm, thread_index, sw_if_index[0], count);
806
807       /* Increment super-interface drop/punt counters for
808          sub-interfaces. */
809       sw_if0 = vnet_get_sw_interface (vnm, sw_if_index[0]);
810       if (sw_if0->sup_sw_if_index != sw_if_index[0])
811         vlib_increment_simple_counter
812           (cm, thread_index, sw_if0->sup_sw_if_index, count);
813     }
814
815   vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
816
817   return frame->n_vectors;
818 }
819
820 static inline void
821 pcap_drop_trace (vlib_main_t * vm,
822                  vnet_interface_main_t * im,
823                  vnet_pcap_t * pp, vlib_frame_t * f)
824 {
825   u32 *from;
826   u32 n_left = f->n_vectors;
827   vlib_buffer_t *b0, *p1;
828   u32 bi0;
829   i16 save_current_data;
830   u16 save_current_length;
831   vlib_error_main_t *em = &vm->error_main;
832   int do_trace = 0;
833
834
835   from = vlib_frame_vector_args (f);
836
837   while (n_left > 0)
838     {
839       if (PREDICT_TRUE (n_left > 1))
840         {
841           p1 = vlib_get_buffer (vm, from[1]);
842           vlib_prefetch_buffer_header (p1, LOAD);
843         }
844
845       bi0 = from[0];
846       b0 = vlib_get_buffer (vm, bi0);
847       from++;
848       n_left--;
849
850       /* See if we're pointedly ignoring this specific error */
851       if (im->pcap_drop_filter_hash
852           && hash_get (im->pcap_drop_filter_hash, b0->error))
853         continue;
854
855       do_trace = (pp->pcap_sw_if_index == 0) ||
856         pp->pcap_sw_if_index == vnet_buffer (b0)->sw_if_index[VLIB_RX];
857
858       if (PREDICT_FALSE
859           (do_trace == 0 && pp->filter_classify_table_index != ~0))
860         {
861           do_trace = vnet_is_packet_traced_inline
862             (b0, pp->filter_classify_table_index, 0 /* full classify */ );
863         }
864
865       /* Trace all drops, or drops received on a specific interface */
866       if (do_trace)
867         {
868           save_current_data = b0->current_data;
869           save_current_length = b0->current_length;
870
871           /*
872            * Typically, we'll need to rewind the buffer
873            * if l2_hdr_offset is valid, make sure to rewind to the start of
874            * the L2 header. This may not be the buffer start in case we pop-ed
875            * vlan tags.
876            * Otherwise, rewind to buffer start and hope for the best.
877            */
878           if (b0->flags & VNET_BUFFER_F_L2_HDR_OFFSET_VALID)
879             {
880               if (b0->current_data > vnet_buffer (b0)->l2_hdr_offset)
881                 vlib_buffer_advance (b0,
882                                      vnet_buffer (b0)->l2_hdr_offset -
883                                      b0->current_data);
884             }
885           else if (b0->current_data > 0)
886             vlib_buffer_advance (b0, (word) - b0->current_data);
887
888           {
889             vlib_buffer_t *last = b0;
890             u32 error_node_index;
891             int drop_string_len;
892             vlib_node_t *n;
893             /* Length of the error string */
894             int error_string_len =
895               clib_strnlen (em->error_strings_heap[b0->error], 128);
896
897             /* Dig up the drop node */
898             error_node_index = vm->node_main.node_by_error[b0->error];
899             n = vlib_get_node (vm, error_node_index);
900
901             /* Length of full drop string, w/ "nodename: " prepended */
902             drop_string_len = error_string_len + vec_len (n->name) + 2;
903
904             /* Find the last buffer in the chain */
905             while (last->flags & VLIB_BUFFER_NEXT_PRESENT)
906               last = vlib_get_buffer (vm, last->next_buffer);
907
908             /*
909              * Append <nodename>: <error-string> to the capture,
910              * only if we can do that without allocating a new buffer.
911              */
912             if (PREDICT_TRUE ((last->current_data + last->current_length)
913                               < (VLIB_BUFFER_DEFAULT_DATA_SIZE
914                                  - drop_string_len)))
915               {
916                 clib_memcpy_fast (last->data + last->current_data +
917                                   last->current_length, n->name,
918                                   vec_len (n->name));
919                 clib_memcpy_fast (last->data + last->current_data +
920                                   last->current_length + vec_len (n->name),
921                                   ": ", 2);
922                 clib_memcpy_fast (last->data + last->current_data +
923                                   last->current_length + vec_len (n->name) +
924                                   2, em->error_strings_heap[b0->error],
925                                   error_string_len);
926                 last->current_length += drop_string_len;
927                 b0->flags &= ~(VLIB_BUFFER_TOTAL_LENGTH_VALID);
928                 pcap_add_buffer (&pp->pcap_main, vm, bi0,
929                                  pp->max_bytes_per_pkt);
930                 last->current_length -= drop_string_len;
931                 b0->current_data = save_current_data;
932                 b0->current_length = save_current_length;
933                 continue;
934               }
935           }
936
937           /*
938            * Didn't have space in the last buffer, here's the dropped
939            * packet as-is
940            */
941           pcap_add_buffer (&pp->pcap_main, vm, bi0, pp->max_bytes_per_pkt);
942
943           b0->current_data = save_current_data;
944           b0->current_length = save_current_length;
945         }
946     }
947 }
948
949 #ifndef CLIB_MARCH_VARIANT
950 void
951 vnet_pcap_drop_trace_filter_add_del (u32 error_index, int is_add)
952 {
953   vnet_interface_main_t *im = &vnet_get_main ()->interface_main;
954
955   if (im->pcap_drop_filter_hash == 0)
956     im->pcap_drop_filter_hash = hash_create (0, sizeof (uword));
957
958   if (is_add)
959     hash_set (im->pcap_drop_filter_hash, error_index, 1);
960   else
961     hash_unset (im->pcap_drop_filter_hash, error_index);
962 }
963 #endif /* CLIB_MARCH_VARIANT */
964
965 VLIB_NODE_FN (interface_drop) (vlib_main_t * vm,
966                                vlib_node_runtime_t * node,
967                                vlib_frame_t * frame)
968 {
969   vnet_interface_main_t *im = &vnet_get_main ()->interface_main;
970   vnet_pcap_t *pp = &vlib_global_main.pcap;
971
972   if (PREDICT_FALSE (pp->pcap_drop_enable))
973     pcap_drop_trace (vm, im, pp, frame);
974
975   return interface_drop_punt (vm, node, frame, VNET_ERROR_DISPOSITION_DROP);
976 }
977
978 VLIB_NODE_FN (interface_punt) (vlib_main_t * vm,
979                                vlib_node_runtime_t * node,
980                                vlib_frame_t * frame)
981 {
982   return interface_drop_punt (vm, node, frame, VNET_ERROR_DISPOSITION_PUNT);
983 }
984
985 /* *INDENT-OFF* */
986 VLIB_REGISTER_NODE (interface_drop) = {
987   .name = "error-drop",
988   .vector_size = sizeof (u32),
989   .format_trace = format_vnet_error_trace,
990   .n_next_nodes = 1,
991   .next_nodes = {
992     [0] = "drop",
993   },
994 };
995 /* *INDENT-ON* */
996
997 /* *INDENT-OFF* */
998 VLIB_REGISTER_NODE (interface_punt) = {
999   .name = "error-punt",
1000   .vector_size = sizeof (u32),
1001   .format_trace = format_vnet_error_trace,
1002   .n_next_nodes = 1,
1003   .next_nodes = {
1004     [0] = "punt",
1005   },
1006 };
1007 /* *INDENT-ON* */
1008
1009 /* *INDENT-OFF* */
1010 VLIB_REGISTER_NODE (vnet_per_buffer_interface_output_node) = {
1011   .name = "interface-output",
1012   .vector_size = sizeof (u32),
1013 };
1014 /* *INDENT-ON* */
1015
1016 static uword
1017 interface_tx_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
1018                       vlib_frame_t * from_frame)
1019 {
1020   vnet_main_t *vnm = vnet_get_main ();
1021   u32 last_sw_if_index = ~0;
1022   vlib_frame_t *to_frame = 0;
1023   vnet_hw_interface_t *hw = 0;
1024   u32 *from, *to_next = 0;
1025   u32 n_left_from;
1026
1027   from = vlib_frame_vector_args (from_frame);
1028   n_left_from = from_frame->n_vectors;
1029   while (n_left_from > 0)
1030     {
1031       u32 bi0;
1032       vlib_buffer_t *b0;
1033       u32 sw_if_index0;
1034
1035       bi0 = from[0];
1036       from++;
1037       n_left_from--;
1038       b0 = vlib_get_buffer (vm, bi0);
1039       sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
1040
1041       if (PREDICT_FALSE ((last_sw_if_index != sw_if_index0) || to_frame == 0))
1042         {
1043           if (to_frame)
1044             {
1045               hw = vnet_get_sup_hw_interface (vnm, last_sw_if_index);
1046               vlib_put_frame_to_node (vm, hw->tx_node_index, to_frame);
1047             }
1048           last_sw_if_index = sw_if_index0;
1049           hw = vnet_get_sup_hw_interface (vnm, sw_if_index0);
1050           to_frame = vlib_get_frame_to_node (vm, hw->tx_node_index);
1051           to_next = vlib_frame_vector_args (to_frame);
1052         }
1053
1054       to_next[0] = bi0;
1055       to_next++;
1056       to_frame->n_vectors++;
1057     }
1058   vlib_put_frame_to_node (vm, hw->tx_node_index, to_frame);
1059   return from_frame->n_vectors;
1060 }
1061
1062 /* *INDENT-OFF* */
1063 VLIB_REGISTER_NODE (interface_tx) = {
1064   .function = interface_tx_node_fn,
1065   .name = "interface-tx",
1066   .vector_size = sizeof (u32),
1067   .n_next_nodes = 1,
1068   .next_nodes = {
1069     [0] = "error-drop",
1070   },
1071 };
1072
1073 VNET_FEATURE_ARC_INIT (interface_output, static) =
1074 {
1075   .arc_name  = "interface-output",
1076   .start_nodes = VNET_FEATURES (0),
1077   .last_in_arc = "interface-tx",
1078   .arc_index_ptr = &vnet_main.interface_main.output_feature_arc_index,
1079 };
1080
1081 VNET_FEATURE_INIT (span_tx, static) = {
1082   .arc_name = "interface-output",
1083   .node_name = "span-output",
1084   .runs_before = VNET_FEATURES ("interface-tx"),
1085 };
1086
1087 VNET_FEATURE_INIT (ipsec_if_tx, static) = {
1088   .arc_name = "interface-output",
1089   .node_name = "ipsec-if-output",
1090   .runs_before = VNET_FEATURES ("interface-tx"),
1091 };
1092
1093 VNET_FEATURE_INIT (interface_tx, static) = {
1094   .arc_name = "interface-output",
1095   .node_name = "interface-tx",
1096   .runs_before = 0,
1097 };
1098 /* *INDENT-ON* */
1099
1100 #ifndef CLIB_MARCH_VARIANT
1101 clib_error_t *
1102 vnet_per_buffer_interface_output_hw_interface_add_del (vnet_main_t * vnm,
1103                                                        u32 hw_if_index,
1104                                                        u32 is_create)
1105 {
1106   vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
1107   u32 next_index;
1108
1109   if (hi->output_node_index == 0)
1110     return 0;
1111
1112   next_index = vlib_node_add_next
1113     (vnm->vlib_main, vnet_per_buffer_interface_output_node.index,
1114      hi->output_node_index);
1115   hi->output_node_next_index = next_index;
1116
1117   return 0;
1118 }
1119
1120 VNET_HW_INTERFACE_ADD_DEL_FUNCTION
1121   (vnet_per_buffer_interface_output_hw_interface_add_del);
1122
1123 void
1124 vnet_set_interface_output_node (vnet_main_t * vnm,
1125                                 u32 hw_if_index, u32 node_index)
1126 {
1127   ASSERT (node_index);
1128   vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
1129   u32 next_index = vlib_node_add_next
1130     (vnm->vlib_main, vnet_per_buffer_interface_output_node.index, node_index);
1131   hi->output_node_next_index = next_index;
1132   hi->output_node_index = node_index;
1133 }
1134 #endif /* CLIB_MARCH_VARIANT */
1135
1136 /*
1137  * fd.io coding-style-patch-verification: ON
1138  *
1139  * Local Variables:
1140  * eval: (c-set-style "gnu")
1141  * End:
1142  */