dpdk: remove support for dpdk 16.04
[vpp.git] / vnet / vnet / devices / dpdk / node.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/error.h>
18 #include <vppinfra/format.h>
19 #include <vppinfra/xxhash.h>
20
21 #include <vnet/ethernet/ethernet.h>
22 #include <vnet/devices/dpdk/dpdk.h>
23 #include <vnet/classify/vnet_classify.h>
24 #include <vnet/mpls/packet.h>
25 #include <vnet/handoff.h>
26
27 #include "dpdk_priv.h"
28
29 #ifndef MAX
30 #define MAX(a,b) ((a) < (b) ? (b) : (a))
31 #endif
32
33 #ifndef MIN
34 #define MIN(a,b) ((a) < (b) ? (a) : (b))
35 #endif
36
37 /*
38  * At least in certain versions of ESXi, vmware e1000's don't honor the
39  * "strip rx CRC" bit. Set this flag to work around that bug FOR UNIT TEST ONLY.
40  *
41  * If wireshark complains like so:
42  *
43  * "Frame check sequence: 0x00000000 [incorrect, should be <hex-num>]"
44  * and you're using ESXi emulated e1000's, set this flag FOR UNIT TEST ONLY.
45  *
46  * Note: do NOT check in this file with this workaround enabled! You'll lose
47  * actual data from e.g. 10xGE interfaces. The extra 4 bytes annoy
48  * wireshark, but they're harmless...
49  */
50 #define VMWARE_LENGTH_BUG_WORKAROUND 0
51
52 static char *dpdk_error_strings[] = {
53 #define _(n,s) s,
54   foreach_dpdk_error
55 #undef _
56 };
57
58 always_inline int
59 dpdk_mbuf_is_ip4 (struct rte_mbuf *mb)
60 {
61   return RTE_ETH_IS_IPV4_HDR (mb->packet_type) != 0;
62 }
63
64 always_inline int
65 dpdk_mbuf_is_ip6 (struct rte_mbuf *mb)
66 {
67   return RTE_ETH_IS_IPV6_HDR (mb->packet_type) != 0;
68 }
69
70 always_inline int
71 vlib_buffer_is_mpls (vlib_buffer_t * b)
72 {
73   ethernet_header_t *h = (ethernet_header_t *) b->data;
74   return (h->type == clib_host_to_net_u16 (ETHERNET_TYPE_MPLS_UNICAST));
75 }
76
77 always_inline void
78 dpdk_rx_next_and_error_from_mb_flags_x1 (dpdk_device_t * xd,
79                                          struct rte_mbuf *mb,
80                                          vlib_buffer_t * b0, u8 * next0,
81                                          u8 * error0)
82 {
83   u8 n0;
84   uint16_t mb_flags = mb->ol_flags;
85
86   if (PREDICT_FALSE (mb_flags & (
87 #ifdef RTE_LIBRTE_MBUF_EXT_RX_OLFLAGS
88                                   PKT_EXT_RX_PKT_ERROR | PKT_EXT_RX_BAD_FCS |
89 #endif /* RTE_LIBRTE_MBUF_EXT_RX_OLFLAGS */
90                                   PKT_RX_IP_CKSUM_BAD | PKT_RX_L4_CKSUM_BAD)))
91     {
92       /* some error was flagged. determine the drop reason */
93       n0 = DPDK_RX_NEXT_DROP;
94       *error0 =
95 #ifdef RTE_LIBRTE_MBUF_EXT_RX_OLFLAGS
96         (mb_flags & PKT_EXT_RX_PKT_ERROR) ? DPDK_ERROR_RX_PACKET_ERROR :
97         (mb_flags & PKT_EXT_RX_BAD_FCS) ? DPDK_ERROR_RX_BAD_FCS :
98 #endif /* RTE_LIBRTE_MBUF_EXT_RX_OLFLAGS */
99         (mb_flags & PKT_RX_IP_CKSUM_BAD) ? DPDK_ERROR_IP_CHECKSUM_ERROR :
100         (mb_flags & PKT_RX_L4_CKSUM_BAD) ? DPDK_ERROR_L4_CHECKSUM_ERROR :
101         DPDK_ERROR_NONE;
102     }
103   else
104     {
105       *error0 = DPDK_ERROR_NONE;
106       if (PREDICT_FALSE (xd->per_interface_next_index != ~0))
107         {
108           n0 = xd->per_interface_next_index;
109           b0->flags |= BUFFER_HANDOFF_NEXT_VALID;
110           if (PREDICT_TRUE (dpdk_mbuf_is_ip4 (mb)))
111             vnet_buffer (b0)->handoff.next_index =
112               HANDOFF_DISPATCH_NEXT_IP4_INPUT;
113           else if (PREDICT_TRUE (dpdk_mbuf_is_ip6 (mb)))
114             vnet_buffer (b0)->handoff.next_index =
115               HANDOFF_DISPATCH_NEXT_IP6_INPUT;
116           else if (PREDICT_TRUE (vlib_buffer_is_mpls (b0)))
117             vnet_buffer (b0)->handoff.next_index =
118               HANDOFF_DISPATCH_NEXT_MPLS_INPUT;
119           else
120             vnet_buffer (b0)->handoff.next_index =
121               HANDOFF_DISPATCH_NEXT_ETHERNET_INPUT;
122         }
123       else
124         if (PREDICT_FALSE ((xd->flags & DPDK_DEVICE_FLAG_HAVE_SUBIF) ||
125                            (mb_flags & PKT_RX_VLAN_PKT)))
126         n0 = DPDK_RX_NEXT_ETHERNET_INPUT;
127       else
128         {
129           if (PREDICT_TRUE (dpdk_mbuf_is_ip4 (mb)))
130             n0 = DPDK_RX_NEXT_IP4_INPUT;
131           else if (PREDICT_TRUE (dpdk_mbuf_is_ip6 (mb)))
132             n0 = DPDK_RX_NEXT_IP6_INPUT;
133           else if (PREDICT_TRUE (vlib_buffer_is_mpls (b0)))
134             n0 = DPDK_RX_NEXT_MPLS_INPUT;
135           else
136             n0 = DPDK_RX_NEXT_ETHERNET_INPUT;
137         }
138     }
139   *next0 = n0;
140 }
141
142 void
143 dpdk_rx_trace (dpdk_main_t * dm,
144                vlib_node_runtime_t * node,
145                dpdk_device_t * xd,
146                u16 queue_id, u32 * buffers, uword n_buffers)
147 {
148   vlib_main_t *vm = vlib_get_main ();
149   u32 *b, n_left;
150   u8 next0;
151
152   n_left = n_buffers;
153   b = buffers;
154
155   while (n_left >= 1)
156     {
157       u32 bi0;
158       vlib_buffer_t *b0;
159       dpdk_rx_dma_trace_t *t0;
160       struct rte_mbuf *mb;
161       u8 error0;
162
163       bi0 = b[0];
164       n_left -= 1;
165
166       b0 = vlib_get_buffer (vm, bi0);
167       mb = rte_mbuf_from_vlib_buffer (b0);
168       dpdk_rx_next_and_error_from_mb_flags_x1 (xd, mb, b0, &next0, &error0);
169       vlib_trace_buffer (vm, node, next0, b0, /* follow_chain */ 0);
170       t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
171       t0->queue_index = queue_id;
172       t0->device_index = xd->device_index;
173       t0->buffer_index = bi0;
174
175       clib_memcpy (&t0->mb, mb, sizeof (t0->mb));
176       clib_memcpy (&t0->buffer, b0, sizeof (b0[0]) - sizeof (b0->pre_data));
177       clib_memcpy (t0->buffer.pre_data, b0->data,
178                    sizeof (t0->buffer.pre_data));
179       clib_memcpy (&t0->data, mb->buf_addr + mb->data_off, sizeof (t0->data));
180
181 #ifdef RTE_LIBRTE_MBUF_EXT_RX_OLFLAGS
182       /*
183        * Clear overloaded TX offload flags when a DPDK driver
184        * is using them for RX flags (e.g. Cisco VIC Ethernet driver)
185        */
186       mb->ol_flags &= PKT_EXT_RX_CLR_TX_FLAGS_MASK;
187 #endif /* RTE_LIBRTE_MBUF_EXT_RX_OLFLAGS */
188
189       b += 1;
190     }
191 }
192
193 /*
194  * dpdk_efd_update_counters()
195  * Update EFD (early-fast-discard) counters
196  */
197 void
198 dpdk_efd_update_counters (dpdk_device_t * xd, u32 n_buffers, u16 enabled)
199 {
200   if (enabled & DPDK_EFD_MONITOR_ENABLED)
201     {
202       u64 now = clib_cpu_time_now ();
203       if (xd->efd_agent.last_poll_time > 0)
204         {
205           u64 elapsed_time = (now - xd->efd_agent.last_poll_time);
206           if (elapsed_time > xd->efd_agent.max_poll_delay)
207             xd->efd_agent.max_poll_delay = elapsed_time;
208         }
209       xd->efd_agent.last_poll_time = now;
210     }
211
212   xd->efd_agent.total_packet_cnt += n_buffers;
213   xd->efd_agent.last_burst_sz = n_buffers;
214
215   if (n_buffers > xd->efd_agent.max_burst_sz)
216     xd->efd_agent.max_burst_sz = n_buffers;
217
218   if (PREDICT_FALSE (n_buffers == VLIB_FRAME_SIZE))
219     {
220       xd->efd_agent.full_frames_cnt++;
221       xd->efd_agent.consec_full_frames_cnt++;
222     }
223   else
224     {
225       xd->efd_agent.consec_full_frames_cnt = 0;
226     }
227 }
228
229 /* is_efd_discardable()
230  *   returns non zero DPDK error if packet meets early-fast-discard criteria,
231  *           zero otherwise
232  */
233 u32
234 is_efd_discardable (vlib_thread_main_t * tm,
235                     vlib_buffer_t * b0, struct rte_mbuf *mb)
236 {
237   ethernet_header_t *eh = (ethernet_header_t *) b0->data;
238
239   if (eh->type == clib_host_to_net_u16 (ETHERNET_TYPE_IP4))
240     {
241       ip4_header_t *ipv4 =
242         (ip4_header_t *) & (b0->data[sizeof (ethernet_header_t)]);
243       u8 pkt_prec = (ipv4->tos >> 5);
244
245       return (tm->efd.ip_prec_bitmap & (1 << pkt_prec) ?
246               DPDK_ERROR_IPV4_EFD_DROP_PKTS : DPDK_ERROR_NONE);
247     }
248   else if (eh->type == clib_net_to_host_u16 (ETHERNET_TYPE_IP6))
249     {
250       ip6_header_t *ipv6 =
251         (ip6_header_t *) & (b0->data[sizeof (ethernet_header_t)]);
252       u8 pkt_tclass =
253         ((ipv6->ip_version_traffic_class_and_flow_label >> 20) & 0xff);
254
255       return (tm->efd.ip_prec_bitmap & (1 << pkt_tclass) ?
256               DPDK_ERROR_IPV6_EFD_DROP_PKTS : DPDK_ERROR_NONE);
257     }
258   else if (eh->type == clib_net_to_host_u16 (ETHERNET_TYPE_MPLS_UNICAST))
259     {
260       mpls_unicast_header_t *mpls =
261         (mpls_unicast_header_t *) & (b0->data[sizeof (ethernet_header_t)]);
262       u8 pkt_exp = ((mpls->label_exp_s_ttl >> 9) & 0x07);
263
264       return (tm->efd.mpls_exp_bitmap & (1 << pkt_exp) ?
265               DPDK_ERROR_MPLS_EFD_DROP_PKTS : DPDK_ERROR_NONE);
266     }
267   else if ((eh->type == clib_net_to_host_u16 (ETHERNET_TYPE_VLAN)) ||
268            (eh->type == clib_net_to_host_u16 (ETHERNET_TYPE_DOT1AD)))
269     {
270       ethernet_vlan_header_t *vlan =
271         (ethernet_vlan_header_t *) & (b0->data[sizeof (ethernet_header_t)]);
272       u8 pkt_cos = ((vlan->priority_cfi_and_id >> 13) & 0x07);
273
274       return (tm->efd.vlan_cos_bitmap & (1 << pkt_cos) ?
275               DPDK_ERROR_VLAN_EFD_DROP_PKTS : DPDK_ERROR_NONE);
276     }
277
278   return DPDK_ERROR_NONE;
279 }
280
281 static inline u32
282 dpdk_rx_burst (dpdk_main_t * dm, dpdk_device_t * xd, u16 queue_id)
283 {
284   u32 n_buffers;
285   u32 n_left;
286   u32 n_this_chunk;
287
288   n_left = VLIB_FRAME_SIZE;
289   n_buffers = 0;
290
291   if (PREDICT_TRUE (xd->flags & DPDK_DEVICE_FLAG_PMD))
292     {
293       while (n_left)
294         {
295           n_this_chunk = rte_eth_rx_burst (xd->device_index, queue_id,
296                                            xd->rx_vectors[queue_id] +
297                                            n_buffers, n_left);
298           n_buffers += n_this_chunk;
299           n_left -= n_this_chunk;
300
301           /* Empirically, DPDK r1.8 produces vectors w/ 32 or fewer elts */
302           if (n_this_chunk < 32)
303             break;
304         }
305     }
306   else
307     {
308       ASSERT (0);
309     }
310
311   return n_buffers;
312 }
313
314 /*
315  * This function is used when there are no worker threads.
316  * The main thread performs IO and forwards the packets.
317  */
318 static inline u32
319 dpdk_device_input (dpdk_main_t * dm,
320                    dpdk_device_t * xd,
321                    vlib_node_runtime_t * node,
322                    u32 cpu_index, u16 queue_id, int use_efd)
323 {
324   u32 n_buffers;
325   u32 next_index = DPDK_RX_NEXT_ETHERNET_INPUT;
326   u32 n_left_to_next, *to_next;
327   u32 mb_index;
328   vlib_main_t *vm = vlib_get_main ();
329   uword n_rx_bytes = 0;
330   u32 n_trace, trace_cnt __attribute__ ((unused));
331   vlib_buffer_free_list_t *fl;
332   u8 efd_discard_burst = 0;
333   u32 buffer_flags_template;
334
335   if ((xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP) == 0)
336     return 0;
337
338   n_buffers = dpdk_rx_burst (dm, xd, queue_id);
339
340   if (n_buffers == 0)
341     {
342       /* check if EFD (dpdk) is enabled */
343       if (PREDICT_FALSE (use_efd && dm->efd.enabled))
344         {
345           /* reset a few stats */
346           xd->efd_agent.last_poll_time = 0;
347           xd->efd_agent.last_burst_sz = 0;
348         }
349       return 0;
350     }
351
352   buffer_flags_template = dm->buffer_flags_template;
353
354   vec_reset_length (xd->d_trace_buffers);
355   trace_cnt = n_trace = vlib_get_trace_count (vm, node);
356
357   fl = vlib_buffer_get_free_list (vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
358
359   /* Check for congestion if EFD (Early-Fast-Discard) is enabled
360    * in any mode (e.g. dpdk, monitor, or drop_all)
361    */
362   if (PREDICT_FALSE (use_efd && dm->efd.enabled))
363     {
364       /* update EFD counters */
365       dpdk_efd_update_counters (xd, n_buffers, dm->efd.enabled);
366
367       if (PREDICT_FALSE (dm->efd.enabled & DPDK_EFD_DROPALL_ENABLED))
368         {
369           /* discard all received packets */
370           for (mb_index = 0; mb_index < n_buffers; mb_index++)
371             rte_pktmbuf_free (xd->rx_vectors[queue_id][mb_index]);
372
373           xd->efd_agent.discard_cnt += n_buffers;
374           increment_efd_drop_counter (vm,
375                                       DPDK_ERROR_VLAN_EFD_DROP_PKTS,
376                                       n_buffers);
377
378           return 0;
379         }
380
381       if (PREDICT_FALSE (xd->efd_agent.consec_full_frames_cnt >=
382                          dm->efd.consec_full_frames_hi_thresh))
383         {
384           u32 device_queue_sz = rte_eth_rx_queue_count (xd->device_index,
385                                                         queue_id);
386           if (device_queue_sz >= dm->efd.queue_hi_thresh)
387             {
388               /* dpdk device queue has reached the critical threshold */
389               xd->efd_agent.congestion_cnt++;
390
391               /* apply EFD to packets from the burst */
392               efd_discard_burst = 1;
393             }
394         }
395     }
396
397   mb_index = 0;
398
399   while (n_buffers > 0)
400     {
401       u32 bi0;
402       u8 next0, error0;
403       u32 l3_offset0;
404       vlib_buffer_t *b0, *b_seg, *b_chain = 0;
405       u32 cntr_type;
406
407       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
408
409       while (n_buffers > 0 && n_left_to_next > 0)
410         {
411           u8 nb_seg = 1;
412           struct rte_mbuf *mb = xd->rx_vectors[queue_id][mb_index];
413           struct rte_mbuf *mb_seg = mb->next;
414
415           if (PREDICT_TRUE (n_buffers > 2))
416             {
417               struct rte_mbuf *pfmb = xd->rx_vectors[queue_id][mb_index + 2];
418               vlib_buffer_t *bp = vlib_buffer_from_rte_mbuf (pfmb);
419               CLIB_PREFETCH (pfmb, CLIB_CACHE_LINE_BYTES, STORE);
420               CLIB_PREFETCH (bp, CLIB_CACHE_LINE_BYTES, STORE);
421             }
422
423           ASSERT (mb);
424
425           b0 = vlib_buffer_from_rte_mbuf (mb);
426
427           /* check whether EFD is looking for packets to discard */
428           if (PREDICT_FALSE (efd_discard_burst))
429             {
430               vlib_thread_main_t *tm = vlib_get_thread_main ();
431
432               if (PREDICT_TRUE (cntr_type = is_efd_discardable (tm, b0, mb)))
433                 {
434                   rte_pktmbuf_free (mb);
435                   xd->efd_agent.discard_cnt++;
436                   increment_efd_drop_counter (vm, cntr_type, 1);
437                   n_buffers--;
438                   mb_index++;
439                   continue;
440                 }
441             }
442
443           /* Prefetch one next segment if it exists. */
444           if (PREDICT_FALSE (mb->nb_segs > 1))
445             {
446               struct rte_mbuf *pfmb = mb->next;
447               vlib_buffer_t *bp = vlib_buffer_from_rte_mbuf (pfmb);
448               CLIB_PREFETCH (pfmb, CLIB_CACHE_LINE_BYTES, LOAD);
449               CLIB_PREFETCH (bp, CLIB_CACHE_LINE_BYTES, STORE);
450               b_chain = b0;
451             }
452
453           vlib_buffer_init_for_free_list (b0, fl);
454
455           bi0 = vlib_get_buffer_index (vm, b0);
456
457           to_next[0] = bi0;
458           to_next++;
459           n_left_to_next--;
460
461           dpdk_rx_next_and_error_from_mb_flags_x1 (xd, mb, b0,
462                                                    &next0, &error0);
463 #ifdef RTE_LIBRTE_MBUF_EXT_RX_OLFLAGS
464           /*
465            * Clear overloaded TX offload flags when a DPDK driver
466            * is using them for RX flags (e.g. Cisco VIC Ethernet driver)
467            */
468
469           if (PREDICT_TRUE (trace_cnt == 0))
470             mb->ol_flags &= PKT_EXT_RX_CLR_TX_FLAGS_MASK;
471           else
472             trace_cnt--;
473 #endif /* RTE_LIBRTE_MBUF_EXT_RX_OLFLAGS */
474
475           b0->error = node->errors[error0];
476
477           l3_offset0 = ((next0 == DPDK_RX_NEXT_IP4_INPUT ||
478                          next0 == DPDK_RX_NEXT_IP6_INPUT ||
479                          next0 == DPDK_RX_NEXT_MPLS_INPUT) ?
480                         sizeof (ethernet_header_t) : 0);
481
482           b0->current_data = l3_offset0;
483           /* Some drivers like fm10k receive frames with
484              mb->data_off > RTE_PKTMBUF_HEADROOM */
485           b0->current_data += mb->data_off - RTE_PKTMBUF_HEADROOM;
486           b0->current_length = mb->data_len - l3_offset0;
487
488           b0->flags = buffer_flags_template;
489
490           if (VMWARE_LENGTH_BUG_WORKAROUND)
491             b0->current_length -= 4;
492
493           vnet_buffer (b0)->sw_if_index[VLIB_RX] = xd->vlib_sw_if_index;
494           vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
495           n_rx_bytes += mb->pkt_len;
496
497           /* Process subsequent segments of multi-segment packets */
498           while ((mb->nb_segs > 1) && (nb_seg < mb->nb_segs))
499             {
500               ASSERT (mb_seg != 0);
501
502               b_seg = vlib_buffer_from_rte_mbuf (mb_seg);
503               vlib_buffer_init_for_free_list (b_seg, fl);
504
505               ASSERT ((b_seg->flags & VLIB_BUFFER_NEXT_PRESENT) == 0);
506               ASSERT (b_seg->current_data == 0);
507
508               /*
509                * The driver (e.g. virtio) may not put the packet data at the start
510                * of the segment, so don't assume b_seg->current_data == 0 is correct.
511                */
512               b_seg->current_data =
513                 (mb_seg->buf_addr + mb_seg->data_off) - (void *) b_seg->data;
514
515               b_seg->current_length = mb_seg->data_len;
516               b0->total_length_not_including_first_buffer += mb_seg->data_len;
517
518               b_chain->flags |= VLIB_BUFFER_NEXT_PRESENT;
519               b_chain->next_buffer = vlib_get_buffer_index (vm, b_seg);
520
521               b_chain = b_seg;
522               mb_seg = mb_seg->next;
523               nb_seg++;
524             }
525
526           /*
527            * Turn this on if you run into
528            * "bad monkey" contexts, and you want to know exactly
529            * which nodes they've visited... See main.c...
530            */
531           VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
532
533           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
534                                            to_next, n_left_to_next,
535                                            bi0, next0);
536           if (PREDICT_FALSE (n_trace > mb_index))
537             vec_add1 (xd->d_trace_buffers, bi0);
538           n_buffers--;
539           mb_index++;
540         }
541       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
542     }
543
544   if (PREDICT_FALSE (vec_len (xd->d_trace_buffers) > 0))
545     {
546       dpdk_rx_trace (dm, node, xd, queue_id, xd->d_trace_buffers,
547                      vec_len (xd->d_trace_buffers));
548       vlib_set_trace_count (vm, node,
549                             n_trace - vec_len (xd->d_trace_buffers));
550     }
551
552   vlib_increment_combined_counter
553     (vnet_get_main ()->interface_main.combined_sw_if_counters
554      + VNET_INTERFACE_COUNTER_RX,
555      cpu_index, xd->vlib_sw_if_index, mb_index, n_rx_bytes);
556
557   dpdk_worker_t *dw = vec_elt_at_index (dm->workers, cpu_index);
558   dw->aggregate_rx_packets += mb_index;
559
560   return mb_index;
561 }
562
563 static inline void
564 poll_rate_limit (dpdk_main_t * dm)
565 {
566   /* Limit the poll rate by sleeping for N msec between polls */
567   if (PREDICT_FALSE (dm->poll_sleep != 0))
568     {
569       struct timespec ts, tsrem;
570
571       ts.tv_sec = 0;
572       ts.tv_nsec = 1000 * 1000 * dm->poll_sleep;        /* 1ms */
573
574       while (nanosleep (&ts, &tsrem) < 0)
575         {
576           ts = tsrem;
577         }
578     }
579 }
580
581 /** \brief Main DPDK input node
582     @node dpdk-input
583
584     This is the main DPDK input node: across each assigned interface,
585     call rte_eth_rx_burst(...) or similar to obtain a vector of
586     packets to process. Handle early packet discard. Derive @c
587     vlib_buffer_t metadata from <code>struct rte_mbuf</code> metadata,
588     Depending on the resulting metadata: adjust <code>b->current_data,
589     b->current_length </code> and dispatch directly to
590     ip4-input-no-checksum, or ip6-input. Trace the packet if required.
591
592     @param vm   vlib_main_t corresponding to the current thread
593     @param node vlib_node_runtime_t
594     @param f    vlib_frame_t input-node, not used.
595
596     @par Graph mechanics: buffer metadata, next index usage
597
598     @em Uses:
599     - <code>struct rte_mbuf mb->ol_flags</code>
600         - PKT_EXT_RX_PKT_ERROR, PKT_EXT_RX_BAD_FCS
601         PKT_RX_IP_CKSUM_BAD, PKT_RX_L4_CKSUM_BAD
602     - <code> RTE_ETH_IS_xxx_HDR(mb->packet_type) </code>
603         - packet classification result
604
605     @em Sets:
606     - <code>b->error</code> if the packet is to be dropped immediately
607     - <code>b->current_data, b->current_length</code>
608         - adjusted as needed to skip the L2 header in  direct-dispatch cases
609     - <code>vnet_buffer(b)->sw_if_index[VLIB_RX]</code>
610         - rx interface sw_if_index
611     - <code>vnet_buffer(b)->sw_if_index[VLIB_TX] = ~0</code>
612         - required by ipX-lookup
613     - <code>b->flags</code>
614         - to indicate multi-segment pkts (VLIB_BUFFER_NEXT_PRESENT), etc.
615
616     <em>Next Nodes:</em>
617     - Static arcs to: error-drop, ethernet-input,
618       ip4-input-no-checksum, ip6-input, mpls-input
619     - per-interface redirection, controlled by
620       <code>xd->per_interface_next_index</code>
621 */
622
623 static uword
624 dpdk_input (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * f)
625 {
626   dpdk_main_t *dm = &dpdk_main;
627   dpdk_device_t *xd;
628   uword n_rx_packets = 0;
629   dpdk_device_and_queue_t *dq;
630   u32 cpu_index = os_get_cpu_number ();
631
632   /*
633    * Poll all devices on this cpu for input/interrupts.
634    */
635   /* *INDENT-OFF* */
636   vec_foreach (dq, dm->devices_by_cpu[cpu_index])
637     {
638       xd = vec_elt_at_index(dm->devices, dq->device);
639       ASSERT(dq->queue_id == 0);
640       n_rx_packets += dpdk_device_input (dm, xd, node, cpu_index, 0, 0);
641     }
642   /* *INDENT-ON* */
643
644   poll_rate_limit (dm);
645
646   return n_rx_packets;
647 }
648
649 uword
650 dpdk_input_rss (vlib_main_t * vm,
651                 vlib_node_runtime_t * node, vlib_frame_t * f)
652 {
653   dpdk_main_t *dm = &dpdk_main;
654   dpdk_device_t *xd;
655   uword n_rx_packets = 0;
656   dpdk_device_and_queue_t *dq;
657   u32 cpu_index = os_get_cpu_number ();
658
659   /*
660    * Poll all devices on this cpu for input/interrupts.
661    */
662   /* *INDENT-OFF* */
663   vec_foreach (dq, dm->devices_by_cpu[cpu_index])
664     {
665       xd = vec_elt_at_index(dm->devices, dq->device);
666       n_rx_packets += dpdk_device_input (dm, xd, node, cpu_index, dq->queue_id, 0);
667     }
668   /* *INDENT-ON* */
669
670   poll_rate_limit (dm);
671
672   return n_rx_packets;
673 }
674
675 uword
676 dpdk_input_efd (vlib_main_t * vm,
677                 vlib_node_runtime_t * node, vlib_frame_t * f)
678 {
679   dpdk_main_t *dm = &dpdk_main;
680   dpdk_device_t *xd;
681   uword n_rx_packets = 0;
682   dpdk_device_and_queue_t *dq;
683   u32 cpu_index = os_get_cpu_number ();
684
685   /*
686    * Poll all devices on this cpu for input/interrupts.
687    */
688   /* *INDENT-OFF* */
689   vec_foreach (dq, dm->devices_by_cpu[cpu_index])
690     {
691       xd = vec_elt_at_index(dm->devices, dq->device);
692       n_rx_packets += dpdk_device_input (dm, xd, node, cpu_index, dq->queue_id, 1);
693     }
694   /* *INDENT-ON* */
695
696   poll_rate_limit (dm);
697
698   return n_rx_packets;
699 }
700
701 /* *INDENT-OFF* */
702 VLIB_REGISTER_NODE (dpdk_input_node) = {
703   .function = dpdk_input,
704   .type = VLIB_NODE_TYPE_INPUT,
705   .name = "dpdk-input",
706
707   /* Will be enabled if/when hardware is detected. */
708   .state = VLIB_NODE_STATE_DISABLED,
709
710   .format_buffer = format_ethernet_header_with_length,
711   .format_trace = format_dpdk_rx_dma_trace,
712
713   .n_errors = DPDK_N_ERROR,
714   .error_strings = dpdk_error_strings,
715
716   .n_next_nodes = DPDK_RX_N_NEXT,
717   .next_nodes = {
718     [DPDK_RX_NEXT_DROP] = "error-drop",
719     [DPDK_RX_NEXT_ETHERNET_INPUT] = "ethernet-input",
720     [DPDK_RX_NEXT_IP4_INPUT] = "ip4-input-no-checksum",
721     [DPDK_RX_NEXT_IP6_INPUT] = "ip6-input",
722     [DPDK_RX_NEXT_MPLS_INPUT] = "mpls-input",
723   },
724 };
725
726
727 /* handle dpdk_input_rss alternative function */
728 VLIB_NODE_FUNCTION_MULTIARCH_CLONE(dpdk_input)
729 VLIB_NODE_FUNCTION_MULTIARCH_CLONE(dpdk_input_rss)
730 VLIB_NODE_FUNCTION_MULTIARCH_CLONE(dpdk_input_efd)
731
732 /* this macro defines dpdk_input_rss_multiarch_select() */
733 CLIB_MULTIARCH_SELECT_FN(dpdk_input);
734 CLIB_MULTIARCH_SELECT_FN(dpdk_input_rss);
735 CLIB_MULTIARCH_SELECT_FN(dpdk_input_efd);
736
737 /*
738  * Override the next nodes for the dpdk input nodes.
739  * Must be invoked prior to VLIB_INIT_FUNCTION calls.
740  */
741 void
742 dpdk_set_next_node (dpdk_rx_next_t next, char *name)
743 {
744   vlib_node_registration_t *r = &dpdk_input_node;
745   vlib_node_registration_t *r_handoff = &handoff_dispatch_node;
746
747   switch (next)
748     {
749     case DPDK_RX_NEXT_IP4_INPUT:
750     case DPDK_RX_NEXT_IP6_INPUT:
751     case DPDK_RX_NEXT_MPLS_INPUT:
752     case DPDK_RX_NEXT_ETHERNET_INPUT:
753       r->next_nodes[next] = name;
754       r_handoff->next_nodes[next] = name;
755       break;
756
757     default:
758       clib_warning ("%s: illegal next %d\n", __FUNCTION__, next);
759       break;
760     }
761 }
762
763 /*
764  * set_efd_bitmap()
765  * Based on the operation type, set lower/upper bits for the given index value
766  */
767 void
768 set_efd_bitmap (u8 * bitmap, u32 value, u32 op)
769 {
770   int ix;
771
772   *bitmap = 0;
773   for (ix = 0; ix < 8; ix++)
774     {
775       if (((op == EFD_OPERATION_LESS_THAN) && (ix < value)) ||
776           ((op == EFD_OPERATION_GREATER_OR_EQUAL) && (ix >= value)))
777         {
778           (*bitmap) |= (1 << ix);
779         }
780     }
781 }
782
783 void
784 efd_config (u32 enabled,
785             u32 ip_prec, u32 ip_op,
786             u32 mpls_exp, u32 mpls_op, u32 vlan_cos, u32 vlan_op)
787 {
788   vlib_thread_main_t *tm = vlib_get_thread_main ();
789   dpdk_main_t *dm = &dpdk_main;
790
791   if (enabled)
792     {
793       tm->efd.enabled |= VLIB_EFD_DISCARD_ENABLED;
794       dm->efd.enabled |= DPDK_EFD_DISCARD_ENABLED;
795     }
796   else
797     {
798       tm->efd.enabled &= ~VLIB_EFD_DISCARD_ENABLED;
799       dm->efd.enabled &= ~DPDK_EFD_DISCARD_ENABLED;
800     }
801
802   set_efd_bitmap (&tm->efd.ip_prec_bitmap, ip_prec, ip_op);
803   set_efd_bitmap (&tm->efd.mpls_exp_bitmap, mpls_exp, mpls_op);
804   set_efd_bitmap (&tm->efd.vlan_cos_bitmap, vlan_cos, vlan_op);
805 }