blob: 618257747aa522110989dc689dbb4188692ede65 [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 Scandoloe33447a2019-10-31 12:38:23 -070035 "time"
Matteo Scandolo4747d292019-08-05 11:50:18 -070036)
37
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070038var onuLogger = log.WithFields(log.Fields{
39 "module": "ONU",
40})
41
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070042type Onu struct {
Matteo Scandolo27428702019-10-11 16:21:16 -070043 ID uint32
44 PonPortID uint32
45 PonPort PonPort
46 STag int
47 CTag int
Matteo Scandoloc1147092019-10-29 09:38:33 -070048 Auth bool // automatically start EAPOL if set to true
49 Dhcp bool // automatically start DHCP if set to true
Matteo Scandolo27428702019-10-11 16:21:16 -070050 // PortNo comes with flows and it's used when sending packetIndications,
51 // There is one PortNo per UNI Port, for now we're only storing the first one
52 // FIXME add support for multiple UNIs
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070053 HwAddress net.HardwareAddr
54 InternalState *fsm.FSM
55
Matteo Scandolo99f18462019-10-28 14:14:28 -070056 // ONU State
57 PortNo uint32
58 DhcpFlowReceived bool
59
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070060 OperState *fsm.FSM
61 SerialNumber *openolt.SerialNumber
62
63 Channel chan Message // this Channel is to track state changes OMCI messages, EAPOL and DHCP packets
Matteo Scandolo40e067f2019-10-16 16:59:41 -070064
65 // OMCI params
66 tid uint16
67 hpTid uint16
68 seqNumber uint16
69 HasGemPort bool
70
71 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 -070072}
73
Matteo Scandolo99f18462019-10-28 14:14:28 -070074func (o *Onu) Sn() string {
Matteo Scandolo40e067f2019-10-16 16:59:41 -070075 return common.OnuSnToString(o.SerialNumber)
Matteo Scandolo86e8ce62019-10-11 12:03:10 -070076}
77
Matteo Scandoloc1147092019-10-29 09:38:33 -070078func CreateONU(olt OltDevice, pon PonPort, id uint32, sTag int, cTag int, auth bool, dhcp bool) *Onu {
Matteo Scandolo4747d292019-08-05 11:50:18 -070079
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070080 o := Onu{
Matteo Scandolo99f18462019-10-28 14:14:28 -070081 ID: id,
82 PonPortID: pon.ID,
83 PonPort: pon,
84 STag: sTag,
85 CTag: cTag,
Matteo Scandoloc1147092019-10-29 09:38:33 -070086 Auth: auth,
87 Dhcp: dhcp,
Matteo Scandolo99f18462019-10-28 14:14:28 -070088 HwAddress: net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, byte(pon.ID), byte(id)},
89 PortNo: 0,
90 Channel: make(chan Message, 2048),
91 tid: 0x1,
92 hpTid: 0x8000,
93 seqNumber: 0,
94 DoneChannel: make(chan bool, 1),
95 DhcpFlowReceived: false,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070096 }
97 o.SerialNumber = o.NewSN(olt.ID, pon.ID, o.ID)
Matteo Scandolo9a3518c2019-08-13 14:36:01 -070098
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070099 // NOTE this state machine is used to track the operational
100 // state as requested by VOLTHA
101 o.OperState = getOperStateFSM(func(e *fsm.Event) {
102 onuLogger.WithFields(log.Fields{
103 "ID": o.ID,
104 }).Debugf("Changing ONU OperState from %s to %s", e.Src, e.Dst)
105 })
106
107 // NOTE this state machine is used to activate the OMCI, EAPOL and DHCP clients
108 o.InternalState = fsm.NewFSM(
109 "created",
110 fsm.Events{
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700111 // DEVICE Lifecycle
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700112 {Name: "discover", Src: []string{"created"}, Dst: "discovered"},
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700113 {Name: "enable", Src: []string{"discovered", "disabled"}, Dst: "enabled"},
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700114 {Name: "receive_eapol_flow", Src: []string{"enabled", "gem_port_added"}, Dst: "eapol_flow_received"},
115 {Name: "add_gem_port", Src: []string{"enabled", "eapol_flow_received"}, Dst: "gem_port_added"},
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700116 // NOTE should disabled state be diffente for oper_disabled (emulating an error) and admin_disabled (received a disabled call via VOLTHA)?
117 {Name: "disable", Src: []string{"eap_response_success_received", "auth_failed", "dhcp_ack_received", "dhcp_failed"}, Dst: "disabled"},
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700118 // EAPOL
Matteo Scandoloe383d5d2019-10-25 14:47:27 -0700119 {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 -0700120 {Name: "eap_start_sent", Src: []string{"auth_started"}, Dst: "eap_start_sent"},
121 {Name: "eap_response_identity_sent", Src: []string{"eap_start_sent"}, Dst: "eap_response_identity_sent"},
122 {Name: "eap_response_challenge_sent", Src: []string{"eap_response_identity_sent"}, Dst: "eap_response_challenge_sent"},
123 {Name: "eap_response_success_received", Src: []string{"eap_response_challenge_sent"}, Dst: "eap_response_success_received"},
124 {Name: "auth_failed", Src: []string{"auth_started", "eap_start_sent", "eap_response_identity_sent", "eap_response_challenge_sent"}, Dst: "auth_failed"},
125 // DHCP
Matteo Scandolofe9ac252019-10-25 11:40:17 -0700126 {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 -0700127 {Name: "dhcp_discovery_sent", Src: []string{"dhcp_started"}, Dst: "dhcp_discovery_sent"},
128 {Name: "dhcp_request_sent", Src: []string{"dhcp_discovery_sent"}, Dst: "dhcp_request_sent"},
129 {Name: "dhcp_ack_received", Src: []string{"dhcp_request_sent"}, Dst: "dhcp_ack_received"},
130 {Name: "dhcp_failed", Src: []string{"dhcp_started", "dhcp_discovery_sent", "dhcp_request_sent"}, Dst: "dhcp_failed"},
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700131 // BBR States
132 // TODO add start OMCI state
133 {Name: "send_eapol_flow", Src: []string{"created"}, Dst: "eapol_flow_sent"},
134 {Name: "send_dhcp_flow", Src: []string{"eapol_flow_sent"}, Dst: "dhcp_flow_sent"},
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700135 },
136 fsm.Callbacks{
137 "enter_state": func(e *fsm.Event) {
138 o.logStateChange(e.Src, e.Dst)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700139 },
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700140 "enter_enabled": func(event *fsm.Event) {
141 msg := Message{
142 Type: OnuIndication,
143 Data: OnuIndicationMessage{
144 OnuSN: o.SerialNumber,
145 PonPortID: o.PonPortID,
146 OperState: UP,
147 },
148 }
149 o.Channel <- msg
150 },
151 "enter_disabled": func(event *fsm.Event) {
152 msg := Message{
153 Type: OnuIndication,
154 Data: OnuIndicationMessage{
155 OnuSN: o.SerialNumber,
156 PonPortID: o.PonPortID,
157 OperState: DOWN,
158 },
159 }
160 o.Channel <- msg
161 },
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700162 "enter_auth_started": func(e *fsm.Event) {
163 o.logStateChange(e.Src, e.Dst)
164 msg := Message{
165 Type: StartEAPOL,
166 Data: PacketMessage{
167 PonPortID: o.PonPortID,
168 OnuID: o.ID,
169 },
170 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700171 o.Channel <- msg
Matteo Scandolo4747d292019-08-05 11:50:18 -0700172 },
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700173 "enter_auth_failed": func(e *fsm.Event) {
174 onuLogger.WithFields(log.Fields{
175 "OnuId": o.ID,
176 "IntfId": o.PonPortID,
177 "OnuSn": o.Sn(),
178 }).Errorf("ONU failed to authenticate!")
179 },
Matteo Scandolo99f18462019-10-28 14:14:28 -0700180 "before_start_dhcp": func(e *fsm.Event) {
181 if o.DhcpFlowReceived == false {
182 e.Cancel(errors.New("cannot-go-to-dhcp-started-as-dhcp-flow-is-missing"))
183 }
184 },
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700185 "enter_dhcp_started": func(e *fsm.Event) {
186 msg := Message{
187 Type: StartDHCP,
188 Data: PacketMessage{
189 PonPortID: o.PonPortID,
190 OnuID: o.ID,
191 },
192 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700193 o.Channel <- msg
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700194 },
195 "enter_dhcp_failed": func(e *fsm.Event) {
196 onuLogger.WithFields(log.Fields{
197 "OnuId": o.ID,
198 "IntfId": o.PonPortID,
199 "OnuSn": o.Sn(),
200 }).Errorf("ONU failed to DHCP!")
201 },
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700202 "enter_eapol_flow_sent": func(e *fsm.Event) {
203 msg := Message{
204 Type: SendEapolFlow,
205 }
206 o.Channel <- msg
207 },
208 "enter_dhcp_flow_sent": func(e *fsm.Event) {
209 msg := Message{
210 Type: SendDhcpFlow,
211 }
212 o.Channel <- msg
213 },
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700214 },
215 )
Matteo Scandolo27428702019-10-11 16:21:16 -0700216 return &o
Matteo Scandolo4747d292019-08-05 11:50:18 -0700217}
218
William Kurkian0418bc82019-11-06 12:16:24 -0500219func (o *Onu) logStateChange(src string, dst string) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700220 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700221 "OnuId": o.ID,
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700222 "IntfId": o.PonPortID,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700223 "OnuSn": o.Sn(),
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700224 }).Debugf("Changing ONU InternalState from %s to %s", src, dst)
225}
226
Matteo Scandolo99f18462019-10-28 14:14:28 -0700227func (o *Onu) ProcessOnuMessages(stream openolt.Openolt_EnableIndicationServer, client openolt.OpenoltClient) {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700228 onuLogger.WithFields(log.Fields{
Matteo Scandolo4747d292019-08-05 11:50:18 -0700229 "onuID": o.ID,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700230 "onuSN": o.Sn(),
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700231 }).Debug("Started ONU Indication Channel")
232
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700233 for message := range o.Channel {
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700234 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700235 "onuID": o.ID,
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700236 "onuSN": o.Sn(),
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700237 "messageType": message.Type,
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700238 }).Tracef("Received message on ONU Channel")
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700239
240 switch message.Type {
241 case OnuDiscIndication:
242 msg, _ := message.Data.(OnuDiscIndicationMessage)
Matteo Scandoloe33447a2019-10-31 12:38:23 -0700243 // NOTE we need to slow down and send ONU Discovery Indication in batches to better emulate a real scenario
244 time.Sleep(time.Duration(int(o.ID)*o.PonPort.Olt.Delay) * time.Millisecond)
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700245 o.sendOnuDiscIndication(msg, stream)
246 case OnuIndication:
247 msg, _ := message.Data.(OnuIndicationMessage)
248 o.sendOnuIndication(msg, stream)
249 case OMCI:
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700250 msg, _ := message.Data.(OmciMessage)
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700251 o.handleOmciMessage(msg, stream)
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700252 case FlowUpdate:
253 msg, _ := message.Data.(OnuFlowUpdateMessage)
Matteo Scandolo813402b2019-10-23 19:24:52 -0700254 o.handleFlowUpdate(msg)
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700255 case StartEAPOL:
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700256 log.Infof("Receive StartEAPOL message on ONU Channel")
Matteo Scandolo27428702019-10-11 16:21:16 -0700257 eapol.SendEapStart(o.ID, o.PonPortID, o.Sn(), o.PortNo, o.HwAddress, o.InternalState, stream)
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700258 case StartDHCP:
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700259 log.Infof("Receive StartDHCP message on ONU Channel")
Matteo Scandolo27428702019-10-11 16:21:16 -0700260 // FIXME use id, ponId as SendEapStart
261 dhcp.SendDHCPDiscovery(o.PonPortID, o.ID, o.Sn(), o.PortNo, o.InternalState, o.HwAddress, o.CTag, stream)
Matteo Scandolo075b1892019-10-07 12:11:07 -0700262 case OnuPacketOut:
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700263
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700264 msg, _ := message.Data.(OnuPacketMessage)
265
266 log.WithFields(log.Fields{
267 "IntfId": msg.IntfId,
268 "OnuId": msg.OnuId,
269 "pktType": msg.Type,
270 }).Trace("Received OnuPacketOut Message")
271
272 if msg.Type == packetHandlers.EAPOL {
273 eapol.HandleNextPacket(msg.OnuId, msg.IntfId, o.Sn(), o.PortNo, o.InternalState, msg.Packet, stream, client)
274 } else if msg.Type == packetHandlers.DHCP {
Matteo Scandolo075b1892019-10-07 12:11:07 -0700275 // NOTE here we receive packets going from the DHCP Server to the ONU
276 // for now we expect them to be double-tagged, but ideally the should be single tagged
Matteo Scandolo27428702019-10-11 16:21:16 -0700277 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 -0700278 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700279 case OnuPacketIn:
280 // NOTE we only receive BBR packets here.
281 // Eapol.HandleNextPacket can handle both BBSim and BBr cases so the call is the same
282 // in the DHCP case VOLTHA only act as a proxy, the behaviour is completely different thus we have a dhcp.HandleNextBbrPacket
283 msg, _ := message.Data.(OnuPacketMessage)
284
285 log.WithFields(log.Fields{
286 "IntfId": msg.IntfId,
287 "OnuId": msg.OnuId,
288 "pktType": msg.Type,
289 }).Trace("Received OnuPacketIn Message")
290
291 if msg.Type == packetHandlers.EAPOL {
292 eapol.HandleNextPacket(msg.OnuId, msg.IntfId, o.Sn(), o.PortNo, o.InternalState, msg.Packet, stream, client)
293 } else if msg.Type == packetHandlers.DHCP {
294 dhcp.HandleNextBbrPacket(o.ID, o.PonPortID, o.Sn(), o.STag, o.HwAddress, o.DoneChannel, msg.Packet, client)
295 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700296 case DyingGaspIndication:
297 msg, _ := message.Data.(DyingGaspIndicationMessage)
298 o.sendDyingGaspInd(msg, stream)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700299 case OmciIndication:
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700300 msg, _ := message.Data.(OmciIndicationMessage)
301 o.handleOmci(msg, client)
302 case SendEapolFlow:
303 o.sendEapolFlow(client)
304 case SendDhcpFlow:
305 o.sendDhcpFlow(client)
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700306 default:
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700307 onuLogger.Warnf("Received unknown message data %v for type %v in OLT Channel", message.Data, message.Type)
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700308 }
309 }
Matteo Scandolo4747d292019-08-05 11:50:18 -0700310}
311
William Kurkian0418bc82019-11-06 12:16:24 -0500312func (o *Onu) processOmciMessage(message omcisim.OmciChMessage) {
William Kurkian9dadc5b2019-10-22 13:51:57 -0400313 switch message.Type {
314 case omcisim.GemPortAdded:
315 log.WithFields(log.Fields{
316 "OnuId": message.Data.OnuId,
317 "IntfId": message.Data.IntfId,
318 }).Infof("GemPort Added")
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700319
William Kurkian9dadc5b2019-10-22 13:51:57 -0400320 // NOTE if we receive the GemPort but we don't have EAPOL flows
321 // go an intermediate state, otherwise start auth
322 if o.InternalState.Is("enabled") {
323 if err := o.InternalState.Event("add_gem_port"); err != nil {
324 log.Errorf("Can't go to gem_port_added: %v", err)
325 }
326 } else if o.InternalState.Is("eapol_flow_received") {
327 if err := o.InternalState.Event("start_auth"); err != nil {
328 log.Errorf("Can't go to auth_started: %v", err)
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700329 }
330 }
331 }
332}
333
William Kurkian0418bc82019-11-06 12:16:24 -0500334func (o *Onu) NewSN(oltid int, intfid uint32, onuid uint32) *openolt.SerialNumber {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700335
336 sn := new(openolt.SerialNumber)
337
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700338 //sn = new(openolt.SerialNumber)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700339 sn.VendorId = []byte("BBSM")
340 sn.VendorSpecific = []byte{0, byte(oltid % 256), byte(intfid), byte(onuid)}
341
342 return sn
343}
344
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700345// NOTE handle_/process methods can change the ONU internal state as they are receiving messages
346// send method should not change the ONU state
347
William Kurkian0418bc82019-11-06 12:16:24 -0500348func (o *Onu) sendDyingGaspInd(msg DyingGaspIndicationMessage, stream openolt.Openolt_EnableIndicationServer) error {
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700349 alarmData := &openolt.AlarmIndication_DyingGaspInd{
350 DyingGaspInd: &openolt.DyingGaspIndication{
351 IntfId: msg.PonPortID,
352 OnuId: msg.OnuID,
353 Status: "on",
354 },
355 }
356 data := &openolt.Indication_AlarmInd{AlarmInd: &openolt.AlarmIndication{Data: alarmData}}
357
358 if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
359 onuLogger.Errorf("Failed to send DyingGaspInd : %v", err)
360 return err
361 }
362 onuLogger.WithFields(log.Fields{
363 "IntfId": msg.PonPortID,
364 "OnuSn": o.Sn(),
365 "OnuId": msg.OnuID,
366 }).Info("sendDyingGaspInd")
367 return nil
368}
369
William Kurkian0418bc82019-11-06 12:16:24 -0500370func (o *Onu) sendOnuDiscIndication(msg OnuDiscIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700371 discoverData := &openolt.Indication_OnuDiscInd{OnuDiscInd: &openolt.OnuDiscIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700372 IntfId: msg.Onu.PonPortID,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700373 SerialNumber: msg.Onu.SerialNumber,
374 }}
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700375
Matteo Scandolo4747d292019-08-05 11:50:18 -0700376 if err := stream.Send(&openolt.Indication{Data: discoverData}); err != nil {
Matteo Scandolo11006992019-08-28 11:29:46 -0700377 log.Errorf("Failed to send Indication_OnuDiscInd: %v", err)
Matteo Scandolo99f18462019-10-28 14:14:28 -0700378 return
Matteo Scandolo4747d292019-08-05 11:50:18 -0700379 }
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700380
381 if err := o.InternalState.Event("discover"); err != nil {
382 oltLogger.WithFields(log.Fields{
383 "IntfId": o.PonPortID,
384 "OnuSn": o.Sn(),
385 "OnuId": o.ID,
386 }).Infof("Failed to transition ONU to discovered state: %s", err.Error())
387 }
388
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700389 onuLogger.WithFields(log.Fields{
Matteo Scandolo4747d292019-08-05 11:50:18 -0700390 "IntfId": msg.Onu.PonPortID,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700391 "OnuSn": msg.Onu.Sn(),
392 "OnuId": o.ID,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700393 }).Debug("Sent Indication_OnuDiscInd")
394}
395
William Kurkian0418bc82019-11-06 12:16:24 -0500396func (o *Onu) sendOnuIndication(msg OnuIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700397 // NOTE voltha returns an ID, but if we use that ID then it complains:
398 // expected_onu_id: 1, received_onu_id: 1024, event: ONU-id-mismatch, can happen if both voltha and the olt rebooted
399 // so we're using the internal ID that is 1
400 // o.ID = msg.OnuID
Matteo Scandolo4747d292019-08-05 11:50:18 -0700401
402 indData := &openolt.Indication_OnuInd{OnuInd: &openolt.OnuIndication{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700403 IntfId: o.PonPortID,
404 OnuId: o.ID,
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700405 OperState: msg.OperState.String(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700406 AdminState: o.OperState.Current(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700407 SerialNumber: o.SerialNumber,
408 }}
409 if err := stream.Send(&openolt.Indication{Data: indData}); err != nil {
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700410 // TODO do we need to transition to a broken state?
Matteo Scandolo11006992019-08-28 11:29:46 -0700411 log.Errorf("Failed to send Indication_OnuInd: %v", err)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700412 }
Matteo Scandolo9a3518c2019-08-13 14:36:01 -0700413 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700414 "IntfId": o.PonPortID,
415 "OnuId": o.ID,
416 "OperState": msg.OperState.String(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700417 "AdminState": msg.OperState.String(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700418 "OnuSn": o.Sn(),
Matteo Scandolo4747d292019-08-05 11:50:18 -0700419 }).Debug("Sent Indication_OnuInd")
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700420
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700421}
422
William Kurkian0418bc82019-11-06 12:16:24 -0500423func (o *Onu) handleOmciMessage(msg OmciMessage, stream openolt.Openolt_EnableIndicationServer) {
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700424
425 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700426 "IntfId": o.PonPortID,
Matteo Scandolo27428702019-10-11 16:21:16 -0700427 "SerialNumber": o.Sn(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700428 "omciPacket": msg.omciMsg.Pkt,
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700429 }).Tracef("Received OMCI message")
430
431 var omciInd openolt.OmciIndication
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700432 respPkt, err := omcisim.OmciSim(o.PonPortID, o.ID, HexDecode(msg.omciMsg.Pkt))
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700433 if err != nil {
Matteo Scandolo27428702019-10-11 16:21:16 -0700434 onuLogger.WithFields(log.Fields{
435 "IntfId": o.PonPortID,
436 "SerialNumber": o.Sn(),
437 "omciPacket": omciInd.Pkt,
438 "msg": msg,
439 }).Errorf("Error handling OMCI message %v", msg)
440 return
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700441 }
442
443 omciInd.IntfId = o.PonPortID
444 omciInd.OnuId = o.ID
445 omciInd.Pkt = respPkt
446
447 omci := &openolt.Indication_OmciInd{OmciInd: &omciInd}
448 if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
Matteo Scandolo27428702019-10-11 16:21:16 -0700449 onuLogger.WithFields(log.Fields{
450 "IntfId": o.PonPortID,
451 "SerialNumber": o.Sn(),
452 "omciPacket": omciInd.Pkt,
453 "msg": msg,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700454 }).Errorf("send omcisim indication failed: %v", err)
Matteo Scandolo27428702019-10-11 16:21:16 -0700455 return
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700456 }
457 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700458 "IntfId": o.PonPortID,
Matteo Scandolo27428702019-10-11 16:21:16 -0700459 "SerialNumber": o.Sn(),
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700460 "omciPacket": omciInd.Pkt,
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700461 }).Tracef("Sent OMCI message")
462}
463
Matteo Scandolo27428702019-10-11 16:21:16 -0700464func (o *Onu) storePortNumber(portNo uint32) {
Matteo Scandolo813402b2019-10-23 19:24:52 -0700465 // NOTE this needed only as long as we don't support multiple UNIs
Matteo Scandolo27428702019-10-11 16:21:16 -0700466 // we need to add support for multiple UNIs
467 // the action plan is:
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700468 // - refactor the omcisim-sim library to use https://github.com/cboling/omci instead of canned messages
Matteo Scandolo27428702019-10-11 16:21:16 -0700469 // - change the library so that it reports a single UNI and remove this workaroung
470 // - add support for multiple UNIs in BBSim
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700471 if o.PortNo == 0 || portNo < o.PortNo {
Matteo Scandolo813402b2019-10-23 19:24:52 -0700472 onuLogger.WithFields(log.Fields{
473 "IntfId": o.PonPortID,
474 "OnuId": o.ID,
475 "SerialNumber": o.Sn(),
476 "OnuPortNo": o.PortNo,
477 "FlowPortNo": portNo,
478 }).Debug("Storing ONU portNo")
Matteo Scandolo27428702019-10-11 16:21:16 -0700479 o.PortNo = portNo
480 }
481}
482
William Kurkian0418bc82019-11-06 12:16:24 -0500483func (o *Onu) SetID(id uint32) {
484 o.ID = id
485}
486
Matteo Scandolo813402b2019-10-23 19:24:52 -0700487func (o *Onu) handleFlowUpdate(msg OnuFlowUpdateMessage) {
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700488 onuLogger.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700489 "DstPort": msg.Flow.Classifier.DstPort,
490 "EthType": fmt.Sprintf("%x", msg.Flow.Classifier.EthType),
491 "FlowId": msg.Flow.FlowId,
492 "FlowType": msg.Flow.FlowType,
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700493 "InnerVlan": msg.Flow.Classifier.IVid,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700494 "IntfId": msg.Flow.AccessIntfId,
495 "IpProto": msg.Flow.Classifier.IpProto,
496 "OnuId": msg.Flow.OnuId,
497 "OnuSn": o.Sn(),
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700498 "OuterVlan": msg.Flow.Classifier.OVid,
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700499 "PortNo": msg.Flow.PortNo,
500 "SrcPort": msg.Flow.Classifier.SrcPort,
501 "UniID": msg.Flow.UniId,
502 }).Debug("ONU receives Flow")
503
Matteo Scandolo813402b2019-10-23 19:24:52 -0700504 if msg.Flow.UniId != 0 {
505 // as of now BBSim only support a single UNI, so ignore everything that is not targeted to it
506 onuLogger.WithFields(log.Fields{
507 "IntfId": o.PonPortID,
508 "OnuId": o.ID,
509 "SerialNumber": o.Sn(),
510 }).Debug("Ignoring flow as it's not for the first UNI")
511 return
512 }
513
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700514 if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeEAPOL) && msg.Flow.Classifier.OVid == 4091 {
Matteo Scandolo27428702019-10-11 16:21:16 -0700515 // NOTE storing the PortNO, it's needed when sending PacketIndications
Matteo Scandolo813402b2019-10-23 19:24:52 -0700516 o.storePortNumber(uint32(msg.Flow.PortNo))
Matteo Scandolo27428702019-10-11 16:21:16 -0700517
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700518 // NOTE if we receive the EAPOL flows but we don't have GemPorts
519 // go an intermediate state, otherwise start auth
520 if o.InternalState.Is("enabled") {
521 if err := o.InternalState.Event("receive_eapol_flow"); err != nil {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700522 log.Warnf("Can't go to eapol_flow_received: %v", err)
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700523 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700524 } else if o.InternalState.Is("gem_port_added") {
Matteo Scandoloc1147092019-10-29 09:38:33 -0700525
526 if o.Auth == true {
527 if err := o.InternalState.Event("start_auth"); err != nil {
528 log.Warnf("Can't go to auth_started: %v", err)
529 }
530 } else {
531 onuLogger.WithFields(log.Fields{
532 "IntfId": o.PonPortID,
533 "OnuId": o.ID,
534 "SerialNumber": o.Sn(),
535 }).Warn("Not starting authentication as Auth bit is not set in CLI parameters")
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700536 }
Matteo Scandoloc1147092019-10-29 09:38:33 -0700537
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700538 }
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700539 } else if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeIPv4) &&
540 msg.Flow.Classifier.SrcPort == uint32(68) &&
541 msg.Flow.Classifier.DstPort == uint32(67) {
Matteo Scandolo99f18462019-10-28 14:14:28 -0700542
543 // keep track that we reveived the DHCP Flows so that we can transition the state to dhcp_started
544 o.DhcpFlowReceived = true
Matteo Scandoloc1147092019-10-29 09:38:33 -0700545
546 if o.Dhcp == true {
547 // NOTE we are receiving mulitple DHCP flows but we shouldn't call the transition multiple times
548 if err := o.InternalState.Event("start_dhcp"); err != nil {
549 log.Errorf("Can't go to dhcp_started: %v", err)
550 }
551 } else {
552 onuLogger.WithFields(log.Fields{
553 "IntfId": o.PonPortID,
554 "OnuId": o.ID,
555 "SerialNumber": o.Sn(),
556 }).Warn("Not starting DHCP as Dhcp bit is not set in CLI parameters")
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700557 }
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700558 }
559}
560
Matteo Scandoloc559ef12019-08-20 13:24:21 -0700561// HexDecode converts the hex encoding to binary
562func HexDecode(pkt []byte) []byte {
563 p := make([]byte, len(pkt)/2)
564 for i, j := 0, 0; i < len(pkt); i, j = i+2, j+1 {
565 // Go figure this ;)
566 u := (pkt[i] & 15) + (pkt[i]>>6)*9
567 l := (pkt[i+1] & 15) + (pkt[i+1]>>6)*9
568 p[j] = u<<4 + l
569 }
570 onuLogger.Tracef("Omci decoded: %x.", p)
571 return p
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700572}
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700573
574// BBR methods
575
576func sendOmciMsg(pktBytes []byte, intfId uint32, onuId uint32, sn *openolt.SerialNumber, msgType string, client openolt.OpenoltClient) {
577 omciMsg := openolt.OmciMsg{
578 IntfId: intfId,
579 OnuId: onuId,
580 Pkt: pktBytes,
581 }
582
583 if _, err := client.OmciMsgOut(context.Background(), &omciMsg); err != nil {
584 log.WithFields(log.Fields{
585 "IntfId": intfId,
586 "OnuId": onuId,
587 "SerialNumber": common.OnuSnToString(sn),
588 "Pkt": omciMsg.Pkt,
589 }).Fatalf("Failed to send MIB Reset")
590 }
591 log.WithFields(log.Fields{
592 "IntfId": intfId,
593 "OnuId": onuId,
594 "SerialNumber": common.OnuSnToString(sn),
595 "Pkt": omciMsg.Pkt,
596 }).Tracef("Sent OMCI message %s", msgType)
597}
598
599func (onu *Onu) getNextTid(highPriority ...bool) uint16 {
600 var next uint16
601 if len(highPriority) > 0 && highPriority[0] {
602 next = onu.hpTid
603 onu.hpTid += 1
604 if onu.hpTid < 0x8000 {
605 onu.hpTid = 0x8000
606 }
607 } else {
608 next = onu.tid
609 onu.tid += 1
610 if onu.tid >= 0x8000 {
611 onu.tid = 1
612 }
613 }
614 return next
615}
616
617// TODO move this method in responders/omcisim
618func (o *Onu) StartOmci(client openolt.OpenoltClient) {
619 mibReset, _ := omcilib.CreateMibResetRequest(o.getNextTid(false))
620 sendOmciMsg(mibReset, o.PonPortID, o.ID, o.SerialNumber, "mibReset", client)
621}
622
623func (o *Onu) handleOmci(msg OmciIndicationMessage, client openolt.OpenoltClient) {
624 msgType, packet := omcilib.DecodeOmci(msg.OmciInd.Pkt)
625
626 log.WithFields(log.Fields{
627 "IntfId": msg.OmciInd.IntfId,
628 "OnuId": msg.OmciInd.OnuId,
629 "OnuSn": common.OnuSnToString(o.SerialNumber),
630 "Pkt": msg.OmciInd.Pkt,
631 "msgType": msgType,
632 }).Trace("ONU Receveives OMCI Msg")
633 switch msgType {
634 default:
Matteo Scandolo813402b2019-10-23 19:24:52 -0700635 log.WithFields(log.Fields{
636 "IntfId": msg.OmciInd.IntfId,
637 "OnuId": msg.OmciInd.OnuId,
638 "OnuSn": common.OnuSnToString(o.SerialNumber),
639 "Pkt": msg.OmciInd.Pkt,
640 "msgType": msgType,
641 }).Fatalf("unexpected frame: %v", packet)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700642 case omci.MibResetResponseType:
643 mibUpload, _ := omcilib.CreateMibUploadRequest(o.getNextTid(false))
644 sendOmciMsg(mibUpload, o.PonPortID, o.ID, o.SerialNumber, "mibUpload", client)
645 case omci.MibUploadResponseType:
646 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
647 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
648 case omci.MibUploadNextResponseType:
649 o.seqNumber++
650
651 if o.seqNumber > 290 {
652 // NOTE we are done with the MIB Upload (290 is the number of messages the omci-sim library will respond to)
653 galEnet, _ := omcilib.CreateGalEnetRequest(o.getNextTid(false))
654 sendOmciMsg(galEnet, o.PonPortID, o.ID, o.SerialNumber, "CreateGalEnetRequest", client)
655 } else {
656 mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber)
657 sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client)
658 }
659 case omci.CreateResponseType:
660 // NOTE Creating a GemPort,
661 // BBsim actually doesn't care about the values, so we can do we want with the parameters
662 // In the same way we can create a GemPort even without setting up UNIs/TConts/...
663 // but we need the GemPort to trigger the state change
664
665 if !o.HasGemPort {
666 // NOTE this sends a CreateRequestType and BBSim replies with a CreateResponseType
667 // thus we send this request only once
668 gemReq, _ := omcilib.CreateGemPortRequest(o.getNextTid(false))
669 sendOmciMsg(gemReq, o.PonPortID, o.ID, o.SerialNumber, "CreateGemPortRequest", client)
670 o.HasGemPort = true
671 } else {
672 if err := o.InternalState.Event("send_eapol_flow"); err != nil {
673 onuLogger.WithFields(log.Fields{
674 "OnuId": o.ID,
675 "IntfId": o.PonPortID,
676 "OnuSn": o.Sn(),
677 }).Errorf("Error while transitioning ONU State %v", err)
678 }
679 }
680
681 }
682}
683
684func (o *Onu) sendEapolFlow(client openolt.OpenoltClient) {
685
686 classifierProto := openolt.Classifier{
687 EthType: uint32(layers.EthernetTypeEAPOL),
688 OVid: 4091,
689 }
690
691 actionProto := openolt.Action{}
692
693 downstreamFlow := openolt.Flow{
694 AccessIntfId: int32(o.PonPortID),
695 OnuId: int32(o.ID),
Matteo Scandolo813402b2019-10-23 19:24:52 -0700696 UniId: int32(0), // NOTE do not hardcode this, we need to support multiple UNIs
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700697 FlowId: uint32(o.ID),
698 FlowType: "downstream",
699 AllocId: int32(0),
700 NetworkIntfId: int32(0),
701 GemportId: int32(1), // FIXME use the same value as CreateGemPortRequest PortID, do not hardcode
702 Classifier: &classifierProto,
703 Action: &actionProto,
704 Priority: int32(100),
705 Cookie: uint64(o.ID),
706 PortNo: uint32(o.ID), // NOTE we are using this to map an incoming packetIndication to an ONU
707 }
708
709 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
710 log.WithFields(log.Fields{
711 "IntfId": o.PonPortID,
712 "OnuId": o.ID,
713 "FlowId": downstreamFlow.FlowId,
714 "PortNo": downstreamFlow.PortNo,
715 "SerialNumber": common.OnuSnToString(o.SerialNumber),
716 }).Fatalf("Failed to EAPOL Flow")
717 }
718 log.WithFields(log.Fields{
719 "IntfId": o.PonPortID,
720 "OnuId": o.ID,
721 "FlowId": downstreamFlow.FlowId,
722 "PortNo": downstreamFlow.PortNo,
723 "SerialNumber": common.OnuSnToString(o.SerialNumber),
724 }).Info("Sent EAPOL Flow")
725}
726
727func (o *Onu) sendDhcpFlow(client openolt.OpenoltClient) {
728 classifierProto := openolt.Classifier{
729 EthType: uint32(layers.EthernetTypeIPv4),
730 SrcPort: uint32(68),
731 DstPort: uint32(67),
732 }
733
734 actionProto := openolt.Action{}
735
736 downstreamFlow := openolt.Flow{
737 AccessIntfId: int32(o.PonPortID),
738 OnuId: int32(o.ID),
Matteo Scandolo813402b2019-10-23 19:24:52 -0700739 UniId: int32(0), // FIXME do not hardcode this
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700740 FlowId: uint32(o.ID),
741 FlowType: "downstream",
742 AllocId: int32(0),
743 NetworkIntfId: int32(0),
744 GemportId: int32(1), // FIXME use the same value as CreateGemPortRequest PortID, do not hardcode
745 Classifier: &classifierProto,
746 Action: &actionProto,
747 Priority: int32(100),
748 Cookie: uint64(o.ID),
749 PortNo: uint32(o.ID), // NOTE we are using this to map an incoming packetIndication to an ONU
750 }
751
752 if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil {
753 log.WithFields(log.Fields{
754 "IntfId": o.PonPortID,
755 "OnuId": o.ID,
756 "FlowId": downstreamFlow.FlowId,
757 "PortNo": downstreamFlow.PortNo,
758 "SerialNumber": common.OnuSnToString(o.SerialNumber),
759 }).Fatalf("Failed to send DHCP Flow")
760 }
761 log.WithFields(log.Fields{
762 "IntfId": o.PonPortID,
763 "OnuId": o.ID,
764 "FlowId": downstreamFlow.FlowId,
765 "PortNo": downstreamFlow.PortNo,
766 "SerialNumber": common.OnuSnToString(o.SerialNumber),
767 }).Info("Sent DHCP Flow")
768}