File: usdt_lib.py

package info (click to toggle)
openvswitch 3.6.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 99,632 kB
  • sloc: sh: 1,683,183; ansic: 313,349; python: 28,192; xml: 21,442; makefile: 548; javascript: 191
file content (102 lines) | stat: -rw-r--r-- 3,043 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# Copyright (c) 2021 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import re
import subprocess


class DpPortMapping:
    """Class used to retrieve and cache datapath port numbers to port names."""

    MAX_REQUESTS = 2

    def __init__(self):
        self.cache_map = None
        self.n_requests = 0

    def get_map(self):
        """Get the cache map."""
        self._get_mapping()

        return self.cache_map

    def set_map(self, cache_map):
        """Override the internal cache map."""
        self.cache_map = cache_map
        self.n_requests = self.MAX_REQUESTS

    def _retry(self, func):
        self._get_mapping()

        result = func(self.cache_map)

        if not result:
            self._get_mapping(refresh=True)
            return func(self.cache_map)

        return result

    def get_port_name(self, dp, port_no):
        """Get the port name from a port number."""
        def _get_port_name(cache_map):
            if not cache_map.get(dp):
                return None
            for name, num in cache_map[dp].items():
                if num == port_no:
                    return name

        return self._retry(_get_port_name)

    def get_port_number(self, dp, port):
        """Get the port number from a port name."""
        def _get_port_number(cache_map):
            return cache_map.get(dp, {}).get(port, None)

        return self._retry(_get_port_number)

    def _get_mapping(self, refresh=False):
        """Get the datapath port mapping from the running OVS."""
        if self.n_requests >= self.MAX_REQUESTS \
           or (self.cache_map and not refresh):
            return

        self.n_requests += 1

        try:
            output = subprocess.check_output(
                ["ovs-appctl", "dpctl/show"], encoding="utf8"
            ).split("\n")
        except subprocess.CalledProcessError:
            output = ""
            return

        current_dp = None
        self.cache_map = {}

        for line in output:
            match = re.match("^system@(.*):$", line)
            if match:
                current_dp = match.group(1)

            match = re.match("^  port ([0-9]+): ([^ /]*)", line)
            if match and current_dp:
                try:
                    self.cache_map[current_dp][match.group(2)] = int(
                        match.group(1)
                    )
                except KeyError:
                    self.cache_map[current_dp] = {
                        match.group(2): int(match.group(1))
                    }