[Midnightbsd-cvs] src [11334] trunk/contrib/hyperv: add hyperv

laffer1 at midnightbsd.org laffer1 at midnightbsd.org
Wed Jul 4 21:32:01 EDT 2018


Revision: 11334
          http://svnweb.midnightbsd.org/src/?rev=11334
Author:   laffer1
Date:     2018-07-04 21:32:00 -0400 (Wed, 04 Jul 2018)
Log Message:
-----------
add hyperv

Added Paths:
-----------
    trunk/contrib/hyperv/
    trunk/contrib/hyperv/tools/
    trunk/contrib/hyperv/tools/hv_kvp_daemon.8
    trunk/contrib/hyperv/tools/hv_kvp_daemon.c
    trunk/contrib/hyperv/tools/hv_vss_daemon.8
    trunk/contrib/hyperv/tools/hv_vss_daemon.c
    trunk/contrib/hyperv/tools/scripts/
    trunk/contrib/hyperv/tools/scripts/hv_get_dhcp_info
    trunk/contrib/hyperv/tools/scripts/hv_get_dns_info
    trunk/contrib/hyperv/tools/scripts/hv_set_ifconfig
    trunk/contrib/hyperv/tools/scripts/hyperv_vfattach
    trunk/contrib/hyperv/tools/scripts/hyperv_vfup

Added: trunk/contrib/hyperv/tools/hv_kvp_daemon.8
===================================================================
--- trunk/contrib/hyperv/tools/hv_kvp_daemon.8	                        (rev 0)
+++ trunk/contrib/hyperv/tools/hv_kvp_daemon.8	2018-07-05 01:32:00 UTC (rev 11334)
@@ -0,0 +1,100 @@
+.\" Copyright (c) 2014 Microsoft Corp.
+.\" All rights reserved.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\"    notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\"    notice, this list of conditions and the following disclaimer in the
+.\"    documentation and/or other materials provided with the distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+.\" SUCH DAMAGE.
+.\"
+.\" $FreeBSD: stable/10/contrib/hyperv/tools/hv_kvp_daemon.8 274372 2014-11-11 05:55:37Z delphij $
+.Dd October 27, 2014
+.Dt HV_KVP_DAEMON 8
+.Os
+.Sh NAME
+.Nm hv_kvp_daemon
+.Nd Hyper-V Key Value Pair Daemon
+.Sh SYNOPSIS
+.Nm
+.Op Fl dn
+.Sh DESCRIPTION
+The
+.Nm
+daemon provides the ability to store, retrieve, modify and delete 
+Key Value pairs for
+.Fx
+guest partitions running on Hyper-V.
+.Pp
+Hyper-V allows administrators to store custom metadata in the form
+of Key Value pairs inside the
+.Fx
+guest partition.
+Administrators can
+use Windows Powershell scripts to add, read, modify and delete such
+Key Value pairs.
+.Pp
+The
+.Nm
+accepts Key Value pair management requests from the
+.Xr hv_utils 4
+driver and performs the actual metadata management on the file-system.
+.Pp
+The same daemon and driver combination is also used to set and get
+IP addresses from a
+.Fx
+guest. 
+.Pp
+The set functionality is particularly
+useful when the
+.Fx
+guest is assigned a static IP address and is failed
+over from one Hyper-V host to another.
+After failover, Hyper-V uses the set IP
+functionality to automatically
+update the
+.Fx
+guest's IP address to its original static value.
+.Pp
+On the other hand, the get IP functionality is used to update the guest IP
+address in the Hyper-V management console window.
+.Pp
+The options are as follows:
+.Bl -tag -width indent
+.It Fl d
+Run as regular process instead of a daemon for debugging purpose.
+.It Fl n
+Generate debugging output.
+.El
+.Sh SEE ALSO
+.Xr hv_vmbus 4 ,
+.Xr hv_utils 4 ,
+.Xr hv_netvsc 4 ,
+.Xr hv_storvsc 4 ,
+.Xr hv_ata_pci_disengage 4 ,
+.Xr hv_kvp 4
+.Sh HISTORY
+Support for Hyper-V in the form of ports was first released in September 2013.
+The daemon was developed through a joint effort between Citrix Inc., 
+Microsoft Corp. and Network Appliance Inc..
+.Sh AUTHORS
+.An -nosplit
+.Fx
+support for
+.Nm
+was first added by
+.An Microsoft BSD Integration Services Team Aq Mt bsdic at microsoft.com .


Property changes on: trunk/contrib/hyperv/tools/hv_kvp_daemon.8
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: trunk/contrib/hyperv/tools/hv_kvp_daemon.c
===================================================================
--- trunk/contrib/hyperv/tools/hv_kvp_daemon.c	                        (rev 0)
+++ trunk/contrib/hyperv/tools/hv_kvp_daemon.c	2018-07-05 01:32:00 UTC (rev 11334)
@@ -0,0 +1,1529 @@
+/*-
+ * Copyright (c) 2014 Microsoft Corp.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice unmodified, this list of conditions, and the following
+ *    disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/poll.h>
+#include <sys/utsname.h>
+#include <sys/stat.h>
+#include <sys/un.h>
+
+#include <arpa/inet.h>
+#include <ifaddrs.h>
+#include <netdb.h>
+
+#include <netinet/in.h>
+#include <net/ethernet.h>
+#include <net/if_dl.h>
+#include <net/if_types.h>
+
+#include <assert.h>
+
+#include <ctype.h>
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <poll.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <syslog.h>
+#include <unistd.h>
+#include <assert.h>
+
+#include "hv_kvp.h"
+#include "hv_utilreg.h"
+typedef uint8_t		__u8;
+typedef uint16_t	__u16;
+typedef uint32_t	__u32;
+typedef uint64_t	__u64;
+
+#define POOL_FILE_MODE	(S_IRUSR | S_IWUSR)
+#define POOL_DIR_MODE	(POOL_FILE_MODE | S_IXUSR)
+#define POOL_DIR	"/var/db/hyperv/pool"
+
+/*
+ * ENUM Data
+ */
+
+enum key_index {
+	FullyQualifiedDomainName = 0,
+	IntegrationServicesVersion, /*This key is serviced in the kernel*/
+	NetworkAddressIPv4,
+	NetworkAddressIPv6,
+	OSBuildNumber,
+	OSName,
+	OSMajorVersion,
+	OSMinorVersion,
+	OSVersion,
+	ProcessorArchitecture
+};
+
+
+enum {
+	IPADDR = 0,
+	NETMASK,
+	GATEWAY,
+	DNS
+};
+
+
+/* Global Variables */
+
+/*
+ * The structure for operation handlers.
+ */
+struct kvp_op_hdlr {
+	int	kvp_op_key;
+	void	(*kvp_op_init)(void);
+ 	int	(*kvp_op_exec)(struct hv_kvp_msg *kvp_op_msg, void *data);
+};
+
+static struct kvp_op_hdlr kvp_op_hdlrs[HV_KVP_OP_COUNT];
+
+/* OS information */
+
+static const char *os_name = "";
+static const char *os_major = "";
+static const char *os_minor = "";
+static const char *processor_arch;
+static const char *os_build;
+static const char *lic_version = "BSD Pre-Release version";
+static struct utsname uts_buf;
+
+/* Global flags */
+static int is_daemon = 1;
+static int is_debugging = 0;
+
+#define	KVP_LOG(priority, format, args...) do	{			\
+		if (is_debugging == 1) {				\
+			if (is_daemon == 1)				\
+				syslog(priority, format, ## args);	\
+			else						\
+				printf(format, ## args);		\
+		} else {						\
+			if (priority < LOG_DEBUG) {			\
+				if (is_daemon == 1)			\
+					syslog(priority, format, ## args);	\
+				else					\
+					printf(format, ## args);	\
+			}						\
+		}							\
+	} while(0)
+
+/*
+ * For KVP pool file
+ */
+
+#define MAX_FILE_NAME		100
+#define ENTRIES_PER_BLOCK	50
+
+struct kvp_record {
+	char	key[HV_KVP_EXCHANGE_MAX_KEY_SIZE];
+	char	value[HV_KVP_EXCHANGE_MAX_VALUE_SIZE];
+};
+
+struct kvp_pool {
+	int			pool_fd;
+	int			num_blocks;
+	struct kvp_record	*records;
+	int			num_records;
+	char			fname[MAX_FILE_NAME];
+};
+
+static struct kvp_pool kvp_pools[HV_KVP_POOL_COUNT];
+
+
+static void
+kvp_acquire_lock(int pool)
+{
+	struct flock fl = { 0, 0, 0, F_WRLCK, SEEK_SET, 0 };
+
+	fl.l_pid = getpid();
+
+	if (fcntl(kvp_pools[pool].pool_fd, F_SETLKW, &fl) == -1) {
+		KVP_LOG(LOG_ERR, "Failed to acquire the lock pool: %d", pool);
+		exit(EXIT_FAILURE);
+	}
+}
+
+
+static void
+kvp_release_lock(int pool)
+{
+	struct flock fl = { 0, 0, 0, F_UNLCK, SEEK_SET, 0 };
+
+	fl.l_pid = getpid();
+
+	if (fcntl(kvp_pools[pool].pool_fd, F_SETLK, &fl) == -1) {
+		perror("fcntl");
+		KVP_LOG(LOG_ERR, "Failed to release the lock pool: %d\n", pool);
+		exit(EXIT_FAILURE);
+	}
+}
+
+
+/*
+ * Write in-memory copy of KVP to pool files
+ */
+static void
+kvp_update_file(int pool)
+{
+	FILE *filep;
+	size_t bytes_written;
+
+	kvp_acquire_lock(pool);
+
+	filep = fopen(kvp_pools[pool].fname, "w");
+	if (!filep) {
+		kvp_release_lock(pool);
+		KVP_LOG(LOG_ERR, "Failed to open file, pool: %d\n", pool);
+		exit(EXIT_FAILURE);
+	}
+
+	bytes_written = fwrite(kvp_pools[pool].records,
+		sizeof(struct kvp_record),
+		kvp_pools[pool].num_records, filep);
+
+	if (ferror(filep) || fclose(filep)) {
+		kvp_release_lock(pool);
+		KVP_LOG(LOG_ERR, "Failed to write file, pool: %d\n", pool);
+		exit(EXIT_FAILURE);
+	}
+
+	kvp_release_lock(pool);
+}
+
+
+/*
+ * Read KVPs from pool files and store in memory
+ */
+static void
+kvp_update_mem_state(int pool)
+{
+	FILE *filep;
+	size_t records_read = 0;
+	struct kvp_record *record = kvp_pools[pool].records;
+	struct kvp_record *readp;
+	int num_blocks = kvp_pools[pool].num_blocks;
+	int alloc_unit = sizeof(struct kvp_record) * ENTRIES_PER_BLOCK;
+
+	kvp_acquire_lock(pool);
+
+	filep = fopen(kvp_pools[pool].fname, "r");
+	if (!filep) {
+		kvp_release_lock(pool);
+		KVP_LOG(LOG_ERR, "Failed to open file, pool: %d\n", pool);
+		exit(EXIT_FAILURE);
+	}
+	for ( ; ; )
+	{
+		readp = &record[records_read];
+		records_read += fread(readp, sizeof(struct kvp_record),
+			ENTRIES_PER_BLOCK * num_blocks,
+			filep);
+
+		if (ferror(filep)) {
+			KVP_LOG(LOG_ERR, "Failed to read file, pool: %d\n", pool);
+			exit(EXIT_FAILURE);
+		}
+
+		if (!feof(filep)) {
+			/*
+			 * Have more data to read. Expand the memory.
+			 */
+			num_blocks++;
+			record = realloc(record, alloc_unit * num_blocks);
+
+			if (record == NULL) {
+				KVP_LOG(LOG_ERR, "malloc failed\n");
+				exit(EXIT_FAILURE);
+			}
+			continue;
+		}
+		break;
+	}
+
+	kvp_pools[pool].num_blocks = num_blocks;
+	kvp_pools[pool].records = record;
+	kvp_pools[pool].num_records = records_read;
+
+	fclose(filep);
+	kvp_release_lock(pool);
+}
+
+
+static int
+kvp_file_init(void)
+{
+	int fd;
+	FILE *filep;
+	size_t records_read;
+	char *fname;
+	struct kvp_record *record;
+	struct kvp_record *readp;
+	int num_blocks;
+	int i;
+	int alloc_unit = sizeof(struct kvp_record) * ENTRIES_PER_BLOCK;
+
+	if (mkdir(POOL_DIR, POOL_DIR_MODE) < 0 &&
+	    (errno != EEXIST && errno != EISDIR)) {
+		KVP_LOG(LOG_ERR, " Failed to create /var/db/hyperv/pool\n");
+		exit(EXIT_FAILURE);
+	}
+	chmod(POOL_DIR, POOL_DIR_MODE); /* fix old mistake */
+
+	for (i = 0; i < HV_KVP_POOL_COUNT; i++)
+	{
+		fname = kvp_pools[i].fname;
+		records_read = 0;
+		num_blocks = 1;
+		snprintf(fname, MAX_FILE_NAME, "/var/db/hyperv/pool/.kvp_pool_%d", i);
+		fd = open(fname, O_RDWR | O_CREAT, POOL_FILE_MODE);
+
+		if (fd == -1) {
+			return (1);
+		}
+		fchmod(fd, POOL_FILE_MODE); /* fix old mistake */
+
+
+		filep = fopen(fname, "r");
+		if (!filep) {
+			close(fd);
+			return (1);
+		}
+
+		record = malloc(alloc_unit * num_blocks);
+		if (record == NULL) {
+			close(fd);
+			fclose(filep);
+			return (1);
+		}
+		for ( ; ; )
+		{
+			readp = &record[records_read];
+			records_read += fread(readp, sizeof(struct kvp_record),
+				ENTRIES_PER_BLOCK,
+				filep);
+
+			if (ferror(filep)) {
+				KVP_LOG(LOG_ERR, "Failed to read file, pool: %d\n",
+				    i);
+				exit(EXIT_FAILURE);
+			}
+
+			if (!feof(filep)) {
+				/*
+				 * More data to read.
+				 */
+				num_blocks++;
+				record = realloc(record, alloc_unit *
+					num_blocks);
+				if (record == NULL) {
+					close(fd);
+					fclose(filep);
+					return (1);
+				}
+				continue;
+			}
+			break;
+		}
+		kvp_pools[i].pool_fd = fd;
+		kvp_pools[i].num_blocks = num_blocks;
+		kvp_pools[i].records = record;
+		kvp_pools[i].num_records = records_read;
+		fclose(filep);
+	}
+
+	return (0);
+}
+
+
+static int
+kvp_key_delete(int pool, __u8 *key, int key_size)
+{
+	int i;
+	int j, k;
+	int num_records;
+	struct kvp_record *record;
+
+	KVP_LOG(LOG_DEBUG, "kvp_key_delete: pool =  %d, "
+	    "key = %s\n", pool, key);
+
+	/* Update in-memory state */
+	kvp_update_mem_state(pool);
+
+	num_records = kvp_pools[pool].num_records;
+	record = kvp_pools[pool].records;
+
+	for (i = 0; i < num_records; i++)
+	{
+		if (memcmp(key, record[i].key, key_size)) {
+			continue;
+		}
+
+		KVP_LOG(LOG_DEBUG, "Found delete key in pool %d.\n",
+		    pool);
+		/*
+		 * We found a match at the end; Just update the number of
+		 * entries and we are done.
+		 */
+		if (i == num_records) {
+			kvp_pools[pool].num_records--;
+			kvp_update_file(pool);
+			return (0);
+		}
+
+		/*
+		 * We found a match in the middle; Move the remaining
+		 * entries up.
+		 */
+		j = i;
+		k = j + 1;
+		for ( ; k < num_records; k++)
+		{
+			strcpy(record[j].key, record[k].key);
+			strcpy(record[j].value, record[k].value);
+			j++;
+		}
+		kvp_pools[pool].num_records--;
+		kvp_update_file(pool);
+		return (0);
+	}
+	KVP_LOG(LOG_DEBUG, "Not found delete key in pool %d.\n",
+	    pool);
+	return (1);
+}
+
+
+static int
+kvp_key_add_or_modify(int pool, __u8 *key, __u32 key_size, __u8 *value,
+    __u32 value_size)
+{
+	int i;
+	int num_records;
+	struct kvp_record *record;
+	int num_blocks;
+
+	KVP_LOG(LOG_DEBUG, "kvp_key_add_or_modify: pool =  %d, "
+	    "key = %s, value = %s\n,", pool, key, value);
+
+	if ((key_size > HV_KVP_EXCHANGE_MAX_KEY_SIZE) ||
+	    (value_size > HV_KVP_EXCHANGE_MAX_VALUE_SIZE)) {
+		KVP_LOG(LOG_ERR, "kvp_key_add_or_modify: returning 1\n");
+		return (1);
+	}
+
+	/* Update the in-memory state. */
+	kvp_update_mem_state(pool);
+
+	num_records = kvp_pools[pool].num_records;
+	record = kvp_pools[pool].records;
+	num_blocks = kvp_pools[pool].num_blocks;
+
+	for (i = 0; i < num_records; i++)
+	{
+		if (memcmp(key, record[i].key, key_size)) {
+			continue;
+		}
+
+		/*
+		 * Key exists. Just update the value and we are done.
+		 */
+		memcpy(record[i].value, value, value_size);
+		kvp_update_file(pool);
+		return (0);
+	}
+
+	/*
+	 * Key doesn't exist; Add a new KVP.
+	 */
+	if (num_records == (ENTRIES_PER_BLOCK * num_blocks)) {
+		/* Increase the size of the recodrd array. */
+		record = realloc(record, sizeof(struct kvp_record) *
+			ENTRIES_PER_BLOCK * (num_blocks + 1));
+
+		if (record == NULL) {
+			return (1);
+		}
+		kvp_pools[pool].num_blocks++;
+	}
+	memcpy(record[i].value, value, value_size);
+	memcpy(record[i].key, key, key_size);
+	kvp_pools[pool].records = record;
+	kvp_pools[pool].num_records++;
+	kvp_update_file(pool);
+	return (0);
+}
+
+
+static int
+kvp_get_value(int pool, __u8 *key, int key_size, __u8 *value,
+    int value_size)
+{
+	int i;
+	int num_records;
+	struct kvp_record *record;
+
+	KVP_LOG(LOG_DEBUG, "kvp_get_value: pool =  %d, key = %s\n,",
+	    pool, key);
+
+	if ((key_size > HV_KVP_EXCHANGE_MAX_KEY_SIZE) ||
+	    (value_size > HV_KVP_EXCHANGE_MAX_VALUE_SIZE)) {
+		return (1);
+	}
+
+	/* Update the in-memory state first. */
+	kvp_update_mem_state(pool);
+
+	num_records = kvp_pools[pool].num_records;
+	record = kvp_pools[pool].records;
+
+	for (i = 0; i < num_records; i++)
+	{
+		if (memcmp(key, record[i].key, key_size)) {
+			continue;
+		}
+
+		/* Found the key */
+		memcpy(value, record[i].value, value_size);
+		return (0);
+	}
+
+	return (1);
+}
+
+
+static int
+kvp_pool_enumerate(int pool, int idx, __u8 *key, int key_size,
+    __u8 *value, int value_size)
+{
+	struct kvp_record *record;
+
+	KVP_LOG(LOG_DEBUG, "kvp_pool_enumerate: pool = %d, index = %d\n,",
+	    pool, idx);
+
+	/* First update our in-memory state first. */
+	kvp_update_mem_state(pool);
+	record = kvp_pools[pool].records;
+
+	/* Index starts with 0 */
+	if (idx >= kvp_pools[pool].num_records) {
+		return (1);
+	}
+
+	memcpy(key, record[idx].key, key_size);
+	memcpy(value, record[idx].value, value_size);
+	return (0);
+}
+
+
+static void
+kvp_get_os_info(void)
+{
+	char *p;
+
+	uname(&uts_buf);
+	os_build = uts_buf.release;
+	os_name = uts_buf.sysname;
+	processor_arch = uts_buf.machine;
+
+	/*
+	 * Win7 host expects the build string to be of the form: x.y.z
+	 * Strip additional information we may have.
+	 */
+	p = strchr(os_build, '-');
+	if (p) {
+		*p = '\0';
+	}
+
+	/*
+	 * We don't have any other information about the FreeBSD os.
+	 */
+	return;
+}
+
+/*
+ * Given the interface name, return the MAC address.
+ */
+static char *
+kvp_if_name_to_mac(char *if_name)
+{
+	char *mac_addr = NULL;
+	struct ifaddrs *ifaddrs_ptr;
+	struct ifaddrs *head_ifaddrs_ptr;
+	struct sockaddr_dl *sdl;
+	int status;
+
+	status = getifaddrs(&ifaddrs_ptr);
+
+	if (status >= 0) {
+		head_ifaddrs_ptr = ifaddrs_ptr;
+		do {
+			sdl = (struct sockaddr_dl *)(uintptr_t)ifaddrs_ptr->ifa_addr;
+			if ((sdl->sdl_type == IFT_ETHER) &&
+			    (strcmp(ifaddrs_ptr->ifa_name, if_name) == 0)) {
+				mac_addr = strdup(ether_ntoa((struct ether_addr *)(LLADDR(sdl))));
+				break;
+			}
+		} while ((ifaddrs_ptr = ifaddrs_ptr->ifa_next) != NULL);
+		freeifaddrs(head_ifaddrs_ptr);
+	}
+
+	return (mac_addr);
+}
+
+
+/*
+ * Given the MAC address, return the interface name.
+ */
+static char *
+kvp_mac_to_if_name(char *mac)
+{
+	char *if_name = NULL;
+	struct ifaddrs *ifaddrs_ptr;
+	struct ifaddrs *head_ifaddrs_ptr;
+	struct sockaddr_dl *sdl;
+	int status;
+	char *buf_ptr, *p;
+
+	status = getifaddrs(&ifaddrs_ptr);
+
+	if (status >= 0) {
+		head_ifaddrs_ptr = ifaddrs_ptr;
+		do {
+			sdl = (struct sockaddr_dl *)(uintptr_t)ifaddrs_ptr->ifa_addr;
+			if (sdl->sdl_type == IFT_ETHER) {
+				buf_ptr = strdup(ether_ntoa((struct ether_addr *)(LLADDR(sdl))));
+				if (buf_ptr != NULL) {
+					for (p = buf_ptr; *p != '\0'; p++)
+						*p = toupper(*p);
+
+					if (strncmp(buf_ptr, mac, strlen(mac)) == 0) {
+						/* Caller will free the memory */
+						if_name = strdup(ifaddrs_ptr->ifa_name);
+						free(buf_ptr);
+						break;
+					} else
+						free(buf_ptr);
+				}
+			}
+		} while ((ifaddrs_ptr = ifaddrs_ptr->ifa_next) != NULL);
+		freeifaddrs(head_ifaddrs_ptr);
+	}
+	return (if_name);
+}
+
+
+static void
+kvp_process_ipconfig_file(char *cmd,
+    char *config_buf, size_t len,
+    size_t 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';
+		strlcat(config_buf, p, len);
+		strlcat(config_buf, ";", len);
+	}
+	pclose(file);
+}
+
+
+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;
+
+	/*
+	 * Retrieve the IPV4 address of default gateway.
+	 */
+	snprintf(cmd, sizeof(cmd), "netstat -rn | grep %s | awk '/default/ {print $2 }'", if_name);
+
+	/*
+	 * Execute the command to gather gateway IPV4 info.
+	 */
+	kvp_process_ipconfig_file(cmd, (char *)buffer->gate_way,
+	    (MAX_GATEWAY_SIZE * 2), INET_ADDRSTRLEN, 0);
+	/*
+	 * Retrieve the IPV6 address of default gateway.
+	 */
+	snprintf(cmd, sizeof(cmd), "netstat -rn inet6 | grep %s | awk '/default/ {print $2 }'", if_name);
+
+	/*
+	 * Execute the command to gather gateway IPV6 info.
+	 */
+	kvp_process_ipconfig_file(cmd, (char *)buffer->gate_way,
+	    (MAX_GATEWAY_SIZE * 2), INET6_ADDRSTRLEN, 1);
+	/*
+	 * we just invoke an external script to get the DNS info.
+	 *
+	 * Following is the expected format of the information from the script:
+	 *
+	 * ipaddr1 (nameserver1)
+	 * ipaddr2 (nameserver2)
+	 * .
+	 * .
+	 */
+	/* Scripts are stored in /usr/libexec/hyperv/ directory */
+	snprintf(cmd, sizeof(cmd), "%s", "sh /usr/libexec/hyperv/hv_get_dns_info");
+
+	/*
+	 * Execute the command to get DNS info.
+	 */
+	kvp_process_ipconfig_file(cmd, (char *)buffer->dns_addr,
+	    (MAX_IP_ADDR_SIZE * 2), INET_ADDRSTRLEN, 0);
+
+	/*
+	 * Invoke an external script to get the DHCP state info.
+	 * The parameter to the script is the interface name.
+	 * Here is the expected output:
+	 *
+	 * Enabled: DHCP enabled.
+	 */
+
+
+	snprintf(cmd, sizeof(cmd), "%s %s",
+	    "sh /usr/libexec/hyperv/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);
+}
+
+
+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)
+{
+	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 (EINVAL);
+	}
+	if (str == NULL) {
+		strlcpy(buffer, "inet_ntop failed\n", length);
+		return (errno);
+	}
+	if (*offset == 0) {
+		strlcpy(buffer, tmp, length);
+	} else{
+		strlcat(buffer, tmp, length);
+	}
+	strlcat(buffer, ";", length);
+
+	*offset += strlen(str) + 1;
+	return (0);
+}
+
+
+static int
+kvp_get_ip_info(int family, char *if_name, int op,
+    void *out_buffer, size_t length)
+{
+	struct ifaddrs *ifap;
+	struct ifaddrs *curp;
+	int offset = 0;
+	int sn_offset = 0;
+	int error = 0;
+	char *buffer;
+	size_t buffer_length;
+	struct hv_kvp_ipaddr_value *ip_buffer;
+	char cidr_mask[5];
+	int weight;
+	int i;
+	unsigned int *w = NULL;
+	char *sn_str;
+	size_t sn_str_length;
+	struct sockaddr_in6 *addr6;
+
+	if (op == HV_KVP_OP_ENUMERATE) {
+		buffer = out_buffer;
+		buffer_length = length;
+	} else {
+		ip_buffer = out_buffer;
+		buffer = (char *)ip_buffer->ip_addr;
+		buffer_length = sizeof(ip_buffer->ip_addr);
+		ip_buffer->addr_family = 0;
+	}
+
+	if (getifaddrs(&ifap)) {
+		strlcpy(buffer, "getifaddrs failed\n", buffer_length);
+		return (errno);
+	}
+
+	curp = ifap;
+	while (curp != NULL) {
+		if (curp->ifa_addr == NULL) {
+			curp = curp->ifa_next;
+			continue;
+		}
+
+		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;
+		}
+
+		/*
+		 * We support two address families: AF_INET and AF_INET6.
+		 * If family value is 0, we gather 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 (op == HV_KVP_OP_GET_IP_INFO) {
+			/*
+			 * Get the info other than the IP address.
+			 */
+			if (curp->ifa_addr->sa_family == AF_INET) {
+				ip_buffer->addr_family |= ADDR_FAMILY_IPV4;
+
+				/*
+				 * Get subnet info.
+				 */
+				error = kvp_process_ip_address(
+					curp->ifa_netmask,
+					AF_INET,
+					(char *)
+					ip_buffer->sub_net,
+					length,
+					&sn_offset);
+				if (error) {
+					goto kvp_get_ip_info_ipaddr;
+				}
+			} else {
+				ip_buffer->addr_family |= ADDR_FAMILY_IPV6;
+
+				/*
+				 * Get subnet info in CIDR format.
+				 */
+				weight = 0;
+				sn_str = (char *)ip_buffer->sub_net;
+				sn_str_length = sizeof(ip_buffer->sub_net);
+				addr6 = (struct sockaddr_in6 *)(uintptr_t)
+				    curp->ifa_netmask;
+				w = (unsigned int *)(uintptr_t)addr6->sin6_addr.s6_addr;
+
+				for (i = 0; i < 4; i++)
+				{
+					weight += hweight32(&w[i]);
+				}
+
+				snprintf(cidr_mask, sizeof(cidr_mask), "/%d", weight);
+				if ((length - sn_offset) <
+				    (strlen(cidr_mask) + 1)) {
+					goto kvp_get_ip_info_ipaddr;
+				}
+
+				if (sn_offset == 0) {
+					strlcpy(sn_str, cidr_mask, sn_str_length);
+				} else{
+					strlcat(sn_str, cidr_mask, sn_str_length);
+				}
+				strlcat((char *)ip_buffer->sub_net, ";", sn_str_length);
+				sn_offset += strlen(sn_str) + 1;
+			}
+
+			/*
+			 * Collect other ip configuration info.
+			 */
+			kvp_get_ipconfig_info(if_name, ip_buffer);
+		}
+
+kvp_get_ip_info_ipaddr:
+		error = kvp_process_ip_address(curp->ifa_addr,
+			curp->ifa_addr->sa_family,
+			buffer,
+			length, &offset);
+		if (error) {
+			goto kvp_get_ip_info_done;
+		}
+
+		curp = curp->ifa_next;
+	}
+
+kvp_get_ip_info_done:
+	freeifaddrs(ifap);
+	return (error);
+}
+
+
+static int
+kvp_write_file(FILE *f, const char *s1, const char *s2, const char *s3)
+{
+	int ret;
+
+	ret = fprintf(f, "%s%s%s%s\n", s1, s2, "=", s3);
+
+	if (ret < 0) {
+		return (EIO);
+	}
+
+	return (0);
+}
+
+
+static int
+kvp_set_ip_info(char *if_name, struct hv_kvp_ipaddr_value *new_val)
+{
+	int error = 0;
+	char if_file[128];
+	FILE *file;
+	char cmd[512];
+	char *mac_addr;
+
+	/*
+	 * FreeBSD - Configuration File
+	 */
+	snprintf(if_file, sizeof(if_file), "%s%s", "/var/db/hyperv",
+	    "hv_set_ip_data");
+	file = fopen(if_file, "w");
+
+	if (file == NULL) {
+		KVP_LOG(LOG_ERR, "FreeBSD Failed to open config file\n");
+		return (errno);
+	}
+
+	/*
+	 * Write out the MAC address.
+	 */
+
+	mac_addr = kvp_if_name_to_mac(if_name);
+	if (mac_addr == NULL) {
+		error = EINVAL;
+		goto kvp_set_ip_info_error;
+	}
+	/* MAC Address */
+	error = kvp_write_file(file, "HWADDR", "", mac_addr);
+	if (error) {
+		goto kvp_set_ip_info_error;
+	}
+
+	/* Interface Name  */
+	error = kvp_write_file(file, "IF_NAME", "", if_name);
+	if (error) {
+		goto kvp_set_ip_info_error;
+	}
+
+	/* IP Address  */
+	error = kvp_write_file(file, "IP_ADDR", "",
+	    (char *)new_val->ip_addr);
+	if (error) {
+		goto kvp_set_ip_info_error;
+	}
+
+	/* Subnet Mask */
+	error = kvp_write_file(file, "SUBNET", "",
+	    (char *)new_val->sub_net);
+	if (error) {
+		goto kvp_set_ip_info_error;
+	}
+
+
+	/* Gateway */
+	error = kvp_write_file(file, "GATEWAY", "",
+	    (char *)new_val->gate_way);
+	if (error) {
+		goto kvp_set_ip_info_error;
+	}
+
+	/* DNS */
+	error = kvp_write_file(file, "DNS", "", (char *)new_val->dns_addr);
+	if (error) {
+		goto kvp_set_ip_info_error;
+	}
+
+	/* DHCP */
+	if (new_val->dhcp_enabled) {
+		error = kvp_write_file(file, "DHCP", "", "1");
+	} else{
+		error = kvp_write_file(file, "DHCP", "", "0");
+	}
+
+	if (error) {
+		goto kvp_set_ip_info_error;
+	}
+
+	free(mac_addr);
+	fclose(file);
+
+	/*
+	 * Invoke the external script with the populated
+	 * configuration file.
+	 */
+
+	snprintf(cmd, sizeof(cmd), "%s %s",
+	    "sh /usr/libexec/hyperv/hv_set_ifconfig", if_file);
+	system(cmd);
+	return (0);
+
+kvp_set_ip_info_error:
+	KVP_LOG(LOG_ERR, "Failed to write config file\n");
+	free(mac_addr);
+	fclose(file);
+	return (error);
+}
+
+
+static int
+kvp_get_domain_name(char *buffer, int length)
+{
+	struct addrinfo hints, *info;
+	int error = 0;
+
+	gethostname(buffer, length);
+	memset(&hints, 0, sizeof(hints));
+	hints.ai_family = AF_INET;    /* Get only ipv4 addrinfo. */
+	hints.ai_socktype = SOCK_STREAM;
+	hints.ai_flags = AI_CANONNAME;
+
+	error = getaddrinfo(buffer, NULL, &hints, &info);
+	if (error != 0) {
+		strlcpy(buffer, "getaddrinfo failed\n", length);
+		return (error);
+	}
+	strlcpy(buffer, info->ai_canonname, length);
+	freeaddrinfo(info);
+	return (error);
+}
+
+
+static int
+kvp_op_getipinfo(struct hv_kvp_msg *op_msg, void *data __unused)
+{
+	struct hv_kvp_ipaddr_value *ip_val;
+	char *if_name;
+	int error = 0;
+
+	assert(op_msg != NULL);
+	KVP_LOG(LOG_DEBUG, "In kvp_op_getipinfo.\n");
+
+	ip_val = &op_msg->body.kvp_ip_val;
+	op_msg->hdr.error = HV_S_OK;
+
+	if_name = kvp_mac_to_if_name((char *)ip_val->adapter_id);
+
+	if (if_name == NULL) {
+		/* No interface found with the mac address. */
+		op_msg->hdr.error = HV_E_FAIL;
+		goto kvp_op_getipinfo_done;
+	}
+
+	error = kvp_get_ip_info(0, if_name,
+	    HV_KVP_OP_GET_IP_INFO, ip_val, (MAX_IP_ADDR_SIZE * 2));
+	if (error)
+		op_msg->hdr.error = HV_E_FAIL;
+	free(if_name);
+
+kvp_op_getipinfo_done:
+	return (error);
+}
+
+
+static int
+kvp_op_setipinfo(struct hv_kvp_msg *op_msg, void *data __unused)
+{
+	struct hv_kvp_ipaddr_value *ip_val;
+	char *if_name;
+	int error = 0;
+
+	assert(op_msg != NULL);
+	KVP_LOG(LOG_DEBUG, "In kvp_op_setipinfo.\n");
+
+	ip_val = &op_msg->body.kvp_ip_val;
+	op_msg->hdr.error = HV_S_OK;
+
+	if_name = (char *)ip_val->adapter_id;
+
+	if (if_name == NULL) {
+		/* No adapter provided. */
+		op_msg->hdr.error = HV_GUID_NOTFOUND;
+		goto kvp_op_setipinfo_done;
+	}
+
+	error = kvp_set_ip_info(if_name, ip_val);
+	if (error)
+		op_msg->hdr.error = HV_E_FAIL;
+kvp_op_setipinfo_done:
+	return (error);
+}
+
+
+static int
+kvp_op_setgetdel(struct hv_kvp_msg *op_msg, void *data)
+{
+	struct kvp_op_hdlr *op_hdlr = (struct kvp_op_hdlr *)data;
+	int error = 0;
+	int op_pool;
+
+	assert(op_msg != NULL);
+	assert(op_hdlr != NULL);
+
+	op_pool = op_msg->hdr.kvp_hdr.pool;
+	op_msg->hdr.error = HV_S_OK;
+
+	switch(op_hdlr->kvp_op_key) {
+	case HV_KVP_OP_SET:
+		if (op_pool == HV_KVP_POOL_AUTO) {
+			/* Auto Pool is not writeable from host side. */
+			error = 1;
+			KVP_LOG(LOG_ERR, "Ilegal to write to pool %d from host\n",
+			    op_pool);
+		} else {
+			error = kvp_key_add_or_modify(op_pool,
+			    op_msg->body.kvp_set.data.key,
+			    op_msg->body.kvp_set.data.key_size,
+			    op_msg->body.kvp_set.data.msg_value.value,
+			    op_msg->body.kvp_set.data.value_size);
+		}
+		break;
+
+	case HV_KVP_OP_GET:
+		error = kvp_get_value(op_pool,
+		    op_msg->body.kvp_get.data.key,
+		    op_msg->body.kvp_get.data.key_size,
+		    op_msg->body.kvp_get.data.msg_value.value,
+		    op_msg->body.kvp_get.data.value_size);
+		break;
+
+	case HV_KVP_OP_DELETE:
+		if (op_pool == HV_KVP_POOL_AUTO) {
+			/* Auto Pool is not writeable from host side. */
+			error = 1;
+			KVP_LOG(LOG_ERR, "Ilegal to change pool %d from host\n",
+			    op_pool);
+		} else {
+			error = kvp_key_delete(op_pool,
+			    op_msg->body.kvp_delete.key,
+			    op_msg->body.kvp_delete.key_size);
+		}
+		break;
+
+	default:
+		break;
+	}
+
+	if (error != 0)
+		op_msg->hdr.error = HV_S_CONT;
+	return(error);
+}
+
+
+static int
+kvp_op_enumerate(struct hv_kvp_msg *op_msg, void *data __unused)
+{
+	char *key_name, *key_value;
+	int error = 0;
+	int op_pool;
+	int op;
+
+	assert(op_msg != NULL);
+
+	op = op_msg->hdr.kvp_hdr.operation;
+	op_pool = op_msg->hdr.kvp_hdr.pool;
+	op_msg->hdr.error = HV_S_OK;
+
+	/*
+	 * If the pool is not HV_KVP_POOL_AUTO, read from the appropriate
+	 * pool and return the KVP according to the index requested.
+	 */
+	if (op_pool != HV_KVP_POOL_AUTO) {
+		if (kvp_pool_enumerate(op_pool,
+		    op_msg->body.kvp_enum_data.index,
+		    op_msg->body.kvp_enum_data.data.key,
+		    HV_KVP_EXCHANGE_MAX_KEY_SIZE,
+		    op_msg->body.kvp_enum_data.data.msg_value.value,
+		    HV_KVP_EXCHANGE_MAX_VALUE_SIZE)) {
+			op_msg->hdr.error = HV_S_CONT;
+			error = -1;
+		}
+		goto kvp_op_enumerate_done;
+	}
+
+	key_name = (char *)op_msg->body.kvp_enum_data.data.key;
+	key_value = (char *)op_msg->body.kvp_enum_data.data.msg_value.value;
+
+	switch (op_msg->body.kvp_enum_data.index)
+	{
+	case FullyQualifiedDomainName:
+		kvp_get_domain_name(key_value,
+		    HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
+		strcpy(key_name, "FullyQualifiedDomainName");
+		break;
+
+	case IntegrationServicesVersion:
+		strcpy(key_name, "IntegrationServicesVersion");
+		strlcpy(key_value, lic_version, HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
+		break;
+
+	case NetworkAddressIPv4:
+		kvp_get_ip_info(AF_INET, NULL, HV_KVP_OP_ENUMERATE,
+		    key_value, HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
+		strcpy(key_name, "NetworkAddressIPv4");
+		break;
+
+	case NetworkAddressIPv6:
+		kvp_get_ip_info(AF_INET6, NULL, HV_KVP_OP_ENUMERATE,
+		    key_value, HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
+		strcpy(key_name, "NetworkAddressIPv6");
+		break;
+
+	case OSBuildNumber:
+		strlcpy(key_value, os_build, HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
+		strcpy(key_name, "OSBuildNumber");
+		break;
+
+	case OSName:
+		strlcpy(key_value, os_name, HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
+		strcpy(key_name, "OSName");
+		break;
+
+	case OSMajorVersion:
+		strlcpy(key_value, os_major, HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
+		strcpy(key_name, "OSMajorVersion");
+		break;
+
+	case OSMinorVersion:
+		strlcpy(key_value, os_minor, HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
+		strcpy(key_name, "OSMinorVersion");
+		break;
+
+	case OSVersion:
+		strlcpy(key_value, os_build, HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
+		strcpy(key_name, "OSVersion");
+		break;
+
+	case ProcessorArchitecture:
+		strlcpy(key_value, processor_arch, HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
+		strcpy(key_name, "ProcessorArchitecture");
+		break;
+
+	default:
+#ifdef DEBUG
+		KVP_LOG(LOG_ERR, "Auto pool Index %d not found.\n",
+		    op_msg->body.kvp_enum_data.index);
+#endif
+		op_msg->hdr.error = HV_S_CONT;
+		error = -1;
+		break;
+	}
+
+kvp_op_enumerate_done:
+	if (error != 0)
+		op_msg->hdr.error = HV_S_CONT;
+	return(error);
+}
+
+
+/*
+ * Load handler, and call init routine if provided.
+ */
+static int
+kvp_op_load(int key, void (*init)(void),
+	    int (*exec)(struct hv_kvp_msg *, void *))
+{
+	int error = 0;
+
+	if (key < 0 || key >= HV_KVP_OP_COUNT) {
+		KVP_LOG(LOG_ERR, "Operation key out of supported range\n");
+		error = -1;
+		goto kvp_op_load_done;
+	}
+
+	kvp_op_hdlrs[key].kvp_op_key = key;
+	kvp_op_hdlrs[key].kvp_op_init = init;
+	kvp_op_hdlrs[key].kvp_op_exec = exec;
+
+	if (kvp_op_hdlrs[key].kvp_op_init != NULL)
+		kvp_op_hdlrs[key].kvp_op_init();
+
+kvp_op_load_done:
+	return(error);
+}
+
+
+/*
+ * Initialize the operation hanlders.
+ */
+static int
+kvp_ops_init(void)
+{
+	int i;
+
+	/* Set the initial values. */
+	for (i = 0; i < HV_KVP_OP_COUNT; i++) {
+		kvp_op_hdlrs[i].kvp_op_key = -1;
+		kvp_op_hdlrs[i].kvp_op_init = NULL;
+		kvp_op_hdlrs[i].kvp_op_exec = NULL;
+	}
+
+	return(kvp_op_load(HV_KVP_OP_GET, NULL, kvp_op_setgetdel) |
+	    kvp_op_load(HV_KVP_OP_SET, NULL, kvp_op_setgetdel) |
+	    kvp_op_load(HV_KVP_OP_DELETE, NULL, kvp_op_setgetdel) |
+	    kvp_op_load(HV_KVP_OP_ENUMERATE, kvp_get_os_info,
+	        kvp_op_enumerate) |
+	    kvp_op_load(HV_KVP_OP_GET_IP_INFO, NULL, kvp_op_getipinfo) |
+	    kvp_op_load(HV_KVP_OP_SET_IP_INFO, NULL, kvp_op_setipinfo));
+}
+
+
+int
+main(int argc, char *argv[])
+{
+	struct hv_kvp_msg *hv_kvp_dev_buf;
+	struct hv_kvp_msg *hv_msg;
+	struct pollfd hv_kvp_poll_fd[1];
+	int op, pool;
+	int hv_kvp_dev_fd, error, len, r;
+	int ch;
+
+	while ((ch = getopt(argc, argv, "dn")) != -1) {
+		switch (ch) {
+		case 'n':
+			/* Run as regular process for debugging purpose. */
+			is_daemon = 0;
+			break;
+		case 'd':
+			/* Generate debugging output */
+			is_debugging = 1;
+			break;
+		default:
+			break;
+		}
+	}
+
+	openlog("HV_KVP", 0, LOG_USER);
+
+	/* Become daemon first. */
+	if (is_daemon == 1)
+		daemon(1, 0);
+	else
+		KVP_LOG(LOG_DEBUG, "Run as regular process.\n");
+
+	KVP_LOG(LOG_INFO, "HV_KVP starting; pid is: %d\n", getpid());
+
+	/* Communication buffer hv_kvp_dev_buf */
+	hv_kvp_dev_buf = malloc(sizeof(*hv_kvp_dev_buf));
+	/* Buffer for daemon internal use */
+	hv_msg = malloc(sizeof(*hv_msg));
+
+	/* Memory allocation failed */
+	if (hv_kvp_dev_buf == NULL || hv_msg == NULL) {
+		KVP_LOG(LOG_ERR, "Failed to allocate memory for hv buffer\n");
+		exit(EXIT_FAILURE);
+	}
+
+	/* Initialize op handlers */
+	if (kvp_ops_init() != 0) {
+		KVP_LOG(LOG_ERR, "Failed to initizlize operation handlers\n");
+		exit(EXIT_FAILURE);
+	}
+
+	if (kvp_file_init()) {
+		KVP_LOG(LOG_ERR, "Failed to initialize the pools\n");
+		exit(EXIT_FAILURE);
+	}
+
+	/* Open the Character Device */
+	hv_kvp_dev_fd = open("/dev/hv_kvp_dev", O_RDWR);
+
+	if (hv_kvp_dev_fd < 0) {
+		KVP_LOG(LOG_ERR, "open /dev/hv_kvp_dev failed; error: %d %s\n",
+		    errno, strerror(errno));
+		exit(EXIT_FAILURE);
+	}
+
+	/* Initialize the struct for polling the char device */
+	hv_kvp_poll_fd[0].fd = hv_kvp_dev_fd;
+	hv_kvp_poll_fd[0].events = (POLLIN | POLLRDNORM);
+
+	/* Register the daemon to the KVP driver */
+	memset(hv_kvp_dev_buf, 0, sizeof(*hv_kvp_dev_buf));
+	hv_kvp_dev_buf->hdr.kvp_hdr.operation = HV_KVP_OP_REGISTER;
+	len = write(hv_kvp_dev_fd, hv_kvp_dev_buf, sizeof(*hv_kvp_dev_buf));
+
+
+	for (;;) {
+		r = poll (hv_kvp_poll_fd, 1, INFTIM);
+
+		KVP_LOG(LOG_DEBUG, "poll returned r = %d, revent = 0x%x\n",
+		    r, hv_kvp_poll_fd[0].revents);
+
+		if (r == 0 || (r < 0 && errno == EAGAIN) ||
+		    (r < 0 && errno == EINTR)) {
+			/* Nothing to read */
+			continue;
+		}
+
+		if (r < 0) {
+			/*
+			 * For pread return failure other than EAGAIN,
+			 * we want to exit.
+			 */
+			KVP_LOG(LOG_ERR, "Poll failed.\n");
+			perror("poll");
+			exit(EIO);
+		}
+
+		/* Read from character device */
+		len = pread(hv_kvp_dev_fd, hv_kvp_dev_buf,
+		    sizeof(*hv_kvp_dev_buf), 0);
+
+		if (len < 0) {
+			KVP_LOG(LOG_ERR, "Read failed.\n");
+			perror("pread");
+			exit(EIO);
+		}
+
+		if (len != sizeof(struct hv_kvp_msg)) {
+			KVP_LOG(LOG_ERR, "read len is: %d\n", len);
+			continue;
+		}
+
+		/* Copy hv_kvp_dev_buf to hv_msg */
+		memcpy(hv_msg, hv_kvp_dev_buf, sizeof(*hv_msg));
+
+		/*
+		 * We will use the KVP header information to pass back
+		 * the error from this daemon. So, first save the op
+		 * and pool info to local variables.
+		 */
+
+		op = hv_msg->hdr.kvp_hdr.operation;
+		pool = hv_msg->hdr.kvp_hdr.pool;
+
+		if (op < 0 || op >= HV_KVP_OP_COUNT ||
+		    kvp_op_hdlrs[op].kvp_op_exec == NULL) {
+			KVP_LOG(LOG_WARNING,
+			    "Unsupported operation OP = %d\n", op);
+			hv_msg->hdr.error = HV_ERROR_NOT_SUPPORTED;
+		} else {
+			/*
+			 * Call the operateion handler's execution routine.
+			 */
+			error = kvp_op_hdlrs[op].kvp_op_exec(hv_msg,
+			    (void *)&kvp_op_hdlrs[op]);
+			if (error != 0) {
+				assert(hv_msg->hdr.error != HV_S_OK);
+				if (hv_msg->hdr.error != HV_S_CONT)
+					KVP_LOG(LOG_WARNING,
+					    "Operation failed OP = %d, error = 0x%x\n",
+					    op, error);
+			}
+		}
+
+		/*
+		 * Send the value back to the kernel. The response is
+		 * already in the receive buffer.
+		 */
+hv_kvp_done:
+		len = pwrite(hv_kvp_dev_fd, hv_msg, sizeof(*hv_kvp_dev_buf), 0);
+
+		if (len != sizeof(struct hv_kvp_msg)) {
+			KVP_LOG(LOG_ERR, "write len is: %d\n", len);
+			goto hv_kvp_done;
+		}
+	}
+}


Property changes on: trunk/contrib/hyperv/tools/hv_kvp_daemon.c
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: trunk/contrib/hyperv/tools/hv_vss_daemon.8
===================================================================
--- trunk/contrib/hyperv/tools/hv_vss_daemon.8	                        (rev 0)
+++ trunk/contrib/hyperv/tools/hv_vss_daemon.8	2018-07-05 01:32:00 UTC (rev 11334)
@@ -0,0 +1,88 @@
+.\" Copyright (c) 2016 Microsoft Corp.
+.\" All rights reserved.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\"    notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\"    notice, this list of conditions and the following disclaimer in the
+.\"    documentation and/or other materials provided with the distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+.\" SUCH DAMAGE.
+.\"
+.\" $FreeBSD$
+.Dd October 12, 2016
+.Dt HV_VSS_DAEMON 8
+.Os
+.Sh NAME
+.Nm hv_vss_daemon
+.Nd Hyper-V Volume Shadow Copy Service Daemon
+.Sh SYNOPSIS
+.Nm
+.Op Fl dn
+.Sh DESCRIPTION
+The
+.Nm
+daemon provides the ability to freeze and thaw the file system for
+.Fx
+guest partitions running on Hyper-V.
+.Pp
+Hyper-V allows administrators to backup or restore the 
+.Fx
+guest partition.
+Administrators can
+use Windows Powershell scripts to backup or restore the
+.Fx
+VM.
+.Pp
+The
+.Nm
+accepts file system freeze and thaw requests from the
+.Xr hv_utils 4
+driver and performs the actual file-system operation.
+.Pp
+The file system freeze and thaw functionality is
+useful when the Hyper-V host wants to do live backup of
+.Fx
+guest. Hyper-V host sends file system freezing request to
+.Nm
+which conducts the real operation. After successfully freezing file
+system, Hyper-V host takes a snapshot of the VM. In the future,
+Hyper-V host can restore the
+.Fx
+VM through that snapshot.
+.Pp
+The options are as follows:
+.Bl -tag -width indent
+.It Fl d
+Run as regular process instead of a daemon for debugging purpose.
+.It Fl n
+Generate debugging output.
+.El
+.Sh SEE ALSO
+.Xr hv_vmbus 4 ,
+.Xr hv_utils 4 ,
+.Xr hv_netvsc 4 ,
+.Xr hv_storvsc 4 ,
+.Xr hv_kvp 4
+.Sh HISTORY
+The daemon was introduced in October 2016 and developed by Microsoft Corp.
+.Sh AUTHORS
+.An -nosplit
+.Fx
+support for
+.Nm
+was first added by
+.An Microsoft BSD Integration Services Team Aq Mt bsdic at microsoft.com .


Property changes on: trunk/contrib/hyperv/tools/hv_vss_daemon.8
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: trunk/contrib/hyperv/tools/hv_vss_daemon.c
===================================================================
--- trunk/contrib/hyperv/tools/hv_vss_daemon.c	                        (rev 0)
+++ trunk/contrib/hyperv/tools/hv_vss_daemon.c	2018-07-05 01:32:00 UTC (rev 11334)
@@ -0,0 +1,274 @@
+#include <string.h>
+#include <stdio.h>
+#include <sys/ioctl.h>
+#include <sys/param.h>
+#include <sys/ucred.h>
+#include <sys/mount.h>
+#include <sys/types.h>
+
+#include <unistd.h>
+#include <stdlib.h>
+#include <poll.h>
+#include <stdint.h>
+#include <syslog.h>
+#include <errno.h>
+#include <err.h>
+#include <fcntl.h>
+#include <ufs/ffs/fs.h>
+#include <paths.h>
+#include <sysexits.h>
+
+#include "hv_snapshot.h"
+
+#define UNDEF_FREEZE_THAW       (0)
+#define FREEZE                  (1)
+#define THAW                    (2)
+
+#define	VSS_LOG(priority, format, args...) do	{				\
+		if (is_debugging == 1) {					\
+			if (is_daemon == 1)					\
+				syslog(priority, format, ## args);		\
+			else							\
+				printf(format, ## args);			\
+		} else {							\
+			if (priority < LOG_DEBUG) {				\
+				if (is_daemon == 1)				\
+					syslog(priority, format, ## args);	\
+				else						\
+					printf(format, ## args);		\
+			}							\
+		}								\
+	} while(0)
+
+static int is_daemon = 1;
+static int is_debugging = 0;
+static int g_ufs_suspend_handle = -1;
+
+static const char *dev = "/dev";
+
+static int
+check(void)
+{
+	struct statfs *mntbuf, *statfsp;
+	int mntsize;
+	int i;
+
+	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
+	if (mntsize == 0) {
+		VSS_LOG(LOG_ERR, "There is no mount information\n");
+		return (EINVAL);
+	}
+	for (i = mntsize - 1; i >= 0; --i)
+	{
+		statfsp = &mntbuf[i];
+
+		if (strncmp(statfsp->f_mntonname, dev, strlen(dev)) == 0) {
+			continue; /* skip to freeze '/dev' */
+		} else if (statfsp->f_flags & MNT_RDONLY) {
+			continue; /* skip to freeze RDONLY partition */
+		} else if (strncmp(statfsp->f_fstypename, "ufs", 3) != 0) {
+			return (EPERM); /* only UFS can be freezed */
+		}
+	}
+
+	return (0);
+}
+
+static int
+freeze(void)
+{
+	struct statfs *mntbuf, *statfsp;
+	int mntsize;
+	int error = 0;
+	int i;
+
+	g_ufs_suspend_handle = open(_PATH_UFSSUSPEND, O_RDWR);
+	if (g_ufs_suspend_handle == -1) {
+		VSS_LOG(LOG_ERR, "unable to open %s", _PATH_UFSSUSPEND);
+		return (errno);
+	}
+
+	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
+	if (mntsize == 0) {
+		VSS_LOG(LOG_ERR, "There is no mount information\n");
+		return (EINVAL);
+	}
+	for (i = mntsize - 1; i >= 0; --i)
+	{
+		statfsp = &mntbuf[i];
+
+		if (strncmp(statfsp->f_mntonname, dev, strlen(dev)) == 0) {
+			continue; /* skip to freeze '/dev' */
+		} else if (statfsp->f_flags & MNT_RDONLY) {
+			continue; /* skip to freeze RDONLY partition */
+		} else if (strncmp(statfsp->f_fstypename, "ufs", 3) != 0) {
+			continue; /* only UFS can be freezed */
+		}
+		error = ioctl(g_ufs_suspend_handle, UFSSUSPEND, &statfsp->f_fsid);
+		if (error != 0) {
+			VSS_LOG(LOG_ERR, "error: %d\n", errno);
+			error = errno;
+		} else {
+			VSS_LOG(LOG_INFO, "Successfully suspend fs: %s\n",
+			    statfsp->f_mntonname);
+		}
+	}
+
+	return (error);
+}
+
+/**
+ * close the opened handle will thaw the FS.
+ */
+static int
+thaw(void)
+{
+	int error = 0;
+	if (g_ufs_suspend_handle != -1) {
+		error = close(g_ufs_suspend_handle);
+		if (!error) {
+			g_ufs_suspend_handle = -1;
+			VSS_LOG(LOG_INFO, "Successfully thaw the fs\n");
+		} else {
+			error = errno;
+			VSS_LOG(LOG_ERR, "Fail to thaw the fs: "
+			    "%d %s\n", errno, strerror(errno));
+		}
+	} else {
+		VSS_LOG(LOG_INFO, "The fs has already been thawed\n");
+	}
+
+	return (error);
+}
+
+static void
+usage(const char* cmd)
+{
+	fprintf(stderr, "%s: daemon for UFS file system freeze/thaw\n"
+	    " -d : enable debug log printing. Default is disabled.\n"
+	    " -n : run as a regular process instead of a daemon. Default is a daemon.\n"
+	    " -h : print usage.\n", cmd);
+	exit(1);
+}
+
+int
+main(int argc, char* argv[])
+{
+	struct hv_vss_opt_msg  userdata;
+
+	struct pollfd hv_vss_poll_fd[1];
+	uint32_t op;
+	int ch, r, error;
+	int hv_vss_dev_fd;
+
+	while ((ch = getopt(argc, argv, "dnh")) != -1) {
+		switch (ch) {
+		case 'n':
+			/* Run as regular process for debugging purpose. */
+			is_daemon = 0;
+			break;
+		case 'd':
+			/* Generate debugging output */
+			is_debugging = 1;
+			break;
+		case 'h':
+		default:
+			usage(argv[0]);
+			break;
+		}
+	}
+
+	openlog("HV_VSS", 0, LOG_USER);
+
+	/* Become daemon first. */
+	if (is_daemon == 1)
+		daemon(1, 0);
+	else
+		VSS_LOG(LOG_DEBUG, "Run as regular process.\n");
+
+	VSS_LOG(LOG_INFO, "HV_VSS starting; pid is: %d\n", getpid());
+
+	memset(&userdata, 0, sizeof(struct hv_vss_opt_msg));
+	/* register the daemon */
+	hv_vss_dev_fd = open(VSS_DEV(FS_VSS_DEV_NAME), O_RDWR);
+
+	if (hv_vss_dev_fd < 0) {
+		VSS_LOG(LOG_ERR, "Fail to open %s, error: %d %s\n",
+		    VSS_DEV(FS_VSS_DEV_NAME), errno, strerror(errno));
+		exit(EXIT_FAILURE);
+	}
+	hv_vss_poll_fd[0].fd = hv_vss_dev_fd;
+	hv_vss_poll_fd[0].events = POLLIN | POLLRDNORM;
+
+	while (1) {
+		r = poll(hv_vss_poll_fd, 1, INFTIM);
+
+		VSS_LOG(LOG_DEBUG, "poll returned r = %d, revent = 0x%x\n",
+		    r, hv_vss_poll_fd[0].revents);
+
+		if (r == 0 || (r < 0 && errno == EAGAIN) ||
+		    (r < 0 && errno == EINTR)) {
+			/* Nothing to read */
+			continue;
+		}
+
+		if (r < 0) {
+			/*
+			 * For poll return failure other than EAGAIN,
+			 * we want to exit.
+			 */
+			VSS_LOG(LOG_ERR, "Poll failed.\n");
+			perror("poll");
+			exit(EIO);
+		}
+
+		/* Read from character device */
+		error = ioctl(hv_vss_dev_fd, IOCHVVSSREAD, &userdata);
+		if (error < 0) {
+			VSS_LOG(LOG_ERR, "Read failed.\n");
+			perror("pread");
+			exit(EIO);
+		}
+
+		if (userdata.status != 0) {
+			VSS_LOG(LOG_ERR, "data read error\n");
+			continue;
+		}
+
+		/*
+		 * We will use the KVP header information to pass back
+		 * the error from this daemon. So, first save the op
+		 * and pool info to local variables.
+		 */
+
+		op = userdata.opt;
+
+		switch (op) {
+		case HV_VSS_CHECK:
+			error = check();
+			break;
+		case HV_VSS_FREEZE:
+			error = freeze();
+			break;
+		case HV_VSS_THAW:
+			error = thaw();
+			break;
+		default:
+			VSS_LOG(LOG_ERR, "Illegal operation: %d\n", op);
+			error = VSS_FAIL;
+		}
+		if (error)
+			userdata.status = VSS_FAIL;
+		else
+			userdata.status = VSS_SUCCESS;
+		error = ioctl(hv_vss_dev_fd, IOCHVVSSWRITE, &userdata);
+		if (error != 0) {
+			VSS_LOG(LOG_ERR, "Fail to write to device\n");
+			exit(EXIT_FAILURE);
+		} else {
+			VSS_LOG(LOG_INFO, "Send response %d for %s to kernel\n",
+			    userdata.status, op == HV_VSS_FREEZE ? "Freeze" :
+			    (op == HV_VSS_THAW ? "Thaw" : "Check"));
+		}
+	}
+}


Property changes on: trunk/contrib/hyperv/tools/hv_vss_daemon.c
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: trunk/contrib/hyperv/tools/scripts/hv_get_dhcp_info
===================================================================
--- trunk/contrib/hyperv/tools/scripts/hv_get_dhcp_info	                        (rev 0)
+++ trunk/contrib/hyperv/tools/scripts/hv_get_dhcp_info	2018-07-05 01:32:00 UTC (rev 11334)
@@ -0,0 +1,24 @@
+#!/bin/sh
+
+# This is the script retrieves the DHCP state of a given interface.
+# The kvp daemon code invokes this external script to gather
+# DHCP setting for the specific interface.
+#
+# Input: Name of the interface
+#
+# Output: The script prints the string "Enabled" to stdout to indicate
+#	that DHCP is enabled on the interface. If DHCP is not enabled,
+#	the script prints the string "Disabled" to stdout.
+#
+
+. /etc/rc.subr
+. /etc/network.subr
+
+load_rc_config netif
+
+if dhcpif hn0;
+then
+echo "Enabled"
+else
+echo "Disabled"
+fi

Added: trunk/contrib/hyperv/tools/scripts/hv_get_dns_info
===================================================================
--- trunk/contrib/hyperv/tools/scripts/hv_get_dns_info	                        (rev 0)
+++ trunk/contrib/hyperv/tools/scripts/hv_get_dns_info	2018-07-05 01:32:00 UTC (rev 11334)
@@ -0,0 +1,12 @@
+#!/bin/sh
+# This script parses /etc/resolv.conf to retrive DNS information.
+# Khe kvp daemon code invokes this external script to gather
+# DNS information.
+# This script is expected to print the nameserver values to stdout.
+
+#if test -r /etc/resolv.conf
+#then	
+#	awk -- '/^nameserver/ { print $2 }' /etc/resolv.conf
+#fi
+cat /etc/resolv.conf 2>/dev/null | awk '/^nameserver/ { print $2 }'
+

Added: trunk/contrib/hyperv/tools/scripts/hv_set_ifconfig
===================================================================
--- trunk/contrib/hyperv/tools/scripts/hv_set_ifconfig	                        (rev 0)
+++ trunk/contrib/hyperv/tools/scripts/hv_set_ifconfig	2018-07-05 01:32:00 UTC (rev 11334)
@@ -0,0 +1,73 @@
+#!/bin/sh
+
+# This script activates an interface based on the specified
+# configuration. The kvp daemon code invokes this external script
+# to configure the interface.
+#
+# The only argument to this script is the configuration file that is to
+# be used to configure the interface.
+#
+# Here is the format of the ip configuration file:
+#
+# HWADDR=macaddr
+# IF_NAME=interface name
+# DHCP=yes (This is optional; if yes, DHCP is configured)
+#
+# IPADDR=ipaddr1
+# IPADDR_1=ipaddr2
+# IPADDR_x=ipaddry (where y = x + 1)
+#
+# NETMASK=netmask1
+# NETMASK_x=netmasky (where y = x + 1)
+#
+# GATEWAY=ipaddr1
+# GATEWAY_x=ipaddry (where y = x + 1)
+#
+# DNSx=ipaddrx (where first DNS address is tagged as DNS1 etc)
+#
+# IPV6 addresses will be tagged as IPV6ADDR, IPV6 gateway will be
+# tagged as IPV6_DEFAULTGW and IPV6 NETMASK will be tagged as
+# IPV6NETMASK.
+#
+# The host can specify multiple ipv4 and ipv6 addresses to be
+# configured for the interface. Furthermore, the configuration
+# needs to be persistent. A subsequent GET call on the interface
+# is expected to return the configuration that is set via the SET
+# call.
+#
+
+. $1
+
+sed -i".bak" '/ifconfig_hn0="SYNCDHCP"/d' /etc/rc.conf
+sed -i".bak" '/ifconfig_hn0="DHCP"/d' /etc/rc.conf
+
+# MAC Address
+ifconfig $IF_NAME ether $HWADDR 
+
+# IP and Subnet Mask
+ifconfig $IF_NAME inet $IP_ADDR netmask $SUBNET 
+
+# DNS
+sed -i".bak" '/nameserver/d' /etc/resolv.conf
+echo "nameserver" $DNS >> /etc/resolv.conf 
+
+#Gateway
+# Need to implment if Gateway is not present 
+route flush
+route add default $GATEWAY
+#route change default $GATEWAY
+
+#/etc/rc.d/netif restart 
+#/etc/rc.d/routing restart
+
+
+# DHCP
+if [ $DHCP -eq 1 ]
+then
+	echo ifconfig_hn0=\"DHCP\" >> /etc/rc.conf
+	echo Enabled 
+else
+	echo Disabled DHCP >> /var/log/messages
+	echo Disabled
+fi
+echo "Set IP-Injection Success"

Added: trunk/contrib/hyperv/tools/scripts/hyperv_vfattach
===================================================================
--- trunk/contrib/hyperv/tools/scripts/hyperv_vfattach	                        (rev 0)
+++ trunk/contrib/hyperv/tools/scripts/hyperv_vfattach	2018-07-05 01:32:00 UTC (rev 11334)
@@ -0,0 +1,79 @@
+#!/bin/sh
+
+#
+# If transparent VF is enabled, don't do anything.
+#
+
+sysctl -n hw.hn.vf_transparent > /dev/null 2>&1
+if [ $? -ne 0 ]
+then
+	# Old kernel; no transparent VF.
+	vf_transparent=0
+else
+	vf_transparent=`sysctl -n hw.hn.vf_transparent`
+fi
+
+if [ $vf_transparent -ne 0 ]
+then
+	# Transparent VF; done!
+	exit 0
+fi
+
+iface=$1
+delay=$2
+
+if [ $delay -gt 0 ]
+then
+	#
+	# Delayed VF up.
+	#
+	sleep $delay
+	ifconfig $iface up
+	# Done!
+	exit $?
+fi
+
+#
+# Check to see whether $iface is a VF or not.
+# If $iface is a VF, bring it up now.
+#
+
+# for hyperv_vf_delay
+. /etc/rc.conf
+
+sysctl -n hw.hn.vflist > /dev/null 2>&1
+if [ $? -ne 0 ]
+then
+	# Old kernel; nothing could be done properly.
+	exit 0
+fi
+vf_list=`sysctl -n hw.hn.vflist`
+
+for vf in $vf_list
+do
+	if [ $vf = $iface ]
+	then
+		#
+		# Linger a little bit (at least 2 seconds) mainly to
+		# make sure that $iface is fully attached.
+		#
+		# NOTE:
+		# In Azure hyperv_vf_delay should be configured to a
+		# large value, e.g. 120 seconds, to avoid racing cloud
+		# agent goofs.
+		#
+		test $hyperv_vf_delay -ge 2 > /dev/null 2>&1
+		if [ $? -ne 0 ]
+		then
+			hyperv_vf_delay=2
+		fi
+		#
+		# NOTE:
+		# "(sleep ..; ifconfig .. up) > /dev/null 2>&1 &"
+		# does _not_ work.
+		#
+		daemon -f /usr/libexec/hyperv/hyperv_vfattach \
+		    $iface $hyperv_vf_delay
+		break
+	fi
+done

Added: trunk/contrib/hyperv/tools/scripts/hyperv_vfup
===================================================================
--- trunk/contrib/hyperv/tools/scripts/hyperv_vfup	                        (rev 0)
+++ trunk/contrib/hyperv/tools/scripts/hyperv_vfup	2018-07-05 01:32:00 UTC (rev 11334)
@@ -0,0 +1,119 @@
+#!/bin/sh
+
+. /etc/rc.subr
+. /etc/network.subr
+
+load_rc_config netif
+
+#
+# Customized per-interface setup, e.g. hyperv_vfup.hn1
+#
+# NOTE-CUSTOMIZE:
+# Comment this out, if this script is used as template
+# for the customized per-interface setup.
+#
+if [ -f /usr/libexec/hyperv/hyperv_vfup.$1 ]
+then
+	/usr/libexec/hyperv/hyperv_vfup.$1
+	exit $?
+fi
+
+# NOTE-CUSTOMIZE:
+#hn=${0##*.}
+hn=$1
+hn_unit=`echo $hn | sed 's/[^0-9]*//g'`
+
+vf=`sysctl -n dev.hn.$hn_unit.vf`
+if [ ! $vf ]
+then
+	# Race happened; VF was removed, before we ran.
+	echo "$hn: VF was detached"
+	exit 0
+fi
+
+#
+# Create laggX for hnX.
+# Add VF and hnX to laggX.
+#
+
+lagg=lagg$hn_unit
+
+ifconfig $lagg > /dev/null 2>&1
+if [ $? -ne 0 ]
+then
+	#
+	# No laggX, create it now.
+	#
+	ifconfig $lagg create > /dev/null 2>&1
+	if [ $? -ne 0 ]
+	then
+		echo "$lagg creation failed"
+		exit 1
+	fi
+
+	#
+	# Configure laggX (failover), add hnX and VF to it.
+	#
+	ifconfig $lagg laggproto failover laggport $hn laggport $vf
+	ifconfig $lagg inet6 no_dad
+
+	#
+	# Stop dhclient on hnX, if any.
+	#
+	pidfile=/var/run/dhclient.$hn.pid
+	if [ -f $pidfile ]
+	then
+		kill -TERM `cat $pidfile`
+	fi
+
+	#
+	# Remove all configured IPv4 addresses on hnX, e.g.
+	# configured by dhclient.  laggX will take over the
+	# network operations.
+	#
+	while true
+	do
+		ifconfig $hn -alias > /dev/null 2>&1
+		if [ $? -ne 0 ]
+		then
+			break
+		fi
+	done
+
+	# TODO: Remove IPv6 addresses on hnX
+
+	#
+	# Use hnX's configuration for laggX
+	#
+	# NOTE-CUSTOMIZE:
+	# If this script is used as template for the customized
+	# per-interface setup, replace this with whatever you
+	# want to do with the laggX.
+	#
+	if dhcpif $hn;
+	then
+		ifconfig $lagg up
+		if syncdhcpif $hn;
+		then
+			dhclient $lagg
+		else
+			dhclient -b $lagg
+		fi
+	else
+		ifconfig_args=`ifconfig_getargs $hn`
+		if [ -n "$ifconfig_args" ]
+		then
+			ifconfig $lagg $ifconfig_args
+		fi
+	fi
+else
+	#
+	# laggX exists.  Check whether VF was there or not.
+	# If VF was not added to laggX, add it now.
+	#
+	ifconfig $lagg | grep "laggport: $vf" > /dev/null 2>&1
+	if [ $? -ne 0 ]
+	then
+		ifconfig $lagg laggport $vf
+	fi
+fi



More information about the Midnightbsd-cvs mailing list