OpenCores
URL https://opencores.org/ocsvn/openarty/openarty/trunk

Subversion Repositories openarty

[/] [openarty/] [trunk/] [sw/] [host/] [manping.cpp] - Blame information for rev 35

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 30 dgisselq
////////////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    manping.cpp
4
//
5
// Project:     OpenArty, an entirely open SoC based upon the Arty platform
6
//
7
// Purpose:     To command the network to ping a target.
8
//
9
//
10
// Creator:     Dan Gisselquist, Ph.D.
11
//              Gisselquist Technology, LLC
12
//
13
////////////////////////////////////////////////////////////////////////////////
14
//
15
// Copyright (C) 2015-2016, Gisselquist Technology, LLC
16
//
17
// This program is free software (firmware): you can redistribute it and/or
18
// modify it under the terms of  the GNU General Public License as published
19
// by the Free Software Foundation, either version 3 of the License, or (at
20
// your option) any later version.
21
//
22
// This program is distributed in the hope that it will be useful, but WITHOUT
23
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
24
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
25
// for more details.
26
//
27
// You should have received a copy of the GNU General Public License along
28
// with this program.  (It's in the $(ROOT)/doc directory, run make with no
29
// target there if the PDF file isn't present.)  If not, see
30
// <http://www.gnu.org/licenses/> for a copy.
31
//
32
// License:     GPL, v3, as defined and found on www.gnu.org,
33
//              http://www.gnu.org/licenses/gpl.html
34
//
35
//
36
////////////////////////////////////////////////////////////////////////////////
37
//
38
//
39
#include <stdio.h>
40
#include <stdlib.h>
41
#include <unistd.h>
42
#include <strings.h>
43
#include <ctype.h>
44
#include <string.h>
45
#include <signal.h>
46
#include <assert.h>
47
 
48
#include "port.h"
49
#include "regdefs.h"
50
 
51
#define TXGO            0x04000
52
#define NOHWCRC         0x08000
53
#define NOHWMAC         0x10000
54
#define NETRESET        0x20000
55
 
56
//
57
// Define DONT_INVERT for debugging only, as it will break the interface
58
// test
59
//
60
// #define      DONT_INVERT
61
 
62
 
63
FPGA    *m_fpga;
64
void    closeup(int v) {
65
        m_fpga->kill();
66
        exit(0);
67
}
68
 
69
void    usage(void) {
70
        printf("USAGE: manping EN:RX:xx:xx:xx:xx AR:TY:EN:TX:xx:xx de.st.ip.x ar.ty.ip.x\n");
71
}
72
 
73
bool    strtoenetaddr(char *s, unsigned char *addr) {
74
        char    *p, *c;
75
 
76
        p = s;
77
        addr[0] = (unsigned char)(strtoul(p, NULL, 16)&0x0ff);
78
        c = strchr(p,':');
79
        if((!c) || ((c-p)>=3))
80
                return false;
81
 
82
        p = c+1;
83
        addr[1] = (unsigned char)(strtoul(p, NULL, 16)&0x0ff);
84
        c = strchr(p,':');
85
        if((!c) || ((c-p)>=3))
86
                return false;
87
 
88
        p = c+1;
89
        addr[2] = (unsigned char)(strtoul(p, NULL, 16)&0x0ff);
90
        c = strchr(p,':');
91
        if((!c) || ((c-p)>=3))
92
                return false;
93
 
94
        p = c+1;
95
        addr[3] = (unsigned char)(strtoul(p, NULL, 16)&0x0ff);
96
        c = strchr(p,':');
97
        if((!c) || ((c-p)>=3))
98
                return false;
99
 
100
        p = c+1;
101
        addr[4] = (unsigned char)(strtoul(p, NULL, 16)&0x0ff);
102
        c = strchr(p,':');
103
        if((!c) || ((c-p)>=3))
104
                return false;
105
 
106
        p = c+1;
107
        addr[5] = (unsigned char)(strtoul(p, NULL, 16)&0x0ff);
108
 
109
        return true;
110
}
111
 
112
bool    strtoinetaddr(char *s, unsigned char *addr) {
113
        char    *p, *c;
114
 
115
        p = s;
116
        addr[0] = (unsigned char)(strtoul(p, NULL, 10)&0x0ff);
117
        c = strchr(p,'.');
118
        if((!c) || ((c-p)>3))
119
                return false;
120
 
121
        p = c+1;
122
        addr[1] = (unsigned char)(strtoul(p, NULL, 10)&0x0ff);
123
        c = strchr(p,'.');
124
        if((!c) || ((c-p)>3))
125
                return false;
126
 
127
        p = c+1;
128
        addr[2] = (unsigned char)(strtoul(p, NULL, 10)&0x0ff);
129
        c = strchr(p,'.');
130
        if((!c) || ((c-p)>3))
131
                return false;
132
 
133
        p = c+1;
134
        addr[3] = (unsigned char)(strtoul(p, NULL, 10)&0x0ff);
135
 
136
        return true;
137
}
138
 
139
unsigned        calccrc(const int bytelen, const unsigned *buf) {
140
        const unsigned int      taps = 0xedb88320u;
141
#ifdef  DONT_INVERT
142
        unsigned int    crc = 0;
143
#else
144
        unsigned int    crc = 0xffffffff; // initial value
145
#endif
146
        int     bidx;
147
        int     bp = 0;
148
 
149
        for(bidx = 0; bidx<bytelen; bidx++) {
150
                if (bidx == 14)
151
                        bidx+=2;
152
                unsigned char   byte = buf[(bidx>>2)]>>(24-((bidx&3)<<3));
153
 
154
                // printf("CRC[%2d]: %02x ([%2d]0x%08x)\n", bidx, byte, (bidx>>2), buf[(bidx>>2)]);
155
 
156
                for(int bit=8; --bit>= 0; byte >>= 1) {
157
                        if ((crc ^ byte) & 1) {
158
                                crc >>= 1;
159
                                crc ^= taps;
160
                        } else
161
                                crc >>= 1;
162
                } bp++;
163
        }
164
#ifndef DONT_INVERT
165
        crc ^= 0xffffffff;
166
#endif
167
        // Now, we need to reverse these bytes
168
        // ABCD
169
        unsigned a,b,c,d;
170
        a = (crc>>24); // &0x0ff;
171
        b = (crc>>16)&0x0ff;
172
        c = (crc>> 8)&0x0ff;
173
        d = crc; // (crc    )&0x0ff;
174
        crc = (d<<24)|(c<<16)|(b<<8)|a;
175
 
176
        // printf("%d bytes processed\n", bp);
177
        return crc;
178
}
179
 
180
void    ipchecksum(unsigned *packet) {
181
        int npkt = (packet[0]>>24)&0x0f;
182
        unsigned checksum = 0;
183
 
184
        packet[2] &= 0xffff0000;
185
        printf("PKT[2] set to %08x\n", packet[2]);
186
        printf("checksum = %08x\n", checksum);
187
        for(int i=0; i<npkt; i++)
188
                checksum += packet[i] & 0x0ffff;
189
        printf("checksum = %08x\n", checksum);
190
        for(int i=0; i<npkt; i++)
191
                checksum += (packet[i]>>16)&0x0ffff;
192
        printf("checksum = %08x\n", checksum);
193
        checksum = (checksum & 0x0ffff) + (checksum >> 16);
194
        checksum = (checksum & 0x0ffff) + (checksum >> 16);
195
        packet[2] |= (checksum & 0x0ffff)^0x0ffff;
196
 
197
        printf("PKT[2] set to 0x%08x\n", packet[2]);
198
        checksum = 0;
199
        for(int i=0; i<npkt; i++)
200
                checksum += packet[i] & 0x0ffff;
201
        for(int i=0; i<npkt; i++)
202
                checksum += (packet[i]>>16)&0x0ffff;
203
        checksum = (checksum & 0x0ffff) + (checksum >> 16);
204
        checksum = (checksum & 0x0ffff) + (checksum >> 16);
205
        checksum ^= 0x0ffff;
206
 
207
        assert(checksum == 0);
208
}
209
 
210
void    clear_scope(FPGA *fpga) {
211
        unsigned        scopev;
212
 
213
        scopev = m_fpga->readio(R_NETSCOPE);
214
        int delay = (scopev>>20)&0x0f;
215
        delay = (1<<(delay))-32;
216
        m_fpga->writeio(R_NETSCOPE, (delay));
217
}
218
 
219
int main(int argc, char **argv) {
220
        int     skp=0, port = FPGAPORT;
221
        bool    config_hw_mac = true, config_hw_crc = true;
222
        FPGA::BUSW      txstat;
223
        int     argn;
224
        unsigned        checksum;
225
        unsigned        urand[16], nu = 0;
226
 
227
        {
228
                FILE *fp;
229
                for(int i=0; i<16; i++)
230
                        urand[i] = rand();
231
 
232
                // Now, see if we can do better than the library random
233
                // number generator--but don't fail if we can't.
234
                fp = fopen("/dev/urandom", "r");
235
                if (fp != NULL) {
236
                        int nr = fread(urand, sizeof(short), 16, fp);
237
                        fclose(fp);
238
                }
239
        }
240
 
241
 
242
        FPGAOPEN(m_fpga);
243
 
244
        signal(SIGSTOP, closeup);
245
        signal(SIGHUP, closeup);
246
 
247
        txstat = m_fpga->readio(R_NET_TXCMD);
248
 
249
        // Take the ethernet out of reset
250
        if ((txstat & NETRESET) != 0)
251
                m_fpga->writeio(R_NET_TXCMD, (txstat &=(~NETRESET)));
252
 
253
        unsigned        packet[14];
254
 
255
        unsigned char   smac[6], dmac[6];
256
        unsigned char   sip[4],  dip[4];
257
 
258
        // I know the ethernet MAC of the computer I wish to test with
259
        dmac[0] = 0xc8; dmac[1] = 0x3a; dmac[2] = 0x35;
260
        dmac[3] = 0xd2; dmac[4] = 0x07; dmac[5] = 0xb1;
261
        // And just something from /dev/urandom to create our source address
262
        smac[0] = 0xd2; smac[1] = 0xd8; smac[2] = 0x28;
263
        smac[3] = 0xe8; smac[4] = 0xb0; smac[5] = 0x96;
264
 
265
        // Similarly with the destination IP of the computer I wish to test with
266
        dip[0] = 192; dip[1] = 168; dip[2] = 10; dip[3] = 1;
267
        // and let's pick a source IP just ... somewhere on that network
268
        sip[0] = 192; sip[1] = 168; sip[2] = 10; sip[3] = 22;
269
 
270
        clear_scope(m_fpga);
271
 
272
        argn = 1;
273
 
274
        {
275
                bool    bad_address = false;
276
                char    *badp = NULL;
277
                if ((argn<argc)&&(strchr(argv[argn], ':'))) {
278
                        if (!strtoenetaddr(argv[argn++], dmac)) {
279
                                badp = argv[argn-1];
280
                                bad_address = true;
281
                        } else if ((argn<argc)&&(strchr(argv[argn], ':'))) {
282
                                if (!strtoenetaddr(argv[argn++], smac)) {
283
                                        badp = argv[argn-1];
284
                                        bad_address = true;
285
                                }
286
                        }
287
                } if ((argn<argc)&&(!bad_address)&&(strchr(argv[argn], '.'))) {
288
                        if (!strtoinetaddr(argv[argn++], dip)) {
289
                                badp = argv[argn-1];
290
                                bad_address = true;
291
                        } else if ((argn<argc)&&(strchr(argv[argn], '.'))) {
292
                                if (!strtoinetaddr(argv[argn++], sip)) {
293
                                        badp = argv[argn-1];
294
                                        bad_address = true;
295
                                }
296
                        }
297
                }
298
 
299
                if (bad_address) {
300
                        usage();
301
                        fprintf(stderr, "ERR: could not comprehend address, %s\n", badp);
302
                        exit(EXIT_FAILURE);
303
                }
304
        }
305
 
306
        printf("Building packet\n");
307
        printf("From %3d.%3d.%3d.%3d [%02x:%02x:%02x:%02x:%02x:%02x]\n",
308
                sip[0], sip[1], sip[2], sip[3],
309
                smac[0], smac[1], smac[2], smac[3], smac[4], smac[5]);
310
        printf("To   %3d.%3d.%3d.%3d [%02x:%02x:%02x:%02x:%02x:%02x]\n",
311
                dip[0], dip[1], dip[2], dip[3],
312
                dmac[0], dmac[1], dmac[2], dmac[3], dmac[4], dmac[5]);
313
 
314
 
315
        // Let's build ourselves a ping packet
316
        packet[ 0] = (dmac[0]<<24)|(dmac[1]<<16)|(dmac[2]<<8)|(dmac[3]);
317
        packet[ 1] = (dmac[4]<<24)|(dmac[5]<<16)|(smac[0]<<8)|(smac[1]);
318
        packet[ 2] = (smac[2]<<24)|(smac[3]<<16)|(smac[4]<<8)|(smac[5]);
319
        packet[ 3] = 0x08000800;
320
        packet[ 4] = 0x4500001c; // IPv4, 20byte header, type of service = 0
321
        packet[ 5] = (urand[nu++]&0xffff0000); // Packet ID
322
        packet[ 6] = 0x80010000; // no flags, fragment offset=0, ttl=0, proto=1
323
        packet[ 7] = (sip[0]<<24)|(sip[1]<<16)|(sip[2]<<8)|(sip[3]);
324
        packet[ 8] = (dip[0]<<24)|(dip[1]<<16)|(dip[2]<<8)|(dip[3]);
325
        // Ping payload: type = 0x08 (PING, the response will be zero)
326
        //      CODE = 0
327
        //      Checksum will be filled in later
328
        packet[ 9] = 0x08000000;
329
        // This is the PING identifier and sequence number.  For now, we'll
330
        // just feed it random information--doesn't really matter what
331
        packet[10] = urand[nu++];
332
        // Now, the minimum ethernet packet is 16 words.  So, let's flush
333
        // ourselves out to that minimum length.
334
        packet[11] = 0;
335
        packet[12] = 0;
336
        packet[13] = 0;
337
        packet[14] = 0;
338
 
339
        // Calculate the IP header checksum
340
        ipchecksum(&packet[4]);
341
 
342
        // Calculate the PING payload checksum
343
        checksum  =  packet[ 9] & 0x0ffff;
344
        checksum += (packet[ 9]>>16)&0x0ffff;
345
        checksum +=  packet[10] & 0x0ffff;
346
        checksum += (packet[10]>>16)&0x0ffff;
347
        checksum  = ((checksum >> 16)&0x0ffff) + (checksum & 0x0ffff);
348
        checksum  = ((checksum >> 16)&0x0ffff) + (checksum & 0x0ffff);
349
        packet[ 9] = ((packet[9] & 0xffff0000)|(checksum))^0x0ffff;
350
 
351
        // Calculate the CRC--assuming we'll use it.
352
        packet[15] = calccrc(15*4, packet);
353
 
354
        // Clear any/all pending receiving errors or packets
355
        m_fpga->writeio(R_NET_RXCMD, 0x0fffff);
356
        if (config_hw_mac) {
357
                int ln;
358
 
359
                m_fpga->writeio(R_NET_MACHI, (smac[0]<<8)|(smac[1]));
360
                m_fpga->writeio(R_NET_MACLO, (smac[2]<<24)|(smac[3]<<16)|(smac[4]<<8)|(smac[5]));
361
 
362
                // Now, let's rebuild our packet for the non-hw-mac option,
363
                // now that we know the CRC.  In general, we're just going
364
                // to copy the packet we created earlier, but we need to
365
                // shift things as we do so.
366
                packet[ 0] = (dmac[0]<<24)|(dmac[1]<<16)|(dmac[2]<<8)|(dmac[3]);
367
                packet[ 1] = (dmac[4]<<24)|(dmac[5]<<16)|0x0800;
368
                packet[ 2] = packet[ 4];
369
                packet[ 3] = packet[ 5];
370
                packet[ 4] = packet[ 6];
371
                packet[ 5] = packet[ 7];
372
                packet[ 6] = packet[ 8];
373
                packet[ 7] = packet[ 9];
374
                packet[ 8] = packet[10];
375
                packet[ 9] = packet[11];
376
                packet[10] = packet[12];
377
                packet[11] = packet[13];
378
                packet[12] = packet[14];
379
                packet[13] = packet[15];
380
 
381
                ln = (config_hw_crc)?9:14;
382
                printf("Packet:\n");
383
                for(int i=0; i<14; i++)
384
                        printf("\t%2d: 0x%08x\n", i, packet[i]);
385
 
386
                // Load the packet into the hardware buffer
387
                m_fpga->writei(R_NET_TXBUF, ln, packet);
388
 
389
                // And give it the transmit command.
390 33 dgisselq
                { unsigned cmd;
391
                cmd = TXGO|(ln<<2)|((config_hw_crc)?0:NOHWCRC);
392
                m_fpga->writeio(R_NET_TXCMD, cmd);
393
                printf("Sent TX command: 0x%x\n", cmd);
394
                }
395 30 dgisselq
 
396
        } else {
397
                int     ln;
398
 
399
                ln = (config_hw_crc)?11:12;
400
                printf("Packet:\n");
401
                for(int i=0; i<15; i++)
402
                        printf("\t%3d: 0x%08x\n", i, packet[i]);
403
                printf("\tCRC: 0x%08x\n", packet[15]);
404
 
405
                // Load the packet into the hardware buffer
406
                m_fpga->writei(R_NET_TXBUF, ln, packet);
407
 
408
                // And give it the transmit command
409
                m_fpga->writeio(R_NET_TXCMD, TXGO|NOHWMAC|(ln<<2)|((config_hw_crc)?0:NOHWCRC));
410
        }
411
 
412
        // First, we need to look for any ARP requests, and we'll need to
413
        // respond to them.  If during this time we get a ping response
414
        // packet, we're done.
415
 
416
        printf("\nLooking for a response ...\n");
417
        unsigned rxstat;
418
        int     errcount = 0;
419
        do {
420
                rxstat = m_fpga->readio(R_NET_RXCMD);
421
                if (rxstat & 0x04000) {
422
                        int     rxlen;
423
                        unsigned *buf;
424
                        printf("RX Status = %08x\n", rxstat);
425
                        rxlen = ((rxstat & 0x03fff)+3)>>2;
426
                        buf = new unsigned[rxlen];
427
                        m_fpga->readi(R_NET_RXBUF, rxlen, buf);
428
                        for(int i=0; i<rxlen; i++)
429
                                printf("\tRX[%2d]: 0x%08x\n", i, buf[i]);
430
                        delete[] buf;
431 33 dgisselq
                        // m_fpga->writeio(R_NET_RXCMD, 0xffffff);
432 30 dgisselq
                        break;
433
                }
434 33 dgisselq
        } while(((rxstat & 0x04000)==0)&&(errcount++ < 500));
435 30 dgisselq
 
436
        rxstat = m_fpga->readio(R_NET_RXCMD);
437
        printf("Final Rx Status = %08x\n", rxstat);
438
 
439
 
440
        delete  m_fpga;
441
}
442
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.