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/format.h>
18 #include <vlib/unix/cj.h>
21 #include <vnet/ethernet/ethernet.h>
22 #include <vnet/devices/dpdk/dpdk.h>
24 #include "dpdk_priv.h"
25 #include <vppinfra/error.h>
27 #define foreach_dpdk_tx_func_error \
28 _(BAD_RETVAL, "DPDK tx function returned an error") \
29 _(RING_FULL, "Tx packet drops (ring full)") \
30 _(PKT_DROP, "Tx packet drops (dpdk tx failure)") \
31 _(REPL_FAIL, "Tx packet drops (replication failure)")
34 #define _(f,s) DPDK_TX_FUNC_ERROR_##f,
35 foreach_dpdk_tx_func_error
38 } dpdk_tx_func_error_t;
40 static char * dpdk_tx_func_error_strings[] = {
42 foreach_dpdk_tx_func_error
47 dpdk_set_mac_address (vnet_hw_interface_t * hi, char * address)
50 dpdk_main_t * dm = &dpdk_main;
51 dpdk_device_t * xd = vec_elt_at_index (dm->devices, hi->dev_instance);
53 error=rte_eth_dev_default_mac_addr_set(xd->device_index,
54 (struct ether_addr *) address);
57 return clib_error_return (0, "mac address set failed: %d", error);
64 dpdk_set_mc_filter (vnet_hw_interface_t * hi,
65 struct ether_addr mc_addr_vec[], int naddr)
68 dpdk_main_t * dm = &dpdk_main;
69 dpdk_device_t * xd = vec_elt_at_index (dm->devices, hi->dev_instance);
71 error=rte_eth_dev_set_mc_addr_list(xd->device_index, mc_addr_vec, naddr);
74 return clib_error_return (0, "mc addr list failed: %d", error);
80 struct rte_mbuf * dpdk_replicate_packet_mb (vlib_buffer_t * b)
82 vlib_main_t * vm = vlib_get_main();
83 vlib_buffer_main_t * bm = vm->buffer_main;
84 struct rte_mbuf * first_mb = 0, * new_mb, * pkt_mb, ** prev_mb_next = 0;
85 u8 nb_segs, nb_segs_left;
87 unsigned socket_id = rte_socket_id();
89 ASSERT (bm->pktmbuf_pools[socket_id]);
90 pkt_mb = rte_mbuf_from_vlib_buffer(b);
91 nb_segs = pkt_mb->nb_segs;
92 for (nb_segs_left = nb_segs; nb_segs_left; nb_segs_left--)
94 if (PREDICT_FALSE(pkt_mb == 0))
96 clib_warning ("Missing %d mbuf chain segment(s): "
97 "(nb_segs = %d, nb_segs_left = %d)!",
98 nb_segs - nb_segs_left, nb_segs, nb_segs_left);
100 rte_pktmbuf_free(first_mb);
103 new_mb = rte_pktmbuf_alloc (bm->pktmbuf_pools[socket_id]);
104 if (PREDICT_FALSE(new_mb == 0))
107 rte_pktmbuf_free(first_mb);
112 * Copy packet info into 1st segment.
117 rte_pktmbuf_pkt_len (first_mb) = pkt_mb->pkt_len;
118 first_mb->nb_segs = pkt_mb->nb_segs;
119 first_mb->port = pkt_mb->port;
120 #ifdef DAW_FIXME // TX Offload support TBD
121 first_mb->vlan_macip = pkt_mb->vlan_macip;
122 first_mb->hash = pkt_mb->hash;
123 first_mb->ol_flags = pkt_mb->ol_flags
128 ASSERT(prev_mb_next != 0);
129 *prev_mb_next = new_mb;
133 * Copy packet segment data into new mbuf segment.
135 rte_pktmbuf_data_len (new_mb) = pkt_mb->data_len;
136 copy_bytes = pkt_mb->data_len + RTE_PKTMBUF_HEADROOM;
137 ASSERT(copy_bytes <= pkt_mb->buf_len);
138 clib_memcpy(new_mb->buf_addr, pkt_mb->buf_addr, copy_bytes);
140 prev_mb_next = &new_mb->next;
141 pkt_mb = pkt_mb->next;
145 __rte_mbuf_sanity_check(first_mb, 1);
150 struct rte_mbuf * dpdk_zerocopy_replicate_packet_mb (vlib_buffer_t * b)
152 vlib_main_t * vm = vlib_get_main();
153 vlib_buffer_main_t * bm = vm->buffer_main;
154 struct rte_mbuf * first_mb = 0, * new_mb, * pkt_mb, ** prev_mb_next = 0;
155 u8 nb_segs, nb_segs_left;
156 unsigned socket_id = rte_socket_id();
158 ASSERT (bm->pktmbuf_pools[socket_id]);
159 pkt_mb = rte_mbuf_from_vlib_buffer(b);
160 nb_segs = pkt_mb->nb_segs;
161 for (nb_segs_left = nb_segs; nb_segs_left; nb_segs_left--)
163 if (PREDICT_FALSE(pkt_mb == 0))
165 clib_warning ("Missing %d mbuf chain segment(s): "
166 "(nb_segs = %d, nb_segs_left = %d)!",
167 nb_segs - nb_segs_left, nb_segs, nb_segs_left);
169 rte_pktmbuf_free(first_mb);
172 new_mb = rte_pktmbuf_clone(pkt_mb, bm->pktmbuf_pools[socket_id]);
173 if (PREDICT_FALSE(new_mb == 0))
176 rte_pktmbuf_free(first_mb);
181 * Copy packet info into 1st segment.
186 rte_pktmbuf_pkt_len (first_mb) = pkt_mb->pkt_len;
187 first_mb->nb_segs = pkt_mb->nb_segs;
188 first_mb->port = pkt_mb->port;
189 #ifdef DAW_FIXME // TX Offload support TBD
190 first_mb->vlan_macip = pkt_mb->vlan_macip;
191 first_mb->hash = pkt_mb->hash;
192 first_mb->ol_flags = pkt_mb->ol_flags
197 ASSERT(prev_mb_next != 0);
198 *prev_mb_next = new_mb;
202 * Copy packet segment data into new mbuf segment.
204 rte_pktmbuf_data_len (new_mb) = pkt_mb->data_len;
206 prev_mb_next = &new_mb->next;
207 pkt_mb = pkt_mb->next;
211 __rte_mbuf_sanity_check(first_mb, 1);
219 dpdk_tx_trace_buffer (dpdk_main_t * dm,
220 vlib_node_runtime_t * node,
224 vlib_buffer_t * buffer)
226 vlib_main_t * vm = vlib_get_main();
227 dpdk_tx_dma_trace_t * t0;
228 struct rte_mbuf * mb;
230 mb = rte_mbuf_from_vlib_buffer(buffer);
232 t0 = vlib_add_trace (vm, node, buffer, sizeof (t0[0]));
233 t0->queue_index = queue_id;
234 t0->device_index = xd->device_index;
235 t0->buffer_index = buffer_index;
236 clib_memcpy (&t0->mb, mb, sizeof (t0->mb));
237 clib_memcpy (&t0->buffer, buffer, sizeof (buffer[0]) - sizeof (buffer->pre_data));
238 clib_memcpy (t0->buffer.pre_data, buffer->data + buffer->current_data,
239 sizeof (t0->buffer.pre_data));
243 * This function calls the dpdk's tx_burst function to transmit the packets
244 * on the tx_vector. It manages a lock per-device if the device does not
245 * support multiple queues. It returns the number of packets untransmitted
246 * on the tx_vector. If all packets are transmitted (the normal case), the
247 * function returns 0.
249 * The tx_burst function may not be able to transmit all packets because the
250 * dpdk ring is full. If a flowcontrol callback function has been configured
251 * then the function simply returns. If no callback has been configured, the
252 * function will retry calling tx_burst with the remaining packets. This will
253 * continue until all packets are transmitted or tx_burst indicates no packets
254 * could be transmitted. (The caller can drop the remaining packets.)
256 * The function assumes there is at least one packet on the tx_vector.
259 u32 tx_burst_vector_internal (vlib_main_t * vm,
261 struct rte_mbuf ** tx_vector)
263 dpdk_main_t * dm = &dpdk_main;
272 ring = vec_header(tx_vector, sizeof(*ring));
274 n_packets = ring->tx_head - ring->tx_tail;
276 tx_head = ring->tx_head % DPDK_TX_RING_SIZE;
279 * Ensure rte_eth_tx_burst is not called with 0 packets, which can lead to
280 * unpredictable results.
282 ASSERT(n_packets > 0);
285 * Check for tx_vector overflow. If this fails it is a system configuration
286 * error. The ring should be sized big enough to handle the largest un-flowed
287 * off burst from a traffic manager. A larger size also helps performance
288 * a bit because it decreases the probability of having to issue two tx_burst
289 * calls due to a ring wrap.
291 ASSERT(n_packets < DPDK_TX_RING_SIZE);
294 * If there is no flowcontrol callback, there is only temporary buffering
295 * on the tx_vector and so the tail should always be 0.
297 ASSERT(dm->flowcontrol_callback || ring->tx_tail == 0);
300 * If there is a flowcontrol callback, don't retry any incomplete tx_bursts.
301 * Apply backpressure instead. If there is no callback, keep retrying until
302 * a tx_burst sends no packets. n_retry of 255 essentially means no retry
305 n_retry = dm->flowcontrol_callback ? 0 : 255;
307 queue_id = vm->cpu_index;
310 /* start the burst at the tail */
311 tx_tail = ring->tx_tail % DPDK_TX_RING_SIZE;
314 * This device only supports one TX queue,
315 * and we're running multi-threaded...
317 if (PREDICT_FALSE(xd->dev_type != VNET_DPDK_DEV_VHOST_USER &&
320 queue_id = queue_id % xd->tx_q_used;
321 while (__sync_lock_test_and_set (xd->lockp[queue_id], 1))
323 queue_id = (queue_id + 1) % xd->tx_q_used;
326 if (PREDICT_TRUE(xd->dev_type == VNET_DPDK_DEV_ETH))
328 if (PREDICT_TRUE(tx_head > tx_tail))
330 /* no wrap, transmit in one burst */
331 rv = rte_eth_tx_burst(xd->device_index,
334 (uint16_t) (tx_head-tx_tail));
339 * This can only happen if there is a flowcontrol callback.
340 * We need to split the transmit into two calls: one for
341 * the packets up to the wrap point, and one to continue
342 * at the start of the ring.
343 * Transmit pkts up to the wrap point.
345 rv = rte_eth_tx_burst(xd->device_index,
348 (uint16_t) (DPDK_TX_RING_SIZE - tx_tail));
351 * If we transmitted everything we wanted, then allow 1 retry
352 * so we can try to transmit the rest. If we didn't transmit
353 * everything, stop now.
355 n_retry = (rv == DPDK_TX_RING_SIZE - tx_tail) ? 1 : 0;
359 else if (xd->dev_type == VNET_DPDK_DEV_VHOST_USER)
362 if (xd->need_txlock) {
364 while (__sync_lock_test_and_set (xd->lockp[queue_id], 1));
367 dpdk_device_and_queue_t * dq;
368 vec_foreach (dq, dm->devices_by_cpu[vm->cpu_index])
370 if (xd->device_index == dq->device)
374 offset = dq->queue_id * VIRTIO_QNUM;
376 if (PREDICT_TRUE(tx_head > tx_tail))
378 int i; u32 bytes = 0;
379 struct rte_mbuf **pkts = &tx_vector[tx_tail];
380 for (i = 0; i < (tx_head - tx_tail); i++) {
381 struct rte_mbuf *buff = pkts[i];
382 bytes += rte_pktmbuf_data_len(buff);
385 /* no wrap, transmit in one burst */
386 rv = rte_vhost_enqueue_burst(&xd->vu_vhost_dev, offset + VIRTIO_RXQ,
388 (uint16_t) (tx_head-tx_tail));
389 if (PREDICT_TRUE(rv > 0))
391 dpdk_vu_vring *vring = &(xd->vu_intf->vrings[offset + VIRTIO_TXQ]);
392 vring->packets += rv;
393 vring->bytes += bytes;
395 if (dpdk_vhost_user_want_interrupt(xd, offset + VIRTIO_RXQ)) {
396 vring = &(xd->vu_intf->vrings[offset + VIRTIO_RXQ]);
397 vring->n_since_last_int += rv;
399 f64 now = vlib_time_now (vm);
400 if (vring->int_deadline < now ||
401 vring->n_since_last_int > dm->conf->vhost_coalesce_frames)
402 dpdk_vhost_user_send_interrupt(vm, xd, offset + VIRTIO_RXQ);
407 rte_pktmbuf_free (tx_vector[tx_tail+c]);
413 * If we transmitted everything we wanted, then allow 1 retry
414 * so we can try to transmit the rest. If we didn't transmit
415 * everything, stop now.
417 int i; u32 bytes = 0;
418 struct rte_mbuf **pkts = &tx_vector[tx_tail];
419 for (i = 0; i < (DPDK_TX_RING_SIZE - tx_tail); i++) {
420 struct rte_mbuf *buff = pkts[i];
421 bytes += rte_pktmbuf_data_len(buff);
423 rv = rte_vhost_enqueue_burst(&xd->vu_vhost_dev, offset + VIRTIO_RXQ,
425 (uint16_t) (DPDK_TX_RING_SIZE - tx_tail));
427 if (PREDICT_TRUE(rv > 0))
429 dpdk_vu_vring *vring = &(xd->vu_intf->vrings[offset + VIRTIO_TXQ]);
430 vring->packets += rv;
431 vring->bytes += bytes;
433 if (dpdk_vhost_user_want_interrupt(xd, offset + VIRTIO_RXQ)) {
434 vring = &(xd->vu_intf->vrings[offset + VIRTIO_RXQ]);
435 vring->n_since_last_int += rv;
437 f64 now = vlib_time_now (vm);
438 if (vring->int_deadline < now ||
439 vring->n_since_last_int > dm->conf->vhost_coalesce_frames)
440 dpdk_vhost_user_send_interrupt(vm, xd, offset + VIRTIO_RXQ);
445 rte_pktmbuf_free (tx_vector[tx_tail+c]);
448 n_retry = (rv == DPDK_TX_RING_SIZE - tx_tail) ? 1 : 0;
452 *xd->lockp[queue_id] = 0;
456 else if (xd->dev_type == VNET_DPDK_DEV_KNI)
458 if (PREDICT_TRUE(tx_head > tx_tail))
460 /* no wrap, transmit in one burst */
461 rv = rte_kni_tx_burst(xd->kni,
463 (uint16_t) (tx_head-tx_tail));
468 * This can only happen if there is a flowcontrol callback.
469 * We need to split the transmit into two calls: one for
470 * the packets up to the wrap point, and one to continue
471 * at the start of the ring.
472 * Transmit pkts up to the wrap point.
474 rv = rte_kni_tx_burst(xd->kni,
476 (uint16_t) (DPDK_TX_RING_SIZE - tx_tail));
479 * If we transmitted everything we wanted, then allow 1 retry
480 * so we can try to transmit the rest. If we didn't transmit
481 * everything, stop now.
483 n_retry = (rv == DPDK_TX_RING_SIZE - tx_tail) ? 1 : 0;
493 if (PREDICT_FALSE(xd->dev_type != VNET_DPDK_DEV_VHOST_USER &&
495 *xd->lockp[queue_id] = 0;
497 if (PREDICT_FALSE(rv < 0))
499 // emit non-fatal message, bump counter
500 vnet_main_t * vnm = dm->vnet_main;
501 vnet_interface_main_t * im = &vnm->interface_main;
504 node_index = vec_elt_at_index(im->hw_interfaces,
505 xd->vlib_hw_if_index)->tx_node_index;
507 vlib_error_count (vm, node_index, DPDK_TX_FUNC_ERROR_BAD_RETVAL, 1);
508 clib_warning ("rte_eth_tx_burst[%d]: error %d", xd->device_index, rv);
509 return n_packets; // untransmitted packets
511 ring->tx_tail += (u16)rv;
512 n_packets -= (uint16_t) rv;
513 } while (rv && n_packets && (n_retry>0));
520 * This function transmits any packets on the interface's tx_vector and returns
521 * the number of packets untransmitted on the tx_vector. If the tx_vector is
522 * empty the function simply returns 0.
524 * It is intended to be called by a traffic manager which has flowed-off an
525 * interface to see if the interface can be flowed-on again.
527 u32 dpdk_interface_tx_vector (vlib_main_t * vm, u32 dev_instance)
529 dpdk_main_t * dm = &dpdk_main;
532 struct rte_mbuf ** tx_vector;
535 /* param is dev_instance and not hw_if_index to save another lookup */
536 xd = vec_elt_at_index (dm->devices, dev_instance);
538 queue_id = vm->cpu_index;
539 tx_vector = xd->tx_vectors[queue_id];
541 /* If no packets on the ring, don't bother calling tx function */
542 ring = vec_header(tx_vector, sizeof(*ring));
543 if (ring->tx_head == ring->tx_tail)
548 return tx_burst_vector_internal (vm, xd, tx_vector);
552 * Transmits the packets on the frame to the interface associated with the
553 * node. It first copies packets on the frame to a tx_vector containing the
554 * rte_mbuf pointers. It then passes this vector to tx_burst_vector_internal
555 * which calls the dpdk tx_burst function.
557 * The tx_vector is treated slightly differently depending on whether or
558 * not a flowcontrol callback function has been configured. If there is no
559 * callback, the tx_vector is a temporary array of rte_mbuf packet pointers.
560 * Its entries are written and consumed before the function exits.
562 * If there is a callback then the transmit is being invoked in the presence
563 * of a traffic manager. Here the tx_vector is treated like a ring of rte_mbuf
564 * pointers. If not all packets can be transmitted, the untransmitted packets
565 * stay on the tx_vector until the next call. The callback allows the traffic
566 * manager to flow-off dequeues to the interface. The companion function
567 * dpdk_interface_tx_vector() allows the traffic manager to detect when
568 * it should flow-on the interface again.
571 dpdk_interface_tx (vlib_main_t * vm,
572 vlib_node_runtime_t * node,
575 dpdk_main_t * dm = &dpdk_main;
576 vnet_interface_output_runtime_t * rd = (void *) node->runtime_data;
577 dpdk_device_t * xd = vec_elt_at_index (dm->devices, rd->dev_instance);
578 u32 n_packets = f->n_vectors;
581 struct rte_mbuf ** tx_vector;
589 my_cpu = vm->cpu_index;
593 tx_vector = xd->tx_vectors[queue_id];
594 ring = vec_header(tx_vector, sizeof(*ring));
596 n_on_ring = ring->tx_head - ring->tx_tail;
597 from = vlib_frame_vector_args (f);
599 ASSERT(n_packets <= VLIB_FRAME_SIZE);
601 if (PREDICT_FALSE(n_on_ring + n_packets > DPDK_TX_RING_SIZE))
604 * Overflowing the ring should never happen.
605 * If it does then drop the whole frame.
607 vlib_error_count (vm, node->node_index, DPDK_TX_FUNC_ERROR_RING_FULL,
612 u32 bi0 = from[n_packets];
613 vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
614 struct rte_mbuf *mb0 = rte_mbuf_from_vlib_buffer(b0);
615 rte_pktmbuf_free (mb0);
620 if (PREDICT_FALSE(dm->tx_pcap_enable))
626 vlib_buffer_t * b0 = vlib_get_buffer (vm, bi0);
627 if (dm->pcap_sw_if_index == 0 ||
628 dm->pcap_sw_if_index == vnet_buffer(b0)->sw_if_index [VLIB_TX])
629 pcap_add_buffer (&dm->pcap_main, vm, bi0, 512);
635 from = vlib_frame_vector_args (f);
637 i = ring->tx_head % DPDK_TX_RING_SIZE;
643 struct rte_mbuf * mb0, * mb1;
644 struct rte_mbuf * prefmb0, * prefmb1;
645 vlib_buffer_t * b0, * b1;
646 vlib_buffer_t * pref0, * pref1;
648 u16 new_data_len0, new_data_len1;
649 u16 new_pkt_len0, new_pkt_len1;
654 pref0 = vlib_get_buffer (vm, pi0);
655 pref1 = vlib_get_buffer (vm, pi1);
657 prefmb0 = rte_mbuf_from_vlib_buffer(pref0);
658 prefmb1 = rte_mbuf_from_vlib_buffer(pref1);
660 CLIB_PREFETCH(prefmb0, CLIB_CACHE_LINE_BYTES, LOAD);
661 CLIB_PREFETCH(pref0, CLIB_CACHE_LINE_BYTES, LOAD);
662 CLIB_PREFETCH(prefmb1, CLIB_CACHE_LINE_BYTES, LOAD);
663 CLIB_PREFETCH(pref1, CLIB_CACHE_LINE_BYTES, LOAD);
669 b0 = vlib_get_buffer (vm, bi0);
670 b1 = vlib_get_buffer (vm, bi1);
672 mb0 = rte_mbuf_from_vlib_buffer(b0);
673 mb1 = rte_mbuf_from_vlib_buffer(b1);
675 any_clone = (b0->flags & VLIB_BUFFER_RECYCLE)
676 | (b1->flags & VLIB_BUFFER_RECYCLE);
677 if (PREDICT_FALSE(any_clone != 0))
680 ((b0->flags & VLIB_BUFFER_RECYCLE) != 0))
682 struct rte_mbuf * mb0_new = dpdk_replicate_packet_mb (b0);
683 if (PREDICT_FALSE(mb0_new == 0))
685 vlib_error_count (vm, node->node_index,
686 DPDK_TX_FUNC_ERROR_REPL_FAIL, 1);
687 b0->flags |= VLIB_BUFFER_REPL_FAIL;
691 vec_add1 (dm->recycle[my_cpu], bi0);
694 ((b1->flags & VLIB_BUFFER_RECYCLE) != 0))
696 struct rte_mbuf * mb1_new = dpdk_replicate_packet_mb (b1);
697 if (PREDICT_FALSE(mb1_new == 0))
699 vlib_error_count (vm, node->node_index,
700 DPDK_TX_FUNC_ERROR_REPL_FAIL, 1);
701 b1->flags |= VLIB_BUFFER_REPL_FAIL;
705 vec_add1 (dm->recycle[my_cpu], bi1);
709 delta0 = PREDICT_FALSE(b0->flags & VLIB_BUFFER_REPL_FAIL) ? 0 :
710 vlib_buffer_length_in_chain (vm, b0) - (i16) mb0->pkt_len;
711 delta1 = PREDICT_FALSE(b1->flags & VLIB_BUFFER_REPL_FAIL) ? 0 :
712 vlib_buffer_length_in_chain (vm, b1) - (i16) mb1->pkt_len;
714 new_data_len0 = (u16)((i16) mb0->data_len + delta0);
715 new_data_len1 = (u16)((i16) mb1->data_len + delta1);
716 new_pkt_len0 = (u16)((i16) mb0->pkt_len + delta0);
717 new_pkt_len1 = (u16)((i16) mb1->pkt_len + delta1);
719 b0->current_length = new_data_len0;
720 b1->current_length = new_data_len1;
721 mb0->data_len = new_data_len0;
722 mb1->data_len = new_data_len1;
723 mb0->pkt_len = new_pkt_len0;
724 mb1->pkt_len = new_pkt_len1;
726 mb0->data_off = (PREDICT_FALSE(b0->flags & VLIB_BUFFER_REPL_FAIL)) ?
727 mb0->data_off : (u16)(RTE_PKTMBUF_HEADROOM + b0->current_data);
728 mb1->data_off = (PREDICT_FALSE(b1->flags & VLIB_BUFFER_REPL_FAIL)) ?
729 mb1->data_off : (u16)(RTE_PKTMBUF_HEADROOM + b1->current_data);
731 if (PREDICT_FALSE(node->flags & VLIB_NODE_FLAG_TRACE))
733 if (b0->flags & VLIB_BUFFER_IS_TRACED)
734 dpdk_tx_trace_buffer (dm, node, xd, queue_id, bi0, b0);
735 if (b1->flags & VLIB_BUFFER_IS_TRACED)
736 dpdk_tx_trace_buffer (dm, node, xd, queue_id, bi1, b1);
739 if (PREDICT_TRUE(any_clone == 0))
741 tx_vector[i % DPDK_TX_RING_SIZE] = mb0;
743 tx_vector[i % DPDK_TX_RING_SIZE] = mb1;
748 /* cloning was done, need to check for failure */
749 if (PREDICT_TRUE((b0->flags & VLIB_BUFFER_REPL_FAIL) == 0))
751 tx_vector[i % DPDK_TX_RING_SIZE] = mb0;
754 if (PREDICT_TRUE((b1->flags & VLIB_BUFFER_REPL_FAIL) == 0))
756 tx_vector[i % DPDK_TX_RING_SIZE] = mb1;
766 struct rte_mbuf * mb0;
775 b0 = vlib_get_buffer (vm, bi0);
777 mb0 = rte_mbuf_from_vlib_buffer(b0);
778 if (PREDICT_FALSE((b0->flags & VLIB_BUFFER_RECYCLE) != 0))
780 struct rte_mbuf * mb0_new = dpdk_replicate_packet_mb (b0);
781 if (PREDICT_FALSE(mb0_new == 0))
783 vlib_error_count (vm, node->node_index,
784 DPDK_TX_FUNC_ERROR_REPL_FAIL, 1);
785 b0->flags |= VLIB_BUFFER_REPL_FAIL;
789 vec_add1 (dm->recycle[my_cpu], bi0);
792 delta0 = PREDICT_FALSE(b0->flags & VLIB_BUFFER_REPL_FAIL) ? 0 :
793 vlib_buffer_length_in_chain (vm, b0) - (i16) mb0->pkt_len;
795 new_data_len0 = (u16)((i16) mb0->data_len + delta0);
796 new_pkt_len0 = (u16)((i16) mb0->pkt_len + delta0);
798 b0->current_length = new_data_len0;
799 mb0->data_len = new_data_len0;
800 mb0->pkt_len = new_pkt_len0;
801 mb0->data_off = (PREDICT_FALSE(b0->flags & VLIB_BUFFER_REPL_FAIL)) ?
802 mb0->data_off : (u16)(RTE_PKTMBUF_HEADROOM + b0->current_data);
804 if (PREDICT_FALSE(node->flags & VLIB_NODE_FLAG_TRACE))
805 if (b0->flags & VLIB_BUFFER_IS_TRACED)
806 dpdk_tx_trace_buffer (dm, node, xd, queue_id, bi0, b0);
808 if (PREDICT_TRUE((b0->flags & VLIB_BUFFER_REPL_FAIL) == 0))
810 tx_vector[i % DPDK_TX_RING_SIZE] = mb0;
816 /* account for additional packets in the ring */
817 ring->tx_head += n_packets;
818 n_on_ring = ring->tx_head - ring->tx_tail;
820 /* transmit as many packets as possible */
821 n_packets = tx_burst_vector_internal (vm, xd, tx_vector);
824 * tx_pkts is the number of packets successfully transmitted
825 * This is the number originally on ring minus the number remaining on ring
827 tx_pkts = n_on_ring - n_packets;
829 if (PREDICT_FALSE(dm->flowcontrol_callback != 0))
831 if (PREDICT_FALSE(n_packets))
833 /* Callback may want to enable flowcontrol */
834 dm->flowcontrol_callback(vm, xd->vlib_hw_if_index, ring->tx_head - ring->tx_tail);
838 /* Reset head/tail to avoid unnecessary wrap */
845 /* If there is no callback then drop any non-transmitted packets */
846 if (PREDICT_FALSE(n_packets))
848 vlib_simple_counter_main_t * cm;
849 vnet_main_t * vnm = vnet_get_main();
851 cm = vec_elt_at_index (vnm->interface_main.sw_if_counters,
852 VNET_INTERFACE_COUNTER_TX_ERROR);
854 vlib_increment_simple_counter (cm, my_cpu, xd->vlib_sw_if_index, n_packets);
856 vlib_error_count (vm, node->node_index, DPDK_TX_FUNC_ERROR_PKT_DROP,
860 rte_pktmbuf_free (tx_vector[ring->tx_tail + n_packets]);
863 /* Reset head/tail to avoid unnecessary wrap */
868 /* Recycle replicated buffers */
869 if (PREDICT_FALSE(vec_len(dm->recycle[my_cpu])))
871 vlib_buffer_free (vm, dm->recycle[my_cpu], vec_len(dm->recycle[my_cpu]));
872 _vec_len(dm->recycle[my_cpu]) = 0;
875 ASSERT(ring->tx_head >= ring->tx_tail);
880 static int dpdk_device_renumber (vnet_hw_interface_t * hi,
881 u32 new_dev_instance)
884 dpdk_main_t * dm = &dpdk_main;
885 dpdk_device_t * xd = vec_elt_at_index (dm->devices, hi->dev_instance);
887 if (!xd || xd->dev_type != VNET_DPDK_DEV_VHOST_USER) {
888 clib_warning("cannot renumber non-vhost-user interface (sw_if_index: %d)",
893 xd->vu_if_id = new_dev_instance;
898 static void dpdk_clear_hw_interface_counters (u32 instance)
900 dpdk_main_t * dm = &dpdk_main;
901 dpdk_device_t * xd = vec_elt_at_index (dm->devices, instance);
904 * DAW-FIXME: VMXNET3 device stop/start doesn't work,
905 * therefore fake the stop in the dpdk driver by
906 * silently dropping all of the incoming pkts instead of
907 * stopping the driver / hardware.
909 if (xd->admin_up != 0xff)
912 * Set the "last_cleared_stats" to the current stats, so that
913 * things appear to clear from a display perspective.
915 dpdk_update_counters (xd, vlib_time_now (dm->vlib_main));
917 clib_memcpy (&xd->last_cleared_stats, &xd->stats, sizeof(xd->stats));
918 clib_memcpy (xd->last_cleared_xstats, xd->xstats,
919 vec_len(xd->last_cleared_xstats) *
920 sizeof(xd->last_cleared_xstats[0]));
925 * Internally rte_eth_xstats_reset() is calling rte_eth_stats_reset(),
926 * so we're only calling xstats_reset() here.
928 rte_eth_xstats_reset (xd->device_index);
929 memset (&xd->stats, 0, sizeof(xd->stats));
930 memset (&xd->last_stats, 0, sizeof (xd->last_stats));
934 if (PREDICT_FALSE(xd->dev_type == VNET_DPDK_DEV_VHOST_USER)) {
936 for (i = 0; i < xd->rx_q_used * VIRTIO_QNUM; i++) {
937 xd->vu_intf->vrings[i].packets = 0;
938 xd->vu_intf->vrings[i].bytes = 0;
944 #ifdef RTE_LIBRTE_KNI
946 kni_config_network_if(u8 port_id, u8 if_up)
948 vnet_main_t * vnm = vnet_get_main();
949 dpdk_main_t * dm = &dpdk_main;
953 p = hash_get (dm->dpdk_device_by_kni_port_id, port_id);
955 clib_warning("unknown interface");
958 xd = vec_elt_at_index (dm->devices, p[0]);
961 vnet_hw_interface_set_flags (vnm, xd->vlib_hw_if_index,
962 if_up ? VNET_HW_INTERFACE_FLAG_LINK_UP |
963 ETH_LINK_FULL_DUPLEX : 0);
968 kni_change_mtu(u8 port_id, unsigned new_mtu)
970 vnet_main_t * vnm = vnet_get_main();
971 dpdk_main_t * dm = &dpdk_main;
974 vnet_hw_interface_t * hif;
976 p = hash_get (dm->dpdk_device_by_kni_port_id, port_id);
978 clib_warning("unknown interface");
981 xd = vec_elt_at_index (dm->devices, p[0]);
983 hif = vnet_get_hw_interface (vnm, xd->vlib_hw_if_index);
985 hif->max_packet_bytes = new_mtu;
991 static clib_error_t *
992 dpdk_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
994 vnet_hw_interface_t * hif = vnet_get_hw_interface (vnm, hw_if_index);
995 uword is_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
996 dpdk_main_t * dm = &dpdk_main;
997 dpdk_device_t * xd = vec_elt_at_index (dm->devices, hif->dev_instance);
1000 #ifdef RTE_LIBRTE_KNI
1001 if (xd->dev_type == VNET_DPDK_DEV_KNI)
1005 struct rte_kni_conf conf;
1006 struct rte_kni_ops ops;
1007 vlib_main_t * vm = vlib_get_main();
1008 vlib_buffer_main_t * bm = vm->buffer_main;
1009 memset(&conf, 0, sizeof(conf));
1010 snprintf(conf.name, RTE_KNI_NAMESIZE, "vpp%u", xd->kni_port_id);
1011 conf.mbuf_size = VLIB_BUFFER_DATA_SIZE;
1012 memset(&ops, 0, sizeof(ops));
1013 ops.port_id = xd->kni_port_id;
1014 ops.change_mtu = kni_change_mtu;
1015 ops.config_network_if = kni_config_network_if;
1017 xd->kni = rte_kni_alloc(bm->pktmbuf_pools[rte_socket_id()], &conf, &ops);
1020 clib_warning("failed to allocate kni interface");
1024 hif->max_packet_bytes = 1500; /* kni interface default value */
1031 rte_kni_release(xd->kni);
1037 if (xd->dev_type == VNET_DPDK_DEV_VHOST_USER)
1041 if (xd->vu_is_running)
1042 vnet_hw_interface_set_flags (vnm, xd->vlib_hw_if_index,
1043 VNET_HW_INTERFACE_FLAG_LINK_UP |
1044 ETH_LINK_FULL_DUPLEX );
1049 vnet_hw_interface_set_flags (vnm, xd->vlib_hw_if_index, 0);
1060 f64 now = vlib_time_now (dm->vlib_main);
1063 * DAW-FIXME: VMXNET3 device stop/start doesn't work,
1064 * therefore fake the stop in the dpdk driver by
1065 * silently dropping all of the incoming pkts instead of
1066 * stopping the driver / hardware.
1068 if (xd->admin_up == 0)
1069 rv = rte_eth_dev_start (xd->device_index);
1072 rte_eth_promiscuous_enable(xd->device_index);
1074 rte_eth_promiscuous_disable(xd->device_index);
1076 rte_eth_allmulticast_enable (xd->device_index);
1078 dpdk_update_counters (xd, now);
1079 dpdk_update_link_state (xd, now);
1084 * DAW-FIXME: VMXNET3 device stop/start doesn't work,
1085 * therefore fake the stop in the dpdk driver by
1086 * silently dropping all of the incoming pkts instead of
1087 * stopping the driver / hardware.
1089 if (xd->pmd != VNET_DPDK_PMD_VMXNET3)
1094 rte_eth_allmulticast_disable (xd->device_index);
1095 vnet_hw_interface_set_flags (vnm, xd->vlib_hw_if_index, 0);
1098 * DAW-FIXME: VMXNET3 device stop/start doesn't work,
1099 * therefore fake the stop in the dpdk driver by
1100 * silently dropping all of the incoming pkts instead of
1101 * stopping the driver / hardware.
1103 if (xd->pmd != VNET_DPDK_PMD_VMXNET3)
1104 rte_eth_dev_stop (xd->device_index);
1106 /* For bonded interface, stop slave links */
1107 if (xd->pmd == VNET_DPDK_PMD_BOND)
1110 int nlink = rte_eth_bond_slaves_get(xd->device_index, slink, 16);
1113 u8 dpdk_port = slink[--nlink];
1114 rte_eth_dev_stop (dpdk_port);
1120 clib_warning ("rte_eth_dev_%s error: %d", is_up ? "start" : "stop",
1123 return /* no error */ 0;
1127 * Dynamically redirect all pkts from a specific interface
1128 * to the specified node
1130 static void dpdk_set_interface_next_node (vnet_main_t *vnm, u32 hw_if_index,
1133 dpdk_main_t * xm = &dpdk_main;
1134 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
1135 dpdk_device_t * xd = vec_elt_at_index (xm->devices, hw->dev_instance);
1137 /* Shut off redirection */
1138 if (node_index == ~0)
1140 xd->per_interface_next_index = node_index;
1144 xd->per_interface_next_index =
1145 vlib_node_add_next (xm->vlib_main, dpdk_input_node.index, node_index);
1149 static clib_error_t *
1150 dpdk_subif_add_del_function (vnet_main_t * vnm,
1152 struct vnet_sw_interface_t * st,
1155 dpdk_main_t * xm = &dpdk_main;
1156 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
1157 dpdk_device_t * xd = vec_elt_at_index (xm->devices, hw->dev_instance);
1158 vnet_sw_interface_t * t = (vnet_sw_interface_t *) st;
1159 int r, vlan_offload;
1160 u32 prev_subifs = xd->vlan_subifs;
1162 if (is_add) xd->vlan_subifs++;
1163 else if (xd->vlan_subifs) xd->vlan_subifs--;
1165 if (xd->dev_type != VNET_DPDK_DEV_ETH)
1168 /* currently we program VLANS only for IXGBE VF and I40E VF */
1169 if ((xd->pmd != VNET_DPDK_PMD_IXGBEVF) &&
1170 (xd->pmd != VNET_DPDK_PMD_I40EVF))
1173 if (t->sub.eth.flags.no_tags == 1)
1176 if ((t->sub.eth.flags.one_tag != 1) || (t->sub.eth.flags.exact_match != 1 )) {
1177 xd->vlan_subifs = prev_subifs;
1178 return clib_error_return (0, "unsupported VLAN setup");
1181 vlan_offload = rte_eth_dev_get_vlan_offload(xd->device_index);
1182 vlan_offload |= ETH_VLAN_FILTER_OFFLOAD;
1184 if ((r = rte_eth_dev_set_vlan_offload(xd->device_index, vlan_offload))) {
1185 xd->vlan_subifs = prev_subifs;
1186 return clib_error_return (0, "rte_eth_dev_set_vlan_offload[%d]: err %d",
1187 xd->device_index, r);
1191 if ((r = rte_eth_dev_vlan_filter(xd->device_index, t->sub.eth.outer_vlan_id, is_add))) {
1192 xd->vlan_subifs = prev_subifs;
1193 return clib_error_return (0, "rte_eth_dev_vlan_filter[%d]: err %d",
1194 xd->device_index, r);
1200 VNET_DEVICE_CLASS (dpdk_device_class) = {
1202 .tx_function = dpdk_interface_tx,
1203 .tx_function_n_errors = DPDK_TX_FUNC_N_ERROR,
1204 .tx_function_error_strings = dpdk_tx_func_error_strings,
1205 .format_device_name = format_dpdk_device_name,
1206 .format_device = format_dpdk_device,
1207 .format_tx_trace = format_dpdk_tx_dma_trace,
1208 .clear_counters = dpdk_clear_hw_interface_counters,
1209 .admin_up_down_function = dpdk_interface_admin_up_down,
1210 .subif_add_del_function = dpdk_subif_add_del_function,
1211 .rx_redirect_to_node = dpdk_set_interface_next_node,
1212 .no_flatten_output_chains = 1,
1213 .name_renumber = dpdk_device_renumber,
1216 VLIB_DEVICE_TX_FUNCTION_MULTIARCH (dpdk_device_class,
1219 void dpdk_set_flowcontrol_callback (vlib_main_t *vm,
1220 dpdk_flowcontrol_callback_t callback)
1222 dpdk_main.flowcontrol_callback = callback;
1225 #define UP_DOWN_FLAG_EVENT 1
1228 u32 dpdk_get_admin_up_down_in_progress (void)
1230 return dpdk_main.admin_up_down_in_progress;
1234 admin_up_down_process (vlib_main_t * vm,
1235 vlib_node_runtime_t * rt,
1238 clib_error_t * error = 0;
1240 uword *event_data = 0;
1246 vlib_process_wait_for_event (vm);
1248 event_type = vlib_process_get_events (vm, &event_data);
1250 dpdk_main.admin_up_down_in_progress = 1;
1252 switch (event_type) {
1253 case UP_DOWN_FLAG_EVENT:
1255 if (vec_len(event_data) == 2) {
1256 sw_if_index = event_data[0];
1257 flags = event_data[1];
1258 error = vnet_sw_interface_set_flags (vnet_get_main(), sw_if_index, flags);
1259 clib_error_report(error);
1265 vec_reset_length (event_data);
1267 dpdk_main.admin_up_down_in_progress = 0;
1270 return 0; /* or not */
1273 VLIB_REGISTER_NODE (admin_up_down_process_node,static) = {
1274 .function = admin_up_down_process,
1275 .type = VLIB_NODE_TYPE_PROCESS,
1276 .name = "admin-up-down-process",
1277 .process_log2_n_stack_bytes = 17, // 256KB
1281 * Asynchronously invoke vnet_sw_interface_set_flags via the admin_up_down
1282 * process. Useful for avoiding long blocking delays (>150ms) in the dpdk
1284 * WARNING: when posting this event, no other interface-related calls should
1285 * be made (e.g. vnet_create_sw_interface()) while the event is being
1286 * processed (admin_up_down_in_progress). This is required in order to avoid
1287 * race conditions in manipulating interface data structures.
1289 void post_sw_interface_set_flags (vlib_main_t *vm, u32 sw_if_index, u32 flags)
1291 uword * d = vlib_process_signal_event_data
1292 (vm, admin_up_down_process_node.index,
1293 UP_DOWN_FLAG_EVENT, 2, sizeof(u32));
1299 * Return a copy of the DPDK port stats in dest.
1302 dpdk_get_hw_interface_stats (u32 hw_if_index, struct rte_eth_stats* dest)
1304 dpdk_main_t * dm = &dpdk_main;
1305 vnet_main_t * vnm = vnet_get_main();
1306 vnet_hw_interface_t * hi = vnet_get_hw_interface (vnm, hw_if_index);
1307 dpdk_device_t * xd = vec_elt_at_index (dm->devices, hi->dev_instance);
1310 return clib_error_return (0, "Missing or NULL argument");
1313 return clib_error_return (0, "Unable to get DPDK device from HW interface");
1316 dpdk_update_counters (xd, vlib_time_now (dm->vlib_main));
1318 clib_memcpy(dest, &xd->stats, sizeof(xd->stats));
1323 * Return the number of dpdk mbufs
1325 u32 dpdk_num_mbufs (void)
1327 dpdk_main_t * dm = &dpdk_main;
1329 return dm->conf->num_mbufs;
1333 * Return the pmd type for a given hardware interface
1335 dpdk_pmd_t dpdk_get_pmd_type (vnet_hw_interface_t *hi)
1337 dpdk_main_t * dm = &dpdk_main;
1342 xd = vec_elt_at_index (dm->devices, hi->dev_instance);
1350 * Return the cpu socket for a given hardware interface
1352 i8 dpdk_get_cpu_socket (vnet_hw_interface_t *hi)
1354 dpdk_main_t * dm = &dpdk_main;
1359 xd = vec_elt_at_index(dm->devices, hi->dev_instance);
1363 return xd->cpu_socket;