-
Notifications
You must be signed in to change notification settings - Fork 13
/
emu-top.cpp
405 lines (344 loc) · 13 KB
/
emu-top.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
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
//
// Copyright (c) 2016-2017 Haggai Eran, Gabi Malka, Lior Zeno, Maroun Tork
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation and/or
// other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
#include "emu.hpp"
#include "nica-top.hpp"
#include "threshold-impl.hpp"
#include "passthrough-impl.hpp"
#include "pktgen-impl.hpp"
#include "memcached-ik.hpp"
#include <boost/preprocessor/iteration/local.hpp>
#include <mutex>
namespace emulation {
template <typename T>
static void var_access(T& lhs, uint32_t* rhs, bool read)
{
if (read)
*rhs = lhs;
else
lhs = *rhs;
}
struct gateway_wrapper {
hls_ik::gateway_registers& gateway;
gateway_wrapper(hls_ik::gateway_registers& r) : gateway(r) {}
void reg_access(uint32_t address, uint32_t* value, bool read)
{
switch (address) {
case 0x0: {
uint32_t cmd = gateway.cmd.addr | (gateway.cmd.go << 31);
var_access(cmd, value, read);
gateway.cmd.addr = cmd & 0x7fffffff;
gateway.cmd.go = cmd >> 31;
break;
}
case 0x8: /* data_i */
case 0x10: /* data_o */
var_access(gateway.data, value, read);
break;
case 0x18:
var_access(gateway.done, value, read);
break;
case 0x14: // data_o valid
case 0x1c: // done valid
if (read) *value = 1;
break;
default:
std::cerr << "Unknown address in gateway: " << address << '\n';
break;
}
}
};
struct virt_gateway_wrapper : public gateway_wrapper {
hls_ik::virt_gateway_registers& gateway;
virt_gateway_wrapper(hls_ik::virt_gateway_registers& r) : gateway_wrapper(r.common), gateway(r) {}
void reg_access(uint32_t address, uint32_t* value, bool read)
{
switch (address) {
case 0x20:
var_access(gateway.ikernel_id, value, read);
break;
default:
gateway_wrapper::reg_access(address, value, read);
break;
}
}
};
struct ikernel_wrapper {
hls_ik::ports ports;
hls_ik::tc_ikernel_data_counts tc;
hls_ik::ikernel_id id;
virt_gateway_wrapper gateway;
hls_ik::virt_gateway_registers gateway_regs;
using ikernel_top_func = std::function<void(hls_ik::ports&,
hls_ik::ikernel_id&, hls_ik::virt_gateway_registers&,
hls_ik::tc_ikernel_data_counts&)>;
ikernel_top_func func;
ikernel_wrapper() :
gateway(gateway_regs)
{}
void init(size_t i)
{
std::string ikernel_env = std::string("IKERNEL") + std::to_string(i);
char *ikernel_name = std::getenv(ikernel_env.c_str());
std::string ikernel_str = ikernel_name ? ikernel_name : "threshold";
if (ikernel_str == "threshold")
func = threshold_top;
else if (ikernel_str == "passthrough")
func = passthrough_top;
else if (ikernel_str == "pktgen")
func = pktgen_top;
else if (ikernel_str == "memcached")
func = memcached_top;
else
throw std::exception();
hls_ik::init(tc);
}
void step()
{
func(ports, id, gateway.gateway, tc);
}
void reg_access(uint32_t address, uint32_t* value, bool read)
{
if (address >= 0x14 && address <= 0x38)
return gateway.reg_access(address - 0x14, value, read);
switch (address) {
case 0x0:
case 0x4:
case 0x8:
case 0xc:
var_access(*(uint32_t *)(id.uuid + address), value, read);
break;
case 0x10: // uuid valid
if (read) *value = 1;
break;
case 0x50: {
uint32_t msn_ring = ports.host_credit_regs.reset;
msn_ring <<= ports.host_credit_regs.max_msn.width;
msn_ring |= ports.host_credit_regs.max_msn;
msn_ring <<= ports.host_credit_regs.ring_id.width;
msn_ring |= ports.host_credit_regs.ring_id;
var_access(msn_ring, value, read);
ports.host_credit_regs.ring_id = msn_ring;
msn_ring >>= ports.host_credit_regs.ring_id.width;
ports.host_credit_regs.max_msn = msn_ring;
msn_ring >>= ports.host_credit_regs.max_msn.width;
ports.host_credit_regs.reset = msn_ring;
break;
}
default:
std::cerr << "Unknown address in ikernel: " << address << '\n';
break;
}
}
};
static nica_config cfg;
static nica_stats stats;
static trace_event events[NUM_TRACE_EVENTS];
static mlx::stream prt_nw2sbu("prt_nw2sbu"),
sbu2prt_nw("sbu2prt_nw"),
prt_cx2sbu("prt_cx2sbu"),
sbu2prt_cx("sbu2prt_cx");
static std::mutex emulation_interface_mutex;
static size_t num_ikernels = 0;
static gateway_wrapper n2h_flow_table_gateway(cfg.n2h.common.flow_table_gateway),
h2n_flow_table_gateway(cfg.h2n.common.flow_table_gateway),
n2h_custom_ring_gateway(cfg.n2h.custom_ring_gateway);
static tc_ports h2n_tc, n2h_tc;
static std::vector<ikernel_wrapper> init_ikernels()
{
const char *num_ikernels_str = std::getenv("NUM_IKERNELS") ?: "1";
num_ikernels = std::stoi(num_ikernels_str);
if (num_ikernels > NUM_IKERNELS)
throw std::exception();
std::vector<ikernel_wrapper> ikernels(num_ikernels);
for (size_t i = 0; i < num_ikernels; ++i)
ikernels[i].init(i);
return ikernels;
}
static std::vector<ikernel_wrapper> ikernels = init_ikernels();
void step()
{
std::lock_guard<std::mutex> lock(emulation_interface_mutex);
nica(prt_nw2sbu, sbu2prt_nw, prt_cx2sbu, sbu2prt_cx, &cfg, &stats, events
#define BOOST_PP_LOCAL_MACRO(n) \
, ikernels[n].ports
#define BOOST_PP_LOCAL_LIMITS (0, NUM_IKERNELS - 1)
%:include BOOST_PP_LOCAL_ITERATE()
,
h2n_tc, h2n_tc, n2h_tc, n2h_tc
);
for (auto& ik : ikernels)
ik.step();
}
static void reg_access(uint32_t address, uint32_t* value, bool read)
{
std::lock_guard<std::mutex> lock(emulation_interface_mutex);
if (address >= 0x1000 && address < 0x1000 * (num_ikernels + 1)) {
auto& ik = ikernels[(address / 0x1000) - 1];
ik.reg_access(address - (address / 0x1000) * 0x1000, value, read);
return;
} else if (address >= 0x18 && address <= 0x34) {
return n2h_flow_table_gateway.reg_access(address - 0x18, value, read);
} else if (address >= 0x418 && address <= 0x434) {
return h2n_flow_table_gateway.reg_access(address - 0x418, value, read);
} else if (address >= 0x78 && address <= 0x94) {
return n2h_custom_ring_gateway.reg_access(address - 0x78, value, read);
}
switch (address) {
case 0x10:
var_access(cfg.n2h.common.enable, value, read);
break;
case 0x410:
var_access(cfg.h2n.common.enable, value, read);
break;
case 0x800:
var_access(stats.flow_table_size, value, read);
break;
default:
std::cerr << "Unknown address: " << address << '\n';
break;
}
}
void reg_read(uint32_t address, uint32_t* value)
{
reg_access(address, value, true);
}
void reg_write(uint32_t address, uint32_t value)
{
reg_access(address, &value, false);
}
void send_packet(const packet* pkt)
{
std::lock_guard<std::mutex> lock(emulation_interface_mutex);
for (size_t i = 0; i < pkt->len; i += 32) {
hls_ik::axi_data flit;
const uint8_t cur_len = std::min(pkt->len - i, 32ul);
flit.set_data(pkt->data + i, cur_len);
flit.last = i + cur_len == pkt->len;
mlx::axi4s mlx_flit(flit, 0, 1);
if (pkt->dir == Net)
prt_nw2sbu.write(mlx_flit);
else
prt_cx2sbu.write(mlx_flit);
}
}
struct packet_buffer {
char data[2048]; // TODO
packet pkt;
mlx::stream& out;
packet_buffer(interface dir, mlx::stream& out) :
pkt(), out(out)
{
pkt.data = data;
pkt.dir = dir;
}
packet* get_packet()
{
std::lock_guard<std::mutex> lock(emulation_interface_mutex);
packet *ret_pkt = NULL;
while (!out.empty()) {
hls_ik::axi_data flit = out.read();
if (pkt.len < sizeof(data) - 32)
pkt.len += flit.get_data(data + pkt.len);
if (flit.last) {
ret_pkt = new packet(pkt);
pkt.len = 0;
break;
}
}
return ret_pkt;
}
};
static packet_buffer net_pkt_buffer(Net, sbu2prt_nw),
host_pkt_buffer(Host, sbu2prt_cx);
static uint32_t ntohl(uint32_t const net) {
uint8_t data[4] = {};
memcpy(&data, &net, sizeof(data));
return ((uint32_t) data[3] << 0)
| ((uint32_t) data[2] << 8)
| ((uint32_t) data[1] << 16)
| ((uint32_t) data[0] << 24);
}
static uint16_t htons(uint16_t n) {
return ((((unsigned short)(n) & 0xFF)) << 8) | (((unsigned short)(n) & 0xFF00) >> 8);
}
static uint16_t ip_checksum(void* vdata,size_t length) {
// Cast the data pointer to one that can be indexed.
char* data=(char*)vdata;
// Initialise the accumulator.
uint64_t acc=0xffff;
// Handle any partial block at the start of the data.
unsigned int offset=((uintptr_t)data)&3;
if (offset) {
size_t count=4-offset;
if (count>length) count=length;
uint32_t word=0;
memcpy(offset+(char*)&word,data,count);
acc+=ntohl(word);
data+=count;
length-=count;
}
// Handle any complete 32-bit blocks.
char* data_end=data+(length&~3);
while (data!=data_end) {
uint32_t word;
memcpy(&word,data,4);
acc+=ntohl(word);
data+=4;
}
length&=3;
// Handle any partial block at the end of the data.
if (length) {
uint32_t word=0;
memcpy(&word,data,length);
acc+=ntohl(word);
}
// Handle deferred carries.
acc=(acc&0xffffffff)+(acc>>32);
while (acc>>16) {
acc=(acc&0xffff)+(acc>>16);
}
// If the data began at an odd byte address
// then reverse the byte order to compensate.
if (offset&1) {
acc=((acc&0xff00)>>8)|((acc&0x00ff)<<8);
}
// Return the checksum in network byte order.
return htons(~acc);
}
packet* get_packet()
{
packet* ret = net_pkt_buffer.get_packet();
if (!ret) {
ret = host_pkt_buffer.get_packet();
}
if (!ret) return ret;
if (ret->len < 26) return ret;
if ((*(uint16_t*)(ret->data+12)) != 8) return ret;
std::memset(ret->data + 24, 0, 2);
uint16_t csum = ip_checksum(ret->data + 14, 20);
std::memcpy(ret->data + 24, (void*)&csum, sizeof(uint16_t));
return ret;
}
}