blob: fabe69c2edca41e06197dc47a46e26059469c393 [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 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +010074 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
84 if isDhcp == false && isLldp == false {
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 +0100148func deleteNNIPair(executor Executor, nniPort *NniPort) error {
149 if err := executor.Command("ip", "link", "del", nniPort.nniVeth).Run(); err != nil {
150 nniLogger.Errorf("Couldn't delete veth pair between %s and %s", nniPort.nniVeth, nniPort.upstreamVeth)
151 return err
152 }
153 return nil
154}
155
156// NewVethChan returns a new channel for receiving packets over the NNI interface
Matteo Scandolo401503a2019-12-11 14:48:14 -0800157func (n *NniPort) NewVethChan() (chan *types.PacketMsg, *pcap.Handle, error) {
158 ch, handle, err := listenOnVeth(n.nniVeth)
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100159 if err != nil {
Matteo Scandolo401503a2019-12-11 14:48:14 -0800160 return nil, nil, err
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100161 }
Matteo Scandolo401503a2019-12-11 14:48:14 -0800162 return ch, handle, err
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100163}
164
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700165// setVethUp is responsible to activate a virtual interface
Matteo Scandolo27428702019-10-11 16:21:16 -0700166func setVethUp(executor Executor, vethName string) error {
167 if err := executor.Command("ip", "link", "set", vethName, "up").Run(); err != nil {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700168 nniLogger.Errorf("Couldn't change interface %s state to up: %v", vethName, err)
169 return err
170 }
171 return nil
172}
173
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100174var startDHCPServer = func(upstreamVeth string, dhcpServerIp string) error {
175 // TODO the DHCP server should support multiple interfaces
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700176 if err := exec.Command("ip", "addr", "add", dhcpServerIp, "dev", upstreamVeth).Run(); err != nil {
177 nniLogger.Errorf("Couldn't assing ip %s to interface %s: %v", dhcpServerIp, upstreamVeth, err)
178 return err
179 }
180
Matteo Scandolo27428702019-10-11 16:21:16 -0700181 if err := setVethUp(executor, upstreamVeth); err != nil {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700182 return err
183 }
184
185 dhcp := "/usr/local/bin/dhcpd"
186 conf := "/etc/dhcp/dhcpd.conf" // copied in the container from configs/dhcpd.conf
187 logfile := "/tmp/dhcplog"
188 var stderr bytes.Buffer
Matteo Scandolofe9ac252019-10-25 11:40:17 -0700189 cmd := exec.Command(dhcp, "-cf", conf, upstreamVeth, "-tf", logfile, "-4")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700190 cmd.Stderr = &stderr
191 err := cmd.Run()
192 if err != nil {
193 nniLogger.Errorf("Fail to start DHCP Server: %s, %s", err, stderr.String())
194 return err
195 }
196 nniLogger.Info("Successfully activated DHCP Server")
197 return nil
198}
199
200func getVethHandler(vethName string) (*pcap.Handle, error) {
201 var (
202 device = vethName
203 snapshotLen int32 = 1518
204 promiscuous = false
205 timeout = pcap.BlockForever
206 )
207 handle, err := pcap.OpenLive(device, snapshotLen, promiscuous, timeout)
208 if err != nil {
209 nniLogger.Errorf("Can't retrieve handler for interface %s", vethName)
210 return nil, err
211 }
212 return handle, nil
213}
214
Matteo Scandolo401503a2019-12-11 14:48:14 -0800215var listenOnVeth = func(vethName string) (chan *types.PacketMsg, *pcap.Handle, error) {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700216
217 handle, err := getVethHandler(vethName)
218 if err != nil {
Matteo Scandolo401503a2019-12-11 14:48:14 -0800219 return nil, nil, err
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700220 }
221
Matteo Scandoloe33447a2019-10-31 12:38:23 -0700222 channel := make(chan *types.PacketMsg, 1024)
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700223
224 go func() {
Matteo Scandolo401503a2019-12-11 14:48:14 -0800225 nniLogger.Info("Start listening on NNI for packets")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700226 packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
227 for packet := range packetSource.Packets() {
228
229 if !packetHandlers.IsIncomingPacket(packet) {
230 nniLogger.Tracef("Ignoring packet as it's going out")
231 continue
232 }
233
234 nniLogger.WithFields(log.Fields{
235 "packet": packet.Dump(),
236 }).Tracef("Received packet on NNI Port")
237 pkt := types.PacketMsg{
238 Pkt: packet,
239 }
240 channel <- &pkt
241 }
Matteo Scandolo401503a2019-12-11 14:48:14 -0800242 nniLogger.Info("Stop listening on NNI for packets")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700243 }()
244
Matteo Scandolo401503a2019-12-11 14:48:14 -0800245 return channel, handle, nil
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700246}