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/devices/devices.h>
27 #include <vnet/feature/feature.h>
29 #include <vnet/devices/af_packet/af_packet.h>
31 #define foreach_af_packet_input_error
35 #define _(f,s) AF_PACKET_INPUT_ERROR_##f,
36 foreach_af_packet_input_error
38 AF_PACKET_INPUT_N_ERROR,
39 } af_packet_input_error_t;
41 static char *af_packet_input_error_strings[] = {
43 foreach_af_packet_input_error
52 struct tpacket2_hdr tph;
53 } af_packet_input_trace_t;
56 format_af_packet_input_trace (u8 * s, va_list * args)
58 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
59 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
60 af_packet_input_trace_t *t = va_arg (*args, af_packet_input_trace_t *);
61 uword indent = format_get_indent (s);
63 s = format (s, "af_packet: hw_if_index %d next-index %d",
64 t->hw_if_index, t->next_index);
68 "\n%Utpacket2_hdr:\n%Ustatus 0x%x len %u snaplen %u mac %u net %u"
69 "\n%Usec 0x%x nsec 0x%x vlan %U"
70 #ifdef TP_STATUS_VLAN_TPID_VALID
74 format_white_space, indent + 2,
75 format_white_space, indent + 4,
81 format_white_space, indent + 4,
83 t->tph.tp_nsec, format_ethernet_vlan_tci, t->tph.tp_vlan_tci
84 #ifdef TP_STATUS_VLAN_TPID_VALID
92 buffer_add_to_chain (vlib_main_t * vm, u32 bi, u32 first_bi, u32 prev_bi)
94 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
95 vlib_buffer_t *first_b = vlib_get_buffer (vm, first_bi);
96 vlib_buffer_t *prev_b = vlib_get_buffer (vm, prev_bi);
98 /* update first buffer */
99 first_b->total_length_not_including_first_buffer += b->current_length;
101 /* update previous buffer */
102 prev_b->next_buffer = bi;
103 prev_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
105 /* update current buffer */
110 af_packet_device_input_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
111 vlib_frame_t * frame, af_packet_if_t * apif)
113 af_packet_main_t *apm = &af_packet_main;
114 struct tpacket2_hdr *tph;
115 u32 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
119 u32 n_rx_packets = 0;
122 u32 block_size = apif->rx_req->tp_block_size;
123 u32 frame_size = apif->rx_req->tp_frame_size;
124 u32 frame_num = apif->rx_req->tp_frame_nr;
125 u8 *block_start = apif->rx_ring + block * block_size;
126 uword n_trace = vlib_get_trace_count (vm, node);
127 u32 thread_index = vlib_get_thread_index ();
128 u32 n_buffer_bytes = vlib_buffer_free_list_buffer_size (vm,
129 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
130 u32 min_bufs = apif->rx_req->tp_frame_size / n_buffer_bytes;
132 if (apif->per_interface_next_index != ~0)
133 next_index = apif->per_interface_next_index;
135 n_free_bufs = vec_len (apm->rx_buffers[thread_index]);
136 if (PREDICT_FALSE (n_free_bufs < VLIB_FRAME_SIZE))
138 vec_validate (apm->rx_buffers[thread_index],
139 VLIB_FRAME_SIZE + n_free_bufs - 1);
141 vlib_buffer_alloc (vm, &apm->rx_buffers[thread_index][n_free_bufs],
143 _vec_len (apm->rx_buffers[thread_index]) = n_free_bufs;
146 rx_frame = apif->next_rx_frame;
147 tph = (struct tpacket2_hdr *) (block_start + rx_frame * frame_size);
148 while ((tph->tp_status & TP_STATUS_USER) && (n_free_bufs > min_bufs))
150 vlib_buffer_t *b0 = 0, *first_b0 = 0;
151 u32 next0 = next_index;
154 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
155 while ((tph->tp_status & TP_STATUS_USER) && (n_free_bufs > min_bufs) &&
158 u32 data_len = tph->tp_snaplen;
160 u32 bi0 = 0, first_bi0 = 0, prev_bi0;
164 /* grab free buffer */
165 u32 last_empty_buffer =
166 vec_len (apm->rx_buffers[thread_index]) - 1;
168 bi0 = apm->rx_buffers[thread_index][last_empty_buffer];
169 b0 = vlib_get_buffer (vm, bi0);
170 _vec_len (apm->rx_buffers[thread_index]) = last_empty_buffer;
175 data_len > n_buffer_bytes ? n_buffer_bytes : data_len;
177 u32 bytes_copied = 0;
178 b0->current_data = 0;
179 /* Kernel removes VLAN headers, so reconstruct VLAN */
180 if (PREDICT_FALSE (tph->tp_status & TP_STATUS_VLAN_VALID))
182 if (PREDICT_TRUE (offset == 0))
184 clib_memcpy (vlib_buffer_get_current (b0),
185 (u8 *) tph + tph->tp_mac,
186 sizeof (ethernet_header_t));
187 ethernet_header_t *eth = vlib_buffer_get_current (b0);
188 ethernet_vlan_header_t *vlan =
189 (ethernet_vlan_header_t *) (eth + 1);
190 vlan->priority_cfi_and_id =
191 clib_host_to_net_u16 (tph->tp_vlan_tci);
192 vlan->type = eth->type;
193 eth->type = clib_host_to_net_u16 (ETHERNET_TYPE_VLAN);
194 vlan_len = sizeof (ethernet_vlan_header_t);
195 bytes_copied = sizeof (ethernet_header_t);
198 clib_memcpy (((u8 *) vlib_buffer_get_current (b0)) +
199 bytes_copied + vlan_len,
200 (u8 *) tph + tph->tp_mac + offset + bytes_copied,
201 (bytes_to_copy - bytes_copied));
203 /* fill buffer header */
204 b0->current_length = bytes_to_copy + vlan_len;
208 b0->total_length_not_including_first_buffer = 0;
209 b0->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID;
210 vnet_buffer (b0)->sw_if_index[VLIB_RX] = apif->sw_if_index;
211 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
213 first_b0 = vlib_get_buffer (vm, first_bi0);
216 buffer_add_to_chain (vm, bi0, first_bi0, prev_bi0);
218 offset += bytes_to_copy;
219 data_len -= bytes_to_copy;
222 n_rx_bytes += tph->tp_snaplen;
223 to_next[0] = first_bi0;
228 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (first_b0);
229 if (PREDICT_FALSE (n_trace > 0))
231 af_packet_input_trace_t *tr;
232 vlib_trace_buffer (vm, node, next0, first_b0, /* follow_chain */
234 vlib_set_trace_count (vm, node, --n_trace);
235 tr = vlib_add_trace (vm, node, first_b0, sizeof (*tr));
236 tr->next_index = next0;
237 tr->hw_if_index = apif->hw_if_index;
238 clib_memcpy (&tr->tph, tph, sizeof (struct tpacket2_hdr));
241 /* redirect if feature path enabled */
242 vnet_feature_start_device_input_x1 (apif->sw_if_index, &next0, b0);
244 /* enque and take next packet */
245 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
246 n_left_to_next, first_bi0, next0);
249 tph->tp_status = TP_STATUS_KERNEL;
250 rx_frame = (rx_frame + 1) % frame_num;
251 tph = (struct tpacket2_hdr *) (block_start + rx_frame * frame_size);
254 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
257 apif->next_rx_frame = rx_frame;
259 vlib_increment_combined_counter
260 (vnet_get_main ()->interface_main.combined_sw_if_counters
261 + VNET_INTERFACE_COUNTER_RX,
262 vlib_get_thread_index (), apif->hw_if_index, n_rx_packets, n_rx_bytes);
264 vnet_device_increment_rx_packets (thread_index, n_rx_packets);
269 af_packet_input_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
270 vlib_frame_t * frame)
272 u32 n_rx_packets = 0;
273 af_packet_main_t *apm = &af_packet_main;
274 vnet_device_input_runtime_t *rt = (void *) node->runtime_data;
275 vnet_device_and_queue_t *dq;
277 foreach_device_and_queue (dq, rt->devices_and_queues)
279 af_packet_if_t *apif;
280 apif = vec_elt_at_index (apm->interfaces, dq->dev_instance);
281 if (apif->is_admin_up)
282 n_rx_packets += af_packet_device_input_fn (vm, node, frame, apif);
289 VLIB_REGISTER_NODE (af_packet_input_node) = {
290 .function = af_packet_input_fn,
291 .name = "af-packet-input",
292 .sibling_of = "device-input",
293 .format_trace = format_af_packet_input_trace,
294 .type = VLIB_NODE_TYPE_INPUT,
295 .state = VLIB_NODE_STATE_INTERRUPT,
296 .n_errors = AF_PACKET_INPUT_N_ERROR,
297 .error_strings = af_packet_input_error_strings,
300 VLIB_NODE_FUNCTION_MULTIARCH (af_packet_input_node, af_packet_input_fn)
305 * fd.io coding-style-patch-verification: ON
308 * eval: (c-set-style "gnu")