9f40c277b20755e39857c214c58d5593ee1cc784
[vpp.git] / vnet / vnet / devices / dpdk / device.c
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include <vnet/vnet.h>
16 #include <vppinfra/vec.h>
17 #include <vppinfra/format.h>
18 #include <vlib/unix/cj.h>
19 #include <assert.h>
20
21 #include <vnet/ethernet/ethernet.h>
22 #include <vnet/devices/dpdk/dpdk.h>
23
24 #include "dpdk_priv.h"
25 #include <vppinfra/error.h>
26
27 #define foreach_dpdk_tx_func_error                      \
28   _(BAD_RETVAL, "DPDK tx function returned an error")   \
29   _(RING_FULL, "Tx packet drops (ring full)")           \
30   _(PKT_DROP, "Tx packet drops (dpdk tx failure)")      \
31   _(REPL_FAIL, "Tx packet drops (replication failure)")
32
33 typedef enum
34 {
35 #define _(f,s) DPDK_TX_FUNC_ERROR_##f,
36   foreach_dpdk_tx_func_error
37 #undef _
38     DPDK_TX_FUNC_N_ERROR,
39 } dpdk_tx_func_error_t;
40
41 static char *dpdk_tx_func_error_strings[] = {
42 #define _(n,s) s,
43   foreach_dpdk_tx_func_error
44 #undef _
45 };
46
47 clib_error_t *
48 dpdk_set_mac_address (vnet_hw_interface_t * hi, char *address)
49 {
50   int error;
51   dpdk_main_t *dm = &dpdk_main;
52   dpdk_device_t *xd = vec_elt_at_index (dm->devices, hi->dev_instance);
53
54   error = rte_eth_dev_default_mac_addr_set (xd->device_index,
55                                             (struct ether_addr *) address);
56
57   if (error)
58     {
59       return clib_error_return (0, "mac address set failed: %d", error);
60     }
61   else
62     {
63       return NULL;
64     }
65 }
66
67 clib_error_t *
68 dpdk_set_mc_filter (vnet_hw_interface_t * hi,
69                     struct ether_addr mc_addr_vec[], int naddr)
70 {
71   int error;
72   dpdk_main_t *dm = &dpdk_main;
73   dpdk_device_t *xd = vec_elt_at_index (dm->devices, hi->dev_instance);
74
75   error = rte_eth_dev_set_mc_addr_list (xd->device_index, mc_addr_vec, naddr);
76
77   if (error)
78     {
79       return clib_error_return (0, "mc addr list failed: %d", error);
80     }
81   else
82     {
83       return NULL;
84     }
85 }
86
87 struct rte_mbuf *
88 dpdk_replicate_packet_mb (vlib_buffer_t * b)
89 {
90   vlib_main_t *vm = vlib_get_main ();
91   vlib_buffer_main_t *bm = vm->buffer_main;
92   struct rte_mbuf **mbufs = 0, *s, *d;
93   u8 nb_segs;
94   unsigned socket_id = rte_socket_id ();
95   int i;
96
97   ASSERT (bm->pktmbuf_pools[socket_id]);
98   s = rte_mbuf_from_vlib_buffer (b);
99   nb_segs = s->nb_segs;
100   vec_validate (mbufs, nb_segs - 1);
101
102   if (rte_pktmbuf_alloc_bulk (bm->pktmbuf_pools[socket_id], mbufs, nb_segs))
103     {
104       vec_free (mbufs);
105       return 0;
106     }
107
108   d = mbufs[0];
109   d->nb_segs = s->nb_segs;
110   d->data_len = s->data_len;
111   d->pkt_len = s->pkt_len;
112   d->data_off = s->data_off;
113   clib_memcpy (d->buf_addr, s->buf_addr, RTE_PKTMBUF_HEADROOM + s->data_len);
114
115   for (i = 1; i < nb_segs; i++)
116     {
117       d->next = mbufs[i];
118       d = mbufs[i];
119       s = s->next;
120       d->data_len = s->data_len;
121       clib_memcpy (d->buf_addr, s->buf_addr,
122                    RTE_PKTMBUF_HEADROOM + s->data_len);
123     }
124
125   d = mbufs[0];
126   vec_free (mbufs);
127   return d;
128 }
129
130 static void
131 dpdk_tx_trace_buffer (dpdk_main_t * dm,
132                       vlib_node_runtime_t * node,
133                       dpdk_device_t * xd,
134                       u16 queue_id, u32 buffer_index, vlib_buffer_t * buffer)
135 {
136   vlib_main_t *vm = vlib_get_main ();
137   dpdk_tx_dma_trace_t *t0;
138   struct rte_mbuf *mb;
139
140   mb = rte_mbuf_from_vlib_buffer (buffer);
141
142   t0 = vlib_add_trace (vm, node, buffer, sizeof (t0[0]));
143   t0->queue_index = queue_id;
144   t0->device_index = xd->device_index;
145   t0->buffer_index = buffer_index;
146   clib_memcpy (&t0->mb, mb, sizeof (t0->mb));
147   clib_memcpy (&t0->buffer, buffer,
148                sizeof (buffer[0]) - sizeof (buffer->pre_data));
149   clib_memcpy (t0->buffer.pre_data, buffer->data + buffer->current_data,
150                sizeof (t0->buffer.pre_data));
151 }
152
153 static_always_inline void
154 dpdk_validate_rte_mbuf (vlib_main_t * vm, vlib_buffer_t * b,
155                         int maybe_multiseg)
156 {
157   struct rte_mbuf *mb, *first_mb, *last_mb;
158
159   /* buffer is coming from non-dpdk source so we need to init
160      rte_mbuf header */
161   if (PREDICT_FALSE ((b->flags & VNET_BUFFER_RTE_MBUF_VALID) == 0))
162     {
163       last_mb = mb = rte_mbuf_from_vlib_buffer (b);
164       rte_pktmbuf_reset (mb);
165       while (maybe_multiseg && (b->flags & VLIB_BUFFER_NEXT_PRESENT))
166         {
167           b = vlib_get_buffer (vm, b->next_buffer);
168           mb = rte_mbuf_from_vlib_buffer (b);
169           last_mb->next = mb;
170           last_mb = mb;
171           rte_pktmbuf_reset (mb);
172         }
173     }
174
175   first_mb = mb = rte_mbuf_from_vlib_buffer (b);
176   first_mb->nb_segs = 1;
177   mb->data_len = b->current_length;
178   mb->pkt_len = maybe_multiseg ? vlib_buffer_length_in_chain (vm, b) :
179     b->current_length;
180   mb->data_off = VLIB_BUFFER_PRE_DATA_SIZE + b->current_data;
181
182   while (maybe_multiseg && (b->flags & VLIB_BUFFER_NEXT_PRESENT))
183     {
184       b = vlib_get_buffer (vm, b->next_buffer);
185       mb = rte_mbuf_from_vlib_buffer (b);
186       mb->data_len = b->current_length;
187       mb->pkt_len = b->current_length;
188       mb->data_off = VLIB_BUFFER_PRE_DATA_SIZE + b->current_data;
189       first_mb->nb_segs++;
190     }
191 }
192
193 /*
194  * This function calls the dpdk's tx_burst function to transmit the packets
195  * on the tx_vector. It manages a lock per-device if the device does not
196  * support multiple queues. It returns the number of packets untransmitted
197  * on the tx_vector. If all packets are transmitted (the normal case), the
198  * function returns 0.
199  *
200  * The function assumes there is at least one packet on the tx_vector.
201  */
202 static_always_inline
203   u32 tx_burst_vector_internal (vlib_main_t * vm,
204                                 dpdk_device_t * xd,
205                                 struct rte_mbuf **tx_vector)
206 {
207   dpdk_main_t *dm = &dpdk_main;
208   u32 n_packets;
209   u32 tx_head;
210   u32 tx_tail;
211   u32 n_retry;
212   int rv;
213   int queue_id;
214   tx_ring_hdr_t *ring;
215
216   ring = vec_header (tx_vector, sizeof (*ring));
217
218   n_packets = ring->tx_head - ring->tx_tail;
219
220   tx_head = ring->tx_head % xd->nb_tx_desc;
221
222   /*
223    * Ensure rte_eth_tx_burst is not called with 0 packets, which can lead to
224    * unpredictable results.
225    */
226   ASSERT (n_packets > 0);
227
228   /*
229    * Check for tx_vector overflow. If this fails it is a system configuration
230    * error. The ring should be sized big enough to handle the largest un-flowed
231    * off burst from a traffic manager. A larger size also helps performance
232    * a bit because it decreases the probability of having to issue two tx_burst
233    * calls due to a ring wrap.
234    */
235   ASSERT (n_packets < xd->nb_tx_desc);
236   ASSERT (ring->tx_tail == 0);
237
238   n_retry = 16;
239   queue_id = vm->cpu_index;
240
241   do
242     {
243       /* start the burst at the tail */
244       tx_tail = ring->tx_tail % xd->nb_tx_desc;
245
246       /*
247        * This device only supports one TX queue,
248        * and we're running multi-threaded...
249        */
250       if (PREDICT_FALSE (xd->lockp != 0))
251         {
252           queue_id = queue_id % xd->tx_q_used;
253           while (__sync_lock_test_and_set (xd->lockp[queue_id], 1))
254             /* zzzz */
255             queue_id = (queue_id + 1) % xd->tx_q_used;
256         }
257
258       if (PREDICT_FALSE (xd->flags & DPDK_DEVICE_FLAG_HQOS))    /* HQoS ON */
259         {
260           /* no wrap, transmit in one burst */
261           dpdk_device_hqos_per_worker_thread_t *hqos =
262             &xd->hqos_wt[vm->cpu_index];
263
264           dpdk_hqos_metadata_set (hqos,
265                                   &tx_vector[tx_tail], tx_head - tx_tail);
266           rv = rte_ring_sp_enqueue_burst (hqos->swq,
267                                           (void **) &tx_vector[tx_tail],
268                                           (uint16_t) (tx_head - tx_tail));
269         }
270       else if (PREDICT_TRUE (xd->flags & DPDK_DEVICE_FLAG_PMD))
271         {
272           /* no wrap, transmit in one burst */
273           rv = rte_eth_tx_burst (xd->device_index,
274                                  (uint16_t) queue_id,
275                                  &tx_vector[tx_tail],
276                                  (uint16_t) (tx_head - tx_tail));
277         }
278       else
279         {
280           ASSERT (0);
281           rv = 0;
282         }
283
284       if (PREDICT_FALSE (xd->lockp != 0))
285         *xd->lockp[queue_id] = 0;
286
287       if (PREDICT_FALSE (rv < 0))
288         {
289           // emit non-fatal message, bump counter
290           vnet_main_t *vnm = dm->vnet_main;
291           vnet_interface_main_t *im = &vnm->interface_main;
292           u32 node_index;
293
294           node_index = vec_elt_at_index (im->hw_interfaces,
295                                          xd->vlib_hw_if_index)->tx_node_index;
296
297           vlib_error_count (vm, node_index, DPDK_TX_FUNC_ERROR_BAD_RETVAL, 1);
298           clib_warning ("rte_eth_tx_burst[%d]: error %d", xd->device_index,
299                         rv);
300           return n_packets;     // untransmitted packets
301         }
302       ring->tx_tail += (u16) rv;
303       n_packets -= (uint16_t) rv;
304     }
305   while (rv && n_packets && (n_retry > 0));
306
307   return n_packets;
308 }
309
310 static_always_inline void
311 dpdk_prefetch_buffer_by_index (vlib_main_t * vm, u32 bi)
312 {
313   vlib_buffer_t *b;
314   struct rte_mbuf *mb;
315   b = vlib_get_buffer (vm, bi);
316   mb = rte_mbuf_from_vlib_buffer (b);
317   CLIB_PREFETCH (mb, CLIB_CACHE_LINE_BYTES, LOAD);
318   CLIB_PREFETCH (b, CLIB_CACHE_LINE_BYTES, LOAD);
319 }
320
321 static_always_inline void
322 dpdk_buffer_recycle (vlib_main_t * vm, vlib_node_runtime_t * node,
323                      vlib_buffer_t * b, u32 bi, struct rte_mbuf **mbp)
324 {
325   dpdk_main_t *dm = &dpdk_main;
326   u32 my_cpu = vm->cpu_index;
327   struct rte_mbuf *mb_new;
328
329   if (PREDICT_FALSE (b->flags & VLIB_BUFFER_RECYCLE) == 0)
330     return;
331
332   mb_new = dpdk_replicate_packet_mb (b);
333   if (PREDICT_FALSE (mb_new == 0))
334     {
335       vlib_error_count (vm, node->node_index,
336                         DPDK_TX_FUNC_ERROR_REPL_FAIL, 1);
337       b->flags |= VLIB_BUFFER_REPL_FAIL;
338     }
339   else
340     *mbp = mb_new;
341
342   vec_add1 (dm->recycle[my_cpu], bi);
343 }
344
345 /*
346  * Transmits the packets on the frame to the interface associated with the
347  * node. It first copies packets on the frame to a tx_vector containing the
348  * rte_mbuf pointers. It then passes this vector to tx_burst_vector_internal
349  * which calls the dpdk tx_burst function.
350  */
351 static uword
352 dpdk_interface_tx (vlib_main_t * vm,
353                    vlib_node_runtime_t * node, vlib_frame_t * f)
354 {
355   dpdk_main_t *dm = &dpdk_main;
356   vnet_interface_output_runtime_t *rd = (void *) node->runtime_data;
357   dpdk_device_t *xd = vec_elt_at_index (dm->devices, rd->dev_instance);
358   u32 n_packets = f->n_vectors;
359   u32 n_left;
360   u32 *from;
361   struct rte_mbuf **tx_vector;
362   u16 i;
363   u16 nb_tx_desc = xd->nb_tx_desc;
364   int queue_id;
365   u32 my_cpu;
366   u32 tx_pkts = 0;
367   tx_ring_hdr_t *ring;
368   u32 n_on_ring;
369
370   my_cpu = vm->cpu_index;
371
372   queue_id = my_cpu;
373
374   tx_vector = xd->tx_vectors[queue_id];
375   ring = vec_header (tx_vector, sizeof (*ring));
376
377   n_on_ring = ring->tx_head - ring->tx_tail;
378   from = vlib_frame_vector_args (f);
379
380   ASSERT (n_packets <= VLIB_FRAME_SIZE);
381
382   if (PREDICT_FALSE (n_on_ring + n_packets > nb_tx_desc))
383     {
384       /*
385        * Overflowing the ring should never happen.
386        * If it does then drop the whole frame.
387        */
388       vlib_error_count (vm, node->node_index, DPDK_TX_FUNC_ERROR_RING_FULL,
389                         n_packets);
390
391       while (n_packets--)
392         {
393           u32 bi0 = from[n_packets];
394           vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
395           struct rte_mbuf *mb0 = rte_mbuf_from_vlib_buffer (b0);
396           rte_pktmbuf_free (mb0);
397         }
398       return n_on_ring;
399     }
400
401   if (PREDICT_FALSE (dm->tx_pcap_enable))
402     {
403       n_left = n_packets;
404       while (n_left > 0)
405         {
406           u32 bi0 = from[0];
407           vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
408           if (dm->pcap_sw_if_index == 0 ||
409               dm->pcap_sw_if_index == vnet_buffer (b0)->sw_if_index[VLIB_TX])
410             pcap_add_buffer (&dm->pcap_main, vm, bi0, 512);
411           from++;
412           n_left--;
413         }
414     }
415
416   from = vlib_frame_vector_args (f);
417   n_left = n_packets;
418   i = ring->tx_head % nb_tx_desc;
419
420   while (n_left >= 8)
421     {
422       u32 bi0, bi1, bi2, bi3;
423       struct rte_mbuf *mb0, *mb1, *mb2, *mb3;
424       vlib_buffer_t *b0, *b1, *b2, *b3;
425       u32 or_flags;
426
427       dpdk_prefetch_buffer_by_index (vm, from[4]);
428       dpdk_prefetch_buffer_by_index (vm, from[5]);
429       dpdk_prefetch_buffer_by_index (vm, from[6]);
430       dpdk_prefetch_buffer_by_index (vm, from[7]);
431
432       bi0 = from[0];
433       bi1 = from[1];
434       bi2 = from[2];
435       bi3 = from[3];
436       from += 4;
437
438       b0 = vlib_get_buffer (vm, bi0);
439       b1 = vlib_get_buffer (vm, bi1);
440       b2 = vlib_get_buffer (vm, bi2);
441       b3 = vlib_get_buffer (vm, bi3);
442
443       or_flags = b0->flags | b1->flags | b2->flags | b3->flags;
444
445       if (or_flags & VLIB_BUFFER_NEXT_PRESENT)
446         {
447           dpdk_validate_rte_mbuf (vm, b0, 1);
448           dpdk_validate_rte_mbuf (vm, b1, 1);
449           dpdk_validate_rte_mbuf (vm, b2, 1);
450           dpdk_validate_rte_mbuf (vm, b3, 1);
451         }
452       else
453         {
454           dpdk_validate_rte_mbuf (vm, b0, 0);
455           dpdk_validate_rte_mbuf (vm, b1, 0);
456           dpdk_validate_rte_mbuf (vm, b2, 0);
457           dpdk_validate_rte_mbuf (vm, b3, 0);
458         }
459
460       mb0 = rte_mbuf_from_vlib_buffer (b0);
461       mb1 = rte_mbuf_from_vlib_buffer (b1);
462       mb2 = rte_mbuf_from_vlib_buffer (b2);
463       mb3 = rte_mbuf_from_vlib_buffer (b3);
464
465       if (PREDICT_FALSE (or_flags & VLIB_BUFFER_RECYCLE))
466         {
467           dpdk_buffer_recycle (vm, node, b0, bi0, &mb0);
468           dpdk_buffer_recycle (vm, node, b1, bi1, &mb1);
469           dpdk_buffer_recycle (vm, node, b2, bi2, &mb2);
470           dpdk_buffer_recycle (vm, node, b3, bi3, &mb3);
471
472           /* dont enqueue packets if replication failed as they must
473              be sent back to recycle */
474           if (PREDICT_TRUE ((b0->flags & VLIB_BUFFER_REPL_FAIL) == 0))
475             tx_vector[i++ % nb_tx_desc] = mb0;
476           if (PREDICT_TRUE ((b1->flags & VLIB_BUFFER_REPL_FAIL) == 0))
477             tx_vector[i++ % nb_tx_desc] = mb1;
478           if (PREDICT_TRUE ((b2->flags & VLIB_BUFFER_REPL_FAIL) == 0))
479             tx_vector[i++ % nb_tx_desc] = mb2;
480           if (PREDICT_TRUE ((b3->flags & VLIB_BUFFER_REPL_FAIL) == 0))
481             tx_vector[i++ % nb_tx_desc] = mb3;
482         }
483       else
484         {
485           if (PREDICT_FALSE (i + 3 >= nb_tx_desc))
486             {
487               tx_vector[i++ % nb_tx_desc] = mb0;
488               tx_vector[i++ % nb_tx_desc] = mb1;
489               tx_vector[i++ % nb_tx_desc] = mb2;
490               tx_vector[i++ % nb_tx_desc] = mb3;
491               i %= nb_tx_desc;
492             }
493           else
494             {
495               tx_vector[i++] = mb0;
496               tx_vector[i++] = mb1;
497               tx_vector[i++] = mb2;
498               tx_vector[i++] = mb3;
499             }
500         }
501
502
503       if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
504         {
505           if (b0->flags & VLIB_BUFFER_IS_TRACED)
506             dpdk_tx_trace_buffer (dm, node, xd, queue_id, bi0, b0);
507           if (b1->flags & VLIB_BUFFER_IS_TRACED)
508             dpdk_tx_trace_buffer (dm, node, xd, queue_id, bi1, b1);
509           if (b2->flags & VLIB_BUFFER_IS_TRACED)
510             dpdk_tx_trace_buffer (dm, node, xd, queue_id, bi2, b2);
511           if (b3->flags & VLIB_BUFFER_IS_TRACED)
512             dpdk_tx_trace_buffer (dm, node, xd, queue_id, bi3, b3);
513         }
514
515       n_left -= 4;
516     }
517   while (n_left > 0)
518     {
519       u32 bi0;
520       struct rte_mbuf *mb0;
521       vlib_buffer_t *b0;
522
523       bi0 = from[0];
524       from++;
525
526       b0 = vlib_get_buffer (vm, bi0);
527
528       dpdk_validate_rte_mbuf (vm, b0, 1);
529
530       mb0 = rte_mbuf_from_vlib_buffer (b0);
531       dpdk_buffer_recycle (vm, node, b0, bi0, &mb0);
532
533       if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
534         if (b0->flags & VLIB_BUFFER_IS_TRACED)
535           dpdk_tx_trace_buffer (dm, node, xd, queue_id, bi0, b0);
536
537       if (PREDICT_TRUE ((b0->flags & VLIB_BUFFER_REPL_FAIL) == 0))
538         {
539           tx_vector[i % nb_tx_desc] = mb0;
540           i++;
541         }
542       n_left--;
543     }
544
545   /* account for additional packets in the ring */
546   ring->tx_head += n_packets;
547   n_on_ring = ring->tx_head - ring->tx_tail;
548
549   /* transmit as many packets as possible */
550   n_packets = tx_burst_vector_internal (vm, xd, tx_vector);
551
552   /*
553    * tx_pkts is the number of packets successfully transmitted
554    * This is the number originally on ring minus the number remaining on ring
555    */
556   tx_pkts = n_on_ring - n_packets;
557
558   {
559     /* If there is no callback then drop any non-transmitted packets */
560     if (PREDICT_FALSE (n_packets))
561       {
562         vlib_simple_counter_main_t *cm;
563         vnet_main_t *vnm = vnet_get_main ();
564
565         cm = vec_elt_at_index (vnm->interface_main.sw_if_counters,
566                                VNET_INTERFACE_COUNTER_TX_ERROR);
567
568         vlib_increment_simple_counter (cm, my_cpu, xd->vlib_sw_if_index,
569                                        n_packets);
570
571         vlib_error_count (vm, node->node_index, DPDK_TX_FUNC_ERROR_PKT_DROP,
572                           n_packets);
573
574         while (n_packets--)
575           rte_pktmbuf_free (tx_vector[ring->tx_tail + n_packets]);
576       }
577
578     /* Reset head/tail to avoid unnecessary wrap */
579     ring->tx_head = 0;
580     ring->tx_tail = 0;
581   }
582
583   /* Recycle replicated buffers */
584   if (PREDICT_FALSE (vec_len (dm->recycle[my_cpu])))
585     {
586       vlib_buffer_free (vm, dm->recycle[my_cpu],
587                         vec_len (dm->recycle[my_cpu]));
588       _vec_len (dm->recycle[my_cpu]) = 0;
589     }
590
591   ASSERT (ring->tx_head >= ring->tx_tail);
592
593   return tx_pkts;
594 }
595
596 static void
597 dpdk_clear_hw_interface_counters (u32 instance)
598 {
599   dpdk_main_t *dm = &dpdk_main;
600   dpdk_device_t *xd = vec_elt_at_index (dm->devices, instance);
601
602   /*
603    * Set the "last_cleared_stats" to the current stats, so that
604    * things appear to clear from a display perspective.
605    */
606   dpdk_update_counters (xd, vlib_time_now (dm->vlib_main));
607
608   clib_memcpy (&xd->last_cleared_stats, &xd->stats, sizeof (xd->stats));
609   clib_memcpy (xd->last_cleared_xstats, xd->xstats,
610                vec_len (xd->last_cleared_xstats) *
611                sizeof (xd->last_cleared_xstats[0]));
612
613 }
614
615 static clib_error_t *
616 dpdk_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
617 {
618   vnet_hw_interface_t *hif = vnet_get_hw_interface (vnm, hw_if_index);
619   uword is_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
620   dpdk_main_t *dm = &dpdk_main;
621   dpdk_device_t *xd = vec_elt_at_index (dm->devices, hif->dev_instance);
622   int rv = 0;
623
624   if (is_up)
625     {
626       f64 now = vlib_time_now (dm->vlib_main);
627
628       if ((xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP) == 0)
629         rv = rte_eth_dev_start (xd->device_index);
630
631       if (xd->flags & DPDK_DEVICE_FLAG_PROMISC)
632         rte_eth_promiscuous_enable (xd->device_index);
633       else
634         rte_eth_promiscuous_disable (xd->device_index);
635
636       rte_eth_allmulticast_enable (xd->device_index);
637       xd->flags |= DPDK_DEVICE_FLAG_ADMIN_UP;
638       dpdk_update_counters (xd, now);
639       dpdk_update_link_state (xd, now);
640     }
641   else
642     {
643       xd->flags &= ~DPDK_DEVICE_FLAG_ADMIN_UP;
644
645       rte_eth_allmulticast_disable (xd->device_index);
646       vnet_hw_interface_set_flags (vnm, xd->vlib_hw_if_index, 0);
647       rte_eth_dev_stop (xd->device_index);
648
649       /* For bonded interface, stop slave links */
650       if (xd->pmd == VNET_DPDK_PMD_BOND)
651         {
652           u8 slink[16];
653           int nlink = rte_eth_bond_slaves_get (xd->device_index, slink, 16);
654           while (nlink >= 1)
655             {
656               u8 dpdk_port = slink[--nlink];
657               rte_eth_dev_stop (dpdk_port);
658             }
659         }
660     }
661
662   if (rv < 0)
663     clib_warning ("rte_eth_dev_%s error: %d", is_up ? "start" : "stop", rv);
664
665   return /* no error */ 0;
666 }
667
668 /*
669  * Dynamically redirect all pkts from a specific interface
670  * to the specified node
671  */
672 static void
673 dpdk_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index,
674                               u32 node_index)
675 {
676   dpdk_main_t *xm = &dpdk_main;
677   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
678   dpdk_device_t *xd = vec_elt_at_index (xm->devices, hw->dev_instance);
679
680   /* Shut off redirection */
681   if (node_index == ~0)
682     {
683       xd->per_interface_next_index = node_index;
684       return;
685     }
686
687   xd->per_interface_next_index =
688     vlib_node_add_next (xm->vlib_main, dpdk_input_node.index, node_index);
689 }
690
691
692 static clib_error_t *
693 dpdk_subif_add_del_function (vnet_main_t * vnm,
694                              u32 hw_if_index,
695                              struct vnet_sw_interface_t *st, int is_add)
696 {
697   dpdk_main_t *xm = &dpdk_main;
698   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
699   dpdk_device_t *xd = vec_elt_at_index (xm->devices, hw->dev_instance);
700   vnet_sw_interface_t *t = (vnet_sw_interface_t *) st;
701   int r, vlan_offload;
702   u32 prev_subifs = xd->num_subifs;
703   clib_error_t *err = 0;
704
705   if (is_add)
706     xd->num_subifs++;
707   else if (xd->num_subifs)
708     xd->num_subifs--;
709
710   if ((xd->flags & DPDK_DEVICE_FLAG_PMD) == 0)
711     goto done;
712
713   /* currently we program VLANS only for IXGBE VF and I40E VF */
714   if ((xd->pmd != VNET_DPDK_PMD_IXGBEVF) && (xd->pmd != VNET_DPDK_PMD_I40EVF))
715     goto done;
716
717   if (t->sub.eth.flags.no_tags == 1)
718     goto done;
719
720   if ((t->sub.eth.flags.one_tag != 1) || (t->sub.eth.flags.exact_match != 1))
721     {
722       xd->num_subifs = prev_subifs;
723       err = clib_error_return (0, "unsupported VLAN setup");
724       goto done;
725     }
726
727   vlan_offload = rte_eth_dev_get_vlan_offload (xd->device_index);
728   vlan_offload |= ETH_VLAN_FILTER_OFFLOAD;
729
730   if ((r = rte_eth_dev_set_vlan_offload (xd->device_index, vlan_offload)))
731     {
732       xd->num_subifs = prev_subifs;
733       err = clib_error_return (0, "rte_eth_dev_set_vlan_offload[%d]: err %d",
734                                xd->device_index, r);
735       goto done;
736     }
737
738
739   if ((r =
740        rte_eth_dev_vlan_filter (xd->device_index, t->sub.eth.outer_vlan_id,
741                                 is_add)))
742     {
743       xd->num_subifs = prev_subifs;
744       err = clib_error_return (0, "rte_eth_dev_vlan_filter[%d]: err %d",
745                                xd->device_index, r);
746       goto done;
747     }
748
749 done:
750   if (xd->num_subifs)
751     xd->flags |= DPDK_DEVICE_FLAG_HAVE_SUBIF;
752   else
753     xd->flags &= ~DPDK_DEVICE_FLAG_HAVE_SUBIF;
754
755   return err;
756 }
757
758 /* *INDENT-OFF* */
759 VNET_DEVICE_CLASS (dpdk_device_class) = {
760   .name = "dpdk",
761   .tx_function = dpdk_interface_tx,
762   .tx_function_n_errors = DPDK_TX_FUNC_N_ERROR,
763   .tx_function_error_strings = dpdk_tx_func_error_strings,
764   .format_device_name = format_dpdk_device_name,
765   .format_device = format_dpdk_device,
766   .format_tx_trace = format_dpdk_tx_dma_trace,
767   .clear_counters = dpdk_clear_hw_interface_counters,
768   .admin_up_down_function = dpdk_interface_admin_up_down,
769   .subif_add_del_function = dpdk_subif_add_del_function,
770   .rx_redirect_to_node = dpdk_set_interface_next_node,
771   .no_flatten_output_chains = 1,
772   .mac_addr_change_function = dpdk_set_mac_address,
773 };
774
775 VLIB_DEVICE_TX_FUNCTION_MULTIARCH (dpdk_device_class, dpdk_interface_tx)
776 /* *INDENT-ON* */
777
778 #define UP_DOWN_FLAG_EVENT 1
779
780 uword
781 admin_up_down_process (vlib_main_t * vm,
782                        vlib_node_runtime_t * rt, vlib_frame_t * f)
783 {
784   clib_error_t *error = 0;
785   uword event_type;
786   uword *event_data = 0;
787   u32 sw_if_index;
788   u32 flags;
789
790   while (1)
791     {
792       vlib_process_wait_for_event (vm);
793
794       event_type = vlib_process_get_events (vm, &event_data);
795
796       dpdk_main.admin_up_down_in_progress = 1;
797
798       switch (event_type)
799         {
800         case UP_DOWN_FLAG_EVENT:
801           {
802             if (vec_len (event_data) == 2)
803               {
804                 sw_if_index = event_data[0];
805                 flags = event_data[1];
806                 error =
807                   vnet_sw_interface_set_flags (vnet_get_main (), sw_if_index,
808                                                flags);
809                 clib_error_report (error);
810               }
811           }
812           break;
813         }
814
815       vec_reset_length (event_data);
816
817       dpdk_main.admin_up_down_in_progress = 0;
818
819     }
820   return 0;                     /* or not */
821 }
822
823 /* *INDENT-OFF* */
824 VLIB_REGISTER_NODE (admin_up_down_process_node,static) = {
825     .function = admin_up_down_process,
826     .type = VLIB_NODE_TYPE_PROCESS,
827     .name = "admin-up-down-process",
828     .process_log2_n_stack_bytes = 17,  // 256KB
829 };
830 /* *INDENT-ON* */
831
832 /*
833  * fd.io coding-style-patch-verification: ON
834  *
835  * Local Variables:
836  * eval: (c-set-style "gnu")
837  * End:
838  */