gso: fix the segmentation to use current_data offset
[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/ip/icmp46_packet.h>
42 #include <vnet/ip/ip4.h>
43 #include <vnet/ip/ip6.h>
44 #include <vnet/udp/udp_packet.h>
45 #include <vnet/feature/feature.h>
46
47 typedef struct
48 {
49   u32 sw_if_index;
50   u32 flags;
51   u16 gso_size;
52   u8 gso_l4_hdr_sz;
53   u8 data[128 - 3 * 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 #define _(bit, name, v, x) \
86           if (v && (t->flags & VNET_BUFFER_F_##name)) \
87             s = format (s, "%s ", v);
88       foreach_vnet_buffer_flag
89 #undef _
90         if (t->flags & VNET_BUFFER_F_GSO)
91         {
92           s = format (s, "\n%Ugso_sz %d gso_l4_hdr_sz %d",
93                       format_white_space, indent + 2, t->gso_size,
94                       t->gso_l4_hdr_sz);
95         }
96       s =
97         format (s, "\n%U%U", format_white_space, indent,
98                 node->format_buffer ? node->format_buffer : format_hex_bytes,
99                 t->data, sizeof (t->data));
100     }
101   return s;
102 }
103
104 static void
105 vnet_interface_output_trace (vlib_main_t * vm,
106                              vlib_node_runtime_t * node,
107                              vlib_frame_t * frame, uword n_buffers)
108 {
109   u32 n_left, *from;
110
111   n_left = n_buffers;
112   from = vlib_frame_vector_args (frame);
113
114   while (n_left >= 4)
115     {
116       u32 bi0, bi1;
117       vlib_buffer_t *b0, *b1;
118       interface_output_trace_t *t0, *t1;
119
120       /* Prefetch next iteration. */
121       vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
122       vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
123
124       bi0 = from[0];
125       bi1 = from[1];
126
127       b0 = vlib_get_buffer (vm, bi0);
128       b1 = vlib_get_buffer (vm, bi1);
129
130       if (b0->flags & VLIB_BUFFER_IS_TRACED)
131         {
132           t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
133           t0->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX];
134           t0->flags = b0->flags;
135           t0->gso_size = vnet_buffer2 (b0)->gso_size;
136           t0->gso_l4_hdr_sz = vnet_buffer2 (b0)->gso_l4_hdr_sz;
137           clib_memcpy_fast (t0->data, vlib_buffer_get_current (b0),
138                             sizeof (t0->data));
139         }
140       if (b1->flags & VLIB_BUFFER_IS_TRACED)
141         {
142           t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0]));
143           t1->sw_if_index = vnet_buffer (b1)->sw_if_index[VLIB_TX];
144           t1->flags = b1->flags;
145           t1->gso_size = vnet_buffer2 (b1)->gso_size;
146           t1->gso_l4_hdr_sz = vnet_buffer2 (b1)->gso_l4_hdr_sz;
147           clib_memcpy_fast (t1->data, vlib_buffer_get_current (b1),
148                             sizeof (t1->data));
149         }
150       from += 2;
151       n_left -= 2;
152     }
153
154   while (n_left >= 1)
155     {
156       u32 bi0;
157       vlib_buffer_t *b0;
158       interface_output_trace_t *t0;
159
160       bi0 = from[0];
161
162       b0 = vlib_get_buffer (vm, bi0);
163
164       if (b0->flags & VLIB_BUFFER_IS_TRACED)
165         {
166           t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
167           t0->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX];
168           t0->flags = b0->flags;
169           t0->gso_size = vnet_buffer2 (b0)->gso_size;
170           t0->gso_l4_hdr_sz = vnet_buffer2 (b0)->gso_l4_hdr_sz;
171           clib_memcpy_fast (t0->data, vlib_buffer_get_current (b0),
172                             sizeof (t0->data));
173         }
174       from += 1;
175       n_left -= 1;
176     }
177 }
178
179 static_always_inline void
180 calc_checksums (vlib_main_t * vm, vlib_buffer_t * b)
181 {
182   tcp_header_t *th;
183   udp_header_t *uh;
184
185   int is_ip4 = (b->flags & VNET_BUFFER_F_IS_IP4) != 0;
186   int is_ip6 = (b->flags & VNET_BUFFER_F_IS_IP6) != 0;
187
188   ASSERT (!(is_ip4 && is_ip6));
189
190   th = (tcp_header_t *) (b->data + vnet_buffer (b)->l4_hdr_offset);
191   uh = (udp_header_t *) (b->data + vnet_buffer (b)->l4_hdr_offset);
192
193   if (is_ip4)
194     {
195       ip4_header_t *ip4;
196
197       ip4 = (ip4_header_t *) (b->data + vnet_buffer (b)->l3_hdr_offset);
198       if (b->flags & VNET_BUFFER_F_OFFLOAD_IP_CKSUM)
199         ip4->checksum = ip4_header_checksum (ip4);
200       if (b->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM)
201         {
202           th->checksum = 0;
203           th->checksum = ip4_tcp_udp_compute_checksum (vm, b, ip4);
204         }
205       else if (b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM)
206         uh->checksum = ip4_tcp_udp_compute_checksum (vm, b, ip4);
207     }
208   else if (is_ip6)
209     {
210       int bogus;
211       ip6_header_t *ip6;
212
213       ip6 = (ip6_header_t *) (b->data + vnet_buffer (b)->l3_hdr_offset);
214       if (b->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM)
215         {
216           th->checksum = 0;
217           th->checksum =
218             ip6_tcp_udp_icmp_compute_checksum (vm, b, ip6, &bogus);
219         }
220       else if (b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM)
221         {
222           uh->checksum = 0;
223           uh->checksum =
224             ip6_tcp_udp_icmp_compute_checksum (vm, b, ip6, &bogus);
225         }
226     }
227   b->flags &= ~VNET_BUFFER_F_OFFLOAD_TCP_CKSUM;
228   b->flags &= ~VNET_BUFFER_F_OFFLOAD_UDP_CKSUM;
229   b->flags &= ~VNET_BUFFER_F_OFFLOAD_IP_CKSUM;
230 }
231
232 static_always_inline u16
233 tso_alloc_tx_bufs (vlib_main_t * vm,
234                    vnet_interface_per_thread_data_t * ptd,
235                    vlib_buffer_t * b0, u32 n_bytes_b0, u16 l234_sz,
236                    u16 gso_size)
237 {
238   /* rounded-up division */
239   u16 n_bufs = (n_bytes_b0 - l234_sz + (gso_size - 1)) / gso_size;
240   u16 n_alloc;
241
242   ASSERT (n_bufs > 0);
243   vec_validate (ptd->split_buffers, n_bufs - 1);
244
245   n_alloc = vlib_buffer_alloc (vm, ptd->split_buffers, n_bufs);
246   if (n_alloc < n_bufs)
247     {
248       vlib_buffer_free (vm, ptd->split_buffers, n_alloc);
249       return 0;
250     }
251   return n_alloc;
252 }
253
254 static_always_inline void
255 tso_init_buf_from_template_base (vlib_buffer_t * nb0, vlib_buffer_t * b0,
256                                  u32 flags, u16 length)
257 {
258   nb0->current_data = b0->current_data;
259   nb0->total_length_not_including_first_buffer = 0;
260   nb0->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID | flags;
261   clib_memcpy_fast (&nb0->opaque, &b0->opaque, sizeof (nb0->opaque));
262   clib_memcpy_fast (vlib_buffer_get_current (nb0),
263                     vlib_buffer_get_current (b0), length);
264   nb0->current_length = length;
265 }
266
267 static_always_inline void
268 tso_init_buf_from_template (vlib_main_t * vm, vlib_buffer_t * nb0,
269                             vlib_buffer_t * b0, u16 template_data_sz,
270                             u16 gso_size, u8 ** p_dst_ptr, u16 * p_dst_left,
271                             u32 next_tcp_seq, u32 flags)
272 {
273   tso_init_buf_from_template_base (nb0, b0, flags, template_data_sz);
274
275   *p_dst_left =
276     clib_min (gso_size,
277               vlib_buffer_get_default_data_size (vm) - (template_data_sz +
278                                                         nb0->current_data));
279   *p_dst_ptr = vlib_buffer_get_current (nb0) + template_data_sz;
280
281   tcp_header_t *tcp =
282     (tcp_header_t *) (nb0->data + vnet_buffer (nb0)->l4_hdr_offset);
283   tcp->seq_number = clib_host_to_net_u32 (next_tcp_seq);
284 }
285
286 static_always_inline void
287 tso_fixup_segmented_buf (vlib_buffer_t * b0, u8 tcp_flags, int is_ip6)
288 {
289   u16 l3_hdr_offset = vnet_buffer (b0)->l3_hdr_offset;
290   u16 l4_hdr_offset = vnet_buffer (b0)->l4_hdr_offset;
291   ip4_header_t *ip4 = (ip4_header_t *) (b0->data + l3_hdr_offset);
292   ip6_header_t *ip6 = (ip6_header_t *) (b0->data + l3_hdr_offset);
293   tcp_header_t *tcp = (tcp_header_t *) (b0->data + l4_hdr_offset);
294
295   tcp->flags = tcp_flags;
296
297   if (is_ip6)
298     ip6->payload_length =
299       clib_host_to_net_u16 (b0->current_length -
300                             (l4_hdr_offset - b0->current_data));
301   else
302     ip4->length =
303       clib_host_to_net_u16 (b0->current_length -
304                             (l3_hdr_offset - b0->current_data));
305 }
306
307 /**
308  * Allocate the necessary number of ptd->split_buffers,
309  * and segment the possibly chained buffer(s) from b0 into
310  * there.
311  *
312  * Return the cumulative number of bytes sent or zero
313  * if allocation failed.
314  */
315
316 static_always_inline u32
317 tso_segment_buffer (vlib_main_t * vm, vnet_interface_per_thread_data_t * ptd,
318                     int do_tx_offloads, u32 sbi0, vlib_buffer_t * sb0,
319                     u32 n_bytes_b0)
320 {
321   u32 n_tx_bytes = 0;
322   int is_ip4 = sb0->flags & VNET_BUFFER_F_IS_IP4;
323   int is_ip6 = sb0->flags & VNET_BUFFER_F_IS_IP6;
324   ASSERT (is_ip4 || is_ip6);
325   ASSERT (sb0->flags & VNET_BUFFER_F_L2_HDR_OFFSET_VALID);
326   ASSERT (sb0->flags & VNET_BUFFER_F_L3_HDR_OFFSET_VALID);
327   ASSERT (sb0->flags & VNET_BUFFER_F_L4_HDR_OFFSET_VALID);
328   u16 gso_size = vnet_buffer2 (sb0)->gso_size;
329
330   int l4_hdr_sz = vnet_buffer2 (sb0)->gso_l4_hdr_sz;
331   u8 save_tcp_flags = 0;
332   u8 tcp_flags_no_fin_psh = 0;
333   u32 next_tcp_seq = 0;
334
335   tcp_header_t *tcp =
336     (tcp_header_t *) (sb0->data + vnet_buffer (sb0)->l4_hdr_offset);
337   next_tcp_seq = clib_net_to_host_u32 (tcp->seq_number);
338   /* store original flags for last packet and reset FIN and PSH */
339   save_tcp_flags = tcp->flags;
340   tcp_flags_no_fin_psh = tcp->flags & ~(TCP_FLAG_FIN | TCP_FLAG_PSH);
341   tcp->checksum = 0;
342
343   u32 default_bflags =
344     sb0->flags & ~(VNET_BUFFER_F_GSO | VLIB_BUFFER_NEXT_PRESENT);
345   u16 l234_sz = vnet_buffer (sb0)->l4_hdr_offset + l4_hdr_sz
346     - sb0->current_data;
347   int first_data_size = clib_min (gso_size, sb0->current_length - l234_sz);
348   next_tcp_seq += first_data_size;
349
350   if (PREDICT_FALSE
351       (!tso_alloc_tx_bufs (vm, ptd, sb0, n_bytes_b0, l234_sz, gso_size)))
352     return 0;
353
354   vlib_buffer_t *b0 = vlib_get_buffer (vm, ptd->split_buffers[0]);
355   tso_init_buf_from_template_base (b0, sb0, default_bflags,
356                                    l234_sz + first_data_size);
357
358   u32 total_src_left = n_bytes_b0 - l234_sz - first_data_size;
359   if (total_src_left)
360     {
361       /* Need to copy more segments */
362       u8 *src_ptr, *dst_ptr;
363       u16 src_left, dst_left;
364       /* current source buffer */
365       vlib_buffer_t *csb0 = sb0;
366       u32 csbi0 = sbi0;
367       /* current dest buffer */
368       vlib_buffer_t *cdb0;
369       u16 dbi = 1;              /* the buffer [0] is b0 */
370
371       src_ptr = vlib_buffer_get_current (sb0) + l234_sz + first_data_size;
372       src_left = sb0->current_length - l234_sz - first_data_size;
373
374       tso_fixup_segmented_buf (b0, tcp_flags_no_fin_psh, is_ip6);
375       if (do_tx_offloads)
376         calc_checksums (vm, b0);
377
378       /* grab a second buffer and prepare the loop */
379       ASSERT (dbi < vec_len (ptd->split_buffers));
380       cdb0 = vlib_get_buffer (vm, ptd->split_buffers[dbi++]);
381       tso_init_buf_from_template (vm, cdb0, b0, l234_sz, gso_size, &dst_ptr,
382                                   &dst_left, next_tcp_seq, default_bflags);
383
384       /* an arbitrary large number to catch the runaway loops */
385       int nloops = 2000;
386       while (total_src_left)
387         {
388           if (nloops-- <= 0)
389             clib_panic ("infinite loop detected");
390           u16 bytes_to_copy = clib_min (src_left, dst_left);
391
392           clib_memcpy_fast (dst_ptr, src_ptr, bytes_to_copy);
393
394           src_left -= bytes_to_copy;
395           src_ptr += bytes_to_copy;
396           total_src_left -= bytes_to_copy;
397           dst_left -= bytes_to_copy;
398           dst_ptr += bytes_to_copy;
399           next_tcp_seq += bytes_to_copy;
400           cdb0->current_length += bytes_to_copy;
401
402           if (0 == src_left)
403             {
404               int has_next = (csb0->flags & VLIB_BUFFER_NEXT_PRESENT);
405               u32 next_bi = csb0->next_buffer;
406
407               /* init src to the next buffer in chain */
408               if (has_next)
409                 {
410                   csbi0 = next_bi;
411                   csb0 = vlib_get_buffer (vm, csbi0);
412                   src_left = csb0->current_length;
413                   src_ptr = vlib_buffer_get_current (csb0);
414                 }
415               else
416                 {
417                   ASSERT (total_src_left == 0);
418                   break;
419                 }
420             }
421           if (0 == dst_left && total_src_left)
422             {
423               if (do_tx_offloads)
424                 calc_checksums (vm, cdb0);
425               n_tx_bytes += cdb0->current_length;
426               ASSERT (dbi < vec_len (ptd->split_buffers));
427               cdb0 = vlib_get_buffer (vm, ptd->split_buffers[dbi++]);
428               tso_init_buf_from_template (vm, cdb0, b0, l234_sz,
429                                           gso_size, &dst_ptr, &dst_left,
430                                           next_tcp_seq, default_bflags);
431             }
432         }
433
434       tso_fixup_segmented_buf (cdb0, save_tcp_flags, is_ip6);
435       if (do_tx_offloads)
436         calc_checksums (vm, cdb0);
437
438       n_tx_bytes += cdb0->current_length;
439     }
440   n_tx_bytes += b0->current_length;
441   return n_tx_bytes;
442 }
443
444 static_always_inline void
445 drop_one_buffer_and_count (vlib_main_t * vm, vnet_main_t * vnm,
446                            vlib_node_runtime_t * node, u32 * pbi0,
447                            u32 drop_error_code)
448 {
449   u32 thread_index = vm->thread_index;
450   vnet_interface_output_runtime_t *rt = (void *) node->runtime_data;
451
452   vlib_simple_counter_main_t *cm;
453   cm =
454     vec_elt_at_index (vnm->interface_main.sw_if_counters,
455                       VNET_INTERFACE_COUNTER_TX_ERROR);
456   vlib_increment_simple_counter (cm, thread_index, rt->sw_if_index, 1);
457
458   vlib_error_drop_buffers (vm, node, pbi0,
459                            /* buffer stride */ 1,
460                            /* n_buffers */ 1,
461                            VNET_INTERFACE_OUTPUT_NEXT_DROP,
462                            node->node_index, drop_error_code);
463 }
464
465 static_always_inline uword
466 vnet_interface_output_node_inline_gso (vlib_main_t * vm,
467                                        vlib_node_runtime_t * node,
468                                        vlib_frame_t * frame,
469                                        vnet_main_t * vnm,
470                                        vnet_hw_interface_t * hi,
471                                        int do_tx_offloads,
472                                        int do_segmentation)
473 {
474   vnet_interface_output_runtime_t *rt = (void *) node->runtime_data;
475   vnet_sw_interface_t *si;
476   u32 n_left_to_tx, *from, *from_end, *to_tx;
477   u32 n_bytes, n_buffers, n_packets;
478   u32 n_bytes_b0, n_bytes_b1, n_bytes_b2, n_bytes_b3;
479   u32 thread_index = vm->thread_index;
480   vnet_interface_main_t *im = &vnm->interface_main;
481   u32 next_index = VNET_INTERFACE_OUTPUT_NEXT_TX;
482   u32 current_config_index = ~0;
483   u8 arc = im->output_feature_arc_index;
484   vnet_interface_per_thread_data_t *ptd =
485     vec_elt_at_index (im->per_thread_data, thread_index);
486   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
487
488   n_buffers = frame->n_vectors;
489
490   if (node->flags & VLIB_NODE_FLAG_TRACE)
491     vnet_interface_output_trace (vm, node, frame, n_buffers);
492
493   from = vlib_frame_vector_args (frame);
494   vlib_get_buffers (vm, from, b, n_buffers);
495
496   if (rt->is_deleted)
497     return vlib_error_drop_buffers (vm, node, from,
498                                     /* buffer stride */ 1,
499                                     n_buffers,
500                                     VNET_INTERFACE_OUTPUT_NEXT_DROP,
501                                     node->node_index,
502                                     VNET_INTERFACE_OUTPUT_ERROR_INTERFACE_DELETED);
503
504   si = vnet_get_sw_interface (vnm, rt->sw_if_index);
505   hi = vnet_get_sup_hw_interface (vnm, rt->sw_if_index);
506   if (!(si->flags & (VNET_SW_INTERFACE_FLAG_ADMIN_UP |
507                      VNET_SW_INTERFACE_FLAG_BOND_SLAVE)) ||
508       !(hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
509     {
510       vlib_simple_counter_main_t *cm;
511
512       cm = vec_elt_at_index (vnm->interface_main.sw_if_counters,
513                              VNET_INTERFACE_COUNTER_TX_ERROR);
514       vlib_increment_simple_counter (cm, thread_index,
515                                      rt->sw_if_index, n_buffers);
516
517       return vlib_error_drop_buffers (vm, node, from,
518                                       /* buffer stride */ 1,
519                                       n_buffers,
520                                       VNET_INTERFACE_OUTPUT_NEXT_DROP,
521                                       node->node_index,
522                                       VNET_INTERFACE_OUTPUT_ERROR_INTERFACE_DOWN);
523     }
524
525   from_end = from + n_buffers;
526
527   /* Total byte count of all buffers. */
528   n_bytes = 0;
529   n_packets = 0;
530
531   /* interface-output feature arc handling */
532   if (PREDICT_FALSE (vnet_have_features (arc, rt->sw_if_index)))
533     {
534       vnet_feature_config_main_t *fcm;
535       fcm = vnet_feature_get_config_main (arc);
536       current_config_index = vnet_get_feature_config_index (arc,
537                                                             rt->sw_if_index);
538       vnet_get_config_data (&fcm->config_main, &current_config_index,
539                             &next_index, 0);
540     }
541
542   while (from < from_end)
543     {
544       /* Get new next frame since previous incomplete frame may have less
545          than VNET_FRAME_SIZE vectors in it. */
546       vlib_get_new_next_frame (vm, node, next_index, to_tx, n_left_to_tx);
547
548       while (from + 8 <= from_end && n_left_to_tx >= 4)
549         {
550           u32 bi0, bi1, bi2, bi3;
551           u32 tx_swif0, tx_swif1, tx_swif2, tx_swif3;
552           u32 or_flags;
553
554           /* Prefetch next iteration. */
555           vlib_prefetch_buffer_header (b[4], LOAD);
556           vlib_prefetch_buffer_header (b[5], LOAD);
557           vlib_prefetch_buffer_header (b[6], LOAD);
558           vlib_prefetch_buffer_header (b[7], LOAD);
559
560           bi0 = from[0];
561           bi1 = from[1];
562           bi2 = from[2];
563           bi3 = from[3];
564           to_tx[0] = bi0;
565           to_tx[1] = bi1;
566           to_tx[2] = bi2;
567           to_tx[3] = bi3;
568
569           or_flags = b[0]->flags | b[1]->flags | b[2]->flags | b[3]->flags;
570
571           if (do_segmentation)
572             {
573               /* go to single loop if we need TSO segmentation */
574               if (PREDICT_FALSE (or_flags & VNET_BUFFER_F_GSO))
575                 break;
576             }
577           from += 4;
578           to_tx += 4;
579           n_left_to_tx -= 4;
580
581           /* Be grumpy about zero length buffers for benefit of
582              driver tx function. */
583           ASSERT (b[0]->current_length > 0);
584           ASSERT (b[1]->current_length > 0);
585           ASSERT (b[2]->current_length > 0);
586           ASSERT (b[3]->current_length > 0);
587
588           n_bytes_b0 = vlib_buffer_length_in_chain (vm, b[0]);
589           n_bytes_b1 = vlib_buffer_length_in_chain (vm, b[1]);
590           n_bytes_b2 = vlib_buffer_length_in_chain (vm, b[2]);
591           n_bytes_b3 = vlib_buffer_length_in_chain (vm, b[3]);
592           tx_swif0 = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
593           tx_swif1 = vnet_buffer (b[1])->sw_if_index[VLIB_TX];
594           tx_swif2 = vnet_buffer (b[2])->sw_if_index[VLIB_TX];
595           tx_swif3 = vnet_buffer (b[3])->sw_if_index[VLIB_TX];
596
597           n_bytes += n_bytes_b0 + n_bytes_b1;
598           n_bytes += n_bytes_b2 + n_bytes_b3;
599           n_packets += 4;
600
601           if (PREDICT_FALSE (current_config_index != ~0))
602             {
603               vnet_buffer (b[0])->feature_arc_index = arc;
604               vnet_buffer (b[1])->feature_arc_index = arc;
605               vnet_buffer (b[2])->feature_arc_index = arc;
606               vnet_buffer (b[3])->feature_arc_index = arc;
607               b[0]->current_config_index = current_config_index;
608               b[1]->current_config_index = current_config_index;
609               b[2]->current_config_index = current_config_index;
610               b[3]->current_config_index = current_config_index;
611             }
612
613           /* update vlan subif tx counts, if required */
614           if (PREDICT_FALSE (tx_swif0 != rt->sw_if_index))
615             {
616               vlib_increment_combined_counter (im->combined_sw_if_counters +
617                                                VNET_INTERFACE_COUNTER_TX,
618                                                thread_index, tx_swif0, 1,
619                                                n_bytes_b0);
620             }
621
622           if (PREDICT_FALSE (tx_swif1 != rt->sw_if_index))
623             {
624
625               vlib_increment_combined_counter (im->combined_sw_if_counters +
626                                                VNET_INTERFACE_COUNTER_TX,
627                                                thread_index, tx_swif1, 1,
628                                                n_bytes_b1);
629             }
630
631           if (PREDICT_FALSE (tx_swif2 != rt->sw_if_index))
632             {
633
634               vlib_increment_combined_counter (im->combined_sw_if_counters +
635                                                VNET_INTERFACE_COUNTER_TX,
636                                                thread_index, tx_swif2, 1,
637                                                n_bytes_b2);
638             }
639           if (PREDICT_FALSE (tx_swif3 != rt->sw_if_index))
640             {
641
642               vlib_increment_combined_counter (im->combined_sw_if_counters +
643                                                VNET_INTERFACE_COUNTER_TX,
644                                                thread_index, tx_swif3, 1,
645                                                n_bytes_b3);
646             }
647
648           if (do_tx_offloads)
649             {
650               if (or_flags &
651                   (VNET_BUFFER_F_OFFLOAD_TCP_CKSUM |
652                    VNET_BUFFER_F_OFFLOAD_UDP_CKSUM |
653                    VNET_BUFFER_F_OFFLOAD_IP_CKSUM))
654                 {
655                   calc_checksums (vm, b[0]);
656                   calc_checksums (vm, b[1]);
657                   calc_checksums (vm, b[2]);
658                   calc_checksums (vm, b[3]);
659                 }
660             }
661           b += 4;
662
663         }
664
665       while (from + 1 <= from_end && n_left_to_tx >= 1)
666         {
667           u32 bi0;
668           u32 tx_swif0;
669
670           bi0 = from[0];
671           to_tx[0] = bi0;
672           from += 1;
673           to_tx += 1;
674           n_left_to_tx -= 1;
675
676           /* Be grumpy about zero length buffers for benefit of
677              driver tx function. */
678           ASSERT (b[0]->current_length > 0);
679
680           n_bytes_b0 = vlib_buffer_length_in_chain (vm, b[0]);
681           tx_swif0 = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
682           n_bytes += n_bytes_b0;
683           n_packets += 1;
684
685           if (PREDICT_FALSE (current_config_index != ~0))
686             {
687               vnet_buffer (b[0])->feature_arc_index = arc;
688               b[0]->current_config_index = current_config_index;
689             }
690
691           if (do_segmentation)
692             {
693               if (PREDICT_FALSE (b[0]->flags & VNET_BUFFER_F_GSO))
694                 {
695                   /*
696                    * Undo the enqueue of the b0 - it is not going anywhere,
697                    * and will be freed either after it's segmented or
698                    * when dropped, if there is no buffers to segment into.
699                    */
700                   to_tx -= 1;
701                   n_left_to_tx += 1;
702                   /* undo the counting. */
703                   n_bytes -= n_bytes_b0;
704                   n_packets -= 1;
705
706                   u32 n_tx_bytes = 0;
707
708                   n_tx_bytes =
709                     tso_segment_buffer (vm, ptd, do_tx_offloads, bi0, b[0],
710                                         n_bytes_b0);
711
712                   if (PREDICT_FALSE (n_tx_bytes == 0))
713                     {
714                       drop_one_buffer_and_count (vm, vnm, node, from - 1,
715                                                  VNET_INTERFACE_OUTPUT_ERROR_NO_BUFFERS_FOR_GSO);
716                       b += 1;
717                       continue;
718                     }
719
720                   u16 n_tx_bufs = vec_len (ptd->split_buffers);
721                   u32 *from_tx_seg = ptd->split_buffers;
722
723                   while (n_tx_bufs > 0)
724                     {
725                       if (n_tx_bufs >= n_left_to_tx)
726                         {
727                           while (n_left_to_tx > 0)
728                             {
729                               to_tx[0] = from_tx_seg[0];
730                               to_tx += 1;
731                               from_tx_seg += 1;
732                               n_left_to_tx -= 1;
733                               n_tx_bufs -= 1;
734                               n_packets += 1;
735                             }
736                           vlib_put_next_frame (vm, node, next_index,
737                                                n_left_to_tx);
738                           vlib_get_new_next_frame (vm, node, next_index,
739                                                    to_tx, n_left_to_tx);
740                         }
741                       while (n_tx_bufs > 0)
742                         {
743                           to_tx[0] = from_tx_seg[0];
744                           to_tx += 1;
745                           from_tx_seg += 1;
746                           n_left_to_tx -= 1;
747                           n_tx_bufs -= 1;
748                           n_packets += 1;
749                         }
750                     }
751                   n_bytes += n_tx_bytes;
752                   if (PREDICT_FALSE (tx_swif0 != rt->sw_if_index))
753                     {
754
755                       vlib_increment_combined_counter
756                         (im->combined_sw_if_counters +
757                          VNET_INTERFACE_COUNTER_TX, thread_index, tx_swif0,
758                          _vec_len (ptd->split_buffers), n_tx_bytes);
759                     }
760                   /* The buffers were enqueued. Reset the length */
761                   _vec_len (ptd->split_buffers) = 0;
762                   /* Free the now segmented buffer */
763                   vlib_buffer_free_one (vm, bi0);
764                   b += 1;
765                   continue;
766                 }
767             }
768
769           if (PREDICT_FALSE (tx_swif0 != rt->sw_if_index))
770             {
771
772               vlib_increment_combined_counter (im->combined_sw_if_counters +
773                                                VNET_INTERFACE_COUNTER_TX,
774                                                thread_index, tx_swif0, 1,
775                                                n_bytes_b0);
776             }
777
778           if (do_tx_offloads)
779             calc_checksums (vm, b[0]);
780
781           b += 1;
782         }
783
784       vlib_put_next_frame (vm, node, next_index, n_left_to_tx);
785     }
786
787   /* Update main interface stats. */
788   vlib_increment_combined_counter (im->combined_sw_if_counters
789                                    + VNET_INTERFACE_COUNTER_TX,
790                                    thread_index,
791                                    rt->sw_if_index, n_packets, n_bytes);
792   return n_buffers;
793 }
794 #endif /* CLIB_MARCH_VARIANT */
795
796 static_always_inline void vnet_interface_pcap_tx_trace
797   (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame,
798    int sw_if_index_from_buffer)
799 {
800   u32 n_left_from, *from;
801   u32 sw_if_index;
802
803   if (PREDICT_TRUE (vlib_global_main.pcap[VLIB_TX].pcap_enable == 0))
804     return;
805
806   if (sw_if_index_from_buffer == 0)
807     {
808       vnet_interface_output_runtime_t *rt = (void *) node->runtime_data;
809       sw_if_index = rt->sw_if_index;
810     }
811   else
812     sw_if_index = ~0;
813
814   n_left_from = frame->n_vectors;
815   from = vlib_frame_vector_args (frame);
816
817   while (n_left_from > 0)
818     {
819       u32 bi0 = from[0];
820       vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
821
822       if (sw_if_index_from_buffer)
823         sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX];
824
825       if (vlib_global_main.pcap[VLIB_TX].pcap_sw_if_index == 0 ||
826           vlib_global_main.pcap[VLIB_TX].pcap_sw_if_index == sw_if_index)
827         pcap_add_buffer (&vlib_global_main.pcap[VLIB_TX].pcap_main, vm, bi0,
828                          512);
829       from++;
830       n_left_from--;
831     }
832 }
833
834 #ifndef CLIB_MARCH_VARIANT
835 static_always_inline uword
836 vnet_interface_output_node_inline (vlib_main_t * vm,
837                                    vlib_node_runtime_t * node,
838                                    vlib_frame_t * frame, vnet_main_t * vnm,
839                                    vnet_hw_interface_t * hi,
840                                    int do_tx_offloads)
841 {
842   /*
843    * The 3-headed "if" is here because we want to err on the side
844    * of not impacting the non-GSO performance - so for the more
845    * common case of no GSO interfaces we want to prevent the
846    * segmentation codepath from being there altogether.
847    */
848   if (PREDICT_TRUE (vnm->interface_main.gso_interface_count == 0))
849     return vnet_interface_output_node_inline_gso (vm, node, frame, vnm, hi,
850                                                   do_tx_offloads,
851                                                   /* do_segmentation */ 0);
852   else if (hi->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO)
853     return vnet_interface_output_node_inline_gso (vm, node, frame, vnm, hi,
854                                                   do_tx_offloads,
855                                                   /* do_segmentation */ 0);
856   else
857     return vnet_interface_output_node_inline_gso (vm, node, frame, vnm, hi,
858                                                   do_tx_offloads,
859                                                   /* do_segmentation */ 1);
860 }
861
862 uword
863 vnet_interface_output_node (vlib_main_t * vm, vlib_node_runtime_t * node,
864                             vlib_frame_t * frame)
865 {
866   vnet_main_t *vnm = vnet_get_main ();
867   vnet_hw_interface_t *hi;
868   vnet_interface_output_runtime_t *rt = (void *) node->runtime_data;
869   hi = vnet_get_sup_hw_interface (vnm, rt->sw_if_index);
870
871   vnet_interface_pcap_tx_trace (vm, node, frame,
872                                 0 /* sw_if_index_from_buffer */ );
873
874   if (hi->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD)
875     return vnet_interface_output_node_inline (vm, node, frame, vnm, hi,
876                                               /* do_tx_offloads */ 0);
877   else
878     return vnet_interface_output_node_inline (vm, node, frame, vnm, hi,
879                                               /* do_tx_offloads */ 1);
880 }
881 #endif /* CLIB_MARCH_VARIANT */
882
883 /* Use buffer's sw_if_index[VNET_TX] to choose output interface. */
884 VLIB_NODE_FN (vnet_per_buffer_interface_output_node) (vlib_main_t * vm,
885                                                       vlib_node_runtime_t *
886                                                       node,
887                                                       vlib_frame_t * frame)
888 {
889   vnet_main_t *vnm = vnet_get_main ();
890   u32 n_left_to_next, *from, *to_next;
891   u32 n_left_from, next_index;
892
893   vnet_interface_pcap_tx_trace (vm, node, frame,
894                                 1 /* sw_if_index_from_buffer */ );
895
896   n_left_from = frame->n_vectors;
897
898   from = vlib_frame_vector_args (frame);
899   next_index = node->cached_next_index;
900
901   while (n_left_from > 0)
902     {
903       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
904
905       while (n_left_from >= 4 && n_left_to_next >= 2)
906         {
907           u32 bi0, bi1, next0, next1;
908           vlib_buffer_t *b0, *b1;
909           vnet_hw_interface_t *hi0, *hi1;
910
911           /* Prefetch next iteration. */
912           vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
913           vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
914
915           bi0 = from[0];
916           bi1 = from[1];
917           to_next[0] = bi0;
918           to_next[1] = bi1;
919           from += 2;
920           to_next += 2;
921           n_left_to_next -= 2;
922           n_left_from -= 2;
923
924           b0 = vlib_get_buffer (vm, bi0);
925           b1 = vlib_get_buffer (vm, bi1);
926
927           hi0 =
928             vnet_get_sup_hw_interface (vnm,
929                                        vnet_buffer (b0)->sw_if_index
930                                        [VLIB_TX]);
931           hi1 =
932             vnet_get_sup_hw_interface (vnm,
933                                        vnet_buffer (b1)->sw_if_index
934                                        [VLIB_TX]);
935
936           next0 = hi0->output_node_next_index;
937           next1 = hi1->output_node_next_index;
938
939           vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
940                                            n_left_to_next, bi0, bi1, next0,
941                                            next1);
942         }
943
944       while (n_left_from > 0 && n_left_to_next > 0)
945         {
946           u32 bi0, next0;
947           vlib_buffer_t *b0;
948           vnet_hw_interface_t *hi0;
949
950           bi0 = from[0];
951           to_next[0] = bi0;
952           from += 1;
953           to_next += 1;
954           n_left_to_next -= 1;
955           n_left_from -= 1;
956
957           b0 = vlib_get_buffer (vm, bi0);
958
959           hi0 =
960             vnet_get_sup_hw_interface (vnm,
961                                        vnet_buffer (b0)->sw_if_index
962                                        [VLIB_TX]);
963
964           next0 = hi0->output_node_next_index;
965
966           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
967                                            n_left_to_next, bi0, next0);
968         }
969
970       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
971     }
972
973   return frame->n_vectors;
974 }
975
976 typedef struct vnet_error_trace_t_
977 {
978   u32 sw_if_index;
979 } vnet_error_trace_t;
980
981
982 static u8 *
983 format_vnet_error_trace (u8 * s, va_list * va)
984 {
985   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
986   CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
987   vnet_error_trace_t *t = va_arg (*va, vnet_error_trace_t *);
988
989   s = format (s, "rx:%U", format_vnet_sw_if_index_name,
990               vnet_get_main (), t->sw_if_index);
991
992   return s;
993 }
994
995 static void
996 interface_trace_buffers (vlib_main_t * vm,
997                          vlib_node_runtime_t * node, vlib_frame_t * frame)
998 {
999   u32 n_left, *buffers;
1000
1001   buffers = vlib_frame_vector_args (frame);
1002   n_left = frame->n_vectors;
1003
1004   while (n_left >= 4)
1005     {
1006       u32 bi0, bi1;
1007       vlib_buffer_t *b0, *b1;
1008       vnet_error_trace_t *t0, *t1;
1009
1010       /* Prefetch next iteration. */
1011       vlib_prefetch_buffer_with_index (vm, buffers[2], LOAD);
1012       vlib_prefetch_buffer_with_index (vm, buffers[3], LOAD);
1013
1014       bi0 = buffers[0];
1015       bi1 = buffers[1];
1016
1017       b0 = vlib_get_buffer (vm, bi0);
1018       b1 = vlib_get_buffer (vm, bi1);
1019
1020       if (b0->flags & VLIB_BUFFER_IS_TRACED)
1021         {
1022           t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
1023           t0->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1024         }
1025       if (b1->flags & VLIB_BUFFER_IS_TRACED)
1026         {
1027           t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0]));
1028           t1->sw_if_index = vnet_buffer (b1)->sw_if_index[VLIB_RX];
1029         }
1030       buffers += 2;
1031       n_left -= 2;
1032     }
1033
1034   while (n_left >= 1)
1035     {
1036       u32 bi0;
1037       vlib_buffer_t *b0;
1038       vnet_error_trace_t *t0;
1039
1040       bi0 = buffers[0];
1041
1042       b0 = vlib_get_buffer (vm, bi0);
1043
1044       if (b0->flags & VLIB_BUFFER_IS_TRACED)
1045         {
1046           t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
1047           t0->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1048         }
1049       buffers += 1;
1050       n_left -= 1;
1051     }
1052 }
1053
1054 typedef enum
1055 {
1056   VNET_ERROR_DISPOSITION_DROP,
1057   VNET_ERROR_DISPOSITION_PUNT,
1058   VNET_ERROR_N_DISPOSITION,
1059 } vnet_error_disposition_t;
1060
1061 static_always_inline uword
1062 interface_drop_punt (vlib_main_t * vm,
1063                      vlib_node_runtime_t * node,
1064                      vlib_frame_t * frame,
1065                      vnet_error_disposition_t disposition)
1066 {
1067   u32 *from, n_left, thread_index, *sw_if_index;
1068   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
1069   u32 sw_if_indices[VLIB_FRAME_SIZE];
1070   vlib_simple_counter_main_t *cm;
1071   u16 nexts[VLIB_FRAME_SIZE];
1072   vnet_main_t *vnm;
1073
1074   vnm = vnet_get_main ();
1075   thread_index = vm->thread_index;
1076   from = vlib_frame_vector_args (frame);
1077   n_left = frame->n_vectors;
1078   b = bufs;
1079   sw_if_index = sw_if_indices;
1080
1081   vlib_get_buffers (vm, from, bufs, n_left);
1082
1083   if (node->flags & VLIB_NODE_FLAG_TRACE)
1084     interface_trace_buffers (vm, node, frame);
1085
1086   /* All going to drop regardless, this is just a counting exercise */
1087   clib_memset (nexts, 0, sizeof (nexts));
1088
1089   cm = vec_elt_at_index (vnm->interface_main.sw_if_counters,
1090                          (disposition == VNET_ERROR_DISPOSITION_PUNT
1091                           ? VNET_INTERFACE_COUNTER_PUNT
1092                           : VNET_INTERFACE_COUNTER_DROP));
1093
1094   /* collect the array of interfaces first ... */
1095   while (n_left >= 4)
1096     {
1097       if (n_left >= 12)
1098         {
1099           /* Prefetch 8 ahead - there's not much going on in each iteration */
1100           vlib_prefetch_buffer_header (b[4], LOAD);
1101           vlib_prefetch_buffer_header (b[5], LOAD);
1102           vlib_prefetch_buffer_header (b[6], LOAD);
1103           vlib_prefetch_buffer_header (b[7], LOAD);
1104         }
1105       sw_if_index[0] = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
1106       sw_if_index[1] = vnet_buffer (b[1])->sw_if_index[VLIB_RX];
1107       sw_if_index[2] = vnet_buffer (b[2])->sw_if_index[VLIB_RX];
1108       sw_if_index[3] = vnet_buffer (b[3])->sw_if_index[VLIB_RX];
1109
1110       sw_if_index += 4;
1111       n_left -= 4;
1112       b += 4;
1113     }
1114   while (n_left)
1115     {
1116       sw_if_index[0] = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
1117
1118       sw_if_index += 1;
1119       n_left -= 1;
1120       b += 1;
1121     }
1122
1123   /* ... then count against them in blocks */
1124   n_left = frame->n_vectors;
1125
1126   while (n_left)
1127     {
1128       vnet_sw_interface_t *sw_if0;
1129       u16 off, count;
1130
1131       off = frame->n_vectors - n_left;
1132
1133       sw_if_index = sw_if_indices + off;
1134
1135       count = clib_count_equal_u32 (sw_if_index, n_left);
1136       n_left -= count;
1137
1138       vlib_increment_simple_counter (cm, thread_index, sw_if_index[0], count);
1139
1140       /* Increment super-interface drop/punt counters for
1141          sub-interfaces. */
1142       sw_if0 = vnet_get_sw_interface (vnm, sw_if_index[0]);
1143       if (sw_if0->sup_sw_if_index != sw_if_index[0])
1144         vlib_increment_simple_counter
1145           (cm, thread_index, sw_if0->sup_sw_if_index, count);
1146     }
1147
1148   vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
1149
1150   return frame->n_vectors;
1151 }
1152
1153 static inline void
1154 pcap_drop_trace (vlib_main_t * vm,
1155                  vnet_interface_main_t * im, vlib_frame_t * f)
1156 {
1157   u32 *from;
1158   u32 n_left = f->n_vectors;
1159   vlib_buffer_t *b0, *p1;
1160   u32 bi0;
1161   i16 save_current_data;
1162   u16 save_current_length;
1163
1164   from = vlib_frame_vector_args (f);
1165
1166   while (n_left > 0)
1167     {
1168       if (PREDICT_TRUE (n_left > 1))
1169         {
1170           p1 = vlib_get_buffer (vm, from[1]);
1171           vlib_prefetch_buffer_header (p1, LOAD);
1172         }
1173
1174       bi0 = from[0];
1175       b0 = vlib_get_buffer (vm, bi0);
1176       from++;
1177       n_left--;
1178
1179       /* See if we're pointedly ignoring this specific error */
1180       if (im->pcap_drop_filter_hash
1181           && hash_get (im->pcap_drop_filter_hash, b0->error))
1182         continue;
1183
1184       /* Trace all drops, or drops received on a specific interface */
1185       if (im->pcap_sw_if_index == 0 ||
1186           im->pcap_sw_if_index == vnet_buffer (b0)->sw_if_index[VLIB_RX])
1187         {
1188           save_current_data = b0->current_data;
1189           save_current_length = b0->current_length;
1190
1191           /*
1192            * Typically, we'll need to rewind the buffer
1193            */
1194           if (b0->current_data > 0)
1195             vlib_buffer_advance (b0, (word) - b0->current_data);
1196
1197           pcap_add_buffer (&im->pcap_main, vm, bi0, 512);
1198
1199           b0->current_data = save_current_data;
1200           b0->current_length = save_current_length;
1201         }
1202     }
1203 }
1204
1205 #ifndef CLIB_MARCH_VARIANT
1206 void
1207 vnet_pcap_drop_trace_filter_add_del (u32 error_index, int is_add)
1208 {
1209   vnet_interface_main_t *im = &vnet_get_main ()->interface_main;
1210
1211   if (im->pcap_drop_filter_hash == 0)
1212     im->pcap_drop_filter_hash = hash_create (0, sizeof (uword));
1213
1214   if (is_add)
1215     hash_set (im->pcap_drop_filter_hash, error_index, 1);
1216   else
1217     hash_unset (im->pcap_drop_filter_hash, error_index);
1218 }
1219 #endif /* CLIB_MARCH_VARIANT */
1220
1221 VLIB_NODE_FN (interface_drop) (vlib_main_t * vm,
1222                                vlib_node_runtime_t * node,
1223                                vlib_frame_t * frame)
1224 {
1225   vnet_interface_main_t *im = &vnet_get_main ()->interface_main;
1226
1227   if (PREDICT_FALSE (im->drop_pcap_enable))
1228     pcap_drop_trace (vm, im, frame);
1229
1230   return interface_drop_punt (vm, node, frame, VNET_ERROR_DISPOSITION_DROP);
1231 }
1232
1233 VLIB_NODE_FN (interface_punt) (vlib_main_t * vm,
1234                                vlib_node_runtime_t * node,
1235                                vlib_frame_t * frame)
1236 {
1237   return interface_drop_punt (vm, node, frame, VNET_ERROR_DISPOSITION_PUNT);
1238 }
1239
1240 /* *INDENT-OFF* */
1241 VLIB_REGISTER_NODE (interface_drop) = {
1242   .name = "error-drop",
1243   .vector_size = sizeof (u32),
1244   .format_trace = format_vnet_error_trace,
1245   .n_next_nodes = 1,
1246   .next_nodes = {
1247     [0] = "drop",
1248   },
1249 };
1250 /* *INDENT-ON* */
1251
1252 /* *INDENT-OFF* */
1253 VLIB_REGISTER_NODE (interface_punt) = {
1254   .name = "error-punt",
1255   .vector_size = sizeof (u32),
1256   .format_trace = format_vnet_error_trace,
1257   .n_next_nodes = 1,
1258   .next_nodes = {
1259     [0] = "punt",
1260   },
1261 };
1262 /* *INDENT-ON* */
1263
1264 /* *INDENT-OFF* */
1265 VLIB_REGISTER_NODE (vnet_per_buffer_interface_output_node) = {
1266   .name = "interface-output",
1267   .vector_size = sizeof (u32),
1268 };
1269 /* *INDENT-ON* */
1270
1271 static uword
1272 interface_tx_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
1273                       vlib_frame_t * from_frame)
1274 {
1275   vnet_main_t *vnm = vnet_get_main ();
1276   u32 last_sw_if_index = ~0;
1277   vlib_frame_t *to_frame = 0;
1278   vnet_hw_interface_t *hw = 0;
1279   u32 *from, *to_next = 0;
1280   u32 n_left_from;
1281
1282   from = vlib_frame_vector_args (from_frame);
1283   n_left_from = from_frame->n_vectors;
1284   while (n_left_from > 0)
1285     {
1286       u32 bi0;
1287       vlib_buffer_t *b0;
1288       u32 sw_if_index0;
1289
1290       bi0 = from[0];
1291       from++;
1292       n_left_from--;
1293       b0 = vlib_get_buffer (vm, bi0);
1294       sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
1295
1296       if (PREDICT_FALSE ((last_sw_if_index != sw_if_index0) || to_frame == 0))
1297         {
1298           if (to_frame)
1299             {
1300               hw = vnet_get_sup_hw_interface (vnm, last_sw_if_index);
1301               vlib_put_frame_to_node (vm, hw->tx_node_index, to_frame);
1302             }
1303           last_sw_if_index = sw_if_index0;
1304           hw = vnet_get_sup_hw_interface (vnm, sw_if_index0);
1305           to_frame = vlib_get_frame_to_node (vm, hw->tx_node_index);
1306           to_next = vlib_frame_vector_args (to_frame);
1307         }
1308
1309       to_next[0] = bi0;
1310       to_next++;
1311       to_frame->n_vectors++;
1312     }
1313   vlib_put_frame_to_node (vm, hw->tx_node_index, to_frame);
1314   return from_frame->n_vectors;
1315 }
1316
1317 /* *INDENT-OFF* */
1318 VLIB_REGISTER_NODE (interface_tx, static) = {
1319   .function = interface_tx_node_fn,
1320   .name = "interface-tx",
1321   .vector_size = sizeof (u32),
1322   .n_next_nodes = 1,
1323   .next_nodes = {
1324     [0] = "error-drop",
1325   },
1326 };
1327
1328 VNET_FEATURE_ARC_INIT (interface_output, static) =
1329 {
1330   .arc_name  = "interface-output",
1331   .start_nodes = VNET_FEATURES (0),
1332   .last_in_arc = "interface-tx",
1333   .arc_index_ptr = &vnet_main.interface_main.output_feature_arc_index,
1334 };
1335
1336 VNET_FEATURE_INIT (span_tx, static) = {
1337   .arc_name = "interface-output",
1338   .node_name = "span-output",
1339   .runs_before = VNET_FEATURES ("interface-tx"),
1340 };
1341
1342 VNET_FEATURE_INIT (ipsec_if_tx, static) = {
1343   .arc_name = "interface-output",
1344   .node_name = "ipsec-if-output",
1345   .runs_before = VNET_FEATURES ("interface-tx"),
1346 };
1347
1348 VNET_FEATURE_INIT (interface_tx, static) = {
1349   .arc_name = "interface-output",
1350   .node_name = "interface-tx",
1351   .runs_before = 0,
1352 };
1353 /* *INDENT-ON* */
1354
1355 #ifndef CLIB_MARCH_VARIANT
1356 clib_error_t *
1357 vnet_per_buffer_interface_output_hw_interface_add_del (vnet_main_t * vnm,
1358                                                        u32 hw_if_index,
1359                                                        u32 is_create)
1360 {
1361   vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
1362   u32 next_index;
1363
1364   if (hi->output_node_index == 0)
1365     return 0;
1366
1367   next_index = vlib_node_add_next
1368     (vnm->vlib_main, vnet_per_buffer_interface_output_node.index,
1369      hi->output_node_index);
1370   hi->output_node_next_index = next_index;
1371
1372   return 0;
1373 }
1374
1375 VNET_HW_INTERFACE_ADD_DEL_FUNCTION
1376   (vnet_per_buffer_interface_output_hw_interface_add_del);
1377
1378 void
1379 vnet_set_interface_output_node (vnet_main_t * vnm,
1380                                 u32 hw_if_index, u32 node_index)
1381 {
1382   ASSERT (node_index);
1383   vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
1384   u32 next_index = vlib_node_add_next
1385     (vnm->vlib_main, vnet_per_buffer_interface_output_node.index, node_index);
1386   hi->output_node_next_index = next_index;
1387   hi->output_node_index = node_index;
1388 }
1389 #endif /* CLIB_MARCH_VARIANT */
1390
1391 static clib_error_t *
1392 pcap_drop_trace_command_fn (vlib_main_t * vm,
1393                             unformat_input_t * input,
1394                             vlib_cli_command_t * cmd)
1395 {
1396   vnet_main_t *vnm = vnet_get_main ();
1397   vnet_interface_main_t *im = &vnm->interface_main;
1398   u8 *filename;
1399   u32 max;
1400   int matched = 0;
1401   clib_error_t *error = 0;
1402
1403   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1404     {
1405       if (unformat (input, "on"))
1406         {
1407           if (im->drop_pcap_enable == 0)
1408             {
1409               if (im->pcap_filename == 0)
1410                 im->pcap_filename = format (0, "/tmp/drop.pcap%c", 0);
1411
1412               clib_memset (&im->pcap_main, 0, sizeof (im->pcap_main));
1413               im->pcap_main.file_name = (char *) im->pcap_filename;
1414               im->pcap_main.n_packets_to_capture = PCAP_DEF_PKT_TO_CAPTURE;
1415               if (im->pcap_pkts_to_capture)
1416                 im->pcap_main.n_packets_to_capture = im->pcap_pkts_to_capture;
1417
1418               im->pcap_main.packet_type = PCAP_PACKET_TYPE_ethernet;
1419               im->drop_pcap_enable = 1;
1420               matched = 1;
1421               vlib_cli_output (vm, "pcap drop capture on...");
1422             }
1423           else
1424             {
1425               vlib_cli_output (vm, "pcap drop capture already on...");
1426             }
1427           matched = 1;
1428         }
1429       else if (unformat (input, "off"))
1430         {
1431           matched = 1;
1432
1433           if (im->drop_pcap_enable)
1434             {
1435               vlib_cli_output (vm, "captured %d pkts...",
1436                                im->pcap_main.n_packets_captured);
1437               if (im->pcap_main.n_packets_captured)
1438                 {
1439                   im->pcap_main.n_packets_to_capture =
1440                     im->pcap_main.n_packets_captured;
1441                   error = pcap_write (&im->pcap_main);
1442                   if (error)
1443                     clib_error_report (error);
1444                   else
1445                     vlib_cli_output (vm, "saved to %s...", im->pcap_filename);
1446                 }
1447             }
1448           else
1449             {
1450               vlib_cli_output (vm, "pcap drop capture already off...");
1451             }
1452
1453           im->drop_pcap_enable = 0;
1454         }
1455       else if (unformat (input, "max %d", &max))
1456         {
1457           im->pcap_pkts_to_capture = max;
1458           matched = 1;
1459         }
1460
1461       else if (unformat (input, "intfc %U",
1462                          unformat_vnet_sw_interface, vnm,
1463                          &im->pcap_sw_if_index))
1464         matched = 1;
1465       else if (unformat (input, "intfc any"))
1466         {
1467           im->pcap_sw_if_index = 0;
1468           matched = 1;
1469         }
1470       else if (unformat (input, "file %s", &filename))
1471         {
1472           u8 *chroot_filename;
1473           /* Brain-police user path input */
1474           if (strstr ((char *) filename, "..")
1475               || index ((char *) filename, '/'))
1476             {
1477               vlib_cli_output (vm, "illegal characters in filename '%s'",
1478                                filename);
1479               continue;
1480             }
1481
1482           chroot_filename = format (0, "/tmp/%s%c", filename, 0);
1483           vec_free (filename);
1484
1485           if (im->pcap_filename)
1486             vec_free (im->pcap_filename);
1487           im->pcap_filename = chroot_filename;
1488           im->pcap_main.file_name = (char *) im->pcap_filename;
1489           matched = 1;
1490         }
1491       else if (unformat (input, "status"))
1492         {
1493           if (im->drop_pcap_enable == 0)
1494             {
1495               vlib_cli_output (vm, "pcap drop capture is off...");
1496               continue;
1497             }
1498
1499           vlib_cli_output (vm, "pcap drop capture: %d of %d pkts...",
1500                            im->pcap_main.n_packets_captured,
1501                            im->pcap_main.n_packets_to_capture);
1502           matched = 1;
1503         }
1504
1505       else
1506         break;
1507     }
1508
1509   if (matched == 0)
1510     return clib_error_return (0, "unknown input `%U'",
1511                               format_unformat_error, input);
1512
1513   return 0;
1514 }
1515
1516 /* *INDENT-OFF* */
1517 VLIB_CLI_COMMAND (pcap_trace_command, static) = {
1518   .path = "pcap drop trace",
1519   .short_help =
1520   "pcap drop trace on off max <nn> intfc <intfc> file <name> status",
1521   .function = pcap_drop_trace_command_fn,
1522 };
1523 /* *INDENT-ON* */
1524
1525 /*
1526  * fd.io coding-style-patch-verification: ON
1527  *
1528  * Local Variables:
1529  * eval: (c-set-style "gnu")
1530  * End:
1531  */