110923SN/A/*
210923SN/A * Copyright (c) 2015 ARM Limited
310923SN/A * All rights reserved
410923SN/A *
510923SN/A * The license below extends only to copyright in the software and shall
610923SN/A * not be construed as granting a license to any other intellectual
710923SN/A * property including but not limited to intellectual property relating
810923SN/A * to a hardware implementation of the functionality of the software
910923SN/A * licensed hereunder.  You may use the software subject to the license
1010923SN/A * terms below provided that you ensure that this notice is replicated
1110923SN/A * unmodified and in its entirety in all distributions of the software,
1210923SN/A * modified or unmodified, in source code or in binary form.
1310923SN/A *
1410923SN/A * Redistribution and use in source and binary forms, with or without
1510923SN/A * modification, are permitted provided that the following conditions are
1610923SN/A * met: redistributions of source code must retain the above copyright
1710923SN/A * notice, this list of conditions and the following disclaimer;
1810923SN/A * redistributions in binary form must reproduce the above copyright
1910923SN/A * notice, this list of conditions and the following disclaimer in the
2010923SN/A * documentation and/or other materials provided with the distribution;
2110923SN/A * neither the name of the copyright holders nor the names of its
2210923SN/A * contributors may be used to endorse or promote products derived from
2310923SN/A * this software without specific prior written permission.
2410923SN/A *
2510923SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2610923SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2710923SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2810923SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2910923SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3010923SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3110923SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3210923SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3310923SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3410923SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3510923SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3610923SN/A *
3710923SN/A * Authors: Gabor Dozsa
3811290Sgabor.dozsa@arm.com *          Mohammad Alian
3910923SN/A */
4010923SN/A
4110923SN/A/* @file
4211290Sgabor.dozsa@arm.com * TCP stream socket based interface class for dist-gem5 runs.
4310923SN/A *
4411290Sgabor.dozsa@arm.com * For a high level description about dist-gem5 see comments in
4511290Sgabor.dozsa@arm.com * header file dist_iface.hh.
4610923SN/A *
4711290Sgabor.dozsa@arm.com * Each gem5 process connects to the server (another gem5 process which
4811290Sgabor.dozsa@arm.com * simulates a switch box) via a stream socket. The server process
4910923SN/A * transfers messages and co-ordinates the synchronisation among the gem5
5010923SN/A * peers.
5110923SN/A */
5211263Sandreas.sandberg@arm.com#ifndef __DEV_NET_TCP_IFACE_HH__
5311263Sandreas.sandberg@arm.com#define __DEV_NET_TCP_IFACE_HH__
5410923SN/A
5510923SN/A
5610923SN/A#include <string>
5710923SN/A
5811290Sgabor.dozsa@arm.com#include "dev/net/dist_iface.hh"
5910923SN/A
6010923SN/Aclass EventManager;
6110923SN/A
6211290Sgabor.dozsa@arm.comclass TCPIface : public DistIface
6310923SN/A{
6410923SN/A  private:
6510923SN/A    /**
6610923SN/A     * The stream socket to connect to the server.
6710923SN/A     */
6810923SN/A    int sock;
6910923SN/A
7011290Sgabor.dozsa@arm.com    std::string serverName;
7111290Sgabor.dozsa@arm.com    int serverPort;
7211290Sgabor.dozsa@arm.com
7311290Sgabor.dozsa@arm.com    bool isSwitch;
7411290Sgabor.dozsa@arm.com
7511290Sgabor.dozsa@arm.com    bool listening;
7611290Sgabor.dozsa@arm.com    static bool anyListening;
7711290Sgabor.dozsa@arm.com    static int fdStatic;
7811290Sgabor.dozsa@arm.com
7910923SN/A    /**
8011290Sgabor.dozsa@arm.com     * Compute node info and storage for the very first connection from each
8111290Sgabor.dozsa@arm.com     * node (used by the switch)
8211290Sgabor.dozsa@arm.com     */
8311290Sgabor.dozsa@arm.com    struct NodeInfo
8411290Sgabor.dozsa@arm.com    {
8511290Sgabor.dozsa@arm.com        unsigned rank;
8611290Sgabor.dozsa@arm.com        unsigned distIfaceId;
8711290Sgabor.dozsa@arm.com        unsigned distIfaceNum;
8811290Sgabor.dozsa@arm.com    };
8911290Sgabor.dozsa@arm.com    static std::vector<std::pair<NodeInfo, int> > nodes;
9011290Sgabor.dozsa@arm.com    /**
9111290Sgabor.dozsa@arm.com     * Storage for all opened sockets
9210923SN/A     */
9310923SN/A    static std::vector<int> sockRegistry;
9410923SN/A
9510923SN/A  private:
9610923SN/A
9710923SN/A    /**
9810923SN/A     * Send out a message through a TCP stream socket.
9910923SN/A     *
10010923SN/A     * @param sock TCP stream socket.
10110923SN/A     * @param buf Start address of the message.
10210923SN/A     * @param length Size of the message in bytes.
10310923SN/A     */
10410923SN/A    void
10511290Sgabor.dozsa@arm.com    sendTCP(int sock, const void *buf, unsigned length);
10610923SN/A
10710923SN/A    /**
10810923SN/A     * Receive the next incoming message through a TCP stream socket.
10910923SN/A     *
11010923SN/A     * @param sock TCP stream socket.
11110923SN/A     * @param buf Start address of buffer to store the message.
11210923SN/A     * @param length Exact size of the expected message in bytes.
11310923SN/A     */
11410923SN/A    bool recvTCP(int sock, void *buf, unsigned length);
11511290Sgabor.dozsa@arm.com    bool listen(int port);
11611290Sgabor.dozsa@arm.com    void accept();
11711290Sgabor.dozsa@arm.com    void connect();
11811290Sgabor.dozsa@arm.com    int getfdStatic() const { return fdStatic; }
11911290Sgabor.dozsa@arm.com    bool islistening() const { return listening; }
12011290Sgabor.dozsa@arm.com    bool anyislistening() const { return anyListening; }
12111290Sgabor.dozsa@arm.com    void establishConnection();
12210923SN/A
12310923SN/A  protected:
12410923SN/A
12511290Sgabor.dozsa@arm.com    void sendPacket(const Header &header,
12611290Sgabor.dozsa@arm.com                    const EthPacketPtr &packet) override;
12710923SN/A
12811290Sgabor.dozsa@arm.com    void sendCmd(const Header &header) override;
12910923SN/A
13011290Sgabor.dozsa@arm.com    bool recvHeader(Header &header) override;
13111290Sgabor.dozsa@arm.com
13211290Sgabor.dozsa@arm.com    void recvPacket(const Header &header, EthPacketPtr &packet) override;
13311290Sgabor.dozsa@arm.com
13411290Sgabor.dozsa@arm.com    void initTransport() override;
13510923SN/A
13610923SN/A  public:
13710923SN/A    /**
13810923SN/A     * The ctor creates and connects the stream socket to the server.
13910923SN/A     * @param server_name The name (or IP address) of the host running the
14010923SN/A     * server process.
14110923SN/A     * @param server_port The port number the server listening for new
14210923SN/A     * connections.
14311290Sgabor.dozsa@arm.com     * @param sync_start The tick for the first dist synchronisation.
14411290Sgabor.dozsa@arm.com     * @param sync_repeat The frequency of dist synchronisation.
14510923SN/A     * @param em The EventManager object associated with the simulated
14610923SN/A     * Ethernet link.
14710923SN/A     */
14810923SN/A    TCPIface(std::string server_name, unsigned server_port,
14911290Sgabor.dozsa@arm.com             unsigned dist_rank, unsigned dist_size,
15011290Sgabor.dozsa@arm.com             Tick sync_start, Tick sync_repeat, EventManager *em,
15111703Smichael.lebeane@amd.com             bool use_pseudo_op, bool is_switch, int num_nodes);
15210923SN/A
15311168SN/A    ~TCPIface() override;
15410923SN/A};
15510923SN/A
15611263Sandreas.sandberg@arm.com#endif // __DEV_NET_TCP_IFACE_HH__
157