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