blob: 96e8e24a2c74c885e0e7194ae50d54db624cf1d2 [file] [log] [blame]
Finn Williams2ed809c2020-04-20 21:21:07 +01001//
Jim Flynnbbfe6032020-07-20 16:57:44 +01002// Copyright © 2020 Arm Ltd and Contributors. All rights reserved.
Finn Williams2ed809c2020-04-20 21:21:07 +01003// SPDX-License-Identifier: MIT
4//
5
Jim Flynnbbfe6032020-07-20 16:57:44 +01006#include <server/include/basePipeServer/BasePipeServer.hpp>
Finn Williams9937f932020-04-29 12:00:24 +01007
Jim Flynnbbfe6032020-07-20 16:57:44 +01008#include <common/include/Constants.hpp>
9#include <common/include/NumericCast.hpp>
Finn Williamse09fc822020-04-29 13:17:30 +010010
Finn Williams2ed809c2020-04-20 21:21:07 +010011#include <iostream>
Finn Williams2ed809c2020-04-20 21:21:07 +010012#include <vector>
13#include <iomanip>
Finn Williamsbadcc3f2020-05-22 14:28:15 +010014#include <string.h>
Finn Williams2ed809c2020-04-20 21:21:07 +010015
Jim Flynnbbfe6032020-07-20 16:57:44 +010016namespace arm
17{
Finn Williams2ed809c2020-04-20 21:21:07 +010018
Jim Flynnbbfe6032020-07-20 16:57:44 +010019namespace pipe
Finn Williams2ed809c2020-04-20 21:21:07 +010020{
21
22bool BasePipeServer::ReadFromSocket(uint8_t* packetData, uint32_t expectedLength)
23{
24 // This is a blocking read until either expectedLength has been received or an error is detected.
25 long totalBytesRead = 0;
Jim Flynnbbfe6032020-07-20 16:57:44 +010026 while (arm::pipe::numeric_cast<uint32_t>(totalBytesRead) < expectedLength)
Finn Williams2ed809c2020-04-20 21:21:07 +010027 {
Jim Flynnbbfe6032020-07-20 16:57:44 +010028 long bytesRead = arm::pipe::Read(m_ClientConnection, packetData, expectedLength);
Finn Williams2ed809c2020-04-20 21:21:07 +010029 if (bytesRead < 0)
30 {
31 std::cerr << ": Failure when reading from client socket: " << strerror(errno) << std::endl;
32 return false;
33 }
34 if (bytesRead == 0)
35 {
36 std::cerr << ": EOF while reading from client socket." << std::endl;
37 return false;
38 }
39 totalBytesRead += bytesRead;
40 }
41 return true;
42};
43
44bool BasePipeServer::WaitForStreamMetaData()
45{
46 if (m_EchoPackets)
47 {
48 std::cout << "Waiting for stream meta data..." << std::endl;
49 }
50 // The start of the stream metadata is 2x32bit words, 0 and packet length.
51 uint8_t header[8];
52 if (!ReadFromSocket(header, 8))
53 {
54 return false;
55 }
56 EchoPacket(PacketDirection::ReceivedHeader, header, 8);
57 // The first word, stream_metadata_identifer, should always be 0.
58 if (ToUint32(&header[0], TargetEndianness::BeWire) != 0)
59 {
60 std::cerr << ": Protocol error. The stream_metadata_identifer was not 0." << std::endl;
61 return false;
62 }
63
64 uint8_t pipeMagic[4];
65 if (!ReadFromSocket(pipeMagic, 4))
66 {
67 return false;
68 }
69 EchoPacket(PacketDirection::ReceivedData, pipeMagic, 4);
70
71 // Before we interpret the length we need to read the pipe_magic word to determine endianness.
Jim Flynnbbfe6032020-07-20 16:57:44 +010072 if (ToUint32(&pipeMagic[0], TargetEndianness::BeWire) == PIPE_MAGIC)
Finn Williams2ed809c2020-04-20 21:21:07 +010073 {
74 m_Endianness = TargetEndianness::BeWire;
75 }
Jim Flynnbbfe6032020-07-20 16:57:44 +010076 else if (ToUint32(&pipeMagic[0], TargetEndianness::LeWire) == PIPE_MAGIC)
Finn Williams2ed809c2020-04-20 21:21:07 +010077 {
78 m_Endianness = TargetEndianness::LeWire;
79 }
80 else
81 {
Jim Flynnbbfe6032020-07-20 16:57:44 +010082 std::cerr << ": Protocol read error. Unable to read the PIPE_MAGIC value." << std::endl;
Finn Williams2ed809c2020-04-20 21:21:07 +010083 return false;
84 }
85 // Now we know the endianness we can get the length from the header.
86 // Remember we already read the pipe magic 4 bytes.
87 uint32_t metaDataLength = ToUint32(&header[4], m_Endianness) - 4;
88 // Read the entire packet.
89 std::vector<uint8_t> packetData(metaDataLength);
90 if (metaDataLength !=
Jim Flynnbbfe6032020-07-20 16:57:44 +010091 arm::pipe::numeric_cast<uint32_t>(arm::pipe::Read(m_ClientConnection, packetData.data(), metaDataLength)))
Finn Williams2ed809c2020-04-20 21:21:07 +010092 {
93 std::cerr << ": Protocol read error. Data length mismatch." << std::endl;
94 return false;
95 }
96 EchoPacket(PacketDirection::ReceivedData, packetData.data(), metaDataLength);
97 m_StreamMetaDataVersion = ToUint32(&packetData[0], m_Endianness);
98 m_StreamMetaDataMaxDataLen = ToUint32(&packetData[4], m_Endianness);
99 m_StreamMetaDataPid = ToUint32(&packetData[8], m_Endianness);
100
101 return true;
102}
103
Jim Flynnbbfe6032020-07-20 16:57:44 +0100104arm::pipe::Packet BasePipeServer::WaitForPacket(uint32_t timeoutMs)
Finn Williams2ed809c2020-04-20 21:21:07 +0100105{
106 // Is there currently more than a headers worth of data waiting to be read?
107 int bytes_available;
Jim Flynnbbfe6032020-07-20 16:57:44 +0100108 arm::pipe::Ioctl(m_ClientConnection, FIONREAD, &bytes_available);
Finn Williams2ed809c2020-04-20 21:21:07 +0100109 if (bytes_available > 8)
110 {
111 // Yes there is. Read it:
112 return ReceivePacket();
113 }
114 else
115 {
116 // No there's not. Poll for more data.
117 struct pollfd pollingFd[1]{};
118 pollingFd[0].fd = m_ClientConnection;
Jim Flynnbbfe6032020-07-20 16:57:44 +0100119 int pollResult = arm::pipe::Poll(pollingFd, 1, static_cast<int>(timeoutMs));
Finn Williams2ed809c2020-04-20 21:21:07 +0100120
121 switch (pollResult)
122 {
123 // Error
124 case -1:
Jim Flynnbbfe6032020-07-20 16:57:44 +0100125 throw ProfilingException(std::string("File descriptor reported an error during polling: ") +
126 strerror(errno));
Finn Williams2ed809c2020-04-20 21:21:07 +0100127
128 // Timeout
129 case 0:
Jim Flynnbbfe6032020-07-20 16:57:44 +0100130 throw arm::pipe::TimeoutException("Timeout while waiting to receive packet.");
Finn Williams2ed809c2020-04-20 21:21:07 +0100131
132 // Normal poll return. It could still contain an error signal
133 default:
134 // Check if the socket reported an error
135 if (pollingFd[0].revents & (POLLNVAL | POLLERR | POLLHUP))
136 {
137 if (pollingFd[0].revents == POLLNVAL)
138 {
Jim Flynnbbfe6032020-07-20 16:57:44 +0100139 throw arm::pipe::ProfilingException(
140 std::string("Error while polling receiving socket: POLLNVAL"));
Finn Williams2ed809c2020-04-20 21:21:07 +0100141 }
142 if (pollingFd[0].revents == POLLERR)
143 {
Jim Flynnbbfe6032020-07-20 16:57:44 +0100144 throw arm::pipe::ProfilingException(
145 std::string("Error while polling receiving socket: POLLERR: ") + strerror(errno));
Finn Williams2ed809c2020-04-20 21:21:07 +0100146 }
147 if (pollingFd[0].revents == POLLHUP)
148 {
Jim Flynnbbfe6032020-07-20 16:57:44 +0100149 throw arm::pipe::ProfilingException(
150 std::string("Connection closed by remote client: POLLHUP"));
Finn Williams2ed809c2020-04-20 21:21:07 +0100151 }
152 }
153
154 // Check if there is data to read
155 if (!(pollingFd[0].revents & (POLLIN)))
156 {
157 // This is a corner case. The socket as been woken up but not with any data.
158 // We'll throw a timeout exception to loop around again.
Jim Flynnbbfe6032020-07-20 16:57:44 +0100159 throw arm::pipe::TimeoutException(
160 "File descriptor was polled but no data was available to receive.");
Finn Williams2ed809c2020-04-20 21:21:07 +0100161 }
162 return ReceivePacket();
163 }
164 }
165}
166
Jim Flynnbbfe6032020-07-20 16:57:44 +0100167arm::pipe::Packet BasePipeServer::ReceivePacket()
Finn Williams2ed809c2020-04-20 21:21:07 +0100168{
169 uint32_t header[2];
170 if (!ReadHeader(header))
171 {
Jim Flynnbbfe6032020-07-20 16:57:44 +0100172 return arm::pipe::Packet();
Finn Williams2ed809c2020-04-20 21:21:07 +0100173 }
174 // Read data_length bytes from the socket.
175 std::unique_ptr<unsigned char[]> uniquePacketData = std::make_unique<unsigned char[]>(header[1]);
176 unsigned char* packetData = reinterpret_cast<unsigned char*>(uniquePacketData.get());
177
178 if (!ReadFromSocket(packetData, header[1]))
179 {
Jim Flynnbbfe6032020-07-20 16:57:44 +0100180 return arm::pipe::Packet();
Finn Williams2ed809c2020-04-20 21:21:07 +0100181 }
182
183 EchoPacket(PacketDirection::ReceivedData, packetData, header[1]);
184
185 // Construct received packet
Jim Flynnbbfe6032020-07-20 16:57:44 +0100186 arm::pipe::Packet packetRx = arm::pipe::Packet(header[0], header[1], uniquePacketData);
Finn Williams2ed809c2020-04-20 21:21:07 +0100187 if (m_EchoPackets)
188 {
189 std::cout << "Processing packet ID= " << packetRx.GetPacketId() << " Length=" << packetRx.GetLength()
190 << std::endl;
191 }
192
193 return packetRx;
194}
195
196bool BasePipeServer::SendPacket(uint32_t packetFamily, uint32_t packetId, const uint8_t* data, uint32_t dataLength)
197{
198 // Construct a packet from the id and data given and send it to the client.
199 // Encode the header.
200 uint32_t header[2];
201 header[0] = packetFamily << 26 | packetId << 16;
202 header[1] = dataLength;
203 // Add the header to the packet.
204 std::vector<uint8_t> packet(8 + dataLength);
205 InsertU32(header[0], packet.data(), m_Endianness);
206 InsertU32(header[1], packet.data() + 4, m_Endianness);
207 // And the rest of the data if there is any.
208 if (dataLength > 0)
209 {
David Monahan6a1d5062023-08-29 09:10:50 +0100210 if (data == nullptr)
211 {
212 throw ProfilingException(
213 "basePipeServer: SendPacket: Attempting to send a non-zero length data packet with a null data pointer"
214 );
215 }
Finn Williams2ed809c2020-04-20 21:21:07 +0100216 memcpy((packet.data() + 8), data, dataLength);
217 }
218 EchoPacket(PacketDirection::Sending, packet.data(), packet.size());
Jim Flynnbbfe6032020-07-20 16:57:44 +0100219 if (-1 == arm::pipe::Write(m_ClientConnection, packet.data(), packet.size()))
Finn Williams2ed809c2020-04-20 21:21:07 +0100220 {
221 std::cerr << ": Failure when writing to client socket: " << strerror(errno) << std::endl;
222 return false;
223 }
224 return true;
225}
226
227bool BasePipeServer::ReadHeader(uint32_t headerAsWords[2])
228{
229 // The header will always be 2x32bit words.
230 uint8_t header[8];
231 if (!ReadFromSocket(header, 8))
232 {
233 return false;
234 }
235 EchoPacket(PacketDirection::ReceivedHeader, header, 8);
236 headerAsWords[0] = ToUint32(&header[0], m_Endianness);
237 headerAsWords[1] = ToUint32(&header[4], m_Endianness);
238 return true;
239}
240
241void BasePipeServer::EchoPacket(PacketDirection direction, uint8_t* packet, size_t lengthInBytes)
242{
243 // If enabled print the contents of the data packet to the console.
244 if (m_EchoPackets)
245 {
246 if (direction == PacketDirection::Sending)
247 {
248 std::cout << "TX " << std::dec << lengthInBytes << " bytes : ";
249 }
250 else if (direction == PacketDirection::ReceivedHeader)
251 {
252 std::cout << "RX Header " << std::dec << lengthInBytes << " bytes : ";
253 }
254 else
255 {
256 std::cout << "RX Data " << std::dec << lengthInBytes << " bytes : ";
257 }
258 for (unsigned int i = 0; i < lengthInBytes; i++)
259 {
260 if ((i % 10) == 0)
261 {
262 std::cout << std::endl;
263 }
264 std::cout << "0x" << std::setfill('0') << std::setw(2) << std::hex << static_cast<unsigned int>(packet[i])
265 << " ";
266 }
267 std::cout << std::endl;
268 }
269}
270
271uint32_t BasePipeServer::ToUint32(uint8_t* data, TargetEndianness endianness)
272{
273 // Extract the first 4 bytes starting at data and push them into a 32bit integer based on the
274 // specified endianness.
275 if (endianness == TargetEndianness::BeWire)
276 {
277 return static_cast<uint32_t>(data[0]) << 24 | static_cast<uint32_t>(data[1]) << 16 |
278 static_cast<uint32_t>(data[2]) << 8 | static_cast<uint32_t>(data[3]);
279 }
280 else
281 {
282 return static_cast<uint32_t>(data[3]) << 24 | static_cast<uint32_t>(data[2]) << 16 |
283 static_cast<uint32_t>(data[1]) << 8 | static_cast<uint32_t>(data[0]);
284 }
285}
286
287void BasePipeServer::InsertU32(uint32_t value, uint8_t* data, TargetEndianness endianness)
288{
289 // Take the bytes of a 32bit integer and copy them into char array starting at data considering
290 // the endianness value.
291 if (endianness == TargetEndianness::BeWire)
292 {
293 *data = static_cast<uint8_t>((value >> 24) & 0xFF);
294 *(data + 1) = static_cast<uint8_t>((value >> 16) & 0xFF);
295 *(data + 2) = static_cast<uint8_t>((value >> 8) & 0xFF);
296 *(data + 3) = static_cast<uint8_t>(value & 0xFF);
297 }
298 else
299 {
300 *(data + 3) = static_cast<uint8_t>((value >> 24) & 0xFF);
301 *(data + 2) = static_cast<uint8_t>((value >> 16) & 0xFF);
302 *(data + 1) = static_cast<uint8_t>((value >> 8) & 0xFF);
303 *data = static_cast<uint8_t>(value & 0xFF);
304 }
305}
306
Jim Flynnbbfe6032020-07-20 16:57:44 +0100307} // namespace pipe
308} // namespace arm