transmit_pcap_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_pcap/transmit_pcap_example.c Source File
transmit_pcap_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_pcap/transmit_pcap_example.c
46  * @section transmit_pcap_example_description Description
47  *
48  * This source file is an example of how to transmit PCAP packets using the
49  * packet interface in NTAPI. The example will transmit 2500000 packets with a
50  * size of 1024 bytes from port 0. The packet contains an incrementing 32bit
51  * pattern.
52  *
53  * PCAP can also be used with transmit on timestamp, or segment interfaces. See
54  * the respective examples to see how to use those interfaces. Note that for
55  * PCAP transmit on timestamp, the "forceTxOnTs" feature must be enabled.
56  *
57  * The following NTAPI functions are used:
58  * - @ref NT_Init()
59  * - @ref NT_NetTxOpen_v2()
60  * - @ref NT_NetTxGet()
61  * - @ref NT_NET_GET_PKT_L2_PTR()
62  * - @ref NT_NetTxRelease()
63  * - @ref NT_NetTxClose()
64  * - @ref NT_Done()
65  * - @ref NT_ExplainError()
66  *
67  * @section transmit_pcap_example_prerequisites Prerequisites
68  * - The ntservice.ini must have at least one HostBuffersTx defined. Below is
69  * an example of a minimum ini-file. It will create a 4MB TX hostbuffer from
70  * NUMA node 0.
71  *
72  * @code
73  * [System]
74  * TimestampFormat = NATIVE
75  *
76  * [Adapter0]
77  * AdapterType = NT20E2
78  * BusId = 00:0a:00.00
79  * HostBuffersTx = [1,4,0]
80  * @endcode
81  *
82  * @section transmit_pcap_example_flow Program flow
83  * @{
84  * The following is required to transmit packages:
85  * - \#include/nt.h - Applications/Tools only need to include @ref
86  * nt.h to obtain prototypes, macros etc. from NTAPI.
87  * - @ref NT_Init(@ref NTAPI_VERSION) - Initialize the NTAPI
88  * library. @ref NTAPI_VERSION is a define that describes the version
89  * of the API described in the header files included by @ref
90  * nt.h. NT_Init() will ask the NTAPI library to convert return data
91  * to the @ref NTAPI_VERSION if possible. This will ensure that
92  * applications can run on NTAPI libraries of newer versions.
93  * - @ref NT_NetTxOpen_v2() - Open a hostbuffer than can transmit packets to port 0.
94  * - @ref NT_NetTxGet() - Get an empty tx buffer. This will get a 296 byte
95  * wire length packet buffer that will be sent onto port 0 when
96  * released.
97  * - @ref NT_NET_GET_PKT_DESCR_PTR() is used to retrieve the descriptor
98  * - @ref NT_NET_GET_PKT_L2_PTR() is used to get the L2 pointer to the tx
99  * buffer, this is where the payload is placed.
100  * - @ref NT_NetTxRelease() - Release the tx packet buffer. Once a tx
101  * buffer is released it will be transmitted
102  * - @ref NT_NetTxClose() - Close the TX stream.
103  * - @ref NT_Done() - Close down the NTAPI library.
104  *
105  *<hr>
106  * @section transmit_pcap_example_code Code
107  * @}
108  */
109 
110 // Include this in order to access the Napatech API
111 #include <nt.h>
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 // This is the timespec for PCAP. For PCAP_NANO, the ts_usec field
119 // will instead of micro-seconds contain nanoseconds.
120 struct pcap_timespec_s {
121  uint32_t ts_sec;
122  uint32_t ts_usec;
123 };
124 
125 // Standard PCAP record header.
126 struct pcap_record_s {
127  struct pcap_timespec_s time;
128  uint32_t incl_len;
129  uint32_t orig_len;
130 };
131 
132 // printError is a simple convenience function for printing an NTAPI error
133 // message to stderr.
134 static void printError(const char *prefix, int errorCode) {
136  NT_ExplainError(errorCode, errorBuffer, sizeof errorBuffer);
137  fprintf(stderr, "%s: %s\n", prefix, errorBuffer);
138 }
139 
140 int main(void)
141 {
142  //
143  // Initialize the API. This also checks if we are compatible with the
144  // installed library version.
145  //
146  int status;
147 
148  if ((status = NT_Init(NTAPI_VERSION)) != NT_SUCCESS) {
149  printError("NT_Init() failed", status);
150  return -1;
151  }
152 
153  //
154  // Open a TX stream
155  //
156  NtNetStreamTx_t hNetTx;
157  NtNetTxAttr_t txAttr;
158  NT_NetTxOpenAttrSetName(&txAttr, "pcaptransmit_example_txstream");
159  NT_NetTxOpenAttrSetPortMask(&txAttr, (1<<PORT));
163 
164  if ((status = NT_NetTxOpen_Attr(&hNetTx, &txAttr)) != NT_SUCCESS) {
165  printError("NT_NetTxOpen_Attr() failed", status);
166  return -1;
167  }
168 
169  //
170  // Retrieve a packet buffer, fill it, and repeat until we have transmitted
171  // the requested amount of packets.
172  //
173  printf("Commencing transmission\n");
174 
175  NtNetBuf_t hNetBufTx;
176  int numPackets;
177 
178  for (numPackets = 0; numPackets < PACKETS; numPackets++) {
179 
180  // Get a packet TX buffer for this tx stream and port, without timeout
181  if ((status = NT_NetTxGet(hNetTx, &hNetBufTx, PORT, PACKET_SIZE,
182  NT_NETTX_PACKET_OPTION_DEFAULT, -1)) != NT_SUCCESS) {
183  printError("NT_NetTxGet() failed", status);
184  return -1;
185  }
186 
187  // Fill the packet with an incrementing payload. Note that this will result
188  // in a garbage ethernet frame.
189  uint32_t *ptr = (uint32_t*)NT_NET_GET_PKT_L2_PTR(hNetBufTx);
190  for (uint32_t i = 0; i < PACKET_SIZE/4; i++) {
191  *(ptr+i) = i;
192  }
193 
194  // Set the pcap header fields. If used in a transmit on timestamp context,
195  // the timeval fields must be set to a sensible value.
196  struct pcap_record_s *hdr = (struct pcap_record_s*)NT_NET_GET_PKT_DESCR_PTR(hNetBufTx);
197  hdr->incl_len = PACKET_SIZE;
198  hdr->orig_len = PACKET_SIZE;
199  hdr->time.ts_sec = 0;
200  hdr->time.ts_usec = 0;
201 
202  // Release the TX buffer to transmit the packet.
203  if ((status = NT_NetTxRelease(hNetTx, hNetBufTx)) != NT_SUCCESS) {
204  printError("NT_NetTxRelease() failed", status);
205  return -1;
206  }
207  }
208 
209  //
210  // Terminate
211  //
212  printf("Done: %d packets sent\n", numPackets);
213 
214  // Close the TX stream
215  NT_NetTxClose(hNetTx);
216 
217  // Close the API
218  NT_Done();
219 
220  return 0;
221 }