]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - keystone-rtos/netapi.git/blobdiff - ti/runtime/netapi/applications/ipsec_offload/ipsecmgr/src/netapi_ipsecmgr.c
Fix for SDOCM00106989: Need to update netapi ipsecmgr with K2L/K2E device-tree paths...
[keystone-rtos/netapi.git] / ti / runtime / netapi / applications / ipsec_offload / ipsecmgr / src / netapi_ipsecmgr.c
index d474382ad995a22c810e4f40849955c6bdc262fc..ea91baa801c43c61fc235b5fb6b9b468116fda8c 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> 
+
+#if defined(DEVICE_K2H)
+#include <ti/runtime/hplib/device/k2h/src/hplib_device.c>
+#include <ti/drv/nwal/device/k2h/src/nwal_device.c>
+#include <ti/runtime/netapi/device/k2h/src/netapi_device.c>
+#include <ti/drv/qmss/device/k2h/src/qmss_device.c>
+#include <ti/drv/cppi/device/k2h/src/cppi_device.c>
+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"};
+#elif defined (DEVICE_K2K)
+#include <ti/runtime/hplib/device/k2k/src/hplib_device.c>
+#include <ti/drv/nwal/device/k2k/src/nwal_device.c>
+#include <ti/runtime/netapi/device/k2k/src/netapi_device.c>
+#include <ti/drv/qmss/device/k2k/src/qmss_device.c>
+#include <ti/drv/cppi/device/k2k/src/cppi_device.c>
+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"};
+#elif defined (DEVICE_K2L)
+#include <ti/runtime/hplib/device/k2l/src/hplib_device.c>
+#include <ti/drv/nwal/device/k2l/src/nwal_device.c>
+#include <ti/runtime/netapi/device/k2l/src/netapi_device.c>
+#include <ti/drv/qmss/device/k2l/src/qmss_device.c>
+#include <ti/drv/cppi/device/k2l/src/cppi_device.c>
+char*  DTS_LOG_FILE_QUEUE_ETHx[] = {
+ "/proc/device-tree/soc/pktdma@26186000/channels/netrx0/complete-queue"};
+char*  DTS_LOG_FILE_FLOW_ETHx[] = {
+"/proc/device-tree/soc/pktdma@26186000/channels/netrx0/flow"};
+#elif defined (DEVICE_K2E)
+#include <ti/runtime/hplib/device/k2e/src/hplib_device.c>
+#include <ti/drv/nwal/device/k2e/src/nwal_device.c>
+#include <ti/runtime/netapi/device/k2e/src/netapi_device.c>
+#include <ti/drv/qmss/device/k2e/src/qmss_device.c>
+#include <ti/drv/cppi/device/k2e/src/cppi_device.c>
+char*  DTS_LOG_FILE_QUEUE_ETHx[] = {
+ "/proc/device-tree/soc/pktdma@26186000/channels/netrx0/complete-queue"};
+char*  DTS_LOG_FILE_FLOW_ETHx[] = {
+"/proc/device-tree/soc/pktdma@26186000/channels/netrx0/flow"};
+
+#else /*Default */
+#include <ti/runtime/hplib/device/k2h/src/hplib_device.c>
+#include <ti/drv/nwal/device/k2h/src/nwal_device.c>
+#include <ti/runtime/netapi/device/k2h/src/netapi_device.c>
+#include <ti/drv/qmss/device/k2h/src/qmss_device.c>
+#include <ti/drv/cppi/device/k2h/src/cppi_device.c>
+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"};
+
+#endif
+
 /**********************************************************************
  ************************** 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"
+#if 0
+
+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"};
+
+char*  DTS_LOG_FILE_QUEUE_ETHx[] = {
+ "/proc/device-tree/soc/pktdma@26186000/channels/netrx0/complete-queue"};
+char*  DTS_LOG_FILE_FLOW_ETHx[] = {
+"/proc/device-tree/soc/pktdma@26186000/channels/netrx0/flow"};
+#endif
 
 
 /**********************************************************************
 static ipsecmgr_ipc_daemon_send_if_t *send_iface;
 NETAPI_T netapi_handle;
 
-//paSysStats_t netcp_stats;
-ipsecMgrMcb_t globalCfg;
+ipsecMgrMcb_t globalDB;
 
 /* Lock file for the daemon */
 #define LOCK_FILE   "/var/lock/ipsecmgr_daemon"
@@ -78,10 +155,15 @@ 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 */
 static void recv_cb(struct PKTIO_HANDLE_Tag * channel, Ti_Pkt* p_recv[],
                          PKTIO_METADATA_T meta[], int n_pkts,
@@ -104,9 +186,56 @@ static unsigned int swap32 (unsigned int x)
 
 }
 
+void cleanup_sa_sp()
+{
+    int slot, error=0;;
+    /* delete any offloaded rx SA's and policies */
+    /* and delete any offloaded tx SA's */
+    for (slot = 0;slot < 64;slot++)
+    {
+        if(globalDB.rx_sa[slot].in_use)
+        {
+            globalDB.rx_sa[slot].in_use = 0;
+            if(globalDB.rx_sa[slot].spAppId)
+            {
+                netapi_secDelRxPolicy(netapi_handle,
+                              (NETCP_CFG_IPSEC_POLICY_T) globalDB.rx_sa[slot].spAppId,
+                              &error);
+                ipsecmgr_syslog_msg (SYSLOG_LEVEL_INFO,
+                "cleanup_sa_sp: SP deleted: sp_app_id: 0x%x, slot: %d, error: %d\n", 
+                globalDB.rx_sa[slot].spAppId, slot, error);
+            }
+            netapi_secDelSA(netapi_handle,
+                        NETCP_CFG_NO_INTERFACE,
+                        (NETCP_CFG_SA_T) globalDB.rx_sa[slot].saAppId,
+                        &error);
+            ipsecmgr_syslog_msg (SYSLOG_LEVEL_INFO,
+                "cleanup_sa_sp: SA deleted: sa_app_id: 0x%x, slot: %d, error: %d\n", 
+                globalDB.rx_sa[slot].saAppId, slot, error);
+            
+        }
+        if(globalDB.tx_sa[slot].in_use)
+        {
+            globalDB.tx_sa[slot].in_use = 0;
+            netapi_secDelSA(netapi_handle,
+                            NETCP_CFG_NO_INTERFACE,
+                           (NETCP_CFG_SA_T) globalDB.tx_sa[slot].saAppId,
+                            &error);
+            ipsecmgr_syslog_msg (SYSLOG_LEVEL_INFO,
+                "cleanup_sa_sp: SA deleted: sa_app_id: 0x%x, slot: %d, error: %d\n", 
+                globalDB.tx_sa[slot].saAppId, slot, error);
+        }
+    }
+}
+
 
 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");
 
@@ -117,7 +246,14 @@ static void* snoop_run_thread (void* arg)
 
         /* Poll for message from Kernel */
         ipsecmgr_snoop_run();
+        if (QUIT == 1)
+            break;
     }
+    ipsecmgr_syslog_msg(SYSLOG_LEVEL_INFO,
+        "snoop_run_thread: calling shutdown\n");
+    ipsecmgr_snoop_shutdown ();
+    cleanup_sa_sp();
+    netapi_shutdown(netapi_handle);
 
     return;
 }
@@ -127,17 +263,12 @@ static void* snoop_run_thread (void* arg)
  *  @n  
  *      SIGTERM handler.
  *
- *  @param[in]  lock_file
- *      Lock file to be used by the daemon
+ *  @param[in]  signum
+ *      signal number to terminate deamon.
  */
 static void sig_term_handler(int signum)
 {
-
-    /* Cleanup and exit */        
-    ipsecmgr_snoop_shutdown ();
-
-    netapi_shutdown(netapi_handle);
-    exit (0);
+    QUIT = 1;
 }
 
 /**
@@ -425,27 +556,29 @@ 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,
-            "ERROR: NETAPI Proxy Poll Thread failed to start, error code\n"); 
-
-        /* Cleanup the plugin and exit */
-        //netapiIPSecMgrMcb.pluginMcb.exit ();
+            "ERROR: snoop run thread failed to start, error code\n"); 
         return -1;
     }
 
     /* Setup signal handler for SIGTERM */
     if (signal(SIGTERM, sig_term_handler) == SIG_ERR) {
         ipsecmgr_syslog_msg(SYSLOG_LEVEL_WARN,
-            "snoop_run: cannot handle SIGTERM\n");
+            "init_ipsecmgr: cannot handle SIGTERM\n");
     }
     /* Wait for the NETAPI Proxy task to finish its processing and exit. */
     pthread_join (snoop_run_th, NULL);
-
+#endif
     return 0;
 }
 
@@ -547,121 +680,93 @@ 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);
-        globalCfg.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)
     {
         fread((void*)&temp, sizeof(uint32_t), 1, pDts);
-        globalCfg.flowId = (int)swap32(temp);
+        globalDB.flowId = (int)swap32(temp);
         fclose(pDts);
     }
     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));
 
-    pktio_cfg.qnum = globalCfg.qNum;
+    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;
 
-    globalCfg.pktio_channel = netapi_pktioCreate(netapi_handle,
+    globalDB.pktio_channel = netapi_pktioCreate(netapi_handle,
                                                &name[0],
                                                (PKTIO_CB)recv_cb,
                                                &pktio_cfg,
                                                &error);
-    if (!globalCfg.pktio_channel)
+    if (!globalDB.pktio_channel)
     {
         ipsecmgr_syslog_msg (SYSLOG_LEVEL_ERROR,
             "create_pktio_channel: failed\n");
         return -1;
     }
 
+
+    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);
+
     return 0;
 }
 /**
@@ -682,19 +787,39 @@ 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();
 
-    /* create netapi */
-    netapi_handle = netapi_init(NETAPI_SYS_MASTER, &our_netapi_default_cfg);
+    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,
+                                &hplibDeviceGblCfgParam,
+                                &nwalDeviceGblCfgParam,
+                                &netapiDeviceGblCfgParam);
     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
-        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())
     {
@@ -702,25 +827,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)
@@ -731,5 +850,7 @@ int32_t main (int argc, char* argv[])
         return -1;
     }
     else
-        printf("main: init_ipsecmgr passed\n");
+        ipsecmgr_syslog_msg (SYSLOG_LEVEL_INFO,
+                             "main: ipsecmgr daemon shutdonw complete\n");
+
 }