blob: 86c70ba58575a7c76c76ea9386fe9a455f44f8a6 [file] [log] [blame]
Matteo Scandolo11006992019-08-28 11:29:46 -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
Matteo Scandolo4747d292019-08-05 11:50:18 -070017package devices
18
19import (
Matteo Scandolo40e067f2019-10-16 16:59:41 -070020 "context"
Matteo Scandolo618a6582020-09-09 12:21:29 -070021 "encoding/hex"
Matteo Scandolo3bc73742019-08-20 14:04:04 -070022 "fmt"
Matteo Scandolof9d43412021-01-12 11:11:34 -080023 pb "github.com/opencord/bbsim/api/bbsim"
24 "github.com/opencord/bbsim/internal/bbsim/alarmsim"
25 bbsim "github.com/opencord/bbsim/internal/bbsim/types"
26 me "github.com/opencord/omci-lib-go/generated"
27 "strconv"
28
Matteo Scandolo4a036262020-08-17 15:56:13 -070029 "github.com/opencord/bbsim/internal/bbsim/packetHandlers"
30 "github.com/opencord/bbsim/internal/bbsim/responders/dhcp"
31 "github.com/opencord/bbsim/internal/bbsim/responders/eapol"
Zdravko Bozakov681364d2019-11-10 14:28:46 +010032 "net"
Zdravko Bozakov681364d2019-11-10 14:28:46 +010033 "time"
34
Matteo Scandolo3bc73742019-08-20 14:04:04 -070035 "github.com/google/gopacket/layers"
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -070036 "github.com/jpillora/backoff"
Matteo Scandolo4747d292019-08-05 11:50:18 -070037 "github.com/looplab/fsm"
Matteo Scandolo40e067f2019-10-16 16:59:41 -070038 "github.com/opencord/bbsim/internal/common"
39 omcilib "github.com/opencord/bbsim/internal/common/omci"
Matteo Scandolof9d43412021-01-12 11:11:34 -080040 "github.com/opencord/omci-lib-go"
Matteo Scandolo4f4ac792020-10-01 16:33:21 -070041 "github.com/opencord/voltha-protos/v4/go/openolt"
42 "github.com/opencord/voltha-protos/v4/go/tech_profile"
Matteo Scandolo4747d292019-08-05 11:50:18 -070043 log "github.com/sirupsen/logrus"
44)
45
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070046var onuLogger = log.WithFields(log.Fields{
47 "module": "ONU",
48})
49
Pragya Arya8bdb4532020-03-02 17:08:09 +053050type FlowKey struct {
Matteo Scandolo4f4ac792020-10-01 16:33:21 -070051 ID uint64
Pragya Arya8bdb4532020-03-02 17:08:09 +053052 Direction string
53}
54
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070055type Onu struct {
Matteo Scandoloe811ae92019-12-10 17:50:14 -080056 ID uint32
57 PonPortID uint32
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -070058 PonPort *PonPort
Matteo Scandoloe811ae92019-12-10 17:50:14 -080059 InternalState *fsm.FSM
Pragya Arya2225f202020-01-29 18:05:01 +053060 DiscoveryRetryDelay time.Duration // this is the time between subsequent Discovery Indication
61 DiscoveryDelay time.Duration // this is the time to send the first Discovery Indication
Matteo Scandolo4a036262020-08-17 15:56:13 -070062
63 Services []ServiceIf
64
65 Backoff *backoff.Backoff
Matteo Scandoloe811ae92019-12-10 17:50:14 -080066 // ONU State
Matteo Scandolo27428702019-10-11 16:21:16 -070067 // PortNo comes with flows and it's used when sending packetIndications,
68 // There is one PortNo per UNI Port, for now we're only storing the first one
Matteo Scandolo47ef64b2020-04-20 14:16:07 -070069 // FIXME add support for multiple UNIs (each UNI has a different PortNo)
Matteo Scandolo75ed5b92020-09-03 09:03:16 -070070 PortNo uint32
71 // deprecated (gemPort is on a Service basis)
Matteo Scandolo4a036262020-08-17 15:56:13 -070072 GemPortAdded bool
73 Flows []FlowKey
Matteo Scandolo4f4ac792020-10-01 16:33:21 -070074 FlowIds []uint64 // keep track of the flows we currently have in the ONU
Matteo Scandolo99f18462019-10-28 14:14:28 -070075
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070076 OperState *fsm.FSM
77 SerialNumber *openolt.SerialNumber
78
Matteo Scandolof9d43412021-01-12 11:11:34 -080079 Channel chan bbsim.Message // this Channel is to track state changes OMCI messages, EAPOL and DHCP packets
Matteo Scandolo40e067f2019-10-16 16:59:41 -070080
81 // OMCI params
Matteo Scandolo992a23e2021-02-04 15:35:04 -080082 MibDataSync uint8
83
84 // OMCI params (Used in BBR)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -070085 tid uint16
86 hpTid uint16
87 seqNumber uint16
Matteo Scandolo40e067f2019-10-16 16:59:41 -070088
Anand S Katti09541352020-01-29 15:54:01 +053089 DoneChannel chan bool // this channel is used to signal once the onu is complete (when the struct is used by BBR)
90 TrafficSchedulers *tech_profile.TrafficSchedulers
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070091}
92
Matteo Scandolo99f18462019-10-28 14:14:28 -070093func (o *Onu) Sn() string {
Matteo Scandolo40e067f2019-10-16 16:59:41 -070094 return common.OnuSnToString(o.SerialNumber)
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070095}
96
Matteo Scandolo4a036262020-08-17 15:56:13 -070097func CreateONU(olt *OltDevice, pon *PonPort, id uint32, delay time.Duration, isMock bool) *Onu {
Matteo Scandolo4747d292019-08-05 11:50:18 -070098
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070099 o := Onu{
Matteo Scandolo4a036262020-08-17 15:56:13 -0700100 ID: id,
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800101 PonPortID: pon.ID,
102 PonPort: pon,
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800103 PortNo: 0,
104 tid: 0x1,
105 hpTid: 0x8000,
106 seqNumber: 0,
107 DoneChannel: make(chan bool, 1),
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800108 GemPortAdded: false,
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800109 DiscoveryRetryDelay: 60 * time.Second, // this is used to send OnuDiscoveryIndications until an activate call is received
Pragya Arya8bdb4532020-03-02 17:08:09 +0530110 Flows: []FlowKey{},
Pragya Arya2225f202020-01-29 18:05:01 +0530111 DiscoveryDelay: delay,
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800112 MibDataSync: 0,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700113 }
Pragya Arya2225f202020-01-29 18:05:01 +0530114 o.SerialNumber = o.NewSN(olt.ID, pon.ID, id)
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700115 // NOTE this state machine is used to track the operational
116 // state as requested by VOLTHA
117 o.OperState = getOperStateFSM(func(e *fsm.Event) {
118 onuLogger.WithFields(log.Fields{
119 "ID": o.ID,
120 }).Debugf("Changing ONU OperState from %s to %s", e.Src, e.Dst)
121 })
122
123 // NOTE this state machine is used to activate the OMCI, EAPOL and DHCP clients
124 o.InternalState = fsm.NewFSM(
125 "created",
126 fsm.Events{
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700127 // DEVICE Lifecycle
Hardik Windlassad790cb2020-06-17 21:26:22 +0530128 {Name: "initialize", Src: []string{"created", "disabled", "pon_disabled"}, Dst: "initialized"},
129 {Name: "discover", Src: []string{"initialized"}, Dst: "discovered"},
130 {Name: "enable", Src: []string{"discovered", "pon_disabled"}, Dst: "enabled"},
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100131 // NOTE should disabled state be different for oper_disabled (emulating an error) and admin_disabled (received a disabled call via VOLTHA)?
Matteo Scandolof380a972020-09-11 12:09:40 -0700132 {Name: "disable", Src: []string{"enabled", "pon_disabled"}, Dst: "disabled"},
Pragya Arya6a708d62020-01-01 17:17:20 +0530133 // ONU state when PON port is disabled but ONU is power ON(more states should be added in src?)
Matteo Scandolof380a972020-09-11 12:09:40 -0700134 {Name: "pon_disabled", Src: []string{"enabled"}, Dst: "pon_disabled"},
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700135 // BBR States
136 // TODO add start OMCI state
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100137 {Name: "send_eapol_flow", Src: []string{"initialized"}, Dst: "eapol_flow_sent"},
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700138 {Name: "send_dhcp_flow", Src: []string{"eapol_flow_sent"}, Dst: "dhcp_flow_sent"},
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700139 },
140 fsm.Callbacks{
141 "enter_state": func(e *fsm.Event) {
142 o.logStateChange(e.Src, e.Dst)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700143 },
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100144 "enter_initialized": func(e *fsm.Event) {
145 // create new channel for ProcessOnuMessages Go routine
Matteo Scandolof9d43412021-01-12 11:11:34 -0800146 o.Channel = make(chan bbsim.Message, 2048)
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800147
148 if err := o.OperState.Event("enable"); err != nil {
149 onuLogger.WithFields(log.Fields{
150 "OnuId": o.ID,
151 "IntfId": o.PonPortID,
152 "OnuSn": o.Sn(),
153 }).Errorf("Cannot change ONU OperState to up: %s", err.Error())
154 }
155
Pragya Arya1cbefa42020-01-13 12:15:29 +0530156 if !isMock {
157 // start ProcessOnuMessages Go routine
Matteo Scandolo4a036262020-08-17 15:56:13 -0700158 go o.ProcessOnuMessages(olt.enableContext, olt.OpenoltStream, nil)
Pragya Arya1cbefa42020-01-13 12:15:29 +0530159 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100160 },
161 "enter_discovered": func(e *fsm.Event) {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800162 msg := bbsim.Message{
163 Type: bbsim.OnuDiscIndication,
164 Data: bbsim.OnuDiscIndicationMessage{
165 OperState: bbsim.UP,
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100166 },
167 }
168 o.Channel <- msg
169 },
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700170 "enter_enabled": func(event *fsm.Event) {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800171 msg := bbsim.Message{
172 Type: bbsim.OnuIndication,
173 Data: bbsim.OnuIndicationMessage{
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700174 OnuSN: o.SerialNumber,
175 PonPortID: o.PonPortID,
Matteo Scandolof9d43412021-01-12 11:11:34 -0800176 OperState: bbsim.UP,
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700177 },
178 }
179 o.Channel <- msg
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700180
181 // Once the ONU is enabled start listening for packets
182 for _, s := range o.Services {
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700183 s.Initialize(o.PonPort.Olt.OpenoltStream)
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700184 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700185 },
186 "enter_disabled": func(event *fsm.Event) {
Matteo Scandolo47ef64b2020-04-20 14:16:07 -0700187
188 // clean the ONU state
Matteo Scandolo328c5f02020-06-26 14:16:39 -0700189 o.GemPortAdded = false
Matteo Scandolo47ef64b2020-04-20 14:16:07 -0700190 o.PortNo = 0
191 o.Flows = []FlowKey{}
192
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700193 // set the OperState to disabled
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800194 if err := o.OperState.Event("disable"); err != nil {
195 onuLogger.WithFields(log.Fields{
196 "OnuId": o.ID,
197 "IntfId": o.PonPortID,
198 "OnuSn": o.Sn(),
199 }).Errorf("Cannot change ONU OperState to down: %s", err.Error())
200 }
Matteo Scandolo47ef64b2020-04-20 14:16:07 -0700201
202 // send the OnuIndication DOWN event
Matteo Scandolof9d43412021-01-12 11:11:34 -0800203 msg := bbsim.Message{
204 Type: bbsim.OnuIndication,
205 Data: bbsim.OnuIndicationMessage{
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700206 OnuSN: o.SerialNumber,
207 PonPortID: o.PonPortID,
Matteo Scandolof9d43412021-01-12 11:11:34 -0800208 OperState: bbsim.DOWN,
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700209 },
210 }
211 o.Channel <- msg
Hardik Windlass7b3405b2020-07-08 15:10:05 +0530212
213 // verify all the flows removes are handled and
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100214 // terminate the ONU's ProcessOnuMessages Go routine
Hardik Windlass7b3405b2020-07-08 15:10:05 +0530215 if len(o.FlowIds) == 0 {
216 close(o.Channel)
217 }
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700218
219 for _, s := range o.Services {
220 s.Disable()
221 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700222 },
Matteo Scandolo4a036262020-08-17 15:56:13 -0700223 // BBR states
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700224 "enter_eapol_flow_sent": func(e *fsm.Event) {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800225 msg := bbsim.Message{
226 Type: bbsim.SendEapolFlow,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700227 }
228 o.Channel <- msg
229 },
230 "enter_dhcp_flow_sent": func(e *fsm.Event) {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800231 msg := bbsim.Message{
232 Type: bbsim.SendDhcpFlow,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700233 }
234 o.Channel <- msg
235 },
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700236 },
237 )
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100238
Matteo Scandolo27428702019-10-11 16:21:16 -0700239 return &o
Matteo Scandolo4747d292019-08-05 11:50:18 -0700240}
241
William Kurkian0418bc82019-11-06 12:16:24 -0500242func (o *Onu) logStateChange(src string, dst string) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700243 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700244 "OnuId": o.ID,
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700245 "IntfId": o.PonPortID,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700246 "OnuSn": o.Sn(),
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700247 }).Debugf("Changing ONU InternalState from %s to %s", src, dst)
248}
249
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100250// ProcessOnuMessages starts indication channel for each ONU
David Bainbridge103cf022019-12-16 20:11:35 +0000251func (o *Onu) ProcessOnuMessages(ctx context.Context, stream openolt.Openolt_EnableIndicationServer, client openolt.OpenoltClient) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700252 onuLogger.WithFields(log.Fields{
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100253 "onuID": o.ID,
254 "onuSN": o.Sn(),
255 "ponPort": o.PonPortID,
256 }).Debug("Starting ONU Indication Channel")
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700257
David Bainbridge103cf022019-12-16 20:11:35 +0000258loop:
259 for {
260 select {
261 case <-ctx.Done():
262 onuLogger.WithFields(log.Fields{
263 "onuID": o.ID,
264 "onuSN": o.Sn(),
265 }).Tracef("ONU message handling canceled via context")
266 break loop
267 case message, ok := <-o.Channel:
268 if !ok || ctx.Err() != nil {
269 onuLogger.WithFields(log.Fields{
270 "onuID": o.ID,
271 "onuSN": o.Sn(),
272 }).Tracef("ONU message handling canceled via channel close")
273 break loop
Matteo Scandolo075b1892019-10-07 12:11:07 -0700274 }
David Bainbridge103cf022019-12-16 20:11:35 +0000275 onuLogger.WithFields(log.Fields{
276 "onuID": o.ID,
277 "onuSN": o.Sn(),
278 "messageType": message.Type,
279 }).Tracef("Received message on ONU Channel")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700280
David Bainbridge103cf022019-12-16 20:11:35 +0000281 switch message.Type {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800282 case bbsim.OnuDiscIndication:
283 msg, _ := message.Data.(bbsim.OnuDiscIndicationMessage)
David Bainbridge103cf022019-12-16 20:11:35 +0000284 // NOTE we need to slow down and send ONU Discovery Indication in batches to better emulate a real scenario
Pragya Arya2225f202020-01-29 18:05:01 +0530285 time.Sleep(o.DiscoveryDelay)
David Bainbridge103cf022019-12-16 20:11:35 +0000286 o.sendOnuDiscIndication(msg, stream)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800287 case bbsim.OnuIndication:
288 msg, _ := message.Data.(bbsim.OnuIndicationMessage)
David Bainbridge103cf022019-12-16 20:11:35 +0000289 o.sendOnuIndication(msg, stream)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800290 case bbsim.OMCI:
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800291 // these are OMCI messages received by the ONU
Matteo Scandolof9d43412021-01-12 11:11:34 -0800292 msg, _ := message.Data.(bbsim.OmciMessage)
293 o.handleOmciRequest(msg, stream)
294 case bbsim.UniStatusAlarm:
295 msg, _ := message.Data.(bbsim.UniStatusAlarmMessage)
296 pkt := omcilib.CreateUniStatusAlarm(msg.AdminState, msg.EntityID)
297 if err := o.sendOmciIndication(pkt, 0, stream); err != nil {
298 onuLogger.WithFields(log.Fields{
299 "IntfId": o.PonPortID,
300 "SerialNumber": o.Sn(),
301 "omciPacket": pkt,
302 "adminState": msg.AdminState,
303 "entityID": msg.EntityID,
304 }).Errorf("failed-to-send-UNI-Link-Alarm: %v", err)
305 }
306 onuLogger.WithFields(log.Fields{
307 "IntfId": o.PonPortID,
308 "SerialNumber": o.Sn(),
309 "omciPacket": pkt,
310 "adminState": msg.AdminState,
311 "entityID": msg.EntityID,
312 }).Trace("UNI-Link-alarm-sent")
313 case bbsim.FlowAdd:
314 msg, _ := message.Data.(bbsim.OnuFlowUpdateMessage)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700315 o.handleFlowAdd(msg)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800316 case bbsim.FlowRemoved:
317 msg, _ := message.Data.(bbsim.OnuFlowUpdateMessage)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700318 o.handleFlowRemove(msg)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800319 case bbsim.OnuPacketOut:
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700320
Matteo Scandolof9d43412021-01-12 11:11:34 -0800321 msg, _ := message.Data.(bbsim.OnuPacketMessage)
David Bainbridge103cf022019-12-16 20:11:35 +0000322
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700323 onuLogger.WithFields(log.Fields{
David Bainbridge103cf022019-12-16 20:11:35 +0000324 "IntfId": msg.IntfId,
325 "OnuId": msg.OnuId,
326 "pktType": msg.Type,
327 }).Trace("Received OnuPacketOut Message")
328
Matteo Scandolo618a6582020-09-09 12:21:29 -0700329 if msg.Type == packetHandlers.EAPOL || msg.Type == packetHandlers.DHCP {
330
331 service, err := o.findServiceByMacAddress(msg.MacAddress)
332 if err != nil {
333 onuLogger.WithFields(log.Fields{
334 "IntfId": msg.IntfId,
335 "OnuId": msg.OnuId,
336 "pktType": msg.Type,
337 "MacAddress": msg.MacAddress,
338 "Pkt": hex.EncodeToString(msg.Packet.Data()),
339 "OnuSn": o.Sn(),
340 }).Error("Cannot find Service associated with packet")
341 return
342 }
343 service.PacketCh <- msg
344 } else if msg.Type == packetHandlers.IGMP {
345 // if it's an IGMP packet we assume we have a single IGMP service
346 for _, s := range o.Services {
347 service := s.(*Service)
348
349 if service.NeedsIgmp {
350 service.PacketCh <- msg
351 }
352 }
David Bainbridge103cf022019-12-16 20:11:35 +0000353 }
Matteo Scandolo4a036262020-08-17 15:56:13 -0700354
Matteo Scandolof9d43412021-01-12 11:11:34 -0800355 case bbsim.OnuPacketIn:
David Bainbridge103cf022019-12-16 20:11:35 +0000356 // NOTE we only receive BBR packets here.
357 // Eapol.HandleNextPacket can handle both BBSim and BBr cases so the call is the same
358 // in the DHCP case VOLTHA only act as a proxy, the behaviour is completely different thus we have a dhcp.HandleNextBbrPacket
Matteo Scandolof9d43412021-01-12 11:11:34 -0800359 msg, _ := message.Data.(bbsim.OnuPacketMessage)
David Bainbridge103cf022019-12-16 20:11:35 +0000360
361 log.WithFields(log.Fields{
362 "IntfId": msg.IntfId,
363 "OnuId": msg.OnuId,
364 "pktType": msg.Type,
365 }).Trace("Received OnuPacketIn Message")
366
367 if msg.Type == packetHandlers.EAPOL {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700368 eapol.HandleNextPacket(msg.OnuId, msg.IntfId, msg.GemPortId, o.Sn(), o.PortNo, o.InternalState, msg.Packet, stream, client)
David Bainbridge103cf022019-12-16 20:11:35 +0000369 } else if msg.Type == packetHandlers.DHCP {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700370 _ = dhcp.HandleNextBbrPacket(o.ID, o.PonPortID, o.Sn(), o.DoneChannel, msg.Packet, client)
David Bainbridge103cf022019-12-16 20:11:35 +0000371 }
Matteo Scandolo7f656fb2020-09-08 15:18:15 -0700372 // BBR specific messages
Matteo Scandolof9d43412021-01-12 11:11:34 -0800373 case bbsim.OmciIndication:
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800374 // these are OMCI messages received by BBR (VOLTHA emulator)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800375 msg, _ := message.Data.(bbsim.OmciIndicationMessage)
376 o.handleOmciResponse(msg, client)
377 case bbsim.SendEapolFlow:
David Bainbridge103cf022019-12-16 20:11:35 +0000378 o.sendEapolFlow(client)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800379 case bbsim.SendDhcpFlow:
David Bainbridge103cf022019-12-16 20:11:35 +0000380 o.sendDhcpFlow(client)
381 default:
382 onuLogger.Warnf("Received unknown message data %v for type %v in OLT Channel", message.Data, message.Type)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700383 }
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700384 }
385 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100386 onuLogger.WithFields(log.Fields{
387 "onuID": o.ID,
388 "onuSN": o.Sn(),
389 }).Debug("Stopped handling ONU Indication Channel")
Matteo Scandolo4747d292019-08-05 11:50:18 -0700390}
391
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100392func (o Onu) NewSN(oltid int, intfid uint32, onuid uint32) *openolt.SerialNumber {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700393
394 sn := new(openolt.SerialNumber)
395
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700396 //sn = new(openolt.SerialNumber)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700397 sn.VendorId = []byte("BBSM")
398 sn.VendorSpecific = []byte{0, byte(oltid % 256), byte(intfid), byte(onuid)}
399
400 return sn
401}
402
Matteo Scandolof9d43412021-01-12 11:11:34 -0800403func (o *Onu) sendOnuDiscIndication(msg bbsim.OnuDiscIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700404 discoverData := &openolt.Indication_OnuDiscInd{OnuDiscInd: &openolt.OnuDiscIndication{
Matteo Scandolof9d43412021-01-12 11:11:34 -0800405 IntfId: o.PonPortID,
406 SerialNumber: o.SerialNumber,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700407 }}
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700408
Matteo Scandolo4747d292019-08-05 11:50:18 -0700409 if err := stream.Send(&openolt.Indication{Data: discoverData}); err != nil {
Matteo Scandolo11006992019-08-28 11:29:46 -0700410 log.Errorf("Failed to send Indication_OnuDiscInd: %v", err)
Matteo Scandolo99f18462019-10-28 14:14:28 -0700411 return
Matteo Scandolo4747d292019-08-05 11:50:18 -0700412 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700413
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700414 onuLogger.WithFields(log.Fields{
Matteo Scandolof9d43412021-01-12 11:11:34 -0800415 "IntfId": o.PonPortID,
416 "OnuSn": o.Sn(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700417 "OnuId": o.ID,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700418 }).Debug("Sent Indication_OnuDiscInd")
Matteo Scandolof9d43412021-01-12 11:11:34 -0800419 publishEvent("ONU-discovery-indication-sent", int32(o.PonPortID), int32(o.ID), o.Sn())
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800420
421 // after DiscoveryRetryDelay check if the state is the same and in case send a new OnuDiscIndication
422 go func(delay time.Duration) {
Matteo Scandolo569e7172019-12-20 11:51:51 -0800423 time.Sleep(delay)
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800424 if o.InternalState.Current() == "discovered" {
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800425 o.sendOnuDiscIndication(msg, stream)
426 }
427 }(o.DiscoveryRetryDelay)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700428}
429
Matteo Scandolof9d43412021-01-12 11:11:34 -0800430func (o *Onu) sendOnuIndication(msg bbsim.OnuIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700431 // NOTE voltha returns an ID, but if we use that ID then it complains:
432 // expected_onu_id: 1, received_onu_id: 1024, event: ONU-id-mismatch, can happen if both voltha and the olt rebooted
433 // so we're using the internal ID that is 1
434 // o.ID = msg.OnuID
Matteo Scandolo4747d292019-08-05 11:50:18 -0700435
436 indData := &openolt.Indication_OnuInd{OnuInd: &openolt.OnuIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700437 IntfId: o.PonPortID,
438 OnuId: o.ID,
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700439 OperState: msg.OperState.String(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700440 AdminState: o.OperState.Current(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700441 SerialNumber: o.SerialNumber,
442 }}
443 if err := stream.Send(&openolt.Indication{Data: indData}); err != nil {
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800444 // NOTE do we need to transition to a broken state?
Matteo Scandolo11006992019-08-28 11:29:46 -0700445 log.Errorf("Failed to send Indication_OnuInd: %v", err)
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700446 return
Matteo Scandolo4747d292019-08-05 11:50:18 -0700447 }
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700448 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700449 "IntfId": o.PonPortID,
450 "OnuId": o.ID,
451 "OperState": msg.OperState.String(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700452 "AdminState": msg.OperState.String(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700453 "OnuSn": o.Sn(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700454 }).Debug("Sent Indication_OnuInd")
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700455
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700456}
457
Matteo Scandolof9d43412021-01-12 11:11:34 -0800458func (o *Onu) HandleShutdownONU() error {
459
460 dyingGasp := pb.ONUAlarmRequest{
461 AlarmType: "DYING_GASP",
462 SerialNumber: o.Sn(),
463 Status: "on",
464 }
465
466 if err := alarmsim.SimulateOnuAlarm(&dyingGasp, o.ID, o.PonPortID, o.PonPort.Olt.channel); err != nil {
467 onuLogger.WithFields(log.Fields{
468 "OnuId": o.ID,
469 "IntfId": o.PonPortID,
470 "OnuSn": o.Sn(),
471 }).Errorf("Cannot send Dying Gasp: %s", err.Error())
472 return err
473 }
474
475 losReq := pb.ONUAlarmRequest{
476 AlarmType: "ONU_ALARM_LOS",
477 SerialNumber: o.Sn(),
478 Status: "on",
479 }
480
481 if err := alarmsim.SimulateOnuAlarm(&losReq, o.ID, o.PonPortID, o.PonPort.Olt.channel); err != nil {
482 onuLogger.WithFields(log.Fields{
483 "OnuId": o.ID,
484 "IntfId": o.PonPortID,
485 "OnuSn": o.Sn(),
486 }).Errorf("Cannot send LOS: %s", err.Error())
487
488 return err
489 }
490
491 // TODO if it's the last ONU on the PON, then send a PON LOS
492
493 if err := o.InternalState.Event("disable"); err != nil {
494 onuLogger.WithFields(log.Fields{
495 "OnuId": o.ID,
496 "IntfId": o.PonPortID,
497 "OnuSn": o.Sn(),
498 }).Errorf("Cannot shutdown ONU: %s", err.Error())
499 return err
500 }
501
502 return nil
503}
504
505func (o *Onu) HandlePowerOnONU() error {
506 intitalState := o.InternalState.Current()
507
508 // initialize the ONU
509 if intitalState == "created" || intitalState == "disabled" {
510 if err := o.InternalState.Event("initialize"); err != nil {
511 onuLogger.WithFields(log.Fields{
512 "OnuId": o.ID,
513 "IntfId": o.PonPortID,
514 "OnuSn": o.Sn(),
515 }).Errorf("Cannot poweron ONU: %s", err.Error())
516 return err
517 }
518 }
519
520 // turn off the LOS Alarm
521 losReq := pb.ONUAlarmRequest{
522 AlarmType: "ONU_ALARM_LOS",
523 SerialNumber: o.Sn(),
524 Status: "off",
525 }
526
527 if err := alarmsim.SimulateOnuAlarm(&losReq, o.ID, o.PonPortID, o.PonPort.Olt.channel); err != nil {
528 onuLogger.WithFields(log.Fields{
529 "OnuId": o.ID,
530 "IntfId": o.PonPortID,
531 "OnuSn": o.Sn(),
532 }).Errorf("Cannot send LOS: %s", err.Error())
533 return err
534 }
535
536 // Send a ONU Discovery indication
537 if err := o.InternalState.Event("discover"); err != nil {
538 onuLogger.WithFields(log.Fields{
539 "OnuId": o.ID,
540 "IntfId": o.PonPortID,
541 "OnuSn": o.Sn(),
542 }).Errorf("Cannot poweron ONU: %s", err.Error())
543 return err
544 }
545
546 // move o directly to enable state only when its a powercycle case
547 // in case of first time o poweron o will be moved to enable on
548 // receiving ActivateOnu request from openolt adapter
549 if intitalState == "disabled" {
550 if err := o.InternalState.Event("enable"); err != nil {
551 onuLogger.WithFields(log.Fields{
552 "OnuId": o.ID,
553 "IntfId": o.PonPortID,
554 "OnuSn": o.Sn(),
555 }).Errorf("Cannot enable ONU: %s", err.Error())
556 return err
557 }
558 }
559
560 return nil
561}
562
563func (o *Onu) SetAlarm(alarmType string, status string) error {
564 alarmReq := pb.ONUAlarmRequest{
565 AlarmType: alarmType,
566 SerialNumber: o.Sn(),
567 Status: status,
568 }
569
570 err := alarmsim.SimulateOnuAlarm(&alarmReq, o.ID, o.PonPortID, o.PonPort.Olt.channel)
571 if err != nil {
572 return err
573 }
574 return nil
575}
576
577func (o *Onu) publishOmciEvent(msg bbsim.OmciMessage) {
Pragya Arya324337e2020-02-20 14:35:08 +0530578 if olt.PublishEvents {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800579 _, omciMsg, err := omcilib.ParseOpenOltOmciPacket(msg.OmciMsg.Pkt)
Pragya Arya324337e2020-02-20 14:35:08 +0530580 if err != nil {
581 log.Errorf("error in getting msgType %v", err)
582 return
583 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800584 if omciMsg.MessageType == omci.MibUploadRequestType {
Pragya Arya324337e2020-02-20 14:35:08 +0530585 o.seqNumber = 0
586 publishEvent("MIB-upload-received", int32(o.PonPortID), int32(o.ID), common.OnuSnToString(o.SerialNumber))
Matteo Scandolof9d43412021-01-12 11:11:34 -0800587 } else if omciMsg.MessageType == omci.MibUploadNextRequestType {
Pragya Arya324337e2020-02-20 14:35:08 +0530588 o.seqNumber++
589 if o.seqNumber > 290 {
590 publishEvent("MIB-upload-done", int32(o.PonPortID), int32(o.ID), common.OnuSnToString(o.SerialNumber))
591 }
592 }
593 }
594}
595
Scott Bakerb90c4312020-03-12 21:33:25 -0700596// Create a TestResponse packet and send it
Matteo Scandolof9d43412021-01-12 11:11:34 -0800597func (o *Onu) sendTestResult(msg bbsim.OmciMessage, stream openolt.Openolt_EnableIndicationServer) error {
598 resp, err := omcilib.BuildTestResult(msg.OmciMsg.Pkt)
Scott Bakerb90c4312020-03-12 21:33:25 -0700599 if err != nil {
600 return err
601 }
602
603 var omciInd openolt.OmciIndication
604 omciInd.IntfId = o.PonPortID
605 omciInd.OnuId = o.ID
606 omciInd.Pkt = resp
607
608 omci := &openolt.Indication_OmciInd{OmciInd: &omciInd}
609 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
Scott Bakerb90c4312020-03-12 21:33:25 -0700610 return err
611 }
612 onuLogger.WithFields(log.Fields{
613 "IntfId": o.PonPortID,
614 "SerialNumber": o.Sn(),
615 "omciPacket": omciInd.Pkt,
616 }).Tracef("Sent TestResult OMCI message")
617
618 return nil
619}
620
Matteo Scandolof9d43412021-01-12 11:11:34 -0800621// handleOmciRequest is responsible to parse the OMCI packets received from the openolt adapter
622// and generate the appropriate response to it
623func (o *Onu) handleOmciRequest(msg bbsim.OmciMessage, stream openolt.Openolt_EnableIndicationServer) {
624
625 omciPkt, omciMsg, err := omcilib.ParseOpenOltOmciPacket(msg.OmciMsg.Pkt)
626 if err != nil {
627 log.WithFields(log.Fields{
628 "IntfId": o.PonPortID,
629 "SerialNumber": o.Sn(),
630 "omciPacket": msg.OmciMsg.Pkt,
631 }).Error("cannot-parse-OMCI-packet")
632 }
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700633
634 onuLogger.WithFields(log.Fields{
Matteo Scandolof9d43412021-01-12 11:11:34 -0800635 "omciMsgType": omciMsg.MessageType,
636 "transCorrId": strconv.FormatInt(int64(omciMsg.TransactionID), 16),
637 "DeviceIdent": omciMsg.DeviceIdentifier,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700638 "IntfId": o.PonPortID,
Matteo Scandolo27428702019-10-11 16:21:16 -0700639 "SerialNumber": o.Sn(),
Matteo Scandolof9d43412021-01-12 11:11:34 -0800640 }).Trace("omci-message-decoded")
641
642 var responsePkt []byte
643 switch omciMsg.MessageType {
644 case omci.MibResetRequestType:
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800645 o.MibDataSync = 0
646 onuLogger.WithFields(log.Fields{
647 "IntfId": o.PonPortID,
648 "OnuId": o.ID,
649 "SerialNumber": o.Sn(),
650 }).Debug("received-mib-reset-request-resetting-mds")
Matteo Scandolof9d43412021-01-12 11:11:34 -0800651 responsePkt, _ = omcilib.CreateMibResetResponse(omciMsg.TransactionID)
652 case omci.MibUploadRequestType:
653 responsePkt, _ = omcilib.CreateMibUploadResponse(omciMsg.TransactionID)
654 case omci.MibUploadNextRequestType:
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800655 responsePkt, _ = omcilib.CreateMibUploadNextResponse(omciPkt, omciMsg, o.MibDataSync)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800656 case omci.GetRequestType:
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800657 responsePkt, _ = omcilib.CreateGetResponse(omciPkt, omciMsg, o.SerialNumber, o.MibDataSync)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800658 case omci.SetRequestType:
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800659 o.MibDataSync++
Matteo Scandolof9d43412021-01-12 11:11:34 -0800660 responsePkt, _ = omcilib.CreateSetResponse(omciPkt, omciMsg)
661
662 msgObj, _ := omcilib.ParseSetRequest(omciPkt)
663 switch msgObj.EntityClass {
664 case me.PhysicalPathTerminationPointEthernetUniClassID:
665 // if we're Setting a PPTP state
666 // we need to send the appropriate alarm
667
668 if msgObj.EntityInstance == 257 {
669 // for now we're only caring about the first UNI
670 // NOTE that the EntityID for the UNI port is for now hardcoded in
671 // omci/mibpackets.go where the PhysicalPathTerminationPointEthernetUni
672 // are reported during the MIB Upload sequence
673 adminState := msgObj.Attributes["AdministrativeState"].(uint8)
674 msg := bbsim.Message{
675 Type: bbsim.UniStatusAlarm,
676 Data: bbsim.UniStatusAlarmMessage{
677 OnuSN: o.SerialNumber,
678 OnuID: o.ID,
679 AdminState: adminState,
680 EntityID: msgObj.EntityInstance,
681 },
682 }
683 o.Channel <- msg
684 }
685 }
686 case omci.CreateRequestType:
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800687 o.MibDataSync++
Matteo Scandolof9d43412021-01-12 11:11:34 -0800688 responsePkt, _ = omcilib.CreateCreateResponse(omciPkt, omciMsg)
689 case omci.DeleteRequestType:
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800690 o.MibDataSync++
Matteo Scandolof9d43412021-01-12 11:11:34 -0800691 responsePkt, _ = omcilib.CreateDeleteResponse(omciPkt, omciMsg)
692 case omci.RebootRequestType:
693
694 responsePkt, _ = omcilib.CreateRebootResponse(omciPkt, omciMsg)
695
696 // powercycle the ONU
697 go func() {
698 // we run this in a separate goroutine so that
699 // the RebootRequestResponse is sent to VOLTHA
700 onuLogger.WithFields(log.Fields{
701 "IntfId": o.PonPortID,
702 "SerialNumber": o.Sn(),
703 }).Debug("shutting-down-onu-for-omci-reboot")
704 _ = o.HandleShutdownONU()
705 time.Sleep(10 * time.Second)
706 onuLogger.WithFields(log.Fields{
707 "IntfId": o.PonPortID,
708 "SerialNumber": o.Sn(),
709 }).Debug("power-on-onu-for-omci-reboot")
710 _ = o.HandlePowerOnONU()
711 }()
712 case omci.TestRequestType:
713
714 // Test message is special, it requires sending two packets:
715 // first packet: TestResponse, says whether test was started successully, handled by omci-sim
716 // second packet, TestResult, reports the result of running the self-test
717 // TestResult can come some time after a TestResponse
718 // TODO: Implement some delay between the TestResponse and the TestResult
719 isTest, err := omcilib.IsTestRequest(msg.OmciMsg.Pkt)
720 if (err == nil) && (isTest) {
721 if sendErr := o.sendTestResult(msg, stream); sendErr != nil {
722 onuLogger.WithFields(log.Fields{
723 "IntfId": o.PonPortID,
724 "SerialNumber": o.Sn(),
725 "omciPacket": msg.OmciMsg.Pkt,
726 "msg": msg,
727 "err": sendErr,
728 }).Error("send-TestResult-indication-failed")
729 }
730 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800731 default:
732 log.WithFields(log.Fields{
733 "omciMsgType": omciMsg.MessageType,
734 "transCorrId": omciMsg.TransactionID,
735 "IntfId": o.PonPortID,
736 "SerialNumber": o.Sn(),
737 }).Warnf("OMCI-message-not-supported")
738 }
739
740 if responsePkt != nil {
741 if err := o.sendOmciIndication(responsePkt, omciMsg.TransactionID, stream); err != nil {
742 onuLogger.WithFields(log.Fields{
743 "IntfId": o.PonPortID,
744 "SerialNumber": o.Sn(),
745 "omciPacket": responsePkt,
746 "omciMsgType": omciMsg.MessageType,
747 "transCorrId": omciMsg.TransactionID,
748 }).Errorf("failed-to-send-omci-message: %v", err)
749 }
750 }
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700751
Pragya Arya324337e2020-02-20 14:35:08 +0530752 o.publishOmciEvent(msg)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800753}
Pragya Arya324337e2020-02-20 14:35:08 +0530754
Matteo Scandolof9d43412021-01-12 11:11:34 -0800755// sendOmciIndication takes an OMCI packet and sends it up to VOLTHA
756func (o *Onu) sendOmciIndication(responsePkt []byte, txId uint16, stream bbsim.Stream) error {
757 indication := &openolt.Indication_OmciInd{
758 OmciInd: &openolt.OmciIndication{
759 IntfId: o.PonPortID,
760 OnuId: o.ID,
761 Pkt: responsePkt,
762 },
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700763 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800764 if err := stream.Send(&openolt.Indication{Data: indication}); err != nil {
765 return fmt.Errorf("failed-to-send-omci-message: %v", err)
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700766 }
767 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700768 "IntfId": o.PonPortID,
Matteo Scandolo27428702019-10-11 16:21:16 -0700769 "SerialNumber": o.Sn(),
Matteo Scandolof9d43412021-01-12 11:11:34 -0800770 "omciPacket": indication.OmciInd.Pkt,
771 "transCorrId": txId,
772 }).Trace("omci-message-sent")
773 return nil
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700774}
775
Matteo Scandolo27428702019-10-11 16:21:16 -0700776func (o *Onu) storePortNumber(portNo uint32) {
Matteo Scandolo813402b2019-10-23 19:24:52 -0700777 // NOTE this needed only as long as we don't support multiple UNIs
Matteo Scandolo27428702019-10-11 16:21:16 -0700778 // we need to add support for multiple UNIs
779 // the action plan is:
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700780 // - refactor the omcisim-sim library to use https://github.com/cboling/omci instead of canned messages
Matteo Scandolo27428702019-10-11 16:21:16 -0700781 // - change the library so that it reports a single UNI and remove this workaroung
782 // - add support for multiple UNIs in BBSim
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700783 if o.PortNo == 0 || portNo < o.PortNo {
Matteo Scandolo813402b2019-10-23 19:24:52 -0700784 onuLogger.WithFields(log.Fields{
785 "IntfId": o.PonPortID,
786 "OnuId": o.ID,
787 "SerialNumber": o.Sn(),
788 "OnuPortNo": o.PortNo,
789 "FlowPortNo": portNo,
790 }).Debug("Storing ONU portNo")
Matteo Scandolo27428702019-10-11 16:21:16 -0700791 o.PortNo = portNo
792 }
793}
794
William Kurkian0418bc82019-11-06 12:16:24 -0500795func (o *Onu) SetID(id uint32) {
Matteo Scandolo583f17d2020-02-13 10:35:17 -0800796 onuLogger.WithFields(log.Fields{
797 "IntfId": o.PonPortID,
798 "OnuId": id,
799 "SerialNumber": o.Sn(),
800 }).Debug("Storing OnuId ")
William Kurkian0418bc82019-11-06 12:16:24 -0500801 o.ID = id
802}
803
Matteo Scandolof9d43412021-01-12 11:11:34 -0800804func (o *Onu) handleFlowAdd(msg bbsim.OnuFlowUpdateMessage) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700805 onuLogger.WithFields(log.Fields{
Matteo Scandoloedf30c72020-09-18 18:15:28 -0700806 "Cookie": msg.Flow.Cookie,
807 "DstPort": msg.Flow.Classifier.DstPort,
808 "FlowId": msg.Flow.FlowId,
809 "FlowType": msg.Flow.FlowType,
810 "GemportId": msg.Flow.GemportId,
811 "InnerVlan": msg.Flow.Classifier.IVid,
812 "IntfId": msg.Flow.AccessIntfId,
813 "IpProto": msg.Flow.Classifier.IpProto,
814 "OnuId": msg.Flow.OnuId,
815 "OnuSn": o.Sn(),
816 "OuterVlan": msg.Flow.Classifier.OVid,
817 "PortNo": msg.Flow.PortNo,
818 "SrcPort": msg.Flow.Classifier.SrcPort,
819 "UniID": msg.Flow.UniId,
820 "ClassifierEthType": fmt.Sprintf("%x", msg.Flow.Classifier.EthType),
821 "ClassifierOPbits": msg.Flow.Classifier.OPbits,
822 "ClassifierIVid": msg.Flow.Classifier.IVid,
823 "ClassifierOVid": msg.Flow.Classifier.OVid,
Matteo Scandolo4f4ac792020-10-01 16:33:21 -0700824 "ReplicateFlow": msg.Flow.ReplicateFlow,
825 "PbitToGemport": msg.Flow.PbitToGemport,
Matteo Scandoloedf30c72020-09-18 18:15:28 -0700826 }).Debug("OLT receives FlowAdd for ONU")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700827
Matteo Scandolo813402b2019-10-23 19:24:52 -0700828 if msg.Flow.UniId != 0 {
829 // as of now BBSim only support a single UNI, so ignore everything that is not targeted to it
830 onuLogger.WithFields(log.Fields{
831 "IntfId": o.PonPortID,
832 "OnuId": o.ID,
833 "SerialNumber": o.Sn(),
834 }).Debug("Ignoring flow as it's not for the first UNI")
835 return
836 }
837
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700838 o.FlowIds = append(o.FlowIds, msg.Flow.FlowId)
Matteo Scandolo4f4ac792020-10-01 16:33:21 -0700839
840 var gemPortId uint32
841 if msg.Flow.ReplicateFlow {
842 // This means that the OLT should replicate the flow for each PBIT, for BBSim it's enough to use the
843 // first available gemport (we only need to send one packet)
844 // NOTE different TP may create different mapping between PBits and GemPorts, this may require some changes
845 gemPortId = msg.Flow.PbitToGemport[0]
846 } else {
847 // if replicateFlows is false, then the flow is carrying the correct GemPortId
848 gemPortId = uint32(msg.Flow.GemportId)
849 }
850 o.addGemPortToService(gemPortId, msg.Flow.Classifier.EthType, msg.Flow.Classifier.OVid, msg.Flow.Classifier.IVid)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700851
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700852 if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeEAPOL) && msg.Flow.Classifier.OVid == 4091 {
Matteo Scandolo27428702019-10-11 16:21:16 -0700853 // NOTE storing the PortNO, it's needed when sending PacketIndications
Matteo Scandolo813402b2019-10-23 19:24:52 -0700854 o.storePortNumber(uint32(msg.Flow.PortNo))
Matteo Scandolo4a036262020-08-17 15:56:13 -0700855
856 for _, s := range o.Services {
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700857 s.HandleAuth()
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700858 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700859 } else if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeIPv4) &&
860 msg.Flow.Classifier.SrcPort == uint32(68) &&
Matteo Scandolobd875b32020-09-18 17:46:31 -0700861 msg.Flow.Classifier.DstPort == uint32(67) {
Matteo Scandolo99f18462019-10-28 14:14:28 -0700862
Matteo Scandolo4a036262020-08-17 15:56:13 -0700863 for _, s := range o.Services {
Matteo Scandolobd875b32020-09-18 17:46:31 -0700864 s.HandleDhcp(uint8(msg.Flow.Classifier.OPbits), int(msg.Flow.Classifier.OVid))
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700865 }
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700866 }
867}
868
Matteo Scandolof9d43412021-01-12 11:11:34 -0800869func (o *Onu) handleFlowRemove(msg bbsim.OnuFlowUpdateMessage) {
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700870 onuLogger.WithFields(log.Fields{
871 "IntfId": o.PonPortID,
872 "OnuId": o.ID,
873 "SerialNumber": o.Sn(),
874 "FlowId": msg.Flow.FlowId,
875 "FlowType": msg.Flow.FlowType,
876 }).Debug("ONU receives FlowRemove")
877
878 for idx, flow := range o.FlowIds {
879 // If the gemport is found, delete it from local cache.
880 if flow == msg.Flow.FlowId {
881 o.FlowIds = append(o.FlowIds[:idx], o.FlowIds[idx+1:]...)
882 break
883 }
884 }
885
886 if len(o.FlowIds) == 0 {
887 onuLogger.WithFields(log.Fields{
888 "IntfId": o.PonPortID,
889 "OnuId": o.ID,
890 "SerialNumber": o.Sn(),
891 }).Info("Resetting GemPort")
892 o.GemPortAdded = false
893
Hardik Windlass7b3405b2020-07-08 15:10:05 +0530894 // check if ONU delete is performed and
895 // terminate the ONU's ProcessOnuMessages Go routine
896 if o.InternalState.Current() == "disabled" {
897 close(o.Channel)
898 }
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700899 }
900}
901
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700902// BBR methods
903
904func sendOmciMsg(pktBytes []byte, intfId uint32, onuId uint32, sn *openolt.SerialNumber, msgType string, client openolt.OpenoltClient) {
905 omciMsg := openolt.OmciMsg{
906 IntfId: intfId,
907 OnuId: onuId,
908 Pkt: pktBytes,
909 }
910
911 if _, err := client.OmciMsgOut(context.Background(), &omciMsg); err != nil {
912 log.WithFields(log.Fields{
913 "IntfId": intfId,
914 "OnuId": onuId,
915 "SerialNumber": common.OnuSnToString(sn),
916 "Pkt": omciMsg.Pkt,
917 }).Fatalf("Failed to send MIB Reset")
918 }
919 log.WithFields(log.Fields{
920 "IntfId": intfId,
921 "OnuId": onuId,
922 "SerialNumber": common.OnuSnToString(sn),
923 "Pkt": omciMsg.Pkt,
924 }).Tracef("Sent OMCI message %s", msgType)
925}
926
927func (onu *Onu) getNextTid(highPriority ...bool) uint16 {
928 var next uint16
929 if len(highPriority) > 0 && highPriority[0] {
930 next = onu.hpTid
931 onu.hpTid += 1
932 if onu.hpTid < 0x8000 {
933 onu.hpTid = 0x8000
934 }
935 } else {
936 next = onu.tid
937 onu.tid += 1
938 if onu.tid >= 0x8000 {
939 onu.tid = 1
940 }
941 }
942 return next
943}
944
945// TODO move this method in responders/omcisim
946func (o *Onu) StartOmci(client openolt.OpenoltClient) {
947 mibReset, _ := omcilib.CreateMibResetRequest(o.getNextTid(false))
948 sendOmciMsg(mibReset, o.PonPortID, o.ID, o.SerialNumber, "mibReset", client)
949}
950
Matteo Scandolof9d43412021-01-12 11:11:34 -0800951// handleOmciResponse is used in BBR to generate the OMCI packets the openolt-adapter would send to the device
952func (o *Onu) handleOmciResponse(msg bbsim.OmciIndicationMessage, client openolt.OpenoltClient) {
953
954 // we need to encode the packet in HEX
955 pkt := make([]byte, len(msg.OmciInd.Pkt)*2)
956 hex.Encode(pkt, msg.OmciInd.Pkt)
957 packet, omciMsg, err := omcilib.ParseOpenOltOmciPacket(pkt)
958 if err != nil {
959 log.WithFields(log.Fields{
960 "IntfId": o.PonPortID,
961 "SerialNumber": o.Sn(),
962 "omciPacket": msg.OmciInd.Pkt,
963 }).Error("BBR Cannot parse OMCI packet")
964 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700965
966 log.WithFields(log.Fields{
967 "IntfId": msg.OmciInd.IntfId,
968 "OnuId": msg.OmciInd.OnuId,
Matteo Scandolof9d43412021-01-12 11:11:34 -0800969 "OnuSn": o.Sn(),
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700970 "Pkt": msg.OmciInd.Pkt,
Matteo Scandolof9d43412021-01-12 11:11:34 -0800971 "msgType": omciMsg.MessageType,
Anand S Katti09541352020-01-29 15:54:01 +0530972 }).Trace("ONU Receives OMCI Msg")
Matteo Scandolof9d43412021-01-12 11:11:34 -0800973 switch omciMsg.MessageType {
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700974 default:
Matteo Scandolo813402b2019-10-23 19:24:52 -0700975 log.WithFields(log.Fields{
976 "IntfId": msg.OmciInd.IntfId,
977 "OnuId": msg.OmciInd.OnuId,
Matteo Scandolof9d43412021-01-12 11:11:34 -0800978 "OnuSn": o.Sn(),
Matteo Scandolo813402b2019-10-23 19:24:52 -0700979 "Pkt": msg.OmciInd.Pkt,
Matteo Scandolof9d43412021-01-12 11:11:34 -0800980 "msgType": omciMsg.MessageType,
Matteo Scandolo813402b2019-10-23 19:24:52 -0700981 }).Fatalf("unexpected frame: %v", packet)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700982 case omci.MibResetResponseType:
983 mibUpload, _ := omcilib.CreateMibUploadRequest(o.getNextTid(false))
984 sendOmciMsg(mibUpload, o.PonPortID, o.ID, o.SerialNumber, "mibUpload", client)
985 case omci.MibUploadResponseType:
986 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
987 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
988 case omci.MibUploadNextResponseType:
989 o.seqNumber++
990
991 if o.seqNumber > 290 {
992 // NOTE we are done with the MIB Upload (290 is the number of messages the omci-sim library will respond to)
993 galEnet, _ := omcilib.CreateGalEnetRequest(o.getNextTid(false))
994 sendOmciMsg(galEnet, o.PonPortID, o.ID, o.SerialNumber, "CreateGalEnetRequest", client)
995 } else {
996 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
997 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
998 }
999 case omci.CreateResponseType:
1000 // NOTE Creating a GemPort,
1001 // BBsim actually doesn't care about the values, so we can do we want with the parameters
1002 // In the same way we can create a GemPort even without setting up UNIs/TConts/...
1003 // but we need the GemPort to trigger the state change
1004
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001005 if !o.GemPortAdded {
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001006 // NOTE this sends a CreateRequestType and BBSim replies with a CreateResponseType
1007 // thus we send this request only once
1008 gemReq, _ := omcilib.CreateGemPortRequest(o.getNextTid(false))
1009 sendOmciMsg(gemReq, o.PonPortID, o.ID, o.SerialNumber, "CreateGemPortRequest", client)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001010 o.GemPortAdded = true
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001011 } else {
1012 if err := o.InternalState.Event("send_eapol_flow"); err != nil {
1013 onuLogger.WithFields(log.Fields{
1014 "OnuId": o.ID,
1015 "IntfId": o.PonPortID,
1016 "OnuSn": o.Sn(),
1017 }).Errorf("Error while transitioning ONU State %v", err)
1018 }
1019 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001020 }
1021}
1022
1023func (o *Onu) sendEapolFlow(client openolt.OpenoltClient) {
1024
1025 classifierProto := openolt.Classifier{
1026 EthType: uint32(layers.EthernetTypeEAPOL),
1027 OVid: 4091,
1028 }
1029
1030 actionProto := openolt.Action{}
1031
1032 downstreamFlow := openolt.Flow{
1033 AccessIntfId: int32(o.PonPortID),
1034 OnuId: int32(o.ID),
Matteo Scandolo813402b2019-10-23 19:24:52 -07001035 UniId: int32(0), // NOTE do not hardcode this, we need to support multiple UNIs
Matteo Scandolo4f4ac792020-10-01 16:33:21 -07001036 FlowId: uint64(o.ID),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001037 FlowType: "downstream",
1038 AllocId: int32(0),
1039 NetworkIntfId: int32(0),
1040 GemportId: int32(1), // FIXME use the same value as CreateGemPortRequest PortID, do not hardcode
1041 Classifier: &classifierProto,
1042 Action: &actionProto,
1043 Priority: int32(100),
1044 Cookie: uint64(o.ID),
1045 PortNo: uint32(o.ID), // NOTE we are using this to map an incoming packetIndication to an ONU
1046 }
1047
1048 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
1049 log.WithFields(log.Fields{
1050 "IntfId": o.PonPortID,
1051 "OnuId": o.ID,
1052 "FlowId": downstreamFlow.FlowId,
1053 "PortNo": downstreamFlow.PortNo,
1054 "SerialNumber": common.OnuSnToString(o.SerialNumber),
Matteo Scandolob0e3e622020-04-23 17:00:29 -07001055 }).Fatalf("Failed to add EAPOL Flow")
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001056 }
1057 log.WithFields(log.Fields{
1058 "IntfId": o.PonPortID,
1059 "OnuId": o.ID,
1060 "FlowId": downstreamFlow.FlowId,
1061 "PortNo": downstreamFlow.PortNo,
1062 "SerialNumber": common.OnuSnToString(o.SerialNumber),
1063 }).Info("Sent EAPOL Flow")
1064}
1065
1066func (o *Onu) sendDhcpFlow(client openolt.OpenoltClient) {
Matteo Scandolo4a036262020-08-17 15:56:13 -07001067
1068 // BBR only works with a single service (ATT HSIA)
1069 hsia := o.Services[0].(*Service)
1070
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001071 classifierProto := openolt.Classifier{
1072 EthType: uint32(layers.EthernetTypeIPv4),
1073 SrcPort: uint32(68),
1074 DstPort: uint32(67),
Matteo Scandolo4a036262020-08-17 15:56:13 -07001075 OVid: uint32(hsia.CTag),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001076 }
1077
1078 actionProto := openolt.Action{}
1079
1080 downstreamFlow := openolt.Flow{
1081 AccessIntfId: int32(o.PonPortID),
1082 OnuId: int32(o.ID),
Matteo Scandolo813402b2019-10-23 19:24:52 -07001083 UniId: int32(0), // FIXME do not hardcode this
Matteo Scandolo4f4ac792020-10-01 16:33:21 -07001084 FlowId: uint64(o.ID),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001085 FlowType: "downstream",
1086 AllocId: int32(0),
1087 NetworkIntfId: int32(0),
1088 GemportId: int32(1), // FIXME use the same value as CreateGemPortRequest PortID, do not hardcode
1089 Classifier: &classifierProto,
1090 Action: &actionProto,
1091 Priority: int32(100),
1092 Cookie: uint64(o.ID),
1093 PortNo: uint32(o.ID), // NOTE we are using this to map an incoming packetIndication to an ONU
1094 }
1095
1096 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
1097 log.WithFields(log.Fields{
1098 "IntfId": o.PonPortID,
1099 "OnuId": o.ID,
1100 "FlowId": downstreamFlow.FlowId,
1101 "PortNo": downstreamFlow.PortNo,
1102 "SerialNumber": common.OnuSnToString(o.SerialNumber),
1103 }).Fatalf("Failed to send DHCP Flow")
1104 }
1105 log.WithFields(log.Fields{
1106 "IntfId": o.PonPortID,
1107 "OnuId": o.ID,
1108 "FlowId": downstreamFlow.FlowId,
1109 "PortNo": downstreamFlow.PortNo,
1110 "SerialNumber": common.OnuSnToString(o.SerialNumber),
1111 }).Info("Sent DHCP Flow")
1112}
Pragya Arya8bdb4532020-03-02 17:08:09 +05301113
1114// DeleteFlow method search and delete flowKey from the onu flows slice
1115func (onu *Onu) DeleteFlow(key FlowKey) {
1116 for pos, flowKey := range onu.Flows {
1117 if flowKey == key {
1118 // delete the flowKey by shifting all flowKeys by one
1119 onu.Flows = append(onu.Flows[:pos], onu.Flows[pos+1:]...)
1120 t := make([]FlowKey, len(onu.Flows))
1121 copy(t, onu.Flows)
1122 onu.Flows = t
1123 break
1124 }
1125 }
1126}
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301127
1128func (onu *Onu) ReDiscoverOnu() {
1129 // Wait for few seconds to be sure of the cleanup
1130 time.Sleep(5 * time.Second)
1131
1132 onuLogger.WithFields(log.Fields{
1133 "IntfId": onu.PonPortID,
1134 "OnuId": onu.ID,
1135 "OnuSn": onu.Sn(),
1136 }).Debug("Send ONU Re-Discovery")
1137
1138 // ONU Re-Discovery
1139 if err := onu.InternalState.Event("initialize"); err != nil {
1140 log.WithFields(log.Fields{
1141 "IntfId": onu.PonPortID,
1142 "OnuSn": onu.Sn(),
1143 "OnuId": onu.ID,
1144 }).Infof("Failed to transition ONU to initialized state: %s", err.Error())
1145 }
1146
1147 if err := onu.InternalState.Event("discover"); err != nil {
1148 log.WithFields(log.Fields{
1149 "IntfId": onu.PonPortID,
1150 "OnuSn": onu.Sn(),
1151 "OnuId": onu.ID,
1152 }).Infof("Failed to transition ONU to discovered state: %s", err.Error())
1153 }
1154}
Matteo Scandolo4a036262020-08-17 15:56:13 -07001155
1156func (onu *Onu) addGemPortToService(gemport uint32, ethType uint32, oVlan uint32, iVlan uint32) {
1157 for _, s := range onu.Services {
1158 if service, ok := s.(*Service); ok {
1159 // EAPOL is a strange case, as packets are untagged
1160 // but we assume we will have a single service requiring EAPOL
1161 if ethType == uint32(layers.EthernetTypeEAPOL) && service.NeedsEapol {
1162 service.GemPort = gemport
1163 }
1164
1165 // For DHCP services we single tag the outgoing packets,
1166 // thus the flow only contains the CTag and we can use that to match the service
1167 if ethType == uint32(layers.EthernetTypeIPv4) && service.NeedsDhcp && service.CTag == int(oVlan) {
1168 service.GemPort = gemport
1169 }
1170
1171 // for dataplane services match both C and S tags
1172 if service.CTag == int(iVlan) && service.STag == int(oVlan) {
1173 service.GemPort = gemport
1174 }
1175 }
1176 }
1177}
1178
1179func (onu *Onu) findServiceByMacAddress(macAddress net.HardwareAddr) (*Service, error) {
1180 for _, s := range onu.Services {
1181 service := s.(*Service)
1182 if service.HwAddress.String() == macAddress.String() {
1183 return service, nil
1184 }
1185 }
1186 return nil, fmt.Errorf("cannot-find-service-with-mac-address-%s", macAddress.String())
1187}