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