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