cloudFPGA (cF) API  1.0
The documentation of the source code of cloudFPGA (cF)
nts_types.hpp
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 
41 #ifndef _NTS_TYPES_H_
42 #define _NTS_TYPES_H_
43 
44 #include <hls_stream.h>
45 
46 #include "AxisEth.hpp" // ETHernet
47 #include "AxisIp4.hpp" // IPv4
48 using namespace hls;
49 
50 
51 
55 #define NTS_OK 1
56 #define NTS_KO 0
57 #define OK NTS_OK
58 #define KO NTS_KO
59 
60 #define CMD_INIT 1
61 #define CMD_DROP 1
62 #define CMD_KEEP 0
63 #define CMD_ENABLE 1
64 #define CMD_DISABLE 0
65 #define QUERY_RD 0
66 #define QUERY_WR 1
67 #define QUERY_INIT 1
68 #define QUERY_FAST_RETRANSMIT true
69 
70 #define FLAG_OFF 0
71 #define FLAG_ON 1
72 
73 #define FLAG_OOO true
74 #define FLAG_INO false
75 
76 #define LKP_HIT true
77 #define LKP_NO_HIT false
78 
79 #define STS_OK 1
80 #define STS_KO 0
81 #define STS_OPENED 1
82 #define STS_CLOSED 0
83 #define STS_ERROR 1
84 #define STS_NO_ERR 0
85 
86 #define ACK_ON 1
87 #define NO_ACK 0
88 
89 
104 //========================================================
105 //== SINGLE BIT DEFINITIONS
106 //========================================================
107 typedef ap_uint<1> AckBit; // Acknowledge: Always has to go back to the source of the stimulus (e.g. OpenReq/OpenAck).
108 typedef ap_uint<1> CmdBit; // Command : A verb indicating an order (e.g. DropCmd). Does not expect a return from recipient.
109 typedef ap_uint<1> ErrBit; // Error : Noun or verb indicating an error (e.g. writeError). Does not expect a return from recipient.
110 typedef ap_uint<1> FlagBit; // Flag : Noon or a verb indicating a toggling state (e.g. on/off). Does not expect a return from recipient.
111 typedef ap_uint<1> RdWrBit; // Access mode: Read(0) or Write(1)
112 typedef ap_uint<1> ReqBit; // Request : Verb indicating a demand. Always expects a reply or an acknowledgment (e.g. GetReq/GetRep).
113 typedef ap_uint<1> RepBit; // Reply : Always has to go back to the source of the stimulus (e.g. GetReq/GetRep)
114 typedef ap_uint<1> RspBit; // Response : Used when a reply does not go back to the source of the stimulus.
115 typedef ap_uint<1> SigBit; // Signal : Noun indicating a signal (e.g. RxEventSig). Does not expect a return from recipient.
116 typedef ap_uint<1> StsBit; // Status : Noun or verb indicating a status (e.g. isOpen). Does not have to go back to source of stimulus.
117 typedef ap_uint<1> ValBit; // Valid bit : Must go along with something to validate/invalidate.
118 
119 //========================================================
120 //== BOOLEAN DEFINITIONS
121 //========================================================
122 typedef bool AckBool; // Acknowledge: Always has to go back to the source of the stimulus (e.g. OpenReq/OpenAck).
123 typedef bool CmdBool; // Command : Verb indicating an order (e.g. DropCmd). Does not expect a return from recipient.
124 typedef bool FlagBool; // Flag : Noon or verb indicating a toggling state (e.g. on/off). Does not expect a return from recipient.
125 typedef bool HitBool; // Hit : Noon or verb indicating a success (e.g. match). Does not expect a return from recipient.
126 typedef bool ReqBool; // Request : Verb indicating a demand. Always expects a reply or an acknowledgment (e.g. GetReq/GetRep).
127 typedef bool RepBool; // Reply : Always has to go back to the source of the stimulus (e.g. GetReq/GetRep)
128 typedef bool RspBool; // Response : Used when a reply does not go back to the source of the stimulus.
129 typedef bool SigBool; // Signal : Noun indicating a signal (e.g. TxEventSig). Does not expect a return from recipient.
130 typedef bool StsBool; // Status : Noun or verb indicating a status (e.g. isOpen). Does not have to go back to source of stimulus.
131 typedef bool ValBool; // Valid : Must go along with something to validate/invalidate.
132 
133 //========================================================
134 //== MULTI-BITS DEFINITIONS
135 //========================================================
136 typedef ap_uint<16> SessionId; // TCP Session ID (FIXME - Consider renaming)
137 typedef ap_uint<16> TcpSessId; // TCP Session ID (alias for SessionId)
138 
139 
140 
148 //=========================================================
149 //== ETHERNET FRAME FIELDS - Constant Definitions
150 //=========================================================
151 // Ethernet Broadcast MAC Address
152 #define ETH_BROADCAST_ADDR 0xFFFFFFFFFFFF
153 
154 // EtherType protocol numbers
155 #define ETH_ETHERTYPE_IP4 0x0800
156 #define ETH_ETHERTYPE_ARP 0x0806
157 
158 //=========================================================
159 //== ARP MESSAGE FIELDS - Constant Definitions
160 //=========================================================
161 #define ARP_HTYPE_ETHERNET 0x0001 // Hardware type for Ethernet
162 #define ARP_PTYPE_IPV4 0x0800 // Protocol type for IPv4
163 #define ARP_HLEN_ETHERNET 6 // Hardware addr length for Ethernet
164 #define ARP_PLEN_IPV4 4 // Protocol addr length for IPv4
165 #define ARP_OPER_REQUEST 0x0001 // Operation is request
166 #define ARP_OPER_REPLY 0x0002 // Operation is reply
167 
168 
176 //=====================================================
177 //== IP4 PACKET FIELDS - Constant Definitions
178 //=====================================================
179 
180 // IP4 Broadcast Address
181 #define IP4_BROADCAST_ADDR 0xFFFFFFFF
182 // IP4 Protocol numbers
183 #define IP4_PROT_ICMP 0x01
184 #define IP4_PROT_TCP 0x06
185 #define IP4_PROT_UDP 0x11
186 
187 
188 
196 //========================================================
197 //== LAYER-4 - COMMON TCP and UDP HEADER FIELDS
198 //========================================================
199 typedef ap_uint<16> LE_Ly4Port; // Layer-4 Port in LE order
200 typedef ap_uint<16> LE_Ly4Len; // Layer-4 Length in LE_order
201 typedef ap_uint<16> Ly4Port; // Layer-4 Port
202 typedef ap_uint<16> Ly4Len; // Layer-4 header plus data Length
203 
204 //--------------------------------------------------------
205 //-- LAYER-4 - SOCKET ADDRESS
206 //--------------------------------------------------------
207 class SockAddr { // Socket Address stored in NETWORK BYTE ORDER
208  public:
209  Ip4Addr addr; // IPv4 address in NETWORK BYTE ORDER
210  Ly4Port port; // Layer-4 port in NETWORK BYTE ORDER
211  SockAddr() {}
212  SockAddr(Ip4Addr ip4Addr, Ly4Port layer4Port) :
213  addr(ip4Addr), port(layer4Port) {}
214 };
215 
216 inline bool operator == (SockAddr const &s1, SockAddr const &s2) {
217  return ((s1.addr == s2.addr) and (s1.port == s2.port));
218 }
219 
220 class LE_SockAddr { // Socket Address stored in LITTLE-ENDIAN order !!!
221  public:
222  LE_Ip4Address addr; // IPv4 address in LITTLE-ENDIAN order !!!
223  LE_Ly4Port port; // Layer-4 port in LITTLE-ENDIAN order !!!
226  addr(addr), port(port) {}
227 };
228 
229 struct fourTuple { // [FIXME-TODO] - Replace w/ LE_SocketPair
230  ap_uint<32> srcIp; // IPv4 address in LITTLE-ENDIAN order !!!
231  ap_uint<32> dstIp; // IPv4 address in LITTLE-ENDIAN order !!!
232  ap_uint<16> srcPort; // TCP port in in LITTLE-ENDIAN order !!!
233  ap_uint<16> dstPort; // TCP port in in LITTLE-ENDIAN order !!!
235  fourTuple(ap_uint<32> srcIp, ap_uint<32> dstIp, ap_uint<16> srcPort, ap_uint<16> dstPort)
236  : srcIp(srcIp), dstIp(dstIp), srcPort(srcPort), dstPort(dstPort) {}
237 };
238 
239 inline bool operator < (fourTuple const& lhs, fourTuple const& rhs) {
240  return lhs.dstIp < rhs.dstIp || (lhs.dstIp == rhs.dstIp && lhs.srcIp < rhs.srcIp);
241 }
242 
243 //--------------------------------------------------------
244 //-- LAYER-4 - SOCKET PAIR ASSOCIATION
245 //--------------------------------------------------------
246 class SocketPair { // Socket Pair Association in NETWORK-BYTE order !!!
247  public:
248  SockAddr src; // Source socket address in NETWORK-BYTE order !!!
249  SockAddr dst; // Destination socket address in NETWORK-BYTE order !!!
252  src(src), dst(dst) {}
253 };
254 
255 inline bool operator < (SocketPair const &s1, SocketPair const &s2) {
256  return ((s1.dst.addr < s2.dst.addr) ||
257  (s1.dst.addr == s2.dst.addr && s1.src.addr < s2.src.addr));
258 }
259 
260 class LE_SocketPair { // Socket Pair Association in LITTLE-ENDIAN order !!!
261  public:
262  LE_SockAddr src; // Source socket address in LITTLE-ENDIAN order !!!
263  LE_SockAddr dst; // Destination socket address in LITTLE-ENDIAN order !!!
266  src(src), dst(dst) {}
267 };
268 
269 inline bool operator < (LE_SocketPair const &s1, LE_SocketPair const &s2) {
270  return ((s1.dst.addr < s2.dst.addr) ||
271  (s1.dst.addr == s2.dst.addr && s1.src.addr < s2.src.addr));
272 }
273 
274 
275 
282 //=========================================================
283 //== TCP Connection States
284 //== The RFC-793 defines a set of states that a connection
285 //== may progresses through during its lifetime. These
286 //== states are: LISTEN, SYN-SENT, SYN-RECEIVED, ESTABLISHED,
287 //== FIN-WAIT-1, FIN-WAIT-2, CLOSE-WAIT, CLOSING, LAST-ACK,
288 //== TIME-WAIT, and the fictional state CLOSED.
289 //== The implementation of [TOE] does use all of these states:
290 //== * There is no explicit 'LISTEN' which is merged into 'CLOSED'.
291 //== * The 'CLOSE-WAIT' is not used, since 'sndFIN' is sent out
292 //== immediately after the reception of a 'rcvFIN' and the
293 //== application is simply notified.
294 //== * 'FIN_WAIT_2' is also not used.
295 //=========================================================
297  FIN_WAIT_1, FIN_WAIT_2, CLOSING, TIME_WAIT, \
298  LAST_ACK };
299 
300 //=========================================================
301 //== TCP Application Send Error Codes
302 //== Error codes returned by NTS after a send request.
303 //=========================================================
305 
306 //=========================================================
307 //== TCP Application Notification Codes
308 //== Error codes returned by NTS after a data send transfer
309 //=========================================================
310 // [TODO] enum TcpAppWrStsCode { FAILED_TO_OPEN_CON=false, CON_IS_OPENED=true };
311 
312 
313 
318 
325 //========================================================
326 //== ARPCAM - TYPES and CLASSES USED BY THIS INTERFACE
327 //========================================================
328 
329 //---------------------------------------------------------
330 //-- ARPCAM - BIND PAIR - {MAC,IPv4} ASSOCIATION
331 //---------------------------------------------------------
332 class ArpBindPair {
333  public:
337  ArpBindPair(EthAddr newMac, Ip4Addr newIp4) :
338  macAddr(newMac), ip4Addr(newIp4) {}
339 };
340 
341 //---------------------------------------------------------
342 //-- ARPCAM - LOOKUP REQUEST
343 //---------------------------------------------------------
345 
346 //---------------------------------------------------------
347 //-- ARPCAM - LOOKUP REPLY
348 //---------------------------------------------------------
349 class ArpLkpReply {
350  public:
354  ArpLkpReply(EthAddr macAdd, HitBool hit) :
355  macAddress(macAdd), hit(hit) {}
356 };
357 
358 
359 
375 //========================================================
376 //== TOECAM - TYPES and CLASSES USED BY THIS INTERFACE
377 //========================================================
378 typedef ap_uint<14> RtlSessId; // Used by RTL-based CAM
379 typedef ap_uint< 1> LkpSrcBit; // Encodes the initiator of a CAM lookup or update.
380 #define FROM_RXe 0
381 #define FROM_TAi 1
382 enum LkpOpBit { INSERT=0, DELETE }; // Encodes the CAM operation
383 
384 //--------------------------------------------------------
385 //-- TOECAM - Four Tuple
386 //-- This class defines the internal storage used by the
387 //-- TOECAM implementation for the SocketPair. The class
388 //-- uses the terms 'my' and 'their' instead of 'dest' and
389 //-- 'src'.
390 //-- The operator '<' is necessary here for the c++ dummy
391 //-- memory implementation which uses an std::map.
392  //=========================================================
393 class FourTuple { // [FIXME - Replace with SocketPair]
394  public:
400  FourTuple(LE_Ip4Addr myIp, LE_Ip4Addr theirIp, LE_TcpPort myPort, LE_TcpPort theirPort) :
401  myIp(myIp), theirIp(theirIp), myPort(myPort), theirPort(theirPort) {}
402 
403  bool operator < (const FourTuple& other) const {
404  if (myIp < other.myIp) {
405  return true;
406  }
407  else if (myIp == other.myIp) {
408  if (theirIp < other.theirIp) {
409  return true;
410  }
411  else if(theirIp == other.theirIp) {
412  if (myPort < other.myPort) {
413  return true;
414  }
415  else if (myPort == other.myPort) {
416  if (theirPort < other.theirPort) {
417  return true;
418  }
419  }
420  }
421  }
422  return false;
423  }
424 };
425 
426 inline bool operator == (FourTuple const &s1, FourTuple const &s2) {
427  return ((s1.myIp == s2.myIp) && (s1.myPort == s2.myPort) &&
428  (s1.theirIp == s2.theirIp) && (s1.theirPort == s2.theirPort));
429 }
430 
431 //--------------------------------------------------------
432 //-- TOECAM - Session Lookup Request
433 //--------------------------------------------------------
435  public:
436  FourTuple key; // 96 bits
437  LkpSrcBit source; // 1 bit : '0' is [RXe], '1' is [TAi]
438 
441  : key(tuple), source(src) {}
442 };
443 
444 //--------------------------------------------------------
445 //-- CAM - Session Lookup Reply
446 //--------------------------------------------------------
448  public:
449  RtlSessId sessionID; // 14 bits
450  LkpSrcBit source; // 1 bit : '0' is [RXe], '1' is [TAi]
451  bool hit; // 1 bit
452 
455  hit(hit), sessionID(0), source(src) {}
457  hit(hit), sessionID(id), source(src) {}
458 };
459 
460 //--------------------------------------------------------
461 //-- CAM - Session Update Request
462 //--------------------------------------------------------
464  public:
465  FourTuple key; // 96 bits
466  RtlSessId value; // 14 bits
467  LkpSrcBit source; // 1 bit : '0' is [RXe], '1' is [TAi]
468  LkpOpBit op; // 1 bit : '0' is INSERT, '1' is DELETE
469 
472  key(key), value(value), op(op), source(src) {}
473 };
474 
475 //--------------------------------------------------------
476 //-- CAM - Session Update Reply
477 //--------------------------------------------------------
479  public:
480  RtlSessId sessionID; // 14 bits
481  LkpSrcBit source; // 1 bit : '0' is [RXe], '1' is [TAi]
482  LkpOpBit op; // 1 bit : '0' is INSERT, '1' is DELETE
483 
486  op(op), source(src) {}
488  sessionID(id), op(op), source(src) {}
489 };
490 
491 #endif
492 
: A class to access an ETHernet data chunk transmitted over an AXI4-Stream interface.
: A class to access an IPv4 data chunk transmitted over an AXI4-Stream interface.
ArpBindPair(EthAddr newMac, Ip4Addr newIp4)
Definition: nts_types.hpp:337
Ip4Addr ip4Addr
Definition: nts_types.hpp:335
EthAddr macAddr
Definition: nts_types.hpp:334
EthAddr macAddress
Definition: nts_types.hpp:351
ArpLkpReply(EthAddr macAdd, HitBool hit)
Definition: nts_types.hpp:354
HitBool hit
Definition: nts_types.hpp:352
CamSessionLookupReply(bool hit, RtlSessId id, LkpSrcBit src)
Definition: nts_types.hpp:456
CamSessionLookupReply(bool hit, LkpSrcBit src)
Definition: nts_types.hpp:454
CamSessionLookupRequest(FourTuple tuple, LkpSrcBit src)
Definition: nts_types.hpp:440
CamSessionUpdateReply(LkpOpBit op, LkpSrcBit src)
Definition: nts_types.hpp:485
CamSessionUpdateReply(RtlSessId id, LkpOpBit op, LkpSrcBit src)
Definition: nts_types.hpp:487
CamSessionUpdateRequest(FourTuple key, RtlSessId value, LkpOpBit op, LkpSrcBit src)
Definition: nts_types.hpp:471
LE_TcpPort theirPort
Definition: nts_types.hpp:398
LE_Ip4Addr theirIp
Definition: nts_types.hpp:396
FourTuple(LE_Ip4Addr myIp, LE_Ip4Addr theirIp, LE_TcpPort myPort, LE_TcpPort theirPort)
Definition: nts_types.hpp:400
LE_TcpPort myPort
Definition: nts_types.hpp:397
LE_Ip4Addr myIp
Definition: nts_types.hpp:395
LE_SockAddr(LE_Ip4Address addr, LE_Ly4Port port)
Definition: nts_types.hpp:225
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_SocketPair(LE_SockAddr src, LE_SockAddr dst)
Definition: nts_types.hpp:265
LE_SockAddr src
Definition: nts_types.hpp:262
Ip4Addr addr
Definition: nts_types.hpp:209
SockAddr(Ip4Addr ip4Addr, Ly4Port layer4Port)
Definition: nts_types.hpp:212
Ly4Port port
Definition: nts_types.hpp:210
SocketPair(SockAddr src, SockAddr dst)
Definition: nts_types.hpp:251
SockAddr dst
Definition: nts_types.hpp:249
SockAddr src
Definition: nts_types.hpp:248
Ip4Addr ArpLkpRequest
Definition: nts_types.hpp:344
ap_uint< 16 > LE_TcpPort
Definition: AxisTcp.hpp:88
ap_uint< 1 > ErrBit
Definition: nts_types.hpp:109
LkpOpBit
Definition: nts_types.hpp:382
ap_uint< 1 > SigBit
Definition: nts_types.hpp:115
ap_uint< 1 > AckBit
Definition: nts_types.hpp:107
ap_uint< 16 > LE_Ly4Len
Definition: nts_types.hpp:200
ap_uint< 16 > SessionId
Definition: nts_types.hpp:136
ap_uint< 1 > LkpSrcBit
Definition: nts_types.hpp:379
ap_uint< 48 > EthAddr
Definition: AxisEth.hpp:120
bool SigBool
Definition: nts_types.hpp:129
ap_uint< 1 > ReqBit
Definition: nts_types.hpp:112
ap_uint< 32 > LE_Ip4Addr
Definition: AxisIp4.hpp:149
bool operator<(fourTuple const &lhs, fourTuple const &rhs)
Definition: nts_types.hpp:239
bool AckBool
Definition: nts_types.hpp:122
TcpState
Definition: nts_types.hpp:296
ap_uint< 1 > RepBit
Definition: nts_types.hpp:113
ap_uint< 1 > StsBit
Definition: nts_types.hpp:116
ap_uint< 16 > Ly4Port
Definition: nts_types.hpp:201
ap_uint< 1 > FlagBit
Definition: nts_types.hpp:110
bool RspBool
Definition: nts_types.hpp:128
bool StsBool
Definition: nts_types.hpp:130
ap_uint< 16 > Ly4Len
Definition: nts_types.hpp:202
bool operator==(SockAddr const &s1, SockAddr const &s2)
Definition: nts_types.hpp:216
ap_uint< 32 > Ip4Addr
Definition: AxisIp4.hpp:169
ap_uint< 1 > ValBit
Definition: nts_types.hpp:117
ap_uint< 1 > RdWrBit
Definition: nts_types.hpp:111
bool ValBool
Definition: nts_types.hpp:131
bool FlagBool
Definition: nts_types.hpp:124
TcpAppSndErr
Definition: nts_types.hpp:304
ap_uint< 1 > RspBit
Definition: nts_types.hpp:114
ap_uint< 16 > LE_Ly4Port
Definition: nts_types.hpp:199
bool HitBool
Definition: nts_types.hpp:125
ap_uint< 1 > CmdBit
Definition: nts_types.hpp:108
bool RepBool
Definition: nts_types.hpp:127
ap_uint< 14 > RtlSessId
Definition: nts_types.hpp:378
ap_uint< 32 > LE_Ip4Address
Definition: AxisIp4.hpp:148
ap_uint< 16 > TcpSessId
Definition: nts_types.hpp:137
bool ReqBool
Definition: nts_types.hpp:126
bool CmdBool
Definition: nts_types.hpp:123
@ DELETE
Definition: nts_types.hpp:382
@ INSERT
Definition: nts_types.hpp:382
@ FIN_WAIT_2
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
@ NO_ERROR
Definition: nts_types.hpp:304
@ NO_CONNECTION
Definition: nts_types.hpp:304
@ NO_SPACE
Definition: nts_types.hpp:304
ap_uint< 32 > dstIp
Definition: nts_types.hpp:231
ap_uint< 32 > srcIp
Definition: nts_types.hpp:230
ap_uint< 16 > dstPort
Definition: nts_types.hpp:233
fourTuple(ap_uint< 32 > srcIp, ap_uint< 32 > dstIp, ap_uint< 16 > srcPort, ap_uint< 16 > dstPort)
Definition: nts_types.hpp:235
ap_uint< 16 > srcPort
Definition: nts_types.hpp:232