flowmatch/flowmatch_example_main.cpp

Reference Documentation

Platform
IntelĀ® PAC
Napatech SmartNIC
Content Type
Reference Information
Napatech Software Suite: flowmatch/flowmatch_example_main.cpp
flowmatch/flowmatch_example_main.cpp

Description

This example covers a number flowmatching functionalities working together:

  1. Set up and print statistics stream examples/flowmatch/learn_example_flowstat.cpp
  2. Set up NTPL filters examples/flowmatch/learn_example_ntpl.cpp
  3. Start threads to handle incoming data from MISS and UNHANDELED filters examples/flowmatch/learn_example_receiver.cpp
  4. Transmit data in a number of simple scenarios examples/flowmatch/learn_example_main.cpp
  5. Print flow info and status records examples/flowmatch/learn_example_receiver.cpp

For the purpose of self-containment, this example also implements the functionality for transmitting data which you will find in: examples/flowmatch/learn_example_transmitter.cpp However, that functionality is outside the scope of the flowmatcher, and other and better examples exists for transmitting data.

Finally, a handful of helper functions exists in: examples/flowmatch/learn_example_helper.cpp Just like the transmitter functionality, the helper functions is outside the scope of the flowmatcher, and can safely be ignored when working to understand the flowmatcher functionality.

The example program uses the following NTAPI functions:

This example is build for adapters with flowmatcher functionality, and with at least 2 ports.

/*
*
* Copyright 2020 Napatech A/S. All Rights Reserved.
*
* 1. Copying, modification, and distribution of this file, or executable
* versions of this file, is governed by the terms of the Napatech Software
* license agreement under which this file was made available. If you do not
* agree to the terms of the license do not install, copy, access or
* otherwise use this file.
*
* 2. Under the Napatech Software license agreement you are granted a
* limited, non-exclusive, non-assignable, copyright license to copy, modify
* and distribute this file in conjunction with Napatech SmartNIC's and
* similar hardware manufactured or supplied by Napatech A/S.
*
* 3. The full Napatech Software license agreement is included in this
* distribution, please see "NP-0405 Napatech Software license
* agreement.pdf"
*
* 4. Redistributions of source code must retain this copyright notice,
* list of conditions and the following disclaimer.
*
* THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTIES, EXPRESS OR
* IMPLIED, AND NAPATECH DISCLAIMS ALL IMPLIED WARRANTIES INCLUDING ANY
* IMPLIED WARRANTY OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, OR OF
* FITNESS FOR A PARTICULAR PURPOSE. TO THE EXTENT NOT PROHIBITED BY
* APPLICABLE LAW, IN NO EVENT SHALL NAPATECH BE LIABLE FOR PERSONAL INJURY,
* OR ANY INCIDENTAL, SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES WHATSOEVER,
* INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF PROFITS, CORRUPTION OR
* LOSS OF DATA, FAILURE TO TRANSMIT OR RECEIVE ANY DATA OR INFORMATION,
* BUSINESS INTERRUPTION OR ANY OTHER COMMERCIAL DAMAGES OR LOSSES, ARISING
* OUT OF OR RELATED TO YOUR USE OR INABILITY TO USE NAPATECH SOFTWARE OR
* SERVICES OR ANY THIRD PARTY SOFTWARE OR APPLICATIONS IN CONJUNCTION WITH
* THE NAPATECH SOFTWARE OR SERVICES, HOWEVER CAUSED, REGARDLESS OF THE THEORY
* OF LIABILITY (CONTRACT, TORT OR OTHERWISE) AND EVEN IF NAPATECH HAS BEEN
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME JURISDICTIONS DO NOT ALLOW
* THE EXCLUSION OR LIMITATION OF LIABILITY FOR PERSONAL INJURY, OR OF
* INCIDENTAL OR CONSEQUENTIAL DAMAGES, SO THIS LIMITATION MAY NOT APPLY TO YOU.
*
*
*/
/**
* @example flowmatch/flowmatch_example_main.cpp
* @section flowmatch_example_description Description
*
* This example covers a number flowmatching functionalities working together:
*
* 1. Set up and print statistics stream
* examples/flowmatch/learn_example_flowstat.cpp
*
* 2. Set up NTPL filters
* examples/flowmatch/learn_example_ntpl.cpp
*
* 3. Start threads to handle incoming data from MISS and UNHANDELED filters
* examples/flowmatch/learn_example_receiver.cpp
*
* 4. Transmit data in a number of simple scenarios
* examples/flowmatch/learn_example_main.cpp
*
* 5. Print flow info and status records
* examples/flowmatch/learn_example_receiver.cpp
*
* For the purpose of self-containment, this example also implements the
* functionality for transmitting data which you will find in:
* examples/flowmatch/learn_example_transmitter.cpp
* However, that functionality is outside the scope of the flowmatcher,
* and other and better examples exists for transmitting data.
*
* Finally, a handful of helper functions exists in:
* examples/flowmatch/learn_example_helper.cpp
* Just like the transmitter functionality, the helper functions is outside
* the scope of the flowmatcher, and can safely be ignored when working to
* understand the flowmatcher functionality.
*
* The example program uses the following NTAPI functions:
* - @ref NT_Init()
* - @ref NT_Done()
* - @ref NT_StatOpen()
* - @ref NT_StatRead()
* - @ref NT_StatClose()
* - @ref NT_ExplainError()
* - @ref NT_NTPL()
* - @ref NT_ConfigOpen()
* - @ref NT_ConfigRead()
* - @ref NT_ConfigWrite()
* - @ref NT_ConfigClose()
* - @ref NT_FlowOpenAttrInit()
* - @ref NT_FlowOpenAttrSetAdapterNo()
* - @ref NT_FlowOpen_Attr()
* - @ref NT_FlowClose()
* - @ref NT_FlowStatusRead()
* - @ref NT_FlowRead()
* - @ref NT_FlowWrite()
* - @ref NT_NetRxOpen()
* - @ref NT_NetRxClose()
* - @ref NT_NetRxGetNextPacket()
* - @ref NT_NetTxOpen()
* - @ref NT_NetTxClose()
* - @ref NT_NetTxGet()
* - @ref NT_NetTxRelease()
* - @ref _NT_NET_GET_PKT_DESCR_PTR_DYN3()
*
* This example is build for adapters with flowmatcher functionality,
* and with at least 2 ports.
*/
#include <atomic>
#include <cstdint>
#include <iostream>
#include <thread>
#include <nt.h>
namespace {
constexpr size_t TxPkgSize = 128;
constexpr uint8_t IPv4Package1[] = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x08, 0x00, 0x45, 0x00, 0x00, 0x2d, 0x00, 0x01, 0x00, 0x00, 0x40, 0x06,
0x5c, 0x67, 0x0a, 0x0a, 0x05, 0x0f, 0x0a, 0x0a, 0x05, 0x41, 0x27, 0x10,
0x23, 0x28, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, 0x02,
0x20, 0x00, 0x00, 0x7d, 0x00, 0x00, 0x62, 0x62, 0x62, 0x62, 0x62};
constexpr uint8_t IPv4Package2[] = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x08, 0x00, 0x45, 0x00, 0x00, 0x2d, 0x00, 0x01, 0x00, 0x00, 0x40, 0x06,
0x5c, 0x67, 0x0a, 0x0a, 0x15, 0x0f, 0x0a, 0x0a, 0x05, 0x51, 0x27, 0x10,
0x23, 0x28, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, 0x02,
0x20, 0x00, 0x00, 0x7d, 0x00, 0x00, 0x62, 0x62, 0x62, 0x62, 0x62};
constexpr uint8_t IPv6Package[] = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x86, 0xdd, 0x60, 0x00, 0x00, 0x00, 0x00, 0x19, 0x06, 0x40, 0xde, 0xad,
0xbe, 0xef, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
0x00, 0x10, 0xde, 0xad, 0xbe, 0xef, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x01, 0x00, 0x65, 0x27, 0x10, 0x23, 0x28, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, 0x02, 0x20, 0x00, 0xe3, 0x2e,
0x00, 0x00, 0x62, 0x62, 0x62, 0x62, 0x62};
} // Unnamed namespace
int main(int, char**)
{
std::atomic<uint64_t> ipv4Counter { 0U };
std::atomic<uint64_t> ipv6Counter { 0U };
std::atomic<uint64_t> unhandledCounter { 0U };
NtStatStream_t hStatStream;
handleErrorStatus(status, "NT_Init() failed");
setHostLoopback(0, true);
setHostLoopback(1, true);
openAndClearFlowStats(hStatStream);
// Create the flow handling threads:
// - receive packets not matched by the Flow matcher
// - program new flows into the flow matcher
// - read flow information statistics
std::thread receiverThread1(taskReceiverMiss, "flowmatch_example_receiver_net_rx_miss", STREAM_ID_MISS, &ipv4Counter, &ipv6Counter);
std::thread receiverThread2(taskReceiverCounter, "flowmatch_example_receiver_net_rx_unhandled", STREAM_ID_UNHA, &unhandledCounter);
// Test 1:
// First send 1 IPv4 package for learning, then send 100000 packages on
// port 0 which will "hit" the learned flow.
// CounterSetA packages: 100000
// CounterSetB packages: 0
taskTransmitter(0, IPv4Package1, sizeof(IPv4Package1), TxPkgSize, 1, 0, false);
taskTransmitter(0, IPv4Package1, sizeof(IPv4Package1), TxPkgSize, 100000, 0, false);
// Test 2:
// Send 10 IPv6 packages on both adapter port 0 and 1.
// This very first package is a miss, and will be received by the miss thread,
// after that the remaining 19 packages will be dropped by the filters.
// CounterSetA packages: 9
// CounterSetB packages: 10
taskTransmitter(0, IPv6Package, sizeof(IPv6Package), TxPkgSize, 10, 10, false);
taskTransmitter(1, IPv6Package, sizeof(IPv6Package), TxPkgSize, 10, 10, true);
// Test 3:
// Send 5 IPv4 packages without delay and then 5 packages with delay.
// The application will receive the first 5 packages before is has the chance
// to learn the flow. This will cause the software to attempt to learn the
// flow 5 times; 1 will be successful and 4 will be ignored.
// With default ntservice.ini settings, the 4 ignored learns will generate
// flow status records which are printed.
// CounterSetA packages: 0
// CounterSetB packages: 5
taskTransmitter(0, IPv4Package2, sizeof(IPv4Package2), TxPkgSize, 5, 0, false);
taskTransmitter(1, IPv4Package2, sizeof(IPv4Package2), TxPkgSize, 5, 10, true);
receiverThread2.join();
receiverThread1.join();
printAndCloseFlowStats(hStatStream);
std::cout << std::endl
<< "Total received IPv4 packages: " << ipv4Counter << std::endl
<< "Total received IPv6 packages: " << ipv6Counter << std::endl
<< "Total received unhandled packages: " << unhandledCounter << std::endl;
setHostLoopback(0, false);
setHostLoopback(1, false);
return 0;
}