blob: 2aba5d29ee40f352e6252cad4a9c380432e1b4a4 [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 Scandolo3bc73742019-08-20 14:04:04 -070021 "fmt"
Matteo Scandolo40e067f2019-10-16 16:59:41 -070022 "github.com/cboling/omci"
Matteo Scandolo3bc73742019-08-20 14:04:04 -070023 "github.com/google/gopacket/layers"
Matteo Scandolo4747d292019-08-05 11:50:18 -070024 "github.com/looplab/fsm"
Matteo Scandolo075b1892019-10-07 12:11:07 -070025 "github.com/opencord/bbsim/internal/bbsim/packetHandlers"
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070026 "github.com/opencord/bbsim/internal/bbsim/responders/dhcp"
27 "github.com/opencord/bbsim/internal/bbsim/responders/eapol"
Matteo Scandolo40e067f2019-10-16 16:59:41 -070028 "github.com/opencord/bbsim/internal/common"
29 omcilib "github.com/opencord/bbsim/internal/common/omci"
30 omcisim "github.com/opencord/omci-sim"
Matteo Scandolo3bc73742019-08-20 14:04:04 -070031 "github.com/opencord/voltha-protos/go/openolt"
Matteo Scandolo4747d292019-08-05 11:50:18 -070032 log "github.com/sirupsen/logrus"
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070033 "net"
Matteo Scandolo4747d292019-08-05 11:50:18 -070034)
35
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070036var onuLogger = log.WithFields(log.Fields{
37 "module": "ONU",
38})
39
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070040type Onu struct {
Matteo Scandolo27428702019-10-11 16:21:16 -070041 ID uint32
42 PonPortID uint32
43 PonPort PonPort
44 STag int
45 CTag int
46 // PortNo comes with flows and it's used when sending packetIndications,
47 // There is one PortNo per UNI Port, for now we're only storing the first one
48 // FIXME add support for multiple UNIs
49 PortNo uint32
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070050 HwAddress net.HardwareAddr
51 InternalState *fsm.FSM
52
53 OperState *fsm.FSM
54 SerialNumber *openolt.SerialNumber
55
56 Channel chan Message // this Channel is to track state changes OMCI messages, EAPOL and DHCP packets
Matteo Scandolo40e067f2019-10-16 16:59:41 -070057
58 // OMCI params
59 tid uint16
60 hpTid uint16
61 seqNumber uint16
62 HasGemPort bool
63
64 DoneChannel chan bool // this channel is used to signal once the onu is complete (when the struct is used by BBR)
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070065}
66
67func (o Onu) Sn() string {
Matteo Scandolo40e067f2019-10-16 16:59:41 -070068 return common.OnuSnToString(o.SerialNumber)
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070069}
70
Matteo Scandolo27428702019-10-11 16:21:16 -070071func CreateONU(olt OltDevice, pon PonPort, id uint32, sTag int, cTag int) *Onu {
Matteo Scandolo4747d292019-08-05 11:50:18 -070072
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070073 o := Onu{
Matteo Scandolo40e067f2019-10-16 16:59:41 -070074 ID: id,
75 PonPortID: pon.ID,
76 PonPort: pon,
77 STag: sTag,
78 CTag: cTag,
79 HwAddress: net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, byte(pon.ID), byte(id)},
80 PortNo: 0,
81 Channel: make(chan Message, 2048),
82 tid: 0x1,
83 hpTid: 0x8000,
84 seqNumber: 0,
85 DoneChannel: make(chan bool, 1),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070086 }
87 o.SerialNumber = o.NewSN(olt.ID, pon.ID, o.ID)
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070088
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070089 // NOTE this state machine is used to track the operational
90 // state as requested by VOLTHA
91 o.OperState = getOperStateFSM(func(e *fsm.Event) {
92 onuLogger.WithFields(log.Fields{
93 "ID": o.ID,
94 }).Debugf("Changing ONU OperState from %s to %s", e.Src, e.Dst)
95 })
96
97 // NOTE this state machine is used to activate the OMCI, EAPOL and DHCP clients
98 o.InternalState = fsm.NewFSM(
99 "created",
100 fsm.Events{
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700101 // DEVICE Lifecycle
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700102 {Name: "discover", Src: []string{"created"}, Dst: "discovered"},
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700103 {Name: "enable", Src: []string{"discovered", "disabled"}, Dst: "enabled"},
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700104 {Name: "receive_eapol_flow", Src: []string{"enabled", "gem_port_added"}, Dst: "eapol_flow_received"},
105 {Name: "add_gem_port", Src: []string{"enabled", "eapol_flow_received"}, Dst: "gem_port_added"},
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700106 // NOTE should disabled state be diffente for oper_disabled (emulating an error) and admin_disabled (received a disabled call via VOLTHA)?
107 {Name: "disable", Src: []string{"eap_response_success_received", "auth_failed", "dhcp_ack_received", "dhcp_failed"}, Dst: "disabled"},
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700108 // EAPOL
109 {Name: "start_auth", Src: []string{"eapol_flow_received", "gem_port_added"}, Dst: "auth_started"},
110 {Name: "eap_start_sent", Src: []string{"auth_started"}, Dst: "eap_start_sent"},
111 {Name: "eap_response_identity_sent", Src: []string{"eap_start_sent"}, Dst: "eap_response_identity_sent"},
112 {Name: "eap_response_challenge_sent", Src: []string{"eap_response_identity_sent"}, Dst: "eap_response_challenge_sent"},
113 {Name: "eap_response_success_received", Src: []string{"eap_response_challenge_sent"}, Dst: "eap_response_success_received"},
114 {Name: "auth_failed", Src: []string{"auth_started", "eap_start_sent", "eap_response_identity_sent", "eap_response_challenge_sent"}, Dst: "auth_failed"},
115 // DHCP
116 {Name: "start_dhcp", Src: []string{"eap_response_success_received"}, Dst: "dhcp_started"},
117 {Name: "dhcp_discovery_sent", Src: []string{"dhcp_started"}, Dst: "dhcp_discovery_sent"},
118 {Name: "dhcp_request_sent", Src: []string{"dhcp_discovery_sent"}, Dst: "dhcp_request_sent"},
119 {Name: "dhcp_ack_received", Src: []string{"dhcp_request_sent"}, Dst: "dhcp_ack_received"},
120 {Name: "dhcp_failed", Src: []string{"dhcp_started", "dhcp_discovery_sent", "dhcp_request_sent"}, Dst: "dhcp_failed"},
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700121 // BBR States
122 // TODO add start OMCI state
123 {Name: "send_eapol_flow", Src: []string{"created"}, Dst: "eapol_flow_sent"},
124 {Name: "send_dhcp_flow", Src: []string{"eapol_flow_sent"}, Dst: "dhcp_flow_sent"},
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700125 },
126 fsm.Callbacks{
127 "enter_state": func(e *fsm.Event) {
128 o.logStateChange(e.Src, e.Dst)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700129 },
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700130 "enter_enabled": func(event *fsm.Event) {
131 msg := Message{
132 Type: OnuIndication,
133 Data: OnuIndicationMessage{
134 OnuSN: o.SerialNumber,
135 PonPortID: o.PonPortID,
136 OperState: UP,
137 },
138 }
139 o.Channel <- msg
140 },
141 "enter_disabled": func(event *fsm.Event) {
142 msg := Message{
143 Type: OnuIndication,
144 Data: OnuIndicationMessage{
145 OnuSN: o.SerialNumber,
146 PonPortID: o.PonPortID,
147 OperState: DOWN,
148 },
149 }
150 o.Channel <- msg
151 },
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700152 "enter_auth_started": func(e *fsm.Event) {
153 o.logStateChange(e.Src, e.Dst)
154 msg := Message{
155 Type: StartEAPOL,
156 Data: PacketMessage{
157 PonPortID: o.PonPortID,
158 OnuID: o.ID,
159 },
160 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700161 o.Channel <- msg
Matteo Scandolo4747d292019-08-05 11:50:18 -0700162 },
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700163 "enter_auth_failed": func(e *fsm.Event) {
164 onuLogger.WithFields(log.Fields{
165 "OnuId": o.ID,
166 "IntfId": o.PonPortID,
167 "OnuSn": o.Sn(),
168 }).Errorf("ONU failed to authenticate!")
169 },
170 "enter_dhcp_started": func(e *fsm.Event) {
171 msg := Message{
172 Type: StartDHCP,
173 Data: PacketMessage{
174 PonPortID: o.PonPortID,
175 OnuID: o.ID,
176 },
177 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700178 o.Channel <- msg
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700179 },
180 "enter_dhcp_failed": func(e *fsm.Event) {
181 onuLogger.WithFields(log.Fields{
182 "OnuId": o.ID,
183 "IntfId": o.PonPortID,
184 "OnuSn": o.Sn(),
185 }).Errorf("ONU failed to DHCP!")
186 },
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700187 "enter_eapol_flow_sent": func(e *fsm.Event) {
188 msg := Message{
189 Type: SendEapolFlow,
190 }
191 o.Channel <- msg
192 },
193 "enter_dhcp_flow_sent": func(e *fsm.Event) {
194 msg := Message{
195 Type: SendDhcpFlow,
196 }
197 o.Channel <- msg
198 },
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700199 },
200 )
Matteo Scandolo27428702019-10-11 16:21:16 -0700201 return &o
Matteo Scandolo4747d292019-08-05 11:50:18 -0700202}
203
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700204func (o Onu) logStateChange(src string, dst string) {
205 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700206 "OnuId": o.ID,
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700207 "IntfId": o.PonPortID,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700208 "OnuSn": o.Sn(),
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700209 }).Debugf("Changing ONU InternalState from %s to %s", src, dst)
210}
211
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700212func (o Onu) ProcessOnuMessages(stream openolt.Openolt_EnableIndicationServer, client openolt.OpenoltClient) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700213 onuLogger.WithFields(log.Fields{
Matteo Scandolo4747d292019-08-05 11:50:18 -0700214 "onuID": o.ID,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700215 "onuSN": o.Sn(),
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700216 }).Debug("Started ONU Indication Channel")
217
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700218 for message := range o.Channel {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700219 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700220 "onuID": o.ID,
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700221 "onuSN": o.Sn(),
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700222 "messageType": message.Type,
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700223 }).Tracef("Received message on ONU Channel")
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700224
225 switch message.Type {
226 case OnuDiscIndication:
227 msg, _ := message.Data.(OnuDiscIndicationMessage)
228 o.sendOnuDiscIndication(msg, stream)
229 case OnuIndication:
230 msg, _ := message.Data.(OnuIndicationMessage)
231 o.sendOnuIndication(msg, stream)
232 case OMCI:
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700233 msg, _ := message.Data.(OmciMessage)
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700234 o.handleOmciMessage(msg, stream)
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700235 case FlowUpdate:
236 msg, _ := message.Data.(OnuFlowUpdateMessage)
237 o.handleFlowUpdate(msg, stream)
238 case StartEAPOL:
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700239 log.Infof("Receive StartEAPOL message on ONU Channel")
Matteo Scandolo27428702019-10-11 16:21:16 -0700240 eapol.SendEapStart(o.ID, o.PonPortID, o.Sn(), o.PortNo, o.HwAddress, o.InternalState, stream)
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700241 case StartDHCP:
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700242 log.Infof("Receive StartDHCP message on ONU Channel")
Matteo Scandolo27428702019-10-11 16:21:16 -0700243 // FIXME use id, ponId as SendEapStart
244 dhcp.SendDHCPDiscovery(o.PonPortID, o.ID, o.Sn(), o.PortNo, o.InternalState, o.HwAddress, o.CTag, stream)
Matteo Scandolo075b1892019-10-07 12:11:07 -0700245 case OnuPacketOut:
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700246
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700247 msg, _ := message.Data.(OnuPacketMessage)
248
249 log.WithFields(log.Fields{
250 "IntfId": msg.IntfId,
251 "OnuId": msg.OnuId,
252 "pktType": msg.Type,
253 }).Trace("Received OnuPacketOut Message")
254
255 if msg.Type == packetHandlers.EAPOL {
256 eapol.HandleNextPacket(msg.OnuId, msg.IntfId, o.Sn(), o.PortNo, o.InternalState, msg.Packet, stream, client)
257 } else if msg.Type == packetHandlers.DHCP {
Matteo Scandolo075b1892019-10-07 12:11:07 -0700258 // NOTE here we receive packets going from the DHCP Server to the ONU
259 // for now we expect them to be double-tagged, but ideally the should be single tagged
Matteo Scandolo27428702019-10-11 16:21:16 -0700260 dhcp.HandleNextPacket(o.ID, o.PonPortID, o.Sn(), o.PortNo, o.HwAddress, o.CTag, o.InternalState, msg.Packet, stream)
Matteo Scandolo075b1892019-10-07 12:11:07 -0700261 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700262 case OnuPacketIn:
263 // NOTE we only receive BBR packets here.
264 // Eapol.HandleNextPacket can handle both BBSim and BBr cases so the call is the same
265 // in the DHCP case VOLTHA only act as a proxy, the behaviour is completely different thus we have a dhcp.HandleNextBbrPacket
266 msg, _ := message.Data.(OnuPacketMessage)
267
268 log.WithFields(log.Fields{
269 "IntfId": msg.IntfId,
270 "OnuId": msg.OnuId,
271 "pktType": msg.Type,
272 }).Trace("Received OnuPacketIn Message")
273
274 if msg.Type == packetHandlers.EAPOL {
275 eapol.HandleNextPacket(msg.OnuId, msg.IntfId, o.Sn(), o.PortNo, o.InternalState, msg.Packet, stream, client)
276 } else if msg.Type == packetHandlers.DHCP {
277 dhcp.HandleNextBbrPacket(o.ID, o.PonPortID, o.Sn(), o.STag, o.HwAddress, o.DoneChannel, msg.Packet, client)
278 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700279 case DyingGaspIndication:
280 msg, _ := message.Data.(DyingGaspIndicationMessage)
281 o.sendDyingGaspInd(msg, stream)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700282 case OmciIndication:
283 // TODO handle me!
284 // here https://gerrit.opencord.org/#/c/15521/11/internal/bbr/onu.go in startOmci
285 msg, _ := message.Data.(OmciIndicationMessage)
286 o.handleOmci(msg, client)
287 case SendEapolFlow:
288 o.sendEapolFlow(client)
289 case SendDhcpFlow:
290 o.sendDhcpFlow(client)
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700291 default:
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700292 onuLogger.Warnf("Received unknown message data %v for type %v in OLT Channel", message.Data, message.Type)
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700293 }
294 }
Matteo Scandolo4747d292019-08-05 11:50:18 -0700295}
296
William Kurkian9dadc5b2019-10-22 13:51:57 -0400297func (o Onu) processOmciMessage(message omcisim.OmciChMessage) {
298 switch message.Type {
299 case omcisim.GemPortAdded:
300 log.WithFields(log.Fields{
301 "OnuId": message.Data.OnuId,
302 "IntfId": message.Data.IntfId,
303 }).Infof("GemPort Added")
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700304
William Kurkian9dadc5b2019-10-22 13:51:57 -0400305 // NOTE if we receive the GemPort but we don't have EAPOL flows
306 // go an intermediate state, otherwise start auth
307 if o.InternalState.Is("enabled") {
308 if err := o.InternalState.Event("add_gem_port"); err != nil {
309 log.Errorf("Can't go to gem_port_added: %v", err)
310 }
311 } else if o.InternalState.Is("eapol_flow_received") {
312 if err := o.InternalState.Event("start_auth"); err != nil {
313 log.Errorf("Can't go to auth_started: %v", err)
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700314 }
315 }
316 }
317}
318
Matteo Scandolo4747d292019-08-05 11:50:18 -0700319func (o Onu) NewSN(oltid int, intfid uint32, onuid uint32) *openolt.SerialNumber {
320
321 sn := new(openolt.SerialNumber)
322
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700323 //sn = new(openolt.SerialNumber)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700324 sn.VendorId = []byte("BBSM")
325 sn.VendorSpecific = []byte{0, byte(oltid % 256), byte(intfid), byte(onuid)}
326
327 return sn
328}
329
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700330// NOTE handle_/process methods can change the ONU internal state as they are receiving messages
331// send method should not change the ONU state
332
333func (o Onu) sendDyingGaspInd(msg DyingGaspIndicationMessage, stream openolt.Openolt_EnableIndicationServer) error {
334 alarmData := &openolt.AlarmIndication_DyingGaspInd{
335 DyingGaspInd: &openolt.DyingGaspIndication{
336 IntfId: msg.PonPortID,
337 OnuId: msg.OnuID,
338 Status: "on",
339 },
340 }
341 data := &openolt.Indication_AlarmInd{AlarmInd: &openolt.AlarmIndication{Data: alarmData}}
342
343 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
344 onuLogger.Errorf("Failed to send DyingGaspInd : %v", err)
345 return err
346 }
347 onuLogger.WithFields(log.Fields{
348 "IntfId": msg.PonPortID,
349 "OnuSn": o.Sn(),
350 "OnuId": msg.OnuID,
351 }).Info("sendDyingGaspInd")
352 return nil
353}
354
Matteo Scandolo4747d292019-08-05 11:50:18 -0700355func (o Onu) sendOnuDiscIndication(msg OnuDiscIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
356 discoverData := &openolt.Indication_OnuDiscInd{OnuDiscInd: &openolt.OnuDiscIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700357 IntfId: msg.Onu.PonPortID,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700358 SerialNumber: msg.Onu.SerialNumber,
359 }}
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700360
Matteo Scandolo4747d292019-08-05 11:50:18 -0700361 if err := stream.Send(&openolt.Indication{Data: discoverData}); err != nil {
Matteo Scandolo11006992019-08-28 11:29:46 -0700362 log.Errorf("Failed to send Indication_OnuDiscInd: %v", err)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700363 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700364
365 if err := o.InternalState.Event("discover"); err != nil {
366 oltLogger.WithFields(log.Fields{
367 "IntfId": o.PonPortID,
368 "OnuSn": o.Sn(),
369 "OnuId": o.ID,
370 }).Infof("Failed to transition ONU to discovered state: %s", err.Error())
371 }
372
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700373 onuLogger.WithFields(log.Fields{
Matteo Scandolo4747d292019-08-05 11:50:18 -0700374 "IntfId": msg.Onu.PonPortID,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700375 "OnuSn": msg.Onu.Sn(),
376 "OnuId": o.ID,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700377 }).Debug("Sent Indication_OnuDiscInd")
378}
379
380func (o Onu) sendOnuIndication(msg OnuIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
381 // NOTE voltha returns an ID, but if we use that ID then it complains:
382 // expected_onu_id: 1, received_onu_id: 1024, event: ONU-id-mismatch, can happen if both voltha and the olt rebooted
383 // so we're using the internal ID that is 1
384 // o.ID = msg.OnuID
Matteo Scandolo4747d292019-08-05 11:50:18 -0700385
386 indData := &openolt.Indication_OnuInd{OnuInd: &openolt.OnuIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700387 IntfId: o.PonPortID,
388 OnuId: o.ID,
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700389 OperState: msg.OperState.String(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700390 AdminState: o.OperState.Current(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700391 SerialNumber: o.SerialNumber,
392 }}
393 if err := stream.Send(&openolt.Indication{Data: indData}); err != nil {
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700394 // TODO do we need to transition to a broken state?
Matteo Scandolo11006992019-08-28 11:29:46 -0700395 log.Errorf("Failed to send Indication_OnuInd: %v", err)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700396 }
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700397 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700398 "IntfId": o.PonPortID,
399 "OnuId": o.ID,
400 "OperState": msg.OperState.String(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700401 "AdminState": msg.OperState.String(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700402 "OnuSn": o.Sn(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700403 }).Debug("Sent Indication_OnuInd")
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700404
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700405}
406
407func (o Onu) handleOmciMessage(msg OmciMessage, stream openolt.Openolt_EnableIndicationServer) {
408
409 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700410 "IntfId": o.PonPortID,
Matteo Scandolo27428702019-10-11 16:21:16 -0700411 "SerialNumber": o.Sn(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700412 "omciPacket": msg.omciMsg.Pkt,
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700413 }).Tracef("Received OMCI message")
414
415 var omciInd openolt.OmciIndication
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700416 respPkt, err := omcisim.OmciSim(o.PonPortID, o.ID, HexDecode(msg.omciMsg.Pkt))
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700417 if err != nil {
Matteo Scandolo27428702019-10-11 16:21:16 -0700418 onuLogger.WithFields(log.Fields{
419 "IntfId": o.PonPortID,
420 "SerialNumber": o.Sn(),
421 "omciPacket": omciInd.Pkt,
422 "msg": msg,
423 }).Errorf("Error handling OMCI message %v", msg)
424 return
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700425 }
426
427 omciInd.IntfId = o.PonPortID
428 omciInd.OnuId = o.ID
429 omciInd.Pkt = respPkt
430
431 omci := &openolt.Indication_OmciInd{OmciInd: &omciInd}
432 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
Matteo Scandolo27428702019-10-11 16:21:16 -0700433 onuLogger.WithFields(log.Fields{
434 "IntfId": o.PonPortID,
435 "SerialNumber": o.Sn(),
436 "omciPacket": omciInd.Pkt,
437 "msg": msg,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700438 }).Errorf("send omcisim indication failed: %v", err)
Matteo Scandolo27428702019-10-11 16:21:16 -0700439 return
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700440 }
441 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700442 "IntfId": o.PonPortID,
Matteo Scandolo27428702019-10-11 16:21:16 -0700443 "SerialNumber": o.Sn(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700444 "omciPacket": omciInd.Pkt,
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700445 }).Tracef("Sent OMCI message")
446}
447
Matteo Scandolo27428702019-10-11 16:21:16 -0700448func (o *Onu) storePortNumber(portNo uint32) {
449 // FIXME this is a workaround to always use the SN-1 entry in sadis,
450 // we need to add support for multiple UNIs
451 // the action plan is:
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700452 // - refactor the omcisim-sim library to use https://github.com/cboling/omci instead of canned messages
Matteo Scandolo27428702019-10-11 16:21:16 -0700453 // - change the library so that it reports a single UNI and remove this workaroung
454 // - add support for multiple UNIs in BBSim
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700455 if o.PortNo == 0 || portNo < o.PortNo {
Matteo Scandolo27428702019-10-11 16:21:16 -0700456 o.PortNo = portNo
457 }
458}
459
460func (o *Onu) handleFlowUpdate(msg OnuFlowUpdateMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700461 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700462 "DstPort": msg.Flow.Classifier.DstPort,
463 "EthType": fmt.Sprintf("%x", msg.Flow.Classifier.EthType),
464 "FlowId": msg.Flow.FlowId,
465 "FlowType": msg.Flow.FlowType,
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700466 "InnerVlan": msg.Flow.Classifier.IVid,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700467 "IntfId": msg.Flow.AccessIntfId,
468 "IpProto": msg.Flow.Classifier.IpProto,
469 "OnuId": msg.Flow.OnuId,
470 "OnuSn": o.Sn(),
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700471 "OuterVlan": msg.Flow.Classifier.OVid,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700472 "PortNo": msg.Flow.PortNo,
473 "SrcPort": msg.Flow.Classifier.SrcPort,
474 "UniID": msg.Flow.UniId,
475 }).Debug("ONU receives Flow")
476
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700477 if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeEAPOL) && msg.Flow.Classifier.OVid == 4091 {
Matteo Scandolo27428702019-10-11 16:21:16 -0700478 // NOTE storing the PortNO, it's needed when sending PacketIndications
479 if o.PortNo == 0 {
480 o.storePortNumber(uint32(msg.Flow.PortNo))
481 }
482
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700483 // NOTE if we receive the EAPOL flows but we don't have GemPorts
484 // go an intermediate state, otherwise start auth
485 if o.InternalState.Is("enabled") {
486 if err := o.InternalState.Event("receive_eapol_flow"); err != nil {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700487 log.Warnf("Can't go to eapol_flow_received: %v", err)
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700488 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700489 } else if o.InternalState.Is("gem_port_added") {
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700490 if err := o.InternalState.Event("start_auth"); err != nil {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700491 log.Warnf("Can't go to auth_started: %v", err)
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700492 }
493 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700494 } else if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeIPv4) &&
495 msg.Flow.Classifier.SrcPort == uint32(68) &&
496 msg.Flow.Classifier.DstPort == uint32(67) {
497 // NOTE we are receiving mulitple DHCP flows but we shouldn't call the transition multiple times
498 if err := o.InternalState.Event("start_dhcp"); err != nil {
499 log.Warnf("Can't go to dhcp_started: %v", err)
500 }
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700501 }
502}
503
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700504// HexDecode converts the hex encoding to binary
505func HexDecode(pkt []byte) []byte {
506 p := make([]byte, len(pkt)/2)
507 for i, j := 0, 0; i < len(pkt); i, j = i+2, j+1 {
508 // Go figure this ;)
509 u := (pkt[i] & 15) + (pkt[i]>>6)*9
510 l := (pkt[i+1] & 15) + (pkt[i+1]>>6)*9
511 p[j] = u<<4 + l
512 }
513 onuLogger.Tracef("Omci decoded: %x.", p)
514 return p
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700515}
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700516
517// BBR methods
518
519func sendOmciMsg(pktBytes []byte, intfId uint32, onuId uint32, sn *openolt.SerialNumber, msgType string, client openolt.OpenoltClient) {
520 omciMsg := openolt.OmciMsg{
521 IntfId: intfId,
522 OnuId: onuId,
523 Pkt: pktBytes,
524 }
525
526 if _, err := client.OmciMsgOut(context.Background(), &omciMsg); err != nil {
527 log.WithFields(log.Fields{
528 "IntfId": intfId,
529 "OnuId": onuId,
530 "SerialNumber": common.OnuSnToString(sn),
531 "Pkt": omciMsg.Pkt,
532 }).Fatalf("Failed to send MIB Reset")
533 }
534 log.WithFields(log.Fields{
535 "IntfId": intfId,
536 "OnuId": onuId,
537 "SerialNumber": common.OnuSnToString(sn),
538 "Pkt": omciMsg.Pkt,
539 }).Tracef("Sent OMCI message %s", msgType)
540}
541
542func (onu *Onu) getNextTid(highPriority ...bool) uint16 {
543 var next uint16
544 if len(highPriority) > 0 && highPriority[0] {
545 next = onu.hpTid
546 onu.hpTid += 1
547 if onu.hpTid < 0x8000 {
548 onu.hpTid = 0x8000
549 }
550 } else {
551 next = onu.tid
552 onu.tid += 1
553 if onu.tid >= 0x8000 {
554 onu.tid = 1
555 }
556 }
557 return next
558}
559
560// TODO move this method in responders/omcisim
561func (o *Onu) StartOmci(client openolt.OpenoltClient) {
562 mibReset, _ := omcilib.CreateMibResetRequest(o.getNextTid(false))
563 sendOmciMsg(mibReset, o.PonPortID, o.ID, o.SerialNumber, "mibReset", client)
564}
565
566func (o *Onu) handleOmci(msg OmciIndicationMessage, client openolt.OpenoltClient) {
567 msgType, packet := omcilib.DecodeOmci(msg.OmciInd.Pkt)
568
569 log.WithFields(log.Fields{
570 "IntfId": msg.OmciInd.IntfId,
571 "OnuId": msg.OmciInd.OnuId,
572 "OnuSn": common.OnuSnToString(o.SerialNumber),
573 "Pkt": msg.OmciInd.Pkt,
574 "msgType": msgType,
575 }).Trace("ONU Receveives OMCI Msg")
576 switch msgType {
577 default:
578 log.Fatalf("unexpected frame: %v", packet)
579 case omci.MibResetResponseType:
580 mibUpload, _ := omcilib.CreateMibUploadRequest(o.getNextTid(false))
581 sendOmciMsg(mibUpload, o.PonPortID, o.ID, o.SerialNumber, "mibUpload", client)
582 case omci.MibUploadResponseType:
583 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
584 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
585 case omci.MibUploadNextResponseType:
586 o.seqNumber++
587
588 if o.seqNumber > 290 {
589 // NOTE we are done with the MIB Upload (290 is the number of messages the omci-sim library will respond to)
590 galEnet, _ := omcilib.CreateGalEnetRequest(o.getNextTid(false))
591 sendOmciMsg(galEnet, o.PonPortID, o.ID, o.SerialNumber, "CreateGalEnetRequest", client)
592 } else {
593 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
594 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
595 }
596 case omci.CreateResponseType:
597 // NOTE Creating a GemPort,
598 // BBsim actually doesn't care about the values, so we can do we want with the parameters
599 // In the same way we can create a GemPort even without setting up UNIs/TConts/...
600 // but we need the GemPort to trigger the state change
601
602 if !o.HasGemPort {
603 // NOTE this sends a CreateRequestType and BBSim replies with a CreateResponseType
604 // thus we send this request only once
605 gemReq, _ := omcilib.CreateGemPortRequest(o.getNextTid(false))
606 sendOmciMsg(gemReq, o.PonPortID, o.ID, o.SerialNumber, "CreateGemPortRequest", client)
607 o.HasGemPort = true
608 } else {
609 if err := o.InternalState.Event("send_eapol_flow"); err != nil {
610 onuLogger.WithFields(log.Fields{
611 "OnuId": o.ID,
612 "IntfId": o.PonPortID,
613 "OnuSn": o.Sn(),
614 }).Errorf("Error while transitioning ONU State %v", err)
615 }
616 }
617
618 }
619}
620
621func (o *Onu) sendEapolFlow(client openolt.OpenoltClient) {
622
623 classifierProto := openolt.Classifier{
624 EthType: uint32(layers.EthernetTypeEAPOL),
625 OVid: 4091,
626 }
627
628 actionProto := openolt.Action{}
629
630 downstreamFlow := openolt.Flow{
631 AccessIntfId: int32(o.PonPortID),
632 OnuId: int32(o.ID),
633 UniId: int32(0x101), // FIXME do not hardcode this
634 FlowId: uint32(o.ID),
635 FlowType: "downstream",
636 AllocId: int32(0),
637 NetworkIntfId: int32(0),
638 GemportId: int32(1), // FIXME use the same value as CreateGemPortRequest PortID, do not hardcode
639 Classifier: &classifierProto,
640 Action: &actionProto,
641 Priority: int32(100),
642 Cookie: uint64(o.ID),
643 PortNo: uint32(o.ID), // NOTE we are using this to map an incoming packetIndication to an ONU
644 }
645
646 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
647 log.WithFields(log.Fields{
648 "IntfId": o.PonPortID,
649 "OnuId": o.ID,
650 "FlowId": downstreamFlow.FlowId,
651 "PortNo": downstreamFlow.PortNo,
652 "SerialNumber": common.OnuSnToString(o.SerialNumber),
653 }).Fatalf("Failed to EAPOL Flow")
654 }
655 log.WithFields(log.Fields{
656 "IntfId": o.PonPortID,
657 "OnuId": o.ID,
658 "FlowId": downstreamFlow.FlowId,
659 "PortNo": downstreamFlow.PortNo,
660 "SerialNumber": common.OnuSnToString(o.SerialNumber),
661 }).Info("Sent EAPOL Flow")
662}
663
664func (o *Onu) sendDhcpFlow(client openolt.OpenoltClient) {
665 classifierProto := openolt.Classifier{
666 EthType: uint32(layers.EthernetTypeIPv4),
667 SrcPort: uint32(68),
668 DstPort: uint32(67),
669 }
670
671 actionProto := openolt.Action{}
672
673 downstreamFlow := openolt.Flow{
674 AccessIntfId: int32(o.PonPortID),
675 OnuId: int32(o.ID),
676 UniId: int32(0x101), // FIXME do not hardcode this
677 FlowId: uint32(o.ID),
678 FlowType: "downstream",
679 AllocId: int32(0),
680 NetworkIntfId: int32(0),
681 GemportId: int32(1), // FIXME use the same value as CreateGemPortRequest PortID, do not hardcode
682 Classifier: &classifierProto,
683 Action: &actionProto,
684 Priority: int32(100),
685 Cookie: uint64(o.ID),
686 PortNo: uint32(o.ID), // NOTE we are using this to map an incoming packetIndication to an ONU
687 }
688
689 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
690 log.WithFields(log.Fields{
691 "IntfId": o.PonPortID,
692 "OnuId": o.ID,
693 "FlowId": downstreamFlow.FlowId,
694 "PortNo": downstreamFlow.PortNo,
695 "SerialNumber": common.OnuSnToString(o.SerialNumber),
696 }).Fatalf("Failed to send DHCP Flow")
697 }
698 log.WithFields(log.Fields{
699 "IntfId": o.PonPortID,
700 "OnuId": o.ID,
701 "FlowId": downstreamFlow.FlowId,
702 "PortNo": downstreamFlow.PortNo,
703 "SerialNumber": common.OnuSnToString(o.SerialNumber),
704 }).Info("Sent DHCP Flow")
705}