summaryrefslogtreecommitdiff
path: root/neutron/tests/functional/agent/ovn/extensions/test_qos_hwol.py
blob: 83d5c58ab90f75a8b60bc0c9b9274728eddb27fd (plain)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# Copyright (c) 2023 Red Hat, Inc.
# All Rights Reserved.
#
#    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.

from unittest import mock

from oslo_utils import uuidutils

from neutron.agent.common import ovs_lib
from neutron.agent.ovn.agent import ovsdb as agent_ovsdb
from neutron.agent.ovn.extensions import qos_hwol
from neutron.common.ovn import constants as ovn_const
from neutron.common.ovn import utils
from neutron.common import utils as n_utils
from neutron.tests import base as test_base
from neutron.tests.common import net_helpers
from neutron.tests.functional import base


class OVSInterfaceEventTestCase(base.TestOVNFunctionalBase):

    @test_base.unstable_test(
        'LP#2006603, it is being addressed in '
        'https://review.opendev.org/c/openstack/neutron/+/873118')
    def test_port_creation_and_deletion(self):
        def check_add_port_called():
            try:
                mock_agent.qos_hwol_ext.add_port.assert_has_calls(
                    [mock.call('port_iface-id', port_name)])
                return True
            except AssertionError:
                return False

        def check_remove_egress_called():
            try:
                mock_agent.qos_hwol_ext.remove_egress.assert_has_calls(
                    [mock.call('port_iface-id')])
                return True
            except AssertionError:
                return False

        mock_agent = mock.Mock()
        events = [qos_hwol.OVSInterfaceEvent(mock_agent)]
        agent_ovsdb.MonitorAgentOvsIdl(events=events).start()
        br = self.useFixture(net_helpers.OVSBridgeFixture()).bridge
        self.ovs_bridge = ovs_lib.OVSBridge(br.br_name)
        port_name = ('port-' + uuidutils.generate_uuid())[:8]

        self.ovs_bridge.add_port(
            port_name, ('external_ids', {'iface-id': 'port_iface-id'}))
        n_utils.wait_until_true(check_add_port_called, timeout=5)

        self.ovs_bridge.delete_port(port_name)
        n_utils.wait_until_true(check_remove_egress_called, timeout=5)


class QoSBandwidthLimitEventTestCase(base.TestOVNFunctionalBase):

    def setUp(self, **kwargs):
        super().setUp(**kwargs)
        self.net = self._make_network(self.fmt, 'n1', True)['network']
        res = self._create_subnet(self.fmt, self.net['id'], '10.0.0.0/24')
        self.subnet = self.deserialize(self.fmt, res)['subnet']
        res = self._create_port(self.fmt, self.net['id'])
        self.port = self.deserialize(self.fmt, res)['port']

    def test_qos_bw_limit_created_and_updated(self):
        def check_update_egress_called(rate):
            try:
                mock_agent.qos_hwol_ext.update_egress.assert_has_calls(
                    [mock.call(port_id, rate, 0)])
                return True
            except AssertionError:
                return False

        mock_agent = mock.Mock(nb_idl=self.nb_api)
        events = [qos_hwol.QoSBandwidthLimitEvent(mock_agent)]
        agent_ovsdb.MonitorAgentOvnNbIdl(qos_hwol.NB_IDL_TABLES,
                                         events).start()
        lswitch_name = utils.ovn_name(self.net['id'])
        port_id = self.port['id']
        ovn_rule = {'switch': lswitch_name,
                    'priority': 1000,
                    'direction': 'from-lport',
                    'match': 'inport == ' + port_id,
                    'rate': 10000,
                    'external_ids': {ovn_const.OVN_PORT_EXT_ID_KEY: port_id}}
        self.nb_api.qos_add(**ovn_rule).execute(check_error=True)
        n_utils.wait_until_true(
            lambda: check_update_egress_called(ovn_rule['rate']), timeout=5)

        ovn_rule['rate'] = 15000
        self.nb_api.qos_add(**ovn_rule, may_exist=True).execute(
            check_error=True)
        n_utils.wait_until_true(
            lambda: check_update_egress_called(ovn_rule['rate']), timeout=5)


class QoSMinimumBandwidthEventTestCase(base.TestOVNFunctionalBase):

    def setUp(self, **kwargs):
        super().setUp(**kwargs)
        self.net = self._make_network(self.fmt, 'n1', True)['network']
        res = self._create_subnet(self.fmt, self.net['id'], '10.0.0.0/24')
        self.subnet = self.deserialize(self.fmt, res)['subnet']
        res = self._create_port(self.fmt, self.net['id'])
        self.port = self.deserialize(self.fmt, res)['port']

    def test_qos_min_bw_created_and_updated(self):
        def check_update_egress_called(max_kbps, min_kbps):
            try:
                mock_agent.qos_hwol_ext.update_egress.assert_has_calls(
                    [mock.call(port_id, max_kbps, min_kbps)])
                return True
            except AssertionError:
                return False

        mock_agent = mock.Mock(nb_idl=self.nb_api)
        events = [qos_hwol.QoSMinimumBandwidthEvent(mock_agent)]
        agent_ovsdb.MonitorAgentOvnNbIdl(qos_hwol.NB_IDL_TABLES,
                                         events).start()
        port_id = self.port['id']
        min_kbps = 5000
        lsp = self.nb_api.lsp_get(port_id).execute(check_error=True)
        options = {ovn_const.LSP_OPTIONS_QOS_MIN_RATE: str(min_kbps)}
        self.nb_api.update_lswitch_qos_options(lsp, **options).execute(
            check_error=True)
        n_utils.wait_until_true(
            lambda: check_update_egress_called(0, min_kbps), timeout=5)


class PortBindingChassisCreatedEventTestCase(base.TestOVNFunctionalBase):

    def setUp(self, **kwargs):
        super().setUp(**kwargs)
        self.net = self._make_network(self.fmt, 'n1', True)['network']
        res = self._create_subnet(self.fmt, self.net['id'], '10.0.0.0/24')
        self.subnet = self.deserialize(self.fmt, res)['subnet']
        res = self._create_port(self.fmt, self.net['id'])
        self.port = self.deserialize(self.fmt, res)['port']

    @mock.patch.object(agent_ovsdb, 'get_ovs_port_name')
    @mock.patch.object(agent_ovsdb, 'get_port_qos')
    def test_port_binding_chassis_create_event(self, mock_get_port_qos,
                                               *args):
        def check_update_egress_called(max_kbps, min_kbps):
            try:
                mock_agent.qos_hwol_ext.update_egress.assert_has_calls(
                    [mock.call(self.port['id'], max_kbps, min_kbps)])
                return True
            except AssertionError:
                return False

        max_kbps, min_kbps = 1000, 800
        mock_get_port_qos.return_value = max_kbps, min_kbps
        mock_agent = mock.Mock(nb_idl=self.nb_api)
        events = [qos_hwol.PortBindingChassisCreatedEvent(mock_agent)]
        chassis_name = self.add_fake_chassis('ovn-host-fake')
        mock_agent.chassis = chassis_name
        agent_ovsdb.MonitorAgentOvnSbIdl(qos_hwol.SB_IDL_TABLES, events,
                                         chassis=chassis_name).start()
        lsp_columns = {}
        lsp_name = self.port['id']
        ls_name = utils.ovn_name(self.net['id'])
        self.nb_api.create_lswitch_port(
            lsp_name, ls_name, **lsp_columns).execute(check_error=True)
        self.sb_api.lsp_bind(lsp_name, chassis_name).execute(check_error=True)
        n_utils.wait_until_true(
            lambda: check_update_egress_called(max_kbps, min_kbps), timeout=5)