1
|
|
|
"""Defines Header 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 GenericStruct |
8
|
1 |
|
from pyof.foundation.basic_types import UBInt8, UBInt16, UBInt32 |
9
|
1 |
|
from pyof.v0x01.common.constants import OFP_VERSION |
10
|
|
|
|
11
|
|
|
# Third-party imports |
12
|
|
|
|
13
|
1 |
|
__all__ = ('Header', 'Type') |
14
|
|
|
|
15
|
|
|
# Enums |
16
|
|
|
|
17
|
|
|
|
18
|
1 |
|
class Type(IntEnum): |
19
|
|
|
"""Enumeration of Message Types.""" |
20
|
|
|
|
21
|
|
|
# Symetric/Immutable messages |
22
|
1 |
|
OFPT_HELLO = 0 |
23
|
1 |
|
OFPT_ERROR = 1 |
24
|
1 |
|
OFPT_ECHO_REQUEST = 2 |
25
|
1 |
|
OFPT_ECHO_REPLY = 3 |
26
|
1 |
|
OFPT_VENDOR = 4 |
27
|
|
|
|
28
|
|
|
# Switch configuration messages |
29
|
|
|
# Controller/Switch messages |
30
|
1 |
|
OFPT_FEATURES_REQUEST = 5 |
31
|
1 |
|
OFPT_FEATURES_REPLY = 6 |
32
|
1 |
|
OFPT_GET_CONFIG_REQUEST = 7 |
33
|
1 |
|
OFPT_GET_CONFIG_REPLY = 8 |
34
|
1 |
|
OFPT_SET_CONFIG = 9 |
35
|
|
|
|
36
|
|
|
# Async messages |
37
|
1 |
|
OFPT_PACKET_IN = 10 |
38
|
1 |
|
OFPT_FLOW_REMOVED = 11 |
39
|
1 |
|
OFPT_PORT_STATUS = 12 |
40
|
|
|
|
41
|
|
|
# Controller command messages |
42
|
|
|
# Controller/switch message |
43
|
1 |
|
OFPT_PACKET_OUT = 13 |
44
|
1 |
|
OFPT_FLOW_MOD = 14 |
45
|
1 |
|
OFPT_PORT_MOD = 15 |
46
|
|
|
|
47
|
|
|
# Statistics messages |
48
|
|
|
# Controller/Switch message |
49
|
1 |
|
OFPT_STATS_REQUEST = 16 |
50
|
1 |
|
OFPT_STATS_REPLY = 17 |
51
|
|
|
|
52
|
|
|
# Barrier messages |
53
|
|
|
# Controller/Switch message |
54
|
1 |
|
OFPT_BARRIER_REQUEST = 18 |
55
|
1 |
|
OFPT_BARRIER_REPLY = 19 |
56
|
|
|
|
57
|
|
|
# Queue Configuration messages |
58
|
|
|
# Controller/Switch message |
59
|
1 |
|
OFPT_QUEUE_GET_CONFIG_REQUEST = 20 |
60
|
1 |
|
OFPT_QUEUE_GET_CONFIG_REPLY = 21 |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
# Classes |
64
|
|
|
|
65
|
|
|
|
66
|
1 |
|
class Header(GenericStruct): |
67
|
|
|
"""Representation of an OpenFlow message Header.""" |
68
|
|
|
|
69
|
1 |
|
version = UBInt8(OFP_VERSION) |
70
|
1 |
|
message_type = UBInt8(enum_ref=Type) |
71
|
1 |
|
length = UBInt16() |
72
|
1 |
|
xid = UBInt32() |
73
|
|
|
|
74
|
1 |
|
def __init__(self, message_type=None, length=None, xid=None): |
75
|
|
|
"""Create a Header with the optional parameters below. |
76
|
|
|
|
77
|
|
|
Args: |
78
|
|
|
message_type (~pyof.v0x01.common.header.Type): Type of the message. |
79
|
|
|
xid (int): ID of the message. Defaults to a random integer. |
80
|
|
|
length (int): Length of the message, including the header itself. |
81
|
|
|
""" |
82
|
1 |
|
super().__init__() |
83
|
1 |
|
self.message_type = message_type |
84
|
1 |
|
self.length = length |
85
|
1 |
|
self.xid = xid |
86
|
|
|
|
87
|
1 |
|
def __str__(self): |
88
|
|
|
"""Get just the header type. Eg.: 'OFPT_SET_CONFIG'.""" |
89
|
|
|
return self.message_type.name |
90
|
|
|
|
91
|
1 |
|
def __repr__(self): |
92
|
|
|
"""Show a full representation of the Header, including version.""" |
93
|
|
|
return "%s(message_type=%s, length=%r, xid=%r, version=%r)" \ |
94
|
|
|
% (self.__class__.__name__, self.message_type, self.length, |
95
|
|
|
self.xid, self.version) |
96
|
|
|
|