blob: dd092da7cbb991c06f97ca057a243bdbda5c78b5 [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 "net"
21 "sync"
22
23 "github.com/google/gopacket"
24 "github.com/google/gopacket/layers"
25)
26
27// OnuPkgType describes target package type
28var OnuPkgType = "PkgB"
29
30// OnuPkgTypeA is a constant of package
31const OnuPkgTypeA = "PkgA"
32
33// OnuPkgTypeB is a constant of package
34const OnuPkgTypeB = "PkgB"
35
36// TibitFrame is a typical structure of a frame
37type TibitFrame struct {
38 layers.BaseLayer
39 Data []byte
40}
41
42// LayerType returns ethernet layer type
43func (t *TibitFrame) LayerType() gopacket.LayerType { return layers.LayerTypeEthernet }
44
45// SerializeTo serializes a data structure to byte arrays
46func (t *TibitFrame) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
47 length := len(t.Data)
48 bytes, err := b.PrependBytes(length)
49 if err != nil {
50 return err
51 }
52 copy(bytes, t.Data)
53 return nil
54}
55
56// TomiObjectContext is a structure for tomi object
57type TomiObjectContext struct {
58 Branch uint8
59 Type uint16
60 Length uint8
61 Instance uint32
62}
63
64// CreateMessage creates l2 message
65func CreateMessage(srcMac string, dstMac string, ethernetType layers.EthernetType, tibitData gopacket.SerializableLayer) []byte {
66 srcMAC, _ := net.ParseMAC(srcMac)
67 dstMAC, _ := net.ParseMAC(dstMac)
68
69 ethernetLayer := &layers.Ethernet{
70 SrcMAC: srcMAC,
71 DstMAC: dstMAC,
72 EthernetType: ethernetType,
73 }
74
75 buf := gopacket.NewSerializeBuffer()
76 if err := gopacket.SerializeLayers(
77 buf,
78 gopacket.SerializeOptions{
79 ComputeChecksums: true,
80 FixLengths: true,
81 },
82 ethernetLayer,
83 tibitData,
84 ); err != nil {
85 return buf.Bytes()
86 }
87 return buf.Bytes()
88}
89
90// CreateMessage creates vlan message
91func CreateMessageVlan(srcMac string, dstMac string, ethernetType layers.EthernetType, tibitData gopacket.SerializableLayer, vlanLayer *layers.Dot1Q) []byte {
92 srcMAC, _ := net.ParseMAC(srcMac)
93 dstMAC, _ := net.ParseMAC(dstMac)
94
95 ethernetLayer := &layers.Ethernet{
96 SrcMAC: srcMAC,
97 DstMAC: dstMAC,
98 EthernetType: ethernetType,
99 }
100
101 buf := gopacket.NewSerializeBuffer()
102 if err := gopacket.SerializeLayers(
103 buf,
104 gopacket.SerializeOptions{
105 ComputeChecksums: true,
106 FixLengths: true,
107 },
108 ethernetLayer,
109 vlanLayer,
110 tibitData,
111 ); err != nil {
112 return buf.Bytes()
113 }
114 return buf.Bytes()
115}
116
117// CorrelationTag instance ID
118// It is incremented automatically for each TOMI message with OLT
119var instance uint32 = 0x5c1f6a60
120var instanceMutex sync.Mutex
121
122func getOltInstance() uint32 {
123 instanceMutex.Lock()
124 defer instanceMutex.Unlock()
125 instance = instance + 1
126
127 return instance
128
129}