aboutsummaryrefslogtreecommitdiff
path: root/samples
diff options
context:
space:
mode:
authorswapnili <swapnil.ingle@nutanix.com>2020-11-24 21:55:47 +0100
committerGitHub <noreply@github.com>2020-11-24 21:55:47 +0100
commit5429a84fe3b7def278ad1df6819d2ad7467ee5b0 (patch)
treee2f7be049a096eae9a16bf84d13f9dc1a62b8cd6 /samples
parentd33f124c5b0335c0525187e820c6b42be0bae743 (diff)
downloadlibvfio-user-5429a84fe3b7def278ad1df6819d2ad7467ee5b0.zip
libvfio-user-5429a84fe3b7def278ad1df6819d2ad7467ee5b0.tar.gz
libvfio-user-5429a84fe3b7def278ad1df6819d2ad7467ee5b0.tar.bz2
Api refactoring (#115)
API refactoring
Diffstat (limited to 'samples')
-rw-r--r--samples/client.c10
-rw-r--r--samples/gpio-pci-idio-16.c42
-rw-r--r--samples/null.c5
-rw-r--r--samples/server.c105
4 files changed, 89 insertions, 73 deletions
diff --git a/samples/client.c b/samples/client.c
index 91ce8a0..fa8e040 100644
--- a/samples/client.c
+++ b/samples/client.c
@@ -50,11 +50,11 @@
#define CLIENT_MAX_FDS (32)
static char *irq_to_str[] = {
- [LM_DEV_INTX_IRQ_IDX] = "INTx",
- [LM_DEV_MSI_IRQ_IDX] = "MSI",
- [LM_DEV_MSIX_IRQ_IDX] = "MSI-X",
- [LM_DEV_ERR_IRQ_INDEX] = "ERR",
- [LM_DEV_REQ_IRQ_INDEX] = "REQ"
+ [LM_DEV_INTX_IRQ] = "INTx",
+ [LM_DEV_MSI_IRQ] = "MSI",
+ [LM_DEV_MSIX_IRQ] = "MSI-X",
+ [LM_DEV_ERR_IRQ] = "ERR",
+ [LM_DEV_REQ_IRQ] = "REQ"
};
void
diff --git a/samples/gpio-pci-idio-16.c b/samples/gpio-pci-idio-16.c
index d4e485f..7dd8ec6 100644
--- a/samples/gpio-pci-idio-16.c
+++ b/samples/gpio-pci-idio-16.c
@@ -75,6 +75,9 @@ main(int argc, char *argv[])
char opt;
struct sigaction act = {.sa_handler = _sa_handler};
lm_ctx_t *lm_ctx;
+ lm_pci_hdr_id_t id = {.vid = 0x494F, .did = 0x0DC8};
+ lm_pci_hdr_ss_t ss = {0};
+ lm_pci_hdr_cc_t cc = {0};
while ((opt = getopt(argc, argv, "v")) != -1) {
switch (opt) {
@@ -91,27 +94,13 @@ main(int argc, char *argv[])
errx(EXIT_FAILURE, "missing MUSER socket path");
}
- lm_dev_info_t dev_info = {
- .trans = LM_TRANS_SOCK,
- .log = verbose ? _log : NULL,
- .log_lvl = LM_DBG,
- .pci_info = {
- .id = {.vid = 0x494F, .did = 0x0DC8 },
- .reg_info[LM_DEV_BAR2_REG_IDX] = {
- .flags = LM_REG_FLAG_RW,
- .size = 0x100,
- .fn = &bar2_access
- },
- },
- .uuid = argv[optind],
- };
-
sigemptyset(&act.sa_mask);
if (sigaction(SIGINT, &act, NULL) == -1) {
err(EXIT_FAILURE, "failed to register signal handler");
}
- lm_ctx = lm_ctx_create(&dev_info);
+ lm_ctx = lm_create_ctx(LM_TRANS_SOCK, argv[optind], 0,
+ verbose ? _log : NULL, LM_DBG, NULL);
if (lm_ctx == NULL) {
if (errno == EINTR) {
printf("interrupted\n");
@@ -120,16 +109,31 @@ main(int argc, char *argv[])
err(EXIT_FAILURE, "failed to initialize device emulation");
}
- ret = lm_ctx_drive(lm_ctx);
+ ret = lm_pci_setup_config_hdr(lm_ctx, id, ss, cc, false);
+ if (ret < 0) {
+ fprintf(stderr, "failed to setup pci header\n");
+ goto out;
+ }
+ ret = lm_setup_region(lm_ctx, LM_DEV_BAR2_REG_IDX, 0x100, &bar2_access,
+ LM_REG_FLAG_RW, NULL, NULL);
+ if (ret < 0) {
+ fprintf(stderr, "failed to setup region\n");
+ goto out;
+ }
+
+ ret = lm_ctx_drive(lm_ctx);
if (ret != 0) {
if (ret != -ENOTCONN && ret != -EINTR) {
- err(EXIT_FAILURE, "failed to realize device emulation");
+ fprintf(stderr, "failed to realize device emulation\n");
+ goto out;
}
+ ret = 0;
}
+out:
lm_ctx_destroy(lm_ctx);
- return EXIT_SUCCESS;
+ return ret;
}
/* ex: set tabstop=4 shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/samples/null.c b/samples/null.c
index 97c3fcf..5d52893 100644
--- a/samples/null.c
+++ b/samples/null.c
@@ -78,9 +78,8 @@ int main(int argc, char **argv)
errx(EXIT_FAILURE, "missing MUSER socket path");
}
- lm_dev_info_t dev_info = {.uuid = argv[1], .log = null_log, .log_lvl = LM_DBG };
-
- lm_ctx_t *lm_ctx = lm_ctx_create(&dev_info);
+ lm_ctx_t *lm_ctx = lm_create_ctx(LM_TRANS_SOCK, argv[1], 0, null_log,
+ LM_DBG, NULL);
if (lm_ctx == NULL) {
err(EXIT_FAILURE, "failed to create libmuser context");
}
diff --git a/samples/server.c b/samples/server.c
index 432d0ce..f39452c 100644
--- a/samples/server.c
+++ b/samples/server.c
@@ -154,13 +154,14 @@ static void map_dma(void *pvt, uint64_t iova, uint64_t len)
server_data->regions[idx].len = len;
}
-static int unmap_dma(void *pvt, uint64_t iova)
+static int unmap_dma(void *pvt, uint64_t iova, uint64_t len)
{
struct server_data *server_data = pvt;
int idx;
for (idx = 0; idx < NR_DMA_REGIONS; idx++) {
- if (server_data->regions[idx].addr == iova) {
+ if (server_data->regions[idx].addr == iova &&
+ server_data->regions[idx].len == len) {
server_data->regions[idx].addr = 0;
server_data->regions[idx].len = 0;
return 0;
@@ -382,6 +383,9 @@ int main(int argc, char *argv[])
int nr_sparse_areas = 2, size = 1024, i;
struct lm_sparse_mmap_areas *sparse_areas;
lm_ctx_t *lm_ctx;
+ lm_pci_hdr_id_t id = {.raw = 0xdeadbeef};
+ lm_pci_hdr_ss_t ss = {.raw = 0xcafebabe};
+ lm_pci_hdr_cc_t cc = {.pi = 0xab, .scc = 0xcd, .bcc = 0xef};
while ((opt = getopt(argc, argv, "v")) != -1) {
switch (opt) {
@@ -413,62 +417,71 @@ int main(int argc, char *argv[])
sparse_areas->areas[i].start += size;
sparse_areas->areas[i].size = size;
}
-
- lm_dev_info_t dev_info = {
- .trans = LM_TRANS_SOCK,
- .log = verbose ? _log : NULL,
- .log_lvl = LM_DBG,
- .pci_info = {
- .id.raw = 0xdeadbeef,
- .ss.raw = 0xcafebabe,
- .cc = {.pi = 0xab, .scc = 0xcd, .bcc = 0xef},
- .reg_info[LM_DEV_BAR0_REG_IDX] = {
- .flags = LM_REG_FLAG_RW,
- .size = sizeof(time_t),
- .fn = &bar0_access
- },
- .reg_info[LM_DEV_BAR1_REG_IDX] = {
- .flags = LM_REG_FLAG_RW,
- .size = sysconf(_SC_PAGESIZE),
- .fn = &bar1_access,
- .mmap_areas = sparse_areas,
- .map = map_area
- },
- .irq_count[LM_DEV_INTX_IRQ_IDX] = 1,
- },
- .uuid = argv[optind],
- .reset = device_reset,
- .map_dma = map_dma,
- .unmap_dma = unmap_dma,
- .pvt = &server_data,
- .migration = {
- .size = server_data.migration.migr_data_len,
- .mmap_areas = sparse_areas,
- .callbacks = {
- .transition = &migration_device_state_transition,
- .get_pending_bytes = &migration_get_pending_bytes,
- .prepare_data = &migration_prepare_data,
- .read_data = &migration_read_data,
- .data_written = &migration_data_written,
- .write_data = &migration_write_data
- }
- }
- };
-
sigemptyset(&act.sa_mask);
if (sigaction(SIGALRM, &act, NULL) == -1) {
err(EXIT_FAILURE, "failed to register signal handler");
}
- server_data.lm_ctx = lm_ctx = lm_ctx_create(&dev_info);
+ server_data.lm_ctx = lm_ctx = lm_create_ctx(LM_TRANS_SOCK, argv[optind], 0,
+ verbose ? _log : NULL, LM_DBG, &server_data);
if (lm_ctx == NULL) {
err(EXIT_FAILURE, "failed to initialize device emulation\n");
}
+ ret = lm_pci_setup_config_hdr(lm_ctx, id, ss, cc, false);
+ if (ret < 0) {
+ err(EXIT_FAILURE, "failed to setup PCI header");
+ }
+
+ ret = lm_setup_region(lm_ctx, LM_DEV_BAR0_REG_IDX, sizeof(time_t),
+ &bar0_access, LM_REG_FLAG_RW, NULL, NULL);
+ if (ret < 0) {
+ err(EXIT_FAILURE, "failed to setup BAR0 region");
+ }
+
+ ret = lm_setup_region(lm_ctx, LM_DEV_BAR1_REG_IDX, sysconf(_SC_PAGESIZE),
+ &bar1_access, LM_REG_FLAG_RW, sparse_areas, map_area);
+ if (ret < 0) {
+ err(EXIT_FAILURE, "failed to setup BAR1 region");
+ }
+
+ ret = lm_setup_device_reset_cb(lm_ctx, &device_reset);
+ if (ret < 0) {
+ err(EXIT_FAILURE, "failed to setup device reset callbacks");
+ }
+
+ ret = lm_setup_device_dma_cb(lm_ctx, &map_dma, &unmap_dma);
+ if (ret < 0) {
+ err(EXIT_FAILURE, "failed to setup device DMA callbacks");
+ }
+
+ ret = lm_setup_device_nr_irqs(lm_ctx, LM_DEV_INTX_IRQ, 1);
+ if (ret < 0) {
+ err(EXIT_FAILURE, "failed to setup irq counts");
+ }
+
+ lm_migration_t migration = {
+ .size = server_data.migration.migr_data_len,
+ .mmap_areas = sparse_areas,
+ .callbacks = {
+ .transition = &migration_device_state_transition,
+ .get_pending_bytes = &migration_get_pending_bytes,
+ .prepare_data = &migration_prepare_data,
+ .read_data = &migration_read_data,
+ .data_written = &migration_data_written,
+ .write_data = &migration_write_data
+ }
+ };
+
+ ret = lm_setup_device_migration(lm_ctx, &migration);
+ if (ret < 0) {
+ err(EXIT_FAILURE, "failed to setup device migration");
+ }
+
server_data.migration.migr_data = aligned_alloc(server_data.migration.migr_data_len,
server_data.migration.migr_data_len);
if (server_data.migration.migr_data == NULL) {
- errx(EXIT_FAILURE, "failed to allocate migration data");
+ err(EXIT_FAILURE, "failed to allocate migration data");
}
do {