blob: eadee69291981ec3ba84cd998a73f12128dd09d6 [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>
Mohsin Kazmi72e73122019-10-22 13:33:13 +020041#include <vnet/gso/gso.h>
Dave Barach2c0a4f42017-06-29 09:30:15 -040042#include <vnet/ip/icmp46_packet.h>
43#include <vnet/ip/ip4.h>
44#include <vnet/ip/ip6.h>
45#include <vnet/udp/udp_packet.h>
Damjan Marion152e21d2016-11-29 14:55:43 +010046#include <vnet/feature/feature.h>
Dave Barach9137e542019-09-13 17:47:50 -040047#include <vnet/classify/trace_classify.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070048
Dave Barachba868bb2016-08-08 09:51:21 -040049typedef struct
50{
Ed Warnickecb9cada2015-12-08 15:45:58 -070051 u32 sw_if_index;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +020052 u32 flags;
Mohsin Kazmi29467b52019-10-08 19:42:38 +020053 u8 data[128 - 2 * 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 s =
86 format (s, "\n%U%U", format_white_space, indent,
87 node->format_buffer ? node->format_buffer : format_hex_bytes,
88 t->data, sizeof (t->data));
Ed Warnickecb9cada2015-12-08 15:45:58 -070089 }
90 return s;
91}
92
93static void
94vnet_interface_output_trace (vlib_main_t * vm,
95 vlib_node_runtime_t * node,
Dave Barachba868bb2016-08-08 09:51:21 -040096 vlib_frame_t * frame, uword n_buffers)
Ed Warnickecb9cada2015-12-08 15:45:58 -070097{
Dave Barachba868bb2016-08-08 09:51:21 -040098 u32 n_left, *from;
Ed Warnickecb9cada2015-12-08 15:45:58 -070099
100 n_left = n_buffers;
Damjan Mariona3d59862018-11-10 10:23:00 +0100101 from = vlib_frame_vector_args (frame);
Dave Barachba868bb2016-08-08 09:51:21 -0400102
Ed Warnickecb9cada2015-12-08 15:45:58 -0700103 while (n_left >= 4)
104 {
105 u32 bi0, bi1;
Dave Barachba868bb2016-08-08 09:51:21 -0400106 vlib_buffer_t *b0, *b1;
107 interface_output_trace_t *t0, *t1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700108
109 /* Prefetch next iteration. */
110 vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
111 vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
112
113 bi0 = from[0];
114 bi1 = from[1];
115
116 b0 = vlib_get_buffer (vm, bi0);
117 b1 = vlib_get_buffer (vm, bi1);
118
119 if (b0->flags & VLIB_BUFFER_IS_TRACED)
120 {
121 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
122 t0->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX];
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200123 t0->flags = b0->flags;
Dave Barach178cf492018-11-13 16:34:13 -0500124 clib_memcpy_fast (t0->data, vlib_buffer_get_current (b0),
125 sizeof (t0->data));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700126 }
127 if (b1->flags & VLIB_BUFFER_IS_TRACED)
128 {
129 t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0]));
130 t1->sw_if_index = vnet_buffer (b1)->sw_if_index[VLIB_TX];
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200131 t1->flags = b1->flags;
Dave Barach178cf492018-11-13 16:34:13 -0500132 clib_memcpy_fast (t1->data, vlib_buffer_get_current (b1),
133 sizeof (t1->data));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700134 }
135 from += 2;
136 n_left -= 2;
137 }
138
139 while (n_left >= 1)
140 {
141 u32 bi0;
Dave Barachba868bb2016-08-08 09:51:21 -0400142 vlib_buffer_t *b0;
143 interface_output_trace_t *t0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700144
145 bi0 = from[0];
146
147 b0 = vlib_get_buffer (vm, bi0);
148
149 if (b0->flags & VLIB_BUFFER_IS_TRACED)
150 {
151 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
152 t0->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX];
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200153 t0->flags = b0->flags;
Dave Barach178cf492018-11-13 16:34:13 -0500154 clib_memcpy_fast (t0->data, vlib_buffer_get_current (b0),
155 sizeof (t0->data));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700156 }
157 from += 1;
158 n_left -= 1;
159 }
160}
161
Dave Barach2c0a4f42017-06-29 09:30:15 -0400162static_always_inline void
163calc_checksums (vlib_main_t * vm, vlib_buffer_t * b)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700164{
Dave Barach2c0a4f42017-06-29 09:30:15 -0400165 tcp_header_t *th;
166 udp_header_t *uh;
Mohsin Kazmi72e73122019-10-22 13:33:13 +0200167 gso_header_offset_t gho = { 0 };
Dave Barach2c0a4f42017-06-29 09:30:15 -0400168
169 int is_ip4 = (b->flags & VNET_BUFFER_F_IS_IP4) != 0;
170 int is_ip6 = (b->flags & VNET_BUFFER_F_IS_IP6) != 0;
171
172 ASSERT (!(is_ip4 && is_ip6));
173
Mohsin Kazmi72e73122019-10-22 13:33:13 +0200174 gho = vnet_gso_header_offset_parser (b, is_ip6);
175 th = (tcp_header_t *) (vlib_buffer_get_current (b) + gho.l4_hdr_offset);
176 uh = (udp_header_t *) (vlib_buffer_get_current (b) + gho.l4_hdr_offset);
Dave Barach2c0a4f42017-06-29 09:30:15 -0400177
178 if (is_ip4)
179 {
Zhiyong Yang24c50122019-04-19 05:22:31 -0400180 ip4_header_t *ip4;
181
Mohsin Kazmi72e73122019-10-22 13:33:13 +0200182 ip4 =
183 (ip4_header_t *) (vlib_buffer_get_current (b) + gho.l3_hdr_offset);
Dave Barach2c0a4f42017-06-29 09:30:15 -0400184 if (b->flags & VNET_BUFFER_F_OFFLOAD_IP_CKSUM)
185 ip4->checksum = ip4_header_checksum (ip4);
186 if (b->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM)
Andrej Kozemcak9d7570c2019-01-07 08:39:22 +0100187 {
188 th->checksum = 0;
189 th->checksum = ip4_tcp_udp_compute_checksum (vm, b, ip4);
190 }
Zhiyong Yang24c50122019-04-19 05:22:31 -0400191 else if (b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM)
Steven Luong03328ec2020-01-27 10:37:56 -0800192 {
193 uh->checksum = 0;
194 uh->checksum = ip4_tcp_udp_compute_checksum (vm, b, ip4);
195 }
Dave Barach2c0a4f42017-06-29 09:30:15 -0400196 }
Zhiyong Yang24c50122019-04-19 05:22:31 -0400197 else if (is_ip6)
Dave Barach2c0a4f42017-06-29 09:30:15 -0400198 {
199 int bogus;
Zhiyong Yang24c50122019-04-19 05:22:31 -0400200 ip6_header_t *ip6;
201
Mohsin Kazmi72e73122019-10-22 13:33:13 +0200202 ip6 =
203 (ip6_header_t *) (vlib_buffer_get_current (b) + gho.l3_hdr_offset);
Dave Barach2c0a4f42017-06-29 09:30:15 -0400204 if (b->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM)
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200205 {
206 th->checksum = 0;
207 th->checksum =
208 ip6_tcp_udp_icmp_compute_checksum (vm, b, ip6, &bogus);
209 }
Zhiyong Yang24c50122019-04-19 05:22:31 -0400210 else if (b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM)
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200211 {
212 uh->checksum = 0;
213 uh->checksum =
214 ip6_tcp_udp_icmp_compute_checksum (vm, b, ip6, &bogus);
215 }
Dave Barach2c0a4f42017-06-29 09:30:15 -0400216 }
Florin Coras08f2a5d2019-08-06 13:48:50 -0700217 b->flags &= ~VNET_BUFFER_F_OFFLOAD_TCP_CKSUM;
218 b->flags &= ~VNET_BUFFER_F_OFFLOAD_UDP_CKSUM;
219 b->flags &= ~VNET_BUFFER_F_OFFLOAD_IP_CKSUM;
Dave Barach2c0a4f42017-06-29 09:30:15 -0400220}
221
222static_always_inline uword
Mohsin Kazmi29467b52019-10-08 19:42:38 +0200223vnet_interface_output_node_inline (vlib_main_t * vm,
224 vlib_node_runtime_t * node,
225 vlib_frame_t * frame,
226 vnet_main_t * vnm,
227 vnet_hw_interface_t * hi,
228 int do_tx_offloads)
Dave Barach2c0a4f42017-06-29 09:30:15 -0400229{
Dave Barachba868bb2016-08-08 09:51:21 -0400230 vnet_interface_output_runtime_t *rt = (void *) node->runtime_data;
231 vnet_sw_interface_t *si;
Dave Barachba868bb2016-08-08 09:51:21 -0400232 u32 n_left_to_tx, *from, *from_end, *to_tx;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700233 u32 n_bytes, n_buffers, n_packets;
Damjan Marion363640d2017-03-06 11:53:10 +0100234 u32 n_bytes_b0, n_bytes_b1, n_bytes_b2, n_bytes_b3;
Damjan Marion586afd72017-04-05 19:18:20 +0200235 u32 thread_index = vm->thread_index;
Dave Barachba868bb2016-08-08 09:51:21 -0400236 vnet_interface_main_t *im = &vnm->interface_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700237 u32 next_index = VNET_INTERFACE_OUTPUT_NEXT_TX;
Damjan Marion152e21d2016-11-29 14:55:43 +0100238 u32 current_config_index = ~0;
239 u8 arc = im->output_feature_arc_index;
Zhiyong Yanga462c072019-05-05 19:32:25 +0800240 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700241
242 n_buffers = frame->n_vectors;
243
244 if (node->flags & VLIB_NODE_FLAG_TRACE)
245 vnet_interface_output_trace (vm, node, frame, n_buffers);
246
Damjan Mariona3d59862018-11-10 10:23:00 +0100247 from = vlib_frame_vector_args (frame);
Zhiyong Yanga462c072019-05-05 19:32:25 +0800248 vlib_get_buffers (vm, from, b, n_buffers);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700249
250 if (rt->is_deleted)
Dave Barachba868bb2016-08-08 09:51:21 -0400251 return vlib_error_drop_buffers (vm, node, from,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700252 /* buffer stride */ 1,
253 n_buffers,
254 VNET_INTERFACE_OUTPUT_NEXT_DROP,
255 node->node_index,
256 VNET_INTERFACE_OUTPUT_ERROR_INTERFACE_DELETED);
257
258 si = vnet_get_sw_interface (vnm, rt->sw_if_index);
259 hi = vnet_get_sup_hw_interface (vnm, rt->sw_if_index);
Steven Luong5ad541e2019-08-27 07:43:27 -0700260 if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ||
Dave Barachba868bb2016-08-08 09:51:21 -0400261 !(hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700262 {
Dave Barachba868bb2016-08-08 09:51:21 -0400263 vlib_simple_counter_main_t *cm;
264
Ed Warnickecb9cada2015-12-08 15:45:58 -0700265 cm = vec_elt_at_index (vnm->interface_main.sw_if_counters,
Dave Barachba868bb2016-08-08 09:51:21 -0400266 VNET_INTERFACE_COUNTER_TX_ERROR);
Damjan Marion586afd72017-04-05 19:18:20 +0200267 vlib_increment_simple_counter (cm, thread_index,
Dave Barachba868bb2016-08-08 09:51:21 -0400268 rt->sw_if_index, n_buffers);
269
270 return vlib_error_drop_buffers (vm, node, from,
271 /* buffer stride */ 1,
272 n_buffers,
273 VNET_INTERFACE_OUTPUT_NEXT_DROP,
274 node->node_index,
275 VNET_INTERFACE_OUTPUT_ERROR_INTERFACE_DOWN);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700276 }
277
278 from_end = from + n_buffers;
279
280 /* Total byte count of all buffers. */
281 n_bytes = 0;
282 n_packets = 0;
283
Damjan Marion152e21d2016-11-29 14:55:43 +0100284 /* interface-output feature arc handling */
285 if (PREDICT_FALSE (vnet_have_features (arc, rt->sw_if_index)))
286 {
287 vnet_feature_config_main_t *fcm;
288 fcm = vnet_feature_get_config_main (arc);
289 current_config_index = vnet_get_feature_config_index (arc,
290 rt->sw_if_index);
291 vnet_get_config_data (&fcm->config_main, &current_config_index,
292 &next_index, 0);
293 }
294
Ed Warnickecb9cada2015-12-08 15:45:58 -0700295 while (from < from_end)
296 {
297 /* Get new next frame since previous incomplete frame may have less
Dave Barachba868bb2016-08-08 09:51:21 -0400298 than VNET_FRAME_SIZE vectors in it. */
299 vlib_get_new_next_frame (vm, node, next_index, to_tx, n_left_to_tx);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700300
Damjan Marion363640d2017-03-06 11:53:10 +0100301 while (from + 8 <= from_end && n_left_to_tx >= 4)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700302 {
Damjan Marion363640d2017-03-06 11:53:10 +0100303 u32 bi0, bi1, bi2, bi3;
Damjan Marion363640d2017-03-06 11:53:10 +0100304 u32 tx_swif0, tx_swif1, tx_swif2, tx_swif3;
Dave Barach2c0a4f42017-06-29 09:30:15 -0400305 u32 or_flags;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700306
307 /* Prefetch next iteration. */
Zhiyong Yanga462c072019-05-05 19:32:25 +0800308 vlib_prefetch_buffer_header (b[4], LOAD);
309 vlib_prefetch_buffer_header (b[5], LOAD);
310 vlib_prefetch_buffer_header (b[6], LOAD);
311 vlib_prefetch_buffer_header (b[7], LOAD);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700312
313 bi0 = from[0];
314 bi1 = from[1];
Damjan Marion363640d2017-03-06 11:53:10 +0100315 bi2 = from[2];
316 bi3 = from[3];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700317 to_tx[0] = bi0;
318 to_tx[1] = bi1;
Damjan Marion363640d2017-03-06 11:53:10 +0100319 to_tx[2] = bi2;
320 to_tx[3] = bi3;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700321
Zhiyong Yanga462c072019-05-05 19:32:25 +0800322 or_flags = b[0]->flags | b[1]->flags | b[2]->flags | b[3]->flags;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700323
Zhiyong Yanga462c072019-05-05 19:32:25 +0800324 from += 4;
325 to_tx += 4;
326 n_left_to_tx -= 4;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200327
Ed Warnickecb9cada2015-12-08 15:45:58 -0700328 /* Be grumpy about zero length buffers for benefit of
329 driver tx function. */
Zhiyong Yanga462c072019-05-05 19:32:25 +0800330 ASSERT (b[0]->current_length > 0);
331 ASSERT (b[1]->current_length > 0);
332 ASSERT (b[2]->current_length > 0);
333 ASSERT (b[3]->current_length > 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700334
Zhiyong Yanga462c072019-05-05 19:32:25 +0800335 n_bytes_b0 = vlib_buffer_length_in_chain (vm, b[0]);
336 n_bytes_b1 = vlib_buffer_length_in_chain (vm, b[1]);
337 n_bytes_b2 = vlib_buffer_length_in_chain (vm, b[2]);
338 n_bytes_b3 = vlib_buffer_length_in_chain (vm, b[3]);
339 tx_swif0 = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
340 tx_swif1 = vnet_buffer (b[1])->sw_if_index[VLIB_TX];
341 tx_swif2 = vnet_buffer (b[2])->sw_if_index[VLIB_TX];
342 tx_swif3 = vnet_buffer (b[3])->sw_if_index[VLIB_TX];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700343
344 n_bytes += n_bytes_b0 + n_bytes_b1;
Damjan Marion363640d2017-03-06 11:53:10 +0100345 n_bytes += n_bytes_b2 + n_bytes_b3;
346 n_packets += 4;
Dave Barachba868bb2016-08-08 09:51:21 -0400347
Damjan Marion152e21d2016-11-29 14:55:43 +0100348 if (PREDICT_FALSE (current_config_index != ~0))
349 {
Zhiyong Yanga462c072019-05-05 19:32:25 +0800350 vnet_buffer (b[0])->feature_arc_index = arc;
351 vnet_buffer (b[1])->feature_arc_index = arc;
352 vnet_buffer (b[2])->feature_arc_index = arc;
353 vnet_buffer (b[3])->feature_arc_index = arc;
354 b[0]->current_config_index = current_config_index;
355 b[1]->current_config_index = current_config_index;
356 b[2]->current_config_index = current_config_index;
357 b[3]->current_config_index = current_config_index;
Damjan Marion152e21d2016-11-29 14:55:43 +0100358 }
359
Damjan Marion363640d2017-03-06 11:53:10 +0100360 /* update vlan subif tx counts, if required */
Damjan Marion9c6ae5f2016-11-15 23:20:01 +0100361 if (PREDICT_FALSE (tx_swif0 != rt->sw_if_index))
Dave Barachba868bb2016-08-08 09:51:21 -0400362 {
Damjan Marion9c6ae5f2016-11-15 23:20:01 +0100363 vlib_increment_combined_counter (im->combined_sw_if_counters +
364 VNET_INTERFACE_COUNTER_TX,
Damjan Marion586afd72017-04-05 19:18:20 +0200365 thread_index, tx_swif0, 1,
Damjan Marion9c6ae5f2016-11-15 23:20:01 +0100366 n_bytes_b0);
Dave Barachba868bb2016-08-08 09:51:21 -0400367 }
Damjan Marion9c6ae5f2016-11-15 23:20:01 +0100368
Damjan Marion9c6ae5f2016-11-15 23:20:01 +0100369 if (PREDICT_FALSE (tx_swif1 != rt->sw_if_index))
370 {
371
372 vlib_increment_combined_counter (im->combined_sw_if_counters +
373 VNET_INTERFACE_COUNTER_TX,
Damjan Marion586afd72017-04-05 19:18:20 +0200374 thread_index, tx_swif1, 1,
Damjan Marion9c6ae5f2016-11-15 23:20:01 +0100375 n_bytes_b1);
376 }
Damjan Marion363640d2017-03-06 11:53:10 +0100377
378 if (PREDICT_FALSE (tx_swif2 != rt->sw_if_index))
379 {
380
381 vlib_increment_combined_counter (im->combined_sw_if_counters +
382 VNET_INTERFACE_COUNTER_TX,
Damjan Marion586afd72017-04-05 19:18:20 +0200383 thread_index, tx_swif2, 1,
Damjan Marion363640d2017-03-06 11:53:10 +0100384 n_bytes_b2);
385 }
386 if (PREDICT_FALSE (tx_swif3 != rt->sw_if_index))
387 {
388
389 vlib_increment_combined_counter (im->combined_sw_if_counters +
390 VNET_INTERFACE_COUNTER_TX,
Damjan Marion586afd72017-04-05 19:18:20 +0200391 thread_index, tx_swif3, 1,
Damjan Marion363640d2017-03-06 11:53:10 +0100392 n_bytes_b3);
393 }
Dave Barach2c0a4f42017-06-29 09:30:15 -0400394
Dave Barach2c0a4f42017-06-29 09:30:15 -0400395 if (do_tx_offloads)
396 {
397 if (or_flags &
398 (VNET_BUFFER_F_OFFLOAD_TCP_CKSUM |
399 VNET_BUFFER_F_OFFLOAD_UDP_CKSUM |
400 VNET_BUFFER_F_OFFLOAD_IP_CKSUM))
401 {
Zhiyong Yanga462c072019-05-05 19:32:25 +0800402 calc_checksums (vm, b[0]);
403 calc_checksums (vm, b[1]);
404 calc_checksums (vm, b[2]);
405 calc_checksums (vm, b[3]);
Dave Barach2c0a4f42017-06-29 09:30:15 -0400406 }
407 }
Zhiyong Yanga462c072019-05-05 19:32:25 +0800408 b += 4;
409
Ed Warnickecb9cada2015-12-08 15:45:58 -0700410 }
411
412 while (from + 1 <= from_end && n_left_to_tx >= 1)
413 {
414 u32 bi0;
Dave Barachba868bb2016-08-08 09:51:21 -0400415 u32 tx_swif0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700416
417 bi0 = from[0];
418 to_tx[0] = bi0;
419 from += 1;
420 to_tx += 1;
421 n_left_to_tx -= 1;
422
Ed Warnickecb9cada2015-12-08 15:45:58 -0700423 /* Be grumpy about zero length buffers for benefit of
424 driver tx function. */
Zhiyong Yanga462c072019-05-05 19:32:25 +0800425 ASSERT (b[0]->current_length > 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700426
Zhiyong Yanga462c072019-05-05 19:32:25 +0800427 n_bytes_b0 = vlib_buffer_length_in_chain (vm, b[0]);
428 tx_swif0 = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700429 n_bytes += n_bytes_b0;
430 n_packets += 1;
431
Damjan Marion152e21d2016-11-29 14:55:43 +0100432 if (PREDICT_FALSE (current_config_index != ~0))
433 {
Zhiyong Yanga462c072019-05-05 19:32:25 +0800434 vnet_buffer (b[0])->feature_arc_index = arc;
435 b[0]->current_config_index = current_config_index;
Damjan Marion152e21d2016-11-29 14:55:43 +0100436 }
437
Damjan Marion9c6ae5f2016-11-15 23:20:01 +0100438 if (PREDICT_FALSE (tx_swif0 != rt->sw_if_index))
Dave Barachba868bb2016-08-08 09:51:21 -0400439 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700440
Damjan Marion9c6ae5f2016-11-15 23:20:01 +0100441 vlib_increment_combined_counter (im->combined_sw_if_counters +
442 VNET_INTERFACE_COUNTER_TX,
Damjan Marion586afd72017-04-05 19:18:20 +0200443 thread_index, tx_swif0, 1,
Damjan Marion9c6ae5f2016-11-15 23:20:01 +0100444 n_bytes_b0);
Dave Barachba868bb2016-08-08 09:51:21 -0400445 }
Dave Barach2c0a4f42017-06-29 09:30:15 -0400446
447 if (do_tx_offloads)
Mohsin Kazmi29467b52019-10-08 19:42:38 +0200448 {
449 if (b[0]->flags &
450 (VNET_BUFFER_F_OFFLOAD_TCP_CKSUM |
451 VNET_BUFFER_F_OFFLOAD_UDP_CKSUM |
452 VNET_BUFFER_F_OFFLOAD_IP_CKSUM))
453 calc_checksums (vm, b[0]);
454 }
Zhiyong Yanga462c072019-05-05 19:32:25 +0800455 b += 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700456 }
457
Dave Barachba868bb2016-08-08 09:51:21 -0400458 vlib_put_next_frame (vm, node, next_index, n_left_to_tx);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700459 }
460
461 /* Update main interface stats. */
462 vlib_increment_combined_counter (im->combined_sw_if_counters
Dave Barachba868bb2016-08-08 09:51:21 -0400463 + VNET_INTERFACE_COUNTER_TX,
Damjan Marion586afd72017-04-05 19:18:20 +0200464 thread_index,
Dave Barachba868bb2016-08-08 09:51:21 -0400465 rt->sw_if_index, n_packets, n_bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700466 return n_buffers;
467}
Filip Tehlar62668772019-03-04 03:33:32 -0800468#endif /* CLIB_MARCH_VARIANT */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700469
Dave Barach5ecd5a52019-02-25 15:27:28 -0500470static_always_inline void vnet_interface_pcap_tx_trace
471 (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame,
472 int sw_if_index_from_buffer)
473{
474 u32 n_left_from, *from;
475 u32 sw_if_index;
Dave Barach33909772019-09-23 10:27:27 -0400476 vnet_pcap_t *pp = &vlib_global_main.pcap;
Dave Barach5ecd5a52019-02-25 15:27:28 -0500477
Dave Barach33909772019-09-23 10:27:27 -0400478 if (PREDICT_TRUE (pp->pcap_tx_enable == 0))
Dave Barach5ecd5a52019-02-25 15:27:28 -0500479 return;
480
481 if (sw_if_index_from_buffer == 0)
482 {
483 vnet_interface_output_runtime_t *rt = (void *) node->runtime_data;
484 sw_if_index = rt->sw_if_index;
485 }
486 else
487 sw_if_index = ~0;
488
489 n_left_from = frame->n_vectors;
490 from = vlib_frame_vector_args (frame);
491
492 while (n_left_from > 0)
493 {
Dave Barach9137e542019-09-13 17:47:50 -0400494 int classify_filter_result;
Dave Barach5ecd5a52019-02-25 15:27:28 -0500495 u32 bi0 = from[0];
496 vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
Dave Barach9137e542019-09-13 17:47:50 -0400497 from++;
498 n_left_from--;
499
Dave Barachf5667c32019-09-25 11:27:46 -0400500 if (pp->filter_classify_table_index != ~0)
Dave Barach9137e542019-09-13 17:47:50 -0400501 {
502 classify_filter_result =
503 vnet_is_packet_traced_inline
Dave Barachf5667c32019-09-25 11:27:46 -0400504 (b0, pp->filter_classify_table_index, 0 /* full classify */ );
Dave Barach9137e542019-09-13 17:47:50 -0400505 if (classify_filter_result)
Dave Barach33909772019-09-23 10:27:27 -0400506 pcap_add_buffer (&pp->pcap_main, vm, bi0, pp->max_bytes_per_pkt);
Dave Barach9137e542019-09-13 17:47:50 -0400507 continue;
508 }
Dave Barach5ecd5a52019-02-25 15:27:28 -0500509
510 if (sw_if_index_from_buffer)
511 sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX];
512
Dave Barach33909772019-09-23 10:27:27 -0400513 if (pp->pcap_sw_if_index == 0 || pp->pcap_sw_if_index == sw_if_index)
Dave Barachd28437c2019-11-20 09:28:31 -0500514 {
515 vnet_main_t *vnm = vnet_get_main ();
516 vnet_hw_interface_t *hi =
517 vnet_get_sup_hw_interface (vnm, sw_if_index);
518 /* Capture pkt if not filtered, or if filter hits */
519 if (hi->trace_classify_table_index == ~0 ||
520 vnet_is_packet_traced_inline
521 (b0, hi->trace_classify_table_index, 0 /* full classify */ ))
522 pcap_add_buffer (&pp->pcap_main, vm, bi0, pp->max_bytes_per_pkt);
523 }
Dave Barach5ecd5a52019-02-25 15:27:28 -0500524 }
525}
526
Filip Tehlar62668772019-03-04 03:33:32 -0800527#ifndef CLIB_MARCH_VARIANT
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200528
Damjan Marion652d2e12019-02-02 00:15:27 +0100529uword
Dave Barach2c0a4f42017-06-29 09:30:15 -0400530vnet_interface_output_node (vlib_main_t * vm, vlib_node_runtime_t * node,
531 vlib_frame_t * frame)
532{
533 vnet_main_t *vnm = vnet_get_main ();
534 vnet_hw_interface_t *hi;
535 vnet_interface_output_runtime_t *rt = (void *) node->runtime_data;
536 hi = vnet_get_sup_hw_interface (vnm, rt->sw_if_index);
537
Dave Barach5ecd5a52019-02-25 15:27:28 -0500538 vnet_interface_pcap_tx_trace (vm, node, frame,
539 0 /* sw_if_index_from_buffer */ );
540
Dave Barach2c0a4f42017-06-29 09:30:15 -0400541 if (hi->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD)
542 return vnet_interface_output_node_inline (vm, node, frame, vnm, hi,
543 /* do_tx_offloads */ 0);
544 else
545 return vnet_interface_output_node_inline (vm, node, frame, vnm, hi,
546 /* do_tx_offloads */ 1);
547}
Filip Tehlar62668772019-03-04 03:33:32 -0800548#endif /* CLIB_MARCH_VARIANT */
Dave Barach2c0a4f42017-06-29 09:30:15 -0400549
Ed Warnickecb9cada2015-12-08 15:45:58 -0700550/* Use buffer's sw_if_index[VNET_TX] to choose output interface. */
Filip Tehlar62668772019-03-04 03:33:32 -0800551VLIB_NODE_FN (vnet_per_buffer_interface_output_node) (vlib_main_t * vm,
552 vlib_node_runtime_t *
553 node,
554 vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700555{
Dave Barachba868bb2016-08-08 09:51:21 -0400556 vnet_main_t *vnm = vnet_get_main ();
557 u32 n_left_to_next, *from, *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700558 u32 n_left_from, next_index;
559
Dave Barach5ecd5a52019-02-25 15:27:28 -0500560 vnet_interface_pcap_tx_trace (vm, node, frame,
561 1 /* sw_if_index_from_buffer */ );
562
Ed Warnickecb9cada2015-12-08 15:45:58 -0700563 n_left_from = frame->n_vectors;
564
Damjan Mariona3d59862018-11-10 10:23:00 +0100565 from = vlib_frame_vector_args (frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700566 next_index = node->cached_next_index;
567
568 while (n_left_from > 0)
569 {
570 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
571
572 while (n_left_from >= 4 && n_left_to_next >= 2)
573 {
574 u32 bi0, bi1, next0, next1;
Dave Barachba868bb2016-08-08 09:51:21 -0400575 vlib_buffer_t *b0, *b1;
576 vnet_hw_interface_t *hi0, *hi1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700577
578 /* Prefetch next iteration. */
579 vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
580 vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
581
582 bi0 = from[0];
583 bi1 = from[1];
584 to_next[0] = bi0;
585 to_next[1] = bi1;
586 from += 2;
587 to_next += 2;
588 n_left_to_next -= 2;
589 n_left_from -= 2;
590
591 b0 = vlib_get_buffer (vm, bi0);
592 b1 = vlib_get_buffer (vm, bi1);
593
Dave Barachba868bb2016-08-08 09:51:21 -0400594 hi0 =
595 vnet_get_sup_hw_interface (vnm,
596 vnet_buffer (b0)->sw_if_index
597 [VLIB_TX]);
598 hi1 =
599 vnet_get_sup_hw_interface (vnm,
600 vnet_buffer (b1)->sw_if_index
601 [VLIB_TX]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700602
John Loe5453d02018-01-23 19:21:34 -0500603 next0 = hi0->output_node_next_index;
604 next1 = hi1->output_node_next_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700605
Dave Barachba868bb2016-08-08 09:51:21 -0400606 vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
607 n_left_to_next, bi0, bi1, next0,
608 next1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700609 }
610
611 while (n_left_from > 0 && n_left_to_next > 0)
612 {
613 u32 bi0, next0;
Dave Barachba868bb2016-08-08 09:51:21 -0400614 vlib_buffer_t *b0;
615 vnet_hw_interface_t *hi0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700616
617 bi0 = from[0];
618 to_next[0] = bi0;
619 from += 1;
620 to_next += 1;
621 n_left_to_next -= 1;
622 n_left_from -= 1;
623
624 b0 = vlib_get_buffer (vm, bi0);
625
Dave Barachba868bb2016-08-08 09:51:21 -0400626 hi0 =
627 vnet_get_sup_hw_interface (vnm,
628 vnet_buffer (b0)->sw_if_index
629 [VLIB_TX]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700630
John Loe5453d02018-01-23 19:21:34 -0500631 next0 = hi0->output_node_next_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700632
Dave Barachba868bb2016-08-08 09:51:21 -0400633 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
634 n_left_to_next, bi0, next0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700635 }
636
637 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
638 }
639
640 return frame->n_vectors;
641}
642
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000643typedef struct vnet_error_trace_t_
Ed Warnickecb9cada2015-12-08 15:45:58 -0700644{
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000645 u32 sw_if_index;
646} vnet_error_trace_t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700647
Ed Warnickecb9cada2015-12-08 15:45:58 -0700648
Dave Barachba868bb2016-08-08 09:51:21 -0400649static u8 *
650format_vnet_error_trace (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700651{
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000652 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700653 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000654 vnet_error_trace_t *t = va_arg (*va, vnet_error_trace_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700655
Neale Ranns7227c392019-03-21 10:20:15 +0000656 s = format (s, "rx:%U", format_vnet_sw_if_index_name,
657 vnet_get_main (), t->sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700658
659 return s;
660}
661
662static void
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000663interface_trace_buffers (vlib_main_t * vm,
664 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700665{
Dave Barachba868bb2016-08-08 09:51:21 -0400666 u32 n_left, *buffers;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700667
668 buffers = vlib_frame_vector_args (frame);
669 n_left = frame->n_vectors;
Dave Barachba868bb2016-08-08 09:51:21 -0400670
Ed Warnickecb9cada2015-12-08 15:45:58 -0700671 while (n_left >= 4)
672 {
673 u32 bi0, bi1;
Dave Barachba868bb2016-08-08 09:51:21 -0400674 vlib_buffer_t *b0, *b1;
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000675 vnet_error_trace_t *t0, *t1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700676
677 /* Prefetch next iteration. */
678 vlib_prefetch_buffer_with_index (vm, buffers[2], LOAD);
679 vlib_prefetch_buffer_with_index (vm, buffers[3], LOAD);
680
681 bi0 = buffers[0];
682 bi1 = buffers[1];
683
684 b0 = vlib_get_buffer (vm, bi0);
685 b1 = vlib_get_buffer (vm, bi1);
686
687 if (b0->flags & VLIB_BUFFER_IS_TRACED)
688 {
689 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000690 t0->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700691 }
692 if (b1->flags & VLIB_BUFFER_IS_TRACED)
693 {
694 t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0]));
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000695 t1->sw_if_index = vnet_buffer (b1)->sw_if_index[VLIB_RX];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700696 }
697 buffers += 2;
698 n_left -= 2;
699 }
700
701 while (n_left >= 1)
702 {
703 u32 bi0;
Dave Barachba868bb2016-08-08 09:51:21 -0400704 vlib_buffer_t *b0;
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000705 vnet_error_trace_t *t0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700706
707 bi0 = buffers[0];
708
709 b0 = vlib_get_buffer (vm, bi0);
710
711 if (b0->flags & VLIB_BUFFER_IS_TRACED)
712 {
713 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000714 t0->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700715 }
716 buffers += 1;
717 n_left -= 1;
718 }
719}
720
Dave Barachba868bb2016-08-08 09:51:21 -0400721typedef enum
722{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700723 VNET_ERROR_DISPOSITION_DROP,
724 VNET_ERROR_DISPOSITION_PUNT,
725 VNET_ERROR_N_DISPOSITION,
726} vnet_error_disposition_t;
727
Ed Warnickecb9cada2015-12-08 15:45:58 -0700728static_always_inline uword
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000729interface_drop_punt (vlib_main_t * vm,
730 vlib_node_runtime_t * node,
731 vlib_frame_t * frame,
732 vnet_error_disposition_t disposition)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700733{
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000734 u32 *from, n_left, thread_index, *sw_if_index;
735 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
736 u32 sw_if_indices[VLIB_FRAME_SIZE];
Dave Barachba868bb2016-08-08 09:51:21 -0400737 vlib_simple_counter_main_t *cm;
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000738 u16 nexts[VLIB_FRAME_SIZE];
739 vnet_main_t *vnm;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700740
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000741 vnm = vnet_get_main ();
742 thread_index = vm->thread_index;
743 from = vlib_frame_vector_args (frame);
744 n_left = frame->n_vectors;
745 b = bufs;
746 sw_if_index = sw_if_indices;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700747
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000748 vlib_get_buffers (vm, from, bufs, n_left);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700749
750 if (node->flags & VLIB_NODE_FLAG_TRACE)
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000751 interface_trace_buffers (vm, node, frame);
Dave Barachba868bb2016-08-08 09:51:21 -0400752
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000753 /* All going to drop regardless, this is just a counting exercise */
754 clib_memset (nexts, 0, sizeof (nexts));
755
Ed Warnickecb9cada2015-12-08 15:45:58 -0700756 cm = vec_elt_at_index (vnm->interface_main.sw_if_counters,
757 (disposition == VNET_ERROR_DISPOSITION_PUNT
758 ? VNET_INTERFACE_COUNTER_PUNT
759 : VNET_INTERFACE_COUNTER_DROP));
760
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000761 /* collect the array of interfaces first ... */
762 while (n_left >= 4)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700763 {
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000764 if (n_left >= 12)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700765 {
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000766 /* Prefetch 8 ahead - there's not much going on in each iteration */
767 vlib_prefetch_buffer_header (b[4], LOAD);
768 vlib_prefetch_buffer_header (b[5], LOAD);
769 vlib_prefetch_buffer_header (b[6], LOAD);
770 vlib_prefetch_buffer_header (b[7], LOAD);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700771 }
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000772 sw_if_index[0] = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
773 sw_if_index[1] = vnet_buffer (b[1])->sw_if_index[VLIB_RX];
774 sw_if_index[2] = vnet_buffer (b[2])->sw_if_index[VLIB_RX];
775 sw_if_index[3] = vnet_buffer (b[3])->sw_if_index[VLIB_RX];
776
777 sw_if_index += 4;
778 n_left -= 4;
779 b += 4;
780 }
781 while (n_left)
782 {
783 sw_if_index[0] = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
784
785 sw_if_index += 1;
786 n_left -= 1;
787 b += 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700788 }
789
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000790 /* ... then count against them in blocks */
791 n_left = frame->n_vectors;
792
793 while (n_left)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700794 {
Dave Barachba868bb2016-08-08 09:51:21 -0400795 vnet_sw_interface_t *sw_if0;
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000796 u16 off, count;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700797
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000798 off = frame->n_vectors - n_left;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700799
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000800 sw_if_index = sw_if_indices + off;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700801
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000802 count = clib_count_equal_u32 (sw_if_index, n_left);
803 n_left -= count;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700804
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000805 vlib_increment_simple_counter (cm, thread_index, sw_if_index[0], count);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700806
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000807 /* Increment super-interface drop/punt counters for
808 sub-interfaces. */
809 sw_if0 = vnet_get_sw_interface (vnm, sw_if_index[0]);
810 if (sw_if0->sup_sw_if_index != sw_if_index[0])
811 vlib_increment_simple_counter
812 (cm, thread_index, sw_if0->sup_sw_if_index, count);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700813 }
814
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000815 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700816
817 return frame->n_vectors;
818}
819
Dave Barachba868bb2016-08-08 09:51:21 -0400820static inline void
821pcap_drop_trace (vlib_main_t * vm,
Dave Barach33909772019-09-23 10:27:27 -0400822 vnet_interface_main_t * im,
823 vnet_pcap_t * pp, vlib_frame_t * f)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700824{
Dave Barachba868bb2016-08-08 09:51:21 -0400825 u32 *from;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700826 u32 n_left = f->n_vectors;
Dave Barachba868bb2016-08-08 09:51:21 -0400827 vlib_buffer_t *b0, *p1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700828 u32 bi0;
829 i16 save_current_data;
830 u16 save_current_length;
Dave Barach9382ad92019-09-23 16:03:49 -0400831 vlib_error_main_t *em = &vm->error_main;
Dave Barachf5667c32019-09-25 11:27:46 -0400832 int do_trace = 0;
833
Ed Warnickecb9cada2015-12-08 15:45:58 -0700834
835 from = vlib_frame_vector_args (f);
836
837 while (n_left > 0)
838 {
839 if (PREDICT_TRUE (n_left > 1))
Dave Barachba868bb2016-08-08 09:51:21 -0400840 {
841 p1 = vlib_get_buffer (vm, from[1]);
842 vlib_prefetch_buffer_header (p1, LOAD);
843 }
844
Ed Warnickecb9cada2015-12-08 15:45:58 -0700845 bi0 = from[0];
846 b0 = vlib_get_buffer (vm, bi0);
847 from++;
848 n_left--;
Dave Barachba868bb2016-08-08 09:51:21 -0400849
Ed Warnickecb9cada2015-12-08 15:45:58 -0700850 /* See if we're pointedly ignoring this specific error */
Dave Barachba868bb2016-08-08 09:51:21 -0400851 if (im->pcap_drop_filter_hash
852 && hash_get (im->pcap_drop_filter_hash, b0->error))
853 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700854
Dave Barachf5667c32019-09-25 11:27:46 -0400855 do_trace = (pp->pcap_sw_if_index == 0) ||
856 pp->pcap_sw_if_index == vnet_buffer (b0)->sw_if_index[VLIB_RX];
857
858 if (PREDICT_FALSE
859 (do_trace == 0 && pp->filter_classify_table_index != ~0))
860 {
861 do_trace = vnet_is_packet_traced_inline
862 (b0, pp->filter_classify_table_index, 0 /* full classify */ );
863 }
864
Ed Warnickecb9cada2015-12-08 15:45:58 -0700865 /* Trace all drops, or drops received on a specific interface */
Dave Barachf5667c32019-09-25 11:27:46 -0400866 if (do_trace)
Dave Barachba868bb2016-08-08 09:51:21 -0400867 {
868 save_current_data = b0->current_data;
869 save_current_length = b0->current_length;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700870
Dave Barachba868bb2016-08-08 09:51:21 -0400871 /*
872 * Typically, we'll need to rewind the buffer
Benoît Ganne4e323cb2019-09-17 17:30:35 +0200873 * if l2_hdr_offset is valid, make sure to rewind to the start of
874 * the L2 header. This may not be the buffer start in case we pop-ed
875 * vlan tags.
876 * Otherwise, rewind to buffer start and hope for the best.
Dave Barachba868bb2016-08-08 09:51:21 -0400877 */
Benoît Ganne4e323cb2019-09-17 17:30:35 +0200878 if (b0->flags & VNET_BUFFER_F_L2_HDR_OFFSET_VALID)
879 {
880 if (b0->current_data > vnet_buffer (b0)->l2_hdr_offset)
881 vlib_buffer_advance (b0,
882 vnet_buffer (b0)->l2_hdr_offset -
883 b0->current_data);
884 }
885 else if (b0->current_data > 0)
Dave Barachba868bb2016-08-08 09:51:21 -0400886 vlib_buffer_advance (b0, (word) - b0->current_data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700887
Dave Barach9382ad92019-09-23 16:03:49 -0400888 {
889 vlib_buffer_t *last = b0;
890 u32 error_node_index;
891 int drop_string_len;
892 vlib_node_t *n;
893 /* Length of the error string */
894 int error_string_len =
895 clib_strnlen (em->error_strings_heap[b0->error], 128);
896
897 /* Dig up the drop node */
898 error_node_index = vm->node_main.node_by_error[b0->error];
899 n = vlib_get_node (vm, error_node_index);
900
901 /* Length of full drop string, w/ "nodename: " prepended */
902 drop_string_len = error_string_len + vec_len (n->name) + 2;
903
904 /* Find the last buffer in the chain */
905 while (last->flags & VLIB_BUFFER_NEXT_PRESENT)
906 last = vlib_get_buffer (vm, last->next_buffer);
907
908 /*
909 * Append <nodename>: <error-string> to the capture,
910 * only if we can do that without allocating a new buffer.
911 */
912 if (PREDICT_TRUE ((last->current_data + last->current_length)
913 < (VLIB_BUFFER_DEFAULT_DATA_SIZE
914 - drop_string_len)))
915 {
916 clib_memcpy_fast (last->data + last->current_data +
917 last->current_length, n->name,
918 vec_len (n->name));
919 clib_memcpy_fast (last->data + last->current_data +
920 last->current_length + vec_len (n->name),
921 ": ", 2);
922 clib_memcpy_fast (last->data + last->current_data +
923 last->current_length + vec_len (n->name) +
924 2, em->error_strings_heap[b0->error],
925 error_string_len);
926 last->current_length += drop_string_len;
927 b0->flags &= ~(VLIB_BUFFER_TOTAL_LENGTH_VALID);
928 pcap_add_buffer (&pp->pcap_main, vm, bi0,
929 pp->max_bytes_per_pkt);
930 last->current_length -= drop_string_len;
931 b0->current_data = save_current_data;
932 b0->current_length = save_current_length;
933 continue;
934 }
935 }
936
937 /*
938 * Didn't have space in the last buffer, here's the dropped
939 * packet as-is
940 */
Dave Barach33909772019-09-23 10:27:27 -0400941 pcap_add_buffer (&pp->pcap_main, vm, bi0, pp->max_bytes_per_pkt);
Dave Barachba868bb2016-08-08 09:51:21 -0400942
943 b0->current_data = save_current_data;
944 b0->current_length = save_current_length;
945 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700946 }
947}
948
Filip Tehlar62668772019-03-04 03:33:32 -0800949#ifndef CLIB_MARCH_VARIANT
Dave Barachba868bb2016-08-08 09:51:21 -0400950void
951vnet_pcap_drop_trace_filter_add_del (u32 error_index, int is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700952{
Dave Barachba868bb2016-08-08 09:51:21 -0400953 vnet_interface_main_t *im = &vnet_get_main ()->interface_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700954
955 if (im->pcap_drop_filter_hash == 0)
Dave Barachba868bb2016-08-08 09:51:21 -0400956 im->pcap_drop_filter_hash = hash_create (0, sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700957
958 if (is_add)
959 hash_set (im->pcap_drop_filter_hash, error_index, 1);
960 else
961 hash_unset (im->pcap_drop_filter_hash, error_index);
962}
Filip Tehlar62668772019-03-04 03:33:32 -0800963#endif /* CLIB_MARCH_VARIANT */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700964
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000965VLIB_NODE_FN (interface_drop) (vlib_main_t * vm,
966 vlib_node_runtime_t * node,
967 vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700968{
Dave Barachba868bb2016-08-08 09:51:21 -0400969 vnet_interface_main_t *im = &vnet_get_main ()->interface_main;
Dave Barach33909772019-09-23 10:27:27 -0400970 vnet_pcap_t *pp = &vlib_global_main.pcap;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700971
Dave Barach33909772019-09-23 10:27:27 -0400972 if (PREDICT_FALSE (pp->pcap_drop_enable))
973 pcap_drop_trace (vm, im, pp, frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700974
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000975 return interface_drop_punt (vm, node, frame, VNET_ERROR_DISPOSITION_DROP);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700976}
977
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000978VLIB_NODE_FN (interface_punt) (vlib_main_t * vm,
979 vlib_node_runtime_t * node,
980 vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700981{
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000982 return interface_drop_punt (vm, node, frame, VNET_ERROR_DISPOSITION_PUNT);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700983}
984
Dave Barachba868bb2016-08-08 09:51:21 -0400985/* *INDENT-OFF* */
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000986VLIB_REGISTER_NODE (interface_drop) = {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700987 .name = "error-drop",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700988 .vector_size = sizeof (u32),
989 .format_trace = format_vnet_error_trace,
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000990 .n_next_nodes = 1,
991 .next_nodes = {
992 [0] = "drop",
993 },
Ed Warnickecb9cada2015-12-08 15:45:58 -0700994};
Dave Barachba868bb2016-08-08 09:51:21 -0400995/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700996
Dave Barachba868bb2016-08-08 09:51:21 -0400997/* *INDENT-OFF* */
Neale Ranns22e1f1d2019-03-01 15:53:11 +0000998VLIB_REGISTER_NODE (interface_punt) = {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700999 .name = "error-punt",
1000 .vector_size = sizeof (u32),
1001 .format_trace = format_vnet_error_trace,
Neale Ranns22e1f1d2019-03-01 15:53:11 +00001002 .n_next_nodes = 1,
1003 .next_nodes = {
1004 [0] = "punt",
1005 },
Ed Warnickecb9cada2015-12-08 15:45:58 -07001006};
Dave Barachba868bb2016-08-08 09:51:21 -04001007/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001008
Dave Barachba868bb2016-08-08 09:51:21 -04001009/* *INDENT-OFF* */
Filip Tehlar62668772019-03-04 03:33:32 -08001010VLIB_REGISTER_NODE (vnet_per_buffer_interface_output_node) = {
Ed Warnickecb9cada2015-12-08 15:45:58 -07001011 .name = "interface-output",
1012 .vector_size = sizeof (u32),
1013};
Dave Barachba868bb2016-08-08 09:51:21 -04001014/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001015
Damjan Marion152e21d2016-11-29 14:55:43 +01001016static uword
1017interface_tx_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
1018 vlib_frame_t * from_frame)
1019{
1020 vnet_main_t *vnm = vnet_get_main ();
1021 u32 last_sw_if_index = ~0;
1022 vlib_frame_t *to_frame = 0;
1023 vnet_hw_interface_t *hw = 0;
1024 u32 *from, *to_next = 0;
1025 u32 n_left_from;
1026
1027 from = vlib_frame_vector_args (from_frame);
1028 n_left_from = from_frame->n_vectors;
1029 while (n_left_from > 0)
1030 {
1031 u32 bi0;
1032 vlib_buffer_t *b0;
1033 u32 sw_if_index0;
1034
1035 bi0 = from[0];
1036 from++;
1037 n_left_from--;
1038 b0 = vlib_get_buffer (vm, bi0);
1039 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
1040
1041 if (PREDICT_FALSE ((last_sw_if_index != sw_if_index0) || to_frame == 0))
1042 {
1043 if (to_frame)
1044 {
1045 hw = vnet_get_sup_hw_interface (vnm, last_sw_if_index);
1046 vlib_put_frame_to_node (vm, hw->tx_node_index, to_frame);
1047 }
1048 last_sw_if_index = sw_if_index0;
1049 hw = vnet_get_sup_hw_interface (vnm, sw_if_index0);
1050 to_frame = vlib_get_frame_to_node (vm, hw->tx_node_index);
1051 to_next = vlib_frame_vector_args (to_frame);
1052 }
1053
1054 to_next[0] = bi0;
1055 to_next++;
1056 to_frame->n_vectors++;
1057 }
1058 vlib_put_frame_to_node (vm, hw->tx_node_index, to_frame);
1059 return from_frame->n_vectors;
1060}
1061
1062/* *INDENT-OFF* */
Damjan Mariond770cfc2019-09-02 19:00:33 +02001063VLIB_REGISTER_NODE (interface_tx) = {
Damjan Marion152e21d2016-11-29 14:55:43 +01001064 .function = interface_tx_node_fn,
1065 .name = "interface-tx",
1066 .vector_size = sizeof (u32),
1067 .n_next_nodes = 1,
1068 .next_nodes = {
1069 [0] = "error-drop",
1070 },
1071};
1072
1073VNET_FEATURE_ARC_INIT (interface_output, static) =
1074{
1075 .arc_name = "interface-output",
1076 .start_nodes = VNET_FEATURES (0),
Dave Baracha25def72018-11-26 11:04:45 -05001077 .last_in_arc = "interface-tx",
Damjan Marion152e21d2016-11-29 14:55:43 +01001078 .arc_index_ptr = &vnet_main.interface_main.output_feature_arc_index,
1079};
1080
Pavel Kotucek3a2a1c42016-12-06 10:10:10 +01001081VNET_FEATURE_INIT (span_tx, static) = {
1082 .arc_name = "interface-output",
1083 .node_name = "span-output",
1084 .runs_before = VNET_FEATURES ("interface-tx"),
1085};
1086
Matthew Smith537eeec2018-04-09 11:49:20 -05001087VNET_FEATURE_INIT (ipsec_if_tx, static) = {
1088 .arc_name = "interface-output",
1089 .node_name = "ipsec-if-output",
1090 .runs_before = VNET_FEATURES ("interface-tx"),
1091};
1092
Damjan Marion152e21d2016-11-29 14:55:43 +01001093VNET_FEATURE_INIT (interface_tx, static) = {
1094 .arc_name = "interface-output",
1095 .node_name = "interface-tx",
1096 .runs_before = 0,
1097};
1098/* *INDENT-ON* */
1099
Filip Tehlar62668772019-03-04 03:33:32 -08001100#ifndef CLIB_MARCH_VARIANT
Ed Warnickecb9cada2015-12-08 15:45:58 -07001101clib_error_t *
1102vnet_per_buffer_interface_output_hw_interface_add_del (vnet_main_t * vnm,
1103 u32 hw_if_index,
1104 u32 is_create)
1105{
Dave Barachba868bb2016-08-08 09:51:21 -04001106 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001107 u32 next_index;
1108
John Loe5453d02018-01-23 19:21:34 -05001109 if (hi->output_node_index == 0)
1110 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001111
John Loe5453d02018-01-23 19:21:34 -05001112 next_index = vlib_node_add_next
1113 (vnm->vlib_main, vnet_per_buffer_interface_output_node.index,
1114 hi->output_node_index);
1115 hi->output_node_next_index = next_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001116
1117 return 0;
1118}
1119
Dave Barachba868bb2016-08-08 09:51:21 -04001120VNET_HW_INTERFACE_ADD_DEL_FUNCTION
1121 (vnet_per_buffer_interface_output_hw_interface_add_del);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001122
John Loe5453d02018-01-23 19:21:34 -05001123void
1124vnet_set_interface_output_node (vnet_main_t * vnm,
1125 u32 hw_if_index, u32 node_index)
1126{
1127 ASSERT (node_index);
1128 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
1129 u32 next_index = vlib_node_add_next
1130 (vnm->vlib_main, vnet_per_buffer_interface_output_node.index, node_index);
1131 hi->output_node_next_index = next_index;
1132 hi->output_node_index = node_index;
1133}
Filip Tehlar62668772019-03-04 03:33:32 -08001134#endif /* CLIB_MARCH_VARIANT */
John Loe5453d02018-01-23 19:21:34 -05001135
Dave Barachba868bb2016-08-08 09:51:21 -04001136/*
1137 * fd.io coding-style-patch-verification: ON
1138 *
1139 * Local Variables:
1140 * eval: (c-set-style "gnu")
1141 * End:
1142 */