blob: 80308da731dff8866dc2cbfc03748782d4a137e5 [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 Scandolo3bc73742019-08-20 14:04:04 -070021 "fmt"
Matteo Scandolo4a036262020-08-17 15:56:13 -070022 "github.com/opencord/bbsim/internal/bbsim/packetHandlers"
23 "github.com/opencord/bbsim/internal/bbsim/responders/dhcp"
24 "github.com/opencord/bbsim/internal/bbsim/responders/eapol"
Zdravko Bozakov681364d2019-11-10 14:28:46 +010025 "net"
26
27 "time"
28
Matteo Scandolo40e067f2019-10-16 16:59:41 -070029 "github.com/cboling/omci"
Matteo Scandolo3bc73742019-08-20 14:04:04 -070030 "github.com/google/gopacket/layers"
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -070031 "github.com/jpillora/backoff"
Matteo Scandolo4747d292019-08-05 11:50:18 -070032 "github.com/looplab/fsm"
Matteo Scandolo40e067f2019-10-16 16:59:41 -070033 "github.com/opencord/bbsim/internal/common"
34 omcilib "github.com/opencord/bbsim/internal/common/omci"
35 omcisim "github.com/opencord/omci-sim"
Matteo Scandolo3de9de02019-11-14 13:40:03 -080036 "github.com/opencord/voltha-protos/v2/go/openolt"
Matteo Scandolod74abba2020-04-16 16:36:44 -070037 "github.com/opencord/voltha-protos/v2/go/tech_profile"
Matteo Scandolo4747d292019-08-05 11:50:18 -070038 log "github.com/sirupsen/logrus"
39)
40
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070041var onuLogger = log.WithFields(log.Fields{
42 "module": "ONU",
43})
44
Pragya Arya8bdb4532020-03-02 17:08:09 +053045type FlowKey struct {
46 ID uint32
47 Direction string
48}
49
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070050type Onu struct {
Matteo Scandoloe811ae92019-12-10 17:50:14 -080051 ID uint32
52 PonPortID uint32
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -070053 PonPort *PonPort
Matteo Scandoloe811ae92019-12-10 17:50:14 -080054 InternalState *fsm.FSM
Pragya Arya2225f202020-01-29 18:05:01 +053055 DiscoveryRetryDelay time.Duration // this is the time between subsequent Discovery Indication
56 DiscoveryDelay time.Duration // this is the time to send the first Discovery Indication
Matteo Scandolo4a036262020-08-17 15:56:13 -070057
58 Services []ServiceIf
59
60 Backoff *backoff.Backoff
Matteo Scandoloe811ae92019-12-10 17:50:14 -080061 // ONU State
Matteo Scandolo27428702019-10-11 16:21:16 -070062 // PortNo comes with flows and it's used when sending packetIndications,
63 // There is one PortNo per UNI Port, for now we're only storing the first one
Matteo Scandolo47ef64b2020-04-20 14:16:07 -070064 // FIXME add support for multiple UNIs (each UNI has a different PortNo)
Matteo Scandolo75ed5b92020-09-03 09:03:16 -070065 PortNo uint32
66 // deprecated (gemPort is on a Service basis)
Matteo Scandolo4a036262020-08-17 15:56:13 -070067 GemPortAdded bool
68 Flows []FlowKey
69 FlowIds []uint32 // keep track of the flows we currently have in the ONU
Matteo Scandolo99f18462019-10-28 14:14:28 -070070
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070071 OperState *fsm.FSM
72 SerialNumber *openolt.SerialNumber
73
Matteo Scandolo4a036262020-08-17 15:56:13 -070074 Channel chan Message // this Channel is to track state changes OMCI messages, EAPOL and DHCP packets
Matteo Scandolo40e067f2019-10-16 16:59:41 -070075
76 // OMCI params
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -070077 tid uint16
78 hpTid uint16
79 seqNumber uint16
Matteo Scandolo40e067f2019-10-16 16:59:41 -070080
Anand S Katti09541352020-01-29 15:54:01 +053081 DoneChannel chan bool // this channel is used to signal once the onu is complete (when the struct is used by BBR)
82 TrafficSchedulers *tech_profile.TrafficSchedulers
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070083}
84
Matteo Scandolo99f18462019-10-28 14:14:28 -070085func (o *Onu) Sn() string {
Matteo Scandolo40e067f2019-10-16 16:59:41 -070086 return common.OnuSnToString(o.SerialNumber)
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070087}
88
Matteo Scandolo4a036262020-08-17 15:56:13 -070089func CreateONU(olt *OltDevice, pon *PonPort, id uint32, delay time.Duration, isMock bool) *Onu {
Shrey Baidf8abccc2020-06-15 19:41:22 +053090 b := &backoff.Backoff{
91 //These are the defaults
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -070092 Min: 5 * time.Second,
Shrey Baidf8abccc2020-06-15 19:41:22 +053093 Max: 35 * time.Second,
94 Factor: 1.5,
95 Jitter: false,
96 }
Matteo Scandolo4747d292019-08-05 11:50:18 -070097
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070098 o := Onu{
Matteo Scandolo4a036262020-08-17 15:56:13 -070099 ID: id,
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800100 PonPortID: pon.ID,
101 PonPort: pon,
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800102 PortNo: 0,
103 tid: 0x1,
104 hpTid: 0x8000,
105 seqNumber: 0,
106 DoneChannel: make(chan bool, 1),
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800107 GemPortAdded: false,
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800108 DiscoveryRetryDelay: 60 * time.Second, // this is used to send OnuDiscoveryIndications until an activate call is received
Pragya Arya8bdb4532020-03-02 17:08:09 +0530109 Flows: []FlowKey{},
Pragya Arya2225f202020-01-29 18:05:01 +0530110 DiscoveryDelay: delay,
Shrey Baidf8abccc2020-06-15 19:41:22 +0530111 Backoff: b,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700112 }
Pragya Arya2225f202020-01-29 18:05:01 +0530113 o.SerialNumber = o.NewSN(olt.ID, pon.ID, id)
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700114 // NOTE this state machine is used to track the operational
115 // state as requested by VOLTHA
116 o.OperState = getOperStateFSM(func(e *fsm.Event) {
117 onuLogger.WithFields(log.Fields{
118 "ID": o.ID,
119 }).Debugf("Changing ONU OperState from %s to %s", e.Src, e.Dst)
120 })
121
122 // NOTE this state machine is used to activate the OMCI, EAPOL and DHCP clients
123 o.InternalState = fsm.NewFSM(
124 "created",
125 fsm.Events{
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700126 // DEVICE Lifecycle
Hardik Windlassad790cb2020-06-17 21:26:22 +0530127 {Name: "initialize", Src: []string{"created", "disabled", "pon_disabled"}, Dst: "initialized"},
128 {Name: "discover", Src: []string{"initialized"}, Dst: "discovered"},
129 {Name: "enable", Src: []string{"discovered", "pon_disabled"}, Dst: "enabled"},
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700130 {Name: "receive_eapol_flow", Src: []string{"enabled", "gem_port_added"}, Dst: "eapol_flow_received"},
131 {Name: "add_gem_port", Src: []string{"enabled", "eapol_flow_received"}, Dst: "gem_port_added"},
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100132 // 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 -0800133 {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 +0530134 // 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 -0800135 {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 -0700136 // BBR States
137 // TODO add start OMCI state
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100138 {Name: "send_eapol_flow", Src: []string{"initialized"}, Dst: "eapol_flow_sent"},
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700139 {Name: "send_dhcp_flow", Src: []string{"eapol_flow_sent"}, Dst: "dhcp_flow_sent"},
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700140 },
141 fsm.Callbacks{
142 "enter_state": func(e *fsm.Event) {
143 o.logStateChange(e.Src, e.Dst)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700144 },
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100145 "enter_initialized": func(e *fsm.Event) {
146 // create new channel for ProcessOnuMessages Go routine
147 o.Channel = make(chan Message, 2048)
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800148
149 if err := o.OperState.Event("enable"); err != nil {
150 onuLogger.WithFields(log.Fields{
151 "OnuId": o.ID,
152 "IntfId": o.PonPortID,
153 "OnuSn": o.Sn(),
154 }).Errorf("Cannot change ONU OperState to up: %s", err.Error())
155 }
156
Pragya Arya1cbefa42020-01-13 12:15:29 +0530157 if !isMock {
158 // start ProcessOnuMessages Go routine
Matteo Scandolo4a036262020-08-17 15:56:13 -0700159 go o.ProcessOnuMessages(olt.enableContext, olt.OpenoltStream, nil)
Pragya Arya1cbefa42020-01-13 12:15:29 +0530160 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100161 },
162 "enter_discovered": func(e *fsm.Event) {
163 msg := Message{
164 Type: OnuDiscIndication,
165 Data: OnuDiscIndicationMessage{
166 Onu: &o,
167 OperState: UP,
168 },
169 }
170 o.Channel <- msg
171 },
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700172 "enter_enabled": func(event *fsm.Event) {
173 msg := Message{
174 Type: OnuIndication,
175 Data: OnuIndicationMessage{
176 OnuSN: o.SerialNumber,
177 PonPortID: o.PonPortID,
178 OperState: UP,
179 },
180 }
181 o.Channel <- msg
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700182
183 // Once the ONU is enabled start listening for packets
184 for _, s := range o.Services {
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700185 s.Initialize(o.PonPort.Olt.OpenoltStream)
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700186 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700187 },
188 "enter_disabled": func(event *fsm.Event) {
Matteo Scandolo47ef64b2020-04-20 14:16:07 -0700189
190 // clean the ONU state
Matteo Scandolo328c5f02020-06-26 14:16:39 -0700191 o.GemPortAdded = false
Matteo Scandolo47ef64b2020-04-20 14:16:07 -0700192 o.PortNo = 0
193 o.Flows = []FlowKey{}
194
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700195 // set the OperState to disabled
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800196 if err := o.OperState.Event("disable"); err != nil {
197 onuLogger.WithFields(log.Fields{
198 "OnuId": o.ID,
199 "IntfId": o.PonPortID,
200 "OnuSn": o.Sn(),
201 }).Errorf("Cannot change ONU OperState to down: %s", err.Error())
202 }
Matteo Scandolo47ef64b2020-04-20 14:16:07 -0700203
204 // send the OnuIndication DOWN event
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700205 msg := Message{
206 Type: OnuIndication,
207 Data: OnuIndicationMessage{
208 OnuSN: o.SerialNumber,
209 PonPortID: o.PonPortID,
210 OperState: DOWN,
211 },
212 }
213 o.Channel <- msg
Hardik Windlass7b3405b2020-07-08 15:10:05 +0530214
215 // verify all the flows removes are handled and
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100216 // terminate the ONU's ProcessOnuMessages Go routine
Hardik Windlass7b3405b2020-07-08 15:10:05 +0530217 if len(o.FlowIds) == 0 {
218 close(o.Channel)
219 }
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700220
221 for _, s := range o.Services {
222 s.Disable()
223 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700224 },
Matteo Scandolo4a036262020-08-17 15:56:13 -0700225 // BBR states
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700226 "enter_eapol_flow_sent": func(e *fsm.Event) {
227 msg := Message{
228 Type: SendEapolFlow,
229 }
230 o.Channel <- msg
231 },
232 "enter_dhcp_flow_sent": func(e *fsm.Event) {
233 msg := Message{
234 Type: SendDhcpFlow,
235 }
236 o.Channel <- msg
237 },
Arjun E K57a7fcb2020-01-30 06:44:45 +0000238 "igmp_join_start": func(e *fsm.Event) {
239 msg := Message{
240 Type: IGMPMembershipReportV2,
241 }
242 o.Channel <- msg
243 },
244 "igmp_leave": func(e *fsm.Event) {
245 msg := Message{
246 Type: IGMPLeaveGroup}
247 o.Channel <- msg
248 },
Anand S Katti09541352020-01-29 15:54:01 +0530249 "igmp_join_startv3": func(e *fsm.Event) {
250 msg := Message{
251 Type: IGMPMembershipReportV3,
252 }
253 o.Channel <- msg
254 },
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700255 },
256 )
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100257
Matteo Scandolo27428702019-10-11 16:21:16 -0700258 return &o
Matteo Scandolo4747d292019-08-05 11:50:18 -0700259}
260
William Kurkian0418bc82019-11-06 12:16:24 -0500261func (o *Onu) logStateChange(src string, dst string) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700262 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700263 "OnuId": o.ID,
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700264 "IntfId": o.PonPortID,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700265 "OnuSn": o.Sn(),
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700266 }).Debugf("Changing ONU InternalState from %s to %s", src, dst)
267}
268
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100269// ProcessOnuMessages starts indication channel for each ONU
David Bainbridge103cf022019-12-16 20:11:35 +0000270func (o *Onu) ProcessOnuMessages(ctx context.Context, stream openolt.Openolt_EnableIndicationServer, client openolt.OpenoltClient) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700271 onuLogger.WithFields(log.Fields{
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100272 "onuID": o.ID,
273 "onuSN": o.Sn(),
274 "ponPort": o.PonPortID,
275 }).Debug("Starting ONU Indication Channel")
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700276
David Bainbridge103cf022019-12-16 20:11:35 +0000277loop:
278 for {
279 select {
280 case <-ctx.Done():
281 onuLogger.WithFields(log.Fields{
282 "onuID": o.ID,
283 "onuSN": o.Sn(),
284 }).Tracef("ONU message handling canceled via context")
285 break loop
286 case message, ok := <-o.Channel:
287 if !ok || ctx.Err() != nil {
288 onuLogger.WithFields(log.Fields{
289 "onuID": o.ID,
290 "onuSN": o.Sn(),
291 }).Tracef("ONU message handling canceled via channel close")
292 break loop
Matteo Scandolo075b1892019-10-07 12:11:07 -0700293 }
David Bainbridge103cf022019-12-16 20:11:35 +0000294 onuLogger.WithFields(log.Fields{
295 "onuID": o.ID,
296 "onuSN": o.Sn(),
297 "messageType": message.Type,
298 }).Tracef("Received message on ONU Channel")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700299
David Bainbridge103cf022019-12-16 20:11:35 +0000300 switch message.Type {
301 case OnuDiscIndication:
302 msg, _ := message.Data.(OnuDiscIndicationMessage)
303 // 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 +0530304 time.Sleep(o.DiscoveryDelay)
David Bainbridge103cf022019-12-16 20:11:35 +0000305 o.sendOnuDiscIndication(msg, stream)
306 case OnuIndication:
307 msg, _ := message.Data.(OnuIndicationMessage)
308 o.sendOnuIndication(msg, stream)
309 case OMCI:
310 msg, _ := message.Data.(OmciMessage)
311 o.handleOmciMessage(msg, stream)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700312 case FlowAdd:
David Bainbridge103cf022019-12-16 20:11:35 +0000313 msg, _ := message.Data.(OnuFlowUpdateMessage)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700314 o.handleFlowAdd(msg)
315 case FlowRemoved:
316 msg, _ := message.Data.(OnuFlowUpdateMessage)
317 o.handleFlowRemove(msg)
David Bainbridge103cf022019-12-16 20:11:35 +0000318 case OnuPacketOut:
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700319
David Bainbridge103cf022019-12-16 20:11:35 +0000320 msg, _ := message.Data.(OnuPacketMessage)
321
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700322 onuLogger.WithFields(log.Fields{
David Bainbridge103cf022019-12-16 20:11:35 +0000323 "IntfId": msg.IntfId,
324 "OnuId": msg.OnuId,
325 "pktType": msg.Type,
326 }).Trace("Received OnuPacketOut Message")
327
Matteo Scandolo4a036262020-08-17 15:56:13 -0700328 service, err := o.findServiceByMacAddress(msg.MacAddress)
329 if err != nil {
330 onuLogger.WithFields(log.Fields{
331 "IntfId": msg.IntfId,
332 "OnuId": msg.OnuId,
333 "pktType": msg.Type,
334 "MacAddress": msg.MacAddress,
335 "OnuSn": o.Sn(),
336 }).Error("Cannot find Service associated with packet")
337 return
David Bainbridge103cf022019-12-16 20:11:35 +0000338 }
Matteo Scandolo4a036262020-08-17 15:56:13 -0700339
340 service.PacketCh <- msg
341
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700342 onuLogger.WithFields(log.Fields{
343 "IntfId": msg.IntfId,
344 "OnuId": msg.OnuId,
345 "pktType": msg.Type,
346 "ServiceName": service.Name,
347 }).Info("OnuPacketOut Sent on Service Packet channel")
348
David Bainbridge103cf022019-12-16 20:11:35 +0000349 case OnuPacketIn:
350 // NOTE we only receive BBR packets here.
351 // Eapol.HandleNextPacket can handle both BBSim and BBr cases so the call is the same
352 // in the DHCP case VOLTHA only act as a proxy, the behaviour is completely different thus we have a dhcp.HandleNextBbrPacket
353 msg, _ := message.Data.(OnuPacketMessage)
354
355 log.WithFields(log.Fields{
356 "IntfId": msg.IntfId,
357 "OnuId": msg.OnuId,
358 "pktType": msg.Type,
359 }).Trace("Received OnuPacketIn Message")
360
361 if msg.Type == packetHandlers.EAPOL {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700362 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 +0000363 } else if msg.Type == packetHandlers.DHCP {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700364 _ = dhcp.HandleNextBbrPacket(o.ID, o.PonPortID, o.Sn(), o.DoneChannel, msg.Packet, client)
David Bainbridge103cf022019-12-16 20:11:35 +0000365 }
Matteo Scandolo7f656fb2020-09-08 15:18:15 -0700366 // BBR specific messages
David Bainbridge103cf022019-12-16 20:11:35 +0000367 case OmciIndication:
368 msg, _ := message.Data.(OmciIndicationMessage)
369 o.handleOmci(msg, client)
370 case SendEapolFlow:
371 o.sendEapolFlow(client)
372 case SendDhcpFlow:
373 o.sendDhcpFlow(client)
374 default:
375 onuLogger.Warnf("Received unknown message data %v for type %v in OLT Channel", message.Data, message.Type)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700376 }
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700377 }
378 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100379 onuLogger.WithFields(log.Fields{
380 "onuID": o.ID,
381 "onuSN": o.Sn(),
382 }).Debug("Stopped handling ONU Indication Channel")
Matteo Scandolo4747d292019-08-05 11:50:18 -0700383}
384
Matteo Scandolodf3f85d2020-01-15 12:50:48 -0800385func (o *Onu) processOmciMessage(message omcisim.OmciChMessage, stream openolt.Openolt_EnableIndicationServer) {
William Kurkian9dadc5b2019-10-22 13:51:57 -0400386 switch message.Type {
Matteo Scandolodf3f85d2020-01-15 12:50:48 -0800387 case omcisim.UniLinkUp, omcisim.UniLinkDown:
388 onuLogger.WithFields(log.Fields{
389 "OnuId": message.Data.OnuId,
390 "IntfId": message.Data.IntfId,
391 "Type": message.Type,
Matteo Scandolo4a036262020-08-17 15:56:13 -0700392 }).Debug("UNI Link Alarm")
Matteo Scandolodf3f85d2020-01-15 12:50:48 -0800393 // TODO send to OLT
394
395 omciInd := openolt.OmciIndication{
396 IntfId: message.Data.IntfId,
397 OnuId: message.Data.OnuId,
398 Pkt: message.Packet,
399 }
400
401 omci := &openolt.Indication_OmciInd{OmciInd: &omciInd}
402 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
403 onuLogger.WithFields(log.Fields{
404 "IntfId": o.PonPortID,
405 "SerialNumber": o.Sn(),
406 "Type": message.Type,
407 "omciPacket": omciInd.Pkt,
408 }).Errorf("Failed to send UNI Link Alarm: %v", err)
409 return
410 }
411
412 onuLogger.WithFields(log.Fields{
413 "IntfId": o.PonPortID,
414 "SerialNumber": o.Sn(),
415 "Type": message.Type,
416 "omciPacket": omciInd.Pkt,
Matteo Scandolo4a036262020-08-17 15:56:13 -0700417 }).Debug("UNI Link alarm sent")
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700418 }
419}
420
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100421func (o Onu) NewSN(oltid int, intfid uint32, onuid uint32) *openolt.SerialNumber {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700422
423 sn := new(openolt.SerialNumber)
424
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700425 //sn = new(openolt.SerialNumber)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700426 sn.VendorId = []byte("BBSM")
427 sn.VendorSpecific = []byte{0, byte(oltid % 256), byte(intfid), byte(onuid)}
428
429 return sn
430}
431
William Kurkian0418bc82019-11-06 12:16:24 -0500432func (o *Onu) sendOnuDiscIndication(msg OnuDiscIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700433 discoverData := &openolt.Indication_OnuDiscInd{OnuDiscInd: &openolt.OnuDiscIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700434 IntfId: msg.Onu.PonPortID,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700435 SerialNumber: msg.Onu.SerialNumber,
436 }}
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700437
Matteo Scandolo4747d292019-08-05 11:50:18 -0700438 if err := stream.Send(&openolt.Indication{Data: discoverData}); err != nil {
Matteo Scandolo11006992019-08-28 11:29:46 -0700439 log.Errorf("Failed to send Indication_OnuDiscInd: %v", err)
Matteo Scandolo99f18462019-10-28 14:14:28 -0700440 return
Matteo Scandolo4747d292019-08-05 11:50:18 -0700441 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700442
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700443 onuLogger.WithFields(log.Fields{
Matteo Scandolo4747d292019-08-05 11:50:18 -0700444 "IntfId": msg.Onu.PonPortID,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700445 "OnuSn": msg.Onu.Sn(),
446 "OnuId": o.ID,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700447 }).Debug("Sent Indication_OnuDiscInd")
Pragya Arya324337e2020-02-20 14:35:08 +0530448 publishEvent("ONU-discovery-indication-sent", int32(msg.Onu.PonPortID), int32(o.ID), msg.Onu.Sn())
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800449
450 // after DiscoveryRetryDelay check if the state is the same and in case send a new OnuDiscIndication
451 go func(delay time.Duration) {
Matteo Scandolo569e7172019-12-20 11:51:51 -0800452 time.Sleep(delay)
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800453 if o.InternalState.Current() == "discovered" {
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800454 o.sendOnuDiscIndication(msg, stream)
455 }
456 }(o.DiscoveryRetryDelay)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700457}
458
William Kurkian0418bc82019-11-06 12:16:24 -0500459func (o *Onu) sendOnuIndication(msg OnuIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700460 // NOTE voltha returns an ID, but if we use that ID then it complains:
461 // expected_onu_id: 1, received_onu_id: 1024, event: ONU-id-mismatch, can happen if both voltha and the olt rebooted
462 // so we're using the internal ID that is 1
463 // o.ID = msg.OnuID
Matteo Scandolo4747d292019-08-05 11:50:18 -0700464
465 indData := &openolt.Indication_OnuInd{OnuInd: &openolt.OnuIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700466 IntfId: o.PonPortID,
467 OnuId: o.ID,
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700468 OperState: msg.OperState.String(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700469 AdminState: o.OperState.Current(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700470 SerialNumber: o.SerialNumber,
471 }}
472 if err := stream.Send(&openolt.Indication{Data: indData}); err != nil {
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800473 // NOTE do we need to transition to a broken state?
Matteo Scandolo11006992019-08-28 11:29:46 -0700474 log.Errorf("Failed to send Indication_OnuInd: %v", err)
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700475 return
Matteo Scandolo4747d292019-08-05 11:50:18 -0700476 }
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700477 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700478 "IntfId": o.PonPortID,
479 "OnuId": o.ID,
480 "OperState": msg.OperState.String(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700481 "AdminState": msg.OperState.String(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700482 "OnuSn": o.Sn(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700483 }).Debug("Sent Indication_OnuInd")
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700484
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700485}
486
Pragya Arya324337e2020-02-20 14:35:08 +0530487func (o *Onu) publishOmciEvent(msg OmciMessage) {
488 if olt.PublishEvents {
489 _, _, msgType, _, _, _, err := omcisim.ParsePkt(HexDecode(msg.omciMsg.Pkt))
490 if err != nil {
491 log.Errorf("error in getting msgType %v", err)
492 return
493 }
494 if msgType == omcisim.MibUpload {
495 o.seqNumber = 0
496 publishEvent("MIB-upload-received", int32(o.PonPortID), int32(o.ID), common.OnuSnToString(o.SerialNumber))
497 } else if msgType == omcisim.MibUploadNext {
498 o.seqNumber++
499 if o.seqNumber > 290 {
500 publishEvent("MIB-upload-done", int32(o.PonPortID), int32(o.ID), common.OnuSnToString(o.SerialNumber))
501 }
502 }
503 }
504}
505
Scott Bakerb90c4312020-03-12 21:33:25 -0700506// Create a TestResponse packet and send it
507func (o *Onu) sendTestResult(msg OmciMessage, stream openolt.Openolt_EnableIndicationServer) error {
508 resp, err := omcilib.BuildTestResult(HexDecode(msg.omciMsg.Pkt))
509 if err != nil {
510 return err
511 }
512
513 var omciInd openolt.OmciIndication
514 omciInd.IntfId = o.PonPortID
515 omciInd.OnuId = o.ID
516 omciInd.Pkt = resp
517
518 omci := &openolt.Indication_OmciInd{OmciInd: &omciInd}
519 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
520 onuLogger.WithFields(log.Fields{
521 "IntfId": o.PonPortID,
522 "SerialNumber": o.Sn(),
523 "omciPacket": omciInd.Pkt,
524 "msg": msg,
525 }).Errorf("send TestResult omcisim indication failed: %v", err)
526 return err
527 }
528 onuLogger.WithFields(log.Fields{
529 "IntfId": o.PonPortID,
530 "SerialNumber": o.Sn(),
531 "omciPacket": omciInd.Pkt,
532 }).Tracef("Sent TestResult OMCI message")
533
534 return nil
535}
536
William Kurkian0418bc82019-11-06 12:16:24 -0500537func (o *Onu) handleOmciMessage(msg OmciMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700538
539 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700540 "IntfId": o.PonPortID,
Matteo Scandolo27428702019-10-11 16:21:16 -0700541 "SerialNumber": o.Sn(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700542 "omciPacket": msg.omciMsg.Pkt,
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700543 }).Tracef("Received OMCI message")
544
Pragya Arya324337e2020-02-20 14:35:08 +0530545 o.publishOmciEvent(msg)
546
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700547 var omciInd openolt.OmciIndication
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700548 respPkt, err := omcisim.OmciSim(o.PonPortID, o.ID, HexDecode(msg.omciMsg.Pkt))
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700549 if err != nil {
Matteo Scandolo27428702019-10-11 16:21:16 -0700550 onuLogger.WithFields(log.Fields{
551 "IntfId": o.PonPortID,
552 "SerialNumber": o.Sn(),
553 "omciPacket": omciInd.Pkt,
554 "msg": msg,
555 }).Errorf("Error handling OMCI message %v", msg)
556 return
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700557 }
558
559 omciInd.IntfId = o.PonPortID
560 omciInd.OnuId = o.ID
561 omciInd.Pkt = respPkt
562
563 omci := &openolt.Indication_OmciInd{OmciInd: &omciInd}
564 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
Matteo Scandolo27428702019-10-11 16:21:16 -0700565 onuLogger.WithFields(log.Fields{
566 "IntfId": o.PonPortID,
567 "SerialNumber": o.Sn(),
568 "omciPacket": omciInd.Pkt,
569 "msg": msg,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700570 }).Errorf("send omcisim indication failed: %v", err)
Matteo Scandolo27428702019-10-11 16:21:16 -0700571 return
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700572 }
573 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700574 "IntfId": o.PonPortID,
Matteo Scandolo27428702019-10-11 16:21:16 -0700575 "SerialNumber": o.Sn(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700576 "omciPacket": omciInd.Pkt,
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700577 }).Tracef("Sent OMCI message")
Scott Bakerb90c4312020-03-12 21:33:25 -0700578
579 // Test message is special, it requires sending two packets:
580 // first packet: TestResponse, says whether test was started successully, handled by omci-sim
581 // second packet, TestResult, reports the result of running the self-test
582 // TestResult can come some time after a TestResponse
583 // TODO: Implement some delay between the TestResponse and the TestResult
584 isTest, err := omcilib.IsTestRequest(HexDecode(msg.omciMsg.Pkt))
585 if (err == nil) && (isTest) {
Shrey Baid688b4242020-07-10 20:40:10 +0530586 _ = o.sendTestResult(msg, stream)
Scott Bakerb90c4312020-03-12 21:33:25 -0700587 }
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700588}
589
Matteo Scandolo27428702019-10-11 16:21:16 -0700590func (o *Onu) storePortNumber(portNo uint32) {
Matteo Scandolo813402b2019-10-23 19:24:52 -0700591 // NOTE this needed only as long as we don't support multiple UNIs
Matteo Scandolo27428702019-10-11 16:21:16 -0700592 // we need to add support for multiple UNIs
593 // the action plan is:
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700594 // - refactor the omcisim-sim library to use https://github.com/cboling/omci instead of canned messages
Matteo Scandolo27428702019-10-11 16:21:16 -0700595 // - change the library so that it reports a single UNI and remove this workaroung
596 // - add support for multiple UNIs in BBSim
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700597 if o.PortNo == 0 || portNo < o.PortNo {
Matteo Scandolo813402b2019-10-23 19:24:52 -0700598 onuLogger.WithFields(log.Fields{
599 "IntfId": o.PonPortID,
600 "OnuId": o.ID,
601 "SerialNumber": o.Sn(),
602 "OnuPortNo": o.PortNo,
603 "FlowPortNo": portNo,
604 }).Debug("Storing ONU portNo")
Matteo Scandolo27428702019-10-11 16:21:16 -0700605 o.PortNo = portNo
606 }
607}
608
William Kurkian0418bc82019-11-06 12:16:24 -0500609func (o *Onu) SetID(id uint32) {
Matteo Scandolo583f17d2020-02-13 10:35:17 -0800610 onuLogger.WithFields(log.Fields{
611 "IntfId": o.PonPortID,
612 "OnuId": id,
613 "SerialNumber": o.Sn(),
614 }).Debug("Storing OnuId ")
William Kurkian0418bc82019-11-06 12:16:24 -0500615 o.ID = id
616}
617
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700618func (o *Onu) handleFlowAdd(msg OnuFlowUpdateMessage) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700619 onuLogger.WithFields(log.Fields{
Matteo Scandolo7f656fb2020-09-08 15:18:15 -0700620 "Cookie": msg.Flow.Cookie,
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800621 "DstPort": msg.Flow.Classifier.DstPort,
622 "EthType": fmt.Sprintf("%x", msg.Flow.Classifier.EthType),
623 "FlowId": msg.Flow.FlowId,
624 "FlowType": msg.Flow.FlowType,
625 "GemportId": msg.Flow.GemportId,
626 "InnerVlan": msg.Flow.Classifier.IVid,
627 "IntfId": msg.Flow.AccessIntfId,
628 "IpProto": msg.Flow.Classifier.IpProto,
629 "OnuId": msg.Flow.OnuId,
630 "OnuSn": o.Sn(),
631 "OuterVlan": msg.Flow.Classifier.OVid,
632 "PortNo": msg.Flow.PortNo,
633 "SrcPort": msg.Flow.Classifier.SrcPort,
634 "UniID": msg.Flow.UniId,
Matteo Scandolo3ac52792020-03-05 14:21:08 -0800635 "ClassifierOPbits": msg.Flow.Classifier.OPbits,
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700636 }).Debug("ONU receives FlowAdd")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700637
Matteo Scandolo813402b2019-10-23 19:24:52 -0700638 if msg.Flow.UniId != 0 {
639 // as of now BBSim only support a single UNI, so ignore everything that is not targeted to it
640 onuLogger.WithFields(log.Fields{
641 "IntfId": o.PonPortID,
642 "OnuId": o.ID,
643 "SerialNumber": o.Sn(),
644 }).Debug("Ignoring flow as it's not for the first UNI")
645 return
646 }
647
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700648 o.FlowIds = append(o.FlowIds, msg.Flow.FlowId)
Matteo Scandolo4a036262020-08-17 15:56:13 -0700649 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 -0700650
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700651 if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeEAPOL) && msg.Flow.Classifier.OVid == 4091 {
Matteo Scandolo27428702019-10-11 16:21:16 -0700652 // NOTE storing the PortNO, it's needed when sending PacketIndications
Matteo Scandolo813402b2019-10-23 19:24:52 -0700653 o.storePortNumber(uint32(msg.Flow.PortNo))
Matteo Scandolo4a036262020-08-17 15:56:13 -0700654
655 for _, s := range o.Services {
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700656 s.HandleAuth()
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700657 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700658 } else if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeIPv4) &&
659 msg.Flow.Classifier.SrcPort == uint32(68) &&
Matteo Scandolo3ac52792020-03-05 14:21:08 -0800660 msg.Flow.Classifier.DstPort == uint32(67) &&
Matteo Scandolod74abba2020-04-16 16:36:44 -0700661 (msg.Flow.Classifier.OPbits == 0 || msg.Flow.Classifier.OPbits == 255) {
Matteo Scandolo99f18462019-10-28 14:14:28 -0700662
Matteo Scandolo4a036262020-08-17 15:56:13 -0700663 for _, s := range o.Services {
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700664 s.HandleDhcp(int(msg.Flow.Classifier.OVid))
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700665 }
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700666 }
667}
668
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700669func (o *Onu) handleFlowRemove(msg OnuFlowUpdateMessage) {
670 onuLogger.WithFields(log.Fields{
671 "IntfId": o.PonPortID,
672 "OnuId": o.ID,
673 "SerialNumber": o.Sn(),
674 "FlowId": msg.Flow.FlowId,
675 "FlowType": msg.Flow.FlowType,
676 }).Debug("ONU receives FlowRemove")
677
678 for idx, flow := range o.FlowIds {
679 // If the gemport is found, delete it from local cache.
680 if flow == msg.Flow.FlowId {
681 o.FlowIds = append(o.FlowIds[:idx], o.FlowIds[idx+1:]...)
682 break
683 }
684 }
685
686 if len(o.FlowIds) == 0 {
687 onuLogger.WithFields(log.Fields{
688 "IntfId": o.PonPortID,
689 "OnuId": o.ID,
690 "SerialNumber": o.Sn(),
691 }).Info("Resetting GemPort")
692 o.GemPortAdded = false
693
Hardik Windlass7b3405b2020-07-08 15:10:05 +0530694 // check if ONU delete is performed and
695 // terminate the ONU's ProcessOnuMessages Go routine
696 if o.InternalState.Current() == "disabled" {
697 close(o.Channel)
698 }
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700699 }
700}
701
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700702// HexDecode converts the hex encoding to binary
703func HexDecode(pkt []byte) []byte {
704 p := make([]byte, len(pkt)/2)
705 for i, j := 0, 0; i < len(pkt); i, j = i+2, j+1 {
706 // Go figure this ;)
707 u := (pkt[i] & 15) + (pkt[i]>>6)*9
708 l := (pkt[i+1] & 15) + (pkt[i+1]>>6)*9
709 p[j] = u<<4 + l
710 }
711 onuLogger.Tracef("Omci decoded: %x.", p)
712 return p
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700713}
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700714
715// BBR methods
716
717func sendOmciMsg(pktBytes []byte, intfId uint32, onuId uint32, sn *openolt.SerialNumber, msgType string, client openolt.OpenoltClient) {
718 omciMsg := openolt.OmciMsg{
719 IntfId: intfId,
720 OnuId: onuId,
721 Pkt: pktBytes,
722 }
723
724 if _, err := client.OmciMsgOut(context.Background(), &omciMsg); err != nil {
725 log.WithFields(log.Fields{
726 "IntfId": intfId,
727 "OnuId": onuId,
728 "SerialNumber": common.OnuSnToString(sn),
729 "Pkt": omciMsg.Pkt,
730 }).Fatalf("Failed to send MIB Reset")
731 }
732 log.WithFields(log.Fields{
733 "IntfId": intfId,
734 "OnuId": onuId,
735 "SerialNumber": common.OnuSnToString(sn),
736 "Pkt": omciMsg.Pkt,
737 }).Tracef("Sent OMCI message %s", msgType)
738}
739
740func (onu *Onu) getNextTid(highPriority ...bool) uint16 {
741 var next uint16
742 if len(highPriority) > 0 && highPriority[0] {
743 next = onu.hpTid
744 onu.hpTid += 1
745 if onu.hpTid < 0x8000 {
746 onu.hpTid = 0x8000
747 }
748 } else {
749 next = onu.tid
750 onu.tid += 1
751 if onu.tid >= 0x8000 {
752 onu.tid = 1
753 }
754 }
755 return next
756}
757
758// TODO move this method in responders/omcisim
759func (o *Onu) StartOmci(client openolt.OpenoltClient) {
760 mibReset, _ := omcilib.CreateMibResetRequest(o.getNextTid(false))
761 sendOmciMsg(mibReset, o.PonPortID, o.ID, o.SerialNumber, "mibReset", client)
762}
763
764func (o *Onu) handleOmci(msg OmciIndicationMessage, client openolt.OpenoltClient) {
765 msgType, packet := omcilib.DecodeOmci(msg.OmciInd.Pkt)
766
767 log.WithFields(log.Fields{
768 "IntfId": msg.OmciInd.IntfId,
769 "OnuId": msg.OmciInd.OnuId,
770 "OnuSn": common.OnuSnToString(o.SerialNumber),
771 "Pkt": msg.OmciInd.Pkt,
772 "msgType": msgType,
Anand S Katti09541352020-01-29 15:54:01 +0530773 }).Trace("ONU Receives OMCI Msg")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700774 switch msgType {
775 default:
Matteo Scandolo813402b2019-10-23 19:24:52 -0700776 log.WithFields(log.Fields{
777 "IntfId": msg.OmciInd.IntfId,
778 "OnuId": msg.OmciInd.OnuId,
779 "OnuSn": common.OnuSnToString(o.SerialNumber),
780 "Pkt": msg.OmciInd.Pkt,
781 "msgType": msgType,
782 }).Fatalf("unexpected frame: %v", packet)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700783 case omci.MibResetResponseType:
784 mibUpload, _ := omcilib.CreateMibUploadRequest(o.getNextTid(false))
785 sendOmciMsg(mibUpload, o.PonPortID, o.ID, o.SerialNumber, "mibUpload", client)
786 case omci.MibUploadResponseType:
787 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
788 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
789 case omci.MibUploadNextResponseType:
790 o.seqNumber++
791
792 if o.seqNumber > 290 {
793 // NOTE we are done with the MIB Upload (290 is the number of messages the omci-sim library will respond to)
794 galEnet, _ := omcilib.CreateGalEnetRequest(o.getNextTid(false))
795 sendOmciMsg(galEnet, o.PonPortID, o.ID, o.SerialNumber, "CreateGalEnetRequest", client)
796 } else {
797 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
798 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
799 }
800 case omci.CreateResponseType:
801 // NOTE Creating a GemPort,
802 // BBsim actually doesn't care about the values, so we can do we want with the parameters
803 // In the same way we can create a GemPort even without setting up UNIs/TConts/...
804 // but we need the GemPort to trigger the state change
805
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700806 if !o.GemPortAdded {
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700807 // NOTE this sends a CreateRequestType and BBSim replies with a CreateResponseType
808 // thus we send this request only once
809 gemReq, _ := omcilib.CreateGemPortRequest(o.getNextTid(false))
810 sendOmciMsg(gemReq, o.PonPortID, o.ID, o.SerialNumber, "CreateGemPortRequest", client)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700811 o.GemPortAdded = true
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700812 } else {
813 if err := o.InternalState.Event("send_eapol_flow"); err != nil {
814 onuLogger.WithFields(log.Fields{
815 "OnuId": o.ID,
816 "IntfId": o.PonPortID,
817 "OnuSn": o.Sn(),
818 }).Errorf("Error while transitioning ONU State %v", err)
819 }
820 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700821 }
822}
823
824func (o *Onu) sendEapolFlow(client openolt.OpenoltClient) {
825
826 classifierProto := openolt.Classifier{
827 EthType: uint32(layers.EthernetTypeEAPOL),
828 OVid: 4091,
829 }
830
831 actionProto := openolt.Action{}
832
833 downstreamFlow := openolt.Flow{
834 AccessIntfId: int32(o.PonPortID),
835 OnuId: int32(o.ID),
Matteo Scandolo813402b2019-10-23 19:24:52 -0700836 UniId: int32(0), // NOTE do not hardcode this, we need to support multiple UNIs
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700837 FlowId: uint32(o.ID),
838 FlowType: "downstream",
839 AllocId: int32(0),
840 NetworkIntfId: int32(0),
841 GemportId: int32(1), // FIXME use the same value as CreateGemPortRequest PortID, do not hardcode
842 Classifier: &classifierProto,
843 Action: &actionProto,
844 Priority: int32(100),
845 Cookie: uint64(o.ID),
846 PortNo: uint32(o.ID), // NOTE we are using this to map an incoming packetIndication to an ONU
847 }
848
849 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
850 log.WithFields(log.Fields{
851 "IntfId": o.PonPortID,
852 "OnuId": o.ID,
853 "FlowId": downstreamFlow.FlowId,
854 "PortNo": downstreamFlow.PortNo,
855 "SerialNumber": common.OnuSnToString(o.SerialNumber),
Matteo Scandolob0e3e622020-04-23 17:00:29 -0700856 }).Fatalf("Failed to add EAPOL Flow")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700857 }
858 log.WithFields(log.Fields{
859 "IntfId": o.PonPortID,
860 "OnuId": o.ID,
861 "FlowId": downstreamFlow.FlowId,
862 "PortNo": downstreamFlow.PortNo,
863 "SerialNumber": common.OnuSnToString(o.SerialNumber),
864 }).Info("Sent EAPOL Flow")
865}
866
867func (o *Onu) sendDhcpFlow(client openolt.OpenoltClient) {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700868
869 // BBR only works with a single service (ATT HSIA)
870 hsia := o.Services[0].(*Service)
871
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700872 classifierProto := openolt.Classifier{
873 EthType: uint32(layers.EthernetTypeIPv4),
874 SrcPort: uint32(68),
875 DstPort: uint32(67),
Matteo Scandolo4a036262020-08-17 15:56:13 -0700876 OVid: uint32(hsia.CTag),
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700877 }
878
879 actionProto := openolt.Action{}
880
881 downstreamFlow := openolt.Flow{
882 AccessIntfId: int32(o.PonPortID),
883 OnuId: int32(o.ID),
Matteo Scandolo813402b2019-10-23 19:24:52 -0700884 UniId: int32(0), // FIXME do not hardcode this
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700885 FlowId: uint32(o.ID),
886 FlowType: "downstream",
887 AllocId: int32(0),
888 NetworkIntfId: int32(0),
889 GemportId: int32(1), // FIXME use the same value as CreateGemPortRequest PortID, do not hardcode
890 Classifier: &classifierProto,
891 Action: &actionProto,
892 Priority: int32(100),
893 Cookie: uint64(o.ID),
894 PortNo: uint32(o.ID), // NOTE we are using this to map an incoming packetIndication to an ONU
895 }
896
897 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
898 log.WithFields(log.Fields{
899 "IntfId": o.PonPortID,
900 "OnuId": o.ID,
901 "FlowId": downstreamFlow.FlowId,
902 "PortNo": downstreamFlow.PortNo,
903 "SerialNumber": common.OnuSnToString(o.SerialNumber),
904 }).Fatalf("Failed to send DHCP Flow")
905 }
906 log.WithFields(log.Fields{
907 "IntfId": o.PonPortID,
908 "OnuId": o.ID,
909 "FlowId": downstreamFlow.FlowId,
910 "PortNo": downstreamFlow.PortNo,
911 "SerialNumber": common.OnuSnToString(o.SerialNumber),
912 }).Info("Sent DHCP Flow")
913}
Pragya Arya8bdb4532020-03-02 17:08:09 +0530914
915// DeleteFlow method search and delete flowKey from the onu flows slice
916func (onu *Onu) DeleteFlow(key FlowKey) {
917 for pos, flowKey := range onu.Flows {
918 if flowKey == key {
919 // delete the flowKey by shifting all flowKeys by one
920 onu.Flows = append(onu.Flows[:pos], onu.Flows[pos+1:]...)
921 t := make([]FlowKey, len(onu.Flows))
922 copy(t, onu.Flows)
923 onu.Flows = t
924 break
925 }
926 }
927}
Hardik Windlass7b3405b2020-07-08 15:10:05 +0530928
929func (onu *Onu) ReDiscoverOnu() {
930 // Wait for few seconds to be sure of the cleanup
931 time.Sleep(5 * time.Second)
932
933 onuLogger.WithFields(log.Fields{
934 "IntfId": onu.PonPortID,
935 "OnuId": onu.ID,
936 "OnuSn": onu.Sn(),
937 }).Debug("Send ONU Re-Discovery")
938
939 // ONU Re-Discovery
940 if err := onu.InternalState.Event("initialize"); err != nil {
941 log.WithFields(log.Fields{
942 "IntfId": onu.PonPortID,
943 "OnuSn": onu.Sn(),
944 "OnuId": onu.ID,
945 }).Infof("Failed to transition ONU to initialized state: %s", err.Error())
946 }
947
948 if err := onu.InternalState.Event("discover"); err != nil {
949 log.WithFields(log.Fields{
950 "IntfId": onu.PonPortID,
951 "OnuSn": onu.Sn(),
952 "OnuId": onu.ID,
953 }).Infof("Failed to transition ONU to discovered state: %s", err.Error())
954 }
955}
Matteo Scandolo4a036262020-08-17 15:56:13 -0700956
957func (onu *Onu) addGemPortToService(gemport uint32, ethType uint32, oVlan uint32, iVlan uint32) {
958 for _, s := range onu.Services {
959 if service, ok := s.(*Service); ok {
960 // EAPOL is a strange case, as packets are untagged
961 // but we assume we will have a single service requiring EAPOL
962 if ethType == uint32(layers.EthernetTypeEAPOL) && service.NeedsEapol {
963 service.GemPort = gemport
964 }
965
966 // For DHCP services we single tag the outgoing packets,
967 // thus the flow only contains the CTag and we can use that to match the service
968 if ethType == uint32(layers.EthernetTypeIPv4) && service.NeedsDhcp && service.CTag == int(oVlan) {
969 service.GemPort = gemport
970 }
971
972 // for dataplane services match both C and S tags
973 if service.CTag == int(iVlan) && service.STag == int(oVlan) {
974 service.GemPort = gemport
975 }
976 }
977 }
978}
979
980func (onu *Onu) findServiceByMacAddress(macAddress net.HardwareAddr) (*Service, error) {
981 for _, s := range onu.Services {
982 service := s.(*Service)
983 if service.HwAddress.String() == macAddress.String() {
984 return service, nil
985 }
986 }
987 return nil, fmt.Errorf("cannot-find-service-with-mac-address-%s", macAddress.String())
988}