NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
flow_switch_hot_upgrade_main.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2023-2024 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 #include <doca_dpdk.h>
32 
33 #include <dpdk_utils.h>
34 
35 #include "flow_switch_common.h"
36 
37 DOCA_LOG_REGISTER(FLOW_SWITCH_HOT_UPGRADE::MAIN);
38 
39 /* Sample's Logic */
41  int nb_ports,
42  struct doca_dev *dev_main,
43  struct doca_dev *dev_sec,
45 
46 /* doca flow hot upgrade context */
48  struct flow_switch_ctx switch_ctx; /* common switch context */
49  enum doca_flow_port_operation_state state; /* operation state to use after port configuration */
50 };
51 
52 /*
53  * Get DOCA Flow Hot Upgrade directory path.
54  *
55  * @param [in]: input parameter
56  * @config [out]: configuration context
57  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise.
58  */
60 {
61  struct flow_hot_upgrade_ctx *ctx = (struct flow_hot_upgrade_ctx *)config;
62  int state = *(int *)param;
63 
64  ctx->state = state;
65  DOCA_LOG_DBG("Operation state %d is configured for current instance", state);
66 
67  return DOCA_SUCCESS;
68 }
69 
70 /*
71  * Register DOCA Flow extra parameters.
72  *
73  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise.
74  */
76 {
78  struct doca_argp_param *state_param;
79 
80  /* Register common switch sample parameters */
82  if (result != DOCA_SUCCESS) {
83  DOCA_LOG_ERR("Failed to register flow switch parameters: %s", doca_error_get_descr(result));
84  return result;
85  }
86 
87  /* Register extra parameter for this sample */
88  result = doca_argp_param_create(&state_param);
89  if (result != DOCA_SUCCESS) {
90  DOCA_LOG_ERR("Failed to create flow hot upgrade operation state ARGP param: %s",
92  return result;
93  }
94  doca_argp_param_set_short_name(state_param, "s");
95  doca_argp_param_set_long_name(state_param, "state");
97  state_param,
98  "Set the (numeric) operation state for the ports <0=ACTIVE, 1=ACTIVE_READY_TO_SWAP, 2=STANDBY, 3=UNCONNECTED>");
101  result = doca_argp_register_param(state_param);
102  if (result != DOCA_SUCCESS) {
103  DOCA_LOG_ERR("Failed to register flow hot upgrade operation state ARGP param: %s",
105  return result;
106  }
107 
108  return DOCA_SUCCESS;
109 }
110 
111 /*
112  * Sample main function
113  *
114  * @argc [in]: command line arguments size
115  * @argv [in]: array of command line arguments
116  * @return: EXIT_SUCCESS on success and EXIT_FAILURE otherwise
117  */
118 int main(int argc, char **argv)
119 {
121  struct doca_log_backend *sdk_log;
122  int exit_status = EXIT_FAILURE;
123  struct application_dpdk_config dpdk_config = {
124  .port_config.nb_ports = 6,
125  .port_config.nb_queues = 1,
126  .port_config.isolated_mode = 1,
127  };
128  struct flow_hot_upgrade_ctx ctx = {0};
129 
130  /* Register a logger backend */
132  if (result != DOCA_SUCCESS)
133  goto sample_exit;
134 
135  /* Register a logger backend for internal SDK errors and warnings */
136  result = doca_log_backend_create_with_file_sdk(stderr, &sdk_log);
137  if (result != DOCA_SUCCESS)
138  goto sample_exit;
140  if (result != DOCA_SUCCESS)
141  goto sample_exit;
142 
143  DOCA_LOG_INFO("Starting the sample");
144 
145  result = doca_argp_init(NULL, &ctx.switch_ctx);
146  if (result != DOCA_SUCCESS) {
147  DOCA_LOG_ERR("Failed to init ARGP resources: %s", doca_error_get_descr(result));
148  goto sample_exit;
149  }
150 
152  if (result != DOCA_SUCCESS) {
153  DOCA_LOG_ERR("Failed to register extra parameters: %s", doca_error_get_descr(result));
154  goto argp_cleanup;
155  }
156 
158  result = doca_argp_start(argc, argv);
159  if (result != DOCA_SUCCESS) {
160  DOCA_LOG_ERR("Failed to parse sample input: %s", doca_error_get_descr(result));
161  goto argp_cleanup;
162  }
163 
164  result = init_doca_flow_switch_common(&ctx.switch_ctx);
165  if (result != DOCA_SUCCESS) {
166  DOCA_LOG_ERR("Failed to init flow switch common: %s", doca_error_get_descr(result));
167  goto dpdk_cleanup;
168  }
169 
170  /* update queues and ports */
171  result = dpdk_queues_and_ports_init(&dpdk_config);
172  if (result != DOCA_SUCCESS) {
173  DOCA_LOG_ERR("Failed to update ports and queues: %s", doca_error_get_descr(result));
174  goto dpdk_cleanup;
175  }
176 
177  /* run sample */
179  dpdk_config.port_config.nb_ports,
180  ctx.switch_ctx.doca_dev[0],
181  ctx.switch_ctx.doca_dev[1],
182  ctx.state);
183  if (result != DOCA_SUCCESS) {
184  DOCA_LOG_ERR("flow_switch_hot_upgrade() encountered an error: %s", doca_error_get_descr(result));
185  goto dpdk_ports_queues_cleanup;
186  }
187 
188  exit_status = EXIT_SUCCESS;
189 
190 dpdk_ports_queues_cleanup:
191  dpdk_queues_and_ports_fini(&dpdk_config);
192 dpdk_cleanup:
193  dpdk_fini();
194 argp_cleanup:
196 sample_exit:
198  if (exit_status == EXIT_SUCCESS)
199  DOCA_LOG_INFO("Sample finished successfully");
200  else
201  DOCA_LOG_INFO("Sample finished with errors");
202  return exit_status;
203 }
#define NULL
Definition: __stddef_null.h:26
int32_t 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
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_error_t register_extra_params(void)
DOCA_LOG_REGISTER(FLOW_SWITCH_HOT_UPGRADE::MAIN)
int main(int argc, char **argv)
doca_error_t flow_switch_hot_upgrade(int nb_queues, int nb_ports, struct doca_dev *dev_main, struct doca_dev *dev_sec, enum doca_flow_port_operation_state state)
static doca_error_t param_flow_hot_upgrade_operation_state_callback(void *param, void *config)
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 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_INT
Definition: doca_argp.h:57
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_SUCCESS
Definition: doca_error.h:38
doca_flow_port_operation_state
Defines the operation states for a port instance.
Definition: doca_flow.h:1147
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.
#define DOCA_LOG_DBG(format,...)
Generates a DEBUG application log message.
Definition: doca_log.h:496
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 switch_ctx
enum doca_flow_port_operation_state state
static int nb_ports
Definition: switch_core.c:44
struct upf_accel_ctx * ctx