net_tap.c 3.98 KB
Newer Older
1
/*
Antoine Kaufmann's avatar
Antoine Kaufmann committed
2
3
 * Copyright 2021 Max Planck Institute for Software Systems, and
 * National University of Singapore
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include <fcntl.h>
26
#include <pthread.h>
27
28
29
30
31
32
33
34
35
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <unistd.h>
#include <linux/if.h>
#include <linux/if_tun.h>

36
#include <simbricks/netif/netsim.h>
37

38
39
//#define DEBUG_PKTMETA

40
static struct netsim_interface nsif;
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
static int tap_fd;

static int tap_open(const char *name)
{
    struct ifreq ifr;
    int fd;

    if ((fd = open("/dev/net/tun", O_RDWR)) < 0) {
        perror("tap_open: open failed");
        return -1;
    }

    memset(&ifr, 0, sizeof(ifr));
    ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
    strncpy(ifr.ifr_name, name, IFNAMSIZ);

    if (ioctl(fd, TUNSETIFF, &ifr) != 0) {
        perror("tap_open: ioctl failed");
        close(fd);
        return -1;
    }

    return fd;
}

static void d2n_send(volatile struct cosim_eth_proto_d2n_send *s)
{
68
#ifdef DEBUG_PKTMETA
69
    printf("sent packet: len=%u\n", s->len);
70
#endif
71
72
73
74
75
76
77
78

    if (write(tap_fd, (void *) s->data, s->len) != (ssize_t) s->len) {
        perror("d2n_send: send failed");
    }
}

static void poll_d2n(void)
{
79
    volatile union cosim_eth_proto_d2n *msg = netsim_d2n_poll(&nsif, 0);
80
81
82
    uint8_t type;

    /* message not ready */
83
    if (msg == NULL)
84
85
86
87
88
89
90
91
92
93
94
95
        return;

    type = msg->dummy.own_type & COSIM_ETH_PROTO_D2N_MSG_MASK;
    switch (type) {
        case COSIM_ETH_PROTO_D2N_MSG_SEND:
            d2n_send(&msg->send);
            break;

        default:
            fprintf(stderr, "poll_d2n: unsupported type=%u\n", type);
    }

96
    netsim_d2n_done(&nsif, msg);
97
98
}

99
100
101
102
103
104
105
static void *rx_handler(void *arg)
{
    volatile union cosim_eth_proto_n2d *msg;
    volatile struct cosim_eth_proto_n2d_recv *rx;
    ssize_t len;

    while (1) {
106
        msg = netsim_n2d_alloc(&nsif, 0, 0);
107
        if (msg == NULL) {
108
109
110
111
112
            fprintf(stderr, "coudl not allocate message for rx\n");
            abort();
        }
        rx = &msg->recv;

113
        len = read(tap_fd, (void *) rx->data, nsif.n2d_elen - sizeof(*msg));
114
115
116
117
118
        if (len <= 0) {
            perror("rx handler: read failed");
        }
        rx->len = len;
        rx->port = 0;
119
#ifdef DEBUG_PKTMETA
120
        printf("received packet: len=%u\n", rx->len);
121
#endif
122
123
124
125
126
127
128

        // WMB();
        rx->own_type = COSIM_ETH_PROTO_N2D_MSG_RECV |
            COSIM_ETH_PROTO_N2D_OWN_DEV;
    }
}

129
130
int main(int argc, char *argv[])
{
131
    int sync;
132

133
134
    if (argc != 3) {
        fprintf(stderr, "Usage: net_tap TAP_DEVICE_NAME SOCKET\n");
135
136
137
138
139
140
141
        return EXIT_FAILURE;
    }

    if ((tap_fd = tap_open(argv[1])) < 0) {
        return -1;
    }

142
    sync = 0;
143
    if (netsim_init(&nsif, argv[2], &sync) != 0) {
144
        close(tap_fd);
145
146
147
        return -1;
    }

148
149
150
151
152
    pthread_t worker;
    if (pthread_create(&worker, NULL, rx_handler, NULL) != 0) {
        return EXIT_FAILURE;
    }

153
154
155
156
157
158
    printf("start polling\n");
    while (1) {
        poll_d2n();
    }
    return 0;
}