Rich Lane | 4d9f0f6 | 2013-05-09 15:50:57 -0700 | [diff] [blame] | 1 | # 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 Wundsam | fef7d5f | 2013-08-01 22:15:44 -0700 | [diff] [blame] | 28 | from generic_utils import find |
Rich Lane | 4d9f0f6 | 2013-05-09 15:50:57 -0700 | [diff] [blame] | 29 | from 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 Wundsam | 780e0c9 | 2013-08-02 17:48:27 -0700 | [diff] [blame] | 39 | 'OFDiscriminatorMember', |
Rich Lane | 4d9f0f6 | 2013-05-09 15:50:57 -0700 | [diff] [blame] | 40 | 'OFLengthMember', |
| 41 | 'OFFieldLengthMember', |
| 42 | 'OFPadMember', |
| 43 | 'OFEnum', |
Andreas Wundsam | 4ee5146 | 2013-07-30 11:00:37 -0700 | [diff] [blame] | 44 | 'OFEnumEntry' |
Rich Lane | 4d9f0f6 | 2013-05-09 15:50:57 -0700 | [diff] [blame] | 45 | ] |
| 46 | |
| 47 | """ |
| 48 | One input file |
| 49 | |
| 50 | @param wire_versions Set of integer wire versions this file applies to |
| 51 | @param classes List of OFClass objects in the same order as in the file |
| 52 | @param enums List of Enum objects in the same order as in the file |
| 53 | """ |
| 54 | OFInput = namedtuple('OFInput', ['wire_versions', 'classes', 'enums']) |
| 55 | |
| 56 | """ |
| 57 | One version of the OpenFlow protocol |
| 58 | |
| 59 | Combination of multiple OFInput objects. |
| 60 | |
| 61 | @param wire_version |
| 62 | @param classes List of OFClass objects |
| 63 | @param enums List of Enum objects |
| 64 | """ |
Rich Lane | 5902b49 | 2013-10-01 22:30:46 -0700 | [diff] [blame] | 65 | class OFProtocol(namedtuple('OFProtocol', ['wire_version', 'classes', 'enums'])): |
| 66 | def class_by_name(self, name): |
| 67 | return find(lambda ofclass: ofclass.name == name, self.classes) |
Rich Lane | 4d9f0f6 | 2013-05-09 15:50:57 -0700 | [diff] [blame] | 68 | |
Rich Lane | 7c49d4e | 2013-10-02 15:57:46 -0700 | [diff] [blame] | 69 | def enum_by_name(self, name): |
| 70 | return find(lambda enum: enum.name == name, self.enums) |
| 71 | |
Rich Lane | 4d9f0f6 | 2013-05-09 15:50:57 -0700 | [diff] [blame] | 72 | """ |
| 73 | An OpenFlow class |
| 74 | |
| 75 | All compound objects like messages, actions, instructions, etc are |
| 76 | uniformly represented by this class. |
| 77 | |
| 78 | The members are in the same order as on the wire. |
| 79 | |
| 80 | @param name |
Andreas Wundsam | 780e0c9 | 2013-08-02 17:48:27 -0700 | [diff] [blame] | 81 | @param superclass name of the super class |
Rich Lane | 4d9f0f6 | 2013-05-09 15:50:57 -0700 | [diff] [blame] | 82 | @param members List of *Member objects |
Andreas Wundsam | fef7d5f | 2013-08-01 22:15:44 -0700 | [diff] [blame] | 83 | @param params optional dictionary of parameters |
Rich Lane | 4d9f0f6 | 2013-05-09 15:50:57 -0700 | [diff] [blame] | 84 | """ |
Andreas Wundsam | 780e0c9 | 2013-08-02 17:48:27 -0700 | [diff] [blame] | 85 | class OFClass(namedtuple('OFClass', ['name', 'superclass', 'members', 'virtual', 'params'])): |
Andreas Wundsam | fef7d5f | 2013-08-01 22:15:44 -0700 | [diff] [blame] | 86 | def member_by_name(self, name): |
Rich Lane | 24c025f | 2013-10-01 22:05:35 -0700 | [diff] [blame] | 87 | return find(lambda m: hasattr(m, "name") and m.name == name, self.members) |
| 88 | |
| 89 | @property |
| 90 | def discriminator(self): |
| 91 | return find(lambda m: type(m) == OFDiscriminatorMember, self.members) |
Rich Lane | 4d9f0f6 | 2013-05-09 15:50:57 -0700 | [diff] [blame] | 92 | |
| 93 | """ |
| 94 | Normal field |
| 95 | |
| 96 | @param name |
| 97 | @param oftype C-like type string |
| 98 | |
| 99 | Example: packet_in.buffer_id |
| 100 | """ |
| 101 | OFDataMember = namedtuple('OFDataMember', ['name', 'oftype']) |
| 102 | |
| 103 | """ |
Andreas Wundsam | 780e0c9 | 2013-08-02 17:48:27 -0700 | [diff] [blame] | 104 | Field that declares that this is an abstract super-class and |
| 105 | that the sub classes will be discriminated based on this field. |
| 106 | E.g., 'type' is the discriminator member of the abstract superclass |
| 107 | of_action. |
| 108 | |
| 109 | @param name |
| 110 | """ |
Andreas Wundsam | 7933beb | 2013-08-02 22:36:42 -0700 | [diff] [blame] | 111 | OFDiscriminatorMember = namedtuple('OFDiscriminatorMember', ['name', 'oftype']) |
Andreas Wundsam | 780e0c9 | 2013-08-02 17:48:27 -0700 | [diff] [blame] | 112 | |
| 113 | """ |
Rich Lane | 4d9f0f6 | 2013-05-09 15:50:57 -0700 | [diff] [blame] | 114 | Field used to determine the type of an OpenFlow object |
| 115 | |
| 116 | @param name |
| 117 | @param oftype C-like type string |
| 118 | @param value Fixed type value |
| 119 | |
| 120 | Example: packet_in.type, flow_add._command |
| 121 | """ |
| 122 | OFTypeMember = namedtuple('OFTypeMember', ['name', 'oftype', 'value']) |
| 123 | |
| 124 | """ |
| 125 | Field with the length of the containing object |
| 126 | |
| 127 | @param name |
| 128 | @param oftype C-like type string |
| 129 | |
| 130 | Example: packet_in.length, action_output.len |
| 131 | """ |
| 132 | OFLengthMember = namedtuple('OFLengthMember', ['name', 'oftype']) |
| 133 | |
| 134 | """ |
| 135 | Field with the length of another field in the containing object |
| 136 | |
| 137 | @param name |
| 138 | @param oftype C-like type string |
| 139 | @param field_name Peer field whose length this field contains |
| 140 | |
| 141 | Example: packet_out.actions_len (only usage) |
| 142 | """ |
| 143 | OFFieldLengthMember = namedtuple('OFFieldLengthMember', ['name', 'oftype', 'field_name']) |
| 144 | |
| 145 | """ |
| 146 | Zero-filled padding |
| 147 | |
| 148 | @param length Length in bytes |
| 149 | |
| 150 | Example: packet_in.pad |
| 151 | """ |
| 152 | OFPadMember = namedtuple('OFPadMember', ['length']) |
| 153 | |
| 154 | """ |
| 155 | An OpenFlow enumeration |
| 156 | |
| 157 | All values are Python ints. |
| 158 | |
| 159 | @param name |
Andreas Wundsam | 4ee5146 | 2013-07-30 11:00:37 -0700 | [diff] [blame] | 160 | @param entries List of OFEnumEntry objects in input order |
| 161 | @params dict of optional params. Currently defined: |
| 162 | - wire_type: the low_level type of the enum values (uint8,...) |
Rich Lane | 4d9f0f6 | 2013-05-09 15:50:57 -0700 | [diff] [blame] | 163 | """ |
Andreas Wundsam | 4ee5146 | 2013-07-30 11:00:37 -0700 | [diff] [blame] | 164 | class OFEnum(namedtuple('OFEnum', ['name', 'entries', 'params'])): |
| 165 | @property |
| 166 | def values(self): |
| 167 | return [(e.name, e.value) for e in self.entries] |
| 168 | |
Andreas Wundsam | 0a218c7 | 2013-09-17 13:54:20 -0700 | [diff] [blame] | 169 | @property |
| 170 | def is_bitmask(self): |
| 171 | return "bitmask" in self.params and self.params['bitmask'] |
| 172 | |
| 173 | |
Andreas Wundsam | 4ee5146 | 2013-07-30 11:00:37 -0700 | [diff] [blame] | 174 | OFEnumEntry = namedtuple('OFEnumEntry', ['name', 'value', 'params']) |