Damjan Marion | c517020 | 2017-10-11 14:15:47 +0200 | [diff] [blame] | 1 | /* |
| 2 | *------------------------------------------------------------------ |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 3 | * af_packet.c - linux kernel packet interface |
| 4 | * |
| 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: |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 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 | *------------------------------------------------------------------ |
| 18 | */ |
| 19 | |
| 20 | #include <linux/if_packet.h> |
| 21 | |
| 22 | #include <vlib/vlib.h> |
| 23 | #include <vlib/unix/unix.h> |
| 24 | #include <vnet/ip/ip.h> |
| 25 | #include <vnet/ethernet/ethernet.h> |
Mohammed Hawari | 85c1943 | 2020-12-18 16:29:45 +0100 | [diff] [blame] | 26 | #include <vnet/interface/rx_queue_funcs.h> |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 27 | #include <vnet/feature/feature.h> |
Jakub Grajciar | 92b0275 | 2017-10-20 13:37:28 +0200 | [diff] [blame] | 28 | #include <vnet/ethernet/packet.h> |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 29 | |
| 30 | #include <vnet/devices/af_packet/af_packet.h> |
Mohsin Kazmi | c1fd17b | 2022-03-22 21:40:04 +0000 | [diff] [blame] | 31 | #include <vnet/devices/virtio/virtio_std.h> |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 32 | |
Chaoyu Jin | a608b60 | 2018-02-28 10:15:53 -0800 | [diff] [blame] | 33 | #define foreach_af_packet_input_error \ |
| 34 | _(PARTIAL_PKT, "partial packet") |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 35 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 36 | typedef enum |
| 37 | { |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 38 | #define _(f,s) AF_PACKET_INPUT_ERROR_##f, |
| 39 | foreach_af_packet_input_error |
| 40 | #undef _ |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 41 | AF_PACKET_INPUT_N_ERROR, |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 42 | } af_packet_input_error_t; |
| 43 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 44 | static char *af_packet_input_error_strings[] = { |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 45 | #define _(n,s) s, |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 46 | foreach_af_packet_input_error |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 47 | #undef _ |
| 48 | }; |
| 49 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 50 | typedef struct |
| 51 | { |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 52 | u32 next_index; |
| 53 | u32 hw_if_index; |
| 54 | int block; |
Mohsin Kazmi | c1fd17b | 2022-03-22 21:40:04 +0000 | [diff] [blame] | 55 | u32 pkt_num; |
Mohsin Kazmi | 219cbcb | 2022-03-18 16:58:31 +0000 | [diff] [blame] | 56 | void *block_start; |
| 57 | block_desc_t bd; |
| 58 | tpacket3_hdr_t tph; |
Mohsin Kazmi | c1fd17b | 2022-03-22 21:40:04 +0000 | [diff] [blame] | 59 | vnet_virtio_net_hdr_t vnet_hdr; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 60 | } af_packet_input_trace_t; |
| 61 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 62 | static u8 * |
| 63 | format_af_packet_input_trace (u8 * s, va_list * args) |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 64 | { |
| 65 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 66 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 67 | af_packet_input_trace_t *t = va_arg (*args, af_packet_input_trace_t *); |
Christophe Fontaine | d3c008d | 2017-10-02 18:10:54 +0200 | [diff] [blame] | 68 | u32 indent = format_get_indent (s); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 69 | |
| 70 | s = format (s, "af_packet: hw_if_index %d next-index %d", |
| 71 | t->hw_if_index, t->next_index); |
| 72 | |
Mohsin Kazmi | c1fd17b | 2022-03-22 21:40:04 +0000 | [diff] [blame] | 73 | s = format ( |
| 74 | s, "\n%Ublock %u:\n%Uaddress %p version %u seq_num %lu pkt_num %u", |
| 75 | format_white_space, indent + 2, t->block, format_white_space, indent + 4, |
| 76 | t->block_start, t->bd.version, t->bd.hdr.bh1.seq_num, t->pkt_num); |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 77 | s = |
| 78 | format (s, |
Mohsin Kazmi | 219cbcb | 2022-03-18 16:58:31 +0000 | [diff] [blame] | 79 | "\n%Utpacket3_hdr:\n%Ustatus 0x%x len %u snaplen %u mac %u net %u" |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 80 | "\n%Usec 0x%x nsec 0x%x vlan %U" |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 81 | #ifdef TP_STATUS_VLAN_TPID_VALID |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 82 | " vlan_tpid %u" |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 83 | #endif |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 84 | , |
Mohsin Kazmi | 219cbcb | 2022-03-18 16:58:31 +0000 | [diff] [blame] | 85 | format_white_space, indent + 2, format_white_space, indent + 4, |
| 86 | t->tph.tp_status, t->tph.tp_len, t->tph.tp_snaplen, t->tph.tp_mac, |
| 87 | t->tph.tp_net, format_white_space, indent + 4, t->tph.tp_sec, |
| 88 | t->tph.tp_nsec, format_ethernet_vlan_tci, t->tph.hv1.tp_vlan_tci |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 89 | #ifdef TP_STATUS_VLAN_TPID_VALID |
Mohsin Kazmi | 219cbcb | 2022-03-18 16:58:31 +0000 | [diff] [blame] | 90 | , |
| 91 | t->tph.hv1.tp_vlan_tpid |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 92 | #endif |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 93 | ); |
Mohsin Kazmi | c1fd17b | 2022-03-22 21:40:04 +0000 | [diff] [blame] | 94 | |
| 95 | s = format (s, |
| 96 | "\n%Uvnet-hdr:\n%Uflags 0x%02x gso_type 0x%02x hdr_len %u" |
| 97 | "\n%Ugso_size %u csum_start %u csum_offset %u", |
| 98 | format_white_space, indent + 2, format_white_space, indent + 4, |
| 99 | t->vnet_hdr.flags, t->vnet_hdr.gso_type, t->vnet_hdr.hdr_len, |
| 100 | format_white_space, indent + 4, t->vnet_hdr.gso_size, |
| 101 | t->vnet_hdr.csum_start, t->vnet_hdr.csum_offset); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 102 | return s; |
| 103 | } |
| 104 | |
| 105 | always_inline void |
Mohsin Kazmi | c1fd17b | 2022-03-22 21:40:04 +0000 | [diff] [blame] | 106 | buffer_add_to_chain (vlib_buffer_t *b, vlib_buffer_t *first_b, |
| 107 | vlib_buffer_t *prev_b, u32 bi) |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 108 | { |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 109 | /* update first buffer */ |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 110 | first_b->total_length_not_including_first_buffer += b->current_length; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 111 | |
| 112 | /* update previous buffer */ |
| 113 | prev_b->next_buffer = bi; |
| 114 | prev_b->flags |= VLIB_BUFFER_NEXT_PRESENT; |
| 115 | |
| 116 | /* update current buffer */ |
Mohsin Kazmi | c1fd17b | 2022-03-22 21:40:04 +0000 | [diff] [blame] | 117 | b->next_buffer = ~0; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 118 | } |
| 119 | |
Jakub Grajciar | 92b0275 | 2017-10-20 13:37:28 +0200 | [diff] [blame] | 120 | static_always_inline void |
Mohsin Kazmi | c1fd17b | 2022-03-22 21:40:04 +0000 | [diff] [blame] | 121 | fill_gso_offload (vlib_buffer_t *b, u32 gso_size, u8 l4_hdr_sz) |
Nathan Skrzypczak | ffc6bdc | 2021-02-01 17:13:59 +0100 | [diff] [blame] | 122 | { |
| 123 | b->flags |= VNET_BUFFER_F_GSO; |
| 124 | vnet_buffer2 (b)->gso_size = gso_size; |
| 125 | vnet_buffer2 (b)->gso_l4_hdr_sz = l4_hdr_sz; |
| 126 | } |
| 127 | |
| 128 | static_always_inline void |
Mohsin Kazmi | c1fd17b | 2022-03-22 21:40:04 +0000 | [diff] [blame] | 129 | fill_cksum_offload (vlib_buffer_t *b, u8 *l4_hdr_sz, u8 is_ip) |
Jakub Grajciar | 92b0275 | 2017-10-20 13:37:28 +0200 | [diff] [blame] | 130 | { |
Mohsin Kazmi | 36f7a6a | 2021-05-05 14:26:38 +0200 | [diff] [blame] | 131 | vnet_buffer_oflags_t oflags = 0; |
Mohsin Kazmi | c1fd17b | 2022-03-22 21:40:04 +0000 | [diff] [blame] | 132 | u16 l2hdr_sz = 0; |
| 133 | u16 ethertype = 0; |
| 134 | u8 l4_proto = 0; |
| 135 | |
| 136 | if (is_ip) |
Jakub Grajciar | 92b0275 | 2017-10-20 13:37:28 +0200 | [diff] [blame] | 137 | { |
Mohsin Kazmi | c1fd17b | 2022-03-22 21:40:04 +0000 | [diff] [blame] | 138 | switch (b->data[0] & 0xf0) |
Jakub Grajciar | 92b0275 | 2017-10-20 13:37:28 +0200 | [diff] [blame] | 139 | { |
Mohsin Kazmi | c1fd17b | 2022-03-22 21:40:04 +0000 | [diff] [blame] | 140 | case 0x40: |
| 141 | ethertype = ETHERNET_TYPE_IP4; |
| 142 | break; |
| 143 | case 0x60: |
| 144 | ethertype = ETHERNET_TYPE_IP6; |
| 145 | break; |
Jakub Grajciar | 92b0275 | 2017-10-20 13:37:28 +0200 | [diff] [blame] | 146 | } |
Jakub Grajciar | 92b0275 | 2017-10-20 13:37:28 +0200 | [diff] [blame] | 147 | } |
Mohsin Kazmi | c1fd17b | 2022-03-22 21:40:04 +0000 | [diff] [blame] | 148 | else |
Jakub Grajciar | 92b0275 | 2017-10-20 13:37:28 +0200 | [diff] [blame] | 149 | { |
Mohsin Kazmi | c1fd17b | 2022-03-22 21:40:04 +0000 | [diff] [blame] | 150 | ethernet_header_t *eth = vlib_buffer_get_current (b); |
| 151 | ethertype = clib_net_to_host_u16 (eth->type); |
| 152 | l2hdr_sz = sizeof (ethernet_header_t); |
| 153 | if (ethernet_frame_is_tagged (ethertype)) |
| 154 | { |
| 155 | ethernet_vlan_header_t *vlan = (ethernet_vlan_header_t *) (eth + 1); |
| 156 | |
| 157 | ethertype = clib_net_to_host_u16 (vlan->type); |
| 158 | l2hdr_sz += sizeof (*vlan); |
| 159 | if (ethertype == ETHERNET_TYPE_VLAN) |
| 160 | { |
| 161 | vlan++; |
| 162 | ethertype = clib_net_to_host_u16 (vlan->type); |
| 163 | l2hdr_sz += sizeof (*vlan); |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | vnet_buffer (b)->l2_hdr_offset = 0; |
| 169 | vnet_buffer (b)->l3_hdr_offset = l2hdr_sz; |
| 170 | |
| 171 | if (ethertype == ETHERNET_TYPE_IP4) |
| 172 | { |
| 173 | ip4_header_t *ip4 = (vlib_buffer_get_current (b) + l2hdr_sz); |
| 174 | vnet_buffer (b)->l4_hdr_offset = l2hdr_sz + ip4_header_bytes (ip4); |
| 175 | b->flags |= (VNET_BUFFER_F_IS_IP4 | VNET_BUFFER_F_L2_HDR_OFFSET_VALID | |
| 176 | VNET_BUFFER_F_L3_HDR_OFFSET_VALID | |
| 177 | VNET_BUFFER_F_L4_HDR_OFFSET_VALID); |
| 178 | |
| 179 | l4_proto = ip4->protocol; |
| 180 | } |
| 181 | else if (ethertype == ETHERNET_TYPE_IP6) |
| 182 | { |
| 183 | ip6_header_t *ip6 = (vlib_buffer_get_current (b) + l2hdr_sz); |
| 184 | b->flags |= (VNET_BUFFER_F_IS_IP6 | VNET_BUFFER_F_L2_HDR_OFFSET_VALID | |
| 185 | VNET_BUFFER_F_L3_HDR_OFFSET_VALID | |
| 186 | VNET_BUFFER_F_L4_HDR_OFFSET_VALID); |
Jakub Grajciar | 92b0275 | 2017-10-20 13:37:28 +0200 | [diff] [blame] | 187 | u16 ip6_hdr_len = sizeof (ip6_header_t); |
Mohsin Kazmi | c1fd17b | 2022-03-22 21:40:04 +0000 | [diff] [blame] | 188 | |
Jakub Grajciar | 92b0275 | 2017-10-20 13:37:28 +0200 | [diff] [blame] | 189 | if (ip6_ext_hdr (ip6->protocol)) |
| 190 | { |
| 191 | ip6_ext_header_t *p = (void *) (ip6 + 1); |
| 192 | ip6_hdr_len += ip6_ext_header_len (p); |
| 193 | while (ip6_ext_hdr (p->next_hdr)) |
| 194 | { |
| 195 | ip6_hdr_len += ip6_ext_header_len (p); |
| 196 | p = ip6_ext_next_header (p); |
| 197 | } |
Mohsin Kazmi | c1fd17b | 2022-03-22 21:40:04 +0000 | [diff] [blame] | 198 | l4_proto = p->next_hdr; |
Jakub Grajciar | 92b0275 | 2017-10-20 13:37:28 +0200 | [diff] [blame] | 199 | } |
Mohsin Kazmi | c1fd17b | 2022-03-22 21:40:04 +0000 | [diff] [blame] | 200 | else |
| 201 | l4_proto = ip6->protocol; |
| 202 | vnet_buffer (b)->l4_hdr_offset = l2hdr_sz + ip6_hdr_len; |
Jakub Grajciar | 92b0275 | 2017-10-20 13:37:28 +0200 | [diff] [blame] | 203 | } |
Mohsin Kazmi | c1fd17b | 2022-03-22 21:40:04 +0000 | [diff] [blame] | 204 | |
| 205 | if (l4_proto == IP_PROTOCOL_TCP) |
| 206 | { |
| 207 | oflags |= VNET_BUFFER_OFFLOAD_F_TCP_CKSUM; |
| 208 | tcp_header_t *tcp = (tcp_header_t *) (vlib_buffer_get_current (b) + |
| 209 | vnet_buffer (b)->l4_hdr_offset); |
| 210 | *l4_hdr_sz = tcp_header_bytes (tcp); |
| 211 | } |
| 212 | else if (l4_proto == IP_PROTOCOL_UDP) |
| 213 | { |
| 214 | oflags |= VNET_BUFFER_OFFLOAD_F_UDP_CKSUM; |
| 215 | *l4_hdr_sz = sizeof (udp_header_t); |
| 216 | } |
| 217 | |
| 218 | if (oflags) |
| 219 | vnet_buffer_offload_flags_set (b, oflags); |
Jakub Grajciar | 92b0275 | 2017-10-20 13:37:28 +0200 | [diff] [blame] | 220 | } |
| 221 | |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 222 | always_inline uword |
Mohsin Kazmi | c1fd17b | 2022-03-22 21:40:04 +0000 | [diff] [blame] | 223 | af_packet_device_input_fn (vlib_main_t *vm, vlib_node_runtime_t *node, |
| 224 | vlib_frame_t *frame, af_packet_if_t *apif, |
| 225 | u8 is_cksum_gso_enabled) |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 226 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 227 | af_packet_main_t *apm = &af_packet_main; |
Mohsin Kazmi | 219cbcb | 2022-03-18 16:58:31 +0000 | [diff] [blame] | 228 | tpacket3_hdr_t *tph; |
Mohsin Kazmi | cae84fa | 2021-10-08 15:10:49 +0000 | [diff] [blame] | 229 | u32 next_index; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 230 | u32 n_free_bufs; |
| 231 | u32 n_rx_packets = 0; |
| 232 | u32 n_rx_bytes = 0; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 233 | u32 *to_next = 0; |
Mohsin Kazmi | 219cbcb | 2022-03-18 16:58:31 +0000 | [diff] [blame] | 234 | u32 block = apif->next_rx_block; |
| 235 | u32 block_nr = apif->rx_req->tp_block_nr; |
| 236 | u8 *block_start = 0; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 237 | uword n_trace = vlib_get_trace_count (vm, node); |
Damjan Marion | 067cd62 | 2018-07-11 12:47:43 +0200 | [diff] [blame] | 238 | u32 thread_index = vm->thread_index; |
Damjan Marion | 8934a04 | 2019-02-09 23:29:26 +0100 | [diff] [blame] | 239 | u32 n_buffer_bytes = vlib_buffer_get_default_data_size (vm); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 240 | u32 min_bufs = apif->rx_req->tp_frame_size / n_buffer_bytes; |
Mohsin Kazmi | 219cbcb | 2022-03-18 16:58:31 +0000 | [diff] [blame] | 241 | u32 num_pkts = 0; |
| 242 | u32 rx_frame_offset = 0; |
| 243 | block_desc_t *bd = 0; |
Mohsin Kazmi | cae84fa | 2021-10-08 15:10:49 +0000 | [diff] [blame] | 244 | vlib_buffer_t bt; |
Mohsin Kazmi | c1fd17b | 2022-03-22 21:40:04 +0000 | [diff] [blame] | 245 | u8 is_ip = (apif->mode == AF_PACKET_IF_MODE_IP); |
Mohsin Kazmi | cae84fa | 2021-10-08 15:10:49 +0000 | [diff] [blame] | 246 | |
Mohsin Kazmi | c1fd17b | 2022-03-22 21:40:04 +0000 | [diff] [blame] | 247 | if (is_ip) |
| 248 | next_index = VNET_DEVICE_INPUT_NEXT_IP4_INPUT; |
Mohsin Kazmi | cae84fa | 2021-10-08 15:10:49 +0000 | [diff] [blame] | 249 | else |
| 250 | { |
| 251 | next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT; |
| 252 | if (PREDICT_FALSE (apif->per_interface_next_index != ~0)) |
| 253 | next_index = apif->per_interface_next_index; |
| 254 | |
| 255 | /* redirect if feature path enabled */ |
| 256 | vnet_feature_start_device_input_x1 (apif->sw_if_index, &next_index, &bt); |
| 257 | } |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 258 | |
Mohsin Kazmi | 89d74bd | 2022-03-25 00:33:27 +0000 | [diff] [blame] | 259 | if ((((block_desc_t *) (block_start = apif->rx_ring[block])) |
| 260 | ->hdr.bh1.block_status & |
| 261 | TP_STATUS_USER) != 0) |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 262 | { |
Mohsin Kazmi | 219cbcb | 2022-03-18 16:58:31 +0000 | [diff] [blame] | 263 | u32 n_required = 0; |
Mohsin Kazmi | 89d74bd | 2022-03-25 00:33:27 +0000 | [diff] [blame] | 264 | bd = (block_desc_t *) block_start; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 265 | |
Mohsin Kazmi | 89d74bd | 2022-03-25 00:33:27 +0000 | [diff] [blame] | 266 | if (PREDICT_FALSE (apif->ss.is_save)) |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 267 | { |
Mohsin Kazmi | 89d74bd | 2022-03-25 00:33:27 +0000 | [diff] [blame] | 268 | num_pkts = apif->ss.num_pkts; |
| 269 | rx_frame_offset = apif->ss.rx_frame_offset; |
| 270 | apif->ss.is_save = 0; |
| 271 | } |
| 272 | else |
| 273 | { |
Mohsin Kazmi | 219cbcb | 2022-03-18 16:58:31 +0000 | [diff] [blame] | 274 | num_pkts = bd->hdr.bh1.num_pkts; |
| 275 | rx_frame_offset = sizeof (block_desc_t); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 276 | } |
| 277 | |
Mohsin Kazmi | 219cbcb | 2022-03-18 16:58:31 +0000 | [diff] [blame] | 278 | n_required = clib_max (num_pkts, VLIB_FRAME_SIZE); |
| 279 | n_free_bufs = vec_len (apm->rx_buffers[thread_index]); |
| 280 | if (PREDICT_FALSE (n_free_bufs < n_required)) |
| 281 | { |
| 282 | vec_validate (apm->rx_buffers[thread_index], |
| 283 | n_required + n_free_bufs - 1); |
| 284 | n_free_bufs += vlib_buffer_alloc ( |
| 285 | vm, &apm->rx_buffers[thread_index][n_free_bufs], n_required); |
| 286 | _vec_len (apm->rx_buffers[thread_index]) = n_free_bufs; |
| 287 | } |
| 288 | |
Mohsin Kazmi | 89d74bd | 2022-03-25 00:33:27 +0000 | [diff] [blame] | 289 | while (num_pkts && (n_free_bufs >= min_bufs)) |
Mohsin Kazmi | 219cbcb | 2022-03-18 16:58:31 +0000 | [diff] [blame] | 290 | { |
Mohsin Kazmi | 219cbcb | 2022-03-18 16:58:31 +0000 | [diff] [blame] | 291 | u32 next0 = next_index; |
| 292 | u32 n_left_to_next; |
Mohsin Kazmi | c1fd17b | 2022-03-22 21:40:04 +0000 | [diff] [blame] | 293 | |
Mohsin Kazmi | 219cbcb | 2022-03-18 16:58:31 +0000 | [diff] [blame] | 294 | vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); |
Mohsin Kazmi | c1fd17b | 2022-03-22 21:40:04 +0000 | [diff] [blame] | 295 | |
Mohsin Kazmi | 89d74bd | 2022-03-25 00:33:27 +0000 | [diff] [blame] | 296 | while (num_pkts && n_left_to_next && (n_free_bufs >= min_bufs)) |
Mohsin Kazmi | 219cbcb | 2022-03-18 16:58:31 +0000 | [diff] [blame] | 297 | { |
| 298 | tph = (tpacket3_hdr_t *) (block_start + rx_frame_offset); |
| 299 | |
| 300 | if (num_pkts > 1) |
| 301 | CLIB_PREFETCH (block_start + rx_frame_offset + |
| 302 | tph->tp_next_offset, |
| 303 | 2 * CLIB_CACHE_LINE_BYTES, LOAD); |
Mohsin Kazmi | c1fd17b | 2022-03-22 21:40:04 +0000 | [diff] [blame] | 304 | |
| 305 | vlib_buffer_t *b0 = 0, *first_b0 = 0, *prev_b0 = 0; |
| 306 | vnet_virtio_net_hdr_t *vnet_hdr = 0; |
Mohsin Kazmi | 219cbcb | 2022-03-18 16:58:31 +0000 | [diff] [blame] | 307 | u32 data_len = tph->tp_snaplen; |
| 308 | u32 offset = 0; |
Mohsin Kazmi | c1fd17b | 2022-03-22 21:40:04 +0000 | [diff] [blame] | 309 | u32 bi0 = ~0, first_bi0 = ~0; |
Mohsin Kazmi | 219cbcb | 2022-03-18 16:58:31 +0000 | [diff] [blame] | 310 | u8 l4_hdr_sz = 0; |
| 311 | |
Mohsin Kazmi | c1fd17b | 2022-03-22 21:40:04 +0000 | [diff] [blame] | 312 | if (is_cksum_gso_enabled) |
| 313 | vnet_hdr = |
| 314 | (vnet_virtio_net_hdr_t *) ((u8 *) tph + tph->tp_mac - |
| 315 | sizeof (vnet_virtio_net_hdr_t)); |
| 316 | |
Mohsin Kazmi | 89d74bd | 2022-03-25 00:33:27 +0000 | [diff] [blame] | 317 | // save current state and return |
Mohsin Kazmi | c1fd17b | 2022-03-22 21:40:04 +0000 | [diff] [blame] | 318 | if (PREDICT_FALSE (((data_len / n_buffer_bytes) + 1) > |
| 319 | vec_len (apm->rx_buffers[thread_index]))) |
| 320 | { |
Mohsin Kazmi | 89d74bd | 2022-03-25 00:33:27 +0000 | [diff] [blame] | 321 | apif->ss.rx_frame_offset = rx_frame_offset; |
| 322 | apif->ss.num_pkts = num_pkts; |
| 323 | apif->ss.is_save = 1; |
| 324 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 325 | goto done; |
Mohsin Kazmi | c1fd17b | 2022-03-22 21:40:04 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Mohsin Kazmi | 219cbcb | 2022-03-18 16:58:31 +0000 | [diff] [blame] | 328 | while (data_len) |
| 329 | { |
| 330 | /* grab free buffer */ |
| 331 | u32 last_empty_buffer = |
| 332 | vec_len (apm->rx_buffers[thread_index]) - 1; |
Mohsin Kazmi | 219cbcb | 2022-03-18 16:58:31 +0000 | [diff] [blame] | 333 | bi0 = apm->rx_buffers[thread_index][last_empty_buffer]; |
Mohsin Kazmi | 219cbcb | 2022-03-18 16:58:31 +0000 | [diff] [blame] | 334 | _vec_len (apm->rx_buffers[thread_index]) = last_empty_buffer; |
| 335 | n_free_bufs--; |
| 336 | |
| 337 | /* copy data */ |
| 338 | u32 bytes_to_copy = |
| 339 | data_len > n_buffer_bytes ? n_buffer_bytes : data_len; |
| 340 | u32 vlan_len = 0; |
| 341 | u32 bytes_copied = 0; |
Mohsin Kazmi | c1fd17b | 2022-03-22 21:40:04 +0000 | [diff] [blame] | 342 | |
| 343 | b0 = vlib_get_buffer (vm, bi0); |
Mohsin Kazmi | 219cbcb | 2022-03-18 16:58:31 +0000 | [diff] [blame] | 344 | b0->current_data = 0; |
Mohsin Kazmi | c1fd17b | 2022-03-22 21:40:04 +0000 | [diff] [blame] | 345 | |
Mohsin Kazmi | 219cbcb | 2022-03-18 16:58:31 +0000 | [diff] [blame] | 346 | /* Kernel removes VLAN headers, so reconstruct VLAN */ |
| 347 | if (PREDICT_FALSE (tph->tp_status & TP_STATUS_VLAN_VALID)) |
| 348 | { |
| 349 | if (PREDICT_TRUE (offset == 0)) |
| 350 | { |
| 351 | clib_memcpy_fast (vlib_buffer_get_current (b0), |
| 352 | (u8 *) tph + tph->tp_mac, |
| 353 | sizeof (ethernet_header_t)); |
| 354 | ethernet_header_t *eth = |
| 355 | vlib_buffer_get_current (b0); |
| 356 | ethernet_vlan_header_t *vlan = |
| 357 | (ethernet_vlan_header_t *) (eth + 1); |
| 358 | vlan->priority_cfi_and_id = |
| 359 | clib_host_to_net_u16 (tph->hv1.tp_vlan_tci); |
| 360 | vlan->type = eth->type; |
| 361 | eth->type = |
| 362 | clib_host_to_net_u16 (ETHERNET_TYPE_VLAN); |
| 363 | vlan_len = sizeof (ethernet_vlan_header_t); |
| 364 | bytes_copied = sizeof (ethernet_header_t); |
| 365 | } |
| 366 | } |
| 367 | clib_memcpy_fast (((u8 *) vlib_buffer_get_current (b0)) + |
| 368 | bytes_copied + vlan_len, |
| 369 | (u8 *) tph + tph->tp_mac + offset + |
| 370 | bytes_copied, |
| 371 | (bytes_to_copy - bytes_copied)); |
| 372 | |
| 373 | /* fill buffer header */ |
| 374 | b0->current_length = bytes_to_copy + vlan_len; |
| 375 | |
| 376 | if (offset == 0) |
| 377 | { |
| 378 | b0->total_length_not_including_first_buffer = 0; |
| 379 | b0->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID; |
| 380 | vnet_buffer (b0)->sw_if_index[VLIB_RX] = |
| 381 | apif->sw_if_index; |
| 382 | vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~0; |
Mohsin Kazmi | c1fd17b | 2022-03-22 21:40:04 +0000 | [diff] [blame] | 383 | first_b0 = b0; |
Mohsin Kazmi | 219cbcb | 2022-03-18 16:58:31 +0000 | [diff] [blame] | 384 | first_bi0 = bi0; |
Mohsin Kazmi | c1fd17b | 2022-03-22 21:40:04 +0000 | [diff] [blame] | 385 | if (is_cksum_gso_enabled) |
| 386 | { |
| 387 | if (vnet_hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) |
| 388 | fill_cksum_offload (first_b0, &l4_hdr_sz, is_ip); |
| 389 | if (vnet_hdr->gso_type & (VIRTIO_NET_HDR_GSO_TCPV4 | |
| 390 | VIRTIO_NET_HDR_GSO_TCPV6)) |
| 391 | fill_gso_offload (first_b0, vnet_hdr->gso_size, |
| 392 | l4_hdr_sz); |
| 393 | } |
Mohsin Kazmi | 219cbcb | 2022-03-18 16:58:31 +0000 | [diff] [blame] | 394 | } |
| 395 | else |
Mohsin Kazmi | c1fd17b | 2022-03-22 21:40:04 +0000 | [diff] [blame] | 396 | buffer_add_to_chain (b0, first_b0, prev_b0, bi0); |
Mohsin Kazmi | 219cbcb | 2022-03-18 16:58:31 +0000 | [diff] [blame] | 397 | |
Mohsin Kazmi | c1fd17b | 2022-03-22 21:40:04 +0000 | [diff] [blame] | 398 | prev_b0 = b0; |
Mohsin Kazmi | 219cbcb | 2022-03-18 16:58:31 +0000 | [diff] [blame] | 399 | offset += bytes_to_copy; |
| 400 | data_len -= bytes_to_copy; |
| 401 | } |
| 402 | n_rx_packets++; |
| 403 | n_rx_bytes += tph->tp_snaplen; |
| 404 | to_next[0] = first_bi0; |
| 405 | to_next += 1; |
| 406 | n_left_to_next--; |
| 407 | |
| 408 | /* drop partial packets */ |
| 409 | if (PREDICT_FALSE (tph->tp_len != tph->tp_snaplen)) |
| 410 | { |
| 411 | next0 = VNET_DEVICE_INPUT_NEXT_DROP; |
| 412 | first_b0->error = |
| 413 | node->errors[AF_PACKET_INPUT_ERROR_PARTIAL_PKT]; |
| 414 | } |
| 415 | else |
| 416 | { |
| 417 | if (PREDICT_FALSE (apif->mode == AF_PACKET_IF_MODE_IP)) |
| 418 | { |
| 419 | switch (first_b0->data[0] & 0xf0) |
| 420 | { |
| 421 | case 0x40: |
| 422 | next0 = VNET_DEVICE_INPUT_NEXT_IP4_INPUT; |
| 423 | break; |
| 424 | case 0x60: |
| 425 | next0 = VNET_DEVICE_INPUT_NEXT_IP6_INPUT; |
| 426 | break; |
| 427 | default: |
| 428 | next0 = VNET_DEVICE_INPUT_NEXT_DROP; |
| 429 | break; |
| 430 | } |
| 431 | if (PREDICT_FALSE (apif->per_interface_next_index != ~0)) |
| 432 | next0 = apif->per_interface_next_index; |
| 433 | } |
| 434 | else |
| 435 | { |
| 436 | /* copy feature arc data from template */ |
| 437 | first_b0->current_config_index = bt.current_config_index; |
| 438 | vnet_buffer (first_b0)->feature_arc_index = |
| 439 | vnet_buffer (&bt)->feature_arc_index; |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | /* trace */ |
| 444 | if (PREDICT_FALSE (n_trace > 0 && |
| 445 | vlib_trace_buffer (vm, node, next0, first_b0, |
| 446 | /* follow_chain */ 0))) |
| 447 | { |
| 448 | af_packet_input_trace_t *tr; |
| 449 | vlib_set_trace_count (vm, node, --n_trace); |
| 450 | tr = vlib_add_trace (vm, node, first_b0, sizeof (*tr)); |
| 451 | tr->next_index = next0; |
| 452 | tr->hw_if_index = apif->hw_if_index; |
| 453 | tr->block = block; |
| 454 | tr->block_start = bd; |
Mohsin Kazmi | c1fd17b | 2022-03-22 21:40:04 +0000 | [diff] [blame] | 455 | tr->pkt_num = bd->hdr.bh1.num_pkts - num_pkts; |
Mohsin Kazmi | 219cbcb | 2022-03-18 16:58:31 +0000 | [diff] [blame] | 456 | clib_memcpy_fast (&tr->bd, bd, sizeof (block_desc_t)); |
| 457 | clib_memcpy_fast (&tr->tph, tph, sizeof (tpacket3_hdr_t)); |
Mohsin Kazmi | c1fd17b | 2022-03-22 21:40:04 +0000 | [diff] [blame] | 458 | if (is_cksum_gso_enabled) |
| 459 | clib_memcpy_fast (&tr->vnet_hdr, vnet_hdr, |
| 460 | sizeof (vnet_virtio_net_hdr_t)); |
| 461 | else |
| 462 | clib_memset_u8 (&tr->vnet_hdr, 0, |
| 463 | sizeof (vnet_virtio_net_hdr_t)); |
Mohsin Kazmi | 219cbcb | 2022-03-18 16:58:31 +0000 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | /* enque and take next packet */ |
| 467 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, |
| 468 | n_left_to_next, first_bi0, |
| 469 | next0); |
| 470 | |
| 471 | /* next packet */ |
| 472 | num_pkts--; |
| 473 | rx_frame_offset += tph->tp_next_offset; |
| 474 | } |
| 475 | |
| 476 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 477 | } |
| 478 | |
| 479 | if (PREDICT_TRUE (num_pkts == 0)) |
| 480 | { |
| 481 | bd->hdr.bh1.block_status = TP_STATUS_KERNEL; |
| 482 | block = (block + 1) % block_nr; |
| 483 | } |
Mohsin Kazmi | 89d74bd | 2022-03-25 00:33:27 +0000 | [diff] [blame] | 484 | else |
| 485 | { |
| 486 | apif->ss.rx_frame_offset = rx_frame_offset; |
| 487 | apif->ss.num_pkts = num_pkts; |
| 488 | apif->ss.is_save = 1; |
| 489 | } |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 490 | } |
| 491 | |
Mohsin Kazmi | 219cbcb | 2022-03-18 16:58:31 +0000 | [diff] [blame] | 492 | apif->next_rx_block = block; |
Mohsin Kazmi | 89d74bd | 2022-03-25 00:33:27 +0000 | [diff] [blame] | 493 | |
| 494 | done: |
| 495 | |
| 496 | if ((((block_desc_t *) (block_start = apif->rx_ring[block])) |
| 497 | ->hdr.bh1.block_status & |
| 498 | TP_STATUS_USER) != 0) |
| 499 | vlib_node_set_state (vm, node->node_index, VLIB_NODE_STATE_POLLING); |
| 500 | else |
| 501 | vlib_node_set_state (vm, node->node_index, VLIB_NODE_STATE_INTERRUPT); |
| 502 | |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 503 | vlib_increment_combined_counter |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 504 | (vnet_get_main ()->interface_main.combined_sw_if_counters |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 505 | + VNET_INTERFACE_COUNTER_RX, |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 506 | vlib_get_thread_index (), apif->hw_if_index, n_rx_packets, n_rx_bytes); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 507 | |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 508 | vnet_device_increment_rx_packets (thread_index, n_rx_packets); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 509 | return n_rx_packets; |
| 510 | } |
| 511 | |
Filip Tehlar | 608996d | 2019-03-04 03:03:13 -0800 | [diff] [blame] | 512 | VLIB_NODE_FN (af_packet_input_node) (vlib_main_t * vm, |
| 513 | vlib_node_runtime_t * node, |
| 514 | vlib_frame_t * frame) |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 515 | { |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 516 | u32 n_rx_packets = 0; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 517 | af_packet_main_t *apm = &af_packet_main; |
Mohammed Hawari | 85c1943 | 2020-12-18 16:29:45 +0100 | [diff] [blame] | 518 | vnet_hw_if_rxq_poll_vector_t *pv; |
| 519 | pv = vnet_hw_if_get_rxq_poll_vector (vm, node); |
| 520 | for (int i = 0; i < vec_len (pv); i++) |
| 521 | { |
| 522 | af_packet_if_t *apif; |
| 523 | apif = vec_elt_at_index (apm->interfaces, pv[i].dev_instance); |
| 524 | if (apif->is_admin_up) |
Mohsin Kazmi | c1fd17b | 2022-03-22 21:40:04 +0000 | [diff] [blame] | 525 | { |
| 526 | if (apif->is_cksum_gso_enabled) |
| 527 | n_rx_packets += |
| 528 | af_packet_device_input_fn (vm, node, frame, apif, 1); |
| 529 | else |
| 530 | n_rx_packets += |
| 531 | af_packet_device_input_fn (vm, node, frame, apif, 0); |
| 532 | } |
Mohammed Hawari | 85c1943 | 2020-12-18 16:29:45 +0100 | [diff] [blame] | 533 | } |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 534 | return n_rx_packets; |
| 535 | } |
| 536 | |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 537 | VLIB_REGISTER_NODE (af_packet_input_node) = { |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 538 | .name = "af-packet-input", |
Damjan Marion | 7ca5aaa | 2019-09-24 18:10:49 +0200 | [diff] [blame] | 539 | .flags = VLIB_NODE_FLAG_TRACE_SUPPORTED, |
Damjan Marion | 51327ac | 2016-11-09 11:59:42 +0100 | [diff] [blame] | 540 | .sibling_of = "device-input", |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 541 | .format_trace = format_af_packet_input_trace, |
| 542 | .type = VLIB_NODE_TYPE_INPUT, |
| 543 | .state = VLIB_NODE_STATE_INTERRUPT, |
| 544 | .n_errors = AF_PACKET_INPUT_N_ERROR, |
| 545 | .error_strings = af_packet_input_error_strings, |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 546 | }; |
Damjan Marion | 1c80e83 | 2016-05-11 23:07:18 +0200 | [diff] [blame] | 547 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 548 | |
| 549 | /* |
| 550 | * fd.io coding-style-patch-verification: ON |
| 551 | * |
| 552 | * Local Variables: |
| 553 | * eval: (c-set-style "gnu") |
| 554 | * End: |
| 555 | */ |