Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package devices |
| 18 | |
| 19 | import ( |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 20 | "encoding/hex" |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 21 | "github.com/google/gopacket" |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 22 | "github.com/looplab/fsm" |
| 23 | "github.com/opencord/bbsim/internal/bbsim/packetHandlers" |
David K. Bainbridge | c415efe | 2021-08-19 13:05:21 +0000 | [diff] [blame] | 24 | "github.com/opencord/voltha-protos/v5/go/openolt" |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 25 | log "github.com/sirupsen/logrus" |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 26 | ) |
| 27 | |
Matteo Scandolo | 90d08f6 | 2020-10-29 12:06:55 -0700 | [diff] [blame] | 28 | var nniLogger = log.WithFields(log.Fields{"module": "NNI"}) |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 29 | |
Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame] | 30 | type NniPort struct { |
| 31 | // BBSIM Internals |
Matteo Scandolo | 90d08f6 | 2020-10-29 12:06:55 -0700 | [diff] [blame] | 32 | ID uint32 |
| 33 | Olt *OltDevice |
Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame] | 34 | |
| 35 | // PON Attributes |
Matteo Scandolo | 90d08f6 | 2020-10-29 12:06:55 -0700 | [diff] [blame] | 36 | OperState *fsm.FSM |
| 37 | Type string |
| 38 | PacketCount uint64 // dummy value for the stats |
Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 41 | func CreateNNI(olt *OltDevice) (NniPort, error) { |
| 42 | nniPort := NniPort{ |
Matteo Scandolo | 90d08f6 | 2020-10-29 12:06:55 -0700 | [diff] [blame] | 43 | ID: uint32(0), |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 44 | 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 Scandolo | 90d08f6 | 2020-10-29 12:06:55 -0700 | [diff] [blame] | 48 | Olt: olt, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 49 | } |
Matteo Scandolo | 90d08f6 | 2020-10-29 12:06:55 -0700 | [diff] [blame] | 50 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 51 | return nniPort, nil |
| 52 | } |
| 53 | |
Matteo Scandolo | 90d08f6 | 2020-10-29 12:06:55 -0700 | [diff] [blame] | 54 | // handleNniPacket will send a packet to a fake DHCP server implementation |
| 55 | func (n *NniPort) handleNniPacket(packet gopacket.Packet) error { |
Matteo Scandolo | 73c488d | 2019-11-01 14:44:22 -0700 | [diff] [blame] | 56 | isDhcp := packetHandlers.IsDhcpPacket(packet) |
| 57 | isLldp := packetHandlers.IsLldpPacket(packet) |
Matteo Scandolo | ec170af | 2021-10-07 11:53:22 -0700 | [diff] [blame] | 58 | isIcmp := packetHandlers.IsIcmpPacket(packet) |
Matteo Scandolo | 73c488d | 2019-11-01 14:44:22 -0700 | [diff] [blame] | 59 | |
Matteo Scandolo | ec170af | 2021-10-07 11:53:22 -0700 | [diff] [blame] | 60 | if !isDhcp && !isLldp && !isIcmp { |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 61 | nniLogger.WithFields(log.Fields{ |
| 62 | "packet": packet, |
| 63 | }).Trace("Dropping NNI packet as it's not DHCP") |
Matteo Scandolo | 73c488d | 2019-11-01 14:44:22 -0700 | [diff] [blame] | 64 | return nil |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Matteo Scandolo | 73c488d | 2019-11-01 14:44:22 -0700 | [diff] [blame] | 67 | if isDhcp { |
Matteo Scandolo | 90d08f6 | 2020-10-29 12:06:55 -0700 | [diff] [blame] | 68 | |
| 69 | // get a response packet from the DHCP server |
| 70 | pkt, err := n.Olt.dhcpServer.HandleServerPacket(packet) |
Matteo Scandolo | 73c488d | 2019-11-01 14:44:22 -0700 | [diff] [blame] | 71 | if err != nil { |
| 72 | nniLogger.WithFields(log.Fields{ |
Matteo Scandolo | 90d08f6 | 2020-10-29 12:06:55 -0700 | [diff] [blame] | 73 | "SourcePkt": hex.EncodeToString(packet.Data()), |
| 74 | "Err": err, |
| 75 | }).Error("DHCP Server can't handle packet") |
Matteo Scandolo | 73c488d | 2019-11-01 14:44:22 -0700 | [diff] [blame] | 76 | return err |
| 77 | } |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 78 | |
Matteo Scandolo | 90d08f6 | 2020-10-29 12:06:55 -0700 | [diff] [blame] | 79 | // 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 Scandolo | 73c488d | 2019-11-01 14:44:22 -0700 | [diff] [blame] | 90 | return err |
| 91 | } |
Matteo Scandolo | 73c488d | 2019-11-01 14:44:22 -0700 | [diff] [blame] | 92 | } else if isLldp { |
| 93 | // TODO rework this when BBSim supports data-plane packets |
| 94 | nniLogger.Trace("Received LLDP Packet, ignoring it") |
Matteo Scandolo | ec170af | 2021-10-07 11:53:22 -0700 | [diff] [blame] | 95 | } else if isIcmp { |
| 96 | nniLogger.Trace("Received ICMP Packet, ignoring it") |
Matteo Scandolo | 73c488d | 2019-11-01 14:44:22 -0700 | [diff] [blame] | 97 | } |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 98 | return nil |
| 99 | } |