tcp_iface.hh revision 10923
112854Sgabeblack@google.com/*
212854Sgabeblack@google.com * Copyright (c) 2015 ARM Limited
312854Sgabeblack@google.com * All rights reserved
412854Sgabeblack@google.com *
512854Sgabeblack@google.com * The license below extends only to copyright in the software and shall
612854Sgabeblack@google.com * not be construed as granting a license to any other intellectual
712854Sgabeblack@google.com * property including but not limited to intellectual property relating
812854Sgabeblack@google.com * to a hardware implementation of the functionality of the software
912854Sgabeblack@google.com * licensed hereunder.  You may use the software subject to the license
1012854Sgabeblack@google.com * terms below provided that you ensure that this notice is replicated
1112854Sgabeblack@google.com * unmodified and in its entirety in all distributions of the software,
1212854Sgabeblack@google.com * modified or unmodified, in source code or in binary form.
1312854Sgabeblack@google.com *
1412854Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
1512854Sgabeblack@google.com * modification, are permitted provided that the following conditions are
1612854Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
1712854Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
1812854Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
1912854Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
2012854Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
2112854Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
2212854Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
2312854Sgabeblack@google.com * this software without specific prior written permission.
2412854Sgabeblack@google.com *
2512854Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2612854Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2712854Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2812854Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2912854Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3012854Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3112854Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3212854Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3312854Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3412854Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3512854Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3612854Sgabeblack@google.com *
3712854Sgabeblack@google.com * Authors: Gabor Dozsa
3812854Sgabeblack@google.com */
3912854Sgabeblack@google.com
4012854Sgabeblack@google.com/* @file
4112854Sgabeblack@google.com * TCP stream socket based interface class for multi gem5 runs.
4212854Sgabeblack@google.com *
4312854Sgabeblack@google.com * For a high level description about multi gem5 see comments in
4412854Sgabeblack@google.com * header file multi_iface.hh.
4512854Sgabeblack@google.com *
4612854Sgabeblack@google.com * The TCP subclass of MultiIface uses a separate server process
4712854Sgabeblack@google.com * (see tcp_server.[hh,cc] under directory gem5/util/multi). Each gem5
4812854Sgabeblack@google.com * process connects to the server via a stream socket. The server process
4912854Sgabeblack@google.com * transfers messages and co-ordinates the synchronisation among the gem5
5012854Sgabeblack@google.com * peers.
5112854Sgabeblack@google.com */
5212854Sgabeblack@google.com#ifndef __DEV_TCP_IFACE_HH__
5312854Sgabeblack@google.com#define __DEV_TCP_IFACE_HH__
5412854Sgabeblack@google.com
5512854Sgabeblack@google.com
5612854Sgabeblack@google.com#include <string>
5712854Sgabeblack@google.com
5812854Sgabeblack@google.com#include "dev/multi_iface.hh"
5912854Sgabeblack@google.com
6012854Sgabeblack@google.comclass EventManager;
6112854Sgabeblack@google.com
6212854Sgabeblack@google.comclass TCPIface : public MultiIface
6312854Sgabeblack@google.com{
6412854Sgabeblack@google.com  private:
6512854Sgabeblack@google.com    /**
6612854Sgabeblack@google.com     * The stream socket to connect to the server.
6712854Sgabeblack@google.com     */
6812854Sgabeblack@google.com    int sock;
6912854Sgabeblack@google.com
7012854Sgabeblack@google.com    /**
7112854Sgabeblack@google.com     * Registry for all sockets to the server opened by this gem5 process.
7212854Sgabeblack@google.com     */
7312854Sgabeblack@google.com    static std::vector<int> sockRegistry;
7413322Sgabeblack@google.com
7512854Sgabeblack@google.com  private:
7612854Sgabeblack@google.com
7712854Sgabeblack@google.com    /**
7812854Sgabeblack@google.com     * Send out a message through a TCP stream socket.
7912854Sgabeblack@google.com     *
8012854Sgabeblack@google.com     * @param sock TCP stream socket.
8112854Sgabeblack@google.com     * @param buf Start address of the message.
8212854Sgabeblack@google.com     * @param length Size of the message in bytes.
8312854Sgabeblack@google.com     */
8412854Sgabeblack@google.com    void
8512854Sgabeblack@google.com    sendTCP(int sock, void *buf, unsigned length);
8612854Sgabeblack@google.com
8712854Sgabeblack@google.com    /**
8812854Sgabeblack@google.com     * Receive the next incoming message through a TCP stream socket.
8912854Sgabeblack@google.com     *
9012854Sgabeblack@google.com     * @param sock TCP stream socket.
9112854Sgabeblack@google.com     * @param buf Start address of buffer to store the message.
9212854Sgabeblack@google.com     * @param length Exact size of the expected message in bytes.
9312854Sgabeblack@google.com     */
9412854Sgabeblack@google.com    bool recvTCP(int sock, void *buf, unsigned length);
9512854Sgabeblack@google.com
9612854Sgabeblack@google.com
9712854Sgabeblack@google.com  protected:
9812854Sgabeblack@google.com
9912854Sgabeblack@google.com    virtual void
10012854Sgabeblack@google.com    sendRaw(void *buf, unsigned length,
10112854Sgabeblack@google.com            const MultiHeaderPkt::AddressType dest_addr=nullptr)
10212854Sgabeblack@google.com        M5_ATTR_OVERRIDE
10312854Sgabeblack@google.com    {
10412854Sgabeblack@google.com        sendTCP(sock, buf, length);
10512854Sgabeblack@google.com    }
10612854Sgabeblack@google.com
10712854Sgabeblack@google.com    virtual bool recvRaw(void *buf, unsigned length) M5_ATTR_OVERRIDE
10812854Sgabeblack@google.com    {
10912854Sgabeblack@google.com        return recvTCP(sock, buf, length);
11012854Sgabeblack@google.com    }
11112854Sgabeblack@google.com
11212854Sgabeblack@google.com    virtual void syncRaw(MultiHeaderPkt::MsgType sync_req,
11312854Sgabeblack@google.com                         Tick sync_tick) M5_ATTR_OVERRIDE;
11412854Sgabeblack@google.com
11512854Sgabeblack@google.com  public:
11612854Sgabeblack@google.com    /**
11712854Sgabeblack@google.com     * The ctor creates and connects the stream socket to the server.
11812854Sgabeblack@google.com     * @param server_name The name (or IP address) of the host running the
11912854Sgabeblack@google.com     * server process.
12012854Sgabeblack@google.com     * @param server_port The port number the server listening for new
12112854Sgabeblack@google.com     * connections.
12212854Sgabeblack@google.com     * @param sync_start The tick for the first multi synchronisation.
12312854Sgabeblack@google.com     * @param sync_repeat The frequency of multi synchronisation.
12412854Sgabeblack@google.com     * @param em The EventManager object associated with the simulated
12512854Sgabeblack@google.com     * Ethernet link.
12612854Sgabeblack@google.com     */
12712854Sgabeblack@google.com    TCPIface(std::string server_name, unsigned server_port,
12812854Sgabeblack@google.com             unsigned multi_rank, Tick sync_start, Tick sync_repeat,
12912854Sgabeblack@google.com             EventManager *em);
13012854Sgabeblack@google.com
13112854Sgabeblack@google.com    ~TCPIface() M5_ATTR_OVERRIDE;
13212854Sgabeblack@google.com};
13312854Sgabeblack@google.com
13412854Sgabeblack@google.com#endif
13512854Sgabeblack@google.com