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)")
35 #define _(f,s) DPDK_TX_FUNC_ERROR_##f,
36 foreach_dpdk_tx_func_error
39 } dpdk_tx_func_error_t;
41 static char *dpdk_tx_func_error_strings[] = {
43 foreach_dpdk_tx_func_error
48 dpdk_set_mac_address (vnet_hw_interface_t * hi, char *address)
51 dpdk_main_t *dm = &dpdk_main;
52 dpdk_device_t *xd = vec_elt_at_index (dm->devices, hi->dev_instance);
54 error = rte_eth_dev_default_mac_addr_set (xd->device_index,
55 (struct ether_addr *) address);
59 return clib_error_return (0, "mac address set failed: %d", error);
68 dpdk_set_mc_filter (vnet_hw_interface_t * hi,
69 struct ether_addr mc_addr_vec[], int naddr)
72 dpdk_main_t *dm = &dpdk_main;
73 dpdk_device_t *xd = vec_elt_at_index (dm->devices, hi->dev_instance);
75 error = rte_eth_dev_set_mc_addr_list (xd->device_index, mc_addr_vec, naddr);
79 return clib_error_return (0, "mc addr list failed: %d", error);
88 dpdk_replicate_packet_mb (vlib_buffer_t * b)
90 vlib_main_t *vm = vlib_get_main ();
91 vlib_buffer_main_t *bm = vm->buffer_main;
92 struct rte_mbuf *first_mb = 0, *new_mb, *pkt_mb, **prev_mb_next = 0;
93 u8 nb_segs, nb_segs_left;
95 unsigned socket_id = rte_socket_id ();
97 ASSERT (bm->pktmbuf_pools[socket_id]);
98 pkt_mb = rte_mbuf_from_vlib_buffer (b);
99 nb_segs = pkt_mb->nb_segs;
100 for (nb_segs_left = nb_segs; nb_segs_left; nb_segs_left--)
102 if (PREDICT_FALSE (pkt_mb == 0))
104 clib_warning ("Missing %d mbuf chain segment(s): "
105 "(nb_segs = %d, nb_segs_left = %d)!",
106 nb_segs - nb_segs_left, nb_segs, nb_segs_left);
108 rte_pktmbuf_free (first_mb);
111 new_mb = rte_pktmbuf_alloc (bm->pktmbuf_pools[socket_id]);
112 if (PREDICT_FALSE (new_mb == 0))
115 rte_pktmbuf_free (first_mb);
120 * Copy packet info into 1st segment.
125 rte_pktmbuf_pkt_len (first_mb) = pkt_mb->pkt_len;
126 first_mb->nb_segs = pkt_mb->nb_segs;
127 first_mb->port = pkt_mb->port;
128 #ifdef DAW_FIXME // TX Offload support TBD
129 first_mb->vlan_macip = pkt_mb->vlan_macip;
130 first_mb->hash = pkt_mb->hash;
131 first_mb->ol_flags = pkt_mb->ol_flags
136 ASSERT (prev_mb_next != 0);
137 *prev_mb_next = new_mb;
141 * Copy packet segment data into new mbuf segment.
143 rte_pktmbuf_data_len (new_mb) = pkt_mb->data_len;
144 copy_bytes = pkt_mb->data_len + RTE_PKTMBUF_HEADROOM;
145 ASSERT (copy_bytes <= pkt_mb->buf_len);
146 clib_memcpy (new_mb->buf_addr, pkt_mb->buf_addr, copy_bytes);
148 prev_mb_next = &new_mb->next;
149 pkt_mb = pkt_mb->next;
152 ASSERT (pkt_mb == 0);
153 __rte_mbuf_sanity_check (first_mb, 1);
159 dpdk_zerocopy_replicate_packet_mb (vlib_buffer_t * b)
161 vlib_main_t *vm = vlib_get_main ();
162 vlib_buffer_main_t *bm = vm->buffer_main;
163 struct rte_mbuf *first_mb = 0, *new_mb, *pkt_mb, **prev_mb_next = 0;
164 u8 nb_segs, nb_segs_left;
165 unsigned socket_id = rte_socket_id ();
167 ASSERT (bm->pktmbuf_pools[socket_id]);
168 pkt_mb = rte_mbuf_from_vlib_buffer (b);
169 nb_segs = pkt_mb->nb_segs;
170 for (nb_segs_left = nb_segs; nb_segs_left; nb_segs_left--)
172 if (PREDICT_FALSE (pkt_mb == 0))
174 clib_warning ("Missing %d mbuf chain segment(s): "
175 "(nb_segs = %d, nb_segs_left = %d)!",
176 nb_segs - nb_segs_left, nb_segs, nb_segs_left);
178 rte_pktmbuf_free (first_mb);
181 new_mb = rte_pktmbuf_clone (pkt_mb, bm->pktmbuf_pools[socket_id]);
182 if (PREDICT_FALSE (new_mb == 0))
185 rte_pktmbuf_free (first_mb);
190 * Copy packet info into 1st segment.
195 rte_pktmbuf_pkt_len (first_mb) = pkt_mb->pkt_len;
196 first_mb->nb_segs = pkt_mb->nb_segs;
197 first_mb->port = pkt_mb->port;
198 #ifdef DAW_FIXME // TX Offload support TBD
199 first_mb->vlan_macip = pkt_mb->vlan_macip;
200 first_mb->hash = pkt_mb->hash;
201 first_mb->ol_flags = pkt_mb->ol_flags
206 ASSERT (prev_mb_next != 0);
207 *prev_mb_next = new_mb;
211 * Copy packet segment data into new mbuf segment.
213 rte_pktmbuf_data_len (new_mb) = pkt_mb->data_len;
215 prev_mb_next = &new_mb->next;
216 pkt_mb = pkt_mb->next;
219 ASSERT (pkt_mb == 0);
220 __rte_mbuf_sanity_check (first_mb, 1);
228 dpdk_tx_trace_buffer (dpdk_main_t * dm,
229 vlib_node_runtime_t * node,
231 u16 queue_id, u32 buffer_index, vlib_buffer_t * buffer)
233 vlib_main_t *vm = vlib_get_main ();
234 dpdk_tx_dma_trace_t *t0;
237 mb = rte_mbuf_from_vlib_buffer (buffer);
239 t0 = vlib_add_trace (vm, node, buffer, sizeof (t0[0]));
240 t0->queue_index = queue_id;
241 t0->device_index = xd->device_index;
242 t0->buffer_index = buffer_index;
243 clib_memcpy (&t0->mb, mb, sizeof (t0->mb));
244 clib_memcpy (&t0->buffer, buffer,
245 sizeof (buffer[0]) - sizeof (buffer->pre_data));
246 clib_memcpy (t0->buffer.pre_data, buffer->data + buffer->current_data,
247 sizeof (t0->buffer.pre_data));
251 * This function calls the dpdk's tx_burst function to transmit the packets
252 * on the tx_vector. It manages a lock per-device if the device does not
253 * support multiple queues. It returns the number of packets untransmitted
254 * on the tx_vector. If all packets are transmitted (the normal case), the
255 * function returns 0.
257 * The tx_burst function may not be able to transmit all packets because the
258 * dpdk ring is full. If a flowcontrol callback function has been configured
259 * then the function simply returns. If no callback has been configured, the
260 * function will retry calling tx_burst with the remaining packets. This will
261 * continue until all packets are transmitted or tx_burst indicates no packets
262 * could be transmitted. (The caller can drop the remaining packets.)
264 * The function assumes there is at least one packet on the tx_vector.
267 u32 tx_burst_vector_internal (vlib_main_t * vm,
269 struct rte_mbuf **tx_vector)
271 dpdk_main_t *dm = &dpdk_main;
280 ring = vec_header (tx_vector, sizeof (*ring));
282 n_packets = ring->tx_head - ring->tx_tail;
284 tx_head = ring->tx_head % xd->nb_tx_desc;
287 * Ensure rte_eth_tx_burst is not called with 0 packets, which can lead to
288 * unpredictable results.
290 ASSERT (n_packets > 0);
293 * Check for tx_vector overflow. If this fails it is a system configuration
294 * error. The ring should be sized big enough to handle the largest un-flowed
295 * off burst from a traffic manager. A larger size also helps performance
296 * a bit because it decreases the probability of having to issue two tx_burst
297 * calls due to a ring wrap.
299 ASSERT (n_packets < xd->nb_tx_desc);
302 * If there is no flowcontrol callback, there is only temporary buffering
303 * on the tx_vector and so the tail should always be 0.
305 ASSERT (dm->flowcontrol_callback || ring->tx_tail == 0);
308 * If there is a flowcontrol callback, don't retry any incomplete tx_bursts.
309 * Apply backpressure instead. If there is no callback, keep retrying until
310 * a tx_burst sends no packets. n_retry of 255 essentially means no retry
313 n_retry = dm->flowcontrol_callback ? 0 : 255;
315 queue_id = vm->cpu_index;
319 /* start the burst at the tail */
320 tx_tail = ring->tx_tail % xd->nb_tx_desc;
323 * This device only supports one TX queue,
324 * and we're running multi-threaded...
326 if (PREDICT_FALSE ((xd->flags & DPDK_DEVICE_FLAG_VHOST_USER) == 0 &&
329 queue_id = queue_id % xd->tx_q_used;
330 while (__sync_lock_test_and_set (xd->lockp[queue_id], 1))
332 queue_id = (queue_id + 1) % xd->tx_q_used;
335 if (PREDICT_TRUE (xd->flags & DPDK_DEVICE_FLAG_PMD))
337 if (PREDICT_TRUE (tx_head > tx_tail))
339 /* no wrap, transmit in one burst */
340 rv = rte_eth_tx_burst (xd->device_index,
343 (uint16_t) (tx_head - tx_tail));
348 * This can only happen if there is a flowcontrol callback.
349 * We need to split the transmit into two calls: one for
350 * the packets up to the wrap point, and one to continue
351 * at the start of the ring.
352 * Transmit pkts up to the wrap point.
354 rv = rte_eth_tx_burst (xd->device_index,
357 (uint16_t) (xd->nb_tx_desc - tx_tail));
360 * If we transmitted everything we wanted, then allow 1 retry
361 * so we can try to transmit the rest. If we didn't transmit
362 * everything, stop now.
364 n_retry = (rv == xd->nb_tx_desc - tx_tail) ? 1 : 0;
368 else if (xd->flags & DPDK_DEVICE_FLAG_VHOST_USER)
374 while (__sync_lock_test_and_set (xd->lockp[queue_id], 1));
378 dpdk_device_and_queue_t *dq;
379 vec_foreach (dq, dm->devices_by_cpu[vm->cpu_index])
381 if (xd->device_index == dq->device)
385 offset = dq->queue_id * VIRTIO_QNUM;
387 if (PREDICT_TRUE (tx_head > tx_tail))
391 struct rte_mbuf **pkts = &tx_vector[tx_tail];
392 for (i = 0; i < (tx_head - tx_tail); i++)
394 struct rte_mbuf *buff = pkts[i];
395 bytes += rte_pktmbuf_data_len (buff);
398 /* no wrap, transmit in one burst */
400 rte_vhost_enqueue_burst (&xd->vu_vhost_dev,
403 (uint16_t) (tx_head - tx_tail));
404 if (PREDICT_TRUE (rv > 0))
406 dpdk_vu_vring *vring =
407 &(xd->vu_intf->vrings[offset + VIRTIO_TXQ]);
408 vring->packets += rv;
409 vring->bytes += bytes;
411 if (dpdk_vhost_user_want_interrupt
412 (xd, offset + VIRTIO_RXQ))
414 vring = &(xd->vu_intf->vrings[offset + VIRTIO_RXQ]);
415 vring->n_since_last_int += rv;
417 f64 now = vlib_time_now (vm);
418 if (vring->int_deadline < now ||
419 vring->n_since_last_int >
420 dm->conf->vhost_coalesce_frames)
421 dpdk_vhost_user_send_interrupt (vm, xd,
422 offset + VIRTIO_RXQ);
427 rte_pktmbuf_free (tx_vector[tx_tail + c]);
433 * If we transmitted everything we wanted, then allow 1 retry
434 * so we can try to transmit the rest. If we didn't transmit
435 * everything, stop now.
439 struct rte_mbuf **pkts = &tx_vector[tx_tail];
440 for (i = 0; i < (xd->nb_tx_desc - tx_tail); i++)
442 struct rte_mbuf *buff = pkts[i];
443 bytes += rte_pktmbuf_data_len (buff);
446 rte_vhost_enqueue_burst (&xd->vu_vhost_dev,
449 (uint16_t) (xd->nb_tx_desc -
452 if (PREDICT_TRUE (rv > 0))
454 dpdk_vu_vring *vring =
455 &(xd->vu_intf->vrings[offset + VIRTIO_TXQ]);
456 vring->packets += rv;
457 vring->bytes += bytes;
459 if (dpdk_vhost_user_want_interrupt
460 (xd, offset + VIRTIO_RXQ))
462 vring = &(xd->vu_intf->vrings[offset + VIRTIO_RXQ]);
463 vring->n_since_last_int += rv;
465 f64 now = vlib_time_now (vm);
466 if (vring->int_deadline < now ||
467 vring->n_since_last_int >
468 dm->conf->vhost_coalesce_frames)
469 dpdk_vhost_user_send_interrupt (vm, xd,
470 offset + VIRTIO_RXQ);
475 rte_pktmbuf_free (tx_vector[tx_tail + c]);
478 n_retry = (rv == xd->nb_tx_desc - tx_tail) ? 1 : 0;
482 *xd->lockp[queue_id] = 0;
486 else if (xd->flags & DPDK_DEVICE_FLAG_KNI)
488 if (PREDICT_TRUE (tx_head > tx_tail))
490 /* no wrap, transmit in one burst */
491 rv = rte_kni_tx_burst (xd->kni,
493 (uint16_t) (tx_head - tx_tail));
498 * This can only happen if there is a flowcontrol callback.
499 * We need to split the transmit into two calls: one for
500 * the packets up to the wrap point, and one to continue
501 * at the start of the ring.
502 * Transmit pkts up to the wrap point.
504 rv = rte_kni_tx_burst (xd->kni,
506 (uint16_t) (xd->nb_tx_desc - tx_tail));
509 * If we transmitted everything we wanted, then allow 1 retry
510 * so we can try to transmit the rest. If we didn't transmit
511 * everything, stop now.
513 n_retry = (rv == xd->nb_tx_desc - tx_tail) ? 1 : 0;
523 if (PREDICT_FALSE ((xd->flags & DPDK_DEVICE_FLAG_VHOST_USER) == 0 &&
525 *xd->lockp[queue_id] = 0;
527 if (PREDICT_FALSE (rv < 0))
529 // emit non-fatal message, bump counter
530 vnet_main_t *vnm = dm->vnet_main;
531 vnet_interface_main_t *im = &vnm->interface_main;
534 node_index = vec_elt_at_index (im->hw_interfaces,
535 xd->vlib_hw_if_index)->tx_node_index;
537 vlib_error_count (vm, node_index, DPDK_TX_FUNC_ERROR_BAD_RETVAL, 1);
538 clib_warning ("rte_eth_tx_burst[%d]: error %d", xd->device_index,
540 return n_packets; // untransmitted packets
542 ring->tx_tail += (u16) rv;
543 n_packets -= (uint16_t) rv;
545 while (rv && n_packets && (n_retry > 0));
552 * This function transmits any packets on the interface's tx_vector and returns
553 * the number of packets untransmitted on the tx_vector. If the tx_vector is
554 * empty the function simply returns 0.
556 * It is intended to be called by a traffic manager which has flowed-off an
557 * interface to see if the interface can be flowed-on again.
560 dpdk_interface_tx_vector (vlib_main_t * vm, u32 dev_instance)
562 dpdk_main_t *dm = &dpdk_main;
565 struct rte_mbuf **tx_vector;
568 /* param is dev_instance and not hw_if_index to save another lookup */
569 xd = vec_elt_at_index (dm->devices, dev_instance);
571 queue_id = vm->cpu_index;
572 tx_vector = xd->tx_vectors[queue_id];
574 /* If no packets on the ring, don't bother calling tx function */
575 ring = vec_header (tx_vector, sizeof (*ring));
576 if (ring->tx_head == ring->tx_tail)
581 return tx_burst_vector_internal (vm, xd, tx_vector);
585 * Transmits the packets on the frame to the interface associated with the
586 * node. It first copies packets on the frame to a tx_vector containing the
587 * rte_mbuf pointers. It then passes this vector to tx_burst_vector_internal
588 * which calls the dpdk tx_burst function.
590 * The tx_vector is treated slightly differently depending on whether or
591 * not a flowcontrol callback function has been configured. If there is no
592 * callback, the tx_vector is a temporary array of rte_mbuf packet pointers.
593 * Its entries are written and consumed before the function exits.
595 * If there is a callback then the transmit is being invoked in the presence
596 * of a traffic manager. Here the tx_vector is treated like a ring of rte_mbuf
597 * pointers. If not all packets can be transmitted, the untransmitted packets
598 * stay on the tx_vector until the next call. The callback allows the traffic
599 * manager to flow-off dequeues to the interface. The companion function
600 * dpdk_interface_tx_vector() allows the traffic manager to detect when
601 * it should flow-on the interface again.
604 dpdk_interface_tx (vlib_main_t * vm,
605 vlib_node_runtime_t * node, vlib_frame_t * f)
607 dpdk_main_t *dm = &dpdk_main;
608 vnet_interface_output_runtime_t *rd = (void *) node->runtime_data;
609 dpdk_device_t *xd = vec_elt_at_index (dm->devices, rd->dev_instance);
610 u32 n_packets = f->n_vectors;
613 struct rte_mbuf **tx_vector;
621 my_cpu = vm->cpu_index;
625 tx_vector = xd->tx_vectors[queue_id];
626 ring = vec_header (tx_vector, sizeof (*ring));
628 n_on_ring = ring->tx_head - ring->tx_tail;
629 from = vlib_frame_vector_args (f);
631 ASSERT (n_packets <= VLIB_FRAME_SIZE);
633 if (PREDICT_FALSE (n_on_ring + n_packets > xd->nb_tx_desc))
636 * Overflowing the ring should never happen.
637 * If it does then drop the whole frame.
639 vlib_error_count (vm, node->node_index, DPDK_TX_FUNC_ERROR_RING_FULL,
644 u32 bi0 = from[n_packets];
645 vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
646 struct rte_mbuf *mb0 = rte_mbuf_from_vlib_buffer (b0);
647 rte_pktmbuf_free (mb0);
652 if (PREDICT_FALSE (dm->tx_pcap_enable))
658 vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
659 if (dm->pcap_sw_if_index == 0 ||
660 dm->pcap_sw_if_index == vnet_buffer (b0)->sw_if_index[VLIB_TX])
661 pcap_add_buffer (&dm->pcap_main, vm, bi0, 512);
667 from = vlib_frame_vector_args (f);
669 i = ring->tx_head % xd->nb_tx_desc;
675 struct rte_mbuf *mb0, *mb1;
676 struct rte_mbuf *prefmb0, *prefmb1;
677 vlib_buffer_t *b0, *b1;
678 vlib_buffer_t *pref0, *pref1;
680 u16 new_data_len0, new_data_len1;
681 u16 new_pkt_len0, new_pkt_len1;
686 pref0 = vlib_get_buffer (vm, pi0);
687 pref1 = vlib_get_buffer (vm, pi1);
689 prefmb0 = rte_mbuf_from_vlib_buffer (pref0);
690 prefmb1 = rte_mbuf_from_vlib_buffer (pref1);
692 CLIB_PREFETCH (prefmb0, CLIB_CACHE_LINE_BYTES, LOAD);
693 CLIB_PREFETCH (pref0, CLIB_CACHE_LINE_BYTES, LOAD);
694 CLIB_PREFETCH (prefmb1, CLIB_CACHE_LINE_BYTES, LOAD);
695 CLIB_PREFETCH (pref1, CLIB_CACHE_LINE_BYTES, LOAD);
701 b0 = vlib_get_buffer (vm, bi0);
702 b1 = vlib_get_buffer (vm, bi1);
704 mb0 = rte_mbuf_from_vlib_buffer (b0);
705 mb1 = rte_mbuf_from_vlib_buffer (b1);
707 any_clone = (b0->flags & VLIB_BUFFER_RECYCLE)
708 | (b1->flags & VLIB_BUFFER_RECYCLE);
709 if (PREDICT_FALSE (any_clone != 0))
711 if (PREDICT_FALSE ((b0->flags & VLIB_BUFFER_RECYCLE) != 0))
713 struct rte_mbuf *mb0_new = dpdk_replicate_packet_mb (b0);
714 if (PREDICT_FALSE (mb0_new == 0))
716 vlib_error_count (vm, node->node_index,
717 DPDK_TX_FUNC_ERROR_REPL_FAIL, 1);
718 b0->flags |= VLIB_BUFFER_REPL_FAIL;
722 vec_add1 (dm->recycle[my_cpu], bi0);
724 if (PREDICT_FALSE ((b1->flags & VLIB_BUFFER_RECYCLE) != 0))
726 struct rte_mbuf *mb1_new = dpdk_replicate_packet_mb (b1);
727 if (PREDICT_FALSE (mb1_new == 0))
729 vlib_error_count (vm, node->node_index,
730 DPDK_TX_FUNC_ERROR_REPL_FAIL, 1);
731 b1->flags |= VLIB_BUFFER_REPL_FAIL;
735 vec_add1 (dm->recycle[my_cpu], bi1);
739 delta0 = PREDICT_FALSE (b0->flags & VLIB_BUFFER_REPL_FAIL) ? 0 :
740 vlib_buffer_length_in_chain (vm, b0) - (i16) mb0->pkt_len;
741 delta1 = PREDICT_FALSE (b1->flags & VLIB_BUFFER_REPL_FAIL) ? 0 :
742 vlib_buffer_length_in_chain (vm, b1) - (i16) mb1->pkt_len;
744 new_data_len0 = (u16) ((i16) mb0->data_len + delta0);
745 new_data_len1 = (u16) ((i16) mb1->data_len + delta1);
746 new_pkt_len0 = (u16) ((i16) mb0->pkt_len + delta0);
747 new_pkt_len1 = (u16) ((i16) mb1->pkt_len + delta1);
749 b0->current_length = new_data_len0;
750 b1->current_length = new_data_len1;
751 mb0->data_len = new_data_len0;
752 mb1->data_len = new_data_len1;
753 mb0->pkt_len = new_pkt_len0;
754 mb1->pkt_len = new_pkt_len1;
756 mb0->data_off = (PREDICT_FALSE (b0->flags & VLIB_BUFFER_REPL_FAIL)) ?
757 mb0->data_off : (u16) (RTE_PKTMBUF_HEADROOM + b0->current_data);
758 mb1->data_off = (PREDICT_FALSE (b1->flags & VLIB_BUFFER_REPL_FAIL)) ?
759 mb1->data_off : (u16) (RTE_PKTMBUF_HEADROOM + b1->current_data);
761 if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
763 if (b0->flags & VLIB_BUFFER_IS_TRACED)
764 dpdk_tx_trace_buffer (dm, node, xd, queue_id, bi0, b0);
765 if (b1->flags & VLIB_BUFFER_IS_TRACED)
766 dpdk_tx_trace_buffer (dm, node, xd, queue_id, bi1, b1);
769 if (PREDICT_TRUE (any_clone == 0))
771 tx_vector[i % xd->nb_tx_desc] = mb0;
773 tx_vector[i % xd->nb_tx_desc] = mb1;
778 /* cloning was done, need to check for failure */
779 if (PREDICT_TRUE ((b0->flags & VLIB_BUFFER_REPL_FAIL) == 0))
781 tx_vector[i % xd->nb_tx_desc] = mb0;
784 if (PREDICT_TRUE ((b1->flags & VLIB_BUFFER_REPL_FAIL) == 0))
786 tx_vector[i % xd->nb_tx_desc] = mb1;
796 struct rte_mbuf *mb0;
805 b0 = vlib_get_buffer (vm, bi0);
807 mb0 = rte_mbuf_from_vlib_buffer (b0);
808 if (PREDICT_FALSE ((b0->flags & VLIB_BUFFER_RECYCLE) != 0))
810 struct rte_mbuf *mb0_new = dpdk_replicate_packet_mb (b0);
811 if (PREDICT_FALSE (mb0_new == 0))
813 vlib_error_count (vm, node->node_index,
814 DPDK_TX_FUNC_ERROR_REPL_FAIL, 1);
815 b0->flags |= VLIB_BUFFER_REPL_FAIL;
819 vec_add1 (dm->recycle[my_cpu], bi0);
822 delta0 = PREDICT_FALSE (b0->flags & VLIB_BUFFER_REPL_FAIL) ? 0 :
823 vlib_buffer_length_in_chain (vm, b0) - (i16) mb0->pkt_len;
825 new_data_len0 = (u16) ((i16) mb0->data_len + delta0);
826 new_pkt_len0 = (u16) ((i16) mb0->pkt_len + delta0);
828 b0->current_length = new_data_len0;
829 mb0->data_len = new_data_len0;
830 mb0->pkt_len = new_pkt_len0;
831 mb0->data_off = (PREDICT_FALSE (b0->flags & VLIB_BUFFER_REPL_FAIL)) ?
832 mb0->data_off : (u16) (RTE_PKTMBUF_HEADROOM + b0->current_data);
834 if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
835 if (b0->flags & VLIB_BUFFER_IS_TRACED)
836 dpdk_tx_trace_buffer (dm, node, xd, queue_id, bi0, b0);
838 if (PREDICT_TRUE ((b0->flags & VLIB_BUFFER_REPL_FAIL) == 0))
840 tx_vector[i % xd->nb_tx_desc] = mb0;
846 /* account for additional packets in the ring */
847 ring->tx_head += n_packets;
848 n_on_ring = ring->tx_head - ring->tx_tail;
850 /* transmit as many packets as possible */
851 n_packets = tx_burst_vector_internal (vm, xd, tx_vector);
854 * tx_pkts is the number of packets successfully transmitted
855 * This is the number originally on ring minus the number remaining on ring
857 tx_pkts = n_on_ring - n_packets;
859 if (PREDICT_FALSE (dm->flowcontrol_callback != 0))
861 if (PREDICT_FALSE (n_packets))
863 /* Callback may want to enable flowcontrol */
864 dm->flowcontrol_callback (vm, xd->vlib_hw_if_index,
865 ring->tx_head - ring->tx_tail);
869 /* Reset head/tail to avoid unnecessary wrap */
876 /* If there is no callback then drop any non-transmitted packets */
877 if (PREDICT_FALSE (n_packets))
879 vlib_simple_counter_main_t *cm;
880 vnet_main_t *vnm = vnet_get_main ();
882 cm = vec_elt_at_index (vnm->interface_main.sw_if_counters,
883 VNET_INTERFACE_COUNTER_TX_ERROR);
885 vlib_increment_simple_counter (cm, my_cpu, xd->vlib_sw_if_index,
888 vlib_error_count (vm, node->node_index, DPDK_TX_FUNC_ERROR_PKT_DROP,
892 rte_pktmbuf_free (tx_vector[ring->tx_tail + n_packets]);
895 /* Reset head/tail to avoid unnecessary wrap */
900 /* Recycle replicated buffers */
901 if (PREDICT_FALSE (vec_len (dm->recycle[my_cpu])))
903 vlib_buffer_free (vm, dm->recycle[my_cpu],
904 vec_len (dm->recycle[my_cpu]));
905 _vec_len (dm->recycle[my_cpu]) = 0;
908 ASSERT (ring->tx_head >= ring->tx_tail);
914 dpdk_device_renumber (vnet_hw_interface_t * hi, u32 new_dev_instance)
917 dpdk_main_t *dm = &dpdk_main;
918 dpdk_device_t *xd = vec_elt_at_index (dm->devices, hi->dev_instance);
920 if (!xd || (xd->flags & DPDK_DEVICE_FLAG_VHOST_USER) == 0)
923 ("cannot renumber non-vhost-user interface (sw_if_index: %d)",
928 xd->vu_if_id = new_dev_instance;
934 dpdk_clear_hw_interface_counters (u32 instance)
936 dpdk_main_t *dm = &dpdk_main;
937 dpdk_device_t *xd = vec_elt_at_index (dm->devices, instance);
940 * Set the "last_cleared_stats" to the current stats, so that
941 * things appear to clear from a display perspective.
943 dpdk_update_counters (xd, vlib_time_now (dm->vlib_main));
945 clib_memcpy (&xd->last_cleared_stats, &xd->stats, sizeof (xd->stats));
946 clib_memcpy (xd->last_cleared_xstats, xd->xstats,
947 vec_len (xd->last_cleared_xstats) *
948 sizeof (xd->last_cleared_xstats[0]));
951 if (PREDICT_FALSE (xd->flags & DPDK_DEVICE_FLAG_VHOST_USER))
954 for (i = 0; i < xd->rx_q_used * VIRTIO_QNUM; i++)
956 xd->vu_intf->vrings[i].packets = 0;
957 xd->vu_intf->vrings[i].bytes = 0;
963 #ifdef RTE_LIBRTE_KNI
965 kni_config_network_if (u8 port_id, u8 if_up)
967 vnet_main_t *vnm = vnet_get_main ();
968 dpdk_main_t *dm = &dpdk_main;
972 p = hash_get (dm->dpdk_device_by_kni_port_id, port_id);
975 clib_warning ("unknown interface");
980 xd = vec_elt_at_index (dm->devices, p[0]);
983 vnet_hw_interface_set_flags (vnm, xd->vlib_hw_if_index,
984 if_up ? VNET_HW_INTERFACE_FLAG_LINK_UP |
985 ETH_LINK_FULL_DUPLEX : 0);
990 kni_change_mtu (u8 port_id, unsigned new_mtu)
992 vnet_main_t *vnm = vnet_get_main ();
993 dpdk_main_t *dm = &dpdk_main;
996 vnet_hw_interface_t *hif;
998 p = hash_get (dm->dpdk_device_by_kni_port_id, port_id);
1001 clib_warning ("unknown interface");
1006 xd = vec_elt_at_index (dm->devices, p[0]);
1008 hif = vnet_get_hw_interface (vnm, xd->vlib_hw_if_index);
1010 hif->max_packet_bytes = new_mtu;
1016 static clib_error_t *
1017 dpdk_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
1019 vnet_hw_interface_t *hif = vnet_get_hw_interface (vnm, hw_if_index);
1020 uword is_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
1021 dpdk_main_t *dm = &dpdk_main;
1022 dpdk_device_t *xd = vec_elt_at_index (dm->devices, hif->dev_instance);
1025 #ifdef RTE_LIBRTE_KNI
1026 if (xd->flags & DPDK_DEVICE_FLAG_KNI)
1030 struct rte_kni_conf conf;
1031 struct rte_kni_ops ops;
1032 vlib_main_t *vm = vlib_get_main ();
1033 vlib_buffer_main_t *bm = vm->buffer_main;
1034 memset (&conf, 0, sizeof (conf));
1035 snprintf (conf.name, RTE_KNI_NAMESIZE, "vpp%u", xd->kni_port_id);
1036 conf.mbuf_size = VLIB_BUFFER_DATA_SIZE;
1037 memset (&ops, 0, sizeof (ops));
1038 ops.port_id = xd->kni_port_id;
1039 ops.change_mtu = kni_change_mtu;
1040 ops.config_network_if = kni_config_network_if;
1043 rte_kni_alloc (bm->pktmbuf_pools[rte_socket_id ()], &conf, &ops);
1046 clib_warning ("failed to allocate kni interface");
1050 hif->max_packet_bytes = 1500; /* kni interface default value */
1051 xd->flags |= DPDK_DEVICE_FLAG_ADMIN_UP;
1056 xd->flags &= ~DPDK_DEVICE_FLAG_ADMIN_UP;
1059 kni_rv = rte_kni_release (xd->kni);
1061 clib_warning ("rte_kni_release returned %d", kni_rv);
1067 if (xd->flags & DPDK_DEVICE_FLAG_VHOST_USER)
1071 if (xd->vu_is_running)
1072 vnet_hw_interface_set_flags (vnm, xd->vlib_hw_if_index,
1073 VNET_HW_INTERFACE_FLAG_LINK_UP |
1074 ETH_LINK_FULL_DUPLEX);
1075 xd->flags |= DPDK_DEVICE_FLAG_ADMIN_UP;
1079 vnet_hw_interface_set_flags (vnm, xd->vlib_hw_if_index, 0);
1080 xd->flags &= ~DPDK_DEVICE_FLAG_ADMIN_UP;
1090 f64 now = vlib_time_now (dm->vlib_main);
1092 if ((xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP) == 0)
1093 rv = rte_eth_dev_start (xd->device_index);
1095 if (xd->flags & DPDK_DEVICE_FLAG_PROMISC)
1096 rte_eth_promiscuous_enable (xd->device_index);
1098 rte_eth_promiscuous_disable (xd->device_index);
1100 rte_eth_allmulticast_enable (xd->device_index);
1101 xd->flags |= DPDK_DEVICE_FLAG_ADMIN_UP;
1102 dpdk_update_counters (xd, now);
1103 dpdk_update_link_state (xd, now);
1107 xd->flags &= ~DPDK_DEVICE_FLAG_ADMIN_UP;
1109 rte_eth_allmulticast_disable (xd->device_index);
1110 vnet_hw_interface_set_flags (vnm, xd->vlib_hw_if_index, 0);
1111 rte_eth_dev_stop (xd->device_index);
1113 /* For bonded interface, stop slave links */
1114 if (xd->pmd == VNET_DPDK_PMD_BOND)
1117 int nlink = rte_eth_bond_slaves_get (xd->device_index, slink, 16);
1120 u8 dpdk_port = slink[--nlink];
1121 rte_eth_dev_stop (dpdk_port);
1127 clib_warning ("rte_eth_dev_%s error: %d", is_up ? "start" : "stop", rv);
1129 return /* no error */ 0;
1133 * Dynamically redirect all pkts from a specific interface
1134 * to the specified node
1137 dpdk_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index,
1140 dpdk_main_t *xm = &dpdk_main;
1141 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
1142 dpdk_device_t *xd = vec_elt_at_index (xm->devices, hw->dev_instance);
1144 /* Shut off redirection */
1145 if (node_index == ~0)
1147 xd->per_interface_next_index = node_index;
1151 xd->per_interface_next_index =
1152 vlib_node_add_next (xm->vlib_main, dpdk_input_node.index, node_index);
1156 static clib_error_t *
1157 dpdk_subif_add_del_function (vnet_main_t * vnm,
1159 struct vnet_sw_interface_t *st, int is_add)
1161 dpdk_main_t *xm = &dpdk_main;
1162 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
1163 dpdk_device_t *xd = vec_elt_at_index (xm->devices, hw->dev_instance);
1164 vnet_sw_interface_t *t = (vnet_sw_interface_t *) st;
1165 int r, vlan_offload;
1166 u32 prev_subifs = xd->num_subifs;
1167 clib_error_t *err = 0;
1171 else if (xd->num_subifs)
1174 if ((xd->flags & DPDK_DEVICE_FLAG_PMD) == 0)
1177 /* currently we program VLANS only for IXGBE VF and I40E VF */
1178 if ((xd->pmd != VNET_DPDK_PMD_IXGBEVF) && (xd->pmd != VNET_DPDK_PMD_I40EVF))
1181 if (t->sub.eth.flags.no_tags == 1)
1184 if ((t->sub.eth.flags.one_tag != 1) || (t->sub.eth.flags.exact_match != 1))
1186 xd->num_subifs = prev_subifs;
1187 err = clib_error_return (0, "unsupported VLAN setup");
1191 vlan_offload = rte_eth_dev_get_vlan_offload (xd->device_index);
1192 vlan_offload |= ETH_VLAN_FILTER_OFFLOAD;
1194 if ((r = rte_eth_dev_set_vlan_offload (xd->device_index, vlan_offload)))
1196 xd->num_subifs = prev_subifs;
1197 err = clib_error_return (0, "rte_eth_dev_set_vlan_offload[%d]: err %d",
1198 xd->device_index, r);
1204 rte_eth_dev_vlan_filter (xd->device_index, t->sub.eth.outer_vlan_id,
1207 xd->num_subifs = prev_subifs;
1208 err = clib_error_return (0, "rte_eth_dev_vlan_filter[%d]: err %d",
1209 xd->device_index, r);
1215 xd->flags |= DPDK_DEVICE_FLAG_HAVE_SUBIF;
1217 xd->flags &= ~DPDK_DEVICE_FLAG_HAVE_SUBIF;
1223 VNET_DEVICE_CLASS (dpdk_device_class) = {
1225 .tx_function = dpdk_interface_tx,
1226 .tx_function_n_errors = DPDK_TX_FUNC_N_ERROR,
1227 .tx_function_error_strings = dpdk_tx_func_error_strings,
1228 .format_device_name = format_dpdk_device_name,
1229 .format_device = format_dpdk_device,
1230 .format_tx_trace = format_dpdk_tx_dma_trace,
1231 .clear_counters = dpdk_clear_hw_interface_counters,
1232 .admin_up_down_function = dpdk_interface_admin_up_down,
1233 .subif_add_del_function = dpdk_subif_add_del_function,
1234 .rx_redirect_to_node = dpdk_set_interface_next_node,
1235 .no_flatten_output_chains = 1,
1236 .name_renumber = dpdk_device_renumber,
1239 VLIB_DEVICE_TX_FUNCTION_MULTIARCH (dpdk_device_class, dpdk_interface_tx)
1243 dpdk_set_flowcontrol_callback (vlib_main_t * vm,
1244 dpdk_flowcontrol_callback_t callback)
1246 dpdk_main.flowcontrol_callback = callback;
1249 #define UP_DOWN_FLAG_EVENT 1
1253 dpdk_get_admin_up_down_in_progress (void)
1255 return dpdk_main.admin_up_down_in_progress;
1259 admin_up_down_process (vlib_main_t * vm,
1260 vlib_node_runtime_t * rt, vlib_frame_t * f)
1262 clib_error_t *error = 0;
1264 uword *event_data = 0;
1270 vlib_process_wait_for_event (vm);
1272 event_type = vlib_process_get_events (vm, &event_data);
1274 dpdk_main.admin_up_down_in_progress = 1;
1278 case UP_DOWN_FLAG_EVENT:
1280 if (vec_len (event_data) == 2)
1282 sw_if_index = event_data[0];
1283 flags = event_data[1];
1285 vnet_sw_interface_set_flags (vnet_get_main (), sw_if_index,
1287 clib_error_report (error);
1293 vec_reset_length (event_data);
1295 dpdk_main.admin_up_down_in_progress = 0;
1298 return 0; /* or not */
1302 VLIB_REGISTER_NODE (admin_up_down_process_node,static) = {
1303 .function = admin_up_down_process,
1304 .type = VLIB_NODE_TYPE_PROCESS,
1305 .name = "admin-up-down-process",
1306 .process_log2_n_stack_bytes = 17, // 256KB
1311 * Asynchronously invoke vnet_sw_interface_set_flags via the admin_up_down
1312 * process. Useful for avoiding long blocking delays (>150ms) in the dpdk
1314 * WARNING: when posting this event, no other interface-related calls should
1315 * be made (e.g. vnet_create_sw_interface()) while the event is being
1316 * processed (admin_up_down_in_progress). This is required in order to avoid
1317 * race conditions in manipulating interface data structures.
1320 post_sw_interface_set_flags (vlib_main_t * vm, u32 sw_if_index, u32 flags)
1322 uword *d = vlib_process_signal_event_data
1323 (vm, admin_up_down_process_node.index,
1324 UP_DOWN_FLAG_EVENT, 2, sizeof (u32));
1330 * Return a copy of the DPDK port stats in dest.
1333 dpdk_get_hw_interface_stats (u32 hw_if_index, struct rte_eth_stats *dest)
1335 dpdk_main_t *dm = &dpdk_main;
1336 vnet_main_t *vnm = vnet_get_main ();
1337 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
1338 dpdk_device_t *xd = vec_elt_at_index (dm->devices, hi->dev_instance);
1342 return clib_error_return (0, "Missing or NULL argument");
1346 return clib_error_return (0,
1347 "Unable to get DPDK device from HW interface");
1350 dpdk_update_counters (xd, vlib_time_now (dm->vlib_main));
1352 clib_memcpy (dest, &xd->stats, sizeof (xd->stats));
1357 * Return the number of dpdk mbufs
1360 dpdk_num_mbufs (void)
1362 dpdk_main_t *dm = &dpdk_main;
1364 return dm->conf->num_mbufs;
1368 * Return the pmd type for a given hardware interface
1371 dpdk_get_pmd_type (vnet_hw_interface_t * hi)
1373 dpdk_main_t *dm = &dpdk_main;
1378 xd = vec_elt_at_index (dm->devices, hi->dev_instance);
1386 * Return the cpu socket for a given hardware interface
1389 dpdk_get_cpu_socket (vnet_hw_interface_t * hi)
1391 dpdk_main_t *dm = &dpdk_main;
1396 xd = vec_elt_at_index (dm->devices, hi->dev_instance);
1400 return xd->cpu_socket;
1404 * fd.io coding-style-patch-verification: ON
1407 * eval: (c-set-style "gnu")