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