equilibrium2-mgmt_kcom.c

Go to the documentation of this file.
00001 /*
00002  * $Id: equilibrium2-mgmt_kcom.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/equilibrium2.h>
00022 #include "equilibrium2-mgmt.h"
00023 
00024 #include <stdlib.h>
00025 #include <string.h>
00026 #include <jnx/aux_types.h>
00027 #include <jnx/trace.h>
00028 #include <jnx/junos_trace.h>
00029 
00030 #include EQUILIBRIUM2_OUT_H
00031 
00032 /*** Constants ***/
00033 
00034 #define MSP_IFD_NAME_PATTERN "ms-"  
00036 /*** Data Structures ***/
00037 
00038 /*** STATIC/INTERNAL Functions ***/
00039 
00054 static int
00055 ifd_async_hdlr (kcom_ifdev_t *msg, void *user_info UNUSED)
00056 {
00057     char *name = msg->ifdev_name;
00058 
00059     if ((msg->ifdev_op == KCOM_DELETE) && strstr(name, MSP_IFD_NAME_PATTERN)) {
00060 
00061         EQ2_TRACE(EQ2_TRACEFLAG_KCOM, "%s: KCOM_DELETE %s.",
00062                 __func__, name);
00063     }
00064     if (junos_kcom_ifd_down(msg) && strstr(name, MSP_IFD_NAME_PATTERN)) {
00065 
00066         EQ2_TRACE(EQ2_TRACEFLAG_KCOM, "%s: ifd %s is down",
00067                 __func__, name);
00068 
00069         /* An MS PIC is going down and may be one we're connected to */
00070     }
00071 
00072     junos_kcom_msg_free(msg);
00073     return 0;
00074 }
00075 
00076 
00090 static int
00091 ssrb_async_hdlr (junos_kcom_pub_blob_msg_t *kpb_msg, void *arg UNUSED)
00092 {
00093     junos_kcom_pub_ssrb_t *ssrb;
00094 
00095     if (kpb_msg->key.blob_id != JNX_PUB_EXT_SVC_SSR_BLOB) {
00096         EQ2_LOG(TRACE_LOG_ERR, "%s: Public blob ID %d, op %d ERROR!",
00097                 __func__, kpb_msg->key.blob_id, kpb_msg->op);
00098         free(kpb_msg);
00099         return 0;
00100     }
00101 
00102     ssrb = &kpb_msg->pub_blob.blob.ssrb;
00103     switch (kpb_msg->op) {
00104     case JUNOS_KCOM_PUB_BLOB_MSG_ADD:
00105         EQ2_TRACE(EQ2_TRACEFLAG_KCOM, "%s: SSRB <%s, %d> was added.",
00106                 __func__, ssrb->svc_set_name, ssrb->svc_set_id);
00107         config_ssrb_op(ssrb, CONFIG_SSRB_ADD);
00108         break;
00109     case JUNOS_KCOM_PUB_BLOB_MSG_DEL:
00110         EQ2_TRACE(EQ2_TRACEFLAG_KCOM, "%s: SSRB %d was deleted.",
00111                 __func__, kpb_msg->key.blob_key.pub_ssrb_key.svc_set_id);
00112         config_ssrb_op(ssrb, CONFIG_SSRB_DEL);
00113         break;
00114     case JUNOS_KCOM_PUB_BLOB_MSG_CHANGE:
00115         EQ2_TRACE(EQ2_TRACEFLAG_KCOM, "%s: SSRB <%s, %d> was changed.",
00116                 __func__, ssrb->svc_set_name, ssrb->svc_set_id);
00117         config_ssrb_op(ssrb, CONFIG_SSRB_CHANGE);
00118         break;
00119     default:
00120         EQ2_TRACE(EQ2_TRACEFLAG_KCOM, "%s: Unknown SSRB event <%s, %d>.",
00121                 __func__, ssrb->svc_set_name, ssrb->svc_set_id);
00122     }
00123 
00124     free(kpb_msg);
00125     return 0;
00126 }
00127 
00141 static int 
00142 get_initial_ssrb (junos_kcom_pub_blob_t *pub_blob, void *arg UNUSED)
00143 {
00144     if (pub_blob->blob_id != JNX_PUB_EXT_SVC_SSR_BLOB) {
00145         EQ2_LOG(LOG_ERR, "%s: Public blob, but not an SSRB <%d>",
00146             __func__, pub_blob->blob_id);
00147         free(pub_blob);
00148         return 0;
00149     }
00150     EQ2_TRACE(EQ2_TRACEFLAG_KCOM, "%s: Get initial SSRB <%s, %d>.",
00151             __func__, pub_blob->blob.ssrb.svc_set_name,
00152             pub_blob->blob.ssrb.svc_set_id);
00153     if (config_ssrb_op(&pub_blob->blob.ssrb, CONFIG_SSRB_ADD) < 0) {
00154         EQ2_LOG(LOG_ERR, "%s: Add initial SSRB <%s, %d> to ERROR!",
00155                 __func__, pub_blob->blob.ssrb.svc_set_name,
00156                 pub_blob->blob.ssrb.svc_set_id);
00157     }
00158     free(pub_blob);
00159     return 0;
00160 }
00161 
00172 static int 
00173 gencfg_async_hdlr (junos_kcom_gencfg_t *gencfg)
00174 {
00175     EQ2_TRACE(EQ2_TRACEFLAG_KCOM, "%s", __func__);
00176 
00177     gencfg->opcode = JUNOS_KCOM_GENCFG_OPCODE_GET_FREE;
00178     /*
00179      * Set get_p to NULL. Otherwise junos_kcom_gencfg() will
00180      * free the data to which get_p points to. The data will be
00181      * freed later (in kcom_msg_handler())
00182      */
00183     gencfg->get_p = NULL;
00184     junos_kcom_gencfg(gencfg);
00185     junos_kcom_msg_free(gencfg);
00186     return 0;
00187 }
00188 
00189 
00190 /*** GLOBAL/EXTERNAL Functions ***/
00191 
00205 int
00206 kcom_add_config_blob (config_blob_key_t *key, void *blob)
00207 {
00208     uint32_t blob_size;
00209     junos_kcom_gencfg_t gencfg;
00210     junos_kcom_gencfg_peer_info_t *peer_info;
00211     junos_kcom_gencfg_peer_info_t *peer;
00212     svc_if_t *svc_if;
00213     blob_svr_group_set_t *blob_gs;
00214     blob_svc_set_t *blob_ss;
00215     uint16_t cksum = 0;
00216     int err;
00217 
00218     if (svc_if_count == 0) {
00219         EQ2_TRACE(EQ2_TRACEFLAG_KCOM, "%s: No service interface configured!",
00220                 __func__);
00221         return 0;
00222     }
00223     JUNOS_KCOM_GENCFG_INIT(&gencfg);
00224     gencfg.opcode = JUNOS_KCOM_GENCFG_OPCODE_BLOB_ADD;
00225     gencfg.blob_id = eq2_provider_id;
00226     gencfg.cfg_index = key->key_plugin_id;
00227     gencfg.key.size = sizeof(config_blob_key_t);
00228     gencfg.key.data_p = key;
00229     gencfg.blob.data_p = blob;
00230     gencfg.dest = JUNOS_KCOM_GENCFG_DEST_RE_AND_PFE;
00231 
00232     if (key->key_tag == CONFIG_BLOB_SVR_GROUP) {
00233         peer_info = calloc(1, svc_if_count *
00234                 sizeof(junos_kcom_gencfg_peer_info_t));
00235         INSIST_ERR(peer_info != NULL);
00236         blob_gs = blob;
00237         cksum = blob_gs->gs_cksum;
00238         blob_size = ntohs(blob_gs->gs_size);
00239         peer = peer_info;
00240         LIST_FOREACH(svc_if, &svc_if_head, entry) {
00241             peer->peer_id.peer_name = svc_if->if_name;
00242             peer++;
00243         }
00244         gencfg.peer_info = peer_info;
00245         gencfg.peer_count = svc_if_count;
00246         EQ2_TRACE(EQ2_TRACEFLAG_KCOM, "%s: peer %d, %s",
00247                 __func__, svc_if_count, peer_info->peer_id.peer_name);
00248     } else if (key->key_tag == CONFIG_BLOB_SVC_SET) {
00249         peer_info = calloc(1, sizeof(junos_kcom_gencfg_peer_info_t));
00250         INSIST_ERR(peer_info != NULL);
00251         blob_ss = blob;
00252         cksum = blob_ss->ss_cksum;
00253         blob_size = ntohs(blob_ss->ss_size);
00254         peer_info->peer_id.peer_name = blob_ss->ss_if_name;
00255         gencfg.peer_info = peer_info;
00256         gencfg.peer_count = 1;
00257     } else {
00258         EQ2_LOG(TRACE_LOG_ERR, "%s: Unknown key tag!", __func__);
00259         return -1;
00260     }
00261     gencfg.blob.size = blob_size;
00262 
00263     EQ2_TRACE(EQ2_TRACEFLAG_KCOM, "%s: blob ID %d, size %d, key %s, "
00264             "tag %d, pid %d, peer %d, cksum %d", __func__, gencfg.blob_id,
00265             blob_size, key->key_name, key->key_tag, key->key_plugin_id,
00266             gencfg.peer_count, cksum);
00267 
00268     err = junos_kcom_gencfg(&gencfg);
00269     if (err != KCOM_OK) {
00270         EQ2_LOG(TRACE_LOG_ERR, "%s: Add blob ERROR %d!", __func__, err);
00271         free(peer_info);
00272         return -1;
00273     }
00274     free(peer_info);
00275     return 0;
00276 }
00277 
00288 int
00289 kcom_del_config_blob (config_blob_key_t *key)
00290 {
00291     junos_kcom_gencfg_t gencfg;
00292     int err;
00293 
00294     EQ2_TRACE(EQ2_TRACEFLAG_KCOM, "%s: blob %s, %d, %d.", __func__,
00295             key->key_name, key->key_tag, key->key_plugin_id);
00296 
00297     JUNOS_KCOM_GENCFG_INIT(&gencfg);
00298     gencfg.opcode = JUNOS_KCOM_GENCFG_OPCODE_BLOB_DEL;
00299     gencfg.dest = JUNOS_KCOM_GENCFG_DEST_RE_AND_PFE;
00300     gencfg.blob_id = eq2_provider_id;
00301     gencfg.key.size = sizeof(config_blob_key_t);
00302     gencfg.key.data_p = key;
00303 
00304     err = junos_kcom_gencfg(&gencfg);
00305     if (err != KCOM_OK) {
00306         EQ2_LOG(TRACE_LOG_ERR, "%s: Delete blob ERROR %d!", __func__, err);
00307         return -1;
00308     }
00309     return 0;
00310 }
00311 
00319 int
00320 kcom_get_config_blob (void)
00321 {
00322     junos_kcom_gencfg_t gencfg;
00323     int err = 0;
00324     config_blob_key_t key;
00325 
00326     EQ2_TRACE(EQ2_TRACEFLAG_KCOM, "%s", __func__);
00327 
00328     JUNOS_KCOM_GENCFG_INIT(&gencfg);
00329     gencfg.blob_id = eq2_provider_id;
00330     gencfg.dest = JUNOS_KCOM_GENCFG_DEST_RE_AND_PFE;
00331 
00332     /* Clear key pointer and size to get the first blob. */
00333     gencfg.key.data_p = NULL;
00334     gencfg.key.size = 0;
00335     key.key_name[0] = '\0';
00336     key.key_tag = 0;
00337     key.key_plugin_id = 0;
00338     while (1) {
00339         EQ2_TRACE(EQ2_TRACEFLAG_KCOM, "%s: Req blob %s, tag %d, pid %d, %d.",
00340                 __func__, key.key_name, key.key_tag, key.key_plugin_id,
00341                 gencfg.key.size);
00342 
00343         gencfg.opcode = JUNOS_KCOM_GENCFG_OPCODE_BLOB_GETNEXT;
00344         err = junos_kcom_gencfg(&gencfg);
00345 
00346         /* ENOENT indicates no more blob when doing get-next. */
00347         if (err == ENOENT) {
00348             EQ2_TRACE(EQ2_TRACEFLAG_KCOM, "%s: No more blob.", __func__);
00349             break;
00350         } else if (err != KCOM_OK) {
00351             EQ2_LOG(TRACE_LOG_ERR, "%s: Get next blob ERROR (%d)!",
00352                     __func__, err);
00353             return -1;
00354         }
00355 
00356         /* Save the blob key for getting next blob. */
00357         memcpy(&key, gencfg.key.data_p, sizeof(key));
00358         EQ2_TRACE(EQ2_TRACEFLAG_KCOM, "%s: Got blob %s, tag %d, pid %d, "
00359                 "key size %d.",
00360                 __func__, key.key_name, key.key_tag, key.key_plugin_id,
00361                 gencfg.key.size);
00362 
00363         switch (key.key_tag) {
00364         case CONFIG_BLOB_SVC_SET:
00365             config_svc_set_blob_proc(&key, gencfg.blob.data_p);
00366             break;
00367         case CONFIG_BLOB_SVR_GROUP:
00368             config_svr_group_blob_proc(&key, gencfg.blob.data_p);
00369             break;
00370         default:
00371             EQ2_LOG(TRACE_LOG_ERR, "%s: Unkown blob!", __func__);
00372         }
00373 
00374         /* Free blob memory assigned by API. */
00375         gencfg.opcode = JUNOS_KCOM_GENCFG_OPCODE_GET_FREE;
00376         err = junos_kcom_gencfg(&gencfg);
00377         if (err != KCOM_OK) {
00378             EQ2_LOG(TRACE_LOG_ERR, "%s: Free blob ERROR!", __func__);
00379             return -1;
00380         }
00381 
00382         /* Set parameters to get the next blob. */
00383         gencfg.key.size = sizeof(config_blob_key_t);
00384         gencfg.key.data_p = &key;
00385     }
00386 
00387     return 0;
00388 }
00389 
00400 int
00401 kcom_init (evContext ctx)
00402 {
00403     int status;
00404     junos_kcom_gencfg_t kcom_req;
00405     junos_kcom_pub_blob_req_t kpb_req;
00406 
00407     EQ2_TRACE(EQ2_TRACEFLAG_KCOM, "%s: Initialize KCOM.", __func__);
00408 
00409     status = junos_kcom_init(eq2_origin_id, ctx);
00410     INSIST(status == KCOM_OK);
00411 
00412     /* Register for IFD notification (listen for ms interfaces going down). */
00413     status = junos_kcom_register_ifd_handler(NULL, ifd_async_hdlr);
00414     if (status != KCOM_OK) {
00415         EQ2_LOG(TRACE_LOG_ERR, "%s: Register ifd handler ERROR!",
00416                 __func__);
00417     }
00418 
00419     /* Register gencfg handler. */
00420     JUNOS_KCOM_GENCFG_INIT(&kcom_req);
00421     kcom_req.opcode = JUNOS_KCOM_GENCFG_OPCODE_INIT;
00422     kcom_req.user_handler = gencfg_async_hdlr;
00423 
00424     status = junos_kcom_gencfg(&kcom_req);
00425     if (status != KCOM_OK) {
00426         EQ2_LOG(TRACE_LOG_ERR, "%s: Register gencfg handler ERROR!",
00427                 __func__);
00428         return status;
00429     }
00430 
00431     /* Register SSRB handler. */
00432     bzero(&kpb_req, sizeof(junos_kcom_pub_blob_req_t));
00433     kpb_req.opcode = JUNOS_KCOM_PUB_BLOB_REG;
00434     kpb_req.blob_id = JNX_PUB_EXT_SVC_SSR_BLOB; 
00435     kpb_req.msg_user_info = NULL;
00436     kpb_req.msg_user_handler = ssrb_async_hdlr;
00437 
00438     status = junos_kcom_pub_blob_req(&kpb_req);
00439     if (status != KCOM_OK) {
00440         EQ2_LOG(TRACE_LOG_ERR, "%s: Register SSRB handler ERROR!",
00441                 __func__);
00442         return status;
00443     }
00444 
00445     /* Request the KCOM to get all the public blobs registered till now. */
00446     bzero(&kpb_req, sizeof(junos_kcom_pub_blob_req_t));
00447     kpb_req.opcode = JUNOS_KCOM_PUB_BLOB_GET_ALL;
00448     kpb_req.blob_id = JNX_PUB_EXT_SVC_SSR_BLOB;
00449     kpb_req.get_all_user_info = NULL;
00450     kpb_req.get_all_user_func = get_initial_ssrb;
00451 
00452     status = junos_kcom_pub_blob_req(&kpb_req);
00453     if (status != KCOM_OK) {
00454         EQ2_LOG(TRACE_LOG_ERR, "%s: Get all pub blobs ERROR!",
00455                 __func__);
00456         return status;
00457     }
00458 
00459     return status;
00460 }
00461 
00466 void
00467 kcom_close (void)
00468 {
00469     junos_kcom_pub_blob_req_t kpb_req;
00470 
00471     EQ2_TRACE(EQ2_TRACEFLAG_KCOM, "%s", __func__);
00472 
00473     bzero(&kpb_req, sizeof(junos_kcom_pub_blob_req_t));
00474     kpb_req.opcode = JUNOS_KCOM_PUB_BLOB_DEREG;
00475     kpb_req.blob_id = JNX_PUB_EXT_SVC_SSR_BLOB; 
00476     junos_kcom_pub_blob_req(&kpb_req);
00477 
00478     junos_kcom_shutdown();
00479 }
00480 

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