connector.txt 6.6 KB

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