This repository has been archived by the owner on Jul 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
image-remote-pvt.c
553 lines (485 loc) · 18.4 KB
/
image-remote-pvt.c
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
#include <unistd.h>
#include <stdlib.h>
#include <semaphore.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include "image-remote-pvt.h"
#include "criu-log.h"
typedef struct wthread {
pthread_t tid;
struct list_head l;
} worker_thread;
static LIST_HEAD(rimg_head);
static pthread_mutex_t rimg_lock;
static sem_t rimg_semph;
static LIST_HEAD(workers_head);
static pthread_mutex_t workers_lock;
static sem_t workers_semph;
static int finished = 0;
static int putting = 0;
static void* (*get_func)(void*);
static void* (*put_func)(void*);
static remote_image* get_rimg_by_name(const char* namespace, const char* path)
{
remote_image* rimg = NULL;
pthread_mutex_lock(&rimg_lock);
list_for_each_entry(rimg, &rimg_head, l) {
if( !strncmp(rimg->path, path, PATHLEN) &&
!strncmp(rimg->namespace, namespace, PATHLEN)) {
pthread_mutex_unlock(&rimg_lock);
return rimg;
}
}
pthread_mutex_unlock(&rimg_lock);
return NULL;
}
int init_sync_structures()
{
if (pthread_mutex_init(&rimg_lock, NULL) != 0) {
pr_perror("Remote image connection mutex init failed");
return -1;
}
if (sem_init(&rimg_semph, 0, 0) != 0) {
pr_perror("Remote image connection semaphore init failed");
return -1;
}
if (pthread_mutex_init(&workers_lock, NULL) != 0) {
pr_perror("Workers mutex init failed");
return -1;
}
if (sem_init(&workers_semph, 0, 0) != 0) {
pr_perror("Workers semaphore init failed");
return -1;
}
return 0;
}
void* get_remote_image(void* fd)
{
int cli_fd = (long) fd;
remote_image* rimg = NULL;
char path_buf[PATHLEN];
char namespace_buf[PATHLEN];
if(read_header(cli_fd, namespace_buf, path_buf) < 0) {
pr_perror("Error reading header");
return NULL;
}
pr_info("Received GET for %s:%s.\n", path_buf, namespace_buf);
rimg = wait_for_image(cli_fd, namespace_buf, path_buf);
if (!rimg)
return NULL;
rimg->dst_fd = cli_fd;
send_remote_image(rimg->dst_fd, rimg->path, &rimg->buf_head);
return NULL;
}
void prepare_put_rimg()
{
pthread_mutex_lock(&rimg_lock);
putting++;
pthread_mutex_unlock(&rimg_lock);
}
void finalize_put_rimg(remote_image* rimg)
{
pthread_mutex_lock(&rimg_lock);
list_add_tail(&(rimg->l), &rimg_head);
putting--;
pthread_mutex_unlock(&rimg_lock);
sem_post(&rimg_semph);
}
int init_proxy()
{
#if GC_COMPRESSION
get_func = get_proxied_image;
#else
get_func = get_remote_image;
#endif
put_func = proxy_remote_image;
return init_sync_structures();
}
int init_cache()
{
get_func = get_remote_image;
put_func = cache_remote_image;
return init_sync_structures();
}
int prepare_server_socket(int port)
{
struct sockaddr_in serv_addr;
int sockopt = 1;
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
pr_perror("Unable to open image socket");
return -1;
}
bzero((char *) &serv_addr, sizeof (serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(port);
if (setsockopt(
sockfd, SOL_SOCKET, SO_REUSEADDR, &sockopt, sizeof (sockopt)) == -1) {
pr_perror("Unable to set SO_REUSEADDR");
return -1;
}
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof (serv_addr)) < 0) {
pr_perror("Unable to bind image socket");
return -1;
}
if (listen(sockfd, DEFAULT_LISTEN)) {
pr_perror("Unable to listen image socket");
return -1;
}
return sockfd;
}
int prepare_client_socket(char* hostname, int port)
{
struct hostent *server;
struct sockaddr_in serv_addr;
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
pr_perror("Unable to open recover image socket");
return -1;
}
server = gethostbyname(hostname);
if (server == NULL) {
pr_perror("Unable to get host by name (%s)", hostname);
return -1;
}
bzero((char *) &serv_addr, sizeof (serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *) server->h_addr,
(char *) &serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(port);
if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
pr_perror("Unable to connect to remote restore host %s", hostname);
return -1;
}
return sockfd;
}
static void add_worker(pthread_t tid)
{
worker_thread* wthread = malloc(sizeof(worker_thread));
if(!wthread) {
pr_perror("Unable to allocate worker thread structure");
}
wthread->tid = tid;
pthread_mutex_lock(&workers_lock);
list_add_tail(&(wthread->l), &workers_head);
pthread_mutex_unlock(&workers_lock);
sem_post(&workers_semph);
}
void join_workers()
{
worker_thread* wthread = NULL;
while(1) {
if(list_empty(&workers_head)) {
sem_wait(&workers_semph);
continue;
}
wthread = list_entry(workers_head.next, worker_thread, l);
if(pthread_join(wthread->tid, NULL)) {
pr_perror("Could not join thread %lu", (unsigned long) wthread->tid);
}
else {
//pr_info("Joined thread %lu\n", (unsigned long) wthread->tid);
list_del(&(wthread->l));
free(wthread);
}
}
}
remote_image* wait_for_image(int cli_fd, char* namespace, char* path)
{
remote_image *result;
while (1) {
result = get_rimg_by_name(namespace, path);
// The file exists
if(result != NULL) {
if(write_header(cli_fd, namespace, path) < 0) {
pr_perror("Error writing header for %s:%s",
path, namespace);
close(cli_fd);
return NULL;
}
return result;
}
// The file does not exist and we do not expect new files
if(finished && !putting) {
if(write_header(cli_fd, NULL_NAMESPACE, DUMP_FINISH) < 0) {
pr_perror("Error writing header for %s:%s",
DUMP_FINISH, NULL_NAMESPACE);
}
close(cli_fd);
return NULL;
}
// The file does not exist but the request is for a parent file.
// A parent file may not exist for the first process.
if(!putting && !strncmp(path, PARENT_IMG, PATHLEN)) {
if(write_header(cli_fd, namespace, path) < 0) {
pr_perror("Error writing header for %s:%s",
path, namespace);
}
close(cli_fd);
return NULL;
}
sem_wait(&rimg_semph);
}
}
void* accept_get_image_connections(void* port)
{
socklen_t clilen;
long cli_fd;
pthread_t tid;
int get_fd = *((int*) port);
struct sockaddr_in cli_addr;
clilen = sizeof (cli_addr);
while (1) {
cli_fd = accept(get_fd, (struct sockaddr *) &cli_addr, &clilen);
if (cli_fd < 0) {
pr_perror("Unable to accept get image connection");
return NULL;
}
if (pthread_create(
&tid, NULL, get_func, (void*) cli_fd)) {
pr_perror("Unable to create put thread");
return NULL;
}
add_worker(tid);
}
}
void* accept_put_image_connections(void* port)
{
socklen_t clilen;
int cli_fd;
pthread_t tid;
int put_fd = *((int*) port);
struct sockaddr_in cli_addr;
clilen = sizeof(cli_addr);
char path_buf[PATHLEN];
char namespace_buf[PATHLEN];
while (1) {
cli_fd = accept(put_fd, (struct sockaddr *) &cli_addr, &clilen);
if (cli_fd < 0) {
pr_perror("Unable to accept put image connection");
return NULL;
}
if(read_header(cli_fd, namespace_buf, path_buf) < 0) {
pr_perror("Error reading header");
continue;
}
remote_image* rimg = get_rimg_by_name(namespace_buf, path_buf);
pr_info("Reveiced PUT request for %s:%s\n", path_buf, namespace_buf);
if(rimg == NULL) {
rimg = malloc(sizeof (remote_image));
if (rimg == NULL) {
pr_perror("Unable to allocate remote_image structures");
return NULL;
}
remote_buffer* buf = malloc(sizeof (remote_buffer));
if(buf == NULL) {
pr_perror("Unable to allocate remote_buffer structures");
return NULL;
}
strncpy(rimg->path, path_buf, PATHLEN);
strncpy(rimg->namespace, namespace_buf, PATHLEN);
buf->nbytes = 0;
INIT_LIST_HEAD(&(rimg->buf_head));
list_add_tail(&(buf->l), &(rimg->buf_head));
}
// NOTE: we implement a PUT by clearing the previous file.
else {
pr_info("Clearing previous images for %s:%s\n",
path_buf, namespace_buf);
pthread_mutex_lock(&rimg_lock);
list_del(&(rimg->l));
pthread_mutex_unlock(&rimg_lock);
while(!list_is_singular(&(rimg->buf_head))) {
list_del(rimg->buf_head.prev);
}
list_entry(rimg->buf_head.next, remote_buffer, l)->nbytes = 0;
}
rimg->src_fd = cli_fd;
rimg->dst_fd = -1;
if (pthread_create(
&tid, NULL, put_func, (void*) rimg)) {
pr_perror("Unable to create put thread");
return NULL;
}
pr_info("Serving PUT request for %s:%s (tid=%lu)\n",
rimg->path, rimg->namespace, (unsigned long) tid);
add_worker(tid);
if (!strncmp(path_buf, DUMP_FINISH, sizeof (DUMP_FINISH))) {
finished = 1;
pr_info("Received DUMP FINISH\n");
sem_post(&rimg_semph);
}
}
}
int recv_remote_image(int fd, char* path, struct list_head* rbuff_head)
{
remote_buffer* curr_buf = list_entry(rbuff_head->next, remote_buffer, l);
int n, nblocks;
nblocks = 0;
while(1) {
n = read(fd,
curr_buf->buffer + curr_buf->nbytes,
BUF_SIZE - curr_buf->nbytes);
if (n == 0) {
pr_info("Finished receiving %s (%d full blocks, %d bytes on last block)\n",
path, nblocks, curr_buf->nbytes);
close(fd);
return nblocks*BUF_SIZE + curr_buf->nbytes;
}
else if (n > 0) {
curr_buf->nbytes += n;
if(curr_buf->nbytes == BUF_SIZE) {
remote_buffer* buf = malloc(sizeof(remote_buffer));
if(buf == NULL) {
pr_perror("Unable to allocate remote_buffer structures");
return -1;
}
buf->nbytes = 0;
buf->garbage = 0;
list_add_tail(&(buf->l), rbuff_head);
curr_buf = buf;
nblocks++;
}
}
else {
pr_perror("Read on %s socket failed", path);
return -1;
}
}
}
int send_remote_image(int fd, char* path, struct list_head* rbuff_head)
{
remote_buffer* curr_buf = list_entry(rbuff_head->next, remote_buffer, l);
int n, curr_offset, nblocks;
nblocks = 0;
curr_offset = 0;
while(1) {
n = send(
fd,
curr_buf->buffer + curr_offset,
MIN(BUF_SIZE, curr_buf->nbytes) - curr_offset,
MSG_NOSIGNAL);
if(n > -1) {
curr_offset += n;
if(curr_offset == BUF_SIZE) {
curr_buf =
list_entry(curr_buf->l.next, remote_buffer, l);
nblocks++;
curr_offset = 0;
}
else if(curr_offset == curr_buf->nbytes) {
pr_info("Finished forwarding %s (%d full blocks, %d bytes on last block)\n",
path, nblocks, curr_offset);
close(fd);
return nblocks*BUF_SIZE + curr_buf->nbytes;
}
}
else if(errno == EPIPE || errno == ECONNRESET) {
pr_warn("Connection for %s was closed early than expected\n",
path);
return 0;
}
else {
pr_perror("Write on %s socket failed", path);
return -1;
}
}
}
#if GC_COMPRESSION
size_t recv_remote_obj(int fd, char* buff, size_t size)
{
size_t n = 0;
size_t curr = 0;
while(1) {
n = read(fd, buff + curr, size - curr);
if(n < 1) {
return n;
}
curr += n;
if(curr == size) {
return size;
}
}
}
size_t send_remote_obj(int fd, char* buff, size_t size)
{
size_t n = 0;
size_t curr = 0;
while(1) {
n = send(fd, buff + curr, size - curr, MSG_NOSIGNAL);
if( n < 1) {
return n;
}
curr += n;
if(curr == size) {
return size;
}
}
}
int recv_remote_pages(int fd, char* path, struct list_head* rbuff_head)
{
remote_buffer* curr_buf = list_entry(rbuff_head->next, remote_buffer, l);
int n, nblocks;
nblocks = 0;
while(1) {
n = recv_remote_obj(fd, &(curr_buf->garbage), sizeof(char));
if(n == 0) {
close(fd);
pr_info("Finished caching %s (%d full blocks)\n", path, nblocks);
return nblocks;
}
else if(n != sizeof(char)) {
pr_perror("Read on %s socket failed (garbage)", path);
return -1;
}
if(curr_buf->garbage != 1) {
n = recv_remote_obj(fd, curr_buf->buffer, PAGESIZE);
if(n == PAGESIZE) {
nblocks++;
}
else {
pr_perror("Read on %s socket failed (page, n=%d, nblocks=%d)", path, n, nblocks);
return -1;
}
}
curr_buf->nbytes = PAGESIZE;
curr_buf = malloc(sizeof(remote_buffer));
if(curr_buf == NULL) {
pr_perror("Unable to allocate remote_buffer structures");
return -1;
}
curr_buf->garbage = 1;
list_add_tail(&(curr_buf->l), rbuff_head);
}
}
int send_remote_pages(int fd, char* path, struct list_head* rbuff_head)
{
remote_buffer* curr_buf = list_entry(rbuff_head->next, remote_buffer, l);
int nblocks = 0;
while(1) {
if(send_remote_obj(fd, &(curr_buf->garbage), sizeof(char)) != sizeof(char)) {
pr_perror("Write on %s socket failed (garbage)", path);
return -1;
}
if(curr_buf->garbage != 1) {
if(send_remote_obj(fd, curr_buf->buffer, PAGESIZE) == PAGESIZE) {
nblocks++;
}
else {
pr_perror("Write on %s socket failed (page)", path);
return -1;
}
}
curr_buf = list_entry(curr_buf->l.next, remote_buffer, l);
// The normal receive adds an extra buffer (empty) in the end.
if(curr_buf->nbytes == 0) {
close(fd);
pr_info("Finished forwarding %s (%d full blocks)\n", path, nblocks);
return nblocks;
}
}
}
#endif