cloudFPGA (cF) API  1.0
The documentation of the source code of cloudFPGA (cF)
nts_utils.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2016 -- 2021 IBM Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
31 #include "nts_utils.hpp"
32 
33 using namespace std;
34 
35 
38 #define THIS_NAME "NtsUtils"
39 
40 
46 void printAxisRaw(const char *callerName, AxisRaw chunk) {
47  printInfo(callerName, "AxisRaw = {D=0x%16.16lX, K=0x%2.2X, L=%d} \n",
48  chunk.getLE_TData().to_ulong(), chunk.getLE_TKeep().to_int(), chunk.getLE_TLast().to_int());
49 }
50 
51 
58 void printAxisRaw(const char *callerName, const char *message, AxisRaw chunk) {
59  printInfo(callerName, "%s {D=0x%16.16lX, K=0x%2.2X, L=%d} \n", \
60  message, chunk.getLE_TData().to_ulong(), chunk.getLE_TKeep().to_int(), chunk.getLE_TLast().to_int());
61 }
62 
63 
69 void printDmCmd(const char *callerName, DmCmd dmCmd) {
70  printInfo(callerName, "DmCmd = {BTT=0x%6.6X, TYPE=0x%1.1X DSA=0x%2.2X, EOF=0x%1.1X, DRR=0x%1.1X, SADDR=0x%8.8X, TAG=0x%1.1X} \n", \
71  dmCmd.btt.to_uint(), dmCmd.type.to_uint(), dmCmd.dsa.to_uint(), dmCmd.eof.to_uint(), dmCmd.drr.to_uint(), dmCmd.saddr.to_uint(), dmCmd.tag.to_uint());
72 }
73 
74 
80 void printArpBindPair (const char *callerName, ArpBindPair arpBind) {
81  printInfo(callerName, "ArpBind {MAC,IP4} = {0x%12.12lx,0x%8.8lx} = {%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X,%3.3d.%3.3d.%3.3d.%3.3d}\n",
82  arpBind.macAddr.to_ulong(),
83  arpBind.ip4Addr.to_ulong(),
84  (unsigned char)((arpBind.macAddr.to_ulong() & 0xFF0000000000) >> 40),
85  (unsigned char)((arpBind.macAddr.to_ulong() & 0x00FF00000000) >> 32),
86  (unsigned char)((arpBind.macAddr.to_ulong() & 0x0000FF000000) >> 24),
87  (unsigned char)((arpBind.macAddr.to_ulong() & 0x000000FF0000) >> 16),
88  (unsigned char)((arpBind.macAddr.to_ulong() & 0x00000000FF00) >> 8),
89  (unsigned char)((arpBind.macAddr.to_ulong() & 0x0000000000FF) >> 0),
90  (unsigned char)((arpBind.ip4Addr.to_ulong() & 0xFF000000) >> 24),
91  (unsigned char)((arpBind.ip4Addr.to_ulong() & 0x00FF0000) >> 16),
92  (unsigned char)((arpBind.ip4Addr.to_ulong() & 0x0000FF00) >> 8),
93  (unsigned char)((arpBind.ip4Addr.to_ulong() & 0x000000FF) >> 0));
94 }
95 
96 
102 void printLE_SockPair(const char *callerName, LE_SocketPair sockPair) {
103  printInfo(callerName, "LE_SocketPair {Src,Dst} = {{0x%8.8X:0x%4.4X} {0x%8.8X:0x%4.4X}} \n", \
104  sockPair.src.addr.to_uint(), sockPair.src.port.to_uint(), \
105  sockPair.dst.addr.to_uint(), sockPair.dst.port.to_uint());
106 }
107 
108 
114 void printSockPair(const char *callerName, SocketPair sockPair) {
115  printInfo(callerName, "SocketPair {Src,Dst} = {0x%8.8X:0x%4.4X, 0x%8.8X:0x%4.4X} = {%3.3d.%3.3d.%3.3d.%3.3d:%5.5d, %3.3d.%3.3d.%3.3d.%3.3d:%5.5d}\n", \
116  sockPair.src.addr.to_uint(),
117  sockPair.src.port.to_uint(),
118  sockPair.dst.addr.to_uint(),
119  sockPair.dst.port.to_uint(),
120  (sockPair.src.addr.to_uint() & 0xFF000000) >> 24,
121  (sockPair.src.addr.to_uint() & 0x00FF0000) >> 16,
122  (sockPair.src.addr.to_uint() & 0x0000FF00) >> 8,
123  (sockPair.src.addr.to_uint() & 0x000000FF) >> 0,
124  sockPair.src.port.to_uint(),
125  (sockPair.dst.addr.to_uint() & 0xFF000000) >> 24,
126  (sockPair.dst.addr.to_uint() & 0x00FF0000) >> 16,
127  (sockPair.dst.addr.to_uint() & 0x0000FF00) >> 8,
128  (sockPair.dst.addr.to_uint() & 0x000000FF) >> 0,
129  sockPair.dst.port.to_uint());
130 }
131 
132 
138 void printSockPair(const char *callerName, LE_SocketPair leSockPair) {
139  printInfo(callerName, "SocketPair {Src,Dst} = {0x%8.8X:0x%4.4X, 0x%8.8X:0x%4.4X} = {%3.3d.%3.3d.%3.3d.%3.3d:%5.5d, %3.3d.%3.3d.%3.3d.%3.3d:%5.5d}\n", \
140  byteSwap32(leSockPair.src.addr).to_uint(),
141  byteSwap16(leSockPair.src.port).to_uint(),
142  byteSwap32(leSockPair.dst.addr).to_uint(),
143  byteSwap16(leSockPair.dst.port).to_uint(),
144  (byteSwap32(leSockPair.src.addr).to_uint() & 0xFF000000) >> 24,
145  (byteSwap32(leSockPair.src.addr).to_uint() & 0x00FF0000) >> 16,
146  (byteSwap32(leSockPair.src.addr).to_uint() & 0x0000FF00) >> 8,
147  (byteSwap32(leSockPair.src.addr).to_uint() & 0x000000FF) >> 0,
148  byteSwap16(leSockPair.src.port).to_uint(),
149  (byteSwap32(leSockPair.dst.addr).to_uint() & 0xFF000000) >> 24,
150  (byteSwap32(leSockPair.dst.addr).to_uint() & 0x00FF0000) >> 16,
151  (byteSwap32(leSockPair.dst.addr).to_uint() & 0x0000FF00) >> 8,
152  (byteSwap32(leSockPair.dst.addr).to_uint() & 0x000000FF) >> 0,
153  byteSwap16(leSockPair.dst.port).to_uint());
154 }
155 
156 
162 void printLE_SockAddr(const char *callerName, LE_SockAddr leSockAddr)
163 {
164  printInfo(callerName, "LE_SocketAddr {IpAddr:TcpPort} = {0x%8.8X:0x%4.4X} \n", \
165  leSockAddr.addr.to_uint(), leSockAddr.port.to_uint());
166 }
167 
168 
174 void printSockAddr(const char *callerName, SockAddr sockAddr) {
175  printInfo(callerName, "SocketAddr {IpAddr:TcpPort} = {0x%8.8X:0x%4.4X} = {%3.3d.%3.3d.%3.3d.%3.3d:%5.5d} \n", \
176  sockAddr.addr.to_uint(),
177  sockAddr.port.to_uint(),
178  (sockAddr.addr.to_uint() & 0xFF000000) >> 24,
179  (sockAddr.addr.to_uint() & 0x00FF0000) >> 16,
180  (sockAddr.addr.to_uint() & 0x0000FF00) >> 8,
181  (sockAddr.addr.to_uint() & 0x000000FF) >> 0,
182  sockAddr.port.to_uint());
183 }
184 
185 
191 void printSockAddr(const char *callerName, LE_SockAddr leSockAddr)
192 {
193  SockAddr sockAddr(byteSwap32(leSockAddr.addr),
194  byteSwap16(leSockAddr.port));
195  printSockAddr(callerName, sockAddr);
196 }
197 
198 
205 void printIp4Addr(const char *callerName, const char *message, Ip4Addr ip4Addr) {
206  printInfo(callerName, "%s IPv4 Addr = 0x%8.8X = %3.3d.%3.3d.%3.3d.%3.3d\n",
207  message,
208  ip4Addr.to_uint(),
209  (ip4Addr.to_uint() & 0xFF000000) >> 24,
210  (ip4Addr.to_uint() & 0x00FF0000) >> 16,
211  (ip4Addr.to_uint() & 0x0000FF00) >> 8,
212  (ip4Addr.to_uint() & 0x000000FF) >> 0);
213 }
214 
215 
221 void printIp4Addr(const char *callerName, Ip4Addr ip4Addr) {
222  printInfo(callerName, "IPv4 Addr = 0x%8.8X = %3.3d.%3.3d.%3.3d.%3.3d\n",
223  ip4Addr.to_uint(),
224  (ip4Addr.to_uint() & 0xFF000000) >> 24,
225  (ip4Addr.to_uint() & 0x00FF0000) >> 16,
226  (ip4Addr.to_uint() & 0x0000FF00) >> 8,
227  (ip4Addr.to_uint() & 0x000000FF) >> 0);
228 }
229 
230 
237 void printEthAddr(const char *callerName, const char *message, EthAddr ethAddr) {
238  printInfo(callerName, "%s ETH Addr = 0x%12.12lX \n", message, ethAddr.to_ulong());
239 }
240 
241 
247 void printEthAddr(const char *callerName, EthAddr ethAddr) {
248  printInfo(callerName, "ETH Addr = 0x%12.12lX \n", ethAddr.to_ulong());
249 }
250 
251 
257 void printTcpPort(const char *callerName, TcpPort tcpPort) {
258  printInfo(callerName, "TCP Port = 0x%4.4X = %5.5d\n", \
259  tcpPort.to_uint(), tcpPort.to_uint());
260 }
261 
262 
266 
272 const char *getTcpStateName(TcpState tcpState) {
273  switch (tcpState) {
274  case CLOSED:
275  return "CLOSED";
276  case SYN_SENT:
277  return "SYN_SENT";
278  case SYN_RECEIVED:
279  return "SYN_RECEIVED";
280  case ESTABLISHED:
281  return "ESTABLISHED";
282  case FIN_WAIT_1:
283  return "FIN_WAIT_1";
284  case FIN_WAIT_2:
285  return "FIN_WAIT_2";
286  case CLOSING:
287  return "CLOSING";
288  case TIME_WAIT:
289  return "TIME_WAIT";
290  case LAST_ACK:
291  return "LAST_ACK";
292  default:
293  return "ERROR: UNKNOWN TCP STATE!";
294  }
295 }
296 
297 
301 
307 LE_tKeep lenToLE_tKeep(ap_uint<4> noValidBytes) {
308  LE_tKeep leKeep = 0;
309  switch(noValidBytes) {
310  case 1: leKeep = 0x01; break;
311  case 2: leKeep = 0x03; break;
312  case 3: leKeep = 0x07; break;
313  case 4: leKeep = 0x0F; break;
314  case 5: leKeep = 0x1F; break;
315  case 6: leKeep = 0x3F; break;
316  case 7: leKeep = 0x7F; break;
317  case 8: leKeep = 0xFF; break;
318  }
319  return leKeep;
320 }
321 
322 
328 tKeep lenTotKeep(ap_uint<4> noValidBytes) {
329  tKeep keep = 0;
330  switch(noValidBytes) {
331  case 1: keep = 0x80; break;
332  case 2: keep = 0xC0; break;
333  case 3: keep = 0xE0; break;
334  case 4: keep = 0xF0; break;
335  case 5: keep = 0xF8; break;
336  case 6: keep = 0xFC; break;
337  case 7: keep = 0xFE; break;
338  case 8: keep = 0xFF; break;
339  }
340  return keep;
341 }
342 
343 
349 ap_uint<16> byteSwap16(ap_uint<16> inputValue) {
350  return (inputValue.range(7,0), inputValue(15, 8));
351 }
352 
353 
359 ap_uint<32> byteSwap32(ap_uint<32> inputValue) {
360  return (inputValue.range( 7, 0), inputValue(15, 8),
361  inputValue.range(23,16), inputValue(31, 24));
362 }
363 
364 
370 ap_uint<48> byteSwap48(ap_uint<48> inputValue) {
371  return (inputValue.range( 7, 0), inputValue.range(15, 8),
372  inputValue.range(23, 16), inputValue.range(31, 24),
373  inputValue.range(39, 32), inputValue.range(47, 40));
374 }
375 
376 
382 ap_uint<64> byteSwap64(ap_uint<64> inputValue) {
383  return (inputValue.range( 7, 0), inputValue(15, 8),
384  inputValue.range(23,16), inputValue(31, 24),
385  inputValue.range(39,32), inputValue(47, 40),
386  inputValue.range(55,48), inputValue(63, 56));
387 }
388 
Ip4Addr ip4Addr
Definition: nts_types.hpp:335
EthAddr macAddr
Definition: nts_types.hpp:334
LE_tKeep getLE_TKeep(int leHi=64/8-1, int leLo=0) const
Definition: AxisRaw.hpp:264
LE_tData getLE_TData(int leHi=64 -1, int leLo=0) const
Definition: AxisRaw.hpp:260
LE_tLast getLE_TLast() const
Definition: AxisRaw.hpp:268
ap_uint< 40 > saddr
ap_uint< 1 > drr
ap_uint< 1 > eof
ap_uint< 4 > tag
ap_uint< 6 > dsa
ap_uint< 23 > btt
Definition: mem.hpp:85
ap_uint< 1 > type
LE_Ip4Address addr
Definition: nts_types.hpp:222
LE_Ly4Port port
Definition: nts_types.hpp:223
LE_SockAddr dst
Definition: nts_types.hpp:263
LE_SockAddr src
Definition: nts_types.hpp:262
Ip4Addr addr
Definition: nts_types.hpp:209
Ly4Port port
Definition: nts_types.hpp:210
SockAddr dst
Definition: nts_types.hpp:249
SockAddr src
Definition: nts_types.hpp:248
ap_uint< 48 > byteSwap48(ap_uint< 48 > inputValue)
Swap the six bytes of a triple-word (.i.e, 48 bits).
Definition: nts_utils.cpp:370
void printLE_SockPair(const char *callerName, LE_SocketPair sockPair)
Print a socket pair association in LITTLE-ENDIAN order.
Definition: nts_utils.cpp:102
ap_uint< 32 > byteSwap32(ap_uint< 32 > inputValue)
Swap the four bytes of a double-word (.i.e, 32 bits).
Definition: nts_utils.cpp:359
ap_uint< 64/8 > tKeep
Definition: AxisRaw.hpp:128
ap_uint< 48 > EthAddr
Definition: AxisEth.hpp:120
ap_uint< 16 > byteSwap16(ap_uint< 16 > inputValue)
Swap the two bytes of a word (.i.e, 16 bits).
Definition: nts_utils.cpp:349
LE_tKeep lenToLE_tKeep(ap_uint< 4 > noValidBytes)
A function to set a number of '1' in an 8-bit field. It is used here to set the number of valid bytes...
Definition: nts_utils.cpp:307
void printArpBindPair(const char *callerName, ArpBindPair arpBind)
Print an ARP binding pair association.
Definition: nts_utils.cpp:80
TcpState
Definition: nts_types.hpp:296
ap_uint< 64 > byteSwap64(ap_uint< 64 > inputValue)
Swap the eight bytes of a quad-word (.i.e, 64 bits).
Definition: nts_utils.cpp:382
void printAxisRaw(const char *callerName, AxisRaw chunk)
Prints an Axis raw data chunk (used for debugging).
Definition: nts_utils.cpp:46
ap_uint< 64/8 > LE_tKeep
Definition: AxisRaw.hpp:124
ap_uint< 32 > Ip4Addr
Definition: AxisIp4.hpp:169
void printDmCmd(const char *callerName, DmCmd dmCmd)
Prints the details of a Data Mover Command (used for debugging).
Definition: nts_utils.cpp:69
void printSockAddr(const char *callerName, SockAddr sockAddr)
Print a socket address.
Definition: nts_utils.cpp:174
void printLE_SockAddr(const char *callerName, LE_SockAddr leSockAddr)
Print a socket address encoded in LITTLE_ENDIAN order.
Definition: nts_utils.cpp:162
ap_uint< 16 > TcpPort
Definition: AxisTcp.hpp:105
const char * getTcpStateName(TcpState tcpState)
Returns the name of an enum-based TCP-State as a user friendly string.
Definition: nts_utils.cpp:272
tKeep lenTotKeep(ap_uint< 4 > noValidBytes)
A function to set a number of '1' in an 8-bit field. It is used here to set the number of valid bytes...
Definition: nts_utils.cpp:328
#define printInfo(callerName, format,...)
A macro to print an information message.
Definition: nts_utils.hpp:169
void printIp4Addr(const char *callerName, const char *message, Ip4Addr ip4Addr)
Print an IPv4 address prepended with a message (used for debugging).
Definition: nts_utils.cpp:205
void printTcpPort(const char *callerName, TcpPort tcpPort)
Print a TCP port.
Definition: nts_utils.cpp:257
void printSockPair(const char *callerName, SocketPair sockPair)
Print a socket pair association.
Definition: nts_utils.cpp:114
void printEthAddr(const char *callerName, const char *message, EthAddr ethAddr)
Print an ETHERNET MAC address prepended with a message (for debug).
Definition: nts_utils.cpp:237
@ LAST_ACK
Definition: nts_types.hpp:298
@ FIN_WAIT_2
Definition: nts_types.hpp:297
@ FIN_WAIT_1
Definition: nts_types.hpp:297
@ SYN_SENT
Definition: nts_types.hpp:296
@ TIME_WAIT
Definition: nts_types.hpp:297
@ ESTABLISHED
Definition: nts_types.hpp:296
@ CLOSING
Definition: nts_types.hpp:297
@ CLOSED
Definition: nts_types.hpp:296
@ SYN_RECEIVED
Definition: nts_types.hpp:296
: Utilities and helpers for the Network-Transport-Stack (NTS) components.