blob: 564c21eec17eb10cfccd420bebb9621f4a24842d [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
Matteo Scandoloc1147092019-10-29 09:38:33 -070047 Auth bool // automatically start EAPOL if set to true
48 Dhcp bool // automatically start DHCP if set to true
Matteo Scandolo27428702019-10-11 16:21:16 -070049 // PortNo comes with flows and it's used when sending packetIndications,
50 // There is one PortNo per UNI Port, for now we're only storing the first one
51 // FIXME add support for multiple UNIs
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070052 HwAddress net.HardwareAddr
53 InternalState *fsm.FSM
54
Matteo Scandolo99f18462019-10-28 14:14:28 -070055 // ONU State
56 PortNo uint32
57 DhcpFlowReceived bool
58
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070059 OperState *fsm.FSM
60 SerialNumber *openolt.SerialNumber
61
62 Channel chan Message // this Channel is to track state changes OMCI messages, EAPOL and DHCP packets
Matteo Scandolo40e067f2019-10-16 16:59:41 -070063
64 // OMCI params
65 tid uint16
66 hpTid uint16
67 seqNumber uint16
68 HasGemPort bool
69
70 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 -070071}
72
Matteo Scandolo99f18462019-10-28 14:14:28 -070073func (o *Onu) Sn() string {
Matteo Scandolo40e067f2019-10-16 16:59:41 -070074 return common.OnuSnToString(o.SerialNumber)
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070075}
76
Matteo Scandoloc1147092019-10-29 09:38:33 -070077func CreateONU(olt OltDevice, pon PonPort, id uint32, sTag int, cTag int, auth bool, dhcp bool) *Onu {
Matteo Scandolo4747d292019-08-05 11:50:18 -070078
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070079 o := Onu{
Matteo Scandolo99f18462019-10-28 14:14:28 -070080 ID: id,
81 PonPortID: pon.ID,
82 PonPort: pon,
83 STag: sTag,
84 CTag: cTag,
Matteo Scandoloc1147092019-10-29 09:38:33 -070085 Auth: auth,
86 Dhcp: dhcp,
Matteo Scandolo99f18462019-10-28 14:14:28 -070087 HwAddress: net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, byte(pon.ID), byte(id)},
88 PortNo: 0,
89 Channel: make(chan Message, 2048),
90 tid: 0x1,
91 hpTid: 0x8000,
92 seqNumber: 0,
93 DoneChannel: make(chan bool, 1),
94 DhcpFlowReceived: false,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070095 }
96 o.SerialNumber = o.NewSN(olt.ID, pon.ID, o.ID)
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070097
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070098 // NOTE this state machine is used to track the operational
99 // state as requested by VOLTHA
100 o.OperState = getOperStateFSM(func(e *fsm.Event) {
101 onuLogger.WithFields(log.Fields{
102 "ID": o.ID,
103 }).Debugf("Changing ONU OperState from %s to %s", e.Src, e.Dst)
104 })
105
106 // NOTE this state machine is used to activate the OMCI, EAPOL and DHCP clients
107 o.InternalState = fsm.NewFSM(
108 "created",
109 fsm.Events{
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700110 // DEVICE Lifecycle
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700111 {Name: "discover", Src: []string{"created"}, Dst: "discovered"},
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700112 {Name: "enable", Src: []string{"discovered", "disabled"}, Dst: "enabled"},
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700113 {Name: "receive_eapol_flow", Src: []string{"enabled", "gem_port_added"}, Dst: "eapol_flow_received"},
114 {Name: "add_gem_port", Src: []string{"enabled", "eapol_flow_received"}, Dst: "gem_port_added"},
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700115 // NOTE should disabled state be diffente for oper_disabled (emulating an error) and admin_disabled (received a disabled call via VOLTHA)?
116 {Name: "disable", Src: []string{"eap_response_success_received", "auth_failed", "dhcp_ack_received", "dhcp_failed"}, Dst: "disabled"},
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700117 // EAPOL
Matteo Scandoloe383d5d2019-10-25 14:47:27 -0700118 {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 -0700119 {Name: "eap_start_sent", Src: []string{"auth_started"}, Dst: "eap_start_sent"},
120 {Name: "eap_response_identity_sent", Src: []string{"eap_start_sent"}, Dst: "eap_response_identity_sent"},
121 {Name: "eap_response_challenge_sent", Src: []string{"eap_response_identity_sent"}, Dst: "eap_response_challenge_sent"},
122 {Name: "eap_response_success_received", Src: []string{"eap_response_challenge_sent"}, Dst: "eap_response_success_received"},
123 {Name: "auth_failed", Src: []string{"auth_started", "eap_start_sent", "eap_response_identity_sent", "eap_response_challenge_sent"}, Dst: "auth_failed"},
124 // DHCP
Matteo Scandolofe9ac252019-10-25 11:40:17 -0700125 {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 -0700126 {Name: "dhcp_discovery_sent", Src: []string{"dhcp_started"}, Dst: "dhcp_discovery_sent"},
127 {Name: "dhcp_request_sent", Src: []string{"dhcp_discovery_sent"}, Dst: "dhcp_request_sent"},
128 {Name: "dhcp_ack_received", Src: []string{"dhcp_request_sent"}, Dst: "dhcp_ack_received"},
129 {Name: "dhcp_failed", Src: []string{"dhcp_started", "dhcp_discovery_sent", "dhcp_request_sent"}, Dst: "dhcp_failed"},
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700130 // BBR States
131 // TODO add start OMCI state
132 {Name: "send_eapol_flow", Src: []string{"created"}, Dst: "eapol_flow_sent"},
133 {Name: "send_dhcp_flow", Src: []string{"eapol_flow_sent"}, Dst: "dhcp_flow_sent"},
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700134 },
135 fsm.Callbacks{
136 "enter_state": func(e *fsm.Event) {
137 o.logStateChange(e.Src, e.Dst)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700138 },
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700139 "enter_enabled": func(event *fsm.Event) {
140 msg := Message{
141 Type: OnuIndication,
142 Data: OnuIndicationMessage{
143 OnuSN: o.SerialNumber,
144 PonPortID: o.PonPortID,
145 OperState: UP,
146 },
147 }
148 o.Channel <- msg
149 },
150 "enter_disabled": func(event *fsm.Event) {
151 msg := Message{
152 Type: OnuIndication,
153 Data: OnuIndicationMessage{
154 OnuSN: o.SerialNumber,
155 PonPortID: o.PonPortID,
156 OperState: DOWN,
157 },
158 }
159 o.Channel <- msg
160 },
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700161 "enter_auth_started": func(e *fsm.Event) {
162 o.logStateChange(e.Src, e.Dst)
163 msg := Message{
164 Type: StartEAPOL,
165 Data: PacketMessage{
166 PonPortID: o.PonPortID,
167 OnuID: o.ID,
168 },
169 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700170 o.Channel <- msg
Matteo Scandolo4747d292019-08-05 11:50:18 -0700171 },
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700172 "enter_auth_failed": func(e *fsm.Event) {
173 onuLogger.WithFields(log.Fields{
174 "OnuId": o.ID,
175 "IntfId": o.PonPortID,
176 "OnuSn": o.Sn(),
177 }).Errorf("ONU failed to authenticate!")
178 },
Matteo Scandolo99f18462019-10-28 14:14:28 -0700179 "before_start_dhcp": func(e *fsm.Event) {
180 if o.DhcpFlowReceived == false {
181 e.Cancel(errors.New("cannot-go-to-dhcp-started-as-dhcp-flow-is-missing"))
182 }
183 },
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700184 "enter_dhcp_started": func(e *fsm.Event) {
185 msg := Message{
186 Type: StartDHCP,
187 Data: PacketMessage{
188 PonPortID: o.PonPortID,
189 OnuID: o.ID,
190 },
191 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700192 o.Channel <- msg
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700193 },
194 "enter_dhcp_failed": func(e *fsm.Event) {
195 onuLogger.WithFields(log.Fields{
196 "OnuId": o.ID,
197 "IntfId": o.PonPortID,
198 "OnuSn": o.Sn(),
199 }).Errorf("ONU failed to DHCP!")
200 },
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700201 "enter_eapol_flow_sent": func(e *fsm.Event) {
202 msg := Message{
203 Type: SendEapolFlow,
204 }
205 o.Channel <- msg
206 },
207 "enter_dhcp_flow_sent": func(e *fsm.Event) {
208 msg := Message{
209 Type: SendDhcpFlow,
210 }
211 o.Channel <- msg
212 },
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700213 },
214 )
Matteo Scandolo27428702019-10-11 16:21:16 -0700215 return &o
Matteo Scandolo4747d292019-08-05 11:50:18 -0700216}
217
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700218func (o Onu) logStateChange(src string, dst string) {
219 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700220 "OnuId": o.ID,
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700221 "IntfId": o.PonPortID,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700222 "OnuSn": o.Sn(),
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700223 }).Debugf("Changing ONU InternalState from %s to %s", src, dst)
224}
225
Matteo Scandolo99f18462019-10-28 14:14:28 -0700226func (o *Onu) ProcessOnuMessages(stream openolt.Openolt_EnableIndicationServer, client openolt.OpenoltClient) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700227 onuLogger.WithFields(log.Fields{
Matteo Scandolo4747d292019-08-05 11:50:18 -0700228 "onuID": o.ID,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700229 "onuSN": o.Sn(),
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700230 }).Debug("Started ONU Indication Channel")
231
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700232 for message := range o.Channel {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700233 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700234 "onuID": o.ID,
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700235 "onuSN": o.Sn(),
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700236 "messageType": message.Type,
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700237 }).Tracef("Received message on ONU Channel")
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700238
239 switch message.Type {
240 case OnuDiscIndication:
241 msg, _ := message.Data.(OnuDiscIndicationMessage)
242 o.sendOnuDiscIndication(msg, stream)
243 case OnuIndication:
244 msg, _ := message.Data.(OnuIndicationMessage)
245 o.sendOnuIndication(msg, stream)
246 case OMCI:
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700247 msg, _ := message.Data.(OmciMessage)
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700248 o.handleOmciMessage(msg, stream)
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700249 case FlowUpdate:
250 msg, _ := message.Data.(OnuFlowUpdateMessage)
Matteo Scandolo813402b2019-10-23 19:24:52 -0700251 o.handleFlowUpdate(msg)
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700252 case StartEAPOL:
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700253 log.Infof("Receive StartEAPOL message on ONU Channel")
Matteo Scandolo27428702019-10-11 16:21:16 -0700254 eapol.SendEapStart(o.ID, o.PonPortID, o.Sn(), o.PortNo, o.HwAddress, o.InternalState, stream)
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700255 case StartDHCP:
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700256 log.Infof("Receive StartDHCP message on ONU Channel")
Matteo Scandolo27428702019-10-11 16:21:16 -0700257 // FIXME use id, ponId as SendEapStart
258 dhcp.SendDHCPDiscovery(o.PonPortID, o.ID, o.Sn(), o.PortNo, o.InternalState, o.HwAddress, o.CTag, stream)
Matteo Scandolo075b1892019-10-07 12:11:07 -0700259 case OnuPacketOut:
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700260
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700261 msg, _ := message.Data.(OnuPacketMessage)
262
263 log.WithFields(log.Fields{
264 "IntfId": msg.IntfId,
265 "OnuId": msg.OnuId,
266 "pktType": msg.Type,
267 }).Trace("Received OnuPacketOut Message")
268
269 if msg.Type == packetHandlers.EAPOL {
270 eapol.HandleNextPacket(msg.OnuId, msg.IntfId, o.Sn(), o.PortNo, o.InternalState, msg.Packet, stream, client)
271 } else if msg.Type == packetHandlers.DHCP {
Matteo Scandolo075b1892019-10-07 12:11:07 -0700272 // NOTE here we receive packets going from the DHCP Server to the ONU
273 // for now we expect them to be double-tagged, but ideally the should be single tagged
Matteo Scandolo27428702019-10-11 16:21:16 -0700274 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 -0700275 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700276 case OnuPacketIn:
277 // NOTE we only receive BBR packets here.
278 // Eapol.HandleNextPacket can handle both BBSim and BBr cases so the call is the same
279 // in the DHCP case VOLTHA only act as a proxy, the behaviour is completely different thus we have a dhcp.HandleNextBbrPacket
280 msg, _ := message.Data.(OnuPacketMessage)
281
282 log.WithFields(log.Fields{
283 "IntfId": msg.IntfId,
284 "OnuId": msg.OnuId,
285 "pktType": msg.Type,
286 }).Trace("Received OnuPacketIn Message")
287
288 if msg.Type == packetHandlers.EAPOL {
289 eapol.HandleNextPacket(msg.OnuId, msg.IntfId, o.Sn(), o.PortNo, o.InternalState, msg.Packet, stream, client)
290 } else if msg.Type == packetHandlers.DHCP {
291 dhcp.HandleNextBbrPacket(o.ID, o.PonPortID, o.Sn(), o.STag, o.HwAddress, o.DoneChannel, msg.Packet, client)
292 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700293 case DyingGaspIndication:
294 msg, _ := message.Data.(DyingGaspIndicationMessage)
295 o.sendDyingGaspInd(msg, stream)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700296 case OmciIndication:
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700297 msg, _ := message.Data.(OmciIndicationMessage)
298 o.handleOmci(msg, client)
299 case SendEapolFlow:
300 o.sendEapolFlow(client)
301 case SendDhcpFlow:
302 o.sendDhcpFlow(client)
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700303 default:
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700304 onuLogger.Warnf("Received unknown message data %v for type %v in OLT Channel", message.Data, message.Type)
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700305 }
306 }
Matteo Scandolo4747d292019-08-05 11:50:18 -0700307}
308
William Kurkian9dadc5b2019-10-22 13:51:57 -0400309func (o Onu) processOmciMessage(message omcisim.OmciChMessage) {
310 switch message.Type {
311 case omcisim.GemPortAdded:
312 log.WithFields(log.Fields{
313 "OnuId": message.Data.OnuId,
314 "IntfId": message.Data.IntfId,
315 }).Infof("GemPort Added")
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700316
William Kurkian9dadc5b2019-10-22 13:51:57 -0400317 // NOTE if we receive the GemPort but we don't have EAPOL flows
318 // go an intermediate state, otherwise start auth
319 if o.InternalState.Is("enabled") {
320 if err := o.InternalState.Event("add_gem_port"); err != nil {
321 log.Errorf("Can't go to gem_port_added: %v", err)
322 }
323 } else if o.InternalState.Is("eapol_flow_received") {
324 if err := o.InternalState.Event("start_auth"); err != nil {
325 log.Errorf("Can't go to auth_started: %v", err)
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700326 }
327 }
328 }
329}
330
Matteo Scandolo4747d292019-08-05 11:50:18 -0700331func (o Onu) NewSN(oltid int, intfid uint32, onuid uint32) *openolt.SerialNumber {
332
333 sn := new(openolt.SerialNumber)
334
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700335 //sn = new(openolt.SerialNumber)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700336 sn.VendorId = []byte("BBSM")
337 sn.VendorSpecific = []byte{0, byte(oltid % 256), byte(intfid), byte(onuid)}
338
339 return sn
340}
341
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700342// NOTE handle_/process methods can change the ONU internal state as they are receiving messages
343// send method should not change the ONU state
344
345func (o Onu) sendDyingGaspInd(msg DyingGaspIndicationMessage, stream openolt.Openolt_EnableIndicationServer) error {
346 alarmData := &openolt.AlarmIndication_DyingGaspInd{
347 DyingGaspInd: &openolt.DyingGaspIndication{
348 IntfId: msg.PonPortID,
349 OnuId: msg.OnuID,
350 Status: "on",
351 },
352 }
353 data := &openolt.Indication_AlarmInd{AlarmInd: &openolt.AlarmIndication{Data: alarmData}}
354
355 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
356 onuLogger.Errorf("Failed to send DyingGaspInd : %v", err)
357 return err
358 }
359 onuLogger.WithFields(log.Fields{
360 "IntfId": msg.PonPortID,
361 "OnuSn": o.Sn(),
362 "OnuId": msg.OnuID,
363 }).Info("sendDyingGaspInd")
364 return nil
365}
366
Matteo Scandolo4747d292019-08-05 11:50:18 -0700367func (o Onu) sendOnuDiscIndication(msg OnuDiscIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
368 discoverData := &openolt.Indication_OnuDiscInd{OnuDiscInd: &openolt.OnuDiscIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700369 IntfId: msg.Onu.PonPortID,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700370 SerialNumber: msg.Onu.SerialNumber,
371 }}
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700372
Matteo Scandolo4747d292019-08-05 11:50:18 -0700373 if err := stream.Send(&openolt.Indication{Data: discoverData}); err != nil {
Matteo Scandolo11006992019-08-28 11:29:46 -0700374 log.Errorf("Failed to send Indication_OnuDiscInd: %v", err)
Matteo Scandolo99f18462019-10-28 14:14:28 -0700375 return
Matteo Scandolo4747d292019-08-05 11:50:18 -0700376 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700377
378 if err := o.InternalState.Event("discover"); err != nil {
379 oltLogger.WithFields(log.Fields{
380 "IntfId": o.PonPortID,
381 "OnuSn": o.Sn(),
382 "OnuId": o.ID,
383 }).Infof("Failed to transition ONU to discovered state: %s", err.Error())
384 }
385
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700386 onuLogger.WithFields(log.Fields{
Matteo Scandolo4747d292019-08-05 11:50:18 -0700387 "IntfId": msg.Onu.PonPortID,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700388 "OnuSn": msg.Onu.Sn(),
389 "OnuId": o.ID,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700390 }).Debug("Sent Indication_OnuDiscInd")
391}
392
393func (o Onu) sendOnuIndication(msg OnuIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
394 // NOTE voltha returns an ID, but if we use that ID then it complains:
395 // expected_onu_id: 1, received_onu_id: 1024, event: ONU-id-mismatch, can happen if both voltha and the olt rebooted
396 // so we're using the internal ID that is 1
397 // o.ID = msg.OnuID
Matteo Scandolo4747d292019-08-05 11:50:18 -0700398
399 indData := &openolt.Indication_OnuInd{OnuInd: &openolt.OnuIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700400 IntfId: o.PonPortID,
401 OnuId: o.ID,
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700402 OperState: msg.OperState.String(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700403 AdminState: o.OperState.Current(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700404 SerialNumber: o.SerialNumber,
405 }}
406 if err := stream.Send(&openolt.Indication{Data: indData}); err != nil {
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700407 // TODO do we need to transition to a broken state?
Matteo Scandolo11006992019-08-28 11:29:46 -0700408 log.Errorf("Failed to send Indication_OnuInd: %v", err)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700409 }
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700410 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700411 "IntfId": o.PonPortID,
412 "OnuId": o.ID,
413 "OperState": msg.OperState.String(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700414 "AdminState": msg.OperState.String(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700415 "OnuSn": o.Sn(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700416 }).Debug("Sent Indication_OnuInd")
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700417
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700418}
419
420func (o Onu) handleOmciMessage(msg OmciMessage, stream openolt.Openolt_EnableIndicationServer) {
421
422 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700423 "IntfId": o.PonPortID,
Matteo Scandolo27428702019-10-11 16:21:16 -0700424 "SerialNumber": o.Sn(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700425 "omciPacket": msg.omciMsg.Pkt,
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700426 }).Tracef("Received OMCI message")
427
428 var omciInd openolt.OmciIndication
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700429 respPkt, err := omcisim.OmciSim(o.PonPortID, o.ID, HexDecode(msg.omciMsg.Pkt))
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700430 if 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,
436 }).Errorf("Error handling OMCI message %v", msg)
437 return
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700438 }
439
440 omciInd.IntfId = o.PonPortID
441 omciInd.OnuId = o.ID
442 omciInd.Pkt = respPkt
443
444 omci := &openolt.Indication_OmciInd{OmciInd: &omciInd}
445 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
Matteo Scandolo27428702019-10-11 16:21:16 -0700446 onuLogger.WithFields(log.Fields{
447 "IntfId": o.PonPortID,
448 "SerialNumber": o.Sn(),
449 "omciPacket": omciInd.Pkt,
450 "msg": msg,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700451 }).Errorf("send omcisim indication failed: %v", err)
Matteo Scandolo27428702019-10-11 16:21:16 -0700452 return
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700453 }
454 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700455 "IntfId": o.PonPortID,
Matteo Scandolo27428702019-10-11 16:21:16 -0700456 "SerialNumber": o.Sn(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700457 "omciPacket": omciInd.Pkt,
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700458 }).Tracef("Sent OMCI message")
459}
460
Matteo Scandolo27428702019-10-11 16:21:16 -0700461func (o *Onu) storePortNumber(portNo uint32) {
Matteo Scandolo813402b2019-10-23 19:24:52 -0700462 // NOTE this needed only as long as we don't support multiple UNIs
Matteo Scandolo27428702019-10-11 16:21:16 -0700463 // we need to add support for multiple UNIs
464 // the action plan is:
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700465 // - refactor the omcisim-sim library to use https://github.com/cboling/omci instead of canned messages
Matteo Scandolo27428702019-10-11 16:21:16 -0700466 // - change the library so that it reports a single UNI and remove this workaroung
467 // - add support for multiple UNIs in BBSim
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700468 if o.PortNo == 0 || portNo < o.PortNo {
Matteo Scandolo813402b2019-10-23 19:24:52 -0700469 onuLogger.WithFields(log.Fields{
470 "IntfId": o.PonPortID,
471 "OnuId": o.ID,
472 "SerialNumber": o.Sn(),
473 "OnuPortNo": o.PortNo,
474 "FlowPortNo": portNo,
475 }).Debug("Storing ONU portNo")
Matteo Scandolo27428702019-10-11 16:21:16 -0700476 o.PortNo = portNo
477 }
478}
479
Matteo Scandolo813402b2019-10-23 19:24:52 -0700480func (o *Onu) handleFlowUpdate(msg OnuFlowUpdateMessage) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700481 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700482 "DstPort": msg.Flow.Classifier.DstPort,
483 "EthType": fmt.Sprintf("%x", msg.Flow.Classifier.EthType),
484 "FlowId": msg.Flow.FlowId,
485 "FlowType": msg.Flow.FlowType,
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700486 "InnerVlan": msg.Flow.Classifier.IVid,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700487 "IntfId": msg.Flow.AccessIntfId,
488 "IpProto": msg.Flow.Classifier.IpProto,
489 "OnuId": msg.Flow.OnuId,
490 "OnuSn": o.Sn(),
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700491 "OuterVlan": msg.Flow.Classifier.OVid,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700492 "PortNo": msg.Flow.PortNo,
493 "SrcPort": msg.Flow.Classifier.SrcPort,
494 "UniID": msg.Flow.UniId,
495 }).Debug("ONU receives Flow")
496
Matteo Scandolo813402b2019-10-23 19:24:52 -0700497 if msg.Flow.UniId != 0 {
498 // as of now BBSim only support a single UNI, so ignore everything that is not targeted to it
499 onuLogger.WithFields(log.Fields{
500 "IntfId": o.PonPortID,
501 "OnuId": o.ID,
502 "SerialNumber": o.Sn(),
503 }).Debug("Ignoring flow as it's not for the first UNI")
504 return
505 }
506
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700507 if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeEAPOL) && msg.Flow.Classifier.OVid == 4091 {
Matteo Scandolo27428702019-10-11 16:21:16 -0700508 // NOTE storing the PortNO, it's needed when sending PacketIndications
Matteo Scandolo813402b2019-10-23 19:24:52 -0700509 o.storePortNumber(uint32(msg.Flow.PortNo))
Matteo Scandolo27428702019-10-11 16:21:16 -0700510
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700511 // NOTE if we receive the EAPOL flows but we don't have GemPorts
512 // go an intermediate state, otherwise start auth
513 if o.InternalState.Is("enabled") {
514 if err := o.InternalState.Event("receive_eapol_flow"); err != nil {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700515 log.Warnf("Can't go to eapol_flow_received: %v", err)
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700516 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700517 } else if o.InternalState.Is("gem_port_added") {
Matteo Scandoloc1147092019-10-29 09:38:33 -0700518
519 if o.Auth == true {
520 if err := o.InternalState.Event("start_auth"); err != nil {
521 log.Warnf("Can't go to auth_started: %v", err)
522 }
523 } else {
524 onuLogger.WithFields(log.Fields{
525 "IntfId": o.PonPortID,
526 "OnuId": o.ID,
527 "SerialNumber": o.Sn(),
528 }).Warn("Not starting authentication as Auth bit is not set in CLI parameters")
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700529 }
Matteo Scandoloc1147092019-10-29 09:38:33 -0700530
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700531 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700532 } else if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeIPv4) &&
533 msg.Flow.Classifier.SrcPort == uint32(68) &&
534 msg.Flow.Classifier.DstPort == uint32(67) {
Matteo Scandolo99f18462019-10-28 14:14:28 -0700535
536 // keep track that we reveived the DHCP Flows so that we can transition the state to dhcp_started
537 o.DhcpFlowReceived = true
Matteo Scandoloc1147092019-10-29 09:38:33 -0700538
539 if o.Dhcp == true {
540 // NOTE we are receiving mulitple DHCP flows but we shouldn't call the transition multiple times
541 if err := o.InternalState.Event("start_dhcp"); err != nil {
542 log.Errorf("Can't go to dhcp_started: %v", err)
543 }
544 } else {
545 onuLogger.WithFields(log.Fields{
546 "IntfId": o.PonPortID,
547 "OnuId": o.ID,
548 "SerialNumber": o.Sn(),
549 }).Warn("Not starting DHCP as Dhcp bit is not set in CLI parameters")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700550 }
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700551 }
552}
553
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700554// HexDecode converts the hex encoding to binary
555func HexDecode(pkt []byte) []byte {
556 p := make([]byte, len(pkt)/2)
557 for i, j := 0, 0; i < len(pkt); i, j = i+2, j+1 {
558 // Go figure this ;)
559 u := (pkt[i] & 15) + (pkt[i]>>6)*9
560 l := (pkt[i+1] & 15) + (pkt[i+1]>>6)*9
561 p[j] = u<<4 + l
562 }
563 onuLogger.Tracef("Omci decoded: %x.", p)
564 return p
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700565}
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700566
567// BBR methods
568
569func sendOmciMsg(pktBytes []byte, intfId uint32, onuId uint32, sn *openolt.SerialNumber, msgType string, client openolt.OpenoltClient) {
570 omciMsg := openolt.OmciMsg{
571 IntfId: intfId,
572 OnuId: onuId,
573 Pkt: pktBytes,
574 }
575
576 if _, err := client.OmciMsgOut(context.Background(), &omciMsg); err != nil {
577 log.WithFields(log.Fields{
578 "IntfId": intfId,
579 "OnuId": onuId,
580 "SerialNumber": common.OnuSnToString(sn),
581 "Pkt": omciMsg.Pkt,
582 }).Fatalf("Failed to send MIB Reset")
583 }
584 log.WithFields(log.Fields{
585 "IntfId": intfId,
586 "OnuId": onuId,
587 "SerialNumber": common.OnuSnToString(sn),
588 "Pkt": omciMsg.Pkt,
589 }).Tracef("Sent OMCI message %s", msgType)
590}
591
592func (onu *Onu) getNextTid(highPriority ...bool) uint16 {
593 var next uint16
594 if len(highPriority) > 0 && highPriority[0] {
595 next = onu.hpTid
596 onu.hpTid += 1
597 if onu.hpTid < 0x8000 {
598 onu.hpTid = 0x8000
599 }
600 } else {
601 next = onu.tid
602 onu.tid += 1
603 if onu.tid >= 0x8000 {
604 onu.tid = 1
605 }
606 }
607 return next
608}
609
610// TODO move this method in responders/omcisim
611func (o *Onu) StartOmci(client openolt.OpenoltClient) {
612 mibReset, _ := omcilib.CreateMibResetRequest(o.getNextTid(false))
613 sendOmciMsg(mibReset, o.PonPortID, o.ID, o.SerialNumber, "mibReset", client)
614}
615
616func (o *Onu) handleOmci(msg OmciIndicationMessage, client openolt.OpenoltClient) {
617 msgType, packet := omcilib.DecodeOmci(msg.OmciInd.Pkt)
618
619 log.WithFields(log.Fields{
620 "IntfId": msg.OmciInd.IntfId,
621 "OnuId": msg.OmciInd.OnuId,
622 "OnuSn": common.OnuSnToString(o.SerialNumber),
623 "Pkt": msg.OmciInd.Pkt,
624 "msgType": msgType,
625 }).Trace("ONU Receveives OMCI Msg")
626 switch msgType {
627 default:
Matteo Scandolo813402b2019-10-23 19:24:52 -0700628 log.WithFields(log.Fields{
629 "IntfId": msg.OmciInd.IntfId,
630 "OnuId": msg.OmciInd.OnuId,
631 "OnuSn": common.OnuSnToString(o.SerialNumber),
632 "Pkt": msg.OmciInd.Pkt,
633 "msgType": msgType,
634 }).Fatalf("unexpected frame: %v", packet)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700635 case omci.MibResetResponseType:
636 mibUpload, _ := omcilib.CreateMibUploadRequest(o.getNextTid(false))
637 sendOmciMsg(mibUpload, o.PonPortID, o.ID, o.SerialNumber, "mibUpload", client)
638 case omci.MibUploadResponseType:
639 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
640 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
641 case omci.MibUploadNextResponseType:
642 o.seqNumber++
643
644 if o.seqNumber > 290 {
645 // NOTE we are done with the MIB Upload (290 is the number of messages the omci-sim library will respond to)
646 galEnet, _ := omcilib.CreateGalEnetRequest(o.getNextTid(false))
647 sendOmciMsg(galEnet, o.PonPortID, o.ID, o.SerialNumber, "CreateGalEnetRequest", client)
648 } else {
649 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
650 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
651 }
652 case omci.CreateResponseType:
653 // NOTE Creating a GemPort,
654 // BBsim actually doesn't care about the values, so we can do we want with the parameters
655 // In the same way we can create a GemPort even without setting up UNIs/TConts/...
656 // but we need the GemPort to trigger the state change
657
658 if !o.HasGemPort {
659 // NOTE this sends a CreateRequestType and BBSim replies with a CreateResponseType
660 // thus we send this request only once
661 gemReq, _ := omcilib.CreateGemPortRequest(o.getNextTid(false))
662 sendOmciMsg(gemReq, o.PonPortID, o.ID, o.SerialNumber, "CreateGemPortRequest", client)
663 o.HasGemPort = true
664 } else {
665 if err := o.InternalState.Event("send_eapol_flow"); err != nil {
666 onuLogger.WithFields(log.Fields{
667 "OnuId": o.ID,
668 "IntfId": o.PonPortID,
669 "OnuSn": o.Sn(),
670 }).Errorf("Error while transitioning ONU State %v", err)
671 }
672 }
673
674 }
675}
676
677func (o *Onu) sendEapolFlow(client openolt.OpenoltClient) {
678
679 classifierProto := openolt.Classifier{
680 EthType: uint32(layers.EthernetTypeEAPOL),
681 OVid: 4091,
682 }
683
684 actionProto := openolt.Action{}
685
686 downstreamFlow := openolt.Flow{
687 AccessIntfId: int32(o.PonPortID),
688 OnuId: int32(o.ID),
Matteo Scandolo813402b2019-10-23 19:24:52 -0700689 UniId: int32(0), // NOTE do not hardcode this, we need to support multiple UNIs
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700690 FlowId: uint32(o.ID),
691 FlowType: "downstream",
692 AllocId: int32(0),
693 NetworkIntfId: int32(0),
694 GemportId: int32(1), // FIXME use the same value as CreateGemPortRequest PortID, do not hardcode
695 Classifier: &classifierProto,
696 Action: &actionProto,
697 Priority: int32(100),
698 Cookie: uint64(o.ID),
699 PortNo: uint32(o.ID), // NOTE we are using this to map an incoming packetIndication to an ONU
700 }
701
702 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
703 log.WithFields(log.Fields{
704 "IntfId": o.PonPortID,
705 "OnuId": o.ID,
706 "FlowId": downstreamFlow.FlowId,
707 "PortNo": downstreamFlow.PortNo,
708 "SerialNumber": common.OnuSnToString(o.SerialNumber),
709 }).Fatalf("Failed to EAPOL Flow")
710 }
711 log.WithFields(log.Fields{
712 "IntfId": o.PonPortID,
713 "OnuId": o.ID,
714 "FlowId": downstreamFlow.FlowId,
715 "PortNo": downstreamFlow.PortNo,
716 "SerialNumber": common.OnuSnToString(o.SerialNumber),
717 }).Info("Sent EAPOL Flow")
718}
719
720func (o *Onu) sendDhcpFlow(client openolt.OpenoltClient) {
721 classifierProto := openolt.Classifier{
722 EthType: uint32(layers.EthernetTypeIPv4),
723 SrcPort: uint32(68),
724 DstPort: uint32(67),
725 }
726
727 actionProto := openolt.Action{}
728
729 downstreamFlow := openolt.Flow{
730 AccessIntfId: int32(o.PonPortID),
731 OnuId: int32(o.ID),
Matteo Scandolo813402b2019-10-23 19:24:52 -0700732 UniId: int32(0), // FIXME do not hardcode this
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700733 FlowId: uint32(o.ID),
734 FlowType: "downstream",
735 AllocId: int32(0),
736 NetworkIntfId: int32(0),
737 GemportId: int32(1), // FIXME use the same value as CreateGemPortRequest PortID, do not hardcode
738 Classifier: &classifierProto,
739 Action: &actionProto,
740 Priority: int32(100),
741 Cookie: uint64(o.ID),
742 PortNo: uint32(o.ID), // NOTE we are using this to map an incoming packetIndication to an ONU
743 }
744
745 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
746 log.WithFields(log.Fields{
747 "IntfId": o.PonPortID,
748 "OnuId": o.ID,
749 "FlowId": downstreamFlow.FlowId,
750 "PortNo": downstreamFlow.PortNo,
751 "SerialNumber": common.OnuSnToString(o.SerialNumber),
752 }).Fatalf("Failed to send DHCP Flow")
753 }
754 log.WithFields(log.Fields{
755 "IntfId": o.PonPortID,
756 "OnuId": o.ID,
757 "FlowId": downstreamFlow.FlowId,
758 "PortNo": downstreamFlow.PortNo,
759 "SerialNumber": common.OnuSnToString(o.SerialNumber),
760 }).Info("Sent DHCP Flow")
761}