blob: ee16cb882763886d21b928d4f7eef76119dced11 [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
Pragya Arya8bdb4532020-03-02 17:08:09 +053046type FlowKey struct {
47 ID uint32
48 Direction string
49}
50
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070051type Onu struct {
Matteo Scandoloe811ae92019-12-10 17:50:14 -080052 ID uint32
53 PonPortID uint32
54 PonPort PonPort
55 STag int
56 CTag int
57 Auth bool // automatically start EAPOL if set to true
58 Dhcp bool // automatically start DHCP if set to true
59 HwAddress net.HardwareAddr
60 InternalState *fsm.FSM
Pragya Arya2225f202020-01-29 18:05:01 +053061 DiscoveryRetryDelay time.Duration // this is the time between subsequent Discovery Indication
62 DiscoveryDelay time.Duration // this is the time to send the first Discovery Indication
Matteo Scandoloe811ae92019-12-10 17:50:14 -080063
64 // ONU State
Matteo Scandolo27428702019-10-11 16:21:16 -070065 // PortNo comes with flows and it's used when sending packetIndications,
66 // There is one PortNo per UNI Port, for now we're only storing the first one
67 // FIXME add support for multiple UNIs
Matteo Scandolo99f18462019-10-28 14:14:28 -070068 PortNo uint32
69 DhcpFlowReceived bool
Pragya Arya8bdb4532020-03-02 17:08:09 +053070 Flows []FlowKey
Matteo Scandolo99f18462019-10-28 14:14:28 -070071
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070072 OperState *fsm.FSM
73 SerialNumber *openolt.SerialNumber
74
75 Channel chan Message // this Channel is to track state changes OMCI messages, EAPOL and DHCP packets
Matteo Scandolo40e067f2019-10-16 16:59:41 -070076
77 // OMCI params
78 tid uint16
79 hpTid uint16
80 seqNumber uint16
81 HasGemPort bool
82
Anand S Katti09541352020-01-29 15:54:01 +053083 DoneChannel chan bool // this channel is used to signal once the onu is complete (when the struct is used by BBR)
84 TrafficSchedulers *tech_profile.TrafficSchedulers
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070085}
86
Matteo Scandolo99f18462019-10-28 14:14:28 -070087func (o *Onu) Sn() string {
Matteo Scandolo40e067f2019-10-16 16:59:41 -070088 return common.OnuSnToString(o.SerialNumber)
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070089}
90
Pragya Arya2225f202020-01-29 18:05:01 +053091func 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 -070092
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070093 o := Onu{
Pragya Arya2225f202020-01-29 18:05:01 +053094 ID: 0,
Matteo Scandoloe811ae92019-12-10 17:50:14 -080095 PonPortID: pon.ID,
96 PonPort: pon,
97 STag: sTag,
98 CTag: cTag,
99 Auth: auth,
100 Dhcp: dhcp,
101 HwAddress: net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, byte(pon.ID), byte(id)},
102 PortNo: 0,
103 tid: 0x1,
104 hpTid: 0x8000,
105 seqNumber: 0,
106 DoneChannel: make(chan bool, 1),
107 DhcpFlowReceived: false,
108 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,
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 Scandolo9a3518c2019-08-13 14:36:01 -0700113
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
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100127 {Name: "initialize", Src: []string{"created", "disabled"}, Dst: "initialized"},
Pragya Arya6a708d62020-01-01 17:17:20 +0530128 {Name: "discover", Src: []string{"initialized", "pon_disabled"}, Dst: "discovered"},
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700129 {Name: "enable", Src: []string{"discovered", "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)?
Pragya Arya2225f202020-01-29 18:05:01 +0530133 {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 +0530134 // 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 -0800135 {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 -0700136 // EAPOL
Shubham Sharmabd4b6572020-02-12 13:00:44 +0000137 {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 -0700138 {Name: "eap_start_sent", Src: []string{"auth_started"}, Dst: "eap_start_sent"},
139 {Name: "eap_response_identity_sent", Src: []string{"eap_start_sent"}, Dst: "eap_response_identity_sent"},
140 {Name: "eap_response_challenge_sent", Src: []string{"eap_response_identity_sent"}, Dst: "eap_response_challenge_sent"},
141 {Name: "eap_response_success_received", Src: []string{"eap_response_challenge_sent"}, Dst: "eap_response_success_received"},
142 {Name: "auth_failed", Src: []string{"auth_started", "eap_start_sent", "eap_response_identity_sent", "eap_response_challenge_sent"}, Dst: "auth_failed"},
143 // DHCP
Shubham Sharmabd4b6572020-02-12 13:00:44 +0000144 {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 -0700145 {Name: "dhcp_discovery_sent", Src: []string{"dhcp_started"}, Dst: "dhcp_discovery_sent"},
146 {Name: "dhcp_request_sent", Src: []string{"dhcp_discovery_sent"}, Dst: "dhcp_request_sent"},
147 {Name: "dhcp_ack_received", Src: []string{"dhcp_request_sent"}, Dst: "dhcp_ack_received"},
148 {Name: "dhcp_failed", Src: []string{"dhcp_started", "dhcp_discovery_sent", "dhcp_request_sent"}, Dst: "dhcp_failed"},
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700149 // BBR States
150 // TODO add start OMCI state
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100151 {Name: "send_eapol_flow", Src: []string{"initialized"}, Dst: "eapol_flow_sent"},
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700152 {Name: "send_dhcp_flow", Src: []string{"eapol_flow_sent"}, Dst: "dhcp_flow_sent"},
Arjun E K57a7fcb2020-01-30 06:44:45 +0000153 // IGMP
Shubham Sharmabd4b6572020-02-12 13:00:44 +0000154 {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 +0000155 {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 +0000156 {Name: "igmp_join_error", Src: []string{"igmp_join_started"}, Dst: "igmp_join_error"},
157 {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 -0700158 },
159 fsm.Callbacks{
160 "enter_state": func(e *fsm.Event) {
161 o.logStateChange(e.Src, e.Dst)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700162 },
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100163 "enter_initialized": func(e *fsm.Event) {
164 // create new channel for ProcessOnuMessages Go routine
165 o.Channel = make(chan Message, 2048)
Pragya Arya1cbefa42020-01-13 12:15:29 +0530166 if !isMock {
167 // start ProcessOnuMessages Go routine
168 go o.ProcessOnuMessages(olt.enableContext, *olt.OpenoltStream, nil)
169 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100170 },
171 "enter_discovered": func(e *fsm.Event) {
172 msg := Message{
173 Type: OnuDiscIndication,
174 Data: OnuDiscIndicationMessage{
175 Onu: &o,
176 OperState: UP,
177 },
178 }
179 o.Channel <- msg
180 },
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700181 "enter_enabled": func(event *fsm.Event) {
182 msg := Message{
183 Type: OnuIndication,
184 Data: OnuIndicationMessage{
185 OnuSN: o.SerialNumber,
186 PonPortID: o.PonPortID,
187 OperState: UP,
188 },
189 }
190 o.Channel <- msg
191 },
192 "enter_disabled": func(event *fsm.Event) {
193 msg := Message{
194 Type: OnuIndication,
195 Data: OnuIndicationMessage{
196 OnuSN: o.SerialNumber,
197 PonPortID: o.PonPortID,
198 OperState: DOWN,
199 },
200 }
201 o.Channel <- msg
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100202 // terminate the ONU's ProcessOnuMessages Go routine
203 close(o.Channel)
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700204 },
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700205 "enter_auth_started": func(e *fsm.Event) {
206 o.logStateChange(e.Src, e.Dst)
207 msg := Message{
208 Type: StartEAPOL,
209 Data: PacketMessage{
210 PonPortID: o.PonPortID,
211 OnuID: o.ID,
212 },
213 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700214 o.Channel <- msg
Matteo Scandolo4747d292019-08-05 11:50:18 -0700215 },
Pragya Arya324337e2020-02-20 14:35:08 +0530216 "enter_eap_response_success_received": func(e *fsm.Event) {
217 publishEvent("ONU-authentication-done", int32(o.PonPortID), int32(o.ID), o.Sn())
218 },
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700219 "enter_auth_failed": func(e *fsm.Event) {
220 onuLogger.WithFields(log.Fields{
221 "OnuId": o.ID,
222 "IntfId": o.PonPortID,
223 "OnuSn": o.Sn(),
224 }).Errorf("ONU failed to authenticate!")
225 },
Matteo Scandolo99f18462019-10-28 14:14:28 -0700226 "before_start_dhcp": func(e *fsm.Event) {
227 if o.DhcpFlowReceived == false {
228 e.Cancel(errors.New("cannot-go-to-dhcp-started-as-dhcp-flow-is-missing"))
229 }
230 },
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700231 "enter_dhcp_started": func(e *fsm.Event) {
232 msg := Message{
233 Type: StartDHCP,
234 Data: PacketMessage{
235 PonPortID: o.PonPortID,
236 OnuID: o.ID,
237 },
238 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700239 o.Channel <- msg
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700240 },
Pragya Arya324337e2020-02-20 14:35:08 +0530241 "enter_dhcp_ack_received": func(e *fsm.Event) {
242 publishEvent("ONU-DHCP-ACK-received", int32(o.PonPortID), int32(o.ID), o.Sn())
243 },
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700244 "enter_dhcp_failed": func(e *fsm.Event) {
245 onuLogger.WithFields(log.Fields{
246 "OnuId": o.ID,
247 "IntfId": o.PonPortID,
248 "OnuSn": o.Sn(),
249 }).Errorf("ONU failed to DHCP!")
250 },
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700251 "enter_eapol_flow_sent": func(e *fsm.Event) {
252 msg := Message{
253 Type: SendEapolFlow,
254 }
255 o.Channel <- msg
256 },
257 "enter_dhcp_flow_sent": func(e *fsm.Event) {
258 msg := Message{
259 Type: SendDhcpFlow,
260 }
261 o.Channel <- msg
262 },
Arjun E K57a7fcb2020-01-30 06:44:45 +0000263 "igmp_join_start": func(e *fsm.Event) {
264 msg := Message{
265 Type: IGMPMembershipReportV2,
266 }
267 o.Channel <- msg
268 },
269 "igmp_leave": func(e *fsm.Event) {
270 msg := Message{
271 Type: IGMPLeaveGroup}
272 o.Channel <- msg
273 },
Anand S Katti09541352020-01-29 15:54:01 +0530274 "igmp_join_startv3": func(e *fsm.Event) {
275 msg := Message{
276 Type: IGMPMembershipReportV3,
277 }
278 o.Channel <- msg
279 },
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700280 },
281 )
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100282
Matteo Scandolo27428702019-10-11 16:21:16 -0700283 return &o
Matteo Scandolo4747d292019-08-05 11:50:18 -0700284}
285
William Kurkian0418bc82019-11-06 12:16:24 -0500286func (o *Onu) logStateChange(src string, dst string) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700287 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700288 "OnuId": o.ID,
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700289 "IntfId": o.PonPortID,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700290 "OnuSn": o.Sn(),
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700291 }).Debugf("Changing ONU InternalState from %s to %s", src, dst)
292}
293
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100294// ProcessOnuMessages starts indication channel for each ONU
David Bainbridge103cf022019-12-16 20:11:35 +0000295func (o *Onu) ProcessOnuMessages(ctx context.Context, stream openolt.Openolt_EnableIndicationServer, client openolt.OpenoltClient) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700296 onuLogger.WithFields(log.Fields{
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100297 "onuID": o.ID,
298 "onuSN": o.Sn(),
299 "ponPort": o.PonPortID,
300 }).Debug("Starting ONU Indication Channel")
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700301
David Bainbridge103cf022019-12-16 20:11:35 +0000302loop:
303 for {
304 select {
305 case <-ctx.Done():
306 onuLogger.WithFields(log.Fields{
307 "onuID": o.ID,
308 "onuSN": o.Sn(),
309 }).Tracef("ONU message handling canceled via context")
310 break loop
311 case message, ok := <-o.Channel:
312 if !ok || ctx.Err() != nil {
313 onuLogger.WithFields(log.Fields{
314 "onuID": o.ID,
315 "onuSN": o.Sn(),
316 }).Tracef("ONU message handling canceled via channel close")
317 break loop
Matteo Scandolo075b1892019-10-07 12:11:07 -0700318 }
David Bainbridge103cf022019-12-16 20:11:35 +0000319 onuLogger.WithFields(log.Fields{
320 "onuID": o.ID,
321 "onuSN": o.Sn(),
322 "messageType": message.Type,
323 }).Tracef("Received message on ONU Channel")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700324
David Bainbridge103cf022019-12-16 20:11:35 +0000325 switch message.Type {
326 case OnuDiscIndication:
327 msg, _ := message.Data.(OnuDiscIndicationMessage)
328 // 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 +0530329 time.Sleep(o.DiscoveryDelay)
David Bainbridge103cf022019-12-16 20:11:35 +0000330 o.sendOnuDiscIndication(msg, stream)
331 case OnuIndication:
332 msg, _ := message.Data.(OnuIndicationMessage)
333 o.sendOnuIndication(msg, stream)
334 case OMCI:
335 msg, _ := message.Data.(OmciMessage)
336 o.handleOmciMessage(msg, stream)
337 case FlowUpdate:
338 msg, _ := message.Data.(OnuFlowUpdateMessage)
339 o.handleFlowUpdate(msg)
340 case StartEAPOL:
341 log.Infof("Receive StartEAPOL message on ONU Channel")
342 eapol.SendEapStart(o.ID, o.PonPortID, o.Sn(), o.PortNo, o.HwAddress, o.InternalState, stream)
343 case StartDHCP:
344 log.Infof("Receive StartDHCP message on ONU Channel")
345 // FIXME use id, ponId as SendEapStart
346 dhcp.SendDHCPDiscovery(o.PonPortID, o.ID, o.Sn(), o.PortNo, o.InternalState, o.HwAddress, o.CTag, stream)
347 case OnuPacketOut:
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700348
David Bainbridge103cf022019-12-16 20:11:35 +0000349 msg, _ := message.Data.(OnuPacketMessage)
350
351 log.WithFields(log.Fields{
352 "IntfId": msg.IntfId,
353 "OnuId": msg.OnuId,
354 "pktType": msg.Type,
355 }).Trace("Received OnuPacketOut Message")
356
357 if msg.Type == packetHandlers.EAPOL {
358 eapol.HandleNextPacket(msg.OnuId, msg.IntfId, o.Sn(), o.PortNo, o.InternalState, msg.Packet, stream, client)
359 } else if msg.Type == packetHandlers.DHCP {
360 // NOTE here we receive packets going from the DHCP Server to the ONU
361 // for now we expect them to be double-tagged, but ideally the should be single tagged
362 dhcp.HandleNextPacket(o.ID, o.PonPortID, o.Sn(), o.PortNo, o.HwAddress, o.CTag, o.InternalState, msg.Packet, stream)
363 }
364 case OnuPacketIn:
365 // NOTE we only receive BBR packets here.
366 // Eapol.HandleNextPacket can handle both BBSim and BBr cases so the call is the same
367 // in the DHCP case VOLTHA only act as a proxy, the behaviour is completely different thus we have a dhcp.HandleNextBbrPacket
368 msg, _ := message.Data.(OnuPacketMessage)
369
370 log.WithFields(log.Fields{
371 "IntfId": msg.IntfId,
372 "OnuId": msg.OnuId,
373 "pktType": msg.Type,
374 }).Trace("Received OnuPacketIn Message")
375
376 if msg.Type == packetHandlers.EAPOL {
377 eapol.HandleNextPacket(msg.OnuId, msg.IntfId, o.Sn(), o.PortNo, o.InternalState, msg.Packet, stream, client)
378 } else if msg.Type == packetHandlers.DHCP {
379 dhcp.HandleNextBbrPacket(o.ID, o.PonPortID, o.Sn(), o.STag, o.HwAddress, o.DoneChannel, msg.Packet, client)
380 }
381 case DyingGaspIndication:
382 msg, _ := message.Data.(DyingGaspIndicationMessage)
383 o.sendDyingGaspInd(msg, stream)
384 case OmciIndication:
385 msg, _ := message.Data.(OmciIndicationMessage)
386 o.handleOmci(msg, client)
387 case SendEapolFlow:
388 o.sendEapolFlow(client)
389 case SendDhcpFlow:
390 o.sendDhcpFlow(client)
Arjun E K57a7fcb2020-01-30 06:44:45 +0000391 case IGMPMembershipReportV2:
392 log.Infof("Recieved IGMPMembershipReportV2 message on ONU channel")
393 igmp.SendIGMPMembershipReportV2(o.PonPortID, o.ID, o.Sn(), o.PortNo, o.HwAddress, stream)
394 case IGMPLeaveGroup:
395 log.Infof("Recieved IGMPLeaveGroupV2 message on ONU channel")
396 igmp.SendIGMPLeaveGroupV2(o.PonPortID, o.ID, o.Sn(), o.PortNo, o.HwAddress, stream)
Anand S Katti09541352020-01-29 15:54:01 +0530397 case IGMPMembershipReportV3:
398 log.Infof("Recieved IGMPMembershipReportV3 message on ONU channel")
399 igmp.SendIGMPMembershipReportV3(o.PonPortID, o.ID, o.Sn(), o.PortNo, o.HwAddress, stream)
David Bainbridge103cf022019-12-16 20:11:35 +0000400 default:
401 onuLogger.Warnf("Received unknown message data %v for type %v in OLT Channel", message.Data, message.Type)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700402 }
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700403 }
404 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100405 onuLogger.WithFields(log.Fields{
406 "onuID": o.ID,
407 "onuSN": o.Sn(),
408 }).Debug("Stopped handling ONU Indication Channel")
Matteo Scandolo4747d292019-08-05 11:50:18 -0700409}
410
Matteo Scandolodf3f85d2020-01-15 12:50:48 -0800411func (o *Onu) processOmciMessage(message omcisim.OmciChMessage, stream openolt.Openolt_EnableIndicationServer) {
William Kurkian9dadc5b2019-10-22 13:51:57 -0400412 switch message.Type {
Matteo Scandolodf3f85d2020-01-15 12:50:48 -0800413 case omcisim.UniLinkUp, omcisim.UniLinkDown:
414 onuLogger.WithFields(log.Fields{
415 "OnuId": message.Data.OnuId,
416 "IntfId": message.Data.IntfId,
417 "Type": message.Type,
418 }).Infof("UNI Link Alarm")
419 // TODO send to OLT
420
421 omciInd := openolt.OmciIndication{
422 IntfId: message.Data.IntfId,
423 OnuId: message.Data.OnuId,
424 Pkt: message.Packet,
425 }
426
427 omci := &openolt.Indication_OmciInd{OmciInd: &omciInd}
428 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
429 onuLogger.WithFields(log.Fields{
430 "IntfId": o.PonPortID,
431 "SerialNumber": o.Sn(),
432 "Type": message.Type,
433 "omciPacket": omciInd.Pkt,
434 }).Errorf("Failed to send UNI Link Alarm: %v", err)
435 return
436 }
437
438 onuLogger.WithFields(log.Fields{
439 "IntfId": o.PonPortID,
440 "SerialNumber": o.Sn(),
441 "Type": message.Type,
442 "omciPacket": omciInd.Pkt,
443 }).Info("UNI Link alarm sent")
444
William Kurkian9dadc5b2019-10-22 13:51:57 -0400445 case omcisim.GemPortAdded:
446 log.WithFields(log.Fields{
447 "OnuId": message.Data.OnuId,
448 "IntfId": message.Data.IntfId,
449 }).Infof("GemPort Added")
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700450
William Kurkian9dadc5b2019-10-22 13:51:57 -0400451 // NOTE if we receive the GemPort but we don't have EAPOL flows
452 // go an intermediate state, otherwise start auth
453 if o.InternalState.Is("enabled") {
454 if err := o.InternalState.Event("add_gem_port"); err != nil {
455 log.Errorf("Can't go to gem_port_added: %v", err)
456 }
457 } else if o.InternalState.Is("eapol_flow_received") {
Matteo Scandolo3c656a12019-12-10 09:54:51 -0800458 if o.Auth == true {
459 if err := o.InternalState.Event("start_auth"); err != nil {
460 log.Warnf("Can't go to auth_started: %v", err)
461 }
462 } else {
463 onuLogger.WithFields(log.Fields{
464 "IntfId": o.PonPortID,
465 "OnuId": o.ID,
466 "SerialNumber": o.Sn(),
467 }).Warn("Not starting authentication as Auth bit is not set in CLI parameters")
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700468 }
469 }
470 }
471}
472
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100473func (o Onu) NewSN(oltid int, intfid uint32, onuid uint32) *openolt.SerialNumber {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700474
475 sn := new(openolt.SerialNumber)
476
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700477 //sn = new(openolt.SerialNumber)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700478 sn.VendorId = []byte("BBSM")
479 sn.VendorSpecific = []byte{0, byte(oltid % 256), byte(intfid), byte(onuid)}
480
481 return sn
482}
483
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700484// NOTE handle_/process methods can change the ONU internal state as they are receiving messages
485// send method should not change the ONU state
486
William Kurkian0418bc82019-11-06 12:16:24 -0500487func (o *Onu) sendDyingGaspInd(msg DyingGaspIndicationMessage, stream openolt.Openolt_EnableIndicationServer) error {
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700488 alarmData := &openolt.AlarmIndication_DyingGaspInd{
489 DyingGaspInd: &openolt.DyingGaspIndication{
490 IntfId: msg.PonPortID,
491 OnuId: msg.OnuID,
492 Status: "on",
493 },
494 }
495 data := &openolt.Indication_AlarmInd{AlarmInd: &openolt.AlarmIndication{Data: alarmData}}
496
497 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
498 onuLogger.Errorf("Failed to send DyingGaspInd : %v", err)
499 return err
500 }
501 onuLogger.WithFields(log.Fields{
502 "IntfId": msg.PonPortID,
503 "OnuSn": o.Sn(),
504 "OnuId": msg.OnuID,
505 }).Info("sendDyingGaspInd")
506 return nil
507}
508
William Kurkian0418bc82019-11-06 12:16:24 -0500509func (o *Onu) sendOnuDiscIndication(msg OnuDiscIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700510 discoverData := &openolt.Indication_OnuDiscInd{OnuDiscInd: &openolt.OnuDiscIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700511 IntfId: msg.Onu.PonPortID,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700512 SerialNumber: msg.Onu.SerialNumber,
513 }}
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700514
Matteo Scandolo4747d292019-08-05 11:50:18 -0700515 if err := stream.Send(&openolt.Indication{Data: discoverData}); err != nil {
Matteo Scandolo11006992019-08-28 11:29:46 -0700516 log.Errorf("Failed to send Indication_OnuDiscInd: %v", err)
Matteo Scandolo99f18462019-10-28 14:14:28 -0700517 return
Matteo Scandolo4747d292019-08-05 11:50:18 -0700518 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700519
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700520 onuLogger.WithFields(log.Fields{
Matteo Scandolo4747d292019-08-05 11:50:18 -0700521 "IntfId": msg.Onu.PonPortID,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700522 "OnuSn": msg.Onu.Sn(),
523 "OnuId": o.ID,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700524 }).Debug("Sent Indication_OnuDiscInd")
Pragya Arya324337e2020-02-20 14:35:08 +0530525 publishEvent("ONU-discovery-indication-sent", int32(msg.Onu.PonPortID), int32(o.ID), msg.Onu.Sn())
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800526
527 // after DiscoveryRetryDelay check if the state is the same and in case send a new OnuDiscIndication
528 go func(delay time.Duration) {
Matteo Scandolo569e7172019-12-20 11:51:51 -0800529 time.Sleep(delay)
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800530 if o.InternalState.Current() == "discovered" {
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800531 o.sendOnuDiscIndication(msg, stream)
532 }
533 }(o.DiscoveryRetryDelay)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700534}
535
William Kurkian0418bc82019-11-06 12:16:24 -0500536func (o *Onu) sendOnuIndication(msg OnuIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700537 // NOTE voltha returns an ID, but if we use that ID then it complains:
538 // expected_onu_id: 1, received_onu_id: 1024, event: ONU-id-mismatch, can happen if both voltha and the olt rebooted
539 // so we're using the internal ID that is 1
540 // o.ID = msg.OnuID
Matteo Scandolo4747d292019-08-05 11:50:18 -0700541
542 indData := &openolt.Indication_OnuInd{OnuInd: &openolt.OnuIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700543 IntfId: o.PonPortID,
544 OnuId: o.ID,
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700545 OperState: msg.OperState.String(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700546 AdminState: o.OperState.Current(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700547 SerialNumber: o.SerialNumber,
548 }}
549 if err := stream.Send(&openolt.Indication{Data: indData}); err != nil {
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700550 // TODO do we need to transition to a broken state?
Matteo Scandolo11006992019-08-28 11:29:46 -0700551 log.Errorf("Failed to send Indication_OnuInd: %v", err)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700552 }
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700553 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700554 "IntfId": o.PonPortID,
555 "OnuId": o.ID,
556 "OperState": msg.OperState.String(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700557 "AdminState": msg.OperState.String(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700558 "OnuSn": o.Sn(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700559 }).Debug("Sent Indication_OnuInd")
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700560
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700561}
562
Pragya Arya324337e2020-02-20 14:35:08 +0530563func (o *Onu) publishOmciEvent(msg OmciMessage) {
564 if olt.PublishEvents {
565 _, _, msgType, _, _, _, err := omcisim.ParsePkt(HexDecode(msg.omciMsg.Pkt))
566 if err != nil {
567 log.Errorf("error in getting msgType %v", err)
568 return
569 }
570 if msgType == omcisim.MibUpload {
571 o.seqNumber = 0
572 publishEvent("MIB-upload-received", int32(o.PonPortID), int32(o.ID), common.OnuSnToString(o.SerialNumber))
573 } else if msgType == omcisim.MibUploadNext {
574 o.seqNumber++
575 if o.seqNumber > 290 {
576 publishEvent("MIB-upload-done", int32(o.PonPortID), int32(o.ID), common.OnuSnToString(o.SerialNumber))
577 }
578 }
579 }
580}
581
William Kurkian0418bc82019-11-06 12:16:24 -0500582func (o *Onu) handleOmciMessage(msg OmciMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700583
584 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700585 "IntfId": o.PonPortID,
Matteo Scandolo27428702019-10-11 16:21:16 -0700586 "SerialNumber": o.Sn(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700587 "omciPacket": msg.omciMsg.Pkt,
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700588 }).Tracef("Received OMCI message")
589
Pragya Arya324337e2020-02-20 14:35:08 +0530590 o.publishOmciEvent(msg)
591
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700592 var omciInd openolt.OmciIndication
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700593 respPkt, err := omcisim.OmciSim(o.PonPortID, o.ID, HexDecode(msg.omciMsg.Pkt))
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700594 if err != nil {
Matteo Scandolo27428702019-10-11 16:21:16 -0700595 onuLogger.WithFields(log.Fields{
596 "IntfId": o.PonPortID,
597 "SerialNumber": o.Sn(),
598 "omciPacket": omciInd.Pkt,
599 "msg": msg,
600 }).Errorf("Error handling OMCI message %v", msg)
601 return
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700602 }
603
604 omciInd.IntfId = o.PonPortID
605 omciInd.OnuId = o.ID
606 omciInd.Pkt = respPkt
607
608 omci := &openolt.Indication_OmciInd{OmciInd: &omciInd}
609 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
Matteo Scandolo27428702019-10-11 16:21:16 -0700610 onuLogger.WithFields(log.Fields{
611 "IntfId": o.PonPortID,
612 "SerialNumber": o.Sn(),
613 "omciPacket": omciInd.Pkt,
614 "msg": msg,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700615 }).Errorf("send omcisim indication failed: %v", err)
Matteo Scandolo27428702019-10-11 16:21:16 -0700616 return
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700617 }
618 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700619 "IntfId": o.PonPortID,
Matteo Scandolo27428702019-10-11 16:21:16 -0700620 "SerialNumber": o.Sn(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700621 "omciPacket": omciInd.Pkt,
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700622 }).Tracef("Sent OMCI message")
623}
624
Matteo Scandolo27428702019-10-11 16:21:16 -0700625func (o *Onu) storePortNumber(portNo uint32) {
Matteo Scandolo813402b2019-10-23 19:24:52 -0700626 // NOTE this needed only as long as we don't support multiple UNIs
Matteo Scandolo27428702019-10-11 16:21:16 -0700627 // we need to add support for multiple UNIs
628 // the action plan is:
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700629 // - refactor the omcisim-sim library to use https://github.com/cboling/omci instead of canned messages
Matteo Scandolo27428702019-10-11 16:21:16 -0700630 // - change the library so that it reports a single UNI and remove this workaroung
631 // - add support for multiple UNIs in BBSim
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700632 if o.PortNo == 0 || portNo < o.PortNo {
Matteo Scandolo813402b2019-10-23 19:24:52 -0700633 onuLogger.WithFields(log.Fields{
634 "IntfId": o.PonPortID,
635 "OnuId": o.ID,
636 "SerialNumber": o.Sn(),
637 "OnuPortNo": o.PortNo,
638 "FlowPortNo": portNo,
639 }).Debug("Storing ONU portNo")
Matteo Scandolo27428702019-10-11 16:21:16 -0700640 o.PortNo = portNo
641 }
642}
643
William Kurkian0418bc82019-11-06 12:16:24 -0500644func (o *Onu) SetID(id uint32) {
Matteo Scandolo583f17d2020-02-13 10:35:17 -0800645 onuLogger.WithFields(log.Fields{
646 "IntfId": o.PonPortID,
647 "OnuId": id,
648 "SerialNumber": o.Sn(),
649 }).Debug("Storing OnuId ")
William Kurkian0418bc82019-11-06 12:16:24 -0500650 o.ID = id
651}
652
Matteo Scandolo813402b2019-10-23 19:24:52 -0700653func (o *Onu) handleFlowUpdate(msg OnuFlowUpdateMessage) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700654 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700655 "DstPort": msg.Flow.Classifier.DstPort,
656 "EthType": fmt.Sprintf("%x", msg.Flow.Classifier.EthType),
657 "FlowId": msg.Flow.FlowId,
658 "FlowType": msg.Flow.FlowType,
Matteo Scandolo3ac52792020-03-05 14:21:08 -0800659 "GemportId": msg.Flow.GemportId,
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700660 "InnerVlan": msg.Flow.Classifier.IVid,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700661 "IntfId": msg.Flow.AccessIntfId,
662 "IpProto": msg.Flow.Classifier.IpProto,
663 "OnuId": msg.Flow.OnuId,
664 "OnuSn": o.Sn(),
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700665 "OuterVlan": msg.Flow.Classifier.OVid,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700666 "PortNo": msg.Flow.PortNo,
667 "SrcPort": msg.Flow.Classifier.SrcPort,
668 "UniID": msg.Flow.UniId,
Matteo Scandolo3ac52792020-03-05 14:21:08 -0800669 "ClassifierOPbits": msg.Flow.Classifier.OPbits,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700670 }).Debug("ONU receives Flow")
671
Matteo Scandolo813402b2019-10-23 19:24:52 -0700672 if msg.Flow.UniId != 0 {
673 // as of now BBSim only support a single UNI, so ignore everything that is not targeted to it
674 onuLogger.WithFields(log.Fields{
675 "IntfId": o.PonPortID,
676 "OnuId": o.ID,
677 "SerialNumber": o.Sn(),
678 }).Debug("Ignoring flow as it's not for the first UNI")
679 return
680 }
681
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700682 if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeEAPOL) && msg.Flow.Classifier.OVid == 4091 {
Matteo Scandolo27428702019-10-11 16:21:16 -0700683 // NOTE storing the PortNO, it's needed when sending PacketIndications
Matteo Scandolo813402b2019-10-23 19:24:52 -0700684 o.storePortNumber(uint32(msg.Flow.PortNo))
Matteo Scandolo27428702019-10-11 16:21:16 -0700685
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700686 // NOTE if we receive the EAPOL flows but we don't have GemPorts
687 // go an intermediate state, otherwise start auth
688 if o.InternalState.Is("enabled") {
689 if err := o.InternalState.Event("receive_eapol_flow"); err != nil {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700690 log.Warnf("Can't go to eapol_flow_received: %v", err)
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700691 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700692 } else if o.InternalState.Is("gem_port_added") {
Matteo Scandoloc1147092019-10-29 09:38:33 -0700693
694 if o.Auth == true {
695 if err := o.InternalState.Event("start_auth"); err != nil {
696 log.Warnf("Can't go to auth_started: %v", err)
697 }
698 } else {
699 onuLogger.WithFields(log.Fields{
700 "IntfId": o.PonPortID,
701 "OnuId": o.ID,
702 "SerialNumber": o.Sn(),
703 }).Warn("Not starting authentication as Auth bit is not set in CLI parameters")
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700704 }
Matteo Scandoloc1147092019-10-29 09:38:33 -0700705
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700706 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700707 } else if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeIPv4) &&
708 msg.Flow.Classifier.SrcPort == uint32(68) &&
Matteo Scandolo3ac52792020-03-05 14:21:08 -0800709 msg.Flow.Classifier.DstPort == uint32(67) &&
710 msg.Flow.Classifier.OPbits == 0 {
Matteo Scandolo99f18462019-10-28 14:14:28 -0700711
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100712 // 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 -0700713 o.DhcpFlowReceived = true
Matteo Scandoloc1147092019-10-29 09:38:33 -0700714
715 if o.Dhcp == true {
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100716 // NOTE we are receiving multiple DHCP flows but we shouldn't call the transition multiple times
Matteo Scandoloc1147092019-10-29 09:38:33 -0700717 if err := o.InternalState.Event("start_dhcp"); err != nil {
718 log.Errorf("Can't go to dhcp_started: %v", err)
719 }
720 } else {
721 onuLogger.WithFields(log.Fields{
722 "IntfId": o.PonPortID,
723 "OnuId": o.ID,
724 "SerialNumber": o.Sn(),
725 }).Warn("Not starting DHCP as Dhcp bit is not set in CLI parameters")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700726 }
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700727 }
728}
729
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700730// HexDecode converts the hex encoding to binary
731func HexDecode(pkt []byte) []byte {
732 p := make([]byte, len(pkt)/2)
733 for i, j := 0, 0; i < len(pkt); i, j = i+2, j+1 {
734 // Go figure this ;)
735 u := (pkt[i] & 15) + (pkt[i]>>6)*9
736 l := (pkt[i+1] & 15) + (pkt[i+1]>>6)*9
737 p[j] = u<<4 + l
738 }
739 onuLogger.Tracef("Omci decoded: %x.", p)
740 return p
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700741}
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700742
743// BBR methods
744
745func sendOmciMsg(pktBytes []byte, intfId uint32, onuId uint32, sn *openolt.SerialNumber, msgType string, client openolt.OpenoltClient) {
746 omciMsg := openolt.OmciMsg{
747 IntfId: intfId,
748 OnuId: onuId,
749 Pkt: pktBytes,
750 }
751
752 if _, err := client.OmciMsgOut(context.Background(), &omciMsg); err != nil {
753 log.WithFields(log.Fields{
754 "IntfId": intfId,
755 "OnuId": onuId,
756 "SerialNumber": common.OnuSnToString(sn),
757 "Pkt": omciMsg.Pkt,
758 }).Fatalf("Failed to send MIB Reset")
759 }
760 log.WithFields(log.Fields{
761 "IntfId": intfId,
762 "OnuId": onuId,
763 "SerialNumber": common.OnuSnToString(sn),
764 "Pkt": omciMsg.Pkt,
765 }).Tracef("Sent OMCI message %s", msgType)
766}
767
768func (onu *Onu) getNextTid(highPriority ...bool) uint16 {
769 var next uint16
770 if len(highPriority) > 0 && highPriority[0] {
771 next = onu.hpTid
772 onu.hpTid += 1
773 if onu.hpTid < 0x8000 {
774 onu.hpTid = 0x8000
775 }
776 } else {
777 next = onu.tid
778 onu.tid += 1
779 if onu.tid >= 0x8000 {
780 onu.tid = 1
781 }
782 }
783 return next
784}
785
786// TODO move this method in responders/omcisim
787func (o *Onu) StartOmci(client openolt.OpenoltClient) {
788 mibReset, _ := omcilib.CreateMibResetRequest(o.getNextTid(false))
789 sendOmciMsg(mibReset, o.PonPortID, o.ID, o.SerialNumber, "mibReset", client)
790}
791
792func (o *Onu) handleOmci(msg OmciIndicationMessage, client openolt.OpenoltClient) {
793 msgType, packet := omcilib.DecodeOmci(msg.OmciInd.Pkt)
794
795 log.WithFields(log.Fields{
796 "IntfId": msg.OmciInd.IntfId,
797 "OnuId": msg.OmciInd.OnuId,
798 "OnuSn": common.OnuSnToString(o.SerialNumber),
799 "Pkt": msg.OmciInd.Pkt,
800 "msgType": msgType,
Anand S Katti09541352020-01-29 15:54:01 +0530801 }).Trace("ONU Receives OMCI Msg")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700802 switch msgType {
803 default:
Matteo Scandolo813402b2019-10-23 19:24:52 -0700804 log.WithFields(log.Fields{
805 "IntfId": msg.OmciInd.IntfId,
806 "OnuId": msg.OmciInd.OnuId,
807 "OnuSn": common.OnuSnToString(o.SerialNumber),
808 "Pkt": msg.OmciInd.Pkt,
809 "msgType": msgType,
810 }).Fatalf("unexpected frame: %v", packet)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700811 case omci.MibResetResponseType:
812 mibUpload, _ := omcilib.CreateMibUploadRequest(o.getNextTid(false))
813 sendOmciMsg(mibUpload, o.PonPortID, o.ID, o.SerialNumber, "mibUpload", client)
814 case omci.MibUploadResponseType:
815 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
816 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
817 case omci.MibUploadNextResponseType:
818 o.seqNumber++
819
820 if o.seqNumber > 290 {
821 // NOTE we are done with the MIB Upload (290 is the number of messages the omci-sim library will respond to)
822 galEnet, _ := omcilib.CreateGalEnetRequest(o.getNextTid(false))
823 sendOmciMsg(galEnet, o.PonPortID, o.ID, o.SerialNumber, "CreateGalEnetRequest", client)
824 } else {
825 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
826 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
827 }
828 case omci.CreateResponseType:
829 // NOTE Creating a GemPort,
830 // BBsim actually doesn't care about the values, so we can do we want with the parameters
831 // In the same way we can create a GemPort even without setting up UNIs/TConts/...
832 // but we need the GemPort to trigger the state change
833
834 if !o.HasGemPort {
835 // NOTE this sends a CreateRequestType and BBSim replies with a CreateResponseType
836 // thus we send this request only once
837 gemReq, _ := omcilib.CreateGemPortRequest(o.getNextTid(false))
838 sendOmciMsg(gemReq, o.PonPortID, o.ID, o.SerialNumber, "CreateGemPortRequest", client)
839 o.HasGemPort = true
840 } else {
841 if err := o.InternalState.Event("send_eapol_flow"); err != nil {
842 onuLogger.WithFields(log.Fields{
843 "OnuId": o.ID,
844 "IntfId": o.PonPortID,
845 "OnuSn": o.Sn(),
846 }).Errorf("Error while transitioning ONU State %v", err)
847 }
848 }
849
850 }
851}
852
853func (o *Onu) sendEapolFlow(client openolt.OpenoltClient) {
854
855 classifierProto := openolt.Classifier{
856 EthType: uint32(layers.EthernetTypeEAPOL),
857 OVid: 4091,
858 }
859
860 actionProto := openolt.Action{}
861
862 downstreamFlow := openolt.Flow{
863 AccessIntfId: int32(o.PonPortID),
864 OnuId: int32(o.ID),
Matteo Scandolo813402b2019-10-23 19:24:52 -0700865 UniId: int32(0), // NOTE do not hardcode this, we need to support multiple UNIs
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700866 FlowId: uint32(o.ID),
867 FlowType: "downstream",
868 AllocId: int32(0),
869 NetworkIntfId: int32(0),
870 GemportId: int32(1), // FIXME use the same value as CreateGemPortRequest PortID, do not hardcode
871 Classifier: &classifierProto,
872 Action: &actionProto,
873 Priority: int32(100),
874 Cookie: uint64(o.ID),
875 PortNo: uint32(o.ID), // NOTE we are using this to map an incoming packetIndication to an ONU
876 }
877
878 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
879 log.WithFields(log.Fields{
880 "IntfId": o.PonPortID,
881 "OnuId": o.ID,
882 "FlowId": downstreamFlow.FlowId,
883 "PortNo": downstreamFlow.PortNo,
884 "SerialNumber": common.OnuSnToString(o.SerialNumber),
885 }).Fatalf("Failed to EAPOL Flow")
886 }
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 }).Info("Sent EAPOL Flow")
894}
895
896func (o *Onu) sendDhcpFlow(client openolt.OpenoltClient) {
897 classifierProto := openolt.Classifier{
898 EthType: uint32(layers.EthernetTypeIPv4),
899 SrcPort: uint32(68),
900 DstPort: uint32(67),
901 }
902
903 actionProto := openolt.Action{}
904
905 downstreamFlow := openolt.Flow{
906 AccessIntfId: int32(o.PonPortID),
907 OnuId: int32(o.ID),
Matteo Scandolo813402b2019-10-23 19:24:52 -0700908 UniId: int32(0), // FIXME do not hardcode this
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700909 FlowId: uint32(o.ID),
910 FlowType: "downstream",
911 AllocId: int32(0),
912 NetworkIntfId: int32(0),
913 GemportId: int32(1), // FIXME use the same value as CreateGemPortRequest PortID, do not hardcode
914 Classifier: &classifierProto,
915 Action: &actionProto,
916 Priority: int32(100),
917 Cookie: uint64(o.ID),
918 PortNo: uint32(o.ID), // NOTE we are using this to map an incoming packetIndication to an ONU
919 }
920
921 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
922 log.WithFields(log.Fields{
923 "IntfId": o.PonPortID,
924 "OnuId": o.ID,
925 "FlowId": downstreamFlow.FlowId,
926 "PortNo": downstreamFlow.PortNo,
927 "SerialNumber": common.OnuSnToString(o.SerialNumber),
928 }).Fatalf("Failed to send DHCP Flow")
929 }
930 log.WithFields(log.Fields{
931 "IntfId": o.PonPortID,
932 "OnuId": o.ID,
933 "FlowId": downstreamFlow.FlowId,
934 "PortNo": downstreamFlow.PortNo,
935 "SerialNumber": common.OnuSnToString(o.SerialNumber),
936 }).Info("Sent DHCP Flow")
937}
Pragya Arya8bdb4532020-03-02 17:08:09 +0530938
939// DeleteFlow method search and delete flowKey from the onu flows slice
940func (onu *Onu) DeleteFlow(key FlowKey) {
941 for pos, flowKey := range onu.Flows {
942 if flowKey == key {
943 // delete the flowKey by shifting all flowKeys by one
944 onu.Flows = append(onu.Flows[:pos], onu.Flows[pos+1:]...)
945 t := make([]FlowKey, len(onu.Flows))
946 copy(t, onu.Flows)
947 onu.Flows = t
948 break
949 }
950 }
951}