blob: 0fe549723fb3ca0f47f88cb0f7f1dff6b0539de6 [file] [log] [blame]
Rich Lane4d9f0f62013-05-09 15:50:57 -07001# Copyright 2013, Big Switch Networks, Inc.
2#
3# LoxiGen is licensed under the Eclipse Public License, version 1.0 (EPL), with
4# the following special exception:
5#
6# LOXI Exception
7#
8# As a special exception to the terms of the EPL, you may distribute libraries
9# generated by LoxiGen (LoxiGen Libraries) under the terms of your choice, provided
10# that copyright and licensing notices generated by LoxiGen are not altered or removed
11# from the LoxiGen Libraries and the notice provided below is (i) included in
12# the LoxiGen Libraries, if distributed in source code form and (ii) included in any
13# documentation for the LoxiGen Libraries, if distributed in binary form.
14#
15# Notice: "Copyright 2013, Big Switch Networks, Inc. This library was generated by the LoxiGen Compiler."
16#
17# You may not use this file except in compliance with the EPL or LOXI Exception. You may obtain
18# a copy of the EPL at:
19#
20# http://www.eclipse.org/legal/epl-v10.html
21#
22# Unless required by applicable law or agreed to in writing, software
23# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
24# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
25# EPL for the specific language governing permissions and limitations
26# under the EPL.
27
Andreas Wundsamfef7d5f2013-08-01 22:15:44 -070028from generic_utils import find
Rich Lane4d9f0f62013-05-09 15:50:57 -070029from collections import namedtuple
30
31# This module is intended to be imported like this: from loxi_ir import *
32# All public names are prefixed with 'OF'.
33__all__ = [
34 'OFInput',
35 'OFProtocol',
36 'OFClass',
37 'OFDataMember',
38 'OFTypeMember',
Andreas Wundsam780e0c92013-08-02 17:48:27 -070039 'OFDiscriminatorMember',
Rich Lane4d9f0f62013-05-09 15:50:57 -070040 'OFLengthMember',
41 'OFFieldLengthMember',
42 'OFPadMember',
43 'OFEnum',
Andreas Wundsam4ee51462013-07-30 11:00:37 -070044 'OFEnumEntry'
Rich Lane4d9f0f62013-05-09 15:50:57 -070045]
46
47"""
48One input file
49
Rich Lane72875d62013-11-13 12:39:53 -080050@param name Name of the input file
Rich Lane4d9f0f62013-05-09 15:50:57 -070051@param wire_versions Set of integer wire versions this file applies to
52@param classes List of OFClass objects in the same order as in the file
53@param enums List of Enum objects in the same order as in the file
54"""
Rich Lane72875d62013-11-13 12:39:53 -080055OFInput = namedtuple('OFInput', ['name', 'wire_versions', 'classes', 'enums'])
Rich Lane4d9f0f62013-05-09 15:50:57 -070056
57"""
58One version of the OpenFlow protocol
59
60Combination of multiple OFInput objects.
61
62@param wire_version
63@param classes List of OFClass objects
64@param enums List of Enum objects
65"""
Rich Lane5902b492013-10-01 22:30:46 -070066class OFProtocol(namedtuple('OFProtocol', ['wire_version', 'classes', 'enums'])):
67 def class_by_name(self, name):
68 return find(lambda ofclass: ofclass.name == name, self.classes)
Rich Lane4d9f0f62013-05-09 15:50:57 -070069
Rich Lane7c49d4e2013-10-02 15:57:46 -070070 def enum_by_name(self, name):
71 return find(lambda enum: enum.name == name, self.enums)
72
Rich Lane4d9f0f62013-05-09 15:50:57 -070073"""
74An OpenFlow class
75
76All compound objects like messages, actions, instructions, etc are
77uniformly represented by this class.
78
79The members are in the same order as on the wire.
80
81@param name
Andreas Wundsam780e0c92013-08-02 17:48:27 -070082@param superclass name of the super class
Rich Lane4d9f0f62013-05-09 15:50:57 -070083@param members List of *Member objects
Andreas Wundsamfef7d5f2013-08-01 22:15:44 -070084@param params optional dictionary of parameters
Rich Lane4d9f0f62013-05-09 15:50:57 -070085"""
Andreas Wundsam780e0c92013-08-02 17:48:27 -070086class OFClass(namedtuple('OFClass', ['name', 'superclass', 'members', 'virtual', 'params'])):
Andreas Wundsamfef7d5f2013-08-01 22:15:44 -070087 def member_by_name(self, name):
Rich Lane24c025f2013-10-01 22:05:35 -070088 return find(lambda m: hasattr(m, "name") and m.name == name, self.members)
89
90 @property
91 def discriminator(self):
92 return find(lambda m: type(m) == OFDiscriminatorMember, self.members)
Rich Lane4d9f0f62013-05-09 15:50:57 -070093
94"""
95Normal field
96
97@param name
98@param oftype C-like type string
99
100Example: packet_in.buffer_id
101"""
102OFDataMember = namedtuple('OFDataMember', ['name', 'oftype'])
103
104"""
Andreas Wundsam780e0c92013-08-02 17:48:27 -0700105Field that declares that this is an abstract super-class and
106that the sub classes will be discriminated based on this field.
107E.g., 'type' is the discriminator member of the abstract superclass
108of_action.
109
110@param name
111"""
Andreas Wundsam7933beb2013-08-02 22:36:42 -0700112OFDiscriminatorMember = namedtuple('OFDiscriminatorMember', ['name', 'oftype'])
Andreas Wundsam780e0c92013-08-02 17:48:27 -0700113
114"""
Rich Lane4d9f0f62013-05-09 15:50:57 -0700115Field used to determine the type of an OpenFlow object
116
117@param name
118@param oftype C-like type string
119@param value Fixed type value
120
121Example: packet_in.type, flow_add._command
122"""
123OFTypeMember = namedtuple('OFTypeMember', ['name', 'oftype', 'value'])
124
125"""
126Field with the length of the containing object
127
128@param name
129@param oftype C-like type string
130
131Example: packet_in.length, action_output.len
132"""
133OFLengthMember = namedtuple('OFLengthMember', ['name', 'oftype'])
134
135"""
136Field with the length of another field in the containing object
137
138@param name
139@param oftype C-like type string
140@param field_name Peer field whose length this field contains
141
142Example: packet_out.actions_len (only usage)
143"""
144OFFieldLengthMember = namedtuple('OFFieldLengthMember', ['name', 'oftype', 'field_name'])
145
146"""
147Zero-filled padding
148
149@param length Length in bytes
150
151Example: packet_in.pad
152"""
153OFPadMember = namedtuple('OFPadMember', ['length'])
154
155"""
156An OpenFlow enumeration
157
158All values are Python ints.
159
160@param name
Andreas Wundsam4ee51462013-07-30 11:00:37 -0700161@param entries List of OFEnumEntry objects in input order
162@params dict of optional params. Currently defined:
163 - wire_type: the low_level type of the enum values (uint8,...)
Rich Lane4d9f0f62013-05-09 15:50:57 -0700164"""
Andreas Wundsam4ee51462013-07-30 11:00:37 -0700165class OFEnum(namedtuple('OFEnum', ['name', 'entries', 'params'])):
166 @property
167 def values(self):
168 return [(e.name, e.value) for e in self.entries]
169
Andreas Wundsam0a218c72013-09-17 13:54:20 -0700170 @property
171 def is_bitmask(self):
172 return "bitmask" in self.params and self.params['bitmask']
173
174
Andreas Wundsam4ee51462013-07-30 11:00:37 -0700175OFEnumEntry = namedtuple('OFEnumEntry', ['name', 'value', 'params'])