-
Notifications
You must be signed in to change notification settings - Fork 0
/
common-ix.c
379 lines (318 loc) · 8.25 KB
/
common-ix.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
#include <errno.h>
#include <pthread.h>
#include <ixev.h>
#include <mempool.h>
#include "common.h"
#include "memcached.h"
#define ROUND_UP(num, multiple) ((((num) + (multiple) - 1) / (multiple)) * (multiple))
struct conn {
struct ixev_ctx ctx;
enum conn_state state;
int partial;
binary_header_t header;
};
static struct mempool_datastore conn_datastore;
static __thread struct mempool conn_pool;
struct udp_response {
struct ip_tuple dest;
struct udp_response_header header;
};
static struct mempool_datastore udp_response_datastore;
static __thread struct mempool udp_response_pool;
__thread int thread_no;
int nr_cpu;
static void handler(struct ixev_ctx *ctx, unsigned int reason);
static bool recv_exactly(struct ixev_ctx *ctx, void *buf, int size)
{
ssize_t ret;
char *cbuf = (char *) buf;
struct conn *conn = container_of(ctx, struct conn, ctx);
assert(size >= 0);
while (conn->partial < size) {
ret = ixev_recv(ctx, &cbuf[conn->partial], size - conn->partial);
if (ret == -EAGAIN)
return false;
if (ret == -EIO) {
ixev_close(ctx);
return false;
}
assert(ret > 0);
conn->partial += ret;
}
assert(conn->partial == size);
conn->partial = 0;
return true;
}
static bool send_exactly(struct ixev_ctx *ctx, void *buf, int size)
{
ssize_t ret;
char *cbuf = (char *) buf;
struct conn *conn = container_of(ctx, struct conn, ctx);
assert(size >= 0);
while (conn->partial < size) {
ret = ixev_send(ctx, &cbuf[conn->partial], size - conn->partial);
if (ret == -EAGAIN)
return false;
assert(ret > 0);
conn->partial += ret;
}
assert(conn->partial == size);
conn->partial = 0;
return true;
}
static bool drain_exactly(struct ixev_ctx *ctx, int size)
{
ssize_t ret;
char buf[4096];
struct conn *conn = container_of(ctx, struct conn, ctx);
assert(size >= 0);
while (conn->partial < size) {
ret = ixev_recv(ctx, buf, min(sizeof(buf), size - conn->partial));
if (ret == -EAGAIN)
return false;
assert(ret > 0);
conn->partial += ret;
}
assert(conn->partial == size);
conn->partial = 0;
return true;
}
static bool drive_machine(struct ixev_ctx *ctx, unsigned int reason)
{
bool ok;
struct conn *conn = container_of(ctx, struct conn, ctx);
switch (conn->state) {
case STATE_HEADER:
ok = recv_exactly(ctx, &conn->header, sizeof(conn->header));
if (!ok)
return true;
assert(conn->header.magic == 0x80);
conn->state = STATE_EXTRA;
/* fallthrough */
case STATE_EXTRA:
ok = drain_exactly(ctx, conn->header.extra_len);
if (!ok)
return true;
conn->state = STATE_KEY;
/* fallthrough */
case STATE_KEY:
ok = drain_exactly(ctx, __builtin_bswap16(conn->header.key_len));
if (!ok)
return true;
conn->state = STATE_VALUE;
/* fallthrough */
case STATE_VALUE:
if (conn->header.opcode == CMD_SET) {
ok = drain_exactly(ctx, __builtin_bswap32(conn->header.body_len) - __builtin_bswap16(conn->header.key_len) - conn->header.extra_len);
if (!ok)
return true;
} else {
assert(conn->header.opcode == CMD_GET);
}
conn->state = STATE_PROC;
/* fallthrough */
case STATE_PROC:
process_request();
conn->state = STATE_RESPONSE;
conn->header.magic = 0x81;
conn->header.status = __builtin_bswap16(1); /* Key not found */
conn->header.body_len = 0;
/* fallthrough */
case STATE_RESPONSE:
ok = send_exactly(ctx, &conn->header, sizeof(conn->header));
if (!ok)
return true;
conn->state = STATE_HEADER;
break;
default:
assert(0);
}
return false;
}
static void handler(struct ixev_ctx *ctx, unsigned int reason)
{
bool stop;
struct conn *conn = container_of(ctx, struct conn, ctx);
while (1) {
stop = drive_machine(ctx, reason);
if (stop)
break;
}
switch (conn->state) {
case STATE_HEADER:
case STATE_EXTRA:
case STATE_KEY:
case STATE_VALUE:
ixev_set_handler(&conn->ctx, IXEVIN, &handler);
break;
case STATE_PROC:
assert(0);
case STATE_RESPONSE:
ixev_set_handler(&conn->ctx, IXEVOUT, &handler);
break;
default:
assert(0);
}
}
static struct ixev_ctx *accept(struct ip_tuple *id)
{
struct conn *conn = mempool_alloc(&conn_pool);
assert(conn);
conn->state = STATE_HEADER;
conn->partial = 0;
ixev_ctx_init(&conn->ctx);
ixev_set_handler(&conn->ctx, IXEVIN, &handler);
return &conn->ctx;
}
static void release(struct ixev_ctx *ctx)
{
struct conn *conn = container_of(ctx, struct conn, ctx);
memset(conn, 0xcc, sizeof(*conn));
mempool_free(&conn_pool, conn);
}
static void udp_recv(void *addr, size_t len, struct ip_tuple *id)
{
binary_header_t *req_header;
struct mc_header *mch;
struct udp_response *response;
#define DEBUG 0
#if DEBUG
unsigned char *data = addr;
printf("udp_recv: addr %p len %ld from %x:%d\n", addr, len, id->src_ip, id->src_port);
for (int i=0;i<len;i++) printf("%02x ", data[i]);
for (int i=0,c;i<len;i++) {
c = data[i];
printf("%c", c >= 0x20 && c <= 0x7f ? c : '.');
}
puts("");
#endif
mch = addr;
req_header = (void *)mch + sizeof(struct mc_header);
assert(req_header->magic == 0x80);
assert(req_header->opcode == CMD_GET);
assert(len == sizeof(*mch) + sizeof(*req_header) + req_header->extra_len + __builtin_bswap16(req_header->key_len));
process_request();
response = mempool_alloc(&udp_response_pool);
if (!response)
goto out;
response->dest.src_ip = id->dst_ip;
response->dest.dst_ip = id->src_ip;
response->dest.src_port = id->dst_port;
response->dest.dst_port = id->src_port;
bzero(&response->header, sizeof(response->header));
/*copy the request's memcached header*/
response->header.mc_h = *mch;
response->header.req_h.magic = (uint8_t)0x81;
response->header.req_h.status = __builtin_bswap16(1); /* Key not found */
response->header.req_h.body_len = 0;
response->header.req_h.key_len = req_header->key_len;
#if DEBUG
printf("udp_send: addr %p len %ld to %x:%d\n", &response->header, sizeof(response->header), id->src_ip, id->src_port);
#endif
ix_udp_send(&response->header, sizeof(response->header), &response->dest, (unsigned long) response);
out:
ix_udp_recv_done(addr);
}
static void udp_sent(unsigned long cookie)
{
#if DEBUG
printf("udp_sent: cookie %lx\n", cookie);
#endif
struct udp_response *r = (struct udp_response *) cookie;
mempool_free(&udp_response_pool, r);
}
static void ignore()
{
}
static struct ixev_conn_ops tcp_ops = {
.accept = &accept,
.release = &release,
};
static struct ix_ops udp_ops = {
.udp_recv = &udp_recv,
.udp_sent = &udp_sent,
.tcp_connected = ignore,
.tcp_knock = ignore,
.tcp_recv = ignore,
.tcp_sent = ignore,
.tcp_dead = ignore,
};
static void *tcp_thread_main(void *arg)
{
int ret;
thread_no = (long) arg;
ret = ixev_init_thread();
if (ret) {
fprintf(stderr, "unable to init IXEV\n");
return NULL;
}
ret = mempool_create(&conn_pool, &conn_datastore);
if (ret) {
fprintf(stderr, "unable to create mempool\n");
return NULL;
}
init_thread();
while (1)
ixev_wait();
return NULL;
}
static void *udp_thread_main(void *arg)
{
int ret;
ret = ix_init(&udp_ops, 4096);
assert(!ret);
ret = mempool_create(&udp_response_pool, &udp_response_datastore);
if (ret) {
fprintf(stderr, "unable to create mempool\n");
return NULL;
}
while (1) {
ix_poll();
/* TODO: handle failed syscalls */
karr->len = 0;
ix_handle_events();
}
}
void init_ix(int udp)
{
int ret;
unsigned int conn_pool_entries;
srand48(mytime());
conn_pool_entries = ROUND_UP(16384, MEMPOOL_DEFAULT_CHUNKSIZE);
if (udp) {
ret = mempool_create_datastore(&udp_response_datastore, 65536, sizeof(struct udp_response), 0, MEMPOOL_DEFAULT_CHUNKSIZE, "conn");
if (ret) {
fprintf(stderr, "unable to create mempool\n");
exit(-1);
}
} else {
ret = ixev_init(&tcp_ops);
if (ret) {
fprintf(stderr, "failed to initialize ixev\n");
exit(-1);
}
ret = mempool_create_datastore(&conn_datastore, conn_pool_entries, sizeof(struct conn), 0, MEMPOOL_DEFAULT_CHUNKSIZE, "conn");
if (ret) {
fprintf(stderr, "unable to create mempool\n");
exit(-1);
}
}
nr_cpu = sys_nrcpus();
if (nr_cpu < 1) {
fprintf(stderr, "got invalid cpu count %d\n", nr_cpu);
exit(-1);
}
}
void start_ix_server(int udp)
{
int i;
pthread_t tid;
sys_spawnmode(true);
for (i = 1; i < nr_cpu; i++) {
if (pthread_create(&tid, NULL, udp ? udp_thread_main : tcp_thread_main, (void *) (long) i)) {
fprintf(stderr, "failed to spawn thread %d\n", i);
exit(-1);
}
}
udp ? udp_thread_main(0) : tcp_thread_main(0);
}