blob: 9fa224d9c8676f5a12295b5f7aad06f88832309c [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
William Kurkian0418bc82019-11-06 12:16:24 -0500218func (o *Onu) logStateChange(src string, dst string) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700219 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 Kurkian0418bc82019-11-06 12:16:24 -0500309func (o *Onu) processOmciMessage(message omcisim.OmciChMessage) {
William Kurkian9dadc5b2019-10-22 13:51:57 -0400310 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
William Kurkian0418bc82019-11-06 12:16:24 -0500331func (o *Onu) NewSN(oltid int, intfid uint32, onuid uint32) *openolt.SerialNumber {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700332
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
William Kurkian0418bc82019-11-06 12:16:24 -0500345func (o *Onu) sendDyingGaspInd(msg DyingGaspIndicationMessage, stream openolt.Openolt_EnableIndicationServer) error {
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700346 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
William Kurkian0418bc82019-11-06 12:16:24 -0500367func (o *Onu) sendOnuDiscIndication(msg OnuDiscIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700368 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
William Kurkian0418bc82019-11-06 12:16:24 -0500393func (o *Onu) sendOnuIndication(msg OnuIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700394 // 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
William Kurkian0418bc82019-11-06 12:16:24 -0500420func (o *Onu) handleOmciMessage(msg OmciMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700421
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
William Kurkian0418bc82019-11-06 12:16:24 -0500480func (o *Onu) SetID(id uint32) {
481 o.ID = id
482}
483
Matteo Scandolo813402b2019-10-23 19:24:52 -0700484func (o *Onu) handleFlowUpdate(msg OnuFlowUpdateMessage) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700485 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700486 "DstPort": msg.Flow.Classifier.DstPort,
487 "EthType": fmt.Sprintf("%x", msg.Flow.Classifier.EthType),
488 "FlowId": msg.Flow.FlowId,
489 "FlowType": msg.Flow.FlowType,
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700490 "InnerVlan": msg.Flow.Classifier.IVid,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700491 "IntfId": msg.Flow.AccessIntfId,
492 "IpProto": msg.Flow.Classifier.IpProto,
493 "OnuId": msg.Flow.OnuId,
494 "OnuSn": o.Sn(),
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700495 "OuterVlan": msg.Flow.Classifier.OVid,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700496 "PortNo": msg.Flow.PortNo,
497 "SrcPort": msg.Flow.Classifier.SrcPort,
498 "UniID": msg.Flow.UniId,
499 }).Debug("ONU receives Flow")
500
Matteo Scandolo813402b2019-10-23 19:24:52 -0700501 if msg.Flow.UniId != 0 {
502 // as of now BBSim only support a single UNI, so ignore everything that is not targeted to it
503 onuLogger.WithFields(log.Fields{
504 "IntfId": o.PonPortID,
505 "OnuId": o.ID,
506 "SerialNumber": o.Sn(),
507 }).Debug("Ignoring flow as it's not for the first UNI")
508 return
509 }
510
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700511 if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeEAPOL) && msg.Flow.Classifier.OVid == 4091 {
Matteo Scandolo27428702019-10-11 16:21:16 -0700512 // NOTE storing the PortNO, it's needed when sending PacketIndications
Matteo Scandolo813402b2019-10-23 19:24:52 -0700513 o.storePortNumber(uint32(msg.Flow.PortNo))
Matteo Scandolo27428702019-10-11 16:21:16 -0700514
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700515 // NOTE if we receive the EAPOL flows but we don't have GemPorts
516 // go an intermediate state, otherwise start auth
517 if o.InternalState.Is("enabled") {
518 if err := o.InternalState.Event("receive_eapol_flow"); err != nil {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700519 log.Warnf("Can't go to eapol_flow_received: %v", err)
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700520 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700521 } else if o.InternalState.Is("gem_port_added") {
Matteo Scandoloc1147092019-10-29 09:38:33 -0700522
523 if o.Auth == true {
524 if err := o.InternalState.Event("start_auth"); err != nil {
525 log.Warnf("Can't go to auth_started: %v", err)
526 }
527 } else {
528 onuLogger.WithFields(log.Fields{
529 "IntfId": o.PonPortID,
530 "OnuId": o.ID,
531 "SerialNumber": o.Sn(),
532 }).Warn("Not starting authentication as Auth bit is not set in CLI parameters")
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700533 }
Matteo Scandoloc1147092019-10-29 09:38:33 -0700534
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700535 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700536 } else if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeIPv4) &&
537 msg.Flow.Classifier.SrcPort == uint32(68) &&
538 msg.Flow.Classifier.DstPort == uint32(67) {
Matteo Scandolo99f18462019-10-28 14:14:28 -0700539
540 // keep track that we reveived the DHCP Flows so that we can transition the state to dhcp_started
541 o.DhcpFlowReceived = true
Matteo Scandoloc1147092019-10-29 09:38:33 -0700542
543 if o.Dhcp == true {
544 // NOTE we are receiving mulitple DHCP flows but we shouldn't call the transition multiple times
545 if err := o.InternalState.Event("start_dhcp"); err != nil {
546 log.Errorf("Can't go to dhcp_started: %v", err)
547 }
548 } else {
549 onuLogger.WithFields(log.Fields{
550 "IntfId": o.PonPortID,
551 "OnuId": o.ID,
552 "SerialNumber": o.Sn(),
553 }).Warn("Not starting DHCP as Dhcp bit is not set in CLI parameters")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700554 }
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700555 }
556}
557
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700558// HexDecode converts the hex encoding to binary
559func HexDecode(pkt []byte) []byte {
560 p := make([]byte, len(pkt)/2)
561 for i, j := 0, 0; i < len(pkt); i, j = i+2, j+1 {
562 // Go figure this ;)
563 u := (pkt[i] & 15) + (pkt[i]>>6)*9
564 l := (pkt[i+1] & 15) + (pkt[i+1]>>6)*9
565 p[j] = u<<4 + l
566 }
567 onuLogger.Tracef("Omci decoded: %x.", p)
568 return p
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700569}
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700570
571// BBR methods
572
573func sendOmciMsg(pktBytes []byte, intfId uint32, onuId uint32, sn *openolt.SerialNumber, msgType string, client openolt.OpenoltClient) {
574 omciMsg := openolt.OmciMsg{
575 IntfId: intfId,
576 OnuId: onuId,
577 Pkt: pktBytes,
578 }
579
580 if _, err := client.OmciMsgOut(context.Background(), &omciMsg); err != nil {
581 log.WithFields(log.Fields{
582 "IntfId": intfId,
583 "OnuId": onuId,
584 "SerialNumber": common.OnuSnToString(sn),
585 "Pkt": omciMsg.Pkt,
586 }).Fatalf("Failed to send MIB Reset")
587 }
588 log.WithFields(log.Fields{
589 "IntfId": intfId,
590 "OnuId": onuId,
591 "SerialNumber": common.OnuSnToString(sn),
592 "Pkt": omciMsg.Pkt,
593 }).Tracef("Sent OMCI message %s", msgType)
594}
595
596func (onu *Onu) getNextTid(highPriority ...bool) uint16 {
597 var next uint16
598 if len(highPriority) > 0 && highPriority[0] {
599 next = onu.hpTid
600 onu.hpTid += 1
601 if onu.hpTid < 0x8000 {
602 onu.hpTid = 0x8000
603 }
604 } else {
605 next = onu.tid
606 onu.tid += 1
607 if onu.tid >= 0x8000 {
608 onu.tid = 1
609 }
610 }
611 return next
612}
613
614// TODO move this method in responders/omcisim
615func (o *Onu) StartOmci(client openolt.OpenoltClient) {
616 mibReset, _ := omcilib.CreateMibResetRequest(o.getNextTid(false))
617 sendOmciMsg(mibReset, o.PonPortID, o.ID, o.SerialNumber, "mibReset", client)
618}
619
620func (o *Onu) handleOmci(msg OmciIndicationMessage, client openolt.OpenoltClient) {
621 msgType, packet := omcilib.DecodeOmci(msg.OmciInd.Pkt)
622
623 log.WithFields(log.Fields{
624 "IntfId": msg.OmciInd.IntfId,
625 "OnuId": msg.OmciInd.OnuId,
626 "OnuSn": common.OnuSnToString(o.SerialNumber),
627 "Pkt": msg.OmciInd.Pkt,
628 "msgType": msgType,
629 }).Trace("ONU Receveives OMCI Msg")
630 switch msgType {
631 default:
Matteo Scandolo813402b2019-10-23 19:24:52 -0700632 log.WithFields(log.Fields{
633 "IntfId": msg.OmciInd.IntfId,
634 "OnuId": msg.OmciInd.OnuId,
635 "OnuSn": common.OnuSnToString(o.SerialNumber),
636 "Pkt": msg.OmciInd.Pkt,
637 "msgType": msgType,
638 }).Fatalf("unexpected frame: %v", packet)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700639 case omci.MibResetResponseType:
640 mibUpload, _ := omcilib.CreateMibUploadRequest(o.getNextTid(false))
641 sendOmciMsg(mibUpload, o.PonPortID, o.ID, o.SerialNumber, "mibUpload", client)
642 case omci.MibUploadResponseType:
643 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
644 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
645 case omci.MibUploadNextResponseType:
646 o.seqNumber++
647
648 if o.seqNumber > 290 {
649 // NOTE we are done with the MIB Upload (290 is the number of messages the omci-sim library will respond to)
650 galEnet, _ := omcilib.CreateGalEnetRequest(o.getNextTid(false))
651 sendOmciMsg(galEnet, o.PonPortID, o.ID, o.SerialNumber, "CreateGalEnetRequest", client)
652 } else {
653 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
654 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
655 }
656 case omci.CreateResponseType:
657 // NOTE Creating a GemPort,
658 // BBsim actually doesn't care about the values, so we can do we want with the parameters
659 // In the same way we can create a GemPort even without setting up UNIs/TConts/...
660 // but we need the GemPort to trigger the state change
661
662 if !o.HasGemPort {
663 // NOTE this sends a CreateRequestType and BBSim replies with a CreateResponseType
664 // thus we send this request only once
665 gemReq, _ := omcilib.CreateGemPortRequest(o.getNextTid(false))
666 sendOmciMsg(gemReq, o.PonPortID, o.ID, o.SerialNumber, "CreateGemPortRequest", client)
667 o.HasGemPort = true
668 } else {
669 if err := o.InternalState.Event("send_eapol_flow"); err != nil {
670 onuLogger.WithFields(log.Fields{
671 "OnuId": o.ID,
672 "IntfId": o.PonPortID,
673 "OnuSn": o.Sn(),
674 }).Errorf("Error while transitioning ONU State %v", err)
675 }
676 }
677
678 }
679}
680
681func (o *Onu) sendEapolFlow(client openolt.OpenoltClient) {
682
683 classifierProto := openolt.Classifier{
684 EthType: uint32(layers.EthernetTypeEAPOL),
685 OVid: 4091,
686 }
687
688 actionProto := openolt.Action{}
689
690 downstreamFlow := openolt.Flow{
691 AccessIntfId: int32(o.PonPortID),
692 OnuId: int32(o.ID),
Matteo Scandolo813402b2019-10-23 19:24:52 -0700693 UniId: int32(0), // NOTE do not hardcode this, we need to support multiple UNIs
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700694 FlowId: uint32(o.ID),
695 FlowType: "downstream",
696 AllocId: int32(0),
697 NetworkIntfId: int32(0),
698 GemportId: int32(1), // FIXME use the same value as CreateGemPortRequest PortID, do not hardcode
699 Classifier: &classifierProto,
700 Action: &actionProto,
701 Priority: int32(100),
702 Cookie: uint64(o.ID),
703 PortNo: uint32(o.ID), // NOTE we are using this to map an incoming packetIndication to an ONU
704 }
705
706 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
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 }).Fatalf("Failed to EAPOL Flow")
714 }
715 log.WithFields(log.Fields{
716 "IntfId": o.PonPortID,
717 "OnuId": o.ID,
718 "FlowId": downstreamFlow.FlowId,
719 "PortNo": downstreamFlow.PortNo,
720 "SerialNumber": common.OnuSnToString(o.SerialNumber),
721 }).Info("Sent EAPOL Flow")
722}
723
724func (o *Onu) sendDhcpFlow(client openolt.OpenoltClient) {
725 classifierProto := openolt.Classifier{
726 EthType: uint32(layers.EthernetTypeIPv4),
727 SrcPort: uint32(68),
728 DstPort: uint32(67),
729 }
730
731 actionProto := openolt.Action{}
732
733 downstreamFlow := openolt.Flow{
734 AccessIntfId: int32(o.PonPortID),
735 OnuId: int32(o.ID),
Matteo Scandolo813402b2019-10-23 19:24:52 -0700736 UniId: int32(0), // FIXME do not hardcode this
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700737 FlowId: uint32(o.ID),
738 FlowType: "downstream",
739 AllocId: int32(0),
740 NetworkIntfId: int32(0),
741 GemportId: int32(1), // FIXME use the same value as CreateGemPortRequest PortID, do not hardcode
742 Classifier: &classifierProto,
743 Action: &actionProto,
744 Priority: int32(100),
745 Cookie: uint64(o.ID),
746 PortNo: uint32(o.ID), // NOTE we are using this to map an incoming packetIndication to an ONU
747 }
748
749 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
750 log.WithFields(log.Fields{
751 "IntfId": o.PonPortID,
752 "OnuId": o.ID,
753 "FlowId": downstreamFlow.FlowId,
754 "PortNo": downstreamFlow.PortNo,
755 "SerialNumber": common.OnuSnToString(o.SerialNumber),
756 }).Fatalf("Failed to send DHCP Flow")
757 }
758 log.WithFields(log.Fields{
759 "IntfId": o.PonPortID,
760 "OnuId": o.ID,
761 "FlowId": downstreamFlow.FlowId,
762 "PortNo": downstreamFlow.PortNo,
763 "SerialNumber": common.OnuSnToString(o.SerialNumber),
764 }).Info("Sent DHCP Flow")
765}