dpdk: code preparation for bumping to DPDK 22.11
[vpp.git] / src / plugins / dpdk / device / common.c
1 /*
2  * Copyright (c) 2017 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
16 #include <vnet/vnet.h>
17 #include <vppinfra/vec.h>
18 #include <vppinfra/format.h>
19 #include <vppinfra/file.h>
20 #include <vlib/unix/unix.h>
21 #include <assert.h>
22
23 #include <vnet/ip/ip.h>
24 #include <vnet/ethernet/ethernet.h>
25 #include <vnet/ethernet/arp_packet.h>
26 #include <vnet/interface/rx_queue_funcs.h>
27 #include <dpdk/buffer.h>
28 #include <dpdk/device/dpdk.h>
29 #include <dpdk/device/dpdk_priv.h>
30 #include <vppinfra/error.h>
31
32 /* DPDK TX offload to vnet hw interface caps mapppings */
33 static struct
34 {
35   u64 offload;
36   vnet_hw_if_caps_t caps;
37 } tx_off_caps_map[] = {
38   { RTE_ETH_TX_OFFLOAD_IPV4_CKSUM, VNET_HW_IF_CAP_TX_IP4_CKSUM },
39   { RTE_ETH_TX_OFFLOAD_TCP_CKSUM, VNET_HW_IF_CAP_TX_TCP_CKSUM },
40   { RTE_ETH_TX_OFFLOAD_UDP_CKSUM, VNET_HW_IF_CAP_TX_UDP_CKSUM },
41   { RTE_ETH_TX_OFFLOAD_OUTER_IPV4_CKSUM, VNET_HW_IF_CAP_TX_IP4_OUTER_CKSUM },
42   { RTE_ETH_TX_OFFLOAD_OUTER_UDP_CKSUM, VNET_HW_IF_CAP_TX_UDP_OUTER_CKSUM },
43   { RTE_ETH_TX_OFFLOAD_TCP_TSO, VNET_HW_IF_CAP_TCP_GSO },
44   { RTE_ETH_TX_OFFLOAD_VXLAN_TNL_TSO, VNET_HW_IF_CAP_VXLAN_TNL_GSO }
45 };
46
47 void
48 dpdk_device_error (dpdk_device_t * xd, char *str, int rv)
49 {
50   dpdk_log_err ("Interface %U error %d: %s",
51                 format_dpdk_device_name, xd->port_id, rv, rte_strerror (rv));
52   xd->errors = clib_error_return (xd->errors, "%s[port:%d, errno:%d]: %s",
53                                   str, xd->port_id, rv, rte_strerror (rv));
54 }
55
56 void
57 dpdk_device_setup (dpdk_device_t * xd)
58 {
59   vlib_main_t *vm = vlib_get_main ();
60   vnet_main_t *vnm = vnet_get_main ();
61   vnet_sw_interface_t *sw = vnet_get_sw_interface (vnm, xd->sw_if_index);
62   vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, xd->hw_if_index);
63   u16 buf_sz = vlib_buffer_get_default_data_size (vm);
64   vnet_hw_if_caps_change_t caps = {};
65   struct rte_eth_dev_info dev_info;
66   struct rte_eth_conf conf = {};
67   u64 rxo, txo;
68   u32 max_frame_size;
69   int rv;
70   int j;
71
72   ASSERT (vlib_get_thread_index () == 0);
73
74   clib_error_free (xd->errors);
75   sw->flags &= ~VNET_SW_INTERFACE_FLAG_ERROR;
76
77   if (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP)
78     {
79       vnet_hw_interface_set_flags (vnm, xd->hw_if_index, 0);
80       dpdk_device_stop (xd);
81     }
82
83   rte_eth_dev_info_get (xd->port_id, &dev_info);
84
85   dpdk_log_debug ("[%u] configuring device %U", xd->port_id,
86                   format_dpdk_rte_device, dev_info.device);
87
88   /* create rx and tx offload wishlist */
89   rxo = RTE_ETH_RX_OFFLOAD_IPV4_CKSUM;
90   txo = 0;
91
92   if (xd->conf.enable_tcp_udp_checksum)
93     rxo |= RTE_ETH_RX_OFFLOAD_UDP_CKSUM | RTE_ETH_RX_OFFLOAD_TCP_CKSUM;
94
95   if (xd->conf.disable_tx_checksum_offload == 0 &&
96       xd->conf.enable_outer_checksum_offload)
97     txo |=
98       RTE_ETH_TX_OFFLOAD_OUTER_IPV4_CKSUM | RTE_ETH_TX_OFFLOAD_OUTER_UDP_CKSUM;
99
100   if (xd->conf.disable_tx_checksum_offload == 0)
101     txo |= RTE_ETH_TX_OFFLOAD_IPV4_CKSUM | RTE_ETH_TX_OFFLOAD_TCP_CKSUM |
102            RTE_ETH_TX_OFFLOAD_UDP_CKSUM;
103
104   if (xd->conf.disable_multi_seg == 0)
105     {
106       txo |= RTE_ETH_TX_OFFLOAD_MULTI_SEGS;
107       rxo |= RTE_ETH_RX_OFFLOAD_SCATTER;
108 #if RTE_VERSION < RTE_VERSION_NUM(21, 11, 0, 0)
109       rxo |= DEV_RX_OFFLOAD_JUMBO_FRAME;
110 #endif
111     }
112
113   if (xd->conf.enable_lro)
114     rxo |= RTE_ETH_RX_OFFLOAD_TCP_LRO;
115
116   /* per-device offload config */
117   if (xd->conf.enable_tso)
118     txo |= RTE_ETH_TX_OFFLOAD_TCP_CKSUM | RTE_ETH_TX_OFFLOAD_TCP_TSO |
119            RTE_ETH_TX_OFFLOAD_VXLAN_TNL_TSO;
120
121   if (xd->conf.disable_rx_scatter)
122     rxo &= ~RTE_ETH_RX_OFFLOAD_SCATTER;
123
124   /* mask unsupported offloads */
125   rxo &= dev_info.rx_offload_capa;
126   txo &= dev_info.tx_offload_capa;
127
128   dpdk_log_debug ("[%u] Supported RX offloads: %U", xd->port_id,
129                   format_dpdk_rx_offload_caps, dev_info.rx_offload_capa);
130   dpdk_log_debug ("[%u] Configured RX offloads: %U", xd->port_id,
131                   format_dpdk_rx_offload_caps, rxo);
132   dpdk_log_debug ("[%u] Supported TX offloads: %U", xd->port_id,
133                   format_dpdk_tx_offload_caps, dev_info.tx_offload_capa);
134   dpdk_log_debug ("[%u] Configured TX offloads: %U", xd->port_id,
135                   format_dpdk_tx_offload_caps, txo);
136
137   /* finalize configuration */
138   conf.rxmode.offloads = rxo;
139   conf.txmode.offloads = txo;
140   if (rxo & RTE_ETH_RX_OFFLOAD_TCP_LRO)
141     conf.rxmode.max_lro_pkt_size = xd->conf.max_lro_pkt_size;
142
143   if (xd->conf.enable_lsc_int)
144     conf.intr_conf.lsc = 1;
145   if (xd->conf.enable_rxq_int)
146     conf.intr_conf.rxq = 1;
147
148   conf.rxmode.mq_mode = RTE_ETH_MQ_RX_NONE;
149   if (xd->conf.n_rx_queues > 1)
150     {
151       if (xd->conf.disable_rss == 0)
152         {
153           conf.rxmode.mq_mode = RTE_ETH_MQ_RX_RSS;
154           conf.rx_adv_conf.rss_conf.rss_hf = xd->conf.rss_hf;
155         }
156     }
157
158 #if RTE_VERSION < RTE_VERSION_NUM(21, 11, 0, 0)
159   if (rxo & DEV_RX_OFFLOAD_JUMBO_FRAME)
160     {
161       conf.rxmode.max_rx_pkt_len = dev_info.max_rx_pktlen;
162       xd->max_supported_frame_size = dev_info.max_rx_pktlen;
163       mtu = xd->max_supported_frame_size - xd->driver_frame_overhead;
164     }
165   else
166     {
167       xd->max_supported_frame_size =
168         clib_min (1500 + xd->driver_frame_overhead, buf_sz);
169     }
170 #else
171   if (xd->conf.disable_multi_seg)
172     xd->max_supported_frame_size = clib_min (dev_info.max_rx_pktlen, buf_sz);
173   else
174     xd->max_supported_frame_size = dev_info.max_rx_pktlen;
175 #endif
176
177   max_frame_size = clib_min (xd->max_supported_frame_size,
178                              ethernet_main.default_mtu + hi->frame_overhead);
179
180 #if RTE_VERSION >= RTE_VERSION_NUM(21, 11, 0, 0)
181   conf.rxmode.mtu = max_frame_size - xd->driver_frame_overhead;
182 #endif
183
184 retry:
185   rv = rte_eth_dev_configure (xd->port_id, xd->conf.n_rx_queues,
186                               xd->conf.n_tx_queues, &conf);
187   if (rv < 0 && conf.intr_conf.rxq)
188     {
189       conf.intr_conf.rxq = 0;
190       goto retry;
191     }
192
193 #if RTE_VERSION < RTE_VERSION_NUM(21, 11, 0, 0)
194   rte_eth_dev_set_mtu (xd->port_id,
195                        max_frame_size - xd->driver_frame_overhead);
196 #endif
197
198   hi->max_frame_size = 0;
199   vnet_hw_interface_set_max_frame_size (vnm, xd->hw_if_index, max_frame_size);
200   dpdk_log_debug ("[%u] max_frame_size %u max max_frame_size %u "
201                   "driver_frame_overhead %u",
202                   xd->port_id, hi->max_frame_size,
203                   xd->max_supported_frame_size, xd->driver_frame_overhead);
204
205   vec_validate_aligned (xd->tx_queues, xd->conf.n_tx_queues - 1,
206                         CLIB_CACHE_LINE_BYTES);
207   for (j = 0; j < xd->conf.n_tx_queues; j++)
208     {
209       rv = rte_eth_tx_queue_setup (xd->port_id, j, xd->conf.n_tx_desc,
210                                    xd->cpu_socket, 0);
211
212       /* retry with any other CPU socket */
213       if (rv < 0)
214         rv = rte_eth_tx_queue_setup (xd->port_id, j, xd->conf.n_tx_desc,
215                                      SOCKET_ID_ANY, 0);
216       if (rv < 0)
217         dpdk_device_error (xd, "rte_eth_tx_queue_setup", rv);
218
219       clib_spinlock_init (&vec_elt (xd->tx_queues, j).lock);
220     }
221
222   vec_validate_aligned (xd->rx_queues, xd->conf.n_rx_queues - 1,
223                         CLIB_CACHE_LINE_BYTES);
224
225   for (j = 0; j < xd->conf.n_rx_queues; j++)
226     {
227       dpdk_rx_queue_t *rxq = vec_elt_at_index (xd->rx_queues, j);
228       u8 bpidx = vlib_buffer_pool_get_default_for_numa (
229         vm, vnet_hw_if_get_rx_queue_numa_node (vnm, rxq->queue_index));
230       vlib_buffer_pool_t *bp = vlib_get_buffer_pool (vm, bpidx);
231       struct rte_mempool *mp = dpdk_mempool_by_buffer_pool_index[bpidx];
232
233       rv = rte_eth_rx_queue_setup (xd->port_id, j, xd->conf.n_rx_desc,
234                                    xd->cpu_socket, 0, mp);
235
236       /* retry with any other CPU socket */
237       if (rv < 0)
238         rv = rte_eth_rx_queue_setup (xd->port_id, j, xd->conf.n_rx_desc,
239                                      SOCKET_ID_ANY, 0, mp);
240
241       rxq->buffer_pool_index = bp->index;
242
243       if (rv < 0)
244         dpdk_device_error (xd, "rte_eth_rx_queue_setup", rv);
245     }
246
247   if (vec_len (xd->errors))
248     goto error;
249
250   xd->buffer_flags =
251     (VLIB_BUFFER_TOTAL_LENGTH_VALID | VLIB_BUFFER_EXT_HDR_VALID);
252
253   if ((rxo & (RTE_ETH_RX_OFFLOAD_TCP_CKSUM | RTE_ETH_RX_OFFLOAD_UDP_CKSUM)) ==
254       (RTE_ETH_RX_OFFLOAD_TCP_CKSUM | RTE_ETH_RX_OFFLOAD_UDP_CKSUM))
255     xd->buffer_flags |=
256       (VNET_BUFFER_F_L4_CHECKSUM_COMPUTED | VNET_BUFFER_F_L4_CHECKSUM_CORRECT);
257
258   dpdk_device_flag_set (xd, DPDK_DEVICE_FLAG_RX_IP4_CKSUM,
259                         rxo & RTE_ETH_RX_OFFLOAD_IPV4_CKSUM);
260   dpdk_device_flag_set (xd, DPDK_DEVICE_FLAG_MAYBE_MULTISEG,
261                         rxo & RTE_ETH_RX_OFFLOAD_SCATTER);
262   dpdk_device_flag_set (
263     xd, DPDK_DEVICE_FLAG_TX_OFFLOAD,
264     (txo & (RTE_ETH_TX_OFFLOAD_TCP_CKSUM | RTE_ETH_TX_OFFLOAD_UDP_CKSUM)) ==
265       (RTE_ETH_TX_OFFLOAD_TCP_CKSUM | RTE_ETH_TX_OFFLOAD_UDP_CKSUM));
266
267   /* unconditionally set mac filtering cap */
268   caps.val = caps.mask = VNET_HW_IF_CAP_MAC_FILTER;
269
270   ethernet_set_flags (vnm, xd->hw_if_index,
271                       ETHERNET_INTERFACE_FLAG_DEFAULT_L3);
272
273   for (int i = 0; i < ARRAY_LEN (tx_off_caps_map); i++)
274     {
275       __typeof__ (tx_off_caps_map[0]) *v = tx_off_caps_map + i;
276       caps.mask |= v->caps;
277       if ((v->offload & txo) == v->offload)
278         caps.val |= v->caps;
279     }
280
281   vnet_hw_if_change_caps (vnm, xd->hw_if_index, &caps);
282   xd->enabled_rx_off = rxo;
283   xd->enabled_tx_off = txo;
284
285   if (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP)
286     dpdk_device_start (xd);
287
288   if (vec_len (xd->errors))
289     goto error;
290
291   return;
292
293 error:
294   xd->flags |= DPDK_DEVICE_FLAG_PMD_INIT_FAIL;
295   sw->flags |= VNET_SW_INTERFACE_FLAG_ERROR;
296 }
297
298 static clib_error_t *
299 dpdk_rx_read_ready (clib_file_t *uf)
300 {
301   vnet_main_t *vnm = vnet_get_main ();
302   dpdk_main_t *dm = &dpdk_main;
303   u32 qidx = uf->private_data;
304   vnet_hw_if_rx_queue_t *rxq = vnet_hw_if_get_rx_queue (vnm, qidx);
305   dpdk_device_t *xd = vec_elt_at_index (dm->devices, rxq->dev_instance);
306
307   u64 b;
308   CLIB_UNUSED (ssize_t size) = read (uf->file_descriptor, &b, sizeof (b));
309   if (rxq->mode != VNET_HW_IF_RX_MODE_POLLING)
310     {
311       vnet_hw_if_rx_queue_set_int_pending (vnm, uf->private_data);
312       rte_eth_dev_rx_intr_enable (xd->port_id, rxq->queue_id);
313     }
314
315   return 0;
316 }
317
318 static void
319 dpdk_setup_interrupts (dpdk_device_t *xd)
320 {
321   vnet_main_t *vnm = vnet_get_main ();
322   vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, xd->hw_if_index);
323   int int_mode = 0;
324   if (!hi)
325     return;
326
327   if (!xd->conf.enable_rxq_int)
328     return;
329
330   /* Probe for interrupt support */
331   if (rte_eth_dev_rx_intr_enable (xd->port_id, 0))
332     {
333       dpdk_log_info ("probe for interrupt mode for device %U. Failed.\n",
334                      format_dpdk_device_name, xd->port_id);
335     }
336   else
337     {
338       xd->flags |= DPDK_DEVICE_FLAG_INT_SUPPORTED;
339       if (!(xd->flags & DPDK_DEVICE_FLAG_INT_UNMASKABLE))
340         rte_eth_dev_rx_intr_disable (xd->port_id, 0);
341       dpdk_log_info ("Probe for interrupt mode for device %U. Success.\n",
342                      format_dpdk_device_name, xd->port_id);
343     }
344
345   if (xd->flags & DPDK_DEVICE_FLAG_INT_SUPPORTED)
346     {
347       int_mode = 1;
348       for (int q = 0; q < xd->conf.n_rx_queues; q++)
349         {
350           dpdk_rx_queue_t *rxq = vec_elt_at_index (xd->rx_queues, q);
351           clib_file_t f = { 0 };
352           rxq->efd = rte_eth_dev_rx_intr_ctl_q_get_fd (xd->port_id, q);
353           if (rxq->efd < 0)
354             {
355               xd->flags &= ~DPDK_DEVICE_FLAG_INT_SUPPORTED;
356               int_mode = 0;
357               break;
358             }
359           f.read_function = dpdk_rx_read_ready;
360           f.flags = UNIX_FILE_EVENT_EDGE_TRIGGERED;
361           f.file_descriptor = rxq->efd;
362           f.private_data = rxq->queue_index;
363           f.description =
364             format (0, "%U queue %u", format_dpdk_device_name, xd->port_id, q);
365           rxq->clib_file_index = clib_file_add (&file_main, &f);
366           vnet_hw_if_set_rx_queue_file_index (vnm, rxq->queue_index,
367                                               rxq->clib_file_index);
368           if (xd->flags & DPDK_DEVICE_FLAG_INT_UNMASKABLE)
369             {
370               clib_file_main_t *fm = &file_main;
371               clib_file_t *f =
372                 pool_elt_at_index (fm->file_pool, rxq->clib_file_index);
373               fm->file_update (f, UNIX_FILE_UPDATE_DELETE);
374             }
375         }
376     }
377
378   if (int_mode)
379     vnet_hw_if_set_caps (vnm, hi->hw_if_index, VNET_HW_IF_CAP_INT_MODE);
380   else
381     vnet_hw_if_unset_caps (vnm, hi->hw_if_index, VNET_HW_IF_CAP_INT_MODE);
382   vnet_hw_if_update_runtime_data (vnm, xd->hw_if_index);
383 }
384
385 void
386 dpdk_device_start (dpdk_device_t * xd)
387 {
388   int rv;
389
390   if (xd->flags & DPDK_DEVICE_FLAG_PMD_INIT_FAIL)
391     return;
392
393   rv = rte_eth_dev_start (xd->port_id);
394
395   if (rv)
396     {
397       dpdk_device_error (xd, "rte_eth_dev_start", rv);
398       return;
399     }
400
401   dpdk_log_debug ("[%u] RX burst function: %U", xd->port_id,
402                   format_dpdk_burst_fn, xd, VLIB_RX);
403   dpdk_log_debug ("[%u] TX burst function: %U", xd->port_id,
404                   format_dpdk_burst_fn, xd, VLIB_TX);
405
406   dpdk_setup_interrupts (xd);
407
408   if (xd->default_mac_address)
409     rv = rte_eth_dev_default_mac_addr_set (xd->port_id,
410                                            (void *) xd->default_mac_address);
411
412   if (rv)
413     dpdk_device_error (xd, "rte_eth_dev_default_mac_addr_set", rv);
414
415   if (xd->flags & DPDK_DEVICE_FLAG_PROMISC)
416     rte_eth_promiscuous_enable (xd->port_id);
417   else
418     rte_eth_promiscuous_disable (xd->port_id);
419
420   rte_eth_allmulticast_enable (xd->port_id);
421
422   dpdk_log_info ("Interface %U started",
423                  format_dpdk_device_name, xd->port_id);
424 }
425
426 void
427 dpdk_device_stop (dpdk_device_t * xd)
428 {
429   if (xd->flags & DPDK_DEVICE_FLAG_PMD_INIT_FAIL)
430     return;
431
432   rte_eth_allmulticast_disable (xd->port_id);
433   rte_eth_dev_stop (xd->port_id);
434   clib_memset (&xd->link, 0, sizeof (struct rte_eth_link));
435
436   dpdk_log_info ("Interface %U stopped",
437                  format_dpdk_device_name, xd->port_id);
438 }
439
440 void vl_api_force_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
441
442 always_inline int
443 dpdk_port_state_callback_inline (dpdk_portid_t port_id,
444                                  enum rte_eth_event_type type, void *param)
445 {
446   struct rte_eth_link link;
447
448   RTE_SET_USED (param);
449   if (type != RTE_ETH_EVENT_INTR_LSC)
450     {
451       dpdk_log_info ("Unknown event %d received for port %d", type, port_id);
452       return -1;
453     }
454
455   rte_eth_link_get_nowait (port_id, &link);
456   u8 link_up = link.link_status;
457   if (link_up)
458     dpdk_log_info ("Port %d Link Up - speed %u Mbps - %s", port_id,
459                    (unsigned) link.link_speed,
460                    (link.link_duplex == RTE_ETH_LINK_FULL_DUPLEX) ?
461                            "full-duplex" :
462                            "half-duplex");
463   else
464     dpdk_log_info ("Port %d Link Down\n\n", port_id);
465
466   return 0;
467 }
468
469 int
470 dpdk_port_state_callback (dpdk_portid_t port_id,
471                           enum rte_eth_event_type type,
472                           void *param,
473                           void *ret_param __attribute__ ((unused)))
474 {
475   return dpdk_port_state_callback_inline (port_id, type, param);
476 }
477
478 /* If this device is PCI return pointer to info, otherwise NULL */
479 struct rte_pci_device *
480 dpdk_get_pci_device (const struct rte_eth_dev_info *info)
481 {
482   const struct rte_bus *bus;
483
484   bus = rte_bus_find_by_device (info->device);
485 #if RTE_VERSION >= RTE_VERSION_NUM(22, 11, 0, 0)
486   if (bus && !strcmp (rte_bus_name (bus), "pci"))
487 #else
488   if (bus && !strcmp (bus->name, "pci"))
489 #endif
490     return RTE_DEV_TO_PCI (info->device);
491   else
492     return NULL;
493 }
494
495 /* If this device is VMBUS return pointer to info, otherwise NULL */
496 struct rte_vmbus_device *
497 dpdk_get_vmbus_device (const struct rte_eth_dev_info *info)
498 {
499   const struct rte_bus *bus;
500
501   bus = rte_bus_find_by_device (info->device);
502 #if RTE_VERSION >= RTE_VERSION_NUM(22, 11, 0, 0)
503   if (bus && !strcmp (rte_bus_name (bus), "vmbus"))
504 #else
505   if (bus && !strcmp (bus->name, "vmbus"))
506 #endif
507     return container_of (info->device, struct rte_vmbus_device, device);
508   else
509     return NULL;
510 }
511
512 /*
513  * fd.io coding-style-patch-verification: ON
514  *
515  * Local Variables:
516  * eval: (c-set-style "gnu")
517  * End:
518  */