blob: d4dfd23a26071dd2d475f389aad68533bfae5873 [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"
David K. Bainbridgec415efe2021-08-19 13:05:21 +000024 "github.com/opencord/voltha-protos/v5/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)
Matteo Scandoloec170af2021-10-07 11:53:22 -070058 isIcmp := packetHandlers.IsIcmpPacket(packet)
Matteo Scandolo73c488d2019-11-01 14:44:22 -070059
Matteo Scandoloec170af2021-10-07 11:53:22 -070060 if !isDhcp && !isLldp && !isIcmp {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070061 nniLogger.WithFields(log.Fields{
62 "packet": packet,
63 }).Trace("Dropping NNI packet as it's not DHCP")
Matteo Scandolo73c488d2019-11-01 14:44:22 -070064 return nil
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070065 }
66
Matteo Scandolo73c488d2019-11-01 14:44:22 -070067 if isDhcp {
Matteo Scandolo90d08f62020-10-29 12:06:55 -070068
69 // get a response packet from the DHCP server
70 pkt, err := n.Olt.dhcpServer.HandleServerPacket(packet)
Matteo Scandolo73c488d2019-11-01 14:44:22 -070071 if err != nil {
72 nniLogger.WithFields(log.Fields{
Matteo Scandolo90d08f62020-10-29 12:06:55 -070073 "SourcePkt": hex.EncodeToString(packet.Data()),
74 "Err": err,
75 }).Error("DHCP Server can't handle packet")
Matteo Scandolo73c488d2019-11-01 14:44:22 -070076 return err
77 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070078
Matteo Scandolo90d08f62020-10-29 12:06:55 -070079 // send packetIndication to VOLTHA
80 data := &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{
81 IntfType: "nni",
82 IntfId: n.ID,
83 Pkt: pkt.Data()}}
84 if err := n.Olt.OpenoltStream.Send(&openolt.Indication{Data: data}); err != nil {
85 oltLogger.WithFields(log.Fields{
86 "IntfType": data.PktInd.IntfType,
87 "IntfId": n.ID,
88 "Pkt": hex.EncodeToString(pkt.Data()),
89 }).Errorf("Fail to send PktInd indication: %v", err)
Matteo Scandolo73c488d2019-11-01 14:44:22 -070090 return err
91 }
Matteo Scandolo73c488d2019-11-01 14:44:22 -070092 } else if isLldp {
93 // TODO rework this when BBSim supports data-plane packets
94 nniLogger.Trace("Received LLDP Packet, ignoring it")
Matteo Scandoloec170af2021-10-07 11:53:22 -070095 } else if isIcmp {
96 nniLogger.Trace("Received ICMP Packet, ignoring it")
Matteo Scandolo73c488d2019-11-01 14:44:22 -070097 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070098 return nil
99}