158 lines · plain
1.. SPDX-License-Identifier: GPL-2.02 3================4Kernel Connector5================6 7Kernel connector - new netlink based userspace <-> kernel space easy8to use communication module.9 10The Connector driver makes it easy to connect various agents using a11netlink based network. One must register a callback and an identifier.12When the driver receives a special netlink message with the appropriate13identifier, the appropriate callback will be called.14 15From the userspace point of view it's quite straightforward:16 17 - socket();18 - bind();19 - send();20 - recv();21 22But if kernelspace wants to use the full power of such connections, the23driver writer must create special sockets, must know about struct sk_buff24handling, etc... The Connector driver allows any kernelspace agents to use25netlink based networking for inter-process communication in a significantly26easier way::27 28 int cn_add_callback(const struct cb_id *id, char *name, void (*callback) (struct cn_msg *, struct netlink_skb_parms *));29 void cn_netlink_send_mult(struct cn_msg *msg, u16 len, u32 portid, u32 __group, int gfp_mask);30 void cn_netlink_send(struct cn_msg *msg, u32 portid, u32 __group, int gfp_mask);31 32 struct cb_id33 {34 __u32 idx;35 __u32 val;36 };37 38idx and val are unique identifiers which must be registered in the39connector.h header for in-kernel usage. `void (*callback) (void *)` is a40callback function which will be called when a message with above idx.val41is received by the connector core. The argument for that function must42be dereferenced to `struct cn_msg *`::43 44 struct cn_msg45 {46 struct cb_id id;47 48 __u32 seq;49 __u32 ack;50 51 __u16 len; /* Length of the following data */52 __u16 flags;53 __u8 data[0];54 };55 56Connector interfaces57====================58 59 .. kernel-doc:: include/linux/connector.h60 61 Note:62 When registering new callback user, connector core assigns63 netlink group to the user which is equal to its id.idx.64 65Protocol description66====================67 68The current framework offers a transport layer with fixed headers. The69recommended protocol which uses such a header is as following:70 71msg->seq and msg->ack are used to determine message genealogy. When72someone sends a message, they use a locally unique sequence and random73acknowledge number. The sequence number may be copied into74nlmsghdr->nlmsg_seq too.75 76The sequence number is incremented with each message sent.77 78If you expect a reply to the message, then the sequence number in the79received message MUST be the same as in the original message, and the80acknowledge number MUST be the same + 1.81 82If we receive a message and its sequence number is not equal to one we83are expecting, then it is a new message. If we receive a message and84its sequence number is the same as one we are expecting, but its85acknowledge is not equal to the sequence number in the original86message + 1, then it is a new message.87 88Obviously, the protocol header contains the above id.89 90The connector allows event notification in the following form: kernel91driver or userspace process can ask connector to notify it when92selected ids will be turned on or off (registered or unregistered its93callback). It is done by sending a special command to the connector94driver (it also registers itself with id={-1, -1}).95 96As example of this usage can be found in the cn_test.c module which97uses the connector to request notification and to send messages.98 99Reliability100===========101 102Netlink itself is not a reliable protocol. That means that messages can103be lost due to memory pressure or process' receiving queue overflowed,104so caller is warned that it must be prepared. That is why the struct105cn_msg [main connector's message header] contains u32 seq and u32 ack106fields.107 108Userspace usage109===============110 1112.6.14 has a new netlink socket implementation, which by default does not112allow people to send data to netlink groups other than 1.113So, if you wish to use a netlink socket (for example using connector)114with a different group number, the userspace application must subscribe to115that group first. It can be achieved by the following pseudocode::116 117 s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);118 119 l_local.nl_family = AF_NETLINK;120 l_local.nl_groups = 12345;121 l_local.nl_pid = 0;122 123 if (bind(s, (struct sockaddr *)&l_local, sizeof(struct sockaddr_nl)) == -1) {124 perror("bind");125 close(s);126 return -1;127 }128 129 {130 int on = l_local.nl_groups;131 setsockopt(s, 270, 1, &on, sizeof(on));132 }133 134Where 270 above is SOL_NETLINK, and 1 is a NETLINK_ADD_MEMBERSHIP socket135option. To drop a multicast subscription, one should call the above socket136option with the NETLINK_DROP_MEMBERSHIP parameter which is defined as 0.137 1382.6.14 netlink code only allows to select a group which is less or equal to139the maximum group number, which is used at netlink_kernel_create() time.140In case of connector it is CN_NETLINK_USERS + 0xf, so if you want to use141group number 12345, you must increment CN_NETLINK_USERS to that number.142Additional 0xf numbers are allocated to be used by non-in-kernel users.143 144Due to this limitation, group 0xffffffff does not work now, so one can145not use add/remove connector's group notifications, but as far as I know,146only cn_test.c test module used it.147 148Some work in netlink area is still being done, so things can be changed in1492.6.15 timeframe, if it will happen, documentation will be updated for that150kernel.151 152Code samples153============154 155Sample code for a connector test module and user space can be found156in samples/connector/. To build this code, enable CONFIG_CONNECTOR157and CONFIG_SAMPLES.158