blob: 69721dfc260f20f06c558f6a66aff701be2db291 [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)
Matteo Scandolo813402b2019-10-23 19:24:52 -0700237 o.handleFlowUpdate(msg)
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700238 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:
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700283 msg, _ := message.Data.(OmciIndicationMessage)
284 o.handleOmci(msg, client)
285 case SendEapolFlow:
286 o.sendEapolFlow(client)
287 case SendDhcpFlow:
288 o.sendDhcpFlow(client)
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700289 default:
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700290 onuLogger.Warnf("Received unknown message data %v for type %v in OLT Channel", message.Data, message.Type)
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700291 }
292 }
Matteo Scandolo4747d292019-08-05 11:50:18 -0700293}
294
William Kurkian9dadc5b2019-10-22 13:51:57 -0400295func (o Onu) processOmciMessage(message omcisim.OmciChMessage) {
296 switch message.Type {
297 case omcisim.GemPortAdded:
298 log.WithFields(log.Fields{
299 "OnuId": message.Data.OnuId,
300 "IntfId": message.Data.IntfId,
301 }).Infof("GemPort Added")
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700302
William Kurkian9dadc5b2019-10-22 13:51:57 -0400303 // NOTE if we receive the GemPort but we don't have EAPOL flows
304 // go an intermediate state, otherwise start auth
305 if o.InternalState.Is("enabled") {
306 if err := o.InternalState.Event("add_gem_port"); err != nil {
307 log.Errorf("Can't go to gem_port_added: %v", err)
308 }
309 } else if o.InternalState.Is("eapol_flow_received") {
310 if err := o.InternalState.Event("start_auth"); err != nil {
311 log.Errorf("Can't go to auth_started: %v", err)
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700312 }
313 }
314 }
315}
316
Matteo Scandolo4747d292019-08-05 11:50:18 -0700317func (o Onu) NewSN(oltid int, intfid uint32, onuid uint32) *openolt.SerialNumber {
318
319 sn := new(openolt.SerialNumber)
320
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700321 //sn = new(openolt.SerialNumber)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700322 sn.VendorId = []byte("BBSM")
323 sn.VendorSpecific = []byte{0, byte(oltid % 256), byte(intfid), byte(onuid)}
324
325 return sn
326}
327
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700328// NOTE handle_/process methods can change the ONU internal state as they are receiving messages
329// send method should not change the ONU state
330
331func (o Onu) sendDyingGaspInd(msg DyingGaspIndicationMessage, stream openolt.Openolt_EnableIndicationServer) error {
332 alarmData := &openolt.AlarmIndication_DyingGaspInd{
333 DyingGaspInd: &openolt.DyingGaspIndication{
334 IntfId: msg.PonPortID,
335 OnuId: msg.OnuID,
336 Status: "on",
337 },
338 }
339 data := &openolt.Indication_AlarmInd{AlarmInd: &openolt.AlarmIndication{Data: alarmData}}
340
341 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
342 onuLogger.Errorf("Failed to send DyingGaspInd : %v", err)
343 return err
344 }
345 onuLogger.WithFields(log.Fields{
346 "IntfId": msg.PonPortID,
347 "OnuSn": o.Sn(),
348 "OnuId": msg.OnuID,
349 }).Info("sendDyingGaspInd")
350 return nil
351}
352
Matteo Scandolo4747d292019-08-05 11:50:18 -0700353func (o Onu) sendOnuDiscIndication(msg OnuDiscIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
354 discoverData := &openolt.Indication_OnuDiscInd{OnuDiscInd: &openolt.OnuDiscIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700355 IntfId: msg.Onu.PonPortID,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700356 SerialNumber: msg.Onu.SerialNumber,
357 }}
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700358
Matteo Scandolo4747d292019-08-05 11:50:18 -0700359 if err := stream.Send(&openolt.Indication{Data: discoverData}); err != nil {
Matteo Scandolo11006992019-08-28 11:29:46 -0700360 log.Errorf("Failed to send Indication_OnuDiscInd: %v", err)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700361 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700362
363 if err := o.InternalState.Event("discover"); err != nil {
364 oltLogger.WithFields(log.Fields{
365 "IntfId": o.PonPortID,
366 "OnuSn": o.Sn(),
367 "OnuId": o.ID,
368 }).Infof("Failed to transition ONU to discovered state: %s", err.Error())
369 }
370
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700371 onuLogger.WithFields(log.Fields{
Matteo Scandolo4747d292019-08-05 11:50:18 -0700372 "IntfId": msg.Onu.PonPortID,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700373 "OnuSn": msg.Onu.Sn(),
374 "OnuId": o.ID,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700375 }).Debug("Sent Indication_OnuDiscInd")
376}
377
378func (o Onu) sendOnuIndication(msg OnuIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
379 // NOTE voltha returns an ID, but if we use that ID then it complains:
380 // expected_onu_id: 1, received_onu_id: 1024, event: ONU-id-mismatch, can happen if both voltha and the olt rebooted
381 // so we're using the internal ID that is 1
382 // o.ID = msg.OnuID
Matteo Scandolo4747d292019-08-05 11:50:18 -0700383
384 indData := &openolt.Indication_OnuInd{OnuInd: &openolt.OnuIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700385 IntfId: o.PonPortID,
386 OnuId: o.ID,
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700387 OperState: msg.OperState.String(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700388 AdminState: o.OperState.Current(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700389 SerialNumber: o.SerialNumber,
390 }}
391 if err := stream.Send(&openolt.Indication{Data: indData}); err != nil {
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700392 // TODO do we need to transition to a broken state?
Matteo Scandolo11006992019-08-28 11:29:46 -0700393 log.Errorf("Failed to send Indication_OnuInd: %v", err)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700394 }
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700395 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700396 "IntfId": o.PonPortID,
397 "OnuId": o.ID,
398 "OperState": msg.OperState.String(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700399 "AdminState": msg.OperState.String(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700400 "OnuSn": o.Sn(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700401 }).Debug("Sent Indication_OnuInd")
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700402
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700403}
404
405func (o Onu) handleOmciMessage(msg OmciMessage, stream openolt.Openolt_EnableIndicationServer) {
406
407 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700408 "IntfId": o.PonPortID,
Matteo Scandolo27428702019-10-11 16:21:16 -0700409 "SerialNumber": o.Sn(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700410 "omciPacket": msg.omciMsg.Pkt,
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700411 }).Tracef("Received OMCI message")
412
413 var omciInd openolt.OmciIndication
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700414 respPkt, err := omcisim.OmciSim(o.PonPortID, o.ID, HexDecode(msg.omciMsg.Pkt))
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700415 if err != nil {
Matteo Scandolo27428702019-10-11 16:21:16 -0700416 onuLogger.WithFields(log.Fields{
417 "IntfId": o.PonPortID,
418 "SerialNumber": o.Sn(),
419 "omciPacket": omciInd.Pkt,
420 "msg": msg,
421 }).Errorf("Error handling OMCI message %v", msg)
422 return
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700423 }
424
425 omciInd.IntfId = o.PonPortID
426 omciInd.OnuId = o.ID
427 omciInd.Pkt = respPkt
428
429 omci := &openolt.Indication_OmciInd{OmciInd: &omciInd}
430 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
Matteo Scandolo27428702019-10-11 16:21:16 -0700431 onuLogger.WithFields(log.Fields{
432 "IntfId": o.PonPortID,
433 "SerialNumber": o.Sn(),
434 "omciPacket": omciInd.Pkt,
435 "msg": msg,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700436 }).Errorf("send omcisim indication failed: %v", err)
Matteo Scandolo27428702019-10-11 16:21:16 -0700437 return
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700438 }
439 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700440 "IntfId": o.PonPortID,
Matteo Scandolo27428702019-10-11 16:21:16 -0700441 "SerialNumber": o.Sn(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700442 "omciPacket": omciInd.Pkt,
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700443 }).Tracef("Sent OMCI message")
444}
445
Matteo Scandolo27428702019-10-11 16:21:16 -0700446func (o *Onu) storePortNumber(portNo uint32) {
Matteo Scandolo813402b2019-10-23 19:24:52 -0700447 // NOTE this needed only as long as we don't support multiple UNIs
Matteo Scandolo27428702019-10-11 16:21:16 -0700448 // we need to add support for multiple UNIs
449 // the action plan is:
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700450 // - refactor the omcisim-sim library to use https://github.com/cboling/omci instead of canned messages
Matteo Scandolo27428702019-10-11 16:21:16 -0700451 // - change the library so that it reports a single UNI and remove this workaroung
452 // - add support for multiple UNIs in BBSim
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700453 if o.PortNo == 0 || portNo < o.PortNo {
Matteo Scandolo813402b2019-10-23 19:24:52 -0700454 onuLogger.WithFields(log.Fields{
455 "IntfId": o.PonPortID,
456 "OnuId": o.ID,
457 "SerialNumber": o.Sn(),
458 "OnuPortNo": o.PortNo,
459 "FlowPortNo": portNo,
460 }).Debug("Storing ONU portNo")
Matteo Scandolo27428702019-10-11 16:21:16 -0700461 o.PortNo = portNo
462 }
463}
464
Matteo Scandolo813402b2019-10-23 19:24:52 -0700465func (o *Onu) handleFlowUpdate(msg OnuFlowUpdateMessage) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700466 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700467 "DstPort": msg.Flow.Classifier.DstPort,
468 "EthType": fmt.Sprintf("%x", msg.Flow.Classifier.EthType),
469 "FlowId": msg.Flow.FlowId,
470 "FlowType": msg.Flow.FlowType,
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700471 "InnerVlan": msg.Flow.Classifier.IVid,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700472 "IntfId": msg.Flow.AccessIntfId,
473 "IpProto": msg.Flow.Classifier.IpProto,
474 "OnuId": msg.Flow.OnuId,
475 "OnuSn": o.Sn(),
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700476 "OuterVlan": msg.Flow.Classifier.OVid,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700477 "PortNo": msg.Flow.PortNo,
478 "SrcPort": msg.Flow.Classifier.SrcPort,
479 "UniID": msg.Flow.UniId,
480 }).Debug("ONU receives Flow")
481
Matteo Scandolo813402b2019-10-23 19:24:52 -0700482 if msg.Flow.UniId != 0 {
483 // as of now BBSim only support a single UNI, so ignore everything that is not targeted to it
484 onuLogger.WithFields(log.Fields{
485 "IntfId": o.PonPortID,
486 "OnuId": o.ID,
487 "SerialNumber": o.Sn(),
488 }).Debug("Ignoring flow as it's not for the first UNI")
489 return
490 }
491
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700492 if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeEAPOL) && msg.Flow.Classifier.OVid == 4091 {
Matteo Scandolo27428702019-10-11 16:21:16 -0700493 // NOTE storing the PortNO, it's needed when sending PacketIndications
Matteo Scandolo813402b2019-10-23 19:24:52 -0700494 o.storePortNumber(uint32(msg.Flow.PortNo))
Matteo Scandolo27428702019-10-11 16:21:16 -0700495
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700496 // NOTE if we receive the EAPOL flows but we don't have GemPorts
497 // go an intermediate state, otherwise start auth
498 if o.InternalState.Is("enabled") {
499 if err := o.InternalState.Event("receive_eapol_flow"); err != nil {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700500 log.Warnf("Can't go to eapol_flow_received: %v", err)
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700501 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700502 } else if o.InternalState.Is("gem_port_added") {
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700503 if err := o.InternalState.Event("start_auth"); err != nil {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700504 log.Warnf("Can't go to auth_started: %v", err)
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700505 }
506 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700507 } else if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeIPv4) &&
508 msg.Flow.Classifier.SrcPort == uint32(68) &&
509 msg.Flow.Classifier.DstPort == uint32(67) {
510 // NOTE we are receiving mulitple DHCP flows but we shouldn't call the transition multiple times
511 if err := o.InternalState.Event("start_dhcp"); err != nil {
512 log.Warnf("Can't go to dhcp_started: %v", err)
513 }
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700514 }
515}
516
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700517// HexDecode converts the hex encoding to binary
518func HexDecode(pkt []byte) []byte {
519 p := make([]byte, len(pkt)/2)
520 for i, j := 0, 0; i < len(pkt); i, j = i+2, j+1 {
521 // Go figure this ;)
522 u := (pkt[i] & 15) + (pkt[i]>>6)*9
523 l := (pkt[i+1] & 15) + (pkt[i+1]>>6)*9
524 p[j] = u<<4 + l
525 }
526 onuLogger.Tracef("Omci decoded: %x.", p)
527 return p
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700528}
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700529
530// BBR methods
531
532func sendOmciMsg(pktBytes []byte, intfId uint32, onuId uint32, sn *openolt.SerialNumber, msgType string, client openolt.OpenoltClient) {
533 omciMsg := openolt.OmciMsg{
534 IntfId: intfId,
535 OnuId: onuId,
536 Pkt: pktBytes,
537 }
538
539 if _, err := client.OmciMsgOut(context.Background(), &omciMsg); err != nil {
540 log.WithFields(log.Fields{
541 "IntfId": intfId,
542 "OnuId": onuId,
543 "SerialNumber": common.OnuSnToString(sn),
544 "Pkt": omciMsg.Pkt,
545 }).Fatalf("Failed to send MIB Reset")
546 }
547 log.WithFields(log.Fields{
548 "IntfId": intfId,
549 "OnuId": onuId,
550 "SerialNumber": common.OnuSnToString(sn),
551 "Pkt": omciMsg.Pkt,
552 }).Tracef("Sent OMCI message %s", msgType)
553}
554
555func (onu *Onu) getNextTid(highPriority ...bool) uint16 {
556 var next uint16
557 if len(highPriority) > 0 && highPriority[0] {
558 next = onu.hpTid
559 onu.hpTid += 1
560 if onu.hpTid < 0x8000 {
561 onu.hpTid = 0x8000
562 }
563 } else {
564 next = onu.tid
565 onu.tid += 1
566 if onu.tid >= 0x8000 {
567 onu.tid = 1
568 }
569 }
570 return next
571}
572
573// TODO move this method in responders/omcisim
574func (o *Onu) StartOmci(client openolt.OpenoltClient) {
575 mibReset, _ := omcilib.CreateMibResetRequest(o.getNextTid(false))
576 sendOmciMsg(mibReset, o.PonPortID, o.ID, o.SerialNumber, "mibReset", client)
577}
578
579func (o *Onu) handleOmci(msg OmciIndicationMessage, client openolt.OpenoltClient) {
580 msgType, packet := omcilib.DecodeOmci(msg.OmciInd.Pkt)
581
582 log.WithFields(log.Fields{
583 "IntfId": msg.OmciInd.IntfId,
584 "OnuId": msg.OmciInd.OnuId,
585 "OnuSn": common.OnuSnToString(o.SerialNumber),
586 "Pkt": msg.OmciInd.Pkt,
587 "msgType": msgType,
588 }).Trace("ONU Receveives OMCI Msg")
589 switch msgType {
590 default:
Matteo Scandolo813402b2019-10-23 19:24:52 -0700591 log.WithFields(log.Fields{
592 "IntfId": msg.OmciInd.IntfId,
593 "OnuId": msg.OmciInd.OnuId,
594 "OnuSn": common.OnuSnToString(o.SerialNumber),
595 "Pkt": msg.OmciInd.Pkt,
596 "msgType": msgType,
597 }).Fatalf("unexpected frame: %v", packet)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700598 case omci.MibResetResponseType:
599 mibUpload, _ := omcilib.CreateMibUploadRequest(o.getNextTid(false))
600 sendOmciMsg(mibUpload, o.PonPortID, o.ID, o.SerialNumber, "mibUpload", client)
601 case omci.MibUploadResponseType:
602 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
603 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
604 case omci.MibUploadNextResponseType:
605 o.seqNumber++
606
607 if o.seqNumber > 290 {
608 // NOTE we are done with the MIB Upload (290 is the number of messages the omci-sim library will respond to)
609 galEnet, _ := omcilib.CreateGalEnetRequest(o.getNextTid(false))
610 sendOmciMsg(galEnet, o.PonPortID, o.ID, o.SerialNumber, "CreateGalEnetRequest", client)
611 } else {
612 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
613 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
614 }
615 case omci.CreateResponseType:
616 // NOTE Creating a GemPort,
617 // BBsim actually doesn't care about the values, so we can do we want with the parameters
618 // In the same way we can create a GemPort even without setting up UNIs/TConts/...
619 // but we need the GemPort to trigger the state change
620
621 if !o.HasGemPort {
622 // NOTE this sends a CreateRequestType and BBSim replies with a CreateResponseType
623 // thus we send this request only once
624 gemReq, _ := omcilib.CreateGemPortRequest(o.getNextTid(false))
625 sendOmciMsg(gemReq, o.PonPortID, o.ID, o.SerialNumber, "CreateGemPortRequest", client)
626 o.HasGemPort = true
627 } else {
628 if err := o.InternalState.Event("send_eapol_flow"); err != nil {
629 onuLogger.WithFields(log.Fields{
630 "OnuId": o.ID,
631 "IntfId": o.PonPortID,
632 "OnuSn": o.Sn(),
633 }).Errorf("Error while transitioning ONU State %v", err)
634 }
635 }
636
637 }
638}
639
640func (o *Onu) sendEapolFlow(client openolt.OpenoltClient) {
641
642 classifierProto := openolt.Classifier{
643 EthType: uint32(layers.EthernetTypeEAPOL),
644 OVid: 4091,
645 }
646
647 actionProto := openolt.Action{}
648
649 downstreamFlow := openolt.Flow{
650 AccessIntfId: int32(o.PonPortID),
651 OnuId: int32(o.ID),
Matteo Scandolo813402b2019-10-23 19:24:52 -0700652 UniId: int32(0), // NOTE do not hardcode this, we need to support multiple UNIs
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700653 FlowId: uint32(o.ID),
654 FlowType: "downstream",
655 AllocId: int32(0),
656 NetworkIntfId: int32(0),
657 GemportId: int32(1), // FIXME use the same value as CreateGemPortRequest PortID, do not hardcode
658 Classifier: &classifierProto,
659 Action: &actionProto,
660 Priority: int32(100),
661 Cookie: uint64(o.ID),
662 PortNo: uint32(o.ID), // NOTE we are using this to map an incoming packetIndication to an ONU
663 }
664
665 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
666 log.WithFields(log.Fields{
667 "IntfId": o.PonPortID,
668 "OnuId": o.ID,
669 "FlowId": downstreamFlow.FlowId,
670 "PortNo": downstreamFlow.PortNo,
671 "SerialNumber": common.OnuSnToString(o.SerialNumber),
672 }).Fatalf("Failed to EAPOL Flow")
673 }
674 log.WithFields(log.Fields{
675 "IntfId": o.PonPortID,
676 "OnuId": o.ID,
677 "FlowId": downstreamFlow.FlowId,
678 "PortNo": downstreamFlow.PortNo,
679 "SerialNumber": common.OnuSnToString(o.SerialNumber),
680 }).Info("Sent EAPOL Flow")
681}
682
683func (o *Onu) sendDhcpFlow(client openolt.OpenoltClient) {
684 classifierProto := openolt.Classifier{
685 EthType: uint32(layers.EthernetTypeIPv4),
686 SrcPort: uint32(68),
687 DstPort: uint32(67),
688 }
689
690 actionProto := openolt.Action{}
691
692 downstreamFlow := openolt.Flow{
693 AccessIntfId: int32(o.PonPortID),
694 OnuId: int32(o.ID),
Matteo Scandolo813402b2019-10-23 19:24:52 -0700695 UniId: int32(0), // FIXME do not hardcode this
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700696 FlowId: uint32(o.ID),
697 FlowType: "downstream",
698 AllocId: int32(0),
699 NetworkIntfId: int32(0),
700 GemportId: int32(1), // FIXME use the same value as CreateGemPortRequest PortID, do not hardcode
701 Classifier: &classifierProto,
702 Action: &actionProto,
703 Priority: int32(100),
704 Cookie: uint64(o.ID),
705 PortNo: uint32(o.ID), // NOTE we are using this to map an incoming packetIndication to an ONU
706 }
707
708 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
709 log.WithFields(log.Fields{
710 "IntfId": o.PonPortID,
711 "OnuId": o.ID,
712 "FlowId": downstreamFlow.FlowId,
713 "PortNo": downstreamFlow.PortNo,
714 "SerialNumber": common.OnuSnToString(o.SerialNumber),
715 }).Fatalf("Failed to send DHCP Flow")
716 }
717 log.WithFields(log.Fields{
718 "IntfId": o.PonPortID,
719 "OnuId": o.ID,
720 "FlowId": downstreamFlow.FlowId,
721 "PortNo": downstreamFlow.PortNo,
722 "SerialNumber": common.OnuSnToString(o.SerialNumber),
723 }).Info("Sent DHCP Flow")
724}