Integrate net_test_router with RM.
[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;
56 #if defined(DEVICE_K2H)
57 #include <ti/drv/qmss/device/k2h/src/qmss_device.c>
58 #include <ti/drv/cppi/device/k2h/src/cppi_device.c>
59 #elif defined (DEVICE_K2K)
60 #include <ti/drv/qmss/device/k2k/src/qmss_device.c>
61 #include <ti/drv/cppi/device/k2k/src/cppi_device.c>
62 #elif defined (DEVICE_K2L)
63 #include <ti/drv/qmss/device/k2l/src/qmss_device.c>
64 #include <ti/drv/cppi/device/k2l/src/cppi_device.c>
65 #elif defined (DEVICE_K2E)
66 #include <ti/drv/qmss/device/k2e/src/qmss_device.c>
67 #include <ti/drv/cppi/device/k2e/src/cppi_device.c>
68 #else /*Default */
69 #include <ti/drv/qmss/device/k2h/src/qmss_device.c>
70 #include <ti/drv/cppi/device/k2h/src/cppi_device.c>
71 #endif
72 /* Global definitions */
74 extern Rm_ServiceHandle   *rmClientServiceHandle;
76 hplib_spinLock_T net_test_thread_lock;
77 netTestConfig_t netTestCfg;
78 static  netTestConfigFile_t config_file;
80 char    input_file_name[] = "/etc/netapi/net_test_config.txt";
82 nwal_RetValue       nwalRetVal;
83 Pktlib_HeapHandle ourHeap;
85 PKTIO_HANDLE_T *netcp_tx_chan_no_crypto;
86 PKTIO_HANDLE_T *netcp_rx_chan;
87 PKTIO_HANDLE_T *netcp_tx_chan_esp;
88 PKTIO_HANDLE_T *netcp_tx_chan_ah;
93 PKTIO_CFG_T netcp_rx_cfg={PKTIO_RX, PKTIO_NA, PKTIO_NA, 8};
94 PKTIO_CFG_T netcp_tx_cfg={PKTIO_TX, PKTIO_NA, PKTIO_NA, 8};
98 NETCP_CFG_EXCEPTION_PKT_T expPkt_appid;
100 Trie *p_trie_sa_rx;
101 Trie *p_trie_sa_tx;
104 #include "router.c"
105 Trie * our_router;
107 OUR_ROUTE_T routes[MAX_ROUTES]=
110     {0,{0xD4,0xbe,0xd9,0x00,0xd3,0x7e, 0x00,0x01,0x02,0x03,0x14,0x02,0x08,0x00},0},
111     {0,{0x00,0x00,0x0,0x00,0x0,0x0, 0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00},0},
112     {0,{0xD4,0xbe,0xd9,0x00,0xd3,0x7e, 0x00,0x01,0x02,0x03,0x14,0x02,0x08,0x00},0},
113     {0,{0x00,0x15,0x60,0xa1,0xf7,0xbe, 0x00,0x01,0x02,0x03,0x04,0x05,0x08,0x00},0},
114     {0,{0xd4,0xbe,0xd9,0x00,0xd3,0x7e, 0x00,0x01,0x02,0x03,0x04,0x05,0x08,0x00},0}
115 };
116 unsigned int ip[MAX_ROUTES]={BE(0x0a0100c8),BE(0x0a00000a),BE(0x0a02000a),BE(0xc0a8010a),BE(0x9eda6719)};
118 void recv_cb_router(struct PKTIO_HANDLE_Tag * channel, Ti_Pkt* p_recv[],
119                          PKTIO_METADATA_T meta[], int n_pkts,
120                          uint64_t ts );
123 extern netTestStats_T stats[TUNE_NETAPI_NUM_CORES];
124 extern paSysStats_t netcp_stats;
126 /*******************************************
127  *************NETAPI OBJECTS***************
128  *****************************************/
129 static NETAPI_CFG_T our_netapi_default_cfg=
131 TUNE_NETAPI_PERM_MEM_SZ,
132 128,  //start of packet offset for hw to place data on rx for default flow
133 TUNE_NETAPI_QM_CONFIG_MAX_DESC_NUM, //max number of descriptors in system
134 TUNE_NETAPI_NUM_GLOBAL_DESC,        //total we will use
135 TUNE_NETAPI_DEFAULT_NUM_BUFFERS,   //#descriptors+buffers in default heap
136 64, //#descriptors w/o buffers in default heap
137 TUNE_NETAPI_DEFAULT_BUFFER_SIZE+128+128,  //size of buffers in default heap
138 128,  //tail room
139 256,  //extra room
140 0,
141 NULL
142 };
146 void house(NETAPI_SCHED_HANDLE_T *s);
147 NETAPI_T netapi_handle;
148 NETAPI_SCHED_HANDLE_T * our_sched;
149 #ifdef netTest_MULTI_THREAD
150 NETAPI_SCHED_HANDLE_T * scheduler[TUNE_NETAPI_NUM_CORES];
151 #endif
152 NETAPI_SCHED_CONFIG_T our_sched_cfg={
153   NETAPI_SCHED_DURATION|NETAPI_SCHED_CBV, 0, house, 5000000  //every 5000000 poll loops
154 };
157 NETCP_CFG_IP_T ip_rule[NET_TEST_MAX_IP];
158 NETCP_CFG_MACIF_T mac[NET_TEST_MAX_MAC];
162 /* security objects. (for loopback mode) */
163 netTestSA_t sa_info[MAX_SEC_INDEX];
166 NETCP_CFG_IPSEC_POLICY_T rx_policy[MAX_SEC_INDEX];
171 /*************************END NETAPI OBJECTS***********************/
173 void update_header(netTestHead_T * p_head, int len)
175    unsigned char *p = (unsigned char *) &p_head->udp[1];
176    len -= (20+14);
177    /* update ip checksum */
178    /* update udp checksum */
179    /* update length */
180    *p= (len&0xff00)>>8;
181    *(p+1) = len&0xff;
184 #ifdef netTest_MULTI_THREAD
185 /* Templates to build command labels at startup up time, required by open_pktio_tx_channels() */
186 nwalTxPktInfo_t txPktInfoESP = 
188     NULL,                                                                                               /* p_pkt */
189     NWAL_TX_FLAG1_DO_IPSEC_ESP_CRYPTO| NWAL_TX_FLAG1_DO_IPV4_CHKSUM| NWAL_TX_FLAG1_META_DATA_VALID,      /* txFlags */
190     0,                                                                                                  /* lpbackPass */
191     1,                                                                                                  /* enetport */
192     0,                                                                                                  /* msuSize */
193     0,                                                                                                   /* startOffset */
194     netTest_MAC_HEADER_LEN  + netTest_IP_HEADER_LEN,                                                    /* saOffBytes */
195     0,                                                                                                  /* saPayLoadLen */
196     0               ,                                                                                    /* saAhIcvOffBytes */
197     0,                                                                                                 /* saAhMacSize */
198     0,                                                                                                  /* etherLenOffBytes */
199     netTest_MAC_HEADER_LEN,                                                        /* ipOffBytes */
200     0,                          /* l4OffBytes */
201     0,                         /* l4HdrLen */
202     0,                                                                                   /* pseudoHdrChecksum */
203     0                                                                                                   /* pLoadLen */
204 };
207 nwalTxPktInfo_t txPktInfoAH = 
209     NULL,                                                                                               /* p_pkt */
210     NWAL_TX_FLAG1_DO_IPSEC_AH_CRYPTO| NWAL_TX_FLAG1_DO_IPV4_CHKSUM | NWAL_TX_FLAG1_META_DATA_VALID,      /* txFlags */
211     0,                                                                                                  /* lpbackPass */
212     0,                                                                                                  /* enetport */
213     0,                                                                                                  /* msuSize */
214     0,                                                                                                   /* startOffset */
215     netTest_MAC_HEADER_LEN  + netTest_IP_HEADER_LEN,                                                    /* saOffBytes */
216     0,                                                                                                  /* saPayLoadLen */
217     netTest_MAC_HEADER_LEN + netTest_IP_HEADER_LEN + netTest_IPSEC_AH_FIXED_HDR_SIZE,                    /* saAhIcvOffBytes */
218     12,                                                                                                 /* saAhMacSize */
219     0,                                                                                                  /* etherLenOffBytes */
220     netTest_MAC_HEADER_LEN,   /* ipOffBytes */
221     0,                    /* l4OffBytes */
222     netTest_UDP_HEADER_LEN,                                                                            /* l4HdrLen */
223     0,                                                                                                  /* pseudoHdrChecksum */
224     0                                                                                                   /* pLoadLen */
225 };
227 nwalTxPktInfo_t txPktInfoNoCrypto = 
229     NULL,                                                                                               /* p_pkt */
230     NWAL_TX_FLAG1_DO_IPV4_CHKSUM| NWAL_TX_FLAG1_META_DATA_VALID,      /* txFlags */
231     0,                                                                                                  /* lpbackPass */
232     0,                                                                                                  /* enetport */
233     0,                                                                                                  /* msuSize */
234     0,                                                                                                   /* startOffset */
235     0,                                                    /* saOffBytes */
236     0,                                                                                                  /* saPayLoadLen */
237     0               ,                                                                                    /* saAhIcvOffBytes */
238     0,                                                                                                 /* saAhMacSize */
239     0,                                                                                                  /* etherLenOffBytes */
240     netTest_MAC_HEADER_LEN,         /* ipOffBytes */
241     netTest_MAC_HEADER_LEN +netTest_IP_HEADER_LEN,                                        /* l4OffBytes */
242     netTest_UDP_HEADER_LEN,                                                             /* l4HdrLen */
243     0,                                                                         /* pseudoHdrChecksum */
244     0                                                                                                   /* pLoadLen */
245 };
247 void close_pktio_channels(void)
249     int err;
250     netapi_pktioClose(netcp_tx_chan_no_crypto ,&err);
251     netapi_pktioClose(netcp_tx_chan_esp ,&err);
252     netapi_pktioClose(netcp_tx_chan_ah ,&err);
257 void open_pktio_tx_channels()
259     int err;
260     /* open netcp default TX channels for non-ipsec*/
261     netcp_tx_chan_no_crypto= netapi_pktioOpen(netapi_handle, NETCP_TX, NULL, &netcp_tx_cfg,  &err);
262     if (!netcp_tx_chan_no_crypto)
263     {
264         netapi_Log("pktio open TX failed err=%d\n",err);
265         exit(1);
266     }
267     else
268     {
269             PKTIO_CONTROL_T control;
270             control.op = PKTIO_UPDATE_FAST_PATH;
271             PKTIO_CFG_T cfg;
272             cfg.fast_path_cfg.fp_send_option = PKTIO_FP_NO_CRYPTO_NO_CKSUM_PORT;
273             cfg.fast_path_cfg.txPktInfo= &txPktInfoNoCrypto;
274             netapi_pktioControl(netcp_tx_chan_no_crypto, NULL, &cfg, &control, &err);
275     }
276     /* open netcp default TX for ESP packets */
277     netcp_tx_chan_esp= netapi_pktioOpen(netapi_handle, NETCP_TX, NULL, &netcp_tx_cfg,  &err);
278     if (!netcp_tx_chan_esp)
279     {
280         netapi_Log("pktio open TX failed err=%d\n",err);
281         exit(1);
282     }
283     else
284     {
285         if(netTestCfg.ipsec_mode_tx == IPSEC_MODE_TX_INFLOW)
286         {
287             PKTIO_CONTROL_T control;
288             control.op = PKTIO_UPDATE_FAST_PATH;
289             PKTIO_CFG_T cfg;
290             cfg.fast_path_cfg.fp_send_option = PKTIO_FP_ESP_L3CKSUM_PORT;
291             cfg.fast_path_cfg.txPktInfo= &txPktInfoESP;
292             netapi_pktioControl(netcp_tx_chan_esp, NULL, &cfg, &control, &err);
293         }
294     }
296     /*/* open netcp default  TX for AH packets */
297     netcp_tx_chan_ah= netapi_pktioOpen(netapi_handle, NETCP_TX, NULL, &netcp_tx_cfg,  &err);
298     if (!netcp_tx_chan_ah)
299     {
300         netapi_Log("pktio open TX failed err=%d\n",err);
301         exit(1);
302     }
303     else
304     {
305         if(netTestCfg.ipsec_mode_tx == IPSEC_MODE_TX_INFLOW)
306         {
307             PKTIO_CONTROL_T control;
308             control.op = PKTIO_UPDATE_FAST_PATH;
309             PKTIO_CFG_T cfg;
310             cfg.fast_path_cfg.fp_send_option = PKTIO_FP_AH_PORT;
311             cfg.fast_path_cfg.txPktInfo= &txPktInfoAH;
312             netapi_pktioControl(netcp_tx_chan_ah, NULL, &cfg, &control, &err);
313         }
314     }
320 NETAPI_T worker_nh[TUNE_NETAPI_NUM_CORES];
321 void slow_path_thread(uint32_t index)
323     int err,i;
324     uint32_t thread_num;
325     PKTIO_HANDLE_T *rx_chan;
326     PKTIO_HANDLE_T *sb_tx_chan;
327     PKTIO_HANDLE_T *sb_rx_chan;
328     cpu_set_t cpu_set;
330     thread_num = netTestCfg.sp_thread_num[index];
331     printf("slow_path_thread for index %d  called for thread %d\n", index, thread_num);
333     CPU_ZERO( &cpu_set);
334 #ifdef CORTEX_A8
335    for (i = netTestCfg.sp_proc_start[index]; i <= netTestCfg.sp_proc_end[index];i++)
336     {
337         printf("slow_path_thread: setting cpu %d to cpu_set\n", i);
338         CPU_SET( i, &cpu_set);
339     }
340     hplib_utilSetupThread(0, &cpu_set, hplib_spinLock_Type_LOL);
341 #else
342     for (i = netTestCfg.sp_proc_start[index]; i <= netTestCfg.sp_proc_end[index];i++)
343     {
344         printf("slow_path_thread: setting cpu %d to cpu_set\n", i);
345         CPU_SET( i, &cpu_set);
346     }
347     hplib_utilSetupThread(thread_num, &cpu_set, hplib_spinLock_Type_LOL);
348 #endif
349     worker_nh[thread_num]=netapi_init(NETAPI_CORE_MASTER,
350                                       NULL);
351     if (worker_nh[thread_num] == NULL)
352     {
353         printf("slow_path_thread: netapi_init failure, exiting\n");
354         exit(1);
355     }
356     NETAPI_HANDLE_T * n = (NETAPI_HANDLE_T *) worker_nh[thread_num];
358    /* open netcp RX channel */
359     //rx_chan = netapi_pktioOpen(worker_nh[thread_num], NETCP_RX, (PKTIO_CB) recv_cb_router, &netcp_rx_cfg,  &err);
363     netapi_setCookie(worker_nh[thread_num],(void*) (thread_num | NET_TEST_SP_THREAD_MASK));
364       
365     scheduler[thread_num] =netapi_schedOpen(worker_nh[thread_num],&our_sched_cfg, &err);
366     if (!scheduler[thread_num]) 
367     {
368         netapi_Log("sched create failed for core%d\n",thread_num); 
369         exit(1);
370     }
371       scheduler[thread_num]->config.yield = NETAPI_FALSE;
372       scheduler[thread_num]->config.pollGarbageQ = NETAPI_TRUE;
373       scheduler[thread_num]->config.pollCtrlQ = NETAPI_TRUE;
374             /*********************************************/
375     /**************Entry point into scheduler ****/
376     /*********************************************/
377     netapi_schedRun(scheduler[thread_num], &err);
378     netapi_Log("slow_path_thread: core %d worker thread done\n",thread_num);
380     //netapi_pktioClose(rx_chan, &err);
381     netapi_shutdown(worker_nh[thread_num]);
385 void fast_path_thread(uint32_t index)
387     int err,i;
388     uint32_t thread_num;
389     PKTIO_HANDLE_T *rx_chan;
390     PKTIO_HANDLE_T *sb_tx_chan;
392     cpu_set_t cpu_set;
394     thread_num = netTestCfg.fp_thread_num[index];
395     printf("fast_path_thread for index %d  called for thread %d\n", index, thread_num);
396     CPU_ZERO( &cpu_set);
397 #ifdef CORTEX_A8
398     for (i = netTestCfg.fp_proc_start[index]; i <= netTestCfg.fp_proc_end[index];i++)
399     {
400         printf("fast_path_thread: setting cpu %d to cpu_set\n", i);
401         CPU_SET( i, &cpu_set);
402     }
403     hplib_utilSetupThread(thread_num, &cpu_set, hplib_spinLock_Type_LOL);
404 #else
405     for (i = netTestCfg.fp_proc_start[index]; i <= netTestCfg.fp_proc_end[index];i++)
406     {
407         printf("fast_path_thread: setting cpu %d to cpu_set\n", i);
408         CPU_SET( i, &cpu_set);
409     }
410     hplib_utilSetupThread(thread_num, &cpu_set, hplib_spinLock_Type_LOL);
411 #endif
412     hplib_mSpinLockLock(&net_test_thread_lock);
413     worker_nh[thread_num]=netapi_init(NETAPI_CORE_MASTER,
414                                       NULL);
415     if (worker_nh[thread_num] == NULL)
416     {
417         printf("fast_path_thread: netapi_init failure, exiting\n");
418         hplib_mSpinLockUnlock(&net_test_thread_lock);
419         exit(1);
420     }
421     hplib_mSpinLockUnlock(&net_test_thread_lock);
422     NETAPI_HANDLE_T * n = (NETAPI_HANDLE_T *) worker_nh[thread_num];
424    /* open netcp RX channel */
425     rx_chan = netapi_pktioOpen(worker_nh[thread_num], NETCP_RX, (PKTIO_CB) recv_cb_router, &netcp_rx_cfg,  &err);
426     netapi_setCookie(worker_nh[thread_num],(void*)thread_num);
428     scheduler[thread_num] =netapi_schedOpen(worker_nh[thread_num],&our_sched_cfg, &err);
429     if (!scheduler[thread_num]) 
430         {
431         netapi_Log("sched create failed for core%d\n",thread_num); 
432         exit(1);
433     }
435     /*********************************************/
436     /**************Entry point into scheduler ****/
437     /*********************************************/
438     scheduler[thread_num]->config.yield = NETAPI_FALSE;
439      scheduler[thread_num]->config.pollGarbageQ = NETAPI_FALSE;
440      scheduler[thread_num]->config.pollCtrlQ = NETAPI_FALSE;
441      //sleep(100000);
442     netapi_schedRun(scheduler[thread_num], &err);
443     netapi_Log("fast_path_thread: core %d worker thread done\n",thread_num);
445     netapi_pktioClose(rx_chan, &err);
446     netapi_shutdown(worker_nh[thread_num]);
448 #endif
450 /***************************************
451  ********** test driver*****************
452  ***************************************/
453 int main(int argc, char **argv)
455     int err,i;
456     Pktlib_HeapCfg      heapCfg;
457     int32_t             errCode;
458     Pktlib_HeapIfTable*  pPktifTable;
459     FILE * fpr = NULL;
461     cpu_set_t cpu_set;
462     /* install signal handler for ^c */
463     signal(SIGINT,netTest_utilMySig);
465     if (argc == 2)
466     {
467         fpr = fopen(argv[1], "r");
468     }
469     else
470     {
471         fpr = fopen(input_file_name, "r");
472     }
473     if (fpr == NULL)
474     {
475         exit(1);
476     }
477     else
478     {
479         if (initRm())
480         {
481             printf("main: initRm() returned error\n");
482             exit(1);
483         }
484         hplib_mSpinLockInit(&net_test_thread_lock );
485         our_netapi_default_cfg.rmHandle = rmClientServiceHandle;
486         memset(&config_file, 0, sizeof(netTestConfigFile_t));
487         memset(&netTestCfg, 0, sizeof(netTestConfig_t));
488         netTest_utilProcessConfigFile(fpr,&config_file);
489         netTest_utilParseMac(&config_file);
491         /* parse slow path/fast path thread configuration parameters */
492         netTest_utilParseThreadParams(&config_file);
494         netTest_utilParseIP(&config_file);
496         netTest_utilParseIpsecMode(&config_file);
498         /* DSP mac processing */
499         parse_dsp_mac(&config_file.dsp_mac[0]);
501        /* DSP IP processing */
502         parse_dsp_ip(&config_file.dsp_ip[0]);
504         netTest_utilParseRoutes(&config_file, &routes[0], &our_router);
505         
506         /* IPSEC interface number processing */
507         parse_simple_param_u32((char*)&config_file.ipsec_if_no[0], &netTestCfg.ipsec_if_no);
509         netTest_utilParseSA(&config_file);
510     }
512     memset(&sa_info, 0, sizeof(sa_info));
514 #ifdef netTest_MULTI_THREAD
515     /* assign main net_test thread to run on core 0 */
516     CPU_ZERO( &cpu_set);
517     CPU_SET( 0, &cpu_set);
518     hplib_utilSetupThread(0, &cpu_set, hplib_spinLock_Type_LOL);
519 #endif
520     /* create netapi */
521     netapi_handle = netapi_init(NETAPI_SYS_MASTER,
522                                 &our_netapi_default_cfg);
524     if (netapi_handle == NULL)
525     {
526         printf("main: netapi_init failure, exiting\n");
527         exit(1);
528     }
529     /* configure expection packet handling with netapi */
530     netapi_netcpCfgExceptions(netapi_handle, NETCP_CFG_ALL_EXCEPTIONS, NETCP_CFG_ACTION_DISCARD, (NETCP_CFG_ROUTE_HANDLE_T) NULL);
531     expPkt_appid = netapi_netcpCfgExceptions(netapi_handle, 7, NETCP_CFG_ACTION_TO_SW, (NETCP_CFG_ROUTE_HANDLE_T) NULL);
533     /* open the main heap */
534     ourHeap = Pktlib_findHeapByName("netapi");
535     if (!ourHeap)
536     {
537         netapi_Log("Pktlib_findHeapByName()  fail\n");
538         exit(1);
539     }
541     /* Open all required PKTIO TX channels */
542     open_pktio_tx_channels();
544     netapi_Log("net_test> %d bytes left in our CMA area\n", netapi_getBufMemRemainder());
545     /* create scheduler instance */
546     our_sched =netapi_schedOpen(netapi_handle,&our_sched_cfg, &err);
547     if (!our_sched) {netapi_Log("sched create failed\n"); exit(1);}
550     /*create net_test MAC interfaces, attach IP to created MAC interfaces */
551     netTest_utilCreateInterfaces(netTestCfg.num_macs, netTestCfg.num_ips);
553     /* Lookup Database for SA context, this is used by packet processing routines to get RX and TX SA information*/
554     p_trie_sa_rx = trie_new();
555     p_trie_sa_tx = trie_new();
556    if (!p_trie_sa_rx || !p_trie_sa_tx)
557         {netapi_Log("trie alloc for SA  failed\n"); exit(1);}
559     /* Create RX SA's, RX Policy and TX SA's, all SA configuration parameters are read from net_test_config.txt file */
560     netTest_utilCreateSecAssoc();
563 #ifdef netTest_MULTI_THREAD
565     int c;
566     /* create and set affinity of slow path and fast path threads to
567     * specific CPU cores as specified in the net_test_config.txt file */
568      netTest_utilCreateSpFpThreads(netTestCfg.num_sp_threads, 
569                                   (NET_TEST_FUNC_PTR) slow_path_thread,
570                                   netTestCfg.num_fp_threads,
571                                   (NET_TEST_FUNC_PTR) fast_path_thread);
573         //this thread of execution (main) now just waits on user input
574         for(;;)
575         {
576            printf(">");
577            c=getchar();
578            if (c=='q') {QUIT=1;break;}
579            else if (c=='s') netTest_utilsStatsCb(netapi_handle, &netcp_stats);
580            else if (c=='h') printf("'q' to quit,  's' for stats, 'h' for help\n");
581         }
582         netTest_utilRemoveSpFpThreads(netTestCfg.num_sp_threads, netTestCfg.num_fp_threads);
584 #else
585     /*********************************************/
586     /**************Entry point into scheduler ****/
587     /*********************************************/
588     netapi_schedRun(our_sched, &err);
589 #endif
591 /* done */
592 netTest_utilsStatsCb(netapi_handle, NULL);
596     /* cleanup*/
597     netTest_utilDeleteSecAssoc();
598     netTest_utilDeleteInterfaces(netTestCfg.num_macs, netTestCfg.num_ips);
600     /* close pktio channels we opened via open_pktio_tx_channels() */
601     close_pktio_channels();
602     netapi_shutdown(netapi_handle);
605 static inline void send_it(Ti_Pkt *tip, int len, netTestSA_t * p_sec, int out_port)
607   unsigned long st1;
608   unsigned long st2;
609   int err=0;
610   PKTIO_METADATA_T meta2 = {PKTIO_META_TX,{0},0};
611   nwalTxPktInfo_t meta_tx2={0};
612   st1=hplib_mUtilGetPmuCCNT();
613 #ifdef netTest_MULTI_THREAD
614     int coreid=Osal_nwalGetProcId();  //who we are(thread local)
615     //int coreid = our_core;
616 #else
617     int coreid=0;
618 #endif
619   if (len<60) 
620   { 
621      unsigned int templen;
622      char * p_pkt;
623      len=60; 
624      Pktlib_getDataBuffer(tip,(uint8_t**)&p_pkt,&templen);//ignore templen
625      Cppi_setData (Cppi_DescType_HOST, (Cppi_Desc *) tip, p_pkt,len);
626      stats[coreid].tx_min+=1;
627   }
628   Pktlib_setPacketLen(tip,len);
629   meta_tx2.txFlag1 = (NWAL_TX_FLAG1_DO_IPV4_CHKSUM| NWAL_TX_FLAG1_META_DATA_VALID );
630   meta_tx2.startOffset = 0;
631   meta_tx2.ipOffBytes = netTest_MAC_HEADER_LEN;
632   meta_tx2.ploadLen = len ;
633   meta_tx2.enetPort=out_port;
634   if(p_sec)
635   {   
636        meta_tx2.txFlag1 |= NWAL_TX_FLAG1_DO_IPSEC_ESP_CRYPTO ;
637        meta2.sa_handle = (void*)p_sec->tx_tunnel;
638        meta_tx2.saOffBytes=netTest_MAC_HEADER_LEN + netTest_IP_HEADER_LEN;
639        meta_tx2.saPayloadLen=len-netTest_MAC_HEADER_LEN - netTest_IP_HEADER_LEN;   //don't include tag, mac and outer header
640        meta2.u.tx_meta=&meta_tx2;
641        netapi_pktioSend(netcp_tx_chan_esp,tip,&meta2,&err);
642        stats[coreid].sec_tx+=1;
643   }
644   else
645   {
646       meta2.u.tx_meta=&meta_tx2;
647       netapi_pktioSend(netcp_tx_chan_no_crypto,tip,&meta2,&err);
649   }
650   stats[coreid].tx +=1;
651   st2=hplib_mUtilGetPmuCCNT();
652   stats[coreid].send_cycles += (unsigned long long) (st2-st1);  
653
654 void recv_cb_router(struct PKTIO_HANDLE_Tag * channel, Ti_Pkt* p_recv[],
655                          PKTIO_METADATA_T meta[], int n_pkts,
656                          uint64_t ts )
658     int i;
659     int len;
660     int p;
661     Ti_Pkt * tip;
662     unsigned int templen;
663     char * p_pkt;
664     netTestHead_T temp_head;
665     unsigned int appid;
666     IP_netTestHead_T th;
667     netTestSA_t *sec_data=NULL;
668     unsigned long t1;
669     unsigned long t2;
670     unsigned long long ct1;
671     unsigned long long ct2;
672     unsigned short ip_pl;
673     unsigned long long n_c_ops;
674     int ifno;
675     int out_port;
676 #ifdef netTest_MULTI_THREAD
677         int coreid=Osal_nwalGetProcId();  //who we are(thread local)
678         //int coreid = our_core;
679 #else
680     int coreid=0;
681 #endif
682     t1=hplib_mUtilGetPmuCCNT();
683     ct1 =Osal_cache_op_measure(&n_c_ops);
684     for(i=0;i<n_pkts;i++)
685     {
686         ifno = ((unsigned int)meta[i].u.rx_meta->appId)&0xff;
687         if(coreid<TUNE_NETAPI_NUM_CORES) 
688         {
689             stats[coreid].rx+=1;
690             if (ifno < TUNE_NETAPI_MAX_NUM_MAC)
691                 stats[coreid].if_rx[ifno]+=1;
692         }
693         tip = p_recv[i];
694         Pktlib_getDataBuffer(tip,(uint8_t**)&p_pkt,&templen);//ignore templen
695         len = Pktlib_getPacketLen(tip)-4;//real length, subtract mac trailer
696         appid = ((unsigned int)meta[i].u.rx_meta->appId)&0xff000000;
697         switch(appid)
698         {
699            case(NETAPI_NETCP_MATCH_IPSEC):
700            case(NETAPI_NETCP_MATCH_IPSEC_POLICY):
701            {
702              int tailen=12+2;
703              memcpy(&temp_head,&p_pkt[14],sizeof(netTestHead_T));
704              if (!netTest_utilCheckHeader(&temp_head,&meta[i])) {
705                 stats[coreid].n_bad+=1;
706                 Pktlib_freePacket(tip); 
707                 continue;
708              }
709              tailen+=p_pkt[len-12-2]; //padding length  (12)should come from sec_ptr
710              p_pkt = &p_pkt[8+16+20];   //16= iv len, should come from sec_ptr
711              len -= (8+16+20+tailen);  //16= iv len should come from sec ptr
712             
713              //now check inner headder.
714              memcpy(&th,&p_pkt[14],20);
715              if (!netTest_utilCheckHeader(&temp_head,&meta[i])) {
716                 stats[coreid].n_bad+=1;
717                 Pktlib_freePacket(tip);
718                 continue;
719              }
720              Cppi_setData (Cppi_DescType_HOST, (Cppi_Desc *) tip, p_pkt,len);
721              Pktlib_setPacketLen(tip,len);
722              
723              if (route_pkt(our_router, tip, &th, p_pkt, &len,&sec_data,&out_port)<0)
724               {
725                 stats[coreid].n_bad+=1;
726                 Pktlib_freePacket(tip);
727              }
728              else
729              {
730                send_it(tip,len,sec_data,out_port);
731              }
732              break;
733            }
734            case(NETAPI_NETCP_MATCH_GENERIC_MAC):
735               if((p_pkt[12]!=0x8)||(p_pkt[13]!=0x00)) 
736               {
737                 stats[coreid].n_new+=1;
738                 Pktlib_freePacket(tip); 
739                 continue;
740               }
741               if (!netTest_utilCheckHeader(&temp_head,&meta[i])) 
742               {
743                 stats[coreid].n_bad+=1;
744                 Pktlib_freePacket(tip);
745                 continue;
746               }
747               memcpy(&th,&p_pkt[14],20);
748               ip_pl= (((unsigned char *)&th.w1)[2]<<8) | ((unsigned char *)&th.w1)[3];
749               if ((ip_pl+14)<60)
750               {
751                 len-= (60-(ip_pl+14));
752                 stats[coreid].rx_min+=1;
753               }
754               Pktlib_setPacketLen(tip,len);
755               if (route_pkt(our_router, tip, &th, p_pkt, &len,&sec_data,&out_port)<0)
756               {
757                 stats[coreid].n_bad+=1;
758                 Pktlib_freePacket(tip);
759               }
760               else
761               {
762                  send_it(tip,len,sec_data,out_port);
763               }
764               break;
765            case(NETAPI_NETCP_MATCH_GENERIC_IP):
766               Pktlib_freePacket(tip); 
767               stats[coreid].n_new=1;
768               break;
769            default:
770               stats[coreid].n_new+=1;
771               Pktlib_freePacket(tip);
772               break;
773        }
775 t2=hplib_mUtilGetPmuCCNT();
776 ct2 =Osal_cache_op_measure(&n_c_ops);
777 stats[coreid].app_cycles +=  (unsigned long long) (t2-t1);
778 stats[coreid].tx_cache_cycles += (unsigned long long) (ct2-ct1);
779 return;
782 /* STUB functions required for compilation */
783 void recv_cb(struct PKTIO_HANDLE_Tag * channel, Ti_Pkt* p_recv[],
784                          PKTIO_METADATA_T meta[], int n_pkts,
785                          uint64_t ts )
789 void recv_sb_cb(struct PKTIO_HANDLE_Tag * channel, Ti_Pkt* p_recv[],
790                          PKTIO_METADATA_T meta[], int n_pkts,
791                          uint64_t ts )