gem5  [DEVELOP-FOR-23.0]
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
socket.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 The Regents of the University of California
3  * All rights reserved
4  *
5  * Copyright (c) 2002-2005 The Regents of The University of Michigan
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are
10  * met: redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer;
12  * redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution;
15  * neither the name of the copyright holders nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include "base/socket.hh"
33 
34 #include <netinet/in.h>
35 #include <netinet/tcp.h>
36 #include <sys/socket.h>
37 #include <sys/types.h>
38 #include <sys/un.h>
39 #include <unistd.h>
40 
41 #include <cerrno>
42 
43 // check if filesystem library is available
44 #if defined(__cpp_lib_filesystem) || __has_include(<filesystem>)
45  #include <filesystem>
46 #else
47  // This is only reachable if we're using GCC 7 or clang versions 6
48  // through 10 (note: gem5 does not support GCC versions older than
49  // GCC 7 or clang versions older than clang 6.0 as they do not
50  // support the C++17 standard).
51  // If we're using GCC 7 or clang versions 6 through 10, we need to use
52  // <experimental/filesystem>.
53  #include <experimental/filesystem>
54  namespace std {
55  namespace filesystem = experimental::filesystem;
56  }
57 #endif
58 
59 #include "base/logging.hh"
60 #include "base/output.hh"
61 #include "base/str.hh"
62 #include "base/types.hh"
63 #include "sim/byteswap.hh"
64 
65 namespace gem5
66 {
67 
69 bool ListenSocket::anyListening = false;
70 
71 bool ListenSocket::bindToLoopback = false;
72 
73 void
75 {
76  listeningDisabled = false;
77  anyListening = false;
78  bindToLoopback = false;
79 }
80 
81 void
83 {
84  if (anyListening)
85  panic("Too late to disable all listeners, already have a listener");
86  listeningDisabled = true;
87 }
88 
89 bool
91 {
92  return listeningDisabled;
93 }
94 
95 void
97 {
98  if (anyListening)
99  panic("Too late to bind to loopback, already have a listener");
100  bindToLoopback = true;
101 }
102 
103 // Wrappers to stub out SOCK_CLOEXEC/accept4 availability
104 
105 int
106 ListenSocket::socketCloexec(int domain, int type, int protocol)
107 {
108 #ifdef SOCK_CLOEXEC
109  type |= SOCK_CLOEXEC;
110 #endif
111  return ::socket(domain, type, protocol);
112 }
113 
114 int
115 ListenSocket::acceptCloexec(int sockfd, struct sockaddr *addr,
116  socklen_t *addrlen)
117 {
118 #if defined(_GNU_SOURCE) && defined(SOCK_CLOEXEC)
119  return ::accept4(sockfd, addr, addrlen, SOCK_CLOEXEC);
120 #else
121  return ::accept(sockfd, addr, addrlen);
122 #endif
123 }
124 
126 //
127 //
128 
129 ListenSocket::ListenSocket(const std::string &_name) : Named(_name) {}
130 
132 {
133  if (fd != -1)
134  close(fd);
135 }
136 
137 // Open a connection. Accept will block, so if you don't want it to,
138 // make sure a connection is ready before you call accept.
139 int
141 {
142  struct sockaddr_in sockaddr;
143  socklen_t slen = sizeof(sockaddr);
144  int sfd = acceptCloexec(fd, (struct sockaddr *)&sockaddr, &slen);
145  panic_if(sfd == -1, "%s: Failed to accept connection: %s",
146  name(), strerror(errno));
147 
148  return sfd;
149 }
150 
151 bool
152 ListenSocketConfig::parseIni(const std::string &value,
153  ListenSocketConfig &retval)
154 {
155  if (value.size() == 0) {
156  retval = listenSocketEmptyConfig();
157  return true;
158  } else if (value[0] == '@') {
159  retval = listenSocketUnixAbstractConfig(value.substr(1));
160  return true;
161  } else if (value[0] == 'P') {
162  std::filesystem::path p(value.substr(1));
163  retval = listenSocketUnixFileConfig(p.parent_path(), p.filename());
164  return true;
165  } else if (value[0] == '#') {
166  uint64_t port;
167  bool ret = to_number(value.substr(1), port);
168  if (!ret)
169  return false;
170  retval = listenSocketInetConfig(port);
171  return true;
172  } else {
173  panic("Can't interpret %s as a host socket.", value);
174  }
175 }
176 
177 ListenSocketInet::ListenSocketInet(const std::string &_name, int port)
178  : ListenSocket(_name), _port(port)
179 {}
180 
181 int
183 {
184  int sfd = ListenSocket::accept();
185  if (sfd == -1)
186  return -1;
187 
188  int i = 1;
189  int ret = ::setsockopt(sfd, IPPROTO_TCP, TCP_NODELAY, &i, sizeof(i));
190  warn_if(ret < 0, "ListenSocket(accept): setsockopt() TCP_NODELAY failed!");
191 
192  return sfd;
193 }
194 
195 // Create a socket and configure it for listening
196 bool
198 {
199  panic_if(listening, "Socket already listening!");
200 
201  // only create socket if not already created by a previous call
202  if (fd == -1) {
203  fd = socketCloexec(PF_INET, SOCK_STREAM, 0);
204  panic_if(fd < 0, "Can't create socket:%s !", strerror(errno));
205  }
206 
207  int i = 1;
208  int ret = ::setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
209  panic_if(ret < 0,
210  "ListenSocket(listen): setsockopt() SO_REUSEADDR failed!");
211 
212  struct sockaddr_in sockaddr;
213  sockaddr.sin_family = PF_INET;
214  sockaddr.sin_addr.s_addr =
215  htobe<in_addr_t>(bindToLoopback ? INADDR_LOOPBACK : INADDR_ANY);
216  sockaddr.sin_port = htons(port);
217  // finally clear sin_zero
218  std::memset(&sockaddr.sin_zero, 0, sizeof(sockaddr.sin_zero));
219  ret = ::bind(fd, (struct sockaddr *)&sockaddr, sizeof (sockaddr));
220  if (ret != 0) {
221  panic_if(ret == -1 && errno != EADDRINUSE,
222  "ListenSocket(listen): bind() failed!");
223  return false;
224  }
225 
226  if (::listen(fd, 1) == -1) {
227  panic_if(errno != EADDRINUSE,
228  "ListenSocket(listen): listen() failed!");
229  // User may decide to retry with a different port later; however, the
230  // socket is already bound to a port and the next bind will surely
231  // fail. We'll close the socket and reset fd to -1 so our user can
232  // retry with a cleaner state.
233  close(fd);
234  fd = -1;
235  return false;
236  }
237 
238  setListening();
239  return true;
240 }
241 
242 void
244 {
245  while (!listen(_port)) {
246  _port++;
247  fatal_if(_port > 65536, "%s: cannot find an available port.", name());
248  }
249  ccprintf(std::cerr, "%s: Listening for connections on %s\n",
250  name(), *this);
251 }
252 
253 void
254 ListenSocketInet::output(std::ostream &os) const
255 {
256  os << "port " << _port;
257 }
258 
261 {
262  return ListenSocketConfig([port](const std::string &name) {
263  return std::make_unique<ListenSocketInet>(name, port);
264  });
265 }
266 
267 void
268 ListenSocketUnix::checkPathLength(const std::string &original, size_t max_len)
269 {
270  fatal_if(original.size() > max_len,
271  "Length of socket path '%s' is %d, greater than max %d.",
272  original, original.size(), max_len);
273 }
274 
275 void
277 {
278  panic_if(listening, "%s: Socket already listening!", name());
279 
280  // only create socket if not already created by previous call
281  if (fd == -1) {
282  fd = socketCloexec(PF_UNIX, SOCK_STREAM, 0);
283  panic_if(fd < 0, "%s: Can't create unix socket:%s !",
284  name(), strerror(errno));
285  }
286 
287  sockaddr_un serv_addr;
288  std::memset(&serv_addr, 0, sizeof(serv_addr));
289  size_t addr_size = prepSockaddrUn(serv_addr);
290 
291  fatal_if(bind(fd, (struct sockaddr *)&(serv_addr), addr_size) != 0,
292  "%s: Cannot bind unix socket %s: %s", name(), *this,
293  strerror(errno));
294 
295  fatal_if(::listen(fd, 1) == -1, "%s: Failed to listen on %s: %s\n",
296  name(), *this, strerror(errno));
297 
298  ccprintf(std::cerr, "%s: Listening for connections on %s\n",
299  name(), *this);
300 
301  setListening();
302 }
303 
305  const std::string &_dir, const std::string &_fname) :
306  ListenSocketUnix(_name), dir(_dir), fname(_fname)
307 {
308  checkPathLength(fname, sizeof(sockaddr_un::sun_path) - 1);
309 }
310 
312 {
313  if (fd != -1) {
314  close(fd);
315  fd = -1;
316  unlink();
317  }
318 }
319 
320 bool
322 {
323  auto path = resolvedDir + "/" + fname;
324  return ::unlink(path.c_str()) == 0;
325 }
326 
327 size_t
329 {
330  addr.sun_family = AF_UNIX;
331  std::memcpy(addr.sun_path, fname.c_str(), fname.size());
332  return sizeof(addr.sun_path);
333 }
334 
335 void
337 {
339  warn_if(unlink(),
340  "%s: server path %s was occupied and will be replaced. Please "
341  "make sure there is no other server using the same path.",
342  name(), resolvedDir + "/" + fname);
343 
344  // Make sure "dir" exists.
345  std::error_code ec;
346  std::filesystem::create_directory(resolvedDir, ec);
347  fatal_if(ec, "Failed to create directory %s", ec.message());
348 
349  // Change the working directory to the directory containing the socket so
350  // that we maximize the limited space in sockaddr_un.sun_path.
351  auto cwd = std::filesystem::current_path(ec);
352  panic_if(ec, "Failed to get current working directory %s", ec.message());
353  std::filesystem::current_path(resolvedDir, ec);
354  fatal_if(ec, "Failed to change to directory %s: %s",
355  resolvedDir, ec.message());
356 
358 
359  std::filesystem::current_path(cwd, ec);
360  panic_if(ec, "Failed to change back working directory %s", ec.message());
361 }
362 
363 void
364 ListenSocketUnixFile::output(std::ostream &os) const
365 {
366  os << "socket \"" << dir << "/" << fname << "\"";
367 }
368 
370 listenSocketUnixFileConfig(std::string dir, std::string fname)
371 {
372  return ListenSocketConfig([dir, fname](const std::string &name) {
373  return std::make_unique<ListenSocketUnixFile>(name, dir, fname);
374  });
375 }
376 
377 size_t
379 {
380  addr.sun_family = AF_UNIX;
381  addr.sun_path[0] = '\0';
382  std::memcpy(&addr.sun_path[1], path.c_str(), path.size());
383  return offsetof(sockaddr_un, sun_path) + path.size() + 1;
384 }
385 
387  const std::string &_name, const std::string &_path) :
388  ListenSocketUnix(_name), path(_path)
389 {
390  checkPathLength(path, sizeof(sockaddr_un::sun_path) - 1);
391 }
392 
393 void
395 {
396  os << "abstract socket \"" << path << "\"";
397 }
398 
401 {
402  return ListenSocketConfig([path](const std::string &name) {
403  return std::make_unique<ListenSocketUnixAbstract>(name, path);
404  });
405 }
406 
407 } // namespace gem5
gem5::ListenSocketUnixFile::listen
void listen() override
Definition: socket.cc:336
gem5::ListenSocket::disableAll
static void disableAll()
Definition: socket.cc:82
gem5::ListenSocketUnixFile::resolvedDir
std::string resolvedDir
Definition: socket.hh:177
gem5::ListenSocketConfig::parseIni
static bool parseIni(const std::string &value, ListenSocketConfig &retval)
Definition: socket.cc:152
gem5::ListenSocket::listening
bool listening
Definition: socket.hh:65
socket.hh
gem5::ListenSocketUnixFile::output
void output(std::ostream &os) const override
Definition: socket.cc:364
gem5::ListenSocket::bindToLoopback
static bool bindToLoopback
Definition: socket.hh:56
gem5::ListenSocket::~ListenSocket
virtual ~ListenSocket()
Definition: socket.cc:131
gem5::ListenSocketUnix::checkPathLength
void checkPathLength(const std::string &original, size_t max_len)
Definition: socket.cc:268
gem5::ListenSocketUnixFile::ListenSocketUnixFile
ListenSocketUnixFile(const std::string &_name, const std::string &_dir, const std::string &_fname)
Definition: socket.cc:304
gem5::ListenSocketInet::listen
void listen() override
Definition: socket.cc:243
gem5::ListenSocketUnix::prepSockaddrUn
virtual size_t prepSockaddrUn(sockaddr_un &addr) const =0
gem5::ArmISA::domain
Bitfield< 7, 4 > domain
Definition: misc_types.hh:481
gem5::ListenSocketConfig
Definition: socket.hh:114
gem5::ListenSocketUnix::listen
void listen() override
Definition: socket.cc:276
gem5::ListenSocket
Definition: socket.hh:46
gem5::ListenSocketUnixFile::dir
std::string dir
Definition: socket.hh:176
gem5::ListenSocket::cleanup
static void cleanup()
Definition: socket.cc:74
gem5::simout
OutputDirectory simout
Definition: output.cc:62
gem5::to_number
bool to_number(const std::string &value, Pixel &retval)
Definition: pixel.hh:217
gem5::listenSocketUnixFileConfig
ListenSocketConfig listenSocketUnixFileConfig(std::string dir, std::string fname)
Definition: socket.cc:370
gem5::ListenSocketUnixFile::unlink
bool unlink() const
Definition: socket.cc:321
gem5::ArmISA::i
Bitfield< 7 > i
Definition: misc_types.hh:67
output.hh
gem5::ListenSocketInet::accept
int accept() override
Definition: socket.cc:182
gem5::ccprintf
void ccprintf(cp::Print &print)
Definition: cprintf.hh:130
gem5::ArmISA::ec
ec
Definition: misc_types.hh:727
gem5::Named
Interface for things with names.
Definition: named.hh:38
str.hh
gem5::listenSocketInetConfig
ListenSocketConfig listenSocketInetConfig(int port)
Definition: socket.cc:260
gem5::ListenSocketUnixAbstract::output
void output(std::ostream &os) const override
Definition: socket.cc:394
gem5::ListenSocket::listeningDisabled
static bool listeningDisabled
The following variables are only used by socket unit tests: listeningDisabled, anyListening,...
Definition: socket.hh:53
gem5::Named::name
virtual std::string name() const
Definition: named.hh:47
gem5::VegaISA::p
Bitfield< 54 > p
Definition: pagetable.hh:70
gem5::ListenSocket::acceptCloexec
static int acceptCloexec(int sockfd, struct sockaddr *addr, socklen_t *addrlen)
Definition: socket.cc:115
gem5::ListenSocket::anyListening
static bool anyListening
Definition: socket.hh:54
gem5::ListenSocketUnix
Definition: socket.hh:160
gem5::X86ISA::type
type
Definition: misc.hh:734
gem5::ListenSocketInet::ListenSocketInet
ListenSocketInet(const std::string &_name, int port)
Definition: socket.cc:177
gem5::OutputDirectory::resolve
std::string resolve(const std::string &name) const
Returns relative file names prepended with name of this directory.
Definition: output.cc:204
gem5::ListenSocket::ListenSocket
ListenSocket(const std::string &_name)
Definition: socket.cc:129
gem5::ListenSocketUnixAbstract::ListenSocketUnixAbstract
ListenSocketUnixAbstract(const std::string &_name, const std::string &_path)
Definition: socket.cc:386
gem5::ListenSocket::accept
virtual int accept()
Definition: socket.cc:140
gem5::ListenSocket::allDisabled
static bool allDisabled()
Definition: socket.cc:90
gem5::listenSocketUnixAbstractConfig
ListenSocketConfig listenSocketUnixAbstractConfig(std::string path)
Definition: socket.cc:400
gem5::ListenSocket::fd
int fd
Definition: socket.hh:66
name
const std::string & name()
Definition: trace.cc:48
gem5::ListenSocketUnixFile::fname
std::string fname
Definition: socket.hh:178
warn_if
#define warn_if(cond,...)
Conditional warning macro that checks the supplied condition and only prints a warning if the conditi...
Definition: logging.hh:283
panic_if
#define panic_if(cond,...)
Conditional panic macro that checks the supplied condition and only panics if the condition is true a...
Definition: logging.hh:214
std
Overload hash function for BasicBlockRange type.
Definition: misc.hh:2909
types.hh
gem5::X86ISA::os
Bitfield< 17 > os
Definition: misc.hh:810
gem5::ListenSocket::socketCloexec
static int socketCloexec(int domain, int type, int protocol)
Definition: socket.cc:106
gem5::ListenSocketInet::_port
int _port
Definition: socket.hh:144
logging.hh
gem5::ListenSocket::loopbackOnly
static void loopbackOnly()
Definition: socket.cc:96
fatal_if
#define fatal_if(cond,...)
Conditional fatal macro that checks the supplied condition and only causes a fatal error if the condi...
Definition: logging.hh:236
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: gpu_translation_state.hh:37
gem5::listenSocketEmptyConfig
static ListenSocketConfig listenSocketEmptyConfig()
Definition: socket.hh:137
gem5::ListenSocketUnixAbstract::prepSockaddrUn
size_t prepSockaddrUn(sockaddr_un &addr) const override
Definition: socket.cc:378
gem5::ListenSocket::setListening
void setListening()
Definition: socket.hh:69
byteswap.hh
gem5::ListenSocketInet::output
void output(std::ostream &os) const override
Definition: socket.cc:254
gem5::ListenSocketUnixFile::prepSockaddrUn
size_t prepSockaddrUn(sockaddr_un &addr) const override
Definition: socket.cc:328
panic
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:188
gem5::ListenSocketUnixAbstract::path
std::string path
Definition: socket.hh:199
gem5::ListenSocketUnixFile::~ListenSocketUnixFile
~ListenSocketUnixFile()
Definition: socket.cc:311
gem5::X86ISA::addr
Bitfield< 3 > addr
Definition: types.hh:84

Generated on Sun Jul 30 2023 01:56:51 for gem5 by doxygen 1.8.17