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