blob: e74e97b762e4e8eedf8cb0a11cdf6d01e8dcd76e [file] [log] [blame]
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001/*
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 devices
18
19import (
20 "bytes"
Zdravko Bozakov681364d2019-11-10 14:28:46 +010021 "os/exec"
22
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070023 "github.com/google/gopacket"
24 "github.com/google/gopacket/pcap"
25 "github.com/looplab/fsm"
26 "github.com/opencord/bbsim/internal/bbsim/packetHandlers"
27 "github.com/opencord/bbsim/internal/bbsim/types"
28 log "github.com/sirupsen/logrus"
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070029)
30
31var (
32 nniLogger = log.WithFields(log.Fields{"module": "NNI"})
Matteo Scandolo40e067f2019-10-16 16:59:41 -070033 dhcpServerIp = "192.168.254.1"
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070034)
35
Matteo Scandolo27428702019-10-11 16:21:16 -070036type Executor interface {
37 Command(name string, arg ...string) Runnable
38}
39
40type DefaultExecutor struct{}
41
42func (d DefaultExecutor) Command(name string, arg ...string) Runnable {
43 return exec.Command(name, arg...)
44}
45
46type Runnable interface {
47 Run() error
48}
49
50var executor = DefaultExecutor{}
51
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070052type NniPort struct {
53 // BBSIM Internals
Zdravko Bozakov681364d2019-11-10 14:28:46 +010054 ID uint32
55 nniVeth string
56 upstreamVeth string
Pragya Arya996a0892020-03-09 21:47:52 +053057 PacketCount uint64
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070058
59 // PON Attributes
60 OperState *fsm.FSM
61 Type string
62}
63
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070064func CreateNNI(olt *OltDevice) (NniPort, error) {
65 nniPort := NniPort{
Zdravko Bozakov681364d2019-11-10 14:28:46 +010066 ID: uint32(0),
67 nniVeth: "nni",
68 upstreamVeth: "upstream",
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070069 OperState: getOperStateFSM(func(e *fsm.Event) {
70 oltLogger.Debugf("Changing NNI OperState from %s to %s", e.Src, e.Dst)
71 }),
72 Type: "nni",
73 }
Shrey Baid688b4242020-07-10 20:40:10 +053074 _ = createNNIPair(executor, olt, &nniPort)
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070075 return nniPort, nil
76}
77
78// sendNniPacket will send a packet out of the NNI interface.
79// We will send upstream only DHCP packets and drop anything else
Zdravko Bozakov681364d2019-11-10 14:28:46 +010080func (n *NniPort) sendNniPacket(packet gopacket.Packet) error {
Matteo Scandolo73c488d2019-11-01 14:44:22 -070081 isDhcp := packetHandlers.IsDhcpPacket(packet)
82 isLldp := packetHandlers.IsLldpPacket(packet)
83
Shrey Baid688b4242020-07-10 20:40:10 +053084 if !isDhcp && !isLldp {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070085 nniLogger.WithFields(log.Fields{
86 "packet": packet,
87 }).Trace("Dropping NNI packet as it's not DHCP")
Matteo Scandolo73c488d2019-11-01 14:44:22 -070088 return nil
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070089 }
90
Matteo Scandolo73c488d2019-11-01 14:44:22 -070091 if isDhcp {
92 packet, err := packetHandlers.PopDoubleTag(packet)
93 if err != nil {
94 nniLogger.WithFields(log.Fields{
95 "packet": packet,
96 }).Errorf("Can't remove double tags from packet: %v", err)
97 return err
98 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070099
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100100 handle, err := getVethHandler(n.nniVeth)
Matteo Scandolo73c488d2019-11-01 14:44:22 -0700101 if err != nil {
102 return err
103 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700104
Matteo Scandolo73c488d2019-11-01 14:44:22 -0700105 err = handle.WritePacketData(packet.Data())
106 if err != nil {
107 nniLogger.WithFields(log.Fields{
108 "packet": packet,
109 }).Errorf("Failed to send packet out of the NNI: %s", err)
110 return err
111 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700112
Matteo Scandolo73c488d2019-11-01 14:44:22 -0700113 nniLogger.Infof("Sent packet out of NNI")
114 } else if isLldp {
115 // TODO rework this when BBSim supports data-plane packets
116 nniLogger.Trace("Received LLDP Packet, ignoring it")
117 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700118 return nil
119}
120
Matteo Scandolo27428702019-10-11 16:21:16 -0700121//createNNIBridge will create a veth bridge to fake the connection between the NNI port
122//and something upstream, in this case a DHCP server.
123//It is also responsible to start the DHCP server itself
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100124func createNNIPair(executor Executor, olt *OltDevice, nniPort *NniPort) error {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700125
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100126 if err := executor.Command("ip", "link", "add", nniPort.nniVeth, "type", "veth", "peer", "name", nniPort.upstreamVeth).Run(); err != nil {
127 nniLogger.Errorf("Couldn't create veth pair between %s and %s", nniPort.nniVeth, nniPort.upstreamVeth)
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700128 return err
129 }
130
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100131 if err := setVethUp(executor, nniPort.nniVeth); err != nil {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700132 return err
133 }
134
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100135 if err := setVethUp(executor, nniPort.upstreamVeth); err != nil {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700136 return err
137 }
138
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100139 // TODO should be moved out of this function in case there are multiple NNI interfaces.
140 // Only one DHCP server should be running and listening on all NNI interfaces
141 if err := startDHCPServer(nniPort.upstreamVeth, dhcpServerIp); err != nil {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700142 return err
143 }
144
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700145 return nil
146}
147
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100148// NewVethChan returns a new channel for receiving packets over the NNI interface
Matteo Scandolo401503a2019-12-11 14:48:14 -0800149func (n *NniPort) NewVethChan() (chan *types.PacketMsg, *pcap.Handle, error) {
150 ch, handle, err := listenOnVeth(n.nniVeth)
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100151 if err != nil {
Matteo Scandolo401503a2019-12-11 14:48:14 -0800152 return nil, nil, err
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100153 }
Matteo Scandolo401503a2019-12-11 14:48:14 -0800154 return ch, handle, err
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100155}
156
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700157// setVethUp is responsible to activate a virtual interface
Matteo Scandolo27428702019-10-11 16:21:16 -0700158func setVethUp(executor Executor, vethName string) error {
159 if err := executor.Command("ip", "link", "set", vethName, "up").Run(); err != nil {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700160 nniLogger.Errorf("Couldn't change interface %s state to up: %v", vethName, err)
161 return err
162 }
163 return nil
164}
165
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100166var startDHCPServer = func(upstreamVeth string, dhcpServerIp string) error {
167 // TODO the DHCP server should support multiple interfaces
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700168 if err := exec.Command("ip", "addr", "add", dhcpServerIp, "dev", upstreamVeth).Run(); err != nil {
169 nniLogger.Errorf("Couldn't assing ip %s to interface %s: %v", dhcpServerIp, upstreamVeth, err)
170 return err
171 }
172
Matteo Scandolo27428702019-10-11 16:21:16 -0700173 if err := setVethUp(executor, upstreamVeth); err != nil {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700174 return err
175 }
176
177 dhcp := "/usr/local/bin/dhcpd"
178 conf := "/etc/dhcp/dhcpd.conf" // copied in the container from configs/dhcpd.conf
179 logfile := "/tmp/dhcplog"
180 var stderr bytes.Buffer
Matteo Scandolofe9ac252019-10-25 11:40:17 -0700181 cmd := exec.Command(dhcp, "-cf", conf, upstreamVeth, "-tf", logfile, "-4")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700182 cmd.Stderr = &stderr
183 err := cmd.Run()
184 if err != nil {
185 nniLogger.Errorf("Fail to start DHCP Server: %s, %s", err, stderr.String())
186 return err
187 }
188 nniLogger.Info("Successfully activated DHCP Server")
189 return nil
190}
191
192func getVethHandler(vethName string) (*pcap.Handle, error) {
193 var (
194 device = vethName
195 snapshotLen int32 = 1518
196 promiscuous = false
197 timeout = pcap.BlockForever
198 )
199 handle, err := pcap.OpenLive(device, snapshotLen, promiscuous, timeout)
200 if err != nil {
201 nniLogger.Errorf("Can't retrieve handler for interface %s", vethName)
202 return nil, err
203 }
204 return handle, nil
205}
206
Matteo Scandolo401503a2019-12-11 14:48:14 -0800207var listenOnVeth = func(vethName string) (chan *types.PacketMsg, *pcap.Handle, error) {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700208
209 handle, err := getVethHandler(vethName)
210 if err != nil {
Matteo Scandolo401503a2019-12-11 14:48:14 -0800211 return nil, nil, err
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700212 }
213
Matteo Scandoloe33447a2019-10-31 12:38:23 -0700214 channel := make(chan *types.PacketMsg, 1024)
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700215
216 go func() {
Matteo Scandolo401503a2019-12-11 14:48:14 -0800217 nniLogger.Info("Start listening on NNI for packets")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700218 packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
219 for packet := range packetSource.Packets() {
220
221 if !packetHandlers.IsIncomingPacket(packet) {
222 nniLogger.Tracef("Ignoring packet as it's going out")
223 continue
224 }
225
226 nniLogger.WithFields(log.Fields{
227 "packet": packet.Dump(),
228 }).Tracef("Received packet on NNI Port")
229 pkt := types.PacketMsg{
230 Pkt: packet,
231 }
232 channel <- &pkt
233 }
Matteo Scandolo401503a2019-12-11 14:48:14 -0800234 nniLogger.Info("Stop listening on NNI for packets")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700235 }()
236
Matteo Scandolo401503a2019-12-11 14:48:14 -0800237 return channel, handle, nil
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700238}