cloudFPGA (cF) API  1.0
The documentation of the source code of cloudFPGA (cF)
test_udp_shell_if.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 
47 #include "test_udp_shell_if.hpp"
48 
49 using namespace hls;
50 using namespace std;
51 
52 //---------------------------------------------------------
53 // HELPERS FOR THE DEBUGGING TRACES
54 // .e.g: DEBUG_LEVEL = (MDL_TRACE | IPS_TRACE)
55 //---------------------------------------------------------
56 #define THIS_NAME "TB_USIF"
57 
58 #ifndef __SYNTHESIS__
59  extern unsigned int gSimCycCnt;
60  extern unsigned int gMaxSimCycles;
61 #endif
62 
63 
64 
74 int main(int argc, char *argv[]) {
75 
76  //------------------------------------------------------
77  //-- TESTBENCH GLOBAL VARIABLES
78  //------------------------------------------------------
79  gSimCycCnt = 0; // Simulation cycle counter as a global variable
80 
81  //------------------------------------------------------
82  //-- DUT SIGNAL INTERFACES
83  //------------------------------------------------------
84  //-- SHL / Mmio Interface
85  CmdBit sMMIO_USIF_Enable;
86  //-- UOE->MMIO / Ready Signal
87  StsBit sUOE_MMIO_Ready;
88  //------------------------------------------------------
89  //-- DUT STREAM INTERFACES
90  //------------------------------------------------------
91  //-- UAF->USIF / UDP Tx Data Interface
92  stream<UdpAppData> ssUAF_USIF_Data ("ssUAF_USIF_Data");
93  stream<UdpAppMeta> ssUAF_USIF_Meta ("ssUAF_USIF_Meta");
94  stream<UdpAppDLen> ssUAF_USIF_DLen ("ssUAF_USIF_DLen");
95  //-- USIF->UOE / UDP Tx Data Interface
96  stream<UdpAppData> ssUSIF_UOE_Data ("ssUSIF_UOE_Data");
97  stream<UdpAppMeta> ssUSIF_UOE_Meta ("ssUSIF_UOE_Meta");
98  stream<UdpAppDLen> ssUSIF_UOE_DLen ("ssUSIF_UOE_DLen");
99  //-- UOE->USIF / UDP Rx Data Interface
100  stream<UdpAppData> ssUOE_USIF_Data ("ssUOE_USIF_Data");
101  stream<UdpAppMeta> ssUOE_USIF_Meta ("ssUOE_USIF_Meta");
102  stream<UdpAppDLen> ssUOE_USIF_DLen ("ssUOE_USIF_DLen");
103  //-- USIF->UAF / UDP Rx Data Interface
104  stream<UdpAppData> ssUSIF_UAF_Data ("ssUSIF_UAF_Data");
105  stream<UdpAppMeta> ssUSIF_UAF_Meta ("ssUSIF_UAF_Meta");
106  stream<UdpAppDLen> ssUSIF_UAF_DLen ("ssUSIF_UAF_DLen");
107  //-- UOE / Control Port Interfaces
108  stream<UdpPort> ssUSIF_UOE_LsnReq ("ssUSIF_UOE_LsnReq");
109  stream<StsBool> ssUOE_USIF_LsnRep ("ssUOE_USIF_LsnRep");
110  stream<UdpPort> ssUSIF_UOE_ClsReq ("ssUSIF_UOE_ClsReq");
111  stream<StsBool> ssUOE_USIF_ClsRep ("ssUOE_USIF_ClsRep");
112 
113  //------------------------------------------------------
114  //-- TESTBENCH VARIABLES
115  //------------------------------------------------------
116  int nrErr = 0; // Total number of testbench errors
117  ofstream ofUOE_Data; // Data streams delivered to UOE
118  ofstream ofUOE_Meta; // Meta streams delivered to UOE
119  string ofUOE_DataName = "../../../../test/simOutFiles/soUOE_Data.dat";
120  string ofUOE_MetaName = "../../../../test/simOutFiles/soUOE_Meta.dat";
121  ofstream ofUOE_DataGold; // Data gold streams to compare with
122  ofstream ofUOE_MetaGold; // Meta gold streams to compare with
123  string ofUOE_DataGoldName = "../../../../test/simOutFiles/soUOE_DataGold.dat";
124  string ofUOE_MetaGoldName = "../../../../test/simOutFiles/soUOE_MetaGold.dat";
125 
126  const int defaultLenOfDatagramEcho = 42;
127  const int defaultDestHostIpv4Test = 0xC0A80096; // 192.168.0.150
128  const int defaultDestHostPortTest = 2718;
129  const int defaultLenOfDatagramTest = 43;
130 
131  int echoLenOfDatagram = defaultLenOfDatagramEcho;
132  ap_uint<32> testDestHostIpv4 = defaultDestHostIpv4Test;
133  ap_uint<16> testDestHostPort = defaultDestHostIpv4Test;
134  int testLenOfDatagram = defaultLenOfDatagramTest;
135 
136  //------------------------------------------------------
137  //-- PARSING THE TESBENCH ARGUMENTS
138  //------------------------------------------------------
139  if (argc >= 2) {
140  echoLenOfDatagram = atoi(argv[1]);
141  if ((echoLenOfDatagram < 1) or (echoLenOfDatagram >= 0x10000)) {
142  printFatal(THIS_NAME, "Argument 'len' is out of range [1:65535].\n");
143  return NTS_KO;
144  }
145  }
146  if (argc >= 3) {
147  if (isDottedDecimal(argv[2])) {
148  testDestHostIpv4 = myDottedDecimalIpToUint32(argv[2]);
149  }
150  else {
151  testDestHostIpv4 = atoi(argv[2]);
152  }
153  if ((testDestHostIpv4 < 0x00000000) or (testDestHostIpv4 > 0xFFFFFFFF)) {
154  printFatal(THIS_NAME, "Argument 'IPv4' is out of range [0x00000000:0xFFFFFFFF].\n");
155  return NTS_KO;
156  }
157  }
158  if (argc >= 4) {
159  testDestHostPort = atoi(argv[3]);
160  if ((testDestHostPort < 0x0000) or (testDestHostPort >= 0x10000)) {
161  printFatal(THIS_NAME, "Argument 'port' is out of range [0:65535].\n");
162  return NTS_KO;
163  }
164  }
165  if (argc >= 5) {
166  testLenOfDatagram = atoi(argv[4]);
167  if ((testLenOfDatagram <= 0) or (testLenOfDatagram >= 0x10000)) {
168  printFatal(THIS_NAME, "Argument 'len' is out of range [0:65535].\n");
169  return NTS_KO;
170  }
171  }
172 
173  SockAddr testSock(testDestHostIpv4, testDestHostPort);
174 
175  //------------------------------------------------------
176  //-- REMOVE PREVIOUS OLD SIM FILES and OPEN NEW ONES
177  //------------------------------------------------------
178  remove(ofUOE_DataName.c_str());
179  if (!ofUOE_Data.is_open()) {
180  ofUOE_Data.open(ofUOE_DataName.c_str(), ofstream::out);
181  if (!ofUOE_Data) {
182  printError(THIS_NAME, "Cannot open the file: \'%s\'.\n", ofUOE_DataName.c_str());
183  return(NTS_KO);
184  }
185  }
186  remove(ofUOE_MetaName.c_str());
187  if (!ofUOE_Meta.is_open()) {
188  ofUOE_Meta.open(ofUOE_MetaName.c_str(), ofstream::out);
189  if (!ofUOE_Meta) {
190  printError(THIS_NAME, "Cannot open the file: \'%s\'.\n", ofUOE_MetaName.c_str());
191  return(NTS_KO);
192  }
193  }
194  remove(ofUOE_DataGoldName.c_str());
195  if (!ofUOE_DataGold.is_open()) {
196  ofUOE_DataGold.open(ofUOE_DataGoldName.c_str(), ofstream::out);
197  if (!ofUOE_DataGold) {
198  printError(THIS_NAME, "Cannot open the file: \'%s\'.\n", ofUOE_DataGoldName.c_str());
199  return(NTS_KO);
200  }
201  }
202  remove(ofUOE_MetaGoldName.c_str());
203  if (!ofUOE_MetaGold.is_open()) {
204  ofUOE_MetaGold.open(ofUOE_MetaGoldName.c_str(), ofstream::out);
205  if (!ofUOE_MetaGold) {
206  printError(THIS_NAME, "Cannot open the file: \'%s\'.\n", ofUOE_MetaGoldName.c_str());
207  return(NTS_KO);
208  }
209  }
210 
212  "############################################################################\n");
214  "## TESTBENCH 'test_udp_shell' STARTS HERE ##\n");
216  "############################################################################\n\n");
217  if (argc > 1) {
219  "This testbench will be executed with the following parameters: \n");
220  for (int i = 1; i < argc; i++) {
221  printInfo(THIS_NAME, "\t==> Param[%d] = %s\n", (i - 1), argv[i]);
222  }
223  }
224 
225  //-----------------------------------------------------
226  //-- MAIN LOOP
227  //-----------------------------------------------------
228  do {
229  //-------------------------------------------------
230  //-- EMULATE SHELL/NTS/UOE
231  //-------------------------------------------------
232  pUOE(
233  nrErr,
234  ofUOE_DataGold,
235  ofUOE_Data,
236  ofUOE_MetaGold,
237  ofUOE_Meta,
238  echoLenOfDatagram,
239  testSock,
240  testLenOfDatagram,
241  //-- UOE / Ready Signal
242  &sUOE_MMIO_Ready,
243  //-- UOE->USIF / UDP Rx Data Interfaces
244  ssUOE_USIF_Data,
245  ssUOE_USIF_Meta,
246  ssUOE_USIF_DLen,
247  //-- USIF->UOE / UDP Tx Data Interfaces
248  ssUSIF_UOE_Data,
249  ssUSIF_UOE_Meta,
250  ssUSIF_UOE_DLen,
251  //-- USIF<->UOE / Listen Interfaces
252  ssUSIF_UOE_LsnReq,
253  ssUOE_USIF_LsnRep,
254  //-- USIF<->UOE / Close Interfaces
255  ssUSIF_UOE_ClsReq);
256 
257  //-------------------------------------------------
258  //-- EMULATE SHELL/MMIO
259  //-------------------------------------------------
260  pMMIO(
261  //-- UOE / Ready Signal
262  &sUOE_MMIO_Ready,
263  //-- MMIO / Enable Layer-7 (.i.e APP alias ROLE)
264  &sMMIO_USIF_Enable);
265 
266  //-------------------------------------------------
267  //-- RUN DUT
268  //-------------------------------------------------
269  udp_shell_if(
270  //-- SHELL / Mmio Interface
271  &sMMIO_USIF_Enable,
272  //-- SHELL / Control Port Interfaces
273  ssUSIF_UOE_LsnReq,
274  ssUOE_USIF_LsnRep,
275  ssUSIF_UOE_ClsReq,
276  ssUOE_USIF_ClsRep,
277  //-- SHELL / Rx Data Interfaces
278  ssUOE_USIF_Data,
279  ssUOE_USIF_Meta,
280  ssUOE_USIF_DLen,
281  //-- SHELL / Tx Data Interfaces
282  ssUSIF_UOE_Data,
283  ssUSIF_UOE_Meta,
284  ssUSIF_UOE_DLen,
285  //-- UAF / Tx Data Interfaces
286  ssUAF_USIF_Data,
287  ssUAF_USIF_Meta,
288  ssUAF_USIF_DLen,
289  //-- UAF / Rx Data Interfaces
290  ssUSIF_UAF_Data,
291  ssUSIF_UAF_Meta,
292  ssUSIF_UAF_DLen);
293 
294  //-------------------------------------------------
295  //-- EMULATE ROLE/UdpApplicationFlash (UAF)
296  //-------------------------------------------------
297  pUAF(
298  //-- USIF / Data Interface
299  ssUSIF_UAF_Data,
300  ssUSIF_UAF_Meta,
301  //-- UAF / Data Interface
302  ssUAF_USIF_Data,
303  ssUAF_USIF_Meta,
304  ssUAF_USIF_DLen);
305 
306  //------------------------------------------------------
307  //-- INCREMENT SIMULATION COUNTER
308  //------------------------------------------------------
309  stepSim();
310 
311  } while ( (gSimCycCnt < gMaxSimCycles) or gFatalError or (nrErr > 10) );
312 
313  //---------------------------------
314  //-- CLOSING OPEN FILES
315  //---------------------------------
316  ofUOE_DataGold.close();
317  ofUOE_Data.close();
318  ofUOE_MetaGold.close();
319  ofUOE_Meta.close();
320 
321  printf("-- [@%4.4d] -----------------------------\n", gSimCycCnt);
322  printf("############################################################################\n");
323  printf("## TESTBENCH 'test_udp_shell_if' ENDS HERE ##\n");
324  printf("############################################################################\n");
325 
326  //---------------------------------------------------------------
327  //-- PRINT TESTBENCH STATUS
328  //---------------------------------------------------------------
329  printf("\n");
330  printInfo(THIS_NAME, "This testbench was executed with the following parameters: \n");
331  for (int i=1; i<argc; i++) {
332  printInfo(THIS_NAME, "\t==> Param[%d] = %s\n", (i-1), argv[i]);
333  }
334  printf("\n");
335 
336  //---------------------------------------------------------------
337  //-- COMPARE THE RESULTS FILES WITH GOLDEN FILES
338  //---------------------------------------------------------------
339  int res = myDiffTwoFiles(std::string(ofUOE_DataName),
340  std::string(ofUOE_DataGoldName));
341  if (res) {
342  printError(THIS_NAME, "File \'%s\' does not match \'%s\'.\n", \
343  ofUOE_DataName.c_str(), ofUOE_DataGoldName.c_str());
344  nrErr += 1;
345  }
346  res = myDiffTwoFiles(std::string(ofUOE_MetaName),
347  std::string(ofUOE_MetaGoldName));
348  if (res) {
349  printError(THIS_NAME, "File \'%s\' does not match \'%s\'.\n", \
350  ofUOE_MetaName.c_str(), ofUOE_MetaGoldName.c_str());
351  nrErr += 1;
352  }
353 
354  if (nrErr) {
355  printError(THIS_NAME, "###############################################################################\n");
356  printError(THIS_NAME, "#### TESTBENCH 'test_udp_shell_if' FAILED : TOTAL NUMBER OF ERROR(S) = %2d ####\n", nrErr);
357  printError(THIS_NAME, "###############################################################################\n");
358 
359  printInfo(THIS_NAME, "FYI - You may want to check for \'ERROR\' and/or \'WARNING\' alarms in the LOG file...\n\n");
360  }
361  else {
362  printInfo(THIS_NAME, "#############################################################\n");
363  printInfo(THIS_NAME, "#### SUCCESSFUL END OF 'test_udp_shell_if' ####\n");
364  printInfo(THIS_NAME, "#############################################################\n");
365  }
366 
367  return(nrErr);
368 
369 }
370 
bool gFatalError
Definition: tb_nal.cpp:152
void stepSim()
Increment the simulation counter.
Definition: test_arp.cpp:54
bool isDottedDecimal(string ipStr)
Checks if a string contains an IP address represented in dot-decimal notation.
Definition: SimNtsUtils.cpp:72
int myDiffTwoFiles(string dataFileName, string goldFileName)
Compares 2 files line-by-line, up to length of the 2nd file.
ap_uint< 32 > myDottedDecimalIpToUint32(string ipStr)
Converts an IPv4 address represented with a dotted-decimal string into an UINT32.
#define NTS_KO
Definition: nts_types.hpp:56
#define printError(callerName, format,...)
A macro to print an error message.
Definition: nts_utils.hpp:195
ap_uint< 1 > StsBit
Definition: nts_types.hpp:116
#define printInfo(callerName, format,...)
A macro to print an information message.
Definition: nts_utils.hpp:169
ap_uint< 1 > CmdBit
Definition: nts_types.hpp:108
#define printFatal(callerName, format,...)
A macro to print a fatal error message and exit.
Definition: nts_utils.hpp:208
void pMMIO(StsBit *piSHL_Ready, CmdBit *poTSIF_Enable)
Emulate the behavior of the SHELL & MMIO.
int main(int argc, char *argv[])
Main function for the test of the UDP Shell Interface (USIF).
void pUAF(stream< UdpAppData > &siUSIF_Data, stream< UdpAppMeta > &siUSIF_Meta, stream< UdpAppData > &soUSIF_Data, stream< UdpAppMeta > &soUSIF_Meta, stream< UdpAppDLen > &soUSIF_DLen)
Emulate the behavior of the ROLE/UdpAppFlash (UAF).
unsigned int gMaxSimCycles
Definition: test_arp.hpp:69
unsigned int gSimCycCnt
Definition: tb_nal.cpp:150
void udp_shell_if(CmdBit *piSHL_Mmio_En, stream< UdpPort > &soSHL_LsnReq, stream< StsBool > &siSHL_LsnRep, stream< UdpPort > &soSHL_ClsReq, stream< StsBool > &siSHL_ClsRep, stream< UdpAppData > &siSHL_Data, stream< UdpAppMeta > &siSHL_Meta, stream< UdpAppDLen > &siSHL_DLen, stream< UdpAppData > &soSHL_Data, stream< UdpAppMeta > &soSHL_Meta, stream< UdpAppDLen > &soSHL_DLen, stream< UdpAppData > &siUAF_Data, stream< UdpAppMeta > &siUAF_Meta, stream< UdpAppDLen > &siUAF_DLen, stream< UdpAppData > &soUAF_Data, stream< UdpAppMeta > &soUAF_Meta, stream< UdpAppDLen > &soUAF_DLen)
Main process of the UDP Shell Interface (USIF).
#define THIS_NAME
void pUOE(int &nrErr, ofstream &dataGoldFile, ofstream &dataFile, ofstream &metaGoldFile, ofstream &metaFile, int echoDgrmLen, SockAddr testSock, int testDgrmLen, StsBit *poMMIO_Ready, stream< UdpAppData > &soUSIF_Data, stream< UdpAppMeta > &soUSIF_Meta, stream< UdpAppDLen > &soUSIF_DLen, stream< UdpAppData > &siUSIF_Data, stream< UdpAppMeta > &siUSIF_Meta, stream< UdpAppDLen > &siUSIF_DLen, stream< UdpPort > &siUSIF_LsnReq, stream< StsBool > &soUSIF_LsnRep, stream< UdpPort > &siUSIF_ClsReq)
Emulate behavior of the SHELL/NTS/UDP Offload Engine (UOE).
out
Definition: test.py:12
: Testbench for the UDP Shell Interface (USIF).