Blame view
Documentation/connector/connector.txt
6.6 KB
7672d0b54
|
1 2 3 4 5 6 |
/*****************************************/ Kernel Connector. /*****************************************/ Kernel connector - new netlink based userspace <-> kernel space easy to use communication module. |
41144ca3d
|
7 8 9 10 |
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
|
11 12 13 14 15 16 17 |
From the userspace point of view it's quite straightforward: socket(); bind(); send(); recv(); |
41144ca3d
|
18 19 20 21 |
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 |
7672d0b54
|
22 |
easier way: |
7069331db
|
23 |
int cn_add_callback(struct cb_id *id, char *name, void (*callback) (struct cn_msg *, struct netlink_skb_parms *)); |
7672d0b54
|
24 25 26 27 28 29 30 |
void cn_netlink_send(struct cn_msg *msg, u32 __group, int gfp_mask); struct cb_id { __u32 idx; __u32 val; }; |
41144ca3d
|
31 32 33 34 |
idx and val are unique identifiers which must be registered in the connector.h header for in-kernel usage. void (*callback) (void *) is a 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 |
7672d0b54
|
35 36 37 38 |
be dereferenced to struct cn_msg *. struct cn_msg { |
41144ca3d
|
39 |
struct cb_id id; |
7672d0b54
|
40 41 42 43 44 45 46 47 48 49 50 |
__u32 seq; __u32 ack; __u32 len; /* Length of the following data */ __u8 data[0]; }; /*****************************************/ Connector interfaces. /*****************************************/ |
7069331db
|
51 |
int cn_add_callback(struct cb_id *id, char *name, void (*callback) (struct cn_msg *, struct netlink_skb_parms *)); |
7672d0b54
|
52 |
|
41144ca3d
|
53 |
Registers new callback with connector core. |
7672d0b54
|
54 |
|
41144ca3d
|
55 56 57 |
struct cb_id *id - unique connector's user identifier. It must be registered in connector.h for legal in-kernel users. char *name - connector's callback symbolic name. |
7069331db
|
58 59 |
void (*callback) (struct cn..) - connector's callback. cn_msg and the sender's credentials |
7672d0b54
|
60 |
|
41144ca3d
|
61 |
|
7672d0b54
|
62 |
void cn_del_callback(struct cb_id *id); |
41144ca3d
|
63 64 65 |
Unregisters new callback with connector core. struct cb_id *id - unique connector's user identifier. |
7672d0b54
|
66 |
|
7672d0b54
|
67 |
|
b191ba0d5
|
68 |
int cn_netlink_send(struct cn_msg *msg, u32 __groups, int gfp_mask); |
7672d0b54
|
69 |
|
41144ca3d
|
70 71 72 |
Sends message to the specified groups. It can be safely called from softirq context, but may silently fail under strong memory pressure. If there are no listeners for given group -ESRCH can be returned. |
7672d0b54
|
73 |
|
41144ca3d
|
74 75 |
struct cn_msg * - message header(with attached data). u32 __group - destination group. |
7672d0b54
|
76 77 78 79 80 81 |
If __group is zero, then appropriate group will be searched through all registered connector users, and message will be delivered to the group which was created for user with the same ID as in msg. If __group is not zero, then message will be delivered to the specified group. |
41144ca3d
|
82 |
int gfp_mask - GFP mask. |
7672d0b54
|
83 |
|
41144ca3d
|
84 |
Note: When registering new callback user, connector core assigns |
a33f32244
|
85 |
netlink group to the user which is equal to its id.idx. |
7672d0b54
|
86 87 88 89 |
/*****************************************/ Protocol description. /*****************************************/ |
41144ca3d
|
90 91 |
The current framework offers a transport layer with fixed headers. The recommended protocol which uses such a header is as following: |
7672d0b54
|
92 93 |
msg->seq and msg->ack are used to determine message genealogy. When |
41144ca3d
|
94 95 |
someone sends a message, they use a locally unique sequence and random acknowledge number. The sequence number may be copied into |
7672d0b54
|
96 |
nlmsghdr->nlmsg_seq too. |
41144ca3d
|
97 |
The sequence number is incremented with each message sent. |
7672d0b54
|
98 |
|
41144ca3d
|
99 100 101 |
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
|
102 |
|
41144ca3d
|
103 104 105 106 107 |
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 acknowledge is not equal to the acknowledge number in the original message + 1, then it is a new message. |
7672d0b54
|
108 |
|
41144ca3d
|
109 |
Obviously, the protocol header contains the above id. |
7672d0b54
|
110 |
|
41144ca3d
|
111 |
The connector allows event notification in the following form: kernel |
7672d0b54
|
112 |
driver or userspace process can ask connector to notify it when |
41144ca3d
|
113 114 115 |
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
|
116 |
|
41144ca3d
|
117 118 |
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
|
119 120 121 122 |
/*****************************************/ Reliability. /*****************************************/ |
41144ca3d
|
123 |
Netlink itself is not a reliable protocol. That means that messages can |
7672d0b54
|
124 |
be lost due to memory pressure or process' receiving queue overflowed, |
41144ca3d
|
125 126 127 |
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
|
128 129 130 131 |
/*****************************************/ Userspace usage. /*****************************************/ |
41144ca3d
|
132 |
|
eb0d60411
|
133 |
2.6.14 has a new netlink socket implementation, which by default does not |
41144ca3d
|
134 135 136 137 |
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 that group first. It can be achieved by the following pseudocode: |
eb0d60411
|
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR); l_local.nl_family = AF_NETLINK; l_local.nl_groups = 12345; l_local.nl_pid = 0; if (bind(s, (struct sockaddr *)&l_local, sizeof(struct sockaddr_nl)) == -1) { perror("bind"); close(s); return -1; } { int on = l_local.nl_groups; setsockopt(s, 270, 1, &on, sizeof(on)); } Where 270 above is SOL_NETLINK, and 1 is a NETLINK_ADD_MEMBERSHIP socket |
41144ca3d
|
157 158 |
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
|
159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
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 not use add/remove connector's group notifications, but as far as I know, 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. |