OpenCores
URL https://opencores.org/ocsvn/1g_ethernet_dpi/1g_ethernet_dpi/trunk

Subversion Repositories 1g_ethernet_dpi

[/] [1g_ethernet_dpi/] [trunk/] [sw/] [dev/] [test_bfm/] [test_bfm.c] - Blame information for rev 4

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 kuzmi4
#include <stdio.h>
2
 
3
#include <arpa/inet.h> // sockaddr, ..  / ?? net/if.h
4
#include <linux/if.h>
5
#include <linux/if_tun.h>
6
#include <string.h> // memset, ..
7
 
8
#include <sys/types.h>
9
#include <sys/stat.h>
10
#include <fcntl.h> // open, ..
11
 
12
#include <sys/ioctl.h> // ioctl
13
#include <errno.h> // 
14
 
15
#include <stdlib.h> // system
16
 
17
 
18
/* */
19
#include "eth_host_bfm.h" // DPI-C
20
#include "ether.h" // ether_tx, ether_rx_ok, ether_rx
21
/* */
22
 
23
 
24
// ??
25
#define BUFSIZE     2048 // 2KB
26
// ??
27
char buffer[BUFSIZE];
28
 
29
 
30
// tap-if READ
31
int cread(int fd, char *buf, int n){
32
 
33
  int nread;
34
 
35
  if((nread=read(fd, buf, n))<0){
36
    perror("Reading data");
37
    exit(1);
38
  }
39
  return nread;
40
}
41
// tap-if WRITE
42
int cwrite(int fd, char *buf, int n){
43
 
44
  int nwrite;
45
 
46
  if((nwrite=write(fd, buf, n))<0){
47
    perror("Writing data");
48
    exit(1);
49
  }
50
  return nwrite;
51
}
52
 
53
// MAIN
54
int test_bfm(void)
55
{   // dec vars
56
    struct ifreq ifr;
57
    int tap_fd, err;
58
    char tap_nm[IFNAMSIZ] = "tap0";
59
 
60
    // prep
61
    memset(&ifr, 0, sizeof(ifr));
62
    tap_fd = -1;
63
 
64
    // open
65
    tap_fd = open("/dev/net/tun", O_RDWR);
66
    if( tap_fd < 0 ) {
67
        printf("ERR0: fd=%x\n", tap_fd);
68
        return -1;
69
    }
70
    // cfg: {tap-if} + {no-packet-info}
71
    ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
72
    strncpy(ifr.ifr_name, tap_nm, IFNAMSIZ);
73
    err = ioctl(tap_fd, TUNSETIFF, (void *)&ifr); // tun_set_iff: name + flags
74
    if( err < 0 ) {
75
        int errsv = errno;
76
        printf("ERR1: fd=%x, errsv=%x :  %s\n", tap_fd, errsv, strerror(errsv));
77
        close(tap_fd);
78
        return -2;
79
    }
80
    // msg2usr
81
    printf("ifr_name=%s\n", ifr.ifr_name);
82
    //printf("ifr_flags=%x\n", ifr.ifr_flags);
83
 
84
    // tap_fd == 'O_NONBLOCK'
85
    int flags = fcntl(tap_fd, F_GETFL, 0);
86
    fcntl(tap_fd, F_SETFL, flags | O_NONBLOCK);
87
 
88
    // main-loop
89
    int idx=0;
90
    while (1) {
91
 
92
        int ret;
93
        fd_set rd_set;
94
        struct timeval timeout;
95
 
96
        timeout.tv_sec=0; // no-wait, just check
97
        timeout.tv_usec=0;
98
        FD_ZERO(&rd_set);
99
        FD_SET(tap_fd, &rd_set);
100
 
101
        // HDL-time-consumption!!!
102
        host_delay(10);
103
 
104
        // sw-wait
105
        ret = select(tap_fd + 1, &rd_set, NULL, NULL, &timeout); // man7.org: if timeout is NULL, select() can block indefinitely...
106
        if ( ret < 0 ) {
107
            int errsv = errno;
108
            if (errsv == EINTR) { // EINTR -> not an ERR in curr situation
109
                //printf("EINTR\n");
110
                continue;
111
            }
112
            if (errsv == EAGAIN) { // EAGAIN -> nothing to read
113
                // printf("EAGAIN\n");
114
                continue;
115
            } else { // all others -> ERR-case
116
                printf("ERR4: fd=%x, errsv=%x :  %s\n", tap_fd, errsv, strerror(errsv));
117
                close(tap_fd);
118
                return -5;
119
            }
120
        }
121
 
122
        // check TAP-RX
123
        if(FD_ISSET(tap_fd, &rd_set)) {
124
            int nread = cread(tap_fd, &buffer[0], BUFSIZE);
125
            //printf("TAP-RD: nread=%03x\n", nread);
126
            if (nread) {
127
                ether_tx(&buffer[0], nread);
128
            } else {
129
                printf("FD_ISSET: nread=%x\n", nread);
130
            }
131
            //if (idx == 32) break;
132
        }
133
 
134
        // check TAP-TX
135
        if (ether_rx_ok()) {
136
            int nwrite_0;
137
            ether_rx(&buffer[0], &nwrite_0);
138
            int nwrite_1 = cwrite(tap_fd, &buffer[0], nwrite_0);
139
        }
140
 
141
    }
142
    // end / ;)
143
    printf("close TAP\n");
144
    close(tap_fd);
145
 
146
    // Final
147
    return 0;
148
}

powered by: WebSVN 2.1.0

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