blob: 56fba073ddd5c58b474c9148cdf4a7cb23ee8cd6 [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,
Matteo Scandolo378b8c92020-04-16 14:34:22 -0700101 HwAddress: net.HardwareAddr{0x2e, 0x60, 0x70, byte(olt.ID), byte(pon.ID), byte(id)},
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800102 PortNo: 0,
103 tid: 0x1,
104 hpTid: 0x8000,
105 seqNumber: 0,
106 DoneChannel: make(chan bool, 1),
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
Jonathan Hartacfa20e2020-03-31 15:20:14 -0700154 {Name: "igmp_join_start", Src: []string{"eap_response_success_received", "gem_port_added", "eapol_flow_received", "dhcp_ack_received", "igmp_left", "igmp_join_error", "igmp_join_started"}, Dst: "igmp_join_started"},
155 {Name: "igmp_join_startv3", Src: []string{"eap_response_success_received", "gem_port_added", "eapol_flow_received", "dhcp_ack_received", "igmp_left", "igmp_join_error", "igmp_join_started"}, 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
Matteo Scandolo378b8c92020-04-16 14:34:22 -0700362 dhcp.SendDHCPDiscovery(o.PonPort.Olt.ID, o.PonPortID, o.ID, o.Sn(), o.PortNo, o.InternalState, o.HwAddress, o.CTag, stream)
David Bainbridge103cf022019-12-16 20:11:35 +0000363 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
Matteo Scandolo378b8c92020-04-16 14:34:22 -0700378 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 +0000379 }
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,
Matteo Scandoloa26b0692020-03-26 11:15:07 -0700462 "OnuSn": o.Sn(),
William Kurkian9dadc5b2019-10-22 13:51:57 -0400463 }).Infof("GemPort Added")
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700464
William Kurkian9dadc5b2019-10-22 13:51:57 -0400465 // NOTE if we receive the GemPort but we don't have EAPOL flows
466 // go an intermediate state, otherwise start auth
467 if o.InternalState.Is("enabled") {
468 if err := o.InternalState.Event("add_gem_port"); err != nil {
469 log.Errorf("Can't go to gem_port_added: %v", err)
470 }
471 } else if o.InternalState.Is("eapol_flow_received") {
Matteo Scandolo3c656a12019-12-10 09:54:51 -0800472 if o.Auth == true {
473 if err := o.InternalState.Event("start_auth"); err != nil {
474 log.Warnf("Can't go to auth_started: %v", err)
475 }
476 } else {
477 onuLogger.WithFields(log.Fields{
478 "IntfId": o.PonPortID,
479 "OnuId": o.ID,
480 "SerialNumber": o.Sn(),
481 }).Warn("Not starting authentication as Auth bit is not set in CLI parameters")
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700482 }
483 }
484 }
485}
486
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100487func (o Onu) NewSN(oltid int, intfid uint32, onuid uint32) *openolt.SerialNumber {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700488
489 sn := new(openolt.SerialNumber)
490
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700491 //sn = new(openolt.SerialNumber)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700492 sn.VendorId = []byte("BBSM")
493 sn.VendorSpecific = []byte{0, byte(oltid % 256), byte(intfid), byte(onuid)}
494
495 return sn
496}
497
William Kurkian0418bc82019-11-06 12:16:24 -0500498func (o *Onu) sendOnuDiscIndication(msg OnuDiscIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700499 discoverData := &openolt.Indication_OnuDiscInd{OnuDiscInd: &openolt.OnuDiscIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700500 IntfId: msg.Onu.PonPortID,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700501 SerialNumber: msg.Onu.SerialNumber,
502 }}
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700503
Matteo Scandolo4747d292019-08-05 11:50:18 -0700504 if err := stream.Send(&openolt.Indication{Data: discoverData}); err != nil {
Matteo Scandolo11006992019-08-28 11:29:46 -0700505 log.Errorf("Failed to send Indication_OnuDiscInd: %v", err)
Matteo Scandolo99f18462019-10-28 14:14:28 -0700506 return
Matteo Scandolo4747d292019-08-05 11:50:18 -0700507 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700508
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700509 onuLogger.WithFields(log.Fields{
Matteo Scandolo4747d292019-08-05 11:50:18 -0700510 "IntfId": msg.Onu.PonPortID,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700511 "OnuSn": msg.Onu.Sn(),
512 "OnuId": o.ID,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700513 }).Debug("Sent Indication_OnuDiscInd")
Pragya Arya324337e2020-02-20 14:35:08 +0530514 publishEvent("ONU-discovery-indication-sent", int32(msg.Onu.PonPortID), int32(o.ID), msg.Onu.Sn())
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800515
516 // after DiscoveryRetryDelay check if the state is the same and in case send a new OnuDiscIndication
517 go func(delay time.Duration) {
Matteo Scandolo569e7172019-12-20 11:51:51 -0800518 time.Sleep(delay)
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800519 if o.InternalState.Current() == "discovered" {
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800520 o.sendOnuDiscIndication(msg, stream)
521 }
522 }(o.DiscoveryRetryDelay)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700523}
524
William Kurkian0418bc82019-11-06 12:16:24 -0500525func (o *Onu) sendOnuIndication(msg OnuIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700526 // NOTE voltha returns an ID, but if we use that ID then it complains:
527 // expected_onu_id: 1, received_onu_id: 1024, event: ONU-id-mismatch, can happen if both voltha and the olt rebooted
528 // so we're using the internal ID that is 1
529 // o.ID = msg.OnuID
Matteo Scandolo4747d292019-08-05 11:50:18 -0700530
531 indData := &openolt.Indication_OnuInd{OnuInd: &openolt.OnuIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700532 IntfId: o.PonPortID,
533 OnuId: o.ID,
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700534 OperState: msg.OperState.String(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700535 AdminState: o.OperState.Current(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700536 SerialNumber: o.SerialNumber,
537 }}
538 if err := stream.Send(&openolt.Indication{Data: indData}); err != nil {
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800539 // NOTE do we need to transition to a broken state?
Matteo Scandolo11006992019-08-28 11:29:46 -0700540 log.Errorf("Failed to send Indication_OnuInd: %v", err)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700541 }
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700542 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700543 "IntfId": o.PonPortID,
544 "OnuId": o.ID,
545 "OperState": msg.OperState.String(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700546 "AdminState": msg.OperState.String(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700547 "OnuSn": o.Sn(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700548 }).Debug("Sent Indication_OnuInd")
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700549
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700550}
551
Pragya Arya324337e2020-02-20 14:35:08 +0530552func (o *Onu) publishOmciEvent(msg OmciMessage) {
553 if olt.PublishEvents {
554 _, _, msgType, _, _, _, err := omcisim.ParsePkt(HexDecode(msg.omciMsg.Pkt))
555 if err != nil {
556 log.Errorf("error in getting msgType %v", err)
557 return
558 }
559 if msgType == omcisim.MibUpload {
560 o.seqNumber = 0
561 publishEvent("MIB-upload-received", int32(o.PonPortID), int32(o.ID), common.OnuSnToString(o.SerialNumber))
562 } else if msgType == omcisim.MibUploadNext {
563 o.seqNumber++
564 if o.seqNumber > 290 {
565 publishEvent("MIB-upload-done", int32(o.PonPortID), int32(o.ID), common.OnuSnToString(o.SerialNumber))
566 }
567 }
568 }
569}
570
Scott Bakerb90c4312020-03-12 21:33:25 -0700571// Create a TestResponse packet and send it
572func (o *Onu) sendTestResult(msg OmciMessage, stream openolt.Openolt_EnableIndicationServer) error {
573 resp, err := omcilib.BuildTestResult(HexDecode(msg.omciMsg.Pkt))
574 if err != nil {
575 return err
576 }
577
578 var omciInd openolt.OmciIndication
579 omciInd.IntfId = o.PonPortID
580 omciInd.OnuId = o.ID
581 omciInd.Pkt = resp
582
583 omci := &openolt.Indication_OmciInd{OmciInd: &omciInd}
584 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
585 onuLogger.WithFields(log.Fields{
586 "IntfId": o.PonPortID,
587 "SerialNumber": o.Sn(),
588 "omciPacket": omciInd.Pkt,
589 "msg": msg,
590 }).Errorf("send TestResult omcisim indication failed: %v", err)
591 return err
592 }
593 onuLogger.WithFields(log.Fields{
594 "IntfId": o.PonPortID,
595 "SerialNumber": o.Sn(),
596 "omciPacket": omciInd.Pkt,
597 }).Tracef("Sent TestResult OMCI message")
598
599 return nil
600}
601
William Kurkian0418bc82019-11-06 12:16:24 -0500602func (o *Onu) handleOmciMessage(msg OmciMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700603
604 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700605 "IntfId": o.PonPortID,
Matteo Scandolo27428702019-10-11 16:21:16 -0700606 "SerialNumber": o.Sn(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700607 "omciPacket": msg.omciMsg.Pkt,
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700608 }).Tracef("Received OMCI message")
609
Pragya Arya324337e2020-02-20 14:35:08 +0530610 o.publishOmciEvent(msg)
611
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700612 var omciInd openolt.OmciIndication
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700613 respPkt, err := omcisim.OmciSim(o.PonPortID, o.ID, HexDecode(msg.omciMsg.Pkt))
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700614 if err != nil {
Matteo Scandolo27428702019-10-11 16:21:16 -0700615 onuLogger.WithFields(log.Fields{
616 "IntfId": o.PonPortID,
617 "SerialNumber": o.Sn(),
618 "omciPacket": omciInd.Pkt,
619 "msg": msg,
620 }).Errorf("Error handling OMCI message %v", msg)
621 return
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700622 }
623
624 omciInd.IntfId = o.PonPortID
625 omciInd.OnuId = o.ID
626 omciInd.Pkt = respPkt
627
628 omci := &openolt.Indication_OmciInd{OmciInd: &omciInd}
629 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
Matteo Scandolo27428702019-10-11 16:21:16 -0700630 onuLogger.WithFields(log.Fields{
631 "IntfId": o.PonPortID,
632 "SerialNumber": o.Sn(),
633 "omciPacket": omciInd.Pkt,
634 "msg": msg,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700635 }).Errorf("send omcisim indication failed: %v", err)
Matteo Scandolo27428702019-10-11 16:21:16 -0700636 return
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700637 }
638 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700639 "IntfId": o.PonPortID,
Matteo Scandolo27428702019-10-11 16:21:16 -0700640 "SerialNumber": o.Sn(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700641 "omciPacket": omciInd.Pkt,
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700642 }).Tracef("Sent OMCI message")
Scott Bakerb90c4312020-03-12 21:33:25 -0700643
644 // Test message is special, it requires sending two packets:
645 // first packet: TestResponse, says whether test was started successully, handled by omci-sim
646 // second packet, TestResult, reports the result of running the self-test
647 // TestResult can come some time after a TestResponse
648 // TODO: Implement some delay between the TestResponse and the TestResult
649 isTest, err := omcilib.IsTestRequest(HexDecode(msg.omciMsg.Pkt))
650 if (err == nil) && (isTest) {
651 o.sendTestResult(msg, stream)
652 }
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700653}
654
Matteo Scandolo27428702019-10-11 16:21:16 -0700655func (o *Onu) storePortNumber(portNo uint32) {
Matteo Scandolo813402b2019-10-23 19:24:52 -0700656 // NOTE this needed only as long as we don't support multiple UNIs
Matteo Scandolo27428702019-10-11 16:21:16 -0700657 // we need to add support for multiple UNIs
658 // the action plan is:
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700659 // - refactor the omcisim-sim library to use https://github.com/cboling/omci instead of canned messages
Matteo Scandolo27428702019-10-11 16:21:16 -0700660 // - change the library so that it reports a single UNI and remove this workaroung
661 // - add support for multiple UNIs in BBSim
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700662 if o.PortNo == 0 || portNo < o.PortNo {
Matteo Scandolo813402b2019-10-23 19:24:52 -0700663 onuLogger.WithFields(log.Fields{
664 "IntfId": o.PonPortID,
665 "OnuId": o.ID,
666 "SerialNumber": o.Sn(),
667 "OnuPortNo": o.PortNo,
668 "FlowPortNo": portNo,
669 }).Debug("Storing ONU portNo")
Matteo Scandolo27428702019-10-11 16:21:16 -0700670 o.PortNo = portNo
671 }
672}
673
William Kurkian0418bc82019-11-06 12:16:24 -0500674func (o *Onu) SetID(id uint32) {
Matteo Scandolo583f17d2020-02-13 10:35:17 -0800675 onuLogger.WithFields(log.Fields{
676 "IntfId": o.PonPortID,
677 "OnuId": id,
678 "SerialNumber": o.Sn(),
679 }).Debug("Storing OnuId ")
William Kurkian0418bc82019-11-06 12:16:24 -0500680 o.ID = id
681}
682
Matteo Scandolo813402b2019-10-23 19:24:52 -0700683func (o *Onu) handleFlowUpdate(msg OnuFlowUpdateMessage) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700684 onuLogger.WithFields(log.Fields{
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800685 "DstPort": msg.Flow.Classifier.DstPort,
686 "EthType": fmt.Sprintf("%x", msg.Flow.Classifier.EthType),
687 "FlowId": msg.Flow.FlowId,
688 "FlowType": msg.Flow.FlowType,
689 "GemportId": msg.Flow.GemportId,
690 "InnerVlan": msg.Flow.Classifier.IVid,
691 "IntfId": msg.Flow.AccessIntfId,
692 "IpProto": msg.Flow.Classifier.IpProto,
693 "OnuId": msg.Flow.OnuId,
694 "OnuSn": o.Sn(),
695 "OuterVlan": msg.Flow.Classifier.OVid,
696 "PortNo": msg.Flow.PortNo,
697 "SrcPort": msg.Flow.Classifier.SrcPort,
698 "UniID": msg.Flow.UniId,
Matteo Scandolo3ac52792020-03-05 14:21:08 -0800699 "ClassifierOPbits": msg.Flow.Classifier.OPbits,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700700 }).Debug("ONU receives Flow")
701
Matteo Scandolo813402b2019-10-23 19:24:52 -0700702 if msg.Flow.UniId != 0 {
703 // as of now BBSim only support a single UNI, so ignore everything that is not targeted to it
704 onuLogger.WithFields(log.Fields{
705 "IntfId": o.PonPortID,
706 "OnuId": o.ID,
707 "SerialNumber": o.Sn(),
708 }).Debug("Ignoring flow as it's not for the first UNI")
709 return
710 }
711
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700712 if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeEAPOL) && msg.Flow.Classifier.OVid == 4091 {
Matteo Scandolo27428702019-10-11 16:21:16 -0700713 // NOTE storing the PortNO, it's needed when sending PacketIndications
Matteo Scandolo813402b2019-10-23 19:24:52 -0700714 o.storePortNumber(uint32(msg.Flow.PortNo))
Matteo Scandolo27428702019-10-11 16:21:16 -0700715
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700716 // NOTE if we receive the EAPOL flows but we don't have GemPorts
717 // go an intermediate state, otherwise start auth
718 if o.InternalState.Is("enabled") {
719 if err := o.InternalState.Event("receive_eapol_flow"); err != nil {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700720 log.Warnf("Can't go to eapol_flow_received: %v", err)
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700721 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700722 } else if o.InternalState.Is("gem_port_added") {
Matteo Scandoloc1147092019-10-29 09:38:33 -0700723
724 if o.Auth == true {
725 if err := o.InternalState.Event("start_auth"); err != nil {
726 log.Warnf("Can't go to auth_started: %v", err)
727 }
728 } else {
729 onuLogger.WithFields(log.Fields{
730 "IntfId": o.PonPortID,
731 "OnuId": o.ID,
732 "SerialNumber": o.Sn(),
733 }).Warn("Not starting authentication as Auth bit is not set in CLI parameters")
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700734 }
Matteo Scandoloc1147092019-10-29 09:38:33 -0700735
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700736 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700737 } else if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeIPv4) &&
738 msg.Flow.Classifier.SrcPort == uint32(68) &&
Matteo Scandolo3ac52792020-03-05 14:21:08 -0800739 msg.Flow.Classifier.DstPort == uint32(67) &&
740 msg.Flow.Classifier.OPbits == 0 {
Matteo Scandolo99f18462019-10-28 14:14:28 -0700741
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100742 // 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 -0700743 o.DhcpFlowReceived = true
Matteo Scandoloc1147092019-10-29 09:38:33 -0700744
745 if o.Dhcp == true {
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100746 // NOTE we are receiving multiple DHCP flows but we shouldn't call the transition multiple times
Matteo Scandoloc1147092019-10-29 09:38:33 -0700747 if err := o.InternalState.Event("start_dhcp"); err != nil {
748 log.Errorf("Can't go to dhcp_started: %v", err)
749 }
750 } else {
751 onuLogger.WithFields(log.Fields{
752 "IntfId": o.PonPortID,
753 "OnuId": o.ID,
754 "SerialNumber": o.Sn(),
755 }).Warn("Not starting DHCP as Dhcp bit is not set in CLI parameters")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700756 }
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700757 }
758}
759
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700760// HexDecode converts the hex encoding to binary
761func HexDecode(pkt []byte) []byte {
762 p := make([]byte, len(pkt)/2)
763 for i, j := 0, 0; i < len(pkt); i, j = i+2, j+1 {
764 // Go figure this ;)
765 u := (pkt[i] & 15) + (pkt[i]>>6)*9
766 l := (pkt[i+1] & 15) + (pkt[i+1]>>6)*9
767 p[j] = u<<4 + l
768 }
769 onuLogger.Tracef("Omci decoded: %x.", p)
770 return p
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700771}
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700772
773// BBR methods
774
775func sendOmciMsg(pktBytes []byte, intfId uint32, onuId uint32, sn *openolt.SerialNumber, msgType string, client openolt.OpenoltClient) {
776 omciMsg := openolt.OmciMsg{
777 IntfId: intfId,
778 OnuId: onuId,
779 Pkt: pktBytes,
780 }
781
782 if _, err := client.OmciMsgOut(context.Background(), &omciMsg); err != nil {
783 log.WithFields(log.Fields{
784 "IntfId": intfId,
785 "OnuId": onuId,
786 "SerialNumber": common.OnuSnToString(sn),
787 "Pkt": omciMsg.Pkt,
788 }).Fatalf("Failed to send MIB Reset")
789 }
790 log.WithFields(log.Fields{
791 "IntfId": intfId,
792 "OnuId": onuId,
793 "SerialNumber": common.OnuSnToString(sn),
794 "Pkt": omciMsg.Pkt,
795 }).Tracef("Sent OMCI message %s", msgType)
796}
797
798func (onu *Onu) getNextTid(highPriority ...bool) uint16 {
799 var next uint16
800 if len(highPriority) > 0 && highPriority[0] {
801 next = onu.hpTid
802 onu.hpTid += 1
803 if onu.hpTid < 0x8000 {
804 onu.hpTid = 0x8000
805 }
806 } else {
807 next = onu.tid
808 onu.tid += 1
809 if onu.tid >= 0x8000 {
810 onu.tid = 1
811 }
812 }
813 return next
814}
815
816// TODO move this method in responders/omcisim
817func (o *Onu) StartOmci(client openolt.OpenoltClient) {
818 mibReset, _ := omcilib.CreateMibResetRequest(o.getNextTid(false))
819 sendOmciMsg(mibReset, o.PonPortID, o.ID, o.SerialNumber, "mibReset", client)
820}
821
822func (o *Onu) handleOmci(msg OmciIndicationMessage, client openolt.OpenoltClient) {
823 msgType, packet := omcilib.DecodeOmci(msg.OmciInd.Pkt)
824
825 log.WithFields(log.Fields{
826 "IntfId": msg.OmciInd.IntfId,
827 "OnuId": msg.OmciInd.OnuId,
828 "OnuSn": common.OnuSnToString(o.SerialNumber),
829 "Pkt": msg.OmciInd.Pkt,
830 "msgType": msgType,
Anand S Katti09541352020-01-29 15:54:01 +0530831 }).Trace("ONU Receives OMCI Msg")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700832 switch msgType {
833 default:
Matteo Scandolo813402b2019-10-23 19:24:52 -0700834 log.WithFields(log.Fields{
835 "IntfId": msg.OmciInd.IntfId,
836 "OnuId": msg.OmciInd.OnuId,
837 "OnuSn": common.OnuSnToString(o.SerialNumber),
838 "Pkt": msg.OmciInd.Pkt,
839 "msgType": msgType,
840 }).Fatalf("unexpected frame: %v", packet)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700841 case omci.MibResetResponseType:
842 mibUpload, _ := omcilib.CreateMibUploadRequest(o.getNextTid(false))
843 sendOmciMsg(mibUpload, o.PonPortID, o.ID, o.SerialNumber, "mibUpload", client)
844 case omci.MibUploadResponseType:
845 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
846 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
847 case omci.MibUploadNextResponseType:
848 o.seqNumber++
849
850 if o.seqNumber > 290 {
851 // NOTE we are done with the MIB Upload (290 is the number of messages the omci-sim library will respond to)
852 galEnet, _ := omcilib.CreateGalEnetRequest(o.getNextTid(false))
853 sendOmciMsg(galEnet, o.PonPortID, o.ID, o.SerialNumber, "CreateGalEnetRequest", client)
854 } else {
855 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
856 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
857 }
858 case omci.CreateResponseType:
859 // NOTE Creating a GemPort,
860 // BBsim actually doesn't care about the values, so we can do we want with the parameters
861 // In the same way we can create a GemPort even without setting up UNIs/TConts/...
862 // but we need the GemPort to trigger the state change
863
864 if !o.HasGemPort {
865 // NOTE this sends a CreateRequestType and BBSim replies with a CreateResponseType
866 // thus we send this request only once
867 gemReq, _ := omcilib.CreateGemPortRequest(o.getNextTid(false))
868 sendOmciMsg(gemReq, o.PonPortID, o.ID, o.SerialNumber, "CreateGemPortRequest", client)
869 o.HasGemPort = true
870 } else {
871 if err := o.InternalState.Event("send_eapol_flow"); err != nil {
872 onuLogger.WithFields(log.Fields{
873 "OnuId": o.ID,
874 "IntfId": o.PonPortID,
875 "OnuSn": o.Sn(),
876 }).Errorf("Error while transitioning ONU State %v", err)
877 }
878 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700879 }
880}
881
882func (o *Onu) sendEapolFlow(client openolt.OpenoltClient) {
883
884 classifierProto := openolt.Classifier{
885 EthType: uint32(layers.EthernetTypeEAPOL),
886 OVid: 4091,
887 }
888
889 actionProto := openolt.Action{}
890
891 downstreamFlow := openolt.Flow{
892 AccessIntfId: int32(o.PonPortID),
893 OnuId: int32(o.ID),
Matteo Scandolo813402b2019-10-23 19:24:52 -0700894 UniId: int32(0), // NOTE do not hardcode this, we need to support multiple UNIs
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700895 FlowId: uint32(o.ID),
896 FlowType: "downstream",
897 AllocId: int32(0),
898 NetworkIntfId: int32(0),
899 GemportId: int32(1), // FIXME use the same value as CreateGemPortRequest PortID, do not hardcode
900 Classifier: &classifierProto,
901 Action: &actionProto,
902 Priority: int32(100),
903 Cookie: uint64(o.ID),
904 PortNo: uint32(o.ID), // NOTE we are using this to map an incoming packetIndication to an ONU
905 }
906
907 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
908 log.WithFields(log.Fields{
909 "IntfId": o.PonPortID,
910 "OnuId": o.ID,
911 "FlowId": downstreamFlow.FlowId,
912 "PortNo": downstreamFlow.PortNo,
913 "SerialNumber": common.OnuSnToString(o.SerialNumber),
914 }).Fatalf("Failed to EAPOL Flow")
915 }
916 log.WithFields(log.Fields{
917 "IntfId": o.PonPortID,
918 "OnuId": o.ID,
919 "FlowId": downstreamFlow.FlowId,
920 "PortNo": downstreamFlow.PortNo,
921 "SerialNumber": common.OnuSnToString(o.SerialNumber),
922 }).Info("Sent EAPOL Flow")
923}
924
925func (o *Onu) sendDhcpFlow(client openolt.OpenoltClient) {
926 classifierProto := openolt.Classifier{
927 EthType: uint32(layers.EthernetTypeIPv4),
928 SrcPort: uint32(68),
929 DstPort: uint32(67),
930 }
931
932 actionProto := openolt.Action{}
933
934 downstreamFlow := openolt.Flow{
935 AccessIntfId: int32(o.PonPortID),
936 OnuId: int32(o.ID),
Matteo Scandolo813402b2019-10-23 19:24:52 -0700937 UniId: int32(0), // FIXME do not hardcode this
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700938 FlowId: uint32(o.ID),
939 FlowType: "downstream",
940 AllocId: int32(0),
941 NetworkIntfId: int32(0),
942 GemportId: int32(1), // FIXME use the same value as CreateGemPortRequest PortID, do not hardcode
943 Classifier: &classifierProto,
944 Action: &actionProto,
945 Priority: int32(100),
946 Cookie: uint64(o.ID),
947 PortNo: uint32(o.ID), // NOTE we are using this to map an incoming packetIndication to an ONU
948 }
949
950 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
951 log.WithFields(log.Fields{
952 "IntfId": o.PonPortID,
953 "OnuId": o.ID,
954 "FlowId": downstreamFlow.FlowId,
955 "PortNo": downstreamFlow.PortNo,
956 "SerialNumber": common.OnuSnToString(o.SerialNumber),
957 }).Fatalf("Failed to send DHCP Flow")
958 }
959 log.WithFields(log.Fields{
960 "IntfId": o.PonPortID,
961 "OnuId": o.ID,
962 "FlowId": downstreamFlow.FlowId,
963 "PortNo": downstreamFlow.PortNo,
964 "SerialNumber": common.OnuSnToString(o.SerialNumber),
965 }).Info("Sent DHCP Flow")
966}
Pragya Arya8bdb4532020-03-02 17:08:09 +0530967
968// DeleteFlow method search and delete flowKey from the onu flows slice
969func (onu *Onu) DeleteFlow(key FlowKey) {
970 for pos, flowKey := range onu.Flows {
971 if flowKey == key {
972 // delete the flowKey by shifting all flowKeys by one
973 onu.Flows = append(onu.Flows[:pos], onu.Flows[pos+1:]...)
974 t := make([]FlowKey, len(onu.Flows))
975 copy(t, onu.Flows)
976 onu.Flows = t
977 break
978 }
979 }
980}