2 * An implementation of key value pair (KVP) functionality for Linux.
5 * Copyright (C) 2010, Novell, Inc.
6 * Author : K. Y. Srinivasan <ksrinivasan@novell.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 as published
10 * by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
15 * NON INFRINGEMENT. See the GNU General Public License for more
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include <sys/types.h>
26 #include <sys/socket.h>
28 #include <sys/utsname.h>
29 #include <linux/types.h>
35 #include <arpa/inet.h>
36 #include <linux/connector.h>
37 #include <linux/hyperv.h>
38 #include <linux/netlink.h>
46 * KVP protocol: The user mode component first registers with the
47 * the kernel component. Subsequently, the kernel component requests, data
48 * for the specified keys. In response to this message the user mode component
49 * fills in the value corresponding to the specified key. We overload the
50 * sequence field in the cn_msg header to define our KVP message types.
52 * We use this infrastructure for also supporting queries from user mode
53 * application for state that may be maintained in the KVP kernel component.
59 FullyQualifiedDomainName = 0,
60 IntegrationServicesVersion, /*This key is serviced in the kernel*/
71 static char kvp_send_buffer[4096];
72 static char kvp_recv_buffer[4096];
73 static struct sockaddr_nl addr;
75 static char *os_name = "";
76 static char *os_major = "";
77 static char *os_minor = "";
78 static char *processor_arch;
79 static char *os_build;
80 static char *lic_version;
81 static struct utsname uts_buf;
84 #define MAX_FILE_NAME 100
85 #define ENTRIES_PER_BLOCK 50
88 __u8 key[HV_KVP_EXCHANGE_MAX_KEY_SIZE];
89 __u8 value[HV_KVP_EXCHANGE_MAX_VALUE_SIZE];
92 struct kvp_file_state {
95 struct kvp_record *records;
97 __u8 fname[MAX_FILE_NAME];
100 static struct kvp_file_state kvp_file_info[KVP_POOL_COUNT];
102 static void kvp_acquire_lock(int pool)
104 struct flock fl = {F_WRLCK, SEEK_SET, 0, 0, 0};
107 if (fcntl(kvp_file_info[pool].fd, F_SETLKW, &fl) == -1) {
108 syslog(LOG_ERR, "Failed to acquire the lock pool: %d", pool);
113 static void kvp_release_lock(int pool)
115 struct flock fl = {F_UNLCK, SEEK_SET, 0, 0, 0};
118 if (fcntl(kvp_file_info[pool].fd, F_SETLK, &fl) == -1) {
120 syslog(LOG_ERR, "Failed to release the lock pool: %d", pool);
125 static void kvp_update_file(int pool)
128 size_t bytes_written;
131 * We are going to write our in-memory registry out to
132 * disk; acquire the lock first.
134 kvp_acquire_lock(pool);
136 filep = fopen(kvp_file_info[pool].fname, "w");
138 kvp_release_lock(pool);
139 syslog(LOG_ERR, "Failed to open file, pool: %d", pool);
143 bytes_written = fwrite(kvp_file_info[pool].records,
144 sizeof(struct kvp_record),
145 kvp_file_info[pool].num_records, filep);
148 kvp_release_lock(pool);
151 static int kvp_file_init(void)
157 struct kvp_record *record;
158 struct kvp_record *readp;
161 int alloc_unit = sizeof(struct kvp_record) * ENTRIES_PER_BLOCK;
163 if (access("/var/opt/hyperv", F_OK)) {
164 if (mkdir("/var/opt/hyperv", S_IRUSR | S_IWUSR | S_IROTH)) {
165 syslog(LOG_ERR, " Failed to create /var/opt/hyperv");
170 for (i = 0; i < KVP_POOL_COUNT; i++) {
171 fname = kvp_file_info[i].fname;
174 sprintf(fname, "/var/opt/hyperv/.kvp_pool_%d", i);
175 fd = open(fname, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IROTH);
181 filep = fopen(fname, "r");
185 record = malloc(alloc_unit * num_blocks);
186 if (record == NULL) {
190 while (!feof(filep)) {
191 readp = &record[records_read];
192 records_read += fread(readp, sizeof(struct kvp_record),
198 * We have more data to read.
201 record = realloc(record, alloc_unit *
203 if (record == NULL) {
211 kvp_file_info[i].fd = fd;
212 kvp_file_info[i].num_blocks = num_blocks;
213 kvp_file_info[i].records = record;
214 kvp_file_info[i].num_records = records_read;
222 static int kvp_key_delete(int pool, __u8 *key, int key_size)
226 int num_records = kvp_file_info[pool].num_records;
227 struct kvp_record *record = kvp_file_info[pool].records;
229 for (i = 0; i < num_records; i++) {
230 if (memcmp(key, record[i].key, key_size))
233 * Found a match; just move the remaining
236 if (i == num_records) {
237 kvp_file_info[pool].num_records--;
238 kvp_update_file(pool);
244 for (; k < num_records; k++) {
245 strcpy(record[j].key, record[k].key);
246 strcpy(record[j].value, record[k].value);
250 kvp_file_info[pool].num_records--;
251 kvp_update_file(pool);
257 static int kvp_key_add_or_modify(int pool, __u8 *key, int key_size, __u8 *value,
262 int num_records = kvp_file_info[pool].num_records;
263 struct kvp_record *record = kvp_file_info[pool].records;
264 int num_blocks = kvp_file_info[pool].num_blocks;
266 if ((key_size > HV_KVP_EXCHANGE_MAX_KEY_SIZE) ||
267 (value_size > HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
270 for (i = 0; i < num_records; i++) {
271 if (memcmp(key, record[i].key, key_size))
274 * Found a match; just update the value -
275 * this is the modify case.
277 memcpy(record[i].value, value, value_size);
278 kvp_update_file(pool);
283 * Need to add a new entry;
285 if (num_records == (ENTRIES_PER_BLOCK * num_blocks)) {
286 /* Need to allocate a larger array for reg entries. */
287 record = realloc(record, sizeof(struct kvp_record) *
288 ENTRIES_PER_BLOCK * (num_blocks + 1));
292 kvp_file_info[pool].num_blocks++;
295 memcpy(record[i].value, value, value_size);
296 memcpy(record[i].key, key, key_size);
297 kvp_file_info[pool].records = record;
298 kvp_file_info[pool].num_records++;
299 kvp_update_file(pool);
303 static int kvp_get_value(int pool, __u8 *key, int key_size, __u8 *value,
307 int num_records = kvp_file_info[pool].num_records;
308 struct kvp_record *record = kvp_file_info[pool].records;
310 if ((key_size > HV_KVP_EXCHANGE_MAX_KEY_SIZE) ||
311 (value_size > HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
314 for (i = 0; i < num_records; i++) {
315 if (memcmp(key, record[i].key, key_size))
318 * Found a match; just copy the value out.
320 memcpy(value, record[i].value, value_size);
327 void kvp_get_os_info(void)
333 os_build = uts_buf.release;
334 processor_arch = uts_buf.machine;
337 * The current windows host (win7) expects the build
338 * string to be of the form: x.y.z
339 * Strip additional information we may have.
341 p = strchr(os_build, '-');
345 file = fopen("/etc/SuSE-release", "r");
347 goto kvp_osinfo_found;
348 file = fopen("/etc/redhat-release", "r");
350 goto kvp_osinfo_found;
352 * Add code for other supported platforms.
356 * We don't have information about the os.
358 os_name = uts_buf.sysname;
362 /* up to three lines */
363 p = fgets(buf, sizeof(buf), file);
365 p = strchr(buf, '\n');
374 p = fgets(buf, sizeof(buf), file);
376 p = strchr(buf, '\n');
385 p = fgets(buf, sizeof(buf), file);
387 p = strchr(buf, '\n');
403 kvp_get_ip_address(int family, char *buffer, int length)
405 struct ifaddrs *ifap;
406 struct ifaddrs *curp;
407 int ipv4_len = strlen("255.255.255.255") + 1;
408 int ipv6_len = strlen("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")+1;
415 * On entry into this function, the buffer is capable of holding the
416 * maximum key value (2048 bytes).
419 if (getifaddrs(&ifap)) {
420 strcpy(buffer, "getifaddrs failed\n");
425 while (curp != NULL) {
426 if ((curp->ifa_addr != NULL) &&
427 (curp->ifa_addr->sa_family == family)) {
428 if (family == AF_INET) {
429 struct sockaddr_in *addr =
430 (struct sockaddr_in *) curp->ifa_addr;
432 str = inet_ntop(family, &addr->sin_addr,
435 strcpy(buffer, "inet_ntop failed\n");
445 offset += strlen(str) + 1;
446 if ((length - offset) < (ipv4_len + 1))
452 * We only support AF_INET and AF_INET6
453 * and the list of addresses is separated by a ";".
455 struct sockaddr_in6 *addr =
456 (struct sockaddr_in6 *) curp->ifa_addr;
458 str = inet_ntop(family,
459 &addr->sin6_addr.s6_addr,
462 strcpy(buffer, "inet_ntop failed\n");
471 offset += strlen(str) + 1;
472 if ((length - offset) < (ipv6_len + 1))
478 curp = curp->ifa_next;
488 kvp_get_domain_name(char *buffer, int length)
490 struct addrinfo hints, *info ;
493 gethostname(buffer, length);
494 memset(&hints, 0, sizeof(hints));
495 hints.ai_family = AF_INET; /*Get only ipv4 addrinfo. */
496 hints.ai_socktype = SOCK_STREAM;
497 hints.ai_flags = AI_CANONNAME;
499 error = getaddrinfo(buffer, NULL, &hints, &info);
501 strcpy(buffer, "getaddrinfo failed\n");
504 strcpy(buffer, info->ai_canonname);
510 netlink_send(int fd, struct cn_msg *msg)
512 struct nlmsghdr *nlh;
514 struct msghdr message;
518 size = NLMSG_SPACE(sizeof(struct cn_msg) + msg->len);
520 nlh = (struct nlmsghdr *)buffer;
522 nlh->nlmsg_pid = getpid();
523 nlh->nlmsg_type = NLMSG_DONE;
524 nlh->nlmsg_len = NLMSG_LENGTH(size - sizeof(*nlh));
525 nlh->nlmsg_flags = 0;
527 iov[0].iov_base = nlh;
528 iov[0].iov_len = sizeof(*nlh);
530 iov[1].iov_base = msg;
531 iov[1].iov_len = size;
533 memset(&message, 0, sizeof(message));
534 message.msg_name = &addr;
535 message.msg_namelen = sizeof(addr);
536 message.msg_iov = iov;
537 message.msg_iovlen = 2;
539 return sendmsg(fd, &message, 0);
544 int fd, len, sock_opt;
546 struct cn_msg *message;
548 struct nlmsghdr *incoming_msg;
549 struct cn_msg *incoming_cn_msg;
550 struct hv_kvp_msg *hv_msg;
556 openlog("KVP", 0, LOG_USER);
557 syslog(LOG_INFO, "KVP starting; pid is:%d", getpid());
559 * Retrieve OS release information.
563 if (kvp_file_init()) {
564 syslog(LOG_ERR, "Failed to initialize the pools");
568 fd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);
570 syslog(LOG_ERR, "netlink socket creation failed; error:%d", fd);
573 addr.nl_family = AF_NETLINK;
576 addr.nl_groups = CN_KVP_IDX;
579 error = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
581 syslog(LOG_ERR, "bind failed; error:%d", error);
585 sock_opt = addr.nl_groups;
586 setsockopt(fd, 270, 1, &sock_opt, sizeof(sock_opt));
588 * Register ourselves with the kernel.
590 message = (struct cn_msg *)kvp_send_buffer;
591 message->id.idx = CN_KVP_IDX;
592 message->id.val = CN_KVP_VAL;
594 hv_msg = (struct hv_kvp_msg *)message->data;
595 hv_msg->kvp_hdr.operation = KVP_OP_REGISTER;
597 message->len = sizeof(struct hv_kvp_msg);
599 len = netlink_send(fd, message);
601 syslog(LOG_ERR, "netlink_send failed; error:%d", len);
613 len = recv(fd, kvp_recv_buffer, sizeof(kvp_recv_buffer), 0);
616 syslog(LOG_ERR, "recv failed; error:%d", len);
621 incoming_msg = (struct nlmsghdr *)kvp_recv_buffer;
622 incoming_cn_msg = (struct cn_msg *)NLMSG_DATA(incoming_msg);
623 hv_msg = (struct hv_kvp_msg *)incoming_cn_msg->data;
625 switch (hv_msg->kvp_hdr.operation) {
626 case KVP_OP_REGISTER:
628 * Driver is registering with us; stash away the version
631 p = (char *)hv_msg->body.kvp_register.version;
632 lic_version = malloc(strlen(p) + 1);
634 strcpy(lic_version, p);
635 syslog(LOG_INFO, "KVP LIC Version: %s",
638 syslog(LOG_ERR, "malloc failed");
643 * The current protocol with the kernel component uses a
644 * NULL key name to pass an error condition.
645 * For the SET, GET and DELETE operations,
646 * use the existing protocol to pass back error.
650 if (kvp_key_add_or_modify(hv_msg->kvp_hdr.pool,
651 hv_msg->body.kvp_set.data.key,
652 hv_msg->body.kvp_set.data.key_size,
653 hv_msg->body.kvp_set.data.value,
654 hv_msg->body.kvp_set.data.value_size))
655 strcpy(hv_msg->body.kvp_set.data.key, "");
659 if (kvp_get_value(hv_msg->kvp_hdr.pool,
660 hv_msg->body.kvp_set.data.key,
661 hv_msg->body.kvp_set.data.key_size,
662 hv_msg->body.kvp_set.data.value,
663 hv_msg->body.kvp_set.data.value_size))
664 strcpy(hv_msg->body.kvp_set.data.key, "");
668 if (kvp_key_delete(hv_msg->kvp_hdr.pool,
669 hv_msg->body.kvp_delete.key,
670 hv_msg->body.kvp_delete.key_size))
671 strcpy(hv_msg->body.kvp_delete.key, "");
678 if (hv_msg->kvp_hdr.operation != KVP_OP_ENUMERATE)
681 hv_msg = (struct hv_kvp_msg *)incoming_cn_msg->data;
682 key_name = (char *)hv_msg->body.kvp_enum_data.data.key;
683 key_value = (char *)hv_msg->body.kvp_enum_data.data.value;
685 switch (hv_msg->body.kvp_enum_data.index) {
686 case FullyQualifiedDomainName:
687 kvp_get_domain_name(key_value,
688 HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
689 strcpy(key_name, "FullyQualifiedDomainName");
691 case IntegrationServicesVersion:
692 strcpy(key_name, "IntegrationServicesVersion");
693 strcpy(key_value, lic_version);
695 case NetworkAddressIPv4:
696 kvp_get_ip_address(AF_INET, key_value,
697 HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
698 strcpy(key_name, "NetworkAddressIPv4");
700 case NetworkAddressIPv6:
701 kvp_get_ip_address(AF_INET6, key_value,
702 HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
703 strcpy(key_name, "NetworkAddressIPv6");
706 strcpy(key_value, os_build);
707 strcpy(key_name, "OSBuildNumber");
710 strcpy(key_value, os_name);
711 strcpy(key_name, "OSName");
714 strcpy(key_value, os_major);
715 strcpy(key_name, "OSMajorVersion");
718 strcpy(key_value, os_minor);
719 strcpy(key_name, "OSMinorVersion");
722 strcpy(key_value, os_build);
723 strcpy(key_name, "OSVersion");
725 case ProcessorArchitecture:
726 strcpy(key_value, processor_arch);
727 strcpy(key_name, "ProcessorArchitecture");
730 strcpy(key_value, "Unknown Key");
732 * We use a null key name to terminate enumeration.
734 strcpy(key_name, "");
738 * Send the value back to the kernel. The response is
739 * already in the receive buffer. Update the cn_msg header to
740 * reflect the key value that has been added to the message
744 incoming_cn_msg->id.idx = CN_KVP_IDX;
745 incoming_cn_msg->id.val = CN_KVP_VAL;
746 incoming_cn_msg->ack = 0;
747 incoming_cn_msg->len = sizeof(struct hv_kvp_msg);
749 len = netlink_send(fd, incoming_cn_msg);
751 syslog(LOG_ERR, "net_link send failed; error:%d", len);