ia64/xen-unstable

changeset 15278:c5cf3942b5da

[ACM] Python-to-C Interface for new ACM hypercalls.

Provide an interface for using recently-added Xen functionality from
python.

Signed-off-by: Stefan Berger <stefanb@us.ibm.com>
author kfraser@localhost.localdomain
date Tue Jun 05 15:51:59 2007 +0100 (2007-06-05)
parents 0feaf2fc75d3
children faef0f795490
files tools/python/xen/lowlevel/acm/acm.c
line diff
     1.1 --- a/tools/python/xen/lowlevel/acm/acm.c	Tue Jun 05 10:28:59 2007 +0100
     1.2 +++ b/tools/python/xen/lowlevel/acm/acm.c	Tue Jun 05 15:51:59 2007 +0100
     1.3 @@ -1,10 +1,11 @@
     1.4  /****************************************************************
     1.5   * acm.c
     1.6   *
     1.7 - * Copyright (C) 2006 IBM Corporation
     1.8 + * Copyright (C) 2006,2007 IBM Corporation
     1.9   *
    1.10   * Authors:
    1.11   * Reiner Sailer <sailer@watson.ibm.com>
    1.12 + * Stefan Berger <stefanb@us.ibm.com>
    1.13   *
    1.14   * This program is free software; you can redistribute it and/or
    1.15   * modify it under the terms of the GNU General Public License as
    1.16 @@ -197,13 +198,120 @@ static PyObject *getdecision(PyObject * 
    1.17      return Py_BuildValue("s", decision);
    1.18  }
    1.19  
    1.20 +/* error messages for exceptions */
    1.21 +const char bad_arg[] = "Bad function argument.";
    1.22 +const char ctrlif_op[] = "Could not open control interface.";
    1.23 +const char hv_op_err[] = "Error from hypervisor operation.";
    1.24 +
    1.25 +
    1.26 +static PyObject *chgpolicy(PyObject *self, PyObject *args)
    1.27 +{
    1.28 +    struct acm_change_policy chgpolicy;
    1.29 +    int xc_handle, rc;
    1.30 +    char *bin_pol = NULL, *del_arr = NULL, *chg_arr = NULL;
    1.31 +    int bin_pol_len = 0, del_arr_len = 0, chg_arr_len = 0;
    1.32 +    uint errarray_mbrs = 20 * 2;
    1.33 +    uint32_t error_array[errarray_mbrs];
    1.34 +    PyObject *result;
    1.35 +    uint len;
    1.36 +
    1.37 +    memset(&chgpolicy, 0x0, sizeof(chgpolicy));
    1.38 +
    1.39 +    if (!PyArg_ParseTuple(args, "s#s#s#" ,&bin_pol, &bin_pol_len,
    1.40 +                                          &del_arr, &del_arr_len,
    1.41 +                                          &chg_arr, &chg_arr_len)) {
    1.42 +        PyErr_SetString(PyExc_TypeError, bad_arg);
    1.43 +        return NULL;
    1.44 +    }
    1.45 +
    1.46 +    chgpolicy.policy_pushcache_size = bin_pol_len;
    1.47 +    chgpolicy.delarray_size = del_arr_len;
    1.48 +    chgpolicy.chgarray_size = chg_arr_len;
    1.49 +    chgpolicy.errarray_size = sizeof(error_array);
    1.50 +
    1.51 +    set_xen_guest_handle(chgpolicy.policy_pushcache, bin_pol);
    1.52 +    set_xen_guest_handle(chgpolicy.del_array, del_arr);
    1.53 +    set_xen_guest_handle(chgpolicy.chg_array, chg_arr);
    1.54 +    set_xen_guest_handle(chgpolicy.err_array, error_array);
    1.55 +
    1.56 +    if ((xc_handle = xc_interface_open()) <= 0) {
    1.57 +        PyErr_SetString(PyExc_IOError, ctrlif_op);
    1.58 +        return NULL;
    1.59 +    }
    1.60 +
    1.61 +    rc = xc_acm_op(xc_handle, ACMOP_chgpolicy, &chgpolicy, sizeof(chgpolicy));
    1.62 +
    1.63 +    xc_interface_close(xc_handle);
    1.64 +
    1.65 +    /* only pass the filled error codes */
    1.66 +    for (len = 0; (len + 1) < errarray_mbrs; len += 2) {
    1.67 +        if (error_array[len] == 0) {
    1.68 +            len *= sizeof(error_array[0]);
    1.69 +            break;
    1.70 +        }
    1.71 +    }
    1.72 +
    1.73 +    result = Py_BuildValue("is#", rc, error_array, len);
    1.74 +    return result;
    1.75 +}
    1.76 +
    1.77 +
    1.78 +static PyObject *relabel_domains(PyObject *self, PyObject *args)
    1.79 +{
    1.80 +    struct acm_relabel_doms reldoms;
    1.81 +    int xc_handle, rc;
    1.82 +    char *relabel_rules = NULL;
    1.83 +    int rel_rules_len = 0;
    1.84 +    uint errarray_mbrs = 20 * 2;
    1.85 +    uint32_t error_array[errarray_mbrs];
    1.86 +    PyObject *result;
    1.87 +    uint len;
    1.88 +
    1.89 +    memset(&reldoms, 0x0, sizeof(reldoms));
    1.90 +
    1.91 +    if (!PyArg_ParseTuple(args, "s#" ,&relabel_rules, &rel_rules_len)) {
    1.92 +        PyErr_SetString(PyExc_TypeError, bad_arg);
    1.93 +        return NULL;
    1.94 +    }
    1.95 +
    1.96 +    reldoms.relabel_map_size = rel_rules_len;
    1.97 +    reldoms.errarray_size = sizeof(error_array);
    1.98 +
    1.99 +    set_xen_guest_handle(reldoms.relabel_map, relabel_rules);
   1.100 +    set_xen_guest_handle(reldoms.err_array, error_array);
   1.101 +
   1.102 +    if ((xc_handle = xc_interface_open()) <= 0) {
   1.103 +        PyErr_SetString(PyExc_IOError, ctrlif_op);
   1.104 +        return NULL;
   1.105 +    }
   1.106 +
   1.107 +    rc = xc_acm_op(xc_handle, ACMOP_relabeldoms, &reldoms, sizeof(reldoms));
   1.108 +
   1.109 +    xc_interface_close(xc_handle);
   1.110 +
   1.111 +
   1.112 +    /* only pass the filled error codes */
   1.113 +    for (len = 0; (len + 1) < errarray_mbrs; len += 2) {
   1.114 +        if (error_array[len] == 0) {
   1.115 +            len *= sizeof(error_array[0]);
   1.116 +            break;
   1.117 +        }
   1.118 +    }
   1.119 +
   1.120 +    result = Py_BuildValue("is#", rc, error_array, len);
   1.121 +    return result;
   1.122 +}
   1.123 +
   1.124 +
   1.125  /*=================General Python Extension Declarations=================*/
   1.126  
   1.127  /* methods */
   1.128  static PyMethodDef acmMethods[] = {
   1.129 -    {"policy", policy, METH_VARARGS, "Retrieve Active ACM Policy Reference Name"},
   1.130 -    {"getssid", getssid, METH_VARARGS, "Retrieve label information and ssidref for a domain"},
   1.131 +    {"policy",      policy,      METH_VARARGS, "Retrieve Active ACM Policy Reference Name"},
   1.132 +    {"getssid",     getssid,     METH_VARARGS, "Retrieve label information and ssidref for a domain"},
   1.133      {"getdecision", getdecision, METH_VARARGS, "Retrieve ACM access control decision"},
   1.134 +    {"chgpolicy",   chgpolicy,   METH_VARARGS, "Change the policy in one step"},
   1.135 +    {"relabel_domains", relabel_domains, METH_VARARGS, "Relabel domains"},
   1.136      /* end of list (extend list above this line) */
   1.137      {NULL, NULL, 0, NULL}
   1.138  };