hostbuffer_poll_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/net/hostbuffer_poll/hostbuffer_poll_example.c Source File
hostbuffer_poll_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 net/hostbuffer_poll_example.c
45  * @section hostbuffer_poll_example_description Description
46  *
47  * This source file is an example of how to poll for hostbuffers attached using NTAPI.
48  *
49  * The following NTAPI functions are used:
50  * - @ref NT_Init()
51  * - @ref NT_ConfigOpen()
52  * - @ref NT_NTPL()
53  * - @ref NT_ConfigClose()
54  * - @ref NT_NetRxOpen()
55  * - @ref NT_NetRxClose()
56  * - @ref NT_NetRxRead()
57  * - @ref NT_Done()
58  * - @ref NT_ExplainError()
59  *
60  */
61 
62 // Include this in order to access the Napatech API
63 #include <nt.h>
64 
65 #if defined(__linux__) || defined(__FreeBSD__)
66  #include <sys/time.h>
67  #include <unistd.h> // sleep()
68 #elif defined(WIN32) || defined (WIN64)
69  #include <winsock2.h>
70  #include <time.h>
71  #include <sys/timeb.h>
72 #endif
73 
74 #if defined(WIN32) || defined (WIN64)
75  #define snprintf(dst, ...) _snprintf_s((dst), _countof(dst), __VA_ARGS__)
76 
77  int gettimeofday(struct timeval *tv, struct timezone *tz);
78  void usleep(unsigned long usec);
79 
80 #endif
81 
82 int main(void)
83 {
84  char errorBuffer[NT_ERRBUF_SIZE]; // Error buffer
85  int status; // Status variable
86  NtNetStreamRx_t hNetRx; // Handle to the RX stream
87  NtConfigStream_t hCfgStream; // Handle to a config stream
88  NtNtplInfo_t ntplInfo; // Return data structure from the NT_NTPL() call.
89 
90  // Initialize the NTAPI library and thereby check if NTAPI_VERSION can be used together with this library
91  if ((status = NT_Init(NTAPI_VERSION)) != NT_SUCCESS) {
92  // Get the status code as text
93  NT_ExplainError(status, errorBuffer, sizeof(errorBuffer));
94  fprintf(stderr, "NT_Init() failed: %s\n", errorBuffer);
95  return -1;
96  }
97 
98  // Open a config stream to assign a filter to a stream ID.
99  if ((status = NT_ConfigOpen(&hCfgStream, "TestStream")) != NT_SUCCESS) {
100  // Get the status code as text
101  NT_ExplainError(status, errorBuffer, sizeof(errorBuffer));
102  fprintf(stderr, "NT_ConfigOpen() failed: %s\n", errorBuffer);
103  return -1;
104  }
105 
106  // Setup stream 0
107  if ((status = NT_NTPL(hCfgStream, "Assign[Streamid=0] = Port == 0", &ntplInfo, NT_NTPL_PARSER_VALIDATE_NORMAL)) != NT_SUCCESS) {
108  // Get the status code as text
109  NT_ExplainError(status, errorBuffer, sizeof(errorBuffer));
110  fprintf(stderr, "NT_NTPL() failed: %s\n", errorBuffer);
111  fprintf(stderr, ">>> NTPL errorcode: %X\n", ntplInfo.u.errorData.errCode);
112  fprintf(stderr, ">>> %s\n", ntplInfo.u.errorData.errBuffer[0]);
113  fprintf(stderr, ">>> %s\n", ntplInfo.u.errorData.errBuffer[1]);
114  fprintf(stderr, ">>> %s\n", ntplInfo.u.errorData.errBuffer[2]);
115  return -1;
116  }
117 
118  // Close the config stream
119  if ((status = NT_ConfigClose(hCfgStream)) != NT_SUCCESS) {
120  // Get the status code as text
121  NT_ExplainError(status, errorBuffer, sizeof(errorBuffer));
122  fprintf(stderr, "NT_ConfigClose() failed: %s\n", errorBuffer);
123  return -1;
124  }
125 
126  // Get a stream handle with stream ID 0. NT_NET_INTERFACE_PACKET
127  // specify that we will receive data in a packet based matter.
128  if ((status = NT_NetRxOpen(&hNetRx, "TestStream", NT_NET_INTERFACE_PACKET, 0, -1)) != NT_SUCCESS) {
129  // Get the status code as text
130  NT_ExplainError(status, errorBuffer, sizeof(errorBuffer));
131  fprintf(stderr, "NT_NetRxOpen() failed: %s\n", errorBuffer);
132  return -1;
133  }
134 
135  struct timeval begin, end;
136  gettimeofday(&begin, 0);
137 
138  // This loop will poll for the host buffer to be attached to the stream
139  NtNetRx_t rxCmd;
140  int loop_cnt = 200;
141  while (--loop_cnt) {
143  rxCmd.u.hbInfo.numAddedHostBuffers = 0;
144  if ((status = NT_NetRxRead(hNetRx, &rxCmd)) != 0) {
145  NT_ExplainError(status, errorBuffer, sizeof(errorBuffer));
146  fprintf(stderr, "NT_NetRxRead() failed: %s\n", errorBuffer);
147  break;
148  }
149  if (rxCmd.u.hbInfo.numAddedHostBuffers >= 1) {
150  gettimeofday(&end, 0);
151  long seconds = end.tv_sec - begin.tv_sec;
152  long microseconds = end.tv_usec - begin.tv_usec;
153  double elapsed = (double)seconds + (double)microseconds*1e-6;
154 
155  fprintf(stdout,"Hostbuffer attached, time elapsed = %.03f s\n", elapsed);
156  break;
157  }
158  usleep(100);
159  }
160  if (loop_cnt <= 0) {
161  fprintf(stdout, "ERR: %s: timeout - no host buffers attached\n", __func__);
162  }
163 
164  // Close the stream and release the hostbuffer. This will also remove the NTPL assignments performed.
165  NT_NetRxClose(hNetRx);
166 
167  // Open a config stream to delete a filter.
168  if ((status = NT_ConfigOpen(&hCfgStream, "TestStream")) != NT_SUCCESS) {
169  // Get the status code as text
170  NT_ExplainError(status, errorBuffer, sizeof(errorBuffer));
171  fprintf(stderr, "NT_ConfigOpen() failed: %s\n", errorBuffer);
172  return -1;
173  }
174 
175  if ((status = NT_NTPL(hCfgStream, "Delete = All", &ntplInfo, NT_NTPL_PARSER_VALIDATE_NORMAL)) != NT_SUCCESS) {
176  // Get the status code as text
177  NT_ExplainError(status, errorBuffer, sizeof(errorBuffer));
178  fprintf(stderr, "NT_NTPL() failed: %s\n", errorBuffer);
179  fprintf(stderr, ">>> NTPL errorcode: %X\n", ntplInfo.u.errorData.errCode);
180  fprintf(stderr, ">>> %s\n", ntplInfo.u.errorData.errBuffer[0]);
181  fprintf(stderr, ">>> %s\n", ntplInfo.u.errorData.errBuffer[1]);
182  fprintf(stderr, ">>> %s\n", ntplInfo.u.errorData.errBuffer[2]);
183  return -1;
184  }
185 
186  // Close the config stream
187  if ((status = NT_ConfigClose(hCfgStream)) != NT_SUCCESS) {
188  // Get the status code as text
189  NT_ExplainError(status, errorBuffer, sizeof(errorBuffer));
190  fprintf(stderr, "NT_ConfigClose() failed: %s\n", errorBuffer);
191  return -1;
192  }
193 
194  // Close down the NTAPI library
195  NT_Done();
196 
197  return 0;
198 }