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