Gabe Black has uploaded this change for review. ( https://gem5-review.googlesource.com/c/public/gem5/+/69160?usp=email )

Change subject: base,cpu,dev,sim: Pull common logic into ListenSocket::listen().
......................................................................

base,cpu,dev,sim: Pull common logic into ListenSocket::listen().

Create a version of listen() which handles common logic internally,
including scanning for an available port number, and notifying what
port was chosen.

The port is managed internal to ListenSocket, so that the logic
interacting with it doesn't need to manually manage a port number, and
hence a port number does not need to exist for non AF_INET sockets.

Change-Id: Ie371eccc4d0da5e7b90714508e4cb72fb0091875
---
M src/base/remote_gdb.cc
M src/base/remote_gdb.hh
M src/base/socket.cc
M src/base/socket.hh
M src/base/vnc/vncserver.cc
M src/base/vnc/vncserver.hh
M src/cpu/nativetrace.cc
M src/dev/net/ethertap.cc
M src/dev/serial/terminal.cc
M src/dev/serial/terminal.hh
M src/sim/workload.cc
11 files changed, 64 insertions(+), 60 deletions(-)



diff --git a/src/base/remote_gdb.cc b/src/base/remote_gdb.cc
index 1a2fef4..2e66a92 100644
--- a/src/base/remote_gdb.cc
+++ b/src/base/remote_gdb.cc
@@ -392,7 +392,7 @@

 BaseRemoteGDB::BaseRemoteGDB(System *_system, int _port) :
         incomingConnectionEvent(nullptr), incomingDataEvent(nullptr),
-        _port(_port), fd(-1), sys(_system),
+        listener(name(), _port), fd(-1), sys(_system),
         connectEvent(*this), disconnectEvent(*this), trapEvent(this),
         singleStepEvent(*this)
 {}
@@ -417,17 +417,14 @@
         return;
     }

-    while (!listener.listen(_port)) {
-        DPRINTF(GDBMisc, "Can't bind port %d\n", _port);
-        _port++;
-    }
+    listener.listen();

     incomingConnectionEvent =
             new IncomingConnectionEvent(this, listener.getfd(), POLLIN);
     pollQueue.schedule(incomingConnectionEvent);

-    ccprintf(std::cerr, "%d: %s: listening for remote gdb on port %d\n",
-             curTick(), name(), _port);
+    ccprintf(std::cerr, "%d: %s: listening for remote gdb on %s\n",
+             curTick(), name(), listener);
 }

 void
@@ -448,12 +445,12 @@
     }
 }

-int
-BaseRemoteGDB::port() const
+const ListenSocket &
+BaseRemoteGDB::hostSocket() const
 {
     panic_if(!listener.islistening(),
- "Remote GDB port is unknown until listen() has been called.\n");
-    return _port;
+ "Remote GDB socket is unknown until listen() has been called.");
+    return listener;
 }

 void
diff --git a/src/base/remote_gdb.hh b/src/base/remote_gdb.hh
index 7981a13..25b7c54 100644
--- a/src/base/remote_gdb.hh
+++ b/src/base/remote_gdb.hh
@@ -161,7 +161,7 @@
     void listen();
     void connect();

-    int port() const;
+    const ListenSocket &hostSocket() const;

     void attach(int fd);
     void detach();
@@ -232,7 +232,6 @@
     IncomingDataEvent *incomingDataEvent;

     ListenSocket listener;
-    int _port;

     // The socket commands come in through.
     int fd;
diff --git a/src/base/socket.cc b/src/base/socket.cc
index 280f92b..1aff73a 100644
--- a/src/base/socket.cc
+++ b/src/base/socket.cc
@@ -173,10 +173,12 @@
 //
 //

-ListenSocket::ListenSocket()
-    : listening(false), fd(-1)
+ListenSocket::ListenSocket(const std::string &_name, int port)
+    : Named(_name), listening(false), fd(-1), _port(port)
 {}

+ListenSocket::ListenSocket() : ListenSocket("<unnammed>", -1) {}
+
 ListenSocket::~ListenSocket()
 {
     if (fd != -1)
@@ -231,6 +233,23 @@
     return true;
 }

+void
+ListenSocket::listen()
+{
+    while (!listen(_port)) {
+        _port++;
+ fatal_if(_port > 65536, "%s: cannot find an available port.", name());
+    }
+    ccprintf(std::cerr, "%s: Listening for connections on %s\n",
+            name(), *this);
+}
+
+void
+ListenSocket::output(std::ostream &os) const
+{
+    os << "port " << _port;
+}
+

 // Open a connection.  Accept will block, so if you don't want it to,
 // make sure a connection is ready before you call accept.
diff --git a/src/base/socket.hh b/src/base/socket.hh
index d2393e9..7a616bd 100644
--- a/src/base/socket.hh
+++ b/src/base/socket.hh
@@ -35,6 +35,8 @@

 #include <string>

+#include "base/named.hh"
+
 namespace gem5
 {

@@ -69,7 +71,7 @@
     std::string formattedPath;
 };

-class ListenSocket
+class ListenSocket : public Named
 {
   protected:
     /**
@@ -90,6 +92,7 @@
   protected:
     bool listening;
     int fd;
+    int _port;

     /*
      * cleanup resets the static variables back to their default values.
@@ -101,12 +104,16 @@
      * @ingroup api_socket
      * @{
      */
+    ListenSocket(const std::string &_name, int port);
     ListenSocket();
     virtual ~ListenSocket();

     virtual int accept();

     virtual bool listen(int port);
+    virtual void listen();
+
+    virtual void output(std::ostream &os) const;

     int getfd() const { return fd; }
     bool islistening() const { return listening; }
@@ -119,6 +126,13 @@
     /** @} */ // end of api_socket
 };

+inline static std::ostream &
+operator << (std::ostream &os, const ListenSocket &socket)
+{
+    socket.output(os);
+    return os;
+}
+
 } // namespace gem5

 #endif //__SOCKET_HH__
diff --git a/src/base/vnc/vncserver.cc b/src/base/vnc/vncserver.cc
index 39a1338..2d32cef 100644
--- a/src/base/vnc/vncserver.cc
+++ b/src/base/vnc/vncserver.cc
@@ -117,11 +117,11 @@
  */
 VncServer::VncServer(const Params &p)
     : VncInput(p), listenEvent(NULL), dataEvent(NULL), number(p.number),
-      dataFd(-1), sendUpdate(false),
+      listener(p.name, p.port), sendUpdate(false),
       supportsRawEnc(false), supportsResizeEnc(false)
 {
     if (p.port)
-        listen(p.port);
+        listen();

     curState = WaitForProtocolVersion;

@@ -157,22 +157,14 @@

 //socket creation and vnc client attach
 void
-VncServer::listen(int port)
+VncServer::listen()
 {
     if (ListenSocket::allDisabled()) {
warn_once("Sockets disabled, not accepting vnc client connections");
         return;
     }

-    while (!listener.listen(port)) {
-        DPRINTF(VNC,
-                "can't bind address vnc server port %d in use PID %d\n",
-                port, getpid());
-        port++;
-    }
-
-    ccprintf(std::cerr, "%s: Listening for connections on port %d\n",
-             name(), port);
+    listener.listen();

     listenEvent = new ListenEvent(this, listener.getfd(), POLLIN);
     pollQueue.schedule(listenEvent);
diff --git a/src/base/vnc/vncserver.hh b/src/base/vnc/vncserver.hh
index 091cb4d..7455799 100644
--- a/src/base/vnc/vncserver.hh
+++ b/src/base/vnc/vncserver.hh
@@ -182,7 +182,7 @@

     ListenSocket listener;

-    void listen(int port);
+    void listen();
     void accept();
     void data();
     void detach();
diff --git a/src/cpu/nativetrace.cc b/src/cpu/nativetrace.cc
index 714787f..05fb41b 100644
--- a/src/cpu/nativetrace.cc
+++ b/src/cpu/nativetrace.cc
@@ -39,17 +39,13 @@
 namespace trace {

 NativeTrace::NativeTrace(const Params &p)
-    : ExeTracer(p)
+    : ExeTracer(p), native_listener(p.name, 8000)
 {
     if (ListenSocket::allDisabled())
         fatal("All listeners are disabled!");

-    int port = 8000;
-    while (!native_listener.listen(port)) {
-        DPRINTF(GDBMisc, "Can't bind port %d\n", port);
-        port++;
-    }
-    ccprintf(std::cerr, "Listening for native process on port %d\n", port);
+    native_listener.listen();
+
     fd = native_listener.accept();
 }

diff --git a/src/dev/net/ethertap.cc b/src/dev/net/ethertap.cc
index 0769ad1..587dba5 100644
--- a/src/dev/net/ethertap.cc
+++ b/src/dev/net/ethertap.cc
@@ -240,17 +240,16 @@
     };

     friend class Event;
-    Event *event;
+    Event *event = nullptr;

     void accept();

   protected:
     ListenSocket listener;
     EtherTapStub *tap;
-    int port;

   public:
-    TapListener(EtherTapStub *t, int p) : event(NULL), tap(t), port(p) {}
+    TapListener(EtherTapStub *t, int p) : listener(t->name(), p), tap(t) {}
     ~TapListener() { delete event; }

     void listen();
@@ -259,12 +258,8 @@
 void
 TapListener::listen()
 {
-    while (!listener.listen(port)) {
- DPRINTF(Ethernet, "TapListener(listen): Can't bind port %d\n", port);
-        port++;
-    }
+    listener.listen();

-    ccprintf(std::cerr, "Listening for tap connection on port %d\n", port);
     event = new Event(this, listener.getfd(), POLLIN|POLLERR);
     pollQueue.schedule(event);
 }
diff --git a/src/dev/serial/terminal.cc b/src/dev/serial/terminal.cc
index 9564876..02052b5 100644
--- a/src/dev/serial/terminal.cc
+++ b/src/dev/serial/terminal.cc
@@ -121,8 +121,8 @@
  */
 Terminal::Terminal(const Params &p)
     : SerialDevice(p), listenEvent(NULL), dataEvent(NULL),
-      number(p.number), data_fd(-1), txbuf(16384), rxbuf(16384),
-      outfile(terminalDump(p))
+      number(p.number), data_fd(-1), listener(p.name, p.port),
+      txbuf(16384), rxbuf(16384), outfile(terminalDump(p))
 #if TRACING_ON == 1
       , linebuf(16384)
 #endif
@@ -131,7 +131,7 @@
         outfile->stream()->setf(std::ios::unitbuf);

     if (p.port)
-        listen(p.port);
+        listen();
 }

 Terminal::~Terminal()
@@ -168,22 +168,14 @@
 //

 void
-Terminal::listen(int port)
+Terminal::listen()
 {
     if (ListenSocket::allDisabled()) {
         warn_once("Sockets disabled, not accepting terminal connections");
         return;
     }

-    while (!listener.listen(port)) {
-        DPRINTF(Terminal,
-                ": can't bind address terminal port %d inuse PID %d\n",
-                port, getpid());
-        port++;
-    }
-
-    ccprintf(std::cerr, "%s: Listening for connections on port %d\n",
-             name(), port);
+    listener.listen();

     listenEvent = new ListenEvent(this, listener.getfd(), POLLIN);
     pollQueue.schedule(listenEvent);
diff --git a/src/dev/serial/terminal.hh b/src/dev/serial/terminal.hh
index 83ea64b..bd6711d 100644
--- a/src/dev/serial/terminal.hh
+++ b/src/dev/serial/terminal.hh
@@ -103,7 +103,7 @@
   protected:
     ListenSocket listener;

-    void listen(int port);
+    void listen();
     void accept();

   protected:
diff --git a/src/sim/workload.cc b/src/sim/workload.cc
index ceb1029..84b1e40 100644
--- a/src/sim/workload.cc
+++ b/src/sim/workload.cc
@@ -97,8 +97,8 @@
// Now that we're about to start simulation, wait for GDB connections if
     // requested.
     if (gdb && waitForRemoteGDB) {
- inform("%s: Waiting for a remote GDB connection on port %d.", name(),
-                gdb->port());
+        inform("%s: Waiting for a remote GDB connection on %s.", name(),
+                gdb->hostSocket());
         gdb->connect();
     }
 }

--
To view, visit https://gem5-review.googlesource.com/c/public/gem5/+/69160?usp=email To unsubscribe, or for help writing mail filters, visit https://gem5-review.googlesource.com/settings

Gerrit-Project: public/gem5
Gerrit-Branch: develop
Gerrit-Change-Id: Ie371eccc4d0da5e7b90714508e4cb72fb0091875
Gerrit-Change-Number: 69160
Gerrit-PatchSet: 1
Gerrit-Owner: Gabe Black <gabe.bl...@gmail.com>
Gerrit-CC: Gabe Black <gabebl...@google.com>
Gerrit-MessageType: newchange
_______________________________________________
gem5-dev mailing list -- gem5-dev@gem5.org
To unsubscribe send an email to gem5-dev-le...@gem5.org

Reply via email to