Multiple interface routing support updates.
[keystone-rtos/netapi.git] / ti / runtime / netapi / applications / ipsec_offload / ipsecmgr / src / netapi_ipsecmgr.c
index dd0d2022da5140e9b78948aa52ff74a54bbb86dc..8e95419a79c7205791056d7c74f0ae03b821c45e 100755 (executable)
 #include <stdlib.h>
 #include <signal.h>
 #include <stdarg.h>
+
+
+
+#include <arpa/inet.h>
+#include <sys/socket.h>
+#include <ifaddrs.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <sys/types.h>
+#include <netinet/in.h> 
+#include <string.h> 
+
 /**********************************************************************
  ************************** Local Definitions *************************
  **********************************************************************/
-#define DTS_LOG_FILE_QUEUE "/proc/device-tree/soc/pktdma@2004000/channels/netrx0/complete-queue"
-#define DTS_LOG_FILE_FLOW  "/proc/device-tree/soc/pktdma@2004000/channels/netrx0/flow"
+char*  DTS_LOG_FILE_QUEUE_ETHx[] = {
+ "/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"};
 
 
 /**********************************************************************
@@ -78,11 +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 */
@@ -149,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 */
@@ -162,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 shutdowns\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);
@@ -184,12 +284,7 @@ static void* snoop_run_thread (void* arg)
  */
 static void sig_term_handler(int signum)
 {
-
     QUIT = 1;
-    /* Cleanup and exit */        
-    //ipsecmgr_snoop_shutdown ();
-    //netapi_shutdown(netapi_handle);
-    //exit (0);
 }
 
 /**
@@ -477,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,
@@ -494,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;
 }
 
@@ -596,81 +696,28 @@ 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;
-
-#if 0
-            /* Close the default log file if we opened it already */
-            if (netapiIPSecMgrMcb.logFd)
-                fclose (netapiIPSecMgrMcb.logFd);                    
-
-            /* Open the log file specified at command line */
-            if ((netapiIPSecMgrMcb.logFd  = fopen(argv[i+1], "a")) == NULL)
-            {
-                printf ("Error opening log file %s\n", argv[i+1]);
-                return -1;
-            }
-#endif
-            i++;
-        }
-
-        else
-        {
-            /* Invalid options */                
-            return -1;
-        }
-    }
-
-    /* Success */
-    return 0;
-}
-
 int get_kernel_config()
 {
     uint32_t temp=0;
-    char name[] = "offload_chan";
     FILE *pDts = NULL;
 
-    pDts = fopen(DTS_LOG_FILE_QUEUE, "rb");
+    pDts = fopen(DTS_LOG_FILE_QUEUE_ETHx[0], "rb");
 
     if(pDts)
     {
         fread((void*)&temp, sizeof(uint32_t), 1, pDts);
-        globalDB.qNum= (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);
+            "main: error opening device tree file: %s\n",DTS_LOG_FILE_QUEUE_ETHx[0]);
         return -1;
     }
 
     pDts = NULL;
-    pDts = fopen(DTS_LOG_FILE_FLOW, "rb");
+    pDts = fopen(DTS_LOG_FILE_FLOW_ETHx[0], "rb");
 
     if(pDts)
     {
@@ -680,20 +727,32 @@ int get_kernel_config()
     }
     else
     {
-        printf("main: error opening %s\n",DTS_LOG_FILE_FLOW);
+        ipsecmgr_syslog_msg (SYSLOG_LEVEL_ERROR,
+            "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: flow: 0x%x, qNum: 0x%x\n", 
+             globalDB.flowId, globalDB.qNum);
     return 0;
 }
 
 int create_pktio_channel()
 {
+    static int count = 0;
     int error = 0;
-    char name[] = "offload_chan";
+    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;
@@ -711,6 +770,23 @@ int create_pktio_channel()
         return -1;
     }
 
+    printf("create_pktio_channel: qnum: %d, pktio_handle: 0x%x\n",
+            pktio_cfg.qnum,
+            globalDB.pktio_channel);
+
+    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);
+
     return 0;
 }
 /**
@@ -731,23 +807,36 @@ int create_pktio_channel()
 int32_t main (int argc, char* argv[])
 {
     int32_t             retVal;
+    int i, iface;
+    char* pTok = NULL;
+    int ip_entry_count = 0;
+    struct ifaddrs *ifaddr, *ifa;
+    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));
 
+    /* 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);
-
+    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");
-        printf("main: 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);
-
-        printf("main: netapi_init passed\n");
+        netapi_netcpCfgExceptions(netapi_handle,
+                                  NETCP_CFG_ALL_EXCEPTIONS,
+                                  NETCP_CFG_ACTION_DISCARD,
+                                  (NETCP_CFG_ROUTE_HANDLE_T) NULL);
 
     if (get_kernel_config())
     {
@@ -755,25 +844,19 @@ int32_t main (int argc, char* argv[])
                             "ERROR: main: get_kernel_config() failed\n");
         return -1;
     }
-
-        if (create_pktio_channel())
-    {
-        ipsecmgr_syslog_msg (SYSLOG_LEVEL_ERROR,
-                            "ERROR: main: create_pktio_channel() failed\n");
-        return -1;
-    }
-
-#if 1
-    /* Read and parse command line options */
-    if (parse_cmd_line_args (argc, argv) < 0)
+    else
     {
-        printf ("Usage format: netfpproxy.out [-f <log_file>]\n");
-        return -1;
+        /* create pktio channel */
+        if(create_pktio_channel())
+        {
+            ipsecmgr_syslog_msg (SYSLOG_LEVEL_ERROR,
+                            "ERROR: main: pktio channel creation failed\n");
+            return -1;
+        }
     }
-#endif
+     /*Create the proxy daemon. */
     printf("main: calling daemonize\n");
-    /* Create the proxy daemon. */
-    daemonize (LOCK_FILE);
+    daemonize (LOCK_FILE); 
 
     /* Initialize and start the IPSec Mgr Snoop functionality */
     if ((retVal = init_ipsecmgr ()) < 0)
@@ -784,6 +867,7 @@ int32_t main (int argc, char* argv[])
         return -1;
     }
     else
-        printf("main: ipsecmgr daemon shutdonw complete\n");
+        ipsecmgr_syslog_msg (SYSLOG_LEVEL_INFO,
+                             "main: ipsecmgr daemon shutdonw complete\n");
 
 }