1*4882a593Smuzhiyun.. SPDX-License-Identifier: GPL-2.0 2*4882a593Smuzhiyun 3*4882a593Smuzhiyun================ 4*4882a593SmuzhiyunKernel Connector 5*4882a593Smuzhiyun================ 6*4882a593Smuzhiyun 7*4882a593SmuzhiyunKernel connector - new netlink based userspace <-> kernel space easy 8*4882a593Smuzhiyunto use communication module. 9*4882a593Smuzhiyun 10*4882a593SmuzhiyunThe Connector driver makes it easy to connect various agents using a 11*4882a593Smuzhiyunnetlink based network. One must register a callback and an identifier. 12*4882a593SmuzhiyunWhen the driver receives a special netlink message with the appropriate 13*4882a593Smuzhiyunidentifier, the appropriate callback will be called. 14*4882a593Smuzhiyun 15*4882a593SmuzhiyunFrom the userspace point of view it's quite straightforward: 16*4882a593Smuzhiyun 17*4882a593Smuzhiyun - socket(); 18*4882a593Smuzhiyun - bind(); 19*4882a593Smuzhiyun - send(); 20*4882a593Smuzhiyun - recv(); 21*4882a593Smuzhiyun 22*4882a593SmuzhiyunBut if kernelspace wants to use the full power of such connections, the 23*4882a593Smuzhiyundriver writer must create special sockets, must know about struct sk_buff 24*4882a593Smuzhiyunhandling, etc... The Connector driver allows any kernelspace agents to use 25*4882a593Smuzhiyunnetlink based networking for inter-process communication in a significantly 26*4882a593Smuzhiyuneasier way:: 27*4882a593Smuzhiyun 28*4882a593Smuzhiyun int cn_add_callback(struct cb_id *id, char *name, void (*callback) (struct cn_msg *, struct netlink_skb_parms *)); 29*4882a593Smuzhiyun void cn_netlink_send_mult(struct cn_msg *msg, u16 len, u32 portid, u32 __group, int gfp_mask); 30*4882a593Smuzhiyun void cn_netlink_send(struct cn_msg *msg, u32 portid, u32 __group, int gfp_mask); 31*4882a593Smuzhiyun 32*4882a593Smuzhiyun struct cb_id 33*4882a593Smuzhiyun { 34*4882a593Smuzhiyun __u32 idx; 35*4882a593Smuzhiyun __u32 val; 36*4882a593Smuzhiyun }; 37*4882a593Smuzhiyun 38*4882a593Smuzhiyunidx and val are unique identifiers which must be registered in the 39*4882a593Smuzhiyunconnector.h header for in-kernel usage. `void (*callback) (void *)` is a 40*4882a593Smuzhiyuncallback function which will be called when a message with above idx.val 41*4882a593Smuzhiyunis received by the connector core. The argument for that function must 42*4882a593Smuzhiyunbe dereferenced to `struct cn_msg *`:: 43*4882a593Smuzhiyun 44*4882a593Smuzhiyun struct cn_msg 45*4882a593Smuzhiyun { 46*4882a593Smuzhiyun struct cb_id id; 47*4882a593Smuzhiyun 48*4882a593Smuzhiyun __u32 seq; 49*4882a593Smuzhiyun __u32 ack; 50*4882a593Smuzhiyun 51*4882a593Smuzhiyun __u16 len; /* Length of the following data */ 52*4882a593Smuzhiyun __u16 flags; 53*4882a593Smuzhiyun __u8 data[0]; 54*4882a593Smuzhiyun }; 55*4882a593Smuzhiyun 56*4882a593SmuzhiyunConnector interfaces 57*4882a593Smuzhiyun==================== 58*4882a593Smuzhiyun 59*4882a593Smuzhiyun .. kernel-doc:: include/linux/connector.h 60*4882a593Smuzhiyun 61*4882a593Smuzhiyun Note: 62*4882a593Smuzhiyun When registering new callback user, connector core assigns 63*4882a593Smuzhiyun netlink group to the user which is equal to its id.idx. 64*4882a593Smuzhiyun 65*4882a593SmuzhiyunProtocol description 66*4882a593Smuzhiyun==================== 67*4882a593Smuzhiyun 68*4882a593SmuzhiyunThe current framework offers a transport layer with fixed headers. The 69*4882a593Smuzhiyunrecommended protocol which uses such a header is as following: 70*4882a593Smuzhiyun 71*4882a593Smuzhiyunmsg->seq and msg->ack are used to determine message genealogy. When 72*4882a593Smuzhiyunsomeone sends a message, they use a locally unique sequence and random 73*4882a593Smuzhiyunacknowledge number. The sequence number may be copied into 74*4882a593Smuzhiyunnlmsghdr->nlmsg_seq too. 75*4882a593Smuzhiyun 76*4882a593SmuzhiyunThe sequence number is incremented with each message sent. 77*4882a593Smuzhiyun 78*4882a593SmuzhiyunIf you expect a reply to the message, then the sequence number in the 79*4882a593Smuzhiyunreceived message MUST be the same as in the original message, and the 80*4882a593Smuzhiyunacknowledge number MUST be the same + 1. 81*4882a593Smuzhiyun 82*4882a593SmuzhiyunIf we receive a message and its sequence number is not equal to one we 83*4882a593Smuzhiyunare expecting, then it is a new message. If we receive a message and 84*4882a593Smuzhiyunits sequence number is the same as one we are expecting, but its 85*4882a593Smuzhiyunacknowledge is not equal to the sequence number in the original 86*4882a593Smuzhiyunmessage + 1, then it is a new message. 87*4882a593Smuzhiyun 88*4882a593SmuzhiyunObviously, the protocol header contains the above id. 89*4882a593Smuzhiyun 90*4882a593SmuzhiyunThe connector allows event notification in the following form: kernel 91*4882a593Smuzhiyundriver or userspace process can ask connector to notify it when 92*4882a593Smuzhiyunselected ids will be turned on or off (registered or unregistered its 93*4882a593Smuzhiyuncallback). It is done by sending a special command to the connector 94*4882a593Smuzhiyundriver (it also registers itself with id={-1, -1}). 95*4882a593Smuzhiyun 96*4882a593SmuzhiyunAs example of this usage can be found in the cn_test.c module which 97*4882a593Smuzhiyunuses the connector to request notification and to send messages. 98*4882a593Smuzhiyun 99*4882a593SmuzhiyunReliability 100*4882a593Smuzhiyun=========== 101*4882a593Smuzhiyun 102*4882a593SmuzhiyunNetlink itself is not a reliable protocol. That means that messages can 103*4882a593Smuzhiyunbe lost due to memory pressure or process' receiving queue overflowed, 104*4882a593Smuzhiyunso caller is warned that it must be prepared. That is why the struct 105*4882a593Smuzhiyuncn_msg [main connector's message header] contains u32 seq and u32 ack 106*4882a593Smuzhiyunfields. 107*4882a593Smuzhiyun 108*4882a593SmuzhiyunUserspace usage 109*4882a593Smuzhiyun=============== 110*4882a593Smuzhiyun 111*4882a593Smuzhiyun2.6.14 has a new netlink socket implementation, which by default does not 112*4882a593Smuzhiyunallow people to send data to netlink groups other than 1. 113*4882a593SmuzhiyunSo, if you wish to use a netlink socket (for example using connector) 114*4882a593Smuzhiyunwith a different group number, the userspace application must subscribe to 115*4882a593Smuzhiyunthat group first. It can be achieved by the following pseudocode:: 116*4882a593Smuzhiyun 117*4882a593Smuzhiyun s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR); 118*4882a593Smuzhiyun 119*4882a593Smuzhiyun l_local.nl_family = AF_NETLINK; 120*4882a593Smuzhiyun l_local.nl_groups = 12345; 121*4882a593Smuzhiyun l_local.nl_pid = 0; 122*4882a593Smuzhiyun 123*4882a593Smuzhiyun if (bind(s, (struct sockaddr *)&l_local, sizeof(struct sockaddr_nl)) == -1) { 124*4882a593Smuzhiyun perror("bind"); 125*4882a593Smuzhiyun close(s); 126*4882a593Smuzhiyun return -1; 127*4882a593Smuzhiyun } 128*4882a593Smuzhiyun 129*4882a593Smuzhiyun { 130*4882a593Smuzhiyun int on = l_local.nl_groups; 131*4882a593Smuzhiyun setsockopt(s, 270, 1, &on, sizeof(on)); 132*4882a593Smuzhiyun } 133*4882a593Smuzhiyun 134*4882a593SmuzhiyunWhere 270 above is SOL_NETLINK, and 1 is a NETLINK_ADD_MEMBERSHIP socket 135*4882a593Smuzhiyunoption. To drop a multicast subscription, one should call the above socket 136*4882a593Smuzhiyunoption with the NETLINK_DROP_MEMBERSHIP parameter which is defined as 0. 137*4882a593Smuzhiyun 138*4882a593Smuzhiyun2.6.14 netlink code only allows to select a group which is less or equal to 139*4882a593Smuzhiyunthe maximum group number, which is used at netlink_kernel_create() time. 140*4882a593SmuzhiyunIn case of connector it is CN_NETLINK_USERS + 0xf, so if you want to use 141*4882a593Smuzhiyungroup number 12345, you must increment CN_NETLINK_USERS to that number. 142*4882a593SmuzhiyunAdditional 0xf numbers are allocated to be used by non-in-kernel users. 143*4882a593Smuzhiyun 144*4882a593SmuzhiyunDue to this limitation, group 0xffffffff does not work now, so one can 145*4882a593Smuzhiyunnot use add/remove connector's group notifications, but as far as I know, 146*4882a593Smuzhiyunonly cn_test.c test module used it. 147*4882a593Smuzhiyun 148*4882a593SmuzhiyunSome work in netlink area is still being done, so things can be changed in 149*4882a593Smuzhiyun2.6.15 timeframe, if it will happen, documentation will be updated for that 150*4882a593Smuzhiyunkernel. 151*4882a593Smuzhiyun 152*4882a593SmuzhiyunCode samples 153*4882a593Smuzhiyun============ 154*4882a593Smuzhiyun 155*4882a593SmuzhiyunSample code for a connector test module and user space can be found 156*4882a593Smuzhiyunin samples/connector/. To build this code, enable CONFIG_CONNECTOR 157*4882a593Smuzhiyunand CONFIG_SAMPLES. 158