blob: 28f44c3d42a8688a203057db4478bf3fc01cefbb [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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>
Dave Barach2c0a4f42017-06-29 09:30:15 -040041#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>
Damjan Marion152e21d2016-11-29 14:55:43 +010045#include <vnet/feature/feature.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070046
Dave Barachba868bb2016-08-08 09:51:21 -040047typedef struct
48{
Ed Warnickecb9cada2015-12-08 15:45:58 -070049 u32 sw_if_index;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +020050 u32 flags;
51 u16 gso_size;
52 u8 gso_l4_hdr_sz;
53 u8 data[128 - 3 * sizeof (u32)];
Dave Barachba868bb2016-08-08 09:51:21 -040054}
55interface_output_trace_t;
Ed Warnickecb9cada2015-12-08 15:45:58 -070056
Filip Tehlar62668772019-03-04 03:33:32 -080057#ifndef CLIB_MARCH_VARIANT
Dave Barachba868bb2016-08-08 09:51:21 -040058u8 *
59format_vnet_interface_output_trace (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -070060{
61 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
Dave Barachba868bb2016-08-08 09:51:21 -040062 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;
Christophe Fontained3c008d2017-10-02 18:10:54 +020066 u32 indent;
Ed Warnickecb9cada2015-12-08 15:45:58 -070067
Dave Barachba868bb2016-08-08 09:51:21 -040068 if (t->sw_if_index != (u32) ~ 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -070069 {
Ed Warnickecb9cada2015-12-08 15:45:58 -070070 indent = format_get_indent (s);
Dave Barachba868bb2016-08-08 09:51:21 -040071
Neale Ranns177bbdc2016-11-15 09:46:51 +000072 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 */
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +020076 s = format (s, "sw_if_index: %d ", t->sw_if_index);
Neale Ranns177bbdc2016-11-15 09:46:51 +000077 }
78 else
79 {
80 si = vnet_get_sw_interface (vnm, t->sw_if_index);
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +020081 s =
82 format (s, "%U ", format_vnet_sw_interface_name, vnm, si,
83 t->flags);
Neale Ranns177bbdc2016-11-15 09:46:51 +000084 }
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +020085#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));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700100 }
101 return s;
102}
103
104static void
105vnet_interface_output_trace (vlib_main_t * vm,
106 vlib_node_runtime_t * node,
Dave Barachba868bb2016-08-08 09:51:21 -0400107 vlib_frame_t * frame, uword n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700108{
Dave Barachba868bb2016-08-08 09:51:21 -0400109 u32 n_left, *from;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700110
111 n_left = n_buffers;
Damjan Mariona3d59862018-11-10 10:23:00 +0100112 from = vlib_frame_vector_args (frame);
Dave Barachba868bb2016-08-08 09:51:21 -0400113
Ed Warnickecb9cada2015-12-08 15:45:58 -0700114 while (n_left >= 4)
115 {
116 u32 bi0, bi1;
Dave Barachba868bb2016-08-08 09:51:21 -0400117 vlib_buffer_t *b0, *b1;
118 interface_output_trace_t *t0, *t1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700119
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];
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200134 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;
Dave Barach178cf492018-11-13 16:34:13 -0500137 clib_memcpy_fast (t0->data, vlib_buffer_get_current (b0),
138 sizeof (t0->data));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700139 }
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];
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200144 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;
Dave Barach178cf492018-11-13 16:34:13 -0500147 clib_memcpy_fast (t1->data, vlib_buffer_get_current (b1),
148 sizeof (t1->data));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149 }
150 from += 2;
151 n_left -= 2;
152 }
153
154 while (n_left >= 1)
155 {
156 u32 bi0;
Dave Barachba868bb2016-08-08 09:51:21 -0400157 vlib_buffer_t *b0;
158 interface_output_trace_t *t0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700159
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];
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200168 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;
Dave Barach178cf492018-11-13 16:34:13 -0500171 clib_memcpy_fast (t0->data, vlib_buffer_get_current (b0),
172 sizeof (t0->data));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700173 }
174 from += 1;
175 n_left -= 1;
176 }
177}
178
Dave Barach2c0a4f42017-06-29 09:30:15 -0400179static_always_inline void
180calc_checksums (vlib_main_t * vm, vlib_buffer_t * b)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700181{
Dave Barach2c0a4f42017-06-29 09:30:15 -0400182 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
Dave Barach2c0a4f42017-06-29 09:30:15 -0400190 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 {
Zhiyong Yang24c50122019-04-19 05:22:31 -0400195 ip4_header_t *ip4;
196
Dave Barach2c0a4f42017-06-29 09:30:15 -0400197 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)
Andrej Kozemcak9d7570c2019-01-07 08:39:22 +0100201 {
202 th->checksum = 0;
203 th->checksum = ip4_tcp_udp_compute_checksum (vm, b, ip4);
204 }
Zhiyong Yang24c50122019-04-19 05:22:31 -0400205 else if (b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM)
Dave Barach2c0a4f42017-06-29 09:30:15 -0400206 uh->checksum = ip4_tcp_udp_compute_checksum (vm, b, ip4);
207 }
Zhiyong Yang24c50122019-04-19 05:22:31 -0400208 else if (is_ip6)
Dave Barach2c0a4f42017-06-29 09:30:15 -0400209 {
210 int bogus;
Zhiyong Yang24c50122019-04-19 05:22:31 -0400211 ip6_header_t *ip6;
212
213 ip6 = (ip6_header_t *) (b->data + vnet_buffer (b)->l3_hdr_offset);
Dave Barach2c0a4f42017-06-29 09:30:15 -0400214 if (b->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM)
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200215 {
216 th->checksum = 0;
217 th->checksum =
218 ip6_tcp_udp_icmp_compute_checksum (vm, b, ip6, &bogus);
219 }
Zhiyong Yang24c50122019-04-19 05:22:31 -0400220 else if (b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM)
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200221 {
222 uh->checksum = 0;
223 uh->checksum =
224 ip6_tcp_udp_icmp_compute_checksum (vm, b, ip6, &bogus);
225 }
Dave Barach2c0a4f42017-06-29 09:30:15 -0400226 }
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
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200233static_always_inline u16
234tso_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
257static_always_inline void
258tso_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
269static_always_inline void
270tso_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
287static_always_inline void
288tso_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
317static_always_inline u32
318tso_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 {
Dave Baracha2aefef2019-02-27 12:36:28 -0500388 if (nloops-- <= 0)
389 clib_panic ("infinite loop detected");
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200390 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
444static_always_inline void
445drop_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
Dave Barach2c0a4f42017-06-29 09:30:15 -0400465static_always_inline uword
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200466vnet_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)
Dave Barach2c0a4f42017-06-29 09:30:15 -0400473{
Dave Barachba868bb2016-08-08 09:51:21 -0400474 vnet_interface_output_runtime_t *rt = (void *) node->runtime_data;
475 vnet_sw_interface_t *si;
Dave Barachba868bb2016-08-08 09:51:21 -0400476 u32 n_left_to_tx, *from, *from_end, *to_tx;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700477 u32 n_bytes, n_buffers, n_packets;
Damjan Marion363640d2017-03-06 11:53:10 +0100478 u32 n_bytes_b0, n_bytes_b1, n_bytes_b2, n_bytes_b3;
Damjan Marion586afd72017-04-05 19:18:20 +0200479 u32 thread_index = vm->thread_index;
Dave Barachba868bb2016-08-08 09:51:21 -0400480 vnet_interface_main_t *im = &vnm->interface_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700481 u32 next_index = VNET_INTERFACE_OUTPUT_NEXT_TX;
Damjan Marion152e21d2016-11-29 14:55:43 +0100482 u32 current_config_index = ~0;
483 u8 arc = im->output_feature_arc_index;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200484 vnet_interface_per_thread_data_t *ptd =
485 vec_elt_at_index (im->per_thread_data, thread_index);
Zhiyong Yanga462c072019-05-05 19:32:25 +0800486 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700487
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
Damjan Mariona3d59862018-11-10 10:23:00 +0100493 from = vlib_frame_vector_args (frame);
Zhiyong Yanga462c072019-05-05 19:32:25 +0800494 vlib_get_buffers (vm, from, b, n_buffers);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700495
496 if (rt->is_deleted)
Dave Barachba868bb2016-08-08 09:51:21 -0400497 return vlib_error_drop_buffers (vm, node, from,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700498 /* 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);
Steve Shin042a6212017-07-07 14:57:46 -0700506 if (!(si->flags & (VNET_SW_INTERFACE_FLAG_ADMIN_UP |
507 VNET_SW_INTERFACE_FLAG_BOND_SLAVE)) ||
Dave Barachba868bb2016-08-08 09:51:21 -0400508 !(hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700509 {
Dave Barachba868bb2016-08-08 09:51:21 -0400510 vlib_simple_counter_main_t *cm;
511
Ed Warnickecb9cada2015-12-08 15:45:58 -0700512 cm = vec_elt_at_index (vnm->interface_main.sw_if_counters,
Dave Barachba868bb2016-08-08 09:51:21 -0400513 VNET_INTERFACE_COUNTER_TX_ERROR);
Damjan Marion586afd72017-04-05 19:18:20 +0200514 vlib_increment_simple_counter (cm, thread_index,
Dave Barachba868bb2016-08-08 09:51:21 -0400515 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);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700523 }
524
525 from_end = from + n_buffers;
526
527 /* Total byte count of all buffers. */
528 n_bytes = 0;
529 n_packets = 0;
530
Damjan Marion152e21d2016-11-29 14:55:43 +0100531 /* 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
Ed Warnickecb9cada2015-12-08 15:45:58 -0700542 while (from < from_end)
543 {
544 /* Get new next frame since previous incomplete frame may have less
Dave Barachba868bb2016-08-08 09:51:21 -0400545 than VNET_FRAME_SIZE vectors in it. */
546 vlib_get_new_next_frame (vm, node, next_index, to_tx, n_left_to_tx);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700547
Damjan Marion363640d2017-03-06 11:53:10 +0100548 while (from + 8 <= from_end && n_left_to_tx >= 4)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700549 {
Damjan Marion363640d2017-03-06 11:53:10 +0100550 u32 bi0, bi1, bi2, bi3;
Damjan Marion363640d2017-03-06 11:53:10 +0100551 u32 tx_swif0, tx_swif1, tx_swif2, tx_swif3;
Dave Barach2c0a4f42017-06-29 09:30:15 -0400552 u32 or_flags;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700553
554 /* Prefetch next iteration. */
Zhiyong Yanga462c072019-05-05 19:32:25 +0800555 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);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700559
560 bi0 = from[0];
561 bi1 = from[1];
Damjan Marion363640d2017-03-06 11:53:10 +0100562 bi2 = from[2];
563 bi3 = from[3];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700564 to_tx[0] = bi0;
565 to_tx[1] = bi1;
Damjan Marion363640d2017-03-06 11:53:10 +0100566 to_tx[2] = bi2;
567 to_tx[3] = bi3;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700568
Zhiyong Yanga462c072019-05-05 19:32:25 +0800569 or_flags = b[0]->flags | b[1]->flags | b[2]->flags | b[3]->flags;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700570
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200571 if (do_segmentation)
572 {
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200573 /* go to single loop if we need TSO segmentation */
574 if (PREDICT_FALSE (or_flags & VNET_BUFFER_F_GSO))
575 break;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200576 }
Zhiyong Yanga462c072019-05-05 19:32:25 +0800577 from += 4;
578 to_tx += 4;
579 n_left_to_tx -= 4;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200580
Ed Warnickecb9cada2015-12-08 15:45:58 -0700581 /* Be grumpy about zero length buffers for benefit of
582 driver tx function. */
Zhiyong Yanga462c072019-05-05 19:32:25 +0800583 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);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700587
Zhiyong Yanga462c072019-05-05 19:32:25 +0800588 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];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700596
597 n_bytes += n_bytes_b0 + n_bytes_b1;
Damjan Marion363640d2017-03-06 11:53:10 +0100598 n_bytes += n_bytes_b2 + n_bytes_b3;
599 n_packets += 4;
Dave Barachba868bb2016-08-08 09:51:21 -0400600
Damjan Marion152e21d2016-11-29 14:55:43 +0100601 if (PREDICT_FALSE (current_config_index != ~0))
602 {
Zhiyong Yanga462c072019-05-05 19:32:25 +0800603 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;
Damjan Marion152e21d2016-11-29 14:55:43 +0100611 }
612
Damjan Marion363640d2017-03-06 11:53:10 +0100613 /* update vlan subif tx counts, if required */
Damjan Marion9c6ae5f2016-11-15 23:20:01 +0100614 if (PREDICT_FALSE (tx_swif0 != rt->sw_if_index))
Dave Barachba868bb2016-08-08 09:51:21 -0400615 {
Damjan Marion9c6ae5f2016-11-15 23:20:01 +0100616 vlib_increment_combined_counter (im->combined_sw_if_counters +
617 VNET_INTERFACE_COUNTER_TX,
Damjan Marion586afd72017-04-05 19:18:20 +0200618 thread_index, tx_swif0, 1,
Damjan Marion9c6ae5f2016-11-15 23:20:01 +0100619 n_bytes_b0);
Dave Barachba868bb2016-08-08 09:51:21 -0400620 }
Damjan Marion9c6ae5f2016-11-15 23:20:01 +0100621
Damjan Marion9c6ae5f2016-11-15 23:20:01 +0100622 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,
Damjan Marion586afd72017-04-05 19:18:20 +0200627 thread_index, tx_swif1, 1,
Damjan Marion9c6ae5f2016-11-15 23:20:01 +0100628 n_bytes_b1);
629 }
Damjan Marion363640d2017-03-06 11:53:10 +0100630
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,
Damjan Marion586afd72017-04-05 19:18:20 +0200636 thread_index, tx_swif2, 1,
Damjan Marion363640d2017-03-06 11:53:10 +0100637 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,
Damjan Marion586afd72017-04-05 19:18:20 +0200644 thread_index, tx_swif3, 1,
Damjan Marion363640d2017-03-06 11:53:10 +0100645 n_bytes_b3);
646 }
Dave Barach2c0a4f42017-06-29 09:30:15 -0400647
Dave Barach2c0a4f42017-06-29 09:30:15 -0400648 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 {
Zhiyong Yanga462c072019-05-05 19:32:25 +0800655 calc_checksums (vm, b[0]);
656 calc_checksums (vm, b[1]);
657 calc_checksums (vm, b[2]);
658 calc_checksums (vm, b[3]);
Dave Barach2c0a4f42017-06-29 09:30:15 -0400659 }
660 }
Zhiyong Yanga462c072019-05-05 19:32:25 +0800661 b += 4;
662
Ed Warnickecb9cada2015-12-08 15:45:58 -0700663 }
664
665 while (from + 1 <= from_end && n_left_to_tx >= 1)
666 {
667 u32 bi0;
Dave Barachba868bb2016-08-08 09:51:21 -0400668 u32 tx_swif0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700669
670 bi0 = from[0];
671 to_tx[0] = bi0;
672 from += 1;
673 to_tx += 1;
674 n_left_to_tx -= 1;
675
Ed Warnickecb9cada2015-12-08 15:45:58 -0700676 /* Be grumpy about zero length buffers for benefit of
677 driver tx function. */
Zhiyong Yanga462c072019-05-05 19:32:25 +0800678 ASSERT (b[0]->current_length > 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700679
Zhiyong Yanga462c072019-05-05 19:32:25 +0800680 n_bytes_b0 = vlib_buffer_length_in_chain (vm, b[0]);
681 tx_swif0 = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700682 n_bytes += n_bytes_b0;
683 n_packets += 1;
684
Damjan Marion152e21d2016-11-29 14:55:43 +0100685 if (PREDICT_FALSE (current_config_index != ~0))
686 {
Zhiyong Yanga462c072019-05-05 19:32:25 +0800687 vnet_buffer (b[0])->feature_arc_index = arc;
688 b[0]->current_config_index = current_config_index;
Damjan Marion152e21d2016-11-29 14:55:43 +0100689 }
690
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200691 if (do_segmentation)
692 {
Zhiyong Yanga462c072019-05-05 19:32:25 +0800693 if (PREDICT_FALSE (b[0]->flags & VNET_BUFFER_F_GSO))
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200694 {
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 =
Zhiyong Yanga462c072019-05-05 19:32:25 +0800709 tso_segment_buffer (vm, ptd, do_tx_offloads, bi0, b[0],
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200710 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);
Mohsin Kazmidac1edb2019-06-18 23:45:54 +0200716 b += 1;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200717 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 }
Mohsin Kazmidac1edb2019-06-18 23:45:54 +0200741 while (n_tx_bufs > 0)
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200742 {
Mohsin Kazmidac1edb2019-06-18 23:45:54 +0200743 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;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200749 }
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);
Mohsin Kazmidac1edb2019-06-18 23:45:54 +0200764 b += 1;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200765 continue;
766 }
767 }
768
Damjan Marion9c6ae5f2016-11-15 23:20:01 +0100769 if (PREDICT_FALSE (tx_swif0 != rt->sw_if_index))
Dave Barachba868bb2016-08-08 09:51:21 -0400770 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700771
Damjan Marion9c6ae5f2016-11-15 23:20:01 +0100772 vlib_increment_combined_counter (im->combined_sw_if_counters +
773 VNET_INTERFACE_COUNTER_TX,
Damjan Marion586afd72017-04-05 19:18:20 +0200774 thread_index, tx_swif0, 1,
Damjan Marion9c6ae5f2016-11-15 23:20:01 +0100775 n_bytes_b0);
Dave Barachba868bb2016-08-08 09:51:21 -0400776 }
Dave Barach2c0a4f42017-06-29 09:30:15 -0400777
778 if (do_tx_offloads)
Zhiyong Yanga462c072019-05-05 19:32:25 +0800779 calc_checksums (vm, b[0]);
780
781 b += 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700782 }
783
Dave Barachba868bb2016-08-08 09:51:21 -0400784 vlib_put_next_frame (vm, node, next_index, n_left_to_tx);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700785 }
786
787 /* Update main interface stats. */
788 vlib_increment_combined_counter (im->combined_sw_if_counters
Dave Barachba868bb2016-08-08 09:51:21 -0400789 + VNET_INTERFACE_COUNTER_TX,
Damjan Marion586afd72017-04-05 19:18:20 +0200790 thread_index,
Dave Barachba868bb2016-08-08 09:51:21 -0400791 rt->sw_if_index, n_packets, n_bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700792 return n_buffers;
793}
Filip Tehlar62668772019-03-04 03:33:32 -0800794#endif /* CLIB_MARCH_VARIANT */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700795
Dave Barach5ecd5a52019-02-25 15:27:28 -0500796static_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 (vm->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 (vm->pcap[VLIB_TX].pcap_sw_if_index == 0 ||
826 vm->pcap[VLIB_TX].pcap_sw_if_index == sw_if_index)
827 pcap_add_buffer (&vm->pcap[VLIB_TX].pcap_main, vm, bi0, 512);
828 from++;
829 n_left_from--;
830 }
831}
832
Filip Tehlar62668772019-03-04 03:33:32 -0800833#ifndef CLIB_MARCH_VARIANT
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200834static_always_inline uword
835vnet_interface_output_node_inline (vlib_main_t * vm,
836 vlib_node_runtime_t * node,
837 vlib_frame_t * frame, vnet_main_t * vnm,
838 vnet_hw_interface_t * hi,
839 int do_tx_offloads)
840{
841 /*
842 * The 3-headed "if" is here because we want to err on the side
843 * of not impacting the non-GSO performance - so for the more
844 * common case of no GSO interfaces we want to prevent the
845 * segmentation codepath from being there altogether.
846 */
847 if (PREDICT_TRUE (vnm->interface_main.gso_interface_count == 0))
848 return vnet_interface_output_node_inline_gso (vm, node, frame, vnm, hi,
849 do_tx_offloads,
850 /* do_segmentation */ 0);
851 else if (hi->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO)
852 return vnet_interface_output_node_inline_gso (vm, node, frame, vnm, hi,
853 do_tx_offloads,
854 /* do_segmentation */ 0);
855 else
856 return vnet_interface_output_node_inline_gso (vm, node, frame, vnm, hi,
857 do_tx_offloads,
858 /* do_segmentation */ 1);
859}
860
Damjan Marion652d2e12019-02-02 00:15:27 +0100861uword
Dave Barach2c0a4f42017-06-29 09:30:15 -0400862vnet_interface_output_node (vlib_main_t * vm, vlib_node_runtime_t * node,
863 vlib_frame_t * frame)
864{
865 vnet_main_t *vnm = vnet_get_main ();
866 vnet_hw_interface_t *hi;
867 vnet_interface_output_runtime_t *rt = (void *) node->runtime_data;
868 hi = vnet_get_sup_hw_interface (vnm, rt->sw_if_index);
869
Dave Barach5ecd5a52019-02-25 15:27:28 -0500870 vnet_interface_pcap_tx_trace (vm, node, frame,
871 0 /* sw_if_index_from_buffer */ );
872
Dave Barach2c0a4f42017-06-29 09:30:15 -0400873 if (hi->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD)
874 return vnet_interface_output_node_inline (vm, node, frame, vnm, hi,
875 /* do_tx_offloads */ 0);
876 else
877 return vnet_interface_output_node_inline (vm, node, frame, vnm, hi,
878 /* do_tx_offloads */ 1);
879}
Filip Tehlar62668772019-03-04 03:33:32 -0800880#endif /* CLIB_MARCH_VARIANT */
Dave Barach2c0a4f42017-06-29 09:30:15 -0400881
Ed Warnickecb9cada2015-12-08 15:45:58 -0700882/* Use buffer's sw_if_index[VNET_TX] to choose output interface. */
Filip Tehlar62668772019-03-04 03:33:32 -0800883VLIB_NODE_FN (vnet_per_buffer_interface_output_node) (vlib_main_t * vm,
884 vlib_node_runtime_t *
885 node,
886 vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700887{
Dave Barachba868bb2016-08-08 09:51:21 -0400888 vnet_main_t *vnm = vnet_get_main ();
889 u32 n_left_to_next, *from, *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700890 u32 n_left_from, next_index;
891
Dave Barach5ecd5a52019-02-25 15:27:28 -0500892 vnet_interface_pcap_tx_trace (vm, node, frame,
893 1 /* sw_if_index_from_buffer */ );
894
Ed Warnickecb9cada2015-12-08 15:45:58 -0700895 n_left_from = frame->n_vectors;
896
Damjan Mariona3d59862018-11-10 10:23:00 +0100897 from = vlib_frame_vector_args (frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700898 next_index = node->cached_next_index;
899
900 while (n_left_from > 0)
901 {
902 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
903
904 while (n_left_from >= 4 && n_left_to_next >= 2)
905 {
906 u32 bi0, bi1, next0, next1;
Dave Barachba868bb2016-08-08 09:51:21 -0400907 vlib_buffer_t *b0, *b1;
908 vnet_hw_interface_t *hi0, *hi1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700909
910 /* Prefetch next iteration. */
911 vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
912 vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
913
914 bi0 = from[0];
915 bi1 = from[1];
916 to_next[0] = bi0;
917 to_next[1] = bi1;
918 from += 2;
919 to_next += 2;
920 n_left_to_next -= 2;
921 n_left_from -= 2;
922
923 b0 = vlib_get_buffer (vm, bi0);
924 b1 = vlib_get_buffer (vm, bi1);
925
Dave Barachba868bb2016-08-08 09:51:21 -0400926 hi0 =
927 vnet_get_sup_hw_interface (vnm,
928 vnet_buffer (b0)->sw_if_index
929 [VLIB_TX]);
930 hi1 =
931 vnet_get_sup_hw_interface (vnm,
932 vnet_buffer (b1)->sw_if_index
933 [VLIB_TX]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700934
John Loe5453d02018-01-23 19:21:34 -0500935 next0 = hi0->output_node_next_index;
936 next1 = hi1->output_node_next_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700937
Dave Barachba868bb2016-08-08 09:51:21 -0400938 vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
939 n_left_to_next, bi0, bi1, next0,
940 next1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700941 }
942
943 while (n_left_from > 0 && n_left_to_next > 0)
944 {
945 u32 bi0, next0;
Dave Barachba868bb2016-08-08 09:51:21 -0400946 vlib_buffer_t *b0;
947 vnet_hw_interface_t *hi0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700948
949 bi0 = from[0];
950 to_next[0] = bi0;
951 from += 1;
952 to_next += 1;
953 n_left_to_next -= 1;
954 n_left_from -= 1;
955
956 b0 = vlib_get_buffer (vm, bi0);
957
Dave Barachba868bb2016-08-08 09:51:21 -0400958 hi0 =
959 vnet_get_sup_hw_interface (vnm,
960 vnet_buffer (b0)->sw_if_index
961 [VLIB_TX]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700962
John Loe5453d02018-01-23 19:21:34 -0500963 next0 = hi0->output_node_next_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700964
Dave Barachba868bb2016-08-08 09:51:21 -0400965 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
966 n_left_to_next, bi0, next0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700967 }
968
969 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
970 }
971
972 return frame->n_vectors;
973}
974
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000975typedef struct vnet_error_trace_t_
Ed Warnickecb9cada2015-12-08 15:45:58 -0700976{
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000977 u32 sw_if_index;
978} vnet_error_trace_t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700979
Ed Warnickecb9cada2015-12-08 15:45:58 -0700980
Dave Barachba868bb2016-08-08 09:51:21 -0400981static u8 *
982format_vnet_error_trace (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700983{
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000984 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700985 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000986 vnet_error_trace_t *t = va_arg (*va, vnet_error_trace_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700987
Neale Ranns7227c392019-03-21 10:20:15 +0000988 s = format (s, "rx:%U", format_vnet_sw_if_index_name,
989 vnet_get_main (), t->sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700990
991 return s;
992}
993
994static void
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000995interface_trace_buffers (vlib_main_t * vm,
996 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700997{
Dave Barachba868bb2016-08-08 09:51:21 -0400998 u32 n_left, *buffers;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700999
1000 buffers = vlib_frame_vector_args (frame);
1001 n_left = frame->n_vectors;
Dave Barachba868bb2016-08-08 09:51:21 -04001002
Ed Warnickecb9cada2015-12-08 15:45:58 -07001003 while (n_left >= 4)
1004 {
1005 u32 bi0, bi1;
Dave Barachba868bb2016-08-08 09:51:21 -04001006 vlib_buffer_t *b0, *b1;
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001007 vnet_error_trace_t *t0, *t1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001008
1009 /* Prefetch next iteration. */
1010 vlib_prefetch_buffer_with_index (vm, buffers[2], LOAD);
1011 vlib_prefetch_buffer_with_index (vm, buffers[3], LOAD);
1012
1013 bi0 = buffers[0];
1014 bi1 = buffers[1];
1015
1016 b0 = vlib_get_buffer (vm, bi0);
1017 b1 = vlib_get_buffer (vm, bi1);
1018
1019 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1020 {
1021 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001022 t0->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001023 }
1024 if (b1->flags & VLIB_BUFFER_IS_TRACED)
1025 {
1026 t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0]));
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001027 t1->sw_if_index = vnet_buffer (b1)->sw_if_index[VLIB_RX];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001028 }
1029 buffers += 2;
1030 n_left -= 2;
1031 }
1032
1033 while (n_left >= 1)
1034 {
1035 u32 bi0;
Dave Barachba868bb2016-08-08 09:51:21 -04001036 vlib_buffer_t *b0;
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001037 vnet_error_trace_t *t0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001038
1039 bi0 = buffers[0];
1040
1041 b0 = vlib_get_buffer (vm, bi0);
1042
1043 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1044 {
1045 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001046 t0->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001047 }
1048 buffers += 1;
1049 n_left -= 1;
1050 }
1051}
1052
Dave Barachba868bb2016-08-08 09:51:21 -04001053typedef enum
1054{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001055 VNET_ERROR_DISPOSITION_DROP,
1056 VNET_ERROR_DISPOSITION_PUNT,
1057 VNET_ERROR_N_DISPOSITION,
1058} vnet_error_disposition_t;
1059
Ed Warnickecb9cada2015-12-08 15:45:58 -07001060static_always_inline uword
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001061interface_drop_punt (vlib_main_t * vm,
1062 vlib_node_runtime_t * node,
1063 vlib_frame_t * frame,
1064 vnet_error_disposition_t disposition)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001065{
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001066 u32 *from, n_left, thread_index, *sw_if_index;
1067 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
1068 u32 sw_if_indices[VLIB_FRAME_SIZE];
Dave Barachba868bb2016-08-08 09:51:21 -04001069 vlib_simple_counter_main_t *cm;
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001070 u16 nexts[VLIB_FRAME_SIZE];
1071 vnet_main_t *vnm;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001072
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001073 vnm = vnet_get_main ();
1074 thread_index = vm->thread_index;
1075 from = vlib_frame_vector_args (frame);
1076 n_left = frame->n_vectors;
1077 b = bufs;
1078 sw_if_index = sw_if_indices;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001079
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001080 vlib_get_buffers (vm, from, bufs, n_left);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001081
1082 if (node->flags & VLIB_NODE_FLAG_TRACE)
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001083 interface_trace_buffers (vm, node, frame);
Dave Barachba868bb2016-08-08 09:51:21 -04001084
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001085 /* All going to drop regardless, this is just a counting exercise */
1086 clib_memset (nexts, 0, sizeof (nexts));
1087
Ed Warnickecb9cada2015-12-08 15:45:58 -07001088 cm = vec_elt_at_index (vnm->interface_main.sw_if_counters,
1089 (disposition == VNET_ERROR_DISPOSITION_PUNT
1090 ? VNET_INTERFACE_COUNTER_PUNT
1091 : VNET_INTERFACE_COUNTER_DROP));
1092
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001093 /* collect the array of interfaces first ... */
1094 while (n_left >= 4)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001095 {
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001096 if (n_left >= 12)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001097 {
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001098 /* Prefetch 8 ahead - there's not much going on in each iteration */
1099 vlib_prefetch_buffer_header (b[4], LOAD);
1100 vlib_prefetch_buffer_header (b[5], LOAD);
1101 vlib_prefetch_buffer_header (b[6], LOAD);
1102 vlib_prefetch_buffer_header (b[7], LOAD);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001103 }
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001104 sw_if_index[0] = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
1105 sw_if_index[1] = vnet_buffer (b[1])->sw_if_index[VLIB_RX];
1106 sw_if_index[2] = vnet_buffer (b[2])->sw_if_index[VLIB_RX];
1107 sw_if_index[3] = vnet_buffer (b[3])->sw_if_index[VLIB_RX];
1108
1109 sw_if_index += 4;
1110 n_left -= 4;
1111 b += 4;
1112 }
1113 while (n_left)
1114 {
1115 sw_if_index[0] = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
1116
1117 sw_if_index += 1;
1118 n_left -= 1;
1119 b += 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001120 }
1121
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001122 /* ... then count against them in blocks */
1123 n_left = frame->n_vectors;
1124
1125 while (n_left)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001126 {
Dave Barachba868bb2016-08-08 09:51:21 -04001127 vnet_sw_interface_t *sw_if0;
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001128 u16 off, count;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001129
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001130 off = frame->n_vectors - n_left;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001131
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001132 sw_if_index = sw_if_indices + off;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001133
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001134 count = clib_count_equal_u32 (sw_if_index, n_left);
1135 n_left -= count;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001136
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001137 vlib_increment_simple_counter (cm, thread_index, sw_if_index[0], count);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001138
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001139 /* Increment super-interface drop/punt counters for
1140 sub-interfaces. */
1141 sw_if0 = vnet_get_sw_interface (vnm, sw_if_index[0]);
1142 if (sw_if0->sup_sw_if_index != sw_if_index[0])
1143 vlib_increment_simple_counter
1144 (cm, thread_index, sw_if0->sup_sw_if_index, count);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001145 }
1146
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001147 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001148
1149 return frame->n_vectors;
1150}
1151
Dave Barachba868bb2016-08-08 09:51:21 -04001152static inline void
1153pcap_drop_trace (vlib_main_t * vm,
1154 vnet_interface_main_t * im, vlib_frame_t * f)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001155{
Dave Barachba868bb2016-08-08 09:51:21 -04001156 u32 *from;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001157 u32 n_left = f->n_vectors;
Dave Barachba868bb2016-08-08 09:51:21 -04001158 vlib_buffer_t *b0, *p1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001159 u32 bi0;
1160 i16 save_current_data;
1161 u16 save_current_length;
1162
1163 from = vlib_frame_vector_args (f);
1164
1165 while (n_left > 0)
1166 {
1167 if (PREDICT_TRUE (n_left > 1))
Dave Barachba868bb2016-08-08 09:51:21 -04001168 {
1169 p1 = vlib_get_buffer (vm, from[1]);
1170 vlib_prefetch_buffer_header (p1, LOAD);
1171 }
1172
Ed Warnickecb9cada2015-12-08 15:45:58 -07001173 bi0 = from[0];
1174 b0 = vlib_get_buffer (vm, bi0);
1175 from++;
1176 n_left--;
Dave Barachba868bb2016-08-08 09:51:21 -04001177
Ed Warnickecb9cada2015-12-08 15:45:58 -07001178 /* See if we're pointedly ignoring this specific error */
Dave Barachba868bb2016-08-08 09:51:21 -04001179 if (im->pcap_drop_filter_hash
1180 && hash_get (im->pcap_drop_filter_hash, b0->error))
1181 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001182
1183 /* Trace all drops, or drops received on a specific interface */
1184 if (im->pcap_sw_if_index == 0 ||
Dave Barachba868bb2016-08-08 09:51:21 -04001185 im->pcap_sw_if_index == vnet_buffer (b0)->sw_if_index[VLIB_RX])
1186 {
1187 save_current_data = b0->current_data;
1188 save_current_length = b0->current_length;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001189
Dave Barachba868bb2016-08-08 09:51:21 -04001190 /*
1191 * Typically, we'll need to rewind the buffer
1192 */
1193 if (b0->current_data > 0)
1194 vlib_buffer_advance (b0, (word) - b0->current_data);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001195
Dave Barachba868bb2016-08-08 09:51:21 -04001196 pcap_add_buffer (&im->pcap_main, vm, bi0, 512);
1197
1198 b0->current_data = save_current_data;
1199 b0->current_length = save_current_length;
1200 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001201 }
1202}
1203
Filip Tehlar62668772019-03-04 03:33:32 -08001204#ifndef CLIB_MARCH_VARIANT
Dave Barachba868bb2016-08-08 09:51:21 -04001205void
1206vnet_pcap_drop_trace_filter_add_del (u32 error_index, int is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001207{
Dave Barachba868bb2016-08-08 09:51:21 -04001208 vnet_interface_main_t *im = &vnet_get_main ()->interface_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001209
1210 if (im->pcap_drop_filter_hash == 0)
Dave Barachba868bb2016-08-08 09:51:21 -04001211 im->pcap_drop_filter_hash = hash_create (0, sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001212
1213 if (is_add)
1214 hash_set (im->pcap_drop_filter_hash, error_index, 1);
1215 else
1216 hash_unset (im->pcap_drop_filter_hash, error_index);
1217}
Filip Tehlar62668772019-03-04 03:33:32 -08001218#endif /* CLIB_MARCH_VARIANT */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001219
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001220VLIB_NODE_FN (interface_drop) (vlib_main_t * vm,
1221 vlib_node_runtime_t * node,
1222 vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001223{
Dave Barachba868bb2016-08-08 09:51:21 -04001224 vnet_interface_main_t *im = &vnet_get_main ()->interface_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001225
1226 if (PREDICT_FALSE (im->drop_pcap_enable))
1227 pcap_drop_trace (vm, im, frame);
1228
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001229 return interface_drop_punt (vm, node, frame, VNET_ERROR_DISPOSITION_DROP);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001230}
1231
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001232VLIB_NODE_FN (interface_punt) (vlib_main_t * vm,
1233 vlib_node_runtime_t * node,
1234 vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001235{
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001236 return interface_drop_punt (vm, node, frame, VNET_ERROR_DISPOSITION_PUNT);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001237}
1238
Dave Barachba868bb2016-08-08 09:51:21 -04001239/* *INDENT-OFF* */
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001240VLIB_REGISTER_NODE (interface_drop) = {
Ed Warnickecb9cada2015-12-08 15:45:58 -07001241 .name = "error-drop",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001242 .vector_size = sizeof (u32),
1243 .format_trace = format_vnet_error_trace,
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001244 .n_next_nodes = 1,
1245 .next_nodes = {
1246 [0] = "drop",
1247 },
Ed Warnickecb9cada2015-12-08 15:45:58 -07001248};
Dave Barachba868bb2016-08-08 09:51:21 -04001249/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001250
Dave Barachba868bb2016-08-08 09:51:21 -04001251/* *INDENT-OFF* */
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001252VLIB_REGISTER_NODE (interface_punt) = {
Ed Warnickecb9cada2015-12-08 15:45:58 -07001253 .name = "error-punt",
1254 .vector_size = sizeof (u32),
1255 .format_trace = format_vnet_error_trace,
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001256 .n_next_nodes = 1,
1257 .next_nodes = {
1258 [0] = "punt",
1259 },
Ed Warnickecb9cada2015-12-08 15:45:58 -07001260};
Dave Barachba868bb2016-08-08 09:51:21 -04001261/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001262
Dave Barachba868bb2016-08-08 09:51:21 -04001263/* *INDENT-OFF* */
Filip Tehlar62668772019-03-04 03:33:32 -08001264VLIB_REGISTER_NODE (vnet_per_buffer_interface_output_node) = {
Ed Warnickecb9cada2015-12-08 15:45:58 -07001265 .name = "interface-output",
1266 .vector_size = sizeof (u32),
1267};
Dave Barachba868bb2016-08-08 09:51:21 -04001268/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001269
Damjan Marion152e21d2016-11-29 14:55:43 +01001270static uword
1271interface_tx_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
1272 vlib_frame_t * from_frame)
1273{
1274 vnet_main_t *vnm = vnet_get_main ();
1275 u32 last_sw_if_index = ~0;
1276 vlib_frame_t *to_frame = 0;
1277 vnet_hw_interface_t *hw = 0;
1278 u32 *from, *to_next = 0;
1279 u32 n_left_from;
1280
1281 from = vlib_frame_vector_args (from_frame);
1282 n_left_from = from_frame->n_vectors;
1283 while (n_left_from > 0)
1284 {
1285 u32 bi0;
1286 vlib_buffer_t *b0;
1287 u32 sw_if_index0;
1288
1289 bi0 = from[0];
1290 from++;
1291 n_left_from--;
1292 b0 = vlib_get_buffer (vm, bi0);
1293 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
1294
1295 if (PREDICT_FALSE ((last_sw_if_index != sw_if_index0) || to_frame == 0))
1296 {
1297 if (to_frame)
1298 {
1299 hw = vnet_get_sup_hw_interface (vnm, last_sw_if_index);
1300 vlib_put_frame_to_node (vm, hw->tx_node_index, to_frame);
1301 }
1302 last_sw_if_index = sw_if_index0;
1303 hw = vnet_get_sup_hw_interface (vnm, sw_if_index0);
1304 to_frame = vlib_get_frame_to_node (vm, hw->tx_node_index);
1305 to_next = vlib_frame_vector_args (to_frame);
1306 }
1307
1308 to_next[0] = bi0;
1309 to_next++;
1310 to_frame->n_vectors++;
1311 }
1312 vlib_put_frame_to_node (vm, hw->tx_node_index, to_frame);
1313 return from_frame->n_vectors;
1314}
1315
1316/* *INDENT-OFF* */
1317VLIB_REGISTER_NODE (interface_tx, static) = {
1318 .function = interface_tx_node_fn,
1319 .name = "interface-tx",
1320 .vector_size = sizeof (u32),
1321 .n_next_nodes = 1,
1322 .next_nodes = {
1323 [0] = "error-drop",
1324 },
1325};
1326
1327VNET_FEATURE_ARC_INIT (interface_output, static) =
1328{
1329 .arc_name = "interface-output",
1330 .start_nodes = VNET_FEATURES (0),
Dave Baracha25def72018-11-26 11:04:45 -05001331 .last_in_arc = "interface-tx",
Damjan Marion152e21d2016-11-29 14:55:43 +01001332 .arc_index_ptr = &vnet_main.interface_main.output_feature_arc_index,
1333};
1334
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +01001335VNET_FEATURE_INIT (span_tx, static) = {
1336 .arc_name = "interface-output",
1337 .node_name = "span-output",
1338 .runs_before = VNET_FEATURES ("interface-tx"),
1339};
1340
Matthew Smith537eeec2018-04-09 11:49:20 -05001341VNET_FEATURE_INIT (ipsec_if_tx, static) = {
1342 .arc_name = "interface-output",
1343 .node_name = "ipsec-if-output",
1344 .runs_before = VNET_FEATURES ("interface-tx"),
1345};
1346
Damjan Marion152e21d2016-11-29 14:55:43 +01001347VNET_FEATURE_INIT (interface_tx, static) = {
1348 .arc_name = "interface-output",
1349 .node_name = "interface-tx",
1350 .runs_before = 0,
1351};
1352/* *INDENT-ON* */
1353
Filip Tehlar62668772019-03-04 03:33:32 -08001354#ifndef CLIB_MARCH_VARIANT
Ed Warnickecb9cada2015-12-08 15:45:58 -07001355clib_error_t *
1356vnet_per_buffer_interface_output_hw_interface_add_del (vnet_main_t * vnm,
1357 u32 hw_if_index,
1358 u32 is_create)
1359{
Dave Barachba868bb2016-08-08 09:51:21 -04001360 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001361 u32 next_index;
1362
John Loe5453d02018-01-23 19:21:34 -05001363 if (hi->output_node_index == 0)
1364 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001365
John Loe5453d02018-01-23 19:21:34 -05001366 next_index = vlib_node_add_next
1367 (vnm->vlib_main, vnet_per_buffer_interface_output_node.index,
1368 hi->output_node_index);
1369 hi->output_node_next_index = next_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001370
1371 return 0;
1372}
1373
Dave Barachba868bb2016-08-08 09:51:21 -04001374VNET_HW_INTERFACE_ADD_DEL_FUNCTION
1375 (vnet_per_buffer_interface_output_hw_interface_add_del);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001376
John Loe5453d02018-01-23 19:21:34 -05001377void
1378vnet_set_interface_output_node (vnet_main_t * vnm,
1379 u32 hw_if_index, u32 node_index)
1380{
1381 ASSERT (node_index);
1382 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
1383 u32 next_index = vlib_node_add_next
1384 (vnm->vlib_main, vnet_per_buffer_interface_output_node.index, node_index);
1385 hi->output_node_next_index = next_index;
1386 hi->output_node_index = node_index;
1387}
Filip Tehlar62668772019-03-04 03:33:32 -08001388#endif /* CLIB_MARCH_VARIANT */
John Loe5453d02018-01-23 19:21:34 -05001389
Ed Warnickecb9cada2015-12-08 15:45:58 -07001390static clib_error_t *
1391pcap_drop_trace_command_fn (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -04001392 unformat_input_t * input,
1393 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001394{
Dave Barachba868bb2016-08-08 09:51:21 -04001395 vnet_main_t *vnm = vnet_get_main ();
1396 vnet_interface_main_t *im = &vnm->interface_main;
1397 u8 *filename;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001398 u32 max;
1399 int matched = 0;
Dave Barachba868bb2016-08-08 09:51:21 -04001400 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001401
Dave Barachba868bb2016-08-08 09:51:21 -04001402 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001403 {
1404 if (unformat (input, "on"))
Dave Barachba868bb2016-08-08 09:51:21 -04001405 {
1406 if (im->drop_pcap_enable == 0)
1407 {
1408 if (im->pcap_filename == 0)
1409 im->pcap_filename = format (0, "/tmp/drop.pcap%c", 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001410
Dave Barachb7b92992018-10-17 10:38:51 -04001411 clib_memset (&im->pcap_main, 0, sizeof (im->pcap_main));
Dave Barachba868bb2016-08-08 09:51:21 -04001412 im->pcap_main.file_name = (char *) im->pcap_filename;
1413 im->pcap_main.n_packets_to_capture = 100;
1414 if (im->pcap_pkts_to_capture)
1415 im->pcap_main.n_packets_to_capture = im->pcap_pkts_to_capture;
1416
1417 im->pcap_main.packet_type = PCAP_PACKET_TYPE_ethernet;
1418 im->drop_pcap_enable = 1;
1419 matched = 1;
1420 vlib_cli_output (vm, "pcap drop capture on...");
1421 }
1422 else
1423 {
1424 vlib_cli_output (vm, "pcap drop capture already on...");
1425 }
1426 matched = 1;
1427 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001428 else if (unformat (input, "off"))
Dave Barachba868bb2016-08-08 09:51:21 -04001429 {
1430 matched = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001431
Dave Barachba868bb2016-08-08 09:51:21 -04001432 if (im->drop_pcap_enable)
1433 {
1434 vlib_cli_output (vm, "captured %d pkts...",
1435 im->pcap_main.n_packets_captured);
1436 if (im->pcap_main.n_packets_captured)
1437 {
1438 im->pcap_main.n_packets_to_capture =
1439 im->pcap_main.n_packets_captured;
1440 error = pcap_write (&im->pcap_main);
1441 if (error)
1442 clib_error_report (error);
1443 else
1444 vlib_cli_output (vm, "saved to %s...", im->pcap_filename);
1445 }
1446 }
1447 else
1448 {
1449 vlib_cli_output (vm, "pcap drop capture already off...");
1450 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001451
Dave Barachba868bb2016-08-08 09:51:21 -04001452 im->drop_pcap_enable = 0;
1453 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001454 else if (unformat (input, "max %d", &max))
Dave Barachba868bb2016-08-08 09:51:21 -04001455 {
1456 im->pcap_pkts_to_capture = max;
1457 matched = 1;
1458 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001459
Dave Barachba868bb2016-08-08 09:51:21 -04001460 else if (unformat (input, "intfc %U",
1461 unformat_vnet_sw_interface, vnm,
1462 &im->pcap_sw_if_index))
1463 matched = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001464 else if (unformat (input, "intfc any"))
Dave Barachba868bb2016-08-08 09:51:21 -04001465 {
1466 im->pcap_sw_if_index = 0;
1467 matched = 1;
1468 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001469 else if (unformat (input, "file %s", &filename))
Dave Barachba868bb2016-08-08 09:51:21 -04001470 {
1471 u8 *chroot_filename;
1472 /* Brain-police user path input */
1473 if (strstr ((char *) filename, "..")
1474 || index ((char *) filename, '/'))
1475 {
1476 vlib_cli_output (vm, "illegal characters in filename '%s'",
1477 filename);
1478 continue;
1479 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001480
Dave Barachba868bb2016-08-08 09:51:21 -04001481 chroot_filename = format (0, "/tmp/%s%c", filename, 0);
1482 vec_free (filename);
1483
1484 if (im->pcap_filename)
1485 vec_free (im->pcap_filename);
Dave Barachba868bb2016-08-08 09:51:21 -04001486 im->pcap_filename = chroot_filename;
jerryian361abef2017-04-10 18:48:07 +08001487 im->pcap_main.file_name = (char *) im->pcap_filename;
Dave Barachba868bb2016-08-08 09:51:21 -04001488 matched = 1;
1489 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001490 else if (unformat (input, "status"))
Dave Barachba868bb2016-08-08 09:51:21 -04001491 {
1492 if (im->drop_pcap_enable == 0)
1493 {
1494 vlib_cli_output (vm, "pcap drop capture is off...");
1495 continue;
1496 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001497
Dave Barachba868bb2016-08-08 09:51:21 -04001498 vlib_cli_output (vm, "pcap drop capture: %d of %d pkts...",
1499 im->pcap_main.n_packets_captured,
1500 im->pcap_main.n_packets_to_capture);
1501 matched = 1;
1502 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001503
1504 else
Dave Barachba868bb2016-08-08 09:51:21 -04001505 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001506 }
1507
1508 if (matched == 0)
Dave Barachba868bb2016-08-08 09:51:21 -04001509 return clib_error_return (0, "unknown input `%U'",
1510 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001511
1512 return 0;
1513}
1514
Dave Barachba868bb2016-08-08 09:51:21 -04001515/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001516VLIB_CLI_COMMAND (pcap_trace_command, static) = {
Dave Barachba868bb2016-08-08 09:51:21 -04001517 .path = "pcap drop trace",
1518 .short_help =
1519 "pcap drop trace on off max <nn> intfc <intfc> file <name> status",
1520 .function = pcap_drop_trace_command_fn,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001521};
Dave Barachba868bb2016-08-08 09:51:21 -04001522/* *INDENT-ON* */
1523
1524/*
1525 * fd.io coding-style-patch-verification: ON
1526 *
1527 * Local Variables:
1528 * eval: (c-set-style "gnu")
1529 * End:
1530 */