cloudFPGA (cF) API  1.0
The documentation of the source code of cloudFPGA (cF)
test_toecam.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 
30 #include "test_toecam.hpp"
31 
32 using namespace hls;
33 using namespace std;
34 
35 //---------------------------------------------------------
36 // HELPERS FOR THE DEBUGGING TRACES
37 // .e.g: DEBUG_LEVEL = (MDL_TRACE | IPS_TRACE)
38 //---------------------------------------------------------
39 #define THIS_NAME "TB"
40 
41 #define TRACE_OFF 0x0000
42 #define TRACE_TOE 1 << 1
43 #define TRACE_ALL 0xFFFF
44 
45 #define DEBUG_LEVEL (TRACE_OFF)
46 
47 
50 void stepSim() {
51  gSimCycCnt++;
52  if (gTraceEvent || ((gSimCycCnt % 1000) == 0)) {
53  printInfo(THIS_NAME, "-- [@%4.4d] -----------------------------\n", gSimCycCnt);
54  gTraceEvent = false;
55  }
56  else if (0) {
57  printInfo(THIS_NAME, "------------------- [@%d] ------------\n", gSimCycCnt);
58  }
59 }
60 
61 
73 void pTOE(
74  int &nrErr,
75  //-- Session Lookup & Update Interfaces
76  stream<CamSessionLookupRequest> &soCAM_SssLkpReq,
77  stream<CamSessionLookupReply> &siCAM_SssLkpRep,
78  stream<CamSessionUpdateRequest> &soCAM_SssUpdReq,
79  stream<CamSessionUpdateReply> &siCAM_SssUpdRep)
80 {
81  const char *myName = concat3(THIS_NAME, "/", "TOE");
82 
83  static enum slcStates { LOOKUP_REQ, LOOKUP_REP,
84  INSERT_REQ, INSERT_REP, \
85  DELETE_REQ, DELETE_REP, \
86  TB_ERROR, TB_DONE } slcState = LOOKUP_REQ;
87 
88  //------------------------------------------------------
89  //-- EMULATE THE SESSION LOOKUP CONTROLLER
90  //------------------------------------------------------
91  static int rdCnt;
92 
93  switch (slcState) {
94  case LOOKUP_REQ: // SEND A LOOKUP REQUEST TO [CAM]
95  for (int i=0; i<CAM_SIZE; i++) {
96  if (!soCAM_SssLkpReq.full()) {
97  // Build a new request
100  LkpSrcBit src = FROM_RXe;
101  CamSessionLookupRequest lkpRequest(key, src);
102  // Send the new request
103  soCAM_SssLkpReq.write(lkpRequest);
104  printInfo(myName, "Sending LOOKUP request[%d] to [CAM].\n", i);
105  }
106  else {
107  printWarn(myName, "Cannot send LOOKUP request to [CAM] because stream is full.\n");
108  nrErr++;
109  slcState = TB_ERROR;
110  }
111  }
112  // Goto next step
113  slcState = LOOKUP_REP;
114  rdCnt = 0;
115  break;
116  case LOOKUP_REP: // WAIT FOR LOOKUP REPLY FROM [CAM]
117  while (rdCnt < CAM_SIZE) {
118  if (!siCAM_SssLkpRep.empty()) {
119  CamSessionLookupReply lkpReply;
120  siCAM_SssLkpRep.read(lkpReply);
121  if (DEBUG_LEVEL & TRACE_TOE) {
122  printInfo(myName, "Received a lookup reply from [CAM]. \n");
123  printInfo(myName, "Src=%d, SessId=%d, Hit=%d\n", lkpReply.source.to_int(),
124  lkpReply.sessionID.to_int(), lkpReply.hit);
125  }
126  rdCnt++;
127  }
128  else
129  return;
130  }
131  // Goto next step
132  slcState = INSERT_REQ;
133  rdCnt = 0;
134  break;
135  case INSERT_REQ: // SEND AN INSERT REQUEST TO [CAM]
136  for (int i=0; i<CAM_SIZE; i++) {
137  if (!soCAM_SssUpdReq.full()) {
138  // Build a new request
141  LkpSrcBit src = FROM_RXe;
142  RtlSessId value = DEFAULT_SESSION_ID+i;
143  CamSessionUpdateRequest updRequest(key, value, INSERT, src);
144  // Send the new request
145  soCAM_SssUpdReq.write(updRequest);
146  printInfo(myName, "Sending UPDATE request[%d] to [CAM].\n", i);
147  }
148  else {
149  printWarn(myName, "Cannot send INSERT request to [CAM] because stream is full.\n");
150  nrErr++;
151  slcState = TB_ERROR;
152  }
153  }
154  // Goto next step
155  slcState = INSERT_REP;
156  rdCnt = 0;
157  break;
158  case INSERT_REP: // WAIT FOR INSERT REPLY FROM [CAM]
159  while (rdCnt<CAM_SIZE) {
160  if (!siCAM_SssUpdRep.empty()) {
161  CamSessionUpdateReply updReply;
162  siCAM_SssUpdRep.read(updReply);
163  if (DEBUG_LEVEL & TRACE_TOE) {
164  printInfo(myName, "Received an insert reply from [CAM]. \n");
165  printInfo(myName, "Src=%d, Op=%d, SessId=%d.\n",
166  updReply.source.to_int(), updReply.op,
167  updReply.sessionID.to_int());
168  }
169  if (updReply.sessionID != DEFAULT_SESSION_ID+rdCnt) {
170  printError(myName, "Got a wrong session ID (%d) as reply from [CAM].\n",
171  updReply.source.to_int());
172  nrErr++;
173  slcState = TB_ERROR;
174  }
175  rdCnt++;
176  }
177  else
178  return;
179  }
180  // Goto next step
181  slcState = DELETE_REQ;
182  rdCnt = 0;
183  break;
184  case DELETE_REQ: // SEND A DELETE REQUEST TO [CAM]
185  for (int i=0; i<CAM_SIZE; i++) {
186  if (!soCAM_SssUpdReq.full()) {
187  // Build a new request
190  LkpSrcBit src = FROM_RXe;
191  RtlSessId value = DEFAULT_SESSION_ID+i;
192  CamSessionUpdateRequest updRequest(key, value, DELETE, src);
193  // Send the new request
194  soCAM_SssUpdReq.write(updRequest);
195  printInfo(myName, "Sending DELETE request[%d] to [CAM].\n", i);
196  }
197  else {
198  printWarn(myName, "Cannot send DELETE request to [CAM] because stream is full.\n");
199  nrErr++;
200  slcState = TB_ERROR;
201  }
202  }
203  // Goto next step
204  slcState = DELETE_REP;
205  rdCnt = 0;
206  break;
207  case DELETE_REP: // WAIT FOR DELETE REPLY FROM [CAM]
208  while (rdCnt<CAM_SIZE) {
209  if (!siCAM_SssUpdRep.empty()) {
210  CamSessionUpdateReply updReply;
211  siCAM_SssUpdRep.read(updReply);
212  if (DEBUG_LEVEL & TRACE_TOE) {
213  printInfo(myName, "Received a delete reply from [CAM]. \n");
214  printInfo(myName, "Src=%d, Op=%d, SessId=%d.\n",
215  updReply.source.to_int(), updReply.op,
216  updReply.sessionID.to_int());
217  }
218  if (updReply.sessionID != DEFAULT_SESSION_ID+rdCnt) {
219  printError(myName, "Got a wrong session ID (%d) as reply from [CAM].\n",
220  updReply.source.to_int());
221  nrErr++;
222  slcState = TB_ERROR;
223  }
224  rdCnt++;
225  }
226  else
227  return;
228  }
229  // Goto next step
230  slcState = TB_DONE;
231  break;
232  case TB_ERROR:
233  slcState = TB_ERROR;
234  break;
235  case TB_DONE:
236  slcState = TB_DONE;
237  break;
238  } // End-of: switch (lsnState) {
239 
240 }
241 
242 
243 
247 int main(int argc, char* argv[]) {
248 
249  //------------------------------------------------------
250  //-- TESTBENCH GLOBAL VARIABLES
251  //------------------------------------------------------
252  gTraceEvent = false;
253  gFatalError = false;
254  gSimCycCnt = 0;
255 
256  //------------------------------------------------------
257  //-- DUT STREAM INTERFACES
258  //------------------------------------------------------
259  //-- MMIO Interfaces
260  ap_uint<1> sMMIO_CamReady("sMMIO_CamReady");
261  //-- CAM / This / Session Lookup & Update Interfaces
262  stream<CamSessionLookupRequest> ssTOE_CAM_SssLkpReq("ssTOE_CAM_SssLkpReq");
263  stream<CamSessionLookupReply> ssCAM_TOE_SssLkpRep("ssCAM_TOE_SssLkpRep");
264  stream<CamSessionUpdateRequest> ssTOE_CAM_SssUpdReq("ssTOE_CAM_SssUpdReq");
265  stream<CamSessionUpdateReply> ssCAM_TOE_SssUpdRep("ssCAM_TOE_SssUpdRep");
266 
267  //------------------------------------------------------
268  //-- TESTBENCH VARIABLES
269  //------------------------------------------------------
270  int nrErr = 0; // Total number of testbench errors
271  int tbRun = 0; // Total duration of the test (in clock cycles)
272 
273  printInfo(THIS_NAME, "############################################################################\n");
274  printInfo(THIS_NAME, "## TESTBENCH 'test_toecam' STARTS HERE ##\n");
275  printInfo(THIS_NAME, "############################################################################\n");
276  printInfo(THIS_NAME, "This testbench will be executed with the following parameters: \n");
277  for (int i=1; i<argc; i++) {
278  printInfo(THIS_NAME, "\t==> Param[%d] = %s\n", (i-1), argv[i]);
279  }
280  printf("\n\n");
281 
282  //-----------------------------------------------------
283  //-- MAIN LOOP
284  //-----------------------------------------------------
285  tbRun = (nrErr == 0) ? (TB_MAX_SIM_CYCLES + TB_GRACE_TIME) : 0;
286  while (tbRun) {
287  //-------------------------------------------------
288  //-- EMULATE TOE
289  //-------------------------------------------------
290  if (sMMIO_CamReady == 1) {
291  pTOE(
292  nrErr,
293  //-- TOE / Lookup Request Interfaces
294  ssTOE_CAM_SssLkpReq,
295  ssCAM_TOE_SssLkpRep,
296  //-- TOE / Update Request Interfaces
297  ssTOE_CAM_SssUpdReq,
298  ssCAM_TOE_SssUpdRep
299  );
300  }
301  //-------------------------------------------------
302  //-- RUN DUT
303  //-------------------------------------------------
304  toecam_top(
305  //-- MMIO Interfaces
306  &sMMIO_CamReady,
307  //-- Session Lookup & Update Interfaces
308  ssTOE_CAM_SssLkpReq,
309  ssCAM_TOE_SssLkpRep,
310  ssTOE_CAM_SssUpdReq,
311  ssCAM_TOE_SssUpdRep
312  );
313 
314  //------------------------------------------------------
315  //-- INCREMENT SIMULATION COUNTER
316  //------------------------------------------------------
317  stepSim();
318  tbRun--;
319  } // End of: while()
320 
321  printInfo(THIS_NAME, "############################################################################\n");
322  printInfo(THIS_NAME, "## TESTBENCH 'test_toecam' ENDS HERE ##\n");
323  printInfo(THIS_NAME, "############################################################################\n");
324  stepSim();
325 
326  if (nrErr) {
327  printError(THIS_NAME, "###########################################################\n");
328  printError(THIS_NAME, "#### TEST BENCH FAILED : TOTAL NUMBER OF ERROR(S) = %2d ####\n", nrErr);
329  printError(THIS_NAME, "###########################################################\n");
330  printInfo(THIS_NAME, "FYI - You may want to check for \'ERROR\' and/or \'WARNING\' alarms in the LOG file...\n\n");
331  }
332  else {
333  printInfo(THIS_NAME, "#############################################################\n");
334  printInfo(THIS_NAME, "#### SUCCESSFUL END OF TEST ####\n");
335  printInfo(THIS_NAME, "#############################################################\n");
336  }
337 
338  return(nrErr);
339 
340 }
341 
#define DEFAULT_SESSION_ID
Definition: tb_nal.cpp:75
unsigned int gSimCycCnt
Definition: tb_nal.cpp:150
bool gTraceEvent
Definition: tb_nal.cpp:151
bool gFatalError
Definition: tb_nal.cpp:152
#define DEFAULT_HOST_IP4_ADDR
Definition: tb_nal.cpp:72
#define DEFAULT_FPGA_IP4_ADDR
Definition: tb_nal.cpp:70
#define TB_GRACE_TIME
Definition: test_arp.hpp:54
#define TB_MAX_SIM_CYCLES
Definition: test_arp.hpp:52
int main(int argc, char *argv[])
Main function.
void stepSim()
Increment the simulation counter.
Definition: test_toecam.cpp:50
#define CAM_SIZE
Definition: test_toecam.hpp:83
#define DEFAULT_HOST_TCP_PORT
Definition: test_toecam.hpp:79
void pTOE(int &nrErr, stream< CamSessionLookupRequest > &soCAM_SssLkpReq, stream< CamSessionLookupReply > &siCAM_SssLkpRep, stream< CamSessionUpdateRequest > &soCAM_SssUpdReq, stream< CamSessionUpdateReply > &siCAM_SssUpdRep)
Emulate the behavior of the TCP Offload Engine (TOE).
Definition: test_toecam.cpp:73
#define THIS_NAME
Definition: test_toecam.cpp:39
#define DEFAULT_FPGA_TCP_PORT
Definition: test_toecam.hpp:77
#define DEBUG_LEVEL
Definition: test_toecam.cpp:45
#define TRACE_TOE
Definition: test_toecam.cpp:42
void toecam_top(ap_uint< 1 > *poMMIO_CamReady, stream< CamSessionLookupRequest > &siTOE_SssLkpReq, stream< CamSessionLookupReply > &soTOE_SssLkpRep, stream< CamSessionUpdateRequest > &siTOE_SssUpdReq, stream< CamSessionUpdateReply > &soTOE_SssUpdRep)
Top of Content-Addressable Memory (TOECAM).
Definition: toecam.cpp:414
ap_uint< 1 > LkpSrcBit
Definition: nts_types.hpp:379
#define printError(callerName, format,...)
A macro to print an error message.
Definition: nts_utils.hpp:195
#define printInfo(callerName, format,...)
A macro to print an information message.
Definition: nts_utils.hpp:169
#define FROM_RXe
Definition: nts_types.hpp:380
#define printWarn(callerName, format,...)
A macro to print a warning message.
Definition: nts_utils.hpp:182
#define concat3(firstCharConst, secondCharConst, thirdCharConst)
Definition: nts_utils.hpp:161
ap_uint< 14 > RtlSessId
Definition: nts_types.hpp:378
@ DELETE
Definition: nts_types.hpp:382
@ INSERT
Definition: nts_types.hpp:382