Takahiro Suzuki | d7bf820 | 2020-12-17 20:21:59 +0900 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2020-present Open Networking Foundation |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package l2oam |
| 18 | |
| 19 | import ( |
| 20 | "encoding/binary" |
| 21 | "encoding/hex" |
| 22 | "fmt" |
| 23 | |
| 24 | |
| 25 | "github.com/google/gopacket" |
| 26 | "github.com/google/gopacket/layers" |
| 27 | ) |
| 28 | |
| 29 | // TOAMGetRequest is a structure for GET request of TOAM message |
| 30 | type TOAMGetRequest struct { |
| 31 | layers.BaseLayer |
| 32 | Opcode uint8 |
| 33 | Flags uint16 |
| 34 | OAMPDUCode uint8 |
| 35 | OUId []byte // Organizationally Unique Identifier: 2a:ea:15 (Tibit Communications) |
| 36 | TOMIOpcode uint8 |
| 37 | CTBranch uint8 |
| 38 | CTType uint16 |
| 39 | CTLength uint8 |
| 40 | CTInstance uint32 |
| 41 | OCBranch uint8 |
| 42 | OCType uint16 |
| 43 | OCLength uint8 |
| 44 | OCInstance uint32 |
| 45 | VdBranch uint8 |
| 46 | VdLeaf uint16 |
| 47 | EndBranch uint8 |
| 48 | } |
| 49 | |
| 50 | // String returns the string expression of TOAMGetRequest |
| 51 | func (d *TOAMGetRequest) String() string { |
| 52 | message := fmt.Sprintf("Opcode:%02x, Flags:%04x, OAMPDUCode:%02x, OUId:%v", d.Opcode, d.Flags, d.OAMPDUCode, hex.EncodeToString(d.OUId)) |
| 53 | message = fmt.Sprintf("%s, TOMIOpcode:%02x", message, d.TOMIOpcode) |
| 54 | message = fmt.Sprintf("%s, CTBranch:%02x, CTType:%04x, CTLength:%02x, CTInstance:%08x", message, d.CTBranch, d.CTType, d.CTLength, d.CTInstance) |
| 55 | message = fmt.Sprintf("%s, OCBranch:%02x, OCType:%04x, OCLength:%02x, OCInstance:%08x", message, d.OCBranch, d.OCType, d.OCLength, d.OCInstance) |
| 56 | message = fmt.Sprintf("%s, VdBranch:%02x, VdLeaf:%04x, EndBranch:%02x", message, d.VdBranch, d.VdLeaf, d.EndBranch) |
| 57 | return message |
| 58 | } |
| 59 | |
| 60 | // Len returns the length of TOAMGetRequest |
| 61 | func (d *TOAMGetRequest) Len() int { |
| 62 | return 29 |
| 63 | } |
| 64 | |
| 65 | // LayerType returns the ethernet type of TOAMGetRequest |
| 66 | func (d *TOAMGetRequest) LayerType() gopacket.LayerType { return layers.LayerTypeEthernet } |
| 67 | |
| 68 | // SerializeTo serializes a data structure to byte arrays |
| 69 | func (d *TOAMGetRequest) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error { |
| 70 | plen := int(d.Len()) |
| 71 | data, err := b.PrependBytes(plen) |
| 72 | if err != nil { |
| 73 | return err |
| 74 | } |
| 75 | |
| 76 | i := 0 |
| 77 | data[i] = byte(d.Opcode) |
| 78 | i++ |
| 79 | binary.BigEndian.PutUint16(data[i:i+2], d.Flags) |
| 80 | i += 2 |
| 81 | data[i] = byte(d.OAMPDUCode) |
| 82 | i++ |
| 83 | copy(data[i:i+len(d.OUId)], d.OUId) |
| 84 | i += len(d.OUId) |
| 85 | data[i] = byte(d.TOMIOpcode) |
| 86 | i++ |
| 87 | data[i] = byte(d.CTBranch) |
| 88 | i++ |
| 89 | binary.BigEndian.PutUint16(data[i:i+2], d.CTType) |
| 90 | i += 2 |
| 91 | data[i] = byte(d.CTLength) |
| 92 | i++ |
| 93 | binary.BigEndian.PutUint32(data[i:i+4], d.CTInstance) |
| 94 | i += 4 |
| 95 | data[i] = byte(d.OCBranch) |
| 96 | i++ |
| 97 | binary.BigEndian.PutUint16(data[i:i+2], d.OCType) |
| 98 | i += 2 |
| 99 | data[i] = byte(d.OCLength) |
| 100 | i++ |
| 101 | binary.BigEndian.PutUint32(data[i:i+4], d.OCInstance) |
| 102 | i += 4 |
| 103 | data[i] = byte(d.VdBranch) |
| 104 | i++ |
| 105 | binary.BigEndian.PutUint16(data[i:i+2], d.VdLeaf) |
| 106 | i += 2 |
| 107 | data[i] = byte(d.EndBranch) |
| 108 | |
| 109 | return nil |
| 110 | } |
| 111 | |
| 112 | // TOAMGetResponse is a structure for GET response of TOAM message |
| 113 | type TOAMGetResponse struct { |
| 114 | layers.BaseLayer |
| 115 | Opcode uint8 |
| 116 | Flags uint16 |
| 117 | OAMPDUCode uint8 |
| 118 | OUId []byte // Organizationally Unique Identifier: 2a:ea:15 (Tibit Communications) |
| 119 | TOMIOpcode uint8 |
| 120 | CTBranch uint8 |
| 121 | CTType uint16 |
| 122 | CTLength uint8 |
| 123 | CTInstance uint32 |
| 124 | OCBranch uint8 |
| 125 | OCType uint16 |
| 126 | OCLength uint8 |
| 127 | OCInstance uint32 |
| 128 | VcBranch uint8 |
| 129 | VcLeaf uint16 |
| 130 | VcLength uint8 |
| 131 | } |
| 132 | |
| 133 | // String returns the string expression of TOAMGetResponse |
| 134 | func (d *TOAMGetResponse) String() string { |
| 135 | message := fmt.Sprintf("Opcode:%02x, Flags:%04x, OAMPDUCode:%02x, OUId:%v", d.Opcode, d.Flags, d.OAMPDUCode, hex.EncodeToString(d.OUId)) |
| 136 | message = fmt.Sprintf("%s, TOMIOpcode:%02x", message, d.TOMIOpcode) |
| 137 | message = fmt.Sprintf("%s, CTBranch:%02x, CTType:%04x, CTLength:%02x, CTInstance:%08x", message, d.CTBranch, d.CTType, d.CTLength, d.CTInstance) |
| 138 | message = fmt.Sprintf("%s, OCBranch:%02x, OCType:%04x, OCLength:%02x, OCInstance:%08x", message, d.OCBranch, d.OCType, d.OCLength, d.OCInstance) |
| 139 | message = fmt.Sprintf("%s, VcBranch:%02x, VcLeaf:%04x, VcLength:%02x", message, d.VcBranch, d.VcLeaf, d.VcLength) |
| 140 | return message |
| 141 | } |
| 142 | |
| 143 | // Len returns the length of TOAMGetResponse |
| 144 | func (d *TOAMGetResponse) Len() int { |
| 145 | return 28 |
| 146 | } |
| 147 | |
| 148 | // LayerType returns the ethernet type of TOAMGetResponse |
| 149 | func (d *TOAMGetResponse) LayerType() gopacket.LayerType { return layers.LayerTypeEthernet } |
| 150 | |
| 151 | // Decode decodes byte arrays to a data structure |
| 152 | func (d *TOAMGetResponse) Decode(data []byte) { |
| 153 | i := 0 |
| 154 | d.Opcode = data[i] |
| 155 | i++ |
| 156 | d.Flags = binary.BigEndian.Uint16(data[i : i+2]) |
| 157 | i += 2 |
| 158 | d.OAMPDUCode = data[i] |
| 159 | i++ |
| 160 | d.OUId = data[i : i+3] |
| 161 | i += len(d.OUId) |
| 162 | d.TOMIOpcode = data[i] |
| 163 | i++ |
| 164 | d.CTBranch = data[i] |
| 165 | i++ |
| 166 | d.CTType = binary.BigEndian.Uint16(data[i : i+2]) |
| 167 | i += 2 |
| 168 | d.CTLength = data[i] |
| 169 | i++ |
| 170 | d.CTInstance = binary.BigEndian.Uint32(data[i : i+4]) |
| 171 | i += 4 |
| 172 | d.OCBranch = data[i] |
| 173 | i++ |
| 174 | d.OCType = binary.BigEndian.Uint16(data[i : i+2]) |
| 175 | i += 2 |
| 176 | d.OCLength = data[i] |
| 177 | i++ |
| 178 | d.OCInstance = binary.BigEndian.Uint32(data[i : i+4]) |
| 179 | i += 4 |
| 180 | d.VcBranch = data[i] |
| 181 | i++ |
| 182 | d.VcLeaf = binary.BigEndian.Uint16(data[i : i+2]) |
| 183 | i += 2 |
| 184 | d.VcLength = data[i] |
| 185 | } |