-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmynetperf.cpp
296 lines (201 loc) · 8.22 KB
/
mynetperf.cpp
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
// (C) 2020-2023 by folkert van heusden <[email protected]>, released under Apache License v2.0
#include <jansson.h>
#include "application.h"
#include "log.h"
#include "pstream.h"
#include "tcp.h"
#include "types.h"
#include "utils.h"
using namespace std::chrono_literals;
void mynetperf_init()
{
}
void mynetperf_deinit()
{
}
void mynetperf_handle_data(session *const session_in, std::unique_lock<std::mutex> *const lck)
{
mynetperf_session_data *session = dynamic_cast<mynetperf_session_data *>(session_in->get_callback_private_data());
if (session->state == mynetperf_session_data::mnp_command) {
const uint8_t *lf = reinterpret_cast<const uint8_t *>(memchr(session->req_data, '\n', session->req_len));
if (lf) {
json_error_t err { 0 };
json_t *command = json_loadb(reinterpret_cast<const char *>(session->req_data), lf - session->req_data, 0, &err); // -1: lf
if (!command) {
DOLOG(ll_info, "mynetperf_handle_data: json decode failed (%s) for \"%s\"\n", err.text, std::string(reinterpret_cast<const char *>(session->req_data), lf - session->req_data).c_str());
session->terminate = true;
return;
}
json_t *rc1 = json_object_get(command, "block_size");
json_t *rc2 = json_object_get(command, "mode");
if (!rc1 || !rc2) {
DOLOG(ll_info, "mynetperf_handle_data: block_size and/or mode missing\n");
json_decref(command);
session->terminate = true;
return;
}
session->block_size = json_integer_value(rc1);
std::string mode = json_string_value(rc2);
if (mode == "receive")
session->state = mynetperf_session_data::mnp_receive;
else if (mode == "send")
session->state = mynetperf_session_data::mnp_send;
else {
json_decref(command);
session->terminate = true;
DOLOG(ll_info, "mynetperf_handle_data: mode invalid\n");
return;
}
std::string reply = "{ \"result\" : \"ok\" }\n";
session_in->get_stream_target()->send_data(session_in, reinterpret_cast<const uint8_t *>(reply.c_str()), reply.size());
DOLOG(ll_info, "mynetperf_handle_data: command \"%s\" received for %zu bytes\n", mode.c_str(), size_t(session->block_size));
json_decref(command);
uint64_t bytes_processed = (lf - session->req_data) + 1;
int64_t bytes_left = session->req_len - bytes_processed;
if (bytes_left > 0) {
memmove(&session->req_data[0], &session->req_data[bytes_processed], bytes_left);
session->req_len = bytes_left;
}
else {
free(session->req_data);
session->req_data = nullptr;
session->req_len = 0;
}
session->data_transferred = 0;
session->start_ts = get_us();
}
else if (session->req_len > 1024 * 1024) {
free(session->req_data);
session->req_data = nullptr;
session->req_len = 0;
session->terminate = true;
}
}
if (session->state == mynetperf_session_data::mnp_receive) {
uint64_t now_ts = get_us();
uint64_t data_left = session->block_size - session->data_transferred;
DOLOG(ll_debug, "mynetperf_handle_data: transfer (receive) requested for %zu bytes\n", size_t(data_left));
if (data_left <= session->req_len) {
int64_t data_keep = session->req_len - data_left;
DOLOG(ll_debug, "mynetperf_handle_data: receive has %zd bytes overshoot\n", size_t(data_keep));
if (data_keep > 0)
memmove(&session->req_data[0], &session->req_data[data_left], data_keep);
session->data_transferred = 0;
session->block_size = 0;
session->req_len = data_keep;
std::string reply = myformat("{ \"result\": \"ok\", \"took\": %zu, \"unit\": \"us\" }\n", size_t(now_ts - session->start_ts));
session_in->get_stream_target()->send_data(session_in, reinterpret_cast<const uint8_t *>(reply.c_str()), reply.size());
session->state = mynetperf_session_data::mnp_command;
DOLOG(ll_debug, "mynetperf_handle_data: block receive succeeded\n");
}
else {
session->data_transferred += session->req_len;
DOLOG(ll_debug, "mynetperf_handle_data: stored %zu bytes in buffer, %zu left\n", session->req_len, size_t(session->block_size - session->data_transferred));
session->req_len = 0;
free(session->req_data);
session->req_data = nullptr;
}
}
if (session->state == mynetperf_session_data::mnp_send) {
uint64_t now_ts = get_us();
uint64_t data_left = session->block_size - session->data_transferred;
DOLOG(ll_debug, "mynetperf_handle_data: transfer (send) requested for %zu bytes\n", size_t(data_left));
const uint64_t max_block_size = 65536; // largest possible mtu on ethernet? (TODO)
uint8_t *data = reinterpret_cast<uint8_t *>(malloc(max_block_size));
while(data_left > 0) {
uint64_t cur_block_size = std::min(max_block_size, data_left);
session->data_transferred += cur_block_size;
data_left -= cur_block_size;
session_in->get_stream_target()->send_data(session_in, data, cur_block_size);
// yield the lock
lck->unlock();
std::this_thread::yield();
lck->lock();
}
free(data);
std::string reply = myformat("{ \"result\": \"ok\", \"took\": %zu, \"unit\": \"us\" }\n", size_t(now_ts - session->start_ts));
session_in->get_stream_target()->send_data(session_in, reinterpret_cast<const uint8_t *>(reply.c_str()), reply.size());
session->state = mynetperf_session_data::mnp_command;
DOLOG(ll_debug, "mynetperf_handle_data: block transmit succeeded\n");
}
}
void mynetperf_thread(session *session_in)
{
set_thread_name("myip-mynetperf");
DOLOG(ll_info, "mynetperf_thread: starting\n");
mynetperf_session_data *session = dynamic_cast<mynetperf_session_data *>(session_in->get_callback_private_data());
std::unique_lock<std::mutex> lck(session->r_lock);
for(;session->terminate == false;) {
if (session->req_data != nullptr)
mynetperf_handle_data(session_in, &lck);
session->r_cond.wait_for(lck, 500ms);
}
DOLOG(ll_info, "mynetperf_thread: terminates\n");
}
bool mynetperf_new_session(pstream *const ps, session *const session)
{
DOLOG(ll_info, "mynetperf_new_session\n");
mynetperf_session_data *ts = new mynetperf_session_data();
ts->req_data = nullptr;
ts->req_len = 0;
ts->buffer = nullptr;
ts->block_size = 0;
ts->state = mynetperf_session_data::mnp_command;
//
ts->data_transferred = 0;
any_addr src_addr = session->get_their_addr();
ts->client_addr = src_addr.to_str();
session->set_callback_private_data(ts);
ts->th = new std::thread(mynetperf_thread, session);
return true;
}
bool mynetperf_new_data(pstream *const ps, session *const session_in, buffer_in data)
{
if (!session_in) {
DOLOG(ll_info, "MYNETPERF: Data for a non-existing session\n");
return false;
}
mynetperf_session_data *session = dynamic_cast<mynetperf_session_data *>(session_in->get_callback_private_data());
int data_len = data.get_n_bytes_left();
DOLOG(ll_debug, "mynetperf_new_data: %d bytes\n", data_len);
if (data_len == 0) {
DOLOG(ll_debug, "MYNETPERF: client closed session\n");
return true;
}
const std::lock_guard<std::mutex> lck(session->r_lock);
session->req_data = reinterpret_cast<uint8_t *>(realloc(session->req_data, session->req_len + data_len));
memcpy(&session->req_data[session->req_len], data.get_bytes(data_len), data_len);
session->req_len += data_len;
session->r_cond.notify_one();
return true;
}
bool mynetperf_session_closed_1(pstream *const ps, session *const session)
{
return true;
}
bool mynetperf_session_closed_2(pstream *const ps, session *const session)
{
session_data *sd = session->get_callback_private_data();
if (sd) {
mynetperf_session_data *esd = dynamic_cast<mynetperf_session_data *>(sd);
esd->terminate = true;
esd->th->join();
delete esd->th;
esd->th = nullptr;
free(esd->req_data);
delete esd;
session->set_callback_private_data(nullptr);
}
return true;
}
port_handler_t mynetperf_get_handler()
{
port_handler_t meta { 0 };
meta.init = mynetperf_init;
meta.deinit = mynetperf_deinit;
meta.new_session = mynetperf_new_session;
meta.new_data = mynetperf_new_data;
meta.session_closed_1 = mynetperf_session_closed_1;
meta.session_closed_2 = mynetperf_session_closed_2;
return meta;
}