VPP-363: add ability to change mac address of the interface
[vpp.git] / vnet / vnet / devices / dpdk / device.c
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15 #include <vnet/vnet.h>
16 #include <vppinfra/vec.h>
17 #include <vppinfra/format.h>
18 #include <vlib/unix/cj.h>
19 #include <assert.h>
20
21 #include <vnet/ethernet/ethernet.h>
22 #include <vnet/devices/dpdk/dpdk.h>
23
24 #include "dpdk_priv.h"
25 #include <vppinfra/error.h>
26
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)")
32
33 typedef enum
34 {
35 #define _(f,s) DPDK_TX_FUNC_ERROR_##f,
36   foreach_dpdk_tx_func_error
37 #undef _
38     DPDK_TX_FUNC_N_ERROR,
39 } dpdk_tx_func_error_t;
40
41 static char *dpdk_tx_func_error_strings[] = {
42 #define _(n,s) s,
43   foreach_dpdk_tx_func_error
44 #undef _
45 };
46
47 clib_error_t *
48 dpdk_set_mac_address (vnet_hw_interface_t * hi, char *address)
49 {
50   int error;
51   dpdk_main_t *dm = &dpdk_main;
52   dpdk_device_t *xd = vec_elt_at_index (dm->devices, hi->dev_instance);
53
54   error = rte_eth_dev_default_mac_addr_set (xd->device_index,
55                                             (struct ether_addr *) address);
56
57   if (error)
58     {
59       return clib_error_return (0, "mac address set failed: %d", error);
60     }
61   else
62     {
63       return NULL;
64     }
65 }
66
67 clib_error_t *
68 dpdk_set_mc_filter (vnet_hw_interface_t * hi,
69                     struct ether_addr mc_addr_vec[], int naddr)
70 {
71   int error;
72   dpdk_main_t *dm = &dpdk_main;
73   dpdk_device_t *xd = vec_elt_at_index (dm->devices, hi->dev_instance);
74
75   error = rte_eth_dev_set_mc_addr_list (xd->device_index, mc_addr_vec, naddr);
76
77   if (error)
78     {
79       return clib_error_return (0, "mc addr list failed: %d", error);
80     }
81   else
82     {
83       return NULL;
84     }
85 }
86
87 struct rte_mbuf *
88 dpdk_replicate_packet_mb (vlib_buffer_t * b)
89 {
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;
94   u32 copy_bytes;
95   unsigned socket_id = rte_socket_id ();
96
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--)
101     {
102       if (PREDICT_FALSE (pkt_mb == 0))
103         {
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);
107           if (first_mb)
108             rte_pktmbuf_free (first_mb);
109           return NULL;
110         }
111       new_mb = rte_pktmbuf_alloc (bm->pktmbuf_pools[socket_id]);
112       if (PREDICT_FALSE (new_mb == 0))
113         {
114           if (first_mb)
115             rte_pktmbuf_free (first_mb);
116           return NULL;
117         }
118
119       /*
120        * Copy packet info into 1st segment.
121        */
122       if (first_mb == 0)
123         {
124           first_mb = new_mb;
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
132 #endif
133         }
134       else
135         {
136           ASSERT (prev_mb_next != 0);
137           *prev_mb_next = new_mb;
138         }
139
140       /*
141        * Copy packet segment data into new mbuf segment.
142        */
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);
147
148       prev_mb_next = &new_mb->next;
149       pkt_mb = pkt_mb->next;
150     }
151
152   ASSERT (pkt_mb == 0);
153   __rte_mbuf_sanity_check (first_mb, 1);
154
155   return first_mb;
156 }
157
158 struct rte_mbuf *
159 dpdk_zerocopy_replicate_packet_mb (vlib_buffer_t * b)
160 {
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 ();
166
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--)
171     {
172       if (PREDICT_FALSE (pkt_mb == 0))
173         {
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);
177           if (first_mb)
178             rte_pktmbuf_free (first_mb);
179           return NULL;
180         }
181       new_mb = rte_pktmbuf_clone (pkt_mb, bm->pktmbuf_pools[socket_id]);
182       if (PREDICT_FALSE (new_mb == 0))
183         {
184           if (first_mb)
185             rte_pktmbuf_free (first_mb);
186           return NULL;
187         }
188
189       /*
190        * Copy packet info into 1st segment.
191        */
192       if (first_mb == 0)
193         {
194           first_mb = new_mb;
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
202 #endif
203         }
204       else
205         {
206           ASSERT (prev_mb_next != 0);
207           *prev_mb_next = new_mb;
208         }
209
210       /*
211        * Copy packet segment data into new mbuf segment.
212        */
213       rte_pktmbuf_data_len (new_mb) = pkt_mb->data_len;
214
215       prev_mb_next = &new_mb->next;
216       pkt_mb = pkt_mb->next;
217     }
218
219   ASSERT (pkt_mb == 0);
220   __rte_mbuf_sanity_check (first_mb, 1);
221
222   return first_mb;
223
224
225 }
226
227 static void
228 dpdk_tx_trace_buffer (dpdk_main_t * dm,
229                       vlib_node_runtime_t * node,
230                       dpdk_device_t * xd,
231                       u16 queue_id, u32 buffer_index, vlib_buffer_t * buffer)
232 {
233   vlib_main_t *vm = vlib_get_main ();
234   dpdk_tx_dma_trace_t *t0;
235   struct rte_mbuf *mb;
236
237   mb = rte_mbuf_from_vlib_buffer (buffer);
238
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));
248 }
249
250 /*
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.
256  *
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.)
263  *
264  * The function assumes there is at least one packet on the tx_vector.
265  */
266 static_always_inline
267   u32 tx_burst_vector_internal (vlib_main_t * vm,
268                                 dpdk_device_t * xd,
269                                 struct rte_mbuf **tx_vector)
270 {
271   dpdk_main_t *dm = &dpdk_main;
272   u32 n_packets;
273   u32 tx_head;
274   u32 tx_tail;
275   u32 n_retry;
276   int rv;
277   int queue_id;
278   tx_ring_hdr_t *ring;
279
280   ring = vec_header (tx_vector, sizeof (*ring));
281
282   n_packets = ring->tx_head - ring->tx_tail;
283
284   tx_head = ring->tx_head % xd->nb_tx_desc;
285
286   /*
287    * Ensure rte_eth_tx_burst is not called with 0 packets, which can lead to
288    * unpredictable results.
289    */
290   ASSERT (n_packets > 0);
291
292   /*
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.
298    */
299   ASSERT (n_packets < xd->nb_tx_desc);
300
301   /*
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.
304    */
305   ASSERT (dm->flowcontrol_callback || ring->tx_tail == 0);
306
307   /*
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
311    * limit.
312    */
313   n_retry = dm->flowcontrol_callback ? 0 : 255;
314
315   queue_id = vm->cpu_index;
316
317   do
318     {
319       /* start the burst at the tail */
320       tx_tail = ring->tx_tail % xd->nb_tx_desc;
321
322       /*
323        * This device only supports one TX queue,
324        * and we're running multi-threaded...
325        */
326       if (PREDICT_FALSE ((xd->flags & DPDK_DEVICE_FLAG_VHOST_USER) == 0 &&
327                          xd->lockp != 0))
328         {
329           queue_id = queue_id % xd->tx_q_used;
330           while (__sync_lock_test_and_set (xd->lockp[queue_id], 1))
331             /* zzzz */
332             queue_id = (queue_id + 1) % xd->tx_q_used;
333         }
334
335       if (PREDICT_TRUE (xd->flags & DPDK_DEVICE_FLAG_HQOS))     /* HQoS ON */
336         {
337           if (PREDICT_TRUE (tx_head > tx_tail))
338             {
339               /* no wrap, transmit in one burst */
340               dpdk_device_hqos_per_worker_thread_t *hqos =
341                 &xd->hqos_wt[vm->cpu_index];
342
343               dpdk_hqos_metadata_set (hqos,
344                                       &tx_vector[tx_tail], tx_head - tx_tail);
345               rv = rte_ring_sp_enqueue_burst (hqos->swq,
346                                               (void **) &tx_vector[tx_tail],
347                                               (uint16_t) (tx_head - tx_tail));
348             }
349           else
350             {
351               /*
352                * This can only happen if there is a flowcontrol callback.
353                * We need to split the transmit into two calls: one for
354                * the packets up to the wrap point, and one to continue
355                * at the start of the ring.
356                * Transmit pkts up to the wrap point.
357                */
358               dpdk_device_hqos_per_worker_thread_t *hqos =
359                 &xd->hqos_wt[vm->cpu_index];
360
361               dpdk_hqos_metadata_set (hqos,
362                                       &tx_vector[tx_tail],
363                                       xd->nb_tx_desc - tx_tail);
364               rv = rte_ring_sp_enqueue_burst (hqos->swq,
365                                               (void **) &tx_vector[tx_tail],
366                                               (uint16_t) (xd->nb_tx_desc -
367                                                           tx_tail));
368               /*
369                * If we transmitted everything we wanted, then allow 1 retry
370                * so we can try to transmit the rest. If we didn't transmit
371                * everything, stop now.
372                */
373               n_retry = (rv == xd->nb_tx_desc - tx_tail) ? 1 : 0;
374             }
375         }
376       else if (PREDICT_TRUE (xd->flags & DPDK_DEVICE_FLAG_PMD))
377         {
378           if (PREDICT_TRUE (tx_head > tx_tail))
379             {
380               /* no wrap, transmit in one burst */
381               rv = rte_eth_tx_burst (xd->device_index,
382                                      (uint16_t) queue_id,
383                                      &tx_vector[tx_tail],
384                                      (uint16_t) (tx_head - tx_tail));
385             }
386           else
387             {
388               /*
389                * This can only happen if there is a flowcontrol callback.
390                * We need to split the transmit into two calls: one for
391                * the packets up to the wrap point, and one to continue
392                * at the start of the ring.
393                * Transmit pkts up to the wrap point.
394                */
395               rv = rte_eth_tx_burst (xd->device_index,
396                                      (uint16_t) queue_id,
397                                      &tx_vector[tx_tail],
398                                      (uint16_t) (xd->nb_tx_desc - tx_tail));
399
400               /*
401                * If we transmitted everything we wanted, then allow 1 retry
402                * so we can try to transmit the rest. If we didn't transmit
403                * everything, stop now.
404                */
405               n_retry = (rv == xd->nb_tx_desc - tx_tail) ? 1 : 0;
406             }
407         }
408 #if DPDK_VHOST_USER
409       else if (xd->flags & DPDK_DEVICE_FLAG_VHOST_USER)
410         {
411           u32 offset = 0;
412           if (xd->need_txlock)
413             {
414               queue_id = 0;
415               while (__sync_lock_test_and_set (xd->lockp[queue_id], 1));
416             }
417           else
418             {
419               dpdk_device_and_queue_t *dq;
420               vec_foreach (dq, dm->devices_by_cpu[vm->cpu_index])
421               {
422                 if (xd->device_index == dq->device)
423                   break;
424               }
425               assert (dq);
426               offset = dq->queue_id * VIRTIO_QNUM;
427             }
428           if (PREDICT_TRUE (tx_head > tx_tail))
429             {
430               int i;
431               u32 bytes = 0;
432               struct rte_mbuf **pkts = &tx_vector[tx_tail];
433               for (i = 0; i < (tx_head - tx_tail); i++)
434                 {
435                   struct rte_mbuf *buff = pkts[i];
436                   bytes += rte_pktmbuf_data_len (buff);
437                 }
438
439               /* no wrap, transmit in one burst */
440               rv =
441                 rte_vhost_enqueue_burst (&xd->vu_vhost_dev,
442                                          offset + VIRTIO_RXQ,
443                                          &tx_vector[tx_tail],
444                                          (uint16_t) (tx_head - tx_tail));
445               if (PREDICT_TRUE (rv > 0))
446                 {
447                   dpdk_vu_vring *vring =
448                     &(xd->vu_intf->vrings[offset + VIRTIO_TXQ]);
449                   vring->packets += rv;
450                   vring->bytes += bytes;
451
452                   if (dpdk_vhost_user_want_interrupt
453                       (xd, offset + VIRTIO_RXQ))
454                     {
455                       vring = &(xd->vu_intf->vrings[offset + VIRTIO_RXQ]);
456                       vring->n_since_last_int += rv;
457
458                       f64 now = vlib_time_now (vm);
459                       if (vring->int_deadline < now ||
460                           vring->n_since_last_int >
461                           dm->conf->vhost_coalesce_frames)
462                         dpdk_vhost_user_send_interrupt (vm, xd,
463                                                         offset + VIRTIO_RXQ);
464                     }
465
466                   int c = rv;
467                   while (c--)
468                     rte_pktmbuf_free (tx_vector[tx_tail + c]);
469                 }
470             }
471           else
472             {
473               /*
474                * If we transmitted everything we wanted, then allow 1 retry
475                * so we can try to transmit the rest. If we didn't transmit
476                * everything, stop now.
477                */
478               int i;
479               u32 bytes = 0;
480               struct rte_mbuf **pkts = &tx_vector[tx_tail];
481               for (i = 0; i < (xd->nb_tx_desc - tx_tail); i++)
482                 {
483                   struct rte_mbuf *buff = pkts[i];
484                   bytes += rte_pktmbuf_data_len (buff);
485                 }
486               rv =
487                 rte_vhost_enqueue_burst (&xd->vu_vhost_dev,
488                                          offset + VIRTIO_RXQ,
489                                          &tx_vector[tx_tail],
490                                          (uint16_t) (xd->nb_tx_desc -
491                                                      tx_tail));
492
493               if (PREDICT_TRUE (rv > 0))
494                 {
495                   dpdk_vu_vring *vring =
496                     &(xd->vu_intf->vrings[offset + VIRTIO_TXQ]);
497                   vring->packets += rv;
498                   vring->bytes += bytes;
499
500                   if (dpdk_vhost_user_want_interrupt
501                       (xd, offset + VIRTIO_RXQ))
502                     {
503                       vring = &(xd->vu_intf->vrings[offset + VIRTIO_RXQ]);
504                       vring->n_since_last_int += rv;
505
506                       f64 now = vlib_time_now (vm);
507                       if (vring->int_deadline < now ||
508                           vring->n_since_last_int >
509                           dm->conf->vhost_coalesce_frames)
510                         dpdk_vhost_user_send_interrupt (vm, xd,
511                                                         offset + VIRTIO_RXQ);
512                     }
513
514                   int c = rv;
515                   while (c--)
516                     rte_pktmbuf_free (tx_vector[tx_tail + c]);
517                 }
518
519               n_retry = (rv == xd->nb_tx_desc - tx_tail) ? 1 : 0;
520             }
521
522           if (xd->need_txlock)
523             *xd->lockp[queue_id] = 0;
524         }
525 #endif
526 #if RTE_LIBRTE_KNI
527       else if (xd->flags & DPDK_DEVICE_FLAG_KNI)
528         {
529           if (PREDICT_TRUE (tx_head > tx_tail))
530             {
531               /* no wrap, transmit in one burst */
532               rv = rte_kni_tx_burst (xd->kni,
533                                      &tx_vector[tx_tail],
534                                      (uint16_t) (tx_head - tx_tail));
535             }
536           else
537             {
538               /*
539                * This can only happen if there is a flowcontrol callback.
540                * We need to split the transmit into two calls: one for
541                * the packets up to the wrap point, and one to continue
542                * at the start of the ring.
543                * Transmit pkts up to the wrap point.
544                */
545               rv = rte_kni_tx_burst (xd->kni,
546                                      &tx_vector[tx_tail],
547                                      (uint16_t) (xd->nb_tx_desc - tx_tail));
548
549               /*
550                * If we transmitted everything we wanted, then allow 1 retry
551                * so we can try to transmit the rest. If we didn't transmit
552                * everything, stop now.
553                */
554               n_retry = (rv == xd->nb_tx_desc - tx_tail) ? 1 : 0;
555             }
556         }
557 #endif
558       else
559         {
560           ASSERT (0);
561           rv = 0;
562         }
563
564       if (PREDICT_FALSE ((xd->flags & DPDK_DEVICE_FLAG_VHOST_USER) == 0 &&
565                          xd->lockp != 0))
566         *xd->lockp[queue_id] = 0;
567
568       if (PREDICT_FALSE (rv < 0))
569         {
570           // emit non-fatal message, bump counter
571           vnet_main_t *vnm = dm->vnet_main;
572           vnet_interface_main_t *im = &vnm->interface_main;
573           u32 node_index;
574
575           node_index = vec_elt_at_index (im->hw_interfaces,
576                                          xd->vlib_hw_if_index)->tx_node_index;
577
578           vlib_error_count (vm, node_index, DPDK_TX_FUNC_ERROR_BAD_RETVAL, 1);
579           clib_warning ("rte_eth_tx_burst[%d]: error %d", xd->device_index,
580                         rv);
581           return n_packets;     // untransmitted packets
582         }
583       ring->tx_tail += (u16) rv;
584       n_packets -= (uint16_t) rv;
585     }
586   while (rv && n_packets && (n_retry > 0));
587
588   return n_packets;
589 }
590
591
592 /*
593  * This function transmits any packets on the interface's tx_vector and returns
594  * the number of packets untransmitted on the tx_vector. If the tx_vector is
595  * empty the function simply returns 0.
596  *
597  * It is intended to be called by a traffic manager which has flowed-off an
598  * interface to see if the interface can be flowed-on again.
599  */
600 u32
601 dpdk_interface_tx_vector (vlib_main_t * vm, u32 dev_instance)
602 {
603   dpdk_main_t *dm = &dpdk_main;
604   dpdk_device_t *xd;
605   int queue_id;
606   struct rte_mbuf **tx_vector;
607   tx_ring_hdr_t *ring;
608
609   /* param is dev_instance and not hw_if_index to save another lookup */
610   xd = vec_elt_at_index (dm->devices, dev_instance);
611
612   queue_id = vm->cpu_index;
613   tx_vector = xd->tx_vectors[queue_id];
614
615   /* If no packets on the ring, don't bother calling tx function */
616   ring = vec_header (tx_vector, sizeof (*ring));
617   if (ring->tx_head == ring->tx_tail)
618     {
619       return 0;
620     }
621
622   return tx_burst_vector_internal (vm, xd, tx_vector);
623 }
624
625 /*
626  * Transmits the packets on the frame to the interface associated with the
627  * node. It first copies packets on the frame to a tx_vector containing the
628  * rte_mbuf pointers. It then passes this vector to tx_burst_vector_internal
629  * which calls the dpdk tx_burst function.
630  *
631  * The tx_vector is treated slightly differently depending on whether or
632  * not a flowcontrol callback function has been configured. If there is no
633  * callback, the tx_vector is a temporary array of rte_mbuf packet pointers.
634  * Its entries are written and consumed before the function exits.
635  *
636  * If there is a callback then the transmit is being invoked in the presence
637  * of a traffic manager. Here the tx_vector is treated like a ring of rte_mbuf
638  * pointers. If not all packets can be transmitted, the untransmitted packets
639  * stay on the tx_vector until the next call. The callback allows the traffic
640  * manager to flow-off dequeues to the interface. The companion function
641  * dpdk_interface_tx_vector() allows the traffic manager to detect when
642  * it should flow-on the interface again.
643  */
644 static uword
645 dpdk_interface_tx (vlib_main_t * vm,
646                    vlib_node_runtime_t * node, vlib_frame_t * f)
647 {
648   dpdk_main_t *dm = &dpdk_main;
649   vnet_interface_output_runtime_t *rd = (void *) node->runtime_data;
650   dpdk_device_t *xd = vec_elt_at_index (dm->devices, rd->dev_instance);
651   u32 n_packets = f->n_vectors;
652   u32 n_left;
653   u32 *from;
654   struct rte_mbuf **tx_vector;
655   int i;
656   int queue_id;
657   u32 my_cpu;
658   u32 tx_pkts = 0;
659   tx_ring_hdr_t *ring;
660   u32 n_on_ring;
661
662   my_cpu = vm->cpu_index;
663
664   queue_id = my_cpu;
665
666   tx_vector = xd->tx_vectors[queue_id];
667   ring = vec_header (tx_vector, sizeof (*ring));
668
669   n_on_ring = ring->tx_head - ring->tx_tail;
670   from = vlib_frame_vector_args (f);
671
672   ASSERT (n_packets <= VLIB_FRAME_SIZE);
673
674   if (PREDICT_FALSE (n_on_ring + n_packets > xd->nb_tx_desc))
675     {
676       /*
677        * Overflowing the ring should never happen.
678        * If it does then drop the whole frame.
679        */
680       vlib_error_count (vm, node->node_index, DPDK_TX_FUNC_ERROR_RING_FULL,
681                         n_packets);
682
683       while (n_packets--)
684         {
685           u32 bi0 = from[n_packets];
686           vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
687           struct rte_mbuf *mb0 = rte_mbuf_from_vlib_buffer (b0);
688           rte_pktmbuf_free (mb0);
689         }
690       return n_on_ring;
691     }
692
693   if (PREDICT_FALSE (dm->tx_pcap_enable))
694     {
695       n_left = n_packets;
696       while (n_left > 0)
697         {
698           u32 bi0 = from[0];
699           vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
700           if (dm->pcap_sw_if_index == 0 ||
701               dm->pcap_sw_if_index == vnet_buffer (b0)->sw_if_index[VLIB_TX])
702             pcap_add_buffer (&dm->pcap_main, vm, bi0, 512);
703           from++;
704           n_left--;
705         }
706     }
707
708   from = vlib_frame_vector_args (f);
709   n_left = n_packets;
710   i = ring->tx_head % xd->nb_tx_desc;
711
712   while (n_left >= 4)
713     {
714       u32 bi0, bi1;
715       u32 pi0, pi1;
716       struct rte_mbuf *mb0, *mb1;
717       struct rte_mbuf *prefmb0, *prefmb1;
718       vlib_buffer_t *b0, *b1;
719       vlib_buffer_t *pref0, *pref1;
720       i16 delta0, delta1;
721       u16 new_data_len0, new_data_len1;
722       u16 new_pkt_len0, new_pkt_len1;
723       u32 any_clone;
724
725       pi0 = from[2];
726       pi1 = from[3];
727       pref0 = vlib_get_buffer (vm, pi0);
728       pref1 = vlib_get_buffer (vm, pi1);
729
730       prefmb0 = rte_mbuf_from_vlib_buffer (pref0);
731       prefmb1 = rte_mbuf_from_vlib_buffer (pref1);
732
733       CLIB_PREFETCH (prefmb0, CLIB_CACHE_LINE_BYTES, LOAD);
734       CLIB_PREFETCH (pref0, CLIB_CACHE_LINE_BYTES, LOAD);
735       CLIB_PREFETCH (prefmb1, CLIB_CACHE_LINE_BYTES, LOAD);
736       CLIB_PREFETCH (pref1, CLIB_CACHE_LINE_BYTES, LOAD);
737
738       bi0 = from[0];
739       bi1 = from[1];
740       from += 2;
741
742       b0 = vlib_get_buffer (vm, bi0);
743       b1 = vlib_get_buffer (vm, bi1);
744
745       mb0 = rte_mbuf_from_vlib_buffer (b0);
746       mb1 = rte_mbuf_from_vlib_buffer (b1);
747
748       any_clone = (b0->flags & VLIB_BUFFER_RECYCLE)
749         | (b1->flags & VLIB_BUFFER_RECYCLE);
750       if (PREDICT_FALSE (any_clone != 0))
751         {
752           if (PREDICT_FALSE ((b0->flags & VLIB_BUFFER_RECYCLE) != 0))
753             {
754               struct rte_mbuf *mb0_new = dpdk_replicate_packet_mb (b0);
755               if (PREDICT_FALSE (mb0_new == 0))
756                 {
757                   vlib_error_count (vm, node->node_index,
758                                     DPDK_TX_FUNC_ERROR_REPL_FAIL, 1);
759                   b0->flags |= VLIB_BUFFER_REPL_FAIL;
760                 }
761               else
762                 mb0 = mb0_new;
763               vec_add1 (dm->recycle[my_cpu], bi0);
764             }
765           if (PREDICT_FALSE ((b1->flags & VLIB_BUFFER_RECYCLE) != 0))
766             {
767               struct rte_mbuf *mb1_new = dpdk_replicate_packet_mb (b1);
768               if (PREDICT_FALSE (mb1_new == 0))
769                 {
770                   vlib_error_count (vm, node->node_index,
771                                     DPDK_TX_FUNC_ERROR_REPL_FAIL, 1);
772                   b1->flags |= VLIB_BUFFER_REPL_FAIL;
773                 }
774               else
775                 mb1 = mb1_new;
776               vec_add1 (dm->recycle[my_cpu], bi1);
777             }
778         }
779
780       delta0 = PREDICT_FALSE (b0->flags & VLIB_BUFFER_REPL_FAIL) ? 0 :
781         vlib_buffer_length_in_chain (vm, b0) - (i16) mb0->pkt_len;
782       delta1 = PREDICT_FALSE (b1->flags & VLIB_BUFFER_REPL_FAIL) ? 0 :
783         vlib_buffer_length_in_chain (vm, b1) - (i16) mb1->pkt_len;
784
785       new_data_len0 = (u16) ((i16) mb0->data_len + delta0);
786       new_data_len1 = (u16) ((i16) mb1->data_len + delta1);
787       new_pkt_len0 = (u16) ((i16) mb0->pkt_len + delta0);
788       new_pkt_len1 = (u16) ((i16) mb1->pkt_len + delta1);
789
790       b0->current_length = new_data_len0;
791       b1->current_length = new_data_len1;
792       mb0->data_len = new_data_len0;
793       mb1->data_len = new_data_len1;
794       mb0->pkt_len = new_pkt_len0;
795       mb1->pkt_len = new_pkt_len1;
796
797       mb0->data_off = (PREDICT_FALSE (b0->flags & VLIB_BUFFER_REPL_FAIL)) ?
798         mb0->data_off : (u16) (RTE_PKTMBUF_HEADROOM + b0->current_data);
799       mb1->data_off = (PREDICT_FALSE (b1->flags & VLIB_BUFFER_REPL_FAIL)) ?
800         mb1->data_off : (u16) (RTE_PKTMBUF_HEADROOM + b1->current_data);
801
802       if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
803         {
804           if (b0->flags & VLIB_BUFFER_IS_TRACED)
805             dpdk_tx_trace_buffer (dm, node, xd, queue_id, bi0, b0);
806           if (b1->flags & VLIB_BUFFER_IS_TRACED)
807             dpdk_tx_trace_buffer (dm, node, xd, queue_id, bi1, b1);
808         }
809
810       if (PREDICT_TRUE (any_clone == 0))
811         {
812           tx_vector[i % xd->nb_tx_desc] = mb0;
813           i++;
814           tx_vector[i % xd->nb_tx_desc] = mb1;
815           i++;
816         }
817       else
818         {
819           /* cloning was done, need to check for failure */
820           if (PREDICT_TRUE ((b0->flags & VLIB_BUFFER_REPL_FAIL) == 0))
821             {
822               tx_vector[i % xd->nb_tx_desc] = mb0;
823               i++;
824             }
825           if (PREDICT_TRUE ((b1->flags & VLIB_BUFFER_REPL_FAIL) == 0))
826             {
827               tx_vector[i % xd->nb_tx_desc] = mb1;
828               i++;
829             }
830         }
831
832       n_left -= 2;
833     }
834   while (n_left > 0)
835     {
836       u32 bi0;
837       struct rte_mbuf *mb0;
838       vlib_buffer_t *b0;
839       i16 delta0;
840       u16 new_data_len0;
841       u16 new_pkt_len0;
842
843       bi0 = from[0];
844       from++;
845
846       b0 = vlib_get_buffer (vm, bi0);
847
848       mb0 = rte_mbuf_from_vlib_buffer (b0);
849       if (PREDICT_FALSE ((b0->flags & VLIB_BUFFER_RECYCLE) != 0))
850         {
851           struct rte_mbuf *mb0_new = dpdk_replicate_packet_mb (b0);
852           if (PREDICT_FALSE (mb0_new == 0))
853             {
854               vlib_error_count (vm, node->node_index,
855                                 DPDK_TX_FUNC_ERROR_REPL_FAIL, 1);
856               b0->flags |= VLIB_BUFFER_REPL_FAIL;
857             }
858           else
859             mb0 = mb0_new;
860           vec_add1 (dm->recycle[my_cpu], bi0);
861         }
862
863       delta0 = PREDICT_FALSE (b0->flags & VLIB_BUFFER_REPL_FAIL) ? 0 :
864         vlib_buffer_length_in_chain (vm, b0) - (i16) mb0->pkt_len;
865
866       new_data_len0 = (u16) ((i16) mb0->data_len + delta0);
867       new_pkt_len0 = (u16) ((i16) mb0->pkt_len + delta0);
868
869       b0->current_length = new_data_len0;
870       mb0->data_len = new_data_len0;
871       mb0->pkt_len = new_pkt_len0;
872       mb0->data_off = (PREDICT_FALSE (b0->flags & VLIB_BUFFER_REPL_FAIL)) ?
873         mb0->data_off : (u16) (RTE_PKTMBUF_HEADROOM + b0->current_data);
874
875       if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
876         if (b0->flags & VLIB_BUFFER_IS_TRACED)
877           dpdk_tx_trace_buffer (dm, node, xd, queue_id, bi0, b0);
878
879       if (PREDICT_TRUE ((b0->flags & VLIB_BUFFER_REPL_FAIL) == 0))
880         {
881           tx_vector[i % xd->nb_tx_desc] = mb0;
882           i++;
883         }
884       n_left--;
885     }
886
887   /* account for additional packets in the ring */
888   ring->tx_head += n_packets;
889   n_on_ring = ring->tx_head - ring->tx_tail;
890
891   /* transmit as many packets as possible */
892   n_packets = tx_burst_vector_internal (vm, xd, tx_vector);
893
894   /*
895    * tx_pkts is the number of packets successfully transmitted
896    * This is the number originally on ring minus the number remaining on ring
897    */
898   tx_pkts = n_on_ring - n_packets;
899
900   if (PREDICT_FALSE (dm->flowcontrol_callback != 0))
901     {
902       if (PREDICT_FALSE (n_packets))
903         {
904           /* Callback may want to enable flowcontrol */
905           dm->flowcontrol_callback (vm, xd->vlib_hw_if_index,
906                                     ring->tx_head - ring->tx_tail);
907         }
908       else
909         {
910           /* Reset head/tail to avoid unnecessary wrap */
911           ring->tx_head = 0;
912           ring->tx_tail = 0;
913         }
914     }
915   else
916     {
917       /* If there is no callback then drop any non-transmitted packets */
918       if (PREDICT_FALSE (n_packets))
919         {
920           vlib_simple_counter_main_t *cm;
921           vnet_main_t *vnm = vnet_get_main ();
922
923           cm = vec_elt_at_index (vnm->interface_main.sw_if_counters,
924                                  VNET_INTERFACE_COUNTER_TX_ERROR);
925
926           vlib_increment_simple_counter (cm, my_cpu, xd->vlib_sw_if_index,
927                                          n_packets);
928
929           vlib_error_count (vm, node->node_index, DPDK_TX_FUNC_ERROR_PKT_DROP,
930                             n_packets);
931
932           while (n_packets--)
933             rte_pktmbuf_free (tx_vector[ring->tx_tail + n_packets]);
934         }
935
936       /* Reset head/tail to avoid unnecessary wrap */
937       ring->tx_head = 0;
938       ring->tx_tail = 0;
939     }
940
941   /* Recycle replicated buffers */
942   if (PREDICT_FALSE (vec_len (dm->recycle[my_cpu])))
943     {
944       vlib_buffer_free (vm, dm->recycle[my_cpu],
945                         vec_len (dm->recycle[my_cpu]));
946       _vec_len (dm->recycle[my_cpu]) = 0;
947     }
948
949   ASSERT (ring->tx_head >= ring->tx_tail);
950
951   return tx_pkts;
952 }
953
954 static int
955 dpdk_device_renumber (vnet_hw_interface_t * hi, u32 new_dev_instance)
956 {
957 #if DPDK_VHOST_USER
958   dpdk_main_t *dm = &dpdk_main;
959   dpdk_device_t *xd = vec_elt_at_index (dm->devices, hi->dev_instance);
960
961   if (!xd || (xd->flags & DPDK_DEVICE_FLAG_VHOST_USER) == 0)
962     {
963       clib_warning
964         ("cannot renumber non-vhost-user interface (sw_if_index: %d)",
965          hi->sw_if_index);
966       return 0;
967     }
968
969   xd->vu_if_id = new_dev_instance;
970 #endif
971   return 0;
972 }
973
974 static void
975 dpdk_clear_hw_interface_counters (u32 instance)
976 {
977   dpdk_main_t *dm = &dpdk_main;
978   dpdk_device_t *xd = vec_elt_at_index (dm->devices, instance);
979
980   /*
981    * Set the "last_cleared_stats" to the current stats, so that
982    * things appear to clear from a display perspective.
983    */
984   dpdk_update_counters (xd, vlib_time_now (dm->vlib_main));
985
986   clib_memcpy (&xd->last_cleared_stats, &xd->stats, sizeof (xd->stats));
987   clib_memcpy (xd->last_cleared_xstats, xd->xstats,
988                vec_len (xd->last_cleared_xstats) *
989                sizeof (xd->last_cleared_xstats[0]));
990
991 #if DPDK_VHOST_USER
992   if (PREDICT_FALSE (xd->flags & DPDK_DEVICE_FLAG_VHOST_USER))
993     {
994       int i;
995       for (i = 0; i < xd->rx_q_used * VIRTIO_QNUM; i++)
996         {
997           xd->vu_intf->vrings[i].packets = 0;
998           xd->vu_intf->vrings[i].bytes = 0;
999         }
1000     }
1001 #endif
1002 }
1003
1004 #ifdef RTE_LIBRTE_KNI
1005 static int
1006 kni_config_network_if (u8 port_id, u8 if_up)
1007 {
1008   vnet_main_t *vnm = vnet_get_main ();
1009   dpdk_main_t *dm = &dpdk_main;
1010   dpdk_device_t *xd;
1011   uword *p;
1012
1013   p = hash_get (dm->dpdk_device_by_kni_port_id, port_id);
1014   if (p == 0)
1015     {
1016       clib_warning ("unknown interface");
1017       return 0;
1018     }
1019   else
1020     {
1021       xd = vec_elt_at_index (dm->devices, p[0]);
1022     }
1023
1024   vnet_hw_interface_set_flags (vnm, xd->vlib_hw_if_index,
1025                                if_up ? VNET_HW_INTERFACE_FLAG_LINK_UP |
1026                                ETH_LINK_FULL_DUPLEX : 0);
1027   return 0;
1028 }
1029
1030 static int
1031 kni_change_mtu (u8 port_id, unsigned new_mtu)
1032 {
1033   vnet_main_t *vnm = vnet_get_main ();
1034   dpdk_main_t *dm = &dpdk_main;
1035   dpdk_device_t *xd;
1036   uword *p;
1037   vnet_hw_interface_t *hif;
1038
1039   p = hash_get (dm->dpdk_device_by_kni_port_id, port_id);
1040   if (p == 0)
1041     {
1042       clib_warning ("unknown interface");
1043       return 0;
1044     }
1045   else
1046     {
1047       xd = vec_elt_at_index (dm->devices, p[0]);
1048     }
1049   hif = vnet_get_hw_interface (vnm, xd->vlib_hw_if_index);
1050
1051   hif->max_packet_bytes = new_mtu;
1052
1053   return 0;
1054 }
1055 #endif
1056
1057 static clib_error_t *
1058 dpdk_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
1059 {
1060   vnet_hw_interface_t *hif = vnet_get_hw_interface (vnm, hw_if_index);
1061   uword is_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
1062   dpdk_main_t *dm = &dpdk_main;
1063   dpdk_device_t *xd = vec_elt_at_index (dm->devices, hif->dev_instance);
1064   int rv = 0;
1065
1066 #ifdef RTE_LIBRTE_KNI
1067   if (xd->flags & DPDK_DEVICE_FLAG_KNI)
1068     {
1069       if (is_up)
1070         {
1071           struct rte_kni_conf conf;
1072           struct rte_kni_ops ops;
1073           vlib_main_t *vm = vlib_get_main ();
1074           vlib_buffer_main_t *bm = vm->buffer_main;
1075           memset (&conf, 0, sizeof (conf));
1076           snprintf (conf.name, RTE_KNI_NAMESIZE, "vpp%u", xd->kni_port_id);
1077           conf.mbuf_size = VLIB_BUFFER_DATA_SIZE;
1078           memset (&ops, 0, sizeof (ops));
1079           ops.port_id = xd->kni_port_id;
1080           ops.change_mtu = kni_change_mtu;
1081           ops.config_network_if = kni_config_network_if;
1082
1083           xd->kni =
1084             rte_kni_alloc (bm->pktmbuf_pools[rte_socket_id ()], &conf, &ops);
1085           if (!xd->kni)
1086             {
1087               clib_warning ("failed to allocate kni interface");
1088             }
1089           else
1090             {
1091               hif->max_packet_bytes = 1500;     /* kni interface default value */
1092               xd->flags |= DPDK_DEVICE_FLAG_ADMIN_UP;
1093             }
1094         }
1095       else
1096         {
1097           xd->flags &= ~DPDK_DEVICE_FLAG_ADMIN_UP;
1098           int kni_rv;
1099
1100           kni_rv = rte_kni_release (xd->kni);
1101           if (kni_rv < 0)
1102             clib_warning ("rte_kni_release returned %d", kni_rv);
1103         }
1104       return 0;
1105     }
1106 #endif
1107 #if DPDK_VHOST_USER
1108   if (xd->flags & DPDK_DEVICE_FLAG_VHOST_USER)
1109     {
1110       if (is_up)
1111         {
1112           if (xd->vu_is_running)
1113             vnet_hw_interface_set_flags (vnm, xd->vlib_hw_if_index,
1114                                          VNET_HW_INTERFACE_FLAG_LINK_UP |
1115                                          ETH_LINK_FULL_DUPLEX);
1116           xd->flags |= DPDK_DEVICE_FLAG_ADMIN_UP;
1117         }
1118       else
1119         {
1120           vnet_hw_interface_set_flags (vnm, xd->vlib_hw_if_index, 0);
1121           xd->flags &= ~DPDK_DEVICE_FLAG_ADMIN_UP;
1122         }
1123
1124       return 0;
1125     }
1126 #endif
1127
1128
1129   if (is_up)
1130     {
1131       f64 now = vlib_time_now (dm->vlib_main);
1132
1133       if ((xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP) == 0)
1134         rv = rte_eth_dev_start (xd->device_index);
1135
1136       if (xd->flags & DPDK_DEVICE_FLAG_PROMISC)
1137         rte_eth_promiscuous_enable (xd->device_index);
1138       else
1139         rte_eth_promiscuous_disable (xd->device_index);
1140
1141       rte_eth_allmulticast_enable (xd->device_index);
1142       xd->flags |= DPDK_DEVICE_FLAG_ADMIN_UP;
1143       dpdk_update_counters (xd, now);
1144       dpdk_update_link_state (xd, now);
1145     }
1146   else
1147     {
1148       xd->flags &= ~DPDK_DEVICE_FLAG_ADMIN_UP;
1149
1150       rte_eth_allmulticast_disable (xd->device_index);
1151       vnet_hw_interface_set_flags (vnm, xd->vlib_hw_if_index, 0);
1152       rte_eth_dev_stop (xd->device_index);
1153
1154       /* For bonded interface, stop slave links */
1155       if (xd->pmd == VNET_DPDK_PMD_BOND)
1156         {
1157           u8 slink[16];
1158           int nlink = rte_eth_bond_slaves_get (xd->device_index, slink, 16);
1159           while (nlink >= 1)
1160             {
1161               u8 dpdk_port = slink[--nlink];
1162               rte_eth_dev_stop (dpdk_port);
1163             }
1164         }
1165     }
1166
1167   if (rv < 0)
1168     clib_warning ("rte_eth_dev_%s error: %d", is_up ? "start" : "stop", rv);
1169
1170   return /* no error */ 0;
1171 }
1172
1173 /*
1174  * Dynamically redirect all pkts from a specific interface
1175  * to the specified node
1176  */
1177 static void
1178 dpdk_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index,
1179                               u32 node_index)
1180 {
1181   dpdk_main_t *xm = &dpdk_main;
1182   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
1183   dpdk_device_t *xd = vec_elt_at_index (xm->devices, hw->dev_instance);
1184
1185   /* Shut off redirection */
1186   if (node_index == ~0)
1187     {
1188       xd->per_interface_next_index = node_index;
1189       return;
1190     }
1191
1192   xd->per_interface_next_index =
1193     vlib_node_add_next (xm->vlib_main, dpdk_input_node.index, node_index);
1194 }
1195
1196
1197 static clib_error_t *
1198 dpdk_subif_add_del_function (vnet_main_t * vnm,
1199                              u32 hw_if_index,
1200                              struct vnet_sw_interface_t *st, int is_add)
1201 {
1202   dpdk_main_t *xm = &dpdk_main;
1203   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
1204   dpdk_device_t *xd = vec_elt_at_index (xm->devices, hw->dev_instance);
1205   vnet_sw_interface_t *t = (vnet_sw_interface_t *) st;
1206   int r, vlan_offload;
1207   u32 prev_subifs = xd->num_subifs;
1208   clib_error_t *err = 0;
1209
1210   if (is_add)
1211     xd->num_subifs++;
1212   else if (xd->num_subifs)
1213     xd->num_subifs--;
1214
1215   if ((xd->flags & DPDK_DEVICE_FLAG_PMD) == 0)
1216     goto done;
1217
1218   /* currently we program VLANS only for IXGBE VF and I40E VF */
1219   if ((xd->pmd != VNET_DPDK_PMD_IXGBEVF) && (xd->pmd != VNET_DPDK_PMD_I40EVF))
1220     goto done;
1221
1222   if (t->sub.eth.flags.no_tags == 1)
1223     goto done;
1224
1225   if ((t->sub.eth.flags.one_tag != 1) || (t->sub.eth.flags.exact_match != 1))
1226     {
1227       xd->num_subifs = prev_subifs;
1228       err = clib_error_return (0, "unsupported VLAN setup");
1229       goto done;
1230     }
1231
1232   vlan_offload = rte_eth_dev_get_vlan_offload (xd->device_index);
1233   vlan_offload |= ETH_VLAN_FILTER_OFFLOAD;
1234
1235   if ((r = rte_eth_dev_set_vlan_offload (xd->device_index, vlan_offload)))
1236     {
1237       xd->num_subifs = prev_subifs;
1238       err = clib_error_return (0, "rte_eth_dev_set_vlan_offload[%d]: err %d",
1239                                xd->device_index, r);
1240       goto done;
1241     }
1242
1243
1244   if ((r =
1245        rte_eth_dev_vlan_filter (xd->device_index, t->sub.eth.outer_vlan_id,
1246                                 is_add)))
1247     {
1248       xd->num_subifs = prev_subifs;
1249       err = clib_error_return (0, "rte_eth_dev_vlan_filter[%d]: err %d",
1250                                xd->device_index, r);
1251       goto done;
1252     }
1253
1254 done:
1255   if (xd->num_subifs)
1256     xd->flags |= DPDK_DEVICE_FLAG_HAVE_SUBIF;
1257   else
1258     xd->flags &= ~DPDK_DEVICE_FLAG_HAVE_SUBIF;
1259
1260   return err;
1261 }
1262
1263 /* *INDENT-OFF* */
1264 VNET_DEVICE_CLASS (dpdk_device_class) = {
1265   .name = "dpdk",
1266   .tx_function = dpdk_interface_tx,
1267   .tx_function_n_errors = DPDK_TX_FUNC_N_ERROR,
1268   .tx_function_error_strings = dpdk_tx_func_error_strings,
1269   .format_device_name = format_dpdk_device_name,
1270   .format_device = format_dpdk_device,
1271   .format_tx_trace = format_dpdk_tx_dma_trace,
1272   .clear_counters = dpdk_clear_hw_interface_counters,
1273   .admin_up_down_function = dpdk_interface_admin_up_down,
1274   .subif_add_del_function = dpdk_subif_add_del_function,
1275   .rx_redirect_to_node = dpdk_set_interface_next_node,
1276   .no_flatten_output_chains = 1,
1277   .name_renumber = dpdk_device_renumber,
1278   .mac_addr_change_function = dpdk_set_mac_address,
1279 };
1280
1281 VLIB_DEVICE_TX_FUNCTION_MULTIARCH (dpdk_device_class, dpdk_interface_tx)
1282 /* *INDENT-ON* */
1283
1284 void
1285 dpdk_set_flowcontrol_callback (vlib_main_t * vm,
1286                                dpdk_flowcontrol_callback_t callback)
1287 {
1288   dpdk_main.flowcontrol_callback = callback;
1289 }
1290
1291 #define UP_DOWN_FLAG_EVENT 1
1292
1293
1294 u32
1295 dpdk_get_admin_up_down_in_progress (void)
1296 {
1297   return dpdk_main.admin_up_down_in_progress;
1298 }
1299
1300 uword
1301 admin_up_down_process (vlib_main_t * vm,
1302                        vlib_node_runtime_t * rt, vlib_frame_t * f)
1303 {
1304   clib_error_t *error = 0;
1305   uword event_type;
1306   uword *event_data = 0;
1307   u32 sw_if_index;
1308   u32 flags;
1309
1310   while (1)
1311     {
1312       vlib_process_wait_for_event (vm);
1313
1314       event_type = vlib_process_get_events (vm, &event_data);
1315
1316       dpdk_main.admin_up_down_in_progress = 1;
1317
1318       switch (event_type)
1319         {
1320         case UP_DOWN_FLAG_EVENT:
1321           {
1322             if (vec_len (event_data) == 2)
1323               {
1324                 sw_if_index = event_data[0];
1325                 flags = event_data[1];
1326                 error =
1327                   vnet_sw_interface_set_flags (vnet_get_main (), sw_if_index,
1328                                                flags);
1329                 clib_error_report (error);
1330               }
1331           }
1332           break;
1333         }
1334
1335       vec_reset_length (event_data);
1336
1337       dpdk_main.admin_up_down_in_progress = 0;
1338
1339     }
1340   return 0;                     /* or not */
1341 }
1342
1343 /* *INDENT-OFF* */
1344 VLIB_REGISTER_NODE (admin_up_down_process_node,static) = {
1345     .function = admin_up_down_process,
1346     .type = VLIB_NODE_TYPE_PROCESS,
1347     .name = "admin-up-down-process",
1348     .process_log2_n_stack_bytes = 17,  // 256KB
1349 };
1350 /* *INDENT-ON* */
1351
1352 /*
1353  * Asynchronously invoke vnet_sw_interface_set_flags via the admin_up_down
1354  * process. Useful for avoiding long blocking delays (>150ms) in the dpdk
1355  * drivers.
1356  * WARNING: when posting this event, no other interface-related calls should
1357  * be made (e.g. vnet_create_sw_interface()) while the event is being
1358  * processed (admin_up_down_in_progress). This is required in order to avoid
1359  * race conditions in manipulating interface data structures.
1360  */
1361 void
1362 post_sw_interface_set_flags (vlib_main_t * vm, u32 sw_if_index, u32 flags)
1363 {
1364   uword *d = vlib_process_signal_event_data
1365     (vm, admin_up_down_process_node.index,
1366      UP_DOWN_FLAG_EVENT, 2, sizeof (u32));
1367   d[0] = sw_if_index;
1368   d[1] = flags;
1369 }
1370
1371 /*
1372  * Return a copy of the DPDK port stats in dest.
1373  */
1374 clib_error_t *
1375 dpdk_get_hw_interface_stats (u32 hw_if_index, struct rte_eth_stats *dest)
1376 {
1377   dpdk_main_t *dm = &dpdk_main;
1378   vnet_main_t *vnm = vnet_get_main ();
1379   vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
1380   dpdk_device_t *xd = vec_elt_at_index (dm->devices, hi->dev_instance);
1381
1382   if (!dest)
1383     {
1384       return clib_error_return (0, "Missing or NULL argument");
1385     }
1386   if (!xd)
1387     {
1388       return clib_error_return (0,
1389                                 "Unable to get DPDK device from HW interface");
1390     }
1391
1392   dpdk_update_counters (xd, vlib_time_now (dm->vlib_main));
1393
1394   clib_memcpy (dest, &xd->stats, sizeof (xd->stats));
1395   return (0);
1396 }
1397
1398 /*
1399  * Return the number of dpdk mbufs
1400  */
1401 u32
1402 dpdk_num_mbufs (void)
1403 {
1404   dpdk_main_t *dm = &dpdk_main;
1405
1406   return dm->conf->num_mbufs;
1407 }
1408
1409 /*
1410  * Return the pmd type for a given hardware interface
1411  */
1412 dpdk_pmd_t
1413 dpdk_get_pmd_type (vnet_hw_interface_t * hi)
1414 {
1415   dpdk_main_t *dm = &dpdk_main;
1416   dpdk_device_t *xd;
1417
1418   assert (hi);
1419
1420   xd = vec_elt_at_index (dm->devices, hi->dev_instance);
1421
1422   assert (xd);
1423
1424   return xd->pmd;
1425 }
1426
1427 /*
1428  * Return the cpu socket for a given hardware interface
1429  */
1430 i8
1431 dpdk_get_cpu_socket (vnet_hw_interface_t * hi)
1432 {
1433   dpdk_main_t *dm = &dpdk_main;
1434   dpdk_device_t *xd;
1435
1436   assert (hi);
1437
1438   xd = vec_elt_at_index (dm->devices, hi->dev_instance);
1439
1440   assert (xd);
1441
1442   return xd->cpu_socket;
1443 }
1444
1445 /*
1446  * fd.io coding-style-patch-verification: ON
1447  *
1448  * Local Variables:
1449  * eval: (c-set-style "gnu")
1450  * End:
1451  */