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