xhyve-ruby 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e6b3306528c86e2cefcf0f27af532d40d5161bdb
4
+ data.tar.gz: a2be4f5098ed71764dc2601aa4da17c9920e6d55
5
+ SHA512:
6
+ metadata.gz: 11b66ab3d71dcc8df1115ea5e517d6dd711cad6f8dd58a76a658727d131bba29eb4de0a9f9b1b22e894f2ca3ba8435d435f3235f773699e75f0ed9dd93b02164
7
+ data.tar.gz: 1ddea73c5928460bae1184d38edfae0a69e0a990c31a38c48b9e328c0a968ac281abb68c1410127fa08a80adb7637195f9714dd043e74a7b20c84641ef8abc64
@@ -0,0 +1,28 @@
1
+ require "mkmf"
2
+
3
+ require 'rbconfig'
4
+
5
+ def host_os
6
+ os = RbConfig::CONFIG['host_os']
7
+ case
8
+ when os.downcase.include?('linux')
9
+ 'linux'
10
+ when os.downcase.include?('darwin')
11
+ 'darwin'
12
+ else
13
+ puts 'You are not on a supported platform. exiting...'
14
+ puts 'Mac OS X and Linux are the only supported platforms.'
15
+ exit
16
+ end
17
+ end
18
+
19
+ abort "missing vmnet.h" unless have_header "uuid.h"
20
+ abort "missing vmnet.h" unless have_header "vmnet/vmnet.h"
21
+
22
+ if host_os == 'darwin'
23
+ abort "missing vmnet" unless have_framework "vmnet"
24
+ else
25
+ abort "missing vmnet" unless have_library "vmnet"
26
+ end
27
+
28
+ create_makefile "vmnet/vmnet"
data/ext/vmnet/uuid.h ADDED
@@ -0,0 +1,153 @@
1
+ /*-
2
+ * Copyright (c) 2002,2005 Marcel Moolenaar
3
+ * Copyright (c) 2002 Hiten Mahesh Pandya
4
+ * All rights reserved.
5
+ *
6
+ * Redistribution and use in source and binary forms, with or without
7
+ * modification, are permitted provided that the following conditions
8
+ * are met:
9
+ * 1. Redistributions of source code must retain the above copyright
10
+ * notice, this list of conditions and the following disclaimer.
11
+ * 2. Redistributions in binary form must reproduce the above copyright
12
+ * notice, this list of conditions and the following disclaimer in the
13
+ * documentation and/or other materials provided with the distribution.
14
+ *
15
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25
+ * SUCH DAMAGE.
26
+ *
27
+ * $FreeBSD$
28
+ */
29
+
30
+ #pragma once
31
+
32
+ #include <stdint.h>
33
+ #include <stdio.h>
34
+ #include <string.h>
35
+
36
+ #define _UUID_NODE_LEN 6
37
+
38
+ #pragma clang diagnostic push
39
+ #pragma clang diagnostic ignored "-Wpadded"
40
+ struct uuid {
41
+ uint32_t time_low;
42
+ uint16_t time_mid;
43
+ uint16_t time_hi_and_version;
44
+ uint8_t clock_seq_hi_and_reserved;
45
+ uint8_t clock_seq_low;
46
+ uint8_t node[_UUID_NODE_LEN];
47
+ };
48
+ #pragma clang diagnostic pop
49
+
50
+ typedef struct uuid uuid_internal_t;
51
+
52
+ /*
53
+ * This implementation mostly conforms to the DCE 1.1 specification.
54
+ * See Also:
55
+ * uuidgen(1), uuidgen(2), uuid(3)
56
+ */
57
+
58
+ /* Status codes returned by the functions. */
59
+ #define uuid_s_ok 0
60
+ #define uuid_s_bad_version 1
61
+ #define uuid_s_invalid_string_uuid 2
62
+ #define uuid_s_no_memory 3
63
+
64
+ /*
65
+ * uuid_create_nil() - create a nil UUID.
66
+ * See also:
67
+ * http://www.opengroup.org/onlinepubs/009629399/uuid_create_nil.htm
68
+ */
69
+ static inline void
70
+ uuid_create_nil(uuid_t *u, uint32_t *status)
71
+ {
72
+ if (status)
73
+ *status = uuid_s_ok;
74
+
75
+ bzero(u, sizeof(*u));
76
+ }
77
+
78
+ static inline void
79
+ uuid_enc_le(void *buf, uuid_t *uuid)
80
+ {
81
+ uuid_internal_t *u = (uuid_internal_t *) ((void *) uuid);
82
+ uint8_t *p = buf;
83
+ int i;
84
+
85
+ memcpy(p, &u->time_low, 4);
86
+ memcpy(p, &u->time_mid, 2);
87
+ memcpy(p, &u->time_hi_and_version, 2);
88
+ p[8] = u->clock_seq_hi_and_reserved;
89
+ p[9] = u->clock_seq_low;
90
+ for (i = 0; i < _UUID_NODE_LEN; i++)
91
+ p[10 + i] = u->node[i];
92
+ }
93
+
94
+ /*
95
+ * uuid_from_string() - convert a string representation of an UUID into
96
+ * a binary representation.
97
+ * See also:
98
+ * http://www.opengroup.org/onlinepubs/009629399/uuid_from_string.htm
99
+ *
100
+ * NOTE: The sequence field is in big-endian, while the time fields are in
101
+ * native byte order.
102
+ */
103
+ static inline void
104
+ uuid_from_string(const char *s, uuid_t *uuid, uint32_t *status)
105
+ {
106
+ uuid_internal_t *u = (uuid_internal_t *) ((void *) uuid);
107
+ int n;
108
+
109
+ /* Short-circuit 2 special cases: NULL pointer and empty string. */
110
+ if (s == NULL || *s == '\0') {
111
+ uuid_create_nil(((uuid_t *) u), status);
112
+ return;
113
+ }
114
+
115
+ /* Assume the worst. */
116
+ if (status != NULL)
117
+ *status = uuid_s_invalid_string_uuid;
118
+
119
+ /* The UUID string representation has a fixed length. */
120
+ if (strlen(s) != 36)
121
+ return;
122
+
123
+ /*
124
+ * We only work with "new" UUIDs. New UUIDs have the form:
125
+ * 01234567-89ab-cdef-0123-456789abcdef
126
+ * The so called "old" UUIDs, which we don't support, have the form:
127
+ * 0123456789ab.cd.ef.01.23.45.67.89.ab
128
+ */
129
+ if (s[8] != '-')
130
+ return;
131
+
132
+ n = sscanf(s,
133
+ "%8x-%4hx-%4hx-%2hhx%2hhx-%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx",
134
+ &u->time_low, &u->time_mid, &u->time_hi_and_version,
135
+ &u->clock_seq_hi_and_reserved, &u->clock_seq_low, &u->node[0],
136
+ &u->node[1], &u->node[2], &u->node[3], &u->node[4], &u->node[5]);
137
+
138
+ /* Make sure we have all conversions. */
139
+ if (n != 11)
140
+ return;
141
+
142
+ /* We have a successful scan. Check semantics... */
143
+ n = u->clock_seq_hi_and_reserved;
144
+ if ((n & 0x80) != 0x00 && /* variant 0? */
145
+ (n & 0xc0) != 0x80 && /* variant 1? */
146
+ (n & 0xe0) != 0xc0) { /* variant 2? */
147
+ if (status != NULL)
148
+ *status = uuid_s_bad_version;
149
+ } else {
150
+ if (status != NULL)
151
+ *status = uuid_s_ok;
152
+ }
153
+ }
data/ext/vmnet/vmnet.c ADDED
@@ -0,0 +1,146 @@
1
+ /*-
2
+ * Copyright (c) 2011 NetApp, Inc.
3
+ * Copyright (c) 2015 xhyve developers
4
+ * All rights reserved.
5
+ *
6
+ * Redistribution and use in source and binary forms, with or without
7
+ * modification, are permitted provided that the following conditions
8
+ * are met:
9
+ * 1. Redistributions of source code must retain the above copyright
10
+ * notice, this list of conditions and the following disclaimer.
11
+ * 2. Redistributions in binary form must reproduce the above copyright
12
+ * notice, this list of conditions and the following disclaimer in the
13
+ * documentation and/or other materials provided with the distribution.
14
+ *
15
+ * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
16
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18
+ * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
19
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25
+ * SUCH DAMAGE.
26
+ *
27
+ * $FreeBSD$
28
+ */
29
+
30
+ /*
31
+ * https://github.com/mirage/ocaml-vmnet/blob/master/lib/vmnet_stubs.c
32
+ *
33
+ * Copyright (C) 2014 Anil Madhavapeddy <anil@recoil.org>
34
+ *
35
+ * Permission to use, copy, modify, and distribute this software for any
36
+ * purpose with or without fee is hereby granted, provided that the above
37
+ * copyright notice and this permission notice appear in all copies.
38
+ *
39
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
40
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
41
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
42
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
43
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
44
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
45
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
46
+ */
47
+
48
+ #include <ruby.h>
49
+
50
+ #include <stdio.h>
51
+
52
+ #include <vmnet/vmnet.h>
53
+
54
+ #include "uuid.h"
55
+
56
+ static VALUE
57
+ vmnet_get_mac_address_from_uuid(VALUE self, VALUE guest_uuid_str_ruby) {
58
+ /*
59
+ * from vmn_create() in https://github.com/mist64/xhyve/blob/master/src/pci_virtio_vmnet.c
60
+ */
61
+ char* guest_uuid_str;
62
+ xpc_object_t interface_desc;
63
+ uuid_t uuid;
64
+ __block interface_ref iface;
65
+ __block vmnet_return_t iface_status;
66
+ __block char* mac = malloc(18);
67
+ dispatch_semaphore_t iface_created, iface_stopped;
68
+ dispatch_queue_t if_create_q, if_stop_q;
69
+ uint32_t uuid_status;
70
+
71
+ guest_uuid_str = RSTRING_PTR(guest_uuid_str_ruby);
72
+ interface_desc = xpc_dictionary_create(NULL, NULL, 0);
73
+ xpc_dictionary_set_uint64(interface_desc, vmnet_operation_mode_key, VMNET_SHARED_MODE);
74
+
75
+ uuid_from_string(guest_uuid_str, &uuid, &uuid_status);
76
+ if (uuid_status != uuid_s_ok) {
77
+ fprintf(stderr, "Invalid UUID %s\n", guest_uuid_str);
78
+ return rb_str_new2("");
79
+ }
80
+
81
+ xpc_dictionary_set_uuid(interface_desc, vmnet_interface_id_key, uuid);
82
+ iface = NULL;
83
+ iface_status = 0;
84
+
85
+ if_create_q = dispatch_queue_create("org.xhyve.vmnet.create", DISPATCH_QUEUE_SERIAL);
86
+
87
+ iface_created = dispatch_semaphore_create(0);
88
+
89
+ iface = vmnet_start_interface(interface_desc, if_create_q,
90
+ ^(vmnet_return_t status, xpc_object_t interface_param)
91
+ {
92
+ iface_status = status;
93
+ if (status != VMNET_SUCCESS || !interface_param) {
94
+ dispatch_semaphore_signal(iface_created);
95
+ return;
96
+ }
97
+
98
+ //printf("%s\n", xpc_dictionary_get_string(interface_param, vmnet_mac_address_key));
99
+ const char *macStr = xpc_dictionary_get_string(interface_param, vmnet_mac_address_key);
100
+ strcpy(mac, macStr);
101
+
102
+ dispatch_semaphore_signal(iface_created);
103
+ });
104
+
105
+ dispatch_semaphore_wait(iface_created, DISPATCH_TIME_FOREVER);
106
+ dispatch_release(if_create_q);
107
+
108
+ if (iface == NULL || iface_status != VMNET_SUCCESS) {
109
+ fprintf(stderr, "virtio_net: Could not create vmnet interface, "
110
+ "permission denied or no entitlement?\n");
111
+ return rb_str_new2("");
112
+ }
113
+
114
+ iface_status = 0;
115
+
116
+ if_stop_q = dispatch_queue_create("org.xhyve.vmnet.stop", DISPATCH_QUEUE_SERIAL);
117
+
118
+ iface_stopped = dispatch_semaphore_create(0);
119
+
120
+ iface_status = vmnet_stop_interface(iface, if_stop_q,
121
+ ^(vmnet_return_t status)
122
+ {
123
+ iface_status = status;
124
+ dispatch_semaphore_signal(iface_stopped);
125
+ });
126
+
127
+ dispatch_semaphore_wait(iface_stopped, DISPATCH_TIME_FOREVER);
128
+ dispatch_release(if_stop_q);
129
+
130
+ if (iface_status != VMNET_SUCCESS) {
131
+ fprintf(stderr, "virtio_net: Could not stop vmnet interface, "
132
+ "permission denied or no entitlement?\n");
133
+ return rb_str_new2("");
134
+ }
135
+
136
+ return rb_str_new2(mac);
137
+ }
138
+
139
+ void
140
+ Init_vmnet(void){
141
+ VALUE cVmnet;
142
+
143
+ cVmnet = rb_define_module("VMNet");
144
+
145
+ rb_define_singleton_method(cVmnet, "uuid_to_mac", vmnet_get_mac_address_from_uuid, 1);
146
+ }
@@ -0,0 +1,3 @@
1
+ module Xhyve
2
+ VERSION='0.0.1'
3
+ end
Binary file
data/lib/xhyve.rb ADDED
@@ -0,0 +1,4 @@
1
+ $LOAD_PATH.unshift(File.expand_path('../lib', __FILE__))
2
+
3
+ require 'xhyve/version'
4
+ require 'xhyve/vmnet/vmnet.bundle'
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: xhyve-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Dale Hamel
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-11-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 10.4.2
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 10.4.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake-compiler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 0.9.5
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 0.9.5
41
+ description: Provides a means of interacting with xhyve from ruby
42
+ email: dale.hamel@srvthe.net
43
+ executables: []
44
+ extensions:
45
+ - ext/vmnet/extconf.rb
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ext/vmnet/extconf.rb
49
+ - ext/vmnet/uuid.h
50
+ - ext/vmnet/vmnet.c
51
+ - lib/xhyve.rb
52
+ - lib/xhyve/version.rb
53
+ - lib/xhyve/vmnet/vmnet.bundle
54
+ homepage: http://rubygems.org/gems/ruby-xhyve
55
+ licenses:
56
+ - MIT
57
+ metadata: {}
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubyforge_project:
74
+ rubygems_version: 2.4.3
75
+ signing_key:
76
+ specification_version: 4
77
+ summary: Ruby wrapper for xhyve
78
+ test_files: []