blob: f5503cd4156eea3c2c02d208c7375fd21e514a67 [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 {
77 if isDhcp := packetHandlers.IsDhcpPacket(packet); !isDhcp {
78 nniLogger.WithFields(log.Fields{
79 "packet": packet,
80 }).Trace("Dropping NNI packet as it's not DHCP")
81 }
82
83 packet, err := packetHandlers.PopDoubleTag(packet)
84 if err != nil {
85 nniLogger.WithFields(log.Fields{
86 "packet": packet,
87 }).Errorf("Can't remove double tags from packet: %v", err)
88 return err
89 }
90
91 handle, err := getVethHandler(nniVeth)
92 if err != nil {
93 return err
94 }
95
96 err = handle.WritePacketData(packet.Data())
97 if err != nil {
98 nniLogger.WithFields(log.Fields{
99 "packet": packet,
100 }).Errorf("Failed to send packet out of the NNI: %s", err)
101 return err
102 }
103
104 nniLogger.Infof("Sent packet out of NNI")
105 return nil
106}
107
Matteo Scandolo27428702019-10-11 16:21:16 -0700108//createNNIBridge will create a veth bridge to fake the connection between the NNI port
109//and something upstream, in this case a DHCP server.
110//It is also responsible to start the DHCP server itself
111func createNNIPair(executor Executor, olt *OltDevice) error {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700112
Matteo Scandolo27428702019-10-11 16:21:16 -0700113 if err := executor.Command("ip", "link", "add", nniVeth, "type", "veth", "peer", "name", upstreamVeth).Run(); err != nil {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700114 nniLogger.Errorf("Couldn't create veth pair between %s and %s", nniVeth, upstreamVeth)
115 return err
116 }
117
Matteo Scandolo27428702019-10-11 16:21:16 -0700118 if err := setVethUp(executor, nniVeth); err != nil {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700119 return err
120 }
121
Matteo Scandolo27428702019-10-11 16:21:16 -0700122 if err := setVethUp(executor, upstreamVeth); err != nil {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700123 return err
124 }
125
126 if err := startDHCPServer(); err != nil {
127 return err
128 }
129
130 ch, err := listenOnVeth(nniVeth)
131 if err != nil {
132 return err
133 }
134 olt.nniPktInChannel = ch
135 return nil
136}
137
138// setVethUp is responsible to activate a virtual interface
Matteo Scandolo27428702019-10-11 16:21:16 -0700139func setVethUp(executor Executor, vethName string) error {
140 if err := executor.Command("ip", "link", "set", vethName, "up").Run(); err != nil {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700141 nniLogger.Errorf("Couldn't change interface %s state to up: %v", vethName, err)
142 return err
143 }
144 return nil
145}
146
Matteo Scandolo27428702019-10-11 16:21:16 -0700147var startDHCPServer = func() error {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700148 if err := exec.Command("ip", "addr", "add", dhcpServerIp, "dev", upstreamVeth).Run(); err != nil {
149 nniLogger.Errorf("Couldn't assing ip %s to interface %s: %v", dhcpServerIp, upstreamVeth, err)
150 return err
151 }
152
Matteo Scandolo27428702019-10-11 16:21:16 -0700153 if err := setVethUp(executor, upstreamVeth); err != nil {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700154 return err
155 }
156
157 dhcp := "/usr/local/bin/dhcpd"
158 conf := "/etc/dhcp/dhcpd.conf" // copied in the container from configs/dhcpd.conf
159 logfile := "/tmp/dhcplog"
160 var stderr bytes.Buffer
161 cmd := exec.Command(dhcp, "-cf", conf, upstreamVeth, "-tf", logfile)
162 cmd.Stderr = &stderr
163 err := cmd.Run()
164 if err != nil {
165 nniLogger.Errorf("Fail to start DHCP Server: %s, %s", err, stderr.String())
166 return err
167 }
168 nniLogger.Info("Successfully activated DHCP Server")
169 return nil
170}
171
172func getVethHandler(vethName string) (*pcap.Handle, error) {
173 var (
174 device = vethName
175 snapshotLen int32 = 1518
176 promiscuous = false
177 timeout = pcap.BlockForever
178 )
179 handle, err := pcap.OpenLive(device, snapshotLen, promiscuous, timeout)
180 if err != nil {
181 nniLogger.Errorf("Can't retrieve handler for interface %s", vethName)
182 return nil, err
183 }
184 return handle, nil
185}
186
Matteo Scandolo27428702019-10-11 16:21:16 -0700187var listenOnVeth = func(vethName string) (chan *types.PacketMsg, error) {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700188
189 handle, err := getVethHandler(vethName)
190 if err != nil {
191 return nil, err
192 }
193
194 channel := make(chan *types.PacketMsg, 32)
195
196 go func() {
197 packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
198 for packet := range packetSource.Packets() {
199
200 if !packetHandlers.IsIncomingPacket(packet) {
201 nniLogger.Tracef("Ignoring packet as it's going out")
202 continue
203 }
204
205 nniLogger.WithFields(log.Fields{
206 "packet": packet.Dump(),
207 }).Tracef("Received packet on NNI Port")
208 pkt := types.PacketMsg{
209 Pkt: packet,
210 }
211 channel <- &pkt
212 }
213 }()
214
215 return channel, nil
216}