]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - keystone-rtos/netapi.git/blob - ti/runtime/netapi/test/net_test.c
fc7124f88140a017f8cb305fc30e2673539e01b8
[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  *****************************************/
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <unistd.h>
47 #include <string.h>
50 #include "trie.h"
51 #include "string.h"
52 #include "netapi.h"
53 #include "pktio.h"
54 #include <sys/resource.h>
55 #include "net_test.h"
56 #include <ti/drv/sa/salld.h>
57 #define EXPERIMENTAL
58 #ifdef EXPERIMENTAL
59 #include "router.c"
60 Trie * our_router;
62 OUR_ROUTE_T routes[MAX_ROUTES]=
63 {
64 {0,{0xD4,0xbe,0xd9,0x00,0xd3,0x7e, 0x00,0x01,0x02,0x03,0x14,0x02,0x08,0x00},0},
65 {0,{0x00,0x00,0x0,0x00,0x0,0x0, 0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00},0},
66 {0,{0xD4,0xbe,0xd9,0x00,0xd3,0x7e, 0x00,0x01,0x02,0x03,0x14,0x02,0x08,0x00},0},
67 {0,{0x00,0x15,0x60,0xa1,0xf7,0xbe, 0x00,0x01,0x02,0x03,0x04,0x05,0x08,0x00},0},
68 {0,{0xd4,0xbe,0xd9,0x00,0xd3,0x7e, 0x00,0x01,0x02,0x03,0x04,0x05,0x08,0x00},0}};
70 unsigned int ip[MAX_ROUTES]={BE(0x0a0100c8),BE(0x0a00000a),BE(0x0a02000a),BE(0xc0a8010a),BE(0x9eda6719)};
72 #endif
73 //#define TEST_TIMERS
75 /*************debug********************/
76 void dump_descr(unsigned long *p, int n)
77 {
78    printf("--------dump of descriptor %d %x\n", n, (int) p);
79    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]);
80    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]);
81    printf("-----------------------------\n");
82 }
83 void dump_header(unsigned long *p, int n, int a, int r)
84 {
85    printf("--------dump of header %d %x appID=%x flag1=%x\n", n, (int) p,a,r);
86    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]);
87    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]);
88    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]);
89    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]);
90    printf("-----------------------------\n");
91 }
93 /*****************************************/
96 //************for multi pkt burst  xfer test in loopback mode
97 #define TX_BURST 700 
98 int pktloopback=TUNE_NETAPI_NWAL_ENABLE_PASS_LOOPBACK;
99 nwalTxPSCmdInfo_t   flowPSCmdInfo;
101 //this device: 10.0.0.100, mac 0x,01,02,03,04,05  and .. 0x6
103 //test packet, setup for loopback (so dest is ourself)
104 static uint8_t testPkt[] = {
106   /* MAC header */
107   0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
108   0x00, 0xe0, 0xa6, 0x66, 0x57, 0x04,
109   0x08, 0x00,
111   /* IP header */
112   0x45, 0x00,
113   0x00, 0x6c,  /* Length (including this header) */
114   0x00, 0x00, 0x00, 0x00, 0x05, 0x11,
115   0x00, 0x00,  /* Header checksum */
116   0x0a, 0x00, 0x00, 0x0a, 0x0a, 0x00, 0x00, 0x64,
118   /* UDP header */
119   0x12, 0x34, 0x05, 0x55,
120   0x00, 0x58,  /* Length, including this header */
121   0x00, 0x00,  /* Header checksum */
123  /* Payload */
124   0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
125   0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41,
126   0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
127   0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51,
128   0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
129   0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 0x60, 0x61,
130   0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
131   0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71,
132   0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
133   0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81
135 };
137 char    input_file_name[] = "net_test_config.txt";
138 #define MAX_LINE_LENGTH 40
140 #define TEST_PAYLOAD_LEN            80
142 #define TEST_PKT_IP_OFFSET_BYTES        14
143 #define TEST_PKT_UDP_OFFSET_BYTES       34
144 #define TEST_PKT_PLOAD_OFFSET_BYTES     42
145 #define TEST_PKT_UDP_HDR_LEN            8
146 /* Offsets to length fields */
147 #define TEST_PKT_OFFSET_IP_LEN      16
148 #define TEST_PKT_OFFSET_UDP_LEN     38
150 #define TEST_PKT_LEN                122
152 /* The pseudo header checksum of the packet except for the 16 bit length */
153 #define TEST_PKT_PSEUDO_HDR_CHKSUM_SANS_LEN  0x0FFC
155 void example_fast_poll( PKTIO_HANDLE_T * p_pktio, int max_pkts);
156 void example_fast_pushpop(Pktlib_HeapHandle p_heap, int ntrials);
158 void recv_cb_router(struct PKTIO_HANDLE_Tag * channel, Ti_Pkt* p_recv[],
159                          PKTIO_METADATA_T meta[], int n_pkts,
160                          uint64_t ts );
163 //#include "arpa/inet.h"
164 long htonl(long x)
166         long temp = (x&0xff000000)>>24 | (x&0xff0000)>>8 | (x&0xff00)<<8 |  (x&0xff)<<24 ;
167         return temp;
170 /********************************************************************
171  *  FUNCTION PURPOSE: Ones complement addition utility
172  ********************************************************************
173  ********************************************************************/
174 uint16_t test_utilOnesComplementAdd (uint16_t v1, uint16_t v2)
176   uint32_t result;
178   result = (uint32_t)v1 + (uint32_t)v2;
179   result = (result >> 16) + (result & 0xffff);
180   result = (result >> 16) + (result & 0xffff);
182   return ((uint16_t)result);
185 /********************************************************************
186  *  FUNCTION PURPOSE: Ones complement checksum utility
187  ********************************************************************
188  ********************************************************************/
189  uint16_t test_utilOnesCompChkSum (uint8_t *p, uint32_t nwords)
191   uint16_t chksum = 0;
192   uint16_t v;
193   uint32_t i;
194   uint32_t j;
196   for (i = j = 0; i < nwords; i++, j+=2)  {
197     v = (p[j] << 8) | p[j+1];
198     chksum = test_utilOnesComplementAdd (chksum, v);
199   }
200   return (chksum);
201 } /* utilOnesCompChkSum */
203 /**************************************************************************************
204  * FUNCTION PURPOSE: Compute ipv4 psudo checksum
205  **************************************************************************************
206  * DESCRIPTION: Compute ipv4 psudo checksum
207  **************************************************************************************/
208 uint16_t test_utilGetIpv4PsudoChkSum (uint8_t *data, uint16_t payloadLen)
210   uint16_t psudo_chksum;
212   psudo_chksum = test_utilOnesCompChkSum (&data[12], 4);
213   psudo_chksum = test_utilOnesComplementAdd(psudo_chksum, (uint16_t) data[9]);
214   psudo_chksum = test_utilOnesComplementAdd(psudo_chksum, payloadLen);
216   return (psudo_chksum);
218 } /* utilGetIpv4PsudoChkSum */
222 /* net test default configuration */
223 netTestConfig_t config =
225     {0x00,0x01,0x02,0x03,0x05,0x05},
226     {0x00,0x01,0x02,0x03,0x05,0x06},
227     {10, 0, 0, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
228     {10, 0, 1, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
229     {10, 0, 2, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
230     {192,168 , 1, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
231     {192,168 , 1, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
232     IPSEC_MODE_RX_SIDEBAND,
233     IPSEC_MODE_TX_SIDEBAND,
234     0
235 };
237 #if 1  //goes with real tx (to laptop) 
238 unsigned char real_mac_header[]={0xd4,0xbe,0xd9,0x00,0xd3,0x7e,
239                       0x00,0x01,0x02,0x03,0x04,0x05,
240                       0x08,0x00};
241 unsigned char real_ip_addr[]={0xa,0x00,0x00,0x64,0xa,0x0,0x0,0xa};
242 #endif
244 #if 0  //goes with loopback
245 unsigned char mac_header[]={0x00,0x01,0x02,0x03,0x04,0x05, 
246                       0x00,0x11,0x22,0x33,0x44,0x55,
247                       0x08,0x00};
248 #endif
249 #define NE 65536 
250 HEAD_T *nat;
252 #define NP 5000
253 int n_pkt = NP;
254 STATS_T stats;
256 Trie * P_trie;
257 Trie *p_trie_sa;
258 HEAD_T pkts[NP];
259 #define PERSLOW  10  //% of pkts that will not be fastpath'd 
260 int perslow= PERSLOW;
262 /*******************************************
263  *************NETAPI OBJECTS***************
264  *****************************************/
265 static NETAPI_CFG_T our_netapi_default_cfg=
267 TUNE_NETAPI_PERM_MEM_SZ,
268 128,  //start of packet offset for hw to place data on rx for default flow
269 TUNE_NETAPI_QM_CONFIG_MAX_DESC_NUM, //max number of descriptors in system
270 TUNE_NETAPI_NUM_GLOBAL_DESC,        //total we will use
271 TUNE_NETAPI_DEFAULT_NUM_BUFFERS,   //#descriptors+buffers in default heap
272 64, //#descriptors w/o buffers in default heap
273 TUNE_NETAPI_DEFAULT_BUFFER_SIZE+128+128,  //size of buffers in default heap
274 128   ,  //tail room
275 256      //extra room 
276 };
278 Pktlib_HeapHandle OurHeap;
279 Pktlib_HeapHandle specialSmall;
280 Pktlib_HeapHandle specialLarge;
282 PKTIO_HANDLE_T *our_chan;
283 PKTIO_HANDLE_T *netcp_rx_chan;
284 PKTIO_HANDLE_T *netcp_rx_chan2;
285 PKTIO_HANDLE_T *netcp_tx_chan;
286 PKTIO_HANDLE_T *netcp_sb_tx_chan;
287 PKTIO_HANDLE_T *netcp_sb_rx_chan;
288 PKTIO_CFG_T our_chan_cfg={PKTIO_RW, PKTIO_LOCAL, PKTIO_Q_ANY, 8};
289 PKTIO_CFG_T netcp_rx_cfg={PKTIO_R, PKTIO_NA, PKTIO_NA, 8};
290 PKTIO_CFG_T netcp_rx_cfg2={PKTIO_R, (PKTIO_GLOBAL|PKTIO_PKT), PKTIO_Q_ANY, 8};
291 PKTIO_CFG_T netcp_tx_cfg={PKTIO_W, PKTIO_NA, PKTIO_NA, 8};
292 PKTIO_CFG_T netcp_sb_rx_cfg={PKTIO_R, PKTIO_NA, PKTIO_NA, 8};
293 PKTIO_CFG_T netcp_sb_tx_cfg={PKTIO_W, PKTIO_NA, PKTIO_NA, 8};
295 void house(NETAPI_SCHED_HANDLE_T *s);
296 NETAPI_T netapi_handle;
297 NETAPI_SCHED_HANDLE_T * our_sched;
298 NETAPI_SCHED_CONFIG_T our_sched_cfg={
299   NETAPI_SCHED_DURATION|NETAPI_SCHED_CBV, 0, house, 5000000  //every 5000000 poll loops
300 };
301 void our_stats_cb(NETAPI_T h, paSysStats_t* pPaStats);
302 NETAPI_TIMER_GROUP_HANDLE_T ourTimerBlock; 
303 NETAPI_TIMER_T t1;
304 NETAPI_TIMER_T t2;
305 NETAPI_TIMER_T t3;
307 void our_timer_cb( NETAPI_TIMER_GROUP_HANDLE_T th,
308         int n_fired,     //# timers fired
309         NETAPI_TIMER_LIST_T fired_list,
310         uint64_t currentTime);
312 NETCP_CFG_IP_T ip_rule0;
313 NETCP_CFG_IP_T ip_rule1;
314 NETCP_CFG_CLASS_T class_0;
315 NETCP_CFG_CLASS_T class_1;
316 NETCP_CFG_CLASS_T class_2;
317 NETCP_CFG_FLOW_HANDLE_T specialFlow;
319 NETCP_CFG_CLASSIFIER_T class_0_cfg=
321    NETCP_CFG_CLASS_TYPE_L4,
322    {
323         {0,0, NWAL_APP_PLOAD_PROTO_UDP, {2500}}
324    }
325 };
327 NETCP_CFG_CLASSIFIER_T class_1_cfg=
329    NETCP_CFG_CLASS_TYPE_L4,
330    {
331         {0,0, NWAL_APP_PLOAD_PROTO_UDP, {2502}}
332    }
333 };
335 NETCP_CFG_ROUTE_T  class2_route=
337 NULL, NULL  //* to be filled in
338 };
339 NETCP_CFG_CLASSIFIER_T class_2_cfg=  
341    NETCP_CFG_CLASS_TYPE_L3_L4,
342    {
343         {0,  4 ,0/*fill in below*/ , NULL, NULL,          //L2/L3
344            NWAL_APP_PLOAD_PROTO_UDP, {2504}}   //L4
345    }
346 };
348 PKTIO_CONTROL_T zap_channel_control={PKTIO_CLEAR, NULL};
350 /* security objects. (for loopback mode) */
351 netTestSA_t sa_info[6];
352 int netapi_algorithm_set = 0;
353 int netapi_sec_sa_mode = 2;
354 /* tmannan-end */
355 //NETCP_CFG_SA_T rx_tunnel;
356 //NETCP_CFG_SA_T tx_tunnel;
357 NETCP_CFG_IPSEC_POLICY_T rx_policy[4];
358 //void * rx_data_mode_handle;
359 //void * tx_data_mode_handle;
360 //void * rx_inflow_mode_handle;
361 //void * tx_inflow_mode_handle;
362 /* rx */
365 NETAPI_SEC_SA_INFO_T rx_sa [4] = {
367     NWAL_SA_DIR_INBOUND,
368     0x11111111,  //spi
369     nwal_IpSecProtoESP, //ESP mode
370     nwal_SA_MODE_TUNNEL,  //tunnel mode
371     nwal_IPV4, //v4
372     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* Src IP (them) -> set below */
373     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* dst IP (us)-> set below*/
374     64,/* replayWindow */
375     NWAL_SA_AALG_HMAC_SHA1,
376     NWAL_SA_EALG_AES_CBC,
377     0,0  //na
378 },
380     NWAL_SA_DIR_INBOUND,
381     0x22222222,  //spi
382     nwal_IpSecProtoESP, //ESP mode
383     nwal_SA_MODE_TUNNEL,  //tunnel mode
384     nwal_IPV4, //v4
385     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* Src IP (them) -> set below */
386     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* dst IP (us)-> set below*/
387     64,/* replayWindow */
388     NWAL_SA_AALG_HMAC_SHA2_256,
389     NWAL_SA_EALG_AES_CTR,
390     0,0  //na
391 },
393     NWAL_SA_DIR_INBOUND,
394     0x33333333,  //spi
395     nwal_IpSecProtoESP, //ESP mode
396     nwal_SA_MODE_TUNNEL,  //tunnel mode
397     nwal_IPV4, //v4
398     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* Src IP (them) -> set below */
399     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* dst IP (us)-> set below*/
400     64,/* replayWindow */
401     NWAL_SA_AALG_HMAC_SHA2_256,
402     NWAL_SA_EALG_3DES_CBC,
403     0,0  //na
404 },
406     NWAL_SA_DIR_INBOUND,
407     0x44444444,  //spi
408     nwal_IpSecProtoESP, //ESP mode
409     nwal_SA_MODE_TUNNEL,  //tunnel mode
410     nwal_IPV4, //v4
411     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* Src IP (them) -> set below */
412     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* dst IP (us)-> set below*/
413     64,/* replayWindow */
414     NWAL_SA_AALG_HMAC_MD5,
415     NWAL_SA_EALG_NULL,
416     0,0  //na
418 };
420 /*tx */
421 NETAPI_SEC_SA_INFO_T tx_sa[4]= {
423     NWAL_SA_DIR_OUTBOUND,
424     0x11111111,  //spi
425     nwal_IpSecProtoESP, //ESP mode
426     nwal_SA_MODE_TUNNEL,  //tunnel mode
427     nwal_IPV4, //v4
428     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* Src IP (us) -> set below */
429     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* dst IP (them) -> set below*/
430     64, /* NA replayWindow */
431     NWAL_SA_AALG_HMAC_SHA1,
432     NWAL_SA_EALG_AES_CBC,
433     0,0  //seq no
434 },
435     {
436     NWAL_SA_DIR_OUTBOUND,
437     0x22222222,  //spi
438     nwal_IpSecProtoESP, //ESP mode
439     nwal_SA_MODE_TUNNEL,  //tunnel mode
440     nwal_IPV4, //v4
441     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* Src IP (us) -> set below */
442     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* dst IP (them) -> set below*/
443     64, /* NA replayWindow */
444     NWAL_SA_AALG_HMAC_SHA2_256,
445     NWAL_SA_EALG_AES_CTR,
446     0,0  //seq no
447 },
449     NWAL_SA_DIR_OUTBOUND,
450     0x33333333,  //spi
451     nwal_IpSecProtoESP, //ESP mode
452     nwal_SA_MODE_TUNNEL,  //tunnel mode
453     nwal_IPV4, //v4
454     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* Src IP (us) -> set below */
455     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* dst IP (them) -> set below*/
456     64, /* NA replayWindow */
457     NWAL_SA_AALG_HMAC_SHA2_256,
458     NWAL_SA_EALG_3DES_CBC,
459     0,0  //seq no
460 },
462     NWAL_SA_DIR_OUTBOUND,
463     0x44444444,  //spi
464     nwal_IpSecProtoESP, //ESP mode
465     nwal_SA_MODE_TUNNEL,  //tunnel mode
466     nwal_IPV4, //v4
467     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* Src IP (us) -> set below */
468     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* dst IP (them) -> set below*/
469     64, /* NA replayWindow */
470     NWAL_SA_AALG_HMAC_MD5,
471     NWAL_SA_EALG_NULL,
472     0,0  //seq no
474 };
476 static nwalSecKeyParams_t ourTXKeyParams[4] ={
478     16, /* encKeySize: CTR 16 bytes Encryption Key and 4 bytes Salt : 24 bytes:NWAL_SA_EALG_3DES_CBC and 0 bytes Salt*/
479     20, /* macKeySize: 16 bytes NWAL_SA_AALG_HMAC_SHA1 */
480     NULL, //set below
481     NULL, //set below
482 },
484     20, /* encKeySize: CTR 16 bytes Encryption Key and 4 bytes Salt : 24 bytes:NWAL_SA_EALG_AES_CTR and 0 bytes Salt*/
485     32, /* macKeySize: 16 bytes NWAL_SA_AALG_HMAC_SHA2_256 */
486     NULL, //set below
487     NULL, //set below
488 },
490     24, /* encKeySize: DES-CBC: 24 bytes:NWAL_SA_EALG_3DES_CBC and 0 bytes Salt*/
491     32, /* macKeySize: 16 bytes NWAL_SA_AALG_HMAC_SHA2_256 */
492     NULL, //set below
493     NULL, //set below
494 },
496     0, /* NULL*/
497     16, /* MD5, 16 bytes */
498     NULL, //set below
499     NULL, //set below
501 };
503 /* these keys are for aes-ctr and hmac sha2_256 */
504 static nwalSecKeyParams_t ourRXKeyParams[4] ={
506     16, /* encKeySize: CTR 16 bytes Encryption Key and 4 bytes Salt : 24 bytes:NWAL_SA_EALG_3DES_CBC and 0 bytes Salt*/
507     20, /* macKeySize: 16 bytes NWAL_SA_AALG_HMAC_SHA1 */
508     NULL, //set below
509     NULL, //set below
510 },
512     20, /* encKeySize: CTR 16 bytes Encryption Key and 4 bytes Salt : 24 bytes:NWAL_SA_EALG_AES_CTR and 0 bytes Salt*/
513     32, /* macKeySize: 16 bytes NWAL_SA_AALG_HMAC_SHA2_256 */
514     NULL, //set below
515     NULL, //set below
516 },
518     24, /* encKeySize: DES-CBC: 24 bytes:NWAL_SA_EALG_3DES_CBC and 0 bytes Salt*/
519     32, /* macKeySize: 16 bytes NWAL_SA_AALG_HMAC_SHA2_256 */
520     NULL, //set below
521     NULL, //set below
522 },
524     0, /* NWAL_SA_EALG_NULL*/
525     16, /* NWAL_SA_AALG_HMAC_MD5, 16 bytes */
526     NULL, //set below
527     NULL, //set below
529 };
532 static uint8_t ourAuthKey[36] =
533         {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
534          0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
535          0x20, 0x21, 0x22, 0x23 };
538 static uint8_t ourEncrKey[36] = 
539         {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
540          0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
541          0x30, 0x31, 0x32, 0x33 }; 
544 /*************************END NETAPI OBJECTS***********************/
546 #define START_SRC_IP 0x0a00000a
547 #define DST_IP       0xc0a80001
548 #define NEW_START_SRC_IP 0x9eda000a
549 #define DST_PORT 0x555 
550 #define START_SRC_PORT 0x1234
551 #define NEW_START_SRC_PORT 100
552 void update_header(HEAD_T * p_head, int len)
554    unsigned char *p = (unsigned char *) &p_head->udp[1];
555    len -= (20+14);
556    /* update ip checksum */
557    /* update udp checksum */
558    /* update length */
559    *p= (len&0xff00)>>8;
560    *(p+1) = len&0xff;
563 #if 0
564 void gen_pkts(int np)
566 int i;
567 int ip = START_SRC_IP &0xff;
568 int port= START_SRC_PORT;
569 //HEAD_T temp={{0x25000200,0xdead0000,0x80110000,START_SRC_IP,DST_IP},
570 //             {START_SRC_PORT<<16|DST_PORT,0x01ec<<16|0x0000}};
571 HEAD_T temp;
572 memcpy(&temp,&testPkt[0],sizeof(HEAD_T));
574 for(i=0;(i<np) && (i<NP);i++)
575   {
576        memcpy(&pkts[i],&temp,sizeof(temp));
577        update_header(&pkts[i],512);      /* update checksums etc */
578        /* change template for new pkt */
579        ip+=1;
580        if(ip>254) {(ip=START_SRC_IP&0xff); port+=1; }
581        temp.ip[3] = htonl((START_SRC_IP&0xffffff00)| ip);
582        temp.udp[0] = htonl( (temp.udp[0]&0xffff0000)| port);
583        temp.udp[1] = htonl(temp.udp[1]);
584      
585   }
586   n_pkt=np;
588 #endif
590 void build_table(Trie * p_trie)
592 int i;
593 int sport=NEW_START_SRC_PORT; 
594 HEAD_T temp,temp2;
595 KEY_T key;
597 memcpy(&temp,&testPkt[14],sizeof(temp));
599  //insert entry into trie
600 key.src_ip = temp.ip[3];
601 key.dst_ip = temp.ip[4];
602 key.src_port= (temp.udp[0]&0xffff0000)>>16;
603 key.dst_port= (temp.udp[0]&0x0000ffff);
604 trie_insert(p_trie,(char *)&key,sizeof(key), (void *) &nat[0]); //asociate with nat entry 0
606 //build nat table
607 for(i=0;i<100;i++)
609    memcpy(&temp2,&testPkt[14],sizeof(temp));
610    temp2.udp[0] = (temp2.udp[0] & 0xffff0000) |  sport;
611    memcpy(&nat[i], &temp2, sizeof(temp2));
612    sport+= 1;
616 //===========stub transmitter==================
617 void send_pkt(Ti_Pkt *pkt, int len)
619 //just free pkt.  Don't send
620 Pktlib_freePacket((Ti_Pkt*)pkt);
621         return;
624 //==========stub slow path============
625 void slow_path(Ti_Pkt *pkt, int len)
627 // debug: check descriptor for validity by verifying that desciptor link field is null as expected\n");
628          {Ti_Pkt * k= Pktlib_getNextPacket(pkt); if(k != 0) {printf(" slowpath, nexpkt != NULL");}}
629 //just free pkt
630 Pktlib_freePacket((Ti_Pkt*)pkt);
631         return;
633 /* check header */
634 struct LastPktInfo
636 int iface;
637 int ipcsum;
638 int l4csum;
639 } ;
640 static struct LastPktInfo lpInfo;
642 int check_header(HEAD_T * p_head, PKTIO_METADATA_T * p_meta)
644 if (NWAL_RX_FLAG1_META_DATA_VALID & p_meta->u.rx_meta->rxFlag1)
646 lpInfo.iface = ((unsigned int)p_meta->u.rx_meta->appId) &0xff; //last byte is interface num
647 lpInfo.ipcsum =(p_meta->u.rx_meta->rxFlag1 & NWAL_RX_FLAG1_IPV4_CHKSUM_VERIFY_MASK )== NWAL_RX_FLAG1_IPV4_CHKSUM_VERIFY_ACK ? 1 : 0;
648 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; 
649 if ((unsigned int)p_meta->u.rx_meta->appId & NETAPI_NETCP_MATCH_IPSEC)
651    stats.sec_rx++;
653 if ((unsigned int)p_meta->u.rx_meta->appId & NETAPI_NETCP_MATCH_IPSEC_POLICY)
655    stats.secp_rx++;
658 if ((unsigned int)p_meta->u.rx_meta->appId & NETAPI_NETCP_MATCH_CLASS)
660   int c= ((unsigned int)p_meta->u.rx_meta->appId >>8)&0xffff;
661   if (c==0)  stats.n_class0_rx +=1;
662   else if (c==1) stats.n_class1_rx +=1;
663   else if (c==2) stats.n_class2_rx +=1;
664   else printf("**NET_TEST RX -unknown class: %x\n",  p_meta->u.rx_meta->appId);
668  return 1;
671 #define PKT_LEN 1400
672 void test_alloc_free(int n)
674 int i;
675 Ti_Pkt * b;
677 for(i=0;i<n;i++)
679   b=Pktlib_allocPacket(OurHeap,PKT_LEN);
680   Pktlib_freePacket(b);
684 //measurement test points
685 unsigned int vv1;
686 unsigned int vv2;
687 unsigned int vv3;
688 unsigned int vv4;
689 unsigned int vv5;
690 unsigned int vv6;
691 //these are updated by pktio.
692 #ifdef PKTIO_GET_BENCHMARK
693 extern unsigned int vv7p;
694 extern unsigned int vv8p;
695 extern unsigned int vv9p;
696 extern unsigned int vv10p;
697 extern unsigned int vv11p;
698 extern unsigned int vv12p;
700 extern unsigned int vv13p;  //rcv path
701 extern unsigned int vv14p;
702 extern unsigned int vv15p;
703 #endif
705 unsigned int vv11;
707 extern unsigned int nwal_prof1,nwal_prof2,nwal_prof3,nwal_prof4,nwal_prof5,nwal_prof6;
709 //#define REASSEMBLE_BENCH
710 #ifdef REASSEMBLE_BENCH
711 #include <ti/drv/pa/example/reassemLib/reassemLib.h>
712 /*--------------reassembly benchmark--------------------------------*/
713 void our_reassemble_bench(int nfrags)
715 paIPReassemblyConfig_t Config={5,128,10000 };
716 int i,j;
717 int len;
718 Ti_Pkt tip;
719 char *buffer;
720 unsigned long v1;
721 unsigned long v2;
722 unsigned long sum1=0;
723 unsigned long sum2=0;
724 paEx_reassemLibInit(&Config);
725 for(j=0;j<200/nfrags;j++)
727   for(i=0;i<nfrags;i++)
728   {
729     short temp;
730     tip=Pktlib_allocPacket(OurHeap,PKT_LEN);
731     Pktlib_getDataBuffer(tip,(uint8_t**)&buffer,&len);
732     memcpy(&buffer[0],&testPkt[14],20); //IP header
733     if (i < (nfrags-1)) buffer[6] = 0x20;
734     temp = i*40; 
735     buffer[6]|= (temp&0x1f00)>>8;
736     buffer[7]= (temp&0xff);
737     temp = 20+40*8; 
738     buffer[2]= (temp&0xff00)>>8;
739     buffer[3]= (temp&0xff);
740     Pktlib_setPacketLen(tip, temp);
741     v1= netapi_timing_stop();
742     paEx_reassemLibProc(tip, 0xffff);
743     v2= netapi_timing_stop();
744     sum1+= v2-v1;
745   }
746   sum2 += v2-v1;
748 printf("reasssembly test:  %d trials, %d frags/pkt  %d cycles/frag %d cycles/last frag\n",j,nfrags, sum1/(j*nfrags), sum2/(j));
750 #endif
752 /*--------------basic pktio send/recv benchmark----------------------*/
753 unsigned int timings[10];
754 void our_pktio_bench(int ntrials)
756 int i;
757 #define NBATCH 8
758 Ti_Pkt tip;
759 unsigned char * pData;
760 int len;
761 int n;
762 int err;
763 int sum =0;
765    Osal_cache_op_measure_reset();
766    for(i=0;i<10;i++) timings[i]=0;
767    printf("calibration loop .. ");
768    for(i=0;i<1000;i++)
769    {
770    vv1= netapi_timing_stop();
771    vv2= netapi_timing_stop();
772    sum+=(vv2-vv1);
773    }
774    printf(" accuracy = +- %d cycles\n", sum/1000);
775    sleep(1);
776   
777 PKTIO_METADATA_T meta[10]={0};
778 //send single, recv single
779 for(i=0;i<ntrials;i++)
781    vv1= netapi_timing_stop();
782    tip=Pktlib_allocPacket(OurHeap,PKT_LEN);
783    vv2= netapi_timing_stop();
784    Pktlib_getDataBuffer(tip,&pData,&len);
785    vv3= netapi_timing_stop();
786    pktio_send(our_chan,tip,&meta[0],&err);
787    vv4= netapi_timing_stop();
788    n=pktio_poll(our_chan,NULL , &err);
789    vv5=   netapi_timing_stop();
790    timings[0]+=(vv6-vv4);
791    timings[1]+=(vv5-vv4);
792    timings[3]+=(vv4-vv3); 
793    timings[5]+=(vv3-vv1);
794    timings[8]+=(vv11-vv6);
796 #ifdef PKTIO_GET_BENCHMARK
797    timings[2]+=(vv7p-vv4);
798    timings[4]+=(vv8p-vv3);
799    timings[6]+=(vv9p-vv8p);
800    timings[7]+=(vv10p-vv7p);
801 #endif
804 #ifdef PKTIO_GET_BENCHMARK
805    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,
806            timings[1]/ntrials, timings[2]/ntrials,timings[3]/ntrials, timings[4]/ntrials,timings[5]/ntrials,
807            timings[6]/ntrials,timings[7]/ntrials, timings[8]/ntrials );
808    {
809        unsigned int ccycles;
810        int n_c_ops;
811        ccycles =Osal_cache_op_measure(&n_c_ops);
812        printf("n_c_ops=%d cache_op_time=%d (total) = %d (pp)\n", n_c_ops, ccycles,  n_c_ops? (ccycles/(n_c_ops)) : 0);
813    }
814 #endif
817 /*-----------test driver: gen an input pkt------- */
818 //char buffer[sizeof(HEAD_T)+PKT_LEN];
819 Ti_Pkt * get_pkt(int n, unsigned int *p_len, Pktlib_HeapHandle heap2use, int size, unsigned char * buf2cpy, int copy_size)
821    int ind;
822    long long temp;
823    Ti_Pkt * b;
824    char * buffer;
825    unsigned int len;
827   if (pktloopback==0)
828   {
829         if (n>=TX_BURST) return NULL;   //just gen pkts to warm swtich, so that it knows
830                                 //our mac is valid
831   }  
832   b=Pktlib_allocPacket(heap2use,size);
833   if (!b) 
834     {printf("net_test: get_pkt() heap empty!! %d pkts gen'd %d \n", n); return NULL;};
836     //debug - way to validate descriptor
837     {Ti_Pkt* k= Pktlib_getNextPacket(b); 
838          if(k != 0) {printf(" genpkt, nexpkt != NULL");}}
841    //get pointer to buffer area of packet
842    Pktlib_getDataBuffer(b,(uint8_t**)&buffer,&len);
843    if (!buffer) 
844     {printf("net_test: get_pkt() heap returned empty buffer %d \n", n); return NULL;};
846 #if 0 
847 if (pktloopback==0)
849    temp = (long long) rand();
850    temp *= PKT_LEN;
851    temp /= RAND_MAX;
852    temp +=2;
853    *p_len = (int) temp; 
854    *p_len = *p_len &0xfffffffe;
855    temp = (long long) rand();
856    temp *= n_pkt;
857    temp /= RAND_MAX;
858    ind = (int) temp;
859    update_header(&pkts[ind],*p_len);
860    //printf("get pkt:%d %d ind=%d len=%d\n",RAND_MAX, rand(),ind, *p_len);
861     memcpy(&buffer[0], &mac_header[0],14);
862     memcpy(&buffer[14],(char*)&pkts[ind],sizeof(HEAD_T)); 
864 else
865 #endif
867    //copy test packet into buffer
869     memcpy(&buffer[0], buf2cpy, copy_size);
870     *p_len = copy_size;
872     return b; 
875 static int eof=0;
876 /*--------------------------------------------------------------
877  *----------utility to flip a packet and send 
878  *--------------------back to source----------------------------
879  *                   flag=1 => ipsec
880  *--------------------------------------------------------------*/
881 void flip_and_send_pkt(Ti_Pkt *tip,  unsigned char * p_pkt, int len, int flag)
883 unsigned char mac_temp[6];
884 unsigned char ip_temp[4];
885 unsigned char new_dest_port[2]={0x75,0x30};  // 30000
886 uint16_t blah; 
887 uint16_t i=1;   /* for testing only */
889 uint8_t *p_spi; 
890 netTestSA_t * p_sa_info;
891 uint8_t p_iv[16];
893 Pktlib_setPacketLen(tip,len);
894 //mac
895 memcpy(&mac_temp,&p_pkt[0],6);
896 memcpy(&p_pkt[0],&p_pkt[6],6);
897 memcpy(&p_pkt[6],&mac_temp,6);
898 //memcpy(&p_pkt[0],real_mac_header,6); //for testing to wireshark pc
900 //ip  (outer in case of ipsec)
901 memcpy(&ip_temp, &p_pkt[14+12],4);
902 memcpy(&p_pkt[14+12],&p_pkt[14+12+4],4);
903 memcpy(&p_pkt[14+12+4],&ip_temp,4);
905 //outer checksum to 0
906 if (!flag) memset(&p_pkt[14+10],0,2);
911 //inner ip &udp for ipsec
912 if (flag) 
914     p_spi = &(p_pkt[netTest_MAC_HEADER_LEN+netTest_IP_HEADER_LEN]);
915     p_sa_info = (netTestSA_t *) trie_lookup(p_trie_sa, (char *)p_spi ,4);
916     if (p_sa_info == NULL)
917     {
918         printf("flip_and_send_pkt(): trie_lookup() failed\n");
919         return;
920     }
921     //just drop non-udp packet
922     if (p_pkt[p_sa_info->tx_payload_info.encOffset+9]!=0x11)
923   
924     {
925         stats.n_new+=1;Pktlib_freePacket(tip); return;
926     }
928 memcpy(&ip_temp, &p_pkt[p_sa_info->tx_payload_info.encOffset+12],4);
929 memcpy(&p_pkt[p_sa_info->tx_payload_info.encOffset+12],&p_pkt[p_sa_info->tx_payload_info.encOffset+12+4],4);
930 memcpy(&p_pkt[p_sa_info->tx_payload_info.encOffset+12+4],&ip_temp,4);
932 //udp
933 memcpy(&p_pkt[p_sa_info->tx_payload_info.encOffset+20+2],&new_dest_port[0],2);
934 memset(&p_pkt[p_sa_info->tx_payload_info.encOffset+20+6],0,2); //checksum
939 if (config.ipsec_mode_tx == IPSEC_MODE_TX_SIDEBAND)
941     //inner ip checksum : leave alone
942 #if 0
943     blah=test_utilOnesCompChkSum (&p_pkt[14+20+8+16], 10);
944     p_pkt[14+20+8+16+10]= (blah&0xff00)>>8;
945     p_pkt[14+20+8+16+11]= blah&0xff;
946 #endif
948     //tbd udp checksum (leave at 0)
950     //outer ip, set to 0 (we will compute on way out
951     memset(&p_pkt[14+10],0,2);
953 else
955 //#else //inflow, don't touch outer , clear inner 
956 //DALmemset(&p_pkt[14+20+8+16+10],0,2); //inner checksum, we will compute on way out
957 //outer ip checksum : leave alone
959 #if 0
960 blah = test_utilOnesCompChkSum (&p_pkt[14], 10);
961 p_pkt[14+10]= (blah&0xff00)>>8;
962 p_pkt[14+11]= blah&0xff;
963 #endif
967 else
969 memset(&p_pkt[14+20+6],0,2);//0 udp checksum (we will compute on way out
970 memcpy(&p_pkt[14+20+2],&new_dest_port[0],2);
973 //IPSEC case, 
974 if (flag)
976         if (config.ipsec_mode_tx == IPSEC_MODE_TX_SIDEBAND)
977  //send to crypto for encryption
978 //12 byte auth tag
979         {
980            PKTIO_METADATA_T meta = {PKTIO_META_SB_TX,{0},0};
981            int err;
982            nwalDmTxPayloadInfo_t meta_tx={0};
983            meta.sa_handle=p_sa_info->tx_data_mode_handle;  //use TX SA context
985 #if 0
986            meta_tx.ploadLen = len;
988            meta_tx.encOffset =p_sa_info->tx_payload_info.encOffset;
989            
990            meta_tx.authOffset =netTest_MAC_HEADER_LEN+netTest_IP_HEADER_LEN;
992            //meta_tx.encSize=len - 14- 20-8-16-12;
993            meta_tx.encSize = len - p_sa_info->tx_payload_info.encOffset -netTest_ICV_LEN;
994            
995            //meta_tx.authSize= len -14-20-12;
996            meta_tx.authSize = len - meta_tx.authOffset - netTest_ICV_LEN;
997            //meta_tx.encIvSize=16;
998            meta_tx.encIvSize = p_sa_info->tx_payload_info.encIvSize;
999            //meta_tx.pEncIV= &p_pkt[14+20+8];  //just use same IV..
1000 #endif
1001             memcpy(&meta_tx, &(p_sa_info->tx_payload_info), sizeof(nwalDmTxPayloadInfo_t));
1003            meta_tx.ploadLen = len;
1004            meta_tx.encSize = len - p_sa_info->tx_payload_info.encOffset -netTest_ICV_LEN;
1005            meta_tx.authSize = len - meta_tx.authOffset - netTest_ICV_LEN;
1007            meta_tx.encIvSize = p_sa_info->tx_payload_info.encIvSize;
1008 #if 0
1009             printf("flip_and_send_pkt(): encOffset %d\n", meta_tx.encOffset);
1010             printf("flip_and_send_pkt():authOffset %d\n", meta_tx.authOffset);
1011             printf("flip_and_send_pkt(): encSize %d\n", meta_tx.encSize);
1012             printf("flip_and_send_pkt(): authSize %d\n", meta_tx.authSize);
1013             printf("flip_and_send_pkt(): encIvSize %d\n", meta_tx.encIvSize);
1014 #endif
1016         if (p_sa_info->cipherMode ==  NWAL_SA_EALG_AES_CTR)
1017         {
1018            memcpy(&p_iv[0], &ourEncrKey[16], 4);
1019            memcpy(&p_iv[4], &p_pkt[netTest_MAC_HEADER_LEN +netTest_IP_HEADER_LEN + netTest_ESP_HEADER_LEN], 8);
1020             p_iv[12] = 0;
1021             p_iv[13] = 0;
1022             p_iv[14] = 0;
1023             p_iv[15] = 1;
1024            meta_tx.pEncIV = &p_iv[0];
1025          }
1026         else if (p_sa_info->cipherMode ==  NWAL_SA_EALG_NULL)
1027         {
1028             meta_tx.pEncIV = NULL;
1029         }
1030         else
1031         {
1032             meta_tx.pEncIV = &p_pkt[netTest_MAC_HEADER_LEN +netTest_IP_HEADER_LEN + netTest_ESP_HEADER_LEN ];
1033         }
1034            meta_tx.authIvSize=0;
1035            meta_tx.pAuthIV=NULL;
1036            meta_tx.aadSize=0;
1037            meta_tx.pAad=NULL;
1038            /* post it to netcp sb tx channel*/
1039            meta_tx.appCtxId = netapi_timing_start();
1040            meta.u.tx_sb_meta=&meta_tx;
1041            pktio_send(netcp_sb_tx_chan,tip,&meta,&err);
1042        }
1043 else
1044  {
1045   //inflow tx
1046   //send pkt directly, asking for IP and UDP checksum offloads AND IPSEC to be applied
1047            PKTIO_METADATA_T meta = {PKTIO_META_TX,{0},0};
1048            int err;
1049            nwalTxPktInfo_t meta_tx={0};
1050 #define USE_COPY
1051 #ifdef USE_COPY
1052 //debug:  see if re-using RX descriptor for TX is causing our SA lockup
1054   int new_len=0;
1055   Ti_Pkt new_tip =  get_pkt(0, &new_len, specialLarge , len+10 , &p_pkt[0]   , len);
1056   if (!new_tip)
1057   {
1058      printf("net_test> new_tip NULL\n");
1059   }
1060   else
1061   {
1062     Pktlib_setPacketLen(new_tip,new_len);
1063     Pktlib_freePacket(tip);
1064     tip=new_tip;  
1065     Pktlib_getDataBuffer(tip,(uint8_t**)&p_pkt,&new_len);  //reset p_pkt to point to new buffer as its used below
1066     Cppi_setTimeStamp (Cppi_DescType_HOST, (Cppi_Desc *) tip,stats.sec_tx); 
1067   }
1069 if (len <1500)  
1071   eof+=1;
1073 #endif
1074            meta.sa_handle=p_sa_info->tx_inflow_mode_handle; //this tells netapi that inflow crypto needs to be applied
1075            //meta_tx.txFlag1 = (NWAL_TX_FLAG1_DO_IPV4_CHKSUM|NWAL_TX_FLAG1_DO_UDP_CHKSUM| NWAL_TX_FLAG1_DO_IPSEC_CRYPTO| NWAL_TX_FLAG1_META_DATA_VALID );
1076            meta_tx.txFlag1 = p_sa_info->tx_pkt_info.txFlag1;
1077            meta_tx.enetPort=0;
1078            //meta_tx.saOffBytes=14+20;  
1079            meta_tx.saOffBytes=  p_sa_info->tx_pkt_info.saOffBytes;
1080            meta_tx.saPayloadLen=len-14-20;   //don't include tag, mac and outer header
1081            //meta_tx.startOffset = 0;
1082            meta_tx.startOffset = p_sa_info->tx_pkt_info.startOffset;
1083            //meta_tx.ipOffBytes = 14+20+8+16;   //to inner header
1084            meta_tx.ipOffBytes =p_sa_info->tx_payload_info.encOffset; 
1085            //meta_tx.l4OffBytes = 14+20+8+16+20; //to L4
1086            meta_tx.l4OffBytes = p_sa_info->tx_pkt_info.l4OffBytes;
1087            meta_tx.l4HdrLen = p_sa_info->tx_pkt_info.l4HdrLen;
1088            meta_tx.ploadLen = (unsigned) ((p_pkt[meta_tx.l4OffBytes+4]<<8)|p_pkt[meta_tx.l4OffBytes+4+1]) -8 ;
1089            meta_tx.pseudoHdrChecksum =
1090              test_utilGetIpv4PsudoChkSum(&p_pkt[meta_tx.ipOffBytes],8+ meta_tx.ploadLen);
1092            /* post it to netcp tx channel*/
1093            meta.u.tx_meta=&meta_tx;
1094            if (stats.sec_tx<20) dump_descr((long *) tip, stats.sec_tx);
1095            pktio_send(netcp_tx_chan,tip,&meta,&err);
1096            stats.tx +=1;
1097            stats.sec_tx +=1;
1098      }
1100 else  //non ipsec send pkt directly, asking for IP and UDP checksum ofload
1102            PKTIO_METADATA_T meta2 = {PKTIO_META_TX,{0},0};
1103            int err;
1104            nwalTxPktInfo_t meta_tx2={0};
1105            meta2.sa_handle=nwal_HANDLE_INVALID;
1106            meta_tx2.txFlag1 = (NWAL_TX_FLAG1_DO_IPV4_CHKSUM|NWAL_TX_FLAG1_DO_UDP_CHKSUM| NWAL_TX_FLAG1_META_DATA_VALID );
1107            meta_tx2.startOffset = 0;
1108            meta_tx2.ipOffBytes = 14;
1109            meta_tx2.l4OffBytes = 14+20;
1110            meta_tx2.l4HdrLen = 8;
1111            meta_tx2.ploadLen = (unsigned) ((p_pkt[14+20+4]<<8)|p_pkt[14+20+4+1]) -8 ;
1112            meta_tx2.pseudoHdrChecksum =
1113              test_utilGetIpv4PsudoChkSum(&p_pkt[14],8+ meta_tx2.ploadLen);
1115            /* post it to netcp tx channel*/
1116            meta2.u.tx_meta=&meta_tx2;
1117            pktio_send(netcp_tx_chan,tip,&meta2,&err);
1118            stats.tx +=1;
1125 /***************************************
1126  benchmark receive handler
1127 ****************************************/
1128 void recv_cb_bench(struct PKTIO_HANDLE_Tag * channel, Ti_Pkt* p_recv[],
1129                          PKTIO_METADATA_T meta[], int n_pkts,
1130                          uint64_t ts )
1132    int i;
1133    vv6=   netapi_timing_stop();
1134    for (i=0;i<n_pkts; i++) 
1135    {
1136      Pktlib_freePacket(p_recv[i]);
1137    }
1138    vv11 = netapi_timing_stop();
1141 /****************************************************************************************/
1142 /******************SB Accelerator Callback PKT RECEIVE HANDLER *************************/
1143 /******************  Handles Decrypt and Encrypt operation callbacks ******************/
1144 /******************************************************************************************/
1145 void recv_sb_cb(struct PKTIO_HANDLE_Tag * channel, Ti_Pkt* p_recv[],
1146                          PKTIO_METADATA_T meta[], int n_pkts,
1147                          uint64_t ts )
1149 int i;
1150 int len;
1151 int p;
1152 HEAD_T * p_res;
1153 Ti_Pkt * tip;
1154 unsigned int templen;
1155 int err;
1156 KEY_T key;
1157 char * p_pkt;
1158 HEAD_T * p_head;
1159 HEAD_T temp_head;
1160 int tag_cmp=0;
1161 unsigned int hash[3];
1162 uint8_t *p_spi;
1163 netTestSA_t *p_sa_info;
1165 //nwal_AppId time;
1166 unsigned long time, delta_time;
1167  /* loop over received pkts */
1168    for(i=0;i<n_pkts;i++)
1169    {
1170         tip = p_recv[i];
1171         Pktlib_getDataBuffer(tip,(uint8_t**)&p_pkt,&templen);//ignore templen
1172         len = Pktlib_getPacketLen(tip);//real length
1175          p_spi = &(p_pkt[netTest_MAC_HEADER_LEN+netTest_IP_HEADER_LEN]);
1176         p_sa_info = (netTestSA_t *) trie_lookup(p_trie_sa, (char *)p_spi ,4);
1177         if (p_sa_info == NULL)
1178         {
1179             printf("recv_sb_cb(): trie_lookup failed\n");
1180             continue;
1181         }
1184         //is this a decrypt (rx_tunnel) complete
1185         if ((int)meta[i].u.rx_sb_meta->appId == p_sa_info->rx_tunnel)
1186         {
1187            
1188             time = netapi_timing_start();
1189            delta_time = time -(unsigned long) meta[i].u.rx_sb_meta->appCtxId;
1190             stats.total_decrypt_time += delta_time;
1191             stats.sb_rx+=1;
1192            //copy hash out of meta data (for some reason it needs endian conversion)
1193            hash[0]= htonl( meta[i].u.rx_sb_meta->pAuthTag[0]);
1194            hash[1]= htonl( meta[i].u.rx_sb_meta->pAuthTag[1]);
1195            hash[2]= htonl( meta[i].u.rx_sb_meta->pAuthTag[2]);
1197            if(stats.sb_rx<=16)
1198            {
1199              char *tp = (char *) &hash[0];
1200              //dump_header((long*)p_pkt, stats.sb_rx, (int)meta[i].u.rx_sb_meta->appId,0);
1201 #if 0
1202              printf("tag in pkt=%x %x %x %x %x %x %x %x %x %x %x %x\n",
1203                       p_pkt[len-12],p_pkt[len-11],p_pkt[len-10],p_pkt[len-9], p_pkt[len-8],
1204                       p_pkt[len-7],p_pkt[len-6],
1205                       p_pkt[len-5],p_pkt[len-4],p_pkt[len-3],p_pkt[len-2],p_pkt[len-1]);
1206              printf("tag from SA=%x %x %x %x %x %x %x %x %x %x %x %x\n",
1207                        tp[0],tp[1],tp[2],tp[3],tp[4],tp[5],
1208                        tp[6],tp[7],tp[8],tp[9],tp[10],tp[11]);
1209 #endif
1210            }
1211            //check tag 
1212            tag_cmp = memcmp(&p_pkt[len-12],(char*) &hash[0],12); //todo, really use meta->authTagLen
1213            stats.n_auth_ok += !(tag_cmp);
1214            
1215             flip_and_send_pkt(tip, p_pkt, len,1);  //flip packet to echo back and send
1216         }
1217         //this is an encrypt (tx tunnel) complete
1218         else if((int)meta[i].u.rx_sb_meta->appId== p_sa_info->tx_tunnel )
1219         {
1220            hash[0]= htonl( meta[i].u.rx_sb_meta->pAuthTag[0]);
1221            hash[1]= htonl( meta[i].u.rx_sb_meta->pAuthTag[1]);
1222            hash[2]= htonl( meta[i].u.rx_sb_meta->pAuthTag[2]);
1223            stats.sb_tx+=1;
1224            if(stats.sb_tx<=16)
1225            {
1226              char *tp1 = (char *) &hash[0];
1227              //dump_header((long*)p_pkt, stats.sb_tx, (int)meta[i].u.rx_sb_meta->appId,0);
1228 #if 0
1229              printf("tag in original rx pkt=%x %x %x %x %x %x %x %x %x %x %x %x\n",
1230                       p_pkt[len-12],p_pkt[len-11],p_pkt[len-10],p_pkt[len-9], p_pkt[len-8],
1231                       p_pkt[len-7],p_pkt[len-6],
1232                       p_pkt[len-5],p_pkt[len-4],p_pkt[len-3],p_pkt[len-2],p_pkt[len-1]);
1234              printf("tag from SA=%x %x %x %x %x %x %x %x %x %x %x %x\n",
1235                        tp1[0],tp1[1],tp1[2],tp1[3],tp1[4],tp1[5],
1236                        tp1[6],tp1[7],tp1[8],tp1[9],tp1[10],tp1[11]);
1237 #endif
1238            }
1239            //put the computed tag in the packet
1240            memcpy(&p_pkt[len-12],(char*)&hash[0],12); //todo, really use meta->authTagLen
1241            {
1242            PKTIO_METADATA_T meta2 = {PKTIO_META_TX,{0},0};
1243            nwalTxPktInfo_t meta_tx={0};
1244            // now send directly 
1245            meta2.sa_handle=nwal_HANDLE_INVALID;
1246            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
1247            meta_tx.startOffset = 0;
1248            meta_tx.ipOffBytes = netTest_MAC_HEADER_LEN;
1249            //not used
1250            meta_tx.l4OffBytes = 0;
1251            meta_tx.l4HdrLen = 0;
1252            meta_tx.ploadLen = 0;
1253             time = netapi_timing_start();
1254             delta_time = time -(unsigned long) meta[i].u.rx_sb_meta->appCtxId;
1255            stats.total_encrypt_time += delta_time;
1256         
1258            /* post it to netcp tx channel*/
1259            meta2.u.tx_meta=&meta_tx;
1260            pktio_send(netcp_tx_chan,tip,&meta2,&err);
1261            stats.tx +=1;
1262            }
1263         }
1264         else printf("netapi recv_sb_cb: unknown appiD %x \n",meta[i].u.rx_sb_meta->appId );
1265     }
1268 /******************************************************/
1269 /******************PKT RECEIVE HANDLER *************************/
1270 /******************************************************/
1271 void recv_cb(struct PKTIO_HANDLE_Tag * channel, Ti_Pkt* p_recv[],
1272                          PKTIO_METADATA_T meta[], int n_pkts,
1273                          uint64_t ts )
1275 int i;
1276 int len;
1277 int p;
1278 HEAD_T * p_res;
1279 Ti_Pkt * tip;
1280 unsigned int templen;
1281 int err;
1282 KEY_T key;
1283 char * p_pkt;
1284 HEAD_T * p_head;
1285 HEAD_T temp_head;
1286 netTestSA_t *p_sa_info;
1287 uint8_t *p_spi;
1288 uint8_t p_iv[16];
1290     p_head=&temp_head;
1292     //debug
1293 #if 0
1294     if (n_pkts != TX_BURST) {
1295       printf("recv_cb, txsofar=%d rxsofar=%d  np = %d, NOT %d\n", 
1296              stats.itx, stats.rx, n_pkts,TX_BURST);
1297       our_stats_cb(netapi_handle,NULL);
1298     }
1299 #endif
1300     //test_alloc_free(7);
1301     //printf("recv start\n");
1303     /* loop over received pkts */
1304     for(i=0;i<n_pkts;i++)
1305    {
1306         tip = p_recv[i];
1307         Pktlib_getDataBuffer(tip,(uint8_t**)&p_pkt,&templen);//ignore templen
1308         len = Pktlib_getPacketLen(tip)-4;//real length, subtract mac trailer
1310         //debug: validate descriptor */
1311         if(Pktlib_getNextPacket(tip) != 0) 
1312         {
1313             printf(" rcv_cb, nexpkt != NULL");
1314         }
1315         //debug printf("recv pkt, len=%d %d\n", len, templen);
1316         stats.rx+=1;
1318 #ifdef DEBUG_DESC
1319         if (stats.rx<16)
1320         {
1321             printf(">rx dmp.."); 
1322             dump_descr((long *) tip, stats.rx);
1323         }
1324         else if (stats.rx>99) 
1325         {
1326             printf(">rx dmp.."); 
1327             dump_descr((long *) tip,stats.rx);
1328         }
1329 #endif
1330 #if 0
1331         if(stats.rx<=16)
1332         {
1333             dump_header((long*)p_pkt, stats.rx, (int)meta[i].u.rx_meta->appId,meta[i].u.rx_meta->rxFlag1);
1334         }
1335 #endif
1336         /* check header */
1337         memcpy(p_head,&p_pkt[14],sizeof(HEAD_T));
1339         if ((p_head->ip[2]&0x0000ff00)==0x00003200)
1340         {
1341             if (!check_header(p_head,&meta[i]))
1342             {
1343                 stats.n_bad+=1;Pktlib_freePacket(tip); 
1344                 continue;
1345             }
1350             //process IP SEC PACKET
1351         if (config.ipsec_mode_rx == IPSEC_MODE_RX_SIDEBAND)
1352         {
1353             p_spi = &(p_pkt[netTest_MAC_HEADER_LEN+netTest_IP_HEADER_LEN]);
1354             p_sa_info = (netTestSA_t *) trie_lookup(p_trie_sa, (char *)p_spi ,4);
1355             if (p_sa_info == NULL)
1356             {
1357                 printf("recv_cb(): trie_lookup() failed\n");
1358                 continue;
1359             }
1360            //ship to crypto for decrypt!!
1361            //12 byte auth tag
1362             PKTIO_METADATA_T meta2 = {PKTIO_META_SB_TX,{0},0};
1363            nwalDmTxPayloadInfo_t meta_tx={0};
1364            meta2.sa_handle=p_sa_info->rx_data_mode_handle;
1366            memcpy(&meta_tx, &(p_sa_info->tx_payload_info), sizeof(nwalDmTxPayloadInfo_t));
1368            meta_tx.ploadLen = len;
1369            meta_tx.encSize = len - p_sa_info->tx_payload_info.encOffset -netTest_ICV_LEN;
1370            meta_tx.authSize = len - meta_tx.authOffset - netTest_ICV_LEN;
1372            meta_tx.encIvSize = p_sa_info->tx_payload_info.encIvSize;
1373 #if 0
1374             printf("recv_cb(): encOffset %d\n", meta_tx.encOffset);
1375             printf("recv_cb():authOffset %d\n", meta_tx.authOffset);
1376             printf("recv_cb(): encSize %d\n", meta_tx.encSize);
1377             printf("recv_cb(): authSize %d\n", meta_tx.authSize);
1378             printf("recv_cb(): encIvSize %d\n", meta_tx.encIvSize);
1379 #endif
1381             if (p_sa_info->cipherMode ==  NWAL_SA_EALG_AES_CTR)
1382             {
1383                 memcpy(&p_iv[0], &ourEncrKey[16], 4);
1384                 memcpy(&p_iv[4], &p_pkt[netTest_MAC_HEADER_LEN +netTest_IP_HEADER_LEN + netTest_ESP_HEADER_LEN], 8);
1385                 p_iv[12] = 0;
1386                 p_iv[13] = 0;
1387                 p_iv[14] = 0;
1388                 p_iv[15] = 1;
1389                  meta_tx.pEncIV = &p_iv[0];
1390             }
1391             else if (p_sa_info->cipherMode ==  NWAL_SA_EALG_NULL)
1392             {
1393                 meta_tx.pEncIV = NULL;
1394             }
1395             else
1396             {
1397                 meta_tx.pEncIV = &p_pkt[netTest_MAC_HEADER_LEN +netTest_IP_HEADER_LEN + netTest_ESP_HEADER_LEN ];
1398             }
1399             meta_tx.appCtxId = netapi_timing_start();
1400             //printf("recv_cb appCtxId: %lu\n", meta_tx.appCtxId);
1402            /* post it to netcp sb tx channel*/
1403            meta2.u.tx_sb_meta=&meta_tx;
1404            pktio_send(netcp_sb_tx_chan,tip,&meta2,&err);
1405            continue;
1406         }
1407         else 
1408             //inflow mode.  flip and send
1409             flip_and_send_pkt(tip,p_pkt,len,1);
1410     }
1411     else if ((p_head->ip[2]&0x0000ff00)!=0x00001100)
1412     {
1413         stats.n_new+=1;Pktlib_freePacket(tip); continue;
1414     }
1415     else  //non ipsec
1416     {
1417         if (!check_header(p_head,&meta[i])) { 
1418                 stats.n_bad+=1;Pktlib_freePacket(tip); 
1419         continue;
1420         }
1422 #if 0
1423         /* lookup flow */
1424         key.src_ip = p_head->ip[3];
1425         key.dst_ip = p_head->ip[4];
1426         key.src_port= (p_head->udp[0]&0xffff0000)>>16;
1427         key.dst_port= (p_head->udp[0]&0x0000ffff);
1428         p_res= (HEAD_T *) trie_lookup(P_trie, (char *) &key, sizeof(key));
1429         if (!p_res) { stats.n_new+=1;  slow_path(tip, len); continue;}
1431         /* copy header */
1432         memcpy((char *) p_head, (char *) p_res, sizeof(HEAD_T));
1434         memcpy(&p_pkt[14],p_head,sizeof(HEAD_T));
1435         /* update_mac(&p_pkt[0]);  */
1437         /* 'simulate' send pkt */
1438         send_pkt(tip,len);
1439 #endif
1440              //just flip and send
1441             flip_and_send_pkt(tip,p_pkt,len,0);
1442         }
1443     }
1444     //printf("recv done\n");
1447 //timer callback 
1448 void our_timer_cb( NETAPI_TIMER_GROUP_HANDLE_T th,
1449         int n_fired,     //# timers fired
1450         NETAPI_TIMER_LIST_T fired_list,
1451         uint64_t currentTime)
1453 int i;
1454 NETAPI_TIMER_T tx;
1455 int cookie;
1456 int err;
1457 unsigned long long et;
1458 //DEBUGprintf("TIMER CALLBACK @ %lld %d timers\n", currentTime, n_fired);
1459 tx = netapi_TimerGetFirst(fired_list);
1460 for(i=0;i<n_fired;i++)
1462   cookie = (int) netapi_TimerGetCookie(tx);
1463   et =  netapi_TimerGetTs(tx); //debug
1464   //DEBUGprintf("   timer %d - cookie = %d expected ts=%lld (delta=%lld)\n", i, cookie, et, currentTime-et);
1465   if (cookie ==1)
1466   {  
1467      stats.n_t1+=1;
1468      t1 = netapi_TimerGroupStartTimer(
1469         th,
1470         (void *) 1,
1471         100LL,  //timer group tics
1472         &err);
1473   }
1474   else if (cookie ==2)
1475   {
1476       stats.n_t2+=1;
1477       t2 = netapi_TimerGroupStartTimer(
1478         th,
1479         (void *) 2,
1480         200LL,  //timer group ticks
1481         &err);
1482   }
1483   else
1484   {
1485     stats.n_t3+=1;
1486     t3 = netapi_TimerGroupStartTimer(
1487         th,
1488         (void *) 3,
1489         300LL,  //timer group ticks
1490         &err);
1491     //cancel 1 and restart 1
1492    netapi_TimerGroupCancel(th,t1,&err);
1493    t1 = netapi_TimerGroupStartTimer(
1494         th,
1495         (void *) 1,
1496         100LL,  //timer group ticks
1497         &err);
1498  }
1499   tx = netapi_TimerGetNext(fired_list,tx); 
1503 void print_ipsec_stats(Sa_IpsecStats_t     *p_saIpsecStats, nwal_saAALG auth, nwal_saEALG cipher)
1505 #if 0
1506     if(retVal != nwal_OK)
1507     {
1508         System_printf("CORE: %d Error getting IP Sec Stats: Ret Status: %d \n",
1509                        retVal);
1510         return(nwal_FALSE);
1511     }
1512     if((p_saIpsecStats->pktEncHi) ||(p_saIpsecStats->pktEncLo))
1513     {
1514         printf("------------- IPSec TX (Encryption Channel) Stats BEGIN --\n");
1515     }
1516     else
1517     {
1518         printf("------------- IPSec RX (Decryption Channel) Stats BEGIN --\n");
1519     }
1520 #endif
1521     printf("\nAutentication mode: %d, Encryption Mode: %d\n", auth, cipher);
1522     printf("IPSec replayOld:0x%x,replayDup:0x%x,authFail:0x%x \n",
1523                    p_saIpsecStats->replayOld,p_saIpsecStats->replayDup,p_saIpsecStats->authFail);
1524     printf("IPSec txESN:0x%x,rxESN:0x%x,pktEncHi:0x%x,pktEncLo:0x%x,pktDecHi:0x%x,pktDecLo:0x%x \n",
1525                    p_saIpsecStats->txESN,p_saIpsecStats->rxESN,p_saIpsecStats->pktEncHi,
1526                    p_saIpsecStats->pktEncLo,p_saIpsecStats->pktDecHi,p_saIpsecStats->pktDecLo);
1527     printf("------------- IPSec Stats END ----------------------------\n\n");
1530 void print_datamode_stats(Sa_DataModeStats_t     *p_saDataModeStats, nwal_saAALG auth, nwal_saEALG cipher)
1533     printf("\nAutentication mode: %d, Encryption Mode: %d\n", auth, cipher);
1534     printf(" Packets processedHi: 0x%x, Packets processed Lo:0x%x\n",
1535             p_saDataModeStats->pktHi, p_saDataModeStats->pktLo);
1541 static int np2process = NP;
1542 /******************************************************
1543  * stats callback
1544  *******************************************************/
1545 void our_stats_cb(NETAPI_T h, paSysStats_t* pPaStats)
1547 uint32_t numFreeDataPackets;
1548 uint32_t            numZeroBufferPackets;
1549 uint32_t            numPacketsinGarbage;
1550 Pktlib_HeapStats    pktLibHeapStats;
1551 int i;
1552 unsigned long long bcpp;
1553 unsigned long long bcpp_noc;
1554 unsigned long long bcpp_app;
1555 unsigned long long bcpp_tx;
1556 unsigned long long npL;
1557 unsigned long long cyclesL;
1558 unsigned long long ccyclesL; //cache cycles
1559 NETAPI_SA_STATS_T netapi_sa_stats;
1561 printf(">*****stats @ %lld\n", netapi_getTimestamp());
1562 //printf("netcp_tx_handle check %x\n", netcp_tx_chan->back);
1563 printf(">itx=%d rx=%d tx=%d bad=%d slow=%d \n>rx_class0=%d rx_class1=%d rx_class2=%d  secRx=%d  secPRX=%d sb_rx=%d sb_tx=%d auth_ok=%d sec_tx=%d\n",
1564          stats.itx, stats.rx, stats.tx, stats.n_bad, stats.n_new, 
1565          stats.n_class0_rx, stats.n_class1_rx, 
1566          stats.n_class2_rx, stats.sec_rx, stats.secp_rx, stats.sb_rx, stats.sb_tx, stats.n_auth_ok,
1567          stats.sec_tx);
1569 if (stats.rx && stats.tx)
1570     printf("decrypt time per packet(avg): %lu, encrypt  time per packet(avg): %lu\n", 
1571     (unsigned long) stats.total_decrypt_time/stats.rx, (unsigned long) stats.total_encrypt_time/stats.tx);
1572 netapi_sched_get_stats(&npL,&cyclesL,&ccyclesL);
1573 if (npL && stats.rx) {
1574    bcpp = cyclesL/npL; 
1575    bcpp_noc = (cyclesL-ccyclesL)/npL; 
1576    bcpp_app = (stats.app_cycles-stats.tx_cache_cycles)/stats.rx;
1577
1578 else {bcpp = bcpp_noc=bcpp_app=0L;}
1579 if (stats.tx)
1581     bcpp_tx = (stats.send_cycles-stats.tx_cache_cycles)/stats.tx;
1582 }else {bcpp_tx = 0L;}
1583 printf(">         ++ busy cycles pp=%lld (%lld wo cache ops) (app+tx= %lld) (tx= %lld) ++\n",
1584          bcpp,bcpp_noc,bcpp_app, bcpp_tx);
1585 if(pPaStats)
1587        printf("C1 number of packets:           %d\n", pPaStats->classify1.nPackets);
1588        printf("C1 number IPv4 packets:         %d\n", pPaStats->classify1.nIpv4Packets);
1589        printf("C1 number IPv6 packets:        %d\n", pPaStats->classify1.nIpv6Packets);
1590        printf("C1 number Custom packets:        %d\n", pPaStats->classify1.nCustomPackets);
1591        printf("C1 number SRIO packets:        %d\n", pPaStats->classify1.nSrioPackets);
1592        printf("C1 number LLC/SNAP Fail packets:        %d\n", pPaStats->classify1.nLlcSnapFail);
1593        printf("C1 number table matched:        %d\n", pPaStats->classify1.nTableMatch);
1594        printf("C1 number failed table matched: %d\n", pPaStats->classify1.nNoTableMatch);
1595        printf("C1 number IP Fragmented packets: %d\n", pPaStats->classify1.nIpFrag);
1596        printf("C1 number IP Depth Overflow: %d\n", pPaStats->classify1.nIpDepthOverflow);
1597        printf("C1 number VLAN Depth Overflow: %d\n", pPaStats->classify1.nVlanDepthOverflow);
1598        printf("C1 number GRE Depth Overflow: %d\n", pPaStats->classify1.nGreDepthOverflow);
1599        printf("C1 number MPLS Packets: %d\n", pPaStats->classify1.nMplsPackets);
1600        printf ("C1 number of parse fail:        %d\n",pPaStats->classify1.nParseFail);
1601        printf("C1 number of Invalid IPv6 Opt:  %d\n", pPaStats->classify1.nInvalidIPv6Opt);
1602        printf("C1 number of TX IP Fragments:  %d\n", pPaStats->classify1.nTxIpFrag);
1603        printf ("C1 number of silent discard:    %d\n",pPaStats->classify1.nSilentDiscard);
1604        printf("C1 number of invalid control:   %d\n", pPaStats->classify1.nInvalidControl);
1605        printf ("C1 number of invalid states:    %d\n",pPaStats->classify1.nInvalidState);
1606        printf ("C1 number of system fails:      %d\n",pPaStats->classify1.nSystemFail);
1607        printf ("C2 number Packets  :           %d\n",pPaStats->classify2.nPackets);
1608        printf ("C2 number udp           :      %d\n",pPaStats->classify2.nUdp);
1609        printf ("C2 number tcp           :      %d\n",pPaStats->classify2.nTcp);
1610        printf ("C2 number Custom       :      %d\n",pPaStats->classify2.nCustom);
1611        printf ("C2 number silent drop   :      %d\n",pPaStats->classify2.nSilentDiscard);
1612        printf ("C2 number invalid cntrl :      %d\n\n",pPaStats->classify2.nInvalidControl);
1613        printf ("C2 number Modify Stats Cmd Fail :      %d\n\n",pPaStats->modify.nCommandFail);
1615 Pktlib_getHeapStats(OurHeap, &pktLibHeapStats);
1616 printf("main heap stats>  #free=%d #zb=%d #garbage=%d\n", pktLibHeapStats.numFreeDataPackets,
1617                                 pktLibHeapStats.numZeroBufferPackets, pktLibHeapStats.numPacketsinGarbage);
1618 printf("               >  #dataBufThreshStatus=%d #dataBufStarvCounter=%d #zBufThreshStatus=%d #zBufStarvCounter=%d \n", 
1619                         pktLibHeapStats.dataBufferThresholdStatus,pktLibHeapStats.dataBufferStarvationCounter,
1620                         pktLibHeapStats.zeroDataBufferThresholdStatus, pktLibHeapStats.zeroDataBufferStarvationCounter);
1622 Pktlib_getHeapStats(specialSmall, &pktLibHeapStats);
1623 printf("specialSmall heap stats>  #free=%d #zb=%d #garbage=%d\n", pktLibHeapStats.numFreeDataPackets,
1624                                 pktLibHeapStats.numZeroBufferPackets, pktLibHeapStats.numPacketsinGarbage);
1625 printf("                       >  #dataBufThreshStatus=%d #dataBufStarvCounter=%d #zBufThreshStatus=%d #zBufStarvCounter=%d \n", 
1626                                 pktLibHeapStats.dataBufferThresholdStatus,pktLibHeapStats.dataBufferStarvationCounter,
1627                                 pktLibHeapStats.zeroDataBufferThresholdStatus, pktLibHeapStats.zeroDataBufferStarvationCounter);
1630 Pktlib_getHeapStats(specialLarge, &pktLibHeapStats);
1631 printf("specialLarge heap stats>  #free=%d #zb=%d #garbage=%d\n", pktLibHeapStats.numFreeDataPackets,
1632                                 pktLibHeapStats.numZeroBufferPackets, pktLibHeapStats.numPacketsinGarbage);
1633 printf("                       >  #dataBufThreshStatus=%d #dataBufStarvCounter=%d #zBufThreshStatus=%d #zBufStarvCounter=%d \n", 
1634                                 pktLibHeapStats.dataBufferThresholdStatus,pktLibHeapStats.dataBufferStarvationCounter,
1635                                 pktLibHeapStats.zeroDataBufferThresholdStatus, pktLibHeapStats.zeroDataBufferStarvationCounter);
1638 #if 0
1639 printf("pa2sa descriptor area dump\n");
1640 for(i=0;i<TUNE_NETAPI_CONFIG_MAX_PA_TO_SA_DESC;i++)
1642    extern long * pa2sa_descr_base;
1643    long * tip= &pa2sa_descr_base[32*i]; 
1644    dump_descr(tip, i);
1646 printf("sa2pa descriptor area dump\n");
1647 for(i=0;i<TUNE_NETAPI_CONFIG_MAX_SA_TO_PA_DESC;i++)
1649    extern long * sa2pa_descr_base;
1650    long * tip= &sa2pa_descr_base[32*i]; 
1651    dump_descr(tip, i);
1653 #endif
1654 //debug = dump timer polling stats
1655 //dump_poll_stats();
1657     for (i = 0; i < MAX_SEC_INDEX; i++)
1658     {
1659         if (config.ipsec_mode_rx == IPSEC_MODE_RX_INFLOW)
1660         {
1661             memset(&netapi_sa_stats, 0, sizeof(netapi_sa_stats));
1662             netapi_getSaStats(netapi_handle, sa_info[i].rx_tunnel, &netapi_sa_stats);
1663             if (netapi_sa_stats.mode_active & NETAPI_INFLOW_MODE_ACTIVE)
1664                 print_ipsec_stats(&(netapi_sa_stats.saIpsecStats), rx_sa[i].authMode, rx_sa[i].cipherMode);
1665         }
1666 #if 0
1667         else
1668         {
1669             memset(&netapi_sa_stats, 0, sizeof(netapi_sa_stats));
1670             netapi_getSaStats(netapi_handle, sa_info[i].rx_tunnel, &netapi_sa_stats);
1671              if (netapi_sa_stats.mode_active & NETAPI_SIDEBAND_MODE_ACTIVE)
1672             print_datamode_stats(&(netapi_sa_stats.dataModeStats),rx_sa[i].authMode, rx_sa[i].cipherMode);
1673         }
1674 #endif
1676         if (config.ipsec_mode_tx == IPSEC_MODE_TX_INFLOW)
1677         {
1678             memset(&netapi_sa_stats, 0, sizeof(netapi_sa_stats));
1679             netapi_getSaStats(netapi_handle, sa_info[i].tx_tunnel, &netapi_sa_stats);
1680              if (netapi_sa_stats.mode_active & NETAPI_INFLOW_MODE_ACTIVE)
1681                 print_ipsec_stats(&(netapi_sa_stats.saIpsecStats), tx_sa[i].authMode, tx_sa[i].cipherMode);
1682         }
1683 #if 0
1684         else
1685         {
1686             memset(&netapi_sa_stats, 0, sizeof(netapi_sa_stats));
1687             netapi_getSaStats(netapi_handle, sa_info[i].tx_tunnel, &netapi_sa_stats);
1688             if (netapi_sa_stats.mode_active & NETAPI_SIDEBAND_MODE_ACTIVE)
1689                 print_datamode_stats(&(netapi_sa_stats.dataModeStats),tx_sa[i].authMode, tx_sa[i].cipherMode);
1690         }
1691  #endif
1692         
1693     }
1696 //******************************************************
1697 //use scheduling housekeeping callback to generate pkts
1698 //******************************************************
1699 static int done_burst=0;
1700 void house(NETAPI_SCHED_HANDLE_T * s)
1702 Ti_Pkt * tip;
1703 unsigned int len;
1704 nwalTxPktInfo_t meta_tx = {0};
1705 PKTIO_METADATA_T meta = {PKTIO_META_TX,{0},0};
1706 int err;
1707 static int house_pkts_gened=0;
1708 int p;
1709 unsigned char * pIpHdr,* pData;
1710 unsigned int vv1,vv2,vv3;
1711 unsigned int sum_vv1=0;
1712 unsigned int sum_vv2=0;
1713 unsigned int sum_vv3=0;
1714 unsigned int sum_vv4=0;
1715 unsigned int sum_vv5=0;
1717 unsigned int nwal_flow_vv1,nwal_flow_vv2;
1718 unsigned int nwal_sum_vv1=0;
1719 unsigned int nwal_sum_vv2=0;
1720 unsigned int nwal_sum_vv3=0;
1721 unsigned int nwal_sum_vv4=0;
1722 unsigned int nwal_sum_vv5=0;
1723 unsigned int nwal_sum_vv6=0;
1725 unsigned int nwal_sum_flow_vv1=0;
1726 unsigned int nwal_sum_flow_vv2=0;
1727 unsigned int cache_op_b1;
1728 unsigned int cache_op_b2;
1729 int n_c_ops;
1730 static int first =0;
1731 Cppi_HostDesc*      pPktDesc;
1733 if(done_burst)
1735   house_pkts_gened+=TX_BURST;
1736   printf("net_test> request stats at n=%d \n",house_pkts_gened);
1737   netcp_cfgReqStats(netapi_handle, our_stats_cb, 0,&err);
1738   if (err!=0) {printf("stats req failed\n");}
1739   if (house_pkts_gened >= np2process+ 100)
1740   {
1741      netapi_schedShutdown(s,NULL,&err);
1742   }
1743   return;
1745 done_burst=1;
1746 Osal_cache_op_measure_reset();
1747 memset(&meta_tx,0,sizeof(meta_tx));
1748 for(p=0;p<TX_BURST;p++) {  
1749 //reguest stats 
1750 if ((house_pkts_gened>0) && (! (house_pkts_gened%1000)) )
1752    printf("net_test> request stats at n=%d \n",house_pkts_gened);
1753    netcp_cfgReqStats(netapi_handle, our_stats_cb, 0,&err); 
1754    if (err!=0) {printf("stats req failed\n");}
1758   if (house_pkts_gened >= np2process+ 100)
1759   {
1760      //shutdown
1761      netapi_schedShutdown(s,NULL,&err);
1762      continue;
1763   }
1765   else if (house_pkts_gened >= np2process) { house_pkts_gened+=1;  continue;}
1766   
1768 /* manufacture a pkt to transmit */
1769    tip = get_pkt(house_pkts_gened, &len, OurHeap, PKT_LEN,&testPkt[0]  , TEST_PKT_LEN);
1770    if(!tip) { house_pkts_gened +=1; continue; }
1773    /* set the pkt length */
1774    vv1 = netapi_timing_start();
1775    Pktlib_setPacketLen(tip, len);
1777    /* set up meta data */
1778     meta.sa_handle=nwal_HANDLE_INVALID;
1779 //#define BENCH_UDP_SEND
1780 #ifdef BEND_UDP_SEND
1781     meta_tx.txFlag1 = (NWAL_TX_FLAG1_DO_IPV4_CHKSUM | NWAL_TX_FLAG1_DO_UDP_CHKSUM| NWAL_TX_FLAG1_META_DATA_VALID);
1782     meta_tx.startOffset = 0;
1783     //GONE in V2 meta_tx.pktLen = len;
1784     meta_tx.ipOffBytes = TEST_PKT_IP_OFFSET_BYTES;
1785     meta_tx.l4OffBytes = TEST_PKT_UDP_OFFSET_BYTES;
1786     meta_tx.l4HdrLen = TEST_PKT_UDP_HDR_LEN;
1787     //GONE in V2 meta_tx.ploadOffBytes = TEST_PKT_PLOAD_OFFSET_BYTES;
1788     meta_tx.ploadLen = TEST_PAYLOAD_LEN;
1790     Pktlib_getDataBuffer(tip,&pData,&len);
1791    if(house_pkts_gened &0x1)
1792    {
1793         memcpy(&pData[6],&config.mac1[0] ,6);
1794    }
1795     pIpHdr = pData + meta_tx.ipOffBytes;
1796     meta_tx.pseudoHdrChecksum =
1797         test_utilGetIpv4PsudoChkSum(pIpHdr,(TEST_PAYLOAD_LEN+TEST_PKT_UDP_HDR_LEN));
1798 #else
1799    Pktlib_getDataBuffer(tip,&pData,&len);
1800    if(house_pkts_gened &0x1)
1801    {
1802         memcpy(&pData[6],&config.mac1[0] ,6);
1803    }
1804     meta_tx.txFlag1 = NWAL_TX_FLAG1_META_DATA_VALID;
1805     meta_tx.startOffset = 0;
1806     meta_tx.ploadLen = TEST_PAYLOAD_LEN;
1807 #endif
1808    /* post it to netcp tx channel*/
1809    meta.u.tx_meta=&meta_tx;
1810 #ifdef DEBUG_DESC
1811    if (house_pkts_gened<16) dump_descr((long *) tip, house_pkts_gened);
1812    else if (house_pkts_gened>99) dump_descr((long *) tip,house_pkts_gened);
1813 #endif
1815    if(!first)
1816    {
1817        first++;
1818        nwal_flow_vv1= netapi_timing_stop();
1819        if(nwal_initPSCmdInfo(PKTIO_GET_NWAL_INSTANCE(netcp_tx_chan),
1820                              &meta_tx,
1821                              &flowPSCmdInfo) != nwal_OK)
1822        {
1823            printf("nwal_initPSCmdInfo() ERROR \n");
1824        }
1825        nwal_flow_vv2= netapi_timing_stop();
1826        nwal_sum_flow_vv1 += (nwal_flow_vv1-vv1); 
1827        nwal_sum_flow_vv2 += (nwal_flow_vv2-nwal_flow_vv1); 
1828    }
1829        
1830    cache_op_b1= Osal_cache_op_measure(&n_c_ops);
1831    vv2= netapi_timing_stop();
1832 #ifdef BEND_UDP_SEND
1833    nwal_mCmdSetL4CkSumPort(  tip,
1834                              &flowPSCmdInfo,
1835                              TEST_PKT_UDP_OFFSET_BYTES,
1836                              (TEST_PKT_UDP_HDR_LEN + TEST_PAYLOAD_LEN),
1837                              meta_tx.pseudoHdrChecksum,
1838                              meta_tx.enetPort);
1839 #else
1840    nwal_mCmdSetPort  (tip,
1841                       &flowPSCmdInfo,  //could be NULL
1842                       0);  //port 0 -> switch decides
1844 #endif
1846    pPktDesc = Pktlib_getDescFromPacket(tip);
1847    /* Send the packet out to transmit Q*/
1848    Qmss_queuePushDescSize (flowPSCmdInfo.txQueue, 
1849                         pPktDesc, 
1850                         NWAL_DESC_SIZE);
1851    vv3= netapi_timing_stop();
1852    cache_op_b2= Osal_cache_op_measure(&n_c_ops);
1854    sum_vv1 += (vv2-vv1);
1855    if(!house_pkts_gened)
1856    {
1857        /* first packet. Take out the PS command label creation cost */
1858        sum_vv1 = sum_vv1 - nwal_sum_flow_vv2;
1859    }
1861    sum_vv3 += (vv3-vv2)-(cache_op_b2-cache_op_b1); //sub out cache op cost
1863    // printf("pktio send. full=%d metadata=%d pktio_send=%d\n", vv3-vv1,  vv2-vv1,  vv3-vv2);
1864    
1866    stats.itx +=1;
1867    house_pkts_gened +=1;
1868  }
1870    unsigned int ccycles;
1871    ccycles =Osal_cache_op_measure(&n_c_ops);
1872    if (sum_vv1) 
1873    {
1874       printf("BURST NWAL Fast send %d pkts.  metadata=%d Cmd Label Creation Cost=%d  nwal Fast Send Cost (less cacheop)= %d n_c_ops=%d cache_op_time=%d (pp-> %d)\n", 
1875               stats.itx, sum_vv1/stats.itx,  nwal_sum_flow_vv2, sum_vv3/stats.itx, 
1876               n_c_ops, ccycles, n_c_ops? (ccycles/(n_c_ops/2)) : 0);
1877 #if 0
1878       printf("NWAL Profile Cycles: Prof1= %d,Prof2=%d,Prof3=%d,Prof4=%d,Prof5=%d ,Prof6=%d \n",
1879               nwal_sum_vv1/stats.itx,nwal_sum_vv2/stats.itx,nwal_sum_vv3/stats.itx,
1880               nwal_sum_vv4/stats.itx,nwal_sum_vv5/stats.itx,nwal_sum_vv6/stats.itx);
1881   
1882 #endif  
1884       if(stats.itx2)
1885       {
1886           printf("nwal_flowSend Profile Cycles: Prof1= %d,Prof2=%d \n",
1887               nwal_sum_flow_vv1/stats.itx2,nwal_sum_flow_vv2/stats.itx2);
1888       }
1890    }
1894 void  build_sa_db(int i)
1897     if ((tx_sa[i].authMode == NWAL_SA_AALG_HMAC_SHA1) && (tx_sa[i].cipherMode == NWAL_SA_EALG_AES_CBC))
1898     {
1899         /* static configuration, will not change */
1900         sa_info[i].tx_payload_info.aadSize = 0;
1901         sa_info[i].tx_payload_info.pAad = NULL;
1902         sa_info[i].tx_payload_info.authIvSize = 0;
1903         sa_info[i].tx_payload_info.pAuthIV = NULL;
1904         sa_info[i].tx_payload_info.authOffset = netTest_MAC_HEADER_LEN  + netTest_IP_HEADER_LEN;        /*done: same for all cipher suites */
1905         sa_info[i].tx_payload_info.encIvSize = netTest_AES_CBC_IV_LEN;
1906         sa_info[i].tx_payload_info.encOffset = netTest_MAC_HEADER_LEN +
1907                                                                      netTest_IP_HEADER_LEN +
1908                                                                     netTest_ESP_HEADER_LEN +
1909                                                                     netTest_AES_CBC_IV_LEN;
1911        
1913         /* dynamic configuration, will  be calculated on the fly */
1914         sa_info[i].tx_payload_info.authSize = 0; /* pkt len - mac - ip -icv (12) */
1915         sa_info[i].tx_payload_info.encSize = 0;   /* authSize - esp header size (always 8 bytes) */
1916         sa_info[i].tx_payload_info.ploadLen = 0;  /* will be packet length */
1917         sa_info[i].tx_payload_info.pEncIV = 0;
1918        sa_info[i].tx_payload_info.pPkt = 0;     /* not being referenced in net_test */
1920         sa_info[i].cipherMode = NWAL_SA_EALG_AES_CBC;
1921         sa_info[i].inner_ip_offset = sa_info[i].tx_payload_info.encOffset;
1922         sa_info[i].auth_tag_size = netTest_ICV_LEN;  /* icv or mac size,. always 12 except for AES_CCM/AES_GCM */
1923 #ifdef EXPERIMENTAL
1924         sa_info[i].iv_len=16;
1925         sa_info[i].bl=16;
1926         sa_info[i].spi = tx_sa[i].spi;
1927         sa_info[i].src =*((unsigned int *)(&config.local_ipsec_ip.ipv4[0]));
1928         sa_info[i].dst =*((unsigned int *)(&config.remote_ipsec_ip.ipv4[0]));
1929 #endif
1930         sa_info[i].tx_pkt_info.enetPort = 0;
1931         sa_info[i].tx_pkt_info.ipOffBytes = sa_info[i].tx_payload_info.encOffset;
1932         sa_info[i].tx_pkt_info.l4HdrLen = netTest_UDP_HEADER_LEN; /*UDP header len */
1933         sa_info[i].tx_pkt_info.l4OffBytes = sa_info[i].inner_ip_offset + netTest_IP_HEADER_LEN;
1934         sa_info[i].tx_pkt_info.startOffset = 0;
1935         sa_info[i].tx_pkt_info.lpbackPass = 0;
1936         sa_info[i].tx_pkt_info.ploadLen = 0; /*sa_info[i].tx_pkt_info.l4OffBytes + 4 */
1937         sa_info[i].tx_pkt_info.pPkt = NULL;
1938         sa_info[i].tx_pkt_info.saOffBytes = netTest_MAC_HEADER_LEN  + netTest_IP_HEADER_LEN;
1939         sa_info[i].tx_pkt_info.saPayloadLen = 0;
1940         sa_info[i].tx_pkt_info.pseudoHdrChecksum =0;
1941        
1942         sa_info[i].tx_pkt_info.txFlag1 = NWAL_TX_FLAG1_DO_IPSEC_CRYPTO| NWAL_TX_FLAG1_DO_UDP_CHKSUM| NWAL_TX_FLAG1_META_DATA_VALID ;
1944          trie_insert(p_trie_sa,(char *)&(tx_sa[i].spi),4, (void *) &sa_info[i]); //asociate with tx sa SPI
1945          
1946     }
1947     else if ((tx_sa[i].authMode == NWAL_SA_AALG_HMAC_SHA2_256) && (tx_sa[i].cipherMode == NWAL_SA_EALG_AES_CTR))
1948     {
1949         printf("inside build_sa_db, index %d\n", i);
1950         /* static configuration, will not change */
1951         sa_info[i].tx_payload_info.aadSize = 0;
1952         sa_info[i].tx_payload_info.pAad = NULL;
1953         sa_info[i].tx_payload_info.authIvSize = 0;
1954         sa_info[i].tx_payload_info.pAuthIV = NULL;
1955         sa_info[i].tx_payload_info.authOffset = netTest_MAC_HEADER_LEN  + netTest_IP_HEADER_LEN;        /*done: same for all cipher suites */
1956         sa_info[i].tx_payload_info.encIvSize = netTest_AES_CTR_IV_CONTEXT_LEN;
1957         sa_info[i].tx_payload_info.encOffset = netTest_MAC_HEADER_LEN +
1958                                                                      netTest_IP_HEADER_LEN +
1959                                                                     netTest_ESP_HEADER_LEN +
1960                                                                     netTest_AES_CTR_IV_PACKET_LEN;
1965         /* dynamic configuration, will  be calculated on the fly */
1966         sa_info[i].tx_payload_info.authSize = 0;
1967         sa_info[i].tx_payload_info.encSize = 0;
1968         sa_info[i].tx_payload_info.ploadLen = 0;
1969         sa_info[i].tx_payload_info.pEncIV = 0;
1970         sa_info[i].tx_payload_info.pPkt = 0;
1972         sa_info[i].cipherMode = NWAL_SA_EALG_AES_CTR;
1973         sa_info[i].inner_ip_offset = sa_info[i].tx_payload_info.encOffset;
1974         sa_info[i].auth_tag_size = netTest_ICV_LEN;  /* icv or mac size,. always 12 except for AES_CCM/AES_GCM */
1975 #ifdef EXPERIMENTAL
1976         sa_info[i].iv_len=8;
1977         sa_info[i].bl=8;
1978         sa_info[i].spi = tx_sa[i].spi;
1979         sa_info[i].src =*((unsigned int *)(&config.local_ipsec_ip.ipv4[0]));
1980         sa_info[i].dst =*((unsigned int *)(&config.remote_ipsec_ip.ipv4[0]));
1981 #endif
1983         sa_info[i].tx_pkt_info.enetPort = 0;
1984         sa_info[i].tx_pkt_info.ipOffBytes = sa_info[i].tx_payload_info.encOffset;
1985         sa_info[i].tx_pkt_info.l4HdrLen = netTest_UDP_HEADER_LEN; /*UDP header len */
1986         sa_info[i].tx_pkt_info.l4OffBytes = sa_info[i].inner_ip_offset + netTest_IP_HEADER_LEN;
1987         sa_info[i].tx_pkt_info.startOffset = 0;
1988         sa_info[i].tx_pkt_info.lpbackPass = 0;
1989         sa_info[i].tx_pkt_info.ploadLen = 0; /*sa_info[i].tx_pkt_info.l4OffBytes + 4 */
1990         sa_info[i].tx_pkt_info.pPkt = NULL;
1991         sa_info[i].tx_pkt_info.saOffBytes = netTest_MAC_HEADER_LEN  + netTest_IP_HEADER_LEN;
1992         sa_info[i].tx_pkt_info.saPayloadLen = 0;
1993         sa_info[i].tx_pkt_info.pseudoHdrChecksum =0;
1994        
1995         sa_info[i].tx_pkt_info.txFlag1 = NWAL_TX_FLAG1_DO_IPSEC_CRYPTO| NWAL_TX_FLAG1_DO_UDP_CHKSUM| NWAL_TX_FLAG1_META_DATA_VALID ;
1997        trie_insert(p_trie_sa,(char *)&(tx_sa[i].spi),4, (void *) &sa_info[i]); //asociate with tx sa SPI
1998     }
1999     else if ((tx_sa[i].authMode == NWAL_SA_AALG_HMAC_SHA2_256) && (tx_sa[i].cipherMode == NWAL_SA_EALG_3DES_CBC))
2000     {
2001         /* static configuration, will not change */
2002         sa_info[i].tx_payload_info.aadSize = 0;
2003         sa_info[i].tx_payload_info.pAad = NULL;
2004         sa_info[i].tx_payload_info.authIvSize = 0;
2005         sa_info[i].tx_payload_info.pAuthIV = NULL;
2006         sa_info[i].tx_payload_info.authOffset = netTest_MAC_HEADER_LEN  + netTest_IP_HEADER_LEN;        /*done: same for all cipher suites */
2007         sa_info[i].tx_payload_info.encIvSize = netTest_3DES_CBC_IV_LEN;
2008         sa_info[i].tx_payload_info.encOffset = netTest_MAC_HEADER_LEN +
2009                                                                      netTest_IP_HEADER_LEN +
2010                                                                     netTest_ESP_HEADER_LEN +
2011                                                                     netTest_3DES_CBC_IV_LEN;
2013         /* dynamic configuration, will  be calculated on the fly */
2014         sa_info[i].tx_payload_info.authSize = 0;
2015         sa_info[i].tx_payload_info.encSize = 0;
2016         sa_info[i].tx_payload_info.ploadLen = 0;
2017         sa_info[i].tx_payload_info.pEncIV = 0;
2018         sa_info[i].tx_payload_info.pPkt = 0;
2020         sa_info[i].cipherMode = NWAL_SA_EALG_3DES_CBC;
2021         sa_info[i].inner_ip_offset = sa_info[i].tx_payload_info.encOffset;
2022         sa_info[i].auth_tag_size = netTest_ICV_LEN;  /* icv or mac size,. always 12 except for AES_CCM/AES_GCM */
2023 #ifdef EXPERIMENTAL
2024         sa_info[i].iv_len=8;
2025         sa_info[i].bl=8;
2026         sa_info[i].spi = tx_sa[i].spi;
2027         sa_info[i].src =*((unsigned int *)(&config.local_ipsec_ip.ipv4[0]));
2028         sa_info[i].dst =*((unsigned int *)(&config.remote_ipsec_ip.ipv4[0]));
2029 #endif
2031         sa_info[i].tx_pkt_info.enetPort = 0;
2032         sa_info[i].tx_pkt_info.ipOffBytes = sa_info[i].tx_payload_info.encOffset;
2033         sa_info[i].tx_pkt_info.l4HdrLen = netTest_UDP_HEADER_LEN; /*UDP header len */
2034         sa_info[i].tx_pkt_info.l4OffBytes = sa_info[i].inner_ip_offset + netTest_IP_HEADER_LEN;
2035         sa_info[i].tx_pkt_info.startOffset = 0;
2036         sa_info[i].tx_pkt_info.lpbackPass = 0;
2037         sa_info[i].tx_pkt_info.ploadLen = 0; /*sa_info[i].tx_pkt_info.l4OffBytes + 4 */
2038         sa_info[i].tx_pkt_info.pPkt = NULL;
2039         sa_info[i].tx_pkt_info.saOffBytes = netTest_MAC_HEADER_LEN  + netTest_IP_HEADER_LEN;
2040         sa_info[i].tx_pkt_info.saPayloadLen = 0;
2041         sa_info[i].tx_pkt_info.pseudoHdrChecksum =0;
2042        
2043         sa_info[i].tx_pkt_info.txFlag1 = NWAL_TX_FLAG1_DO_IPSEC_CRYPTO| NWAL_TX_FLAG1_DO_UDP_CHKSUM| NWAL_TX_FLAG1_META_DATA_VALID ;
2045        trie_insert(p_trie_sa,(char *)&(tx_sa[i].spi),4, (void *) &sa_info[i]); //asociate with tx sa SPI
2046     }
2047     else if ((tx_sa[i].authMode == NWAL_SA_AALG_HMAC_MD5) && (rx_sa[i].cipherMode == NWAL_SA_EALG_NULL))
2048     {
2049         /* static configuration, will not change */
2050         sa_info[i].tx_payload_info.aadSize = 0;
2051         sa_info[i].tx_payload_info.pAad = NULL;
2052         sa_info[i].tx_payload_info.authIvSize = 0;
2053         sa_info[i].tx_payload_info.pAuthIV = NULL;
2054         sa_info[i].tx_payload_info.authOffset = netTest_MAC_HEADER_LEN  + netTest_IP_HEADER_LEN;        /*done: same for all cipher suites */
2055         sa_info[i].tx_payload_info.encIvSize = netTest_NULL_IV_LEN;
2057         sa_info[i].tx_payload_info.encOffset =         netTest_MAC_HEADER_LEN +
2058                                                                      netTest_IP_HEADER_LEN +
2059                                                                     netTest_ESP_HEADER_LEN +
2060                                                                     netTest_NULL_IV_LEN;
2061 #ifdef EXPERIMENTAL
2062         sa_info[i].iv_len=0;
2063         sa_info[i].bl=4;
2064         sa_info[i].spi = tx_sa[i].spi;
2065         sa_info[i].src =*((unsigned int *)(&config.local_ipsec_ip.ipv4[0]));
2066         sa_info[i].dst =*((unsigned int *)(&config.remote_ipsec_ip.ipv4[0]));
2067 #endif
2070         /* dynamic configuration, will  be calculated on the fly */
2071         sa_info[i].tx_payload_info.authSize = 0;
2072         sa_info[i].tx_payload_info.encSize = 0;
2073         sa_info[i].tx_payload_info.ploadLen = 0;
2074         sa_info[i].tx_payload_info.pEncIV = 0;
2075         sa_info[i].tx_payload_info.pPkt = 0;
2077         sa_info[i].cipherMode = NWAL_SA_EALG_NULL;
2078         sa_info[i].inner_ip_offset = sa_info[i].tx_payload_info.encOffset;
2079         sa_info[i].auth_tag_size = netTest_ICV_LEN;  /* icv or mac size,. always 12 except for AES_CCM/AES_GCM */
2081         sa_info[i].tx_pkt_info.enetPort = 0;
2082         sa_info[i].tx_pkt_info.ipOffBytes = sa_info[i].tx_payload_info.encOffset;
2083         sa_info[i].tx_pkt_info.l4HdrLen = netTest_UDP_HEADER_LEN; /*UDP header len */
2084         sa_info[i].tx_pkt_info.l4OffBytes = sa_info[i].inner_ip_offset + netTest_IP_HEADER_LEN;
2085         sa_info[i].tx_pkt_info.startOffset = 0;
2086         sa_info[i].tx_pkt_info.lpbackPass = 0;
2087         sa_info[i].tx_pkt_info.ploadLen = 0; /*sa_info[i].tx_pkt_info.l4OffBytes + 4 */
2088         sa_info[i].tx_pkt_info.pPkt = NULL;
2089         sa_info[i].tx_pkt_info.saOffBytes = netTest_MAC_HEADER_LEN  + netTest_IP_HEADER_LEN;
2090         sa_info[i].tx_pkt_info.saPayloadLen = 0;
2091         sa_info[i].tx_pkt_info.pseudoHdrChecksum =0;
2092        
2093         sa_info[i].tx_pkt_info.txFlag1 = NWAL_TX_FLAG1_DO_IPSEC_CRYPTO| NWAL_TX_FLAG1_DO_UDP_CHKSUM| NWAL_TX_FLAG1_META_DATA_VALID ;
2095        trie_insert(p_trie_sa,(char *)&(tx_sa[i].spi),4, (void *) &sa_info[i]); //asociate with tx sa SPI
2096     }
2097     else
2098         printf("build_sa_db(): invalid encryption/authentication combination selected\n");
2100     //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);
2101     
2102     
2107 #define CHECK_SET_PARAM(ARG1, ARG2)     \
2108     do { \
2109         if(strcmp(key, ARG1) == 0) { \
2110         if(data)strncpy(ARG2,data,CONFIG_STRING_LEN); \
2111         printf("CHECK_SET_PARM, match found, input cong string %s\n", ARG2); \
2112         continue; \
2113         } \
2114     } while(0)
2116 #define CHECK_SET_PARAM2(ARG1, ARG2,  ARG3)     \
2117     do { \
2118         if(strcmp(key, ARG1) == 0) { \
2119         if(data) strncpy(ARG2,data,CONFIG_STRING_LEN); \
2120         if(data2) strncpy(ARG3,data2,CONFIG_STRING_LEN); \
2121         printf("CHECK_SET_PARM, match found, input cong string %s %s\n", ARG2, ARG3); \
2122         continue; \
2123         } \
2124     } while(0)
2126 unsigned char hex2dec(char *p_s)
2128   int val;
2129   sscanf(p_s,"%x",&val); 
2130  return val&0xff;
2132 void parse_one_mac(char * p_mac_str, unsigned char *p_mac)
2134     int index = 0;
2135     int i;
2136     char *pch = strtok (&(p_mac_str[0]),"-");
2138     while (pch != NULL)
2139     {
2140          printf ("%s\n",pch);
2141          p_mac[index] = hex2dec(pch);
2142          index++;
2143          pch = strtok (NULL,"-");
2144     }
2145      printf("index value : %d\n", index);
2146     for (i=0; i<6;i++)
2147          printf("************mac0[%d]: 0x%x\n",i,  p_mac[i]);
2150 void parse_one_ip(char * p_ip_addr_str, unsigned char * p_ip)
2152     int index = 0;
2153     int i;
2154     char * pch = strtok (&p_ip_addr_str[0],".");
2156      while (pch != NULL)
2157      {
2158         printf ("xxxxx: %s\n",pch);
2159         p_ip[index] = atoi(pch);
2160         index++;
2161         pch = strtok (NULL,".");
2162     }
2163     printf("index value : %d\n", index);
2164     for (i=0; i<4;i++)
2165         printf("************ip[%d]: 0x%x\n",i, p_ip[i]);
2170 void parse_mac_address(netTestConfigFile_t *pConfig)
2172     if (strlen(&pConfig->mac0[0]))
2173         parse_one_mac(&pConfig->mac0[0],&config.mac0[0]);
2174     if (strlen(&pConfig->mac1[0]))
2175         parse_one_mac(&pConfig->mac1[0],&config.mac1[0]);
2177 void parse_ip_address(netTestConfigFile_t *pConfig)
2179     if (strlen(&pConfig->ip0[0]))
2180         parse_one_ip(&pConfig->ip0[0],&config.ip0.ipv4[0]);
2181     if (strlen(&pConfig->ip1[0]))
2182         parse_one_ip(&pConfig->ip1[0],&config.ip1.ipv4[0]);
2183     if (strlen(&pConfig->ip2[0]))
2184         parse_one_ip(&pConfig->ip2[0],&config.ip2.ipv4[0]);
2185     if (strlen(&pConfig->local_ipsec_ip[0]))
2186         parse_one_ip(&pConfig->local_ipsec_ip[0],&config.local_ipsec_ip.ipv4[0]);
2187     if (strlen(&pConfig->remote_ipsec_ip[0]))
2188     parse_one_ip(&pConfig->remote_ipsec_ip[0],&config.remote_ipsec_ip.ipv4[0]);
2192 void parse_ipsec_mode(netTestConfigFile_t *pConfig)
2195     printf("parse_ipsec_mode, string length %d\n", strlen(&pConfig->ipsec_mode_rx));
2196     if (strlen(&pConfig->ipsec_mode_rx[0]))
2197     {
2198         if (strcmp(pConfig->ipsec_mode_rx, "SIDEBAND") == 0)
2199         {
2200             config.ipsec_mode_rx = IPSEC_MODE_RX_SIDEBAND;
2201         }
2202         else if (strcmp(pConfig->ipsec_mode_rx, "INFLOW") == 0)
2203         {
2204             config.ipsec_mode_rx = IPSEC_MODE_RX_INFLOW;
2205         }
2206         else
2207             printf("parse_ipsec_mode(), invalid RX ipsec mode in config file \n");
2208     }
2210     if (strlen(&pConfig->ipsec_mode_tx[0]))
2211     {
2212         if (strcmp(pConfig->ipsec_mode_tx, "SIDEBAND") == 0)
2213         {
2214             config.ipsec_mode_tx = IPSEC_MODE_TX_SIDEBAND;
2215         }
2216         else if (strcmp(pConfig->ipsec_mode_tx, "INFLOW") == 0)
2217         {
2218             config.ipsec_mode_tx = IPSEC_MODE_TX_INFLOW;
2219         }
2220     else
2221         printf("parse_ipsec_mode(), invalid TX ipsec mode in config file \n");
2222     }
2224     printf("parse_ipsec_mode(): RX mode %d\n", config.ipsec_mode_rx);
2225     printf("parse_ipsec_mode(): TX mode %d\n", config.ipsec_mode_tx);
2227     
2230 #ifdef EXPERIMENTAL
2231 int n_routes=0;
2232 int n_dst_ips=0;
2233 void parse_routes(netTestConfigFile_t *pConfig)
2235 int i;
2236 int said=0;
2237 for(i=0;i<MAX_ROUTES;i++)
2239    int port;
2240    if (pConfig->routes[i][0])
2241    {
2242       port=atoi(&pConfig->ports[i][0]);
2243       if((port<1)||(port>2)) continue; //bad port #: only 1 or 2 valid
2244       if(strncmp(&pConfig->routes[i][0],"MAC",3)==0)
2245       {
2246          routes[i].out_port = port;
2247          parse_one_mac(&pConfig->routes[i][3],&routes[i].out_mac[0]);
2248          memcpy(&routes[i].out_mac[6], ((port==1) ?&config.mac0[0]: &config.mac1[0] ),6); 
2249          routes[i].out_mac[12]=0x08;
2250          routes[i].out_mac[13]=0x00;
2251          routes[i].sec_ptr=NULL;
2252          n_routes+=1;
2253       }
2254       else if (strncmp(&pConfig->routes[i][0],"SA",2)==0)
2255       {      
2256          said=atoi(&pConfig->routes[i][2]) ;
2257          routes[i].sec_ptr=&sa_info[said];
2258          n_routes+=1;
2259       }
2260    }
2262 our_router = route_init();
2263 for (i=0;i<MAX_ROUTES;i++)
2265   unsigned long ip_be;
2266   int route_index;
2267   if (pConfig->dst_ips[i][0])
2268   {
2269      parse_one_ip(&pConfig->dst_ips[i][0],(unsigned char *)&ip_be);
2270      sscanf(&pConfig->paths[i][0],"route%d",&route_index);
2271      route_add(our_router,&ip_be,&routes[route_index]);
2272      n_dst_ips+=1;
2273   }
2275 printf(">Route DB built.  %d entries\n",n_dst_ips);
2277 #endif
2278 static void parse_config_file(FILE * fpr, netTestConfigFile_t *pConfig)
2280     char line[MAX_LINE_LENGTH + 1];
2281     int i;
2282  
2283     char *key, *data, *ep, *data2;
2284     char tokens[] = " :=;\n";
2285     char temp_str[50];
2286     memset(line, 0, MAX_LINE_LENGTH + 1);
2287     memset(pConfig, 0, sizeof(netTestConfigFile_t));
2288     while (fgets(line, MAX_LINE_LENGTH + 1, fpr)) 
2289     {
2290         if(line[0]=='#') continue; //skip comment
2291         key  = (char *)strtok(line, tokens);
2292         data = (char *)strtok(NULL, tokens);
2293         data2 = (char *)strtok(NULL, tokens);
2294         if (!key) continue;
2295         if (!data) continue;
2297         if(strlen(data) == 0) 
2298         {
2299             continue;
2300         }
2302          CHECK_SET_PARAM(INIT_CONFIG_MAC0,&(pConfig->mac0[0]));
2303          CHECK_SET_PARAM(INIT_CONFIG_MAC1,&(pConfig->mac1[0]));
2304          CHECK_SET_PARAM(INIT_CONFIG_IP0,&(pConfig->ip0[0]));
2305          CHECK_SET_PARAM(INIT_CONFIG_IP1,&(pConfig->ip1[0]));
2306          CHECK_SET_PARAM(INIT_CONFIG_IP2,&(pConfig->ip2[0]));
2307          CHECK_SET_PARAM(INIT_CONFIG_LOCAL_IPSEC_IP,&(pConfig->local_ipsec_ip[0]));
2308          CHECK_SET_PARAM(INIT_CONFIG_REMOTE_IPSEC_IP,&(pConfig->remote_ipsec_ip[0]));
2309          CHECK_SET_PARAM(INIT_CONFIG_IPSEC_MODE_RX,&(pConfig->ipsec_mode_rx[0]));
2310          CHECK_SET_PARAM(INIT_CONFIG_IPSEC_MODE_TX,&(pConfig->ipsec_mode_tx[0]));
2311          CHECK_SET_PARAM(INIT_CONFIG_IPSEC_IF_NO,&(pConfig->ipsec_if_no[0]));
2312 #ifdef EXPERIMENTAL
2313     for(i=0;i<MAX_ROUTES;i++)
2314     {
2315         sprintf(temp_str,"route%d",i);
2316         CHECK_SET_PARAM2(temp_str,&pConfig->routes[i][0],&pConfig->ports[i][0] );
2317     }
2318     for(i=0;i<MAX_ROUTES;i++)
2319     {
2320         sprintf(temp_str,"dstip%d",i);
2321         CHECK_SET_PARAM2(temp_str,&pConfig->dst_ips[i][0],&pConfig->paths[i][0] );
2322     }
2323 #endif
2324          
2325     }
2326     parse_mac_address(pConfig);
2328     parse_ip_address(pConfig);
2330     parse_ipsec_mode(pConfig);
2331 #ifdef EXPERIMENTAL
2332      if (strlen(&pConfig->ipsec_if_no[0]))
2333         config.ipsec_if_no = atoi(&pConfig->ipsec_if_no[0]);
2334     parse_routes(pConfig);
2335 #endif
2338 static  netTestConfigFile_t config_file;
2339 /***************************************
2340  ********** test driver*****************
2341  ***************************************/
2342 int main(int argc, char **argv)
2344     int err,i;
2345     rlim_t oss,ss = 1024*1024;
2346     struct rlimit rl;
2348     Pktlib_HeapCfg      heapCfg;
2349     int32_t             errCode;
2350     Pktlib_HeapIfTable*  pPktifTable;
2351  
2352     FILE * fpr = NULL;
2356     err= getrlimit(RLIMIT_STACK,&rl);
2357     if (!err) printf(" stack limit = %d\n",rl.rlim_cur); else printf("getrlimit failed\n");
2358 #if 0
2359     rl.rlim_cur = ss;
2360     err=setrlimit(RLIMIT_STACK,&rl);
2361 i   f (!err) printf("set stack to %d\n",rl.rlim_cur); else printf("setrlimit failed\n");
2362 #endif
2365     if (argc>=2)  np2process = atoi(argv[1]);
2366         printf("*************** np2process %d\n", np2process);
2367     if (np2process<0) np2process = NP; /* default */
2368     if (argc==3)  perslow = atoi(argv[2]);
2369     if ((perslow<0)||(perslow>100)) perslow=PERSLOW;//default
2370     if (argc>3) {printf("net_test  <no of pkts to process> <percent slow path>\n"); exit(1);}
2371 #if 1
2373 #if 0
2374     if (argc >= 2) 
2375     {
2376         fpr = fopen(argv[2], "r");
2377         if (fpr == NULL) 
2378         {
2379             printf("Error in opening %s input file\n", argv[2]);
2380         }
2381     } 
2382     else
2383 #endif
2384     {
2385         fpr = fopen(input_file_name, "r");
2386         if (fpr == NULL) 
2387         {
2388             printf("Error in opening %s input file\n", input_file_name);
2389         }
2390         else
2391         {
2392             parse_config_file(fpr,&config_file);
2393         }
2394     }
2396 #endif
2398     memset(&sa_info, 0, sizeof(sa_info));
2401 //real mode, so update our test packet mac header and ip header
2402 if (pktloopback==0)
2404 memcpy(&testPkt,&real_mac_header[0],14); //overwrite test pkt mac address
2405 memcpy(&testPkt[26],&real_ip_addr[0],8);//overrite test pkt ip addresses
2407 #if 0 
2408 our_router = route_init();
2409 for (i=0;i<n_dst_ips;i++)
2411   route_add(our_router,&ip[i],&routes[i]);
2413 #endif
2415 /*******************************************/
2416 /*************NETAPI STARTUP****************/
2417 /*******************************************/
2419 /* create netapi */
2420 netapi_handle = netapi_init(NETAPI_SYS_MASTER, &our_netapi_default_cfg);
2422 /* open the main heap */
2423 OurHeap = Pktlib_findHeapByName("netapi");
2424 if (!OurHeap) {printf("findheapbyname fail\n"); exit(1);}
2426 /* create two secondary heaps */
2427 /* Initialize the heap configuration. */
2428 memset ((void *)&heapCfg, 0, sizeof(Pktlib_HeapCfg));
2430 pPktifTable = netapi_getPktlibIfTable();
2431 /* Populate the heap configuration */
2432 heapCfg.name                = "netapi-small";
2433 heapCfg.memRegion           = NETAPI_GLOBAL_REGION;
2434 heapCfg.sharedHeap          = 1;
2435 heapCfg.useStarvationQueue  = 0;
2436 heapCfg.dataBufferSize      = 512;
2437 heapCfg.numPkts             = 64;
2438 heapCfg.numZeroBufferPackets= 0;
2439 heapCfg.heapInterfaceTable.data_malloc  = pPktifTable->data_malloc;
2440 heapCfg.heapInterfaceTable.data_free    = pPktifTable->data_free;
2441 heapCfg.dataBufferPktThreshold   = 0;
2442 heapCfg.zeroBufferPktThreshold   = 0;
2444 specialSmall = Pktlib_createHeap(&heapCfg, &errCode);
2445 heapCfg.name                = "netapi-big";
2446 heapCfg.dataBufferSize      = 1600;
2447 specialLarge = Pktlib_createHeap(&heapCfg, &errCode);
2448 //register these heaps so poll routine will include their garbage queues.
2449 netapi_registerHeap(netapi_handle, specialSmall);
2450 netapi_registerHeap(netapi_handle, specialLarge);
2452 #ifdef REASSEMBLE_BENCH
2453 our_reassemble_bench(2);
2454 exit(1);
2455 #endif
2457 #ifndef EXPERIMENTAL
2458 /* create a pktio channel */
2459 our_chan=pktio_create(netapi_handle,"our1stq",(PKTIO_CB) recv_cb_bench, &our_chan_cfg,&err);
2460 if (!our_chan) {printf("pktio create failed err=%d\n",err); exit(1);}
2461 #endif
2463 /* open netcp default tx, rx queues */
2464 netcp_tx_chan= pktio_open(netapi_handle, NETCP_TX, NULL, &netcp_tx_cfg,  &err);
2465 if (!netcp_tx_chan) {printf("pktio open TX failed err=%d\n",err); exit(1);}
2466 #ifdef EXPERIMENTAL
2467 netcp_rx_chan= pktio_open(netapi_handle, NETCP_RX, (PKTIO_CB) recv_cb_router, &netcp_rx_cfg,  &err);
2468 #else
2469 netcp_rx_chan= pktio_open(netapi_handle, NETCP_RX, (PKTIO_CB) recv_cb, &netcp_rx_cfg,  &err);
2470 #endif
2471 if (!netcp_rx_chan) {printf("pktio open RX failed err=%d\n",err); exit(1);}
2473 #ifndef EXPERIMENTAL
2474 /* create a pktio channel for specially classified pkts */
2475 netcp_rx_chan2= pktio_create(netapi_handle, "classq", (PKTIO_CB) recv_cb, &netcp_rx_cfg2,  &err);
2476 if (!netcp_rx_chan2) {printf("pktio create RX2 failed err=%d\n",err); exit(1);}
2478 /* open netcp default tx, rx queues for sideband crypto */
2479 netcp_sb_tx_chan= pktio_open(netapi_handle, NETCP_SB_TX, NULL, &netcp_sb_tx_cfg,  &err);
2480 if (!netcp_sb_tx_chan) {printf("pktio open SB TX failed err=%d\n",err); exit(1);}
2481 netcp_sb_rx_chan= pktio_open(netapi_handle, NETCP_SB_RX, (PKTIO_CB) recv_sb_cb, &netcp_sb_rx_cfg,  &err);
2482 if (!netcp_sb_rx_chan) {printf("pktio open SB RX failed err=%d\n",err); exit(1);}
2483 #endif
2485 printf("net_test> %d bytes left in our CMA area\n", netapi_getBufMemRemainder());
2486 /* create scheduler instance */
2487 our_sched =netapi_schedOpen(netapi_handle,&our_sched_cfg, &err);
2488 if (!our_sched) {printf("sched create failed\n"); exit(1);}
2490 #if 0
2491 /********************************************
2492 * Basic pkt loopback test
2493 *********************************************/
2494 printf("...running pure push/pop benchmark\n");
2495 our_pktio_bench(1000);
2496 our_pktio_bench(1000);
2497 our_pktio_bench(1000);
2498 our_pktio_bench(1000);
2499 our_pktio_bench(1000);
2500 /*********************************************/
2501 #endif
2506 /* add mac intefaces */
2507 netcp_cfgCreateMacInterface(
2508                   netapi_handle,
2509                   &config.mac0[0],
2510                   0,0,
2511                   (NETCP_CFG_ROUTE_HANDLE_T)  NULL,
2512                   (NETCP_CFG_VLAN_T ) NULL ,  //future
2513                   1, 
2514                   &err);
2515 if (err) {printf("addmac0 failed %d\n",err); exit(1); } 
2517 //attach an IP to this interface
2518 ip_rule0=netcp_addIp(
2519                   netapi_handle,
2520                   0,
2521                   nwal_IPV4,
2522                   &config.ip0,
2523                   NULL,  //all IP
2524                   (NETCP_CFG_ROUTE_HANDLE_T) NULL,
2525                   &err
2526                   );
2527 if (err) {printf("addip0 failed %d\n",err); exit(1); } 
2529 //create a 2nd mac instance
2530 netcp_cfgCreateMacInterface(
2531                   netapi_handle,
2532                   &config.mac1[0],
2533                   1,1,
2534                   (NETCP_CFG_ROUTE_HANDLE_T)  NULL,
2535                   (NETCP_CFG_VLAN_T ) NULL ,  //future
2536                   1,
2537                   &err);
2538 if (err) {printf("addmac1 failed %d\n",err); exit(1); }
2540 //attach an IP to this interface
2541 ip_rule1=netcp_addIp(
2542                   netapi_handle,
2543                   1,
2544                   nwal_IPV4,
2545                   &config.ip1,
2546                   NULL,  //all IP
2547                   (NETCP_CFG_ROUTE_HANDLE_T) NULL,
2548                   &err
2549                   );
2550 if (err) {printf("addip1 failed %d\n",err); exit(1); }
2552 #ifndef EXPERIMENTAL
2553 //attach 2 classifiers to iface 0, ip0
2554 class_0_cfg.u.c_l4.ip = ip_rule0;
2555 class_0 =  netcp_cfgAddClass(netapi_handle,
2556                              &class_0_cfg,
2557                              NULL,
2558                              NETCP_CFG_ACTION_TO_SW,
2559                              &err);
2560 if (err) {printf("addclass0 failed %d\n",err); exit(1);}
2562 class_1_cfg.u.c_l4.ip = ip_rule0;
2563 class_1 =  netcp_cfgAddClass(netapi_handle,
2564                              &class_1_cfg,
2565                              NULL,
2566                              NETCP_CFG_ACTION_TO_SW,
2567                              &err);
2568 if (err) {printf("addclass1 failed %d\n",err); exit(1);}
2571 //3rd classifier has a different IP and route
2572 class_2_cfg.u.c_l3_l4.ip_addr = &config.ip2;
2574 //create specialFlow for this classifier
2576 Pktlib_HeapHandle heaps[2];
2577 int sizes[2];
2578 heaps[0]= specialSmall;
2579 heaps[1]= specialLarge;
2580 #define SPECIAL_SOP_OFF 128
2581 sizes[0]=512-SPECIAL_SOP_OFF;
2582 sizes[1]=1600-SPECIAL_SOP_OFF;
2583 #if 0
2584 specialFlow = netcp_cfgAddFlow( netapi_handle,
2585                                 2,
2586                                 heaps,
2587                                 sizes,
2588                                 SPECIAL_SOP_OFF,  //offset to start rx is 128 
2589                                 &err);
2590 if (err) {printf("add flow failed\n", err); exit(1);}
2591 #endif
2593 #if 0
2594 //special route for this classifier:  different flow + destination q
2595 class2_route.p_dest_q = netcp_rx_chan2;
2596 class2_route.p_flow = specialFlow;
2597 class_2 = netcp_cfgAddClass(netapi_handle,
2598                             &class_2_cfg,
2599                             (void*) &class2_route,
2600                             NETCP_CFG_ACTION_TO_SW,
2601                             &err);
2602 if (err) {printf("addclass2 failed %d\n",err); exit(1);}
2603 #endif
2605 #endif
2606 //security stuff 
2607     p_trie_sa = trie_new();
2608    if (!p_trie_sa) {printf("trie alloc for SA  failed\n"); exit(1);}
2612     for (i=0; i < MAX_SEC_INDEX;i++)
2613     {
2614         ourRXKeyParams[i].pEncKey = &ourEncrKey[0];
2615         ourRXKeyParams[i].pAuthKey = &ourAuthKey[0];
2616         memcpy(&(rx_sa[i].src), &config.remote_ipsec_ip,4);
2617         memcpy(&(rx_sa[i].dst), &config.local_ipsec_ip,4);
2619         build_sa_db(i);
2620         sa_info[i].rx_tunnel = netapi_secAddSA(
2621             netapi_handle,
2622             config.ipsec_if_no, //iface #0 
2623             &rx_sa[i],
2624             &ourRXKeyParams[i],
2625             config.ipsec_mode_rx == IPSEC_MODE_RX_SIDEBAND ? NETAPI_SEC_SA_SIDEBAND: NETAPI_SEC_SA_INFLOW,
2626                 NULL,  //use default route 
2627             &(sa_info[i].rx_data_mode_handle),
2628             &(sa_info[i].rx_inflow_mode_handle),
2629             &err);
2630         if (err) {printf("addRxSa failed %d\n",err); exit(1);}
2632     if (config.ipsec_mode_rx == IPSEC_MODE_RX_INFLOW)
2633     {
2634         //assume inner and outer ip is the same
2635         rx_policy[i]= netapi_secAddRxPolicy( netapi_handle,
2636                          sa_info[i].rx_tunnel,  //link to tunnel above
2637                          4,         //ipv4
2638                          &config.remote_ipsec_ip, //src -> them
2639                          &config.local_ipsec_ip,  //dst -> us
2640                         NULL,  // no qualifiers
2641                         NULL,  //default route
2642                          &err);
2643         if (err) {printf("addSaPolicy failed  %d, for index %d\n",err,i); exit(1);}
2644     }
2645     else 
2646         rx_policy[i] = 0;
2647     }
2649 //tx SA
2650 //security stuff 
2651      for (i=0; i < MAX_SEC_INDEX;i++)
2652     {
2653     ourTXKeyParams[i].pEncKey = &ourEncrKey[0];
2654     ourTXKeyParams[i].pAuthKey = &ourAuthKey[0];
2655     memcpy(&(tx_sa[i].src), &config.local_ipsec_ip,4);
2656     memcpy(&(tx_sa[i].dst), &config.remote_ipsec_ip,4);
2657     sa_info[i].tx_tunnel = netapi_secAddSA( netapi_handle,
2658                  0, //iface #0 
2659                 &(tx_sa[i]),
2660                 &ourTXKeyParams[i],
2661                 config.ipsec_mode_tx == IPSEC_MODE_TX_SIDEBAND ? NETAPI_SEC_SA_SIDEBAND: NETAPI_SEC_SA_INFLOW,
2662                 NULL,  //use default route 
2663                 &(sa_info[i].tx_data_mode_handle),
2664                 &(sa_info[i].tx_inflow_mode_handle),
2665                 &err);
2666 if (err) {printf("addTxSa failed %d\n",err); exit(1);}
2668     }
2669 #ifdef TEST_TIMERS
2670 //timers
2671 ourTimerBlock = netapi_TimerGroupCreate(
2672         netapi_handle,
2673         "our1sttimer",
2674         our_timer_cb,
2675         0,    //1 if timers local to thread
2676         0,    //1 if expect to cancel
2677         netapi_getTicksPerSec()/1000,  /* 1 msc resolution for these timers */
2678         netapi_getTicksPerSec()/5000, /* would like .5 msc tolerence */
2679         10,  //small # of timers to test garbage collection
2680         &err);
2681 if (err) {printf("timergroupcreate failed %d\n",err); exit(1);}
2683 //start a couple of timers 
2684 t1 = netapi_TimerGroupStartTimer(
2685         ourTimerBlock,
2686         (void *) 1,
2687         100LL,  //timer group ticks
2688         &err);
2689 if (err) {printf("timerstart failed %d\n");}
2690 t2 = netapi_TimerGroupStartTimer(
2691         ourTimerBlock,
2692         (void *) 2,
2693         200LL,  //timer group ticks
2694         &err);
2695 if (err) {printf("timerstart failed %d\n");}
2696 t3 = netapi_TimerGroupStartTimer(
2697         ourTimerBlock,
2698         (void *) 3,
2699         300LL,  //timer group ticks
2700         &err);
2701 if (err) {printf("timerstart failed %d\n");}
2702 #endif
2703 netcp_cfgReqStats(netapi_handle, our_stats_cb, 1,&err);
2704 if (err!=0) {printf("stats req failed\n");}
2706 /*********************************************/
2707 /*****************end NETAPI STARTUP**********/
2708 /*********************************************/
2711 #if 0
2712 /********************************************
2713 * Basic pkt loopback test
2714 *********************************************/
2715 printf("...runnining pure push/pop benchmark\n");
2716 our_pktio_bench(100);
2717 #endif
2719 /**************unused stuff******************/
2720 #if 0
2721 /* create TRIE */
2722 P_trie = trie_new();
2723 if (!P_trie) {printf("trie alloc failed\n"); exit(1);}
2725 nat = (HEAD_T *) malloc(NE * sizeof(HEAD_T));
2726 if (!nat) {printf("malloc of nat table failed\n"); exit(1);}
2728 //gen_pkts(np2process<NP ? np2process:NP);
2729 n_pkt= np2process;
2731 /* build table */
2732 build_table(P_trie);
2735 /* processing loop: get pkt, check it, look up in table, copy new header,
2736    send packet */
2737 srand((unsigned) np2process);
2738 #endif
2740 /*********************************************/
2741 /**************Entry point into scheduler ****/
2742 /*********************************************/
2743 netapi_schedWaitForEvents(our_sched, &err);
2745 /* done */
2746 our_stats_cb(netapi_handle, NULL);
2749 #define DO_FAST_POLL
2750 #ifdef DO_FAST_POLL
2751 //example_fast_pushpop(OurHeap, 500);
2752 //example_fast_poll(netcp_rx_chan,20000);
2753 #endif
2755 /*************************************************
2756  ************CLEAN UP****************************
2757  ************************************************/
2759 #ifndef EXPERIMENTAL
2760 //delete Classifiers
2761 netcp_cfgDelClass(netapi_handle, class_0, &err); 
2762 netcp_cfgDelClass(netapi_handle, class_1, &err); 
2763 //netcp_cfgDelClass(netapi_handle, class_2, &err); 
2765 #if 0
2766 //delete flow 
2767 netcp_cfgDelFlow(netapi_handle, specialFlow, &err);
2768 #endif
2769 #endif
2770 #if 1
2771 //delete policy
2772      for (i=0; i < MAX_SEC_INDEX;i++)
2773     {
2774         if (rx_policy[i])
2775             netapi_secDelRxPolicy(netapi_handle, rx_policy[i], &err);
2776      
2777         //delete tunnels
2778         netapi_secDelSA(netapi_handle, 0, sa_info[i].rx_tunnel, &err);
2779         netapi_secDelSA(netapi_handle, 0, sa_info[i].tx_tunnel, &err);
2780 #endif
2781     }
2782 //delete IPs and MAC Interfacess
2783 netcp_cfgDelIp(netapi_handle, 0, 0, NULL, NULL, ip_rule0, &err);
2784 netcp_cfgDelIp(netapi_handle, 1, 0, NULL, NULL, ip_rule1, &err);
2785 netcp_cfgDelMac(netapi_handle,0,&err);
2786 netcp_cfgDelMac(netapi_handle,1,&err);
2789 //close pktio channels we opened
2790 pktio_close(netcp_tx_chan ,&err);
2791 pktio_close(netcp_rx_chan ,&err);
2792 #ifndef EXPERIMENTAL
2793 pktio_close(netcp_sb_tx_chan ,&err);
2794 pktio_close(netcp_sb_rx_chan ,&err);
2796 //clear pktio channel we created
2797 pktio_control(our_chan, (PKTIO_CB) NULL, (PKTIO_CFG_T *) NULL, &zap_channel_control, &err);
2798 pktio_control(netcp_rx_chan2, (PKTIO_CB) NULL, (PKTIO_CFG_T *) NULL, &zap_channel_control, &err);
2800 //delete pktio channels we created
2801 pktio_delete(our_chan, &err);
2802 pktio_delete(netcp_rx_chan2,&err);
2803 #endif
2804 netapi_shutdown(netapi_handle);
2808 //EXAMPLE FAST POLL
2809 /* PLD */
2810 void netapi_pld(void * x)
2811 {   
2812            asm volatile("pld [r0]");
2814 #include "ti/drv/nwal/nwal_util.h"
2816 #define M 1008
2817 static int l3_off[M], l4_off[M], L3_chk_ok[M], L4_chk_ok[M], len[M] ;
2818 static nwal_AppId appid[M];
2819 static unsigned char * buf[M];
2820 #define N2POLL 8 //batch size
2821 #define NTOPOP 190
2822 Ti_Pkt * pHd[NTOPOP];
2823 void example_fast_pushpop(Pktlib_HeapHandle h , int n_trials)
2825 int i,j;
2826 unsigned long t1;
2827 unsigned long t2;
2828 unsigned long sumf=0;
2829 unsigned long sumt=0;
2830 unsigned long sump=0;
2831 #define NTOPOP 190
2832 Ti_Pkt * tempVA;
2833 Qmss_QueueHnd freeQ=Pktlib_getInternalHeapQueue(h);
2835 //n_trials of NTOPOP actions
2836 for(i=0;i<n_trials;i++)
2838     //raw pop
2839     t1= netapi_timing_start();
2840     pHd[0] = (Ti_Pkt *)QMSS_DESC_PTR(PKTIO_QMSS_QUEUE_POP_RAW (freeQ));
2841     for(j=1;j<NTOPOP;j++)
2842     {
2843        tempVA  =  _Osal_qmssPhyToVirt(pHd[j-1]);
2844        netapi_pld(tempVA);
2845         //__builtin_prefetch(tempVA);
2846        pHd[j] = (Ti_Pkt *)QMSS_DESC_PTR(PKTIO_QMSS_QUEUE_POP_RAW (freeQ));
2847        sumf+= (int) (   ((Cppi_HostDesc *) tempVA)->origBuffPtr -
2848                   ((Cppi_HostDesc *) tempVA)->buffPtr);
2849     }
2850     tempVA  =  _Osal_qmssPhyToVirt(pHd[j-1]);
2851     sumf+= (int) (   ((Cppi_HostDesc *) tempVA)->origBuffPtr -
2852                   ((Cppi_HostDesc *) tempVA)->buffPtr);
2853     t2=netapi_timing_stop();
2854     sumt+= (t2-t1);
2856     //invalidate/wb 
2857     for(j=0;j<NTOPOP;j++)
2858     {
2859         tempVA  =  _Osal_qmssPhyToVirt(pHd[j]);
2860         netapi_utilCacheWbInv(tempVA,128);
2861     }
2863     //raw push 
2864     t1=netapi_timing_start();
2865     for(j=0;j<NTOPOP;j++)
2866     {
2867         PKTIO_QMSS_QUEUE_PUSH_DESC_SIZE_RAW (freeQ,
2868                                              (void *) pHd[j],
2869                                              128);
2870     }
2871     t2=netapi_timing_stop();
2872     sump += (t2-t1);
2874 printf("\nfast poppush: np=%d  pop=%d  push=%d sumf=%d\n", (n_trials*NTOPOP),
2875         sumt/(n_trials*NTOPOP),  sump/(n_trials*NTOPOP), sumf);
2876 sleep(1);
2877 return;
2882 void example_fast_poll( PKTIO_HANDLE_T * p_pktio, int max_pkts)
2884 int j=0;
2885 int jj=0;
2886 int i,k,l=0,ltot=0;
2887 int n= N2POLL; //max # of pkts to poll
2888 Ti_Pkt * tempVA;
2889 pasahoLongInfo_t* pinfo;
2890 unsigned long t1;
2891 unsigned long t2;
2892 unsigned long t11;
2893 unsigned long t12;
2894 unsigned long np;
2895 unsigned long sumt=0;
2896 unsigned long sumf=0;
2897 unsigned long sump=0;
2898 unsigned long totlen=0;
2899 int max_batch=0;
2901 //this should  be done once and saved
2902 Qmss_QueueHnd rxQ= PKTIO_GET_DEFAULT_NETCP_Q(p_pktio);
2903 Qmss_QueueHnd freeQ;
2904 //loop forever
2905 for(;max_pkts>0;)
2907     t1= netapi_timing_stop();
2908     pHd[0] = (Ti_Pkt *)QMSS_DESC_PTR(PKTIO_QMSS_QUEUE_POP_RAW (rxQ));
2909     if (!pHd[0]) continue;
2910     //got pkt
2911     for(i=1;(i<n) && (pHd[i-1]);i++)
2912     {
2913         //convert previous descriptor PA -> VA
2914         tempVA  =  Osal_qmssPhyToVirt(pHd[i-1]); 
2916         //try and preload desriptor
2917          __builtin_prefetch(tempVA);
2918         //netapi_pld(tempVA);
2920         //read next descriptor from queue 
2921         pHd[i] = (Ti_Pkt *)QMSS_DESC_PTR(PKTIO_QMSS_QUEUE_POP_RAW (rxQ));
2922 #if 1   
2923         /* extract some meta data */
2924         Cppi_getData (Cppi_DescType_HOST, (Cppi_Desc*)tempVA, &buf[jj], &len[jj]);
2925         pinfo =  nwal_mGetProtoInfo(tempVA);
2926         l3_off[jj]= nwal_mGetL3OffBytes(pinfo);
2927         l4_off[jj]= nwal_mGetL4Offset(pinfo);
2928         if(nwal_mGetAppidFmPkt(tempVA,&appid[jj]) != nwal_TRUE)
2929         {
2930             printf("ERROR!!!! AppID not available in incoming packet \n");
2931         }
2932 //#define VERIFY_SOP
2933 #ifdef VERIFY_SOP
2934   if (sump < 200)  printf("..sop off=%d\n", (int) buf[jj]-
2935            (int)  ((Cppi_HostDesc *) tempVA)->origBuffPtr) ;
2936 #endif
2937 #endif
2938         jj+=1;
2939     }
2940     //finish last pkt in burst
2941     if(pHd[i-1])
2942     {
2943         //convert previous descriptor PA -> VA
2944         tempVA  =  Osal_qmssPhyToVirt(pHd[i-1]); 
2946         /* extract some meta data */
2947 #if 1
2948         pinfo =  nwal_mGetProtoInfo(tempVA);
2949         l3_off[jj]= nwal_mGetL3OffBytes(pinfo);
2950         l4_off[jj]= nwal_mGetL4Offset(pinfo);
2951         if(nwal_mGetAppidFmPkt(tempVA,&appid[jj]) != nwal_TRUE)
2952         {
2953             printf("ERROR!!!! AppID not available in incoming packet \n");
2954         }
2955 #endif
2956         //get ptr (Physical address) and length of associate buffer
2957         Cppi_getData (Cppi_DescType_HOST, (Cppi_Desc*)tempVA, &buf[jj], &len[jj]);
2958         jj+=1;
2959     }
2960     t2= netapi_timing_stop();
2961     j+=(pHd[i-1]? i: (i-1)) ;
2962     if (jj>(M-n)) jj=0;
2963     l+=1;  //n batches
2964     ltot+=1;
2965     if(pHd[i-1])
2966     {
2967       if (i>max_batch) max_batch= i;
2968     }
2969     else
2970     {
2971       if( (i-1) >max_batch) max_batch = i-1;
2972     }
2974     //cleanup
2975     //printf("cleanup %d\n",i);
2976     for(k=0;k<i;k++)
2977     {
2978         //cleanup.  need to covert all of desriptor to VA so that i can use freePacket() 
2979         //alternative would be to just do cache ops plus descriptor raw push to pktlib
2980         // heap free queue
2981         if(pHd[k])
2982         {
2984         //tempVA=Qmss_osalConvertDescPhyToVirt(pHd[k]);
2985         tempVA  =  Osal_qmssPhyToVirt(pHd[k]);
2986         freeQ=Qmss_getQueueHandle(Cppi_getReturnQueue (Cppi_DescType_HOST, (Cppi_Desc *)tempVA));
2987         netapi_utilCacheWbInv(tempVA,128);
2988         //would need to wbInv buffer also in practice. Also need to walk
2989         // descriptor chain 
2990         t11= netapi_timing_stop();
2991        // Pktlib_freePacket(tempVA);
2992         PKTIO_QMSS_QUEUE_PUSH_DESC_SIZE_RAW (freeQ,
2993                                              (void *) pHd[k],
2994                                              128);
2996         t12= netapi_timing_stop();
2997         sumf += (t12-t11);       
2998         }
2999     }
3000     sumt += (t2-t1);
3001     sump +=(pHd[i-1]? i: (i-1));
3002     max_pkts -= (pHd[i-1]? i: (i-1));
3004     //printf("end cleanup %d %d %d\n",sumt,sumf,sump );
3005     if (sump > 10000) {
3006        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", 
3007                   j,ltot, appid[j%M], 
3008                   l3_off[j%M],l4_off[j%M],  
3009                   len[j%M],buf[j%M],
3010                    sumt/sump,  (sump*1000)/l, max_batch,
3011                    sumf/sump);
3012        sumt=sump=sumf=0;
3013        l=0;
3014     }
3017     //cleanup any remaining buffers
3018     for(;;)
3019     {
3020         pHd[0] = (Ti_Pkt *)QMSS_DESC_PTR(PKTIO_QMSS_QUEUE_POP_RAW (rxQ));
3021         if(!pHd[0]) break;
3022         tempVA  =  Osal_qmssPhyToVirt(pHd[0]);
3023         freeQ=Qmss_getQueueHandle(Cppi_getReturnQueue (Cppi_DescType_HOST, (Cppi_Desc *)tempVA));
3024         netapi_utilCacheWbInv(tempVA,128);
3025         PKTIO_QMSS_QUEUE_PUSH_DESC_SIZE_RAW (freeQ,
3026                                              (void *) pHd[0],
3027                                              128);
3028     }
3031 #ifdef EXPERIMENTAL
3033 static inline void send_it(Ti_Pkt *tip, int len, ROUTE_SEC_T * p_sec)
3035   unsigned long st1;
3036   unsigned long st2;
3037   int err=0;
3038   PKTIO_METADATA_T meta2 = {PKTIO_META_TX,{0},0};
3039   nwalTxPktInfo_t meta_tx2={0};
3040   st1=netapi_timing_start();
3041   if (len<60) 
3042   { 
3043      unsigned int templen;
3044      char * p_pkt;
3045      len=60; 
3046      Pktlib_getDataBuffer(tip,(uint8_t**)&p_pkt,&templen);//ignore templen
3047      Cppi_setData (Cppi_DescType_HOST, (Cppi_Desc *) tip, p_pkt,len);
3048      stats.tx_min+=1;
3049   }
3050   Pktlib_setPacketLen(tip,len);
3051   meta_tx2.txFlag1 = (NWAL_TX_FLAG1_DO_IPV4_CHKSUM| NWAL_TX_FLAG1_META_DATA_VALID );
3052   meta_tx2.startOffset = 0;
3053   meta_tx2.ipOffBytes = 14;
3054   meta_tx2.ploadLen = len ;
3055   if(p_sec)
3056   {   
3057        meta_tx2.txFlag1 |= NWAL_TX_FLAG1_DO_IPSEC_CRYPTO ;
3058        meta2.sa_handle=p_sec->tx_inflow_mode_handle; //this tells netapi that inflow crypto needs to be applied
3059        meta_tx2.enetPort=0;
3060        meta_tx2.saOffBytes=14+20;
3061        meta_tx2.saPayloadLen=len-14-20;   //don't include tag, mac and outer header
3062        stats.sec_tx+=1;
3063   }
3064   meta2.u.tx_meta=&meta_tx2;
3065   pktio_send(netcp_tx_chan,tip,&meta2,&err);
3066   stats.tx +=1;
3067   st2=netapi_timing_start();
3068   stats.send_cycles += (unsigned long long) (st2-st1);  
3069
3070 void recv_cb_router(struct PKTIO_HANDLE_Tag * channel, Ti_Pkt* p_recv[],
3071                          PKTIO_METADATA_T meta[], int n_pkts,
3072                          uint64_t ts )
3074 int i;
3075 int len;
3076 int p;
3077 Ti_Pkt * tip;
3078 unsigned int templen;
3079 char * p_pkt;
3080 HEAD_T temp_head;
3081 unsigned int appid;
3082 IP_HEAD_T th;
3083 ROUTE_SEC_T *sec_data=NULL;
3084 unsigned long t1;
3085 unsigned long t2;
3086 unsigned long ct1;
3087 unsigned long ct2;
3088 unsigned short ip_pl;
3089 int n_c_ops;
3090 t1=netapi_timing_start();
3091 ct1 =Osal_cache_op_measure(&n_c_ops);
3092 for(i=0;i<n_pkts;i++)
3094         tip = p_recv[i];
3095         Pktlib_getDataBuffer(tip,(uint8_t**)&p_pkt,&templen);//ignore templen
3096         len = Pktlib_getPacketLen(tip)-4;//real length, subtract mac trailer
3097         stats.rx+=1;
3098         appid = ((unsigned int)meta[i].u.rx_meta->appId)&0xff000000;
3099         switch(appid)
3100         {
3101            case(NETAPI_NETCP_MATCH_IPSEC):
3102            case(NETAPI_NETCP_MATCH_IPSEC_POLICY):
3103            {
3104              int tailen=12+2;
3105              memcpy(&temp_head,&p_pkt[14],sizeof(HEAD_T));
3106              if (!check_header(&temp_head,&meta[i])) {
3107                 stats.n_bad+=1;
3108                 Pktlib_freePacket(tip); 
3109                 continue;
3110              }
3111              tailen+=p_pkt[len-12-2]; //padding length  (12)should come from sec_ptr
3112              p_pkt = &p_pkt[8+16+20];   //16= iv len, should come from sec_ptr
3113              len -= (8+16+20+tailen);  //16= iv len should come from sec ptr
3114             
3115              //now check inner headder.
3116              memcpy(&th,&p_pkt[14],20);
3117              if (!check_header(&temp_head,&meta[i])) {
3118                 stats.n_bad+=1;
3119                 Pktlib_freePacket(tip);
3120                 continue;
3121              }
3122              Cppi_setData (Cppi_DescType_HOST, (Cppi_Desc *) tip, p_pkt,len);
3123              Pktlib_setPacketLen(tip,len);
3124              
3125              if (route_pkt(our_router, tip, &th, p_pkt, &len,&sec_data)<0)
3126               {
3127                 stats.n_bad+=1;
3128                 Pktlib_freePacket(tip);
3129              }
3130              else
3131              {
3132                send_it(tip,len,sec_data);
3133              }
3134              break;
3135            }
3136            case(NETAPI_NETCP_MATCH_GENERIC_MAC):
3137               if((p_pkt[12]!=0x8)||(p_pkt[13]!=0x00)) 
3138               {
3139                 stats.n_new+=1;
3140                 Pktlib_freePacket(tip); 
3141                 continue;
3142               }
3143               if (!check_header(&temp_head,&meta[i])) 
3144               {
3145                 stats.n_bad+=1;
3146                 Pktlib_freePacket(tip);
3147                 continue;
3148               }
3149               memcpy(&th,&p_pkt[14],20);
3150               ip_pl= (((unsigned char *)&th.w1)[2]<<8) | ((unsigned char *)&th.w1)[3];
3151               if ((ip_pl+14)<60)
3152               {
3153                 len-= (60-(ip_pl+14));
3154                 stats.rx_min+=1;
3155               }
3156               Pktlib_setPacketLen(tip,len);
3157               if (route_pkt(our_router, tip, &th, p_pkt, &len,&sec_data)<0)
3158               {
3159                 stats.n_bad+=1;
3160                 Pktlib_freePacket(tip);
3161               }
3162               else
3163               {
3164                  send_it(tip,len,sec_data);
3165               }
3166               break;
3167            case(NETAPI_NETCP_MATCH_GENERIC_IP):
3168               Pktlib_freePacket(tip); 
3169               stats.n_new=1;
3170               break;
3171            default:
3172               stats.n_new+=1;
3173               Pktlib_freePacket(tip);
3174               break;
3175        }
3177 t2=netapi_timing_start();
3178 ct2 =Osal_cache_op_measure(&n_c_ops);
3179 stats.app_cycles +=  (unsigned long long) (t2-t1);
3180 stats.tx_cache_cycles += (unsigned long long) (ct2-ct1);
3181 return;
3183 #endif