-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathcommon.c
More file actions
263 lines (229 loc) · 7.52 KB
/
common.c
File metadata and controls
263 lines (229 loc) · 7.52 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
* common.c
*
* Copyright (c) 2014 Virtual Open Systems Sarl.
*
* This work is licensed under the terms of the GNU GPL, version 2 or later.
* See the COPYING file in the top-level directory.
*
*/
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include "common.h"
#include "vring.h"
#include "vhost_user.h"
const char* cmd_from_vhostmsg(const VhostUserMsg* msg)
{
switch (msg->request) {
case VHOST_USER_NONE:
return "VHOST_USER_NONE";
case VHOST_USER_GET_FEATURES:
return "VHOST_USER_GET_FEATURES";
case VHOST_USER_SET_FEATURES:
return "VHOST_USER_SET_FEATURES";
case VHOST_USER_SET_OWNER:
return "VHOST_USER_SET_OWNER";
case VHOST_USER_RESET_OWNER:
return "VHOST_USER_RESET_OWNER";
case VHOST_USER_SET_MEM_TABLE:
return "VHOST_USER_SET_MEM_TABLE";
case VHOST_USER_SET_LOG_BASE:
return "VHOST_USER_SET_LOG_BASE";
case VHOST_USER_SET_LOG_FD:
return "VHOST_USER_SET_LOG_FD";
case VHOST_USER_SET_VRING_NUM:
return "VHOST_USER_SET_VRING_NUM";
case VHOST_USER_SET_VRING_ADDR:
return "VHOST_USER_SET_VRING_ADDR";
case VHOST_USER_SET_VRING_BASE:
return "VHOST_USER_SET_VRING_BASE";
case VHOST_USER_GET_VRING_BASE:
return "VHOST_USER_GET_VRING_BASE";
case VHOST_USER_SET_VRING_KICK:
return "VHOST_USER_SET_VRING_KICK";
case VHOST_USER_SET_VRING_CALL:
return "VHOST_USER_SET_VRING_CALL";
case VHOST_USER_SET_VRING_ERR:
return "VHOST_USER_SET_VRING_ERR";
case VHOST_USER_MAX:
return "VHOST_USER_MAX";
}
return "UNDEFINED";
}
void dump_vhostmsg(const VhostUserMsg* msg)
{
int i = 0;
fprintf(stdout, "Cmd: %s (0x%x)\n", cmd_from_vhostmsg(msg), msg->request);
fprintf(stdout, "Flags: 0x%x\n", msg->flags);
// command specific `dumps`
switch (msg->request) {
case VHOST_USER_GET_FEATURES:
fprintf(stdout, "u64: 0x%"PRIx64"\n", msg->u64);
break;
case VHOST_USER_SET_FEATURES:
fprintf(stdout, "u64: 0x%"PRIx64"\n", msg->u64);
break;
case VHOST_USER_SET_OWNER:
break;
case VHOST_USER_RESET_OWNER:
break;
case VHOST_USER_SET_MEM_TABLE:
fprintf(stdout, "nregions: %d\n", msg->memory.nregions);
for (i = 0; i < msg->memory.nregions; i++) {
fprintf(stdout,
"region: \n\tgpa = 0x%"PRIX64"\n\tsize = %"PRId64"\n\tua = 0x%"PRIx64"\n",
msg->memory.regions[i].guest_phys_addr,
msg->memory.regions[i].memory_size,
msg->memory.regions[i].userspace_addr);
}
break;
case VHOST_USER_SET_LOG_BASE:
fprintf(stdout, "u64: 0x%"PRIx64"\n", msg->u64);
break;
case VHOST_USER_SET_LOG_FD:
break;
case VHOST_USER_SET_VRING_NUM:
fprintf(stdout, "state: %d %d\n", msg->state.index, msg->state.num);
break;
case VHOST_USER_SET_VRING_ADDR:
fprintf(stdout, "addr:\n\tidx = %d\n\tflags = 0x%x\n"
"\tdua = 0x%"PRIx64"\n"
"\tuua = 0x%"PRIx64"\n"
"\taua = 0x%"PRIx64"\n"
"\tlga = 0x%"PRIx64"\n", msg->addr.index, msg->addr.flags,
msg->addr.desc_user_addr, msg->addr.used_user_addr,
msg->addr.avail_user_addr, msg->addr.log_guest_addr);
break;
case VHOST_USER_SET_VRING_BASE:
fprintf(stdout, "state: %d %d\n", msg->state.index, msg->state.num);
break;
case VHOST_USER_GET_VRING_BASE:
fprintf(stdout, "state: %d %d\n", msg->state.index, msg->state.num);
break;
case VHOST_USER_SET_VRING_KICK:
case VHOST_USER_SET_VRING_CALL:
case VHOST_USER_SET_VRING_ERR:
fprintf(stdout, "u64: 0x%"PRIx64"\n", msg->u64);
break;
case VHOST_USER_NONE:
case VHOST_USER_MAX:
break;
}
fprintf(stdout,
"................................................................................\n");
}
void dump_buffer(uint8_t* p, size_t len)
{
int i;
fprintf(stdout,
"................................................................................");
for(i=0;i<len;i++) {
if(i%16 == 0)fprintf(stdout,"\n");
fprintf(stdout,"%.2x ",p[i]);
}
fprintf(stdout,"\n");
}
void dump_vring(struct vring_desc* desc, struct vring_avail* avail,struct vring_used* used)
{
int idx;
fprintf(stdout,"desc:\n");
for(idx=0;idx<VHOST_VRING_SIZE;idx++){
fprintf(stdout, "%d: 0x%"PRIx64" %d 0x%x %d\n",
idx,
desc[idx].addr, desc[idx].len,
desc[idx].flags, desc[idx].next);
}
fprintf(stdout,"avail:\n");
for(idx=0;idx<VHOST_VRING_SIZE;idx++){
int desc_idx = avail->ring[idx];
fprintf(stdout, "%d: %d\n",idx, desc_idx);
//dump_buffer((uint8_t*)desc[desc_idx].addr, desc[desc_idx].len);
}
}
void dump_vhost_vring(struct vhost_vring* vring)
{
fprintf(stdout, "kickfd: 0x%x, callfd: 0x%x\n", vring->kickfd, vring->callfd);
dump_vring(vring->desc, &vring->avail, &vring->used);
}
int vhost_user_send_fds(int fd, const VhostUserMsg *msg, int *fds,
size_t fd_num)
{
int ret;
struct msghdr msgh;
struct iovec iov[1];
size_t fd_size = fd_num * sizeof(int);
char control[CMSG_SPACE(fd_size)];
struct cmsghdr *cmsg;
memset(&msgh, 0, sizeof(msgh));
memset(control, 0, sizeof(control));
/* set the payload */
iov[0].iov_base = (void *) msg;
iov[0].iov_len = VHOST_USER_HDR_SIZE + msg->size;
msgh.msg_iov = iov;
msgh.msg_iovlen = 1;
if (fd_num) {
msgh.msg_control = control;
msgh.msg_controllen = sizeof(control);
cmsg = CMSG_FIRSTHDR(&msgh);
cmsg->cmsg_len = CMSG_LEN(fd_size);
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
memcpy(CMSG_DATA(cmsg), fds, fd_size);
} else {
msgh.msg_control = 0;
msgh.msg_controllen = 0;
}
do {
ret = sendmsg(fd, &msgh, 0);
} while (ret < 0 && errno == EINTR);
if (ret < 0) {
fprintf(stderr, "Failed to send msg, reason: %s\n", strerror(errno));
}
return ret;
}
int vhost_user_recv_fds(int fd, const VhostUserMsg *msg, int *fds,
size_t *fd_num)
{
int ret;
struct msghdr msgh;
struct iovec iov[1];
size_t fd_size = (*fd_num) * sizeof(int);
char control[CMSG_SPACE(fd_size)];
struct cmsghdr *cmsg;
memset(&msgh, 0, sizeof(msgh));
memset(control, 0, sizeof(control));
*fd_num = 0;
/* set the payload */
iov[0].iov_base = (void *) msg;
iov[0].iov_len = VHOST_USER_HDR_SIZE;
msgh.msg_iov = iov;
msgh.msg_iovlen = 1;
msgh.msg_control = control;
msgh.msg_controllen = sizeof(control);
ret = recvmsg(fd, &msgh, 0);
if (ret > 0) {
if (msgh.msg_flags & (MSG_TRUNC | MSG_CTRUNC)) {
ret = -1;
} else {
cmsg = CMSG_FIRSTHDR(&msgh);
if (cmsg && cmsg->cmsg_len > 0&&
cmsg->cmsg_level == SOL_SOCKET &&
cmsg->cmsg_type == SCM_RIGHTS) {
if (fd_size >= cmsg->cmsg_len - CMSG_LEN(0)) {
fd_size = cmsg->cmsg_len - CMSG_LEN(0);
memcpy(fds, CMSG_DATA(cmsg), fd_size);
*fd_num = fd_size / sizeof(int);
}
}
}
}
if (ret < 0) {
fprintf(stderr, "Failed recvmsg, reason: %s\n", strerror(errno));
} else {
read(fd, ((char*)msg) + ret, msg->size);
}
return ret;
}