net_switch.cc 6.04 KB
Newer Older
Antoine Kaufmann's avatar
Antoine Kaufmann committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*
 * Copyright 2021 Max Planck Institute for Software Systems, and
 * National University of Singapore
 *
 * 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.
 */

25
26
27
28
29
#include <unistd.h>

#include <cassert>
#include <climits>
#include <csignal>
Jialin Li's avatar
Jialin Li committed
30
31
32
33
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <unordered_map>
34
#include <vector>
Jialin Li's avatar
Jialin Li committed
35
36

extern "C" {
37
#include <simbricks/netif/netsim.h>
Jialin Li's avatar
Jialin Li committed
38
39
};

Antoine Kaufmann's avatar
Antoine Kaufmann committed
40
41
static uint64_t sync_period = (500 * 1000ULL);  // 500ns
static uint64_t eth_latency = (500 * 1000ULL);  // 500ns
Jialin Li's avatar
Jialin Li committed
42
43
44

/* MAC address type */
struct MAC {
45
  const volatile uint8_t *data;
Jialin Li's avatar
Jialin Li committed
46

47
48
  MAC(const volatile uint8_t *data) : data(data) {
  }
Jialin Li's avatar
Jialin Li committed
49

50
51
52
53
54
  bool operator==(const MAC &other) const {
    for (int i = 0; i < 6; i++) {
      if (data[i] != other.data[i]) {
        return false;
      }
Jialin Li's avatar
Jialin Li committed
55
    }
56
57
    return true;
  }
Jialin Li's avatar
Jialin Li committed
58
59
};
namespace std {
Antoine Kaufmann's avatar
Antoine Kaufmann committed
60
template <>
61
62
struct hash<MAC> {
  size_t operator()(const MAC &m) const {
Antoine Kaufmann's avatar
Antoine Kaufmann committed
63
64
    size_t res = 0;
    for (int i = 0; i < 6; i++) {
65
      res = (res << 4) | (res ^ m.data[i]);
Antoine Kaufmann's avatar
Antoine Kaufmann committed
66
67
    }
    return res;
68
  }
Antoine Kaufmann's avatar
Antoine Kaufmann committed
69
70
};
}  // namespace std
Jialin Li's avatar
Jialin Li committed
71
72
73
74
75
76
77
78
79

/* 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;

80
81
static void sigint_handler(int dummy) {
  exiting = 1;
Jialin Li's avatar
Jialin Li committed
82
83
}

84
static void forward_pkt(volatile struct SimbricksProtoNetD2NSend *tx,
85
                        int port) {
86
  volatile union SimbricksProtoNetN2D *msg_to;
87
88
  msg_to = netsim_n2d_alloc(&nsifs[port], cur_ts, eth_latency);
  if (msg_to != NULL) {
89
    volatile struct SimbricksProtoNetN2DRecv *rx;
90
91
92
93
94
95
    rx = &msg_to->recv;
    rx->len = tx->len;
    rx->port = 0;
    memcpy((void *)rx->data, (void *)tx->data, tx->len);

    // WMB();
96
97
    rx->own_type = SIMBRICKS_PROTO_NET_N2D_MSG_RECV |
        SIMBRICKS_PROTO_NET_N2D_OWN_DEV;
98
99
100
  } else {
    fprintf(stderr, "forward_pkt: dropping packet\n");
  }
Jialin Li's avatar
Jialin Li committed
101
102
}

103
static void switch_pkt(struct netsim_interface *nsif, int iport) {
104
105
  volatile union SimbricksProtoNetD2N *msg_from =
      netsim_d2n_poll(nsif, cur_ts);
106
107
108
109
  if (msg_from == NULL) {
    return;
  }

110
111
112
  uint8_t type = msg_from->dummy.own_type & SIMBRICKS_PROTO_NET_D2N_MSG_MASK;
  if (type == SIMBRICKS_PROTO_NET_D2N_MSG_SEND) {
    volatile struct SimbricksProtoNetD2NSend *tx;
113
114
115
116
117
118
    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;
Jialin Li's avatar
Jialin Li committed
119
    }
120
121
122
123
    // L2 forwarding
    if (mac_table.count(dst) > 0) {
      int eport = mac_table.at(dst);
      forward_pkt(tx, eport);
Jialin Li's avatar
Jialin Li committed
124
    } else {
125
126
127
128
129
130
131
      // Broadcast
      for (int eport = 0; eport < nsifs.size(); eport++) {
        if (eport != iport) {
          // Do not forward to ingress port
          forward_pkt(tx, eport);
        }
      }
Jialin Li's avatar
Jialin Li committed
132
    }
133
  } else if (type == SIMBRICKS_PROTO_NET_D2N_MSG_SYNC) {
134
135
136
137
138
  } else {
    fprintf(stderr, "switch_pkt: unsupported type=%u\n", type);
    abort();
  }
  netsim_d2n_done(nsif, msg_from);
Jialin Li's avatar
Jialin Li committed
139
140
}

141
142
143
144
145
146
147
148
149
150
151
152
153
154
int main(int argc, char *argv[]) {
  int c;
  int bad_option = 0;
  int sync_mode = SYNC_MODES;

  // Parse command line argument
  while ((c = getopt(argc, argv, "s:S:E:m:")) != -1 && !bad_option) {
    switch (c) {
      case 's': {
        struct netsim_interface nsif;
        int sync = 1;
        if (netsim_init(&nsif, optarg, &sync) != 0) {
          fprintf(stderr, "connecting to %s failed\n", optarg);
          return EXIT_FAILURE;
Jialin Li's avatar
Jialin Li committed
155
        }
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
        nsifs.push_back(nsif);
        break;
      }

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

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

      case 'm':
        sync_mode = strtol(optarg, NULL, 0);
        assert(sync_mode == SYNC_MODES || sync_mode == SYNC_BARRIER);
        break;

      default:
        fprintf(stderr, "unknown option %c\n", c);
        bad_option = 1;
        break;
Jialin Li's avatar
Jialin Li committed
177
    }
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
  }

  if (nsifs.empty() || bad_option) {
    fprintf(stderr,
            "Usage: net_switch [-S SYNC-PERIOD] [-E ETH-LATENCY] "
            "-s SOCKET-A [-s SOCKET-B ...]\n");
    return EXIT_FAILURE;
  }

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

  printf("start polling\n");
  while (!exiting) {
    // Sync all interfaces
    for (auto &nsif : nsifs) {
      if (netsim_n2d_sync(&nsif, cur_ts, eth_latency, sync_period, sync_mode) !=
          0) {
        fprintf(stderr, "netsim_n2d_sync failed\n");
        abort();
      }
Jialin Li's avatar
Jialin Li committed
199
    }
200
201
202
203
204
205
206
207
208
209
210
211
    netsim_advance_epoch(cur_ts, sync_period, sync_mode);

    // 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;
Jialin Li's avatar
Jialin Li committed
212
        }
213
214
215
216
217
218
      }
    } while (!exiting && (min_ts <= cur_ts));

    // Update cur_ts
    if (min_ts < ULLONG_MAX) {
      cur_ts = netsim_advance_time(min_ts, sync_period, sync_mode);
Jialin Li's avatar
Jialin Li committed
219
    }
220
  }
Jialin Li's avatar
Jialin Li committed
221

222
  return 0;
Jialin Li's avatar
Jialin Li committed
223
}