Path: blob/master/Documentation/connector/connector.txt
10821 views
/*****************************************/1Kernel Connector.2/*****************************************/34Kernel connector - new netlink based userspace <-> kernel space easy5to use communication module.67The Connector driver makes it easy to connect various agents using a8netlink based network. One must register a callback and an identifier.9When the driver receives a special netlink message with the appropriate10identifier, the appropriate callback will be called.1112From the userspace point of view it's quite straightforward:1314socket();15bind();16send();17recv();1819But if kernelspace wants to use the full power of such connections, the20driver writer must create special sockets, must know about struct sk_buff21handling, etc... The Connector driver allows any kernelspace agents to use22netlink based networking for inter-process communication in a significantly23easier way:2425int cn_add_callback(struct cb_id *id, char *name, void (*callback) (struct cn_msg *, struct netlink_skb_parms *));26void cn_netlink_send(struct cn_msg *msg, u32 __group, int gfp_mask);2728struct cb_id29{30__u32 idx;31__u32 val;32};3334idx and val are unique identifiers which must be registered in the35connector.h header for in-kernel usage. void (*callback) (void *) is a36callback function which will be called when a message with above idx.val37is received by the connector core. The argument for that function must38be dereferenced to struct cn_msg *.3940struct cn_msg41{42struct cb_id id;4344__u32 seq;45__u32 ack;4647__u32 len; /* Length of the following data */48__u8 data[0];49};5051/*****************************************/52Connector interfaces.53/*****************************************/5455int cn_add_callback(struct cb_id *id, char *name, void (*callback) (struct cn_msg *, struct netlink_skb_parms *));5657Registers new callback with connector core.5859struct cb_id *id - unique connector's user identifier.60It must be registered in connector.h for legal in-kernel users.61char *name - connector's callback symbolic name.62void (*callback) (struct cn..) - connector's callback.63cn_msg and the sender's credentials646566void cn_del_callback(struct cb_id *id);6768Unregisters new callback with connector core.6970struct cb_id *id - unique connector's user identifier.717273int cn_netlink_send(struct cn_msg *msg, u32 __groups, int gfp_mask);7475Sends message to the specified groups. It can be safely called from76softirq context, but may silently fail under strong memory pressure.77If there are no listeners for given group -ESRCH can be returned.7879struct cn_msg * - message header(with attached data).80u32 __group - destination group.81If __group is zero, then appropriate group will82be searched through all registered connector users,83and message will be delivered to the group which was84created for user with the same ID as in msg.85If __group is not zero, then message will be delivered86to the specified group.87int gfp_mask - GFP mask.8889Note: When registering new callback user, connector core assigns90netlink group to the user which is equal to its id.idx.9192/*****************************************/93Protocol description.94/*****************************************/9596The current framework offers a transport layer with fixed headers. The97recommended protocol which uses such a header is as following:9899msg->seq and msg->ack are used to determine message genealogy. When100someone sends a message, they use a locally unique sequence and random101acknowledge number. The sequence number may be copied into102nlmsghdr->nlmsg_seq too.103104The sequence number is incremented with each message sent.105106If you expect a reply to the message, then the sequence number in the107received message MUST be the same as in the original message, and the108acknowledge number MUST be the same + 1.109110If we receive a message and its sequence number is not equal to one we111are expecting, then it is a new message. If we receive a message and112its sequence number is the same as one we are expecting, but its113acknowledge is not equal to the acknowledge number in the original114message + 1, then it is a new message.115116Obviously, the protocol header contains the above id.117118The connector allows event notification in the following form: kernel119driver or userspace process can ask connector to notify it when120selected ids will be turned on or off (registered or unregistered its121callback). It is done by sending a special command to the connector122driver (it also registers itself with id={-1, -1}).123124As example of this usage can be found in the cn_test.c module which125uses the connector to request notification and to send messages.126127/*****************************************/128Reliability.129/*****************************************/130131Netlink itself is not a reliable protocol. That means that messages can132be lost due to memory pressure or process' receiving queue overflowed,133so caller is warned that it must be prepared. That is why the struct134cn_msg [main connector's message header] contains u32 seq and u32 ack135fields.136137/*****************************************/138Userspace usage.139/*****************************************/1401412.6.14 has a new netlink socket implementation, which by default does not142allow people to send data to netlink groups other than 1.143So, if you wish to use a netlink socket (for example using connector)144with a different group number, the userspace application must subscribe to145that group first. It can be achieved by the following pseudocode:146147s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);148149l_local.nl_family = AF_NETLINK;150l_local.nl_groups = 12345;151l_local.nl_pid = 0;152153if (bind(s, (struct sockaddr *)&l_local, sizeof(struct sockaddr_nl)) == -1) {154perror("bind");155close(s);156return -1;157}158159{160int on = l_local.nl_groups;161setsockopt(s, 270, 1, &on, sizeof(on));162}163164Where 270 above is SOL_NETLINK, and 1 is a NETLINK_ADD_MEMBERSHIP socket165option. To drop a multicast subscription, one should call the above socket166option with the NETLINK_DROP_MEMBERSHIP parameter which is defined as 0.1671682.6.14 netlink code only allows to select a group which is less or equal to169the maximum group number, which is used at netlink_kernel_create() time.170In case of connector it is CN_NETLINK_USERS + 0xf, so if you want to use171group number 12345, you must increment CN_NETLINK_USERS to that number.172Additional 0xf numbers are allocated to be used by non-in-kernel users.173174Due to this limitation, group 0xffffffff does not work now, so one can175not use add/remove connector's group notifications, but as far as I know,176only cn_test.c test module used it.177178Some work in netlink area is still being done, so things can be changed in1792.6.15 timeframe, if it will happen, documentation will be updated for that180kernel.181182183