blob: 32fffaa80532838aadbb5c671a46643b0fd6ddcb [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 (
Matteo Scandolo4a036262020-08-17 15:56:13 -070020 "encoding/hex"
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070021 "github.com/google/gopacket"
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070022 "github.com/looplab/fsm"
23 "github.com/opencord/bbsim/internal/bbsim/packetHandlers"
Matteo Scandolo90d08f62020-10-29 12:06:55 -070024 "github.com/opencord/voltha-protos/v4/go/openolt"
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070025 log "github.com/sirupsen/logrus"
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070026)
27
Matteo Scandolo90d08f62020-10-29 12:06:55 -070028var nniLogger = log.WithFields(log.Fields{"module": "NNI"})
Matteo Scandolo27428702019-10-11 16:21:16 -070029
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070030type NniPort struct {
31 // BBSIM Internals
Matteo Scandolo90d08f62020-10-29 12:06:55 -070032 ID uint32
33 Olt *OltDevice
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070034
35 // PON Attributes
Matteo Scandolo90d08f62020-10-29 12:06:55 -070036 OperState *fsm.FSM
37 Type string
38 PacketCount uint64 // dummy value for the stats
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070039}
40
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070041func CreateNNI(olt *OltDevice) (NniPort, error) {
42 nniPort := NniPort{
Matteo Scandolo90d08f62020-10-29 12:06:55 -070043 ID: uint32(0),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070044 OperState: getOperStateFSM(func(e *fsm.Event) {
45 oltLogger.Debugf("Changing NNI OperState from %s to %s", e.Src, e.Dst)
46 }),
47 Type: "nni",
Matteo Scandolo90d08f62020-10-29 12:06:55 -070048 Olt: olt,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070049 }
Matteo Scandolo90d08f62020-10-29 12:06:55 -070050
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070051 return nniPort, nil
52}
53
Matteo Scandolo90d08f62020-10-29 12:06:55 -070054// handleNniPacket will send a packet to a fake DHCP server implementation
55func (n *NniPort) handleNniPacket(packet gopacket.Packet) error {
Matteo Scandolo73c488d2019-11-01 14:44:22 -070056 isDhcp := packetHandlers.IsDhcpPacket(packet)
57 isLldp := packetHandlers.IsLldpPacket(packet)
58
Shrey Baid688b4242020-07-10 20:40:10 +053059 if !isDhcp && !isLldp {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070060 nniLogger.WithFields(log.Fields{
61 "packet": packet,
62 }).Trace("Dropping NNI packet as it's not DHCP")
Matteo Scandolo73c488d2019-11-01 14:44:22 -070063 return nil
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070064 }
65
Matteo Scandolo73c488d2019-11-01 14:44:22 -070066 if isDhcp {
Matteo Scandolo90d08f62020-10-29 12:06:55 -070067
68 // get a response packet from the DHCP server
69 pkt, err := n.Olt.dhcpServer.HandleServerPacket(packet)
Matteo Scandolo73c488d2019-11-01 14:44:22 -070070 if err != nil {
71 nniLogger.WithFields(log.Fields{
Matteo Scandolo90d08f62020-10-29 12:06:55 -070072 "SourcePkt": hex.EncodeToString(packet.Data()),
73 "Err": err,
74 }).Error("DHCP Server can't handle packet")
Matteo Scandolo73c488d2019-11-01 14:44:22 -070075 return err
76 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070077
Matteo Scandolo90d08f62020-10-29 12:06:55 -070078 // send packetIndication to VOLTHA
79 data := &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{
80 IntfType: "nni",
81 IntfId: n.ID,
82 Pkt: pkt.Data()}}
83 if err := n.Olt.OpenoltStream.Send(&openolt.Indication{Data: data}); err != nil {
84 oltLogger.WithFields(log.Fields{
85 "IntfType": data.PktInd.IntfType,
86 "IntfId": n.ID,
87 "Pkt": hex.EncodeToString(pkt.Data()),
88 }).Errorf("Fail to send PktInd indication: %v", err)
Matteo Scandolo73c488d2019-11-01 14:44:22 -070089 return err
90 }
Matteo Scandolo73c488d2019-11-01 14:44:22 -070091 } else if isLldp {
92 // TODO rework this when BBSim supports data-plane packets
93 nniLogger.Trace("Received LLDP Packet, ignoring it")
94 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070095 return nil
96}