NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
flow_mark_set_main.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2022-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 #include "flow_mark_set.h"
34 
35 DOCA_LOG_REGISTER(FLOW_MARK_SET::MAIN);
36 
37 /* Sample's Logic */
38 doca_error_t flow_mark_set(int nb_queues, struct mark_set_config *app_cfg);
39 
40 /*
41  * Callback for arg action flavour
42  *
43  * @param [in]: parameter
44  * @config [in]: app dpdk config
45  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
46  */
47 static doca_error_t action_type_param_callback(void *param, void *config)
48 {
49  struct mark_set_config *app_config = config;
50  const char *str = param;
51 
52  if (strcmp(str, "changeable") == 0)
53  app_config->action_changeable = true;
54  else if (strcmp(str, "specific") == 0)
55  app_config->action_changeable = false;
56  else {
57  DOCA_LOG_ERR("Unknown action-type '%s' was specified", str);
59  }
60 
61  return DOCA_SUCCESS;
62 }
63 
64 /*
65  * Callback for arg action flavour
66  *
67  * @param [in]: parameter
68  * @config [in]: app dpdk config
69  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
70  */
71 static doca_error_t mark_value_param_callback(void *param, void *config)
72 {
73  struct mark_set_config *app_config = config;
74  const char *str = param;
75  char *endptr;
76 
77  app_config->mark_value = strtol(str, &endptr, 0);
78  if (*endptr) {
79  DOCA_LOG_ERR("Invalid value for mark val: %s", str);
80  return -EINVAL;
81  }
82 
83  return DOCA_SUCCESS;
84 }
85 
86 /*
87  * Register for mark_set params
88  *
89  * @return: EXIT_SUCCESS on success and EXIT_FAILURE otherwise
90  */
91 static int register_app_params(void)
92 {
94  struct doca_argp_param *action_type_param;
95  struct doca_argp_param *mark_value_param;
96 
97  /* Create and register mark-type paras */
98  result = doca_argp_param_create(&action_type_param);
99  if (result != DOCA_SUCCESS) {
100  DOCA_LOG_ERR("Failed to create mark-type ARGP param: %s", doca_error_get_descr(result));
101  return result;
102  }
103 
104  doca_argp_param_set_short_name(action_type_param, "type");
105  doca_argp_param_set_long_name(action_type_param, "action-type");
106  doca_argp_param_set_arguments(action_type_param, "<action-type>");
107  doca_argp_param_set_description(action_type_param, "Set action-type (\"changeable\", \"specific\")");
110  result = doca_argp_register_param(action_type_param);
111  if (result != DOCA_SUCCESS) {
112  DOCA_LOG_ERR("Failed to register program param: %s", doca_error_get_descr(result));
113  return result;
114  }
115 
116  /* Create and register markt value param */
117  result = doca_argp_param_create(&mark_value_param);
118  if (result != DOCA_SUCCESS) {
119  DOCA_LOG_ERR("Failed to create mark value ARGP param: %s", doca_error_get_descr(result));
120  return result;
121  }
122  doca_argp_param_set_short_name(mark_value_param, "markval");
123  doca_argp_param_set_long_name(mark_value_param, "mark-value");
124  doca_argp_param_set_arguments(mark_value_param, "<mark-value>");
125  doca_argp_param_set_description(mark_value_param, "Set the mark value");
128  result = doca_argp_register_param(mark_value_param);
129  if (result != DOCA_SUCCESS) {
130  DOCA_LOG_ERR("Failed to register program param: %s", doca_error_get_descr(result));
131  return result;
132  }
133 
134  return 0;
135 }
136 
137 /*
138  * Sample main function
139  *
140  * @argc [in]: command line arguments size
141  * @argv [in]: array of command line arguments
142  * @return: EXIT_SUCCESS on success and EXIT_FAILURE otherwise
143  */
144 int main(int argc, char **argv)
145 {
147  struct doca_log_backend *sdk_log;
148  int exit_status = EXIT_FAILURE;
149  struct application_dpdk_config dpdk_config = {
150  .port_config.nb_ports = 2,
151  .port_config.nb_queues = 4,
152  .port_config.nb_hairpin_q = 1,
153  .reserve_main_thread = true,
154  };
155  struct mark_set_config app_cfg = {.action_changeable = false, .mark_value = 0x123456};
156 
157  /* Register a logger backend */
159  if (result != DOCA_SUCCESS)
160  goto sample_exit;
161 
162  /* Register a logger backend for internal SDK errors and warnings */
163  result = doca_log_backend_create_with_file_sdk(stderr, &sdk_log);
164  if (result != DOCA_SUCCESS)
165  goto sample_exit;
167  if (result != DOCA_SUCCESS)
168  goto sample_exit;
169 
170  DOCA_LOG_INFO("Starting the sample");
171 
173  if (result != DOCA_SUCCESS) {
174  DOCA_LOG_ERR("Failed to init ARGP resources: %s", doca_error_get_descr(result));
175  goto sample_exit;
176  }
178  if (result != DOCA_SUCCESS) {
179  DOCA_LOG_ERR("Failed to register Flow parameters: %s", doca_error_get_descr(result));
180  goto argp_cleanup;
181  }
183  result = doca_argp_start(argc, argv);
184  if (result != DOCA_SUCCESS) {
185  DOCA_LOG_ERR("Failed to parse sample input: %s", doca_error_get_descr(result));
186  goto argp_cleanup;
187  }
188 
189  /* update queues and ports */
190  result = dpdk_queues_and_ports_init(&dpdk_config);
191  if (result != DOCA_SUCCESS) {
192  DOCA_LOG_ERR("Failed to update ports and queues");
193  goto dpdk_cleanup;
194  }
195 
196  /* run sample */
198  if (result != DOCA_SUCCESS) {
199  DOCA_LOG_ERR("flow_mark_set() encountered an error: %s", doca_error_get_descr(result));
200  goto dpdk_ports_queues_cleanup;
201  }
202 
203  exit_status = EXIT_SUCCESS;
204 
205 dpdk_ports_queues_cleanup:
206  dpdk_queues_and_ports_fini(&dpdk_config);
207 dpdk_cleanup:
208  dpdk_fini();
209 argp_cleanup:
211 sample_exit:
212  if (exit_status == EXIT_SUCCESS)
213  DOCA_LOG_INFO("Sample finished successfully");
214  else
215  DOCA_LOG_INFO("Sample finished with errors");
216  return exit_status;
217 }
#define NULL
Definition: __stddef_null.h:26
int32_t result
doca_error_t dpdk_init(int argc, char **argv)
Definition: dpdk_utils.c:907
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
int main(int argc, char **argv)
static doca_error_t mark_value_param_callback(void *param, void *config)
DOCA_LOG_REGISTER(FLOW_MARK_SET::MAIN)
doca_error_t flow_mark_set(int nb_queues, struct mark_set_config *app_cfg)
static int register_app_params(void)
static doca_error_t action_type_param_callback(void *param, void *config)
static struct app_gpu_cfg app_cfg
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