NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
eth_txq_send_ethernet_frames_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 #include <string.h>
28 
29 #include <doca_argp.h>
30 #include <doca_dev.h>
31 #include <doca_error.h>
32 #include <doca_log.h>
33 
34 #include "eth_common.h"
35 
36 DOCA_LOG_REGISTER(ETH_TXQ_SEND_ETHERNET_FRAMES::MAIN);
37 
38 /* Configuration struct */
39 struct eth_txq_cfg {
40  char ib_dev_name[DOCA_DEVINFO_IBDEV_NAME_SIZE]; /* DOCA IB device name */
41  uint8_t dest_mac_address[DOCA_DEVINFO_MAC_ADDR_SIZE]; /* destination MAC address */
42 };
43 
44 /* Sample's Logic */
45 doca_error_t eth_txq_send_ethernet_frames(const char *ib_dev_name, uint8_t *dest_mac_addr);
46 
47 /*
48  * ARGP Callback - Handle IB device name parameter
49  *
50  * @param [in]: Input parameter
51  * @config [out]: Program configuration context
52  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
53  */
54 static doca_error_t ibdev_name_callback(void *param, void *config)
55 {
56  struct eth_txq_cfg *eth_txq_cfg = (struct eth_txq_cfg *)config;
57 
58  return extract_ibdev_name((char *)param, eth_txq_cfg->ib_dev_name);
59 }
60 
61 /*
62  * ARGP Callback - Handle MAC address parameter
63  *
64  * @param [in]: Input parameter
65  * @config [out]: Program configuration context
66  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
67  */
68 static doca_error_t mac_addr_callback(void *param, void *config)
69 {
70  struct eth_txq_cfg *eth_txq_cfg = (struct eth_txq_cfg *)config;
71 
72  return extract_mac_addr((char *)param, eth_txq_cfg->dest_mac_address);
73 }
74 
75 /*
76  * Register the command line parameters for the sample.
77  *
78  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
79  */
81 {
83  struct doca_argp_param *dev_ib_name_param, *mac_param;
84 
85  result = doca_argp_param_create(&dev_ib_name_param);
86  if (result != DOCA_SUCCESS) {
87  DOCA_LOG_ERR("Failed to create ARGP param: %s", doca_error_get_descr(result));
88  return result;
89  }
90 
91  doca_argp_param_set_short_name(dev_ib_name_param, "d");
92  doca_argp_param_set_long_name(dev_ib_name_param, "device");
93  doca_argp_param_set_description(dev_ib_name_param, "IB device name - default: mlx5_0");
96  result = doca_argp_register_param(dev_ib_name_param);
97  if (result != DOCA_SUCCESS) {
98  DOCA_LOG_ERR("Failed to register program param: %s", doca_error_get_descr(result));
99  return result;
100  }
101 
102  result = doca_argp_param_create(&mac_param);
103  if (result != DOCA_SUCCESS) {
104  DOCA_LOG_ERR("Failed to create ARGP param: %s", doca_error_get_descr(result));
105  return result;
106  }
107  doca_argp_param_set_short_name(mac_param, "m");
108  doca_argp_param_set_long_name(mac_param, "mac-addr");
110  mac_param,
111  "Destination MAC address to associate with the ethernet frames - default: FF:FF:FF:FF:FF:FF");
114  result = doca_argp_register_param(mac_param);
115  if (result != DOCA_SUCCESS) {
116  DOCA_LOG_ERR("Failed to register program param: %s", doca_error_get_descr(result));
117  return result;
118  }
119 
120  return DOCA_SUCCESS;
121 }
122 
123 /*
124  * Sample main function
125  *
126  * @argc [in]: command line arguments size
127  * @argv [in]: array of command line arguments
128  * @return: EXIT_SUCCESS on success and EXIT_FAILURE otherwise
129  */
130 int main(int argc, char **argv)
131 {
133  struct eth_txq_cfg eth_txq_cfg;
134  struct doca_log_backend *sdk_log;
135  int exit_status = EXIT_FAILURE;
136  int i;
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  strcpy(eth_txq_cfg.ib_dev_name, "mlx5_0");
152  for (i = 0; i < DOCA_DEVINFO_MAC_ADDR_SIZE; i++)
153  eth_txq_cfg.dest_mac_address[i] = 0xff;
154 
156  if (result != DOCA_SUCCESS) {
157  DOCA_LOG_ERR("Failed to init ARGP resources: %s", doca_error_get_descr(result));
158  goto sample_exit;
159  }
160 
162  if (result != DOCA_SUCCESS) {
163  DOCA_LOG_ERR("Failed to register ARGP params: %s", doca_error_get_descr(result));
164  goto argp_cleanup;
165  }
166 
167  result = doca_argp_start(argc, argv);
168  if (result != DOCA_SUCCESS) {
169  DOCA_LOG_ERR("Failed to parse sample input: %s", doca_error_get_descr(result));
170  goto argp_cleanup;
171  }
172 
174  if (result != DOCA_SUCCESS) {
175  DOCA_LOG_ERR("eth_txq_send_ethernet_frames() encountered an error: %s", doca_error_get_descr(result));
176  goto argp_cleanup;
177  }
178 
179  exit_status = EXIT_SUCCESS;
180 
181 argp_cleanup:
183 sample_exit:
184  if (exit_status == EXIT_SUCCESS)
185  DOCA_LOG_INFO("Sample finished successfully");
186  else
187  DOCA_LOG_INFO("Sample finished with errors");
188  return exit_status;
189 }
#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)
doca_error_t eth_txq_send_ethernet_frames(const char *ib_dev_name, uint8_t *dest_mac_addr)
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_LOG_REGISTER(ETH_TXQ_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]