Test Failed
Pull Request — master (#505)
by macartur
01:31
created

PortNo

Complexity

Total Complexity 0

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 0
dl 0
loc 30
ccs 0
cts 10
cp 0
c 0
b 0
f 0
1
"""Defines physical port classes and related items."""
2
3
# System imports
4
from enum import IntEnum
5
6
# Local source tree imports
7
from pyof.foundation.base import GenericBitMask, GenericStruct
8
from pyof.foundation.basic_types import (
9
    Char, FixedTypeList, HWAddress, Pad, UBInt32)
10
from pyof.foundation.constants import OFP_MAX_PORT_NAME_LEN
11
12
# Third-party imports
13
14
__all__ = ('ListOfPorts', 'Port', 'PortNo', 'PortConfig', 'PortFeatures',
15
           'PortState')
16
17
18
class PortNo(IntEnum):
19
    """Port numbering.
20
21
    Ports are numbered starting from 1.
22
    """
23
24
    #: Maximum number of physical and logical switch ports.
25
    OFPP_MAX = 0xffffff00
26
    # Reserved OpenFlow port (fake output "ports")
27
    #: Send the packet out the input port. This reserved port must be
28
    #: explicitly used in order to send back out of the input port.
29
    OFPP_IN_PORT = 0xfffffff8
30
    #: Submit the packet to the first flow table
31
    #: NB: This destination port can only be used in packet-out messages.
32
    OFPP_TABLE = 0xfffffff9
33
    #: Process with normal L2/L3 switching.
34
    OFPP_NORMAL = 0xfffffffa
35
    #: All physical ports in VLAN, except input port and thos blocked or link
36
    #: down.
37
    OFPP_FLOOD = 0xfffffffb
38
    #: All physical ports except input port
39
    OFPP_ALL = 0xfffffffc
40
    #: Send to controller
41
    OFPP_CONTROLLER = 0xfffffffd
42
    #: Local openflow "port"
43
    OFPP_LOCAL = 0xfffffffe
44
    #: Wildcard port used only for flow mod (delete) and flow stats requests.
45
    #: Selects all flows regardless of output port (including flows with no
46
    #: output port).
47
    OFPP_ANY = 0xffffffff
48
49
50
class PortConfig(GenericBitMask):
51
    """Flags to indicate behavior of the physical port.
52
53
    These flags are used in :class:`Port` to describe the current
54
    configuration. They are used in the
55
    :class:`~pyof.v0x04.controller2switch.port_mod.PortMod`
56
    message to configure the port's behavior.
57
58
    The :attr:`OFPPC_PORT_DOWN` bit indicates that the port has been
59
    administratively brought down and should not be used by OpenFlow. The
60
    :attr:`~OFPPC_NO_RECV` bit indicates that packets received on that port
61
    should be ignored. The :attr:`OFPPC_NO_FWD` bit indicates that OpenFlow
62
    should not send packets to that port. The :attr:`OFPPC_NO_PACKET_IN` bit
63
    indicates that packets on that port that generate a table miss should never
64
    trigger a packet-in message to the controller.
65
66
    In general, the port config bits are set by the controller and not changed
67
    by the switch. Those bits may be useful for the controller to implement
68
    protocols such as STP or BFD. If the port config bits are changed by the
69
    switch through another administrative interface, the switch sends an
70
    :attr:`OFPT_PORT_STATUS` message to notify the controller of the change.
71
    """
72
73
    #: Port is administratively down.
74
    OFPPC_PORT_DOWN = 1 << 0
75
    #: Drop all packets received by port.
76
    OFPPC_NO_RECV = 1 << 2
77
    #: Drop packets forwarded to port.
78
    OFPPC_NO_FWD = 1 << 5
79
    #: Do not send packet-in msgs for port.
80
    OFPPC_NO_PACKET_IN = 1 << 6
81
82
83
class PortFeatures(GenericBitMask):
84
    """Physical ports features.
85
86
    The curr, advertised, supported, and peer fields indicate link modes
87
    (speed and duplexity), link type (copper/fiber) and link features
88
    (autonegotiation and pause).
89
90
    Multiple of these flags may be set simultaneously. If none of the port
91
    speed flags are set, the max_speed or curr_speed are used.
92
93
    The curr_speed and max_speed fields indicate the current and maximum bit
94
    rate (raw transmission speed) of the link in kbps. The number should be
95
    rounded to match common usage. For example, an optical 10 Gb Ethernet port
96
    should have this field set to 10000000 (instead of 10312500), and an OC-192
97
    port should have this field set to 10000000 (instead of 9953280).
98
99
    The max_speed fields indicate the maximum configured capacity of the link,
100
    whereas the curr_speed indicates the current capacity. If the port is a LAG
101
    with 3 links of 1Gb/s capacity, with one of the ports of the LAG being
102
    down, one port auto-negotiated at 1Gb/s and 1 port auto-negotiated at
103
    100Mb/s, the max_speed is 3 Gb/s and the curr_speed is 1.1 Gb/s.
104
    """
105
106
    #: 10 Mb half-duplex rate support.
107
    OFPPF_10MB_HD = 1 << 0
108
    #: 10 Mb full-duplex rate support.
109
    OFPPF_10MB_FD = 1 << 1
110
    #: 100 Mb half-duplex rate support.
111
    OFPPF_100MB_HD = 1 << 2
112
    #: 100 Mb full-duplex rate support.
113
    OFPPF_100MB_FD = 1 << 3
114
    #: 1 Gb half-duplex rate support.
115
    OFPPF_1GB_HD = 1 << 4
116
    #: 1 Gb full-duplex rate support.
117
    OFPPF_1GB_FD = 1 << 5
118
    #: 10 Gb full-duplex rate support.
119
    OFPPF_10GB_FD = 1 << 6
120
    #: 40 Gb full-duplex rate support.
121
    OFPPF_40GB_FD = 1 << 7
122
    #: 100 Gb full-duplex rate support.
123
    OFPPF_100GB_FD = 1 << 8
124
    #: 1 Tb full-duplex rate support.
125
    OFPPF_1TB_FD = 1 << 9
126
    #: Other rate, not in the list
127
    OFPPF_OTHER = 1 << 10
128
129
    #: Copper medium.
130
    OFPPF_COPPER = 1 << 11
131
    #: Fiber medium.
132
    OFPPF_FIBER = 1 << 12
133
    #: Auto-negotiation.
134
    OFPPF_AUTONEG = 1 << 13
135
    #: Pause.
136
    OFPPF_PAUSE = 1 << 14
137
    #: Asymmetric pause.
138
    OFPPF_PAUSE_ASYM = 1 << 15
139
140
141
class PortState(GenericBitMask):
142
    """Current state of the physical port.
143
144
    These are not configurable from the controller.
145
146
    The port state bits represent the state of the physical link or switch
147
    protocols outside of OpenFlow. The :attr:`~PortConfig.OFPPS_LINK_DOWN` bit
148
    indicates the the physical link is not present. The
149
    :attr:`~PortConfig.OFPPS_BLOCKED` bit indicates that a switch protocol
150
    outside of OpenFlow, such as 802.1D Spanning Tree, is preventing the use of
151
    that port with :attr:`~PortConfig.OFPP_FLOOD`.
152
153
    All port state bits are read-only and cannot be changed by the controller.
154
    When the port flags are changed, the switch sends an
155
    :attr:`v0x04.common.header.Type.OFPT_PORT_STATUS` message to notify the
156
    controller of the change.
157
    """
158
159
    #: Not physical link present.
160
    OFPPS_LINK_DOWN = 1 << 0
161
    #: Port is blocked.
162
    OFPPS_BLOCKED = 1 << 1
163
    #: Live for Fast Failover Group.
164
    OFPPS_LIVE = 1 << 2
165
166
167
# Classes
168
169
class Port(GenericStruct):
170
    """Description of a port.
171
172
    The port_no field uniquely identifies a port within a switch. The hw_addr
173
    field typically is the MAC address for the port;
174
    :data:`.OFP_MAX_ETH_ALEN` is 6. The name field is a null-terminated string
175
    containing a human-readable name for the interface.
176
    The value of :data:`.OFP_MAX_PORT_NAME_LEN` is 16.
177
178
    :attr:`curr`, :attr:`advertised`, :attr:`supported` and :attr:`peer` fields
179
    indicate link modes (speed and duplexity), link type (copper/fiber) and
180
    link features (autonegotiation and pause). They are bitmaps of
181
    :class:`PortFeatures` enum values that describe features.
182
    Multiple of these flags may be set simultaneously. If none of the port
183
    speed flags are set, the :attr:`max_speed` or :attr:`curr_speed` are used.
184
    """
185
186
    port_no = UBInt32()
187
    pad = Pad(4)
188
    hw_addr = HWAddress()
189
    pad2 = Pad(2)
190
    name = Char(length=OFP_MAX_PORT_NAME_LEN)
191
    config = UBInt32(enum_ref=PortConfig)
192
    state = UBInt32(enum_ref=PortState)
193
    curr = UBInt32(enum_ref=PortFeatures)
194
    advertised = UBInt32(enum_ref=PortFeatures)
195
    supported = UBInt32(enum_ref=PortFeatures)
196
    peer = UBInt32(enum_ref=PortFeatures)
197
    curr_speed = UBInt32()
198
    max_speed = UBInt32()
199
200
    def __init__(self, port_no=None, hw_addr=None, name=None, config=None,
201
                 state=None, curr=None, advertised=None, supported=None,
202
                 peer=None, curr_speed=None, max_speed=None):
203
        """Create a Port with the optional parameters below.
204
205
        Args:
206
            port_no (int): Port number.
207
            hw_addr (HWAddress): Hardware address.
208
            name (str): Null-terminated name.
209
            config (~pyof.v0x04.common.port.PortConfig):
210
                Bitmap of OFPPC* flags.
211
            state (~pyof.v0x04.common.port.PortState): Bitmap of OFPPS* flags.
212
            curr (~pyof.v0x04.common.port.PortFeatures): Current features.
213
            advertised (~pyof.v0x04.common.port.PortFeatures):
214
                Features being advertised by the port.
215
            supported (~pyof.v0x04.common.port.PortFeatures):
216
                Features supported by the port.
217
            peer (~pyof.v0x04.common.port.PortFeatures):
218
                Features advertised by peer.
219
            curr_speed (int): Current port bitrate in kbps.
220
            max_speed (int): Max port bitrate in kbps.
221
        """
222
        super().__init__()
223
        self.port_no = port_no
224
        self.hw_addr = hw_addr
225
        self.name = name
226
        self.config = config
227
        self.state = state
228
        self.curr = curr
229
        self.advertised = advertised
230
        self.supported = supported
231
        self.peer = peer
232
        self.curr_speed = curr_speed
233
        self.max_speed = max_speed
234
235
236
class ListOfPorts(FixedTypeList):
237
    """List of Ports.
238
239
    Represented by instances of :class:`Port` and used on
240
    :class:`~pyof.v0x04.controller2switch.features_reply.FeaturesReply`/
241
    :class:`~pyof.v0x04.controller2switch.features_reply.SwitchFeatures`
242
    objects.
243
    """
244
245
    def __init__(self, items=None):
246
        """Create a ListOfPort with the optional parameters below.
247
248
        Args:
249
            items (:class:`list`, :class:`~pyof.v0x04.common.port.Port`):
250
                One :class:`~pyof.v0x04.common.port.Port` instance or list.
251
        """
252
        super().__init__(pyof_class=Port,
253
                         items=items)
254