equilibrium2-classify_ctrl.c

Go to the documentation of this file.
00001 /*
00002  * $Id: equilibrium2-classify_ctrl.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 
00020 #include <sync/equilibrium2.h>
00021 #include <sync/equilibrium2_svc.h>
00022 #include "equilibrium2-classify.h"
00023 
00024 #include <jnx/ipc_types.h>
00025 #include <jnx/junos_kcom_mpsdk_cfg.h>
00026 #include <jnx/mpsdk.h>
00027 #include <jnx/msp_objcache.h>
00028 
00038 static void
00039 proc_svc_set_blob (void *blob, junos_kcom_gencfg_opcode_t op)
00040 {
00041     blob_svc_set_t *blob_ss = (blob_svc_set_t *)blob;
00042     blob_svc_set_t *policy = NULL;
00043     blob_rule_t *blob_rule;
00044     blob_term_t *blob_term;
00045     int rule, term;
00046     msp_policy_db_params_t policy_db_params;
00047     sp_svc_set_t *ss;
00048 
00049     NTOHS(blob_ss->ss_id);
00050     NTOHL(blob_ss->ss_svc_id);
00051     NTOHL(blob_ss->ss_gen_num);
00052     NTOHS(blob_ss->ss_size);
00053     NTOHS(blob_ss->ss_rule_count);
00054 
00055     msp_log(LOG_INFO, "%s: %s (%d) with %d rules.", __func__,
00056             blob_ss->ss_name, blob_ss->ss_id, blob_ss->ss_rule_count);
00057 
00058     blob_rule = blob_ss->ss_rule;
00059     for (rule = 0; rule < blob_ss->ss_rule_count; rule++) {
00060         NTOHS(blob_rule->rule_term_count);
00061 
00062         blob_term = blob_rule->rule_term;
00063         for (term = 0; term < blob_rule->rule_term_count; term++) {
00064             NTOHS(blob_term->term_match_port);
00065             blob_term++;
00066         }
00067         blob_rule = (blob_rule_t *)blob_term;
00068     }
00069 
00070     /* Setup policy-db parameters. */
00071     bzero(&policy_db_params, sizeof(msp_policy_db_params_t));
00072     policy_db_params.handle = ctrl_ctx->policy_db_handle;
00073     policy_db_params.svc_set_id = blob_ss->ss_id;
00074     policy_db_params.svc_id = blob_ss->ss_svc_id;
00075     policy_db_params.plugin_id = classify_pid;
00076     strlcpy(policy_db_params.plugin_name, EQ2_CLASSIFY_SVC_NAME,
00077             sizeof(policy_db_params.plugin_name));
00078 
00079     switch (op) {
00080     case JUNOS_KCOM_GENCFG_OPCODE_BLOB_ADD:
00081         msp_log(LOG_INFO, "%s: Add policy.", __func__);
00082 
00083         msp_spinlock_lock(&svc_set_lock);
00084 
00085         /* Check active service-set. */
00086         ss = get_svc_set(blob_ss->ss_id, true);
00087         if (ss) {
00088             msp_log(LOG_ERR, "%s: Check active service-set ERROR!",
00089                     __func__);
00090             goto done;
00091         }
00092 
00093         /* Allocate memory from policy-db shared memory. */
00094         policy = msp_shm_alloc(ctrl_ctx->policy_shm_handle, blob_ss->ss_size);
00095         if (policy == NULL) {
00096             msp_log(LOG_ERR, "%s: Allocate memory ERROR!", __func__);
00097             goto done;
00098         }
00099 
00100         /* Copy blob to policy. */
00101         bcopy(blob_ss, policy, blob_ss->ss_size);
00102 
00103         /* Add service-set to the list and mark it active. */
00104         ss = calloc(1, sizeof(*ss));
00105         INSIST_ERR(ss != NULL);
00106         ss->ss_policy = policy;
00107         ss->ss_active = true;
00108         LIST_INSERT_HEAD(&svc_set_head, ss, entry);
00109 
00110         /* Check inactive service-set. */
00111         if (get_svc_set(blob_ss->ss_id, false)) {
00112 
00113             /* Inactive service-set exists, there are sessions using this
00114              * old policy in the middle, the old policy was not detached from
00115              * service-set yet, so don't add new policy to policy-db.
00116              */
00117         } else {
00118 
00119             /* No inactive service-set, the old policy must have been detached
00120              * and deleted, add new policy to policy-db.
00121              */
00122             policy_db_params.policy_op = MSP_POLICY_DB_POLICY_ADD;
00123             policy_db_params.op.add_params.gen_num = blob_ss->ss_gen_num;
00124             policy_db_params.op.add_params.policy = policy;
00125             if (msp_policy_db_op(&policy_db_params) != MSP_OK) {
00126                 msp_log(LOG_ERR, "%s: Policy operation %d ERROR!",
00127                         __func__, policy_db_params.policy_op);
00128                 del_svc_set(ss);
00129             }
00130         }
00131         break;
00132     case JUNOS_KCOM_GENCFG_OPCODE_BLOB_DEL:
00133         msp_log(LOG_INFO, "%s: Delete policy.", __func__);
00134 
00135         msp_spinlock_lock(&svc_set_lock);
00136 
00137         /* Check inactive service-set. */
00138         if (get_svc_set(blob_ss->ss_id, false)) {
00139             msp_log(LOG_ERR, "%s: Inactive sevice-set exists!", __func__);
00140             goto done;
00141         }
00142 
00143         /* Check active service-set. */
00144         ss = get_svc_set(blob_ss->ss_id, true);
00145         if (ss == NULL) {
00146             msp_log(LOG_ERR, "%s: No active sevice-set to delete!",
00147                     __func__);
00148             goto done;
00149         }
00150 
00151         /* Check session counter. */
00152         if (ss->ss_ssn_count) {
00153 
00154             /* There are sesions using this service-set policy, to not break
00155              * thoes sessions, don't delete this service-set, just mark it
00156              * inactive. Don't detach it from policy-db either, so data handler
00157              * can keep receiving the rest packet of thoes sessions.
00158              * Any new session won't be accepted till all existing sessions
00159              * are closed and new policy is attached.
00160              */
00161             ss->ss_active = FALSE;
00162         } else {
00163 
00164             /* There is no session using this service-set policy,
00165              * detach policy from policy-db, free policy memory and
00166              * delete service-set.
00167              */
00168             policy_db_params.policy_op = MSP_POLICY_DB_POLICY_DEL;
00169             policy_db_params.op.del_params.gen_num = blob_ss->ss_gen_num;
00170             if (msp_policy_db_op(&policy_db_params) != MSP_OK) {
00171                 msp_log(LOG_ERR, "%s: Policy operation %d ERROR!",
00172                         __func__, policy_db_params.policy_op);
00173             }
00174             del_svc_set(ss);
00175         }
00176         break;
00177     default:
00178         msp_log(LOG_INFO, "%s: Ignore operation %d.", __func__, op);
00179     }
00180 done:
00181     msp_spinlock_unlock(&svc_set_lock);
00182     return;
00183 }
00184 
00191 static void
00192 read_gencfg_blob (junos_kcom_gencfg_t *kcom_gencfg)
00193 {
00194     config_blob_key_t *key;
00195 
00196     key = kcom_gencfg->key.data_p;
00197     msp_log(LOG_INFO, "%s: %s, tag: %d, key len %d, blob len %d",
00198             __func__, key->key_name, key->key_tag, kcom_gencfg->key.size,
00199             kcom_gencfg->blob.size);
00200 
00201     switch (key->key_tag) {
00202     case CONFIG_BLOB_SVC_SET:
00203         proc_svc_set_blob(kcom_gencfg->blob.data_p, kcom_gencfg->opcode);
00204         break;
00205     default:
00206         msp_log(LOG_INFO, "%s: Ignore blob.", __func__);
00207     }
00208     JUNOS_KCOM_MPSDK_CFG_FREE(kcom_gencfg);
00209 }
00210 
00225 sp_svc_set_t *
00226 get_svc_set (uint16_t id, bool active)
00227 {
00228     sp_svc_set_t *ss;
00229 
00230     LIST_FOREACH(ss, &svc_set_head, entry) {
00231         if ((ss->ss_policy->ss_id == id) && (ss->ss_active = active)){
00232             break;
00233         }
00234     }
00235     return ss;
00236 }
00237 
00244 void
00245 del_svc_set (sp_svc_set_t *ss)
00246 {
00247     LIST_REMOVE(ss, entry);
00248     msp_shm_free(ctrl_ctx->policy_shm_handle, ss->ss_policy);
00249     free(ss);
00250 }
00251 
00264 int
00265 equilibrium2_classify_ctrl_hdlr (msvcs_control_context_t *ctx,
00266         msvcs_control_event_t ev)
00267 {
00268 
00269     /* Save control context. */
00270     ctrl_ctx = ctx;
00271 
00272     switch (ev) {
00273     case MSVCS_CONTROL_EV_INIT:
00274         msp_log(LOG_INFO, "%s: MSVCS_CONTROL_EV_INIT", __func__);
00275         LIST_INIT(&svc_set_head);
00276         msp_spinlock_init(&svc_set_lock);
00277         break;
00278 
00279     case MSVCS_CONTROL_EV_CFG_BLOB:
00280         msp_log(LOG_INFO, "%s: MSVCS_CONTROL_EV_CFG_BLOB", __func__);
00281         read_gencfg_blob(ctx->plugin_data);
00282         break;
00283 
00284     default:
00285         msp_log(LOG_ERR, "%s: Unknown event!", __func__);
00286     }
00287     return 0;
00288 }
00289 

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:08 2010 for SDK Your Net Corporation Equilibrium II: equilibrium2-classify 1.0 by Doxygen 1.5.1