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