blob: 237c353d52736db12bd40fbba84408a84c7d0a66 [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 Scandolo99f18462019-10-28 14:14:28 -070021 "errors"
Matteo Scandolo3bc73742019-08-20 14:04:04 -070022 "fmt"
Zdravko Bozakov681364d2019-11-10 14:28:46 +010023 "net"
24
25 "time"
26
Matteo Scandolo40e067f2019-10-16 16:59:41 -070027 "github.com/cboling/omci"
Matteo Scandolo3bc73742019-08-20 14:04:04 -070028 "github.com/google/gopacket/layers"
Matteo Scandolo4747d292019-08-05 11:50:18 -070029 "github.com/looplab/fsm"
Matteo Scandolo075b1892019-10-07 12:11:07 -070030 "github.com/opencord/bbsim/internal/bbsim/packetHandlers"
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070031 "github.com/opencord/bbsim/internal/bbsim/responders/dhcp"
32 "github.com/opencord/bbsim/internal/bbsim/responders/eapol"
Arjun E K57a7fcb2020-01-30 06:44:45 +000033 "github.com/opencord/bbsim/internal/bbsim/responders/igmp"
Matteo Scandolo40e067f2019-10-16 16:59:41 -070034 "github.com/opencord/bbsim/internal/common"
35 omcilib "github.com/opencord/bbsim/internal/common/omci"
36 omcisim "github.com/opencord/omci-sim"
Matteo Scandolo3de9de02019-11-14 13:40:03 -080037 "github.com/opencord/voltha-protos/v2/go/openolt"
Anand S Katti09541352020-01-29 15:54:01 +053038 tech_profile "github.com/opencord/voltha-protos/v2/go/tech_profile"
Matteo Scandolo4747d292019-08-05 11:50:18 -070039 log "github.com/sirupsen/logrus"
40)
41
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070042var onuLogger = log.WithFields(log.Fields{
43 "module": "ONU",
44})
45
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070046type Onu struct {
Matteo Scandoloe811ae92019-12-10 17:50:14 -080047 ID uint32
48 PonPortID uint32
49 PonPort PonPort
50 STag int
51 CTag int
52 Auth bool // automatically start EAPOL if set to true
53 Dhcp bool // automatically start DHCP if set to true
54 HwAddress net.HardwareAddr
55 InternalState *fsm.FSM
Pragya Arya2225f202020-01-29 18:05:01 +053056 DiscoveryRetryDelay time.Duration // this is the time between subsequent Discovery Indication
57 DiscoveryDelay time.Duration // this is the time to send the first Discovery Indication
Matteo Scandoloe811ae92019-12-10 17:50:14 -080058
59 // ONU State
Matteo Scandolo27428702019-10-11 16:21:16 -070060 // PortNo comes with flows and it's used when sending packetIndications,
61 // There is one PortNo per UNI Port, for now we're only storing the first one
62 // FIXME add support for multiple UNIs
Matteo Scandolo99f18462019-10-28 14:14:28 -070063 PortNo uint32
64 DhcpFlowReceived bool
65
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070066 OperState *fsm.FSM
67 SerialNumber *openolt.SerialNumber
68
69 Channel chan Message // this Channel is to track state changes OMCI messages, EAPOL and DHCP packets
Matteo Scandolo40e067f2019-10-16 16:59:41 -070070
71 // OMCI params
72 tid uint16
73 hpTid uint16
74 seqNumber uint16
75 HasGemPort bool
76
Anand S Katti09541352020-01-29 15:54:01 +053077 DoneChannel chan bool // this channel is used to signal once the onu is complete (when the struct is used by BBR)
78 TrafficSchedulers *tech_profile.TrafficSchedulers
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070079}
80
Matteo Scandolo99f18462019-10-28 14:14:28 -070081func (o *Onu) Sn() string {
Matteo Scandolo40e067f2019-10-16 16:59:41 -070082 return common.OnuSnToString(o.SerialNumber)
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070083}
84
Pragya Arya2225f202020-01-29 18:05:01 +053085func CreateONU(olt *OltDevice, pon PonPort, id uint32, sTag int, cTag int, auth bool, dhcp bool, delay time.Duration, isMock bool) *Onu {
Matteo Scandolo4747d292019-08-05 11:50:18 -070086
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070087 o := Onu{
Pragya Arya2225f202020-01-29 18:05:01 +053088 ID: 0,
Matteo Scandoloe811ae92019-12-10 17:50:14 -080089 PonPortID: pon.ID,
90 PonPort: pon,
91 STag: sTag,
92 CTag: cTag,
93 Auth: auth,
94 Dhcp: dhcp,
95 HwAddress: net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, byte(pon.ID), byte(id)},
96 PortNo: 0,
97 tid: 0x1,
98 hpTid: 0x8000,
99 seqNumber: 0,
100 DoneChannel: make(chan bool, 1),
101 DhcpFlowReceived: false,
102 DiscoveryRetryDelay: 60 * time.Second, // this is used to send OnuDiscoveryIndications until an activate call is received
Pragya Arya2225f202020-01-29 18:05:01 +0530103 DiscoveryDelay: delay,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700104 }
Pragya Arya2225f202020-01-29 18:05:01 +0530105 o.SerialNumber = o.NewSN(olt.ID, pon.ID, id)
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700106
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700107 // NOTE this state machine is used to track the operational
108 // state as requested by VOLTHA
109 o.OperState = getOperStateFSM(func(e *fsm.Event) {
110 onuLogger.WithFields(log.Fields{
111 "ID": o.ID,
112 }).Debugf("Changing ONU OperState from %s to %s", e.Src, e.Dst)
113 })
114
115 // NOTE this state machine is used to activate the OMCI, EAPOL and DHCP clients
116 o.InternalState = fsm.NewFSM(
117 "created",
118 fsm.Events{
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700119 // DEVICE Lifecycle
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100120 {Name: "initialize", Src: []string{"created", "disabled"}, Dst: "initialized"},
Pragya Arya6a708d62020-01-01 17:17:20 +0530121 {Name: "discover", Src: []string{"initialized", "pon_disabled"}, Dst: "discovered"},
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700122 {Name: "enable", Src: []string{"discovered", "disabled"}, Dst: "enabled"},
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700123 {Name: "receive_eapol_flow", Src: []string{"enabled", "gem_port_added"}, Dst: "eapol_flow_received"},
124 {Name: "add_gem_port", Src: []string{"enabled", "eapol_flow_received"}, Dst: "gem_port_added"},
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100125 // NOTE should disabled state be different for oper_disabled (emulating an error) and admin_disabled (received a disabled call via VOLTHA)?
Pragya Arya2225f202020-01-29 18:05:01 +0530126 {Name: "disable", Src: []string{"enabled", "eapol_flow_received", "gem_port_added", "eap_response_success_received", "auth_failed", "dhcp_ack_received", "dhcp_failed", "pon_disabled"}, Dst: "disabled"},
Pragya Arya6a708d62020-01-01 17:17:20 +0530127 // ONU state when PON port is disabled but ONU is power ON(more states should be added in src?)
Matteo Scandolodf3f85d2020-01-15 12:50:48 -0800128 {Name: "pon_disabled", Src: []string{"enabled", "gem_port_added", "eapol_flow_received", "eap_response_success_received", "auth_failed", "dhcp_ack_received", "dhcp_failed"}, Dst: "pon_disabled"},
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700129 // EAPOL
Shubham Sharmabd4b6572020-02-12 13:00:44 +0000130 {Name: "start_auth", Src: []string{"eapol_flow_received", "gem_port_added", "eap_start_sent", "eap_response_identity_sent", "eap_response_challenge_sent", "eap_response_success_received", "auth_failed", "dhcp_ack_received", "dhcp_failed", "igmp_join_started", "igmp_left", "igmp_join_error"}, Dst: "auth_started"},
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700131 {Name: "eap_start_sent", Src: []string{"auth_started"}, Dst: "eap_start_sent"},
132 {Name: "eap_response_identity_sent", Src: []string{"eap_start_sent"}, Dst: "eap_response_identity_sent"},
133 {Name: "eap_response_challenge_sent", Src: []string{"eap_response_identity_sent"}, Dst: "eap_response_challenge_sent"},
134 {Name: "eap_response_success_received", Src: []string{"eap_response_challenge_sent"}, Dst: "eap_response_success_received"},
135 {Name: "auth_failed", Src: []string{"auth_started", "eap_start_sent", "eap_response_identity_sent", "eap_response_challenge_sent"}, Dst: "auth_failed"},
136 // DHCP
Shubham Sharmabd4b6572020-02-12 13:00:44 +0000137 {Name: "start_dhcp", Src: []string{"eap_response_success_received", "dhcp_discovery_sent", "dhcp_request_sent", "dhcp_ack_received", "dhcp_failed", "igmp_join_started", "igmp_left", "igmp_join_error"}, Dst: "dhcp_started"},
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700138 {Name: "dhcp_discovery_sent", Src: []string{"dhcp_started"}, Dst: "dhcp_discovery_sent"},
139 {Name: "dhcp_request_sent", Src: []string{"dhcp_discovery_sent"}, Dst: "dhcp_request_sent"},
140 {Name: "dhcp_ack_received", Src: []string{"dhcp_request_sent"}, Dst: "dhcp_ack_received"},
141 {Name: "dhcp_failed", Src: []string{"dhcp_started", "dhcp_discovery_sent", "dhcp_request_sent"}, Dst: "dhcp_failed"},
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700142 // BBR States
143 // TODO add start OMCI state
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100144 {Name: "send_eapol_flow", Src: []string{"initialized"}, Dst: "eapol_flow_sent"},
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700145 {Name: "send_dhcp_flow", Src: []string{"eapol_flow_sent"}, Dst: "dhcp_flow_sent"},
Arjun E K57a7fcb2020-01-30 06:44:45 +0000146 // IGMP
Shubham Sharmabd4b6572020-02-12 13:00:44 +0000147 {Name: "igmp_join_start", Src: []string{"eap_response_success_received", "gem_port_added", "eapol_flow_received", "dhcp_ack_received", "igmp_left", "igmp_join_error"}, Dst: "igmp_join_started"},
Arjun E Kdd443f02020-02-07 15:24:01 +0000148 {Name: "igmp_join_startv3", Src: []string{"eap_response_success_received", "gem_port_added", "eapol_flow_received", "dhcp_ack_received", "igmp_left", "igmp_join_error"}, Dst: "igmp_join_started"},
Shubham Sharmabd4b6572020-02-12 13:00:44 +0000149 {Name: "igmp_join_error", Src: []string{"igmp_join_started"}, Dst: "igmp_join_error"},
150 {Name: "igmp_leave", Src: []string{"igmp_join_started", "gem_port_added", "eapol_flow_received", "eap_response_success_received", "dhcp_ack_received"}, Dst: "igmp_left"},
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700151 },
152 fsm.Callbacks{
153 "enter_state": func(e *fsm.Event) {
154 o.logStateChange(e.Src, e.Dst)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700155 },
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100156 "enter_initialized": func(e *fsm.Event) {
157 // create new channel for ProcessOnuMessages Go routine
158 o.Channel = make(chan Message, 2048)
Pragya Arya1cbefa42020-01-13 12:15:29 +0530159 if !isMock {
160 // start ProcessOnuMessages Go routine
161 go o.ProcessOnuMessages(olt.enableContext, *olt.OpenoltStream, nil)
162 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100163 },
164 "enter_discovered": func(e *fsm.Event) {
165 msg := Message{
166 Type: OnuDiscIndication,
167 Data: OnuDiscIndicationMessage{
168 Onu: &o,
169 OperState: UP,
170 },
171 }
172 o.Channel <- msg
173 },
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700174 "enter_enabled": func(event *fsm.Event) {
175 msg := Message{
176 Type: OnuIndication,
177 Data: OnuIndicationMessage{
178 OnuSN: o.SerialNumber,
179 PonPortID: o.PonPortID,
180 OperState: UP,
181 },
182 }
183 o.Channel <- msg
184 },
185 "enter_disabled": func(event *fsm.Event) {
186 msg := Message{
187 Type: OnuIndication,
188 Data: OnuIndicationMessage{
189 OnuSN: o.SerialNumber,
190 PonPortID: o.PonPortID,
191 OperState: DOWN,
192 },
193 }
194 o.Channel <- msg
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100195 // terminate the ONU's ProcessOnuMessages Go routine
196 close(o.Channel)
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700197 },
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700198 "enter_auth_started": func(e *fsm.Event) {
199 o.logStateChange(e.Src, e.Dst)
200 msg := Message{
201 Type: StartEAPOL,
202 Data: PacketMessage{
203 PonPortID: o.PonPortID,
204 OnuID: o.ID,
205 },
206 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700207 o.Channel <- msg
Matteo Scandolo4747d292019-08-05 11:50:18 -0700208 },
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700209 "enter_auth_failed": func(e *fsm.Event) {
210 onuLogger.WithFields(log.Fields{
211 "OnuId": o.ID,
212 "IntfId": o.PonPortID,
213 "OnuSn": o.Sn(),
214 }).Errorf("ONU failed to authenticate!")
215 },
Matteo Scandolo99f18462019-10-28 14:14:28 -0700216 "before_start_dhcp": func(e *fsm.Event) {
217 if o.DhcpFlowReceived == false {
218 e.Cancel(errors.New("cannot-go-to-dhcp-started-as-dhcp-flow-is-missing"))
219 }
220 },
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700221 "enter_dhcp_started": func(e *fsm.Event) {
222 msg := Message{
223 Type: StartDHCP,
224 Data: PacketMessage{
225 PonPortID: o.PonPortID,
226 OnuID: o.ID,
227 },
228 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700229 o.Channel <- msg
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700230 },
231 "enter_dhcp_failed": func(e *fsm.Event) {
232 onuLogger.WithFields(log.Fields{
233 "OnuId": o.ID,
234 "IntfId": o.PonPortID,
235 "OnuSn": o.Sn(),
236 }).Errorf("ONU failed to DHCP!")
237 },
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700238 "enter_eapol_flow_sent": func(e *fsm.Event) {
239 msg := Message{
240 Type: SendEapolFlow,
241 }
242 o.Channel <- msg
243 },
244 "enter_dhcp_flow_sent": func(e *fsm.Event) {
245 msg := Message{
246 Type: SendDhcpFlow,
247 }
248 o.Channel <- msg
249 },
Arjun E K57a7fcb2020-01-30 06:44:45 +0000250 "igmp_join_start": func(e *fsm.Event) {
251 msg := Message{
252 Type: IGMPMembershipReportV2,
253 }
254 o.Channel <- msg
255 },
256 "igmp_leave": func(e *fsm.Event) {
257 msg := Message{
258 Type: IGMPLeaveGroup}
259 o.Channel <- msg
260 },
Anand S Katti09541352020-01-29 15:54:01 +0530261 "igmp_join_startv3": func(e *fsm.Event) {
262 msg := Message{
263 Type: IGMPMembershipReportV3,
264 }
265 o.Channel <- msg
266 },
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700267 },
268 )
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100269
Matteo Scandolo27428702019-10-11 16:21:16 -0700270 return &o
Matteo Scandolo4747d292019-08-05 11:50:18 -0700271}
272
William Kurkian0418bc82019-11-06 12:16:24 -0500273func (o *Onu) logStateChange(src string, dst string) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700274 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700275 "OnuId": o.ID,
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700276 "IntfId": o.PonPortID,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700277 "OnuSn": o.Sn(),
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700278 }).Debugf("Changing ONU InternalState from %s to %s", src, dst)
279}
280
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100281// ProcessOnuMessages starts indication channel for each ONU
David Bainbridge103cf022019-12-16 20:11:35 +0000282func (o *Onu) ProcessOnuMessages(ctx context.Context, stream openolt.Openolt_EnableIndicationServer, client openolt.OpenoltClient) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700283 onuLogger.WithFields(log.Fields{
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100284 "onuID": o.ID,
285 "onuSN": o.Sn(),
286 "ponPort": o.PonPortID,
287 }).Debug("Starting ONU Indication Channel")
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700288
David Bainbridge103cf022019-12-16 20:11:35 +0000289loop:
290 for {
291 select {
292 case <-ctx.Done():
293 onuLogger.WithFields(log.Fields{
294 "onuID": o.ID,
295 "onuSN": o.Sn(),
296 }).Tracef("ONU message handling canceled via context")
297 break loop
298 case message, ok := <-o.Channel:
299 if !ok || ctx.Err() != nil {
300 onuLogger.WithFields(log.Fields{
301 "onuID": o.ID,
302 "onuSN": o.Sn(),
303 }).Tracef("ONU message handling canceled via channel close")
304 break loop
Matteo Scandolo075b1892019-10-07 12:11:07 -0700305 }
David Bainbridge103cf022019-12-16 20:11:35 +0000306 onuLogger.WithFields(log.Fields{
307 "onuID": o.ID,
308 "onuSN": o.Sn(),
309 "messageType": message.Type,
310 }).Tracef("Received message on ONU Channel")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700311
David Bainbridge103cf022019-12-16 20:11:35 +0000312 switch message.Type {
313 case OnuDiscIndication:
314 msg, _ := message.Data.(OnuDiscIndicationMessage)
315 // 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 +0530316 time.Sleep(o.DiscoveryDelay)
David Bainbridge103cf022019-12-16 20:11:35 +0000317 o.sendOnuDiscIndication(msg, stream)
318 case OnuIndication:
319 msg, _ := message.Data.(OnuIndicationMessage)
320 o.sendOnuIndication(msg, stream)
321 case OMCI:
322 msg, _ := message.Data.(OmciMessage)
323 o.handleOmciMessage(msg, stream)
324 case FlowUpdate:
325 msg, _ := message.Data.(OnuFlowUpdateMessage)
326 o.handleFlowUpdate(msg)
327 case StartEAPOL:
328 log.Infof("Receive StartEAPOL message on ONU Channel")
329 eapol.SendEapStart(o.ID, o.PonPortID, o.Sn(), o.PortNo, o.HwAddress, o.InternalState, stream)
330 case StartDHCP:
331 log.Infof("Receive StartDHCP message on ONU Channel")
332 // FIXME use id, ponId as SendEapStart
333 dhcp.SendDHCPDiscovery(o.PonPortID, o.ID, o.Sn(), o.PortNo, o.InternalState, o.HwAddress, o.CTag, stream)
334 case OnuPacketOut:
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700335
David Bainbridge103cf022019-12-16 20:11:35 +0000336 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 OnuPacketOut Message")
343
344 if msg.Type == packetHandlers.EAPOL {
345 eapol.HandleNextPacket(msg.OnuId, msg.IntfId, o.Sn(), o.PortNo, o.InternalState, msg.Packet, stream, client)
346 } else if msg.Type == packetHandlers.DHCP {
347 // NOTE here we receive packets going from the DHCP Server to the ONU
348 // for now we expect them to be double-tagged, but ideally the should be single tagged
349 dhcp.HandleNextPacket(o.ID, o.PonPortID, o.Sn(), o.PortNo, o.HwAddress, o.CTag, o.InternalState, msg.Packet, stream)
350 }
351 case OnuPacketIn:
352 // NOTE we only receive BBR packets here.
353 // Eapol.HandleNextPacket can handle both BBSim and BBr cases so the call is the same
354 // in the DHCP case VOLTHA only act as a proxy, the behaviour is completely different thus we have a dhcp.HandleNextBbrPacket
355 msg, _ := message.Data.(OnuPacketMessage)
356
357 log.WithFields(log.Fields{
358 "IntfId": msg.IntfId,
359 "OnuId": msg.OnuId,
360 "pktType": msg.Type,
361 }).Trace("Received OnuPacketIn Message")
362
363 if msg.Type == packetHandlers.EAPOL {
364 eapol.HandleNextPacket(msg.OnuId, msg.IntfId, o.Sn(), o.PortNo, o.InternalState, msg.Packet, stream, client)
365 } else if msg.Type == packetHandlers.DHCP {
366 dhcp.HandleNextBbrPacket(o.ID, o.PonPortID, o.Sn(), o.STag, o.HwAddress, o.DoneChannel, msg.Packet, client)
367 }
368 case DyingGaspIndication:
369 msg, _ := message.Data.(DyingGaspIndicationMessage)
370 o.sendDyingGaspInd(msg, stream)
371 case OmciIndication:
372 msg, _ := message.Data.(OmciIndicationMessage)
373 o.handleOmci(msg, client)
374 case SendEapolFlow:
375 o.sendEapolFlow(client)
376 case SendDhcpFlow:
377 o.sendDhcpFlow(client)
Arjun E K57a7fcb2020-01-30 06:44:45 +0000378 case IGMPMembershipReportV2:
379 log.Infof("Recieved IGMPMembershipReportV2 message on ONU channel")
380 igmp.SendIGMPMembershipReportV2(o.PonPortID, o.ID, o.Sn(), o.PortNo, o.HwAddress, stream)
381 case IGMPLeaveGroup:
382 log.Infof("Recieved IGMPLeaveGroupV2 message on ONU channel")
383 igmp.SendIGMPLeaveGroupV2(o.PonPortID, o.ID, o.Sn(), o.PortNo, o.HwAddress, stream)
Anand S Katti09541352020-01-29 15:54:01 +0530384 case IGMPMembershipReportV3:
385 log.Infof("Recieved IGMPMembershipReportV3 message on ONU channel")
386 igmp.SendIGMPMembershipReportV3(o.PonPortID, o.ID, o.Sn(), o.PortNo, o.HwAddress, stream)
David Bainbridge103cf022019-12-16 20:11:35 +0000387 default:
388 onuLogger.Warnf("Received unknown message data %v for type %v in OLT Channel", message.Data, message.Type)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700389 }
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700390 }
391 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100392 onuLogger.WithFields(log.Fields{
393 "onuID": o.ID,
394 "onuSN": o.Sn(),
395 }).Debug("Stopped handling ONU Indication Channel")
Matteo Scandolo4747d292019-08-05 11:50:18 -0700396}
397
Matteo Scandolodf3f85d2020-01-15 12:50:48 -0800398func (o *Onu) processOmciMessage(message omcisim.OmciChMessage, stream openolt.Openolt_EnableIndicationServer) {
William Kurkian9dadc5b2019-10-22 13:51:57 -0400399 switch message.Type {
Matteo Scandolodf3f85d2020-01-15 12:50:48 -0800400 case omcisim.UniLinkUp, omcisim.UniLinkDown:
401 onuLogger.WithFields(log.Fields{
402 "OnuId": message.Data.OnuId,
403 "IntfId": message.Data.IntfId,
404 "Type": message.Type,
405 }).Infof("UNI Link Alarm")
406 // TODO send to OLT
407
408 omciInd := openolt.OmciIndication{
409 IntfId: message.Data.IntfId,
410 OnuId: message.Data.OnuId,
411 Pkt: message.Packet,
412 }
413
414 omci := &openolt.Indication_OmciInd{OmciInd: &omciInd}
415 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
416 onuLogger.WithFields(log.Fields{
417 "IntfId": o.PonPortID,
418 "SerialNumber": o.Sn(),
419 "Type": message.Type,
420 "omciPacket": omciInd.Pkt,
421 }).Errorf("Failed to send UNI Link Alarm: %v", err)
422 return
423 }
424
425 onuLogger.WithFields(log.Fields{
426 "IntfId": o.PonPortID,
427 "SerialNumber": o.Sn(),
428 "Type": message.Type,
429 "omciPacket": omciInd.Pkt,
430 }).Info("UNI Link alarm sent")
431
William Kurkian9dadc5b2019-10-22 13:51:57 -0400432 case omcisim.GemPortAdded:
433 log.WithFields(log.Fields{
434 "OnuId": message.Data.OnuId,
435 "IntfId": message.Data.IntfId,
436 }).Infof("GemPort Added")
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700437
William Kurkian9dadc5b2019-10-22 13:51:57 -0400438 // NOTE if we receive the GemPort but we don't have EAPOL flows
439 // go an intermediate state, otherwise start auth
440 if o.InternalState.Is("enabled") {
441 if err := o.InternalState.Event("add_gem_port"); err != nil {
442 log.Errorf("Can't go to gem_port_added: %v", err)
443 }
444 } else if o.InternalState.Is("eapol_flow_received") {
Matteo Scandolo3c656a12019-12-10 09:54:51 -0800445 if o.Auth == true {
446 if err := o.InternalState.Event("start_auth"); err != nil {
447 log.Warnf("Can't go to auth_started: %v", err)
448 }
449 } else {
450 onuLogger.WithFields(log.Fields{
451 "IntfId": o.PonPortID,
452 "OnuId": o.ID,
453 "SerialNumber": o.Sn(),
454 }).Warn("Not starting authentication as Auth bit is not set in CLI parameters")
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700455 }
456 }
457 }
458}
459
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100460func (o Onu) NewSN(oltid int, intfid uint32, onuid uint32) *openolt.SerialNumber {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700461
462 sn := new(openolt.SerialNumber)
463
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700464 //sn = new(openolt.SerialNumber)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700465 sn.VendorId = []byte("BBSM")
466 sn.VendorSpecific = []byte{0, byte(oltid % 256), byte(intfid), byte(onuid)}
467
468 return sn
469}
470
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700471// NOTE handle_/process methods can change the ONU internal state as they are receiving messages
472// send method should not change the ONU state
473
William Kurkian0418bc82019-11-06 12:16:24 -0500474func (o *Onu) sendDyingGaspInd(msg DyingGaspIndicationMessage, stream openolt.Openolt_EnableIndicationServer) error {
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700475 alarmData := &openolt.AlarmIndication_DyingGaspInd{
476 DyingGaspInd: &openolt.DyingGaspIndication{
477 IntfId: msg.PonPortID,
478 OnuId: msg.OnuID,
479 Status: "on",
480 },
481 }
482 data := &openolt.Indication_AlarmInd{AlarmInd: &openolt.AlarmIndication{Data: alarmData}}
483
484 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
485 onuLogger.Errorf("Failed to send DyingGaspInd : %v", err)
486 return err
487 }
488 onuLogger.WithFields(log.Fields{
489 "IntfId": msg.PonPortID,
490 "OnuSn": o.Sn(),
491 "OnuId": msg.OnuID,
492 }).Info("sendDyingGaspInd")
493 return nil
494}
495
William Kurkian0418bc82019-11-06 12:16:24 -0500496func (o *Onu) sendOnuDiscIndication(msg OnuDiscIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700497 discoverData := &openolt.Indication_OnuDiscInd{OnuDiscInd: &openolt.OnuDiscIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700498 IntfId: msg.Onu.PonPortID,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700499 SerialNumber: msg.Onu.SerialNumber,
500 }}
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700501
Matteo Scandolo4747d292019-08-05 11:50:18 -0700502 if err := stream.Send(&openolt.Indication{Data: discoverData}); err != nil {
Matteo Scandolo11006992019-08-28 11:29:46 -0700503 log.Errorf("Failed to send Indication_OnuDiscInd: %v", err)
Matteo Scandolo99f18462019-10-28 14:14:28 -0700504 return
Matteo Scandolo4747d292019-08-05 11:50:18 -0700505 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700506
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700507 onuLogger.WithFields(log.Fields{
Matteo Scandolo4747d292019-08-05 11:50:18 -0700508 "IntfId": msg.Onu.PonPortID,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700509 "OnuSn": msg.Onu.Sn(),
510 "OnuId": o.ID,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700511 }).Debug("Sent Indication_OnuDiscInd")
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800512
513 // after DiscoveryRetryDelay check if the state is the same and in case send a new OnuDiscIndication
514 go func(delay time.Duration) {
Matteo Scandolo569e7172019-12-20 11:51:51 -0800515 time.Sleep(delay)
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800516 if o.InternalState.Current() == "discovered" {
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800517 o.sendOnuDiscIndication(msg, stream)
518 }
519 }(o.DiscoveryRetryDelay)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700520}
521
William Kurkian0418bc82019-11-06 12:16:24 -0500522func (o *Onu) sendOnuIndication(msg OnuIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700523 // NOTE voltha returns an ID, but if we use that ID then it complains:
524 // expected_onu_id: 1, received_onu_id: 1024, event: ONU-id-mismatch, can happen if both voltha and the olt rebooted
525 // so we're using the internal ID that is 1
526 // o.ID = msg.OnuID
Matteo Scandolo4747d292019-08-05 11:50:18 -0700527
528 indData := &openolt.Indication_OnuInd{OnuInd: &openolt.OnuIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700529 IntfId: o.PonPortID,
530 OnuId: o.ID,
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700531 OperState: msg.OperState.String(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700532 AdminState: o.OperState.Current(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700533 SerialNumber: o.SerialNumber,
534 }}
535 if err := stream.Send(&openolt.Indication{Data: indData}); err != nil {
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700536 // TODO do we need to transition to a broken state?
Matteo Scandolo11006992019-08-28 11:29:46 -0700537 log.Errorf("Failed to send Indication_OnuInd: %v", err)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700538 }
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700539 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700540 "IntfId": o.PonPortID,
541 "OnuId": o.ID,
542 "OperState": msg.OperState.String(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700543 "AdminState": msg.OperState.String(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700544 "OnuSn": o.Sn(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700545 }).Debug("Sent Indication_OnuInd")
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700546
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700547}
548
William Kurkian0418bc82019-11-06 12:16:24 -0500549func (o *Onu) handleOmciMessage(msg OmciMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700550
551 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700552 "IntfId": o.PonPortID,
Matteo Scandolo27428702019-10-11 16:21:16 -0700553 "SerialNumber": o.Sn(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700554 "omciPacket": msg.omciMsg.Pkt,
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700555 }).Tracef("Received OMCI message")
556
557 var omciInd openolt.OmciIndication
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700558 respPkt, err := omcisim.OmciSim(o.PonPortID, o.ID, HexDecode(msg.omciMsg.Pkt))
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700559 if err != nil {
Matteo Scandolo27428702019-10-11 16:21:16 -0700560 onuLogger.WithFields(log.Fields{
561 "IntfId": o.PonPortID,
562 "SerialNumber": o.Sn(),
563 "omciPacket": omciInd.Pkt,
564 "msg": msg,
565 }).Errorf("Error handling OMCI message %v", msg)
566 return
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700567 }
568
569 omciInd.IntfId = o.PonPortID
570 omciInd.OnuId = o.ID
571 omciInd.Pkt = respPkt
572
573 omci := &openolt.Indication_OmciInd{OmciInd: &omciInd}
574 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
Matteo Scandolo27428702019-10-11 16:21:16 -0700575 onuLogger.WithFields(log.Fields{
576 "IntfId": o.PonPortID,
577 "SerialNumber": o.Sn(),
578 "omciPacket": omciInd.Pkt,
579 "msg": msg,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700580 }).Errorf("send omcisim indication failed: %v", err)
Matteo Scandolo27428702019-10-11 16:21:16 -0700581 return
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700582 }
583 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700584 "IntfId": o.PonPortID,
Matteo Scandolo27428702019-10-11 16:21:16 -0700585 "SerialNumber": o.Sn(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700586 "omciPacket": omciInd.Pkt,
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700587 }).Tracef("Sent OMCI message")
588}
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 Scandolo813402b2019-10-23 19:24:52 -0700618func (o *Onu) handleFlowUpdate(msg OnuFlowUpdateMessage) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700619 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700620 "DstPort": msg.Flow.Classifier.DstPort,
621 "EthType": fmt.Sprintf("%x", msg.Flow.Classifier.EthType),
622 "FlowId": msg.Flow.FlowId,
623 "FlowType": msg.Flow.FlowType,
Matteo Scandolo3ac52792020-03-05 14:21:08 -0800624 "GemportId": msg.Flow.GemportId,
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700625 "InnerVlan": msg.Flow.Classifier.IVid,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700626 "IntfId": msg.Flow.AccessIntfId,
627 "IpProto": msg.Flow.Classifier.IpProto,
628 "OnuId": msg.Flow.OnuId,
629 "OnuSn": o.Sn(),
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700630 "OuterVlan": msg.Flow.Classifier.OVid,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700631 "PortNo": msg.Flow.PortNo,
632 "SrcPort": msg.Flow.Classifier.SrcPort,
633 "UniID": msg.Flow.UniId,
Matteo Scandolo3ac52792020-03-05 14:21:08 -0800634 "ClassifierOPbits": msg.Flow.Classifier.OPbits,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700635 }).Debug("ONU receives Flow")
636
Matteo Scandolo813402b2019-10-23 19:24:52 -0700637 if msg.Flow.UniId != 0 {
638 // as of now BBSim only support a single UNI, so ignore everything that is not targeted to it
639 onuLogger.WithFields(log.Fields{
640 "IntfId": o.PonPortID,
641 "OnuId": o.ID,
642 "SerialNumber": o.Sn(),
643 }).Debug("Ignoring flow as it's not for the first UNI")
644 return
645 }
646
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700647 if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeEAPOL) && msg.Flow.Classifier.OVid == 4091 {
Matteo Scandolo27428702019-10-11 16:21:16 -0700648 // NOTE storing the PortNO, it's needed when sending PacketIndications
Matteo Scandolo813402b2019-10-23 19:24:52 -0700649 o.storePortNumber(uint32(msg.Flow.PortNo))
Matteo Scandolo27428702019-10-11 16:21:16 -0700650
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700651 // NOTE if we receive the EAPOL flows but we don't have GemPorts
652 // go an intermediate state, otherwise start auth
653 if o.InternalState.Is("enabled") {
654 if err := o.InternalState.Event("receive_eapol_flow"); err != nil {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700655 log.Warnf("Can't go to eapol_flow_received: %v", err)
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700656 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700657 } else if o.InternalState.Is("gem_port_added") {
Matteo Scandoloc1147092019-10-29 09:38:33 -0700658
659 if o.Auth == true {
660 if err := o.InternalState.Event("start_auth"); err != nil {
661 log.Warnf("Can't go to auth_started: %v", err)
662 }
663 } else {
664 onuLogger.WithFields(log.Fields{
665 "IntfId": o.PonPortID,
666 "OnuId": o.ID,
667 "SerialNumber": o.Sn(),
668 }).Warn("Not starting authentication as Auth bit is not set in CLI parameters")
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700669 }
Matteo Scandoloc1147092019-10-29 09:38:33 -0700670
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700671 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700672 } else if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeIPv4) &&
673 msg.Flow.Classifier.SrcPort == uint32(68) &&
Matteo Scandolo3ac52792020-03-05 14:21:08 -0800674 msg.Flow.Classifier.DstPort == uint32(67) &&
675 msg.Flow.Classifier.OPbits == 0 {
Matteo Scandolo99f18462019-10-28 14:14:28 -0700676
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100677 // keep track that we received the DHCP Flows so that we can transition the state to dhcp_started
Matteo Scandolo99f18462019-10-28 14:14:28 -0700678 o.DhcpFlowReceived = true
Matteo Scandoloc1147092019-10-29 09:38:33 -0700679
680 if o.Dhcp == true {
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100681 // NOTE we are receiving multiple DHCP flows but we shouldn't call the transition multiple times
Matteo Scandoloc1147092019-10-29 09:38:33 -0700682 if err := o.InternalState.Event("start_dhcp"); err != nil {
683 log.Errorf("Can't go to dhcp_started: %v", err)
684 }
685 } else {
686 onuLogger.WithFields(log.Fields{
687 "IntfId": o.PonPortID,
688 "OnuId": o.ID,
689 "SerialNumber": o.Sn(),
690 }).Warn("Not starting DHCP as Dhcp bit is not set in CLI parameters")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700691 }
Matteo Scandolo3bc73742019-08-20 14:04:04 -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
799 if !o.HasGemPort {
800 // 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)
804 o.HasGemPort = true
805 } 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 }
814
815 }
816}
817
818func (o *Onu) sendEapolFlow(client openolt.OpenoltClient) {
819
820 classifierProto := openolt.Classifier{
821 EthType: uint32(layers.EthernetTypeEAPOL),
822 OVid: 4091,
823 }
824
825 actionProto := openolt.Action{}
826
827 downstreamFlow := openolt.Flow{
828 AccessIntfId: int32(o.PonPortID),
829 OnuId: int32(o.ID),
Matteo Scandolo813402b2019-10-23 19:24:52 -0700830 UniId: int32(0), // NOTE do not hardcode this, we need to support multiple UNIs
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700831 FlowId: uint32(o.ID),
832 FlowType: "downstream",
833 AllocId: int32(0),
834 NetworkIntfId: int32(0),
835 GemportId: int32(1), // FIXME use the same value as CreateGemPortRequest PortID, do not hardcode
836 Classifier: &classifierProto,
837 Action: &actionProto,
838 Priority: int32(100),
839 Cookie: uint64(o.ID),
840 PortNo: uint32(o.ID), // NOTE we are using this to map an incoming packetIndication to an ONU
841 }
842
843 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
844 log.WithFields(log.Fields{
845 "IntfId": o.PonPortID,
846 "OnuId": o.ID,
847 "FlowId": downstreamFlow.FlowId,
848 "PortNo": downstreamFlow.PortNo,
849 "SerialNumber": common.OnuSnToString(o.SerialNumber),
850 }).Fatalf("Failed to EAPOL Flow")
851 }
852 log.WithFields(log.Fields{
853 "IntfId": o.PonPortID,
854 "OnuId": o.ID,
855 "FlowId": downstreamFlow.FlowId,
856 "PortNo": downstreamFlow.PortNo,
857 "SerialNumber": common.OnuSnToString(o.SerialNumber),
858 }).Info("Sent EAPOL Flow")
859}
860
861func (o *Onu) sendDhcpFlow(client openolt.OpenoltClient) {
862 classifierProto := openolt.Classifier{
863 EthType: uint32(layers.EthernetTypeIPv4),
864 SrcPort: uint32(68),
865 DstPort: uint32(67),
866 }
867
868 actionProto := openolt.Action{}
869
870 downstreamFlow := openolt.Flow{
871 AccessIntfId: int32(o.PonPortID),
872 OnuId: int32(o.ID),
Matteo Scandolo813402b2019-10-23 19:24:52 -0700873 UniId: int32(0), // FIXME do not hardcode this
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700874 FlowId: uint32(o.ID),
875 FlowType: "downstream",
876 AllocId: int32(0),
877 NetworkIntfId: int32(0),
878 GemportId: int32(1), // FIXME use the same value as CreateGemPortRequest PortID, do not hardcode
879 Classifier: &classifierProto,
880 Action: &actionProto,
881 Priority: int32(100),
882 Cookie: uint64(o.ID),
883 PortNo: uint32(o.ID), // NOTE we are using this to map an incoming packetIndication to an ONU
884 }
885
886 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
887 log.WithFields(log.Fields{
888 "IntfId": o.PonPortID,
889 "OnuId": o.ID,
890 "FlowId": downstreamFlow.FlowId,
891 "PortNo": downstreamFlow.PortNo,
892 "SerialNumber": common.OnuSnToString(o.SerialNumber),
893 }).Fatalf("Failed to send DHCP Flow")
894 }
895 log.WithFields(log.Fields{
896 "IntfId": o.PonPortID,
897 "OnuId": o.ID,
898 "FlowId": downstreamFlow.FlowId,
899 "PortNo": downstreamFlow.PortNo,
900 "SerialNumber": common.OnuSnToString(o.SerialNumber),
901 }).Info("Sent DHCP Flow")
902}