Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [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 devices |
| 18 | |
| 19 | import ( |
| 20 | "bytes" |
| 21 | "github.com/google/gopacket" |
| 22 | "github.com/google/gopacket/pcap" |
| 23 | "github.com/looplab/fsm" |
| 24 | "github.com/opencord/bbsim/internal/bbsim/packetHandlers" |
| 25 | "github.com/opencord/bbsim/internal/bbsim/types" |
| 26 | log "github.com/sirupsen/logrus" |
| 27 | "os/exec" |
| 28 | ) |
| 29 | |
| 30 | var ( |
| 31 | nniLogger = log.WithFields(log.Fields{"module": "NNI"}) |
| 32 | nniVeth = "nni" |
| 33 | upstreamVeth = "upstream" |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 34 | dhcpServerIp = "192.168.254.1" |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 35 | ) |
| 36 | |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 37 | type Executor interface { |
| 38 | Command(name string, arg ...string) Runnable |
| 39 | } |
| 40 | |
| 41 | type DefaultExecutor struct{} |
| 42 | |
| 43 | func (d DefaultExecutor) Command(name string, arg ...string) Runnable { |
| 44 | return exec.Command(name, arg...) |
| 45 | } |
| 46 | |
| 47 | type Runnable interface { |
| 48 | Run() error |
| 49 | } |
| 50 | |
| 51 | var executor = DefaultExecutor{} |
| 52 | |
Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame] | 53 | type NniPort struct { |
| 54 | // BBSIM Internals |
| 55 | ID uint32 |
| 56 | |
| 57 | // PON Attributes |
| 58 | OperState *fsm.FSM |
| 59 | Type string |
| 60 | } |
| 61 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 62 | func CreateNNI(olt *OltDevice) (NniPort, error) { |
| 63 | nniPort := NniPort{ |
| 64 | ID: uint32(0), |
| 65 | OperState: getOperStateFSM(func(e *fsm.Event) { |
| 66 | oltLogger.Debugf("Changing NNI OperState from %s to %s", e.Src, e.Dst) |
| 67 | }), |
| 68 | Type: "nni", |
| 69 | } |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 70 | createNNIPair(executor, olt) |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 71 | return nniPort, nil |
| 72 | } |
| 73 | |
| 74 | // sendNniPacket will send a packet out of the NNI interface. |
| 75 | // We will send upstream only DHCP packets and drop anything else |
| 76 | func sendNniPacket(packet gopacket.Packet) error { |
Matteo Scandolo | 73c488d | 2019-11-01 14:44:22 -0700 | [diff] [blame^] | 77 | isDhcp := packetHandlers.IsDhcpPacket(packet) |
| 78 | isLldp := packetHandlers.IsLldpPacket(packet) |
| 79 | |
| 80 | if isDhcp == false && isLldp == false { |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 81 | nniLogger.WithFields(log.Fields{ |
| 82 | "packet": packet, |
| 83 | }).Trace("Dropping NNI packet as it's not DHCP") |
Matteo Scandolo | 73c488d | 2019-11-01 14:44:22 -0700 | [diff] [blame^] | 84 | return nil |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Matteo Scandolo | 73c488d | 2019-11-01 14:44:22 -0700 | [diff] [blame^] | 87 | if isDhcp { |
| 88 | packet, err := packetHandlers.PopDoubleTag(packet) |
| 89 | if err != nil { |
| 90 | nniLogger.WithFields(log.Fields{ |
| 91 | "packet": packet, |
| 92 | }).Errorf("Can't remove double tags from packet: %v", err) |
| 93 | return err |
| 94 | } |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 95 | |
Matteo Scandolo | 73c488d | 2019-11-01 14:44:22 -0700 | [diff] [blame^] | 96 | handle, err := getVethHandler(nniVeth) |
| 97 | if err != nil { |
| 98 | return err |
| 99 | } |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 100 | |
Matteo Scandolo | 73c488d | 2019-11-01 14:44:22 -0700 | [diff] [blame^] | 101 | err = handle.WritePacketData(packet.Data()) |
| 102 | if err != nil { |
| 103 | nniLogger.WithFields(log.Fields{ |
| 104 | "packet": packet, |
| 105 | }).Errorf("Failed to send packet out of the NNI: %s", err) |
| 106 | return err |
| 107 | } |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 108 | |
Matteo Scandolo | 73c488d | 2019-11-01 14:44:22 -0700 | [diff] [blame^] | 109 | nniLogger.Infof("Sent packet out of NNI") |
| 110 | } else if isLldp { |
| 111 | // TODO rework this when BBSim supports data-plane packets |
| 112 | nniLogger.Trace("Received LLDP Packet, ignoring it") |
| 113 | } |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 114 | return nil |
| 115 | } |
| 116 | |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 117 | //createNNIBridge will create a veth bridge to fake the connection between the NNI port |
| 118 | //and something upstream, in this case a DHCP server. |
| 119 | //It is also responsible to start the DHCP server itself |
| 120 | func createNNIPair(executor Executor, olt *OltDevice) error { |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 121 | |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 122 | if err := executor.Command("ip", "link", "add", nniVeth, "type", "veth", "peer", "name", upstreamVeth).Run(); err != nil { |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 123 | nniLogger.Errorf("Couldn't create veth pair between %s and %s", nniVeth, upstreamVeth) |
| 124 | return err |
| 125 | } |
| 126 | |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 127 | if err := setVethUp(executor, nniVeth); err != nil { |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 128 | return err |
| 129 | } |
| 130 | |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 131 | if err := setVethUp(executor, upstreamVeth); err != nil { |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 132 | return err |
| 133 | } |
| 134 | |
| 135 | if err := startDHCPServer(); err != nil { |
| 136 | return err |
| 137 | } |
| 138 | |
| 139 | ch, err := listenOnVeth(nniVeth) |
| 140 | if err != nil { |
| 141 | return err |
| 142 | } |
| 143 | olt.nniPktInChannel = ch |
| 144 | return nil |
| 145 | } |
| 146 | |
| 147 | // setVethUp is responsible to activate a virtual interface |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 148 | func setVethUp(executor Executor, vethName string) error { |
| 149 | if err := executor.Command("ip", "link", "set", vethName, "up").Run(); err != nil { |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 150 | nniLogger.Errorf("Couldn't change interface %s state to up: %v", vethName, err) |
| 151 | return err |
| 152 | } |
| 153 | return nil |
| 154 | } |
| 155 | |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 156 | var startDHCPServer = func() error { |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 157 | if err := exec.Command("ip", "addr", "add", dhcpServerIp, "dev", upstreamVeth).Run(); err != nil { |
| 158 | nniLogger.Errorf("Couldn't assing ip %s to interface %s: %v", dhcpServerIp, upstreamVeth, err) |
| 159 | return err |
| 160 | } |
| 161 | |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 162 | if err := setVethUp(executor, upstreamVeth); err != nil { |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 163 | return err |
| 164 | } |
| 165 | |
| 166 | dhcp := "/usr/local/bin/dhcpd" |
| 167 | conf := "/etc/dhcp/dhcpd.conf" // copied in the container from configs/dhcpd.conf |
| 168 | logfile := "/tmp/dhcplog" |
| 169 | var stderr bytes.Buffer |
Matteo Scandolo | fe9ac25 | 2019-10-25 11:40:17 -0700 | [diff] [blame] | 170 | cmd := exec.Command(dhcp, "-cf", conf, upstreamVeth, "-tf", logfile, "-4") |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 171 | cmd.Stderr = &stderr |
| 172 | err := cmd.Run() |
| 173 | if err != nil { |
| 174 | nniLogger.Errorf("Fail to start DHCP Server: %s, %s", err, stderr.String()) |
| 175 | return err |
| 176 | } |
| 177 | nniLogger.Info("Successfully activated DHCP Server") |
| 178 | return nil |
| 179 | } |
| 180 | |
| 181 | func getVethHandler(vethName string) (*pcap.Handle, error) { |
| 182 | var ( |
| 183 | device = vethName |
| 184 | snapshotLen int32 = 1518 |
| 185 | promiscuous = false |
| 186 | timeout = pcap.BlockForever |
| 187 | ) |
| 188 | handle, err := pcap.OpenLive(device, snapshotLen, promiscuous, timeout) |
| 189 | if err != nil { |
| 190 | nniLogger.Errorf("Can't retrieve handler for interface %s", vethName) |
| 191 | return nil, err |
| 192 | } |
| 193 | return handle, nil |
| 194 | } |
| 195 | |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 196 | var listenOnVeth = func(vethName string) (chan *types.PacketMsg, error) { |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 197 | |
| 198 | handle, err := getVethHandler(vethName) |
| 199 | if err != nil { |
| 200 | return nil, err |
| 201 | } |
| 202 | |
| 203 | channel := make(chan *types.PacketMsg, 32) |
| 204 | |
| 205 | go func() { |
| 206 | packetSource := gopacket.NewPacketSource(handle, handle.LinkType()) |
| 207 | for packet := range packetSource.Packets() { |
| 208 | |
| 209 | if !packetHandlers.IsIncomingPacket(packet) { |
| 210 | nniLogger.Tracef("Ignoring packet as it's going out") |
| 211 | continue |
| 212 | } |
| 213 | |
| 214 | nniLogger.WithFields(log.Fields{ |
| 215 | "packet": packet.Dump(), |
| 216 | }).Tracef("Received packet on NNI Port") |
| 217 | pkt := types.PacketMsg{ |
| 218 | Pkt: packet, |
| 219 | } |
| 220 | channel <- &pkt |
| 221 | } |
| 222 | }() |
| 223 | |
| 224 | return channel, nil |
| 225 | } |