blob: a73841c1a3bf224051c8684ca112137defd4b7e2 [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"
Holger Hildebrandtc10bab12021-04-27 09:23:48 +000023 "sync"
24
Matteo Scandolof9d43412021-01-12 11:11:34 -080025 pb "github.com/opencord/bbsim/api/bbsim"
26 "github.com/opencord/bbsim/internal/bbsim/alarmsim"
Holger Hildebrandtc10bab12021-04-27 09:23:48 +000027
28 "net"
29 "strconv"
30 "time"
Himani Chawla13b1ee02021-03-15 01:43:53 +053031
Matteo Scandolo4a036262020-08-17 15:56:13 -070032 "github.com/opencord/bbsim/internal/bbsim/packetHandlers"
33 "github.com/opencord/bbsim/internal/bbsim/responders/dhcp"
34 "github.com/opencord/bbsim/internal/bbsim/responders/eapol"
Matteo Scandolo4b077aa2021-02-16 17:33:37 -080035 bbsim "github.com/opencord/bbsim/internal/bbsim/types"
36 me "github.com/opencord/omci-lib-go/generated"
Zdravko Bozakov681364d2019-11-10 14:28:46 +010037
Matteo Scandolo3bc73742019-08-20 14:04:04 -070038 "github.com/google/gopacket/layers"
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -070039 "github.com/jpillora/backoff"
Matteo Scandolo4747d292019-08-05 11:50:18 -070040 "github.com/looplab/fsm"
Matteo Scandolo40e067f2019-10-16 16:59:41 -070041 "github.com/opencord/bbsim/internal/common"
42 omcilib "github.com/opencord/bbsim/internal/common/omci"
Matteo Scandolof9d43412021-01-12 11:11:34 -080043 "github.com/opencord/omci-lib-go"
Matteo Scandolo4f4ac792020-10-01 16:33:21 -070044 "github.com/opencord/voltha-protos/v4/go/openolt"
45 "github.com/opencord/voltha-protos/v4/go/tech_profile"
Matteo Scandolo4747d292019-08-05 11:50:18 -070046 log "github.com/sirupsen/logrus"
47)
48
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070049var onuLogger = log.WithFields(log.Fields{
50 "module": "ONU",
51})
52
Matteo Scandolocedde462021-03-09 17:37:16 -080053const (
Holger Hildebrandtc10bab12021-04-27 09:23:48 +000054 maxOmciMsgCounter = 10
55)
56
57const (
Matteo Scandolocedde462021-03-09 17:37:16 -080058 // ONU transitions
59 OnuTxInitialize = "initialize"
60 OnuTxDiscover = "discover"
61 OnuTxEnable = "enable"
62 OnuTxDisable = "disable"
63 OnuTxPonDisable = "pon_disable"
64 OnuTxStartImageDownload = "start_image_download"
65 OnuTxProgressImageDownload = "progress_image_download"
66 OnuTxCompleteImageDownload = "complete_image_download"
67 OnuTxFailImageDownload = "fail_image_download"
68 OnuTxActivateImage = "activate_image"
69 OnuTxCommitImage = "commit_image"
70
71 // ONU States
72 OnuStateCreated = "created"
73 OnuStateInitialized = "initialized"
74 OnuStateDiscovered = "discovered"
75 OnuStateEnabled = "enabled"
76 OnuStateDisabled = "disabled"
77 OnuStatePonDisabled = "pon_disabled"
78 OnuStateImageDownloadStarted = "image_download_started"
79 OnuStateImageDownloadInProgress = "image_download_in_progress"
80 OnuStateImageDownloadComplete = "image_download_completed"
81 OnuStateImageDownloadError = "image_download_error"
82 OnuStateImageActivated = "software_image_activated"
83 OnuStateImageCommitted = "software_image_committed"
84
85 // BBR ONU States and Transitions
86 BbrOnuTxSendEapolFlow = "send_eapol_flow"
87 BbrOnuStateEapolFlowSent = "eapol_flow_sent"
88 BbrOnuTxSendDhcpFlow = "send_dhcp_flow"
89 BbrOnuStateDhcpFlowSent = "dhcp_flow_sent"
90)
91
Pragya Arya8bdb4532020-03-02 17:08:09 +053092type FlowKey struct {
Matteo Scandolo4f4ac792020-10-01 16:33:21 -070093 ID uint64
Pragya Arya8bdb4532020-03-02 17:08:09 +053094 Direction string
95}
96
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070097type Onu struct {
Matteo Scandoloe811ae92019-12-10 17:50:14 -080098 ID uint32
99 PonPortID uint32
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700100 PonPort *PonPort
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800101 InternalState *fsm.FSM
Pragya Arya2225f202020-01-29 18:05:01 +0530102 DiscoveryRetryDelay time.Duration // this is the time between subsequent Discovery Indication
103 DiscoveryDelay time.Duration // this is the time to send the first Discovery Indication
Matteo Scandolo4a036262020-08-17 15:56:13 -0700104
105 Services []ServiceIf
106
107 Backoff *backoff.Backoff
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800108 // ONU State
Matteo Scandolo27428702019-10-11 16:21:16 -0700109 // PortNo comes with flows and it's used when sending packetIndications,
110 // There is one PortNo per UNI Port, for now we're only storing the first one
Matteo Scandolo47ef64b2020-04-20 14:16:07 -0700111 // FIXME add support for multiple UNIs (each UNI has a different PortNo)
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800112 PortNo uint32
113 Flows []FlowKey
114 FlowIds []uint64 // keep track of the flows we currently have in the ONU
Matteo Scandolo99f18462019-10-28 14:14:28 -0700115
Matteo Scandolo86e8ce62019-10-11 12:03:10 -0700116 OperState *fsm.FSM
117 SerialNumber *openolt.SerialNumber
118
Matteo Scandolof9d43412021-01-12 11:11:34 -0800119 Channel chan bbsim.Message // this Channel is to track state changes OMCI messages, EAPOL and DHCP packets
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700120
121 // OMCI params
Matteo Scandolocedde462021-03-09 17:37:16 -0800122 MibDataSync uint8
123 ImageSoftwareExpectedSections int
124 ImageSoftwareReceivedSections int
125 ActiveImageEntityId uint16
126 CommittedImageEntityId uint16
Holger Hildebrandtc10bab12021-04-27 09:23:48 +0000127 OmciResponseRate uint8
128 OmciMsgCounter uint8
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800129
130 // OMCI params (Used in BBR)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700131 tid uint16
132 hpTid uint16
133 seqNumber uint16
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700134
Anand S Katti09541352020-01-29 15:54:01 +0530135 DoneChannel chan bool // this channel is used to signal once the onu is complete (when the struct is used by BBR)
136 TrafficSchedulers *tech_profile.TrafficSchedulers
Himani Chawla13b1ee02021-03-15 01:43:53 +0530137 onuAlarmsInfoLock sync.RWMutex
138 onuAlarmsInfo map[omcilib.OnuAlarmInfoMapKey]omcilib.OnuAlarmInfo
Matteo Scandolo86e8ce62019-10-11 12:03:10 -0700139}
140
Matteo Scandolo99f18462019-10-28 14:14:28 -0700141func (o *Onu) Sn() string {
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700142 return common.OnuSnToString(o.SerialNumber)
Matteo Scandolo86e8ce62019-10-11 12:03:10 -0700143}
144
Matteo Scandolo4a036262020-08-17 15:56:13 -0700145func CreateONU(olt *OltDevice, pon *PonPort, id uint32, delay time.Duration, isMock bool) *Onu {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700146
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700147 o := Onu{
Matteo Scandolocedde462021-03-09 17:37:16 -0800148 ID: id,
149 PonPortID: pon.ID,
150 PonPort: pon,
151 PortNo: 0,
152 tid: 0x1,
153 hpTid: 0x8000,
154 seqNumber: 0,
155 DoneChannel: make(chan bool, 1),
156 DiscoveryRetryDelay: 60 * time.Second, // this is used to send OnuDiscoveryIndications until an activate call is received
157 Flows: []FlowKey{},
158 DiscoveryDelay: delay,
159 MibDataSync: 0,
160 ImageSoftwareExpectedSections: 0, // populated during OMCI StartSoftwareDownloadRequest
161 ImageSoftwareReceivedSections: 0,
162 ActiveImageEntityId: 0, // when we start the SoftwareImage with ID 0 is active and committed
163 CommittedImageEntityId: 0,
Holger Hildebrandtc10bab12021-04-27 09:23:48 +0000164 OmciResponseRate: olt.OmciResponseRate,
165 OmciMsgCounter: 0,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700166 }
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800167 o.SerialNumber = NewSN(olt.ID, pon.ID, id)
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700168 // NOTE this state machine is used to track the operational
169 // state as requested by VOLTHA
170 o.OperState = getOperStateFSM(func(e *fsm.Event) {
171 onuLogger.WithFields(log.Fields{
172 "ID": o.ID,
173 }).Debugf("Changing ONU OperState from %s to %s", e.Src, e.Dst)
174 })
Himani Chawla13b1ee02021-03-15 01:43:53 +0530175 o.onuAlarmsInfo = make(map[omcilib.OnuAlarmInfoMapKey]omcilib.OnuAlarmInfo)
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700176 // NOTE this state machine is used to activate the OMCI, EAPOL and DHCP clients
177 o.InternalState = fsm.NewFSM(
Matteo Scandolocedde462021-03-09 17:37:16 -0800178 OnuStateCreated,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700179 fsm.Events{
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700180 // DEVICE Lifecycle
Matteo Scandolocedde462021-03-09 17:37:16 -0800181 {Name: OnuTxInitialize, Src: []string{OnuStateCreated, OnuStateDisabled, OnuStatePonDisabled}, Dst: OnuStateInitialized},
182 {Name: OnuTxDiscover, Src: []string{OnuStateInitialized}, Dst: OnuStateDiscovered},
183 {Name: OnuTxEnable, Src: []string{OnuStateDiscovered, OnuStatePonDisabled}, Dst: OnuStateEnabled},
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100184 // NOTE should disabled state be different for oper_disabled (emulating an error) and admin_disabled (received a disabled call via VOLTHA)?
Matteo Scandolocedde462021-03-09 17:37:16 -0800185 {Name: OnuTxDisable, Src: []string{OnuStateEnabled, OnuStatePonDisabled, OnuStateImageActivated, OnuStateImageDownloadError, OnuStateImageCommitted}, Dst: OnuStateDisabled},
Pragya Arya6a708d62020-01-01 17:17:20 +0530186 // ONU state when PON port is disabled but ONU is power ON(more states should be added in src?)
Matteo Scandolocedde462021-03-09 17:37:16 -0800187 {Name: OnuTxPonDisable, Src: []string{OnuStateEnabled, OnuStateImageActivated, OnuStateImageDownloadError, OnuStateImageCommitted}, Dst: OnuStatePonDisabled},
188 // Software Image Download related states
189 {Name: OnuTxStartImageDownload, Src: []string{OnuStateEnabled, OnuStateImageDownloadComplete, OnuStateImageDownloadError}, Dst: OnuStateImageDownloadStarted},
190 {Name: OnuTxProgressImageDownload, Src: []string{OnuStateImageDownloadStarted}, Dst: OnuStateImageDownloadInProgress},
191 {Name: OnuTxCompleteImageDownload, Src: []string{OnuStateImageDownloadInProgress}, Dst: OnuStateImageDownloadComplete},
192 {Name: OnuTxFailImageDownload, Src: []string{OnuStateImageDownloadInProgress}, Dst: OnuStateImageDownloadError},
193 {Name: OnuTxActivateImage, Src: []string{OnuStateImageDownloadComplete}, Dst: OnuStateImageActivated},
194 {Name: OnuTxCommitImage, Src: []string{OnuStateEnabled}, Dst: OnuStateImageCommitted}, // the image is committed after a ONU reboot
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700195 // BBR States
196 // TODO add start OMCI state
Matteo Scandolocedde462021-03-09 17:37:16 -0800197 {Name: BbrOnuTxSendEapolFlow, Src: []string{OnuStateInitialized}, Dst: BbrOnuStateEapolFlowSent},
198 {Name: BbrOnuTxSendDhcpFlow, Src: []string{BbrOnuStateEapolFlowSent}, Dst: BbrOnuStateDhcpFlowSent},
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700199 },
200 fsm.Callbacks{
201 "enter_state": func(e *fsm.Event) {
202 o.logStateChange(e.Src, e.Dst)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700203 },
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700204 fmt.Sprintf("enter_%s", OnuStateInitialized): func(e *fsm.Event) {
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100205 // create new channel for ProcessOnuMessages Go routine
Matteo Scandolof9d43412021-01-12 11:11:34 -0800206 o.Channel = make(chan bbsim.Message, 2048)
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800207
Matteo Scandolocedde462021-03-09 17:37:16 -0800208 if err := o.OperState.Event(OnuTxEnable); err != nil {
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800209 onuLogger.WithFields(log.Fields{
210 "OnuId": o.ID,
211 "IntfId": o.PonPortID,
212 "OnuSn": o.Sn(),
213 }).Errorf("Cannot change ONU OperState to up: %s", err.Error())
214 }
215
Pragya Arya1cbefa42020-01-13 12:15:29 +0530216 if !isMock {
217 // start ProcessOnuMessages Go routine
Matteo Scandolo4a036262020-08-17 15:56:13 -0700218 go o.ProcessOnuMessages(olt.enableContext, olt.OpenoltStream, nil)
Pragya Arya1cbefa42020-01-13 12:15:29 +0530219 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100220 },
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700221 fmt.Sprintf("enter_%s", OnuStateDiscovered): func(e *fsm.Event) {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800222 msg := bbsim.Message{
223 Type: bbsim.OnuDiscIndication,
224 Data: bbsim.OnuDiscIndicationMessage{
225 OperState: bbsim.UP,
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100226 },
227 }
228 o.Channel <- msg
229 },
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700230 fmt.Sprintf("enter_%s", OnuStateEnabled): func(event *fsm.Event) {
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800231
232 if used, sn := o.PonPort.isOnuIdAllocated(o.ID); used {
233 onuLogger.WithFields(log.Fields{
234 "IntfId": o.PonPortID,
235 "OnuId": o.ID,
236 "SerialNumber": o.Sn(),
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700237 }).Errorf("onu-id-duplicated-with-%s", common.OnuSnToString(sn))
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800238 return
239 } else {
240 o.PonPort.storeOnuId(o.ID, o.SerialNumber)
241 }
242
Matteo Scandolof9d43412021-01-12 11:11:34 -0800243 msg := bbsim.Message{
244 Type: bbsim.OnuIndication,
245 Data: bbsim.OnuIndicationMessage{
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700246 OnuSN: o.SerialNumber,
247 PonPortID: o.PonPortID,
Matteo Scandolof9d43412021-01-12 11:11:34 -0800248 OperState: bbsim.UP,
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700249 },
250 }
251 o.Channel <- msg
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700252
253 // Once the ONU is enabled start listening for packets
254 for _, s := range o.Services {
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700255 s.Initialize(o.PonPort.Olt.OpenoltStream)
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700256 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700257 },
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700258 fmt.Sprintf("enter_%s", OnuStateDisabled): func(event *fsm.Event) {
Matteo Scandolo47ef64b2020-04-20 14:16:07 -0700259
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700260 o.cleanupOnuState()
Matteo Scandolo47ef64b2020-04-20 14:16:07 -0700261
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700262 // set the OperState to disabled
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800263 if err := o.OperState.Event("disable"); err != nil {
264 onuLogger.WithFields(log.Fields{
265 "OnuId": o.ID,
266 "IntfId": o.PonPortID,
267 "OnuSn": o.Sn(),
268 }).Errorf("Cannot change ONU OperState to down: %s", err.Error())
269 }
Matteo Scandolo47ef64b2020-04-20 14:16:07 -0700270 // send the OnuIndication DOWN event
Matteo Scandolof9d43412021-01-12 11:11:34 -0800271 msg := bbsim.Message{
272 Type: bbsim.OnuIndication,
273 Data: bbsim.OnuIndicationMessage{
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700274 OnuSN: o.SerialNumber,
275 PonPortID: o.PonPortID,
Matteo Scandolof9d43412021-01-12 11:11:34 -0800276 OperState: bbsim.DOWN,
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700277 },
278 }
279 o.Channel <- msg
Hardik Windlass7b3405b2020-07-08 15:10:05 +0530280
281 // verify all the flows removes are handled and
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100282 // terminate the ONU's ProcessOnuMessages Go routine
Hardik Windlass7b3405b2020-07-08 15:10:05 +0530283 if len(o.FlowIds) == 0 {
284 close(o.Channel)
285 }
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700286
287 for _, s := range o.Services {
288 s.Disable()
289 }
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700290
291 },
292 fmt.Sprintf("enter_%s", OnuStatePonDisabled): func(event *fsm.Event) {
293 o.cleanupOnuState()
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700294 },
Matteo Scandolo4a036262020-08-17 15:56:13 -0700295 // BBR states
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700296 fmt.Sprintf("enter_%s", BbrOnuStateEapolFlowSent): func(e *fsm.Event) {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800297 msg := bbsim.Message{
298 Type: bbsim.SendEapolFlow,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700299 }
300 o.Channel <- msg
301 },
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700302 fmt.Sprintf("enter_%s", BbrOnuStateDhcpFlowSent): func(e *fsm.Event) {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800303 msg := bbsim.Message{
304 Type: bbsim.SendDhcpFlow,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700305 }
306 o.Channel <- msg
307 },
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700308 },
309 )
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100310
Matteo Scandolo27428702019-10-11 16:21:16 -0700311 return &o
Matteo Scandolo4747d292019-08-05 11:50:18 -0700312}
313
William Kurkian0418bc82019-11-06 12:16:24 -0500314func (o *Onu) logStateChange(src string, dst string) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700315 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700316 "OnuId": o.ID,
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700317 "IntfId": o.PonPortID,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700318 "OnuSn": o.Sn(),
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700319 }).Debugf("Changing ONU InternalState from %s to %s", src, dst)
320}
321
Matteo Scandolod15c0b42021-03-22 11:38:13 -0700322// cleanupOnuState this method is to clean the local state when the ONU is disabled
323func (o *Onu) cleanupOnuState() {
324 // clean the ONU state
325 o.PortNo = 0
326 o.Flows = []FlowKey{}
327 o.PonPort.removeOnuId(o.ID)
328 o.PonPort.removeAllocId(o.SerialNumber)
329 o.PonPort.removeGemPortBySn(o.SerialNumber)
330
331 o.onuAlarmsInfoLock.Lock()
332 o.onuAlarmsInfo = make(map[omcilib.OnuAlarmInfoMapKey]omcilib.OnuAlarmInfo) //Basically reset everything on onu disable
333 o.onuAlarmsInfoLock.Unlock()
334}
335
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100336// ProcessOnuMessages starts indication channel for each ONU
David Bainbridge103cf022019-12-16 20:11:35 +0000337func (o *Onu) ProcessOnuMessages(ctx context.Context, stream openolt.Openolt_EnableIndicationServer, client openolt.OpenoltClient) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700338 onuLogger.WithFields(log.Fields{
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100339 "onuID": o.ID,
340 "onuSN": o.Sn(),
341 "ponPort": o.PonPortID,
Matteo Scandolo9ddb3a92021-04-14 16:16:20 -0700342 "stream": stream,
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100343 }).Debug("Starting ONU Indication Channel")
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700344
Matteo Scandolob307d8a2021-05-10 15:19:27 -0700345 defer onuLogger.WithFields(log.Fields{
346 "onuID": o.ID,
347 "onuSN": o.Sn(),
348 "stream": stream,
349 }).Debug("Stopped handling ONU Indication Channel")
350
David Bainbridge103cf022019-12-16 20:11:35 +0000351loop:
352 for {
353 select {
354 case <-ctx.Done():
355 onuLogger.WithFields(log.Fields{
356 "onuID": o.ID,
357 "onuSN": o.Sn(),
Matteo Scandolo9ddb3a92021-04-14 16:16:20 -0700358 }).Debug("ONU message handling canceled via context")
359 break loop
360 case <-stream.Context().Done():
361 onuLogger.WithFields(log.Fields{
362 "onuID": o.ID,
363 "onuSN": o.Sn(),
364 }).Debug("ONU message handling canceled via stream context")
David Bainbridge103cf022019-12-16 20:11:35 +0000365 break loop
366 case message, ok := <-o.Channel:
367 if !ok || ctx.Err() != nil {
368 onuLogger.WithFields(log.Fields{
369 "onuID": o.ID,
370 "onuSN": o.Sn(),
Matteo Scandolo9ddb3a92021-04-14 16:16:20 -0700371 }).Debug("ONU message handling canceled via channel close")
David Bainbridge103cf022019-12-16 20:11:35 +0000372 break loop
Matteo Scandolo075b1892019-10-07 12:11:07 -0700373 }
David Bainbridge103cf022019-12-16 20:11:35 +0000374 onuLogger.WithFields(log.Fields{
375 "onuID": o.ID,
376 "onuSN": o.Sn(),
377 "messageType": message.Type,
378 }).Tracef("Received message on ONU Channel")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700379
David Bainbridge103cf022019-12-16 20:11:35 +0000380 switch message.Type {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800381 case bbsim.OnuDiscIndication:
382 msg, _ := message.Data.(bbsim.OnuDiscIndicationMessage)
David Bainbridge103cf022019-12-16 20:11:35 +0000383 // 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 +0530384 time.Sleep(o.DiscoveryDelay)
David Bainbridge103cf022019-12-16 20:11:35 +0000385 o.sendOnuDiscIndication(msg, stream)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800386 case bbsim.OnuIndication:
387 msg, _ := message.Data.(bbsim.OnuIndicationMessage)
David Bainbridge103cf022019-12-16 20:11:35 +0000388 o.sendOnuIndication(msg, stream)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800389 case bbsim.OMCI:
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800390 // these are OMCI messages received by the ONU
Matteo Scandolof9d43412021-01-12 11:11:34 -0800391 msg, _ := message.Data.(bbsim.OmciMessage)
Andrea Campanellabe1b7cf2021-04-30 09:53:40 +0200392 _ = o.handleOmciRequest(msg, stream)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800393 case bbsim.UniStatusAlarm:
394 msg, _ := message.Data.(bbsim.UniStatusAlarmMessage)
Himani Chawla13b1ee02021-03-15 01:43:53 +0530395 onuAlarmMapKey := omcilib.OnuAlarmInfoMapKey{
396 MeInstance: msg.EntityID,
397 MeClassID: me.PhysicalPathTerminationPointEthernetUniClassID,
398 }
399 seqNo := o.IncrementAlarmSequenceNumber(onuAlarmMapKey)
400 o.onuAlarmsInfoLock.Lock()
401 var alarmInfo = o.onuAlarmsInfo[onuAlarmMapKey]
402 pkt, alarmBitMap := omcilib.CreateUniStatusAlarm(msg.RaiseOMCIAlarm, msg.EntityID, seqNo)
403 if pkt != nil { //pkt will be nil if we are unable to create the alarm
404 if err := o.sendOmciIndication(pkt, 0, stream); err != nil {
405 onuLogger.WithFields(log.Fields{
406 "IntfId": o.PonPortID,
407 "SerialNumber": o.Sn(),
408 "omciPacket": pkt,
409 "adminState": msg.AdminState,
410 "entityID": msg.EntityID,
411 }).Errorf("failed-to-send-UNI-Link-Alarm: %v", err)
412 alarmInfo.SequenceNo--
413 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800414 onuLogger.WithFields(log.Fields{
415 "IntfId": o.PonPortID,
416 "SerialNumber": o.Sn(),
417 "omciPacket": pkt,
418 "adminState": msg.AdminState,
419 "entityID": msg.EntityID,
Himani Chawla13b1ee02021-03-15 01:43:53 +0530420 }).Trace("UNI-Link-alarm-sent")
421 if alarmBitMap == [28]byte{0} {
422 delete(o.onuAlarmsInfo, onuAlarmMapKey)
423 } else {
424 alarmInfo.AlarmBitMap = alarmBitMap
425 o.onuAlarmsInfo[onuAlarmMapKey] = alarmInfo
426 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800427 }
Himani Chawla13b1ee02021-03-15 01:43:53 +0530428 o.onuAlarmsInfoLock.Unlock()
Matteo Scandolof9d43412021-01-12 11:11:34 -0800429 case bbsim.FlowAdd:
430 msg, _ := message.Data.(bbsim.OnuFlowUpdateMessage)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700431 o.handleFlowAdd(msg)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800432 case bbsim.FlowRemoved:
433 msg, _ := message.Data.(bbsim.OnuFlowUpdateMessage)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700434 o.handleFlowRemove(msg)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800435 case bbsim.OnuPacketOut:
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700436
Matteo Scandolof9d43412021-01-12 11:11:34 -0800437 msg, _ := message.Data.(bbsim.OnuPacketMessage)
David Bainbridge103cf022019-12-16 20:11:35 +0000438
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700439 onuLogger.WithFields(log.Fields{
David Bainbridge103cf022019-12-16 20:11:35 +0000440 "IntfId": msg.IntfId,
441 "OnuId": msg.OnuId,
442 "pktType": msg.Type,
443 }).Trace("Received OnuPacketOut Message")
444
Matteo Scandolo618a6582020-09-09 12:21:29 -0700445 if msg.Type == packetHandlers.EAPOL || msg.Type == packetHandlers.DHCP {
446
447 service, err := o.findServiceByMacAddress(msg.MacAddress)
448 if err != nil {
449 onuLogger.WithFields(log.Fields{
450 "IntfId": msg.IntfId,
451 "OnuId": msg.OnuId,
452 "pktType": msg.Type,
453 "MacAddress": msg.MacAddress,
454 "Pkt": hex.EncodeToString(msg.Packet.Data()),
455 "OnuSn": o.Sn(),
456 }).Error("Cannot find Service associated with packet")
457 return
458 }
459 service.PacketCh <- msg
460 } else if msg.Type == packetHandlers.IGMP {
461 // if it's an IGMP packet we assume we have a single IGMP service
462 for _, s := range o.Services {
463 service := s.(*Service)
464
465 if service.NeedsIgmp {
466 service.PacketCh <- msg
467 }
468 }
David Bainbridge103cf022019-12-16 20:11:35 +0000469 }
Matteo Scandolo4a036262020-08-17 15:56:13 -0700470
Matteo Scandolof9d43412021-01-12 11:11:34 -0800471 case bbsim.OnuPacketIn:
David Bainbridge103cf022019-12-16 20:11:35 +0000472 // NOTE we only receive BBR packets here.
473 // Eapol.HandleNextPacket can handle both BBSim and BBr cases so the call is the same
474 // 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 -0800475 msg, _ := message.Data.(bbsim.OnuPacketMessage)
David Bainbridge103cf022019-12-16 20:11:35 +0000476
477 log.WithFields(log.Fields{
478 "IntfId": msg.IntfId,
479 "OnuId": msg.OnuId,
480 "pktType": msg.Type,
481 }).Trace("Received OnuPacketIn Message")
482
483 if msg.Type == packetHandlers.EAPOL {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700484 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 +0000485 } else if msg.Type == packetHandlers.DHCP {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700486 _ = dhcp.HandleNextBbrPacket(o.ID, o.PonPortID, o.Sn(), o.DoneChannel, msg.Packet, client)
David Bainbridge103cf022019-12-16 20:11:35 +0000487 }
Matteo Scandolo7f656fb2020-09-08 15:18:15 -0700488 // BBR specific messages
Matteo Scandolof9d43412021-01-12 11:11:34 -0800489 case bbsim.OmciIndication:
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800490 // these are OMCI messages received by BBR (VOLTHA emulator)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800491 msg, _ := message.Data.(bbsim.OmciIndicationMessage)
492 o.handleOmciResponse(msg, client)
493 case bbsim.SendEapolFlow:
David Bainbridge103cf022019-12-16 20:11:35 +0000494 o.sendEapolFlow(client)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800495 case bbsim.SendDhcpFlow:
David Bainbridge103cf022019-12-16 20:11:35 +0000496 o.sendDhcpFlow(client)
497 default:
498 onuLogger.Warnf("Received unknown message data %v for type %v in OLT Channel", message.Data, message.Type)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700499 }
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700500 }
501 }
Matteo Scandolo4747d292019-08-05 11:50:18 -0700502}
503
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800504func NewSN(oltid int, intfid uint32, onuid uint32) *openolt.SerialNumber {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700505 sn := new(openolt.SerialNumber)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700506 sn.VendorId = []byte("BBSM")
507 sn.VendorSpecific = []byte{0, byte(oltid % 256), byte(intfid), byte(onuid)}
Matteo Scandolo4747d292019-08-05 11:50:18 -0700508 return sn
509}
510
Matteo Scandolof9d43412021-01-12 11:11:34 -0800511func (o *Onu) sendOnuDiscIndication(msg bbsim.OnuDiscIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700512 discoverData := &openolt.Indication_OnuDiscInd{OnuDiscInd: &openolt.OnuDiscIndication{
Matteo Scandolof9d43412021-01-12 11:11:34 -0800513 IntfId: o.PonPortID,
514 SerialNumber: o.SerialNumber,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700515 }}
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700516
Matteo Scandolo4747d292019-08-05 11:50:18 -0700517 if err := stream.Send(&openolt.Indication{Data: discoverData}); err != nil {
Matteo Scandolo11006992019-08-28 11:29:46 -0700518 log.Errorf("Failed to send Indication_OnuDiscInd: %v", err)
Matteo Scandolo99f18462019-10-28 14:14:28 -0700519 return
Matteo Scandolo4747d292019-08-05 11:50:18 -0700520 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700521
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700522 onuLogger.WithFields(log.Fields{
Matteo Scandolof9d43412021-01-12 11:11:34 -0800523 "IntfId": o.PonPortID,
524 "OnuSn": o.Sn(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700525 "OnuId": o.ID,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700526 }).Debug("Sent Indication_OnuDiscInd")
Matteo Scandolof9d43412021-01-12 11:11:34 -0800527 publishEvent("ONU-discovery-indication-sent", int32(o.PonPortID), int32(o.ID), o.Sn())
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800528
529 // after DiscoveryRetryDelay check if the state is the same and in case send a new OnuDiscIndication
530 go func(delay time.Duration) {
Matteo Scandolo569e7172019-12-20 11:51:51 -0800531 time.Sleep(delay)
Matteo Scandolocedde462021-03-09 17:37:16 -0800532 if o.InternalState.Current() == OnuStateDiscovered {
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800533 o.sendOnuDiscIndication(msg, stream)
534 }
535 }(o.DiscoveryRetryDelay)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700536}
537
Matteo Scandolof9d43412021-01-12 11:11:34 -0800538func (o *Onu) sendOnuIndication(msg bbsim.OnuIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800539 // NOTE the ONU ID is set by VOLTHA in the ActivateOnu call (via openolt.proto)
540 // and stored in the Onu struct via onu.SetID
Matteo Scandolo4747d292019-08-05 11:50:18 -0700541
542 indData := &openolt.Indication_OnuInd{OnuInd: &openolt.OnuIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700543 IntfId: o.PonPortID,
544 OnuId: o.ID,
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700545 OperState: msg.OperState.String(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700546 AdminState: o.OperState.Current(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700547 SerialNumber: o.SerialNumber,
548 }}
549 if err := stream.Send(&openolt.Indication{Data: indData}); err != nil {
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800550 // NOTE do we need to transition to a broken state?
Matteo Scandolo11006992019-08-28 11:29:46 -0700551 log.Errorf("Failed to send Indication_OnuInd: %v", err)
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700552 return
Matteo Scandolo4747d292019-08-05 11:50:18 -0700553 }
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700554 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800555 "IntfId": o.PonPortID,
556 "OnuId": o.ID,
557 "VolthaOnuId": msg.OnuID,
558 "OperState": msg.OperState.String(),
559 "AdminState": msg.OperState.String(),
560 "OnuSn": o.Sn(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700561 }).Debug("Sent Indication_OnuInd")
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700562
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700563}
564
Matteo Scandolof9d43412021-01-12 11:11:34 -0800565func (o *Onu) HandleShutdownONU() error {
566
567 dyingGasp := pb.ONUAlarmRequest{
568 AlarmType: "DYING_GASP",
569 SerialNumber: o.Sn(),
570 Status: "on",
571 }
572
573 if err := alarmsim.SimulateOnuAlarm(&dyingGasp, o.ID, o.PonPortID, o.PonPort.Olt.channel); err != nil {
574 onuLogger.WithFields(log.Fields{
575 "OnuId": o.ID,
576 "IntfId": o.PonPortID,
577 "OnuSn": o.Sn(),
578 }).Errorf("Cannot send Dying Gasp: %s", err.Error())
579 return err
580 }
581
582 losReq := pb.ONUAlarmRequest{
583 AlarmType: "ONU_ALARM_LOS",
584 SerialNumber: o.Sn(),
585 Status: "on",
586 }
587
588 if err := alarmsim.SimulateOnuAlarm(&losReq, o.ID, o.PonPortID, o.PonPort.Olt.channel); err != nil {
589 onuLogger.WithFields(log.Fields{
590 "OnuId": o.ID,
591 "IntfId": o.PonPortID,
592 "OnuSn": o.Sn(),
593 }).Errorf("Cannot send LOS: %s", err.Error())
594
595 return err
596 }
Himani Chawla13b1ee02021-03-15 01:43:53 +0530597 o.SendOMCIAlarmNotificationMsg(true, losReq.AlarmType)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800598 // TODO if it's the last ONU on the PON, then send a PON LOS
599
Matteo Scandolocedde462021-03-09 17:37:16 -0800600 if err := o.InternalState.Event(OnuTxDisable); err != nil {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800601 onuLogger.WithFields(log.Fields{
602 "OnuId": o.ID,
603 "IntfId": o.PonPortID,
604 "OnuSn": o.Sn(),
605 }).Errorf("Cannot shutdown ONU: %s", err.Error())
606 return err
607 }
608
609 return nil
610}
611
612func (o *Onu) HandlePowerOnONU() error {
613 intitalState := o.InternalState.Current()
614
615 // initialize the ONU
Matteo Scandolocedde462021-03-09 17:37:16 -0800616 if intitalState == OnuStateCreated || intitalState == OnuStateDisabled {
617 if err := o.InternalState.Event(OnuTxInitialize); err != nil {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800618 onuLogger.WithFields(log.Fields{
619 "OnuId": o.ID,
620 "IntfId": o.PonPortID,
621 "OnuSn": o.Sn(),
622 }).Errorf("Cannot poweron ONU: %s", err.Error())
623 return err
624 }
625 }
626
627 // turn off the LOS Alarm
628 losReq := pb.ONUAlarmRequest{
629 AlarmType: "ONU_ALARM_LOS",
630 SerialNumber: o.Sn(),
631 Status: "off",
632 }
633
634 if err := alarmsim.SimulateOnuAlarm(&losReq, o.ID, o.PonPortID, o.PonPort.Olt.channel); err != nil {
635 onuLogger.WithFields(log.Fields{
636 "OnuId": o.ID,
637 "IntfId": o.PonPortID,
638 "OnuSn": o.Sn(),
639 }).Errorf("Cannot send LOS: %s", err.Error())
640 return err
641 }
Himani Chawla13b1ee02021-03-15 01:43:53 +0530642 o.SendOMCIAlarmNotificationMsg(false, losReq.AlarmType)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800643
644 // Send a ONU Discovery indication
Matteo Scandolocedde462021-03-09 17:37:16 -0800645 if err := o.InternalState.Event(OnuTxDiscover); err != nil {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800646 onuLogger.WithFields(log.Fields{
647 "OnuId": o.ID,
648 "IntfId": o.PonPortID,
649 "OnuSn": o.Sn(),
650 }).Errorf("Cannot poweron ONU: %s", err.Error())
651 return err
652 }
653
654 // move o directly to enable state only when its a powercycle case
655 // in case of first time o poweron o will be moved to enable on
656 // receiving ActivateOnu request from openolt adapter
Matteo Scandolocedde462021-03-09 17:37:16 -0800657 if intitalState == OnuStateDisabled {
658 if err := o.InternalState.Event(OnuTxEnable); err != nil {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800659 onuLogger.WithFields(log.Fields{
660 "OnuId": o.ID,
661 "IntfId": o.PonPortID,
662 "OnuSn": o.Sn(),
663 }).Errorf("Cannot enable ONU: %s", err.Error())
664 return err
665 }
666 }
667
668 return nil
669}
670
671func (o *Onu) SetAlarm(alarmType string, status string) error {
672 alarmReq := pb.ONUAlarmRequest{
673 AlarmType: alarmType,
674 SerialNumber: o.Sn(),
675 Status: status,
676 }
677
678 err := alarmsim.SimulateOnuAlarm(&alarmReq, o.ID, o.PonPortID, o.PonPort.Olt.channel)
679 if err != nil {
680 return err
681 }
Himani Chawla13b1ee02021-03-15 01:43:53 +0530682 raiseAlarm := false
683 if alarmReq.Status == "on" {
684 raiseAlarm = true
685 }
686 o.SendOMCIAlarmNotificationMsg(raiseAlarm, alarmReq.AlarmType)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800687 return nil
688}
689
690func (o *Onu) publishOmciEvent(msg bbsim.OmciMessage) {
Pragya Arya324337e2020-02-20 14:35:08 +0530691 if olt.PublishEvents {
Matteo Scandolob5913142021-03-19 16:10:18 -0700692 _, omciMsg, err := omcilib.ParseOpenOltOmciPacket(msg.OmciPkt.Data())
Pragya Arya324337e2020-02-20 14:35:08 +0530693 if err != nil {
694 log.Errorf("error in getting msgType %v", err)
695 return
696 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800697 if omciMsg.MessageType == omci.MibUploadRequestType {
Pragya Arya324337e2020-02-20 14:35:08 +0530698 o.seqNumber = 0
699 publishEvent("MIB-upload-received", int32(o.PonPortID), int32(o.ID), common.OnuSnToString(o.SerialNumber))
Matteo Scandolof9d43412021-01-12 11:11:34 -0800700 } else if omciMsg.MessageType == omci.MibUploadNextRequestType {
Pragya Arya324337e2020-02-20 14:35:08 +0530701 o.seqNumber++
702 if o.seqNumber > 290 {
703 publishEvent("MIB-upload-done", int32(o.PonPortID), int32(o.ID), common.OnuSnToString(o.SerialNumber))
704 }
705 }
706 }
707}
708
Scott Bakerb90c4312020-03-12 21:33:25 -0700709// Create a TestResponse packet and send it
Matteo Scandolof9d43412021-01-12 11:11:34 -0800710func (o *Onu) sendTestResult(msg bbsim.OmciMessage, stream openolt.Openolt_EnableIndicationServer) error {
Matteo Scandolob5913142021-03-19 16:10:18 -0700711 resp, err := omcilib.BuildTestResult(msg.OmciPkt.Data())
Scott Bakerb90c4312020-03-12 21:33:25 -0700712 if err != nil {
713 return err
714 }
715
716 var omciInd openolt.OmciIndication
717 omciInd.IntfId = o.PonPortID
718 omciInd.OnuId = o.ID
719 omciInd.Pkt = resp
720
721 omci := &openolt.Indication_OmciInd{OmciInd: &omciInd}
722 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
Scott Bakerb90c4312020-03-12 21:33:25 -0700723 return err
724 }
725 onuLogger.WithFields(log.Fields{
726 "IntfId": o.PonPortID,
727 "SerialNumber": o.Sn(),
728 "omciPacket": omciInd.Pkt,
729 }).Tracef("Sent TestResult OMCI message")
730
731 return nil
732}
733
Matteo Scandolof9d43412021-01-12 11:11:34 -0800734// handleOmciRequest is responsible to parse the OMCI packets received from the openolt adapter
735// and generate the appropriate response to it
Andrea Campanellabe1b7cf2021-04-30 09:53:40 +0200736func (o *Onu) handleOmciRequest(msg bbsim.OmciMessage, stream openolt.Openolt_EnableIndicationServer) error {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800737
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700738 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -0700739 "omciMsgType": msg.OmciMsg.MessageType,
740 "transCorrId": strconv.FormatInt(int64(msg.OmciMsg.TransactionID), 16),
741 "DeviceIdent": msg.OmciMsg.DeviceIdentifier,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700742 "IntfId": o.PonPortID,
Matteo Scandolo27428702019-10-11 16:21:16 -0700743 "SerialNumber": o.Sn(),
Matteo Scandolof9d43412021-01-12 11:11:34 -0800744 }).Trace("omci-message-decoded")
745
Holger Hildebrandtc10bab12021-04-27 09:23:48 +0000746 if o.OmciMsgCounter < maxOmciMsgCounter {
747 o.OmciMsgCounter++
748 } else {
749 o.OmciMsgCounter = 1
750 }
751 if o.OmciMsgCounter > o.OmciResponseRate {
752 onuLogger.WithFields(log.Fields{
753 "OmciMsgCounter": o.OmciMsgCounter,
754 "OmciResponseRate": o.OmciResponseRate,
755 "omciMsgType": msg.OmciMsg.MessageType,
Andrea Campanellabe1b7cf2021-04-30 09:53:40 +0200756 }).Debug("skipping-omci-msg-response")
757 return fmt.Errorf("skipping-omci-msg-response-because-of-response-rate-%d", o.OmciResponseRate)
Holger Hildebrandtc10bab12021-04-27 09:23:48 +0000758 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800759 var responsePkt []byte
Girish Gowdrae2683102021-03-05 08:24:26 -0800760 var errResp error
Matteo Scandolob5913142021-03-19 16:10:18 -0700761 switch msg.OmciMsg.MessageType {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800762 case omci.MibResetRequestType:
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800763 onuLogger.WithFields(log.Fields{
764 "IntfId": o.PonPortID,
765 "OnuId": o.ID,
766 "SerialNumber": o.Sn(),
Matteo Scandolo2fdc3b82021-04-23 15:27:21 -0700767 }).Debug("received-mib-reset-request")
Matteo Scandolob5913142021-03-19 16:10:18 -0700768 if responsePkt, errResp = omcilib.CreateMibResetResponse(msg.OmciMsg.TransactionID); errResp == nil {
Girish Gowdrae2683102021-03-05 08:24:26 -0800769 o.MibDataSync = 0
Matteo Scandolo2fdc3b82021-04-23 15:27:21 -0700770
771 // if the MIB reset is successful then remove all the stored AllocIds and GemPorts
772 o.PonPort.removeAllocId(o.SerialNumber)
773 o.PonPort.removeGemPortBySn(o.SerialNumber)
Girish Gowdrae2683102021-03-05 08:24:26 -0800774 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800775 case omci.MibUploadRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -0700776 responsePkt, _ = omcilib.CreateMibUploadResponse(msg.OmciMsg.TransactionID)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800777 case omci.MibUploadNextRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -0700778 responsePkt, _ = omcilib.CreateMibUploadNextResponse(msg.OmciPkt, msg.OmciMsg, o.MibDataSync)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800779 case omci.GetRequestType:
Girish Gowdra996d81e2021-04-21 16:16:27 -0700780 onuDown := o.OperState.Current() == "down"
781 responsePkt, _ = omcilib.CreateGetResponse(msg.OmciPkt, msg.OmciMsg, o.SerialNumber, o.MibDataSync, o.ActiveImageEntityId, o.CommittedImageEntityId, onuDown)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800782 case omci.SetRequestType:
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800783 success := true
Matteo Scandolob5913142021-03-19 16:10:18 -0700784 msgObj, _ := omcilib.ParseSetRequest(msg.OmciPkt)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800785 switch msgObj.EntityClass {
786 case me.PhysicalPathTerminationPointEthernetUniClassID:
787 // if we're Setting a PPTP state
788 // we need to send the appropriate alarm
789
790 if msgObj.EntityInstance == 257 {
791 // for now we're only caring about the first UNI
792 // NOTE that the EntityID for the UNI port is for now hardcoded in
793 // omci/mibpackets.go where the PhysicalPathTerminationPointEthernetUni
794 // are reported during the MIB Upload sequence
795 adminState := msgObj.Attributes["AdministrativeState"].(uint8)
Himani Chawla13b1ee02021-03-15 01:43:53 +0530796 raiseOMCIAlarm := false
797 if adminState == 1 {
798 raiseOMCIAlarm = true
Girish Gowdra996d81e2021-04-21 16:16:27 -0700799 // set the OperState to disabled
800 if err := o.OperState.Event(OnuTxDisable); err != nil {
801 onuLogger.WithFields(log.Fields{
802 "OnuId": o.ID,
803 "IntfId": o.PonPortID,
804 "OnuSn": o.Sn(),
805 }).Errorf("Cannot change ONU OperState to down: %s", err.Error())
806 }
807 } else {
808 // set the OperState to enabled
809 if err := o.OperState.Event(OnuTxEnable); err != nil {
810 onuLogger.WithFields(log.Fields{
811 "OnuId": o.ID,
812 "IntfId": o.PonPortID,
813 "OnuSn": o.Sn(),
814 }).Errorf("Cannot change ONU OperState to up: %s", err.Error())
815 }
Himani Chawla13b1ee02021-03-15 01:43:53 +0530816 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800817 msg := bbsim.Message{
818 Type: bbsim.UniStatusAlarm,
819 Data: bbsim.UniStatusAlarmMessage{
Himani Chawla13b1ee02021-03-15 01:43:53 +0530820 OnuSN: o.SerialNumber,
821 OnuID: o.ID,
822 AdminState: adminState,
823 EntityID: msgObj.EntityInstance,
824 RaiseOMCIAlarm: raiseOMCIAlarm,
Matteo Scandolof9d43412021-01-12 11:11:34 -0800825 },
826 }
827 o.Channel <- msg
828 }
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800829 case me.TContClassID:
830 allocId := msgObj.Attributes["AllocId"].(uint16)
831
832 // if the AllocId is 255 (0xFF) or 65535 (0xFFFF) it means we are removing it,
833 // otherwise we are adding it
834 if allocId == 255 || allocId == 65535 {
835 onuLogger.WithFields(log.Fields{
836 "IntfId": o.PonPortID,
837 "OnuId": o.ID,
838 "TContId": msgObj.EntityInstance,
839 "AllocId": allocId,
840 "SerialNumber": o.Sn(),
841 }).Trace("freeing-alloc-id-via-omci")
842 o.PonPort.removeAllocId(o.SerialNumber)
843 } else {
844 if used, sn := o.PonPort.isAllocIdAllocated(allocId); used {
845 onuLogger.WithFields(log.Fields{
846 "IntfId": o.PonPortID,
847 "OnuId": o.ID,
848 "AllocId": allocId,
849 "SerialNumber": o.Sn(),
850 }).Errorf("allocid-already-allocated-to-onu-with-sn-%s", common.OnuSnToString(sn))
851 success = false
852 } else {
853 onuLogger.WithFields(log.Fields{
854 "IntfId": o.PonPortID,
855 "OnuId": o.ID,
856 "TContId": msgObj.EntityInstance,
857 "AllocId": allocId,
858 "SerialNumber": o.Sn(),
859 }).Trace("storing-alloc-id-via-omci")
860 o.PonPort.storeAllocId(allocId, o.SerialNumber)
861 }
862 }
863
864 }
865
866 if success {
Matteo Scandolob5913142021-03-19 16:10:18 -0700867 if responsePkt, errResp = omcilib.CreateSetResponse(msg.OmciPkt, msg.OmciMsg, me.Success); errResp == nil {
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800868 o.MibDataSync++
869 }
870 } else {
Matteo Scandolob5913142021-03-19 16:10:18 -0700871 responsePkt, _ = omcilib.CreateSetResponse(msg.OmciPkt, msg.OmciMsg, me.AttributeFailure)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800872 }
873 case omci.CreateRequestType:
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800874 // check for GemPortNetworkCtp and make sure there are no duplicates on the same PON
875 var used bool
876 var sn *openolt.SerialNumber
Matteo Scandolob5913142021-03-19 16:10:18 -0700877 msgObj, err := omcilib.ParseCreateRequest(msg.OmciPkt)
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800878 if err == nil {
879 if msgObj.EntityClass == me.GemPortNetworkCtpClassID {
Matteo Scandolo973b0182021-04-08 11:24:42 -0700880 // GemPort 4069 is reserved for multicast and shared across ONUs
881 if msgObj.EntityInstance != 4069 {
882 if used, sn = o.PonPort.isGemPortAllocated(msgObj.EntityInstance); used {
883 onuLogger.WithFields(log.Fields{
884 "IntfId": o.PonPortID,
885 "OnuId": o.ID,
886 "GemPortId": msgObj.EntityInstance,
887 "SerialNumber": o.Sn(),
888 }).Errorf("gemport-already-allocated-to-onu-with-sn-%s", common.OnuSnToString(sn))
889 } else {
890 onuLogger.WithFields(log.Fields{
891 "IntfId": o.PonPortID,
892 "OnuId": o.ID,
893 "GemPortId": msgObj.EntityInstance,
894 "SerialNumber": o.Sn(),
895 }).Trace("storing-gem-port-id-via-omci")
896 o.PonPort.storeGemPort(msgObj.EntityInstance, o.SerialNumber)
897 }
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800898 }
899 }
900 }
901
902 // if the gemPort is valid then increment the MDS and return a successful response
903 // otherwise fail the request
904 // for now the CreateRequeste for the gemPort is the only one that can fail, if we start supporting multiple
905 // validation this check will need to be rewritten
906 if !used {
Matteo Scandolob5913142021-03-19 16:10:18 -0700907 if responsePkt, errResp = omcilib.CreateCreateResponse(msg.OmciPkt, msg.OmciMsg, me.Success); errResp == nil {
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800908 o.MibDataSync++
909 }
910 } else {
Matteo Scandolob5913142021-03-19 16:10:18 -0700911 responsePkt, _ = omcilib.CreateCreateResponse(msg.OmciPkt, msg.OmciMsg, me.ProcessingError)
Girish Gowdrae2683102021-03-05 08:24:26 -0800912 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800913 case omci.DeleteRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -0700914 msgObj, err := omcilib.ParseDeleteRequest(msg.OmciPkt)
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800915 if err == nil {
916 if msgObj.EntityClass == me.GemPortNetworkCtpClassID {
917 onuLogger.WithFields(log.Fields{
918 "IntfId": o.PonPortID,
919 "OnuId": o.ID,
920 "GemPortId": msgObj.EntityInstance,
921 "SerialNumber": o.Sn(),
922 }).Trace("freeing-gem-port-id-via-omci")
923 o.PonPort.removeGemPort(msgObj.EntityInstance)
924 }
925 }
926
Matteo Scandolob5913142021-03-19 16:10:18 -0700927 if responsePkt, errResp = omcilib.CreateDeleteResponse(msg.OmciPkt, msg.OmciMsg); errResp == nil {
Girish Gowdrae2683102021-03-05 08:24:26 -0800928 o.MibDataSync++
929 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800930 case omci.RebootRequestType:
931
Matteo Scandolob5913142021-03-19 16:10:18 -0700932 responsePkt, _ = omcilib.CreateRebootResponse(msg.OmciPkt, msg.OmciMsg)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800933
934 // powercycle the ONU
Matteo Scandolocedde462021-03-09 17:37:16 -0800935 // we run this in a separate goroutine so that
936 // the RebootRequestResponse is sent to VOLTHA
Matteo Scandolof9d43412021-01-12 11:11:34 -0800937 go func() {
Matteo Scandolocedde462021-03-09 17:37:16 -0800938 if err := o.Reboot(10 * time.Second); err != nil {
939 log.WithFields(log.Fields{
940 "IntfId": o.PonPortID,
941 "OnuId": o.ID,
942 "SerialNumber": o.Sn(),
943 "err": err,
944 }).Error("cannot-reboot-onu-after-omci-reboot-request")
945 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800946 }()
947 case omci.TestRequestType:
948
949 // Test message is special, it requires sending two packets:
950 // first packet: TestResponse, says whether test was started successully, handled by omci-sim
951 // second packet, TestResult, reports the result of running the self-test
952 // TestResult can come some time after a TestResponse
953 // TODO: Implement some delay between the TestResponse and the TestResult
Matteo Scandolob5913142021-03-19 16:10:18 -0700954 isTest, err := omcilib.IsTestRequest(msg.OmciPkt.Data())
Matteo Scandolof9d43412021-01-12 11:11:34 -0800955 if (err == nil) && (isTest) {
956 if sendErr := o.sendTestResult(msg, stream); sendErr != nil {
957 onuLogger.WithFields(log.Fields{
958 "IntfId": o.PonPortID,
Matteo Scandolocedde462021-03-09 17:37:16 -0800959 "OnuId": o.ID,
Matteo Scandolof9d43412021-01-12 11:11:34 -0800960 "SerialNumber": o.Sn(),
Matteo Scandolob5913142021-03-19 16:10:18 -0700961 "omciPacket": msg.OmciPkt.Data(),
Matteo Scandolof9d43412021-01-12 11:11:34 -0800962 "msg": msg,
963 "err": sendErr,
964 }).Error("send-TestResult-indication-failed")
965 }
966 }
Girish Gowdraa539f522021-02-15 23:00:45 -0800967 case omci.SynchronizeTimeRequestType:
968 // MDS counter increment is not required for this message type
Matteo Scandolob5913142021-03-19 16:10:18 -0700969 responsePkt, _ = omcilib.CreateSyncTimeResponse(msg.OmciPkt, msg.OmciMsg)
Matteo Scandolocedde462021-03-09 17:37:16 -0800970 case omci.StartSoftwareDownloadRequestType:
971
972 o.ImageSoftwareReceivedSections = 0
973
Matteo Scandolob5913142021-03-19 16:10:18 -0700974 o.ImageSoftwareExpectedSections = omcilib.ComputeDownloadSectionsCount(msg.OmciPkt)
Matteo Scandolocedde462021-03-09 17:37:16 -0800975
Matteo Scandolob5913142021-03-19 16:10:18 -0700976 if responsePkt, errResp = omcilib.CreateStartSoftwareDownloadResponse(msg.OmciPkt, msg.OmciMsg); errResp == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -0800977 o.MibDataSync++
978 if err := o.InternalState.Event(OnuTxStartImageDownload); err != nil {
979 onuLogger.WithFields(log.Fields{
980 "OnuId": o.ID,
981 "IntfId": o.PonPortID,
982 "OnuSn": o.Sn(),
983 "Err": err.Error(),
984 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageDownloadStarted)
985 }
986 } else {
987 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -0700988 "OmciMsgType": msg.OmciMsg.MessageType,
989 "TransCorrId": msg.OmciMsg.TransactionID,
990 "Err": errResp.Error(),
Matteo Scandolocedde462021-03-09 17:37:16 -0800991 "IntfId": o.PonPortID,
992 "SerialNumber": o.Sn(),
993 }).Error("error-while-processing-start-software-download-request")
994 }
995 case omci.DownloadSectionRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -0700996 if msgObj, err := omcilib.ParseDownloadSectionRequest(msg.OmciPkt); err == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -0800997 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -0700998 "OmciMsgType": msg.OmciMsg.MessageType,
999 "TransCorrId": msg.OmciMsg.TransactionID,
Matteo Scandolocedde462021-03-09 17:37:16 -08001000 "EntityInstance": msgObj.EntityInstance,
1001 "SectionNumber": msgObj.SectionNumber,
1002 "SectionData": msgObj.SectionData,
1003 }).Trace("received-download-section-request")
1004 o.ImageSoftwareReceivedSections++
1005 if o.InternalState.Current() != OnuStateImageDownloadInProgress {
1006 if err := o.InternalState.Event(OnuTxProgressImageDownload); err != nil {
1007 onuLogger.WithFields(log.Fields{
1008 "OnuId": o.ID,
1009 "IntfId": o.PonPortID,
1010 "OnuSn": o.Sn(),
1011 "Err": err.Error(),
1012 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageDownloadInProgress)
1013 }
1014 }
1015 }
1016 case omci.DownloadSectionRequestWithResponseType:
1017 // NOTE we only need to respond if an ACK is requested
Matteo Scandolob5913142021-03-19 16:10:18 -07001018 responsePkt, errResp = omcilib.CreateDownloadSectionResponse(msg.OmciPkt, msg.OmciMsg)
1019 if errResp != nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001020 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -07001021 "OmciMsgType": msg.OmciMsg.MessageType,
1022 "TransCorrId": msg.OmciMsg.TransactionID,
1023 "Err": errResp.Error(),
Matteo Scandolocedde462021-03-09 17:37:16 -08001024 "IntfId": o.PonPortID,
1025 "SerialNumber": o.Sn(),
1026 }).Error("error-while-processing-create-download-section-response")
Andrea Campanellabe1b7cf2021-04-30 09:53:40 +02001027 return fmt.Errorf("error-while-processing-create-download-section-response: %s", errResp.Error())
Matteo Scandolocedde462021-03-09 17:37:16 -08001028 }
1029 o.ImageSoftwareReceivedSections++
1030
1031 case omci.EndSoftwareDownloadRequestType:
1032
1033 // In the startSoftwareDownload we get the image size and the window size.
1034 // We calculate how many DownloadSection we should receive and validate
1035 // that we got the correct amount when we receive this message
1036 success := true
1037 if o.ImageSoftwareExpectedSections != o.ImageSoftwareReceivedSections {
1038 onuLogger.WithFields(log.Fields{
1039 "OnuId": o.ID,
1040 "IntfId": o.PonPortID,
1041 "OnuSn": o.Sn(),
1042 "ExpectedSections": o.ImageSoftwareExpectedSections,
1043 "ReceivedSections": o.ImageSoftwareReceivedSections,
1044 }).Errorf("onu-did-not-receive-all-image-sections")
1045 success = false
1046 }
1047
1048 if success {
Matteo Scandolob5913142021-03-19 16:10:18 -07001049 if responsePkt, errResp = omcilib.CreateEndSoftwareDownloadResponse(msg.OmciPkt, msg.OmciMsg, me.Success); errResp == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001050 o.MibDataSync++
1051 if err := o.InternalState.Event(OnuTxCompleteImageDownload); err != nil {
1052 onuLogger.WithFields(log.Fields{
1053 "OnuId": o.ID,
1054 "IntfId": o.PonPortID,
1055 "OnuSn": o.Sn(),
1056 "Err": err.Error(),
1057 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageDownloadComplete)
1058 }
1059 } else {
1060 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -07001061 "OmciMsgType": msg.OmciMsg.MessageType,
1062 "TransCorrId": msg.OmciMsg.TransactionID,
1063 "Err": errResp.Error(),
Matteo Scandolocedde462021-03-09 17:37:16 -08001064 "IntfId": o.PonPortID,
1065 "SerialNumber": o.Sn(),
1066 }).Error("error-while-processing-end-software-download-request")
1067 }
1068 } else {
Matteo Scandolob5913142021-03-19 16:10:18 -07001069 if responsePkt, errResp = omcilib.CreateEndSoftwareDownloadResponse(msg.OmciPkt, msg.OmciMsg, me.ProcessingError); errResp == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001070 if err := o.InternalState.Event(OnuTxFailImageDownload); err != nil {
1071 onuLogger.WithFields(log.Fields{
1072 "OnuId": o.ID,
1073 "IntfId": o.PonPortID,
1074 "OnuSn": o.Sn(),
1075 "Err": err.Error(),
1076 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageDownloadError)
1077 }
1078 }
1079 }
1080
1081 case omci.ActivateSoftwareRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -07001082 if responsePkt, errResp = omcilib.CreateActivateSoftwareResponse(msg.OmciPkt, msg.OmciMsg); errResp == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001083 o.MibDataSync++
1084 if err := o.InternalState.Event(OnuTxActivateImage); err != nil {
1085 onuLogger.WithFields(log.Fields{
1086 "OnuId": o.ID,
1087 "IntfId": o.PonPortID,
1088 "OnuSn": o.Sn(),
1089 "Err": err.Error(),
1090 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageActivated)
1091 }
Matteo Scandolob5913142021-03-19 16:10:18 -07001092 if msgObj, err := omcilib.ParseActivateSoftwareRequest(msg.OmciPkt); err == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001093 o.ActiveImageEntityId = msgObj.EntityInstance
1094 } else {
1095 onuLogger.Errorf("something-went-wrong-while-activating: %s", err)
1096 }
1097 onuLogger.WithFields(log.Fields{
1098 "OnuId": o.ID,
1099 "IntfId": o.PonPortID,
1100 "OnuSn": o.Sn(),
1101 "ActiveImageEntityId": o.ActiveImageEntityId,
1102 "CommittedImageEntityId": o.CommittedImageEntityId,
1103 }).Info("onu-software-image-activated")
1104
1105 // powercycle the ONU
1106 // we run this in a separate goroutine so that
1107 // the ActivateSoftwareResponse is sent to VOLTHA
1108 // NOTE do we need to wait before rebooting?
1109 go func() {
1110 if err := o.Reboot(10 * time.Second); err != nil {
1111 log.WithFields(log.Fields{
1112 "IntfId": o.PonPortID,
1113 "OnuId": o.ID,
1114 "SerialNumber": o.Sn(),
1115 "err": err,
1116 }).Error("cannot-reboot-onu-after-omci-activate-software-request")
1117 }
1118 }()
1119 }
1120 case omci.CommitSoftwareRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -07001121 if responsePkt, errResp = omcilib.CreateCommitSoftwareResponse(msg.OmciPkt, msg.OmciMsg); errResp == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001122 o.MibDataSync++
Matteo Scandolob5913142021-03-19 16:10:18 -07001123 if msgObj, err := omcilib.ParseCommitSoftwareRequest(msg.OmciPkt); err == nil {
Matteo Scandolocedde462021-03-09 17:37:16 -08001124 // TODO validate that the image to commit is:
1125 // - active
1126 // - not already committed
1127 o.CommittedImageEntityId = msgObj.EntityInstance
1128 } else {
1129 onuLogger.Errorf("something-went-wrong-while-committing: %s", err)
1130 }
1131 if err := o.InternalState.Event(OnuTxCommitImage); err != nil {
1132 onuLogger.WithFields(log.Fields{
1133 "OnuId": o.ID,
1134 "IntfId": o.PonPortID,
1135 "OnuSn": o.Sn(),
1136 "Err": err.Error(),
1137 }).Errorf("cannot-change-onu-internal-state-to-%s", OnuStateImageCommitted)
1138 }
1139 onuLogger.WithFields(log.Fields{
1140 "OnuId": o.ID,
1141 "IntfId": o.PonPortID,
1142 "OnuSn": o.Sn(),
1143 "ActiveImageEntityId": o.ActiveImageEntityId,
1144 "CommittedImageEntityId": o.CommittedImageEntityId,
1145 }).Info("onu-software-image-committed")
1146 }
Himani Chawla13b1ee02021-03-15 01:43:53 +05301147 case omci.GetAllAlarmsRequestType:
1148 // Reset the alarm sequence number on receiving get all alarms request.
1149 o.onuAlarmsInfoLock.Lock()
1150 for key, alarmInfo := range o.onuAlarmsInfo {
1151 // reset the alarm sequence no
1152 alarmInfo.SequenceNo = 0
1153 o.onuAlarmsInfo[key] = alarmInfo
1154 }
1155 o.onuAlarmsInfoLock.Unlock()
Matteo Scandolob5913142021-03-19 16:10:18 -07001156 responsePkt, _ = omcilib.CreateGetAllAlarmsResponse(msg.OmciMsg.TransactionID, o.onuAlarmsInfo)
Himani Chawla13b1ee02021-03-15 01:43:53 +05301157 case omci.GetAllAlarmsNextRequestType:
Matteo Scandolob5913142021-03-19 16:10:18 -07001158 if responsePkt, errResp = omcilib.CreateGetAllAlarmsNextResponse(msg.OmciPkt, msg.OmciMsg, o.onuAlarmsInfo); errResp != nil {
Himani Chawla13b1ee02021-03-15 01:43:53 +05301159 responsePkt = nil //Do not send any response for error case
1160 }
Matteo Scandolof9d43412021-01-12 11:11:34 -08001161 default:
Matteo Scandolocedde462021-03-09 17:37:16 -08001162 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -07001163 "omciBytes": hex.EncodeToString(msg.OmciPkt.Data()),
1164 "omciPkt": msg.OmciPkt,
1165 "omciMsgType": msg.OmciMsg.MessageType,
1166 "transCorrId": msg.OmciMsg.TransactionID,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001167 "IntfId": o.PonPortID,
1168 "SerialNumber": o.Sn(),
1169 }).Warnf("OMCI-message-not-supported")
1170 }
1171
1172 if responsePkt != nil {
Matteo Scandolob5913142021-03-19 16:10:18 -07001173 if err := o.sendOmciIndication(responsePkt, msg.OmciMsg.TransactionID, stream); err != nil {
Matteo Scandolof9d43412021-01-12 11:11:34 -08001174 onuLogger.WithFields(log.Fields{
Matteo Scandolob5913142021-03-19 16:10:18 -07001175 "IntfId": o.PonPortID,
1176 "SerialNumber": o.Sn(),
1177 "omciPacket": responsePkt,
1178 "msg.OmciMsgType": msg.OmciMsg.MessageType,
1179 "transCorrId": msg.OmciMsg.TransactionID,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001180 }).Errorf("failed-to-send-omci-message: %v", err)
1181 }
1182 }
Matteo Scandoloc559ef12019-08-20 13:24:21 -07001183
Pragya Arya324337e2020-02-20 14:35:08 +05301184 o.publishOmciEvent(msg)
Andrea Campanellabe1b7cf2021-04-30 09:53:40 +02001185 return nil
Matteo Scandolof9d43412021-01-12 11:11:34 -08001186}
Pragya Arya324337e2020-02-20 14:35:08 +05301187
Matteo Scandolof9d43412021-01-12 11:11:34 -08001188// sendOmciIndication takes an OMCI packet and sends it up to VOLTHA
1189func (o *Onu) sendOmciIndication(responsePkt []byte, txId uint16, stream bbsim.Stream) error {
1190 indication := &openolt.Indication_OmciInd{
1191 OmciInd: &openolt.OmciIndication{
1192 IntfId: o.PonPortID,
1193 OnuId: o.ID,
1194 Pkt: responsePkt,
1195 },
Matteo Scandoloc559ef12019-08-20 13:24:21 -07001196 }
Matteo Scandolof9d43412021-01-12 11:11:34 -08001197 if err := stream.Send(&openolt.Indication{Data: indication}); err != nil {
1198 return fmt.Errorf("failed-to-send-omci-message: %v", err)
Matteo Scandoloc559ef12019-08-20 13:24:21 -07001199 }
1200 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001201 "IntfId": o.PonPortID,
Matteo Scandolo27428702019-10-11 16:21:16 -07001202 "SerialNumber": o.Sn(),
Matteo Scandolof9d43412021-01-12 11:11:34 -08001203 "omciPacket": indication.OmciInd.Pkt,
1204 "transCorrId": txId,
1205 }).Trace("omci-message-sent")
1206 return nil
Matteo Scandoloc559ef12019-08-20 13:24:21 -07001207}
1208
Matteo Scandolo27428702019-10-11 16:21:16 -07001209func (o *Onu) storePortNumber(portNo uint32) {
Matteo Scandolo813402b2019-10-23 19:24:52 -07001210 // NOTE this needed only as long as we don't support multiple UNIs
Matteo Scandolo27428702019-10-11 16:21:16 -07001211 // we need to add support for multiple UNIs
1212 // the action plan is:
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001213 // - refactor the omcisim-sim library to use https://github.com/cboling/omci instead of canned messages
Matteo Scandolo27428702019-10-11 16:21:16 -07001214 // - change the library so that it reports a single UNI and remove this workaroung
1215 // - add support for multiple UNIs in BBSim
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001216 if o.PortNo == 0 || portNo < o.PortNo {
Matteo Scandolo813402b2019-10-23 19:24:52 -07001217 onuLogger.WithFields(log.Fields{
1218 "IntfId": o.PonPortID,
1219 "OnuId": o.ID,
1220 "SerialNumber": o.Sn(),
1221 "OnuPortNo": o.PortNo,
1222 "FlowPortNo": portNo,
1223 }).Debug("Storing ONU portNo")
Matteo Scandolo27428702019-10-11 16:21:16 -07001224 o.PortNo = portNo
1225 }
1226}
1227
William Kurkian0418bc82019-11-06 12:16:24 -05001228func (o *Onu) SetID(id uint32) {
Matteo Scandolo583f17d2020-02-13 10:35:17 -08001229 onuLogger.WithFields(log.Fields{
1230 "IntfId": o.PonPortID,
1231 "OnuId": id,
1232 "SerialNumber": o.Sn(),
1233 }).Debug("Storing OnuId ")
William Kurkian0418bc82019-11-06 12:16:24 -05001234 o.ID = id
1235}
1236
Matteo Scandolof9d43412021-01-12 11:11:34 -08001237func (o *Onu) handleFlowAdd(msg bbsim.OnuFlowUpdateMessage) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001238 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001239 "AllocId": msg.Flow.AllocId,
Matteo Scandoloedf30c72020-09-18 18:15:28 -07001240 "Cookie": msg.Flow.Cookie,
1241 "DstPort": msg.Flow.Classifier.DstPort,
1242 "FlowId": msg.Flow.FlowId,
1243 "FlowType": msg.Flow.FlowType,
1244 "GemportId": msg.Flow.GemportId,
1245 "InnerVlan": msg.Flow.Classifier.IVid,
1246 "IntfId": msg.Flow.AccessIntfId,
1247 "IpProto": msg.Flow.Classifier.IpProto,
1248 "OnuId": msg.Flow.OnuId,
1249 "OnuSn": o.Sn(),
1250 "OuterVlan": msg.Flow.Classifier.OVid,
1251 "PortNo": msg.Flow.PortNo,
1252 "SrcPort": msg.Flow.Classifier.SrcPort,
1253 "UniID": msg.Flow.UniId,
1254 "ClassifierEthType": fmt.Sprintf("%x", msg.Flow.Classifier.EthType),
1255 "ClassifierOPbits": msg.Flow.Classifier.OPbits,
1256 "ClassifierIVid": msg.Flow.Classifier.IVid,
1257 "ClassifierOVid": msg.Flow.Classifier.OVid,
Matteo Scandolo4f4ac792020-10-01 16:33:21 -07001258 "ReplicateFlow": msg.Flow.ReplicateFlow,
1259 "PbitToGemport": msg.Flow.PbitToGemport,
Matteo Scandoloedf30c72020-09-18 18:15:28 -07001260 }).Debug("OLT receives FlowAdd for ONU")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001261
Matteo Scandolo813402b2019-10-23 19:24:52 -07001262 if msg.Flow.UniId != 0 {
1263 // as of now BBSim only support a single UNI, so ignore everything that is not targeted to it
1264 onuLogger.WithFields(log.Fields{
1265 "IntfId": o.PonPortID,
1266 "OnuId": o.ID,
1267 "SerialNumber": o.Sn(),
1268 }).Debug("Ignoring flow as it's not for the first UNI")
1269 return
1270 }
1271
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001272 o.FlowIds = append(o.FlowIds, msg.Flow.FlowId)
Matteo Scandolo4f4ac792020-10-01 16:33:21 -07001273
1274 var gemPortId uint32
1275 if msg.Flow.ReplicateFlow {
1276 // This means that the OLT should replicate the flow for each PBIT, for BBSim it's enough to use the
1277 // first available gemport (we only need to send one packet)
1278 // NOTE different TP may create different mapping between PBits and GemPorts, this may require some changes
1279 gemPortId = msg.Flow.PbitToGemport[0]
1280 } else {
1281 // if replicateFlows is false, then the flow is carrying the correct GemPortId
1282 gemPortId = uint32(msg.Flow.GemportId)
1283 }
1284 o.addGemPortToService(gemPortId, msg.Flow.Classifier.EthType, msg.Flow.Classifier.OVid, msg.Flow.Classifier.IVid)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001285
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001286 if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeEAPOL) && msg.Flow.Classifier.OVid == 4091 {
Matteo Scandolo27428702019-10-11 16:21:16 -07001287 // NOTE storing the PortNO, it's needed when sending PacketIndications
Matteo Scandolo813402b2019-10-23 19:24:52 -07001288 o.storePortNumber(uint32(msg.Flow.PortNo))
Matteo Scandolo4a036262020-08-17 15:56:13 -07001289
1290 for _, s := range o.Services {
Matteo Scandoloadc72a82020-09-08 18:46:08 -07001291 s.HandleAuth()
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001292 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001293 } else if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeIPv4) &&
1294 msg.Flow.Classifier.SrcPort == uint32(68) &&
Matteo Scandolobd875b32020-09-18 17:46:31 -07001295 msg.Flow.Classifier.DstPort == uint32(67) {
Matteo Scandolo99f18462019-10-28 14:14:28 -07001296
Matteo Scandolo4a036262020-08-17 15:56:13 -07001297 for _, s := range o.Services {
Matteo Scandolobd875b32020-09-18 17:46:31 -07001298 s.HandleDhcp(uint8(msg.Flow.Classifier.OPbits), int(msg.Flow.Classifier.OVid))
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -07001299 }
Matteo Scandolo3bc73742019-08-20 14:04:04 -07001300 }
1301}
1302
Matteo Scandolof9d43412021-01-12 11:11:34 -08001303func (o *Onu) handleFlowRemove(msg bbsim.OnuFlowUpdateMessage) {
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001304 onuLogger.WithFields(log.Fields{
1305 "IntfId": o.PonPortID,
1306 "OnuId": o.ID,
1307 "SerialNumber": o.Sn(),
1308 "FlowId": msg.Flow.FlowId,
1309 "FlowType": msg.Flow.FlowType,
1310 }).Debug("ONU receives FlowRemove")
1311
1312 for idx, flow := range o.FlowIds {
1313 // If the gemport is found, delete it from local cache.
1314 if flow == msg.Flow.FlowId {
1315 o.FlowIds = append(o.FlowIds[:idx], o.FlowIds[idx+1:]...)
1316 break
1317 }
1318 }
1319
1320 if len(o.FlowIds) == 0 {
1321 onuLogger.WithFields(log.Fields{
1322 "IntfId": o.PonPortID,
1323 "OnuId": o.ID,
1324 "SerialNumber": o.Sn(),
1325 }).Info("Resetting GemPort")
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001326
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301327 // check if ONU delete is performed and
1328 // terminate the ONU's ProcessOnuMessages Go routine
Matteo Scandolocedde462021-03-09 17:37:16 -08001329 if o.InternalState.Current() == OnuStateDisabled {
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301330 close(o.Channel)
1331 }
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001332 }
1333}
1334
Matteo Scandolocedde462021-03-09 17:37:16 -08001335func (o *Onu) Reboot(timeout time.Duration) error {
1336 onuLogger.WithFields(log.Fields{
1337 "IntfId": o.PonPortID,
1338 "OnuId": o.ID,
1339 "SerialNumber": o.Sn(),
1340 }).Debug("shutting-down-onu")
1341 if err := o.HandleShutdownONU(); err != nil {
1342 return err
1343 }
1344 time.Sleep(timeout)
1345 onuLogger.WithFields(log.Fields{
1346 "IntfId": o.PonPortID,
1347 "OnuId": o.ID,
1348 "SerialNumber": o.Sn(),
1349 }).Debug("power-on-onu")
1350 if err := o.HandlePowerOnONU(); err != nil {
1351 return err
1352 }
1353 return nil
1354}
1355
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001356// BBR methods
1357
1358func sendOmciMsg(pktBytes []byte, intfId uint32, onuId uint32, sn *openolt.SerialNumber, msgType string, client openolt.OpenoltClient) {
1359 omciMsg := openolt.OmciMsg{
1360 IntfId: intfId,
1361 OnuId: onuId,
1362 Pkt: pktBytes,
1363 }
1364
1365 if _, err := client.OmciMsgOut(context.Background(), &omciMsg); err != nil {
1366 log.WithFields(log.Fields{
1367 "IntfId": intfId,
1368 "OnuId": onuId,
1369 "SerialNumber": common.OnuSnToString(sn),
1370 "Pkt": omciMsg.Pkt,
1371 }).Fatalf("Failed to send MIB Reset")
1372 }
1373 log.WithFields(log.Fields{
1374 "IntfId": intfId,
1375 "OnuId": onuId,
1376 "SerialNumber": common.OnuSnToString(sn),
1377 "Pkt": omciMsg.Pkt,
1378 }).Tracef("Sent OMCI message %s", msgType)
1379}
1380
1381func (onu *Onu) getNextTid(highPriority ...bool) uint16 {
1382 var next uint16
1383 if len(highPriority) > 0 && highPriority[0] {
1384 next = onu.hpTid
1385 onu.hpTid += 1
1386 if onu.hpTid < 0x8000 {
1387 onu.hpTid = 0x8000
1388 }
1389 } else {
1390 next = onu.tid
1391 onu.tid += 1
1392 if onu.tid >= 0x8000 {
1393 onu.tid = 1
1394 }
1395 }
1396 return next
1397}
1398
1399// TODO move this method in responders/omcisim
1400func (o *Onu) StartOmci(client openolt.OpenoltClient) {
1401 mibReset, _ := omcilib.CreateMibResetRequest(o.getNextTid(false))
1402 sendOmciMsg(mibReset, o.PonPortID, o.ID, o.SerialNumber, "mibReset", client)
1403}
1404
Matteo Scandolof9d43412021-01-12 11:11:34 -08001405// handleOmciResponse is used in BBR to generate the OMCI packets the openolt-adapter would send to the device
1406func (o *Onu) handleOmciResponse(msg bbsim.OmciIndicationMessage, client openolt.OpenoltClient) {
1407
1408 // we need to encode the packet in HEX
1409 pkt := make([]byte, len(msg.OmciInd.Pkt)*2)
1410 hex.Encode(pkt, msg.OmciInd.Pkt)
1411 packet, omciMsg, err := omcilib.ParseOpenOltOmciPacket(pkt)
1412 if err != nil {
1413 log.WithFields(log.Fields{
1414 "IntfId": o.PonPortID,
1415 "SerialNumber": o.Sn(),
1416 "omciPacket": msg.OmciInd.Pkt,
1417 }).Error("BBR Cannot parse OMCI packet")
1418 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001419
1420 log.WithFields(log.Fields{
1421 "IntfId": msg.OmciInd.IntfId,
1422 "OnuId": msg.OmciInd.OnuId,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001423 "OnuSn": o.Sn(),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001424 "Pkt": msg.OmciInd.Pkt,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001425 "msgType": omciMsg.MessageType,
Anand S Katti09541352020-01-29 15:54:01 +05301426 }).Trace("ONU Receives OMCI Msg")
Matteo Scandolof9d43412021-01-12 11:11:34 -08001427 switch omciMsg.MessageType {
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001428 default:
Matteo Scandolo813402b2019-10-23 19:24:52 -07001429 log.WithFields(log.Fields{
1430 "IntfId": msg.OmciInd.IntfId,
1431 "OnuId": msg.OmciInd.OnuId,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001432 "OnuSn": o.Sn(),
Matteo Scandolo813402b2019-10-23 19:24:52 -07001433 "Pkt": msg.OmciInd.Pkt,
Matteo Scandolof9d43412021-01-12 11:11:34 -08001434 "msgType": omciMsg.MessageType,
Matteo Scandolo813402b2019-10-23 19:24:52 -07001435 }).Fatalf("unexpected frame: %v", packet)
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001436 case omci.MibResetResponseType:
1437 mibUpload, _ := omcilib.CreateMibUploadRequest(o.getNextTid(false))
1438 sendOmciMsg(mibUpload, o.PonPortID, o.ID, o.SerialNumber, "mibUpload", client)
1439 case omci.MibUploadResponseType:
1440 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
1441 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
1442 case omci.MibUploadNextResponseType:
1443 o.seqNumber++
1444
1445 if o.seqNumber > 290 {
1446 // NOTE we are done with the MIB Upload (290 is the number of messages the omci-sim library will respond to)
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001447 // start sending the flows, we don't care about the OMCI setup in BBR, just that a lot of messages can go through
Matteo Scandolocedde462021-03-09 17:37:16 -08001448 if err := o.InternalState.Event(BbrOnuTxSendEapolFlow); err != nil {
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001449 onuLogger.WithFields(log.Fields{
1450 "OnuId": o.ID,
1451 "IntfId": o.PonPortID,
1452 "OnuSn": o.Sn(),
1453 }).Errorf("Error while transitioning ONU State %v", err)
1454 }
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001455 } else {
1456 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
1457 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001458 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001459 }
1460}
1461
1462func (o *Onu) sendEapolFlow(client openolt.OpenoltClient) {
1463
1464 classifierProto := openolt.Classifier{
1465 EthType: uint32(layers.EthernetTypeEAPOL),
1466 OVid: 4091,
1467 }
1468
1469 actionProto := openolt.Action{}
1470
1471 downstreamFlow := openolt.Flow{
1472 AccessIntfId: int32(o.PonPortID),
1473 OnuId: int32(o.ID),
Matteo Scandolo813402b2019-10-23 19:24:52 -07001474 UniId: int32(0), // NOTE do not hardcode this, we need to support multiple UNIs
Matteo Scandolo4f4ac792020-10-01 16:33:21 -07001475 FlowId: uint64(o.ID),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001476 FlowType: "downstream",
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001477 NetworkIntfId: int32(0),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001478 Classifier: &classifierProto,
1479 Action: &actionProto,
1480 Priority: int32(100),
1481 Cookie: uint64(o.ID),
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001482 PortNo: o.ID, // NOTE we are using this to map an incoming packetIndication to an ONU
1483 // AllocId and GemPorts need to be unique per PON
1484 // for now use the ONU-ID, will need to change once we support multiple UNIs
1485 AllocId: int32(o.ID),
1486 GemportId: int32(o.ID),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001487 }
1488
1489 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
1490 log.WithFields(log.Fields{
1491 "IntfId": o.PonPortID,
1492 "OnuId": o.ID,
1493 "FlowId": downstreamFlow.FlowId,
1494 "PortNo": downstreamFlow.PortNo,
1495 "SerialNumber": common.OnuSnToString(o.SerialNumber),
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001496 "Err": err,
Matteo Scandolob0e3e622020-04-23 17:00:29 -07001497 }).Fatalf("Failed to add EAPOL Flow")
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001498 }
1499 log.WithFields(log.Fields{
1500 "IntfId": o.PonPortID,
1501 "OnuId": o.ID,
1502 "FlowId": downstreamFlow.FlowId,
1503 "PortNo": downstreamFlow.PortNo,
1504 "SerialNumber": common.OnuSnToString(o.SerialNumber),
1505 }).Info("Sent EAPOL Flow")
1506}
1507
1508func (o *Onu) sendDhcpFlow(client openolt.OpenoltClient) {
Matteo Scandolo4a036262020-08-17 15:56:13 -07001509
1510 // BBR only works with a single service (ATT HSIA)
1511 hsia := o.Services[0].(*Service)
1512
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001513 classifierProto := openolt.Classifier{
1514 EthType: uint32(layers.EthernetTypeIPv4),
1515 SrcPort: uint32(68),
1516 DstPort: uint32(67),
Matteo Scandolo4a036262020-08-17 15:56:13 -07001517 OVid: uint32(hsia.CTag),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001518 }
1519
1520 actionProto := openolt.Action{}
1521
1522 downstreamFlow := openolt.Flow{
1523 AccessIntfId: int32(o.PonPortID),
1524 OnuId: int32(o.ID),
Matteo Scandolo813402b2019-10-23 19:24:52 -07001525 UniId: int32(0), // FIXME do not hardcode this
Matteo Scandolo4f4ac792020-10-01 16:33:21 -07001526 FlowId: uint64(o.ID),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001527 FlowType: "downstream",
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001528 NetworkIntfId: int32(0),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001529 Classifier: &classifierProto,
1530 Action: &actionProto,
1531 Priority: int32(100),
1532 Cookie: uint64(o.ID),
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001533 PortNo: o.ID, // NOTE we are using this to map an incoming packetIndication to an ONU
1534 // AllocId and GemPorts need to be unique per PON
1535 // for now use the ONU-ID, will need to change once we support multiple UNIs
1536 AllocId: int32(o.ID),
1537 GemportId: int32(o.ID),
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001538 }
1539
1540 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
1541 log.WithFields(log.Fields{
1542 "IntfId": o.PonPortID,
1543 "OnuId": o.ID,
1544 "FlowId": downstreamFlow.FlowId,
1545 "PortNo": downstreamFlow.PortNo,
1546 "SerialNumber": common.OnuSnToString(o.SerialNumber),
Matteo Scandolo4b077aa2021-02-16 17:33:37 -08001547 "Err": err,
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001548 }).Fatalf("Failed to send DHCP Flow")
1549 }
1550 log.WithFields(log.Fields{
1551 "IntfId": o.PonPortID,
1552 "OnuId": o.ID,
1553 "FlowId": downstreamFlow.FlowId,
1554 "PortNo": downstreamFlow.PortNo,
1555 "SerialNumber": common.OnuSnToString(o.SerialNumber),
1556 }).Info("Sent DHCP Flow")
1557}
Pragya Arya8bdb4532020-03-02 17:08:09 +05301558
1559// DeleteFlow method search and delete flowKey from the onu flows slice
1560func (onu *Onu) DeleteFlow(key FlowKey) {
1561 for pos, flowKey := range onu.Flows {
1562 if flowKey == key {
1563 // delete the flowKey by shifting all flowKeys by one
1564 onu.Flows = append(onu.Flows[:pos], onu.Flows[pos+1:]...)
1565 t := make([]FlowKey, len(onu.Flows))
1566 copy(t, onu.Flows)
1567 onu.Flows = t
1568 break
1569 }
1570 }
1571}
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301572
1573func (onu *Onu) ReDiscoverOnu() {
1574 // Wait for few seconds to be sure of the cleanup
1575 time.Sleep(5 * time.Second)
1576
1577 onuLogger.WithFields(log.Fields{
1578 "IntfId": onu.PonPortID,
1579 "OnuId": onu.ID,
1580 "OnuSn": onu.Sn(),
1581 }).Debug("Send ONU Re-Discovery")
1582
1583 // ONU Re-Discovery
Matteo Scandolocedde462021-03-09 17:37:16 -08001584 if err := onu.InternalState.Event(OnuTxInitialize); err != nil {
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301585 log.WithFields(log.Fields{
1586 "IntfId": onu.PonPortID,
1587 "OnuSn": onu.Sn(),
1588 "OnuId": onu.ID,
Matteo Scandolocedde462021-03-09 17:37:16 -08001589 }).Infof("Failed to transition ONU to %s state: %s", OnuStateInitialized, err.Error())
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301590 }
1591
Matteo Scandolocedde462021-03-09 17:37:16 -08001592 if err := onu.InternalState.Event(OnuTxDiscover); err != nil {
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301593 log.WithFields(log.Fields{
1594 "IntfId": onu.PonPortID,
1595 "OnuSn": onu.Sn(),
1596 "OnuId": onu.ID,
Matteo Scandolocedde462021-03-09 17:37:16 -08001597 }).Infof("Failed to transition ONU to %s state: %s", OnuStateDiscovered, err.Error())
Hardik Windlass7b3405b2020-07-08 15:10:05 +05301598 }
1599}
Matteo Scandolo4a036262020-08-17 15:56:13 -07001600
1601func (onu *Onu) addGemPortToService(gemport uint32, ethType uint32, oVlan uint32, iVlan uint32) {
1602 for _, s := range onu.Services {
1603 if service, ok := s.(*Service); ok {
1604 // EAPOL is a strange case, as packets are untagged
1605 // but we assume we will have a single service requiring EAPOL
1606 if ethType == uint32(layers.EthernetTypeEAPOL) && service.NeedsEapol {
1607 service.GemPort = gemport
1608 }
1609
1610 // For DHCP services we single tag the outgoing packets,
1611 // thus the flow only contains the CTag and we can use that to match the service
1612 if ethType == uint32(layers.EthernetTypeIPv4) && service.NeedsDhcp && service.CTag == int(oVlan) {
1613 service.GemPort = gemport
1614 }
1615
1616 // for dataplane services match both C and S tags
1617 if service.CTag == int(iVlan) && service.STag == int(oVlan) {
1618 service.GemPort = gemport
1619 }
1620 }
1621 }
1622}
1623
1624func (onu *Onu) findServiceByMacAddress(macAddress net.HardwareAddr) (*Service, error) {
1625 for _, s := range onu.Services {
1626 service := s.(*Service)
1627 if service.HwAddress.String() == macAddress.String() {
1628 return service, nil
1629 }
1630 }
1631 return nil, fmt.Errorf("cannot-find-service-with-mac-address-%s", macAddress.String())
1632}
Himani Chawla13b1ee02021-03-15 01:43:53 +05301633
1634func (o *Onu) SendOMCIAlarmNotificationMsg(raiseOMCIAlarm bool, alarmType string) {
1635 switch alarmType {
1636 case "ONU_ALARM_LOS":
1637 msg := bbsim.Message{
1638 Type: bbsim.UniStatusAlarm,
1639 Data: bbsim.UniStatusAlarmMessage{
1640 OnuSN: o.SerialNumber,
1641 OnuID: o.ID,
1642 EntityID: 257,
1643 RaiseOMCIAlarm: raiseOMCIAlarm,
1644 },
1645 }
1646 o.Channel <- msg
1647 }
1648
1649}
1650
1651func (o *Onu) IncrementAlarmSequenceNumber(key omcilib.OnuAlarmInfoMapKey) uint8 {
1652 o.onuAlarmsInfoLock.Lock()
1653 defer o.onuAlarmsInfoLock.Unlock()
1654 if alarmInfo, ok := o.onuAlarmsInfo[key]; ok {
1655 if alarmInfo.SequenceNo == 255 {
1656 alarmInfo.SequenceNo = 1
1657 } else {
1658 alarmInfo.SequenceNo++
1659 }
1660 o.onuAlarmsInfo[key] = alarmInfo
1661 return alarmInfo.SequenceNo
1662 } else {
1663 // This is the first time alarm notification message is being sent
1664 o.onuAlarmsInfo[key] = omcilib.OnuAlarmInfo{
1665 SequenceNo: 1,
1666 }
1667 return 1
1668 }
1669}