flowmatch_example_ntpl.cpp Source File

Reference Documentation

Platform
Napatech SmartNIC
Content Type
Reference Information
Capture Software Version
Link™ Capture Software 12.15
Napatech Software Suite: examples/flowmatch/flowmatch_example_ntpl.cpp Source File
flowmatch_example_ntpl.cpp
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2025 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 "NA-0009 Software License Agreement.pdf"
18  *
19  * 4. Redistributions of source code must retain this copyright notice,
20  * list of conditions and the following disclaimer.
21  *
22  * THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTIES, EXPRESS OR
23  * IMPLIED, AND NAPATECH DISCLAIMS ALL IMPLIED WARRANTIES INCLUDING ANY
24  * IMPLIED WARRANTY OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, OR OF
25  * FITNESS FOR A PARTICULAR PURPOSE. TO THE EXTENT NOT PROHIBITED BY
26  * APPLICABLE LAW, IN NO EVENT SHALL NAPATECH BE LIABLE FOR PERSONAL INJURY,
27  * OR ANY INCIDENTAL, SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES WHATSOEVER,
28  * INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF PROFITS, CORRUPTION OR
29  * LOSS OF DATA, FAILURE TO TRANSMIT OR RECEIVE ANY DATA OR INFORMATION,
30  * BUSINESS INTERRUPTION OR ANY OTHER COMMERCIAL DAMAGES OR LOSSES, ARISING
31  * OUT OF OR RELATED TO YOUR USE OR INABILITY TO USE NAPATECH SOFTWARE OR
32  * SERVICES OR ANY THIRD PARTY SOFTWARE OR APPLICATIONS IN CONJUNCTION WITH
33  * THE NAPATECH SOFTWARE OR SERVICES, HOWEVER CAUSED, REGARDLESS OF THE THEORY
34  * OF LIABILITY (CONTRACT, TORT OR OTHERWISE) AND EVEN IF NAPATECH HAS BEEN
35  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME JURISDICTIONS DO NOT ALLOW
36  * THE EXCLUSION OR LIMITATION OF LIABILITY FOR PERSONAL INJURY, OR OF
37  * INCIDENTAL OR CONSEQUENTIAL DAMAGES, SO THIS LIMITATION MAY NOT APPLY TO YOU.
38  *
39  *
40 
41  */
42 
43 // Include this in order to access the Napatech API
44 #include <nt.h>
45 
46 #include <iostream>
47 
49 
50 #include "flowmatch_example_ntpl.h"
51 
52 #define STR_INNER(A) #A
53 #define STR(A) STR_INNER(A)
54 
55 namespace {
56 
57 /**
58  * Wrapper for NT_NTPL. Handles errors if they occur.
59  */
60 void ntplCall(NtConfigStream_t& hCfgStream, const char* str)
61 {
62  std::cout << str << std::endl;
63 
64  NtNtplInfo_t ntplInfo;
65  int status = NT_NTPL(hCfgStream, str, &ntplInfo, NT_NTPL_PARSER_VALIDATE_NORMAL);
66  handleErrorStatus(status, "NT_NTPL() failed");
67 }
68 
69 } // Unnamed namespace
70 
72 {
73  int status;
74  NtConfigStream_t hCfgStream;
75 
76  std::cout << "NTPL filters:" << std::endl;
77 
78  // Open a configuration stream to assign a filter to a stream ID.
79  status = NT_ConfigOpen(&hCfgStream, "Learn_example_config");
80  handleErrorStatus(status, "NT_ConfigOpen() failed");
81 
82  // Deletion of filters and macros, and clear FPGA flow tables.
83  ntplCall(hCfgStream, "Delete = All");
84 
85  // KeyType Command for IPv4 and IPv6
86  // In this example swapable field values are chosen, denoted by sw_.
87  ntplCall(hCfgStream, "KeyType[Name=kt4] = {sw_32_32, sw_16_16}");
88  ntplCall(hCfgStream, "KeyType[Name=kt6] = {sw_128_128, sw_16_16}");
89 
90  // Define keys for IPv4/IPv6 using src addr, dst addr, src port, dst port
91  // For suffix Y's for LayerXHeader[X]/Y/Y is here equivalent to the bit-field sizes chosen for the KeyType
92  ntplCall(hCfgStream, "KeyDef[Name=kd4; KeyType=kt4; IpProtocolField=Outer] = (Layer3Header[12]/32/32, Layer4Header[0]/16/16)");
93  ntplCall(hCfgStream, "keydef[Name=kd6; KeyType=kt6; IpProtocolField=Outer] = (Layer3Header[8]/128/128, Layer4Header[0]/16/16)");
94 
95  // Shorthand for the checks used in these filters.
96  ntplCall(hCfgStream, "DefineMacro(\"LearnFilterCheck\", \"Port==$1 and Layer2Protocol==EtherII and Layer3Protocol==$2 and Layer4Protocol==tcp\")");
97 
98  // Create filters for not learned flows that should be send to the application.
99  // The colormask option is used to make it easy for the application to distinguish
100  // IPv4 and IPv6. Colormask could also be used to distinguish protocols and ports etc.
101  // The offset0 option is set to the position of the source IP addr.
102  // In this example descriptor DYN4 with ColorBits set to FlowID is used, this will
103  // cause the id, which is set for each individual learned flow, to be available
104  // in the packet descriptor field color1. This feature is only available for DYN4.
105  ntplCall(hCfgStream, "Assign[StreamId=" STR(STREAM_ID_MISS) "; Descriptor=DYN4, ColorBits=FlowID, Offset0=Layer3Header[12]; ColorMask=" STR(COLOR_IPV4) "] = LearnFilterCheck(0,ipv4) and Key(kd4, KeyID=" STR(KEY_ID_IPV4) ")==MISS");
106  ntplCall(hCfgStream, "Assign[StreamId=" STR(STREAM_ID_MISS) "; Descriptor=DYN4, ColorBits=FlowID, Offset0=Layer3Header[8]; ColorMask=" STR(COLOR_IPV6) "] = LearnFilterCheck(0,ipv6) and Key(kd6, KeyID=" STR(KEY_ID_IPV6) ")==MISS");
107  ntplCall(hCfgStream, "Assign[StreamId=" STR(STREAM_ID_MISS) "; Descriptor=DYN4, ColorBits=FlowID, Offset0=Layer3Header[12]; ColorMask=" STR(COLOR_IPV4) "] = LearnFilterCheck(1,ipv4) and Key(kd4, KeyID=" STR(KEY_ID_IPV4) ", FieldAction=Swap)==MISS");
108  ntplCall(hCfgStream, "Assign[StreamId=" STR(STREAM_ID_MISS) "; Descriptor=DYN4, ColorBits=FlowID, Offset0=Layer3Header[8]; ColorMask=" STR(COLOR_IPV6) "] = LearnFilterCheck(1,ipv6) and Key(kd6, KeyID=" STR(KEY_ID_IPV6) ", FieldAction=Swap)==MISS");
109 
110  // Create filters for unhandled packets, which will simply be counted by the application.
111  ntplCall(hCfgStream, "Assign[StreamId=" STR(STREAM_ID_UNHA) "] = LearnFilterCheck(0,ipv4) and Key(kd4, KeyID=" STR(KEY_ID_IPV4) ")==UNHANDLED");
112  ntplCall(hCfgStream, "Assign[StreamId=" STR(STREAM_ID_UNHA) "] = LearnFilterCheck(0,ipv6) and Key(kd6, KeyID=" STR(KEY_ID_IPV6) ")==UNHANDLED");
113  ntplCall(hCfgStream, "Assign[StreamId=" STR(STREAM_ID_UNHA) "] = LearnFilterCheck(1,ipv4) and Key(kd4, KeyID=" STR(KEY_ID_IPV4) ", FieldAction=Swap)==UNHANDLED");
114  ntplCall(hCfgStream, "Assign[StreamId=" STR(STREAM_ID_UNHA) "] = LearnFilterCheck(1,ipv6) and Key(kd6, KeyID=" STR(KEY_ID_IPV6) ", FieldAction=Swap)==UNHANDLED");
115 
116  // Create filters for learned flows. The packets will not be forwarded
117  // to the application, but will be tracked according to the option "CounterSet".
118  // When set, the "KeyId" option signals that the Flow Matcher rather than
119  // Key Matcher should be used for the test.
120  // Note that the CounterSet option given for the final Assign commands for
121  // each of the 4 filters, will overwrite previous CounterSet options.
122  ntplCall(hCfgStream, "Assign[StreamId=Drop] = LearnFilterCheck(0,ipv4) and Key(kd4, KeyID=" STR(KEY_ID_IPV4) ", CounterSet=CSA)==" STR(KEY_SET_ID));
123  ntplCall(hCfgStream, "Assign[StreamId=Drop] = LearnFilterCheck(0,ipv6) and Key(kd6, KeyID=" STR(KEY_ID_IPV6) ", CounterSet=CSA)==" STR(KEY_SET_ID));
124  ntplCall(hCfgStream, "Assign[StreamId=Drop] = LearnFilterCheck(1,ipv4) and Key(kd4, KeyID=" STR(KEY_ID_IPV4) ", CounterSet=CSB, FieldAction=Swap)==" STR(KEY_SET_ID));
125  ntplCall(hCfgStream, "Assign[StreamId=Drop] = LearnFilterCheck(1,ipv6) and Key(kd6, KeyID=" STR(KEY_ID_IPV6) ", CounterSet=CSB, FieldAction=Swap)==" STR(KEY_SET_ID));
126 
127  std::cout << std::endl;
128 
129  // Close configuration stream
130  status = NT_ConfigClose(hCfgStream);
131  handleErrorStatus(status, "NT_ConfigClose() failed");
132 }