transmit_packet_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/transmit_packet/transmit_packet_example.c Source File
transmit_packet_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/transmit_packet/transmit_packet_example.c
46  * @section transmit_packet_example_description Description
47  *
48  * This source file is an example of how to transmit packets using the packet
49  * interface in NTAPI. The example will transmit 2500000 packets with a size of
50  * 1024 bytes from port 0. The packet contains an incrementing 32bit pattern.
51  *
52  * The following NTAPI functions are used:
53  * - @ref NT_Init()
54  * - @ref NT_NetTxOpen()
55  * - @ref NT_NetTxGet()
56  * - @ref NT_NET_GET_PKT_L2_PTR()
57  * - @ref NT_NetTxRelease()
58  * - @ref NT_NetTxClose()
59  * - @ref NT_Done()
60  * - @ref NT_ExplainError()
61  *
62  * @section transmit_packet_example_prerequisites Prerequisites
63  * - The ntservice.ini must have at least one HostBuffersTx defined. Below is
64  * an example of a minimum ini-file. It will create a 4MB TX hostbuffer from
65  * NUMA node 0.
66  *
67  * @code
68  * [System]
69  * TimestampFormat = NATIVE
70  *
71  * [Adapter0]
72  * AdapterType = NT20E2
73  * BusId = 00:0a:00.00
74  * HostBuffersTx = [1,4,0]
75  * @endcode
76  *
77  * @section transmit_packet_example_flow Program flow
78  * @{
79  * The following is required to transmit packages:
80  * - \#include/nt.h - Applications/Tools only need to include @ref
81  * nt.h to obtain prototypes, macros etc. from NTAPI.
82  * - @ref NT_Init(@ref NTAPI_VERSION) - Initialize the NTAPI
83  * library. @ref NTAPI_VERSION is a define that describes the version
84  * of the API described in the header files included by @ref
85  * nt.h. NT_Init() will ask the NTAPI library to convert return data
86  * to the @ref NTAPI_VERSION if possible. This will ensure that
87  * applications can run on NTAPI libraries of newer versions.
88  * - @ref NT_NetTxOpen() - Open a hostbuffer than can transmit packets to port 0.
89  * - @ref NT_NetTxGet() - Get an empty tx buffer. This will get a 296 byte
90  * wire length packet buffer that will be sent onto port 0 when
91  * released.
92  * - @ref NT_NET_GET_PKT_L2_PTR() is used to get the L2 pointer to the tx
93  * buffer, this is where the payload is placed.
94  * - @ref NT_NetTxRelease() - Release the tx packet buffer. Once a tx
95  * buffer is released it will be transmitted
96  * - @ref NT_NetTxClose() - Close the TX stream.
97  * - @ref NT_Done() - Close down the NTAPI library.
98  *
99  *<hr>
100  * @section transmit_packet_example_code Code
101  * @}
102  */
103 
104 // Include this in order to access the Napatech API
105 #include <nt.h>
106 
107 #ifdef WIN32
108  #include <windows.h>
109 #else
110  #include <unistd.h>
111 #endif
112 
113 // default TX packet test setup
114 #define PACKETS 2500000
115 #define PACKET_SIZE 1024 // Packet size to transmit (incl crc.)
116 #define PORT 0
117 
118 
119 static void msleep(int ms)
120 {
121 #ifdef _WIN32
122  Sleep(ms);
123 #else
124  usleep(ms * 1000);
125 #endif
126 }
127 
128 // printError is a simple convenience function for printing an NTAPI error
129 // message to stderr.
130 static void printError(const char *prefix, int errorCode) {
132  NT_ExplainError(errorCode, errorBuffer, sizeof errorBuffer);
133  fprintf(stderr, "%s: %s\n", prefix, errorBuffer);
134 }
135 
136 int main(void)
137 {
138  //
139  // Initialize the API. This also checks if we are compatible with the
140  // installed library version.
141  //
142  int status;
143 
144  if ((status = NT_Init(NTAPI_VERSION)) != NT_SUCCESS) {
145  printError("NT_Init() failed", status);
146  return -1;
147  }
148 
149  //
150  // Open a TX stream
151  //
152  NtNetStreamTx_t hNetTx;
153 
154  status = NT_NetTxOpen(&hNetTx, "transmit_packet_example_txstream",
155  1ULL << PORT, NT_NETTX_NUMA_ANY_HB, 0);
156  if (status != NT_SUCCESS) {
157  printError("NT_NetTxOpen() failed", status);
158  return -1;
159  }
160 
161  //
162  // Retrieve a packet buffer, fill it, and repeat until we have transmitted
163  // the requested amount of packets.
164  //
165  printf("Commencing transmission\n");
166 
167  NtNetBuf_t hNetBufTx;
168  int numPackets;
169 
170  for (numPackets = 0; numPackets < PACKETS; numPackets++) {
171 
172  // Get a packet TX buffer for this tx stream and port, without timeout
173  if ((status = NT_NetTxGet(hNetTx, &hNetBufTx, PORT, PACKET_SIZE,
174  NT_NETTX_PACKET_OPTION_DEFAULT, -1)) != NT_SUCCESS) {
175  printError("NT_NetTxGet() failed", status);
176  return -1;
177  }
178 
179  // Fill the packet with an incrementing payload. Note that this will result
180  // in a garbage ethernet frame.
181  uint32_t *ptr = (uint32_t*)NT_NET_GET_PKT_L2_PTR(hNetBufTx);
182  for (uint32_t i = 0; i < PACKET_SIZE/4; i++) {
183  *(ptr+i) = i;
184  }
185 
186  // Release the TX buffer to transmit the packet.
187  if ((status = NT_NetTxRelease(hNetTx, hNetBufTx)) != NT_SUCCESS) {
188  printError("NT_NetTxRelease() failed", status);
189  return -1;
190  }
191  }
192 
193  //Wait until all packet have been delivered to the FPGA and this when all
194  //packets have left host memory
195  int timeOut = 0;
196 
197  while (true) {
198  if (timeOut >= 1000) { //Wait max 1 second
199  printError("Timeout waiting for data to be sent", NT_ERROR_OPERATION_TIMEOUT);
200  break;
201  }
202 
203  // Evaluate if hostbuffer contents have been delivered to FPGA
204  // This is used to detect when Tx data has left host memory
205  NtNetTx_t ntNetTx;
207  status = NT_NetTxRead(hNetTx, &ntNetTx);
208 
209  if (status != NT_SUCCESS) {
210  printError("NT_NetTxRead failed", status);
211  break;
212  }
213 
214  size_t nHbSizeTotal = ntNetTx.u.hbInfo.aHostBuffer[0].size;
215  size_t nHbSizeAvail = ntNetTx.u.hbInfo.aHostBuffer[0].available;
216  size_t nHbSizeRel = ntNetTx.u.hbInfo.aHostBuffer[0].released;
217  size_t nHbSizeDeq = ntNetTx.u.hbInfo.aHostBuffer[0].dequeued;
218 
219  if ((nHbSizeAvail == nHbSizeTotal) && (nHbSizeRel == 0) && (nHbSizeDeq == 0))
220  break;
221 
222  msleep(1); // Dont busy wait but sleep 1 ms
223  timeOut++;
224  };
225 
226  //printf("timeOut = %dms\n", timeOut); //Find timeout value
227  printf("Done: %d packets sent\n", numPackets);
228 
229  // Close the TX stream
230  NT_NetTxClose(hNetTx);
231 
232  // Close the API
233  NT_Done();
234 
235  return 0;
236 }