Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 1 | package goloxi |
| 2 | |
| 3 | import "fmt" |
| 4 | |
| 5 | const ( |
| 6 | VERSION_1_0 = 1 |
| 7 | VERSION_1_1 = 2 |
| 8 | VERSION_1_2 = 3 |
| 9 | VERSION_1_3 = 4 |
| 10 | VERSION_1_4 = 5 |
| 11 | VERSION_1_5 = 6 |
| 12 | ) |
| 13 | |
| 14 | const ( |
| 15 | OFPTHello = 0 |
| 16 | OFPTError = 1 |
| 17 | OFPTEchoRequest = 2 |
| 18 | OFPTEchoReply = 3 |
| 19 | OFPTExperimenter = 4 |
| 20 | ) |
| 21 | |
| 22 | type Serializable interface { |
| 23 | Serialize(encoder *Encoder) error |
| 24 | } |
| 25 | |
| 26 | type Deserializable interface { |
| 27 | Decode(decoder *Decoder) error |
| 28 | } |
| 29 | |
| 30 | type Header struct { |
| 31 | Version uint8 |
| 32 | Type uint8 |
| 33 | Length uint16 |
| 34 | Xid uint32 |
| 35 | } |
| 36 | |
| 37 | type Message interface { |
| 38 | Serializable |
| 39 | GetVersion() uint8 |
| 40 | GetLength() uint16 |
| 41 | MessageType() uint8 |
| 42 | MessageName() string |
| 43 | GetXid() uint32 |
| 44 | SetXid(xid uint32) |
| 45 | } |
| 46 | |
| 47 | type Uint128 struct { |
| 48 | Hi uint64 |
| 49 | Lo uint64 |
| 50 | } |
| 51 | |
| 52 | type IOxm interface { |
| 53 | Serializable |
| 54 | GetOXMName() string |
| 55 | GetOXMValue() interface{} |
| 56 | } |
| 57 | |
| 58 | type IOxmMasked interface { |
| 59 | Serializable |
| 60 | GetOXMName() string |
| 61 | GetOXMValue() interface{} |
| 62 | GetOXMValueMask() interface{} |
| 63 | } |
| 64 | |
| 65 | type IOxmId interface { |
| 66 | Serializable |
| 67 | GetOXMName() string |
| 68 | } |
| 69 | |
| 70 | type IAction interface { |
| 71 | Serializable |
| 72 | GetType() uint16 |
| 73 | GetLen() uint16 |
| 74 | GetActionName() string |
| 75 | GetActionFields() map[string]interface{} |
| 76 | } |
| 77 | |
| 78 | func (self *Header) Decode(decoder *Decoder) (err error) { |
| 79 | if decoder.Length() < 8 { |
| 80 | return fmt.Errorf("Header packet too short: %d < 4", decoder.Length()) |
| 81 | } |
| 82 | |
| 83 | defer func() { |
| 84 | if r := recover(); r != nil { |
| 85 | var ok bool |
| 86 | err, ok = r.(error) |
| 87 | if !ok { |
| 88 | err = fmt.Errorf("Error while parsing OpenFlow packet: %+v", r) |
| 89 | } |
| 90 | } |
| 91 | }() |
| 92 | |
| 93 | self.Version = decoder.ReadByte() |
| 94 | self.Type = decoder.ReadByte() |
| 95 | self.Length = decoder.ReadUint16() |
| 96 | self.Xid = decoder.ReadUint32() |
| 97 | |
| 98 | return nil |
| 99 | } |