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