Skip to contentSkip to conversation

drivers: hv: kvp

PATCH Applied K. Y. Srinivasan 5 participants 30 messages Aug 17, 2012 – Aug 22, 2012

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v3 cover drivers: hv: kvp

This patch set expands the KVP (Key Value Pair) functionality to implement the mechanism to GET/SET IP addresses in the guest. This functionality is used in Windows Server 2012 to implement VM replication functionality. The way IP configuration information is managed is distro specific. Based on the feedback I have gotten from Olaf, Greg, Steve, Ben and Mairus, I have chosen to seperate distro specific code from this patch-set. Most of the GET operation can be implemented in a way that is completely distro independent and I have implemented that as such and is included in this patch-set. Some of the attributes that can only be fetched in a distro dependent way as well the mechanism for configuring an interface (the SET operation) that is clearly distro specific is to be implemented via external scripts that will be invoked via the KVP code. We define here the interface to these scripts.

Adding support for IP injection resulted in some changes to the protocol between the user level daemon and the kernel driver. These changes have been implemented in way that would retain compatibility with older daemons. I would like to thank Olaf and Greg for pointing out the compatibility issue.

This version of the patch set addresses all of the comments that I have received to date from Olaf, Ben and others. Specifically, I have cleanedup all of the string manipulation code. Furthermore, I have also simplified the format of the interface configuration file that is generated by the KVP daemon and gotten rid of some constant strings that may not be relevant on all distros. I would like to thank Olaf and Ben for their detailed review.

Following are the changes from V2 of this patch set:

1. Addressed the indentation issue Greg noted in the patch:
0001-Drivers-hv-kvp-Support-the-new-IP-injection-messages.patch

2. Cleaned up the Reviewed-byi line based on input from Ben and Greg.

3. Since Ben had an identical patch for properly closing the open streams, I have not included that patch in this series.

4. Greg applied the first 3 patches of the V2 patch set. This version has the remaining patches.

K. Y. Srinivasan (14):
Drivers: hv: kvp: Support the new IP injection messages
Tools: hv: Prepare to expand kvp_get_ip_address() functionality
Tools: hv: Further refactor kvp_get_ip_address()
Tools: hv: Gather address family information
Tools: hv: Gather subnet information
Tools: hv: Represent the ipv6 mask using CIDR notation
Tools: hv: Gather ipv[4,6] gateway information
Tools: hv: Gather DNS information
Tools: hv: Gather DHCP information
Tools: hv: Implement the KVP verb - KVP_OP_SET_IP_INFO
Tools: hv: Rename the function kvp_get_ip_address()
Tools: hv: Implement the KVP verb - KVP_OP_GET_IP_INFO
Tools: hv: Get rid of some unused variables
Tools: hv: Correctly type string variables

 drivers/hv/hv_kvp.c      |  141 +++++++-
 include/linux/hyperv.h   |    2 +
 tools/hv/hv_kvp_daemon.c |  866 ++++++++++++++++++++++++++++++++++++++++++----
 3 files changed, 942 insertions(+), 67 deletions(-)
Signature · 1 line
1.7.4.1
#1 lore raw

PATCH v3 1/14 Drivers: hv: kvp: Support the new IP injection messages

Implement support for the new IP injection messages in the driver code.

---
 drivers/hv/hv_kvp.c |  141 ++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 135 insertions(+), 6 deletions(-)
drivers/hv/hv_kvp.c +135 -6
index eb4d073..d906050 100644
@@ -63,7 +63,7 @@ static int dm_reg_value;
 static void kvp_send_key(struct work_struct *dummy);
 
 
-static void kvp_respond_to_host(char *key, char *value, int error);
+static void kvp_respond_to_host(struct hv_kvp_msg *msg, int error);
 static void kvp_work_func(struct work_struct *dummy);
 static void kvp_register(int);
 
@@ -108,7 +108,7 @@ kvp_work_func(struct work_struct *dummy)
 	 * If the timer fires, the user-mode component has not responded;
 	 * process the pending transaction.
 	 */
-	kvp_respond_to_host("Unknown key", "Guest timed out", HV_E_FAIL);
+	kvp_respond_to_host(NULL, HV_E_FAIL);
 }
 
 static int kvp_handle_handshake(struct hv_kvp_msg *msg)
@@ -199,9 +199,118 @@ kvp_cn_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
 	 * to the host. But first, cancel the timeout.
 	 */
 	if (cancel_delayed_work_sync(&kvp_work))
-		kvp_respond_to_host(data->data.key, data->data.value, error);
+		kvp_respond_to_host(message, error);
 }
 
+
+static int process_ob_ipinfo(void *in_msg, void *out_msg, int op)
+{
+	struct hv_kvp_msg *in = in_msg;
+	struct hv_kvp_ip_msg *out = out_msg;
+	int len;
+
+	switch (op) {
+	case KVP_OP_GET_IP_INFO:
+		/*
+		 * Transform all parameters into utf16 encoding.
+		 */
+		len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.ip_addr,
+				strlen((char *)in->body.kvp_ip_val.ip_addr),
+				UTF16_HOST_ENDIAN,
+				(wchar_t *)out->kvp_ip_val.ip_addr,
+				MAX_IP_ADDR_SIZE);
+		if (len < 0)
+			return len;
+
+		len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.sub_net,
+				strlen((char *)in->body.kvp_ip_val.sub_net),
+				UTF16_HOST_ENDIAN,
+				(wchar_t *)out->kvp_ip_val.sub_net,
+				MAX_IP_ADDR_SIZE);
+		if (len < 0)
+			return len;
+
+		len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.gate_way,
+				strlen((char *)in->body.kvp_ip_val.gate_way),
+				UTF16_HOST_ENDIAN,
+				(wchar_t *)out->kvp_ip_val.gate_way,
+				MAX_GATEWAY_SIZE);
+		if (len < 0)
+			return len;
+
+		len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.dns_addr,
+				strlen((char *)in->body.kvp_ip_val.dns_addr),
+				UTF16_HOST_ENDIAN,
+				(wchar_t *)out->kvp_ip_val.dns_addr,
+				MAX_IP_ADDR_SIZE);
+		if (len < 0)
+			return len;
+
+		len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.adapter_id,
+				strlen((char *)in->body.kvp_ip_val.adapter_id),
+				UTF16_HOST_ENDIAN,
+				(wchar_t *)out->kvp_ip_val.adapter_id,
+				MAX_IP_ADDR_SIZE);
+		if (len < 0)
+			return len;
+
+		out->kvp_ip_val.dhcp_enabled =
+			in->body.kvp_ip_val.dhcp_enabled;
+	}
+
+	return 0;
+}
+
+static void process_ib_ipinfo(void *in_msg, void *out_msg, int op)
+{
+	struct hv_kvp_ip_msg *in = in_msg;
+	struct hv_kvp_msg *out = out_msg;
+
+	switch (op) {
+	case KVP_OP_SET_IP_INFO:
+		/*
+		 * Transform all parameters into utf8 encoding.
+		 */
+		utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.ip_addr,
+				MAX_IP_ADDR_SIZE,
+				UTF16_LITTLE_ENDIAN,
+				(__u8 *)out->body.kvp_ip_val.ip_addr,
+				MAX_IP_ADDR_SIZE);
+
+		utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.sub_net,
+				MAX_IP_ADDR_SIZE,
+				UTF16_LITTLE_ENDIAN,
+				(__u8 *)out->body.kvp_ip_val.sub_net,
+				MAX_IP_ADDR_SIZE);
+
+		utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.gate_way,
+				MAX_GATEWAY_SIZE,
+				UTF16_LITTLE_ENDIAN,
+				(__u8 *)out->body.kvp_ip_val.gate_way,
+				MAX_GATEWAY_SIZE);
+
+		utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.dns_addr,
+				MAX_IP_ADDR_SIZE,
+				UTF16_LITTLE_ENDIAN,
+				(__u8 *)out->body.kvp_ip_val.dns_addr,
+				MAX_IP_ADDR_SIZE);
+
+		out->body.kvp_ip_val.dhcp_enabled = in->kvp_ip_val.dhcp_enabled;
+
+	default:
+		utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.adapter_id,
+				MAX_ADAPTER_ID_SIZE,
+				UTF16_LITTLE_ENDIAN,
+				(__u8 *)out->body.kvp_ip_val.adapter_id,
+				MAX_ADAPTER_ID_SIZE);
+
+		out->body.kvp_ip_val.addr_family = in->kvp_ip_val.addr_family;
+	}
+}
+
+
+
+
 static void
 kvp_send_key(struct work_struct *dummy)
 {
@@ -237,6 +346,12 @@ kvp_send_key(struct work_struct *dummy)
 	 */
 
 	switch (message->kvp_hdr.operation) {
+	case KVP_OP_SET_IP_INFO:
+		process_ib_ipinfo(in_msg, message, KVP_OP_SET_IP_INFO);
+		break;
+	case KVP_OP_GET_IP_INFO:
+		process_ib_ipinfo(in_msg, message, KVP_OP_GET_IP_INFO);
+		break;
 	case KVP_OP_SET:
 		switch (in_msg->body.kvp_set.data.value_type) {
 		case REG_SZ:
@@ -313,17 +428,19 @@ kvp_send_key(struct work_struct *dummy)
  */
 
 static void
-kvp_respond_to_host(char *key, char *value, int error)
+kvp_respond_to_host(struct hv_kvp_msg *msg_to_host, int error)
 {
 	struct hv_kvp_msg  *kvp_msg;
 	struct hv_kvp_exchg_msg_value  *kvp_data;
 	char	*key_name;
+	char	*value;
 	struct icmsg_hdr *icmsghdrp;
 	int	keylen = 0;
 	int	valuelen = 0;
 	u32	buf_len;
 	struct vmbus_channel *channel;
 	u64	req_id;
+	int ret;
 
 	/*
 	 * If a transaction is not active; log and return.
@@ -376,6 +493,16 @@ kvp_respond_to_host(char *key, char *value, int error)
 			sizeof(struct icmsg_hdr)];
 
 	switch (kvp_transaction.kvp_msg->kvp_hdr.operation) {
+	case KVP_OP_GET_IP_INFO:
+		ret = process_ob_ipinfo(msg_to_host,
+				 (struct hv_kvp_ip_msg *)kvp_msg,
+				 KVP_OP_GET_IP_INFO);
+		if (ret < 0)
+			icmsghdrp->status = HV_E_FAIL;
+
+		goto response_done;
+	case KVP_OP_SET_IP_INFO:
+		goto response_done;
 	case KVP_OP_GET:
 		kvp_data = &kvp_msg->body.kvp_get.data;
 		goto copy_value;
@@ -389,7 +516,7 @@ kvp_respond_to_host(char *key, char *value, int error)
 	}
 
 	kvp_data = &kvp_msg->body.kvp_enum_data.data;
-	key_name = key;
+	key_name = msg_to_host->body.kvp_enum_data.data.key;
 
 	/*
 	 * The windows host expects the key/value pair to be encoded
@@ -403,6 +530,7 @@ kvp_respond_to_host(char *key, char *value, int error)
 	kvp_data->key_size = 2*(keylen + 1); /* utf16 encoding */
 
 copy_value:
+	value = msg_to_host->body.kvp_enum_data.data.value;
 	valuelen = utf8s_to_utf16s(value, strlen(value), UTF16_HOST_ENDIAN,
 				(wchar_t *) kvp_data->value,
 				(HV_KVP_EXCHANGE_MAX_VALUE_SIZE / 2) - 2);
@@ -455,7 +583,8 @@ void hv_kvp_onchannelcallback(void *context)
 		return;
 	}
 
-	vmbus_recvpacket(channel, recv_buffer, PAGE_SIZE, &recvlen, &requestid);
+	vmbus_recvpacket(channel, recv_buffer, PAGE_SIZE * 2, &recvlen,
+			 &requestid);
 
 	if (recvlen > 0) {
 		icmsghdrp = (struct icmsg_hdr *)&recv_buffer[
Signature · 1 line
1.7.4.1
#2 lore raw

PATCH v3 2/14 Tools: hv: Prepare to expand kvp_get_ip_address() functionality

kvp_get_ip_address() implemented the functionality to retrieve IP address info. Make this function more generic so that we could retrieve additional per-interface information.

---
 tools/hv/hv_kvp_daemon.c |  129 ++++++++++++++++++++++++++++++----------------
 1 files changed, 84 insertions(+), 45 deletions(-)
tools/hv/hv_kvp_daemon.c +84 -45
index 069e2b3..3af37f0 100644
@@ -492,7 +492,8 @@ done:
 }
 
 static int
-kvp_get_ip_address(int family, char *buffer, int length)
+kvp_get_ip_address(int family, char *if_name, int op,
+		 void  *out_buffer, int length)
 {
 	struct ifaddrs *ifap;
 	struct ifaddrs *curp;
@@ -502,10 +503,19 @@ kvp_get_ip_address(int family, char *buffer, int length)
 	const char *str;
 	char tmp[50];
 	int error = 0;
-
+	char *buffer;
+	struct hv_kvp_ipaddr_value *ip_buffer;
+
+	if (op == KVP_OP_ENUMERATE) {
+		buffer = out_buffer;
+	} else {
+		ip_buffer = out_buffer;
+		buffer = (char *)ip_buffer->ip_addr;
+		ip_buffer->addr_family = 0;
+	}
 	/*
 	 * On entry into this function, the buffer is capable of holding the
-	 * maximum key value (2048 bytes).
+	 * maximum key value.
 	 */
 
 	if (getifaddrs(&ifap)) {
@@ -515,58 +525,87 @@ kvp_get_ip_address(int family, char *buffer, int length)
 
 	curp = ifap;
 	while (curp != NULL) {
-		if ((curp->ifa_addr != NULL) &&
-		   (curp->ifa_addr->sa_family == family)) {
-			if (family == AF_INET) {
-				struct sockaddr_in *addr =
-				(struct sockaddr_in *) curp->ifa_addr;
-
-				str = inet_ntop(family, &addr->sin_addr,
-						tmp, 50);
-				if (str == NULL) {
-					strcpy(buffer, "inet_ntop failed\n");
-					error = 1;
-					goto getaddr_done;
-				}
-				if (offset == 0)
-					strcpy(buffer, tmp);
-				else
-					strcat(buffer, tmp);
-				strcat(buffer, ";");
+		if (curp->ifa_addr == NULL) {
+			curp = curp->ifa_next;
+			continue;
+		}
 
-				offset += strlen(str) + 1;
-				if ((length - offset) < (ipv4_len + 1))
-					goto getaddr_done;
+		if ((if_name != NULL) &&
+			(strncmp(curp->ifa_name, if_name, strlen(if_name)))) {
+			/*
+			 * We want info about a specific interface;
+			 * just continue.
+			 */
+			curp = curp->ifa_next;
+			continue;
+		}
 
-			} else {
+		/*
+		 * We only support two address families: AF_INET and AF_INET6.
+		 * If a family value of 0 is specified, we collect both
+		 * supported address families; if not we gather info on
+		 * the specified address family.
+		 */
+		if ((family != 0) && (curp->ifa_addr->sa_family != family)) {
+			curp = curp->ifa_next;
+			continue;
+		}
+		if ((curp->ifa_addr->sa_family != AF_INET) &&
+			(curp->ifa_addr->sa_family != AF_INET6)) {
+			curp = curp->ifa_next;
+			continue;
+		}
+
+		if ((curp->ifa_addr->sa_family == AF_INET) &&
+			((family == AF_INET) || (family == 0))) {
+			struct sockaddr_in *addr =
+			(struct sockaddr_in *) curp->ifa_addr;
+
+			str = inet_ntop(AF_INET, &addr->sin_addr, tmp, 50);
+			if (str == NULL) {
+				strcpy(buffer, "inet_ntop failed\n");
+				error = 1;
+				goto getaddr_done;
+			}
+			if (offset == 0)
+				strcpy(buffer, tmp);
+			else
+				strcat(buffer, tmp);
+			strcat(buffer, ";");
+
+			offset += strlen(str) + 1;
+			if ((length - offset) < (ipv4_len + 1))
+				goto getaddr_done;
+
+		} else if ((family == AF_INET6) || (family == 0)) {
 
 			/*
 			 * We only support AF_INET and AF_INET6
 			 * and the list of addresses is separated by a ";".
 			 */
-				struct sockaddr_in6 *addr =
+			struct sockaddr_in6 *addr =
 				(struct sockaddr_in6 *) curp->ifa_addr;
 
-				str = inet_ntop(family,
+			str = inet_ntop(AF_INET6,
 					&addr->sin6_addr.s6_addr,
 					tmp, 50);
-				if (str == NULL) {
-					strcpy(buffer, "inet_ntop failed\n");
-					error = 1;
-					goto getaddr_done;
-				}
-				if (offset == 0)
-					strcpy(buffer, tmp);
-				else
-					strcat(buffer, tmp);
-				strcat(buffer, ";");
-				offset += strlen(str) + 1;
-				if ((length - offset) < (ipv6_len + 1))
-					goto getaddr_done;
-
+			if (str == NULL) {
+				strcpy(buffer, "inet_ntop failed\n");
+				error = 1;
+				goto getaddr_done;
 			}
+			if (offset == 0)
+				strcpy(buffer, tmp);
+			else
+				strcat(buffer, tmp);
+			strcat(buffer, ";");
+			offset += strlen(str) + 1;
+			if ((length - offset) < (ipv6_len + 1))
+				goto getaddr_done;
 
 		}
+
+
 		curp = curp->ifa_next;
 	}
 
@@ -811,13 +850,13 @@ int main(void)
 			strcpy(key_value, lic_version);
 			break;
 		case NetworkAddressIPv4:
-			kvp_get_ip_address(AF_INET, key_value,
-					HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
+			kvp_get_ip_address(AF_INET, NULL, KVP_OP_ENUMERATE,
+				key_value, HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
 			strcpy(key_name, "NetworkAddressIPv4");
 			break;
 		case NetworkAddressIPv6:
-			kvp_get_ip_address(AF_INET6, key_value,
-					HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
+			kvp_get_ip_address(AF_INET6, NULL, KVP_OP_ENUMERATE,
+				key_value, HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
 			strcpy(key_name, "NetworkAddressIPv6");
 			break;
 		case OSBuildNumber:
Signature · 1 line
1.7.4.1
#3 lore raw

PATCH v3 3/14 Tools: hv: Further refactor kvp_get_ip_address()

In reply to K. Y. Srinivasan

In preparation for making kvp_get_ip_address() more generic, factor out the code for handling IP addresses.

---
 tools/hv/hv_kvp_daemon.c |   94 ++++++++++++++++++++-------------------------
 1 files changed, 42 insertions(+), 52 deletions(-)
tools/hv/hv_kvp_daemon.c +42 -52
index 3af37f0..3dc989f 100644
@@ -491,17 +491,50 @@ done:
 	return;
 }
 
+static int kvp_process_ip_address(void *addrp,
+				int family, char *buffer,
+				int length,  int *offset)
+{
+	struct sockaddr_in *addr;
+	struct sockaddr_in6 *addr6;
+	int addr_length;
+	char tmp[50];
+	const char *str;
+
+	if (family == AF_INET) {
+		addr = (struct sockaddr_in *)addrp;
+		str = inet_ntop(family, &addr->sin_addr, tmp, 50);
+		addr_length = INET_ADDRSTRLEN;
+	} else {
+		addr6 = (struct sockaddr_in6 *)addrp;
+		str = inet_ntop(family, &addr6->sin6_addr.s6_addr, tmp, 50);
+		addr_length = INET6_ADDRSTRLEN;
+	}
+
+	if ((length - *offset) < addr_length + 1)
+		return 1;
+	if (str == NULL) {
+		strcpy(buffer, "inet_ntop failed\n");
+		return 1;
+	}
+	if (*offset == 0)
+		strcpy(buffer, tmp);
+	else
+		strcat(buffer, tmp);
+	strcat(buffer, ";");
+
+	*offset += strlen(str) + 1;
+	return 0;
+}
+
 static int
 kvp_get_ip_address(int family, char *if_name, int op,
 		 void  *out_buffer, int length)
 {
 	struct ifaddrs *ifap;
 	struct ifaddrs *curp;
-	int ipv4_len = strlen("255.255.255.255") + 1;
-	int ipv6_len = strlen("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")+1;
 	int offset = 0;
 	const char *str;
-	char tmp[50];
 	int error = 0;
 	char *buffer;
 	struct hv_kvp_ipaddr_value *ip_buffer;
@@ -556,55 +589,12 @@ kvp_get_ip_address(int family, char *if_name, int op,
 			continue;
 		}
 
-		if ((curp->ifa_addr->sa_family == AF_INET) &&
-			((family == AF_INET) || (family == 0))) {
-			struct sockaddr_in *addr =
-			(struct sockaddr_in *) curp->ifa_addr;
-
-			str = inet_ntop(AF_INET, &addr->sin_addr, tmp, 50);
-			if (str == NULL) {
-				strcpy(buffer, "inet_ntop failed\n");
-				error = 1;
-				goto getaddr_done;
-			}
-			if (offset == 0)
-				strcpy(buffer, tmp);
-			else
-				strcat(buffer, tmp);
-			strcat(buffer, ";");
-
-			offset += strlen(str) + 1;
-			if ((length - offset) < (ipv4_len + 1))
-				goto getaddr_done;
-
-		} else if ((family == AF_INET6) || (family == 0)) {
-
-			/*
-			 * We only support AF_INET and AF_INET6
-			 * and the list of addresses is separated by a ";".
-			 */
-			struct sockaddr_in6 *addr =
-				(struct sockaddr_in6 *) curp->ifa_addr;
-
-			str = inet_ntop(AF_INET6,
-					&addr->sin6_addr.s6_addr,
-					tmp, 50);
-			if (str == NULL) {
-				strcpy(buffer, "inet_ntop failed\n");
-				error = 1;
-				goto getaddr_done;
-			}
-			if (offset == 0)
-				strcpy(buffer, tmp);
-			else
-				strcat(buffer, tmp);
-			strcat(buffer, ";");
-			offset += strlen(str) + 1;
-			if ((length - offset) < (ipv6_len + 1))
-				goto getaddr_done;
-
-		}
-
+		error = kvp_process_ip_address(curp->ifa_addr,
+						curp->ifa_addr->sa_family,
+						buffer,
+						length, &offset);
+		if (error)
+			goto getaddr_done;
 
 		curp = curp->ifa_next;
 	}
Signature · 1 line
1.7.4.1
#4 lore raw

PATCH v3 4/14 Tools: hv: Gather address family information

In reply to K. Y. Srinivasan

Now, gather address family information for the specified interface.

---
 tools/hv/hv_kvp_daemon.c |   11 +++++++++++
 1 files changed, 11 insertions(+), 0 deletions(-)
tools/hv/hv_kvp_daemon.c +11 -0
index 3dc989f..13ae098 100644
@@ -589,6 +589,17 @@ kvp_get_ip_address(int family, char *if_name, int op,
 			continue;
 		}
 
+		if (op == KVP_OP_GET_IP_INFO) {
+			/*
+			 * Gather info other than the IP address.
+			 * IP address info will be gathered later.
+			 */
+			if (curp->ifa_addr->sa_family == AF_INET)
+				ip_buffer->addr_family |= ADDR_FAMILY_IPV4;
+			else
+				ip_buffer->addr_family |= ADDR_FAMILY_IPV6;
+		}
+
 		error = kvp_process_ip_address(curp->ifa_addr,
 						curp->ifa_addr->sa_family,
 						buffer,
Signature · 1 line
1.7.4.1
#5 lore raw

PATCH v3 5/14 Tools: hv: Gather subnet information

In reply to K. Y. Srinivasan

Now, gather sub-net information for the specified interface.

---
 tools/hv/hv_kvp_daemon.c |   31 +++++++++++++++++++++++++++++--
 1 files changed, 29 insertions(+), 2 deletions(-)
tools/hv/hv_kvp_daemon.c +29 -2
index 13ae098..de1724c 100644
@@ -534,6 +534,7 @@ kvp_get_ip_address(int family, char *if_name, int op,
 	struct ifaddrs *ifap;
 	struct ifaddrs *curp;
 	int offset = 0;
+	int sn_offset = 0;
 	const char *str;
 	int error = 0;
 	char *buffer;
@@ -594,12 +595,38 @@ kvp_get_ip_address(int family, char *if_name, int op,
 			 * Gather info other than the IP address.
 			 * IP address info will be gathered later.
 			 */
-			if (curp->ifa_addr->sa_family == AF_INET)
+			if (curp->ifa_addr->sa_family == AF_INET) {
 				ip_buffer->addr_family |= ADDR_FAMILY_IPV4;
-			else
+				/*
+				 * Get subnet info.
+				 */
+				error = kvp_process_ip_address(
+							     curp->ifa_netmask,
+							     AF_INET,
+							     (char *)
+							     ip_buffer->sub_net,
+							     length,
+							     &sn_offset);
+				if (error)
+					goto gather_ipaddr;
+			} else {
 				ip_buffer->addr_family |= ADDR_FAMILY_IPV6;
+				/*
+				 * Get subnet info.
+				 */
+				error = kvp_process_ip_address(
+							     curp->ifa_netmask,
+							     AF_INET6,
+							     (char *)
+							     ip_buffer->sub_net,
+							     length,
+							     &sn_offset);
+				if (error)
+					goto gather_ipaddr;
+			}
 		}
 
+gather_ipaddr:
 		error = kvp_process_ip_address(curp->ifa_addr,
 						curp->ifa_addr->sa_family,
 						buffer,
Signature · 1 line
1.7.4.1
#6 lore raw

PATCH v3 6/14 Tools: hv: Represent the ipv6 mask using CIDR notation

In reply to K. Y. Srinivasan

Transform ipv6 subnet information to CIDR notation.

---
 tools/hv/hv_kvp_daemon.c |   46 ++++++++++++++++++++++++++++++++++++----------
 1 files changed, 36 insertions(+), 10 deletions(-)
tools/hv/hv_kvp_daemon.c +36 -10
index de1724c..a63e83a 100644
@@ -491,6 +491,15 @@ done:
 	return;
 }
 
+static unsigned int hweight32(unsigned int *w)
+{
+	unsigned int res = *w - ((*w >> 1) & 0x55555555);
+	res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
+	res = (res + (res >> 4)) & 0x0F0F0F0F;
+	res = res + (res >> 8);
+	return (res + (res >> 16)) & 0x000000FF;
+}
+
 static int kvp_process_ip_address(void *addrp,
 				int family, char *buffer,
 				int length,  int *offset)
@@ -535,10 +544,15 @@ kvp_get_ip_address(int family, char *if_name, int op,
 	struct ifaddrs *curp;
 	int offset = 0;
 	int sn_offset = 0;
-	const char *str;
 	int error = 0;
 	char *buffer;
 	struct hv_kvp_ipaddr_value *ip_buffer;
+	char cidr_mask[5]; /* /xyz */
+	int weight;
+	int i;
+	unsigned int *w;
+	char *sn_str;
+	struct sockaddr_in6 *addr6;
 
 	if (op == KVP_OP_ENUMERATE) {
 		buffer = out_buffer;
@@ -611,18 +625,30 @@ kvp_get_ip_address(int family, char *if_name, int op,
 					goto gather_ipaddr;
 			} else {
 				ip_buffer->addr_family |= ADDR_FAMILY_IPV6;
+
 				/*
-				 * Get subnet info.
+				 * Get subnet info in CIDR format.
 				 */
-				error = kvp_process_ip_address(
-							     curp->ifa_netmask,
-							     AF_INET6,
-							     (char *)
-							     ip_buffer->sub_net,
-							     length,
-							     &sn_offset);
-				if (error)
+				weight = 0;
+				sn_str = (char *)ip_buffer->sub_net;
+				addr6 = (struct sockaddr_in6 *)
+					curp->ifa_netmask;
+				w = addr6->sin6_addr.s6_addr32;
+
+				for (i = 0; i < 4; i++)
+					weight += hweight32(&w[i]);
+
+				sprintf(cidr_mask, "/%d", weight);
+				if ((length - sn_offset) <
+					(strlen(cidr_mask) + 1))
 					goto gather_ipaddr;
+
+				if (sn_offset == 0)
+					strcpy(sn_str, cidr_mask);
+				else
+					strcat(sn_str, cidr_mask);
+				strcat((char *)ip_buffer->sub_net, ";");
+				sn_offset += strlen(sn_str) + 1;
 			}
 		}
 
Signature · 1 line
1.7.4.1
#7 lore raw

PATCH v3 7/14 Tools: hv: Gather ipv[4,6] gateway information

In reply to K. Y. Srinivasan

Gather information on the default gateways - ipv4/ipv6.

---
 tools/hv/hv_kvp_daemon.c |   68 ++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 68 insertions(+), 0 deletions(-)
tools/hv/hv_kvp_daemon.c +68 -0
index a63e83a..65d54c8 100644
@@ -491,6 +491,68 @@ done:
 	return;
 }
 
+static void kvp_process_ipconfig_file(char *cmd,
+					char *config_buf, int len,
+					int element_size, int offset)
+{
+	char buf[256];
+	char *p;
+	char *x;
+	FILE *file;
+
+	/*
+	 * First execute the command.
+	 */
+	file = popen(cmd, "r");
+	if (file == NULL)
+		return;
+
+	if (offset == 0)
+		memset(config_buf, 0, len);
+	while ((p = fgets(buf, sizeof(buf), file)) != NULL) {
+		if ((len - strlen(config_buf)) < (element_size + 1))
+			break;
+
+		x = strchr(p, '\n');
+		*x = '\0';
+		strcat(config_buf, p);
+		strcat(config_buf, ";");
+	}
+	pclose(file);
+}
+
+static void kvp_get_ipconfig_info(char *if_name,
+				 struct hv_kvp_ipaddr_value *buffer)
+{
+	char cmd[512];
+
+	/*
+	 * Get the address of default gateway (ipv4).
+	 */
+	sprintf(cmd, "%s %s", "ip route show dev", if_name);
+	strcat(cmd, " | awk '/default/ {print $3 }'");
+
+	/*
+	 * Execute the command to gather gateway info.
+	 */
+	kvp_process_ipconfig_file(cmd, (char *)buffer->gate_way,
+				(MAX_GATEWAY_SIZE * 2), INET_ADDRSTRLEN, 0);
+
+	/*
+	 * Get the address of default gateway (ipv6).
+	 */
+	sprintf(cmd, "%s %s", "ip -f inet6  route show dev", if_name);
+	strcat(cmd, " | awk '/default/ {print $3 }'");
+
+	/*
+	 * Execute the command to gather gateway info (ipv6).
+	 */
+	kvp_process_ipconfig_file(cmd, (char *)buffer->gate_way,
+				(MAX_GATEWAY_SIZE * 2), INET6_ADDRSTRLEN, 1);
+
+}
+
+
 static unsigned int hweight32(unsigned int *w)
 {
 	unsigned int res = *w - ((*w >> 1) & 0x55555555);
@@ -650,6 +712,12 @@ kvp_get_ip_address(int family, char *if_name, int op,
 				strcat((char *)ip_buffer->sub_net, ";");
 				sn_offset += strlen(sn_str) + 1;
 			}
+
+			/*
+			 * Collect other ip related configuration info.
+			 */
+
+			kvp_get_ipconfig_info(if_name, ip_buffer);
 		}
 
 gather_ipaddr:
Signature · 1 line
1.7.4.1
#8 lore raw

PATCH v3 8/14 Tools: hv: Gather DNS information

In reply to K. Y. Srinivasan

Now, gather DNS information. Invoke an external script (that can be distro dependent) to gather the DNS information.

---
 tools/hv/hv_kvp_daemon.c |   23 +++++++++++++++++++++++
 1 files changed, 23 insertions(+), 0 deletions(-)
tools/hv/hv_kvp_daemon.c +23 -0
index 65d54c8..2f8e0b9 100644
@@ -550,6 +550,29 @@ static void kvp_get_ipconfig_info(char *if_name,
 	kvp_process_ipconfig_file(cmd, (char *)buffer->gate_way,
 				(MAX_GATEWAY_SIZE * 2), INET6_ADDRSTRLEN, 1);
 
+
+	/*
+	 * Gather the DNS  state.
+	 * Since there is no standard way to get this information
+	 * across various distributions of interest; we just invoke
+	 * an external script that needs to be ported across distros
+	 * of interest.
+	 *
+	 * Following is the expected format of the information from the script:
+	 *
+	 * ipaddr1 (nameserver1)
+	 * ipaddr2 (nameserver2)
+	 * .
+	 * .
+	 */
+
+	sprintf(cmd, "%s",  "hv_get_dns_info");
+
+	/*
+	 * Execute the command to gather DNS info.
+	 */
+	kvp_process_ipconfig_file(cmd, (char *)buffer->dns_addr,
+				(MAX_IP_ADDR_SIZE * 2), INET_ADDRSTRLEN, 0);
 }
 
 
Signature · 1 line
1.7.4.1
#9 lore raw

Re: [PATCH V3 08/14] Tools: hv: Gather DNS information

Quoted 2 lines · K. Y. Srinivasan

Now, gather DNS information. Invoke an external script (that can be distro dependent) to gather the DNS information.

Where is that script? Why a script, this really isn't in a standard method for all Linux distros? If not, why not work to do that like the freedesktop.org people have done for other system configuration items?

greg k-h

#10 lore raw

Re: [PATCH V3 08/14] Tools: hv: Gather DNS information

Quoted 7 lines · Greg KH
Quoted 2 lines · K. Y. Srinivasan

Now, gather DNS information. Invoke an external script (that can be distro dependent) to gather the DNS information.

Where is that script? Why a script, this really isn't in a standard method for all Linux distros? If not, why not work to do that like the freedesktop.org people have done for other system configuration items?

And please, don't include <[email protected]> in your patches anymore, it just constantly bounces as it's no longer valid (and hasn't been for over 6 months).

It's really annoying.

greg k-h

#11 lore raw

RE: [PATCH V3 08/14] Tools: hv: Gather DNS information

In reply to Greg KH
Quoted 11 lines

-----Original Message-----
From: Greg KH [mailto:[email protected]]
Sent: Friday, August 17, 2012 12:51 PM
To: KY Srinivasan
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH V3 08/14] Tools: hv: Gather DNS information

Quoted 7 lines
Quoted 2 lines · K. Y. Srinivasan

Now, gather DNS information. Invoke an external script (that can be distro dependent) to gather the DNS information.

Where is that script? Why a script, this really isn't in a standard method for all Linux distros? If not, why not work to do that like the freedesktop.org people have done for other system configuration items?

We debated this issue on this mailing list for a long time. The consensus was that we would invoke external "distro" specific scripts for both configuring the interface as well as gathering information that could not be gathered in a distro independent fashion. That is what I have implemented here. This code specifies the interface to this external script. If these external scripts are not there, it is not fatal - on the SET side, the interface will not be configured and on the get side some attributes that are to be gathered via these external scripts will not be collected.

Regards,

K. Y

Applied
#12 lore raw

Re: [PATCH V3 08/14] Tools: hv: Gather DNS information

Quoted 8 lines · KY Srinivasan
Quoted 7 lines · K. Y. Srinivasan
Quoted 2 lines

Now, gather DNS information. Invoke an external script (that can be distro dependent) to gather the DNS information.

Where is that script? Why a script, this really isn't in a standard method for all Linux distros? If not, why not work to do that like the freedesktop.org people have done for other system configuration items?

Quoted 7 lines

We debated this issue on this mailing list for a long time. The consensus was that we would invoke external "distro" specific scripts for both configuring the interface as well as gathering information that could not be gathered in a distro independent fashion. That is what I have implemented here. This code specifies the interface to this external script. If these external scripts are not there, it is not fatal - on the SET side, the interface will not be configured and on the get side some attributes that are to be gathered via these external scripts will not be collected.

I've applied the first 7 patches here, feel free to submit the rest when you answer the other questions I asked as well.

greg k-h

#13 lore raw

RE: [PATCH V3 08/14] Tools: hv: Gather DNS information

Quoted 27 lines

-----Original Message-----
From: Greg KH [mailto:[email protected]]
Sent: Friday, August 17, 2012 1:17 PM
To: KY Srinivasan
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH V3 08/14] Tools: hv: Gather DNS information

Quoted 9 lines · KY Srinivasan
Quoted 7 lines · K. Y. Srinivasan
Quoted 2 lines

Now, gather DNS information. Invoke an external script (that can be distro dependent) to gather the DNS information.

Where is that script? Why a script, this really isn't in a standard method for all Linux distros? If not, why not work to do that like the freedesktop.org people have done for other system configuration items?

We debated this issue on this mailing list for a long time. The consensus was

that

Quoted 1 line

we would invoke external "distro" specific scripts for both configuring the

interface

Quoted 1 line

as well as gathering information that could not be gathered in a distro

independent

Quoted 1 line

fashion. That is what I have implemented here. This code specifies the interface

to this

Quoted 8 lines
Quoted 1 line

external script. If these external scripts are not there, it is not fatal - on the SET

side,

Quoted 1 line

the interface will not be configured and on the get side some attributes that are

to be

Quoted 1 line

gathered via these external scripts will not be collected.

I've applied the first 7 patches here, feel free to submit the rest when you answer the other questions I asked as well.

Greg,

I have tried to answer your questions in other emails I have sent out on this thread. To summarize, the current implementation (with external scripts) does not preclude using Network Manager where it makes sense (in the external scripts).

If you think it will be useful to have example scripts, I could send the scripts I have used to test this code as example code. The reason I did not send the scripts is because (a) not having these scripts is not fatal to this code and
(b) Given that this is a distro specific script, I did not want code checked into the kernel tree that was very distro specific.

Let me know if I have addressed your concerns and if I can re-send the remaining 7 patches.

Regards,

K. Y

#14 lore raw

Re: [PATCH V3 08/14] Tools: hv: Gather DNS information

In reply to Greg KH
Quoted 7 lines · Greg KH
Quoted 2 lines · K. Y. Srinivasan

Now, gather DNS information. Invoke an external script (that can be distro dependent) to gather the DNS information.

Where is that script? Why a script, this really isn't in a standard method for all Linux distros? If not, why not work to do that like the freedesktop.org people have done for other system configuration items?

I suggested using the Network Manager D-Bus API. That obviously isn't going to work everywhere, but it seems to cover the same simple cases that this hypervisor-guest interface allows for.

Ben.

Signature · 3 lines
Ben Hutchings
We get into the habit of living before acquiring the habit of thinking.
                                                              - Albert Camus
#15 lore raw

Re: [PATCH V3 08/14] Tools: hv: Gather DNS information

Quoted 5 lines · Ben Hutchings
Quoted 4 lines · Greg KH
Quoted 2 lines · K. Y. Srinivasan

Now, gather DNS information. Invoke an external script (that can be distro dependent) to gather the DNS information.

Quoted 7 lines
Quoted 3 lines

Where is that script? Why a script, this really isn't in a standard method for all Linux distros? If not, why not work to do that like the freedesktop.org people have done for other system configuration items?

I suggested using the Network Manager D-Bus API. That obviously isn't going to work everywhere, but it seems to cover the same simple cases that this hypervisor-guest interface allows for.

That's a great start, as I think all of the distros have now pretty much standardized on this format, right?

greg k-h

#16 lore raw

Re: [PATCH V3 08/14] Tools: hv: Gather DNS information

Quoted 10 lines · Greg KH
Quoted 9 lines · Ben Hutchings
Quoted 7 lines · Greg KH
Quoted 2 lines · K. Y. Srinivasan

Now, gather DNS information. Invoke an external script (that can be distro dependent) to gather the DNS information.

Where is that script? Why a script, this really isn't in a standard method for all Linux distros? If not, why not work to do that like the freedesktop.org people have done for other system configuration items?

Quoted 6 lines
Quoted 3 lines

I suggested using the Network Manager D-Bus API. That obviously isn't going to work everywhere, but it seems to cover the same simple cases that this hypervisor-guest interface allows for.

That's a great start, as I think all of the distros have now pretty much standardized on this format, right?

I expect it to be available on most current distributions, but not necessarily enabled by default. Debian only installs NM if you choose the 'desktop' task at install time, so text-mode virtal servers wouldn't have it. KY may also be concerned about older distributions that never had NM.

Ben.

Signature · 3 lines
Ben Hutchings
We get into the habit of living before acquiring the habit of thinking.
                                                              - Albert Camus
#17 lore raw

RE: [PATCH V3 08/14] Tools: hv: Gather DNS information

In reply to Greg KH
Quoted 20 lines

-----Original Message-----
From: Greg KH [mailto:[email protected]]
Sent: Friday, August 17, 2012 1:39 PM
To: Ben Hutchings
Cc: KY Srinivasan; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH V3 08/14] Tools: hv: Gather DNS information

Quoted 9 lines · Ben Hutchings
Quoted 7 lines · Greg KH
Quoted 2 lines · K. Y. Srinivasan

Now, gather DNS information. Invoke an external script (that can be distro dependent) to gather the DNS information.

Where is that script? Why a script, this really isn't in a standard method for all Linux distros? If not, why not work to do that like the freedesktop.org people have done for other system configuration items?

Quoted 6 lines
Quoted 3 lines

I suggested using the Network Manager D-Bus API. That obviously isn't going to work everywhere, but it seems to cover the same simple cases that this hypervisor-guest interface allows for.

That's a great start, as I think all of the distros have now pretty much standardized on this format, right?

Based on Ben's suggestion I was looking at using NM D-Bus API. Subsequently, talking to Olaf and Mairus, I chose to go with the external script approach since each Distro could pretty much decide how the functionality would be implemented on a specific Distro including for instance by using the NM D-Bus API in the script. I think the approach of having an external script while not precluding anything makes the KVP code more generic and more widely applicable without sacrificing anything.

Regards,

K. Y

#18 lore raw

RE: [PATCH V3 08/14] Tools: hv: Gather DNS information

In reply to Ben Hutchings
Quoted 15 lines

-----Original Message-----
From: Ben Hutchings [mailto:[email protected]]
Sent: Friday, August 17, 2012 1:32 PM
To: Greg KH
Cc: KY Srinivasan; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH V3 08/14] Tools: hv: Gather DNS information

Quoted 4 lines · Greg KH
Quoted 2 lines · K. Y. Srinivasan

Now, gather DNS information. Invoke an external script (that can be distro dependent) to gather the DNS information.

Quoted 7 lines
Quoted 3 lines

Where is that script? Why a script, this really isn't in a standard method for all Linux distros? If not, why not work to do that like the freedesktop.org people have done for other system configuration items?

I suggested using the Network Manager D-Bus API. That obviously isn't going to work everywhere, but it seems to cover the same simple cases that this hypervisor-guest interface allows for.

The scheme Olaf proposed, which is what I have implemented here does not preclude using the D-BUS API in the external script.

Regards,

K. Y

#19 lore raw

Re: [PATCH V3 08/14] Tools: hv: Gather DNS information

In reply to Ben Hutchings
Quoted 5 lines · Ben Hutchings
Quoted 4 lines · Greg KH
Quoted 2 lines · K. Y. Srinivasan

Now, gather DNS information. Invoke an external script (that can be distro dependent) to gather the DNS information.

Quoted 7 lines
Quoted 3 lines

Where is that script? Why a script, this really isn't in a standard method for all Linux distros? If not, why not work to do that like the freedesktop.org people have done for other system configuration items?

I suggested using the Network Manager D-Bus API. That obviously isn't going to work everywhere, but it seems to cover the same simple cases that this hypervisor-guest interface allows for.

If its not going to work everywhere how can it be the solution?

The script in turn can do whatever it takes, which includes NM. However NM (in its current state) shouldnt be thrown at everyone and everything.

Olaf

#20 lore raw

Re: [PATCH V3 08/14] Tools: hv: Gather DNS information

Quoted 11 lines · Olaf Hering
Quoted 9 lines · Ben Hutchings
Quoted 7 lines · Greg KH
Quoted 2 lines · K. Y. Srinivasan

Now, gather DNS information. Invoke an external script (that can be distro dependent) to gather the DNS information.

Where is that script? Why a script, this really isn't in a standard method for all Linux distros? If not, why not work to do that like the freedesktop.org people have done for other system configuration items?

Quoted 5 lines
Quoted 3 lines

I suggested using the Network Manager D-Bus API. That obviously isn't going to work everywhere, but it seems to cover the same simple cases that this hypervisor-guest interface allows for.

If its not going to work everywhere how can it be the solution?

You are never going to have a 'works everywhere' solution for Linux guests.

How are these distribution-specific integration scripts going to get installed in the guesta, anyway?

Ben.

Quoted 2 lines

The script in turn can do whatever it takes, which includes NM. However NM (in its current state) shouldnt be thrown at everyone and everything.

Signature · 3 lines
Ben Hutchings
We get into the habit of living before acquiring the habit of thinking.
                                                              - Albert Camus
#21 lore raw

Re: [PATCH V3 08/14] Tools: hv: Gather DNS information

Quoted 2 lines · Ben Hutchings

You are never going to have a 'works everywhere' solution for Linux guests.

The hook is the solution.

Quoted 2 lines

How are these distribution-specific integration scripts going to get installed in the guesta, anyway?

The packager of the daemon will write the script, perhaps based on a template included in tools/hv/script_xyz.sh?

Olaf

#22 lore raw

Re: [PATCH V3 08/14] Tools: hv: Gather DNS information

Quoted 5 lines · Olaf Hering
Quoted 2 lines · Ben Hutchings

You are never going to have a 'works everywhere' solution for Linux guests.

Quoted 7 lines

The hook is the solution.

Quoted 2 lines

How are these distribution-specific integration scripts going to get installed in the guesta, anyway?

The packager of the daemon will write the script, perhaps based on a template included in tools/hv/script_xyz.sh?

This seems a reasonable approach and one with precident. We use a similar scheme for how to handle 'make install' in the kernel letting the distro provide /sbin/installkernel so as to avoid having distro smarts for every distro in the kernel source.

It might be nice to share some examples if you have them, but it feels a little grubby to have them in the kernel tree.

-apw

#23 lore raw

RE: [PATCH V3 08/14] Tools: hv: Gather DNS information

In reply to Greg KH
Quoted 11 lines

-----Original Message-----
From: Greg KH [mailto:[email protected]]
Sent: Friday, August 17, 2012 12:51 PM
To: KY Srinivasan
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH V3 08/14] Tools: hv: Gather DNS information

Quoted 5 lines
Quoted 2 lines · K. Y. Srinivasan

Now, gather DNS information. Invoke an external script (that can be distro dependent) to gather the DNS information.

Where is that script?

If you think it is useful, I could send you the scripts that I have used to test this functionality as example scripts.

Quoted 3 lines

Why a script, this really isn't in a standard
method for all Linux distros? If not, why not work to do that like the freedesktop.org people have done for other system configuration items?

When discussing this while preparing the patches it was clear that there
really is no single way to obtain the network information, if you are
using network manager you need to use dbus, basic debian networking as
on a server you need to look in /etc/resolv.conf, if you have dnsmasq
installed you need to look in a third place. On the SLES side that I am somewhat familiar with, if I recall correctly, none of the appliance images have Network Manager installed. The consensus was that we should avoid pulling a vast pile of distro smarts into the source which lead to the hook API proposed in these patches.

The approach chosen here does not preclude using Network Manager in the external scripts if a particular distro chooses to go that route. The current implementation supports maximum flexibility without sacrificing anything. While the current scripts I have do not use NM APIs, we could work on external scripts using DBUS APIs.

If it is ok with you, I could send you the remaining 7 patches along with the scripts I am currently using to test this code. These scripts should be viewed just example code.

Regards,

K. Y

#24 lore raw

PATCH v3 9/14 Tools: hv: Gather DHCP information

In reply to K. Y. Srinivasan

Collect information on dhcp setting for the specified interface. We invoke an external script to get this information.

---
 tools/hv/hv_kvp_daemon.c |   31 +++++++++++++++++++++++++++++++
 1 files changed, 31 insertions(+), 0 deletions(-)
tools/hv/hv_kvp_daemon.c +31 -0
index 2f8e0b9..8a18819 100644
@@ -525,6 +525,9 @@ static void kvp_get_ipconfig_info(char *if_name,
 				 struct hv_kvp_ipaddr_value *buffer)
 {
 	char cmd[512];
+	char dhcp_info[128];
+	char *p;
+	FILE *file;
 
 	/*
 	 * Get the address of default gateway (ipv4).
@@ -573,6 +576,34 @@ static void kvp_get_ipconfig_info(char *if_name,
 	 */
 	kvp_process_ipconfig_file(cmd, (char *)buffer->dns_addr,
 				(MAX_IP_ADDR_SIZE * 2), INET_ADDRSTRLEN, 0);
+
+	/*
+	 * Gather the DHCP state.
+	 * We will gather this state by invoking an external script.
+	 * The parameter to the script is the interface name.
+	 * Here is the expected output:
+	 *
+	 * Enabled: DHCP enabled.
+	 */
+
+	sprintf(cmd, "%s %s", "hv_get_dhcp_info", if_name);
+
+	file = popen(cmd, "r");
+	if (file == NULL)
+		return;
+
+	p = fgets(dhcp_info, sizeof(dhcp_info), file);
+	if (p == NULL) {
+		pclose(file);
+		return;
+	}
+
+	if (!strncmp(p, "Enabled", 7))
+		buffer->dhcp_enabled = 1;
+	else
+		buffer->dhcp_enabled = 0;
+
+	pclose(file);
 }
 
 
Signature · 1 line
1.7.4.1
#25 lore raw

PATCH v3 10/14 Tools: hv: Implement the KVP verb - KVP_OP_SET_IP_INFO

In reply to K. Y. Srinivasan

Implement the KVP verb - KVP_OP_SET_IP_INFO. This operation configures the specified interface based on the given configuration. Since configuring an interface is very distro specific, we invoke an external script to configure the interface.

---
 include/linux/hyperv.h   |    2 +
 tools/hv/hv_kvp_daemon.c |  443 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 445 insertions(+), 0 deletions(-)
include/linux/hyperv.h +2 -0
index b587c44..588d8f2 100644
@@ -191,6 +191,8 @@ enum hv_kvp_exchg_pool {
 #define HV_ERROR_NOT_SUPPORTED		0x80070032
 #define HV_ERROR_MACHINE_LOCKED		0x800704F7
 #define HV_ERROR_DEVICE_NOT_CONNECTED	0x8007048F
+#define HV_INVALIDARG			0x80070057
+#define HV_GUID_NOTFOUND		0x80041002
 
 #define ADDR_FAMILY_NONE	0x00
 #define ADDR_FAMILY_IPV4	0x01
tools/hv/hv_kvp_daemon.c +443 -0

491 lines not on this page · raw message

Signature · 1 line
1.7.4.1
#26 lore raw

PATCH v3 11/14 Tools: hv: Rename the function kvp_get_ip_address()

In reply to K. Y. Srinivasan

Rename the function kvp_get_ip_address() to better reflect the functionality being implemented.

---
 tools/hv/hv_kvp_daemon.c |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)
tools/hv/hv_kvp_daemon.c +3 -3
index 59669a8..bb2a6c4 100644
@@ -766,7 +766,7 @@ static int kvp_process_ip_address(void *addrp,
 }
 
 static int
-kvp_get_ip_address(int family, char *if_name, int op,
+kvp_get_ip_info(int family, char *if_name, int op,
 		 void  *out_buffer, int length)
 {
 	struct ifaddrs *ifap;
@@ -1469,12 +1469,12 @@ int main(void)
 			strcpy(key_value, lic_version);
 			break;
 		case NetworkAddressIPv4:
-			kvp_get_ip_address(AF_INET, NULL, KVP_OP_ENUMERATE,
+			kvp_get_ip_info(AF_INET, NULL, KVP_OP_ENUMERATE,
 				key_value, HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
 			strcpy(key_name, "NetworkAddressIPv4");
 			break;
 		case NetworkAddressIPv6:
-			kvp_get_ip_address(AF_INET6, NULL, KVP_OP_ENUMERATE,
+			kvp_get_ip_info(AF_INET6, NULL, KVP_OP_ENUMERATE,
 				key_value, HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
 			strcpy(key_name, "NetworkAddressIPv6");
 			break;
Signature · 1 line
1.7.4.1
#27 lore raw

PATCH v3 12/14 Tools: hv: Implement the KVP verb - KVP_OP_GET_IP_INFO

In reply to K. Y. Srinivasan

Now implement the KVP verb - KVP_OP_GET_IP_INFO. This operation retrieves IP information for the specified interface.

---
 tools/hv/hv_kvp_daemon.c |   93 ++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 90 insertions(+), 3 deletions(-)
tools/hv/hv_kvp_daemon.c +90 -3
index bb2a6c4..e7db9ac 100644
@@ -604,6 +604,69 @@ static char *kvp_if_name_to_mac(char *if_name)
 }
 
 
+/*
+ * Retrieve the interface name given tha MAC address.
+ */
+
+static char *kvp_mac_to_if_name(char *mac)
+{
+	DIR *dir;
+	struct dirent *entry;
+	FILE    *file;
+	char    *p, *q, *x;
+	char    *if_name = NULL;
+	char    buf[256];
+	char *kvp_net_dir = "/sys/class/net/";
+	char dev_id[256];
+	int i;
+
+	dir = opendir(kvp_net_dir);
+	if (dir == NULL)
+		return NULL;
+
+	snprintf(dev_id, sizeof(dev_id), kvp_net_dir);
+	q = dev_id + strlen(kvp_net_dir);
+
+	while ((entry = readdir(dir)) != NULL) {
+		/*
+		 * Set the state for the next pass.
+		 */
+		*q = '\0';
+
+		strcat(dev_id, entry->d_name);
+		strcat(dev_id, "/address");
+
+		file = fopen(dev_id, "r");
+		if (file == NULL)
+			continue;
+
+		p = fgets(buf, sizeof(buf), file);
+		if (p) {
+			x = strchr(p, '\n');
+			if (x)
+				*x = '\0';
+
+			for (i = 0; i < strlen(p); i++)
+				p[i] = toupper(p[i]);
+
+			if (!strcmp(p, mac)) {
+				/*
+				 * Found the MAC match; return the interface
+				 * name. The caller will free the memory.
+				 */
+				if_name = strdup(entry->d_name);
+				fclose(file);
+				break;
+			}
+		}
+		fclose(file);
+	}
+
+	closedir(dir);
+	return if_name;
+}
+
+
 static void kvp_process_ipconfig_file(char *cmd,
 					char *config_buf, int len,
 					int element_size, int offset)
@@ -750,10 +813,10 @@ static int kvp_process_ip_address(void *addrp,
 	}
 
 	if ((length - *offset) < addr_length + 1)
-		return 1;
+		return HV_E_FAIL;
 	if (str == NULL) {
 		strcpy(buffer, "inet_ntop failed\n");
-		return 1;
+		return HV_E_FAIL;
 	}
 	if (*offset == 0)
 		strcpy(buffer, tmp);
@@ -797,7 +860,7 @@ kvp_get_ip_info(int family, char *if_name, int op,
 
 	if (getifaddrs(&ifap)) {
 		strcpy(buffer, "getifaddrs failed\n");
-		return 1;
+		return HV_E_FAIL;
 	}
 
 	curp = ifap;
@@ -1387,6 +1450,30 @@ int main(void)
 		}
 
 		switch (op) {
+		case KVP_OP_GET_IP_INFO:
+			kvp_ip_val = &hv_msg->body.kvp_ip_val;
+			if_name =
+			kvp_mac_to_if_name((char *)kvp_ip_val->adapter_id);
+
+			if (if_name == NULL) {
+				/*
+				 * We could not map the mac address to an
+				 * interface name; return error.
+				 */
+				hv_msg->error = HV_E_FAIL;
+				break;
+			}
+			error = kvp_get_ip_info(
+						0, if_name, KVP_OP_GET_IP_INFO,
+						kvp_ip_val,
+						(MAX_IP_ADDR_SIZE * 2));
+
+			if (error)
+				hv_msg->error = error;
+
+			free(if_name);
+			break;
+
 		case KVP_OP_SET_IP_INFO:
 			kvp_ip_val = &hv_msg->body.kvp_ip_val;
 			if_name = kvp_get_if_name(
Signature · 1 line
1.7.4.1
#28 lore raw

PATCH v3 13/14 Tools: hv: Get rid of some unused variables

In reply to K. Y. Srinivasan

Get rid of unused variables.

---
 tools/hv/hv_kvp_daemon.c |    3 +--
 1 files changed, 1 insertions(+), 2 deletions(-)
tools/hv/hv_kvp_daemon.c +1 -2
index e7db9ac..4a1428c 100644
@@ -211,7 +211,7 @@ static void kvp_update_mem_state(int pool)
 }
 static int kvp_file_init(void)
 {
-	int ret, fd;
+	int  fd;
 	FILE *filep;
 	size_t records_read;
 	__u8 *fname;
@@ -327,7 +327,6 @@ static int kvp_key_add_or_modify(int pool, __u8 *key, int key_size, __u8 *value,
 			int value_size)
 {
 	int i;
-	int j, k;
 	int num_records;
 	struct kvp_record *record;
 	int num_blocks;
Signature · 1 line
1.7.4.1
#29 lore raw

PATCH v3 14/14 Tools: hv: Correctly type string variables

In reply to K. Y. Srinivasan

Correctly type character strings.

---
 tools/hv/hv_kvp_daemon.c |    8 ++++----
 1 files changed, 4 insertions(+), 4 deletions(-)
tools/hv/hv_kvp_daemon.c +4 -4
index 4a1428c..c8e1013 100644
@@ -101,8 +101,8 @@ static struct utsname uts_buf;
 #define ENTRIES_PER_BLOCK 50
 
 struct kvp_record {
-	__u8 key[HV_KVP_EXCHANGE_MAX_KEY_SIZE];
-	__u8 value[HV_KVP_EXCHANGE_MAX_VALUE_SIZE];
+	char key[HV_KVP_EXCHANGE_MAX_KEY_SIZE];
+	char value[HV_KVP_EXCHANGE_MAX_VALUE_SIZE];
 };
 
 struct kvp_file_state {
@@ -110,7 +110,7 @@ struct kvp_file_state {
 	int num_blocks;
 	struct kvp_record *records;
 	int num_records;
-	__u8 fname[MAX_FILE_NAME];
+	char fname[MAX_FILE_NAME];
 };
 
 static struct kvp_file_state kvp_file_info[KVP_POOL_COUNT];
@@ -214,7 +214,7 @@ static int kvp_file_init(void)
 	int  fd;
 	FILE *filep;
 	size_t records_read;
-	__u8 *fname;
+	char *fname;
 	struct kvp_record *record;
 	struct kvp_record *readp;
 	int num_blocks;
Signature · 1 line
1.7.4.1