A Protocol Independent Hierarchical FIB (VPP-352)
[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 #if DPDK_VHOST_USER
307   else if (xd->flags & DPDK_DEVICE_FLAG_VHOST_USER)
308     {
309       vlib_main_t *vm = vlib_get_main ();
310       vlib_buffer_main_t *bm = vm->buffer_main;
311       unsigned socket_id = rte_socket_id ();
312       u32 offset = 0;
313
314       offset = queue_id * VIRTIO_QNUM;
315
316       struct vhost_virtqueue *vq =
317         xd->vu_vhost_dev.virtqueue[offset + VIRTIO_TXQ];
318
319       if (PREDICT_FALSE (!vq->enabled))
320         return 0;
321
322       struct rte_mbuf **pkts = xd->rx_vectors[queue_id];
323       while (n_left)
324         {
325           n_this_chunk = rte_vhost_dequeue_burst (&xd->vu_vhost_dev,
326                                                   offset + VIRTIO_TXQ,
327                                                   bm->pktmbuf_pools
328                                                   [socket_id],
329                                                   pkts + n_buffers, n_left);
330           n_buffers += n_this_chunk;
331           n_left -= n_this_chunk;
332           if (n_this_chunk == 0)
333             break;
334         }
335
336       int i;
337       u32 bytes = 0;
338       for (i = 0; i < n_buffers; i++)
339         {
340           struct rte_mbuf *buff = pkts[i];
341           bytes += rte_pktmbuf_data_len (buff);
342         }
343
344       f64 now = vlib_time_now (vm);
345
346       dpdk_vu_vring *vring = NULL;
347       /* send pending interrupts if needed */
348       if (dpdk_vhost_user_want_interrupt (xd, offset + VIRTIO_TXQ))
349         {
350           vring = &(xd->vu_intf->vrings[offset + VIRTIO_TXQ]);
351           vring->n_since_last_int += n_buffers;
352
353           if ((vring->n_since_last_int && (vring->int_deadline < now))
354               || (vring->n_since_last_int > dm->conf->vhost_coalesce_frames))
355             dpdk_vhost_user_send_interrupt (vm, xd, offset + VIRTIO_TXQ);
356         }
357
358       vring = &(xd->vu_intf->vrings[offset + VIRTIO_RXQ]);
359       vring->packets += n_buffers;
360       vring->bytes += bytes;
361
362       if (dpdk_vhost_user_want_interrupt (xd, offset + VIRTIO_RXQ))
363         {
364           if (vring->n_since_last_int && (vring->int_deadline < now))
365             dpdk_vhost_user_send_interrupt (vm, xd, offset + VIRTIO_RXQ);
366         }
367
368     }
369 #endif
370 #ifdef RTE_LIBRTE_KNI
371   else if (xd->flags & DPDK_DEVICE_FLAG_KNI)
372     {
373       n_buffers =
374         rte_kni_rx_burst (xd->kni, xd->rx_vectors[queue_id], VLIB_FRAME_SIZE);
375       rte_kni_handle_request (xd->kni);
376     }
377 #endif
378   else
379     {
380       ASSERT (0);
381     }
382
383   return n_buffers;
384 }
385
386 /*
387  * This function is used when there are no worker threads.
388  * The main thread performs IO and forwards the packets.
389  */
390 static inline u32
391 dpdk_device_input (dpdk_main_t * dm,
392                    dpdk_device_t * xd,
393                    vlib_node_runtime_t * node,
394                    u32 cpu_index, u16 queue_id, int use_efd)
395 {
396   u32 n_buffers;
397   u32 next_index = DPDK_RX_NEXT_ETHERNET_INPUT;
398   u32 n_left_to_next, *to_next;
399   u32 mb_index;
400   vlib_main_t *vm = vlib_get_main ();
401   uword n_rx_bytes = 0;
402   u32 n_trace, trace_cnt __attribute__ ((unused));
403   vlib_buffer_free_list_t *fl;
404   u8 efd_discard_burst = 0;
405   u32 buffer_flags_template;
406
407   if ((xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP) == 0)
408     return 0;
409
410   n_buffers = dpdk_rx_burst (dm, xd, queue_id);
411
412   if (n_buffers == 0)
413     {
414       /* check if EFD (dpdk) is enabled */
415       if (PREDICT_FALSE (use_efd && dm->efd.enabled))
416         {
417           /* reset a few stats */
418           xd->efd_agent.last_poll_time = 0;
419           xd->efd_agent.last_burst_sz = 0;
420         }
421       return 0;
422     }
423
424   buffer_flags_template = dm->buffer_flags_template;
425
426   vec_reset_length (xd->d_trace_buffers);
427   trace_cnt = n_trace = vlib_get_trace_count (vm, node);
428
429   fl = vlib_buffer_get_free_list (vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
430
431   /* Check for congestion if EFD (Early-Fast-Discard) is enabled
432    * in any mode (e.g. dpdk, monitor, or drop_all)
433    */
434   if (PREDICT_FALSE (use_efd && dm->efd.enabled))
435     {
436       /* update EFD counters */
437       dpdk_efd_update_counters (xd, n_buffers, dm->efd.enabled);
438
439       if (PREDICT_FALSE (dm->efd.enabled & DPDK_EFD_DROPALL_ENABLED))
440         {
441           /* discard all received packets */
442           for (mb_index = 0; mb_index < n_buffers; mb_index++)
443             rte_pktmbuf_free (xd->rx_vectors[queue_id][mb_index]);
444
445           xd->efd_agent.discard_cnt += n_buffers;
446           increment_efd_drop_counter (vm,
447                                       DPDK_ERROR_VLAN_EFD_DROP_PKTS,
448                                       n_buffers);
449
450           return 0;
451         }
452
453       if (PREDICT_FALSE (xd->efd_agent.consec_full_frames_cnt >=
454                          dm->efd.consec_full_frames_hi_thresh))
455         {
456           u32 device_queue_sz = rte_eth_rx_queue_count (xd->device_index,
457                                                         queue_id);
458           if (device_queue_sz >= dm->efd.queue_hi_thresh)
459             {
460               /* dpdk device queue has reached the critical threshold */
461               xd->efd_agent.congestion_cnt++;
462
463               /* apply EFD to packets from the burst */
464               efd_discard_burst = 1;
465             }
466         }
467     }
468
469   mb_index = 0;
470
471   while (n_buffers > 0)
472     {
473       u32 bi0;
474       u8 next0, error0;
475       u32 l3_offset0;
476       vlib_buffer_t *b0, *b_seg, *b_chain = 0;
477       u32 cntr_type;
478
479       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
480
481       while (n_buffers > 0 && n_left_to_next > 0)
482         {
483           u8 nb_seg = 1;
484           struct rte_mbuf *mb = xd->rx_vectors[queue_id][mb_index];
485           struct rte_mbuf *mb_seg = mb->next;
486
487           if (PREDICT_TRUE (n_buffers > 2))
488             {
489               struct rte_mbuf *pfmb = xd->rx_vectors[queue_id][mb_index + 2];
490               vlib_buffer_t *bp = vlib_buffer_from_rte_mbuf (pfmb);
491               CLIB_PREFETCH (pfmb, CLIB_CACHE_LINE_BYTES, STORE);
492               CLIB_PREFETCH (bp, CLIB_CACHE_LINE_BYTES, STORE);
493             }
494
495           ASSERT (mb);
496
497           b0 = vlib_buffer_from_rte_mbuf (mb);
498
499           /* check whether EFD is looking for packets to discard */
500           if (PREDICT_FALSE (efd_discard_burst))
501             {
502               vlib_thread_main_t *tm = vlib_get_thread_main ();
503
504               if (PREDICT_TRUE (cntr_type = is_efd_discardable (tm, b0, mb)))
505                 {
506                   rte_pktmbuf_free (mb);
507                   xd->efd_agent.discard_cnt++;
508                   increment_efd_drop_counter (vm, cntr_type, 1);
509                   n_buffers--;
510                   mb_index++;
511                   continue;
512                 }
513             }
514
515           /* Prefetch one next segment if it exists. */
516           if (PREDICT_FALSE (mb->nb_segs > 1))
517             {
518               struct rte_mbuf *pfmb = mb->next;
519               vlib_buffer_t *bp = vlib_buffer_from_rte_mbuf (pfmb);
520               CLIB_PREFETCH (pfmb, CLIB_CACHE_LINE_BYTES, LOAD);
521               CLIB_PREFETCH (bp, CLIB_CACHE_LINE_BYTES, STORE);
522               b_chain = b0;
523             }
524
525           vlib_buffer_init_for_free_list (b0, fl);
526
527           bi0 = vlib_get_buffer_index (vm, b0);
528
529           to_next[0] = bi0;
530           to_next++;
531           n_left_to_next--;
532
533           dpdk_rx_next_and_error_from_mb_flags_x1 (xd, mb, b0,
534                                                    &next0, &error0);
535 #ifdef RTE_LIBRTE_MBUF_EXT_RX_OLFLAGS
536           /*
537            * Clear overloaded TX offload flags when a DPDK driver
538            * is using them for RX flags (e.g. Cisco VIC Ethernet driver)
539            */
540
541           if (PREDICT_TRUE (trace_cnt == 0))
542             mb->ol_flags &= PKT_EXT_RX_CLR_TX_FLAGS_MASK;
543           else
544             trace_cnt--;
545 #endif /* RTE_LIBRTE_MBUF_EXT_RX_OLFLAGS */
546
547           b0->error = node->errors[error0];
548
549           l3_offset0 = ((next0 == DPDK_RX_NEXT_IP4_INPUT ||
550                          next0 == DPDK_RX_NEXT_IP6_INPUT ||
551                          next0 == DPDK_RX_NEXT_MPLS_INPUT) ?
552                         sizeof (ethernet_header_t) : 0);
553
554           b0->current_data = l3_offset0;
555           /* Some drivers like fm10k receive frames with
556              mb->data_off > RTE_PKTMBUF_HEADROOM */
557           b0->current_data += mb->data_off - RTE_PKTMBUF_HEADROOM;
558           b0->current_length = mb->data_len - l3_offset0;
559
560           b0->flags = buffer_flags_template;
561
562           if (VMWARE_LENGTH_BUG_WORKAROUND)
563             b0->current_length -= 4;
564
565           vnet_buffer (b0)->sw_if_index[VLIB_RX] = xd->vlib_sw_if_index;
566           vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
567           n_rx_bytes += mb->pkt_len;
568
569           /* Process subsequent segments of multi-segment packets */
570           while ((mb->nb_segs > 1) && (nb_seg < mb->nb_segs))
571             {
572               ASSERT (mb_seg != 0);
573
574               b_seg = vlib_buffer_from_rte_mbuf (mb_seg);
575               vlib_buffer_init_for_free_list (b_seg, fl);
576
577               ASSERT ((b_seg->flags & VLIB_BUFFER_NEXT_PRESENT) == 0);
578               ASSERT (b_seg->current_data == 0);
579
580               /*
581                * The driver (e.g. virtio) may not put the packet data at the start
582                * of the segment, so don't assume b_seg->current_data == 0 is correct.
583                */
584               b_seg->current_data =
585                 (mb_seg->buf_addr + mb_seg->data_off) - (void *) b_seg->data;
586
587               b_seg->current_length = mb_seg->data_len;
588               b0->total_length_not_including_first_buffer += mb_seg->data_len;
589
590               b_chain->flags |= VLIB_BUFFER_NEXT_PRESENT;
591               b_chain->next_buffer = vlib_get_buffer_index (vm, b_seg);
592
593               b_chain = b_seg;
594               mb_seg = mb_seg->next;
595               nb_seg++;
596             }
597
598           /*
599            * Turn this on if you run into
600            * "bad monkey" contexts, and you want to know exactly
601            * which nodes they've visited... See main.c...
602            */
603           VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
604
605           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
606                                            to_next, n_left_to_next,
607                                            bi0, next0);
608           if (PREDICT_FALSE (n_trace > mb_index))
609             vec_add1 (xd->d_trace_buffers, bi0);
610           n_buffers--;
611           mb_index++;
612         }
613       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
614     }
615
616   if (PREDICT_FALSE (vec_len (xd->d_trace_buffers) > 0))
617     {
618       dpdk_rx_trace (dm, node, xd, queue_id, xd->d_trace_buffers,
619                      vec_len (xd->d_trace_buffers));
620       vlib_set_trace_count (vm, node,
621                             n_trace - vec_len (xd->d_trace_buffers));
622     }
623
624   vlib_increment_combined_counter
625     (vnet_get_main ()->interface_main.combined_sw_if_counters
626      + VNET_INTERFACE_COUNTER_RX,
627      cpu_index, xd->vlib_sw_if_index, mb_index, n_rx_bytes);
628
629   dpdk_worker_t *dw = vec_elt_at_index (dm->workers, cpu_index);
630   dw->aggregate_rx_packets += mb_index;
631
632   return mb_index;
633 }
634
635 static inline void
636 poll_rate_limit (dpdk_main_t * dm)
637 {
638   /* Limit the poll rate by sleeping for N msec between polls */
639   if (PREDICT_FALSE (dm->poll_sleep != 0))
640     {
641       struct timespec ts, tsrem;
642
643       ts.tv_sec = 0;
644       ts.tv_nsec = 1000 * 1000 * dm->poll_sleep;        /* 1ms */
645
646       while (nanosleep (&ts, &tsrem) < 0)
647         {
648           ts = tsrem;
649         }
650     }
651 }
652
653 /** \brief Main DPDK input node
654     @node dpdk-input
655
656     This is the main DPDK input node: across each assigned interface,
657     call rte_eth_rx_burst(...) or similar to obtain a vector of
658     packets to process. Handle early packet discard. Derive @c
659     vlib_buffer_t metadata from <code>struct rte_mbuf</code> metadata,
660     Depending on the resulting metadata: adjust <code>b->current_data,
661     b->current_length </code> and dispatch directly to
662     ip4-input-no-checksum, or ip6-input. Trace the packet if required.
663
664     @param vm   vlib_main_t corresponding to the current thread
665     @param node vlib_node_runtime_t
666     @param f    vlib_frame_t input-node, not used.
667
668     @par Graph mechanics: buffer metadata, next index usage
669
670     @em Uses:
671     - <code>struct rte_mbuf mb->ol_flags</code>
672         - PKT_EXT_RX_PKT_ERROR, PKT_EXT_RX_BAD_FCS
673         PKT_RX_IP_CKSUM_BAD, PKT_RX_L4_CKSUM_BAD
674     - <code> RTE_ETH_IS_xxx_HDR(mb->packet_type) </code>
675         - packet classification result
676
677     @em Sets:
678     - <code>b->error</code> if the packet is to be dropped immediately
679     - <code>b->current_data, b->current_length</code>
680         - adjusted as needed to skip the L2 header in  direct-dispatch cases
681     - <code>vnet_buffer(b)->sw_if_index[VLIB_RX]</code>
682         - rx interface sw_if_index
683     - <code>vnet_buffer(b)->sw_if_index[VLIB_TX] = ~0</code>
684         - required by ipX-lookup
685     - <code>b->flags</code>
686         - to indicate multi-segment pkts (VLIB_BUFFER_NEXT_PRESENT), etc.
687
688     <em>Next Nodes:</em>
689     - Static arcs to: error-drop, ethernet-input,
690       ip4-input-no-checksum, ip6-input, mpls-input
691     - per-interface redirection, controlled by
692       <code>xd->per_interface_next_index</code>
693 */
694
695 static uword
696 dpdk_input (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * f)
697 {
698   dpdk_main_t *dm = &dpdk_main;
699   dpdk_device_t *xd;
700   uword n_rx_packets = 0;
701   dpdk_device_and_queue_t *dq;
702   u32 cpu_index = os_get_cpu_number ();
703
704   /*
705    * Poll all devices on this cpu for input/interrupts.
706    */
707   /* *INDENT-OFF* */
708   vec_foreach (dq, dm->devices_by_cpu[cpu_index])
709     {
710       xd = vec_elt_at_index(dm->devices, dq->device);
711       ASSERT(dq->queue_id == 0);
712       n_rx_packets += dpdk_device_input (dm, xd, node, cpu_index, 0, 0);
713     }
714   /* *INDENT-ON* */
715
716   poll_rate_limit (dm);
717
718   return n_rx_packets;
719 }
720
721 uword
722 dpdk_input_rss (vlib_main_t * vm,
723                 vlib_node_runtime_t * node, vlib_frame_t * f)
724 {
725   dpdk_main_t *dm = &dpdk_main;
726   dpdk_device_t *xd;
727   uword n_rx_packets = 0;
728   dpdk_device_and_queue_t *dq;
729   u32 cpu_index = os_get_cpu_number ();
730
731   /*
732    * Poll all devices on this cpu for input/interrupts.
733    */
734   /* *INDENT-OFF* */
735   vec_foreach (dq, dm->devices_by_cpu[cpu_index])
736     {
737       xd = vec_elt_at_index(dm->devices, dq->device);
738       n_rx_packets += dpdk_device_input (dm, xd, node, cpu_index, dq->queue_id, 0);
739     }
740   /* *INDENT-ON* */
741
742   poll_rate_limit (dm);
743
744   return n_rx_packets;
745 }
746
747 uword
748 dpdk_input_efd (vlib_main_t * vm,
749                 vlib_node_runtime_t * node, vlib_frame_t * f)
750 {
751   dpdk_main_t *dm = &dpdk_main;
752   dpdk_device_t *xd;
753   uword n_rx_packets = 0;
754   dpdk_device_and_queue_t *dq;
755   u32 cpu_index = os_get_cpu_number ();
756
757   /*
758    * Poll all devices on this cpu for input/interrupts.
759    */
760   /* *INDENT-OFF* */
761   vec_foreach (dq, dm->devices_by_cpu[cpu_index])
762     {
763       xd = vec_elt_at_index(dm->devices, dq->device);
764       n_rx_packets += dpdk_device_input (dm, xd, node, cpu_index, dq->queue_id, 1);
765     }
766   /* *INDENT-ON* */
767
768   poll_rate_limit (dm);
769
770   return n_rx_packets;
771 }
772
773 /* *INDENT-OFF* */
774 VLIB_REGISTER_NODE (dpdk_input_node) = {
775   .function = dpdk_input,
776   .type = VLIB_NODE_TYPE_INPUT,
777   .name = "dpdk-input",
778
779   /* Will be enabled if/when hardware is detected. */
780   .state = VLIB_NODE_STATE_DISABLED,
781
782   .format_buffer = format_ethernet_header_with_length,
783   .format_trace = format_dpdk_rx_dma_trace,
784
785   .n_errors = DPDK_N_ERROR,
786   .error_strings = dpdk_error_strings,
787
788   .n_next_nodes = DPDK_RX_N_NEXT,
789   .next_nodes = {
790     [DPDK_RX_NEXT_DROP] = "error-drop",
791     [DPDK_RX_NEXT_ETHERNET_INPUT] = "ethernet-input",
792     [DPDK_RX_NEXT_IP4_INPUT] = "ip4-input-no-checksum",
793     [DPDK_RX_NEXT_IP6_INPUT] = "ip6-input",
794     [DPDK_RX_NEXT_MPLS_INPUT] = "mpls-input",
795   },
796 };
797
798
799 /* handle dpdk_input_rss alternative function */
800 VLIB_NODE_FUNCTION_MULTIARCH_CLONE(dpdk_input)
801 VLIB_NODE_FUNCTION_MULTIARCH_CLONE(dpdk_input_rss)
802 VLIB_NODE_FUNCTION_MULTIARCH_CLONE(dpdk_input_efd)
803
804 /* this macro defines dpdk_input_rss_multiarch_select() */
805 CLIB_MULTIARCH_SELECT_FN(dpdk_input);
806 CLIB_MULTIARCH_SELECT_FN(dpdk_input_rss);
807 CLIB_MULTIARCH_SELECT_FN(dpdk_input_efd);
808
809 /*
810  * Override the next nodes for the dpdk input nodes.
811  * Must be invoked prior to VLIB_INIT_FUNCTION calls.
812  */
813 void
814 dpdk_set_next_node (dpdk_rx_next_t next, char *name)
815 {
816   vlib_node_registration_t *r = &dpdk_input_node;
817   vlib_node_registration_t *r_handoff = &handoff_dispatch_node;
818
819   switch (next)
820     {
821     case DPDK_RX_NEXT_IP4_INPUT:
822     case DPDK_RX_NEXT_IP6_INPUT:
823     case DPDK_RX_NEXT_MPLS_INPUT:
824     case DPDK_RX_NEXT_ETHERNET_INPUT:
825       r->next_nodes[next] = name;
826       r_handoff->next_nodes[next] = name;
827       break;
828
829     default:
830       clib_warning ("%s: illegal next %d\n", __FUNCTION__, next);
831       break;
832     }
833 }
834
835 /*
836  * set_efd_bitmap()
837  * Based on the operation type, set lower/upper bits for the given index value
838  */
839 void
840 set_efd_bitmap (u8 * bitmap, u32 value, u32 op)
841 {
842   int ix;
843
844   *bitmap = 0;
845   for (ix = 0; ix < 8; ix++)
846     {
847       if (((op == EFD_OPERATION_LESS_THAN) && (ix < value)) ||
848           ((op == EFD_OPERATION_GREATER_OR_EQUAL) && (ix >= value)))
849         {
850           (*bitmap) |= (1 << ix);
851         }
852     }
853 }
854
855 void
856 efd_config (u32 enabled,
857             u32 ip_prec, u32 ip_op,
858             u32 mpls_exp, u32 mpls_op, u32 vlan_cos, u32 vlan_op)
859 {
860   vlib_thread_main_t *tm = vlib_get_thread_main ();
861   dpdk_main_t *dm = &dpdk_main;
862
863   if (enabled)
864     {
865       tm->efd.enabled |= VLIB_EFD_DISCARD_ENABLED;
866       dm->efd.enabled |= DPDK_EFD_DISCARD_ENABLED;
867     }
868   else
869     {
870       tm->efd.enabled &= ~VLIB_EFD_DISCARD_ENABLED;
871       dm->efd.enabled &= ~DPDK_EFD_DISCARD_ENABLED;
872     }
873
874   set_efd_bitmap (&tm->efd.ip_prec_bitmap, ip_prec, ip_op);
875   set_efd_bitmap (&tm->efd.mpls_exp_bitmap, mpls_exp, mpls_op);
876   set_efd_bitmap (&tm->efd.vlan_cos_bitmap, vlan_cos, vlan_op);
877 }