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" |
| 21 | "gerrit.opencord.org/voltha-bbsim/common" |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 22 | "github.com/google/gopacket" |
| 23 | "github.com/google/gopacket/layers" |
| 24 | "github.com/google/gopacket/pcap" |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 25 | "net" |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame^] | 26 | "strconv" |
| 27 | "time" |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 28 | ) |
| 29 | |
| 30 | func RecvWorker(io *Ioinfo, handler *pcap.Handle, r chan Packet) { |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 31 | logger.Debug("recvWorker runs. handler: %v", *handler) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 32 | packetSource := gopacket.NewPacketSource(handler, handler.LinkType()) |
| 33 | for packet := range packetSource.Packets() { |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 34 | logger.Debug("recv packet from IF: %v \n", *handler) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 35 | //log.Println(packet.Dump()) |
| 36 | pkt := Packet{} |
| 37 | pkt.Info = io |
| 38 | pkt.Pkt = packet |
| 39 | r <- pkt |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | func SendUni(handle *pcap.Handle, packet gopacket.Packet) { |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 44 | err := handle.WritePacketData(packet.Data()) |
| 45 | if err != nil { |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 46 | logger.Error("Error in send packet to UNI-IF: %v e:%s\n", *handle, err) |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 47 | } |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 48 | logger.Debug("Successfully send packet to UNI-IF: %v \n", *handle) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 49 | //log.Println(packet.Dump()) |
| 50 | } |
| 51 | |
| 52 | func SendNni(handle *pcap.Handle, packet gopacket.Packet) { |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 53 | err := handle.WritePacketData(packet.Data()) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame^] | 54 | if err != nil { |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 55 | logger.Error("Error in send packet to NNI e:%s\n", err) |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 56 | } |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 57 | logger.Debug("send packet to NNI-IF: %v \n", *handle) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 58 | //log.Println(packet.Dump()) |
| 59 | } |
| 60 | |
| 61 | func 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 NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 80 | logger.Debug("Pop the 802.1Q header (VID: %d)", vid) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 81 | return retpkt, vid, nil |
| 82 | } |
| 83 | } |
Keita NISHIMOTO | c864da2 | 2018-10-15 22:41:42 +0900 | [diff] [blame] | 84 | return pkt, 0, nil |
| 85 | //return nil, 0, errors.New("failed to pop vlan") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | func 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 NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 115 | logger.Debug("Push the 802.1Q header (VID: %d)", vid) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 116 | return ret, nil |
| 117 | } |
| 118 | return nil, errors.New("failed to push vlan") |
| 119 | } |
| 120 | |
| 121 | func 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 | } |
| 128 | func 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 | |
| 135 | func 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 NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame^] | 144 | |
| 145 | func 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 | |
| 151 | func 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 | |
| 157 | func 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 | |
| 173 | func 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 | } |