blob: 1899f4c0e195fd5d2c891f1c83b1237b1e9530af [file] [log] [blame]
Matteo Scandolo11006992019-08-28 11:29:46 -07001/*
2 * Copyright 2018-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Matteo Scandolo4747d292019-08-05 11:50:18 -070017package devices
18
19import (
Matteo Scandolo40e067f2019-10-16 16:59:41 -070020 "context"
Matteo Scandolo99f18462019-10-28 14:14:28 -070021 "errors"
Matteo Scandolo3bc73742019-08-20 14:04:04 -070022 "fmt"
Zdravko Bozakov681364d2019-11-10 14:28:46 +010023 "net"
24
25 "time"
26
Matteo Scandolo40e067f2019-10-16 16:59:41 -070027 "github.com/cboling/omci"
Matteo Scandolo3bc73742019-08-20 14:04:04 -070028 "github.com/google/gopacket/layers"
Matteo Scandolo4747d292019-08-05 11:50:18 -070029 "github.com/looplab/fsm"
Matteo Scandolo075b1892019-10-07 12:11:07 -070030 "github.com/opencord/bbsim/internal/bbsim/packetHandlers"
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070031 "github.com/opencord/bbsim/internal/bbsim/responders/dhcp"
32 "github.com/opencord/bbsim/internal/bbsim/responders/eapol"
Arjun E K57a7fcb2020-01-30 06:44:45 +000033 "github.com/opencord/bbsim/internal/bbsim/responders/igmp"
Matteo Scandolo40e067f2019-10-16 16:59:41 -070034 "github.com/opencord/bbsim/internal/common"
35 omcilib "github.com/opencord/bbsim/internal/common/omci"
36 omcisim "github.com/opencord/omci-sim"
Matteo Scandolo3de9de02019-11-14 13:40:03 -080037 "github.com/opencord/voltha-protos/v2/go/openolt"
Anand S Katti09541352020-01-29 15:54:01 +053038 tech_profile "github.com/opencord/voltha-protos/v2/go/tech_profile"
Matteo Scandolo4747d292019-08-05 11:50:18 -070039 log "github.com/sirupsen/logrus"
40)
41
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070042var onuLogger = log.WithFields(log.Fields{
43 "module": "ONU",
44})
45
Pragya Arya8bdb4532020-03-02 17:08:09 +053046type FlowKey struct {
47 ID uint32
48 Direction string
49}
50
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070051type Onu struct {
Matteo Scandoloe811ae92019-12-10 17:50:14 -080052 ID uint32
53 PonPortID uint32
54 PonPort PonPort
55 STag int
56 CTag int
57 Auth bool // automatically start EAPOL if set to true
58 Dhcp bool // automatically start DHCP if set to true
59 HwAddress net.HardwareAddr
60 InternalState *fsm.FSM
Pragya Arya2225f202020-01-29 18:05:01 +053061 DiscoveryRetryDelay time.Duration // this is the time between subsequent Discovery Indication
62 DiscoveryDelay time.Duration // this is the time to send the first Discovery Indication
Matteo Scandoloe811ae92019-12-10 17:50:14 -080063
64 // ONU State
Matteo Scandolo27428702019-10-11 16:21:16 -070065 // PortNo comes with flows and it's used when sending packetIndications,
66 // There is one PortNo per UNI Port, for now we're only storing the first one
67 // FIXME add support for multiple UNIs
Matteo Scandolo99f18462019-10-28 14:14:28 -070068 PortNo uint32
69 DhcpFlowReceived bool
Pragya Arya8bdb4532020-03-02 17:08:09 +053070 Flows []FlowKey
Matteo Scandolo99f18462019-10-28 14:14:28 -070071
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070072 OperState *fsm.FSM
73 SerialNumber *openolt.SerialNumber
74
75 Channel chan Message // this Channel is to track state changes OMCI messages, EAPOL and DHCP packets
Matteo Scandolo40e067f2019-10-16 16:59:41 -070076
77 // OMCI params
78 tid uint16
79 hpTid uint16
80 seqNumber uint16
81 HasGemPort bool
82
Anand S Katti09541352020-01-29 15:54:01 +053083 DoneChannel chan bool // this channel is used to signal once the onu is complete (when the struct is used by BBR)
84 TrafficSchedulers *tech_profile.TrafficSchedulers
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070085}
86
Matteo Scandolo99f18462019-10-28 14:14:28 -070087func (o *Onu) Sn() string {
Matteo Scandolo40e067f2019-10-16 16:59:41 -070088 return common.OnuSnToString(o.SerialNumber)
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070089}
90
Pragya Arya2225f202020-01-29 18:05:01 +053091func CreateONU(olt *OltDevice, pon PonPort, id uint32, sTag int, cTag int, auth bool, dhcp bool, delay time.Duration, isMock bool) *Onu {
Matteo Scandolo4747d292019-08-05 11:50:18 -070092
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070093 o := Onu{
Pragya Arya2225f202020-01-29 18:05:01 +053094 ID: 0,
Matteo Scandoloe811ae92019-12-10 17:50:14 -080095 PonPortID: pon.ID,
96 PonPort: pon,
97 STag: sTag,
98 CTag: cTag,
99 Auth: auth,
100 Dhcp: dhcp,
101 HwAddress: net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, byte(pon.ID), byte(id)},
102 PortNo: 0,
103 tid: 0x1,
104 hpTid: 0x8000,
105 seqNumber: 0,
106 DoneChannel: make(chan bool, 1),
107 DhcpFlowReceived: false,
108 DiscoveryRetryDelay: 60 * time.Second, // this is used to send OnuDiscoveryIndications until an activate call is received
Pragya Arya8bdb4532020-03-02 17:08:09 +0530109 Flows: []FlowKey{},
Pragya Arya2225f202020-01-29 18:05:01 +0530110 DiscoveryDelay: delay,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700111 }
Pragya Arya2225f202020-01-29 18:05:01 +0530112 o.SerialNumber = o.NewSN(olt.ID, pon.ID, id)
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700113
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700114 // NOTE this state machine is used to track the operational
115 // state as requested by VOLTHA
116 o.OperState = getOperStateFSM(func(e *fsm.Event) {
117 onuLogger.WithFields(log.Fields{
118 "ID": o.ID,
119 }).Debugf("Changing ONU OperState from %s to %s", e.Src, e.Dst)
120 })
121
122 // NOTE this state machine is used to activate the OMCI, EAPOL and DHCP clients
123 o.InternalState = fsm.NewFSM(
124 "created",
125 fsm.Events{
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700126 // DEVICE Lifecycle
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100127 {Name: "initialize", Src: []string{"created", "disabled"}, Dst: "initialized"},
Pragya Arya6a708d62020-01-01 17:17:20 +0530128 {Name: "discover", Src: []string{"initialized", "pon_disabled"}, Dst: "discovered"},
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700129 {Name: "enable", Src: []string{"discovered", "disabled"}, Dst: "enabled"},
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700130 {Name: "receive_eapol_flow", Src: []string{"enabled", "gem_port_added"}, Dst: "eapol_flow_received"},
131 {Name: "add_gem_port", Src: []string{"enabled", "eapol_flow_received"}, Dst: "gem_port_added"},
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100132 // NOTE should disabled state be different for oper_disabled (emulating an error) and admin_disabled (received a disabled call via VOLTHA)?
Pragya Arya2225f202020-01-29 18:05:01 +0530133 {Name: "disable", Src: []string{"enabled", "eapol_flow_received", "gem_port_added", "eap_response_success_received", "auth_failed", "dhcp_ack_received", "dhcp_failed", "pon_disabled"}, Dst: "disabled"},
Pragya Arya6a708d62020-01-01 17:17:20 +0530134 // ONU state when PON port is disabled but ONU is power ON(more states should be added in src?)
Matteo Scandolodf3f85d2020-01-15 12:50:48 -0800135 {Name: "pon_disabled", Src: []string{"enabled", "gem_port_added", "eapol_flow_received", "eap_response_success_received", "auth_failed", "dhcp_ack_received", "dhcp_failed"}, Dst: "pon_disabled"},
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700136 // EAPOL
Shubham Sharmabd4b6572020-02-12 13:00:44 +0000137 {Name: "start_auth", Src: []string{"eapol_flow_received", "gem_port_added", "eap_start_sent", "eap_response_identity_sent", "eap_response_challenge_sent", "eap_response_success_received", "auth_failed", "dhcp_ack_received", "dhcp_failed", "igmp_join_started", "igmp_left", "igmp_join_error"}, Dst: "auth_started"},
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700138 {Name: "eap_start_sent", Src: []string{"auth_started"}, Dst: "eap_start_sent"},
139 {Name: "eap_response_identity_sent", Src: []string{"eap_start_sent"}, Dst: "eap_response_identity_sent"},
140 {Name: "eap_response_challenge_sent", Src: []string{"eap_response_identity_sent"}, Dst: "eap_response_challenge_sent"},
141 {Name: "eap_response_success_received", Src: []string{"eap_response_challenge_sent"}, Dst: "eap_response_success_received"},
142 {Name: "auth_failed", Src: []string{"auth_started", "eap_start_sent", "eap_response_identity_sent", "eap_response_challenge_sent"}, Dst: "auth_failed"},
143 // DHCP
Shubham Sharmabd4b6572020-02-12 13:00:44 +0000144 {Name: "start_dhcp", Src: []string{"eap_response_success_received", "dhcp_discovery_sent", "dhcp_request_sent", "dhcp_ack_received", "dhcp_failed", "igmp_join_started", "igmp_left", "igmp_join_error"}, Dst: "dhcp_started"},
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700145 {Name: "dhcp_discovery_sent", Src: []string{"dhcp_started"}, Dst: "dhcp_discovery_sent"},
146 {Name: "dhcp_request_sent", Src: []string{"dhcp_discovery_sent"}, Dst: "dhcp_request_sent"},
147 {Name: "dhcp_ack_received", Src: []string{"dhcp_request_sent"}, Dst: "dhcp_ack_received"},
148 {Name: "dhcp_failed", Src: []string{"dhcp_started", "dhcp_discovery_sent", "dhcp_request_sent"}, Dst: "dhcp_failed"},
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700149 // BBR States
150 // TODO add start OMCI state
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100151 {Name: "send_eapol_flow", Src: []string{"initialized"}, Dst: "eapol_flow_sent"},
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700152 {Name: "send_dhcp_flow", Src: []string{"eapol_flow_sent"}, Dst: "dhcp_flow_sent"},
Arjun E K57a7fcb2020-01-30 06:44:45 +0000153 // IGMP
Shubham Sharmabd4b6572020-02-12 13:00:44 +0000154 {Name: "igmp_join_start", Src: []string{"eap_response_success_received", "gem_port_added", "eapol_flow_received", "dhcp_ack_received", "igmp_left", "igmp_join_error"}, Dst: "igmp_join_started"},
Arjun E Kdd443f02020-02-07 15:24:01 +0000155 {Name: "igmp_join_startv3", Src: []string{"eap_response_success_received", "gem_port_added", "eapol_flow_received", "dhcp_ack_received", "igmp_left", "igmp_join_error"}, Dst: "igmp_join_started"},
Shubham Sharmabd4b6572020-02-12 13:00:44 +0000156 {Name: "igmp_join_error", Src: []string{"igmp_join_started"}, Dst: "igmp_join_error"},
157 {Name: "igmp_leave", Src: []string{"igmp_join_started", "gem_port_added", "eapol_flow_received", "eap_response_success_received", "dhcp_ack_received"}, Dst: "igmp_left"},
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700158 },
159 fsm.Callbacks{
160 "enter_state": func(e *fsm.Event) {
161 o.logStateChange(e.Src, e.Dst)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700162 },
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100163 "enter_initialized": func(e *fsm.Event) {
164 // create new channel for ProcessOnuMessages Go routine
165 o.Channel = make(chan Message, 2048)
Pragya Arya1cbefa42020-01-13 12:15:29 +0530166 if !isMock {
167 // start ProcessOnuMessages Go routine
168 go o.ProcessOnuMessages(olt.enableContext, *olt.OpenoltStream, nil)
169 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100170 },
171 "enter_discovered": func(e *fsm.Event) {
172 msg := Message{
173 Type: OnuDiscIndication,
174 Data: OnuDiscIndicationMessage{
175 Onu: &o,
176 OperState: UP,
177 },
178 }
179 o.Channel <- msg
180 },
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700181 "enter_enabled": func(event *fsm.Event) {
182 msg := Message{
183 Type: OnuIndication,
184 Data: OnuIndicationMessage{
185 OnuSN: o.SerialNumber,
186 PonPortID: o.PonPortID,
187 OperState: UP,
188 },
189 }
190 o.Channel <- msg
191 },
192 "enter_disabled": func(event *fsm.Event) {
193 msg := Message{
194 Type: OnuIndication,
195 Data: OnuIndicationMessage{
196 OnuSN: o.SerialNumber,
197 PonPortID: o.PonPortID,
198 OperState: DOWN,
199 },
200 }
201 o.Channel <- msg
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100202 // terminate the ONU's ProcessOnuMessages Go routine
203 close(o.Channel)
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700204 },
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700205 "enter_auth_started": func(e *fsm.Event) {
206 o.logStateChange(e.Src, e.Dst)
207 msg := Message{
208 Type: StartEAPOL,
209 Data: PacketMessage{
210 PonPortID: o.PonPortID,
211 OnuID: o.ID,
212 },
213 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700214 o.Channel <- msg
Matteo Scandolo4747d292019-08-05 11:50:18 -0700215 },
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700216 "enter_auth_failed": func(e *fsm.Event) {
217 onuLogger.WithFields(log.Fields{
218 "OnuId": o.ID,
219 "IntfId": o.PonPortID,
220 "OnuSn": o.Sn(),
221 }).Errorf("ONU failed to authenticate!")
222 },
Matteo Scandolo99f18462019-10-28 14:14:28 -0700223 "before_start_dhcp": func(e *fsm.Event) {
224 if o.DhcpFlowReceived == false {
225 e.Cancel(errors.New("cannot-go-to-dhcp-started-as-dhcp-flow-is-missing"))
226 }
227 },
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700228 "enter_dhcp_started": func(e *fsm.Event) {
229 msg := Message{
230 Type: StartDHCP,
231 Data: PacketMessage{
232 PonPortID: o.PonPortID,
233 OnuID: o.ID,
234 },
235 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700236 o.Channel <- msg
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700237 },
238 "enter_dhcp_failed": func(e *fsm.Event) {
239 onuLogger.WithFields(log.Fields{
240 "OnuId": o.ID,
241 "IntfId": o.PonPortID,
242 "OnuSn": o.Sn(),
243 }).Errorf("ONU failed to DHCP!")
244 },
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700245 "enter_eapol_flow_sent": func(e *fsm.Event) {
246 msg := Message{
247 Type: SendEapolFlow,
248 }
249 o.Channel <- msg
250 },
251 "enter_dhcp_flow_sent": func(e *fsm.Event) {
252 msg := Message{
253 Type: SendDhcpFlow,
254 }
255 o.Channel <- msg
256 },
Arjun E K57a7fcb2020-01-30 06:44:45 +0000257 "igmp_join_start": func(e *fsm.Event) {
258 msg := Message{
259 Type: IGMPMembershipReportV2,
260 }
261 o.Channel <- msg
262 },
263 "igmp_leave": func(e *fsm.Event) {
264 msg := Message{
265 Type: IGMPLeaveGroup}
266 o.Channel <- msg
267 },
Anand S Katti09541352020-01-29 15:54:01 +0530268 "igmp_join_startv3": func(e *fsm.Event) {
269 msg := Message{
270 Type: IGMPMembershipReportV3,
271 }
272 o.Channel <- msg
273 },
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700274 },
275 )
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100276
Matteo Scandolo27428702019-10-11 16:21:16 -0700277 return &o
Matteo Scandolo4747d292019-08-05 11:50:18 -0700278}
279
William Kurkian0418bc82019-11-06 12:16:24 -0500280func (o *Onu) logStateChange(src string, dst string) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700281 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700282 "OnuId": o.ID,
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700283 "IntfId": o.PonPortID,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700284 "OnuSn": o.Sn(),
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700285 }).Debugf("Changing ONU InternalState from %s to %s", src, dst)
286}
287
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100288// ProcessOnuMessages starts indication channel for each ONU
David Bainbridge103cf022019-12-16 20:11:35 +0000289func (o *Onu) ProcessOnuMessages(ctx context.Context, stream openolt.Openolt_EnableIndicationServer, client openolt.OpenoltClient) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700290 onuLogger.WithFields(log.Fields{
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100291 "onuID": o.ID,
292 "onuSN": o.Sn(),
293 "ponPort": o.PonPortID,
294 }).Debug("Starting ONU Indication Channel")
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700295
David Bainbridge103cf022019-12-16 20:11:35 +0000296loop:
297 for {
298 select {
299 case <-ctx.Done():
300 onuLogger.WithFields(log.Fields{
301 "onuID": o.ID,
302 "onuSN": o.Sn(),
303 }).Tracef("ONU message handling canceled via context")
304 break loop
305 case message, ok := <-o.Channel:
306 if !ok || ctx.Err() != nil {
307 onuLogger.WithFields(log.Fields{
308 "onuID": o.ID,
309 "onuSN": o.Sn(),
310 }).Tracef("ONU message handling canceled via channel close")
311 break loop
Matteo Scandolo075b1892019-10-07 12:11:07 -0700312 }
David Bainbridge103cf022019-12-16 20:11:35 +0000313 onuLogger.WithFields(log.Fields{
314 "onuID": o.ID,
315 "onuSN": o.Sn(),
316 "messageType": message.Type,
317 }).Tracef("Received message on ONU Channel")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700318
David Bainbridge103cf022019-12-16 20:11:35 +0000319 switch message.Type {
320 case OnuDiscIndication:
321 msg, _ := message.Data.(OnuDiscIndicationMessage)
322 // 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 +0530323 time.Sleep(o.DiscoveryDelay)
David Bainbridge103cf022019-12-16 20:11:35 +0000324 o.sendOnuDiscIndication(msg, stream)
325 case OnuIndication:
326 msg, _ := message.Data.(OnuIndicationMessage)
327 o.sendOnuIndication(msg, stream)
328 case OMCI:
329 msg, _ := message.Data.(OmciMessage)
330 o.handleOmciMessage(msg, stream)
331 case FlowUpdate:
332 msg, _ := message.Data.(OnuFlowUpdateMessage)
333 o.handleFlowUpdate(msg)
334 case StartEAPOL:
335 log.Infof("Receive StartEAPOL message on ONU Channel")
336 eapol.SendEapStart(o.ID, o.PonPortID, o.Sn(), o.PortNo, o.HwAddress, o.InternalState, stream)
337 case StartDHCP:
338 log.Infof("Receive StartDHCP message on ONU Channel")
339 // FIXME use id, ponId as SendEapStart
340 dhcp.SendDHCPDiscovery(o.PonPortID, o.ID, o.Sn(), o.PortNo, o.InternalState, o.HwAddress, o.CTag, stream)
341 case OnuPacketOut:
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700342
David Bainbridge103cf022019-12-16 20:11:35 +0000343 msg, _ := message.Data.(OnuPacketMessage)
344
345 log.WithFields(log.Fields{
346 "IntfId": msg.IntfId,
347 "OnuId": msg.OnuId,
348 "pktType": msg.Type,
349 }).Trace("Received OnuPacketOut Message")
350
351 if msg.Type == packetHandlers.EAPOL {
352 eapol.HandleNextPacket(msg.OnuId, msg.IntfId, o.Sn(), o.PortNo, o.InternalState, msg.Packet, stream, client)
353 } else if msg.Type == packetHandlers.DHCP {
354 // NOTE here we receive packets going from the DHCP Server to the ONU
355 // for now we expect them to be double-tagged, but ideally the should be single tagged
356 dhcp.HandleNextPacket(o.ID, o.PonPortID, o.Sn(), o.PortNo, o.HwAddress, o.CTag, o.InternalState, msg.Packet, stream)
357 }
358 case OnuPacketIn:
359 // NOTE we only receive BBR packets here.
360 // Eapol.HandleNextPacket can handle both BBSim and BBr cases so the call is the same
361 // in the DHCP case VOLTHA only act as a proxy, the behaviour is completely different thus we have a dhcp.HandleNextBbrPacket
362 msg, _ := message.Data.(OnuPacketMessage)
363
364 log.WithFields(log.Fields{
365 "IntfId": msg.IntfId,
366 "OnuId": msg.OnuId,
367 "pktType": msg.Type,
368 }).Trace("Received OnuPacketIn Message")
369
370 if msg.Type == packetHandlers.EAPOL {
371 eapol.HandleNextPacket(msg.OnuId, msg.IntfId, o.Sn(), o.PortNo, o.InternalState, msg.Packet, stream, client)
372 } else if msg.Type == packetHandlers.DHCP {
373 dhcp.HandleNextBbrPacket(o.ID, o.PonPortID, o.Sn(), o.STag, o.HwAddress, o.DoneChannel, msg.Packet, client)
374 }
375 case DyingGaspIndication:
376 msg, _ := message.Data.(DyingGaspIndicationMessage)
377 o.sendDyingGaspInd(msg, stream)
378 case OmciIndication:
379 msg, _ := message.Data.(OmciIndicationMessage)
380 o.handleOmci(msg, client)
381 case SendEapolFlow:
382 o.sendEapolFlow(client)
383 case SendDhcpFlow:
384 o.sendDhcpFlow(client)
Arjun E K57a7fcb2020-01-30 06:44:45 +0000385 case IGMPMembershipReportV2:
386 log.Infof("Recieved IGMPMembershipReportV2 message on ONU channel")
387 igmp.SendIGMPMembershipReportV2(o.PonPortID, o.ID, o.Sn(), o.PortNo, o.HwAddress, stream)
388 case IGMPLeaveGroup:
389 log.Infof("Recieved IGMPLeaveGroupV2 message on ONU channel")
390 igmp.SendIGMPLeaveGroupV2(o.PonPortID, o.ID, o.Sn(), o.PortNo, o.HwAddress, stream)
Anand S Katti09541352020-01-29 15:54:01 +0530391 case IGMPMembershipReportV3:
392 log.Infof("Recieved IGMPMembershipReportV3 message on ONU channel")
393 igmp.SendIGMPMembershipReportV3(o.PonPortID, o.ID, o.Sn(), o.PortNo, o.HwAddress, stream)
David Bainbridge103cf022019-12-16 20:11:35 +0000394 default:
395 onuLogger.Warnf("Received unknown message data %v for type %v in OLT Channel", message.Data, message.Type)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700396 }
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700397 }
398 }
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100399 onuLogger.WithFields(log.Fields{
400 "onuID": o.ID,
401 "onuSN": o.Sn(),
402 }).Debug("Stopped handling ONU Indication Channel")
Matteo Scandolo4747d292019-08-05 11:50:18 -0700403}
404
Matteo Scandolodf3f85d2020-01-15 12:50:48 -0800405func (o *Onu) processOmciMessage(message omcisim.OmciChMessage, stream openolt.Openolt_EnableIndicationServer) {
William Kurkian9dadc5b2019-10-22 13:51:57 -0400406 switch message.Type {
Matteo Scandolodf3f85d2020-01-15 12:50:48 -0800407 case omcisim.UniLinkUp, omcisim.UniLinkDown:
408 onuLogger.WithFields(log.Fields{
409 "OnuId": message.Data.OnuId,
410 "IntfId": message.Data.IntfId,
411 "Type": message.Type,
412 }).Infof("UNI Link Alarm")
413 // TODO send to OLT
414
415 omciInd := openolt.OmciIndication{
416 IntfId: message.Data.IntfId,
417 OnuId: message.Data.OnuId,
418 Pkt: message.Packet,
419 }
420
421 omci := &openolt.Indication_OmciInd{OmciInd: &omciInd}
422 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
423 onuLogger.WithFields(log.Fields{
424 "IntfId": o.PonPortID,
425 "SerialNumber": o.Sn(),
426 "Type": message.Type,
427 "omciPacket": omciInd.Pkt,
428 }).Errorf("Failed to send UNI Link Alarm: %v", err)
429 return
430 }
431
432 onuLogger.WithFields(log.Fields{
433 "IntfId": o.PonPortID,
434 "SerialNumber": o.Sn(),
435 "Type": message.Type,
436 "omciPacket": omciInd.Pkt,
437 }).Info("UNI Link alarm sent")
438
William Kurkian9dadc5b2019-10-22 13:51:57 -0400439 case omcisim.GemPortAdded:
440 log.WithFields(log.Fields{
441 "OnuId": message.Data.OnuId,
442 "IntfId": message.Data.IntfId,
443 }).Infof("GemPort Added")
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700444
William Kurkian9dadc5b2019-10-22 13:51:57 -0400445 // NOTE if we receive the GemPort but we don't have EAPOL flows
446 // go an intermediate state, otherwise start auth
447 if o.InternalState.Is("enabled") {
448 if err := o.InternalState.Event("add_gem_port"); err != nil {
449 log.Errorf("Can't go to gem_port_added: %v", err)
450 }
451 } else if o.InternalState.Is("eapol_flow_received") {
Matteo Scandolo3c656a12019-12-10 09:54:51 -0800452 if o.Auth == true {
453 if err := o.InternalState.Event("start_auth"); err != nil {
454 log.Warnf("Can't go to auth_started: %v", err)
455 }
456 } else {
457 onuLogger.WithFields(log.Fields{
458 "IntfId": o.PonPortID,
459 "OnuId": o.ID,
460 "SerialNumber": o.Sn(),
461 }).Warn("Not starting authentication as Auth bit is not set in CLI parameters")
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700462 }
463 }
464 }
465}
466
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100467func (o Onu) NewSN(oltid int, intfid uint32, onuid uint32) *openolt.SerialNumber {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700468
469 sn := new(openolt.SerialNumber)
470
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700471 //sn = new(openolt.SerialNumber)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700472 sn.VendorId = []byte("BBSM")
473 sn.VendorSpecific = []byte{0, byte(oltid % 256), byte(intfid), byte(onuid)}
474
475 return sn
476}
477
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700478// NOTE handle_/process methods can change the ONU internal state as they are receiving messages
479// send method should not change the ONU state
480
William Kurkian0418bc82019-11-06 12:16:24 -0500481func (o *Onu) sendDyingGaspInd(msg DyingGaspIndicationMessage, stream openolt.Openolt_EnableIndicationServer) error {
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700482 alarmData := &openolt.AlarmIndication_DyingGaspInd{
483 DyingGaspInd: &openolt.DyingGaspIndication{
484 IntfId: msg.PonPortID,
485 OnuId: msg.OnuID,
486 Status: "on",
487 },
488 }
489 data := &openolt.Indication_AlarmInd{AlarmInd: &openolt.AlarmIndication{Data: alarmData}}
490
491 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
492 onuLogger.Errorf("Failed to send DyingGaspInd : %v", err)
493 return err
494 }
495 onuLogger.WithFields(log.Fields{
496 "IntfId": msg.PonPortID,
497 "OnuSn": o.Sn(),
498 "OnuId": msg.OnuID,
499 }).Info("sendDyingGaspInd")
500 return nil
501}
502
William Kurkian0418bc82019-11-06 12:16:24 -0500503func (o *Onu) sendOnuDiscIndication(msg OnuDiscIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700504 discoverData := &openolt.Indication_OnuDiscInd{OnuDiscInd: &openolt.OnuDiscIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700505 IntfId: msg.Onu.PonPortID,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700506 SerialNumber: msg.Onu.SerialNumber,
507 }}
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700508
Matteo Scandolo4747d292019-08-05 11:50:18 -0700509 if err := stream.Send(&openolt.Indication{Data: discoverData}); err != nil {
Matteo Scandolo11006992019-08-28 11:29:46 -0700510 log.Errorf("Failed to send Indication_OnuDiscInd: %v", err)
Matteo Scandolo99f18462019-10-28 14:14:28 -0700511 return
Matteo Scandolo4747d292019-08-05 11:50:18 -0700512 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700513
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700514 onuLogger.WithFields(log.Fields{
Matteo Scandolo4747d292019-08-05 11:50:18 -0700515 "IntfId": msg.Onu.PonPortID,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700516 "OnuSn": msg.Onu.Sn(),
517 "OnuId": o.ID,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700518 }).Debug("Sent Indication_OnuDiscInd")
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800519
520 // after DiscoveryRetryDelay check if the state is the same and in case send a new OnuDiscIndication
521 go func(delay time.Duration) {
Matteo Scandolo569e7172019-12-20 11:51:51 -0800522 time.Sleep(delay)
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800523 if o.InternalState.Current() == "discovered" {
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800524 o.sendOnuDiscIndication(msg, stream)
525 }
526 }(o.DiscoveryRetryDelay)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700527}
528
William Kurkian0418bc82019-11-06 12:16:24 -0500529func (o *Onu) sendOnuIndication(msg OnuIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700530 // NOTE voltha returns an ID, but if we use that ID then it complains:
531 // expected_onu_id: 1, received_onu_id: 1024, event: ONU-id-mismatch, can happen if both voltha and the olt rebooted
532 // so we're using the internal ID that is 1
533 // o.ID = msg.OnuID
Matteo Scandolo4747d292019-08-05 11:50:18 -0700534
535 indData := &openolt.Indication_OnuInd{OnuInd: &openolt.OnuIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700536 IntfId: o.PonPortID,
537 OnuId: o.ID,
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700538 OperState: msg.OperState.String(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700539 AdminState: o.OperState.Current(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700540 SerialNumber: o.SerialNumber,
541 }}
542 if err := stream.Send(&openolt.Indication{Data: indData}); err != nil {
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700543 // TODO do we need to transition to a broken state?
Matteo Scandolo11006992019-08-28 11:29:46 -0700544 log.Errorf("Failed to send Indication_OnuInd: %v", err)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700545 }
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700546 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700547 "IntfId": o.PonPortID,
548 "OnuId": o.ID,
549 "OperState": msg.OperState.String(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700550 "AdminState": msg.OperState.String(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700551 "OnuSn": o.Sn(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700552 }).Debug("Sent Indication_OnuInd")
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700553
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700554}
555
William Kurkian0418bc82019-11-06 12:16:24 -0500556func (o *Onu) handleOmciMessage(msg OmciMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700557
558 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700559 "IntfId": o.PonPortID,
Matteo Scandolo27428702019-10-11 16:21:16 -0700560 "SerialNumber": o.Sn(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700561 "omciPacket": msg.omciMsg.Pkt,
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700562 }).Tracef("Received OMCI message")
563
564 var omciInd openolt.OmciIndication
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700565 respPkt, err := omcisim.OmciSim(o.PonPortID, o.ID, HexDecode(msg.omciMsg.Pkt))
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700566 if err != nil {
Matteo Scandolo27428702019-10-11 16:21:16 -0700567 onuLogger.WithFields(log.Fields{
568 "IntfId": o.PonPortID,
569 "SerialNumber": o.Sn(),
570 "omciPacket": omciInd.Pkt,
571 "msg": msg,
572 }).Errorf("Error handling OMCI message %v", msg)
573 return
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700574 }
575
576 omciInd.IntfId = o.PonPortID
577 omciInd.OnuId = o.ID
578 omciInd.Pkt = respPkt
579
580 omci := &openolt.Indication_OmciInd{OmciInd: &omciInd}
581 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
Matteo Scandolo27428702019-10-11 16:21:16 -0700582 onuLogger.WithFields(log.Fields{
583 "IntfId": o.PonPortID,
584 "SerialNumber": o.Sn(),
585 "omciPacket": omciInd.Pkt,
586 "msg": msg,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700587 }).Errorf("send omcisim indication failed: %v", err)
Matteo Scandolo27428702019-10-11 16:21:16 -0700588 return
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700589 }
590 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700591 "IntfId": o.PonPortID,
Matteo Scandolo27428702019-10-11 16:21:16 -0700592 "SerialNumber": o.Sn(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700593 "omciPacket": omciInd.Pkt,
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700594 }).Tracef("Sent OMCI message")
595}
596
Matteo Scandolo27428702019-10-11 16:21:16 -0700597func (o *Onu) storePortNumber(portNo uint32) {
Matteo Scandolo813402b2019-10-23 19:24:52 -0700598 // NOTE this needed only as long as we don't support multiple UNIs
Matteo Scandolo27428702019-10-11 16:21:16 -0700599 // we need to add support for multiple UNIs
600 // the action plan is:
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700601 // - refactor the omcisim-sim library to use https://github.com/cboling/omci instead of canned messages
Matteo Scandolo27428702019-10-11 16:21:16 -0700602 // - change the library so that it reports a single UNI and remove this workaroung
603 // - add support for multiple UNIs in BBSim
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700604 if o.PortNo == 0 || portNo < o.PortNo {
Matteo Scandolo813402b2019-10-23 19:24:52 -0700605 onuLogger.WithFields(log.Fields{
606 "IntfId": o.PonPortID,
607 "OnuId": o.ID,
608 "SerialNumber": o.Sn(),
609 "OnuPortNo": o.PortNo,
610 "FlowPortNo": portNo,
611 }).Debug("Storing ONU portNo")
Matteo Scandolo27428702019-10-11 16:21:16 -0700612 o.PortNo = portNo
613 }
614}
615
William Kurkian0418bc82019-11-06 12:16:24 -0500616func (o *Onu) SetID(id uint32) {
Matteo Scandolo583f17d2020-02-13 10:35:17 -0800617 onuLogger.WithFields(log.Fields{
618 "IntfId": o.PonPortID,
619 "OnuId": id,
620 "SerialNumber": o.Sn(),
621 }).Debug("Storing OnuId ")
William Kurkian0418bc82019-11-06 12:16:24 -0500622 o.ID = id
623}
624
Matteo Scandolo813402b2019-10-23 19:24:52 -0700625func (o *Onu) handleFlowUpdate(msg OnuFlowUpdateMessage) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700626 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700627 "DstPort": msg.Flow.Classifier.DstPort,
628 "EthType": fmt.Sprintf("%x", msg.Flow.Classifier.EthType),
629 "FlowId": msg.Flow.FlowId,
630 "FlowType": msg.Flow.FlowType,
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700631 "InnerVlan": msg.Flow.Classifier.IVid,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700632 "IntfId": msg.Flow.AccessIntfId,
633 "IpProto": msg.Flow.Classifier.IpProto,
634 "OnuId": msg.Flow.OnuId,
635 "OnuSn": o.Sn(),
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700636 "OuterVlan": msg.Flow.Classifier.OVid,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700637 "PortNo": msg.Flow.PortNo,
638 "SrcPort": msg.Flow.Classifier.SrcPort,
639 "UniID": msg.Flow.UniId,
640 }).Debug("ONU receives Flow")
641
Matteo Scandolo813402b2019-10-23 19:24:52 -0700642 if msg.Flow.UniId != 0 {
643 // as of now BBSim only support a single UNI, so ignore everything that is not targeted to it
644 onuLogger.WithFields(log.Fields{
645 "IntfId": o.PonPortID,
646 "OnuId": o.ID,
647 "SerialNumber": o.Sn(),
648 }).Debug("Ignoring flow as it's not for the first UNI")
649 return
650 }
651
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700652 if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeEAPOL) && msg.Flow.Classifier.OVid == 4091 {
Matteo Scandolo27428702019-10-11 16:21:16 -0700653 // NOTE storing the PortNO, it's needed when sending PacketIndications
Matteo Scandolo813402b2019-10-23 19:24:52 -0700654 o.storePortNumber(uint32(msg.Flow.PortNo))
Matteo Scandolo27428702019-10-11 16:21:16 -0700655
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700656 // NOTE if we receive the EAPOL flows but we don't have GemPorts
657 // go an intermediate state, otherwise start auth
658 if o.InternalState.Is("enabled") {
659 if err := o.InternalState.Event("receive_eapol_flow"); err != nil {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700660 log.Warnf("Can't go to eapol_flow_received: %v", err)
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700661 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700662 } else if o.InternalState.Is("gem_port_added") {
Matteo Scandoloc1147092019-10-29 09:38:33 -0700663
664 if o.Auth == true {
665 if err := o.InternalState.Event("start_auth"); err != nil {
666 log.Warnf("Can't go to auth_started: %v", err)
667 }
668 } else {
669 onuLogger.WithFields(log.Fields{
670 "IntfId": o.PonPortID,
671 "OnuId": o.ID,
672 "SerialNumber": o.Sn(),
673 }).Warn("Not starting authentication as Auth bit is not set in CLI parameters")
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700674 }
Matteo Scandoloc1147092019-10-29 09:38:33 -0700675
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700676 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700677 } else if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeIPv4) &&
678 msg.Flow.Classifier.SrcPort == uint32(68) &&
679 msg.Flow.Classifier.DstPort == uint32(67) {
Matteo Scandolo99f18462019-10-28 14:14:28 -0700680
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100681 // 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 -0700682 o.DhcpFlowReceived = true
Matteo Scandoloc1147092019-10-29 09:38:33 -0700683
684 if o.Dhcp == true {
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100685 // NOTE we are receiving multiple DHCP flows but we shouldn't call the transition multiple times
Matteo Scandoloc1147092019-10-29 09:38:33 -0700686 if err := o.InternalState.Event("start_dhcp"); err != nil {
687 log.Errorf("Can't go to dhcp_started: %v", err)
688 }
689 } else {
690 onuLogger.WithFields(log.Fields{
691 "IntfId": o.PonPortID,
692 "OnuId": o.ID,
693 "SerialNumber": o.Sn(),
694 }).Warn("Not starting DHCP as Dhcp bit is not set in CLI parameters")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700695 }
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700696 }
697}
698
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700699// HexDecode converts the hex encoding to binary
700func HexDecode(pkt []byte) []byte {
701 p := make([]byte, len(pkt)/2)
702 for i, j := 0, 0; i < len(pkt); i, j = i+2, j+1 {
703 // Go figure this ;)
704 u := (pkt[i] & 15) + (pkt[i]>>6)*9
705 l := (pkt[i+1] & 15) + (pkt[i+1]>>6)*9
706 p[j] = u<<4 + l
707 }
708 onuLogger.Tracef("Omci decoded: %x.", p)
709 return p
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700710}
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700711
712// BBR methods
713
714func sendOmciMsg(pktBytes []byte, intfId uint32, onuId uint32, sn *openolt.SerialNumber, msgType string, client openolt.OpenoltClient) {
715 omciMsg := openolt.OmciMsg{
716 IntfId: intfId,
717 OnuId: onuId,
718 Pkt: pktBytes,
719 }
720
721 if _, err := client.OmciMsgOut(context.Background(), &omciMsg); err != nil {
722 log.WithFields(log.Fields{
723 "IntfId": intfId,
724 "OnuId": onuId,
725 "SerialNumber": common.OnuSnToString(sn),
726 "Pkt": omciMsg.Pkt,
727 }).Fatalf("Failed to send MIB Reset")
728 }
729 log.WithFields(log.Fields{
730 "IntfId": intfId,
731 "OnuId": onuId,
732 "SerialNumber": common.OnuSnToString(sn),
733 "Pkt": omciMsg.Pkt,
734 }).Tracef("Sent OMCI message %s", msgType)
735}
736
737func (onu *Onu) getNextTid(highPriority ...bool) uint16 {
738 var next uint16
739 if len(highPriority) > 0 && highPriority[0] {
740 next = onu.hpTid
741 onu.hpTid += 1
742 if onu.hpTid < 0x8000 {
743 onu.hpTid = 0x8000
744 }
745 } else {
746 next = onu.tid
747 onu.tid += 1
748 if onu.tid >= 0x8000 {
749 onu.tid = 1
750 }
751 }
752 return next
753}
754
755// TODO move this method in responders/omcisim
756func (o *Onu) StartOmci(client openolt.OpenoltClient) {
757 mibReset, _ := omcilib.CreateMibResetRequest(o.getNextTid(false))
758 sendOmciMsg(mibReset, o.PonPortID, o.ID, o.SerialNumber, "mibReset", client)
759}
760
761func (o *Onu) handleOmci(msg OmciIndicationMessage, client openolt.OpenoltClient) {
762 msgType, packet := omcilib.DecodeOmci(msg.OmciInd.Pkt)
763
764 log.WithFields(log.Fields{
765 "IntfId": msg.OmciInd.IntfId,
766 "OnuId": msg.OmciInd.OnuId,
767 "OnuSn": common.OnuSnToString(o.SerialNumber),
768 "Pkt": msg.OmciInd.Pkt,
769 "msgType": msgType,
Anand S Katti09541352020-01-29 15:54:01 +0530770 }).Trace("ONU Receives OMCI Msg")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700771 switch msgType {
772 default:
Matteo Scandolo813402b2019-10-23 19:24:52 -0700773 log.WithFields(log.Fields{
774 "IntfId": msg.OmciInd.IntfId,
775 "OnuId": msg.OmciInd.OnuId,
776 "OnuSn": common.OnuSnToString(o.SerialNumber),
777 "Pkt": msg.OmciInd.Pkt,
778 "msgType": msgType,
779 }).Fatalf("unexpected frame: %v", packet)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700780 case omci.MibResetResponseType:
781 mibUpload, _ := omcilib.CreateMibUploadRequest(o.getNextTid(false))
782 sendOmciMsg(mibUpload, o.PonPortID, o.ID, o.SerialNumber, "mibUpload", client)
783 case omci.MibUploadResponseType:
784 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
785 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
786 case omci.MibUploadNextResponseType:
787 o.seqNumber++
788
789 if o.seqNumber > 290 {
790 // NOTE we are done with the MIB Upload (290 is the number of messages the omci-sim library will respond to)
791 galEnet, _ := omcilib.CreateGalEnetRequest(o.getNextTid(false))
792 sendOmciMsg(galEnet, o.PonPortID, o.ID, o.SerialNumber, "CreateGalEnetRequest", client)
793 } else {
794 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
795 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
796 }
797 case omci.CreateResponseType:
798 // NOTE Creating a GemPort,
799 // BBsim actually doesn't care about the values, so we can do we want with the parameters
800 // In the same way we can create a GemPort even without setting up UNIs/TConts/...
801 // but we need the GemPort to trigger the state change
802
803 if !o.HasGemPort {
804 // NOTE this sends a CreateRequestType and BBSim replies with a CreateResponseType
805 // thus we send this request only once
806 gemReq, _ := omcilib.CreateGemPortRequest(o.getNextTid(false))
807 sendOmciMsg(gemReq, o.PonPortID, o.ID, o.SerialNumber, "CreateGemPortRequest", client)
808 o.HasGemPort = true
809 } else {
810 if err := o.InternalState.Event("send_eapol_flow"); err != nil {
811 onuLogger.WithFields(log.Fields{
812 "OnuId": o.ID,
813 "IntfId": o.PonPortID,
814 "OnuSn": o.Sn(),
815 }).Errorf("Error while transitioning ONU State %v", err)
816 }
817 }
818
819 }
820}
821
822func (o *Onu) sendEapolFlow(client openolt.OpenoltClient) {
823
824 classifierProto := openolt.Classifier{
825 EthType: uint32(layers.EthernetTypeEAPOL),
826 OVid: 4091,
827 }
828
829 actionProto := openolt.Action{}
830
831 downstreamFlow := openolt.Flow{
832 AccessIntfId: int32(o.PonPortID),
833 OnuId: int32(o.ID),
Matteo Scandolo813402b2019-10-23 19:24:52 -0700834 UniId: int32(0), // NOTE do not hardcode this, we need to support multiple UNIs
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700835 FlowId: uint32(o.ID),
836 FlowType: "downstream",
837 AllocId: int32(0),
838 NetworkIntfId: int32(0),
839 GemportId: int32(1), // FIXME use the same value as CreateGemPortRequest PortID, do not hardcode
840 Classifier: &classifierProto,
841 Action: &actionProto,
842 Priority: int32(100),
843 Cookie: uint64(o.ID),
844 PortNo: uint32(o.ID), // NOTE we are using this to map an incoming packetIndication to an ONU
845 }
846
847 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
848 log.WithFields(log.Fields{
849 "IntfId": o.PonPortID,
850 "OnuId": o.ID,
851 "FlowId": downstreamFlow.FlowId,
852 "PortNo": downstreamFlow.PortNo,
853 "SerialNumber": common.OnuSnToString(o.SerialNumber),
854 }).Fatalf("Failed to EAPOL Flow")
855 }
856 log.WithFields(log.Fields{
857 "IntfId": o.PonPortID,
858 "OnuId": o.ID,
859 "FlowId": downstreamFlow.FlowId,
860 "PortNo": downstreamFlow.PortNo,
861 "SerialNumber": common.OnuSnToString(o.SerialNumber),
862 }).Info("Sent EAPOL Flow")
863}
864
865func (o *Onu) sendDhcpFlow(client openolt.OpenoltClient) {
866 classifierProto := openolt.Classifier{
867 EthType: uint32(layers.EthernetTypeIPv4),
868 SrcPort: uint32(68),
869 DstPort: uint32(67),
870 }
871
872 actionProto := openolt.Action{}
873
874 downstreamFlow := openolt.Flow{
875 AccessIntfId: int32(o.PonPortID),
876 OnuId: int32(o.ID),
Matteo Scandolo813402b2019-10-23 19:24:52 -0700877 UniId: int32(0), // FIXME do not hardcode this
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700878 FlowId: uint32(o.ID),
879 FlowType: "downstream",
880 AllocId: int32(0),
881 NetworkIntfId: int32(0),
882 GemportId: int32(1), // FIXME use the same value as CreateGemPortRequest PortID, do not hardcode
883 Classifier: &classifierProto,
884 Action: &actionProto,
885 Priority: int32(100),
886 Cookie: uint64(o.ID),
887 PortNo: uint32(o.ID), // NOTE we are using this to map an incoming packetIndication to an ONU
888 }
889
890 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
891 log.WithFields(log.Fields{
892 "IntfId": o.PonPortID,
893 "OnuId": o.ID,
894 "FlowId": downstreamFlow.FlowId,
895 "PortNo": downstreamFlow.PortNo,
896 "SerialNumber": common.OnuSnToString(o.SerialNumber),
897 }).Fatalf("Failed to send DHCP Flow")
898 }
899 log.WithFields(log.Fields{
900 "IntfId": o.PonPortID,
901 "OnuId": o.ID,
902 "FlowId": downstreamFlow.FlowId,
903 "PortNo": downstreamFlow.PortNo,
904 "SerialNumber": common.OnuSnToString(o.SerialNumber),
905 }).Info("Sent DHCP Flow")
906}
Pragya Arya8bdb4532020-03-02 17:08:09 +0530907
908// DeleteFlow method search and delete flowKey from the onu flows slice
909func (onu *Onu) DeleteFlow(key FlowKey) {
910 for pos, flowKey := range onu.Flows {
911 if flowKey == key {
912 // delete the flowKey by shifting all flowKeys by one
913 onu.Flows = append(onu.Flows[:pos], onu.Flows[pos+1:]...)
914 t := make([]FlowKey, len(onu.Flows))
915 copy(t, onu.Flows)
916 onu.Flows = t
917 break
918 }
919 }
920}