Passed
Pull Request — master (#425)
by Carlos Eduardo
02:30
created

ListOfPorts.__init__()   A

Complexity

Conditions 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1.125

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 1
cts 2
cp 0.5
c 0
b 0
f 0
rs 9.6666
cc 1
crap 1.125
1
"""Defines physical port classes and related items."""
2
3
# System imports
4 1
from enum import IntEnum
5
6
# Local source tree imports
7 1
from pyof.foundation.base import GenericBitMask, GenericStruct
8 1
from pyof.foundation.basic_types import (
9
    Char, FixedTypeList, HWAddress, Pad, UBInt32)
10 1
from pyof.foundation.constants import OFP_MAX_PORT_NAME_LEN
11
12
# Third-party imports
13
14 1
__all__ = ('ListOfPorts', 'Port', 'PortNo', 'PortConfig', 'PortFeatures',
15
           'PortState')
16
17
18 1
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 1
    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 1
    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 1
    OFPP_TABLE = 0xfffffff9
33
    #: Process with normal L2/L3 switching.
34 1
    OFPP_NORMAL = 0xfffffffa
35
    #: All physical ports in VLAN, except input port and thos blocked or link
36
    #: down.
37 1
    OFPP_FLOOD = 0xfffffffb
38
    #: All physical ports except input port
39 1
    OFPP_ALL = 0xfffffffc
40
    #: Send to controller
41 1
    OFPP_CONTROLLER = 0xfffffffd
42
    #: Local openflow "port"
43 1
    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 1
    OFPP_ANY = 0xffffffff
48
49
50 1
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 1
    OFPPC_PORT_DOWN = 1 << 0
75
    #: Drop all packets received by port.
76 1
    OFPPC_NO_RECV = 1 << 2
77
    #: Drop packets forwarded to port.
78 1
    OFPPC_NO_FWD = 1 << 5
79
    #: Do not send packet-in msgs for port.
80 1
    OFPPC_NO_PACKET_IN = 1 << 6
81
82
83 1
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 1
    OFPPF_10MB_HD = 1 << 0
108
    #: 10 Mb full-duplex rate support.
109 1
    OFPPF_10MB_FD = 1 << 1
110
    #: 100 Mb half-duplex rate support.
111 1
    OFPPF_100MB_HD = 1 << 2
112
    #: 100 Mb full-duplex rate support.
113 1
    OFPPF_100MB_FD = 1 << 3
114
    #: 1 Gb half-duplex rate support.
115 1
    OFPPF_1GB_HD = 1 << 4
116
    #: 1 Gb full-duplex rate support.
117 1
    OFPPF_1GB_FD = 1 << 5
118
    #: 10 Gb full-duplex rate support.
119 1
    OFPPF_10GB_FD = 1 << 6
120
    #: 40 Gb full-duplex rate support.
121 1
    OFPPF_40GB_FD = 1 << 7
122
    #: 100 Gb full-duplex rate support.
123 1
    OFPPF_100GB_FD = 1 << 8
124
    #: 1 Tb full-duplex rate support.
125 1
    OFPPF_1TB_FD = 1 << 9
126
    #: Other rate, not in the list
127 1
    OFPPF_OTHER = 1 << 10
128
129
    #: Copper medium.
130 1
    OFPPF_COPPER = 1 << 11
131
    #: Fiber medium.
132 1
    OFPPF_FIBER = 1 << 12
133
    #: Auto-negotiation.
134 1
    OFPPF_AUTONEG = 1 << 13
135
    #: Pause.
136 1
    OFPPF_PAUSE = 1 << 14
137
    #: Asymmetric pause.
138 1
    OFPPF_PAUSE_ASYM = 1 << 15
139
140
141 1
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 1
    OFPPS_LINK_DOWN = 1 << 0
161
    #: Port is blocked.
162 1
    OFPPS_BLOCKED = 1 << 1
163
    #: Live for Fast Failover Group.
164 1
    OFPPS_LIVE = 1 << 2
165
166
167
# Classes
168
169
170 1
class Port(GenericStruct):
171
    """Description of a port.
172
173
    The port_no field uniquely identifies a port within a switch. The hw_addr
174
    field typically is the MAC address for the port;
175
    :data:`.OFP_MAX_ETH_ALEN` is 6. The name field is a null-terminated string
176
    containing a human-readable name for the interface.
177
    The value of :data:`.OFP_MAX_PORT_NAME_LEN` is 16.
178
179
    :attr:`curr`, :attr:`advertised`, :attr:`supported` and :attr:`peer` fields
180
    indicate link modes (speed and duplexity), link type (copper/fiber) and
181
    link features (autonegotiation and pause). They are bitmaps of
182
    :class:`PortFeatures` enum values that describe features.
183
    Multiple of these flags may be set simultaneously. If none of the port
184
    speed flags are set, the :attr:`max_speed` or :attr:`curr_speed` are used.
185
    """
186
187 1
    port_no = UBInt32()
188 1
    pad = Pad(4)
189 1
    hw_addr = HWAddress()
190 1
    pad2 = Pad(2)
191 1
    name = Char(length=OFP_MAX_PORT_NAME_LEN)
192 1
    config = UBInt32(enum_ref=PortConfig)
193 1
    state = UBInt32(enum_ref=PortState)
194 1
    curr = UBInt32(enum_ref=PortFeatures)
195 1
    advertised = UBInt32(enum_ref=PortFeatures)
196 1
    supported = UBInt32(enum_ref=PortFeatures)
197 1
    peer = UBInt32(enum_ref=PortFeatures)
198 1
    curr_speed = UBInt32()
199 1
    max_speed = UBInt32()
200
201 1
    def __init__(self, port_no=None, hw_addr=None, name=None, config=None,
202
                 state=None, curr=None, advertised=None, supported=None,
203
                 peer=None, curr_speed=None, max_speed=None):
204
        """The constructor takes the optional parameters below.
205
206
        Args:
207
            port_no (int): Port number.
208
            hw_addr (HWAddress): Hardware address.
209
            name (str): Null-terminated name.
210
            config (~pyof.v0x04.common.port.PortConfig):
211
                Bitmap of OFPPC* flags.
212
            state (~pyof.v0x04.common.port.PortState): Bitmap of OFPPS* flags.
213
            curr (~pyof.v0x04.common.port.PortFeatures): Current features.
214
            advertised (~pyof.v0x04.common.port.PortFeatures):
215
                Features being advertised by the port.
216
            supported (~pyof.v0x04.common.port.PortFeatures):
217
                Features supported by the port.
218
            peer (~pyof.v0x04.common.port.PortFeatures):
219
                Features advertised by peer.
220
            curr_speed (int): Current port bitrate in kbps.
221
            max_speed (int): Max port bitrate in kbps.
222
        """
223 1
        super().__init__()
224 1
        self.port_no = port_no
225 1
        self.hw_addr = hw_addr
226 1
        self.name = name
227 1
        self.config = config
228 1
        self.state = state
229 1
        self.curr = curr
230 1
        self.advertised = advertised
231 1
        self.supported = supported
232 1
        self.peer = peer
233 1
        self.curr_speed = curr_speed
234 1
        self.max_speed = max_speed
235
236
237 1
class ListOfPorts(FixedTypeList):
238
    """List of Ports.
239
240
    Represented by instances of :class:`Port` and used on
241
    :class:`~pyof.v0x04.controller2switch.features_reply.FeaturesReply`/
242
    :class:`~pyof.v0x04.controller2switch.features_reply.SwitchFeatures`
243
    objects.
244
    """
245
246 1
    def __init__(self, items=None):
247
        """The constructor takes the optional parameter below.
248
249
        Args:
250
            items (:class:`list`, :class:`~pyof.v0x04.common.port.Port`):
251
                One :class:`~pyof.v0x04.common.port.Port` instance or list.
252
        """
253
        super().__init__(pyof_class=Port,
254
                         items=items)
255