dpdk: improve checksum computation
[vpp.git] / src / plugins / dpdk / device / 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 <dpdk/buffer.h>
23 #include <dpdk/device/dpdk.h>
24 #include <vnet/classify/vnet_classify.h>
25 #include <vnet/mpls/packet.h>
26 #include <vnet/devices/devices.h>
27 #include <vnet/interface/rx_queue_funcs.h>
28 #include <vnet/feature/feature.h>
29
30 #include <dpdk/device/dpdk_priv.h>
31
32 static char *dpdk_error_strings[] = {
33 #define _(n,s) s,
34   foreach_dpdk_error
35 #undef _
36 };
37
38 /* make sure all flags we need are stored in lower 32 bits */
39 STATIC_ASSERT ((u64) (PKT_RX_IP_CKSUM_BAD | PKT_RX_L4_CKSUM_BAD | PKT_RX_FDIR |
40                       PKT_RX_LRO) < (1ULL << 32),
41                "dpdk flags not in lower word, fix needed");
42
43 STATIC_ASSERT (PKT_RX_L4_CKSUM_BAD == (1ULL << 3),
44                "bit number of PKT_RX_L4_CKSUM_BAD is no longer 3!");
45
46 static_always_inline uword
47 dpdk_process_subseq_segs (vlib_main_t * vm, vlib_buffer_t * b,
48                           struct rte_mbuf *mb, vlib_buffer_t * bt)
49 {
50   u8 nb_seg = 1;
51   struct rte_mbuf *mb_seg = 0;
52   vlib_buffer_t *b_seg, *b_chain = 0;
53   mb_seg = mb->next;
54   b_chain = b;
55
56   if (mb->nb_segs < 2)
57     return 0;
58
59   b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
60   b->total_length_not_including_first_buffer = 0;
61
62   while (nb_seg < mb->nb_segs)
63     {
64       ASSERT (mb_seg != 0);
65
66       b_seg = vlib_buffer_from_rte_mbuf (mb_seg);
67       vlib_buffer_copy_template (b_seg, bt);
68
69       /*
70        * The driver (e.g. virtio) may not put the packet data at the start
71        * of the segment, so don't assume b_seg->current_data == 0 is correct.
72        */
73       b_seg->current_data =
74         (mb_seg->buf_addr + mb_seg->data_off) - (void *) b_seg->data;
75
76       b_seg->current_length = mb_seg->data_len;
77       b->total_length_not_including_first_buffer += mb_seg->data_len;
78
79       b_chain->flags |= VLIB_BUFFER_NEXT_PRESENT;
80       b_chain->next_buffer = vlib_get_buffer_index (vm, b_seg);
81
82       b_chain = b_seg;
83       mb_seg = mb_seg->next;
84       nb_seg++;
85     }
86   return b->total_length_not_including_first_buffer;
87 }
88
89 static_always_inline void
90 dpdk_prefetch_mbuf_x4 (struct rte_mbuf *mb[])
91 {
92   clib_prefetch_load (mb[0]);
93   clib_prefetch_load (mb[1]);
94   clib_prefetch_load (mb[2]);
95   clib_prefetch_load (mb[3]);
96 }
97
98 static_always_inline void
99 dpdk_prefetch_buffer_x4 (struct rte_mbuf *mb[])
100 {
101   vlib_buffer_t *b;
102   b = vlib_buffer_from_rte_mbuf (mb[0]);
103   clib_prefetch_store (b);
104   b = vlib_buffer_from_rte_mbuf (mb[1]);
105   clib_prefetch_store (b);
106   b = vlib_buffer_from_rte_mbuf (mb[2]);
107   clib_prefetch_store (b);
108   b = vlib_buffer_from_rte_mbuf (mb[3]);
109   clib_prefetch_store (b);
110 }
111
112 /** \brief Main DPDK input node
113     @node dpdk-input
114
115     This is the main DPDK input node: across each assigned interface,
116     call rte_eth_rx_burst(...) or similar to obtain a vector of
117     packets to process. Derive @c vlib_buffer_t metadata from
118     <code>struct rte_mbuf</code> metadata,
119     Depending on the resulting metadata: adjust <code>b->current_data,
120     b->current_length </code> and dispatch directly to
121     ip4-input-no-checksum, or ip6-input. Trace the packet if required.
122
123     @param vm   vlib_main_t corresponding to the current thread
124     @param node vlib_node_runtime_t
125     @param f    vlib_frame_t input-node, not used.
126
127     @par Graph mechanics: buffer metadata, next index usage
128
129     @em Uses:
130     - <code>struct rte_mbuf mb->ol_flags</code>
131         - PKT_RX_IP_CKSUM_BAD
132
133     @em Sets:
134     - <code>b->error</code> if the packet is to be dropped immediately
135     - <code>b->current_data, b->current_length</code>
136         - adjusted as needed to skip the L2 header in  direct-dispatch cases
137     - <code>vnet_buffer(b)->sw_if_index[VLIB_RX]</code>
138         - rx interface sw_if_index
139     - <code>vnet_buffer(b)->sw_if_index[VLIB_TX] = ~0</code>
140         - required by ipX-lookup
141     - <code>b->flags</code>
142         - to indicate multi-segment pkts (VLIB_BUFFER_NEXT_PRESENT), etc.
143
144     <em>Next Nodes:</em>
145     - Static arcs to: error-drop, ethernet-input,
146       ip4-input-no-checksum, ip6-input, mpls-input
147     - per-interface redirection, controlled by
148       <code>xd->per_interface_next_index</code>
149 */
150
151 static_always_inline u32
152 dpdk_ol_flags_extract (struct rte_mbuf **mb, u32 *flags, int count)
153 {
154   u32 rv = 0;
155   int i;
156   for (i = 0; i < count; i++)
157     {
158       /* all flags we are interested in are in lower 8 bits but
159          that might change */
160       flags[i] = (u32) mb[i]->ol_flags;
161       rv |= flags[i];
162     }
163   return rv;
164 }
165
166 static_always_inline uword
167 dpdk_process_rx_burst (vlib_main_t *vm, dpdk_per_thread_data_t *ptd,
168                        uword n_rx_packets, int maybe_multiseg, u32 *or_flagsp)
169 {
170   u32 n_left = n_rx_packets;
171   vlib_buffer_t *b[4];
172   struct rte_mbuf **mb = ptd->mbufs;
173   uword n_bytes = 0;
174   u32 *flags, or_flags = 0;
175   vlib_buffer_t bt;
176
177   mb = ptd->mbufs;
178   flags = ptd->flags;
179
180   /* copy template into local variable - will save per packet load */
181   vlib_buffer_copy_template (&bt, &ptd->buffer_template);
182   while (n_left >= 8)
183     {
184       dpdk_prefetch_buffer_x4 (mb + 4);
185
186       b[0] = vlib_buffer_from_rte_mbuf (mb[0]);
187       b[1] = vlib_buffer_from_rte_mbuf (mb[1]);
188       b[2] = vlib_buffer_from_rte_mbuf (mb[2]);
189       b[3] = vlib_buffer_from_rte_mbuf (mb[3]);
190
191       vlib_buffer_copy_template (b[0], &bt);
192       vlib_buffer_copy_template (b[1], &bt);
193       vlib_buffer_copy_template (b[2], &bt);
194       vlib_buffer_copy_template (b[3], &bt);
195
196       dpdk_prefetch_mbuf_x4 (mb + 4);
197
198       or_flags |= dpdk_ol_flags_extract (mb, flags, 4);
199       flags += 4;
200
201       b[0]->current_data = mb[0]->data_off - RTE_PKTMBUF_HEADROOM;
202       n_bytes += b[0]->current_length = mb[0]->data_len;
203
204       b[1]->current_data = mb[1]->data_off - RTE_PKTMBUF_HEADROOM;
205       n_bytes += b[1]->current_length = mb[1]->data_len;
206
207       b[2]->current_data = mb[2]->data_off - RTE_PKTMBUF_HEADROOM;
208       n_bytes += b[2]->current_length = mb[2]->data_len;
209
210       b[3]->current_data = mb[3]->data_off - RTE_PKTMBUF_HEADROOM;
211       n_bytes += b[3]->current_length = mb[3]->data_len;
212
213       if (maybe_multiseg)
214         {
215           n_bytes += dpdk_process_subseq_segs (vm, b[0], mb[0], &bt);
216           n_bytes += dpdk_process_subseq_segs (vm, b[1], mb[1], &bt);
217           n_bytes += dpdk_process_subseq_segs (vm, b[2], mb[2], &bt);
218           n_bytes += dpdk_process_subseq_segs (vm, b[3], mb[3], &bt);
219         }
220
221       /* next */
222       mb += 4;
223       n_left -= 4;
224     }
225
226   while (n_left)
227     {
228       b[0] = vlib_buffer_from_rte_mbuf (mb[0]);
229       vlib_buffer_copy_template (b[0], &bt);
230       or_flags |= dpdk_ol_flags_extract (mb, flags, 1);
231       flags += 1;
232
233       b[0]->current_data = mb[0]->data_off - RTE_PKTMBUF_HEADROOM;
234       n_bytes += b[0]->current_length = mb[0]->data_len;
235
236       if (maybe_multiseg)
237         n_bytes += dpdk_process_subseq_segs (vm, b[0], mb[0], &bt);
238
239       /* next */
240       mb += 1;
241       n_left -= 1;
242     }
243
244   *or_flagsp = or_flags;
245   return n_bytes;
246 }
247
248 static_always_inline void
249 dpdk_process_flow_offload (dpdk_device_t * xd, dpdk_per_thread_data_t * ptd,
250                            uword n_rx_packets)
251 {
252   uword n;
253   dpdk_flow_lookup_entry_t *fle;
254   vlib_buffer_t *b0;
255
256   /* TODO prefetch and quad-loop */
257   for (n = 0; n < n_rx_packets; n++)
258     {
259       if ((ptd->flags[n] & PKT_RX_FDIR_ID) == 0)
260         continue;
261
262       fle = pool_elt_at_index (xd->flow_lookup_entries,
263                                ptd->mbufs[n]->hash.fdir.hi);
264
265       if (fle->next_index != (u16) ~ 0)
266         ptd->next[n] = fle->next_index;
267
268       if (fle->flow_id != ~0)
269         {
270           b0 = vlib_buffer_from_rte_mbuf (ptd->mbufs[n]);
271           b0->flow_id = fle->flow_id;
272         }
273
274       if (fle->buffer_advance != ~0)
275         {
276           b0 = vlib_buffer_from_rte_mbuf (ptd->mbufs[n]);
277           vlib_buffer_advance (b0, fle->buffer_advance);
278         }
279     }
280 }
281
282 static_always_inline u16
283 dpdk_lro_find_l4_hdr_sz (vlib_buffer_t *b)
284 {
285   u16 l4_hdr_sz = 0;
286   u16 current_offset = 0;
287   ethernet_header_t *e;
288   tcp_header_t *tcp;
289   u8 *data = vlib_buffer_get_current (b);
290   u16 ethertype;
291   e = (void *) data;
292   current_offset += sizeof (e[0]);
293   ethertype = clib_net_to_host_u16 (e->type);
294   if (ethernet_frame_is_tagged (ethertype))
295     {
296       ethernet_vlan_header_t *vlan = (ethernet_vlan_header_t *) (e + 1);
297       ethertype = clib_net_to_host_u16 (vlan->type);
298       current_offset += sizeof (*vlan);
299       if (ethertype == ETHERNET_TYPE_VLAN)
300         {
301           vlan++;
302           current_offset += sizeof (*vlan);
303           ethertype = clib_net_to_host_u16 (vlan->type);
304         }
305     }
306   data += current_offset;
307   if (ethertype == ETHERNET_TYPE_IP4)
308     {
309       data += sizeof (ip4_header_t);
310       tcp = (void *) data;
311       l4_hdr_sz = tcp_header_bytes (tcp);
312     }
313   else
314     {
315       /* FIXME: extension headers...*/
316       data += sizeof (ip6_header_t);
317       tcp = (void *) data;
318       l4_hdr_sz = tcp_header_bytes (tcp);
319     }
320   return l4_hdr_sz;
321 }
322
323 static_always_inline void
324 dpdk_process_lro_offload (dpdk_device_t *xd, dpdk_per_thread_data_t *ptd,
325                           uword n_rx_packets)
326 {
327   uword n;
328   vlib_buffer_t *b0;
329   for (n = 0; n < n_rx_packets; n++)
330     {
331       b0 = vlib_buffer_from_rte_mbuf (ptd->mbufs[n]);
332       if (ptd->flags[n] & PKT_RX_LRO)
333         {
334           b0->flags |= VNET_BUFFER_F_GSO;
335           vnet_buffer2 (b0)->gso_size = ptd->mbufs[n]->tso_segsz;
336           vnet_buffer2 (b0)->gso_l4_hdr_sz = dpdk_lro_find_l4_hdr_sz (b0);
337         }
338     }
339 }
340
341 static_always_inline u32
342 dpdk_device_input (vlib_main_t * vm, dpdk_main_t * dm, dpdk_device_t * xd,
343                    vlib_node_runtime_t * node, u32 thread_index, u16 queue_id)
344 {
345   uword n_rx_packets = 0, n_rx_bytes;
346   dpdk_rx_queue_t *rxq = vec_elt_at_index (xd->rx_queues, queue_id);
347   u32 n_left, n_trace;
348   u32 *buffers;
349   u32 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
350   struct rte_mbuf **mb;
351   vlib_buffer_t *b0;
352   u16 *next;
353   u32 or_flags;
354   u32 n;
355   int single_next = 0;
356
357   dpdk_per_thread_data_t *ptd = vec_elt_at_index (dm->per_thread_data,
358                                                   thread_index);
359   vlib_buffer_t *bt = &ptd->buffer_template;
360
361   if ((xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP) == 0)
362     return 0;
363
364   /* get up to DPDK_RX_BURST_SZ buffers from PMD */
365   while (n_rx_packets < DPDK_RX_BURST_SZ)
366     {
367       n = rte_eth_rx_burst (xd->port_id, queue_id,
368                             ptd->mbufs + n_rx_packets,
369                             DPDK_RX_BURST_SZ - n_rx_packets);
370       n_rx_packets += n;
371
372       if (n < 32)
373         break;
374     }
375
376   if (n_rx_packets == 0)
377     return 0;
378
379   /* Update buffer template */
380   vnet_buffer (bt)->sw_if_index[VLIB_RX] = xd->sw_if_index;
381   bt->error = node->errors[DPDK_ERROR_NONE];
382   bt->flags = xd->buffer_flags;
383   /* as DPDK is allocating empty buffers from mempool provided before interface
384      start for each queue, it is safe to store this in the template */
385   bt->buffer_pool_index = rxq->buffer_pool_index;
386   bt->ref_count = 1;
387   vnet_buffer (bt)->feature_arc_index = 0;
388   bt->current_config_index = 0;
389
390   /* receive burst of packets from DPDK PMD */
391   if (PREDICT_FALSE (xd->per_interface_next_index != ~0))
392     next_index = xd->per_interface_next_index;
393
394   /* as all packets belong to the same interface feature arc lookup
395      can be don once and result stored in the buffer template */
396   if (PREDICT_FALSE (vnet_device_input_have_features (xd->sw_if_index)))
397     vnet_feature_start_device_input_x1 (xd->sw_if_index, &next_index, bt);
398
399   if (xd->flags & DPDK_DEVICE_FLAG_MAYBE_MULTISEG)
400     n_rx_bytes = dpdk_process_rx_burst (vm, ptd, n_rx_packets, 1, &or_flags);
401   else
402     n_rx_bytes = dpdk_process_rx_burst (vm, ptd, n_rx_packets, 0, &or_flags);
403
404   if (PREDICT_FALSE ((or_flags & PKT_RX_LRO)))
405     dpdk_process_lro_offload (xd, ptd, n_rx_packets);
406
407   if (PREDICT_FALSE ((or_flags & PKT_RX_L4_CKSUM_BAD) &&
408                      (xd->buffer_flags & VNET_BUFFER_F_L4_CHECKSUM_CORRECT)))
409     {
410       for (n = 0; n < n_rx_packets; n++)
411         {
412           /* Check and reset VNET_BUFFER_F_L4_CHECKSUM_CORRECT flag
413              if PKT_RX_L4_CKSUM_BAD is set.
414              The magic num 3 is the bit number of PKT_RX_L4_CKSUM_BAD
415              which is defined in DPDK.
416              Have made a STATIC_ASSERT in this file to ensure this.
417            */
418           b0 = vlib_buffer_from_rte_mbuf (ptd->mbufs[n]);
419           b0->flags ^= (ptd->flags[n] & PKT_RX_L4_CKSUM_BAD)
420                        << (VNET_BUFFER_F_LOG2_L4_CHECKSUM_CORRECT - 3);
421         }
422     }
423
424   if (PREDICT_FALSE (or_flags & PKT_RX_FDIR))
425     {
426       /* some packets will need to go to different next nodes */
427       for (n = 0; n < n_rx_packets; n++)
428         ptd->next[n] = next_index;
429
430       /* flow offload - process if rx flow offload enabled and at least one
431          packet is marked */
432       if (PREDICT_FALSE ((xd->flags & DPDK_DEVICE_FLAG_RX_FLOW_OFFLOAD) &&
433                          (or_flags & PKT_RX_FDIR)))
434         dpdk_process_flow_offload (xd, ptd, n_rx_packets);
435
436       /* enqueue buffers to the next node */
437       vlib_get_buffer_indices_with_offset (vm, (void **) ptd->mbufs,
438                                            ptd->buffers, n_rx_packets,
439                                            sizeof (struct rte_mbuf));
440
441       vlib_buffer_enqueue_to_next (vm, node, ptd->buffers, ptd->next,
442                                    n_rx_packets);
443     }
444   else
445     {
446       u32 *to_next, n_left_to_next;
447
448       vlib_get_new_next_frame (vm, node, next_index, to_next, n_left_to_next);
449       vlib_get_buffer_indices_with_offset (vm, (void **) ptd->mbufs, to_next,
450                                            n_rx_packets,
451                                            sizeof (struct rte_mbuf));
452
453       if (PREDICT_TRUE (next_index == VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT))
454         {
455           vlib_next_frame_t *nf;
456           vlib_frame_t *f;
457           ethernet_input_frame_t *ef;
458           nf = vlib_node_runtime_get_next_frame (vm, node, next_index);
459           f = vlib_get_frame (vm, nf->frame);
460           f->flags = ETH_INPUT_FRAME_F_SINGLE_SW_IF_IDX;
461
462           ef = vlib_frame_scalar_args (f);
463           ef->sw_if_index = xd->sw_if_index;
464           ef->hw_if_index = xd->hw_if_index;
465
466           /* if PMD supports ip4 checksum check and there are no packets
467              marked as ip4 checksum bad we can notify ethernet input so it
468              can send pacets to ip4-input-no-checksum node */
469           if (xd->flags & DPDK_DEVICE_FLAG_RX_IP4_CKSUM &&
470               (or_flags & PKT_RX_IP_CKSUM_BAD) == 0)
471             f->flags |= ETH_INPUT_FRAME_F_IP4_CKSUM_OK;
472           vlib_frame_no_append (f);
473         }
474       n_left_to_next -= n_rx_packets;
475       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
476       single_next = 1;
477     }
478
479   /* packet trace if enabled */
480   if (PREDICT_FALSE ((n_trace = vlib_get_trace_count (vm, node))))
481     {
482       if (single_next)
483         vlib_get_buffer_indices_with_offset (vm, (void **) ptd->mbufs,
484                                              ptd->buffers, n_rx_packets,
485                                              sizeof (struct rte_mbuf));
486
487       n_left = n_rx_packets;
488       buffers = ptd->buffers;
489       mb = ptd->mbufs;
490       next = ptd->next;
491
492       while (n_trace && n_left)
493         {
494           b0 = vlib_get_buffer (vm, buffers[0]);
495           if (single_next == 0)
496             next_index = next[0];
497
498           if (PREDICT_TRUE
499               (vlib_trace_buffer
500                (vm, node, next_index, b0, /* follow_chain */ 0)))
501             {
502
503               dpdk_rx_trace_t *t0 =
504                 vlib_add_trace (vm, node, b0, sizeof t0[0]);
505               t0->queue_index = queue_id;
506               t0->device_index = xd->device_index;
507               t0->buffer_index = vlib_get_buffer_index (vm, b0);
508
509               clib_memcpy_fast (&t0->mb, mb[0], sizeof t0->mb);
510               clib_memcpy_fast (&t0->buffer, b0,
511                                 sizeof b0[0] - sizeof b0->pre_data);
512               clib_memcpy_fast (t0->buffer.pre_data, b0->data,
513                                 sizeof t0->buffer.pre_data);
514               clib_memcpy_fast (&t0->data, mb[0]->buf_addr + mb[0]->data_off,
515                                 sizeof t0->data);
516               n_trace--;
517             }
518
519           n_left--;
520           buffers++;
521           mb++;
522           next++;
523         }
524       vlib_set_trace_count (vm, node, n_trace);
525     }
526
527   vlib_increment_combined_counter
528     (vnet_get_main ()->interface_main.combined_sw_if_counters
529      + VNET_INTERFACE_COUNTER_RX, thread_index, xd->sw_if_index,
530      n_rx_packets, n_rx_bytes);
531
532   vnet_device_increment_rx_packets (thread_index, n_rx_packets);
533
534   return n_rx_packets;
535 }
536
537 VLIB_NODE_FN (dpdk_input_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
538                                 vlib_frame_t * f)
539 {
540   dpdk_main_t *dm = &dpdk_main;
541   dpdk_device_t *xd;
542   uword n_rx_packets = 0;
543   vnet_hw_if_rxq_poll_vector_t *pv;
544   u32 thread_index = node->thread_index;
545
546   /*
547    * Poll all devices on this cpu for input/interrupts.
548    */
549
550   pv = vnet_hw_if_get_rxq_poll_vector (vm, node);
551
552   for (int i = 0; i < vec_len (pv); i++)
553     {
554       xd = vec_elt_at_index (dm->devices, pv[i].dev_instance);
555       n_rx_packets +=
556         dpdk_device_input (vm, dm, xd, node, thread_index, pv[i].queue_id);
557     }
558   return n_rx_packets;
559 }
560
561 /* *INDENT-OFF* */
562 VLIB_REGISTER_NODE (dpdk_input_node) = {
563   .type = VLIB_NODE_TYPE_INPUT,
564   .name = "dpdk-input",
565   .sibling_of = "device-input",
566   .flags = VLIB_NODE_FLAG_TRACE_SUPPORTED,
567
568   /* Will be enabled if/when hardware is detected. */
569   .state = VLIB_NODE_STATE_DISABLED,
570
571   .format_buffer = format_ethernet_header_with_length,
572   .format_trace = format_dpdk_rx_trace,
573
574   .n_errors = DPDK_N_ERROR,
575   .error_strings = dpdk_error_strings,
576 };
577 /* *INDENT-ON* */
578
579 /*
580  * fd.io coding-style-patch-verification: ON
581  *
582  * Local Variables:
583  * eval: (c-set-style "gnu")
584  * End:
585  */