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> |
Damjan Marion | 8bdc63b | 2016-11-02 14:48:21 +0100 | [diff] [blame] | 26 | #include <vnet/devices/devices.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> |
| 31 | |
| 32 | #define foreach_af_packet_input_error |
| 33 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 34 | typedef enum |
| 35 | { |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 36 | #define _(f,s) AF_PACKET_INPUT_ERROR_##f, |
| 37 | foreach_af_packet_input_error |
| 38 | #undef _ |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 39 | AF_PACKET_INPUT_N_ERROR, |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 40 | } af_packet_input_error_t; |
| 41 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 42 | static char *af_packet_input_error_strings[] = { |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 43 | #define _(n,s) s, |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 44 | foreach_af_packet_input_error |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 45 | #undef _ |
| 46 | }; |
| 47 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 48 | typedef struct |
| 49 | { |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 50 | u32 next_index; |
| 51 | u32 hw_if_index; |
| 52 | int block; |
| 53 | struct tpacket2_hdr tph; |
| 54 | } af_packet_input_trace_t; |
| 55 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 56 | static u8 * |
| 57 | format_af_packet_input_trace (u8 * s, va_list * args) |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 58 | { |
| 59 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 60 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 61 | 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] | 62 | u32 indent = format_get_indent (s); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 63 | |
| 64 | s = format (s, "af_packet: hw_if_index %d next-index %d", |
| 65 | t->hw_if_index, t->next_index); |
| 66 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 67 | s = |
| 68 | format (s, |
| 69 | "\n%Utpacket2_hdr:\n%Ustatus 0x%x len %u snaplen %u mac %u net %u" |
| 70 | "\n%Usec 0x%x nsec 0x%x vlan %U" |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 71 | #ifdef TP_STATUS_VLAN_TPID_VALID |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 72 | " vlan_tpid %u" |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 73 | #endif |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 74 | , |
| 75 | format_white_space, indent + 2, |
| 76 | format_white_space, indent + 4, |
| 77 | t->tph.tp_status, |
| 78 | t->tph.tp_len, |
| 79 | t->tph.tp_snaplen, |
| 80 | t->tph.tp_mac, |
| 81 | t->tph.tp_net, |
| 82 | format_white_space, indent + 4, |
| 83 | t->tph.tp_sec, |
| 84 | t->tph.tp_nsec, format_ethernet_vlan_tci, t->tph.tp_vlan_tci |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 85 | #ifdef TP_STATUS_VLAN_TPID_VALID |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 86 | , t->tph.tp_vlan_tpid |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 87 | #endif |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 88 | ); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 89 | return s; |
| 90 | } |
| 91 | |
| 92 | always_inline void |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 93 | buffer_add_to_chain (vlib_main_t * vm, u32 bi, u32 first_bi, u32 prev_bi) |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 94 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 95 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
| 96 | vlib_buffer_t *first_b = vlib_get_buffer (vm, first_bi); |
| 97 | vlib_buffer_t *prev_b = vlib_get_buffer (vm, prev_bi); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 98 | |
| 99 | /* update first buffer */ |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 100 | first_b->total_length_not_including_first_buffer += b->current_length; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 101 | |
| 102 | /* update previous buffer */ |
| 103 | prev_b->next_buffer = bi; |
| 104 | prev_b->flags |= VLIB_BUFFER_NEXT_PRESENT; |
| 105 | |
| 106 | /* update current buffer */ |
| 107 | b->next_buffer = 0; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 108 | } |
| 109 | |
Jakub Grajciar | 92b0275 | 2017-10-20 13:37:28 +0200 | [diff] [blame] | 110 | static_always_inline void |
| 111 | mark_tcp_udp_cksum_calc (vlib_buffer_t * b) |
| 112 | { |
| 113 | ethernet_header_t *eth = vlib_buffer_get_current (b); |
| 114 | if (clib_net_to_host_u16 (eth->type) == ETHERNET_TYPE_IP4) |
| 115 | { |
| 116 | ip4_header_t *ip4 = |
| 117 | (vlib_buffer_get_current (b) + sizeof (ethernet_header_t)); |
Jakub Grajciar | 5665a22 | 2017-11-15 17:25:50 +0100 | [diff] [blame] | 118 | b->flags |= VNET_BUFFER_F_IS_IP4; |
Jakub Grajciar | 92b0275 | 2017-10-20 13:37:28 +0200 | [diff] [blame] | 119 | if (ip4->protocol == IP_PROTOCOL_TCP) |
| 120 | { |
| 121 | b->flags |= VNET_BUFFER_F_OFFLOAD_TCP_CKSUM; |
| 122 | ((tcp_header_t |
| 123 | *) (vlib_buffer_get_current (b) + |
| 124 | sizeof (ethernet_header_t) + |
| 125 | ip4_header_bytes (ip4)))->checksum = 0; |
| 126 | } |
| 127 | else if (ip4->protocol == IP_PROTOCOL_UDP) |
| 128 | { |
| 129 | b->flags |= VNET_BUFFER_F_OFFLOAD_UDP_CKSUM; |
| 130 | ((udp_header_t |
| 131 | *) (vlib_buffer_get_current (b) + |
| 132 | sizeof (ethernet_header_t) + |
| 133 | ip4_header_bytes (ip4)))->checksum = 0; |
| 134 | } |
| 135 | vnet_buffer (b)->l3_hdr_offset = sizeof (ethernet_header_t); |
| 136 | vnet_buffer (b)->l4_hdr_offset = |
| 137 | sizeof (ethernet_header_t) + ip4_header_bytes (ip4); |
| 138 | } |
| 139 | else if (clib_net_to_host_u16 (eth->type) == ETHERNET_TYPE_IP6) |
| 140 | { |
| 141 | ip6_header_t *ip6 = |
| 142 | (vlib_buffer_get_current (b) + sizeof (ethernet_header_t)); |
| 143 | b->flags |= VNET_BUFFER_F_IS_IP6; |
| 144 | u16 ip6_hdr_len = sizeof (ip6_header_t); |
| 145 | if (ip6_ext_hdr (ip6->protocol)) |
| 146 | { |
| 147 | ip6_ext_header_t *p = (void *) (ip6 + 1); |
| 148 | ip6_hdr_len += ip6_ext_header_len (p); |
| 149 | while (ip6_ext_hdr (p->next_hdr)) |
| 150 | { |
| 151 | ip6_hdr_len += ip6_ext_header_len (p); |
| 152 | p = ip6_ext_next_header (p); |
| 153 | } |
| 154 | } |
| 155 | if (ip6->protocol == IP_PROTOCOL_TCP) |
| 156 | { |
| 157 | b->flags |= VNET_BUFFER_F_OFFLOAD_TCP_CKSUM; |
| 158 | ((tcp_header_t |
| 159 | *) (vlib_buffer_get_current (b) + |
| 160 | sizeof (ethernet_header_t) + ip6_hdr_len))->checksum = 0; |
| 161 | } |
| 162 | else if (ip6->protocol == IP_PROTOCOL_UDP) |
| 163 | { |
| 164 | b->flags |= VNET_BUFFER_F_OFFLOAD_UDP_CKSUM; |
| 165 | ((udp_header_t |
| 166 | *) (vlib_buffer_get_current (b) + |
| 167 | sizeof (ethernet_header_t) + ip6_hdr_len))->checksum = 0; |
| 168 | } |
| 169 | vnet_buffer (b)->l3_hdr_offset = sizeof (ethernet_header_t); |
| 170 | vnet_buffer (b)->l4_hdr_offset = |
| 171 | sizeof (ethernet_header_t) + ip6_hdr_len; |
| 172 | } |
| 173 | } |
| 174 | |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 175 | always_inline uword |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 176 | af_packet_device_input_fn (vlib_main_t * vm, vlib_node_runtime_t * node, |
Mohsin KAZMI | cf751ec | 2017-01-18 11:59:45 +0100 | [diff] [blame] | 177 | vlib_frame_t * frame, af_packet_if_t * apif) |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 178 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 179 | af_packet_main_t *apm = &af_packet_main; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 180 | struct tpacket2_hdr *tph; |
Damjan Marion | 8bdc63b | 2016-11-02 14:48:21 +0100 | [diff] [blame] | 181 | u32 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 182 | u32 block = 0; |
| 183 | u32 rx_frame; |
| 184 | u32 n_free_bufs; |
| 185 | u32 n_rx_packets = 0; |
| 186 | u32 n_rx_bytes = 0; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 187 | u32 *to_next = 0; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 188 | u32 block_size = apif->rx_req->tp_block_size; |
| 189 | u32 frame_size = apif->rx_req->tp_frame_size; |
| 190 | u32 frame_num = apif->rx_req->tp_frame_nr; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 191 | u8 *block_start = apif->rx_ring + block * block_size; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 192 | uword n_trace = vlib_get_trace_count (vm, node); |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 193 | u32 thread_index = vlib_get_thread_index (); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 194 | u32 n_buffer_bytes = vlib_buffer_free_list_buffer_size (vm, |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 195 | VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 196 | u32 min_bufs = apif->rx_req->tp_frame_size / n_buffer_bytes; |
| 197 | |
Dave Barach | 13f3c45 | 2016-03-29 11:56:41 -0400 | [diff] [blame] | 198 | if (apif->per_interface_next_index != ~0) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 199 | next_index = apif->per_interface_next_index; |
Dave Barach | 13f3c45 | 2016-03-29 11:56:41 -0400 | [diff] [blame] | 200 | |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 201 | n_free_bufs = vec_len (apm->rx_buffers[thread_index]); |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 202 | if (PREDICT_FALSE (n_free_bufs < VLIB_FRAME_SIZE)) |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 203 | { |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 204 | vec_validate (apm->rx_buffers[thread_index], |
Damjan Marion | 553f6bd | 2016-09-07 11:54:22 +0200 | [diff] [blame] | 205 | VLIB_FRAME_SIZE + n_free_bufs - 1); |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 206 | n_free_bufs += |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 207 | vlib_buffer_alloc (vm, &apm->rx_buffers[thread_index][n_free_bufs], |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 208 | VLIB_FRAME_SIZE); |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 209 | _vec_len (apm->rx_buffers[thread_index]) = n_free_bufs; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | rx_frame = apif->next_rx_frame; |
| 213 | tph = (struct tpacket2_hdr *) (block_start + rx_frame * frame_size); |
| 214 | while ((tph->tp_status & TP_STATUS_USER) && (n_free_bufs > min_bufs)) |
| 215 | { |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 216 | vlib_buffer_t *b0 = 0, *first_b0 = 0; |
Dave Barach | 13f3c45 | 2016-03-29 11:56:41 -0400 | [diff] [blame] | 217 | u32 next0 = next_index; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 218 | |
| 219 | u32 n_left_to_next; |
| 220 | vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); |
| 221 | while ((tph->tp_status & TP_STATUS_USER) && (n_free_bufs > min_bufs) && |
| 222 | n_left_to_next) |
| 223 | { |
| 224 | u32 data_len = tph->tp_snaplen; |
| 225 | u32 offset = 0; |
Benoît Ganne | d530445 | 2016-04-08 22:25:05 -0700 | [diff] [blame] | 226 | u32 bi0 = 0, first_bi0 = 0, prev_bi0; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 227 | |
| 228 | while (data_len) |
| 229 | { |
| 230 | /* grab free buffer */ |
Damjan Marion | 553f6bd | 2016-09-07 11:54:22 +0200 | [diff] [blame] | 231 | u32 last_empty_buffer = |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 232 | vec_len (apm->rx_buffers[thread_index]) - 1; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 233 | prev_bi0 = bi0; |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 234 | bi0 = apm->rx_buffers[thread_index][last_empty_buffer]; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 235 | b0 = vlib_get_buffer (vm, bi0); |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 236 | _vec_len (apm->rx_buffers[thread_index]) = last_empty_buffer; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 237 | n_free_bufs--; |
| 238 | |
| 239 | /* copy data */ |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 240 | u32 bytes_to_copy = |
| 241 | data_len > n_buffer_bytes ? n_buffer_bytes : data_len; |
Damjan Marion | c517020 | 2017-10-11 14:15:47 +0200 | [diff] [blame] | 242 | u32 vlan_len = 0; |
Akshaya N | 535f0bf | 2017-09-15 17:37:53 +0530 | [diff] [blame] | 243 | u32 bytes_copied = 0; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 244 | b0->current_data = 0; |
Akshaya N | 535f0bf | 2017-09-15 17:37:53 +0530 | [diff] [blame] | 245 | /* Kernel removes VLAN headers, so reconstruct VLAN */ |
| 246 | if (PREDICT_FALSE (tph->tp_status & TP_STATUS_VLAN_VALID)) |
| 247 | { |
| 248 | if (PREDICT_TRUE (offset == 0)) |
| 249 | { |
| 250 | clib_memcpy (vlib_buffer_get_current (b0), |
| 251 | (u8 *) tph + tph->tp_mac, |
| 252 | sizeof (ethernet_header_t)); |
| 253 | ethernet_header_t *eth = vlib_buffer_get_current (b0); |
| 254 | ethernet_vlan_header_t *vlan = |
| 255 | (ethernet_vlan_header_t *) (eth + 1); |
| 256 | vlan->priority_cfi_and_id = |
| 257 | clib_host_to_net_u16 (tph->tp_vlan_tci); |
| 258 | vlan->type = eth->type; |
| 259 | eth->type = clib_host_to_net_u16 (ETHERNET_TYPE_VLAN); |
| 260 | vlan_len = sizeof (ethernet_vlan_header_t); |
| 261 | bytes_copied = sizeof (ethernet_header_t); |
| 262 | } |
| 263 | } |
Damjan Marion | c517020 | 2017-10-11 14:15:47 +0200 | [diff] [blame] | 264 | clib_memcpy (((u8 *) vlib_buffer_get_current (b0)) + |
| 265 | bytes_copied + vlan_len, |
| 266 | (u8 *) tph + tph->tp_mac + offset + bytes_copied, |
| 267 | (bytes_to_copy - bytes_copied)); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 268 | |
| 269 | /* fill buffer header */ |
Akshaya N | 535f0bf | 2017-09-15 17:37:53 +0530 | [diff] [blame] | 270 | b0->current_length = bytes_to_copy + vlan_len; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 271 | |
| 272 | if (offset == 0) |
| 273 | { |
| 274 | b0->total_length_not_including_first_buffer = 0; |
| 275 | b0->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 276 | vnet_buffer (b0)->sw_if_index[VLIB_RX] = apif->sw_if_index; |
| 277 | vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 278 | first_bi0 = bi0; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 279 | first_b0 = vlib_get_buffer (vm, first_bi0); |
Jakub Grajciar | 92b0275 | 2017-10-20 13:37:28 +0200 | [diff] [blame] | 280 | if (tph->tp_status & TP_STATUS_CSUMNOTREADY) |
| 281 | mark_tcp_udp_cksum_calc (first_b0); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 282 | } |
| 283 | else |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 284 | buffer_add_to_chain (vm, bi0, first_bi0, prev_bi0); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 285 | |
| 286 | offset += bytes_to_copy; |
| 287 | data_len -= bytes_to_copy; |
| 288 | } |
| 289 | n_rx_packets++; |
| 290 | n_rx_bytes += tph->tp_snaplen; |
| 291 | to_next[0] = first_bi0; |
| 292 | to_next += 1; |
| 293 | n_left_to_next--; |
| 294 | |
| 295 | /* trace */ |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 296 | VLIB_BUFFER_TRACE_TRAJECTORY_INIT (first_b0); |
| 297 | if (PREDICT_FALSE (n_trace > 0)) |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 298 | { |
| 299 | af_packet_input_trace_t *tr; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 300 | vlib_trace_buffer (vm, node, next0, first_b0, /* follow_chain */ |
| 301 | 0); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 302 | vlib_set_trace_count (vm, node, --n_trace); |
| 303 | tr = vlib_add_trace (vm, node, first_b0, sizeof (*tr)); |
| 304 | tr->next_index = next0; |
| 305 | tr->hw_if_index = apif->hw_if_index; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 306 | clib_memcpy (&tr->tph, tph, sizeof (struct tpacket2_hdr)); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 307 | } |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 308 | |
| 309 | /* redirect if feature path enabled */ |
Damjan Marion | 35af9e5 | 2017-03-06 12:02:50 +0100 | [diff] [blame] | 310 | vnet_feature_start_device_input_x1 (apif->sw_if_index, &next0, b0); |
Damjan Marion | 2231150 | 2016-10-28 20:30:15 +0200 | [diff] [blame] | 311 | |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 312 | /* enque and take next packet */ |
| 313 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, |
| 314 | n_left_to_next, first_bi0, next0); |
| 315 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 316 | /* next packet */ |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 317 | tph->tp_status = TP_STATUS_KERNEL; |
| 318 | rx_frame = (rx_frame + 1) % frame_num; |
| 319 | tph = (struct tpacket2_hdr *) (block_start + rx_frame * frame_size); |
| 320 | } |
| 321 | |
| 322 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 323 | } |
| 324 | |
| 325 | apif->next_rx_frame = rx_frame; |
| 326 | |
| 327 | vlib_increment_combined_counter |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 328 | (vnet_get_main ()->interface_main.combined_sw_if_counters |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 329 | + VNET_INTERFACE_COUNTER_RX, |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 330 | 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] | 331 | |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 332 | vnet_device_increment_rx_packets (thread_index, n_rx_packets); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 333 | return n_rx_packets; |
| 334 | } |
| 335 | |
| 336 | static uword |
| 337 | af_packet_input_fn (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 338 | vlib_frame_t * frame) |
| 339 | { |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 340 | u32 n_rx_packets = 0; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 341 | af_packet_main_t *apm = &af_packet_main; |
Damjan Marion | eb743fa | 2017-03-20 16:34:15 +0100 | [diff] [blame] | 342 | vnet_device_input_runtime_t *rt = (void *) node->runtime_data; |
| 343 | vnet_device_and_queue_t *dq; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 344 | |
Damjan Marion | 153646e | 2017-04-05 18:15:45 +0200 | [diff] [blame] | 345 | foreach_device_and_queue (dq, rt->devices_and_queues) |
Damjan Marion | eb743fa | 2017-03-20 16:34:15 +0100 | [diff] [blame] | 346 | { |
| 347 | af_packet_if_t *apif; |
| 348 | apif = vec_elt_at_index (apm->interfaces, dq->dev_instance); |
| 349 | if (apif->is_admin_up) |
| 350 | n_rx_packets += af_packet_device_input_fn (vm, node, frame, apif); |
| 351 | } |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 352 | |
| 353 | return n_rx_packets; |
| 354 | } |
| 355 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 356 | /* *INDENT-OFF* */ |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 357 | VLIB_REGISTER_NODE (af_packet_input_node) = { |
| 358 | .function = af_packet_input_fn, |
| 359 | .name = "af-packet-input", |
Damjan Marion | 51327ac | 2016-11-09 11:59:42 +0100 | [diff] [blame] | 360 | .sibling_of = "device-input", |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 361 | .format_trace = format_af_packet_input_trace, |
| 362 | .type = VLIB_NODE_TYPE_INPUT, |
| 363 | .state = VLIB_NODE_STATE_INTERRUPT, |
| 364 | .n_errors = AF_PACKET_INPUT_N_ERROR, |
| 365 | .error_strings = af_packet_input_error_strings, |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 366 | }; |
Damjan Marion | 1c80e83 | 2016-05-11 23:07:18 +0200 | [diff] [blame] | 367 | |
| 368 | VLIB_NODE_FUNCTION_MULTIARCH (af_packet_input_node, af_packet_input_fn) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 369 | /* *INDENT-ON* */ |
Damjan Marion | 1c80e83 | 2016-05-11 23:07:18 +0200 | [diff] [blame] | 370 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 371 | |
| 372 | /* |
| 373 | * fd.io coding-style-patch-verification: ON |
| 374 | * |
| 375 | * Local Variables: |
| 376 | * eval: (c-set-style "gnu") |
| 377 | * End: |
| 378 | */ |