NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
flow_pipe_resize_main.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 <stdlib.h>
27 
28 #include <doca_argp.h>
29 #include <doca_flow.h>
30 #include <doca_log.h>
31 
32 #include <dpdk_utils.h>
33 
34 #include "flow_switch_common.h"
35 
36 DOCA_LOG_REGISTER(FLOW_PIPE_RESIZE::MAIN);
37 
38 /*
39  * Config for pipe resize params
40  */
42  struct flow_switch_ctx ctx; /* switch context */
43  bool is_basic_pipe; /* determine pipe type under resize */
44 };
45 
46 /* Sample's Logic */
47 doca_error_t flow_pipe_resize(uint16_t nb_queues, struct flow_switch_ctx *ctx, bool is_basic_pipe);
48 
49 /*
50  * Callback for arg pipe-type
51  *
52  * @param [in]: parameter
53  * @config [in]: app configuration
54  * @return: DOCA_SUCCESS on success and negative number otherwise
55  */
56 static doca_error_t pipe_type_callback(void *param, void *config)
57 {
58  struct flow_resize_ctx *app_config = (struct flow_resize_ctx *)config;
59  const char *str = (const char *)param;
60 
61  if (strcmp(str, "basic") == 0)
62  app_config->is_basic_pipe = true;
63  else if (strcmp(str, "control") == 0)
64  app_config->is_basic_pipe = false;
65  else {
66  DOCA_LOG_ERR("Unsupported resize pipe_type '%s' was specified", str);
68  }
69 
70  return DOCA_SUCCESS;
71 }
72 
73 /*
74  * Register for which pipe type (BASIC/CONTROL) to resize.
75  *
76  * @return: DOCA_SUCCESS on success, negative number otherwise.
77  *
78  */
79 static int register_pipe_type_params(void)
80 {
82  struct doca_argp_param *pipe_type_param;
83 
84  /* Create and register pipe-type para */
85  result = doca_argp_param_create(&pipe_type_param);
86  if (result != DOCA_SUCCESS) {
87  DOCA_LOG_ERR("Failed to create pipe-type ARGP param: %s", doca_error_get_descr(result));
88  return result;
89  }
90 
91  doca_argp_param_set_short_name(pipe_type_param, "ptype");
92  doca_argp_param_set_long_name(pipe_type_param, "pipe-type");
93  doca_argp_param_set_arguments(pipe_type_param, "<pipe-type>");
94  doca_argp_param_set_description(pipe_type_param, "Set pipe-type (\"basic\", \"control\") to resize");
97  result = doca_argp_register_param(pipe_type_param);
98  if (result != DOCA_SUCCESS) {
99  DOCA_LOG_ERR("Failed to register program param: %s", doca_error_get_descr(result));
100  return result;
101  }
102 
103  return 0;
104 }
105 
106 /*
107  * Sample main function
108  *
109  * @argc [in]: command line arguments size
110  * @argv [in]: array of command line arguments
111  * @return: EXIT_SUCCESS on success and EXIT_FAILURE otherwise
112  */
113 int main(int argc, char **argv)
114 {
116  struct doca_log_backend *sdk_log;
117  int exit_status = EXIT_FAILURE;
118  struct application_dpdk_config dpdk_config = {
119  .port_config.nb_ports = 3,
120  .port_config.isolated_mode = 1,
121  .port_config.switch_mode = 1,
122  };
123  struct flow_resize_ctx resize_ctx = {0};
124 
125  /* Register a logger backend */
127  if (result != DOCA_SUCCESS)
128  goto sample_exit;
129 
130  /* Register a logger backend for internal SDK errors and warnings */
131  result = doca_log_backend_create_with_file_sdk(stderr, &sdk_log);
132  if (result != DOCA_SUCCESS)
133  goto sample_exit;
135  if (result != DOCA_SUCCESS)
136  goto sample_exit;
137 
138  DOCA_LOG_INFO("Starting the sample");
139 
140  result = doca_argp_init(NULL, &resize_ctx);
141  if (result != DOCA_SUCCESS) {
142  DOCA_LOG_ERR("Failed to init ARGP resources: %s", doca_error_get_descr(result));
143  goto sample_exit;
144  }
146  if (result != DOCA_SUCCESS) {
147  DOCA_LOG_ERR("Failed to register flow param: %s", doca_error_get_descr(result));
148  goto argp_cleanup;
149  }
151  if (result != DOCA_SUCCESS) {
152  DOCA_LOG_ERR("Failed to register flow parameters: %s", doca_error_get_descr(result));
153  goto argp_cleanup;
154  }
156  result = doca_argp_start(argc, argv);
157  if (result != DOCA_SUCCESS) {
158  DOCA_LOG_ERR("Failed to parse sample input: %s", doca_error_get_descr(result));
159  goto argp_cleanup;
160  }
161 
162  result = init_doca_flow_switch_common(&resize_ctx.ctx);
163  if (result != DOCA_SUCCESS) {
164  DOCA_LOG_ERR("Failed to init flow switch common: %s", doca_error_get_descr(result));
165  goto dpdk_cleanup;
166  }
167 
168  /* update queues and ports */
169  result = dpdk_queues_and_ports_init(&dpdk_config);
170  if (result != DOCA_SUCCESS) {
171  DOCA_LOG_ERR("Failed to update ports and queues");
172  goto dpdk_cleanup;
173  }
174 
175  /* run sample */
176  result = flow_pipe_resize(dpdk_config.port_config.nb_queues, &resize_ctx.ctx, resize_ctx.is_basic_pipe);
177  if (result != DOCA_SUCCESS) {
178  DOCA_LOG_ERR("flow_pipe_resize() encountered an error: %s", doca_error_get_descr(result));
179  goto dpdk_ports_queues_cleanup;
180  }
181 
182  exit_status = EXIT_SUCCESS;
183 
184 dpdk_ports_queues_cleanup:
185  dpdk_queues_and_ports_fini(&dpdk_config);
186 dpdk_cleanup:
187  dpdk_fini();
188 argp_cleanup:
190 sample_exit:
192  if (exit_status == EXIT_SUCCESS)
193  DOCA_LOG_INFO("Sample finished successfully");
194  else
195  DOCA_LOG_INFO("Sample finished with errors");
196  return exit_status;
197 }
#define NULL
Definition: __stddef_null.h:26
int32_t result
if(bitoffset % 64+bitlength > 64) result|
void dpdk_fini(void)
Definition: dpdk_utils.c:919
doca_error_t dpdk_queues_and_ports_init(struct application_dpdk_config *app_dpdk_config)
Definition: dpdk_utils.c:515
void dpdk_queues_and_ports_fini(struct application_dpdk_config *app_dpdk_config)
Definition: dpdk_utils.c:564
static doca_error_t pipe_type_callback(void *param, void *config)
static int register_pipe_type_params(void)
int main(int argc, char **argv)
doca_error_t flow_pipe_resize(uint16_t nb_queues, struct flow_switch_ctx *ctx, bool is_basic_pipe)
DOCA_LOG_REGISTER(FLOW_PIPE_RESIZE::MAIN)
doca_error_t init_doca_flow_switch_common(struct flow_switch_ctx *ctx)
void destroy_doca_flow_switch_common(struct flow_switch_ctx *ctx)
doca_error_t register_doca_flow_switch_param(void)
doca_error_t init_flow_switch_dpdk(int argc, char **dpdk_argv)
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 doca_error_t doca_argp_start(int argc, char **argv)
Parse incoming arguments (cmd line/json).
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 doca_error_t doca_argp_init(const char *program_name, void *program_config)
Initialize the parser interface.
DOCA_EXPERIMENTAL void doca_argp_set_dpdk_program(doca_argp_dpdk_cb_t callback)
Mark the program as based on DPDK API.
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_destroy(void)
ARG Parser destroy.
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
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_NOT_SUPPORTED
Definition: doca_error.h:42
@ 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
#define DOCA_LOG_INFO(format,...)
Generates an INFO application log message.
Definition: doca_log.h:486
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
struct application_port_config port_config
Definition: dpdk_utils.h:70
struct flow_switch_ctx ctx
struct upf_accel_ctx * ctx