transmit_on_timestamp_setclock_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_on_timestamp_setclock/transmit_on_timestamp_setclock_example.c Source File
transmit_on_timestamp_setclock_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_on_timestamp_setclock/transmit_on_timestamp_setclock_example.c
46  * @section transmit_on_timestamp_setclock_example_description Description
47  *
48  * This source file is an example of how to transmit a sequence of packets
49  * on timestamp multiple times, but instead of manually reconfiguring the
50  * delta for each iteration, TXSETCLOCK is used to advance the delta with
51  * the duration of a single replay iteration. An example application of this
52  * functionality would be where one wishes to replay a single capture file
53  * multiple times.
54  *
55  * This example will transmit 100000 packets of 296 bytes from port 0 at a rate
56  * of approximately 5 Mb/s. The packet contains an incrementing 32bit pattern.
57  *
58  * The transmit on timestamp functionality is also available in segment mode.
59  * See the @ref net/transmit_segment/transmit_segment_example.c example code
60  * to see how to use the segment interface.
61  *
62  * The following NTAPI functions are used:
63  * - @ref NT_Init()
64  * - @ref NT_ConfigOpen()
65  * - @ref NT_ConfigRead()
66  * - @ref NT_ConfigWrite()
67  * - @ref NT_ConfigClose()
68  * - @ref NT_NetTxOpen_v2()
69  * - @ref NT_NetTxGet()
70  * - @ref NT_NET_SET_PKT_TIMESTAMP()
71  * - @ref NT_NET_SET_PKT_TXSETCLOCK()
72  * - @ref NT_NET_GET_PKT_L2_PTR()
73  * - @ref NT_NetTxRelease()
74  * - @ref NT_NetTxClose()
75  * - @ref NT_Done()
76  * - @ref NT_ExplainError()
77  *
78  * @section transmit_on_timestamp_setclock_example_prerequisites Prerequisites
79  * - This example is only compatible with adapters supporting 4GA transmit on
80  * timestamp.
81  * - The ntservice.ini must have at least one HostBuffersTx defined. Below is
82  * an example of a minimum ini-file. It will create a 4MB TX hostbuffer from
83  * NUMA node 0.
84  *
85  * @code
86  * [Adapter0]
87  * AdapterType = NT20E2
88  * BusId = 00:0a:00.00
89  * HostBuffersTx = [1,4,0]
90  * @endcode
91  *
92  * @section transmit_on_timestamp_setclock_example_flow Program flow
93  * @{
94  * The following is required to transmit packages:
95  * - \#include/nt.h - Applications/Tools only need to include @ref
96  * nt.h to obtain prototypes, macros etc. from NTAPI.
97  * - @ref NT_Init(@ref NTAPI_VERSION) - Initialize the NTAPI
98  * library. @ref NTAPI_VERSION is a define that describes the version
99  * of the API described in the header files included by @ref
100  * nt.h. NT_Init() will ask the NTAPI library to convert return data
101  * to the @ref NTAPI_VERSION if possible. This will ensure that
102  * applications can run on NTAPI libraries of newer versions.
103  * - @ref NT_ConfigOpen() - Open the config stream.
104  * - @ref NT_ConfigRead() - Use the config stream to read the current adapter
105  * time.
106  * - @ref NT_ConfigWrite() - Use the config stream to configure transmit on
107  * timestamp, using the retrieved adapter time.
108  * - @ref NT_NetTxOpen() - Open a hostbuffer than can transmit packets to port 0.
109  * - @ref NT_NetTxOpen_v2() - Open a hostbuffer than can transmit packets to port 0.
110  * - @ref NT_NetTxGet() - Get an empty tx buffer. This will get a 296 byte
111  * wire length packet buffer that will be sent onto port 0 when the packet
112  * buffer is released, and the timestamp has been reached on the adapter.
113  * - @ref NT_NET_GET_PKT_L2_PTR() is used to get the L2 pointer to the tx
114  * buffer, this is where the payload is placed.
115  * - @ref NT_NET_SET_PKT_TXSETCLOCK() is used to enable txsetclock on the
116  * first packet in the replay sequence.
117  * - @ref NT_NET_SET_PKT_TIMESTAMP() is used to set the timestamp of the
118  * packet
119  * - @ref NT_NetTxRelease() - Release the tx packet buffer. Once a tx
120  * buffer is released it will be transmitted
121  * - @ref NT_ConfigRead() - Read the adapter timestamp to check if the last
122  * packet should have been transmitted. We need to do this in order to avoid
123  * disabling transmit on timestamp too early.
124  * - @ref NT_ConfigWrite() - Disable transmit on timestamp.
125  * - @ref NT_ConfigClose() - Close the configuration stream.
126  * - @ref NT_NetTxClose() - Close the TX stream.
127  * - @ref NT_Done() - Close down the NTAPI library.
128  *
129  *<hr>
130  * @section transmit_on_timestamp_setclock_example_code Code
131  * @}
132  */
133 
134 // Include this in order to access the Napatech API
135 #include <nt.h>
136 
137 #if defined(__linux__) || defined(__FreeBSD__) || (__MINGW32__)
138  #include <unistd.h> // sleep()
139 #endif
140 
141 #if defined(_MSC_VER)
142  #include <timesupport.h>
143 #endif
144 
145 #include <assert.h>
146 
147 #define PACKETS 100L // How many packets to send
148 #define PACKET_SIZE 296L // Packet size to transmit (incl crc.)
149 #define PACKET_GAP 500000L // Distance in 1ns between packets
150 #define REPLAYS 1000L // How many times we will repeat the packets
151 #define TX_DELAY 1000000000 // Initial transmission delay (one second)
152 #define PORT 0
153 #define ADAPTER 0
154 
155 // This program, by default, uses the NT descriptor type to transmit packets.
156 // To use a PCAP descriptor, set the boolean variable pcapPackets to true.
157 bool pcapPackets = false; // Set to true to enable PCAP descriptor type.
158 
159 // Time in nanoseconds to transmit one replay session
160 #define REPLAY_TRANSMIT_TIME (PACKET_GAP * PACKETS)
161 
162 // Time in nanoseconds to transmit all replay sessions
163 #define TOTAL_TRANSMIT_TIME (REPLAY_TRANSMIT_TIME * REPLAYS)
164 
165 // Struct pcap_timespec_s is the time specification for PCAP. For PCAP_NANO,
166 // the ts_usec field is in nanoseconds instead of microseconds.
167 struct pcap_timespec_s {
168  uint32_t ts_sec;
169  uint32_t ts_usec;
170 };
171 
172 // Standard PCAP record header.
174  struct pcap_timespec_s time;
175  uint32_t incl_len;
176  uint32_t orig_len;
177 };
178 
179 // printError is a simple convenience function for printing an NTAPI error
180 // message to stderr.
181 static void printError(const char *prefix, int errorCode) {
183  NT_ExplainError(errorCode, errorBuffer, sizeof errorBuffer);
184  fprintf(stderr, "%s: %s\n", prefix, errorBuffer);
185 }
186 
187 static uint64_t
188 convertToUnixNsTs(const struct NtConfigTimestampReadData_s *data)
189 {
190  const struct pcap_timespec_s *pcap_ts = (const struct pcap_timespec_s *)&data->ts;
191  switch (data->tsType) {
193  return pcap_ts->ts_sec * 1000000000ULL + pcap_ts->ts_usec;
195  return data->ts;
196  default:
197  // default to nativeUnixTs when nanosecond resolution unavailable
198  // this is Unix Time with 10ns resolution.
199  return data->nativeUnixTs * 10;
200  }
201 }
202 
203 int main(void)
204 {
205  //
206  // Initialize the API. This also checks if we are compatible with the
207  // installed library version.
208  //
209  int status;
210 
211  if ((status = NT_Init(NTAPI_VERSION)) != NT_SUCCESS) {
212  printError("NT_Init() failed", status);
213  return -1;
214  }
215 
216  //
217  // Open the config stream, in order to retrieve the adapter timestamp and
218  // to configure the transmit on timestamp functionality. We configure
219  // transmit on timestamp in such a way that a packet with timestamp 0 will
220  // transmit in approximately one second from this point.
221  //
222  NtConfigStream_t hConfig;
223  NtConfig_t config;
224 
225  if ((status = NT_ConfigOpen(&hConfig, "replay")) != NT_SUCCESS) {
226  printError("NT_ConfigOpen() failed", status);
227  return -1;
228  }
229 
231  config.u.timestampRead.adapter = ADAPTER;
232 
233  if ((status = NT_ConfigRead(hConfig, &config)) != NT_SUCCESS) {
234  printError("NT_ConfigRead() failed", status);
235  return -1;
236  }
237 
238  // Store the adapter timestamp and convert it to unix nanoseconds.
239  // Nanosecond precision is only available if TimestampFormat = UNIX_NS or
240  // TimestampFormat = PCAP_NS as defined in ntservice.ini.
241  // If TimestampFormat is set to anything else, it will derive the
242  // nanosecond timestamp from the NATIVE timestamp, which is measured
243  // in 10-ns ticks, and hence lose a small amount of precision.
244  uint64_t adapterTS = convertToUnixNsTs(&config.u.timestampRead.data);
245 
246  // Configure transmit on timestamp. In scenarios where a packet descriptor
247  // that does not contain a TXNOW bit is used, such as when transmitting PCAP,
248  // forceTxOnTs must be set to true. However, this example uses NT
249  // descriptors, and can thus use the TXNOW bit.
250  //
251  // timeDeltaAdjust is set to the time it takes to complete a single replay
252  // session, so that the TXSETCLOCK bit will cause timeDelta to be prepared
253  // for the next replay run. Note that since we set TXSETCLOCK on the first
254  // packet of every iteration, including the first one, that the value of
255  // timeDeltaAdjust need to be subtracted from timeDelta to avoid accidentally
256  // delaying the initial transmission.
257  // There must be "room" for one replay session within the delay window.
258  const uint64_t txBeginTS = adapterTS + TX_DELAY;
260  config.u.transmitOnTimestamp.portNo = (uint8_t)PORT;
261  assert(txBeginTS >= REPLAY_TRANSMIT_TIME);
264  config.u.transmitOnTimestamp.data.enable = true;
265  if (pcapPackets) {
266  // PCAP descriptor
267  config.u.transmitOnTimestamp.data.forceTxOnTs = true;
268  } else {
269  // NT descriptor
270  config.u.transmitOnTimestamp.data.forceTxOnTs = false;
271  }
272 
273  if ((status = NT_ConfigWrite(hConfig, &config)) != NT_SUCCESS) {
274  printError("NT_ConfigWrite() failed", status);
275  return -1;
276  }
277 
278  //
279  // Open a TX stream
280  //
281  NtNetStreamTx_t hNetTx;
282  if ((status = NT_NetTxOpen_v2(
283  &hNetTx, "transmit_on_timestamp_example_txstream", 1ULL << PORT,
287  printError("NT_NetTxOpen_v2() failed", status);
288  return -1;
289  }
290 
291  //
292  // Retrieve a packet buffer, fill it, and repeat until we have transmitted
293  // the requested amount of packets. The timestamp will be set so that the
294  // adapter will transmit at approximately 5 Mb/s.
295  //
296  printf("Commencing transmission of %ld packets %ld times\n", PACKETS, REPLAYS);
297 
298  NtNetBuf_t hNetBufTx;
299  int totalPackets = 0;
300 
301  for (int replays = 0; replays < REPLAYS; replays++) {
302 
303  uint64_t timestamp = 0;
304  for (int pkts = 0; pkts < PACKETS; pkts++, totalPackets++) {
305 
306  // Get a packet TX buffer for this tx stream and port, without timeout
307  if ((status = NT_NetTxGet(hNetTx, &hNetBufTx, PORT, PACKET_SIZE,
308  NT_NETTX_PACKET_OPTION_DEFAULT, -1)) != NT_SUCCESS) {
309  printError("NT_NetTxGet() failed", status);
310  return -1;
311  }
312 
313  // Set the packet timestamp
314  if (pcapPackets) {
315  // PCAP descriptor
316  struct pcap_record_s *hdr =
317  (struct pcap_record_s *)NT_NET_GET_PKT_DESCR_PTR(hNetBufTx);
318  hdr->incl_len = PACKET_SIZE;
319  hdr->orig_len = PACKET_SIZE;
320  hdr->time.ts_sec = (uint32_t)(timestamp / 1000000000U);
321  hdr->time.ts_usec = (uint32_t)(timestamp % 1000000000U);
322  } else {
323  // NT descriptor
324  NT_NET_SET_PKT_TIMESTAMP(hNetBufTx, timestamp);
325  }
326 
327  timestamp += PACKET_GAP; // increment the timestamp for the next packet
328 
329  if (pkts == 0) {
330  // We set TXSETCLOCK on the first packet. This results in the adapter
331  // adding timeDeltaAdjust to timeDelta before transmitting this packet.
332  NT_NET_SET_PKT_TXSETCLOCK(hNetBufTx, 1);
333  }
334 
335  // Fill the packet with an incrementing payload. Note that this will result
336  // in a garbage ethernet frame.
337  uint32_t *ptr = (uint32_t*)NT_NET_GET_PKT_L2_PTR(hNetBufTx);
338  for (uint32_t i = 0; i < PACKET_SIZE/4; i++) {
339  *(ptr+i) = i;
340  }
341 
342  // Release the TX buffer to commit the packet. It will transmit once the
343  // timestamp is reached, or when transmit on timestamp is disabled.
344  if ((status = NT_NetTxRelease(hNetTx, hNetBufTx)) != NT_SUCCESS) {
345  printError("NT_NetTxRelease() failed", status);
346  return -1;
347  }
348  }
349  }
350 
351  //
352  // Wait for all packets to be transmitted. There are multiple ways to do
353  // this, but in this example, we read the adapter timestamp in a loop, and
354  // compare it to the value of our timestamp variable from the loop above.
355  //
356  printf("Waiting for transmission to end\n");
357 
359  config.u.timestampRead.adapter = ADAPTER;
360 
361  const uint64_t txStopTS = txBeginTS + TOTAL_TRANSMIT_TIME;
362  uint64_t newAdapterTS;
363  do {
364  sleep(1);
365  if ((status = NT_ConfigRead(hConfig, &config)) != NT_SUCCESS) {
366  printError("NT_ConfigRead() failed", status);
367  return -1;
368  }
369 
370  newAdapterTS = convertToUnixNsTs(&config.u.timestampRead.data);
371  } while (newAdapterTS < txStopTS);
372 
373  //
374  // Disable transmit on timestamp. If any packets are left in the buffera this
375  // point, they will transmit unhindered at line rate.
376  //
378  config.u.transmitOnTimestamp.portNo = (uint8_t)PORT;
379  config.u.transmitOnTimestamp.data.timeDelta = 0;
381  config.u.transmitOnTimestamp.data.enable = false;
382  config.u.transmitOnTimestamp.data.forceTxOnTs = false;
383 
384  if ((status = NT_ConfigWrite(hConfig, &config)) != NT_SUCCESS) {
385  printError("NT_ConfigWrite() failed", status);
386  return -1;
387  }
388 
389  //
390  // Terminate
391  //
392  printf("Done: %d packets sent\n", totalPackets);
393 
394  //Close the Config stream
395  NT_ConfigClose(hConfig);
396 
397  // Close the TX stream
398  NT_NetTxClose(hNetTx);
399 
400  // Close the API
401  NT_Done();
402 
403  return 0;
404 }