blob: 41aec4bdd50e8081c54ea4e7e94301c0cc9eaddd [file] [log] [blame]
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +09001/*
2 * Copyright 2018-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 core
18
19import (
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090020 "errors"
21 "gerrit.opencord.org/voltha-bbsim/common"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090022 "github.com/google/gopacket"
23 "github.com/google/gopacket/layers"
24 "github.com/google/gopacket/pcap"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090025 "net"
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090026 "strconv"
27 "time"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090028)
29
30func RecvWorker(io *Ioinfo, handler *pcap.Handle, r chan Packet) {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +090031 logger.Debug("recvWorker runs. handler: %v", *handler)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090032 packetSource := gopacket.NewPacketSource(handler, handler.LinkType())
33 for packet := range packetSource.Packets() {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +090034 logger.Debug("recv packet from IF: %v \n", *handler)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090035 //log.Println(packet.Dump())
36 pkt := Packet{}
37 pkt.Info = io
38 pkt.Pkt = packet
39 r <- pkt
40 }
41}
42
43func SendUni(handle *pcap.Handle, packet gopacket.Packet) {
Keita NISHIMOTOb8417492018-10-19 17:37:38 +090044 err := handle.WritePacketData(packet.Data())
45 if err != nil {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +090046 logger.Error("Error in send packet to UNI-IF: %v e:%s\n", *handle, err)
Keita NISHIMOTOb8417492018-10-19 17:37:38 +090047 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +090048 logger.Debug("Successfully send packet to UNI-IF: %v \n", *handle)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090049 //log.Println(packet.Dump())
50}
51
52func SendNni(handle *pcap.Handle, packet gopacket.Packet) {
Keita NISHIMOTOb8417492018-10-19 17:37:38 +090053 err := handle.WritePacketData(packet.Data())
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090054 if err != nil {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +090055 logger.Error("Error in send packet to NNI e:%s\n", err)
Keita NISHIMOTOb8417492018-10-19 17:37:38 +090056 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +090057 logger.Debug("send packet to NNI-IF: %v \n", *handle)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090058 //log.Println(packet.Dump())
59}
60
61func PopVLAN(pkt gopacket.Packet) (gopacket.Packet, uint16, error) {
62 if layer := getDot1QLayer(pkt); layer != nil {
63 if eth := getEthernetLayer(pkt); eth != nil {
64 ethernetLayer := &layers.Ethernet{
65 SrcMAC: eth.SrcMAC,
66 DstMAC: eth.DstMAC,
67 EthernetType: layer.Type,
68 }
69 buffer := gopacket.NewSerializeBuffer()
70 gopacket.SerializeLayers(buffer, gopacket.SerializeOptions{},
71 ethernetLayer,
72 gopacket.Payload(layer.Payload),
73 )
74 retpkt := gopacket.NewPacket(
75 buffer.Bytes(),
76 layers.LayerTypeEthernet,
77 gopacket.Default,
78 )
79 vid := uint16(4095 & layer.VLANIdentifier)
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +090080 logger.Debug("Pop the 802.1Q header (VID: %d)", vid)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090081 return retpkt, vid, nil
82 }
83 }
Keita NISHIMOTOc864da22018-10-15 22:41:42 +090084 return pkt, 0, nil
85 //return nil, 0, errors.New("failed to pop vlan")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090086}
87
88func PushVLAN(pkt gopacket.Packet, vid uint16) (gopacket.Packet, error) {
89 if eth := getEthernetLayer(pkt); eth != nil {
90 ethernetLayer := &layers.Ethernet{
91 SrcMAC: eth.SrcMAC,
92 DstMAC: eth.DstMAC,
93 EthernetType: 0x8100,
94 }
95 dot1qLayer := &layers.Dot1Q{
96 Type: eth.EthernetType,
97 VLANIdentifier: uint16(vid),
98 }
99
100 buffer := gopacket.NewSerializeBuffer()
101 gopacket.SerializeLayers(
102 buffer,
103 gopacket.SerializeOptions{
104 FixLengths: false,
105 },
106 ethernetLayer,
107 dot1qLayer,
108 gopacket.Payload(eth.Payload),
109 )
110 ret := gopacket.NewPacket(
111 buffer.Bytes(),
112 layers.LayerTypeEthernet,
113 gopacket.Default,
114 )
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900115 logger.Debug("Push the 802.1Q header (VID: %d)", vid)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900116 return ret, nil
117 }
118 return nil, errors.New("failed to push vlan")
119}
120
121func getEthernetLayer(pkt gopacket.Packet) *layers.Ethernet {
122 eth := &layers.Ethernet{}
123 if ethLayer := pkt.Layer(layers.LayerTypeEthernet); ethLayer != nil {
124 eth, _ = ethLayer.(*layers.Ethernet)
125 }
126 return eth
127}
128func getDot1QLayer(pkt gopacket.Packet) (dot1q *layers.Dot1Q) {
129 if dot1qLayer := pkt.Layer(layers.LayerTypeDot1Q); dot1qLayer != nil {
130 dot1q = dot1qLayer.(*layers.Dot1Q)
131 }
132 return dot1q
133}
134
135func getMacAddress(ifName string) net.HardwareAddr {
136 var err error
137 var netIf *net.Interface
138 var hwAddr net.HardwareAddr
139 if netIf, err = net.InterfaceByName(ifName); err == nil {
140 hwAddr = netIf.HardwareAddr
141 }
142 return hwAddr
143}
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900144
145func makeUniName(oltid uint32, intfid uint32, onuid uint32) (upif string, dwif string) {
146 upif = UNI_VETH_UP_PFX + strconv.Itoa(int(oltid)) + "_" + strconv.Itoa(int(intfid)) + "_" + strconv.Itoa(int(onuid))
147 dwif = UNI_VETH_DW_PFX + strconv.Itoa(int(oltid)) + "_" + strconv.Itoa(int(intfid)) + "_" + strconv.Itoa(int(onuid))
148 return
149}
150
151func makeNniName(oltid uint32) (upif string, dwif string) {
152 upif = NNI_VETH_UP_PFX + strconv.Itoa(int(oltid))
153 dwif = NNI_VETH_DW_PFX + strconv.Itoa(int(oltid))
154 return
155}
156
157func setupVethHandler(inveth string, outveth string, vethnames []string) (*pcap.Handle, []string, error) {
158 logger.Debug("SetupVethHandler(%s, %s) called ", inveth, outveth)
159 err1 := CreateVethPairs(inveth, outveth)
160 vethnames = append(vethnames, inveth)
161 if err1 != nil {
162 RemoveVeths(vethnames)
163 return nil, vethnames, err1
164 }
165 handler, err2 := getVethHandler(inveth)
166 if err2 != nil {
167 RemoveVeths(vethnames)
168 return nil, vethnames, err2
169 }
170 return handler, vethnames, nil
171}
172
173func getVethHandler(vethname string) (*pcap.Handle, error) {
174 var (
175 device string = vethname
176 snapshot_len int32 = 1518
177 promiscuous bool = false
178 err error
179 timeout time.Duration = pcap.BlockForever
180 )
181 handle, err := pcap.OpenLive(device, snapshot_len, promiscuous, timeout)
182 if err != nil {
183 return nil, err
184 }
185 logger.Debug("Server handle is created for %s\n", vethname)
186 return handle, nil
187}