transmit_packet_example.c

Reference Documentation

product_line_custom
Napatech SmartNIC
category
Reference Information

Go to the documentation of this file.

1/* 2 * %NT_SOFTWARE_LICENSE% 3 */ 4 5/** 6 * @example net/transmit_packet/transmit_packet_example.c 7 * @section transmit_packet_example_description Description 8 * 9 * This source file is an example of how to transmit packets using the packet 10 * interface in NTAPI. The example will transmit 2500000 packets with a size of 11 * 1024 bytes from port 0. The packet contains an incrementing 32bit pattern. 12 * 13 * The following NTAPI functions are used: 14 * - @ref NT_Init() 15 * - @ref NT_NetTxOpen() 16 * - @ref NT_NetTxGet() 17 * - @ref NT_NET_GET_PKT_L2_PTR() 18 * - @ref NT_NetTxRelease() 19 * - @ref NT_NetTxClose() 20 * - @ref NT_Done() 21 * - @ref NT_ExplainError() 22 * 23 * @section transmit_packet_example_prerequisites Prerequisites 24 * - The ntservice.ini must have at least one HostBuffersTx defined. Below is 25 * an example of a minimum ini-file. It will create a 4MB TX hostbuffer from 26 * NUMA node 0. 27 * 28 * @code 29 * [System] 30 * TimestampFormat = NATIVE 31 * 32 * [Adapter0] 33 * AdapterType = NT20E2 34 * BusId = 00:0a:00.00 35 * HostBuffersTx = [1,4,0] 36 * @endcode 37 * 38 * @section transmit_packet_example_flow Program flow 39 * @{ 40 * The following is required to transmit packages: 41 * - \#include/nt.h - Applications/Tools only need to include @ref 42 * nt.h to obtain prototypes, macros etc. from NTAPI. 43 * - @ref NT_Init(@ref NTAPI_VERSION) - Initialize the NTAPI 44 * library. @ref NTAPI_VERSION is a define that describes the version 45 * of the API described in the header files included by @ref 46 * nt.h. NT_Init() will ask the NTAPI library to convert return data 47 * to the @ref NTAPI_VERSION if possible. This will ensure that 48 * applications can run on NTAPI libraries of newer versions. 49 * - @ref NT_NetTxOpen() - Open a hostbuffer than can transmit packets to port 0. 50 * - @ref NT_NetTxGet() - Get an empty tx buffer. This will get a 296 byte 51 * wire length packet buffer that will be sent onto port 0 when 52 * released. 53 * - @ref NT_NET_GET_PKT_L2_PTR() is used to get the L2 pointer to the tx 54 * buffer, this is where the payload is placed. 55 * - @ref NT_NetTxRelease() - Release the tx packet buffer. Once a tx 56 * buffer is released it will be transmitted 57 * - @ref NT_NetTxClose() - Close the TX stream. 58 * - @ref NT_Done() - Close down the NTAPI library. 59 * 60 *<hr> 61 * @section transmit_packet_example_code Code 62 * @} 63 */ 64 65#include <nt.h> 66 67// default TX packet test setup 68#define PACKETS 2500000 69#define PACKET_SIZE 1024 // Packet size to transmit (incl crc.) 70#define PORT 0 71 72// printError is a simple convenience function for printing an NTAPI error 73// message to stderr. 74static void printError(const char *prefix, int errorCode) { 75 char errorBuffer[NT_ERRBUF_SIZE]; 76 NT_ExplainError(errorCode, errorBuffer, sizeof errorBuffer); 77 fprintf(stderr, "%s: %s\n", prefix, errorBuffer); 78} 79 80int main(void) 81{ 82 // 83 // Initialize the API. This also checks if we are compatible with the 84 // installed library version. 85 // 86 int status; 87 88 if((status = NT_Init(NTAPI_VERSION)) != NT_SUCCESS) { 89 printError("NT_Init() failed", status); 90 return -1; 91 } 92 93 // 94 // Open a TX stream 95 // 96 NtNetStreamTx_t hNetTx; 97 98 if ((status = NT_NetTxOpen(&hNetTx, "transmit_packet_example_txstream", 99 (1<<PORT), NT_NETTX_NUMA_ANY_HB, 0)) != NT_SUCCESS) { 100 printError("NT_NetTxOpen() failed", status); 101 return -1; 102 } 103 104 // 105 // Retrieve a packet buffer, fill it, and repeat until we have transmitted 106 // the requested amount of packets. 107 // 108 printf("Commencing transmission\n"); 109 110 NtNetBuf_t hNetBufTx; 111 int numPackets; 112 113 for (numPackets = 0; numPackets < PACKETS; numPackets++) { 114 115 // Get a packet TX buffer for this tx stream and port, without timeout 116 if ((status = NT_NetTxGet(hNetTx, &hNetBufTx, PORT, PACKET_SIZE, 117 NT_NETTX_PACKET_OPTION_DEFAULT, -1)) != NT_SUCCESS) { 118 printError("NT_NetTxGet() failed", status); 119 return -1; 120 } 121 122 // Fill the packet with an incrementing payload. Note that this will result 123 // in a garbage ethernet frame. 124 uint32_t *ptr = (uint32_t*)NT_NET_GET_PKT_L2_PTR(hNetBufTx); 125 for (int i = 0; i < PACKET_SIZE/4; i++) { 126 *(ptr+i) = i; 127 } 128 129 // Release the TX buffer to transmit the packet. 130 if ((status = NT_NetTxRelease(hNetTx, hNetBufTx)) != NT_SUCCESS) { 131 printError("NT_NetTxRelease() failed", status); 132 return -1; 133 } 134 } 135 136 // 137 // Terminate 138 // 139 printf("Done: %d packets sent\n", numPackets); 140 141 // Close the TX stream 142 NT_NetTxClose(hNetTx); 143 144 // Close the API 145 NT_Done(); 146 147 return 0; 148}