Deleted Added
sdiff udiff text old ( 11263:8dcc6b40f164 ) new ( 11290:1640dd68b0a4 )
full compact
1/*
2 * Copyright (c) 2015 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software

--- 21 unchanged lines hidden (view full) ---

30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 * Authors: Gabor Dozsa
38 */
39
40/* @file
41 * TCP stream socket based interface class implementation for multi gem5 runs.
42 */
43
44#include "dev/net/tcp_iface.hh"
45
46#include <arpa/inet.h>
47#include <netdb.h>
48#include <sys/socket.h>
49#include <sys/types.h>
50#include <unistd.h>
51
52#include <cerrno>
53#include <cstring>
54
55#include "base/types.hh"
56#include "debug/MultiEthernet.hh"
57
58// MSG_NOSIGNAL does not exists on OS X
59#if defined(__APPLE__) || defined(__MACH__)
60#ifndef MSG_NOSIGNAL
61#define MSG_NOSIGNAL SO_NOSIGPIPE
62#endif
63#endif
64
65using namespace std;
66
67vector<int> TCPIface::sockRegistry;
68
69TCPIface::TCPIface(string server_name, unsigned server_port,
70 unsigned multi_rank, Tick sync_start, Tick sync_repeat,
71 EventManager *em) :
72 MultiIface(multi_rank, sync_start, sync_repeat, em)
73{
74 struct addrinfo addr_hint, *addr_results;
75 int ret;
76
77 string port_str = to_string(server_port);
78
79 sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
80 panic_if(sock < 0, "socket() failed: %s", strerror(errno));
81
82 bzero(&addr_hint, sizeof(addr_hint));
83 addr_hint.ai_family = AF_INET;
84 addr_hint.ai_socktype = SOCK_STREAM;
85 addr_hint.ai_protocol = IPPROTO_TCP;
86
87 ret = getaddrinfo(server_name.c_str(), port_str.c_str(),
88 &addr_hint, &addr_results);
89 panic_if(ret < 0, "getaddrinf() failed: %s", strerror(errno));
90
91 DPRINTF(MultiEthernet, "Connecting to %s:%u\n",
92 server_name.c_str(), port_str.c_str());
93
94 ret = ::connect(sock, (struct sockaddr *)(addr_results->ai_addr),
95 addr_results->ai_addrlen);
96 panic_if(ret < 0, "connect() failed: %s", strerror(errno));
97
98 freeaddrinfo(addr_results);
99 // add our socket to the static registry
100 sockRegistry.push_back(sock);
101 // let the server know who we are
102 sendTCP(sock, &multi_rank, sizeof(multi_rank));
103}
104
105TCPIface::~TCPIface()
106{
107 int M5_VAR_USED ret;
108
109 ret = close(sock);
110 assert(ret == 0);
111}
112
113void
114TCPIface::sendTCP(int sock, void *buf, unsigned length)
115{
116 ssize_t ret;
117
118 ret = ::send(sock, buf, length, MSG_NOSIGNAL);
119 panic_if(ret < 0, "send() failed: %s", strerror(errno));
120 panic_if(ret != length, "send() failed");
121}
122
123bool
124TCPIface::recvTCP(int sock, void *buf, unsigned length)
125{
126 ssize_t ret;
127

--- 7 unchanged lines hidden (view full) ---

135 inform("recv(): Connection closed");
136 } else if (ret != length)
137 panic("recv() failed");
138
139 return (ret == length);
140}
141
142void
143TCPIface::syncRaw(MultiHeaderPkt::MsgType sync_req, Tick sync_tick)
144{
145 /*
146 * Barrier is simply implemented by point-to-point messages to the server
147 * for now. This method is called by only one TCPIface object.
148 * The server will send back an 'ack' message when it gets the
149 * sync request from all clients.
150 */
151 MultiHeaderPkt::Header header_pkt;
152 header_pkt.msgType = sync_req;
153 header_pkt.sendTick = sync_tick;
154
155 for (auto s : sockRegistry)
156 sendTCP(s, (void *)&header_pkt, sizeof(header_pkt));
157}
158