-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin_client.c
More file actions
243 lines (197 loc) · 5.45 KB
/
Copy pathplugin_client.c
File metadata and controls
243 lines (197 loc) · 5.45 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
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <time.h>
#include <errno.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdbool.h>
#include <fcntl.h>
#include "vfio-migration-plugin.h"
#include "plugin.h"
#include "common.h"
struct plugin_handle {
char *devid;
char *arg;
unsigned long pending_bytes;
int fd;
};
struct plugin_handle handle;
static int plugin_connect(char *ip, uint32_t port)
{
int sockfd;
int ret = 0;
struct sockaddr_in server_addr;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("socket error");
exit(1);
}
bzero(&server_addr,sizeof(struct sockaddr_in));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
server_addr.sin_addr.s_addr = inet_addr(ip);
ret = connect(sockfd,(struct sockaddr*)&server_addr,sizeof(struct sockaddr));
if (ret < 0) {
perror("connect failed");
exit(1);
}
return sockfd;
}
static uint64_t plugin_get_u64(int fd, VFIOMigrationRequest request, uint64_t *u64)
{
Plugin_Message message = { 0 };
Plugin_Message msg_reply = { 0 };
int ret = 0;
message.request = request;
if (!send_all(fd, (void *)&message, sizeof(Plugin_Message))) {
perror("send error");
}
if (!receive_all(fd, (void *)&msg_reply, sizeof(msg_reply))) {
perror("receive u64 error");
return -1;
}
if (msg_reply.request != VFIO_MIG_MESSAGE_REPLY) {
perror("unknow message");
return -1;
}
*u64 = msg_reply.u64;
return 0;
}
static void plugin_set_u64(int fd, VFIOMigrationRequest request, uint64_t u64)
{
Plugin_Message message = { 0 };
message.request = request;
message.size = sizeof(uint64_t);
message.u64 = u64;
if (!send_all(fd, (char *)&message, sizeof(message))) {
perror("send u64 error");
}
}
static void* plugin_init(char *devid, char *arg)
{
char addr[32] = "\0";
char *ip = NULL;
uint32_t port;
strcpy(addr, arg);
ip= strtok(addr, ":");
port = atoi(strtok(NULL, ":"));
handle.devid = devid;
handle.arg = arg;
handle.fd = plugin_connect(ip, port);
return (void *)&handle;
}
static int plugin_save(void *arg, uint8_t *state, uint64_t len)
{
struct plugin_handle *handle = (struct plugin_handle *)arg;
Plugin_Message message = { 0 };
int ret = 0;
uint8_t *buffer = NULL;
message.request = VFIO_MIG_SAVE_BUFFER;
message.size = len;
if (!send_all(handle->fd, (void *)&message, sizeof(message))) {
perror("send error");
}
/* if (!receive_all(handle->fd, (void *)&message, sizeof(message))) {
perror("receive error");
return -1;
}
*/
if (!receive_all(handle->fd, (void *)state, len)) {
perror("receive data error");
return -1;
}
return 0;
}
static int plugin_load(void *arg, uint8_t *state, uint64_t len)
{
struct plugin_handle *handle = (struct plugin_handle *)arg;
Plugin_Message msg = { 0 };
msg.request = VFIO_MIG_LOAD_BUFFER;
msg.size = len;
if (!send_all(handle->fd, (void *)&msg, sizeof(Plugin_Message))) {
perror("send error");
}
if (!send_all(handle->fd, (void *)state, len)) {
perror("send error");
}
return 0;
}
static int plugin_update_pending(void *arg, uint64_t *pending_bytes)
{
struct plugin_handle *handle = (struct plugin_handle *)arg;
if (plugin_get_u64(handle->fd, VFIO_MIG_UPDATE_PENDING, pending_bytes) < 0) {
perror("get pending bytes error");
return -1;
}
return 0;
}
static int plugin_set_state(void *arg, uint32_t value)
{
struct plugin_handle *handle = (struct plugin_handle *)arg;
plugin_set_u64(handle->fd, VFIO_MIG_SET_STATE, value);
return 0;
}
static int plugin_get_state(void *arg, uint32_t *value)
{
struct plugin_handle *handle = (struct plugin_handle *)arg;
uint64_t u64 = 0;
if (plugin_get_u64(handle->fd, VFIO_MIG_GET_STATE, &u64) < 0) {
perror("plugin get state error");
return -1;
}
*value = u64;
return 0;
}
static int plugin_cleanup(void *arg)
{
struct plugin_handle *handle = (struct plugin_handle *)arg;
Plugin_Message msg = { 0 };
msg.request = VFIO_MIG_FINISH;
if(!send_all(handle->fd, (void *)&msg, sizeof(Plugin_Message))) {
perror("send error");
}
close(handle->fd);
return 0;
}
static VFIOMigrationPluginOps plugin_ops = {
.init = plugin_init,
.save = plugin_save,
.load = plugin_load,
.update_pending = plugin_update_pending,
.set_state = plugin_set_state,
.get_state = plugin_get_state,
.cleanup = plugin_cleanup,
};
int vfio_lm_get_plugin_version(void)
{
return VFIO_LM_PLUGIN_API_VERSION;
}
VFIOMigrationPluginOps* vfio_lm_get_plugin_ops(void)
{
return &plugin_ops;
}
/*
int main(void)
{
void *handle = NULL;
uint8_t *buffer = NULL;
uint64_t pending_bytes = 0;
uint32_t state = 0;
char *arg = "127.0.0.1:3333";
handle = plugin_init(NULL, arg);
plugin_set_state(handle, 3);
plugin_get_state(handle, &state);
printf("state is:%d\n", state);
plugin_update_pending(handle, &pending_bytes);
buffer = malloc(pending_bytes);
plugin_save(handle, buffer, pending_bytes);
printf("device states is:%s\n", buffer);
plugin_load(handle, buffer, pending_bytes);
plugin_cleanup(handle);
free(buffer);
}*/