NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
utils.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021-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 <arpa/inet.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <stdnoreturn.h>
32 
33 #include <doca_version.h>
34 #include <doca_log.h>
35 #include <doca_argp.h>
36 
37 #include "utils.h"
38 
40 
41 noreturn doca_error_t sdk_version_callback(void *param, void *doca_config)
42 {
43  (void)(param);
44  (void)(doca_config);
45 
46  printf("DOCA SDK Version (Compilation): %s\n", doca_version());
47  printf("DOCA Runtime Version (Runtime): %s\n", doca_version_runtime());
48 
49  /* Cleanup after ARGP initialization */
51 
52  /* We assume that when printing DOCA's versions there is no need to continue the program's execution */
53  exit(EXIT_SUCCESS);
54 }
55 
56 doca_error_t read_file(char const *path, char **out_bytes, size_t *out_bytes_len)
57 {
58  FILE *file;
59  char *bytes;
60 
61  file = fopen(path, "rb");
62  if (file == NULL)
63  return DOCA_ERROR_NOT_FOUND;
64 
65  if (fseek(file, 0, SEEK_END) != 0) {
66  fclose(file);
67  return DOCA_ERROR_IO_FAILED;
68  }
69 
70  long const nb_file_bytes = ftell(file);
71 
72  if (nb_file_bytes == -1) {
73  fclose(file);
74  return DOCA_ERROR_IO_FAILED;
75  }
76 
77  if (nb_file_bytes == 0) {
78  fclose(file);
80  }
81 
82  bytes = malloc(nb_file_bytes);
83  if (bytes == NULL) {
84  fclose(file);
85  return DOCA_ERROR_NO_MEMORY;
86  }
87 
88  if (fseek(file, 0, SEEK_SET) != 0) {
89  free(bytes);
90  fclose(file);
91  return DOCA_ERROR_IO_FAILED;
92  }
93 
94  size_t const read_byte_count = fread(bytes, 1, nb_file_bytes, file);
95 
96  fclose(file);
97 
98  if (read_byte_count != (size_t)nb_file_bytes) {
99  free(bytes);
100  return DOCA_ERROR_IO_FAILED;
101  }
102 
103  *out_bytes = bytes;
104  *out_bytes_len = read_byte_count;
105 
106  return DOCA_SUCCESS;
107 }
108 
109 void linear_array_init_u16(uint16_t *array, uint16_t n)
110 {
111  uint16_t i;
112 
113  for (i = 0; i < n; i++)
114  array[i] = i;
115 }
116 
117 #ifndef DOCA_USE_LIBBSD
118 
119 #ifndef strlcpy
120 
121 #include <string.h>
122 
123 size_t strlcpy(char *dst, const char *src, size_t size)
124 {
125  size_t trimmed_size;
126  size_t src_len = strlen(src);
127 
128  if (size > 0) {
129  trimmed_size = MIN(src_len, (size - 1));
130 
131  memcpy(dst, src, trimmed_size);
132  dst[trimmed_size] = '\0';
133  }
134 
135  return src_len;
136 }
137 
138 #endif /* strlcpy */
139 
140 #ifndef strlcat
141 
142 #include <string.h>
143 
144 size_t strlcat(char *dst, const char *src, size_t size)
145 {
146  size_t dst_len = strnlen(dst, size);
147 
148  if (dst_len >= size)
149  return size;
150 
151  return dst_len + strlcpy(dst + dst_len, src, size - dst_len);
152 }
153 
154 #endif /* strlcat */
155 
156 #endif /* ! DOCA_USE_LIBBSD */
#define NULL
Definition: __stddef_null.h:26
char path[MAX_PATH_LEN+1]
#define MIN(X, Y)
Definition: utils.h:30
static uint64_t *restrict src
Definition: dpaintrin.h:230
DOCA_EXPERIMENTAL doca_error_t doca_argp_destroy(void)
ARG Parser destroy.
enum doca_error doca_error_t
DOCA API return codes.
@ DOCA_ERROR_INVALID_VALUE
Definition: doca_error.h:44
@ DOCA_ERROR_NOT_FOUND
Definition: doca_error.h:54
@ DOCA_ERROR_IO_FAILED
Definition: doca_error.h:55
@ DOCA_SUCCESS
Definition: doca_error.h:38
@ DOCA_ERROR_NO_MEMORY
Definition: doca_error.h:45
static const char * doca_version(void)
Function returning DOCA's (SDK) exact version string.
Definition: doca_version.h:90
DOCA_EXPERIMENTAL const char * doca_version_runtime(void)
Function returning DOCA's (runtime) exact version string.
#define noreturn
Definition: stdnoreturn.h:13
size_t strlcat(char *dst, const char *src, size_t size)
Definition: utils.c:144
doca_error_t read_file(char const *path, char **out_bytes, size_t *out_bytes_len)
Definition: utils.c:56
size_t strlcpy(char *dst, const char *src, size_t size)
Definition: utils.c:123
DOCA_LOG_REGISTER(UTILS)
void linear_array_init_u16(uint16_t *array, uint16_t n)
Definition: utils.c:109
noreturn doca_error_t sdk_version_callback(void *param, void *doca_config)
Definition: utils.c:41