]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - keystone-rtos/netapi.git/blob - ti/runtime/netapi/test/net_test.c
Adding netcp_cfgExceptions API and related changes, fixing AH support using nwal...
[keystone-rtos/netapi.git] / ti / runtime / netapi / test / net_test.c
1 /******************************************
2  * File: net_test.c
3  * Purpose: test app for netapi
4  **************************************************************
5  * FILE:  net_test.c
6  * 
7  * DESCRIPTION:  netapi user space transport
8  *               library  test application
9  * 
10  * REVISION HISTORY:  rev 0.0.1 
11  *
12  *  Copyright (c) Texas Instruments Incorporated 2010-2011
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  *****************************************/
43 //#define NET_TEST_ENABLE_SIDE_BAND_LOW_LEVEL_API
44 #ifdef NET_TEST_ENABLE_SIDE_BAND_LOW_LEVEL_API
45 #define NWAL_ENABLE_SA
46 #endif
47 #define AH_SUPPORT
48 #define NET_TEST_ENABLE_INFLOW_LOW_LEVEL_API
49 #ifdef NET_TEST_ENABLE_INFLOW_LOW_LEVEL_API
50 #define NWAL_ENABLE_SA
51 #endif
53 //define this for multi-thread
54 #define MULTI_THREAD
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <unistd.h>
58 #include <string.h>
59 #include <signal.h>
60 #include <pthread.h>
62 #include "trie.h"
63 #include "string.h"
64 #include "netapi.h"
65 #include "pktio.h"
66 #include <sys/resource.h>
67 #include "net_test.h"
68 #include <ti/drv/sa/salld.h>
69 #include <ti/drv/pa/pa.h>
71 //#define EXPERIMENTAL
72 #ifdef EXPERIMENTAL
73 #include "router.c"
74 Trie * our_router;
77 OUR_ROUTE_T routes[MAX_ROUTES]=
78 {
79 {0,{0xD4,0xbe,0xd9,0x00,0xd3,0x7e, 0x00,0x01,0x02,0x03,0x14,0x02,0x08,0x00},0},
80 {0,{0x00,0x00,0x0,0x00,0x0,0x0, 0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00},0},
81 {0,{0xD4,0xbe,0xd9,0x00,0xd3,0x7e, 0x00,0x01,0x02,0x03,0x14,0x02,0x08,0x00},0},
82 {0,{0x00,0x15,0x60,0xa1,0xf7,0xbe, 0x00,0x01,0x02,0x03,0x04,0x05,0x08,0x00},0},
83 {0,{0xd4,0xbe,0xd9,0x00,0xd3,0x7e, 0x00,0x01,0x02,0x03,0x04,0x05,0x08,0x00},0}};
85 unsigned int ip[MAX_ROUTES]={BE(0x0a0100c8),BE(0x0a00000a),BE(0x0a02000a),BE(0xc0a8010a),BE(0x9eda6719)};
87 #endif
89 #ifdef MULTI_THREAD
90     //__thread int our_core;
92     cpu_set_t cpu_set;
93 #endif
95 NETCP_CFG_EXCEPTION_PKT_T expPkt_appid;
96 hplib_spinLock_T stats_lock;
98 //#define TEST_TIMERS
99 static int scnt=0;
100 static int QUIT=0;
101 void mysig(int x)
103   QUIT=1;
104   scnt+=1;
105   printf(">net_test: recv'd signal %d cnt=%d\n",x,scnt);
106   if (scnt > 10) {printf(">net_test: WARNING EXITING WITH PROPER SHUTDOWN, LUTS LEFT ACTIVE\n");exit(1);}
110 static unsigned char all_mac[]={0,0,0,0,0,0};
112 /*************debug********************/
113 void dump_descr(unsigned long *p, int n)
115    printf("--------dump of descriptor %d %x\n", n, (int) p);
116    printf("> %x %x %x %x %x %x %x %x\n",p[0],p[1],p[2],p[3],p[4],p[5],p[6],p[7]);
117    printf("> %x %x %x %x %x %x %x %x\n",p[8],p[9],p[10],p[11],p[12],p[13],p[14],p[15]);
118    printf("-----------------------------\n");
120 void dump_header(unsigned long *p, int n, int a, int r)
122    printf("--------dump of header %d %x appID=%x flag1=%x\n", n, (int) p,a,r);
123    printf("> %x %x %x %x %x %x %x %x\n",p[0],p[1],p[2],p[3],p[4],p[5],p[6],p[7]);
124    printf("> %x %x %x %x %x %x %x %x\n",p[8],p[9],p[10],p[11],p[12],p[13],p[14],p[15]);
125    printf("> %x %x %x %x %x %x %x %x\n",p[16],p[17],p[18],p[19],p[20],p[21],p[22],p[23]);
126    printf("> %x %x %x %x %x %x %x %x\n",p[24],p[25],p[26],p[27],p[28],p[29],p[30],p[31]);
127    printf("-----------------------------\n");
130 static void  netapi_dump_buf
132     unsigned long *                      buf,
133     uint32_t                      buf_length
136     uint8_t                       count = 0;
137     uint16_t                      dump_size;
138     uint8_t*                     tmp_buf;
139     uint8_t                       row_count;
140     static uint8_t                first = 0;
142     //if(first > 2) return;
144     //first++;
146     dump_size = buf_length ;
148     tmp_buf = (uint8_t *)(buf);
150     printf("netapi *:  - 8 bit word hex Length: %d Start \n",buf_length);
151     do
152     {
153         row_count = (dump_size - count);
155         if(row_count == 0)
156         {
157             break;
158         }
160         if(row_count > 4)
161         {
162             row_count = 4;
163         }
165         switch (row_count)
166         {
167             case 4:
168             {
169                 printf("netapi *:%02d : %02x    %02x    %02x    %02x \n",
170                       count,tmp_buf[0],tmp_buf[1],tmp_buf[2],tmp_buf[3]);
171                 break;
172             }
173             case 3:
174             {
175                 printf("netapi *: %02d : %02x    %02x    %02x \n",
176                       count,tmp_buf[0],tmp_buf[1],tmp_buf[2]);
177                 break;
178             }
180             case 2:
181             {
182                 printf("netapi *: %02d : %02x    %02x \n",
183                       count,tmp_buf[0],tmp_buf[1]);
184                 break;
185             }
187             case 1:
188             {
189                 printf("netapi *: %02d : %02x \n",
190                       count,tmp_buf[0]);
191                 break;
192             }
194             default:
195             {
196                 /* Should never reach here */
197                 printf("netapi *: Internal Error in netapi_dump_buf().Row Count: %d \n",
198                     row_count);
199                 return;
200             }
201         }
203         tmp_buf = tmp_buf + row_count;
204         count = count +  row_count;
206     }while(count < dump_size);
208     printf("netapi *:  - Byte hex Dump End \n");
213 /*****************************************/
216 //************for multi pkt burst  xfer test in loopback mode
217 #define TX_BURST 700 
218 int pktloopback=TUNE_NETAPI_NWAL_ENABLE_PASS_LOOPBACK;
219 nwalTxPSCmdInfo_t   flowPSCmdInfo;
220 nwal_RetValue       nwalRetVal;
222 /* Local Per Process default resourcese maintained at NWAL */
223 nwalLocCxtInfo_t    nwalLocCxt;
225 //this device: 10.0.0.100, mac 0x,01,02,03,04,05  and .. 0x6
227 //test packet, setup for loopback (so dest is ourself)
228 static uint8_t testPkt[] = {
230   /* MAC header */
231   0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
232   0x00, 0xe0, 0xa6, 0x66, 0x57, 0x04,
233   0x08, 0x00,
235   /* IP header */
236   0x45, 0x00,
237   0x00, 0x6c,  /* Length (including this header) */
238   0x00, 0x00, 0x00, 0x00, 0x05, 0x11,
239   0x00, 0x00,  /* Header checksum */
240   0x0a, 0x00, 0x00, 0x0a, 0x0a, 0x00, 0x00, 0x64,
242   /* UDP header */
243   0x12, 0x34, 0x05, 0x55,
244   0x00, 0x58,  /* Length, including this header */
245   0x00, 0x00,  /* Header checksum */
247  /* Payload */
248   0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
249   0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41,
250   0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
251   0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51,
252   0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
253   0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 0x60, 0x61,
254   0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
255   0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71,
256   0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
257   0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81
259 };
261 char    input_file_name[] = "net_test_config.txt";
262 #define MAX_LINE_LENGTH 40
264 #define TEST_PAYLOAD_LEN            80
266 #define TEST_PKT_IP_OFFSET_BYTES        14
267 #define TEST_PKT_UDP_OFFSET_BYTES       34
268 #define TEST_PKT_PLOAD_OFFSET_BYTES     42
269 #define TEST_PKT_UDP_HDR_LEN            8
270 /* Offsets to length fields */
271 #define TEST_PKT_OFFSET_IP_LEN      16
272 #define TEST_PKT_OFFSET_UDP_LEN     38
274 #define TEST_PKT_LEN                122
276 /* The pseudo header checksum of the packet except for the 16 bit length */
277 #define TEST_PKT_PSEUDO_HDR_CHKSUM_SANS_LEN  0x0FFC
279 void example_fast_poll( PKTIO_HANDLE_T * p_pktio, int max_pkts);
280 void example_fast_pushpop(Pktlib_HeapHandle p_heap, int ntrials);
282 void recv_cb_router(struct PKTIO_HANDLE_Tag * channel, Ti_Pkt* p_recv[],
283                          PKTIO_METADATA_T meta[], int n_pkts,
284                          uint64_t ts );
287 //#include "arpa/inet.h"
288 long htonl(long x)
290         long temp = (x&0xff000000)>>24 | (x&0xff0000)>>8 | (x&0xff00)<<8 |  (x&0xff)<<24 ;
291         return temp;
295 unsigned long peek(unsigned long * p)
297   return *p;
301 /********************************************************************
302  *  FUNCTION PURPOSE: Ones complement addition utility
303  ********************************************************************
304  ********************************************************************/
305 uint16_t test_utilOnesComplementAdd (uint16_t v1, uint16_t v2)
307   uint32_t result;
309   result = (uint32_t)v1 + (uint32_t)v2;
310   result = (result >> 16) + (result & 0xffff);
311   result = (result >> 16) + (result & 0xffff);
313   return ((uint16_t)result);
316 /********************************************************************
317  *  FUNCTION PURPOSE: Ones complement checksum utility
318  ********************************************************************
319  ********************************************************************/
320  uint16_t test_utilOnesCompChkSum (uint8_t *p, uint32_t nwords)
322   uint16_t chksum = 0;
323   uint16_t v;
324   uint32_t i;
325   uint32_t j;
327   for (i = j = 0; i < nwords; i++, j+=2)  {
328     v = (p[j] << 8) | p[j+1];
329     chksum = test_utilOnesComplementAdd (chksum, v);
330   }
331   return (chksum);
332 } /* utilOnesCompChkSum */
334 /**************************************************************************************
335  * FUNCTION PURPOSE: Compute ipv4 psudo checksum
336  **************************************************************************************
337  * DESCRIPTION: Compute ipv4 psudo checksum
338  **************************************************************************************/
339 uint16_t test_utilGetIpv4PsudoChkSum (uint8_t *data, uint16_t payloadLen)
341   uint16_t psudo_chksum;
343   psudo_chksum = test_utilOnesCompChkSum (&data[12], 4);
344   psudo_chksum = test_utilOnesComplementAdd(psudo_chksum, (uint16_t) data[9]);
345   psudo_chksum = test_utilOnesComplementAdd(psudo_chksum, payloadLen);
347   return (psudo_chksum);
349 } /* utilGetIpv4PsudoChkSum */
353 /* net test default configuration */
354 netTestConfig_t config =
356     {0x00,0x01,0x02,0x03,0x05,0x05},
357     {0x00,0x01,0x02,0x03,0x05,0x06},
358     {10, 0, 0, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
359     {10, 0, 1, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
360     {10, 0, 2, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
361     {192,168 , 1, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
362     {192,168 , 1, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
363     IPSEC_MODE_RX_SIDEBAND,
364     IPSEC_MODE_TX_SIDEBAND,
365     0
366 };
368 #if 1  //goes with real tx (to laptop) 
369 unsigned char real_mac_header[]={0xd4,0xbe,0xd9,0x00,0xd3,0x7e,
370                       0x00,0x01,0x02,0x03,0x04,0x05,
371                       0x08,0x00};
372 unsigned char real_ip_addr[]={0xa,0x00,0x00,0x64,0xa,0x0,0x0,0xa};
373 #endif
375 #if 0  //goes with loopback
376 unsigned char mac_header[]={0x00,0x01,0x02,0x03,0x04,0x05, 
377                       0x00,0x11,0x22,0x33,0x44,0x55,
378                       0x08,0x00};
379 #endif
380 #define NE 65536 
381 HEAD_T *nat;
383 #define NP 5000
384 int n_pkt = NP;
385 STATS_T stats[TUNE_NETAPI_NUM_CORES];
386 paSysStats_t netcp_stats;
388 Trie * P_trie;
389 Trie *p_trie_sa;
390 HEAD_T pkts[NP];
391 #define PERSLOW  10  //% of pkts that will not be fastpath'd 
392 int perslow= PERSLOW;
394 /*******************************************
395  *************NETAPI OBJECTS***************
396  *****************************************/
397 static NETAPI_CFG_T our_netapi_default_cfg=
399 TUNE_NETAPI_PERM_MEM_SZ,
400 128,  //start of packet offset for hw to place data on rx for default flow
401 TUNE_NETAPI_QM_CONFIG_MAX_DESC_NUM, //max number of descriptors in system
402 TUNE_NETAPI_NUM_GLOBAL_DESC,        //total we will use
403 TUNE_NETAPI_DEFAULT_NUM_BUFFERS,   //#descriptors+buffers in default heap
404 64, //#descriptors w/o buffers in default heap
405 TUNE_NETAPI_DEFAULT_BUFFER_SIZE+128+128,  //size of buffers in default heap
406 128   ,  //tail room
407 256      //extra room 
408 };
410 Pktlib_HeapHandle ourHeap;
411 Pktlib_HeapHandle specialSmall;
412 Pktlib_HeapHandle specialLarge;
414 PKTIO_HANDLE_T *our_chan;
415 PKTIO_HANDLE_T *netcp_rx_chan;
416 PKTIO_HANDLE_T *netcp_rx_chan2;
417 PKTIO_HANDLE_T *netcp_tx_chan;
418 PKTIO_HANDLE_T *netcp_sb_tx_chan;
419 PKTIO_HANDLE_T *netcp_sb_rx_chan;
420 PKTIO_CFG_T our_chan_cfg={PKTIO_RW, PKTIO_LOCAL, PKTIO_Q_ANY, 8};
421 PKTIO_CFG_T netcp_rx_cfg={PKTIO_R, PKTIO_NA, PKTIO_NA, 8};
422 PKTIO_CFG_T netcp_rx_cfg2={PKTIO_R, (PKTIO_GLOBAL|PKTIO_PKT), PKTIO_Q_ANY, 8};
423 PKTIO_CFG_T netcp_tx_cfg={PKTIO_W, PKTIO_NA, PKTIO_NA, 8};
424 PKTIO_CFG_T netcp_sb_rx_cfg={PKTIO_R, PKTIO_NA, PKTIO_NA, 8};
425 PKTIO_CFG_T netcp_sb_tx_cfg={PKTIO_W, PKTIO_NA, PKTIO_NA, 8};
427 void house(NETAPI_SCHED_HANDLE_T *s);
428 NETAPI_T netapi_handle;
429 NETAPI_SCHED_HANDLE_T * our_sched;
430 #ifdef MULTI_THREAD
431 NETAPI_SCHED_HANDLE_T * scheduler[TUNE_NETAPI_NUM_CORES];
432 #endif
433 NETAPI_SCHED_CONFIG_T our_sched_cfg={
434   NETAPI_SCHED_DURATION|NETAPI_SCHED_CBV, 0, house, 5000000  //every 5000000 poll loops
435 };
436 void our_stats_cb(NETAPI_T h, paSysStats_t* pPaStats);
437 void our_stats_cb_mt(NETAPI_T h, paSysStats_t* pPaStats);
440 #ifdef TEST_TIMERS
441 HPLIB_TIMER_GROUP_HANDLE_T ourTimerBlock; 
442 HPLIB_TIMER_T t1;
443 HPLIB_TIMER_T t2;
444 HPLIB_TIMER_T t3;
446 void our_timer_cb( HPLIB_TIMER_GROUP_HANDLE_T th,
447         int n_fired,     //# timers fired
448         HPLIB_TIMER_LIST_T fired_list,
449         uint64_t currentTime);
450 #endif
451 NETCP_CFG_IP_T ip_rule0;
452 NETCP_CFG_IP_T ip_rule1;
453 NETCP_CFG_CLASS_T class_0;
454 NETCP_CFG_CLASS_T class_1;
455 NETCP_CFG_CLASS_T class_2;
456 NETCP_CFG_FLOW_HANDLE_T specialFlow;
459 NETCP_CFG_CLASSIFIER_T class_0_cfg=
461    NETCP_CFG_CLASS_TYPE_L4,
462    {
463         .c_l4={0,0, NWAL_APP_PLOAD_PROTO_UDP, {2500}}
464    }
465 };
467 NETCP_CFG_CLASSIFIER_T class_1_cfg=
469     NETCP_CFG_CLASS_TYPE_L4,
470     {
471        .c_l4= {0,0, NWAL_APP_PLOAD_PROTO_UDP, {2502}}
472    }
473 };
475 NETCP_CFG_ROUTE_T  class2_route=
477 NULL, NULL  //* to be filled in
478 };
479 NETCP_CFG_CLASSIFIER_T class_2_cfg=  
481    NETCP_CFG_CLASS_TYPE_L3_L4,
482    {
483         .c_l3_l4={0,  4 ,0/*fill in below*/ , NULL, NULL,          //L2/L3
484            NWAL_APP_PLOAD_PROTO_UDP, {2504}}   //L4
485    }
486 };
488 PKTIO_CONTROL_T zap_channel_control={PKTIO_CLEAR, NULL};
490 /* security objects. (for loopback mode) */
491 netTestSA_t sa_info[7];
492 int netapi_algorithm_set = 0;
493 int netapi_sec_sa_mode = 2;
495 NETCP_CFG_IPSEC_POLICY_T rx_policy[7];
499 NETAPI_SEC_SA_INFO_T rx_sa [7] = {
501     NWAL_SA_DIR_INBOUND,
502     0x11111111,  //spi
503     nwal_IpSecProtoESP, //ESP mode
504     nwal_SA_MODE_TUNNEL,  //tunnel mode
505     nwal_IPV4, //v4
506     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* Src IP (them) -> set below */
507     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* dst IP (us)-> set below*/
508     64,/* replayWindow */
509     NWAL_SA_AALG_HMAC_SHA1,
510     NWAL_SA_EALG_AES_CBC,
511     0,0  //na
512 },
514     NWAL_SA_DIR_INBOUND,
515     0x22222222,  //spi
516     nwal_IpSecProtoESP, //ESP mode
517     nwal_SA_MODE_TUNNEL,  //tunnel mode
518     nwal_IPV4, //v4
519     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* Src IP (them) -> set below */
520     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* dst IP (us)-> set below*/
521     64,/* replayWindow */
522     NWAL_SA_AALG_HMAC_SHA2_256,
523     NWAL_SA_EALG_AES_CTR,
524     0,0  //na
525 },
527     NWAL_SA_DIR_INBOUND,
528     0x33333333,  //spi
529     nwal_IpSecProtoESP, //ESP mode
530     nwal_SA_MODE_TUNNEL,  //tunnel mode
531     nwal_IPV4, //v4
532     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* Src IP (them) -> set below */
533     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* dst IP (us)-> set below*/
534     64,/* replayWindow */
535     NWAL_SA_AALG_HMAC_SHA2_256,
536     NWAL_SA_EALG_3DES_CBC,
537     0,0  //na
538 },
540     NWAL_SA_DIR_INBOUND,
541     0x44444444,  //spi
542     nwal_IpSecProtoAH, //ESP mode
543     nwal_SA_MODE_TUNNEL,  //tunnel mode
544     nwal_IPV4, //v4
545     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* Src IP (them) -> set below */
546     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* dst IP (us)-> set below*/
547     64,/* replayWindow */
548     NWAL_SA_AALG_HMAC_MD5,
549     NWAL_SA_EALG_NULL,
550     0,0  //na
551 },
553     NWAL_SA_DIR_INBOUND,
554     0x55555555,  //spi
555     nwal_IpSecProtoESP, //ESP mode
556     nwal_SA_MODE_TUNNEL,  //tunnel mode
557     nwal_IPV4, //v4
558     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* Src IP (them) -> set below */
559     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* dst IP (us)-> set below*/
560     64,/* replayWindow */
561     NWAL_SA_AALG_NULL,
562     NWAL_SA_EALG_AES_GCM,
563     0,0  //na
564 },
566     NWAL_SA_DIR_INBOUND,
567     0x66666666,  //spi
568     nwal_IpSecProtoESP, //ESP mode
569     nwal_SA_MODE_TUNNEL,  //tunnel mode
570     nwal_IPV4, //v4
571     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* Src IP (them) -> set below */
572     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* dst IP (us)-> set below*/
573     64,/* replayWindow */
574     NWAL_SA_AALG_NULL,
575     NWAL_SA_EALG_AES_CCM,
576     0,0  //na
577 },
579     NWAL_SA_DIR_INBOUND,
580     0x77777777,  //spi
581     nwal_IpSecProtoESP, //ESP mode
582     nwal_SA_MODE_TUNNEL,  //tunnel mode
583     nwal_IPV4, //v4
584     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* Src IP (them) -> set below */
585     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* dst IP (us)-> set below*/
586     64,/* replayWindow */
587     NWAL_SA_AALG_AES_XCBC,
588     NWAL_SA_EALG_NULL,
589     0,0  //na
591 };
593 /*tx */
594 NETAPI_SEC_SA_INFO_T tx_sa[7]= {
596     NWAL_SA_DIR_OUTBOUND,
597     0x11111111,  //spi
598     nwal_IpSecProtoESP, //ESP mode
599     nwal_SA_MODE_TUNNEL,  //tunnel mode
600     nwal_IPV4, //v4
601     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* Src IP (us) -> set below */
602     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* dst IP (them) -> set below*/
603     64, /* NA replayWindow */
604     NWAL_SA_AALG_HMAC_SHA1,
605     NWAL_SA_EALG_AES_CBC,
606     0,0  //seq no
607 },
608     {
609     NWAL_SA_DIR_OUTBOUND,
610     0x22222222,  //spi
611     nwal_IpSecProtoESP, //ESP mode
612     nwal_SA_MODE_TUNNEL,  //tunnel mode
613     nwal_IPV4, //v4
614     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* Src IP (us) -> set below */
615     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* dst IP (them) -> set below*/
616     64, /* NA replayWindow */
617     NWAL_SA_AALG_HMAC_SHA2_256,
618     NWAL_SA_EALG_AES_CTR,
619     0,0  //seq no
620 },
622     NWAL_SA_DIR_OUTBOUND,
623     0x33333333,  //spi
624     nwal_IpSecProtoESP, //ESP mode
625     nwal_SA_MODE_TUNNEL,  //tunnel mode
626     nwal_IPV4, //v4
627     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* Src IP (us) -> set below */
628     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* dst IP (them) -> set below*/
629     64, /* NA replayWindow */
630     NWAL_SA_AALG_HMAC_SHA2_256,
631     NWAL_SA_EALG_3DES_CBC,
632     0,0  //seq no
633 },
635     NWAL_SA_DIR_OUTBOUND,
636     0x44444444,  //spi
637     nwal_IpSecProtoAH, //ESP mode
638     nwal_SA_MODE_TUNNEL,  //tunnel mode
639     nwal_IPV4, //v4
640     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* Src IP (us) -> set below */
641     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* dst IP (them) -> set below*/
642     64, /* NA replayWindow */
643     NWAL_SA_AALG_HMAC_MD5,
644     NWAL_SA_EALG_NULL,
645 },
647     NWAL_SA_DIR_OUTBOUND,
648     0x55555555,  //spi
649     nwal_IpSecProtoESP, //ESP mode
650     nwal_SA_MODE_TUNNEL,  //tunnel mode
651     nwal_IPV4, //v4
652     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* Src IP (them) -> set below */
653     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* dst IP (us)-> set below*/
654     64,/* replayWindow */
655     NWAL_SA_AALG_NULL,
656     NWAL_SA_EALG_AES_GCM,
657     0,0  //na
658 },
660     NWAL_SA_DIR_OUTBOUND,
661     0x66666666,  //spi
662     nwal_IpSecProtoESP, //ESP mode
663     nwal_SA_MODE_TUNNEL,  //tunnel mode
664     nwal_IPV4, //v4
665     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* Src IP (them) -> set below */
666     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* dst IP (us)-> set below*/
667     64,/* replayWindow */
668     NWAL_SA_AALG_NULL,
669     NWAL_SA_EALG_AES_CCM,
670     0,0  //na
671 },
673     NWAL_SA_DIR_OUTBOUND,
674     0x77777777,  //spi
675     nwal_IpSecProtoESP, //ESP mode
676     nwal_SA_MODE_TUNNEL,  //tunnel mode
677     nwal_IPV4, //v4
678     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* Src IP (them) -> set below */
679     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* dst IP (us)-> set below*/
680     64,/* replayWindow */
681     NWAL_SA_AALG_AES_XCBC,
682     NWAL_SA_EALG_NULL,
683     0,0  //na
685 };
688 static nwalSecKeyParams_t ourTXKeyParams[7] ={
690     32, /* encKeySize: CTR 16 bytes Encryption Key and 4 bytes Salt : 24 bytes:NWAL_SA_EALG_3DES_CBC and 0 bytes Salt*/
691     20, /* macKeySize: 16 bytes NWAL_SA_AALG_HMAC_SHA1 */
692     NULL, //set below
693     NULL, //set below
694 },
696     20, /* encKeySize: CTR 16 bytes Encryption Key and 4 bytes Salt : 24 bytes:NWAL_SA_EALG_AES_CTR and 0 bytes Salt*/
697     32, /* macKeySize: 16 bytes NWAL_SA_AALG_HMAC_SHA2_256 */
698     NULL, //set below
699     NULL, //set below
700 },
702     24, /* encKeySize: DES-CBC: 24 bytes:NWAL_SA_EALG_3DES_CBC and 0 bytes Salt*/
703     32, /* macKeySize: 16 bytes NWAL_SA_AALG_HMAC_SHA2_256 */
704     NULL, //set below
705     NULL, //set below
706 },
708     0, /* NULL*/
709     16, /* MD5, 16 bytes */
710     NULL, //set below
711     NULL, //set below
712 },
714     20, /* encKeySize: GCM 16 bytes Encryption Key and 4 bytes Salt */
715     0, /* macKeySize: 0*/
716     NULL, //set below
717     NULL, //set below
718 },
720     19, /* encKeySize: CTR 16 bytes Encryption Key and 3 bytes Salt : 24 bytes:NWAL_SA_EALG_AES_CTR and 0 bytes Salt*/
721     0, /* macKeySize 0*/
722     NULL, //set below
723     NULL, //set below
724 },
726     0, /* encKeySize: CTR 16 bytes Encryption Key and 3 bytes Salt : 24 bytes:NWAL_SA_EALG_AES_CTR and 0 bytes Salt*/
727     16, /* macKeySize 0*/
728     NULL, //set below
729     NULL, //set below
731 };
733 /* these keys are for aes-ctr and hmac sha2_256 */
734 static nwalSecKeyParams_t ourRXKeyParams[7] ={
736     32, /* encKeySize: CTR 16 bytes Encryption Key and 4 bytes Salt : 24 bytes:NWAL_SA_EALG_3DES_CBC and 0 bytes Salt*/
737     20, /* macKeySize: 16 bytes NWAL_SA_AALG_HMAC_SHA1 */
738     NULL, //set below
739     NULL, //set below
740 },
742     20, /* encKeySize: CTR 16 bytes Encryption Key and 4 bytes Salt */
743     32, /* macKeySize: 16 bytes NWAL_SA_AALG_HMAC_SHA2_256 */
744     NULL, //set below
745     NULL, //set below
746 },
748     24, /* encKeySize: DES-CBC: 24 bytes:NWAL_SA_EALG_3DES_CBC and 0 bytes Salt*/
749     32, /* macKeySize: 16 bytes NWAL_SA_AALG_HMAC_SHA2_256 */
750     NULL, //set below
751     NULL, //set below
752 },
754     0, /* NWAL_SA_EALG_NULL*/
755     16, /* NWAL_SA_AALG_HMAC_MD5, 16 bytes */
756     NULL, //set below
757     NULL, //set below
758 },
760     20, /* encKeySize: GCM 16 bytes Encryption Key and 4 bytes Salt */
761     0, /* macKeySize: 0*/
762     NULL, //set below
763     NULL, //set below
764 },
766     19, /* encKeySize: CTR 16 bytes Encryption Key and 3 bytes Salt t*/
767     0, /* macKeySize 0*/
768     NULL, //set below
769     NULL, //set below
770 },
772     0, /* encKeySize: CTR 16 bytes Encryption Key and 3 bytes Salt : 24 bytes:NWAL_SA_EALG_AES_CTR and 0 bytes Salt*/
773     16, /* macKeySize 0*/
774     NULL, //set below
775     NULL, //set below
777 };
780 static uint8_t ourAuthKey[36] =
781         {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
782          0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
783          0x20, 0x21, 0x22, 0x23 };
786 static uint8_t ourEncrKey[36] = 
787         {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
788          0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
789          0x30, 0x31, 0x32, 0x33 }; 
792 /*************************END NETAPI OBJECTS***********************/
794 #define START_SRC_IP 0x0a00000a
795 #define DST_IP       0xc0a80001
796 #define NEW_START_SRC_IP 0x9eda000a
797 #define DST_PORT 0x555 
798 #define START_SRC_PORT 0x1234
799 #define NEW_START_SRC_PORT 100
800 void update_header(HEAD_T * p_head, int len)
802    unsigned char *p = (unsigned char *) &p_head->udp[1];
803    len -= (20+14);
804    /* update ip checksum */
805    /* update udp checksum */
806    /* update length */
807    *p= (len&0xff00)>>8;
808    *(p+1) = len&0xff;
811 #if 0
812 void gen_pkts(int np)
814 int i;
815 int ip = START_SRC_IP &0xff;
816 int port= START_SRC_PORT;
817 //HEAD_T temp={{0x25000200,0xdead0000,0x80110000,START_SRC_IP,DST_IP},
818 //             {START_SRC_PORT<<16|DST_PORT,0x01ec<<16|0x0000}};
819 HEAD_T temp;
820 memcpy(&temp,&testPkt[0],sizeof(HEAD_T));
822 for(i=0;(i<np) && (i<NP);i++)
823   {
824        memcpy(&pkts[i],&temp,sizeof(temp));
825        update_header(&pkts[i],512);      /* update checksums etc */
826        /* change template for new pkt */
827        ip+=1;
828        if(ip>254) {(ip=START_SRC_IP&0xff); port+=1; }
829        temp.ip[3] = htonl((START_SRC_IP&0xffffff00)| ip);
830        temp.udp[0] = htonl( (temp.udp[0]&0xffff0000)| port);
831        temp.udp[1] = htonl(temp.udp[1]);
832      
833   }
834   n_pkt=np;
836 #endif
838 void build_table(Trie * p_trie)
840 int i;
841 int sport=NEW_START_SRC_PORT; 
842 HEAD_T temp,temp2;
843 KEY_T key;
845 memcpy(&temp,&testPkt[14],sizeof(temp));
847  //insert entry into trie
848 key.src_ip = temp.ip[3];
849 key.dst_ip = temp.ip[4];
850 key.src_port= (temp.udp[0]&0xffff0000)>>16;
851 key.dst_port= (temp.udp[0]&0x0000ffff);
852 trie_insert(p_trie,(char *)&key,sizeof(key), (void *) &nat[0]); //asociate with nat entry 0
854 //build nat table
855 for(i=0;i<100;i++)
857    memcpy(&temp2,&testPkt[14],sizeof(temp));
858    temp2.udp[0] = (temp2.udp[0] & 0xffff0000) |  sport;
859    memcpy(&nat[i], &temp2, sizeof(temp2));
860    sport+= 1;
864 //===========stub transmitter==================
865 void send_pkt(Ti_Pkt *pkt, int len)
867 //just free pkt.  Don't send
868 Pktlib_freePacket((Ti_Pkt*)pkt);
869         return;
872 //==========stub slow path============
873 void slow_path(Ti_Pkt *pkt, int len)
875 // debug: check descriptor for validity by verifying that desciptor link field is null as expected\n");
876          {Ti_Pkt * k= Pktlib_getNextPacket(pkt); if(k != 0) {printf(" slowpath, nexpkt != NULL");}}
877 //just free pkt
878 Pktlib_freePacket((Ti_Pkt*)pkt);
879         return;
881 /* check header */
882 struct LastPktInfo
884 int iface;
885 int ipcsum;
886 int l4csum;
887 } ;
888 static struct LastPktInfo lpInfo;
890 int check_header(HEAD_T * p_head, PKTIO_METADATA_T * p_meta)
892 #ifdef MULTI_THREAD
893     int coreid=Osal_nwalGetProcId();  //who we are(thread local)
894     //int coreid = our_core;
895 #else
896     int coreid=0;
897 #endif
898 if (NWAL_RX_FLAG1_META_DATA_VALID & p_meta->u.rx_meta->rxFlag1)
900     lpInfo.iface = ((unsigned int)p_meta->u.rx_meta->appId) &0xff; //last byte is interface num
901     lpInfo.ipcsum =(p_meta->u.rx_meta->rxFlag1 & NWAL_RX_FLAG1_IPV4_CHKSUM_VERIFY_MASK )== NWAL_RX_FLAG1_IPV4_CHKSUM_VERIFY_ACK ? 1 : 0;
902     lpInfo.l4csum = (p_meta->u.rx_meta->rxFlag1 & NWAL_RX_FLAG1_L4_CHKSUM_VERIFY_MASK )== ((NWAL_RX_FLAG1_L4_CHKSUM_VERIFY_ACK) << NWAL_RX_FLAG1_L4_CHKSUM_VERIFY_SHIFT) ? 1 : 0; 
903     if ((unsigned int)p_meta->u.rx_meta->appId & NETAPI_NETCP_MATCH_IPSEC)
904     {
905     stats[coreid].sec_rx++;
906     }
907     if ((unsigned int)p_meta->u.rx_meta->appId & NETAPI_NETCP_MATCH_IPSEC_POLICY)
908     {
909     stats[coreid].secp_rx++;
910     }
912     if ((unsigned int)p_meta->u.rx_meta->appId & NETAPI_NETCP_MATCH_CLASS)
913     {
914         int c= ((unsigned int)p_meta->u.rx_meta->appId >>8)&0xffff;
915     if (c==0)  stats[coreid].n_class0_rx +=1;
916     else if (c==1) stats[coreid].n_class1_rx +=1;
917     else if (c==2) stats[coreid].n_class2_rx +=1;
918     else printf("**NET_TEST RX -unknown class: %x\n",  p_meta->u.rx_meta->appId);
922  return 1;
925 #define PKT_LEN 1400
926 void test_alloc_free(int n)
928 int i;
929 Ti_Pkt * b;
931 for(i=0;i<n;i++)
933   b=Pktlib_allocPacket(ourHeap,PKT_LEN);
934   Pktlib_freePacket(b);
938 //measurement test points
939 unsigned int vv1;
940 unsigned int vv2;
941 unsigned int vv3;
942 unsigned int vv4;
943 unsigned int vv5;
944 unsigned int vv6;
947 unsigned int vv11;
949 extern unsigned int nwal_prof1,nwal_prof2,nwal_prof3,nwal_prof4,nwal_prof5,nwal_prof6;
951 //#define REASSEMBLE_BENCH
952 #ifdef REASSEMBLE_BENCH
953 #include <ti/drv/pa/example/reassemLib/reassemLib.h>
954 /*--------------reassembly benchmark--------------------------------*/
955 void our_reassemble_bench(int nfrags)
957 paIPReassemblyConfig_t Config={5,128,10000 };
958 int i,j;
959 int len;
960 Ti_Pkt tip;
961 char *buffer;
962 unsigned long v1;
963 unsigned long v2;
964 unsigned long sum1=0;
965 unsigned long sum2=0;
966 paEx_reassemLibInit(&Config);
967 for(j=0;j<200/nfrags;j++)
969   for(i=0;i<nfrags;i++)
970   {
971     short temp;
972     tip=Pktlib_allocPacket(ourHeap,PKT_LEN);
973     Pktlib_getDataBuffer(tip,(uint8_t**)&buffer,&len);
974     memcpy(&buffer[0],&testPkt[14],20); //IP header
975     if (i < (nfrags-1)) buffer[6] = 0x20;
976     temp = i*40; 
977     buffer[6]|= (temp&0x1f00)>>8;
978     buffer[7]= (temp&0xff);
979     temp = 20+40*8; 
980     buffer[2]= (temp&0xff00)>>8;
981     buffer[3]= (temp&0xff);
982     Pktlib_setPacketLen(tip, temp);
983     v1= hplib_mUtilGetPmuCCNT();
984     paEx_reassemLibProc(tip, 0xffff);
985     v2= hplib_mUtilGetPmuCCNT();
986     sum1+= v2-v1;
987   }
988   sum2 += v2-v1;
990 printf("reasssembly test:  %d trials, %d frags/pkt  %d cycles/frag %d cycles/last frag\n",j,nfrags, sum1/(j*nfrags), sum2/(j));
992 #endif
994 /*--------------basic pktio send/recv benchmark----------------------*/
995 unsigned int timings[10];
996 void our_pktio_bench(int ntrials)
998 int i;
999 #define NBATCH 8
1000 Ti_Pkt tip;
1001 unsigned char * pData;
1002 int len;
1003 int n;
1004 int err;
1005 int sum =0;
1007    Osal_cache_op_measure_reset();
1008    for(i=0;i<10;i++) timings[i]=0;
1009    printf("calibration loop .. ");
1010    for(i=0;i<1000;i++)
1011    {
1012    vv1= hplib_mUtilGetPmuCCNT();
1013    vv2= hplib_mUtilGetPmuCCNT();
1014    sum+=(vv2-vv1);
1015    }
1016    printf(" accuracy = +- %d cycles\n", sum/1000);
1017    sleep(1);
1018   
1019 PKTIO_METADATA_T meta[10]={0};
1020 //send single, recv single
1021 for(i=0;i<ntrials;i++)
1023    vv1= hplib_mUtilGetPmuCCNT();
1024    tip=Pktlib_allocPacket(ourHeap,PKT_LEN);
1025    vv2= hplib_mUtilGetPmuCCNT();
1026    Pktlib_getDataBuffer(tip,&pData,&len);
1027    vv3= hplib_mUtilGetPmuCCNT();
1028    pktio_send(our_chan,tip,&meta[0],&err);
1029    vv4= hplib_mUtilGetPmuCCNT();
1030    n=pktio_poll(our_chan,NULL , &err);
1031    vv5=   hplib_mUtilGetPmuCCNT();
1032    timings[0]+=(vv6-vv4);
1033    timings[1]+=(vv5-vv4);
1034    timings[3]+=(vv4-vv3); 
1035    timings[5]+=(vv3-vv1);
1036    timings[8]+=(vv11-vv6);
1038 #ifdef PKTIO_GET_BENCHMARK
1039    timings[2]+=(our_chan->vv7p-vv4);
1040    timings[4]+=(our_chan->vv8p-vv3);
1041    timings[6]+=(our_chan->vv9p-our_chan->vv8p);
1042    timings[7]+=(our_chan->vv10p-our_chan->vv7p);
1043 #endif
1046 #ifdef PKTIO_GET_BENCHMARK
1047    printf("pktio bench. rx=%d (wcb%d) (toqm%d) tx=%d (toqm%d) alloc=%d qpush=%d qpop=%d free=%d\n", timings[0]/ntrials,
1048            timings[1]/ntrials, timings[2]/ntrials,timings[3]/ntrials, timings[4]/ntrials,timings[5]/ntrials,
1049            timings[6]/ntrials,timings[7]/ntrials, timings[8]/ntrials );
1050    {
1051        unsigned long long ccycles;
1052        unsigned long long  n_c_ops;
1053        ccycles =Osal_cache_op_measure(&n_c_ops);
1054        printf("n_c_ops=%lld cache_op_time=%lld (total) = %lld (pp)\n", n_c_ops, ccycles,  n_c_ops? (ccycles/(n_c_ops)) : 0);
1055    }
1056 #endif
1059 /*-----------test driver: gen an input pkt------- */
1060 //char buffer[sizeof(HEAD_T)+PKT_LEN];
1061 Ti_Pkt * get_pkt(int n, unsigned int *p_len, Pktlib_HeapHandle heap2use, int size, unsigned char * buf2cpy, int copy_size)
1063    int ind;
1064    long long temp;
1065    Ti_Pkt * b;
1066    char * buffer;
1067    unsigned int len;
1069   if (pktloopback==0)
1070   {
1071         if (n>=TX_BURST) return NULL;   //just gen pkts to warm swtich, so that it knows
1072                                 //our mac is valid
1073   }  
1074   b=Pktlib_allocPacket(heap2use,size);
1075   if (!b) 
1076     {printf("net_test: get_pkt() heap empty!! %d pkts gen'd %d \n", n); return NULL;};
1078     //debug - way to validate descriptor
1079     {Ti_Pkt* k= Pktlib_getNextPacket(b); 
1080          if(k != 0) {printf(" genpkt, nexpkt != NULL");}}
1083    //get pointer to buffer area of packet
1084    Pktlib_getDataBuffer(b,(uint8_t**)&buffer,&len);
1085    if (!buffer) 
1086     {printf("net_test: get_pkt() heap returned empty buffer %d \n", n); return NULL;};
1088 #if 0 
1089 if (pktloopback==0)
1091    temp = (long long) rand();
1092    temp *= PKT_LEN;
1093    temp /= RAND_MAX;
1094    temp +=2;
1095    *p_len = (int) temp; 
1096    *p_len = *p_len &0xfffffffe;
1097    temp = (long long) rand();
1098    temp *= n_pkt;
1099    temp /= RAND_MAX;
1100    ind = (int) temp;
1101    update_header(&pkts[ind],*p_len);
1102    //printf("get pkt:%d %d ind=%d len=%d\n",RAND_MAX, rand(),ind, *p_len);
1103     memcpy(&buffer[0], &mac_header[0],14);
1104     memcpy(&buffer[14],(char*)&pkts[ind],sizeof(HEAD_T)); 
1106 else
1107 #endif
1109    //copy test packet into buffer
1111     memcpy(&buffer[0], buf2cpy, copy_size);
1112     *p_len = copy_size;
1114     return b; 
1117 static int eof=0;
1118 /*--------------------------------------------------------------
1119  *----------utility to flip a packet and send 
1120  *--------------------back to source----------------------------
1121  *                   flag=1 => ipsec
1122  *--------------------------------------------------------------*/
1123 void flip_and_send_pkt(Ti_Pkt *tip,  unsigned char * p_pkt, int len, int flag, uint16_t enet_port)
1125     unsigned char mac_temp[6];
1126     unsigned char ip_temp[4];
1127     unsigned char new_dest_port[2]={0x75,0x30};  // 30000
1128     uint16_t blah; 
1129     uint16_t i=1;   /* for testing only */
1131     uint8_t *p_spi; 
1132     netTestSA_t * p_sa_info;
1133     uint8_t p_iv[16];
1134     uint8_t p_add[8];
1135     Cppi_HostDesc*          pPloadDesc;
1136     uint8_t ah_len;
1137 #ifdef AH_SUPPORT
1138     HEAD_T * p_head;
1139     HEAD_T temp_head;
1140     int pkt_type;
1141 #endif
1143 #ifdef MULTI_THREAD
1144     int coreid=Osal_nwalGetProcId(); //who we are(thread local)
1145         //int coreid = our_core;
1146 #else
1147 int coreid=0;
1148 #endif
1149               //netapi_dump_buf((long*)p_pkt,len);
1150     Pktlib_setPacketLen(tip,len);
1151     //flip the mac address
1152     memcpy(&mac_temp,&p_pkt[0],6);
1153     memcpy(&p_pkt[0],&p_pkt[6],6);
1154     memcpy(&p_pkt[6],&mac_temp,6);
1155     //memcpy(&p_pkt[0],real_mac_header,6); //for testing to wireshark pc
1157     //flip the ip  (outer in case of ipsec)
1158     memcpy(&ip_temp, &p_pkt[14+12],4);
1159     memcpy(&p_pkt[14+12],&p_pkt[14+12+4],4);
1160     memcpy(&p_pkt[14+12+4],&ip_temp,4);
1162 #ifdef AH_SUPPORT
1163     p_head=&temp_head;
1164 #endif
1165     //outer checksum to 0
1166     if (!flag) 
1167     {
1168         memset(&p_pkt[14+10],0,2);
1169     }
1171     //inner ip &udp for ipsec
1172     if (flag) 
1173     {
1174 #ifdef AH_SUPPORT
1175         memcpy(p_head,&p_pkt[14],sizeof(HEAD_T));
1177         if ((p_head->ip[2]&0x0000ff00)==0x00003300)
1178         {
1179             p_spi = &(p_pkt[netTest_MAC_HEADER_LEN+netTest_IP_HEADER_LEN + 4]);
1180             pkt_type = IPSEC_AH_PKT;
1181             printf("flip_and_send_pkt: IPSEC AH packet received\n");
1182         }
1183         else if ((p_head->ip[2]&0x0000ff00)==0x00003200)
1184         {
1185             p_spi = &(p_pkt[netTest_MAC_HEADER_LEN+netTest_IP_HEADER_LEN]);
1186             pkt_type = IPSEC_ESP_PKT;
1188             printf("flip_and_send_pkt: IPSEC ESP packet received\n");
1190         }
1191         else
1192         {
1193             printf("flip_and_send_pkt: un-supported IPSEC protocol\n");
1194             Pktlib_freePacket(tip);
1195             return;
1196         }
1197 #endif
1199         //p_spi = &(p_pkt[netTest_MAC_HEADER_LEN+netTest_IP_HEADER_LEN + 4]);
1200         p_sa_info = (netTestSA_t *) trie_lookup(p_trie_sa, (char *)p_spi ,4);
1201         if (p_sa_info == NULL)
1202         {
1203             printf("flip_and_send_pkt(): trie_lookup() failed\n");
1204             Pktlib_freePacket(tip);
1205             return;
1206         }
1207         //just drop non-udp packet
1208         if (p_pkt[p_sa_info->tx_payload_info.encOffset+9]!=0x11)
1209         {
1210             stats[coreid].n_new+=1;Pktlib_freePacket(tip); return;
1211         }
1213         /* flip inner IP */
1214         printf("flip_and_send_pkt: encOffSet %d\n", p_sa_info->tx_payload_info.encOffset);
1215         memcpy(&ip_temp, &p_pkt[p_sa_info->tx_payload_info.encOffset+12],4);
1216         memcpy(&p_pkt[p_sa_info->tx_payload_info.encOffset+12],&p_pkt[p_sa_info->tx_payload_info.encOffset+12+4],4);
1217         memcpy(&p_pkt[p_sa_info->tx_payload_info.encOffset+12+4],&ip_temp,4);
1218         /* setting udp ports */
1219         memcpy(&p_pkt[p_sa_info->tx_payload_info.encOffset+20+2],&new_dest_port[0],2);
1220         memset(&p_pkt[p_sa_info->tx_payload_info.encOffset+20+6],0,2); //checksum
1222         if (config.ipsec_mode_tx == IPSEC_MODE_TX_SIDEBAND)
1223         {
1224             /* inner ip checksum : leave alone */
1225             /* outer ip, set to 0 (we will compute on way out */
1226             memset(&p_pkt[14+10],0,2);
1227         }
1228         else
1229         {
1230             //#else //inflow, don't touch outer , clear inner 
1231             //DALmemset(&p_pkt[14+20+8+16+10],0,2); //inner checksum, we will compute on way out
1232             //outer ip checksum : leave alone
1233         }
1234     }
1235     else
1236     {
1237         memset(&p_pkt[14+20+6],0,2);//0 udp checksum (we will compute on way out
1238         memcpy(&p_pkt[14+20+2],&new_dest_port[0],2);
1239     }
1241     /*IPSEC case */ 
1242     if (flag)
1243     {
1244         if (config.ipsec_mode_tx == IPSEC_MODE_TX_SIDEBAND)
1245         //send to crypto for encryption
1246         //12 byte auth tag
1247         {
1248             PKTIO_METADATA_T meta = {PKTIO_META_SB_TX,{0},0};
1249             int err;
1250             nwalDmTxPayloadInfo_t meta_tx={0};
1251             meta.sa_handle=p_sa_info->tx_data_mode_handle;  //use TX SA context
1253             memcpy(&meta_tx, &(p_sa_info->tx_payload_info), sizeof(nwalDmTxPayloadInfo_t));
1255  
1256                 meta_tx.encSize = len - p_sa_info->tx_payload_info.encOffset -p_sa_info->auth_tag_size;
1257                 meta_tx.authSize = len - meta_tx.authOffset - p_sa_info->auth_tag_size;
1259 #if 0
1260             printf("flip_and_send_pkt(): encOffset %d\n", meta_tx.encOffset);
1261             printf("flip_and_send_pkt():authOffset %d\n", meta_tx.authOffset);
1262             printf("flip_and_send_pkt(): encSize %d\n", meta_tx.encSize);
1263             printf("flip_and_send_pkt(): authSize %d\n", meta_tx.authSize);
1264 #endif
1266             meta_tx.pAuthIV=NULL;
1267             meta_tx.aadSize=0;
1268             meta_tx.pAad=NULL;
1269             if (p_sa_info->cipherMode ==  NWAL_SA_EALG_AES_CTR)
1270             {
1271                 memcpy(&p_iv[0], &ourEncrKey[16], 4);
1272                 memcpy(&p_iv[4], &p_pkt[netTest_MAC_HEADER_LEN +netTest_IP_HEADER_LEN + netTest_ESP_HEADER_LEN], 8);
1273                 p_iv[12] = 0;
1274                 p_iv[13] = 0;
1275                 p_iv[14] = 0;
1276                 p_iv[15] = 1;
1277                 meta_tx.pEncIV = &p_iv[0];
1278             }
1279             else if ((p_sa_info->cipherMode == NWAL_SA_EALG_AES_GCM) ||
1280                 (p_sa_info->cipherMode == NWAL_SA_EALG_AES_CCM))
1281                 {
1282                         memcpy(&p_iv[0], &p_pkt[netTest_MAC_HEADER_LEN +netTest_IP_HEADER_LEN + netTest_ESP_HEADER_LEN], 8);
1283                         meta_tx.pEncIV = &p_iv[0];
1284                         memcpy(&p_add[0], &p_pkt[netTest_MAC_HEADER_LEN +netTest_IP_HEADER_LEN], 8);
1285                         meta_tx.pAad= &p_add[0];
1286                         meta_tx.aadSize = 8;
1287                 }
1288             else if (p_sa_info->cipherMode ==  NWAL_SA_EALG_NULL)
1289             {
1290                 meta_tx.pEncIV = NULL;
1291             }
1292             else
1293             {
1294                 meta_tx.pEncIV = &p_pkt[netTest_MAC_HEADER_LEN +netTest_IP_HEADER_LEN + netTest_ESP_HEADER_LEN ];
1295             }
1296             //meta_tx.authIvSize=0;
1298             /* post it to netcp sb tx channel*/
1299             meta_tx.appCtxId = (nwal_AppId)hplib_mUtilGetPmuCCNT();
1300             meta.u.tx_sb_meta=&meta_tx;
1302 #ifdef NET_TEST_ENABLE_SIDE_BAND_LOW_LEVEL_API
1303             /* Use the command label which was cached during create time
1304             * Update the Queue to receive output packet from SA to the local 
1305             * core Queue. Below information can be saved by application in 
1306             * per process context to avoid API overhead per packet
1307             * Application can use below rxSbSaQ for polling packets back from
1308             * SA
1309             */
1310             nwalRetVal =  
1311             nwal_getLocCxtInfo(PKTIO_GET_NWAL_INSTANCE(netcp_sb_tx_chan),
1312                                                       &nwalLocCxt);
1313             if(nwalRetVal == nwal_OK)
1314             {
1315                 p_sa_info->tx_dmPSCmdInfo.rxSbSaQ = nwalLocCxt.rxSbSaQ;
1316             }
1317             nwal_mCmdDMUpdate(tip,
1318                               &p_sa_info->tx_dmPSCmdInfo,
1319                               meta_tx.appCtxId,
1320                               meta_tx.encOffset,
1321                               meta_tx.encSize,
1322                               meta_tx.pEncIV,
1323                               meta_tx.authOffset,
1324                               meta_tx.authSize,
1325                               meta_tx.pAuthIV,
1326                               meta_tx.aadSize,
1327                               meta_tx.pAad);
1328             pPloadDesc = Pktlib_getDescFromPacket(tip);
1329             pPloadDesc = Qmss_osalConvertDescVirtToPhy(pPloadDesc);
1330             Qmss_queuePushDescSizeRaw(p_sa_info->tx_dmPSCmdInfo.txQueue,
1331                                        pPloadDesc,
1332                                        NWAL_DESC_SIZE);
1334 #else
1335            pktio_send(netcp_sb_tx_chan,tip,&meta,&err);
1336 #endif
1337         }
1338         else
1339         {
1340             //INFLOW TX *******************
1341             //send pkt directly, asking for IP and UDP checksum offloads AND IPSEC to be applied
1342             PKTIO_METADATA_T meta = {PKTIO_META_TX,{0},0};
1343             int err;
1344             nwalTxPktInfo_t meta_tx={0};
1345 #define USE_COPY
1346 #ifdef USE_COPY
1347 //debug:  see if re-using RX descriptor for TX is causing our SA lockup
1348             {
1349             int new_len=0;
1350             Ti_Pkt new_tip =  get_pkt(0, &new_len, specialLarge , len+10 , &p_pkt[0]   , len);
1351             if (!new_tip)
1352             {
1353                 printf("net_test> new_tip NULL\n");
1354             }
1355             else
1356             {
1357                 Pktlib_setPacketLen(new_tip,new_len);
1358                 Pktlib_freePacket(tip);
1359                 tip=new_tip;  
1360                 Pktlib_getDataBuffer(tip,(uint8_t**)&p_pkt,&new_len);  //reset p_pkt to point to new buffer as its used below
1361 //                Cppi_setTimeStamp (Cppi_DescType_HOST, (Cppi_Desc *) tip,stats[coreid].sec_tx); 
1362             }
1363             }
1364             if (len <1500)  
1365             {
1366                 eof+=1;
1367             }
1368 #endif
1370             meta.sa_handle=p_sa_info->tx_inflow_mode_handle; //this tells netapi that inflow crypto needs to be applied
1371             meta_tx.startOffset = p_sa_info->tx_pkt_info.startOffset;
1372             meta_tx.ipOffBytes =p_sa_info->tx_payload_info.encOffset; 
1373             meta_tx.l4OffBytes = p_sa_info->tx_pkt_info.l4OffBytes;
1374             meta_tx.l4HdrLen = p_sa_info->tx_pkt_info.l4HdrLen;
1375             meta_tx.ploadLen = (unsigned) ((p_pkt[meta_tx.l4OffBytes+4]<<8)|p_pkt[meta_tx.l4OffBytes+4+1]) -8 ;
1376             meta_tx.saOffBytes=  p_sa_info->tx_pkt_info.saOffBytes;
1377             if (pkt_type == IPSEC_AH_PKT)
1378             {
1379                 memset(&p_pkt[netTest_MAC_HEADER_LEN+netTest_IP_HEADER_LEN+netTest_IPSEC_AH_FIXED_HDR_SIZE],0,netTest_IPSEC_AH_FIXED_HDR_SIZE);
1380                 meta_tx.txFlag1 = p_sa_info->tx_pkt_info.txFlag1;
1381                 meta_tx.saPayloadLen=len-netTest_MAC_HEADER_LEN;   //don't inlcude mac 
1382                 meta_tx.saAhMacSize = 12;
1383                 meta_tx.saAhIcvOffBytes = netTest_MAC_HEADER_LEN + netTest_IP_HEADER_LEN + netTest_IPSEC_AH_FIXED_HDR_SIZE;
1384                 printf("flip_and_send_pkt: enet_port %d\n", enet_port);
1385                 meta_tx.enetPort = enet_port;
1386             }
1387             else if (pkt_type == IPSEC_ESP_PKT)
1388             {
1389                 meta_tx.txFlag1 = p_sa_info->tx_pkt_info.txFlag1;
1390                 meta_tx.saPayloadLen=len-netTest_MAC_HEADER_LEN-netTest_IP_HEADER_LEN;   //don't include mac and ip outer header
1391                 meta_tx.enetPort = 0;
1392             }
1393             else
1394             {
1395                 printf("flip_and_send_pkt: invalid ESP protocol\n");
1396                 return;
1397             }
1399             meta_tx.pseudoHdrChecksum =
1400             test_utilGetIpv4PsudoChkSum(&p_pkt[meta_tx.ipOffBytes],8+ meta_tx.ploadLen);
1403             /* post it to netcp tx channel*/
1404             meta.u.tx_meta=&meta_tx;
1405             if (stats[coreid].sec_tx<20) dump_descr((long *) tip, stats[coreid].sec_tx);
1406 #ifdef NET_TEST_ENABLE_INFLOW_LOW_LEVEL_API
1407 #if 0
1408         nwal_mCmdSetCrypPort (tip,
1409                                           &p_sa_info->tx_psCmdInfo,
1410                                          p_sa_info->tx_pkt_info.saOffBytes,
1411                                          meta_tx.saPayloadLen,
1412                 p_sa_info->swInfo0, 
1413                 p_sa_info->swInfo1, 
1414                 NWAL_ENET_PORT_UNKNOWN);
1415 #endif
1417             if ( pkt_type == IPSEC_ESP_PKT)
1418             {
1419                 nwal_mCmdSetL4CkSumCrypPort(tip,
1420                                         &p_sa_info->tx_psCmdInfo,
1421                                         meta_tx.l4OffBytes, 
1422                                         meta_tx.ploadLen + meta_tx.l4HdrLen, 
1423                                         meta_tx.pseudoHdrChecksum, 
1424                                         p_sa_info->tx_pkt_info.saOffBytes, 
1425                                         meta_tx.saPayloadLen, 
1426                                         p_sa_info->swInfo0, 
1427                                         p_sa_info->swInfo1, 
1428                                         0);
1429             }
1430             else
1431             {
1432                 nwal_mCmdSetL4CkSumAHCrypPort(tip,
1433                                         &p_sa_info->tx_psCmdInfo,
1434                                         meta_tx.l4OffBytes, 
1435                                         meta_tx.ploadLen + meta_tx.l4HdrLen, 
1436                                         meta_tx.pseudoHdrChecksum, 
1437                                         meta_tx.saOffBytes,
1438                                         meta_tx.saPayloadLen,
1439                                         p_sa_info->swInfo0, 
1440                                         p_sa_info->swInfo1, 
1441                                         meta_tx.saAhIcvOffBytes,
1442                                         meta_tx.saAhMacSize,
1443                                         meta_tx.enetPort);
1444             }
1445             pPloadDesc = Pktlib_getDescFromPacket(tip);
1446             pPloadDesc = Qmss_osalConvertDescVirtToPhy(pPloadDesc);
1447             Qmss_queuePushDescSizeRaw(p_sa_info->tx_psCmdInfo.txQueue,
1448                                          pPloadDesc,
1449                                         NWAL_DESC_SIZE);
1450 #else
1451                pktio_send(netcp_tx_chan,tip,&meta,&err);
1452 #endif
1453             stats[coreid].tx +=1;
1454             stats[coreid].sec_tx +=1;
1455         }
1456     }
1457     else  //non ipsec send pkt directly, asking for IP and UDP checksum ofload
1458     {
1459         PKTIO_METADATA_T meta2 = {PKTIO_META_TX,{0},0};
1460         int err;
1461         nwalTxPktInfo_t meta_tx2={0};
1462         meta2.sa_handle=nwal_HANDLE_INVALID;
1463         meta_tx2.txFlag1 = (NWAL_TX_FLAG1_DO_IPV4_CHKSUM|NWAL_TX_FLAG1_DO_UDP_CHKSUM| NWAL_TX_FLAG1_META_DATA_VALID );
1464         meta_tx2.startOffset = 0;
1465         meta_tx2.ipOffBytes = 14;
1466         meta_tx2.l4OffBytes = 14+20;
1467         meta_tx2.l4HdrLen = 8;
1468         meta_tx2.ploadLen = (unsigned) ((p_pkt[14+20+4]<<8)|p_pkt[14+20+4+1]) -8 ;
1469         meta_tx2.pseudoHdrChecksum =
1470         test_utilGetIpv4PsudoChkSum(&p_pkt[14],8+ meta_tx2.ploadLen);
1472         /* post it to netcp tx channel*/
1473         meta2.u.tx_meta=&meta_tx2;
1474         pktio_send(netcp_tx_chan,tip,&meta2,&err);
1475         stats[coreid].tx +=1;
1476     }
1482 /***************************************
1483  benchmark receive handler
1484 ****************************************/
1485 void recv_cb_bench(struct PKTIO_HANDLE_Tag * channel, Ti_Pkt* p_recv[],
1486                          PKTIO_METADATA_T meta[], int n_pkts,
1487                          uint64_t ts )
1489    int i;
1490    vv6=   hplib_mUtilGetPmuCCNT();
1491    for (i=0;i<n_pkts; i++) 
1492    {
1493      Pktlib_freePacket(p_recv[i]);
1494    }
1495    vv11 = hplib_mUtilGetPmuCCNT();
1498 /****************************************************************************************/
1499 /******************SB Accelerator Callback PKT RECEIVE HANDLER *************************/
1500 /******************  Handles Decrypt and Encrypt operation callbacks ******************/
1501 /******************************************************************************************/
1502 void recv_sb_cb(struct PKTIO_HANDLE_Tag * channel, Ti_Pkt* p_recv[],
1503                          PKTIO_METADATA_T meta[], int n_pkts,
1504                          uint64_t ts )
1506 int i;
1507 int len;
1508 int p;
1509 HEAD_T * p_res;
1510 Ti_Pkt * tip;
1511 unsigned int templen;
1512 int err;
1513 KEY_T key;
1514 char * p_pkt;
1515 HEAD_T * p_head;
1516 HEAD_T temp_head;
1517 int tag_cmp=0;
1518 unsigned int hash[4];
1519 uint8_t *p_spi;
1520 netTestSA_t *p_sa_info;
1522 #ifdef MULTI_THREAD
1523     int coreid=Osal_nwalGetProcId();  //who we are(thread local)
1524     //int coreid = our_core;
1525 #else
1526 int coreid=0;
1527 #endif
1528 //nwal_AppId time;
1529 unsigned long time, delta_time;
1530  /* loop over received pkts */
1531    for(i=0;i<n_pkts;i++)
1532    {
1533         tip = p_recv[i];
1534         Pktlib_getDataBuffer(tip,(uint8_t**)&p_pkt,&templen);//ignore templen
1535         len = Pktlib_getPacketLen(tip);//real length
1538          p_spi = &(p_pkt[netTest_MAC_HEADER_LEN+netTest_IP_HEADER_LEN]);
1540         p_sa_info = (netTestSA_t *) trie_lookup(p_trie_sa, (char *)p_spi ,4);
1541         if (p_sa_info == NULL)
1542         {
1543             printf("recv_sb_cb(): trie_lookup failed\n");
1544             continue;
1545         }
1548         //is this a decrypt (rx_tunnel) complete
1549         if ((int)meta[i].u.rx_sb_meta->appId == p_sa_info->rx_tunnel)
1550         {
1551            
1552             time = hplib_mUtilGetPmuCCNT();
1553            delta_time = time -(unsigned long) meta[i].u.rx_sb_meta->appCtxId;
1554             stats[coreid].total_decrypt_time += delta_time;
1555             stats[coreid].sb_rx+=1;
1556            //copy hash out of meta data (for some reason it needs endian conversion)
1557            hash[0]= htonl( meta[i].u.rx_sb_meta->pAuthTag[0]);
1558            hash[1]= htonl( meta[i].u.rx_sb_meta->pAuthTag[1]);
1559            hash[2]= htonl( meta[i].u.rx_sb_meta->pAuthTag[2]);
1560            hash[3]= htonl( meta[i].u.rx_sb_meta->pAuthTag[3]);
1561            if(stats[coreid].sb_rx<=16)
1562            {
1563              char *tp = (char *) &hash[0];
1564              //dump_header((long*)p_pkt, stats[coreid].sb_rx, (int)meta[i].u.rx_sb_meta->appId,0);
1565 #if 0
1566              printf("decrypt complete: tag in pkt= %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x \n",
1567                       p_pkt[len-16],p_pkt[len-15],p_pkt[len-14],p_pkt[len-13],
1568                       p_pkt[len-12],p_pkt[len-11],p_pkt[len-10],p_pkt[len-9], p_pkt[len-8],
1569                       p_pkt[len-7],p_pkt[len-6],
1570                       p_pkt[len-5],p_pkt[len-4],p_pkt[len-3],p_pkt[len-2],p_pkt[len-1]);
1571              printf("decrypt complete: tag from SA=%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x \n",
1572                        tp[0],tp[1],tp[2],tp[3],tp[4],tp[5],
1573                        tp[6],tp[7],tp[8],tp[9],tp[10],tp[11],tp[12],tp[13],tp[14],tp[15]);
1575             // netapi_dump_buf((long*)p_pkt,len);
1576 #endif
1577            }
1578            //check tag 
1579            //printf("recv_sb_cb(); auth tag size %d\n", p_sa_info->auth_tag_size);
1580            tag_cmp = memcmp(&p_pkt[len-p_sa_info->auth_tag_size],(char*) &hash[0],p_sa_info->auth_tag_size); //todo, really use meta->authTagLen
1581            stats[coreid].n_auth_ok += !(tag_cmp);
1582            
1583             flip_and_send_pkt(tip, p_pkt, len,1, 0);  //flip packet to echo back and send
1584         }
1585         //this is an encrypt (tx tunnel) complete
1586         else if((int)meta[i].u.rx_sb_meta->appId== p_sa_info->tx_tunnel )
1587         {
1588            hash[0]= htonl( meta[i].u.rx_sb_meta->pAuthTag[0]);
1589            hash[1]= htonl( meta[i].u.rx_sb_meta->pAuthTag[1]);
1590            hash[2]= htonl( meta[i].u.rx_sb_meta->pAuthTag[2]);
1591            hash[3]= htonl( meta[i].u.rx_sb_meta->pAuthTag[3]);
1592            stats[coreid].sb_tx+=1;
1593            if(stats[coreid].sb_tx<=16)
1594            {
1595              char *tp1 = (char *) &hash[0];
1596              //dump_header((long*)p_pkt, stats[coreid].sb_tx, (int)meta[i].u.rx_sb_meta->appId,0);
1597 #if 0
1598             printf("encrypt complete: tag in pkt=  %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x \n",
1599                       p_pkt[len-16],p_pkt[len-15],p_pkt[len-14],p_pkt[len-13],
1600                       p_pkt[len-12],p_pkt[len-11],p_pkt[len-10],p_pkt[len-9], p_pkt[len-8],
1601                       p_pkt[len-7],p_pkt[len-6],
1602                       p_pkt[len-5],p_pkt[len-4],p_pkt[len-3],p_pkt[len-2],p_pkt[len-1]);
1603              printf("encrypt complete: tag from SA=%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x \n",
1604                        tp1[0],tp1[1],tp1[2],tp1[3],tp1[4],tp1[5],
1605                        tp1[6],tp1[7],tp1[8],tp1[9],tp1[10],tp1[11],tp1[12],tp1[13],tp1[14],tp1[15]);
1606              //netapi_dump_buf((long*)p_pkt,len);
1607 #endif
1608            }
1609            //put the computed tag in the packet
1610            memcpy(&p_pkt[len-p_sa_info->auth_tag_size],(char*)&hash[0],p_sa_info->auth_tag_size); //todo, really use meta->authTagLen
1611 //          printf("recv_sb_cb(): dumping pkt after updating computed tag, len %d, auth tag size %d\n", len, p_sa_info->auth_tag_size);
1612            //netapi_dump_buf(p_pkt, len);
1613            {
1614            PKTIO_METADATA_T meta2 = {PKTIO_META_TX,{0},0};
1615            nwalTxPktInfo_t meta_tx={0};
1616            // now send directly 
1617            meta2.sa_handle=nwal_HANDLE_INVALID;
1618            meta_tx.txFlag1 = (NWAL_TX_FLAG1_DO_IPV4_CHKSUM| NWAL_TX_FLAG1_META_DATA_VALID);//only outer IP header checksum. no udp checksum possible since pkt is already encrypted
1619            meta_tx.startOffset = 0;
1620            meta_tx.ipOffBytes = netTest_MAC_HEADER_LEN;
1621            //not used
1622            meta_tx.l4OffBytes = 0;
1623            meta_tx.l4HdrLen = 0;
1624            meta_tx.ploadLen = 0;
1625             time = hplib_mUtilGetPmuCCNT();
1626             delta_time = time -(unsigned long) meta[i].u.rx_sb_meta->appCtxId;
1627            stats[coreid].total_encrypt_time += delta_time;
1628         
1630            /* post it to netcp tx channel*/
1631            meta2.u.tx_meta=&meta_tx;
1632 //           printf("recv_sb_cb(): calling pktio send to send it to network\n");
1633            pktio_send(netcp_tx_chan,tip,&meta2,&err);
1634            hplib_cacheWbInv(p_pkt,len);
1635            stats[coreid].tx +=1;
1636            }
1637         }
1638         else printf("netapi recv_sb_cb: unknown appiD %x \n",meta[i].u.rx_sb_meta->appId );
1639     }
1642 /******************************************************/
1643 /******************PKT RECEIVE HANDLER *************************/
1644 /******************************************************/
1645 void recv_cb(struct PKTIO_HANDLE_Tag * channel, Ti_Pkt* p_recv[],
1646                          PKTIO_METADATA_T meta[], int n_pkts,
1647                          uint64_t ts )
1649     int i;
1650     int len;
1651     int p;
1652     HEAD_T * p_res;
1653     Ti_Pkt * tip;
1654     unsigned int templen;
1655     int err = 0;
1656     KEY_T key;
1657     char * p_pkt;
1658     HEAD_T * p_head;
1659     HEAD_T temp_head;
1660     netTestSA_t *p_sa_info;
1661     uint8_t *p_spi;
1662     uint8_t p_iv[16];
1663     uint8_t p_add[8];
1664     uint8_t p_add1[1500];
1665     int16_t       retVal;
1667      nwalGlobCxtInfo_t   nwalGlobCxt;
1668     nwalLocCxtInfo_t    nwalLocCxt;
1670     Cppi_HostDesc*          pPloadDesc;
1671     int ifno;
1672     uint16_t enet_port = 0;
1674 #ifdef MULTI_THREAD
1675     int coreid=Osal_nwalGetProcId();  //who we are(thread local)
1676     //int coreid = our_core;
1677 #else
1678     int coreid=0;
1679 #endif
1680     p_head=&temp_head;
1683     printf("recv_cb: entered with n_pkts %d\n", n_pkts);
1684     /* loop over received pkts */
1685     for(i=0;i<n_pkts;i++)
1686     {
1687         ifno = ((unsigned int)meta[i].u.rx_meta->appId)&0xff;
1688         
1689         enet_port = meta[i].u.rx_meta->enetPort;
1690         tip = p_recv[i];
1692         Pktlib_getDataBuffer(tip,(uint8_t**)&p_pkt,&templen);/*ignore templen */
1693         len = Pktlib_getPacketLen(tip)-4;   /*real length, subtract mac trailer */
1694         Cppi_setData (Cppi_DescType_HOST, (Cppi_Desc *) tip, p_pkt,len);
1695         Pktlib_setPacketLen(tip,len);
1696         //printf("recv_cb() packet len %d\n", len);
1697         printf("recv_cb:flags: 0x%x, appid 0x%x\n", (unsigned int)meta[i].u.rx_meta->rxFlag1, (unsigned int)meta[i].u.rx_meta->appId);
1698         if(((unsigned int)meta[i].u.rx_meta->appId) == expPkt_appid)
1699         {
1700             printf("recv_cb: received exception packet\n");
1701               Pktlib_freePacket(tip);
1702             if(((unsigned int)meta[i].u.rx_meta->rxFlag1  & NWAL_RX_IP_FRAGMENT_PKT) ==
1703                                              NWAL_RX_IP_FRAGMENT_PKT)
1704             {
1705                 printf("recv_cb: received fragmented packet\n");
1706                 stats[coreid].exceptionPktsFrag+=1;
1707                 }
1708             else
1709             {
1710                 printf("recv_cb: received exception packet but not fragmented packet\n");
1711                 stats[coreid].exceptionPktsOther+=1;
1712                 }
1713             continue;
1714         }
1715         //debug: validate descriptor */
1716         if(Pktlib_getNextPacket(tip) != 0) 
1717         {
1718             printf(" rcv_cb, nexpkt != NULL");
1719         }
1721         if(coreid<TUNE_NETAPI_NUM_CORES)
1722             stats[coreid].rx+=1;
1723         if (ifno < TUNE_NETAPI_MAX_NUM_MAC)
1724             stats[coreid].if_rx[ifno]+=1;
1727 #ifdef DEBUG_DESC
1728         if (stats[coreid].rx<16)
1729         {
1730             printf(">rx dmp.."); 
1731             dump_descr((long *) tip, stats[coreid].rx);
1732         }
1733         else if (stats[coreid].rx>99) 
1734         {
1735             printf(">rx dmp.."); 
1736             dump_descr((long *) tip,stats[coreid].rx);
1737         }
1738 #endif
1739 #if 0
1740         if(stats[coreid].rx<=16)
1741         {
1742             dump_header((long*)p_pkt,stats[coreid].rx, (int)meta[i].u.rx_meta->appId,meta[i].u.rx_meta->rxFlag1);
1743              netapi_dump_buf((long*)p_pkt,len);
1744         }
1745 #endif
1746         /* check header */
1747         memcpy(p_head,&p_pkt[14],sizeof(HEAD_T));
1749         /* check for IPSEC ESP or AH packet, 0x32 is ESP tunnel mode, 0x33 is AH tunnel mode*/
1750        if (((p_head->ip[2]&0x0000ff00)==0x00003200) || ((p_head->ip[2]&0x0000ff00)==0x00003300))
1751         {
1752             if (!check_header(p_head,&meta[i]))
1753             {
1754                 stats[coreid].n_bad+=1;Pktlib_freePacket(tip); 
1755                 continue;
1756             }
1758             //process IP SEC PACKET
1759             if (config.ipsec_mode_rx == IPSEC_MODE_RX_SIDEBAND)
1760             {
1761                 
1762                 p_spi = &(p_pkt[netTest_MAC_HEADER_LEN+netTest_IP_HEADER_LEN]);
1763                  p_sa_info = (netTestSA_t *) trie_lookup(p_trie_sa, (char *)p_spi ,4);
1764                 if (p_sa_info == NULL)
1765                 {
1766                     printf("recv_cb(): trie_lookup() failed\n");
1767                     continue;
1768                 }
1770                 //ship to crypto for decrypt!!
1771                 //12 byte auth tag
1772                 PKTIO_METADATA_T meta2 = {PKTIO_META_SB_TX,{0},0};
1773                 nwalDmTxPayloadInfo_t meta_tx={0};
1774                 meta2.sa_handle=p_sa_info->rx_data_mode_handle;
1776                 memcpy(&meta_tx, &(p_sa_info->tx_payload_info), sizeof(nwalDmTxPayloadInfo_t));
1778                 //meta_tx.ploadLen = len;
1782                     meta_tx.encSize = len - p_sa_info->tx_payload_info.encOffset -p_sa_info->auth_tag_size;
1783                     meta_tx.authSize = len - meta_tx.authOffset - p_sa_info->auth_tag_size;
1787 #if 0
1788                 printf("recv_cb(): packet length %d\n", len);
1789                 printf("recv_cb(): encOffset %d\n", meta_tx.encOffset);
1790                 printf("recv_cb():authOffset %d\n", meta_tx.authOffset);
1791                 printf("recv_cb(): encSize %d\n", meta_tx.encSize);
1792                 printf("recv_cb(): authSize %d\n", meta_tx.authSize);
1793 #endif
1795                 if (p_sa_info->cipherMode == NWAL_SA_EALG_AES_CTR)
1796                 {
1797                     memcpy(&p_iv[0], &ourEncrKey[16], 4);
1798                     memcpy(&p_iv[4], &p_pkt[netTest_MAC_HEADER_LEN +netTest_IP_HEADER_LEN + netTest_ESP_HEADER_LEN], 8);
1799                     p_iv[12] = 0;
1800                     p_iv[13] = 0;
1801                     p_iv[14] = 0;
1802                     p_iv[15] = 1;
1803                     meta_tx.pEncIV = &p_iv[0];
1804                  
1805                 }
1806                 else if ((p_sa_info->cipherMode == NWAL_SA_EALG_AES_GCM)  ||
1807                             (p_sa_info->cipherMode == NWAL_SA_EALG_AES_CCM))
1808                 {
1809                         memcpy(&p_iv[0], &p_pkt[netTest_MAC_HEADER_LEN +netTest_IP_HEADER_LEN + netTest_ESP_HEADER_LEN], 8);
1810                         meta_tx.pEncIV = &p_iv[0];
1811                         /* aad is the ESP header which is 8 bytes */
1812                         memcpy(&p_add[0], &p_pkt[netTest_MAC_HEADER_LEN +netTest_IP_HEADER_LEN], 8);
1813                         meta_tx.pAad= &p_add[0];
1814                         meta_tx.aadSize = 8;
1815                 }
1816                 else if (p_sa_info->cipherMode ==  NWAL_SA_EALG_NULL)
1817                 {
1818                     meta_tx.pEncIV = NULL;
1819                 }
1820                 else
1821                 {
1822                     meta_tx.pEncIV = &p_pkt[netTest_MAC_HEADER_LEN +netTest_IP_HEADER_LEN + netTest_ESP_HEADER_LEN ];
1823                 }
1824                
1825                 meta_tx.appCtxId = (nwal_AppId)hplib_mUtilGetPmuCCNT();
1826                 //printf("recv_cb appCtxId: %lu\n", meta_tx.appCtxId);
1828                 /* post it to netcp sb tx channel*/
1829             meta2.u.tx_sb_meta=&meta_tx;
1831 /* TODO: Move the following ifdef code into ptkio_sendDM */
1832 #ifdef NET_TEST_ENABLE_SIDE_BAND_LOW_LEVEL_API
1833             /* Use the command label which was cached during create time
1834             * Update the Queue to receive output packet from SA to the local 
1835             * core Queue. Below information can be saved by application in 
1836             * per process context to avoid API overhead per packet
1837             * Application can use below rxSbSaQ for polling packets back from
1838             * SA
1839             */
1840             nwalRetVal =  
1841             nwal_getLocCxtInfo(PKTIO_GET_NWAL_INSTANCE(netcp_sb_tx_chan),
1842                                                       &nwalLocCxt);
1843             if(nwalRetVal == nwal_OK)
1844             {
1845                 p_sa_info->rx_dmPSCmdInfo.rxSbSaQ = nwalLocCxt.rxSbSaQ;
1846             }
1847             nwal_mCmdDMUpdate(tip,
1848                               &p_sa_info->rx_dmPSCmdInfo,
1849                               meta_tx.appCtxId,
1850                               meta_tx.encOffset,
1851                               meta_tx.encSize,
1852                               meta_tx.pEncIV,
1853                               meta_tx.authOffset,
1854                               meta_tx.authSize,
1855                               meta_tx.pAuthIV,
1856                               meta_tx.aadSize,
1857                               meta_tx.pAad);
1858             pPloadDesc = Pktlib_getDescFromPacket(tip);
1859             pPloadDesc = Qmss_osalConvertDescVirtToPhy(pPloadDesc);
1860             Qmss_queuePushDescSizeRaw(p_sa_info->rx_dmPSCmdInfo.txQueue,
1861                                        pPloadDesc,
1862                                        NWAL_DESC_SIZE);
1864 #else
1865             pktio_send(netcp_sb_tx_chan,tip,&meta2,&err);
1866 #endif
1867             continue;
1868         }
1869         else
1870         {
1871             //inflow mode.  flip and send
1872             flip_and_send_pkt(tip,p_pkt,len,1, enet_port);
1873         }
1874     }
1875     else if ((p_head->ip[2]&0x0000ff00)!=0x00001100)
1876     {
1877         stats[coreid].n_new+=1;Pktlib_freePacket(tip); continue;
1878     }
1879     else  //non ipsec
1880     {
1881         if (!check_header(p_head,&meta[i]))
1882         {
1883             stats[coreid].n_bad+=1;Pktlib_freePacket(tip); 
1884             continue;
1885         }
1887 #if 0
1888         /* lookup flow */
1889         key.src_ip = p_head->ip[3];
1890         key.dst_ip = p_head->ip[4];
1891         key.src_port= (p_head->udp[0]&0xffff0000)>>16;
1892         key.dst_port= (p_head->udp[0]&0x0000ffff);
1893         p_res= (HEAD_T *) trie_lookup(P_trie, (char *) &key, sizeof(key));
1894         if (!p_res) { stats[coreid].n_new+=1;  slow_path(tip, len); continue;}
1896         /* copy header */
1897         memcpy((char *) p_head, (char *) p_res, sizeof(HEAD_T));
1899         memcpy(&p_pkt[14],p_head,sizeof(HEAD_T));
1900         /* update_mac(&p_pkt[0]);  */
1902         /* 'simulate' send pkt */
1903         send_pkt(tip,len);
1904 #endif
1905              //just flip and send
1906             flip_and_send_pkt(tip,p_pkt,len,0, enet_port);
1907         }
1908     }
1909     //printf("recv done\n");
1912 #ifdef TEST_TIMERS
1913 //timer callback 
1914 void our_timer_cb( HPLIB_TIMER_GROUP_HANDLE_T th,
1915         int n_fired,     //# timers fired
1916         HPLIB_TIMER_LIST_T fired_list,
1917         uint64_t currentTime)
1919 int i;
1920 HPLIB_TIMER_T tx;
1921 int cookie;
1922 int err;
1923 unsigned long long et;
1924 #ifdef MULTI_THREAD
1925     int coreid=Osal_nwalGetProcId();  //who we are(thread local)
1926     //int coreid = our_core;
1927 #else
1928     int coreid=0;
1929 #endif
1930 //DEBUGprintf("TIMER CALLBACK @ %lld %d timers\n", currentTime, n_fired);
1931 tx = hplib_Timer_GetFirst(fired_list);
1932 for(i=0;i<n_fired;i++)
1934   cookie = (int) hplib_Time_rGetCookie(tx);
1935   et =  hplib_Timer_GetTs(tx); //debug
1936   //DEBUGprintf("   timer %d - cookie = %d expected ts=%lld (delta=%lld)\n", i, cookie, et, currentTime-et);
1937   if (cookie ==1)
1938   {  
1939      stats[coreid].n_t1+=1;
1940      t1 = hplib_Timer_Start(
1941         th,
1942         (void *) 1,
1943         100LL,  //timer group tics
1944         &err);
1945   }
1946   else if (cookie ==2)
1947   {
1948       stats[coreid].n_t2+=1;
1949       t2 = hplib_Timer_Start(
1950         th,
1951         (void *) 2,
1952         200LL,  //timer group ticks
1953         &err);
1954   }
1955   else
1956   {
1957     stats[coreid].n_t3+=1;
1958     t3 = hplib_Timer_Start(
1959         th,
1960         (void *) 3,
1961         300LL,  //timer group ticks
1962         &err);
1963     //cancel 1 and restart 1
1964    hplib_Timer_Cancel(th,t1,&err);
1965    t1 = hplib_Timer_Start(
1966         th,
1967         (void *) 1,
1968         100LL,  //timer group ticks
1969         &err);
1970  }
1971   tx = hplib_Timer_GetNext(fired_list,tx); 
1974 #endif
1975 void print_ipsec_stats(Sa_IpsecStats_t     *p_saIpsecStats, nwal_saAALG auth, nwal_saEALG cipher)
1977 #if 0
1978     if(retVal != nwal_OK)
1979     {
1980         System_printf("CORE: %d Error getting IP Sec Stats: Ret Status: %d \n",
1981                        retVal);
1982         return(nwal_FALSE);
1983     }
1984     if((p_saIpsecStats->pktEncHi) ||(p_saIpsecStats->pktEncLo))
1985     {
1986         printf("------------- IPSec TX (Encryption Channel) Stats BEGIN --\n");
1987     }
1988     else
1989     {
1990         printf("------------- IPSec RX (Decryption Channel) Stats BEGIN --\n");
1991     }
1992 #endif
1993     printf("\nAutentication mode: %d, Encryption Mode: %d\n", auth, cipher);
1994     printf("IPSec replayOld:0x%x,replayDup:0x%x,authFail:0x%x \n",
1995                    p_saIpsecStats->replayOld,p_saIpsecStats->replayDup,p_saIpsecStats->authFail);
1996     printf("IPSec txESN:0x%x,rxESN:0x%x,pktEncHi:0x%x,pktEncLo:0x%x,pktDecHi:0x%x,pktDecLo:0x%x \n",
1997                    p_saIpsecStats->txESN,p_saIpsecStats->rxESN,p_saIpsecStats->pktEncHi,
1998                    p_saIpsecStats->pktEncLo,p_saIpsecStats->pktDecHi,p_saIpsecStats->pktDecLo);
2001 void print_datamode_stats(Sa_DataModeStats_t     *p_saDataModeStats, nwal_saAALG auth, nwal_saEALG cipher)
2004     printf("\nAutentication mode: %d, Encryption Mode: %d\n", auth, cipher);
2005     printf(" Packets processedHi: 0x%x, Packets processed Lo:0x%x\n",
2006             p_saDataModeStats->pktHi, p_saDataModeStats->pktLo);
2012 static int np2process = NP;
2013 /******************************************************
2014  * stats callback
2015  *******************************************************/
2016 void our_stats_cb_mt(NETAPI_T h, paSysStats_t* pPaStats)
2019 #ifdef MULTI_THREAD
2020     int coreid=Osal_nwalGetProcId();  //who we are(thread local)
2021     //int coreid = our_core;
2022 #else
2023 int coreid=0;
2024 #endif
2026   stats[coreid].n_stats_cb +=1;
2027   if(pPaStats) memcpy(&netcp_stats,pPaStats, sizeof(paSysStats_t));
2029 void our_stats_cb(NETAPI_T h, paSysStats_t* pPaStats)
2031 uint32_t numFreeDataPackets;
2032 uint32_t            numZeroBufferPackets;
2033 uint32_t            numPacketsinGarbage;
2034 Pktlib_HeapStats    pktLibHeapStats;
2035 int i;
2036 unsigned long long bcpp;
2037 unsigned long long bcpp_noc;
2038 unsigned long long bcpp_app;
2039 unsigned long long bcpp_tx;
2040 unsigned long long npL;
2041 unsigned long long cyclesL;
2042 unsigned long long ccyclesL; //cache cycles
2043 unsigned long long tmp_npL[TUNE_NETAPI_NUM_CORES];
2044 unsigned long long tmp_cyclesL[TUNE_NETAPI_NUM_CORES];
2045 unsigned long long tmp_ccyclesL[TUNE_NETAPI_NUM_CORES]; //cache cycles
2046 NETAPI_SA_STATS_T netapi_sa_stats;
2048     for (i=0;i < TUNE_NETAPI_NUM_CORES;i++)
2049     {
2050         printf("Per Core Statistics for CORE %d\n", i);
2051         printf(">*****stats @ %lld (#cbs%d) \n", hplib_mUtilGetTimestamp(),stats[i].n_stats_cb);
2052         printf(">itx=%d rx=%d tx=%d bad=%d slow=%d \n>rx_class0=%d rx_class1=%d rx_class2=%dsecRx=%d\t"
2053             "secPRX=%d sb_rx=%d sb_tx=%d auth_ok=%d sec_tx=%d  min_rx=%d min_tx=%d  fragPkt=%d, exceptionPktOther=%d\n",
2054          stats[i].itx, stats[i].rx, stats[i].tx, stats[i].n_bad, stats[i].n_new, 
2055          stats[i].n_class0_rx, stats[i].n_class1_rx,  stats[i].n_class2_rx, stats[i].sec_rx,
2056          stats[i].secp_rx, stats[i].sb_rx, stats[i].sb_tx, stats[i].n_auth_ok,
2057          stats[i].sec_tx, stats[i].rx_min, stats[i].tx_min, stats[i].exceptionPktsFrag, stats[i].exceptionPktsOther);
2058         printf(">if rx stats:  %d %d %d\n",stats[i].if_rx[0],stats[i].if_rx[1],stats[i].if_rx[2]);
2059         printf(">core rx stats:  %d \n",stats[i].core_rx);
2061         if (stats[i].rx && stats[i].tx)
2062             printf("decrypt time per packet(avg): %lu, encrypt  time per packet(avg): %lu\n", 
2063             (unsigned long) stats[i].total_decrypt_time/stats[i].rx, (unsigned long) stats[i].total_encrypt_time/stats[i].tx);
2066         if (npL && stats[i].rx)
2067         {
2068             bcpp = cyclesL/npL; 
2069             bcpp_noc = (cyclesL-ccyclesL)/npL; 
2070             bcpp_app = (stats[i].app_cycles-stats[i].tx_cache_cycles)/stats[i].rx;
2071         }
2072         else
2073         {
2074             bcpp = bcpp_noc=bcpp_app=0L;
2075         }
2076         if (stats[i].tx)
2077         {
2078             bcpp_tx = (stats[i].send_cycles-stats[i].tx_cache_cycles)/stats[i].tx;
2079         }
2080         else
2081         {
2082             bcpp_tx = 0L;
2083         }
2084         printf(">         ++ busy cycles pp=%lld (%lld wo cache ops) (app+tx= %lld) (tx= %lld) ++\n",
2085          bcpp,bcpp_noc,bcpp_app, bcpp_tx);
2086         printf("\n");
2087     }
2089 #ifdef MULTI_THREAD
2090     netapi_sched_get_stats(scheduler[0],&tmp_npL[0],&tmp_cyclesL[0],&tmp_ccyclesL[0]);
2091     netapi_sched_get_stats(scheduler[1],&tmp_npL[1],&tmp_cyclesL[1],&tmp_ccyclesL[1]);
2092 npL = tmp_npL[0]+tmp_npL[1];
2093 cyclesL = tmp_cyclesL[0]+tmp_cyclesL[1];
2094 ccyclesL = tmp_ccyclesL[0]+tmp_ccyclesL[1];
2095 #else
2096 netapi_sched_get_stats(our_sched, &npL,&cyclesL,&ccyclesL);
2097 #endif
2099 if(pPaStats)
2101        printf("C1 number of packets:           %d\n", pPaStats->classify1.nPackets);
2102        printf("C1 number IPv4 packets:         %d\n", pPaStats->classify1.nIpv4Packets);
2103        printf("C1 number IPv6 packets:        %d\n", pPaStats->classify1.nIpv6Packets);
2104        printf("C1 number Custom packets:        %d\n", pPaStats->classify1.nCustomPackets);
2105        printf("C1 number SRIO packets:        %d\n", pPaStats->classify1.nSrioPackets);
2106        printf("C1 number LLC/SNAP Fail packets:        %d\n", pPaStats->classify1.nLlcSnapFail);
2107        printf("C1 number table matched:        %d\n", pPaStats->classify1.nTableMatch);
2108        printf("C1 number failed table matched: %d\n", pPaStats->classify1.nNoTableMatch);
2109        printf("C1 number IP Fragmented packets: %d\n", pPaStats->classify1.nIpFrag);
2110        printf("C1 number IP Depth Overflow: %d\n", pPaStats->classify1.nIpDepthOverflow);
2111        printf("C1 number VLAN Depth Overflow: %d\n", pPaStats->classify1.nVlanDepthOverflow);
2112        printf("C1 number GRE Depth Overflow: %d\n", pPaStats->classify1.nGreDepthOverflow);
2113        printf("C1 number MPLS Packets: %d\n", pPaStats->classify1.nMplsPackets);
2114        printf ("C1 number of parse fail:        %d\n",pPaStats->classify1.nParseFail);
2115        printf("C1 number of Invalid IPv6 Opt:  %d\n", pPaStats->classify1.nInvalidIPv6Opt);
2116        printf("C1 number of TX IP Fragments:  %d\n", pPaStats->classify1.nTxIpFrag);
2117        printf ("C1 number of silent discard:    %d\n",pPaStats->classify1.nSilentDiscard);
2118        printf("C1 number of invalid control:   %d\n", pPaStats->classify1.nInvalidControl);
2119        printf ("C1 number of invalid states:    %d\n",pPaStats->classify1.nInvalidState);
2120        printf ("C1 number of system fails:      %d\n",pPaStats->classify1.nSystemFail);
2121        printf ("C2 number Packets  :           %d\n",pPaStats->classify2.nPackets);
2122        printf ("C2 number udp           :      %d\n",pPaStats->classify2.nUdp);
2123        printf ("C2 number tcp           :      %d\n",pPaStats->classify2.nTcp);
2124        printf ("C2 number Custom       :      %d\n",pPaStats->classify2.nCustom);
2125        printf ("C2 number silent drop   :      %d\n",pPaStats->classify2.nSilentDiscard);
2126        printf ("C2 number invalid cntrl :      %d\n\n",pPaStats->classify2.nInvalidControl);
2127        printf ("C2 number Modify Stats Cmd Fail :      %d\n\n",pPaStats->modify.nCommandFail);
2129 Pktlib_getHeapStats(ourHeap, &pktLibHeapStats);
2130 printf("main heap stats>  #free=%d #zb=%d #garbage=%d\n", pktLibHeapStats.numFreeDataPackets,
2131                                 pktLibHeapStats.numZeroBufferPackets, pktLibHeapStats.numPacketsinGarbage);
2132 printf("               >  #dataBufThreshStatus=%d #dataBufStarvCounter=%d #zBufThreshStatus=%d #zBufStarvCounter=%d \n", 
2133                         pktLibHeapStats.dataBufferThresholdStatus,pktLibHeapStats.dataBufferStarvationCounter,
2134                         pktLibHeapStats.zeroDataBufferThresholdStatus, pktLibHeapStats.zeroDataBufferStarvationCounter);
2136 Pktlib_getHeapStats(specialSmall, &pktLibHeapStats);
2137 printf("specialSmall heap stats>  #free=%d #zb=%d #garbage=%d\n", pktLibHeapStats.numFreeDataPackets,
2138                                 pktLibHeapStats.numZeroBufferPackets, pktLibHeapStats.numPacketsinGarbage);
2139 printf("                       >  #dataBufThreshStatus=%d #dataBufStarvCounter=%d #zBufThreshStatus=%d #zBufStarvCounter=%d \n", 
2140                                 pktLibHeapStats.dataBufferThresholdStatus,pktLibHeapStats.dataBufferStarvationCounter,
2141                                 pktLibHeapStats.zeroDataBufferThresholdStatus, pktLibHeapStats.zeroDataBufferStarvationCounter);
2144 Pktlib_getHeapStats(specialLarge, &pktLibHeapStats);
2145 printf("specialLarge heap stats>  #free=%d #zb=%d #garbage=%d\n", pktLibHeapStats.numFreeDataPackets,
2146                                 pktLibHeapStats.numZeroBufferPackets, pktLibHeapStats.numPacketsinGarbage);
2147 printf("                       >  #dataBufThreshStatus=%d #dataBufStarvCounter=%d #zBufThreshStatus=%d #zBufStarvCounter=%d \n", 
2148                                 pktLibHeapStats.dataBufferThresholdStatus,pktLibHeapStats.dataBufferStarvationCounter,
2149                                 pktLibHeapStats.zeroDataBufferThresholdStatus, pktLibHeapStats.zeroDataBufferStarvationCounter);
2152 #if 0
2153 printf("pa2sa descriptor area dump\n");
2154 for(i=0;i<TUNE_NETAPI_CONFIG_MAX_PA_TO_SA_DESC;i++)
2156    extern long * pa2sa_descr_base;
2157    long * tip= &pa2sa_descr_base[32*i]; 
2158    dump_descr(tip, i);
2160 printf("sa2pa descriptor area dump\n");
2161 for(i=0;i<TUNE_NETAPI_CONFIG_MAX_SA_TO_PA_DESC;i++)
2163    extern long * sa2pa_descr_base;
2164    long * tip= &sa2pa_descr_base[32*i]; 
2165    dump_descr(tip, i);
2167 #endif
2168 #ifdef TEST_TIMERS
2169 //debug = dump timer polling stats
2170 dump_poll_stats();
2171 //debug = dump timer polling stats
2172 //dump_poll_stats();
2173 #endif
2174 #ifndef EXPERIMENTAL
2175     for (i = 0; i < MAX_SEC_INDEX; i++)
2176 #else
2177     for (i=0; i < 1;i++)
2178 #endif
2179     {
2180         /* Statistics for RX Tunnel */
2181         memset(&netapi_sa_stats, 0, sizeof(netapi_sa_stats));
2182         netapi_getSaStats(netapi_handle, sa_info[i].rx_tunnel, &netapi_sa_stats);
2183         if (netapi_sa_stats.validParams & NETAPI_IPSEC_STAT_VALID)
2184         {
2185              print_ipsec_stats(&(netapi_sa_stats.saIpsecStats), 
2186                                  rx_sa[i].authMode, 
2187                                  rx_sa[i].cipherMode);
2188         }
2189         if (netapi_sa_stats.validParams & NETAPI_SIDEBAND_DATA_MODE_STAT_VALID)
2190         {
2191             print_datamode_stats(&(netapi_sa_stats.dataModeStats),
2192                                    rx_sa[i].authMode, 
2193                                    rx_sa[i].cipherMode);
2194         }
2195         
2196         /* Statistics for TX Tunnel */
2197         memset(&netapi_sa_stats, 0, sizeof(netapi_sa_stats));
2198         netapi_getSaStats(netapi_handle, sa_info[i].tx_tunnel, &netapi_sa_stats);
2199         if (netapi_sa_stats.validParams & NETAPI_IPSEC_STAT_VALID)
2200         {
2201              print_ipsec_stats(&(netapi_sa_stats.saIpsecStats), 
2202                                  tx_sa[i].authMode, 
2203                                  tx_sa[i].cipherMode);
2204         }
2205         if (netapi_sa_stats.validParams & NETAPI_SIDEBAND_DATA_MODE_STAT_VALID)
2206         {
2207             print_datamode_stats(&(netapi_sa_stats.dataModeStats),
2208                                    tx_sa[i].authMode, 
2209                                    tx_sa[i].cipherMode);
2210         }
2211     }
2212     netapi_dump_internal_heap_stats();
2215 //******************************************************
2216 //use scheduling housekeeping callback to generate pkts
2217 //******************************************************
2218 static int done_burst=0;
2219 void house(NETAPI_SCHED_HANDLE_T * s)
2221 Ti_Pkt * tip;
2222 unsigned int len;
2223 nwalTxPktInfo_t meta_tx = {0};
2224 PKTIO_METADATA_T meta = {PKTIO_META_TX,{0},0};
2225 int err;
2226 static int house_pkts_gened=0;
2227 int p;
2228 unsigned char * pIpHdr,* pData;
2229 unsigned int vv1,vv2,vv3;
2230 unsigned int sum_vv1=0;
2231 unsigned int sum_vv2=0;
2232 unsigned int sum_vv3=0;
2233 unsigned int sum_vv4=0;
2234 unsigned int sum_vv5=0;
2236 unsigned int nwal_flow_vv1,nwal_flow_vv2;
2237 unsigned int nwal_sum_vv1=0;
2238 unsigned int nwal_sum_vv2=0;
2239 unsigned int nwal_sum_vv3=0;
2240 unsigned int nwal_sum_vv4=0;
2241 unsigned int nwal_sum_vv5=0;
2242 unsigned int nwal_sum_vv6=0;
2244 unsigned int nwal_sum_flow_vv1=0;
2245 unsigned int nwal_sum_flow_vv2=0;
2246 unsigned long long cache_op_b1;
2247 unsigned long long  cache_op_b2;
2248 unsigned long long  n_c_ops;
2249 static int first =0;
2250 Cppi_HostDesc*      pPktDesc;
2252 int coreid = 0;  //who we are
2253 #ifdef MULTI_THREAD
2254 NETAPI_T nh= netapi_schedGetNetapiHandle(s);
2255 coreid=(int) netapi_getCookie(nh);
2257 if (QUIT) {netapi_schedShutdown(s,NULL,&err); return;}
2259 if (coreid==0) //slave
2261    netcp_cfgReqStats(nh, our_stats_cb_mt, 0,&err); 
2263 #else
2264 if(done_burst)
2266   house_pkts_gened+=TX_BURST;
2267   printf("net_test> request stats at n=%d \n",house_pkts_gened);
2268   netcp_cfgReqStats(netapi_handle, our_stats_cb, 0,&err);
2269   if (err!=0) {printf("stats req failed\n");}
2270   if (house_pkts_gened >= np2process+ 100)
2271   {
2272      netapi_schedShutdown(s,NULL,&err);
2273   }
2274   return;
2276 done_burst=1;
2277 Osal_cache_op_measure_reset();
2278 memset(&meta_tx,0,sizeof(meta_tx));
2279 for(p=0;p<TX_BURST;p++) {  
2280 //reguest stats 
2281 if ((house_pkts_gened>0) && (! (house_pkts_gened%1000)) )
2283    printf("net_test> request stats at n=%d \n",house_pkts_gened);
2284    netcp_cfgReqStats(netapi_handle, our_stats_cb, 0,&err); 
2285    if (err!=0) {printf("stats req failed\n");}
2289   if (house_pkts_gened >= np2process+ 100)
2290   {
2291      //shutdown
2292      netapi_schedShutdown(s,NULL,&err);
2293      continue;
2294   }
2296   else if (house_pkts_gened >= np2process) { house_pkts_gened+=1;  continue;}
2297   
2299 /* manufacture a pkt to transmit */
2300    tip = get_pkt(house_pkts_gened, &len, ourHeap, PKT_LEN,&testPkt[0]  , TEST_PKT_LEN);
2301    if(!tip) { house_pkts_gened +=1; continue; }
2304    /* set the pkt length */
2305    vv1 = hplib_mUtilGetPmuCCNT();
2306    Pktlib_setPacketLen(tip, len);
2308    /* set up meta data */
2309     meta.sa_handle=nwal_HANDLE_INVALID;
2310     /* #define BENCH_UDP_SEND */
2311 #ifdef BEND_UDP_SEND
2312     meta_tx.txFlag1 = (NWAL_TX_FLAG1_DO_IPV4_CHKSUM | NWAL_TX_FLAG1_DO_UDP_CHKSUM| NWAL_TX_FLAG1_META_DATA_VALID);
2313     meta_tx.startOffset = 0;
2314     /* GONE in V2 meta_tx.pktLen = len; */
2315     meta_tx.ipOffBytes = TEST_PKT_IP_OFFSET_BYTES;
2316     meta_tx.l4OffBytes = TEST_PKT_UDP_OFFSET_BYTES;
2317     meta_tx.l4HdrLen = TEST_PKT_UDP_HDR_LEN;
2318     //GONE in V2 meta_tx.ploadOffBytes = TEST_PKT_PLOAD_OFFSET_BYTES;
2319     meta_tx.ploadLen = TEST_PAYLOAD_LEN;
2321     Pktlib_getDataBuffer(tip,&pData,&len);
2322    if(house_pkts_gened &0x1)
2323    {
2324         memcpy(&pData[6],&config.mac1[0] ,6);
2325    }
2326     pIpHdr = pData + meta_tx.ipOffBytes;
2327     meta_tx.pseudoHdrChecksum =
2328         test_utilGetIpv4PsudoChkSum(pIpHdr,(TEST_PAYLOAD_LEN+TEST_PKT_UDP_HDR_LEN));
2329 #else
2330    Pktlib_getDataBuffer(tip,&pData,&len);
2331    if(house_pkts_gened &0x1)
2332    {
2333         memcpy(&pData[6],&config.mac1[0] ,6);
2334    }
2335     meta_tx.txFlag1 = NWAL_TX_FLAG1_META_DATA_VALID;
2336     meta_tx.startOffset = 0;
2337     meta_tx.ploadLen = TEST_PAYLOAD_LEN;
2338 #endif
2339    /* post it to netcp tx channel*/
2340    meta.u.tx_meta=&meta_tx;
2341 #ifdef DEBUG_DESC
2342    if (house_pkts_gened<16) dump_descr((long *) tip, house_pkts_gened);
2343    else if (house_pkts_gened>99) dump_descr((long *) tip,house_pkts_gened);
2344 #endif
2346    if(!first)
2347    {
2348        first++;
2349        nwal_flow_vv1= hplib_mUtilGetPmuCCNT();
2350        if(nwal_initPSCmdInfo(PKTIO_GET_NWAL_INSTANCE(netcp_tx_chan),
2351                              &meta_tx,
2352                              &flowPSCmdInfo) != nwal_OK)
2353        {
2354            printf("nwal_initPSCmdInfo() ERROR \n");
2355        }
2356        nwal_flow_vv2= hplib_mUtilGetPmuCCNT();
2357        nwal_sum_flow_vv1 += (nwal_flow_vv1-vv1); 
2358        nwal_sum_flow_vv2 += (nwal_flow_vv2-nwal_flow_vv1); 
2359    }
2360        
2361    cache_op_b1= Osal_cache_op_measure(&n_c_ops);
2362    vv2= hplib_mUtilGetPmuCCNT();
2363 #ifdef BEND_UDP_SEND
2364    nwal_mCmdSetL4CkSumPort(  tip,
2365                              &flowPSCmdInfo,
2366                              TEST_PKT_UDP_OFFSET_BYTES,
2367                              (TEST_PKT_UDP_HDR_LEN + TEST_PAYLOAD_LEN),
2368                              meta_tx.pseudoHdrChecksum,
2369                              meta_tx.enetPort);
2370 #else
2371    nwal_mCmdSetPort  (tip,
2372                       &flowPSCmdInfo,  //could be NULL
2373                       0);  //port 0 -> switch decides
2375 #endif
2377    pPktDesc = Pktlib_getDescFromPacket(tip);
2378    /* Send the packet out to transmit Q*/
2379    Qmss_queuePushDescSize (flowPSCmdInfo.txQueue, 
2380                         pPktDesc, 
2381                         NWAL_DESC_SIZE);
2382    vv3= hplib_mUtilGetPmuCCNT();
2383    cache_op_b2= Osal_cache_op_measure(&n_c_ops);
2385    sum_vv1 += (vv2-vv1);
2386    if(!house_pkts_gened)
2387    {
2388        /* first packet. Take out the PS command label creation cost */
2389        sum_vv1 = sum_vv1 - nwal_sum_flow_vv2;
2390    }
2392    sum_vv3 += (vv3-vv2)-(long) (cache_op_b2-cache_op_b1); //sub out cache op cost
2394    // printf("pktio send. full=%d metadata=%d pktio_send=%d\n", vv3-vv1,  vv2-vv1,  vv3-vv2);
2395    
2397    stats[coreid].itx +=1;
2398    house_pkts_gened +=1;
2399  }
2401    unsigned long long  ccycles;
2402    ccycles =Osal_cache_op_measure(&n_c_ops);
2403    if (sum_vv1) 
2404    {
2405       printf("BURST NWAL Fast send %d pkts.  metadata=%d Cmd Label Creation Cost=%d  nwal Fast Send Cost (less cacheop)= %d n_c_ops=%lld cache_op_time=%lld (pp-> %d)\n", 
2406               stats[coreid].itx, sum_vv1/stats[coreid].itx,  nwal_sum_flow_vv2, sum_vv3/stats[coreid].itx, 
2407               n_c_ops, ccycles, n_c_ops? (ccycles/(n_c_ops/2L)) : 0);
2408 #if 0
2409       printf("NWAL Profile Cycles: Prof1= %d,Prof2=%d,Prof3=%d,Prof4=%d,Prof5=%d ,Prof6=%d \n",
2410               nwal_sum_vv1/stats[coreid].itx,nwal_sum_vv2/stats[coreid].itx,nwal_sum_vv3/stats[coreid].itx,
2411               nwal_sum_vv4/stats[coreid].itx,nwal_sum_vv5/stats[coreid].itx,nwal_sum_vv6/stats[coreid].itx);
2412   
2413 #endif  
2415       if(stats[core_id].itx2)
2416       {
2417           printf("nwal_flowSend Profile Cycles: Prof1= %d,Prof2=%d \n",
2418               nwal_sum_flow_vv1/stats[coreid].itx2,nwal_sum_flow_vv2/stats[coreid].itx2);
2419       }
2421    }
2423 #endif
2426 void  build_sa_db(int i)
2428     long tmp_spi;
2429     if ((tx_sa[i].authMode == NWAL_SA_AALG_HMAC_SHA1) && (tx_sa[i].cipherMode == NWAL_SA_EALG_AES_CBC))
2430     {
2431         /* static configuration, will not change */
2432         sa_info[i].tx_payload_info.aadSize = 0;
2433         sa_info[i].tx_payload_info.pAad = NULL;
2434         sa_info[i].tx_payload_info.pAuthIV = NULL;
2435         sa_info[i].tx_payload_info.authOffset = netTest_MAC_HEADER_LEN  + netTest_IP_HEADER_LEN;        /*done: same for all cipher suites */         
2436         sa_info[i].tx_payload_info.encOffset = netTest_MAC_HEADER_LEN +
2437                                                                      netTest_IP_HEADER_LEN +
2438                                                                     netTest_ESP_HEADER_LEN +
2439                                                                     netTest_AES_CBC_IV_LEN;
2441         /* dynamic configuration, will  be calculated on the fly */
2442         sa_info[i].tx_payload_info.authSize = 0; /* pkt len - mac - ip -icv (12) */
2443         sa_info[i].tx_payload_info.encSize = 0;   /* authSize - esp header size (always 8 bytes) */
2444         sa_info[i].tx_payload_info.pEncIV = 0;
2445         sa_info[i].tx_payload_info.pPkt = 0;     /* not being referenced in net_test */
2447         sa_info[i].cipherMode = NWAL_SA_EALG_AES_CBC;
2448         sa_info[i].inner_ip_offset = sa_info[i].tx_payload_info.encOffset;
2449         sa_info[i].auth_tag_size = netTest_ICV_LEN;  /* icv or mac size,. always 12 except for AES_CCM/AES_GCM */
2450 #ifdef EXPERIMENTAL
2451         sa_info[i].iv_len=16;
2452         sa_info[i].bl=16;
2453         sa_info[i].spi = tx_sa[i].spi;
2454         sa_info[i].src =*((unsigned int *)(&config.local_ipsec_ip.ipv4[0]));
2455         sa_info[i].dst =*((unsigned int *)(&config.remote_ipsec_ip.ipv4[0]));
2456 #endif
2457         sa_info[i].tx_pkt_info.enetPort = 0;
2458         sa_info[i].tx_pkt_info.ipOffBytes = sa_info[i].tx_payload_info.encOffset;
2459         sa_info[i].tx_pkt_info.l4HdrLen = netTest_UDP_HEADER_LEN; /*UDP header len */
2460         sa_info[i].tx_pkt_info.l4OffBytes = sa_info[i].inner_ip_offset + netTest_IP_HEADER_LEN;
2461         sa_info[i].tx_pkt_info.startOffset = 0;
2462         sa_info[i].tx_pkt_info.lpbackPass = 0;
2463         sa_info[i].tx_pkt_info.ploadLen = 0; /*sa_info[i].tx_pkt_info.l4OffBytes + 4 */
2464         sa_info[i].tx_pkt_info.pPkt = NULL;
2465         sa_info[i].tx_pkt_info.saOffBytes = netTest_MAC_HEADER_LEN  + netTest_IP_HEADER_LEN;
2466         sa_info[i].tx_pkt_info.saPayloadLen = 0;
2467         sa_info[i].tx_pkt_info.pseudoHdrChecksum =0;
2468        
2469         sa_info[i].tx_pkt_info.txFlag1 = NWAL_TX_FLAG1_DO_IPSEC_ESP_CRYPTO| NWAL_TX_FLAG1_DO_UDP_CHKSUM| NWAL_TX_FLAG1_META_DATA_VALID ;
2471         tmp_spi = htonl((long)(tx_sa[i].spi));
2472          trie_insert(p_trie_sa,(char *)&tmp_spi,4, (void *) &sa_info[i]); //asociate with tx sa SPI
2473          
2474          
2475     }
2476     else if ((tx_sa[i].authMode == NWAL_SA_AALG_HMAC_SHA2_256) && (tx_sa[i].cipherMode == NWAL_SA_EALG_AES_CTR))
2477     {
2478         /* static configuration, will not change */
2479         sa_info[i].tx_payload_info.aadSize = 0;
2480         sa_info[i].tx_payload_info.pAad = NULL;
2481         sa_info[i].tx_payload_info.pAuthIV = NULL;
2482         sa_info[i].tx_payload_info.authOffset = netTest_MAC_HEADER_LEN  + netTest_IP_HEADER_LEN;        /*done: same for all cipher suites */
2483         sa_info[i].tx_payload_info.encOffset = netTest_MAC_HEADER_LEN +
2484                                                                      netTest_IP_HEADER_LEN +
2485                                                                     netTest_ESP_HEADER_LEN +
2486                                                                     netTest_AES_CTR_IV_LEN;
2491         /* dynamic configuration, will  be calculated on the fly */
2492         sa_info[i].tx_payload_info.authSize = 0;
2493         sa_info[i].tx_payload_info.encSize = 0;
2494         sa_info[i].tx_payload_info.pEncIV = 0;
2495         sa_info[i].tx_payload_info.pPkt = 0;
2497         sa_info[i].cipherMode = NWAL_SA_EALG_AES_CTR;
2498         sa_info[i].inner_ip_offset = sa_info[i].tx_payload_info.encOffset;
2499         sa_info[i].auth_tag_size = netTest_ICV_LEN;  /* icv or mac size,. always 12 except for AES_CCM/AES_GCM */
2500 #ifdef EXPERIMENTAL
2501         sa_info[i].iv_len=8;
2502         sa_info[i].bl=8;
2503         sa_info[i].spi = tx_sa[i].spi;
2504         sa_info[i].src =*((unsigned int *)(&config.local_ipsec_ip.ipv4[0]));
2505         sa_info[i].dst =*((unsigned int *)(&config.remote_ipsec_ip.ipv4[0]));
2506 #endif
2508         sa_info[i].tx_pkt_info.enetPort = 0;
2509         sa_info[i].tx_pkt_info.ipOffBytes = sa_info[i].tx_payload_info.encOffset;
2510         sa_info[i].tx_pkt_info.l4HdrLen = netTest_UDP_HEADER_LEN; /*UDP header len */
2511         sa_info[i].tx_pkt_info.l4OffBytes = sa_info[i].inner_ip_offset + netTest_IP_HEADER_LEN;
2512         sa_info[i].tx_pkt_info.startOffset = 0;
2513         sa_info[i].tx_pkt_info.lpbackPass = 0;
2514         sa_info[i].tx_pkt_info.ploadLen = 0; /*sa_info[i].tx_pkt_info.l4OffBytes + 4 */
2515         sa_info[i].tx_pkt_info.pPkt = NULL;
2516         sa_info[i].tx_pkt_info.saOffBytes = netTest_MAC_HEADER_LEN  + netTest_IP_HEADER_LEN;
2517         sa_info[i].tx_pkt_info.saPayloadLen = 0;
2518         sa_info[i].tx_pkt_info.pseudoHdrChecksum =0;
2519        
2520         sa_info[i].tx_pkt_info.txFlag1 = NWAL_TX_FLAG1_DO_IPSEC_ESP_CRYPTO| NWAL_TX_FLAG1_DO_UDP_CHKSUM| NWAL_TX_FLAG1_META_DATA_VALID ;
2521         tmp_spi = htonl((long)(tx_sa[i].spi));
2522          trie_insert(p_trie_sa,(char *)&tmp_spi,4, (void *) &sa_info[i]); //asociate with tx sa SPI
2523     }
2524     else if ((tx_sa[i].authMode == NWAL_SA_AALG_HMAC_SHA2_256) && (tx_sa[i].cipherMode == NWAL_SA_EALG_3DES_CBC))
2525     {
2526         /* static configuration, will not change */
2527         sa_info[i].tx_payload_info.aadSize = 0;
2528         sa_info[i].tx_payload_info.pAad = NULL;
2529         sa_info[i].tx_payload_info.pAuthIV = NULL;
2530         sa_info[i].tx_payload_info.authOffset = netTest_MAC_HEADER_LEN  + netTest_IP_HEADER_LEN;        /*done: same for all cipher suites */
2531         sa_info[i].tx_payload_info.encOffset = netTest_MAC_HEADER_LEN +
2532                                                                      netTest_IP_HEADER_LEN +
2533                                                                     netTest_ESP_HEADER_LEN +
2534                                                                     netTest_3DES_CBC_IV_LEN;
2536         /* dynamic configuration, will  be calculated on the fly */
2537         sa_info[i].tx_payload_info.authSize = 0;
2538         sa_info[i].tx_payload_info.encSize = 0;
2539         sa_info[i].tx_payload_info.pEncIV = 0;
2540         sa_info[i].tx_payload_info.pPkt = 0;
2542         sa_info[i].cipherMode = NWAL_SA_EALG_3DES_CBC;
2543         sa_info[i].inner_ip_offset = sa_info[i].tx_payload_info.encOffset;
2544         sa_info[i].auth_tag_size = netTest_ICV_LEN;  /* icv or mac size,. always 12 except for AES_CCM/AES_GCM */
2545 #ifdef EXPERIMENTAL
2546         sa_info[i].iv_len=8;
2547         sa_info[i].bl=8;
2548         sa_info[i].spi = tx_sa[i].spi;
2549         sa_info[i].src =*((unsigned int *)(&config.local_ipsec_ip.ipv4[0]));
2550         sa_info[i].dst =*((unsigned int *)(&config.remote_ipsec_ip.ipv4[0]));
2551 #endif
2553         sa_info[i].tx_pkt_info.enetPort = 0;
2554         sa_info[i].tx_pkt_info.ipOffBytes = sa_info[i].tx_payload_info.encOffset;
2555         sa_info[i].tx_pkt_info.l4HdrLen = netTest_UDP_HEADER_LEN; /*UDP header len */
2556         sa_info[i].tx_pkt_info.l4OffBytes = sa_info[i].inner_ip_offset + netTest_IP_HEADER_LEN;
2557         sa_info[i].tx_pkt_info.startOffset = 0;
2558         sa_info[i].tx_pkt_info.lpbackPass = 0;
2559         sa_info[i].tx_pkt_info.ploadLen = 0; /*sa_info[i].tx_pkt_info.l4OffBytes + 4 */
2560         sa_info[i].tx_pkt_info.pPkt = NULL;
2561         sa_info[i].tx_pkt_info.saOffBytes = netTest_MAC_HEADER_LEN  + netTest_IP_HEADER_LEN;
2562         sa_info[i].tx_pkt_info.saPayloadLen = 0;
2563         sa_info[i].tx_pkt_info.pseudoHdrChecksum =0;
2564        
2565         sa_info[i].tx_pkt_info.txFlag1 = NWAL_TX_FLAG1_DO_IPSEC_ESP_CRYPTO| NWAL_TX_FLAG1_DO_UDP_CHKSUM| NWAL_TX_FLAG1_META_DATA_VALID ;
2567         tmp_spi = htonl((long)(tx_sa[i].spi));
2568          trie_insert(p_trie_sa,(char *)&tmp_spi,4, (void *) &sa_info[i]); //asociate with tx sa SPI
2569     }
2570     else if ((tx_sa[i].authMode == NWAL_SA_AALG_HMAC_MD5) && (rx_sa[i].cipherMode == NWAL_SA_EALG_NULL))
2571     {
2572         /* static configuration, will not change */
2573         sa_info[i].tx_payload_info.aadSize = 0;
2574         sa_info[i].tx_payload_info.pAad = NULL;
2575         sa_info[i].tx_payload_info.pAuthIV = NULL;
2576         //sa_info[i].tx_payload_info.authOffset = netTest_MAC_HEADER_LEN  + netTest_IP_HEADER_LEN + 24;        /*done: same for all cipher suites */
2577         sa_info[i].tx_payload_info.authOffset = netTest_MAC_HEADER_LEN;
2579         sa_info[i].tx_payload_info.encOffset =         netTest_MAC_HEADER_LEN +
2580                                                                      netTest_IP_HEADER_LEN +
2581                                                                     netTest_NULL_ESP_HEADER_LEN +
2582                                                                     netTest_NULL_IV_LEN +
2583                                                                     24;
2585         
2586 #ifdef EXPERIMENTAL
2587         sa_info[i].iv_len=0;
2588         sa_info[i].bl=4;
2589         sa_info[i].spi = tx_sa[i].spi;
2590         sa_info[i].src =*((unsigned int *)(&config.local_ipsec_ip.ipv4[0]));
2591         sa_info[i].dst =*((unsigned int *)(&config.remote_ipsec_ip.ipv4[0]));
2592 #endif
2595         /* dynamic configuration, will  be calculated on the fly */
2596         sa_info[i].tx_payload_info.authSize = 0;
2597         sa_info[i].tx_payload_info.encSize = 0;
2598         sa_info[i].tx_payload_info.pEncIV = 0;
2599         sa_info[i].tx_payload_info.pPkt = 0;
2601         sa_info[i].cipherMode = NWAL_SA_EALG_NULL;
2602         sa_info[i].inner_ip_offset = sa_info[i].tx_payload_info.encOffset;
2603         sa_info[i].auth_tag_size = netTest_ICV_LEN;  /* icv or mac size,. always 12 except for AES_CCM/AES_GCM */
2605         sa_info[i].tx_pkt_info.enetPort = 0;
2606         sa_info[i].tx_pkt_info.ipOffBytes = sa_info[i].tx_payload_info.encOffset;
2607         sa_info[i].tx_pkt_info.l4HdrLen = netTest_UDP_HEADER_LEN; /*UDP header len */
2608         sa_info[i].tx_pkt_info.l4OffBytes = sa_info[i].inner_ip_offset + netTest_IP_HEADER_LEN;
2609         sa_info[i].tx_pkt_info.startOffset = 0;
2610         sa_info[i].tx_pkt_info.lpbackPass = 0;
2611         sa_info[i].tx_pkt_info.ploadLen = 0; /*sa_info[i].tx_pkt_info.l4OffBytes + 4 */
2612         sa_info[i].tx_pkt_info.pPkt = NULL;
2613         sa_info[i].tx_pkt_info.saOffBytes = netTest_MAC_HEADER_LEN;
2614         sa_info[i].tx_pkt_info.saPayloadLen = 0;
2615         sa_info[i].tx_pkt_info.pseudoHdrChecksum =0;
2616         sa_info[i].tx_pkt_info.txFlag1 = NWAL_TX_FLAG1_DO_IPSEC_AH_CRYPTO| NWAL_TX_FLAG1_DO_UDP_CHKSUM | NWAL_TX_FLAG1_META_DATA_VALID ;
2617         tmp_spi = htonl((long)(tx_sa[i].spi));
2618          trie_insert(p_trie_sa,(char *)&tmp_spi,4, (void *) &sa_info[i]); //asociate with tx sa SPI
2619     }
2620     else if ((tx_sa[i].authMode == NWAL_SA_AALG_NULL) && (rx_sa[i].cipherMode == NWAL_SA_EALG_AES_GCM))
2621     {
2622         /* static configuration, will not change */
2623         sa_info[i].tx_payload_info.aadSize = 0;
2624         sa_info[i].tx_payload_info.pAad = NULL;
2625         sa_info[i].tx_payload_info.pAuthIV = NULL;
2626         sa_info[i].tx_payload_info.authOffset = netTest_MAC_HEADER_LEN  + netTest_IP_HEADER_LEN;        /*done: same for all cipher suites */
2627         //sa_info[i].tx_payload_info.encIvSize = netTest_AES_GCM_IV_LEN;
2629         sa_info[i].tx_payload_info.encOffset =         netTest_MAC_HEADER_LEN +
2630                                                                      netTest_IP_HEADER_LEN +
2631                                                                     netTest_ESP_HEADER_LEN +
2632                                                                     netTest_AES_GCM_IV_LEN;
2633 #ifdef EXPERIMENTAL
2634         sa_info[i].iv_len=0;
2635         sa_info[i].bl=4;
2636         sa_info[i].spi = tx_sa[i].spi;
2637         sa_info[i].src =*((unsigned int *)(&config.local_ipsec_ip.ipv4[0]));
2638         sa_info[i].dst =*((unsigned int *)(&config.remote_ipsec_ip.ipv4[0]));
2639 #endif
2642         /* dynamic configuration, will  be calculated on the fly */
2643         sa_info[i].tx_payload_info.authSize = 0;
2644         sa_info[i].tx_payload_info.encSize = 0;
2645         //sa_info[i].tx_payload_info.ploadLen = 0;
2646         sa_info[i].tx_payload_info.pEncIV = 0;
2647         sa_info[i].tx_payload_info.pPkt = 0;
2649         sa_info[i].cipherMode = NWAL_SA_EALG_AES_GCM;
2650         sa_info[i].inner_ip_offset = sa_info[i].tx_payload_info.encOffset;
2651         sa_info[i].auth_tag_size = netTest_AES_GCM_CCM_ICV_LEN;  /* icv or mac size,. always 12 except for AES_CCM/AES_GCM */
2653         sa_info[i].tx_pkt_info.enetPort = 0;
2654         sa_info[i].tx_pkt_info.ipOffBytes = sa_info[i].tx_payload_info.encOffset;
2655         sa_info[i].tx_pkt_info.l4HdrLen = netTest_UDP_HEADER_LEN; /*UDP header len */
2656         sa_info[i].tx_pkt_info.l4OffBytes = sa_info[i].inner_ip_offset + netTest_IP_HEADER_LEN;
2657         sa_info[i].tx_pkt_info.startOffset = 0;
2658         sa_info[i].tx_pkt_info.lpbackPass = 0;
2659         sa_info[i].tx_pkt_info.ploadLen = 0; /*sa_info[i].tx_pkt_info.l4OffBytes + 4 */
2660         sa_info[i].tx_pkt_info.pPkt = NULL;
2661         sa_info[i].tx_pkt_info.saOffBytes = netTest_MAC_HEADER_LEN  + netTest_IP_HEADER_LEN;
2662         sa_info[i].tx_pkt_info.saPayloadLen = 0;
2663         sa_info[i].tx_pkt_info.pseudoHdrChecksum =0;
2664        
2665         sa_info[i].tx_pkt_info.txFlag1 = NWAL_TX_FLAG1_DO_IPSEC_ESP_CRYPTO| NWAL_TX_FLAG1_DO_UDP_CHKSUM| NWAL_TX_FLAG1_META_DATA_VALID ;
2667         tmp_spi = htonl((long)(tx_sa[i].spi));
2668          trie_insert(p_trie_sa,(char *)&tmp_spi,4, (void *) &sa_info[i]); //asociate with tx sa SPI
2669     }
2670     else if ((tx_sa[i].authMode == NWAL_SA_AALG_NULL) && (rx_sa[i].cipherMode == NWAL_SA_EALG_AES_CCM))
2671     {
2672         /* static configuration, will not change */
2673         sa_info[i].tx_payload_info.aadSize = 0;
2674         sa_info[i].tx_payload_info.pAad = NULL;
2675         //sa_info[i].tx_payload_info.authIvSize = 0;
2676         sa_info[i].tx_payload_info.pAuthIV = NULL;
2677         sa_info[i].tx_payload_info.authOffset = netTest_MAC_HEADER_LEN  + netTest_IP_HEADER_LEN;        /*done: same for all cipher suites */
2679         sa_info[i].tx_payload_info.encOffset =         netTest_MAC_HEADER_LEN +
2680                                                                      netTest_IP_HEADER_LEN +
2681                                                                     netTest_ESP_HEADER_LEN +
2682                                                                     netTest_AES_CCM_IV_LEN;
2683 #ifdef EXPERIMENTAL
2684         sa_info[i].iv_len=0;
2685         sa_info[i].bl=4;
2686         sa_info[i].spi = tx_sa[i].spi;
2687         sa_info[i].src =*((unsigned int *)(&config.local_ipsec_ip.ipv4[0]));
2688         sa_info[i].dst =*((unsigned int *)(&config.remote_ipsec_ip.ipv4[0]));
2689 #endif
2692         /* dynamic configuration, will  be calculated on the fly */
2693         sa_info[i].tx_payload_info.authSize = 0;
2694         sa_info[i].tx_payload_info.encSize = 0;
2695         //sa_info[i].tx_payload_info.ploadLen = 0;
2696         sa_info[i].tx_payload_info.pEncIV = 0;
2697         sa_info[i].tx_payload_info.pPkt = 0;
2699         sa_info[i].cipherMode = NWAL_SA_EALG_AES_CCM;
2700         sa_info[i].inner_ip_offset = sa_info[i].tx_payload_info.encOffset;
2701         sa_info[i].auth_tag_size = netTest_AES_GCM_CCM_ICV_LEN;  /* icv or mac size,. always 12 except for AES_CCM/AES_GCM */
2703         sa_info[i].tx_pkt_info.enetPort = 0;
2704         sa_info[i].tx_pkt_info.ipOffBytes = sa_info[i].tx_payload_info.encOffset;
2705         sa_info[i].tx_pkt_info.l4HdrLen = netTest_UDP_HEADER_LEN; /*UDP header len */
2706         sa_info[i].tx_pkt_info.l4OffBytes = sa_info[i].inner_ip_offset + netTest_IP_HEADER_LEN;
2707         sa_info[i].tx_pkt_info.startOffset = 0;
2708         sa_info[i].tx_pkt_info.lpbackPass = 0;
2709         sa_info[i].tx_pkt_info.ploadLen = 0; /*sa_info[i].tx_pkt_info.l4OffBytes + 4 */
2710         sa_info[i].tx_pkt_info.pPkt = NULL;
2711         sa_info[i].tx_pkt_info.saOffBytes = netTest_MAC_HEADER_LEN  + netTest_IP_HEADER_LEN;
2712         sa_info[i].tx_pkt_info.saPayloadLen = 0;
2713         sa_info[i].tx_pkt_info.pseudoHdrChecksum =0;
2714        
2715         sa_info[i].tx_pkt_info.txFlag1 = NWAL_TX_FLAG1_DO_IPSEC_ESP_CRYPTO| NWAL_TX_FLAG1_DO_UDP_CHKSUM| NWAL_TX_FLAG1_META_DATA_VALID ;
2716         
2717         tmp_spi = htonl((long)(tx_sa[i].spi));
2718          trie_insert(p_trie_sa,(char *)&tmp_spi,4, (void *) &sa_info[i]); //asociate with tx sa SPI
2719     }
2720     else if ((tx_sa[i].authMode == NWAL_SA_AALG_AES_XCBC) && (rx_sa[i].cipherMode == NWAL_SA_EALG_NULL))
2721     {
2722         /* static configuration, will not change */
2723         sa_info[i].tx_payload_info.aadSize = 0;
2724         sa_info[i].tx_payload_info.pAad = NULL;
2725         sa_info[i].tx_payload_info.pAuthIV = NULL;
2726         sa_info[i].tx_payload_info.authOffset = netTest_MAC_HEADER_LEN  + netTest_IP_HEADER_LEN;
2729         sa_info[i].tx_payload_info.encOffset =         netTest_MAC_HEADER_LEN +
2730                                                                      netTest_IP_HEADER_LEN +
2731                                                                     netTest_ESP_HEADER_LEN +
2732                                                                     netTest_NULL_IV_LEN;
2733 #ifdef EXPERIMENTAL
2734         sa_info[i].iv_len=0;
2735         sa_info[i].bl=4;
2736         sa_info[i].spi = tx_sa[i].spi;
2737         sa_info[i].src =*((unsigned int *)(&config.local_ipsec_ip.ipv4[0]));
2738         sa_info[i].dst =*((unsigned int *)(&config.remote_ipsec_ip.ipv4[0]));
2739 #endif
2742         /* dynamic configuration, will  be calculated on the fly */
2743         sa_info[i].tx_payload_info.authSize = 0;
2744         sa_info[i].tx_payload_info.encSize = 0;
2745         //sa_info[i].tx_payload_info.ploadLen = 0;
2746         sa_info[i].tx_payload_info.pEncIV = 0;
2747         sa_info[i].tx_payload_info.pPkt = 0;
2749         sa_info[i].cipherMode = NWAL_SA_EALG_NULL;
2750         sa_info[i].authMode = NWAL_SA_AALG_AES_XCBC;
2751         sa_info[i].inner_ip_offset = sa_info[i].tx_payload_info.encOffset;
2752         sa_info[i].auth_tag_size = netTest_ICV_LEN;  /* icv or mac size,. always 12 except for AES_CCM/AES_GCM , GMAC*/
2754         sa_info[i].tx_pkt_info.enetPort = 0;
2755         sa_info[i].tx_pkt_info.ipOffBytes = sa_info[i].tx_payload_info.encOffset;
2756         sa_info[i].tx_pkt_info.l4HdrLen = netTest_UDP_HEADER_LEN; /*UDP header len */
2757         sa_info[i].tx_pkt_info.l4OffBytes = sa_info[i].inner_ip_offset + netTest_IP_HEADER_LEN;
2758         sa_info[i].tx_pkt_info.startOffset = 0;
2759         sa_info[i].tx_pkt_info.lpbackPass = 0;
2760         sa_info[i].tx_pkt_info.ploadLen = 0; /*sa_info[i].tx_pkt_info.l4OffBytes + 4 */
2761         sa_info[i].tx_pkt_info.pPkt = NULL;
2762         sa_info[i].tx_pkt_info.saOffBytes = netTest_MAC_HEADER_LEN  + netTest_IP_HEADER_LEN;
2763         sa_info[i].tx_pkt_info.saPayloadLen = 0;
2764         sa_info[i].tx_pkt_info.pseudoHdrChecksum =0;
2765        
2766         sa_info[i].tx_pkt_info.txFlag1 = NWAL_TX_FLAG1_DO_IPSEC_ESP_CRYPTO| NWAL_TX_FLAG1_DO_UDP_CHKSUM| NWAL_TX_FLAG1_META_DATA_VALID ;
2767         
2768         tmp_spi = htonl((long)(tx_sa[i].spi));
2769          trie_insert(p_trie_sa,(char *)&tmp_spi,4, (void *) &sa_info[i]); //asociate with tx sa SPI
2770     }
2771     else
2772         printf("build_sa_db(): invalid encryption/authentication combination selected\n");
2774     //printf("sa_build_db(): authOffset %d, innerIpOffset %d, encOffset %d\n", sa_info[i].tx_payload_info.authOffset, sa_info[i].inner_ip_offset, sa_info[i].tx_payload_info.encOffset);
2775     
2776     
2781 #define CHECK_SET_PARAM(ARG1, ARG2)     \
2782     do { \
2783         if(strcmp(key, ARG1) == 0) { \
2784         if(data)strncpy(ARG2,data,CONFIG_STRING_LEN); \
2785         printf("CHECK_SET_PARM, match found, input cong string %s\n", ARG2); \
2786         continue; \
2787         } \
2788     } while(0)
2790 #define CHECK_SET_PARAM2(ARG1, ARG2,  ARG3)     \
2791     do { \
2792         if(strcmp(key, ARG1) == 0) { \
2793         if(data) strncpy(ARG2,data,CONFIG_STRING_LEN); \
2794         if(data2) strncpy(ARG3,data2,CONFIG_STRING_LEN); \
2795         printf("CHECK_SET_PARM2, match found, input cong string %s %s\n", ARG2, ARG3); \
2796         continue; \
2797         } \
2798     } while(0)
2800 unsigned char hex2dec(char *p_s)
2802   int val;
2803   sscanf(p_s,"%x",&val); 
2804  return val&0xff;
2806 void parse_one_mac(char * p_mac_str, unsigned char *p_mac)
2808     int index = 0;
2809     int i;
2810     char *pch = strtok (&(p_mac_str[0]),"-");
2812     while (pch != NULL)
2813     {
2814          printf ("%s\n",pch);
2815          p_mac[index] = hex2dec(pch);
2816          index++;
2817          pch = strtok (NULL,"-");
2818     }
2819      printf("index value : %d\n", index);
2820     for (i=0; i<6;i++)
2821          printf("************mac0[%d]: 0x%x\n",i,  p_mac[i]);
2824 void parse_one_ip(char * p_ip_addr_str, unsigned char * p_ip)
2826     int index = 0;
2827     int i;
2828     char * pch = strtok (&p_ip_addr_str[0],".");
2830      while (pch != NULL)
2831      {
2832         printf ("xxxxx: %s\n",pch);
2833         p_ip[index] = atoi(pch);
2834         index++;
2835         pch = strtok (NULL,".");
2836     }
2837     printf("index value : %d\n", index);
2838     for (i=0; i<4;i++)
2839         printf("************ip[%d]: 0x%x\n",i, p_ip[i]);
2844 void parse_mac_address(netTestConfigFile_t *pConfig)
2846     if (strlen(&pConfig->mac0[0]))
2847         parse_one_mac(&pConfig->mac0[0],&config.mac0[0]);
2848     if (strlen(&pConfig->mac1[0]))
2849         parse_one_mac(&pConfig->mac1[0],&config.mac1[0]);
2851 void parse_ip_address(netTestConfigFile_t *pConfig)
2853     if (strlen(&pConfig->ip0[0]))
2854         parse_one_ip(&pConfig->ip0[0],&config.ip0.ipv4[0]);
2855     if (strlen(&pConfig->ip1[0]))
2856         parse_one_ip(&pConfig->ip1[0],&config.ip1.ipv4[0]);
2857     if (strlen(&pConfig->ip2[0]))
2858         parse_one_ip(&pConfig->ip2[0],&config.ip2.ipv4[0]);
2859     if (strlen(&pConfig->local_ipsec_ip[0]))
2860         parse_one_ip(&pConfig->local_ipsec_ip[0],&config.local_ipsec_ip.ipv4[0]);
2861     if (strlen(&pConfig->remote_ipsec_ip[0]))
2862     parse_one_ip(&pConfig->remote_ipsec_ip[0],&config.remote_ipsec_ip.ipv4[0]);
2866 void parse_ipsec_mode(netTestConfigFile_t *pConfig)
2869     if (strlen(&pConfig->ipsec_mode_rx[0]))
2870     {
2871         if (strcmp(pConfig->ipsec_mode_rx, "SIDEBAND") == 0)
2872         {
2873             config.ipsec_mode_rx = IPSEC_MODE_RX_SIDEBAND;
2874         }
2875         else if (strcmp(pConfig->ipsec_mode_rx, "INFLOW") == 0)
2876         {
2877             config.ipsec_mode_rx = IPSEC_MODE_RX_INFLOW;
2878         }
2879         else
2880             printf("parse_ipsec_mode(), invalid RX ipsec mode in config file \n");
2881     }
2883     if (strlen(&pConfig->ipsec_mode_tx[0]))
2884     {
2885         if (strcmp(pConfig->ipsec_mode_tx, "SIDEBAND") == 0)
2886         {
2887             config.ipsec_mode_tx = IPSEC_MODE_TX_SIDEBAND;
2888         }
2889         else if (strcmp(pConfig->ipsec_mode_tx, "INFLOW") == 0)
2890         {
2891             config.ipsec_mode_tx = IPSEC_MODE_TX_INFLOW;
2892         }
2893     else
2894         printf("parse_ipsec_mode(), invalid TX ipsec mode in config file \n");
2895     }
2897     printf("parse_ipsec_mode(): RX mode %d\n", config.ipsec_mode_rx);
2898     printf("parse_ipsec_mode(): TX mode %d\n", config.ipsec_mode_tx);
2900     
2903 #ifdef EXPERIMENTAL
2904 int n_routes=0;
2905 int n_dst_ips=0;
2906 void parse_routes(netTestConfigFile_t *pConfig)
2908 int i;
2909 int said=0;
2910 for(i=0;i<MAX_ROUTES;i++)
2912    int port;
2913    if (pConfig->routes[i][0])
2914    {
2915       port=atoi(&pConfig->ports[i][0]);
2916       if((port<1)||(port>2)) continue; //bad port #: only 1 or 2 valid
2917       if(strncmp(&pConfig->routes[i][0],"MAC",3)==0)
2918       {
2919          routes[i].out_port = port;
2920          parse_one_mac(&pConfig->routes[i][3],&routes[i].out_mac[0]);
2921          memcpy(&routes[i].out_mac[6], ((port==1) ?&config.mac0[0]: &config.mac1[0] ),6); 
2922          routes[i].out_mac[12]=0x08;
2923          routes[i].out_mac[13]=0x00;
2924          routes[i].sec_ptr=NULL;
2925          n_routes+=1;
2926       }
2927       else if (strncmp(&pConfig->routes[i][0],"SA",2)==0)
2928       {      
2929          said=atoi(&pConfig->routes[i][2]) ;
2930          routes[i].sec_ptr=&sa_info[said];
2931          n_routes+=1;
2932       }
2933    }
2935 our_router = route_init();
2936 for (i=0;i<MAX_ROUTES;i++)
2938   unsigned long ip_be;
2939   int route_index;
2940   if (pConfig->dst_ips[i][0])
2941   {
2942      parse_one_ip(&pConfig->dst_ips[i][0],(unsigned char *)&ip_be);
2943      sscanf(&pConfig->paths[i][0],"route%d",&route_index);
2944      route_add(our_router,&ip_be,&routes[route_index]);
2945      n_dst_ips+=1;
2946   }
2948 printf(">Route DB built.  %d entries\n",n_dst_ips);
2950 #endif
2951 static void parse_config_file(FILE * fpr, netTestConfigFile_t *pConfig)
2953     char line[MAX_LINE_LENGTH + 1];
2954     int i;
2955  
2956     char *key, *data, *ep, *data2;
2957     char tokens[] = " :=;\n";
2958     char temp_str[50];
2959     memset(line, 0, MAX_LINE_LENGTH + 1);
2960     memset(pConfig, 0, sizeof(netTestConfigFile_t));
2961     while (fgets(line, MAX_LINE_LENGTH + 1, fpr)) 
2962     {
2963         if(line[0]=='#') continue; //skip comment
2964         key  = (char *)strtok(line, tokens);
2965         data = (char *)strtok(NULL, tokens);
2966         data2 = (char *)strtok(NULL, tokens);
2967         if (!key) continue;
2968         if (!data) continue;
2970         if(strlen(data) == 0) 
2971         {
2972             continue;
2973         }
2975          CHECK_SET_PARAM(INIT_CONFIG_MAC0,&(pConfig->mac0[0]));
2976          CHECK_SET_PARAM(INIT_CONFIG_MAC1,&(pConfig->mac1[0]));
2977          CHECK_SET_PARAM(INIT_CONFIG_IP0,&(pConfig->ip0[0]));
2978          CHECK_SET_PARAM(INIT_CONFIG_IP1,&(pConfig->ip1[0]));
2979          CHECK_SET_PARAM(INIT_CONFIG_IP2,&(pConfig->ip2[0]));
2980          CHECK_SET_PARAM(INIT_CONFIG_LOCAL_IPSEC_IP,&(pConfig->local_ipsec_ip[0]));
2981          CHECK_SET_PARAM(INIT_CONFIG_REMOTE_IPSEC_IP,&(pConfig->remote_ipsec_ip[0]));
2982          CHECK_SET_PARAM(INIT_CONFIG_IPSEC_MODE_RX,&(pConfig->ipsec_mode_rx[0]));
2983          CHECK_SET_PARAM(INIT_CONFIG_IPSEC_MODE_TX,&(pConfig->ipsec_mode_tx[0]));
2984          CHECK_SET_PARAM(INIT_CONFIG_IPSEC_IF_NO,&(pConfig->ipsec_if_no[0]));
2985 #ifdef EXPERIMENTAL
2986     for(i=0;i<MAX_ROUTES;i++)
2987     {
2988         sprintf(temp_str,"route%d",i);
2989         CHECK_SET_PARAM2(temp_str,&pConfig->routes[i][0],&pConfig->ports[i][0] );
2990     }
2991     for(i=0;i<MAX_ROUTES;i++)
2992     {
2993         sprintf(temp_str,"dstip%d",i);
2994         CHECK_SET_PARAM2(temp_str,&pConfig->dst_ips[i][0],&pConfig->paths[i][0] );
2995     }
2996 #endif
2997          
2998     }
2999     parse_mac_address(pConfig);
3001     parse_ip_address(pConfig);
3003     parse_ipsec_mode(pConfig);
3004 #ifdef EXPERIMENTAL
3005      if (strlen(&pConfig->ipsec_if_no[0]))
3006         config.ipsec_if_no = atoi(&pConfig->ipsec_if_no[0]);
3007     parse_routes(pConfig);
3008 #endif
3012 #ifdef MULTI_THREAD
3013 NETAPI_T worker_nh[TUNE_NETAPI_NUM_CORES];
3014 void slow_path_thread(int coreid)
3016     int err;
3018     PKTIO_HANDLE_T *our_chan;
3019     PKTIO_HANDLE_T *rx_chan;
3020     PKTIO_HANDLE_T *tx_chan;
3021     PKTIO_HANDLE_T *sb_tx_chan;
3022     PKTIO_HANDLE_T *sb_rx_chan;
3024     
3026     printf("slow_path_thread, mypid: %d, core_id %d\n", gettid(), coreid);
3028     CPU_ZERO( &cpu_set);
3029 #ifdef CORTEX_A15
3030     CPU_SET( coreid, &cpu_set);
3031     hplib_utilSetupCore(coreid, &cpu_set);
3032 #else
3033       CPU_SET( 0, &cpu_set);
3034     hplib_utilSetupCore(coreid+1, &cpu_set);
3035 #endif
3036     worker_nh[coreid]=netapi_init(NETAPI_CORE_MASTER,NULL);
3038     NETAPI_HANDLE_T * n = (NETAPI_HANDLE_T *) worker_nh[coreid];
3040    /* open netcp default tx, rx queues */
3041     tx_chan = pktio_open(worker_nh[coreid], NETCP_TX, NULL, &netcp_tx_cfg,  &err);
3042     
3043     
3044 #ifdef EXPERIMENTAL
3045     rx_chan = pktio_open(worker_nh[coreid], NETCP_RX, (PKTIO_CB) recv_cb_router, &netcp_rx_cfg,  &err);
3046 #else
3047     rx_chan = pktio_open(worker_nh[coreid], NETCP_RX, (PKTIO_CB) recv_cb, &netcp_rx_cfg,  &err);
3048 #endif
3049     
3050 #ifndef EXPERIMENTAL
3051     /* create a pktio channel for specially classified pkts */
3052     /* open netcp default tx, rx queues for sideband crypto */
3053      sb_tx_chan = pktio_open(worker_nh[coreid], NETCP_SB_TX, NULL, &netcp_sb_tx_cfg,  &err);
3054     
3055     sb_rx_chan = pktio_open(worker_nh[coreid], NETCP_SB_RX, (PKTIO_CB) recv_sb_cb, &netcp_sb_rx_cfg,  &err);
3056 #endif
3061     netapi_setCookie(worker_nh[coreid],coreid);
3062     //our_core = coreid;
3063       
3064     scheduler[coreid] =netapi_schedOpen(worker_nh[coreid],&our_sched_cfg, &err);
3065     if (!scheduler[coreid]) 
3066     {
3067         printf("sched create failed for core%d\n",coreid); 
3068         exit(1);
3069     }
3070       scheduler[coreid]->config.yield = FALSE;
3071        scheduler[coreid]->config.pollGarbageQ = TRUE;
3072         scheduler[coreid]->config.pollCtrlQ = TRUE;
3073             /*********************************************/
3074     /**************Entry point into scheduler ****/
3075     /*********************************************/
3076     netapi_schedWaitForEvents(scheduler[coreid], &err);
3077     printf(">net_test: core %d worker thread done\n",coreid);
3078     pktio_close(tx_chan, &err);
3079     pktio_close(rx_chan, &err);
3080     pktio_close(sb_tx_chan, &err);
3081     pktio_close(sb_tx_chan, &err);
3082     netapi_shutdown(worker_nh[coreid]);
3085 void fast_path_thread(int coreid)
3087     int err;
3088         PKTIO_HANDLE_T *our_chan;
3089     PKTIO_HANDLE_T *rx_chan;
3090     PKTIO_HANDLE_T *tx_chan;
3091     PKTIO_HANDLE_T *sb_tx_chan;
3092     PKTIO_HANDLE_T *sb_rx_chan;
3094 #ifdef CORTEX_A15
3095     CPU_SET( coreid, &cpu_set);
3096         hplib_utilSetupCore(coreid, &cpu_set);
3097 #else
3098       CPU_SET( 0, &cpu_set);
3099     hplib_utilSetupCore(coreid + 1, &cpu_set);
3100 #endif
3101     worker_nh[coreid]=netapi_init(NETAPI_CORE_MASTER,NULL);
3103     NETAPI_HANDLE_T * n = (NETAPI_HANDLE_T *) worker_nh[coreid];
3105    /* open netcp default tx, rx queues */
3106     tx_chan = pktio_open(worker_nh[coreid], NETCP_TX, NULL, &netcp_tx_cfg,  &err);
3107     
3108     
3109 #ifdef EXPERIMENTAL
3110     rx_chan = pktio_open(worker_nh[coreid], NETCP_RX, (PKTIO_CB) recv_cb_router, &netcp_rx_cfg,  &err);
3111 #else
3112     rx_chan = pktio_open(worker_nh[coreid], NETCP_RX, (PKTIO_CB) recv_cb, &netcp_rx_cfg,  &err);
3113 #endif
3114     
3115 #ifndef EXPERIMENTAL
3116     /* create a pktio channel for specially classified pkts */
3117     /* open netcp default tx, rx queues for sideband crypto */
3118      sb_tx_chan = pktio_open(worker_nh[coreid], NETCP_SB_TX, NULL, &netcp_sb_tx_cfg,  &err);
3119     
3120     sb_rx_chan = pktio_open(worker_nh[coreid], NETCP_SB_RX, (PKTIO_CB) recv_sb_cb, &netcp_sb_rx_cfg,  &err);
3121 #endif
3124     
3125     netapi_setCookie(worker_nh[coreid],coreid);
3126     //our_core = coreid;
3127     scheduler[coreid] =netapi_schedOpen(worker_nh[coreid],&our_sched_cfg, &err);
3128     if (!scheduler[coreid]) 
3129         {
3130         printf("sched create failed for core%d\n",coreid); 
3131         exit(1);
3132     }
3134     /*********************************************/
3135     /**************Entry point into scheduler ****/
3136     /*********************************************/
3137     scheduler[coreid]->config.yield = FALSE;
3138      scheduler[coreid]->config.pollGarbageQ = FALSE;
3139      scheduler[coreid]->config.pollCtrlQ = FALSE;
3140      //sleep(100000);
3141     netapi_schedWaitForEvents(scheduler[coreid], &err);
3142     printf(">net_test: core %d worker thread done\n",coreid);
3143     pktio_close(tx_chan, &err);
3144     pktio_close(rx_chan, &err);
3145     pktio_close(sb_tx_chan, &err);
3146     pktio_close(sb_tx_chan, &err);
3147     netapi_shutdown(worker_nh[coreid]);
3150 #endif
3151 static  netTestConfigFile_t config_file;
3152 /***************************************
3153  ********** test driver*****************
3154  ***************************************/
3155 int main(int argc, char **argv)
3157     int err,i;
3158     Pktlib_HeapCfg      heapCfg;
3159     rlim_t oss,ss = 1024*1024;
3160     struct rlimit rl;
3161     int32_t             errCode;
3162     Pktlib_HeapIfTable*  pPktifTable;
3163     
3165     FILE * fpr = NULL;
3167     nwalSaIpSecId_t  nwalSaIpSecId;
3169     err= getrlimit(RLIMIT_STACK,&rl);
3170     if (!err) printf(" stack limit = %d\n",rl.rlim_cur); else printf("getrlimit failed\n");
3172     if (argc>=2)  np2process = atoi(argv[1]);
3173         printf("*************** np2process %d\n", np2process);
3174     if (np2process<0) np2process = NP; /* default */
3175     if (argc==3)  perslow = atoi(argv[2]);
3176     if ((perslow<0)||(perslow>100)) perslow=PERSLOW;//default
3177     if (argc>3) {printf("net_test  <no of pkts to process> <percent slow path>\n"); exit(1);}
3179      /* install signal handler for ^c */
3180     signal(SIGINT,mysig);
3182     fpr = fopen(input_file_name, "r");
3183     if (fpr == NULL) 
3184     {
3185         printf("Error in opening %s input file\n", input_file_name);
3186         exit(1);
3187     }
3188     else
3189     {
3190         parse_config_file(fpr,&config_file);
3191     }
3193     memset(&sa_info, 0, sizeof(sa_info));
3196     //real mode, so update our test packet mac header and ip header
3197     if (pktloopback==0)
3198     {
3199         memcpy(&testPkt,&real_mac_header[0],14); //overwrite test pkt mac address
3200         memcpy(&testPkt[26],&real_ip_addr[0],8);//overrite test pkt ip addresses
3201     }
3203 #ifdef MULTI_THREAD
3204     /* assign main net_test thread to run on core 0 */
3205     CPU_ZERO( &cpu_set);
3206     CPU_SET( 0, &cpu_set);
3207     hplib_utilSetupCore(0, &cpu_set);
3208 #endif
3209     /* create netapi */
3210     netapi_handle = netapi_init(NETAPI_SYS_MASTER, &our_netapi_default_cfg);
3212     netcp_cfgExceptions(netapi_handle, NETCP_CFG_ALL_EXCEPTIONS, NETCP_CFG_ACTION_DISCARD, (NETCP_CFG_ROUTE_HANDLE_T) NULL);
3213     expPkt_appid = netcp_cfgExceptions(netapi_handle, 7, NETCP_CFG_ACTION_TO_SW, (NETCP_CFG_ROUTE_HANDLE_T) NULL);
3215     /* open the main heap */
3216     ourHeap = Pktlib_findHeapByName("netapi");
3217     if (!ourHeap)
3218     {
3219         printf("Pktlib_findHeapByName()  fail\n");
3220         exit(1);
3221     }
3223     /* create two secondary heaps */
3224     /* Initialize the heap configuration. */
3225     memset ((void *)&heapCfg, 0, sizeof(Pktlib_HeapCfg));
3227     pPktifTable = netapi_getPktlibIfTable();
3228     /* Populate the heap configuration */
3229     heapCfg.name                = "netapi-small";
3230     heapCfg.memRegion           = NETAPI_GLOBAL_REGION;
3231     heapCfg.sharedHeap          = 1;
3232     heapCfg.useStarvationQueue  = 0;
3233     heapCfg.dataBufferSize      = 512;
3234     heapCfg.numPkts             = 64;
3235     heapCfg.numZeroBufferPackets= 0;
3236     heapCfg.heapInterfaceTable.data_malloc  = pPktifTable->data_malloc;
3237     heapCfg.heapInterfaceTable.data_free    = pPktifTable->data_free;
3238     heapCfg.dataBufferPktThreshold   = 0;
3239     heapCfg.zeroBufferPktThreshold   = 0;
3241     specialSmall = Pktlib_createHeap(&heapCfg, &errCode);
3242     heapCfg.name                = "netapi-big";
3243     heapCfg.dataBufferSize      = 1600;
3244     specialLarge = Pktlib_createHeap(&heapCfg, &errCode);
3245     //register these heaps so poll routine will include their garbage queues.
3246     netapi_registerHeap(netapi_handle, specialSmall);
3247     netapi_registerHeap(netapi_handle, specialLarge);
3249 #ifdef REASSEMBLE_BENCH
3250     our_reassemble_bench(2);
3251 exit(1);
3252 #endif
3254 #ifndef EXPERIMENTAL
3255     /* create a pktio channel */
3256     our_chan=pktio_create(netapi_handle,"our1stq",(PKTIO_CB) recv_cb_bench, &our_chan_cfg,&err);
3257     if (!our_chan)
3258     {
3259         printf("pktio create failed err=%d\n",err);
3260         exit(1);
3261     }
3262 #endif
3264     /* open netcp default tx, rx queues */
3265     netcp_tx_chan= pktio_open(netapi_handle, NETCP_TX, NULL, &netcp_tx_cfg,  &err);
3266     if (!netcp_tx_chan)
3267     {
3268         printf("pktio open TX failed err=%d\n",err);
3269         exit(1);
3270     }
3271 #ifdef EXPERIMENTAL
3272     netcp_rx_chan= pktio_open(netapi_handle, NETCP_RX, (PKTIO_CB) recv_cb_router, &netcp_rx_cfg,  &err);
3273 #else
3274     netcp_rx_chan= pktio_open(netapi_handle, NETCP_RX, (PKTIO_CB) recv_cb, &netcp_rx_cfg,  &err);
3275 #endif
3276     if (!netcp_rx_chan)
3277     {
3278         printf("pktio open RX failed err=%d\n",err);
3279         exit(1);
3280     }
3282 #ifndef EXPERIMENTAL
3283     /* create a pktio channel for specially classified pkts */
3284     netcp_rx_chan2= pktio_create(netapi_handle, "classq", (PKTIO_CB) recv_cb, &netcp_rx_cfg2,  &err);
3285     if (!netcp_rx_chan2) {printf("pktio create RX2 failed err=%d\n",err); exit(1);}
3287     /* open netcp default tx, rx queues for sideband crypto */
3288     netcp_sb_tx_chan= pktio_open(netapi_handle, NETCP_SB_TX, NULL, &netcp_sb_tx_cfg,  &err);
3289     if (!netcp_sb_tx_chan)
3290     {
3291         printf("pktio open SB TX failed err=%d\n",err);
3292         exit(1);
3293     }
3294     netcp_sb_rx_chan= pktio_open(netapi_handle, NETCP_SB_RX, (PKTIO_CB) recv_sb_cb, &netcp_sb_rx_cfg,  &err);
3295     if (!netcp_sb_rx_chan)
3296     {
3297         printf("pktio open SB RX failed err=%d\n",err);
3298         exit(1);
3299     }
3300 #endif
3302     printf("net_test> %d bytes left in our CMA area\n", netapi_getBufMemRemainder());
3303     /* create scheduler instance */
3304     our_sched =netapi_schedOpen(netapi_handle,&our_sched_cfg, &err);
3305     if (!our_sched) {printf("sched create failed\n"); exit(1);}
3307 #if 0
3308     /********************************************
3309     * Basic pkt loopback test
3310     *********************************************/
3311     printf("...running pure push/pop benchmark\n");
3312     our_pktio_bench(1000);
3313     our_pktio_bench(1000);
3314     our_pktio_bench(1000);
3315     our_pktio_bench(1000);
3316     our_pktio_bench(1000);
3317     /*********************************************/
3318 #endif
3319     //#define PROMISCUOUS
3320 #ifdef PROMISCUOUS
3321 #define ETH0 1
3322 #define ETH1 2
3323     printf(">net_test: WARNING, ADDING RULE FOR PROMISCUOUS THAT WILL BREAK LINUX STACK\n");
3324     netcp_cfgCreateMacInterface(
3325                       netapi_handle,
3326                       &all_mac[0],
3327                       0,0,
3328                       (NETCP_CFG_ROUTE_HANDLE_T)  NULL,
3329                       (NETCP_CFG_VLAN_T ) NULL ,  //future
3330                       1,
3331                       &err);
3332     if (err) {printf("addmac0 failed %d\n",err); exit(1); }
3334 #else
3335 #define ETH0 0
3336 #define ETH1 1
3337 #endif
3339     /* add mac intefaces */
3340     netcp_cfgCreateMacInterface(
3341                       netapi_handle,
3342                       &config.mac0[0],
3343                       ETH0,0,
3344                       (NETCP_CFG_ROUTE_HANDLE_T)  NULL,
3345                       (NETCP_CFG_VLAN_T ) NULL ,  //future
3346                       1, 
3347                       &err);
3348     if (err) {printf("addmac0 failed %d\n",err); exit(1); } 
3350     //attach an IP to this interface
3351     ip_rule0=netcp_cfgAddIp(
3352                       netapi_handle,
3353                       ETH0,
3354                       nwal_IPV4,
3355                       &config.ip0,
3356                       NULL,  //all IP
3357                       (NETCP_CFG_ROUTE_HANDLE_T) NULL,
3358                       &err
3359                       );
3360     if (err) {printf("addip0 failed %d\n",err); exit(1); } 
3362     //create a 2nd mac instance
3363     netcp_cfgCreateMacInterface(
3364                       netapi_handle,
3365                       &config.mac1[0],
3366                       ETH1,1,
3367                       (NETCP_CFG_ROUTE_HANDLE_T)  NULL,
3368                       (NETCP_CFG_VLAN_T ) NULL ,  //future
3369                       1,
3370                       &err);
3371     if (err) {printf("addmac1 failed %d\n",err); exit(1); }
3373     //attach an IP to this interface
3374     ip_rule1=netcp_cfgAddIp(
3375                       netapi_handle,
3376                       ETH1,
3377                       nwal_IPV4,
3378                       &config.ip1,
3379                       NULL,  //all IP
3380                       (NETCP_CFG_ROUTE_HANDLE_T) NULL,
3381                       &err
3382                       );
3383     if (err) {printf("addip1 failed %d\n",err); exit(1); }
3385 #ifndef EXPERIMENTAL
3386     //attach 2 classifiers to iface 0, ip0
3387     class_0_cfg.u.c_l4.ip = ip_rule0;
3388     class_0 =  netcp_cfgAddClass(netapi_handle,
3389                                  &class_0_cfg,
3390                                  NULL,
3391                                  NETCP_CFG_ACTION_TO_SW,
3392                                 &err);
3393     if (err) {printf("addclass0 failed %d\n",err); exit(1);}
3395     class_1_cfg.u.c_l4.ip = ip_rule0;
3396     class_1 =  netcp_cfgAddClass(netapi_handle,
3397                                  &class_1_cfg,
3398                                  NULL,
3399                                  NETCP_CFG_ACTION_TO_SW,
3400                                  &err);
3401     if (err) {printf("addclass1 failed %d\n",err); exit(1);}
3404     //3rd classifier has a different IP and route
3405     class_2_cfg.u.c_l3_l4.ip_addr = &config.ip2;
3407     //create specialFlow for this classifier
3408     {
3409     Pktlib_HeapHandle heaps[2];
3410     int sizes[2];
3411     heaps[0]= specialSmall;
3412     heaps[1]= specialLarge;
3413 #define SPECIAL_SOP_OFF 128
3414     sizes[0]=512-SPECIAL_SOP_OFF;
3415     sizes[1]=1600-SPECIAL_SOP_OFF;
3416 #if 0
3417     specialFlow = netcp_cfgAddFlow( netapi_handle,
3418                                 2,
3419                                     heaps,
3420                                     sizes,
3421                                     SPECIAL_SOP_OFF,  //offset to start rx is 128 
3422                                     &err);
3423     if (err) {printf("add flow failed\n", err); exit(1);}
3424 #endif
3425     }
3426 #if 0
3427 //special route for this classifier:  different flow + destination q
3428 class2_route.p_dest_q = netcp_rx_chan2;
3429 class2_route.p_flow = specialFlow;
3430 class_2 = netcp_cfgAddClass(netapi_handle,
3431                             &class_2_cfg,
3432                             (void*) &class2_route,
3433                             NETCP_CFG_ACTION_TO_SW,
3434                             &err);
3435 if (err) {printf("addclass2 failed %d\n",err); exit(1);}
3436 #endif
3438 #endif
3439 //security stuff 
3440     p_trie_sa = trie_new();
3441    if (!p_trie_sa) {printf("trie alloc for SA  failed\n"); exit(1);}
3445     for (i=0; i < MAX_SEC_INDEX;i++)
3446     {
3447         ourRXKeyParams[i].pEncKey = &ourEncrKey[0];
3448         ourRXKeyParams[i].pAuthKey = &ourAuthKey[0];
3449         memcpy(&(rx_sa[i].src), &config.remote_ipsec_ip,4);
3450         memcpy(&(rx_sa[i].dst), &config.local_ipsec_ip,4);
3452         build_sa_db(i);
3453         sa_info[i].rx_tunnel = netapi_secAddSA(
3454             netapi_handle,
3455             config.ipsec_if_no, //iface #0 
3456             &rx_sa[i],
3457             &ourRXKeyParams[i],
3458             config.ipsec_mode_rx == IPSEC_MODE_RX_SIDEBAND ? NETAPI_SEC_SA_SIDEBAND: NETAPI_SEC_SA_INFLOW,
3459                 NULL,  //use default route 
3460             &(sa_info[i].rx_data_mode_handle),
3461             &(sa_info[i].rx_inflow_mode_handle),
3462             &err);
3463         if (err) {printf("addRxSa failed %d\n",err); exit(1);}
3465         if (config.ipsec_mode_rx == IPSEC_MODE_RX_INFLOW)
3466         {
3467             //assume inner and outer ip is the same
3468             rx_policy[i]= netapi_secAddRxPolicy( netapi_handle,
3469                              sa_info[i].rx_tunnel,  //link to tunnel above
3470                              4,         //ipv4
3471                              &config.remote_ipsec_ip, //src -> them
3472                              &config.local_ipsec_ip,  //dst -> us
3473                             NULL,  // no qualifiers
3474                             NULL,  //default route
3475                              &err);
3476             if (err) {printf("addSaPolicy failed  %d, for index %d\n",err,i); exit(1);}
3477         }
3478         else 
3479         {
3480             rx_policy[i] = 0;
3481              
3482         }
3483 #ifdef NET_TEST_ENABLE_SIDE_BAND_LOW_LEVEL_API
3484     /* Remember the NetCP Command Label so save cycles per Crypto 
3485      * operation
3486      * 
3487      */
3488     if(config.ipsec_mode_rx == IPSEC_MODE_RX_SIDEBAND)
3489     {
3490             nwalRetVal =  
3491             nwal_initDMPSCmdInfo(PKTIO_GET_NWAL_INSTANCE(netcp_sb_rx_chan),
3492                                  sa_info[i].rx_data_mode_handle,
3493                                  &sa_info[i].rx_dmPSCmdInfo);
3494     }
3496 #endif
3497     }
3499 //tx SA
3500 //security stuff 
3501      for (i=0; i < MAX_SEC_INDEX;i++)
3502     {
3503     ourTXKeyParams[i].pEncKey = &ourEncrKey[0];
3504     ourTXKeyParams[i].pAuthKey = &ourAuthKey[0];
3505     memcpy(&(tx_sa[i].src), &config.local_ipsec_ip,4);
3506     memcpy(&(tx_sa[i].dst), &config.remote_ipsec_ip,4);
3507     sa_info[i].tx_tunnel = netapi_secAddSA( netapi_handle,
3508                  0, //iface #0 
3509                 &(tx_sa[i]),
3510                 &ourTXKeyParams[i],
3511                 config.ipsec_mode_tx == IPSEC_MODE_TX_SIDEBAND ? NETAPI_SEC_SA_SIDEBAND: NETAPI_SEC_SA_INFLOW,
3512                 NULL,  //use default route 
3513                 &(sa_info[i].tx_data_mode_handle),
3514                 &(sa_info[i].tx_inflow_mode_handle),
3515                 &err);
3516 if (err) {printf("addTxSa failed %d\n",err); exit(1);}
3518 #ifdef NET_TEST_ENABLE_SIDE_BAND_LOW_LEVEL_API
3520     /* Remember the NetCP Command Label so save cycles per Crypto 
3521      * operation
3522      * 
3523      */
3524     if(config.ipsec_mode_tx == IPSEC_MODE_TX_SIDEBAND)
3525     {
3526             nwalRetVal =  
3527             nwal_initDMPSCmdInfo(PKTIO_GET_NWAL_INSTANCE(netcp_sb_tx_chan),
3528                                  sa_info[i].tx_data_mode_handle,
3529                                  &sa_info[i].tx_dmPSCmdInfo);
3530     }
3531 #endif
3533 #ifdef NET_TEST_ENABLE_INFLOW_LOW_LEVEL_API
3534     if(config.ipsec_mode_tx == IPSEC_MODE_TX_INFLOW)
3535     {
3536         memset(&nwalSaIpSecId, 0, sizeof(nwalSaIpSecId_t));
3537         nwalSaIpSecId.spi = tx_sa[i].spi;
3538         memcpy(&(nwalSaIpSecId.src), &config.local_ipsec_ip,sizeof( nwalIpAddr_t));
3539         memcpy(&(nwalSaIpSecId.dst), &config.remote_ipsec_ip,sizeof( nwalIpAddr_t));
3540         nwalSaIpSecId.proto= tx_sa[i].proto;
3542         nwalRetVal = nwal_initPSCmdInfo(PKTIO_GET_NWAL_INSTANCE(netcp_tx_chan), 
3543                                                             &sa_info[i].tx_pkt_info,
3544                                                             &sa_info[i].tx_psCmdInfo);
3546         if (nwalRetVal == nwal_OK)
3547         {
3548              if (nwal_getSecAssoc(PKTIO_GET_NWAL_INSTANCE(netcp_tx_chan),
3549                                 &nwalSaIpSecId, 
3550                                 NWAL_SA_DIR_OUTBOUND,
3551                                 &(sa_info[i].tx_inflow_mode_handle),
3552                                 &sa_info[i].swInfo0,
3553                                &sa_info[i].swInfo1) != nwal_TRUE)
3554             {
3555                 printf("main: nwal_getSecAssoc failed\n");
3556                 exit (1);
3557             }
3558        }
3559         else
3560         {
3561                 printf("main: nwal_initPSCmdInfo failed\n");
3562                 exit (1);
3563          }
3564     }
3565 #endif
3566     }
3568 #ifdef TEST_TIMERS
3569 //timers
3570 ourTimerBlock = hplib_Timer_CreateGroup(
3571         netapi_handle,
3572         "our1sttimer",
3573         our_timer_cb,
3574         0,    //1 if timers local to thread
3575         0,    //1 if expect to cancel
3576         hplib_getTicksPerSec()/1000,  /* 1 msc resolution for these timers */
3577         hplib_getTicksPerSec()/5000, /* would like .5 msc tolerence */
3578         10);  //small # of timers to test garbage collection
3579 if (err) {printf("hplib_Timer_CreateGroup() failed %d\n",err); exit(1);}
3581 //start a couple of timers 
3582 t1 = hplib_Timer_Start(
3583         ourTimerBlock,
3584         (void *) 1,
3585         100LL,  //timer group ticks
3586         &err);
3587 if (err) {printf("timerstart failed %d\n");}
3588 t2 = hplib_Timer_Start(
3589         ourTimerBlock,
3590         (void *) 2,
3591         200LL,  //timer group ticks
3592         &err);
3593 if (err) {printf("timerstart failed %d\n");}
3594 t3 = hplib_Timer_Start(
3595         ourTimerBlock,
3596         (void *) 3,
3597         300LL,  //timer group ticks
3598         &err);
3599 if (err) {printf("timerstart failed %d\n");}
3600 #endif
3601 #if 0
3602 netcp_cfgReqStats(netapi_handle, our_stats_cb, 1,&err);
3603 if (err!=0) {printf("stats req failed\n");}
3604 #endif
3605 /*********************************************/
3606 /*****************end NETAPI STARTUP**********/
3607 /*********************************************/
3610 #if 0
3611 /********************************************
3612 * Basic pkt loopback test
3613 *********************************************/
3614 printf("...runnining pure push/pop benchmark\n");
3615 our_pktio_bench(100);
3616 #endif
3618 /**************unused stuff******************/
3619 #if 0
3620 /* create TRIE */
3621 P_trie = trie_new();
3622 if (!P_trie) {printf("trie alloc failed\n"); exit(1);}
3624 nat = (HEAD_T *) malloc(NE * sizeof(HEAD_T));
3625 if (!nat) {printf("malloc of nat table failed\n"); exit(1);}
3627 //gen_pkts(np2process<NP ? np2process:NP);
3628 n_pkt= np2process;
3630 /* build table */
3631 build_table(P_trie);
3634 /* processing loop: get pkt, check it, look up in table, copy new header,
3635    send packet */
3636 srand((unsigned) np2process);
3637 #endif
3639 #ifdef MULTI_THREAD
3641         CPU_ZERO( &cpu_set );
3642     pthread_t *thrs;
3643         int procs =2;  //get this from config eventually
3644         char c;
3645         thrs = malloc( sizeof( pthread_t ) * procs );
3646         if (thrs == NULL)
3647         {
3648                 perror( "malloc" );
3649                 return -1;
3650         }
3651         printf( "Starting %d threads...\n", procs );
3653         if (pthread_create( &thrs[0], NULL, slow_path_thread,
3654                         (void *)0 ))
3655         {
3656                         perror( "pthread_create" );
3657                         exit(1);
3658         }
3659 #if 1
3660         if (pthread_create( &thrs[1], NULL, fast_path_thread,
3661                         (void *)1 ))
3662         {
3663                         perror( "pthread_create" );
3664                         exit(1);
3665         }
3666 #endif
3667         //this thread of execution (main) now just waits on user input
3668         for(;;)
3669         {
3670            printf(">");
3671            c=getchar();
3672            if (c=='q') {QUIT=1;break;}
3673            else if (c=='s') our_stats_cb(netapi_handle, &netcp_stats);
3674            else if (c=='h') printf("'q' to quit,  's' for stats, 'h' for help\n");
3675         }
3677         //wait for completion 
3678         printf("main task now pending on slow/fast path completion\n");
3679         for (i = 0; i < procs; i++)
3680                 pthread_join( thrs[i], NULL );
3682         free( thrs );
3684 #else
3685     /*********************************************/
3686     /**************Entry point into scheduler ****/
3687     /*********************************************/
3688     netapi_schedWaitForEvents(our_sched, &err);
3690 #endif
3692 /* done */
3693 our_stats_cb(netapi_handle, NULL);
3696 #define DO_FAST_POLL
3697 #ifdef DO_FAST_POLL
3698 //example_fast_pushpop(ourHeap, 500);
3699 //example_fast_poll(netcp_rx_chan,20000);
3700 #endif
3702 /*************************************************
3703  ************CLEAN UP****************************
3704  ************************************************/
3706 #ifndef EXPERIMENTAL
3707 //delete Classifiers
3708 netcp_cfgDelClass(netapi_handle, class_0, &err); 
3709 netcp_cfgDelClass(netapi_handle, class_1, &err); 
3710 //netcp_cfgDelClass(netapi_handle, class_2, &err); 
3712 #if 0
3713 //delete flow 
3714 netcp_cfgDelFlow(netapi_handle, specialFlow, &err);
3715 #endif
3716 #endif
3717 #if 1
3718 //delete policy
3719      for (i=0; i < MAX_SEC_INDEX;i++)
3720     {
3721         if (rx_policy[i])
3722             netapi_secDelRxPolicy(netapi_handle, rx_policy[i], &err);
3723      
3724         //delete tunnels
3725         netapi_secDelSA(netapi_handle, 0, sa_info[i].rx_tunnel, &err);
3726         netapi_secDelSA(netapi_handle, 0, sa_info[i].tx_tunnel, &err);
3727 #endif
3728     }
3729 //delete IPs and MAC Interfacess
3730 netcp_cfgDelIp(netapi_handle, 0, 0, NULL, NULL, ip_rule0, &err);
3731 netcp_cfgDelIp(netapi_handle, 1, 0, NULL, NULL, ip_rule1, &err);
3732 netcp_cfgDelMac(netapi_handle,0,&err);
3733 netcp_cfgDelMac(netapi_handle,1,&err);
3734 #ifdef PROMISCUOUS
3735 netcp_cfgDelMac(netapi_handle,2,&err);
3736 #endif
3738 //close pktio channels we opened
3739 pktio_close(netcp_tx_chan ,&err);
3740 pktio_close(netcp_rx_chan ,&err);
3741 #ifndef EXPERIMENTAL
3742 pktio_close(netcp_sb_tx_chan ,&err);
3743 pktio_close(netcp_sb_rx_chan ,&err);
3745 //clear pktio channel we created
3746 pktio_control(our_chan, (PKTIO_CB) NULL, (PKTIO_CFG_T *) NULL, &zap_channel_control, &err);
3747 pktio_control(netcp_rx_chan2, (PKTIO_CB) NULL, (PKTIO_CFG_T *) NULL, &zap_channel_control, &err);
3749 //delete pktio channels we created
3750 pktio_delete(our_chan, &err);
3751 pktio_delete(netcp_rx_chan2,&err);
3752 #endif
3753 netapi_shutdown(netapi_handle);
3757 //EXAMPLE FAST POLL
3758 /* PLD */
3759 void netapi_pld(void * x)
3760 {   
3761            asm volatile("pld [r0]");
3763 #include "ti/drv/nwal/nwal_util.h"
3765 #define M 1008
3766 static int l3_off[M], l4_off[M], L3_chk_ok[M], L4_chk_ok[M], len[M] ;
3767 static nwal_AppId appid[M];
3768 static unsigned char * buf[M];
3769 #define N2POLL 8 //batch size
3770 #define NTOPOP 190
3771 Ti_Pkt * pHd[NTOPOP];
3772 void example_fast_pushpop(Pktlib_HeapHandle h , int n_trials)
3774 int i,j;
3775 unsigned long t1;
3776 unsigned long t2;
3777 unsigned long sumf=0;
3778 unsigned long sumt=0;
3779 unsigned long sump=0;
3780 #define NTOPOP 190
3781 Ti_Pkt * tempVA;
3782 Qmss_QueueHnd freeQ=Pktlib_getInternalHeapQueue(h);
3784 //n_trials of NTOPOP actions
3785 for(i=0;i<n_trials;i++)
3787     //raw pop
3788     t1= hplib_mUtilGetPmuCCNT();
3789     pHd[0] = (Ti_Pkt *)QMSS_DESC_PTR(PKTIO_QMSS_QUEUE_POP_RAW (freeQ));
3790     for(j=1;j<NTOPOP;j++)
3791     {
3792        tempVA  =  hplib_mVMPhyToVirt(pHd[j-1]);
3793        netapi_pld(tempVA);
3794         //__builtin_prefetch(tempVA);
3795        pHd[j] = (Ti_Pkt *)QMSS_DESC_PTR(PKTIO_QMSS_QUEUE_POP_RAW (freeQ));
3796        sumf+= (int) (   ((Cppi_HostDesc *) tempVA)->origBuffPtr -
3797                   ((Cppi_HostDesc *) tempVA)->buffPtr);
3798     }
3799     tempVA  =  hplib_mVMPhyToVirt(pHd[j-1]);
3800     sumf+= (int) (   ((Cppi_HostDesc *) tempVA)->origBuffPtr -
3801                   ((Cppi_HostDesc *) tempVA)->buffPtr);
3802     t2=hplib_mUtilGetPmuCCNT();
3803     sumt+= (t2-t1);
3805     //invalidate/wb 
3806     for(j=0;j<NTOPOP;j++)
3807     {
3808         tempVA  =  hplib_mVMPhyToVirt(pHd[j]);
3809         hplib_cacheWbInv(tempVA,128);
3810     }
3812     //raw push 
3813     t1=hplib_mUtilGetPmuCCNT();
3814     for(j=0;j<NTOPOP;j++)
3815     {
3816         PKTIO_QMSS_QUEUE_PUSH_DESC_SIZE_RAW (freeQ,
3817                                              (void *) pHd[j],
3818                                              128);
3819     }
3820     t2=hplib_mUtilGetPmuCCNT();
3821     sump += (t2-t1);
3823 printf("\nfast poppush: np=%d  pop=%d  push=%d sumf=%d\n", (n_trials*NTOPOP),
3824         sumt/(n_trials*NTOPOP),  sump/(n_trials*NTOPOP), sumf);
3825 sleep(1);
3826 return;
3831 void example_fast_poll( PKTIO_HANDLE_T * p_pktio, int max_pkts)
3833 int j=0;
3834 int jj=0;
3835 int i,k,l=0,ltot=0;
3836 int n= N2POLL; //max # of pkts to poll
3837 Ti_Pkt * tempVA;
3838 pasahoLongInfo_t* pinfo;
3839 unsigned long t1;
3840 unsigned long t2;
3841 unsigned long t11;
3842 unsigned long t12;
3843 unsigned long np;
3844 unsigned long sumt=0;
3845 unsigned long sumf=0;
3846 unsigned long sump=0;
3847 unsigned long totlen=0;
3848 int max_batch=0;
3850 //this should  be done once and saved
3851 Qmss_QueueHnd rxQ= PKTIO_GET_DEFAULT_NETCP_Q(p_pktio);
3852 Qmss_QueueHnd freeQ;
3853 //loop forever
3854 for(;max_pkts>0;)
3856     t1= hplib_mUtilGetPmuCCNT();
3857     pHd[0] = (Ti_Pkt *)QMSS_DESC_PTR(PKTIO_QMSS_QUEUE_POP_RAW (rxQ));
3858     if (!pHd[0]) continue;
3859     //got pkt
3860     for(i=1;(i<n) && (pHd[i-1]);i++)
3861     {
3862         //convert previous descriptor PA -> VA
3863         tempVA  =  hplib_mVMPhyToVirt(pHd[i-1]); 
3865         //try and preload desriptor
3866          __builtin_prefetch(tempVA);
3867         //netapi_pld(tempVA);
3869         //read next descriptor from queue 
3870         pHd[i] = (Ti_Pkt *)QMSS_DESC_PTR(PKTIO_QMSS_QUEUE_POP_RAW (rxQ));
3871 #if 1   
3872         /* extract some meta data */
3873         Cppi_getData (Cppi_DescType_HOST, (Cppi_Desc*)tempVA, &buf[jj], &len[jj]);
3874         pinfo =  nwal_mGetProtoInfo(tempVA);
3875         l3_off[jj]= nwal_mGetL3OffBytes(pinfo);
3876         l4_off[jj]= nwal_mGetL4Offset(pinfo);
3877         if(nwal_mGetAppidFmPkt(tempVA,&appid[jj]) != nwal_TRUE)
3878         {
3879             printf("ERROR!!!! AppID not available in incoming packet \n");
3880         }
3881 //#define VERIFY_SOP
3882 #ifdef VERIFY_SOP
3883   if (sump < 200)  printf("..sop off=%d\n", (int) buf[jj]-
3884            (int)  ((Cppi_HostDesc *) tempVA)->origBuffPtr) ;
3885 #endif
3886 #endif
3887         jj+=1;
3888     }
3889     //finish last pkt in burst
3890     if(pHd[i-1])
3891     {
3892         //convert previous descriptor PA -> VA
3893         tempVA  =  hplib_mVMPhyToVirt(pHd[i-1]); 
3895         /* extract some meta data */
3896 #if 1
3897         pinfo =  nwal_mGetProtoInfo(tempVA);
3898         l3_off[jj]= nwal_mGetL3OffBytes(pinfo);
3899         l4_off[jj]= nwal_mGetL4Offset(pinfo);
3900         if(nwal_mGetAppidFmPkt(tempVA,&appid[jj]) != nwal_TRUE)
3901         {
3902             printf("ERROR!!!! AppID not available in incoming packet \n");
3903         }
3904 #endif
3905         //get ptr (Physical address) and length of associate buffer
3906         Cppi_getData (Cppi_DescType_HOST, (Cppi_Desc*)tempVA, &buf[jj], &len[jj]);
3907         jj+=1;
3908     }
3909     t2= hplib_mUtilGetPmuCCNT();
3910     j+=(pHd[i-1]? i: (i-1)) ;
3911     if (jj>(M-n)) jj=0;
3912     l+=1;  //n batches
3913     ltot+=1;
3914     if(pHd[i-1])
3915     {
3916       if (i>max_batch) max_batch= i;
3917     }
3918     else
3919     {
3920       if( (i-1) >max_batch) max_batch = i-1;
3921     }
3923     //cleanup
3924     //printf("cleanup %d\n",i);
3925     for(k=0;k<i;k++)
3926     {
3927         //cleanup.  need to covert all of desriptor to VA so that i can use freePacket() 
3928         //alternative would be to just do cache ops plus descriptor raw push to pktlib
3929         // heap free queue
3930         if(pHd[k])
3931         {
3933         //tempVA=Qmss_osalConvertDescPhyToVirt(pHd[k]);
3934         tempVA  =  hplib_mVMPhyToVirt(pHd[k]);
3935         freeQ=Qmss_getQueueHandle(Cppi_getReturnQueue (Cppi_DescType_HOST, (Cppi_Desc *)tempVA));
3936         hplib_cacheWbInv(tempVA,128);
3937         //would need to wbInv buffer also in practice. Also need to walk
3938         // descriptor chain 
3939         t11= hplib_mUtilGetPmuCCNT();
3940        // Pktlib_freePacket(tempVA);
3941         PKTIO_QMSS_QUEUE_PUSH_DESC_SIZE_RAW (freeQ,
3942                                              (void *) pHd[k],
3943                                              128);
3945         t12= hplib_mUtilGetPmuCCNT();
3946         sumf += (t12-t11);       
3947         }
3948     }
3949     sumt += (t2-t1);
3950     sump +=(pHd[i-1]? i: (i-1));
3951     max_pkts -= (pHd[i-1]? i: (i-1));
3953     //printf("end cleanup %d %d %d\n",sumt,sumf,sump );
3954     if (sump > 10000) {
3955        printf("pkts rx %d batches=%d  appid=%x l3_off=%d l4_off=%d len=%d buf=0x%x rxcycle= %d pkts/batchx1000=%d maxbatch=%d cycles per rawpush = %d\n", 
3956                   j,ltot, appid[j%M], 
3957                   l3_off[j%M],l4_off[j%M],  
3958                   len[j%M],buf[j%M],
3959                    sumt/sump,  (sump*1000)/l, max_batch,
3960                    sumf/sump);
3961        sumt=sump=sumf=0;
3962        l=0;
3963     }
3966     //cleanup any remaining buffers
3967     for(;;)
3968     {
3969         pHd[0] = (Ti_Pkt *)QMSS_DESC_PTR(PKTIO_QMSS_QUEUE_POP_RAW (rxQ));
3970         if(!pHd[0]) break;
3971         tempVA  =  Osal_qmssPhyToVirt(pHd[0]);
3972         freeQ=Qmss_getQueueHandle(Cppi_getReturnQueue (Cppi_DescType_HOST, (Cppi_Desc *)tempVA));
3973         hplib_cacheWbInv(tempVA,128);
3974         PKTIO_QMSS_QUEUE_PUSH_DESC_SIZE_RAW (freeQ,
3975                                              (void *) pHd[0],
3976                                              128);
3977     }
3980 #ifdef EXPERIMENTAL
3982 static inline void send_it(Ti_Pkt *tip, int len, ROUTE_SEC_T * p_sec, int out_port)
3984   unsigned long st1;
3985   unsigned long st2;
3986   int err=0;
3987   PKTIO_METADATA_T meta2 = {PKTIO_META_TX,{0},0};
3988   nwalTxPktInfo_t meta_tx2={0};
3989   st1=hplib_mUtilGetPmuCCNT();
3990   if (len<60) 
3991   { 
3992      unsigned int templen;
3993      char * p_pkt;
3994      len=60; 
3995      Pktlib_getDataBuffer(tip,(uint8_t**)&p_pkt,&templen);//ignore templen
3996      Cppi_setData (Cppi_DescType_HOST, (Cppi_Desc *) tip, p_pkt,len);
3997      stats[coreid].tx_min+=1;
3998   }
3999   Pktlib_setPacketLen(tip,len);
4000   meta_tx2.txFlag1 = (NWAL_TX_FLAG1_DO_IPV4_CHKSUM| NWAL_TX_FLAG1_META_DATA_VALID );
4001   meta_tx2.startOffset = 0;
4002   meta_tx2.ipOffBytes = 14;
4003   meta_tx2.ploadLen = len ;
4004   meta_tx2.enetPort=out_port;
4005   if(p_sec)
4006   {   
4007        meta_tx2.txFlag1 |= NWAL_TX_FLAG1_DO_IPSEC_ESP_CRYPTO ;
4008        meta2.sa_handle=p_sec->tx_inflow_mode_handle; //this tells netapi that inflow crypto needs to be applied
4009        meta_tx2.saOffBytes=14+20;
4010        meta_tx2.saPayloadLen=len-14-20;   //don't include tag, mac and outer header
4011        stats[coreid].sec_tx+=1;
4012   }
4013   meta2.u.tx_meta=&meta_tx2;
4014   pktio_send(netcp_tx_chan,tip,&meta2,&err);
4015   stats[coreid].tx +=1;
4016   st2=hplib_mUtilGetPmuCCNT();
4017   stats[coreid].send_cycles += (unsigned long long) (st2-st1);  
4018
4019 void recv_cb_router(struct PKTIO_HANDLE_Tag * channel, Ti_Pkt* p_recv[],
4020                          PKTIO_METADATA_T meta[], int n_pkts,
4021                          uint64_t ts )
4023 int i;
4024 int len;
4025 int p;
4026 Ti_Pkt * tip;
4027 unsigned int templen;
4028 char * p_pkt;
4029 HEAD_T temp_head;
4030 unsigned int appid;
4031 IP_HEAD_T th;
4032 ROUTE_SEC_T *sec_data=NULL;
4033 unsigned long t1;
4034 unsigned long t2;
4035 unsigned long long ct1;
4036 unsigned long long ct2;
4037 unsigned short ip_pl;
4038 unsigned long long n_c_ops;
4039 int ifno;
4040 int out_port;
4041 #ifdef MULTI_THREAD
4042     int coreid=Osal_nwalGetProcId();  //who we are(thread local)
4043     //int coreid = our_core;
4044 #else
4045 int coreid=0;
4046 #endif
4047 t1=hplib_mUtilGetPmuCCNT();
4048 ct1 =Osal_cache_op_measure(&n_c_ops);
4049 for(i=0;i<n_pkts;i++)
4051         ifno = ((unsigned int)meta[i].u.rx_meta->appId)&0xff;
4052         if(coreid<TUNE_NETAPI_NUM_CORES) stats[coreid].rx+=1;
4053         if (ifno < TUNE_NETAPI_MAX_NUM_MAC) stats[coreid].if_rx[ifno]+=1;
4054         tip = p_recv[i];
4055         Pktlib_getDataBuffer(tip,(uint8_t**)&p_pkt,&templen);//ignore templen
4056         len = Pktlib_getPacketLen(tip)-4;//real length, subtract mac trailer
4057         stats[coreid].rx+=1;
4058         appid = ((unsigned int)meta[i].u.rx_meta->appId)&0xff000000;
4059         switch(appid)
4060         {
4061            case(NETAPI_NETCP_MATCH_IPSEC):
4062            case(NETAPI_NETCP_MATCH_IPSEC_POLICY):
4063            {
4064              int tailen=12+2;
4065              memcpy(&temp_head,&p_pkt[14],sizeof(HEAD_T));
4066              if (!check_header(&temp_head,&meta[i])) {
4067                 stats[coreid].n_bad+=1;
4068                 Pktlib_freePacket(tip); 
4069                 continue;
4070              }
4071              tailen+=p_pkt[len-12-2]; //padding length  (12)should come from sec_ptr
4072              p_pkt = &p_pkt[8+16+20];   //16= iv len, should come from sec_ptr
4073              len -= (8+16+20+tailen);  //16= iv len should come from sec ptr
4074             
4075              //now check inner headder.
4076              memcpy(&th,&p_pkt[14],20);
4077              if (!check_header(&temp_head,&meta[i])) {
4078                 stats[coreid].n_bad+=1;
4079                 Pktlib_freePacket(tip);
4080                 continue;
4081              }
4082              Cppi_setData (Cppi_DescType_HOST, (Cppi_Desc *) tip, p_pkt,len);
4083              Pktlib_setPacketLen(tip,len);
4084              
4085              if (route_pkt(our_router, tip, &th, p_pkt, &len,&sec_data,&out_port)<0)
4086               {
4087                 stats[coreid].n_bad+=1;
4088                 Pktlib_freePacket(tip);
4089              }
4090              else
4091              {
4092                send_it(tip,len,sec_data,out_port);
4093              }
4094              break;
4095            }
4096            case(NETAPI_NETCP_MATCH_GENERIC_MAC):
4097               if((p_pkt[12]!=0x8)||(p_pkt[13]!=0x00)) 
4098               {
4099                 stats[coreid].n_new+=1;
4100                 Pktlib_freePacket(tip); 
4101                 continue;
4102               }
4103               if (!check_header(&temp_head,&meta[i])) 
4104               {
4105                 stats[coreid].n_bad+=1;
4106                 Pktlib_freePacket(tip);
4107                 continue;
4108               }
4109               memcpy(&th,&p_pkt[14],20);
4110               ip_pl= (((unsigned char *)&th.w1)[2]<<8) | ((unsigned char *)&th.w1)[3];
4111               if ((ip_pl+14)<60)
4112               {
4113                 len-= (60-(ip_pl+14));
4114                 stats[coreid].rx_min+=1;
4115               }
4116               Pktlib_setPacketLen(tip,len);
4117               if (route_pkt(our_router, tip, &th, p_pkt, &len,&sec_data,&out_port)<0)
4118               {
4119                 stats[coreid].n_bad+=1;
4120                 Pktlib_freePacket(tip);
4121               }
4122               else
4123               {
4124                  send_it(tip,len,sec_data,out_port);
4125               }
4126               break;
4127            case(NETAPI_NETCP_MATCH_GENERIC_IP):
4128               Pktlib_freePacket(tip); 
4129               stats[coreid].n_new=1;
4130               break;
4131            default:
4132               stats[coreid].n_new+=1;
4133               Pktlib_freePacket(tip);
4134               break;
4135        }
4137 t2=hplib_mUtilGetPmuCCNT();
4138 ct2 =Osal_cache_op_measure(&n_c_ops);
4139 stats[coreid].app_cycles +=  (unsigned long long) (t2-t1);
4140 stats[coreid].tx_cache_cycles += (unsigned long long) (ct2-ct1);
4141 return;
4143 #endif