-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDPDKConnection.cc
299 lines (249 loc) · 6.45 KB
/
DPDKConnection.cc
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
#include <string>
#include <map>
#include <rte_config.h>
#include <rte_cycles.h>
#include <rte_ether.h>
#include <rte_lcore.h>
#include "ConnectionOptions.h"
#include "ConnectionStats.h"
#include "Operation.h"
#include "DPDKConnection.h"
#include "binary_protocol.h"
#include "log.h"
#include "dpdktcp.h"
void DPDKConnection::pop_op(Operation *op)
{
assert(op_queue.size() > 0);
op_queue.pop();
if (op_queue.size() > 0) {
switch (op->type) {
case Operation::GET:
read_state = WAITING_FOR_GET;
break;
case Operation::SET:
read_state = WAITING_FOR_SET;
break;
default:
DIE("Not implemented.");
}
} else {
read_state = IDLE;
}
}
bool DPDKConnection::consume_tcp_binary_response(const char *data, size_t length)
{
if (remaining)
consume_rest(data, length);
else
consume_header(data, length);
return remaining == 0;
}
void DPDKConnection::consume_header(const char *data, size_t length)
{
assert(length >= 24);
binary_header_t *h = (binary_header_t *) data;
assert(h->magic == 0x81);
assert(h->opcode == CMD_GET || h->opcode == CMD_SET);
size_t targetLen = 24 + __builtin_bswap32(h->body_len);
assert(length <= targetLen);
// if something other than success, count it as a miss
if (h->opcode == CMD_GET && h->status)
stats.get_misses++;
stats.rx_bytes += targetLen;
remaining = targetLen - length;
}
void DPDKConnection::consume_rest(const char *data, size_t length)
{
assert(length <= remaining);
remaining -= length;
}
void DPDKConnection::read_callback(const char *data, size_t len)
{
Operation *op;
double now;
if (!consume_tcp_binary_response(data, len))
return;
op = &op_queue.front();
now = get_time();
op->end_time = now;
switch (op->type) {
case Operation::GET:
assert(read_state == WAITING_FOR_GET);
stats.log_get(*op);
break;
case Operation::SET:
assert(read_state == WAITING_FOR_SET);
stats.log_set(*op);
break;
default:
DIE("Not implemented.");
}
pop_op(op);
drive_write_machine(now);
}
void DPDKConnection::timer_callback()
{
drive_write_machine();
}
void timer_cb(struct rte_timer *tim, void *arg)
{
DPDKConnection *conn = (DPDKConnection *) arg;
conn->timer_callback();
}
void DPDKConnection::issue_get(string *key, double now)
{
Operation op;
if (now == 0.0)
now = get_time();
op.start_time = now;
op.type = Operation::GET;
op.key = *key;
op_queue.push(op);
uint16_t keylen = op.key.size();
if (read_state == IDLE)
read_state = WAITING_FOR_GET;
struct {
char header[24];
char key[256];
} data;
binary_header_t *h = (binary_header_t *) data.header;
h->magic = 0x80;
h->opcode = CMD_GET;
h->key_len = __builtin_bswap16(keylen);
h->extra_len = 0x00;
h->data_type = 0x00;
h->vbucket = 0;
h->body_len = __builtin_bswap32(keylen);
assert(keylen < 256);
memcpy(data.key, op.key.c_str(), keylen);
tcp_send(pcb, &data, 24 + keylen);
stats.tx_bytes += 24 + keylen;
}
void DPDKConnection::issue_set(string* key, const char* value, int length, double now)
{
Operation op;
uint16_t keylen = key->size();
op.start_time = now;
op.type = Operation::SET;
op.key = *key;
op_queue.push(op);
if (read_state == IDLE)
read_state = WAITING_FOR_SET;
struct {
char header[32];
char keyvalue[32768];
} data;
binary_header_t *h = (binary_header_t *) data.header;
h->magic = 0x80;
h->opcode = CMD_SET;
h->key_len = __builtin_bswap16(keylen);
h->extra_len = 0x08;
h->data_type = 0x00;
h->vbucket = 0;
h->body_len = __builtin_bswap32(keylen + 8 + length);
assert(keylen + length < 32768);
memcpy(data.keyvalue, op.key.c_str(), keylen);
memcpy(data.keyvalue + keylen, value, length);
tcp_send(pcb, &data, 32 + keylen + length);
stats.tx_bytes += 32 + keylen + length;
}
void DPDKConnection::issue_something(double now)
{
string key = keygen->generate(lrand48() % options.records);
if (drand48() < options.update) {
int index = lrand48() % (1024 * 1024);
issue_set(&key, &random_char[index], valuesize->generate(), now);
} else {
issue_get(&key, now);
}
}
void DPDKConnection::drive_write_machine(double now)
{
if (now == 0.0)
now = get_time();
double delay;
while (1) {
switch (write_state) {
case INIT_WRITE:
delay = iagen->generate();
next_time = now + delay;
// TODO: cache rte_get_timer_hz
rte_timer_reset(&timer, delay * rte_get_timer_hz(), SINGLE, rte_lcore_id(), timer_cb, this);
write_state = WAITING_FOR_TIME;
break;
case ISSUING:
if (op_queue.size() >= (size_t) options.depth) {
write_state = WAITING_FOR_OPQ;
break;
} else if (now < next_time) {
write_state = WAITING_FOR_TIME;
break;
}
issue_something(now);
stats.log_op(op_queue.size());
delay = iagen->generate();
next_time += delay;
break;
case WAITING_FOR_TIME:
write_state = ISSUING;
if (now < next_time) {
if (!rte_timer_pending(&timer)) {
delay = next_time - now;
// TODO: cache rte_get_timer_hz
rte_timer_reset(&timer, delay * rte_get_timer_hz(), SINGLE, rte_lcore_id(), timer_cb, this);
}
return;
}
write_state = ISSUING;
break;
case WAITING_FOR_OPQ:
if (op_queue.size() >= (size_t) options.depth)
return;
write_state = ISSUING;
break;
default:
DIE("Not implemented");
}
}
}
DPDKConnection::DPDKConnection(string hostname, int port, options_t options, bool sampling) :
Connection(sampling), connected(false), options(options)
{
pcb = tcp_create(this);
struct rte_ether_addr remote_mac;
parse_mac(args.server_mac_arg, &remote_mac);
tcp_connect(pcb, &remote_mac, parse_ip(hostname.c_str()), port);
remaining = 0;
read_state = IDLE;
write_state = INIT_WRITE;
iagen = createGenerator(options.ia);
iagen->set_lambda(options.lambda);
valuesize = createGenerator(options.valuesize);
auto keysize = createGenerator(options.keysize);
keygen = new KeyGenerator(keysize, options.records);
rte_timer_init(&timer);
}
void on_tcp_connected(void *arg)
{
DPDKConnection *conn = (DPDKConnection *) arg;
conn->connected = true;
}
void on_tcp_recv(void *arg, char *data, size_t len)
{
DPDKConnection *conn = (DPDKConnection *) arg;
conn->read_callback(data, len);
}
void parse_mac(const char *str, struct rte_ether_addr *res)
{
int ret;
ret = sscanf(str, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &res->addr_bytes[0], &res->addr_bytes[1], &res->addr_bytes[2], &res->addr_bytes[3], &res->addr_bytes[4], &res->addr_bytes[5]);
assert(ret == 6);
}
uint32_t parse_ip(const char *str)
{
int ret;
uint8_t a, b, c, d;
ret = sscanf(str, "%hhu.%hhu.%hhu.%hhu", &a, &b, &c, &d);
assert(ret == 4);
return IP4(a, b, c, d);
}