blob: 0496b9c5ac1b93f9275d1a385a7ec0ab0916022e [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
25 "gerrit.opencord.org/voltha-bbsim/common/logger"
26 "github.com/google/gopacket"
27 "github.com/google/gopacket/layers"
28 "github.com/google/gopacket/pcap"
Keita NISHIMOTO2b694202018-12-18 07:30:55 +090029 "gerrit.opencord.org/voltha-bbsim/device"
30 "gerrit.opencord.org/voltha-bbsim/common/utils"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090031)
32
33func RecvWorker(io *Ioinfo, handler *pcap.Handle, r chan Packet) {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +090034 logger.Debug("recvWorker runs. handler: %v", *handler)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090035 packetSource := gopacket.NewPacketSource(handler, handler.LinkType())
36 for packet := range packetSource.Packets() {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080037 logger.Debug("recv packet from IF: %v ", *handler)
Matteo Scandolo88e91892018-11-06 16:29:19 -080038 //logger.Println(packet.Dump())
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090039 pkt := Packet{}
40 pkt.Info = io
41 pkt.Pkt = packet
42 r <- pkt
43 }
44}
45
Matteo Scandoloa286c742018-11-20 08:10:04 -080046func SendUni(handle *pcap.Handle, packet gopacket.Packet, onu *device.Onu) {
Keita NISHIMOTOb8417492018-10-19 17:37:38 +090047 err := handle.WritePacketData(packet.Data())
48 if err != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -070049 utils.LoggerWithOnu(onu).Errorf("Error in send packet to UNI-IF: %v e:%v", *handle, err)
Keita NISHIMOTOb8417492018-10-19 17:37:38 +090050 }
Zack Williamsb85f5932019-05-10 16:21:35 -070051 utils.LoggerWithOnu(onu).Debugf("Successfully send packet to UNI-IF: %v", *handle)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090052}
53
54func SendNni(handle *pcap.Handle, packet gopacket.Packet) {
Keita NISHIMOTOb8417492018-10-19 17:37:38 +090055 err := handle.WritePacketData(packet.Data())
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090056 if err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080057 logger.Error("Error in send packet to NNI e:%s", err)
Keita NISHIMOTOb8417492018-10-19 17:37:38 +090058 }
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080059 logger.Debug("send packet to NNI-IF: %v ", *handle)
Matteo Scandolo88e91892018-11-06 16:29:19 -080060 //logger.Println(packet.Dump())
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090061}
62
63func PopVLAN(pkt gopacket.Packet) (gopacket.Packet, uint16, error) {
64 if layer := getDot1QLayer(pkt); layer != nil {
65 if eth := getEthernetLayer(pkt); eth != nil {
66 ethernetLayer := &layers.Ethernet{
67 SrcMAC: eth.SrcMAC,
68 DstMAC: eth.DstMAC,
69 EthernetType: layer.Type,
70 }
71 buffer := gopacket.NewSerializeBuffer()
72 gopacket.SerializeLayers(buffer, gopacket.SerializeOptions{},
73 ethernetLayer,
74 gopacket.Payload(layer.Payload),
75 )
76 retpkt := gopacket.NewPacket(
77 buffer.Bytes(),
78 layers.LayerTypeEthernet,
79 gopacket.Default,
80 )
81 vid := uint16(4095 & layer.VLANIdentifier)
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +090082 logger.Debug("Pop the 802.1Q header (VID: %d)", vid)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090083 return retpkt, vid, nil
84 }
85 }
Keita NISHIMOTOc864da22018-10-15 22:41:42 +090086 return pkt, 0, nil
87 //return nil, 0, errors.New("failed to pop vlan")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090088}
89
Matteo Scandoloa286c742018-11-20 08:10:04 -080090func PushVLAN(pkt gopacket.Packet, vid uint16, onu *device.Onu) (gopacket.Packet, error) {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090091 if eth := getEthernetLayer(pkt); eth != nil {
92 ethernetLayer := &layers.Ethernet{
93 SrcMAC: eth.SrcMAC,
94 DstMAC: eth.DstMAC,
95 EthernetType: 0x8100,
96 }
97 dot1qLayer := &layers.Dot1Q{
98 Type: eth.EthernetType,
99 VLANIdentifier: uint16(vid),
100 }
101
102 buffer := gopacket.NewSerializeBuffer()
103 gopacket.SerializeLayers(
104 buffer,
105 gopacket.SerializeOptions{
106 FixLengths: false,
107 },
108 ethernetLayer,
109 dot1qLayer,
110 gopacket.Payload(eth.Payload),
111 )
112 ret := gopacket.NewPacket(
113 buffer.Bytes(),
114 layers.LayerTypeEthernet,
115 gopacket.Default,
116 )
Matteo Scandoloa286c742018-11-20 08:10:04 -0800117 utils.LoggerWithOnu(onu).Debugf("Push the 802.1Q header (VID: %d)", vid)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900118 return ret, nil
119 }
120 return nil, errors.New("failed to push vlan")
121}
122
123func getEthernetLayer(pkt gopacket.Packet) *layers.Ethernet {
124 eth := &layers.Ethernet{}
125 if ethLayer := pkt.Layer(layers.LayerTypeEthernet); ethLayer != nil {
126 eth, _ = ethLayer.(*layers.Ethernet)
127 }
128 return eth
129}
130func getDot1QLayer(pkt gopacket.Packet) (dot1q *layers.Dot1Q) {
131 if dot1qLayer := pkt.Layer(layers.LayerTypeDot1Q); dot1qLayer != nil {
132 dot1q = dot1qLayer.(*layers.Dot1Q)
133 }
134 return dot1q
135}
136
137func getMacAddress(ifName string) net.HardwareAddr {
138 var err error
139 var netIf *net.Interface
140 var hwAddr net.HardwareAddr
141 if netIf, err = net.InterfaceByName(ifName); err == nil {
142 hwAddr = netIf.HardwareAddr
143 }
144 return hwAddr
145}
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900146
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900147func makeNniName(oltid uint32) (upif string, dwif string) {
Keita NISHIMOTOdd9f6732019-02-09 09:41:31 +0900148 upif = NNI_VETH_NORTH_PFX + strconv.Itoa(int(oltid))
149 dwif = NNI_VETH_SOUTH_PFX + strconv.Itoa(int(oltid))
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900150 return
151}
152
153func setupVethHandler(inveth string, outveth string, vethnames []string) (*pcap.Handle, []string, error) {
154 logger.Debug("SetupVethHandler(%s, %s) called ", inveth, outveth)
155 err1 := CreateVethPairs(inveth, outveth)
156 vethnames = append(vethnames, inveth)
157 if err1 != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700158 logger.Error("setupVethHandler failed: %v", err1)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900159 RemoveVeths(vethnames)
160 return nil, vethnames, err1
161 }
162 handler, err2 := getVethHandler(inveth)
163 if err2 != nil {
Zack Williamsb85f5932019-05-10 16:21:35 -0700164 logger.Error("getVethHandler failed: %v", err2)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900165 RemoveVeths(vethnames)
166 return nil, vethnames, err2
167 }
168 return handler, vethnames, nil
169}
170
171func getVethHandler(vethname string) (*pcap.Handle, error) {
172 var (
173 device string = vethname
174 snapshot_len int32 = 1518
175 promiscuous bool = false
176 err error
177 timeout time.Duration = pcap.BlockForever
178 )
179 handle, err := pcap.OpenLive(device, snapshot_len, promiscuous, timeout)
180 if err != nil {
181 return nil, err
182 }
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800183 logger.Debug("Server handle is created for %s", vethname)
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900184 return handle, nil
185}