tcp_iface.cc revision 10923
113071Sgabeblack@google.com/*
213071Sgabeblack@google.com * Copyright (c) 2015 ARM Limited
313071Sgabeblack@google.com * All rights reserved
413071Sgabeblack@google.com *
513071Sgabeblack@google.com * The license below extends only to copyright in the software and shall
613071Sgabeblack@google.com * not be construed as granting a license to any other intellectual
713071Sgabeblack@google.com * property including but not limited to intellectual property relating
813071Sgabeblack@google.com * to a hardware implementation of the functionality of the software
913071Sgabeblack@google.com * licensed hereunder.  You may use the software subject to the license
1013071Sgabeblack@google.com * terms below provided that you ensure that this notice is replicated
1113071Sgabeblack@google.com * unmodified and in its entirety in all distributions of the software,
1213071Sgabeblack@google.com * modified or unmodified, in source code or in binary form.
1313071Sgabeblack@google.com *
1413071Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
1513071Sgabeblack@google.com * modification, are permitted provided that the following conditions are
1613071Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
1713071Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
1813071Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
1913071Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
2013071Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
2113071Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
2213071Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
2313071Sgabeblack@google.com * this software without specific prior written permission.
2413071Sgabeblack@google.com *
2513071Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2613071Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2713071Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2813071Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2913071Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3013071Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3113071Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3213071Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3313071Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3413071Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3513071Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3613071Sgabeblack@google.com *
3713071Sgabeblack@google.com * Authors: Gabor Dozsa
3813071Sgabeblack@google.com */
3913071Sgabeblack@google.com
4013071Sgabeblack@google.com/* @file
4113071Sgabeblack@google.com * TCP stream socket based interface class implementation for multi gem5 runs.
4213071Sgabeblack@google.com */
4313071Sgabeblack@google.com
4413071Sgabeblack@google.com#include "dev/tcp_iface.hh"
4513071Sgabeblack@google.com
4613071Sgabeblack@google.com#include <arpa/inet.h>
4713071Sgabeblack@google.com#include <netdb.h>
4813071Sgabeblack@google.com#include <sys/socket.h>
4913071Sgabeblack@google.com#include <sys/types.h>
5013071Sgabeblack@google.com#include <unistd.h>
5113071Sgabeblack@google.com
5213071Sgabeblack@google.com#include <cerrno>
5313071Sgabeblack@google.com#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
128    ret = ::recv(sock, buf, length,  MSG_WAITALL );
129    if (ret < 0) {
130        if (errno == ECONNRESET || errno == EPIPE)
131            inform("recv(): %s", strerror(errno));
132        else if (ret < 0)
133            panic("recv() failed: %s", strerror(errno));
134    } else if (ret == 0) {
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
159