This commit is contained in:
emellor@leeni.uk.xensource.com committed 2005-12-13 10:57:18 +00:00
commit a9b9f0585c
28 files changed
+210 -192

No files matched your search

+23 -38
View File
@@ -19,7 +19,6 @@
import sys
import threading
import select
import socket
import fcntl
@@ -31,21 +30,17 @@ for TCP and unix-domain sockets (see tcp.py and unix.py).
"""
BUFFER_SIZE = 1024
BACKLOG = 5
class SocketServerConnection:
"""An accepted connection to a server.
"""
def __init__(self, sock, protocol, addr, server):
def __init__(self, sock, protocol_class):
self.sock = sock
self.protocol = protocol
self.addr = addr
self.server = server
self.protocol = protocol_class()
self.protocol.setTransport(self)
def run(self):
threading.Thread(target=self.main).start()
@@ -68,6 +63,10 @@ class SocketServerConnection:
pass
def close(self):
self.sock.close()
def write(self, data):
self.sock.send(data)
@@ -77,52 +76,38 @@ class SocketListener:
Accepts connections and runs a thread for each one.
"""
def __init__(self, protocol_class, backlog=None):
if backlog is None:
backlog = 5
def __init__(self, protocol_class, hosts_allow = ''):
self.protocol_class = protocol_class
self.sock = None
self.backlog = backlog
self.thread = None
self.sock = self.createSocket()
threading.Thread(target=self.main).start()
def close(self):
try:
self.sock.close()
except:
pass
def createSocket(self):
raise NotImplementedError()
def setCloExec(self):
fcntl.fcntl(self.sock.fileno(), fcntl.F_SETFD, fcntl.FD_CLOEXEC)
def acceptConnection(self, sock, protocol, addr):
return SocketServerConnection(sock, protocol, addr, self)
def listen(self):
if self.sock or self.thread:
raise IOError("already listening")
self.sock = self.createSocket()
self.sock.listen(self.backlog)
self.run()
def run(self):
self.thread = threading.Thread(target=self.main)
self.thread.start()
raise NotImplementedError()
def main(self):
try:
fcntl.fcntl(self.sock.fileno(), fcntl.F_SETFD, fcntl.FD_CLOEXEC)
self.sock.listen(BACKLOG)
while True:
try:
(sock, addr) = self.sock.accept()
self.acceptConnection(sock, self.protocol_class(),
addr).run()
self.acceptConnection(sock, addr)
except socket.error, ex:
if ex.args[0] not in (EWOULDBLOCK, EAGAIN, EINTR):
break
finally:
try:
self.sock.close()
except:
pass
self.close()
+29 -12
View File
@@ -17,20 +17,25 @@
#============================================================================
import errno
import re
import socket
import time
import errno
from connection import *
import connection
from xen.xend.XendLogging import log
class TCPListener(SocketListener):
class TCPListener(connection.SocketListener):
def __init__(self, port, protocol, backlog=None, interface=''):
SocketListener.__init__(self, protocol, backlog=backlog)
def __init__(self, protocol_class, port, interface, hosts_allow):
self.port = port
self.interface = interface
self.hosts_allow = hosts_allow
connection.SocketListener.__init__(self, protocol_class)
def createSocket(self):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
@@ -49,11 +54,23 @@ class TCPListener(SocketListener):
else:
raise
def acceptConnection(self, sock, protocol, addr):
return SocketServerConnection(sock, protocol, addr, self)
def acceptConnection(self, sock, addrport):
addr = addrport[0]
if self.hosts_allow is None:
connection.SocketServerConnection(sock, self.protocol_class)
else:
fqdn = socket.getfqdn(addr)
for h in self.hosts_allow:
if h.match(fqdn) or h.match(addr):
log.debug("Match %s %s", fqdn, h.pattern)
connection.SocketServerConnection(sock,
self.protocol_class)
return
def listenTCP(port, protocol, interface='', backlog=None):
l = TCPListener(port, protocol, interface=interface, backlog=backlog)
l.listen()
l.setCloExec()
try:
log.warn("Rejected connection from %s:%d (%s) for port %d.",
addr, addrport[1], fqdn, self.port)
sock.close()
except:
pass
+8 -11
View File
@@ -21,15 +21,15 @@ import socket
import os
import os.path
from connection import *
import connection
class UnixListener(SocketListener):
def __init__(self, path, protocol, backlog=None):
SocketListener.__init__(self, protocol, backlog=backlog)
class UnixListener(connection.SocketListener):
def __init__(self, path, protocol_class):
self.path = path
connection.SocketListener.__init__(self, protocol_class)
def createSocket(self):
pathdir = os.path.dirname(self.path)
if not os.path.exists(pathdir):
@@ -45,9 +45,6 @@ class UnixListener(SocketListener):
sock.bind(self.path)
return sock
def acceptConnection(self, sock, protocol, addr):
return SocketServerConnection(sock, protocol, self.path, self)
def listenUNIX(path, protocol, backlog=None):
UnixListener(path, protocol, backlog=backlog).listen()
def acceptConnection(self, sock, _):
connection.SocketServerConnection(sock, self.protocol_class)
+6
View File
@@ -75,6 +75,8 @@ class XendRoot:
"""Default port xend serves relocation at. """
xend_relocation_port_default = '8002'
xend_relocation_hosts_allow_default = ''
"""Default for the flag indicating whether xend should run a unix-domain server."""
xend_unix_server_default = 'yes'
@@ -194,6 +196,10 @@ class XendRoot:
"""
return self.get_config_int('xend-relocation-port', self.xend_relocation_port_default)
def get_xend_relocation_hosts_allow(self):
return self.get_config_value("xend-relocation-hosts-allow",
self.xend_relocation_hosts_allow_default)
def get_xend_address(self):
"""Get the address xend listens at for its HTTP port.
This defaults to the empty string which allows all hosts to connect.
+4 -3
View File
@@ -16,10 +16,10 @@ import traceback
import xen.lowlevel.xc
from xen.xend.server import SrvServer
from xen.xend.XendLogging import log
import relocate
import SrvServer
from params import *
@@ -167,8 +167,9 @@ class Daemon:
while True:
pid = self.fork_pid()
if pid:
os.close(w)
w = False
if w is not None:
os.close(w)
w = None
(_, status) = os.waitpid(pid, 0)
+1 -1
View File
@@ -40,6 +40,7 @@
# todo Support command-line args.
import fcntl
import time
from threading import Thread
from xen.web.httpserver import HttpServer, UnixHttpServer
@@ -48,7 +49,6 @@ from xen.xend import XendRoot
from xen.xend import Vifctl
from xen.xend.XendLogging import log
from xen.web.SrvDir import SrvDir
import time
from SrvRoot import SrvRoot
+11 -2
View File
@@ -16,6 +16,7 @@
# Copyright (C) 2005 XenSource Ltd
#============================================================================
import re
import sys
import StringIO
@@ -116,8 +117,16 @@ def listenRelocation():
xroot = XendRoot.instance()
if xroot.get_xend_unix_server():
path = '/var/lib/xend/relocation-socket'
unix.listenUNIX(path, RelocationProtocol)
unix.UnixListener(path, RelocationProtocol)
if xroot.get_xend_relocation_server():
port = xroot.get_xend_relocation_port()
interface = xroot.get_xend_relocation_address()
tcp.listenTCP(port, RelocationProtocol, interface=interface)
hosts_allow = xroot.get_xend_relocation_hosts_allow()
if hosts_allow == '':
hosts_allow = None
else:
hosts_allow = map(re.compile, hosts_allow.split(" "))
tcp.TCPListener(RelocationProtocol, port, interface = interface,
hosts_allow = hosts_allow)
+9 -6
View File
@@ -696,13 +696,16 @@ def to_string(sxpr):
io.close()
return val
def from_string(str):
def from_string(s):
"""Create an sxpr by parsing a string.
str string
s string
returns sxpr
"""
io = StringIO(str)
if s == '':
return []
io = StringIO(s)
vals = parse(io)
if vals is []:
return None
@@ -710,13 +713,13 @@ def from_string(str):
return vals[0]
def all_from_string(str):
def all_from_string(s):
"""Create an sxpr list by parsing a string.
str string
s string
returns sxpr list
"""
io = StringIO(str)
io = StringIO(s)
vals = parse(io)
return vals
+50 -71
View File
@@ -253,17 +253,12 @@ gopts.var('usb', val='PATH',
use="""Add a physical USB port to a domain, as specified by the path
to that port. This option may be repeated to add more than one port.""")
gopts.var('ipaddr', val="IPADDR",
fn=append_value, default=[],
use="Add an IP address to the domain.")
gopts.var('vif', val="type=TYPE,mac=MAC,be_mac=MAC,bridge=BRIDGE,script=SCRIPT,backend=DOM,vifname=NAME",
gopts.var('vif', val="type=TYPE,mac=MAC,bridge=BRIDGE,ip=IPADDR,script=SCRIPT,backend=DOM,vifname=NAME",
fn=append_value, default=[],
use="""Add a network interface with the given MAC address and bridge.
The vif is configured by calling the given configuration script.
If type is not specified, default is netfront not ioemu device.
If mac is not specified a random MAC address is used.
The MAC address of the backend interface can be selected with be_mac.
If not specified then the network backend chooses it's own MAC address.
If bridge is not specified the first bridge found is used.
If script is not specified the default script is used.
@@ -284,8 +279,10 @@ gopts.var('vtpm', val="instance=INSTANCE,backend=DOM",
given domain.""")
gopts.var('nics', val="NUM",
fn=set_int, default=1,
use="""Set the number of network interfaces.
fn=set_int, default=-1,
use="""DEPRECATED. Use empty vif entries instead.
Set the number of network interfaces.
Use the vif option to define interface parameters, otherwise
defaults are used. Specifying vifs will increase the
number of interfaces as needed.""")
@@ -487,58 +484,38 @@ def configure_vtpm(config_devs, vals):
config_vtpm.append(['backend', backend])
config_devs.append(['device', config_vtpm])
def configure_vifs(config_devs, vals):
"""Create the config for virtual network interfaces.
"""
vifs = vals.vif
vifs_n = max(vals.nics, len(vifs))
for idx in range(0, vifs_n):
if idx < len(vifs):
d = vifs[idx]
mac = d.get('mac')
be_mac = d.get('be_mac')
bridge = d.get('bridge')
script = d.get('script')
backend = d.get('backend')
ip = d.get('ip')
vifname = d.get('vifname')
type = d.get('type')
else:
mac = None
be_mac = None
bridge = None
script = None
backend = None
ip = None
vifname = None
type = None
vifs = vals.vif
vifs_n = len(vifs)
if hasattr(vals, 'nics'):
if vals.nics > 0:
warn("The nics option is deprecated. Please use an empty vif "
"entry instead:\n\n vif = [ '' ]\n")
for _ in range(vifs_n, vals.nics):
vifs.append('')
vifs_n = len(vifs)
elif vals.nics == 0:
warn("The nics option is deprecated. Please remove it.")
for c in vifs:
d = comma_sep_kv_to_dict(c)
config_vif = ['vif']
if mac:
config_vif.append(['mac', mac])
if vifname:
config_vif.append(['vifname', vifname])
if be_mac:
config_vif.append(['be_mac', be_mac])
if bridge:
config_vif.append(['bridge', bridge])
if script:
config_vif.append(['script', script])
if backend:
config_vif.append(['backend', backend])
if ip:
config_vif.append(['ip', ip])
if type:
config_vif.append(['type', type])
def f(k):
if k not in ['backend', 'bridge', 'ip', 'mac', 'script', 'type',
'vifname']:
err('Invalid vif option: ' + k)
config_vif.append([k, d[k]])
map(f, d.keys())
config_devs.append(['device', config_vif])
def configure_vfr(config, vals):
if not vals.ipaddr: return
config_vfr = ['vfr']
idx = 0 # No way of saying which IP is for which vif?
for ip in vals.ipaddr:
config_vfr.append(['vif', ['id', idx], ['ip', ip]])
config.append(config_vfr)
def configure_vmx(config_image, vals):
"""Create the config for VMX devices.
@@ -648,22 +625,6 @@ def preprocess_ioports(vals):
ioports.append(hexd)
vals.ioports = ioports
def preprocess_vifs(vals):
if not vals.vif: return
vifs = []
for vif in vals.vif:
d = {}
a = vif.split(',')
for b in a:
(k, v) = b.strip().split('=', 1)
k = k.strip()
v = v.strip()
if k not in ['type', 'mac', 'be_mac', 'bridge', 'script', 'backend', 'ip', 'vifname']:
err('Invalid vif specifier: ' + vif)
d[k] = v
vifs.append(d)
vals.vif = vifs
def preprocess_vtpm(vals):
if not vals.vtpm: return
vtpms = []
@@ -765,12 +726,30 @@ def preprocess(vals):
preprocess_disk(vals)
preprocess_pci(vals)
preprocess_ioports(vals)
preprocess_vifs(vals)
preprocess_ip(vals)
preprocess_nfs(vals)
preprocess_vnc(vals)
preprocess_vtpm(vals)
def comma_sep_kv_to_dict(c):
"""Convert comma-separated, equals-separated key-value pairs into a
dictionary.
"""
d = {}
c = c.strip()
if len(c) > 0:
a = c.split(',')
for b in a:
if b.find('=') == -1:
err("%s should be a pair, separated by an equals sign." % b)
(k, v) = b.split('=', 1)
k = k.strip()
v = v.strip()
d[k] = v
return d
def make_domain(opts, config):
"""Create, build and start a domain.
+5 -4
View File
@@ -36,7 +36,7 @@ class test_create(unittest.TestCase):
(fd, fname) = tempfile.mkstemp()
os.close(fd)
self.t('-f %s kernel=/mykernel display=fakedisplay '
'macaddr=ab:cd:ef:ed nics=0' % fname,
'macaddr=ab:cd:ef:ed' % fname,
{ 'name' : os.path.basename(fname),
'xm_file' : fname,
'defconfig' : fname,
@@ -50,6 +50,7 @@ class test_create(unittest.TestCase):
'interface' : 'eth0',
'path' : '.:/etc/xen',
'builder' : 'linux',
'nics' : -1,
})
@@ -62,7 +63,6 @@ kernel = "/boot/vmlinuz-xenU-smp"
memory = 768
name = "dom1"
vcpus = 4
nics = 1
disk = ['phy:/dev/virt-blkdev-backend/dom1,sda1,w',
'phy:/dev/virt-blkdev-backend/usr,sda2,r']
root = "/dev/sda1 ro"
@@ -79,7 +79,7 @@ on_crash = 'destroy'
'memory' : 768,
'name' : 'dom1',
'vcpus' : 4,
'nics' : 1,
'nics' : -1,
'root' : '/dev/sda1 ro',
'extra' : ' profile=1 GATEWAY=10.0.1.254 NETMASK=255.255.0.0 IPADDR=10.0.134.1 HOSTNAME=dom1',
'on_poweroff' : 'destroy',
@@ -118,7 +118,7 @@ cpu_weight = 0.75
finally:
os.close(fd)
self.t('-f %s display=fakedisplay macaddr=ab:cd:ef:ed nics=0' %
self.t('-f %s display=fakedisplay macaddr=ab:cd:ef:ed' %
fname,
{ 'name' : 'testname',
'xm_file' : fname,
@@ -137,6 +137,7 @@ cpu_weight = 0.75
'interface' : 'eth0',
'path' : '.:/etc/xen',
'builder' : 'linux',
'nics' : -1,
})
Reference in new issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.