1. 程式人生 > >關於使用netlink message and attributes interface的建議

關於使用netlink message and attributes interface的建議

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;
};