blob: 08d9126fb9f4ee20d9b6941982927d635224bb96 [file] [log] [blame]
Finn Williams2ed809c2020-04-20 21:21:07 +01001//
Colm Donelan73036eb2024-02-12 17:05:52 +00002// Copyright © 2020, 2024 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>
Colm Donelan73036eb2024-02-12 17:05:52 +000014#include <string>
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);
Colm Donelan73036eb2024-02-12 17:05:52 +000090 long bytesRead = arm::pipe::Read(m_ClientConnection, packetData.data(), metaDataLength);
91 // On Socket error Read will return -1.
92 if (bytesRead < 0)
93 {
94 std::cerr << ": Socket error: " << strerror(errno) << std::endl;
95 return false;
96 }
97 // bytesRead cannot be negative here.
98 if (metaDataLength != arm::pipe::numeric_cast<uint32_t>(bytesRead))
Finn Williams2ed809c2020-04-20 21:21:07 +010099 {
100 std::cerr << ": Protocol read error. Data length mismatch." << std::endl;
101 return false;
102 }
103 EchoPacket(PacketDirection::ReceivedData, packetData.data(), metaDataLength);
104 m_StreamMetaDataVersion = ToUint32(&packetData[0], m_Endianness);
105 m_StreamMetaDataMaxDataLen = ToUint32(&packetData[4], m_Endianness);
106 m_StreamMetaDataPid = ToUint32(&packetData[8], m_Endianness);
107
108 return true;
109}
110
Jim Flynnbbfe6032020-07-20 16:57:44 +0100111arm::pipe::Packet BasePipeServer::WaitForPacket(uint32_t timeoutMs)
Finn Williams2ed809c2020-04-20 21:21:07 +0100112{
113 // Is there currently more than a headers worth of data waiting to be read?
114 int bytes_available;
Jim Flynnbbfe6032020-07-20 16:57:44 +0100115 arm::pipe::Ioctl(m_ClientConnection, FIONREAD, &bytes_available);
Finn Williams2ed809c2020-04-20 21:21:07 +0100116 if (bytes_available > 8)
117 {
118 // Yes there is. Read it:
119 return ReceivePacket();
120 }
121 else
122 {
123 // No there's not. Poll for more data.
124 struct pollfd pollingFd[1]{};
125 pollingFd[0].fd = m_ClientConnection;
Jim Flynnbbfe6032020-07-20 16:57:44 +0100126 int pollResult = arm::pipe::Poll(pollingFd, 1, static_cast<int>(timeoutMs));
Finn Williams2ed809c2020-04-20 21:21:07 +0100127
128 switch (pollResult)
129 {
130 // Error
131 case -1:
Jim Flynnbbfe6032020-07-20 16:57:44 +0100132 throw ProfilingException(std::string("File descriptor reported an error during polling: ") +
133 strerror(errno));
Finn Williams2ed809c2020-04-20 21:21:07 +0100134
135 // Timeout
136 case 0:
Jim Flynnbbfe6032020-07-20 16:57:44 +0100137 throw arm::pipe::TimeoutException("Timeout while waiting to receive packet.");
Finn Williams2ed809c2020-04-20 21:21:07 +0100138
139 // Normal poll return. It could still contain an error signal
140 default:
141 // Check if the socket reported an error
142 if (pollingFd[0].revents & (POLLNVAL | POLLERR | POLLHUP))
143 {
144 if (pollingFd[0].revents == POLLNVAL)
145 {
Jim Flynnbbfe6032020-07-20 16:57:44 +0100146 throw arm::pipe::ProfilingException(
147 std::string("Error while polling receiving socket: POLLNVAL"));
Finn Williams2ed809c2020-04-20 21:21:07 +0100148 }
149 if (pollingFd[0].revents == POLLERR)
150 {
Jim Flynnbbfe6032020-07-20 16:57:44 +0100151 throw arm::pipe::ProfilingException(
152 std::string("Error while polling receiving socket: POLLERR: ") + strerror(errno));
Finn Williams2ed809c2020-04-20 21:21:07 +0100153 }
154 if (pollingFd[0].revents == POLLHUP)
155 {
Jim Flynnbbfe6032020-07-20 16:57:44 +0100156 throw arm::pipe::ProfilingException(
157 std::string("Connection closed by remote client: POLLHUP"));
Finn Williams2ed809c2020-04-20 21:21:07 +0100158 }
159 }
160
161 // Check if there is data to read
162 if (!(pollingFd[0].revents & (POLLIN)))
163 {
164 // This is a corner case. The socket as been woken up but not with any data.
165 // We'll throw a timeout exception to loop around again.
Jim Flynnbbfe6032020-07-20 16:57:44 +0100166 throw arm::pipe::TimeoutException(
167 "File descriptor was polled but no data was available to receive.");
Finn Williams2ed809c2020-04-20 21:21:07 +0100168 }
169 return ReceivePacket();
170 }
171 }
172}
173
Jim Flynnbbfe6032020-07-20 16:57:44 +0100174arm::pipe::Packet BasePipeServer::ReceivePacket()
Finn Williams2ed809c2020-04-20 21:21:07 +0100175{
176 uint32_t header[2];
177 if (!ReadHeader(header))
178 {
Jim Flynnbbfe6032020-07-20 16:57:44 +0100179 return arm::pipe::Packet();
Finn Williams2ed809c2020-04-20 21:21:07 +0100180 }
181 // Read data_length bytes from the socket.
182 std::unique_ptr<unsigned char[]> uniquePacketData = std::make_unique<unsigned char[]>(header[1]);
183 unsigned char* packetData = reinterpret_cast<unsigned char*>(uniquePacketData.get());
184
185 if (!ReadFromSocket(packetData, header[1]))
186 {
Jim Flynnbbfe6032020-07-20 16:57:44 +0100187 return arm::pipe::Packet();
Finn Williams2ed809c2020-04-20 21:21:07 +0100188 }
189
190 EchoPacket(PacketDirection::ReceivedData, packetData, header[1]);
191
192 // Construct received packet
Jim Flynnbbfe6032020-07-20 16:57:44 +0100193 arm::pipe::Packet packetRx = arm::pipe::Packet(header[0], header[1], uniquePacketData);
Finn Williams2ed809c2020-04-20 21:21:07 +0100194 if (m_EchoPackets)
195 {
196 std::cout << "Processing packet ID= " << packetRx.GetPacketId() << " Length=" << packetRx.GetLength()
197 << std::endl;
198 }
199
200 return packetRx;
201}
202
203bool BasePipeServer::SendPacket(uint32_t packetFamily, uint32_t packetId, const uint8_t* data, uint32_t dataLength)
204{
205 // Construct a packet from the id and data given and send it to the client.
206 // Encode the header.
207 uint32_t header[2];
208 header[0] = packetFamily << 26 | packetId << 16;
209 header[1] = dataLength;
210 // Add the header to the packet.
211 std::vector<uint8_t> packet(8 + dataLength);
212 InsertU32(header[0], packet.data(), m_Endianness);
213 InsertU32(header[1], packet.data() + 4, m_Endianness);
214 // And the rest of the data if there is any.
215 if (dataLength > 0)
216 {
David Monahan6a1d5062023-08-29 09:10:50 +0100217 if (data == nullptr)
218 {
219 throw ProfilingException(
220 "basePipeServer: SendPacket: Attempting to send a non-zero length data packet with a null data pointer"
221 );
222 }
Finn Williams2ed809c2020-04-20 21:21:07 +0100223 memcpy((packet.data() + 8), data, dataLength);
224 }
225 EchoPacket(PacketDirection::Sending, packet.data(), packet.size());
Jim Flynnbbfe6032020-07-20 16:57:44 +0100226 if (-1 == arm::pipe::Write(m_ClientConnection, packet.data(), packet.size()))
Finn Williams2ed809c2020-04-20 21:21:07 +0100227 {
228 std::cerr << ": Failure when writing to client socket: " << strerror(errno) << std::endl;
229 return false;
230 }
231 return true;
232}
233
234bool BasePipeServer::ReadHeader(uint32_t headerAsWords[2])
235{
236 // The header will always be 2x32bit words.
237 uint8_t header[8];
238 if (!ReadFromSocket(header, 8))
239 {
240 return false;
241 }
242 EchoPacket(PacketDirection::ReceivedHeader, header, 8);
243 headerAsWords[0] = ToUint32(&header[0], m_Endianness);
244 headerAsWords[1] = ToUint32(&header[4], m_Endianness);
245 return true;
246}
247
248void BasePipeServer::EchoPacket(PacketDirection direction, uint8_t* packet, size_t lengthInBytes)
249{
250 // If enabled print the contents of the data packet to the console.
251 if (m_EchoPackets)
252 {
253 if (direction == PacketDirection::Sending)
254 {
255 std::cout << "TX " << std::dec << lengthInBytes << " bytes : ";
256 }
257 else if (direction == PacketDirection::ReceivedHeader)
258 {
259 std::cout << "RX Header " << std::dec << lengthInBytes << " bytes : ";
260 }
261 else
262 {
263 std::cout << "RX Data " << std::dec << lengthInBytes << " bytes : ";
264 }
265 for (unsigned int i = 0; i < lengthInBytes; i++)
266 {
267 if ((i % 10) == 0)
268 {
269 std::cout << std::endl;
270 }
271 std::cout << "0x" << std::setfill('0') << std::setw(2) << std::hex << static_cast<unsigned int>(packet[i])
272 << " ";
273 }
274 std::cout << std::endl;
275 }
276}
277
278uint32_t BasePipeServer::ToUint32(uint8_t* data, TargetEndianness endianness)
279{
280 // Extract the first 4 bytes starting at data and push them into a 32bit integer based on the
281 // specified endianness.
282 if (endianness == TargetEndianness::BeWire)
283 {
284 return static_cast<uint32_t>(data[0]) << 24 | static_cast<uint32_t>(data[1]) << 16 |
285 static_cast<uint32_t>(data[2]) << 8 | static_cast<uint32_t>(data[3]);
286 }
287 else
288 {
289 return static_cast<uint32_t>(data[3]) << 24 | static_cast<uint32_t>(data[2]) << 16 |
290 static_cast<uint32_t>(data[1]) << 8 | static_cast<uint32_t>(data[0]);
291 }
292}
293
294void BasePipeServer::InsertU32(uint32_t value, uint8_t* data, TargetEndianness endianness)
295{
296 // Take the bytes of a 32bit integer and copy them into char array starting at data considering
297 // the endianness value.
298 if (endianness == TargetEndianness::BeWire)
299 {
300 *data = static_cast<uint8_t>((value >> 24) & 0xFF);
301 *(data + 1) = static_cast<uint8_t>((value >> 16) & 0xFF);
302 *(data + 2) = static_cast<uint8_t>((value >> 8) & 0xFF);
303 *(data + 3) = static_cast<uint8_t>(value & 0xFF);
304 }
305 else
306 {
307 *(data + 3) = static_cast<uint8_t>((value >> 24) & 0xFF);
308 *(data + 2) = static_cast<uint8_t>((value >> 16) & 0xFF);
309 *(data + 1) = static_cast<uint8_t>((value >> 8) & 0xFF);
310 *data = static_cast<uint8_t>(value & 0xFF);
311 }
312}
313
Jim Flynnbbfe6032020-07-20 16:57:44 +0100314} // namespace pipe
315} // namespace arm