ports.h 4.18 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
25
26
/*
 * Copyright 2022 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.
 */

#ifndef PORTS_H_
#define PORTS_H_
Antoine Kaufmann's avatar
Antoine Kaufmann committed
27
28
29

#include <stdint.h>

30
#include <simbricks/base/cxxatomicfix.h>
Antoine Kaufmann's avatar
Antoine Kaufmann committed
31
extern "C" {
32
#include <simbricks/network/if.h>
Antoine Kaufmann's avatar
Antoine Kaufmann committed
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
}

extern uint64_t sync_period;
extern uint64_t eth_latency;
extern int sync_mode;

/** Abstract base switch port */
class Port {
 public:
  enum RxPollState {
    kRxPollSuccess = 0,
    kRxPollFail = 1,
    kRxPollSync = 2,
  };

  virtual ~Port() = default;

  virtual bool Connect(const char *path, int sync) = 0;
  virtual bool IsSync() = 0;
  virtual void Sync(uint64_t cur_ts) = 0;
  virtual uint64_t NextTimestamp() = 0;
  virtual enum RxPollState RxPacket(
      const void *& data, size_t &len, uint64_t cur_ts) = 0;
  virtual void RxDone() = 0;
  virtual bool TxPacket(const void *data, size_t len, uint64_t cur_ts) = 0;
};


/** Normal network switch port (conneting to a NIC) */
class NetPort : public Port {
 protected:
64
65
66
67
  struct SimbricksBaseIfParams *params_;
  struct SimbricksNetIf netifObj_;
  struct SimbricksNetIf *netif_;
  volatile union SimbricksProtoNetMsg *rx_;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
68
69
70
  int sync_;

 public:
Antoine Kaufmann's avatar
Antoine Kaufmann committed
71
  explicit NetPort(struct SimbricksBaseIfParams *params) : params_(params),
72
73
      netif_(&netifObj_), rx_(nullptr), sync_(0) {
    memset(&netifObj_, 0, sizeof(netifObj_));
Antoine Kaufmann's avatar
Antoine Kaufmann committed
74
75
  }

76
77
  NetPort(const NetPort &other) : netifObj_(other.netifObj_),
      netif_(&netifObj_), rx_(other.rx_), sync_(other.sync_) {}
Antoine Kaufmann's avatar
Antoine Kaufmann committed
78

Antoine Kaufmann's avatar
Antoine Kaufmann committed
79
  bool Connect(const char *path, int sync) override {
Antoine Kaufmann's avatar
Antoine Kaufmann committed
80
    sync_ = sync;
81
    return SimbricksNetIfInit(netif_, params_, path, &sync_) == 0;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
82
83
  }

Antoine Kaufmann's avatar
Antoine Kaufmann committed
84
  bool IsSync() override {
Antoine Kaufmann's avatar
Antoine Kaufmann committed
85
86
87
    return sync_;
  }

Antoine Kaufmann's avatar
Antoine Kaufmann committed
88
89
  void Sync(uint64_t cur_ts) override {
    while (SimbricksNetIfOutSync(netif_, cur_ts)) {}
Antoine Kaufmann's avatar
Antoine Kaufmann committed
90
91
  }

Antoine Kaufmann's avatar
Antoine Kaufmann committed
92
  uint64_t NextTimestamp() override {
93
    return SimbricksNetIfInTimestamp(netif_);
Antoine Kaufmann's avatar
Antoine Kaufmann committed
94
95
  }

Antoine Kaufmann's avatar
Antoine Kaufmann committed
96
  enum RxPollState RxPacket(
Antoine Kaufmann's avatar
Antoine Kaufmann committed
97
98
99
      const void *& data, size_t &len, uint64_t cur_ts) override {
    assert(rx_ == nullptr);

100
    rx_ = SimbricksNetIfInPoll(netif_, cur_ts);
Antoine Kaufmann's avatar
Antoine Kaufmann committed
101
102
103
    if (!rx_)
      return kRxPollFail;

104
105
106
107
    uint8_t type = SimbricksNetIfInType(netif_, rx_);
    if (type == SIMBRICKS_PROTO_NET_MSG_PACKET) {
      data = (const void *)rx_->packet.data;
      len = rx_->packet.len;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
108
      return kRxPollSuccess;
109
    } else if (type == SIMBRICKS_PROTO_MSG_TYPE_SYNC) {
Antoine Kaufmann's avatar
Antoine Kaufmann committed
110
111
112
113
114
115
116
      return kRxPollSync;
    } else {
      fprintf(stderr, "switch_pkt: unsupported type=%u\n", type);
      abort();
    }
  }

Antoine Kaufmann's avatar
Antoine Kaufmann committed
117
  void RxDone() override {
Antoine Kaufmann's avatar
Antoine Kaufmann committed
118
119
    assert(rx_ != nullptr);

120
    SimbricksNetIfInDone(netif_, rx_);
Antoine Kaufmann's avatar
Antoine Kaufmann committed
121
122
123
    rx_ = nullptr;
  }

Antoine Kaufmann's avatar
Antoine Kaufmann committed
124
  bool TxPacket(
Antoine Kaufmann's avatar
Antoine Kaufmann committed
125
      const void *data, size_t len, uint64_t cur_ts) override {
126
127
    volatile union SimbricksProtoNetMsg *msg_to =
      SimbricksNetIfOutAlloc(netif_, cur_ts);
Antoine Kaufmann's avatar
Antoine Kaufmann committed
128
129
130
131
    if (!msg_to && !sync_) {
      return false;
    } else if (!msg_to && sync_) {
      while (!msg_to)
132
        msg_to = SimbricksNetIfOutAlloc(netif_, cur_ts);
Antoine Kaufmann's avatar
Antoine Kaufmann committed
133
    }
134
135
    volatile struct SimbricksProtoNetMsgPacket *rx;
    rx = &msg_to->packet;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
136
137
138
139
    rx->len = len;
    rx->port = 0;
    memcpy((void *)rx->data, data, len);

140
    SimbricksNetIfOutSend(netif_, msg_to, SIMBRICKS_PROTO_NET_MSG_PACKET);
Antoine Kaufmann's avatar
Antoine Kaufmann committed
141
142
143
144
    return true;
  }
};

Antoine Kaufmann's avatar
Antoine Kaufmann committed
145
#endif  // PORTS_H_