tcp_iface.cc revision 11263
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 9 * licensed hereunder. You may use the software subject to the license 10 * terms below provided that you ensure that this notice is replicated 11 * unmodified and in its entirety in all distributions of the software, 12 * modified or unmodified, in source code or in binary form. 13 * 14 * Redistribution and use in source and binary forms, with or without 15 * modification, are permitted provided that the following conditions are 16 * met: redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer; 18 * redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution; 21 * neither the name of the copyright holders nor the names of its 22 * contributors may be used to endorse or promote products derived from 23 * this software without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 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 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