dev: Turn EtherObject into an interface class.

This class used to drive from SimObject so that it could be derived
from to get both the interface and SimObject while still using single
inheritance.

With this change, EtherObject is now just an interface class with only
one pure virtual function which can be inherited alongside SimObject.
This makes it more flexible so that it can be used in places where you
might want a different inheritance hierarchy, for instance to inherit
from MemObject.

Change-Id: I0f07664d104eed012cf4ce6e30c416ada19505a7
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/17028
Reviewed-by: Andreas Sandberg <andreas.sandberg@arm.com>
Maintainer: Andreas Sandberg <andreas.sandberg@arm.com>
diff --git a/src/dev/net/Ethernet.py b/src/dev/net/Ethernet.py
index 7c3c766..6e900e0 100644
--- a/src/dev/net/Ethernet.py
+++ b/src/dev/net/Ethernet.py
@@ -44,14 +44,10 @@
 from m5.proxy import *
 from m5.objects.PciDevice import PciDevice
 
-class EtherObject(SimObject):
-    type = 'EtherObject'
-    abstract = True
-    cxx_header = "dev/net/etherobject.hh"
-
-class EtherLink(EtherObject):
+class EtherLink(SimObject):
     type = 'EtherLink'
     cxx_header = "dev/net/etherlink.hh"
+    cxx_extra_bases = [ "EtherObject" ]
     int0 = SlavePort("interface 0")
     int1 = SlavePort("interface 1")
     delay = Param.Latency('0us', "packet transmit delay")
@@ -59,9 +55,10 @@
     speed = Param.NetworkBandwidth('1Gbps', "link speed")
     dump = Param.EtherDump(NULL, "dump object")
 
-class DistEtherLink(EtherObject):
+class DistEtherLink(SimObject):
     type = 'DistEtherLink'
     cxx_header = "dev/net/dist_etherlink.hh"
+    cxx_extra_bases = [ "EtherObject" ]
     int0 = SlavePort("interface 0")
     delay = Param.Latency('0us', "packet transmit delay")
     delay_var = Param.Latency('0ns', "packet transmit delay variability")
@@ -77,16 +74,18 @@
     dist_sync_on_pseudo_op = Param.Bool(False, "Start sync with pseudo_op")
     num_nodes = Param.UInt32('2', "Number of simulate nodes")
 
-class EtherBus(EtherObject):
+class EtherBus(SimObject):
     type = 'EtherBus'
     cxx_header = "dev/net/etherbus.hh"
+    cxx_extra_bases = [ "EtherObject" ]
     loopback = Param.Bool(True, "send packet back to the sending interface")
     dump = Param.EtherDump(NULL, "dump object")
     speed = Param.NetworkBandwidth('100Mbps', "bus speed in bits per second")
 
-class EtherSwitch(EtherObject):
+class EtherSwitch(SimObject):
     type = 'EtherSwitch'
     cxx_header = "dev/net/etherswitch.hh"
+    cxx_extra_bases = [ "EtherObject" ]
     dump = Param.EtherDump(NULL, "dump object")
     fabric_speed = Param.NetworkBandwidth('10Gbps', "switch fabric speed in bits "
                                           "per second")
@@ -96,10 +95,11 @@
     delay_var = Param.Latency('0ns', "packet transmit delay variability")
     time_to_live = Param.Latency('10ms', "time to live of MAC address maping")
 
-class EtherTapBase(EtherObject):
+class EtherTapBase(SimObject):
     type = 'EtherTapBase'
     abstract = True
     cxx_header = "dev/net/ethertap.hh"
+    cxx_extra_bases = [ "EtherObject" ]
     bufsz = Param.Int(10000, "tap buffer size")
     dump = Param.EtherDump(NULL, "dump object")
     tap = SlavePort("Ethernet interface to connect to gem5's network")
diff --git a/src/dev/net/SConscript b/src/dev/net/SConscript
index 908dd44..0bb6bbf 100644
--- a/src/dev/net/SConscript
+++ b/src/dev/net/SConscript
@@ -45,6 +45,7 @@
 Import('*')
 
 SimObject('Ethernet.py')
+Source('python.cc', add_tags='python')
 
 # Basic Ethernet infrastructure
 Source('etherbus.cc')
diff --git a/src/dev/net/dist_etherlink.cc b/src/dev/net/dist_etherlink.cc
index 01f21d1..477ad61 100644
--- a/src/dev/net/dist_etherlink.cc
+++ b/src/dev/net/dist_etherlink.cc
@@ -72,7 +72,7 @@
 using namespace std;
 
 DistEtherLink::DistEtherLink(const Params *p)
-    : EtherObject(p), linkDelay(p->delay)
+    : SimObject(p), linkDelay(p->delay)
 {
     DPRINTF(DistEthernet,"DistEtherLink::DistEtherLink() "
             "link delay:%llu ticksPerByte:%f\n", p->delay, p->speed);
diff --git a/src/dev/net/dist_etherlink.hh b/src/dev/net/dist_etherlink.hh
index 83f28f2..51852a5 100644
--- a/src/dev/net/dist_etherlink.hh
+++ b/src/dev/net/dist_etherlink.hh
@@ -53,6 +53,7 @@
 #include <iostream>
 
 #include "dev/net/etherlink.hh"
+#include "dev/net/etherobject.hh"
 #include "params/DistEtherLink.hh"
 
 class DistIface;
@@ -61,7 +62,7 @@
 /**
  * Model for a fixed bandwidth full duplex ethernet link.
  */
-class DistEtherLink : public EtherObject
+class DistEtherLink : public SimObject, public EtherObject
 {
   protected:
     class LocalIface;
@@ -223,8 +224,7 @@
         return dynamic_cast<const Params *>(_params);
     }
 
-    virtual EtherInt *getEthPort(const std::string &if_name,
-                                 int idx) override;
+    EtherInt *getEthPort(const std::string &if_name, int idx) override;
 
     virtual void init() override;
     virtual void startup() override;
diff --git a/src/dev/net/etherbus.cc b/src/dev/net/etherbus.cc
index 7e086bf..dee772c 100644
--- a/src/dev/net/etherbus.cc
+++ b/src/dev/net/etherbus.cc
@@ -51,7 +51,7 @@
 using namespace std;
 
 EtherBus::EtherBus(const Params *p)
-    : EtherObject(p), ticksPerByte(p->speed), loopback(p->loopback),
+    : SimObject(p), ticksPerByte(p->speed), loopback(p->loopback),
       event([this]{ txDone(); }, "ethernet bus completion"),
       sender(0), dump(p->dump)
 {
diff --git a/src/dev/net/etherbus.hh b/src/dev/net/etherbus.hh
index 1b26453..8c1260f 100644
--- a/src/dev/net/etherbus.hh
+++ b/src/dev/net/etherbus.hh
@@ -43,7 +43,7 @@
 
 class EtherDump;
 class EtherInt;
-class EtherBus : public EtherObject
+class EtherBus : public SimObject, public EtherObject
 {
   protected:
     typedef std::list<EtherInt *> devlist_t;
@@ -72,7 +72,7 @@
     void reg(EtherInt *dev);
     bool busy() const { return (bool)packet; }
     bool send(EtherInt *sender, EthPacketPtr &packet);
-    virtual EtherInt *getEthPort(const std::string &if_name, int idx);
+    EtherInt *getEthPort(const std::string &if_name, int idx) override;
 };
 
 #endif // __DEV_NET_ETHERBUS_HH__
diff --git a/src/dev/net/etherlink.cc b/src/dev/net/etherlink.cc
index 9a11018..b160e29 100644
--- a/src/dev/net/etherlink.cc
+++ b/src/dev/net/etherlink.cc
@@ -67,7 +67,7 @@
 using namespace std;
 
 EtherLink::EtherLink(const Params *p)
-    : EtherObject(p)
+    : SimObject(p)
 {
     link[0] = new Link(name() + ".link0", this, 0, p->speed,
                        p->delay, p->delay_var, p->dump);
diff --git a/src/dev/net/etherlink.hh b/src/dev/net/etherlink.hh
index 7a98708..37fa168 100644
--- a/src/dev/net/etherlink.hh
+++ b/src/dev/net/etherlink.hh
@@ -62,7 +62,7 @@
 /*
  * Model for a fixed bandwidth full duplex ethernet link
  */
-class EtherLink : public EtherObject
+class EtherLink : public EtherObject, public SimObject
 {
   protected:
     class Interface;
diff --git a/src/dev/net/etherobject.hh b/src/dev/net/etherobject.hh
index 55cfa97..638c506 100644
--- a/src/dev/net/etherobject.hh
+++ b/src/dev/net/etherobject.hh
@@ -1,5 +1,6 @@
 /*
  * Copyright (c) 2007 The Regents of The University of Michigan
+ * Copyright 2019 Google, Inc.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -26,6 +27,7 @@
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  * Authors: Ali Saidi
+ *          Gabe Black
  */
 
 /**
@@ -36,32 +38,17 @@
 #ifndef __DEV_NET_ETHEROBJECT_HH__
 #define __DEV_NET_ETHEROBJECT_HH__
 
-#include "params/EtherObject.hh"
-#include "sim/sim_object.hh"
+#include <string>
 
 class EtherInt;
 
 /**
- * The base EtherObject class, allows for an accesor function to a
- * simobj that returns the Port.
+ * The base EtherObject interface.
  */
-class EtherObject : public SimObject
+class EtherObject
 {
   public:
-    typedef EtherObjectParams Params;
-    EtherObject(const Params *params)
-        : SimObject(params) {}
-
-    const Params *
-    params() const
-    {
-        return dynamic_cast<const Params *>(_params);
-    }
-
-  public:
-    /** Additional function to return the Port of a memory object. */
-    virtual EtherInt *getEthPort(const std::string &if_name, int idx = -1) = 0;
-
+    virtual EtherInt *getEthPort(const std::string &if_name, int idx=-1) = 0;
 };
 
 #endif // __DEV_NET_ETHEROBJECT_HH__
diff --git a/src/dev/net/etherswitch.cc b/src/dev/net/etherswitch.cc
index 0e1d6f3..c03b594 100644
--- a/src/dev/net/etherswitch.cc
+++ b/src/dev/net/etherswitch.cc
@@ -43,7 +43,7 @@
 using namespace std;
 
 EtherSwitch::EtherSwitch(const Params *p)
-    : EtherObject(p), ttl(p->time_to_live)
+    : SimObject(p), ttl(p->time_to_live)
 {
     for (int i = 0; i < p->port_interface_connection_count; ++i) {
         std::string interfaceName = csprintf("%s.interface%d", name(), i);
diff --git a/src/dev/net/etherswitch.hh b/src/dev/net/etherswitch.hh
index 0887d94..36a0c68 100644
--- a/src/dev/net/etherswitch.hh
+++ b/src/dev/net/etherswitch.hh
@@ -47,8 +47,9 @@
 #include "dev/net/pktfifo.hh"
 #include "params/EtherSwitch.hh"
 #include "sim/eventq.hh"
+#include "sim/sim_object.hh"
 
-class EtherSwitch : public EtherObject
+class EtherSwitch : public SimObject, public EtherObject
 {
   public:
     typedef EtherSwitchParams Params;
diff --git a/src/dev/net/ethertap.cc b/src/dev/net/ethertap.cc
index 4e32a8c..bf2dc68 100644
--- a/src/dev/net/ethertap.cc
+++ b/src/dev/net/ethertap.cc
@@ -92,7 +92,7 @@
 };
 
 EtherTapBase::EtherTapBase(const Params *p)
-    : EtherObject(p), buflen(p->bufsz), dump(p->dump), event(NULL),
+    : SimObject(p), buflen(p->bufsz), dump(p->dump), event(NULL),
       interface(NULL),
       txEvent([this]{ retransmit(); }, "EtherTapBase retransmit")
 {
diff --git a/src/dev/net/ethertap.hh b/src/dev/net/ethertap.hh
index 9b4e175..7db73c5 100644
--- a/src/dev/net/ethertap.hh
+++ b/src/dev/net/ethertap.hh
@@ -56,7 +56,7 @@
 class TapEvent;
 class EtherTapInt;
 
-class EtherTapBase : public EtherObject
+class EtherTapBase : public SimObject, public EtherObject
 {
   public:
     typedef EtherTapBaseParams Params;
diff --git a/src/dev/net/python.cc b/src/dev/net/python.cc
new file mode 100644
index 0000000..a012074
--- /dev/null
+++ b/src/dev/net/python.cc
@@ -0,0 +1,48 @@
+/*
+ * Copyright 2019 Google, Inc.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met: redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer;
+ * redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution;
+ * neither the name of the copyright holders nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Authors: Gabe Black
+ */
+
+#include "python/pybind11/pybind.hh"
+
+#include "dev/net/etherobject.hh"
+#include "sim/init.hh"
+
+namespace
+{
+
+void
+ethernet_pybind(pybind11::module &m_internal)
+{
+    pybind11::module m = m_internal.def_submodule("ethernet");
+    pybind11::class_<
+        EtherObject, std::unique_ptr<EtherObject, pybind11::nodelete>>(
+                m, "EtherObject");
+}
+EmbeddedPyBind embed_("ethernet", &ethernet_pybind);
+
+} // anonymous namespace