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

Subversion Repositories fade_ether_protocol

[/] [fade_ether_protocol/] [trunk/] [stable_jumbo_frames_version/] [linux/] [receiver2t.c] - Blame information for rev 40

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 15 wzab
/*
2
 * fpga_l3_fade - driver for L3 communication protocol with FPGA based system
3
 * Copyright (C) 2012 by Wojciech M. Zabolotny
4
 * Institute of Electronic Systems, Warsaw University of Technology
5
 *
6
 *  This code is PUBLIC DOMAIN
7
 */
8
 
9
#include<termios.h>
10
#include <sys/types.h>
11
#include <sys/stat.h>
12
#include <sys/mman.h>
13
#include <sys/ioctl.h>
14
#include <fcntl.h>
15
#include <poll.h>
16
#include <unistd.h>
17
#include <stdio.h>
18
#include <strings.h>
19
#include <stdlib.h>
20
#include <stdint.h>
21
#include <endian.h>
22
#include <sys/socket.h>
23
#include <linux/serial.h>
24
#include <sched.h>
25
#include "fpga_l3_fade.h"
26
#include <sys/time.h>
27
#include <pthread.h>
28
 
29
int frs[3]={-1,-1,-1};
30
int active[3]={0,0,0};
31 18 wzab
int leave[3]={0,0,0};
32 15 wzab
pthread_t ucmd_thread;
33 18 wzab
double tend=0.0;
34 19 wzab
#define nic0 "eth0"
35 15 wzab
 
36
void * user_cmd_thread(void * a1)
37
{
38
  uint16_t cmd;
39
  int i;
40
  int res;
41 18 wzab
  struct timeval tv;
42
  double tstart=0.0;
43
  sleep(2);
44
  for(i=0;i<=2;i++) {
45
    if(active[i]) {
46
      res = ioctl(frs[i],L3_V1_IOC_STARTMAC,NULL);
47
      printf("Result of startmac for slave %d : %d\n",i,res);
48
    }
49
  }
50
  gettimeofday(&tv, NULL);
51
  tstart=tv.tv_sec+1.0e-6*tv.tv_usec;
52 15 wzab
  //Send three commands to each slave
53
  //Waiting 10 seconds
54
  for(cmd=0x112; cmd<=0x114; cmd++) {
55
    sleep(10);
56
    //Send user command
57
    for(i=0;i<=2;i++) {
58
      if(active[i]) {
59
        int j;
60
        struct l3_v1_usercmd uc;
61
        uc.cmd=cmd;
62
        uc.arg=0x56789abc;
63
        uc.timeout=2;
64
        uc.nr_of_retries=20;
65
        res = ioctl(frs[i],L3_V1_IOC_USERCMD,&uc);
66
        printf("Result of usercmd for slave %d : %d\n",i,res);
67
        for(j=0;j<12;j++) {
68
          printf("%2.2x",(int)(uc.resp[j]));
69
        }
70
        printf("\n");
71
      }
72
    }
73
  }
74 18 wzab
  //Wait for 500 seconds
75
  sleep(500);
76
  for(i=0;i<=2;i++) {
77
    if(active[i]) {
78
      res = ioctl(frs[i],L3_V1_IOC_STOPMAC,NULL);
79
      printf("Result of usercmd for slave %d : %d\n",i,res);
80
    }
81
  }
82
  gettimeofday(&tv, NULL);
83
  tend=tv.tv_sec+1.0e-6*tv.tv_usec;
84
  tend=tend-tstart;
85 15 wzab
}
86
 
87
void main(int argc, char * argv[])
88
{
89
  struct l3_v1_buf_pointers bp;
90
  struct l3_v1_slave sl[3] = {
91
    {
92
      .mac = {0xde, 0xad, 0xba, 0xbe, 0xbe,0xef},
93 19 wzab
      .devname = nic0
94 15 wzab
    },
95
    {
96
      .mac = {0xde, 0xad, 0xba, 0xbe, 0xbe,0xe1},
97 19 wzab
      .devname = nic0
98 15 wzab
    },
99
    {
100
      .mac = {0xde, 0xad, 0xba, 0xbe, 0xbe,0xe2},
101 19 wzab
      .devname = nic0
102 15 wzab
    }
103
  };
104
  int i,j;
105
  int res;
106
  int blen[3];
107
  uint64_t data[3] = {0,0,0};
108
  long long total_len[3]={0,0,0};
109
  unsigned char * v[3];
110
  int stop;
111
  struct sched_param s;
112
  s.sched_priority = 90;
113
  //Read active channels
114
  for(i=1;i<argc;i++) {
115
    int n = atoi(argv[i]);
116
    if ((n>=0) && (n<=2))
117
      active[n]=1;
118
  }
119
  printf("sched=%d\n",sched_setscheduler(0,SCHED_RR,&s));
120
  //Prepare all slaves to work
121
  for(i=0;i<3;i++) {
122
    char devname[30];
123
    sprintf(devname,"/dev/l3_fpga%d",i);
124
    frs[i]=open(devname,O_RDONLY);
125
    if(frs[i]<0) {
126
      printf("I can't open device %s\b",devname);
127
      perror("");
128
      exit(1);
129
    }
130
    //Get the length of the buffer
131
    blen[i] = ioctl(frs[i],L3_V1_IOC_GETBUFLEN,NULL);
132
    //Set the wakeup threshold
133
    res=ioctl(frs[i],L3_V1_IOC_SETWAKEUP,2000000);
134
    printf("length of buffer: %d, result of set wakeup: %d\n",blen[i],res);
135
    v[i]=(unsigned char *)mmap(0,blen[i],PROT_READ,MAP_PRIVATE,frs[i],0);
136
    if(!v[i]) {
137
      printf("mmap for device %s failed\n",devname);
138
      exit(1);
139
    }
140
  }
141 21 wzab
  //Connect to appropriate FPGAs
142 15 wzab
  for(i=0;i<=2;i++) {
143
    if(active[i]) {
144
      res = ioctl(frs[i],L3_V1_IOC_GETMAC,&sl[i]);
145
      printf("Result of get for slave %d : %d\n",i,res);
146 19 wzab
      if(res<0) {
147
        printf("I couldn't bind Ethernet device %s\n",sl[i].devname);
148
        exit(1);
149
      }
150 18 wzab
    } else {
151
      leave[i]=1;
152 15 wzab
    }
153
  }
154 21 wzab
  //Send FADE reset command, so that FADE core is in predictable state
155
  for(i=0;i<=2;i++) {
156
    if(active[i]) {
157
      int j;
158
      for(j=0;j<3;j++) {
159
        res = ioctl(frs[i],L3_V1_IOC_RESETMAC,&sl[i]);
160
        if(res<0) {
161
          printf("I couldn't RESET Ethernet device %d\n",i);
162
          exit(1);
163
        }
164
      }
165
    } else {
166
      leave[i]=1;
167
    }
168
  }
169 18 wzab
  //Start the second thread, which uses user commands
170
  pthread_create(&ucmd_thread, NULL, user_cmd_thread, NULL);
171 15 wzab
  int first_served=0;
172
  do{
173 18 wzab
    struct pollfd pfd[3];
174 15 wzab
    int ptr=0;
175
    int len=0;
176
    int pres;
177 18 wzab
    for(i=0;i<3;i++) {
178
      pfd[i].fd = active[i] ? frs[i] : -1;
179
      pfd[i].events = POLLIN;
180
      pfd[i].revents = 0;
181
    }
182 15 wzab
    //Wait for data using "poll"
183 18 wzab
    pres = poll(pfd,3,1000);
184 15 wzab
    if(pres<0) {
185
      perror("Error in poll:");
186
      exit(1);
187
    }
188
    first_served = (first_served+1) %3; //Rotate priority of slaves
189
    for(j=0;j<3;j++) {
190
      i=(j+first_served) % 3;
191
      if(pfd[i].revents) {
192
        int ofs=0;
193
        len = ioctl(frs[i],L3_V1_IOC_READPTRS,&bp);
194 18 wzab
        if(bp.eof) leave[i]=1;
195 15 wzab
        //OK. The data are read, let's analyze them
196
        while (bp.head != bp.tail)  {
197
          uint64_t c;
198
          c = be64toh(*(uint64_t *)(v[i]+bp.tail));
199 18 wzab
          bp.tail=(bp.tail+8) & (blen[i]-1); //Adjust tail pointer modulo blen[i]
200 15 wzab
          if (__builtin_expect((c != data[i]), 0)) {
201 18 wzab
            printf("Error! received: %llx expected: %llx position: %8.8x\n",c,data[i],total_len[i]+ofs);
202 15 wzab
            exit(1);
203
          }
204
          data[i] += 0x1234567809abcdefL;
205
          ofs++;
206
        }
207
        total_len[i] += len;
208 18 wzab
        //printf("i=%d len=%d total=%lld head:%d tail: %d revents=%d eof=%d\n",i,len,total_len[i],bp.head, bp.tail,pfd[i].revents,(int)bp.eof);
209 15 wzab
        ioctl(frs[i],L3_V1_IOC_WRITEPTRS,len);
210
      }
211
    }
212 18 wzab
    fflush(stdout);
213
  } while ((leave[0] && leave[1] && leave[2]) == 0);
214 23 wzab
  pthread_join(ucmd_thread, NULL);
215 18 wzab
  for(i=0;i<=2;i++) {
216
    if(active[i]) {
217
      res = ioctl(frs[i],L3_V1_IOC_FREEMAC,0);
218 15 wzab
    }
219 18 wzab
    munmap(v[i],blen[i]);
220
    close(frs[i]);
221
  }
222 15 wzab
  fprintf(stderr,"act0:%d act1:%d act2:%d\n",active[0],active[1],active[2]);
223
  for(i=0;i<3;i++) {
224
    fprintf(stderr,"total data %d=%lld time=%g throughput=%g [Mb/s]\n",i,total_len[i], tend, total_len[i]/tend*8.0);
225
  }
226
}

powered by: WebSVN 2.1.0

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