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