blob: 43759c9f1b6adfba428982ee9fc76da9eaee8b78 [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 Scandolod7cc6d32020-02-26 16:51:12 -0800129 {Name: "enable", Src: []string{"discovered"}, 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)
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800166
167 if err := o.OperState.Event("enable"); err != nil {
168 onuLogger.WithFields(log.Fields{
169 "OnuId": o.ID,
170 "IntfId": o.PonPortID,
171 "OnuSn": o.Sn(),
172 }).Errorf("Cannot change ONU OperState to up: %s", err.Error())
173 }
174
Pragya Arya1cbefa42020-01-13 12:15:29 +0530175 if !isMock {
176 // start ProcessOnuMessages Go routine
177 go o.ProcessOnuMessages(olt.enableContext, *olt.OpenoltStream, nil)
178 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100179 },
180 "enter_discovered": func(e *fsm.Event) {
181 msg := Message{
182 Type: OnuDiscIndication,
183 Data: OnuDiscIndicationMessage{
184 Onu: &o,
185 OperState: UP,
186 },
187 }
188 o.Channel <- msg
189 },
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700190 "enter_enabled": func(event *fsm.Event) {
191 msg := Message{
192 Type: OnuIndication,
193 Data: OnuIndicationMessage{
194 OnuSN: o.SerialNumber,
195 PonPortID: o.PonPortID,
196 OperState: UP,
197 },
198 }
199 o.Channel <- msg
200 },
201 "enter_disabled": func(event *fsm.Event) {
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800202 if err := o.OperState.Event("disable"); err != nil {
203 onuLogger.WithFields(log.Fields{
204 "OnuId": o.ID,
205 "IntfId": o.PonPortID,
206 "OnuSn": o.Sn(),
207 }).Errorf("Cannot change ONU OperState to down: %s", err.Error())
208 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700209 msg := Message{
210 Type: OnuIndication,
211 Data: OnuIndicationMessage{
212 OnuSN: o.SerialNumber,
213 PonPortID: o.PonPortID,
214 OperState: DOWN,
215 },
216 }
217 o.Channel <- msg
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100218 // terminate the ONU's ProcessOnuMessages Go routine
219 close(o.Channel)
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700220 },
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700221 "enter_auth_started": func(e *fsm.Event) {
222 o.logStateChange(e.Src, e.Dst)
223 msg := Message{
224 Type: StartEAPOL,
225 Data: PacketMessage{
226 PonPortID: o.PonPortID,
227 OnuID: o.ID,
228 },
229 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700230 o.Channel <- msg
Matteo Scandolo4747d292019-08-05 11:50:18 -0700231 },
Pragya Arya324337e2020-02-20 14:35:08 +0530232 "enter_eap_response_success_received": func(e *fsm.Event) {
233 publishEvent("ONU-authentication-done", int32(o.PonPortID), int32(o.ID), o.Sn())
234 },
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700235 "enter_auth_failed": func(e *fsm.Event) {
236 onuLogger.WithFields(log.Fields{
237 "OnuId": o.ID,
238 "IntfId": o.PonPortID,
239 "OnuSn": o.Sn(),
240 }).Errorf("ONU failed to authenticate!")
241 },
Matteo Scandolo99f18462019-10-28 14:14:28 -0700242 "before_start_dhcp": func(e *fsm.Event) {
243 if o.DhcpFlowReceived == false {
244 e.Cancel(errors.New("cannot-go-to-dhcp-started-as-dhcp-flow-is-missing"))
245 }
246 },
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700247 "enter_dhcp_started": func(e *fsm.Event) {
248 msg := Message{
249 Type: StartDHCP,
250 Data: PacketMessage{
251 PonPortID: o.PonPortID,
252 OnuID: o.ID,
253 },
254 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700255 o.Channel <- msg
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700256 },
Pragya Arya324337e2020-02-20 14:35:08 +0530257 "enter_dhcp_ack_received": func(e *fsm.Event) {
258 publishEvent("ONU-DHCP-ACK-received", int32(o.PonPortID), int32(o.ID), o.Sn())
259 },
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700260 "enter_dhcp_failed": func(e *fsm.Event) {
261 onuLogger.WithFields(log.Fields{
262 "OnuId": o.ID,
263 "IntfId": o.PonPortID,
264 "OnuSn": o.Sn(),
265 }).Errorf("ONU failed to DHCP!")
266 },
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700267 "enter_eapol_flow_sent": func(e *fsm.Event) {
268 msg := Message{
269 Type: SendEapolFlow,
270 }
271 o.Channel <- msg
272 },
273 "enter_dhcp_flow_sent": func(e *fsm.Event) {
274 msg := Message{
275 Type: SendDhcpFlow,
276 }
277 o.Channel <- msg
278 },
Arjun E K57a7fcb2020-01-30 06:44:45 +0000279 "igmp_join_start": func(e *fsm.Event) {
280 msg := Message{
281 Type: IGMPMembershipReportV2,
282 }
283 o.Channel <- msg
284 },
285 "igmp_leave": func(e *fsm.Event) {
286 msg := Message{
287 Type: IGMPLeaveGroup}
288 o.Channel <- msg
289 },
Anand S Katti09541352020-01-29 15:54:01 +0530290 "igmp_join_startv3": func(e *fsm.Event) {
291 msg := Message{
292 Type: IGMPMembershipReportV3,
293 }
294 o.Channel <- msg
295 },
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700296 },
297 )
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100298
Matteo Scandolo27428702019-10-11 16:21:16 -0700299 return &o
Matteo Scandolo4747d292019-08-05 11:50:18 -0700300}
301
William Kurkian0418bc82019-11-06 12:16:24 -0500302func (o *Onu) logStateChange(src string, dst string) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700303 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700304 "OnuId": o.ID,
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700305 "IntfId": o.PonPortID,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700306 "OnuSn": o.Sn(),
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700307 }).Debugf("Changing ONU InternalState from %s to %s", src, dst)
308}
309
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100310// ProcessOnuMessages starts indication channel for each ONU
David Bainbridge103cf022019-12-16 20:11:35 +0000311func (o *Onu) ProcessOnuMessages(ctx context.Context, stream openolt.Openolt_EnableIndicationServer, client openolt.OpenoltClient) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700312 onuLogger.WithFields(log.Fields{
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100313 "onuID": o.ID,
314 "onuSN": o.Sn(),
315 "ponPort": o.PonPortID,
316 }).Debug("Starting ONU Indication Channel")
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700317
David Bainbridge103cf022019-12-16 20:11:35 +0000318loop:
319 for {
320 select {
321 case <-ctx.Done():
322 onuLogger.WithFields(log.Fields{
323 "onuID": o.ID,
324 "onuSN": o.Sn(),
325 }).Tracef("ONU message handling canceled via context")
326 break loop
327 case message, ok := <-o.Channel:
328 if !ok || ctx.Err() != nil {
329 onuLogger.WithFields(log.Fields{
330 "onuID": o.ID,
331 "onuSN": o.Sn(),
332 }).Tracef("ONU message handling canceled via channel close")
333 break loop
Matteo Scandolo075b1892019-10-07 12:11:07 -0700334 }
David Bainbridge103cf022019-12-16 20:11:35 +0000335 onuLogger.WithFields(log.Fields{
336 "onuID": o.ID,
337 "onuSN": o.Sn(),
338 "messageType": message.Type,
339 }).Tracef("Received message on ONU Channel")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700340
David Bainbridge103cf022019-12-16 20:11:35 +0000341 switch message.Type {
342 case OnuDiscIndication:
343 msg, _ := message.Data.(OnuDiscIndicationMessage)
344 // 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 +0530345 time.Sleep(o.DiscoveryDelay)
David Bainbridge103cf022019-12-16 20:11:35 +0000346 o.sendOnuDiscIndication(msg, stream)
347 case OnuIndication:
348 msg, _ := message.Data.(OnuIndicationMessage)
349 o.sendOnuIndication(msg, stream)
350 case OMCI:
351 msg, _ := message.Data.(OmciMessage)
352 o.handleOmciMessage(msg, stream)
353 case FlowUpdate:
354 msg, _ := message.Data.(OnuFlowUpdateMessage)
355 o.handleFlowUpdate(msg)
356 case StartEAPOL:
357 log.Infof("Receive StartEAPOL message on ONU Channel")
358 eapol.SendEapStart(o.ID, o.PonPortID, o.Sn(), o.PortNo, o.HwAddress, o.InternalState, stream)
359 case StartDHCP:
360 log.Infof("Receive StartDHCP message on ONU Channel")
361 // FIXME use id, ponId as SendEapStart
362 dhcp.SendDHCPDiscovery(o.PonPortID, o.ID, o.Sn(), o.PortNo, o.InternalState, o.HwAddress, o.CTag, stream)
363 case OnuPacketOut:
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700364
David Bainbridge103cf022019-12-16 20:11:35 +0000365 msg, _ := message.Data.(OnuPacketMessage)
366
367 log.WithFields(log.Fields{
368 "IntfId": msg.IntfId,
369 "OnuId": msg.OnuId,
370 "pktType": msg.Type,
371 }).Trace("Received OnuPacketOut Message")
372
373 if msg.Type == packetHandlers.EAPOL {
374 eapol.HandleNextPacket(msg.OnuId, msg.IntfId, o.Sn(), o.PortNo, o.InternalState, msg.Packet, stream, client)
375 } else if msg.Type == packetHandlers.DHCP {
376 // NOTE here we receive packets going from the DHCP Server to the ONU
377 // for now we expect them to be double-tagged, but ideally the should be single tagged
378 dhcp.HandleNextPacket(o.ID, o.PonPortID, o.Sn(), o.PortNo, o.HwAddress, o.CTag, o.InternalState, msg.Packet, stream)
379 }
380 case OnuPacketIn:
381 // NOTE we only receive BBR packets here.
382 // Eapol.HandleNextPacket can handle both BBSim and BBr cases so the call is the same
383 // in the DHCP case VOLTHA only act as a proxy, the behaviour is completely different thus we have a dhcp.HandleNextBbrPacket
384 msg, _ := message.Data.(OnuPacketMessage)
385
386 log.WithFields(log.Fields{
387 "IntfId": msg.IntfId,
388 "OnuId": msg.OnuId,
389 "pktType": msg.Type,
390 }).Trace("Received OnuPacketIn Message")
391
392 if msg.Type == packetHandlers.EAPOL {
393 eapol.HandleNextPacket(msg.OnuId, msg.IntfId, o.Sn(), o.PortNo, o.InternalState, msg.Packet, stream, client)
394 } else if msg.Type == packetHandlers.DHCP {
395 dhcp.HandleNextBbrPacket(o.ID, o.PonPortID, o.Sn(), o.STag, o.HwAddress, o.DoneChannel, msg.Packet, client)
396 }
David Bainbridge103cf022019-12-16 20:11:35 +0000397 case OmciIndication:
398 msg, _ := message.Data.(OmciIndicationMessage)
399 o.handleOmci(msg, client)
400 case SendEapolFlow:
401 o.sendEapolFlow(client)
402 case SendDhcpFlow:
403 o.sendDhcpFlow(client)
Arjun E K57a7fcb2020-01-30 06:44:45 +0000404 case IGMPMembershipReportV2:
405 log.Infof("Recieved IGMPMembershipReportV2 message on ONU channel")
406 igmp.SendIGMPMembershipReportV2(o.PonPortID, o.ID, o.Sn(), o.PortNo, o.HwAddress, stream)
407 case IGMPLeaveGroup:
408 log.Infof("Recieved IGMPLeaveGroupV2 message on ONU channel")
409 igmp.SendIGMPLeaveGroupV2(o.PonPortID, o.ID, o.Sn(), o.PortNo, o.HwAddress, stream)
Anand S Katti09541352020-01-29 15:54:01 +0530410 case IGMPMembershipReportV3:
411 log.Infof("Recieved IGMPMembershipReportV3 message on ONU channel")
412 igmp.SendIGMPMembershipReportV3(o.PonPortID, o.ID, o.Sn(), o.PortNo, o.HwAddress, stream)
David Bainbridge103cf022019-12-16 20:11:35 +0000413 default:
414 onuLogger.Warnf("Received unknown message data %v for type %v in OLT Channel", message.Data, message.Type)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700415 }
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700416 }
417 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100418 onuLogger.WithFields(log.Fields{
419 "onuID": o.ID,
420 "onuSN": o.Sn(),
421 }).Debug("Stopped handling ONU Indication Channel")
Matteo Scandolo4747d292019-08-05 11:50:18 -0700422}
423
Matteo Scandolodf3f85d2020-01-15 12:50:48 -0800424func (o *Onu) processOmciMessage(message omcisim.OmciChMessage, stream openolt.Openolt_EnableIndicationServer) {
William Kurkian9dadc5b2019-10-22 13:51:57 -0400425 switch message.Type {
Matteo Scandolodf3f85d2020-01-15 12:50:48 -0800426 case omcisim.UniLinkUp, omcisim.UniLinkDown:
427 onuLogger.WithFields(log.Fields{
428 "OnuId": message.Data.OnuId,
429 "IntfId": message.Data.IntfId,
430 "Type": message.Type,
431 }).Infof("UNI Link Alarm")
432 // TODO send to OLT
433
434 omciInd := openolt.OmciIndication{
435 IntfId: message.Data.IntfId,
436 OnuId: message.Data.OnuId,
437 Pkt: message.Packet,
438 }
439
440 omci := &openolt.Indication_OmciInd{OmciInd: &omciInd}
441 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
442 onuLogger.WithFields(log.Fields{
443 "IntfId": o.PonPortID,
444 "SerialNumber": o.Sn(),
445 "Type": message.Type,
446 "omciPacket": omciInd.Pkt,
447 }).Errorf("Failed to send UNI Link Alarm: %v", err)
448 return
449 }
450
451 onuLogger.WithFields(log.Fields{
452 "IntfId": o.PonPortID,
453 "SerialNumber": o.Sn(),
454 "Type": message.Type,
455 "omciPacket": omciInd.Pkt,
456 }).Info("UNI Link alarm sent")
457
William Kurkian9dadc5b2019-10-22 13:51:57 -0400458 case omcisim.GemPortAdded:
459 log.WithFields(log.Fields{
460 "OnuId": message.Data.OnuId,
461 "IntfId": message.Data.IntfId,
462 }).Infof("GemPort Added")
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700463
William Kurkian9dadc5b2019-10-22 13:51:57 -0400464 // NOTE if we receive the GemPort but we don't have EAPOL flows
465 // go an intermediate state, otherwise start auth
466 if o.InternalState.Is("enabled") {
467 if err := o.InternalState.Event("add_gem_port"); err != nil {
468 log.Errorf("Can't go to gem_port_added: %v", err)
469 }
470 } else if o.InternalState.Is("eapol_flow_received") {
Matteo Scandolo3c656a12019-12-10 09:54:51 -0800471 if o.Auth == true {
472 if err := o.InternalState.Event("start_auth"); err != nil {
473 log.Warnf("Can't go to auth_started: %v", err)
474 }
475 } else {
476 onuLogger.WithFields(log.Fields{
477 "IntfId": o.PonPortID,
478 "OnuId": o.ID,
479 "SerialNumber": o.Sn(),
480 }).Warn("Not starting authentication as Auth bit is not set in CLI parameters")
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700481 }
482 }
483 }
484}
485
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100486func (o Onu) NewSN(oltid int, intfid uint32, onuid uint32) *openolt.SerialNumber {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700487
488 sn := new(openolt.SerialNumber)
489
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700490 //sn = new(openolt.SerialNumber)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700491 sn.VendorId = []byte("BBSM")
492 sn.VendorSpecific = []byte{0, byte(oltid % 256), byte(intfid), byte(onuid)}
493
494 return sn
495}
496
William Kurkian0418bc82019-11-06 12:16:24 -0500497func (o *Onu) sendOnuDiscIndication(msg OnuDiscIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700498 discoverData := &openolt.Indication_OnuDiscInd{OnuDiscInd: &openolt.OnuDiscIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700499 IntfId: msg.Onu.PonPortID,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700500 SerialNumber: msg.Onu.SerialNumber,
501 }}
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700502
Matteo Scandolo4747d292019-08-05 11:50:18 -0700503 if err := stream.Send(&openolt.Indication{Data: discoverData}); err != nil {
Matteo Scandolo11006992019-08-28 11:29:46 -0700504 log.Errorf("Failed to send Indication_OnuDiscInd: %v", err)
Matteo Scandolo99f18462019-10-28 14:14:28 -0700505 return
Matteo Scandolo4747d292019-08-05 11:50:18 -0700506 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700507
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700508 onuLogger.WithFields(log.Fields{
Matteo Scandolo4747d292019-08-05 11:50:18 -0700509 "IntfId": msg.Onu.PonPortID,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700510 "OnuSn": msg.Onu.Sn(),
511 "OnuId": o.ID,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700512 }).Debug("Sent Indication_OnuDiscInd")
Pragya Arya324337e2020-02-20 14:35:08 +0530513 publishEvent("ONU-discovery-indication-sent", int32(msg.Onu.PonPortID), int32(o.ID), msg.Onu.Sn())
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800514
515 // after DiscoveryRetryDelay check if the state is the same and in case send a new OnuDiscIndication
516 go func(delay time.Duration) {
Matteo Scandolo569e7172019-12-20 11:51:51 -0800517 time.Sleep(delay)
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800518 if o.InternalState.Current() == "discovered" {
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800519 o.sendOnuDiscIndication(msg, stream)
520 }
521 }(o.DiscoveryRetryDelay)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700522}
523
William Kurkian0418bc82019-11-06 12:16:24 -0500524func (o *Onu) sendOnuIndication(msg OnuIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700525 // NOTE voltha returns an ID, but if we use that ID then it complains:
526 // expected_onu_id: 1, received_onu_id: 1024, event: ONU-id-mismatch, can happen if both voltha and the olt rebooted
527 // so we're using the internal ID that is 1
528 // o.ID = msg.OnuID
Matteo Scandolo4747d292019-08-05 11:50:18 -0700529
530 indData := &openolt.Indication_OnuInd{OnuInd: &openolt.OnuIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700531 IntfId: o.PonPortID,
532 OnuId: o.ID,
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700533 OperState: msg.OperState.String(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700534 AdminState: o.OperState.Current(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700535 SerialNumber: o.SerialNumber,
536 }}
537 if err := stream.Send(&openolt.Indication{Data: indData}); err != nil {
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800538 // NOTE do we need to transition to a broken state?
Matteo Scandolo11006992019-08-28 11:29:46 -0700539 log.Errorf("Failed to send Indication_OnuInd: %v", err)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700540 }
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700541 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700542 "IntfId": o.PonPortID,
543 "OnuId": o.ID,
544 "OperState": msg.OperState.String(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700545 "AdminState": msg.OperState.String(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700546 "OnuSn": o.Sn(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700547 }).Debug("Sent Indication_OnuInd")
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700548
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700549}
550
Pragya Arya324337e2020-02-20 14:35:08 +0530551func (o *Onu) publishOmciEvent(msg OmciMessage) {
552 if olt.PublishEvents {
553 _, _, msgType, _, _, _, err := omcisim.ParsePkt(HexDecode(msg.omciMsg.Pkt))
554 if err != nil {
555 log.Errorf("error in getting msgType %v", err)
556 return
557 }
558 if msgType == omcisim.MibUpload {
559 o.seqNumber = 0
560 publishEvent("MIB-upload-received", int32(o.PonPortID), int32(o.ID), common.OnuSnToString(o.SerialNumber))
561 } else if msgType == omcisim.MibUploadNext {
562 o.seqNumber++
563 if o.seqNumber > 290 {
564 publishEvent("MIB-upload-done", int32(o.PonPortID), int32(o.ID), common.OnuSnToString(o.SerialNumber))
565 }
566 }
567 }
568}
569
William Kurkian0418bc82019-11-06 12:16:24 -0500570func (o *Onu) handleOmciMessage(msg OmciMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700571
572 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700573 "IntfId": o.PonPortID,
Matteo Scandolo27428702019-10-11 16:21:16 -0700574 "SerialNumber": o.Sn(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700575 "omciPacket": msg.omciMsg.Pkt,
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700576 }).Tracef("Received OMCI message")
577
Pragya Arya324337e2020-02-20 14:35:08 +0530578 o.publishOmciEvent(msg)
579
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700580 var omciInd openolt.OmciIndication
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700581 respPkt, err := omcisim.OmciSim(o.PonPortID, o.ID, HexDecode(msg.omciMsg.Pkt))
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700582 if err != nil {
Matteo Scandolo27428702019-10-11 16:21:16 -0700583 onuLogger.WithFields(log.Fields{
584 "IntfId": o.PonPortID,
585 "SerialNumber": o.Sn(),
586 "omciPacket": omciInd.Pkt,
587 "msg": msg,
588 }).Errorf("Error handling OMCI message %v", msg)
589 return
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700590 }
591
592 omciInd.IntfId = o.PonPortID
593 omciInd.OnuId = o.ID
594 omciInd.Pkt = respPkt
595
596 omci := &openolt.Indication_OmciInd{OmciInd: &omciInd}
597 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
Matteo Scandolo27428702019-10-11 16:21:16 -0700598 onuLogger.WithFields(log.Fields{
599 "IntfId": o.PonPortID,
600 "SerialNumber": o.Sn(),
601 "omciPacket": omciInd.Pkt,
602 "msg": msg,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700603 }).Errorf("send omcisim indication failed: %v", err)
Matteo Scandolo27428702019-10-11 16:21:16 -0700604 return
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700605 }
606 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700607 "IntfId": o.PonPortID,
Matteo Scandolo27428702019-10-11 16:21:16 -0700608 "SerialNumber": o.Sn(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700609 "omciPacket": omciInd.Pkt,
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700610 }).Tracef("Sent OMCI message")
611}
612
Matteo Scandolo27428702019-10-11 16:21:16 -0700613func (o *Onu) storePortNumber(portNo uint32) {
Matteo Scandolo813402b2019-10-23 19:24:52 -0700614 // NOTE this needed only as long as we don't support multiple UNIs
Matteo Scandolo27428702019-10-11 16:21:16 -0700615 // we need to add support for multiple UNIs
616 // the action plan is:
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700617 // - refactor the omcisim-sim library to use https://github.com/cboling/omci instead of canned messages
Matteo Scandolo27428702019-10-11 16:21:16 -0700618 // - change the library so that it reports a single UNI and remove this workaroung
619 // - add support for multiple UNIs in BBSim
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700620 if o.PortNo == 0 || portNo < o.PortNo {
Matteo Scandolo813402b2019-10-23 19:24:52 -0700621 onuLogger.WithFields(log.Fields{
622 "IntfId": o.PonPortID,
623 "OnuId": o.ID,
624 "SerialNumber": o.Sn(),
625 "OnuPortNo": o.PortNo,
626 "FlowPortNo": portNo,
627 }).Debug("Storing ONU portNo")
Matteo Scandolo27428702019-10-11 16:21:16 -0700628 o.PortNo = portNo
629 }
630}
631
William Kurkian0418bc82019-11-06 12:16:24 -0500632func (o *Onu) SetID(id uint32) {
Matteo Scandolo583f17d2020-02-13 10:35:17 -0800633 onuLogger.WithFields(log.Fields{
634 "IntfId": o.PonPortID,
635 "OnuId": id,
636 "SerialNumber": o.Sn(),
637 }).Debug("Storing OnuId ")
William Kurkian0418bc82019-11-06 12:16:24 -0500638 o.ID = id
639}
640
Matteo Scandolo813402b2019-10-23 19:24:52 -0700641func (o *Onu) handleFlowUpdate(msg OnuFlowUpdateMessage) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700642 onuLogger.WithFields(log.Fields{
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800643 "DstPort": msg.Flow.Classifier.DstPort,
644 "EthType": fmt.Sprintf("%x", msg.Flow.Classifier.EthType),
645 "FlowId": msg.Flow.FlowId,
646 "FlowType": msg.Flow.FlowType,
647 "GemportId": msg.Flow.GemportId,
648 "InnerVlan": msg.Flow.Classifier.IVid,
649 "IntfId": msg.Flow.AccessIntfId,
650 "IpProto": msg.Flow.Classifier.IpProto,
651 "OnuId": msg.Flow.OnuId,
652 "OnuSn": o.Sn(),
653 "OuterVlan": msg.Flow.Classifier.OVid,
654 "PortNo": msg.Flow.PortNo,
655 "SrcPort": msg.Flow.Classifier.SrcPort,
656 "UniID": msg.Flow.UniId,
Matteo Scandolo3ac52792020-03-05 14:21:08 -0800657 "ClassifierOPbits": msg.Flow.Classifier.OPbits,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700658 }).Debug("ONU receives Flow")
659
Matteo Scandolo813402b2019-10-23 19:24:52 -0700660 if msg.Flow.UniId != 0 {
661 // as of now BBSim only support a single UNI, so ignore everything that is not targeted to it
662 onuLogger.WithFields(log.Fields{
663 "IntfId": o.PonPortID,
664 "OnuId": o.ID,
665 "SerialNumber": o.Sn(),
666 }).Debug("Ignoring flow as it's not for the first UNI")
667 return
668 }
669
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700670 if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeEAPOL) && msg.Flow.Classifier.OVid == 4091 {
Matteo Scandolo27428702019-10-11 16:21:16 -0700671 // NOTE storing the PortNO, it's needed when sending PacketIndications
Matteo Scandolo813402b2019-10-23 19:24:52 -0700672 o.storePortNumber(uint32(msg.Flow.PortNo))
Matteo Scandolo27428702019-10-11 16:21:16 -0700673
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700674 // NOTE if we receive the EAPOL flows but we don't have GemPorts
675 // go an intermediate state, otherwise start auth
676 if o.InternalState.Is("enabled") {
677 if err := o.InternalState.Event("receive_eapol_flow"); err != nil {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700678 log.Warnf("Can't go to eapol_flow_received: %v", err)
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700679 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700680 } else if o.InternalState.Is("gem_port_added") {
Matteo Scandoloc1147092019-10-29 09:38:33 -0700681
682 if o.Auth == true {
683 if err := o.InternalState.Event("start_auth"); err != nil {
684 log.Warnf("Can't go to auth_started: %v", err)
685 }
686 } else {
687 onuLogger.WithFields(log.Fields{
688 "IntfId": o.PonPortID,
689 "OnuId": o.ID,
690 "SerialNumber": o.Sn(),
691 }).Warn("Not starting authentication as Auth bit is not set in CLI parameters")
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700692 }
Matteo Scandoloc1147092019-10-29 09:38:33 -0700693
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700694 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700695 } else if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeIPv4) &&
696 msg.Flow.Classifier.SrcPort == uint32(68) &&
Matteo Scandolo3ac52792020-03-05 14:21:08 -0800697 msg.Flow.Classifier.DstPort == uint32(67) &&
698 msg.Flow.Classifier.OPbits == 0 {
Matteo Scandolo99f18462019-10-28 14:14:28 -0700699
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100700 // 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 -0700701 o.DhcpFlowReceived = true
Matteo Scandoloc1147092019-10-29 09:38:33 -0700702
703 if o.Dhcp == true {
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100704 // NOTE we are receiving multiple DHCP flows but we shouldn't call the transition multiple times
Matteo Scandoloc1147092019-10-29 09:38:33 -0700705 if err := o.InternalState.Event("start_dhcp"); err != nil {
706 log.Errorf("Can't go to dhcp_started: %v", err)
707 }
708 } else {
709 onuLogger.WithFields(log.Fields{
710 "IntfId": o.PonPortID,
711 "OnuId": o.ID,
712 "SerialNumber": o.Sn(),
713 }).Warn("Not starting DHCP as Dhcp bit is not set in CLI parameters")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700714 }
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700715 }
716}
717
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700718// HexDecode converts the hex encoding to binary
719func HexDecode(pkt []byte) []byte {
720 p := make([]byte, len(pkt)/2)
721 for i, j := 0, 0; i < len(pkt); i, j = i+2, j+1 {
722 // Go figure this ;)
723 u := (pkt[i] & 15) + (pkt[i]>>6)*9
724 l := (pkt[i+1] & 15) + (pkt[i+1]>>6)*9
725 p[j] = u<<4 + l
726 }
727 onuLogger.Tracef("Omci decoded: %x.", p)
728 return p
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700729}
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700730
731// BBR methods
732
733func sendOmciMsg(pktBytes []byte, intfId uint32, onuId uint32, sn *openolt.SerialNumber, msgType string, client openolt.OpenoltClient) {
734 omciMsg := openolt.OmciMsg{
735 IntfId: intfId,
736 OnuId: onuId,
737 Pkt: pktBytes,
738 }
739
740 if _, err := client.OmciMsgOut(context.Background(), &omciMsg); err != nil {
741 log.WithFields(log.Fields{
742 "IntfId": intfId,
743 "OnuId": onuId,
744 "SerialNumber": common.OnuSnToString(sn),
745 "Pkt": omciMsg.Pkt,
746 }).Fatalf("Failed to send MIB Reset")
747 }
748 log.WithFields(log.Fields{
749 "IntfId": intfId,
750 "OnuId": onuId,
751 "SerialNumber": common.OnuSnToString(sn),
752 "Pkt": omciMsg.Pkt,
753 }).Tracef("Sent OMCI message %s", msgType)
754}
755
756func (onu *Onu) getNextTid(highPriority ...bool) uint16 {
757 var next uint16
758 if len(highPriority) > 0 && highPriority[0] {
759 next = onu.hpTid
760 onu.hpTid += 1
761 if onu.hpTid < 0x8000 {
762 onu.hpTid = 0x8000
763 }
764 } else {
765 next = onu.tid
766 onu.tid += 1
767 if onu.tid >= 0x8000 {
768 onu.tid = 1
769 }
770 }
771 return next
772}
773
774// TODO move this method in responders/omcisim
775func (o *Onu) StartOmci(client openolt.OpenoltClient) {
776 mibReset, _ := omcilib.CreateMibResetRequest(o.getNextTid(false))
777 sendOmciMsg(mibReset, o.PonPortID, o.ID, o.SerialNumber, "mibReset", client)
778}
779
780func (o *Onu) handleOmci(msg OmciIndicationMessage, client openolt.OpenoltClient) {
781 msgType, packet := omcilib.DecodeOmci(msg.OmciInd.Pkt)
782
783 log.WithFields(log.Fields{
784 "IntfId": msg.OmciInd.IntfId,
785 "OnuId": msg.OmciInd.OnuId,
786 "OnuSn": common.OnuSnToString(o.SerialNumber),
787 "Pkt": msg.OmciInd.Pkt,
788 "msgType": msgType,
Anand S Katti09541352020-01-29 15:54:01 +0530789 }).Trace("ONU Receives OMCI Msg")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700790 switch msgType {
791 default:
Matteo Scandolo813402b2019-10-23 19:24:52 -0700792 log.WithFields(log.Fields{
793 "IntfId": msg.OmciInd.IntfId,
794 "OnuId": msg.OmciInd.OnuId,
795 "OnuSn": common.OnuSnToString(o.SerialNumber),
796 "Pkt": msg.OmciInd.Pkt,
797 "msgType": msgType,
798 }).Fatalf("unexpected frame: %v", packet)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700799 case omci.MibResetResponseType:
800 mibUpload, _ := omcilib.CreateMibUploadRequest(o.getNextTid(false))
801 sendOmciMsg(mibUpload, o.PonPortID, o.ID, o.SerialNumber, "mibUpload", client)
802 case omci.MibUploadResponseType:
803 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
804 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
805 case omci.MibUploadNextResponseType:
806 o.seqNumber++
807
808 if o.seqNumber > 290 {
809 // NOTE we are done with the MIB Upload (290 is the number of messages the omci-sim library will respond to)
810 galEnet, _ := omcilib.CreateGalEnetRequest(o.getNextTid(false))
811 sendOmciMsg(galEnet, o.PonPortID, o.ID, o.SerialNumber, "CreateGalEnetRequest", client)
812 } else {
813 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
814 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
815 }
816 case omci.CreateResponseType:
817 // NOTE Creating a GemPort,
818 // BBsim actually doesn't care about the values, so we can do we want with the parameters
819 // In the same way we can create a GemPort even without setting up UNIs/TConts/...
820 // but we need the GemPort to trigger the state change
821
822 if !o.HasGemPort {
823 // NOTE this sends a CreateRequestType and BBSim replies with a CreateResponseType
824 // thus we send this request only once
825 gemReq, _ := omcilib.CreateGemPortRequest(o.getNextTid(false))
826 sendOmciMsg(gemReq, o.PonPortID, o.ID, o.SerialNumber, "CreateGemPortRequest", client)
827 o.HasGemPort = true
828 } else {
829 if err := o.InternalState.Event("send_eapol_flow"); err != nil {
830 onuLogger.WithFields(log.Fields{
831 "OnuId": o.ID,
832 "IntfId": o.PonPortID,
833 "OnuSn": o.Sn(),
834 }).Errorf("Error while transitioning ONU State %v", err)
835 }
836 }
837
838 }
839}
840
841func (o *Onu) sendEapolFlow(client openolt.OpenoltClient) {
842
843 classifierProto := openolt.Classifier{
844 EthType: uint32(layers.EthernetTypeEAPOL),
845 OVid: 4091,
846 }
847
848 actionProto := openolt.Action{}
849
850 downstreamFlow := openolt.Flow{
851 AccessIntfId: int32(o.PonPortID),
852 OnuId: int32(o.ID),
Matteo Scandolo813402b2019-10-23 19:24:52 -0700853 UniId: int32(0), // NOTE do not hardcode this, we need to support multiple UNIs
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700854 FlowId: uint32(o.ID),
855 FlowType: "downstream",
856 AllocId: int32(0),
857 NetworkIntfId: int32(0),
858 GemportId: int32(1), // FIXME use the same value as CreateGemPortRequest PortID, do not hardcode
859 Classifier: &classifierProto,
860 Action: &actionProto,
861 Priority: int32(100),
862 Cookie: uint64(o.ID),
863 PortNo: uint32(o.ID), // NOTE we are using this to map an incoming packetIndication to an ONU
864 }
865
866 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
867 log.WithFields(log.Fields{
868 "IntfId": o.PonPortID,
869 "OnuId": o.ID,
870 "FlowId": downstreamFlow.FlowId,
871 "PortNo": downstreamFlow.PortNo,
872 "SerialNumber": common.OnuSnToString(o.SerialNumber),
873 }).Fatalf("Failed to EAPOL Flow")
874 }
875 log.WithFields(log.Fields{
876 "IntfId": o.PonPortID,
877 "OnuId": o.ID,
878 "FlowId": downstreamFlow.FlowId,
879 "PortNo": downstreamFlow.PortNo,
880 "SerialNumber": common.OnuSnToString(o.SerialNumber),
881 }).Info("Sent EAPOL Flow")
882}
883
884func (o *Onu) sendDhcpFlow(client openolt.OpenoltClient) {
885 classifierProto := openolt.Classifier{
886 EthType: uint32(layers.EthernetTypeIPv4),
887 SrcPort: uint32(68),
888 DstPort: uint32(67),
889 }
890
891 actionProto := openolt.Action{}
892
893 downstreamFlow := openolt.Flow{
894 AccessIntfId: int32(o.PonPortID),
895 OnuId: int32(o.ID),
Matteo Scandolo813402b2019-10-23 19:24:52 -0700896 UniId: int32(0), // FIXME do not hardcode this
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700897 FlowId: uint32(o.ID),
898 FlowType: "downstream",
899 AllocId: int32(0),
900 NetworkIntfId: int32(0),
901 GemportId: int32(1), // FIXME use the same value as CreateGemPortRequest PortID, do not hardcode
902 Classifier: &classifierProto,
903 Action: &actionProto,
904 Priority: int32(100),
905 Cookie: uint64(o.ID),
906 PortNo: uint32(o.ID), // NOTE we are using this to map an incoming packetIndication to an ONU
907 }
908
909 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
910 log.WithFields(log.Fields{
911 "IntfId": o.PonPortID,
912 "OnuId": o.ID,
913 "FlowId": downstreamFlow.FlowId,
914 "PortNo": downstreamFlow.PortNo,
915 "SerialNumber": common.OnuSnToString(o.SerialNumber),
916 }).Fatalf("Failed to send DHCP Flow")
917 }
918 log.WithFields(log.Fields{
919 "IntfId": o.PonPortID,
920 "OnuId": o.ID,
921 "FlowId": downstreamFlow.FlowId,
922 "PortNo": downstreamFlow.PortNo,
923 "SerialNumber": common.OnuSnToString(o.SerialNumber),
924 }).Info("Sent DHCP Flow")
925}
Pragya Arya8bdb4532020-03-02 17:08:09 +0530926
927// DeleteFlow method search and delete flowKey from the onu flows slice
928func (onu *Onu) DeleteFlow(key FlowKey) {
929 for pos, flowKey := range onu.Flows {
930 if flowKey == key {
931 // delete the flowKey by shifting all flowKeys by one
932 onu.Flows = append(onu.Flows[:pos], onu.Flows[pos+1:]...)
933 t := make([]FlowKey, len(onu.Flows))
934 copy(t, onu.Flows)
935 onu.Flows = t
936 break
937 }
938 }
939}