blob: ab7ca897fab7ee5ada0ac87992065d550e969508 [file] [log] [blame]
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -07001/*****************************************/
2Kernel Connector.
3/*****************************************/
4
5Kernel connector - new netlink based userspace <-> kernel space easy
6to use communication module.
7
Mike Frysinger41144ca2009-07-17 10:13:58 -07008The Connector driver makes it easy to connect various agents using a
9netlink based network. One must register a callback and an identifier.
10When the driver receives a special netlink message with the appropriate
11identifier, the appropriate callback will be called.
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -070012
13From the userspace point of view it's quite straightforward:
14
15 socket();
16 bind();
17 send();
18 recv();
19
Mike Frysinger41144ca2009-07-17 10:13:58 -070020But if kernelspace wants to use the full power of such connections, the
21driver writer must create special sockets, must know about struct sk_buff
22handling, etc... The Connector driver allows any kernelspace agents to use
23netlink based networking for inter-process communication in a significantly
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -070024easier way:
25
Philipp Reisner70693312009-10-02 02:40:05 +000026int cn_add_callback(struct cb_id *id, char *name, void (*callback) (struct cn_msg *, struct netlink_skb_parms *));
David Fries34470e02014-04-08 22:37:08 -050027void cn_netlink_send_multi(struct cn_msg *msg, u16 len, u32 portid, u32 __group, int gfp_mask);
28void cn_netlink_send(struct cn_msg *msg, u32 portid, u32 __group, int gfp_mask);
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -070029
30struct cb_id
31{
32 __u32 idx;
33 __u32 val;
34};
35
Mike Frysinger41144ca2009-07-17 10:13:58 -070036idx and val are unique identifiers which must be registered in the
37connector.h header for in-kernel usage. void (*callback) (void *) is a
38callback function which will be called when a message with above idx.val
39is received by the connector core. The argument for that function must
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -070040be dereferenced to struct cn_msg *.
41
42struct cn_msg
43{
Mike Frysinger41144ca2009-07-17 10:13:58 -070044 struct cb_id id;
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -070045
46 __u32 seq;
47 __u32 ack;
48
49 __u32 len; /* Length of the following data */
50 __u8 data[0];
51};
52
53/*****************************************/
54Connector interfaces.
55/*****************************************/
56
Philipp Reisner70693312009-10-02 02:40:05 +000057int cn_add_callback(struct cb_id *id, char *name, void (*callback) (struct cn_msg *, struct netlink_skb_parms *));
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -070058
Mike Frysinger41144ca2009-07-17 10:13:58 -070059 Registers new callback with connector core.
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -070060
Mike Frysinger41144ca2009-07-17 10:13:58 -070061 struct cb_id *id - unique connector's user identifier.
62 It must be registered in connector.h for legal in-kernel users.
63 char *name - connector's callback symbolic name.
Philipp Reisner70693312009-10-02 02:40:05 +000064 void (*callback) (struct cn..) - connector's callback.
65 cn_msg and the sender's credentials
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -070066
Mike Frysinger41144ca2009-07-17 10:13:58 -070067
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -070068void cn_del_callback(struct cb_id *id);
69
Mike Frysinger41144ca2009-07-17 10:13:58 -070070 Unregisters new callback with connector core.
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -070071
Mike Frysinger41144ca2009-07-17 10:13:58 -070072 struct cb_id *id - unique connector's user identifier.
73
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -070074
David Fries34470e02014-04-08 22:37:08 -050075int cn_netlink_send_multi(struct cn_msg *msg, u16 len, u32 portid, u32 __groups, int gfp_mask);
76int cn_netlink_send(struct cn_msg *msg, u32 portid, u32 __groups, int gfp_mask);
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -070077
Mike Frysinger41144ca2009-07-17 10:13:58 -070078 Sends message to the specified groups. It can be safely called from
79 softirq context, but may silently fail under strong memory pressure.
80 If there are no listeners for given group -ESRCH can be returned.
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -070081
Mike Frysinger41144ca2009-07-17 10:13:58 -070082 struct cn_msg * - message header(with attached data).
David Fries34470e02014-04-08 22:37:08 -050083 u16 len - for *_multi multiple cn_msg messages can be sent
84 u32 port - destination port.
85 If non-zero the message will be sent to the
86 given port, which should be set to the
87 original sender.
Mike Frysinger41144ca2009-07-17 10:13:58 -070088 u32 __group - destination group.
David Fries34470e02014-04-08 22:37:08 -050089 If port and __group is zero, then appropriate group will
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -070090 be searched through all registered connector users,
91 and message will be delivered to the group which was
92 created for user with the same ID as in msg.
93 If __group is not zero, then message will be delivered
94 to the specified group.
Mike Frysinger41144ca2009-07-17 10:13:58 -070095 int gfp_mask - GFP mask.
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -070096
Mike Frysinger41144ca2009-07-17 10:13:58 -070097 Note: When registering new callback user, connector core assigns
Francis Galieguea33f3222010-04-23 00:08:02 +020098 netlink group to the user which is equal to its id.idx.
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -070099
100/*****************************************/
101Protocol description.
102/*****************************************/
103
Mike Frysinger41144ca2009-07-17 10:13:58 -0700104The current framework offers a transport layer with fixed headers. The
105recommended protocol which uses such a header is as following:
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700106
107msg->seq and msg->ack are used to determine message genealogy. When
Mike Frysinger41144ca2009-07-17 10:13:58 -0700108someone sends a message, they use a locally unique sequence and random
109acknowledge number. The sequence number may be copied into
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700110nlmsghdr->nlmsg_seq too.
111
Mike Frysinger41144ca2009-07-17 10:13:58 -0700112The sequence number is incremented with each message sent.
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700113
Mike Frysinger41144ca2009-07-17 10:13:58 -0700114If you expect a reply to the message, then the sequence number in the
115received message MUST be the same as in the original message, and the
116acknowledge number MUST be the same + 1.
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700117
Mike Frysinger41144ca2009-07-17 10:13:58 -0700118If we receive a message and its sequence number is not equal to one we
119are expecting, then it is a new message. If we receive a message and
120its sequence number is the same as one we are expecting, but its
David Fries8a0427d2014-04-08 22:37:09 -0500121acknowledge is not equal to the sequence number in the original
Mike Frysinger41144ca2009-07-17 10:13:58 -0700122message + 1, then it is a new message.
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700123
Mike Frysinger41144ca2009-07-17 10:13:58 -0700124Obviously, the protocol header contains the above id.
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700125
Mike Frysinger41144ca2009-07-17 10:13:58 -0700126The connector allows event notification in the following form: kernel
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700127driver or userspace process can ask connector to notify it when
Mike Frysinger41144ca2009-07-17 10:13:58 -0700128selected ids will be turned on or off (registered or unregistered its
129callback). It is done by sending a special command to the connector
130driver (it also registers itself with id={-1, -1}).
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700131
Mike Frysinger41144ca2009-07-17 10:13:58 -0700132As example of this usage can be found in the cn_test.c module which
133uses the connector to request notification and to send messages.
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700134
135/*****************************************/
136Reliability.
137/*****************************************/
138
Mike Frysinger41144ca2009-07-17 10:13:58 -0700139Netlink itself is not a reliable protocol. That means that messages can
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700140be lost due to memory pressure or process' receiving queue overflowed,
Mike Frysinger41144ca2009-07-17 10:13:58 -0700141so caller is warned that it must be prepared. That is why the struct
142cn_msg [main connector's message header] contains u32 seq and u32 ack
143fields.
Evgeniy Polyakoveb0d6042005-10-13 14:42:04 -0700144
145/*****************************************/
146Userspace usage.
147/*****************************************/
Mike Frysinger41144ca2009-07-17 10:13:58 -0700148
Evgeniy Polyakoveb0d6042005-10-13 14:42:04 -07001492.6.14 has a new netlink socket implementation, which by default does not
Mike Frysinger41144ca2009-07-17 10:13:58 -0700150allow people to send data to netlink groups other than 1.
151So, if you wish to use a netlink socket (for example using connector)
152with a different group number, the userspace application must subscribe to
153that group first. It can be achieved by the following pseudocode:
Evgeniy Polyakoveb0d6042005-10-13 14:42:04 -0700154
155s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);
156
157l_local.nl_family = AF_NETLINK;
158l_local.nl_groups = 12345;
159l_local.nl_pid = 0;
160
161if (bind(s, (struct sockaddr *)&l_local, sizeof(struct sockaddr_nl)) == -1) {
162 perror("bind");
163 close(s);
164 return -1;
165}
166
167{
168 int on = l_local.nl_groups;
169 setsockopt(s, 270, 1, &on, sizeof(on));
170}
171
172Where 270 above is SOL_NETLINK, and 1 is a NETLINK_ADD_MEMBERSHIP socket
Mike Frysinger41144ca2009-07-17 10:13:58 -0700173option. To drop a multicast subscription, one should call the above socket
174option with the NETLINK_DROP_MEMBERSHIP parameter which is defined as 0.
Evgeniy Polyakoveb0d6042005-10-13 14:42:04 -0700175
1762.6.14 netlink code only allows to select a group which is less or equal to
177the maximum group number, which is used at netlink_kernel_create() time.
178In case of connector it is CN_NETLINK_USERS + 0xf, so if you want to use
179group number 12345, you must increment CN_NETLINK_USERS to that number.
180Additional 0xf numbers are allocated to be used by non-in-kernel users.
181
182Due to this limitation, group 0xffffffff does not work now, so one can
183not use add/remove connector's group notifications, but as far as I know,
184only cn_test.c test module used it.
185
186Some work in netlink area is still being done, so things can be changed in
1872.6.15 timeframe, if it will happen, documentation will be updated for that
188kernel.
Arnd Bergmann14fbff62016-04-25 18:03:08 +0200189
190/*****************************************/
191Code samples
192/*****************************************/
193
194Sample code for a connector test module and user space can be found
195in samples/connector/. To build this code, enable CONFIG_CONNECTOR
196and CONFIG_SAMPLES.