1 /******************************************
2 * File: net_test.c
3 * Purpose: Application for testing out max configuration parameters
4 * such as SA's, MACs/IP interfaces
5 **************************************************************
6 * FILE: net_test.c
7 *
8 * DESCRIPTION: netapi user space transport
9 * library test application
10 *
11 * REVISION HISTORY:
12 *
13 * Copyright (c) Texas Instruments Incorporated 2013
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 *
19 * Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 *
22 * Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the
25 * distribution.
26 *
27 * Neither the name of Texas Instruments Incorporated nor the names of
28 * its contributors may be used to endorse or promote products derived
29 * from this software without specific prior written permission.
30 *
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
37 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
38 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
39 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
41 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43 *****************************************/
45 #include "net_test.h"
46 #include "ti/drv/nwal/test/fw_rm.h"
47 #include <signal.h>
48 #include <pthread.h>
50 extern int QUIT;
51 extern netTestStats_T stats[TUNE_NETAPI_NUM_CORES];
52 extern paSysStats_t netcp_stats;
53 /* Global definitions */
54 #ifdef netTest_MULTI_THREAD
55 cpu_set_t cpu_set;
56 #endif
59 #define SA_CREATE_LOOP_COUNT 64
60 #define IP_CREATE_LOOP_COUNT 64
61 #define MAC_CREATE_LOOP_COUNT 59
64 netTestConfig_t netTestCfg;
65 static netTestConfigFile_t config_file;
67 char input_file_name[] = "net_test_config_max_iface.txt";
69 nwal_RetValue nwalRetVal;
70 Pktlib_HeapHandle ourHeap;
72 PKTIO_HANDLE_T *netcp_rx_chan;
73 PKTIO_HANDLE_T *netcp_tx_chan_no_crypto;
74 PKTIO_HANDLE_T *netcp_tx_chan_esp;
75 PKTIO_HANDLE_T *netcp_tx_chan_ah;
76 PKTIO_HANDLE_T *netcp_sb_tx_chan;
77 PKTIO_HANDLE_T *netcp_sb_rx_chan;
79 PKTIO_CFG_T our_chan_cfg={PKTIO_RX_TX, PKTIO_LOCAL, PKTIO_Q_ANY, 8};
80 PKTIO_CFG_T netcp_rx_cfg={PKTIO_RX, PKTIO_NA, PKTIO_NA, 8};
81 PKTIO_CFG_T netcp_rx_cfg2={PKTIO_RX, (PKTIO_GLOBAL|PKTIO_PKT), PKTIO_Q_ANY, 8};
82 PKTIO_CFG_T netcp_tx_cfg={PKTIO_TX, PKTIO_NA, PKTIO_NA, 8};
83 PKTIO_CFG_T netcp_sb_rx_cfg={PKTIO_RX, PKTIO_NA, PKTIO_NA, 8};
84 PKTIO_CFG_T netcp_sb_tx_cfg={PKTIO_TX, PKTIO_NA, PKTIO_NA, 8};
87 NETCP_CFG_EXCEPTION_PKT_T expPkt_appid;
89 Trie *p_trie_sa_rx;
90 Trie *p_trie_sa_tx;
93 /*******************************************
94 *************NETAPI OBJECTS***************
95 *****************************************/
96 static NETAPI_CFG_T our_netapi_default_cfg=
97 {
98 TUNE_NETAPI_PERM_MEM_SZ,
99 128, //start of packet offset for hw to place data on rx for default flow
100 TUNE_NETAPI_QM_CONFIG_MAX_DESC_NUM, //max number of descriptors in system
101 TUNE_NETAPI_NUM_GLOBAL_DESC, //total we will use
102 TUNE_NETAPI_DEFAULT_NUM_BUFFERS, //#descriptors+buffers in default heap
103 64, //#descriptors w/o buffers in default heap
104 TUNE_NETAPI_DEFAULT_BUFFER_SIZE+128+128, //size of buffers in default heap
105 128 , //tail room
106 256 //extra room
107 };
109 NETAPI_T netapi_handle;
110 NETAPI_SCHED_HANDLE_T * our_sched;
111 #ifdef netTest_MULTI_THREAD
112 NETAPI_SCHED_HANDLE_T * scheduler[TUNE_NETAPI_NUM_CORES];
113 #endif
114 NETAPI_SCHED_CONFIG_T our_sched_cfg={
115 NETAPI_SCHED_DURATION|NETAPI_SCHED_CBV, 0, house, 5000000 //every 5000000 poll loops
116 };
118 NETCP_CFG_IP_T ip_rule[NET_TEST_MAX_IP];
119 NETCP_CFG_MACIF_T mac[NET_TEST_MAX_MAC];
123 /* security objects. (for loopback mode) */
124 netTestSA_t sa_info[MAX_SEC_INDEX];
126 NETCP_CFG_IPSEC_POLICY_T rx_policy[MAX_SEC_INDEX];
128 /* stub functions */
129 void recv_cb(struct PKTIO_HANDLE_Tag * channel, Ti_Pkt* p_recv[],
130 PKTIO_METADATA_T meta[], int n_pkts,
131 uint64_t ts )
132 {
133 return;
134 }
136 void flip_and_send_pkt(Ti_Pkt *tip, unsigned char * p_pkt, int len, int flag, uint16_t enet_port)
137 {
138 return;
139 }
143 void recv_sb_cb(struct PKTIO_HANDLE_Tag * channel, Ti_Pkt* p_recv[],
144 PKTIO_METADATA_T meta[], int n_pkts,
145 uint64_t ts )
146 {
147 return;
148 }
150 Trie * route_init(void)
151 {
152 }
153 void route_add(Trie * Pt, unsigned long * Pdest_ipBE, void * Pour_route)
154 {
155 }
157 #ifdef netTest_MULTI_THREAD
158 NETAPI_T worker_nh[TUNE_NETAPI_NUM_CORES];
160 void slow_path_thread(uint32_t thread_num)
161 {
162 return;
163 }
165 void fast_path_thread(uint32_t thread_num)
166 {
167 return;
168 }
170 #endif
172 /* Templates to build command labels at startup up time, required by open_pktio_tx_channels() */
173 nwalTxPktInfo_t txPktInfoESP =
174 {
175 NULL, /* p_pkt */
176 NWAL_TX_FLAG1_DO_IPSEC_ESP_CRYPTO| NWAL_TX_FLAG1_DO_UDP_CHKSUM| NWAL_TX_FLAG1_META_DATA_VALID, /* txFlags */
177 0, /* lpbackPass */
178 0, /* enetport */
179 0, /* msuSize */
180 0, /* startOffset */
181 netTest_MAC_HEADER_LEN + netTest_IP_HEADER_LEN, /* saOffBytes */
182 0, /* saPayLoadLen */
183 0 , /* saAhIcvOffBytes */
184 0, /* saAhMacSize */
185 0, /* etherLenOffBytes */
186 0, /* ipOffBytes */
187 0, /* l4OffBytes */
188 netTest_UDP_HEADER_LEN, /* l4HdrLen */
189 0, /* pseudoHdrChecksum */
190 0 /* pLoadLen */
191 };
194 nwalTxPktInfo_t txPktInfoAH =
195 {
196 NULL, /* p_pkt */
197 NWAL_TX_FLAG1_DO_IPSEC_AH_CRYPTO| NWAL_TX_FLAG1_DO_UDP_CHKSUM | NWAL_TX_FLAG1_META_DATA_VALID, /* txFlags */
198 0, /* lpbackPass */
199 0, /* enetport */
200 0, /* msuSize */
201 0, /* startOffset */
202 netTest_MAC_HEADER_LEN + netTest_IP_HEADER_LEN, /* saOffBytes */
203 0, /* saPayLoadLen */
204 netTest_MAC_HEADER_LEN + netTest_IP_HEADER_LEN + netTest_IPSEC_AH_FIXED_HDR_SIZE, /* saAhIcvOffBytes */
205 12, /* saAhMacSize */
206 0, /* etherLenOffBytes */
207 0, /* ipOffBytes */
208 0, /* l4OffBytes */
209 netTest_UDP_HEADER_LEN, /* l4HdrLen */
210 0, /* pseudoHdrChecksum */
211 0 /* pLoadLen */
212 };
214 nwalTxPktInfo_t txPktInfoNoCrypto =
215 {
216 NULL, /* p_pkt */
217 NWAL_TX_FLAG1_DO_UDP_CHKSUM| NWAL_TX_FLAG1_META_DATA_VALID, /* txFlags */
218 0, /* lpbackPass */
219 0, /* enetport */
220 0, /* msuSize */
221 0, /* startOffset */
222 0, /* saOffBytes */
223 0, /* saPayLoadLen */
224 0 , /* saAhIcvOffBytes */
225 0, /* saAhMacSize */
226 0, /* etherLenOffBytes */
227 0, /* ipOffBytes */
228 netTest_MAC_HEADER_LEN +netTest_IP_HEADER_LEN, /* l4OffBytes */
229 netTest_UDP_HEADER_LEN, /* l4HdrLen */
230 0, /* pseudoHdrChecksum */
231 0 /* pLoadLen */
232 };
235 void close_pktio_channels(void)
236 {
237 int err;
238 netapi_pktioClose(netcp_tx_chan_esp ,&err);
239 netapi_pktioClose(netcp_tx_chan_ah ,&err);
240 netapi_pktioClose(netcp_sb_tx_chan ,&err);
241 netapi_pktioClose(netcp_tx_chan_no_crypto,&err);
242 }
244 void open_pktio_tx_channels(void)
245 {
246 int err;
247 /* open netcp default TX for ESP packets */
248 netcp_tx_chan_esp= netapi_pktioOpen(netapi_handle, NETCP_TX, NULL, &netcp_tx_cfg, &err);
249 if (!netcp_tx_chan_esp)
250 {
251 printf("pktio open TX failed err=%d\n",err);
252 exit(1);
253 }
254 else
255 {
256 if(netTestCfg.ipsec_mode_tx == IPSEC_MODE_TX_INFLOW)
257 {
258 PKTIO_CONTROL_T control;
259 control.op = PKTIO_UPDATE_FAST_PATH;
260 PKTIO_CFG_T cfg;
261 cfg.fast_path_cfg.fp_send_option = PKTIO_FP_ESP_L4CKSUM_PORT;
262 cfg.fast_path_cfg.txPktInfo= &txPktInfoESP;
263 netapi_pktioControl(netcp_tx_chan_esp, NULL, &cfg, &control, &err);
264 }
265 }
267 /* open netcp default TX for AH packets */
268 netcp_tx_chan_ah= netapi_pktioOpen(netapi_handle, NETCP_TX, NULL, &netcp_tx_cfg, &err);
269 if (!netcp_tx_chan_ah)
270 {
271 printf("pktio open TX failed err=%d\n",err);
272 exit(1);
273 }
274 else
275 {
276 if(netTestCfg.ipsec_mode_tx == IPSEC_MODE_TX_INFLOW)
277 {
278 PKTIO_CONTROL_T control;
279 control.op = PKTIO_UPDATE_FAST_PATH;
280 PKTIO_CFG_T cfg;
281 cfg.fast_path_cfg.fp_send_option = PKTIO_FP_AH_L4CKSUM_PORT;
282 cfg.fast_path_cfg.txPktInfo= &txPktInfoAH;
283 netapi_pktioControl(netcp_tx_chan_ah, NULL, &cfg, &control, &err);
284 }
285 }
287 /* open netcp default TX channels for non-Crypto packets */
288 netcp_tx_chan_no_crypto= netapi_pktioOpen(netapi_handle, NETCP_TX, NULL, &netcp_tx_cfg, &err);
289 if (!netcp_tx_chan_no_crypto)
290 {
291 printf("pktio open TX failed err=%d\n",err);
292 exit(1);
293 }
294 else
295 {
296 if(netTestCfg.ipsec_mode_tx == IPSEC_MODE_TX_INFLOW)
297 {
298 PKTIO_CONTROL_T control;
299 control.op = PKTIO_UPDATE_FAST_PATH;
300 PKTIO_CFG_T cfg;
301 cfg.fast_path_cfg.fp_send_option = PKTIO_FP_L4CKSUM_PORT;
302 cfg.fast_path_cfg.txPktInfo= &txPktInfoNoCrypto;
303 netapi_pktioControl(netcp_tx_chan_no_crypto, NULL, &cfg, &control, &err);
304 }
305 }
307 /* open netcp default TX channels for SB crypto */
308 netcp_sb_tx_chan= netapi_pktioOpen(netapi_handle, NETCP_SB_TX, NULL, &netcp_sb_tx_cfg, &err);
309 if (!netcp_sb_tx_chan)
310 {
311 printf("pktio open SB TX failed err=%d\n",err);
312 exit(1);
313 }
314 }
315 /***************************************
316 ********** test driver*****************
317 ***************************************/
318 int main(int argc, char **argv)
319 {
320 int err,i;
321 Pktlib_HeapCfg heapCfg;
322 int32_t errCode;
323 Pktlib_HeapIfTable* pPktifTable;
324 FILE * fpr = NULL;
327 setvbuf(stdout,NULL,_IONBF,0);
328 /* install signal handler for ^c */
329 signal(SIGINT,netTest_utilMySig);
331 if (argc == 2)
332 {
333 printf("main: filename1 %s\n", argv[1]);
334 fpr = fopen(argv[1], "r");
335 }
336 else
337 {
338 fpr = fopen(input_file_name, "r");
339 }
340 if (fpr == NULL)
341 {
342 printf("error opening configfile\n");
343 exit(1);
344 }
345 else
346 {
347 memset(&config_file, 0, sizeof(netTestConfigFile_t));
348 memset(&netTestCfg, 0, sizeof(netTestConfig_t));
349 netTest_utilProcessConfigFile(fpr,&config_file);
350 #if 1
351 netTest_utilParseMac(&config_file);
353 /* parse slow path/fast path thread configuration parameters */
354 netTest_utilParseThreadParams(&config_file);
356 netTest_utilParseIP(&config_file);
358 netTest_utilParseIpsecMode(&config_file);
360 /* DSP mac processing */
361 parse_dsp_mac(&config_file.dsp_mac[0]);
363 /* DSP IP processing */
364 parse_dsp_ip(&config_file.dsp_ip[0]);
366 /* IPSEC interface number processing */
367 parse_simple_param_u32((char*)&config_file.ipsec_if_no[0], &netTestCfg.ipsec_if_no);
369 netTest_utilParseSA(&config_file);
370 #endif
371 }
373 memset(&sa_info, 0, sizeof(sa_info));
376 #ifdef netTest_MULTI_THREAD
377 /* assign main net_test thread to run on core 0 */
378 CPU_ZERO( &cpu_set);
379 CPU_SET( 0, &cpu_set);
380 hplib_utilSetupCore(0, &cpu_set);
381 #endif
383 /* create netapi */
384 netapi_handle = netapi_init(NETAPI_SYS_MASTER, &our_netapi_default_cfg);
386 if (netapi_handle)
387 printf("main: netapi_init sucess\n");
388 else
389 printf("main: netapi_init fail, null netapi_handle\n");
391 /* open the main heap */
392 ourHeap = Pktlib_findHeapByName("netapi");
393 if (!ourHeap)
394 {
395 printf("Pktlib_findHeapByName() fail\n");
396 exit(1);
397 }
399 open_pktio_tx_channels();
401 /* create scheduler instance */
402 our_sched =netapi_schedOpen(netapi_handle,&our_sched_cfg, &err);
403 if (!our_sched) {printf("sched create failed\n"); exit(1);}
406 /*create net_test MAC interfaces, attach IP to created MAC interfaces */
407 netTest_utilCreateInterfaces(netTestCfg.num_macs, netTestCfg.num_ips);
409 /* lookup Database for SA context, this is used by packet processing routines to get RX and TX SA information*/
410 p_trie_sa_rx = trie_new();
411 p_trie_sa_tx = trie_new();
412 if (!p_trie_sa_rx || !p_trie_sa_tx)
413 {printf("trie alloc for SA failed\n"); exit(1);}
416 #ifdef TEST_SA
417 /* Create RX SA's, RX Policy and TX SA's, all SA configuration parameters are read from net_test_config.txt file */
418 netTest_utilCreateSecAssoc();
419 #endif
420 #ifdef netTest_MULTI_THREAD
421 {
422 char c;
425 //this thread of execution (main) now just waits on user input
426 for(;;)
427 {
428 printf(">");
429 c=getchar();
430 if (c=='q')
431 {
432 QUIT=1;
433 break;
434 }
435 else if (c=='h')
436 printf("'q' to quit, 's' for stats, 'h' for help\n");
437 }
438 }
439 #else
440 /*********************************************/
441 /**************Entry point into scheduler ****/
442 /*********************************************/
443 netapi_schedRun(our_sched, &err);
444 #endif
446 /* cleanup*/
447 netTest_utilDeleteSecAssoc();
449 netTest_utilDeleteInterfaces(netTestCfg.num_macs, netTestCfg.num_ips);
451 /* close pktio channels we opened via open_pktio_tx_channels() */
452 close_pktio_channels();
454 netapi_shutdown(netapi_handle);
456 }