Retire support for DPDK 2.1.0 and older
[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-gre/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, struct rte_mbuf *mb,
79                                          vlib_buffer_t *b0,
80                                          u8 * next0, u8 * error0)
81 {
82   u8 n0;
83   uint16_t mb_flags = mb->ol_flags;
84
85   if (PREDICT_FALSE(mb_flags & (
86 #ifdef RTE_LIBRTE_MBUF_EXT_RX_OLFLAGS
87        PKT_EXT_RX_PKT_ERROR | PKT_EXT_RX_BAD_FCS   |
88 #endif /* RTE_LIBRTE_MBUF_EXT_RX_OLFLAGS */
89         PKT_RX_IP_CKSUM_BAD  | PKT_RX_L4_CKSUM_BAD
90     ))) 
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 = HANDOFF_DISPATCH_NEXT_IP4_INPUT;
112           else if (PREDICT_TRUE(dpdk_mbuf_is_ip6(mb)))
113             vnet_buffer(b0)->handoff.next_index = HANDOFF_DISPATCH_NEXT_IP6_INPUT;
114           else if (PREDICT_TRUE(vlib_buffer_is_mpls(b0)))
115             vnet_buffer(b0)->handoff.next_index = HANDOFF_DISPATCH_NEXT_MPLS_INPUT;
116           else
117             vnet_buffer(b0)->handoff.next_index = HANDOFF_DISPATCH_NEXT_ETHERNET_INPUT;
118         }
119       else if (PREDICT_FALSE(xd->vlan_subifs || (mb_flags & PKT_RX_VLAN_PKT)))
120         n0 = DPDK_RX_NEXT_ETHERNET_INPUT;
121       else
122         {
123           if (PREDICT_TRUE (dpdk_mbuf_is_ip4(mb)))
124             n0 = DPDK_RX_NEXT_IP4_INPUT;
125           else if (PREDICT_TRUE(dpdk_mbuf_is_ip6(mb)))
126             n0 = DPDK_RX_NEXT_IP6_INPUT;
127           else if (PREDICT_TRUE(vlib_buffer_is_mpls(b0)))
128             n0 = DPDK_RX_NEXT_MPLS_INPUT;
129           else
130             n0 = DPDK_RX_NEXT_ETHERNET_INPUT;
131         }
132     }
133   *next0 = n0;
134 }
135
136 void dpdk_rx_trace (dpdk_main_t * dm,
137                     vlib_node_runtime_t * node,
138                     dpdk_device_t * xd,
139                     u16 queue_id,
140                     u32 * buffers,
141                     uword n_buffers)
142 {
143   vlib_main_t * vm = vlib_get_main();
144   u32 * b, n_left;
145   u8 next0;
146
147   n_left = n_buffers;
148   b = buffers;
149
150   while (n_left >= 1)
151     {
152       u32 bi0;
153       vlib_buffer_t * b0;
154       dpdk_rx_dma_trace_t * t0;
155       struct rte_mbuf *mb;
156       u8 error0;
157
158       bi0 = b[0];
159       n_left -= 1;
160
161       b0 = vlib_get_buffer (vm, bi0);
162       mb = rte_mbuf_from_vlib_buffer(b0);
163       dpdk_rx_next_and_error_from_mb_flags_x1 (xd, mb, b0,
164                                                &next0, &error0);
165       vlib_trace_buffer (vm, node, next0, b0, /* follow_chain */ 0);
166       t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
167       t0->queue_index = queue_id;
168       t0->device_index = xd->device_index;
169       t0->buffer_index = bi0;
170
171       clib_memcpy (&t0->mb, mb, sizeof (t0->mb));
172       clib_memcpy (&t0->buffer, b0, sizeof (b0[0]) - sizeof (b0->pre_data));
173       clib_memcpy (t0->buffer.pre_data, b0->data, sizeof (t0->buffer.pre_data));
174       clib_memcpy (&t0->data, mb->buf_addr + mb->data_off, sizeof (t0->data));
175
176 #ifdef RTE_LIBRTE_MBUF_EXT_RX_OLFLAGS
177       /*
178        * Clear overloaded TX offload flags when a DPDK driver
179        * is using them for RX flags (e.g. Cisco VIC Ethernet driver)
180        */
181       mb->ol_flags &= PKT_EXT_RX_CLR_TX_FLAGS_MASK;
182 #endif /* RTE_LIBRTE_MBUF_EXT_RX_OLFLAGS */
183
184       b += 1;
185     }
186 }
187
188 /*
189  * dpdk_efd_update_counters()
190  * Update EFD (early-fast-discard) counters
191  */
192 void dpdk_efd_update_counters (dpdk_device_t *xd,
193                                u32 n_buffers,
194                                u16 enabled)
195 {
196   if (enabled & DPDK_EFD_MONITOR_ENABLED)
197     {
198       u64 now = clib_cpu_time_now();
199       if (xd->efd_agent.last_poll_time > 0)
200         {
201           u64 elapsed_time = (now - xd->efd_agent.last_poll_time);
202           if (elapsed_time > xd->efd_agent.max_poll_delay)
203             xd->efd_agent.max_poll_delay = elapsed_time;
204         }
205       xd->efd_agent.last_poll_time = now;
206     }
207   
208   xd->efd_agent.total_packet_cnt += n_buffers;
209   xd->efd_agent.last_burst_sz = n_buffers;
210
211   if (n_buffers > xd->efd_agent.max_burst_sz)
212     xd->efd_agent.max_burst_sz = n_buffers;
213
214   if (PREDICT_FALSE(n_buffers == VLIB_FRAME_SIZE))
215     {
216       xd->efd_agent.full_frames_cnt++;
217       xd->efd_agent.consec_full_frames_cnt++;
218     }
219   else
220     {
221       xd->efd_agent.consec_full_frames_cnt = 0;
222     }
223 }
224
225 /* is_efd_discardable()
226  *   returns non zero DPDK error if packet meets early-fast-discard criteria,
227  *           zero otherwise
228  */
229 u32 is_efd_discardable (vlib_thread_main_t *tm,
230                         vlib_buffer_t * b0,
231                         struct rte_mbuf *mb)
232 {
233   ethernet_header_t *eh = (ethernet_header_t *) b0->data;
234
235   if (eh->type == clib_host_to_net_u16(ETHERNET_TYPE_IP4))
236     {
237       ip4_header_t *ipv4 =
238           (ip4_header_t *)&(b0->data[sizeof(ethernet_header_t)]);
239       u8 pkt_prec = (ipv4->tos >> 5);
240           
241       return (tm->efd.ip_prec_bitmap & (1 << pkt_prec) ?
242                   DPDK_ERROR_IPV4_EFD_DROP_PKTS : DPDK_ERROR_NONE);
243     }
244   else if (eh->type == clib_net_to_host_u16(ETHERNET_TYPE_IP6))
245     {
246       ip6_header_t *ipv6 =
247           (ip6_header_t *)&(b0->data[sizeof(ethernet_header_t)]);
248       u8 pkt_tclass =
249           ((ipv6->ip_version_traffic_class_and_flow_label >> 20) & 0xff);
250           
251       return (tm->efd.ip_prec_bitmap & (1 << pkt_tclass) ?
252                   DPDK_ERROR_IPV6_EFD_DROP_PKTS : DPDK_ERROR_NONE);
253     }
254   else if (eh->type == clib_net_to_host_u16(ETHERNET_TYPE_MPLS_UNICAST))
255     {
256       mpls_unicast_header_t *mpls =
257           (mpls_unicast_header_t *)&(b0->data[sizeof(ethernet_header_t)]);
258       u8 pkt_exp = ((mpls->label_exp_s_ttl >> 9) & 0x07);
259
260       return (tm->efd.mpls_exp_bitmap & (1 << pkt_exp) ?
261                   DPDK_ERROR_MPLS_EFD_DROP_PKTS : DPDK_ERROR_NONE);
262     }
263   else if ((eh->type == clib_net_to_host_u16(ETHERNET_TYPE_VLAN)) ||
264            (eh->type == clib_net_to_host_u16(ETHERNET_TYPE_DOT1AD)))
265     {
266       ethernet_vlan_header_t *vlan =
267           (ethernet_vlan_header_t *)&(b0->data[sizeof(ethernet_header_t)]);
268       u8 pkt_cos = ((vlan->priority_cfi_and_id >> 13) & 0x07);
269
270       return (tm->efd.vlan_cos_bitmap & (1 << pkt_cos) ?
271                   DPDK_ERROR_VLAN_EFD_DROP_PKTS : DPDK_ERROR_NONE);
272     }
273
274   return DPDK_ERROR_NONE;
275 }
276
277 /*
278  * This function is used when there are no worker threads.
279  * The main thread performs IO and forwards the packets. 
280  */
281 static inline u32 dpdk_device_input ( dpdk_main_t * dm, 
282                                       dpdk_device_t * xd,
283                                       vlib_node_runtime_t * node,
284                                       u32 cpu_index,
285                                       u16 queue_id,
286                                       int use_efd)
287 {
288   u32 n_buffers;
289   u32 next_index = DPDK_RX_NEXT_ETHERNET_INPUT;
290   u32 n_left_to_next, * to_next;
291   u32 mb_index;
292   vlib_main_t * vm = vlib_get_main();
293   uword n_rx_bytes = 0;
294   u32 n_trace, trace_cnt __attribute__((unused));
295   vlib_buffer_free_list_t * fl;
296   u8 efd_discard_burst = 0;
297   u32 buffer_flags_template;
298   
299   if (xd->admin_up == 0)
300     return 0;
301
302   n_buffers = dpdk_rx_burst(dm, xd, queue_id);
303
304   if (n_buffers == 0)
305     {
306       /* check if EFD (dpdk) is enabled */
307       if (PREDICT_FALSE(use_efd && dm->efd.enabled))
308         {
309           /* reset a few stats */
310           xd->efd_agent.last_poll_time = 0;
311           xd->efd_agent.last_burst_sz = 0;
312         }
313       return 0;
314     }
315
316   buffer_flags_template = dm->buffer_flags_template;
317
318   vec_reset_length (xd->d_trace_buffers);
319   trace_cnt = n_trace = vlib_get_trace_count (vm, node);
320
321   fl = vlib_buffer_get_free_list (vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
322
323   /*
324    * DAW-FIXME: VMXNET3 device stop/start doesn't work, 
325    * therefore fake the stop in the dpdk driver by
326    * silently dropping all of the incoming pkts instead of 
327    * stopping the driver / hardware.
328    */
329   if (PREDICT_FALSE(xd->admin_up != 1))
330     {
331       for (mb_index = 0; mb_index < n_buffers; mb_index++)
332         rte_pktmbuf_free (xd->rx_vectors[queue_id][mb_index]);
333       
334       return 0;
335     }
336
337   /* Check for congestion if EFD (Early-Fast-Discard) is enabled
338    * in any mode (e.g. dpdk, monitor, or drop_all)
339    */
340   if (PREDICT_FALSE(use_efd && dm->efd.enabled))
341     {
342       /* update EFD counters */
343       dpdk_efd_update_counters(xd, n_buffers, dm->efd.enabled);
344
345       if (PREDICT_FALSE(dm->efd.enabled & DPDK_EFD_DROPALL_ENABLED))
346         {
347           /* discard all received packets */
348           for (mb_index = 0; mb_index < n_buffers; mb_index++)
349             rte_pktmbuf_free(xd->rx_vectors[queue_id][mb_index]);
350
351           xd->efd_agent.discard_cnt += n_buffers;
352           increment_efd_drop_counter(vm, 
353                                      DPDK_ERROR_VLAN_EFD_DROP_PKTS,
354                                      n_buffers);
355
356           return 0;
357         }
358       
359       if (PREDICT_FALSE(xd->efd_agent.consec_full_frames_cnt >=
360                         dm->efd.consec_full_frames_hi_thresh))
361         {
362           u32 device_queue_sz = rte_eth_rx_queue_count(xd->device_index,
363                                                        queue_id);
364           if (device_queue_sz >= dm->efd.queue_hi_thresh)
365             {
366               /* dpdk device queue has reached the critical threshold */
367               xd->efd_agent.congestion_cnt++;
368
369               /* apply EFD to packets from the burst */
370               efd_discard_burst = 1;
371             }
372         }
373     }
374   
375   mb_index = 0;
376
377   while (n_buffers > 0)
378     {
379       u32 bi0;
380       u8 next0, error0;
381       u32 l3_offset0;
382       vlib_buffer_t * b0, * b_seg, * b_chain = 0;
383       u32 cntr_type;
384
385       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
386
387       while (n_buffers > 0 && n_left_to_next > 0)
388         {
389           u8 nb_seg = 1;
390           struct rte_mbuf *mb = xd->rx_vectors[queue_id][mb_index];
391           struct rte_mbuf *mb_seg = mb->next;
392
393           if (PREDICT_TRUE(n_buffers > 2))
394           {
395               struct rte_mbuf *pfmb = xd->rx_vectors[queue_id][mb_index+2];
396               vlib_buffer_t *bp = vlib_buffer_from_rte_mbuf(pfmb);
397               CLIB_PREFETCH (pfmb, CLIB_CACHE_LINE_BYTES, STORE);
398               CLIB_PREFETCH (bp, CLIB_CACHE_LINE_BYTES, STORE);
399           }
400
401           ASSERT(mb);
402
403           b0 = vlib_buffer_from_rte_mbuf(mb);
404
405           /* check whether EFD is looking for packets to discard */
406           if (PREDICT_FALSE(efd_discard_burst))
407             {
408               vlib_thread_main_t * tm = vlib_get_thread_main();
409
410               if (PREDICT_TRUE(cntr_type = is_efd_discardable(tm, b0, mb)))
411                 {
412                   rte_pktmbuf_free(mb);
413                   xd->efd_agent.discard_cnt++;
414                   increment_efd_drop_counter(vm, 
415                                              cntr_type,
416                                              1);
417                   n_buffers--;
418                   mb_index++;
419                   continue;
420                 }
421             }
422
423           /* Prefetch one next segment if it exists. */
424           if (PREDICT_FALSE(mb->nb_segs > 1))
425             {
426               struct rte_mbuf *pfmb = mb->next;
427               vlib_buffer_t *bp = vlib_buffer_from_rte_mbuf(pfmb);
428               CLIB_PREFETCH (pfmb, CLIB_CACHE_LINE_BYTES, LOAD);
429               CLIB_PREFETCH (bp, CLIB_CACHE_LINE_BYTES, STORE);
430               b_chain = b0;
431             }
432
433           vlib_buffer_init_for_free_list (b0, fl);
434           
435           bi0 = vlib_get_buffer_index (vm, b0);
436
437           to_next[0] = bi0;
438           to_next++;
439           n_left_to_next--;
440           
441           dpdk_rx_next_and_error_from_mb_flags_x1 (xd, mb, b0,
442                                                    &next0, &error0);
443 #ifdef RTE_LIBRTE_MBUF_EXT_RX_OLFLAGS
444           /*
445            * Clear overloaded TX offload flags when a DPDK driver
446            * is using them for RX flags (e.g. Cisco VIC Ethernet driver)
447            */
448
449           if (PREDICT_TRUE(trace_cnt == 0))
450             mb->ol_flags &= PKT_EXT_RX_CLR_TX_FLAGS_MASK;
451           else
452             trace_cnt--;
453 #endif /* RTE_LIBRTE_MBUF_EXT_RX_OLFLAGS */
454
455           b0->error = node->errors[error0];
456
457           l3_offset0 = ((next0 == DPDK_RX_NEXT_IP4_INPUT ||
458                          next0 == DPDK_RX_NEXT_IP6_INPUT ||
459                          next0 == DPDK_RX_NEXT_MPLS_INPUT) ? 
460                         sizeof (ethernet_header_t) : 0);
461
462           b0->current_data = l3_offset0;
463           /* Some drivers like fm10k receive frames with
464              mb->data_off > RTE_PKTMBUF_HEADROOM */
465           b0->current_data += mb->data_off - RTE_PKTMBUF_HEADROOM;
466           b0->current_length = mb->data_len - l3_offset0;
467
468           b0->flags = buffer_flags_template;
469
470           if (VMWARE_LENGTH_BUG_WORKAROUND)
471               b0->current_length -= 4;
472
473           vnet_buffer(b0)->sw_if_index[VLIB_RX] = xd->vlib_sw_if_index;
474           vnet_buffer(b0)->sw_if_index[VLIB_TX] = (u32)~0;
475           n_rx_bytes += mb->pkt_len;
476
477           /* Process subsequent segments of multi-segment packets */
478           while ((mb->nb_segs > 1) && (nb_seg < mb->nb_segs))
479             {
480               ASSERT(mb_seg != 0);
481
482               b_seg = vlib_buffer_from_rte_mbuf(mb_seg);
483               vlib_buffer_init_for_free_list (b_seg, fl);
484
485               ASSERT((b_seg->flags & VLIB_BUFFER_NEXT_PRESENT) == 0);
486               ASSERT(b_seg->current_data == 0);
487
488               /*
489                * The driver (e.g. virtio) may not put the packet data at the start
490                * of the segment, so don't assume b_seg->current_data == 0 is correct.
491                */
492               b_seg->current_data = (mb_seg->buf_addr + mb_seg->data_off) - (void *)b_seg->data;
493
494               b_seg->current_length = mb_seg->data_len;
495               b0->total_length_not_including_first_buffer +=
496                 mb_seg->data_len;
497
498               b_chain->flags |= VLIB_BUFFER_NEXT_PRESENT;
499               b_chain->next_buffer = vlib_get_buffer_index (vm, b_seg);
500
501               b_chain = b_seg;
502               mb_seg = mb_seg->next;
503               nb_seg++;
504             } 
505
506           /*
507            * Turn this on if you run into
508            * "bad monkey" contexts, and you want to know exactly
509            * which nodes they've visited... See main.c...
510            */
511           VLIB_BUFFER_TRACE_TRAJECTORY_INIT(b0);
512
513           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
514                                            to_next, n_left_to_next,
515                                            bi0, next0);
516           if (PREDICT_FALSE (n_trace > mb_index))
517             vec_add1 (xd->d_trace_buffers, bi0);
518           n_buffers--;
519           mb_index++;
520         }
521       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
522     }
523
524   if (PREDICT_FALSE (vec_len (xd->d_trace_buffers) > 0))
525     {
526       dpdk_rx_trace (dm, node, xd, queue_id, xd->d_trace_buffers,
527                      vec_len (xd->d_trace_buffers));
528       vlib_set_trace_count (vm, node, n_trace - vec_len (xd->d_trace_buffers));
529     }
530   
531   vlib_increment_combined_counter 
532     (vnet_get_main()->interface_main.combined_sw_if_counters
533      + VNET_INTERFACE_COUNTER_RX,
534      cpu_index, 
535      xd->vlib_sw_if_index,
536      mb_index, n_rx_bytes);
537
538   dpdk_worker_t * dw = vec_elt_at_index(dm->workers, cpu_index);
539   dw->aggregate_rx_packets += mb_index;
540
541   return mb_index;
542 }
543
544 static inline void poll_rate_limit(dpdk_main_t * dm)
545 {
546   /* Limit the poll rate by sleeping for N msec between polls */
547   if (PREDICT_FALSE (dm->poll_sleep != 0))
548   {
549     struct timespec ts, tsrem;
550
551     ts.tv_sec = 0;
552     ts.tv_nsec = 1000*1000*dm->poll_sleep; /* 1ms */
553
554     while (nanosleep(&ts, &tsrem) < 0)
555       {
556         ts = tsrem;
557       }
558   }
559 }
560
561 static uword
562 dpdk_input (vlib_main_t * vm,
563             vlib_node_runtime_t * node,
564             vlib_frame_t * f)
565 {
566   dpdk_main_t * dm = &dpdk_main;
567   dpdk_device_t * xd;
568   uword n_rx_packets = 0;
569   dpdk_device_and_queue_t * dq;
570   u32 cpu_index = os_get_cpu_number();
571
572   /*
573    * Poll all devices on this cpu for input/interrupts.
574    */
575   vec_foreach (dq, dm->devices_by_cpu[cpu_index])
576     {
577       xd = vec_elt_at_index(dm->devices, dq->device);
578       ASSERT(dq->queue_id == 0);
579       n_rx_packets += dpdk_device_input (dm, xd, node, cpu_index, 0, 0);
580     }
581
582   poll_rate_limit(dm);
583
584   return n_rx_packets;
585 }
586
587 uword
588 dpdk_input_rss (vlib_main_t * vm,
589       vlib_node_runtime_t * node,
590       vlib_frame_t * f)
591 {
592   dpdk_main_t * dm = &dpdk_main;
593   dpdk_device_t * xd;
594   uword n_rx_packets = 0;
595   dpdk_device_and_queue_t * dq;
596   u32 cpu_index = os_get_cpu_number();
597
598   /*
599    * Poll all devices on this cpu for input/interrupts.
600    */
601   vec_foreach (dq, dm->devices_by_cpu[cpu_index])
602     {
603       xd = vec_elt_at_index(dm->devices, dq->device);
604       n_rx_packets += dpdk_device_input (dm, xd, node, cpu_index, dq->queue_id, 0);
605     }
606
607   poll_rate_limit(dm);
608
609   return n_rx_packets;
610 }
611
612 uword
613 dpdk_input_efd (vlib_main_t * vm,
614       vlib_node_runtime_t * node,
615       vlib_frame_t * f)
616 {
617   dpdk_main_t * dm = &dpdk_main;
618   dpdk_device_t * xd;
619   uword n_rx_packets = 0;
620   dpdk_device_and_queue_t * dq;
621   u32 cpu_index = os_get_cpu_number();
622
623   /*
624    * Poll all devices on this cpu for input/interrupts.
625    */
626   vec_foreach (dq, dm->devices_by_cpu[cpu_index])
627     {
628       xd = vec_elt_at_index(dm->devices, dq->device);
629       n_rx_packets += dpdk_device_input (dm, xd, node, cpu_index, dq->queue_id, 1);
630     }
631
632   poll_rate_limit(dm);
633
634   return n_rx_packets;
635 }
636
637
638 VLIB_REGISTER_NODE (dpdk_input_node) = {
639   .function = dpdk_input,
640   .type = VLIB_NODE_TYPE_INPUT,
641   .name = "dpdk-input",
642
643   /* Will be enabled if/when hardware is detected. */
644   .state = VLIB_NODE_STATE_DISABLED,
645
646   .format_buffer = format_ethernet_header_with_length,
647   .format_trace = format_dpdk_rx_dma_trace,
648
649   .n_errors = DPDK_N_ERROR,
650   .error_strings = dpdk_error_strings,
651
652   .n_next_nodes = DPDK_RX_N_NEXT,
653   .next_nodes = {
654     [DPDK_RX_NEXT_DROP] = "error-drop",
655     [DPDK_RX_NEXT_ETHERNET_INPUT] = "ethernet-input",
656     [DPDK_RX_NEXT_IP4_INPUT] = "ip4-input-no-checksum",
657     [DPDK_RX_NEXT_IP6_INPUT] = "ip6-input",
658     [DPDK_RX_NEXT_MPLS_INPUT] = "mpls-gre-input",
659   },
660 };
661
662
663 /* handle dpdk_input_rss alternative function */
664 VLIB_NODE_FUNCTION_MULTIARCH_CLONE(dpdk_input)
665 VLIB_NODE_FUNCTION_MULTIARCH_CLONE(dpdk_input_rss)
666 VLIB_NODE_FUNCTION_MULTIARCH_CLONE(dpdk_input_efd)
667
668 /* this macro defines dpdk_input_rss_multiarch_select() */
669 CLIB_MULTIARCH_SELECT_FN(dpdk_input);
670 CLIB_MULTIARCH_SELECT_FN(dpdk_input_rss);
671 CLIB_MULTIARCH_SELECT_FN(dpdk_input_efd);
672
673 /*
674  * Override the next nodes for the dpdk input nodes.
675  * Must be invoked prior to VLIB_INIT_FUNCTION calls.
676  */
677 void dpdk_set_next_node (dpdk_rx_next_t next, char *name)
678 {
679   vlib_node_registration_t *r = &dpdk_input_node;
680   vlib_node_registration_t *r_handoff = &handoff_dispatch_node;
681
682   switch (next)
683     {
684     case DPDK_RX_NEXT_IP4_INPUT:
685     case DPDK_RX_NEXT_IP6_INPUT:
686     case DPDK_RX_NEXT_MPLS_INPUT:
687     case DPDK_RX_NEXT_ETHERNET_INPUT:
688       r->next_nodes[next] = name;
689       r_handoff->next_nodes[next] = name;
690       break;
691
692     default:
693       clib_warning ("%s: illegal next %d\n", __FUNCTION__, next);
694       break;
695     }
696 }
697
698 /*
699  * set_efd_bitmap()
700  * Based on the operation type, set lower/upper bits for the given index value
701  */
702 void
703 set_efd_bitmap (u8 *bitmap, u32 value, u32 op)
704 {
705     int ix;
706
707     *bitmap = 0;
708     for (ix = 0; ix < 8; ix++) {
709         if (((op == EFD_OPERATION_LESS_THAN) && (ix < value)) ||
710             ((op == EFD_OPERATION_GREATER_OR_EQUAL) && (ix >= value))){
711             (*bitmap) |= (1 << ix);
712         }
713     }
714 }
715
716 void
717 efd_config (u32 enabled, 
718             u32 ip_prec,  u32 ip_op,
719             u32 mpls_exp, u32 mpls_op,
720             u32 vlan_cos, u32 vlan_op)
721 {
722    vlib_thread_main_t * tm = vlib_get_thread_main();
723    dpdk_main_t * dm = &dpdk_main;
724
725    if (enabled) {
726        tm->efd.enabled |= VLIB_EFD_DISCARD_ENABLED;
727        dm->efd.enabled |= DPDK_EFD_DISCARD_ENABLED;
728    } else {
729        tm->efd.enabled &= ~VLIB_EFD_DISCARD_ENABLED;
730        dm->efd.enabled &= ~DPDK_EFD_DISCARD_ENABLED;
731    }
732
733    set_efd_bitmap(&tm->efd.ip_prec_bitmap, ip_prec, ip_op);
734    set_efd_bitmap(&tm->efd.mpls_exp_bitmap, mpls_exp, mpls_op);
735    set_efd_bitmap(&tm->efd.vlan_cos_bitmap, vlan_cos, vlan_op);
736
737 }