ia64/xen-unstable

changeset 1891:de62fd103f99

bitkeeper revision 1.1108.1.16 (41010e2br7ouRoQI0b4MbAxS1GzgpA)

Allow specification of the domain id to use for a new domain.
author mjw@wray-m-3.hpl.hp.com
date Fri Jul 23 13:10:03 2004 +0000 (2004-07-23)
parents 277bd0f6186d
children a50fb088d983
files tools/libxc/xc_domain.c tools/python/xen/lowlevel/xc/xc.c xen/common/dom0_ops.c xen/include/hypervisor-ifs/dom0_ops.h
line diff
     1.1 --- a/tools/libxc/xc_domain.c	Fri Jul 23 10:32:31 2004 +0000
     1.2 +++ b/tools/libxc/xc_domain.c	Fri Jul 23 13:10:03 2004 +0000
     1.3 @@ -18,6 +18,7 @@ int xc_domain_create(int xc_handle,
     1.4      dom0_op_t op;
     1.5  
     1.6      op.cmd = DOM0_CREATEDOMAIN;
     1.7 +    op.u.createdomain.domain = (domid_t)*pdomid;
     1.8      op.u.createdomain.memory_kb = mem_kb;
     1.9      strncpy(op.u.createdomain.name, name, MAX_DOMAIN_NAME);
    1.10      op.u.createdomain.name[MAX_DOMAIN_NAME-1] = '\0';
     2.1 --- a/tools/python/xen/lowlevel/xc/xc.c	Fri Jul 23 10:32:31 2004 +0000
     2.2 +++ b/tools/python/xen/lowlevel/xc/xc.c	Fri Jul 23 13:10:03 2004 +0000
     2.3 @@ -44,13 +44,13 @@ static PyObject *pyxc_domain_create(PyOb
     2.4      unsigned int mem_kb = 0;
     2.5      char        *name   = "(anon)";
     2.6      int          cpu = -1;
     2.7 -    u32          dom;
     2.8 +    u32          dom = 0;
     2.9      int          ret;
    2.10  
    2.11 -    static char *kwd_list[] = { "mem_kb", "name", "cpu", NULL };
    2.12 +    static char *kwd_list[] = { "dom", "mem_kb", "name", "cpu", NULL };
    2.13  
    2.14 -    if ( !PyArg_ParseTupleAndKeywords(args, kwds, "|isi", kwd_list, 
    2.15 -                                      &mem_kb, &name, &cpu) )
    2.16 +    if ( !PyArg_ParseTupleAndKeywords(args, kwds, "|iisi", kwd_list, 
    2.17 +                                      &dom, &mem_kb, &name, &cpu) )
    2.18          return NULL;
    2.19  
    2.20      if ( (ret = xc_domain_create(xc->xc_handle, mem_kb, name, cpu, &dom)) < 0 )
    2.21 @@ -927,6 +927,7 @@ static PyMethodDef pyxc_methods[] = {
    2.22        (PyCFunction)pyxc_domain_create, 
    2.23        METH_VARARGS | METH_KEYWORDS, "\n"
    2.24        "Create a new domain.\n"
    2.25 +      " dom    [int, 0]:        Domain identifier to use (allocated if zero).\n"
    2.26        " mem_kb [int, 0]:        Memory allocation, in kilobytes.\n"
    2.27        " name   [str, '(anon)']: Informative textual name.\n\n"
    2.28        "Returns: [int] new domain identifier; -1 on error.\n" },
     3.1 --- a/xen/common/dom0_ops.c	Fri Jul 23 10:32:31 2004 +0000
     3.2 +++ b/xen/common/dom0_ops.c	Fri Jul 23 13:10:03 2004 +0000
     3.3 @@ -27,6 +27,98 @@ extern unsigned int alloc_new_dom_mem(st
     3.4  extern long arch_do_dom0_op(dom0_op_t *op, dom0_op_t *u_dom0_op);
     3.5  extern void arch_getdomaininfo_ctxt(struct domain *, full_execution_context_t *);
     3.6  
     3.7 +static inline int is_free_domid(domid_t dom)
     3.8 +{
     3.9 +    struct domain  *d;
    3.10 +
    3.11 +    if (dom >= DOMID_SELF) return 0;
    3.12 +    d = find_domain_by_id(dom);
    3.13 +    if (d == NULL) {
    3.14 +        return 1;
    3.15 +    } else {
    3.16 +        put_domain(d);
    3.17 +        return 0;
    3.18 +    }
    3.19 +}
    3.20 +
    3.21 +/** Allocate a free domain id. We try to reuse domain ids in a fairly low range,
    3.22 + * only expanding the range when there are no free domain ids. This is to
    3.23 + * keep domain ids in a range depending on the number that exist simultaneously,
    3.24 + * rather than incrementing domain ids in the full 32-bit range.
    3.25 + */
    3.26 +static int allocate_domid(domid_t *pdom)
    3.27 +{
    3.28 +    static spinlock_t domid_lock = SPIN_LOCK_UNLOCKED;
    3.29 +    static domid_t curdom = 0;
    3.30 +    static domid_t topdom = 101;
    3.31 +    int err = 0;
    3.32 +    domid_t cur, dom, top;
    3.33 +
    3.34 +    /* Try to use a domain id in the range 0..topdom, starting at curdom. */
    3.35 +    spin_lock(&domid_lock);
    3.36 +    cur = curdom;
    3.37 +    dom = curdom;
    3.38 +    top = topdom;
    3.39 +    spin_unlock(&domid_lock);
    3.40 +    do {
    3.41 +        ++dom;
    3.42 +        if (dom == top) {
    3.43 +            dom = 1;
    3.44 +        }
    3.45 +        if (is_free_domid(dom)) goto exit;
    3.46 +    } while (dom != cur);
    3.47 +    /* Couldn't find a free domain id in 0..topdom, try higher. */
    3.48 +    for (dom = top; dom < DOMID_SELF; dom++) {
    3.49 +        if(is_free_domid(dom)) goto exit;
    3.50 +    }
    3.51 +    /* No free domain ids. */
    3.52 +    err = -ENOMEM;
    3.53 +  exit:
    3.54 +    if (err == 0) {
    3.55 +        spin_lock(&domid_lock);
    3.56 +        curdom = dom;
    3.57 +        if (dom >= topdom) {
    3.58 +            topdom = dom + 1;
    3.59 +        }
    3.60 +        spin_unlock(&domid_lock);
    3.61 +        *pdom = dom;
    3.62 +    }
    3.63 +    return err;
    3.64 +}
    3.65 +
    3.66 +#if 0
    3.67 +        struct domain    *d;
    3.68 +        static domid_t    domnr = 0;
    3.69 +        static spinlock_t domnr_lock = SPIN_LOCK_UNLOCKED;
    3.70 +        unsigned int      pro;
    3.71 +        domid_t           dom;
    3.72 +
    3.73 +        ret = -ENOMEM;
    3.74 +
    3.75 +        if(op->u.createdomain.domain > 0){
    3.76 +            d = find_domain_by_id(dom);
    3.77 +            if(d){
    3.78 +                put_domain(d);
    3.79 +                ret = -EINVAL;
    3.80 +                break;
    3.81 +            }
    3.82 +        } else {
    3.83 +            /* Search for an unused domain identifier. */
    3.84 +            for ( ; ; )
    3.85 +            {
    3.86 +                spin_lock(&domnr_lock);
    3.87 +                /* Wrap the roving counter when we reach first special value. */
    3.88 +                if ( (dom = ++domnr) == DOMID_SELF )
    3.89 +                    dom = domnr = 1;
    3.90 +                spin_unlock(&domnr_lock);
    3.91 +                
    3.92 +                if ( (d = find_domain_by_id(dom)) == NULL )
    3.93 +                    break;
    3.94 +                put_domain(d);
    3.95 +             }
    3.96 +        }
    3.97 +#endif
    3.98 +
    3.99  long do_dom0_op(dom0_op_t *u_dom0_op)
   3.100  {
   3.101      long ret = 0;
   3.102 @@ -101,25 +193,24 @@ long do_dom0_op(dom0_op_t *u_dom0_op)
   3.103      case DOM0_CREATEDOMAIN:
   3.104      {
   3.105          struct domain    *d;
   3.106 -        static domid_t    domnr = 0;
   3.107 -        static spinlock_t domnr_lock = SPIN_LOCK_UNLOCKED;
   3.108          unsigned int      pro;
   3.109          domid_t           dom;
   3.110  
   3.111          ret = -ENOMEM;
   3.112  
   3.113 -        /* Search for an unused domain identifier. */
   3.114 -        for ( ; ; )
   3.115 +        dom = op->u.createdomain.domain;
   3.116 +        if ( 0 < dom && dom < DOMID_SELF )
   3.117          {
   3.118 -            spin_lock(&domnr_lock);
   3.119 -            /* Wrap the roving counter when we reach first special value. */
   3.120 -            if ( (dom = ++domnr) == DOMID_SELF )
   3.121 -                dom = domnr = 1;
   3.122 -            spin_unlock(&domnr_lock);
   3.123 -
   3.124 -            if ( (d = find_domain_by_id(dom)) == NULL )
   3.125 +            if ( !is_free_domid(dom) )
   3.126 +            {
   3.127 +                ret = -EINVAL;
   3.128                  break;
   3.129 -            put_domain(d);
   3.130 +            }
   3.131 +        }
   3.132 +        else
   3.133 +        {
   3.134 +            ret = allocate_domid(&dom);
   3.135 +            if ( ret ) break;
   3.136          }
   3.137  
   3.138          if ( op->u.createdomain.cpu == -1 )
     4.1 --- a/xen/include/hypervisor-ifs/dom0_ops.h	Fri Jul 23 10:32:31 2004 +0000
     4.2 +++ b/xen/include/hypervisor-ifs/dom0_ops.h	Fri Jul 23 13:10:03 2004 +0000
     4.3 @@ -55,8 +55,10 @@ typedef struct {
     4.4      u8           name[MAX_DOMAIN_NAME]; /*  8 */
     4.5      u32          cpu;                 /* 24 */
     4.6      u32          __pad;               /* 28 */
     4.7 +    /* IN/OUT parameters. */
     4.8 +    /* If 0, domain is allocated. If non-zero use it unless in use. */
     4.9 +    domid_t      domain;              /* 32 */
    4.10      /* OUT parameters. */
    4.11 -    domid_t      domain;              /* 32 */
    4.12  } PACKED dom0_createdomain_t; /* 36 bytes */
    4.13  
    4.14  #define DOM0_DESTROYDOMAIN     9