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