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