flowmatch_example_main.cpp Source File

Reference Documentation

Platform
Intel® PAC
Napatech SmartNIC
Content Type
Reference Information
Capture Software Version
Link™ Capture Software 12.10
Napatech Software Suite: examples/flowmatch/flowmatch_example_main.cpp Source File
flowmatch_example_main.cpp
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2023 Napatech A/S. All Rights Reserved.
4  *
5  * 1. Copying, modification, and distribution of this file, or executable
6  * versions of this file, is governed by the terms of the Napatech Software
7  * license agreement under which this file was made available. If you do not
8  * agree to the terms of the license do not install, copy, access or
9  * otherwise use this file.
10  *
11  * 2. Under the Napatech Software license agreement you are granted a
12  * limited, non-exclusive, non-assignable, copyright license to copy, modify
13  * and distribute this file in conjunction with Napatech SmartNIC's and
14  * similar hardware manufactured or supplied by Napatech A/S.
15  *
16  * 3. The full Napatech Software license agreement is included in this
17  * distribution, please see "NP-0405 Napatech Software license
18  * agreement.pdf"
19  *
20  * 4. Redistributions of source code must retain this copyright notice,
21  * list of conditions and the following disclaimer.
22  *
23  * THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTIES, EXPRESS OR
24  * IMPLIED, AND NAPATECH DISCLAIMS ALL IMPLIED WARRANTIES INCLUDING ANY
25  * IMPLIED WARRANTY OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, OR OF
26  * FITNESS FOR A PARTICULAR PURPOSE. TO THE EXTENT NOT PROHIBITED BY
27  * APPLICABLE LAW, IN NO EVENT SHALL NAPATECH BE LIABLE FOR PERSONAL INJURY,
28  * OR ANY INCIDENTAL, SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES WHATSOEVER,
29  * INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF PROFITS, CORRUPTION OR
30  * LOSS OF DATA, FAILURE TO TRANSMIT OR RECEIVE ANY DATA OR INFORMATION,
31  * BUSINESS INTERRUPTION OR ANY OTHER COMMERCIAL DAMAGES OR LOSSES, ARISING
32  * OUT OF OR RELATED TO YOUR USE OR INABILITY TO USE NAPATECH SOFTWARE OR
33  * SERVICES OR ANY THIRD PARTY SOFTWARE OR APPLICATIONS IN CONJUNCTION WITH
34  * THE NAPATECH SOFTWARE OR SERVICES, HOWEVER CAUSED, REGARDLESS OF THE THEORY
35  * OF LIABILITY (CONTRACT, TORT OR OTHERWISE) AND EVEN IF NAPATECH HAS BEEN
36  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME JURISDICTIONS DO NOT ALLOW
37  * THE EXCLUSION OR LIMITATION OF LIABILITY FOR PERSONAL INJURY, OR OF
38  * INCIDENTAL OR CONSEQUENTIAL DAMAGES, SO THIS LIMITATION MAY NOT APPLY TO YOU.
39  *
40  *
41 
42  */
43 
44 /**
45  * @example flowmatch/flowmatch_example_main.cpp
46  * @section flowmatch_example_description Description
47  *
48  * This example covers a number flowmatching functionalities working together:
49  *
50  * 1. Set up and print statistics stream
51  * examples/flowmatch/learn_example_flowstat.cpp
52  *
53  * 2. Set up NTPL filters
54  * examples/flowmatch/learn_example_ntpl.cpp
55  *
56  * 3. Start threads to handle incoming data from MISS and UNHANDELED filters
57  * examples/flowmatch/learn_example_receiver.cpp
58  *
59  * 4. Transmit data in a number of simple scenarios
60  * examples/flowmatch/learn_example_main.cpp
61  *
62  * 5. Print flow info and status records
63  * examples/flowmatch/learn_example_receiver.cpp
64  *
65  * For the purpose of self-containment, this example also implements the
66  * functionality for transmitting data which you will find in:
67  * examples/flowmatch/learn_example_transmitter.cpp
68  * However, that functionality is outside the scope of the flowmatcher,
69  * and other and better examples exists for transmitting data.
70  *
71  * Finally, a handful of helper functions exists in:
72  * examples/flowmatch/learn_example_helper.cpp
73  * Just like the transmitter functionality, the helper functions is outside
74  * the scope of the flowmatcher, and can safely be ignored when working to
75  * understand the flowmatcher functionality.
76  *
77  * The example program uses the following NTAPI functions:
78  * - @ref NT_Init()
79  * - @ref NT_Done()
80  * - @ref NT_StatOpen()
81  * - @ref NT_StatRead()
82  * - @ref NT_StatClose()
83  * - @ref NT_ExplainError()
84  * - @ref NT_NTPL()
85  * - @ref NT_ConfigOpen()
86  * - @ref NT_ConfigRead()
87  * - @ref NT_ConfigWrite()
88  * - @ref NT_ConfigClose()
89  * - @ref NT_FlowOpenAttrInit()
90  * - @ref NT_FlowOpenAttrSetAdapterNo()
91  * - @ref NT_FlowOpen_Attr()
92  * - @ref NT_FlowClose()
93  * - @ref NT_FlowStatusRead()
94  * - @ref NT_FlowRead()
95  * - @ref NT_FlowWrite()
96  * - @ref NT_NetRxOpen()
97  * - @ref NT_NetRxClose()
98  * - @ref NT_NetRxGetNextPacket()
99  * - @ref NT_NetTxOpen()
100  * - @ref NT_NetTxClose()
101  * - @ref NT_NetTxGet()
102  * - @ref NT_NetTxRelease()
103  * - @ref _NT_NET_GET_PKT_DESCR_PTR_DYN3()
104  *
105  * This example is build for adapters with flowmatcher functionality,
106  * and with at least 2 ports.
107  */
108 
109 // Include this in order to access the Napatech API
110 #include <nt.h>
111 
112 #include <atomic>
113 #include <cstdint>
114 #include <iostream>
115 #include <thread>
116 
119 #include "flowmatch_example_ntpl.h"
122 
123 namespace {
124 
125 constexpr size_t TxPkgSize = 128;
126 
127 constexpr uint8_t IPv4Package1[] = {
128  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
129  0x08, 0x00, 0x45, 0x00, 0x00, 0x2d, 0x00, 0x01, 0x00, 0x00, 0x40, 0x06,
130  0x5c, 0x67, 0x0a, 0x0a, 0x05, 0x0f, 0x0a, 0x0a, 0x05, 0x41, 0x27, 0x10,
131  0x23, 0x28, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, 0x02,
132  0x20, 0x00, 0x00, 0x7d, 0x00, 0x00, 0x62, 0x62, 0x62, 0x62, 0x62};
133 
134 constexpr uint8_t IPv4Package2[] = {
135  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
136  0x08, 0x00, 0x45, 0x00, 0x00, 0x2d, 0x00, 0x01, 0x00, 0x00, 0x40, 0x06,
137  0x5c, 0x67, 0x0a, 0x0a, 0x15, 0x0f, 0x0a, 0x0a, 0x05, 0x51, 0x27, 0x10,
138  0x23, 0x28, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, 0x02,
139  0x20, 0x00, 0x00, 0x7d, 0x00, 0x00, 0x62, 0x62, 0x62, 0x62, 0x62};
140 
141 constexpr uint8_t IPv6Package[] = {
142  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
143  0x86, 0xdd, 0x60, 0x00, 0x00, 0x00, 0x00, 0x19, 0x06, 0x40, 0xde, 0xad,
144  0xbe, 0xef, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
145  0x00, 0x10, 0xde, 0xad, 0xbe, 0xef, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
146  0x00, 0x00, 0x00, 0x01, 0x00, 0x65, 0x27, 0x10, 0x23, 0x28, 0x00, 0x00,
147  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, 0x02, 0x20, 0x00, 0xe3, 0x2e,
148  0x00, 0x00, 0x62, 0x62, 0x62, 0x62, 0x62};
149 
150 } // Unnamed namespace
151 
152 int main(int, char**)
153 {
154  std::atomic<uint64_t> ipv4Counter { 0U };
155  std::atomic<uint64_t> ipv6Counter { 0U };
156  std::atomic<uint64_t> unhandledCounter { 0U };
157 
158  NtStatStream_t hStatStream;
159 
161  handleErrorStatus(status, "NT_Init() failed");
162 
163  setHostLoopback(0, true);
164  setHostLoopback(1, true);
165 
166  openAndClearFlowStats(hStatStream);
167 
169 
170  // Create the flow handling threads:
171  // - receive packets not matched by the Flow matcher
172  // - program new flows into the flow matcher
173  // - read flow information statistics
174  std::thread receiverThread1(taskReceiverMiss, "flowmatch_example_receiver_net_rx_miss", STREAM_ID_MISS, &ipv4Counter, &ipv6Counter);
175  std::thread receiverThread2(taskReceiverCounter, "flowmatch_example_receiver_net_rx_unhandled", STREAM_ID_UNHA, &unhandledCounter);
176 
177  // Test 1:
178  // First send 1 IPv4 package for learning, then send 100000 packages on
179  // port 0 which will "hit" the learned flow.
180  // CounterSetA packages: 100000
181  // CounterSetB packages: 0
182  taskTransmitter(0, IPv4Package1, sizeof(IPv4Package1), TxPkgSize, 1, 0, false);
183  taskTransmitter(0, IPv4Package1, sizeof(IPv4Package1), TxPkgSize, 100000, 0, false);
184 
185  // Test 2:
186  // Send 10 IPv6 packages on both adapter port 0 and 1.
187  // This very first package is a miss, and will be received by the miss thread,
188  // after that the remaining 19 packages will be dropped by the filters.
189  // CounterSetA packages: 9
190  // CounterSetB packages: 10
191  taskTransmitter(0, IPv6Package, sizeof(IPv6Package), TxPkgSize, 10, 10, false);
192  taskTransmitter(1, IPv6Package, sizeof(IPv6Package), TxPkgSize, 10, 10, true);
193 
194  // Test 3:
195  // Send 5 IPv4 packages without delay and then 5 packages with delay.
196  // The application will receive the first 5 packages before is has the chance
197  // to learn the flow. This will cause the software to attempt to learn the
198  // flow 5 times; 1 will be successful and 4 will be ignored.
199  // With default ntservice.ini settings, the 4 ignored learns will generate
200  // flow status records which are printed.
201  // CounterSetA packages: 0
202  // CounterSetB packages: 5
203  taskTransmitter(0, IPv4Package2, sizeof(IPv4Package2), TxPkgSize, 5, 0, false);
204  taskTransmitter(1, IPv4Package2, sizeof(IPv4Package2), TxPkgSize, 5, 10, true);
205 
206  applicationStop();
207 
208  receiverThread2.join();
209  receiverThread1.join();
210 
211  printAndCloseFlowStats(hStatStream);
212 
213  std::cout << std::endl
214  << "Total received IPv4 packages: " << ipv4Counter << std::endl
215  << "Total received IPv6 packages: " << ipv6Counter << std::endl
216  << "Total received unhandled packages: " << unhandledCounter << std::endl;
217 
218  setHostLoopback(0, false);
219  setHostLoopback(1, false);
220 
221  NT_Done();
222 
223  return 0;
224 }