Using the libsvcs-mgmt Library

As background for this topic, you should understand the KCOM GENCFG system, which is described at Working with the Junos Kernel and Using the GENCFG/SSRB Library.

Applications that implement services need to read the service-set resolution blobs (SSRBs) that the system creates to store service set name-to-ID mappings. These SSRBs are stored in a database that is accessible to external applications. An application's management component on the Routing Engine can use the libsvcs-mgmt functions to access the SSRB database, and the system automatically downloads the data to the PIC.

libsvcs-mgmt provides two event handlers that an application can invoke during startup to do the following:

The APIs are in your backing sandbox in the header file at src/junos/lib/libsvcs-mgmt/h/jnx/ssrb_parse.h.

The asynchronous handler and the synchronous handler have similar functionality. When the application starts up, it first checks for any existing SSRBs in the kernel in the synchronous handler. The system reads through the blobs in the SSRB database and invokes the synchronous callback for each SSRB found. The application can then supply its own data (if any), and continue performing its own work.

In addition, the application registers an asynchronous event handler to capture any new additions of SSRBs to the kernel. Whenever a new SSRB is added, or an existing one is deleted or changed, the system invokes the asynchronous callback. Again, the application can then supply its own data.

The SSRB database is structured as a patricia tree. The ssrb_node_t structure stores each SSRB as a node in the patricia tree.

Example of Using SSRB Callbacks

The following test code shows the use of both a synchronous callback, my_ssrb_sync_callback(), and an asynchronous callback, my_ssrb_sync_callback(). In this simple test code, both callbacks simply print the contents of the fields in the SSRB structure. Both callbacks set parameters to do the following:

The asynchronous callback provides an additional parameter (op) that allows the system to report the kind of SSRB it found: added, deleted, or changed.

#include <ddl/dax.h>
#include <ddl/dtypes.h>
#include <jnx/patricia.h>
#include <jnx/trace.h>
#include <netinet/in.h>
#include <jnx/ipc_types.h>
#include <jnx/ipc_asp_cfg.h>
#include <jnx/junos_kcom.h>
#include <jnx/junos_kcom_pub_blob.h>
#include <jnx/ipc_gencfg.h>
#include <jnx/ipc_gencfg_pub.h>
#include <jnx/ssrb_parse.h>

#define OBJ_DATA "SERVICE SET AND SSRB TEST PROGRAM"
#define OBJ_DATALEN strlen(OBJ_DATA) + 1
#define ATTRIBUTE "service-set-name"
#define SVC_SET_NAME "sset2"

/* Test data */
void *object;

void my_ssrb_sync_callback(junos_kcom_pub_ssrb_t* pub_ssrb, void **data,
                           size_t *datalen);

void my_ssrb_async_callback(junos_kcom_pub_ssrb_t* pub_ssrb, void **data,
                            size_t *datalen, 
                            junos_kcom_pub_blob_msg_opcode_t op);


/*
 * Test async handler for SSRB
*/
void 
my_ssrb_async_callback (junos_kcom_pub_ssrb_t* pub_ssrb, void **data,
                       size_t *datalen, junos_kcom_pub_blob_msg_opcode_t op)
{
    printf("SSRB async callback invoked\n");
    printf("---------------------------\n");

    if (!pub_ssrb) {
        printf("%s: SSRB blob not present\n", __func__);
        return;
    } 

    *data = (char *)object;
    *datalen = OBJ_DATALEN;

    printf("\n<--- SSRB async node Info --->\n");
    printf("Op: %d\n",op); 
    printf("Id: %d\n", pub_ssrb->svc_set_id);
    printf("Name: %s\n", pub_ssrb->svc_set_name);
    printf("Gennum: %d\n", pub_ssrb->gen_num);
    printf("In_nh_idx: %d\n", nh_idx_t_getval(pub_ssrb->in_nh_idx));
    printf("Out_nh_idx: %d\n", nh_idx_t_getval( pub_ssrb->out_nh_idx));

    return;
}

/*
 * Test sync handler for SSRB
 */
void
my_ssrb_sync_callback (junos_kcom_pub_ssrb_t* pub_ssrb, void **data,
                      size_t *datalen) 
{

    printf("SSRB sync callback invoked\n");
    printf("--------------------------\n");

    if (!pub_ssrb) {
        printf("%s: SSRB blob not present\n", __func__);
        return;
    } 

    *data = (char *)object;
    *datalen = OBJ_DATALEN;

    printf("\n<--- SSRB sync node Info --->\n");
    printf("Id: %d\n", pub_ssrb->svc_set_id);
    printf("Name: %s\n", pub_ssrb->svc_set_name);
    printf("Gennum: %d\n", pub_ssrb->gen_num);
    printf("In_nh_idx: %d\n", nh_idx_t_getval(pub_ssrb->in_nh_idx));
    printf("Out_nh_idx: %d\n", nh_idx_t_getval( pub_ssrb->out_nh_idx));
    
    return;
}

/*
 * Print the SSRB node info
 *
 * @param[in] node
 *     SSRB node
 */
static void
print_ssrb_info (ssrb_node_t *node)
{
    if (!node) {
        printf("%s SSRB node NULL\n", __func__);
        return;
    }

    printf("\n<--- SSRB Node Info --->\n");
    printf("Id: %d\n", node->sset_id);
    printf("Name: %s\n", node->sset_name);
    printf("Gennum: %d\n", node->gen_num);
    printf("In_nh_idx: %d\n", nh_idx_t_getval(pub_ssrb->in_nh_idx));
    printf("Out_nh_idx: %d\n", nh_idx_t_getval( pub_ssrb->out_nh_idx));
    
    if (node->datalen) {
        printf("Data: %s\n", node->data);
    }

    return;
}

int 
main (void)
{
    int error;
    ssrb_node_t *ssrb_node;

    evContext context;

    evCreate(&context);

    if (junos_kcom_init(0, context) != KCOM_OK) {
        printf("Failed to initialize KCOM\n");
        return -1;
    }

    object = calloc(1,OBJ_DATALEN);
    
    strlcpy(object, OBJ_DATA, OBJ_DATALEN);

    /* Initialize the patricia tree root that is used for storing 
    SSRB nodes in the SSRB database:*/
    
    ssrb_config_init();

    error = svcset_ssrb_sync(my_ssrb_sync_callback, NULL);

    ssrb_node = ssrb_get_node_by_name("sset1");

    if (!ssrb_node) {
        printf("Request SSRB node not present\n");
    } else {
        print_ssrb_info(ssrb_node);
    }

    error = svcset_ssrb_async(my_ssrb_async_callback, NULL);

    evMainLoopSyncSighdl(context);

    junos_kcom_shutdown();

    free(object);

    return 0;
}

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:47 2010 for Juniper Networks Partner Solution Development Platform JUNOS SDK 10.2R1 by Doxygen 1.4.5