blob: 2533a19423a34b7fee622e8f54fb62189597083b [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"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090021 "net"
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090022 "strconv"
23 "time"
Matteo Scandolo88e91892018-11-06 16:29:19 -080024
Matteo Scandoloa286c742018-11-20 08:10:04 -080025 "gerrit.opencord.org/voltha-bbsim/device"
26
Matteo Scandolo88e91892018-11-06 16:29:19 -080027 "gerrit.opencord.org/voltha-bbsim/common/logger"
Matteo Scandoloa286c742018-11-20 08:10:04 -080028 "gerrit.opencord.org/voltha-bbsim/common/utils"
Matteo Scandolo88e91892018-11-06 16:29:19 -080029 "github.com/google/gopacket"
30 "github.com/google/gopacket/layers"
31 "github.com/google/gopacket/pcap"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090032)
33
34func RecvWorker(io *Ioinfo, handler *pcap.Handle, r chan Packet) {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +090035 logger.Debug("recvWorker runs. handler: %v", *handler)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090036 packetSource := gopacket.NewPacketSource(handler, handler.LinkType())
37 for packet := range packetSource.Packets() {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080038 logger.Debug("recv packet from IF: %v ", *handler)
Matteo Scandolo88e91892018-11-06 16:29:19 -080039 //logger.Println(packet.Dump())
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090040 pkt := Packet{}
41 pkt.Info = io
42 pkt.Pkt = packet
43 r <- pkt
44 }
45}
46
Matteo Scandoloa286c742018-11-20 08:10:04 -080047func SendUni(handle *pcap.Handle, packet gopacket.Packet, onu *device.Onu) {
Keita NISHIMOTOb8417492018-10-19 17:37:38 +090048 err := handle.WritePacketData(packet.Data())
49 if err != nil {
Matteo Scandoloa286c742018-11-20 08:10:04 -080050 utils.LoggerWithOnu(onu).Error("Error in send packet to UNI-IF: %v e:%s", *handle, err)
Keita NISHIMOTOb8417492018-10-19 17:37:38 +090051 }
Matteo Scandoloa286c742018-11-20 08:10:04 -080052 utils.LoggerWithOnu(onu).Debug("Successfully send packet to UNI-IF: %v ", *handle)
Matteo Scandolo88e91892018-11-06 16:29:19 -080053 //logger.Println(packet.Dump())
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090054}
55
56func SendNni(handle *pcap.Handle, packet gopacket.Packet) {
Keita NISHIMOTOb8417492018-10-19 17:37:38 +090057 err := handle.WritePacketData(packet.Data())
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090058 if err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080059 logger.Error("Error in send packet to NNI e:%s", err)
Keita NISHIMOTOb8417492018-10-19 17:37:38 +090060 }
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080061 logger.Debug("send packet to NNI-IF: %v ", *handle)
Matteo Scandolo88e91892018-11-06 16:29:19 -080062 //logger.Println(packet.Dump())
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090063}
64
65func PopVLAN(pkt gopacket.Packet) (gopacket.Packet, uint16, error) {
66 if layer := getDot1QLayer(pkt); layer != nil {
67 if eth := getEthernetLayer(pkt); eth != nil {
68 ethernetLayer := &layers.Ethernet{
69 SrcMAC: eth.SrcMAC,
70 DstMAC: eth.DstMAC,
71 EthernetType: layer.Type,
72 }
73 buffer := gopacket.NewSerializeBuffer()
74 gopacket.SerializeLayers(buffer, gopacket.SerializeOptions{},
75 ethernetLayer,
76 gopacket.Payload(layer.Payload),
77 )
78 retpkt := gopacket.NewPacket(
79 buffer.Bytes(),
80 layers.LayerTypeEthernet,
81 gopacket.Default,
82 )
83 vid := uint16(4095 & layer.VLANIdentifier)
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +090084 logger.Debug("Pop the 802.1Q header (VID: %d)", vid)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090085 return retpkt, vid, nil
86 }
87 }
Keita NISHIMOTOc864da22018-10-15 22:41:42 +090088 return pkt, 0, nil
89 //return nil, 0, errors.New("failed to pop vlan")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090090}
91
Matteo Scandoloa286c742018-11-20 08:10:04 -080092func PushVLAN(pkt gopacket.Packet, vid uint16, onu *device.Onu) (gopacket.Packet, error) {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090093 if eth := getEthernetLayer(pkt); eth != nil {
94 ethernetLayer := &layers.Ethernet{
95 SrcMAC: eth.SrcMAC,
96 DstMAC: eth.DstMAC,
97 EthernetType: 0x8100,
98 }
99 dot1qLayer := &layers.Dot1Q{
100 Type: eth.EthernetType,
101 VLANIdentifier: uint16(vid),
102 }
103
104 buffer := gopacket.NewSerializeBuffer()
105 gopacket.SerializeLayers(
106 buffer,
107 gopacket.SerializeOptions{
108 FixLengths: false,
109 },
110 ethernetLayer,
111 dot1qLayer,
112 gopacket.Payload(eth.Payload),
113 )
114 ret := gopacket.NewPacket(
115 buffer.Bytes(),
116 layers.LayerTypeEthernet,
117 gopacket.Default,
118 )
Matteo Scandoloa286c742018-11-20 08:10:04 -0800119 utils.LoggerWithOnu(onu).Debugf("Push the 802.1Q header (VID: %d)", vid)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900120 return ret, nil
121 }
122 return nil, errors.New("failed to push vlan")
123}
124
125func getEthernetLayer(pkt gopacket.Packet) *layers.Ethernet {
126 eth := &layers.Ethernet{}
127 if ethLayer := pkt.Layer(layers.LayerTypeEthernet); ethLayer != nil {
128 eth, _ = ethLayer.(*layers.Ethernet)
129 }
130 return eth
131}
132func getDot1QLayer(pkt gopacket.Packet) (dot1q *layers.Dot1Q) {
133 if dot1qLayer := pkt.Layer(layers.LayerTypeDot1Q); dot1qLayer != nil {
134 dot1q = dot1qLayer.(*layers.Dot1Q)
135 }
136 return dot1q
137}
138
139func getMacAddress(ifName string) net.HardwareAddr {
140 var err error
141 var netIf *net.Interface
142 var hwAddr net.HardwareAddr
143 if netIf, err = net.InterfaceByName(ifName); err == nil {
144 hwAddr = netIf.HardwareAddr
145 }
146 return hwAddr
147}
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900148
149func makeUniName(oltid uint32, intfid uint32, onuid uint32) (upif string, dwif string) {
150 upif = UNI_VETH_UP_PFX + strconv.Itoa(int(oltid)) + "_" + strconv.Itoa(int(intfid)) + "_" + strconv.Itoa(int(onuid))
151 dwif = UNI_VETH_DW_PFX + strconv.Itoa(int(oltid)) + "_" + strconv.Itoa(int(intfid)) + "_" + strconv.Itoa(int(onuid))
152 return
153}
154
155func makeNniName(oltid uint32) (upif string, dwif string) {
156 upif = NNI_VETH_UP_PFX + strconv.Itoa(int(oltid))
157 dwif = NNI_VETH_DW_PFX + strconv.Itoa(int(oltid))
158 return
159}
160
161func setupVethHandler(inveth string, outveth string, vethnames []string) (*pcap.Handle, []string, error) {
162 logger.Debug("SetupVethHandler(%s, %s) called ", inveth, outveth)
163 err1 := CreateVethPairs(inveth, outveth)
164 vethnames = append(vethnames, inveth)
165 if err1 != nil {
Shad Ansari6a93ee22018-11-16 13:26:47 -0800166 logger.Error("setupVethHandler failed", err1)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900167 RemoveVeths(vethnames)
168 return nil, vethnames, err1
169 }
170 handler, err2 := getVethHandler(inveth)
171 if err2 != nil {
Shad Ansari6a93ee22018-11-16 13:26:47 -0800172 logger.Error("getVethHandler failed", err2)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900173 RemoveVeths(vethnames)
174 return nil, vethnames, err2
175 }
176 return handler, vethnames, nil
177}
178
179func getVethHandler(vethname string) (*pcap.Handle, error) {
180 var (
181 device string = vethname
182 snapshot_len int32 = 1518
183 promiscuous bool = false
184 err error
185 timeout time.Duration = pcap.BlockForever
186 )
187 handle, err := pcap.OpenLive(device, snapshot_len, promiscuous, timeout)
188 if err != nil {
189 return nil, err
190 }
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800191 logger.Debug("Server handle is created for %s", vethname)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900192 return handle, nil
193}