blob: 42c48bb5fb41626a3dbba1713d9a552a6be3b147 [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 Scandolo4f4ac792020-10-01 16:33:21 -070037 "github.com/opencord/voltha-protos/v4/go/openolt"
38 "github.com/opencord/voltha-protos/v4/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 {
Matteo Scandolo4f4ac792020-10-01 16:33:21 -070047 ID uint64
Pragya Arya8bdb4532020-03-02 17:08:09 +053048 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
Matteo Scandolo4f4ac792020-10-01 16:33:21 -070070 FlowIds []uint64 // 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,
Matteo Scandolo4f4ac792020-10-01 16:33:21 -0700625 "ReplicateFlow": msg.Flow.ReplicateFlow,
626 "PbitToGemport": msg.Flow.PbitToGemport,
Matteo Scandoloedf30c72020-09-18 18:15:28 -0700627 }).Debug("OLT receives FlowAdd for ONU")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700628
Matteo Scandolo813402b2019-10-23 19:24:52 -0700629 if msg.Flow.UniId != 0 {
630 // as of now BBSim only support a single UNI, so ignore everything that is not targeted to it
631 onuLogger.WithFields(log.Fields{
632 "IntfId": o.PonPortID,
633 "OnuId": o.ID,
634 "SerialNumber": o.Sn(),
635 }).Debug("Ignoring flow as it's not for the first UNI")
636 return
637 }
638
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700639 o.FlowIds = append(o.FlowIds, msg.Flow.FlowId)
Matteo Scandolo4f4ac792020-10-01 16:33:21 -0700640
641 var gemPortId uint32
642 if msg.Flow.ReplicateFlow {
643 // This means that the OLT should replicate the flow for each PBIT, for BBSim it's enough to use the
644 // first available gemport (we only need to send one packet)
645 // NOTE different TP may create different mapping between PBits and GemPorts, this may require some changes
646 gemPortId = msg.Flow.PbitToGemport[0]
647 } else {
648 // if replicateFlows is false, then the flow is carrying the correct GemPortId
649 gemPortId = uint32(msg.Flow.GemportId)
650 }
651 o.addGemPortToService(gemPortId, msg.Flow.Classifier.EthType, msg.Flow.Classifier.OVid, msg.Flow.Classifier.IVid)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700652
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700653 if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeEAPOL) && msg.Flow.Classifier.OVid == 4091 {
Matteo Scandolo27428702019-10-11 16:21:16 -0700654 // NOTE storing the PortNO, it's needed when sending PacketIndications
Matteo Scandolo813402b2019-10-23 19:24:52 -0700655 o.storePortNumber(uint32(msg.Flow.PortNo))
Matteo Scandolo4a036262020-08-17 15:56:13 -0700656
657 for _, s := range o.Services {
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700658 s.HandleAuth()
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700659 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700660 } else if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeIPv4) &&
661 msg.Flow.Classifier.SrcPort == uint32(68) &&
Matteo Scandolobd875b32020-09-18 17:46:31 -0700662 msg.Flow.Classifier.DstPort == uint32(67) {
Matteo Scandolo99f18462019-10-28 14:14:28 -0700663
Matteo Scandolo4a036262020-08-17 15:56:13 -0700664 for _, s := range o.Services {
Matteo Scandolobd875b32020-09-18 17:46:31 -0700665 s.HandleDhcp(uint8(msg.Flow.Classifier.OPbits), int(msg.Flow.Classifier.OVid))
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700666 }
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700667 }
668}
669
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700670func (o *Onu) handleFlowRemove(msg OnuFlowUpdateMessage) {
671 onuLogger.WithFields(log.Fields{
672 "IntfId": o.PonPortID,
673 "OnuId": o.ID,
674 "SerialNumber": o.Sn(),
675 "FlowId": msg.Flow.FlowId,
676 "FlowType": msg.Flow.FlowType,
677 }).Debug("ONU receives FlowRemove")
678
679 for idx, flow := range o.FlowIds {
680 // If the gemport is found, delete it from local cache.
681 if flow == msg.Flow.FlowId {
682 o.FlowIds = append(o.FlowIds[:idx], o.FlowIds[idx+1:]...)
683 break
684 }
685 }
686
687 if len(o.FlowIds) == 0 {
688 onuLogger.WithFields(log.Fields{
689 "IntfId": o.PonPortID,
690 "OnuId": o.ID,
691 "SerialNumber": o.Sn(),
692 }).Info("Resetting GemPort")
693 o.GemPortAdded = false
694
Hardik Windlass7b3405b2020-07-08 15:10:05 +0530695 // check if ONU delete is performed and
696 // terminate the ONU's ProcessOnuMessages Go routine
697 if o.InternalState.Current() == "disabled" {
698 close(o.Channel)
699 }
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700700 }
701}
702
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700703// HexDecode converts the hex encoding to binary
704func HexDecode(pkt []byte) []byte {
705 p := make([]byte, len(pkt)/2)
706 for i, j := 0, 0; i < len(pkt); i, j = i+2, j+1 {
707 // Go figure this ;)
708 u := (pkt[i] & 15) + (pkt[i]>>6)*9
709 l := (pkt[i+1] & 15) + (pkt[i+1]>>6)*9
710 p[j] = u<<4 + l
711 }
712 onuLogger.Tracef("Omci decoded: %x.", p)
713 return p
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700714}
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700715
716// BBR methods
717
718func sendOmciMsg(pktBytes []byte, intfId uint32, onuId uint32, sn *openolt.SerialNumber, msgType string, client openolt.OpenoltClient) {
719 omciMsg := openolt.OmciMsg{
720 IntfId: intfId,
721 OnuId: onuId,
722 Pkt: pktBytes,
723 }
724
725 if _, err := client.OmciMsgOut(context.Background(), &omciMsg); err != nil {
726 log.WithFields(log.Fields{
727 "IntfId": intfId,
728 "OnuId": onuId,
729 "SerialNumber": common.OnuSnToString(sn),
730 "Pkt": omciMsg.Pkt,
731 }).Fatalf("Failed to send MIB Reset")
732 }
733 log.WithFields(log.Fields{
734 "IntfId": intfId,
735 "OnuId": onuId,
736 "SerialNumber": common.OnuSnToString(sn),
737 "Pkt": omciMsg.Pkt,
738 }).Tracef("Sent OMCI message %s", msgType)
739}
740
741func (onu *Onu) getNextTid(highPriority ...bool) uint16 {
742 var next uint16
743 if len(highPriority) > 0 && highPriority[0] {
744 next = onu.hpTid
745 onu.hpTid += 1
746 if onu.hpTid < 0x8000 {
747 onu.hpTid = 0x8000
748 }
749 } else {
750 next = onu.tid
751 onu.tid += 1
752 if onu.tid >= 0x8000 {
753 onu.tid = 1
754 }
755 }
756 return next
757}
758
759// TODO move this method in responders/omcisim
760func (o *Onu) StartOmci(client openolt.OpenoltClient) {
761 mibReset, _ := omcilib.CreateMibResetRequest(o.getNextTid(false))
762 sendOmciMsg(mibReset, o.PonPortID, o.ID, o.SerialNumber, "mibReset", client)
763}
764
765func (o *Onu) handleOmci(msg OmciIndicationMessage, client openolt.OpenoltClient) {
766 msgType, packet := omcilib.DecodeOmci(msg.OmciInd.Pkt)
767
768 log.WithFields(log.Fields{
769 "IntfId": msg.OmciInd.IntfId,
770 "OnuId": msg.OmciInd.OnuId,
771 "OnuSn": common.OnuSnToString(o.SerialNumber),
772 "Pkt": msg.OmciInd.Pkt,
773 "msgType": msgType,
Anand S Katti09541352020-01-29 15:54:01 +0530774 }).Trace("ONU Receives OMCI Msg")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700775 switch msgType {
776 default:
Matteo Scandolo813402b2019-10-23 19:24:52 -0700777 log.WithFields(log.Fields{
778 "IntfId": msg.OmciInd.IntfId,
779 "OnuId": msg.OmciInd.OnuId,
780 "OnuSn": common.OnuSnToString(o.SerialNumber),
781 "Pkt": msg.OmciInd.Pkt,
782 "msgType": msgType,
783 }).Fatalf("unexpected frame: %v", packet)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700784 case omci.MibResetResponseType:
785 mibUpload, _ := omcilib.CreateMibUploadRequest(o.getNextTid(false))
786 sendOmciMsg(mibUpload, o.PonPortID, o.ID, o.SerialNumber, "mibUpload", client)
787 case omci.MibUploadResponseType:
788 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
789 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
790 case omci.MibUploadNextResponseType:
791 o.seqNumber++
792
793 if o.seqNumber > 290 {
794 // NOTE we are done with the MIB Upload (290 is the number of messages the omci-sim library will respond to)
795 galEnet, _ := omcilib.CreateGalEnetRequest(o.getNextTid(false))
796 sendOmciMsg(galEnet, o.PonPortID, o.ID, o.SerialNumber, "CreateGalEnetRequest", client)
797 } else {
798 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
799 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
800 }
801 case omci.CreateResponseType:
802 // NOTE Creating a GemPort,
803 // BBsim actually doesn't care about the values, so we can do we want with the parameters
804 // In the same way we can create a GemPort even without setting up UNIs/TConts/...
805 // but we need the GemPort to trigger the state change
806
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700807 if !o.GemPortAdded {
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700808 // NOTE this sends a CreateRequestType and BBSim replies with a CreateResponseType
809 // thus we send this request only once
810 gemReq, _ := omcilib.CreateGemPortRequest(o.getNextTid(false))
811 sendOmciMsg(gemReq, o.PonPortID, o.ID, o.SerialNumber, "CreateGemPortRequest", client)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700812 o.GemPortAdded = true
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700813 } else {
814 if err := o.InternalState.Event("send_eapol_flow"); err != nil {
815 onuLogger.WithFields(log.Fields{
816 "OnuId": o.ID,
817 "IntfId": o.PonPortID,
818 "OnuSn": o.Sn(),
819 }).Errorf("Error while transitioning ONU State %v", err)
820 }
821 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700822 }
823}
824
825func (o *Onu) sendEapolFlow(client openolt.OpenoltClient) {
826
827 classifierProto := openolt.Classifier{
828 EthType: uint32(layers.EthernetTypeEAPOL),
829 OVid: 4091,
830 }
831
832 actionProto := openolt.Action{}
833
834 downstreamFlow := openolt.Flow{
835 AccessIntfId: int32(o.PonPortID),
836 OnuId: int32(o.ID),
Matteo Scandolo813402b2019-10-23 19:24:52 -0700837 UniId: int32(0), // NOTE do not hardcode this, we need to support multiple UNIs
Matteo Scandolo4f4ac792020-10-01 16:33:21 -0700838 FlowId: uint64(o.ID),
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700839 FlowType: "downstream",
840 AllocId: int32(0),
841 NetworkIntfId: int32(0),
842 GemportId: int32(1), // FIXME use the same value as CreateGemPortRequest PortID, do not hardcode
843 Classifier: &classifierProto,
844 Action: &actionProto,
845 Priority: int32(100),
846 Cookie: uint64(o.ID),
847 PortNo: uint32(o.ID), // NOTE we are using this to map an incoming packetIndication to an ONU
848 }
849
850 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
851 log.WithFields(log.Fields{
852 "IntfId": o.PonPortID,
853 "OnuId": o.ID,
854 "FlowId": downstreamFlow.FlowId,
855 "PortNo": downstreamFlow.PortNo,
856 "SerialNumber": common.OnuSnToString(o.SerialNumber),
Matteo Scandolob0e3e622020-04-23 17:00:29 -0700857 }).Fatalf("Failed to add EAPOL Flow")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700858 }
859 log.WithFields(log.Fields{
860 "IntfId": o.PonPortID,
861 "OnuId": o.ID,
862 "FlowId": downstreamFlow.FlowId,
863 "PortNo": downstreamFlow.PortNo,
864 "SerialNumber": common.OnuSnToString(o.SerialNumber),
865 }).Info("Sent EAPOL Flow")
866}
867
868func (o *Onu) sendDhcpFlow(client openolt.OpenoltClient) {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700869
870 // BBR only works with a single service (ATT HSIA)
871 hsia := o.Services[0].(*Service)
872
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700873 classifierProto := openolt.Classifier{
874 EthType: uint32(layers.EthernetTypeIPv4),
875 SrcPort: uint32(68),
876 DstPort: uint32(67),
Matteo Scandolo4a036262020-08-17 15:56:13 -0700877 OVid: uint32(hsia.CTag),
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700878 }
879
880 actionProto := openolt.Action{}
881
882 downstreamFlow := openolt.Flow{
883 AccessIntfId: int32(o.PonPortID),
884 OnuId: int32(o.ID),
Matteo Scandolo813402b2019-10-23 19:24:52 -0700885 UniId: int32(0), // FIXME do not hardcode this
Matteo Scandolo4f4ac792020-10-01 16:33:21 -0700886 FlowId: uint64(o.ID),
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700887 FlowType: "downstream",
888 AllocId: int32(0),
889 NetworkIntfId: int32(0),
890 GemportId: int32(1), // FIXME use the same value as CreateGemPortRequest PortID, do not hardcode
891 Classifier: &classifierProto,
892 Action: &actionProto,
893 Priority: int32(100),
894 Cookie: uint64(o.ID),
895 PortNo: uint32(o.ID), // NOTE we are using this to map an incoming packetIndication to an ONU
896 }
897
898 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
899 log.WithFields(log.Fields{
900 "IntfId": o.PonPortID,
901 "OnuId": o.ID,
902 "FlowId": downstreamFlow.FlowId,
903 "PortNo": downstreamFlow.PortNo,
904 "SerialNumber": common.OnuSnToString(o.SerialNumber),
905 }).Fatalf("Failed to send DHCP Flow")
906 }
907 log.WithFields(log.Fields{
908 "IntfId": o.PonPortID,
909 "OnuId": o.ID,
910 "FlowId": downstreamFlow.FlowId,
911 "PortNo": downstreamFlow.PortNo,
912 "SerialNumber": common.OnuSnToString(o.SerialNumber),
913 }).Info("Sent DHCP Flow")
914}
Pragya Arya8bdb4532020-03-02 17:08:09 +0530915
916// DeleteFlow method search and delete flowKey from the onu flows slice
917func (onu *Onu) DeleteFlow(key FlowKey) {
918 for pos, flowKey := range onu.Flows {
919 if flowKey == key {
920 // delete the flowKey by shifting all flowKeys by one
921 onu.Flows = append(onu.Flows[:pos], onu.Flows[pos+1:]...)
922 t := make([]FlowKey, len(onu.Flows))
923 copy(t, onu.Flows)
924 onu.Flows = t
925 break
926 }
927 }
928}
Hardik Windlass7b3405b2020-07-08 15:10:05 +0530929
930func (onu *Onu) ReDiscoverOnu() {
931 // Wait for few seconds to be sure of the cleanup
932 time.Sleep(5 * time.Second)
933
934 onuLogger.WithFields(log.Fields{
935 "IntfId": onu.PonPortID,
936 "OnuId": onu.ID,
937 "OnuSn": onu.Sn(),
938 }).Debug("Send ONU Re-Discovery")
939
940 // ONU Re-Discovery
941 if err := onu.InternalState.Event("initialize"); err != nil {
942 log.WithFields(log.Fields{
943 "IntfId": onu.PonPortID,
944 "OnuSn": onu.Sn(),
945 "OnuId": onu.ID,
946 }).Infof("Failed to transition ONU to initialized state: %s", err.Error())
947 }
948
949 if err := onu.InternalState.Event("discover"); err != nil {
950 log.WithFields(log.Fields{
951 "IntfId": onu.PonPortID,
952 "OnuSn": onu.Sn(),
953 "OnuId": onu.ID,
954 }).Infof("Failed to transition ONU to discovered state: %s", err.Error())
955 }
956}
Matteo Scandolo4a036262020-08-17 15:56:13 -0700957
958func (onu *Onu) addGemPortToService(gemport uint32, ethType uint32, oVlan uint32, iVlan uint32) {
959 for _, s := range onu.Services {
960 if service, ok := s.(*Service); ok {
961 // EAPOL is a strange case, as packets are untagged
962 // but we assume we will have a single service requiring EAPOL
963 if ethType == uint32(layers.EthernetTypeEAPOL) && service.NeedsEapol {
964 service.GemPort = gemport
965 }
966
967 // For DHCP services we single tag the outgoing packets,
968 // thus the flow only contains the CTag and we can use that to match the service
969 if ethType == uint32(layers.EthernetTypeIPv4) && service.NeedsDhcp && service.CTag == int(oVlan) {
970 service.GemPort = gemport
971 }
972
973 // for dataplane services match both C and S tags
974 if service.CTag == int(iVlan) && service.STag == int(oVlan) {
975 service.GemPort = gemport
976 }
977 }
978 }
979}
980
981func (onu *Onu) findServiceByMacAddress(macAddress net.HardwareAddr) (*Service, error) {
982 for _, s := range onu.Services {
983 service := s.(*Service)
984 if service.HwAddress.String() == macAddress.String() {
985 return service, nil
986 }
987 }
988 return nil, fmt.Errorf("cannot-find-service-with-mac-address-%s", macAddress.String())
989}