Update config file paths to point to new location on filesystem
[keystone-rtos/netapi.git] / ti / runtime / netapi / test / net_test_router.c
1 /******************************************************************************
2  * File: net_test_router.c
3  * Purpose: net_test_router application
4  ******************************************************************************
5  * FILE:  net_test_router.c
6  * 
7  * DESCRIPTION:  netapi user space transport
8  *               library  net_test_router application
9  * 
10  * REVISION HISTORY:
11  *
12  *  Copyright (c) Texas Instruments Incorporated 2013
13  * 
14  *  Redistribution and use in source and binary forms, with or without 
15  *  modification, are permitted provided that the following conditions 
16  *  are met:
17  *
18  *    Redistributions of source code must retain the above copyright 
19  *    notice, this list of conditions and the following disclaimer.
20  *
21  *    Redistributions in binary form must reproduce the above copyright
22  *    notice, this list of conditions and the following disclaimer in the 
23  *    documentation and/or other materials provided with the   
24  *    distribution.
25  *
26  *    Neither the name of Texas Instruments Incorporated nor the names of
27  *    its contributors may be used to endorse or promote products derived
28  *    from this software without specific prior written permission.
29  *
30  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
31  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
32  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
33  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
34  *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
35  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
36  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
39  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
40  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42  ******************************************************************************/
44 #include "netapi.h"
45 #include "net_test_sa_utils.h"
46 #include "net_test_utils.h"
47 #include "net_test_thread_utils.h"
49 #include <signal.h>
50 #include <pthread.h>
51 #include <sys/resource.h>
52 #include "router.h"
53 extern int QUIT;
54 extern Rm_ServiceHandle   *rmClientServiceHandle;
57 #if defined(DEVICE_K2H)
58 #include <ti/drv/qmss/device/k2h/src/qmss_device.c>
59 #include <ti/drv/cppi/device/k2h/src/cppi_device.c>
60 #elif defined (DEVICE_K2K)
61 #include <ti/drv/qmss/device/k2k/src/qmss_device.c>
62 #include <ti/drv/cppi/device/k2k/src/cppi_device.c>
63 #elif defined (DEVICE_K2L)
64 #include <ti/drv/qmss/device/k2l/src/qmss_device.c>
65 #include <ti/drv/cppi/device/k2l/src/cppi_device.c>
66 #elif defined (DEVICE_K2E)
67 #include <ti/drv/qmss/device/k2e/src/qmss_device.c>
68 #include <ti/drv/cppi/device/k2e/src/cppi_device.c>
69 #else /*Default */
70 #include <ti/drv/qmss/device/k2h/src/qmss_device.c>
71 #include <ti/drv/cppi/device/k2h/src/cppi_device.c>
72 #endif
73 /* Global definitions */
75 extern Rm_ServiceHandle   *rmClientServiceHandle;
77 hplib_spinLock_T net_test_thread_lock;
78 netTestConfig_t netTestCfg;
79 static  netTestConfigFile_t config_file;
81 char    input_file_name[] = "/etc/transportnetlib/test/net_test_config.txt";
83 nwal_RetValue       nwalRetVal;
84 Pktlib_HeapHandle ourHeap;
86 PKTIO_HANDLE_T *netcp_tx_chan_no_crypto;
87 PKTIO_HANDLE_T *netcp_rx_chan;
88 PKTIO_HANDLE_T *netcp_tx_chan_esp;
89 PKTIO_HANDLE_T *netcp_tx_chan_ah;
94 PKTIO_CFG_T netcp_rx_cfg={PKTIO_RX, PKTIO_NA, PKTIO_NA, 8};
95 PKTIO_CFG_T netcp_tx_cfg={PKTIO_TX, PKTIO_NA, PKTIO_NA, 8};
99 NETCP_CFG_EXCEPTION_PKT_T expPkt_appid;
101 Trie *p_trie_sa_rx;
102 Trie *p_trie_sa_tx;
105 #include "router.c"
106 Trie * our_router;
108 OUR_ROUTE_T routes[MAX_ROUTES]=
111     {0,{0xD4,0xbe,0xd9,0x00,0xd3,0x7e, 0x00,0x01,0x02,0x03,0x14,0x02,0x08,0x00},0},
112     {0,{0x00,0x00,0x0,0x00,0x0,0x0, 0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00},0},
113     {0,{0xD4,0xbe,0xd9,0x00,0xd3,0x7e, 0x00,0x01,0x02,0x03,0x14,0x02,0x08,0x00},0},
114     {0,{0x00,0x15,0x60,0xa1,0xf7,0xbe, 0x00,0x01,0x02,0x03,0x04,0x05,0x08,0x00},0},
115     {0,{0xd4,0xbe,0xd9,0x00,0xd3,0x7e, 0x00,0x01,0x02,0x03,0x04,0x05,0x08,0x00},0}
116 };
117 unsigned int ip[MAX_ROUTES]={BE(0x0a0100c8),BE(0x0a00000a),BE(0x0a02000a),BE(0xc0a8010a),BE(0x9eda6719)};
119 void recv_cb_router(struct PKTIO_HANDLE_Tag * channel, Ti_Pkt* p_recv[],
120                          PKTIO_METADATA_T meta[], int n_pkts,
121                          uint64_t ts );
124 extern netTestStats_T stats[TUNE_NETAPI_NUM_CORES];
125 extern paSysStats_t netcp_stats;
127 /*******************************************
128  *************NETAPI OBJECTS***************
129  *****************************************/
130 static NETAPI_CFG_T our_netapi_default_cfg=
132 TUNE_NETAPI_PERM_MEM_SZ,
133 128,  //start of packet offset for hw to place data on rx for default flow
134 TUNE_NETAPI_QM_CONFIG_MAX_DESC_NUM, //max number of descriptors in system
135 TUNE_NETAPI_NUM_GLOBAL_DESC,        //total we will use
136 TUNE_NETAPI_DEFAULT_NUM_BUFFERS,   //#descriptors+buffers in default heap
137 64, //#descriptors w/o buffers in default heap
138 TUNE_NETAPI_DEFAULT_BUFFER_SIZE+128+128,  //size of buffers in default heap
139 128,  //tail room
140 256,  //extra room
141 0,
142 NULL
143 };
147 void house(NETAPI_SCHED_HANDLE_T *s);
148 NETAPI_T netapi_handle;
149 NETAPI_SCHED_HANDLE_T * our_sched;
150 #ifdef netTest_MULTI_THREAD
151 NETAPI_SCHED_HANDLE_T * scheduler[TUNE_NETAPI_NUM_CORES];
152 #endif
153 NETAPI_SCHED_CONFIG_T our_sched_cfg={
154   NETAPI_SCHED_DURATION|NETAPI_SCHED_CBV, 0, house, 5000000  //every 5000000 poll loops
155 };
158 NETCP_CFG_IP_T ip_rule[NET_TEST_MAX_IP];
159 NETCP_CFG_MACIF_T mac[NET_TEST_MAX_MAC];
163 /* security objects. (for loopback mode) */
164 netTestSA_t sa_info[MAX_SEC_INDEX];
167 NETCP_CFG_IPSEC_POLICY_T rx_policy[MAX_SEC_INDEX];
172 /*************************END NETAPI OBJECTS***********************/
174 void update_header(netTestHead_T * p_head, int len)
176    unsigned char *p = (unsigned char *) &p_head->udp[1];
177    len -= (20+14);
178    /* update ip checksum */
179    /* update udp checksum */
180    /* update length */
181    *p= (len&0xff00)>>8;
182    *(p+1) = len&0xff;
185 #ifdef netTest_MULTI_THREAD
186 /* Templates to build command labels at startup up time, required by open_pktio_tx_channels() */
187 nwalTxPktInfo_t txPktInfoESP = 
189     NULL,                                                                                               /* p_pkt */
190     NWAL_TX_FLAG1_DO_IPSEC_ESP_CRYPTO| NWAL_TX_FLAG1_DO_IPV4_CHKSUM| NWAL_TX_FLAG1_META_DATA_VALID,      /* txFlags */
191     0,                                                                                                  /* lpbackPass */
192     1,                                                                                                  /* enetport */
193     0,                                                                                                  /* msuSize */
194     0,                                                                                                   /* startOffset */
195     netTest_MAC_HEADER_LEN  + netTest_IP_HEADER_LEN,                                                    /* saOffBytes */
196     0,                                                                                                  /* saPayLoadLen */
197     0               ,                                                                                    /* saAhIcvOffBytes */
198     0,                                                                                                 /* saAhMacSize */
199     0,                                                                                                  /* etherLenOffBytes */
200     netTest_MAC_HEADER_LEN,                                                        /* ipOffBytes */
201     0,                          /* l4OffBytes */
202     0,                         /* l4HdrLen */
203     0,                                                                                   /* pseudoHdrChecksum */
204     0                                                                                                   /* pLoadLen */
205 };
208 nwalTxPktInfo_t txPktInfoAH = 
210     NULL,                                                                                               /* p_pkt */
211     NWAL_TX_FLAG1_DO_IPSEC_AH_CRYPTO| NWAL_TX_FLAG1_DO_IPV4_CHKSUM | NWAL_TX_FLAG1_META_DATA_VALID,      /* txFlags */
212     0,                                                                                                  /* lpbackPass */
213     0,                                                                                                  /* enetport */
214     0,                                                                                                  /* msuSize */
215     0,                                                                                                   /* startOffset */
216     netTest_MAC_HEADER_LEN  + netTest_IP_HEADER_LEN,                                                    /* saOffBytes */
217     0,                                                                                                  /* saPayLoadLen */
218     netTest_MAC_HEADER_LEN + netTest_IP_HEADER_LEN + netTest_IPSEC_AH_FIXED_HDR_SIZE,                    /* saAhIcvOffBytes */
219     12,                                                                                                 /* saAhMacSize */
220     0,                                                                                                  /* etherLenOffBytes */
221     netTest_MAC_HEADER_LEN,   /* ipOffBytes */
222     0,                    /* l4OffBytes */
223     netTest_UDP_HEADER_LEN,                                                                            /* l4HdrLen */
224     0,                                                                                                  /* pseudoHdrChecksum */
225     0                                                                                                   /* pLoadLen */
226 };
228 nwalTxPktInfo_t txPktInfoNoCrypto = 
230     NULL,                                                                                               /* p_pkt */
231     NWAL_TX_FLAG1_DO_IPV4_CHKSUM| NWAL_TX_FLAG1_META_DATA_VALID,      /* txFlags */
232     0,                                                                                                  /* lpbackPass */
233     0,                                                                                                  /* enetport */
234     0,                                                                                                  /* msuSize */
235     0,                                                                                                   /* startOffset */
236     0,                                                    /* saOffBytes */
237     0,                                                                                                  /* saPayLoadLen */
238     0               ,                                                                                    /* saAhIcvOffBytes */
239     0,                                                                                                 /* saAhMacSize */
240     0,                                                                                                  /* etherLenOffBytes */
241     netTest_MAC_HEADER_LEN,         /* ipOffBytes */
242     netTest_MAC_HEADER_LEN +netTest_IP_HEADER_LEN,                                        /* l4OffBytes */
243     netTest_UDP_HEADER_LEN,                                                             /* l4HdrLen */
244     0,                                                                         /* pseudoHdrChecksum */
245     0                                                                                                   /* pLoadLen */
246 };
248 void close_pktio_channels(void)
250     int err;
251     netapi_pktioClose(netcp_tx_chan_no_crypto ,&err);
252     netapi_pktioClose(netcp_tx_chan_esp ,&err);
253     netapi_pktioClose(netcp_tx_chan_ah ,&err);
258 void open_pktio_tx_channels()
260     int err;
261     /* open netcp default TX channels for non-ipsec*/
262     netcp_tx_chan_no_crypto= netapi_pktioOpen(netapi_handle, NETCP_TX, NULL, &netcp_tx_cfg,  &err);
263     if (!netcp_tx_chan_no_crypto)
264     {
265         netapi_Log("pktio open TX failed err=%d\n",err);
266         exit(1);
267     }
268     else
269     {
270             PKTIO_CONTROL_T control;
271             control.op = PKTIO_UPDATE_FAST_PATH;
272             PKTIO_CFG_T cfg;
273             cfg.fast_path_cfg.fp_send_option = PKTIO_FP_NO_CRYPTO_NO_CKSUM_PORT;
274             cfg.fast_path_cfg.txPktInfo= &txPktInfoNoCrypto;
275             netapi_pktioControl(netcp_tx_chan_no_crypto, NULL, &cfg, &control, &err);
276     }
277     /* open netcp default TX for ESP packets */
278     netcp_tx_chan_esp= netapi_pktioOpen(netapi_handle, NETCP_TX, NULL, &netcp_tx_cfg,  &err);
279     if (!netcp_tx_chan_esp)
280     {
281         netapi_Log("pktio open TX failed err=%d\n",err);
282         exit(1);
283     }
284     else
285     {
286         if(netTestCfg.ipsec_mode_tx == IPSEC_MODE_TX_INFLOW)
287         {
288             PKTIO_CONTROL_T control;
289             control.op = PKTIO_UPDATE_FAST_PATH;
290             PKTIO_CFG_T cfg;
291             cfg.fast_path_cfg.fp_send_option = PKTIO_FP_ESP_L3CKSUM_PORT;
292             cfg.fast_path_cfg.txPktInfo= &txPktInfoESP;
293             netapi_pktioControl(netcp_tx_chan_esp, NULL, &cfg, &control, &err);
294         }
295     }
297     /*/* open netcp default  TX for AH packets */
298     netcp_tx_chan_ah= netapi_pktioOpen(netapi_handle, NETCP_TX, NULL, &netcp_tx_cfg,  &err);
299     if (!netcp_tx_chan_ah)
300     {
301         netapi_Log("pktio open TX failed err=%d\n",err);
302         exit(1);
303     }
304     else
305     {
306         if(netTestCfg.ipsec_mode_tx == IPSEC_MODE_TX_INFLOW)
307         {
308             PKTIO_CONTROL_T control;
309             control.op = PKTIO_UPDATE_FAST_PATH;
310             PKTIO_CFG_T cfg;
311             cfg.fast_path_cfg.fp_send_option = PKTIO_FP_AH_PORT;
312             cfg.fast_path_cfg.txPktInfo= &txPktInfoAH;
313             netapi_pktioControl(netcp_tx_chan_ah, NULL, &cfg, &control, &err);
314         }
315     }
321 NETAPI_T worker_nh[TUNE_NETAPI_NUM_CORES];
322 void slow_path_thread(uint32_t index)
324     int err,i;
325     uint32_t thread_num;
326     PKTIO_HANDLE_T *rx_chan;
327     PKTIO_HANDLE_T *sb_tx_chan;
328     PKTIO_HANDLE_T *sb_rx_chan;
329     cpu_set_t cpu_set;
331     thread_num = netTestCfg.sp_thread_num[index];
332     printf("slow_path_thread for index %d  called for thread %d\n", index, thread_num);
334     CPU_ZERO( &cpu_set);
335 #ifdef CORTEX_A8
336    for (i = netTestCfg.sp_proc_start[index]; i <= netTestCfg.sp_proc_end[index];i++)
337     {
338         printf("slow_path_thread: setting cpu %d to cpu_set\n", i);
339         CPU_SET( i, &cpu_set);
340     }
341     hplib_utilSetupThread(0, &cpu_set, hplib_spinLock_Type_LOL);
342 #else
343     for (i = netTestCfg.sp_proc_start[index]; i <= netTestCfg.sp_proc_end[index];i++)
344     {
345         printf("slow_path_thread: setting cpu %d to cpu_set\n", i);
346         CPU_SET( i, &cpu_set);
347     }
348     hplib_utilSetupThread(thread_num, &cpu_set, hplib_spinLock_Type_LOL);
349 #endif
350     worker_nh[thread_num]=netapi_init(NETAPI_CORE_MASTER,
351                                       NULL);
352     if (worker_nh[thread_num] == NULL)
353     {
354         printf("slow_path_thread: netapi_init failure, exiting\n");
355         exit(1);
356     }
357     NETAPI_HANDLE_T * n = (NETAPI_HANDLE_T *) worker_nh[thread_num];
359    /* open netcp RX channel */
360     //rx_chan = netapi_pktioOpen(worker_nh[thread_num], NETCP_RX, (PKTIO_CB) recv_cb_router, &netcp_rx_cfg,  &err);
364     netapi_setCookie(worker_nh[thread_num],(void*) (thread_num | NET_TEST_SP_THREAD_MASK));
365       
366     scheduler[thread_num] =netapi_schedOpen(worker_nh[thread_num],&our_sched_cfg, &err);
367     if (!scheduler[thread_num]) 
368     {
369         netapi_Log("sched create failed for core%d\n",thread_num); 
370         exit(1);
371     }
372       scheduler[thread_num]->config.yield = NETAPI_FALSE;
373       scheduler[thread_num]->config.pollGarbageQ = NETAPI_TRUE;
374       scheduler[thread_num]->config.pollCtrlQ = NETAPI_TRUE;
375             /*********************************************/
376     /**************Entry point into scheduler ****/
377     /*********************************************/
378     netapi_schedRun(scheduler[thread_num], &err);
379     netapi_Log("slow_path_thread: core %d worker thread done\n",thread_num);
381     //netapi_pktioClose(rx_chan, &err);
382     netapi_shutdown(worker_nh[thread_num]);
386 void fast_path_thread(uint32_t index)
388     int err,i;
389     uint32_t thread_num;
390     PKTIO_HANDLE_T *rx_chan;
391     PKTIO_HANDLE_T *sb_tx_chan;
393     cpu_set_t cpu_set;
395     thread_num = netTestCfg.fp_thread_num[index];
396     printf("fast_path_thread for index %d  called for thread %d\n", index, thread_num);
397     CPU_ZERO( &cpu_set);
398 #ifdef CORTEX_A8
399     for (i = netTestCfg.fp_proc_start[index]; i <= netTestCfg.fp_proc_end[index];i++)
400     {
401         printf("fast_path_thread: setting cpu %d to cpu_set\n", i);
402         CPU_SET( i, &cpu_set);
403     }
404     hplib_utilSetupThread(thread_num, &cpu_set, hplib_spinLock_Type_LOL);
405 #else
406     for (i = netTestCfg.fp_proc_start[index]; i <= netTestCfg.fp_proc_end[index];i++)
407     {
408         printf("fast_path_thread: setting cpu %d to cpu_set\n", i);
409         CPU_SET( i, &cpu_set);
410     }
411     hplib_utilSetupThread(thread_num, &cpu_set, hplib_spinLock_Type_LOL);
412 #endif
413     hplib_mSpinLockLock(&net_test_thread_lock);
414     worker_nh[thread_num]=netapi_init(NETAPI_CORE_MASTER,
415                                       NULL);
416     if (worker_nh[thread_num] == NULL)
417     {
418         printf("fast_path_thread: netapi_init failure, exiting\n");
419         hplib_mSpinLockUnlock(&net_test_thread_lock);
420         exit(1);
421     }
422     hplib_mSpinLockUnlock(&net_test_thread_lock);
423     NETAPI_HANDLE_T * n = (NETAPI_HANDLE_T *) worker_nh[thread_num];
425    /* open netcp RX channel */
426     rx_chan = netapi_pktioOpen(worker_nh[thread_num], NETCP_RX, (PKTIO_CB) recv_cb_router, &netcp_rx_cfg,  &err);
427     netapi_setCookie(worker_nh[thread_num],(void*)thread_num);
429     scheduler[thread_num] =netapi_schedOpen(worker_nh[thread_num],&our_sched_cfg, &err);
430     if (!scheduler[thread_num]) 
431         {
432         netapi_Log("sched create failed for core%d\n",thread_num); 
433         exit(1);
434     }
436     /*********************************************/
437     /**************Entry point into scheduler ****/
438     /*********************************************/
439     scheduler[thread_num]->config.yield = NETAPI_FALSE;
440      scheduler[thread_num]->config.pollGarbageQ = NETAPI_FALSE;
441      scheduler[thread_num]->config.pollCtrlQ = NETAPI_FALSE;
442      //sleep(100000);
443     netapi_schedRun(scheduler[thread_num], &err);
444     netapi_Log("fast_path_thread: core %d worker thread done\n",thread_num);
446     netapi_pktioClose(rx_chan, &err);
447     netapi_shutdown(worker_nh[thread_num]);
449 #endif
451 /***************************************
452  ********** test driver*****************
453  ***************************************/
454 int main(int argc, char **argv)
456     int err,i;
457     Pktlib_HeapCfg      heapCfg;
458     int32_t             errCode;
459     Pktlib_HeapIfTable*  pPktifTable;
460     FILE * fpr = NULL;
462     cpu_set_t cpu_set;
463     /* install signal handler for ^c */
464     signal(SIGINT,netTest_utilMySig);
466     if (argc == 2)
467     {
468         fpr = fopen(argv[1], "r");
469     }
470     else
471     {
472         fpr = fopen(input_file_name, "r");
473     }
474     if (fpr == NULL)
475     {
476         exit(1);
477     }
478     else
479     {
480         if (initRm())
481         {
482             printf("main: initRm() returned error\n");
483             exit(1);
484         }
485         hplib_mSpinLockInit(&net_test_thread_lock );
486         our_netapi_default_cfg.rmHandle = rmClientServiceHandle;
487         memset(&config_file, 0, sizeof(netTestConfigFile_t));
488         memset(&netTestCfg, 0, sizeof(netTestConfig_t));
489         netTest_utilProcessConfigFile(fpr,&config_file);
490         netTest_utilParseMac(&config_file);
492         /* parse slow path/fast path thread configuration parameters */
493         netTest_utilParseThreadParams(&config_file);
495         netTest_utilParseIP(&config_file);
497         netTest_utilParseIpsecMode(&config_file);
499         /* DSP mac processing */
500         parse_dsp_mac(&config_file.dsp_mac[0]);
502        /* DSP IP processing */
503         parse_dsp_ip(&config_file.dsp_ip[0]);
505         netTest_utilParseRoutes(&config_file, &routes[0], &our_router);
506         
507         /* IPSEC interface number processing */
508         parse_simple_param_u32((char*)&config_file.ipsec_if_no[0], &netTestCfg.ipsec_if_no);
510         netTest_utilParseSA(&config_file);
511     }
513     memset(&sa_info, 0, sizeof(sa_info));
515 #ifdef netTest_MULTI_THREAD
516     /* assign main net_test thread to run on core 0 */
517     CPU_ZERO( &cpu_set);
518     CPU_SET( 0, &cpu_set);
519     hplib_utilSetupThread(0, &cpu_set, hplib_spinLock_Type_LOL);
520 #endif
521     /* create netapi */
522     our_netapi_default_cfg.rmHandle = rmClientServiceHandle;
523     netapi_handle = netapi_init(NETAPI_SYS_MASTER,
524                                 &our_netapi_default_cfg);
526     if (netapi_handle == NULL)
527     {
528         printf("main: netapi_init failure, exiting\n");
529         exit(1);
530     }
531     /* configure expection packet handling with netapi */
532     netapi_netcpCfgExceptions(netapi_handle, NETCP_CFG_ALL_EXCEPTIONS, NETCP_CFG_ACTION_DISCARD, (NETCP_CFG_ROUTE_HANDLE_T) NULL);
533     expPkt_appid = netapi_netcpCfgExceptions(netapi_handle, 7, NETCP_CFG_ACTION_TO_SW, (NETCP_CFG_ROUTE_HANDLE_T) NULL);
535     /* open the main heap */
536     ourHeap = Pktlib_findHeapByName("netapi");
537     if (!ourHeap)
538     {
539         netapi_Log("Pktlib_findHeapByName()  fail\n");
540         exit(1);
541     }
543     /* Open all required PKTIO TX channels */
544     open_pktio_tx_channels();
546     netapi_Log("net_test> %d bytes left in our CMA area\n", netapi_getBufMemRemainder());
547     /* create scheduler instance */
548     our_sched =netapi_schedOpen(netapi_handle,&our_sched_cfg, &err);
549     if (!our_sched) {netapi_Log("sched create failed\n"); exit(1);}
552     /*create net_test MAC interfaces, attach IP to created MAC interfaces */
553     netTest_utilCreateInterfaces(netTestCfg.num_macs, netTestCfg.num_ips);
555     /* Lookup Database for SA context, this is used by packet processing routines to get RX and TX SA information*/
556     p_trie_sa_rx = trie_new();
557     p_trie_sa_tx = trie_new();
558    if (!p_trie_sa_rx || !p_trie_sa_tx)
559         {netapi_Log("trie alloc for SA  failed\n"); exit(1);}
561     /* Create RX SA's, RX Policy and TX SA's, all SA configuration parameters are read from net_test_config.txt file */
562     netTest_utilCreateSecAssoc();
565 #ifdef netTest_MULTI_THREAD
567     int c;
568     /* create and set affinity of slow path and fast path threads to
569     * specific CPU cores as specified in the net_test_config.txt file */
570      netTest_utilCreateSpFpThreads(netTestCfg.num_sp_threads, 
571                                   (NET_TEST_FUNC_PTR) slow_path_thread,
572                                   netTestCfg.num_fp_threads,
573                                   (NET_TEST_FUNC_PTR) fast_path_thread);
575         //this thread of execution (main) now just waits on user input
576         for(;;)
577         {
578            printf(">");
579            c=getchar();
580            if (c=='q') {QUIT=1;break;}
581            else if (c=='s') netTest_utilsStatsCb(netapi_handle, &netcp_stats);
582            else if (c=='h') printf("'q' to quit,  's' for stats, 'h' for help\n");
583         }
584         netTest_utilRemoveSpFpThreads(netTestCfg.num_sp_threads, netTestCfg.num_fp_threads);
586 #else
587     /*********************************************/
588     /**************Entry point into scheduler ****/
589     /*********************************************/
590     netapi_schedRun(our_sched, &err);
591 #endif
593 /* done */
594 netTest_utilsStatsCb(netapi_handle, NULL);
598     /* cleanup*/
599     netTest_utilDeleteSecAssoc();
600     netTest_utilDeleteInterfaces(netTestCfg.num_macs, netTestCfg.num_ips);
602     /* close pktio channels we opened via open_pktio_tx_channels() */
603     close_pktio_channels();
604     netapi_shutdown(netapi_handle);
607 static inline void send_it(Ti_Pkt *tip, int len, netTestSA_t * p_sec, int out_port)
609   unsigned long st1;
610   unsigned long st2;
611   int err=0;
612   PKTIO_METADATA_T meta2 = {PKTIO_META_TX,{0},0};
613   nwalTxPktInfo_t meta_tx2={0};
614   st1=hplib_mUtilGetPmuCCNT();
615 #ifdef netTest_MULTI_THREAD
616     int coreid=Osal_nwalGetProcId();  //who we are(thread local)
617     //int coreid = our_core;
618 #else
619     int coreid=0;
620 #endif
621   if (len<60) 
622   { 
623      unsigned int templen;
624      char * p_pkt;
625      len=60; 
626      Pktlib_getDataBuffer(tip,(uint8_t**)&p_pkt,&templen);//ignore templen
627      Cppi_setData (Cppi_DescType_HOST, (Cppi_Desc *) tip, p_pkt,len);
628      stats[coreid].tx_min+=1;
629   }
630   Pktlib_setPacketLen(tip,len);
631   meta_tx2.txFlag1 = (NWAL_TX_FLAG1_DO_IPV4_CHKSUM| NWAL_TX_FLAG1_META_DATA_VALID );
632   meta_tx2.startOffset = 0;
633   meta_tx2.ipOffBytes = netTest_MAC_HEADER_LEN;
634   meta_tx2.ploadLen = len ;
635   meta_tx2.enetPort=out_port;
636   if(p_sec)
637   {   
638        meta_tx2.txFlag1 |= NWAL_TX_FLAG1_DO_IPSEC_ESP_CRYPTO ;
639        meta2.sa_handle = (void*)p_sec->tx_tunnel;
640        meta_tx2.saOffBytes=netTest_MAC_HEADER_LEN + netTest_IP_HEADER_LEN;
641        meta_tx2.saPayloadLen=len-netTest_MAC_HEADER_LEN - netTest_IP_HEADER_LEN;   //don't include tag, mac and outer header
642        meta2.u.tx_meta=&meta_tx2;
643        netapi_pktioSend(netcp_tx_chan_esp,tip,&meta2,&err);
644        stats[coreid].sec_tx+=1;
645   }
646   else
647   {
648       meta2.u.tx_meta=&meta_tx2;
649       netapi_pktioSend(netcp_tx_chan_no_crypto,tip,&meta2,&err);
651   }
652   stats[coreid].tx +=1;
653   st2=hplib_mUtilGetPmuCCNT();
654   stats[coreid].send_cycles += (unsigned long long) (st2-st1);  
655
656 void recv_cb_router(struct PKTIO_HANDLE_Tag * channel, Ti_Pkt* p_recv[],
657                          PKTIO_METADATA_T meta[], int n_pkts,
658                          uint64_t ts )
660     int i;
661     int len;
662     int p;
663     Ti_Pkt * tip;
664     unsigned int templen;
665     char * p_pkt;
666     netTestHead_T temp_head;
667     unsigned int appid;
668     IP_netTestHead_T th;
669     netTestSA_t *sec_data=NULL;
670     unsigned long t1;
671     unsigned long t2;
672     unsigned long long ct1;
673     unsigned long long ct2;
674     unsigned short ip_pl;
675     unsigned long long n_c_ops;
676     int ifno;
677     int out_port;
678 #ifdef netTest_MULTI_THREAD
679         int coreid=Osal_nwalGetProcId();  //who we are(thread local)
680         //int coreid = our_core;
681 #else
682     int coreid=0;
683 #endif
684     t1=hplib_mUtilGetPmuCCNT();
685     ct1 =Osal_cache_op_measure(&n_c_ops);
686     for(i=0;i<n_pkts;i++)
687     {
688         ifno = ((unsigned int)meta[i].u.rx_meta->appId)&0xff;
689         if(coreid<TUNE_NETAPI_NUM_CORES) 
690         {
691             stats[coreid].rx+=1;
692             if (ifno < TUNE_NETAPI_MAX_NUM_MAC)
693                 stats[coreid].if_rx[ifno]+=1;
694         }
695         tip = p_recv[i];
696         Pktlib_getDataBuffer(tip,(uint8_t**)&p_pkt,&templen);//ignore templen
697         len = Pktlib_getPacketLen(tip)-4;//real length, subtract mac trailer
698         appid = ((unsigned int)meta[i].u.rx_meta->appId)&0xff000000;
699         switch(appid)
700         {
701            case(NETAPI_NETCP_MATCH_IPSEC):
702            case(NETAPI_NETCP_MATCH_IPSEC_POLICY):
703            {
704              int tailen=12+2;
705              memcpy(&temp_head,&p_pkt[14],sizeof(netTestHead_T));
706              if (!netTest_utilCheckHeader(&temp_head,&meta[i])) {
707                 stats[coreid].n_bad+=1;
708                 Pktlib_freePacket(tip); 
709                 continue;
710              }
711              tailen+=p_pkt[len-12-2]; //padding length  (12)should come from sec_ptr
712              p_pkt = &p_pkt[8+16+20];   //16= iv len, should come from sec_ptr
713              len -= (8+16+20+tailen);  //16= iv len should come from sec ptr
714             
715              //now check inner headder.
716              memcpy(&th,&p_pkt[14],20);
717              if (!netTest_utilCheckHeader(&temp_head,&meta[i])) {
718                 stats[coreid].n_bad+=1;
719                 Pktlib_freePacket(tip);
720                 continue;
721              }
722              Cppi_setData (Cppi_DescType_HOST, (Cppi_Desc *) tip, p_pkt,len);
723              Pktlib_setPacketLen(tip,len);
724              
725              if (route_pkt(our_router, tip, &th, p_pkt, &len,&sec_data,&out_port)<0)
726               {
727                 stats[coreid].n_bad+=1;
728                 Pktlib_freePacket(tip);
729              }
730              else
731              {
732                send_it(tip,len,sec_data,out_port);
733              }
734              break;
735            }
736            case(NETAPI_NETCP_MATCH_GENERIC_MAC):
737               if((p_pkt[12]!=0x8)||(p_pkt[13]!=0x00)) 
738               {
739                 stats[coreid].n_new+=1;
740                 Pktlib_freePacket(tip); 
741                 continue;
742               }
743               if (!netTest_utilCheckHeader(&temp_head,&meta[i])) 
744               {
745                 stats[coreid].n_bad+=1;
746                 Pktlib_freePacket(tip);
747                 continue;
748               }
749               memcpy(&th,&p_pkt[14],20);
750               ip_pl= (((unsigned char *)&th.w1)[2]<<8) | ((unsigned char *)&th.w1)[3];
751               if ((ip_pl+14)<60)
752               {
753                 len-= (60-(ip_pl+14));
754                 stats[coreid].rx_min+=1;
755               }
756               Pktlib_setPacketLen(tip,len);
757               if (route_pkt(our_router, tip, &th, p_pkt, &len,&sec_data,&out_port)<0)
758               {
759                 stats[coreid].n_bad+=1;
760                 Pktlib_freePacket(tip);
761               }
762               else
763               {
764                  send_it(tip,len,sec_data,out_port);
765               }
766               break;
767            case(NETAPI_NETCP_MATCH_GENERIC_IP):
768               Pktlib_freePacket(tip); 
769               stats[coreid].n_new=1;
770               break;
771            default:
772               stats[coreid].n_new+=1;
773               Pktlib_freePacket(tip);
774               break;
775        }
777 t2=hplib_mUtilGetPmuCCNT();
778 ct2 =Osal_cache_op_measure(&n_c_ops);
779 stats[coreid].app_cycles +=  (unsigned long long) (t2-t1);
780 stats[coreid].tx_cache_cycles += (unsigned long long) (ct2-ct1);
781 return;
784 /* STUB functions required for compilation */
785 void recv_cb(struct PKTIO_HANDLE_Tag * channel, Ti_Pkt* p_recv[],
786                          PKTIO_METADATA_T meta[], int n_pkts,
787                          uint64_t ts )
791 void recv_sb_cb(struct PKTIO_HANDLE_Tag * channel, Ti_Pkt* p_recv[],
792                          PKTIO_METADATA_T meta[], int n_pkts,
793                          uint64_t ts )