-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.c
More file actions
186 lines (152 loc) · 4.03 KB
/
utils.c
File metadata and controls
186 lines (152 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <error.h>
#include <unistd.h>
#include <getopt.h>
#include "utils.h"
#include "virtio_over_shmem.h"
#include "log.h"
static const char short_options[] = "d:h";
static const struct option
long_options[] = {
{ "driver", required_argument, NULL, 'd' },
{ "help", no_argument, NULL, 'h' },
{ 0, 0, 0, 0 }
};
struct shmem_ops_info {
const char *name;
struct shmem_ops *ops;
};
static struct shmem_ops *shmem_ops[] = {
&ivshm_ivshmem_ops,
NULL
};
static bool starts_with(const char *s, const char *prefix)
{
return strncmp(s, prefix, strlen(prefix)) == 0;
}
static void usage(FILE *fp, int argc __attribute__((unused)), char **argv)
{
fprintf(fp,
"Usage: %s [options] SHM-DEVICE OPTIONS\n\n"
"Options:\n"
"-d | --driver name Shared memory driver name\n"
"-h | --help Print this message\n"
"\n"
"Available drivers:",
argv[0]);
for (struct shmem_ops **ops = shmem_ops; *ops != NULL; ops++)
fprintf(fp, " %s", (*ops)->name);
fprintf(fp, "\n");
}
static int infer_shmem_ops(struct virtio_backend_info *info)
{
if (info->shmem_devpath == NULL)
return -1;
if (starts_with(info->shmem_devpath, "/dev/ivshm")) {
info->shmem_ops = &ivshm_ivshmem_ops;
} else {
return -1;
}
return 0;
}
void parse_shmem_args(struct virtio_backend_info *info, int argc, char *argv[])
{
int c = 0;
bool found;
while(true) {
c = getopt_long(argc, argv,
short_options, long_options, NULL);
if (c < 0) {
if (argc < optind + 2) {
usage(stderr, argc, argv);
exit(EXIT_FAILURE);
}
info->shmem_devpath = argv[optind];
info->opts = argv[optind + 1];
if (!info->shmem_ops && (infer_shmem_ops(info) < 0)) {
fprintf(stderr, "Failed to infer the shared memory driver. Specify one with -d.\n");
exit(EXIT_FAILURE);
}
break;
}
switch (c) {
case 0: /* getopt_long() flag */
break;
case 'd':
found = false;
for (struct shmem_ops **ops = shmem_ops; *ops != NULL; ops ++) {
if (strcmp(optarg, (*ops)->name) == 0) {
info->shmem_ops = *ops;
found = true;
break;
}
}
if (!found) {
fprintf(stderr, "Unknown driver: %s\n\n", optarg);
usage(stderr, argc, argv);
exit(EXIT_FAILURE);
}
break;
case 'h':
usage(stdout, argc, argv);
exit(EXIT_SUCCESS);
default:
usage(stderr, argc, argv);
exit(EXIT_FAILURE);
}
}
pr_info("Backend options:\n"
"Shared memory driver: %s\n"
"Shared memory device path: %s\n"
"Virtual device options: %s\n",
info->shmem_ops->name, info->shmem_devpath, info->opts);
}
void set_shmem_args(struct virtio_backend_info *info)
{
info->shmem_devpath = "/dev/ivshm0.default";
if (!info->shmem_ops && (infer_shmem_ops(info) < 0)) {
fprintf(stderr, "Failed to infer the shared memory driver. Specify one with -d.\n");
exit(EXIT_FAILURE);
}
pr_info("Backend options:\n"
"Shared memory driver: %s\n"
"Shared memory device path: %s\n"
"Virtual device options: %s\n",
info->shmem_ops->name, info->shmem_devpath, info->opts);
}
void *run_backend(void *data, int argc __attribute__((unused)), char *argv[] __attribute__((unused)))
{
int ret;
struct virtio_backend_info *info = (struct virtio_backend_info *)data;
// parse_shmem_args(info, argc, argv);
set_shmem_args(info);
if (info->hook_before_init)
info->hook_before_init(info);
ret = vos_backend_init(info);
if (ret)
error(1, ret, "Backend initialization failed.\n");
vos_backend_run();
vos_backend_deinit(info);
return NULL;
}
void dump_hex(void *base, int size)
{
int i;
for (i = 0; i < size; i++) {
if ((i % 16) == 0) {
pr_info("\n0x%02x:", i);
}
pr_info(" %02x", *((uint8_t*)((char*)base + i)));
}
}
void dump_desc(volatile struct vring_desc *desc, int idx, bool cond __attribute__((unused)))
{
pr_info("desc[%d] @ 0x%llx, size: %d, flags: 0x%x", idx, desc[idx].addr, desc[idx].len, desc[idx].flags);
if (desc[idx].flags & VRING_DESC_F_NEXT) {
pr_info(", next: %d", desc[idx].next);
}
pr_info("\n");
}