NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
flow_loopback_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 <doca_argp.h>
27 #include <doca_flow.h>
28 #include <doca_log.h>
29 
30 #include <dpdk_utils.h>
31 
32 DOCA_LOG_REGISTER(FLOW_LOOPBACK::MAIN);
33 
34 #define NB_PORTS 2
35 #define MAC_ADDR_LEN 6
36 
37 /* Sample's Logic */
38 doca_error_t flow_loopback(int nb_queues, uint8_t mac_addresses[2][6]);
39 
40 /*
41  * ARGP Callback - Handle MAC addresses parameter
42  *
43  * @param [in]: Input parameter
44  * @opaque [in/out]: MAC address
45  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
46  */
47 static doca_error_t mac_addresses_callback(void *param, void *opaque)
48 {
49  char mac1[18];
50  char mac2[18];
51  uint8_t(*mac_addresses)[MAC_ADDR_LEN] = (uint8_t(*)[MAC_ADDR_LEN])opaque;
52  char *mac_addresses_param = (char *)param;
53 
54  /* Split the input into two MAC addresses */
55  if (sscanf(mac_addresses_param, "%17s %17s", mac1, mac2) != 2) {
56  DOCA_LOG_ERR("Invalid input format");
58  }
59 
60  /* Parse the first MAC address */
61  if (sscanf(mac1,
62  "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
63  &mac_addresses[0][0],
64  &mac_addresses[0][1],
65  &mac_addresses[0][2],
66  &mac_addresses[0][3],
67  &mac_addresses[0][4],
68  &mac_addresses[0][5]) != MAC_ADDR_LEN) {
69  DOCA_LOG_ERR("Invalid MAC address format: %s", mac1);
71  }
72 
73  /* Parse the second MAC address */
74  if (sscanf(mac2,
75  "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
76  &mac_addresses[1][0],
77  &mac_addresses[1][1],
78  &mac_addresses[1][2],
79  &mac_addresses[1][3],
80  &mac_addresses[1][4],
81  &mac_addresses[1][5]) != MAC_ADDR_LEN) {
82  DOCA_LOG_ERR("Invalid MAC address format: %s", mac2);
84  }
85  return DOCA_SUCCESS;
86 }
87 
88 /*
89  * Register the command line parameter for the sample
90  *
91  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
92  */
94 {
96  struct doca_argp_param *mac_addresses_param;
97 
98  result = doca_argp_param_create(&mac_addresses_param);
99  if (result != DOCA_SUCCESS) {
100  DOCA_LOG_ERR("Failed to create ARGP param: %s", doca_error_get_descr(result));
101  return result;
102  }
103  doca_argp_param_set_short_name(mac_addresses_param, "m");
104  doca_argp_param_set_long_name(mac_addresses_param, "mac-addresses");
105  doca_argp_param_set_description(mac_addresses_param, "The MAC addresses of the ports, used for encapsulation");
107  doca_argp_param_set_type(mac_addresses_param, DOCA_ARGP_TYPE_STRING);
108  result = doca_argp_register_param(mac_addresses_param);
109  if (result != DOCA_SUCCESS) {
110  DOCA_LOG_ERR("Failed to register program param: %s", doca_error_get_descr(result));
111  return result;
112  }
113 
114  return DOCA_SUCCESS;
115 }
116 
117 /*
118  * Sample main function
119  *
120  * @argc [in]: command line arguments size
121  * @argv [in]: array of command line arguments
122  * @return: EXIT_SUCCESS on success and EXIT_FAILURE otherwise
123  */
124 int main(int argc, char **argv)
125 {
127  struct doca_log_backend *sdk_log;
128  int exit_status = EXIT_FAILURE;
129  uint8_t mac_addresses[NB_PORTS][MAC_ADDR_LEN] = {};
130  struct application_dpdk_config dpdk_config = {
132  .port_config.nb_queues = 1,
133  .port_config.nb_hairpin_q = 1,
134  .port_config.enable_mbuf_metadata = 1,
135  .port_config.lpbk_support = 1,
136  };
137 
138  /* Register a logger backend */
140  if (result != DOCA_SUCCESS)
141  goto sample_exit;
142 
143  /* Register a logger backend for internal SDK errors and warnings */
144  result = doca_log_backend_create_with_file_sdk(stderr, &sdk_log);
145  if (result != DOCA_SUCCESS)
146  goto sample_exit;
148  if (result != DOCA_SUCCESS)
149  goto sample_exit;
150 
151  DOCA_LOG_INFO("Starting the sample");
152 
153  result = doca_argp_init(NULL, &mac_addresses);
154  if (result != DOCA_SUCCESS) {
155  DOCA_LOG_ERR("Failed to init ARGP resources: %s", doca_error_get_descr(result));
156  goto sample_exit;
157  }
160  if (result != DOCA_SUCCESS) {
161  DOCA_LOG_ERR("Failed to register samples params: %s", doca_error_get_descr(result));
163  return EXIT_FAILURE;
164  }
165 
166  result = doca_argp_start(argc, argv);
167  if (result != DOCA_SUCCESS) {
168  DOCA_LOG_ERR("Failed to parse sample input: %s", doca_error_get_descr(result));
169  goto argp_cleanup;
170  }
171 
172  /* update queues and ports */
173  result = dpdk_queues_and_ports_init(&dpdk_config);
174  if (result != DOCA_SUCCESS) {
175  DOCA_LOG_ERR("Failed to update ports and queues");
176  goto dpdk_cleanup;
177  }
178 
179  /* run sample */
180  result = flow_loopback(dpdk_config.port_config.nb_queues, mac_addresses);
181  if (result != DOCA_SUCCESS) {
182  DOCA_LOG_ERR("flow_loopback() encountered an error: %s", doca_error_get_descr(result));
183  goto dpdk_ports_queues_cleanup;
184  }
185 
186  exit_status = EXIT_SUCCESS;
187 
188 dpdk_ports_queues_cleanup:
189  dpdk_queues_and_ports_fini(&dpdk_config);
190 dpdk_cleanup:
191  dpdk_fini();
192 argp_cleanup:
194 sample_exit:
195  if (exit_status == EXIT_SUCCESS)
196  DOCA_LOG_INFO("Sample finished successfully");
197  else
198  DOCA_LOG_INFO("Sample finished with errors");
199  return exit_status;
200 }
#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 doca_error_t mac_addresses_callback(void *param, void *opaque)
int main(int argc, char **argv)
#define MAC_ADDR_LEN
#define NB_PORTS
DOCA_LOG_REGISTER(FLOW_LOOPBACK::MAIN)
doca_error_t register_flow_loopback_params(void)
doca_error_t flow_loopback(int nb_queues, uint8_t mac_addresses[2][6])
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_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_INVALID_VALUE
Definition: doca_error.h:44
@ 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