-
Notifications
You must be signed in to change notification settings - Fork 7
/
udp_tx.cpp
411 lines (353 loc) · 14 KB
/
udp_tx.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
405
406
407
408
409
410
411
#define USE_AP_INT
#include "axi_word.hpp"
#include "checksum_calc.hpp"
#include "utils.hpp"
#include <hls/ap_int.hpp>
using hls::ap_uint;
#include <hls/streaming.hpp>
#include <stdlib.h>
struct metadata {
ap_uint<32> src_addr;
ap_uint<32> dest_addr;
ap_uint<16> src_port;
ap_uint<16> dest_port;
};
void udpTxReadFunction(hls::FIFO<AxiWord> &data_in,
hls::FIFO<metadata> &metadata_in,
hls::FIFO<ap_uint<16>> &length_in,
hls::FIFO<AxiWord> &data_out,
hls::FIFO<AxiWord> &checksum_out) {
#pragma HLS function pipeline
// From http://www.faqs.org/rfcs/rfc768.html
//
// Checksum is the 16-bit one's complement of the one's complement sum of a
// pseudo header of information from
//
// 1. the pseduo IP header, contains the source address, the destination
// address, the protocol, and the UDP length. This information gives
// protection against misrouted datagrams.
//
// Please note that this is different from the real IP header.
//
// 0 7 8 15 16 23 24 31
// +--------+--------+--------+--------+
// | source address |
// +--------+--------+--------+--------+
// | destination address |
// +--------+--------+--------+--------+
// | zero |protocol| UDP length |
// +--------+--------+--------+--------+
//
// 2. the UDP header (8 bytes)
// a. Length is the length in octets of this user datagram
// including this header and the data. (minimum length is eight.)
// b. Checksum field is zero when calculating checksum
//
// 0 7 8 15 16 23 24 31
// +--------+--------+--------+--------+
// | Source | Destination |
// | Port | Port |
// +--------+--------+--------+--------+
// | | |
// | Length | Checksum |
// +--------+--------+--------+--------+
//
// 3. the data, padded with zero octets at the end (if necessary) to
// make a multiple of two octets.
//
// So in summary,
// the checksum = src addr + dest addr + src port + dest port
// + 2 * (data_len + 8) + protocol
// + data
// Order doesn't seem to matter too much as long as they are all added
// together, so this give us some flexibility in terms of when and what to
// feed into the module to calcualte checksum.
static enum State {
IDLE = 0,
PSEUDOHEADER,
FORWARD,
RESIDUE
} udp_tx_r_state;
static ap_uint<16> packet_length = 0;
// Temporary buffer for the data to be sent in the next iteration
static ap_uint<32> remaining = 0;
static ap_uint<32> remaining_extra = 0;
// data to be sent through AXI
AxiWord output_word;
output_word.keep = 0xFF;
output_word.last = 0;
switch (udp_tx_r_state) {
case IDLE:
if (!metadata_in.empty() && !length_in.empty()) {
// Read metadata
metadata temp_metadata = metadata_in.read();
// Read packet length
// Increase the length to take the UDP header into account.
packet_length = length_in.read() + 8;
// create the first data for checksum and downstream, which contains
// udp length (in a format of IP header) + source addr
// 0x1100 is the protocol used
output_word.data = (ByteSwap32(temp_metadata.src_addr),
ByteSwap16(packet_length), ap_uint<16>(0x1100));
// also prepare the next data for checksum and downstream, which has
// dest addr, src & dest port
remaining = ByteSwap32(temp_metadata.dest_addr);
remaining_extra = (ByteSwap16(temp_metadata.dest_port),
ByteSwap16(temp_metadata.src_port));
udp_tx_r_state = PSEUDOHEADER;
data_out.write(output_word);
checksum_out.write(output_word);
}
break;
case PSEUDOHEADER:
output_word.data = (remaining_extra, remaining);
remaining = (ap_uint<16>(0x0000), ByteSwap16(packet_length));
packet_length -= 8;
udp_tx_r_state = FORWARD;
data_out.write(output_word);
checksum_out.write(output_word);
break;
case FORWARD:
// This state streams all the payload data into both the checksum
// calculation stage and the next stage, reformatting them as required
if (!data_in.empty()) {
// read input word
AxiWord input_word = data_in.read();
output_word.data = (input_word.data(31, 0), remaining);
remaining = input_word.data(63, 32);
if (packet_length > 8) {
packet_length -= 8;
} else if (packet_length > 4) {
packet_length -= 4;
udp_tx_r_state = RESIDUE;
} else {
output_word.keep =
(ap_uint<4>(Length2Keep(packet_length)), ap_uint<4>(0xF));
output_word.last = 1;
packet_length = 0;
udp_tx_r_state = IDLE;
}
data_out.write(output_word);
checksum_out.write(output_word);
}
break;
case RESIDUE:
output_word.data = (ap_uint<32>(0), remaining);
output_word.keep = Length2Keep(packet_length);
output_word.last = 1;
packet_length = 0;
udp_tx_r_state = IDLE;
data_out.write(output_word);
checksum_out.write(output_word);
break;
}
}
void udpTxWriteFunction(hls::FIFO<AxiWord> &data_in,
hls::FIFO<ap_uint<16>> &checksum_in,
hls::FIFO<AxiWord> &data_out) {
#pragma HLS function pipeline
// IP Header
//
// 0 1 2 3
// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
// +-------+-------+---------------+-------------------------------+
// |Version| IHL |Type of Service| Total Length |
// +-------+-------+---------------+-----+-------------------------+
// | Identification |Flags| Fragment Offset |
// +---------------+---------------+-----+-------------------------+
// | Time to Live | Protocol | Header Checksum |
// +---------------+---------------+-------------------------------+
// | Source Address |
// +---------------------------------------------------------------+
// | Destination Address |
// +-----------------------------------------------+---------------+
// | Options | Padding |
// +-----------------------------------------------+---------------+
//
// In the current implementaiton, we don't have any option, so the IP header
// include only the first 5 rows (i.e. 20 bytes)
static enum State { IDLE, IP1, IP2, IP3, FORWARD } udp_tx_w_state;
static ap_uint<32> remaining = 0;
AxiWord output_word;
output_word.keep = 0xFF;
output_word.last = 0;
switch (udp_tx_w_state) {
case IDLE:
if (!data_in.empty()) {
AxiWord input_word = data_in.read();
// add 20 bytes to the IP header length
ap_uint<16> temp_length = ByteSwap16(input_word.data(31, 16)) + 20;
output_word.data =
(ap_uint<32>(0), ByteSwap16(temp_length), ap_uint<16>(0x0045));
remaining = input_word.data(63, 32);
udp_tx_w_state = IP1;
data_out.write(output_word);
}
break;
case IP1:
output_word.data = (remaining, ap_uint<32>(0x000011FF));
udp_tx_w_state = IP2;
data_out.write(output_word);
break;
case IP2:
if (!data_in.empty()) {
output_word = data_in.read();
udp_tx_w_state = IP3;
data_out.write(output_word);
}
break;
case IP3:
if (!data_in.empty() && !checksum_in.empty()) {
ap_uint<16> checksum = checksum_in.read();
output_word = data_in.read();
output_word.data(31, 16) = checksum;
if (output_word.last) {
udp_tx_w_state = IDLE;
} else {
udp_tx_w_state = FORWARD;
}
data_out.write(output_word);
}
break;
case FORWARD:
if (!data_in.empty()) {
output_word = data_in.read();
if (output_word.last) {
udp_tx_w_state = IDLE;
}
data_out.write(output_word);
}
break;
}
}
// FIXME: have to create a wrapper function for the shared checksumCalculation,
// otherwise we will have multiple implementations for the same module when we
// integrate multiple projects together
void udpTxChecksum(hls::FIFO<AxiWord> &data_in,
hls::FIFO<ap_uint<16>> &checksum_out) {
#pragma HLS function pipeline
checksumCalculation(data_in, checksum_out);
}
void udpTx(hls::FIFO<AxiWord> &data_in, hls::FIFO<metadata> &metadata_in,
hls::FIFO<ap_uint<16>> &length_in, hls::FIFO<AxiWord> &data_out) {
#pragma HLS function top
#pragma HLS function dataflow
// Declare intermediate hls::FIFOs for inter-function communication
// Note that the FIFO size should be big enough to hold the whole packet
static hls::FIFO<AxiWord> read2write(8192);
// FIXME: Need to use big FIFOs here only to run software testbench
// properly. The FIFO depth can be reduce to 2 when generating hardware to
// save resources.
static hls::FIFO<AxiWord> read2checksum(8192);
static hls::FIFO<ap_uint<16>> checksum2write(8192);
udpTxReadFunction(data_in, metadata_in, length_in, read2write,
read2checksum);
udpTxChecksum(read2checksum, checksum2write);
udpTxWriteFunction(read2write, checksum2write, data_out);
}
int main() {
hls::FIFO<AxiWord> data_in(1024);
hls::FIFO<metadata> metadata_in(1024);
hls::FIFO<ap_uint<16>> length_in(1024);
hls::FIFO<AxiWord> data_out(1024);
AxiWord input_word;
metadata input_md;
ap_uint<16> input_length;
FILE *tx_input = fopen("in.dat", "r");
FILE *tx_output = fopen("out.dat", "w");
if(!tx_input || !tx_output) {
printf("FAIL: Unable to open data file.\n");
return -1;
}
char line[8192];
char *ptr = line;
bool more_input = true;
unsigned bytes_left = 0;
// run extra iterations after the data_out fifo is empty to make sure we
// completely flush out all data from the pipeline
const unsigned EXTRA_ITER = 50;
unsigned additional_iter = 1;
while (more_input || !data_out.empty() || additional_iter != EXTRA_ITER) {
if (more_input) {
if (bytes_left == 0) {
if ((more_input = fgets(line, sizeof(line), tx_input))) {
ptr = line;
input_md.dest_addr = strtoul(ptr, &ptr, 16);
input_md.src_addr = strtoul(ptr, &ptr, 16);
input_md.dest_port = strtoul(ptr, &ptr, 16);
input_md.src_port = strtoul(ptr, &ptr, 16);
input_length = strtoul(ptr, &ptr, 16);
metadata_in.write(input_md);
length_in.write(input_length);
// skip the extra space
ptr++;
unsigned data_length = strlen(ptr) - 1;
bytes_left = data_length / 2;
for (unsigned i = 0; i < 14; i++)
ptr[data_length + i] = '0';
}
}
if (more_input) {
char buf[17] = {0};
strncpy(buf, ptr, 16);
input_word.data = ByteSwap64(strtoull(buf, NULL, 16));
input_word.keep = (bytes_left <= 8) ? Length2Keep(bytes_left)
: ap_uint<8>(0xFF);
input_word.last = (bytes_left <= 8);
data_in.write(input_word);
if (input_word.last) {
bytes_left = 0;
} else {
bytes_left -= 8;
ptr += 16;
}
}
}
if (!data_out.empty()) {
AxiWord output_word = data_out.read();
if (output_word.last) {
unsigned len = Keep2Length(output_word.keep);
fprintf(tx_output, "%0*llx", 2 * len,
((ByteSwap64(output_word.data)) >> (8 * (8 - len)))
.to_uint64());
fprintf(tx_output, "\n");
} else {
fprintf(tx_output, "%016llx",
ByteSwap64(output_word.data).to_uint64());
}
additional_iter = 0;
}
additional_iter++;
udpTx(data_in, metadata_in, length_in, data_out);
}
fclose(tx_input);
fclose(tx_output);
// compare the out.dat with the golden output
FILE *tx_expected = fopen("expected_out.dat", "r");
tx_output = fopen("out.dat", "r");
if(!tx_expected || !tx_output) {
printf("FAIL: Unable to open data file.\n");
return -1;
}
char ch1, ch2;
int error = 0, pos = 0, line_num = 1;
while ((ch1 = fgetc(tx_output)) != EOF && (ch2 = fgetc(tx_expected)) != EOF) {
pos++;
if (ch1 == '\n' && ch2 == '\n') {
line_num++;
pos = 0;
}
if (ch1 != ch2) {
error++;
printf("Line %d, Column %d, data mismatch.\n", line_num, pos);
break;
}
}
fclose(tx_expected);
fclose(tx_output);
if (error == 0)
printf("PASS\n");
else
printf("FAIL\n");
return error;
}