blob: ad01f2fa5ae1624e1e6f74b3bf330d2172dd66f1 [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"
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
30var (
31 nniLogger = log.WithFields(log.Fields{"module": "NNI"})
32 nniVeth = "nni"
33 upstreamVeth = "upstream"
Matteo Scandolo40e067f2019-10-16 16:59:41 -070034 dhcpServerIp = "192.168.254.1"
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070035)
36
Matteo Scandolo27428702019-10-11 16:21:16 -070037type Executor interface {
38 Command(name string, arg ...string) Runnable
39}
40
41type DefaultExecutor struct{}
42
43func (d DefaultExecutor) Command(name string, arg ...string) Runnable {
44 return exec.Command(name, arg...)
45}
46
47type Runnable interface {
48 Run() error
49}
50
51var executor = DefaultExecutor{}
52
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070053type NniPort struct {
54 // BBSIM Internals
55 ID uint32
56
57 // PON Attributes
58 OperState *fsm.FSM
59 Type string
60}
61
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070062func 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 Scandolo27428702019-10-11 16:21:16 -070070 createNNIPair(executor, olt)
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070071 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
76func sendNniPacket(packet gopacket.Packet) error {
Matteo Scandolo73c488d2019-11-01 14:44:22 -070077 isDhcp := packetHandlers.IsDhcpPacket(packet)
78 isLldp := packetHandlers.IsLldpPacket(packet)
79
80 if isDhcp == false && isLldp == false {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070081 nniLogger.WithFields(log.Fields{
82 "packet": packet,
83 }).Trace("Dropping NNI packet as it's not DHCP")
Matteo Scandolo73c488d2019-11-01 14:44:22 -070084 return nil
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070085 }
86
Matteo Scandolo73c488d2019-11-01 14:44:22 -070087 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 Scandolo4b3fc7e2019-09-17 16:49:54 -070095
Matteo Scandolo73c488d2019-11-01 14:44:22 -070096 handle, err := getVethHandler(nniVeth)
97 if err != nil {
98 return err
99 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700100
Matteo Scandolo73c488d2019-11-01 14:44:22 -0700101 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 Scandolo4b3fc7e2019-09-17 16:49:54 -0700108
Matteo Scandolo73c488d2019-11-01 14:44:22 -0700109 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 Scandolo4b3fc7e2019-09-17 16:49:54 -0700114 return nil
115}
116
Matteo Scandolo27428702019-10-11 16:21:16 -0700117//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
120func createNNIPair(executor Executor, olt *OltDevice) error {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700121
Matteo Scandolo27428702019-10-11 16:21:16 -0700122 if err := executor.Command("ip", "link", "add", nniVeth, "type", "veth", "peer", "name", upstreamVeth).Run(); err != nil {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700123 nniLogger.Errorf("Couldn't create veth pair between %s and %s", nniVeth, upstreamVeth)
124 return err
125 }
126
Matteo Scandolo27428702019-10-11 16:21:16 -0700127 if err := setVethUp(executor, nniVeth); err != nil {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700128 return err
129 }
130
Matteo Scandolo27428702019-10-11 16:21:16 -0700131 if err := setVethUp(executor, upstreamVeth); err != nil {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700132 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 Scandolo27428702019-10-11 16:21:16 -0700148func setVethUp(executor Executor, vethName string) error {
149 if err := executor.Command("ip", "link", "set", vethName, "up").Run(); err != nil {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700150 nniLogger.Errorf("Couldn't change interface %s state to up: %v", vethName, err)
151 return err
152 }
153 return nil
154}
155
Matteo Scandolo27428702019-10-11 16:21:16 -0700156var startDHCPServer = func() error {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700157 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 Scandolo27428702019-10-11 16:21:16 -0700162 if err := setVethUp(executor, upstreamVeth); err != nil {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700163 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 Scandolofe9ac252019-10-25 11:40:17 -0700170 cmd := exec.Command(dhcp, "-cf", conf, upstreamVeth, "-tf", logfile, "-4")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700171 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
181func 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 Scandolo27428702019-10-11 16:21:16 -0700196var listenOnVeth = func(vethName string) (chan *types.PacketMsg, error) {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700197
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}