Commit 43825889 authored by Antoine Kaufmann's avatar Antoine Kaufmann
Browse files

sims/net/switch: add nethost for other networks to connect

parent 4d3d3c71
...@@ -39,6 +39,7 @@ ...@@ -39,6 +39,7 @@
extern "C" { extern "C" {
#include <simbricks/netif/netif.h> #include <simbricks/netif/netif.h>
#include <simbricks/nicif/nicif.h>
#include <simbricks/proto/base.h> #include <simbricks/proto/base.h>
}; };
...@@ -94,6 +95,7 @@ struct hash<MAC> { ...@@ -94,6 +95,7 @@ struct hash<MAC> {
}; };
} // namespace std } // namespace std
/** Abstract base switch port */ /** Abstract base switch port */
class Port { class Port {
public: public:
...@@ -116,6 +118,7 @@ class Port { ...@@ -116,6 +118,7 @@ class Port {
virtual bool TxPacket(const void *data, size_t len, uint64_t cur_ts) = 0; virtual bool TxPacket(const void *data, size_t len, uint64_t cur_ts) = 0;
}; };
/** Normal network switch port (conneting to a NIC) */ /** Normal network switch port (conneting to a NIC) */
class NetPort : public Port { class NetPort : public Port {
protected: protected:
...@@ -204,6 +207,114 @@ class NetPort : public Port { ...@@ -204,6 +207,114 @@ class NetPort : public Port {
} }
}; };
/** Hosting network switch port (connected to another network) */
class NetHostPort : public Port {
protected:
struct SimbricksNicIf nicif_;
volatile union SimbricksProtoNetN2D *rx_;
int sync_;
public:
NetHostPort() : rx_(nullptr), sync_(0) {
memset(&nicif_, 0, sizeof(nicif_));
}
NetHostPort(const NetHostPort &other) : nicif_(other.nicif_), rx_(other.rx_),
sync_(other.sync_) {}
virtual bool Connect(const char *path, int sync) override {
sync_ = sync;
std::string shm_path = path;
shm_path += "-shm";
struct SimbricksNicIfParams params = {
.pci_socket_path = nullptr,
.eth_socket_path = path,
.shm_path = shm_path.c_str(),
.pci_latency = 0,
.eth_latency = eth_latency,
.sync_delay = sync_period,
.sync_pci = 0,
.sync_eth = sync,
.sync_mode = sync_mode,
};
struct SimbricksProtoPcieDevIntro di;
int ret = SimbricksNicIfInit(&nicif_, &params, &di);
sync_ = params.sync_eth;
return ret == 0;
}
virtual bool IsSync() override {
return sync_;
}
virtual void Sync(uint64_t cur_ts) override {
if (SimbricksNicIfSync(&nicif_, cur_ts) != 0) {
fprintf(stderr, "SimbricksNicIfSync failed\n");
abort();
}
}
virtual void AdvanceEpoch(uint64_t cur_ts) override {
SimbricksNicIfAdvanceEpoch(&nicif_, cur_ts);
}
virtual uint64_t NextTimestamp() override {
return SimbricksNicIfNextTimestamp(&nicif_);
}
virtual enum RxPollState RxPacket(
const void *& data, size_t &len, uint64_t cur_ts) override {
assert(rx_ == nullptr);
rx_ = SimbricksNicIfN2DPoll(&nicif_, cur_ts);
if (!rx_)
return kRxPollFail;
uint8_t type = rx_->dummy.own_type & SIMBRICKS_PROTO_NET_N2D_MSG_MASK;
if (type == SIMBRICKS_PROTO_NET_N2D_MSG_RECV) {
data = (const void *)rx_->recv.data;
len = rx_->recv.len;
return kRxPollSuccess;
} else if (type == SIMBRICKS_PROTO_NET_N2D_MSG_SYNC) {
return kRxPollSync;
} else {
fprintf(stderr, "switch_pkt: unsupported type=%u\n", type);
abort();
}
}
virtual void RxDone() override {
assert(rx_ != nullptr);
SimbricksNicIfN2DDone(&nicif_, rx_);
SimbricksNicIfN2DNext(&nicif_);
rx_ = nullptr;
}
virtual bool TxPacket(
const void *data, size_t len, uint64_t cur_ts) override {
volatile union SimbricksProtoNetD2N *msg_to =
SimbricksNicIfD2NAlloc(&nicif_, cur_ts);
if (!msg_to)
return false;
volatile struct SimbricksProtoNetD2NSend *rx;
rx = &msg_to->send;
rx->len = len;
rx->port = 0;
memcpy((void *)rx->data, data, len);
// WMB();
rx->own_type =
SIMBRICKS_PROTO_NET_D2N_MSG_SEND | SIMBRICKS_PROTO_NET_D2N_OWN_NET;
return true;
}
};
/* Global variables */ /* Global variables */
static uint64_t cur_ts = 0; static uint64_t cur_ts = 0;
static int exiting = 0; static int exiting = 0;
...@@ -331,7 +442,7 @@ int main(int argc, char *argv[]) { ...@@ -331,7 +442,7 @@ int main(int argc, char *argv[]) {
pcap_t *pc = nullptr; pcap_t *pc = nullptr;
// Parse command line argument // Parse command line argument
while ((c = getopt(argc, argv, "s:uS:E:m:p:")) != -1 && !bad_option) { while ((c = getopt(argc, argv, "s:h:uS:E:m:p:")) != -1 && !bad_option) {
switch (c) { switch (c) {
case 's': { case 's': {
NetPort *port = new NetPort; NetPort *port = new NetPort;
...@@ -344,6 +455,17 @@ int main(int argc, char *argv[]) { ...@@ -344,6 +455,17 @@ int main(int argc, char *argv[]) {
break; break;
} }
case 'h': {
NetHostPort *port = new NetHostPort;
fprintf(stderr, "Switch listening on: %s\n", optarg);
if (!port->Connect(optarg, sync_eth)) {
fprintf(stderr, "listening on %s failed\n", optarg);
return EXIT_FAILURE;
}
ports.push_back(port);
break;
}
case 'u': case 'u':
sync_eth = 0; sync_eth = 0;
break; break;
......
...@@ -28,7 +28,7 @@ OBJS := $(d)net_switch.o ...@@ -28,7 +28,7 @@ OBJS := $(d)net_switch.o
$(OBJS): CPPFLAGS := $(CPPFLAGS) -I$(d)include/ $(OBJS): CPPFLAGS := $(CPPFLAGS) -I$(d)include/
$(bin_net_switch): $(OBJS) $(lib_netif) -lpcap $(bin_net_switch): $(OBJS) $(lib_netif) $(lib_nicif) -lpcap
CLEAN := $(bin_net_switch) $(OBJS) CLEAN := $(bin_net_switch) $(OBJS)
ALL := $(bin_net_switch) ALL := $(bin_net_switch)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment