bypass_config_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 * @example bypass/config/bypass_config_example.c 6 * @section Description Description 7 * 8 * This source file is an example of how to use the @ref ConfigStream 9 * "Configuration stream" interface in NTAPI for Napatech bypass accelerator and port configuration. 10 * 11 * This example switches the state of the relays every time it is run. 12 * 13 * The following NTAPI functions are used: 14 * - @ref NT_Init() 15 * - @ref NT_InfoOpen() 16 * - @ref NT_InfoRead() 17 * - @ref NT_InfoClose() 18 * - @ref NT_ConfigOpen() 19 * - @ref NT_ConfigWrite() 20 * - @ref NT_ConfigRead() 21 * - @ref NT_ConfigClose() 22 * - @ref NT_Done() 23 * - @ref NT_ExplainError() 24 * 25 * <hr> 26 * @section Prerequisites_l Prerequisites 27 * A working system is needed with a Napatech bypass accelerator. 28 * 29 * @section Flow Program flow 30 * @{ 31 * The following is required to perform read and write operations on 32 * the @ref ConfigStream "configuration stream": 33 * 34 * - \#include/nt.h - Applications/Tools only need to include @ref 35 * nt.h to obtain prototypes, macros etc. from NTAPI. 36 * - @ref NT_Init(@ref NTAPI_VERSION) - Initialize the NTAPI 37 * library. @ref NTAPI_VERSION is a define that describes the version 38 * of the API described in the header files included by @ref 39 * nt.h. NT_Init() will ask the NTAPI library to convert return data 40 * to the @ref NTAPI_VERSION if possible. This will ensure that 41 * applications can run on NTAPI libraries of newer versions. 42 * 43 * - @ref NT_InfoOpen() - Open an info stream. 44 * - @ref NT_InfoRead() - Read info stream. 45 * - @ref NT_InfoClose() - Close the stream when terminating. 46 * - @ref NT_ConfigOpen() - Open a configuration stream. 47 * - @ref NT_ConfigRead() - Read configuration. 48 * - @ref NT_ConfigWrite() - Write configuration. 49 * - @ref NT_ConfigClose() - Close the stream when terminating. 50 * - @ref NT_Done() - Close down the NTAPI library. 51 * - @ref NT_ExplainError() - Explain an error code returned by NTAPI functions. 52 * 53 *<hr> 54 * @} 55 */ 56 57 58// std includes 59 60#if defined(__linux__) || defined(__FreeBSD__) 61 #include <unistd.h> 62#endif 63 64#include <stdlib.h> 65#include <stdio.h> 66#include <errno.h> 67 68// NT includes 69#include "nt.h" 70 71 72// 73// main() 74// 75int main(void) 76{ 77 uint32_t status; 78 char errBuf[NT_ERRBUF_SIZE]; 79 // 80 NtInfoStream_t hInfo; 81 NtInfo_t infoSystem; 82 NtInfo_t infoAdapter; 83 NtInfo_t infoPort; 84 // 85 NtConfigStream_t hConfig; 86 NtConfig_t configPort; 87 // 88 uint8_t adapter; 89 uint8_t port; 90 // 91 const char* aBpState[] = {"NA", "Normal", "Bypass"}; 92 const char* aBpFlags[] = {"Off", "On"}; 93 94 // 95 // 96 // 97 98 // Initialize NTAPI library 99 if ((status = NT_Init(NTAPI_VERSION)) != NT_SUCCESS) { 100 NT_ExplainError(status, errBuf, sizeof(errBuf)); 101 fprintf(stderr, "ERROR: NT_Init failed. Code 0x%x = %s\n", status, errBuf); 102 return status; 103 } 104 105 // Open the information stream 106 if ((status = NT_InfoOpen(&hInfo, "bypass_config_example")) != NT_SUCCESS) { 107 NT_ExplainError(status, errBuf, sizeof(errBuf)); 108 fprintf(stderr, ">>> Error: NT_InfoOpen failed. Code 0x%x = %s\n", status, errBuf); 109 return status; 110 } 111 112 // Read system information 113 infoSystem.cmd = NT_INFO_CMD_READ_SYSTEM; 114 if ((status = NT_InfoRead(hInfo, &infoSystem)) != NT_SUCCESS) { 115 NT_ExplainError(status, errBuf, sizeof(errBuf)); 116 fprintf(stderr, "ERROR: NT_InfoRead failed. Code 0x%x = %s\n", status, errBuf); 117 return status; 118 } 119 120 printf("System: %d.%d.%d.%d\n\n", infoSystem.u.system.data.version.major, 121 infoSystem.u.system.data.version.minor, 122 infoSystem.u.system.data.version.patch, 123 infoSystem.u.system.data.version.tag); 124 printf("Adapters: %u\n", infoSystem.u.system.data.numAdapters); 125 printf("Ports: %u\n", infoSystem.u.system.data.numPorts); 126 printf("\n"); 127 128 // Open the config stream 129 status = NT_ConfigOpen(&hConfig, "bypass_config_example"); 130 if (status != NT_SUCCESS) { 131 NT_ExplainError(status, errBuf, sizeof(errBuf)); 132 fprintf(stderr, ">>> Error: NT_ConfigOpen failed. Code 0x%x = %s\n", status, errBuf); 133 return status; 134 } 135 136 for (adapter = 0; adapter < infoSystem.u.system.data.numAdapters; adapter++) { 137 infoAdapter.cmd = NT_INFO_CMD_READ_ADAPTER_V6; 138 infoAdapter.u.adapter_v6.adapterNo = adapter; 139 if ((status = NT_InfoRead(hInfo, &infoAdapter)) != 0) { 140 NT_ExplainError(status, errBuf, sizeof(errBuf)); 141 fprintf(stderr, "ERROR: NT_InfoRead failed. Code 0x%x = %s\n", status, errBuf); 142 return status; 143 } 144 printf("Adapter %u (%u ports):\n", infoAdapter.u.adapter_v6.adapterNo, infoAdapter.u.adapter_v6.data.numPorts); 145 146 configPort.parm = NT_CONFIG_PARM_BYPASS_ADAPTER; 147 configPort.u.bypassConfig.u.adapterNo = adapter; 148 status = NT_ConfigRead(hConfig, &configPort); 149 if (status != 0) { 150 NT_ExplainError(status, errBuf, sizeof(errBuf)); 151 fprintf(stderr, "ERROR: NT_ConfigRead failed. Code 0x%x = %s\n", status, errBuf); 152 continue ; // skip this and continue to next adapter 153 } 154 else { 155 // 156 // Invert bypass adapter state - based on the current bypass adapter state 157 // Skip this step if the already read bypass adapter state is unknown - this means that the ports can be individually configured. 158 // 159 if (configPort.u.bypassConfig.data.currentBypassPortState != NT_BYPASS_PORT_STATE_UNKNOWN) { 160 if (configPort.u.bypassConfig.data.currentBypassPortState == NT_BYPASS_PORT_STATE_NORMAL) { 161 configPort.u.bypassConfig.data.currentBypassPortState = NT_BYPASS_PORT_STATE_BYPASS; 162 configPort.u.bypassConfig.data.onInitBypassPortState = NT_BYPASS_PORT_STATE_BYPASS; 163 configPort.u.bypassConfig.data.onPowerFailBypassPortState = NT_BYPASS_PORT_STATE_BYPASS; 164 configPort.u.bypassConfig.data.onWatchdogFailBypassPortState = NT_BYPASS_PORT_STATE_BYPASS; 165 } else { 166 configPort.u.bypassConfig.data.currentBypassPortState = NT_BYPASS_PORT_STATE_NORMAL; 167 configPort.u.bypassConfig.data.onInitBypassPortState = NT_BYPASS_PORT_STATE_NORMAL; 168 configPort.u.bypassConfig.data.onPowerFailBypassPortState = NT_BYPASS_PORT_STATE_NORMAL; 169 configPort.u.bypassConfig.data.onWatchdogFailBypassPortState = NT_BYPASS_PORT_STATE_NORMAL; 170 } 171 } 172 // 173 // Then write the changed configuration back... 174 // 175 configPort.parm = NT_CONFIG_PARM_BYPASS_ADAPTER; 176 configPort.u.bypassConfig.u.adapterNo = adapter; 177 if ((status = NT_ConfigWrite(hConfig, &configPort)) != 0) { 178 NT_ExplainError(status, errBuf, sizeof(errBuf)); 179 fprintf(stderr, "ERROR: NT_ConfigWrite failed. Code 0x%x = %s\n", status, errBuf); 180 return status; 181 } 182 183 // 184 // Display bypass configuration for the adapter 185 // 186 configPort.parm = NT_CONFIG_PARM_BYPASS_ADAPTER; 187 configPort.u.bypassConfig.u.adapterNo = adapter; 188 if ((status = NT_ConfigRead(hConfig, &configPort)) != 0) { 189 NT_ExplainError(status, errBuf, sizeof(errBuf)); 190 fprintf(stderr, "ERROR: NT_ConfigRead failed. Code 0x%x = %s\n", status, errBuf); 191 return status; 192 } 193 194 printf(" bypass adapter current state: %s\n", aBpState[configPort.u.bypassConfig.data.currentBypassPortState]); 195 printf(" bypass adapter onInit state: %s\n", aBpState[configPort.u.bypassConfig.data.onInitBypassPortState]); 196 printf(" bypass adapter onPwrFail state: %s\n", aBpState[configPort.u.bypassConfig.data.onPowerFailBypassPortState]); 197 printf(" bypass adapter onWdtFail state: %s\n", aBpState[configPort.u.bypassConfig.data.onWatchdogFailBypassPortState]); 198 printf(" bypass adapter onPwrFail detect trigger: %s\n", aBpFlags[((configPort.u.bypassConfig.data.bypassTriggerModes & NT_BYPASS_TRIGGER_PWRFAIL)==0?0:1)]); 199 } 200 printf("\n"); 201 202 for (port = 0; port < infoAdapter.u.adapter_v6.data.numPorts; port++) { 203 infoPort.cmd = NT_INFO_CMD_READ_PORT_V9; 204 infoPort.u.port_v9.portNo = (uint8_t)(infoAdapter.u.adapter_v6.data.portOffset + port); 205 if ((status = NT_InfoRead(hInfo, &infoPort)) != 0) { 206 NT_ExplainError(status, errBuf, sizeof(errBuf)); 207 fprintf(stderr, "ERROR: NT_InfoRead failed. Code 0x%x = %s\n", status, errBuf); 208 return status; 209 } 210 211 printf(" Port %i (adapter #%i port #%i):\n", infoPort.u.port_v9.portNo, infoPort.u.port_v9.data.adapterNo, port); 212 213 if ( infoPort.u.port_v9.data.capabilities.featureMask & NT_PORT_FEATURE_BYPASS) { 214 // 215 // There is no need to configure both ports in a bypass portset pair. 216 // You only need configure one of the ports in a bypass portset pair. 217 // The other port automatically will mirror the bypass configuration of the first port. 218 // 219 if ((port & 0x01) == 0) { 220 // 221 // Read current configuration 222 // 223 configPort.parm = NT_CONFIG_PARM_BYPASS_PORT; 224 configPort.u.bypassConfig.u.portNo = (uint8_t)(infoAdapter.u.adapter_v6.data.portOffset + port); 225 if ((status = NT_ConfigRead(hConfig, &configPort)) != 0) { 226 NT_ExplainError(status, errBuf, sizeof(errBuf)); 227 fprintf(stderr, "ERROR: NT_ConfigRead failed. Code 0x%x = %s\n", status, errBuf); 228 return status; 229 } 230 231 // 232 // Invert bypass portset state - based on the current bypass portset state 233 // 234 if (configPort.u.bypassConfig.data.currentBypassPortState == NT_BYPASS_PORT_STATE_NORMAL) { 235 configPort.u.bypassConfig.data.currentBypassPortState = NT_BYPASS_PORT_STATE_BYPASS; 236 configPort.u.bypassConfig.data.onInitBypassPortState = NT_BYPASS_PORT_STATE_BYPASS; 237 configPort.u.bypassConfig.data.onPowerFailBypassPortState = NT_BYPASS_PORT_STATE_BYPASS; 238 configPort.u.bypassConfig.data.onWatchdogFailBypassPortState = NT_BYPASS_PORT_STATE_BYPASS; 239 } else { 240 configPort.u.bypassConfig.data.currentBypassPortState = NT_BYPASS_PORT_STATE_NORMAL; 241 configPort.u.bypassConfig.data.onInitBypassPortState = NT_BYPASS_PORT_STATE_NORMAL; 242 configPort.u.bypassConfig.data.onPowerFailBypassPortState = NT_BYPASS_PORT_STATE_NORMAL; 243 configPort.u.bypassConfig.data.onWatchdogFailBypassPortState = NT_BYPASS_PORT_STATE_NORMAL; 244 } 245 // 246 // Then write the changed configuration back... 247 // 248 configPort.parm = NT_CONFIG_PARM_BYPASS_PORT; 249 configPort.u.bypassConfig.u.portNo = (uint8_t)(infoAdapter.u.adapter_v6.data.portOffset + port); 250 if ((status = NT_ConfigWrite(hConfig, &configPort)) != 0) { 251 NT_ExplainError(status, errBuf, sizeof(errBuf)); 252 fprintf(stderr, "ERROR: NT_ConfigWrite failed. Code 0x%x = %s\n", status, errBuf); 253 return status; 254 } 255 } 256 257 258 // 259 // Display result of configuration for all ports in a bypass portset 260 // The second port in a bypass portset should have mirrored the settings applied to the first port. 261 // 262 configPort.parm = NT_CONFIG_PARM_BYPASS_PORT; 263 configPort.u.bypassConfig.u.portNo = (uint8_t)(infoAdapter.u.adapter_v6.data.portOffset + port); 264 if ((status = NT_ConfigRead(hConfig, &configPort)) != 0) { 265 NT_ExplainError(status, errBuf, sizeof(errBuf)); 266 fprintf(stderr, "ERROR: NT_ConfigRead failed. Code 0x%x = %s\n", status, errBuf); 267 return status; 268 } 269 270 printf(" bypass port current state: %s\n", aBpState[configPort.u.bypassConfig.data.currentBypassPortState]); 271 printf(" bypass port onInit state: %s\n", aBpState[configPort.u.bypassConfig.data.onInitBypassPortState]); 272 printf(" bypass port onPwrFail state: %s\n", aBpState[configPort.u.bypassConfig.data.onPowerFailBypassPortState]); 273 printf(" bypass port onWdtFail state: %s\n", aBpState[configPort.u.bypassConfig.data.onWatchdogFailBypassPortState]); 274 printf(" bypass port onPwrFail detect trigger: %s\n", aBpFlags[((configPort.u.bypassConfig.data.bypassTriggerModes & NT_BYPASS_TRIGGER_PWRFAIL)==0?0:1)]); 275 276 configPort.parm = NT_CONFIG_PARM_BYPASS_PORT; 277 configPort.u.bypassConfig.u.portNo = (uint8_t)(infoAdapter.u.adapter_v6.data.portOffset + port); 278 if ((status = NT_ConfigWrite(hConfig, &configPort)) != 0) { 279 NT_ExplainError(status, errBuf, sizeof(errBuf)); 280 fprintf(stderr, "ERROR: NT_ConfigWrite failed. Code 0x%x = %s\n", status, errBuf); 281 return status; 282 } 283 284 } 285 printf("\n"); 286 } 287 printf("\n"); 288 } 289 printf("\n"); 290 291 // Close down the NTAPI library 292 NT_Done(); 293 294 return 0; 295} 296 297// 298// EOF 299//