NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
sha_create_main.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2022-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_error.h>
31 #include <doca_dev.h>
32 #include <doca_sha.h>
33 #include <doca_log.h>
34 
35 #include <utils.h>
36 
37 DOCA_LOG_REGISTER(SHA_CREATE::MAIN);
38 
39 #define MAX_USER_DATA_LEN 1024 /* max user data length */
40 #define MAX_DATA_LEN (MAX_USER_DATA_LEN + 1) /* max data length */
41 #define MIN_USER_DATA_LEN 1 /* min user data length */
42 
43 /* Sample's Logic */
44 doca_error_t sha_create(char *src_buffer);
45 
46 /*
47  * ARGP Callback - Handle user data parameter
48  *
49  * @param [in]: Input parameter
50  * @config [in/out]: Program configuration context
51  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
52  */
53 static doca_error_t data_callback(void *param, void *config)
54 {
55  char *data = (char *)config;
56  char *input_data = (char *)param;
57  int len;
58 
59  len = strnlen(input_data, MAX_DATA_LEN);
60  if (len == MAX_DATA_LEN || len < MIN_USER_DATA_LEN) {
61  DOCA_LOG_ERR("Invalid data length, should be between %d and %d", MIN_USER_DATA_LEN, MAX_USER_DATA_LEN);
63  }
64  strcpy(data, input_data);
65  return DOCA_SUCCESS;
66 }
67 
68 /*
69  * Register the command line parameters for the sample.
70  *
71  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
72  */
74 {
76  struct doca_argp_param *data_param;
77 
78  result = doca_argp_param_create(&data_param);
79  if (result != DOCA_SUCCESS) {
80  DOCA_LOG_ERR("Failed to create ARGP param: %s", doca_error_get_descr(result));
81  return result;
82  }
83  doca_argp_param_set_short_name(data_param, "d");
84  doca_argp_param_set_long_name(data_param, "data");
85  doca_argp_param_set_description(data_param, "user data");
88  result = doca_argp_register_param(data_param);
89  if (result != DOCA_SUCCESS) {
90  DOCA_LOG_ERR("Failed to register program param: %s", doca_error_get_descr(result));
91  return result;
92  }
93  return DOCA_SUCCESS;
94 }
95 
96 /*
97  * Sample main function
98  *
99  * @argc [in]: command line arguments size
100  * @argv [in]: array of command line arguments
101  * @return: EXIT_SUCCESS on success and EXIT_FAILURE otherwise
102  */
103 int main(int argc, char **argv)
104 {
106  struct doca_log_backend *sdk_log;
107  int exit_status = EXIT_FAILURE;
108  char data[MAX_DATA_LEN];
109 
110  strcpy(data, "1234567890abcdef");
111 
112  /* Register a logger backend */
114  if (result != DOCA_SUCCESS)
115  goto sample_exit;
116 
117  /* Register a logger backend for internal SDK errors and warnings */
118  result = doca_log_backend_create_with_file_sdk(stderr, &sdk_log);
119  if (result != DOCA_SUCCESS)
120  goto sample_exit;
122  if (result != DOCA_SUCCESS)
123  goto sample_exit;
124 
125  DOCA_LOG_INFO("Starting the sample");
126 
127  result = doca_argp_init(NULL, &data);
128  if (result != DOCA_SUCCESS) {
129  DOCA_LOG_ERR("Failed to init ARGP resources: %s", doca_error_get_descr(result));
130  goto sample_exit;
131  }
132 
134  if (result != DOCA_SUCCESS) {
135  DOCA_LOG_ERR("Failed to register ARGP params: %s", doca_error_get_descr(result));
136  goto argp_cleanup;
137  }
138 
139  result = doca_argp_start(argc, argv);
140  if (result != DOCA_SUCCESS) {
141  DOCA_LOG_ERR("Failed to parse sample input: %s", doca_error_get_descr(result));
142  goto argp_cleanup;
143  }
144 
145  result = sha_create(data);
146  if (result != DOCA_SUCCESS) {
147  DOCA_LOG_ERR("sha_create() encountered an error: %s", doca_error_get_descr(result));
148  goto argp_cleanup;
149  }
150 
151  exit_status = EXIT_SUCCESS;
152 
153 argp_cleanup:
155 sample_exit:
156  if (exit_status == EXIT_SUCCESS)
157  DOCA_LOG_INFO("Sample finished successfully");
158  else
159  DOCA_LOG_INFO("Sample finished with errors");
160  return exit_status;
161 }
#define NULL
Definition: __stddef_null.h:26
int32_t result
uint64_t len
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
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
#define MAX_DATA_LEN
#define MAX_USER_DATA_LEN
static doca_error_t data_callback(void *param, void *config)
doca_error_t sha_create(char *src_buffer)
#define MIN_USER_DATA_LEN
int main(int argc, char **argv)
static doca_error_t register_sha_params(void)
DOCA_LOG_REGISTER(SHA_CREATE::MAIN)