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 18

Go to most recent revision | 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 15 wzab
 
35
void * user_cmd_thread(void * a1)
36
{
37
  uint16_t cmd;
38
  int i;
39
  int res;
40 18 wzab
  struct timeval tv;
41
  double tstart=0.0;
42
  sleep(2);
43
  for(i=0;i<=2;i++) {
44
    if(active[i]) {
45
      res = ioctl(frs[i],L3_V1_IOC_STARTMAC,NULL);
46
      printf("Result of startmac for slave %d : %d\n",i,res);
47
    }
48
  }
49
  gettimeofday(&tv, NULL);
50
  tstart=tv.tv_sec+1.0e-6*tv.tv_usec;
51 15 wzab
  //Send three commands to each slave
52
  //Waiting 10 seconds
53
  for(cmd=0x112; cmd<=0x114; cmd++) {
54
    sleep(10);
55
    //Send user command
56
    for(i=0;i<=2;i++) {
57
      if(active[i]) {
58
        int j;
59
        struct l3_v1_usercmd uc;
60
        uc.cmd=cmd;
61
        uc.arg=0x56789abc;
62
        uc.timeout=2;
63
        uc.nr_of_retries=20;
64
        res = ioctl(frs[i],L3_V1_IOC_USERCMD,&uc);
65
        printf("Result of usercmd for slave %d : %d\n",i,res);
66
        for(j=0;j<12;j++) {
67
          printf("%2.2x",(int)(uc.resp[j]));
68
        }
69
        printf("\n");
70
      }
71
    }
72
  }
73 18 wzab
  //Wait for 500 seconds
74
  sleep(500);
75
  for(i=0;i<=2;i++) {
76
    if(active[i]) {
77
      res = ioctl(frs[i],L3_V1_IOC_STOPMAC,NULL);
78
      printf("Result of usercmd for slave %d : %d\n",i,res);
79
    }
80
  }
81
  gettimeofday(&tv, NULL);
82
  tend=tv.tv_sec+1.0e-6*tv.tv_usec;
83
  tend=tend-tstart;
84 15 wzab
}
85
 
86
void main(int argc, char * argv[])
87
{
88
  struct l3_v1_buf_pointers bp;
89
  struct l3_v1_slave sl[3] = {
90
    {
91
      .mac = {0xde, 0xad, 0xba, 0xbe, 0xbe,0xef},
92 18 wzab
      .devname = "eth0"
93 15 wzab
    },
94
    {
95
      .mac = {0xde, 0xad, 0xba, 0xbe, 0xbe,0xe1},
96 18 wzab
      .devname = "eth0"
97 15 wzab
    },
98
    {
99
      .mac = {0xde, 0xad, 0xba, 0xbe, 0xbe,0xe2},
100 18 wzab
      .devname = "eth0"
101 15 wzab
    }
102
  };
103
  int i,j;
104
  int res;
105
  int blen[3];
106
  uint64_t data[3] = {0,0,0};
107
  long long total_len[3]={0,0,0};
108
  unsigned char * v[3];
109
  int stop;
110
  struct sched_param s;
111
  s.sched_priority = 90;
112
  //Read active channels
113
  for(i=1;i<argc;i++) {
114
    int n = atoi(argv[i]);
115
    if ((n>=0) && (n<=2))
116
      active[n]=1;
117
  }
118
  printf("sched=%d\n",sched_setscheduler(0,SCHED_RR,&s));
119
  //Prepare all slaves to work
120
  for(i=0;i<3;i++) {
121
    char devname[30];
122
    sprintf(devname,"/dev/l3_fpga%d",i);
123
    frs[i]=open(devname,O_RDONLY);
124
    if(frs[i]<0) {
125
      printf("I can't open device %s\b",devname);
126
      perror("");
127
      exit(1);
128
    }
129
    //Get the length of the buffer
130
    blen[i] = ioctl(frs[i],L3_V1_IOC_GETBUFLEN,NULL);
131
    //Set the wakeup threshold
132
    res=ioctl(frs[i],L3_V1_IOC_SETWAKEUP,2000000);
133
    printf("length of buffer: %d, result of set wakeup: %d\n",blen[i],res);
134
    v[i]=(unsigned char *)mmap(0,blen[i],PROT_READ,MAP_PRIVATE,frs[i],0);
135
    if(!v[i]) {
136
      printf("mmap for device %s failed\n",devname);
137
      exit(1);
138
    }
139
  }
140
  //Start the transmission
141
  for(i=0;i<=2;i++) {
142
    if(active[i]) {
143
      res = ioctl(frs[i],L3_V1_IOC_GETMAC,&sl[i]);
144
      printf("Result of get for slave %d : %d\n",i,res);
145 18 wzab
    } else {
146
      leave[i]=1;
147 15 wzab
    }
148
  }
149 18 wzab
  //Start the second thread, which uses user commands
150
  pthread_create(&ucmd_thread, NULL, user_cmd_thread, NULL);
151 15 wzab
  int first_served=0;
152
  do{
153 18 wzab
    struct pollfd pfd[3];
154 15 wzab
    int ptr=0;
155
    int len=0;
156
    int pres;
157 18 wzab
    for(i=0;i<3;i++) {
158
      pfd[i].fd = active[i] ? frs[i] : -1;
159
      pfd[i].events = POLLIN;
160
      pfd[i].revents = 0;
161
    }
162 15 wzab
    //Wait for data using "poll"
163 18 wzab
    pres = poll(pfd,3,1000);
164 15 wzab
    if(pres<0) {
165
      perror("Error in poll:");
166
      exit(1);
167
    }
168
    first_served = (first_served+1) %3; //Rotate priority of slaves
169
    for(j=0;j<3;j++) {
170
      i=(j+first_served) % 3;
171
      if(pfd[i].revents) {
172
        int ofs=0;
173
        len = ioctl(frs[i],L3_V1_IOC_READPTRS,&bp);
174 18 wzab
        if(bp.eof) leave[i]=1;
175 15 wzab
        //OK. The data are read, let's analyze them
176
        while (bp.head != bp.tail)  {
177
          uint64_t c;
178
          c = be64toh(*(uint64_t *)(v[i]+bp.tail));
179 18 wzab
          bp.tail=(bp.tail+8) & (blen[i]-1); //Adjust tail pointer modulo blen[i]
180 15 wzab
          if (__builtin_expect((c != data[i]), 0)) {
181 18 wzab
            printf("Error! received: %llx expected: %llx position: %8.8x\n",c,data[i],total_len[i]+ofs);
182 15 wzab
            exit(1);
183
          }
184
          data[i] += 0x1234567809abcdefL;
185
          ofs++;
186
        }
187
        total_len[i] += len;
188 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);
189 15 wzab
        ioctl(frs[i],L3_V1_IOC_WRITEPTRS,len);
190
      }
191
    }
192 18 wzab
    fflush(stdout);
193
  } while ((leave[0] && leave[1] && leave[2]) == 0);
194
  for(i=0;i<=2;i++) {
195
    if(active[i]) {
196
      res = ioctl(frs[i],L3_V1_IOC_FREEMAC,0);
197 15 wzab
    }
198 18 wzab
    munmap(v[i],blen[i]);
199
    close(frs[i]);
200
  }
201
  pthread_join(ucmd_thread, NULL);
202 15 wzab
  fprintf(stderr,"act0:%d act1:%d act2:%d\n",active[0],active[1],active[2]);
203
  for(i=0;i<3;i++) {
204
    fprintf(stderr,"total data %d=%lld time=%g throughput=%g [Mb/s]\n",i,total_len[i], tend, total_len[i]/tend*8.0);
205
  }
206
}

powered by: WebSVN 2.1.0

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