SINIC: Commit old code from ASPLOS 2006 studies.
[gem5.git] / src / dev / ethertap.cc
index 65089a8b22bfee636c9dcee3468cf980068ebd9c..f1fd1800ba7ad091587df19f24ee05171adb2a4d 100644 (file)
@@ -50,7 +50,6 @@
 #include "dev/etherint.hh"
 #include "dev/etherpkt.hh"
 #include "dev/ethertap.hh"
-#include "sim/builder.hh"
 
 using namespace std;
 
@@ -127,13 +126,17 @@ class TapEvent : public PollEvent
     virtual void process(int revent) { tap->process(revent); }
 };
 
-EtherTap::EtherTap(const string &name, EtherDump *d, int port, int bufsz)
-    : EtherInt(name), event(NULL), socket(-1), buflen(bufsz), dump(d),
-      txEvent(this)
+EtherTap::EtherTap(const Params *p)
+    : EtherObject(p), event(NULL), socket(-1), buflen(p->bufsz), dump(p->dump),
+      interface(NULL), txEvent(this)
 {
+    if (ListenSocket::allDisabled())
+        fatal("All listeners are disabled! EtherTap can't work!");
+
     buffer = new char[buflen];
-    listener = new TapListener(this, port);
+    listener = new TapListener(this, p->port);
     listener->listen();
+    interface = new EtherTapInt(name() + ".interface", this);
 }
 
 EtherTap::~EtherTap()
@@ -179,10 +182,14 @@ EtherTap::recvPacket(EthPacketPtr packet)
     DPRINTF(Ethernet, "EtherTap output len=%d\n", packet->length);
     DDUMP(EthernetData, packet->data, packet->length);
     uint32_t len = htonl(packet->length);
-    write(socket, &len, sizeof(len));
-    write(socket, packet->data, packet->length);
+    ssize_t ret = write(socket, &len, sizeof(len));
+    if (ret != sizeof(len))
+        return false;
+    ret = write(socket, packet->data, packet->length);
+    if (ret != packet->length)
+        return false;
 
-    recvDone();
+    interface->recvDone();
 
     return true;
 }
@@ -235,7 +242,7 @@ EtherTap::process(int revent)
 
         DPRINTF(Ethernet, "EtherTap input len=%d\n", packet->length);
         DDUMP(EthernetData, packet->data, packet->length);
-        if (!sendPacket(packet)) {
+        if (!interface->sendPacket(packet)) {
             DPRINTF(Ethernet, "bus busy...buffer for retransmission\n");
             packetBuffer.push(packet);
             if (!txEvent.scheduled())
@@ -253,7 +260,7 @@ EtherTap::retransmit()
         return;
 
     EthPacketPtr packet = packetBuffer.front();
-    if (sendPacket(packet)) {
+    if (interface->sendPacket(packet)) {
         if (dump)
             dump->dump(packet);
         DPRINTF(Ethernet, "EtherTap retransmit\n");
@@ -265,6 +272,18 @@ EtherTap::retransmit()
         txEvent.schedule(curTick + retryTime);
 }
 
+EtherInt*
+EtherTap::getEthPort(const std::string &if_name, int idx)
+{
+    if (if_name == "tap") {
+        if (interface->getPeer())
+            panic("Interface already connected to\n");
+        return interface;
+    }
+    return NULL;
+}
+
+
 //=====================================================================
 
 void
@@ -313,35 +332,8 @@ EtherTap::unserialize(Checkpoint *cp, const std::string &section)
 
 //=====================================================================
 
-BEGIN_DECLARE_SIM_OBJECT_PARAMS(EtherTap)
-
-    SimObjectParam<EtherInt *> peer;
-    SimObjectParam<EtherDump *> dump;
-    Param<unsigned> port;
-    Param<unsigned> bufsz;
-
-END_DECLARE_SIM_OBJECT_PARAMS(EtherTap)
-
-BEGIN_INIT_SIM_OBJECT_PARAMS(EtherTap)
-
-    INIT_PARAM_DFLT(peer, "peer interface", NULL),
-    INIT_PARAM_DFLT(dump, "object to dump network packets to", NULL),
-    INIT_PARAM_DFLT(port, "tap port", 3500),
-    INIT_PARAM_DFLT(bufsz, "tap buffer size", 10000)
-
-END_INIT_SIM_OBJECT_PARAMS(EtherTap)
-
-
-CREATE_SIM_OBJECT(EtherTap)
+EtherTap *
+EtherTapParams::create()
 {
-    EtherTap *tap = new EtherTap(getInstanceName(), dump, port, bufsz);
-
-    if (peer) {
-        tap->setPeer(peer);
-        peer->setPeer(tap);
-    }
-
-    return tap;
+    return new EtherTap(this);
 }
-
-REGISTER_SIM_OBJECT("EtherTap", EtherTap)