NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
eth_common.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 <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 
30 #include <doca_log.h>
31 
32 #include "eth_common.h"
33 
34 DOCA_LOG_REGISTER(ETH::COMMON);
35 
36 #define CACHE_LINE_SIZE 64 /* Cache-line size in bytes */
37 
39 {
40  doca_error_t status;
41 
42  if (cfg == NULL || resources == NULL) {
43  DOCA_LOG_ERR("Failed to allocate eth_core_resources: invalid parameters");
45  }
46 
47  memset(resources, 0, sizeof(*resources));
48 
49  status = open_doca_device_with_ibdev_name((const uint8_t *)(cfg->ibdev_name),
51  cfg->check_device,
52  &(resources->core_objs.dev));
53  if (status != DOCA_SUCCESS) {
54  DOCA_LOG_ERR("Failed to allocate eth_core_resources: failed to open a device, err: %s",
55  doca_error_get_name(status));
56  return status;
57  }
58 
59  status = create_core_objects(&(resources->core_objs), cfg->inventory_num_elements);
60  if (status != DOCA_SUCCESS) {
61  DOCA_LOG_ERR("Failed to allocate eth_core_resources: failed to create core objects, err: %s",
62  doca_error_get_name(status));
64  return status;
65  }
66 
67  resources->mem_addr = malloc(cfg->mmap_size + CACHE_LINE_SIZE);
68  if (resources->mem_addr == NULL) {
69  DOCA_LOG_ERR("Failed to allocate eth_core_resources: failed to allocate mmap memory");
70  status = DOCA_ERROR_NO_MEMORY;
72  return status;
73  }
74 
75  resources->mmap_addr = (void *)align_up_uint64((uint64_t)resources->mem_addr, CACHE_LINE_SIZE);
76 
77  resources->mmap_size = cfg->mmap_size;
78 
79  status = doca_mmap_set_memrange(resources->core_objs.src_mmap, resources->mmap_addr, resources->mmap_size);
80  if (status != DOCA_SUCCESS) {
81  DOCA_LOG_ERR("Failed to allocate eth_core_resources: failed to set mmap range, err: %s",
82  doca_error_get_name(status));
84  return status;
85  }
86 
87  status = doca_mmap_start(resources->core_objs.src_mmap);
88  if (status != DOCA_SUCCESS) {
89  DOCA_LOG_ERR("Failed to allocate eth_core_resources: failed to start mmap, err: %s",
90  doca_error_get_name(status));
92  return status;
93  }
94 
95  return DOCA_SUCCESS;
96 }
97 
99 {
100  doca_error_t status;
101 
102  if (resources->mem_addr != NULL) {
103  free(resources->mem_addr);
104  resources->mem_addr = NULL;
105  }
106 
107  if (resources->core_objs.pe != NULL) {
108  status = destroy_core_objects(&(resources->core_objs));
109  if (status != DOCA_SUCCESS) {
110  DOCA_LOG_ERR("Failed to destroy core objects, err: %s", doca_error_get_name(status));
111  return status;
112  }
113  resources->core_objs.pe = NULL;
114  }
115 
116  if (resources->core_objs.dev != NULL) {
117  status = doca_dev_close(resources->core_objs.dev);
118  if (status != DOCA_SUCCESS) {
119  DOCA_LOG_ERR("Failed to close device, err: %s", doca_error_get_name(status));
120  return status;
121  }
122  resources->core_objs.dev = NULL;
123  }
124 
125  return DOCA_SUCCESS;
126 }
127 
128 doca_error_t extract_ibdev_name(char *ibdev_name, char *ibdev_name_out)
129 {
130  int len;
131 
132  if (ibdev_name == NULL || ibdev_name_out == NULL)
134 
135  len = strnlen(ibdev_name, DOCA_DEVINFO_IBDEV_NAME_SIZE);
137  DOCA_LOG_ERR("IB device name exceeding the maximum size of %d", DOCA_DEVINFO_IBDEV_NAME_SIZE - 1);
139  }
140 
141  strncpy(ibdev_name_out, ibdev_name, len + 1);
142 
143  return DOCA_SUCCESS;
144 }
145 
146 doca_error_t extract_mac_addr(char *mac_addr, uint8_t *mac_addr_out)
147 {
148  int len, valid_size;
149 
150  if (mac_addr == NULL || mac_addr_out == NULL)
152 
153  valid_size = strlen("FF:FF:FF:FF:FF:FF");
154  len = strnlen(mac_addr, valid_size + 1);
155  if (len != valid_size) {
156  DOCA_LOG_ERR("Invalid MAC address, it should be in the following format FF:FF:FF:FF:FF:FF");
158  }
159 
160  if (sscanf(mac_addr,
161  "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
162  &(mac_addr_out[0]),
163  &(mac_addr_out[1]),
164  &(mac_addr_out[2]),
165  &(mac_addr_out[3]),
166  &(mac_addr_out[4]),
167  &(mac_addr_out[5])) != DOCA_DEVINFO_MAC_ADDR_SIZE) {
168  DOCA_LOG_ERR("Invalid MAC address, it should be in the following format FF:FF:FF:FF:FF:FF");
170  }
171 
172  return DOCA_SUCCESS;
173 }
#define NULL
Definition: __stddef_null.h:26
uint64_t align_up_uint64(uint64_t value, uint64_t alignment)
Definition: common.c:512
doca_error_t destroy_core_objects(struct program_core_objects *state)
Definition: common.c:392
doca_error_t create_core_objects(struct program_core_objects *state, uint32_t max_bufs)
Definition: common.c:302
doca_error_t open_doca_device_with_ibdev_name(const uint8_t *value, size_t val_size, tasks_check func, struct doca_dev **retval)
Definition: common.c:84
uint64_t len
doca_error_t destroy_eth_core_resources(struct eth_core_resources *resources)
Definition: eth_common.c:98
doca_error_t extract_ibdev_name(char *ibdev_name, char *ibdev_name_out)
Definition: eth_common.c:128
doca_error_t allocate_eth_core_resources(struct eth_core_config *cfg, struct eth_core_resources *resources)
Definition: eth_common.c:38
DOCA_LOG_REGISTER(ETH::COMMON)
doca_error_t extract_mac_addr(char *mac_addr, uint8_t *mac_addr_out)
Definition: eth_common.c:146
#define CACHE_LINE_SIZE
Definition: eth_common.c:36
struct rdma_resources resources
#define DOCA_DEVINFO_IBDEV_NAME_SIZE
Buffer size to hold Infiniband/RoCE device name. Including a null terminator.
Definition: doca_dev.h:309
#define DOCA_DEVINFO_MAC_ADDR_SIZE
Length of MAC address.
Definition: doca_dev.h:301
DOCA_STABLE doca_error_t doca_dev_close(struct doca_dev *dev)
Destroy allocated local device instance.
enum doca_error doca_error_t
DOCA API return codes.
DOCA_STABLE const char * doca_error_get_name(doca_error_t error)
Returns the string representation of an error code name.
@ DOCA_ERROR_INVALID_VALUE
Definition: doca_error.h:44
@ DOCA_SUCCESS
Definition: doca_error.h:38
@ DOCA_ERROR_NO_MEMORY
Definition: doca_error.h:45
#define DOCA_LOG_ERR(format,...)
Generates an ERROR application log message.
Definition: doca_log.h:466
DOCA_STABLE doca_error_t doca_mmap_set_memrange(struct doca_mmap *mmap, void *addr, size_t len)
Set the memory range of DOCA memory map.
DOCA_STABLE doca_error_t doca_mmap_start(struct doca_mmap *mmap)
Start DOCA Memory Map.
const struct ip_frag_config * cfg
Definition: ip_frag_dp.c:0
struct doca_pe * pe
Definition: rdma_common.h:86