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