connector.txt 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*****************************************/
  2. Kernel Connector.
  3. /*****************************************/
  4. Kernel connector - new netlink based userspace <-> kernel space easy
  5. to use communication module.
  6. The Connector driver makes it easy to connect various agents using a
  7. netlink based network. One must register a callback and an identifier.
  8. When the driver receives a special netlink message with the appropriate
  9. identifier, the appropriate callback will be called.
  10. From the userspace point of view it's quite straightforward:
  11. socket();
  12. bind();
  13. send();
  14. recv();
  15. But if kernelspace wants to use the full power of such connections, the
  16. driver writer must create special sockets, must know about struct sk_buff
  17. handling, etc... The Connector driver allows any kernelspace agents to use
  18. netlink based networking for inter-process communication in a significantly
  19. easier way:
  20. int cn_add_callback(struct cb_id *id, char *name, void (*callback) (struct cn_msg *, struct netlink_skb_parms *));
  21. void cn_netlink_send_multi(struct cn_msg *msg, u16 len, u32 portid, u32 __group, int gfp_mask);
  22. void cn_netlink_send(struct cn_msg *msg, u32 portid, u32 __group, int gfp_mask);
  23. struct cb_id
  24. {
  25. __u32 idx;
  26. __u32 val;
  27. };
  28. idx and val are unique identifiers which must be registered in the
  29. connector.h header for in-kernel usage. void (*callback) (void *) is a
  30. callback function which will be called when a message with above idx.val
  31. is received by the connector core. The argument for that function must
  32. be dereferenced to struct cn_msg *.
  33. struct cn_msg
  34. {
  35. struct cb_id id;
  36. __u32 seq;
  37. __u32 ack;
  38. __u32 len; /* Length of the following data */
  39. __u8 data[0];
  40. };
  41. /*****************************************/
  42. Connector interfaces.
  43. /*****************************************/
  44. int cn_add_callback(struct cb_id *id, char *name, void (*callback) (struct cn_msg *, struct netlink_skb_parms *));
  45. Registers new callback with connector core.
  46. struct cb_id *id - unique connector's user identifier.
  47. It must be registered in connector.h for legal in-kernel users.
  48. char *name - connector's callback symbolic name.
  49. void (*callback) (struct cn..) - connector's callback.
  50. cn_msg and the sender's credentials
  51. void cn_del_callback(struct cb_id *id);
  52. Unregisters new callback with connector core.
  53. struct cb_id *id - unique connector's user identifier.
  54. int cn_netlink_send_multi(struct cn_msg *msg, u16 len, u32 portid, u32 __groups, int gfp_mask);
  55. int cn_netlink_send(struct cn_msg *msg, u32 portid, u32 __groups, int gfp_mask);
  56. Sends message to the specified groups. It can be safely called from
  57. softirq context, but may silently fail under strong memory pressure.
  58. If there are no listeners for given group -ESRCH can be returned.
  59. struct cn_msg * - message header(with attached data).
  60. u16 len - for *_multi multiple cn_msg messages can be sent
  61. u32 port - destination port.
  62. If non-zero the message will be sent to the
  63. given port, which should be set to the
  64. original sender.
  65. u32 __group - destination group.
  66. If port and __group is zero, then appropriate group will
  67. be searched through all registered connector users,
  68. and message will be delivered to the group which was
  69. created for user with the same ID as in msg.
  70. If __group is not zero, then message will be delivered
  71. to the specified group.
  72. int gfp_mask - GFP mask.
  73. Note: When registering new callback user, connector core assigns
  74. netlink group to the user which is equal to its id.idx.
  75. /*****************************************/
  76. Protocol description.
  77. /*****************************************/
  78. The current framework offers a transport layer with fixed headers. The
  79. recommended protocol which uses such a header is as following:
  80. msg->seq and msg->ack are used to determine message genealogy. When
  81. someone sends a message, they use a locally unique sequence and random
  82. acknowledge number. The sequence number may be copied into
  83. nlmsghdr->nlmsg_seq too.
  84. The sequence number is incremented with each message sent.
  85. If you expect a reply to the message, then the sequence number in the
  86. received message MUST be the same as in the original message, and the
  87. acknowledge number MUST be the same + 1.
  88. If we receive a message and its sequence number is not equal to one we
  89. are expecting, then it is a new message. If we receive a message and
  90. its sequence number is the same as one we are expecting, but its
  91. acknowledge is not equal to the sequence number in the original
  92. message + 1, then it is a new message.
  93. Obviously, the protocol header contains the above id.
  94. The connector allows event notification in the following form: kernel
  95. driver or userspace process can ask connector to notify it when
  96. selected ids will be turned on or off (registered or unregistered its
  97. callback). It is done by sending a special command to the connector
  98. driver (it also registers itself with id={-1, -1}).
  99. As example of this usage can be found in the cn_test.c module which
  100. uses the connector to request notification and to send messages.
  101. /*****************************************/
  102. Reliability.
  103. /*****************************************/
  104. Netlink itself is not a reliable protocol. That means that messages can
  105. be lost due to memory pressure or process' receiving queue overflowed,
  106. so caller is warned that it must be prepared. That is why the struct
  107. cn_msg [main connector's message header] contains u32 seq and u32 ack
  108. fields.
  109. /*****************************************/
  110. Userspace usage.
  111. /*****************************************/
  112. 2.6.14 has a new netlink socket implementation, which by default does not
  113. allow people to send data to netlink groups other than 1.
  114. So, if you wish to use a netlink socket (for example using connector)
  115. with a different group number, the userspace application must subscribe to
  116. that group first. It can be achieved by the following pseudocode:
  117. s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);
  118. l_local.nl_family = AF_NETLINK;
  119. l_local.nl_groups = 12345;
  120. l_local.nl_pid = 0;
  121. if (bind(s, (struct sockaddr *)&l_local, sizeof(struct sockaddr_nl)) == -1) {
  122. perror("bind");
  123. close(s);
  124. return -1;
  125. }
  126. {
  127. int on = l_local.nl_groups;
  128. setsockopt(s, 270, 1, &on, sizeof(on));
  129. }
  130. Where 270 above is SOL_NETLINK, and 1 is a NETLINK_ADD_MEMBERSHIP socket
  131. option. To drop a multicast subscription, one should call the above socket
  132. option with the NETLINK_DROP_MEMBERSHIP parameter which is defined as 0.
  133. 2.6.14 netlink code only allows to select a group which is less or equal to
  134. the maximum group number, which is used at netlink_kernel_create() time.
  135. In case of connector it is CN_NETLINK_USERS + 0xf, so if you want to use
  136. group number 12345, you must increment CN_NETLINK_USERS to that number.
  137. Additional 0xf numbers are allocated to be used by non-in-kernel users.
  138. Due to this limitation, group 0xffffffff does not work now, so one can
  139. not use add/remove connector's group notifications, but as far as I know,
  140. only cn_test.c test module used it.
  141. Some work in netlink area is still being done, so things can be changed in
  142. 2.6.15 timeframe, if it will happen, documentation will be updated for that
  143. kernel.