net_switch.cc 13.2 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
#include <arpa/inet.h>
Antoine Kaufmann's avatar
Antoine Kaufmann committed
26
27
28
29
#include <linux/if_ether.h>
#include <linux/ip.h>
#include <pcap/pcap.h>
#include <unistd.h>
30
31
32
33

#include <cassert>
#include <climits>
#include <csignal>
Jialin Li's avatar
Jialin Li committed
34
35
36
#include <cstdio>
#include <cstdlib>
#include <cstring>
37
#include <string>
Jialin Li's avatar
Jialin Li committed
38
#include <unordered_map>
39
#include <vector>
Jialin Li's avatar
Jialin Li committed
40

41
#include <simbricks/base/cxxatomicfix.h>
Jialin Li's avatar
Jialin Li committed
42
extern "C" {
43
#include <simbricks/network/if.h>
44
#include <simbricks/nicif/nicif.h>
Jialin Li's avatar
Jialin Li committed
45
46
};

Antoine Kaufmann's avatar
Antoine Kaufmann committed
47
// #define NETSWITCH_DEBUG
Hejing Li's avatar
Hejing Li committed
48
#define NETSWITCH_STAT
49

50
struct SimbricksBaseIfParams netParams;
Jialin Li's avatar
Jialin Li committed
51
static pcap_dumper_t *dumpfile = nullptr;
Jialin Li's avatar
Jialin Li committed
52

Hejing Li's avatar
Hejing Li committed
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#ifdef NETSWITCH_STAT
#endif

#ifdef NETSWITCH_STAT
static uint64_t d2n_poll_total = 0;
static uint64_t d2n_poll_suc = 0;
static uint64_t d2n_poll_sync = 0;

static uint64_t s_d2n_poll_total = 0;
static uint64_t s_d2n_poll_suc = 0;
static uint64_t s_d2n_poll_sync = 0;

static int stat_flag = 0;
#endif

Jialin Li's avatar
Jialin Li committed
68
69
/* MAC address type */
struct MAC {
70
  const uint8_t *data;
Jialin Li's avatar
Jialin Li committed
71

72
  explicit MAC(const uint8_t *data) : data(data) {
73
  }
Jialin Li's avatar
Jialin Li committed
74

75
76
77
78
79
  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
80
    }
81
82
    return true;
  }
Jialin Li's avatar
Jialin Li committed
83
84
};
namespace std {
Antoine Kaufmann's avatar
Antoine Kaufmann committed
85
template <>
86
87
struct hash<MAC> {
  size_t operator()(const MAC &m) const {
Antoine Kaufmann's avatar
Antoine Kaufmann committed
88
89
    size_t res = 0;
    for (int i = 0; i < 6; i++) {
90
      res = (res << 4) | (res ^ m.data[i]);
Antoine Kaufmann's avatar
Antoine Kaufmann committed
91
92
    }
    return res;
93
  }
Antoine Kaufmann's avatar
Antoine Kaufmann committed
94
95
};
}  // namespace std
Jialin Li's avatar
Jialin Li committed
96

97
98
/** Normal network switch port (conneting to a NIC) */
class NetPort {
99
100
101
102
103
104
 public:
  enum RxPollState {
    kRxPollSuccess = 0,
    kRxPollFail = 1,
    kRxPollSync = 2,
  };
105
  struct SimbricksNetIf netif_;
106

107
 protected:
108
  volatile union SimbricksProtoNetMsg *rx_;
109
  int sync_;
110
111
112
113
  const char *path_;

  bool Init() {
    struct SimbricksBaseIfParams params = netParams;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
114
115
    params.sync_mode =
        (sync_ ? kSimbricksBaseIfSyncOptional : kSimbricksBaseIfSyncDisabled);
116
117
118
119
120
121
122
123
124
125
    params.sock_path = path_;
    params.blocking_conn = false;

    if (SimbricksBaseIfInit(&netif_.base, &params)) {
      perror("Init: SimbricksBaseIfInit failed");
      return false;
    }

    return true;
  }
126
127

 public:
128
129
  NetPort(const char *path, int sync) : rx_(nullptr), sync_(sync), path_(path) {
    memset(&netif_, 0, sizeof(netif_));
130
131
  }

Antoine Kaufmann's avatar
Antoine Kaufmann committed
132
133
134
135
136
137
  NetPort(const NetPort &other)
      : netif_(other.netif_),
        rx_(other.rx_),
        sync_(other.sync_),
        path_(other.path_) {
  }
138
139
140
141
142
143
144
145
146
147
148
149

  virtual bool Prepare() {
    if (!Init())
      return false;

    if (SimbricksBaseIfConnect(&netif_.base)) {
      perror("Prepare: SimbricksBaseIfConnect failed");
      return false;
    }

    return true;
  }
150

151
152
  virtual void Prepared() {
    sync_ = SimbricksBaseIfSyncEnabled(&netif_.base);
153
154
  }

155
  bool IsSync() {
156
157
158
    return sync_;
  }

159
  void Sync(uint64_t cur_ts) {
Antoine Kaufmann's avatar
Antoine Kaufmann committed
160
161
    while (SimbricksNetIfOutSync(&netif_, cur_ts)) {
    }
162
163
  }

164
165
  uint64_t NextTimestamp() {
    return SimbricksNetIfInTimestamp(&netif_);
166
167
  }

Antoine Kaufmann's avatar
Antoine Kaufmann committed
168
  enum RxPollState RxPacket(const void *&data, size_t &len, uint64_t cur_ts) {
169
170
    assert(rx_ == nullptr);

171
    rx_ = SimbricksNetIfInPoll(&netif_, cur_ts);
172
173
174
    if (!rx_)
      return kRxPollFail;

175
    uint8_t type = SimbricksNetIfInType(&netif_, rx_);
176
177
178
    if (type == SIMBRICKS_PROTO_NET_MSG_PACKET) {
      data = (const void *)rx_->packet.data;
      len = rx_->packet.len;
179
      return kRxPollSuccess;
180
    } else if (type == SIMBRICKS_PROTO_MSG_TYPE_SYNC) {
181
182
183
184
185
186
187
      return kRxPollSync;
    } else {
      fprintf(stderr, "switch_pkt: unsupported type=%u\n", type);
      abort();
    }
  }

188
  void RxDone() {
189
190
    assert(rx_ != nullptr);

191
    SimbricksNetIfInDone(&netif_, rx_);
192
193
194
    rx_ = nullptr;
  }

Antoine Kaufmann's avatar
Antoine Kaufmann committed
195
  bool TxPacket(const void *data, size_t len, uint64_t cur_ts) {
196
    volatile union SimbricksProtoNetMsg *msg_to =
Antoine Kaufmann's avatar
Antoine Kaufmann committed
197
        SimbricksNetIfOutAlloc(&netif_, cur_ts);
198
    if (!msg_to && !sync_) {
199
      return false;
200
201
    } else if (!msg_to && sync_) {
      while (!msg_to)
202
        msg_to = SimbricksNetIfOutAlloc(&netif_, cur_ts);
203
    }
204
205
    volatile struct SimbricksProtoNetMsgPacket *rx;
    rx = &msg_to->packet;
206
207
208
209
    rx->len = len;
    rx->port = 0;
    memcpy((void *)rx->data, data, len);

210
    SimbricksNetIfOutSend(&netif_, msg_to, SIMBRICKS_PROTO_NET_MSG_PACKET);
211
212
213
214
    return true;
  }
};

215
216
/** Listening switch port (connected to by another network) */
class NetListenPort : public NetPort {
217
 protected:
218
  struct SimbricksBaseIfSHMPool pool_;
219
220

 public:
221
222
  NetListenPort(const char *path, int sync) : NetPort(path, sync) {
    memset(&pool_, 0, sizeof(pool_));
223
224
  }

Antoine Kaufmann's avatar
Antoine Kaufmann committed
225
226
  NetListenPort(const NetListenPort &other)
      : NetPort(other), pool_(other.pool_) {
227
  }
228

Antoine Kaufmann's avatar
Antoine Kaufmann committed
229
  bool Prepare() override {
230
231
232
233
    if (!Init())
      return false;

    std::string shm_path = path_;
234
235
    shm_path += "-shm";

Antoine Kaufmann's avatar
Antoine Kaufmann committed
236
237
238
    if (SimbricksBaseIfSHMPoolCreate(
            &pool_, shm_path.c_str(),
            SimbricksBaseIfSHMSize(&netif_.base.params)) != 0) {
239
240
241
242
243
244
245
246
247
248
      perror("Prepare: SimbricksBaseIfSHMPoolCreate failed");
      return false;
    }

    if (SimbricksBaseIfListen(&netif_.base, &pool_) != 0) {
      perror("Prepare: SimbricksBaseIfListen failed");
      return false;
    }

    return true;
249
250
251
  }
};

Antoine Kaufmann's avatar
Antoine Kaufmann committed
252
static bool ConnectAll(std::vector<NetPort *> ports) {
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
  size_t n = ports.size();
  struct SimBricksBaseIfEstablishData ests[n];
  struct SimbricksProtoNetIntro intro;

  printf("start connecting...\n");
  for (size_t i = 0; i < n; i++) {
    NetPort *p = ports[i];
    ests[i].base_if = &p->netif_.base;
    ests[i].tx_intro = &intro;
    ests[i].tx_intro_len = sizeof(intro);
    ests[i].rx_intro = &intro;
    ests[i].rx_intro_len = sizeof(intro);

    if (!p->Prepare())
      return false;
  }

Antoine Kaufmann's avatar
Antoine Kaufmann committed
270
271
272
273
  if (SimBricksBaseIfEstablish(ests, n)) {
    fprintf(stderr, "ConnectAll: SimBricksBaseIfEstablish failed\n");
    return false;
  }
274
275
276
277

  printf("done connecting\n");
  return true;
}
278

Jialin Li's avatar
Jialin Li committed
279
280
281
/* Global variables */
static uint64_t cur_ts = 0;
static int exiting = 0;
282
static const uint8_t bcast[6] = {0xFF};
Jialin Li's avatar
Jialin Li committed
283
static const MAC bcast_addr(bcast);
284
static std::vector<NetPort *> ports;
Jialin Li's avatar
Jialin Li committed
285
286
static std::unordered_map<MAC, int> mac_table;

287
288
static void sigint_handler(int dummy) {
  exiting = 1;
Jialin Li's avatar
Jialin Li committed
289
290
}

Hejing Li's avatar
Hejing Li committed
291
292
293
294
295
296
297
298
299
300
static void sigusr1_handler(int dummy) {
  fprintf(stderr, "main_time = %lu\n", cur_ts);
}

#ifdef NETSWITCH_STAT
static void sigusr2_handler(int dummy) {
  stat_flag = 1;
}
#endif

301
302
static void forward_pkt(const void *pkt_data, size_t pkt_len, size_t port_id,
                        size_t iport_id) {
Jialin Li's avatar
Jialin Li committed
303
  struct pcap_pkthdr ph;
304
  NetPort &dest_port = *ports[port_id];
305

Jialin Li's avatar
Jialin Li committed
306
307
  // log to pcap file if initialized
  if (dumpfile) {
Antoine Kaufmann's avatar
Antoine Kaufmann committed
308
309
310
311
312
313
    memset(&ph, 0, sizeof(ph));
    ph.ts.tv_sec = cur_ts / 1000000000000ULL;
    ph.ts.tv_usec = (cur_ts % 1000000000000ULL) / 1000ULL;
    ph.caplen = pkt_len;
    ph.len = pkt_len;
    pcap_dump((unsigned char *)dumpfile, &ph, (unsigned char *)pkt_data);
Jialin Li's avatar
Jialin Li committed
314
  }
315
  // print sending tick: [packet type] source_IP -> dest_IP len:
316

317
318
319
320
#ifdef NETSWITCH_DEBUG
  uint16_t eth_proto;
  struct ethhdr *hdr;
  struct iphdr *iph;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
321
  hdr = (struct ethhdr *)pkt_data;
322
323
  eth_proto = ntohs(hdr->h_proto);
  iph = (struct iphdr *)(hdr + 1);
Antoine Kaufmann's avatar
Antoine Kaufmann committed
324
325
  uint64_t dmac = (*(uint64_t *)hdr->h_dest) & 0xFFFFFFFFFFULL;
  uint64_t smac = (*(uint64_t *)hdr->h_source) & 0xFFFFFFFFFFULL;
326
327
  fprintf(stderr, "%20lu: [P %zu -> %zu] %lx -> %lx ", cur_ts, iport_id,
          port_id, smac, dmac);
Antoine Kaufmann's avatar
Antoine Kaufmann committed
328
  if (eth_proto == ETH_P_IP) {
329
    fprintf(stderr, "[ IP] ");
330
331
    fprintf(stderr, "%8X -> %8X len: %lu\n", iph->saddr, iph->daddr,
            ntohs(iph->tot_len) + sizeof(struct ethhdr));
Antoine Kaufmann's avatar
Antoine Kaufmann committed
332
  } else if (eth_proto == ETH_P_ARP) {
333
    fprintf(stderr, "[ARP] %8X -> %8X\n",
Antoine Kaufmann's avatar
Antoine Kaufmann committed
334
335
336
            *(uint32_t *)((uint8_t *)pkt_data + 28),
            *(uint32_t *)((uint8_t *)pkt_data + 38));
  } else {
337
    fprintf(stderr, "unknown eth type\n");
338
339
340
  }
#endif

341
342
  if (!dest_port.TxPacket(pkt_data, pkt_len, cur_ts))
    fprintf(stderr, "forward_pkt: dropping packet on port %zu\n", port_id);
Jialin Li's avatar
Jialin Li committed
343
344
}

345
static void switch_pkt(NetPort &port, size_t iport) {
346
347
348
  const void *pkt_data;
  size_t pkt_len;

Hejing Li's avatar
Hejing Li committed
349
350
#ifdef NETSWITCH_STAT
  d2n_poll_total += 1;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
351
  if (stat_flag) {
Hejing Li's avatar
Hejing Li committed
352
353
354
355
    s_d2n_poll_total += 1;
  }
#endif

356
357
  enum NetPort::RxPollState poll = port.RxPacket(pkt_data, pkt_len, cur_ts);
  if (poll == NetPort::kRxPollFail) {
358
359
360
    return;
  }

Hejing Li's avatar
Hejing Li committed
361
362
#ifdef NETSWITCH_STAT
  d2n_poll_suc += 1;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
363
  if (stat_flag) {
Hejing Li's avatar
Hejing Li committed
364
365
366
367
    s_d2n_poll_suc += 1;
  }
#endif

368
  if (poll == NetPort::kRxPollSuccess) {
369
    // Get MAC addresses
370
    MAC dst((const uint8_t *)pkt_data), src((const uint8_t *)pkt_data + 6);
371
372
373
    // MAC learning
    if (!(src == bcast_addr)) {
      mac_table[src] = iport;
Jialin Li's avatar
Jialin Li committed
374
    }
375
    // L2 forwarding
376
377
378
    auto i = mac_table.find(dst);
    if (i != mac_table.end()) {
      size_t eport = i->second;
379
      if (eport != iport)
380
        forward_pkt(pkt_data, pkt_len, eport, iport);
Jialin Li's avatar
Jialin Li committed
381
    } else {
382
      // Broadcast
383
      for (size_t eport = 0; eport < ports.size(); eport++) {
384
385
        if (eport != iport) {
          // Do not forward to ingress port
386
          forward_pkt(pkt_data, pkt_len, eport, iport);
387
388
        }
      }
Jialin Li's avatar
Jialin Li committed
389
    }
390
  } else if (poll == NetPort::kRxPollSync) {
Hejing Li's avatar
Hejing Li committed
391
392
#ifdef NETSWITCH_STAT
    d2n_poll_sync += 1;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
393
    if (stat_flag) {
Hejing Li's avatar
Hejing Li committed
394
395
396
      s_d2n_poll_sync += 1;
    }
#endif
397
  } else {
398
    fprintf(stderr, "switch_pkt: unsupported poll result=%u\n", poll);
399
400
    abort();
  }
401
  port.RxDone();
Jialin Li's avatar
Jialin Li committed
402
403
}

404
405
406
int main(int argc, char *argv[]) {
  int c;
  int bad_option = 0;
407
  int sync_eth = 1;
Jialin Li's avatar
Jialin Li committed
408
  pcap_t *pc = nullptr;
409

410
411
  SimbricksNetIfDefaultParams(&netParams);

412
  // Parse command line argument
413
  while ((c = getopt(argc, argv, "s:h:uS:E:p:")) != -1 && !bad_option) {
414
415
    switch (c) {
      case 's': {
416
        NetPort *port = new NetPort(optarg, sync_eth);
417
        fprintf(stderr, "Switch connecting to: %s\n", optarg);
418
        ports.push_back(port);
419
420
421
        break;
      }

422
      case 'h': {
423
        NetListenPort *port = new NetListenPort(optarg, sync_eth);
424
425
426
427
428
        fprintf(stderr, "Switch listening on: %s\n", optarg);
        ports.push_back(port);
        break;
      }

429
430
431
432
      case 'u':
        sync_eth = 0;
        break;

433
      case 'S':
434
        netParams.sync_interval = strtoull(optarg, NULL, 0) * 1000ULL;
435
436
437
        break;

      case 'E':
438
        netParams.link_latency = strtoull(optarg, NULL, 0) * 1000ULL;
439
440
        break;

Jialin Li's avatar
Jialin Li committed
441
442
443
444
      case 'p':
        pc = pcap_open_dead_with_tstamp_precision(DLT_EN10MB, 65535,
                                                  PCAP_TSTAMP_PRECISION_NANO);
        if (pc == nullptr) {
Antoine Kaufmann's avatar
Antoine Kaufmann committed
445
446
          perror("pcap_open_dead failed");
          return EXIT_FAILURE;
Jialin Li's avatar
Jialin Li committed
447
448
449
450
451
        }

        dumpfile = pcap_dump_open(pc, optarg);
        break;

452
453
454
455
      default:
        fprintf(stderr, "unknown option %c\n", c);
        bad_option = 1;
        break;
Jialin Li's avatar
Jialin Li committed
456
    }
457
458
  }

459
  if (ports.empty() || bad_option) {
460
461
462
463
464
465
466
467
    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);
Hejing Li's avatar
Hejing Li committed
468
469
470
471
472
473
  signal(SIGUSR1, sigusr1_handler);

#ifdef NETSWITCH_STAT
  signal(SIGUSR2, sigusr2_handler);
#endif

474
475
  if (!ConnectAll(ports))
    return EXIT_FAILURE;
476
477
478
479

  printf("start polling\n");
  while (!exiting) {
    // Sync all interfaces
480
481
    for (auto port : ports)
      port->Sync(cur_ts);
482
483
484
485
486

    // Switch packets
    uint64_t min_ts;
    do {
      min_ts = ULLONG_MAX;
487
      for (size_t port_i = 0; port_i < ports.size(); port_i++) {
488
        auto &port = *ports[port_i];
489
490
491
        switch_pkt(port, port_i);
        if (port.IsSync()) {
          uint64_t ts = port.NextTimestamp();
492
          min_ts = ts < min_ts ? ts : min_ts;
Jialin Li's avatar
Jialin Li committed
493
        }
494
495
496
497
498
      }
    } while (!exiting && (min_ts <= cur_ts));

    // Update cur_ts
    if (min_ts < ULLONG_MAX) {
499
      cur_ts = min_ts;
Jialin Li's avatar
Jialin Li committed
500
    }
501
  }
Jialin Li's avatar
Jialin Li committed
502

Hejing Li's avatar
Hejing Li committed
503
504
505
506
507
508
509
510
#ifdef NETSWITCH_STAT
  fprintf(stderr, "%20s: %22lu %20s: %22lu  poll_suc_rate: %f\n",
          "d2n_poll_total", d2n_poll_total, "d2n_poll_suc", d2n_poll_suc,
          (double)d2n_poll_suc / d2n_poll_total);
  fprintf(stderr, "%65s: %22lu  sync_rate: %f\n", "d2n_poll_sync",
          d2n_poll_sync, (double)d2n_poll_sync / d2n_poll_suc);

  fprintf(stderr, "%20s: %22lu %20s: %22lu  poll_suc_rate: %f\n",
Antoine Kaufmann's avatar
Antoine Kaufmann committed
511
512
          "s_d2n_poll_total", s_d2n_poll_total, "s_d2n_poll_suc",
          s_d2n_poll_suc, (double)s_d2n_poll_suc / s_d2n_poll_total);
Hejing Li's avatar
Hejing Li committed
513
514
515
516
  fprintf(stderr, "%65s: %22lu  sync_rate: %f\n", "s_d2n_poll_sync",
          s_d2n_poll_sync, (double)s_d2n_poll_sync / s_d2n_poll_suc);
#endif

517
  return 0;
Jialin Li's avatar
Jialin Li committed
518
}