blob: 650dcfee11f9ce7a75b3741e682aa454adf84816 [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 Scandolo4a036262020-08-17 15:56:13 -070023 "github.com/opencord/bbsim/internal/bbsim/packetHandlers"
24 "github.com/opencord/bbsim/internal/bbsim/responders/dhcp"
25 "github.com/opencord/bbsim/internal/bbsim/responders/eapol"
Zdravko Bozakov681364d2019-11-10 14:28:46 +010026 "net"
27
28 "time"
29
Matteo Scandolo40e067f2019-10-16 16:59:41 -070030 "github.com/cboling/omci"
Matteo Scandolo3bc73742019-08-20 14:04:04 -070031 "github.com/google/gopacket/layers"
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -070032 "github.com/jpillora/backoff"
Matteo Scandolo4747d292019-08-05 11:50:18 -070033 "github.com/looplab/fsm"
Matteo Scandolo40e067f2019-10-16 16:59:41 -070034 "github.com/opencord/bbsim/internal/common"
35 omcilib "github.com/opencord/bbsim/internal/common/omci"
36 omcisim "github.com/opencord/omci-sim"
Matteo Scandolo618a6582020-09-09 12:21:29 -070037 "github.com/opencord/voltha-protos/v3/go/openolt"
38 "github.com/opencord/voltha-protos/v3/go/tech_profile"
Matteo Scandolo4747d292019-08-05 11:50:18 -070039 log "github.com/sirupsen/logrus"
40)
41
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070042var onuLogger = log.WithFields(log.Fields{
43 "module": "ONU",
44})
45
Pragya Arya8bdb4532020-03-02 17:08:09 +053046type FlowKey struct {
47 ID uint32
48 Direction string
49}
50
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070051type Onu struct {
Matteo Scandoloe811ae92019-12-10 17:50:14 -080052 ID uint32
53 PonPortID uint32
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -070054 PonPort *PonPort
Matteo Scandoloe811ae92019-12-10 17:50:14 -080055 InternalState *fsm.FSM
Pragya Arya2225f202020-01-29 18:05:01 +053056 DiscoveryRetryDelay time.Duration // this is the time between subsequent Discovery Indication
57 DiscoveryDelay time.Duration // this is the time to send the first Discovery Indication
Matteo Scandolo4a036262020-08-17 15:56:13 -070058
59 Services []ServiceIf
60
61 Backoff *backoff.Backoff
Matteo Scandoloe811ae92019-12-10 17:50:14 -080062 // ONU State
Matteo Scandolo27428702019-10-11 16:21:16 -070063 // PortNo comes with flows and it's used when sending packetIndications,
64 // There is one PortNo per UNI Port, for now we're only storing the first one
Matteo Scandolo47ef64b2020-04-20 14:16:07 -070065 // FIXME add support for multiple UNIs (each UNI has a different PortNo)
Matteo Scandolo75ed5b92020-09-03 09:03:16 -070066 PortNo uint32
67 // deprecated (gemPort is on a Service basis)
Matteo Scandolo4a036262020-08-17 15:56:13 -070068 GemPortAdded bool
69 Flows []FlowKey
70 FlowIds []uint32 // keep track of the flows we currently have in the ONU
Matteo Scandolo99f18462019-10-28 14:14:28 -070071
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070072 OperState *fsm.FSM
73 SerialNumber *openolt.SerialNumber
74
Matteo Scandolo4a036262020-08-17 15:56:13 -070075 Channel chan Message // this Channel is to track state changes OMCI messages, EAPOL and DHCP packets
Matteo Scandolo40e067f2019-10-16 16:59:41 -070076
77 // OMCI params
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -070078 tid uint16
79 hpTid uint16
80 seqNumber uint16
Matteo Scandolo40e067f2019-10-16 16:59:41 -070081
Anand S Katti09541352020-01-29 15:54:01 +053082 DoneChannel chan bool // this channel is used to signal once the onu is complete (when the struct is used by BBR)
83 TrafficSchedulers *tech_profile.TrafficSchedulers
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070084}
85
Matteo Scandolo99f18462019-10-28 14:14:28 -070086func (o *Onu) Sn() string {
Matteo Scandolo40e067f2019-10-16 16:59:41 -070087 return common.OnuSnToString(o.SerialNumber)
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070088}
89
Matteo Scandolo4a036262020-08-17 15:56:13 -070090func CreateONU(olt *OltDevice, pon *PonPort, id uint32, delay time.Duration, isMock bool) *Onu {
Shrey Baidf8abccc2020-06-15 19:41:22 +053091 b := &backoff.Backoff{
92 //These are the defaults
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -070093 Min: 5 * time.Second,
Shrey Baidf8abccc2020-06-15 19:41:22 +053094 Max: 35 * time.Second,
95 Factor: 1.5,
96 Jitter: false,
97 }
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,
Shrey Baidf8abccc2020-06-15 19:41:22 +0530112 Backoff: b,
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
146 o.Channel = make(chan 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) {
162 msg := Message{
163 Type: OnuDiscIndication,
164 Data: OnuDiscIndicationMessage{
165 Onu: &o,
166 OperState: UP,
167 },
168 }
169 o.Channel <- msg
170 },
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700171 "enter_enabled": func(event *fsm.Event) {
172 msg := Message{
173 Type: OnuIndication,
174 Data: OnuIndicationMessage{
175 OnuSN: o.SerialNumber,
176 PonPortID: o.PonPortID,
177 OperState: UP,
178 },
179 }
180 o.Channel <- msg
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700181
182 // Once the ONU is enabled start listening for packets
183 for _, s := range o.Services {
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700184 s.Initialize(o.PonPort.Olt.OpenoltStream)
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700185 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700186 },
187 "enter_disabled": func(event *fsm.Event) {
Matteo Scandolo47ef64b2020-04-20 14:16:07 -0700188
189 // clean the ONU state
Matteo Scandolo328c5f02020-06-26 14:16:39 -0700190 o.GemPortAdded = false
Matteo Scandolo47ef64b2020-04-20 14:16:07 -0700191 o.PortNo = 0
192 o.Flows = []FlowKey{}
193
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700194 // set the OperState to disabled
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800195 if err := o.OperState.Event("disable"); err != nil {
196 onuLogger.WithFields(log.Fields{
197 "OnuId": o.ID,
198 "IntfId": o.PonPortID,
199 "OnuSn": o.Sn(),
200 }).Errorf("Cannot change ONU OperState to down: %s", err.Error())
201 }
Matteo Scandolo47ef64b2020-04-20 14:16:07 -0700202
203 // send the OnuIndication DOWN event
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700204 msg := Message{
205 Type: OnuIndication,
206 Data: OnuIndicationMessage{
207 OnuSN: o.SerialNumber,
208 PonPortID: o.PonPortID,
209 OperState: DOWN,
210 },
211 }
212 o.Channel <- msg
Hardik Windlass7b3405b2020-07-08 15:10:05 +0530213
214 // verify all the flows removes are handled and
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100215 // terminate the ONU's ProcessOnuMessages Go routine
Hardik Windlass7b3405b2020-07-08 15:10:05 +0530216 if len(o.FlowIds) == 0 {
217 close(o.Channel)
218 }
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700219
220 for _, s := range o.Services {
221 s.Disable()
222 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700223 },
Matteo Scandolo4a036262020-08-17 15:56:13 -0700224 // BBR states
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700225 "enter_eapol_flow_sent": func(e *fsm.Event) {
226 msg := Message{
227 Type: SendEapolFlow,
228 }
229 o.Channel <- msg
230 },
231 "enter_dhcp_flow_sent": func(e *fsm.Event) {
232 msg := Message{
233 Type: SendDhcpFlow,
234 }
235 o.Channel <- msg
236 },
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700237 },
238 )
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100239
Matteo Scandolo27428702019-10-11 16:21:16 -0700240 return &o
Matteo Scandolo4747d292019-08-05 11:50:18 -0700241}
242
William Kurkian0418bc82019-11-06 12:16:24 -0500243func (o *Onu) logStateChange(src string, dst string) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700244 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700245 "OnuId": o.ID,
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700246 "IntfId": o.PonPortID,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700247 "OnuSn": o.Sn(),
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700248 }).Debugf("Changing ONU InternalState from %s to %s", src, dst)
249}
250
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100251// ProcessOnuMessages starts indication channel for each ONU
David Bainbridge103cf022019-12-16 20:11:35 +0000252func (o *Onu) ProcessOnuMessages(ctx context.Context, stream openolt.Openolt_EnableIndicationServer, client openolt.OpenoltClient) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700253 onuLogger.WithFields(log.Fields{
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100254 "onuID": o.ID,
255 "onuSN": o.Sn(),
256 "ponPort": o.PonPortID,
257 }).Debug("Starting ONU Indication Channel")
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700258
David Bainbridge103cf022019-12-16 20:11:35 +0000259loop:
260 for {
261 select {
262 case <-ctx.Done():
263 onuLogger.WithFields(log.Fields{
264 "onuID": o.ID,
265 "onuSN": o.Sn(),
266 }).Tracef("ONU message handling canceled via context")
267 break loop
268 case message, ok := <-o.Channel:
269 if !ok || ctx.Err() != nil {
270 onuLogger.WithFields(log.Fields{
271 "onuID": o.ID,
272 "onuSN": o.Sn(),
273 }).Tracef("ONU message handling canceled via channel close")
274 break loop
Matteo Scandolo075b1892019-10-07 12:11:07 -0700275 }
David Bainbridge103cf022019-12-16 20:11:35 +0000276 onuLogger.WithFields(log.Fields{
277 "onuID": o.ID,
278 "onuSN": o.Sn(),
279 "messageType": message.Type,
280 }).Tracef("Received message on ONU Channel")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700281
David Bainbridge103cf022019-12-16 20:11:35 +0000282 switch message.Type {
283 case OnuDiscIndication:
284 msg, _ := message.Data.(OnuDiscIndicationMessage)
285 // 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 +0530286 time.Sleep(o.DiscoveryDelay)
David Bainbridge103cf022019-12-16 20:11:35 +0000287 o.sendOnuDiscIndication(msg, stream)
288 case OnuIndication:
289 msg, _ := message.Data.(OnuIndicationMessage)
290 o.sendOnuIndication(msg, stream)
291 case OMCI:
292 msg, _ := message.Data.(OmciMessage)
293 o.handleOmciMessage(msg, stream)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700294 case FlowAdd:
David Bainbridge103cf022019-12-16 20:11:35 +0000295 msg, _ := message.Data.(OnuFlowUpdateMessage)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700296 o.handleFlowAdd(msg)
297 case FlowRemoved:
298 msg, _ := message.Data.(OnuFlowUpdateMessage)
299 o.handleFlowRemove(msg)
David Bainbridge103cf022019-12-16 20:11:35 +0000300 case OnuPacketOut:
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700301
David Bainbridge103cf022019-12-16 20:11:35 +0000302 msg, _ := message.Data.(OnuPacketMessage)
303
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700304 onuLogger.WithFields(log.Fields{
David Bainbridge103cf022019-12-16 20:11:35 +0000305 "IntfId": msg.IntfId,
306 "OnuId": msg.OnuId,
307 "pktType": msg.Type,
308 }).Trace("Received OnuPacketOut Message")
309
Matteo Scandolo618a6582020-09-09 12:21:29 -0700310 if msg.Type == packetHandlers.EAPOL || msg.Type == packetHandlers.DHCP {
311
312 service, err := o.findServiceByMacAddress(msg.MacAddress)
313 if err != nil {
314 onuLogger.WithFields(log.Fields{
315 "IntfId": msg.IntfId,
316 "OnuId": msg.OnuId,
317 "pktType": msg.Type,
318 "MacAddress": msg.MacAddress,
319 "Pkt": hex.EncodeToString(msg.Packet.Data()),
320 "OnuSn": o.Sn(),
321 }).Error("Cannot find Service associated with packet")
322 return
323 }
324 service.PacketCh <- msg
325 } else if msg.Type == packetHandlers.IGMP {
326 // if it's an IGMP packet we assume we have a single IGMP service
327 for _, s := range o.Services {
328 service := s.(*Service)
329
330 if service.NeedsIgmp {
331 service.PacketCh <- msg
332 }
333 }
David Bainbridge103cf022019-12-16 20:11:35 +0000334 }
Matteo Scandolo4a036262020-08-17 15:56:13 -0700335
David Bainbridge103cf022019-12-16 20:11:35 +0000336 case OnuPacketIn:
337 // NOTE we only receive BBR packets here.
338 // Eapol.HandleNextPacket can handle both BBSim and BBr cases so the call is the same
339 // in the DHCP case VOLTHA only act as a proxy, the behaviour is completely different thus we have a dhcp.HandleNextBbrPacket
340 msg, _ := message.Data.(OnuPacketMessage)
341
342 log.WithFields(log.Fields{
343 "IntfId": msg.IntfId,
344 "OnuId": msg.OnuId,
345 "pktType": msg.Type,
346 }).Trace("Received OnuPacketIn Message")
347
348 if msg.Type == packetHandlers.EAPOL {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700349 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 +0000350 } else if msg.Type == packetHandlers.DHCP {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700351 _ = dhcp.HandleNextBbrPacket(o.ID, o.PonPortID, o.Sn(), o.DoneChannel, msg.Packet, client)
David Bainbridge103cf022019-12-16 20:11:35 +0000352 }
Matteo Scandolo7f656fb2020-09-08 15:18:15 -0700353 // BBR specific messages
David Bainbridge103cf022019-12-16 20:11:35 +0000354 case OmciIndication:
355 msg, _ := message.Data.(OmciIndicationMessage)
356 o.handleOmci(msg, client)
357 case SendEapolFlow:
358 o.sendEapolFlow(client)
359 case SendDhcpFlow:
360 o.sendDhcpFlow(client)
361 default:
362 onuLogger.Warnf("Received unknown message data %v for type %v in OLT Channel", message.Data, message.Type)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700363 }
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700364 }
365 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100366 onuLogger.WithFields(log.Fields{
367 "onuID": o.ID,
368 "onuSN": o.Sn(),
369 }).Debug("Stopped handling ONU Indication Channel")
Matteo Scandolo4747d292019-08-05 11:50:18 -0700370}
371
Matteo Scandolodf3f85d2020-01-15 12:50:48 -0800372func (o *Onu) processOmciMessage(message omcisim.OmciChMessage, stream openolt.Openolt_EnableIndicationServer) {
William Kurkian9dadc5b2019-10-22 13:51:57 -0400373 switch message.Type {
Matteo Scandolodf3f85d2020-01-15 12:50:48 -0800374 case omcisim.UniLinkUp, omcisim.UniLinkDown:
375 onuLogger.WithFields(log.Fields{
376 "OnuId": message.Data.OnuId,
377 "IntfId": message.Data.IntfId,
378 "Type": message.Type,
Matteo Scandolo4a036262020-08-17 15:56:13 -0700379 }).Debug("UNI Link Alarm")
Matteo Scandolodf3f85d2020-01-15 12:50:48 -0800380 // TODO send to OLT
381
382 omciInd := openolt.OmciIndication{
383 IntfId: message.Data.IntfId,
384 OnuId: message.Data.OnuId,
385 Pkt: message.Packet,
386 }
387
388 omci := &openolt.Indication_OmciInd{OmciInd: &omciInd}
389 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
390 onuLogger.WithFields(log.Fields{
391 "IntfId": o.PonPortID,
392 "SerialNumber": o.Sn(),
393 "Type": message.Type,
394 "omciPacket": omciInd.Pkt,
395 }).Errorf("Failed to send UNI Link Alarm: %v", err)
396 return
397 }
398
399 onuLogger.WithFields(log.Fields{
400 "IntfId": o.PonPortID,
401 "SerialNumber": o.Sn(),
402 "Type": message.Type,
403 "omciPacket": omciInd.Pkt,
Matteo Scandolo4a036262020-08-17 15:56:13 -0700404 }).Debug("UNI Link alarm sent")
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700405 }
406}
407
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100408func (o Onu) NewSN(oltid int, intfid uint32, onuid uint32) *openolt.SerialNumber {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700409
410 sn := new(openolt.SerialNumber)
411
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700412 //sn = new(openolt.SerialNumber)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700413 sn.VendorId = []byte("BBSM")
414 sn.VendorSpecific = []byte{0, byte(oltid % 256), byte(intfid), byte(onuid)}
415
416 return sn
417}
418
William Kurkian0418bc82019-11-06 12:16:24 -0500419func (o *Onu) sendOnuDiscIndication(msg OnuDiscIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700420 discoverData := &openolt.Indication_OnuDiscInd{OnuDiscInd: &openolt.OnuDiscIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700421 IntfId: msg.Onu.PonPortID,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700422 SerialNumber: msg.Onu.SerialNumber,
423 }}
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700424
Matteo Scandolo4747d292019-08-05 11:50:18 -0700425 if err := stream.Send(&openolt.Indication{Data: discoverData}); err != nil {
Matteo Scandolo11006992019-08-28 11:29:46 -0700426 log.Errorf("Failed to send Indication_OnuDiscInd: %v", err)
Matteo Scandolo99f18462019-10-28 14:14:28 -0700427 return
Matteo Scandolo4747d292019-08-05 11:50:18 -0700428 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700429
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700430 onuLogger.WithFields(log.Fields{
Matteo Scandolo4747d292019-08-05 11:50:18 -0700431 "IntfId": msg.Onu.PonPortID,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700432 "OnuSn": msg.Onu.Sn(),
433 "OnuId": o.ID,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700434 }).Debug("Sent Indication_OnuDiscInd")
Pragya Arya324337e2020-02-20 14:35:08 +0530435 publishEvent("ONU-discovery-indication-sent", int32(msg.Onu.PonPortID), int32(o.ID), msg.Onu.Sn())
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800436
437 // after DiscoveryRetryDelay check if the state is the same and in case send a new OnuDiscIndication
438 go func(delay time.Duration) {
Matteo Scandolo569e7172019-12-20 11:51:51 -0800439 time.Sleep(delay)
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800440 if o.InternalState.Current() == "discovered" {
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800441 o.sendOnuDiscIndication(msg, stream)
442 }
443 }(o.DiscoveryRetryDelay)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700444}
445
William Kurkian0418bc82019-11-06 12:16:24 -0500446func (o *Onu) sendOnuIndication(msg OnuIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700447 // NOTE voltha returns an ID, but if we use that ID then it complains:
448 // expected_onu_id: 1, received_onu_id: 1024, event: ONU-id-mismatch, can happen if both voltha and the olt rebooted
449 // so we're using the internal ID that is 1
450 // o.ID = msg.OnuID
Matteo Scandolo4747d292019-08-05 11:50:18 -0700451
452 indData := &openolt.Indication_OnuInd{OnuInd: &openolt.OnuIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700453 IntfId: o.PonPortID,
454 OnuId: o.ID,
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700455 OperState: msg.OperState.String(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700456 AdminState: o.OperState.Current(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700457 SerialNumber: o.SerialNumber,
458 }}
459 if err := stream.Send(&openolt.Indication{Data: indData}); err != nil {
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800460 // NOTE do we need to transition to a broken state?
Matteo Scandolo11006992019-08-28 11:29:46 -0700461 log.Errorf("Failed to send Indication_OnuInd: %v", err)
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700462 return
Matteo Scandolo4747d292019-08-05 11:50:18 -0700463 }
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700464 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700465 "IntfId": o.PonPortID,
466 "OnuId": o.ID,
467 "OperState": msg.OperState.String(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700468 "AdminState": msg.OperState.String(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700469 "OnuSn": o.Sn(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700470 }).Debug("Sent Indication_OnuInd")
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700471
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700472}
473
Pragya Arya324337e2020-02-20 14:35:08 +0530474func (o *Onu) publishOmciEvent(msg OmciMessage) {
475 if olt.PublishEvents {
476 _, _, msgType, _, _, _, err := omcisim.ParsePkt(HexDecode(msg.omciMsg.Pkt))
477 if err != nil {
478 log.Errorf("error in getting msgType %v", err)
479 return
480 }
481 if msgType == omcisim.MibUpload {
482 o.seqNumber = 0
483 publishEvent("MIB-upload-received", int32(o.PonPortID), int32(o.ID), common.OnuSnToString(o.SerialNumber))
484 } else if msgType == omcisim.MibUploadNext {
485 o.seqNumber++
486 if o.seqNumber > 290 {
487 publishEvent("MIB-upload-done", int32(o.PonPortID), int32(o.ID), common.OnuSnToString(o.SerialNumber))
488 }
489 }
490 }
491}
492
Scott Bakerb90c4312020-03-12 21:33:25 -0700493// Create a TestResponse packet and send it
494func (o *Onu) sendTestResult(msg OmciMessage, stream openolt.Openolt_EnableIndicationServer) error {
495 resp, err := omcilib.BuildTestResult(HexDecode(msg.omciMsg.Pkt))
496 if err != nil {
497 return err
498 }
499
500 var omciInd openolt.OmciIndication
501 omciInd.IntfId = o.PonPortID
502 omciInd.OnuId = o.ID
503 omciInd.Pkt = resp
504
505 omci := &openolt.Indication_OmciInd{OmciInd: &omciInd}
506 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
507 onuLogger.WithFields(log.Fields{
508 "IntfId": o.PonPortID,
509 "SerialNumber": o.Sn(),
510 "omciPacket": omciInd.Pkt,
511 "msg": msg,
512 }).Errorf("send TestResult omcisim indication failed: %v", err)
513 return err
514 }
515 onuLogger.WithFields(log.Fields{
516 "IntfId": o.PonPortID,
517 "SerialNumber": o.Sn(),
518 "omciPacket": omciInd.Pkt,
519 }).Tracef("Sent TestResult OMCI message")
520
521 return nil
522}
523
William Kurkian0418bc82019-11-06 12:16:24 -0500524func (o *Onu) handleOmciMessage(msg OmciMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700525
526 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700527 "IntfId": o.PonPortID,
Matteo Scandolo27428702019-10-11 16:21:16 -0700528 "SerialNumber": o.Sn(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700529 "omciPacket": msg.omciMsg.Pkt,
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700530 }).Tracef("Received OMCI message")
531
Pragya Arya324337e2020-02-20 14:35:08 +0530532 o.publishOmciEvent(msg)
533
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700534 var omciInd openolt.OmciIndication
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700535 respPkt, err := omcisim.OmciSim(o.PonPortID, o.ID, HexDecode(msg.omciMsg.Pkt))
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700536 if err != nil {
Matteo Scandolo27428702019-10-11 16:21:16 -0700537 onuLogger.WithFields(log.Fields{
538 "IntfId": o.PonPortID,
539 "SerialNumber": o.Sn(),
540 "omciPacket": omciInd.Pkt,
541 "msg": msg,
542 }).Errorf("Error handling OMCI message %v", msg)
543 return
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700544 }
545
546 omciInd.IntfId = o.PonPortID
547 omciInd.OnuId = o.ID
548 omciInd.Pkt = respPkt
549
550 omci := &openolt.Indication_OmciInd{OmciInd: &omciInd}
551 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
Matteo Scandolo27428702019-10-11 16:21:16 -0700552 onuLogger.WithFields(log.Fields{
553 "IntfId": o.PonPortID,
554 "SerialNumber": o.Sn(),
555 "omciPacket": omciInd.Pkt,
556 "msg": msg,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700557 }).Errorf("send omcisim indication failed: %v", err)
Matteo Scandolo27428702019-10-11 16:21:16 -0700558 return
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700559 }
560 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700561 "IntfId": o.PonPortID,
Matteo Scandolo27428702019-10-11 16:21:16 -0700562 "SerialNumber": o.Sn(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700563 "omciPacket": omciInd.Pkt,
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700564 }).Tracef("Sent OMCI message")
Scott Bakerb90c4312020-03-12 21:33:25 -0700565
566 // Test message is special, it requires sending two packets:
567 // first packet: TestResponse, says whether test was started successully, handled by omci-sim
568 // second packet, TestResult, reports the result of running the self-test
569 // TestResult can come some time after a TestResponse
570 // TODO: Implement some delay between the TestResponse and the TestResult
571 isTest, err := omcilib.IsTestRequest(HexDecode(msg.omciMsg.Pkt))
572 if (err == nil) && (isTest) {
Shrey Baid688b4242020-07-10 20:40:10 +0530573 _ = o.sendTestResult(msg, stream)
Scott Bakerb90c4312020-03-12 21:33:25 -0700574 }
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700575}
576
Matteo Scandolo27428702019-10-11 16:21:16 -0700577func (o *Onu) storePortNumber(portNo uint32) {
Matteo Scandolo813402b2019-10-23 19:24:52 -0700578 // NOTE this needed only as long as we don't support multiple UNIs
Matteo Scandolo27428702019-10-11 16:21:16 -0700579 // we need to add support for multiple UNIs
580 // the action plan is:
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700581 // - refactor the omcisim-sim library to use https://github.com/cboling/omci instead of canned messages
Matteo Scandolo27428702019-10-11 16:21:16 -0700582 // - change the library so that it reports a single UNI and remove this workaroung
583 // - add support for multiple UNIs in BBSim
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700584 if o.PortNo == 0 || portNo < o.PortNo {
Matteo Scandolo813402b2019-10-23 19:24:52 -0700585 onuLogger.WithFields(log.Fields{
586 "IntfId": o.PonPortID,
587 "OnuId": o.ID,
588 "SerialNumber": o.Sn(),
589 "OnuPortNo": o.PortNo,
590 "FlowPortNo": portNo,
591 }).Debug("Storing ONU portNo")
Matteo Scandolo27428702019-10-11 16:21:16 -0700592 o.PortNo = portNo
593 }
594}
595
William Kurkian0418bc82019-11-06 12:16:24 -0500596func (o *Onu) SetID(id uint32) {
Matteo Scandolo583f17d2020-02-13 10:35:17 -0800597 onuLogger.WithFields(log.Fields{
598 "IntfId": o.PonPortID,
599 "OnuId": id,
600 "SerialNumber": o.Sn(),
601 }).Debug("Storing OnuId ")
William Kurkian0418bc82019-11-06 12:16:24 -0500602 o.ID = id
603}
604
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700605func (o *Onu) handleFlowAdd(msg OnuFlowUpdateMessage) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700606 onuLogger.WithFields(log.Fields{
Matteo Scandoloedf30c72020-09-18 18:15:28 -0700607 "Cookie": msg.Flow.Cookie,
608 "DstPort": msg.Flow.Classifier.DstPort,
609 "FlowId": msg.Flow.FlowId,
610 "FlowType": msg.Flow.FlowType,
611 "GemportId": msg.Flow.GemportId,
612 "InnerVlan": msg.Flow.Classifier.IVid,
613 "IntfId": msg.Flow.AccessIntfId,
614 "IpProto": msg.Flow.Classifier.IpProto,
615 "OnuId": msg.Flow.OnuId,
616 "OnuSn": o.Sn(),
617 "OuterVlan": msg.Flow.Classifier.OVid,
618 "PortNo": msg.Flow.PortNo,
619 "SrcPort": msg.Flow.Classifier.SrcPort,
620 "UniID": msg.Flow.UniId,
621 "ClassifierEthType": fmt.Sprintf("%x", msg.Flow.Classifier.EthType),
622 "ClassifierOPbits": msg.Flow.Classifier.OPbits,
623 "ClassifierIVid": msg.Flow.Classifier.IVid,
624 "ClassifierOVid": msg.Flow.Classifier.OVid,
625 }).Debug("OLT receives FlowAdd for ONU")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700626
Matteo Scandolo813402b2019-10-23 19:24:52 -0700627 if msg.Flow.UniId != 0 {
628 // as of now BBSim only support a single UNI, so ignore everything that is not targeted to it
629 onuLogger.WithFields(log.Fields{
630 "IntfId": o.PonPortID,
631 "OnuId": o.ID,
632 "SerialNumber": o.Sn(),
633 }).Debug("Ignoring flow as it's not for the first UNI")
634 return
635 }
636
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700637 o.FlowIds = append(o.FlowIds, msg.Flow.FlowId)
Matteo Scandolo4a036262020-08-17 15:56:13 -0700638 o.addGemPortToService(uint32(msg.Flow.GemportId), msg.Flow.Classifier.EthType, msg.Flow.Classifier.OVid, msg.Flow.Classifier.IVid)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700639
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700640 if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeEAPOL) && msg.Flow.Classifier.OVid == 4091 {
Matteo Scandolo27428702019-10-11 16:21:16 -0700641 // NOTE storing the PortNO, it's needed when sending PacketIndications
Matteo Scandolo813402b2019-10-23 19:24:52 -0700642 o.storePortNumber(uint32(msg.Flow.PortNo))
Matteo Scandolo4a036262020-08-17 15:56:13 -0700643
644 for _, s := range o.Services {
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700645 s.HandleAuth()
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700646 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700647 } else if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeIPv4) &&
648 msg.Flow.Classifier.SrcPort == uint32(68) &&
Matteo Scandolobd875b32020-09-18 17:46:31 -0700649 msg.Flow.Classifier.DstPort == uint32(67) {
Matteo Scandolo99f18462019-10-28 14:14:28 -0700650
Matteo Scandolo4a036262020-08-17 15:56:13 -0700651 for _, s := range o.Services {
Matteo Scandolobd875b32020-09-18 17:46:31 -0700652 s.HandleDhcp(uint8(msg.Flow.Classifier.OPbits), int(msg.Flow.Classifier.OVid))
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700653 }
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700654 }
655}
656
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700657func (o *Onu) handleFlowRemove(msg OnuFlowUpdateMessage) {
658 onuLogger.WithFields(log.Fields{
659 "IntfId": o.PonPortID,
660 "OnuId": o.ID,
661 "SerialNumber": o.Sn(),
662 "FlowId": msg.Flow.FlowId,
663 "FlowType": msg.Flow.FlowType,
664 }).Debug("ONU receives FlowRemove")
665
666 for idx, flow := range o.FlowIds {
667 // If the gemport is found, delete it from local cache.
668 if flow == msg.Flow.FlowId {
669 o.FlowIds = append(o.FlowIds[:idx], o.FlowIds[idx+1:]...)
670 break
671 }
672 }
673
674 if len(o.FlowIds) == 0 {
675 onuLogger.WithFields(log.Fields{
676 "IntfId": o.PonPortID,
677 "OnuId": o.ID,
678 "SerialNumber": o.Sn(),
679 }).Info("Resetting GemPort")
680 o.GemPortAdded = false
681
Hardik Windlass7b3405b2020-07-08 15:10:05 +0530682 // check if ONU delete is performed and
683 // terminate the ONU's ProcessOnuMessages Go routine
684 if o.InternalState.Current() == "disabled" {
685 close(o.Channel)
686 }
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700687 }
688}
689
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700690// HexDecode converts the hex encoding to binary
691func HexDecode(pkt []byte) []byte {
692 p := make([]byte, len(pkt)/2)
693 for i, j := 0, 0; i < len(pkt); i, j = i+2, j+1 {
694 // Go figure this ;)
695 u := (pkt[i] & 15) + (pkt[i]>>6)*9
696 l := (pkt[i+1] & 15) + (pkt[i+1]>>6)*9
697 p[j] = u<<4 + l
698 }
699 onuLogger.Tracef("Omci decoded: %x.", p)
700 return p
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700701}
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700702
703// BBR methods
704
705func sendOmciMsg(pktBytes []byte, intfId uint32, onuId uint32, sn *openolt.SerialNumber, msgType string, client openolt.OpenoltClient) {
706 omciMsg := openolt.OmciMsg{
707 IntfId: intfId,
708 OnuId: onuId,
709 Pkt: pktBytes,
710 }
711
712 if _, err := client.OmciMsgOut(context.Background(), &omciMsg); err != nil {
713 log.WithFields(log.Fields{
714 "IntfId": intfId,
715 "OnuId": onuId,
716 "SerialNumber": common.OnuSnToString(sn),
717 "Pkt": omciMsg.Pkt,
718 }).Fatalf("Failed to send MIB Reset")
719 }
720 log.WithFields(log.Fields{
721 "IntfId": intfId,
722 "OnuId": onuId,
723 "SerialNumber": common.OnuSnToString(sn),
724 "Pkt": omciMsg.Pkt,
725 }).Tracef("Sent OMCI message %s", msgType)
726}
727
728func (onu *Onu) getNextTid(highPriority ...bool) uint16 {
729 var next uint16
730 if len(highPriority) > 0 && highPriority[0] {
731 next = onu.hpTid
732 onu.hpTid += 1
733 if onu.hpTid < 0x8000 {
734 onu.hpTid = 0x8000
735 }
736 } else {
737 next = onu.tid
738 onu.tid += 1
739 if onu.tid >= 0x8000 {
740 onu.tid = 1
741 }
742 }
743 return next
744}
745
746// TODO move this method in responders/omcisim
747func (o *Onu) StartOmci(client openolt.OpenoltClient) {
748 mibReset, _ := omcilib.CreateMibResetRequest(o.getNextTid(false))
749 sendOmciMsg(mibReset, o.PonPortID, o.ID, o.SerialNumber, "mibReset", client)
750}
751
752func (o *Onu) handleOmci(msg OmciIndicationMessage, client openolt.OpenoltClient) {
753 msgType, packet := omcilib.DecodeOmci(msg.OmciInd.Pkt)
754
755 log.WithFields(log.Fields{
756 "IntfId": msg.OmciInd.IntfId,
757 "OnuId": msg.OmciInd.OnuId,
758 "OnuSn": common.OnuSnToString(o.SerialNumber),
759 "Pkt": msg.OmciInd.Pkt,
760 "msgType": msgType,
Anand S Katti09541352020-01-29 15:54:01 +0530761 }).Trace("ONU Receives OMCI Msg")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700762 switch msgType {
763 default:
Matteo Scandolo813402b2019-10-23 19:24:52 -0700764 log.WithFields(log.Fields{
765 "IntfId": msg.OmciInd.IntfId,
766 "OnuId": msg.OmciInd.OnuId,
767 "OnuSn": common.OnuSnToString(o.SerialNumber),
768 "Pkt": msg.OmciInd.Pkt,
769 "msgType": msgType,
770 }).Fatalf("unexpected frame: %v", packet)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700771 case omci.MibResetResponseType:
772 mibUpload, _ := omcilib.CreateMibUploadRequest(o.getNextTid(false))
773 sendOmciMsg(mibUpload, o.PonPortID, o.ID, o.SerialNumber, "mibUpload", client)
774 case omci.MibUploadResponseType:
775 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
776 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
777 case omci.MibUploadNextResponseType:
778 o.seqNumber++
779
780 if o.seqNumber > 290 {
781 // NOTE we are done with the MIB Upload (290 is the number of messages the omci-sim library will respond to)
782 galEnet, _ := omcilib.CreateGalEnetRequest(o.getNextTid(false))
783 sendOmciMsg(galEnet, o.PonPortID, o.ID, o.SerialNumber, "CreateGalEnetRequest", client)
784 } else {
785 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
786 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
787 }
788 case omci.CreateResponseType:
789 // NOTE Creating a GemPort,
790 // BBsim actually doesn't care about the values, so we can do we want with the parameters
791 // In the same way we can create a GemPort even without setting up UNIs/TConts/...
792 // but we need the GemPort to trigger the state change
793
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700794 if !o.GemPortAdded {
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700795 // NOTE this sends a CreateRequestType and BBSim replies with a CreateResponseType
796 // thus we send this request only once
797 gemReq, _ := omcilib.CreateGemPortRequest(o.getNextTid(false))
798 sendOmciMsg(gemReq, o.PonPortID, o.ID, o.SerialNumber, "CreateGemPortRequest", client)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700799 o.GemPortAdded = true
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700800 } else {
801 if err := o.InternalState.Event("send_eapol_flow"); err != nil {
802 onuLogger.WithFields(log.Fields{
803 "OnuId": o.ID,
804 "IntfId": o.PonPortID,
805 "OnuSn": o.Sn(),
806 }).Errorf("Error while transitioning ONU State %v", err)
807 }
808 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700809 }
810}
811
812func (o *Onu) sendEapolFlow(client openolt.OpenoltClient) {
813
814 classifierProto := openolt.Classifier{
815 EthType: uint32(layers.EthernetTypeEAPOL),
816 OVid: 4091,
817 }
818
819 actionProto := openolt.Action{}
820
821 downstreamFlow := openolt.Flow{
822 AccessIntfId: int32(o.PonPortID),
823 OnuId: int32(o.ID),
Matteo Scandolo813402b2019-10-23 19:24:52 -0700824 UniId: int32(0), // NOTE do not hardcode this, we need to support multiple UNIs
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700825 FlowId: uint32(o.ID),
826 FlowType: "downstream",
827 AllocId: int32(0),
828 NetworkIntfId: int32(0),
829 GemportId: int32(1), // FIXME use the same value as CreateGemPortRequest PortID, do not hardcode
830 Classifier: &classifierProto,
831 Action: &actionProto,
832 Priority: int32(100),
833 Cookie: uint64(o.ID),
834 PortNo: uint32(o.ID), // NOTE we are using this to map an incoming packetIndication to an ONU
835 }
836
837 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
838 log.WithFields(log.Fields{
839 "IntfId": o.PonPortID,
840 "OnuId": o.ID,
841 "FlowId": downstreamFlow.FlowId,
842 "PortNo": downstreamFlow.PortNo,
843 "SerialNumber": common.OnuSnToString(o.SerialNumber),
Matteo Scandolob0e3e622020-04-23 17:00:29 -0700844 }).Fatalf("Failed to add EAPOL Flow")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700845 }
846 log.WithFields(log.Fields{
847 "IntfId": o.PonPortID,
848 "OnuId": o.ID,
849 "FlowId": downstreamFlow.FlowId,
850 "PortNo": downstreamFlow.PortNo,
851 "SerialNumber": common.OnuSnToString(o.SerialNumber),
852 }).Info("Sent EAPOL Flow")
853}
854
855func (o *Onu) sendDhcpFlow(client openolt.OpenoltClient) {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700856
857 // BBR only works with a single service (ATT HSIA)
858 hsia := o.Services[0].(*Service)
859
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700860 classifierProto := openolt.Classifier{
861 EthType: uint32(layers.EthernetTypeIPv4),
862 SrcPort: uint32(68),
863 DstPort: uint32(67),
Matteo Scandolo4a036262020-08-17 15:56:13 -0700864 OVid: uint32(hsia.CTag),
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700865 }
866
867 actionProto := openolt.Action{}
868
869 downstreamFlow := openolt.Flow{
870 AccessIntfId: int32(o.PonPortID),
871 OnuId: int32(o.ID),
Matteo Scandolo813402b2019-10-23 19:24:52 -0700872 UniId: int32(0), // FIXME do not hardcode this
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700873 FlowId: uint32(o.ID),
874 FlowType: "downstream",
875 AllocId: int32(0),
876 NetworkIntfId: int32(0),
877 GemportId: int32(1), // FIXME use the same value as CreateGemPortRequest PortID, do not hardcode
878 Classifier: &classifierProto,
879 Action: &actionProto,
880 Priority: int32(100),
881 Cookie: uint64(o.ID),
882 PortNo: uint32(o.ID), // NOTE we are using this to map an incoming packetIndication to an ONU
883 }
884
885 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
886 log.WithFields(log.Fields{
887 "IntfId": o.PonPortID,
888 "OnuId": o.ID,
889 "FlowId": downstreamFlow.FlowId,
890 "PortNo": downstreamFlow.PortNo,
891 "SerialNumber": common.OnuSnToString(o.SerialNumber),
892 }).Fatalf("Failed to send DHCP Flow")
893 }
894 log.WithFields(log.Fields{
895 "IntfId": o.PonPortID,
896 "OnuId": o.ID,
897 "FlowId": downstreamFlow.FlowId,
898 "PortNo": downstreamFlow.PortNo,
899 "SerialNumber": common.OnuSnToString(o.SerialNumber),
900 }).Info("Sent DHCP Flow")
901}
Pragya Arya8bdb4532020-03-02 17:08:09 +0530902
903// DeleteFlow method search and delete flowKey from the onu flows slice
904func (onu *Onu) DeleteFlow(key FlowKey) {
905 for pos, flowKey := range onu.Flows {
906 if flowKey == key {
907 // delete the flowKey by shifting all flowKeys by one
908 onu.Flows = append(onu.Flows[:pos], onu.Flows[pos+1:]...)
909 t := make([]FlowKey, len(onu.Flows))
910 copy(t, onu.Flows)
911 onu.Flows = t
912 break
913 }
914 }
915}
Hardik Windlass7b3405b2020-07-08 15:10:05 +0530916
917func (onu *Onu) ReDiscoverOnu() {
918 // Wait for few seconds to be sure of the cleanup
919 time.Sleep(5 * time.Second)
920
921 onuLogger.WithFields(log.Fields{
922 "IntfId": onu.PonPortID,
923 "OnuId": onu.ID,
924 "OnuSn": onu.Sn(),
925 }).Debug("Send ONU Re-Discovery")
926
927 // ONU Re-Discovery
928 if err := onu.InternalState.Event("initialize"); err != nil {
929 log.WithFields(log.Fields{
930 "IntfId": onu.PonPortID,
931 "OnuSn": onu.Sn(),
932 "OnuId": onu.ID,
933 }).Infof("Failed to transition ONU to initialized state: %s", err.Error())
934 }
935
936 if err := onu.InternalState.Event("discover"); err != nil {
937 log.WithFields(log.Fields{
938 "IntfId": onu.PonPortID,
939 "OnuSn": onu.Sn(),
940 "OnuId": onu.ID,
941 }).Infof("Failed to transition ONU to discovered state: %s", err.Error())
942 }
943}
Matteo Scandolo4a036262020-08-17 15:56:13 -0700944
945func (onu *Onu) addGemPortToService(gemport uint32, ethType uint32, oVlan uint32, iVlan uint32) {
946 for _, s := range onu.Services {
947 if service, ok := s.(*Service); ok {
948 // EAPOL is a strange case, as packets are untagged
949 // but we assume we will have a single service requiring EAPOL
950 if ethType == uint32(layers.EthernetTypeEAPOL) && service.NeedsEapol {
951 service.GemPort = gemport
952 }
953
954 // For DHCP services we single tag the outgoing packets,
955 // thus the flow only contains the CTag and we can use that to match the service
956 if ethType == uint32(layers.EthernetTypeIPv4) && service.NeedsDhcp && service.CTag == int(oVlan) {
957 service.GemPort = gemport
958 }
959
960 // for dataplane services match both C and S tags
961 if service.CTag == int(iVlan) && service.STag == int(oVlan) {
962 service.GemPort = gemport
963 }
964 }
965 }
966}
967
968func (onu *Onu) findServiceByMacAddress(macAddress net.HardwareAddr) (*Service, error) {
969 for _, s := range onu.Services {
970 service := s.(*Service)
971 if service.HwAddress.String() == macAddress.String() {
972 return service, nil
973 }
974 }
975 return nil, fmt.Errorf("cannot-find-service-with-mac-address-%s", macAddress.String())
976}