blob: 0f78107bfdb85ecb9b6265bc3c7becd31447b5a7 [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 Scandolo4a036262020-08-17 15:56:13 -070065 PortNo uint32
66 GemPortAdded bool
67 Flows []FlowKey
68 FlowIds []uint32 // keep track of the flows we currently have in the ONU
Matteo Scandolo99f18462019-10-28 14:14:28 -070069
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070070 OperState *fsm.FSM
71 SerialNumber *openolt.SerialNumber
72
Matteo Scandolo4a036262020-08-17 15:56:13 -070073 Channel chan Message // this Channel is to track state changes OMCI messages, EAPOL and DHCP packets
Matteo Scandolo40e067f2019-10-16 16:59:41 -070074
75 // OMCI params
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -070076 tid uint16
77 hpTid uint16
78 seqNumber uint16
Matteo Scandolo40e067f2019-10-16 16:59:41 -070079
Anand S Katti09541352020-01-29 15:54:01 +053080 DoneChannel chan bool // this channel is used to signal once the onu is complete (when the struct is used by BBR)
81 TrafficSchedulers *tech_profile.TrafficSchedulers
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070082}
83
Matteo Scandolo99f18462019-10-28 14:14:28 -070084func (o *Onu) Sn() string {
Matteo Scandolo40e067f2019-10-16 16:59:41 -070085 return common.OnuSnToString(o.SerialNumber)
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070086}
87
Matteo Scandolo4a036262020-08-17 15:56:13 -070088func CreateONU(olt *OltDevice, pon *PonPort, id uint32, delay time.Duration, isMock bool) *Onu {
Shrey Baidf8abccc2020-06-15 19:41:22 +053089 b := &backoff.Backoff{
90 //These are the defaults
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -070091 Min: 5 * time.Second,
Shrey Baidf8abccc2020-06-15 19:41:22 +053092 Max: 35 * time.Second,
93 Factor: 1.5,
94 Jitter: false,
95 }
Matteo Scandolo4747d292019-08-05 11:50:18 -070096
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070097 o := Onu{
Matteo Scandolo4a036262020-08-17 15:56:13 -070098 ID: id,
Matteo Scandoloe811ae92019-12-10 17:50:14 -080099 PonPortID: pon.ID,
100 PonPort: pon,
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800101 PortNo: 0,
102 tid: 0x1,
103 hpTid: 0x8000,
104 seqNumber: 0,
105 DoneChannel: make(chan bool, 1),
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800106 GemPortAdded: false,
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800107 DiscoveryRetryDelay: 60 * time.Second, // this is used to send OnuDiscoveryIndications until an activate call is received
Pragya Arya8bdb4532020-03-02 17:08:09 +0530108 Flows: []FlowKey{},
Pragya Arya2225f202020-01-29 18:05:01 +0530109 DiscoveryDelay: delay,
Shrey Baidf8abccc2020-06-15 19:41:22 +0530110 Backoff: b,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700111 }
Pragya Arya2225f202020-01-29 18:05:01 +0530112 o.SerialNumber = o.NewSN(olt.ID, pon.ID, id)
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700113 // NOTE this state machine is used to track the operational
114 // state as requested by VOLTHA
115 o.OperState = getOperStateFSM(func(e *fsm.Event) {
116 onuLogger.WithFields(log.Fields{
117 "ID": o.ID,
118 }).Debugf("Changing ONU OperState from %s to %s", e.Src, e.Dst)
119 })
120
121 // NOTE this state machine is used to activate the OMCI, EAPOL and DHCP clients
122 o.InternalState = fsm.NewFSM(
123 "created",
124 fsm.Events{
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700125 // DEVICE Lifecycle
Hardik Windlassad790cb2020-06-17 21:26:22 +0530126 {Name: "initialize", Src: []string{"created", "disabled", "pon_disabled"}, Dst: "initialized"},
127 {Name: "discover", Src: []string{"initialized"}, Dst: "discovered"},
128 {Name: "enable", Src: []string{"discovered", "pon_disabled"}, Dst: "enabled"},
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700129 {Name: "receive_eapol_flow", Src: []string{"enabled", "gem_port_added"}, Dst: "eapol_flow_received"},
130 {Name: "add_gem_port", Src: []string{"enabled", "eapol_flow_received"}, Dst: "gem_port_added"},
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 Scandolo5ff80082019-12-20 13:20:57 -0800132 {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 +0530133 // 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 -0800134 {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 -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
181 },
182 "enter_disabled": func(event *fsm.Event) {
Matteo Scandolo47ef64b2020-04-20 14:16:07 -0700183
184 // clean the ONU state
Matteo Scandolo328c5f02020-06-26 14:16:39 -0700185 o.GemPortAdded = false
Matteo Scandolo47ef64b2020-04-20 14:16:07 -0700186 o.PortNo = 0
187 o.Flows = []FlowKey{}
188
189 // set the OpenState to disabled
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800190 if err := o.OperState.Event("disable"); err != nil {
191 onuLogger.WithFields(log.Fields{
192 "OnuId": o.ID,
193 "IntfId": o.PonPortID,
194 "OnuSn": o.Sn(),
195 }).Errorf("Cannot change ONU OperState to down: %s", err.Error())
196 }
Matteo Scandolo47ef64b2020-04-20 14:16:07 -0700197
198 // send the OnuIndication DOWN event
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700199 msg := Message{
200 Type: OnuIndication,
201 Data: OnuIndicationMessage{
202 OnuSN: o.SerialNumber,
203 PonPortID: o.PonPortID,
204 OperState: DOWN,
205 },
206 }
207 o.Channel <- msg
Hardik Windlass7b3405b2020-07-08 15:10:05 +0530208
209 // verify all the flows removes are handled and
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100210 // terminate the ONU's ProcessOnuMessages Go routine
Hardik Windlass7b3405b2020-07-08 15:10:05 +0530211 if len(o.FlowIds) == 0 {
212 close(o.Channel)
213 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700214 },
Matteo Scandolo4a036262020-08-17 15:56:13 -0700215 // BBR states
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700216 "enter_eapol_flow_sent": func(e *fsm.Event) {
217 msg := Message{
218 Type: SendEapolFlow,
219 }
220 o.Channel <- msg
221 },
222 "enter_dhcp_flow_sent": func(e *fsm.Event) {
223 msg := Message{
224 Type: SendDhcpFlow,
225 }
226 o.Channel <- msg
227 },
Arjun E K57a7fcb2020-01-30 06:44:45 +0000228 "igmp_join_start": func(e *fsm.Event) {
229 msg := Message{
230 Type: IGMPMembershipReportV2,
231 }
232 o.Channel <- msg
233 },
234 "igmp_leave": func(e *fsm.Event) {
235 msg := Message{
236 Type: IGMPLeaveGroup}
237 o.Channel <- msg
238 },
Anand S Katti09541352020-01-29 15:54:01 +0530239 "igmp_join_startv3": func(e *fsm.Event) {
240 msg := Message{
241 Type: IGMPMembershipReportV3,
242 }
243 o.Channel <- msg
244 },
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700245 },
246 )
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100247
Matteo Scandolo27428702019-10-11 16:21:16 -0700248 return &o
Matteo Scandolo4747d292019-08-05 11:50:18 -0700249}
250
William Kurkian0418bc82019-11-06 12:16:24 -0500251func (o *Onu) logStateChange(src string, dst string) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700252 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700253 "OnuId": o.ID,
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700254 "IntfId": o.PonPortID,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700255 "OnuSn": o.Sn(),
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700256 }).Debugf("Changing ONU InternalState from %s to %s", src, dst)
257}
258
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100259// ProcessOnuMessages starts indication channel for each ONU
David Bainbridge103cf022019-12-16 20:11:35 +0000260func (o *Onu) ProcessOnuMessages(ctx context.Context, stream openolt.Openolt_EnableIndicationServer, client openolt.OpenoltClient) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700261 onuLogger.WithFields(log.Fields{
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100262 "onuID": o.ID,
263 "onuSN": o.Sn(),
264 "ponPort": o.PonPortID,
265 }).Debug("Starting ONU Indication Channel")
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700266
David Bainbridge103cf022019-12-16 20:11:35 +0000267loop:
268 for {
269 select {
270 case <-ctx.Done():
271 onuLogger.WithFields(log.Fields{
272 "onuID": o.ID,
273 "onuSN": o.Sn(),
274 }).Tracef("ONU message handling canceled via context")
275 break loop
276 case message, ok := <-o.Channel:
277 if !ok || ctx.Err() != nil {
278 onuLogger.WithFields(log.Fields{
279 "onuID": o.ID,
280 "onuSN": o.Sn(),
281 }).Tracef("ONU message handling canceled via channel close")
282 break loop
Matteo Scandolo075b1892019-10-07 12:11:07 -0700283 }
David Bainbridge103cf022019-12-16 20:11:35 +0000284 onuLogger.WithFields(log.Fields{
285 "onuID": o.ID,
286 "onuSN": o.Sn(),
287 "messageType": message.Type,
288 }).Tracef("Received message on ONU Channel")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700289
David Bainbridge103cf022019-12-16 20:11:35 +0000290 switch message.Type {
291 case OnuDiscIndication:
292 msg, _ := message.Data.(OnuDiscIndicationMessage)
293 // 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 +0530294 time.Sleep(o.DiscoveryDelay)
David Bainbridge103cf022019-12-16 20:11:35 +0000295 o.sendOnuDiscIndication(msg, stream)
296 case OnuIndication:
297 msg, _ := message.Data.(OnuIndicationMessage)
298 o.sendOnuIndication(msg, stream)
299 case OMCI:
300 msg, _ := message.Data.(OmciMessage)
301 o.handleOmciMessage(msg, stream)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700302 case FlowAdd:
David Bainbridge103cf022019-12-16 20:11:35 +0000303 msg, _ := message.Data.(OnuFlowUpdateMessage)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700304 o.handleFlowAdd(msg)
305 case FlowRemoved:
306 msg, _ := message.Data.(OnuFlowUpdateMessage)
307 o.handleFlowRemove(msg)
David Bainbridge103cf022019-12-16 20:11:35 +0000308 case OnuPacketOut:
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700309
David Bainbridge103cf022019-12-16 20:11:35 +0000310 msg, _ := message.Data.(OnuPacketMessage)
311
312 log.WithFields(log.Fields{
313 "IntfId": msg.IntfId,
314 "OnuId": msg.OnuId,
315 "pktType": msg.Type,
316 }).Trace("Received OnuPacketOut Message")
317
Matteo Scandolo4a036262020-08-17 15:56:13 -0700318 service, err := o.findServiceByMacAddress(msg.MacAddress)
319 if err != nil {
320 onuLogger.WithFields(log.Fields{
321 "IntfId": msg.IntfId,
322 "OnuId": msg.OnuId,
323 "pktType": msg.Type,
324 "MacAddress": msg.MacAddress,
325 "OnuSn": o.Sn(),
326 }).Error("Cannot find Service associated with packet")
327 return
David Bainbridge103cf022019-12-16 20:11:35 +0000328 }
Matteo Scandolo4a036262020-08-17 15:56:13 -0700329
330 service.PacketCh <- msg
331
David Bainbridge103cf022019-12-16 20:11:35 +0000332 case OnuPacketIn:
333 // NOTE we only receive BBR packets here.
334 // Eapol.HandleNextPacket can handle both BBSim and BBr cases so the call is the same
335 // in the DHCP case VOLTHA only act as a proxy, the behaviour is completely different thus we have a dhcp.HandleNextBbrPacket
336 msg, _ := message.Data.(OnuPacketMessage)
337
338 log.WithFields(log.Fields{
339 "IntfId": msg.IntfId,
340 "OnuId": msg.OnuId,
341 "pktType": msg.Type,
342 }).Trace("Received OnuPacketIn Message")
343
344 if msg.Type == packetHandlers.EAPOL {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700345 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 +0000346 } else if msg.Type == packetHandlers.DHCP {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700347 _ = dhcp.HandleNextBbrPacket(o.ID, o.PonPortID, o.Sn(), o.DoneChannel, msg.Packet, client)
David Bainbridge103cf022019-12-16 20:11:35 +0000348 }
David Bainbridge103cf022019-12-16 20:11:35 +0000349 case OmciIndication:
350 msg, _ := message.Data.(OmciIndicationMessage)
351 o.handleOmci(msg, client)
352 case SendEapolFlow:
353 o.sendEapolFlow(client)
354 case SendDhcpFlow:
355 o.sendDhcpFlow(client)
356 default:
357 onuLogger.Warnf("Received unknown message data %v for type %v in OLT Channel", message.Data, message.Type)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700358 }
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700359 }
360 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100361 onuLogger.WithFields(log.Fields{
362 "onuID": o.ID,
363 "onuSN": o.Sn(),
364 }).Debug("Stopped handling ONU Indication Channel")
Matteo Scandolo4747d292019-08-05 11:50:18 -0700365}
366
Matteo Scandolodf3f85d2020-01-15 12:50:48 -0800367func (o *Onu) processOmciMessage(message omcisim.OmciChMessage, stream openolt.Openolt_EnableIndicationServer) {
William Kurkian9dadc5b2019-10-22 13:51:57 -0400368 switch message.Type {
Matteo Scandolodf3f85d2020-01-15 12:50:48 -0800369 case omcisim.UniLinkUp, omcisim.UniLinkDown:
370 onuLogger.WithFields(log.Fields{
371 "OnuId": message.Data.OnuId,
372 "IntfId": message.Data.IntfId,
373 "Type": message.Type,
Matteo Scandolo4a036262020-08-17 15:56:13 -0700374 }).Debug("UNI Link Alarm")
Matteo Scandolodf3f85d2020-01-15 12:50:48 -0800375 // TODO send to OLT
376
377 omciInd := openolt.OmciIndication{
378 IntfId: message.Data.IntfId,
379 OnuId: message.Data.OnuId,
380 Pkt: message.Packet,
381 }
382
383 omci := &openolt.Indication_OmciInd{OmciInd: &omciInd}
384 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
385 onuLogger.WithFields(log.Fields{
386 "IntfId": o.PonPortID,
387 "SerialNumber": o.Sn(),
388 "Type": message.Type,
389 "omciPacket": omciInd.Pkt,
390 }).Errorf("Failed to send UNI Link Alarm: %v", err)
391 return
392 }
393
394 onuLogger.WithFields(log.Fields{
395 "IntfId": o.PonPortID,
396 "SerialNumber": o.Sn(),
397 "Type": message.Type,
398 "omciPacket": omciInd.Pkt,
Matteo Scandolo4a036262020-08-17 15:56:13 -0700399 }).Debug("UNI Link alarm sent")
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700400 }
401}
402
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100403func (o Onu) NewSN(oltid int, intfid uint32, onuid uint32) *openolt.SerialNumber {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700404
405 sn := new(openolt.SerialNumber)
406
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700407 //sn = new(openolt.SerialNumber)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700408 sn.VendorId = []byte("BBSM")
409 sn.VendorSpecific = []byte{0, byte(oltid % 256), byte(intfid), byte(onuid)}
410
411 return sn
412}
413
William Kurkian0418bc82019-11-06 12:16:24 -0500414func (o *Onu) sendOnuDiscIndication(msg OnuDiscIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700415 discoverData := &openolt.Indication_OnuDiscInd{OnuDiscInd: &openolt.OnuDiscIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700416 IntfId: msg.Onu.PonPortID,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700417 SerialNumber: msg.Onu.SerialNumber,
418 }}
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700419
Matteo Scandolo4747d292019-08-05 11:50:18 -0700420 if err := stream.Send(&openolt.Indication{Data: discoverData}); err != nil {
Matteo Scandolo11006992019-08-28 11:29:46 -0700421 log.Errorf("Failed to send Indication_OnuDiscInd: %v", err)
Matteo Scandolo99f18462019-10-28 14:14:28 -0700422 return
Matteo Scandolo4747d292019-08-05 11:50:18 -0700423 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700424
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700425 onuLogger.WithFields(log.Fields{
Matteo Scandolo4747d292019-08-05 11:50:18 -0700426 "IntfId": msg.Onu.PonPortID,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700427 "OnuSn": msg.Onu.Sn(),
428 "OnuId": o.ID,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700429 }).Debug("Sent Indication_OnuDiscInd")
Pragya Arya324337e2020-02-20 14:35:08 +0530430 publishEvent("ONU-discovery-indication-sent", int32(msg.Onu.PonPortID), int32(o.ID), msg.Onu.Sn())
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800431
432 // after DiscoveryRetryDelay check if the state is the same and in case send a new OnuDiscIndication
433 go func(delay time.Duration) {
Matteo Scandolo569e7172019-12-20 11:51:51 -0800434 time.Sleep(delay)
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800435 if o.InternalState.Current() == "discovered" {
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800436 o.sendOnuDiscIndication(msg, stream)
437 }
438 }(o.DiscoveryRetryDelay)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700439}
440
William Kurkian0418bc82019-11-06 12:16:24 -0500441func (o *Onu) sendOnuIndication(msg OnuIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700442 // NOTE voltha returns an ID, but if we use that ID then it complains:
443 // expected_onu_id: 1, received_onu_id: 1024, event: ONU-id-mismatch, can happen if both voltha and the olt rebooted
444 // so we're using the internal ID that is 1
445 // o.ID = msg.OnuID
Matteo Scandolo4747d292019-08-05 11:50:18 -0700446
447 indData := &openolt.Indication_OnuInd{OnuInd: &openolt.OnuIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700448 IntfId: o.PonPortID,
449 OnuId: o.ID,
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700450 OperState: msg.OperState.String(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700451 AdminState: o.OperState.Current(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700452 SerialNumber: o.SerialNumber,
453 }}
454 if err := stream.Send(&openolt.Indication{Data: indData}); err != nil {
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800455 // NOTE do we need to transition to a broken state?
Matteo Scandolo11006992019-08-28 11:29:46 -0700456 log.Errorf("Failed to send Indication_OnuInd: %v", err)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700457 }
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700458 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700459 "IntfId": o.PonPortID,
460 "OnuId": o.ID,
461 "OperState": msg.OperState.String(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700462 "AdminState": msg.OperState.String(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700463 "OnuSn": o.Sn(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700464 }).Debug("Sent Indication_OnuInd")
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700465
Matteo Scandolo4a036262020-08-17 15:56:13 -0700466 for _, s := range o.Services {
467 go s.HandlePackets(stream)
468 }
469
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700470}
471
Pragya Arya324337e2020-02-20 14:35:08 +0530472func (o *Onu) publishOmciEvent(msg OmciMessage) {
473 if olt.PublishEvents {
474 _, _, msgType, _, _, _, err := omcisim.ParsePkt(HexDecode(msg.omciMsg.Pkt))
475 if err != nil {
476 log.Errorf("error in getting msgType %v", err)
477 return
478 }
479 if msgType == omcisim.MibUpload {
480 o.seqNumber = 0
481 publishEvent("MIB-upload-received", int32(o.PonPortID), int32(o.ID), common.OnuSnToString(o.SerialNumber))
482 } else if msgType == omcisim.MibUploadNext {
483 o.seqNumber++
484 if o.seqNumber > 290 {
485 publishEvent("MIB-upload-done", int32(o.PonPortID), int32(o.ID), common.OnuSnToString(o.SerialNumber))
486 }
487 }
488 }
489}
490
Scott Bakerb90c4312020-03-12 21:33:25 -0700491// Create a TestResponse packet and send it
492func (o *Onu) sendTestResult(msg OmciMessage, stream openolt.Openolt_EnableIndicationServer) error {
493 resp, err := omcilib.BuildTestResult(HexDecode(msg.omciMsg.Pkt))
494 if err != nil {
495 return err
496 }
497
498 var omciInd openolt.OmciIndication
499 omciInd.IntfId = o.PonPortID
500 omciInd.OnuId = o.ID
501 omciInd.Pkt = resp
502
503 omci := &openolt.Indication_OmciInd{OmciInd: &omciInd}
504 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
505 onuLogger.WithFields(log.Fields{
506 "IntfId": o.PonPortID,
507 "SerialNumber": o.Sn(),
508 "omciPacket": omciInd.Pkt,
509 "msg": msg,
510 }).Errorf("send TestResult omcisim indication failed: %v", err)
511 return err
512 }
513 onuLogger.WithFields(log.Fields{
514 "IntfId": o.PonPortID,
515 "SerialNumber": o.Sn(),
516 "omciPacket": omciInd.Pkt,
517 }).Tracef("Sent TestResult OMCI message")
518
519 return nil
520}
521
William Kurkian0418bc82019-11-06 12:16:24 -0500522func (o *Onu) handleOmciMessage(msg OmciMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700523
524 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700525 "IntfId": o.PonPortID,
Matteo Scandolo27428702019-10-11 16:21:16 -0700526 "SerialNumber": o.Sn(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700527 "omciPacket": msg.omciMsg.Pkt,
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700528 }).Tracef("Received OMCI message")
529
Pragya Arya324337e2020-02-20 14:35:08 +0530530 o.publishOmciEvent(msg)
531
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700532 var omciInd openolt.OmciIndication
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700533 respPkt, err := omcisim.OmciSim(o.PonPortID, o.ID, HexDecode(msg.omciMsg.Pkt))
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700534 if err != nil {
Matteo Scandolo27428702019-10-11 16:21:16 -0700535 onuLogger.WithFields(log.Fields{
536 "IntfId": o.PonPortID,
537 "SerialNumber": o.Sn(),
538 "omciPacket": omciInd.Pkt,
539 "msg": msg,
540 }).Errorf("Error handling OMCI message %v", msg)
541 return
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700542 }
543
544 omciInd.IntfId = o.PonPortID
545 omciInd.OnuId = o.ID
546 omciInd.Pkt = respPkt
547
548 omci := &openolt.Indication_OmciInd{OmciInd: &omciInd}
549 if err := stream.Send(&openolt.Indication{Data: omci}); 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,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700555 }).Errorf("send omcisim indication failed: %v", err)
Matteo Scandolo27428702019-10-11 16:21:16 -0700556 return
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700557 }
558 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700559 "IntfId": o.PonPortID,
Matteo Scandolo27428702019-10-11 16:21:16 -0700560 "SerialNumber": o.Sn(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700561 "omciPacket": omciInd.Pkt,
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700562 }).Tracef("Sent OMCI message")
Scott Bakerb90c4312020-03-12 21:33:25 -0700563
564 // Test message is special, it requires sending two packets:
565 // first packet: TestResponse, says whether test was started successully, handled by omci-sim
566 // second packet, TestResult, reports the result of running the self-test
567 // TestResult can come some time after a TestResponse
568 // TODO: Implement some delay between the TestResponse and the TestResult
569 isTest, err := omcilib.IsTestRequest(HexDecode(msg.omciMsg.Pkt))
570 if (err == nil) && (isTest) {
Shrey Baid688b4242020-07-10 20:40:10 +0530571 _ = o.sendTestResult(msg, stream)
Scott Bakerb90c4312020-03-12 21:33:25 -0700572 }
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700573}
574
Matteo Scandolo27428702019-10-11 16:21:16 -0700575func (o *Onu) storePortNumber(portNo uint32) {
Matteo Scandolo813402b2019-10-23 19:24:52 -0700576 // NOTE this needed only as long as we don't support multiple UNIs
Matteo Scandolo27428702019-10-11 16:21:16 -0700577 // we need to add support for multiple UNIs
578 // the action plan is:
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700579 // - refactor the omcisim-sim library to use https://github.com/cboling/omci instead of canned messages
Matteo Scandolo27428702019-10-11 16:21:16 -0700580 // - change the library so that it reports a single UNI and remove this workaroung
581 // - add support for multiple UNIs in BBSim
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700582 if o.PortNo == 0 || portNo < o.PortNo {
Matteo Scandolo813402b2019-10-23 19:24:52 -0700583 onuLogger.WithFields(log.Fields{
584 "IntfId": o.PonPortID,
585 "OnuId": o.ID,
586 "SerialNumber": o.Sn(),
587 "OnuPortNo": o.PortNo,
588 "FlowPortNo": portNo,
589 }).Debug("Storing ONU portNo")
Matteo Scandolo27428702019-10-11 16:21:16 -0700590 o.PortNo = portNo
591 }
592}
593
William Kurkian0418bc82019-11-06 12:16:24 -0500594func (o *Onu) SetID(id uint32) {
Matteo Scandolo583f17d2020-02-13 10:35:17 -0800595 onuLogger.WithFields(log.Fields{
596 "IntfId": o.PonPortID,
597 "OnuId": id,
598 "SerialNumber": o.Sn(),
599 }).Debug("Storing OnuId ")
William Kurkian0418bc82019-11-06 12:16:24 -0500600 o.ID = id
601}
602
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700603func (o *Onu) handleFlowAdd(msg OnuFlowUpdateMessage) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700604 onuLogger.WithFields(log.Fields{
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800605 "DstPort": msg.Flow.Classifier.DstPort,
606 "EthType": fmt.Sprintf("%x", msg.Flow.Classifier.EthType),
607 "FlowId": msg.Flow.FlowId,
608 "FlowType": msg.Flow.FlowType,
609 "GemportId": msg.Flow.GemportId,
610 "InnerVlan": msg.Flow.Classifier.IVid,
611 "IntfId": msg.Flow.AccessIntfId,
612 "IpProto": msg.Flow.Classifier.IpProto,
613 "OnuId": msg.Flow.OnuId,
614 "OnuSn": o.Sn(),
615 "OuterVlan": msg.Flow.Classifier.OVid,
616 "PortNo": msg.Flow.PortNo,
617 "SrcPort": msg.Flow.Classifier.SrcPort,
618 "UniID": msg.Flow.UniId,
Matteo Scandolo3ac52792020-03-05 14:21:08 -0800619 "ClassifierOPbits": msg.Flow.Classifier.OPbits,
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700620 }).Debug("ONU receives FlowAdd")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700621
Matteo Scandolo813402b2019-10-23 19:24:52 -0700622 if msg.Flow.UniId != 0 {
623 // as of now BBSim only support a single UNI, so ignore everything that is not targeted to it
624 onuLogger.WithFields(log.Fields{
625 "IntfId": o.PonPortID,
626 "OnuId": o.ID,
627 "SerialNumber": o.Sn(),
628 }).Debug("Ignoring flow as it's not for the first UNI")
629 return
630 }
631
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700632 o.FlowIds = append(o.FlowIds, msg.Flow.FlowId)
Matteo Scandolo4a036262020-08-17 15:56:13 -0700633 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 -0700634
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700635 if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeEAPOL) && msg.Flow.Classifier.OVid == 4091 {
Matteo Scandolo27428702019-10-11 16:21:16 -0700636 // NOTE storing the PortNO, it's needed when sending PacketIndications
Matteo Scandolo813402b2019-10-23 19:24:52 -0700637 o.storePortNumber(uint32(msg.Flow.PortNo))
Matteo Scandolo4a036262020-08-17 15:56:13 -0700638
639 for _, s := range o.Services {
640 s.HandleAuth(o.PonPort.Olt.OpenoltStream)
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700641 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700642 } else if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeIPv4) &&
643 msg.Flow.Classifier.SrcPort == uint32(68) &&
Matteo Scandolo3ac52792020-03-05 14:21:08 -0800644 msg.Flow.Classifier.DstPort == uint32(67) &&
Matteo Scandolod74abba2020-04-16 16:36:44 -0700645 (msg.Flow.Classifier.OPbits == 0 || msg.Flow.Classifier.OPbits == 255) {
Matteo Scandolo99f18462019-10-28 14:14:28 -0700646
Matteo Scandolo4a036262020-08-17 15:56:13 -0700647 for _, s := range o.Services {
648 s.HandleDhcp(o.PonPort.Olt.OpenoltStream, int(msg.Flow.Classifier.OVid))
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700649 }
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700650 }
651}
652
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700653func (o *Onu) handleFlowRemove(msg OnuFlowUpdateMessage) {
654 onuLogger.WithFields(log.Fields{
655 "IntfId": o.PonPortID,
656 "OnuId": o.ID,
657 "SerialNumber": o.Sn(),
658 "FlowId": msg.Flow.FlowId,
659 "FlowType": msg.Flow.FlowType,
660 }).Debug("ONU receives FlowRemove")
661
662 for idx, flow := range o.FlowIds {
663 // If the gemport is found, delete it from local cache.
664 if flow == msg.Flow.FlowId {
665 o.FlowIds = append(o.FlowIds[:idx], o.FlowIds[idx+1:]...)
666 break
667 }
668 }
669
670 if len(o.FlowIds) == 0 {
671 onuLogger.WithFields(log.Fields{
672 "IntfId": o.PonPortID,
673 "OnuId": o.ID,
674 "SerialNumber": o.Sn(),
675 }).Info("Resetting GemPort")
676 o.GemPortAdded = false
677
Hardik Windlass7b3405b2020-07-08 15:10:05 +0530678 // check if ONU delete is performed and
679 // terminate the ONU's ProcessOnuMessages Go routine
680 if o.InternalState.Current() == "disabled" {
681 close(o.Channel)
682 }
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700683 }
684}
685
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700686// HexDecode converts the hex encoding to binary
687func HexDecode(pkt []byte) []byte {
688 p := make([]byte, len(pkt)/2)
689 for i, j := 0, 0; i < len(pkt); i, j = i+2, j+1 {
690 // Go figure this ;)
691 u := (pkt[i] & 15) + (pkt[i]>>6)*9
692 l := (pkt[i+1] & 15) + (pkt[i+1]>>6)*9
693 p[j] = u<<4 + l
694 }
695 onuLogger.Tracef("Omci decoded: %x.", p)
696 return p
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700697}
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700698
699// BBR methods
700
701func sendOmciMsg(pktBytes []byte, intfId uint32, onuId uint32, sn *openolt.SerialNumber, msgType string, client openolt.OpenoltClient) {
702 omciMsg := openolt.OmciMsg{
703 IntfId: intfId,
704 OnuId: onuId,
705 Pkt: pktBytes,
706 }
707
708 if _, err := client.OmciMsgOut(context.Background(), &omciMsg); err != nil {
709 log.WithFields(log.Fields{
710 "IntfId": intfId,
711 "OnuId": onuId,
712 "SerialNumber": common.OnuSnToString(sn),
713 "Pkt": omciMsg.Pkt,
714 }).Fatalf("Failed to send MIB Reset")
715 }
716 log.WithFields(log.Fields{
717 "IntfId": intfId,
718 "OnuId": onuId,
719 "SerialNumber": common.OnuSnToString(sn),
720 "Pkt": omciMsg.Pkt,
721 }).Tracef("Sent OMCI message %s", msgType)
722}
723
724func (onu *Onu) getNextTid(highPriority ...bool) uint16 {
725 var next uint16
726 if len(highPriority) > 0 && highPriority[0] {
727 next = onu.hpTid
728 onu.hpTid += 1
729 if onu.hpTid < 0x8000 {
730 onu.hpTid = 0x8000
731 }
732 } else {
733 next = onu.tid
734 onu.tid += 1
735 if onu.tid >= 0x8000 {
736 onu.tid = 1
737 }
738 }
739 return next
740}
741
742// TODO move this method in responders/omcisim
743func (o *Onu) StartOmci(client openolt.OpenoltClient) {
744 mibReset, _ := omcilib.CreateMibResetRequest(o.getNextTid(false))
745 sendOmciMsg(mibReset, o.PonPortID, o.ID, o.SerialNumber, "mibReset", client)
746}
747
748func (o *Onu) handleOmci(msg OmciIndicationMessage, client openolt.OpenoltClient) {
749 msgType, packet := omcilib.DecodeOmci(msg.OmciInd.Pkt)
750
751 log.WithFields(log.Fields{
752 "IntfId": msg.OmciInd.IntfId,
753 "OnuId": msg.OmciInd.OnuId,
754 "OnuSn": common.OnuSnToString(o.SerialNumber),
755 "Pkt": msg.OmciInd.Pkt,
756 "msgType": msgType,
Anand S Katti09541352020-01-29 15:54:01 +0530757 }).Trace("ONU Receives OMCI Msg")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700758 switch msgType {
759 default:
Matteo Scandolo813402b2019-10-23 19:24:52 -0700760 log.WithFields(log.Fields{
761 "IntfId": msg.OmciInd.IntfId,
762 "OnuId": msg.OmciInd.OnuId,
763 "OnuSn": common.OnuSnToString(o.SerialNumber),
764 "Pkt": msg.OmciInd.Pkt,
765 "msgType": msgType,
766 }).Fatalf("unexpected frame: %v", packet)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700767 case omci.MibResetResponseType:
768 mibUpload, _ := omcilib.CreateMibUploadRequest(o.getNextTid(false))
769 sendOmciMsg(mibUpload, o.PonPortID, o.ID, o.SerialNumber, "mibUpload", client)
770 case omci.MibUploadResponseType:
771 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
772 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
773 case omci.MibUploadNextResponseType:
774 o.seqNumber++
775
776 if o.seqNumber > 290 {
777 // NOTE we are done with the MIB Upload (290 is the number of messages the omci-sim library will respond to)
778 galEnet, _ := omcilib.CreateGalEnetRequest(o.getNextTid(false))
779 sendOmciMsg(galEnet, o.PonPortID, o.ID, o.SerialNumber, "CreateGalEnetRequest", client)
780 } else {
781 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
782 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
783 }
784 case omci.CreateResponseType:
785 // NOTE Creating a GemPort,
786 // BBsim actually doesn't care about the values, so we can do we want with the parameters
787 // In the same way we can create a GemPort even without setting up UNIs/TConts/...
788 // but we need the GemPort to trigger the state change
789
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700790 if !o.GemPortAdded {
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700791 // NOTE this sends a CreateRequestType and BBSim replies with a CreateResponseType
792 // thus we send this request only once
793 gemReq, _ := omcilib.CreateGemPortRequest(o.getNextTid(false))
794 sendOmciMsg(gemReq, o.PonPortID, o.ID, o.SerialNumber, "CreateGemPortRequest", client)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700795 o.GemPortAdded = true
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700796 } else {
797 if err := o.InternalState.Event("send_eapol_flow"); err != nil {
798 onuLogger.WithFields(log.Fields{
799 "OnuId": o.ID,
800 "IntfId": o.PonPortID,
801 "OnuSn": o.Sn(),
802 }).Errorf("Error while transitioning ONU State %v", err)
803 }
804 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700805 }
806}
807
808func (o *Onu) sendEapolFlow(client openolt.OpenoltClient) {
809
810 classifierProto := openolt.Classifier{
811 EthType: uint32(layers.EthernetTypeEAPOL),
812 OVid: 4091,
813 }
814
815 actionProto := openolt.Action{}
816
817 downstreamFlow := openolt.Flow{
818 AccessIntfId: int32(o.PonPortID),
819 OnuId: int32(o.ID),
Matteo Scandolo813402b2019-10-23 19:24:52 -0700820 UniId: int32(0), // NOTE do not hardcode this, we need to support multiple UNIs
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700821 FlowId: uint32(o.ID),
822 FlowType: "downstream",
823 AllocId: int32(0),
824 NetworkIntfId: int32(0),
825 GemportId: int32(1), // FIXME use the same value as CreateGemPortRequest PortID, do not hardcode
826 Classifier: &classifierProto,
827 Action: &actionProto,
828 Priority: int32(100),
829 Cookie: uint64(o.ID),
830 PortNo: uint32(o.ID), // NOTE we are using this to map an incoming packetIndication to an ONU
831 }
832
833 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
834 log.WithFields(log.Fields{
835 "IntfId": o.PonPortID,
836 "OnuId": o.ID,
837 "FlowId": downstreamFlow.FlowId,
838 "PortNo": downstreamFlow.PortNo,
839 "SerialNumber": common.OnuSnToString(o.SerialNumber),
Matteo Scandolob0e3e622020-04-23 17:00:29 -0700840 }).Fatalf("Failed to add EAPOL Flow")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700841 }
842 log.WithFields(log.Fields{
843 "IntfId": o.PonPortID,
844 "OnuId": o.ID,
845 "FlowId": downstreamFlow.FlowId,
846 "PortNo": downstreamFlow.PortNo,
847 "SerialNumber": common.OnuSnToString(o.SerialNumber),
848 }).Info("Sent EAPOL Flow")
849}
850
851func (o *Onu) sendDhcpFlow(client openolt.OpenoltClient) {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700852
853 // BBR only works with a single service (ATT HSIA)
854 hsia := o.Services[0].(*Service)
855
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700856 classifierProto := openolt.Classifier{
857 EthType: uint32(layers.EthernetTypeIPv4),
858 SrcPort: uint32(68),
859 DstPort: uint32(67),
Matteo Scandolo4a036262020-08-17 15:56:13 -0700860 OVid: uint32(hsia.CTag),
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700861 }
862
863 actionProto := openolt.Action{}
864
865 downstreamFlow := openolt.Flow{
866 AccessIntfId: int32(o.PonPortID),
867 OnuId: int32(o.ID),
Matteo Scandolo813402b2019-10-23 19:24:52 -0700868 UniId: int32(0), // FIXME do not hardcode this
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700869 FlowId: uint32(o.ID),
870 FlowType: "downstream",
871 AllocId: int32(0),
872 NetworkIntfId: int32(0),
873 GemportId: int32(1), // FIXME use the same value as CreateGemPortRequest PortID, do not hardcode
874 Classifier: &classifierProto,
875 Action: &actionProto,
876 Priority: int32(100),
877 Cookie: uint64(o.ID),
878 PortNo: uint32(o.ID), // NOTE we are using this to map an incoming packetIndication to an ONU
879 }
880
881 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
882 log.WithFields(log.Fields{
883 "IntfId": o.PonPortID,
884 "OnuId": o.ID,
885 "FlowId": downstreamFlow.FlowId,
886 "PortNo": downstreamFlow.PortNo,
887 "SerialNumber": common.OnuSnToString(o.SerialNumber),
888 }).Fatalf("Failed to send DHCP Flow")
889 }
890 log.WithFields(log.Fields{
891 "IntfId": o.PonPortID,
892 "OnuId": o.ID,
893 "FlowId": downstreamFlow.FlowId,
894 "PortNo": downstreamFlow.PortNo,
895 "SerialNumber": common.OnuSnToString(o.SerialNumber),
896 }).Info("Sent DHCP Flow")
897}
Pragya Arya8bdb4532020-03-02 17:08:09 +0530898
899// DeleteFlow method search and delete flowKey from the onu flows slice
900func (onu *Onu) DeleteFlow(key FlowKey) {
901 for pos, flowKey := range onu.Flows {
902 if flowKey == key {
903 // delete the flowKey by shifting all flowKeys by one
904 onu.Flows = append(onu.Flows[:pos], onu.Flows[pos+1:]...)
905 t := make([]FlowKey, len(onu.Flows))
906 copy(t, onu.Flows)
907 onu.Flows = t
908 break
909 }
910 }
911}
Hardik Windlass7b3405b2020-07-08 15:10:05 +0530912
913func (onu *Onu) ReDiscoverOnu() {
914 // Wait for few seconds to be sure of the cleanup
915 time.Sleep(5 * time.Second)
916
917 onuLogger.WithFields(log.Fields{
918 "IntfId": onu.PonPortID,
919 "OnuId": onu.ID,
920 "OnuSn": onu.Sn(),
921 }).Debug("Send ONU Re-Discovery")
922
923 // ONU Re-Discovery
924 if err := onu.InternalState.Event("initialize"); err != nil {
925 log.WithFields(log.Fields{
926 "IntfId": onu.PonPortID,
927 "OnuSn": onu.Sn(),
928 "OnuId": onu.ID,
929 }).Infof("Failed to transition ONU to initialized state: %s", err.Error())
930 }
931
932 if err := onu.InternalState.Event("discover"); err != nil {
933 log.WithFields(log.Fields{
934 "IntfId": onu.PonPortID,
935 "OnuSn": onu.Sn(),
936 "OnuId": onu.ID,
937 }).Infof("Failed to transition ONU to discovered state: %s", err.Error())
938 }
939}
Matteo Scandolo4a036262020-08-17 15:56:13 -0700940
941func (onu *Onu) addGemPortToService(gemport uint32, ethType uint32, oVlan uint32, iVlan uint32) {
942 for _, s := range onu.Services {
943 if service, ok := s.(*Service); ok {
944 // EAPOL is a strange case, as packets are untagged
945 // but we assume we will have a single service requiring EAPOL
946 if ethType == uint32(layers.EthernetTypeEAPOL) && service.NeedsEapol {
947 service.GemPort = gemport
948 }
949
950 // For DHCP services we single tag the outgoing packets,
951 // thus the flow only contains the CTag and we can use that to match the service
952 if ethType == uint32(layers.EthernetTypeIPv4) && service.NeedsDhcp && service.CTag == int(oVlan) {
953 service.GemPort = gemport
954 }
955
956 // for dataplane services match both C and S tags
957 if service.CTag == int(iVlan) && service.STag == int(oVlan) {
958 service.GemPort = gemport
959 }
960 }
961 }
962}
963
964func (onu *Onu) findServiceByMacAddress(macAddress net.HardwareAddr) (*Service, error) {
965 for _, s := range onu.Services {
966 service := s.(*Service)
967 if service.HwAddress.String() == macAddress.String() {
968 return service, nil
969 }
970 }
971 return nil, fmt.Errorf("cannot-find-service-with-mac-address-%s", macAddress.String())
972}