blob: 9116b318137f026d05913351aaced3014682b33c [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 {
185 s.Initialize()
186 go s.HandlePackets(o.PonPort.Olt.OpenoltStream)
187 }
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 },
Arjun E K57a7fcb2020-01-30 06:44:45 +0000239 "igmp_join_start": func(e *fsm.Event) {
240 msg := Message{
241 Type: IGMPMembershipReportV2,
242 }
243 o.Channel <- msg
244 },
245 "igmp_leave": func(e *fsm.Event) {
246 msg := Message{
247 Type: IGMPLeaveGroup}
248 o.Channel <- msg
249 },
Anand S Katti09541352020-01-29 15:54:01 +0530250 "igmp_join_startv3": func(e *fsm.Event) {
251 msg := Message{
252 Type: IGMPMembershipReportV3,
253 }
254 o.Channel <- msg
255 },
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700256 },
257 )
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100258
Matteo Scandolo27428702019-10-11 16:21:16 -0700259 return &o
Matteo Scandolo4747d292019-08-05 11:50:18 -0700260}
261
William Kurkian0418bc82019-11-06 12:16:24 -0500262func (o *Onu) logStateChange(src string, dst string) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700263 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700264 "OnuId": o.ID,
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700265 "IntfId": o.PonPortID,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700266 "OnuSn": o.Sn(),
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700267 }).Debugf("Changing ONU InternalState from %s to %s", src, dst)
268}
269
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100270// ProcessOnuMessages starts indication channel for each ONU
David Bainbridge103cf022019-12-16 20:11:35 +0000271func (o *Onu) ProcessOnuMessages(ctx context.Context, stream openolt.Openolt_EnableIndicationServer, client openolt.OpenoltClient) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700272 onuLogger.WithFields(log.Fields{
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100273 "onuID": o.ID,
274 "onuSN": o.Sn(),
275 "ponPort": o.PonPortID,
276 }).Debug("Starting ONU Indication Channel")
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700277
David Bainbridge103cf022019-12-16 20:11:35 +0000278loop:
279 for {
280 select {
281 case <-ctx.Done():
282 onuLogger.WithFields(log.Fields{
283 "onuID": o.ID,
284 "onuSN": o.Sn(),
285 }).Tracef("ONU message handling canceled via context")
286 break loop
287 case message, ok := <-o.Channel:
288 if !ok || ctx.Err() != nil {
289 onuLogger.WithFields(log.Fields{
290 "onuID": o.ID,
291 "onuSN": o.Sn(),
292 }).Tracef("ONU message handling canceled via channel close")
293 break loop
Matteo Scandolo075b1892019-10-07 12:11:07 -0700294 }
David Bainbridge103cf022019-12-16 20:11:35 +0000295 onuLogger.WithFields(log.Fields{
296 "onuID": o.ID,
297 "onuSN": o.Sn(),
298 "messageType": message.Type,
299 }).Tracef("Received message on ONU Channel")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700300
David Bainbridge103cf022019-12-16 20:11:35 +0000301 switch message.Type {
302 case OnuDiscIndication:
303 msg, _ := message.Data.(OnuDiscIndicationMessage)
304 // 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 +0530305 time.Sleep(o.DiscoveryDelay)
David Bainbridge103cf022019-12-16 20:11:35 +0000306 o.sendOnuDiscIndication(msg, stream)
307 case OnuIndication:
308 msg, _ := message.Data.(OnuIndicationMessage)
309 o.sendOnuIndication(msg, stream)
310 case OMCI:
311 msg, _ := message.Data.(OmciMessage)
312 o.handleOmciMessage(msg, stream)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700313 case FlowAdd:
David Bainbridge103cf022019-12-16 20:11:35 +0000314 msg, _ := message.Data.(OnuFlowUpdateMessage)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700315 o.handleFlowAdd(msg)
316 case FlowRemoved:
317 msg, _ := message.Data.(OnuFlowUpdateMessage)
318 o.handleFlowRemove(msg)
David Bainbridge103cf022019-12-16 20:11:35 +0000319 case OnuPacketOut:
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700320
David Bainbridge103cf022019-12-16 20:11:35 +0000321 msg, _ := message.Data.(OnuPacketMessage)
322
323 log.WithFields(log.Fields{
324 "IntfId": msg.IntfId,
325 "OnuId": msg.OnuId,
326 "pktType": msg.Type,
327 }).Trace("Received OnuPacketOut Message")
328
Matteo Scandolo4a036262020-08-17 15:56:13 -0700329 service, err := o.findServiceByMacAddress(msg.MacAddress)
330 if err != nil {
331 onuLogger.WithFields(log.Fields{
332 "IntfId": msg.IntfId,
333 "OnuId": msg.OnuId,
334 "pktType": msg.Type,
335 "MacAddress": msg.MacAddress,
336 "OnuSn": o.Sn(),
337 }).Error("Cannot find Service associated with packet")
338 return
David Bainbridge103cf022019-12-16 20:11:35 +0000339 }
Matteo Scandolo4a036262020-08-17 15:56:13 -0700340
341 service.PacketCh <- msg
342
David Bainbridge103cf022019-12-16 20:11:35 +0000343 case OnuPacketIn:
344 // NOTE we only receive BBR packets here.
345 // Eapol.HandleNextPacket can handle both BBSim and BBr cases so the call is the same
346 // in the DHCP case VOLTHA only act as a proxy, the behaviour is completely different thus we have a dhcp.HandleNextBbrPacket
347 msg, _ := message.Data.(OnuPacketMessage)
348
349 log.WithFields(log.Fields{
350 "IntfId": msg.IntfId,
351 "OnuId": msg.OnuId,
352 "pktType": msg.Type,
353 }).Trace("Received OnuPacketIn Message")
354
355 if msg.Type == packetHandlers.EAPOL {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700356 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 +0000357 } else if msg.Type == packetHandlers.DHCP {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700358 _ = dhcp.HandleNextBbrPacket(o.ID, o.PonPortID, o.Sn(), o.DoneChannel, msg.Packet, client)
David Bainbridge103cf022019-12-16 20:11:35 +0000359 }
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700360 // BBR specific messages
David Bainbridge103cf022019-12-16 20:11:35 +0000361 case OmciIndication:
362 msg, _ := message.Data.(OmciIndicationMessage)
363 o.handleOmci(msg, client)
364 case SendEapolFlow:
365 o.sendEapolFlow(client)
366 case SendDhcpFlow:
367 o.sendDhcpFlow(client)
368 default:
369 onuLogger.Warnf("Received unknown message data %v for type %v in OLT Channel", message.Data, message.Type)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700370 }
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700371 }
372 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100373 onuLogger.WithFields(log.Fields{
374 "onuID": o.ID,
375 "onuSN": o.Sn(),
376 }).Debug("Stopped handling ONU Indication Channel")
Matteo Scandolo4747d292019-08-05 11:50:18 -0700377}
378
Matteo Scandolodf3f85d2020-01-15 12:50:48 -0800379func (o *Onu) processOmciMessage(message omcisim.OmciChMessage, stream openolt.Openolt_EnableIndicationServer) {
William Kurkian9dadc5b2019-10-22 13:51:57 -0400380 switch message.Type {
Matteo Scandolodf3f85d2020-01-15 12:50:48 -0800381 case omcisim.UniLinkUp, omcisim.UniLinkDown:
382 onuLogger.WithFields(log.Fields{
383 "OnuId": message.Data.OnuId,
384 "IntfId": message.Data.IntfId,
385 "Type": message.Type,
Matteo Scandolo4a036262020-08-17 15:56:13 -0700386 }).Debug("UNI Link Alarm")
Matteo Scandolodf3f85d2020-01-15 12:50:48 -0800387 // TODO send to OLT
388
389 omciInd := openolt.OmciIndication{
390 IntfId: message.Data.IntfId,
391 OnuId: message.Data.OnuId,
392 Pkt: message.Packet,
393 }
394
395 omci := &openolt.Indication_OmciInd{OmciInd: &omciInd}
396 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
397 onuLogger.WithFields(log.Fields{
398 "IntfId": o.PonPortID,
399 "SerialNumber": o.Sn(),
400 "Type": message.Type,
401 "omciPacket": omciInd.Pkt,
402 }).Errorf("Failed to send UNI Link Alarm: %v", err)
403 return
404 }
405
406 onuLogger.WithFields(log.Fields{
407 "IntfId": o.PonPortID,
408 "SerialNumber": o.Sn(),
409 "Type": message.Type,
410 "omciPacket": omciInd.Pkt,
Matteo Scandolo4a036262020-08-17 15:56:13 -0700411 }).Debug("UNI Link alarm sent")
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700412 }
413}
414
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100415func (o Onu) NewSN(oltid int, intfid uint32, onuid uint32) *openolt.SerialNumber {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700416
417 sn := new(openolt.SerialNumber)
418
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700419 //sn = new(openolt.SerialNumber)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700420 sn.VendorId = []byte("BBSM")
421 sn.VendorSpecific = []byte{0, byte(oltid % 256), byte(intfid), byte(onuid)}
422
423 return sn
424}
425
William Kurkian0418bc82019-11-06 12:16:24 -0500426func (o *Onu) sendOnuDiscIndication(msg OnuDiscIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700427 discoverData := &openolt.Indication_OnuDiscInd{OnuDiscInd: &openolt.OnuDiscIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700428 IntfId: msg.Onu.PonPortID,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700429 SerialNumber: msg.Onu.SerialNumber,
430 }}
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700431
Matteo Scandolo4747d292019-08-05 11:50:18 -0700432 if err := stream.Send(&openolt.Indication{Data: discoverData}); err != nil {
Matteo Scandolo11006992019-08-28 11:29:46 -0700433 log.Errorf("Failed to send Indication_OnuDiscInd: %v", err)
Matteo Scandolo99f18462019-10-28 14:14:28 -0700434 return
Matteo Scandolo4747d292019-08-05 11:50:18 -0700435 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700436
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700437 onuLogger.WithFields(log.Fields{
Matteo Scandolo4747d292019-08-05 11:50:18 -0700438 "IntfId": msg.Onu.PonPortID,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700439 "OnuSn": msg.Onu.Sn(),
440 "OnuId": o.ID,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700441 }).Debug("Sent Indication_OnuDiscInd")
Pragya Arya324337e2020-02-20 14:35:08 +0530442 publishEvent("ONU-discovery-indication-sent", int32(msg.Onu.PonPortID), int32(o.ID), msg.Onu.Sn())
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800443
444 // after DiscoveryRetryDelay check if the state is the same and in case send a new OnuDiscIndication
445 go func(delay time.Duration) {
Matteo Scandolo569e7172019-12-20 11:51:51 -0800446 time.Sleep(delay)
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800447 if o.InternalState.Current() == "discovered" {
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800448 o.sendOnuDiscIndication(msg, stream)
449 }
450 }(o.DiscoveryRetryDelay)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700451}
452
William Kurkian0418bc82019-11-06 12:16:24 -0500453func (o *Onu) sendOnuIndication(msg OnuIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700454 // NOTE voltha returns an ID, but if we use that ID then it complains:
455 // expected_onu_id: 1, received_onu_id: 1024, event: ONU-id-mismatch, can happen if both voltha and the olt rebooted
456 // so we're using the internal ID that is 1
457 // o.ID = msg.OnuID
Matteo Scandolo4747d292019-08-05 11:50:18 -0700458
459 indData := &openolt.Indication_OnuInd{OnuInd: &openolt.OnuIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700460 IntfId: o.PonPortID,
461 OnuId: o.ID,
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700462 OperState: msg.OperState.String(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700463 AdminState: o.OperState.Current(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700464 SerialNumber: o.SerialNumber,
465 }}
466 if err := stream.Send(&openolt.Indication{Data: indData}); err != nil {
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800467 // NOTE do we need to transition to a broken state?
Matteo Scandolo11006992019-08-28 11:29:46 -0700468 log.Errorf("Failed to send Indication_OnuInd: %v", err)
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700469 return
Matteo Scandolo4747d292019-08-05 11:50:18 -0700470 }
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700471 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700472 "IntfId": o.PonPortID,
473 "OnuId": o.ID,
474 "OperState": msg.OperState.String(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700475 "AdminState": msg.OperState.String(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700476 "OnuSn": o.Sn(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700477 }).Debug("Sent Indication_OnuInd")
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700478
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700479}
480
Pragya Arya324337e2020-02-20 14:35:08 +0530481func (o *Onu) publishOmciEvent(msg OmciMessage) {
482 if olt.PublishEvents {
483 _, _, msgType, _, _, _, err := omcisim.ParsePkt(HexDecode(msg.omciMsg.Pkt))
484 if err != nil {
485 log.Errorf("error in getting msgType %v", err)
486 return
487 }
488 if msgType == omcisim.MibUpload {
489 o.seqNumber = 0
490 publishEvent("MIB-upload-received", int32(o.PonPortID), int32(o.ID), common.OnuSnToString(o.SerialNumber))
491 } else if msgType == omcisim.MibUploadNext {
492 o.seqNumber++
493 if o.seqNumber > 290 {
494 publishEvent("MIB-upload-done", int32(o.PonPortID), int32(o.ID), common.OnuSnToString(o.SerialNumber))
495 }
496 }
497 }
498}
499
Scott Bakerb90c4312020-03-12 21:33:25 -0700500// Create a TestResponse packet and send it
501func (o *Onu) sendTestResult(msg OmciMessage, stream openolt.Openolt_EnableIndicationServer) error {
502 resp, err := omcilib.BuildTestResult(HexDecode(msg.omciMsg.Pkt))
503 if err != nil {
504 return err
505 }
506
507 var omciInd openolt.OmciIndication
508 omciInd.IntfId = o.PonPortID
509 omciInd.OnuId = o.ID
510 omciInd.Pkt = resp
511
512 omci := &openolt.Indication_OmciInd{OmciInd: &omciInd}
513 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
514 onuLogger.WithFields(log.Fields{
515 "IntfId": o.PonPortID,
516 "SerialNumber": o.Sn(),
517 "omciPacket": omciInd.Pkt,
518 "msg": msg,
519 }).Errorf("send TestResult omcisim indication failed: %v", err)
520 return err
521 }
522 onuLogger.WithFields(log.Fields{
523 "IntfId": o.PonPortID,
524 "SerialNumber": o.Sn(),
525 "omciPacket": omciInd.Pkt,
526 }).Tracef("Sent TestResult OMCI message")
527
528 return nil
529}
530
William Kurkian0418bc82019-11-06 12:16:24 -0500531func (o *Onu) handleOmciMessage(msg OmciMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700532
533 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700534 "IntfId": o.PonPortID,
Matteo Scandolo27428702019-10-11 16:21:16 -0700535 "SerialNumber": o.Sn(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700536 "omciPacket": msg.omciMsg.Pkt,
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700537 }).Tracef("Received OMCI message")
538
Pragya Arya324337e2020-02-20 14:35:08 +0530539 o.publishOmciEvent(msg)
540
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700541 var omciInd openolt.OmciIndication
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700542 respPkt, err := omcisim.OmciSim(o.PonPortID, o.ID, HexDecode(msg.omciMsg.Pkt))
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700543 if err != nil {
Matteo Scandolo27428702019-10-11 16:21:16 -0700544 onuLogger.WithFields(log.Fields{
545 "IntfId": o.PonPortID,
546 "SerialNumber": o.Sn(),
547 "omciPacket": omciInd.Pkt,
548 "msg": msg,
549 }).Errorf("Error handling OMCI message %v", msg)
550 return
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700551 }
552
553 omciInd.IntfId = o.PonPortID
554 omciInd.OnuId = o.ID
555 omciInd.Pkt = respPkt
556
557 omci := &openolt.Indication_OmciInd{OmciInd: &omciInd}
558 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
Matteo Scandolo27428702019-10-11 16:21:16 -0700559 onuLogger.WithFields(log.Fields{
560 "IntfId": o.PonPortID,
561 "SerialNumber": o.Sn(),
562 "omciPacket": omciInd.Pkt,
563 "msg": msg,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700564 }).Errorf("send omcisim indication failed: %v", err)
Matteo Scandolo27428702019-10-11 16:21:16 -0700565 return
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700566 }
567 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700568 "IntfId": o.PonPortID,
Matteo Scandolo27428702019-10-11 16:21:16 -0700569 "SerialNumber": o.Sn(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700570 "omciPacket": omciInd.Pkt,
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700571 }).Tracef("Sent OMCI message")
Scott Bakerb90c4312020-03-12 21:33:25 -0700572
573 // Test message is special, it requires sending two packets:
574 // first packet: TestResponse, says whether test was started successully, handled by omci-sim
575 // second packet, TestResult, reports the result of running the self-test
576 // TestResult can come some time after a TestResponse
577 // TODO: Implement some delay between the TestResponse and the TestResult
578 isTest, err := omcilib.IsTestRequest(HexDecode(msg.omciMsg.Pkt))
579 if (err == nil) && (isTest) {
Shrey Baid688b4242020-07-10 20:40:10 +0530580 _ = o.sendTestResult(msg, stream)
Scott Bakerb90c4312020-03-12 21:33:25 -0700581 }
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700582}
583
Matteo Scandolo27428702019-10-11 16:21:16 -0700584func (o *Onu) storePortNumber(portNo uint32) {
Matteo Scandolo813402b2019-10-23 19:24:52 -0700585 // NOTE this needed only as long as we don't support multiple UNIs
Matteo Scandolo27428702019-10-11 16:21:16 -0700586 // we need to add support for multiple UNIs
587 // the action plan is:
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700588 // - refactor the omcisim-sim library to use https://github.com/cboling/omci instead of canned messages
Matteo Scandolo27428702019-10-11 16:21:16 -0700589 // - change the library so that it reports a single UNI and remove this workaroung
590 // - add support for multiple UNIs in BBSim
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700591 if o.PortNo == 0 || portNo < o.PortNo {
Matteo Scandolo813402b2019-10-23 19:24:52 -0700592 onuLogger.WithFields(log.Fields{
593 "IntfId": o.PonPortID,
594 "OnuId": o.ID,
595 "SerialNumber": o.Sn(),
596 "OnuPortNo": o.PortNo,
597 "FlowPortNo": portNo,
598 }).Debug("Storing ONU portNo")
Matteo Scandolo27428702019-10-11 16:21:16 -0700599 o.PortNo = portNo
600 }
601}
602
William Kurkian0418bc82019-11-06 12:16:24 -0500603func (o *Onu) SetID(id uint32) {
Matteo Scandolo583f17d2020-02-13 10:35:17 -0800604 onuLogger.WithFields(log.Fields{
605 "IntfId": o.PonPortID,
606 "OnuId": id,
607 "SerialNumber": o.Sn(),
608 }).Debug("Storing OnuId ")
William Kurkian0418bc82019-11-06 12:16:24 -0500609 o.ID = id
610}
611
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700612func (o *Onu) handleFlowAdd(msg OnuFlowUpdateMessage) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700613 onuLogger.WithFields(log.Fields{
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800614 "DstPort": msg.Flow.Classifier.DstPort,
615 "EthType": fmt.Sprintf("%x", msg.Flow.Classifier.EthType),
616 "FlowId": msg.Flow.FlowId,
617 "FlowType": msg.Flow.FlowType,
618 "GemportId": msg.Flow.GemportId,
619 "InnerVlan": msg.Flow.Classifier.IVid,
620 "IntfId": msg.Flow.AccessIntfId,
621 "IpProto": msg.Flow.Classifier.IpProto,
622 "OnuId": msg.Flow.OnuId,
623 "OnuSn": o.Sn(),
624 "OuterVlan": msg.Flow.Classifier.OVid,
625 "PortNo": msg.Flow.PortNo,
626 "SrcPort": msg.Flow.Classifier.SrcPort,
627 "UniID": msg.Flow.UniId,
Matteo Scandolo3ac52792020-03-05 14:21:08 -0800628 "ClassifierOPbits": msg.Flow.Classifier.OPbits,
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700629 }).Debug("ONU receives FlowAdd")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700630
Matteo Scandolo813402b2019-10-23 19:24:52 -0700631 if msg.Flow.UniId != 0 {
632 // as of now BBSim only support a single UNI, so ignore everything that is not targeted to it
633 onuLogger.WithFields(log.Fields{
634 "IntfId": o.PonPortID,
635 "OnuId": o.ID,
636 "SerialNumber": o.Sn(),
637 }).Debug("Ignoring flow as it's not for the first UNI")
638 return
639 }
640
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700641 o.FlowIds = append(o.FlowIds, msg.Flow.FlowId)
Matteo Scandolo4a036262020-08-17 15:56:13 -0700642 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 -0700643
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700644 if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeEAPOL) && msg.Flow.Classifier.OVid == 4091 {
Matteo Scandolo27428702019-10-11 16:21:16 -0700645 // NOTE storing the PortNO, it's needed when sending PacketIndications
Matteo Scandolo813402b2019-10-23 19:24:52 -0700646 o.storePortNumber(uint32(msg.Flow.PortNo))
Matteo Scandolo4a036262020-08-17 15:56:13 -0700647
648 for _, s := range o.Services {
649 s.HandleAuth(o.PonPort.Olt.OpenoltStream)
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700650 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700651 } else if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeIPv4) &&
652 msg.Flow.Classifier.SrcPort == uint32(68) &&
Matteo Scandolo3ac52792020-03-05 14:21:08 -0800653 msg.Flow.Classifier.DstPort == uint32(67) &&
Matteo Scandolod74abba2020-04-16 16:36:44 -0700654 (msg.Flow.Classifier.OPbits == 0 || msg.Flow.Classifier.OPbits == 255) {
Matteo Scandolo99f18462019-10-28 14:14:28 -0700655
Matteo Scandolo4a036262020-08-17 15:56:13 -0700656 for _, s := range o.Services {
657 s.HandleDhcp(o.PonPort.Olt.OpenoltStream, int(msg.Flow.Classifier.OVid))
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700658 }
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700659 }
660}
661
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700662func (o *Onu) handleFlowRemove(msg OnuFlowUpdateMessage) {
663 onuLogger.WithFields(log.Fields{
664 "IntfId": o.PonPortID,
665 "OnuId": o.ID,
666 "SerialNumber": o.Sn(),
667 "FlowId": msg.Flow.FlowId,
668 "FlowType": msg.Flow.FlowType,
669 }).Debug("ONU receives FlowRemove")
670
671 for idx, flow := range o.FlowIds {
672 // If the gemport is found, delete it from local cache.
673 if flow == msg.Flow.FlowId {
674 o.FlowIds = append(o.FlowIds[:idx], o.FlowIds[idx+1:]...)
675 break
676 }
677 }
678
679 if len(o.FlowIds) == 0 {
680 onuLogger.WithFields(log.Fields{
681 "IntfId": o.PonPortID,
682 "OnuId": o.ID,
683 "SerialNumber": o.Sn(),
684 }).Info("Resetting GemPort")
685 o.GemPortAdded = false
686
Hardik Windlass7b3405b2020-07-08 15:10:05 +0530687 // check if ONU delete is performed and
688 // terminate the ONU's ProcessOnuMessages Go routine
689 if o.InternalState.Current() == "disabled" {
690 close(o.Channel)
691 }
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700692 }
693}
694
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700695// HexDecode converts the hex encoding to binary
696func HexDecode(pkt []byte) []byte {
697 p := make([]byte, len(pkt)/2)
698 for i, j := 0, 0; i < len(pkt); i, j = i+2, j+1 {
699 // Go figure this ;)
700 u := (pkt[i] & 15) + (pkt[i]>>6)*9
701 l := (pkt[i+1] & 15) + (pkt[i+1]>>6)*9
702 p[j] = u<<4 + l
703 }
704 onuLogger.Tracef("Omci decoded: %x.", p)
705 return p
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700706}
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700707
708// BBR methods
709
710func sendOmciMsg(pktBytes []byte, intfId uint32, onuId uint32, sn *openolt.SerialNumber, msgType string, client openolt.OpenoltClient) {
711 omciMsg := openolt.OmciMsg{
712 IntfId: intfId,
713 OnuId: onuId,
714 Pkt: pktBytes,
715 }
716
717 if _, err := client.OmciMsgOut(context.Background(), &omciMsg); err != nil {
718 log.WithFields(log.Fields{
719 "IntfId": intfId,
720 "OnuId": onuId,
721 "SerialNumber": common.OnuSnToString(sn),
722 "Pkt": omciMsg.Pkt,
723 }).Fatalf("Failed to send MIB Reset")
724 }
725 log.WithFields(log.Fields{
726 "IntfId": intfId,
727 "OnuId": onuId,
728 "SerialNumber": common.OnuSnToString(sn),
729 "Pkt": omciMsg.Pkt,
730 }).Tracef("Sent OMCI message %s", msgType)
731}
732
733func (onu *Onu) getNextTid(highPriority ...bool) uint16 {
734 var next uint16
735 if len(highPriority) > 0 && highPriority[0] {
736 next = onu.hpTid
737 onu.hpTid += 1
738 if onu.hpTid < 0x8000 {
739 onu.hpTid = 0x8000
740 }
741 } else {
742 next = onu.tid
743 onu.tid += 1
744 if onu.tid >= 0x8000 {
745 onu.tid = 1
746 }
747 }
748 return next
749}
750
751// TODO move this method in responders/omcisim
752func (o *Onu) StartOmci(client openolt.OpenoltClient) {
753 mibReset, _ := omcilib.CreateMibResetRequest(o.getNextTid(false))
754 sendOmciMsg(mibReset, o.PonPortID, o.ID, o.SerialNumber, "mibReset", client)
755}
756
757func (o *Onu) handleOmci(msg OmciIndicationMessage, client openolt.OpenoltClient) {
758 msgType, packet := omcilib.DecodeOmci(msg.OmciInd.Pkt)
759
760 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,
Anand S Katti09541352020-01-29 15:54:01 +0530766 }).Trace("ONU Receives OMCI Msg")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700767 switch msgType {
768 default:
Matteo Scandolo813402b2019-10-23 19:24:52 -0700769 log.WithFields(log.Fields{
770 "IntfId": msg.OmciInd.IntfId,
771 "OnuId": msg.OmciInd.OnuId,
772 "OnuSn": common.OnuSnToString(o.SerialNumber),
773 "Pkt": msg.OmciInd.Pkt,
774 "msgType": msgType,
775 }).Fatalf("unexpected frame: %v", packet)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700776 case omci.MibResetResponseType:
777 mibUpload, _ := omcilib.CreateMibUploadRequest(o.getNextTid(false))
778 sendOmciMsg(mibUpload, o.PonPortID, o.ID, o.SerialNumber, "mibUpload", client)
779 case omci.MibUploadResponseType:
780 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
781 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
782 case omci.MibUploadNextResponseType:
783 o.seqNumber++
784
785 if o.seqNumber > 290 {
786 // NOTE we are done with the MIB Upload (290 is the number of messages the omci-sim library will respond to)
787 galEnet, _ := omcilib.CreateGalEnetRequest(o.getNextTid(false))
788 sendOmciMsg(galEnet, o.PonPortID, o.ID, o.SerialNumber, "CreateGalEnetRequest", client)
789 } else {
790 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
791 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
792 }
793 case omci.CreateResponseType:
794 // NOTE Creating a GemPort,
795 // BBsim actually doesn't care about the values, so we can do we want with the parameters
796 // In the same way we can create a GemPort even without setting up UNIs/TConts/...
797 // but we need the GemPort to trigger the state change
798
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700799 if !o.GemPortAdded {
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700800 // NOTE this sends a CreateRequestType and BBSim replies with a CreateResponseType
801 // thus we send this request only once
802 gemReq, _ := omcilib.CreateGemPortRequest(o.getNextTid(false))
803 sendOmciMsg(gemReq, o.PonPortID, o.ID, o.SerialNumber, "CreateGemPortRequest", client)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700804 o.GemPortAdded = true
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700805 } else {
806 if err := o.InternalState.Event("send_eapol_flow"); err != nil {
807 onuLogger.WithFields(log.Fields{
808 "OnuId": o.ID,
809 "IntfId": o.PonPortID,
810 "OnuSn": o.Sn(),
811 }).Errorf("Error while transitioning ONU State %v", err)
812 }
813 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700814 }
815}
816
817func (o *Onu) sendEapolFlow(client openolt.OpenoltClient) {
818
819 classifierProto := openolt.Classifier{
820 EthType: uint32(layers.EthernetTypeEAPOL),
821 OVid: 4091,
822 }
823
824 actionProto := openolt.Action{}
825
826 downstreamFlow := openolt.Flow{
827 AccessIntfId: int32(o.PonPortID),
828 OnuId: int32(o.ID),
Matteo Scandolo813402b2019-10-23 19:24:52 -0700829 UniId: int32(0), // NOTE do not hardcode this, we need to support multiple UNIs
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700830 FlowId: uint32(o.ID),
831 FlowType: "downstream",
832 AllocId: int32(0),
833 NetworkIntfId: int32(0),
834 GemportId: int32(1), // FIXME use the same value as CreateGemPortRequest PortID, do not hardcode
835 Classifier: &classifierProto,
836 Action: &actionProto,
837 Priority: int32(100),
838 Cookie: uint64(o.ID),
839 PortNo: uint32(o.ID), // NOTE we are using this to map an incoming packetIndication to an ONU
840 }
841
842 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
843 log.WithFields(log.Fields{
844 "IntfId": o.PonPortID,
845 "OnuId": o.ID,
846 "FlowId": downstreamFlow.FlowId,
847 "PortNo": downstreamFlow.PortNo,
848 "SerialNumber": common.OnuSnToString(o.SerialNumber),
Matteo Scandolob0e3e622020-04-23 17:00:29 -0700849 }).Fatalf("Failed to add EAPOL Flow")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700850 }
851 log.WithFields(log.Fields{
852 "IntfId": o.PonPortID,
853 "OnuId": o.ID,
854 "FlowId": downstreamFlow.FlowId,
855 "PortNo": downstreamFlow.PortNo,
856 "SerialNumber": common.OnuSnToString(o.SerialNumber),
857 }).Info("Sent EAPOL Flow")
858}
859
860func (o *Onu) sendDhcpFlow(client openolt.OpenoltClient) {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700861
862 // BBR only works with a single service (ATT HSIA)
863 hsia := o.Services[0].(*Service)
864
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700865 classifierProto := openolt.Classifier{
866 EthType: uint32(layers.EthernetTypeIPv4),
867 SrcPort: uint32(68),
868 DstPort: uint32(67),
Matteo Scandolo4a036262020-08-17 15:56:13 -0700869 OVid: uint32(hsia.CTag),
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700870 }
871
872 actionProto := openolt.Action{}
873
874 downstreamFlow := openolt.Flow{
875 AccessIntfId: int32(o.PonPortID),
876 OnuId: int32(o.ID),
Matteo Scandolo813402b2019-10-23 19:24:52 -0700877 UniId: int32(0), // FIXME do not hardcode this
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700878 FlowId: uint32(o.ID),
879 FlowType: "downstream",
880 AllocId: int32(0),
881 NetworkIntfId: int32(0),
882 GemportId: int32(1), // FIXME use the same value as CreateGemPortRequest PortID, do not hardcode
883 Classifier: &classifierProto,
884 Action: &actionProto,
885 Priority: int32(100),
886 Cookie: uint64(o.ID),
887 PortNo: uint32(o.ID), // NOTE we are using this to map an incoming packetIndication to an ONU
888 }
889
890 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
891 log.WithFields(log.Fields{
892 "IntfId": o.PonPortID,
893 "OnuId": o.ID,
894 "FlowId": downstreamFlow.FlowId,
895 "PortNo": downstreamFlow.PortNo,
896 "SerialNumber": common.OnuSnToString(o.SerialNumber),
897 }).Fatalf("Failed to send DHCP Flow")
898 }
899 log.WithFields(log.Fields{
900 "IntfId": o.PonPortID,
901 "OnuId": o.ID,
902 "FlowId": downstreamFlow.FlowId,
903 "PortNo": downstreamFlow.PortNo,
904 "SerialNumber": common.OnuSnToString(o.SerialNumber),
905 }).Info("Sent DHCP Flow")
906}
Pragya Arya8bdb4532020-03-02 17:08:09 +0530907
908// DeleteFlow method search and delete flowKey from the onu flows slice
909func (onu *Onu) DeleteFlow(key FlowKey) {
910 for pos, flowKey := range onu.Flows {
911 if flowKey == key {
912 // delete the flowKey by shifting all flowKeys by one
913 onu.Flows = append(onu.Flows[:pos], onu.Flows[pos+1:]...)
914 t := make([]FlowKey, len(onu.Flows))
915 copy(t, onu.Flows)
916 onu.Flows = t
917 break
918 }
919 }
920}
Hardik Windlass7b3405b2020-07-08 15:10:05 +0530921
922func (onu *Onu) ReDiscoverOnu() {
923 // Wait for few seconds to be sure of the cleanup
924 time.Sleep(5 * time.Second)
925
926 onuLogger.WithFields(log.Fields{
927 "IntfId": onu.PonPortID,
928 "OnuId": onu.ID,
929 "OnuSn": onu.Sn(),
930 }).Debug("Send ONU Re-Discovery")
931
932 // ONU Re-Discovery
933 if err := onu.InternalState.Event("initialize"); err != nil {
934 log.WithFields(log.Fields{
935 "IntfId": onu.PonPortID,
936 "OnuSn": onu.Sn(),
937 "OnuId": onu.ID,
938 }).Infof("Failed to transition ONU to initialized state: %s", err.Error())
939 }
940
941 if err := onu.InternalState.Event("discover"); err != nil {
942 log.WithFields(log.Fields{
943 "IntfId": onu.PonPortID,
944 "OnuSn": onu.Sn(),
945 "OnuId": onu.ID,
946 }).Infof("Failed to transition ONU to discovered state: %s", err.Error())
947 }
948}
Matteo Scandolo4a036262020-08-17 15:56:13 -0700949
950func (onu *Onu) addGemPortToService(gemport uint32, ethType uint32, oVlan uint32, iVlan uint32) {
951 for _, s := range onu.Services {
952 if service, ok := s.(*Service); ok {
953 // EAPOL is a strange case, as packets are untagged
954 // but we assume we will have a single service requiring EAPOL
955 if ethType == uint32(layers.EthernetTypeEAPOL) && service.NeedsEapol {
956 service.GemPort = gemport
957 }
958
959 // For DHCP services we single tag the outgoing packets,
960 // thus the flow only contains the CTag and we can use that to match the service
961 if ethType == uint32(layers.EthernetTypeIPv4) && service.NeedsDhcp && service.CTag == int(oVlan) {
962 service.GemPort = gemport
963 }
964
965 // for dataplane services match both C and S tags
966 if service.CTag == int(iVlan) && service.STag == int(oVlan) {
967 service.GemPort = gemport
968 }
969 }
970 }
971}
972
973func (onu *Onu) findServiceByMacAddress(macAddress net.HardwareAddr) (*Service, error) {
974 for _, s := range onu.Services {
975 service := s.(*Service)
976 if service.HwAddress.String() == macAddress.String() {
977 return service, nil
978 }
979 }
980 return nil, fmt.Errorf("cannot-find-service-with-mac-address-%s", macAddress.String())
981}