NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
switch.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2022-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 <doca_argp.h>
27 #include <doca_log.h>
28 
29 #include "dpdk_utils.h"
30 #include "flow_switch_common.h"
31 
32 #include "switch_core.h"
33 
35 
36 /*
37  * Switch application main function
38  *
39  * @argc [in]: command line arguments size
40  * @argv [in]: array of command line arguments
41  * @return: EXIT_SUCCESS on success and EXIT_FAILURE otherwise
42  */
43 int main(int argc, char **argv)
44 {
46  int exit_status = EXIT_FAILURE;
47  struct doca_log_backend *sdk_log;
48  struct flow_switch_ctx ctx = {0};
49  struct application_dpdk_config dpdk_config = {0};
50 
51  /* Register a logger backend */
53  if (result != DOCA_SUCCESS)
54  return exit_status;
55 
56  /* Register a logger backend for internal SDK errors and warnings */
58  if (result != DOCA_SUCCESS)
59  return exit_status;
61  if (result != DOCA_SUCCESS)
62  return exit_status;
63 
64  /* Parse cmdline/json arguments */
66  if (result != DOCA_SUCCESS) {
67  DOCA_LOG_ERR("Failed to init ARGP resources: %s", doca_error_get_descr(result));
68  return exit_status;
69  }
70 
72  if (result != DOCA_SUCCESS) {
73  DOCA_LOG_ERR("Failed to register application param: %s", doca_error_get_descr(result));
74  goto argp_cleanup;
75  }
76 
78  result = doca_argp_start(argc, argv);
79  if (result != DOCA_SUCCESS) {
80  DOCA_LOG_ERR("Failed to parse application input: %s", doca_error_get_descr(result));
81  goto argp_cleanup;
82  }
83 
85  if (result != DOCA_SUCCESS) {
86  DOCA_LOG_ERR("Failed to init application param: %s", doca_error_get_descr(result));
87  goto dpdk_destroy;
88  }
89 
90  dpdk_config.port_config.nb_ports = get_dpdk_nb_ports();
91  dpdk_config.port_config.switch_mode = 1;
92  dpdk_config.port_config.isolated_mode = 1;
93 
94  /* Update queues and ports */
95  result = dpdk_queues_and_ports_init(&dpdk_config);
96  if (result != DOCA_SUCCESS) {
97  DOCA_LOG_ERR("Failed to update application ports and queues: %s", doca_error_get_descr(result));
98  exit_status = EXIT_FAILURE;
99  goto dpdk_destroy;
100  }
101 
102  /* Init switch */
103  result = switch_init(&dpdk_config, &ctx);
104  if (result != DOCA_SUCCESS) {
105  exit_status = EXIT_FAILURE;
106  goto dpdk_cleanup;
107  }
108 
109  /* Initiate Flow Parser */
110  result = flow_parser_init("SWITCH>> ", false);
111  if (result != DOCA_SUCCESS) {
112  exit_status = EXIT_FAILURE;
113  goto switch_cleanup;
114  }
115 
116  exit_status = EXIT_SUCCESS;
117 
118  /* Clean Flow Parser structures */
120 
121 switch_cleanup:
122  /* Closing and releasing switch resources */
123  switch_destroy();
124 dpdk_cleanup:
125  /* DPDK ports cleanup */
126  dpdk_queues_and_ports_fini(&dpdk_config);
127 dpdk_destroy:
128  /* DPDK cleanup + device closure */
129  dpdk_fini();
131 argp_cleanup:
133 
134  return exit_status;
135 }
#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 flow_parser_init(char *shell_prompt, bool fw_subset)
Definition: flow_parser.c:2328
void flow_parser_cleanup(void)
Definition: flow_parser.c:2347
doca_error_t init_doca_flow_switch_common(struct flow_switch_ctx *ctx)
uint8_t get_dpdk_nb_ports(void)
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 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 doca_error_t doca_argp_destroy(void)
ARG Parser destroy.
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_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
struct application_port_config port_config
Definition: dpdk_utils.h:70
int main(int argc, char **argv)
Definition: switch.c:43
DOCA_LOG_REGISTER(SWITCH)
doca_error_t switch_init(struct application_dpdk_config *app_dpdk_config, struct flow_switch_ctx *ctx)
Definition: switch_core.c:382
void switch_destroy(void)
Definition: switch_core.c:433
struct upf_accel_ctx * ctx