NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
eth_txq_batch_send_ethernet_frames_main.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 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 #include <stdint.h>
28 #include <string.h>
29 
30 #include <doca_argp.h>
31 #include <doca_dev.h>
32 #include <doca_error.h>
33 #include <doca_log.h>
34 
35 #include "eth_common.h"
36 
37 DOCA_LOG_REGISTER(ETH_TXQ_BATCH_SEND_ETHERNET_FRAMES::MAIN);
38 
39 /* Configuration struct */
40 struct eth_txq_cfg {
41  char ib_dev_name[DOCA_DEVINFO_IBDEV_NAME_SIZE]; /* DOCA IB device name */
42  uint8_t dest_mac_address[DOCA_DEVINFO_MAC_ADDR_SIZE]; /* destination MAC address */
43 };
44 
45 /* Sample's Logic */
46 doca_error_t eth_txq_batch_send_ethernet_frames(const char *ib_dev_name, uint8_t *dest_mac_addr);
47 
48 /*
49  * ARGP Callback - Handle IB device name parameter
50  *
51  * @param [in]: Input parameter
52  * @config [out]: Program configuration context
53  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
54  */
55 static doca_error_t ibdev_name_callback(void *param, void *config)
56 {
57  struct eth_txq_cfg *eth_txq_cfg = (struct eth_txq_cfg *)config;
58 
59  return extract_ibdev_name((char *)param, eth_txq_cfg->ib_dev_name);
60 }
61 
62 /*
63  * ARGP Callback - Handle MAC address parameter
64  *
65  * @param [in]: Input parameter
66  * @config [out]: Program configuration context
67  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
68  */
69 static doca_error_t mac_addr_callback(void *param, void *config)
70 {
71  struct eth_txq_cfg *eth_txq_cfg = (struct eth_txq_cfg *)config;
72 
73  return extract_mac_addr((char *)param, eth_txq_cfg->dest_mac_address);
74 }
75 
76 /*
77  * Register the command line parameters for the sample.
78  *
79  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
80  */
82 {
84  struct doca_argp_param *dev_ib_name_param, *mac_param;
85 
86  result = doca_argp_param_create(&dev_ib_name_param);
87  if (result != DOCA_SUCCESS) {
88  DOCA_LOG_ERR("Failed to create ARGP param: %s", doca_error_get_descr(result));
89  return result;
90  }
91 
92  doca_argp_param_set_short_name(dev_ib_name_param, "d");
93  doca_argp_param_set_long_name(dev_ib_name_param, "device");
94  doca_argp_param_set_description(dev_ib_name_param, "IB device name - default: mlx5_0");
97  result = doca_argp_register_param(dev_ib_name_param);
98  if (result != DOCA_SUCCESS) {
99  DOCA_LOG_ERR("Failed to register program param: %s", doca_error_get_descr(result));
100  return result;
101  }
102 
103  result = doca_argp_param_create(&mac_param);
104  if (result != DOCA_SUCCESS) {
105  DOCA_LOG_ERR("Failed to create ARGP param: %s", doca_error_get_descr(result));
106  return result;
107  }
108  doca_argp_param_set_short_name(mac_param, "m");
109  doca_argp_param_set_long_name(mac_param, "mac-addr");
111  mac_param,
112  "Destination MAC address to associate with the ethernet frames - default: FF:FF:FF:FF:FF:FF");
115  result = doca_argp_register_param(mac_param);
116  if (result != DOCA_SUCCESS) {
117  DOCA_LOG_ERR("Failed to register program param: %s", doca_error_get_descr(result));
118  return result;
119  }
120 
121  return DOCA_SUCCESS;
122 }
123 
124 /*
125  * Sample main function
126  *
127  * @argc [in]: command line arguments size
128  * @argv [in]: array of command line arguments
129  * @return: EXIT_SUCCESS on success and EXIT_FAILURE otherwise
130  */
131 int main(int argc, char **argv)
132 {
134  struct eth_txq_cfg eth_txq_cfg;
135  struct doca_log_backend *sdk_log;
136  int exit_status = EXIT_FAILURE;
137  int i;
138 
139  /* Register a logger backend */
141  if (result != DOCA_SUCCESS)
142  goto sample_exit;
143 
144  /* Register a logger backend for internal SDK errors and warnings */
145  result = doca_log_backend_create_with_file_sdk(stderr, &sdk_log);
146  if (result != DOCA_SUCCESS)
147  goto sample_exit;
149  if (result != DOCA_SUCCESS)
150  goto sample_exit;
151 
152  strcpy(eth_txq_cfg.ib_dev_name, "mlx5_0");
153  for (i = 0; i < DOCA_DEVINFO_MAC_ADDR_SIZE; i++)
154  eth_txq_cfg.dest_mac_address[i] = 0xff;
155 
157  if (result != DOCA_SUCCESS) {
158  DOCA_LOG_ERR("Failed to init ARGP resources: %s", doca_error_get_descr(result));
159  goto sample_exit;
160  }
161 
163  if (result != DOCA_SUCCESS) {
164  DOCA_LOG_ERR("Failed to register ARGP params: %s", doca_error_get_descr(result));
165  goto argp_cleanup;
166  }
167 
168  result = doca_argp_start(argc, argv);
169  if (result != DOCA_SUCCESS) {
170  DOCA_LOG_ERR("Failed to parse sample input: %s", doca_error_get_descr(result));
171  goto argp_cleanup;
172  }
173 
175  if (result != DOCA_SUCCESS) {
176  DOCA_LOG_ERR("eth_txq_batch_send_ethernet_frames() encountered an error: %s",
178  goto argp_cleanup;
179  }
180 
181  exit_status = EXIT_SUCCESS;
182 
183 argp_cleanup:
185 sample_exit:
186  if (exit_status == EXIT_SUCCESS)
187  DOCA_LOG_INFO("Sample finished successfully");
188  else
189  DOCA_LOG_INFO("Sample finished with errors");
190  return exit_status;
191 }
#define NULL
Definition: __stddef_null.h:26
int32_t result
doca_error_t extract_ibdev_name(char *ibdev_name, char *ibdev_name_out)
Definition: eth_common.c:128
doca_error_t extract_mac_addr(char *mac_addr, uint8_t *mac_addr_out)
Definition: eth_common.c:146
static doca_error_t ibdev_name_callback(void *param, void *config)
int main(int argc, char **argv)
static doca_error_t register_eth_txq_params(void)
static doca_error_t mac_addr_callback(void *param, void *config)
doca_error_t eth_txq_batch_send_ethernet_frames(const char *ib_dev_name, uint8_t *dest_mac_addr)
DOCA_LOG_REGISTER(ETH_TXQ_BATCH_SEND_ETHERNET_FRAMES::MAIN)
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_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
#define DOCA_DEVINFO_IBDEV_NAME_SIZE
Buffer size to hold Infiniband/RoCE device name. Including a null terminator.
Definition: doca_dev.h:309
#define DOCA_DEVINFO_MAC_ADDR_SIZE
Length of MAC address.
Definition: doca_dev.h:301
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
uint8_t dest_mac_address[DOCA_DEVINFO_MAC_ADDR_SIZE]
char ib_dev_name[DOCA_DEVINFO_IBDEV_NAME_SIZE]