connector.txt 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*****************************************/
  2. Kernel Connector.
  3. /*****************************************/
  4. Kernel connector - new netlink based userspace <-> kernel space easy
  5. to use communication module.
  6. Connector driver adds possibility to connect various agents using
  7. netlink based network. One must register callback and
  8. identifier. When driver receives special netlink message with
  9. appropriate identifier, 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 want to use full power of such connections, driver
  16. writer must create special sockets, must know about struct sk_buff
  17. handling... Connector allows any kernelspace agents to use netlink
  18. 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) (void *));
  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
  28. connector.h for in-kernel usage. void (*callback) (void *) - is a
  29. callback function which will be called when message with above idx.val
  30. will be received by connector core. 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) (void *));
  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) (void *) - connector's callback.
  49. Argument must be dereferenced to struct cn_msg *.
  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 it's id.idx.
  68. /*****************************************/
  69. Protocol description.
  70. /*****************************************/
  71. Current offers transport layer with fixed header. Recommended
  72. protocol which uses such header is following:
  73. msg->seq and msg->ack are used to determine message genealogy. When
  74. someone sends message it puts there locally unique sequence and random
  75. acknowledge numbers. Sequence number may be copied into
  76. nlmsghdr->nlmsg_seq too.
  77. Sequence number is incremented with each message to be sent.
  78. If we expect reply to our message, then sequence number in received
  79. message MUST be the same as in original message, and acknowledge
  80. number MUST be the same + 1.
  81. If we receive message and it's sequence number is not equal to one we
  82. are expecting, then it is new message. If we receive message and it's
  83. sequence number is the same as one we are expecting, but it's
  84. acknowledge is not equal acknowledge number in original message + 1,
  85. then it is new message.
  86. Obviously, protocol header contains above id.
  87. connector allows event notification in the following form: kernel
  88. driver or userspace process can ask connector to notify it when
  89. selected id's will be turned on or off(registered or unregistered it's
  90. callback). It is done by sending special command to connector
  91. driver(it also registers itself with id={-1, -1}).
  92. As example of usage Documentation/connector now contains cn_test.c -
  93. testing module which uses connector to request notification and to
  94. send messages.
  95. /*****************************************/
  96. Reliability.
  97. /*****************************************/
  98. Netlink itself is not reliable protocol, that means that messages can
  99. be lost due to memory pressure or process' receiving queue overflowed,
  100. so caller is warned must be prepared. That is why struct cn_msg [main
  101. connector's message header] contains u32 seq and u32 ack fields.
  102. /*****************************************/
  103. Userspace usage.
  104. /*****************************************/
  105. 2.6.14 has a new netlink socket implementation, which by default does not
  106. allow to send data to netlink groups other than 1.
  107. So, if to use netlink socket (for example using connector)
  108. with different group number userspace application must subscribe to
  109. that group. It can be achieved by 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 multicast subscription one should call above socket option
  125. with 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.