blob: ecb91f8f67e7d044a80bd2d08bdd27911e90d48f [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"
Matteo Scandolod74abba2020-04-16 16:36:44 -070038 "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
Matteo Scandolo47ef64b2020-04-20 14:16:07 -070067 // FIXME add support for multiple UNIs (each UNI has a different PortNo)
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
Hardik Windlassad790cb2020-06-17 21:26:22 +0530127 {Name: "initialize", Src: []string{"created", "disabled", "pon_disabled"}, Dst: "initialized"},
128 {Name: "discover", Src: []string{"initialized"}, Dst: "discovered"},
129 {Name: "enable", Src: []string{"discovered", "pon_disabled"}, Dst: "enabled"},
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700130 {Name: "receive_eapol_flow", Src: []string{"enabled", "gem_port_added"}, Dst: "eapol_flow_received"},
131 {Name: "add_gem_port", Src: []string{"enabled", "eapol_flow_received"}, Dst: "gem_port_added"},
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100132 // NOTE should disabled state be different for oper_disabled (emulating an error) and admin_disabled (received a disabled call via VOLTHA)?
Pragya Arya2225f202020-01-29 18:05:01 +0530133 {Name: "disable", Src: []string{"enabled", "eapol_flow_received", "gem_port_added", "eap_response_success_received", "auth_failed", "dhcp_ack_received", "dhcp_failed", "pon_disabled"}, Dst: "disabled"},
Pragya Arya6a708d62020-01-01 17:17:20 +0530134 // ONU state when PON port is disabled but ONU is power ON(more states should be added in src?)
Matteo Scandolodf3f85d2020-01-15 12:50:48 -0800135 {Name: "pon_disabled", Src: []string{"enabled", "gem_port_added", "eapol_flow_received", "eap_response_success_received", "auth_failed", "dhcp_ack_received", "dhcp_failed"}, Dst: "pon_disabled"},
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700136 // EAPOL
Shubham Sharmabd4b6572020-02-12 13:00:44 +0000137 {Name: "start_auth", Src: []string{"eapol_flow_received", "gem_port_added", "eap_start_sent", "eap_response_identity_sent", "eap_response_challenge_sent", "eap_response_success_received", "auth_failed", "dhcp_ack_received", "dhcp_failed", "igmp_join_started", "igmp_left", "igmp_join_error"}, Dst: "auth_started"},
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700138 {Name: "eap_start_sent", Src: []string{"auth_started"}, Dst: "eap_start_sent"},
139 {Name: "eap_response_identity_sent", Src: []string{"eap_start_sent"}, Dst: "eap_response_identity_sent"},
140 {Name: "eap_response_challenge_sent", Src: []string{"eap_response_identity_sent"}, Dst: "eap_response_challenge_sent"},
141 {Name: "eap_response_success_received", Src: []string{"eap_response_challenge_sent"}, Dst: "eap_response_success_received"},
142 {Name: "auth_failed", Src: []string{"auth_started", "eap_start_sent", "eap_response_identity_sent", "eap_response_challenge_sent"}, Dst: "auth_failed"},
143 // DHCP
Shubham Sharmabd4b6572020-02-12 13:00:44 +0000144 {Name: "start_dhcp", Src: []string{"eap_response_success_received", "dhcp_discovery_sent", "dhcp_request_sent", "dhcp_ack_received", "dhcp_failed", "igmp_join_started", "igmp_left", "igmp_join_error"}, Dst: "dhcp_started"},
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700145 {Name: "dhcp_discovery_sent", Src: []string{"dhcp_started"}, Dst: "dhcp_discovery_sent"},
146 {Name: "dhcp_request_sent", Src: []string{"dhcp_discovery_sent"}, Dst: "dhcp_request_sent"},
147 {Name: "dhcp_ack_received", Src: []string{"dhcp_request_sent"}, Dst: "dhcp_ack_received"},
148 {Name: "dhcp_failed", Src: []string{"dhcp_started", "dhcp_discovery_sent", "dhcp_request_sent"}, Dst: "dhcp_failed"},
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700149 // BBR States
150 // TODO add start OMCI state
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100151 {Name: "send_eapol_flow", Src: []string{"initialized"}, Dst: "eapol_flow_sent"},
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700152 {Name: "send_dhcp_flow", Src: []string{"eapol_flow_sent"}, Dst: "dhcp_flow_sent"},
Arjun E K57a7fcb2020-01-30 06:44:45 +0000153 // IGMP
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 Scandolo47ef64b2020-04-20 14:16:07 -0700202
203 // clean the ONU state
204 o.DhcpFlowReceived = false
205 o.PortNo = 0
206 o.Flows = []FlowKey{}
207
208 // set the OpenState to disabled
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800209 if err := o.OperState.Event("disable"); err != nil {
210 onuLogger.WithFields(log.Fields{
211 "OnuId": o.ID,
212 "IntfId": o.PonPortID,
213 "OnuSn": o.Sn(),
214 }).Errorf("Cannot change ONU OperState to down: %s", err.Error())
215 }
Matteo Scandolo47ef64b2020-04-20 14:16:07 -0700216
217 // send the OnuIndication DOWN event
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700218 msg := Message{
219 Type: OnuIndication,
220 Data: OnuIndicationMessage{
221 OnuSN: o.SerialNumber,
222 PonPortID: o.PonPortID,
223 OperState: DOWN,
224 },
225 }
226 o.Channel <- msg
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100227 // terminate the ONU's ProcessOnuMessages Go routine
228 close(o.Channel)
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700229 },
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700230 "enter_auth_started": func(e *fsm.Event) {
231 o.logStateChange(e.Src, e.Dst)
232 msg := Message{
233 Type: StartEAPOL,
234 Data: PacketMessage{
235 PonPortID: o.PonPortID,
236 OnuID: o.ID,
237 },
238 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700239 o.Channel <- msg
Matteo Scandolo4747d292019-08-05 11:50:18 -0700240 },
Pragya Arya324337e2020-02-20 14:35:08 +0530241 "enter_eap_response_success_received": func(e *fsm.Event) {
242 publishEvent("ONU-authentication-done", int32(o.PonPortID), int32(o.ID), o.Sn())
243 },
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700244 "enter_auth_failed": func(e *fsm.Event) {
245 onuLogger.WithFields(log.Fields{
246 "OnuId": o.ID,
247 "IntfId": o.PonPortID,
248 "OnuSn": o.Sn(),
249 }).Errorf("ONU failed to authenticate!")
250 },
Matteo Scandolo99f18462019-10-28 14:14:28 -0700251 "before_start_dhcp": func(e *fsm.Event) {
252 if o.DhcpFlowReceived == false {
253 e.Cancel(errors.New("cannot-go-to-dhcp-started-as-dhcp-flow-is-missing"))
254 }
255 },
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700256 "enter_dhcp_started": func(e *fsm.Event) {
257 msg := Message{
258 Type: StartDHCP,
259 Data: PacketMessage{
260 PonPortID: o.PonPortID,
261 OnuID: o.ID,
262 },
263 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700264 o.Channel <- msg
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700265 },
Pragya Arya324337e2020-02-20 14:35:08 +0530266 "enter_dhcp_ack_received": func(e *fsm.Event) {
267 publishEvent("ONU-DHCP-ACK-received", int32(o.PonPortID), int32(o.ID), o.Sn())
268 },
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700269 "enter_dhcp_failed": func(e *fsm.Event) {
270 onuLogger.WithFields(log.Fields{
271 "OnuId": o.ID,
272 "IntfId": o.PonPortID,
273 "OnuSn": o.Sn(),
274 }).Errorf("ONU failed to DHCP!")
275 },
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700276 "enter_eapol_flow_sent": func(e *fsm.Event) {
277 msg := Message{
278 Type: SendEapolFlow,
279 }
280 o.Channel <- msg
281 },
282 "enter_dhcp_flow_sent": func(e *fsm.Event) {
283 msg := Message{
284 Type: SendDhcpFlow,
285 }
286 o.Channel <- msg
287 },
Arjun E K57a7fcb2020-01-30 06:44:45 +0000288 "igmp_join_start": func(e *fsm.Event) {
289 msg := Message{
290 Type: IGMPMembershipReportV2,
291 }
292 o.Channel <- msg
293 },
294 "igmp_leave": func(e *fsm.Event) {
295 msg := Message{
296 Type: IGMPLeaveGroup}
297 o.Channel <- msg
298 },
Anand S Katti09541352020-01-29 15:54:01 +0530299 "igmp_join_startv3": func(e *fsm.Event) {
300 msg := Message{
301 Type: IGMPMembershipReportV3,
302 }
303 o.Channel <- msg
304 },
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700305 },
306 )
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100307
Matteo Scandolo27428702019-10-11 16:21:16 -0700308 return &o
Matteo Scandolo4747d292019-08-05 11:50:18 -0700309}
310
William Kurkian0418bc82019-11-06 12:16:24 -0500311func (o *Onu) logStateChange(src string, dst string) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700312 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700313 "OnuId": o.ID,
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700314 "IntfId": o.PonPortID,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700315 "OnuSn": o.Sn(),
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700316 }).Debugf("Changing ONU InternalState from %s to %s", src, dst)
317}
318
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100319// ProcessOnuMessages starts indication channel for each ONU
David Bainbridge103cf022019-12-16 20:11:35 +0000320func (o *Onu) ProcessOnuMessages(ctx context.Context, stream openolt.Openolt_EnableIndicationServer, client openolt.OpenoltClient) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700321 onuLogger.WithFields(log.Fields{
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100322 "onuID": o.ID,
323 "onuSN": o.Sn(),
324 "ponPort": o.PonPortID,
325 }).Debug("Starting ONU Indication Channel")
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700326
David Bainbridge103cf022019-12-16 20:11:35 +0000327loop:
328 for {
329 select {
330 case <-ctx.Done():
331 onuLogger.WithFields(log.Fields{
332 "onuID": o.ID,
333 "onuSN": o.Sn(),
334 }).Tracef("ONU message handling canceled via context")
335 break loop
336 case message, ok := <-o.Channel:
337 if !ok || ctx.Err() != nil {
338 onuLogger.WithFields(log.Fields{
339 "onuID": o.ID,
340 "onuSN": o.Sn(),
341 }).Tracef("ONU message handling canceled via channel close")
342 break loop
Matteo Scandolo075b1892019-10-07 12:11:07 -0700343 }
David Bainbridge103cf022019-12-16 20:11:35 +0000344 onuLogger.WithFields(log.Fields{
345 "onuID": o.ID,
346 "onuSN": o.Sn(),
347 "messageType": message.Type,
348 }).Tracef("Received message on ONU Channel")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700349
David Bainbridge103cf022019-12-16 20:11:35 +0000350 switch message.Type {
351 case OnuDiscIndication:
352 msg, _ := message.Data.(OnuDiscIndicationMessage)
353 // 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 +0530354 time.Sleep(o.DiscoveryDelay)
David Bainbridge103cf022019-12-16 20:11:35 +0000355 o.sendOnuDiscIndication(msg, stream)
356 case OnuIndication:
357 msg, _ := message.Data.(OnuIndicationMessage)
358 o.sendOnuIndication(msg, stream)
359 case OMCI:
360 msg, _ := message.Data.(OmciMessage)
361 o.handleOmciMessage(msg, stream)
362 case FlowUpdate:
363 msg, _ := message.Data.(OnuFlowUpdateMessage)
364 o.handleFlowUpdate(msg)
365 case StartEAPOL:
366 log.Infof("Receive StartEAPOL message on ONU Channel")
367 eapol.SendEapStart(o.ID, o.PonPortID, o.Sn(), o.PortNo, o.HwAddress, o.InternalState, stream)
368 case StartDHCP:
369 log.Infof("Receive StartDHCP message on ONU Channel")
370 // FIXME use id, ponId as SendEapStart
Matteo Scandolo378b8c92020-04-16 14:34:22 -0700371 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 +0000372 case OnuPacketOut:
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700373
David Bainbridge103cf022019-12-16 20:11:35 +0000374 msg, _ := message.Data.(OnuPacketMessage)
375
376 log.WithFields(log.Fields{
377 "IntfId": msg.IntfId,
378 "OnuId": msg.OnuId,
379 "pktType": msg.Type,
380 }).Trace("Received OnuPacketOut Message")
381
382 if msg.Type == packetHandlers.EAPOL {
383 eapol.HandleNextPacket(msg.OnuId, msg.IntfId, o.Sn(), o.PortNo, o.InternalState, msg.Packet, stream, client)
384 } else if msg.Type == packetHandlers.DHCP {
385 // NOTE here we receive packets going from the DHCP Server to the ONU
386 // for now we expect them to be double-tagged, but ideally the should be single tagged
Matteo Scandolo378b8c92020-04-16 14:34:22 -0700387 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 +0000388 }
389 case OnuPacketIn:
390 // NOTE we only receive BBR packets here.
391 // Eapol.HandleNextPacket can handle both BBSim and BBr cases so the call is the same
392 // in the DHCP case VOLTHA only act as a proxy, the behaviour is completely different thus we have a dhcp.HandleNextBbrPacket
393 msg, _ := message.Data.(OnuPacketMessage)
394
395 log.WithFields(log.Fields{
396 "IntfId": msg.IntfId,
397 "OnuId": msg.OnuId,
398 "pktType": msg.Type,
399 }).Trace("Received OnuPacketIn Message")
400
401 if msg.Type == packetHandlers.EAPOL {
402 eapol.HandleNextPacket(msg.OnuId, msg.IntfId, o.Sn(), o.PortNo, o.InternalState, msg.Packet, stream, client)
403 } else if msg.Type == packetHandlers.DHCP {
404 dhcp.HandleNextBbrPacket(o.ID, o.PonPortID, o.Sn(), o.STag, o.HwAddress, o.DoneChannel, msg.Packet, client)
405 }
David Bainbridge103cf022019-12-16 20:11:35 +0000406 case OmciIndication:
407 msg, _ := message.Data.(OmciIndicationMessage)
408 o.handleOmci(msg, client)
409 case SendEapolFlow:
410 o.sendEapolFlow(client)
411 case SendDhcpFlow:
412 o.sendDhcpFlow(client)
Arjun E K57a7fcb2020-01-30 06:44:45 +0000413 case IGMPMembershipReportV2:
414 log.Infof("Recieved IGMPMembershipReportV2 message on ONU channel")
415 igmp.SendIGMPMembershipReportV2(o.PonPortID, o.ID, o.Sn(), o.PortNo, o.HwAddress, stream)
416 case IGMPLeaveGroup:
417 log.Infof("Recieved IGMPLeaveGroupV2 message on ONU channel")
418 igmp.SendIGMPLeaveGroupV2(o.PonPortID, o.ID, o.Sn(), o.PortNo, o.HwAddress, stream)
Anand S Katti09541352020-01-29 15:54:01 +0530419 case IGMPMembershipReportV3:
420 log.Infof("Recieved IGMPMembershipReportV3 message on ONU channel")
421 igmp.SendIGMPMembershipReportV3(o.PonPortID, o.ID, o.Sn(), o.PortNo, o.HwAddress, stream)
David Bainbridge103cf022019-12-16 20:11:35 +0000422 default:
423 onuLogger.Warnf("Received unknown message data %v for type %v in OLT Channel", message.Data, message.Type)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700424 }
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700425 }
426 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100427 onuLogger.WithFields(log.Fields{
428 "onuID": o.ID,
429 "onuSN": o.Sn(),
430 }).Debug("Stopped handling ONU Indication Channel")
Matteo Scandolo4747d292019-08-05 11:50:18 -0700431}
432
Matteo Scandolodf3f85d2020-01-15 12:50:48 -0800433func (o *Onu) processOmciMessage(message omcisim.OmciChMessage, stream openolt.Openolt_EnableIndicationServer) {
William Kurkian9dadc5b2019-10-22 13:51:57 -0400434 switch message.Type {
Matteo Scandolodf3f85d2020-01-15 12:50:48 -0800435 case omcisim.UniLinkUp, omcisim.UniLinkDown:
436 onuLogger.WithFields(log.Fields{
437 "OnuId": message.Data.OnuId,
438 "IntfId": message.Data.IntfId,
439 "Type": message.Type,
440 }).Infof("UNI Link Alarm")
441 // TODO send to OLT
442
443 omciInd := openolt.OmciIndication{
444 IntfId: message.Data.IntfId,
445 OnuId: message.Data.OnuId,
446 Pkt: message.Packet,
447 }
448
449 omci := &openolt.Indication_OmciInd{OmciInd: &omciInd}
450 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
451 onuLogger.WithFields(log.Fields{
452 "IntfId": o.PonPortID,
453 "SerialNumber": o.Sn(),
454 "Type": message.Type,
455 "omciPacket": omciInd.Pkt,
456 }).Errorf("Failed to send UNI Link Alarm: %v", err)
457 return
458 }
459
460 onuLogger.WithFields(log.Fields{
461 "IntfId": o.PonPortID,
462 "SerialNumber": o.Sn(),
463 "Type": message.Type,
464 "omciPacket": omciInd.Pkt,
465 }).Info("UNI Link alarm sent")
466
William Kurkian9dadc5b2019-10-22 13:51:57 -0400467 case omcisim.GemPortAdded:
468 log.WithFields(log.Fields{
469 "OnuId": message.Data.OnuId,
470 "IntfId": message.Data.IntfId,
Matteo Scandolod74abba2020-04-16 16:36:44 -0700471 "OnuSn": o.Sn(),
William Kurkian9dadc5b2019-10-22 13:51:57 -0400472 }).Infof("GemPort Added")
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700473
William Kurkian9dadc5b2019-10-22 13:51:57 -0400474 // NOTE if we receive the GemPort but we don't have EAPOL flows
475 // go an intermediate state, otherwise start auth
476 if o.InternalState.Is("enabled") {
477 if err := o.InternalState.Event("add_gem_port"); err != nil {
478 log.Errorf("Can't go to gem_port_added: %v", err)
479 }
480 } else if o.InternalState.Is("eapol_flow_received") {
Matteo Scandolo3c656a12019-12-10 09:54:51 -0800481 if o.Auth == true {
482 if err := o.InternalState.Event("start_auth"); err != nil {
483 log.Warnf("Can't go to auth_started: %v", err)
484 }
485 } else {
486 onuLogger.WithFields(log.Fields{
487 "IntfId": o.PonPortID,
488 "OnuId": o.ID,
489 "SerialNumber": o.Sn(),
490 }).Warn("Not starting authentication as Auth bit is not set in CLI parameters")
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700491 }
492 }
493 }
494}
495
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100496func (o Onu) NewSN(oltid int, intfid uint32, onuid uint32) *openolt.SerialNumber {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700497
498 sn := new(openolt.SerialNumber)
499
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700500 //sn = new(openolt.SerialNumber)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700501 sn.VendorId = []byte("BBSM")
502 sn.VendorSpecific = []byte{0, byte(oltid % 256), byte(intfid), byte(onuid)}
503
504 return sn
505}
506
William Kurkian0418bc82019-11-06 12:16:24 -0500507func (o *Onu) sendOnuDiscIndication(msg OnuDiscIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700508 discoverData := &openolt.Indication_OnuDiscInd{OnuDiscInd: &openolt.OnuDiscIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700509 IntfId: msg.Onu.PonPortID,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700510 SerialNumber: msg.Onu.SerialNumber,
511 }}
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700512
Matteo Scandolo4747d292019-08-05 11:50:18 -0700513 if err := stream.Send(&openolt.Indication{Data: discoverData}); err != nil {
Matteo Scandolo11006992019-08-28 11:29:46 -0700514 log.Errorf("Failed to send Indication_OnuDiscInd: %v", err)
Matteo Scandolo99f18462019-10-28 14:14:28 -0700515 return
Matteo Scandolo4747d292019-08-05 11:50:18 -0700516 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700517
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700518 onuLogger.WithFields(log.Fields{
Matteo Scandolo4747d292019-08-05 11:50:18 -0700519 "IntfId": msg.Onu.PonPortID,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700520 "OnuSn": msg.Onu.Sn(),
521 "OnuId": o.ID,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700522 }).Debug("Sent Indication_OnuDiscInd")
Pragya Arya324337e2020-02-20 14:35:08 +0530523 publishEvent("ONU-discovery-indication-sent", int32(msg.Onu.PonPortID), int32(o.ID), msg.Onu.Sn())
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800524
525 // after DiscoveryRetryDelay check if the state is the same and in case send a new OnuDiscIndication
526 go func(delay time.Duration) {
Matteo Scandolo569e7172019-12-20 11:51:51 -0800527 time.Sleep(delay)
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800528 if o.InternalState.Current() == "discovered" {
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800529 o.sendOnuDiscIndication(msg, stream)
530 }
531 }(o.DiscoveryRetryDelay)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700532}
533
William Kurkian0418bc82019-11-06 12:16:24 -0500534func (o *Onu) sendOnuIndication(msg OnuIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700535 // NOTE voltha returns an ID, but if we use that ID then it complains:
536 // expected_onu_id: 1, received_onu_id: 1024, event: ONU-id-mismatch, can happen if both voltha and the olt rebooted
537 // so we're using the internal ID that is 1
538 // o.ID = msg.OnuID
Matteo Scandolo4747d292019-08-05 11:50:18 -0700539
540 indData := &openolt.Indication_OnuInd{OnuInd: &openolt.OnuIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700541 IntfId: o.PonPortID,
542 OnuId: o.ID,
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700543 OperState: msg.OperState.String(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700544 AdminState: o.OperState.Current(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700545 SerialNumber: o.SerialNumber,
546 }}
547 if err := stream.Send(&openolt.Indication{Data: indData}); err != nil {
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800548 // NOTE do we need to transition to a broken state?
Matteo Scandolo11006992019-08-28 11:29:46 -0700549 log.Errorf("Failed to send Indication_OnuInd: %v", err)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700550 }
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700551 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700552 "IntfId": o.PonPortID,
553 "OnuId": o.ID,
554 "OperState": msg.OperState.String(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700555 "AdminState": msg.OperState.String(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700556 "OnuSn": o.Sn(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700557 }).Debug("Sent Indication_OnuInd")
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700558
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700559}
560
Pragya Arya324337e2020-02-20 14:35:08 +0530561func (o *Onu) publishOmciEvent(msg OmciMessage) {
562 if olt.PublishEvents {
563 _, _, msgType, _, _, _, err := omcisim.ParsePkt(HexDecode(msg.omciMsg.Pkt))
564 if err != nil {
565 log.Errorf("error in getting msgType %v", err)
566 return
567 }
568 if msgType == omcisim.MibUpload {
569 o.seqNumber = 0
570 publishEvent("MIB-upload-received", int32(o.PonPortID), int32(o.ID), common.OnuSnToString(o.SerialNumber))
571 } else if msgType == omcisim.MibUploadNext {
572 o.seqNumber++
573 if o.seqNumber > 290 {
574 publishEvent("MIB-upload-done", int32(o.PonPortID), int32(o.ID), common.OnuSnToString(o.SerialNumber))
575 }
576 }
577 }
578}
579
Scott Bakerb90c4312020-03-12 21:33:25 -0700580// Create a TestResponse packet and send it
581func (o *Onu) sendTestResult(msg OmciMessage, stream openolt.Openolt_EnableIndicationServer) error {
582 resp, err := omcilib.BuildTestResult(HexDecode(msg.omciMsg.Pkt))
583 if err != nil {
584 return err
585 }
586
587 var omciInd openolt.OmciIndication
588 omciInd.IntfId = o.PonPortID
589 omciInd.OnuId = o.ID
590 omciInd.Pkt = resp
591
592 omci := &openolt.Indication_OmciInd{OmciInd: &omciInd}
593 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
594 onuLogger.WithFields(log.Fields{
595 "IntfId": o.PonPortID,
596 "SerialNumber": o.Sn(),
597 "omciPacket": omciInd.Pkt,
598 "msg": msg,
599 }).Errorf("send TestResult omcisim indication failed: %v", err)
600 return err
601 }
602 onuLogger.WithFields(log.Fields{
603 "IntfId": o.PonPortID,
604 "SerialNumber": o.Sn(),
605 "omciPacket": omciInd.Pkt,
606 }).Tracef("Sent TestResult OMCI message")
607
608 return nil
609}
610
William Kurkian0418bc82019-11-06 12:16:24 -0500611func (o *Onu) handleOmciMessage(msg OmciMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700612
613 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700614 "IntfId": o.PonPortID,
Matteo Scandolo27428702019-10-11 16:21:16 -0700615 "SerialNumber": o.Sn(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700616 "omciPacket": msg.omciMsg.Pkt,
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700617 }).Tracef("Received OMCI message")
618
Pragya Arya324337e2020-02-20 14:35:08 +0530619 o.publishOmciEvent(msg)
620
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700621 var omciInd openolt.OmciIndication
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700622 respPkt, err := omcisim.OmciSim(o.PonPortID, o.ID, HexDecode(msg.omciMsg.Pkt))
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700623 if err != nil {
Matteo Scandolo27428702019-10-11 16:21:16 -0700624 onuLogger.WithFields(log.Fields{
625 "IntfId": o.PonPortID,
626 "SerialNumber": o.Sn(),
627 "omciPacket": omciInd.Pkt,
628 "msg": msg,
629 }).Errorf("Error handling OMCI message %v", msg)
630 return
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700631 }
632
633 omciInd.IntfId = o.PonPortID
634 omciInd.OnuId = o.ID
635 omciInd.Pkt = respPkt
636
637 omci := &openolt.Indication_OmciInd{OmciInd: &omciInd}
638 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
Matteo Scandolo27428702019-10-11 16:21:16 -0700639 onuLogger.WithFields(log.Fields{
640 "IntfId": o.PonPortID,
641 "SerialNumber": o.Sn(),
642 "omciPacket": omciInd.Pkt,
643 "msg": msg,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700644 }).Errorf("send omcisim indication failed: %v", err)
Matteo Scandolo27428702019-10-11 16:21:16 -0700645 return
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700646 }
647 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700648 "IntfId": o.PonPortID,
Matteo Scandolo27428702019-10-11 16:21:16 -0700649 "SerialNumber": o.Sn(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700650 "omciPacket": omciInd.Pkt,
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700651 }).Tracef("Sent OMCI message")
Scott Bakerb90c4312020-03-12 21:33:25 -0700652
653 // Test message is special, it requires sending two packets:
654 // first packet: TestResponse, says whether test was started successully, handled by omci-sim
655 // second packet, TestResult, reports the result of running the self-test
656 // TestResult can come some time after a TestResponse
657 // TODO: Implement some delay between the TestResponse and the TestResult
658 isTest, err := omcilib.IsTestRequest(HexDecode(msg.omciMsg.Pkt))
659 if (err == nil) && (isTest) {
660 o.sendTestResult(msg, stream)
661 }
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700662}
663
Matteo Scandolo27428702019-10-11 16:21:16 -0700664func (o *Onu) storePortNumber(portNo uint32) {
Matteo Scandolo813402b2019-10-23 19:24:52 -0700665 // NOTE this needed only as long as we don't support multiple UNIs
Matteo Scandolo27428702019-10-11 16:21:16 -0700666 // we need to add support for multiple UNIs
667 // the action plan is:
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700668 // - refactor the omcisim-sim library to use https://github.com/cboling/omci instead of canned messages
Matteo Scandolo27428702019-10-11 16:21:16 -0700669 // - change the library so that it reports a single UNI and remove this workaroung
670 // - add support for multiple UNIs in BBSim
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700671 if o.PortNo == 0 || portNo < o.PortNo {
Matteo Scandolo813402b2019-10-23 19:24:52 -0700672 onuLogger.WithFields(log.Fields{
673 "IntfId": o.PonPortID,
674 "OnuId": o.ID,
675 "SerialNumber": o.Sn(),
676 "OnuPortNo": o.PortNo,
677 "FlowPortNo": portNo,
678 }).Debug("Storing ONU portNo")
Matteo Scandolo27428702019-10-11 16:21:16 -0700679 o.PortNo = portNo
680 }
681}
682
William Kurkian0418bc82019-11-06 12:16:24 -0500683func (o *Onu) SetID(id uint32) {
Matteo Scandolo583f17d2020-02-13 10:35:17 -0800684 onuLogger.WithFields(log.Fields{
685 "IntfId": o.PonPortID,
686 "OnuId": id,
687 "SerialNumber": o.Sn(),
688 }).Debug("Storing OnuId ")
William Kurkian0418bc82019-11-06 12:16:24 -0500689 o.ID = id
690}
691
Matteo Scandolo813402b2019-10-23 19:24:52 -0700692func (o *Onu) handleFlowUpdate(msg OnuFlowUpdateMessage) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700693 onuLogger.WithFields(log.Fields{
Matteo Scandolod7cc6d32020-02-26 16:51:12 -0800694 "DstPort": msg.Flow.Classifier.DstPort,
695 "EthType": fmt.Sprintf("%x", msg.Flow.Classifier.EthType),
696 "FlowId": msg.Flow.FlowId,
697 "FlowType": msg.Flow.FlowType,
698 "GemportId": msg.Flow.GemportId,
699 "InnerVlan": msg.Flow.Classifier.IVid,
700 "IntfId": msg.Flow.AccessIntfId,
701 "IpProto": msg.Flow.Classifier.IpProto,
702 "OnuId": msg.Flow.OnuId,
703 "OnuSn": o.Sn(),
704 "OuterVlan": msg.Flow.Classifier.OVid,
705 "PortNo": msg.Flow.PortNo,
706 "SrcPort": msg.Flow.Classifier.SrcPort,
707 "UniID": msg.Flow.UniId,
Matteo Scandolo3ac52792020-03-05 14:21:08 -0800708 "ClassifierOPbits": msg.Flow.Classifier.OPbits,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700709 }).Debug("ONU receives Flow")
710
Matteo Scandolo813402b2019-10-23 19:24:52 -0700711 if msg.Flow.UniId != 0 {
712 // as of now BBSim only support a single UNI, so ignore everything that is not targeted to it
713 onuLogger.WithFields(log.Fields{
714 "IntfId": o.PonPortID,
715 "OnuId": o.ID,
716 "SerialNumber": o.Sn(),
717 }).Debug("Ignoring flow as it's not for the first UNI")
718 return
719 }
720
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700721 if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeEAPOL) && msg.Flow.Classifier.OVid == 4091 {
Matteo Scandolo27428702019-10-11 16:21:16 -0700722 // NOTE storing the PortNO, it's needed when sending PacketIndications
Matteo Scandolo813402b2019-10-23 19:24:52 -0700723 o.storePortNumber(uint32(msg.Flow.PortNo))
Matteo Scandolo27428702019-10-11 16:21:16 -0700724
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700725 // NOTE if we receive the EAPOL flows but we don't have GemPorts
726 // go an intermediate state, otherwise start auth
727 if o.InternalState.Is("enabled") {
728 if err := o.InternalState.Event("receive_eapol_flow"); err != nil {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700729 log.Warnf("Can't go to eapol_flow_received: %v", err)
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700730 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700731 } else if o.InternalState.Is("gem_port_added") {
Matteo Scandoloc1147092019-10-29 09:38:33 -0700732
733 if o.Auth == true {
734 if err := o.InternalState.Event("start_auth"); err != nil {
735 log.Warnf("Can't go to auth_started: %v", err)
736 }
737 } else {
738 onuLogger.WithFields(log.Fields{
739 "IntfId": o.PonPortID,
740 "OnuId": o.ID,
741 "SerialNumber": o.Sn(),
742 }).Warn("Not starting authentication as Auth bit is not set in CLI parameters")
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700743 }
Matteo Scandoloc1147092019-10-29 09:38:33 -0700744
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700745 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700746 } else if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeIPv4) &&
747 msg.Flow.Classifier.SrcPort == uint32(68) &&
Matteo Scandolo3ac52792020-03-05 14:21:08 -0800748 msg.Flow.Classifier.DstPort == uint32(67) &&
Matteo Scandolod74abba2020-04-16 16:36:44 -0700749 (msg.Flow.Classifier.OPbits == 0 || msg.Flow.Classifier.OPbits == 255) {
Matteo Scandolo99f18462019-10-28 14:14:28 -0700750
Matteo Scandoloc1147092019-10-29 09:38:33 -0700751
752 if o.Dhcp == true {
Matteo Scandolod74abba2020-04-16 16:36:44 -0700753 if o.DhcpFlowReceived == false {
754 // keep track that we received the DHCP Flows
755 // so that we can transition the state to dhcp_started
756 // this is needed as a check in case someone trigger DHCP from the CLI
757 o.DhcpFlowReceived = true
758
759 // now start the DHCP state machine
760 if err := o.InternalState.Event("start_dhcp"); err != nil {
761 log.Errorf("Can't go to dhcp_started: %v", err)
762 }
763 } else {
764 onuLogger.WithFields(log.Fields{
765 "IntfId": o.PonPortID,
766 "OnuId": o.ID,
767 "SerialNumber": o.Sn(),
768 "DhcpFlowReceived": o.DhcpFlowReceived,
769 }).Warn("DHCP already started")
Matteo Scandoloc1147092019-10-29 09:38:33 -0700770 }
771 } else {
772 onuLogger.WithFields(log.Fields{
773 "IntfId": o.PonPortID,
774 "OnuId": o.ID,
775 "SerialNumber": o.Sn(),
776 }).Warn("Not starting DHCP as Dhcp bit is not set in CLI parameters")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700777 }
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700778 }
779}
780
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700781// HexDecode converts the hex encoding to binary
782func HexDecode(pkt []byte) []byte {
783 p := make([]byte, len(pkt)/2)
784 for i, j := 0, 0; i < len(pkt); i, j = i+2, j+1 {
785 // Go figure this ;)
786 u := (pkt[i] & 15) + (pkt[i]>>6)*9
787 l := (pkt[i+1] & 15) + (pkt[i+1]>>6)*9
788 p[j] = u<<4 + l
789 }
790 onuLogger.Tracef("Omci decoded: %x.", p)
791 return p
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700792}
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700793
794// BBR methods
795
796func sendOmciMsg(pktBytes []byte, intfId uint32, onuId uint32, sn *openolt.SerialNumber, msgType string, client openolt.OpenoltClient) {
797 omciMsg := openolt.OmciMsg{
798 IntfId: intfId,
799 OnuId: onuId,
800 Pkt: pktBytes,
801 }
802
803 if _, err := client.OmciMsgOut(context.Background(), &omciMsg); err != nil {
804 log.WithFields(log.Fields{
805 "IntfId": intfId,
806 "OnuId": onuId,
807 "SerialNumber": common.OnuSnToString(sn),
808 "Pkt": omciMsg.Pkt,
809 }).Fatalf("Failed to send MIB Reset")
810 }
811 log.WithFields(log.Fields{
812 "IntfId": intfId,
813 "OnuId": onuId,
814 "SerialNumber": common.OnuSnToString(sn),
815 "Pkt": omciMsg.Pkt,
816 }).Tracef("Sent OMCI message %s", msgType)
817}
818
819func (onu *Onu) getNextTid(highPriority ...bool) uint16 {
820 var next uint16
821 if len(highPriority) > 0 && highPriority[0] {
822 next = onu.hpTid
823 onu.hpTid += 1
824 if onu.hpTid < 0x8000 {
825 onu.hpTid = 0x8000
826 }
827 } else {
828 next = onu.tid
829 onu.tid += 1
830 if onu.tid >= 0x8000 {
831 onu.tid = 1
832 }
833 }
834 return next
835}
836
837// TODO move this method in responders/omcisim
838func (o *Onu) StartOmci(client openolt.OpenoltClient) {
839 mibReset, _ := omcilib.CreateMibResetRequest(o.getNextTid(false))
840 sendOmciMsg(mibReset, o.PonPortID, o.ID, o.SerialNumber, "mibReset", client)
841}
842
843func (o *Onu) handleOmci(msg OmciIndicationMessage, client openolt.OpenoltClient) {
844 msgType, packet := omcilib.DecodeOmci(msg.OmciInd.Pkt)
845
846 log.WithFields(log.Fields{
847 "IntfId": msg.OmciInd.IntfId,
848 "OnuId": msg.OmciInd.OnuId,
849 "OnuSn": common.OnuSnToString(o.SerialNumber),
850 "Pkt": msg.OmciInd.Pkt,
851 "msgType": msgType,
Anand S Katti09541352020-01-29 15:54:01 +0530852 }).Trace("ONU Receives OMCI Msg")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700853 switch msgType {
854 default:
Matteo Scandolo813402b2019-10-23 19:24:52 -0700855 log.WithFields(log.Fields{
856 "IntfId": msg.OmciInd.IntfId,
857 "OnuId": msg.OmciInd.OnuId,
858 "OnuSn": common.OnuSnToString(o.SerialNumber),
859 "Pkt": msg.OmciInd.Pkt,
860 "msgType": msgType,
861 }).Fatalf("unexpected frame: %v", packet)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700862 case omci.MibResetResponseType:
863 mibUpload, _ := omcilib.CreateMibUploadRequest(o.getNextTid(false))
864 sendOmciMsg(mibUpload, o.PonPortID, o.ID, o.SerialNumber, "mibUpload", client)
865 case omci.MibUploadResponseType:
866 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
867 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
868 case omci.MibUploadNextResponseType:
869 o.seqNumber++
870
871 if o.seqNumber > 290 {
872 // NOTE we are done with the MIB Upload (290 is the number of messages the omci-sim library will respond to)
873 galEnet, _ := omcilib.CreateGalEnetRequest(o.getNextTid(false))
874 sendOmciMsg(galEnet, o.PonPortID, o.ID, o.SerialNumber, "CreateGalEnetRequest", client)
875 } else {
876 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
877 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
878 }
879 case omci.CreateResponseType:
880 // NOTE Creating a GemPort,
881 // BBsim actually doesn't care about the values, so we can do we want with the parameters
882 // In the same way we can create a GemPort even without setting up UNIs/TConts/...
883 // but we need the GemPort to trigger the state change
884
885 if !o.HasGemPort {
886 // NOTE this sends a CreateRequestType and BBSim replies with a CreateResponseType
887 // thus we send this request only once
888 gemReq, _ := omcilib.CreateGemPortRequest(o.getNextTid(false))
889 sendOmciMsg(gemReq, o.PonPortID, o.ID, o.SerialNumber, "CreateGemPortRequest", client)
890 o.HasGemPort = true
891 } else {
892 if err := o.InternalState.Event("send_eapol_flow"); err != nil {
893 onuLogger.WithFields(log.Fields{
894 "OnuId": o.ID,
895 "IntfId": o.PonPortID,
896 "OnuSn": o.Sn(),
897 }).Errorf("Error while transitioning ONU State %v", err)
898 }
899 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700900 }
901}
902
903func (o *Onu) sendEapolFlow(client openolt.OpenoltClient) {
904
905 classifierProto := openolt.Classifier{
906 EthType: uint32(layers.EthernetTypeEAPOL),
907 OVid: 4091,
908 }
909
910 actionProto := openolt.Action{}
911
912 downstreamFlow := openolt.Flow{
913 AccessIntfId: int32(o.PonPortID),
914 OnuId: int32(o.ID),
Matteo Scandolo813402b2019-10-23 19:24:52 -0700915 UniId: int32(0), // NOTE do not hardcode this, we need to support multiple UNIs
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700916 FlowId: uint32(o.ID),
917 FlowType: "downstream",
918 AllocId: int32(0),
919 NetworkIntfId: int32(0),
920 GemportId: int32(1), // FIXME use the same value as CreateGemPortRequest PortID, do not hardcode
921 Classifier: &classifierProto,
922 Action: &actionProto,
923 Priority: int32(100),
924 Cookie: uint64(o.ID),
925 PortNo: uint32(o.ID), // NOTE we are using this to map an incoming packetIndication to an ONU
926 }
927
928 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
929 log.WithFields(log.Fields{
930 "IntfId": o.PonPortID,
931 "OnuId": o.ID,
932 "FlowId": downstreamFlow.FlowId,
933 "PortNo": downstreamFlow.PortNo,
934 "SerialNumber": common.OnuSnToString(o.SerialNumber),
Matteo Scandolob0e3e622020-04-23 17:00:29 -0700935 }).Fatalf("Failed to add EAPOL Flow")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700936 }
937 log.WithFields(log.Fields{
938 "IntfId": o.PonPortID,
939 "OnuId": o.ID,
940 "FlowId": downstreamFlow.FlowId,
941 "PortNo": downstreamFlow.PortNo,
942 "SerialNumber": common.OnuSnToString(o.SerialNumber),
943 }).Info("Sent EAPOL Flow")
944}
945
946func (o *Onu) sendDhcpFlow(client openolt.OpenoltClient) {
947 classifierProto := openolt.Classifier{
948 EthType: uint32(layers.EthernetTypeIPv4),
949 SrcPort: uint32(68),
950 DstPort: uint32(67),
951 }
952
953 actionProto := openolt.Action{}
954
955 downstreamFlow := openolt.Flow{
956 AccessIntfId: int32(o.PonPortID),
957 OnuId: int32(o.ID),
Matteo Scandolo813402b2019-10-23 19:24:52 -0700958 UniId: int32(0), // FIXME do not hardcode this
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700959 FlowId: uint32(o.ID),
960 FlowType: "downstream",
961 AllocId: int32(0),
962 NetworkIntfId: int32(0),
963 GemportId: int32(1), // FIXME use the same value as CreateGemPortRequest PortID, do not hardcode
964 Classifier: &classifierProto,
965 Action: &actionProto,
966 Priority: int32(100),
967 Cookie: uint64(o.ID),
968 PortNo: uint32(o.ID), // NOTE we are using this to map an incoming packetIndication to an ONU
969 }
970
971 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
972 log.WithFields(log.Fields{
973 "IntfId": o.PonPortID,
974 "OnuId": o.ID,
975 "FlowId": downstreamFlow.FlowId,
976 "PortNo": downstreamFlow.PortNo,
977 "SerialNumber": common.OnuSnToString(o.SerialNumber),
978 }).Fatalf("Failed to send DHCP Flow")
979 }
980 log.WithFields(log.Fields{
981 "IntfId": o.PonPortID,
982 "OnuId": o.ID,
983 "FlowId": downstreamFlow.FlowId,
984 "PortNo": downstreamFlow.PortNo,
985 "SerialNumber": common.OnuSnToString(o.SerialNumber),
986 }).Info("Sent DHCP Flow")
987}
Pragya Arya8bdb4532020-03-02 17:08:09 +0530988
989// DeleteFlow method search and delete flowKey from the onu flows slice
990func (onu *Onu) DeleteFlow(key FlowKey) {
991 for pos, flowKey := range onu.Flows {
992 if flowKey == key {
993 // delete the flowKey by shifting all flowKeys by one
994 onu.Flows = append(onu.Flows[:pos], onu.Flows[pos+1:]...)
995 t := make([]FlowKey, len(onu.Flows))
996 copy(t, onu.Flows)
997 onu.Flows = t
998 break
999 }
1000 }
1001}