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