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 15

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

powered by: WebSVN 2.1.0

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