Multiple interface routing support updates.
[keystone-rtos/netapi.git] / ti / runtime / netapi / applications / ipsec_offload / ipsecmgr / src / netapi_ipsecmgr.c
index c6995d53da2bd44fb8e4ae5a20858bd3d4319e10..8e95419a79c7205791056d7c74f0ae03b821c45e 100755 (executable)
  ************************** Local Definitions *************************
  **********************************************************************/
 char*  DTS_LOG_FILE_QUEUE_ETHx[] = {
- "/proc/device-tree/soc/pktdma@2004000/channels/netrx0/complete-queue",
- "/proc/device-tree/soc/pktdma@2004000/channels/netrx1/complete-queue",
- "/proc/device-tree/soc/pktdma@2004000/channels/netrx2/complete-queue",
- "/proc/device-tree/soc/pktdma@2004000/channels/netrx3/complete-queue",
- "/proc/device-tree/soc/pktdma@2004000/channels/netrx4/complete-queue",
- "/proc/device-tree/soc/pktdma@2004000/channels/netrx5/complete-queue",
- "/proc/device-tree/soc/pktdma@2004000/channels/netrx6/complete-queue",
- "/proc/device-tree/soc/pktdma@2004000/channels/netrx7/complete-queue",
- "/proc/device-tree/soc/pktdma@2004000/channels/netrx8/complete-queue",
- "/proc/device-tree/soc/pktdma@2004000/channels/netrx9/complete-queue",
- "/proc/device-tree/soc/pktdma@2004000/channels/netrx10/complete-queue",
- "/proc/device-tree/soc/pktdma@2004000/channels/netrx11/complete-queue",
- "/proc/device-tree/soc/pktdma@2004000/channels/netrx12/complete-queue",
- "/proc/device-tree/soc/pktdma@2004000/channels/netrx13/complete-queue",
- "/proc/device-tree/soc/pktdma@2004000/channels/netrx14/complete-queue",
- "/proc/device-tree/soc/pktdma@2004000/channels/netrx15/complete-queue"
-};
+ "/proc/device-tree/soc/pktdma@2004000/channels/netrx0/complete-queue"};
 char*  DTS_LOG_FILE_FLOW_ETHx[] = {
-"/proc/device-tree/soc/pktdma@2004000/channels/netrx0/flow",
-"/proc/device-tree/soc/pktdma@2004000/channels/netrx1/flow",
-"/proc/device-tree/soc/pktdma@2004000/channels/netrx2/flow",
-"/proc/device-tree/soc/pktdma@2004000/channels/netrx3/flow",
-"/proc/device-tree/soc/pktdma@2004000/channels/netrx4/flow",
-"/proc/device-tree/soc/pktdma@2004000/channels/netrx5/flow",
-"/proc/device-tree/soc/pktdma@2004000/channels/netrx6/flow",
-"/proc/device-tree/soc/pktdma@2004000/channels/netrx7/flow",
-"/proc/device-tree/soc/pktdma@2004000/channels/netrx8/flow",
-"/proc/device-tree/soc/pktdma@2004000/channels/netrx9/flow",
-"/proc/device-tree/soc/pktdma@2004000/channels/netrx10/flow",
-"/proc/device-tree/soc/pktdma@2004000/channels/netrx11/flow",
-"/proc/device-tree/soc/pktdma@2004000/channels/netrx12/flow",
-"/proc/device-tree/soc/pktdma@2004000/channels/netrx13/flow",
-"/proc/device-tree/soc/pktdma@2004000/channels/netrx14/flow",
-"/proc/device-tree/soc/pktdma@2004000/channels/netrx15/flow",
-};
+"/proc/device-tree/soc/pktdma@2004000/channels/netrx0/flow"};
+
 
 /**********************************************************************
  ************************** Global Variables **************************
@@ -109,7 +78,6 @@ NETAPI_T netapi_handle;
 
 //paSysStats_t netcp_stats;
 ipsecMgrMcb_t globalDB;
-ipsecMgrIfConfigEntry_T ipConfigList[16];
 
 /* Lock file for the daemon */
 #define LOCK_FILE   "/var/lock/ipsecmgr_daemon"
@@ -126,13 +94,13 @@ static NETAPI_CFG_T our_netapi_default_cfg=
     TUNE_NETAPI_DEFAULT_NUM_BUFFERS,   //#descriptors+buffers in default heap
     64, //#descriptors w/o buffers in default heap
     TUNE_NETAPI_DEFAULT_BUFFER_SIZE+128+128,  //size of buffers in default heap
-    128   ,  //tail room
-    256      //extra room
+    128,    //tail room
+    256,    //extra room
+    0
 };
 
 
 
-
 static int QUIT = 0;
 
 /* stub functions */
@@ -199,12 +167,87 @@ void cleanup_sa_sp()
     }
 }
 
+//void netTest_utilsStatsCb(NETAPI_T h, paSysStats_t* pPaStats)
+NETAPI_SA_STATS_T netapi_sa_statsrx;
+NETAPI_SA_STATS_T netapi_sa_statstx;
+
+
+void printIPSecStats(Sa_IpsecStats_t* p_saIpsecStats,
+                     nwal_saAALG auth,
+                     nwal_saEALG cipher,
+                     uint32_t    saAppId)
+{
+
+    printf("\n appId: 0x%x: Autentication mode: %d, Encryption Mode: %d\n",
+                saAppId,
+                auth,
+                cipher);
+                
+    printf("IPSec replayOld:0x%x,replayDup:0x%x,authFail:0x%x \n",
+                   p_saIpsecStats->replayOld,p_saIpsecStats->replayDup,p_saIpsecStats->authFail);
+    printf("IPSec txESN:0x%x,rxESN:0x%x,pktEncHi:0x%x,pktEncLo:0x%x,pktDecHi:0x%x,pktDecLo:0x%x \n",
+                   p_saIpsecStats->txESN,p_saIpsecStats->rxESN,p_saIpsecStats->pktEncHi,
+                   p_saIpsecStats->pktEncLo,p_saIpsecStats->pktDecHi,p_saIpsecStats->pktDecLo);
+}
+
+
+
+
 
+void utilsStatsCb(NETAPI_T h)
+{
+    uint32_t numFreeDataPackets;
+    uint32_t            numZeroBufferPackets;
+    uint32_t            numPacketsinGarbage;
+    Pktlib_HeapStats    pktLibHeapStats;
+    int i,j;
+
+
+
+
+        if(globalDB.rx_sa[0].saAppId)
+        {
+            /* Statistics for RX Tunnel */
+            memset(&netapi_sa_statsrx, 0, sizeof(netapi_sa_statsrx));
+            netapi_getSaStats(netapi_handle,
+                              globalDB.rx_sa[0].saAppId,
+                              &netapi_sa_statsrx);
+            if (netapi_sa_statsrx.validParams & NETAPI_IPSEC_STAT_VALID)
+            {
+                printIPSecStats(&netapi_sa_statsrx.saIpsecStats,
+                                 NWAL_SA_AALG_HMAC_SHA1,
+                                 NWAL_SA_EALG_AES_CBC,
+                                 globalDB.rx_sa[0].saAppId);
+            }
+        }
+#if 1
+        if (globalDB.tx_sa[0].saAppId)
+        {
+            memset(&netapi_sa_statstx, 0, sizeof(netapi_sa_statstx));
+            netapi_getSaStats(netapi_handle,
+                              globalDB.tx_sa[0].saAppId,
+                              &netapi_sa_statstx);
+            if (netapi_sa_statstx.validParams & NETAPI_IPSEC_STAT_VALID)
+            {
+                 printIPSecStats(&netapi_sa_statstx.saIpsecStats,
+                                   NWAL_SA_AALG_HMAC_SHA1,
+                                   NWAL_SA_EALG_AES_CBC,
+                                   globalDB.tx_sa[0].saAppId);
+            }
+        }
+#endif
+}
 static void* snoop_run_thread (void* arg)
 {
+    cpu_set_t cpu_set;
+    CPU_ZERO( &cpu_set);
+    CPU_SET( 0, &cpu_set);
+
+    hplib_utilSetupThread(0, &cpu_set, hplib_spinLock_Type_LOL);
     ipsecmgr_syslog_msg(SYSLOG_LEVEL_INFO,
         "snoop_run_thread: daemon entering forever event loop\n");
-
+    int count=0;
+    int sat=500000;
     while (1)
     {
         /* Poll for message from user application */
@@ -212,11 +255,18 @@ static void* snoop_run_thread (void* arg)
 
         /* Poll for message from Kernel */
         ipsecmgr_snoop_run();
-
+#if 1
+        if((count % 500000) == 0)
+            utilsStatsCb(netapi_handle);
+        
+#endif
+        count++;
         if (QUIT == 1)
             break;
     }
-    printf("snoop_run_thread: calling shutdown\n");
+    ipsecmgr_syslog_msg(SYSLOG_LEVEL_INFO,
+        "snoop_run_thread: calling shutdown\n");
+    utilsStatsCb(netapi_handle);
     ipsecmgr_snoop_shutdown ();
     cleanup_sa_sp();
     netapi_shutdown(netapi_handle);
@@ -522,9 +572,14 @@ static int32_t init_ipsecmgr (void)
         return -1;
     }
 
+
+
+#ifdef GDB_DEBUG
+    snoop_run_thread(NULL);
+#else
     /* Create the task context for snoop library */
     pthread_attr_init(&threadAttr);
-    pthread_attr_setstacksize(&threadAttr, 0x8000);
+    pthread_attr_setstacksize(&threadAttr, 0x10000);
     if (pthread_create(&snoop_run_th, (void*) NULL, snoop_run_thread, NULL))
     {
         ipsecmgr_syslog_msg (SYSLOG_LEVEL_ERROR,
@@ -539,7 +594,7 @@ static int32_t init_ipsecmgr (void)
     }
     /* Wait for the NETAPI Proxy task to finish its processing and exit. */
     pthread_join (snoop_run_th, NULL);
-
+#endif
     return 0;
 }
 
@@ -641,115 +696,97 @@ static void daemonize (const char *lock_file)
     return;
 }
 
-/* FUNCTION PURPOSE:validate and process inputs to the application */
-int32_t parse_cmd_line_args(int argc, char** argv)
-{
-    int32_t             i;
-
-    /* Valid command line options */
-    if (argc == 1)
-    {
-        printf("netapi_proxyParseCommandLineArgs, argc =1\n");
-        return 0;
-    }
-    else
-    {
-           printf("netapi_proxyParseCommandLineArgs, argc=%d\n", argc);
-     
-    }
-
-    /* Scan and parse the command line options */
-    for (i = 1; i < argc; i ++)
-    {
-        if (argv[i][0] == '-' && argv[i][1] == 'f')
-        {
-            /* Log file name */                
-            if (i+1 >= argc || argv[i+1] == NULL)
-                return -1;
-            i++;
-        }
-        else
-        {
-            /* Invalid options */
-            return -1;
-        }
-    }
-    /* Success */
-    return 0;
-}
-
-int get_kernel_config(int index)
+int get_kernel_config()
 {
     uint32_t temp=0;
     FILE *pDts = NULL;
 
-    pDts = fopen(DTS_LOG_FILE_QUEUE_ETHx[index], "rb");
+    pDts = fopen(DTS_LOG_FILE_QUEUE_ETHx[0], "rb");
 
     if(pDts)
     {
         fread((void*)&temp, sizeof(uint32_t), 1, pDts);
-        globalDB.qNum[index]= (int)swap32(temp);
+        globalDB.qNum = (int)swap32(temp);
         fclose(pDts);
     }
     else
     {
         ipsecmgr_syslog_msg (SYSLOG_LEVEL_ERROR,
-            "main: error opening device tree file: %s\n",DTS_LOG_FILE_QUEUE_ETHx[index]);
+            "main: error opening device tree file: %s\n",DTS_LOG_FILE_QUEUE_ETHx[0]);
         return -1;
     }
 
     pDts = NULL;
-    pDts = fopen(DTS_LOG_FILE_FLOW_ETHx[index], "rb");
+    pDts = fopen(DTS_LOG_FILE_FLOW_ETHx[0], "rb");
 
     if(pDts)
     {
         fread((void*)&temp, sizeof(uint32_t), 1, pDts);
-        globalDB.flowId[index] = (int)swap32(temp);
+        globalDB.flowId = (int)swap32(temp);
         fclose(pDts);
     }
     else
     {
         ipsecmgr_syslog_msg (SYSLOG_LEVEL_ERROR,
-            "main: error opening device tree file: %s\n",DTS_LOG_FILE_FLOW_ETHx[index]);
+            "main: error opening device tree file: %s\n",DTS_LOG_FILE_FLOW_ETHx[0]);
         return -1;
     }
     ipsecmgr_syslog_msg (SYSLOG_LEVEL_INFO,
-            "get_kernel_config(%d): flow: 0x%x, qNum: 0x%x\n", 
-                    index, globalDB.flowId[index], globalDB.qNum[index]);
+            "get_kernel_config: flow: 0x%x, qNum: 0x%x\n", 
+             globalDB.flowId, globalDB.qNum);
     return 0;
 }
 
-int create_pktio_channel(int index)
+int create_pktio_channel()
 {
+    static int count = 0;
     int error = 0;
     char name[19];
     PKTIO_HANDLE_T *pktio_channel;
     PKTIO_CFG_T pktio_cfg;
+    NETCP_CFG_ROUTE_T route;
+    NETCP_CFG_FLOW_T flow;
+    NETCP_CFG_EXCEPTION_PKT_T expPkt_appid = 0;
+
+    memset(&pktio_cfg,0,sizeof(PKTIO_CFG_T));
+    memset((void *)&route, 0, sizeof (NETCP_CFG_ROUTE_T));
+    memset((void *)&flow, 0, sizeof (NETCP_CFG_FLOW_T));
+
+    sprintf(&name[0],"%s","offload_0");
+    pktio_cfg.qnum = globalDB.qNum;
+    pktio_cfg.flags1 = PKTIO_RX;
+    pktio_cfg.flags2 = PKTIO_GLOBAL | PKTIO_PKT;
+    pktio_cfg.max_n = 8;
+
+    globalDB.pktio_channel = netapi_pktioCreate(netapi_handle,
+                                               &name[0],
+                                               (PKTIO_CB)recv_cb,
+                                               &pktio_cfg,
+                                               &error);
+    if (!globalDB.pktio_channel)
+    {
+        ipsecmgr_syslog_msg (SYSLOG_LEVEL_ERROR,
+            "create_pktio_channel: failed\n");
+        return -1;
+    }
 
-        memset(&pktio_cfg,0,sizeof(PKTIO_CFG_T));
+    printf("create_pktio_channel: qnum: %d, pktio_handle: 0x%x\n",
+            pktio_cfg.qnum,
+            globalDB.pktio_channel);
 
-        sprintf(&name[0],"%s%d","offload_", index);
-        pktio_cfg.qnum = globalDB.qNum[index];
-        pktio_cfg.flags1 = PKTIO_RX;
-        pktio_cfg.flags2 = PKTIO_GLOBAL | PKTIO_PKT;
-        pktio_cfg.max_n = 8;
+    flow.dma_engine= 1;
+    flow.flowid = globalDB.flowId;
+    route.p_dest_q = globalDB.pktio_channel;
+    route.p_flow = &flow;
+    route.valid_params |= NETCP_CFG_VALID_PARAM_ROUTE_TYPE;
+    route.routeType = NWAL_ROUTE_RX_INTF_W_FLOW;
+    /* enable exception packet handling for fragmented packets */
+    expPkt_appid = netapi_netcpCfgExceptions(netapi_handle,
+                                             7,
+                                             NETCP_CFG_ACTION_TO_SW,
+                                             (NETCP_CFG_ROUTE_HANDLE_T) &route);
+    printf("create_pktio_channel: expPkt appid: 0x%x\n", expPkt_appid);
 
-        globalDB.pktio_channel[index] = netapi_pktioCreate(netapi_handle,
-                                                   &name[0],
-                                                   (PKTIO_CB)recv_cb,
-                                                   &pktio_cfg,
-                                                   &error);
-        if (!globalDB.pktio_channel[index])
-        {
-            ipsecmgr_syslog_msg (SYSLOG_LEVEL_ERROR,
-                "create_pktio_channel: failed\n");
-            return -1;
-        }
-        else
-        {
-            ipsecmgr_syslog_msg (SYSLOG_LEVEL_INFO,
-            "created pktio channel for queue :0x%x\n", globalDB.qNum[index]);
-        }
     return 0;
 }
 /**
@@ -774,111 +811,52 @@ int32_t main (int argc, char* argv[])
     char* pTok = NULL;
     int ip_entry_count = 0;
     struct ifaddrs *ifaddr, *ifa;
-           int family, s;
-           char host[NI_MAXHOST];
-           char sa_data[14];
+    int family, s;
+    char host[NI_MAXHOST];
+    struct sockaddr_in6 ipv6_addr;
+    cpu_set_t cpu_set;
     ipsecmgr_syslog_init();
 
     memset(&globalDB, 0, sizeof(globalDB));
-    memset(&ipConfigList, 0, sizeof(ipConfigList));
-    /* create netapi */
-    netapi_handle = netapi_init(NETAPI_SYS_MASTER, &our_netapi_default_cfg);
 
+    /* assign main net_test thread to run on core 0 */
+    CPU_ZERO( &cpu_set);
+    CPU_SET( 0, &cpu_set);
+    hplib_utilSetupThread(0, &cpu_set, hplib_spinLock_Type_LOL);
+    /* create netapi */
+    netapi_handle = netapi_init(NETAPI_SYS_MASTER,
+                                &our_netapi_default_cfg);
     if(netapi_handle == NULL)
     {
-        ipsecmgr_syslog_msg (SYSLOG_LEVEL_ERROR, "ERROR: netapi_init failed\n");
+        ipsecmgr_syslog_msg (SYSLOG_LEVEL_ERROR,
+                             "ERROR: netapi_init failed\n");
         return -1;
     }
     else
-        netapi_netcpCfgExceptions(netapi_handle, NETCP_CFG_ALL_EXCEPTIONS, NETCP_CFG_ACTION_DISCARD, (NETCP_CFG_ROUTE_HANDLE_T) NULL);
-
-    ipsecmgr_syslog_msg (SYSLOG_LEVEL_INFO, "ERROR: netapi_init failed\n");
-
-
-    for (i = 0; i < 16; i++)
-    {
-        if (get_kernel_config(i))
-        {
-            ipsecmgr_syslog_msg (SYSLOG_LEVEL_ERROR,
-                                "ERROR: main: get_kernel_config() failed\n");
-            continue;
-        }
-        else
-        {
-            /* create pktio channel */
-            if(create_pktio_channel(i))
-            {
-                ipsecmgr_syslog_msg (SYSLOG_LEVEL_ERROR,
-                                "ERROR: main: pktio channel creation failed\n");
-            }
-        }
-    }
-    /* Read and parse command line options */
-    if (parse_cmd_line_args (argc, argv) < 0)
-    {
-        printf ("Usage format: netfpproxy.out [-f <log_file>]\n");
-        return -1;
-    }
-    printf("main: calling daemonize\n");
+        netapi_netcpCfgExceptions(netapi_handle,
+                                  NETCP_CFG_ALL_EXCEPTIONS,
+                                  NETCP_CFG_ACTION_DISCARD,
+                                  (NETCP_CFG_ROUTE_HANDLE_T) NULL);
 
-    /* Create the proxy daemon. */
-    //daemonize (LOCK_FILE);
-
-    if (getifaddrs(&ifaddr) == -1)
+    if (get_kernel_config())
     {
         ipsecmgr_syslog_msg (SYSLOG_LEVEL_ERROR,
-                                "main: getifaddrs failure\n");
+                            "ERROR: main: get_kernel_config() failed\n");
         return -1;
     }
-
-    for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
+    else
     {
-        if (ifa->ifa_addr == NULL)
-            continue;
-
-        family = ifa->ifa_addr->sa_family;
-
-        if (family == AF_INET)
+        /* create pktio channel */
+        if(create_pktio_channel())
         {
-            s = getnameinfo(ifa->ifa_addr,sizeof(struct sockaddr_in),
-                            host,
-                            NI_MAXHOST,
-                            NULL,
-                            0,
-                            NI_NUMERICHOST); 
-            if (s != 0)
-            {
-                ipsecmgr_syslog_msg (SYSLOG_LEVEL_ERROR,
-                    "getnameinfo() failed: %s\n", gai_strerror(s));
-                    continue;
-            }
-            for (i=0;i<4;i++)
-            {
-                ipConfigList[ip_entry_count].ip[i] = ifa->ifa_addr->sa_data[i+2];
-            }
-            memcpy(&ipConfigList[ip_entry_count].name[0],
-                    ifa->ifa_name,
-                    strlen(ifa->ifa_name));
-            pTok = strtok(ifa->ifa_name, ":.");
-            /* now we have the interface name, is this eth0 or eth1 */
-            if (pTok)
-            {
-                if(strstr(pTok,"eth"))
-                {
-                    sscanf(pTok,"eth%d", &iface);
-                    ipConfigList[ip_entry_count].iface = iface;
-                }
-                else
-                {
-                    ipConfigList[ip_entry_count].iface = -1;
-                }
-                ip_entry_count++;
-            }
+            ipsecmgr_syslog_msg (SYSLOG_LEVEL_ERROR,
+                            "ERROR: main: pktio channel creation failed\n");
+            return -1;
         }
     }
-
-    if (ifaddr)
-        freeifaddrs(ifaddr);
+     /*Create the proxy daemon. */
+    printf("main: calling daemonize\n");
+    daemonize (LOCK_FILE); 
 
     /* Initialize and start the IPSec Mgr Snoop functionality */
     if ((retVal = init_ipsecmgr ()) < 0)