aboutsummaryrefslogtreecommitdiffstats
path: root/tools/vnet
diff options
context:
space:
mode:
authorkaf24@firebug.cl.cam.ac.uk <kaf24@firebug.cl.cam.ac.uk>2005-09-12 14:11:59 +0000
committerkaf24@firebug.cl.cam.ac.uk <kaf24@firebug.cl.cam.ac.uk>2005-09-12 14:11:59 +0000
commit999ef6d9c5db00168da5b1466025c212a81287b9 (patch)
treec77ba1cba8101a32baf708d350b397b1cea722a3 /tools/vnet
parent0cbc2f5eb460aab641cf660ea045bbc13c0de557 (diff)
downloadxen-999ef6d9c5db00168da5b1466025c212a81287b9.tar.gz
xen-999ef6d9c5db00168da5b1466025c212a81287b9.tar.bz2
xen-999ef6d9c5db00168da5b1466025c212a81287b9.zip
The last vnet patch was missing some files that had been added
to my tree but did not appear in 'hg diff'. This patch adds them. Signed-off-by: Mike Wray <mike.wray@hp.com>
Diffstat (limited to 'tools/vnet')
-rw-r--r--tools/vnet/Make.env20
-rw-r--r--tools/vnet/examples/vnet-insert28
-rw-r--r--tools/vnet/libxutil/mem_stream.c319
-rw-r--r--tools/vnet/libxutil/mem_stream.h32
-rw-r--r--tools/vnet/vnet-module/varp_util.c77
-rw-r--r--tools/vnet/vnet-module/varp_util.h142
6 files changed, 618 insertions, 0 deletions
diff --git a/tools/vnet/Make.env b/tools/vnet/Make.env
new file mode 100644
index 0000000000..a633ed366d
--- /dev/null
+++ b/tools/vnet/Make.env
@@ -0,0 +1,20 @@
+# -*- mode: Makefile; -*-
+
+export XEN_ROOT = $(shell cd $(VNET_ROOT)/../.. && pwd)
+export LINUX_SERIES ?= 2.6
+
+DISTDIR ?= $(XEN_ROOT)/dist
+export DESTDIR ?= $(DISTDIR)/install
+
+export VNET_MODULE_DIR = $(VNET_ROOT)/vnet-module
+export VNETD_DIR = $(VNET_ROOT)/vnetd
+export LIBXUTIL_DIR = $(VNET_ROOT)/libxutil
+
+export GC_DIR = $(VNET_ROOT)/build/gc
+export GC_INCLUDE = $(GC_DIR)/include
+export GC_LIB_DIR = $(GC_DIR)/lib
+export GC_LIB_A = $(GC_LIB_DIR)/libgc.a
+export GC_LIB_SO = $(GC_LIB_DIR)/libgc.so
+
+#$(warning XEN_ROOT = $(XEN_ROOT))
+#$(warning DESTDIR = $(DESTDIR))
diff --git a/tools/vnet/examples/vnet-insert b/tools/vnet/examples/vnet-insert
new file mode 100644
index 0000000000..49269f6646
--- /dev/null
+++ b/tools/vnet/examples/vnet-insert
@@ -0,0 +1,28 @@
+#!/bin/bash
+
+# Insert the vnet module if it can be found and
+# it's not already there.
+vnet_insert () {
+ local module="vnet_module"
+ local mod_dir=/lib/modules/$(uname -r)
+ local mod_obj=""
+
+ if lsmod | grep -q ${module} ; then
+ echo "VNET: ${module} loaded"
+ return
+ fi
+ local mods=$(find ${mod_dir} -name "${module}.*o")
+ if [[ ${mods} ]] ; then
+ for mod_obj in ${mods} ; do
+ break
+ done
+ fi
+ if [ -z "${mod_obj}" ] ; then
+ echo "VNET: ${module} not found"
+ exit 1
+ fi
+ echo "VNET: Loading ${module} from ${mod_obj}"
+ insmod ${mod_obj} "$@"
+}
+
+vnet_insert "$@"
diff --git a/tools/vnet/libxutil/mem_stream.c b/tools/vnet/libxutil/mem_stream.c
new file mode 100644
index 0000000000..20340e4437
--- /dev/null
+++ b/tools/vnet/libxutil/mem_stream.c
@@ -0,0 +1,319 @@
+/*
+ * Copyright (C) 2005 Mike Wray <mike.wray@hp.com>
+ *
+ * This library is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+/** @file
+ * IOStream subtype for input and output to memory.
+ * Usable from user or kernel code (with __KERNEL__ defined).
+ */
+
+#include "sys_string.h"
+#include "mem_stream.h"
+#include "allocate.h"
+
+/** Internal state for a memory stream.
+ *
+ * The memory stream buffer is treated as a circular buffer.
+ * The lo and hi markers indicate positions in the buffer, but
+ * are not reduced modulo the buffer size. This avoids the ambiguity
+ * between a full and empty buffer when using reduced values.
+ *
+ * If x is a marker, then buf + (x % buf_n) is the corresponding
+ * pointer into the buffer. When the buffer is empty, lo == hi,
+ * and the corresponding pointers are equal. When the buffer is
+ * full, hi == lo + buf_n, and the corresponding pointers
+ * are also equal.
+ *
+ * Data is written after the high pointer and read from the lo pointer.
+ * The value hi - lo is the number of bytes in the buffer.
+ */
+typedef struct MemData {
+ /** Data buffer. */
+ char *buf;
+ /** Low marker - start of readable area. */
+ unsigned long lo;
+ /** High marker - end of readable area, start of writeable area. */
+ unsigned long hi;
+ /** Size of the buffer. */
+ unsigned int buf_n;
+ /** Maximum size the buffer can grow to. */
+ unsigned int buf_max;
+ /** Error code. */
+ int err;
+} MemData;
+
+/** Get number of bytes available to read.
+ *
+ * @param data mem stream
+ * @return bytes
+ */
+static inline int mem_len(struct MemData *data){
+ return data->hi - data->lo;
+}
+
+/** Get available space left in the buffer.
+ *
+ * @param data mem stream
+ * @return bytes
+ */
+static inline int mem_room(struct MemData *data){
+ return data->buf_n - mem_len(data);
+}
+
+/** Get a pointer to the start of the data in the buffer.
+ *
+ * @param data mem stream
+ * @return lo pointer
+ */
+static inline char * mem_lo(struct MemData *data){
+ return data->buf + (data->lo % data->buf_n);
+}
+
+/** Get a pointer to the end of the data in the buffer.
+ *
+ * @param data mem stream
+ * @return hi pointer
+ */
+static inline char * mem_hi(struct MemData *data){
+ return data->buf + (data->hi % data->buf_n);
+}
+
+/** Get a pointer to the end of the buffer.
+ *
+ * @param data mem stream
+ * @return end pointer
+ */
+static inline char * mem_end(struct MemData *data){
+ return data->buf + data->buf_n;
+}
+
+static int mem_error(IOStream *io);
+static int mem_close(IOStream *io);
+static void mem_free(IOStream *io);
+static int mem_write(IOStream *io, const void *msg, size_t n);
+static int mem_read(IOStream *io, void *buf, size_t n);
+
+/** Minimum delta used to increment the buffer. */
+static int delta_min = 256;
+
+/** Methods for a memory stream. */
+static IOMethods mem_methods = {
+ read: mem_read,
+ write: mem_write,
+ error: mem_error,
+ close: mem_close,
+ free: mem_free,
+};
+
+/** Get the memory stream state.
+ *
+ * @param io memory stream
+ * @return state
+ */
+static inline MemData *get_mem_data(IOStream *io){
+ return (MemData*)io->data;
+}
+
+/** Get the number of bytes available to read.
+ *
+ * @param io memory stream
+ * @return number of bytes
+ */
+int mem_stream_avail(IOStream *io){
+ MemData *data = get_mem_data(io);
+ return (data->err ? -data->err : mem_len(data));
+}
+
+/** Copy bytes from a memory stream into a buffer.
+ *
+ * @param data mem stream
+ * @param buf buffer
+ * @param n number of bytes to copy
+ */
+static void mem_get(MemData *data, char *buf, size_t n){
+ char *start = mem_lo(data);
+ char *end = mem_end(data);
+ if (start + n < end) {
+ memcpy(buf, start, n);
+ } else {
+ int k = end - start;
+ memcpy(buf, start, k);
+ memcpy(buf + k, data->buf, n - k);
+ }
+}
+
+/** Copy bytes from a buffer into a memory stream.
+ *
+ * @param data mem stream
+ * @param buf buffer
+ * @param n number of bytes to copy
+ */
+static void mem_put(MemData *data, const char *buf, size_t n){
+ char *start = mem_hi(data);
+ char *end = mem_end(data);
+ if(start + n < end){
+ memcpy(start, buf, n);
+ } else {
+ int k = end - start;
+ memcpy(start, buf, k);
+ memcpy(data->buf, buf + k, n - k);
+ }
+}
+
+/** Expand the buffer used by a memory stream.
+ *
+ * @param data mem stream
+ * @param extra number of bytes to expand by
+ * @return 0 on success, negative error otherwise
+ */
+static int mem_expand(MemData *data, size_t extra){
+ int err = -ENOMEM;
+ int delta = (extra < delta_min ? delta_min : extra);
+ if(data->buf_max > 0){
+ int delta_max = data->buf_max - data->buf_n;
+ if(delta > delta_max){
+ delta = extra;
+ if(delta > delta_max) goto exit;
+ }
+ }
+ int buf_n = data->buf_n + delta;
+ char *buf = allocate(buf_n);
+ if(!buf) goto exit;
+ mem_get(data, buf, mem_len(data));
+ data->hi = mem_len(data);
+ data->lo = 0;
+ deallocate(data->buf);
+ data->buf = buf;
+ data->buf_n = buf_n;
+ err = 0;
+ exit:
+ if(err){
+ data->err = -err;
+ }
+ return err;
+}
+
+/** Write bytes from a buffer into a memory stream.
+ * The internal buffer is expanded as needed to hold the data,
+ * up to the stream maximum (if specified). If the buffer cannot
+ * be expanded -ENOMEM is returned.
+ *
+ * @param io mem stream
+ * @param buf buffer
+ * @param n number of bytes to write
+ * @return number of bytes written on success, negative error code otherwise
+ */
+static int mem_write(IOStream *io, const void *msg, size_t n){
+ MemData *data = get_mem_data(io);
+ if(data->err) return -data->err;
+ int room = mem_room(data);
+ if(n > room){
+ int err = mem_expand(data, n - room);
+ if(err) return err;
+ }
+ mem_put(data, msg, n);
+ data->hi += n;
+ return n;
+}
+
+/** Read bytes from a memory stream into a buffer.
+ *
+ * @param io mem stream
+ * @param buf buffer
+ * @param n maximum number of bytes to read
+ * @return number of bytes read on success, negative error code otherwise
+ */
+static int mem_read(IOStream *io, void *buf, size_t n){
+ MemData *data = get_mem_data(io);
+ if(data->err) return -data->err;
+ int k = mem_len(data);
+ if(n > k){
+ n = k;
+ }
+ mem_get(data, buf, n);
+ data->lo += n;
+ return n;
+}
+
+/** Test if a memory stream has an error.
+ *
+ * @param io mem stream
+ * @return 0 if ok, error code otherwise
+ */
+static int mem_error(IOStream *io){
+ MemData *data = get_mem_data(io);
+ return data->err;
+}
+
+/** Close a memory stream.
+ *
+ * @param io mem stream
+ * @return 0
+ */
+static int mem_close(IOStream *io){
+ MemData *data = get_mem_data(io);
+ if(!data->err){
+ data->err = ENOTCONN;
+ }
+ return 0;
+}
+
+/** Free a memory stream.
+ *
+ * @param io mem stream
+ */
+static void mem_free(IOStream *io){
+ MemData *data = get_mem_data(io);
+ deallocate(data->buf);
+ memzero(data, sizeof(*data));
+ deallocate(data);
+}
+
+/** Allocate and initialise a memory stream.
+ *
+ * @param buf_n initial buffer size (0 means default)
+ * @param buf_max maximum buffer size (0 means no max)
+ * @return new stream (free using IOStream_close)
+ */
+IOStream *mem_stream_new_size(size_t buf_n, size_t buf_max){
+ int err = -ENOMEM;
+ MemData *data = ALLOCATE(MemData);
+ if(!data) goto exit;
+ IOStream *io = ALLOCATE(IOStream);
+ if(!io) goto exit;
+ if(buf_n <= delta_min){
+ buf_n = delta_min;
+ }
+ if(buf_max > 0 && buf_max < buf_n){
+ buf_max = buf_n;
+ }
+ data->buf = allocate(buf_n);
+ if(!data->buf) goto exit;
+ data->buf_n = buf_n;
+ data->buf_max = buf_max;
+ io->methods = &mem_methods;
+ io->data = data;
+ io->nofree = 0;
+ err = 0;
+ exit:
+ if(err){
+ deallocate(data);
+ deallocate(io);
+ io = NULL;
+ }
+ return io;
+}
diff --git a/tools/vnet/libxutil/mem_stream.h b/tools/vnet/libxutil/mem_stream.h
new file mode 100644
index 0000000000..0108966597
--- /dev/null
+++ b/tools/vnet/libxutil/mem_stream.h
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2005 Mike Wray <mike.wray@hp.com>
+ *
+ * This library is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#ifndef _XUTIL_MEM_STREAM_H_
+#define _XUTIL_MEM_STREAM_H_
+
+#include "iostream.h"
+
+extern IOStream *mem_stream_new_size(size_t buf_n, size_t buf_max);
+
+extern int mem_stream_avail(IOStream *io);
+
+static inline IOStream *mem_stream_new(void){
+ return mem_stream_new_size(0, 0);
+}
+
+#endif /* !_XUTIL_MEM_STREAM_H_ */
diff --git a/tools/vnet/vnet-module/varp_util.c b/tools/vnet/vnet-module/varp_util.c
new file mode 100644
index 0000000000..6f0f6fb72b
--- /dev/null
+++ b/tools/vnet/vnet-module/varp_util.c
@@ -0,0 +1,77 @@
+/*
+ * Copyright (C) 2005 Mike Wray <mike.wray@hp.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free software Foundation, Inc.,
+ * 59 Temple Place, suite 330, Boston, MA 02111-1307 USA
+ *
+ */
+
+static int hex16(char *s, uint16_t *val)
+{
+ int err = -EINVAL;
+ uint16_t v = 0;
+
+ for( ; *s; s++){
+ v <<= 4;
+ if('0' <= *s && *s <= '9'){
+ v |= *s - '0';
+ } else if('A' <= *s && *s <= 'F'){
+ v |= *s - 'A' + 10;
+ } else if('a' <= *s && *s <= 'f'){
+ v |= *s - 'a' + 10;
+ } else {
+ goto exit;
+ }
+ }
+ err = 0;
+ exit:
+ *val = (err ? 0 : v);
+ return err;
+}
+
+int VnetId_aton(const char *s, VnetId *vnet){
+ int err = -EINVAL;
+ const char *p, *q;
+ uint16_t v;
+ char buf[5];
+ int buf_n = sizeof(buf) - 1;
+ int i, n;
+ const int elts_n = 8;
+
+ q = s;
+ p = strchr(q, ':');
+ i = (p ? 0 : elts_n - 1);
+ do {
+ if(!p){
+ if(i < elts_n - 1) goto exit;
+ p = s + strlen(s);
+ }
+ n = p - q;
+ if(n > buf_n) goto exit;
+ memcpy(buf, q, n);
+ buf[n] = '\0';
+ err = hex16(buf, &v);
+ if(err) goto exit;
+ vnet->u.vnet16[i] = htons(v);
+ q = p+1;
+ p = strchr(q, ':');
+ i++;
+ } while(i < elts_n);
+ err = 0;
+ exit:
+ if(err){
+ *vnet = (VnetId){};
+ }
+ return err;
+}
diff --git a/tools/vnet/vnet-module/varp_util.h b/tools/vnet/vnet-module/varp_util.h
new file mode 100644
index 0000000000..f20d12a7e6
--- /dev/null
+++ b/tools/vnet/vnet-module/varp_util.h
@@ -0,0 +1,142 @@
+/*
+ * Copyright (C) 2004 Mike Wray <mike.wray@hp.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free software Foundation, Inc.,
+ * 59 Temple Place, suite 330, Boston, MA 02111-1307 USA
+ *
+ */
+#ifndef _VNET_VARP_UTIL_H
+#define _VNET_VARP_UTIL_H
+
+#include "hash_table.h"
+
+/** Size of a string buffer to store a varp address. */
+#define VARP_ADDR_BUF 56
+
+/** Size of a string buffer to store a vnet id. */
+#define VNET_ID_BUF 56
+
+#ifndef NIPQUAD
+#define NIPQUAD(addr) \
+ ((unsigned char *)&addr)[0], \
+ ((unsigned char *)&addr)[1], \
+ ((unsigned char *)&addr)[2], \
+ ((unsigned char *)&addr)[3]
+#endif
+
+#ifndef NIP6
+#define NIP6(addr) \
+ ntohs((addr).s6_addr16[0]), \
+ ntohs((addr).s6_addr16[1]), \
+ ntohs((addr).s6_addr16[2]), \
+ ntohs((addr).s6_addr16[3]), \
+ ntohs((addr).s6_addr16[4]), \
+ ntohs((addr).s6_addr16[5]), \
+ ntohs((addr).s6_addr16[6]), \
+ ntohs((addr).s6_addr16[7])
+#endif
+
+
+static inline const char *VarpAddr_ntoa(VarpAddr *addr, char buf[VARP_ADDR_BUF])
+{
+ switch(addr->family){
+ default:
+ case AF_INET:
+ sprintf(buf, "%u.%u.%u.%u",
+ NIPQUAD(addr->u.ip4));
+ break;
+ case AF_INET6:
+ sprintf(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x",
+ NIP6(addr->u.ip6));
+ break;
+ }
+ return buf;
+}
+
+static inline const char *VnetId_ntoa(VnetId *vnet, char buf[VNET_ID_BUF])
+{
+ sprintf(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x",
+ ntohs(vnet->u.vnet16[0]), \
+ ntohs(vnet->u.vnet16[1]), \
+ ntohs(vnet->u.vnet16[2]), \
+ ntohs(vnet->u.vnet16[3]), \
+ ntohs(vnet->u.vnet16[4]), \
+ ntohs(vnet->u.vnet16[5]), \
+ ntohs(vnet->u.vnet16[6]), \
+ ntohs(vnet->u.vnet16[7]));
+ return buf;
+}
+
+extern int VnetId_aton(const char *s, VnetId *vnet);
+
+/** Convert an unsigned in host order to a vnet id.
+ */
+static inline struct VnetId toVnetId(uint32_t vnetid){
+ struct VnetId vnet = {};
+ vnet.u.vnet32[3] = htonl(vnetid);
+ return vnet;
+}
+
+static inline uint32_t VnetId_hash(uint32_t h, VnetId *vnet)
+{
+ h = hash_hul(h, vnet->u.vnet32[0]);
+ h = hash_hul(h, vnet->u.vnet32[1]);
+ h = hash_hul(h, vnet->u.vnet32[2]);
+ h = hash_hul(h, vnet->u.vnet32[3]);
+ return h;
+}
+
+static inline int VnetId_eq(VnetId *vnet1, VnetId *vnet2)
+{
+ return memcmp(vnet1, vnet2, sizeof(VnetId)) == 0;
+}
+
+static inline uint32_t VarpAddr_hash(uint32_t h, VarpAddr *addr)
+{
+ h = hash_hul(h, addr->family);
+ if(addr->family == AF_INET6){
+ h = hash_hul(h, addr->u.ip6.s6_addr32[0]);
+ h = hash_hul(h, addr->u.ip6.s6_addr32[1]);
+ h = hash_hul(h, addr->u.ip6.s6_addr32[2]);
+ h = hash_hul(h, addr->u.ip6.s6_addr32[3]);
+ } else {
+ h = hash_hul(h, addr->u.ip4.s_addr);
+ }
+ return h;
+}
+
+static inline int VarpAddr_eq(VarpAddr *addr1, VarpAddr*addr2)
+{
+ return memcmp(addr1, addr2, sizeof(VarpAddr)) == 0;
+}
+
+static inline uint32_t Vmac_hash(uint32_t h, Vmac *vmac)
+{
+ h = hash_hul(h,
+ (vmac->mac[0] << 24) |
+ (vmac->mac[1] << 16) |
+ (vmac->mac[2] << 8) |
+ (vmac->mac[3] ));
+ h = hash_hul(h,
+ (vmac->mac[4] << 8) |
+ (vmac->mac[5] ));
+ return h;
+}
+
+static inline int Vmac_eq(Vmac *vmac1, Vmac *vmac2)
+{
+ return memcmp(vmac1, vmac2, sizeof(Vmac)) == 0;
+}
+
+#endif /* _VNET_VARP_UTIL_H */