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