blob: 9bfe805aad5ae77a0f569837307bf665d9b00beb [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
Girish Gowdrae2683102021-03-05 08:24:26 -0800643 var errResp error
Matteo Scandolof9d43412021-01-12 11:11:34 -0800644 switch omciMsg.MessageType {
645 case omci.MibResetRequestType:
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800646 onuLogger.WithFields(log.Fields{
647 "IntfId": o.PonPortID,
648 "OnuId": o.ID,
649 "SerialNumber": o.Sn(),
650 }).Debug("received-mib-reset-request-resetting-mds")
Girish Gowdrae2683102021-03-05 08:24:26 -0800651 if responsePkt, errResp = omcilib.CreateMibResetResponse(omciMsg.TransactionID); errResp == nil {
652 o.MibDataSync = 0
653 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800654 case omci.MibUploadRequestType:
655 responsePkt, _ = omcilib.CreateMibUploadResponse(omciMsg.TransactionID)
656 case omci.MibUploadNextRequestType:
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800657 responsePkt, _ = omcilib.CreateMibUploadNextResponse(omciPkt, omciMsg, o.MibDataSync)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800658 case omci.GetRequestType:
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800659 responsePkt, _ = omcilib.CreateGetResponse(omciPkt, omciMsg, o.SerialNumber, o.MibDataSync)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800660 case omci.SetRequestType:
Girish Gowdrae2683102021-03-05 08:24:26 -0800661 if responsePkt, errResp = omcilib.CreateSetResponse(omciPkt, omciMsg); errResp == nil {
662 o.MibDataSync++
663 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800664
665 msgObj, _ := omcilib.ParseSetRequest(omciPkt)
666 switch msgObj.EntityClass {
667 case me.PhysicalPathTerminationPointEthernetUniClassID:
668 // if we're Setting a PPTP state
669 // we need to send the appropriate alarm
670
671 if msgObj.EntityInstance == 257 {
672 // for now we're only caring about the first UNI
673 // NOTE that the EntityID for the UNI port is for now hardcoded in
674 // omci/mibpackets.go where the PhysicalPathTerminationPointEthernetUni
675 // are reported during the MIB Upload sequence
676 adminState := msgObj.Attributes["AdministrativeState"].(uint8)
677 msg := bbsim.Message{
678 Type: bbsim.UniStatusAlarm,
679 Data: bbsim.UniStatusAlarmMessage{
680 OnuSN: o.SerialNumber,
681 OnuID: o.ID,
682 AdminState: adminState,
683 EntityID: msgObj.EntityInstance,
684 },
685 }
686 o.Channel <- msg
687 }
688 }
689 case omci.CreateRequestType:
Girish Gowdrae2683102021-03-05 08:24:26 -0800690 if responsePkt, errResp = omcilib.CreateCreateResponse(omciPkt, omciMsg); errResp == nil {
691 o.MibDataSync++
692 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800693 case omci.DeleteRequestType:
Girish Gowdrae2683102021-03-05 08:24:26 -0800694 if responsePkt, errResp = omcilib.CreateDeleteResponse(omciPkt, omciMsg); errResp == nil {
695 o.MibDataSync++
696 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800697 case omci.RebootRequestType:
698
699 responsePkt, _ = omcilib.CreateRebootResponse(omciPkt, omciMsg)
700
701 // powercycle the ONU
702 go func() {
703 // we run this in a separate goroutine so that
704 // the RebootRequestResponse is sent to VOLTHA
705 onuLogger.WithFields(log.Fields{
706 "IntfId": o.PonPortID,
707 "SerialNumber": o.Sn(),
708 }).Debug("shutting-down-onu-for-omci-reboot")
709 _ = o.HandleShutdownONU()
710 time.Sleep(10 * time.Second)
711 onuLogger.WithFields(log.Fields{
712 "IntfId": o.PonPortID,
713 "SerialNumber": o.Sn(),
714 }).Debug("power-on-onu-for-omci-reboot")
715 _ = o.HandlePowerOnONU()
716 }()
717 case omci.TestRequestType:
718
719 // Test message is special, it requires sending two packets:
720 // first packet: TestResponse, says whether test was started successully, handled by omci-sim
721 // second packet, TestResult, reports the result of running the self-test
722 // TestResult can come some time after a TestResponse
723 // TODO: Implement some delay between the TestResponse and the TestResult
724 isTest, err := omcilib.IsTestRequest(msg.OmciMsg.Pkt)
725 if (err == nil) && (isTest) {
726 if sendErr := o.sendTestResult(msg, stream); sendErr != nil {
727 onuLogger.WithFields(log.Fields{
728 "IntfId": o.PonPortID,
729 "SerialNumber": o.Sn(),
730 "omciPacket": msg.OmciMsg.Pkt,
731 "msg": msg,
732 "err": sendErr,
733 }).Error("send-TestResult-indication-failed")
734 }
735 }
Girish Gowdraa539f522021-02-15 23:00:45 -0800736 case omci.SynchronizeTimeRequestType:
737 // MDS counter increment is not required for this message type
738 responsePkt, _ = omcilib.CreateSyncTimeResponse(omciPkt, omciMsg)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800739 default:
740 log.WithFields(log.Fields{
741 "omciMsgType": omciMsg.MessageType,
742 "transCorrId": omciMsg.TransactionID,
743 "IntfId": o.PonPortID,
744 "SerialNumber": o.Sn(),
745 }).Warnf("OMCI-message-not-supported")
746 }
747
748 if responsePkt != nil {
749 if err := o.sendOmciIndication(responsePkt, omciMsg.TransactionID, stream); err != nil {
750 onuLogger.WithFields(log.Fields{
751 "IntfId": o.PonPortID,
752 "SerialNumber": o.Sn(),
753 "omciPacket": responsePkt,
754 "omciMsgType": omciMsg.MessageType,
755 "transCorrId": omciMsg.TransactionID,
756 }).Errorf("failed-to-send-omci-message: %v", err)
757 }
758 }
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700759
Pragya Arya324337e2020-02-20 14:35:08 +0530760 o.publishOmciEvent(msg)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800761}
Pragya Arya324337e2020-02-20 14:35:08 +0530762
Matteo Scandolof9d43412021-01-12 11:11:34 -0800763// sendOmciIndication takes an OMCI packet and sends it up to VOLTHA
764func (o *Onu) sendOmciIndication(responsePkt []byte, txId uint16, stream bbsim.Stream) error {
765 indication := &openolt.Indication_OmciInd{
766 OmciInd: &openolt.OmciIndication{
767 IntfId: o.PonPortID,
768 OnuId: o.ID,
769 Pkt: responsePkt,
770 },
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700771 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800772 if err := stream.Send(&openolt.Indication{Data: indication}); err != nil {
773 return fmt.Errorf("failed-to-send-omci-message: %v", err)
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700774 }
775 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700776 "IntfId": o.PonPortID,
Matteo Scandolo27428702019-10-11 16:21:16 -0700777 "SerialNumber": o.Sn(),
Matteo Scandolof9d43412021-01-12 11:11:34 -0800778 "omciPacket": indication.OmciInd.Pkt,
779 "transCorrId": txId,
780 }).Trace("omci-message-sent")
781 return nil
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700782}
783
Matteo Scandolo27428702019-10-11 16:21:16 -0700784func (o *Onu) storePortNumber(portNo uint32) {
Matteo Scandolo813402b2019-10-23 19:24:52 -0700785 // NOTE this needed only as long as we don't support multiple UNIs
Matteo Scandolo27428702019-10-11 16:21:16 -0700786 // we need to add support for multiple UNIs
787 // the action plan is:
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700788 // - refactor the omcisim-sim library to use https://github.com/cboling/omci instead of canned messages
Matteo Scandolo27428702019-10-11 16:21:16 -0700789 // - change the library so that it reports a single UNI and remove this workaroung
790 // - add support for multiple UNIs in BBSim
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700791 if o.PortNo == 0 || portNo < o.PortNo {
Matteo Scandolo813402b2019-10-23 19:24:52 -0700792 onuLogger.WithFields(log.Fields{
793 "IntfId": o.PonPortID,
794 "OnuId": o.ID,
795 "SerialNumber": o.Sn(),
796 "OnuPortNo": o.PortNo,
797 "FlowPortNo": portNo,
798 }).Debug("Storing ONU portNo")
Matteo Scandolo27428702019-10-11 16:21:16 -0700799 o.PortNo = portNo
800 }
801}
802
William Kurkian0418bc82019-11-06 12:16:24 -0500803func (o *Onu) SetID(id uint32) {
Matteo Scandolo583f17d2020-02-13 10:35:17 -0800804 onuLogger.WithFields(log.Fields{
805 "IntfId": o.PonPortID,
806 "OnuId": id,
807 "SerialNumber": o.Sn(),
808 }).Debug("Storing OnuId ")
William Kurkian0418bc82019-11-06 12:16:24 -0500809 o.ID = id
810}
811
Matteo Scandolof9d43412021-01-12 11:11:34 -0800812func (o *Onu) handleFlowAdd(msg bbsim.OnuFlowUpdateMessage) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700813 onuLogger.WithFields(log.Fields{
Matteo Scandoloedf30c72020-09-18 18:15:28 -0700814 "Cookie": msg.Flow.Cookie,
815 "DstPort": msg.Flow.Classifier.DstPort,
816 "FlowId": msg.Flow.FlowId,
817 "FlowType": msg.Flow.FlowType,
818 "GemportId": msg.Flow.GemportId,
819 "InnerVlan": msg.Flow.Classifier.IVid,
820 "IntfId": msg.Flow.AccessIntfId,
821 "IpProto": msg.Flow.Classifier.IpProto,
822 "OnuId": msg.Flow.OnuId,
823 "OnuSn": o.Sn(),
824 "OuterVlan": msg.Flow.Classifier.OVid,
825 "PortNo": msg.Flow.PortNo,
826 "SrcPort": msg.Flow.Classifier.SrcPort,
827 "UniID": msg.Flow.UniId,
828 "ClassifierEthType": fmt.Sprintf("%x", msg.Flow.Classifier.EthType),
829 "ClassifierOPbits": msg.Flow.Classifier.OPbits,
830 "ClassifierIVid": msg.Flow.Classifier.IVid,
831 "ClassifierOVid": msg.Flow.Classifier.OVid,
Matteo Scandolo4f4ac792020-10-01 16:33:21 -0700832 "ReplicateFlow": msg.Flow.ReplicateFlow,
833 "PbitToGemport": msg.Flow.PbitToGemport,
Matteo Scandoloedf30c72020-09-18 18:15:28 -0700834 }).Debug("OLT receives FlowAdd for ONU")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700835
Matteo Scandolo813402b2019-10-23 19:24:52 -0700836 if msg.Flow.UniId != 0 {
837 // as of now BBSim only support a single UNI, so ignore everything that is not targeted to it
838 onuLogger.WithFields(log.Fields{
839 "IntfId": o.PonPortID,
840 "OnuId": o.ID,
841 "SerialNumber": o.Sn(),
842 }).Debug("Ignoring flow as it's not for the first UNI")
843 return
844 }
845
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700846 o.FlowIds = append(o.FlowIds, msg.Flow.FlowId)
Matteo Scandolo4f4ac792020-10-01 16:33:21 -0700847
848 var gemPortId uint32
849 if msg.Flow.ReplicateFlow {
850 // This means that the OLT should replicate the flow for each PBIT, for BBSim it's enough to use the
851 // first available gemport (we only need to send one packet)
852 // NOTE different TP may create different mapping between PBits and GemPorts, this may require some changes
853 gemPortId = msg.Flow.PbitToGemport[0]
854 } else {
855 // if replicateFlows is false, then the flow is carrying the correct GemPortId
856 gemPortId = uint32(msg.Flow.GemportId)
857 }
858 o.addGemPortToService(gemPortId, msg.Flow.Classifier.EthType, msg.Flow.Classifier.OVid, msg.Flow.Classifier.IVid)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700859
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700860 if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeEAPOL) && msg.Flow.Classifier.OVid == 4091 {
Matteo Scandolo27428702019-10-11 16:21:16 -0700861 // NOTE storing the PortNO, it's needed when sending PacketIndications
Matteo Scandolo813402b2019-10-23 19:24:52 -0700862 o.storePortNumber(uint32(msg.Flow.PortNo))
Matteo Scandolo4a036262020-08-17 15:56:13 -0700863
864 for _, s := range o.Services {
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700865 s.HandleAuth()
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700866 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700867 } else if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeIPv4) &&
868 msg.Flow.Classifier.SrcPort == uint32(68) &&
Matteo Scandolobd875b32020-09-18 17:46:31 -0700869 msg.Flow.Classifier.DstPort == uint32(67) {
Matteo Scandolo99f18462019-10-28 14:14:28 -0700870
Matteo Scandolo4a036262020-08-17 15:56:13 -0700871 for _, s := range o.Services {
Matteo Scandolobd875b32020-09-18 17:46:31 -0700872 s.HandleDhcp(uint8(msg.Flow.Classifier.OPbits), int(msg.Flow.Classifier.OVid))
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700873 }
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700874 }
875}
876
Matteo Scandolof9d43412021-01-12 11:11:34 -0800877func (o *Onu) handleFlowRemove(msg bbsim.OnuFlowUpdateMessage) {
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700878 onuLogger.WithFields(log.Fields{
879 "IntfId": o.PonPortID,
880 "OnuId": o.ID,
881 "SerialNumber": o.Sn(),
882 "FlowId": msg.Flow.FlowId,
883 "FlowType": msg.Flow.FlowType,
884 }).Debug("ONU receives FlowRemove")
885
886 for idx, flow := range o.FlowIds {
887 // If the gemport is found, delete it from local cache.
888 if flow == msg.Flow.FlowId {
889 o.FlowIds = append(o.FlowIds[:idx], o.FlowIds[idx+1:]...)
890 break
891 }
892 }
893
894 if len(o.FlowIds) == 0 {
895 onuLogger.WithFields(log.Fields{
896 "IntfId": o.PonPortID,
897 "OnuId": o.ID,
898 "SerialNumber": o.Sn(),
899 }).Info("Resetting GemPort")
900 o.GemPortAdded = false
901
Hardik Windlass7b3405b2020-07-08 15:10:05 +0530902 // check if ONU delete is performed and
903 // terminate the ONU's ProcessOnuMessages Go routine
904 if o.InternalState.Current() == "disabled" {
905 close(o.Channel)
906 }
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700907 }
908}
909
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700910// BBR methods
911
912func sendOmciMsg(pktBytes []byte, intfId uint32, onuId uint32, sn *openolt.SerialNumber, msgType string, client openolt.OpenoltClient) {
913 omciMsg := openolt.OmciMsg{
914 IntfId: intfId,
915 OnuId: onuId,
916 Pkt: pktBytes,
917 }
918
919 if _, err := client.OmciMsgOut(context.Background(), &omciMsg); err != nil {
920 log.WithFields(log.Fields{
921 "IntfId": intfId,
922 "OnuId": onuId,
923 "SerialNumber": common.OnuSnToString(sn),
924 "Pkt": omciMsg.Pkt,
925 }).Fatalf("Failed to send MIB Reset")
926 }
927 log.WithFields(log.Fields{
928 "IntfId": intfId,
929 "OnuId": onuId,
930 "SerialNumber": common.OnuSnToString(sn),
931 "Pkt": omciMsg.Pkt,
932 }).Tracef("Sent OMCI message %s", msgType)
933}
934
935func (onu *Onu) getNextTid(highPriority ...bool) uint16 {
936 var next uint16
937 if len(highPriority) > 0 && highPriority[0] {
938 next = onu.hpTid
939 onu.hpTid += 1
940 if onu.hpTid < 0x8000 {
941 onu.hpTid = 0x8000
942 }
943 } else {
944 next = onu.tid
945 onu.tid += 1
946 if onu.tid >= 0x8000 {
947 onu.tid = 1
948 }
949 }
950 return next
951}
952
953// TODO move this method in responders/omcisim
954func (o *Onu) StartOmci(client openolt.OpenoltClient) {
955 mibReset, _ := omcilib.CreateMibResetRequest(o.getNextTid(false))
956 sendOmciMsg(mibReset, o.PonPortID, o.ID, o.SerialNumber, "mibReset", client)
957}
958
Matteo Scandolof9d43412021-01-12 11:11:34 -0800959// handleOmciResponse is used in BBR to generate the OMCI packets the openolt-adapter would send to the device
960func (o *Onu) handleOmciResponse(msg bbsim.OmciIndicationMessage, client openolt.OpenoltClient) {
961
962 // we need to encode the packet in HEX
963 pkt := make([]byte, len(msg.OmciInd.Pkt)*2)
964 hex.Encode(pkt, msg.OmciInd.Pkt)
965 packet, omciMsg, err := omcilib.ParseOpenOltOmciPacket(pkt)
966 if err != nil {
967 log.WithFields(log.Fields{
968 "IntfId": o.PonPortID,
969 "SerialNumber": o.Sn(),
970 "omciPacket": msg.OmciInd.Pkt,
971 }).Error("BBR Cannot parse OMCI packet")
972 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700973
974 log.WithFields(log.Fields{
975 "IntfId": msg.OmciInd.IntfId,
976 "OnuId": msg.OmciInd.OnuId,
Matteo Scandolof9d43412021-01-12 11:11:34 -0800977 "OnuSn": o.Sn(),
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700978 "Pkt": msg.OmciInd.Pkt,
Matteo Scandolof9d43412021-01-12 11:11:34 -0800979 "msgType": omciMsg.MessageType,
Anand S Katti09541352020-01-29 15:54:01 +0530980 }).Trace("ONU Receives OMCI Msg")
Matteo Scandolof9d43412021-01-12 11:11:34 -0800981 switch omciMsg.MessageType {
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700982 default:
Matteo Scandolo813402b2019-10-23 19:24:52 -0700983 log.WithFields(log.Fields{
984 "IntfId": msg.OmciInd.IntfId,
985 "OnuId": msg.OmciInd.OnuId,
Matteo Scandolof9d43412021-01-12 11:11:34 -0800986 "OnuSn": o.Sn(),
Matteo Scandolo813402b2019-10-23 19:24:52 -0700987 "Pkt": msg.OmciInd.Pkt,
Matteo Scandolof9d43412021-01-12 11:11:34 -0800988 "msgType": omciMsg.MessageType,
Matteo Scandolo813402b2019-10-23 19:24:52 -0700989 }).Fatalf("unexpected frame: %v", packet)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700990 case omci.MibResetResponseType:
991 mibUpload, _ := omcilib.CreateMibUploadRequest(o.getNextTid(false))
992 sendOmciMsg(mibUpload, o.PonPortID, o.ID, o.SerialNumber, "mibUpload", client)
993 case omci.MibUploadResponseType:
994 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
995 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
996 case omci.MibUploadNextResponseType:
997 o.seqNumber++
998
999 if o.seqNumber > 290 {
1000 // NOTE we are done with the MIB Upload (290 is the number of messages the omci-sim library will respond to)
1001 galEnet, _ := omcilib.CreateGalEnetRequest(o.getNextTid(false))
1002 sendOmciMsg(galEnet, o.PonPortID, o.ID, o.SerialNumber, "CreateGalEnetRequest", client)
1003 } else {
1004 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
1005 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
1006 }
1007 case omci.CreateResponseType:
1008 // NOTE Creating a GemPort,
1009 // BBsim actually doesn't care about the values, so we can do we want with the parameters
1010 // In the same way we can create a GemPort even without setting up UNIs/TConts/...
1011 // but we need the GemPort to trigger the state change
1012
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001013 if !o.GemPortAdded {
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001014 // NOTE this sends a CreateRequestType and BBSim replies with a CreateResponseType
1015 // thus we send this request only once
1016 gemReq, _ := omcilib.CreateGemPortRequest(o.getNextTid(false))
1017 sendOmciMsg(gemReq, o.PonPortID, o.ID, o.SerialNumber, "CreateGemPortRequest", client)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001018 o.GemPortAdded = true
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001019 } else {
1020 if err := o.InternalState.Event("send_eapol_flow"); err != nil {
1021 onuLogger.WithFields(log.Fields{
1022 "OnuId": o.ID,
1023 "IntfId": o.PonPortID,
1024 "OnuSn": o.Sn(),
1025 }).Errorf("Error while transitioning ONU State %v", err)
1026 }
1027 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001028 }
1029}
1030
1031func (o *Onu) sendEapolFlow(client openolt.OpenoltClient) {
1032
1033 classifierProto := openolt.Classifier{
1034 EthType: uint32(layers.EthernetTypeEAPOL),
1035 OVid: 4091,
1036 }
1037
1038 actionProto := openolt.Action{}
1039
1040 downstreamFlow := openolt.Flow{
1041 AccessIntfId: int32(o.PonPortID),
1042 OnuId: int32(o.ID),
Matteo Scandolo813402b2019-10-23 19:24:52 -07001043 UniId: int32(0), // NOTE do not hardcode this, we need to support multiple UNIs
Matteo Scandolo4f4ac792020-10-01 16:33:21 -07001044 FlowId: uint64(o.ID),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001045 FlowType: "downstream",
1046 AllocId: int32(0),
1047 NetworkIntfId: int32(0),
1048 GemportId: int32(1), // FIXME use the same value as CreateGemPortRequest PortID, do not hardcode
1049 Classifier: &classifierProto,
1050 Action: &actionProto,
1051 Priority: int32(100),
1052 Cookie: uint64(o.ID),
1053 PortNo: uint32(o.ID), // NOTE we are using this to map an incoming packetIndication to an ONU
1054 }
1055
1056 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
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),
Matteo Scandolob0e3e622020-04-23 17:00:29 -07001063 }).Fatalf("Failed to add EAPOL Flow")
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001064 }
1065 log.WithFields(log.Fields{
1066 "IntfId": o.PonPortID,
1067 "OnuId": o.ID,
1068 "FlowId": downstreamFlow.FlowId,
1069 "PortNo": downstreamFlow.PortNo,
1070 "SerialNumber": common.OnuSnToString(o.SerialNumber),
1071 }).Info("Sent EAPOL Flow")
1072}
1073
1074func (o *Onu) sendDhcpFlow(client openolt.OpenoltClient) {
Matteo Scandolo4a036262020-08-17 15:56:13 -07001075
1076 // BBR only works with a single service (ATT HSIA)
1077 hsia := o.Services[0].(*Service)
1078
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001079 classifierProto := openolt.Classifier{
1080 EthType: uint32(layers.EthernetTypeIPv4),
1081 SrcPort: uint32(68),
1082 DstPort: uint32(67),
Matteo Scandolo4a036262020-08-17 15:56:13 -07001083 OVid: uint32(hsia.CTag),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001084 }
1085
1086 actionProto := openolt.Action{}
1087
1088 downstreamFlow := openolt.Flow{
1089 AccessIntfId: int32(o.PonPortID),
1090 OnuId: int32(o.ID),
Matteo Scandolo813402b2019-10-23 19:24:52 -07001091 UniId: int32(0), // FIXME do not hardcode this
Matteo Scandolo4f4ac792020-10-01 16:33:21 -07001092 FlowId: uint64(o.ID),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001093 FlowType: "downstream",
1094 AllocId: int32(0),
1095 NetworkIntfId: int32(0),
1096 GemportId: int32(1), // FIXME use the same value as CreateGemPortRequest PortID, do not hardcode
1097 Classifier: &classifierProto,
1098 Action: &actionProto,
1099 Priority: int32(100),
1100 Cookie: uint64(o.ID),
1101 PortNo: uint32(o.ID), // NOTE we are using this to map an incoming packetIndication to an ONU
1102 }
1103
1104 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
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 }).Fatalf("Failed to send DHCP Flow")
1112 }
1113 log.WithFields(log.Fields{
1114 "IntfId": o.PonPortID,
1115 "OnuId": o.ID,
1116 "FlowId": downstreamFlow.FlowId,
1117 "PortNo": downstreamFlow.PortNo,
1118 "SerialNumber": common.OnuSnToString(o.SerialNumber),
1119 }).Info("Sent DHCP Flow")
1120}
Pragya Arya8bdb4532020-03-02 17:08:09 +05301121
1122// DeleteFlow method search and delete flowKey from the onu flows slice
1123func (onu *Onu) DeleteFlow(key FlowKey) {
1124 for pos, flowKey := range onu.Flows {
1125 if flowKey == key {
1126 // delete the flowKey by shifting all flowKeys by one
1127 onu.Flows = append(onu.Flows[:pos], onu.Flows[pos+1:]...)
1128 t := make([]FlowKey, len(onu.Flows))
1129 copy(t, onu.Flows)
1130 onu.Flows = t
1131 break
1132 }
1133 }
1134}
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301135
1136func (onu *Onu) ReDiscoverOnu() {
1137 // Wait for few seconds to be sure of the cleanup
1138 time.Sleep(5 * time.Second)
1139
1140 onuLogger.WithFields(log.Fields{
1141 "IntfId": onu.PonPortID,
1142 "OnuId": onu.ID,
1143 "OnuSn": onu.Sn(),
1144 }).Debug("Send ONU Re-Discovery")
1145
1146 // ONU Re-Discovery
1147 if err := onu.InternalState.Event("initialize"); 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 initialized state: %s", err.Error())
1153 }
1154
1155 if err := onu.InternalState.Event("discover"); err != nil {
1156 log.WithFields(log.Fields{
1157 "IntfId": onu.PonPortID,
1158 "OnuSn": onu.Sn(),
1159 "OnuId": onu.ID,
1160 }).Infof("Failed to transition ONU to discovered state: %s", err.Error())
1161 }
1162}
Matteo Scandolo4a036262020-08-17 15:56:13 -07001163
1164func (onu *Onu) addGemPortToService(gemport uint32, ethType uint32, oVlan uint32, iVlan uint32) {
1165 for _, s := range onu.Services {
1166 if service, ok := s.(*Service); ok {
1167 // EAPOL is a strange case, as packets are untagged
1168 // but we assume we will have a single service requiring EAPOL
1169 if ethType == uint32(layers.EthernetTypeEAPOL) && service.NeedsEapol {
1170 service.GemPort = gemport
1171 }
1172
1173 // For DHCP services we single tag the outgoing packets,
1174 // thus the flow only contains the CTag and we can use that to match the service
1175 if ethType == uint32(layers.EthernetTypeIPv4) && service.NeedsDhcp && service.CTag == int(oVlan) {
1176 service.GemPort = gemport
1177 }
1178
1179 // for dataplane services match both C and S tags
1180 if service.CTag == int(iVlan) && service.STag == int(oVlan) {
1181 service.GemPort = gemport
1182 }
1183 }
1184 }
1185}
1186
1187func (onu *Onu) findServiceByMacAddress(macAddress net.HardwareAddr) (*Service, error) {
1188 for _, s := range onu.Services {
1189 service := s.(*Service)
1190 if service.HwAddress.String() == macAddress.String() {
1191 return service, nil
1192 }
1193 }
1194 return nil, fmt.Errorf("cannot-find-service-with-mac-address-%s", macAddress.String())
1195}