Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package core |
| 18 | |
| 19 | import ( |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 20 | "errors" |
Keita NISHIMOTO | 9617c85 | 2019-06-17 21:46:44 +0900 | [diff] [blame] | 21 | "net" |
| 22 | "strconv" |
| 23 | |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 24 | "github.com/google/gopacket" |
| 25 | "github.com/google/gopacket/layers" |
| 26 | "github.com/google/gopacket/pcap" |
Zack Williams | 2abf393 | 2019-08-05 14:07:05 -0700 | [diff] [blame] | 27 | "github.com/opencord/voltha-bbsim/common/logger" |
| 28 | "github.com/opencord/voltha-bbsim/device" |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 29 | ) |
| 30 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 31 | // RecvWorker receives the packet and forwards to the channel |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 32 | func RecvWorker(io *Ioinfo, handler *pcap.Handle, r chan Packet) { |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 33 | logger.Debug("recvWorker runs. handler: %v", *handler) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 34 | packetSource := gopacket.NewPacketSource(handler, handler.LinkType()) |
| 35 | for packet := range packetSource.Packets() { |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 36 | logger.Debug("recv packet from IF: %v", *handler) |
| 37 | logger.Debug("Packet received %v", packet) |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 38 | // logger.Println(packet.Dump()) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 39 | pkt := Packet{} |
| 40 | pkt.Info = io |
| 41 | pkt.Pkt = packet |
| 42 | r <- pkt |
| 43 | } |
| 44 | } |
| 45 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 46 | // SendUni sends packet to UNI interface |
Matteo Scandolo | a286c74 | 2018-11-20 08:10:04 -0800 | [diff] [blame] | 47 | func SendUni(handle *pcap.Handle, packet gopacket.Packet, onu *device.Onu) { |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 48 | err := handle.WritePacketData(packet.Data()) |
| 49 | if err != nil { |
Keita NISHIMOTO | 9617c85 | 2019-06-17 21:46:44 +0900 | [diff] [blame] | 50 | device.LoggerWithOnu(onu).Errorf("Error in send packet to UNI-IF: %v e:%v", *handle, err) |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 51 | } |
Keita NISHIMOTO | 9617c85 | 2019-06-17 21:46:44 +0900 | [diff] [blame] | 52 | device.LoggerWithOnu(onu).Debugf("Successfully send packet to UNI-IF: %v", *handle) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 53 | } |
| 54 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 55 | // SendNni sends packaet to NNI interface |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 56 | func SendNni(handle *pcap.Handle, packet gopacket.Packet) { |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 57 | err := handle.WritePacketData(packet.Data()) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 58 | if err != nil { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 59 | logger.Error("Error in send packet to NNI e:%s", err) |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 60 | } |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 61 | logger.Debug("send packet to NNI-IF: %v ", *handle) |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 62 | // logger.Println(packet.Dump()) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 63 | } |
| 64 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 65 | // PopVLAN pops the vlan ans return packet |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 66 | func PopVLAN(pkt gopacket.Packet) (gopacket.Packet, uint16, error) { |
| 67 | if layer := getDot1QLayer(pkt); layer != nil { |
| 68 | if eth := getEthernetLayer(pkt); eth != nil { |
| 69 | ethernetLayer := &layers.Ethernet{ |
| 70 | SrcMAC: eth.SrcMAC, |
| 71 | DstMAC: eth.DstMAC, |
| 72 | EthernetType: layer.Type, |
| 73 | } |
| 74 | buffer := gopacket.NewSerializeBuffer() |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 75 | err := gopacket.SerializeLayers(buffer, gopacket.SerializeOptions{}, |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 76 | ethernetLayer, |
| 77 | gopacket.Payload(layer.Payload), |
| 78 | ) |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 79 | if err != nil { |
| 80 | logger.Error("%v", err) |
| 81 | } |
| 82 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 83 | retpkt := gopacket.NewPacket( |
| 84 | buffer.Bytes(), |
| 85 | layers.LayerTypeEthernet, |
| 86 | gopacket.Default, |
| 87 | ) |
| 88 | vid := uint16(4095 & layer.VLANIdentifier) |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 89 | logger.Debug("Pop the 802.1Q header (VID: %d)", vid) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 90 | return retpkt, vid, nil |
| 91 | } |
| 92 | } |
Scott Baker | 2b4ffb7 | 2019-08-02 16:20:47 -0700 | [diff] [blame] | 93 | return nil, 0, errors.New("failed to pop vlan") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 94 | } |
| 95 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 96 | // PushVLAN pushes the vlan header to the packet and returns tha packet |
Matteo Scandolo | a286c74 | 2018-11-20 08:10:04 -0800 | [diff] [blame] | 97 | func PushVLAN(pkt gopacket.Packet, vid uint16, onu *device.Onu) (gopacket.Packet, error) { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 98 | if eth := getEthernetLayer(pkt); eth != nil { |
| 99 | ethernetLayer := &layers.Ethernet{ |
| 100 | SrcMAC: eth.SrcMAC, |
| 101 | DstMAC: eth.DstMAC, |
| 102 | EthernetType: 0x8100, |
| 103 | } |
| 104 | dot1qLayer := &layers.Dot1Q{ |
| 105 | Type: eth.EthernetType, |
| 106 | VLANIdentifier: uint16(vid), |
| 107 | } |
| 108 | |
| 109 | buffer := gopacket.NewSerializeBuffer() |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 110 | err := gopacket.SerializeLayers( |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 111 | buffer, |
| 112 | gopacket.SerializeOptions{ |
| 113 | FixLengths: false, |
| 114 | }, |
| 115 | ethernetLayer, |
| 116 | dot1qLayer, |
| 117 | gopacket.Payload(eth.Payload), |
| 118 | ) |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 119 | if err != nil { |
| 120 | logger.Error("%v", err) |
| 121 | } |
| 122 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 123 | ret := gopacket.NewPacket( |
| 124 | buffer.Bytes(), |
| 125 | layers.LayerTypeEthernet, |
| 126 | gopacket.Default, |
| 127 | ) |
Keita NISHIMOTO | 9617c85 | 2019-06-17 21:46:44 +0900 | [diff] [blame] | 128 | device.LoggerWithOnu(onu).Debugf("Push the 802.1Q header (VID: %d)", vid) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 129 | return ret, nil |
| 130 | } |
| 131 | return nil, errors.New("failed to push vlan") |
| 132 | } |
| 133 | |
| 134 | func getEthernetLayer(pkt gopacket.Packet) *layers.Ethernet { |
| 135 | eth := &layers.Ethernet{} |
| 136 | if ethLayer := pkt.Layer(layers.LayerTypeEthernet); ethLayer != nil { |
| 137 | eth, _ = ethLayer.(*layers.Ethernet) |
| 138 | } |
| 139 | return eth |
| 140 | } |
| 141 | func getDot1QLayer(pkt gopacket.Packet) (dot1q *layers.Dot1Q) { |
| 142 | if dot1qLayer := pkt.Layer(layers.LayerTypeDot1Q); dot1qLayer != nil { |
| 143 | dot1q = dot1qLayer.(*layers.Dot1Q) |
| 144 | } |
| 145 | return dot1q |
| 146 | } |
| 147 | |
| 148 | func getMacAddress(ifName string) net.HardwareAddr { |
| 149 | var err error |
| 150 | var netIf *net.Interface |
| 151 | var hwAddr net.HardwareAddr |
| 152 | if netIf, err = net.InterfaceByName(ifName); err == nil { |
| 153 | hwAddr = netIf.HardwareAddr |
| 154 | } |
| 155 | return hwAddr |
| 156 | } |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 157 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 158 | func makeNniName(oltid uint32) (upif string, dwif string) { |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 159 | upif = NniVethNorthPfx + strconv.Itoa(int(oltid)) |
| 160 | dwif = NniVethSouthPfx + strconv.Itoa(int(oltid)) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 161 | return |
| 162 | } |
| 163 | |
| 164 | func setupVethHandler(inveth string, outveth string, vethnames []string) (*pcap.Handle, []string, error) { |
| 165 | logger.Debug("SetupVethHandler(%s, %s) called ", inveth, outveth) |
| 166 | err1 := CreateVethPairs(inveth, outveth) |
| 167 | vethnames = append(vethnames, inveth) |
| 168 | if err1 != nil { |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 169 | logger.Error("setupVethHandler failed: %v", err1) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 170 | RemoveVeths(vethnames) |
| 171 | return nil, vethnames, err1 |
| 172 | } |
| 173 | handler, err2 := getVethHandler(inveth) |
| 174 | if err2 != nil { |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 175 | logger.Error("getVethHandler failed: %v", err2) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 176 | RemoveVeths(vethnames) |
| 177 | return nil, vethnames, err2 |
| 178 | } |
| 179 | return handler, vethnames, nil |
| 180 | } |
| 181 | |
| 182 | func getVethHandler(vethname string) (*pcap.Handle, error) { |
| 183 | var ( |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 184 | deviceName = vethname |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 185 | snapshotLen int32 = 1518 |
| 186 | promiscuous = false |
| 187 | err error |
Keita NISHIMOTO | 9617c85 | 2019-06-17 21:46:44 +0900 | [diff] [blame] | 188 | timeout = pcap.BlockForever |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 189 | ) |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 190 | handle, err := pcap.OpenLive(deviceName, snapshotLen, promiscuous, timeout) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 191 | if err != nil { |
| 192 | return nil, err |
| 193 | } |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 194 | logger.Debug("Server handle is created for %s", vethname) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 195 | return handle, nil |
| 196 | } |