User Tools

Site Tools


mywiki:linux:generic_netlink

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
mywiki:linux:generic_netlink [2015/05/16 07:13] – [define operation] shaoguohmywiki:linux:generic_netlink [2019/09/15 18:55] (current) – external edit 127.0.0.1
Line 1: Line 1:
 Generic Netlink Generic Netlink
  
-| Reference | [[http://www.linuxfoundation.org/collaborate/workgroups/networking/generic_netlink_howto|Generic Netlink Howto]] |+| Reference | [[http://www.linuxfoundation.org/collaborate/workgroups/networking/generic_netlink_howto|Generic Netlink Howto]] | [[http://blog.csdn.net/zuoziji416/article/details/16892331|Generic Netlink详解 ]] | [[http://blog.chinaunix.net/uid-26710703-id-4056696.html|Linux Generic Netlink 设计与实现 ]] |
  
 ====== Generic Netlink Architecture ====== ====== Generic Netlink Architecture ======
Line 11: Line 11:
  
 Users who want to use a service query the controller to see if the service exists and to determine the correct channel number.  Users who want to use a service query the controller to see if the service exists and to determine the correct channel number. 
 +
  
 <file> <file>
Line 43: Line 44:
  
 </file> </file>
 +
 +
 +====== Data Structure of Generic Netlink ======
 +
 +
 +{{:mywiki:linux:generic_netlink_data_structure.jpg|}}
 +
 +
 +One Family Data structure
 +<file>
 +                   --------------------------
 +               -- |Global attribute: attrbuf |            
 +                   ----------------------- --
 +                         
 +               |
 +               |
 + One Family -- |           
 +               |
 +                                             -----------------------------------------------
 +                                           --| 1st operation: unique command and its policy |
 +                                             ----------------------------------------------- 
 +                   ------------------------    -----------------------------------------------
 +               -- |operation list:ops_list | --| 2nd operation: unique command and its policy |          
 +                   ------------------------    -----------------------------------------------
 +                                               -----------------------------------------------
 +                                             --| nth operation: unique command and its policy |
 +                                               ----------------------------------------------- 
 +                 
 +
 +</file>
 +
  
 ====== Generic Netlink message format ====== ====== Generic Netlink message format ======
Line 63: Line 95:
 </file> </file>
  
-====== Data Structure of Generic Netlink ====== +{{:mywiki:linux:message_hdr.png|}}
- +
- +
-{{:mywiki:linux:generic_netlink_data_structure.jpg|}}+
  
 +{{:mywiki:linux:message_map.png|}}
 ====== Procedure in kernel ====== ====== Procedure in kernel ======
 +
 ===== Register a family ===== ===== Register a family =====
 Generic Netlink families are defined by the genl_family structure, which is shown below: Generic Netlink families are defined by the genl_family structure, which is shown below:
Line 107: Line 138:
 </file> </file>
  
-===== define operation and policy ===== +===== Define operation and policy ===== 
  
 +
 +==== Operation =====  
 Generic Netlink operations are defined by the genl_ops structure, which is shown below: Generic Netlink operations are defined by the genl_ops structure, which is shown below:
 <file> <file>
Line 146: Line 179:
 </file> </file>
        
-===== Define Message structure =====  +==== Policy =====   
- +when sending netlink messages the sender needs to adhere to the protocol format.  
-Generic Netlink message information is passed by the genl_info structure, which is shown below:+The receiver of the message will use struct nla_policy to validate the attributes before the payload is accessed.
  
 <file> <file>
-    struct genl_info 
-    { 
-       u32                     snd_seq; 
-       u32                     snd_pid; 
-       struct nlmsghdr *       nlhdr; 
-       struct genlmsghdr *     genlhdr; 
-       void *                  userhdr; 
-       struct nlattr **        attrs; 
-    }; 
  
-    Fig: The genl_info structure +The nla_policy Structure
- +
-The fields are populated in the following manner: +
- +
-    u32 snd_seq +
-    This is the Netlink sequence number of the request. +
-    u32 snd_pid +
-    This is the Netlink PID of the client which issued the request; it is important to note that the Netlink PID is not the same as the standard kernel PID. +
-    struct nlmsghdr *nlhdr +
-    This is set to point to the Netlink message header of the request. +
-    struct genlmsghdr *genlhdr +
-    This is set to point to the Generic Netlink message header of the request. +
-    void *userhdr +
-    If the Generic Netlink family makes use of a family specific header, this pointer will be set to point to the start of the family specific header. +
-    struct nlattr **attrs +
-    The parsed Netlink attributes from the request; if the Generic Netlink family definition specified a Netlink attribute policy then the attributes would have already been validated. +
-    The doit() handler should do whatever processing is necessary and return zero on success or a negative value on failure. Negative return values will cause an NLMSG_ERROR message to be sent while a zero return value will only cause the NLMSG_ERROR message to be sent if the request is received with the NLM_F_ACK flag set. +
-</file>   +
- +
- +
-===== Define attribute policy ===== +
- +
- +
-<file> +
- The nla_policy Structure+
  
 Generic Netlink attribute policy is defined by the nla_policy structure, which is shown below: Generic Netlink attribute policy is defined by the nla_policy structure, which is shown below:
Line 195: Line 195:
     };     };
  
-Figure 11: The nla_policy structure+    Fig: The nla_policy structure
  
 The fields are used in the following manner: The fields are used in the following manner:
Line 224: Line 224:
     When the attribute type is one of the string types then this field should be set to the maximum length of the string, not including the terminal NULL byte. If the attribute type is unknown or NLA_UNSPEC then this field should be set to the exact length of the attribute's payload.     When the attribute type is one of the string types then this field should be set to the maximum length of the string, not including the terminal NULL byte. If the attribute type is unknown or NLA_UNSPEC then this field should be set to the exact length of the attribute's payload.
     Unless the attribute type is one of the fixed-length types above, a value of zero indicates that no validation of the attribute should be performed.     Unless the attribute type is one of the fixed-length types above, a value of zero indicates that no validation of the attribute should be performed.
 +
 +</file>   
 +
 +
 +===== Define attribute =====
 +
 +
 +<file>
 + 
 +
 </file> </file>
  
Line 234: Line 244:
  
 It is also important to use unique attributes as much as possible. This helps make the most of the Netlink attribute mechanisms and provides for easy changes to the message format in the future.  It is also important to use unique attributes as much as possible. This helps make the most of the Netlink attribute mechanisms and provides for easy changes to the message format in the future. 
 +
 +===== Define Message structure ===== 
 +
 +Generic Netlink message information is passed by the genl_info structure, which is shown below:
 +
 +<file>
 +    struct genl_info
 +    {
 +       u32                     snd_seq;
 +       u32                     snd_pid;
 +       struct nlmsghdr *       nlhdr;
 +       struct genlmsghdr *     genlhdr;
 +       void *                  userhdr;
 +       struct nlattr **        attrs;
 +    };
 +
 +    Fig: The genl_info structure
 +
 +The fields are populated in the following manner:
 +
 +    u32 snd_seq
 +    This is the Netlink sequence number of the request.
 +    u32 snd_pid
 +    This is the Netlink PID of the client which issued the request; it is important to note that the Netlink PID is not the same as the standard kernel PID.
 +    struct nlmsghdr *nlhdr
 +    This is set to point to the Netlink message header of the request.
 +    struct genlmsghdr *genlhdr
 +    This is set to point to the Generic Netlink message header of the request.
 +    void *userhdr
 +    If the Generic Netlink family makes use of a family specific header, this pointer will be set to point to the start of the family specific header.
 +    struct nlattr **attrs
 +    The parsed Netlink attributes from the request; if the Generic Netlink family definition specified a Netlink attribute policy then the attributes would have already been validated.
 +    The doit() handler should do whatever processing is necessary and return zero on success or a negative value on failure. Negative return values will cause an NLMSG_ERROR message to be sent while a zero return value will only cause the NLMSG_ERROR message to be sent if the request is received with the NLM_F_ACK flag set.
 +</file>  
 +
mywiki/linux/generic_netlink.1431731627.txt.gz · Last modified: (external edit)