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