Matteo Scandolo | 1100699 | 2019-08-28 11:29:46 -0700 | [diff] [blame] | 1 | /* |
| 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 Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 17 | package devices |
| 18 | |
| 19 | import ( |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 20 | "context" |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 21 | "errors" |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 22 | "fmt" |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 23 | "net" |
| 24 | |
| 25 | "time" |
| 26 | |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 27 | "github.com/cboling/omci" |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 28 | "github.com/google/gopacket/layers" |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 29 | "github.com/looplab/fsm" |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 30 | "github.com/opencord/bbsim/internal/bbsim/packetHandlers" |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 31 | "github.com/opencord/bbsim/internal/bbsim/responders/dhcp" |
| 32 | "github.com/opencord/bbsim/internal/bbsim/responders/eapol" |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 33 | "github.com/opencord/bbsim/internal/common" |
| 34 | omcilib "github.com/opencord/bbsim/internal/common/omci" |
| 35 | omcisim "github.com/opencord/omci-sim" |
Matteo Scandolo | 3de9de0 | 2019-11-14 13:40:03 -0800 | [diff] [blame] | 36 | "github.com/opencord/voltha-protos/v2/go/openolt" |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 37 | log "github.com/sirupsen/logrus" |
| 38 | ) |
| 39 | |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 40 | var onuLogger = log.WithFields(log.Fields{ |
| 41 | "module": "ONU", |
| 42 | }) |
| 43 | |
Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame] | 44 | type Onu struct { |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 45 | ID uint32 |
| 46 | PonPortID uint32 |
| 47 | PonPort PonPort |
| 48 | STag int |
| 49 | CTag int |
| 50 | Auth bool // automatically start EAPOL if set to true |
| 51 | Dhcp bool // automatically start DHCP if set to true |
| 52 | HwAddress net.HardwareAddr |
| 53 | InternalState *fsm.FSM |
| 54 | DiscoveryRetryDelay time.Duration |
| 55 | |
| 56 | // ONU State |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 57 | // PortNo comes with flows and it's used when sending packetIndications, |
| 58 | // There is one PortNo per UNI Port, for now we're only storing the first one |
| 59 | // FIXME add support for multiple UNIs |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 60 | PortNo uint32 |
| 61 | DhcpFlowReceived bool |
| 62 | |
Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame] | 63 | OperState *fsm.FSM |
| 64 | SerialNumber *openolt.SerialNumber |
| 65 | |
| 66 | Channel chan Message // this Channel is to track state changes OMCI messages, EAPOL and DHCP packets |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 67 | |
| 68 | // OMCI params |
| 69 | tid uint16 |
| 70 | hpTid uint16 |
| 71 | seqNumber uint16 |
| 72 | HasGemPort bool |
| 73 | |
| 74 | DoneChannel chan bool // this channel is used to signal once the onu is complete (when the struct is used by BBR) |
Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 77 | func (o *Onu) Sn() string { |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 78 | return common.OnuSnToString(o.SerialNumber) |
Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Matteo Scandolo | c114709 | 2019-10-29 09:38:33 -0700 | [diff] [blame] | 81 | func CreateONU(olt OltDevice, pon PonPort, id uint32, sTag int, cTag int, auth bool, dhcp bool) *Onu { |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 82 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 83 | o := Onu{ |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 84 | ID: id, |
| 85 | PonPortID: pon.ID, |
| 86 | PonPort: pon, |
| 87 | STag: sTag, |
| 88 | CTag: cTag, |
| 89 | Auth: auth, |
| 90 | Dhcp: dhcp, |
| 91 | HwAddress: net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, byte(pon.ID), byte(id)}, |
| 92 | PortNo: 0, |
| 93 | tid: 0x1, |
| 94 | hpTid: 0x8000, |
| 95 | seqNumber: 0, |
| 96 | DoneChannel: make(chan bool, 1), |
| 97 | DhcpFlowReceived: false, |
| 98 | DiscoveryRetryDelay: 60 * time.Second, // this is used to send OnuDiscoveryIndications until an activate call is received |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 99 | } |
| 100 | o.SerialNumber = o.NewSN(olt.ID, pon.ID, o.ID) |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 101 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 102 | // NOTE this state machine is used to track the operational |
| 103 | // state as requested by VOLTHA |
| 104 | o.OperState = getOperStateFSM(func(e *fsm.Event) { |
| 105 | onuLogger.WithFields(log.Fields{ |
| 106 | "ID": o.ID, |
| 107 | }).Debugf("Changing ONU OperState from %s to %s", e.Src, e.Dst) |
| 108 | }) |
| 109 | |
| 110 | // NOTE this state machine is used to activate the OMCI, EAPOL and DHCP clients |
| 111 | o.InternalState = fsm.NewFSM( |
| 112 | "created", |
| 113 | fsm.Events{ |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 114 | // DEVICE Lifecycle |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 115 | {Name: "initialize", Src: []string{"created", "disabled"}, Dst: "initialized"}, |
Pragya Arya | 6a708d6 | 2020-01-01 17:17:20 +0530 | [diff] [blame^] | 116 | {Name: "discover", Src: []string{"initialized", "pon_disabled"}, Dst: "discovered"}, |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 117 | {Name: "enable", Src: []string{"discovered", "disabled"}, Dst: "enabled"}, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 118 | {Name: "receive_eapol_flow", Src: []string{"enabled", "gem_port_added"}, Dst: "eapol_flow_received"}, |
| 119 | {Name: "add_gem_port", Src: []string{"enabled", "eapol_flow_received"}, Dst: "gem_port_added"}, |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 120 | // NOTE should disabled state be different for oper_disabled (emulating an error) and admin_disabled (received a disabled call via VOLTHA)? |
| 121 | {Name: "disable", Src: []string{"enabled", "eap_response_success_received", "auth_failed", "dhcp_ack_received", "dhcp_failed"}, Dst: "disabled"}, |
Pragya Arya | 6a708d6 | 2020-01-01 17:17:20 +0530 | [diff] [blame^] | 122 | // ONU state when PON port is disabled but ONU is power ON(more states should be added in src?) |
| 123 | {Name: "pon_disabled", Src: []string{"enabled", "gem_port_added", "eapol_flow_received", "eap_response_success_received", "auth_failed", "dhcp_ack_received", "dhcp_failed"}, Dst:"pon_disabled"}, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 124 | // EAPOL |
Matteo Scandolo | 5e081b5 | 2019-11-21 14:34:25 -0800 | [diff] [blame] | 125 | {Name: "start_auth", Src: []string{"eapol_flow_received", "gem_port_added", "eap_start_sent", "eap_response_identity_sent", "eap_response_challenge_sent", "eap_response_success_received", "auth_failed", "dhcp_ack_received", "dhcp_failed"}, Dst: "auth_started"}, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 126 | {Name: "eap_start_sent", Src: []string{"auth_started"}, Dst: "eap_start_sent"}, |
| 127 | {Name: "eap_response_identity_sent", Src: []string{"eap_start_sent"}, Dst: "eap_response_identity_sent"}, |
| 128 | {Name: "eap_response_challenge_sent", Src: []string{"eap_response_identity_sent"}, Dst: "eap_response_challenge_sent"}, |
| 129 | {Name: "eap_response_success_received", Src: []string{"eap_response_challenge_sent"}, Dst: "eap_response_success_received"}, |
| 130 | {Name: "auth_failed", Src: []string{"auth_started", "eap_start_sent", "eap_response_identity_sent", "eap_response_challenge_sent"}, Dst: "auth_failed"}, |
| 131 | // DHCP |
Matteo Scandolo | fe9ac25 | 2019-10-25 11:40:17 -0700 | [diff] [blame] | 132 | {Name: "start_dhcp", Src: []string{"eap_response_success_received", "dhcp_discovery_sent", "dhcp_request_sent", "dhcp_ack_received", "dhcp_failed"}, Dst: "dhcp_started"}, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 133 | {Name: "dhcp_discovery_sent", Src: []string{"dhcp_started"}, Dst: "dhcp_discovery_sent"}, |
| 134 | {Name: "dhcp_request_sent", Src: []string{"dhcp_discovery_sent"}, Dst: "dhcp_request_sent"}, |
| 135 | {Name: "dhcp_ack_received", Src: []string{"dhcp_request_sent"}, Dst: "dhcp_ack_received"}, |
| 136 | {Name: "dhcp_failed", Src: []string{"dhcp_started", "dhcp_discovery_sent", "dhcp_request_sent"}, Dst: "dhcp_failed"}, |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 137 | // BBR States |
| 138 | // TODO add start OMCI state |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 139 | {Name: "send_eapol_flow", Src: []string{"initialized"}, Dst: "eapol_flow_sent"}, |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 140 | {Name: "send_dhcp_flow", Src: []string{"eapol_flow_sent"}, Dst: "dhcp_flow_sent"}, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 141 | }, |
| 142 | fsm.Callbacks{ |
| 143 | "enter_state": func(e *fsm.Event) { |
| 144 | o.logStateChange(e.Src, e.Dst) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 145 | }, |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 146 | "enter_initialized": func(e *fsm.Event) { |
| 147 | // create new channel for ProcessOnuMessages Go routine |
| 148 | o.Channel = make(chan Message, 2048) |
| 149 | }, |
| 150 | "enter_discovered": func(e *fsm.Event) { |
| 151 | msg := Message{ |
| 152 | Type: OnuDiscIndication, |
| 153 | Data: OnuDiscIndicationMessage{ |
| 154 | Onu: &o, |
| 155 | OperState: UP, |
| 156 | }, |
| 157 | } |
| 158 | o.Channel <- msg |
| 159 | }, |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 160 | "enter_enabled": func(event *fsm.Event) { |
| 161 | msg := Message{ |
| 162 | Type: OnuIndication, |
| 163 | Data: OnuIndicationMessage{ |
| 164 | OnuSN: o.SerialNumber, |
| 165 | PonPortID: o.PonPortID, |
| 166 | OperState: UP, |
| 167 | }, |
| 168 | } |
| 169 | o.Channel <- msg |
| 170 | }, |
| 171 | "enter_disabled": func(event *fsm.Event) { |
| 172 | msg := Message{ |
| 173 | Type: OnuIndication, |
| 174 | Data: OnuIndicationMessage{ |
| 175 | OnuSN: o.SerialNumber, |
| 176 | PonPortID: o.PonPortID, |
| 177 | OperState: DOWN, |
| 178 | }, |
| 179 | } |
| 180 | o.Channel <- msg |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 181 | // terminate the ONU's ProcessOnuMessages Go routine |
| 182 | close(o.Channel) |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 183 | }, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 184 | "enter_auth_started": func(e *fsm.Event) { |
| 185 | o.logStateChange(e.Src, e.Dst) |
| 186 | msg := Message{ |
| 187 | Type: StartEAPOL, |
| 188 | Data: PacketMessage{ |
| 189 | PonPortID: o.PonPortID, |
| 190 | OnuID: o.ID, |
| 191 | }, |
| 192 | } |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 193 | o.Channel <- msg |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 194 | }, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 195 | "enter_auth_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 authenticate!") |
| 201 | }, |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 202 | "before_start_dhcp": func(e *fsm.Event) { |
| 203 | if o.DhcpFlowReceived == false { |
| 204 | e.Cancel(errors.New("cannot-go-to-dhcp-started-as-dhcp-flow-is-missing")) |
| 205 | } |
| 206 | }, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 207 | "enter_dhcp_started": func(e *fsm.Event) { |
| 208 | msg := Message{ |
| 209 | Type: StartDHCP, |
| 210 | Data: PacketMessage{ |
| 211 | PonPortID: o.PonPortID, |
| 212 | OnuID: o.ID, |
| 213 | }, |
| 214 | } |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 215 | o.Channel <- msg |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 216 | }, |
| 217 | "enter_dhcp_failed": func(e *fsm.Event) { |
| 218 | onuLogger.WithFields(log.Fields{ |
| 219 | "OnuId": o.ID, |
| 220 | "IntfId": o.PonPortID, |
| 221 | "OnuSn": o.Sn(), |
| 222 | }).Errorf("ONU failed to DHCP!") |
| 223 | }, |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 224 | "enter_eapol_flow_sent": func(e *fsm.Event) { |
| 225 | msg := Message{ |
| 226 | Type: SendEapolFlow, |
| 227 | } |
| 228 | o.Channel <- msg |
| 229 | }, |
| 230 | "enter_dhcp_flow_sent": func(e *fsm.Event) { |
| 231 | msg := Message{ |
| 232 | Type: SendDhcpFlow, |
| 233 | } |
| 234 | o.Channel <- msg |
| 235 | }, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 236 | }, |
| 237 | ) |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 238 | |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 239 | return &o |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 240 | } |
| 241 | |
William Kurkian | 0418bc8 | 2019-11-06 12:16:24 -0500 | [diff] [blame] | 242 | func (o *Onu) logStateChange(src string, dst string) { |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 243 | onuLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 244 | "OnuId": o.ID, |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 245 | "IntfId": o.PonPortID, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 246 | "OnuSn": o.Sn(), |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 247 | }).Debugf("Changing ONU InternalState from %s to %s", src, dst) |
| 248 | } |
| 249 | |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 250 | // ProcessOnuMessages starts indication channel for each ONU |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 251 | func (o *Onu) ProcessOnuMessages(ctx context.Context, stream openolt.Openolt_EnableIndicationServer, client openolt.OpenoltClient) { |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 252 | onuLogger.WithFields(log.Fields{ |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 253 | "onuID": o.ID, |
| 254 | "onuSN": o.Sn(), |
| 255 | "ponPort": o.PonPortID, |
| 256 | }).Debug("Starting ONU Indication Channel") |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 257 | |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 258 | loop: |
| 259 | for { |
| 260 | select { |
| 261 | case <-ctx.Done(): |
| 262 | onuLogger.WithFields(log.Fields{ |
| 263 | "onuID": o.ID, |
| 264 | "onuSN": o.Sn(), |
| 265 | }).Tracef("ONU message handling canceled via context") |
| 266 | break loop |
| 267 | case message, ok := <-o.Channel: |
| 268 | if !ok || ctx.Err() != nil { |
| 269 | onuLogger.WithFields(log.Fields{ |
| 270 | "onuID": o.ID, |
| 271 | "onuSN": o.Sn(), |
| 272 | }).Tracef("ONU message handling canceled via channel close") |
| 273 | break loop |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 274 | } |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 275 | onuLogger.WithFields(log.Fields{ |
| 276 | "onuID": o.ID, |
| 277 | "onuSN": o.Sn(), |
| 278 | "messageType": message.Type, |
| 279 | }).Tracef("Received message on ONU Channel") |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 280 | |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 281 | switch message.Type { |
| 282 | case OnuDiscIndication: |
| 283 | msg, _ := message.Data.(OnuDiscIndicationMessage) |
| 284 | // NOTE we need to slow down and send ONU Discovery Indication in batches to better emulate a real scenario |
| 285 | time.Sleep(time.Duration(int(o.ID)*o.PonPort.Olt.Delay) * time.Millisecond) |
| 286 | o.sendOnuDiscIndication(msg, stream) |
| 287 | case OnuIndication: |
| 288 | msg, _ := message.Data.(OnuIndicationMessage) |
| 289 | o.sendOnuIndication(msg, stream) |
| 290 | case OMCI: |
| 291 | msg, _ := message.Data.(OmciMessage) |
| 292 | o.handleOmciMessage(msg, stream) |
| 293 | case FlowUpdate: |
| 294 | msg, _ := message.Data.(OnuFlowUpdateMessage) |
| 295 | o.handleFlowUpdate(msg) |
| 296 | case StartEAPOL: |
| 297 | log.Infof("Receive StartEAPOL message on ONU Channel") |
| 298 | eapol.SendEapStart(o.ID, o.PonPortID, o.Sn(), o.PortNo, o.HwAddress, o.InternalState, stream) |
| 299 | case StartDHCP: |
| 300 | log.Infof("Receive StartDHCP message on ONU Channel") |
| 301 | // FIXME use id, ponId as SendEapStart |
| 302 | dhcp.SendDHCPDiscovery(o.PonPortID, o.ID, o.Sn(), o.PortNo, o.InternalState, o.HwAddress, o.CTag, stream) |
| 303 | case OnuPacketOut: |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 304 | |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 305 | msg, _ := message.Data.(OnuPacketMessage) |
| 306 | |
| 307 | log.WithFields(log.Fields{ |
| 308 | "IntfId": msg.IntfId, |
| 309 | "OnuId": msg.OnuId, |
| 310 | "pktType": msg.Type, |
| 311 | }).Trace("Received OnuPacketOut Message") |
| 312 | |
| 313 | if msg.Type == packetHandlers.EAPOL { |
| 314 | eapol.HandleNextPacket(msg.OnuId, msg.IntfId, o.Sn(), o.PortNo, o.InternalState, msg.Packet, stream, client) |
| 315 | } else if msg.Type == packetHandlers.DHCP { |
| 316 | // NOTE here we receive packets going from the DHCP Server to the ONU |
| 317 | // for now we expect them to be double-tagged, but ideally the should be single tagged |
| 318 | dhcp.HandleNextPacket(o.ID, o.PonPortID, o.Sn(), o.PortNo, o.HwAddress, o.CTag, o.InternalState, msg.Packet, stream) |
| 319 | } |
| 320 | case OnuPacketIn: |
| 321 | // NOTE we only receive BBR packets here. |
| 322 | // Eapol.HandleNextPacket can handle both BBSim and BBr cases so the call is the same |
| 323 | // in the DHCP case VOLTHA only act as a proxy, the behaviour is completely different thus we have a dhcp.HandleNextBbrPacket |
| 324 | msg, _ := message.Data.(OnuPacketMessage) |
| 325 | |
| 326 | log.WithFields(log.Fields{ |
| 327 | "IntfId": msg.IntfId, |
| 328 | "OnuId": msg.OnuId, |
| 329 | "pktType": msg.Type, |
| 330 | }).Trace("Received OnuPacketIn Message") |
| 331 | |
| 332 | if msg.Type == packetHandlers.EAPOL { |
| 333 | eapol.HandleNextPacket(msg.OnuId, msg.IntfId, o.Sn(), o.PortNo, o.InternalState, msg.Packet, stream, client) |
| 334 | } else if msg.Type == packetHandlers.DHCP { |
| 335 | dhcp.HandleNextBbrPacket(o.ID, o.PonPortID, o.Sn(), o.STag, o.HwAddress, o.DoneChannel, msg.Packet, client) |
| 336 | } |
| 337 | case DyingGaspIndication: |
| 338 | msg, _ := message.Data.(DyingGaspIndicationMessage) |
| 339 | o.sendDyingGaspInd(msg, stream) |
| 340 | case OmciIndication: |
| 341 | msg, _ := message.Data.(OmciIndicationMessage) |
| 342 | o.handleOmci(msg, client) |
| 343 | case SendEapolFlow: |
| 344 | o.sendEapolFlow(client) |
| 345 | case SendDhcpFlow: |
| 346 | o.sendDhcpFlow(client) |
| 347 | default: |
| 348 | onuLogger.Warnf("Received unknown message data %v for type %v in OLT Channel", message.Data, message.Type) |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 349 | } |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 350 | } |
| 351 | } |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 352 | onuLogger.WithFields(log.Fields{ |
| 353 | "onuID": o.ID, |
| 354 | "onuSN": o.Sn(), |
| 355 | }).Debug("Stopped handling ONU Indication Channel") |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 356 | } |
| 357 | |
William Kurkian | 0418bc8 | 2019-11-06 12:16:24 -0500 | [diff] [blame] | 358 | func (o *Onu) processOmciMessage(message omcisim.OmciChMessage) { |
William Kurkian | 9dadc5b | 2019-10-22 13:51:57 -0400 | [diff] [blame] | 359 | switch message.Type { |
| 360 | case omcisim.GemPortAdded: |
| 361 | log.WithFields(log.Fields{ |
| 362 | "OnuId": message.Data.OnuId, |
| 363 | "IntfId": message.Data.IntfId, |
| 364 | }).Infof("GemPort Added") |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 365 | |
William Kurkian | 9dadc5b | 2019-10-22 13:51:57 -0400 | [diff] [blame] | 366 | // NOTE if we receive the GemPort but we don't have EAPOL flows |
| 367 | // go an intermediate state, otherwise start auth |
| 368 | if o.InternalState.Is("enabled") { |
| 369 | if err := o.InternalState.Event("add_gem_port"); err != nil { |
| 370 | log.Errorf("Can't go to gem_port_added: %v", err) |
| 371 | } |
| 372 | } else if o.InternalState.Is("eapol_flow_received") { |
Matteo Scandolo | 3c656a1 | 2019-12-10 09:54:51 -0800 | [diff] [blame] | 373 | if o.Auth == true { |
| 374 | if err := o.InternalState.Event("start_auth"); err != nil { |
| 375 | log.Warnf("Can't go to auth_started: %v", err) |
| 376 | } |
| 377 | } else { |
| 378 | onuLogger.WithFields(log.Fields{ |
| 379 | "IntfId": o.PonPortID, |
| 380 | "OnuId": o.ID, |
| 381 | "SerialNumber": o.Sn(), |
| 382 | }).Warn("Not starting authentication as Auth bit is not set in CLI parameters") |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 383 | } |
| 384 | } |
| 385 | } |
| 386 | } |
| 387 | |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 388 | func (o Onu) NewSN(oltid int, intfid uint32, onuid uint32) *openolt.SerialNumber { |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 389 | |
| 390 | sn := new(openolt.SerialNumber) |
| 391 | |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 392 | //sn = new(openolt.SerialNumber) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 393 | sn.VendorId = []byte("BBSM") |
| 394 | sn.VendorSpecific = []byte{0, byte(oltid % 256), byte(intfid), byte(onuid)} |
| 395 | |
| 396 | return sn |
| 397 | } |
| 398 | |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 399 | // NOTE handle_/process methods can change the ONU internal state as they are receiving messages |
| 400 | // send method should not change the ONU state |
| 401 | |
William Kurkian | 0418bc8 | 2019-11-06 12:16:24 -0500 | [diff] [blame] | 402 | func (o *Onu) sendDyingGaspInd(msg DyingGaspIndicationMessage, stream openolt.Openolt_EnableIndicationServer) error { |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 403 | alarmData := &openolt.AlarmIndication_DyingGaspInd{ |
| 404 | DyingGaspInd: &openolt.DyingGaspIndication{ |
| 405 | IntfId: msg.PonPortID, |
| 406 | OnuId: msg.OnuID, |
| 407 | Status: "on", |
| 408 | }, |
| 409 | } |
| 410 | data := &openolt.Indication_AlarmInd{AlarmInd: &openolt.AlarmIndication{Data: alarmData}} |
| 411 | |
| 412 | if err := stream.Send(&openolt.Indication{Data: data}); err != nil { |
| 413 | onuLogger.Errorf("Failed to send DyingGaspInd : %v", err) |
| 414 | return err |
| 415 | } |
| 416 | onuLogger.WithFields(log.Fields{ |
| 417 | "IntfId": msg.PonPortID, |
| 418 | "OnuSn": o.Sn(), |
| 419 | "OnuId": msg.OnuID, |
| 420 | }).Info("sendDyingGaspInd") |
| 421 | return nil |
| 422 | } |
| 423 | |
William Kurkian | 0418bc8 | 2019-11-06 12:16:24 -0500 | [diff] [blame] | 424 | func (o *Onu) sendOnuDiscIndication(msg OnuDiscIndicationMessage, stream openolt.Openolt_EnableIndicationServer) { |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 425 | discoverData := &openolt.Indication_OnuDiscInd{OnuDiscInd: &openolt.OnuDiscIndication{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 426 | IntfId: msg.Onu.PonPortID, |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 427 | SerialNumber: msg.Onu.SerialNumber, |
| 428 | }} |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 429 | |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 430 | if err := stream.Send(&openolt.Indication{Data: discoverData}); err != nil { |
Matteo Scandolo | 1100699 | 2019-08-28 11:29:46 -0700 | [diff] [blame] | 431 | log.Errorf("Failed to send Indication_OnuDiscInd: %v", err) |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 432 | return |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 433 | } |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 434 | |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 435 | onuLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 436 | "IntfId": msg.Onu.PonPortID, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 437 | "OnuSn": msg.Onu.Sn(), |
| 438 | "OnuId": o.ID, |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 439 | }).Debug("Sent Indication_OnuDiscInd") |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 440 | |
| 441 | // after DiscoveryRetryDelay check if the state is the same and in case send a new OnuDiscIndication |
| 442 | go func(delay time.Duration) { |
Matteo Scandolo | 569e717 | 2019-12-20 11:51:51 -0800 | [diff] [blame] | 443 | time.Sleep(delay) |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 444 | if o.InternalState.Current() == "discovered" { |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 445 | o.sendOnuDiscIndication(msg, stream) |
| 446 | } |
| 447 | }(o.DiscoveryRetryDelay) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 448 | } |
| 449 | |
William Kurkian | 0418bc8 | 2019-11-06 12:16:24 -0500 | [diff] [blame] | 450 | func (o *Onu) sendOnuIndication(msg OnuIndicationMessage, stream openolt.Openolt_EnableIndicationServer) { |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 451 | // NOTE voltha returns an ID, but if we use that ID then it complains: |
| 452 | // expected_onu_id: 1, received_onu_id: 1024, event: ONU-id-mismatch, can happen if both voltha and the olt rebooted |
| 453 | // so we're using the internal ID that is 1 |
| 454 | // o.ID = msg.OnuID |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 455 | |
| 456 | indData := &openolt.Indication_OnuInd{OnuInd: &openolt.OnuIndication{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 457 | IntfId: o.PonPortID, |
| 458 | OnuId: o.ID, |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 459 | OperState: msg.OperState.String(), |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 460 | AdminState: o.OperState.Current(), |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 461 | SerialNumber: o.SerialNumber, |
| 462 | }} |
| 463 | if err := stream.Send(&openolt.Indication{Data: indData}); err != nil { |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 464 | // TODO do we need to transition to a broken state? |
Matteo Scandolo | 1100699 | 2019-08-28 11:29:46 -0700 | [diff] [blame] | 465 | log.Errorf("Failed to send Indication_OnuInd: %v", err) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 466 | } |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 467 | onuLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 468 | "IntfId": o.PonPortID, |
| 469 | "OnuId": o.ID, |
| 470 | "OperState": msg.OperState.String(), |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 471 | "AdminState": msg.OperState.String(), |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 472 | "OnuSn": o.Sn(), |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 473 | }).Debug("Sent Indication_OnuInd") |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 474 | |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 475 | } |
| 476 | |
William Kurkian | 0418bc8 | 2019-11-06 12:16:24 -0500 | [diff] [blame] | 477 | func (o *Onu) handleOmciMessage(msg OmciMessage, stream openolt.Openolt_EnableIndicationServer) { |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 478 | |
| 479 | onuLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 480 | "IntfId": o.PonPortID, |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 481 | "SerialNumber": o.Sn(), |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 482 | "omciPacket": msg.omciMsg.Pkt, |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 483 | }).Tracef("Received OMCI message") |
| 484 | |
| 485 | var omciInd openolt.OmciIndication |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 486 | respPkt, err := omcisim.OmciSim(o.PonPortID, o.ID, HexDecode(msg.omciMsg.Pkt)) |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 487 | if err != nil { |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 488 | onuLogger.WithFields(log.Fields{ |
| 489 | "IntfId": o.PonPortID, |
| 490 | "SerialNumber": o.Sn(), |
| 491 | "omciPacket": omciInd.Pkt, |
| 492 | "msg": msg, |
| 493 | }).Errorf("Error handling OMCI message %v", msg) |
| 494 | return |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | omciInd.IntfId = o.PonPortID |
| 498 | omciInd.OnuId = o.ID |
| 499 | omciInd.Pkt = respPkt |
| 500 | |
| 501 | omci := &openolt.Indication_OmciInd{OmciInd: &omciInd} |
| 502 | if err := stream.Send(&openolt.Indication{Data: omci}); err != nil { |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 503 | onuLogger.WithFields(log.Fields{ |
| 504 | "IntfId": o.PonPortID, |
| 505 | "SerialNumber": o.Sn(), |
| 506 | "omciPacket": omciInd.Pkt, |
| 507 | "msg": msg, |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 508 | }).Errorf("send omcisim indication failed: %v", err) |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 509 | return |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 510 | } |
| 511 | onuLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 512 | "IntfId": o.PonPortID, |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 513 | "SerialNumber": o.Sn(), |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 514 | "omciPacket": omciInd.Pkt, |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 515 | }).Tracef("Sent OMCI message") |
| 516 | } |
| 517 | |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 518 | func (o *Onu) storePortNumber(portNo uint32) { |
Matteo Scandolo | 813402b | 2019-10-23 19:24:52 -0700 | [diff] [blame] | 519 | // NOTE this needed only as long as we don't support multiple UNIs |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 520 | // we need to add support for multiple UNIs |
| 521 | // the action plan is: |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 522 | // - refactor the omcisim-sim library to use https://github.com/cboling/omci instead of canned messages |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 523 | // - change the library so that it reports a single UNI and remove this workaroung |
| 524 | // - add support for multiple UNIs in BBSim |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 525 | if o.PortNo == 0 || portNo < o.PortNo { |
Matteo Scandolo | 813402b | 2019-10-23 19:24:52 -0700 | [diff] [blame] | 526 | onuLogger.WithFields(log.Fields{ |
| 527 | "IntfId": o.PonPortID, |
| 528 | "OnuId": o.ID, |
| 529 | "SerialNumber": o.Sn(), |
| 530 | "OnuPortNo": o.PortNo, |
| 531 | "FlowPortNo": portNo, |
| 532 | }).Debug("Storing ONU portNo") |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 533 | o.PortNo = portNo |
| 534 | } |
| 535 | } |
| 536 | |
William Kurkian | 0418bc8 | 2019-11-06 12:16:24 -0500 | [diff] [blame] | 537 | func (o *Onu) SetID(id uint32) { |
| 538 | o.ID = id |
| 539 | } |
| 540 | |
Matteo Scandolo | 813402b | 2019-10-23 19:24:52 -0700 | [diff] [blame] | 541 | func (o *Onu) handleFlowUpdate(msg OnuFlowUpdateMessage) { |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 542 | onuLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 543 | "DstPort": msg.Flow.Classifier.DstPort, |
| 544 | "EthType": fmt.Sprintf("%x", msg.Flow.Classifier.EthType), |
| 545 | "FlowId": msg.Flow.FlowId, |
| 546 | "FlowType": msg.Flow.FlowType, |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 547 | "InnerVlan": msg.Flow.Classifier.IVid, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 548 | "IntfId": msg.Flow.AccessIntfId, |
| 549 | "IpProto": msg.Flow.Classifier.IpProto, |
| 550 | "OnuId": msg.Flow.OnuId, |
| 551 | "OnuSn": o.Sn(), |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 552 | "OuterVlan": msg.Flow.Classifier.OVid, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 553 | "PortNo": msg.Flow.PortNo, |
| 554 | "SrcPort": msg.Flow.Classifier.SrcPort, |
| 555 | "UniID": msg.Flow.UniId, |
| 556 | }).Debug("ONU receives Flow") |
| 557 | |
Matteo Scandolo | 813402b | 2019-10-23 19:24:52 -0700 | [diff] [blame] | 558 | if msg.Flow.UniId != 0 { |
| 559 | // as of now BBSim only support a single UNI, so ignore everything that is not targeted to it |
| 560 | onuLogger.WithFields(log.Fields{ |
| 561 | "IntfId": o.PonPortID, |
| 562 | "OnuId": o.ID, |
| 563 | "SerialNumber": o.Sn(), |
| 564 | }).Debug("Ignoring flow as it's not for the first UNI") |
| 565 | return |
| 566 | } |
| 567 | |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 568 | if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeEAPOL) && msg.Flow.Classifier.OVid == 4091 { |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 569 | // NOTE storing the PortNO, it's needed when sending PacketIndications |
Matteo Scandolo | 813402b | 2019-10-23 19:24:52 -0700 | [diff] [blame] | 570 | o.storePortNumber(uint32(msg.Flow.PortNo)) |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 571 | |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 572 | // NOTE if we receive the EAPOL flows but we don't have GemPorts |
| 573 | // go an intermediate state, otherwise start auth |
| 574 | if o.InternalState.Is("enabled") { |
| 575 | if err := o.InternalState.Event("receive_eapol_flow"); err != nil { |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 576 | log.Warnf("Can't go to eapol_flow_received: %v", err) |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 577 | } |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 578 | } else if o.InternalState.Is("gem_port_added") { |
Matteo Scandolo | c114709 | 2019-10-29 09:38:33 -0700 | [diff] [blame] | 579 | |
| 580 | if o.Auth == true { |
| 581 | if err := o.InternalState.Event("start_auth"); err != nil { |
| 582 | log.Warnf("Can't go to auth_started: %v", err) |
| 583 | } |
| 584 | } else { |
| 585 | onuLogger.WithFields(log.Fields{ |
| 586 | "IntfId": o.PonPortID, |
| 587 | "OnuId": o.ID, |
| 588 | "SerialNumber": o.Sn(), |
| 589 | }).Warn("Not starting authentication as Auth bit is not set in CLI parameters") |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 590 | } |
Matteo Scandolo | c114709 | 2019-10-29 09:38:33 -0700 | [diff] [blame] | 591 | |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 592 | } |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 593 | } else if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeIPv4) && |
| 594 | msg.Flow.Classifier.SrcPort == uint32(68) && |
| 595 | msg.Flow.Classifier.DstPort == uint32(67) { |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 596 | |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 597 | // keep track that we received the DHCP Flows so that we can transition the state to dhcp_started |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 598 | o.DhcpFlowReceived = true |
Matteo Scandolo | c114709 | 2019-10-29 09:38:33 -0700 | [diff] [blame] | 599 | |
| 600 | if o.Dhcp == true { |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 601 | // NOTE we are receiving multiple DHCP flows but we shouldn't call the transition multiple times |
Matteo Scandolo | c114709 | 2019-10-29 09:38:33 -0700 | [diff] [blame] | 602 | if err := o.InternalState.Event("start_dhcp"); err != nil { |
| 603 | log.Errorf("Can't go to dhcp_started: %v", err) |
| 604 | } |
| 605 | } else { |
| 606 | onuLogger.WithFields(log.Fields{ |
| 607 | "IntfId": o.PonPortID, |
| 608 | "OnuId": o.ID, |
| 609 | "SerialNumber": o.Sn(), |
| 610 | }).Warn("Not starting DHCP as Dhcp bit is not set in CLI parameters") |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 611 | } |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 612 | } |
| 613 | } |
| 614 | |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 615 | // HexDecode converts the hex encoding to binary |
| 616 | func HexDecode(pkt []byte) []byte { |
| 617 | p := make([]byte, len(pkt)/2) |
| 618 | for i, j := 0, 0; i < len(pkt); i, j = i+2, j+1 { |
| 619 | // Go figure this ;) |
| 620 | u := (pkt[i] & 15) + (pkt[i]>>6)*9 |
| 621 | l := (pkt[i+1] & 15) + (pkt[i+1]>>6)*9 |
| 622 | p[j] = u<<4 + l |
| 623 | } |
| 624 | onuLogger.Tracef("Omci decoded: %x.", p) |
| 625 | return p |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 626 | } |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 627 | |
| 628 | // BBR methods |
| 629 | |
| 630 | func sendOmciMsg(pktBytes []byte, intfId uint32, onuId uint32, sn *openolt.SerialNumber, msgType string, client openolt.OpenoltClient) { |
| 631 | omciMsg := openolt.OmciMsg{ |
| 632 | IntfId: intfId, |
| 633 | OnuId: onuId, |
| 634 | Pkt: pktBytes, |
| 635 | } |
| 636 | |
| 637 | if _, err := client.OmciMsgOut(context.Background(), &omciMsg); err != nil { |
| 638 | log.WithFields(log.Fields{ |
| 639 | "IntfId": intfId, |
| 640 | "OnuId": onuId, |
| 641 | "SerialNumber": common.OnuSnToString(sn), |
| 642 | "Pkt": omciMsg.Pkt, |
| 643 | }).Fatalf("Failed to send MIB Reset") |
| 644 | } |
| 645 | log.WithFields(log.Fields{ |
| 646 | "IntfId": intfId, |
| 647 | "OnuId": onuId, |
| 648 | "SerialNumber": common.OnuSnToString(sn), |
| 649 | "Pkt": omciMsg.Pkt, |
| 650 | }).Tracef("Sent OMCI message %s", msgType) |
| 651 | } |
| 652 | |
| 653 | func (onu *Onu) getNextTid(highPriority ...bool) uint16 { |
| 654 | var next uint16 |
| 655 | if len(highPriority) > 0 && highPriority[0] { |
| 656 | next = onu.hpTid |
| 657 | onu.hpTid += 1 |
| 658 | if onu.hpTid < 0x8000 { |
| 659 | onu.hpTid = 0x8000 |
| 660 | } |
| 661 | } else { |
| 662 | next = onu.tid |
| 663 | onu.tid += 1 |
| 664 | if onu.tid >= 0x8000 { |
| 665 | onu.tid = 1 |
| 666 | } |
| 667 | } |
| 668 | return next |
| 669 | } |
| 670 | |
| 671 | // TODO move this method in responders/omcisim |
| 672 | func (o *Onu) StartOmci(client openolt.OpenoltClient) { |
| 673 | mibReset, _ := omcilib.CreateMibResetRequest(o.getNextTid(false)) |
| 674 | sendOmciMsg(mibReset, o.PonPortID, o.ID, o.SerialNumber, "mibReset", client) |
| 675 | } |
| 676 | |
| 677 | func (o *Onu) handleOmci(msg OmciIndicationMessage, client openolt.OpenoltClient) { |
| 678 | msgType, packet := omcilib.DecodeOmci(msg.OmciInd.Pkt) |
| 679 | |
| 680 | log.WithFields(log.Fields{ |
| 681 | "IntfId": msg.OmciInd.IntfId, |
| 682 | "OnuId": msg.OmciInd.OnuId, |
| 683 | "OnuSn": common.OnuSnToString(o.SerialNumber), |
| 684 | "Pkt": msg.OmciInd.Pkt, |
| 685 | "msgType": msgType, |
| 686 | }).Trace("ONU Receveives OMCI Msg") |
| 687 | switch msgType { |
| 688 | default: |
Matteo Scandolo | 813402b | 2019-10-23 19:24:52 -0700 | [diff] [blame] | 689 | log.WithFields(log.Fields{ |
| 690 | "IntfId": msg.OmciInd.IntfId, |
| 691 | "OnuId": msg.OmciInd.OnuId, |
| 692 | "OnuSn": common.OnuSnToString(o.SerialNumber), |
| 693 | "Pkt": msg.OmciInd.Pkt, |
| 694 | "msgType": msgType, |
| 695 | }).Fatalf("unexpected frame: %v", packet) |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 696 | case omci.MibResetResponseType: |
| 697 | mibUpload, _ := omcilib.CreateMibUploadRequest(o.getNextTid(false)) |
| 698 | sendOmciMsg(mibUpload, o.PonPortID, o.ID, o.SerialNumber, "mibUpload", client) |
| 699 | case omci.MibUploadResponseType: |
| 700 | mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber) |
| 701 | sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client) |
| 702 | case omci.MibUploadNextResponseType: |
| 703 | o.seqNumber++ |
| 704 | |
| 705 | if o.seqNumber > 290 { |
| 706 | // NOTE we are done with the MIB Upload (290 is the number of messages the omci-sim library will respond to) |
| 707 | galEnet, _ := omcilib.CreateGalEnetRequest(o.getNextTid(false)) |
| 708 | sendOmciMsg(galEnet, o.PonPortID, o.ID, o.SerialNumber, "CreateGalEnetRequest", client) |
| 709 | } else { |
| 710 | mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber) |
| 711 | sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client) |
| 712 | } |
| 713 | case omci.CreateResponseType: |
| 714 | // NOTE Creating a GemPort, |
| 715 | // BBsim actually doesn't care about the values, so we can do we want with the parameters |
| 716 | // In the same way we can create a GemPort even without setting up UNIs/TConts/... |
| 717 | // but we need the GemPort to trigger the state change |
| 718 | |
| 719 | if !o.HasGemPort { |
| 720 | // NOTE this sends a CreateRequestType and BBSim replies with a CreateResponseType |
| 721 | // thus we send this request only once |
| 722 | gemReq, _ := omcilib.CreateGemPortRequest(o.getNextTid(false)) |
| 723 | sendOmciMsg(gemReq, o.PonPortID, o.ID, o.SerialNumber, "CreateGemPortRequest", client) |
| 724 | o.HasGemPort = true |
| 725 | } else { |
| 726 | if err := o.InternalState.Event("send_eapol_flow"); err != nil { |
| 727 | onuLogger.WithFields(log.Fields{ |
| 728 | "OnuId": o.ID, |
| 729 | "IntfId": o.PonPortID, |
| 730 | "OnuSn": o.Sn(), |
| 731 | }).Errorf("Error while transitioning ONU State %v", err) |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | func (o *Onu) sendEapolFlow(client openolt.OpenoltClient) { |
| 739 | |
| 740 | classifierProto := openolt.Classifier{ |
| 741 | EthType: uint32(layers.EthernetTypeEAPOL), |
| 742 | OVid: 4091, |
| 743 | } |
| 744 | |
| 745 | actionProto := openolt.Action{} |
| 746 | |
| 747 | downstreamFlow := openolt.Flow{ |
| 748 | AccessIntfId: int32(o.PonPortID), |
| 749 | OnuId: int32(o.ID), |
Matteo Scandolo | 813402b | 2019-10-23 19:24:52 -0700 | [diff] [blame] | 750 | UniId: int32(0), // NOTE do not hardcode this, we need to support multiple UNIs |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 751 | FlowId: uint32(o.ID), |
| 752 | FlowType: "downstream", |
| 753 | AllocId: int32(0), |
| 754 | NetworkIntfId: int32(0), |
| 755 | GemportId: int32(1), // FIXME use the same value as CreateGemPortRequest PortID, do not hardcode |
| 756 | Classifier: &classifierProto, |
| 757 | Action: &actionProto, |
| 758 | Priority: int32(100), |
| 759 | Cookie: uint64(o.ID), |
| 760 | PortNo: uint32(o.ID), // NOTE we are using this to map an incoming packetIndication to an ONU |
| 761 | } |
| 762 | |
| 763 | if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil { |
| 764 | log.WithFields(log.Fields{ |
| 765 | "IntfId": o.PonPortID, |
| 766 | "OnuId": o.ID, |
| 767 | "FlowId": downstreamFlow.FlowId, |
| 768 | "PortNo": downstreamFlow.PortNo, |
| 769 | "SerialNumber": common.OnuSnToString(o.SerialNumber), |
| 770 | }).Fatalf("Failed to EAPOL Flow") |
| 771 | } |
| 772 | log.WithFields(log.Fields{ |
| 773 | "IntfId": o.PonPortID, |
| 774 | "OnuId": o.ID, |
| 775 | "FlowId": downstreamFlow.FlowId, |
| 776 | "PortNo": downstreamFlow.PortNo, |
| 777 | "SerialNumber": common.OnuSnToString(o.SerialNumber), |
| 778 | }).Info("Sent EAPOL Flow") |
| 779 | } |
| 780 | |
| 781 | func (o *Onu) sendDhcpFlow(client openolt.OpenoltClient) { |
| 782 | classifierProto := openolt.Classifier{ |
| 783 | EthType: uint32(layers.EthernetTypeIPv4), |
| 784 | SrcPort: uint32(68), |
| 785 | DstPort: uint32(67), |
| 786 | } |
| 787 | |
| 788 | actionProto := openolt.Action{} |
| 789 | |
| 790 | downstreamFlow := openolt.Flow{ |
| 791 | AccessIntfId: int32(o.PonPortID), |
| 792 | OnuId: int32(o.ID), |
Matteo Scandolo | 813402b | 2019-10-23 19:24:52 -0700 | [diff] [blame] | 793 | UniId: int32(0), // FIXME do not hardcode this |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 794 | FlowId: uint32(o.ID), |
| 795 | FlowType: "downstream", |
| 796 | AllocId: int32(0), |
| 797 | NetworkIntfId: int32(0), |
| 798 | GemportId: int32(1), // FIXME use the same value as CreateGemPortRequest PortID, do not hardcode |
| 799 | Classifier: &classifierProto, |
| 800 | Action: &actionProto, |
| 801 | Priority: int32(100), |
| 802 | Cookie: uint64(o.ID), |
| 803 | PortNo: uint32(o.ID), // NOTE we are using this to map an incoming packetIndication to an ONU |
| 804 | } |
| 805 | |
| 806 | if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil { |
| 807 | log.WithFields(log.Fields{ |
| 808 | "IntfId": o.PonPortID, |
| 809 | "OnuId": o.ID, |
| 810 | "FlowId": downstreamFlow.FlowId, |
| 811 | "PortNo": downstreamFlow.PortNo, |
| 812 | "SerialNumber": common.OnuSnToString(o.SerialNumber), |
| 813 | }).Fatalf("Failed to send DHCP Flow") |
| 814 | } |
| 815 | log.WithFields(log.Fields{ |
| 816 | "IntfId": o.PonPortID, |
| 817 | "OnuId": o.ID, |
| 818 | "FlowId": downstreamFlow.FlowId, |
| 819 | "PortNo": downstreamFlow.PortNo, |
| 820 | "SerialNumber": common.OnuSnToString(o.SerialNumber), |
| 821 | }).Info("Sent DHCP Flow") |
| 822 | } |