Proper array usage

Greg S. Woods jimbeam at woods.nu
Thu May 2 13:58:56 EDT 2013


I am trying to determine the correct way to send an array via netlink. 
The "generic_netlink_howto" says the following "Arrays can be
represented by using a single nested attribute as a container with
several of the same attribute type inside each representing a spot in
the array."

What I am trying to do is send an array of 32-bit unsigned integers to
the kernel and have it echo them back(assume that the kernel stuff is
already coded and working).  I "think" this means that I should have a
nested attribute that is filled with NLA_U32's.  Is this how I would go
about doing this?  Is passing the array index into nla_put_u32() as the
attrtype correct?  If I do this what functions would be best to use to
receive the data?  It does not seem like using nla_parse_nested() and a
nla_policy structure would work in this situation.  Should I use
something like nla_for_each_nested() instead?

==============================
/* attributes */
enum
{
    TEST_A_UNSPEC,
    TEST_A_FOO1,
    TEST_A_FOO2,
    TEST_A_ARRAY,
    TEST_A_FOO3,
    __TEST_A_MAX,
};

#define TEST_A_MAX (__TEST_A_MAX - 1)


==============================
static struct nla_policy    test_policy[TEST_A_MAX + 1] =
{
    [TEST_A_FOO1] = {.type = NLA_U16},
    [TEST_A_FOO2] = {.type = NLA_U32},
    [TEST_A_ARRAY] = {.type = NLA_NESTED},
    [TEST_A_FOO3] = {.type = NLA_U8},
};

void sending_function()
{
    /* Add other attributes here */

    const uint32_t array[] =
    {
        /* STUFF HERE */
    };

    const size_t    array_dimension = sizeof( array ) / sizeof( array[ 0
    ] );

    struct nlattr*  elements = nla_nest_start( msg, TEST_A_ARRAY );
    unsigned int i;
    int err;

    for ( i=0; i<array_dimension; i++ )
    {
       err = nla_put_u32( msg, i, array[ i ] );
        if ( err < 0 )
        {
            printf( "Unable to add attribute array: %s\n", nl_geterror(
            err ) );
            break;
        }
    }

    nla_nest_end( msg, elements );

    /* Add other attributes here */
}

void receiving_function()
{
    /* Get other attributes here */

    /* How to parse incoming array? */

    /* Get other attributes here */
{






More information about the libnl mailing list