2 *------------------------------------------------------------------
3 * af_packet.c - linux kernel packet interface
5 * Copyright (c) 2016 Cisco and/or its affiliates.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *------------------------------------------------------------------
20 #include <linux/if_packet.h>
22 #include <vlib/vlib.h>
23 #include <vlib/unix/unix.h>
24 #include <vnet/ip/ip.h>
25 #include <vnet/ethernet/ethernet.h>
26 #include <vnet/interface/rx_queue_funcs.h>
27 #include <vnet/feature/feature.h>
28 #include <vnet/ethernet/packet.h>
30 #include <af_packet/af_packet.h>
31 #include <vnet/devices/virtio/virtio_std.h>
33 #define foreach_af_packet_input_error \
34 _ (PARTIAL_PKT, "partial packet") \
35 _ (TIMEDOUT_BLK, "timed out block") \
36 _ (TOTAL_RECV_BLK, "total received block")
39 #define _(f,s) AF_PACKET_INPUT_ERROR_##f,
40 foreach_af_packet_input_error
42 AF_PACKET_INPUT_N_ERROR,
43 } af_packet_input_error_t;
45 static char *af_packet_input_error_strings[] = {
47 foreach_af_packet_input_error
65 vnet_virtio_net_hdr_t vnet_hdr;
67 } af_packet_input_trace_t;
70 format_af_packet_input_trace (u8 * s, va_list * args)
72 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
73 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
74 af_packet_input_trace_t *t = va_arg (*args, af_packet_input_trace_t *);
75 u32 indent = format_get_indent (s);
77 s = format (s, "af_packet: hw_if_index %d rx-queue %u next-index %d",
78 t->hw_if_index, t->queue_id, t->next_index);
83 s, "\n%Ublock %u:\n%Uaddress %p version %u seq_num %lu pkt_num %u",
84 format_white_space, indent + 2, t->block, format_white_space,
85 indent + 4, t->block_start, t->bd.version, t->bd.hdr.bh1.seq_num,
89 "\n%Utpacket3_hdr:\n%Ustatus 0x%x len %u snaplen %u mac %u net %u"
90 "\n%Usec 0x%x nsec 0x%x vlan %U"
91 #ifdef TP_STATUS_VLAN_TPID_VALID
95 format_white_space, indent + 2, format_white_space, indent + 4,
96 t->tph3.tp_status, t->tph3.tp_len, t->tph3.tp_snaplen, t->tph3.tp_mac,
97 t->tph3.tp_net, format_white_space, indent + 4, t->tph3.tp_sec,
98 t->tph3.tp_nsec, format_ethernet_vlan_tci, t->tph3.hv1.tp_vlan_tci
99 #ifdef TP_STATUS_VLAN_TPID_VALID
101 t->tph3.hv1.tp_vlan_tpid
109 "\n%Utpacket2_hdr:\n%Ustatus 0x%x len %u snaplen %u mac %u net %u"
110 "\n%Usec 0x%x nsec 0x%x vlan %U"
111 #ifdef TP_STATUS_VLAN_TPID_VALID
115 format_white_space, indent + 2, format_white_space, indent + 4,
116 t->tph2.tp_status, t->tph2.tp_len, t->tph2.tp_snaplen, t->tph2.tp_mac,
117 t->tph2.tp_net, format_white_space, indent + 4, t->tph2.tp_sec,
118 t->tph2.tp_nsec, format_ethernet_vlan_tci, t->tph2.tp_vlan_tci
119 #ifdef TP_STATUS_VLAN_TPID_VALID
127 "\n%Uvnet-hdr:\n%Uflags 0x%02x gso_type 0x%02x hdr_len %u"
128 "\n%Ugso_size %u csum_start %u csum_offset %u",
129 format_white_space, indent + 2, format_white_space, indent + 4,
130 t->vnet_hdr.flags, t->vnet_hdr.gso_type, t->vnet_hdr.hdr_len,
131 format_white_space, indent + 4, t->vnet_hdr.gso_size,
132 t->vnet_hdr.csum_start, t->vnet_hdr.csum_offset);
137 buffer_add_to_chain (vlib_buffer_t *b, vlib_buffer_t *first_b,
138 vlib_buffer_t *prev_b, u32 bi)
140 /* update first buffer */
141 first_b->total_length_not_including_first_buffer += b->current_length;
143 /* update previous buffer */
144 prev_b->next_buffer = bi;
145 prev_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
147 /* update current buffer */
151 static_always_inline void
152 fill_gso_offload (vlib_buffer_t *b, u32 gso_size, u8 l4_hdr_sz)
154 b->flags |= VNET_BUFFER_F_GSO;
155 vnet_buffer2 (b)->gso_size = gso_size;
156 vnet_buffer2 (b)->gso_l4_hdr_sz = l4_hdr_sz;
159 static_always_inline void
160 fill_cksum_offload (vlib_buffer_t *b, u8 *l4_hdr_sz, u8 is_ip)
162 vnet_buffer_oflags_t oflags = 0;
169 switch (b->data[0] & 0xf0)
172 ethertype = ETHERNET_TYPE_IP4;
175 ethertype = ETHERNET_TYPE_IP6;
181 ethernet_header_t *eth = (ethernet_header_t *) b->data;
182 ethertype = clib_net_to_host_u16 (eth->type);
183 l2hdr_sz = sizeof (ethernet_header_t);
184 if (ethernet_frame_is_tagged (ethertype))
186 ethernet_vlan_header_t *vlan = (ethernet_vlan_header_t *) (eth + 1);
188 ethertype = clib_net_to_host_u16 (vlan->type);
189 l2hdr_sz += sizeof (*vlan);
190 if (ethertype == ETHERNET_TYPE_VLAN)
193 ethertype = clib_net_to_host_u16 (vlan->type);
194 l2hdr_sz += sizeof (*vlan);
199 vnet_buffer (b)->l2_hdr_offset = 0;
200 vnet_buffer (b)->l3_hdr_offset = l2hdr_sz;
202 if (ethertype == ETHERNET_TYPE_IP4)
204 ip4_header_t *ip4 = (ip4_header_t *) (b->data + l2hdr_sz);
205 vnet_buffer (b)->l4_hdr_offset = l2hdr_sz + ip4_header_bytes (ip4);
206 b->flags |= (VNET_BUFFER_F_IS_IP4 | VNET_BUFFER_F_L2_HDR_OFFSET_VALID |
207 VNET_BUFFER_F_L3_HDR_OFFSET_VALID |
208 VNET_BUFFER_F_L4_HDR_OFFSET_VALID);
210 l4_proto = ip4->protocol;
212 else if (ethertype == ETHERNET_TYPE_IP6)
214 ip6_header_t *ip6 = (ip6_header_t *) (b->data + l2hdr_sz);
215 b->flags |= (VNET_BUFFER_F_IS_IP6 | VNET_BUFFER_F_L2_HDR_OFFSET_VALID |
216 VNET_BUFFER_F_L3_HDR_OFFSET_VALID |
217 VNET_BUFFER_F_L4_HDR_OFFSET_VALID);
218 u16 ip6_hdr_len = sizeof (ip6_header_t);
220 if (ip6_ext_hdr (ip6->protocol))
222 ip6_ext_header_t *p = (void *) (ip6 + 1);
223 ip6_hdr_len += ip6_ext_header_len (p);
224 while (ip6_ext_hdr (p->next_hdr))
226 ip6_hdr_len += ip6_ext_header_len (p);
227 p = ip6_ext_next_header (p);
229 l4_proto = p->next_hdr;
232 l4_proto = ip6->protocol;
233 vnet_buffer (b)->l4_hdr_offset = l2hdr_sz + ip6_hdr_len;
236 if (l4_proto == IP_PROTOCOL_TCP)
238 oflags |= VNET_BUFFER_OFFLOAD_F_TCP_CKSUM;
240 (tcp_header_t *) (b->data + vnet_buffer (b)->l4_hdr_offset);
241 *l4_hdr_sz = tcp_header_bytes (tcp);
243 else if (l4_proto == IP_PROTOCOL_UDP)
245 oflags |= VNET_BUFFER_OFFLOAD_F_UDP_CKSUM;
246 *l4_hdr_sz = sizeof (udp_header_t);
250 vnet_buffer_offload_flags_set (b, oflags);
254 af_packet_v3_device_input_fn (vlib_main_t *vm, vlib_node_runtime_t *node,
255 vlib_frame_t *frame, af_packet_if_t *apif,
256 u16 queue_id, u8 is_cksum_gso_enabled)
258 af_packet_main_t *apm = &af_packet_main;
259 af_packet_queue_t *rx_queue = vec_elt_at_index (apif->rx_queues, queue_id);
263 u32 n_rx_packets = 0;
265 u32 timedout_blk = 0;
268 u32 block = rx_queue->next_rx_block;
269 u32 block_nr = rx_queue->rx_req->req3.tp_block_nr;
271 uword n_trace = vlib_get_trace_count (vm, node);
272 u32 thread_index = vm->thread_index;
273 u32 n_buffer_bytes = vlib_buffer_get_default_data_size (vm);
274 u32 min_bufs = rx_queue->rx_req->req3.tp_frame_size / n_buffer_bytes;
276 u32 rx_frame_offset = 0;
277 block_desc_t *bd = 0;
278 vlib_buffer_t bt = {};
279 u8 is_ip = (apif->mode == AF_PACKET_IF_MODE_IP);
282 next_index = VNET_DEVICE_INPUT_NEXT_IP4_INPUT;
285 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
286 if (PREDICT_FALSE (apif->per_interface_next_index != ~0))
287 next_index = apif->per_interface_next_index;
289 /* redirect if feature path enabled */
290 vnet_feature_start_device_input (apif->sw_if_index, &next_index, &bt);
293 if ((((block_desc_t *) (block_start = rx_queue->rx_ring[block]))
294 ->hdr.bh1.block_status &
295 TP_STATUS_USER) != 0)
298 bd = (block_desc_t *) block_start;
300 if (PREDICT_FALSE (rx_queue->is_rx_pending))
302 num_pkts = rx_queue->num_rx_pkts;
303 rx_frame_offset = rx_queue->rx_frame_offset;
304 rx_queue->is_rx_pending = 0;
308 num_pkts = bd->hdr.bh1.num_pkts;
309 rx_frame_offset = bd->hdr.bh1.offset_to_first_pkt;
312 if (TP_STATUS_BLK_TMO & bd->hdr.bh1.block_status)
316 n_required = clib_max (num_pkts, VLIB_FRAME_SIZE);
317 n_free_bufs = vec_len (apm->rx_buffers[thread_index]);
318 if (PREDICT_FALSE (n_free_bufs < n_required))
320 vec_validate (apm->rx_buffers[thread_index],
321 n_required + n_free_bufs - 1);
322 n_free_bufs += vlib_buffer_alloc (
323 vm, &apm->rx_buffers[thread_index][n_free_bufs], n_required);
324 vec_set_len (apm->rx_buffers[thread_index], n_free_bufs);
327 while (num_pkts && (n_free_bufs >= min_bufs))
329 u32 next0 = next_index;
332 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
334 while (num_pkts && n_left_to_next && (n_free_bufs >= min_bufs))
336 tph = (tpacket3_hdr_t *) (block_start + rx_frame_offset);
339 CLIB_PREFETCH (block_start + rx_frame_offset +
341 2 * CLIB_CACHE_LINE_BYTES, LOAD);
343 vlib_buffer_t *b0 = 0, *first_b0 = 0, *prev_b0 = 0;
344 vnet_virtio_net_hdr_t *vnet_hdr = 0;
345 u32 data_len = tph->tp_snaplen;
347 u32 bi0 = ~0, first_bi0 = ~0;
350 if (is_cksum_gso_enabled)
352 (vnet_virtio_net_hdr_t *) ((u8 *) tph + tph->tp_mac -
353 sizeof (vnet_virtio_net_hdr_t));
355 // save current state and return
356 if (PREDICT_FALSE (((data_len / n_buffer_bytes) + 1) >
357 vec_len (apm->rx_buffers[thread_index])))
359 rx_queue->rx_frame_offset = rx_frame_offset;
360 rx_queue->num_rx_pkts = num_pkts;
361 rx_queue->is_rx_pending = 1;
362 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
368 /* grab free buffer */
369 u32 last_empty_buffer =
370 vec_len (apm->rx_buffers[thread_index]) - 1;
371 bi0 = apm->rx_buffers[thread_index][last_empty_buffer];
372 vec_set_len (apm->rx_buffers[thread_index],
378 data_len > n_buffer_bytes ? n_buffer_bytes : data_len;
380 u32 bytes_copied = 0;
382 b0 = vlib_get_buffer (vm, bi0);
383 b0->current_data = 0;
385 /* Kernel removes VLAN headers, so reconstruct VLAN */
386 if (PREDICT_FALSE (tph->tp_status & TP_STATUS_VLAN_VALID))
388 if (PREDICT_TRUE (offset == 0))
390 clib_memcpy_fast (vlib_buffer_get_current (b0),
391 (u8 *) tph + tph->tp_mac,
392 sizeof (ethernet_header_t));
393 ethernet_header_t *eth =
394 vlib_buffer_get_current (b0);
395 ethernet_vlan_header_t *vlan =
396 (ethernet_vlan_header_t *) (eth + 1);
397 vlan->priority_cfi_and_id =
398 clib_host_to_net_u16 (tph->hv1.tp_vlan_tci);
399 vlan->type = eth->type;
401 clib_host_to_net_u16 (ETHERNET_TYPE_VLAN);
402 vlan_len = sizeof (ethernet_vlan_header_t);
403 bytes_copied = sizeof (ethernet_header_t);
406 clib_memcpy_fast (((u8 *) vlib_buffer_get_current (b0)) +
407 bytes_copied + vlan_len,
408 (u8 *) tph + tph->tp_mac + offset +
410 (bytes_to_copy - bytes_copied));
412 /* fill buffer header */
413 b0->current_length = bytes_to_copy + vlan_len;
417 b0->total_length_not_including_first_buffer = 0;
418 b0->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID;
419 vnet_buffer (b0)->sw_if_index[VLIB_RX] =
421 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~0;
424 if (is_cksum_gso_enabled)
426 if (vnet_hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM)
427 fill_cksum_offload (first_b0, &l4_hdr_sz, is_ip);
428 if (vnet_hdr->gso_type & (VIRTIO_NET_HDR_GSO_TCPV4 |
429 VIRTIO_NET_HDR_GSO_TCPV6))
430 fill_gso_offload (first_b0, vnet_hdr->gso_size,
435 buffer_add_to_chain (b0, first_b0, prev_b0, bi0);
438 offset += bytes_to_copy;
439 data_len -= bytes_to_copy;
442 n_rx_bytes += tph->tp_snaplen;
443 to_next[0] = first_bi0;
447 /* drop partial packets */
448 if (PREDICT_FALSE (tph->tp_len != tph->tp_snaplen))
450 next0 = VNET_DEVICE_INPUT_NEXT_DROP;
452 node->errors[AF_PACKET_INPUT_ERROR_PARTIAL_PKT];
456 if (PREDICT_FALSE (apif->mode == AF_PACKET_IF_MODE_IP))
458 switch (first_b0->data[0] & 0xf0)
461 next0 = VNET_DEVICE_INPUT_NEXT_IP4_INPUT;
464 next0 = VNET_DEVICE_INPUT_NEXT_IP6_INPUT;
467 next0 = VNET_DEVICE_INPUT_NEXT_DROP;
470 if (PREDICT_FALSE (apif->per_interface_next_index != ~0))
471 next0 = apif->per_interface_next_index;
475 next0 = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
476 if (PREDICT_FALSE (apif->per_interface_next_index != ~0))
477 next0 = apif->per_interface_next_index;
478 /* copy feature arc data from template */
479 first_b0->current_config_index = bt.current_config_index;
480 vnet_buffer (first_b0)->feature_arc_index =
481 vnet_buffer (&bt)->feature_arc_index;
486 if (PREDICT_FALSE (n_trace > 0 &&
487 vlib_trace_buffer (vm, node, next0, first_b0,
488 /* follow_chain */ 0)))
490 af_packet_input_trace_t *tr;
491 vlib_set_trace_count (vm, node, --n_trace);
492 tr = vlib_add_trace (vm, node, first_b0, sizeof (*tr));
494 tr->next_index = next0;
495 tr->hw_if_index = apif->hw_if_index;
496 tr->queue_id = queue_id;
498 tr->block_start = bd;
499 tr->pkt_num = bd->hdr.bh1.num_pkts - num_pkts;
500 clib_memcpy_fast (&tr->bd, bd, sizeof (block_desc_t));
501 clib_memcpy_fast (&tr->tph3, tph, sizeof (tpacket3_hdr_t));
502 if (is_cksum_gso_enabled)
503 clib_memcpy_fast (&tr->vnet_hdr, vnet_hdr,
504 sizeof (vnet_virtio_net_hdr_t));
506 clib_memset_u8 (&tr->vnet_hdr, 0,
507 sizeof (vnet_virtio_net_hdr_t));
510 /* enque and take next packet */
511 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
512 n_left_to_next, first_bi0,
517 rx_frame_offset += tph->tp_next_offset;
520 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
523 if (PREDICT_TRUE (num_pkts == 0))
525 bd->hdr.bh1.block_status = TP_STATUS_KERNEL;
526 block = (block + 1) % block_nr;
530 rx_queue->rx_frame_offset = rx_frame_offset;
531 rx_queue->num_rx_pkts = num_pkts;
532 rx_queue->is_rx_pending = 1;
536 rx_queue->next_rx_block = block;
540 if (apm->polling_count == 0)
542 if ((((block_desc_t *) (block_start = rx_queue->rx_ring[block]))
543 ->hdr.bh1.block_status &
544 TP_STATUS_USER) != 0)
545 vlib_node_set_state (vm, node->node_index, VLIB_NODE_STATE_POLLING);
547 vlib_node_set_state (vm, node->node_index, VLIB_NODE_STATE_INTERRUPT);
550 vlib_error_count (vm, node->node_index, AF_PACKET_INPUT_ERROR_TOTAL_RECV_BLK,
552 vlib_error_count (vm, node->node_index, AF_PACKET_INPUT_ERROR_TIMEDOUT_BLK,
555 vlib_increment_combined_counter
556 (vnet_get_main ()->interface_main.combined_sw_if_counters
557 + VNET_INTERFACE_COUNTER_RX,
558 vlib_get_thread_index (), apif->hw_if_index, n_rx_packets, n_rx_bytes);
560 vnet_device_increment_rx_packets (thread_index, n_rx_packets);
565 af_packet_v2_device_input_fn (vlib_main_t *vm, vlib_node_runtime_t *node,
566 vlib_frame_t *frame, af_packet_if_t *apif,
567 u16 queue_id, u8 is_cksum_gso_enabled)
569 af_packet_main_t *apm = &af_packet_main;
570 af_packet_queue_t *rx_queue = vec_elt_at_index (apif->rx_queues, queue_id);
576 u32 n_rx_packets = 0;
579 u32 frame_size = rx_queue->rx_req->req.tp_frame_size;
580 u32 frame_num = rx_queue->rx_req->req.tp_frame_nr;
581 u8 *block_start = rx_queue->rx_ring[block];
582 uword n_trace = vlib_get_trace_count (vm, node);
583 u32 thread_index = vm->thread_index;
584 u32 n_buffer_bytes = vlib_buffer_get_default_data_size (vm);
585 u32 min_bufs = rx_queue->rx_req->req.tp_frame_size / n_buffer_bytes;
586 u8 is_ip = (apif->mode == AF_PACKET_IF_MODE_IP);
587 vlib_buffer_t bt = {};
591 next_index = VNET_DEVICE_INPUT_NEXT_IP4_INPUT;
595 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
596 if (PREDICT_FALSE (apif->per_interface_next_index != ~0))
597 next_index = apif->per_interface_next_index;
599 /* redirect if feature path enabled */
600 vnet_feature_start_device_input (apif->sw_if_index, &next_index, &bt);
603 n_free_bufs = vec_len (apm->rx_buffers[thread_index]);
604 if (PREDICT_FALSE (n_free_bufs < VLIB_FRAME_SIZE))
606 vec_validate (apm->rx_buffers[thread_index],
607 VLIB_FRAME_SIZE + n_free_bufs - 1);
608 n_free_bufs += vlib_buffer_alloc (
609 vm, &apm->rx_buffers[thread_index][n_free_bufs], VLIB_FRAME_SIZE);
610 vec_set_len (apm->rx_buffers[thread_index], n_free_bufs);
613 rx_frame = rx_queue->next_rx_frame;
614 tph = (tpacket2_hdr_t *) (block_start + rx_frame * frame_size);
615 while ((tph->tp_status & TP_STATUS_USER) && (n_free_bufs > min_bufs))
617 vlib_buffer_t *b0 = 0, *first_b0 = 0, *prev_b0 = 0;
618 u32 next0 = next_index;
621 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
622 while ((tph->tp_status & TP_STATUS_USER) && (n_free_bufs > min_bufs) &&
625 vnet_virtio_net_hdr_t *vnet_hdr = 0;
626 u32 data_len = tph->tp_snaplen;
628 u32 bi0 = 0, first_bi0 = 0;
631 if (is_cksum_gso_enabled)
633 (vnet_virtio_net_hdr_t *) ((u8 *) tph + tph->tp_mac -
634 sizeof (vnet_virtio_net_hdr_t));
637 /* grab free buffer */
638 u32 last_empty_buffer =
639 vec_len (apm->rx_buffers[thread_index]) - 1;
640 bi0 = apm->rx_buffers[thread_index][last_empty_buffer];
641 b0 = vlib_get_buffer (vm, bi0);
642 vec_set_len (apm->rx_buffers[thread_index], last_empty_buffer);
647 data_len > n_buffer_bytes ? n_buffer_bytes : data_len;
649 u32 bytes_copied = 0;
650 b0->current_data = 0;
651 /* Kernel removes VLAN headers, so reconstruct VLAN */
652 if (PREDICT_FALSE (tph->tp_status & TP_STATUS_VLAN_VALID))
654 if (PREDICT_TRUE (offset == 0))
656 clib_memcpy_fast (vlib_buffer_get_current (b0),
657 (u8 *) tph + tph->tp_mac,
658 sizeof (ethernet_header_t));
659 ethernet_header_t *eth = vlib_buffer_get_current (b0);
660 ethernet_vlan_header_t *vlan =
661 (ethernet_vlan_header_t *) (eth + 1);
662 vlan->priority_cfi_and_id =
663 clib_host_to_net_u16 (tph->tp_vlan_tci);
664 vlan->type = eth->type;
665 eth->type = clib_host_to_net_u16 (ETHERNET_TYPE_VLAN);
666 vlan_len = sizeof (ethernet_vlan_header_t);
667 bytes_copied = sizeof (ethernet_header_t);
670 clib_memcpy_fast (((u8 *) vlib_buffer_get_current (b0)) +
671 bytes_copied + vlan_len,
672 (u8 *) tph + tph->tp_mac + offset +
674 (bytes_to_copy - bytes_copied));
676 /* fill buffer header */
677 b0->current_length = bytes_to_copy + vlan_len;
681 b0->total_length_not_including_first_buffer = 0;
682 b0->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID;
683 vnet_buffer (b0)->sw_if_index[VLIB_RX] = apif->sw_if_index;
684 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~0;
686 first_b0 = vlib_get_buffer (vm, first_bi0);
688 if (is_cksum_gso_enabled)
690 if (vnet_hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM)
691 fill_cksum_offload (first_b0, &l4_hdr_sz, is_ip);
692 if (vnet_hdr->gso_type & (VIRTIO_NET_HDR_GSO_TCPV4 |
693 VIRTIO_NET_HDR_GSO_TCPV6))
694 fill_gso_offload (first_b0, vnet_hdr->gso_size,
699 buffer_add_to_chain (b0, first_b0, prev_b0, bi0);
702 offset += bytes_to_copy;
703 data_len -= bytes_to_copy;
706 n_rx_bytes += tph->tp_snaplen;
707 to_next[0] = first_bi0;
711 /* drop partial packets */
712 if (PREDICT_FALSE (tph->tp_len != tph->tp_snaplen))
714 next0 = VNET_DEVICE_INPUT_NEXT_DROP;
716 node->errors[AF_PACKET_INPUT_ERROR_PARTIAL_PKT];
720 if (PREDICT_FALSE (is_ip))
722 switch (first_b0->data[0] & 0xf0)
725 next0 = VNET_DEVICE_INPUT_NEXT_IP4_INPUT;
728 next0 = VNET_DEVICE_INPUT_NEXT_IP6_INPUT;
731 next0 = VNET_DEVICE_INPUT_NEXT_DROP;
734 if (PREDICT_FALSE (apif->per_interface_next_index != ~0))
735 next0 = apif->per_interface_next_index;
739 next0 = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
740 if (PREDICT_FALSE (apif->per_interface_next_index != ~0))
741 next0 = apif->per_interface_next_index;
742 /* copy feature arc data from template */
743 first_b0->current_config_index = bt.current_config_index;
744 vnet_buffer (first_b0)->feature_arc_index =
745 vnet_buffer (&bt)->feature_arc_index;
750 if (PREDICT_FALSE (n_trace > 0 &&
751 vlib_trace_buffer (vm, node, next0, first_b0,
752 /* follow_chain */ 0)))
754 af_packet_input_trace_t *tr;
755 vlib_set_trace_count (vm, node, --n_trace);
756 tr = vlib_add_trace (vm, node, first_b0, sizeof (*tr));
758 tr->next_index = next0;
759 tr->hw_if_index = apif->hw_if_index;
760 tr->queue_id = queue_id;
761 clib_memcpy_fast (&tr->tph2, tph, sizeof (struct tpacket2_hdr));
762 if (is_cksum_gso_enabled)
763 clib_memcpy_fast (&tr->vnet_hdr, vnet_hdr,
764 sizeof (vnet_virtio_net_hdr_t));
766 clib_memset_u8 (&tr->vnet_hdr, 0,
767 sizeof (vnet_virtio_net_hdr_t));
770 /* enque and take next packet */
771 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
772 n_left_to_next, first_bi0, next0);
775 tph->tp_status = TP_STATUS_KERNEL;
776 rx_frame = (rx_frame + 1) % frame_num;
777 tph = (struct tpacket2_hdr *) (block_start + rx_frame * frame_size);
780 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
783 rx_queue->next_rx_frame = rx_frame;
785 vlib_increment_combined_counter (
786 vnet_get_main ()->interface_main.combined_sw_if_counters +
787 VNET_INTERFACE_COUNTER_RX,
788 vlib_get_thread_index (), apif->hw_if_index, n_rx_packets, n_rx_bytes);
790 vnet_device_increment_rx_packets (thread_index, n_rx_packets);
795 af_packet_device_input_fn (vlib_main_t *vm, vlib_node_runtime_t *node,
796 vlib_frame_t *frame, af_packet_if_t *apif,
797 u16 queue_id, u8 is_cksum_gso_enabled)
800 if (apif->version == TPACKET_V3)
801 return af_packet_v3_device_input_fn (vm, node, frame, apif, queue_id,
802 is_cksum_gso_enabled);
804 return af_packet_v2_device_input_fn (vm, node, frame, apif, queue_id,
805 is_cksum_gso_enabled);
808 VLIB_NODE_FN (af_packet_input_node) (vlib_main_t * vm,
809 vlib_node_runtime_t * node,
810 vlib_frame_t * frame)
812 u32 n_rx_packets = 0;
813 af_packet_main_t *apm = &af_packet_main;
814 vnet_hw_if_rxq_poll_vector_t *pv;
815 pv = vnet_hw_if_get_rxq_poll_vector (vm, node);
816 for (int i = 0; i < vec_len (pv); i++)
818 af_packet_if_t *apif;
819 apif = vec_elt_at_index (apm->interfaces, pv[i].dev_instance);
820 if (apif->is_admin_up)
822 if (apif->is_cksum_gso_enabled)
823 n_rx_packets += af_packet_device_input_fn (vm, node, frame, apif,
826 n_rx_packets += af_packet_device_input_fn (vm, node, frame, apif,
833 VLIB_REGISTER_NODE (af_packet_input_node) = {
834 .name = "af-packet-input",
835 .flags = VLIB_NODE_FLAG_TRACE_SUPPORTED,
836 .sibling_of = "device-input",
837 .format_trace = format_af_packet_input_trace,
838 .type = VLIB_NODE_TYPE_INPUT,
839 .state = VLIB_NODE_STATE_INTERRUPT,
840 .n_errors = AF_PACKET_INPUT_N_ERROR,
841 .error_strings = af_packet_input_error_strings,
846 * fd.io coding-style-patch-verification: ON
849 * eval: (c-set-style "gnu")