NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
simple_fwd_vnf.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021-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 <stdint.h>
27 #include <signal.h>
28 
29 #include <rte_cycles.h>
30 #include <rte_launch.h>
31 #include <rte_ethdev.h>
32 
33 #include <doca_argp.h>
34 #include <doca_flow_tune_server.h>
35 #include <doca_log.h>
36 
37 #include <dpdk_utils.h>
38 #include <utils.h>
39 
40 #include "simple_fwd.h"
41 #include "simple_fwd_port.h"
42 #include "simple_fwd_vnf_core.h"
43 
44 DOCA_LOG_REGISTER(SIMPLE_FWD_VNF);
45 
46 #define DEFAULT_NB_METERS (1 << 13) /* Maximum number of meters used */
47 
48 /*
49  * Signal handler
50  *
51  * @signum [in]: The signal received to handle
52  */
53 static void signal_handler(int signum)
54 {
55  if (signum == SIGINT || signum == SIGTERM) {
56  DOCA_LOG_INFO("Signal %d received, preparing to exit", signum);
58  }
59 }
60 
61 /*
62  * Simple forward VNF application main function
63  *
64  * @argc [in]: command line arguments size
65  * @argv [in]: array of command line arguments
66  * @return: EXIT_SUCCESS on success and EXIT_FAILURE otherwise
67  */
68 int main(int argc, char **argv)
69 {
71  int exit_status = EXIT_SUCCESS;
72  struct doca_log_backend *logger;
73  struct doca_log_backend *sdk_log;
74  struct simple_fwd_port_cfg port_cfg = {0};
75  struct application_dpdk_config dpdk_config = {
76  .port_config.nb_ports = 2,
77  .port_config.nb_queues = 4,
78  .port_config.nb_hairpin_q = 4,
79  .port_config.enable_mbuf_metadata = 1,
80  .reserve_main_thread = true,
81  };
82  struct simple_fwd_config app_cfg = {
83  .dpdk_cfg = &dpdk_config,
84  .rx_only = 0,
85  .hw_offload = 0,
86  .stats_timer = 100000,
87  .age_thread = false,
88  .is_hairpin = false,
89  };
90  struct app_vnf *vnf;
91  struct simple_fwd_process_pkts_params process_pkts_params = {.cfg = &app_cfg};
92  struct doca_flow_tune_server_cfg *server_cfg;
93 
94  /* Register a logger backend */
96  if (result != DOCA_SUCCESS)
97  return EXIT_FAILURE;
98 
99  /* Register a logger backend for internal SDK errors and warnings */
100  result = doca_log_backend_create_with_file_sdk(stderr, &sdk_log);
101  if (result != DOCA_SUCCESS)
102  return EXIT_FAILURE;
104  if (result != DOCA_SUCCESS)
105  return EXIT_FAILURE;
106 
107  /* Parse cmdline/json arguments */
109  if (result != DOCA_SUCCESS) {
110  DOCA_LOG_ERR("Failed to init ARGP resources: %s", doca_error_get_descr(result));
111  return EXIT_FAILURE;
112  }
115  if (result != DOCA_SUCCESS) {
116  DOCA_LOG_ERR("Failed to register application params: %s", doca_error_get_descr(result));
118  return EXIT_FAILURE;
119  }
120  result = doca_argp_start(argc, argv);
121  if (result != DOCA_SUCCESS) {
122  DOCA_LOG_ERR("Failed to parse application input: %s", doca_error_get_descr(result));
124  return EXIT_FAILURE;
125  }
126 
127  result = doca_log_backend_create_with_syslog("doca_core", &logger);
128  if (result != DOCA_SUCCESS) {
129  DOCA_LOG_ERR("Failed to allocate the logger");
131  return EXIT_FAILURE;
132  }
133 
134  /* update queues and ports */
135  result = dpdk_queues_and_ports_init(&dpdk_config);
136  if (result != DOCA_SUCCESS) {
137  DOCA_LOG_ERR("Failed to update application ports and queues: %s", doca_error_get_descr(result));
138  exit_status = EXIT_FAILURE;
139  goto dpdk_destroy;
140  }
141 
142  signal(SIGINT, signal_handler);
143  signal(SIGTERM, signal_handler);
144 
145  /* convert to number of cycles */
146  app_cfg.stats_timer *= rte_get_timer_hz();
147 
148  vnf = simple_fwd_get_vnf();
149  port_cfg.nb_queues = dpdk_config.port_config.nb_queues;
150  port_cfg.is_hairpin = app_cfg.is_hairpin;
151  port_cfg.nb_meters = DEFAULT_NB_METERS;
152  port_cfg.nb_counters = (1 << 13);
153  port_cfg.age_thread = app_cfg.age_thread;
154  if (vnf->vnf_init(&port_cfg) != 0) {
155  DOCA_LOG_ERR("VNF application init error");
156  exit_status = EXIT_FAILURE;
157  goto exit_app;
158  }
159 
160  /* Init DOCA Flow Tune Server */
162  if (result != DOCA_SUCCESS) {
163  DOCA_LOG_ERR("Failed to create flow tune server configuration");
164  return result;
165  }
166  result = doca_flow_tune_server_init(server_cfg);
167  if (result != DOCA_SUCCESS) {
169  DOCA_LOG_DBG("DOCA Flow Tune Server isn't supported in this runtime version");
170  } else {
171  DOCA_LOG_ERR("Failed to initialize the flow tune server");
173  return result;
174  }
175  }
177 
179  process_pkts_params.vnf = vnf;
180  rte_eal_mp_remote_launch(simple_fwd_process_pkts, &process_pkts_params, CALL_MAIN);
181  rte_eal_mp_wait_lcore();
182 exit_app:
183  /* cleanup app resources */
184  simple_fwd_destroy(vnf);
186 
187  /* DPDK cleanup resources */
188  dpdk_queues_and_ports_fini(&dpdk_config);
189 dpdk_destroy:
190  dpdk_fini();
191 
192  /* ARGP cleanup */
194 
195  return exit_status;
196 }
#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
static struct app_gpu_cfg app_cfg
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_ERROR_NOT_SUPPORTED
Definition: doca_error.h:42
@ DOCA_SUCCESS
Definition: doca_error.h:38
DOCA_EXPERIMENTAL doca_error_t doca_flow_tune_server_init(struct doca_flow_tune_server_cfg *cfg)
Initialize a DOCA Flow Tune Server.
DOCA_EXPERIMENTAL doca_error_t doca_flow_tune_server_cfg_destroy(struct doca_flow_tune_server_cfg *cfg)
Destroy DOCA Flow Tune Server configuration struct.
DOCA_EXPERIMENTAL doca_error_t doca_flow_tune_server_cfg_create(struct doca_flow_tune_server_cfg **cfg)
Create DOCA Flow Tune Server configuration struct.
DOCA_EXPERIMENTAL void doca_flow_tune_server_destroy(void)
Destroy the DOCA Flow Tune Server.
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_syslog(const char *name, struct doca_log_backend **backend)
Create a logging backend for application messages with a syslog output.
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 app_vnf * simple_fwd_get_vnf(void)
Definition: simple_fwd.c:1536
static int simple_fwd_destroy(void)
Definition: simple_fwd.c:344
#define DEFAULT_NB_METERS
int main(int argc, char **argv)
DOCA_LOG_REGISTER(SIMPLE_FWD_VNF)
static void signal_handler(int signum)
int simple_fwd_process_pkts(void *process_pkts_params)
void simple_fwd_process_pkts_stop(void)
void simple_fwd_map_queue(uint16_t nb_queues)
doca_error_t register_simple_fwd_params(void)
int(* vnf_init)(void *p)
Definition: app_vnf.h:35
struct application_port_config port_config
Definition: dpdk_utils.h:70
struct simple_fwd_config * cfg