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