blob: 49c335a73fca623fbd7c713f017d28082e7c809e [file] [log] [blame]
Takahiro Suzukid7bf8202020-12-17 20:21:59 +09001/*
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
17package l2oam
18
19import (
20 "encoding/binary"
21
22 "github.com/google/gopacket"
23 "github.com/google/gopacket/layers"
24)
25
26// GenerateCaptureProtocols generates "Protocol Filter/Capture Protocols" message
27func GenerateCaptureProtocols(ocInstance uint32) gopacket.SerializableLayer {
28
29 return &SetGenerteCaptureProtocolsreq{
30 // IEEE 1904.2
31 Opcode: 0x03,
32 // OAM Protocol
33 Flags: 0x0050,
34 OAMPDUCode: 0xfe,
35 OUId: []byte{0x2a, 0xea, 0x15},
36 // TiBit OLT Management Interface
37 TOMIOpcode: 0x03,
38 // Correlation Tag
39 CTBranch: 0x0c,
40 CTType: 0x0c7a,
41 CTLength: 4,
42 CTInstance: getOltInstance(),
43 // Object Context
44 OCBranch: 0x0c,
45 OCType: 0x0cff,
46 OCLength: 4,
47 OCInstance: ocInstance,
48 // Vc
49 VcBranch: 0xcf,
50 VcLeaf: 0x0003,
51 VcLength: 5,
52 // EC
53 ECLength: 4,
54 //Protocol
55 ProtocolLength: 1,
56 ProtocolValue: 0x01,
57 //Action
58 ActionLength: 1,
59 ActionValue: 0x01,
60 // End
61 EndBranch: 0x00,
62 }
63
64}
65
66// SetGenerteCaptureProtocolsreq is a structure for a request of "Protocol Filter/Capture Protocols" message
67type SetGenerteCaptureProtocolsreq struct {
68 layers.BaseLayer
69 Opcode uint8
70 Flags uint16
71 OAMPDUCode uint8
72 OUId []byte // Organizationally Unique Identifier: 2a:ea:15 (Tibit Communications)
73 TOMIOpcode uint8
74 CTBranch uint8
75 CTType uint16
76 CTLength uint8
77 CTInstance uint32
78 OCBranch uint8
79 OCType uint16
80 OCLength uint8
81 OCInstance uint32
82 VcBranch uint8
83 VcLeaf uint16
84 VcLength uint8
85 ECLength uint8
86 ProtocolLength uint8
87 ProtocolValue uint8
88 ActionLength uint8
89 ActionValue uint8
90 EndBranch uint8
91}
92
93// Len returns the length of SetGenerteCaptureProtocolsreq
94func (d *SetGenerteCaptureProtocolsreq) Len() int {
95 return 21 + int(d.CTLength) + int(d.OCLength) + int(d.VcLength)
96}
97
98// LayerType returns the ethernet type of SetGenerteCaptureProtocolsreq
99func (d *SetGenerteCaptureProtocolsreq) LayerType() gopacket.LayerType {
100 return layers.LayerTypeEthernet
101}
102
103// SerializeTo serializes a data structure to byte arrays
104func (d *SetGenerteCaptureProtocolsreq) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
105 plen := int(d.Len())
106 data, err := b.PrependBytes(plen)
107 if err != nil {
108 return err
109 }
110
111 i := 0
112 data[i] = byte(d.Opcode)
113 i++
114 binary.BigEndian.PutUint16(data[i:i+2], d.Flags)
115 i += 2
116 data[i] = byte(d.OAMPDUCode)
117 i++
118 copy(data[i:i+len(d.OUId)], d.OUId)
119 i += len(d.OUId)
120 data[i] = byte(d.TOMIOpcode)
121 i++
122 data[i] = byte(d.CTBranch)
123 i++
124 binary.BigEndian.PutUint16(data[i:i+2], d.CTType)
125 i += 2
126 data[i] = byte(d.CTLength)
127 i++
128 binary.BigEndian.PutUint32(data[i:i+4], d.CTInstance)
129 i += 4
130 data[i] = byte(d.OCBranch)
131 i++
132 binary.BigEndian.PutUint16(data[i:i+2], d.OCType)
133 i += 2
134 data[i] = byte(d.OCLength)
135 i++
136 binary.BigEndian.PutUint32(data[i:i+4], d.OCInstance)
137 i += 4
138 data[i] = byte(d.VcBranch)
139 i++
140 binary.BigEndian.PutUint16(data[i:i+2], d.VcLeaf)
141 i += 2
142 data[i] = byte(d.VcLength)
143 i++
144 data[i] = byte(d.ECLength)
145 i++
146 data[i] = byte(d.ProtocolLength)
147 i++
148 data[i] = byte(d.ProtocolValue)
149 i++
150 data[i] = byte(d.ActionLength)
151 i++
152 data[i] = byte(d.ActionValue)
153 i++
154 data[i] = byte(d.EndBranch)
155
156 return nil
157
158}
159
160// Decode decodes byte arrays to a data structure
161func (d *SetGenerteCaptureProtocolsreq) Decode(data []byte) error {
162 i := 0
163 d.Opcode = data[i]
164 i++
165 d.Flags = binary.BigEndian.Uint16(data[i : i+2])
166 i += 2
167 d.OAMPDUCode = data[i]
168 i++
169 d.OUId = data[i : i+3]
170 i += len(d.OUId)
171 d.TOMIOpcode = data[i]
172 i++
173 d.CTBranch = data[i]
174 i++
175 d.CTType = binary.BigEndian.Uint16(data[i : i+2])
176 i += 2
177 d.CTLength = data[i]
178 i++
179 d.CTInstance = binary.BigEndian.Uint32(data[i : i+4])
180 i += 4
181 d.OCBranch = data[i]
182 i++
183 d.OCType = binary.BigEndian.Uint16(data[i : i+2])
184 i += 2
185 d.OCLength = data[i]
186 i++
187 d.OCInstance = binary.BigEndian.Uint32(data[i : i+4])
188 i += 4
189 d.VcBranch = data[i]
190 i++
191 d.VcLeaf = binary.BigEndian.Uint16(data[i : i+2])
192 i += 2
193 d.VcLength = data[i]
194 i++
195 d.ECLength = data[i]
196 i++
197 d.ProtocolLength = data[i]
198 i++
199 d.ProtocolValue = data[i]
200 i++
201 d.ActionLength = data[i]
202 i++
203 d.ActionValue = data[i]
204 i++
205 d.EndBranch = data[i]
206
207 return nil
208}