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