VPP-346 More VPP doc fixes
[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,
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->vlan_subifs || (mb_flags & PKT_RX_VLAN_PKT)))
125         n0 = DPDK_RX_NEXT_ETHERNET_INPUT;
126       else
127         {
128           if (PREDICT_TRUE (dpdk_mbuf_is_ip4 (mb)))
129             n0 = DPDK_RX_NEXT_IP4_INPUT;
130           else if (PREDICT_TRUE (dpdk_mbuf_is_ip6 (mb)))
131             n0 = DPDK_RX_NEXT_IP6_INPUT;
132           else if (PREDICT_TRUE (vlib_buffer_is_mpls (b0)))
133             n0 = DPDK_RX_NEXT_MPLS_INPUT;
134           else
135             n0 = DPDK_RX_NEXT_ETHERNET_INPUT;
136         }
137     }
138   *next0 = n0;
139 }
140
141 void
142 dpdk_rx_trace (dpdk_main_t * dm,
143                vlib_node_runtime_t * node,
144                dpdk_device_t * xd,
145                u16 queue_id, u32 * buffers, uword n_buffers)
146 {
147   vlib_main_t *vm = vlib_get_main ();
148   u32 *b, n_left;
149   u8 next0;
150
151   n_left = n_buffers;
152   b = buffers;
153
154   while (n_left >= 1)
155     {
156       u32 bi0;
157       vlib_buffer_t *b0;
158       dpdk_rx_dma_trace_t *t0;
159       struct rte_mbuf *mb;
160       u8 error0;
161
162       bi0 = b[0];
163       n_left -= 1;
164
165       b0 = vlib_get_buffer (vm, bi0);
166       mb = rte_mbuf_from_vlib_buffer (b0);
167       dpdk_rx_next_and_error_from_mb_flags_x1 (xd, mb, b0, &next0, &error0);
168       vlib_trace_buffer (vm, node, next0, b0, /* follow_chain */ 0);
169       t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
170       t0->queue_index = queue_id;
171       t0->device_index = xd->device_index;
172       t0->buffer_index = bi0;
173
174       clib_memcpy (&t0->mb, mb, sizeof (t0->mb));
175       clib_memcpy (&t0->buffer, b0, sizeof (b0[0]) - sizeof (b0->pre_data));
176       clib_memcpy (t0->buffer.pre_data, b0->data,
177                    sizeof (t0->buffer.pre_data));
178       clib_memcpy (&t0->data, mb->buf_addr + mb->data_off, sizeof (t0->data));
179
180 #ifdef RTE_LIBRTE_MBUF_EXT_RX_OLFLAGS
181       /*
182        * Clear overloaded TX offload flags when a DPDK driver
183        * is using them for RX flags (e.g. Cisco VIC Ethernet driver)
184        */
185       mb->ol_flags &= PKT_EXT_RX_CLR_TX_FLAGS_MASK;
186 #endif /* RTE_LIBRTE_MBUF_EXT_RX_OLFLAGS */
187
188       b += 1;
189     }
190 }
191
192 /*
193  * dpdk_efd_update_counters()
194  * Update EFD (early-fast-discard) counters
195  */
196 void
197 dpdk_efd_update_counters (dpdk_device_t * xd, u32 n_buffers, u16 enabled)
198 {
199   if (enabled & DPDK_EFD_MONITOR_ENABLED)
200     {
201       u64 now = clib_cpu_time_now ();
202       if (xd->efd_agent.last_poll_time > 0)
203         {
204           u64 elapsed_time = (now - xd->efd_agent.last_poll_time);
205           if (elapsed_time > xd->efd_agent.max_poll_delay)
206             xd->efd_agent.max_poll_delay = elapsed_time;
207         }
208       xd->efd_agent.last_poll_time = now;
209     }
210
211   xd->efd_agent.total_packet_cnt += n_buffers;
212   xd->efd_agent.last_burst_sz = n_buffers;
213
214   if (n_buffers > xd->efd_agent.max_burst_sz)
215     xd->efd_agent.max_burst_sz = n_buffers;
216
217   if (PREDICT_FALSE (n_buffers == VLIB_FRAME_SIZE))
218     {
219       xd->efd_agent.full_frames_cnt++;
220       xd->efd_agent.consec_full_frames_cnt++;
221     }
222   else
223     {
224       xd->efd_agent.consec_full_frames_cnt = 0;
225     }
226 }
227
228 /* is_efd_discardable()
229  *   returns non zero DPDK error if packet meets early-fast-discard criteria,
230  *           zero otherwise
231  */
232 u32
233 is_efd_discardable (vlib_thread_main_t * tm,
234                     vlib_buffer_t * b0, struct rte_mbuf *mb)
235 {
236   ethernet_header_t *eh = (ethernet_header_t *) b0->data;
237
238   if (eh->type == clib_host_to_net_u16 (ETHERNET_TYPE_IP4))
239     {
240       ip4_header_t *ipv4 =
241         (ip4_header_t *) & (b0->data[sizeof (ethernet_header_t)]);
242       u8 pkt_prec = (ipv4->tos >> 5);
243
244       return (tm->efd.ip_prec_bitmap & (1 << pkt_prec) ?
245               DPDK_ERROR_IPV4_EFD_DROP_PKTS : DPDK_ERROR_NONE);
246     }
247   else if (eh->type == clib_net_to_host_u16 (ETHERNET_TYPE_IP6))
248     {
249       ip6_header_t *ipv6 =
250         (ip6_header_t *) & (b0->data[sizeof (ethernet_header_t)]);
251       u8 pkt_tclass =
252         ((ipv6->ip_version_traffic_class_and_flow_label >> 20) & 0xff);
253
254       return (tm->efd.ip_prec_bitmap & (1 << pkt_tclass) ?
255               DPDK_ERROR_IPV6_EFD_DROP_PKTS : DPDK_ERROR_NONE);
256     }
257   else if (eh->type == clib_net_to_host_u16 (ETHERNET_TYPE_MPLS_UNICAST))
258     {
259       mpls_unicast_header_t *mpls =
260         (mpls_unicast_header_t *) & (b0->data[sizeof (ethernet_header_t)]);
261       u8 pkt_exp = ((mpls->label_exp_s_ttl >> 9) & 0x07);
262
263       return (tm->efd.mpls_exp_bitmap & (1 << pkt_exp) ?
264               DPDK_ERROR_MPLS_EFD_DROP_PKTS : DPDK_ERROR_NONE);
265     }
266   else if ((eh->type == clib_net_to_host_u16 (ETHERNET_TYPE_VLAN)) ||
267            (eh->type == clib_net_to_host_u16 (ETHERNET_TYPE_DOT1AD)))
268     {
269       ethernet_vlan_header_t *vlan =
270         (ethernet_vlan_header_t *) & (b0->data[sizeof (ethernet_header_t)]);
271       u8 pkt_cos = ((vlan->priority_cfi_and_id >> 13) & 0x07);
272
273       return (tm->efd.vlan_cos_bitmap & (1 << pkt_cos) ?
274               DPDK_ERROR_VLAN_EFD_DROP_PKTS : DPDK_ERROR_NONE);
275     }
276
277   return DPDK_ERROR_NONE;
278 }
279
280 static inline u32
281 dpdk_rx_burst (dpdk_main_t * dm, dpdk_device_t * xd, u16 queue_id)
282 {
283   u32 n_buffers;
284   u32 n_left;
285   u32 n_this_chunk;
286
287   n_left = VLIB_FRAME_SIZE;
288   n_buffers = 0;
289
290   if (PREDICT_TRUE (xd->dev_type == VNET_DPDK_DEV_ETH))
291     {
292       while (n_left)
293         {
294           n_this_chunk = rte_eth_rx_burst (xd->device_index, queue_id,
295                                            xd->rx_vectors[queue_id] +
296                                            n_buffers, n_left);
297           n_buffers += n_this_chunk;
298           n_left -= n_this_chunk;
299
300           /* Empirically, DPDK r1.8 produces vectors w/ 32 or fewer elts */
301           if (n_this_chunk < 32)
302             break;
303         }
304     }
305 #if DPDK_VHOST_USER
306   else if (xd->dev_type == VNET_DPDK_DEV_VHOST_USER)
307     {
308       vlib_main_t *vm = vlib_get_main ();
309       vlib_buffer_main_t *bm = vm->buffer_main;
310       unsigned socket_id = rte_socket_id ();
311       u32 offset = 0;
312
313       offset = queue_id * VIRTIO_QNUM;
314
315       struct vhost_virtqueue *vq =
316         xd->vu_vhost_dev.virtqueue[offset + VIRTIO_TXQ];
317
318       if (PREDICT_FALSE (!vq->enabled))
319         return 0;
320
321       struct rte_mbuf **pkts = xd->rx_vectors[queue_id];
322       while (n_left)
323         {
324           n_this_chunk = rte_vhost_dequeue_burst (&xd->vu_vhost_dev,
325                                                   offset + VIRTIO_TXQ,
326                                                   bm->pktmbuf_pools
327                                                   [socket_id],
328                                                   pkts + n_buffers, n_left);
329           n_buffers += n_this_chunk;
330           n_left -= n_this_chunk;
331           if (n_this_chunk == 0)
332             break;
333         }
334
335       int i;
336       u32 bytes = 0;
337       for (i = 0; i < n_buffers; i++)
338         {
339           struct rte_mbuf *buff = pkts[i];
340           bytes += rte_pktmbuf_data_len (buff);
341         }
342
343       f64 now = vlib_time_now (vm);
344
345       dpdk_vu_vring *vring = NULL;
346       /* send pending interrupts if needed */
347       if (dpdk_vhost_user_want_interrupt (xd, offset + VIRTIO_TXQ))
348         {
349           vring = &(xd->vu_intf->vrings[offset + VIRTIO_TXQ]);
350           vring->n_since_last_int += n_buffers;
351
352           if ((vring->n_since_last_int && (vring->int_deadline < now))
353               || (vring->n_since_last_int > dm->conf->vhost_coalesce_frames))
354             dpdk_vhost_user_send_interrupt (vm, xd, offset + VIRTIO_TXQ);
355         }
356
357       vring = &(xd->vu_intf->vrings[offset + VIRTIO_RXQ]);
358       vring->packets += n_buffers;
359       vring->bytes += bytes;
360
361       if (dpdk_vhost_user_want_interrupt (xd, offset + VIRTIO_RXQ))
362         {
363           if (vring->n_since_last_int && (vring->int_deadline < now))
364             dpdk_vhost_user_send_interrupt (vm, xd, offset + VIRTIO_RXQ);
365         }
366
367     }
368 #endif
369 #ifdef RTE_LIBRTE_KNI
370   else if (xd->dev_type == VNET_DPDK_DEV_KNI)
371     {
372       n_buffers =
373         rte_kni_rx_burst (xd->kni, xd->rx_vectors[queue_id], VLIB_FRAME_SIZE);
374       rte_kni_handle_request (xd->kni);
375     }
376 #endif
377   else
378     {
379       ASSERT (0);
380     }
381
382   return n_buffers;
383 }
384
385 /*
386  * This function is used when there are no worker threads.
387  * The main thread performs IO and forwards the packets.
388  */
389 static inline u32
390 dpdk_device_input (dpdk_main_t * dm,
391                    dpdk_device_t * xd,
392                    vlib_node_runtime_t * node,
393                    u32 cpu_index, u16 queue_id, int use_efd)
394 {
395   u32 n_buffers;
396   u32 next_index = DPDK_RX_NEXT_ETHERNET_INPUT;
397   u32 n_left_to_next, *to_next;
398   u32 mb_index;
399   vlib_main_t *vm = vlib_get_main ();
400   uword n_rx_bytes = 0;
401   u32 n_trace, trace_cnt __attribute__ ((unused));
402   vlib_buffer_free_list_t *fl;
403   u8 efd_discard_burst = 0;
404   u32 buffer_flags_template;
405
406   if ((xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP) == 0)
407     return 0;
408
409   n_buffers = dpdk_rx_burst (dm, xd, queue_id);
410
411   if (n_buffers == 0)
412     {
413       /* check if EFD (dpdk) is enabled */
414       if (PREDICT_FALSE (use_efd && dm->efd.enabled))
415         {
416           /* reset a few stats */
417           xd->efd_agent.last_poll_time = 0;
418           xd->efd_agent.last_burst_sz = 0;
419         }
420       return 0;
421     }
422
423   buffer_flags_template = dm->buffer_flags_template;
424
425   vec_reset_length (xd->d_trace_buffers);
426   trace_cnt = n_trace = vlib_get_trace_count (vm, node);
427
428   fl = vlib_buffer_get_free_list (vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
429
430   /* Check for congestion if EFD (Early-Fast-Discard) is enabled
431    * in any mode (e.g. dpdk, monitor, or drop_all)
432    */
433   if (PREDICT_FALSE (use_efd && dm->efd.enabled))
434     {
435       /* update EFD counters */
436       dpdk_efd_update_counters (xd, n_buffers, dm->efd.enabled);
437
438       if (PREDICT_FALSE (dm->efd.enabled & DPDK_EFD_DROPALL_ENABLED))
439         {
440           /* discard all received packets */
441           for (mb_index = 0; mb_index < n_buffers; mb_index++)
442             rte_pktmbuf_free (xd->rx_vectors[queue_id][mb_index]);
443
444           xd->efd_agent.discard_cnt += n_buffers;
445           increment_efd_drop_counter (vm,
446                                       DPDK_ERROR_VLAN_EFD_DROP_PKTS,
447                                       n_buffers);
448
449           return 0;
450         }
451
452       if (PREDICT_FALSE (xd->efd_agent.consec_full_frames_cnt >=
453                          dm->efd.consec_full_frames_hi_thresh))
454         {
455           u32 device_queue_sz = rte_eth_rx_queue_count (xd->device_index,
456                                                         queue_id);
457           if (device_queue_sz >= dm->efd.queue_hi_thresh)
458             {
459               /* dpdk device queue has reached the critical threshold */
460               xd->efd_agent.congestion_cnt++;
461
462               /* apply EFD to packets from the burst */
463               efd_discard_burst = 1;
464             }
465         }
466     }
467
468   mb_index = 0;
469
470   while (n_buffers > 0)
471     {
472       u32 bi0;
473       u8 next0, error0;
474       u32 l3_offset0;
475       vlib_buffer_t *b0, *b_seg, *b_chain = 0;
476       u32 cntr_type;
477
478       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
479
480       while (n_buffers > 0 && n_left_to_next > 0)
481         {
482           u8 nb_seg = 1;
483           struct rte_mbuf *mb = xd->rx_vectors[queue_id][mb_index];
484           struct rte_mbuf *mb_seg = mb->next;
485
486           if (PREDICT_TRUE (n_buffers > 2))
487             {
488               struct rte_mbuf *pfmb = xd->rx_vectors[queue_id][mb_index + 2];
489               vlib_buffer_t *bp = vlib_buffer_from_rte_mbuf (pfmb);
490               CLIB_PREFETCH (pfmb, CLIB_CACHE_LINE_BYTES, STORE);
491               CLIB_PREFETCH (bp, CLIB_CACHE_LINE_BYTES, STORE);
492             }
493
494           ASSERT (mb);
495
496           b0 = vlib_buffer_from_rte_mbuf (mb);
497
498           /* check whether EFD is looking for packets to discard */
499           if (PREDICT_FALSE (efd_discard_burst))
500             {
501               vlib_thread_main_t *tm = vlib_get_thread_main ();
502
503               if (PREDICT_TRUE (cntr_type = is_efd_discardable (tm, b0, mb)))
504                 {
505                   rte_pktmbuf_free (mb);
506                   xd->efd_agent.discard_cnt++;
507                   increment_efd_drop_counter (vm, cntr_type, 1);
508                   n_buffers--;
509                   mb_index++;
510                   continue;
511                 }
512             }
513
514           /* Prefetch one next segment if it exists. */
515           if (PREDICT_FALSE (mb->nb_segs > 1))
516             {
517               struct rte_mbuf *pfmb = mb->next;
518               vlib_buffer_t *bp = vlib_buffer_from_rte_mbuf (pfmb);
519               CLIB_PREFETCH (pfmb, CLIB_CACHE_LINE_BYTES, LOAD);
520               CLIB_PREFETCH (bp, CLIB_CACHE_LINE_BYTES, STORE);
521               b_chain = b0;
522             }
523
524           vlib_buffer_init_for_free_list (b0, fl);
525
526           bi0 = vlib_get_buffer_index (vm, b0);
527
528           to_next[0] = bi0;
529           to_next++;
530           n_left_to_next--;
531
532           dpdk_rx_next_and_error_from_mb_flags_x1 (xd, mb, b0,
533                                                    &next0, &error0);
534 #ifdef RTE_LIBRTE_MBUF_EXT_RX_OLFLAGS
535           /*
536            * Clear overloaded TX offload flags when a DPDK driver
537            * is using them for RX flags (e.g. Cisco VIC Ethernet driver)
538            */
539
540           if (PREDICT_TRUE (trace_cnt == 0))
541             mb->ol_flags &= PKT_EXT_RX_CLR_TX_FLAGS_MASK;
542           else
543             trace_cnt--;
544 #endif /* RTE_LIBRTE_MBUF_EXT_RX_OLFLAGS */
545
546           b0->error = node->errors[error0];
547
548           l3_offset0 = ((next0 == DPDK_RX_NEXT_IP4_INPUT ||
549                          next0 == DPDK_RX_NEXT_IP6_INPUT ||
550                          next0 == DPDK_RX_NEXT_MPLS_INPUT) ?
551                         sizeof (ethernet_header_t) : 0);
552
553           b0->current_data = l3_offset0;
554           /* Some drivers like fm10k receive frames with
555              mb->data_off > RTE_PKTMBUF_HEADROOM */
556           b0->current_data += mb->data_off - RTE_PKTMBUF_HEADROOM;
557           b0->current_length = mb->data_len - l3_offset0;
558
559           b0->flags = buffer_flags_template;
560
561           if (VMWARE_LENGTH_BUG_WORKAROUND)
562             b0->current_length -= 4;
563
564           vnet_buffer (b0)->sw_if_index[VLIB_RX] = xd->vlib_sw_if_index;
565           vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
566           n_rx_bytes += mb->pkt_len;
567
568           /* Process subsequent segments of multi-segment packets */
569           while ((mb->nb_segs > 1) && (nb_seg < mb->nb_segs))
570             {
571               ASSERT (mb_seg != 0);
572
573               b_seg = vlib_buffer_from_rte_mbuf (mb_seg);
574               vlib_buffer_init_for_free_list (b_seg, fl);
575
576               ASSERT ((b_seg->flags & VLIB_BUFFER_NEXT_PRESENT) == 0);
577               ASSERT (b_seg->current_data == 0);
578
579               /*
580                * The driver (e.g. virtio) may not put the packet data at the start
581                * of the segment, so don't assume b_seg->current_data == 0 is correct.
582                */
583               b_seg->current_data =
584                 (mb_seg->buf_addr + mb_seg->data_off) - (void *) b_seg->data;
585
586               b_seg->current_length = mb_seg->data_len;
587               b0->total_length_not_including_first_buffer += mb_seg->data_len;
588
589               b_chain->flags |= VLIB_BUFFER_NEXT_PRESENT;
590               b_chain->next_buffer = vlib_get_buffer_index (vm, b_seg);
591
592               b_chain = b_seg;
593               mb_seg = mb_seg->next;
594               nb_seg++;
595             }
596
597           /*
598            * Turn this on if you run into
599            * "bad monkey" contexts, and you want to know exactly
600            * which nodes they've visited... See main.c...
601            */
602           VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
603
604           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
605                                            to_next, n_left_to_next,
606                                            bi0, next0);
607           if (PREDICT_FALSE (n_trace > mb_index))
608             vec_add1 (xd->d_trace_buffers, bi0);
609           n_buffers--;
610           mb_index++;
611         }
612       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
613     }
614
615   if (PREDICT_FALSE (vec_len (xd->d_trace_buffers) > 0))
616     {
617       dpdk_rx_trace (dm, node, xd, queue_id, xd->d_trace_buffers,
618                      vec_len (xd->d_trace_buffers));
619       vlib_set_trace_count (vm, node,
620                             n_trace - vec_len (xd->d_trace_buffers));
621     }
622
623   vlib_increment_combined_counter
624     (vnet_get_main ()->interface_main.combined_sw_if_counters
625      + VNET_INTERFACE_COUNTER_RX,
626      cpu_index, xd->vlib_sw_if_index, mb_index, n_rx_bytes);
627
628   dpdk_worker_t *dw = vec_elt_at_index (dm->workers, cpu_index);
629   dw->aggregate_rx_packets += mb_index;
630
631   return mb_index;
632 }
633
634 static inline void
635 poll_rate_limit (dpdk_main_t * dm)
636 {
637   /* Limit the poll rate by sleeping for N msec between polls */
638   if (PREDICT_FALSE (dm->poll_sleep != 0))
639     {
640       struct timespec ts, tsrem;
641
642       ts.tv_sec = 0;
643       ts.tv_nsec = 1000 * 1000 * dm->poll_sleep;        /* 1ms */
644
645       while (nanosleep (&ts, &tsrem) < 0)
646         {
647           ts = tsrem;
648         }
649     }
650 }
651
652 /** \brief Main DPDK input node
653     @node dpdk-input
654
655     This is the main DPDK input node: across each assigned interface,
656     call rte_eth_rx_burst(...) or similar to obtain a vector of
657     packets to process. Handle early packet discard. Derive @c
658     vlib_buffer_t metadata from <code>struct rte_mbuf</code> metadata,
659     Depending on the resulting metadata: adjust <code>b->current_data,
660     b->current_length </code> and dispatch directly to
661     ip4-input-no-checksum, or ip6-input. Trace the packet if required.
662
663     @param vm   vlib_main_t corresponding to the current thread
664     @param node vlib_node_runtime_t
665     @param f    vlib_frame_t input-node, not used.
666
667     @par Graph mechanics: buffer metadata, next index usage
668
669     @em Uses:
670     - <code>struct rte_mbuf mb->ol_flags</code>
671         - PKT_EXT_RX_PKT_ERROR, PKT_EXT_RX_BAD_FCS
672         PKT_RX_IP_CKSUM_BAD, PKT_RX_L4_CKSUM_BAD
673     - <code> RTE_ETH_IS_xxx_HDR(mb->packet_type) </code>
674         - packet classification result
675
676     @em Sets:
677     - <code>b->error</code> if the packet is to be dropped immediately
678     - <code>b->current_data, b->current_length</code>
679         - adjusted as needed to skip the L2 header in  direct-dispatch cases
680     - <code>vnet_buffer(b)->sw_if_index[VLIB_RX]</code>
681         - rx interface sw_if_index
682     - <code>vnet_buffer(b)->sw_if_index[VLIB_TX] = ~0</code>
683         - required by ipX-lookup
684     - <code>b->flags</code>
685         - to indicate multi-segment pkts (VLIB_BUFFER_NEXT_PRESENT), etc.
686
687     <em>Next Nodes:</em>
688     - Static arcs to: error-drop, ethernet-input,
689       ip4-input-no-checksum, ip6-input, mpls-gre-input
690     - per-interface redirection, controlled by
691       <code>xd->per_interface_next_index</code>
692 */
693
694 static uword
695 dpdk_input (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * f)
696 {
697   dpdk_main_t *dm = &dpdk_main;
698   dpdk_device_t *xd;
699   uword n_rx_packets = 0;
700   dpdk_device_and_queue_t *dq;
701   u32 cpu_index = os_get_cpu_number ();
702
703   /*
704    * Poll all devices on this cpu for input/interrupts.
705    */
706   /* *INDENT-OFF* */
707   vec_foreach (dq, dm->devices_by_cpu[cpu_index])
708     {
709       xd = vec_elt_at_index(dm->devices, dq->device);
710       ASSERT(dq->queue_id == 0);
711       n_rx_packets += dpdk_device_input (dm, xd, node, cpu_index, 0, 0);
712     }
713   /* *INDENT-ON* */
714
715   poll_rate_limit (dm);
716
717   return n_rx_packets;
718 }
719
720 uword
721 dpdk_input_rss (vlib_main_t * vm,
722                 vlib_node_runtime_t * node, vlib_frame_t * f)
723 {
724   dpdk_main_t *dm = &dpdk_main;
725   dpdk_device_t *xd;
726   uword n_rx_packets = 0;
727   dpdk_device_and_queue_t *dq;
728   u32 cpu_index = os_get_cpu_number ();
729
730   /*
731    * Poll all devices on this cpu for input/interrupts.
732    */
733   /* *INDENT-OFF* */
734   vec_foreach (dq, dm->devices_by_cpu[cpu_index])
735     {
736       xd = vec_elt_at_index(dm->devices, dq->device);
737       n_rx_packets += dpdk_device_input (dm, xd, node, cpu_index, dq->queue_id, 0);
738     }
739   /* *INDENT-ON* */
740
741   poll_rate_limit (dm);
742
743   return n_rx_packets;
744 }
745
746 uword
747 dpdk_input_efd (vlib_main_t * vm,
748                 vlib_node_runtime_t * node, vlib_frame_t * f)
749 {
750   dpdk_main_t *dm = &dpdk_main;
751   dpdk_device_t *xd;
752   uword n_rx_packets = 0;
753   dpdk_device_and_queue_t *dq;
754   u32 cpu_index = os_get_cpu_number ();
755
756   /*
757    * Poll all devices on this cpu for input/interrupts.
758    */
759   /* *INDENT-OFF* */
760   vec_foreach (dq, dm->devices_by_cpu[cpu_index])
761     {
762       xd = vec_elt_at_index(dm->devices, dq->device);
763       n_rx_packets += dpdk_device_input (dm, xd, node, cpu_index, dq->queue_id, 1);
764     }
765   /* *INDENT-ON* */
766
767   poll_rate_limit (dm);
768
769   return n_rx_packets;
770 }
771
772 /* *INDENT-OFF* */
773 VLIB_REGISTER_NODE (dpdk_input_node) = {
774   .function = dpdk_input,
775   .type = VLIB_NODE_TYPE_INPUT,
776   .name = "dpdk-input",
777
778   /* Will be enabled if/when hardware is detected. */
779   .state = VLIB_NODE_STATE_DISABLED,
780
781   .format_buffer = format_ethernet_header_with_length,
782   .format_trace = format_dpdk_rx_dma_trace,
783
784   .n_errors = DPDK_N_ERROR,
785   .error_strings = dpdk_error_strings,
786
787   .n_next_nodes = DPDK_RX_N_NEXT,
788   .next_nodes = {
789     [DPDK_RX_NEXT_DROP] = "error-drop",
790     [DPDK_RX_NEXT_ETHERNET_INPUT] = "ethernet-input",
791     [DPDK_RX_NEXT_IP4_INPUT] = "ip4-input-no-checksum",
792     [DPDK_RX_NEXT_IP6_INPUT] = "ip6-input",
793     [DPDK_RX_NEXT_MPLS_INPUT] = "mpls-gre-input",
794   },
795 };
796
797
798 /* handle dpdk_input_rss alternative function */
799 VLIB_NODE_FUNCTION_MULTIARCH_CLONE(dpdk_input)
800 VLIB_NODE_FUNCTION_MULTIARCH_CLONE(dpdk_input_rss)
801 VLIB_NODE_FUNCTION_MULTIARCH_CLONE(dpdk_input_efd)
802
803 /* this macro defines dpdk_input_rss_multiarch_select() */
804 CLIB_MULTIARCH_SELECT_FN(dpdk_input);
805 CLIB_MULTIARCH_SELECT_FN(dpdk_input_rss);
806 CLIB_MULTIARCH_SELECT_FN(dpdk_input_efd);
807 /* *INDENT-ON* */
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 }
878
879 /*
880  * fd.io coding-style-patch-verification: ON
881  *
882  * Local Variables:
883  * eval: (c-set-style "gnu")
884  * End:
885  */