]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - keystone-rtos/netapi.git/blob - ti/runtime/netapi/applications/ipsec_offload/ipsecmgr/src/netapi_ipsecmgr.c
This commit adds the following:
[keystone-rtos/netapi.git] / ti / runtime / netapi / applications / ipsec_offload / ipsecmgr / src / netapi_ipsecmgr.c
1 /*
2  * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com/
3  *
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions
7  *  are met:
8  *
9  *    Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  *    Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the
15  *    distribution.
16  *
17  *    Neither the name of Texas Instruments Incorporated nor the names of
18  *    its contributors may be used to endorse or promote products derived
19  *    from this software without specific prior written permission.
20  *
21  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33 */
36 /* IPSec Manager Include Files */
37 #include "ipsecmgr_snoop.h"
38 #include "ipsecmgr_ipc.h"
39 #include "ipsecmgr_syslog.h"
41 /* Local include */
42 #include "netapilib_interface.h"
44 /* Standard includes */
45 #include <dlfcn.h>
46 #include <stdlib.h>
47 #include <signal.h>
48 #include <stdarg.h>
52 #include <arpa/inet.h>
53 #include <sys/socket.h>
54 #include <ifaddrs.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <unistd.h>
59 #include <sys/types.h>
60 #include <netinet/in.h> 
61 #include <string.h> 
63 /**********************************************************************
64  ************************** Local Definitions *************************
65  **********************************************************************/
66 char*  DTS_LOG_FILE_QUEUE_ETHx[] = {
67  "/proc/device-tree/soc/pktdma@2004000/channels/netrx0/complete-queue"};
68 char*  DTS_LOG_FILE_FLOW_ETHx[] = {
69 "/proc/device-tree/soc/pktdma@2004000/channels/netrx0/flow"};
72 /**********************************************************************
73  ************************** Global Variables **************************
74  **********************************************************************/
76 static ipsecmgr_ipc_daemon_send_if_t *send_iface;
77 NETAPI_T netapi_handle;
79 ipsecMgrMcb_t globalDB;
81 /* Lock file for the daemon */
82 #define LOCK_FILE   "/var/lock/ipsecmgr_daemon"
84 /* snoop task */
85 static pthread_t    snoop_run_th;
87 static NETAPI_CFG_T our_netapi_default_cfg=
88 {
89     TUNE_NETAPI_PERM_MEM_SZ,
90     128,  //start of packet offset for hw to place data on rx for default flow
91     TUNE_NETAPI_QM_CONFIG_MAX_DESC_NUM, //max number of descriptors in system
92     TUNE_NETAPI_NUM_GLOBAL_DESC,        //total we will use
93     TUNE_NETAPI_DEFAULT_NUM_BUFFERS,   //#descriptors+buffers in default heap
94     64, //#descriptors w/o buffers in default heap
95     TUNE_NETAPI_DEFAULT_BUFFER_SIZE+128+128,  //size of buffers in default heap
96     128,    //tail room
97     256,    //extra room
98     0
99 };
103 static int QUIT = 0;
105 /* stub functions */
106 static void recv_cb(struct PKTIO_HANDLE_Tag * channel, Ti_Pkt* p_recv[],
107                          PKTIO_METADATA_T meta[], int n_pkts,
108                          uint64_t ts )
110     return;
113 /* byte swap routine */
114 static unsigned int swap32 (unsigned int x)
116     unsigned int y;
118     y = (((x >> 24) & 0xff) <<  0) |
119         (((x >> 16) & 0xff) <<  8) |
120         (((x >>  8) & 0xff) << 16) |
121         (((x >>  0) & 0xff) << 24) ;
123     return (y);
127 void cleanup_sa_sp()
129     int slot, error=0;;
130     /* delete any offloaded rx SA's and policies */
131     /* and delete any offloaded tx SA's */
132     for (slot = 0;slot < 64;slot++)
133     {
134         if(globalDB.rx_sa[slot].in_use)
135         {
136             globalDB.rx_sa[slot].in_use = 0;
137             if(globalDB.rx_sa[slot].spAppId)
138             {
139                 netapi_secDelRxPolicy(netapi_handle,
140                               (NETCP_CFG_IPSEC_POLICY_T) globalDB.rx_sa[slot].spAppId,
141                               &error);
142                 ipsecmgr_syslog_msg (SYSLOG_LEVEL_INFO,
143                 "cleanup_sa_sp: SP deleted: sp_app_id: 0x%x, slot: %d, error: %d\n", 
144                 globalDB.rx_sa[slot].spAppId, slot, error);
145             }
146             netapi_secDelSA(netapi_handle,
147                         NETCP_CFG_NO_INTERFACE,
148                         (NETCP_CFG_SA_T) globalDB.rx_sa[slot].saAppId,
149                         &error);
150             ipsecmgr_syslog_msg (SYSLOG_LEVEL_INFO,
151                 "cleanup_sa_sp: SA deleted: sa_app_id: 0x%x, slot: %d, error: %d\n", 
152                 globalDB.rx_sa[slot].saAppId, slot, error);
153             
154         }
155         if(globalDB.tx_sa[slot].in_use)
156         {
157             globalDB.tx_sa[slot].in_use = 0;
158             netapi_secDelSA(netapi_handle,
159                             NETCP_CFG_NO_INTERFACE,
160                            (NETCP_CFG_SA_T) globalDB.tx_sa[slot].saAppId,
161                             &error);
162             ipsecmgr_syslog_msg (SYSLOG_LEVEL_INFO,
163                 "cleanup_sa_sp: SA deleted: sa_app_id: 0x%x, slot: %d, error: %d\n", 
164                 globalDB.tx_sa[slot].saAppId, slot, error);
165         }
166     }
170 static void* snoop_run_thread (void* arg)
172     cpu_set_t cpu_set;
173     CPU_ZERO( &cpu_set);
174     CPU_SET( 0, &cpu_set);
176     hplib_utilSetupThread(0, &cpu_set, hplib_spinLock_Type_LOL);
177     ipsecmgr_syslog_msg(SYSLOG_LEVEL_INFO,
178         "snoop_run_thread: daemon entering forever event loop\n");
180     while (1)
181     {
182         /* Poll for message from user application */
183         ipsecmgr_ipc_poll();
185         /* Poll for message from Kernel */
186         ipsecmgr_snoop_run();
187         if (QUIT == 1)
188             break;
189     }
190     ipsecmgr_syslog_msg(SYSLOG_LEVEL_INFO,
191         "snoop_run_thread: calling shutdown\n");
192     ipsecmgr_snoop_shutdown ();
193     cleanup_sa_sp();
194     netapi_shutdown(netapi_handle);
196     return;
199 /**
200  *  @b Description
201  *  @n  
202  *      SIGTERM handler.
203  *
204  *  @param[in]  signum
205  *      signal number to terminate deamon.
206  */
207 static void sig_term_handler(int signum)
209     QUIT = 1;
212 /**
213  *  @b Description
214  *  @n  
215  *      Function to implement task sleep functionality 
216  *      for IPSecMgr
217  *
218  *  @param[in]  time_in_msec
219  *      Time in milliseconds to sleep
220  *
221  *  @retval
222  *      Not Applicable.
223  */
224 static void task_sleep(uint32_t time_in_msec)
226     pthread_mutex_t fake_mutex = PTHREAD_MUTEX_INITIALIZER;
227     pthread_cond_t fake_cond = PTHREAD_COND_INITIALIZER;
228     struct timespec ts;
229     int rt;
230     unsigned int sec, nsec;
232     sec = time_in_msec/1000;
233     nsec = (time_in_msec - (sec*1000)) * 1000000;
235     /* Use the wall-clock time */
236     clock_gettime(CLOCK_REALTIME, &ts);
238     ts.tv_sec += sec;
239     ts.tv_nsec += nsec;
241     pthread_mutex_lock(&fake_mutex);
242     rt = pthread_cond_timedwait(&fake_cond, &fake_mutex, &ts);
243     pthread_mutex_unlock(&fake_mutex);
247 /**
248  *  @b Description
249  *  @n  
250  *      NETAPI Proxy's IPSecMgr Start Offload Response 
251  *      message handler.
252  *
253  *      This function is called by the IPSecMgr library
254  *      when it has a response ready corresponding to an
255  *      Start Offload SP request issued by the user application.
256  *
257  *  @param[in]  rsp
258  *      IPSecMgr's Start Offload SP response
259  *  @param[in]  addr
260  *      Destination address (user application) to send 
261  *      the response to
262  *  @param[in]  addr_size
263  *      Size of destination address passed
264  *
265  *  @retval
266  *      Success -   0
267  *  @retval
268  *      ERROR   -   <0
269  */
270 static int offload_sp_rsp_send
272     ipsecmgr_snoop_offload_sp_rsp_param_t *rsp,
273     void                                  *addr,
274     uint32_t                              addr_size
277     ipsecmgr_ipc_offload_sp_rsp_param_t offload_sp_rsp;
279     offload_sp_rsp.type = rsp->type;
280     offload_sp_rsp.result = rsp->result;
281     offload_sp_rsp.trans_id = rsp->trans_id;
282     offload_sp_rsp.err_code = rsp->err_code;
283     offload_sp_rsp.sp_handle = rsp->sp_handle;
284     offload_sp_rsp.sa_handle = rsp->sa_handle;
286     if (addr_size != sizeof(ipsecmgr_ipc_addr_t)) {
287         ipsecmgr_syslog_msg(SYSLOG_LEVEL_ERROR,
288             "offload_sp_rsp_send: addr size not correct\n");
289         return -1;
290     }
292     return send_iface->offload_sp_rsp(&offload_sp_rsp,
293                                       (ipsecmgr_ipc_addr_t *)addr);
295 static void offload_sp_req_recv
297     ipsecmgr_ipc_offload_sp_req_param_t *req,
298     ipsecmgr_ipc_addr_t                 *src_addr
301     ipsecmgr_syslog_msg (SYSLOG_LEVEL_INFO,"offload_sp_req_recv called for policy id\n",
302     req->policy_id);
303     ipsecmgr_snoop_offload_sp_rsp_param_t rsp;
304     uint32_t addr_size = sizeof(ipsecmgr_ipc_addr_t);
305     ipsecmgr_snoop_offload_sp_req_param_t offload_sp_req;
307     offload_sp_req.trans_id = req->trans_id;
308     offload_sp_req.policy_id = req->policy_id;
309     offload_sp_req.sa_flags = req->sa_flags;
310     offload_sp_req.if_name = req->if_name;
311     offload_sp_req.dscp_cfg = req->dscp_cfg;
312     offload_sp_req.l5_selector = req->l5_selector;
314     memset(&rsp, 0, sizeof(rsp));
315     rsp.trans_id = req->trans_id;
317     if (ipsecmgr_snoop_offload_sp_req(&offload_sp_req, (void*)src_addr,
318                                       addr_size)) {
319         ipsecmgr_syslog_msg (SYSLOG_LEVEL_ERROR,
320             "offload_sp_req_recv: snoop_offload_sp_req failed\n");
321         rsp.result = RESULT_FAILURE;
322         rsp.type = RSP_TYPE_ACK | RSP_TYPE_DONE;
323     }
324     else
325     {
326         rsp.result = RESULT_SUCCESS;
327         rsp.type = RSP_TYPE_ACK;
328     }
329     
330     if (offload_sp_rsp_send(&rsp, (void *)src_addr, addr_size))
331     {
332         ipsecmgr_syslog_msg (SYSLOG_LEVEL_ERROR,
333             "offload_sp_req_recv: failed to send ACK for offload_sp\n");
334     }
335     return;
339 /**
340  *  @b Description
341  *  @n  
342  *      NETAPI Proxy's IPSecMgr Stop Offload response message 
343  *      handler.
344  *
345  *      This function is called by the IPSecMgr library
346  *      when it has a response ready corresponding to an
347  *      Stop Offload SP request issued by the user application.
348  *
349  *  @param[in]  rsp
350  *      IPSecMgr's Stop Offload SP response
351  *  @param[in]  addr
352  *      Destination address (user application) to send 
353  *      the response to
354  *  @param[in]  addr_size
355  *      Size of destination address passed
356  *
357  *  @retval
358  *      Success -   0
359  *  @retval
360  *      ERROR   -   <0
361  */
362 static int stop_offload_rsp_send
364     ipsecmgr_snoop_stop_offload_rsp_param_t *rsp,
365     void                                    *addr,
366     uint32_t                                addr_size
369     ipsecmgr_ipc_stop_offload_rsp_param_t stop_offload_rsp;
371     stop_offload_rsp.type = rsp->type;
372     stop_offload_rsp.result = rsp->result;
373     stop_offload_rsp.trans_id = rsp->trans_id;
375     if (addr_size != sizeof(ipsecmgr_ipc_addr_t)) {
376         ipsecmgr_syslog_msg(SYSLOG_LEVEL_ERROR,
377             "stop_offload_rsp_send: addr size not correct\n");
378         return -1;
379     }
381     return send_iface->stop_offload_rsp(&stop_offload_rsp,
382                                       (ipsecmgr_ipc_addr_t *)addr);
385 static void stop_offload_req_recv
387     ipsecmgr_ipc_stop_offload_req_param_t   *req,
388     ipsecmgr_ipc_addr_t                     *src_addr
391     ipsecmgr_snoop_stop_offload_req_param_t stop_offload_req;
392     uint32_t addr_size = sizeof(ipsecmgr_ipc_addr_t);
393     ipsecmgr_snoop_stop_offload_rsp_param_t rsp;
395     stop_offload_req.trans_id = req->trans_id;
396     stop_offload_req.policy_id = req->policy_id;
398     memset(&rsp, 0, sizeof(rsp));
399     rsp.trans_id = req->trans_id;
401     if (ipsecmgr_snoop_stop_offload(&stop_offload_req, (void*)src_addr,
402                                       addr_size)) {
403         ipsecmgr_syslog_msg (SYSLOG_LEVEL_ERROR,
404             "stop_offload_req_recv: snoop_stop_offload failed\n");
405         rsp.result = RESULT_FAILURE;
406         rsp.type = RSP_TYPE_ACK | RSP_TYPE_DONE;
407     }
408     else
409     {
410         rsp.result = RESULT_SUCCESS;
411         rsp.type = RSP_TYPE_ACK;
412     }
414     if (stop_offload_rsp_send(&rsp, (void *)src_addr, addr_size))
415     {
416         ipsecmgr_syslog_msg (SYSLOG_LEVEL_ERROR,
417             "stop_offload_req_recv: failed to send ACK for stop_offload\n");
418     }
419     return;
422 /**
423  *  @b Description
424  *  @n  
425  *      Function to initialize IPSec Manager library.
426  *
427  *  @retval
428  *      Success -   0
429  *  @retval
430  *      ERROR   -   >0
431  */
432 static int32_t init_ipsecmgr (void)
434     struct ipsecmgr_snoop_fp_cfg_cb     fp_cfg_cb;
435     struct ipsecmgr_snoop_mgnt_cb       mgnt_cb;
436     struct ipsecmgr_snoop_platform_cb   plat_cb;
437     ipsecmgr_ipc_daemon_recv_if_t       recv_if;
438     ipsecmgr_ipc_cfg_t                  ipc_cfg;
439     int32_t                             status;
440     pthread_attr_t threadAttr;
442     /* Initializations */
443     ipsecmgr_syslog_msg (SYSLOG_LEVEL_INFO, 
444                         "DEBUG: init_ipsecmgr() starting initialization\n");
445     memset(&fp_cfg_cb, 0, sizeof(fp_cfg_cb));
446     memset(&mgnt_cb, 0, sizeof(mgnt_cb));
447     memset(&plat_cb, 0, sizeof(plat_cb));
448     memset(&recv_if, 0, sizeof(recv_if));
449     memset(&ipc_cfg, 0, sizeof(ipc_cfg));
451     /* Initialize IPC library */
452     ipc_cfg.mode = IPC_MODE_SNOOP_DAEMON;
453     if (ipsecmgr_ipc_init(&ipc_cfg)) {
454         ipsecmgr_syslog_msg (SYSLOG_LEVEL_ERROR,
455             "init_ipsecmgr: ipc_init failed\n");
456         return -1;
457     }
458     else
459         ipsecmgr_syslog_msg (SYSLOG_LEVEL_ERROR,
460             "init_ipsecmgr: ipc_init sucess\n");
462     if (ipsecmgr_ipc_get_daemon_send_iface(&send_iface)) {
463         ipsecmgr_syslog_msg (SYSLOG_LEVEL_ERROR,
464             "init_ipsecmgr: ipc_get_daemon_send_iface failed\n");
465         return -1;
466     }
467     recv_if.offload_sp_req = offload_sp_req_recv;
468     recv_if.stop_offload_req = stop_offload_req_recv;
469     /* Register ipsecmgr daemon recieve i/f */
470     if (ipsecmgr_ipc_register_daemon_recv_iface(&recv_if)) {
471         ipsecmgr_syslog_msg (SYSLOG_LEVEL_ERROR,
472             "snoop_run: ipc_register_daemon_recv_iface failed\n");
473         return -1;
474     }
476     /* Initialize the IPSec Manager Snoop library */
477     fp_cfg_cb.add_sa        =   netapilib_ifAddSA;
478     fp_cfg_cb.add_sp        =   netapilib_ifAddSP;
479     fp_cfg_cb.del_sa        =   netapilib_ifDeleteSA;
480     fp_cfg_cb.del_sp        =   netapilib_ifDeleteSP;
481     fp_cfg_cb.get_sa_ctx    =   netapilib_ifGetSACtx;
483     plat_cb.log_msg  = (ipsecmgr_snoop_log_msg_t)ipsecmgr_syslog_msg;
484     plat_cb.sleep           =   task_sleep;
486     mgnt_cb.offload_sp_rsp  =   offload_sp_rsp_send;
487     mgnt_cb.stop_offload_rsp=   stop_offload_rsp_send;
488     mgnt_cb.rekey_event     =   NULL; // No explicit notifications needed on Rekey completion
490     if ((status = ipsecmgr_snoop_init (&fp_cfg_cb, &mgnt_cb, &plat_cb))) 
491     {
492         ipsecmgr_syslog_msg (SYSLOG_LEVEL_INFO,
493                             "DEBUG: init_ipsecmgr() ipsecmgr_snoop_init failed (%d)\n", status);
494         return -1;
495     }
499 #ifdef GDB_DEBUG
500     snoop_run_thread(NULL);
501 #else
502     /* Create the task context for snoop library */
503     pthread_attr_init(&threadAttr);
504     pthread_attr_setstacksize(&threadAttr, 0x10000);
505     if (pthread_create(&snoop_run_th, (void*) NULL, snoop_run_thread, NULL))
506     {
507         ipsecmgr_syslog_msg (SYSLOG_LEVEL_ERROR,
508             "ERROR: snoop run thread failed to start, error code\n"); 
509         return -1;
510     }
512     /* Setup signal handler for SIGTERM */
513     if (signal(SIGTERM, sig_term_handler) == SIG_ERR) {
514         ipsecmgr_syslog_msg(SYSLOG_LEVEL_WARN,
515             "init_ipsecmgr: cannot handle SIGTERM\n");
516     }
517     /* Wait for the NETAPI Proxy task to finish its processing and exit. */
518     pthread_join (snoop_run_th, NULL);
519 #endif
520     return 0;
523 /**
524  *  @b Description
525  *  @n  
526  *      Utility function to daemonize the current
527  *      application.
528  *
529  *  @param[in]  lock_file
530  *      Lock file to be used by the daemon
531  */
532 static void daemonize (const char *lock_file)
534     pid_t       pid, sid;
535     int32_t     lock_fp = -1;
536     char        str[10];
538     /* already a daemon */
539     if (getppid () == 1)
540         return;
542     /* Fork off the parent process */
543     if ((pid = fork ()) < 0) 
544     {
545         ipsecmgr_syslog_msg(SYSLOG_LEVEL_ERROR, 
546                             "ERROR: daemonize() unable to fork daemon, code=%d (%s)",
547                             errno, 
548                             strerror(errno));
549         exit (-1);
550     }
552     /* If we got a PID, then exit the parent process. */
553     if (pid > 0)
554         exit (0);
556     /* At this point we are executing as the child process */
558     /* Create a new SID for the child process */
559     if ((sid = setsid ()) < 0) 
560     {
561         ipsecmgr_syslog_msg(SYSLOG_LEVEL_ERROR, 
562                             "ERROR: daemonize() unable to create a new session, code %d (%s)",
563                             errno, 
564                             strerror(errno));
565         exit (-1);
566     }
568     /* Change the file mode mask */
569     umask (027);
571     /* Change the current working directory.  This prevents the current
572      * directory from being locked; hence not being able to remove it. */
573     if ((chdir("/")) < 0) 
574     {
575         ipsecmgr_syslog_msg(SYSLOG_LEVEL_ERROR, 
576                             "ERROR: daemonize() unable to change directory to %s, code %d (%s)",
577                             "/", 
578                             errno, 
579                             strerror(errno));
580         exit (-1);
581     }
583     /* Create the lock file */
584     if (lock_file) 
585     {
586         if ((lock_fp = open(lock_file, O_RDWR|O_CREAT, 0640)) < 0 ) 
587         {
588             ipsecmgr_syslog_msg (SYSLOG_LEVEL_ERROR, 
589                                 "ERROR: daemonize() unable to create lock file %s, code=%d (%s)",
590                                 lock_file, 
591                                 errno, 
592                                 strerror(errno));
593             exit (-1);
594         }
595     }
597     if (lockf (lock_fp, F_TLOCK, 0) < 0) 
598         exit(-1); /* can not lock */
600     /* Record pid to lockfile */
601     sprintf (str, "%d\n", getpid());
602     write (lock_fp, str, strlen(str)); 
604     /* Cancel certain signals */
605     signal (SIGCHLD, SIG_DFL); /* A child process dies */
606     signal (SIGTSTP, SIG_IGN); /* Various TTY signals */
607     signal (SIGTTOU, SIG_IGN);
608     signal (SIGTTIN, SIG_IGN);
609     signal (SIGHUP, SIG_IGN); /* Ignore hangup signal */
610     signal (SIGTERM, sig_term_handler); /* catch SIGTERM */
612     /* Redirect standard files to /dev/null */
613     freopen( "/dev/null", "r", stdin);
614     freopen( "/dev/null", "w", stdout);
615     freopen( "/dev/null", "w", stderr);
617     /* Done setting up the daemon */
618     return;
621 int get_kernel_config()
623     uint32_t temp=0;
624     FILE *pDts = NULL;
626     pDts = fopen(DTS_LOG_FILE_QUEUE_ETHx[0], "rb");
628     if(pDts)
629     {
630         fread((void*)&temp, sizeof(uint32_t), 1, pDts);
631         globalDB.qNum = (int)swap32(temp);
632         fclose(pDts);
633     }
634     else
635     {
636         ipsecmgr_syslog_msg (SYSLOG_LEVEL_ERROR,
637             "main: error opening device tree file: %s\n",DTS_LOG_FILE_QUEUE_ETHx[0]);
638         return -1;
639     }
641     pDts = NULL;
642     pDts = fopen(DTS_LOG_FILE_FLOW_ETHx[0], "rb");
644     if(pDts)
645     {
646         fread((void*)&temp, sizeof(uint32_t), 1, pDts);
647         globalDB.flowId = (int)swap32(temp);
648         fclose(pDts);
649     }
650     else
651     {
652         ipsecmgr_syslog_msg (SYSLOG_LEVEL_ERROR,
653             "main: error opening device tree file: %s\n",DTS_LOG_FILE_FLOW_ETHx[0]);
654         return -1;
655     }
656     ipsecmgr_syslog_msg (SYSLOG_LEVEL_INFO,
657             "get_kernel_config: flow: 0x%x, qNum: 0x%x\n", 
658              globalDB.flowId, globalDB.qNum);
659     return 0;
662 int create_pktio_channel()
664     static int count = 0;
665     int error = 0;
666     char name[19];
667     PKTIO_HANDLE_T *pktio_channel;
668     PKTIO_CFG_T pktio_cfg;
669     NETCP_CFG_ROUTE_T route;
670     NETCP_CFG_FLOW_T flow;
671     NETCP_CFG_EXCEPTION_PKT_T expPkt_appid = 0;
673     memset(&pktio_cfg,0,sizeof(PKTIO_CFG_T));
674     memset((void *)&route, 0, sizeof (NETCP_CFG_ROUTE_T));
675     memset((void *)&flow, 0, sizeof (NETCP_CFG_FLOW_T));
677     sprintf(&name[0],"%s","offload_0");
678     pktio_cfg.qnum = globalDB.qNum;
679     pktio_cfg.flags1 = PKTIO_RX;
680     pktio_cfg.flags2 = PKTIO_GLOBAL | PKTIO_PKT;
681     pktio_cfg.max_n = 8;
683     globalDB.pktio_channel = netapi_pktioCreate(netapi_handle,
684                                                &name[0],
685                                                (PKTIO_CB)recv_cb,
686                                                &pktio_cfg,
687                                                &error);
688     if (!globalDB.pktio_channel)
689     {
690         ipsecmgr_syslog_msg (SYSLOG_LEVEL_ERROR,
691             "create_pktio_channel: failed\n");
692         return -1;
693     }
695     printf("create_pktio_channel: qnum: %d, pktio_handle: 0x%x\n",
696             pktio_cfg.qnum,
697             globalDB.pktio_channel);
699     flow.dma_engine= 1;
700     flow.flowid = globalDB.flowId;
701     route.p_dest_q = globalDB.pktio_channel;
702     route.p_flow = &flow;
703     route.valid_params |= NETCP_CFG_VALID_PARAM_ROUTE_TYPE;
704     route.routeType = NWAL_ROUTE_RX_INTF_W_FLOW;
705     /* enable exception packet handling for fragmented packets */
706     expPkt_appid = netapi_netcpCfgExceptions(netapi_handle,
707                                              7,
708                                              NETCP_CFG_ACTION_TO_SW,
709                                              (NETCP_CFG_ROUTE_HANDLE_T) &route);
710     printf("create_pktio_channel: expPkt appid: 0x%x\n", expPkt_appid);
712     return 0;
714 /**
715  *  @b Description
716  *  @n  
717  *      Entry point for the NETAPI Proxy application.
718  *
719  *  @param[in]  argc
720  *      Number of arguments passed to the application
721  *  @param[in]  argv
722  *      Arguments passed to the application.
723  *
724  *  @retval
725  *      Success -   0
726  *  @retval
727  *      ERROR   -   >0
728  */
729 int32_t main (int argc, char* argv[])
731     int32_t             retVal;
732     int i, iface;
733     char* pTok = NULL;
734     int ip_entry_count = 0;
735     struct ifaddrs *ifaddr, *ifa;
736     int family, s;
737     char host[NI_MAXHOST];
738     struct sockaddr_in6 ipv6_addr;
739     cpu_set_t cpu_set;
740     ipsecmgr_syslog_init();
742     memset(&globalDB, 0, sizeof(globalDB));
744     /* assign main net_test thread to run on core 0 */
745     CPU_ZERO( &cpu_set);
746     CPU_SET( 0, &cpu_set);
747     hplib_utilSetupThread(0, &cpu_set, hplib_spinLock_Type_LOL);
748     /* create netapi */
749     netapi_handle = netapi_init(NETAPI_SYS_MASTER,
750                                 &our_netapi_default_cfg);
751     if(netapi_handle == NULL)
752     {
753         ipsecmgr_syslog_msg (SYSLOG_LEVEL_ERROR,
754                              "ERROR: netapi_init failed\n");
755         return -1;
756     }
757     else
758         netapi_netcpCfgExceptions(netapi_handle,
759                                   NETCP_CFG_ALL_EXCEPTIONS,
760                                   NETCP_CFG_ACTION_DISCARD,
761                                   (NETCP_CFG_ROUTE_HANDLE_T) NULL);
763     if (get_kernel_config())
764     {
765         ipsecmgr_syslog_msg (SYSLOG_LEVEL_ERROR,
766                             "ERROR: main: get_kernel_config() failed\n");
767         return -1;
768     }
769     else
770     {
771         /* create pktio channel */
772         if(create_pktio_channel())
773         {
774             ipsecmgr_syslog_msg (SYSLOG_LEVEL_ERROR,
775                             "ERROR: main: pktio channel creation failed\n");
776             return -1;
777         }
778     }
779      /*Create the proxy daemon. */
780     printf("main: calling daemonize\n");
781     daemonize (LOCK_FILE); 
783     /* Initialize and start the IPSec Mgr Snoop functionality */
784     if ((retVal = init_ipsecmgr ()) < 0)
785     {
786         ipsecmgr_syslog_msg (SYSLOG_LEVEL_ERROR,
787                             "ERROR: IPSec Mgr init failed, error code %d \n",
788                             retVal);
789         return -1;
790     }
791     else
792         ipsecmgr_syslog_msg (SYSLOG_LEVEL_INFO,
793                              "main: ipsecmgr daemon shutdonw complete\n");