blob: 5300b389eac0a90d3b93d0fd56b0a5d259621393 [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"},
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700131 {Name: "receive_eapol_flow", Src: []string{"enabled", "gem_port_added"}, Dst: "eapol_flow_received"},
132 {Name: "add_gem_port", Src: []string{"enabled", "eapol_flow_received"}, Dst: "gem_port_added"},
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100133 // NOTE should disabled state be different for oper_disabled (emulating an error) and admin_disabled (received a disabled call via VOLTHA)?
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800134 {Name: "disable", Src: []string{"enabled", "eap_response_success_received", "auth_failed", "dhcp_ack_received", "dhcp_failed", "pon_disabled"}, Dst: "disabled"},
Pragya Arya6a708d62020-01-01 17:17:20 +0530135 // ONU state when PON port is disabled but ONU is power ON(more states should be added in src?)
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800136 {Name: "pon_disabled", Src: []string{"enabled", "eap_response_success_received", "auth_failed", "dhcp_ack_received", "dhcp_failed"}, Dst: "pon_disabled"},
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700137 // BBR States
138 // TODO add start OMCI state
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100139 {Name: "send_eapol_flow", Src: []string{"initialized"}, Dst: "eapol_flow_sent"},
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700140 {Name: "send_dhcp_flow", Src: []string{"eapol_flow_sent"}, Dst: "dhcp_flow_sent"},
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700141 },
142 fsm.Callbacks{
143 "enter_state": func(e *fsm.Event) {
144 o.logStateChange(e.Src, e.Dst)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700145 },
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100146 "enter_initialized": func(e *fsm.Event) {
147 // create new channel for ProcessOnuMessages Go routine
148 o.Channel = make(chan Message, 2048)
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800149
150 if err := o.OperState.Event("enable"); err != nil {
151 onuLogger.WithFields(log.Fields{
152 "OnuId": o.ID,
153 "IntfId": o.PonPortID,
154 "OnuSn": o.Sn(),
155 }).Errorf("Cannot change ONU OperState to up: %s", err.Error())
156 }
157
Pragya Arya1cbefa42020-01-13 12:15:29 +0530158 if !isMock {
159 // start ProcessOnuMessages Go routine
Matteo Scandolo4a036262020-08-17 15:56:13 -0700160 go o.ProcessOnuMessages(olt.enableContext, olt.OpenoltStream, nil)
Pragya Arya1cbefa42020-01-13 12:15:29 +0530161 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100162 },
163 "enter_discovered": func(e *fsm.Event) {
164 msg := Message{
165 Type: OnuDiscIndication,
166 Data: OnuDiscIndicationMessage{
167 Onu: &o,
168 OperState: UP,
169 },
170 }
171 o.Channel <- msg
172 },
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700173 "enter_enabled": func(event *fsm.Event) {
174 msg := Message{
175 Type: OnuIndication,
176 Data: OnuIndicationMessage{
177 OnuSN: o.SerialNumber,
178 PonPortID: o.PonPortID,
179 OperState: UP,
180 },
181 }
182 o.Channel <- msg
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700183
184 // Once the ONU is enabled start listening for packets
185 for _, s := range o.Services {
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700186 s.Initialize(o.PonPort.Olt.OpenoltStream)
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700187 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700188 },
189 "enter_disabled": func(event *fsm.Event) {
Matteo Scandolo47ef64b2020-04-20 14:16:07 -0700190
191 // clean the ONU state
Matteo Scandolo328c5f02020-06-26 14:16:39 -0700192 o.GemPortAdded = false
Matteo Scandolo47ef64b2020-04-20 14:16:07 -0700193 o.PortNo = 0
194 o.Flows = []FlowKey{}
195
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700196 // set the OperState to disabled
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800197 if err := o.OperState.Event("disable"); err != nil {
198 onuLogger.WithFields(log.Fields{
199 "OnuId": o.ID,
200 "IntfId": o.PonPortID,
201 "OnuSn": o.Sn(),
202 }).Errorf("Cannot change ONU OperState to down: %s", err.Error())
203 }
Matteo Scandolo47ef64b2020-04-20 14:16:07 -0700204
205 // send the OnuIndication DOWN event
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700206 msg := Message{
207 Type: OnuIndication,
208 Data: OnuIndicationMessage{
209 OnuSN: o.SerialNumber,
210 PonPortID: o.PonPortID,
211 OperState: DOWN,
212 },
213 }
214 o.Channel <- msg
Hardik Windlass7b3405b2020-07-08 15:10:05 +0530215
216 // verify all the flows removes are handled and
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100217 // terminate the ONU's ProcessOnuMessages Go routine
Hardik Windlass7b3405b2020-07-08 15:10:05 +0530218 if len(o.FlowIds) == 0 {
219 close(o.Channel)
220 }
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700221
222 for _, s := range o.Services {
223 s.Disable()
224 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700225 },
Matteo Scandolo4a036262020-08-17 15:56:13 -0700226 // BBR states
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700227 "enter_eapol_flow_sent": func(e *fsm.Event) {
228 msg := Message{
229 Type: SendEapolFlow,
230 }
231 o.Channel <- msg
232 },
233 "enter_dhcp_flow_sent": func(e *fsm.Event) {
234 msg := Message{
235 Type: SendDhcpFlow,
236 }
237 o.Channel <- msg
238 },
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700239 },
240 )
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100241
Matteo Scandolo27428702019-10-11 16:21:16 -0700242 return &o
Matteo Scandolo4747d292019-08-05 11:50:18 -0700243}
244
William Kurkian0418bc82019-11-06 12:16:24 -0500245func (o *Onu) logStateChange(src string, dst string) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700246 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700247 "OnuId": o.ID,
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700248 "IntfId": o.PonPortID,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700249 "OnuSn": o.Sn(),
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700250 }).Debugf("Changing ONU InternalState from %s to %s", src, dst)
251}
252
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100253// ProcessOnuMessages starts indication channel for each ONU
David Bainbridge103cf022019-12-16 20:11:35 +0000254func (o *Onu) ProcessOnuMessages(ctx context.Context, stream openolt.Openolt_EnableIndicationServer, client openolt.OpenoltClient) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700255 onuLogger.WithFields(log.Fields{
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100256 "onuID": o.ID,
257 "onuSN": o.Sn(),
258 "ponPort": o.PonPortID,
259 }).Debug("Starting ONU Indication Channel")
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700260
David Bainbridge103cf022019-12-16 20:11:35 +0000261loop:
262 for {
263 select {
264 case <-ctx.Done():
265 onuLogger.WithFields(log.Fields{
266 "onuID": o.ID,
267 "onuSN": o.Sn(),
268 }).Tracef("ONU message handling canceled via context")
269 break loop
270 case message, ok := <-o.Channel:
271 if !ok || ctx.Err() != nil {
272 onuLogger.WithFields(log.Fields{
273 "onuID": o.ID,
274 "onuSN": o.Sn(),
275 }).Tracef("ONU message handling canceled via channel close")
276 break loop
Matteo Scandolo075b1892019-10-07 12:11:07 -0700277 }
David Bainbridge103cf022019-12-16 20:11:35 +0000278 onuLogger.WithFields(log.Fields{
279 "onuID": o.ID,
280 "onuSN": o.Sn(),
281 "messageType": message.Type,
282 }).Tracef("Received message on ONU Channel")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700283
David Bainbridge103cf022019-12-16 20:11:35 +0000284 switch message.Type {
285 case OnuDiscIndication:
286 msg, _ := message.Data.(OnuDiscIndicationMessage)
287 // 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 +0530288 time.Sleep(o.DiscoveryDelay)
David Bainbridge103cf022019-12-16 20:11:35 +0000289 o.sendOnuDiscIndication(msg, stream)
290 case OnuIndication:
291 msg, _ := message.Data.(OnuIndicationMessage)
292 o.sendOnuIndication(msg, stream)
293 case OMCI:
294 msg, _ := message.Data.(OmciMessage)
295 o.handleOmciMessage(msg, stream)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700296 case FlowAdd:
David Bainbridge103cf022019-12-16 20:11:35 +0000297 msg, _ := message.Data.(OnuFlowUpdateMessage)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700298 o.handleFlowAdd(msg)
299 case FlowRemoved:
300 msg, _ := message.Data.(OnuFlowUpdateMessage)
301 o.handleFlowRemove(msg)
David Bainbridge103cf022019-12-16 20:11:35 +0000302 case OnuPacketOut:
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700303
David Bainbridge103cf022019-12-16 20:11:35 +0000304 msg, _ := message.Data.(OnuPacketMessage)
305
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700306 onuLogger.WithFields(log.Fields{
David Bainbridge103cf022019-12-16 20:11:35 +0000307 "IntfId": msg.IntfId,
308 "OnuId": msg.OnuId,
309 "pktType": msg.Type,
310 }).Trace("Received OnuPacketOut Message")
311
Matteo Scandolo618a6582020-09-09 12:21:29 -0700312 if msg.Type == packetHandlers.EAPOL || msg.Type == packetHandlers.DHCP {
313
314 service, err := o.findServiceByMacAddress(msg.MacAddress)
315 if err != nil {
316 onuLogger.WithFields(log.Fields{
317 "IntfId": msg.IntfId,
318 "OnuId": msg.OnuId,
319 "pktType": msg.Type,
320 "MacAddress": msg.MacAddress,
321 "Pkt": hex.EncodeToString(msg.Packet.Data()),
322 "OnuSn": o.Sn(),
323 }).Error("Cannot find Service associated with packet")
324 return
325 }
326 service.PacketCh <- msg
327 } else if msg.Type == packetHandlers.IGMP {
328 // if it's an IGMP packet we assume we have a single IGMP service
329 for _, s := range o.Services {
330 service := s.(*Service)
331
332 if service.NeedsIgmp {
333 service.PacketCh <- msg
334 }
335 }
David Bainbridge103cf022019-12-16 20:11:35 +0000336 }
Matteo Scandolo4a036262020-08-17 15:56:13 -0700337
David Bainbridge103cf022019-12-16 20:11:35 +0000338 case OnuPacketIn:
339 // NOTE we only receive BBR packets here.
340 // Eapol.HandleNextPacket can handle both BBSim and BBr cases so the call is the same
341 // in the DHCP case VOLTHA only act as a proxy, the behaviour is completely different thus we have a dhcp.HandleNextBbrPacket
342 msg, _ := message.Data.(OnuPacketMessage)
343
344 log.WithFields(log.Fields{
345 "IntfId": msg.IntfId,
346 "OnuId": msg.OnuId,
347 "pktType": msg.Type,
348 }).Trace("Received OnuPacketIn Message")
349
350 if msg.Type == packetHandlers.EAPOL {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700351 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 +0000352 } else if msg.Type == packetHandlers.DHCP {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700353 _ = dhcp.HandleNextBbrPacket(o.ID, o.PonPortID, o.Sn(), o.DoneChannel, msg.Packet, client)
David Bainbridge103cf022019-12-16 20:11:35 +0000354 }
Matteo Scandolo7f656fb2020-09-08 15:18:15 -0700355 // BBR specific messages
David Bainbridge103cf022019-12-16 20:11:35 +0000356 case OmciIndication:
357 msg, _ := message.Data.(OmciIndicationMessage)
358 o.handleOmci(msg, client)
359 case SendEapolFlow:
360 o.sendEapolFlow(client)
361 case SendDhcpFlow:
362 o.sendDhcpFlow(client)
363 default:
364 onuLogger.Warnf("Received unknown message data %v for type %v in OLT Channel", message.Data, message.Type)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700365 }
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700366 }
367 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100368 onuLogger.WithFields(log.Fields{
369 "onuID": o.ID,
370 "onuSN": o.Sn(),
371 }).Debug("Stopped handling ONU Indication Channel")
Matteo Scandolo4747d292019-08-05 11:50:18 -0700372}
373
Matteo Scandolodf3f85d2020-01-15 12:50:48 -0800374func (o *Onu) processOmciMessage(message omcisim.OmciChMessage, stream openolt.Openolt_EnableIndicationServer) {
William Kurkian9dadc5b2019-10-22 13:51:57 -0400375 switch message.Type {
Matteo Scandolodf3f85d2020-01-15 12:50:48 -0800376 case omcisim.UniLinkUp, omcisim.UniLinkDown:
377 onuLogger.WithFields(log.Fields{
378 "OnuId": message.Data.OnuId,
379 "IntfId": message.Data.IntfId,
380 "Type": message.Type,
Matteo Scandolo4a036262020-08-17 15:56:13 -0700381 }).Debug("UNI Link Alarm")
Matteo Scandolodf3f85d2020-01-15 12:50:48 -0800382 // TODO send to OLT
383
384 omciInd := openolt.OmciIndication{
385 IntfId: message.Data.IntfId,
386 OnuId: message.Data.OnuId,
387 Pkt: message.Packet,
388 }
389
390 omci := &openolt.Indication_OmciInd{OmciInd: &omciInd}
391 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
392 onuLogger.WithFields(log.Fields{
393 "IntfId": o.PonPortID,
394 "SerialNumber": o.Sn(),
395 "Type": message.Type,
396 "omciPacket": omciInd.Pkt,
397 }).Errorf("Failed to send UNI Link Alarm: %v", err)
398 return
399 }
400
401 onuLogger.WithFields(log.Fields{
402 "IntfId": o.PonPortID,
403 "SerialNumber": o.Sn(),
404 "Type": message.Type,
405 "omciPacket": omciInd.Pkt,
Matteo Scandolo4a036262020-08-17 15:56:13 -0700406 }).Debug("UNI Link alarm sent")
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700407 }
408}
409
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100410func (o Onu) NewSN(oltid int, intfid uint32, onuid uint32) *openolt.SerialNumber {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700411
412 sn := new(openolt.SerialNumber)
413
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700414 //sn = new(openolt.SerialNumber)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700415 sn.VendorId = []byte("BBSM")
416 sn.VendorSpecific = []byte{0, byte(oltid % 256), byte(intfid), byte(onuid)}
417
418 return sn
419}
420
William Kurkian0418bc82019-11-06 12:16:24 -0500421func (o *Onu) sendOnuDiscIndication(msg OnuDiscIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700422 discoverData := &openolt.Indication_OnuDiscInd{OnuDiscInd: &openolt.OnuDiscIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700423 IntfId: msg.Onu.PonPortID,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700424 SerialNumber: msg.Onu.SerialNumber,
425 }}
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700426
Matteo Scandolo4747d292019-08-05 11:50:18 -0700427 if err := stream.Send(&openolt.Indication{Data: discoverData}); err != nil {
Matteo Scandolo11006992019-08-28 11:29:46 -0700428 log.Errorf("Failed to send Indication_OnuDiscInd: %v", err)
Matteo Scandolo99f18462019-10-28 14:14:28 -0700429 return
Matteo Scandolo4747d292019-08-05 11:50:18 -0700430 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700431
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700432 onuLogger.WithFields(log.Fields{
Matteo Scandolo4747d292019-08-05 11:50:18 -0700433 "IntfId": msg.Onu.PonPortID,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700434 "OnuSn": msg.Onu.Sn(),
435 "OnuId": o.ID,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700436 }).Debug("Sent Indication_OnuDiscInd")
Pragya Arya324337e2020-02-20 14:35:08 +0530437 publishEvent("ONU-discovery-indication-sent", int32(msg.Onu.PonPortID), int32(o.ID), msg.Onu.Sn())
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800438
439 // after DiscoveryRetryDelay check if the state is the same and in case send a new OnuDiscIndication
440 go func(delay time.Duration) {
Matteo Scandolo569e7172019-12-20 11:51:51 -0800441 time.Sleep(delay)
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800442 if o.InternalState.Current() == "discovered" {
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800443 o.sendOnuDiscIndication(msg, stream)
444 }
445 }(o.DiscoveryRetryDelay)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700446}
447
William Kurkian0418bc82019-11-06 12:16:24 -0500448func (o *Onu) sendOnuIndication(msg OnuIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700449 // NOTE voltha returns an ID, but if we use that ID then it complains:
450 // expected_onu_id: 1, received_onu_id: 1024, event: ONU-id-mismatch, can happen if both voltha and the olt rebooted
451 // so we're using the internal ID that is 1
452 // o.ID = msg.OnuID
Matteo Scandolo4747d292019-08-05 11:50:18 -0700453
454 indData := &openolt.Indication_OnuInd{OnuInd: &openolt.OnuIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700455 IntfId: o.PonPortID,
456 OnuId: o.ID,
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700457 OperState: msg.OperState.String(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700458 AdminState: o.OperState.Current(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700459 SerialNumber: o.SerialNumber,
460 }}
461 if err := stream.Send(&openolt.Indication{Data: indData}); err != nil {
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800462 // NOTE do we need to transition to a broken state?
Matteo Scandolo11006992019-08-28 11:29:46 -0700463 log.Errorf("Failed to send Indication_OnuInd: %v", err)
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700464 return
Matteo Scandolo4747d292019-08-05 11:50:18 -0700465 }
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700466 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700467 "IntfId": o.PonPortID,
468 "OnuId": o.ID,
469 "OperState": msg.OperState.String(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700470 "AdminState": msg.OperState.String(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700471 "OnuSn": o.Sn(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700472 }).Debug("Sent Indication_OnuInd")
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700473
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700474}
475
Pragya Arya324337e2020-02-20 14:35:08 +0530476func (o *Onu) publishOmciEvent(msg OmciMessage) {
477 if olt.PublishEvents {
478 _, _, msgType, _, _, _, err := omcisim.ParsePkt(HexDecode(msg.omciMsg.Pkt))
479 if err != nil {
480 log.Errorf("error in getting msgType %v", err)
481 return
482 }
483 if msgType == omcisim.MibUpload {
484 o.seqNumber = 0
485 publishEvent("MIB-upload-received", int32(o.PonPortID), int32(o.ID), common.OnuSnToString(o.SerialNumber))
486 } else if msgType == omcisim.MibUploadNext {
487 o.seqNumber++
488 if o.seqNumber > 290 {
489 publishEvent("MIB-upload-done", int32(o.PonPortID), int32(o.ID), common.OnuSnToString(o.SerialNumber))
490 }
491 }
492 }
493}
494
Scott Bakerb90c4312020-03-12 21:33:25 -0700495// Create a TestResponse packet and send it
496func (o *Onu) sendTestResult(msg OmciMessage, stream openolt.Openolt_EnableIndicationServer) error {
497 resp, err := omcilib.BuildTestResult(HexDecode(msg.omciMsg.Pkt))
498 if err != nil {
499 return err
500 }
501
502 var omciInd openolt.OmciIndication
503 omciInd.IntfId = o.PonPortID
504 omciInd.OnuId = o.ID
505 omciInd.Pkt = resp
506
507 omci := &openolt.Indication_OmciInd{OmciInd: &omciInd}
508 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
509 onuLogger.WithFields(log.Fields{
510 "IntfId": o.PonPortID,
511 "SerialNumber": o.Sn(),
512 "omciPacket": omciInd.Pkt,
513 "msg": msg,
514 }).Errorf("send TestResult omcisim indication failed: %v", err)
515 return err
516 }
517 onuLogger.WithFields(log.Fields{
518 "IntfId": o.PonPortID,
519 "SerialNumber": o.Sn(),
520 "omciPacket": omciInd.Pkt,
521 }).Tracef("Sent TestResult OMCI message")
522
523 return nil
524}
525
William Kurkian0418bc82019-11-06 12:16:24 -0500526func (o *Onu) handleOmciMessage(msg OmciMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700527
528 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700529 "IntfId": o.PonPortID,
Matteo Scandolo27428702019-10-11 16:21:16 -0700530 "SerialNumber": o.Sn(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700531 "omciPacket": msg.omciMsg.Pkt,
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700532 }).Tracef("Received OMCI message")
533
Pragya Arya324337e2020-02-20 14:35:08 +0530534 o.publishOmciEvent(msg)
535
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700536 var omciInd openolt.OmciIndication
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700537 respPkt, err := omcisim.OmciSim(o.PonPortID, o.ID, HexDecode(msg.omciMsg.Pkt))
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700538 if err != nil {
Matteo Scandolo27428702019-10-11 16:21:16 -0700539 onuLogger.WithFields(log.Fields{
540 "IntfId": o.PonPortID,
541 "SerialNumber": o.Sn(),
542 "omciPacket": omciInd.Pkt,
543 "msg": msg,
544 }).Errorf("Error handling OMCI message %v", msg)
545 return
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700546 }
547
548 omciInd.IntfId = o.PonPortID
549 omciInd.OnuId = o.ID
550 omciInd.Pkt = respPkt
551
552 omci := &openolt.Indication_OmciInd{OmciInd: &omciInd}
553 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
Matteo Scandolo27428702019-10-11 16:21:16 -0700554 onuLogger.WithFields(log.Fields{
555 "IntfId": o.PonPortID,
556 "SerialNumber": o.Sn(),
557 "omciPacket": omciInd.Pkt,
558 "msg": msg,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700559 }).Errorf("send omcisim indication failed: %v", err)
Matteo Scandolo27428702019-10-11 16:21:16 -0700560 return
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700561 }
562 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700563 "IntfId": o.PonPortID,
Matteo Scandolo27428702019-10-11 16:21:16 -0700564 "SerialNumber": o.Sn(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700565 "omciPacket": omciInd.Pkt,
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700566 }).Tracef("Sent OMCI message")
Scott Bakerb90c4312020-03-12 21:33:25 -0700567
568 // Test message is special, it requires sending two packets:
569 // first packet: TestResponse, says whether test was started successully, handled by omci-sim
570 // second packet, TestResult, reports the result of running the self-test
571 // TestResult can come some time after a TestResponse
572 // TODO: Implement some delay between the TestResponse and the TestResult
573 isTest, err := omcilib.IsTestRequest(HexDecode(msg.omciMsg.Pkt))
574 if (err == nil) && (isTest) {
Shrey Baid688b4242020-07-10 20:40:10 +0530575 _ = o.sendTestResult(msg, stream)
Scott Bakerb90c4312020-03-12 21:33:25 -0700576 }
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700577}
578
Matteo Scandolo27428702019-10-11 16:21:16 -0700579func (o *Onu) storePortNumber(portNo uint32) {
Matteo Scandolo813402b2019-10-23 19:24:52 -0700580 // NOTE this needed only as long as we don't support multiple UNIs
Matteo Scandolo27428702019-10-11 16:21:16 -0700581 // we need to add support for multiple UNIs
582 // the action plan is:
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700583 // - refactor the omcisim-sim library to use https://github.com/cboling/omci instead of canned messages
Matteo Scandolo27428702019-10-11 16:21:16 -0700584 // - change the library so that it reports a single UNI and remove this workaroung
585 // - add support for multiple UNIs in BBSim
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700586 if o.PortNo == 0 || portNo < o.PortNo {
Matteo Scandolo813402b2019-10-23 19:24:52 -0700587 onuLogger.WithFields(log.Fields{
588 "IntfId": o.PonPortID,
589 "OnuId": o.ID,
590 "SerialNumber": o.Sn(),
591 "OnuPortNo": o.PortNo,
592 "FlowPortNo": portNo,
593 }).Debug("Storing ONU portNo")
Matteo Scandolo27428702019-10-11 16:21:16 -0700594 o.PortNo = portNo
595 }
596}
597
William Kurkian0418bc82019-11-06 12:16:24 -0500598func (o *Onu) SetID(id uint32) {
Matteo Scandolo583f17d2020-02-13 10:35:17 -0800599 onuLogger.WithFields(log.Fields{
600 "IntfId": o.PonPortID,
601 "OnuId": id,
602 "SerialNumber": o.Sn(),
603 }).Debug("Storing OnuId ")
William Kurkian0418bc82019-11-06 12:16:24 -0500604 o.ID = id
605}
606
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700607func (o *Onu) handleFlowAdd(msg OnuFlowUpdateMessage) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700608 onuLogger.WithFields(log.Fields{
Matteo Scandolo7f656fb2020-09-08 15:18:15 -0700609 "Cookie": msg.Flow.Cookie,
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800610 "DstPort": msg.Flow.Classifier.DstPort,
611 "EthType": fmt.Sprintf("%x", msg.Flow.Classifier.EthType),
612 "FlowId": msg.Flow.FlowId,
613 "FlowType": msg.Flow.FlowType,
614 "GemportId": msg.Flow.GemportId,
615 "InnerVlan": msg.Flow.Classifier.IVid,
616 "IntfId": msg.Flow.AccessIntfId,
617 "IpProto": msg.Flow.Classifier.IpProto,
618 "OnuId": msg.Flow.OnuId,
619 "OnuSn": o.Sn(),
620 "OuterVlan": msg.Flow.Classifier.OVid,
621 "PortNo": msg.Flow.PortNo,
622 "SrcPort": msg.Flow.Classifier.SrcPort,
623 "UniID": msg.Flow.UniId,
Matteo Scandolo3ac52792020-03-05 14:21:08 -0800624 "ClassifierOPbits": msg.Flow.Classifier.OPbits,
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700625 }).Debug("ONU receives FlowAdd")
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 Scandolo3ac52792020-03-05 14:21:08 -0800649 msg.Flow.Classifier.DstPort == uint32(67) &&
Matteo Scandolod74abba2020-04-16 16:36:44 -0700650 (msg.Flow.Classifier.OPbits == 0 || msg.Flow.Classifier.OPbits == 255) {
Matteo Scandolo99f18462019-10-28 14:14:28 -0700651
Matteo Scandolo4a036262020-08-17 15:56:13 -0700652 for _, s := range o.Services {
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700653 s.HandleDhcp(int(msg.Flow.Classifier.OVid))
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700654 }
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700655 }
656}
657
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700658func (o *Onu) handleFlowRemove(msg OnuFlowUpdateMessage) {
659 onuLogger.WithFields(log.Fields{
660 "IntfId": o.PonPortID,
661 "OnuId": o.ID,
662 "SerialNumber": o.Sn(),
663 "FlowId": msg.Flow.FlowId,
664 "FlowType": msg.Flow.FlowType,
665 }).Debug("ONU receives FlowRemove")
666
667 for idx, flow := range o.FlowIds {
668 // If the gemport is found, delete it from local cache.
669 if flow == msg.Flow.FlowId {
670 o.FlowIds = append(o.FlowIds[:idx], o.FlowIds[idx+1:]...)
671 break
672 }
673 }
674
675 if len(o.FlowIds) == 0 {
676 onuLogger.WithFields(log.Fields{
677 "IntfId": o.PonPortID,
678 "OnuId": o.ID,
679 "SerialNumber": o.Sn(),
680 }).Info("Resetting GemPort")
681 o.GemPortAdded = false
682
Hardik Windlass7b3405b2020-07-08 15:10:05 +0530683 // check if ONU delete is performed and
684 // terminate the ONU's ProcessOnuMessages Go routine
685 if o.InternalState.Current() == "disabled" {
686 close(o.Channel)
687 }
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700688 }
689}
690
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700691// HexDecode converts the hex encoding to binary
692func HexDecode(pkt []byte) []byte {
693 p := make([]byte, len(pkt)/2)
694 for i, j := 0, 0; i < len(pkt); i, j = i+2, j+1 {
695 // Go figure this ;)
696 u := (pkt[i] & 15) + (pkt[i]>>6)*9
697 l := (pkt[i+1] & 15) + (pkt[i+1]>>6)*9
698 p[j] = u<<4 + l
699 }
700 onuLogger.Tracef("Omci decoded: %x.", p)
701 return p
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700702}
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700703
704// BBR methods
705
706func sendOmciMsg(pktBytes []byte, intfId uint32, onuId uint32, sn *openolt.SerialNumber, msgType string, client openolt.OpenoltClient) {
707 omciMsg := openolt.OmciMsg{
708 IntfId: intfId,
709 OnuId: onuId,
710 Pkt: pktBytes,
711 }
712
713 if _, err := client.OmciMsgOut(context.Background(), &omciMsg); err != nil {
714 log.WithFields(log.Fields{
715 "IntfId": intfId,
716 "OnuId": onuId,
717 "SerialNumber": common.OnuSnToString(sn),
718 "Pkt": omciMsg.Pkt,
719 }).Fatalf("Failed to send MIB Reset")
720 }
721 log.WithFields(log.Fields{
722 "IntfId": intfId,
723 "OnuId": onuId,
724 "SerialNumber": common.OnuSnToString(sn),
725 "Pkt": omciMsg.Pkt,
726 }).Tracef("Sent OMCI message %s", msgType)
727}
728
729func (onu *Onu) getNextTid(highPriority ...bool) uint16 {
730 var next uint16
731 if len(highPriority) > 0 && highPriority[0] {
732 next = onu.hpTid
733 onu.hpTid += 1
734 if onu.hpTid < 0x8000 {
735 onu.hpTid = 0x8000
736 }
737 } else {
738 next = onu.tid
739 onu.tid += 1
740 if onu.tid >= 0x8000 {
741 onu.tid = 1
742 }
743 }
744 return next
745}
746
747// TODO move this method in responders/omcisim
748func (o *Onu) StartOmci(client openolt.OpenoltClient) {
749 mibReset, _ := omcilib.CreateMibResetRequest(o.getNextTid(false))
750 sendOmciMsg(mibReset, o.PonPortID, o.ID, o.SerialNumber, "mibReset", client)
751}
752
753func (o *Onu) handleOmci(msg OmciIndicationMessage, client openolt.OpenoltClient) {
754 msgType, packet := omcilib.DecodeOmci(msg.OmciInd.Pkt)
755
756 log.WithFields(log.Fields{
757 "IntfId": msg.OmciInd.IntfId,
758 "OnuId": msg.OmciInd.OnuId,
759 "OnuSn": common.OnuSnToString(o.SerialNumber),
760 "Pkt": msg.OmciInd.Pkt,
761 "msgType": msgType,
Anand S Katti09541352020-01-29 15:54:01 +0530762 }).Trace("ONU Receives OMCI Msg")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700763 switch msgType {
764 default:
Matteo Scandolo813402b2019-10-23 19:24:52 -0700765 log.WithFields(log.Fields{
766 "IntfId": msg.OmciInd.IntfId,
767 "OnuId": msg.OmciInd.OnuId,
768 "OnuSn": common.OnuSnToString(o.SerialNumber),
769 "Pkt": msg.OmciInd.Pkt,
770 "msgType": msgType,
771 }).Fatalf("unexpected frame: %v", packet)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700772 case omci.MibResetResponseType:
773 mibUpload, _ := omcilib.CreateMibUploadRequest(o.getNextTid(false))
774 sendOmciMsg(mibUpload, o.PonPortID, o.ID, o.SerialNumber, "mibUpload", client)
775 case omci.MibUploadResponseType:
776 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
777 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
778 case omci.MibUploadNextResponseType:
779 o.seqNumber++
780
781 if o.seqNumber > 290 {
782 // NOTE we are done with the MIB Upload (290 is the number of messages the omci-sim library will respond to)
783 galEnet, _ := omcilib.CreateGalEnetRequest(o.getNextTid(false))
784 sendOmciMsg(galEnet, o.PonPortID, o.ID, o.SerialNumber, "CreateGalEnetRequest", client)
785 } else {
786 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
787 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
788 }
789 case omci.CreateResponseType:
790 // NOTE Creating a GemPort,
791 // BBsim actually doesn't care about the values, so we can do we want with the parameters
792 // In the same way we can create a GemPort even without setting up UNIs/TConts/...
793 // but we need the GemPort to trigger the state change
794
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700795 if !o.GemPortAdded {
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700796 // NOTE this sends a CreateRequestType and BBSim replies with a CreateResponseType
797 // thus we send this request only once
798 gemReq, _ := omcilib.CreateGemPortRequest(o.getNextTid(false))
799 sendOmciMsg(gemReq, o.PonPortID, o.ID, o.SerialNumber, "CreateGemPortRequest", client)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700800 o.GemPortAdded = true
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700801 } else {
802 if err := o.InternalState.Event("send_eapol_flow"); err != nil {
803 onuLogger.WithFields(log.Fields{
804 "OnuId": o.ID,
805 "IntfId": o.PonPortID,
806 "OnuSn": o.Sn(),
807 }).Errorf("Error while transitioning ONU State %v", err)
808 }
809 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700810 }
811}
812
813func (o *Onu) sendEapolFlow(client openolt.OpenoltClient) {
814
815 classifierProto := openolt.Classifier{
816 EthType: uint32(layers.EthernetTypeEAPOL),
817 OVid: 4091,
818 }
819
820 actionProto := openolt.Action{}
821
822 downstreamFlow := openolt.Flow{
823 AccessIntfId: int32(o.PonPortID),
824 OnuId: int32(o.ID),
Matteo Scandolo813402b2019-10-23 19:24:52 -0700825 UniId: int32(0), // NOTE do not hardcode this, we need to support multiple UNIs
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700826 FlowId: uint32(o.ID),
827 FlowType: "downstream",
828 AllocId: int32(0),
829 NetworkIntfId: int32(0),
830 GemportId: int32(1), // FIXME use the same value as CreateGemPortRequest PortID, do not hardcode
831 Classifier: &classifierProto,
832 Action: &actionProto,
833 Priority: int32(100),
834 Cookie: uint64(o.ID),
835 PortNo: uint32(o.ID), // NOTE we are using this to map an incoming packetIndication to an ONU
836 }
837
838 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
839 log.WithFields(log.Fields{
840 "IntfId": o.PonPortID,
841 "OnuId": o.ID,
842 "FlowId": downstreamFlow.FlowId,
843 "PortNo": downstreamFlow.PortNo,
844 "SerialNumber": common.OnuSnToString(o.SerialNumber),
Matteo Scandolob0e3e622020-04-23 17:00:29 -0700845 }).Fatalf("Failed to add EAPOL Flow")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700846 }
847 log.WithFields(log.Fields{
848 "IntfId": o.PonPortID,
849 "OnuId": o.ID,
850 "FlowId": downstreamFlow.FlowId,
851 "PortNo": downstreamFlow.PortNo,
852 "SerialNumber": common.OnuSnToString(o.SerialNumber),
853 }).Info("Sent EAPOL Flow")
854}
855
856func (o *Onu) sendDhcpFlow(client openolt.OpenoltClient) {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700857
858 // BBR only works with a single service (ATT HSIA)
859 hsia := o.Services[0].(*Service)
860
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700861 classifierProto := openolt.Classifier{
862 EthType: uint32(layers.EthernetTypeIPv4),
863 SrcPort: uint32(68),
864 DstPort: uint32(67),
Matteo Scandolo4a036262020-08-17 15:56:13 -0700865 OVid: uint32(hsia.CTag),
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700866 }
867
868 actionProto := openolt.Action{}
869
870 downstreamFlow := openolt.Flow{
871 AccessIntfId: int32(o.PonPortID),
872 OnuId: int32(o.ID),
Matteo Scandolo813402b2019-10-23 19:24:52 -0700873 UniId: int32(0), // FIXME do not hardcode this
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700874 FlowId: uint32(o.ID),
875 FlowType: "downstream",
876 AllocId: int32(0),
877 NetworkIntfId: int32(0),
878 GemportId: int32(1), // FIXME use the same value as CreateGemPortRequest PortID, do not hardcode
879 Classifier: &classifierProto,
880 Action: &actionProto,
881 Priority: int32(100),
882 Cookie: uint64(o.ID),
883 PortNo: uint32(o.ID), // NOTE we are using this to map an incoming packetIndication to an ONU
884 }
885
886 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
887 log.WithFields(log.Fields{
888 "IntfId": o.PonPortID,
889 "OnuId": o.ID,
890 "FlowId": downstreamFlow.FlowId,
891 "PortNo": downstreamFlow.PortNo,
892 "SerialNumber": common.OnuSnToString(o.SerialNumber),
893 }).Fatalf("Failed to send DHCP Flow")
894 }
895 log.WithFields(log.Fields{
896 "IntfId": o.PonPortID,
897 "OnuId": o.ID,
898 "FlowId": downstreamFlow.FlowId,
899 "PortNo": downstreamFlow.PortNo,
900 "SerialNumber": common.OnuSnToString(o.SerialNumber),
901 }).Info("Sent DHCP Flow")
902}
Pragya Arya8bdb4532020-03-02 17:08:09 +0530903
904// DeleteFlow method search and delete flowKey from the onu flows slice
905func (onu *Onu) DeleteFlow(key FlowKey) {
906 for pos, flowKey := range onu.Flows {
907 if flowKey == key {
908 // delete the flowKey by shifting all flowKeys by one
909 onu.Flows = append(onu.Flows[:pos], onu.Flows[pos+1:]...)
910 t := make([]FlowKey, len(onu.Flows))
911 copy(t, onu.Flows)
912 onu.Flows = t
913 break
914 }
915 }
916}
Hardik Windlass7b3405b2020-07-08 15:10:05 +0530917
918func (onu *Onu) ReDiscoverOnu() {
919 // Wait for few seconds to be sure of the cleanup
920 time.Sleep(5 * time.Second)
921
922 onuLogger.WithFields(log.Fields{
923 "IntfId": onu.PonPortID,
924 "OnuId": onu.ID,
925 "OnuSn": onu.Sn(),
926 }).Debug("Send ONU Re-Discovery")
927
928 // ONU Re-Discovery
929 if err := onu.InternalState.Event("initialize"); err != nil {
930 log.WithFields(log.Fields{
931 "IntfId": onu.PonPortID,
932 "OnuSn": onu.Sn(),
933 "OnuId": onu.ID,
934 }).Infof("Failed to transition ONU to initialized state: %s", err.Error())
935 }
936
937 if err := onu.InternalState.Event("discover"); err != nil {
938 log.WithFields(log.Fields{
939 "IntfId": onu.PonPortID,
940 "OnuSn": onu.Sn(),
941 "OnuId": onu.ID,
942 }).Infof("Failed to transition ONU to discovered state: %s", err.Error())
943 }
944}
Matteo Scandolo4a036262020-08-17 15:56:13 -0700945
946func (onu *Onu) addGemPortToService(gemport uint32, ethType uint32, oVlan uint32, iVlan uint32) {
947 for _, s := range onu.Services {
948 if service, ok := s.(*Service); ok {
949 // EAPOL is a strange case, as packets are untagged
950 // but we assume we will have a single service requiring EAPOL
951 if ethType == uint32(layers.EthernetTypeEAPOL) && service.NeedsEapol {
952 service.GemPort = gemport
953 }
954
955 // For DHCP services we single tag the outgoing packets,
956 // thus the flow only contains the CTag and we can use that to match the service
957 if ethType == uint32(layers.EthernetTypeIPv4) && service.NeedsDhcp && service.CTag == int(oVlan) {
958 service.GemPort = gemport
959 }
960
961 // for dataplane services match both C and S tags
962 if service.CTag == int(iVlan) && service.STag == int(oVlan) {
963 service.GemPort = gemport
964 }
965 }
966 }
967}
968
969func (onu *Onu) findServiceByMacAddress(macAddress net.HardwareAddr) (*Service, error) {
970 for _, s := range onu.Services {
971 service := s.(*Service)
972 if service.HwAddress.String() == macAddress.String() {
973 return service, nil
974 }
975 }
976 return nil, fmt.Errorf("cannot-find-service-with-mac-address-%s", macAddress.String())
977}