packetproc-data_main.c

Go to the documentation of this file.
00001 /*
00002  * $Id: packetproc-data_main.c 358766 2010-01-20 16:37:38Z jamesk $
00003  *
00004  * This code is provided as is by Juniper Networks SDK Developer Support.
00005  * It is provided with no warranties or guarantees, and Juniper Networks
00006  * will not provide support or maintenance of this code in any fashion.
00007  * The code is provided only to help a developer better understand how
00008  * the SDK can be used.
00009  *
00010  * Copyright (c) 2007-2008, Juniper Networks, Inc.
00011  * All rights reserved.
00012  */
00013 
00022 /* The Application and This Daemon's Documentation: */
00023 
00046 #include "packetproc-data.h"
00047 
00048 extern packet_thread_t packet_thrd[];
00049 
00050 /*** STATIC/INTERNAL Functions ***/
00051 static evTimerID  packetproc_idle_timer;
00052 
00056 static void
00057 packetproc_idle(evContext ctx UNUSED, void * uap UNUSED,
00058             struct timespec due UNUSED, struct timespec inter UNUSED)
00059 {
00060     /* Do nothing here, just keep the main process running. */
00061     return;
00062 }
00063 
00067 static void 
00068 packetproc_data_quit(int signo __unused)
00069 {
00070     int cpu;
00071 
00072     /* Send SIGUSR1 to all packet/data threads so they shutdown */
00073     for (cpu = 0; cpu < MSP_MAX_CPUS; cpu++) {
00074         if (packet_thrd[cpu].thrd_tid) {
00075             logging(LOG_INFO, "%s: killing thread %d...", __func__, cpu);
00076             pthread_kill(packet_thrd[cpu].thrd_tid, SIGUSR1);
00077         }
00078     }
00079 
00080     /* Wait for all threads closed. */
00081     for (cpu = 0; cpu < MSP_MAX_CPUS; cpu++) {
00082         if (packet_thrd[cpu].thrd_tid) {
00083 
00084             /* Wait here till the thread was killed. */
00085             pthread_join(packet_thrd[cpu].thrd_tid, NULL);
00086 
00087             packet_thrd[cpu].thrd_tid = NULL;
00088         }
00089     }
00090     logging(LOG_INFO, "%s: packetproc shutting down...", __func__);
00091 
00092     msp_exit();
00093     exit(0);
00094 }
00095 
00104 static int
00105 packetproc_data_init(evContext ctx)
00106 {
00107     int ret, cpu;
00108 
00109     /* Initialize environment for SDK data path application. */
00110     ret = msp_init();
00111     if(ret != MSP_OK) {
00112         return ret;
00113     }
00114 
00115     /* Initialize signal handlers. */
00116     signal(SIGTERM, packetproc_data_quit);
00117     signal(SIGPIPE, SIG_IGN);
00118     signal(SIGHUP, SIG_IGN);
00119 
00120     /* Set logging level and mode, default values are
00121      * LOG_INFO and LOGGING_SYSLOG respectively.
00122      */
00123     logging_set_level(LOG_INFO);
00124     logging_set_mode(LOGGING_SYSLOG);
00125 
00126     for(cpu = 0; cpu < MSP_MAX_CPUS; cpu++) {
00127         packet_thrd[cpu].thrd_tid = 0;
00128     }
00129     
00130     init_packet_loop();
00131 
00132     logging(LOG_INFO, "%s: Create packet loops OK!", __func__);
00133 
00134     /* Set idle timer to keep main process running. */
00135     packetproc_idle_timer.opaque = NULL;
00136     if(evSetTimer(ctx, packetproc_idle, NULL, evConsTime(0, 0),
00137             evConsTime(120, 0), &packetproc_idle_timer)) {
00138         logging(LOG_ERR, "%s: Initialize the idle timer ERROR!",
00139                 __func__);
00140         return MSP_ERROR;
00141     }
00142     return 0;
00143 }
00144 
00145 /*** GLOBAL/EXTERNAL Functions ***/
00146 
00159 int
00160 main(int argc, char** argv)
00161 {
00162     mp_sdk_app_ctx_t app_ctx;
00163     int rc;
00164 
00165     app_ctx = msp_create_app_ctx(argc, argv, "packetproc-data");
00166     msp_set_app_cb_init(app_ctx, packetproc_data_init);
00167 
00168     rc = msp_app_init(app_ctx);
00169 
00170     /* Should never reach this point */
00171     msp_destroy_app_ctx(app_ctx);
00172     return rc;
00173 }
00174 

2007-2009 Juniper Networks, Inc. All rights reserved. The information contained herein is confidential information of Juniper Networks, Inc., and may not be used, disclosed, distributed, modified, or copied without the prior written consent of Juniper Networks, Inc. in an express license. This information is subject to change by Juniper Networks, Inc. Juniper Networks, the Juniper Networks logo, and JUNOS are registered trademarks of Juniper Networks, Inc. in the United States and other countries. All other trademarks, service marks, registered trademarks, or registered service marks are the property of their respective owners.
Generated on Sun May 30 20:27:04 2010 for SDK Your Net Corporation Simple Packet Processing Example 1.0 by Doxygen 1.5.1