connector.rst 5.4 KB

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