aboutsummaryrefslogtreecommitdiffstats
path: root/Documentation/connector
diff options
context:
space:
mode:
authorEvgeniy Polyakov <johnpol@2ka.mipt.ru>2005-09-11 22:15:07 -0400
committerDavid S. Miller <davem@davemloft.net>2005-09-11 22:15:07 -0400
commit7672d0b54411371e0b6a831c1cb2f0ce615de6dc (patch)
tree27d88da3263041b91d18346b3bcd27807d332f1a /Documentation/connector
parent357d596bd552ad157a906289ab13ea6ba7e66e3d (diff)
[NET]: Add netlink connector.
Kernel connector - new userspace <-> kernel space easy to use communication module which implements easy to use bidirectional message bus using netlink as it's backend. Connector was created to eliminate complex skb handling both in send and receive message bus direction. Connector driver adds possibility to connect various agents using as one of it's backends netlink based network. One must register callback and identifier. When driver receives special netlink message with appropriate identifier, appropriate callback will be called. From the userspace point of view it's quite straightforward: socket(); bind(); send(); recv(); But if kernelspace want to use full power of such connections, driver writer must create special sockets, must know about struct sk_buff handling... Connector allows any kernelspace agents to use netlink based networking for inter-process communication in a significantly easier way: int cn_add_callback(struct cb_id *id, char *name, void (*callback) (void *)); void cn_netlink_send(struct cn_msg *msg, u32 __groups, int gfp_mask); struct cb_id { __u32 idx; __u32 val; }; idx and val are unique identifiers which must be registered in connector.h for in-kernel usage. void (*callback) (void *) - is a callback function which will be called when message with above idx.val will be received by connector core. Using connector completely hides low-level transport layer from it's users. Connector uses new netlink ability to have many groups in one socket. [ Incorporating many cleanups and fixes by myself and Andrew Morton -DaveM ] Signed-off-by: Evgeniy Polyakov <johnpol@2ka.mipt.ru> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'Documentation/connector')
-rw-r--r--Documentation/connector/cn_test.c194
-rw-r--r--Documentation/connector/connector.txt133
2 files changed, 327 insertions, 0 deletions
diff --git a/Documentation/connector/cn_test.c b/Documentation/connector/cn_test.c
new file mode 100644
index 000000000000..b7de82e9c0e0
--- /dev/null
+++ b/Documentation/connector/cn_test.c
@@ -0,0 +1,194 @@
1/*
2 * cn_test.c
3 *
4 * 2004-2005 Copyright (c) Evgeniy Polyakov <johnpol@2ka.mipt.ru>
5 * All rights reserved.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/moduleparam.h>
25#include <linux/skbuff.h>
26#include <linux/timer.h>
27
28#include "connector.h"
29
30static struct cb_id cn_test_id = { 0x123, 0x456 };
31static char cn_test_name[] = "cn_test";
32static struct sock *nls;
33static struct timer_list cn_test_timer;
34
35void cn_test_callback(void *data)
36{
37 struct cn_msg *msg = (struct cn_msg *)data;
38
39 printk("%s: %lu: idx=%x, val=%x, seq=%u, ack=%u, len=%d: %s.\n",
40 __func__, jiffies, msg->id.idx, msg->id.val,
41 msg->seq, msg->ack, msg->len, (char *)msg->data);
42}
43
44static int cn_test_want_notify(void)
45{
46 struct cn_ctl_msg *ctl;
47 struct cn_notify_req *req;
48 struct cn_msg *msg = NULL;
49 int size, size0;
50 struct sk_buff *skb;
51 struct nlmsghdr *nlh;
52 u32 group = 1;
53
54 size0 = sizeof(*msg) + sizeof(*ctl) + 3 * sizeof(*req);
55
56 size = NLMSG_SPACE(size0);
57
58 skb = alloc_skb(size, GFP_ATOMIC);
59 if (!skb) {
60 printk(KERN_ERR "Failed to allocate new skb with size=%u.\n",
61 size);
62
63 return -ENOMEM;
64 }
65
66 nlh = NLMSG_PUT(skb, 0, 0x123, NLMSG_DONE, size - sizeof(*nlh));
67
68 msg = (struct cn_msg *)NLMSG_DATA(nlh);
69
70 memset(msg, 0, size0);
71
72 msg->id.idx = -1;
73 msg->id.val = -1;
74 msg->seq = 0x123;
75 msg->ack = 0x345;
76 msg->len = size0 - sizeof(*msg);
77
78 ctl = (struct cn_ctl_msg *)(msg + 1);
79
80 ctl->idx_notify_num = 1;
81 ctl->val_notify_num = 2;
82 ctl->group = group;
83 ctl->len = msg->len - sizeof(*ctl);
84
85 req = (struct cn_notify_req *)(ctl + 1);
86
87 /*
88 * Idx.
89 */
90 req->first = cn_test_id.idx;
91 req->range = 10;
92
93 /*
94 * Val 0.
95 */
96 req++;
97 req->first = cn_test_id.val;
98 req->range = 10;
99
100 /*
101 * Val 1.
102 */
103 req++;
104 req->first = cn_test_id.val + 20;
105 req->range = 10;
106
107 NETLINK_CB(skb).dst_groups = ctl->group;
108 //netlink_broadcast(nls, skb, 0, ctl->group, GFP_ATOMIC);
109 netlink_unicast(nls, skb, 0, 0);
110
111 printk(KERN_INFO "Request was sent. Group=0x%x.\n", ctl->group);
112
113 return 0;
114
115nlmsg_failure:
116 printk(KERN_ERR "Failed to send %u.%u\n", msg->seq, msg->ack);
117 kfree_skb(skb);
118 return -EINVAL;
119}
120
121static u32 cn_test_timer_counter;
122static void cn_test_timer_func(unsigned long __data)
123{
124 struct cn_msg *m;
125 char data[32];
126
127 m = kmalloc(sizeof(*m) + sizeof(data), GFP_ATOMIC);
128 if (m) {
129 memset(m, 0, sizeof(*m) + sizeof(data));
130
131 memcpy(&m->id, &cn_test_id, sizeof(m->id));
132 m->seq = cn_test_timer_counter;
133 m->len = sizeof(data);
134
135 m->len =
136 scnprintf(data, sizeof(data), "counter = %u",
137 cn_test_timer_counter) + 1;
138
139 memcpy(m + 1, data, m->len);
140
141 cn_netlink_send(m, 0, gfp_any());
142 kfree(m);
143 }
144
145 cn_test_timer_counter++;
146
147 mod_timer(&cn_test_timer, jiffies + HZ);
148}
149
150static int cn_test_init(void)
151{
152 int err;
153
154 err = cn_add_callback(&cn_test_id, cn_test_name, cn_test_callback);
155 if (err)
156 goto err_out;
157 cn_test_id.val++;
158 err = cn_add_callback(&cn_test_id, cn_test_name, cn_test_callback);
159 if (err) {
160 cn_del_callback(&cn_test_id);
161 goto err_out;
162 }
163
164 init_timer(&cn_test_timer);
165 cn_test_timer.function = cn_test_timer_func;
166 cn_test_timer.expires = jiffies + HZ;
167 cn_test_timer.data = 0;
168 add_timer(&cn_test_timer);
169
170 return 0;
171
172 err_out:
173 if (nls && nls->sk_socket)
174 sock_release(nls->sk_socket);
175
176 return err;
177}
178
179static void cn_test_fini(void)
180{
181 del_timer_sync(&cn_test_timer);
182 cn_del_callback(&cn_test_id);
183 cn_test_id.val--;
184 cn_del_callback(&cn_test_id);
185 if (nls && nls->sk_socket)
186 sock_release(nls->sk_socket);
187}
188
189module_init(cn_test_init);
190module_exit(cn_test_fini);
191
192MODULE_LICENSE("GPL");
193MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>");
194MODULE_DESCRIPTION("Connector's test module");
diff --git a/Documentation/connector/connector.txt b/Documentation/connector/connector.txt
new file mode 100644
index 000000000000..54a0a14bfbe3
--- /dev/null
+++ b/Documentation/connector/connector.txt
@@ -0,0 +1,133 @@
1/*****************************************/
2Kernel Connector.
3/*****************************************/
4
5Kernel connector - new netlink based userspace <-> kernel space easy
6to use communication module.
7
8Connector driver adds possibility to connect various agents using
9netlink based network. One must register callback and
10identifier. When driver receives special netlink message with
11appropriate identifier, appropriate callback will be called.
12
13From the userspace point of view it's quite straightforward:
14
15 socket();
16 bind();
17 send();
18 recv();
19
20But if kernelspace want to use full power of such connections, driver
21writer must create special sockets, must know about struct sk_buff
22handling... Connector allows any kernelspace agents to use netlink
23based networking for inter-process communication in a significantly
24easier way:
25
26int cn_add_callback(struct cb_id *id, char *name, void (*callback) (void *));
27void cn_netlink_send(struct cn_msg *msg, u32 __group, int gfp_mask);
28
29struct cb_id
30{
31 __u32 idx;
32 __u32 val;
33};
34
35idx and val are unique identifiers which must be registered in
36connector.h for in-kernel usage. void (*callback) (void *) - is a
37callback function which will be called when message with above idx.val
38will be received by connector core. Argument for that function must
39be dereferenced to struct cn_msg *.
40
41struct cn_msg
42{
43 struct cb_id id;
44
45 __u32 seq;
46 __u32 ack;
47
48 __u32 len; /* Length of the following data */
49 __u8 data[0];
50};
51
52/*****************************************/
53Connector interfaces.
54/*****************************************/
55
56int cn_add_callback(struct cb_id *id, char *name, void (*callback) (void *));
57
58Registers new callback with connector core.
59
60struct cb_id *id - unique connector's user identifier.
61 It must be registered in connector.h for legal in-kernel users.
62char *name - connector's callback symbolic name.
63void (*callback) (void *) - connector's callback.
64 Argument must be dereferenced to struct cn_msg *.
65
66void cn_del_callback(struct cb_id *id);
67
68Unregisters new callback with connector core.
69
70struct cb_id *id - unique connector's user identifier.
71
72void cn_netlink_send(struct cn_msg *msg, u32 __groups, int gfp_mask);
73
74Sends message to the specified groups. It can be safely called from
75any context, but may silently fail under strong memory pressure.
76
77struct cn_msg * - message header(with attached data).
78u32 __group - destination group.
79 If __group is zero, then appropriate group will
80 be searched through all registered connector users,
81 and message will be delivered to the group which was
82 created for user with the same ID as in msg.
83 If __group is not zero, then message will be delivered
84 to the specified group.
85int gfp_mask - GFP mask.
86
87Note: When registering new callback user, connector core assigns
88netlink group to the user which is equal to it's id.idx.
89
90/*****************************************/
91Protocol description.
92/*****************************************/
93
94Current offers transport layer with fixed header. Recommended
95protocol which uses such header is following:
96
97msg->seq and msg->ack are used to determine message genealogy. When
98someone sends message it puts there locally unique sequence and random
99acknowledge numbers. Sequence number may be copied into
100nlmsghdr->nlmsg_seq too.
101
102Sequence number is incremented with each message to be sent.
103
104If we expect reply to our message, then sequence number in received
105message MUST be the same as in original message, and acknowledge
106number MUST be the same + 1.
107
108If we receive message and it's sequence number is not equal to one we
109are expecting, then it is new message. If we receive message and it's
110sequence number is the same as one we are expecting, but it's
111acknowledge is not equal acknowledge number in original message + 1,
112then it is new message.
113
114Obviously, protocol header contains above id.
115
116connector allows event notification in the following form: kernel
117driver or userspace process can ask connector to notify it when
118selected id's will be turned on or off(registered or unregistered it's
119callback). It is done by sending special command to connector
120driver(it also registers itself with id={-1, -1}).
121
122As example of usage Documentation/connector now contains cn_test.c -
123testing module which uses connector to request notification and to
124send messages.
125
126/*****************************************/
127Reliability.
128/*****************************************/
129
130Netlink itself is not reliable protocol, that means that messages can
131be lost due to memory pressure or process' receiving queue overflowed,
132so caller is warned must be prepared. That is why struct cn_msg [main
133connector's message header] contains u32 seq and u32 ack fields.