NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
stream_receive_perf.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2025 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 
28 #include <doca_argp.h>
29 #include <doca_log.h>
30 
32 
33 DOCA_LOG_REGISTER(STREAM_RECEIVE_PERF);
34 
35 /*
36  * Stream Receive application main function
37  *
38  * @argc [in]: command line arguments size
39  * @argv [in]: array of command line arguments
40  * @return: EXIT_SUCCESS on success and EXIT_FAILURE otherwise
41  */
42 int main(int argc, char **argv)
43 {
44  struct app_config config;
45  doca_error_t ret;
46  struct doca_log_backend *sdk_log;
47  int exit_code = EXIT_SUCCESS;
48 
49  /* Create a logger backend that prints to the standard output */
51  if (ret != DOCA_SUCCESS) {
52  fprintf(stderr, "Logger initialization failed: %s\n", doca_error_get_name(ret));
53  return EXIT_FAILURE;
54  }
55 
56  /* Register a logger backend for internal SDK errors and warnings */
57  ret = doca_log_backend_create_with_file_sdk(stderr, &sdk_log);
58  if (ret != DOCA_SUCCESS)
59  return EXIT_FAILURE;
61  if (ret != DOCA_SUCCESS)
62  return EXIT_FAILURE;
63 
64  if (!init_config(&config))
65  return EXIT_FAILURE;
66  ret = doca_argp_init(APP_NAME, &config);
67  if (ret != DOCA_SUCCESS) {
68  DOCA_LOG_ERR("Failed to init ARGP resources: %s", doca_error_get_name(ret));
69  exit_code = EXIT_FAILURE;
70  goto cleanup_config;
71  }
72  if (!register_argp_params()) {
73  exit_code = EXIT_FAILURE;
74  goto cleanup_argp;
75  }
76  ret = doca_argp_start(argc, argv);
77  if (ret != DOCA_SUCCESS) {
78  DOCA_LOG_ERR("Failed to parse application command line: %s", doca_error_get_name(ret));
79  exit_code = EXIT_FAILURE;
80  goto cleanup_argp;
81  }
82 
83  if (config.list) {
84  ret = doca_rmax_init();
85  if (ret != DOCA_SUCCESS) {
86  DOCA_LOG_ERR("Failed to initialize DOCA RMAX: %s", doca_error_get_name(ret));
87  exit_code = EXIT_FAILURE;
88  goto cleanup_argp;
89  }
90 
91  list_devices();
92  } else {
93  struct globals globals;
94  struct stream_data data;
95  struct doca_dev *dev = NULL;
96 
97  if (!mandatory_args_set(&config)) {
98  DOCA_LOG_ERR("Not all mandatory arguments were set");
100  exit_code = EXIT_FAILURE;
101  goto cleanup_argp;
102  }
103 
104  if (config.affinity_mask_set) {
105  ret = doca_rmax_set_cpu_affinity_mask(config.affinity_mask);
106  if (ret != DOCA_SUCCESS) {
107  DOCA_LOG_ERR("Error setting CPU affinity mask: %s", doca_error_get_name(ret));
108  exit_code = EXIT_FAILURE;
109  goto cleanup_argp;
110  }
111  }
112  ret = doca_rmax_init();
113  if (ret != DOCA_SUCCESS) {
114  DOCA_LOG_ERR("Failed to initialize DOCA RMAX: %s", doca_error_get_name(ret));
115  exit_code = EXIT_FAILURE;
116  goto cleanup_argp;
117  }
118 
119  dev = open_device(&config.dev_ip);
120  if (dev == NULL) {
121  DOCA_LOG_ERR("Error opening device");
122  exit_code = EXIT_FAILURE;
123  goto cleanup_rmax;
124  }
125 
126  ret = init_globals(&config, dev, &globals);
127  if (ret != DOCA_SUCCESS) {
128  exit_code = EXIT_FAILURE;
129  goto cleanup_device;
130  }
131 
132  ret = init_stream(&config, dev, &globals, &data);
133  if (ret != DOCA_SUCCESS) {
134  exit_code = EXIT_FAILURE;
135  goto cleanup_globals;
136  }
137 
138  /* main loop */
139  if (!run_recv_loop(&config, &globals, &data))
140  exit_code = EXIT_FAILURE;
141 
142  if (!destroy_stream(dev, &globals, &data))
143  exit_code = EXIT_FAILURE;
144 cleanup_globals:
145  if (!destroy_globals(&globals, dev))
146  exit_code = EXIT_FAILURE;
147 cleanup_device:
148  ret = doca_dev_close(dev);
149  if (ret != DOCA_SUCCESS) {
150  exit_code = EXIT_FAILURE;
151  DOCA_LOG_ERR("Error closing device: %s", doca_error_get_name(ret));
152  }
153  }
154 
155 cleanup_rmax:
156  ret = doca_rmax_release();
157  if (ret != DOCA_SUCCESS) {
158  DOCA_LOG_ERR("Failed to release DOCA RMAX: %s", doca_error_get_name(ret));
159  exit_code = EXIT_FAILURE;
160  }
161 cleanup_argp:
163 cleanup_config:
164  destroy_config(&config);
165 
166  return exit_code;
167 }
#define NULL
Definition: __stddef_null.h:26
DOCA_EXPERIMENTAL doca_error_t doca_argp_start(int argc, char **argv)
Parse incoming arguments (cmd line/json).
DOCA_EXPERIMENTAL doca_error_t doca_argp_init(const char *program_name, void *program_config)
Initialize the parser interface.
DOCA_EXPERIMENTAL void doca_argp_usage(void)
Print usage instructions.
DOCA_EXPERIMENTAL doca_error_t doca_argp_destroy(void)
ARG Parser destroy.
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_SUCCESS
Definition: doca_error.h:38
DOCA_EXPERIMENTAL doca_error_t doca_log_backend_create_standard(void)
Create default, non configurable backend for application messages.
#define DOCA_LOG_ERR(format,...)
Generates an ERROR application log message.
Definition: doca_log.h:466
DOCA_EXPERIMENTAL doca_error_t doca_log_backend_create_with_file_sdk(FILE *fptr, struct doca_log_backend **backend)
Create a logging backend with a FILE* stream for SDK messages.
DOCA_EXPERIMENTAL doca_error_t doca_log_backend_set_sdk_level(struct doca_log_backend *backend, uint32_t level)
Set the log level limit for SDK logging backends.
@ DOCA_LOG_LEVEL_WARNING
Definition: doca_log.h:47
doca_dev * open_device(std::string const &identifier)
Definition: doca_utils.cpp:43
int main(int argc, char **argv)
DOCA_LOG_REGISTER(STREAM_RECEIVE_PERF)
bool destroy_stream(struct doca_dev *dev, struct globals *globals, struct stream_data *data)
bool register_argp_params(void)
doca_error_t init_globals(struct app_config *config, struct doca_dev *dev, struct globals *globals)
bool run_recv_loop(const struct app_config *config, struct globals *globals, struct stream_data *data)
bool destroy_globals(struct globals *globals, struct doca_dev *dev)
doca_error_t init_stream(struct app_config *config, struct doca_dev *dev, struct globals *globals, struct stream_data *data)
bool mandatory_args_set(struct app_config *config)
void list_devices(void)
bool init_config(struct app_config *config)
void destroy_config(struct app_config *config)
#define APP_NAME
struct in_addr dev_ip
struct doca_rmax_cpu_affinity * affinity_mask