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