dpm-mgmt_conn.c

Go to the documentation of this file.
00001 /*
00002  * $Id: dpm-mgmt_conn.c 346460 2009-11-14 05:06:47Z ssiano $
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) 2008, Juniper Networks, Inc.
00011  * All rights reserved.
00012  */
00013 
00021 #include <sync/common.h>
00022 #include <sync/dpm_ipc.h>
00023 #include <jnx/pconn.h>
00024 #include "dpm-mgmt_config.h"
00025 #include "dpm-mgmt_conn.h"
00026 #include "dpm-mgmt_logging.h"
00027 
00028 #include DPM_OUT_H
00029 
00030 /*** Constants ***/
00031 
00032 #define DPM_MGMT_SERVER_MAX_CONN 1  
00033 
00034 /*** Data Structures ***/
00035 
00036 static evContext m_ctx;   
00037 
00038 static pconn_server_t     * mgmt_server;  
00039 static pconn_session_t    * ctrl_session; 
00040 static pconn_peer_info_t  * ctrl_info;    
00041 
00042 
00043 /*** STATIC/INTERNAL Functions ***/
00044 
00045 
00058 static void
00059 receive_connection(pconn_session_t * session,
00060                    pconn_event_t event,
00061                    void * cookie __unused)
00062 {
00063     int rc;
00064     
00065     switch (event) {
00066 
00067     case PCONN_EVENT_ESTABLISHED:
00068             
00069         if(ctrl_info == NULL) {
00070             ctrl_info = malloc(sizeof(pconn_peer_info_t));
00071             INSIST(ctrl_info != NULL);
00072         } else {
00073             LOG(TRACE_LOG_ERR, "%s: ctrl_info already present when receiving "
00074                 "incoming connection", __func__);
00075         }
00076         
00077         rc = pconn_session_get_peer_info(session, ctrl_info);
00078         
00079         if(rc != PCONN_OK) {
00080             LOG(TRACE_LOG_ERR, "%s: Cannot retrieve peer info "
00081                 "for session. Error: %d", __func__, rc);
00082         } else {
00083             INSIST_ERR(ctrl_info->ppi_peer_type == PCONN_PEER_TYPE_PIC);
00084             
00085             junos_trace(DPM_TRACEFLAG_CONNECTION,
00086                 "%s: Accepted connection from ms-%d/%d/0",
00087                 __func__, ctrl_info->ppi_fpc_slot, ctrl_info->ppi_pic_slot);
00088         }
00089         
00090         ctrl_session = session;
00091         
00092         // send it all out to the ctrl component
00093         send_configuration();
00094         
00095         break;
00096 
00097     case PCONN_EVENT_SHUTDOWN:
00098         
00099         if(session == ctrl_session) {
00100             ctrl_session = NULL;
00101             
00102             if(ctrl_info != NULL) {
00103                 free(ctrl_info);
00104                 ctrl_info = NULL;
00105             }
00106             
00107             junos_trace(DPM_TRACEFLAG_CONNECTION, "%s: Connection"
00108                 " from the ctrl component was shutdown.", __func__);
00109                 
00110         } else {
00111             LOG(TRACE_LOG_ERR, "%s: Cannot find an existing "
00112                 "peer session to shutdown", __func__);
00113         }
00114 
00115         break;
00116 
00117     case PCONN_EVENT_FAILED:
00118         
00119         if(session == ctrl_session) {
00120             ctrl_session = NULL;
00121             
00122             if(ctrl_info != NULL) {
00123                 free(ctrl_info);
00124                 ctrl_info = NULL;
00125             }
00126             
00127             junos_trace(DPM_TRACEFLAG_CONNECTION, "%s: Connection from "
00128                 "the ctrl component was shutdown (due to a failure).",
00129                 __func__);
00130         }
00131         
00132         break;
00133         
00134     default:
00135         
00136         LOG(TRACE_LOG_ERR, "%s: Received an unknown event", __func__);
00137         
00138         break;
00139     }
00140 }
00141 
00142 
00159 static status_t
00160 receive_message(pconn_session_t * session,
00161                 ipc_msg_t * msg,
00162                 void * cookie __unused)
00163 {
00164     subscriber_status_t * data;
00165     
00166     INSIST_ERR(session == ctrl_session);
00167     
00168     data = (subscriber_status_t *)msg->data;
00169     INSIST_ERR(msg->length == sizeof(subscriber_status_t)); // check msg len
00170     
00171     switch(msg->subtype) {
00172 
00173     case MSG_SUBSCRIBER_LOGIN:
00174 
00175         subscriber_login(data->name, data->class);
00176         
00177         break;
00178         
00179     case MSG_SUBSCRIBER_LOGOUT:
00180         
00181         subscriber_logout(data->name, data->class);
00182         
00183         break;
00184         
00185     default:
00186     
00187         LOG(TRACE_LOG_ERR,
00188             "%s: Received an unknown message type (%d) from the "
00189             "ctrl component", __func__, msg->subtype);
00190         break;
00191     }
00192 
00193     return SUCCESS;
00194 }
00195 
00196 
00197 /*** GLOBAL/EXTERNAL Functions ***/
00198 
00199 
00209 status_t
00210 init_conn_server(evContext ctx)
00211 {
00212     pconn_server_params_t params;
00213     
00214     m_ctx = ctx;
00215     
00216     // init the sessions
00217     ctrl_session = NULL;
00218     ctrl_info = NULL;
00219     
00220     bzero(&params, sizeof(pconn_server_params_t));
00221     
00222     // setup the server args
00223     params.pconn_port            = DPM_PORT_NUM;
00224     params.pconn_max_connections = DPM_MGMT_SERVER_MAX_CONN;
00225     params.pconn_event_handler   = receive_connection;
00226     
00227     // bind
00228     mgmt_server = pconn_server_create(&params, m_ctx, receive_message, NULL);
00229     
00230     if(mgmt_server == NULL) {
00231         LOG(TRACE_LOG_ERR, "%s: Failed to initialize the pconn server"
00232             " on port %d.", __func__, DPM_PORT_NUM);
00233         return EFAIL;
00234     }
00235     
00236     LOG(TRACE_LOG_INFO, "%s: Successfully initialized "
00237             "the pconn server on port %d.", __func__, DPM_PORT_NUM);
00238     
00239     return SUCCESS;
00240 }
00241 
00242 
00246 void
00247 shutdown_conn_server(void)
00248 {
00249     if(ctrl_session) {
00250         junos_trace(DPM_TRACEFLAG_CONNECTION,
00251             "%s: Closing the session to the ctrl component.", __func__);
00252         pconn_session_close(ctrl_session);
00253         ctrl_session = NULL;
00254         if(ctrl_info != NULL) {
00255             free(ctrl_info);
00256             ctrl_info = NULL;
00257         }
00258     }
00259     
00260     if(mgmt_server) {
00261         junos_trace(DPM_TRACEFLAG_CONNECTION,
00262             "%s: Shuting down the server on port %d.",
00263             __func__, DPM_PORT_NUM);
00264         pconn_server_shutdown(mgmt_server);
00265         mgmt_server = NULL;
00266     }
00267 }
00268 
00269 
00276 void
00277 mspic_offline(const char * name)
00278 {
00279     char current_int_prefix[64];
00280     
00281     if(ctrl_info != NULL) {
00282     
00283         sprintf(current_int_prefix,
00284             "ms-%d/%d/0", ctrl_info->ppi_fpc_slot, ctrl_info->ppi_pic_slot);
00285         
00286         if(strstr(name, current_int_prefix) != NULL) {
00287             pconn_session_close(ctrl_session);
00288             ctrl_session = NULL;
00289             free(ctrl_info);
00290             ctrl_info = NULL;
00291             
00292             junos_trace(DPM_TRACEFLAG_CONNECTION, "%s: Connection from "
00293                 "the ctrl component was shutdown (forced by %s down).",
00294                 __func__, current_int_prefix);
00295             
00296             clear_logins();
00297         }
00298     }
00299 }
00300 
00301 
00308 void
00309 notify_configuration_reset(boolean use_classic)
00310 {
00311     reset_info_t data;
00312     int rc;
00313     
00314     if(ctrl_session == NULL)
00315         return;
00316     
00317     junos_trace(DPM_TRACEFLAG_CONNECTION, "%s", __func__);
00318 
00319     data.use_classic_filters = use_classic;
00320 
00321     rc = pconn_server_send(ctrl_session, MSG_CONF_RESET, &data, sizeof(data));
00322     if(rc != PCONN_OK) {
00323          LOG(TRACE_LOG_ERR, "%s: sending msg to ctrl component failed (%s)",
00324                  __func__);
00325     }
00326 }
00327 
00328 
00332 void
00333 notify_configuration_complete(void)
00334 {
00335     int rc;
00336     
00337     if(ctrl_session == NULL)
00338         return;
00339     
00340     junos_trace(DPM_TRACEFLAG_CONNECTION, "%s", __func__);
00341 
00342     rc = pconn_server_send(ctrl_session, MSG_CONF_COMP, NULL, 0);
00343     if(rc != PCONN_OK) {
00344          LOG(TRACE_LOG_ERR, "%s: sending msg to ctrl component failed (%s)",
00345                  __func__);
00346     }
00347 }
00348 
00349 
00356 void
00357 notify_policy_add(policer_info_t * pol)
00358 {
00359     int rc;
00360     
00361     if(ctrl_session == NULL)
00362         return;
00363     
00364     junos_trace(DPM_TRACEFLAG_CONNECTION, "%s", __func__);
00365     
00366     // change to network byte order
00367     pol->if_exceeding.burst_size_limit = 
00368         htonq(pol->if_exceeding.burst_size_limit);
00369     
00370     if(pol->if_exceeding.bw_in_percent) {
00371         pol->if_exceeding.bw_u.bandwidth_percent = 
00372             htonl(pol->if_exceeding.bw_u.bandwidth_percent);
00373     } else {
00374         pol->if_exceeding.bw_u.bandwidth_limit = 
00375             htonq(pol->if_exceeding.bw_u.bandwidth_limit);
00376     }
00377 
00378     rc = pconn_server_send(ctrl_session, MSG_POLICER, pol,
00379             sizeof(policer_info_t));
00380     
00381     if(rc != PCONN_OK) {
00382          LOG(TRACE_LOG_ERR, "%s: sending msg to ctrl component failed (%s)",
00383                  __func__);
00384     }
00385     
00386     // change BACK to host byte order
00387     pol->if_exceeding.burst_size_limit = 
00388         ntohq(pol->if_exceeding.burst_size_limit);
00389     
00390     if(pol->if_exceeding.bw_in_percent) {
00391         pol->if_exceeding.bw_u.bandwidth_percent = 
00392             ntohl(pol->if_exceeding.bw_u.bandwidth_percent);
00393     } else {
00394         pol->if_exceeding.bw_u.bandwidth_limit = 
00395             ntohq(pol->if_exceeding.bw_u.bandwidth_limit);
00396     }
00397 }
00398 
00399 
00415 void
00416 notify_interface_add(char * int_name,
00417                      if_subunit_t ifl_subunit,
00418                      int_def_t * interface,
00419                      ifl_idx_t ifl_index)
00420 {
00421     int_info_t data;
00422     int rc;
00423     
00424     if(ctrl_session == NULL)
00425         return;
00426     
00427     junos_trace(DPM_TRACEFLAG_CONNECTION, "%s", __func__);
00428     
00429     bzero(&data, sizeof(int_info_t));
00430     strcpy(data.name, int_name);
00431     data.subunit = htonl((uint32_t)ifl_subunit);
00432     data.index = htonl((uint32_t)ifl_idx_t_getval(ifl_index));
00433     strcpy(data.input_pol, interface->input_pol);
00434     strcpy(data.output_pol, interface->output_pol);
00435 
00436     rc = pconn_server_send(ctrl_session, MSG_INTERFACE, &data,
00437             sizeof(int_info_t));
00438     
00439     if(rc != PCONN_OK) {
00440          LOG(TRACE_LOG_ERR, "%s: sending msg to ctrl component failed (%s)",
00441                  __func__);
00442     }
00443 }
00444 
00445 
00452 void
00453 notify_subscriber_add(sub_info_t * sub)
00454 {
00455     int rc;
00456     
00457     if(ctrl_session == NULL)
00458         return;
00459     
00460     junos_trace(DPM_TRACEFLAG_CONNECTION, "%s", __func__);
00461 
00462     rc = pconn_server_send(ctrl_session, MSG_SUBSCRIBER, sub,
00463             sizeof(sub_info_t));
00464     
00465     if(rc != PCONN_OK) {
00466          LOG(TRACE_LOG_ERR, "%s: sending msg to ctrl component failed (%s)",
00467                  __func__);
00468     }
00469 }

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:26:55 2010 for SDK Your Net Corporation Dynamic Policy Manager Example: dpm-mgmt 1.0 by Doxygen 1.5.1