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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
15 #include <vnet/vnet.h>
16 #include <vppinfra/vec.h>
17 #include <vppinfra/error.h>
18 #include <vppinfra/format.h>
19 #include <vppinfra/xxhash.h>
21 #include <vnet/ethernet/ethernet.h>
22 #include <dpdk/device/dpdk.h>
23 #include <vnet/classify/vnet_classify.h>
24 #include <vnet/mpls/packet.h>
25 #include <vnet/handoff.h>
26 #include <vnet/devices/devices.h>
27 #include <vnet/feature/feature.h>
29 #include <dpdk/device/dpdk_priv.h>
31 static char *dpdk_error_strings[] = {
37 /* make sure all flags we need are stored in lower 8 bits */
38 STATIC_ASSERT ((PKT_RX_IP_CKSUM_BAD | PKT_RX_FDIR) <
39 256, "dpdk flags not un lower byte, fix needed");
41 static_always_inline uword
42 dpdk_process_subseq_segs (vlib_main_t * vm, vlib_buffer_t * b,
43 struct rte_mbuf *mb, vlib_buffer_t * bt)
46 struct rte_mbuf *mb_seg = 0;
47 vlib_buffer_t *b_seg, *b_chain = 0;
54 b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
55 b->total_length_not_including_first_buffer = 0;
57 while (nb_seg < mb->nb_segs)
61 b_seg = vlib_buffer_from_rte_mbuf (mb_seg);
62 vlib_buffer_copy_template (b_seg, bt);
65 * The driver (e.g. virtio) may not put the packet data at the start
66 * of the segment, so don't assume b_seg->current_data == 0 is correct.
69 (mb_seg->buf_addr + mb_seg->data_off) - (void *) b_seg->data;
71 b_seg->current_length = mb_seg->data_len;
72 b->total_length_not_including_first_buffer += mb_seg->data_len;
74 b_chain->flags |= VLIB_BUFFER_NEXT_PRESENT;
75 b_chain->next_buffer = vlib_get_buffer_index (vm, b_seg);
78 mb_seg = mb_seg->next;
81 return b->total_length_not_including_first_buffer;
84 static_always_inline void
85 dpdk_prefetch_mbuf_x4 (struct rte_mbuf *mb[])
87 CLIB_PREFETCH (mb[0], CLIB_CACHE_LINE_BYTES, LOAD);
88 CLIB_PREFETCH (mb[1], CLIB_CACHE_LINE_BYTES, LOAD);
89 CLIB_PREFETCH (mb[2], CLIB_CACHE_LINE_BYTES, LOAD);
90 CLIB_PREFETCH (mb[3], CLIB_CACHE_LINE_BYTES, LOAD);
93 static_always_inline void
94 dpdk_prefetch_buffer_x4 (struct rte_mbuf *mb[])
97 b = vlib_buffer_from_rte_mbuf (mb[0]);
98 CLIB_PREFETCH (b, CLIB_CACHE_LINE_BYTES, LOAD);
99 b = vlib_buffer_from_rte_mbuf (mb[1]);
100 CLIB_PREFETCH (b, CLIB_CACHE_LINE_BYTES, LOAD);
101 b = vlib_buffer_from_rte_mbuf (mb[2]);
102 CLIB_PREFETCH (b, CLIB_CACHE_LINE_BYTES, LOAD);
103 b = vlib_buffer_from_rte_mbuf (mb[3]);
104 CLIB_PREFETCH (b, CLIB_CACHE_LINE_BYTES, LOAD);
107 /** \brief Main DPDK input node
110 This is the main DPDK input node: across each assigned interface,
111 call rte_eth_rx_burst(...) or similar to obtain a vector of
112 packets to process. Derive @c vlib_buffer_t metadata from
113 <code>struct rte_mbuf</code> metadata,
114 Depending on the resulting metadata: adjust <code>b->current_data,
115 b->current_length </code> and dispatch directly to
116 ip4-input-no-checksum, or ip6-input. Trace the packet if required.
118 @param vm vlib_main_t corresponding to the current thread
119 @param node vlib_node_runtime_t
120 @param f vlib_frame_t input-node, not used.
122 @par Graph mechanics: buffer metadata, next index usage
125 - <code>struct rte_mbuf mb->ol_flags</code>
126 - PKT_RX_IP_CKSUM_BAD
129 - <code>b->error</code> if the packet is to be dropped immediately
130 - <code>b->current_data, b->current_length</code>
131 - adjusted as needed to skip the L2 header in direct-dispatch cases
132 - <code>vnet_buffer(b)->sw_if_index[VLIB_RX]</code>
133 - rx interface sw_if_index
134 - <code>vnet_buffer(b)->sw_if_index[VLIB_TX] = ~0</code>
135 - required by ipX-lookup
136 - <code>b->flags</code>
137 - to indicate multi-segment pkts (VLIB_BUFFER_NEXT_PRESENT), etc.
140 - Static arcs to: error-drop, ethernet-input,
141 ip4-input-no-checksum, ip6-input, mpls-input
142 - per-interface redirection, controlled by
143 <code>xd->per_interface_next_index</code>
146 static_always_inline u8
147 dpdk_ol_flags_extract (struct rte_mbuf **mb, u8 * flags, int count)
151 for (i = 0; i < count; i++)
153 /* all flags we are interested in are in lower 8 bits but
155 flags[i] = (u8) mb[i]->ol_flags;
161 static_always_inline uword
162 dpdk_process_rx_burst (vlib_main_t * vm, dpdk_per_thread_data_t * ptd,
163 uword n_rx_packets, int maybe_multiseg, u8 * or_flagsp)
165 u32 n_left = n_rx_packets;
167 struct rte_mbuf **mb = ptd->mbufs;
169 u8 *flags, or_flags = 0;
175 /* copy template into local variable - will save per packet load */
176 vlib_buffer_copy_template (&bt, &ptd->buffer_template);
179 dpdk_prefetch_buffer_x4 (mb + 4);
181 b[0] = vlib_buffer_from_rte_mbuf (mb[0]);
182 b[1] = vlib_buffer_from_rte_mbuf (mb[1]);
183 b[2] = vlib_buffer_from_rte_mbuf (mb[2]);
184 b[3] = vlib_buffer_from_rte_mbuf (mb[3]);
186 vlib_buffer_copy_template (b[0], &bt);
187 vlib_buffer_copy_template (b[1], &bt);
188 vlib_buffer_copy_template (b[2], &bt);
189 vlib_buffer_copy_template (b[3], &bt);
191 dpdk_prefetch_mbuf_x4 (mb + 4);
193 or_flags |= dpdk_ol_flags_extract (mb, flags, 4);
196 b[0]->current_data = mb[0]->data_off - RTE_PKTMBUF_HEADROOM;
197 n_bytes += b[0]->current_length = mb[0]->data_len;
199 b[1]->current_data = mb[1]->data_off - RTE_PKTMBUF_HEADROOM;
200 n_bytes += b[1]->current_length = mb[1]->data_len;
202 b[2]->current_data = mb[2]->data_off - RTE_PKTMBUF_HEADROOM;
203 n_bytes += b[2]->current_length = mb[2]->data_len;
205 b[3]->current_data = mb[3]->data_off - RTE_PKTMBUF_HEADROOM;
206 n_bytes += b[3]->current_length = mb[3]->data_len;
210 n_bytes += dpdk_process_subseq_segs (vm, b[0], mb[0], &bt);
211 n_bytes += dpdk_process_subseq_segs (vm, b[1], mb[1], &bt);
212 n_bytes += dpdk_process_subseq_segs (vm, b[2], mb[2], &bt);
213 n_bytes += dpdk_process_subseq_segs (vm, b[3], mb[3], &bt);
216 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[0]);
217 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[1]);
218 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[2]);
219 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[3]);
228 b[0] = vlib_buffer_from_rte_mbuf (mb[0]);
229 vlib_buffer_copy_template (b[0], &bt);
230 or_flags |= dpdk_ol_flags_extract (mb, flags, 1);
233 b[0]->current_data = mb[0]->data_off - RTE_PKTMBUF_HEADROOM;
234 n_bytes += b[0]->current_length = mb[0]->data_len;
237 n_bytes += dpdk_process_subseq_segs (vm, b[0], mb[0], &bt);
238 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[0]);
245 *or_flagsp = or_flags;
249 static_always_inline void
250 dpdk_process_flow_offload (dpdk_device_t * xd, dpdk_per_thread_data_t * ptd,
254 dpdk_flow_lookup_entry_t *fle;
257 /* TODO prefetch and quad-loop */
258 for (n = 0; n < n_rx_packets; n++)
260 if ((ptd->flags[n] & (1 << PKT_RX_FDIR)) == 0)
263 fle = pool_elt_at_index (xd->flow_lookup_entries,
264 ptd->mbufs[n]->hash.fdir.hi);
266 if (fle->next_index != (u16) ~ 0)
267 ptd->next[n] = fle->next_index;
269 if (fle->flow_id != ~0)
271 b0 = vlib_buffer_from_rte_mbuf (ptd->mbufs[n]);
272 b0->flow_id = fle->flow_id;
275 if (fle->buffer_advance != ~0)
277 b0 = vlib_buffer_from_rte_mbuf (ptd->mbufs[n]);
278 vlib_buffer_advance (b0, fle->buffer_advance);
283 static_always_inline u32
284 dpdk_device_input (vlib_main_t * vm, dpdk_main_t * dm, dpdk_device_t * xd,
285 vlib_node_runtime_t * node, u32 thread_index, u16 queue_id)
287 uword n_rx_packets = 0, n_rx_bytes;
290 u32 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
291 struct rte_mbuf **mb;
298 dpdk_per_thread_data_t *ptd = vec_elt_at_index (dm->per_thread_data,
300 vlib_buffer_t *bt = &ptd->buffer_template;
302 if ((xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP) == 0)
305 /* get up to DPDK_RX_BURST_SZ buffers from PMD */
306 while (n_rx_packets < DPDK_RX_BURST_SZ)
308 n = rte_eth_rx_burst (xd->port_id, queue_id,
309 ptd->mbufs + n_rx_packets,
310 DPDK_RX_BURST_SZ - n_rx_packets);
317 if (n_rx_packets == 0)
320 /* Update buffer template */
321 vnet_buffer (bt)->sw_if_index[VLIB_RX] = xd->sw_if_index;
322 bt->error = node->errors[DPDK_ERROR_NONE];
323 /* as DPDK is allocating empty buffers from mempool provided before interface
324 start for each queue, it is safe to store this in the template */
325 bt->buffer_pool_index = xd->buffer_pool_for_queue[queue_id];
326 vnet_buffer (bt)->feature_arc_index = 0;
327 bt->current_config_index = 0;
329 /* receive burst of packets from DPDK PMD */
330 if (PREDICT_FALSE (xd->per_interface_next_index != ~0))
331 next_index = xd->per_interface_next_index;
333 /* as all packets belong to the same interface feature arc lookup
334 can be don once and result stored in the buffer template */
335 if (PREDICT_FALSE (vnet_device_input_have_features (xd->sw_if_index)))
336 vnet_feature_start_device_input_x1 (xd->sw_if_index, &next_index, bt);
338 if (xd->flags & DPDK_DEVICE_FLAG_MAYBE_MULTISEG)
339 n_rx_bytes = dpdk_process_rx_burst (vm, ptd, n_rx_packets, 1, &or_flags);
341 n_rx_bytes = dpdk_process_rx_burst (vm, ptd, n_rx_packets, 0, &or_flags);
343 if (PREDICT_FALSE (or_flags & PKT_RX_FDIR))
345 /* some packets will need to go to different next nodes */
346 for (n = 0; n < n_rx_packets; n++)
347 ptd->next[n] = next_index;
349 /* flow offload - process if rx flow offload enabled and at least one
351 if (PREDICT_FALSE ((xd->flags & DPDK_DEVICE_FLAG_RX_FLOW_OFFLOAD) &&
352 (or_flags & PKT_RX_FDIR)))
353 dpdk_process_flow_offload (xd, ptd, n_rx_packets);
355 /* enqueue buffers to the next node */
356 vlib_get_buffer_indices_with_offset (vm, (void **) ptd->mbufs,
357 ptd->buffers, n_rx_packets,
358 sizeof (struct rte_mbuf));
360 vlib_buffer_enqueue_to_next (vm, node, ptd->buffers, ptd->next,
365 u32 *to_next, n_left_to_next;
367 vlib_get_new_next_frame (vm, node, next_index, to_next, n_left_to_next);
368 vlib_get_buffer_indices_with_offset (vm, (void **) ptd->mbufs, to_next,
370 sizeof (struct rte_mbuf));
372 if (PREDICT_TRUE (next_index == VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT))
374 vlib_next_frame_t *nf;
376 ethernet_input_frame_t *ef;
377 nf = vlib_node_runtime_get_next_frame (vm, node, next_index);
378 f = vlib_get_frame (vm, nf->frame_index);
379 f->flags = ETH_INPUT_FRAME_F_SINGLE_SW_IF_IDX;
381 ef = vlib_frame_scalar_args (f);
382 ef->sw_if_index = xd->sw_if_index;
383 ef->hw_if_index = xd->hw_if_index;
385 /* if PMD supports ip4 checksum check and there are no packets
386 marked as ip4 checksum bad we can notify ethernet input so it
387 can send pacets to ip4-input-no-checksum node */
388 if (xd->flags & DPDK_DEVICE_FLAG_RX_IP4_CKSUM &&
389 (or_flags & PKT_RX_IP_CKSUM_BAD) == 0)
390 f->flags |= ETH_INPUT_FRAME_F_IP4_CKSUM_OK;
392 n_left_to_next -= n_rx_packets;
393 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
397 /* packet trace if enabled */
398 if (PREDICT_FALSE ((n_trace = vlib_get_trace_count (vm, node))))
401 vlib_get_buffer_indices_with_offset (vm, (void **) ptd->mbufs,
402 ptd->buffers, n_rx_packets,
403 sizeof (struct rte_mbuf));
405 n_left = n_rx_packets;
406 buffers = ptd->buffers;
410 while (n_trace && n_left)
412 b0 = vlib_get_buffer (vm, buffers[0]);
413 if (single_next == 0)
414 next_index = next[0];
415 vlib_trace_buffer (vm, node, next_index, b0, /* follow_chain */ 0);
417 dpdk_rx_trace_t *t0 = vlib_add_trace (vm, node, b0, sizeof t0[0]);
418 t0->queue_index = queue_id;
419 t0->device_index = xd->device_index;
420 t0->buffer_index = vlib_get_buffer_index (vm, b0);
422 clib_memcpy_fast (&t0->mb, mb[0], sizeof t0->mb);
423 clib_memcpy_fast (&t0->buffer, b0,
424 sizeof b0[0] - sizeof b0->pre_data);
425 clib_memcpy_fast (t0->buffer.pre_data, b0->data,
426 sizeof t0->buffer.pre_data);
427 clib_memcpy_fast (&t0->data, mb[0]->buf_addr + mb[0]->data_off,
435 vlib_set_trace_count (vm, node, n_trace);
438 /* rx pcap capture if enabled */
439 if (PREDICT_FALSE (dm->pcap[VLIB_RX].pcap_enable))
442 n_left = n_rx_packets;
443 buffers = ptd->buffers;
447 b0 = vlib_get_buffer (vm, bi0);
450 if (dm->pcap[VLIB_RX].pcap_sw_if_index == 0 ||
451 dm->pcap[VLIB_RX].pcap_sw_if_index
452 == vnet_buffer (b0)->sw_if_index[VLIB_RX])
459 * Note: current_data will have advanced
460 * when we skip ethernet input.
461 * Temporarily back up to the original DMA
462 * target, so we capture a valid ethernet frame
464 mb = rte_mbuf_from_vlib_buffer (b0);
466 /* Figure out the original data_start */
467 data_start = (mb->buf_addr + mb->data_off) - (void *) b0->data;
468 /* Back up that far */
469 temp_advance = b0->current_data - data_start;
470 vlib_buffer_advance (b0, -temp_advance);
471 /* Trace the packet */
472 pcap_add_buffer (&dm->pcap[VLIB_RX].pcap_main, vm, bi0, 512);
473 /* and advance again */
474 vlib_buffer_advance (b0, temp_advance);
480 vlib_increment_combined_counter
481 (vnet_get_main ()->interface_main.combined_sw_if_counters
482 + VNET_INTERFACE_COUNTER_RX, thread_index, xd->sw_if_index,
483 n_rx_packets, n_rx_bytes);
485 vnet_device_increment_rx_packets (thread_index, n_rx_packets);
490 VLIB_NODE_FN (dpdk_input_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
493 dpdk_main_t *dm = &dpdk_main;
495 uword n_rx_packets = 0;
496 vnet_device_input_runtime_t *rt = (void *) node->runtime_data;
497 vnet_device_and_queue_t *dq;
498 u32 thread_index = node->thread_index;
501 * Poll all devices on this cpu for input/interrupts.
504 foreach_device_and_queue (dq, rt->devices_and_queues)
506 xd = vec_elt_at_index(dm->devices, dq->dev_instance);
507 if (PREDICT_FALSE (xd->flags & DPDK_DEVICE_FLAG_BOND_SLAVE))
508 continue; /* Do not poll slave to a bonded interface */
509 n_rx_packets += dpdk_device_input (vm, dm, xd, node, thread_index,
517 VLIB_REGISTER_NODE (dpdk_input_node) = {
518 .type = VLIB_NODE_TYPE_INPUT,
519 .name = "dpdk-input",
520 .sibling_of = "device-input",
522 /* Will be enabled if/when hardware is detected. */
523 .state = VLIB_NODE_STATE_DISABLED,
525 .format_buffer = format_ethernet_header_with_length,
526 .format_trace = format_dpdk_rx_trace,
528 .n_errors = DPDK_N_ERROR,
529 .error_strings = dpdk_error_strings,
534 * fd.io coding-style-patch-verification: ON
537 * eval: (c-set-style "gnu")