Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 16 | #include <Python.h> |
Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 17 | #include "../pneum/pneum.h" |
Dave Barach | 557d128 | 2016-11-10 14:22:49 -0500 | [diff] [blame] | 18 | #include <vppinfra/hash.h> |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 19 | |
| 20 | static PyObject *pneum_callback = NULL; |
| 21 | |
Dave Barach | 557d128 | 2016-11-10 14:22:49 -0500 | [diff] [blame] | 22 | static void |
| 23 | wrap_pneum_callback (unsigned char * data, int len) |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 24 | { |
| 25 | PyGILState_STATE gstate; |
| 26 | PyObject *result;//, *arglist; |
| 27 | |
| 28 | gstate = PyGILState_Ensure(); |
| 29 | |
| 30 | /* Time to call the callback */ |
Ole Troan | d06b9f9 | 2016-04-25 13:11:19 +0200 | [diff] [blame] | 31 | #if PY_VERSION_HEX >= 0x03000000 |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 32 | result = PyObject_CallFunction(pneum_callback, "y#", data, len); |
Ole Troan | d06b9f9 | 2016-04-25 13:11:19 +0200 | [diff] [blame] | 33 | #else |
| 34 | result = PyObject_CallFunction(pneum_callback, "s#", data, len); |
| 35 | #endif |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 36 | if (result) |
| 37 | Py_DECREF(result); |
| 38 | else |
| 39 | PyErr_Print(); |
| 40 | |
| 41 | PyGILState_Release(gstate); |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | static PyObject * |
| 45 | wrap_connect (PyObject *self, PyObject *args) |
| 46 | { |
Ole Troan | b8602b5 | 2016-10-05 11:10:50 +0200 | [diff] [blame] | 47 | char * name, * chroot_prefix = NULL; |
Dave Barach | f952692 | 2017-01-06 16:33:06 -0500 | [diff] [blame] | 48 | int rx_qlen=32; /* default rx queue length */ |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 49 | int rv; |
Dave Barach | 557d128 | 2016-11-10 14:22:49 -0500 | [diff] [blame] | 50 | PyObject * temp = NULL; |
| 51 | pneum_callback_t cb = NULL; |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 52 | |
Dave Barach | f952692 | 2017-01-06 16:33:06 -0500 | [diff] [blame] | 53 | if (!PyArg_ParseTuple(args, "s|Ois:wrap_connect", |
| 54 | &name, &temp, &rx_qlen, &chroot_prefix)) |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 55 | return (NULL); |
Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 56 | |
Dave Barach | 557d128 | 2016-11-10 14:22:49 -0500 | [diff] [blame] | 57 | if (temp) |
| 58 | { |
| 59 | if (!PyCallable_Check(temp)) |
| 60 | { |
| 61 | PyErr_SetString(PyExc_TypeError, "parameter must be callable"); |
| 62 | return NULL; |
| 63 | } |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 64 | |
Dave Barach | 557d128 | 2016-11-10 14:22:49 -0500 | [diff] [blame] | 65 | Py_XINCREF(temp); /* Add a reference to new callback */ |
| 66 | Py_XDECREF(pneum_callback); /* Dispose of previous callback */ |
| 67 | pneum_callback = temp; /* Remember new callback */ |
| 68 | cb = wrap_pneum_callback; |
| 69 | } |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 70 | Py_BEGIN_ALLOW_THREADS |
Dave Barach | f952692 | 2017-01-06 16:33:06 -0500 | [diff] [blame] | 71 | rv = pneum_connect(name, chroot_prefix, cb, rx_qlen); |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 72 | Py_END_ALLOW_THREADS |
| 73 | return PyLong_FromLong(rv); |
| 74 | } |
| 75 | |
| 76 | static PyObject * |
| 77 | wrap_disconnect (PyObject *self, PyObject *args) |
| 78 | { |
| 79 | int rv; |
| 80 | Py_BEGIN_ALLOW_THREADS |
| 81 | rv = pneum_disconnect(); |
| 82 | Py_END_ALLOW_THREADS |
| 83 | return PyLong_FromLong(rv); |
| 84 | } |
| 85 | static PyObject * |
| 86 | wrap_write (PyObject *self, PyObject *args) |
| 87 | { |
| 88 | char *data; |
| 89 | int len, rv; |
| 90 | |
Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 91 | if (!PyArg_ParseTuple(args, "s#", &data, &len)) |
| 92 | return NULL; |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 93 | Py_BEGIN_ALLOW_THREADS |
| 94 | rv = pneum_write(data, len); |
| 95 | Py_END_ALLOW_THREADS |
| 96 | |
| 97 | return PyLong_FromLong(rv); |
| 98 | } |
| 99 | |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 100 | static PyObject * |
| 101 | wrap_read (PyObject *self, PyObject *args) |
| 102 | { |
| 103 | char *data; |
| 104 | int len, rv; |
| 105 | |
| 106 | Py_BEGIN_ALLOW_THREADS |
| 107 | rv = pneum_read(&data, &len); |
| 108 | Py_END_ALLOW_THREADS |
| 109 | |
| 110 | if (rv != 0) { Py_RETURN_NONE; } |
Ole Troan | d06b9f9 | 2016-04-25 13:11:19 +0200 | [diff] [blame] | 111 | #if PY_VERSION_HEX >= 0x03000000 |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 112 | PyObject *ret = Py_BuildValue("y#", data, len); |
Ole Troan | d06b9f9 | 2016-04-25 13:11:19 +0200 | [diff] [blame] | 113 | #else |
| 114 | PyObject *ret = Py_BuildValue("s#", data, len); |
| 115 | #endif |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 116 | if (!ret) { Py_RETURN_NONE; } |
| 117 | |
Dave Barach | 557d128 | 2016-11-10 14:22:49 -0500 | [diff] [blame] | 118 | pneum_free(data); |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 119 | return ret; |
| 120 | } |
| 121 | |
Dave Barach | 557d128 | 2016-11-10 14:22:49 -0500 | [diff] [blame] | 122 | static PyObject * |
| 123 | wrap_msg_table (PyObject *self, PyObject *args) |
| 124 | { |
| 125 | int i = 0, rv = 0; |
| 126 | hash_pair_t *hp; |
| 127 | uword *h = pneum_msg_table_get_hash(); |
| 128 | PyObject *ret = PyList_New(pneum_msg_table_size()); |
| 129 | if (!ret) goto error; |
| 130 | hash_foreach_pair (hp, h, |
| 131 | ({ |
| 132 | PyObject *item = PyTuple_New(2); |
| 133 | if (!item) goto error; |
| 134 | rv = PyTuple_SetItem(item, 0, PyLong_FromLong((u32)hp->value[0])); |
| 135 | if (rv) goto error; |
| 136 | rv = PyTuple_SetItem(item, 1, PyString_FromString((char *)hp->key)); |
| 137 | if (rv) goto error; |
| 138 | PyList_SetItem(ret, i, item); |
| 139 | i++; |
| 140 | })); |
| 141 | |
| 142 | return ret; |
| 143 | |
| 144 | error: |
| 145 | /* TODO: Raise exception */ |
| 146 | printf("msg_table failed"); |
| 147 | Py_RETURN_NONE; |
| 148 | } |
| 149 | |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 150 | static PyMethodDef vpp_api_Methods[] = { |
| 151 | {"connect", wrap_connect, METH_VARARGS, "Connect to the VPP API."}, |
| 152 | {"disconnect", wrap_disconnect, METH_VARARGS, "Disconnect from the VPP API."}, |
| 153 | {"write", wrap_write, METH_VARARGS, "Write data to the VPP API."}, |
| 154 | {"read", wrap_read, METH_VARARGS, "Read data from the VPP API."}, |
Dave Barach | 557d128 | 2016-11-10 14:22:49 -0500 | [diff] [blame] | 155 | {"msg_table", wrap_msg_table, METH_VARARGS, "Get API dictionary."}, |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 156 | {NULL, NULL, 0, NULL} /* Sentinel */ |
| 157 | }; |
| 158 | |
Ole Troan | d06b9f9 | 2016-04-25 13:11:19 +0200 | [diff] [blame] | 159 | #if PY_VERSION_HEX >= 0x03000000 |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 160 | PyMODINIT_FUNC |
| 161 | PyInit_vpp_api (void) |
Ole Troan | d06b9f9 | 2016-04-25 13:11:19 +0200 | [diff] [blame] | 162 | #else |
| 163 | void |
| 164 | initvpp_api (void) |
| 165 | #endif |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 166 | { |
Ole Troan | d06b9f9 | 2016-04-25 13:11:19 +0200 | [diff] [blame] | 167 | #if PY_VERSION_HEX >= 0x03000000 |
| 168 | static struct PyModuleDef vpp_api_module = { |
Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 169 | #if PY_VERSION_HEX >= 0x03020000 |
Ole Troan | d06b9f9 | 2016-04-25 13:11:19 +0200 | [diff] [blame] | 170 | PyModuleDef_HEAD_INIT, |
Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 171 | #else |
Ole Troan | d06b9f9 | 2016-04-25 13:11:19 +0200 | [diff] [blame] | 172 | { |
| 173 | PyObject_HEAD_INIT(NULL) |
| 174 | NULL, /* m_init */ |
| 175 | 0, /* m_index */ |
| 176 | NULL, /* m_copy */ |
| 177 | }, |
Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 178 | #endif |
Ole Troan | d06b9f9 | 2016-04-25 13:11:19 +0200 | [diff] [blame] | 179 | (char *) "vpp_api", |
| 180 | NULL, |
| 181 | -1, |
| 182 | vpp_api_Methods, |
| 183 | NULL, |
| 184 | NULL, |
| 185 | NULL, |
| 186 | NULL |
| 187 | }; |
| 188 | #endif |
| 189 | |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 190 | /* Ensure threading is initialised */ |
| 191 | if (!PyEval_ThreadsInitialized()) { |
| 192 | PyEval_InitThreads(); |
| 193 | } |
Ole Troan | d06b9f9 | 2016-04-25 13:11:19 +0200 | [diff] [blame] | 194 | |
| 195 | #if PY_VERSION_HEX >= 0x03000000 |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 196 | return PyModule_Create(&vpp_api_module); |
Ole Troan | d06b9f9 | 2016-04-25 13:11:19 +0200 | [diff] [blame] | 197 | #else |
| 198 | Py_InitModule((char *) "vpp_api", vpp_api_Methods); |
| 199 | return; |
| 200 | #endif |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 201 | } |