forked from SymbioticLab/Infiniswap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
infiniswap-daemon.c
114 lines (89 loc) · 2.58 KB
/
infiniswap-daemon.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
/*
* Infiniswap, remote memory paging over RDMA
* Copyright 2017 University of Michigan, Ann Arbor
* GPLv2 License
*/
#include "rdma-common.h"
static int on_connect_request(struct rdma_cm_id *id);
static int on_connection(struct rdma_cm_id *id);
static int on_disconnect(struct rdma_cm_id *id);
static int on_event(struct rdma_cm_event *event);
static void usage(const char *argv0);
long page_size;
int running;
int main(int argc, char **argv)
{
struct sockaddr_in6 addr;
struct rdma_cm_event *event = NULL;
struct rdma_cm_id *listener = NULL;
struct rdma_event_channel *ec = NULL;
uint16_t port = 0;
pthread_t free_mem_thread;
if (argc != 3)
usage(argv[0]);
page_size = sysconf(_SC_PAGE_SIZE);
memset(&addr, 0, sizeof(addr));
addr.sin6_family = AF_INET6;
inet_pton(AF_INET6, argv[1], &addr.sin6_addr);
addr.sin6_port = htons(atoi(argv[2]));
TEST_Z(ec = rdma_create_event_channel());
TEST_NZ(rdma_create_id(ec, &listener, NULL, RDMA_PS_TCP));
TEST_NZ(rdma_bind_addr(listener, (struct sockaddr *)&addr));
TEST_NZ(rdma_listen(listener, 10)); /* backlog=10 is arbitrary */
port = ntohs(rdma_get_src_port(listener));
printf("listening on port %d.\n", port);
//free
running = 1;
TEST_NZ(pthread_create(&free_mem_thread, NULL, (void *)free_mem, NULL));
while (rdma_get_cm_event(ec, &event) == 0) {
struct rdma_cm_event event_copy;
memcpy(&event_copy, event, sizeof(*event));
rdma_ack_cm_event(event);
if (on_event(&event_copy))
break;
}
rdma_destroy_id(listener);
rdma_destroy_event_channel(ec);
return 0;
}
int on_connect_request(struct rdma_cm_id *id)
{
struct rdma_conn_param cm_params;
printf("received connection request.\n");
build_connection(id);
build_params(&cm_params);
TEST_NZ(rdma_accept(id, &cm_params));
return 0;
}
int on_connection(struct rdma_cm_id *id)
{
on_connect(id->context);
printf("connection build\n");
/* J: only server send mr, client doesn't */
send_free_mem_size(id->context);
return 0;
}
int on_disconnect(struct rdma_cm_id *id)
{
printf("peer disconnected.\n");
destroy_connection(id->context);
return 0;
}
int on_event(struct rdma_cm_event *event)
{
int r = 0;
if (event->event == RDMA_CM_EVENT_CONNECT_REQUEST)
r = on_connect_request(event->id);
else if (event->event == RDMA_CM_EVENT_ESTABLISHED)
r = on_connection(event->id);
else if (event->event == RDMA_CM_EVENT_DISCONNECTED)
r = on_disconnect(event->id);
else
die("on_event: unknown event.");
return r;
}
void usage(const char *argv0)
{
fprintf(stderr, "usage: %s ip port\n", argv0);
exit(1);
}