NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
flow_ct_2_ports_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 <stdlib.h>
27 
28 #include <doca_argp.h>
29 #include <doca_log.h>
30 #include <doca_dpdk.h>
31 
32 #include <dpdk_utils.h>
33 
34 #include "flow_ct_common.h"
35 #include "common.h"
36 
37 DOCA_LOG_REGISTER(FLOW_CT_2_PORTS::MAIN);
38 
39 /* Sample's Logic */
40 doca_error_t flow_ct_2_ports(uint16_t nb_queues, struct doca_dev *dev_arr[], int nb_ports);
41 
42 /*
43  * Sample main function
44  *
45  * @argc [in]: command line arguments size
46  * @argv [in]: array of command line arguments
47  * @return: EXIT_SUCCESS on success and EXIT_FAILURE otherwise
48  */
49 int main(int argc, char **argv)
50 {
52  struct doca_log_backend *sdk_log;
53  int exit_status = EXIT_FAILURE;
54  struct doca_dev *ct_dev[MAX_PORTS] = {};
55  struct ct_config ct_cfg = {0};
56  struct application_dpdk_config dpdk_config = {
58  .port_config.isolated_mode = 1,
59  .port_config.switch_mode = 1,
60  .port_config.nb_hairpin_q = 2,
61  .port_config.self_hairpin = 1,
62  .reserve_main_thread = false,
63  };
64  int i;
65 
66  /* Register a logger backend */
68  if (result != DOCA_SUCCESS)
69  goto sample_exit;
70 
71  /* Register a logger backend for internal SDK errors and warnings */
73  if (result != DOCA_SUCCESS)
74  goto sample_exit;
76  if (result != DOCA_SUCCESS)
77  goto sample_exit;
78 
79  DOCA_LOG_INFO("Starting the sample");
80 
81  result = doca_argp_init(NULL, &ct_cfg);
82  if (result != DOCA_SUCCESS) {
83  DOCA_LOG_ERR("Failed to init ARGP resources: %s", doca_error_get_descr(result));
84  goto sample_exit;
85  }
87 
89  if (result != DOCA_SUCCESS) {
90  DOCA_LOG_ERR("Failed to register Flow Ct sample parameters: %s", doca_error_get_descr(result));
91  goto argp_cleanup;
92  }
93 
94  result = doca_argp_start(argc, argv);
95  if (result != DOCA_SUCCESS) {
96  DOCA_LOG_ERR("Failed to parse sample input: %s", doca_error_get_descr(result));
97  goto argp_cleanup;
98  }
99 
100  for (i = 0; i < ct_cfg.n_ports; i++) {
102  if (result != DOCA_SUCCESS) {
103  DOCA_LOG_ERR("Failed to open Flow CT device: %s", doca_error_get_descr(result));
104  goto dpdk_cleanup;
105  }
106 
108  if (result != DOCA_SUCCESS) {
109  DOCA_LOG_ERR("Failed to open Flow CT device: %s", doca_error_get_descr(result));
110  goto device_cleanup;
111  }
112  }
113 
114  /* update queues and ports */
115  dpdk_config.port_config.nb_ports = ct_cfg.n_ports;
116  result = dpdk_queues_and_ports_init(&dpdk_config);
117  if (result != DOCA_SUCCESS) {
118  DOCA_LOG_ERR("Failed to update ports and queues");
119  goto device_cleanup;
120  }
121 
122  /* run sample */
123  result = flow_ct_2_ports(dpdk_config.port_config.nb_queues, ct_dev, ct_cfg.n_ports);
124  if (result != DOCA_SUCCESS) {
125  DOCA_LOG_ERR("Encountered an error: %s", doca_error_get_descr(result));
126  goto dpdk_ports_queues_cleanup;
127  }
128 
129  exit_status = EXIT_SUCCESS;
130 dpdk_ports_queues_cleanup:
131  dpdk_queues_and_ports_fini(&dpdk_config);
132 device_cleanup:
133  for (i = 0; i < ct_cfg.n_ports; i++)
134  if (ct_dev[i] != NULL)
135  doca_dev_close(ct_dev[i]);
136 dpdk_cleanup:
137  dpdk_fini();
138 argp_cleanup:
140 sample_exit:
141  if (exit_status == EXIT_SUCCESS)
142  DOCA_LOG_INFO("Sample finished successfully");
143  else
144  DOCA_LOG_INFO("Sample finished with errors");
145  return exit_status;
146 }
#define NULL
Definition: __stddef_null.h:26
int32_t result
static doca_error_t open_doca_device_with_pci(const char *pcie_value, struct doca_dev **retval)
Definition: device.c:43
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)
doca_error_t flow_ct_2_ports(uint16_t nb_queues, struct doca_dev *dev_arr[], int nb_ports)
DOCA_LOG_REGISTER(FLOW_CT_2_PORTS::MAIN)
doca_error_t flow_ct_dpdk_init(int argc, char **dpdk_argv)
doca_error_t flow_ct_capable(struct doca_devinfo *dev_info)
doca_error_t flow_ct_register_params(void)
#define MAX_PORTS
#define FLOW_CT_COMMON_DEVARGS
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.
DOCA_STABLE doca_error_t doca_dev_close(struct doca_dev *dev)
Destroy allocated local device instance.
DOCA_EXPERIMENTAL doca_error_t doca_dpdk_port_probe(struct doca_dev *dev, const char *devargs)
Attach a DPDK port specified by DOCA device.
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
#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
char ct_dev_pci_addr[MAX_PORTS][DOCA_DEVINFO_PCI_ADDR_SIZE]
static int nb_ports
Definition: switch_core.c:44