cloudFPGA (cF) API  1.0
The documentation of the source code of cloudFPGA (cF)
SimUdpDatagram Class Reference

Class UDP Datagram. More...

#include <SimUdpDatagram.hpp>

Public Member Functions

 SimUdpDatagram (int dgmLen)
 
 SimUdpDatagram ()
 
void pushChunk (AxisUdp udpChunk)
 
AxisUdp pullChunk ()
 
int length ()
 
int size ()
 
void clone (SimUdpDatagram &udpDgm)
 Clone a UDP datagram. More...
 
void cloneHeader (SimUdpDatagram &udpDgm)
 Clone the header of a UDP datagram. More...
 
SimUdpDatagram pullHeader ()
 Pull the header of this datagram. More...
 
void setUdpSourcePort (int port)
 
int getUdpSourcePort ()
 
void setUdpDestinationPort (int port)
 
int getUdpDestinationPort ()
 
void setUdpLength (UdpLen len)
 
UdpLen getUdpLength ()
 
void setUdpChecksum (UdpCsum csum)
 
UdpCsum getUdpChecksum ()
 
void addUdpPayload (string pldStr)
 
UdpCsum calculateUdpChecksum (Ip4Addr ipSa, Ip4Addr ipDa)
 Calculate the UDP checksum of the datagram. More...
 
UdpCsum reCalculateUdpChecksum (Ip4Addr ipSa, Ip4Addr ipDa)
 Recalculate the UDP checksum of a datagram. More...
 
bool isWellFormed (const char *callerName, Ip4Addr ipSa, Ip4Addr ipDa)
 Checks if the datagram header fields are properly set. More...
 
bool writeAxisUdpToFile (AxisUdp *axisUdp, ofstream &outFileStream)
 Dump an AxisUdp chunk to a file. More...
 
bool writeToDatFile (ofstream &outFileStream)
 Dump this UDP datagram as raw AxisUdp chunks into a file. More...
 
bool writePayloadToDatFile (ofstream &outFileStream)
 Dump the payload of this datagram as AxisUdp chunks into a file. More...
 

Detailed Description

Class UDP Datagram.

This class defines an UDP datagram as a stream of 'AxisUdp' data chunks. Such an UDP datagram consists of a double-ended queue that is used to accumulate all these data chunks. For the 10GbE MAC, the UDP chunks are 64 bits wide.

Definition at line 47 of file SimUdpDatagram.hpp.

Constructor & Destructor Documentation

◆ SimUdpDatagram() [1/2]

SimUdpDatagram::SimUdpDatagram ( int  dgmLen)
inline

Definition at line 83 of file SimUdpDatagram.hpp.

83  {
84  this->myName = "SimUdpDatagram";
85  setLen(0);
86  if (dgmLen > 0 && dgmLen <= MTU) {
87  int noBytes = dgmLen;
88  while(noBytes > 8) {
89  pushChunk(AxisUdp(0x0000000000000000, 0xFF, 0));
90  noBytes -= 8;
91  }
92  pushChunk(AxisUdp(0x0000000000000000, lenToLE_tKeep(noBytes), TLAST));
93  }
94  }
void pushChunk(AxisUdp udpChunk)
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
#define TLAST
Definition: AxisRaw.hpp:116
#define MTU
Definition: udp.hpp:71
Here is the call graph for this function:

◆ SimUdpDatagram() [2/2]

SimUdpDatagram::SimUdpDatagram ( )
inline

Definition at line 95 of file SimUdpDatagram.hpp.

95  {
96  this->myName = "SimUdpDatagram";
97  this->len = 0;
98  }

Member Function Documentation

◆ addUdpPayload()

void SimUdpDatagram::addUdpPayload ( string  pldStr)
inline

Definition at line 189 of file SimUdpDatagram.hpp.

189  {
190  if (this->getLen() != UDP_HEADER_LEN) {
191  printFatal(this->myName, "Empty datagram is expected to be of length %d bytes (was found to be %d bytes).\n",
192  UDP_HEADER_LEN, this->getLen());
193  }
194  int hdrLen = this->getLen(); // in bytes
195  int pldLen = pldStr.size();
196  int q = (hdrLen / 8);
197  int b = (hdrLen % 8);
198  int i = 0;
199  // At this point we are aligned on an 8-byte data chunk since all
200  // UDP packets have an 8-byte header.
201  while (i < pldLen) {
202  unsigned long leQword = 0x0000000000000000; // in LE order
203  unsigned char leKeep = 0x00;
204  bool leLast = false;
205  while (b < 8) {
206  unsigned char datByte = pldStr[i];
207  leQword = leQword | (datByte << b*8);
208  leKeep = leKeep | (1 << b);
209  i++;
210  b++;
211  if (i == pldLen) {
212  leLast = true;
213  break;
214  }
215  }
216  b = 0;
217  pushChunk(AxisUdp(leQword, leKeep, leLast));
218  }
219  } // End-of: addUdpPayload
#define UDP_HEADER_LEN
Definition: AxisUdp.hpp:81
#define printFatal(callerName, format,...)
A macro to print a fatal error message and exit.
Definition: nts_utils.hpp:208
Here is the call graph for this function:

◆ calculateUdpChecksum()

UdpCsum SimUdpDatagram::calculateUdpChecksum ( Ip4Addr  ipSa,
Ip4Addr  ipDa 
)
inline

Calculate the UDP checksum of the datagram.

  • This method computes the UDP checksum over the pseudo header, the UDP header and UDP data. According to RFC768, the pseudo header consists of the IP-{SA,DA,Prot} fields and the UDP length field. 0 7 8 15 16 23 24 31 +-----—+-----—+-----—+-----—+ | source address | +-----—+-----—+-----—+-----—+ | destination address | +-----—+-----—+-----—+-----—+ | zero |protocol| UDP length | +-----—+-----—+-----—+-----—+

@Warning The checksum is computed on the double-ended queue which holds the UDP chunks in little-endian order (see AxisUdp) !

Returns
the computed checksum.

Definition at line 240 of file SimUdpDatagram.hpp.

240  {
241  ap_uint<32> csum = 0;
242  csum += byteSwap16(ipSa(31, 16)); // Set IP_SA in LE
243  csum += byteSwap16(ipSa(15, 0));
244  csum += byteSwap16(ipDa(31, 16)); // Set IP_DA in LE
245  csum += byteSwap16(ipDa(15, 0));
246  csum += byteSwap16(ap_uint<16>(IP4_PROT_UDP));
247  csum += byteSwap16(this->getUdpLength());
248  for (int i=0; i<this->size(); ++i) {
249  LE_tData tempInput = 0;
250  if (dgmQ[i].getLE_TKeep() & 0x01)
251  tempInput.range( 7, 0) = (dgmQ[i].getLE_TData()).range( 7, 0);
252  if (dgmQ[i].getLE_TKeep() & 0x02)
253  tempInput.range(15, 8) = (dgmQ[i].getLE_TData()).range(15, 8);
254  if (dgmQ[i].getLE_TKeep() & 0x04)
255  tempInput.range(23,16) = (dgmQ[i].getLE_TData()).range(23,16);
256  if (dgmQ[i].getLE_TKeep() & 0x08)
257  tempInput.range(31,24) = (dgmQ[i].getLE_TData()).range(31,24);
258  if (dgmQ[i].getLE_TKeep() & 0x10)
259  tempInput.range(39,32) = (dgmQ[i].getLE_TData()).range(39,32);
260  if (dgmQ[i].getLE_TKeep() & 0x20)
261  tempInput.range(47,40) = (dgmQ[i].getLE_TData()).range(47,40);
262  if (dgmQ[i].getLE_TKeep() & 0x40)
263  tempInput.range(55,48) = (dgmQ[i].getLE_TData()).range(55,48);
264  if (dgmQ[i].getLE_TKeep() & 0x80)
265  tempInput.range(63,56) = (dgmQ[i].getLE_TData()).range(63,56);
266  csum = ((((csum +
267  tempInput.range(63, 48)) + tempInput.range(47, 32)) +
268  tempInput.range(31, 16)) + tempInput.range(15, 0));
269  }
270  while (csum >> 16) {
271  csum = (csum & 0xFFFF) + (csum >> 16);
272  }
273  // Reverse the bits of the result
274  UdpCsum udpCsum = csum.range(15, 0);
275  udpCsum = ~udpCsum;
276  return byteSwap16(udpCsum);
277  }
ap_uint< 64 > LE_tData
Definition: AxisRaw.hpp:122
#define IP4_PROT_UDP
Definition: nts_types.hpp:185
ap_uint< 16 > UdpCsum
Definition: AxisUdp.hpp:101
ap_uint< 16 > byteSwap16(ap_uint< 16 > inputVector)
Definition: udp.cpp:82
Here is the call graph for this function:
Here is the caller graph for this function:

◆ clone()

void SimUdpDatagram::clone ( SimUdpDatagram udpDgm)
inline

Clone a UDP datagram.

Parameters
[in]udpDgmA reference to the datagram to clone.

Definition at line 132 of file SimUdpDatagram.hpp.

132  {
133  AxisUdp newAxisUdp;
134  for (int i=0; i<udpDgm.dgmQ.size(); i++) {
135  newAxisUdp = udpDgm.dgmQ[i];
136  this->dgmQ.push_back(newAxisUdp);
137  }
138  this->setLen(udpDgm.getLen());
139  }

◆ cloneHeader()

void SimUdpDatagram::cloneHeader ( SimUdpDatagram udpDgm)
inline

Clone the header of a UDP datagram.

Parameters
[in]udpDgmA reference to the datagram to clone.

Definition at line 145 of file SimUdpDatagram.hpp.

145  {
146  int cloneBytes = UDP_HEADER_LEN; // in bytes
147  int inpChunkCnt = 0;
148  while(cloneBytes > 0) {
149  if (cloneBytes > 8) {
150  this->pushChunk(udpDgm.dgmQ[inpChunkCnt]);
151  }
152  else {
153  AxisUdp lastHdrChunk(udpDgm.dgmQ[inpChunkCnt].getLE_TData(),
154  lenToLE_tKeep(cloneBytes), TLAST);
155  this->pushChunk(lastHdrChunk);
156  }
157  cloneBytes -= 8;
158  inpChunkCnt++;
159  }
160  }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ getUdpChecksum()

UdpCsum SimUdpDatagram::getUdpChecksum ( )
inline

Definition at line 186 of file SimUdpDatagram.hpp.

186 { return dgmQ[0].getUdpCsum(); }
Here is the caller graph for this function:

◆ getUdpDestinationPort()

int SimUdpDatagram::getUdpDestinationPort ( )
inline

Definition at line 180 of file SimUdpDatagram.hpp.

180 { return dgmQ[0].getUdpDstPort(); }
Here is the caller graph for this function:

◆ getUdpLength()

UdpLen SimUdpDatagram::getUdpLength ( )
inline

Definition at line 183 of file SimUdpDatagram.hpp.

183 { return dgmQ[0].getUdpLen(); }
Here is the caller graph for this function:

◆ getUdpSourcePort()

int SimUdpDatagram::getUdpSourcePort ( )
inline

Definition at line 177 of file SimUdpDatagram.hpp.

177 { return dgmQ[0].getUdpSrcPort(); }
Here is the caller graph for this function:

◆ isWellFormed()

bool SimUdpDatagram::isWellFormed ( const char *  callerName,
Ip4Addr  ipSa,
Ip4Addr  ipDa 
)
inline

Checks if the datagram header fields are properly set.

Parameters
[in]callerNameThe name of the calling function or process.
[in]ipSaThe IP source address.
[in]ipDaThe IP destination address.
Returns
true if the cheksum and the length fields are valid.

Definition at line 305 of file SimUdpDatagram.hpp.

305  {
306  bool rc = true;
307  // Assess the length field vs datagram length
308  if (this->getUdpLength() != this->getLen()) {
309  printWarn(callerName, "Malformed UDP datagram: 'Length' field does not match the length of the datagram.\n");
310  printWarn(callerName, "\tFound 'Length' field=0x%4.4X, Was expecting 0x%4.4X)\n",
311  (this->getUdpLength()).to_uint(), this->getLen());
312  rc = false;
313  }
314  // Assess the checksum is valid (or 0x0000)
315  UdpCsum udpHCsum = this->getUdpChecksum();
316  UdpCsum calcCsum = this->reCalculateUdpChecksum(ipSa, ipDa);
317  if ((udpHCsum != 0) and (udpHCsum != calcCsum)) {
318  // UDP datagram comes with an invalid checksum
319  printWarn(callerName, "Malformed UDP datagram: 'Checksum' field does not match the checksum of the pseudo-packet.\n");
320  printWarn(callerName, "\tFound 'Checksum' field=0x%4.4X, Was expecting 0x%4.4X)\n",
321  udpHCsum.to_uint(), calcCsum.to_ushort());
322  rc = false;
323  }
324  return rc;
325  }
UdpCsum reCalculateUdpChecksum(Ip4Addr ipSa, Ip4Addr ipDa)
Recalculate the UDP checksum of a datagram.
UdpCsum getUdpChecksum()
#define printWarn(callerName, format,...)
A macro to print a warning message.
Definition: nts_utils.hpp:182
Here is the call graph for this function:

◆ length()

int SimUdpDatagram::length ( )
inline

Definition at line 119 of file SimUdpDatagram.hpp.

119  {
120  return this->len;
121  }
Here is the caller graph for this function:

◆ pullChunk()

AxisUdp SimUdpDatagram::pullChunk ( )
inline

Definition at line 111 of file SimUdpDatagram.hpp.

111  {
112  AxisUdp headingChunk = this->front();
113  this->pop_front();
114  setLen(getLen() - headingChunk.getLen());
115  return headingChunk;
116  }
int getLen() const
Definition: AxisRaw.hpp:411
Here is the call graph for this function:
Here is the caller graph for this function:

◆ pullHeader()

SimUdpDatagram SimUdpDatagram::pullHeader ( )
inline

Pull the header of this datagram.

Returns
the datagram header as a 'SimUdpDatagram'.

Definition at line 166 of file SimUdpDatagram.hpp.

166  {
167  SimUdpDatagram headerAsDatagram;
168  AxisUdp headerChunk = this->front();
169  this->pop_front();
170  setLen(getLen() - headerChunk.getLen());
171  headerAsDatagram.pushChunk(headerChunk);
172  return headerAsDatagram;
173  }
Class UDP Datagram.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ pushChunk()

void SimUdpDatagram::pushChunk ( AxisUdp  udpChunk)
inline

Definition at line 101 of file SimUdpDatagram.hpp.

101  {
102  if (this->size() > 0) {
103  // Always clear 'TLAST' bit of previous chunck
104  this->dgmQ[this->size()-1].setLE_TLast(0);
105  }
106  this->push_back(udpChunk);
107  this->setLen(this->getLen() + udpChunk.getLen());
108  }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ reCalculateUdpChecksum()

UdpCsum SimUdpDatagram::reCalculateUdpChecksum ( Ip4Addr  ipSa,
Ip4Addr  ipDa 
)
inline

Recalculate the UDP checksum of a datagram.

  • While re-computing the checksum, the checksum field itself is replaced with zeros.
  • This will also overwrite the former UDP checksum.
  • You typically use this method if the datagram was modified or when the checksum has not yet been calculated.
Returns
the computed checksum.

Definition at line 289 of file SimUdpDatagram.hpp.

289  {
290  this->setUdpChecksum(0x0000);
291  UdpCsum newUdpCsum = calculateUdpChecksum(ipSa, ipDa);
292  // Overwrite the former UDP checksum
293  this->setUdpChecksum(newUdpCsum);
294  return (newUdpCsum);
295  }
void setUdpChecksum(UdpCsum csum)
UdpCsum calculateUdpChecksum(Ip4Addr ipSa, Ip4Addr ipDa)
Calculate the UDP checksum of the datagram.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ setUdpChecksum()

void SimUdpDatagram::setUdpChecksum ( UdpCsum  csum)
inline

Definition at line 185 of file SimUdpDatagram.hpp.

185 { dgmQ[0].setUdpCsum(csum); }
Here is the caller graph for this function:

◆ setUdpDestinationPort()

void SimUdpDatagram::setUdpDestinationPort ( int  port)
inline

Definition at line 179 of file SimUdpDatagram.hpp.

179 { dgmQ[0].setUdpDstPort(port); }
Here is the caller graph for this function:

◆ setUdpLength()

void SimUdpDatagram::setUdpLength ( UdpLen  len)
inline

Definition at line 182 of file SimUdpDatagram.hpp.

182 { dgmQ[0].setUdpLen(len); }
Here is the caller graph for this function:

◆ setUdpSourcePort()

void SimUdpDatagram::setUdpSourcePort ( int  port)
inline

Definition at line 176 of file SimUdpDatagram.hpp.

176 { dgmQ[0].setUdpSrcPort(port); }
Here is the caller graph for this function:

◆ size()

int SimUdpDatagram::size ( )
inline

Definition at line 124 of file SimUdpDatagram.hpp.

124  {
125  return this->dgmQ.size();
126  }
Here is the caller graph for this function:

◆ writeAxisUdpToFile()

bool SimUdpDatagram::writeAxisUdpToFile ( AxisUdp axisUdp,
ofstream &  outFileStream 
)
inline

Dump an AxisUdp chunk to a file.

Parameters
[in]axisUdpA pointer to the AxisUdp chunk to write.
[in]outFileStreamA reference to the file stream to write.
Returns
true upon success, otherwise false.

Definition at line 333 of file SimUdpDatagram.hpp.

333  {
334  if (!outFileStream.is_open()) {
335  printError(myName, "File is not opened.\n");
336  return false;
337  }
338  AxisRaw axisRaw(axisUdp->getLE_TData(), axisUdp->getLE_TKeep(), axisUdp->getLE_TLast());
339  bool rc = writeAxisRawToFile(axisRaw, outFileStream);
340  return(rc);
341  }
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
bool writeAxisRawToFile(AxisRaw &axisRaw, ofstream &outFileStream)
Dump an Axis raw data chunk to a file.
#define printError(callerName, format,...)
A macro to print an error message.
Definition: nts_utils.hpp:195
Here is the call graph for this function:
Here is the caller graph for this function:

◆ writePayloadToDatFile()

bool SimUdpDatagram::writePayloadToDatFile ( ofstream &  outFileStream)
inline

Dump the payload of this datagram as AxisUdp chunks into a file.

Parameters
[in]outFileStreamA reference to the file stream to write.
Returns
true upon success, otherwise false.

Definition at line 363 of file SimUdpDatagram.hpp.

363  {
364  for (int i=1; i < this->size(); i++) {
365  AxisUdp axisChunk = this->dgmQ[i];
366  if (not this->writeAxisUdpToFile(&axisChunk, outFileStream)) {
367  return false;
368  }
369  }
370  return true;
371  }
bool writeAxisUdpToFile(AxisUdp *axisUdp, ofstream &outFileStream)
Dump an AxisUdp chunk to a file.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ writeToDatFile()

bool SimUdpDatagram::writeToDatFile ( ofstream &  outFileStream)
inline

Dump this UDP datagram as raw AxisUdp chunks into a file.

Parameters
[in]outFileStreamA reference to the file stream to write.
Returns
true upon success, otherwise false.

Definition at line 348 of file SimUdpDatagram.hpp.

348  {
349  for (int i=0; i < this->size(); i++) {
350  AxisUdp axisUdp = this->dgmQ[i];
351  if (not this->writeAxisUdpToFile(&axisUdp, outFileStream)) {
352  return false;
353  }
354  }
355  return true;
356  }
Here is the call graph for this function:

The documentation for this class was generated from the following file: