Enable DPDK KNI code based on DPDK config
[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 static struct rte_mbuf * dpdk_replicate_packet_mb (vlib_buffer_t * b)
47 {
48   vlib_main_t * vm = vlib_get_main();
49   vlib_buffer_main_t * bm = vm->buffer_main;
50   struct rte_mbuf * first_mb = 0, * new_mb, * pkt_mb, ** prev_mb_next = 0;
51   u8 nb_segs, nb_segs_left;
52   u32 copy_bytes;
53   unsigned socket_id = rte_socket_id();
54
55   ASSERT (bm->pktmbuf_pools[socket_id]);
56   pkt_mb = ((struct rte_mbuf *)b)-1;
57   nb_segs = pkt_mb->nb_segs;
58   for (nb_segs_left = nb_segs; nb_segs_left; nb_segs_left--)
59     {
60       if (PREDICT_FALSE(pkt_mb == 0))
61         {
62           clib_warning ("Missing %d mbuf chain segment(s):   "
63                         "(nb_segs = %d, nb_segs_left = %d)!",
64                         nb_segs - nb_segs_left, nb_segs, nb_segs_left);
65           if (first_mb)
66             rte_pktmbuf_free(first_mb);
67           return NULL;
68         }
69       new_mb = rte_pktmbuf_alloc (bm->pktmbuf_pools[socket_id]);
70       if (PREDICT_FALSE(new_mb == 0))
71         {
72           if (first_mb)
73             rte_pktmbuf_free(first_mb);
74           return NULL;
75         }
76       
77       /*
78        * Copy packet info into 1st segment.
79        */
80       if (first_mb == 0)
81         {
82           first_mb = new_mb;
83           rte_pktmbuf_pkt_len (first_mb) = pkt_mb->pkt_len;
84           first_mb->nb_segs = pkt_mb->nb_segs;
85           first_mb->port = pkt_mb->port;
86 #ifdef DAW_FIXME // TX Offload support TBD
87           first_mb->vlan_macip = pkt_mb->vlan_macip;
88           first_mb->hash = pkt_mb->hash;
89           first_mb->ol_flags = pkt_mb->ol_flags
90 #endif
91         }
92       else
93         {
94           ASSERT(prev_mb_next != 0);
95           *prev_mb_next = new_mb;
96         }
97       
98       /*
99        * Copy packet segment data into new mbuf segment.
100        */
101       rte_pktmbuf_data_len (new_mb) = pkt_mb->data_len;
102       copy_bytes = pkt_mb->data_len + RTE_PKTMBUF_HEADROOM;
103       ASSERT(copy_bytes <= pkt_mb->buf_len);
104       memcpy(new_mb->buf_addr, pkt_mb->buf_addr, copy_bytes);
105
106       prev_mb_next = &new_mb->next;
107       pkt_mb = pkt_mb->next;
108     }
109
110   ASSERT(pkt_mb == 0);
111   __rte_mbuf_sanity_check(first_mb, 1);
112
113   return first_mb;
114 }
115
116 typedef struct {
117   u32 buffer_index;
118   u16 device_index;
119   u8 queue_index;
120   struct rte_mbuf mb;
121   /* Copy of VLIB buffer; packet data stored in pre_data. */
122   vlib_buffer_t buffer;
123 } dpdk_tx_dma_trace_t;
124
125 static void
126 dpdk_tx_trace_buffer (dpdk_main_t * dm,
127                       vlib_node_runtime_t * node,
128                       dpdk_device_t * xd,
129                       u16 queue_id,
130                       u32 buffer_index,
131                       vlib_buffer_t * buffer)
132 {
133   vlib_main_t * vm = vlib_get_main();
134   dpdk_tx_dma_trace_t * t0;
135   struct rte_mbuf * mb;
136
137   mb = ((struct rte_mbuf *)buffer)-1;
138
139   t0 = vlib_add_trace (vm, node, buffer, sizeof (t0[0]));
140   t0->queue_index = queue_id;
141   t0->device_index = xd->device_index;
142   t0->buffer_index = buffer_index;
143   memcpy (&t0->mb, mb, sizeof (t0->mb));
144   memcpy (&t0->buffer, buffer, sizeof (buffer[0]) - sizeof (buffer->pre_data));
145   memcpy (t0->buffer.pre_data, buffer->data + buffer->current_data,
146           sizeof (t0->buffer.pre_data));
147 }
148
149 /*
150  * This function calls the dpdk's tx_burst function to transmit the packets
151  * on the tx_vector. It manages a lock per-device if the device does not
152  * support multiple queues. It returns the number of packets untransmitted 
153  * on the tx_vector. If all packets are transmitted (the normal case), the 
154  * function returns 0.
155  * 
156  * The tx_burst function may not be able to transmit all packets because the 
157  * dpdk ring is full. If a flowcontrol callback function has been configured
158  * then the function simply returns. If no callback has been configured, the 
159  * function will retry calling tx_burst with the remaining packets. This will 
160  * continue until all packets are transmitted or tx_burst indicates no packets
161  * could be transmitted. (The caller can drop the remaining packets.)
162  *
163  * The function assumes there is at least one packet on the tx_vector.
164  */
165 static_always_inline
166 u32 tx_burst_vector_internal (vlib_main_t * vm, 
167                               dpdk_device_t * xd,
168                               struct rte_mbuf ** tx_vector)
169 {
170   dpdk_main_t * dm = &dpdk_main;
171   u32 n_packets;
172   u32 tx_head;
173   u32 tx_tail;
174   u32 n_retry;
175   int rv;
176   int queue_id;
177   tx_ring_hdr_t *ring;
178
179   ring = vec_header(tx_vector, sizeof(*ring));
180
181   n_packets = ring->tx_head - ring->tx_tail;
182
183   tx_head = ring->tx_head % DPDK_TX_RING_SIZE;
184
185   /*
186    * Ensure rte_eth_tx_burst is not called with 0 packets, which can lead to
187    * unpredictable results.
188    */
189   ASSERT(n_packets > 0);
190
191   /*
192    * Check for tx_vector overflow. If this fails it is a system configuration
193    * error. The ring should be sized big enough to handle the largest un-flowed
194    * off burst from a traffic manager. A larger size also helps performance
195    * a bit because it decreases the probability of having to issue two tx_burst
196    * calls due to a ring wrap.
197    */
198   ASSERT(n_packets < DPDK_TX_RING_SIZE);
199
200   /*
201    * If there is no flowcontrol callback, there is only temporary buffering
202    * on the tx_vector and so the tail should always be 0.
203    */
204   ASSERT(dm->flowcontrol_callback || ring->tx_tail == 0);
205
206   /*
207    * If there is a flowcontrol callback, don't retry any incomplete tx_bursts. 
208    * Apply backpressure instead. If there is no callback, keep retrying until
209    * a tx_burst sends no packets. n_retry of 255 essentially means no retry 
210    * limit.
211    */
212   n_retry = dm->flowcontrol_callback ? 0 : 255;
213
214   queue_id = vm->cpu_index;
215
216   do {
217       /* start the burst at the tail */
218       tx_tail = ring->tx_tail % DPDK_TX_RING_SIZE;
219
220       /* 
221        * This device only supports one TX queue,
222        * and we're running multi-threaded...
223        */
224       if (PREDICT_FALSE(xd->lockp != 0))
225         {
226           queue_id = queue_id % xd->tx_q_used;
227           while (__sync_lock_test_and_set (xd->lockp[queue_id], 1))
228             /* zzzz */
229             queue_id = (queue_id + 1) % xd->tx_q_used;
230         }
231
232       if (PREDICT_TRUE(xd->dev_type == VNET_DPDK_DEV_ETH)) 
233         {
234           if (PREDICT_TRUE(tx_head > tx_tail)) 
235             {
236               /* no wrap, transmit in one burst */
237               rv = rte_eth_tx_burst(xd->device_index, 
238                                     (uint16_t) queue_id,
239                                     &tx_vector[tx_tail], 
240                                     (uint16_t) (tx_head-tx_tail));
241             }
242           else 
243             {
244               /* 
245                * This can only happen if there is a flowcontrol callback.
246                * We need to split the transmit into two calls: one for
247                * the packets up to the wrap point, and one to continue
248                * at the start of the ring.
249                * Transmit pkts up to the wrap point.
250                */
251               rv = rte_eth_tx_burst(xd->device_index, 
252                                     (uint16_t) queue_id,
253                                     &tx_vector[tx_tail], 
254                                     (uint16_t) (DPDK_TX_RING_SIZE - tx_tail));
255
256               /* 
257                * If we transmitted everything we wanted, then allow 1 retry 
258                * so we can try to transmit the rest. If we didn't transmit
259                * everything, stop now.
260                */
261               n_retry = (rv == DPDK_TX_RING_SIZE - tx_tail) ? 1 : 0;
262             }
263         } 
264       else if (xd->dev_type == VNET_DPDK_DEV_VHOST_USER)
265         {
266           u32 offset = 0;
267 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
268           if (PREDICT_TRUE(xd->lockp == NULL)) {
269               dpdk_device_and_queue_t * dq;
270               vec_foreach (dq, dm->devices_by_cpu[vm->cpu_index])
271               {
272                 if (xd->device_index == dq->device)
273                     break; 
274               }
275               assert (dq);
276               offset = dq->queue_id * VIRTIO_QNUM;
277           } else {
278               offset = queue_id * VIRTIO_QNUM;
279           }
280 #endif
281           if (PREDICT_TRUE(tx_head > tx_tail)) 
282             {
283               /* no wrap, transmit in one burst */
284               rv = rte_vhost_enqueue_burst(&xd->vu_vhost_dev, offset + VIRTIO_RXQ,
285                                            &tx_vector[tx_tail],
286                                            (uint16_t) (tx_head-tx_tail));
287               if (PREDICT_TRUE(rv > 0))
288                 {
289                   if (dpdk_vhost_user_want_interrupt(xd, offset + VIRTIO_RXQ)) {
290                     dpdk_vu_vring *vring = &(xd->vu_intf->vrings[offset + VIRTIO_RXQ]);
291                     vring->n_since_last_int += rv;
292
293                     f64 now = vlib_time_now (vm);
294                     if (vring->int_deadline < now ||
295                         vring->n_since_last_int > dm->vhost_coalesce_frames)
296                       dpdk_vhost_user_send_interrupt(vm, xd, offset + VIRTIO_RXQ);
297                   }
298
299                   int c = rv;
300                   while(c--)
301                     rte_pktmbuf_free (tx_vector[tx_tail+c]);
302                 }
303             }
304           else
305             {
306               /*
307                * If we transmitted everything we wanted, then allow 1 retry
308                * so we can try to transmit the rest. If we didn't transmit
309                * everything, stop now.
310                */
311               rv = rte_vhost_enqueue_burst(&xd->vu_vhost_dev, offset + VIRTIO_RXQ,
312                                            &tx_vector[tx_tail], 
313                                            (uint16_t) (DPDK_TX_RING_SIZE - tx_tail));
314
315               if (PREDICT_TRUE(rv > 0))
316                 {
317                   if (dpdk_vhost_user_want_interrupt(xd, offset + VIRTIO_RXQ)) {
318                     dpdk_vu_vring *vring = &(xd->vu_intf->vrings[offset + VIRTIO_RXQ]);
319                     vring->n_since_last_int += rv;
320
321                     f64 now = vlib_time_now (vm);
322                     if (vring->int_deadline < now ||
323                         vring->n_since_last_int > dm->vhost_coalesce_frames)
324                       dpdk_vhost_user_send_interrupt(vm, xd, offset + VIRTIO_RXQ);
325                   }
326
327                   int c = rv;
328                   while(c--)
329                     rte_pktmbuf_free (tx_vector[tx_tail+c]);
330                 }
331
332               n_retry = (rv == DPDK_TX_RING_SIZE - tx_tail) ? 1 : 0;
333             }
334         }
335 #if RTE_LIBRTE_KNI
336       else if (xd->dev_type == VNET_DPDK_DEV_KNI)
337         {
338           if (PREDICT_TRUE(tx_head > tx_tail)) 
339             {
340               /* no wrap, transmit in one burst */
341               rv = rte_kni_tx_burst(xd->kni, 
342                                     &tx_vector[tx_tail], 
343                                     (uint16_t) (tx_head-tx_tail));
344             }
345           else 
346             {
347               /* 
348                * This can only happen if there is a flowcontrol callback.
349                * We need to split the transmit into two calls: one for
350                * the packets up to the wrap point, and one to continue
351                * at the start of the ring.
352                * Transmit pkts up to the wrap point.
353                */
354               rv = rte_kni_tx_burst(xd->kni, 
355                                     &tx_vector[tx_tail], 
356                                     (uint16_t) (DPDK_TX_RING_SIZE - tx_tail));
357
358               /* 
359                * If we transmitted everything we wanted, then allow 1 retry 
360                * so we can try to transmit the rest. If we didn't transmit
361                * everything, stop now.
362                */
363               n_retry = (rv == DPDK_TX_RING_SIZE - tx_tail) ? 1 : 0;
364             }
365         } 
366 #endif
367       else
368         {
369           ASSERT(0);
370           rv = 0;
371         }
372
373       if (PREDICT_FALSE(xd->lockp != 0))
374           *xd->lockp[queue_id] = 0;
375
376       if (PREDICT_FALSE(rv < 0))
377         {
378           // emit non-fatal message, bump counter
379           vnet_main_t * vnm = dm->vnet_main;
380           vnet_interface_main_t * im = &vnm->interface_main;
381           u32 node_index;
382
383           node_index = vec_elt_at_index(im->hw_interfaces, 
384                                         xd->vlib_hw_if_index)->tx_node_index;
385
386           vlib_error_count (vm, node_index, DPDK_TX_FUNC_ERROR_BAD_RETVAL, 1);
387           clib_warning ("rte_eth_tx_burst[%d]: error %d", xd->device_index, rv);
388           return n_packets; // untransmitted packets
389         }
390       ring->tx_tail += (u16)rv;
391       n_packets -= (uint16_t) rv;
392   } while (rv && n_packets && (n_retry>0));
393
394   return n_packets;
395 }
396
397
398 /*
399  * This function transmits any packets on the interface's tx_vector and returns
400  * the number of packets untransmitted on the tx_vector. If the tx_vector is 
401  * empty the function simply returns 0. 
402  *
403  * It is intended to be called by a traffic manager which has flowed-off an
404  * interface to see if the interface can be flowed-on again.
405  */
406 u32 dpdk_interface_tx_vector (vlib_main_t * vm, u32 dev_instance)
407 {
408   dpdk_main_t * dm = &dpdk_main;
409   dpdk_device_t * xd;
410   int queue_id;
411   struct rte_mbuf ** tx_vector;
412   tx_ring_hdr_t *ring;
413  
414   /* param is dev_instance and not hw_if_index to save another lookup */
415   xd = vec_elt_at_index (dm->devices, dev_instance);
416
417   queue_id = vm->cpu_index;
418   tx_vector = xd->tx_vectors[queue_id];
419
420   /* If no packets on the ring, don't bother calling tx function */
421   ring = vec_header(tx_vector, sizeof(*ring));
422   if (ring->tx_head == ring->tx_tail) 
423     {
424       return 0;
425     }
426
427   return tx_burst_vector_internal (vm, xd, tx_vector);
428 }
429
430 /*
431  * Transmits the packets on the frame to the interface associated with the
432  * node. It first copies packets on the frame to a tx_vector containing the 
433  * rte_mbuf pointers. It then passes this vector to tx_burst_vector_internal 
434  * which calls the dpdk tx_burst function.
435  *
436  * The tx_vector is treated slightly differently depending on whether or
437  * not a flowcontrol callback function has been configured. If there is no
438  * callback, the tx_vector is a temporary array of rte_mbuf packet pointers.
439  * Its entries are written and consumed before the function exits. 
440  *
441  * If there is a callback then the transmit is being invoked in the presence
442  * of a traffic manager. Here the tx_vector is treated like a ring of rte_mbuf
443  * pointers. If not all packets can be transmitted, the untransmitted packets
444  * stay on the tx_vector until the next call. The callback allows the traffic
445  * manager to flow-off dequeues to the interface. The companion function
446  * dpdk_interface_tx_vector() allows the traffic manager to detect when
447  * it should flow-on the interface again.
448  */
449 static uword
450 dpdk_interface_tx (vlib_main_t * vm,
451            vlib_node_runtime_t * node,
452            vlib_frame_t * f)
453 {
454   dpdk_main_t * dm = &dpdk_main;
455   vnet_interface_output_runtime_t * rd = (void *) node->runtime_data;
456   dpdk_device_t * xd = vec_elt_at_index (dm->devices, rd->dev_instance);
457   u32 n_packets = f->n_vectors;
458   u32 n_left;
459   u32 * from;
460   struct rte_mbuf ** tx_vector;
461   int i;
462   int queue_id;
463   u32 my_cpu;
464   u32 tx_pkts = 0;
465   tx_ring_hdr_t *ring;
466   u32 n_on_ring;
467
468   my_cpu = vm->cpu_index;
469
470   queue_id = my_cpu;
471
472   tx_vector = xd->tx_vectors[queue_id];
473   ring = vec_header(tx_vector, sizeof(*ring));
474
475   n_on_ring = ring->tx_head - ring->tx_tail;
476   from = vlib_frame_vector_args (f);
477
478   ASSERT(n_packets <= VLIB_FRAME_SIZE);
479
480   if (PREDICT_FALSE(n_on_ring + n_packets > DPDK_TX_RING_SIZE))
481     {
482       /*
483        * Overflowing the ring should never happen. 
484        * If it does then drop the whole frame.
485        */
486       vlib_error_count (vm, node->node_index, DPDK_TX_FUNC_ERROR_RING_FULL,
487                         n_packets);
488
489       while (n_packets--) 
490         {
491           u32 bi0 = from[n_packets];
492           vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
493           struct rte_mbuf *mb0 = ((struct rte_mbuf *)b0) - 1;
494           rte_pktmbuf_free (mb0);
495         }
496       return n_on_ring;
497     }
498
499   if (PREDICT_FALSE(dm->tx_pcap_enable))
500     {
501       n_left = n_packets;
502       while (n_left > 0)
503         {
504           u32 bi0 = from[0];
505           vlib_buffer_t * b0 = vlib_get_buffer (vm, bi0);
506           if (dm->pcap_sw_if_index == 0 ||
507               dm->pcap_sw_if_index == vnet_buffer(b0)->sw_if_index [VLIB_TX])
508               pcap_add_buffer (&dm->pcap_main, vm, bi0, 512);
509           from++;
510           n_left--;
511         }
512     }
513
514   from = vlib_frame_vector_args (f);
515   n_left = n_packets;
516   i = ring->tx_head % DPDK_TX_RING_SIZE;
517
518   while (n_left >= 4)
519     {
520       u32 bi0, bi1;
521       u32 pi0, pi1;
522       struct rte_mbuf * mb0, * mb1;
523       struct rte_mbuf * prefmb0, * prefmb1;
524       vlib_buffer_t * b0, * b1;
525       vlib_buffer_t * pref0, * pref1;
526       i16 delta0, delta1;
527       u16 new_data_len0, new_data_len1;
528       u16 new_pkt_len0, new_pkt_len1;
529       u32 any_clone;
530
531       pi0 = from[2];
532       pi1 = from[3];
533       pref0 = vlib_get_buffer (vm, pi0);
534       pref1 = vlib_get_buffer (vm, pi1);
535
536       prefmb0 = ((struct rte_mbuf *)pref0) - 1;
537       prefmb1 = ((struct rte_mbuf *)pref1) - 1;
538       
539       CLIB_PREFETCH(prefmb0, CLIB_CACHE_LINE_BYTES, LOAD);
540       CLIB_PREFETCH(pref0, CLIB_CACHE_LINE_BYTES, LOAD);
541       CLIB_PREFETCH(prefmb1, CLIB_CACHE_LINE_BYTES, LOAD);
542       CLIB_PREFETCH(pref1, CLIB_CACHE_LINE_BYTES, LOAD);
543
544       bi0 = from[0];
545       bi1 = from[1];
546       from += 2;
547       
548       b0 = vlib_get_buffer (vm, bi0);
549       b1 = vlib_get_buffer (vm, bi1);
550
551       mb0 = ((struct rte_mbuf *)b0) - 1;
552       mb1 = ((struct rte_mbuf *)b1) - 1;
553
554       any_clone = b0->clone_count | b1->clone_count;
555       if (PREDICT_FALSE(any_clone != 0))
556         {
557           if (PREDICT_FALSE(b0->clone_count != 0))
558         {
559           struct rte_mbuf * mb0_new = dpdk_replicate_packet_mb (b0);
560           if (PREDICT_FALSE(mb0_new == 0))
561             {
562               vlib_error_count (vm, node->node_index,
563                     DPDK_TX_FUNC_ERROR_REPL_FAIL, 1);
564               b0->flags |= VLIB_BUFFER_REPL_FAIL;
565             }
566           else
567             mb0 = mb0_new;
568           vec_add1 (dm->recycle[my_cpu], bi0);
569         }
570           if (PREDICT_FALSE(b1->clone_count != 0))
571         {
572           struct rte_mbuf * mb1_new = dpdk_replicate_packet_mb (b1);
573           if (PREDICT_FALSE(mb1_new == 0))
574             {
575               vlib_error_count (vm, node->node_index,
576                     DPDK_TX_FUNC_ERROR_REPL_FAIL, 1);
577               b1->flags |= VLIB_BUFFER_REPL_FAIL;
578             }
579           else
580             mb1 = mb1_new;
581           vec_add1 (dm->recycle[my_cpu], bi1);
582         }
583     }
584
585       delta0 = PREDICT_FALSE(b0->flags & VLIB_BUFFER_REPL_FAIL) ? 0 :
586     vlib_buffer_length_in_chain (vm, b0) - (i16) mb0->pkt_len;
587       delta1 = PREDICT_FALSE(b1->flags & VLIB_BUFFER_REPL_FAIL) ? 0 :
588     vlib_buffer_length_in_chain (vm, b1) - (i16) mb1->pkt_len;
589       
590       new_data_len0 = (u16)((i16) mb0->data_len + delta0);
591       new_data_len1 = (u16)((i16) mb1->data_len + delta1);
592       new_pkt_len0 = (u16)((i16) mb0->pkt_len + delta0);
593       new_pkt_len1 = (u16)((i16) mb1->pkt_len + delta1);
594
595       b0->current_length = new_data_len0;
596       b1->current_length = new_data_len1;
597       mb0->data_len = new_data_len0;
598       mb1->data_len = new_data_len1;
599       mb0->pkt_len = new_pkt_len0;
600       mb1->pkt_len = new_pkt_len1;
601
602       mb0->data_off = (PREDICT_FALSE(b0->flags & VLIB_BUFFER_REPL_FAIL)) ?
603           mb0->data_off : (u16)(RTE_PKTMBUF_HEADROOM + b0->current_data);
604       mb1->data_off = (PREDICT_FALSE(b1->flags & VLIB_BUFFER_REPL_FAIL)) ?
605           mb1->data_off : (u16)(RTE_PKTMBUF_HEADROOM + b1->current_data);
606
607       if (PREDICT_FALSE(node->flags & VLIB_NODE_FLAG_TRACE))
608     {
609           if (b0->flags & VLIB_BUFFER_IS_TRACED)
610               dpdk_tx_trace_buffer (dm, node, xd, queue_id, bi0, b0);
611           if (b1->flags & VLIB_BUFFER_IS_TRACED)
612               dpdk_tx_trace_buffer (dm, node, xd, queue_id, bi1, b1);
613     }
614
615       if (PREDICT_TRUE(any_clone == 0))
616         {
617       tx_vector[i % DPDK_TX_RING_SIZE] = mb0;
618           i++;
619       tx_vector[i % DPDK_TX_RING_SIZE] = mb1;
620           i++;
621         }
622       else
623         {
624           /* cloning was done, need to check for failure */
625           if (PREDICT_TRUE((b0->flags & VLIB_BUFFER_REPL_FAIL) == 0))
626             {
627           tx_vector[i % DPDK_TX_RING_SIZE] = mb0;
628               i++;
629             }
630           if (PREDICT_TRUE((b1->flags & VLIB_BUFFER_REPL_FAIL) == 0))
631             {
632           tx_vector[i % DPDK_TX_RING_SIZE] = mb1;
633               i++;
634             }
635         }
636
637       n_left -= 2;
638     }
639   while (n_left > 0)
640     {
641       u32 bi0;
642       struct rte_mbuf * mb0;
643       vlib_buffer_t * b0;
644       i16 delta0;
645       u16 new_data_len0;
646       u16 new_pkt_len0;
647
648       bi0 = from[0];
649       from++;
650       
651       b0 = vlib_get_buffer (vm, bi0);
652
653       mb0 = ((struct rte_mbuf *)b0) - 1;
654       if (PREDICT_FALSE(b0->clone_count != 0))
655     {
656       struct rte_mbuf * mb0_new = dpdk_replicate_packet_mb (b0);
657       if (PREDICT_FALSE(mb0_new == 0))
658         {
659           vlib_error_count (vm, node->node_index,
660                 DPDK_TX_FUNC_ERROR_REPL_FAIL, 1);
661           b0->flags |= VLIB_BUFFER_REPL_FAIL;
662         }
663       else
664         mb0 = mb0_new;
665       vec_add1 (dm->recycle[my_cpu], bi0);
666     }
667
668       delta0 = PREDICT_FALSE(b0->flags & VLIB_BUFFER_REPL_FAIL) ? 0 :
669     vlib_buffer_length_in_chain (vm, b0) - (i16) mb0->pkt_len;
670       
671       new_data_len0 = (u16)((i16) mb0->data_len + delta0);
672       new_pkt_len0 = (u16)((i16) mb0->pkt_len + delta0);
673       
674       b0->current_length = new_data_len0;
675       mb0->data_len = new_data_len0;
676       mb0->pkt_len = new_pkt_len0;
677       mb0->data_off = (PREDICT_FALSE(b0->flags & VLIB_BUFFER_REPL_FAIL)) ?
678           mb0->data_off : (u16)(RTE_PKTMBUF_HEADROOM + b0->current_data);
679
680       if (PREDICT_FALSE(node->flags & VLIB_NODE_FLAG_TRACE))
681           if (b0->flags & VLIB_BUFFER_IS_TRACED)
682               dpdk_tx_trace_buffer (dm, node, xd, queue_id, bi0, b0);
683
684       if (PREDICT_TRUE((b0->flags & VLIB_BUFFER_REPL_FAIL) == 0))
685         {
686       tx_vector[i % DPDK_TX_RING_SIZE] = mb0;
687           i++;
688         }
689       n_left--;
690     }
691
692   /* account for additional packets in the ring */
693   ring->tx_head += n_packets;
694   n_on_ring = ring->tx_head - ring->tx_tail;
695
696   /* transmit as many packets as possible */
697   n_packets = tx_burst_vector_internal (vm, xd, tx_vector);
698
699   /*
700    * tx_pkts is the number of packets successfully transmitted
701    * This is the number originally on ring minus the number remaining on ring
702    */
703   tx_pkts = n_on_ring - n_packets; 
704
705   if (PREDICT_FALSE(dm->flowcontrol_callback != 0))
706     {
707       if (PREDICT_FALSE(n_packets))
708         {
709           /* Callback may want to enable flowcontrol */
710           dm->flowcontrol_callback(vm, xd->vlib_hw_if_index, ring->tx_head - ring->tx_tail);
711         } 
712       else 
713         {
714           /* Reset head/tail to avoid unnecessary wrap */
715           ring->tx_head = 0;
716           ring->tx_tail = 0;
717         }
718     }
719   else 
720     {
721       /* If there is no callback then drop any non-transmitted packets */
722       if (PREDICT_FALSE(n_packets))
723         {
724           vlib_simple_counter_main_t * cm;
725           vnet_main_t * vnm = vnet_get_main();
726
727           cm = vec_elt_at_index (vnm->interface_main.sw_if_counters,
728                                  VNET_INTERFACE_COUNTER_TX_ERROR);
729
730           vlib_increment_simple_counter (cm, my_cpu, xd->vlib_sw_if_index, n_packets);
731
732           vlib_error_count (vm, node->node_index, DPDK_TX_FUNC_ERROR_PKT_DROP,
733                 n_packets);
734
735           while (n_packets--)
736             rte_pktmbuf_free (tx_vector[ring->tx_tail + n_packets]);
737         }
738
739         /* Reset head/tail to avoid unnecessary wrap */
740         ring->tx_head = 0;
741         ring->tx_tail = 0;
742     }
743
744   /* Recycle replicated buffers */
745   if (PREDICT_FALSE(vec_len(dm->recycle[my_cpu])))
746     {
747       vlib_buffer_free (vm, dm->recycle[my_cpu], vec_len(dm->recycle[my_cpu]));
748       _vec_len(dm->recycle[my_cpu]) = 0;
749     }
750
751   ASSERT(ring->tx_head >= ring->tx_tail);
752
753   return tx_pkts;
754 }
755
756 static int dpdk_device_renumber (vnet_hw_interface_t * hi,
757                                  u32 new_dev_instance)
758 {
759   dpdk_main_t * dm = &dpdk_main;
760   dpdk_device_t * xd = vec_elt_at_index (dm->devices, hi->dev_instance);
761
762   if (!xd || xd->dev_type != VNET_DPDK_DEV_VHOST_USER) {
763       clib_warning("cannot renumber non-vhost-user interface (sw_if_index: %d)",
764               hi->sw_if_index);
765       return 0;
766   }
767
768   xd->vu_if_id = new_dev_instance;
769   return 0;
770 }
771
772 static u8 * format_dpdk_device_name (u8 * s, va_list * args)
773 {
774   dpdk_main_t * dm = &dpdk_main;
775   char *devname_format;
776   char *device_name;
777   u32 i = va_arg (*args, u32);
778   struct rte_eth_dev_info dev_info;
779   u8 * ret;
780
781   if (dm->interface_name_format_decimal)
782     devname_format = "%s%d/%d/%d";
783   else
784     devname_format = "%s%x/%x/%x";
785
786 #ifdef RTE_LIBRTE_KNI
787   if (dm->devices[i].dev_type == VNET_DPDK_DEV_KNI) {
788        return format(s, "kni%d", dm->devices[i].kni_port_id);
789   } else
790 #endif
791   if (dm->devices[i].dev_type == VNET_DPDK_DEV_VHOST_USER) {
792        return format(s, "VirtualEthernet0/0/%d", dm->devices[i].vu_if_id);
793   }
794   switch (dm->devices[i].port_type)
795     {
796     case VNET_DPDK_PORT_TYPE_ETH_1G:
797       device_name = "GigabitEthernet";
798       break;
799
800     case VNET_DPDK_PORT_TYPE_ETH_10G:
801       device_name = "TenGigabitEthernet";
802       break;
803
804     case VNET_DPDK_PORT_TYPE_ETH_40G:
805       device_name = "FortyGigabitEthernet";
806       break;
807
808     case VNET_DPDK_PORT_TYPE_ETH_SWITCH:
809       device_name = "EthernetSwitch";
810       break;
811
812   #ifdef NETMAP
813     case VNET_DPDK_PORT_TYPE_NETMAP:
814         rte_eth_dev_info_get(i, &dev_info);
815         return format(s, "netmap:%s", dev_info.driver_name);
816   #endif
817
818     case VNET_DPDK_PORT_TYPE_AF_PACKET:
819       rte_eth_dev_info_get(i, &dev_info);
820       return format(s, "af_packet%d", dm->devices[i].af_packet_port_id);
821
822     default:
823     case VNET_DPDK_PORT_TYPE_UNKNOWN:
824       device_name = "UnknownEthernet";
825       break;
826     }
827
828   rte_eth_dev_info_get(i, &dev_info);
829   ret = format (s, devname_format, device_name, dev_info.pci_dev->addr.bus,
830                  dev_info.pci_dev->addr.devid,
831                  dev_info.pci_dev->addr.function);
832
833   /* address Chelsio cards which share PCI address */
834         if (dm->devices[i].pmd ==  VNET_DPDK_PMD_CXGBE) {
835     struct rte_eth_dev_info di;
836
837     di.pci_dev = 0;
838     rte_eth_dev_info_get(i+1, &di);
839     if (di.pci_dev && memcmp(&dev_info.pci_dev->addr, &di.pci_dev->addr,
840         sizeof(struct rte_pci_addr)) == 0)
841             return format(ret, "/0");   
842
843     di.pci_dev = 0;
844     rte_eth_dev_info_get(i-1, &di);
845     if (di.pci_dev && memcmp(&dev_info.pci_dev->addr, &di.pci_dev->addr,
846         sizeof(struct rte_pci_addr)) == 0)
847             return format(ret, "/1");   
848         }
849   return ret;
850 }
851
852 static u8 * format_dpdk_device_type (u8 * s, va_list * args)
853 {
854   dpdk_main_t * dm = &dpdk_main;
855   char *dev_type;
856   u32 i = va_arg (*args, u32);
857
858   if (dm->devices[i].dev_type == VNET_DPDK_DEV_KNI) {
859        return format(s, "Kernel NIC Interface");
860   } else if (dm->devices[i].dev_type == VNET_DPDK_DEV_VHOST_USER) {
861        return format(s, "vhost-user interface");
862   }
863
864   switch (dm->devices[i].pmd)
865     {
866     case VNET_DPDK_PMD_E1000EM:
867         dev_type = "Intel 82540EM (e1000)";
868         break;
869
870     case VNET_DPDK_PMD_IGB:
871         dev_type = "Intel e1000";
872         break;
873
874     case VNET_DPDK_PMD_I40E:
875         dev_type = "Intel X710/XL710 Family";
876         break;
877
878     case VNET_DPDK_PMD_I40EVF:
879         dev_type = "Intel X710/XL710 Family VF";
880         break;
881
882     case VNET_DPDK_PMD_FM10K:
883         dev_type = "Intel FM10000 Family Ethernet Switch";
884         break;
885
886     case VNET_DPDK_PMD_IGBVF:
887         dev_type = "Intel e1000 VF";
888         break;
889
890     case VNET_DPDK_PMD_VIRTIO:
891         dev_type = "Red Hat Virtio";
892         break;
893
894     case VNET_DPDK_PMD_IXGBEVF:
895         dev_type = "Intel 82599 VF";
896         break;
897
898     case VNET_DPDK_PMD_IXGBE:
899         dev_type = "Intel 82599";
900         break;
901
902     case VNET_DPDK_PMD_VICE:
903     case VNET_DPDK_PMD_ENIC:
904         dev_type = "Cisco VIC";
905         break;
906
907     case VNET_DPDK_PMD_CXGBE:
908         dev_type = "Chelsio T4/T5";
909         break;
910
911     case VNET_DPDK_PMD_VMXNET3:
912         dev_type = "VMware VMXNET3";
913         break;
914
915 #ifdef NETMAP
916     case VNET_DPDK_PMD_NETMAP:
917         dev_type = "Netmap/Vale";
918         break;
919 #endif
920
921     case VNET_DPDK_PMD_AF_PACKET:
922   dev_type = "af_packet";
923   break;
924
925     default:
926     case VNET_DPDK_PMD_UNKNOWN:
927         dev_type = "### UNKNOWN ###";
928         break;
929     }
930
931   return format (s, dev_type);
932 }
933
934 static u8 * format_dpdk_link_status (u8 * s, va_list * args)
935 {
936   dpdk_device_t * xd = va_arg (*args, dpdk_device_t *);
937   struct rte_eth_link * l = &xd->link;
938   vnet_main_t * vnm = vnet_get_main();
939   vnet_hw_interface_t * hi = vnet_get_hw_interface (vnm, xd->vlib_hw_if_index);
940   
941   s = format (s, "%s ", l->link_status ? "up" : "down");
942   if (l->link_status)
943     {
944       u32 promisc = rte_eth_promiscuous_get (xd->device_index);
945
946       s = format (s, "%s duplex ", (l->link_duplex == ETH_LINK_FULL_DUPLEX) ?
947                   "full" : "half");
948       s = format (s, "speed %u mtu %d %s\n", l->link_speed,
949                   hi->max_packet_bytes, promisc ? " promisc" : "");
950     }
951   else
952     s = format (s, "\n");
953
954   return s;
955 }
956
957 #define _line_len 72
958 #define _(v, str)                                            \
959 if (bitmap & v) {                                            \
960   if (format_get_indent (s) > next_split ) {                 \
961     next_split += _line_len;                                 \
962     s = format(s,"\n%U", format_white_space, indent);        \
963   }                                                          \
964   s = format(s, "%s ", str);                                 \
965 }
966
967 static u8 * format_dpdk_rss_hf_name(u8 * s, va_list * args)
968 {
969   u64 bitmap = va_arg (*args, u64);
970   int next_split = _line_len;
971   int indent = format_get_indent (s);
972
973   if (!bitmap)
974     return format(s, "none");
975
976   foreach_dpdk_rss_hf
977
978   return s;
979 }
980
981 static u8 * format_dpdk_rx_offload_caps(u8 * s, va_list * args)
982 {
983   u32 bitmap = va_arg (*args, u32);
984   int next_split = _line_len;
985   int indent = format_get_indent (s);
986
987   if (!bitmap)
988     return format(s, "none");
989
990   foreach_dpdk_rx_offload_caps
991
992   return s;
993 }
994
995 static u8 * format_dpdk_tx_offload_caps(u8 * s, va_list * args)
996 {
997   u32 bitmap = va_arg (*args, u32);
998   int next_split = _line_len;
999   int indent = format_get_indent (s);
1000   if (!bitmap)
1001     return format(s, "none");
1002
1003   foreach_dpdk_tx_offload_caps
1004
1005   return s;
1006 }
1007
1008 #undef _line_len
1009 #undef _
1010
1011 static u8 * format_dpdk_device (u8 * s, va_list * args)
1012 {
1013   u32 dev_instance = va_arg (*args, u32);
1014   int verbose = va_arg (*args, int);
1015   dpdk_main_t * dm = &dpdk_main;
1016   dpdk_device_t * xd = vec_elt_at_index (dm->devices, dev_instance);
1017   uword indent = format_get_indent (s);
1018   f64 now = vlib_time_now (dm->vlib_main);
1019
1020   dpdk_update_counters (xd, now);
1021   dpdk_update_link_state (xd, now);
1022
1023   s = format (s, "%U\n%Ucarrier %U",
1024               format_dpdk_device_type, xd->device_index,
1025               format_white_space, indent + 2,
1026               format_dpdk_link_status, xd);
1027
1028   if (verbose > 1 && xd->dev_type == VNET_DPDK_DEV_ETH)
1029     {
1030       struct rte_eth_dev_info di;
1031       struct rte_pci_device * pci;
1032       struct rte_eth_rss_conf rss_conf;
1033       int vlan_off;
1034
1035       rss_conf.rss_key = 0;
1036       rte_eth_dev_info_get(xd->device_index, &di);
1037       rte_eth_dev_rss_hash_conf_get(xd->device_index, &rss_conf);
1038       pci = di.pci_dev;
1039
1040       if (pci)
1041         s = format(s, "%Upci id:            device %04x:%04x subsystem %04x:%04x\n"
1042                       "%Upci address:       %04x:%02x:%02x.%02x\n",
1043                    format_white_space, indent + 2,
1044                    pci->id.vendor_id, pci->id.device_id,
1045                    pci->id.subsystem_vendor_id,
1046                    pci->id.subsystem_device_id,
1047                    format_white_space, indent + 2,
1048                    pci->addr.domain, pci->addr.bus,
1049                    pci->addr.devid, pci->addr.function);
1050       s = format(s, "%Umax rx packet len: %d\n",
1051                  format_white_space, indent + 2, di.max_rx_pktlen);
1052       s = format(s, "%Upromiscuous:       unicast %s all-multicast %s\n",
1053                  format_white_space, indent + 2,
1054                  rte_eth_promiscuous_get(xd->device_index) ? "on" : "off",
1055                  rte_eth_promiscuous_get(xd->device_index) ? "on" : "off");
1056       vlan_off = rte_eth_dev_get_vlan_offload(xd->device_index);
1057       s = format(s, "%Uvlan offload:      strip %s filter %s qinq %s\n",
1058                  format_white_space, indent + 2,
1059                  vlan_off & ETH_VLAN_STRIP_OFFLOAD ? "on" : "off",
1060                  vlan_off & ETH_VLAN_FILTER_OFFLOAD ? "on" : "off",
1061                  vlan_off & ETH_VLAN_EXTEND_OFFLOAD ? "on" : "off");
1062       s = format(s, "%Uqueue size (max):  rx %d (%d) tx %d (%d)\n",
1063                  format_white_space, indent + 2,
1064                  xd->rx_q_used, di.max_rx_queues,
1065                  xd->tx_q_used, di.max_tx_queues);
1066       s = format(s, "%Urx offload caps:   %U\n",
1067                  format_white_space, indent + 2,
1068                  format_dpdk_rx_offload_caps, di.rx_offload_capa);
1069       s = format(s, "%Utx offload caps:   %U\n",
1070                  format_white_space, indent + 2,
1071                  format_dpdk_tx_offload_caps, di.tx_offload_capa);
1072       s = format(s, "%Urss active:        %U\n"
1073                     "%Urss supported:     %U\n",
1074                  format_white_space, indent + 2,
1075                  format_dpdk_rss_hf_name, rss_conf.rss_hf,
1076                  format_white_space, indent + 2,
1077                  format_dpdk_rss_hf_name, di.flow_type_rss_offloads);
1078     }
1079
1080   if (xd->cpu_socket > -1)
1081     s = format (s, "%Ucpu socket %d",
1082                 format_white_space, indent + 2,
1083                 xd->cpu_socket);
1084
1085   /* $$$ MIB counters  */
1086
1087   {
1088 #define _(N, V)                                                 \
1089     if (xd->stats.V != 0)                                       \
1090       s = format (s, "\n%U%-40U%16Ld",                          \
1091                   format_white_space, indent + 2,               \
1092                   format_c_identifier, #N, xd->stats.V);
1093     
1094     foreach_dpdk_counter
1095 #undef _
1096   }
1097
1098   u8 * xs = 0;
1099   struct rte_eth_xstats * xstat;
1100
1101   vec_foreach(xstat, xd->xstats)
1102     {
1103       if (xstat->value)
1104         {
1105           /* format_c_identifier don't like c strings inside vector */
1106           u8 * name = format(0,"%s", xstat->name);
1107           xs = format(xs, "\n%U%-38U%16Ld",
1108                       format_white_space, indent + 4,
1109                       format_c_identifier, name, xstat->value);
1110           vec_free(name);
1111         }
1112     }
1113
1114   if (xs)
1115     {
1116       s = format(s, "\n%Uextended stats:%v",
1117                  format_white_space, indent + 2, xs);
1118       vec_free(xs);
1119     }
1120
1121   return s;
1122 }
1123
1124 static u8 * format_dpdk_tx_dma_trace (u8 * s, va_list * va)
1125 {
1126   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
1127   CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
1128   CLIB_UNUSED (vnet_main_t * vnm) = vnet_get_main();
1129   dpdk_tx_dma_trace_t * t = va_arg (*va, dpdk_tx_dma_trace_t *);
1130   dpdk_main_t * dm = &dpdk_main;
1131   dpdk_device_t * xd = vec_elt_at_index (dm->devices, t->device_index);
1132   uword indent = format_get_indent (s);
1133   vnet_sw_interface_t * sw = vnet_get_sw_interface (vnm, xd->vlib_sw_if_index);
1134
1135   s = format (s, "%U tx queue %d",
1136               format_vnet_sw_interface_name, vnm, sw,
1137               t->queue_index);
1138
1139   s = format (s, "\n%Ubuffer 0x%x: %U",
1140               format_white_space, indent,
1141               t->buffer_index,
1142               format_vlib_buffer, &t->buffer);
1143
1144   s = format (s, "\n%U%U", format_white_space, indent,
1145               format_ethernet_header_with_length, t->buffer.pre_data,
1146               sizeof (t->buffer.pre_data));
1147   
1148   return s;
1149 }
1150
1151 static void dpdk_clear_hw_interface_counters (u32 instance)
1152 {
1153   dpdk_main_t * dm = &dpdk_main;
1154   dpdk_device_t * xd = vec_elt_at_index (dm->devices, instance);
1155
1156   /*
1157    * DAW-FIXME: VMXNET3 device stop/start doesn't work, 
1158    * therefore fake the stop in the dpdk driver by
1159    * silently dropping all of the incoming pkts instead of 
1160    * stopping the driver / hardware.
1161    */
1162   if (xd->admin_up != 0xff)
1163     {
1164       rte_eth_stats_reset (xd->device_index);
1165       memset (&xd->last_stats, 0, sizeof (xd->last_stats));
1166       dpdk_update_counters (xd, vlib_time_now (dm->vlib_main));
1167     }
1168   else
1169     {
1170       rte_eth_stats_reset (xd->device_index);
1171       memset(&xd->stats, 0, sizeof(xd->stats));
1172       memset (&xd->last_stats, 0, sizeof (xd->last_stats));
1173     }
1174   rte_eth_xstats_reset(xd->device_index);
1175 }
1176
1177 #ifdef RTE_LIBRTE_KNI
1178 static int
1179 kni_config_network_if(u8 port_id, u8 if_up)
1180 {
1181   vnet_main_t * vnm = vnet_get_main();
1182   dpdk_main_t * dm = &dpdk_main;
1183   dpdk_device_t * xd;
1184   uword *p;
1185
1186   p = hash_get (dm->dpdk_device_by_kni_port_id, port_id);
1187   if (p == 0) {
1188     clib_warning("unknown interface");
1189     return 0;
1190   } else {
1191     xd = vec_elt_at_index (dm->devices, p[0]);
1192   }
1193
1194   vnet_hw_interface_set_flags (vnm, xd->vlib_hw_if_index,
1195                                if_up ? VNET_HW_INTERFACE_FLAG_LINK_UP |
1196                                ETH_LINK_FULL_DUPLEX : 0);
1197   return 0;
1198 }
1199
1200 static int
1201 kni_change_mtu(u8 port_id, unsigned new_mtu)
1202 {
1203   vnet_main_t * vnm = vnet_get_main();
1204   dpdk_main_t * dm = &dpdk_main;
1205   dpdk_device_t * xd;
1206   uword *p;
1207   vnet_hw_interface_t * hif;
1208
1209   p = hash_get (dm->dpdk_device_by_kni_port_id, port_id);
1210   if (p == 0) {
1211     clib_warning("unknown interface");
1212     return 0;
1213   } else {
1214     xd = vec_elt_at_index (dm->devices, p[0]);
1215   }
1216   hif = vnet_get_hw_interface (vnm, xd->vlib_hw_if_index);
1217
1218   hif->max_packet_bytes = new_mtu;
1219
1220   return 0;
1221 }
1222 #endif
1223
1224 static clib_error_t *
1225 dpdk_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
1226 {
1227   vnet_hw_interface_t * hif = vnet_get_hw_interface (vnm, hw_if_index);
1228   uword is_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
1229   dpdk_main_t * dm = &dpdk_main;
1230   dpdk_device_t * xd = vec_elt_at_index (dm->devices, hif->dev_instance);
1231   int rv = 0;
1232
1233 #ifdef RTE_LIBRTE_KNI
1234   if (xd->dev_type == VNET_DPDK_DEV_KNI)
1235   {
1236       if (is_up)
1237       {
1238           struct rte_kni_conf conf;
1239           struct rte_kni_ops ops;
1240           vlib_main_t * vm = vlib_get_main();
1241           vlib_buffer_main_t * bm = vm->buffer_main;
1242           memset(&conf, 0, sizeof(conf));
1243           snprintf(conf.name, RTE_KNI_NAMESIZE, "vpp%u", xd->kni_port_id);
1244           conf.mbuf_size = MBUF_SIZE;
1245           memset(&ops, 0, sizeof(ops));
1246           ops.port_id = xd->kni_port_id;
1247           ops.change_mtu = kni_change_mtu;
1248           ops.config_network_if = kni_config_network_if;
1249
1250           xd->kni = rte_kni_alloc(bm->pktmbuf_pools[rte_socket_id()], &conf, &ops);
1251           if (!xd->kni)
1252           {
1253             clib_warning("failed to allocate kni interface");
1254           }
1255           else
1256           {
1257             hif->max_packet_bytes = 1500; /* kni interface default value */
1258             xd->admin_up = 1;
1259           }
1260       }
1261       else
1262       {
1263         xd->admin_up = 0;
1264         rte_kni_release(xd->kni);
1265       }
1266       return 0;
1267   }
1268 #endif
1269   if (xd->dev_type == VNET_DPDK_DEV_VHOST_USER)
1270     {
1271       if (is_up)
1272         {
1273           if (xd->vu_is_running)
1274             vnet_hw_interface_set_flags (vnm, xd->vlib_hw_if_index,
1275                                  VNET_HW_INTERFACE_FLAG_LINK_UP |
1276                                  ETH_LINK_FULL_DUPLEX );
1277           xd->admin_up = 1;
1278         }
1279       else
1280         {
1281           vnet_hw_interface_set_flags (vnm, xd->vlib_hw_if_index, 0);
1282                               xd->admin_up = 0;
1283         }
1284
1285       return 0;
1286     }
1287
1288
1289   if (is_up)
1290     {
1291       f64 now = vlib_time_now (dm->vlib_main);
1292
1293       /*
1294        * DAW-FIXME: VMXNET3 device stop/start doesn't work, 
1295        * therefore fake the stop in the dpdk driver by
1296        * silently dropping all of the incoming pkts instead of 
1297        * stopping the driver / hardware.
1298        */
1299       if (xd->admin_up == 0)
1300         rv = rte_eth_dev_start (xd->device_index);
1301
1302       if (xd->promisc)
1303           rte_eth_promiscuous_enable(xd->device_index);
1304       else
1305           rte_eth_promiscuous_disable(xd->device_index);
1306
1307       rte_eth_allmulticast_enable (xd->device_index);
1308       xd->admin_up = 1;
1309       dpdk_update_counters (xd, now);
1310       dpdk_update_link_state (xd, now);
1311     }
1312   else
1313     {
1314       rte_eth_allmulticast_disable (xd->device_index);
1315       vnet_hw_interface_set_flags (vnm, xd->vlib_hw_if_index, 0);
1316
1317       /*
1318        * DAW-FIXME: VMXNET3 device stop/start doesn't work, 
1319        * therefore fake the stop in the dpdk driver by
1320        * silently dropping all of the incoming pkts instead of 
1321        * stopping the driver / hardware.
1322        */
1323       if (xd->pmd != VNET_DPDK_PMD_VMXNET3)
1324         {
1325           rte_eth_dev_stop (xd->device_index);
1326           xd->admin_up = 0;
1327         }
1328       else
1329           xd->admin_up = ~0;
1330     }
1331
1332   if (rv < 0)
1333     clib_warning ("rte_eth_dev_%s error: %d", is_up ? "start" : "stop",
1334                   rv);
1335
1336   return /* no error */ 0;
1337 }
1338
1339 /*
1340  * Dynamically redirect all pkts from a specific interface
1341  * to the specified node
1342  */
1343 static void dpdk_set_interface_next_node (vnet_main_t *vnm, u32 hw_if_index,
1344                                           u32 node_index)
1345 {
1346   dpdk_main_t * xm = &dpdk_main;
1347   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
1348   dpdk_device_t * xd = vec_elt_at_index (xm->devices, hw->dev_instance);
1349   
1350   /* Shut off redirection */
1351   if (node_index == ~0)
1352     {
1353       xd->per_interface_next_index = node_index;
1354       return;
1355     }
1356   
1357   xd->per_interface_next_index = 
1358     vlib_node_add_next (xm->vlib_main, dpdk_input_node.index, node_index);
1359 }
1360
1361
1362 static clib_error_t *
1363 dpdk_subif_add_del_function (vnet_main_t * vnm,
1364                              u32 hw_if_index,
1365                              struct vnet_sw_interface_t * st,
1366                              int is_add)
1367 {
1368   dpdk_main_t * xm = &dpdk_main;
1369   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
1370   dpdk_device_t * xd = vec_elt_at_index (xm->devices, hw->dev_instance);
1371   vnet_sw_interface_t * t = (vnet_sw_interface_t *) st;
1372   int r, vlan_offload;
1373
1374
1375   if (xd->dev_type != VNET_DPDK_DEV_ETH)
1376         return 0;
1377   /* currently we program VLANS only for IXGBE VF */
1378   if (xd->pmd != VNET_DPDK_PMD_IXGBEVF)
1379         return 0;
1380
1381   if (t->sub.eth.flags.no_tags == 1)
1382         return 0;
1383
1384   if ((t->sub.eth.flags.one_tag != 1) || (t->sub.eth.flags.exact_match != 1 ))
1385         return clib_error_return (0, "unsupported VLAN setup");
1386
1387
1388   vlan_offload = rte_eth_dev_get_vlan_offload(xd->device_index);
1389   vlan_offload |= ETH_VLAN_FILTER_OFFLOAD;
1390
1391   if ((r = rte_eth_dev_set_vlan_offload(xd->device_index, vlan_offload)))
1392         return clib_error_return (0, "rte_eth_dev_set_vlan_offload[%d]: err %d",
1393                                   xd->device_index, r);
1394
1395
1396   if ((r = rte_eth_dev_vlan_filter(xd->device_index, t->sub.eth.outer_vlan_id, is_add)))
1397         return clib_error_return (0, "rte_eth_dev_vlan_filter[%d]: err %d",
1398                                  xd->device_index, r);
1399
1400   return 0;
1401 }
1402
1403 VNET_DEVICE_CLASS (dpdk_device_class) = {
1404   .name = "dpdk",
1405   .tx_function = dpdk_interface_tx,
1406   .tx_function_n_errors = DPDK_TX_FUNC_N_ERROR,
1407   .tx_function_error_strings = dpdk_tx_func_error_strings,
1408   .format_device_name = format_dpdk_device_name,
1409   .format_device = format_dpdk_device,
1410   .format_tx_trace = format_dpdk_tx_dma_trace,
1411   .clear_counters = dpdk_clear_hw_interface_counters,
1412   .admin_up_down_function = dpdk_interface_admin_up_down,
1413   .subif_add_del_function = dpdk_subif_add_del_function,
1414   .rx_redirect_to_node = dpdk_set_interface_next_node,
1415   .no_flatten_output_chains = 1,
1416   .name_renumber = dpdk_device_renumber,
1417 };
1418
1419 void dpdk_set_flowcontrol_callback (vlib_main_t *vm, 
1420                                     dpdk_flowcontrol_callback_t callback)
1421 {
1422   dpdk_main.flowcontrol_callback = callback;
1423 }
1424
1425 #define UP_DOWN_FLAG_EVENT 1
1426
1427
1428 u32 dpdk_get_admin_up_down_in_progress (void)
1429 {
1430   return dpdk_main.admin_up_down_in_progress;
1431 }
1432
1433 static uword
1434 admin_up_down_process (vlib_main_t * vm,
1435                        vlib_node_runtime_t * rt,
1436                        vlib_frame_t * f)
1437 {
1438   clib_error_t * error = 0;
1439   uword event_type;
1440   uword *event_data = 0;
1441   u32 index;
1442   u32 sw_if_index;
1443   u32 flags;
1444
1445   while (1)  
1446     { 
1447       vlib_process_wait_for_event (vm);
1448
1449       event_type = vlib_process_get_events (vm, &event_data);
1450
1451       dpdk_main.admin_up_down_in_progress = 1;
1452
1453       for (index=0; index<vec_len(event_data); index++)
1454         {
1455           sw_if_index = event_data[index] >> 32;
1456           flags = (u32) event_data[index];
1457
1458           switch (event_type) {
1459           case UP_DOWN_FLAG_EVENT:
1460             error = vnet_sw_interface_set_flags (vnet_get_main(), sw_if_index, flags);
1461             clib_error_report(error);
1462             break;
1463           }
1464         }
1465
1466       vec_reset_length (event_data);
1467
1468       dpdk_main.admin_up_down_in_progress = 0;
1469
1470     }
1471   return 0; /* or not */
1472 }
1473
1474 VLIB_REGISTER_NODE (admin_up_down_process_node,static) = {
1475     .function = admin_up_down_process,
1476     .type = VLIB_NODE_TYPE_PROCESS,
1477     .name = "admin-up-down-process",
1478     .process_log2_n_stack_bytes = 17,  // 256KB
1479 };
1480
1481 /*
1482  * Asynchronously invoke vnet_sw_interface_set_flags via the admin_up_down 
1483  * process. Useful for avoiding long blocking delays (>150ms) in the dpdk 
1484  * drivers.
1485  * WARNING: when posting this event, no other interface-related calls should
1486  * be made (e.g. vnet_create_sw_interface()) while the event is being
1487  * processed (admin_up_down_in_progress). This is required in order to avoid 
1488  * race conditions in manipulating interface data structures.
1489  */
1490 void post_sw_interface_set_flags (vlib_main_t *vm, u32 sw_if_index, u32 flags)
1491 {
1492   vlib_process_signal_event
1493       (vm, admin_up_down_process_node.index,
1494        UP_DOWN_FLAG_EVENT, 
1495        (((uword)sw_if_index << 32) | flags));
1496 }
1497
1498 /*
1499  * Called by the dpdk driver's rte_delay_us() function. 
1500  * Return 0 to have the dpdk do a regular delay loop.
1501  * Return 1 if to skip the delay loop because we are suspending
1502  * the calling vlib process instead.
1503  */
1504 int rte_delay_us_override (unsigned us) {
1505   vlib_main_t * vm;
1506
1507   /* Don't bother intercepting for short delays */
1508   if (us < 10) return 0;
1509
1510   /* 
1511    * Only intercept if we are in a vlib process. 
1512    * If we are called from a vlib worker thread or the vlib main
1513    * thread then do not intercept. (Must not be called from an 
1514    * independent pthread).
1515    */
1516   if (os_get_cpu_number() == 0)
1517     {
1518       /* 
1519        * We're in the vlib main thread or a vlib process. Make sure
1520        * the process is running and we're not still initializing.
1521        */
1522       vm = vlib_get_main();
1523       if (vlib_in_process_context(vm))
1524         {
1525           /* Only suspend for the admin_down_process */
1526           vlib_process_t * proc = vlib_get_current_process(vm);
1527           if (!(proc->flags & VLIB_PROCESS_IS_RUNNING) ||
1528               (proc->node_runtime.function != admin_up_down_process))
1529                 return 0;
1530
1531           f64 delay = 1e-6 * us;
1532           vlib_process_suspend(vm, delay);
1533           return 1;
1534         }
1535     }
1536   return 0; // no override
1537 }