NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
device.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2023 NVIDIA CORPORATION AND AFFILIATES. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, are permitted
5  * provided that the following conditions are met:
6  * * Redistributions of source code must retain the above copyright notice, this list of
7  * conditions and the following disclaimer.
8  * * Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * * Neither the name of the NVIDIA CORPORATION nor the names of its contributors may be used
12  * to endorse or promote products derived from this software without specific prior written
13  * permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
17  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
21  * STRICT LIABILITY, OR TOR (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  *
24  */
25 
26 #include <rte_ethdev.h>
27 
28 #include "common.h"
29 
30 DOCA_LOG_REGISTER(GPU_PACKET_PROCESSING_DEVICE);
31 
32 struct rte_eth_dev_info dev_info = {0};
33 struct rte_eth_conf eth_conf = {0};
34 struct rte_mempool *mp;
35 
36 /*
37  * Initialize a DOCA device with PCIe address object.
38  *
39  * @pcie_value [in]: PCIe address object
40  * @retval [out]: DOCA device object
41  * @return: DOCA_SUCCESS on success and DOCA_ERROR_INVALID_VALUE otherwise
42  */
43 static doca_error_t open_doca_device_with_pci(const char *pcie_value, struct doca_dev **retval)
44 {
45  struct doca_devinfo **dev_list;
46  uint32_t nb_devs;
47  int res;
48  size_t i;
49  uint8_t is_addr_equal = 0;
50 
51  /* Set default return value */
52  *retval = NULL;
53 
54  res = doca_devinfo_create_list(&dev_list, &nb_devs);
55  if (res != DOCA_SUCCESS) {
56  DOCA_LOG_ERR("Failed to load doca devices list. Doca_error value: %d", res);
57  return res;
58  }
59 
60  /* Search */
61  for (i = 0; i < nb_devs; i++) {
62  res = doca_devinfo_is_equal_pci_addr(dev_list[i], pcie_value, &is_addr_equal);
63  if (res == DOCA_SUCCESS && is_addr_equal) {
64  /* if device can be opened */
65  res = doca_dev_open(dev_list[i], retval);
66  if (res == DOCA_SUCCESS) {
67  doca_devinfo_destroy_list(dev_list);
68  return res;
69  }
70  }
71  }
72 
73  DOCA_LOG_ERR("Matching device not found");
75 
76  doca_devinfo_destroy_list(dev_list);
77  return res;
78 }
79 
80 doca_error_t init_doca_device(char *nic_pcie_addr, struct doca_dev **ddev, uint16_t *dpdk_port_id)
81 {
83  int ret;
84  char *eal_param[3] = {"", "-a", "00:00.0"};
85 
86  if (nic_pcie_addr == NULL || ddev == NULL || dpdk_port_id == NULL)
88 
89  if (strlen(nic_pcie_addr) >= DOCA_DEVINFO_PCI_ADDR_SIZE)
91 
92  result = open_doca_device_with_pci(nic_pcie_addr, ddev);
93  if (result != DOCA_SUCCESS) {
94  DOCA_LOG_ERR("Failed to open NIC device based on PCI address");
95  return result;
96  }
97 
98  ret = rte_eal_init(3, eal_param);
99  if (ret < 0) {
100  DOCA_LOG_ERR("DPDK init failed: %d", ret);
101  return DOCA_ERROR_DRIVER;
102  }
103 
104  /* Enable DOCA Flow HWS mode */
105  result = doca_dpdk_port_probe(*ddev, "dv_flow_en=2");
106  if (result != DOCA_SUCCESS) {
107  DOCA_LOG_ERR("Function doca_dpdk_port_probe returned %s", doca_error_get_descr(result));
108  return result;
109  }
110 
111  result = doca_dpdk_get_first_port_id(*ddev, dpdk_port_id);
112  if (result != DOCA_SUCCESS) {
113  DOCA_LOG_ERR("Function doca_dpdk_get_first_port_id returned %s", doca_error_get_descr(result));
114  return result;
115  }
116 
117  return DOCA_SUCCESS;
118 }
#define NULL
Definition: __stddef_null.h:26
int32_t result
struct rte_eth_conf eth_conf
Definition: device.c:33
static doca_error_t open_doca_device_with_pci(const char *pcie_value, struct doca_dev **retval)
Definition: device.c:43
DOCA_LOG_REGISTER(GPU_PACKET_PROCESSING_DEVICE)
doca_error_t init_doca_device(char *nic_pcie_addr, struct doca_dev **ddev, uint16_t *dpdk_port_id)
Definition: device.c:80
struct rte_mempool * mp
Definition: device.c:34
struct rte_eth_dev_info dev_info
Definition: device.c:32
static struct doca_dev * ddev
DOCA_STABLE doca_error_t doca_devinfo_is_equal_pci_addr(const struct doca_devinfo *devinfo, const char *pci_addr_str, uint8_t *is_equal)
Check if a PCI address belongs to a DOCA devinfo.
DOCA_STABLE doca_error_t doca_devinfo_create_list(struct doca_devinfo ***dev_list, uint32_t *nb_devs)
Creates list of all available local devices.
DOCA_STABLE doca_error_t doca_devinfo_destroy_list(struct doca_devinfo **dev_list)
Destroy list of local device info structures.
DOCA_STABLE doca_error_t doca_dev_open(struct doca_devinfo *devinfo, struct doca_dev **dev)
Initialize local device for use.
#define DOCA_DEVINFO_PCI_ADDR_SIZE
Buffer size to hold PCI BDF format: "XXXX:XX:XX.X". Including a null terminator.
Definition: doca_dev.h:313
DOCA_EXPERIMENTAL doca_error_t doca_dpdk_get_first_port_id(const struct doca_dev *dev, uint16_t *port_id)
Return the first DPDK port id associated to a DOCA device. Assumption is that the doca device that wa...
DOCA_EXPERIMENTAL doca_error_t doca_dpdk_port_probe(struct doca_dev *dev, const char *devargs)
Attach a DPDK port specified by DOCA device.
enum doca_error doca_error_t
DOCA API return codes.
DOCA_STABLE const char * doca_error_get_descr(doca_error_t error)
Returns the description string of an error code.
@ DOCA_ERROR_INVALID_VALUE
Definition: doca_error.h:44
@ DOCA_ERROR_NOT_FOUND
Definition: doca_error.h:54
@ DOCA_SUCCESS
Definition: doca_error.h:38
@ DOCA_ERROR_DRIVER
Definition: doca_error.h:59
#define DOCA_LOG_ERR(format,...)
Generates an ERROR application log message.
Definition: doca_log.h:466