net_switch.cc 5.39 KB
Newer Older
Jialin Li's avatar
Jialin Li committed
1
2
3
4
5
6
7
8
#include <cstdio>
#include <cstdlib>
#include <csignal>
#include <climits>
#include <cstring>
#include <unistd.h>
#include <vector>
#include <unordered_map>
9
#include <cassert>
Jialin Li's avatar
Jialin Li committed
10
11
12
13
14

extern "C" {
#include <netsim.h>
};

15
16
static uint64_t sync_period = (500 * 1000ULL); // 500ns
static uint64_t eth_latency = (500 * 1000ULL); // 500ns
Jialin Li's avatar
Jialin Li committed
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

/* MAC address type */
struct MAC {
    const volatile uint8_t *data;

    MAC(const volatile uint8_t *data)
        : data(data) {}

    bool operator==(const MAC &other) const {
        for (int i = 0; i < 6; i++) {
            if (data[i] != other.data[i]) {
                return false;
            }
        }
        return true;
    }
};
namespace std {
    template <>
    struct hash<MAC>
    {
        size_t operator()(const MAC &m) const {
            size_t res = 0;
            for (int i = 0; i < 6; i++) {
                res = (res << 4) | (res ^ m.data[i]);
            }
            return res;
        }
    };
} // namespace std

/* Global variables */
static uint64_t cur_ts = 0;
static int exiting = 0;
static const volatile uint8_t bcast[6] = {0xFF};
static const MAC bcast_addr(bcast);
static std::vector<struct netsim_interface> nsifs;
static std::unordered_map<MAC, int> mac_table;

static void sigint_handler(int dummy)
{
    exiting = 1;
}

static void forward_pkt(volatile struct cosim_eth_proto_d2n_send *tx, int port)
{
    volatile union cosim_eth_proto_n2d *msg_to;
64
    msg_to = netsim_n2d_alloc(&nsifs[port], cur_ts, eth_latency);
Jialin Li's avatar
Jialin Li committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
    if (msg_to != NULL) {
        volatile struct cosim_eth_proto_n2d_recv *rx;
        rx = &msg_to->recv;
        rx->len = tx->len;
        rx->port = 0;
        memcpy((void *)rx->data, (void *)tx->data, tx->len);

        // WMB();
        rx->own_type = COSIM_ETH_PROTO_N2D_MSG_RECV |
            COSIM_ETH_PROTO_N2D_OWN_DEV;
    } else {
        fprintf(stderr, "forward_pkt: dropping packet\n");
    }
}

static void switch_pkt(struct netsim_interface *nsif, int iport)
{
    volatile union cosim_eth_proto_d2n *msg_from = netsim_d2n_poll(nsif, cur_ts);
    if (msg_from == NULL) {
        return;
    }

    uint8_t type = msg_from->dummy.own_type & COSIM_ETH_PROTO_D2N_MSG_MASK;
    if (type == COSIM_ETH_PROTO_D2N_MSG_SEND) {
        volatile struct cosim_eth_proto_d2n_send *tx;
        tx = &msg_from->send;
        // Get MAC addresses
        MAC dst(tx->data), src(tx->data+6);
        // MAC learning
        if (!(src == bcast_addr)) {
            mac_table[src] = iport;
        }
        // L2 forwarding
        if (mac_table.count(dst) > 0) {
            int eport = mac_table.at(dst);
            forward_pkt(tx, eport);
        } else {
            // Broadcast
            for (int eport = 0; eport < nsifs.size(); eport++) {
                if (eport != iport) {
                    // Do not forward to ingress port
                    forward_pkt(tx, eport);
                }
            }
        }
    } else if (type == COSIM_ETH_PROTO_D2N_MSG_SYNC) {
    } else {
        fprintf(stderr, "switch_pkt: unsupported type=%u\n", type);
        abort();
    }
    netsim_d2n_done(nsif, msg_from);
}

int main(int argc, char *argv[])
{
    int c;
121
    int bad_option = 0;
122
    int sync_mode = SYNC_MODES;
Jialin Li's avatar
Jialin Li committed
123
124

    // Parse command line argument
125
    while ((c = getopt(argc, argv, "s:S:E:m:")) != -1 && !bad_option) {
Jialin Li's avatar
Jialin Li committed
126
127
128
129
130
        switch (c) {
        case 's': {
            struct netsim_interface nsif;
            int sync = 1;
            if (netsim_init(&nsif, optarg, &sync) != 0) {
131
                fprintf(stderr, "connecting to %s failed\n", optarg);
Jialin Li's avatar
Jialin Li committed
132
133
134
                return EXIT_FAILURE;
            }
            nsifs.push_back(nsif);
135
            break;
Jialin Li's avatar
Jialin Li committed
136
        }
137
138
139
140
141
142
143
144
145

        case 'S':
            sync_period = strtoull(optarg, NULL, 0) * 1000ULL;
            break;

        case 'E':
            eth_latency = strtoull(optarg, NULL, 0) * 1000ULL;
            break;

146
147
148
149
150
        case 'm':
            sync_mode = strtol(optarg, NULL, 0);
            assert(sync_mode == SYNC_MODES || sync_mode == SYNC_BARRIER);
            break;

Jialin Li's avatar
Jialin Li committed
151
152
        default:
            fprintf(stderr, "unknown option %c\n", c);
153
154
            bad_option = 1;
            break;
Jialin Li's avatar
Jialin Li committed
155
156
157
        }
    }

158
159
160
    if (nsifs.empty() || bad_option) {
        fprintf(stderr, "Usage: net_switch [-S SYNC-PERIOD] [-E ETH-LATENCY] "
                "-s SOCKET-A [-s SOCKET-B ...]\n");
Jialin Li's avatar
Jialin Li committed
161
162
163
164
165
166
167
168
169
170
        return EXIT_FAILURE;
    }

    signal(SIGINT, sigint_handler);
    signal(SIGTERM, sigint_handler);

    printf("start polling\n");
    while (!exiting) {
        // Sync all interfaces
        for (auto &nsif : nsifs) {
171
172
            if (netsim_n2d_sync(&nsif, cur_ts, eth_latency,
                        sync_period, sync_mode) != 0) {
Jialin Li's avatar
Jialin Li committed
173
174
175
176
                fprintf(stderr, "netsim_n2d_sync failed\n");
                abort();
            }
        }
177
178
        netsim_advance_epoch(cur_ts, sync_period, sync_mode);

Jialin Li's avatar
Jialin Li committed
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
        // Switch packets
        uint64_t min_ts;
        do {
            min_ts = ULLONG_MAX;
            for (int port = 0; port < nsifs.size(); port++) {
                auto &nsif = nsifs.at(port);
                switch_pkt(&nsif, port);
                if (nsif.sync) {
                    uint64_t ts = netsim_d2n_timestamp(&nsif);
                    min_ts = ts < min_ts ? ts : min_ts;
                }
            }
        } while (!exiting && (min_ts <= cur_ts));

        // Update cur_ts
        if (min_ts < ULLONG_MAX) {
195
            cur_ts = netsim_advance_time(min_ts, sync_period, sync_mode);
Jialin Li's avatar
Jialin Li committed
196
197
198
199
200
        }
    }

    return 0;
}