dev: Delete the authors list from files in src/dev.
[gem5.git] / src / dev / net / tcp_iface.hh
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
38 /* @file
39 * TCP stream socket based interface class for dist-gem5 runs.
40 *
41 * For a high level description about dist-gem5 see comments in
42 * header file dist_iface.hh.
43 *
44 * Each gem5 process connects to the server (another gem5 process which
45 * simulates a switch box) via a stream socket. The server process
46 * transfers messages and co-ordinates the synchronisation among the gem5
47 * peers.
48 */
49 #ifndef __DEV_NET_TCP_IFACE_HH__
50 #define __DEV_NET_TCP_IFACE_HH__
51
52
53 #include <string>
54
55 #include "dev/net/dist_iface.hh"
56
57 class EventManager;
58
59 class TCPIface : public DistIface
60 {
61 private:
62 /**
63 * The stream socket to connect to the server.
64 */
65 int sock;
66
67 std::string serverName;
68 int serverPort;
69
70 bool isSwitch;
71
72 bool listening;
73 static bool anyListening;
74 static int fdStatic;
75
76 /**
77 * Compute node info and storage for the very first connection from each
78 * node (used by the switch)
79 */
80 struct NodeInfo
81 {
82 unsigned rank;
83 unsigned distIfaceId;
84 unsigned distIfaceNum;
85 };
86 static std::vector<std::pair<NodeInfo, int> > nodes;
87 /**
88 * Storage for all opened sockets
89 */
90 static std::vector<int> sockRegistry;
91
92 private:
93
94 /**
95 * Send out a message through a TCP stream socket.
96 *
97 * @param sock TCP stream socket.
98 * @param buf Start address of the message.
99 * @param length Size of the message in bytes.
100 */
101 void
102 sendTCP(int sock, const void *buf, unsigned length);
103
104 /**
105 * Receive the next incoming message through a TCP stream socket.
106 *
107 * @param sock TCP stream socket.
108 * @param buf Start address of buffer to store the message.
109 * @param length Exact size of the expected message in bytes.
110 */
111 bool recvTCP(int sock, void *buf, unsigned length);
112 bool listen(int port);
113 void accept();
114 void connect();
115 int getfdStatic() const { return fdStatic; }
116 bool islistening() const { return listening; }
117 bool anyislistening() const { return anyListening; }
118 void establishConnection();
119
120 protected:
121
122 void sendPacket(const Header &header,
123 const EthPacketPtr &packet) override;
124
125 void sendCmd(const Header &header) override;
126
127 bool recvHeader(Header &header) override;
128
129 void recvPacket(const Header &header, EthPacketPtr &packet) override;
130
131 void initTransport() override;
132
133 public:
134 /**
135 * The ctor creates and connects the stream socket to the server.
136 * @param server_name The name (or IP address) of the host running the
137 * server process.
138 * @param server_port The port number the server listening for new
139 * connections.
140 * @param sync_start The tick for the first dist synchronisation.
141 * @param sync_repeat The frequency of dist synchronisation.
142 * @param em The EventManager object associated with the simulated
143 * Ethernet link.
144 */
145 TCPIface(std::string server_name, unsigned server_port,
146 unsigned dist_rank, unsigned dist_size,
147 Tick sync_start, Tick sync_repeat, EventManager *em,
148 bool use_pseudo_op, bool is_switch, int num_nodes);
149
150 ~TCPIface() override;
151 };
152
153 #endif // __DEV_NET_TCP_IFACE_HH__