-
Notifications
You must be signed in to change notification settings - Fork 1
/
dmap-server.c
261 lines (215 loc) · 5 KB
/
dmap-server.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
#include "dmap-server.h"
#include "dmap-trace-helpers.h"
#include "dmap-malloc.h"
#include "dmap.h"
#include "dmap-handler.h"
#include "dmap-neighbor.h"
#include <linux/kthread.h>
#include <linux/delay.h>
int dmap_server_init(struct dmap_server *srv)
{
memset(srv, 0, sizeof(*srv));
mutex_init(&srv->mutex);
atomic64_set(&srv->next_con_id, 0);
INIT_LIST_HEAD(&srv->con_list);
return 0;
}
static int dmap_server_con_thread(void *data)
{
struct dmap_server_con *con;
struct dmap_server *srv;
struct dmap *map;
u32 type, len, result;
int r;
con = (struct dmap_server_con *)data;
srv = con->srv;
map = container_of(srv, struct dmap, server);
while (!kthread_should_stop() && !con->stopping) {
r = dmap_con_recv(&con->con, &con->request,
&type, &len, &result);
if (r) {
if (r == -EAGAIN) {
r = 0;
continue;
}
if (r != -ECONNRESET) {
TRACE_ERR(r, "con 0x%p id %llu recv failed",
con, con->id);
}
break;
}
if (kthread_should_stop() || con->stopping)
break;
result = dmap_handle_request(map, type, con->request.body, len,
con->response.body, &len);
r = dmap_con_send(&con->con, type, len, result,
&con->response);
if (r) {
if (r != -ECONNRESET) {
TRACE_ERR(r, "con 0x%p id %llu send failed",
con, con->id);
}
break;
}
}
return 0;
}
static int dmap_server_con_start(struct dmap_server *srv, struct socket *sock)
{
struct dmap_server_con *con;
struct task_struct *thread;
int r;
con = dmap_kzalloc(sizeof(*con), GFP_KERNEL);
if (!con)
return -ENOMEM;
r = dmap_con_init(&con->con);
if (r)
goto free_con;
con->id = atomic64_inc_return(&srv->next_con_id);
mutex_init(&con->mutex);
INIT_LIST_HEAD(&con->list);
thread = kthread_create(dmap_server_con_thread, con, "dmap-con-%llu",
con->id);
if (IS_ERR(thread)) {
r = PTR_ERR(thread);
goto deinit_con;
}
r = dmap_con_set_socket(&con->con, sock);
if (r)
goto del_thread;
get_task_struct(thread);
con->thread = thread;
con->srv = srv;
mutex_lock(&srv->mutex);
list_add_tail(&con->list, &srv->con_list);
mutex_unlock(&srv->mutex);
wake_up_process(thread);
return 0;
del_thread:
kthread_stop(thread);
deinit_con:
dmap_con_deinit(&con->con);
free_con:
dmap_kfree(con);
return r;
}
static void dmap_server_con_release(struct dmap_server_con *con)
{
mutex_lock(&con->mutex);
if (con->thread) {
con->stopping = true;
PRINTK("going to stop connection thread 0x%p", con->thread);
kthread_stop(con->thread);
put_task_struct(con->thread);
con->thread = NULL;
dmap_con_deinit(&con->con);
con->stopping = false;
}
mutex_unlock(&con->mutex);
dmap_kfree(con);
}
static int dmap_server_thread(void *data)
{
struct dmap_server *srv;
struct socket *sock;
int r;
srv = (struct dmap_server *)data;
PRINTK("server 0x%p running, thread 0x%p", srv, current);
while (!kthread_should_stop() && !srv->stopping) {
r = ksock_accept(&sock, srv->sock);
if (r) {
TRACE_ERR(r, "accept failed");
continue;
}
r = dmap_server_con_start(srv, sock);
if (r) {
TRACE_ERR(r, "connection start failed");
ksock_release(sock);
}
}
PRINTK("server 0x%p stopped, thread 0x%p", srv, current);
return 0;
}
int dmap_server_start(struct dmap_server *srv, char *host, int port)
{
struct dmap_address addr;
struct dmap *map;
struct task_struct *thread;
struct socket *sock;
int r;
int i;
mutex_lock(&srv->mutex);
if (srv->thread) {
r = -EEXIST;
goto unlock;
}
map = container_of(srv, struct dmap, server);
dmap_addr_init(&addr, host, port, map->id);
r = dmap_add_neighbor(map, &addr, true);
if (r)
goto unlock;
snprintf(srv->host, ARRAY_SIZE(srv->host), "%s", host);
srv->port = port;
for (i = 0; i < 5; i++) {
r = ksock_listen_host(&sock, host, port, 5);
if (r) {
TRACE_ERR(r, "listen failed");
if (r == -EADDRINUSE) {
msleep_interruptible(100);
continue;
}
goto remove_neighbor;
} else
break;
}
if (r)
goto remove_neighbor;
thread = kthread_create(dmap_server_thread, srv, "dmap-server");
if (IS_ERR(thread)) {
r = PTR_ERR(thread);
goto release_sock;
}
get_task_struct(thread);
srv->thread = thread;
srv->sock = sock;
INIT_LIST_HEAD(&srv->con_list);
wake_up_process(thread);
r = 0;
goto unlock;
release_sock:
ksock_release(sock);
remove_neighbor:
dmap_remove_neighbor(map, addr.host);
unlock:
mutex_unlock(&srv->mutex);
return r;
}
int dmap_server_stop(struct dmap_server *srv)
{
struct dmap_server_con *con, *tmp;
int r;
mutex_lock(&srv->mutex);
if (srv->thread) {
srv->stopping = true;
ksock_abort_accept(srv->sock);
PRINTK("going to stop server thread 0x%p", srv->thread);
kthread_stop(srv->thread);
put_task_struct(srv->thread);
srv->thread = NULL;
ksock_release(srv->sock);
srv->sock = NULL;
list_for_each_entry_safe(con, tmp, &srv->con_list, list) {
list_del_init(&con->list);
dmap_server_con_release(con);
}
srv->stopping = false;
r = 0;
} else
r = -ENOTTY;
mutex_unlock(&srv->mutex);
return r;
}
void dmap_server_deinit(struct dmap_server *srv)
{
dmap_server_stop(srv);
}