blob: f4ef84b9a5985ab2eff4dbe76cf7906b4f8aa8e2 [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
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700297func (o Onu) processOmciMessages(stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700298 ch := omcisim.GetChannel()
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700299
300 onuLogger.WithFields(log.Fields{
301 "onuID": o.ID,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700302 "onuSN": o.Sn(),
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700303 }).Debug("Started OMCI Indication Channel")
304
305 for message := range ch {
306 switch message.Type {
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700307 case omcisim.GemPortAdded:
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700308 log.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700309 "OnuId": message.Data.OnuId,
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700310 "IntfId": message.Data.IntfId,
311 }).Infof("GemPort Added")
312
313 // NOTE if we receive the GemPort but we don't have EAPOL flows
314 // go an intermediate state, otherwise start auth
315 if o.InternalState.Is("enabled") {
316 if err := o.InternalState.Event("add_gem_port"); err != nil {
317 log.Errorf("Can't go to gem_port_added: %v", err)
318 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700319 } else if o.InternalState.Is("eapol_flow_received") {
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700320 if err := o.InternalState.Event("start_auth"); err != nil {
321 log.Errorf("Can't go to auth_started: %v", err)
322 }
323 }
324 }
325 }
326}
327
Matteo Scandolo4747d292019-08-05 11:50:18 -0700328func (o Onu) NewSN(oltid int, intfid uint32, onuid uint32) *openolt.SerialNumber {
329
330 sn := new(openolt.SerialNumber)
331
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700332 //sn = new(openolt.SerialNumber)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700333 sn.VendorId = []byte("BBSM")
334 sn.VendorSpecific = []byte{0, byte(oltid % 256), byte(intfid), byte(onuid)}
335
336 return sn
337}
338
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700339// NOTE handle_/process methods can change the ONU internal state as they are receiving messages
340// send method should not change the ONU state
341
342func (o Onu) sendDyingGaspInd(msg DyingGaspIndicationMessage, stream openolt.Openolt_EnableIndicationServer) error {
343 alarmData := &openolt.AlarmIndication_DyingGaspInd{
344 DyingGaspInd: &openolt.DyingGaspIndication{
345 IntfId: msg.PonPortID,
346 OnuId: msg.OnuID,
347 Status: "on",
348 },
349 }
350 data := &openolt.Indication_AlarmInd{AlarmInd: &openolt.AlarmIndication{Data: alarmData}}
351
352 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
353 onuLogger.Errorf("Failed to send DyingGaspInd : %v", err)
354 return err
355 }
356 onuLogger.WithFields(log.Fields{
357 "IntfId": msg.PonPortID,
358 "OnuSn": o.Sn(),
359 "OnuId": msg.OnuID,
360 }).Info("sendDyingGaspInd")
361 return nil
362}
363
Matteo Scandolo4747d292019-08-05 11:50:18 -0700364func (o Onu) sendOnuDiscIndication(msg OnuDiscIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
365 discoverData := &openolt.Indication_OnuDiscInd{OnuDiscInd: &openolt.OnuDiscIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700366 IntfId: msg.Onu.PonPortID,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700367 SerialNumber: msg.Onu.SerialNumber,
368 }}
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700369
Matteo Scandolo4747d292019-08-05 11:50:18 -0700370 if err := stream.Send(&openolt.Indication{Data: discoverData}); err != nil {
Matteo Scandolo11006992019-08-28 11:29:46 -0700371 log.Errorf("Failed to send Indication_OnuDiscInd: %v", err)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700372 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700373
374 if err := o.InternalState.Event("discover"); err != nil {
375 oltLogger.WithFields(log.Fields{
376 "IntfId": o.PonPortID,
377 "OnuSn": o.Sn(),
378 "OnuId": o.ID,
379 }).Infof("Failed to transition ONU to discovered state: %s", err.Error())
380 }
381
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700382 onuLogger.WithFields(log.Fields{
Matteo Scandolo4747d292019-08-05 11:50:18 -0700383 "IntfId": msg.Onu.PonPortID,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700384 "OnuSn": msg.Onu.Sn(),
385 "OnuId": o.ID,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700386 }).Debug("Sent Indication_OnuDiscInd")
387}
388
389func (o Onu) sendOnuIndication(msg OnuIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
390 // NOTE voltha returns an ID, but if we use that ID then it complains:
391 // expected_onu_id: 1, received_onu_id: 1024, event: ONU-id-mismatch, can happen if both voltha and the olt rebooted
392 // so we're using the internal ID that is 1
393 // o.ID = msg.OnuID
Matteo Scandolo4747d292019-08-05 11:50:18 -0700394
395 indData := &openolt.Indication_OnuInd{OnuInd: &openolt.OnuIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700396 IntfId: o.PonPortID,
397 OnuId: o.ID,
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700398 OperState: msg.OperState.String(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700399 AdminState: o.OperState.Current(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700400 SerialNumber: o.SerialNumber,
401 }}
402 if err := stream.Send(&openolt.Indication{Data: indData}); err != nil {
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700403 // TODO do we need to transition to a broken state?
Matteo Scandolo11006992019-08-28 11:29:46 -0700404 log.Errorf("Failed to send Indication_OnuInd: %v", err)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700405 }
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700406 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700407 "IntfId": o.PonPortID,
408 "OnuId": o.ID,
409 "OperState": msg.OperState.String(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700410 "AdminState": msg.OperState.String(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700411 "OnuSn": o.Sn(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700412 }).Debug("Sent Indication_OnuInd")
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700413
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700414}
415
416func (o Onu) handleOmciMessage(msg OmciMessage, stream openolt.Openolt_EnableIndicationServer) {
417
418 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700419 "IntfId": o.PonPortID,
Matteo Scandolo27428702019-10-11 16:21:16 -0700420 "SerialNumber": o.Sn(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700421 "omciPacket": msg.omciMsg.Pkt,
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700422 }).Tracef("Received OMCI message")
423
424 var omciInd openolt.OmciIndication
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700425 respPkt, err := omcisim.OmciSim(o.PonPortID, o.ID, HexDecode(msg.omciMsg.Pkt))
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700426 if err != nil {
Matteo Scandolo27428702019-10-11 16:21:16 -0700427 onuLogger.WithFields(log.Fields{
428 "IntfId": o.PonPortID,
429 "SerialNumber": o.Sn(),
430 "omciPacket": omciInd.Pkt,
431 "msg": msg,
432 }).Errorf("Error handling OMCI message %v", msg)
433 return
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700434 }
435
436 omciInd.IntfId = o.PonPortID
437 omciInd.OnuId = o.ID
438 omciInd.Pkt = respPkt
439
440 omci := &openolt.Indication_OmciInd{OmciInd: &omciInd}
441 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
Matteo Scandolo27428702019-10-11 16:21:16 -0700442 onuLogger.WithFields(log.Fields{
443 "IntfId": o.PonPortID,
444 "SerialNumber": o.Sn(),
445 "omciPacket": omciInd.Pkt,
446 "msg": msg,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700447 }).Errorf("send omcisim indication failed: %v", err)
Matteo Scandolo27428702019-10-11 16:21:16 -0700448 return
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700449 }
450 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700451 "IntfId": o.PonPortID,
Matteo Scandolo27428702019-10-11 16:21:16 -0700452 "SerialNumber": o.Sn(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700453 "omciPacket": omciInd.Pkt,
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700454 }).Tracef("Sent OMCI message")
455}
456
Matteo Scandolo27428702019-10-11 16:21:16 -0700457func (o *Onu) storePortNumber(portNo uint32) {
458 // FIXME this is a workaround to always use the SN-1 entry in sadis,
459 // we need to add support for multiple UNIs
460 // the action plan is:
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700461 // - refactor the omcisim-sim library to use https://github.com/cboling/omci instead of canned messages
Matteo Scandolo27428702019-10-11 16:21:16 -0700462 // - change the library so that it reports a single UNI and remove this workaroung
463 // - add support for multiple UNIs in BBSim
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700464 if o.PortNo == 0 || portNo < o.PortNo {
Matteo Scandolo27428702019-10-11 16:21:16 -0700465 o.PortNo = portNo
466 }
467}
468
469func (o *Onu) handleFlowUpdate(msg OnuFlowUpdateMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700470 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700471 "DstPort": msg.Flow.Classifier.DstPort,
472 "EthType": fmt.Sprintf("%x", msg.Flow.Classifier.EthType),
473 "FlowId": msg.Flow.FlowId,
474 "FlowType": msg.Flow.FlowType,
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700475 "InnerVlan": msg.Flow.Classifier.IVid,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700476 "IntfId": msg.Flow.AccessIntfId,
477 "IpProto": msg.Flow.Classifier.IpProto,
478 "OnuId": msg.Flow.OnuId,
479 "OnuSn": o.Sn(),
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700480 "OuterVlan": msg.Flow.Classifier.OVid,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700481 "PortNo": msg.Flow.PortNo,
482 "SrcPort": msg.Flow.Classifier.SrcPort,
483 "UniID": msg.Flow.UniId,
484 }).Debug("ONU receives Flow")
485
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700486 if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeEAPOL) && msg.Flow.Classifier.OVid == 4091 {
Matteo Scandolo27428702019-10-11 16:21:16 -0700487 // NOTE storing the PortNO, it's needed when sending PacketIndications
488 if o.PortNo == 0 {
489 o.storePortNumber(uint32(msg.Flow.PortNo))
490 }
491
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700492 // NOTE if we receive the EAPOL flows but we don't have GemPorts
493 // go an intermediate state, otherwise start auth
494 if o.InternalState.Is("enabled") {
495 if err := o.InternalState.Event("receive_eapol_flow"); err != nil {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700496 log.Warnf("Can't go to eapol_flow_received: %v", err)
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700497 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700498 } else if o.InternalState.Is("gem_port_added") {
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700499 if err := o.InternalState.Event("start_auth"); err != nil {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700500 log.Warnf("Can't go to auth_started: %v", err)
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700501 }
502 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700503 } else if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeIPv4) &&
504 msg.Flow.Classifier.SrcPort == uint32(68) &&
505 msg.Flow.Classifier.DstPort == uint32(67) {
506 // NOTE we are receiving mulitple DHCP flows but we shouldn't call the transition multiple times
507 if err := o.InternalState.Event("start_dhcp"); err != nil {
508 log.Warnf("Can't go to dhcp_started: %v", err)
509 }
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700510 }
511}
512
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700513// HexDecode converts the hex encoding to binary
514func HexDecode(pkt []byte) []byte {
515 p := make([]byte, len(pkt)/2)
516 for i, j := 0, 0; i < len(pkt); i, j = i+2, j+1 {
517 // Go figure this ;)
518 u := (pkt[i] & 15) + (pkt[i]>>6)*9
519 l := (pkt[i+1] & 15) + (pkt[i+1]>>6)*9
520 p[j] = u<<4 + l
521 }
522 onuLogger.Tracef("Omci decoded: %x.", p)
523 return p
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700524}
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700525
526// BBR methods
527
528func sendOmciMsg(pktBytes []byte, intfId uint32, onuId uint32, sn *openolt.SerialNumber, msgType string, client openolt.OpenoltClient) {
529 omciMsg := openolt.OmciMsg{
530 IntfId: intfId,
531 OnuId: onuId,
532 Pkt: pktBytes,
533 }
534
535 if _, err := client.OmciMsgOut(context.Background(), &omciMsg); err != nil {
536 log.WithFields(log.Fields{
537 "IntfId": intfId,
538 "OnuId": onuId,
539 "SerialNumber": common.OnuSnToString(sn),
540 "Pkt": omciMsg.Pkt,
541 }).Fatalf("Failed to send MIB Reset")
542 }
543 log.WithFields(log.Fields{
544 "IntfId": intfId,
545 "OnuId": onuId,
546 "SerialNumber": common.OnuSnToString(sn),
547 "Pkt": omciMsg.Pkt,
548 }).Tracef("Sent OMCI message %s", msgType)
549}
550
551func (onu *Onu) getNextTid(highPriority ...bool) uint16 {
552 var next uint16
553 if len(highPriority) > 0 && highPriority[0] {
554 next = onu.hpTid
555 onu.hpTid += 1
556 if onu.hpTid < 0x8000 {
557 onu.hpTid = 0x8000
558 }
559 } else {
560 next = onu.tid
561 onu.tid += 1
562 if onu.tid >= 0x8000 {
563 onu.tid = 1
564 }
565 }
566 return next
567}
568
569// TODO move this method in responders/omcisim
570func (o *Onu) StartOmci(client openolt.OpenoltClient) {
571 mibReset, _ := omcilib.CreateMibResetRequest(o.getNextTid(false))
572 sendOmciMsg(mibReset, o.PonPortID, o.ID, o.SerialNumber, "mibReset", client)
573}
574
575func (o *Onu) handleOmci(msg OmciIndicationMessage, client openolt.OpenoltClient) {
576 msgType, packet := omcilib.DecodeOmci(msg.OmciInd.Pkt)
577
578 log.WithFields(log.Fields{
579 "IntfId": msg.OmciInd.IntfId,
580 "OnuId": msg.OmciInd.OnuId,
581 "OnuSn": common.OnuSnToString(o.SerialNumber),
582 "Pkt": msg.OmciInd.Pkt,
583 "msgType": msgType,
584 }).Trace("ONU Receveives OMCI Msg")
585 switch msgType {
586 default:
587 log.Fatalf("unexpected frame: %v", packet)
588 case omci.MibResetResponseType:
589 mibUpload, _ := omcilib.CreateMibUploadRequest(o.getNextTid(false))
590 sendOmciMsg(mibUpload, o.PonPortID, o.ID, o.SerialNumber, "mibUpload", client)
591 case omci.MibUploadResponseType:
592 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
593 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
594 case omci.MibUploadNextResponseType:
595 o.seqNumber++
596
597 if o.seqNumber > 290 {
598 // NOTE we are done with the MIB Upload (290 is the number of messages the omci-sim library will respond to)
599 galEnet, _ := omcilib.CreateGalEnetRequest(o.getNextTid(false))
600 sendOmciMsg(galEnet, o.PonPortID, o.ID, o.SerialNumber, "CreateGalEnetRequest", client)
601 } else {
602 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
603 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
604 }
605 case omci.CreateResponseType:
606 // NOTE Creating a GemPort,
607 // BBsim actually doesn't care about the values, so we can do we want with the parameters
608 // In the same way we can create a GemPort even without setting up UNIs/TConts/...
609 // but we need the GemPort to trigger the state change
610
611 if !o.HasGemPort {
612 // NOTE this sends a CreateRequestType and BBSim replies with a CreateResponseType
613 // thus we send this request only once
614 gemReq, _ := omcilib.CreateGemPortRequest(o.getNextTid(false))
615 sendOmciMsg(gemReq, o.PonPortID, o.ID, o.SerialNumber, "CreateGemPortRequest", client)
616 o.HasGemPort = true
617 } else {
618 if err := o.InternalState.Event("send_eapol_flow"); err != nil {
619 onuLogger.WithFields(log.Fields{
620 "OnuId": o.ID,
621 "IntfId": o.PonPortID,
622 "OnuSn": o.Sn(),
623 }).Errorf("Error while transitioning ONU State %v", err)
624 }
625 }
626
627 }
628}
629
630func (o *Onu) sendEapolFlow(client openolt.OpenoltClient) {
631
632 classifierProto := openolt.Classifier{
633 EthType: uint32(layers.EthernetTypeEAPOL),
634 OVid: 4091,
635 }
636
637 actionProto := openolt.Action{}
638
639 downstreamFlow := openolt.Flow{
640 AccessIntfId: int32(o.PonPortID),
641 OnuId: int32(o.ID),
642 UniId: int32(0x101), // FIXME do not hardcode this
643 FlowId: uint32(o.ID),
644 FlowType: "downstream",
645 AllocId: int32(0),
646 NetworkIntfId: int32(0),
647 GemportId: int32(1), // FIXME use the same value as CreateGemPortRequest PortID, do not hardcode
648 Classifier: &classifierProto,
649 Action: &actionProto,
650 Priority: int32(100),
651 Cookie: uint64(o.ID),
652 PortNo: uint32(o.ID), // NOTE we are using this to map an incoming packetIndication to an ONU
653 }
654
655 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
656 log.WithFields(log.Fields{
657 "IntfId": o.PonPortID,
658 "OnuId": o.ID,
659 "FlowId": downstreamFlow.FlowId,
660 "PortNo": downstreamFlow.PortNo,
661 "SerialNumber": common.OnuSnToString(o.SerialNumber),
662 }).Fatalf("Failed to EAPOL Flow")
663 }
664 log.WithFields(log.Fields{
665 "IntfId": o.PonPortID,
666 "OnuId": o.ID,
667 "FlowId": downstreamFlow.FlowId,
668 "PortNo": downstreamFlow.PortNo,
669 "SerialNumber": common.OnuSnToString(o.SerialNumber),
670 }).Info("Sent EAPOL Flow")
671}
672
673func (o *Onu) sendDhcpFlow(client openolt.OpenoltClient) {
674 classifierProto := openolt.Classifier{
675 EthType: uint32(layers.EthernetTypeIPv4),
676 SrcPort: uint32(68),
677 DstPort: uint32(67),
678 }
679
680 actionProto := openolt.Action{}
681
682 downstreamFlow := openolt.Flow{
683 AccessIntfId: int32(o.PonPortID),
684 OnuId: int32(o.ID),
685 UniId: int32(0x101), // FIXME do not hardcode this
686 FlowId: uint32(o.ID),
687 FlowType: "downstream",
688 AllocId: int32(0),
689 NetworkIntfId: int32(0),
690 GemportId: int32(1), // FIXME use the same value as CreateGemPortRequest PortID, do not hardcode
691 Classifier: &classifierProto,
692 Action: &actionProto,
693 Priority: int32(100),
694 Cookie: uint64(o.ID),
695 PortNo: uint32(o.ID), // NOTE we are using this to map an incoming packetIndication to an ONU
696 }
697
698 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
699 log.WithFields(log.Fields{
700 "IntfId": o.PonPortID,
701 "OnuId": o.ID,
702 "FlowId": downstreamFlow.FlowId,
703 "PortNo": downstreamFlow.PortNo,
704 "SerialNumber": common.OnuSnToString(o.SerialNumber),
705 }).Fatalf("Failed to send DHCP Flow")
706 }
707 log.WithFields(log.Fields{
708 "IntfId": o.PonPortID,
709 "OnuId": o.ID,
710 "FlowId": downstreamFlow.FlowId,
711 "PortNo": downstreamFlow.PortNo,
712 "SerialNumber": common.OnuSnToString(o.SerialNumber),
713 }).Info("Sent DHCP Flow")
714}