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