stream_flow.h Source File

Reference Documentation

Platform
Napatech SmartNIC
Content Type
Reference Information
Capture Software Version
Link™ Capture Software 12.15
Napatech Software Suite: include/ntapi/stream_flow.h Source File
stream_flow.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (C) 2025 Napatech A/S.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Napatech A/S nor the
14  * names of its contributors may be used to endorse or promote products
15  * derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL NAPATECH A/S BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  *
29 
30  */
31 
32 /**
33  * @file
34  *
35  * This is header file of the flow stream interface
36  */
37 #ifndef __STREAM_FLOW_H__
38 #define __STREAM_FLOW_H__
39 
40 #if !defined(__NT_H__)
41  #error: My interface file is 'nt.h'. Please include that via #include "nt.h" directly.
42 #endif
43 
44 /** @addtogroup FlowStream
45  * @{
46  * Flow streams are used to program and delete flows as well as receiving flow status
47  * and information records on adapters that support stateful flow tracking.
48  *
49  * A flow stream must be opened before flows can be programmed. This is accomplished
50  * with the @ref NT_FlowOpen_Attr functions which takes a NtFlowAttr_t attribute
51  * structure and returns a handle to the flow stream.
52  *
53  * Three types of records can be present in a flow stream:
54  *
55  * @ref NtFlow_t records for flow programming using @ref NT_FlowWrite
56  *
57  * @ref NtFlowInfo_t records with flow information read using @ref NT_FlowRead. These
58  * records are only generated if flow programming sets the gfi bit.
59  *
60  * @ref NtFlowStatus_t records with async flow programming result read using @ref NT_FlowStatusRead.
61  * Generation of these records depends on ntservice.ini settings.
62  *
63  * To ensure optimal performance an application that both wishes to read and write flows
64  * should alternate between @ref NT_FlowWrite, @ref NT_FlowRead and @ref NT_FlowStatusRead.
65  */
66 
67 
68 /**
69  * Flow stream handle
70  */
71 typedef struct NtFlowStream_s* NtFlowStream_t;
72 
73 /** \cond */
74 #define __SIZEOF_NT_FLOW_ATTR 128
75 union __nt_flow_attr {
76  char __size[__SIZEOF_NT_FLOW_ATTR];
77  long int __align;
78 };
79 typedef union __nt_flow_attr NtFlowAttr_t;
80 /** \endcond */
81 
82 /**
83  * @brief Flow programming structure
84  *
85  * This structure is used when programming flows with the @ref NT_FlowWrite
86  * function.
87  */
88 typedef struct NtFlow_s {
89  uint8_t keyData[40]; /*!< Raw key data array as defined by the NTPL KeyDef command */
90  uint64_t id; /*!< User defined ID.
91  Valid range for Flow stream API version 1: 0 to 18446744073709551615 (64 bits).
92  Valid range for Flow stream API version 2: 0 to 4294967295 (32 bits). */
93  uint32_t color; /*!< 32-bit flow color */
94  uint8_t overwrite:1; /*!< Enable overwrite filter action */
95  uint8_t streamId:7; /*!< Marks the stream id if overwrite filter action is enabled.
96  Unused if overwrite is not enabled */
97  uint8_t ipProtocolField; /*!< Next-protocol field from the IP header. Can be extracted from
98  outer or inner IP header or not at all
99  (see @ref NtplKeyType "KeyType NTPL Command") */
100  uint8_t keyId; /*!< Key ID as used in the @ref NtplKeyType "KeyType NTPL Command" */
101  uint8_t keySetId; /*!< Ket Set id as used in the NTPL filter */
102  uint8_t op:4; /*!< Flow programming operation (0: Unlearn, 1: Learn, 2: Relearn, 3: Probe) */
103  uint8_t gfi:1; /*!< Generate flow info record (1: Generate, 0: Do not generate) */
104  uint8_t tau:1; /*!< TCP auto unlearn (1: Auto unlearn enable, 0: Auto unlearn disable) */
105 
106  uint8_t statProf:3; /*!< Selects profile for Periodic Statistic.
107  Ignored by Flow stream API version 1 */
108  uint8_t reserved_a:1; /*!< Reserved for future use */
109 
110  uint8_t scrubProf:3; /*!< Selects profile for Flow scrubber.
111  Ignored by Flow stream API version 1 */
112  uint8_t reserved_b:1; /*!< Reserved for future use */
113 
114  uint8_t infoSuppress:1; /*!< When set, no flow info record is generated for the unlearn.
115  Only used for unlearn operations. */
116  uint8_t reserved_c:7; /*!< Reserved for future use */
117  uint8_t reserved_d[5]; /*!< Reserved for future use */
118 } NtFlow_t;
119 
120 /**
121  * @brief Flow information structure
122  *
123  * This structure contains state for a particular flow. If flow programming
124  * had the gfi bit set, the flow information structure is
125  * returned on the flow stream when the flow is terminated or timed out.
126  */
127 typedef struct NtFlowInfo_s {
128  uint64_t packetsA; /*!< Packet counter for set A */
129  uint64_t octetsA; /*!< Byte/octet counter for set A */
130  uint64_t packetsB; /*!< Packet counter for set B */
131  uint64_t octetsB; /*!< Byte/octet counter for set B */
132  uint64_t ts; /*!< Time stamp in UNIX_NS format of last seen packet */
133  uint64_t id; /*!< The 64-bit user defined ID from the flow programming operation */
134  uint16_t flagsA; /*!< Bitwise OR of TCP flags for packets in set A */
135  uint16_t flagsB; /*!< Bitwise OR of TCP flags for packets in set B */
136  uint8_t cause; /*!< Cause (0: Software unlearn, 1: Timeout, 2: TCP flow termination, 4: Software probe) */
137 } NtFlowInfo_t;
138 
139 
140 #define NT_FLOW_STAT_LDS 0x1 /*< Learn done status flag */
141 #define NT_FLOW_STAT_LFS 0x2 /*< Learn fail status flag */
142 #define NT_FLOW_STAT_LIS 0x4 /*< Learn ignore status flag */
143 #define NT_FLOW_STAT_UDS 0x8 /*< Unlearn done status flag */
144 #define NT_FLOW_STAT_UIS 0x10 /*< Unlearn ignore status flag */
145 #define NT_FLOW_STAT_RDS 0x20 /*< Relearn done status flag */
146 #define NT_FLOW_STAT_RIS 0x40 /*< Relearn ignore status flag */
147 #define NT_FLOW_STAT_PDS 0x80 /*< Probe done status flag */
148 #define NT_FLOW_STAT_PIS 0x100 /*< Probe ignore status flag */
149 
150 /**
151  * @brief Flow status structure
152  *
153  * This structure contains the status for a particular flow (Un-)learning
154  * operation. A series of flags indicates whether the operation was successful
155  * or not.
156  */
157 typedef struct NtFlowStatus_s {
158  uint64_t id; /*< 64-bit user defined flow id */
159  uint32_t flags; /*< Flags indicating the status of the operation */
161 
162 /**
163  * @brief Initialize flow stream attributes
164  *
165  * See NT_FlowOpen_Attr for further description
166  *
167  * @param attr Pointer to the attributes structure
168  */
169 void NT_FlowOpenAttrInit(NtFlowAttr_t *attr);
170 
171 /**
172  * @brief Set adapter number attribute
173  *
174  * @param attr Pointer to the attributes structure
175  * @param adapterNo Adapter number the stream should be associated to
176  */
177 void NT_FlowOpenAttrSetAdapterNo(NtFlowAttr_t *attr, uint8_t adapterNo);
178 
179 /**
180  * @brief Return the adapter number attribute
181  *
182  * See NT_FlowOpen_Attr for further description
183  *
184  * @return The adapter number attribute
185  */
186 uint8_t NT_FlowOpenAttrGetAdapterNo(NtFlowAttr_t *attr);
187 
188 /**
189  * @brief Set flow stream flags
190  *
191  * Currently no flags are defined so calls of this
192  * function shall be avoided
193  *
194  * @param attr Pointer to attriute structure
195  * @param flags Flow stream flags
196  */
197 void NT_FlowOpenAttrSetFlags(NtFlowAttr_t *attr, uint32_t flags);
198 
199 /**
200  * @brief Get flow stream flags
201  *
202  * @param attr Pointer to attriute structure
203  * @return Flow stream flags
204  */
205 uint32_t NT_FlowOpenAttrGetFlags(NtFlowAttr_t *attr);
206 
207 /**
208  * @brief Opens a flow programming stream and returns a stream handle
209  *
210  * This function opens a flow stream which is used for fast programming of the flow
211  * matcher present on adapters that support the flow matching feature. A flow stream
212  * should only be opened after all relevant NTPL statements configuring the flow matcher
213  * has been executed.
214  *
215  * The default sequence for opening a flow stream is:
216  *
217  * NtFlowAttr_t attr;
218  *
219  * NT_FlowOpenAttrInit(&attr);
220  *
221  * NT_FlowOpenAttrSetAdapterNo(&attr, adapter_no);
222  *
223  * int status = NT_FlowOpen_Attr(&_stream, "name", &attr);
224  *
225  * if (status != NT_SUCCESS) error_handling(status);
226  *
227  * uint32_t flow_api_version = NT_FlowGetVersion(_stream);
228  *
229  * @param[out] hStream Reference to the NtFlowStream_t stream pointer
230  * @param[in] name Name of the stream
231  * @param[in] attr Pointer to the flow stream attributes
232  */
233 int NT_FlowOpen_Attr(NtFlowStream_t* hStream, const char *name, NtFlowAttr_t* attr);
234 
235 /**
236  * @brief Closes a flow stream
237  *
238  * This function closes and frees resources belonging to a flow stream
239  *
240  * @param[in] hStream Flow stream handle to be closed
241  * @retval NT_SUCCESS Success
242  * @retval !=NT_SUCCESS Error - use @ref NT_ExplainError for an error description
243  */
244 int NT_FlowClose(NtFlowStream_t hStream);
245 
246 /**
247  * @brief Returns the Flow stream API version
248  *
249  * When programming flows into the adapter using @ref NT_FlowWrite
250  * the flow configuration argument @ref NtFlow_t must be programmed
251  * in accordance with the current Flow stream API version.
252  *
253  * See @ref NtFlow_t for details.
254  *
255  * @param[in] hStream The flow stream handle obtained in @ref NT_FlowOpen_Attr
256  * @retval Flow stream version
257  */
258 uint32_t NT_FlowGetVersion(NtFlowStream_t hStream);
259 
260 /**
261  * @brief Programs a flow into an adapter
262  *
263  * This function is used to program flows into the adapter associated
264  * with the stream handle. The flow data must be written into the @ref NtFlow_t
265  * structure by the caller before calling this function. Correct use of
266  * @ref NtFlow_t depends on Flow stream API version. Current version can
267  * be read using @ref NT_FlowGetVersion.
268  *
269  * Returns NT_ERROR_INVALID_PARAMETER if NtFlow_t:
270  * - tau = 1 and gfi = 0
271  * - overwrite is set and any of 7 most significant bit in color is set
272  * - overwrite is not set but streamId is different from 0
273  *
274  * @param[in] hStream The flow stream handle obtained in @ref NT_FlowOpen_Attr
275  * @param[in] flow Pointer to the flow data structure
276  * @param[in] timeout Timeout in ms before failing to program the flow (-1 = retry indefinitely)
277  * @retval NT_SUCCESS The flow was successfully programmed
278  * @retval !=NT_SUCCESS Error - use @ref NT_ExplainError for an error description
279  */
280 int NT_FlowWrite(NtFlowStream_t hStream, NtFlow_t* flow, int timeout);
281 
282 /**
283  * @brief Read a flow information record
284  *
285  * This function will attempt to read a flow information record. If a record
286  * is available in the stream it will be returned in the record pointed
287  * to by "info".
288  *
289  * @param[in] hStream The flow stream handle obtained in @ref NT_FlowOpen_Attr
290  * @param[out] info Pointer to flow info structure the record is returned in
291  * @param[in] timeout Timeout in ms before failing to program the flow (-1 = retry indefinitely)
292  * @retval NT_SUCCESS A flow information record was successfully read
293  * @retval !=NT_SUCCESS Error - use @ref NT_ExplainError for an error description
294  */
295 int NT_FlowRead(NtFlowStream_t hStream, NtFlowInfo_t* info, int timeout);
296 
297 /**
298  * @brief Read a flow status record
299  *
300  * This function will attempt to read a flow status record. A status record
301  * is an async result response for an NT_FlowWrite. If a record
302  * is available in the stream it will be returned in the record pointed
303  * to by "status".
304  *
305  * There are 5 types of status records:
306  *
307  * LearnDone which signals a succesful flow program operation
308  *
309  * LearnFail which signals an unsuccesful flow program operation
310  *
311  * LearnIgnore which signals an ignored flow program operation (flow already exists)
312  *
313  * UnlearnDone which signals a succesful flow unlearn operation
314  *
315  * UnlearnIgnore which signals an ignored flow unlearn operation (flow doesn't exists)
316  *
317  * Generation of status records can be enabled/disabled individually in ntservice.ini
318  *
319  *
320  * @param[in] hStream The flow stream handle obtained in @ref NT_FlowOpen_Attr
321  * @param[out] status Pointer to flow status structure the record is returned in
322  * @retval NT_SUCCESS A flow status record was successfully read
323  * @retval NT_STATUS_TRYAGAIN No flow status records available at this time
324  * @retval !=NT_SUCCESS Error - use @ref NT_ExplainError for an error description
325  */
326 int NT_FlowStatusRead(NtFlowStream_t hStream, NtFlowStatus_t* status);
327 
328 /**
329  * @}
330  */
331 
332 #endif