关于使用netlink message and attributes interface的建议


   
关于使用netlink message and attributes interface的建议
2010-07-31 12:44

author: jonathan

本文档的CopyRight归jonathan所有,可自由转载,转载时请保持文档的完整性。
/*----------------------------------------------------------------------------------------------------------------------------*/

1 建议
再写netlink代码(无论是用户空间还是内核空间)其格式最好都按照netlink给出一套规则去做。不仅因为对netlink规则linux给出完整的数据结构和接口函数,而且这套规则符合“名称/属性”标准定义数据的方式,这种方式在rfc中有明确的定义,其灵活可靠。

我在使用过程中基本都是按照这个标准,而且把这种机制也灵活运用到其他项目中。

因此建议大家在写netlink相关项目时候,尽量参考标准netlink机制。

2 注意事项

这里仅简单说一下关于NLA_F_NESTED这个属性。

使用netlink Message and Attributes Interface时,使用nla_parse解析属性时要求每一个属性类型都是唯一的,也就是如下所示:


    nlmsghdr | Pad | Family Header | Pad | attri1 | Pad | attri value | Pad | attri2 | Pad | attri value | Pad | attri3 | ...

其实,一个对象的属性本来就是唯一的。但是如果一个属性下面又分了多个属性,怎么办?NLA_F_NESTED粉末登场了。使用NLA_F_NESTED可以达到属性多级嵌套,其数据结构如下:

    
    nlmsghdr | Pad | Family Header | Pad | attri1 | Pad | attri value | attri2 | Pad | attri:NLA_F_NESTED | Pad | attri2_1 | Pad | attri value | Pad | attri2_2 | Pad | ....

    通过nla_parse得到attri2的属性值,再通过nla_parse_nested得到嵌套在attri2里面的属性值。


附录:

/* ========================================================================
*         Netlink Messages and Attributes Interface (As Seen On TV)
* ------------------------------------------------------------------------
*                          Messages Interface
* ------------------------------------------------------------------------
*
* Message Format:
*    <--- nlmsg_total_size(payload) --->
*    <-- nlmsg_msg_size(payload) ->
*   +----------+- - -+-------------+- - -+-------- - -
*   | nlmsghdr | Pad |   Payload   | Pad | nlmsghdr
*   +----------+- - -+-------------+- - -+-------- - -
*   nlmsg_data(nlh)--^                   ^
*   nlmsg_next(nlh)----------------------+
*
* Payload Format:
*    <---------------------- nlmsg_len(nlh) --------------------->
*    <------ hdrlen ------>       <- nlmsg_attrlen(nlh, hdrlen) ->
*   +----------------------+- - -+--------------------------------+
*   |     Family Header    | Pad |           Attributes           |
*   +----------------------+- - -+--------------------------------+
*   nlmsg_attrdata(nlh, hdrlen)--^
*
* Data Structures:
*   struct nlmsghdr                    netlink message header
*
* Message Construction:
*   nlmsg_new()                        create a new netlink message
*   nlmsg_put()                        add a netlink message to an skb
*   nlmsg_put_answer()                 callback based nlmsg_put()
*   nlmsg_end()                        finanlize netlink message
*   nlmsg_get_pos()                    return current position in message
*   nlmsg_trim()                       trim part of message
*   nlmsg_cancel()                     cancel message construction
*   nlmsg_free()                       free a netlink message
*
* Message Sending:
*   nlmsg_multicast()                  multicast message to several groups
*   nlmsg_unicast()                    unicast a message to a single socket
*   nlmsg_notify()                     send notification message
*
* Message Length Calculations:
*   nlmsg_msg_size(payload)            length of message w/o padding
*   nlmsg_total_size(payload)          length of message w/ padding
*   nlmsg_padlen(payload)              length of padding at tail
*
* Message Payload Access:
*   nlmsg_data(nlh)                    head of message payload
*   nlmsg_len(nlh)                     length of message payload
*   nlmsg_attrdata(nlh, hdrlen)        head of attributes data
*   nlmsg_attrlen(nlh, hdrlen)         length of attributes data
*
* Message Parsing:
*   nlmsg_ok(nlh, remaining)           does nlh fit into remaining bytes?
*   nlmsg_next(nlh, remaining)         get next netlink message
*   nlmsg_parse()                      parse attributes of a message
*   nlmsg_find_attr()                  find an attribute in a message
*   nlmsg_for_each_msg()               loop over all messages
*   nlmsg_validate()                   validate netlink message incl. attrs
*   nlmsg_for_each_attr()              loop over all attributes
*
* Misc:
*   nlmsg_report()                     report back to application?
*
* ------------------------------------------------------------------------
*                          Attributes Interface
* ------------------------------------------------------------------------
*
* Attribute Format:
*    <------- nla_total_size(payload) ------->
*    <---- nla_attr_size(payload) ----->
*   +----------+- - -+- - - - - - - - - +- - -+-------- - -
*   | Header | Pad |     Payload      | Pad | Header
*   +----------+- - -+- - - - - - - - - +- - -+-------- - -
*                     <- nla_len(nla) ->      ^
*   nla_data(nla)----^                        |
*   nla_next(nla)-----------------------------'
*
* Data Structures:
*   struct nlattr                      netlink attribute header
*
* Attribute Construction:
*   nla_reserve(skb, type, len)        reserve room for an attribute
*   nla_reserve_nohdr(skb, len)        reserve room for an attribute w/o hdr
*   nla_put(skb, type, len, data)      add attribute to skb
*   nla_put_nohdr(skb, len, data)      add attribute w/o hdr
*   nla_append(skb, len, data)         append data to skb
*
* Attribute Construction for Basic Types:
*   nla_put_u8(skb, type, value)       add u8 attribute to skb
*   nla_put_u16(skb, type, value)      add u16 attribute to skb
*   nla_put_u32(skb, type, value)      add u32 attribute to skb
*   nla_put_u64(skb, type, value)      add u64 attribute to skb
*   nla_put_string(skb, type, str)     add string attribute to skb
*   nla_put_flag(skb, type)            add flag attribute to skb
*   nla_put_msecs(skb, type, jiffies) add msecs attribute to skb
*
* Exceptions Based Attribute Construction:
*   NLA_PUT(skb, type, len, data)      add attribute to skb
*   NLA_PUT_U8(skb, type, value)       add u8 attribute to skb
*   NLA_PUT_U16(skb, type, value)      add u16 attribute to skb
*   NLA_PUT_U32(skb, type, value)      add u32 attribute to skb
*   NLA_PUT_U64(skb, type, value)      add u64 attribute to skb
*   NLA_PUT_STRING(skb, type, str)     add string attribute to skb
*   NLA_PUT_FLAG(skb, type)            add flag attribute to skb
*   NLA_PUT_MSECS(skb, type, jiffies) add msecs attribute to skb
*
*   The meaning of these functions is equal to their lower case
*   variants but they jump to the label nla_put_failure in case
*   of a failure.
*
* Nested Attributes Construction:
*   nla_nest_start(skb, type)          start a nested attribute
*   nla_nest_end(skb, nla)             finalize a nested attribute
*   nla_nest_compat_start(skb, type,   start a nested compat attribute
*                         len, data)
*   nla_nest_compat_end(skb, type)     finalize a nested compat attribute
*   nla_nest_cancel(skb, nla)          cancel nested attribute construction
*
* Attribute Length Calculations:
*   nla_attr_size(payload)             length of attribute w/o padding
*   nla_total_size(payload)            length of attribute w/ padding
*   nla_padlen(payload)                length of padding
*
* Attribute Payload Access:
*   nla_data(nla)                      head of attribute payload
*   nla_len(nla)                       length of attribute payload
*
* Attribute Payload Access for Basic Types:
*   nla_get_u8(nla)                    get payload for a u8 attribute
*   nla_get_u16(nla)                   get payload for a u16 attribute
*   nla_get_u32(nla)                   get payload for a u32 attribute
*   nla_get_u64(nla)                   get payload for a u64 attribute
*   nla_get_flag(nla)                  return 1 if flag is true
*   nla_get_msecs(nla)                 get payload for a msecs attribute
*
* Attribute Misc:
*   nla_memcpy(dest, nla, count)       copy attribute into memory
*   nla_memcmp(nla, data, size)        compare attribute with memory area
*   nla_strlcpy(dst, nla, size)        copy attribute to a sized string
*   nla_strcmp(nla, str)               compare attribute with string
*
* Attribute Parsing:
*   nla_ok(nla, remaining)             does nla fit into remaining bytes?
*   nla_next(nla, remaining)           get next netlink attribute
*   nla_validate()                     validate a stream of attributes
*   nla_validate_nested()              validate a stream of nested attributes
*   nla_find()                         find attribute in stream of attributes
*   nla_find_nested()                  find attribute in nested attributes
*   nla_parse()                        parse and validate stream of attrs
*   nla_parse_nested()                 parse nested attribuets
*   nla_parse_nested_compat()          parse nested compat attributes
*   nla_for_each_attr()                loop over all attributes
*   nla_for_each_nested()              loop over the nested attributes
*=========================================================================
*/
/**
* Standard attribute types to specify validation policy
*/
enum {
        NLA_UNSPEC,
        NLA_U8,
        NLA_U16,
        NLA_U32,
        NLA_U64,
        NLA_STRING,
        NLA_FLAG,
        NLA_MSECS,
        NLA_NESTED,
        NLA_NESTED_COMPAT,
        NLA_NUL_STRING,
        NLA_BINARY,
        __NLA_TYPE_MAX,
};

#define NLA_TYPE_MAX (__NLA_TYPE_MAX - 1)

/**
* struct nla_policy - attribute validation policy
* @type: Type of attribute or NLA_UNSPEC
* @len: Type specific length of payload
*
* Policies are defined as arrays of this struct, the array must be
* accessible by attribute type up to the highest identifier to be expected.
*
* Meaning of `len' field:
*    NLA_STRING           Maximum length of string
*    NLA_NUL_STRING       Maximum length of string (excluding NUL)
*    NLA_FLAG             Unused
*    NLA_BINARY           Maximum length of attribute payload
*    NLA_NESTED_COMPAT    Exact length of structure payload
*    All other            Exact length of attribute payload
*
* Example:
* static struct nla_policy my_policy[ATTR_MAX+1] __read_mostly = {
*      [ATTR_FOO] = { .type = NLA_U16 },
*      [ATTR_BAR] = { .type = NLA_STRING, .len = BARSIZ },
*      [ATTR_BAZ] = { .len = sizeof(struct mystruct) },
* };
*/
struct nla_policy {
        u16             type;
        u16             len;
};

/**
* struct nl_info - netlink source information
* @nlh: Netlink message header of original request
* @pid: Netlink PID of requesting application
*/
struct nl_info {
        struct nlmsghdr         *nlh;
        struct net              *nl_net;
        u32                     pid;
};

你可能感兴趣的:(String,struct,interface,nested,attributes,construction)