stat_example.c Source File

Reference Documentation

Platform
Napatech SmartNIC
Content Type
Reference Information
Capture Software Version
Link™ Capture Software 12.15
Napatech Software Suite: examples/stat/stat_example.c Source File
stat_example.c
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 /**
44  * @example stat/stat_example.c
45  * @section stat_example_description Description
46  *
47  * This source file is an example of how to use the @ref StatStream
48  * "statistics stream" interface in NTAPI.
49  *
50  * The following NTAPI functions are used:
51  * - @ref NT_Init()
52  * - @ref NT_StatOpen()
53  * - @ref NT_StatRead()
54  * - @ref NT_StatClose()
55  * - @ref NT_Done()
56  * - @ref NT_ExplainError()
57  *
58  * <hr>
59  * @section stat_example_prerequisites Prerequisites
60  * A working system is needed.
61  *
62  * @section stat_example_flow Program flow
63  * @{
64  * The following is required to use the @ref StatStream
65  * "statistics stream" interface in NTAPI:
66  * - \#include/nt.h - Applications/Tools only need to include @ref
67  * nt.h to obtain prototypes, macros etc. from NTAPI.
68  * - @ref NT_Init(@ref NTAPI_VERSION) - Initialize the NTAPI
69  * library. @ref NTAPI_VERSION is a define that describes the version
70  * of the API described in the header files included by @ref
71  * nt.h. NT_Init() will ask the NTAPI library to convert return data
72  * to the @ref NTAPI_VERSION if possible. This will ensure that
73  * applications can run on NTAPI libraries of newer versions.
74  * - @ref NT_StatOpen() - Open an statistics stream.
75  * - @ref NT_StatRead() - Read statistics with clear=1 to clear the statistics counters for the current stream.
76  * - @ref NT_StatRead() - Read statistics with clear=0 to obtain new statistics counters after the reset.
77  * - @ref NT_StatClose() - Close the stream when terminating.
78  * - @ref NT_Done() - Close down the NTAPI library.
79  * - @ref NT_ExplainError() - Explain an error code returned by NTAPI functions.
80  *
81  *<hr>
82  * @}
83  */
84 
85 // Include this in order to access the Napatech API
86 #include <nt.h>
87 
88 #if defined(__linux__) || defined(__FreeBSD__)
89  #include <unistd.h> // sleep()
90 #elif defined(WIN32) || defined (WIN64)
91  #include <winsock2.h> // Sleep()
92 #endif
93 
94 int main(void)
95 {
96  NtStatStream_t hStatStream; // Statistics stream handle
97  NtStatistics_t hStat; // Stat handle.
98  char errorBuffer[NT_ERRBUF_SIZE]; // Error buffer
99  int status; // Status variable
100  unsigned i;
101 
102  // Initialize the NTAPI library and thereby check if NTAPI_VERSION can be used together with this library
103  if ((status = NT_Init(NTAPI_VERSION)) != NT_SUCCESS) {
104  // Get the status code as text
105  NT_ExplainError(status, errorBuffer, sizeof(errorBuffer));
106  fprintf(stderr, "NT_Init() failed: %s\n", errorBuffer);
107  return -1;
108  }
109 
110  // Open the stat stream
111  if ((status = NT_StatOpen(&hStatStream, "ExampleStat")) != NT_SUCCESS) {
112  // Get the status code as text
113  NT_ExplainError(status, errorBuffer, sizeof(errorBuffer));
114  fprintf(stderr, "NT_StatOpen() failed: %s\n", errorBuffer);
115  return -1;
116  }
117 
118  // Read the statistics counters to clear the statistics
119  // This is an optional step. If omitted, the adapter will show statistics form the start of ntservice.
121  hStat.u.query_v4.poll=0; // Wait for a new set
122  hStat.u.query_v4.clear=1; // Clear statistics
123  if ((status = NT_StatRead(hStatStream, &hStat)) != NT_SUCCESS) {
124  // Get the status code as text
125  NT_ExplainError(status, errorBuffer, sizeof(errorBuffer));
126  fprintf(stderr, "NT_StatRead() failed: %s\n", errorBuffer);
127  return -1;
128  }
129 
130  // Read new statistics for 10 seconds
131  printf("Statistics for port 0 the next 10 seconds.\n");
132  printf("--------------------------------------------------------------------------------\n");
133  for (i = 0; i < 10; i++) {
135  hStat.u.query_v4.poll=1; // The the current counters
136  hStat.u.query_v4.clear=0; // Do not clear statistics
137  if ((status = NT_StatRead(hStatStream, &hStat)) != NT_SUCCESS) {
138  // Get the status code as text
139  NT_ExplainError(status, errorBuffer, sizeof(errorBuffer));
140  fprintf(stderr, "NT_StatRead() failed: %s\n", errorBuffer);
141  return -1;
142  }
143 
144  // Print the RMON1 pkts and octets counters
145  if (hStat.u.query_v4.data.port.aPorts[0].rx.valid.RMON1) {
146  printf("%2d: Port0 RX RMON1 pkts: %016llx, octets: %016llx\n",
147  i, (unsigned long long) hStat.u.query_v4.data.port.aPorts[0].rx.RMON1.pkts, (unsigned long long) hStat.u.query_v4.data.port.aPorts[0].rx.RMON1.octets);
148  } else {
149  printf("Port0 doesn't support RMON1 RX counters.\n");
150  }
151  if (hStat.u.query_v4.data.port.aPorts[0].tx.valid.RMON1) {
152  printf(" Tx RMON1 pkts: %016llx, octets: %016llx\n",
153  (unsigned long long) hStat.u.query_v4.data.port.aPorts[0].tx.RMON1.pkts, (unsigned long long) hStat.u.query_v4.data.port.aPorts[0].tx.RMON1.octets);
154  } else {
155  printf("Port0 doesn't support RMON1 TX counters.\n");
156  }
157  printf("--------------------------------------------------------------------------------\n");
158 
159  // Sleep 1 sec
160 #if defined(__linux__) || defined(__FreeBSD__)
161  sleep(1);
162 #elif defined(WIN32) || defined (WIN64)
163  Sleep(1000); // sleep 1000 milliseconds = 1 second
164 #endif
165  }
166 
167  // Close the stat stream
168  if ((status = NT_StatClose(hStatStream)) != NT_SUCCESS) {
169  // Get the status code as text
170  NT_ExplainError(status, errorBuffer, sizeof(errorBuffer));
171  fprintf(stderr, "NT_StatClose() failed: %s\n", errorBuffer);
172  return -1;
173  }
174 
175  // Close down the NTAPI library
176  NT_Done();
177 
178  printf("Done.\n");
179  return 0;
180 }