diff options
author | Arnaldo Carvalho de Melo <acme@redhat.com> | 2008-07-15 11:07:33 -0300 |
---|---|---|
committer | Arnaldo Carvalho de Melo <acme@redhat.com> | 2008-07-15 11:07:33 -0300 |
commit | 824f72f88ead0a623ec844b65e58defb3f4d1231 (patch) | |
tree | cb7a7981561c6b66295df780839af33c9b33c503 /python-ethtool/ethtool.c | |
parent | 0da7f23709e41c14dded246781dafdf663a24f36 (diff) | |
download | python-ethtool-824f72f88ead0a623ec844b65e58defb3f4d1231.tar.gz python-ethtool-824f72f88ead0a623ec844b65e58defb3f4d1231.tar.xz python-ethtool-824f72f88ead0a623ec844b65e58defb3f4d1231.zip |
ethtool: binding for ETHTOOL_SCOALESCE
And support in pethtool (aka ethtool-cmd.py) for setting all the coalesce
parameters, providing, as usual, an interface that mimics the one provided by
the ethtool command.
This cset also introduces struct_desc_from_dict, that will help with other dict
based python bindings, not just in python-ethtool. Please let me know if I'm
reinventing the wheel, i.e. if there are other Python dict to C struct
facilities out there (I bet there is, but heck, this one was easy enough to
implement and doesn't requires external support to get this done 8)).
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Diffstat (limited to 'python-ethtool/ethtool.c')
-rw-r--r-- | python-ethtool/ethtool.c | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/python-ethtool/ethtool.c b/python-ethtool/ethtool.c index 4dd8fdf..6ce5208 100644 --- a/python-ethtool/ethtool.c +++ b/python-ethtool/ethtool.c @@ -603,6 +603,44 @@ free_dict: #define struct_desc_create_dict(table, values) \ __struct_desc_create_dict(table, ARRAY_SIZE(table), values) +static int __struct_desc_from_dict(struct struct_desc *table, + int nr_entries, void *to, PyObject *dict) +{ + char buf[2048]; + int i; + + for (i = 0; i < nr_entries; ++i) { + struct struct_desc *d = &table[i]; + void *val = to + d->offset; + PyObject *obj; + + switch (d->size) { + case sizeof(uint32_t): + obj = PyDict_GetItemString(dict, d->name); + if (obj == NULL) { + snprintf(buf, sizeof(buf), + "Missing dict entry for field %s", + d->name); + PyErr_SetString(PyExc_IOError, buf); + return -1; + } + *(uint32_t *)val = PyInt_AsLong(obj); + break; + default: + snprintf(buf, sizeof(buf), + "Invalid type size %d for field %s", + d->size, d->name); + PyErr_SetString(PyExc_IOError, buf); + return -1; + } + } + + return 0; +} + +#define struct_desc_from_dict(table, to, dict) \ + __struct_desc_from_dict(table, ARRAY_SIZE(table), to, dict) + static PyObject *get_coalesce(PyObject *self __unused, PyObject *args) { struct ethtool_coalesce coal; @@ -613,6 +651,25 @@ static PyObject *get_coalesce(PyObject *self __unused, PyObject *args) return struct_desc_create_dict(ethtool_coalesce_desc, &coal); } +static PyObject *set_coalesce(PyObject *self __unused, PyObject *args) +{ + struct ethtool_coalesce coal; + char *devname; + PyObject *dict; + + if (!PyArg_ParseTuple(args, "sO", &devname, &dict)) + return NULL; + + if (struct_desc_from_dict(ethtool_coalesce_desc, &coal, dict) != 0) + return NULL; + + if (send_command(ETHTOOL_SCOALESCE, devname, &coal)) + return NULL; + + Py_INCREF(Py_None); + return Py_None; +} + static struct PyMethodDef PyEthModuleMethods[] = { { .ml_name = "get_module", @@ -650,6 +707,11 @@ static struct PyMethodDef PyEthModuleMethods[] = { .ml_flags = METH_VARARGS, }, { + .ml_name = "set_coalesce", + .ml_meth = (PyCFunction)set_coalesce, + .ml_flags = METH_VARARGS, + }, + { .ml_name = "get_devices", .ml_meth = (PyCFunction)get_devices, .ml_flags = METH_VARARGS, |