blob: cc467b85f9510bebf0015940380c81b8cafefe33 [file] [log] [blame]
Matteo Scandolo11006992019-08-28 11:29:46 -07001/*
2 * Copyright 2018-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Matteo Scandolo4747d292019-08-05 11:50:18 -070017package devices
18
19import (
Matteo Scandolo40e067f2019-10-16 16:59:41 -070020 "context"
Matteo Scandolo99f18462019-10-28 14:14:28 -070021 "errors"
Matteo Scandolo3bc73742019-08-20 14:04:04 -070022 "fmt"
Matteo Scandolo40e067f2019-10-16 16:59:41 -070023 "github.com/cboling/omci"
Matteo Scandolo3bc73742019-08-20 14:04:04 -070024 "github.com/google/gopacket/layers"
Matteo Scandolo4747d292019-08-05 11:50:18 -070025 "github.com/looplab/fsm"
Matteo Scandolo075b1892019-10-07 12:11:07 -070026 "github.com/opencord/bbsim/internal/bbsim/packetHandlers"
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070027 "github.com/opencord/bbsim/internal/bbsim/responders/dhcp"
28 "github.com/opencord/bbsim/internal/bbsim/responders/eapol"
Matteo Scandolo40e067f2019-10-16 16:59:41 -070029 "github.com/opencord/bbsim/internal/common"
30 omcilib "github.com/opencord/bbsim/internal/common/omci"
31 omcisim "github.com/opencord/omci-sim"
Matteo Scandolo3bc73742019-08-20 14:04:04 -070032 "github.com/opencord/voltha-protos/go/openolt"
Matteo Scandolo4747d292019-08-05 11:50:18 -070033 log "github.com/sirupsen/logrus"
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070034 "net"
Matteo Scandolo4747d292019-08-05 11:50:18 -070035)
36
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070037var onuLogger = log.WithFields(log.Fields{
38 "module": "ONU",
39})
40
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070041type Onu struct {
Matteo Scandolo27428702019-10-11 16:21:16 -070042 ID uint32
43 PonPortID uint32
44 PonPort PonPort
45 STag int
46 CTag int
47 // PortNo comes with flows and it's used when sending packetIndications,
48 // There is one PortNo per UNI Port, for now we're only storing the first one
49 // FIXME add support for multiple UNIs
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070050 HwAddress net.HardwareAddr
51 InternalState *fsm.FSM
52
Matteo Scandolo99f18462019-10-28 14:14:28 -070053 // ONU State
54 PortNo uint32
55 DhcpFlowReceived bool
56
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070057 OperState *fsm.FSM
58 SerialNumber *openolt.SerialNumber
59
60 Channel chan Message // this Channel is to track state changes OMCI messages, EAPOL and DHCP packets
Matteo Scandolo40e067f2019-10-16 16:59:41 -070061
62 // OMCI params
63 tid uint16
64 hpTid uint16
65 seqNumber uint16
66 HasGemPort bool
67
68 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 -070069}
70
Matteo Scandolo99f18462019-10-28 14:14:28 -070071func (o *Onu) Sn() string {
Matteo Scandolo40e067f2019-10-16 16:59:41 -070072 return common.OnuSnToString(o.SerialNumber)
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070073}
74
Matteo Scandolo27428702019-10-11 16:21:16 -070075func CreateONU(olt OltDevice, pon PonPort, id uint32, sTag int, cTag int) *Onu {
Matteo Scandolo4747d292019-08-05 11:50:18 -070076
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070077 o := Onu{
Matteo Scandolo99f18462019-10-28 14:14:28 -070078 ID: id,
79 PonPortID: pon.ID,
80 PonPort: pon,
81 STag: sTag,
82 CTag: cTag,
83 HwAddress: net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, byte(pon.ID), byte(id)},
84 PortNo: 0,
85 Channel: make(chan Message, 2048),
86 tid: 0x1,
87 hpTid: 0x8000,
88 seqNumber: 0,
89 DoneChannel: make(chan bool, 1),
90 DhcpFlowReceived: false,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070091 }
92 o.SerialNumber = o.NewSN(olt.ID, pon.ID, o.ID)
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070093
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070094 // NOTE this state machine is used to track the operational
95 // state as requested by VOLTHA
96 o.OperState = getOperStateFSM(func(e *fsm.Event) {
97 onuLogger.WithFields(log.Fields{
98 "ID": o.ID,
99 }).Debugf("Changing ONU OperState from %s to %s", e.Src, e.Dst)
100 })
101
102 // NOTE this state machine is used to activate the OMCI, EAPOL and DHCP clients
103 o.InternalState = fsm.NewFSM(
104 "created",
105 fsm.Events{
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700106 // DEVICE Lifecycle
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700107 {Name: "discover", Src: []string{"created"}, Dst: "discovered"},
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700108 {Name: "enable", Src: []string{"discovered", "disabled"}, Dst: "enabled"},
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700109 {Name: "receive_eapol_flow", Src: []string{"enabled", "gem_port_added"}, Dst: "eapol_flow_received"},
110 {Name: "add_gem_port", Src: []string{"enabled", "eapol_flow_received"}, Dst: "gem_port_added"},
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700111 // NOTE should disabled state be diffente for oper_disabled (emulating an error) and admin_disabled (received a disabled call via VOLTHA)?
112 {Name: "disable", Src: []string{"eap_response_success_received", "auth_failed", "dhcp_ack_received", "dhcp_failed"}, Dst: "disabled"},
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700113 // EAPOL
Matteo Scandoloe383d5d2019-10-25 14:47:27 -0700114 {Name: "start_auth", Src: []string{"eapol_flow_received", "gem_port_added", "eap_response_success_received", "auth_failed", "dhcp_ack_received", "dhcp_failed"}, Dst: "auth_started"},
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700115 {Name: "eap_start_sent", Src: []string{"auth_started"}, Dst: "eap_start_sent"},
116 {Name: "eap_response_identity_sent", Src: []string{"eap_start_sent"}, Dst: "eap_response_identity_sent"},
117 {Name: "eap_response_challenge_sent", Src: []string{"eap_response_identity_sent"}, Dst: "eap_response_challenge_sent"},
118 {Name: "eap_response_success_received", Src: []string{"eap_response_challenge_sent"}, Dst: "eap_response_success_received"},
119 {Name: "auth_failed", Src: []string{"auth_started", "eap_start_sent", "eap_response_identity_sent", "eap_response_challenge_sent"}, Dst: "auth_failed"},
120 // DHCP
Matteo Scandolofe9ac252019-10-25 11:40:17 -0700121 {Name: "start_dhcp", Src: []string{"eap_response_success_received", "dhcp_discovery_sent", "dhcp_request_sent", "dhcp_ack_received", "dhcp_failed"}, Dst: "dhcp_started"},
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700122 {Name: "dhcp_discovery_sent", Src: []string{"dhcp_started"}, Dst: "dhcp_discovery_sent"},
123 {Name: "dhcp_request_sent", Src: []string{"dhcp_discovery_sent"}, Dst: "dhcp_request_sent"},
124 {Name: "dhcp_ack_received", Src: []string{"dhcp_request_sent"}, Dst: "dhcp_ack_received"},
125 {Name: "dhcp_failed", Src: []string{"dhcp_started", "dhcp_discovery_sent", "dhcp_request_sent"}, Dst: "dhcp_failed"},
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700126 // BBR States
127 // TODO add start OMCI state
128 {Name: "send_eapol_flow", Src: []string{"created"}, Dst: "eapol_flow_sent"},
129 {Name: "send_dhcp_flow", Src: []string{"eapol_flow_sent"}, Dst: "dhcp_flow_sent"},
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700130 },
131 fsm.Callbacks{
132 "enter_state": func(e *fsm.Event) {
133 o.logStateChange(e.Src, e.Dst)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700134 },
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700135 "enter_enabled": func(event *fsm.Event) {
136 msg := Message{
137 Type: OnuIndication,
138 Data: OnuIndicationMessage{
139 OnuSN: o.SerialNumber,
140 PonPortID: o.PonPortID,
141 OperState: UP,
142 },
143 }
144 o.Channel <- msg
145 },
146 "enter_disabled": func(event *fsm.Event) {
147 msg := Message{
148 Type: OnuIndication,
149 Data: OnuIndicationMessage{
150 OnuSN: o.SerialNumber,
151 PonPortID: o.PonPortID,
152 OperState: DOWN,
153 },
154 }
155 o.Channel <- msg
156 },
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700157 "enter_auth_started": func(e *fsm.Event) {
158 o.logStateChange(e.Src, e.Dst)
159 msg := Message{
160 Type: StartEAPOL,
161 Data: PacketMessage{
162 PonPortID: o.PonPortID,
163 OnuID: o.ID,
164 },
165 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700166 o.Channel <- msg
Matteo Scandolo4747d292019-08-05 11:50:18 -0700167 },
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700168 "enter_auth_failed": func(e *fsm.Event) {
169 onuLogger.WithFields(log.Fields{
170 "OnuId": o.ID,
171 "IntfId": o.PonPortID,
172 "OnuSn": o.Sn(),
173 }).Errorf("ONU failed to authenticate!")
174 },
Matteo Scandolo99f18462019-10-28 14:14:28 -0700175 "before_start_dhcp": func(e *fsm.Event) {
176 if o.DhcpFlowReceived == false {
177 e.Cancel(errors.New("cannot-go-to-dhcp-started-as-dhcp-flow-is-missing"))
178 }
179 },
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700180 "enter_dhcp_started": func(e *fsm.Event) {
181 msg := Message{
182 Type: StartDHCP,
183 Data: PacketMessage{
184 PonPortID: o.PonPortID,
185 OnuID: o.ID,
186 },
187 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700188 o.Channel <- msg
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700189 },
190 "enter_dhcp_failed": func(e *fsm.Event) {
191 onuLogger.WithFields(log.Fields{
192 "OnuId": o.ID,
193 "IntfId": o.PonPortID,
194 "OnuSn": o.Sn(),
195 }).Errorf("ONU failed to DHCP!")
196 },
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700197 "enter_eapol_flow_sent": func(e *fsm.Event) {
198 msg := Message{
199 Type: SendEapolFlow,
200 }
201 o.Channel <- msg
202 },
203 "enter_dhcp_flow_sent": func(e *fsm.Event) {
204 msg := Message{
205 Type: SendDhcpFlow,
206 }
207 o.Channel <- msg
208 },
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700209 },
210 )
Matteo Scandolo27428702019-10-11 16:21:16 -0700211 return &o
Matteo Scandolo4747d292019-08-05 11:50:18 -0700212}
213
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700214func (o Onu) logStateChange(src string, dst string) {
215 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700216 "OnuId": o.ID,
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700217 "IntfId": o.PonPortID,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700218 "OnuSn": o.Sn(),
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700219 }).Debugf("Changing ONU InternalState from %s to %s", src, dst)
220}
221
Matteo Scandolo99f18462019-10-28 14:14:28 -0700222func (o *Onu) ProcessOnuMessages(stream openolt.Openolt_EnableIndicationServer, client openolt.OpenoltClient) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700223 onuLogger.WithFields(log.Fields{
Matteo Scandolo4747d292019-08-05 11:50:18 -0700224 "onuID": o.ID,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700225 "onuSN": o.Sn(),
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700226 }).Debug("Started ONU Indication Channel")
227
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700228 for message := range o.Channel {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700229 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700230 "onuID": o.ID,
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700231 "onuSN": o.Sn(),
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700232 "messageType": message.Type,
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700233 }).Tracef("Received message on ONU Channel")
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700234
235 switch message.Type {
236 case OnuDiscIndication:
237 msg, _ := message.Data.(OnuDiscIndicationMessage)
238 o.sendOnuDiscIndication(msg, stream)
239 case OnuIndication:
240 msg, _ := message.Data.(OnuIndicationMessage)
241 o.sendOnuIndication(msg, stream)
242 case OMCI:
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700243 msg, _ := message.Data.(OmciMessage)
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700244 o.handleOmciMessage(msg, stream)
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700245 case FlowUpdate:
246 msg, _ := message.Data.(OnuFlowUpdateMessage)
Matteo Scandolo813402b2019-10-23 19:24:52 -0700247 o.handleFlowUpdate(msg)
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700248 case StartEAPOL:
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700249 log.Infof("Receive StartEAPOL message on ONU Channel")
Matteo Scandolo27428702019-10-11 16:21:16 -0700250 eapol.SendEapStart(o.ID, o.PonPortID, o.Sn(), o.PortNo, o.HwAddress, o.InternalState, stream)
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700251 case StartDHCP:
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700252 log.Infof("Receive StartDHCP message on ONU Channel")
Matteo Scandolo27428702019-10-11 16:21:16 -0700253 // FIXME use id, ponId as SendEapStart
254 dhcp.SendDHCPDiscovery(o.PonPortID, o.ID, o.Sn(), o.PortNo, o.InternalState, o.HwAddress, o.CTag, stream)
Matteo Scandolo075b1892019-10-07 12:11:07 -0700255 case OnuPacketOut:
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700256
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700257 msg, _ := message.Data.(OnuPacketMessage)
258
259 log.WithFields(log.Fields{
260 "IntfId": msg.IntfId,
261 "OnuId": msg.OnuId,
262 "pktType": msg.Type,
263 }).Trace("Received OnuPacketOut Message")
264
265 if msg.Type == packetHandlers.EAPOL {
266 eapol.HandleNextPacket(msg.OnuId, msg.IntfId, o.Sn(), o.PortNo, o.InternalState, msg.Packet, stream, client)
267 } else if msg.Type == packetHandlers.DHCP {
Matteo Scandolo075b1892019-10-07 12:11:07 -0700268 // NOTE here we receive packets going from the DHCP Server to the ONU
269 // for now we expect them to be double-tagged, but ideally the should be single tagged
Matteo Scandolo27428702019-10-11 16:21:16 -0700270 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 -0700271 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700272 case OnuPacketIn:
273 // NOTE we only receive BBR packets here.
274 // Eapol.HandleNextPacket can handle both BBSim and BBr cases so the call is the same
275 // in the DHCP case VOLTHA only act as a proxy, the behaviour is completely different thus we have a dhcp.HandleNextBbrPacket
276 msg, _ := message.Data.(OnuPacketMessage)
277
278 log.WithFields(log.Fields{
279 "IntfId": msg.IntfId,
280 "OnuId": msg.OnuId,
281 "pktType": msg.Type,
282 }).Trace("Received OnuPacketIn Message")
283
284 if msg.Type == packetHandlers.EAPOL {
285 eapol.HandleNextPacket(msg.OnuId, msg.IntfId, o.Sn(), o.PortNo, o.InternalState, msg.Packet, stream, client)
286 } else if msg.Type == packetHandlers.DHCP {
287 dhcp.HandleNextBbrPacket(o.ID, o.PonPortID, o.Sn(), o.STag, o.HwAddress, o.DoneChannel, msg.Packet, client)
288 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700289 case DyingGaspIndication:
290 msg, _ := message.Data.(DyingGaspIndicationMessage)
291 o.sendDyingGaspInd(msg, stream)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700292 case OmciIndication:
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700293 msg, _ := message.Data.(OmciIndicationMessage)
294 o.handleOmci(msg, client)
295 case SendEapolFlow:
296 o.sendEapolFlow(client)
297 case SendDhcpFlow:
298 o.sendDhcpFlow(client)
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700299 default:
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700300 onuLogger.Warnf("Received unknown message data %v for type %v in OLT Channel", message.Data, message.Type)
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700301 }
302 }
Matteo Scandolo4747d292019-08-05 11:50:18 -0700303}
304
William Kurkian9dadc5b2019-10-22 13:51:57 -0400305func (o Onu) processOmciMessage(message omcisim.OmciChMessage) {
306 switch message.Type {
307 case omcisim.GemPortAdded:
308 log.WithFields(log.Fields{
309 "OnuId": message.Data.OnuId,
310 "IntfId": message.Data.IntfId,
311 }).Infof("GemPort Added")
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700312
William Kurkian9dadc5b2019-10-22 13:51:57 -0400313 // 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 }
319 } else if o.InternalState.Is("eapol_flow_received") {
320 if err := o.InternalState.Event("start_auth"); err != nil {
321 log.Errorf("Can't go to auth_started: %v", err)
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700322 }
323 }
324 }
325}
326
Matteo Scandolo4747d292019-08-05 11:50:18 -0700327func (o Onu) NewSN(oltid int, intfid uint32, onuid uint32) *openolt.SerialNumber {
328
329 sn := new(openolt.SerialNumber)
330
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700331 //sn = new(openolt.SerialNumber)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700332 sn.VendorId = []byte("BBSM")
333 sn.VendorSpecific = []byte{0, byte(oltid % 256), byte(intfid), byte(onuid)}
334
335 return sn
336}
337
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700338// NOTE handle_/process methods can change the ONU internal state as they are receiving messages
339// send method should not change the ONU state
340
341func (o Onu) sendDyingGaspInd(msg DyingGaspIndicationMessage, stream openolt.Openolt_EnableIndicationServer) error {
342 alarmData := &openolt.AlarmIndication_DyingGaspInd{
343 DyingGaspInd: &openolt.DyingGaspIndication{
344 IntfId: msg.PonPortID,
345 OnuId: msg.OnuID,
346 Status: "on",
347 },
348 }
349 data := &openolt.Indication_AlarmInd{AlarmInd: &openolt.AlarmIndication{Data: alarmData}}
350
351 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
352 onuLogger.Errorf("Failed to send DyingGaspInd : %v", err)
353 return err
354 }
355 onuLogger.WithFields(log.Fields{
356 "IntfId": msg.PonPortID,
357 "OnuSn": o.Sn(),
358 "OnuId": msg.OnuID,
359 }).Info("sendDyingGaspInd")
360 return nil
361}
362
Matteo Scandolo4747d292019-08-05 11:50:18 -0700363func (o Onu) sendOnuDiscIndication(msg OnuDiscIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
364 discoverData := &openolt.Indication_OnuDiscInd{OnuDiscInd: &openolt.OnuDiscIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700365 IntfId: msg.Onu.PonPortID,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700366 SerialNumber: msg.Onu.SerialNumber,
367 }}
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700368
Matteo Scandolo4747d292019-08-05 11:50:18 -0700369 if err := stream.Send(&openolt.Indication{Data: discoverData}); err != nil {
Matteo Scandolo11006992019-08-28 11:29:46 -0700370 log.Errorf("Failed to send Indication_OnuDiscInd: %v", err)
Matteo Scandolo99f18462019-10-28 14:14:28 -0700371 return
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) {
Matteo Scandolo813402b2019-10-23 19:24:52 -0700458 // NOTE this needed only as long as we don't support multiple UNIs
Matteo Scandolo27428702019-10-11 16:21:16 -0700459 // 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 Scandolo813402b2019-10-23 19:24:52 -0700465 onuLogger.WithFields(log.Fields{
466 "IntfId": o.PonPortID,
467 "OnuId": o.ID,
468 "SerialNumber": o.Sn(),
469 "OnuPortNo": o.PortNo,
470 "FlowPortNo": portNo,
471 }).Debug("Storing ONU portNo")
Matteo Scandolo27428702019-10-11 16:21:16 -0700472 o.PortNo = portNo
473 }
474}
475
Matteo Scandolo813402b2019-10-23 19:24:52 -0700476func (o *Onu) handleFlowUpdate(msg OnuFlowUpdateMessage) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700477 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700478 "DstPort": msg.Flow.Classifier.DstPort,
479 "EthType": fmt.Sprintf("%x", msg.Flow.Classifier.EthType),
480 "FlowId": msg.Flow.FlowId,
481 "FlowType": msg.Flow.FlowType,
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700482 "InnerVlan": msg.Flow.Classifier.IVid,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700483 "IntfId": msg.Flow.AccessIntfId,
484 "IpProto": msg.Flow.Classifier.IpProto,
485 "OnuId": msg.Flow.OnuId,
486 "OnuSn": o.Sn(),
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700487 "OuterVlan": msg.Flow.Classifier.OVid,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700488 "PortNo": msg.Flow.PortNo,
489 "SrcPort": msg.Flow.Classifier.SrcPort,
490 "UniID": msg.Flow.UniId,
491 }).Debug("ONU receives Flow")
492
Matteo Scandolo813402b2019-10-23 19:24:52 -0700493 if msg.Flow.UniId != 0 {
494 // as of now BBSim only support a single UNI, so ignore everything that is not targeted to it
495 onuLogger.WithFields(log.Fields{
496 "IntfId": o.PonPortID,
497 "OnuId": o.ID,
498 "SerialNumber": o.Sn(),
499 }).Debug("Ignoring flow as it's not for the first UNI")
500 return
501 }
502
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700503 if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeEAPOL) && msg.Flow.Classifier.OVid == 4091 {
Matteo Scandolo27428702019-10-11 16:21:16 -0700504 // NOTE storing the PortNO, it's needed when sending PacketIndications
Matteo Scandolo813402b2019-10-23 19:24:52 -0700505 o.storePortNumber(uint32(msg.Flow.PortNo))
Matteo Scandolo27428702019-10-11 16:21:16 -0700506
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700507 // NOTE if we receive the EAPOL flows but we don't have GemPorts
508 // go an intermediate state, otherwise start auth
509 if o.InternalState.Is("enabled") {
510 if err := o.InternalState.Event("receive_eapol_flow"); err != nil {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700511 log.Warnf("Can't go to eapol_flow_received: %v", err)
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700512 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700513 } else if o.InternalState.Is("gem_port_added") {
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700514 if err := o.InternalState.Event("start_auth"); err != nil {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700515 log.Warnf("Can't go to auth_started: %v", err)
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700516 }
517 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700518 } else if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeIPv4) &&
519 msg.Flow.Classifier.SrcPort == uint32(68) &&
520 msg.Flow.Classifier.DstPort == uint32(67) {
Matteo Scandolo99f18462019-10-28 14:14:28 -0700521
522 // keep track that we reveived the DHCP Flows so that we can transition the state to dhcp_started
523 o.DhcpFlowReceived = true
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700524 // NOTE we are receiving mulitple DHCP flows but we shouldn't call the transition multiple times
525 if err := o.InternalState.Event("start_dhcp"); err != nil {
Matteo Scandolo99f18462019-10-28 14:14:28 -0700526 log.Errorf("Can't go to dhcp_started: %v", err)
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700527 }
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700528 }
529}
530
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700531// HexDecode converts the hex encoding to binary
532func HexDecode(pkt []byte) []byte {
533 p := make([]byte, len(pkt)/2)
534 for i, j := 0, 0; i < len(pkt); i, j = i+2, j+1 {
535 // Go figure this ;)
536 u := (pkt[i] & 15) + (pkt[i]>>6)*9
537 l := (pkt[i+1] & 15) + (pkt[i+1]>>6)*9
538 p[j] = u<<4 + l
539 }
540 onuLogger.Tracef("Omci decoded: %x.", p)
541 return p
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700542}
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700543
544// BBR methods
545
546func sendOmciMsg(pktBytes []byte, intfId uint32, onuId uint32, sn *openolt.SerialNumber, msgType string, client openolt.OpenoltClient) {
547 omciMsg := openolt.OmciMsg{
548 IntfId: intfId,
549 OnuId: onuId,
550 Pkt: pktBytes,
551 }
552
553 if _, err := client.OmciMsgOut(context.Background(), &omciMsg); err != nil {
554 log.WithFields(log.Fields{
555 "IntfId": intfId,
556 "OnuId": onuId,
557 "SerialNumber": common.OnuSnToString(sn),
558 "Pkt": omciMsg.Pkt,
559 }).Fatalf("Failed to send MIB Reset")
560 }
561 log.WithFields(log.Fields{
562 "IntfId": intfId,
563 "OnuId": onuId,
564 "SerialNumber": common.OnuSnToString(sn),
565 "Pkt": omciMsg.Pkt,
566 }).Tracef("Sent OMCI message %s", msgType)
567}
568
569func (onu *Onu) getNextTid(highPriority ...bool) uint16 {
570 var next uint16
571 if len(highPriority) > 0 && highPriority[0] {
572 next = onu.hpTid
573 onu.hpTid += 1
574 if onu.hpTid < 0x8000 {
575 onu.hpTid = 0x8000
576 }
577 } else {
578 next = onu.tid
579 onu.tid += 1
580 if onu.tid >= 0x8000 {
581 onu.tid = 1
582 }
583 }
584 return next
585}
586
587// TODO move this method in responders/omcisim
588func (o *Onu) StartOmci(client openolt.OpenoltClient) {
589 mibReset, _ := omcilib.CreateMibResetRequest(o.getNextTid(false))
590 sendOmciMsg(mibReset, o.PonPortID, o.ID, o.SerialNumber, "mibReset", client)
591}
592
593func (o *Onu) handleOmci(msg OmciIndicationMessage, client openolt.OpenoltClient) {
594 msgType, packet := omcilib.DecodeOmci(msg.OmciInd.Pkt)
595
596 log.WithFields(log.Fields{
597 "IntfId": msg.OmciInd.IntfId,
598 "OnuId": msg.OmciInd.OnuId,
599 "OnuSn": common.OnuSnToString(o.SerialNumber),
600 "Pkt": msg.OmciInd.Pkt,
601 "msgType": msgType,
602 }).Trace("ONU Receveives OMCI Msg")
603 switch msgType {
604 default:
Matteo Scandolo813402b2019-10-23 19:24:52 -0700605 log.WithFields(log.Fields{
606 "IntfId": msg.OmciInd.IntfId,
607 "OnuId": msg.OmciInd.OnuId,
608 "OnuSn": common.OnuSnToString(o.SerialNumber),
609 "Pkt": msg.OmciInd.Pkt,
610 "msgType": msgType,
611 }).Fatalf("unexpected frame: %v", packet)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700612 case omci.MibResetResponseType:
613 mibUpload, _ := omcilib.CreateMibUploadRequest(o.getNextTid(false))
614 sendOmciMsg(mibUpload, o.PonPortID, o.ID, o.SerialNumber, "mibUpload", client)
615 case omci.MibUploadResponseType:
616 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
617 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
618 case omci.MibUploadNextResponseType:
619 o.seqNumber++
620
621 if o.seqNumber > 290 {
622 // NOTE we are done with the MIB Upload (290 is the number of messages the omci-sim library will respond to)
623 galEnet, _ := omcilib.CreateGalEnetRequest(o.getNextTid(false))
624 sendOmciMsg(galEnet, o.PonPortID, o.ID, o.SerialNumber, "CreateGalEnetRequest", client)
625 } else {
626 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
627 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
628 }
629 case omci.CreateResponseType:
630 // NOTE Creating a GemPort,
631 // BBsim actually doesn't care about the values, so we can do we want with the parameters
632 // In the same way we can create a GemPort even without setting up UNIs/TConts/...
633 // but we need the GemPort to trigger the state change
634
635 if !o.HasGemPort {
636 // NOTE this sends a CreateRequestType and BBSim replies with a CreateResponseType
637 // thus we send this request only once
638 gemReq, _ := omcilib.CreateGemPortRequest(o.getNextTid(false))
639 sendOmciMsg(gemReq, o.PonPortID, o.ID, o.SerialNumber, "CreateGemPortRequest", client)
640 o.HasGemPort = true
641 } else {
642 if err := o.InternalState.Event("send_eapol_flow"); err != nil {
643 onuLogger.WithFields(log.Fields{
644 "OnuId": o.ID,
645 "IntfId": o.PonPortID,
646 "OnuSn": o.Sn(),
647 }).Errorf("Error while transitioning ONU State %v", err)
648 }
649 }
650
651 }
652}
653
654func (o *Onu) sendEapolFlow(client openolt.OpenoltClient) {
655
656 classifierProto := openolt.Classifier{
657 EthType: uint32(layers.EthernetTypeEAPOL),
658 OVid: 4091,
659 }
660
661 actionProto := openolt.Action{}
662
663 downstreamFlow := openolt.Flow{
664 AccessIntfId: int32(o.PonPortID),
665 OnuId: int32(o.ID),
Matteo Scandolo813402b2019-10-23 19:24:52 -0700666 UniId: int32(0), // NOTE do not hardcode this, we need to support multiple UNIs
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700667 FlowId: uint32(o.ID),
668 FlowType: "downstream",
669 AllocId: int32(0),
670 NetworkIntfId: int32(0),
671 GemportId: int32(1), // FIXME use the same value as CreateGemPortRequest PortID, do not hardcode
672 Classifier: &classifierProto,
673 Action: &actionProto,
674 Priority: int32(100),
675 Cookie: uint64(o.ID),
676 PortNo: uint32(o.ID), // NOTE we are using this to map an incoming packetIndication to an ONU
677 }
678
679 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
680 log.WithFields(log.Fields{
681 "IntfId": o.PonPortID,
682 "OnuId": o.ID,
683 "FlowId": downstreamFlow.FlowId,
684 "PortNo": downstreamFlow.PortNo,
685 "SerialNumber": common.OnuSnToString(o.SerialNumber),
686 }).Fatalf("Failed to EAPOL Flow")
687 }
688 log.WithFields(log.Fields{
689 "IntfId": o.PonPortID,
690 "OnuId": o.ID,
691 "FlowId": downstreamFlow.FlowId,
692 "PortNo": downstreamFlow.PortNo,
693 "SerialNumber": common.OnuSnToString(o.SerialNumber),
694 }).Info("Sent EAPOL Flow")
695}
696
697func (o *Onu) sendDhcpFlow(client openolt.OpenoltClient) {
698 classifierProto := openolt.Classifier{
699 EthType: uint32(layers.EthernetTypeIPv4),
700 SrcPort: uint32(68),
701 DstPort: uint32(67),
702 }
703
704 actionProto := openolt.Action{}
705
706 downstreamFlow := openolt.Flow{
707 AccessIntfId: int32(o.PonPortID),
708 OnuId: int32(o.ID),
Matteo Scandolo813402b2019-10-23 19:24:52 -0700709 UniId: int32(0), // FIXME do not hardcode this
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700710 FlowId: uint32(o.ID),
711 FlowType: "downstream",
712 AllocId: int32(0),
713 NetworkIntfId: int32(0),
714 GemportId: int32(1), // FIXME use the same value as CreateGemPortRequest PortID, do not hardcode
715 Classifier: &classifierProto,
716 Action: &actionProto,
717 Priority: int32(100),
718 Cookie: uint64(o.ID),
719 PortNo: uint32(o.ID), // NOTE we are using this to map an incoming packetIndication to an ONU
720 }
721
722 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
723 log.WithFields(log.Fields{
724 "IntfId": o.PonPortID,
725 "OnuId": o.ID,
726 "FlowId": downstreamFlow.FlowId,
727 "PortNo": downstreamFlow.PortNo,
728 "SerialNumber": common.OnuSnToString(o.SerialNumber),
729 }).Fatalf("Failed to send DHCP Flow")
730 }
731 log.WithFields(log.Fields{
732 "IntfId": o.PonPortID,
733 "OnuId": o.ID,
734 "FlowId": downstreamFlow.FlowId,
735 "PortNo": downstreamFlow.PortNo,
736 "SerialNumber": common.OnuSnToString(o.SerialNumber),
737 }).Info("Sent DHCP Flow")
738}