1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright (c) 2021 NVIDIA Corporation & Affiliates 3 */ 4 5 #include <stdio.h> 6 #include <stdlib.h> 7 #include <string.h> 8 #include <stdint.h> 9 #include <inttypes.h> 10 #include <stdarg.h> 11 #include <errno.h> 12 #include <getopt.h> 13 14 #include <rte_common.h> 15 #include <rte_malloc.h> 16 #include <rte_memory.h> 17 #include <rte_eal.h> 18 #include <rte_ether.h> 19 #include <rte_ethdev.h> 20 #include <rte_mempool.h> 21 #include <rte_mbuf.h> 22 23 #include <rte_gpudev.h> 24 25 enum app_args { 26 ARG_HELP, 27 ARG_MEMPOOL 28 }; 29 30 static void 31 usage(const char *prog_name) 32 { 33 printf("%s [EAL options] --\n", 34 prog_name); 35 } 36 37 static void 38 args_parse(int argc, char **argv) 39 { 40 char **argvopt; 41 int opt; 42 int opt_idx; 43 44 static struct option lgopts[] = { 45 { "help", 0, 0, ARG_HELP}, 46 /* End of options */ 47 { 0, 0, 0, 0 } 48 }; 49 50 argvopt = argv; 51 while ((opt = getopt_long(argc, argvopt, "", 52 lgopts, &opt_idx)) != EOF) { 53 switch (opt) { 54 case ARG_HELP: 55 usage(argv[0]); 56 break; 57 default: 58 usage(argv[0]); 59 rte_exit(EXIT_FAILURE, "Invalid option: %s\n", argv[optind]); 60 break; 61 } 62 } 63 } 64 65 static int 66 alloc_gpu_memory(uint16_t gpu_id) 67 { 68 void *ptr_1 = NULL; 69 void *ptr_2 = NULL; 70 size_t buf_bytes = 1024; 71 int ret; 72 73 printf("\n=======> TEST: Allocate GPU memory\n\n"); 74 75 /* Alloc memory on GPU 0 */ 76 ptr_1 = rte_gpu_mem_alloc(gpu_id, buf_bytes); 77 if (ptr_1 == NULL) { 78 fprintf(stderr, "rte_gpu_mem_alloc GPU memory returned error\n"); 79 goto error; 80 } 81 printf("GPU memory allocated at 0x%p size is %zd bytes\n", 82 ptr_1, buf_bytes); 83 84 ptr_2 = rte_gpu_mem_alloc(gpu_id, buf_bytes); 85 if (ptr_2 == NULL) { 86 fprintf(stderr, "rte_gpu_mem_alloc GPU memory returned error\n"); 87 goto error; 88 } 89 printf("GPU memory allocated at 0x%p size is %zd bytes\n", 90 ptr_2, buf_bytes); 91 92 ret = rte_gpu_mem_free(gpu_id, (uint8_t *)(ptr_1)+0x700); 93 if (ret < 0) { 94 printf("GPU memory 0x%p NOT freed: GPU driver didn't find this memory address internally.\n", 95 (uint8_t *)(ptr_1)+0x700); 96 } else { 97 fprintf(stderr, "ERROR: rte_gpu_mem_free freed GPU memory 0x%p\n", 98 (uint8_t *)(ptr_1)+0x700); 99 goto error; 100 } 101 102 ret = rte_gpu_mem_free(gpu_id, ptr_2); 103 if (ret < 0) { 104 fprintf(stderr, "rte_gpu_mem_free returned error %d\n", ret); 105 goto error; 106 } 107 printf("GPU memory 0x%p freed\n", ptr_2); 108 109 ret = rte_gpu_mem_free(gpu_id, ptr_1); 110 if (ret < 0) { 111 fprintf(stderr, "rte_gpu_mem_free returned error %d\n", ret); 112 goto error; 113 } 114 printf("GPU memory 0x%p freed\n", ptr_1); 115 116 printf("\n=======> TEST: PASSED\n"); 117 return 0; 118 119 error: 120 121 rte_gpu_mem_free(gpu_id, ptr_1); 122 rte_gpu_mem_free(gpu_id, ptr_2); 123 124 printf("\n=======> TEST: FAILED\n"); 125 return -1; 126 } 127 128 static int 129 register_cpu_memory(uint16_t gpu_id) 130 { 131 void *ptr = NULL; 132 size_t buf_bytes = 1024; 133 int ret; 134 135 printf("\n=======> TEST: Register CPU memory\n\n"); 136 137 /* Alloc memory on CPU visible from GPU 0 */ 138 ptr = rte_zmalloc(NULL, buf_bytes, 0); 139 if (ptr == NULL) { 140 fprintf(stderr, "Failed to allocate CPU memory.\n"); 141 goto error; 142 } 143 144 ret = rte_gpu_mem_register(gpu_id, buf_bytes, ptr); 145 if (ret < 0) { 146 fprintf(stderr, "rte_gpu_mem_register CPU memory returned error %d\n", ret); 147 goto error; 148 } 149 printf("CPU memory registered at 0x%p %zdB\n", ptr, buf_bytes); 150 151 ret = rte_gpu_mem_unregister(gpu_id, (uint8_t *)(ptr)+0x700); 152 if (ret < 0) { 153 printf("CPU memory 0x%p NOT unregistered: GPU driver didn't find this memory address internally\n", 154 (uint8_t *)(ptr)+0x700); 155 } else { 156 fprintf(stderr, "ERROR: rte_gpu_mem_unregister unregistered GPU memory 0x%p\n", 157 (uint8_t *)(ptr)+0x700); 158 goto error; 159 } 160 161 ret = rte_gpu_mem_unregister(gpu_id, ptr); 162 if (ret < 0) { 163 fprintf(stderr, "rte_gpu_mem_unregister returned error %d\n", ret); 164 goto error; 165 } 166 printf("CPU memory 0x%p unregistered\n", ptr); 167 168 rte_free(ptr); 169 170 printf("\n=======> TEST: PASSED\n"); 171 return 0; 172 173 error: 174 175 rte_gpu_mem_unregister(gpu_id, ptr); 176 rte_free(ptr); 177 printf("\n=======> TEST: FAILED\n"); 178 return -1; 179 } 180 181 static int 182 create_update_comm_flag(uint16_t gpu_id) 183 { 184 struct rte_gpu_comm_flag devflag; 185 int ret = 0; 186 uint32_t set_val; 187 uint32_t get_val; 188 189 printf("\n=======> TEST: Communication flag\n\n"); 190 191 ret = rte_gpu_comm_create_flag(gpu_id, &devflag, RTE_GPU_COMM_FLAG_CPU); 192 if (ret < 0) { 193 fprintf(stderr, "rte_gpu_comm_create_flag returned error %d\n", ret); 194 goto error; 195 } 196 197 set_val = 25; 198 ret = rte_gpu_comm_set_flag(&devflag, set_val); 199 if (ret < 0) { 200 fprintf(stderr, "rte_gpu_comm_set_flag returned error %d\n", ret); 201 goto error; 202 } 203 204 ret = rte_gpu_comm_get_flag_value(&devflag, &get_val); 205 if (ret < 0) { 206 fprintf(stderr, "rte_gpu_comm_get_flag_value returned error %d\n", ret); 207 goto error; 208 } 209 210 printf("Communication flag value at 0x%p was set to %d and current value is %d\n", 211 devflag.ptr, set_val, get_val); 212 213 set_val = 38; 214 ret = rte_gpu_comm_set_flag(&devflag, set_val); 215 if (ret < 0) { 216 fprintf(stderr, "rte_gpu_comm_set_flag returned error %d\n", ret); 217 goto error; 218 } 219 220 ret = rte_gpu_comm_get_flag_value(&devflag, &get_val); 221 if (ret < 0) { 222 fprintf(stderr, "rte_gpu_comm_get_flag_value returned error %d\n", ret); 223 goto error; 224 } 225 226 printf("Communication flag value at 0x%p was set to %d and current value is %d\n", 227 devflag.ptr, set_val, get_val); 228 229 ret = rte_gpu_comm_destroy_flag(&devflag); 230 if (ret < 0) { 231 fprintf(stderr, "rte_gpu_comm_destroy_flags returned error %d\n", ret); 232 goto error; 233 } 234 235 printf("\n=======> TEST: PASSED\n"); 236 return 0; 237 238 error: 239 240 rte_gpu_comm_destroy_flag(&devflag); 241 printf("\n=======> TEST: FAILED\n"); 242 return -1; 243 } 244 245 static int 246 simulate_gpu_task(struct rte_gpu_comm_list *comm_list_item, int num_pkts) 247 { 248 int idx; 249 250 if (comm_list_item == NULL) 251 return -1; 252 253 for (idx = 0; idx < num_pkts; idx++) { 254 /** 255 * consume(comm_list_item->pkt_list[idx].addr); 256 */ 257 } 258 comm_list_item->status = RTE_GPU_COMM_LIST_DONE; 259 260 return 0; 261 } 262 263 static int 264 create_update_comm_list(uint16_t gpu_id) 265 { 266 int ret = 0; 267 int i = 0; 268 struct rte_gpu_comm_list *comm_list = NULL; 269 uint32_t num_comm_items = 1024; 270 struct rte_mbuf *mbufs[10]; 271 272 printf("\n=======> TEST: Communication list\n\n"); 273 274 comm_list = rte_gpu_comm_create_list(gpu_id, num_comm_items); 275 if (comm_list == NULL) { 276 fprintf(stderr, "rte_gpu_comm_create_list returned error %d\n", ret); 277 goto error; 278 } 279 280 /** 281 * Simulate DPDK receive functions like rte_eth_rx_burst() 282 */ 283 for (i = 0; i < 10; i++) { 284 mbufs[i] = rte_zmalloc(NULL, sizeof(struct rte_mbuf), 0); 285 if (mbufs[i] == NULL) { 286 fprintf(stderr, "Failed to allocate fake mbufs in CPU memory.\n"); 287 goto error; 288 } 289 290 memset(mbufs[i], 0, sizeof(struct rte_mbuf)); 291 } 292 293 /** 294 * Populate just the first item of the list 295 */ 296 ret = rte_gpu_comm_populate_list_pkts(&(comm_list[0]), mbufs, 10); 297 if (ret < 0) { 298 fprintf(stderr, "rte_gpu_comm_populate_list_pkts returned error %d\n", ret); 299 goto error; 300 } 301 302 ret = rte_gpu_comm_cleanup_list(&(comm_list[0])); 303 if (ret == 0) { 304 fprintf(stderr, "rte_gpu_comm_cleanup_list erroneously cleaned the list even if packets have not been consumed yet\n"); 305 goto error; 306 } 307 printf("Communication list not cleaned because packets have not been consumed yet.\n"); 308 309 /** 310 * Simulate a GPU tasks going through the packet list to consume 311 * mbufs packets and release them 312 */ 313 printf("Consuming packets...\n"); 314 simulate_gpu_task(&(comm_list[0]), 10); 315 316 /** 317 * Packets have been consumed, now the communication item 318 * and the related mbufs can be all released 319 */ 320 ret = rte_gpu_comm_cleanup_list(&(comm_list[0])); 321 if (ret < 0) { 322 fprintf(stderr, "rte_gpu_comm_cleanup_list returned error %d\n", ret); 323 goto error; 324 } 325 326 printf("Communication list cleaned because packets have been consumed now.\n"); 327 328 ret = rte_gpu_comm_destroy_list(comm_list, num_comm_items); 329 if (ret < 0) { 330 fprintf(stderr, "rte_gpu_comm_destroy_list returned error %d\n", ret); 331 goto error; 332 } 333 334 for (i = 0; i < 10; i++) 335 rte_free(mbufs[i]); 336 337 printf("\n=======> TEST: PASSED\n"); 338 return 0; 339 340 error: 341 342 rte_gpu_comm_destroy_list(comm_list, num_comm_items); 343 for (i = 0; i < 10; i++) 344 rte_free(mbufs[i]); 345 printf("\n=======> TEST: FAILED\n"); 346 return -1; 347 } 348 349 int 350 main(int argc, char **argv) 351 { 352 int ret; 353 int nb_gpus = 0; 354 int16_t gpu_id = 0; 355 struct rte_gpu_info ginfo; 356 357 /* Init EAL. */ 358 ret = rte_eal_init(argc, argv); 359 if (ret < 0) 360 rte_exit(EXIT_FAILURE, "EAL init failed\n"); 361 argc -= ret; 362 argv += ret; 363 if (argc > 1) 364 args_parse(argc, argv); 365 argc -= ret; 366 argv += ret; 367 368 nb_gpus = rte_gpu_count_avail(); 369 printf("\n\nDPDK found %d GPUs:\n", nb_gpus); 370 RTE_GPU_FOREACH(gpu_id) 371 { 372 if (rte_gpu_info_get(gpu_id, &ginfo)) 373 rte_exit(EXIT_FAILURE, "rte_gpu_info_get error - bye\n"); 374 375 printf("\tGPU ID %d\n\t\tparent ID %d GPU Bus ID %s NUMA node %d Tot memory %.02f MB, Tot processors %d\n", 376 ginfo.dev_id, 377 ginfo.parent, 378 ginfo.name, 379 ginfo.numa_node, 380 (((float)ginfo.total_memory)/(float)1024)/(float)1024, 381 ginfo.processor_count 382 ); 383 } 384 printf("\n\n"); 385 386 if (nb_gpus == 0) { 387 fprintf(stderr, "Need at least one GPU on the system to run the example\n"); 388 return EXIT_FAILURE; 389 } 390 391 gpu_id = 0; 392 393 /** 394 * Memory tests 395 */ 396 alloc_gpu_memory(gpu_id); 397 register_cpu_memory(gpu_id); 398 399 /** 400 * Communication items test 401 */ 402 create_update_comm_flag(gpu_id); 403 create_update_comm_list(gpu_id); 404 405 /* clean up the EAL */ 406 rte_eal_cleanup(); 407 408 return EXIT_SUCCESS; 409 } 410