]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - keystone-rtos/netapi.git/blob - ti/runtime/netapi/test/net_test.c
Additional changes to integrate with highperf-lib, moved osal.c to highperf-lib
[keystone-rtos/netapi.git] / ti / runtime / netapi / test / net_test.c
1 /******************************************
2  * File: net_test.c
3  * Purpose: test app for netapi
4  **************************************************************
5  * FILE:  net_test.c
6  * 
7  * DESCRIPTION:  netapi user space transport
8  *               library  test application
9  * 
10  * REVISION HISTORY:  rev 0.0.1 
11  *
12  *  Copyright (c) Texas Instruments Incorporated 2010-2011
13  * 
14  *  Redistribution and use in source and binary forms, with or without 
15  *  modification, are permitted provided that the following conditions 
16  *  are met:
17  *
18  *    Redistributions of source code must retain the above copyright 
19  *    notice, this list of conditions and the following disclaimer.
20  *
21  *    Redistributions in binary form must reproduce the above copyright
22  *    notice, this list of conditions and the following disclaimer in the 
23  *    documentation and/or other materials provided with the   
24  *    distribution.
25  *
26  *    Neither the name of Texas Instruments Incorporated nor the names of
27  *    its contributors may be used to endorse or promote products derived
28  *    from this software without specific prior written permission.
29  *
30  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
31  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
32  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
33  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
34  *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
35  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
36  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
39  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
40  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42  *****************************************/
43 //#define NET_TEST_ENABLE_SIDE_BAND_LOW_LEVEL_API
44 #ifdef NET_TEST_ENABLE_SIDE_BAND_LOW_LEVEL_API
45 #define NWAL_ENABLE_SA
46 #endif
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <unistd.h>
51 #include <string.h>
54 #include "trie.h"
55 #include "string.h"
56 #include "netapi.h"
57 #include "pktio.h"
58 #include <sys/resource.h>
59 #include "net_test.h"
60 #include <ti/drv/sa/salld.h>
61 //#define EXPERIMENTAL
62 #ifdef EXPERIMENTAL
63 #include "router.c"
64 Trie * our_router;
69 OUR_ROUTE_T routes[MAX_ROUTES]=
70 {
71 {0,{0xD4,0xbe,0xd9,0x00,0xd3,0x7e, 0x00,0x01,0x02,0x03,0x14,0x02,0x08,0x00},0},
72 {0,{0x00,0x00,0x0,0x00,0x0,0x0, 0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00},0},
73 {0,{0xD4,0xbe,0xd9,0x00,0xd3,0x7e, 0x00,0x01,0x02,0x03,0x14,0x02,0x08,0x00},0},
74 {0,{0x00,0x15,0x60,0xa1,0xf7,0xbe, 0x00,0x01,0x02,0x03,0x04,0x05,0x08,0x00},0},
75 {0,{0xd4,0xbe,0xd9,0x00,0xd3,0x7e, 0x00,0x01,0x02,0x03,0x04,0x05,0x08,0x00},0}};
77 unsigned int ip[MAX_ROUTES]={BE(0x0a0100c8),BE(0x0a00000a),BE(0x0a02000a),BE(0xc0a8010a),BE(0x9eda6719)};
79 #endif
80 //#define TEST_TIMERS
82 /*************debug********************/
83 void dump_descr(unsigned long *p, int n)
84 {
85    printf("--------dump of descriptor %d %x\n", n, (int) p);
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("-----------------------------\n");
89 }
90 void dump_header(unsigned long *p, int n, int a, int r)
91 {
92    printf("--------dump of header %d %x appID=%x flag1=%x\n", n, (int) p,a,r);
93    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]);
94    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]);
95    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]);
96    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]);
97    printf("-----------------------------\n");
98 }
101 static void  netapi_dump_buf
103     unsigned long *                      buf,
104     uint32_t                      buf_length
107     uint8_t                       count = 0;
108     uint16_t                      dump_size;
109     uint8_t*                     tmp_buf;
110     uint8_t                       row_count;
111     static uint8_t                first = 0;
113     //if(first > 2) return;
115     //first++;
117     dump_size = buf_length ;
119     tmp_buf = (uint8_t *)(buf);
121     printf("netapi *:  - 8 bit word hex Length: %d Start \n",buf_length);
122     do
123     {
124         row_count = (dump_size - count);
126         if(row_count == 0)
127         {
128             break;
129         }
131         if(row_count > 4)
132         {
133             row_count = 4;
134         }
136         switch (row_count)
137         {
138             case 4:
139             {
140                 printf("netapi *:%02d : %02x    %02x    %02x    %02x \n",
141                       count,tmp_buf[0],tmp_buf[1],tmp_buf[2],tmp_buf[3]);
142                 break;
143             }
144             case 3:
145             {
146                 printf("netapi *: %02d : %02x    %02x    %02x \n",
147                       count,tmp_buf[0],tmp_buf[1],tmp_buf[2]);
148                 break;
149             }
151             case 2:
152             {
153                 printf("netapi *: %02d : %02x    %02x \n",
154                       count,tmp_buf[0],tmp_buf[1]);
155                 break;
156             }
158             case 1:
159             {
160                 printf("netapi *: %02d : %02x \n",
161                       count,tmp_buf[0]);
162                 break;
163             }
165             default:
166             {
167                 /* Should never reach here */
168                 printf("netapi *: Internal Error in netapi_dump_buf().Row Count: %d \n",
169                     row_count);
170                 return;
171             }
172         }
174         tmp_buf = tmp_buf + row_count;
175         count = count +  row_count;
177     }while(count < dump_size);
179     printf("netapi *:  - Byte hex Dump End \n");
184 /*****************************************/
187 //************for multi pkt burst  xfer test in loopback mode
188 #define TX_BURST 700 
189 int pktloopback=TUNE_NETAPI_NWAL_ENABLE_PASS_LOOPBACK;
190 nwalTxPSCmdInfo_t   flowPSCmdInfo;
191 nwal_RetValue       nwalRetVal;
193 /* Local Per Process default resourcese maintained at NWAL */
194 nwalLocCxtInfo_t    nwalLocCxt;
196 //this device: 10.0.0.100, mac 0x,01,02,03,04,05  and .. 0x6
198 //test packet, setup for loopback (so dest is ourself)
199 static uint8_t testPkt[] = {
201   /* MAC header */
202   0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
203   0x00, 0xe0, 0xa6, 0x66, 0x57, 0x04,
204   0x08, 0x00,
206   /* IP header */
207   0x45, 0x00,
208   0x00, 0x6c,  /* Length (including this header) */
209   0x00, 0x00, 0x00, 0x00, 0x05, 0x11,
210   0x00, 0x00,  /* Header checksum */
211   0x0a, 0x00, 0x00, 0x0a, 0x0a, 0x00, 0x00, 0x64,
213   /* UDP header */
214   0x12, 0x34, 0x05, 0x55,
215   0x00, 0x58,  /* Length, including this header */
216   0x00, 0x00,  /* Header checksum */
218  /* Payload */
219   0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
220   0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41,
221   0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
222   0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51,
223   0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
224   0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 0x60, 0x61,
225   0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
226   0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71,
227   0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
228   0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81
230 };
232 char    input_file_name[] = "net_test_config.txt";
233 #define MAX_LINE_LENGTH 40
235 #define TEST_PAYLOAD_LEN            80
237 #define TEST_PKT_IP_OFFSET_BYTES        14
238 #define TEST_PKT_UDP_OFFSET_BYTES       34
239 #define TEST_PKT_PLOAD_OFFSET_BYTES     42
240 #define TEST_PKT_UDP_HDR_LEN            8
241 /* Offsets to length fields */
242 #define TEST_PKT_OFFSET_IP_LEN      16
243 #define TEST_PKT_OFFSET_UDP_LEN     38
245 #define TEST_PKT_LEN                122
247 /* The pseudo header checksum of the packet except for the 16 bit length */
248 #define TEST_PKT_PSEUDO_HDR_CHKSUM_SANS_LEN  0x0FFC
250 void example_fast_poll( PKTIO_HANDLE_T * p_pktio, int max_pkts);
251 void example_fast_pushpop(Pktlib_HeapHandle p_heap, int ntrials);
253 void recv_cb_router(struct PKTIO_HANDLE_Tag * channel, Ti_Pkt* p_recv[],
254                          PKTIO_METADATA_T meta[], int n_pkts,
255                          uint64_t ts );
258 //#include "arpa/inet.h"
259 long htonl(long x)
261         long temp = (x&0xff000000)>>24 | (x&0xff0000)>>8 | (x&0xff00)<<8 |  (x&0xff)<<24 ;
262         return temp;
265 /********************************************************************
266  *  FUNCTION PURPOSE: Ones complement addition utility
267  ********************************************************************
268  ********************************************************************/
269 uint16_t test_utilOnesComplementAdd (uint16_t v1, uint16_t v2)
271   uint32_t result;
273   result = (uint32_t)v1 + (uint32_t)v2;
274   result = (result >> 16) + (result & 0xffff);
275   result = (result >> 16) + (result & 0xffff);
277   return ((uint16_t)result);
280 /********************************************************************
281  *  FUNCTION PURPOSE: Ones complement checksum utility
282  ********************************************************************
283  ********************************************************************/
284  uint16_t test_utilOnesCompChkSum (uint8_t *p, uint32_t nwords)
286   uint16_t chksum = 0;
287   uint16_t v;
288   uint32_t i;
289   uint32_t j;
291   for (i = j = 0; i < nwords; i++, j+=2)  {
292     v = (p[j] << 8) | p[j+1];
293     chksum = test_utilOnesComplementAdd (chksum, v);
294   }
295   return (chksum);
296 } /* utilOnesCompChkSum */
298 /**************************************************************************************
299  * FUNCTION PURPOSE: Compute ipv4 psudo checksum
300  **************************************************************************************
301  * DESCRIPTION: Compute ipv4 psudo checksum
302  **************************************************************************************/
303 uint16_t test_utilGetIpv4PsudoChkSum (uint8_t *data, uint16_t payloadLen)
305   uint16_t psudo_chksum;
307   psudo_chksum = test_utilOnesCompChkSum (&data[12], 4);
308   psudo_chksum = test_utilOnesComplementAdd(psudo_chksum, (uint16_t) data[9]);
309   psudo_chksum = test_utilOnesComplementAdd(psudo_chksum, payloadLen);
311   return (psudo_chksum);
313 } /* utilGetIpv4PsudoChkSum */
317 /* net test default configuration */
318 netTestConfig_t config =
320     {0x00,0x01,0x02,0x03,0x05,0x05},
321     {0x00,0x01,0x02,0x03,0x05,0x06},
322     {10, 0, 0, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
323     {10, 0, 1, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
324     {10, 0, 2, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
325     {192,168 , 1, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
326     {192,168 , 1, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
327     IPSEC_MODE_RX_SIDEBAND,
328     IPSEC_MODE_TX_SIDEBAND,
329     0
330 };
332 #if 1  //goes with real tx (to laptop) 
333 unsigned char real_mac_header[]={0xd4,0xbe,0xd9,0x00,0xd3,0x7e,
334                       0x00,0x01,0x02,0x03,0x04,0x05,
335                       0x08,0x00};
336 unsigned char real_ip_addr[]={0xa,0x00,0x00,0x64,0xa,0x0,0x0,0xa};
337 #endif
339 #if 0  //goes with loopback
340 unsigned char mac_header[]={0x00,0x01,0x02,0x03,0x04,0x05, 
341                       0x00,0x11,0x22,0x33,0x44,0x55,
342                       0x08,0x00};
343 #endif
344 #define NE 65536 
345 HEAD_T *nat;
347 #define NP 5000
348 int n_pkt = NP;
349 STATS_T stats;
351 Trie * P_trie;
352 Trie *p_trie_sa;
353 HEAD_T pkts[NP];
354 #define PERSLOW  10  //% of pkts that will not be fastpath'd 
355 int perslow= PERSLOW;
357 /*******************************************
358  *************NETAPI OBJECTS***************
359  *****************************************/
360 static NETAPI_CFG_T our_netapi_default_cfg=
362 TUNE_NETAPI_PERM_MEM_SZ,
363 128,  //start of packet offset for hw to place data on rx for default flow
364 TUNE_NETAPI_QM_CONFIG_MAX_DESC_NUM, //max number of descriptors in system
365 TUNE_NETAPI_NUM_GLOBAL_DESC,        //total we will use
366 TUNE_NETAPI_DEFAULT_NUM_BUFFERS,   //#descriptors+buffers in default heap
367 64, //#descriptors w/o buffers in default heap
368 TUNE_NETAPI_DEFAULT_BUFFER_SIZE+128+128,  //size of buffers in default heap
369 128   ,  //tail room
370 256      //extra room 
371 };
373 Pktlib_HeapHandle OurHeap;
374 Pktlib_HeapHandle specialSmall;
375 Pktlib_HeapHandle specialLarge;
377 PKTIO_HANDLE_T *our_chan;
378 PKTIO_HANDLE_T *netcp_rx_chan;
379 PKTIO_HANDLE_T *netcp_rx_chan2;
380 PKTIO_HANDLE_T *netcp_tx_chan;
381 PKTIO_HANDLE_T *netcp_sb_tx_chan;
382 PKTIO_HANDLE_T *netcp_sb_rx_chan;
383 PKTIO_CFG_T our_chan_cfg={PKTIO_RW, PKTIO_LOCAL, PKTIO_Q_ANY, 8};
384 PKTIO_CFG_T netcp_rx_cfg={PKTIO_R, PKTIO_NA, PKTIO_NA, 8};
385 PKTIO_CFG_T netcp_rx_cfg2={PKTIO_R, (PKTIO_GLOBAL|PKTIO_PKT), PKTIO_Q_ANY, 8};
386 PKTIO_CFG_T netcp_tx_cfg={PKTIO_W, PKTIO_NA, PKTIO_NA, 8};
387 PKTIO_CFG_T netcp_sb_rx_cfg={PKTIO_R, PKTIO_NA, PKTIO_NA, 8};
388 PKTIO_CFG_T netcp_sb_tx_cfg={PKTIO_W, PKTIO_NA, PKTIO_NA, 8};
390 void house(NETAPI_SCHED_HANDLE_T *s);
391 NETAPI_T netapi_handle;
392 NETAPI_SCHED_HANDLE_T * our_sched;
393 NETAPI_SCHED_CONFIG_T our_sched_cfg={
394   NETAPI_SCHED_DURATION|NETAPI_SCHED_CBV, 0, house, 5000000  //every 5000000 poll loops
395 };
396 void our_stats_cb(NETAPI_T h, paSysStats_t* pPaStats);
397 HPLIB_TIMER_GROUP_HANDLE_T ourTimerBlock; 
398 HPLIB_TIMER_T t1;
399 HPLIB_TIMER_T t2;
400 HPLIB_TIMER_T t3;
402 void our_timer_cb( HPLIB_TIMER_GROUP_HANDLE_T th,
403         int n_fired,     //# timers fired
404         HPLIB_TIMER_LIST_T fired_list,
405         uint64_t currentTime);
407 NETCP_CFG_IP_T ip_rule0;
408 NETCP_CFG_IP_T ip_rule1;
409 NETCP_CFG_CLASS_T class_0;
410 NETCP_CFG_CLASS_T class_1;
411 NETCP_CFG_CLASS_T class_2;
412 NETCP_CFG_FLOW_HANDLE_T specialFlow;
415 NETCP_CFG_CLASSIFIER_T class_0_cfg=
417    NETCP_CFG_CLASS_TYPE_L4,
418    {
419         {0,0, NWAL_APP_PLOAD_PROTO_UDP, {2500}}
420    }
421 };
423 NETCP_CFG_CLASSIFIER_T class_1_cfg=
425     NETCP_CFG_CLASS_TYPE_L4,
426     {
427         {0,0, NWAL_APP_PLOAD_PROTO_UDP, {2502}}
428     }
429 };
431 NETCP_CFG_ROUTE_T  class2_route=
433 NULL, NULL  //* to be filled in
434 };
435 NETCP_CFG_CLASSIFIER_T class_2_cfg=  
437    NETCP_CFG_CLASS_TYPE_L3_L4,
438    {
439         {0,  4 ,0/*fill in below*/ , NULL, NULL,          //L2/L3
440            NWAL_APP_PLOAD_PROTO_UDP, {2504}}   //L4
441    }
442 };
444 PKTIO_CONTROL_T zap_channel_control={PKTIO_CLEAR, NULL};
446 /* security objects. (for loopback mode) */
447 netTestSA_t sa_info[6];
448 int netapi_algorithm_set = 0;
449 int netapi_sec_sa_mode = 2;
451 NETCP_CFG_IPSEC_POLICY_T rx_policy[4];
455 NETAPI_SEC_SA_INFO_T rx_sa [7] = {
457     NWAL_SA_DIR_INBOUND,
458     0x11111111,  //spi
459     nwal_IpSecProtoESP, //ESP mode
460     nwal_SA_MODE_TUNNEL,  //tunnel mode
461     nwal_IPV4, //v4
462     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* Src IP (them) -> set below */
463     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* dst IP (us)-> set below*/
464     64,/* replayWindow */
465     NWAL_SA_AALG_HMAC_SHA1,
466     NWAL_SA_EALG_AES_CBC,
467     0,0  //na
468 },
470     NWAL_SA_DIR_INBOUND,
471     0x22222222,  //spi
472     nwal_IpSecProtoESP, //ESP mode
473     nwal_SA_MODE_TUNNEL,  //tunnel mode
474     nwal_IPV4, //v4
475     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* Src IP (them) -> set below */
476     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* dst IP (us)-> set below*/
477     64,/* replayWindow */
478     NWAL_SA_AALG_HMAC_SHA2_256,
479     NWAL_SA_EALG_AES_CTR,
480     0,0  //na
481 },
483     NWAL_SA_DIR_INBOUND,
484     0x33333333,  //spi
485     nwal_IpSecProtoESP, //ESP mode
486     nwal_SA_MODE_TUNNEL,  //tunnel mode
487     nwal_IPV4, //v4
488     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* Src IP (them) -> set below */
489     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* dst IP (us)-> set below*/
490     0,/* replayWindow */
491     NWAL_SA_AALG_NULL,
492     NWAL_SA_EALG_3DES_CBC,
493     0,0  //na
494 },
496     NWAL_SA_DIR_INBOUND,
497     0x44444444,  //spi
498     nwal_IpSecProtoESP, //ESP mode
499     nwal_SA_MODE_TUNNEL,  //tunnel mode
500     nwal_IPV4, //v4
501     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* Src IP (them) -> set below */
502     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* dst IP (us)-> set below*/
503     64,/* replayWindow */
504     NWAL_SA_AALG_HMAC_MD5,
505     NWAL_SA_EALG_NULL,
506     0,0  //na
507 },
509     NWAL_SA_DIR_INBOUND,
510     0x55555555,  //spi
511     nwal_IpSecProtoESP, //ESP mode
512     nwal_SA_MODE_TUNNEL,  //tunnel mode
513     nwal_IPV4, //v4
514     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* Src IP (them) -> set below */
515     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* dst IP (us)-> set below*/
516     64,/* replayWindow */
517     NWAL_SA_AALG_NULL,
518     NWAL_SA_EALG_AES_GCM,
519     0,0  //na
520 },
522     NWAL_SA_DIR_INBOUND,
523     0x66666666,  //spi
524     nwal_IpSecProtoESP, //ESP mode
525     nwal_SA_MODE_TUNNEL,  //tunnel mode
526     nwal_IPV4, //v4
527     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* Src IP (them) -> set below */
528     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* dst IP (us)-> set below*/
529     64,/* replayWindow */
530     NWAL_SA_AALG_NULL,
531     NWAL_SA_EALG_AES_CCM,
532     0,0  //na
533 },
535     NWAL_SA_DIR_INBOUND,
536     0x77777777,  //spi
537     nwal_IpSecProtoESP, //ESP mode
538     nwal_SA_MODE_TUNNEL,  //tunnel mode
539     nwal_IPV4, //v4
540     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* Src IP (them) -> set below */
541     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* dst IP (us)-> set below*/
542     64,/* replayWindow */
543     NWAL_SA_AALG_GMAC,
544     NWAL_SA_EALG_NULL,
545     0,0  //na
547 };
549 /*tx */
550 NETAPI_SEC_SA_INFO_T tx_sa[7]= {
552     NWAL_SA_DIR_OUTBOUND,
553     0x11111111,  //spi
554     nwal_IpSecProtoESP, //ESP mode
555     nwal_SA_MODE_TUNNEL,  //tunnel mode
556     nwal_IPV4, //v4
557     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* Src IP (us) -> set below */
558     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* dst IP (them) -> set below*/
559     64, /* NA replayWindow */
560     NWAL_SA_AALG_HMAC_SHA1,
561     NWAL_SA_EALG_AES_CBC,
562     0,0  //seq no
563 },
564     {
565     NWAL_SA_DIR_OUTBOUND,
566     0x22222222,  //spi
567     nwal_IpSecProtoESP, //ESP mode
568     nwal_SA_MODE_TUNNEL,  //tunnel mode
569     nwal_IPV4, //v4
570     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* Src IP (us) -> set below */
571     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* dst IP (them) -> set below*/
572     64, /* NA replayWindow */
573     NWAL_SA_AALG_HMAC_SHA2_256,
574     NWAL_SA_EALG_AES_CTR,
575     0,0  //seq no
576 },
578     NWAL_SA_DIR_OUTBOUND,
579     0x33333333,  //spi
580     nwal_IpSecProtoESP, //ESP mode
581     nwal_SA_MODE_TUNNEL,  //tunnel mode
582     nwal_IPV4, //v4
583     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* Src IP (us) -> set below */
584     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* dst IP (them) -> set below*/
585     0, /* NA replayWindow */
586     NWAL_SA_AALG_NULL,
587     NWAL_SA_EALG_3DES_CBC,
588     0,0  //seq no
589 },
591     NWAL_SA_DIR_OUTBOUND,
592     0x44444444,  //spi
593     nwal_IpSecProtoESP, //ESP mode
594     nwal_SA_MODE_TUNNEL,  //tunnel mode
595     nwal_IPV4, //v4
596     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* Src IP (us) -> set below */
597     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* dst IP (them) -> set below*/
598     64, /* NA replayWindow */
599     NWAL_SA_AALG_HMAC_MD5,
600     NWAL_SA_EALG_NULL,
601 },
603     NWAL_SA_DIR_OUTBOUND,
604     0x55555555,  //spi
605     nwal_IpSecProtoESP, //ESP mode
606     nwal_SA_MODE_TUNNEL,  //tunnel mode
607     nwal_IPV4, //v4
608     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* Src IP (them) -> set below */
609     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* dst IP (us)-> set below*/
610     64,/* replayWindow */
611     NWAL_SA_AALG_NULL,
612     NWAL_SA_EALG_AES_GCM,
613     0,0  //na
614 },
616     NWAL_SA_DIR_OUTBOUND,
617     0x66666666,  //spi
618     nwal_IpSecProtoESP, //ESP mode
619     nwal_SA_MODE_TUNNEL,  //tunnel mode
620     nwal_IPV4, //v4
621     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* Src IP (them) -> set below */
622     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* dst IP (us)-> set below*/
623     64,/* replayWindow */
624     NWAL_SA_AALG_NULL,
625     NWAL_SA_EALG_AES_CCM,
626     0,0  //na
627 },
629     NWAL_SA_DIR_OUTBOUND,
630     0x77777777,  //spi
631     nwal_IpSecProtoESP, //ESP mode
632     nwal_SA_MODE_TUNNEL,  //tunnel mode
633     nwal_IPV4, //v4
634     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* Src IP (them) -> set below */
635     { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* dst IP (us)-> set below*/
636     64,/* replayWindow */
637     NWAL_SA_AALG_GMAC,
638     NWAL_SA_EALG_NULL,
639     0,0  //na
641 };
643 static nwalSecKeyParams_t ourTXKeyParams[7] ={
645     16, /* encKeySize: CTR 16 bytes Encryption Key and 4 bytes Salt : 24 bytes:NWAL_SA_EALG_3DES_CBC and 0 bytes Salt*/
646     20, /* macKeySize: 16 bytes NWAL_SA_AALG_HMAC_SHA1 */
647     NULL, //set below
648     NULL, //set below
649 },
651     20, /* encKeySize: CTR 16 bytes Encryption Key and 4 bytes Salt : 24 bytes:NWAL_SA_EALG_AES_CTR and 0 bytes Salt*/
652     32, /* macKeySize: 16 bytes NWAL_SA_AALG_HMAC_SHA2_256 */
653     NULL, //set below
654     NULL, //set below
655 },
657     24, /* encKeySize: DES-CBC: 24 bytes:NWAL_SA_EALG_3DES_CBC and 0 bytes Salt*/
658     0, /* macKeySize: 16 bytes NWAL_SA_AALG_HMAC_SHA2_256 */
659     NULL, //set below
660     NULL, //set below
661 },
663     0, /* NULL*/
664     16, /* MD5, 16 bytes */
665     NULL, //set below
666     NULL, //set below
667 },
669     20, /* encKeySize: GCM 16 bytes Encryption Key and 4 bytes Salt */
670     0, /* macKeySize: 0*/
671     NULL, //set below
672     NULL, //set below
673 },
675     19, /* encKeySize: CTR 16 bytes Encryption Key and 3 bytes Salt : 24 bytes:NWAL_SA_EALG_AES_CTR and 0 bytes Salt*/
676     0, /* macKeySize 0*/
677     NULL, //set below
678     NULL, //set below
679 },
681     0, /* encKeySize: CTR 16 bytes Encryption Key and 3 bytes Salt : 24 bytes:NWAL_SA_EALG_AES_CTR and 0 bytes Salt*/
682     24, /* macKeySize 0*/
683     NULL, //set below
684     NULL, //set below
686 };
688 /* these keys are for aes-ctr and hmac sha2_256 */
689 static nwalSecKeyParams_t ourRXKeyParams[7] ={
691     16, /* encKeySize: CTR 16 bytes Encryption Key and 4 bytes Salt : 24 bytes:NWAL_SA_EALG_3DES_CBC and 0 bytes Salt*/
692     20, /* macKeySize: 16 bytes NWAL_SA_AALG_HMAC_SHA1 */
693     NULL, //set below
694     NULL, //set below
695 },
697     20, /* encKeySize: CTR 16 bytes Encryption Key and 4 bytes Salt */
698     32, /* macKeySize: 16 bytes NWAL_SA_AALG_HMAC_SHA2_256 */
699     NULL, //set below
700     NULL, //set below
701 },
703     24, /* encKeySize: DES-CBC: 24 bytes:NWAL_SA_EALG_3DES_CBC and 0 bytes Salt*/
704     0, /* macKeySize: 16 bytes NWAL_SA_AALG_HMAC_SHA2_256 */
705     NULL, //set below
706     NULL, //set below
707 },
709     0, /* NWAL_SA_EALG_NULL*/
710     16, /* NWAL_SA_AALG_HMAC_MD5, 16 bytes */
711     NULL, //set below
712     NULL, //set below
713 },
715     20, /* encKeySize: GCM 16 bytes Encryption Key and 4 bytes Salt */
716     0, /* macKeySize: 0*/
717     NULL, //set below
718     NULL, //set below
719 },
721     19, /* encKeySize: CTR 16 bytes Encryption Key and 3 bytes Salt t*/
722     0, /* macKeySize 0*/
723     NULL, //set below
724     NULL, //set below
725 },
727     0, /* encKeySize: CTR 16 bytes Encryption Key and 3 bytes Salt : 24 bytes:NWAL_SA_EALG_AES_CTR and 0 bytes Salt*/
728     24, /* macKeySize 0*/
729     NULL, //set below
730     NULL, //set below
732 };
735 static uint8_t ourAuthKey[36] =
736         {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
737          0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
738          0x20, 0x21, 0x22, 0x23 };
741 static uint8_t ourEncrKey[36] = 
742         {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
743          0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
744          0x30, 0x31, 0x32, 0x33 }; 
747 /*************************END NETAPI OBJECTS***********************/
749 #define START_SRC_IP 0x0a00000a
750 #define DST_IP       0xc0a80001
751 #define NEW_START_SRC_IP 0x9eda000a
752 #define DST_PORT 0x555 
753 #define START_SRC_PORT 0x1234
754 #define NEW_START_SRC_PORT 100
755 void update_header(HEAD_T * p_head, int len)
757    unsigned char *p = (unsigned char *) &p_head->udp[1];
758    len -= (20+14);
759    /* update ip checksum */
760    /* update udp checksum */
761    /* update length */
762    *p= (len&0xff00)>>8;
763    *(p+1) = len&0xff;
766 #if 0
767 void gen_pkts(int np)
769 int i;
770 int ip = START_SRC_IP &0xff;
771 int port= START_SRC_PORT;
772 //HEAD_T temp={{0x25000200,0xdead0000,0x80110000,START_SRC_IP,DST_IP},
773 //             {START_SRC_PORT<<16|DST_PORT,0x01ec<<16|0x0000}};
774 HEAD_T temp;
775 memcpy(&temp,&testPkt[0],sizeof(HEAD_T));
777 for(i=0;(i<np) && (i<NP);i++)
778   {
779        memcpy(&pkts[i],&temp,sizeof(temp));
780        update_header(&pkts[i],512);      /* update checksums etc */
781        /* change template for new pkt */
782        ip+=1;
783        if(ip>254) {(ip=START_SRC_IP&0xff); port+=1; }
784        temp.ip[3] = htonl((START_SRC_IP&0xffffff00)| ip);
785        temp.udp[0] = htonl( (temp.udp[0]&0xffff0000)| port);
786        temp.udp[1] = htonl(temp.udp[1]);
787      
788   }
789   n_pkt=np;
791 #endif
793 void build_table(Trie * p_trie)
795 int i;
796 int sport=NEW_START_SRC_PORT; 
797 HEAD_T temp,temp2;
798 KEY_T key;
800 memcpy(&temp,&testPkt[14],sizeof(temp));
802  //insert entry into trie
803 key.src_ip = temp.ip[3];
804 key.dst_ip = temp.ip[4];
805 key.src_port= (temp.udp[0]&0xffff0000)>>16;
806 key.dst_port= (temp.udp[0]&0x0000ffff);
807 trie_insert(p_trie,(char *)&key,sizeof(key), (void *) &nat[0]); //asociate with nat entry 0
809 //build nat table
810 for(i=0;i<100;i++)
812    memcpy(&temp2,&testPkt[14],sizeof(temp));
813    temp2.udp[0] = (temp2.udp[0] & 0xffff0000) |  sport;
814    memcpy(&nat[i], &temp2, sizeof(temp2));
815    sport+= 1;
819 //===========stub transmitter==================
820 void send_pkt(Ti_Pkt *pkt, int len)
822 //just free pkt.  Don't send
823 Pktlib_freePacket((Ti_Pkt*)pkt);
824         return;
827 //==========stub slow path============
828 void slow_path(Ti_Pkt *pkt, int len)
830 // debug: check descriptor for validity by verifying that desciptor link field is null as expected\n");
831          {Ti_Pkt * k= Pktlib_getNextPacket(pkt); if(k != 0) {printf(" slowpath, nexpkt != NULL");}}
832 //just free pkt
833 Pktlib_freePacket((Ti_Pkt*)pkt);
834         return;
836 /* check header */
837 struct LastPktInfo
839 int iface;
840 int ipcsum;
841 int l4csum;
842 } ;
843 static struct LastPktInfo lpInfo;
845 int check_header(HEAD_T * p_head, PKTIO_METADATA_T * p_meta)
847 if (NWAL_RX_FLAG1_META_DATA_VALID & p_meta->u.rx_meta->rxFlag1)
849     lpInfo.iface = ((unsigned int)p_meta->u.rx_meta->appId) &0xff; //last byte is interface num
850     lpInfo.ipcsum =(p_meta->u.rx_meta->rxFlag1 & NWAL_RX_FLAG1_IPV4_CHKSUM_VERIFY_MASK )== NWAL_RX_FLAG1_IPV4_CHKSUM_VERIFY_ACK ? 1 : 0;
851     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; 
852     if ((unsigned int)p_meta->u.rx_meta->appId & NETAPI_NETCP_MATCH_IPSEC)
853     {
854     stats.sec_rx++;
855     }
856     if ((unsigned int)p_meta->u.rx_meta->appId & NETAPI_NETCP_MATCH_IPSEC_POLICY)
857     {
858     stats.secp_rx++;
859     }
861     if ((unsigned int)p_meta->u.rx_meta->appId & NETAPI_NETCP_MATCH_CLASS)
862     {
863         int c= ((unsigned int)p_meta->u.rx_meta->appId >>8)&0xffff;
864     if (c==0)  stats.n_class0_rx +=1;
865     else if (c==1) stats.n_class1_rx +=1;
866     else if (c==2) stats.n_class2_rx +=1;
867     else printf("**NET_TEST RX -unknown class: %x\n",  p_meta->u.rx_meta->appId);
871  return 1;
874 #define PKT_LEN 1400
875 void test_alloc_free(int n)
877 int i;
878 Ti_Pkt * b;
880 for(i=0;i<n;i++)
882   b=Pktlib_allocPacket(OurHeap,PKT_LEN);
883   Pktlib_freePacket(b);
887 //measurement test points
888 unsigned int vv1;
889 unsigned int vv2;
890 unsigned int vv3;
891 unsigned int vv4;
892 unsigned int vv5;
893 unsigned int vv6;
894 //these are updated by pktio.
895 #ifdef PKTIO_GET_BENCHMARK
896 extern unsigned int vv7p;
897 extern unsigned int vv8p;
898 extern unsigned int vv9p;
899 extern unsigned int vv10p;
900 extern unsigned int vv11p;
901 extern unsigned int vv12p;
903 extern unsigned int vv13p;  //rcv path
904 extern unsigned int vv14p;
905 extern unsigned int vv15p;
906 #endif
908 unsigned int vv11;
910 extern unsigned int nwal_prof1,nwal_prof2,nwal_prof3,nwal_prof4,nwal_prof5,nwal_prof6;
912 //#define REASSEMBLE_BENCH
913 #ifdef REASSEMBLE_BENCH
914 #include <ti/drv/pa/example/reassemLib/reassemLib.h>
915 /*--------------reassembly benchmark--------------------------------*/
916 void our_reassemble_bench(int nfrags)
918 paIPReassemblyConfig_t Config={5,128,10000 };
919 int i,j;
920 int len;
921 Ti_Pkt tip;
922 char *buffer;
923 unsigned long v1;
924 unsigned long v2;
925 unsigned long sum1=0;
926 unsigned long sum2=0;
927 paEx_reassemLibInit(&Config);
928 for(j=0;j<200/nfrags;j++)
930   for(i=0;i<nfrags;i++)
931   {
932     short temp;
933     tip=Pktlib_allocPacket(OurHeap,PKT_LEN);
934     Pktlib_getDataBuffer(tip,(uint8_t**)&buffer,&len);
935     memcpy(&buffer[0],&testPkt[14],20); //IP header
936     if (i < (nfrags-1)) buffer[6] = 0x20;
937     temp = i*40; 
938     buffer[6]|= (temp&0x1f00)>>8;
939     buffer[7]= (temp&0xff);
940     temp = 20+40*8; 
941     buffer[2]= (temp&0xff00)>>8;
942     buffer[3]= (temp&0xff);
943     Pktlib_setPacketLen(tip, temp);
944     v1= hplib_mUtil_GetTickCount();
945     paEx_reassemLibProc(tip, 0xffff);
946     v2= hplib_mUtil_GetTickCount();
947     sum1+= v2-v1;
948   }
949   sum2 += v2-v1;
951 printf("reasssembly test:  %d trials, %d frags/pkt  %d cycles/frag %d cycles/last frag\n",j,nfrags, sum1/(j*nfrags), sum2/(j));
953 #endif
955 /*--------------basic pktio send/recv benchmark----------------------*/
956 unsigned int timings[10];
957 void our_pktio_bench(int ntrials)
959 int i;
960 #define NBATCH 8
961 Ti_Pkt tip;
962 unsigned char * pData;
963 int len;
964 int n;
965 int err;
966 int sum =0;
968    Osal_cache_op_measure_reset();
969    for(i=0;i<10;i++) timings[i]=0;
970    printf("calibration loop .. ");
971    for(i=0;i<1000;i++)
972    {
973    vv1= hplib_mUtil_GetTickCount();
974    vv2= hplib_mUtil_GetTickCount();
975    sum+=(vv2-vv1);
976    }
977    printf(" accuracy = +- %d cycles\n", sum/1000);
978    sleep(1);
979   
980 PKTIO_METADATA_T meta[10]={0};
981 //send single, recv single
982 for(i=0;i<ntrials;i++)
984    vv1= hplib_mUtil_GetTickCount();
985    tip=Pktlib_allocPacket(OurHeap,PKT_LEN);
986    vv2= hplib_mUtil_GetTickCount();
987    Pktlib_getDataBuffer(tip,&pData,&len);
988    vv3= hplib_mUtil_GetTickCount();
989    pktio_send(our_chan,tip,&meta[0],&err);
990    vv4= hplib_mUtil_GetTickCount();
991    n=pktio_poll(our_chan,NULL , &err);
992    vv5=   hplib_mUtil_GetTickCount();
993    timings[0]+=(vv6-vv4);
994    timings[1]+=(vv5-vv4);
995    timings[3]+=(vv4-vv3); 
996    timings[5]+=(vv3-vv1);
997    timings[8]+=(vv11-vv6);
999 #ifdef PKTIO_GET_BENCHMARK
1000    timings[2]+=(vv7p-vv4);
1001    timings[4]+=(vv8p-vv3);
1002    timings[6]+=(vv9p-vv8p);
1003    timings[7]+=(vv10p-vv7p);
1004 #endif
1007 #ifdef PKTIO_GET_BENCHMARK
1008    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,
1009            timings[1]/ntrials, timings[2]/ntrials,timings[3]/ntrials, timings[4]/ntrials,timings[5]/ntrials,
1010            timings[6]/ntrials,timings[7]/ntrials, timings[8]/ntrials );
1011    {
1012        unsigned int ccycles;
1013        int n_c_ops;
1014        ccycles =Osal_cache_op_measure(&n_c_ops);
1015        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);
1016    }
1017 #endif
1020 /*-----------test driver: gen an input pkt------- */
1021 //char buffer[sizeof(HEAD_T)+PKT_LEN];
1022 Ti_Pkt * get_pkt(int n, unsigned int *p_len, Pktlib_HeapHandle heap2use, int size, unsigned char * buf2cpy, int copy_size)
1024    int ind;
1025    long long temp;
1026    Ti_Pkt * b;
1027    char * buffer;
1028    unsigned int len;
1030   if (pktloopback==0)
1031   {
1032         if (n>=TX_BURST) return NULL;   //just gen pkts to warm swtich, so that it knows
1033                                 //our mac is valid
1034   }  
1035   b=Pktlib_allocPacket(heap2use,size);
1036   if (!b) 
1037     {printf("net_test: get_pkt() heap empty!! %d pkts gen'd %d \n", n); return NULL;};
1039     //debug - way to validate descriptor
1040     {Ti_Pkt* k= Pktlib_getNextPacket(b); 
1041          if(k != 0) {printf(" genpkt, nexpkt != NULL");}}
1044    //get pointer to buffer area of packet
1045    Pktlib_getDataBuffer(b,(uint8_t**)&buffer,&len);
1046    if (!buffer) 
1047     {printf("net_test: get_pkt() heap returned empty buffer %d \n", n); return NULL;};
1049 #if 0 
1050 if (pktloopback==0)
1052    temp = (long long) rand();
1053    temp *= PKT_LEN;
1054    temp /= RAND_MAX;
1055    temp +=2;
1056    *p_len = (int) temp; 
1057    *p_len = *p_len &0xfffffffe;
1058    temp = (long long) rand();
1059    temp *= n_pkt;
1060    temp /= RAND_MAX;
1061    ind = (int) temp;
1062    update_header(&pkts[ind],*p_len);
1063    //printf("get pkt:%d %d ind=%d len=%d\n",RAND_MAX, rand(),ind, *p_len);
1064     memcpy(&buffer[0], &mac_header[0],14);
1065     memcpy(&buffer[14],(char*)&pkts[ind],sizeof(HEAD_T)); 
1067 else
1068 #endif
1070    //copy test packet into buffer
1072     memcpy(&buffer[0], buf2cpy, copy_size);
1073     *p_len = copy_size;
1075     return b; 
1078 static int eof=0;
1079 /*--------------------------------------------------------------
1080  *----------utility to flip a packet and send 
1081  *--------------------back to source----------------------------
1082  *                   flag=1 => ipsec
1083  *--------------------------------------------------------------*/
1084 void flip_and_send_pkt(Ti_Pkt *tip,  unsigned char * p_pkt, int len, int flag)
1086     unsigned char mac_temp[6];
1087     unsigned char ip_temp[4];
1088     unsigned char new_dest_port[2]={0x75,0x30};  // 30000
1089     uint16_t blah; 
1090     uint16_t i=1;   /* for testing only */
1092     uint8_t *p_spi; 
1093     netTestSA_t * p_sa_info;
1094     uint8_t p_iv[16];
1095     uint8_t p_add[8];
1096     Cppi_HostDesc*          pPloadDesc;
1098     printf("flip_and_send_pkt()\n");
1099               //netapi_dump_buf((long*)p_pkt,len);
1100     Pktlib_setPacketLen(tip,len);
1101     //flip the mac address
1102     memcpy(&mac_temp,&p_pkt[0],6);
1103     memcpy(&p_pkt[0],&p_pkt[6],6);
1104     memcpy(&p_pkt[6],&mac_temp,6);
1105     //memcpy(&p_pkt[0],real_mac_header,6); //for testing to wireshark pc
1107     //flip the ip  (outer in case of ipsec)
1108     memcpy(&ip_temp, &p_pkt[14+12],4);
1109     memcpy(&p_pkt[14+12],&p_pkt[14+12+4],4);
1110     memcpy(&p_pkt[14+12+4],&ip_temp,4);
1112  
1113     //outer checksum to 0
1114     if (!flag) 
1115     {
1116         memset(&p_pkt[14+10],0,2);
1117     }
1119     //inner ip &udp for ipsec
1120     if (flag) 
1121     {
1122         p_spi = &(p_pkt[netTest_MAC_HEADER_LEN+netTest_IP_HEADER_LEN]);
1123         p_sa_info = (netTestSA_t *) trie_lookup(p_trie_sa, (char *)p_spi ,4);
1124         if (p_sa_info == NULL)
1125         {
1126             printf("flip_and_send_pkt(): trie_lookup() failed\n");
1127             return;
1128         }
1129         //just drop non-udp packet
1130         if (p_pkt[p_sa_info->tx_payload_info.encOffset+9]!=0x11)
1131         {
1132             stats.n_new+=1;Pktlib_freePacket(tip); return;
1133         }
1135         /* flip inner IP */
1136         memcpy(&ip_temp, &p_pkt[p_sa_info->tx_payload_info.encOffset+12],4);
1137         memcpy(&p_pkt[p_sa_info->tx_payload_info.encOffset+12],&p_pkt[p_sa_info->tx_payload_info.encOffset+12+4],4);
1138         memcpy(&p_pkt[p_sa_info->tx_payload_info.encOffset+12+4],&ip_temp,4);
1140         /* setting udp ports */
1141         memcpy(&p_pkt[p_sa_info->tx_payload_info.encOffset+20+2],&new_dest_port[0],2);
1142         memset(&p_pkt[p_sa_info->tx_payload_info.encOffset+20+6],0,2); //checksum
1144         if (config.ipsec_mode_tx == IPSEC_MODE_TX_SIDEBAND)
1145         {
1146             /* inner ip checksum : leave alone */
1147             /* outer ip, set to 0 (we will compute on way out */
1148             memset(&p_pkt[14+10],0,2);
1149         }
1150         else
1151         {
1152             //#else //inflow, don't touch outer , clear inner 
1153             //DALmemset(&p_pkt[14+20+8+16+10],0,2); //inner checksum, we will compute on way out
1154             //outer ip checksum : leave alone
1155         }
1156     }
1157     else
1158     {
1159         memset(&p_pkt[14+20+6],0,2);//0 udp checksum (we will compute on way out
1160         memcpy(&p_pkt[14+20+2],&new_dest_port[0],2);
1161     }
1163     /*IPSEC case */ 
1164     if (flag)
1165     {
1166         if (config.ipsec_mode_tx == IPSEC_MODE_TX_SIDEBAND)
1167         //send to crypto for encryption
1168         //12 byte auth tag
1169         {
1170             PKTIO_METADATA_T meta = {PKTIO_META_SB_TX,{0},0};
1171             int err;
1172             nwalDmTxPayloadInfo_t meta_tx={0};
1173             meta.sa_handle=p_sa_info->tx_data_mode_handle;  //use TX SA context
1175             memcpy(&meta_tx, &(p_sa_info->tx_payload_info), sizeof(nwalDmTxPayloadInfo_t));
1177  
1178                 meta_tx.encSize = len - p_sa_info->tx_payload_info.encOffset -p_sa_info->auth_tag_size;
1179                 meta_tx.authSize = len - meta_tx.authOffset - p_sa_info->auth_tag_size;
1181 #if 1
1182             printf("flip_and_send_pkt(): encOffset %d\n", meta_tx.encOffset);
1183             printf("flip_and_send_pkt():authOffset %d\n", meta_tx.authOffset);
1184             printf("flip_and_send_pkt(): encSize %d\n", meta_tx.encSize);
1185             printf("flip_and_send_pkt(): authSize %d\n", meta_tx.authSize);
1186 #endif
1188             meta_tx.pAuthIV=NULL;
1189             meta_tx.aadSize=0;
1190             meta_tx.pAad=NULL;
1191             if (p_sa_info->cipherMode ==  NWAL_SA_EALG_AES_CTR)
1192             {
1193                 memcpy(&p_iv[0], &ourEncrKey[16], 4);
1194                 memcpy(&p_iv[4], &p_pkt[netTest_MAC_HEADER_LEN +netTest_IP_HEADER_LEN + netTest_ESP_HEADER_LEN], 8);
1195                 p_iv[12] = 0;
1196                 p_iv[13] = 0;
1197                 p_iv[14] = 0;
1198                 p_iv[15] = 1;
1199                 meta_tx.pEncIV = &p_iv[0];
1200             }
1201             else if ((p_sa_info->cipherMode == NWAL_SA_EALG_AES_GCM) ||
1202                 (p_sa_info->cipherMode == NWAL_SA_EALG_AES_CCM))
1203                 {
1204                         memcpy(&p_iv[0], &p_pkt[netTest_MAC_HEADER_LEN +netTest_IP_HEADER_LEN + netTest_ESP_HEADER_LEN], 8);
1205                         meta_tx.pEncIV = &p_iv[0];
1206                         memcpy(&p_add[0], &p_pkt[netTest_MAC_HEADER_LEN +netTest_IP_HEADER_LEN], 8);
1207                         meta_tx.pAad= &p_add[0];
1208                         meta_tx.aadSize = 8;
1209                 }
1210             else if (p_sa_info->cipherMode ==  NWAL_SA_EALG_NULL)
1211             {
1212                 meta_tx.pEncIV = NULL;
1213             }
1214             else
1215             {
1216                 meta_tx.pEncIV = &p_pkt[netTest_MAC_HEADER_LEN +netTest_IP_HEADER_LEN + netTest_ESP_HEADER_LEN ];
1217             }
1218             //meta_tx.authIvSize=0;
1220             /* post it to netcp sb tx channel*/
1221             meta_tx.appCtxId = (nwal_AppId)hplib_mUtil_GetTickCount();
1222             meta.u.tx_sb_meta=&meta_tx;
1224 #ifdef NET_TEST_ENABLE_SIDE_BAND_LOW_LEVEL_API
1225             /* Use the command label which was cached during create time
1226             * Update the Queue to receive output packet from SA to the local 
1227             * core Queue. Below information can be saved by application in 
1228             * per process context to avoid API overhead per packet
1229             * Application can use below rxSbSaQ for polling packets back from
1230             * SA
1231             */
1232             nwalRetVal =  
1233             nwal_getLocCxtInfo(PKTIO_GET_NWAL_INSTANCE(netcp_sb_tx_chan),
1234                                                       &nwalLocCxt);
1235             if(nwalRetVal == nwal_OK)
1236             {
1237                 p_sa_info->tx_dmPSCmdInfo.rxSbSaQ = nwalLocCxt.rxSbSaQ;
1238             }
1239             nwal_mCmdDMUpdate(tip,
1240                               &p_sa_info->tx_dmPSCmdInfo,
1241                               meta_tx.appCtxId,
1242                               meta_tx.encOffset,
1243                               meta_tx.encSize,
1244                               meta_tx.pEncIV,
1245                               meta_tx.authOffset,
1246                               meta_tx.authSize,
1247                               meta_tx.pAuthIV,
1248                               meta_tx.aadSize,
1249                               meta_tx.pAad);
1250             pPloadDesc = Pktlib_getDescFromPacket(tip);
1251             pPloadDesc = Qmss_osalConvertDescVirtToPhy(pPloadDesc);
1252             Qmss_queuePushDescSizeRaw(p_sa_info->tx_dmPSCmdInfo.txQueue,
1253                                        pPloadDesc,
1254                                        NWAL_DESC_SIZE);
1256 #else
1257             printf("flip_and_send_pkt(): sending for encryption\n");
1258            pktio_send(netcp_sb_tx_chan,tip,&meta,&err);
1259 #endif
1260        }
1261 else
1262  {
1263   //inflow tx
1264   //send pkt directly, asking for IP and UDP checksum offloads AND IPSEC to be applied
1265            PKTIO_METADATA_T meta = {PKTIO_META_TX,{0},0};
1266            int err;
1267            nwalTxPktInfo_t meta_tx={0};
1268 #define USE_COPY
1269 #ifdef USE_COPY
1270 //debug:  see if re-using RX descriptor for TX is causing our SA lockup
1272   int new_len=0;
1273   Ti_Pkt new_tip =  get_pkt(0, &new_len, specialLarge , len+10 , &p_pkt[0]   , len);
1274   if (!new_tip)
1275   {
1276      printf("net_test> new_tip NULL\n");
1277   }
1278   else
1279   {
1280     Pktlib_setPacketLen(new_tip,new_len);
1281     Pktlib_freePacket(tip);
1282     tip=new_tip;  
1283     Pktlib_getDataBuffer(tip,(uint8_t**)&p_pkt,&new_len);  //reset p_pkt to point to new buffer as its used below
1284     Cppi_setTimeStamp (Cppi_DescType_HOST, (Cppi_Desc *) tip,stats.sec_tx); 
1285   }
1287 if (len <1500)  
1289   eof+=1;
1291 #endif
1292            meta.sa_handle=p_sa_info->tx_inflow_mode_handle; //this tells netapi that inflow crypto needs to be applied
1293            //meta_tx.txFlag1 = (NWAL_TX_FLAG1_DO_IPV4_CHKSUM|NWAL_TX_FLAG1_DO_UDP_CHKSUM| NWAL_TX_FLAG1_DO_IPSEC_ESP_CRYPTO| NWAL_TX_FLAG1_META_DATA_VALID );
1294            meta_tx.txFlag1 = p_sa_info->tx_pkt_info.txFlag1;
1295            meta_tx.enetPort=0;
1296            //meta_tx.saOffBytes=14+20;  
1297            meta_tx.saOffBytes=  p_sa_info->tx_pkt_info.saOffBytes;
1298            meta_tx.saPayloadLen=len-14-20;   //don't include tag, mac and outer header
1299            //meta_tx.startOffset = 0;
1300            meta_tx.startOffset = p_sa_info->tx_pkt_info.startOffset;
1301            //meta_tx.ipOffBytes = 14+20+8+16;   //to inner header
1302            meta_tx.ipOffBytes =p_sa_info->tx_payload_info.encOffset; 
1303            //meta_tx.l4OffBytes = 14+20+8+16+20; //to L4
1304            meta_tx.l4OffBytes = p_sa_info->tx_pkt_info.l4OffBytes;
1305            meta_tx.l4HdrLen = p_sa_info->tx_pkt_info.l4HdrLen;
1306            meta_tx.ploadLen = (unsigned) ((p_pkt[meta_tx.l4OffBytes+4]<<8)|p_pkt[meta_tx.l4OffBytes+4+1]) -8 ;
1307            meta_tx.pseudoHdrChecksum =
1308              test_utilGetIpv4PsudoChkSum(&p_pkt[meta_tx.ipOffBytes],8+ meta_tx.ploadLen);
1310            /* post it to netcp tx channel*/
1311            meta.u.tx_meta=&meta_tx;
1312            if (stats.sec_tx<20) dump_descr((long *) tip, stats.sec_tx);
1313            pktio_send(netcp_tx_chan,tip,&meta,&err);
1314            stats.tx +=1;
1315            stats.sec_tx +=1;
1316      }
1318 else  //non ipsec send pkt directly, asking for IP and UDP checksum ofload
1320            PKTIO_METADATA_T meta2 = {PKTIO_META_TX,{0},0};
1321            int err;
1322            nwalTxPktInfo_t meta_tx2={0};
1323            meta2.sa_handle=nwal_HANDLE_INVALID;
1324            meta_tx2.txFlag1 = (NWAL_TX_FLAG1_DO_IPV4_CHKSUM|NWAL_TX_FLAG1_DO_UDP_CHKSUM| NWAL_TX_FLAG1_META_DATA_VALID );
1325            meta_tx2.startOffset = 0;
1326            meta_tx2.ipOffBytes = 14;
1327            meta_tx2.l4OffBytes = 14+20;
1328            meta_tx2.l4HdrLen = 8;
1329            meta_tx2.ploadLen = (unsigned) ((p_pkt[14+20+4]<<8)|p_pkt[14+20+4+1]) -8 ;
1330            meta_tx2.pseudoHdrChecksum =
1331              test_utilGetIpv4PsudoChkSum(&p_pkt[14],8+ meta_tx2.ploadLen);
1333            /* post it to netcp tx channel*/
1334            meta2.u.tx_meta=&meta_tx2;
1335            pktio_send(netcp_tx_chan,tip,&meta2,&err);
1336            stats.tx +=1;
1343 /***************************************
1344  benchmark receive handler
1345 ****************************************/
1346 void recv_cb_bench(struct PKTIO_HANDLE_Tag * channel, Ti_Pkt* p_recv[],
1347                          PKTIO_METADATA_T meta[], int n_pkts,
1348                          uint64_t ts )
1350    int i;
1351    vv6=   hplib_mUtil_GetTickCount();
1352    for (i=0;i<n_pkts; i++) 
1353    {
1354      Pktlib_freePacket(p_recv[i]);
1355    }
1356    vv11 = hplib_mUtil_GetTickCount();
1359 /****************************************************************************************/
1360 /******************SB Accelerator Callback PKT RECEIVE HANDLER *************************/
1361 /******************  Handles Decrypt and Encrypt operation callbacks ******************/
1362 /******************************************************************************************/
1363 void recv_sb_cb(struct PKTIO_HANDLE_Tag * channel, Ti_Pkt* p_recv[],
1364                          PKTIO_METADATA_T meta[], int n_pkts,
1365                          uint64_t ts )
1367 int i;
1368 int len;
1369 int p;
1370 HEAD_T * p_res;
1371 Ti_Pkt * tip;
1372 unsigned int templen;
1373 int err;
1374 KEY_T key;
1375 char * p_pkt;
1376 HEAD_T * p_head;
1377 HEAD_T temp_head;
1378 int tag_cmp=0;
1379 unsigned int hash[4];
1380 uint8_t *p_spi;
1381 netTestSA_t *p_sa_info;
1383 //nwal_AppId time;
1384 unsigned long time, delta_time;
1385  /* loop over received pkts */
1386    for(i=0;i<n_pkts;i++)
1387    {
1388         tip = p_recv[i];
1389         Pktlib_getDataBuffer(tip,(uint8_t**)&p_pkt,&templen);//ignore templen
1390         len = Pktlib_getPacketLen(tip);//real length
1393          p_spi = &(p_pkt[netTest_MAC_HEADER_LEN+netTest_IP_HEADER_LEN]);
1395         p_sa_info = (netTestSA_t *) trie_lookup(p_trie_sa, (char *)p_spi ,4);
1396         if (p_sa_info == NULL)
1397         {
1398             printf("recv_sb_cb(): trie_lookup failed\n");
1399             continue;
1400         }
1403         //is this a decrypt (rx_tunnel) complete
1404         if ((int)meta[i].u.rx_sb_meta->appId == p_sa_info->rx_tunnel)
1405         {
1406            
1407             time = hplib_mUtil_GetTickCount();
1408            delta_time = time -(unsigned long) meta[i].u.rx_sb_meta->appCtxId;
1409             stats.total_decrypt_time += delta_time;
1410             stats.sb_rx+=1;
1411            //copy hash out of meta data (for some reason it needs endian conversion)
1412            hash[0]= htonl( meta[i].u.rx_sb_meta->pAuthTag[0]);
1413            hash[1]= htonl( meta[i].u.rx_sb_meta->pAuthTag[1]);
1414            hash[2]= htonl( meta[i].u.rx_sb_meta->pAuthTag[2]);
1415            hash[3]= htonl( meta[i].u.rx_sb_meta->pAuthTag[3]);
1416            if(stats.sb_rx<=16)
1417            {
1418              char *tp = (char *) &hash[0];
1419              //dump_header((long*)p_pkt, stats.sb_rx, (int)meta[i].u.rx_sb_meta->appId,0);
1420 #if 1
1421              printf("decrypt complete: tag in pkt= %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x \n",
1422                       p_pkt[len-16],p_pkt[len-15],p_pkt[len-14],p_pkt[len-13],
1423                       p_pkt[len-12],p_pkt[len-11],p_pkt[len-10],p_pkt[len-9], p_pkt[len-8],
1424                       p_pkt[len-7],p_pkt[len-6],
1425                       p_pkt[len-5],p_pkt[len-4],p_pkt[len-3],p_pkt[len-2],p_pkt[len-1]);
1426              printf("decrypt complete: tag from SA=%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x \n",
1427                        tp[0],tp[1],tp[2],tp[3],tp[4],tp[5],
1428                        tp[6],tp[7],tp[8],tp[9],tp[10],tp[11],tp[12],tp[13],tp[14],tp[15]);
1430             // netapi_dump_buf((long*)p_pkt,len);
1431 #endif
1432            }
1433            //check tag 
1434            printf("recv_sb_cb(); auth tag size %d\n", p_sa_info->auth_tag_size);
1435            tag_cmp = memcmp(&p_pkt[len-p_sa_info->auth_tag_size],(char*) &hash[0],p_sa_info->auth_tag_size); //todo, really use meta->authTagLen
1436            stats.n_auth_ok += !(tag_cmp);
1437            
1438             flip_and_send_pkt(tip, p_pkt, len,1);  //flip packet to echo back and send
1439         }
1440         //this is an encrypt (tx tunnel) complete
1441         else if((int)meta[i].u.rx_sb_meta->appId== p_sa_info->tx_tunnel )
1442         {
1443            hash[0]= htonl( meta[i].u.rx_sb_meta->pAuthTag[0]);
1444            hash[1]= htonl( meta[i].u.rx_sb_meta->pAuthTag[1]);
1445            hash[2]= htonl( meta[i].u.rx_sb_meta->pAuthTag[2]);
1446            hash[3]= htonl( meta[i].u.rx_sb_meta->pAuthTag[3]);
1447            stats.sb_tx+=1;
1448            if(stats.sb_tx<=16)
1449            {
1450              char *tp1 = (char *) &hash[0];
1451              //dump_header((long*)p_pkt, stats.sb_tx, (int)meta[i].u.rx_sb_meta->appId,0);
1452 #if 1
1453             printf("encrypt complete: tag in pkt=  %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x \n",
1454                       p_pkt[len-16],p_pkt[len-15],p_pkt[len-14],p_pkt[len-13],
1455                       p_pkt[len-12],p_pkt[len-11],p_pkt[len-10],p_pkt[len-9], p_pkt[len-8],
1456                       p_pkt[len-7],p_pkt[len-6],
1457                       p_pkt[len-5],p_pkt[len-4],p_pkt[len-3],p_pkt[len-2],p_pkt[len-1]);
1458              printf("encrypt complete: tag from SA=%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x \n",
1459                        tp1[0],tp1[1],tp1[2],tp1[3],tp1[4],tp1[5],
1460                        tp1[6],tp1[7],tp1[8],tp1[9],tp1[10],tp1[11],tp1[12],tp1[13],tp1[14],tp1[15]);
1461              //netapi_dump_buf((long*)p_pkt,len);
1462 #endif
1463            }
1464            //put the computed tag in the packet
1465            memcpy(&p_pkt[len-p_sa_info->auth_tag_size],(char*)&hash[0],p_sa_info->auth_tag_size); //todo, really use meta->authTagLen
1466           printf("recv_sb_cb(): dumping pkt after updating computed tag, len %d, auth tag size %d\n", len, p_sa_info->auth_tag_size);
1467            //netapi_dump_buf(p_pkt, len);
1468            {
1469            PKTIO_METADATA_T meta2 = {PKTIO_META_TX,{0},0};
1470            nwalTxPktInfo_t meta_tx={0};
1471            // now send directly 
1472            meta2.sa_handle=nwal_HANDLE_INVALID;
1473            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
1474            meta_tx.startOffset = 0;
1475            meta_tx.ipOffBytes = netTest_MAC_HEADER_LEN;
1476            //not used
1477            meta_tx.l4OffBytes = 0;
1478            meta_tx.l4HdrLen = 0;
1479            meta_tx.ploadLen = 0;
1480             time = hplib_mUtil_GetTickCount();
1481             delta_time = time -(unsigned long) meta[i].u.rx_sb_meta->appCtxId;
1482            stats.total_encrypt_time += delta_time;
1483         
1485            /* post it to netcp tx channel*/
1486            meta2.u.tx_meta=&meta_tx;
1487            printf("recv_sb_cb(): calling pktio send to send it to network\n");
1488            pktio_send(netcp_tx_chan,tip,&meta2,&err);
1489            hplib_CacheWbInv(p_pkt,len);
1490            stats.tx +=1;
1491            }
1492         }
1493         else printf("netapi recv_sb_cb: unknown appiD %x \n",meta[i].u.rx_sb_meta->appId );
1494     }
1497 /******************************************************/
1498 /******************PKT RECEIVE HANDLER *************************/
1499 /******************************************************/
1500 void recv_cb(struct PKTIO_HANDLE_Tag * channel, Ti_Pkt* p_recv[],
1501                          PKTIO_METADATA_T meta[], int n_pkts,
1502                          uint64_t ts )
1504     int i;
1505     int len;
1506     int p;
1507     HEAD_T * p_res;
1508     Ti_Pkt * tip;
1509     unsigned int templen;
1510     int err = 0;
1511     KEY_T key;
1512     char * p_pkt;
1513     HEAD_T * p_head;
1514     HEAD_T temp_head;
1515     netTestSA_t *p_sa_info;
1516     uint8_t *p_spi;
1517     uint8_t p_iv[16];
1518     uint8_t p_add[8];
1519     uint8_t p_add1[1500];
1520     int16_t       retVal;
1521                     nwalGlobCxtInfo_t   nwalGlobCxt;
1522                     nwalLocCxtInfo_t    nwalLocCxt;
1524     Cppi_HostDesc*          pPloadDesc;
1526     p_head=&temp_head;
1528     /* loop over received pkts */
1529     for(i=0;i<n_pkts;i++)
1530     {
1531         tip = p_recv[i];
1532         Pktlib_getDataBuffer(tip,(uint8_t**)&p_pkt,&templen);/*ignore templen */
1533         len = Pktlib_getPacketLen(tip)-4;   /*real length, subtract mac trailer */
1534         Cppi_setData (Cppi_DescType_HOST, (Cppi_Desc *) tip, p_pkt,len);
1535         Pktlib_setPacketLen(tip,len);
1536         printf("recv_cb() packet len %d\n", len);
1538         //debug: validate descriptor */
1539         if(Pktlib_getNextPacket(tip) != 0) 
1540         {
1541             printf(" rcv_cb, nexpkt != NULL");
1542         }
1544         stats.rx+=1;
1546 #if 1
1547         if (stats.rx<16)
1548         {
1549             printf(">rx dmp.."); 
1550             dump_descr((long *) tip, stats.rx);
1551         }
1552         else if (stats.rx>99) 
1553         {
1554             printf(">rx dmp.."); 
1555             dump_descr((long *) tip,stats.rx);
1556         }
1557 #endif
1558 #if 0
1559         if(stats.rx<=16)
1560         {
1561             dump_header((long*)p_pkt, stats.rx, (int)meta[i].u.rx_meta->appId,meta[i].u.rx_meta->rxFlag1);
1562              netapi_dump_buf((long*)p_pkt,len);
1563         }
1564 #endif
1565         /* check header */
1566         memcpy(p_head,&p_pkt[14],sizeof(HEAD_T));
1568         /* check for IPSEC packet, 0x32 is ESP tunnel mode */
1569         if ((p_head->ip[2]&0x0000ff00)==0x00003200)
1570         {
1571             if (!check_header(p_head,&meta[i]))
1572             {
1573                 printf("check header failed\n");
1574                 stats.n_bad+=1;Pktlib_freePacket(tip); 
1575                 continue;
1576             }
1578             //process IP SEC PACKET
1579             if (config.ipsec_mode_rx == IPSEC_MODE_RX_SIDEBAND)
1580             {
1581 #if 0
1582                 {
1583                     
1584                     uint8_t             count;
1585                     NETAPI_HANDLE_T * n = (NETAPI_HANDLE_T *) netapi_handle;
1590                     printf("\n ------------- NWAL MODULE UNIT TEST BEGIN ------------- \n");
1591                     retVal =
1592                     nwal_getGlobCxtInfo(((NETAPI_GLOBAL_T*) (n->global))->nwal_context.nwalInstHandle, 
1593                                         &nwalGlobCxt);
1594                     if(retVal == nwal_OK)
1595                     {
1596                         printf("*******NWAL Global Context Info Dump Begin *******\n");
1597                         printf("rxPaSaFlowId:%d,rxSaPaFlowId:%d rxDefPktQ:0x%x \n",
1598                                        nwalGlobCxt.rxPaSaFlowId,nwalGlobCxt.rxSaPaFlowId,
1599                                         nwalGlobCxt.rxDefPktQ);
1600                         printf("defFlowQ:0x%x,passCppiHandle:0x%x extErr:%d \n",
1601                                        nwalGlobCxt.defFlowQ,nwalGlobCxt.passCppiHandle,
1602                                        nwalGlobCxt.extErr);
1603                         for(count=0;count < nwalGlobCxt.numPaPDSPs;count++)
1604                         {
1605                             printf("NetCP PASS PDSP - %d Version:0x%x \n",
1606                                            count,nwalGlobCxt.pdspVer[count]);
1607                         }
1608                         printf("*******NWAL Global Context Info Dump End *******\n\n");
1609                     }
1610                     printf(" calling nwal_getLocCxtInfo for tx channel\n");
1611                     nwal_getLocCxtInfo(PKTIO_GET_NWAL_INSTANCE(netcp_sb_tx_chan),
1612                                                       &nwalLocCxt);
1613                     printf(" calling nwal_getLocCxtInfo for rx channel\n");
1614                     nwal_getLocCxtInfo(PKTIO_GET_NWAL_INSTANCE(netcp_sb_rx_chan),
1615                                                       &nwalLocCxt);
1617                     
1618               }
1619 #endif
1623                 
1624                 p_spi = &(p_pkt[netTest_MAC_HEADER_LEN+netTest_IP_HEADER_LEN]);
1625                  p_sa_info = (netTestSA_t *) trie_lookup(p_trie_sa, (char *)p_spi ,4);
1626                 if (p_sa_info == NULL)
1627                 {
1628                     printf("recv_cb(): trie_lookup() failed\n");
1629                     continue;
1630                 }
1632                 //ship to crypto for decrypt!!
1633                 //12 byte auth tag
1634                 PKTIO_METADATA_T meta2 = {PKTIO_META_SB_TX,{0},0};
1635                 nwalDmTxPayloadInfo_t meta_tx={0};
1636                 meta2.sa_handle=p_sa_info->rx_data_mode_handle;
1638                 memcpy(&meta_tx, &(p_sa_info->tx_payload_info), sizeof(nwalDmTxPayloadInfo_t));
1640                 //meta_tx.ploadLen = len;
1644                     meta_tx.encSize = len - p_sa_info->tx_payload_info.encOffset -p_sa_info->auth_tag_size;
1645                     meta_tx.authSize = len - meta_tx.authOffset - p_sa_info->auth_tag_size;
1649 #if 1
1650                 printf("recv_cb(): packet length %d\n", len);
1651                 printf("recv_cb(): encOffset %d\n", meta_tx.encOffset);
1652                 printf("recv_cb():authOffset %d\n", meta_tx.authOffset);
1653                 printf("recv_cb(): encSize %d\n", meta_tx.encSize);
1654                 printf("recv_cb(): authSize %d\n", meta_tx.authSize);
1655 #endif
1657                 if (p_sa_info->cipherMode == NWAL_SA_EALG_AES_CTR)
1658                 {
1659                     memcpy(&p_iv[0], &ourEncrKey[16], 4);
1660                     memcpy(&p_iv[4], &p_pkt[netTest_MAC_HEADER_LEN +netTest_IP_HEADER_LEN + netTest_ESP_HEADER_LEN], 8);
1661                     p_iv[12] = 0;
1662                     p_iv[13] = 0;
1663                     p_iv[14] = 0;
1664                     p_iv[15] = 1;
1665                     meta_tx.pEncIV = &p_iv[0];
1666                  
1667                 }
1668                 else if ((p_sa_info->cipherMode == NWAL_SA_EALG_AES_GCM)  ||
1669                             (p_sa_info->cipherMode == NWAL_SA_EALG_AES_CCM))
1670                 {
1671                         memcpy(&p_iv[0], &p_pkt[netTest_MAC_HEADER_LEN +netTest_IP_HEADER_LEN + netTest_ESP_HEADER_LEN], 8);
1672                         meta_tx.pEncIV = &p_iv[0];
1673                         /* aad is the ESP header which is 8 bytes */
1674                         memcpy(&p_add[0], &p_pkt[netTest_MAC_HEADER_LEN +netTest_IP_HEADER_LEN], 8);
1675                         meta_tx.pAad= &p_add[0];
1676                         meta_tx.aadSize = 8;
1677                 }
1678                 else if (p_sa_info->cipherMode ==  NWAL_SA_EALG_NULL)
1679                 {
1680                     meta_tx.pEncIV = NULL;
1681                 }
1682                 else
1683                 {
1684                     meta_tx.pEncIV = &p_pkt[netTest_MAC_HEADER_LEN +netTest_IP_HEADER_LEN + netTest_ESP_HEADER_LEN ];
1685                 }
1686                
1687                 meta_tx.appCtxId = (nwal_AppId)hplib_mUtil_GetTickCount();
1688                 //printf("recv_cb appCtxId: %lu\n", meta_tx.appCtxId);
1690                 /* post it to netcp sb tx channel*/
1691             meta2.u.tx_sb_meta=&meta_tx;
1693 /* TODO: Move the following ifdef code into ptkio_sendDM */
1694 #ifdef NET_TEST_ENABLE_SIDE_BAND_LOW_LEVEL_API
1695             /* Use the command label which was cached during create time
1696             * Update the Queue to receive output packet from SA to the local 
1697             * core Queue. Below information can be saved by application in 
1698             * per process context to avoid API overhead per packet
1699             * Application can use below rxSbSaQ for polling packets back from
1700             * SA
1701             */
1702             nwalRetVal =  
1703             nwal_getLocCxtInfo(PKTIO_GET_NWAL_INSTANCE(netcp_sb_tx_chan),
1704                                                       &nwalLocCxt);
1705             if(nwalRetVal == nwal_OK)
1706             {
1707                 p_sa_info->rx_dmPSCmdInfo.rxSbSaQ = nwalLocCxt.rxSbSaQ;
1708             }
1709             nwal_mCmdDMUpdate(tip,
1710                               &p_sa_info->rx_dmPSCmdInfo,
1711                               meta_tx.appCtxId,
1712                               meta_tx.encOffset,
1713                               meta_tx.encSize,
1714                               meta_tx.pEncIV,
1715                               meta_tx.authOffset,
1716                               meta_tx.authSize,
1717                               meta_tx.pAuthIV,
1718                               meta_tx.aadSize,
1719                               meta_tx.pAad);
1720             pPloadDesc = Pktlib_getDescFromPacket(tip);
1721             pPloadDesc = Qmss_osalConvertDescVirtToPhy(pPloadDesc);
1722             Qmss_queuePushDescSizeRaw(p_sa_info->rx_dmPSCmdInfo.txQueue,
1723                                        pPloadDesc,
1724                                        NWAL_DESC_SIZE);
1726 #else
1727             pktio_send(netcp_sb_tx_chan,tip,&meta2,&err);
1728 #endif
1729             continue;
1730         }
1731         else 
1732             //inflow mode.  flip and send
1733             flip_and_send_pkt(tip,p_pkt,len,1);
1734     }
1735     else if ((p_head->ip[2]&0x0000ff00)!=0x00001100)
1736     {
1737         stats.n_new+=1;Pktlib_freePacket(tip); continue;
1738     }
1739     else  //non ipsec
1740     {
1741         if (!check_header(p_head,&meta[i]))
1742         {
1743             stats.n_bad+=1;Pktlib_freePacket(tip); 
1744             continue;
1745         }
1747 #if 0
1748         /* lookup flow */
1749         key.src_ip = p_head->ip[3];
1750         key.dst_ip = p_head->ip[4];
1751         key.src_port= (p_head->udp[0]&0xffff0000)>>16;
1752         key.dst_port= (p_head->udp[0]&0x0000ffff);
1753         p_res= (HEAD_T *) trie_lookup(P_trie, (char *) &key, sizeof(key));
1754         if (!p_res) { stats.n_new+=1;  slow_path(tip, len); continue;}
1756         /* copy header */
1757         memcpy((char *) p_head, (char *) p_res, sizeof(HEAD_T));
1759         memcpy(&p_pkt[14],p_head,sizeof(HEAD_T));
1760         /* update_mac(&p_pkt[0]);  */
1762         /* 'simulate' send pkt */
1763         send_pkt(tip,len);
1764 #endif
1765              //just flip and send
1766             flip_and_send_pkt(tip,p_pkt,len,0);
1767         }
1768     }
1769     //printf("recv done\n");
1772 #ifdef TEST_TIMERS
1773 //timer callback 
1774 void our_timer_cb( HPLIB_TIMER_GROUP_HANDLE_T th,
1775         int n_fired,     //# timers fired
1776         HPLIB_TIMER_LIST_T fired_list,
1777         uint64_t currentTime)
1779 int i;
1780 HPLIB_TIMER_T tx;
1781 int cookie;
1782 int err;
1783 unsigned long long et;
1784 //DEBUGprintf("TIMER CALLBACK @ %lld %d timers\n", currentTime, n_fired);
1785 tx = hplib_Timer_GetFirst(fired_list);
1786 for(i=0;i<n_fired;i++)
1788   cookie = (int) hplib_Time_rGetCookie(tx);
1789   et =  hplib_Timer_GetTs(tx); //debug
1790   //DEBUGprintf("   timer %d - cookie = %d expected ts=%lld (delta=%lld)\n", i, cookie, et, currentTime-et);
1791   if (cookie ==1)
1792   {  
1793      stats.n_t1+=1;
1794      t1 = hplib_Timer_Start(
1795         th,
1796         (void *) 1,
1797         100LL,  //timer group tics
1798         &err);
1799   }
1800   else if (cookie ==2)
1801   {
1802       stats.n_t2+=1;
1803       t2 = hplib_Timer_Start(
1804         th,
1805         (void *) 2,
1806         200LL,  //timer group ticks
1807         &err);
1808   }
1809   else
1810   {
1811     stats.n_t3+=1;
1812     t3 = hplib_Timer_Start(
1813         th,
1814         (void *) 3,
1815         300LL,  //timer group ticks
1816         &err);
1817     //cancel 1 and restart 1
1818    hplib_Timer_Cancel(th,t1,&err);
1819    t1 = hplib_Timer_Start(
1820         th,
1821         (void *) 1,
1822         100LL,  //timer group ticks
1823         &err);
1824  }
1825   tx = hplib_Timer_GetNext(fired_list,tx); 
1828 #endif
1829 void print_ipsec_stats(Sa_IpsecStats_t     *p_saIpsecStats, nwal_saAALG auth, nwal_saEALG cipher)
1831 #if 0
1832     if(retVal != nwal_OK)
1833     {
1834         System_printf("CORE: %d Error getting IP Sec Stats: Ret Status: %d \n",
1835                        retVal);
1836         return(nwal_FALSE);
1837     }
1838     if((p_saIpsecStats->pktEncHi) ||(p_saIpsecStats->pktEncLo))
1839     {
1840         printf("------------- IPSec TX (Encryption Channel) Stats BEGIN --\n");
1841     }
1842     else
1843     {
1844         printf("------------- IPSec RX (Decryption Channel) Stats BEGIN --\n");
1845     }
1846 #endif
1847     printf("\nAutentication mode: %d, Encryption Mode: %d\n", auth, cipher);
1848     printf("IPSec replayOld:0x%x,replayDup:0x%x,authFail:0x%x \n",
1849                    p_saIpsecStats->replayOld,p_saIpsecStats->replayDup,p_saIpsecStats->authFail);
1850     printf("IPSec txESN:0x%x,rxESN:0x%x,pktEncHi:0x%x,pktEncLo:0x%x,pktDecHi:0x%x,pktDecLo:0x%x \n",
1851                    p_saIpsecStats->txESN,p_saIpsecStats->rxESN,p_saIpsecStats->pktEncHi,
1852                    p_saIpsecStats->pktEncLo,p_saIpsecStats->pktDecHi,p_saIpsecStats->pktDecLo);
1855 void print_datamode_stats(Sa_DataModeStats_t     *p_saDataModeStats, nwal_saAALG auth, nwal_saEALG cipher)
1858     printf("\nAutentication mode: %d, Encryption Mode: %d\n", auth, cipher);
1859     printf(" Packets processedHi: 0x%x, Packets processed Lo:0x%x\n",
1860             p_saDataModeStats->pktHi, p_saDataModeStats->pktLo);
1866 static int np2process = NP;
1867 /******************************************************
1868  * stats callback
1869  *******************************************************/
1870 void our_stats_cb(NETAPI_T h, paSysStats_t* pPaStats)
1872 uint32_t numFreeDataPackets;
1873 uint32_t            numZeroBufferPackets;
1874 uint32_t            numPacketsinGarbage;
1875 Pktlib_HeapStats    pktLibHeapStats;
1876 int i;
1877 unsigned long long bcpp;
1878 unsigned long long bcpp_noc;
1879 unsigned long long bcpp_app;
1880 unsigned long long bcpp_tx;
1881 unsigned long long npL;
1882 unsigned long long cyclesL;
1883 unsigned long long ccyclesL; //cache cycles
1884 NETAPI_SA_STATS_T netapi_sa_stats;
1886 printf(">*****stats @ %lld\n", hplib_mUtil_GetTimestamp());
1887 //printf("netcp_tx_handle check %x\n", netcp_tx_chan->back);
1888 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",
1889          stats.itx, stats.rx, stats.tx, stats.n_bad, stats.n_new, 
1890          stats.n_class0_rx, stats.n_class1_rx, 
1891          stats.n_class2_rx, stats.sec_rx, stats.secp_rx, stats.sb_rx, stats.sb_tx, stats.n_auth_ok,
1892          stats.sec_tx);
1894 if (stats.rx && stats.tx)
1895     printf("decrypt time per packet(avg): %lu, encrypt  time per packet(avg): %lu\n", 
1896     (unsigned long) stats.total_decrypt_time/stats.rx, (unsigned long) stats.total_encrypt_time/stats.tx);
1897 netapi_sched_get_stats(&npL,&cyclesL,&ccyclesL);
1898 if (npL && stats.rx) {
1899    bcpp = cyclesL/npL; 
1900    bcpp_noc = (cyclesL-ccyclesL)/npL; 
1901    bcpp_app = (stats.app_cycles-stats.tx_cache_cycles)/stats.rx;
1902
1903 else {bcpp = bcpp_noc=bcpp_app=0L;}
1904 if (stats.tx)
1906     bcpp_tx = (stats.send_cycles-stats.tx_cache_cycles)/stats.tx;
1907 }else {bcpp_tx = 0L;}
1908 printf(">         ++ busy cycles pp=%lld (%lld wo cache ops) (app+tx= %lld) (tx= %lld) ++\n",
1909          bcpp,bcpp_noc,bcpp_app, bcpp_tx);
1910 if(pPaStats)
1912        printf("C1 number of packets:           %d\n", pPaStats->classify1.nPackets);
1913        printf("C1 number IPv4 packets:         %d\n", pPaStats->classify1.nIpv4Packets);
1914        printf("C1 number IPv6 packets:        %d\n", pPaStats->classify1.nIpv6Packets);
1915        printf("C1 number Custom packets:        %d\n", pPaStats->classify1.nCustomPackets);
1916        printf("C1 number SRIO packets:        %d\n", pPaStats->classify1.nSrioPackets);
1917        printf("C1 number LLC/SNAP Fail packets:        %d\n", pPaStats->classify1.nLlcSnapFail);
1918        printf("C1 number table matched:        %d\n", pPaStats->classify1.nTableMatch);
1919        printf("C1 number failed table matched: %d\n", pPaStats->classify1.nNoTableMatch);
1920        printf("C1 number IP Fragmented packets: %d\n", pPaStats->classify1.nIpFrag);
1921        printf("C1 number IP Depth Overflow: %d\n", pPaStats->classify1.nIpDepthOverflow);
1922        printf("C1 number VLAN Depth Overflow: %d\n", pPaStats->classify1.nVlanDepthOverflow);
1923        printf("C1 number GRE Depth Overflow: %d\n", pPaStats->classify1.nGreDepthOverflow);
1924        printf("C1 number MPLS Packets: %d\n", pPaStats->classify1.nMplsPackets);
1925        printf ("C1 number of parse fail:        %d\n",pPaStats->classify1.nParseFail);
1926        printf("C1 number of Invalid IPv6 Opt:  %d\n", pPaStats->classify1.nInvalidIPv6Opt);
1927        printf("C1 number of TX IP Fragments:  %d\n", pPaStats->classify1.nTxIpFrag);
1928        printf ("C1 number of silent discard:    %d\n",pPaStats->classify1.nSilentDiscard);
1929        printf("C1 number of invalid control:   %d\n", pPaStats->classify1.nInvalidControl);
1930        printf ("C1 number of invalid states:    %d\n",pPaStats->classify1.nInvalidState);
1931        printf ("C1 number of system fails:      %d\n",pPaStats->classify1.nSystemFail);
1932        printf ("C2 number Packets  :           %d\n",pPaStats->classify2.nPackets);
1933        printf ("C2 number udp           :      %d\n",pPaStats->classify2.nUdp);
1934        printf ("C2 number tcp           :      %d\n",pPaStats->classify2.nTcp);
1935        printf ("C2 number Custom       :      %d\n",pPaStats->classify2.nCustom);
1936        printf ("C2 number silent drop   :      %d\n",pPaStats->classify2.nSilentDiscard);
1937        printf ("C2 number invalid cntrl :      %d\n\n",pPaStats->classify2.nInvalidControl);
1938        printf ("C2 number Modify Stats Cmd Fail :      %d\n\n",pPaStats->modify.nCommandFail);
1940 Pktlib_getHeapStats(OurHeap, &pktLibHeapStats);
1941 printf("main heap stats>  #free=%d #zb=%d #garbage=%d\n", pktLibHeapStats.numFreeDataPackets,
1942                                 pktLibHeapStats.numZeroBufferPackets, pktLibHeapStats.numPacketsinGarbage);
1943 printf("               >  #dataBufThreshStatus=%d #dataBufStarvCounter=%d #zBufThreshStatus=%d #zBufStarvCounter=%d \n", 
1944                         pktLibHeapStats.dataBufferThresholdStatus,pktLibHeapStats.dataBufferStarvationCounter,
1945                         pktLibHeapStats.zeroDataBufferThresholdStatus, pktLibHeapStats.zeroDataBufferStarvationCounter);
1947 Pktlib_getHeapStats(specialSmall, &pktLibHeapStats);
1948 printf("specialSmall heap stats>  #free=%d #zb=%d #garbage=%d\n", pktLibHeapStats.numFreeDataPackets,
1949                                 pktLibHeapStats.numZeroBufferPackets, pktLibHeapStats.numPacketsinGarbage);
1950 printf("                       >  #dataBufThreshStatus=%d #dataBufStarvCounter=%d #zBufThreshStatus=%d #zBufStarvCounter=%d \n", 
1951                                 pktLibHeapStats.dataBufferThresholdStatus,pktLibHeapStats.dataBufferStarvationCounter,
1952                                 pktLibHeapStats.zeroDataBufferThresholdStatus, pktLibHeapStats.zeroDataBufferStarvationCounter);
1955 Pktlib_getHeapStats(specialLarge, &pktLibHeapStats);
1956 printf("specialLarge heap stats>  #free=%d #zb=%d #garbage=%d\n", pktLibHeapStats.numFreeDataPackets,
1957                                 pktLibHeapStats.numZeroBufferPackets, pktLibHeapStats.numPacketsinGarbage);
1958 printf("                       >  #dataBufThreshStatus=%d #dataBufStarvCounter=%d #zBufThreshStatus=%d #zBufStarvCounter=%d \n", 
1959                                 pktLibHeapStats.dataBufferThresholdStatus,pktLibHeapStats.dataBufferStarvationCounter,
1960                                 pktLibHeapStats.zeroDataBufferThresholdStatus, pktLibHeapStats.zeroDataBufferStarvationCounter);
1963 #if 0
1964 printf("pa2sa descriptor area dump\n");
1965 for(i=0;i<TUNE_NETAPI_CONFIG_MAX_PA_TO_SA_DESC;i++)
1967    extern long * pa2sa_descr_base;
1968    long * tip= &pa2sa_descr_base[32*i]; 
1969    dump_descr(tip, i);
1971 printf("sa2pa descriptor area dump\n");
1972 for(i=0;i<TUNE_NETAPI_CONFIG_MAX_SA_TO_PA_DESC;i++)
1974    extern long * sa2pa_descr_base;
1975    long * tip= &sa2pa_descr_base[32*i]; 
1976    dump_descr(tip, i);
1978 #endif
1979 #ifdef TEST_TIMERS
1980 //debug = dump timer polling stats
1981 dump_poll_stats();
1982 //debug = dump timer polling stats
1983 //dump_poll_stats();
1984 #endif
1985 #ifndef EXPERIMENTAL
1986     for (i = 0; i < MAX_SEC_INDEX; i++)
1987 #else
1988     for (i=0; i < 1;i++)
1989 #endif
1990     {
1991         /* Statistics for RX Tunnel */
1992         memset(&netapi_sa_stats, 0, sizeof(netapi_sa_stats));
1993         netapi_getSaStats(netapi_handle, sa_info[i].rx_tunnel, &netapi_sa_stats);
1994         if (netapi_sa_stats.validParams & NETAPI_IPSEC_STAT_VALID)
1995         {
1996              print_ipsec_stats(&(netapi_sa_stats.saIpsecStats), 
1997                                  rx_sa[i].authMode, 
1998                                  rx_sa[i].cipherMode);
1999         }
2000         if (netapi_sa_stats.validParams & NETAPI_SIDEBAND_DATA_MODE_STAT_VALID)
2001         {
2002             print_datamode_stats(&(netapi_sa_stats.dataModeStats),
2003                                    rx_sa[i].authMode, 
2004                                    rx_sa[i].cipherMode);
2005         }
2006         
2007         /* Statistics for TX Tunnel */
2008         memset(&netapi_sa_stats, 0, sizeof(netapi_sa_stats));
2009         netapi_getSaStats(netapi_handle, sa_info[i].tx_tunnel, &netapi_sa_stats);
2010         if (netapi_sa_stats.validParams & NETAPI_IPSEC_STAT_VALID)
2011         {
2012              print_ipsec_stats(&(netapi_sa_stats.saIpsecStats), 
2013                                  tx_sa[i].authMode, 
2014                                  tx_sa[i].cipherMode);
2015         }
2016         if (netapi_sa_stats.validParams & NETAPI_SIDEBAND_DATA_MODE_STAT_VALID)
2017         {
2018             print_datamode_stats(&(netapi_sa_stats.dataModeStats),
2019                                    tx_sa[i].authMode, 
2020                                    tx_sa[i].cipherMode);
2021         }
2022     }
2023     netapi_dump_internal_heap_stats();
2026 //******************************************************
2027 //use scheduling housekeeping callback to generate pkts
2028 //******************************************************
2029 static int done_burst=0;
2030 void house(NETAPI_SCHED_HANDLE_T * s)
2032 Ti_Pkt * tip;
2033 unsigned int len;
2034 nwalTxPktInfo_t meta_tx = {0};
2035 PKTIO_METADATA_T meta = {PKTIO_META_TX,{0},0};
2036 int err;
2037 static int house_pkts_gened=0;
2038 int p;
2039 unsigned char * pIpHdr,* pData;
2040 unsigned int vv1,vv2,vv3;
2041 unsigned int sum_vv1=0;
2042 unsigned int sum_vv2=0;
2043 unsigned int sum_vv3=0;
2044 unsigned int sum_vv4=0;
2045 unsigned int sum_vv5=0;
2047 unsigned int nwal_flow_vv1,nwal_flow_vv2;
2048 unsigned int nwal_sum_vv1=0;
2049 unsigned int nwal_sum_vv2=0;
2050 unsigned int nwal_sum_vv3=0;
2051 unsigned int nwal_sum_vv4=0;
2052 unsigned int nwal_sum_vv5=0;
2053 unsigned int nwal_sum_vv6=0;
2055 unsigned int nwal_sum_flow_vv1=0;
2056 unsigned int nwal_sum_flow_vv2=0;
2057 unsigned int cache_op_b1;
2058 unsigned int cache_op_b2;
2059 int n_c_ops;
2060 static int first =0;
2061 Cppi_HostDesc*      pPktDesc;
2063 if(done_burst)
2065   house_pkts_gened+=TX_BURST;
2066   printf("net_test> request stats at n=%d \n",house_pkts_gened);
2067   netcp_cfgReqStats(netapi_handle, our_stats_cb, 0,&err);
2068   if (err!=0) {printf("stats req failed\n");}
2069   if (house_pkts_gened >= np2process+ 100)
2070   {
2071      netapi_schedShutdown(s,NULL,&err);
2072   }
2073   return;
2075 done_burst=1;
2076 Osal_cache_op_measure_reset();
2077 memset(&meta_tx,0,sizeof(meta_tx));
2078 for(p=0;p<TX_BURST;p++) {  
2079 //reguest stats 
2080 if ((house_pkts_gened>0) && (! (house_pkts_gened%1000)) )
2082    printf("net_test> request stats at n=%d \n",house_pkts_gened);
2083    netcp_cfgReqStats(netapi_handle, our_stats_cb, 0,&err); 
2084    if (err!=0) {printf("stats req failed\n");}
2088   if (house_pkts_gened >= np2process+ 100)
2089   {
2090      //shutdown
2091      netapi_schedShutdown(s,NULL,&err);
2092      continue;
2093   }
2095   else if (house_pkts_gened >= np2process) { house_pkts_gened+=1;  continue;}
2096   
2098 /* manufacture a pkt to transmit */
2099    tip = get_pkt(house_pkts_gened, &len, OurHeap, PKT_LEN,&testPkt[0]  , TEST_PKT_LEN);
2100    if(!tip) { house_pkts_gened +=1; continue; }
2103    /* set the pkt length */
2104    vv1 = hplib_mUtil_GetTickCount();
2105    Pktlib_setPacketLen(tip, len);
2107    /* set up meta data */
2108     meta.sa_handle=nwal_HANDLE_INVALID;
2109 //#define BENCH_UDP_SEND
2110 #ifdef BEND_UDP_SEND
2111     meta_tx.txFlag1 = (NWAL_TX_FLAG1_DO_IPV4_CHKSUM | NWAL_TX_FLAG1_DO_UDP_CHKSUM| NWAL_TX_FLAG1_META_DATA_VALID);
2112     meta_tx.startOffset = 0;
2113     //GONE in V2 meta_tx.pktLen = len;
2114     meta_tx.ipOffBytes = TEST_PKT_IP_OFFSET_BYTES;
2115     meta_tx.l4OffBytes = TEST_PKT_UDP_OFFSET_BYTES;
2116     meta_tx.l4HdrLen = TEST_PKT_UDP_HDR_LEN;
2117     //GONE in V2 meta_tx.ploadOffBytes = TEST_PKT_PLOAD_OFFSET_BYTES;
2118     meta_tx.ploadLen = TEST_PAYLOAD_LEN;
2120     Pktlib_getDataBuffer(tip,&pData,&len);
2121    if(house_pkts_gened &0x1)
2122    {
2123         memcpy(&pData[6],&config.mac1[0] ,6);
2124    }
2125     pIpHdr = pData + meta_tx.ipOffBytes;
2126     meta_tx.pseudoHdrChecksum =
2127         test_utilGetIpv4PsudoChkSum(pIpHdr,(TEST_PAYLOAD_LEN+TEST_PKT_UDP_HDR_LEN));
2128 #else
2129    Pktlib_getDataBuffer(tip,&pData,&len);
2130    if(house_pkts_gened &0x1)
2131    {
2132         memcpy(&pData[6],&config.mac1[0] ,6);
2133    }
2134     meta_tx.txFlag1 = NWAL_TX_FLAG1_META_DATA_VALID;
2135     meta_tx.startOffset = 0;
2136     meta_tx.ploadLen = TEST_PAYLOAD_LEN;
2137 #endif
2138    /* post it to netcp tx channel*/
2139    meta.u.tx_meta=&meta_tx;
2140 #ifdef DEBUG_DESC
2141    if (house_pkts_gened<16) dump_descr((long *) tip, house_pkts_gened);
2142    else if (house_pkts_gened>99) dump_descr((long *) tip,house_pkts_gened);
2143 #endif
2145    if(!first)
2146    {
2147        first++;
2148        nwal_flow_vv1= hplib_mUtil_GetTickCount();
2149        if(nwal_initPSCmdInfo(PKTIO_GET_NWAL_INSTANCE(netcp_tx_chan),
2150                              &meta_tx,
2151                              &flowPSCmdInfo) != nwal_OK)
2152        {
2153            printf("nwal_initPSCmdInfo() ERROR \n");
2154        }
2155        nwal_flow_vv2= hplib_mUtil_GetTickCount();
2156        nwal_sum_flow_vv1 += (nwal_flow_vv1-vv1); 
2157        nwal_sum_flow_vv2 += (nwal_flow_vv2-nwal_flow_vv1); 
2158    }
2159        
2160    cache_op_b1= Osal_cache_op_measure(&n_c_ops);
2161    vv2= hplib_mUtil_GetTickCount();
2162 #ifdef BEND_UDP_SEND
2163    nwal_mCmdSetL4CkSumPort(  tip,
2164                              &flowPSCmdInfo,
2165                              TEST_PKT_UDP_OFFSET_BYTES,
2166                              (TEST_PKT_UDP_HDR_LEN + TEST_PAYLOAD_LEN),
2167                              meta_tx.pseudoHdrChecksum,
2168                              meta_tx.enetPort);
2169 #else
2170    nwal_mCmdSetPort  (tip,
2171                       &flowPSCmdInfo,  //could be NULL
2172                       0);  //port 0 -> switch decides
2174 #endif
2176    pPktDesc = Pktlib_getDescFromPacket(tip);
2177    /* Send the packet out to transmit Q*/
2178    Qmss_queuePushDescSize (flowPSCmdInfo.txQueue, 
2179                         pPktDesc, 
2180                         NWAL_DESC_SIZE);
2181    vv3= hplib_mUtil_GetTickCount();
2182    cache_op_b2= Osal_cache_op_measure(&n_c_ops);
2184    sum_vv1 += (vv2-vv1);
2185    if(!house_pkts_gened)
2186    {
2187        /* first packet. Take out the PS command label creation cost */
2188        sum_vv1 = sum_vv1 - nwal_sum_flow_vv2;
2189    }
2191    sum_vv3 += (vv3-vv2)-(cache_op_b2-cache_op_b1); //sub out cache op cost
2193    // printf("pktio send. full=%d metadata=%d pktio_send=%d\n", vv3-vv1,  vv2-vv1,  vv3-vv2);
2194    
2196    stats.itx +=1;
2197    house_pkts_gened +=1;
2198  }
2200    unsigned int ccycles;
2201    ccycles =Osal_cache_op_measure(&n_c_ops);
2202    if (sum_vv1) 
2203    {
2204       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", 
2205               stats.itx, sum_vv1/stats.itx,  nwal_sum_flow_vv2, sum_vv3/stats.itx, 
2206               n_c_ops, ccycles, n_c_ops? (ccycles/(n_c_ops/2)) : 0);
2207 #if 0
2208       printf("NWAL Profile Cycles: Prof1= %d,Prof2=%d,Prof3=%d,Prof4=%d,Prof5=%d ,Prof6=%d \n",
2209               nwal_sum_vv1/stats.itx,nwal_sum_vv2/stats.itx,nwal_sum_vv3/stats.itx,
2210               nwal_sum_vv4/stats.itx,nwal_sum_vv5/stats.itx,nwal_sum_vv6/stats.itx);
2211   
2212 #endif  
2214       if(stats.itx2)
2215       {
2216           printf("nwal_flowSend Profile Cycles: Prof1= %d,Prof2=%d \n",
2217               nwal_sum_flow_vv1/stats.itx2,nwal_sum_flow_vv2/stats.itx2);
2218       }
2220    }
2224 void  build_sa_db(int i)
2226     long tmp_spi;
2227     if ((tx_sa[i].authMode == NWAL_SA_AALG_HMAC_SHA1) && (tx_sa[i].cipherMode == NWAL_SA_EALG_AES_CBC))
2228     {
2229         /* static configuration, will not change */
2230         sa_info[i].tx_payload_info.aadSize = 0;
2231         sa_info[i].tx_payload_info.pAad = NULL;
2232         sa_info[i].tx_payload_info.pAuthIV = NULL;
2233         sa_info[i].tx_payload_info.authOffset = netTest_MAC_HEADER_LEN  + netTest_IP_HEADER_LEN;        /*done: same for all cipher suites */         
2234         sa_info[i].tx_payload_info.encOffset = netTest_MAC_HEADER_LEN +
2235                                                                      netTest_IP_HEADER_LEN +
2236                                                                     netTest_ESP_HEADER_LEN +
2237                                                                     netTest_AES_CBC_IV_LEN;
2239         /* dynamic configuration, will  be calculated on the fly */
2240         sa_info[i].tx_payload_info.authSize = 0; /* pkt len - mac - ip -icv (12) */
2241         sa_info[i].tx_payload_info.encSize = 0;   /* authSize - esp header size (always 8 bytes) */
2242         sa_info[i].tx_payload_info.pEncIV = 0;
2243         sa_info[i].tx_payload_info.pPkt = 0;     /* not being referenced in net_test */
2245         sa_info[i].cipherMode = NWAL_SA_EALG_AES_CBC;
2246         sa_info[i].inner_ip_offset = sa_info[i].tx_payload_info.encOffset;
2247         sa_info[i].auth_tag_size = netTest_ICV_LEN;  /* icv or mac size,. always 12 except for AES_CCM/AES_GCM */
2248 #ifdef EXPERIMENTAL
2249         sa_info[i].iv_len=16;
2250         sa_info[i].bl=16;
2251         sa_info[i].spi = tx_sa[i].spi;
2252         sa_info[i].src =*((unsigned int *)(&config.local_ipsec_ip.ipv4[0]));
2253         sa_info[i].dst =*((unsigned int *)(&config.remote_ipsec_ip.ipv4[0]));
2254 #endif
2255         sa_info[i].tx_pkt_info.enetPort = 0;
2256         sa_info[i].tx_pkt_info.ipOffBytes = sa_info[i].tx_payload_info.encOffset;
2257         sa_info[i].tx_pkt_info.l4HdrLen = netTest_UDP_HEADER_LEN; /*UDP header len */
2258         sa_info[i].tx_pkt_info.l4OffBytes = sa_info[i].inner_ip_offset + netTest_IP_HEADER_LEN;
2259         sa_info[i].tx_pkt_info.startOffset = 0;
2260         sa_info[i].tx_pkt_info.lpbackPass = 0;
2261         sa_info[i].tx_pkt_info.ploadLen = 0; /*sa_info[i].tx_pkt_info.l4OffBytes + 4 */
2262         sa_info[i].tx_pkt_info.pPkt = NULL;
2263         sa_info[i].tx_pkt_info.saOffBytes = netTest_MAC_HEADER_LEN  + netTest_IP_HEADER_LEN;
2264         sa_info[i].tx_pkt_info.saPayloadLen = 0;
2265         sa_info[i].tx_pkt_info.pseudoHdrChecksum =0;
2266        
2267         sa_info[i].tx_pkt_info.txFlag1 = NWAL_TX_FLAG1_DO_IPSEC_ESP_CRYPTO| NWAL_TX_FLAG1_DO_UDP_CHKSUM| NWAL_TX_FLAG1_META_DATA_VALID ;
2269         tmp_spi = htonl((long)(tx_sa[i].spi));
2270          trie_insert(p_trie_sa,(char *)&tmp_spi,4, (void *) &sa_info[i]); //asociate with tx sa SPI
2271          printf("*********** build_sa_db(): spi %d,0x%x\n", tx_sa[i].spi,tx_sa[i].spi);;
2272          
2273          
2274     }
2275     else if ((tx_sa[i].authMode == NWAL_SA_AALG_HMAC_SHA2_256) && (tx_sa[i].cipherMode == NWAL_SA_EALG_AES_CTR))
2276     {
2277         printf("inside build_sa_db, index %d\n", i);
2278         /* static configuration, will not change */
2279         sa_info[i].tx_payload_info.aadSize = 0;
2280         sa_info[i].tx_payload_info.pAad = NULL;
2281         sa_info[i].tx_payload_info.pAuthIV = NULL;
2282         sa_info[i].tx_payload_info.authOffset = netTest_MAC_HEADER_LEN  + netTest_IP_HEADER_LEN;        /*done: same for all cipher suites */
2283         sa_info[i].tx_payload_info.encOffset = netTest_MAC_HEADER_LEN +
2284                                                                      netTest_IP_HEADER_LEN +
2285                                                                     netTest_ESP_HEADER_LEN +
2286                                                                     netTest_AES_CTR_IV_LEN;
2291         /* dynamic configuration, will  be calculated on the fly */
2292         sa_info[i].tx_payload_info.authSize = 0;
2293         sa_info[i].tx_payload_info.encSize = 0;
2294         sa_info[i].tx_payload_info.pEncIV = 0;
2295         sa_info[i].tx_payload_info.pPkt = 0;
2297         sa_info[i].cipherMode = NWAL_SA_EALG_AES_CTR;
2298         sa_info[i].inner_ip_offset = sa_info[i].tx_payload_info.encOffset;
2299         sa_info[i].auth_tag_size = netTest_ICV_LEN;  /* icv or mac size,. always 12 except for AES_CCM/AES_GCM */
2300 #ifdef EXPERIMENTAL
2301         sa_info[i].iv_len=8;
2302         sa_info[i].bl=8;
2303         sa_info[i].spi = tx_sa[i].spi;
2304         sa_info[i].src =*((unsigned int *)(&config.local_ipsec_ip.ipv4[0]));
2305         sa_info[i].dst =*((unsigned int *)(&config.remote_ipsec_ip.ipv4[0]));
2306 #endif
2308         sa_info[i].tx_pkt_info.enetPort = 0;
2309         sa_info[i].tx_pkt_info.ipOffBytes = sa_info[i].tx_payload_info.encOffset;
2310         sa_info[i].tx_pkt_info.l4HdrLen = netTest_UDP_HEADER_LEN; /*UDP header len */
2311         sa_info[i].tx_pkt_info.l4OffBytes = sa_info[i].inner_ip_offset + netTest_IP_HEADER_LEN;
2312         sa_info[i].tx_pkt_info.startOffset = 0;
2313         sa_info[i].tx_pkt_info.lpbackPass = 0;
2314         sa_info[i].tx_pkt_info.ploadLen = 0; /*sa_info[i].tx_pkt_info.l4OffBytes + 4 */
2315         sa_info[i].tx_pkt_info.pPkt = NULL;
2316         sa_info[i].tx_pkt_info.saOffBytes = netTest_MAC_HEADER_LEN  + netTest_IP_HEADER_LEN;
2317         sa_info[i].tx_pkt_info.saPayloadLen = 0;
2318         sa_info[i].tx_pkt_info.pseudoHdrChecksum =0;
2319        
2320         sa_info[i].tx_pkt_info.txFlag1 = NWAL_TX_FLAG1_DO_IPSEC_ESP_CRYPTO| NWAL_TX_FLAG1_DO_UDP_CHKSUM| NWAL_TX_FLAG1_META_DATA_VALID ;
2322         tmp_spi = htonl((long)(tx_sa[i].spi));
2323          trie_insert(p_trie_sa,(char *)&tmp_spi,4, (void *) &sa_info[i]); //asociate with tx sa SPI
2324     }
2325     else if ((tx_sa[i].authMode == NWAL_SA_AALG_NULL) && (tx_sa[i].cipherMode == NWAL_SA_EALG_3DES_CBC))
2326     {
2327         /* static configuration, will not change */
2328         sa_info[i].tx_payload_info.aadSize = 0;
2329         sa_info[i].tx_payload_info.pAad = NULL;
2330         sa_info[i].tx_payload_info.pAuthIV = NULL;
2331         sa_info[i].tx_payload_info.authOffset = netTest_MAC_HEADER_LEN  + netTest_IP_HEADER_LEN;        /*done: same for all cipher suites */
2332         sa_info[i].tx_payload_info.encOffset = netTest_MAC_HEADER_LEN +
2333                                                                      netTest_IP_HEADER_LEN +
2334                                                                     netTest_ESP_HEADER_LEN +
2335                                                                     netTest_3DES_CBC_IV_LEN;
2337         /* dynamic configuration, will  be calculated on the fly */
2338         sa_info[i].tx_payload_info.authSize = 0;
2339         sa_info[i].tx_payload_info.encSize = 0;
2340         sa_info[i].tx_payload_info.pEncIV = 0;
2341         sa_info[i].tx_payload_info.pPkt = 0;
2343         sa_info[i].cipherMode = NWAL_SA_EALG_3DES_CBC;
2344         sa_info[i].inner_ip_offset = sa_info[i].tx_payload_info.encOffset;
2345         sa_info[i].auth_tag_size = 0;  /* icv or mac size,. always 12 except for AES_CCM/AES_GCM */
2346 #ifdef EXPERIMENTAL
2347         sa_info[i].iv_len=8;
2348         sa_info[i].bl=8;
2349         sa_info[i].spi = tx_sa[i].spi;
2350         sa_info[i].src =*((unsigned int *)(&config.local_ipsec_ip.ipv4[0]));
2351         sa_info[i].dst =*((unsigned int *)(&config.remote_ipsec_ip.ipv4[0]));
2352 #endif
2354         sa_info[i].tx_pkt_info.enetPort = 0;
2355         sa_info[i].tx_pkt_info.ipOffBytes = sa_info[i].tx_payload_info.encOffset;
2356         sa_info[i].tx_pkt_info.l4HdrLen = netTest_UDP_HEADER_LEN; /*UDP header len */
2357         sa_info[i].tx_pkt_info.l4OffBytes = sa_info[i].inner_ip_offset + netTest_IP_HEADER_LEN;
2358         sa_info[i].tx_pkt_info.startOffset = 0;
2359         sa_info[i].tx_pkt_info.lpbackPass = 0;
2360         sa_info[i].tx_pkt_info.ploadLen = 0; /*sa_info[i].tx_pkt_info.l4OffBytes + 4 */
2361         sa_info[i].tx_pkt_info.pPkt = NULL;
2362         sa_info[i].tx_pkt_info.saOffBytes = netTest_MAC_HEADER_LEN  + netTest_IP_HEADER_LEN;
2363         sa_info[i].tx_pkt_info.saPayloadLen = 0;
2364         sa_info[i].tx_pkt_info.pseudoHdrChecksum =0;
2365        
2366         sa_info[i].tx_pkt_info.txFlag1 = NWAL_TX_FLAG1_DO_IPSEC_ESP_CRYPTO| NWAL_TX_FLAG1_DO_UDP_CHKSUM| NWAL_TX_FLAG1_META_DATA_VALID ;
2368         tmp_spi = htonl((long)(tx_sa[i].spi));
2369          trie_insert(p_trie_sa,(char *)&tmp_spi,4, (void *) &sa_info[i]); //asociate with tx sa SPI
2370     }
2371     else if ((tx_sa[i].authMode == NWAL_SA_AALG_HMAC_MD5) && (rx_sa[i].cipherMode == NWAL_SA_EALG_NULL))
2372     {
2373         /* static configuration, will not change */
2374         sa_info[i].tx_payload_info.aadSize = 0;
2375         sa_info[i].tx_payload_info.pAad = NULL;
2376         sa_info[i].tx_payload_info.pAuthIV = NULL;
2377         sa_info[i].tx_payload_info.authOffset = netTest_MAC_HEADER_LEN  + netTest_IP_HEADER_LEN;        /*done: same for all cipher suites */
2379         sa_info[i].tx_payload_info.encOffset =         netTest_MAC_HEADER_LEN +
2380                                                                      netTest_IP_HEADER_LEN +
2381                                                                     netTest_ESP_HEADER_LEN +
2382                                                                     netTest_NULL_IV_LEN;
2384         
2385 #ifdef EXPERIMENTAL
2386         sa_info[i].iv_len=0;
2387         sa_info[i].bl=4;
2388         sa_info[i].spi = tx_sa[i].spi;
2389         sa_info[i].src =*((unsigned int *)(&config.local_ipsec_ip.ipv4[0]));
2390         sa_info[i].dst =*((unsigned int *)(&config.remote_ipsec_ip.ipv4[0]));
2391 #endif
2394         /* dynamic configuration, will  be calculated on the fly */
2395         sa_info[i].tx_payload_info.authSize = 0;
2396         sa_info[i].tx_payload_info.encSize = 0;
2397         sa_info[i].tx_payload_info.pEncIV = 0;
2398         sa_info[i].tx_payload_info.pPkt = 0;
2400         sa_info[i].cipherMode = NWAL_SA_EALG_NULL;
2401         sa_info[i].inner_ip_offset = sa_info[i].tx_payload_info.encOffset;
2402         sa_info[i].auth_tag_size = netTest_ICV_LEN;  /* icv or mac size,. always 12 except for AES_CCM/AES_GCM */
2404         sa_info[i].tx_pkt_info.enetPort = 0;
2405         sa_info[i].tx_pkt_info.ipOffBytes = sa_info[i].tx_payload_info.encOffset;
2406         sa_info[i].tx_pkt_info.l4HdrLen = netTest_UDP_HEADER_LEN; /*UDP header len */
2407         sa_info[i].tx_pkt_info.l4OffBytes = sa_info[i].inner_ip_offset + netTest_IP_HEADER_LEN;
2408         sa_info[i].tx_pkt_info.startOffset = 0;
2409         sa_info[i].tx_pkt_info.lpbackPass = 0;
2410         sa_info[i].tx_pkt_info.ploadLen = 0; /*sa_info[i].tx_pkt_info.l4OffBytes + 4 */
2411         sa_info[i].tx_pkt_info.pPkt = NULL;
2412         sa_info[i].tx_pkt_info.saOffBytes = netTest_MAC_HEADER_LEN  + netTest_IP_HEADER_LEN;
2413         sa_info[i].tx_pkt_info.saPayloadLen = 0;
2414         sa_info[i].tx_pkt_info.pseudoHdrChecksum =0;
2415        
2416         sa_info[i].tx_pkt_info.txFlag1 = NWAL_TX_FLAG1_DO_IPSEC_ESP_CRYPTO| NWAL_TX_FLAG1_DO_UDP_CHKSUM| NWAL_TX_FLAG1_META_DATA_VALID ;
2417         tmp_spi = htonl((long)(tx_sa[i].spi));
2418          trie_insert(p_trie_sa,(char *)&tmp_spi,4, (void *) &sa_info[i]); //asociate with tx sa SPI
2419     }
2420     else if ((tx_sa[i].authMode == NWAL_SA_AALG_NULL) && (rx_sa[i].cipherMode == NWAL_SA_EALG_AES_GCM))
2421     {
2422         /* static configuration, will not change */
2423         sa_info[i].tx_payload_info.aadSize = 0;
2424         sa_info[i].tx_payload_info.pAad = NULL;
2425         sa_info[i].tx_payload_info.pAuthIV = NULL;
2426         sa_info[i].tx_payload_info.authOffset = netTest_MAC_HEADER_LEN  + netTest_IP_HEADER_LEN;        /*done: same for all cipher suites */
2427         //sa_info[i].tx_payload_info.encIvSize = netTest_AES_GCM_IV_LEN;
2429         sa_info[i].tx_payload_info.encOffset =         netTest_MAC_HEADER_LEN +
2430                                                                      netTest_IP_HEADER_LEN +
2431                                                                     netTest_ESP_HEADER_LEN +
2432                                                                     netTest_AES_GCM_IV_LEN;
2433 #ifdef EXPERIMENTAL
2434         sa_info[i].iv_len=0;
2435         sa_info[i].bl=4;
2436         sa_info[i].spi = tx_sa[i].spi;
2437         sa_info[i].src =*((unsigned int *)(&config.local_ipsec_ip.ipv4[0]));
2438         sa_info[i].dst =*((unsigned int *)(&config.remote_ipsec_ip.ipv4[0]));
2439 #endif
2442         /* dynamic configuration, will  be calculated on the fly */
2443         sa_info[i].tx_payload_info.authSize = 0;
2444         sa_info[i].tx_payload_info.encSize = 0;
2445         //sa_info[i].tx_payload_info.ploadLen = 0;
2446         sa_info[i].tx_payload_info.pEncIV = 0;
2447         sa_info[i].tx_payload_info.pPkt = 0;
2449         sa_info[i].cipherMode = NWAL_SA_EALG_AES_GCM;
2450         sa_info[i].inner_ip_offset = sa_info[i].tx_payload_info.encOffset;
2451         sa_info[i].auth_tag_size = netTest_AES_GCM_CCM_ICV_LEN;  /* icv or mac size,. always 12 except for AES_CCM/AES_GCM */
2453         sa_info[i].tx_pkt_info.enetPort = 0;
2454         sa_info[i].tx_pkt_info.ipOffBytes = sa_info[i].tx_payload_info.encOffset;
2455         sa_info[i].tx_pkt_info.l4HdrLen = netTest_UDP_HEADER_LEN; /*UDP header len */
2456         sa_info[i].tx_pkt_info.l4OffBytes = sa_info[i].inner_ip_offset + netTest_IP_HEADER_LEN;
2457         sa_info[i].tx_pkt_info.startOffset = 0;
2458         sa_info[i].tx_pkt_info.lpbackPass = 0;
2459         sa_info[i].tx_pkt_info.ploadLen = 0; /*sa_info[i].tx_pkt_info.l4OffBytes + 4 */
2460         sa_info[i].tx_pkt_info.pPkt = NULL;
2461         sa_info[i].tx_pkt_info.saOffBytes = netTest_MAC_HEADER_LEN  + netTest_IP_HEADER_LEN;
2462         sa_info[i].tx_pkt_info.saPayloadLen = 0;
2463         sa_info[i].tx_pkt_info.pseudoHdrChecksum =0;
2464        
2465         sa_info[i].tx_pkt_info.txFlag1 = NWAL_TX_FLAG1_DO_IPSEC_ESP_CRYPTO| NWAL_TX_FLAG1_DO_UDP_CHKSUM| NWAL_TX_FLAG1_META_DATA_VALID ;
2467         tmp_spi = htonl((long)(tx_sa[i].spi));
2468          trie_insert(p_trie_sa,(char *)&tmp_spi,4, (void *) &sa_info[i]); //asociate with tx sa SPI
2469     }
2470     else if ((tx_sa[i].authMode == NWAL_SA_AALG_NULL) && (rx_sa[i].cipherMode == NWAL_SA_EALG_AES_CCM))
2471     {
2472         /* static configuration, will not change */
2473         sa_info[i].tx_payload_info.aadSize = 0;
2474         sa_info[i].tx_payload_info.pAad = NULL;
2475         //sa_info[i].tx_payload_info.authIvSize = 0;
2476         sa_info[i].tx_payload_info.pAuthIV = NULL;
2477         sa_info[i].tx_payload_info.authOffset = netTest_MAC_HEADER_LEN  + netTest_IP_HEADER_LEN;        /*done: same for all cipher suites */
2479         sa_info[i].tx_payload_info.encOffset =         netTest_MAC_HEADER_LEN +
2480                                                                      netTest_IP_HEADER_LEN +
2481                                                                     netTest_ESP_HEADER_LEN +
2482                                                                     netTest_AES_CCM_IV_LEN;
2483 #ifdef EXPERIMENTAL
2484         sa_info[i].iv_len=0;
2485         sa_info[i].bl=4;
2486         sa_info[i].spi = tx_sa[i].spi;
2487         sa_info[i].src =*((unsigned int *)(&config.local_ipsec_ip.ipv4[0]));
2488         sa_info[i].dst =*((unsigned int *)(&config.remote_ipsec_ip.ipv4[0]));
2489 #endif
2492         /* dynamic configuration, will  be calculated on the fly */
2493         sa_info[i].tx_payload_info.authSize = 0;
2494         sa_info[i].tx_payload_info.encSize = 0;
2495         //sa_info[i].tx_payload_info.ploadLen = 0;
2496         sa_info[i].tx_payload_info.pEncIV = 0;
2497         sa_info[i].tx_payload_info.pPkt = 0;
2499         sa_info[i].cipherMode = NWAL_SA_EALG_AES_CCM;
2500         sa_info[i].inner_ip_offset = sa_info[i].tx_payload_info.encOffset;
2501         sa_info[i].auth_tag_size = netTest_AES_GCM_CCM_ICV_LEN;  /* icv or mac size,. always 12 except for AES_CCM/AES_GCM */
2503         sa_info[i].tx_pkt_info.enetPort = 0;
2504         sa_info[i].tx_pkt_info.ipOffBytes = sa_info[i].tx_payload_info.encOffset;
2505         sa_info[i].tx_pkt_info.l4HdrLen = netTest_UDP_HEADER_LEN; /*UDP header len */
2506         sa_info[i].tx_pkt_info.l4OffBytes = sa_info[i].inner_ip_offset + netTest_IP_HEADER_LEN;
2507         sa_info[i].tx_pkt_info.startOffset = 0;
2508         sa_info[i].tx_pkt_info.lpbackPass = 0;
2509         sa_info[i].tx_pkt_info.ploadLen = 0; /*sa_info[i].tx_pkt_info.l4OffBytes + 4 */
2510         sa_info[i].tx_pkt_info.pPkt = NULL;
2511         sa_info[i].tx_pkt_info.saOffBytes = netTest_MAC_HEADER_LEN  + netTest_IP_HEADER_LEN;
2512         sa_info[i].tx_pkt_info.saPayloadLen = 0;
2513         sa_info[i].tx_pkt_info.pseudoHdrChecksum =0;
2514        
2515         sa_info[i].tx_pkt_info.txFlag1 = NWAL_TX_FLAG1_DO_IPSEC_ESP_CRYPTO| NWAL_TX_FLAG1_DO_UDP_CHKSUM| NWAL_TX_FLAG1_META_DATA_VALID ;
2517         tmp_spi = htonl((long)(tx_sa[i].spi));
2518          trie_insert(p_trie_sa,(char *)&tmp_spi,4, (void *) &sa_info[i]); //asociate with tx sa SPI
2519     }
2520     else if ((tx_sa[i].authMode == NWAL_SA_AALG_GMAC) && (rx_sa[i].cipherMode == NWAL_SA_EALG_NULL))
2521     {
2522         /* static configuration, will not change */
2523         sa_info[i].tx_payload_info.aadSize = 0;
2524         sa_info[i].tx_payload_info.pAad = NULL;
2525         sa_info[i].tx_payload_info.pAuthIV = NULL;
2526         sa_info[i].tx_payload_info.authOffset = netTest_MAC_HEADER_LEN  + netTest_IP_HEADER_LEN;
2529         sa_info[i].tx_payload_info.encOffset =         netTest_MAC_HEADER_LEN +
2530                                                                      netTest_IP_HEADER_LEN +
2531                                                                     netTest_ESP_HEADER_LEN +
2532                                                                     netTest_AES_GMAC_IV_LEN;
2533 #ifdef EXPERIMENTAL
2534         sa_info[i].iv_len=0;
2535         sa_info[i].bl=4;
2536         sa_info[i].spi = tx_sa[i].spi;
2537         sa_info[i].src =*((unsigned int *)(&config.local_ipsec_ip.ipv4[0]));
2538         sa_info[i].dst =*((unsigned int *)(&config.remote_ipsec_ip.ipv4[0]));
2539 #endif
2542         /* dynamic configuration, will  be calculated on the fly */
2543         sa_info[i].tx_payload_info.authSize = 0;
2544         sa_info[i].tx_payload_info.encSize = 0;
2545         //sa_info[i].tx_payload_info.ploadLen = 0;
2546         sa_info[i].tx_payload_info.pEncIV = 0;
2547         sa_info[i].tx_payload_info.pPkt = 0;
2549         sa_info[i].cipherMode = NWAL_SA_EALG_NULL;
2550         sa_info[i].authMode = NWAL_SA_AALG_GMAC;
2551         sa_info[i].inner_ip_offset = sa_info[i].tx_payload_info.encOffset;
2552         sa_info[i].auth_tag_size = netTest_AES_GMAC_ICV_LEN;  /* icv or mac size,. always 12 except for AES_CCM/AES_GCM , GMAC*/
2554         sa_info[i].tx_pkt_info.enetPort = 0;
2555         sa_info[i].tx_pkt_info.ipOffBytes = sa_info[i].tx_payload_info.encOffset;
2556         sa_info[i].tx_pkt_info.l4HdrLen = netTest_UDP_HEADER_LEN; /*UDP header len */
2557         sa_info[i].tx_pkt_info.l4OffBytes = sa_info[i].inner_ip_offset + netTest_IP_HEADER_LEN;
2558         sa_info[i].tx_pkt_info.startOffset = 0;
2559         sa_info[i].tx_pkt_info.lpbackPass = 0;
2560         sa_info[i].tx_pkt_info.ploadLen = 0; /*sa_info[i].tx_pkt_info.l4OffBytes + 4 */
2561         sa_info[i].tx_pkt_info.pPkt = NULL;
2562         sa_info[i].tx_pkt_info.saOffBytes = netTest_MAC_HEADER_LEN  + netTest_IP_HEADER_LEN;
2563         sa_info[i].tx_pkt_info.saPayloadLen = 0;
2564         sa_info[i].tx_pkt_info.pseudoHdrChecksum =0;
2565        
2566         sa_info[i].tx_pkt_info.txFlag1 = NWAL_TX_FLAG1_DO_IPSEC_ESP_CRYPTO| NWAL_TX_FLAG1_DO_UDP_CHKSUM| NWAL_TX_FLAG1_META_DATA_VALID ;
2568         tmp_spi = htonl((long)(tx_sa[i].spi));
2569          trie_insert(p_trie_sa,(char *)&tmp_spi,4, (void *) &sa_info[i]); //asociate with tx sa SPI
2570     }
2571     else
2572         printf("build_sa_db(): invalid encryption/authentication combination selected\n");
2574     //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);
2575     
2576     
2581 #define CHECK_SET_PARAM(ARG1, ARG2)     \
2582     do { \
2583         if(strcmp(key, ARG1) == 0) { \
2584         if(data)strncpy(ARG2,data,CONFIG_STRING_LEN); \
2585         printf("CHECK_SET_PARM, match found, input cong string %s\n", ARG2); \
2586         continue; \
2587         } \
2588     } while(0)
2590 #define CHECK_SET_PARAM2(ARG1, ARG2,  ARG3)     \
2591     do { \
2592         if(strcmp(key, ARG1) == 0) { \
2593         if(data) strncpy(ARG2,data,CONFIG_STRING_LEN); \
2594         if(data2) strncpy(ARG3,data2,CONFIG_STRING_LEN); \
2595         printf("CHECK_SET_PARM2, match found, input cong string %s %s\n", ARG2, ARG3); \
2596         continue; \
2597         } \
2598     } while(0)
2600 unsigned char hex2dec(char *p_s)
2602   int val;
2603   sscanf(p_s,"%x",&val); 
2604  return val&0xff;
2606 void parse_one_mac(char * p_mac_str, unsigned char *p_mac)
2608     int index = 0;
2609     int i;
2610     char *pch = strtok (&(p_mac_str[0]),"-");
2612     while (pch != NULL)
2613     {
2614          printf ("%s\n",pch);
2615          p_mac[index] = hex2dec(pch);
2616          index++;
2617          pch = strtok (NULL,"-");
2618     }
2619      printf("index value : %d\n", index);
2620     for (i=0; i<6;i++)
2621          printf("************mac0[%d]: 0x%x\n",i,  p_mac[i]);
2624 void parse_one_ip(char * p_ip_addr_str, unsigned char * p_ip)
2626     int index = 0;
2627     int i;
2628     char * pch = strtok (&p_ip_addr_str[0],".");
2630      while (pch != NULL)
2631      {
2632         printf ("xxxxx: %s\n",pch);
2633         p_ip[index] = atoi(pch);
2634         index++;
2635         pch = strtok (NULL,".");
2636     }
2637     printf("index value : %d\n", index);
2638     for (i=0; i<4;i++)
2639         printf("************ip[%d]: 0x%x\n",i, p_ip[i]);
2644 void parse_mac_address(netTestConfigFile_t *pConfig)
2646     if (strlen(&pConfig->mac0[0]))
2647         parse_one_mac(&pConfig->mac0[0],&config.mac0[0]);
2648     if (strlen(&pConfig->mac1[0]))
2649         parse_one_mac(&pConfig->mac1[0],&config.mac1[0]);
2651 void parse_ip_address(netTestConfigFile_t *pConfig)
2653     if (strlen(&pConfig->ip0[0]))
2654         parse_one_ip(&pConfig->ip0[0],&config.ip0.ipv4[0]);
2655     if (strlen(&pConfig->ip1[0]))
2656         parse_one_ip(&pConfig->ip1[0],&config.ip1.ipv4[0]);
2657     if (strlen(&pConfig->ip2[0]))
2658         parse_one_ip(&pConfig->ip2[0],&config.ip2.ipv4[0]);
2659     if (strlen(&pConfig->local_ipsec_ip[0]))
2660         parse_one_ip(&pConfig->local_ipsec_ip[0],&config.local_ipsec_ip.ipv4[0]);
2661     if (strlen(&pConfig->remote_ipsec_ip[0]))
2662     parse_one_ip(&pConfig->remote_ipsec_ip[0],&config.remote_ipsec_ip.ipv4[0]);
2666 void parse_ipsec_mode(netTestConfigFile_t *pConfig)
2669     if (strlen(&pConfig->ipsec_mode_rx[0]))
2670     {
2671         if (strcmp(pConfig->ipsec_mode_rx, "SIDEBAND") == 0)
2672         {
2673             config.ipsec_mode_rx = IPSEC_MODE_RX_SIDEBAND;
2674         }
2675         else if (strcmp(pConfig->ipsec_mode_rx, "INFLOW") == 0)
2676         {
2677             config.ipsec_mode_rx = IPSEC_MODE_RX_INFLOW;
2678         }
2679         else
2680             printf("parse_ipsec_mode(), invalid RX ipsec mode in config file \n");
2681     }
2683     if (strlen(&pConfig->ipsec_mode_tx[0]))
2684     {
2685         if (strcmp(pConfig->ipsec_mode_tx, "SIDEBAND") == 0)
2686         {
2687             config.ipsec_mode_tx = IPSEC_MODE_TX_SIDEBAND;
2688         }
2689         else if (strcmp(pConfig->ipsec_mode_tx, "INFLOW") == 0)
2690         {
2691             config.ipsec_mode_tx = IPSEC_MODE_TX_INFLOW;
2692         }
2693     else
2694         printf("parse_ipsec_mode(), invalid TX ipsec mode in config file \n");
2695     }
2697     printf("parse_ipsec_mode(): RX mode %d\n", config.ipsec_mode_rx);
2698     printf("parse_ipsec_mode(): TX mode %d\n", config.ipsec_mode_tx);
2700     
2703 #ifdef EXPERIMENTAL
2704 int n_routes=0;
2705 int n_dst_ips=0;
2706 void parse_routes(netTestConfigFile_t *pConfig)
2708 int i;
2709 int said=0;
2710 for(i=0;i<MAX_ROUTES;i++)
2712    int port;
2713    if (pConfig->routes[i][0])
2714    {
2715       port=atoi(&pConfig->ports[i][0]);
2716       if((port<1)||(port>2)) continue; //bad port #: only 1 or 2 valid
2717       if(strncmp(&pConfig->routes[i][0],"MAC",3)==0)
2718       {
2719          routes[i].out_port = port;
2720          parse_one_mac(&pConfig->routes[i][3],&routes[i].out_mac[0]);
2721          memcpy(&routes[i].out_mac[6], ((port==1) ?&config.mac0[0]: &config.mac1[0] ),6); 
2722          routes[i].out_mac[12]=0x08;
2723          routes[i].out_mac[13]=0x00;
2724          routes[i].sec_ptr=NULL;
2725          n_routes+=1;
2726       }
2727       else if (strncmp(&pConfig->routes[i][0],"SA",2)==0)
2728       {      
2729          said=atoi(&pConfig->routes[i][2]) ;
2730          routes[i].sec_ptr=&sa_info[said];
2731          n_routes+=1;
2732       }
2733    }
2735 our_router = route_init();
2736 for (i=0;i<MAX_ROUTES;i++)
2738   unsigned long ip_be;
2739   int route_index;
2740   if (pConfig->dst_ips[i][0])
2741   {
2742      parse_one_ip(&pConfig->dst_ips[i][0],(unsigned char *)&ip_be);
2743      sscanf(&pConfig->paths[i][0],"route%d",&route_index);
2744      route_add(our_router,&ip_be,&routes[route_index]);
2745      n_dst_ips+=1;
2746   }
2748 printf(">Route DB built.  %d entries\n",n_dst_ips);
2750 #endif
2751 static void parse_config_file(FILE * fpr, netTestConfigFile_t *pConfig)
2753     char line[MAX_LINE_LENGTH + 1];
2754     int i;
2755  
2756     char *key, *data, *ep, *data2;
2757     char tokens[] = " :=;\n";
2758     char temp_str[50];
2759     memset(line, 0, MAX_LINE_LENGTH + 1);
2760     memset(pConfig, 0, sizeof(netTestConfigFile_t));
2761     while (fgets(line, MAX_LINE_LENGTH + 1, fpr)) 
2762     {
2763         if(line[0]=='#') continue; //skip comment
2764         key  = (char *)strtok(line, tokens);
2765         data = (char *)strtok(NULL, tokens);
2766         data2 = (char *)strtok(NULL, tokens);
2767         if (!key) continue;
2768         if (!data) continue;
2770         if(strlen(data) == 0) 
2771         {
2772             continue;
2773         }
2775          CHECK_SET_PARAM(INIT_CONFIG_MAC0,&(pConfig->mac0[0]));
2776          CHECK_SET_PARAM(INIT_CONFIG_MAC1,&(pConfig->mac1[0]));
2777          CHECK_SET_PARAM(INIT_CONFIG_IP0,&(pConfig->ip0[0]));
2778          CHECK_SET_PARAM(INIT_CONFIG_IP1,&(pConfig->ip1[0]));
2779          CHECK_SET_PARAM(INIT_CONFIG_IP2,&(pConfig->ip2[0]));
2780          CHECK_SET_PARAM(INIT_CONFIG_LOCAL_IPSEC_IP,&(pConfig->local_ipsec_ip[0]));
2781          CHECK_SET_PARAM(INIT_CONFIG_REMOTE_IPSEC_IP,&(pConfig->remote_ipsec_ip[0]));
2782          CHECK_SET_PARAM(INIT_CONFIG_IPSEC_MODE_RX,&(pConfig->ipsec_mode_rx[0]));
2783          CHECK_SET_PARAM(INIT_CONFIG_IPSEC_MODE_TX,&(pConfig->ipsec_mode_tx[0]));
2784          CHECK_SET_PARAM(INIT_CONFIG_IPSEC_IF_NO,&(pConfig->ipsec_if_no[0]));
2785 #ifdef EXPERIMENTAL
2786     for(i=0;i<MAX_ROUTES;i++)
2787     {
2788         sprintf(temp_str,"route%d",i);
2789         CHECK_SET_PARAM2(temp_str,&pConfig->routes[i][0],&pConfig->ports[i][0] );
2790     }
2791     for(i=0;i<MAX_ROUTES;i++)
2792     {
2793         sprintf(temp_str,"dstip%d",i);
2794         CHECK_SET_PARAM2(temp_str,&pConfig->dst_ips[i][0],&pConfig->paths[i][0] );
2795     }
2796 #endif
2797          
2798     }
2799     parse_mac_address(pConfig);
2801     parse_ip_address(pConfig);
2803     parse_ipsec_mode(pConfig);
2804 #ifdef EXPERIMENTAL
2805      if (strlen(&pConfig->ipsec_if_no[0]))
2806         config.ipsec_if_no = atoi(&pConfig->ipsec_if_no[0]);
2807     parse_routes(pConfig);
2808 #endif
2811 static  netTestConfigFile_t config_file;
2812 /***************************************
2813  ********** test driver*****************
2814  ***************************************/
2815 int main(int argc, char **argv)
2817     int err,i;
2818     rlim_t oss,ss = 1024*1024;
2819     struct rlimit rl;
2821     Pktlib_HeapCfg      heapCfg;
2822     int32_t             errCode;
2823     Pktlib_HeapIfTable*  pPktifTable;
2824  
2825     FILE * fpr = NULL;
2829     err= getrlimit(RLIMIT_STACK,&rl);
2830     if (!err) printf(" stack limit = %d\n",rl.rlim_cur); else printf("getrlimit failed\n");
2831 #if 0
2832     rl.rlim_cur = ss;
2833     err=setrlimit(RLIMIT_STACK,&rl);
2834 i   f (!err) printf("set stack to %d\n",rl.rlim_cur); else printf("setrlimit failed\n");
2835 #endif
2838     if (argc>=2)  np2process = atoi(argv[1]);
2839         printf("*************** np2process %d\n", np2process);
2840     if (np2process<0) np2process = NP; /* default */
2841     if (argc==3)  perslow = atoi(argv[2]);
2842     if ((perslow<0)||(perslow>100)) perslow=PERSLOW;//default
2843     if (argc>3) {printf("net_test  <no of pkts to process> <percent slow path>\n"); exit(1);}
2844 #if 1
2846 #if 0
2847     if (argc >= 2) 
2848     {
2849         fpr = fopen(argv[2], "r");
2850         if (fpr == NULL) 
2851         {
2852             printf("Error in opening %s input file\n", argv[2]);
2853         }
2854     } 
2855     else
2856 #endif
2857     {
2858         fpr = fopen(input_file_name, "r");
2859         if (fpr == NULL) 
2860         {
2861             printf("Error in opening %s input file\n", input_file_name);
2862         }
2863         else
2864         {
2865             parse_config_file(fpr,&config_file);
2866         }
2867     }
2869 #endif
2871     memset(&sa_info, 0, sizeof(sa_info));
2874 //real mode, so update our test packet mac header and ip header
2875 if (pktloopback==0)
2877 memcpy(&testPkt,&real_mac_header[0],14); //overwrite test pkt mac address
2878 memcpy(&testPkt[26],&real_ip_addr[0],8);//overrite test pkt ip addresses
2880 #if 0 
2881 our_router = route_init();
2882 for (i=0;i<n_dst_ips;i++)
2884   route_add(our_router,&ip[i],&routes[i]);
2886 #endif
2888 /*******************************************/
2889 /*************NETAPI STARTUP****************/
2890 /*******************************************/
2892 /* create netapi */
2893 netapi_handle = netapi_init(NETAPI_SYS_MASTER, &our_netapi_default_cfg);
2895 /* open the main heap */
2896 OurHeap = Pktlib_findHeapByName("netapi");
2897 if (!OurHeap) {printf("findheapbyname fail\n"); exit(1);}
2899 /* create two secondary heaps */
2900 /* Initialize the heap configuration. */
2901 memset ((void *)&heapCfg, 0, sizeof(Pktlib_HeapCfg));
2903 pPktifTable = netapi_getPktlibIfTable();
2904 /* Populate the heap configuration */
2905 heapCfg.name                = "netapi-small";
2906 heapCfg.memRegion           = NETAPI_GLOBAL_REGION;
2907 heapCfg.sharedHeap          = 1;
2908 heapCfg.useStarvationQueue  = 0;
2909 heapCfg.dataBufferSize      = 512;
2910 heapCfg.numPkts             = 64;
2911 heapCfg.numZeroBufferPackets= 0;
2912 heapCfg.heapInterfaceTable.data_malloc  = pPktifTable->data_malloc;
2913 heapCfg.heapInterfaceTable.data_free    = pPktifTable->data_free;
2914 heapCfg.dataBufferPktThreshold   = 0;
2915 heapCfg.zeroBufferPktThreshold   = 0;
2917 specialSmall = Pktlib_createHeap(&heapCfg, &errCode);
2918 heapCfg.name                = "netapi-big";
2919 heapCfg.dataBufferSize      = 1600;
2920 specialLarge = Pktlib_createHeap(&heapCfg, &errCode);
2921 //register these heaps so poll routine will include their garbage queues.
2922 netapi_registerHeap(netapi_handle, specialSmall);
2923 netapi_registerHeap(netapi_handle, specialLarge);
2925 #ifdef REASSEMBLE_BENCH
2926 our_reassemble_bench(2);
2927 exit(1);
2928 #endif
2930 #ifndef EXPERIMENTAL
2931 /* create a pktio channel */
2932 our_chan=pktio_create(netapi_handle,"our1stq",(PKTIO_CB) recv_cb_bench, &our_chan_cfg,&err);
2933 if (!our_chan) {printf("pktio create failed err=%d\n",err); exit(1);}
2934 #endif
2936 /* open netcp default tx, rx queues */
2937 netcp_tx_chan= pktio_open(netapi_handle, NETCP_TX, NULL, &netcp_tx_cfg,  &err);
2938 if (!netcp_tx_chan) {printf("pktio open TX failed err=%d\n",err); exit(1);}
2939 #ifdef EXPERIMENTAL
2940 netcp_rx_chan= pktio_open(netapi_handle, NETCP_RX, (PKTIO_CB) recv_cb_router, &netcp_rx_cfg,  &err);
2941 #else
2942 netcp_rx_chan= pktio_open(netapi_handle, NETCP_RX, (PKTIO_CB) recv_cb, &netcp_rx_cfg,  &err);
2943 #endif
2944 if (!netcp_rx_chan) {printf("pktio open RX failed err=%d\n",err); exit(1);}
2946 #ifndef EXPERIMENTAL
2947 /* create a pktio channel for specially classified pkts */
2948 netcp_rx_chan2= pktio_create(netapi_handle, "classq", (PKTIO_CB) recv_cb, &netcp_rx_cfg2,  &err);
2949 if (!netcp_rx_chan2) {printf("pktio create RX2 failed err=%d\n",err); exit(1);}
2951 /* open netcp default tx, rx queues for sideband crypto */
2952 netcp_sb_tx_chan= pktio_open(netapi_handle, NETCP_SB_TX, NULL, &netcp_sb_tx_cfg,  &err);
2953 if (!netcp_sb_tx_chan) {printf("pktio open SB TX failed err=%d\n",err); exit(1);}
2954 netcp_sb_rx_chan= pktio_open(netapi_handle, NETCP_SB_RX, (PKTIO_CB) recv_sb_cb, &netcp_sb_rx_cfg,  &err);
2955 if (!netcp_sb_rx_chan) {printf("pktio open SB RX failed err=%d\n",err); exit(1);}
2956 #endif
2958 printf("net_test> %d bytes left in our CMA area\n", netapi_getBufMemRemainder());
2959 /* create scheduler instance */
2960 our_sched =netapi_schedOpen(netapi_handle,&our_sched_cfg, &err);
2961 if (!our_sched) {printf("sched create failed\n"); exit(1);}
2963 #if 0
2964 /********************************************
2965 * Basic pkt loopback test
2966 *********************************************/
2967 printf("...running pure push/pop benchmark\n");
2968 our_pktio_bench(1000);
2969 our_pktio_bench(1000);
2970 our_pktio_bench(1000);
2971 our_pktio_bench(1000);
2972 our_pktio_bench(1000);
2973 /*********************************************/
2974 #endif
2979 /* add mac intefaces */
2980 netcp_cfgCreateMacInterface(
2981                   netapi_handle,
2982                   &config.mac0[0],
2983                   0,0,
2984                   (NETCP_CFG_ROUTE_HANDLE_T)  NULL,
2985                   (NETCP_CFG_VLAN_T ) NULL ,  //future
2986                   1, 
2987                   &err);
2988 if (err) {printf("addmac0 failed %d\n",err); exit(1); } 
2990 //attach an IP to this interface
2991 ip_rule0=netcp_cfgAddIp(
2992                   netapi_handle,
2993                   0,
2994                   nwal_IPV4,
2995                   &config.ip0,
2996                   NULL,  //all IP
2997                   (NETCP_CFG_ROUTE_HANDLE_T) NULL,
2998                   &err
2999                   );
3000 if (err) {printf("addip0 failed %d\n",err); exit(1); } 
3002 //create a 2nd mac instance
3003 netcp_cfgCreateMacInterface(
3004                   netapi_handle,
3005                   &config.mac1[0],
3006                   1,1,
3007                   (NETCP_CFG_ROUTE_HANDLE_T)  NULL,
3008                   (NETCP_CFG_VLAN_T ) NULL ,  //future
3009                   1,
3010                   &err);
3011 if (err) {printf("addmac1 failed %d\n",err); exit(1); }
3013 //attach an IP to this interface
3014 ip_rule1=netcp_cfgAddIp(
3015                   netapi_handle,
3016                   1,
3017                   nwal_IPV4,
3018                   &config.ip1,
3019                   NULL,  //all IP
3020                   (NETCP_CFG_ROUTE_HANDLE_T) NULL,
3021                   &err
3022                   );
3023 if (err) {printf("addip1 failed %d\n",err); exit(1); }
3025 #ifndef EXPERIMENTAL
3026 //attach 2 classifiers to iface 0, ip0
3027 class_0_cfg.u.c_l4.ip = ip_rule0;
3028 class_0 =  netcp_cfgAddClass(netapi_handle,
3029                              &class_0_cfg,
3030                              NULL,
3031                              NETCP_CFG_ACTION_TO_SW,
3032                              &err);
3033 if (err) {printf("addclass0 failed %d\n",err); exit(1);}
3035 class_1_cfg.u.c_l4.ip = ip_rule0;
3036 class_1 =  netcp_cfgAddClass(netapi_handle,
3037                              &class_1_cfg,
3038                              NULL,
3039                              NETCP_CFG_ACTION_TO_SW,
3040                              &err);
3041 if (err) {printf("addclass1 failed %d\n",err); exit(1);}
3044 //3rd classifier has a different IP and route
3045 class_2_cfg.u.c_l3_l4.ip_addr = &config.ip2;
3047 //create specialFlow for this classifier
3049 Pktlib_HeapHandle heaps[2];
3050 int sizes[2];
3051 heaps[0]= specialSmall;
3052 heaps[1]= specialLarge;
3053 #define SPECIAL_SOP_OFF 128
3054 sizes[0]=512-SPECIAL_SOP_OFF;
3055 sizes[1]=1600-SPECIAL_SOP_OFF;
3056 #if 0
3057 specialFlow = netcp_cfgAddFlow( netapi_handle,
3058                                 2,
3059                                 heaps,
3060                                 sizes,
3061                                 SPECIAL_SOP_OFF,  //offset to start rx is 128 
3062                                 &err);
3063 if (err) {printf("add flow failed\n", err); exit(1);}
3064 #endif
3066 #if 0
3067 //special route for this classifier:  different flow + destination q
3068 class2_route.p_dest_q = netcp_rx_chan2;
3069 class2_route.p_flow = specialFlow;
3070 class_2 = netcp_cfgAddClass(netapi_handle,
3071                             &class_2_cfg,
3072                             (void*) &class2_route,
3073                             NETCP_CFG_ACTION_TO_SW,
3074                             &err);
3075 if (err) {printf("addclass2 failed %d\n",err); exit(1);}
3076 #endif
3078 #endif
3079 //security stuff 
3080     p_trie_sa = trie_new();
3081    if (!p_trie_sa) {printf("trie alloc for SA  failed\n"); exit(1);}
3085     for (i=0; i < MAX_SEC_INDEX;i++)
3086     {
3087         ourRXKeyParams[i].pEncKey = &ourEncrKey[0];
3088         ourRXKeyParams[i].pAuthKey = &ourAuthKey[0];
3089         memcpy(&(rx_sa[i].src), &config.remote_ipsec_ip,4);
3090         memcpy(&(rx_sa[i].dst), &config.local_ipsec_ip,4);
3092         build_sa_db(i);
3093         sa_info[i].rx_tunnel = netapi_secAddSA(
3094             netapi_handle,
3095             config.ipsec_if_no, //iface #0 
3096             &rx_sa[i],
3097             &ourRXKeyParams[i],
3098             config.ipsec_mode_rx == IPSEC_MODE_RX_SIDEBAND ? NETAPI_SEC_SA_SIDEBAND: NETAPI_SEC_SA_INFLOW,
3099                 NULL,  //use default route 
3100             &(sa_info[i].rx_data_mode_handle),
3101             &(sa_info[i].rx_inflow_mode_handle),
3102             &err);
3103         if (err) {printf("addRxSa failed %d\n",err); exit(1);}
3105         if (config.ipsec_mode_rx == IPSEC_MODE_RX_INFLOW)
3106         {
3107             //assume inner and outer ip is the same
3108             rx_policy[i]= netapi_secAddRxPolicy( netapi_handle,
3109                              sa_info[i].rx_tunnel,  //link to tunnel above
3110                              4,         //ipv4
3111                              &config.remote_ipsec_ip, //src -> them
3112                              &config.local_ipsec_ip,  //dst -> us
3113                             NULL,  // no qualifiers
3114                             NULL,  //default route
3115                              &err);
3116             if (err) {printf("addSaPolicy failed  %d, for index %d\n",err,i); exit(1);}
3117         }
3118         else 
3119         {
3120             rx_policy[i] = 0;
3121              
3122         }
3123 #ifdef NET_TEST_ENABLE_SIDE_BAND_LOW_LEVEL_API
3124     /* Remember the NetCP Command Label so save cycles per Crypto 
3125      * operation
3126      * 
3127      */
3128     if(config.ipsec_mode_rx == IPSEC_MODE_RX_SIDEBAND)
3129     {
3130             nwalRetVal =  
3131             nwal_initDMPSCmdInfo(PKTIO_GET_NWAL_INSTANCE(netcp_sb_rx_chan),
3132                                  sa_info[i].rx_data_mode_handle,
3133                                  &sa_info[i].rx_dmPSCmdInfo);
3134     }
3136 #endif
3137     }
3139 //tx SA
3140 //security stuff 
3141      for (i=0; i < MAX_SEC_INDEX;i++)
3142     {
3143     ourTXKeyParams[i].pEncKey = &ourEncrKey[0];
3144     ourTXKeyParams[i].pAuthKey = &ourAuthKey[0];
3145     memcpy(&(tx_sa[i].src), &config.local_ipsec_ip,4);
3146     memcpy(&(tx_sa[i].dst), &config.remote_ipsec_ip,4);
3147     sa_info[i].tx_tunnel = netapi_secAddSA( netapi_handle,
3148                  0, //iface #0 
3149                 &(tx_sa[i]),
3150                 &ourTXKeyParams[i],
3151                 config.ipsec_mode_tx == IPSEC_MODE_TX_SIDEBAND ? NETAPI_SEC_SA_SIDEBAND: NETAPI_SEC_SA_INFLOW,
3152                 NULL,  //use default route 
3153                 &(sa_info[i].tx_data_mode_handle),
3154                 &(sa_info[i].tx_inflow_mode_handle),
3155                 &err);
3156 if (err) {printf("addTxSa failed %d\n",err); exit(1);}
3158 #ifdef NET_TEST_ENABLE_SIDE_BAND_LOW_LEVEL_API
3159     /* Remember the NetCP Command Label so save cycles per Crypto 
3160      * operation
3161      * 
3162      */
3163     if(config.ipsec_mode_tx == IPSEC_MODE_TX_SIDEBAND)
3164     {
3165             nwalRetVal =  
3166             nwal_initDMPSCmdInfo(PKTIO_GET_NWAL_INSTANCE(netcp_sb_tx_chan),
3167                                  sa_info[i].tx_data_mode_handle,
3168                                  &sa_info[i].tx_dmPSCmdInfo);
3169     }
3171 #endif
3172     }
3174 #ifdef TEST_TIMERS
3175 //timers
3176 ourTimerBlock = hplib_Timer_CreateGroup(
3177         netapi_handle,
3178         "our1sttimer",
3179         our_timer_cb,
3180         0,    //1 if timers local to thread
3181         0,    //1 if expect to cancel
3182         hplib_getTicksPerSec()/1000,  /* 1 msc resolution for these timers */
3183         hplib_getTicksPerSec()/5000, /* would like .5 msc tolerence */
3184         10);  //small # of timers to test garbage collection
3185 if (err) {printf("hplib_Timer_CreateGroup() failed %d\n",err); exit(1);}
3187 //start a couple of timers 
3188 t1 = hplib_Timer_Start(
3189         ourTimerBlock,
3190         (void *) 1,
3191         100LL,  //timer group ticks
3192         &err);
3193 if (err) {printf("timerstart failed %d\n");}
3194 t2 = hplib_Timer_Start(
3195         ourTimerBlock,
3196         (void *) 2,
3197         200LL,  //timer group ticks
3198         &err);
3199 if (err) {printf("timerstart failed %d\n");}
3200 t3 = hplib_Timer_Start(
3201         ourTimerBlock,
3202         (void *) 3,
3203         300LL,  //timer group ticks
3204         &err);
3205 if (err) {printf("timerstart failed %d\n");}
3206 #endif
3207 netcp_cfgReqStats(netapi_handle, our_stats_cb, 1,&err);
3208 if (err!=0) {printf("stats req failed\n");}
3210 /*********************************************/
3211 /*****************end NETAPI STARTUP**********/
3212 /*********************************************/
3215 #if 0
3216 /********************************************
3217 * Basic pkt loopback test
3218 *********************************************/
3219 printf("...runnining pure push/pop benchmark\n");
3220 our_pktio_bench(100);
3221 #endif
3223 /**************unused stuff******************/
3224 #if 0
3225 /* create TRIE */
3226 P_trie = trie_new();
3227 if (!P_trie) {printf("trie alloc failed\n"); exit(1);}
3229 nat = (HEAD_T *) malloc(NE * sizeof(HEAD_T));
3230 if (!nat) {printf("malloc of nat table failed\n"); exit(1);}
3232 //gen_pkts(np2process<NP ? np2process:NP);
3233 n_pkt= np2process;
3235 /* build table */
3236 build_table(P_trie);
3239 /* processing loop: get pkt, check it, look up in table, copy new header,
3240    send packet */
3241 srand((unsigned) np2process);
3242 #endif
3244 /*********************************************/
3245 /**************Entry point into scheduler ****/
3246 /*********************************************/
3247 netapi_schedWaitForEvents(our_sched, &err);
3249 /* done */
3250 our_stats_cb(netapi_handle, NULL);
3253 #define DO_FAST_POLL
3254 #ifdef DO_FAST_POLL
3255 //example_fast_pushpop(OurHeap, 500);
3256 //example_fast_poll(netcp_rx_chan,20000);
3257 #endif
3259 /*************************************************
3260  ************CLEAN UP****************************
3261  ************************************************/
3263 #ifndef EXPERIMENTAL
3264 //delete Classifiers
3265 netcp_cfgDelClass(netapi_handle, class_0, &err); 
3266 netcp_cfgDelClass(netapi_handle, class_1, &err); 
3267 //netcp_cfgDelClass(netapi_handle, class_2, &err); 
3269 #if 0
3270 //delete flow 
3271 netcp_cfgDelFlow(netapi_handle, specialFlow, &err);
3272 #endif
3273 #endif
3274 #if 1
3275 //delete policy
3276      for (i=0; i < MAX_SEC_INDEX;i++)
3277     {
3278         if (rx_policy[i])
3279             netapi_secDelRxPolicy(netapi_handle, rx_policy[i], &err);
3280      
3281         //delete tunnels
3282         netapi_secDelSA(netapi_handle, 0, sa_info[i].rx_tunnel, &err);
3283         netapi_secDelSA(netapi_handle, 0, sa_info[i].tx_tunnel, &err);
3284 #endif
3285     }
3286 //delete IPs and MAC Interfacess
3287 netcp_cfgDelIp(netapi_handle, 0, 0, NULL, NULL, ip_rule0, &err);
3288 netcp_cfgDelIp(netapi_handle, 1, 0, NULL, NULL, ip_rule1, &err);
3289 netcp_cfgDelMac(netapi_handle,0,&err);
3290 netcp_cfgDelMac(netapi_handle,1,&err);
3293 //close pktio channels we opened
3294 pktio_close(netcp_tx_chan ,&err);
3295 pktio_close(netcp_rx_chan ,&err);
3296 #ifndef EXPERIMENTAL
3297 pktio_close(netcp_sb_tx_chan ,&err);
3298 pktio_close(netcp_sb_rx_chan ,&err);
3300 //clear pktio channel we created
3301 pktio_control(our_chan, (PKTIO_CB) NULL, (PKTIO_CFG_T *) NULL, &zap_channel_control, &err);
3302 pktio_control(netcp_rx_chan2, (PKTIO_CB) NULL, (PKTIO_CFG_T *) NULL, &zap_channel_control, &err);
3304 //delete pktio channels we created
3305 pktio_delete(our_chan, &err);
3306 pktio_delete(netcp_rx_chan2,&err);
3307 #endif
3308 netapi_shutdown(netapi_handle);
3312 //EXAMPLE FAST POLL
3313 /* PLD */
3314 void netapi_pld(void * x)
3315 {   
3316            asm volatile("pld [r0]");
3318 #include "ti/drv/nwal/nwal_util.h"
3320 #define M 1008
3321 static int l3_off[M], l4_off[M], L3_chk_ok[M], L4_chk_ok[M], len[M] ;
3322 static nwal_AppId appid[M];
3323 static unsigned char * buf[M];
3324 #define N2POLL 8 //batch size
3325 #define NTOPOP 190
3326 Ti_Pkt * pHd[NTOPOP];
3327 void example_fast_pushpop(Pktlib_HeapHandle h , int n_trials)
3329 int i,j;
3330 unsigned long t1;
3331 unsigned long t2;
3332 unsigned long sumf=0;
3333 unsigned long sumt=0;
3334 unsigned long sump=0;
3335 #define NTOPOP 190
3336 Ti_Pkt * tempVA;
3337 Qmss_QueueHnd freeQ=Pktlib_getInternalHeapQueue(h);
3339 //n_trials of NTOPOP actions
3340 for(i=0;i<n_trials;i++)
3342     //raw pop
3343     t1= hplib_mUtil_GetTickCount();
3344     pHd[0] = (Ti_Pkt *)QMSS_DESC_PTR(PKTIO_QMSS_QUEUE_POP_RAW (freeQ));
3345     for(j=1;j<NTOPOP;j++)
3346     {
3347        tempVA  =  hplib_mVM_PhyToVirt(pHd[j-1]);
3348        netapi_pld(tempVA);
3349         //__builtin_prefetch(tempVA);
3350        pHd[j] = (Ti_Pkt *)QMSS_DESC_PTR(PKTIO_QMSS_QUEUE_POP_RAW (freeQ));
3351        sumf+= (int) (   ((Cppi_HostDesc *) tempVA)->origBuffPtr -
3352                   ((Cppi_HostDesc *) tempVA)->buffPtr);
3353     }
3354     tempVA  =  hplib_mVM_PhyToVirt(pHd[j-1]);
3355     sumf+= (int) (   ((Cppi_HostDesc *) tempVA)->origBuffPtr -
3356                   ((Cppi_HostDesc *) tempVA)->buffPtr);
3357     t2=hplib_mUtil_GetTickCount();
3358     sumt+= (t2-t1);
3360     //invalidate/wb 
3361     for(j=0;j<NTOPOP;j++)
3362     {
3363         tempVA  =  hplib_mVM_PhyToVirt(pHd[j]);
3364         hplib_CacheWbInv(tempVA,128);
3365     }
3367     //raw push 
3368     t1=hplib_mUtil_GetTickCount();
3369     for(j=0;j<NTOPOP;j++)
3370     {
3371         PKTIO_QMSS_QUEUE_PUSH_DESC_SIZE_RAW (freeQ,
3372                                              (void *) pHd[j],
3373                                              128);
3374     }
3375     t2=hplib_mUtil_GetTickCount();
3376     sump += (t2-t1);
3378 printf("\nfast poppush: np=%d  pop=%d  push=%d sumf=%d\n", (n_trials*NTOPOP),
3379         sumt/(n_trials*NTOPOP),  sump/(n_trials*NTOPOP), sumf);
3380 sleep(1);
3381 return;
3386 void example_fast_poll( PKTIO_HANDLE_T * p_pktio, int max_pkts)
3388 int j=0;
3389 int jj=0;
3390 int i,k,l=0,ltot=0;
3391 int n= N2POLL; //max # of pkts to poll
3392 Ti_Pkt * tempVA;
3393 pasahoLongInfo_t* pinfo;
3394 unsigned long t1;
3395 unsigned long t2;
3396 unsigned long t11;
3397 unsigned long t12;
3398 unsigned long np;
3399 unsigned long sumt=0;
3400 unsigned long sumf=0;
3401 unsigned long sump=0;
3402 unsigned long totlen=0;
3403 int max_batch=0;
3405 //this should  be done once and saved
3406 Qmss_QueueHnd rxQ= PKTIO_GET_DEFAULT_NETCP_Q(p_pktio);
3407 Qmss_QueueHnd freeQ;
3408 //loop forever
3409 for(;max_pkts>0;)
3411     t1= hplib_mUtil_GetTickCount();
3412     pHd[0] = (Ti_Pkt *)QMSS_DESC_PTR(PKTIO_QMSS_QUEUE_POP_RAW (rxQ));
3413     if (!pHd[0]) continue;
3414     //got pkt
3415     for(i=1;(i<n) && (pHd[i-1]);i++)
3416     {
3417         //convert previous descriptor PA -> VA
3418         tempVA  =  hplib_mVM_PhyToVirt(pHd[i-1]); 
3420         //try and preload desriptor
3421          __builtin_prefetch(tempVA);
3422         //netapi_pld(tempVA);
3424         //read next descriptor from queue 
3425         pHd[i] = (Ti_Pkt *)QMSS_DESC_PTR(PKTIO_QMSS_QUEUE_POP_RAW (rxQ));
3426 #if 1   
3427         /* extract some meta data */
3428         Cppi_getData (Cppi_DescType_HOST, (Cppi_Desc*)tempVA, &buf[jj], &len[jj]);
3429         pinfo =  nwal_mGetProtoInfo(tempVA);
3430         l3_off[jj]= nwal_mGetL3OffBytes(pinfo);
3431         l4_off[jj]= nwal_mGetL4Offset(pinfo);
3432         if(nwal_mGetAppidFmPkt(tempVA,&appid[jj]) != nwal_TRUE)
3433         {
3434             printf("ERROR!!!! AppID not available in incoming packet \n");
3435         }
3436 //#define VERIFY_SOP
3437 #ifdef VERIFY_SOP
3438   if (sump < 200)  printf("..sop off=%d\n", (int) buf[jj]-
3439            (int)  ((Cppi_HostDesc *) tempVA)->origBuffPtr) ;
3440 #endif
3441 #endif
3442         jj+=1;
3443     }
3444     //finish last pkt in burst
3445     if(pHd[i-1])
3446     {
3447         //convert previous descriptor PA -> VA
3448         tempVA  =  hplib_mVM_PhyToVirt(pHd[i-1]); 
3450         /* extract some meta data */
3451 #if 1
3452         pinfo =  nwal_mGetProtoInfo(tempVA);
3453         l3_off[jj]= nwal_mGetL3OffBytes(pinfo);
3454         l4_off[jj]= nwal_mGetL4Offset(pinfo);
3455         if(nwal_mGetAppidFmPkt(tempVA,&appid[jj]) != nwal_TRUE)
3456         {
3457             printf("ERROR!!!! AppID not available in incoming packet \n");
3458         }
3459 #endif
3460         //get ptr (Physical address) and length of associate buffer
3461         Cppi_getData (Cppi_DescType_HOST, (Cppi_Desc*)tempVA, &buf[jj], &len[jj]);
3462         jj+=1;
3463     }
3464     t2= hplib_mUtil_GetTickCount();
3465     j+=(pHd[i-1]? i: (i-1)) ;
3466     if (jj>(M-n)) jj=0;
3467     l+=1;  //n batches
3468     ltot+=1;
3469     if(pHd[i-1])
3470     {
3471       if (i>max_batch) max_batch= i;
3472     }
3473     else
3474     {
3475       if( (i-1) >max_batch) max_batch = i-1;
3476     }
3478     //cleanup
3479     //printf("cleanup %d\n",i);
3480     for(k=0;k<i;k++)
3481     {
3482         //cleanup.  need to covert all of desriptor to VA so that i can use freePacket() 
3483         //alternative would be to just do cache ops plus descriptor raw push to pktlib
3484         // heap free queue
3485         if(pHd[k])
3486         {
3488         //tempVA=Qmss_osalConvertDescPhyToVirt(pHd[k]);
3489         tempVA  =  hplib_mVM_PhyToVirt(pHd[k]);
3490         freeQ=Qmss_getQueueHandle(Cppi_getReturnQueue (Cppi_DescType_HOST, (Cppi_Desc *)tempVA));
3491         hplib_CacheWbInv(tempVA,128);
3492         //would need to wbInv buffer also in practice. Also need to walk
3493         // descriptor chain 
3494         t11= hplib_mUtil_GetTickCount();
3495        // Pktlib_freePacket(tempVA);
3496         PKTIO_QMSS_QUEUE_PUSH_DESC_SIZE_RAW (freeQ,
3497                                              (void *) pHd[k],
3498                                              128);
3500         t12= hplib_mUtil_GetTickCount();
3501         sumf += (t12-t11);       
3502         }
3503     }
3504     sumt += (t2-t1);
3505     sump +=(pHd[i-1]? i: (i-1));
3506     max_pkts -= (pHd[i-1]? i: (i-1));
3508     //printf("end cleanup %d %d %d\n",sumt,sumf,sump );
3509     if (sump > 10000) {
3510        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", 
3511                   j,ltot, appid[j%M], 
3512                   l3_off[j%M],l4_off[j%M],  
3513                   len[j%M],buf[j%M],
3514                    sumt/sump,  (sump*1000)/l, max_batch,
3515                    sumf/sump);
3516        sumt=sump=sumf=0;
3517        l=0;
3518     }
3521     //cleanup any remaining buffers
3522     for(;;)
3523     {
3524         pHd[0] = (Ti_Pkt *)QMSS_DESC_PTR(PKTIO_QMSS_QUEUE_POP_RAW (rxQ));
3525         if(!pHd[0]) break;
3526         tempVA  =  Osal_qmssPhyToVirt(pHd[0]);
3527         freeQ=Qmss_getQueueHandle(Cppi_getReturnQueue (Cppi_DescType_HOST, (Cppi_Desc *)tempVA));
3528         hplib_CacheWbInv(tempVA,128);
3529         PKTIO_QMSS_QUEUE_PUSH_DESC_SIZE_RAW (freeQ,
3530                                              (void *) pHd[0],
3531                                              128);
3532     }
3535 #ifdef EXPERIMENTAL
3537 static inline void send_it(Ti_Pkt *tip, int len, ROUTE_SEC_T * p_sec)
3539   unsigned long st1;
3540   unsigned long st2;
3541   int err=0;
3542   PKTIO_METADATA_T meta2 = {PKTIO_META_TX,{0},0};
3543   nwalTxPktInfo_t meta_tx2={0};
3544   st1=hplib_mUtil_GetTickCount();
3545   if (len<60) 
3546   { 
3547      unsigned int templen;
3548      char * p_pkt;
3549      len=60; 
3550      Pktlib_getDataBuffer(tip,(uint8_t**)&p_pkt,&templen);//ignore templen
3551      Cppi_setData (Cppi_DescType_HOST, (Cppi_Desc *) tip, p_pkt,len);
3552      stats.tx_min+=1;
3553   }
3554   Pktlib_setPacketLen(tip,len);
3555   meta_tx2.txFlag1 = (NWAL_TX_FLAG1_DO_IPV4_CHKSUM| NWAL_TX_FLAG1_META_DATA_VALID );
3556   meta_tx2.startOffset = 0;
3557   meta_tx2.ipOffBytes = 14;
3558   meta_tx2.ploadLen = len ;
3559   if(p_sec)
3560   {   
3561        meta_tx2.txFlag1 |= NWAL_TX_FLAG1_DO_IPSEC_ESP_CRYPTO ;
3562        meta2.sa_handle=p_sec->tx_inflow_mode_handle; //this tells netapi that inflow crypto needs to be applied
3563        meta_tx2.enetPort=0;
3564        meta_tx2.saOffBytes=14+20;
3565        meta_tx2.saPayloadLen=len-14-20;   //don't include tag, mac and outer header
3566        stats.sec_tx+=1;
3567   }
3568   meta2.u.tx_meta=&meta_tx2;
3569   pktio_send(netcp_tx_chan,tip,&meta2,&err);
3570   stats.tx +=1;
3571   st2=hplib_mUtil_GetTickCount();
3572   stats.send_cycles += (unsigned long long) (st2-st1);  
3573
3574 void recv_cb_router(struct PKTIO_HANDLE_Tag * channel, Ti_Pkt* p_recv[],
3575                          PKTIO_METADATA_T meta[], int n_pkts,
3576                          uint64_t ts )
3578 int i;
3579 int len;
3580 int p;
3581 Ti_Pkt * tip;
3582 unsigned int templen;
3583 char * p_pkt;
3584 HEAD_T temp_head;
3585 unsigned int appid;
3586 IP_HEAD_T th;
3587 ROUTE_SEC_T *sec_data=NULL;
3588 unsigned long t1;
3589 unsigned long t2;
3590 unsigned long ct1;
3591 unsigned long ct2;
3592 unsigned short ip_pl;
3593 int n_c_ops;
3594 t1=hplib_mUtil_GetTickCount();
3595 ct1 =Osal_cache_op_measure(&n_c_ops);
3596 for(i=0;i<n_pkts;i++)
3598         tip = p_recv[i];
3599         Pktlib_getDataBuffer(tip,(uint8_t**)&p_pkt,&templen);//ignore templen
3600         len = Pktlib_getPacketLen(tip)-4;//real length, subtract mac trailer
3601         stats.rx+=1;
3602         appid = ((unsigned int)meta[i].u.rx_meta->appId)&0xff000000;
3603         switch(appid)
3604         {
3605            case(NETAPI_NETCP_MATCH_IPSEC):
3606            case(NETAPI_NETCP_MATCH_IPSEC_POLICY):
3607            {
3608              int tailen=12+2;
3609              memcpy(&temp_head,&p_pkt[14],sizeof(HEAD_T));
3610              if (!check_header(&temp_head,&meta[i])) {
3611                 stats.n_bad+=1;
3612                 Pktlib_freePacket(tip); 
3613                 continue;
3614              }
3615              tailen+=p_pkt[len-12-2]; //padding length  (12)should come from sec_ptr
3616              p_pkt = &p_pkt[8+16+20];   //16= iv len, should come from sec_ptr
3617              len -= (8+16+20+tailen);  //16= iv len should come from sec ptr
3618             
3619              //now check inner headder.
3620              memcpy(&th,&p_pkt[14],20);
3621              if (!check_header(&temp_head,&meta[i])) {
3622                 stats.n_bad+=1;
3623                 Pktlib_freePacket(tip);
3624                 continue;
3625              }
3626              Cppi_setData (Cppi_DescType_HOST, (Cppi_Desc *) tip, p_pkt,len);
3627              Pktlib_setPacketLen(tip,len);
3628              
3629              if (route_pkt(our_router, tip, &th, p_pkt, &len,&sec_data)<0)
3630               {
3631                 stats.n_bad+=1;
3632                 Pktlib_freePacket(tip);
3633              }
3634              else
3635              {
3636                send_it(tip,len,sec_data);
3637              }
3638              break;
3639            }
3640            case(NETAPI_NETCP_MATCH_GENERIC_MAC):
3641               if((p_pkt[12]!=0x8)||(p_pkt[13]!=0x00)) 
3642               {
3643                 stats.n_new+=1;
3644                 Pktlib_freePacket(tip); 
3645                 continue;
3646               }
3647               if (!check_header(&temp_head,&meta[i])) 
3648               {
3649                 stats.n_bad+=1;
3650                 Pktlib_freePacket(tip);
3651                 continue;
3652               }
3653               memcpy(&th,&p_pkt[14],20);
3654               ip_pl= (((unsigned char *)&th.w1)[2]<<8) | ((unsigned char *)&th.w1)[3];
3655               if ((ip_pl+14)<60)
3656               {
3657                 len-= (60-(ip_pl+14));
3658                 stats.rx_min+=1;
3659               }
3660               Pktlib_setPacketLen(tip,len);
3661               if (route_pkt(our_router, tip, &th, p_pkt, &len,&sec_data)<0)
3662               {
3663                 stats.n_bad+=1;
3664                 Pktlib_freePacket(tip);
3665               }
3666               else
3667               {
3668                  send_it(tip,len,sec_data);
3669               }
3670               break;
3671            case(NETAPI_NETCP_MATCH_GENERIC_IP):
3672               Pktlib_freePacket(tip); 
3673               stats.n_new=1;
3674               break;
3675            default:
3676               stats.n_new+=1;
3677               Pktlib_freePacket(tip);
3678               break;
3679        }
3681 t2=hplib_mUtil_GetTickCount();
3682 ct2 =Osal_cache_op_measure(&n_c_ops);
3683 stats.app_cycles +=  (unsigned long long) (t2-t1);
3684 stats.tx_cache_cycles += (unsigned long long) (ct2-ct1);
3685 return;
3687 #endif