NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
devemu_pci_device_stateful_region_host_sample.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 <errno.h>
27 #include <linux/vfio.h>
28 #include <sys/ioctl.h>
29 #include <sys/mman.h>
30 #include <stdint.h>
31 #include <unistd.h>
32 
33 #include <common.h>
34 #include <doca_error.h>
35 #include <doca_log.h>
36 
37 #include <devemu_pci_host_common.h>
38 #include <devemu_pci_type_config.h>
39 
40 DOCA_LOG_REGISTER(DEVEMU_PCI_DEVICE_STATEFUL_REGION_HOST);
41 
42 /*
43  * Run DOCA Device Emulation Stateful Region Host sample
44  *
45  * @pci_address [in]: Emulated device PCI address
46  * @vfio_group [in]: VFIO group ID
47  * @region_index [in]: The index of the stateful region
48  * @write_data [in]: The data to write to stateful region
49  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
50  */
52  int vfio_group,
53  int region_index,
54  const char *write_data)
55 {
57  struct devemu_host_resources resources = {0};
58 
59  resources.container_fd = -1;
60  resources.group_fd = -1;
61  resources.device_fd = -1;
62 
65  "No stateful region was configured for type. Please configure at least 1 stateful region to run this sample");
67  }
68 
69  const struct bar_region_config *stateful_config = &stateful_configs[region_index];
70  size_t data_len = strnlen(write_data, PCI_TYPE_MAX_STATEFUL_REGION_SIZE);
71  if (data_len > stateful_configs->size) {
72  DOCA_LOG_ERR("Write data size of %zuB exceeds region size of %luB", data_len, stateful_configs->size);
74  }
75 
76  result = init_vfio_device(&resources, vfio_group, pci_address);
77  if (result != DOCA_SUCCESS) {
79  return result;
80  }
81 
82  result = map_bar_region_memory(&resources, stateful_config, &resources.stateful_region);
83  if (result != DOCA_SUCCESS) {
85  return result;
86  }
87 
88  struct bar_mapped_region *stateful_region = &resources.stateful_region;
89  if (data_len == 0) {
90  // Read data
91  char *dump = hex_dump(stateful_region->mem, stateful_config->size);
92  DOCA_LOG_INFO("Reading stateful region at bar %u start address %zu size %zuB:\n%s",
93  stateful_config->bar_id,
94  stateful_config->start_address,
96  dump);
97  free(dump);
98  } else {
99  // Write data
100  DOCA_LOG_INFO("Writing to stateful region at bar %u start address %zu size %zuB:\n",
101  stateful_config->bar_id,
102  stateful_config->start_address,
103  data_len);
104  memcpy(stateful_region->mem, write_data, data_len);
105  }
106 
107  return DOCA_SUCCESS;
108 }
int32_t result
doca_error_t devemu_pci_device_stateful_region_host(const char *pci_address, int vfio_group, int region_index, const char *write_data)
DOCA_LOG_REGISTER(DEVEMU_PCI_DEVICE_STATEFUL_REGION_HOST)
void devemu_host_resources_cleanup(struct devemu_host_resources *resources)
doca_error_t map_bar_region_memory(struct devemu_host_resources *resources, const struct bar_region_config *bar_region_config, struct bar_mapped_region *mapped_mem)
doca_error_t init_vfio_device(struct devemu_host_resources *resources, int vfio_group, const char *pci_address)
struct rdma_resources resources
enum doca_error doca_error_t
DOCA API return codes.
@ DOCA_ERROR_INVALID_VALUE
Definition: doca_error.h:44
@ DOCA_SUCCESS
Definition: doca_error.h:38
#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
char * hex_dump(const void *data, size_t size)
#define PCI_TYPE_NUM_BAR_STATEFUL_REGIONS
#define PCI_TYPE_MAX_STATEFUL_REGION_SIZE
static const struct bar_region_config stateful_configs[PCI_TYPE_NUM_BAR_STATEFUL_REGIONS]