-
Notifications
You must be signed in to change notification settings - Fork 2
/
con.c
252 lines (214 loc) · 5.34 KB
/
con.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
#include "base.h"
#include "server.h"
#include "coroutine.h"
#include "trace.h"
void tlb_con_delete(struct tlb_con *con)
{
s64 age;
BUG_ON(!list_empty(&con->list_entry));
if (con->start_time) {
age = ktime_ms_delta(ktime_get(), con->start_time);
if (age > 5)
trace_con_too_long(con, age);
}
if (con->target_con)
tlb_target_con_close(con->target_con);
if (con->target)
tlb_target_put(con->target);
if (con->buf)
kmem_cache_free(g_con_buf_cache, con->buf);
if (con->sock) {
trace_con_sock_release(con);
ksock_release(con->sock);
trace_con_sock_release_return(con);
}
trace_con_delete(con, con->co);
coroutine_deref(con->co);
kmem_cache_free(g_con_cache, con);
}
struct tlb_con *tlb_con_create(struct tlb_server *srv)
{
struct tlb_con *con;
con = kmem_cache_alloc(g_con_cache, GFP_KERNEL);
if (!con)
return NULL;
memset(con, 0, sizeof(*con));
atomic_inc(&srv->next_con_thread);
con->co = coroutine_create(&srv->con_thread[atomic_read(&srv->next_con_thread) % srv->nr_con_thread]);
if (!con->co) {
kmem_cache_free(g_con_cache, con);
return NULL;
}
con->srv = srv;
INIT_LIST_HEAD(&con->list_entry);
trace_con_create(con, con->co);
return con;
}
void tlb_con_data_ready(struct sock *sk)
{
struct tlb_con *con = sk->sk_user_data;
coroutine_signal(con->co);
}
void tlb_con_write_space(struct sock *sk)
{
struct tlb_con *con = sk->sk_user_data;
coroutine_signal(con->co);
}
void tlb_con_state_change(struct sock *sk)
{
struct tlb_con *con = sk->sk_user_data;
trace_con_state_change(con, sk->sk_state);
coroutine_signal(con->co);
}
static int copy_socket_coroutine(struct coroutine *co, struct socket *from, struct socket *to, char *buf, int buf_len, bool *closed)
{
int r, received, sent;
*closed = false;
for (;;) {
trace_coroutine_recv(co, buf_len);
r = ksock_recv(from, buf, buf_len);
trace_coroutine_recv_return(co, r);
if (r < 0) {
if (r == -EAGAIN) {
coroutine_yield(co);
continue;
} else
break;
} else if (r == 0) {
*closed = true;
break;
}
received = r;
sent = 0;
while (sent < received) {
trace_coroutine_send(co, received - sent);
r = ksock_send(to, buf + sent, received - sent);
trace_coroutine_send_return(co, r);
if (r < 0) {
if (r == -EAGAIN) {
coroutine_yield(co);
continue;
} else
break;
}
sent += r;
}
if (r < 0)
break;
}
return r;
}
static void *tlb_target_con_coroutine(struct coroutine *co, void *arg)
{
struct tlb_target_con *con = arg;
int r;
bool closed;
trace_target_con_co_enter(con, co);
con->buf_len = TLB_CON_BUF_SIZE;
con->buf = kmem_cache_alloc(g_con_buf_cache, GFP_KERNEL);
if (!con->buf) {
r = -ENOMEM;
goto out;
}
for (;;) {
r = copy_socket_coroutine(co, con->sock, con->src_sock, con->buf, con->buf_len, &closed);
if (r)
break;
if (closed)
break;
}
trace_con_sock_release(con);
ksock_release(con->sock);
trace_con_sock_release_return(con);
con->sock = NULL;
kmem_cache_free(g_con_buf_cache, con->buf);
con->buf = NULL;
out:
trace_target_con_co_leave(con, co, r);
return ERR_PTR(r);
}
static void *tlb_con_coroutine(struct coroutine *co, void *arg)
{
struct tlb_con *con = (struct tlb_con *)arg;
struct tlb_server *srv = con->srv;
struct coroutine *target_con_co;
struct tlb_target *target;
u64 con_time_us;
int r;
bool closed;
BUG_ON(con->co != co);
trace_con_co_enter(con, co);
con->buf_len = TLB_CON_BUF_SIZE;
con->buf = kmem_cache_alloc(g_con_buf_cache, GFP_KERNEL);
if (!con->buf) {
r = -ENOMEM;
goto out;
}
con->target = tlb_server_select_target(srv);
if (!con->target) {
r = -ENOENT;
goto free_buf;
}
atomic64_inc(&con->target->total_cons);
atomic64_inc(&con->target->active_cons);
target_con_co = coroutine_create(co->thread);
if (!target_con_co) {
r = -ENOMEM;
goto put_target;
}
r = tlb_target_connect(con->target, target_con_co, &con->target_con);
coroutine_deref(target_con_co);
if (r)
goto put_target;
con->target_con->src_sock = con->sock;
coroutine_start(target_con_co, tlb_target_con_coroutine, con->target_con);
for (;;) {
r = copy_socket_coroutine(co, con->sock, con->target_con->sock, con->buf, con->buf_len, &closed);
if (r)
break;
if (closed)
break;
}
trace_con_sock_release(con);
ksock_release(con->sock);
trace_con_sock_release_return(con);
con->sock = NULL;
if (r)
coroutine_cancel(con->target_con->co);
else {
void *ret;
coroutine_cancel(con->target_con->co);
ret = con->target_con->co->ret;
if (IS_ERR(ret))
r = PTR_ERR(ret);
}
tlb_target_con_close(con->target_con);
con->target_con = NULL;
put_target:
target = con->target;
atomic64_dec(&target->active_cons);
spin_lock(&target->lock);
con_time_us = ktime_us_delta(ktime_get(), con->start_time);
if (con_time_us > target->max_con_time_us)
target->max_con_time_us = con_time_us;
if (con_time_us < target->min_con_time_us)
target->min_con_time_us = con_time_us;
target->total_con_time_us += con_time_us;
resample_add(&target->con_time_sample, con_time_us);
spin_unlock(&target->lock);
tlb_target_put(target);
con->target = NULL;
free_buf:
kmem_cache_free(g_con_buf_cache, con->buf);
con->buf = NULL;
out:
trace_con_co_leave(con, co, r);
tlb_server_unlink_con(srv, con);
tlb_con_delete(con);
return NULL;
}
void tlb_con_start(struct tlb_con *con, struct socket *sock)
{
con->sock = sock;
coroutine_start(con->co, tlb_con_coroutine, con);
}