blob: 1acb063cef69d5cd8b5e5dc3dc03cf56c15138de [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 Scandoloeb6b5af2020-06-24 16:23:58 -070029 "github.com/jpillora/backoff"
Matteo Scandolo4747d292019-08-05 11:50:18 -070030 "github.com/looplab/fsm"
Matteo Scandolo075b1892019-10-07 12:11:07 -070031 "github.com/opencord/bbsim/internal/bbsim/packetHandlers"
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070032 "github.com/opencord/bbsim/internal/bbsim/responders/dhcp"
33 "github.com/opencord/bbsim/internal/bbsim/responders/eapol"
Arjun E K57a7fcb2020-01-30 06:44:45 +000034 "github.com/opencord/bbsim/internal/bbsim/responders/igmp"
Matteo Scandolo40e067f2019-10-16 16:59:41 -070035 "github.com/opencord/bbsim/internal/common"
36 omcilib "github.com/opencord/bbsim/internal/common/omci"
37 omcisim "github.com/opencord/omci-sim"
Matteo Scandolo3de9de02019-11-14 13:40:03 -080038 "github.com/opencord/voltha-protos/v2/go/openolt"
Matteo Scandolod74abba2020-04-16 16:36:44 -070039 "github.com/opencord/voltha-protos/v2/go/tech_profile"
Matteo Scandolo4747d292019-08-05 11:50:18 -070040 log "github.com/sirupsen/logrus"
41)
42
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070043var onuLogger = log.WithFields(log.Fields{
44 "module": "ONU",
45})
46
Pragya Arya8bdb4532020-03-02 17:08:09 +053047type FlowKey struct {
48 ID uint32
49 Direction string
50}
51
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070052type Onu struct {
Matteo Scandoloe811ae92019-12-10 17:50:14 -080053 ID uint32
54 PonPortID uint32
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -070055 PonPort *PonPort
Matteo Scandoloe811ae92019-12-10 17:50:14 -080056 STag int
57 CTag int
58 Auth bool // automatically start EAPOL if set to true
59 Dhcp bool // automatically start DHCP if set to true
60 HwAddress net.HardwareAddr
61 InternalState *fsm.FSM
Pragya Arya2225f202020-01-29 18:05:01 +053062 DiscoveryRetryDelay time.Duration // this is the time between subsequent Discovery Indication
63 DiscoveryDelay time.Duration // this is the time to send the first Discovery Indication
Shrey Baidf8abccc2020-06-15 19:41:22 +053064 Backoff *backoff.Backoff
Matteo Scandoloe811ae92019-12-10 17:50:14 -080065 // ONU State
Matteo Scandolo27428702019-10-11 16:21:16 -070066 // PortNo comes with flows and it's used when sending packetIndications,
67 // There is one PortNo per UNI Port, for now we're only storing the first one
Matteo Scandolo47ef64b2020-04-20 14:16:07 -070068 // FIXME add support for multiple UNIs (each UNI has a different PortNo)
Matteo Scandolo5ff80082019-12-20 13:20:57 -080069 PortNo uint32
70 GemPortAdded bool
71 EapolFlowReceived bool
72 DhcpFlowReceived bool
73 Flows []FlowKey
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -070074 FlowIds []uint32 // keep track of the flows we currently have in the ONU
Matteo Scandolo99f18462019-10-28 14:14:28 -070075
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070076 OperState *fsm.FSM
77 SerialNumber *openolt.SerialNumber
78
Matteo Scandolo5ff80082019-12-20 13:20:57 -080079 Channel chan Message // this Channel is to track state changes OMCI messages, EAPOL and DHCP packets
80 GemPortChannels []chan bool // this channels are used to notify everyone that is interested that a GemPort has been added
Matteo Scandolo40e067f2019-10-16 16:59:41 -070081
82 // OMCI params
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -070083 tid uint16
84 hpTid uint16
85 seqNumber uint16
Matteo Scandolo40e067f2019-10-16 16:59:41 -070086
Anand S Katti09541352020-01-29 15:54:01 +053087 DoneChannel chan bool // this channel is used to signal once the onu is complete (when the struct is used by BBR)
88 TrafficSchedulers *tech_profile.TrafficSchedulers
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070089}
90
Matteo Scandolo99f18462019-10-28 14:14:28 -070091func (o *Onu) Sn() string {
Matteo Scandolo40e067f2019-10-16 16:59:41 -070092 return common.OnuSnToString(o.SerialNumber)
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070093}
94
Matteo Scandolo5ff80082019-12-20 13:20:57 -080095func (o *Onu) GetGemPortChan() chan bool {
96 listener := make(chan bool, 1)
97 o.GemPortChannels = append(o.GemPortChannels, listener)
98 return listener
99}
100
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700101func CreateONU(olt *OltDevice, pon *PonPort, id uint32, sTag int, cTag int, auth bool, dhcp bool, delay time.Duration, isMock bool) *Onu {
Shrey Baidf8abccc2020-06-15 19:41:22 +0530102 b := &backoff.Backoff{
103 //These are the defaults
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700104 Min: 5 * time.Second,
Shrey Baidf8abccc2020-06-15 19:41:22 +0530105 Max: 35 * time.Second,
106 Factor: 1.5,
107 Jitter: false,
108 }
Matteo Scandolo4747d292019-08-05 11:50:18 -0700109
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700110 o := Onu{
Pragya Arya2225f202020-01-29 18:05:01 +0530111 ID: 0,
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800112 PonPortID: pon.ID,
113 PonPort: pon,
114 STag: sTag,
115 CTag: cTag,
116 Auth: auth,
117 Dhcp: dhcp,
Matteo Scandolo378b8c92020-04-16 14:34:22 -0700118 HwAddress: net.HardwareAddr{0x2e, 0x60, 0x70, byte(olt.ID), byte(pon.ID), byte(id)},
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800119 PortNo: 0,
120 tid: 0x1,
121 hpTid: 0x8000,
122 seqNumber: 0,
123 DoneChannel: make(chan bool, 1),
124 DhcpFlowReceived: false,
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800125 EapolFlowReceived: false,
126 GemPortAdded: false,
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800127 DiscoveryRetryDelay: 60 * time.Second, // this is used to send OnuDiscoveryIndications until an activate call is received
Pragya Arya8bdb4532020-03-02 17:08:09 +0530128 Flows: []FlowKey{},
Pragya Arya2225f202020-01-29 18:05:01 +0530129 DiscoveryDelay: delay,
Shrey Baidf8abccc2020-06-15 19:41:22 +0530130 Backoff: b,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700131 }
Pragya Arya2225f202020-01-29 18:05:01 +0530132 o.SerialNumber = o.NewSN(olt.ID, pon.ID, id)
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700133 // NOTE this state machine is used to track the operational
134 // state as requested by VOLTHA
135 o.OperState = getOperStateFSM(func(e *fsm.Event) {
136 onuLogger.WithFields(log.Fields{
137 "ID": o.ID,
138 }).Debugf("Changing ONU OperState from %s to %s", e.Src, e.Dst)
139 })
140
141 // NOTE this state machine is used to activate the OMCI, EAPOL and DHCP clients
142 o.InternalState = fsm.NewFSM(
143 "created",
144 fsm.Events{
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700145 // DEVICE Lifecycle
Hardik Windlassad790cb2020-06-17 21:26:22 +0530146 {Name: "initialize", Src: []string{"created", "disabled", "pon_disabled"}, Dst: "initialized"},
147 {Name: "discover", Src: []string{"initialized"}, Dst: "discovered"},
148 {Name: "enable", Src: []string{"discovered", "pon_disabled"}, Dst: "enabled"},
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700149 {Name: "receive_eapol_flow", Src: []string{"enabled", "gem_port_added"}, Dst: "eapol_flow_received"},
150 {Name: "add_gem_port", Src: []string{"enabled", "eapol_flow_received"}, Dst: "gem_port_added"},
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100151 // 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 -0800152 {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 +0530153 // 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 -0800154 {Name: "pon_disabled", Src: []string{"enabled", "eap_response_success_received", "auth_failed", "dhcp_ack_received", "dhcp_failed"}, Dst: "pon_disabled"},
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700155 // EAPOL
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800156 {Name: "start_auth", Src: []string{"enabled", "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 -0700157 {Name: "eap_start_sent", Src: []string{"auth_started"}, Dst: "eap_start_sent"},
158 {Name: "eap_response_identity_sent", Src: []string{"eap_start_sent"}, Dst: "eap_response_identity_sent"},
159 {Name: "eap_response_challenge_sent", Src: []string{"eap_response_identity_sent"}, Dst: "eap_response_challenge_sent"},
160 {Name: "eap_response_success_received", Src: []string{"eap_response_challenge_sent"}, Dst: "eap_response_success_received"},
161 {Name: "auth_failed", Src: []string{"auth_started", "eap_start_sent", "eap_response_identity_sent", "eap_response_challenge_sent"}, Dst: "auth_failed"},
162 // DHCP
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800163 {Name: "start_dhcp", Src: []string{"enabled", "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 -0700164 {Name: "dhcp_discovery_sent", Src: []string{"dhcp_started"}, Dst: "dhcp_discovery_sent"},
165 {Name: "dhcp_request_sent", Src: []string{"dhcp_discovery_sent"}, Dst: "dhcp_request_sent"},
166 {Name: "dhcp_ack_received", Src: []string{"dhcp_request_sent"}, Dst: "dhcp_ack_received"},
167 {Name: "dhcp_failed", Src: []string{"dhcp_started", "dhcp_discovery_sent", "dhcp_request_sent"}, Dst: "dhcp_failed"},
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700168 // BBR States
169 // TODO add start OMCI state
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100170 {Name: "send_eapol_flow", Src: []string{"initialized"}, Dst: "eapol_flow_sent"},
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700171 {Name: "send_dhcp_flow", Src: []string{"eapol_flow_sent"}, Dst: "dhcp_flow_sent"},
Arjun E K57a7fcb2020-01-30 06:44:45 +0000172 // IGMP
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800173 {Name: "igmp_join_start", Src: []string{"eap_response_success_received", "dhcp_ack_received", "igmp_left", "igmp_join_error", "igmp_join_started"}, Dst: "igmp_join_started"},
174 {Name: "igmp_join_startv3", Src: []string{"eap_response_success_received", "dhcp_ack_received", "igmp_left", "igmp_join_error", "igmp_join_started"}, Dst: "igmp_join_started"},
Shubham Sharmabd4b6572020-02-12 13:00:44 +0000175 {Name: "igmp_join_error", Src: []string{"igmp_join_started"}, Dst: "igmp_join_error"},
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800176 {Name: "igmp_leave", Src: []string{"igmp_join_started", "eap_response_success_received", "dhcp_ack_received"}, Dst: "igmp_left"},
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700177 },
178 fsm.Callbacks{
179 "enter_state": func(e *fsm.Event) {
180 o.logStateChange(e.Src, e.Dst)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700181 },
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100182 "enter_initialized": func(e *fsm.Event) {
183 // create new channel for ProcessOnuMessages Go routine
184 o.Channel = make(chan Message, 2048)
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800185
186 if err := o.OperState.Event("enable"); err != nil {
187 onuLogger.WithFields(log.Fields{
188 "OnuId": o.ID,
189 "IntfId": o.PonPortID,
190 "OnuSn": o.Sn(),
191 }).Errorf("Cannot change ONU OperState to up: %s", err.Error())
192 }
193
Pragya Arya1cbefa42020-01-13 12:15:29 +0530194 if !isMock {
195 // start ProcessOnuMessages Go routine
196 go o.ProcessOnuMessages(olt.enableContext, *olt.OpenoltStream, nil)
197 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100198 },
199 "enter_discovered": func(e *fsm.Event) {
200 msg := Message{
201 Type: OnuDiscIndication,
202 Data: OnuDiscIndicationMessage{
203 Onu: &o,
204 OperState: UP,
205 },
206 }
207 o.Channel <- msg
208 },
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700209 "enter_enabled": func(event *fsm.Event) {
210 msg := Message{
211 Type: OnuIndication,
212 Data: OnuIndicationMessage{
213 OnuSN: o.SerialNumber,
214 PonPortID: o.PonPortID,
215 OperState: UP,
216 },
217 }
218 o.Channel <- msg
219 },
220 "enter_disabled": func(event *fsm.Event) {
Matteo Scandolo47ef64b2020-04-20 14:16:07 -0700221
222 // clean the ONU state
223 o.DhcpFlowReceived = false
224 o.PortNo = 0
225 o.Flows = []FlowKey{}
226
227 // set the OpenState to disabled
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800228 if err := o.OperState.Event("disable"); err != nil {
229 onuLogger.WithFields(log.Fields{
230 "OnuId": o.ID,
231 "IntfId": o.PonPortID,
232 "OnuSn": o.Sn(),
233 }).Errorf("Cannot change ONU OperState to down: %s", err.Error())
234 }
Matteo Scandolo47ef64b2020-04-20 14:16:07 -0700235
236 // send the OnuIndication DOWN event
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700237 msg := Message{
238 Type: OnuIndication,
239 Data: OnuIndicationMessage{
240 OnuSN: o.SerialNumber,
241 PonPortID: o.PonPortID,
242 OperState: DOWN,
243 },
244 }
245 o.Channel <- msg
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100246 // terminate the ONU's ProcessOnuMessages Go routine
247 close(o.Channel)
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700248 },
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800249 "before_start_auth": func(e *fsm.Event) {
250 if o.EapolFlowReceived == false {
251 e.Cancel(errors.New("cannot-go-to-auth-started-as-eapol-flow-is-missing"))
252 return
253 }
254 if o.GemPortAdded == false {
255 e.Cancel(errors.New("cannot-go-to-auth-started-as-gemport-is-missing"))
256 return
257 }
258 },
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700259 "enter_auth_started": func(e *fsm.Event) {
260 o.logStateChange(e.Src, e.Dst)
261 msg := Message{
262 Type: StartEAPOL,
263 Data: PacketMessage{
264 PonPortID: o.PonPortID,
265 OnuID: o.ID,
266 },
267 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700268 o.Channel <- msg
Matteo Scandolo4747d292019-08-05 11:50:18 -0700269 },
Pragya Arya324337e2020-02-20 14:35:08 +0530270 "enter_eap_response_success_received": func(e *fsm.Event) {
271 publishEvent("ONU-authentication-done", int32(o.PonPortID), int32(o.ID), o.Sn())
272 },
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700273 "enter_auth_failed": func(e *fsm.Event) {
274 onuLogger.WithFields(log.Fields{
275 "OnuId": o.ID,
276 "IntfId": o.PonPortID,
277 "OnuSn": o.Sn(),
278 }).Errorf("ONU failed to authenticate!")
279 },
Matteo Scandolo99f18462019-10-28 14:14:28 -0700280 "before_start_dhcp": func(e *fsm.Event) {
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800281
282 // we allow transition from eanbled to dhcp_started only if auth was set to false
283 if o.InternalState.Current() == "enabled" && o.Auth {
284 e.Cancel(errors.New("cannot-go-to-dhcp-started-as-authentication-is-required"))
285 return
286 }
287
Matteo Scandolo99f18462019-10-28 14:14:28 -0700288 if o.DhcpFlowReceived == false {
289 e.Cancel(errors.New("cannot-go-to-dhcp-started-as-dhcp-flow-is-missing"))
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800290 return
291 }
292
293 if o.GemPortAdded == false {
294 e.Cancel(errors.New("cannot-go-to-dhcp-started-as-gemport-is-missing"))
295 return
Matteo Scandolo99f18462019-10-28 14:14:28 -0700296 }
297 },
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700298 "enter_dhcp_started": func(e *fsm.Event) {
299 msg := Message{
300 Type: StartDHCP,
301 Data: PacketMessage{
302 PonPortID: o.PonPortID,
303 OnuID: o.ID,
304 },
305 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700306 o.Channel <- msg
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700307 },
Pragya Arya324337e2020-02-20 14:35:08 +0530308 "enter_dhcp_ack_received": func(e *fsm.Event) {
309 publishEvent("ONU-DHCP-ACK-received", int32(o.PonPortID), int32(o.ID), o.Sn())
310 },
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700311 "enter_dhcp_failed": func(e *fsm.Event) {
312 onuLogger.WithFields(log.Fields{
313 "OnuId": o.ID,
314 "IntfId": o.PonPortID,
315 "OnuSn": o.Sn(),
316 }).Errorf("ONU failed to DHCP!")
317 },
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700318 "enter_eapol_flow_sent": func(e *fsm.Event) {
319 msg := Message{
320 Type: SendEapolFlow,
321 }
322 o.Channel <- msg
323 },
324 "enter_dhcp_flow_sent": func(e *fsm.Event) {
325 msg := Message{
326 Type: SendDhcpFlow,
327 }
328 o.Channel <- msg
329 },
Arjun E K57a7fcb2020-01-30 06:44:45 +0000330 "igmp_join_start": func(e *fsm.Event) {
331 msg := Message{
332 Type: IGMPMembershipReportV2,
333 }
334 o.Channel <- msg
335 },
336 "igmp_leave": func(e *fsm.Event) {
337 msg := Message{
338 Type: IGMPLeaveGroup}
339 o.Channel <- msg
340 },
Anand S Katti09541352020-01-29 15:54:01 +0530341 "igmp_join_startv3": func(e *fsm.Event) {
342 msg := Message{
343 Type: IGMPMembershipReportV3,
344 }
345 o.Channel <- msg
346 },
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700347 },
348 )
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100349
Matteo Scandolo27428702019-10-11 16:21:16 -0700350 return &o
Matteo Scandolo4747d292019-08-05 11:50:18 -0700351}
352
William Kurkian0418bc82019-11-06 12:16:24 -0500353func (o *Onu) logStateChange(src string, dst string) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700354 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700355 "OnuId": o.ID,
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700356 "IntfId": o.PonPortID,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700357 "OnuSn": o.Sn(),
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700358 }).Debugf("Changing ONU InternalState from %s to %s", src, dst)
359}
360
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100361// ProcessOnuMessages starts indication channel for each ONU
David Bainbridge103cf022019-12-16 20:11:35 +0000362func (o *Onu) ProcessOnuMessages(ctx context.Context, stream openolt.Openolt_EnableIndicationServer, client openolt.OpenoltClient) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700363 onuLogger.WithFields(log.Fields{
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100364 "onuID": o.ID,
365 "onuSN": o.Sn(),
366 "ponPort": o.PonPortID,
367 }).Debug("Starting ONU Indication Channel")
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700368
David Bainbridge103cf022019-12-16 20:11:35 +0000369loop:
370 for {
371 select {
372 case <-ctx.Done():
373 onuLogger.WithFields(log.Fields{
374 "onuID": o.ID,
375 "onuSN": o.Sn(),
376 }).Tracef("ONU message handling canceled via context")
377 break loop
378 case message, ok := <-o.Channel:
379 if !ok || ctx.Err() != nil {
380 onuLogger.WithFields(log.Fields{
381 "onuID": o.ID,
382 "onuSN": o.Sn(),
383 }).Tracef("ONU message handling canceled via channel close")
384 break loop
Matteo Scandolo075b1892019-10-07 12:11:07 -0700385 }
David Bainbridge103cf022019-12-16 20:11:35 +0000386 onuLogger.WithFields(log.Fields{
387 "onuID": o.ID,
388 "onuSN": o.Sn(),
389 "messageType": message.Type,
390 }).Tracef("Received message on ONU Channel")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700391
David Bainbridge103cf022019-12-16 20:11:35 +0000392 switch message.Type {
393 case OnuDiscIndication:
394 msg, _ := message.Data.(OnuDiscIndicationMessage)
395 // 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 +0530396 time.Sleep(o.DiscoveryDelay)
David Bainbridge103cf022019-12-16 20:11:35 +0000397 o.sendOnuDiscIndication(msg, stream)
398 case OnuIndication:
399 msg, _ := message.Data.(OnuIndicationMessage)
400 o.sendOnuIndication(msg, stream)
401 case OMCI:
402 msg, _ := message.Data.(OmciMessage)
403 o.handleOmciMessage(msg, stream)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700404 case FlowAdd:
David Bainbridge103cf022019-12-16 20:11:35 +0000405 msg, _ := message.Data.(OnuFlowUpdateMessage)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700406 o.handleFlowAdd(msg)
407 case FlowRemoved:
408 msg, _ := message.Data.(OnuFlowUpdateMessage)
409 o.handleFlowRemove(msg)
David Bainbridge103cf022019-12-16 20:11:35 +0000410 case StartEAPOL:
Shrey Baidf8abccc2020-06-15 19:41:22 +0530411 o.handleEAPOLStart(stream)
David Bainbridge103cf022019-12-16 20:11:35 +0000412 case StartDHCP:
Shrey Baidf8abccc2020-06-15 19:41:22 +0530413 o.handleDHCPStart(stream)
David Bainbridge103cf022019-12-16 20:11:35 +0000414 case OnuPacketOut:
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700415
David Bainbridge103cf022019-12-16 20:11:35 +0000416 msg, _ := message.Data.(OnuPacketMessage)
417
418 log.WithFields(log.Fields{
419 "IntfId": msg.IntfId,
420 "OnuId": msg.OnuId,
421 "pktType": msg.Type,
422 }).Trace("Received OnuPacketOut Message")
423
424 if msg.Type == packetHandlers.EAPOL {
425 eapol.HandleNextPacket(msg.OnuId, msg.IntfId, o.Sn(), o.PortNo, o.InternalState, msg.Packet, stream, client)
426 } else if msg.Type == packetHandlers.DHCP {
427 // NOTE here we receive packets going from the DHCP Server to the ONU
428 // for now we expect them to be double-tagged, but ideally the should be single tagged
Matteo Scandolo378b8c92020-04-16 14:34:22 -0700429 dhcp.HandleNextPacket(o.PonPort.Olt.ID, o.ID, o.PonPortID, o.Sn(), o.PortNo, o.HwAddress, o.CTag, o.InternalState, msg.Packet, stream)
David Bainbridge103cf022019-12-16 20:11:35 +0000430 }
431 case OnuPacketIn:
432 // NOTE we only receive BBR packets here.
433 // Eapol.HandleNextPacket can handle both BBSim and BBr cases so the call is the same
434 // in the DHCP case VOLTHA only act as a proxy, the behaviour is completely different thus we have a dhcp.HandleNextBbrPacket
435 msg, _ := message.Data.(OnuPacketMessage)
436
437 log.WithFields(log.Fields{
438 "IntfId": msg.IntfId,
439 "OnuId": msg.OnuId,
440 "pktType": msg.Type,
441 }).Trace("Received OnuPacketIn Message")
442
443 if msg.Type == packetHandlers.EAPOL {
444 eapol.HandleNextPacket(msg.OnuId, msg.IntfId, o.Sn(), o.PortNo, o.InternalState, msg.Packet, stream, client)
445 } else if msg.Type == packetHandlers.DHCP {
446 dhcp.HandleNextBbrPacket(o.ID, o.PonPortID, o.Sn(), o.STag, o.HwAddress, o.DoneChannel, msg.Packet, client)
447 }
David Bainbridge103cf022019-12-16 20:11:35 +0000448 case OmciIndication:
449 msg, _ := message.Data.(OmciIndicationMessage)
450 o.handleOmci(msg, client)
451 case SendEapolFlow:
452 o.sendEapolFlow(client)
453 case SendDhcpFlow:
454 o.sendDhcpFlow(client)
Arjun E K57a7fcb2020-01-30 06:44:45 +0000455 case IGMPMembershipReportV2:
456 log.Infof("Recieved IGMPMembershipReportV2 message on ONU channel")
457 igmp.SendIGMPMembershipReportV2(o.PonPortID, o.ID, o.Sn(), o.PortNo, o.HwAddress, stream)
458 case IGMPLeaveGroup:
459 log.Infof("Recieved IGMPLeaveGroupV2 message on ONU channel")
460 igmp.SendIGMPLeaveGroupV2(o.PonPortID, o.ID, o.Sn(), o.PortNo, o.HwAddress, stream)
Anand S Katti09541352020-01-29 15:54:01 +0530461 case IGMPMembershipReportV3:
462 log.Infof("Recieved IGMPMembershipReportV3 message on ONU channel")
463 igmp.SendIGMPMembershipReportV3(o.PonPortID, o.ID, o.Sn(), o.PortNo, o.HwAddress, stream)
David Bainbridge103cf022019-12-16 20:11:35 +0000464 default:
465 onuLogger.Warnf("Received unknown message data %v for type %v in OLT Channel", message.Data, message.Type)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700466 }
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700467 }
468 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100469 onuLogger.WithFields(log.Fields{
470 "onuID": o.ID,
471 "onuSN": o.Sn(),
472 }).Debug("Stopped handling ONU Indication Channel")
Matteo Scandolo4747d292019-08-05 11:50:18 -0700473}
474
Matteo Scandolodf3f85d2020-01-15 12:50:48 -0800475func (o *Onu) processOmciMessage(message omcisim.OmciChMessage, stream openolt.Openolt_EnableIndicationServer) {
William Kurkian9dadc5b2019-10-22 13:51:57 -0400476 switch message.Type {
Matteo Scandolodf3f85d2020-01-15 12:50:48 -0800477 case omcisim.UniLinkUp, omcisim.UniLinkDown:
478 onuLogger.WithFields(log.Fields{
479 "OnuId": message.Data.OnuId,
480 "IntfId": message.Data.IntfId,
481 "Type": message.Type,
482 }).Infof("UNI Link Alarm")
483 // TODO send to OLT
484
485 omciInd := openolt.OmciIndication{
486 IntfId: message.Data.IntfId,
487 OnuId: message.Data.OnuId,
488 Pkt: message.Packet,
489 }
490
491 omci := &openolt.Indication_OmciInd{OmciInd: &omciInd}
492 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
493 onuLogger.WithFields(log.Fields{
494 "IntfId": o.PonPortID,
495 "SerialNumber": o.Sn(),
496 "Type": message.Type,
497 "omciPacket": omciInd.Pkt,
498 }).Errorf("Failed to send UNI Link Alarm: %v", err)
499 return
500 }
501
502 onuLogger.WithFields(log.Fields{
503 "IntfId": o.PonPortID,
504 "SerialNumber": o.Sn(),
505 "Type": message.Type,
506 "omciPacket": omciInd.Pkt,
507 }).Info("UNI Link alarm sent")
508
William Kurkian9dadc5b2019-10-22 13:51:57 -0400509 case omcisim.GemPortAdded:
510 log.WithFields(log.Fields{
511 "OnuId": message.Data.OnuId,
512 "IntfId": message.Data.IntfId,
Matteo Scandolod74abba2020-04-16 16:36:44 -0700513 "OnuSn": o.Sn(),
William Kurkian9dadc5b2019-10-22 13:51:57 -0400514 }).Infof("GemPort Added")
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700515
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800516 o.GemPortAdded = true
517
518 // broadcast the change to all listeners
519 // and close the channels as once the GemPort is set
520 // it won't change anymore
521 for _, ch := range o.GemPortChannels {
522 ch <- true
523 close(ch)
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700524 }
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800525 o.GemPortChannels = []chan bool{}
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700526 }
527}
528
Shrey Baidf8abccc2020-06-15 19:41:22 +0530529func (o *Onu) handleEAPOLStart(stream openolt.Openolt_EnableIndicationServer) {
530 log.Infof("Receive StartEAPOL message on ONU Channel")
531 eapol.SendEapStart(o.ID, o.PonPortID, o.Sn(), o.PortNo, o.HwAddress, o.InternalState, stream)
532 go func(delay time.Duration) {
533 time.Sleep(delay)
534 if (o.InternalState.Current() == "eap_start_sent" ||
535 o.InternalState.Current() == "eap_response_identity_sent" ||
536 o.InternalState.Current() == "eap_response_challenge_sent" ||
537 o.InternalState.Current() == "auth_failed") && common.Options.BBSim.AuthRetry {
538 o.InternalState.Event("start_auth")
539 } else if o.InternalState.Current() == "eap_response_success_received" {
540 o.Backoff.Reset()
541 }
542 }(o.Backoff.Duration())
543}
544
545func (o *Onu) handleDHCPStart(stream openolt.Openolt_EnableIndicationServer) {
546 log.Infof("Receive StartDHCP message on ONU Channel")
547 // FIXME use id, ponId as SendEapStart
548 dhcp.SendDHCPDiscovery(o.PonPort.Olt.ID, o.PonPortID, o.ID, o.Sn(), o.PortNo, o.InternalState, o.HwAddress, o.CTag, stream)
549 go func(delay time.Duration) {
550 time.Sleep(delay)
551 if (o.InternalState.Current() == "dhcp_discovery_sent" ||
552 o.InternalState.Current() == "dhcp_request_sent" ||
553 o.InternalState.Current() == "dhcp_failed") && common.Options.BBSim.DhcpRetry {
554 o.InternalState.Event("start_dhcp")
555 } else if o.InternalState.Current() == "dhcp_ack_received" {
556 o.Backoff.Reset()
557 }
558 }(o.Backoff.Duration())
559}
560
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100561func (o Onu) NewSN(oltid int, intfid uint32, onuid uint32) *openolt.SerialNumber {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700562
563 sn := new(openolt.SerialNumber)
564
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700565 //sn = new(openolt.SerialNumber)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700566 sn.VendorId = []byte("BBSM")
567 sn.VendorSpecific = []byte{0, byte(oltid % 256), byte(intfid), byte(onuid)}
568
569 return sn
570}
571
William Kurkian0418bc82019-11-06 12:16:24 -0500572func (o *Onu) sendOnuDiscIndication(msg OnuDiscIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700573 discoverData := &openolt.Indication_OnuDiscInd{OnuDiscInd: &openolt.OnuDiscIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700574 IntfId: msg.Onu.PonPortID,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700575 SerialNumber: msg.Onu.SerialNumber,
576 }}
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700577
Matteo Scandolo4747d292019-08-05 11:50:18 -0700578 if err := stream.Send(&openolt.Indication{Data: discoverData}); err != nil {
Matteo Scandolo11006992019-08-28 11:29:46 -0700579 log.Errorf("Failed to send Indication_OnuDiscInd: %v", err)
Matteo Scandolo99f18462019-10-28 14:14:28 -0700580 return
Matteo Scandolo4747d292019-08-05 11:50:18 -0700581 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700582
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700583 onuLogger.WithFields(log.Fields{
Matteo Scandolo4747d292019-08-05 11:50:18 -0700584 "IntfId": msg.Onu.PonPortID,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700585 "OnuSn": msg.Onu.Sn(),
586 "OnuId": o.ID,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700587 }).Debug("Sent Indication_OnuDiscInd")
Pragya Arya324337e2020-02-20 14:35:08 +0530588 publishEvent("ONU-discovery-indication-sent", int32(msg.Onu.PonPortID), int32(o.ID), msg.Onu.Sn())
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800589
590 // after DiscoveryRetryDelay check if the state is the same and in case send a new OnuDiscIndication
591 go func(delay time.Duration) {
Matteo Scandolo569e7172019-12-20 11:51:51 -0800592 time.Sleep(delay)
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800593 if o.InternalState.Current() == "discovered" {
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800594 o.sendOnuDiscIndication(msg, stream)
595 }
596 }(o.DiscoveryRetryDelay)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700597}
598
William Kurkian0418bc82019-11-06 12:16:24 -0500599func (o *Onu) sendOnuIndication(msg OnuIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700600 // NOTE voltha returns an ID, but if we use that ID then it complains:
601 // expected_onu_id: 1, received_onu_id: 1024, event: ONU-id-mismatch, can happen if both voltha and the olt rebooted
602 // so we're using the internal ID that is 1
603 // o.ID = msg.OnuID
Matteo Scandolo4747d292019-08-05 11:50:18 -0700604
605 indData := &openolt.Indication_OnuInd{OnuInd: &openolt.OnuIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700606 IntfId: o.PonPortID,
607 OnuId: o.ID,
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700608 OperState: msg.OperState.String(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700609 AdminState: o.OperState.Current(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700610 SerialNumber: o.SerialNumber,
611 }}
612 if err := stream.Send(&openolt.Indication{Data: indData}); err != nil {
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800613 // NOTE do we need to transition to a broken state?
Matteo Scandolo11006992019-08-28 11:29:46 -0700614 log.Errorf("Failed to send Indication_OnuInd: %v", err)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700615 }
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700616 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700617 "IntfId": o.PonPortID,
618 "OnuId": o.ID,
619 "OperState": msg.OperState.String(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700620 "AdminState": msg.OperState.String(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700621 "OnuSn": o.Sn(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700622 }).Debug("Sent Indication_OnuInd")
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700623
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700624}
625
Pragya Arya324337e2020-02-20 14:35:08 +0530626func (o *Onu) publishOmciEvent(msg OmciMessage) {
627 if olt.PublishEvents {
628 _, _, msgType, _, _, _, err := omcisim.ParsePkt(HexDecode(msg.omciMsg.Pkt))
629 if err != nil {
630 log.Errorf("error in getting msgType %v", err)
631 return
632 }
633 if msgType == omcisim.MibUpload {
634 o.seqNumber = 0
635 publishEvent("MIB-upload-received", int32(o.PonPortID), int32(o.ID), common.OnuSnToString(o.SerialNumber))
636 } else if msgType == omcisim.MibUploadNext {
637 o.seqNumber++
638 if o.seqNumber > 290 {
639 publishEvent("MIB-upload-done", int32(o.PonPortID), int32(o.ID), common.OnuSnToString(o.SerialNumber))
640 }
641 }
642 }
643}
644
Scott Bakerb90c4312020-03-12 21:33:25 -0700645// Create a TestResponse packet and send it
646func (o *Onu) sendTestResult(msg OmciMessage, stream openolt.Openolt_EnableIndicationServer) error {
647 resp, err := omcilib.BuildTestResult(HexDecode(msg.omciMsg.Pkt))
648 if err != nil {
649 return err
650 }
651
652 var omciInd openolt.OmciIndication
653 omciInd.IntfId = o.PonPortID
654 omciInd.OnuId = o.ID
655 omciInd.Pkt = resp
656
657 omci := &openolt.Indication_OmciInd{OmciInd: &omciInd}
658 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
659 onuLogger.WithFields(log.Fields{
660 "IntfId": o.PonPortID,
661 "SerialNumber": o.Sn(),
662 "omciPacket": omciInd.Pkt,
663 "msg": msg,
664 }).Errorf("send TestResult omcisim indication failed: %v", err)
665 return err
666 }
667 onuLogger.WithFields(log.Fields{
668 "IntfId": o.PonPortID,
669 "SerialNumber": o.Sn(),
670 "omciPacket": omciInd.Pkt,
671 }).Tracef("Sent TestResult OMCI message")
672
673 return nil
674}
675
William Kurkian0418bc82019-11-06 12:16:24 -0500676func (o *Onu) handleOmciMessage(msg OmciMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700677
678 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700679 "IntfId": o.PonPortID,
Matteo Scandolo27428702019-10-11 16:21:16 -0700680 "SerialNumber": o.Sn(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700681 "omciPacket": msg.omciMsg.Pkt,
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700682 }).Tracef("Received OMCI message")
683
Pragya Arya324337e2020-02-20 14:35:08 +0530684 o.publishOmciEvent(msg)
685
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700686 var omciInd openolt.OmciIndication
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700687 respPkt, err := omcisim.OmciSim(o.PonPortID, o.ID, HexDecode(msg.omciMsg.Pkt))
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700688 if err != nil {
Matteo Scandolo27428702019-10-11 16:21:16 -0700689 onuLogger.WithFields(log.Fields{
690 "IntfId": o.PonPortID,
691 "SerialNumber": o.Sn(),
692 "omciPacket": omciInd.Pkt,
693 "msg": msg,
694 }).Errorf("Error handling OMCI message %v", msg)
695 return
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700696 }
697
698 omciInd.IntfId = o.PonPortID
699 omciInd.OnuId = o.ID
700 omciInd.Pkt = respPkt
701
702 omci := &openolt.Indication_OmciInd{OmciInd: &omciInd}
703 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
Matteo Scandolo27428702019-10-11 16:21:16 -0700704 onuLogger.WithFields(log.Fields{
705 "IntfId": o.PonPortID,
706 "SerialNumber": o.Sn(),
707 "omciPacket": omciInd.Pkt,
708 "msg": msg,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700709 }).Errorf("send omcisim indication failed: %v", err)
Matteo Scandolo27428702019-10-11 16:21:16 -0700710 return
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700711 }
712 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700713 "IntfId": o.PonPortID,
Matteo Scandolo27428702019-10-11 16:21:16 -0700714 "SerialNumber": o.Sn(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700715 "omciPacket": omciInd.Pkt,
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700716 }).Tracef("Sent OMCI message")
Scott Bakerb90c4312020-03-12 21:33:25 -0700717
718 // Test message is special, it requires sending two packets:
719 // first packet: TestResponse, says whether test was started successully, handled by omci-sim
720 // second packet, TestResult, reports the result of running the self-test
721 // TestResult can come some time after a TestResponse
722 // TODO: Implement some delay between the TestResponse and the TestResult
723 isTest, err := omcilib.IsTestRequest(HexDecode(msg.omciMsg.Pkt))
724 if (err == nil) && (isTest) {
725 o.sendTestResult(msg, stream)
726 }
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700727}
728
Matteo Scandolo27428702019-10-11 16:21:16 -0700729func (o *Onu) storePortNumber(portNo uint32) {
Matteo Scandolo813402b2019-10-23 19:24:52 -0700730 // NOTE this needed only as long as we don't support multiple UNIs
Matteo Scandolo27428702019-10-11 16:21:16 -0700731 // we need to add support for multiple UNIs
732 // the action plan is:
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700733 // - refactor the omcisim-sim library to use https://github.com/cboling/omci instead of canned messages
Matteo Scandolo27428702019-10-11 16:21:16 -0700734 // - change the library so that it reports a single UNI and remove this workaroung
735 // - add support for multiple UNIs in BBSim
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700736 if o.PortNo == 0 || portNo < o.PortNo {
Matteo Scandolo813402b2019-10-23 19:24:52 -0700737 onuLogger.WithFields(log.Fields{
738 "IntfId": o.PonPortID,
739 "OnuId": o.ID,
740 "SerialNumber": o.Sn(),
741 "OnuPortNo": o.PortNo,
742 "FlowPortNo": portNo,
743 }).Debug("Storing ONU portNo")
Matteo Scandolo27428702019-10-11 16:21:16 -0700744 o.PortNo = portNo
745 }
746}
747
William Kurkian0418bc82019-11-06 12:16:24 -0500748func (o *Onu) SetID(id uint32) {
Matteo Scandolo583f17d2020-02-13 10:35:17 -0800749 onuLogger.WithFields(log.Fields{
750 "IntfId": o.PonPortID,
751 "OnuId": id,
752 "SerialNumber": o.Sn(),
753 }).Debug("Storing OnuId ")
William Kurkian0418bc82019-11-06 12:16:24 -0500754 o.ID = id
755}
756
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700757func (o *Onu) handleFlowAdd(msg OnuFlowUpdateMessage) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700758 onuLogger.WithFields(log.Fields{
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800759 "DstPort": msg.Flow.Classifier.DstPort,
760 "EthType": fmt.Sprintf("%x", msg.Flow.Classifier.EthType),
761 "FlowId": msg.Flow.FlowId,
762 "FlowType": msg.Flow.FlowType,
763 "GemportId": msg.Flow.GemportId,
764 "InnerVlan": msg.Flow.Classifier.IVid,
765 "IntfId": msg.Flow.AccessIntfId,
766 "IpProto": msg.Flow.Classifier.IpProto,
767 "OnuId": msg.Flow.OnuId,
768 "OnuSn": o.Sn(),
769 "OuterVlan": msg.Flow.Classifier.OVid,
770 "PortNo": msg.Flow.PortNo,
771 "SrcPort": msg.Flow.Classifier.SrcPort,
772 "UniID": msg.Flow.UniId,
Matteo Scandolo3ac52792020-03-05 14:21:08 -0800773 "ClassifierOPbits": msg.Flow.Classifier.OPbits,
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700774 }).Debug("ONU receives FlowAdd")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700775
Matteo Scandolo813402b2019-10-23 19:24:52 -0700776 if msg.Flow.UniId != 0 {
777 // as of now BBSim only support a single UNI, so ignore everything that is not targeted to it
778 onuLogger.WithFields(log.Fields{
779 "IntfId": o.PonPortID,
780 "OnuId": o.ID,
781 "SerialNumber": o.Sn(),
782 }).Debug("Ignoring flow as it's not for the first UNI")
783 return
784 }
785
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700786 o.FlowIds = append(o.FlowIds, msg.Flow.FlowId)
787
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700788 if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeEAPOL) && msg.Flow.Classifier.OVid == 4091 {
Matteo Scandolo27428702019-10-11 16:21:16 -0700789 // NOTE storing the PortNO, it's needed when sending PacketIndications
Matteo Scandolo813402b2019-10-23 19:24:52 -0700790 o.storePortNumber(uint32(msg.Flow.PortNo))
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800791 o.EapolFlowReceived = true
792 // if authentication is not enabled, do nothing
793 if o.Auth {
794 // NOTE if we receive the EAPOL flows but we don't have GemPorts
795 // wait for it before starting auth
796 if !o.GemPortAdded {
797 // wait for Gem and then start auth
798 go func() {
799 for v := range o.GetGemPortChan() {
800 if v == true {
801 if err := o.InternalState.Event("start_auth"); err != nil {
802 onuLogger.Warnf("Can't go to auth_started: %v", err)
803 }
804 }
805 }
806 onuLogger.Trace("GemPortChannel closed")
807 }()
Matteo Scandoloc1147092019-10-29 09:38:33 -0700808 } else {
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800809 // start the EAPOL state machine
810 if err := o.InternalState.Event("start_auth"); err != nil {
811 onuLogger.Warnf("Can't go to auth_started: %v", err)
812 }
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700813 }
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800814 } else {
815 onuLogger.WithFields(log.Fields{
816 "IntfId": o.PonPortID,
817 "OnuId": o.ID,
818 "SerialNumber": o.Sn(),
819 }).Warn("Not starting authentication as Auth bit is not set in CLI parameters")
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700820 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700821 } else if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeIPv4) &&
822 msg.Flow.Classifier.SrcPort == uint32(68) &&
Matteo Scandolo3ac52792020-03-05 14:21:08 -0800823 msg.Flow.Classifier.DstPort == uint32(67) &&
Matteo Scandolod74abba2020-04-16 16:36:44 -0700824 (msg.Flow.Classifier.OPbits == 0 || msg.Flow.Classifier.OPbits == 255) {
Matteo Scandolo99f18462019-10-28 14:14:28 -0700825
Matteo Scandoloc1147092019-10-29 09:38:33 -0700826 if o.Dhcp == true {
Matteo Scandolod74abba2020-04-16 16:36:44 -0700827 if o.DhcpFlowReceived == false {
828 // keep track that we received the DHCP Flows
829 // so that we can transition the state to dhcp_started
830 // this is needed as a check in case someone trigger DHCP from the CLI
831 o.DhcpFlowReceived = true
832
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800833 if !o.GemPortAdded {
834 // wait for Gem and then start DHCP
835 go func() {
836 for v := range o.GetGemPortChan() {
837 if v == true {
838 if err := o.InternalState.Event("start_dhcp"); err != nil {
839 log.Errorf("Can't go to dhcp_started: %v", err)
840 }
841 }
842 }
843 }()
844 } else {
845 // start the DHCP state machine
846 if err := o.InternalState.Event("start_dhcp"); err != nil {
847 log.Errorf("Can't go to dhcp_started: %v", err)
848 }
Matteo Scandolod74abba2020-04-16 16:36:44 -0700849 }
850 } else {
851 onuLogger.WithFields(log.Fields{
852 "IntfId": o.PonPortID,
853 "OnuId": o.ID,
854 "SerialNumber": o.Sn(),
855 "DhcpFlowReceived": o.DhcpFlowReceived,
856 }).Warn("DHCP already started")
Matteo Scandoloc1147092019-10-29 09:38:33 -0700857 }
858 } else {
859 onuLogger.WithFields(log.Fields{
860 "IntfId": o.PonPortID,
861 "OnuId": o.ID,
862 "SerialNumber": o.Sn(),
863 }).Warn("Not starting DHCP as Dhcp bit is not set in CLI parameters")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700864 }
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700865 }
866}
867
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700868func (o *Onu) handleFlowRemove(msg OnuFlowUpdateMessage) {
869 onuLogger.WithFields(log.Fields{
870 "IntfId": o.PonPortID,
871 "OnuId": o.ID,
872 "SerialNumber": o.Sn(),
873 "FlowId": msg.Flow.FlowId,
874 "FlowType": msg.Flow.FlowType,
875 }).Debug("ONU receives FlowRemove")
876
877 for idx, flow := range o.FlowIds {
878 // If the gemport is found, delete it from local cache.
879 if flow == msg.Flow.FlowId {
880 o.FlowIds = append(o.FlowIds[:idx], o.FlowIds[idx+1:]...)
881 break
882 }
883 }
884
885 if len(o.FlowIds) == 0 {
886 onuLogger.WithFields(log.Fields{
887 "IntfId": o.PonPortID,
888 "OnuId": o.ID,
889 "SerialNumber": o.Sn(),
890 }).Info("Resetting GemPort")
891 o.GemPortAdded = false
892
893 // TODO ideally we should keep track of the flow type (and not only the ID)
894 // so that we can properly set these two flag when the flow is removed
895 o.EapolFlowReceived = false
896 o.DhcpFlowReceived = false
897 }
898}
899
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700900// HexDecode converts the hex encoding to binary
901func HexDecode(pkt []byte) []byte {
902 p := make([]byte, len(pkt)/2)
903 for i, j := 0, 0; i < len(pkt); i, j = i+2, j+1 {
904 // Go figure this ;)
905 u := (pkt[i] & 15) + (pkt[i]>>6)*9
906 l := (pkt[i+1] & 15) + (pkt[i+1]>>6)*9
907 p[j] = u<<4 + l
908 }
909 onuLogger.Tracef("Omci decoded: %x.", p)
910 return p
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700911}
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700912
913// BBR methods
914
915func sendOmciMsg(pktBytes []byte, intfId uint32, onuId uint32, sn *openolt.SerialNumber, msgType string, client openolt.OpenoltClient) {
916 omciMsg := openolt.OmciMsg{
917 IntfId: intfId,
918 OnuId: onuId,
919 Pkt: pktBytes,
920 }
921
922 if _, err := client.OmciMsgOut(context.Background(), &omciMsg); err != nil {
923 log.WithFields(log.Fields{
924 "IntfId": intfId,
925 "OnuId": onuId,
926 "SerialNumber": common.OnuSnToString(sn),
927 "Pkt": omciMsg.Pkt,
928 }).Fatalf("Failed to send MIB Reset")
929 }
930 log.WithFields(log.Fields{
931 "IntfId": intfId,
932 "OnuId": onuId,
933 "SerialNumber": common.OnuSnToString(sn),
934 "Pkt": omciMsg.Pkt,
935 }).Tracef("Sent OMCI message %s", msgType)
936}
937
938func (onu *Onu) getNextTid(highPriority ...bool) uint16 {
939 var next uint16
940 if len(highPriority) > 0 && highPriority[0] {
941 next = onu.hpTid
942 onu.hpTid += 1
943 if onu.hpTid < 0x8000 {
944 onu.hpTid = 0x8000
945 }
946 } else {
947 next = onu.tid
948 onu.tid += 1
949 if onu.tid >= 0x8000 {
950 onu.tid = 1
951 }
952 }
953 return next
954}
955
956// TODO move this method in responders/omcisim
957func (o *Onu) StartOmci(client openolt.OpenoltClient) {
958 mibReset, _ := omcilib.CreateMibResetRequest(o.getNextTid(false))
959 sendOmciMsg(mibReset, o.PonPortID, o.ID, o.SerialNumber, "mibReset", client)
960}
961
962func (o *Onu) handleOmci(msg OmciIndicationMessage, client openolt.OpenoltClient) {
963 msgType, packet := omcilib.DecodeOmci(msg.OmciInd.Pkt)
964
965 log.WithFields(log.Fields{
966 "IntfId": msg.OmciInd.IntfId,
967 "OnuId": msg.OmciInd.OnuId,
968 "OnuSn": common.OnuSnToString(o.SerialNumber),
969 "Pkt": msg.OmciInd.Pkt,
970 "msgType": msgType,
Anand S Katti09541352020-01-29 15:54:01 +0530971 }).Trace("ONU Receives OMCI Msg")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700972 switch msgType {
973 default:
Matteo Scandolo813402b2019-10-23 19:24:52 -0700974 log.WithFields(log.Fields{
975 "IntfId": msg.OmciInd.IntfId,
976 "OnuId": msg.OmciInd.OnuId,
977 "OnuSn": common.OnuSnToString(o.SerialNumber),
978 "Pkt": msg.OmciInd.Pkt,
979 "msgType": msgType,
980 }).Fatalf("unexpected frame: %v", packet)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700981 case omci.MibResetResponseType:
982 mibUpload, _ := omcilib.CreateMibUploadRequest(o.getNextTid(false))
983 sendOmciMsg(mibUpload, o.PonPortID, o.ID, o.SerialNumber, "mibUpload", client)
984 case omci.MibUploadResponseType:
985 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
986 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
987 case omci.MibUploadNextResponseType:
988 o.seqNumber++
989
990 if o.seqNumber > 290 {
991 // NOTE we are done with the MIB Upload (290 is the number of messages the omci-sim library will respond to)
992 galEnet, _ := omcilib.CreateGalEnetRequest(o.getNextTid(false))
993 sendOmciMsg(galEnet, o.PonPortID, o.ID, o.SerialNumber, "CreateGalEnetRequest", client)
994 } else {
995 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
996 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
997 }
998 case omci.CreateResponseType:
999 // NOTE Creating a GemPort,
1000 // BBsim actually doesn't care about the values, so we can do we want with the parameters
1001 // In the same way we can create a GemPort even without setting up UNIs/TConts/...
1002 // but we need the GemPort to trigger the state change
1003
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001004 if !o.GemPortAdded {
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001005 // NOTE this sends a CreateRequestType and BBSim replies with a CreateResponseType
1006 // thus we send this request only once
1007 gemReq, _ := omcilib.CreateGemPortRequest(o.getNextTid(false))
1008 sendOmciMsg(gemReq, o.PonPortID, o.ID, o.SerialNumber, "CreateGemPortRequest", client)
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -07001009 o.GemPortAdded = true
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001010 } else {
1011 if err := o.InternalState.Event("send_eapol_flow"); err != nil {
1012 onuLogger.WithFields(log.Fields{
1013 "OnuId": o.ID,
1014 "IntfId": o.PonPortID,
1015 "OnuSn": o.Sn(),
1016 }).Errorf("Error while transitioning ONU State %v", err)
1017 }
1018 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001019 }
1020}
1021
1022func (o *Onu) sendEapolFlow(client openolt.OpenoltClient) {
1023
1024 classifierProto := openolt.Classifier{
1025 EthType: uint32(layers.EthernetTypeEAPOL),
1026 OVid: 4091,
1027 }
1028
1029 actionProto := openolt.Action{}
1030
1031 downstreamFlow := openolt.Flow{
1032 AccessIntfId: int32(o.PonPortID),
1033 OnuId: int32(o.ID),
Matteo Scandolo813402b2019-10-23 19:24:52 -07001034 UniId: int32(0), // NOTE do not hardcode this, we need to support multiple UNIs
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001035 FlowId: uint32(o.ID),
1036 FlowType: "downstream",
1037 AllocId: int32(0),
1038 NetworkIntfId: int32(0),
1039 GemportId: int32(1), // FIXME use the same value as CreateGemPortRequest PortID, do not hardcode
1040 Classifier: &classifierProto,
1041 Action: &actionProto,
1042 Priority: int32(100),
1043 Cookie: uint64(o.ID),
1044 PortNo: uint32(o.ID), // NOTE we are using this to map an incoming packetIndication to an ONU
1045 }
1046
1047 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
1048 log.WithFields(log.Fields{
1049 "IntfId": o.PonPortID,
1050 "OnuId": o.ID,
1051 "FlowId": downstreamFlow.FlowId,
1052 "PortNo": downstreamFlow.PortNo,
1053 "SerialNumber": common.OnuSnToString(o.SerialNumber),
Matteo Scandolob0e3e622020-04-23 17:00:29 -07001054 }).Fatalf("Failed to add EAPOL Flow")
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001055 }
1056 log.WithFields(log.Fields{
1057 "IntfId": o.PonPortID,
1058 "OnuId": o.ID,
1059 "FlowId": downstreamFlow.FlowId,
1060 "PortNo": downstreamFlow.PortNo,
1061 "SerialNumber": common.OnuSnToString(o.SerialNumber),
1062 }).Info("Sent EAPOL Flow")
1063}
1064
1065func (o *Onu) sendDhcpFlow(client openolt.OpenoltClient) {
1066 classifierProto := openolt.Classifier{
1067 EthType: uint32(layers.EthernetTypeIPv4),
1068 SrcPort: uint32(68),
1069 DstPort: uint32(67),
1070 }
1071
1072 actionProto := openolt.Action{}
1073
1074 downstreamFlow := openolt.Flow{
1075 AccessIntfId: int32(o.PonPortID),
1076 OnuId: int32(o.ID),
Matteo Scandolo813402b2019-10-23 19:24:52 -07001077 UniId: int32(0), // FIXME do not hardcode this
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001078 FlowId: uint32(o.ID),
1079 FlowType: "downstream",
1080 AllocId: int32(0),
1081 NetworkIntfId: int32(0),
1082 GemportId: int32(1), // FIXME use the same value as CreateGemPortRequest PortID, do not hardcode
1083 Classifier: &classifierProto,
1084 Action: &actionProto,
1085 Priority: int32(100),
1086 Cookie: uint64(o.ID),
1087 PortNo: uint32(o.ID), // NOTE we are using this to map an incoming packetIndication to an ONU
1088 }
1089
1090 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
1091 log.WithFields(log.Fields{
1092 "IntfId": o.PonPortID,
1093 "OnuId": o.ID,
1094 "FlowId": downstreamFlow.FlowId,
1095 "PortNo": downstreamFlow.PortNo,
1096 "SerialNumber": common.OnuSnToString(o.SerialNumber),
1097 }).Fatalf("Failed to send DHCP Flow")
1098 }
1099 log.WithFields(log.Fields{
1100 "IntfId": o.PonPortID,
1101 "OnuId": o.ID,
1102 "FlowId": downstreamFlow.FlowId,
1103 "PortNo": downstreamFlow.PortNo,
1104 "SerialNumber": common.OnuSnToString(o.SerialNumber),
1105 }).Info("Sent DHCP Flow")
1106}
Pragya Arya8bdb4532020-03-02 17:08:09 +05301107
1108// DeleteFlow method search and delete flowKey from the onu flows slice
1109func (onu *Onu) DeleteFlow(key FlowKey) {
1110 for pos, flowKey := range onu.Flows {
1111 if flowKey == key {
1112 // delete the flowKey by shifting all flowKeys by one
1113 onu.Flows = append(onu.Flows[:pos], onu.Flows[pos+1:]...)
1114 t := make([]FlowKey, len(onu.Flows))
1115 copy(t, onu.Flows)
1116 onu.Flows = t
1117 break
1118 }
1119 }
1120}