NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
urom_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 #ifndef _GNU_SOURCE
27 #define _GNU_SOURCE
28 #endif
29 
30 #include <arpa/inet.h>
31 #include <netdb.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
36 
37 #include <doca_argp.h>
38 #include <doca_ctx.h>
39 #include <doca_log.h>
40 
41 #include "urom_common.h"
42 
43 DOCA_LOG_REGISTER(UROM::SAMPLES : COMMON);
44 
45 /*
46  * ARGP Callback - Handle IB device name parameter
47  *
48  * @param [in]: Input parameter
49  * @config [in/out]: Program configuration context
50  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
51  */
52 static doca_error_t device_address_callback(void *param, void *config)
53 {
54  struct urom_common_cfg *urom_cfg = (struct urom_common_cfg *)config;
55  char *device_name = (char *)param;
56  int len;
57 
60  DOCA_LOG_ERR("Entered IB device name exceeding the maximum size of %d",
63  }
64  strncpy(urom_cfg->device_name, device_name, len + 1);
65 
66  return DOCA_SUCCESS;
67 }
68 
70 {
72  struct doca_argp_param *device_param;
73 
74  /* Create and register device param */
75  result = doca_argp_param_create(&device_param);
76  if (result != DOCA_SUCCESS) {
77  DOCA_LOG_ERR("Failed to create ARGP param: %s", doca_error_get_descr(result));
78  return result;
79  }
80  doca_argp_param_set_short_name(device_param, "d");
81  doca_argp_param_set_long_name(device_param, "device");
82  doca_argp_param_set_arguments(device_param, "<IB device name>");
83  doca_argp_param_set_description(device_param, "IB device name.");
86  result = doca_argp_register_param(device_param);
87  if (result != DOCA_SUCCESS) {
88  DOCA_LOG_ERR("Failed to register program param: %s", doca_error_get_descr(result));
89  return result;
90  }
91 
92  return result;
93 }
94 
96  struct doca_dev *dev,
97  uint64_t nb_workers,
98  struct doca_urom_service **service)
99 {
100  enum doca_ctx_states state;
101  struct doca_urom_service *inst;
102  doca_error_t result, tmp_result;
103 
104  /* Create service context */
106  if (result != DOCA_SUCCESS)
107  return result;
108 
110  if (result != DOCA_SUCCESS)
111  goto service_cleanup;
112 
113  result = doca_urom_service_set_max_workers(inst, nb_workers);
114  if (result != DOCA_SUCCESS)
115  goto service_cleanup;
116 
117  result = doca_urom_service_set_dev(inst, dev);
118  if (result != DOCA_SUCCESS)
119  goto service_cleanup;
120 
122  if (result != DOCA_SUCCESS)
123  goto service_cleanup;
124 
126  if (result != DOCA_SUCCESS || state != DOCA_CTX_STATE_RUNNING)
127  goto service_stop;
128 
129  *service = inst;
130  return DOCA_SUCCESS;
131 
132 service_stop:
133  tmp_result = doca_ctx_stop(doca_urom_service_as_ctx(inst));
134  if (tmp_result != DOCA_SUCCESS) {
135  DOCA_LOG_ERR("Failed to stop UROM service");
136  DOCA_ERROR_PROPAGATE(result, tmp_result);
137  }
138 
139 service_cleanup:
140  tmp_result = doca_urom_service_destroy(inst);
141  if (tmp_result != DOCA_SUCCESS) {
142  DOCA_LOG_ERR("Failed to destroy UROM service");
143  DOCA_ERROR_PROPAGATE(result, tmp_result);
144  }
145  return result;
146 }
147 
149  struct doca_urom_service *service,
150  uint64_t worker_id,
151  uint32_t *gid,
152  uint64_t nb_tasks,
153  doca_cpu_set_t *cpuset,
154  char **env,
155  size_t env_count,
156  uint64_t plugins,
157  struct doca_urom_worker **worker)
158 {
159  enum doca_ctx_states state;
160  struct doca_urom_worker *inst;
161  doca_error_t result, tmp_result;
162 
164  if (result != DOCA_SUCCESS)
165  return result;
166 
167  result = doca_urom_worker_set_service(inst, service);
168  if (result != DOCA_SUCCESS)
169  goto worker_cleanup;
170 
172  if (result != DOCA_SUCCESS)
173  goto worker_cleanup;
174 
175  result = doca_urom_worker_set_id(inst, worker_id);
176  if (result != DOCA_SUCCESS)
177  goto worker_cleanup;
178 
179  if (gid != NULL) {
180  result = doca_urom_worker_set_gid(inst, *gid);
181  if (result != DOCA_SUCCESS)
182  goto worker_cleanup;
183  }
184 
185  if (env != NULL) {
186  result = doca_urom_worker_set_env(inst, env, env_count);
187  if (result != DOCA_SUCCESS)
188  goto worker_cleanup;
189  }
190 
192  if (result != DOCA_SUCCESS)
193  goto worker_cleanup;
194 
195  result = doca_urom_worker_set_plugins(inst, plugins);
196  if (result != DOCA_SUCCESS)
197  goto worker_cleanup;
198 
199  if (cpuset != NULL) {
200  result = doca_urom_worker_set_cpuset(inst, *cpuset);
201  if (result != DOCA_SUCCESS)
202  goto worker_cleanup;
203  }
204 
207  goto worker_cleanup;
208 
210  if (result != DOCA_SUCCESS)
211  goto worker_stop;
212 
213  if (state != DOCA_CTX_STATE_STARTING) {
215  goto worker_stop;
216  }
217 
218  *worker = inst;
219  return DOCA_SUCCESS;
220 
221 worker_stop:
222  tmp_result = doca_ctx_stop(doca_urom_worker_as_ctx(inst));
223  if (tmp_result != DOCA_SUCCESS && tmp_result != DOCA_ERROR_IN_PROGRESS) {
224  DOCA_LOG_ERR("Failed to request stop UROM worker");
225  DOCA_ERROR_PROPAGATE(result, tmp_result);
226  }
227 
228  do {
231  } while (state != DOCA_CTX_STATE_IDLE);
232 
233 worker_cleanup:
234  tmp_result = doca_urom_worker_destroy(inst);
235  if (tmp_result != DOCA_SUCCESS) {
236  DOCA_LOG_ERR("Failed to destroy UROM worker");
237  DOCA_ERROR_PROPAGATE(result, tmp_result);
238  }
239 
240  return result;
241 }
242 
244  struct doca_urom_domain_oob_coll *oob,
245  uint64_t *worker_ids,
246  struct doca_urom_worker **workers,
247  size_t nb_workers,
248  struct urom_domain_buffer_attrs *buffers,
249  size_t nb_buffers,
250  struct doca_urom_domain **domain)
251 {
252  size_t i;
253  doca_error_t result, tmp_result;
254  enum doca_ctx_states state;
255  struct doca_urom_domain *inst;
256 
258  if (result != DOCA_SUCCESS) {
259  DOCA_LOG_ERR("Failed to create domain");
260  return result;
261  }
262 
264  if (result != DOCA_SUCCESS)
265  goto domain_destroy;
266 
267  result = doca_urom_domain_set_oob(inst, oob);
268  if (result != DOCA_SUCCESS)
269  goto domain_destroy;
270 
271  result = doca_urom_domain_set_workers(inst, worker_ids, workers, nb_workers);
272  if (result != DOCA_SUCCESS)
273  goto domain_destroy;
274 
275  if (nb_workers != 0 && buffers != NULL) {
276  result = doca_urom_domain_set_buffers_count(inst, nb_buffers);
277  if (result != DOCA_SUCCESS)
278  goto domain_destroy;
279 
280  for (i = 0; i < nb_buffers; i++) {
282  buffers[i].buffer,
283  buffers[i].buf_len,
284  buffers[i].memh,
285  buffers[i].memh_len,
286  buffers[i].mkey,
287  buffers[i].mkey_len);
288  if (result != DOCA_SUCCESS)
289  goto domain_destroy;
290  }
291  }
292 
295  goto domain_stop;
296 
298  if (result != DOCA_SUCCESS)
299  goto domain_stop;
300 
301  if (state != DOCA_CTX_STATE_STARTING) {
303  goto domain_stop;
304  }
305 
306  *domain = inst;
307  return DOCA_SUCCESS;
308 
309 domain_stop:
310  tmp_result = doca_ctx_stop(doca_urom_domain_as_ctx(inst));
311  if (tmp_result != DOCA_SUCCESS) {
312  DOCA_LOG_ERR("Failed to stop UROM domain");
313  DOCA_ERROR_PROPAGATE(result, tmp_result);
314  }
315 
316 domain_destroy:
317  tmp_result = doca_urom_domain_destroy(inst);
318  if (tmp_result != DOCA_SUCCESS) {
319  DOCA_LOG_ERR("Failed to destroy UROM domain");
320  DOCA_ERROR_PROPAGATE(result, tmp_result);
321  }
322  return result;
323 }
#define NULL
Definition: __stddef_null.h:26
int32_t result
uint64_t len
static enum doca_flow_pipe_domain domain
static struct doca_pe * pe
DOCA_EXPERIMENTAL void doca_argp_param_set_description(struct doca_argp_param *param, const char *description)
Set the description of the program param, used during program usage.
DOCA_EXPERIMENTAL void doca_argp_param_set_long_name(struct doca_argp_param *param, const char *name)
Set the long name of the program param.
DOCA_EXPERIMENTAL void doca_argp_param_set_arguments(struct doca_argp_param *param, const char *arguments)
Set the description of the expected arguments of the program param, used during program usage.
DOCA_EXPERIMENTAL void doca_argp_param_set_callback(struct doca_argp_param *param, doca_argp_param_cb_t callback)
Set the callback function of the program param.
DOCA_EXPERIMENTAL doca_error_t doca_argp_param_create(struct doca_argp_param **param)
Create new program param.
DOCA_EXPERIMENTAL void doca_argp_param_set_type(struct doca_argp_param *param, enum doca_argp_type type)
Set the type of the param arguments.
DOCA_EXPERIMENTAL void doca_argp_param_set_short_name(struct doca_argp_param *param, const char *name)
Set the short name of the program param.
DOCA_EXPERIMENTAL doca_error_t doca_argp_register_param(struct doca_argp_param *input_param)
Register a program flag.
@ DOCA_ARGP_TYPE_STRING
Definition: doca_argp.h:56
DOCA_STABLE doca_error_t doca_ctx_start(struct doca_ctx *ctx)
Finalizes all configurations, and starts the DOCA CTX.
DOCA_STABLE doca_error_t doca_ctx_get_state(const struct doca_ctx *ctx, enum doca_ctx_states *state)
Get context state.
DOCA_STABLE doca_error_t doca_ctx_stop(struct doca_ctx *ctx)
Stops the context allowing reconfiguration.
doca_ctx_states
This enum defines the states of a context.
Definition: doca_ctx.h:83
@ DOCA_CTX_STATE_STARTING
Definition: doca_ctx.h:93
@ DOCA_CTX_STATE_IDLE
Definition: doca_ctx.h:88
@ DOCA_CTX_STATE_RUNNING
Definition: doca_ctx.h:98
#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_ERROR_PROPAGATE(r, t)
Save the first encountered doca_error_t.
Definition: doca_error.h:83
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_BAD_STATE
Definition: doca_error.h:56
@ DOCA_SUCCESS
Definition: doca_error.h:38
@ DOCA_ERROR_IN_PROGRESS
Definition: doca_error.h:64
#define DOCA_LOG_ERR(format,...)
Generates an ERROR application log message.
Definition: doca_log.h:466
DOCA_STABLE doca_error_t doca_pe_connect_ctx(struct doca_pe *pe, struct doca_ctx *ctx)
This method connects a context to a progress engine.
DOCA_STABLE uint8_t doca_pe_progress(struct doca_pe *pe)
Run the progress engine.
DOCA_EXPERIMENTAL struct doca_ctx * doca_urom_domain_as_ctx(struct doca_urom_domain *domain_ctx)
DOCA_EXPERIMENTAL doca_error_t doca_urom_domain_destroy(struct doca_urom_domain *domain_ctx)
DOCA_EXPERIMENTAL doca_error_t doca_urom_domain_set_oob(struct doca_urom_domain *domain_ctx, struct doca_urom_domain_oob_coll *oob)
Set OOB communication info to be used for Domain initialization.
DOCA_EXPERIMENTAL doca_error_t doca_urom_domain_create(struct doca_urom_domain **domain_ctx)
This method creates a UROM Domain context.
DOCA_EXPERIMENTAL doca_error_t doca_urom_domain_add_buffer(struct doca_urom_domain *domain_ctx, void *buffer, size_t buf_len, void *memh, size_t memh_len, void *mkey, size_t mkey_len)
This method attaches local buffer attributes to the Domain. Should be called after calling doca_urom_...
DOCA_EXPERIMENTAL doca_error_t doca_urom_domain_set_workers(struct doca_urom_domain *domain_ctx, uint64_t *domain_worker_ids, struct doca_urom_worker **workers, size_t workers_cnt)
Set the list of Workers in the domain.
DOCA_EXPERIMENTAL doca_error_t doca_urom_domain_set_buffers_count(struct doca_urom_domain *domain_ctx, size_t buffers_cnt)
This method attaches the number of local buffers that will be added to the Domain.
DOCA_EXPERIMENTAL doca_error_t doca_urom_service_set_dev(struct doca_urom_service *service_ctx, struct doca_dev *dev)
This method attaches a DOCA Device to the UROM Service context.
ucs_cpu_set_t doca_cpu_set_t
DOCA CPU set structure.
Definition: doca_urom.h:98
DOCA_EXPERIMENTAL struct doca_ctx * doca_urom_service_as_ctx(struct doca_urom_service *service_ctx)
Convert service_ctx instance into a generalized context for use with DOCA core objects.
DOCA_EXPERIMENTAL doca_error_t doca_urom_service_destroy(struct doca_urom_service *service_ctx)
This method destroys a UROM Service context.
DOCA_EXPERIMENTAL doca_error_t doca_urom_service_set_max_workers(struct doca_urom_service *service_ctx, uint32_t max_workers)
Set maximum number of UROM workers.
DOCA_EXPERIMENTAL doca_error_t doca_urom_service_create(struct doca_urom_service **service_ctx)
This method creates a UROM Service context.
DOCA_EXPERIMENTAL doca_error_t doca_urom_worker_set_service(struct doca_urom_worker *worker_ctx, struct doca_urom_service *service_ctx)
This method attaches a UROM Service to the Worker context.
DOCA_EXPERIMENTAL doca_error_t doca_urom_worker_set_cpuset(struct doca_urom_worker *worker_ctx, doca_cpu_set_t cpuset)
Set the allowed CPUs for the Worker. The cpuset must be a subset of the service's allowed CPUs.
DOCA_EXPERIMENTAL doca_error_t doca_urom_worker_destroy(struct doca_urom_worker *worker_ctx)
This method destroys a UROM Worker context.
DOCA_EXPERIMENTAL doca_error_t doca_urom_worker_set_env(struct doca_urom_worker *worker_ctx, char *const env[], size_t count)
Set worker env variables when spawning worker on DPU side.
DOCA_EXPERIMENTAL doca_error_t doca_urom_worker_create(struct doca_urom_worker **worker_ctx)
This method creates a UROM Worker context.
DOCA_EXPERIMENTAL doca_error_t doca_urom_worker_set_gid(struct doca_urom_worker *worker_ctx, uint32_t gid)
Set worker group ID, must be set before starting the worker context.
DOCA_EXPERIMENTAL doca_error_t doca_urom_worker_set_max_inflight_tasks(struct doca_urom_worker *worker_ctx, uint32_t max_tasks)
Set Worker maximum in-flight tasks.
DOCA_EXPERIMENTAL doca_error_t doca_urom_worker_set_id(struct doca_urom_worker *worker_ctx, uint64_t worker_id)
This method sets the Worker context ID to be used to identify the Worker. Worker IDs enable an applic...
DOCA_EXPERIMENTAL doca_error_t doca_urom_worker_set_plugins(struct doca_urom_worker *worker_ctx, uint64_t plugins)
This method adds plugins mask for the supported plugins by the UROM Worker on the DPU.
DOCA_EXPERIMENTAL struct doca_ctx * doca_urom_worker_as_ctx(struct doca_urom_worker *worker_ctx)
Out-of-band communication descriptor for Domain creation.
Definition: doca_urom.h:1058
char device_name[DOCA_DEVINFO_IBDEV_NAME_SIZE]
Definition: urom_common.h:41
doca_error_t start_urom_service(struct doca_pe *pe, struct doca_dev *dev, uint64_t nb_workers, struct doca_urom_service **service)
Definition: urom_common.c:95
doca_error_t register_urom_common_params(void)
Definition: urom_common.c:69
doca_error_t start_urom_worker(struct doca_pe *pe, struct doca_urom_service *service, uint64_t worker_id, uint32_t *gid, uint64_t nb_tasks, doca_cpu_set_t *cpuset, char **env, size_t env_count, uint64_t plugins, struct doca_urom_worker **worker)
Definition: urom_common.c:148
static doca_error_t device_address_callback(void *param, void *config)
Definition: urom_common.c:52
doca_error_t start_urom_domain(struct doca_pe *pe, struct doca_urom_domain_oob_coll *oob, uint64_t *worker_ids, struct doca_urom_worker **workers, size_t nb_workers, struct urom_domain_buffer_attrs *buffers, size_t nb_buffers, struct doca_urom_domain **domain)
Definition: urom_common.c:243
DOCA_LOG_REGISTER(UROM::SAMPLES :COMMON)