Ole Troan | a03f4ef | 2016-12-02 12:53:55 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python |
Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 2 | # |
| 3 | # Copyright (c) 2016 Cisco and/or its affiliates. |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at: |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
Ole Troan | a03f4ef | 2016-12-02 12:53:55 +0100 | [diff] [blame] | 15 | # |
Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 16 | |
Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 17 | from __future__ import print_function |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 18 | from __future__ import absolute_import |
Ole Troan | 4df9716 | 2017-07-07 16:06:08 +0200 | [diff] [blame] | 19 | import sys |
| 20 | import os |
| 21 | import logging |
| 22 | import collections |
| 23 | import struct |
Paul Vinciguerra | 5fced04 | 2019-02-26 20:39:44 -0800 | [diff] [blame] | 24 | import functools |
Ole Troan | 4df9716 | 2017-07-07 16:06:08 +0200 | [diff] [blame] | 25 | import json |
| 26 | import threading |
Chris Luke | 52bf22e | 2017-11-03 23:32:38 -0400 | [diff] [blame] | 27 | import fnmatch |
Klement Sekera | 180402d | 2018-02-17 10:58:37 +0100 | [diff] [blame] | 28 | import weakref |
Ole Troan | 4df9716 | 2017-07-07 16:06:08 +0200 | [diff] [blame] | 29 | import atexit |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 30 | from . vpp_serializer import VPPType, VPPEnumType, VPPUnionType, BaseTypes |
Ole Troan | 53fffa1 | 2018-11-13 12:36:56 +0100 | [diff] [blame] | 31 | from . vpp_serializer import VPPMessage, vpp_get_type, VPPTypeAlias |
Ole Troan | 8006c6a | 2018-12-17 12:02:26 +0100 | [diff] [blame] | 32 | from . macaddress import MACAddress, mac_pton, mac_ntop |
Ole Troan | 4df9716 | 2017-07-07 16:06:08 +0200 | [diff] [blame] | 33 | |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame] | 34 | logger = logging.getLogger(__name__) |
| 35 | |
Ole Troan | 4df9716 | 2017-07-07 16:06:08 +0200 | [diff] [blame] | 36 | if sys.version[0] == '2': |
| 37 | import Queue as queue |
| 38 | else: |
| 39 | import queue as queue |
| 40 | |
Ole Troan | afddd83 | 2018-02-28 14:55:20 +0100 | [diff] [blame] | 41 | |
Paul Vinciguerra | 5fced04 | 2019-02-26 20:39:44 -0800 | [diff] [blame] | 42 | def metaclass(metaclass): |
| 43 | @functools.wraps(metaclass) |
| 44 | def wrapper(cls): |
| 45 | return metaclass(cls.__name__, cls.__bases__, cls.__dict__.copy()) |
| 46 | |
| 47 | return wrapper |
| 48 | |
| 49 | |
Ole Troan | 0685da4 | 2018-10-16 14:42:50 +0200 | [diff] [blame] | 50 | class VppEnumType(type): |
| 51 | def __getattr__(cls, name): |
| 52 | t = vpp_get_type(name) |
| 53 | return t.enum |
| 54 | |
| 55 | |
Paul Vinciguerra | 5fced04 | 2019-02-26 20:39:44 -0800 | [diff] [blame] | 56 | @metaclass(VppEnumType) |
Paul Vinciguerra | 7e713f1 | 2018-11-26 12:04:48 -0800 | [diff] [blame] | 57 | class VppEnum(object): |
Paul Vinciguerra | 5fced04 | 2019-02-26 20:39:44 -0800 | [diff] [blame] | 58 | pass |
Ole Troan | 0685da4 | 2018-10-16 14:42:50 +0200 | [diff] [blame] | 59 | |
| 60 | |
Klement Sekera | 180402d | 2018-02-17 10:58:37 +0100 | [diff] [blame] | 61 | def vpp_atexit(vpp_weakref): |
Ole Troan | 5016f99 | 2017-01-19 09:44:44 +0100 | [diff] [blame] | 62 | """Clean up VPP connection on shutdown.""" |
Klement Sekera | 180402d | 2018-02-17 10:58:37 +0100 | [diff] [blame] | 63 | vpp_instance = vpp_weakref() |
Ole Troan | 94495f2 | 2018-08-02 11:58:12 +0200 | [diff] [blame] | 64 | if vpp_instance and vpp_instance.transport.connected: |
Klement Sekera | 180402d | 2018-02-17 10:58:37 +0100 | [diff] [blame] | 65 | vpp_instance.logger.debug('Cleaning up VPP on exit') |
| 66 | vpp_instance.disconnect() |
Ole Troan | 5016f99 | 2017-01-19 09:44:44 +0100 | [diff] [blame] | 67 | |
Ole Troan | 8006c6a | 2018-12-17 12:02:26 +0100 | [diff] [blame] | 68 | |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 69 | if sys.version[0] == '2': |
| 70 | def vpp_iterator(d): |
Ole Troan | 4df9716 | 2017-07-07 16:06:08 +0200 | [diff] [blame] | 71 | return d.iteritems() |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 72 | else: |
| 73 | def vpp_iterator(d): |
Ole Troan | 4df9716 | 2017-07-07 16:06:08 +0200 | [diff] [blame] | 74 | return d.items() |
| 75 | |
| 76 | |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame] | 77 | def call_logger(msgdef, kwargs): |
| 78 | s = 'Calling {}('.format(msgdef.name) |
| 79 | for k, v in kwargs.items(): |
| 80 | s += '{}:{} '.format(k, v) |
| 81 | s += ')' |
| 82 | return s |
| 83 | |
| 84 | |
| 85 | def return_logger(r): |
| 86 | s = 'Return from {}'.format(r) |
| 87 | return s |
| 88 | |
| 89 | |
Klement Sekera | 8aedf5e | 2018-07-06 11:07:21 +0200 | [diff] [blame] | 90 | class VppApiDynamicMethodHolder(object): |
Klement Sekera | 7112c54 | 2017-03-01 09:53:19 +0100 | [diff] [blame] | 91 | pass |
| 92 | |
| 93 | |
| 94 | class FuncWrapper(object): |
| 95 | def __init__(self, func): |
| 96 | self._func = func |
| 97 | self.__name__ = func.__name__ |
Paul Vinciguerra | b8daa25 | 2019-03-19 15:04:17 -0700 | [diff] [blame^] | 98 | self.__doc__ = func.__doc__ |
Klement Sekera | 7112c54 | 2017-03-01 09:53:19 +0100 | [diff] [blame] | 99 | |
| 100 | def __call__(self, **kwargs): |
| 101 | return self._func(**kwargs) |
| 102 | |
| 103 | |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 104 | class VPPApiError(Exception): |
| 105 | pass |
| 106 | |
| 107 | |
| 108 | class VPPNotImplementedError(NotImplementedError): |
| 109 | pass |
| 110 | |
| 111 | |
| 112 | class VPPIOError(IOError): |
| 113 | pass |
| 114 | |
| 115 | |
| 116 | class VPPRuntimeError(RuntimeError): |
| 117 | pass |
| 118 | |
| 119 | |
| 120 | class VPPValueError(ValueError): |
| 121 | pass |
| 122 | |
| 123 | |
Paul Vinciguerra | 7e713f1 | 2018-11-26 12:04:48 -0800 | [diff] [blame] | 124 | class VPP(object): |
Ole Troan | 5016f99 | 2017-01-19 09:44:44 +0100 | [diff] [blame] | 125 | """VPP interface. |
| 126 | |
| 127 | This class provides the APIs to VPP. The APIs are loaded |
| 128 | from provided .api.json files and makes functions accordingly. |
| 129 | These functions are documented in the VPP .api files, as they |
| 130 | are dynamically created. |
| 131 | |
| 132 | Additionally, VPP can send callback messages; this class |
| 133 | provides a means to register a callback function to receive |
| 134 | these messages in a background thread. |
| 135 | """ |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 136 | VPPApiError = VPPApiError |
| 137 | VPPRuntimeError = VPPRuntimeError |
| 138 | VPPValueError = VPPValueError |
| 139 | VPPNotImplementedError = VPPNotImplementedError |
| 140 | VPPIOError = VPPIOError |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 141 | |
| 142 | def process_json_file(self, apidef_file): |
| 143 | api = json.load(apidef_file) |
| 144 | types = {} |
| 145 | for t in api['enums']: |
| 146 | t[0] = 'vl_api_' + t[0] + '_t' |
| 147 | types[t[0]] = {'type': 'enum', 'data': t} |
| 148 | for t in api['unions']: |
| 149 | t[0] = 'vl_api_' + t[0] + '_t' |
| 150 | types[t[0]] = {'type': 'union', 'data': t} |
| 151 | for t in api['types']: |
| 152 | t[0] = 'vl_api_' + t[0] + '_t' |
| 153 | types[t[0]] = {'type': 'type', 'data': t} |
Ole Troan | 53fffa1 | 2018-11-13 12:36:56 +0100 | [diff] [blame] | 154 | for t, v in api['aliases'].items(): |
| 155 | types['vl_api_' + t + '_t'] = {'type': 'alias', 'data': v} |
Ole Troan | dfb984d | 2018-12-07 14:31:16 +0100 | [diff] [blame] | 156 | self.services.update(api['services']) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 157 | |
| 158 | i = 0 |
| 159 | while True: |
| 160 | unresolved = {} |
| 161 | for k, v in types.items(): |
| 162 | t = v['data'] |
Ole Troan | 53fffa1 | 2018-11-13 12:36:56 +0100 | [diff] [blame] | 163 | if not vpp_get_type(k): |
Ole Troan | 0685da4 | 2018-10-16 14:42:50 +0200 | [diff] [blame] | 164 | if v['type'] == 'enum': |
| 165 | try: |
| 166 | VPPEnumType(t[0], t[1:]) |
| 167 | except ValueError: |
| 168 | unresolved[k] = v |
| 169 | elif v['type'] == 'union': |
| 170 | try: |
| 171 | VPPUnionType(t[0], t[1:]) |
| 172 | except ValueError: |
| 173 | unresolved[k] = v |
| 174 | elif v['type'] == 'type': |
| 175 | try: |
| 176 | VPPType(t[0], t[1:]) |
| 177 | except ValueError: |
| 178 | unresolved[k] = v |
Ole Troan | 53fffa1 | 2018-11-13 12:36:56 +0100 | [diff] [blame] | 179 | elif v['type'] == 'alias': |
| 180 | try: |
| 181 | VPPTypeAlias(k, t) |
| 182 | except ValueError: |
| 183 | unresolved[k] = v |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 184 | if len(unresolved) == 0: |
| 185 | break |
| 186 | if i > 3: |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 187 | raise VPPValueError('Unresolved type definitions {}' |
| 188 | .format(unresolved)) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 189 | types = unresolved |
| 190 | i += 1 |
| 191 | |
| 192 | for m in api['messages']: |
| 193 | try: |
| 194 | self.messages[m[0]] = VPPMessage(m[0], m[1:]) |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 195 | except VPPNotImplementedError: |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 196 | self.logger.error('Not implemented error for {}'.format(m[0])) |
| 197 | |
Ole Troan | 4df9716 | 2017-07-07 16:06:08 +0200 | [diff] [blame] | 198 | def __init__(self, apifiles=None, testmode=False, async_thread=True, |
Igor Mikhailov (imichail) | 5efd14f | 2018-10-30 12:17:49 -0700 | [diff] [blame] | 199 | logger=None, loglevel=None, |
Ole Troan | 94495f2 | 2018-08-02 11:58:12 +0200 | [diff] [blame] | 200 | read_timeout=5, use_socket=False, |
| 201 | server_address='/run/vpp-api.sock'): |
Ole Troan | 5016f99 | 2017-01-19 09:44:44 +0100 | [diff] [blame] | 202 | """Create a VPP API object. |
| 203 | |
| 204 | apifiles is a list of files containing API |
| 205 | descriptions that will be loaded - methods will be |
| 206 | dynamically created reflecting these APIs. If not |
| 207 | provided this will load the API files from VPP's |
| 208 | default install location. |
Ian Wells | d0e812f | 2018-06-06 14:12:27 +0100 | [diff] [blame] | 209 | |
| 210 | logger, if supplied, is the logging logger object to log to. |
| 211 | loglevel, if supplied, is the log level this logger is set |
| 212 | to report at (from the loglevels in the logging module). |
Ole Troan | 5016f99 | 2017-01-19 09:44:44 +0100 | [diff] [blame] | 213 | """ |
Ian Wells | d0e812f | 2018-06-06 14:12:27 +0100 | [diff] [blame] | 214 | if logger is None: |
| 215 | logger = logging.getLogger(__name__) |
| 216 | if loglevel is not None: |
| 217 | logger.setLevel(loglevel) |
Ole Troan | 3cc4971 | 2017-03-08 12:02:24 +0100 | [diff] [blame] | 218 | self.logger = logger |
Ole Troan | 3cc4971 | 2017-03-08 12:02:24 +0100 | [diff] [blame] | 219 | |
Ole Troan | a03f4ef | 2016-12-02 12:53:55 +0100 | [diff] [blame] | 220 | self.messages = {} |
Ole Troan | dfb984d | 2018-12-07 14:31:16 +0100 | [diff] [blame] | 221 | self.services = {} |
Ole Troan | a03f4ef | 2016-12-02 12:53:55 +0100 | [diff] [blame] | 222 | self.id_names = [] |
| 223 | self.id_msgdef = [] |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 224 | self.header = VPPType('header', [['u16', 'msgid'], |
| 225 | ['u32', 'client_index']]) |
Ole Troan | 5016f99 | 2017-01-19 09:44:44 +0100 | [diff] [blame] | 226 | self.apifiles = [] |
Ole Troan | 3d31f00 | 2017-01-26 11:13:00 +0100 | [diff] [blame] | 227 | self.event_callback = None |
Ole Troan | 4df9716 | 2017-07-07 16:06:08 +0200 | [diff] [blame] | 228 | self.message_queue = queue.Queue() |
dongjuan | 8493752 | 2017-11-09 14:46:36 +0800 | [diff] [blame] | 229 | self.read_timeout = read_timeout |
Klement Sekera | 180402d | 2018-02-17 10:58:37 +0100 | [diff] [blame] | 230 | self.async_thread = async_thread |
Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 231 | |
Ole Troan | 94495f2 | 2018-08-02 11:58:12 +0200 | [diff] [blame] | 232 | if use_socket: |
| 233 | from . vpp_transport_socket import VppTransport |
| 234 | else: |
| 235 | from . vpp_transport_shmem import VppTransport |
| 236 | |
Ole Troan | f5984bd | 2016-12-18 13:15:08 +0100 | [diff] [blame] | 237 | if not apifiles: |
| 238 | # Pick up API definitions from default directory |
Chris Luke | 52bf22e | 2017-11-03 23:32:38 -0400 | [diff] [blame] | 239 | try: |
| 240 | apifiles = self.find_api_files() |
| 241 | except RuntimeError: |
| 242 | # In test mode we don't care that we can't find the API files |
| 243 | if testmode: |
| 244 | apifiles = [] |
| 245 | else: |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 246 | raise VPPRuntimeError |
Ole Troan | f5984bd | 2016-12-18 13:15:08 +0100 | [diff] [blame] | 247 | |
Ole Troan | a03f4ef | 2016-12-02 12:53:55 +0100 | [diff] [blame] | 248 | for file in apifiles: |
Ole Troan | a03f4ef | 2016-12-02 12:53:55 +0100 | [diff] [blame] | 249 | with open(file) as apidef_file: |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 250 | self.process_json_file(apidef_file) |
Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 251 | |
Ole Troan | 4df9716 | 2017-07-07 16:06:08 +0200 | [diff] [blame] | 252 | self.apifiles = apifiles |
Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 253 | |
Ole Troan | a03f4ef | 2016-12-02 12:53:55 +0100 | [diff] [blame] | 254 | # Basic sanity check |
Ole Troan | f5984bd | 2016-12-18 13:15:08 +0100 | [diff] [blame] | 255 | if len(self.messages) == 0 and not testmode: |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 256 | raise VPPValueError(1, 'Missing JSON message definitions') |
Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 257 | |
Ole Troan | 94495f2 | 2018-08-02 11:58:12 +0200 | [diff] [blame] | 258 | self.transport = VppTransport(self, read_timeout=read_timeout, |
| 259 | server_address=server_address) |
Ole Troan | 5016f99 | 2017-01-19 09:44:44 +0100 | [diff] [blame] | 260 | # Make sure we allow VPP to clean up the message rings. |
Klement Sekera | 180402d | 2018-02-17 10:58:37 +0100 | [diff] [blame] | 261 | atexit.register(vpp_atexit, weakref.ref(self)) |
Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 262 | |
Ole Troan | a03f4ef | 2016-12-02 12:53:55 +0100 | [diff] [blame] | 263 | class ContextId(object): |
Ole Troan | 5016f99 | 2017-01-19 09:44:44 +0100 | [diff] [blame] | 264 | """Thread-safe provider of unique context IDs.""" |
Ole Troan | a03f4ef | 2016-12-02 12:53:55 +0100 | [diff] [blame] | 265 | def __init__(self): |
| 266 | self.context = 0 |
Ole Troan | 4df9716 | 2017-07-07 16:06:08 +0200 | [diff] [blame] | 267 | self.lock = threading.Lock() |
| 268 | |
Ole Troan | a03f4ef | 2016-12-02 12:53:55 +0100 | [diff] [blame] | 269 | def __call__(self): |
Ole Troan | 5016f99 | 2017-01-19 09:44:44 +0100 | [diff] [blame] | 270 | """Get a new unique (or, at least, not recently used) context.""" |
Ole Troan | 4df9716 | 2017-07-07 16:06:08 +0200 | [diff] [blame] | 271 | with self.lock: |
| 272 | self.context += 1 |
| 273 | return self.context |
Ole Troan | a03f4ef | 2016-12-02 12:53:55 +0100 | [diff] [blame] | 274 | get_context = ContextId() |
Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 275 | |
Ole Troan | 0685da4 | 2018-10-16 14:42:50 +0200 | [diff] [blame] | 276 | def get_type(self, name): |
| 277 | return vpp_get_type(name) |
| 278 | |
Chris Luke | 52bf22e | 2017-11-03 23:32:38 -0400 | [diff] [blame] | 279 | @classmethod |
| 280 | def find_api_dir(cls): |
| 281 | """Attempt to find the best directory in which API definition |
| 282 | files may reside. If the value VPP_API_DIR exists in the environment |
| 283 | then it is first on the search list. If we're inside a recognized |
| 284 | location in a VPP source tree (src/scripts and src/vpp-api/python) |
| 285 | then entries from there to the likely locations in build-root are |
| 286 | added. Finally the location used by system packages is added. |
| 287 | |
| 288 | :returns: A single directory name, or None if no such directory |
| 289 | could be found. |
| 290 | """ |
| 291 | dirs = [] |
| 292 | |
| 293 | if 'VPP_API_DIR' in os.environ: |
| 294 | dirs.append(os.environ['VPP_API_DIR']) |
| 295 | |
| 296 | # perhaps we're in the 'src/scripts' or 'src/vpp-api/python' dir; |
| 297 | # in which case, plot a course to likely places in the src tree |
| 298 | import __main__ as main |
| 299 | if hasattr(main, '__file__'): |
| 300 | # get the path of the calling script |
| 301 | localdir = os.path.dirname(os.path.realpath(main.__file__)) |
| 302 | else: |
| 303 | # use cwd if there is no calling script |
Andrey "Zed" Zaikin | 68e2ffb | 2018-04-24 14:50:02 +0300 | [diff] [blame] | 304 | localdir = os.getcwd() |
Chris Luke | 52bf22e | 2017-11-03 23:32:38 -0400 | [diff] [blame] | 305 | localdir_s = localdir.split(os.path.sep) |
| 306 | |
| 307 | def dmatch(dir): |
| 308 | """Match dir against right-hand components of the script dir""" |
| 309 | d = dir.split('/') # param 'dir' assumes a / separator |
Ole Troan | afddd83 | 2018-02-28 14:55:20 +0100 | [diff] [blame] | 310 | length = len(d) |
| 311 | return len(localdir_s) > length and localdir_s[-length:] == d |
Chris Luke | 52bf22e | 2017-11-03 23:32:38 -0400 | [diff] [blame] | 312 | |
| 313 | def sdir(srcdir, variant): |
| 314 | """Build a path from srcdir to the staged API files of |
| 315 | 'variant' (typically '' or '_debug')""" |
| 316 | # Since 'core' and 'plugin' files are staged |
| 317 | # in separate directories, we target the parent dir. |
| 318 | return os.path.sep.join(( |
| 319 | srcdir, |
| 320 | 'build-root', |
| 321 | 'install-vpp%s-native' % variant, |
| 322 | 'vpp', |
| 323 | 'share', |
| 324 | 'vpp', |
| 325 | 'api', |
| 326 | )) |
| 327 | |
| 328 | srcdir = None |
| 329 | if dmatch('src/scripts'): |
| 330 | srcdir = os.path.sep.join(localdir_s[:-2]) |
| 331 | elif dmatch('src/vpp-api/python'): |
| 332 | srcdir = os.path.sep.join(localdir_s[:-3]) |
| 333 | elif dmatch('test'): |
| 334 | # we're apparently running tests |
| 335 | srcdir = os.path.sep.join(localdir_s[:-1]) |
| 336 | |
| 337 | if srcdir: |
| 338 | # we're in the source tree, try both the debug and release |
| 339 | # variants. |
Chris Luke | 52bf22e | 2017-11-03 23:32:38 -0400 | [diff] [blame] | 340 | dirs.append(sdir(srcdir, '_debug')) |
| 341 | dirs.append(sdir(srcdir, '')) |
| 342 | |
| 343 | # Test for staged copies of the scripts |
| 344 | # For these, since we explicitly know if we're running a debug versus |
| 345 | # release variant, target only the relevant directory |
| 346 | if dmatch('build-root/install-vpp_debug-native/vpp/bin'): |
| 347 | srcdir = os.path.sep.join(localdir_s[:-4]) |
| 348 | dirs.append(sdir(srcdir, '_debug')) |
| 349 | if dmatch('build-root/install-vpp-native/vpp/bin'): |
| 350 | srcdir = os.path.sep.join(localdir_s[:-4]) |
| 351 | dirs.append(sdir(srcdir, '')) |
| 352 | |
| 353 | # finally, try the location system packages typically install into |
| 354 | dirs.append(os.path.sep.join(('', 'usr', 'share', 'vpp', 'api'))) |
| 355 | |
| 356 | # check the directories for existance; first one wins |
| 357 | for dir in dirs: |
| 358 | if os.path.isdir(dir): |
| 359 | return dir |
| 360 | |
| 361 | return None |
| 362 | |
| 363 | @classmethod |
| 364 | def find_api_files(cls, api_dir=None, patterns='*'): |
| 365 | """Find API definition files from the given directory tree with the |
| 366 | given pattern. If no directory is given then find_api_dir() is used |
| 367 | to locate one. If no pattern is given then all definition files found |
| 368 | in the directory tree are used. |
| 369 | |
| 370 | :param api_dir: A directory tree in which to locate API definition |
| 371 | files; subdirectories are descended into. |
| 372 | If this is None then find_api_dir() is called to discover it. |
| 373 | :param patterns: A list of patterns to use in each visited directory |
| 374 | when looking for files. |
| 375 | This can be a list/tuple object or a comma-separated string of |
| 376 | patterns. Each value in the list will have leading/trialing |
| 377 | whitespace stripped. |
| 378 | The pattern specifies the first part of the filename, '.api.json' |
| 379 | is appended. |
| 380 | The results are de-duplicated, thus overlapping patterns are fine. |
| 381 | If this is None it defaults to '*' meaning "all API files". |
| 382 | :returns: A list of file paths for the API files found. |
| 383 | """ |
| 384 | if api_dir is None: |
| 385 | api_dir = cls.find_api_dir() |
| 386 | if api_dir is None: |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 387 | raise VPPApiError("api_dir cannot be located") |
Chris Luke | 52bf22e | 2017-11-03 23:32:38 -0400 | [diff] [blame] | 388 | |
| 389 | if isinstance(patterns, list) or isinstance(patterns, tuple): |
| 390 | patterns = [p.strip() + '.api.json' for p in patterns] |
| 391 | else: |
| 392 | patterns = [p.strip() + '.api.json' for p in patterns.split(",")] |
| 393 | |
| 394 | api_files = [] |
| 395 | for root, dirnames, files in os.walk(api_dir): |
| 396 | # iterate all given patterns and de-dup the result |
| 397 | files = set(sum([fnmatch.filter(files, p) for p in patterns], [])) |
| 398 | for filename in files: |
| 399 | api_files.append(os.path.join(root, filename)) |
| 400 | |
| 401 | return api_files |
| 402 | |
Klement Sekera | 7112c54 | 2017-03-01 09:53:19 +0100 | [diff] [blame] | 403 | @property |
| 404 | def api(self): |
| 405 | if not hasattr(self, "_api"): |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 406 | raise VPPApiError("Not connected, api definitions not available") |
Klement Sekera | 7112c54 | 2017-03-01 09:53:19 +0100 | [diff] [blame] | 407 | return self._api |
| 408 | |
Ole Troan | eabd607 | 2018-08-09 12:50:55 +0200 | [diff] [blame] | 409 | def make_function(self, msg, i, multipart, do_async): |
| 410 | if (do_async): |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 411 | def f(**kwargs): |
| 412 | return self._call_vpp_async(i, msg, **kwargs) |
| 413 | else: |
| 414 | def f(**kwargs): |
| 415 | return self._call_vpp(i, msg, multipart, **kwargs) |
| 416 | |
| 417 | f.__name__ = str(msg.name) |
| 418 | f.__doc__ = ", ".join(["%s %s" % |
| 419 | (msg.fieldtypes[j], k) |
| 420 | for j, k in enumerate(msg.fields)]) |
Ole Troan | f159f58 | 2019-02-28 20:20:47 +0100 | [diff] [blame] | 421 | f.msg = msg |
| 422 | |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 423 | return f |
| 424 | |
Ole Troan | eabd607 | 2018-08-09 12:50:55 +0200 | [diff] [blame] | 425 | def _register_functions(self, do_async=False): |
Ole Troan | a03f4ef | 2016-12-02 12:53:55 +0100 | [diff] [blame] | 426 | self.id_names = [None] * (self.vpp_dictionary_maxid + 1) |
| 427 | self.id_msgdef = [None] * (self.vpp_dictionary_maxid + 1) |
Klement Sekera | 8aedf5e | 2018-07-06 11:07:21 +0200 | [diff] [blame] | 428 | self._api = VppApiDynamicMethodHolder() |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 429 | for name, msg in vpp_iterator(self.messages): |
| 430 | n = name + '_' + msg.crc[2:] |
Paul Vinciguerra | 3d04e33 | 2019-03-19 17:32:39 -0700 | [diff] [blame] | 431 | i = self.transport.get_msg_index(n.encode('utf-8')) |
Ole Troan | 3cc4971 | 2017-03-08 12:02:24 +0100 | [diff] [blame] | 432 | if i > 0: |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 433 | self.id_msgdef[i] = msg |
Ole Troan | a03f4ef | 2016-12-02 12:53:55 +0100 | [diff] [blame] | 434 | self.id_names[i] = name |
Ole Troan | dfb984d | 2018-12-07 14:31:16 +0100 | [diff] [blame] | 435 | |
| 436 | # Create function for client side messages. |
| 437 | if name in self.services: |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 438 | if 'stream' in self.services[name] and \ |
| 439 | self.services[name]['stream']: |
Ole Troan | dfb984d | 2018-12-07 14:31:16 +0100 | [diff] [blame] | 440 | multipart = True |
| 441 | else: |
| 442 | multipart = False |
| 443 | f = self.make_function(msg, i, multipart, do_async) |
| 444 | setattr(self._api, name, FuncWrapper(f)) |
Ole Troan | 3cc4971 | 2017-03-08 12:02:24 +0100 | [diff] [blame] | 445 | else: |
Ole Troan | 4df9716 | 2017-07-07 16:06:08 +0200 | [diff] [blame] | 446 | self.logger.debug( |
| 447 | 'No such message type or failed CRC checksum: %s', n) |
Ole Troan | a03f4ef | 2016-12-02 12:53:55 +0100 | [diff] [blame] | 448 | |
Ole Troan | 4df9716 | 2017-07-07 16:06:08 +0200 | [diff] [blame] | 449 | def connect_internal(self, name, msg_handler, chroot_prefix, rx_qlen, |
Ole Troan | eabd607 | 2018-08-09 12:50:55 +0200 | [diff] [blame] | 450 | do_async): |
Paul Vinciguerra | 3d04e33 | 2019-03-19 17:32:39 -0700 | [diff] [blame] | 451 | pfx = chroot_prefix.encode('utf-8') if chroot_prefix else None |
Ole Troan | 94495f2 | 2018-08-02 11:58:12 +0200 | [diff] [blame] | 452 | |
Paul Vinciguerra | 3d04e33 | 2019-03-19 17:32:39 -0700 | [diff] [blame] | 453 | rv = self.transport.connect(name.encode('utf-8'), pfx, |
| 454 | msg_handler, rx_qlen) |
Ole Troan | a03f4ef | 2016-12-02 12:53:55 +0100 | [diff] [blame] | 455 | if rv != 0: |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 456 | raise VPPIOError(2, 'Connect failed') |
Ole Troan | 94495f2 | 2018-08-02 11:58:12 +0200 | [diff] [blame] | 457 | self.vpp_dictionary_maxid = self.transport.msg_table_max_index() |
Ole Troan | eabd607 | 2018-08-09 12:50:55 +0200 | [diff] [blame] | 458 | self._register_functions(do_async=do_async) |
Ole Troan | a03f4ef | 2016-12-02 12:53:55 +0100 | [diff] [blame] | 459 | |
| 460 | # Initialise control ping |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 461 | crc = self.messages['control_ping'].crc |
Ole Troan | 94495f2 | 2018-08-02 11:58:12 +0200 | [diff] [blame] | 462 | self.control_ping_index = self.transport.get_msg_index( |
Paul Vinciguerra | 3d04e33 | 2019-03-19 17:32:39 -0700 | [diff] [blame] | 463 | ('control_ping' + '_' + crc[2:]).encode('utf-8')) |
Ole Troan | a03f4ef | 2016-12-02 12:53:55 +0100 | [diff] [blame] | 464 | self.control_ping_msgdef = self.messages['control_ping'] |
Klement Sekera | 180402d | 2018-02-17 10:58:37 +0100 | [diff] [blame] | 465 | if self.async_thread: |
| 466 | self.event_thread = threading.Thread( |
| 467 | target=self.thread_msg_handler) |
| 468 | self.event_thread.daemon = True |
| 469 | self.event_thread.start() |
Ole Troan | 4df9716 | 2017-07-07 16:06:08 +0200 | [diff] [blame] | 470 | return rv |
Ole Troan | a03f4ef | 2016-12-02 12:53:55 +0100 | [diff] [blame] | 471 | |
Ole Troan | eabd607 | 2018-08-09 12:50:55 +0200 | [diff] [blame] | 472 | def connect(self, name, chroot_prefix=None, do_async=False, rx_qlen=32): |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 473 | """Attach to VPP. |
| 474 | |
| 475 | name - the name of the client. |
| 476 | chroot_prefix - if VPP is chroot'ed, the prefix of the jail |
Ole Troan | eabd607 | 2018-08-09 12:50:55 +0200 | [diff] [blame] | 477 | do_async - if true, messages are sent without waiting for a reply |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 478 | rx_qlen - the length of the VPP message receive queue between |
| 479 | client and server. |
| 480 | """ |
Ole Troan | 94495f2 | 2018-08-02 11:58:12 +0200 | [diff] [blame] | 481 | msg_handler = self.transport.get_callback(do_async) |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 482 | return self.connect_internal(name, msg_handler, chroot_prefix, rx_qlen, |
Ole Troan | eabd607 | 2018-08-09 12:50:55 +0200 | [diff] [blame] | 483 | do_async) |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 484 | |
Ole Troan | 6bf177c | 2017-08-17 10:34:32 +0200 | [diff] [blame] | 485 | def connect_sync(self, name, chroot_prefix=None, rx_qlen=32): |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 486 | """Attach to VPP in synchronous mode. Application must poll for events. |
| 487 | |
| 488 | name - the name of the client. |
| 489 | chroot_prefix - if VPP is chroot'ed, the prefix of the jail |
| 490 | rx_qlen - the length of the VPP message receive queue between |
| 491 | client and server. |
| 492 | """ |
| 493 | |
Ole Troan | 94495f2 | 2018-08-02 11:58:12 +0200 | [diff] [blame] | 494 | return self.connect_internal(name, None, chroot_prefix, rx_qlen, |
Ole Troan | eabd607 | 2018-08-09 12:50:55 +0200 | [diff] [blame] | 495 | do_async=False) |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 496 | |
Ole Troan | a03f4ef | 2016-12-02 12:53:55 +0100 | [diff] [blame] | 497 | def disconnect(self): |
Ole Troan | 5016f99 | 2017-01-19 09:44:44 +0100 | [diff] [blame] | 498 | """Detach from VPP.""" |
Ole Troan | 94495f2 | 2018-08-02 11:58:12 +0200 | [diff] [blame] | 499 | rv = self.transport.disconnect() |
Klement Sekera | 180402d | 2018-02-17 10:58:37 +0100 | [diff] [blame] | 500 | self.message_queue.put("terminate event thread") |
Ole Troan | a03f4ef | 2016-12-02 12:53:55 +0100 | [diff] [blame] | 501 | return rv |
| 502 | |
Ole Troan | 5016f99 | 2017-01-19 09:44:44 +0100 | [diff] [blame] | 503 | def msg_handler_sync(self, msg): |
| 504 | """Process an incoming message from VPP in sync mode. |
| 505 | |
| 506 | The message may be a reply or it may be an async notification. |
| 507 | """ |
| 508 | r = self.decode_incoming_msg(msg) |
| 509 | if r is None: |
Ole Troan | a03f4ef | 2016-12-02 12:53:55 +0100 | [diff] [blame] | 510 | return |
| 511 | |
Ole Troan | 5016f99 | 2017-01-19 09:44:44 +0100 | [diff] [blame] | 512 | # If we have a context, then use the context to find any |
| 513 | # request waiting for a reply |
| 514 | context = 0 |
| 515 | if hasattr(r, 'context') and r.context > 0: |
| 516 | context = r.context |
Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 517 | |
Ole Troan | 5016f99 | 2017-01-19 09:44:44 +0100 | [diff] [blame] | 518 | if context == 0: |
| 519 | # No context -> async notification that we feed to the callback |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 520 | self.message_queue.put_nowait(r) |
Ole Troan | a03f4ef | 2016-12-02 12:53:55 +0100 | [diff] [blame] | 521 | else: |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 522 | raise VPPIOError(2, 'RPC reply message received in event handler') |
Ole Troan | 5016f99 | 2017-01-19 09:44:44 +0100 | [diff] [blame] | 523 | |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame] | 524 | def has_context(self, msg): |
| 525 | if len(msg) < 10: |
| 526 | return False |
| 527 | |
| 528 | header = VPPType('header_with_context', [['u16', 'msgid'], |
| 529 | ['u32', 'client_index'], |
| 530 | ['u32', 'context']]) |
| 531 | |
| 532 | (i, ci, context), size = header.unpack(msg, 0) |
| 533 | if self.id_names[i] == 'rx_thread_exit': |
| 534 | return |
| 535 | |
| 536 | # |
| 537 | # Decode message and returns a tuple. |
| 538 | # |
| 539 | msgobj = self.id_msgdef[i] |
| 540 | if 'context' in msgobj.field_by_name and context >= 0: |
| 541 | return True |
| 542 | return False |
| 543 | |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 544 | def decode_incoming_msg(self, msg, no_type_conversion=False): |
Ole Troan | 5016f99 | 2017-01-19 09:44:44 +0100 | [diff] [blame] | 545 | if not msg: |
Ole Troan | 3cc4971 | 2017-03-08 12:02:24 +0100 | [diff] [blame] | 546 | self.logger.warning('vpp_api.read failed') |
Ole Troan | 5016f99 | 2017-01-19 09:44:44 +0100 | [diff] [blame] | 547 | return |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame] | 548 | |
Ole Troan | c84cbad | 2018-09-06 22:58:05 +0200 | [diff] [blame] | 549 | (i, ci), size = self.header.unpack(msg, 0) |
Ole Troan | 5016f99 | 2017-01-19 09:44:44 +0100 | [diff] [blame] | 550 | if self.id_names[i] == 'rx_thread_exit': |
| 551 | return |
| 552 | |
| 553 | # |
| 554 | # Decode message and returns a tuple. |
| 555 | # |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 556 | msgobj = self.id_msgdef[i] |
| 557 | if not msgobj: |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 558 | raise VPPIOError(2, 'Reply message undefined') |
Ole Troan | 5016f99 | 2017-01-19 09:44:44 +0100 | [diff] [blame] | 559 | |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 560 | r, size = msgobj.unpack(msg, ntc=no_type_conversion) |
Ole Troan | a03f4ef | 2016-12-02 12:53:55 +0100 | [diff] [blame] | 561 | return r |
| 562 | |
Ole Troan | 5016f99 | 2017-01-19 09:44:44 +0100 | [diff] [blame] | 563 | def msg_handler_async(self, msg): |
| 564 | """Process a message from VPP in async mode. |
| 565 | |
| 566 | In async mode, all messages are returned to the callback. |
| 567 | """ |
| 568 | r = self.decode_incoming_msg(msg) |
| 569 | if r is None: |
| 570 | return |
| 571 | |
| 572 | msgname = type(r).__name__ |
| 573 | |
Ole Troan | 4df9716 | 2017-07-07 16:06:08 +0200 | [diff] [blame] | 574 | if self.event_callback: |
| 575 | self.event_callback(msgname, r) |
Ole Troan | 5016f99 | 2017-01-19 09:44:44 +0100 | [diff] [blame] | 576 | |
| 577 | def _control_ping(self, context): |
| 578 | """Send a ping command.""" |
| 579 | self._call_vpp_async(self.control_ping_index, |
Ole Troan | 4df9716 | 2017-07-07 16:06:08 +0200 | [diff] [blame] | 580 | self.control_ping_msgdef, |
Ole Troan | 5016f99 | 2017-01-19 09:44:44 +0100 | [diff] [blame] | 581 | context=context) |
| 582 | |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 583 | def validate_args(self, msg, kwargs): |
| 584 | d = set(kwargs.keys()) - set(msg.field_by_name.keys()) |
| 585 | if d: |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 586 | raise VPPValueError('Invalid argument {} to {}' |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 587 | .format(list(d), msg.name)) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 588 | |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame] | 589 | def _call_vpp(self, i, msgdef, multipart, **kwargs): |
Ole Troan | 5016f99 | 2017-01-19 09:44:44 +0100 | [diff] [blame] | 590 | """Given a message, send the message and await a reply. |
| 591 | |
| 592 | msgdef - the message packing definition |
| 593 | i - the message type index |
| 594 | multipart - True if the message returns multiple |
| 595 | messages in return. |
| 596 | context - context number - chosen at random if not |
| 597 | supplied. |
| 598 | The remainder of the kwargs are the arguments to the API call. |
| 599 | |
| 600 | The return value is the message or message array containing |
| 601 | the response. It will raise an IOError exception if there was |
| 602 | no response within the timeout window. |
| 603 | """ |
| 604 | |
Ole Troan | 4df9716 | 2017-07-07 16:06:08 +0200 | [diff] [blame] | 605 | if 'context' not in kwargs: |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 606 | context = self.get_context() |
| 607 | kwargs['context'] = context |
| 608 | else: |
| 609 | context = kwargs['context'] |
| 610 | kwargs['_vl_msg_id'] = i |
Ole Troan | 5016f99 | 2017-01-19 09:44:44 +0100 | [diff] [blame] | 611 | |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 612 | no_type_conversion = kwargs.pop('_no_type_conversion', False) |
| 613 | |
Ole Troan | 94495f2 | 2018-08-02 11:58:12 +0200 | [diff] [blame] | 614 | try: |
| 615 | if self.transport.socket_index: |
| 616 | kwargs['client_index'] = self.transport.socket_index |
| 617 | except AttributeError: |
| 618 | pass |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame] | 619 | self.validate_args(msgdef, kwargs) |
| 620 | |
| 621 | logging.debug(call_logger(msgdef, kwargs)) |
| 622 | |
| 623 | b = msgdef.pack(kwargs) |
Ole Troan | 94495f2 | 2018-08-02 11:58:12 +0200 | [diff] [blame] | 624 | self.transport.suspend() |
| 625 | |
| 626 | self.transport.write(b) |
Ole Troan | 5016f99 | 2017-01-19 09:44:44 +0100 | [diff] [blame] | 627 | |
| 628 | if multipart: |
| 629 | # Send a ping after the request - we use its response |
| 630 | # to detect that we have seen all results. |
| 631 | self._control_ping(context) |
| 632 | |
| 633 | # Block until we get a reply. |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 634 | rl = [] |
| 635 | while (True): |
Ole Troan | 94495f2 | 2018-08-02 11:58:12 +0200 | [diff] [blame] | 636 | msg = self.transport.read() |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 637 | if not msg: |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 638 | raise VPPIOError(2, 'VPP API client: read failed') |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 639 | r = self.decode_incoming_msg(msg, no_type_conversion) |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 640 | msgname = type(r).__name__ |
Ole Troan | 4df9716 | 2017-07-07 16:06:08 +0200 | [diff] [blame] | 641 | if context not in r or r.context == 0 or context != r.context: |
Ole Troan | 94495f2 | 2018-08-02 11:58:12 +0200 | [diff] [blame] | 642 | # Message being queued |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 643 | self.message_queue.put_nowait(r) |
| 644 | continue |
| 645 | |
| 646 | if not multipart: |
| 647 | rl = r |
| 648 | break |
| 649 | if msgname == 'control_ping_reply': |
| 650 | break |
| 651 | |
| 652 | rl.append(r) |
| 653 | |
Ole Troan | 94495f2 | 2018-08-02 11:58:12 +0200 | [diff] [blame] | 654 | self.transport.resume() |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 655 | |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame] | 656 | logger.debug(return_logger(rl)) |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 657 | return rl |
Ole Troan | 5016f99 | 2017-01-19 09:44:44 +0100 | [diff] [blame] | 658 | |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 659 | def _call_vpp_async(self, i, msg, **kwargs): |
Ole Troan | 5016f99 | 2017-01-19 09:44:44 +0100 | [diff] [blame] | 660 | """Given a message, send the message and await a reply. |
| 661 | |
| 662 | msgdef - the message packing definition |
| 663 | i - the message type index |
| 664 | context - context number - chosen at random if not |
| 665 | supplied. |
| 666 | The remainder of the kwargs are the arguments to the API call. |
| 667 | """ |
Ole Troan | 4df9716 | 2017-07-07 16:06:08 +0200 | [diff] [blame] | 668 | if 'context' not in kwargs: |
Ole Troan | 7e3a875 | 2016-12-05 10:27:09 +0100 | [diff] [blame] | 669 | context = self.get_context() |
| 670 | kwargs['context'] = context |
| 671 | else: |
| 672 | context = kwargs['context'] |
Ole Troan | 94495f2 | 2018-08-02 11:58:12 +0200 | [diff] [blame] | 673 | try: |
| 674 | if self.transport.socket_index: |
| 675 | kwargs['client_index'] = self.transport.socket_index |
| 676 | except AttributeError: |
| 677 | kwargs['client_index'] = 0 |
Ole Troan | 7e3a875 | 2016-12-05 10:27:09 +0100 | [diff] [blame] | 678 | kwargs['_vl_msg_id'] = i |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 679 | b = msg.pack(kwargs) |
Ole Troan | 7e3a875 | 2016-12-05 10:27:09 +0100 | [diff] [blame] | 680 | |
Ole Troan | 94495f2 | 2018-08-02 11:58:12 +0200 | [diff] [blame] | 681 | self.transport.write(b) |
Ole Troan | 7e3a875 | 2016-12-05 10:27:09 +0100 | [diff] [blame] | 682 | |
Ole Troan | a03f4ef | 2016-12-02 12:53:55 +0100 | [diff] [blame] | 683 | def register_event_callback(self, callback): |
Ole Troan | 5016f99 | 2017-01-19 09:44:44 +0100 | [diff] [blame] | 684 | """Register a callback for async messages. |
Ole Troan | a03f4ef | 2016-12-02 12:53:55 +0100 | [diff] [blame] | 685 | |
Ole Troan | 5016f99 | 2017-01-19 09:44:44 +0100 | [diff] [blame] | 686 | This will be called for async notifications in sync mode, |
| 687 | and all messages in async mode. In sync mode, replies to |
| 688 | requests will not come here. |
| 689 | |
| 690 | callback is a fn(msg_type_name, msg_type) that will be |
| 691 | called when a message comes in. While this function is |
| 692 | executing, note that (a) you are in a background thread and |
| 693 | may wish to use threading.Lock to protect your datastructures, |
| 694 | and (b) message processing from VPP will stop (so if you take |
| 695 | a long while about it you may provoke reply timeouts or cause |
| 696 | VPP to fill the RX buffer). Passing None will disable the |
| 697 | callback. |
| 698 | """ |
| 699 | self.event_callback = callback |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 700 | |
| 701 | def thread_msg_handler(self): |
Ole Troan | 94495f2 | 2018-08-02 11:58:12 +0200 | [diff] [blame] | 702 | """Python thread calling the user registered message handler. |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 703 | |
| 704 | This is to emulate the old style event callback scheme. Modern |
| 705 | clients should provide their own thread to poll the event |
| 706 | queue. |
| 707 | """ |
| 708 | while True: |
| 709 | r = self.message_queue.get() |
Klement Sekera | 180402d | 2018-02-17 10:58:37 +0100 | [diff] [blame] | 710 | if r == "terminate event thread": |
| 711 | break |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 712 | msgname = type(r).__name__ |
Ole Troan | 4df9716 | 2017-07-07 16:06:08 +0200 | [diff] [blame] | 713 | if self.event_callback: |
| 714 | self.event_callback(msgname, r) |
Chris Luke | 52bf22e | 2017-11-03 23:32:38 -0400 | [diff] [blame] | 715 | |
| 716 | |
| 717 | # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |