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 | eb6b5af | 2020-06-24 16:23:58 -0700 | [diff] [blame] | 29 | "github.com/jpillora/backoff" |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 30 | "github.com/looplab/fsm" |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 31 | "github.com/opencord/bbsim/internal/bbsim/packetHandlers" |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 32 | "github.com/opencord/bbsim/internal/bbsim/responders/dhcp" |
| 33 | "github.com/opencord/bbsim/internal/bbsim/responders/eapol" |
Arjun E K | 57a7fcb | 2020-01-30 06:44:45 +0000 | [diff] [blame] | 34 | "github.com/opencord/bbsim/internal/bbsim/responders/igmp" |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 35 | "github.com/opencord/bbsim/internal/common" |
| 36 | omcilib "github.com/opencord/bbsim/internal/common/omci" |
| 37 | omcisim "github.com/opencord/omci-sim" |
Matteo Scandolo | 3de9de0 | 2019-11-14 13:40:03 -0800 | [diff] [blame] | 38 | "github.com/opencord/voltha-protos/v2/go/openolt" |
Matteo Scandolo | d74abba | 2020-04-16 16:36:44 -0700 | [diff] [blame] | 39 | "github.com/opencord/voltha-protos/v2/go/tech_profile" |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 40 | log "github.com/sirupsen/logrus" |
| 41 | ) |
| 42 | |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 43 | var onuLogger = log.WithFields(log.Fields{ |
| 44 | "module": "ONU", |
| 45 | }) |
| 46 | |
Pragya Arya | 8bdb453 | 2020-03-02 17:08:09 +0530 | [diff] [blame] | 47 | type FlowKey struct { |
| 48 | ID uint32 |
| 49 | Direction string |
| 50 | } |
| 51 | |
Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame] | 52 | type Onu struct { |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 53 | ID uint32 |
| 54 | PonPortID uint32 |
Matteo Scandolo | eb6b5af | 2020-06-24 16:23:58 -0700 | [diff] [blame] | 55 | PonPort *PonPort |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 56 | STag int |
| 57 | CTag int |
| 58 | Auth bool // automatically start EAPOL if set to true |
| 59 | Dhcp bool // automatically start DHCP if set to true |
| 60 | HwAddress net.HardwareAddr |
| 61 | InternalState *fsm.FSM |
Pragya Arya | 2225f20 | 2020-01-29 18:05:01 +0530 | [diff] [blame] | 62 | DiscoveryRetryDelay time.Duration // this is the time between subsequent Discovery Indication |
| 63 | DiscoveryDelay time.Duration // this is the time to send the first Discovery Indication |
Shrey Baid | f8abccc | 2020-06-15 19:41:22 +0530 | [diff] [blame] | 64 | Backoff *backoff.Backoff |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 65 | // ONU State |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 66 | // PortNo comes with flows and it's used when sending packetIndications, |
| 67 | // There is one PortNo per UNI Port, for now we're only storing the first one |
Matteo Scandolo | 47ef64b | 2020-04-20 14:16:07 -0700 | [diff] [blame] | 68 | // FIXME add support for multiple UNIs (each UNI has a different PortNo) |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 69 | PortNo uint32 |
| 70 | GemPortAdded bool |
| 71 | EapolFlowReceived bool |
| 72 | DhcpFlowReceived bool |
| 73 | Flows []FlowKey |
Matteo Scandolo | eb6b5af | 2020-06-24 16:23:58 -0700 | [diff] [blame] | 74 | FlowIds []uint32 // keep track of the flows we currently have in the ONU |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 75 | |
Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame] | 76 | OperState *fsm.FSM |
| 77 | SerialNumber *openolt.SerialNumber |
| 78 | |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 79 | Channel chan Message // this Channel is to track state changes OMCI messages, EAPOL and DHCP packets |
| 80 | GemPortChannels []chan bool // this channels are used to notify everyone that is interested that a GemPort has been added |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 81 | |
| 82 | // OMCI params |
Matteo Scandolo | eb6b5af | 2020-06-24 16:23:58 -0700 | [diff] [blame] | 83 | tid uint16 |
| 84 | hpTid uint16 |
| 85 | seqNumber uint16 |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 86 | |
Anand S Katti | 0954135 | 2020-01-29 15:54:01 +0530 | [diff] [blame] | 87 | DoneChannel chan bool // this channel is used to signal once the onu is complete (when the struct is used by BBR) |
| 88 | TrafficSchedulers *tech_profile.TrafficSchedulers |
Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 91 | func (o *Onu) Sn() string { |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 92 | return common.OnuSnToString(o.SerialNumber) |
Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 95 | func (o *Onu) GetGemPortChan() chan bool { |
| 96 | listener := make(chan bool, 1) |
| 97 | o.GemPortChannels = append(o.GemPortChannels, listener) |
| 98 | return listener |
| 99 | } |
| 100 | |
Matteo Scandolo | eb6b5af | 2020-06-24 16:23:58 -0700 | [diff] [blame] | 101 | func CreateONU(olt *OltDevice, pon *PonPort, id uint32, sTag int, cTag int, auth bool, dhcp bool, delay time.Duration, isMock bool) *Onu { |
Shrey Baid | f8abccc | 2020-06-15 19:41:22 +0530 | [diff] [blame] | 102 | b := &backoff.Backoff{ |
| 103 | //These are the defaults |
Matteo Scandolo | eb6b5af | 2020-06-24 16:23:58 -0700 | [diff] [blame] | 104 | Min: 5 * time.Second, |
Shrey Baid | f8abccc | 2020-06-15 19:41:22 +0530 | [diff] [blame] | 105 | Max: 35 * time.Second, |
| 106 | Factor: 1.5, |
| 107 | Jitter: false, |
| 108 | } |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 109 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 110 | o := Onu{ |
Pragya Arya | 2225f20 | 2020-01-29 18:05:01 +0530 | [diff] [blame] | 111 | ID: 0, |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 112 | PonPortID: pon.ID, |
| 113 | PonPort: pon, |
| 114 | STag: sTag, |
| 115 | CTag: cTag, |
| 116 | Auth: auth, |
| 117 | Dhcp: dhcp, |
Matteo Scandolo | 378b8c9 | 2020-04-16 14:34:22 -0700 | [diff] [blame] | 118 | HwAddress: net.HardwareAddr{0x2e, 0x60, 0x70, byte(olt.ID), byte(pon.ID), byte(id)}, |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 119 | PortNo: 0, |
| 120 | tid: 0x1, |
| 121 | hpTid: 0x8000, |
| 122 | seqNumber: 0, |
| 123 | DoneChannel: make(chan bool, 1), |
| 124 | DhcpFlowReceived: false, |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 125 | EapolFlowReceived: false, |
| 126 | GemPortAdded: false, |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 127 | DiscoveryRetryDelay: 60 * time.Second, // this is used to send OnuDiscoveryIndications until an activate call is received |
Pragya Arya | 8bdb453 | 2020-03-02 17:08:09 +0530 | [diff] [blame] | 128 | Flows: []FlowKey{}, |
Pragya Arya | 2225f20 | 2020-01-29 18:05:01 +0530 | [diff] [blame] | 129 | DiscoveryDelay: delay, |
Shrey Baid | f8abccc | 2020-06-15 19:41:22 +0530 | [diff] [blame] | 130 | Backoff: b, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 131 | } |
Pragya Arya | 2225f20 | 2020-01-29 18:05:01 +0530 | [diff] [blame] | 132 | o.SerialNumber = o.NewSN(olt.ID, pon.ID, id) |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 133 | // NOTE this state machine is used to track the operational |
| 134 | // state as requested by VOLTHA |
| 135 | o.OperState = getOperStateFSM(func(e *fsm.Event) { |
| 136 | onuLogger.WithFields(log.Fields{ |
| 137 | "ID": o.ID, |
| 138 | }).Debugf("Changing ONU OperState from %s to %s", e.Src, e.Dst) |
| 139 | }) |
| 140 | |
| 141 | // NOTE this state machine is used to activate the OMCI, EAPOL and DHCP clients |
| 142 | o.InternalState = fsm.NewFSM( |
| 143 | "created", |
| 144 | fsm.Events{ |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 145 | // DEVICE Lifecycle |
Hardik Windlass | ad790cb | 2020-06-17 21:26:22 +0530 | [diff] [blame] | 146 | {Name: "initialize", Src: []string{"created", "disabled", "pon_disabled"}, Dst: "initialized"}, |
| 147 | {Name: "discover", Src: []string{"initialized"}, Dst: "discovered"}, |
| 148 | {Name: "enable", Src: []string{"discovered", "pon_disabled"}, Dst: "enabled"}, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 149 | {Name: "receive_eapol_flow", Src: []string{"enabled", "gem_port_added"}, Dst: "eapol_flow_received"}, |
| 150 | {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] | 151 | // NOTE should disabled state be different for oper_disabled (emulating an error) and admin_disabled (received a disabled call via VOLTHA)? |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 152 | {Name: "disable", Src: []string{"enabled", "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] | 153 | // ONU state when PON port is disabled but ONU is power ON(more states should be added in src?) |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 154 | {Name: "pon_disabled", Src: []string{"enabled", "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] | 155 | // EAPOL |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 156 | {Name: "start_auth", Src: []string{"enabled", "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] | 157 | {Name: "eap_start_sent", Src: []string{"auth_started"}, Dst: "eap_start_sent"}, |
| 158 | {Name: "eap_response_identity_sent", Src: []string{"eap_start_sent"}, Dst: "eap_response_identity_sent"}, |
| 159 | {Name: "eap_response_challenge_sent", Src: []string{"eap_response_identity_sent"}, Dst: "eap_response_challenge_sent"}, |
| 160 | {Name: "eap_response_success_received", Src: []string{"eap_response_challenge_sent"}, Dst: "eap_response_success_received"}, |
| 161 | {Name: "auth_failed", Src: []string{"auth_started", "eap_start_sent", "eap_response_identity_sent", "eap_response_challenge_sent"}, Dst: "auth_failed"}, |
| 162 | // DHCP |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 163 | {Name: "start_dhcp", Src: []string{"enabled", "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] | 164 | {Name: "dhcp_discovery_sent", Src: []string{"dhcp_started"}, Dst: "dhcp_discovery_sent"}, |
| 165 | {Name: "dhcp_request_sent", Src: []string{"dhcp_discovery_sent"}, Dst: "dhcp_request_sent"}, |
| 166 | {Name: "dhcp_ack_received", Src: []string{"dhcp_request_sent"}, Dst: "dhcp_ack_received"}, |
| 167 | {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] | 168 | // BBR States |
| 169 | // TODO add start OMCI state |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 170 | {Name: "send_eapol_flow", Src: []string{"initialized"}, Dst: "eapol_flow_sent"}, |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 171 | {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] | 172 | // IGMP |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 173 | {Name: "igmp_join_start", Src: []string{"eap_response_success_received", "dhcp_ack_received", "igmp_left", "igmp_join_error", "igmp_join_started"}, Dst: "igmp_join_started"}, |
| 174 | {Name: "igmp_join_startv3", Src: []string{"eap_response_success_received", "dhcp_ack_received", "igmp_left", "igmp_join_error", "igmp_join_started"}, Dst: "igmp_join_started"}, |
Shubham Sharma | bd4b657 | 2020-02-12 13:00:44 +0000 | [diff] [blame] | 175 | {Name: "igmp_join_error", Src: []string{"igmp_join_started"}, Dst: "igmp_join_error"}, |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 176 | {Name: "igmp_leave", Src: []string{"igmp_join_started", "eap_response_success_received", "dhcp_ack_received"}, Dst: "igmp_left"}, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 177 | }, |
| 178 | fsm.Callbacks{ |
| 179 | "enter_state": func(e *fsm.Event) { |
| 180 | o.logStateChange(e.Src, e.Dst) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 181 | }, |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 182 | "enter_initialized": func(e *fsm.Event) { |
| 183 | // create new channel for ProcessOnuMessages Go routine |
| 184 | o.Channel = make(chan Message, 2048) |
Matteo Scandolo | d7cc6d3 | 2020-02-26 16:51:12 -0800 | [diff] [blame] | 185 | |
| 186 | if err := o.OperState.Event("enable"); err != nil { |
| 187 | onuLogger.WithFields(log.Fields{ |
| 188 | "OnuId": o.ID, |
| 189 | "IntfId": o.PonPortID, |
| 190 | "OnuSn": o.Sn(), |
| 191 | }).Errorf("Cannot change ONU OperState to up: %s", err.Error()) |
| 192 | } |
| 193 | |
Pragya Arya | 1cbefa4 | 2020-01-13 12:15:29 +0530 | [diff] [blame] | 194 | if !isMock { |
| 195 | // start ProcessOnuMessages Go routine |
| 196 | go o.ProcessOnuMessages(olt.enableContext, *olt.OpenoltStream, nil) |
| 197 | } |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 198 | }, |
| 199 | "enter_discovered": func(e *fsm.Event) { |
| 200 | msg := Message{ |
| 201 | Type: OnuDiscIndication, |
| 202 | Data: OnuDiscIndicationMessage{ |
| 203 | Onu: &o, |
| 204 | OperState: UP, |
| 205 | }, |
| 206 | } |
| 207 | o.Channel <- msg |
| 208 | }, |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 209 | "enter_enabled": func(event *fsm.Event) { |
| 210 | msg := Message{ |
| 211 | Type: OnuIndication, |
| 212 | Data: OnuIndicationMessage{ |
| 213 | OnuSN: o.SerialNumber, |
| 214 | PonPortID: o.PonPortID, |
| 215 | OperState: UP, |
| 216 | }, |
| 217 | } |
| 218 | o.Channel <- msg |
| 219 | }, |
| 220 | "enter_disabled": func(event *fsm.Event) { |
Matteo Scandolo | 47ef64b | 2020-04-20 14:16:07 -0700 | [diff] [blame] | 221 | |
| 222 | // clean the ONU state |
| 223 | o.DhcpFlowReceived = false |
Matteo Scandolo | 328c5f0 | 2020-06-26 14:16:39 -0700 | [diff] [blame] | 224 | o.EapolFlowReceived = false |
| 225 | o.GemPortAdded = false |
Matteo Scandolo | 47ef64b | 2020-04-20 14:16:07 -0700 | [diff] [blame] | 226 | o.PortNo = 0 |
| 227 | o.Flows = []FlowKey{} |
| 228 | |
| 229 | // set the OpenState to disabled |
Matteo Scandolo | d7cc6d3 | 2020-02-26 16:51:12 -0800 | [diff] [blame] | 230 | if err := o.OperState.Event("disable"); err != nil { |
| 231 | onuLogger.WithFields(log.Fields{ |
| 232 | "OnuId": o.ID, |
| 233 | "IntfId": o.PonPortID, |
| 234 | "OnuSn": o.Sn(), |
| 235 | }).Errorf("Cannot change ONU OperState to down: %s", err.Error()) |
| 236 | } |
Matteo Scandolo | 47ef64b | 2020-04-20 14:16:07 -0700 | [diff] [blame] | 237 | |
| 238 | // send the OnuIndication DOWN event |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 239 | msg := Message{ |
| 240 | Type: OnuIndication, |
| 241 | Data: OnuIndicationMessage{ |
| 242 | OnuSN: o.SerialNumber, |
| 243 | PonPortID: o.PonPortID, |
| 244 | OperState: DOWN, |
| 245 | }, |
| 246 | } |
| 247 | o.Channel <- msg |
Hardik Windlass | 7b3405b | 2020-07-08 15:10:05 +0530 | [diff] [blame^] | 248 | |
| 249 | // verify all the flows removes are handled and |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 250 | // terminate the ONU's ProcessOnuMessages Go routine |
Hardik Windlass | 7b3405b | 2020-07-08 15:10:05 +0530 | [diff] [blame^] | 251 | if len(o.FlowIds) == 0 { |
| 252 | close(o.Channel) |
| 253 | } |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 254 | }, |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 255 | "before_start_auth": func(e *fsm.Event) { |
| 256 | if o.EapolFlowReceived == false { |
| 257 | e.Cancel(errors.New("cannot-go-to-auth-started-as-eapol-flow-is-missing")) |
| 258 | return |
| 259 | } |
| 260 | if o.GemPortAdded == false { |
| 261 | e.Cancel(errors.New("cannot-go-to-auth-started-as-gemport-is-missing")) |
| 262 | return |
| 263 | } |
| 264 | }, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 265 | "enter_auth_started": func(e *fsm.Event) { |
| 266 | o.logStateChange(e.Src, e.Dst) |
| 267 | msg := Message{ |
| 268 | Type: StartEAPOL, |
| 269 | Data: PacketMessage{ |
| 270 | PonPortID: o.PonPortID, |
| 271 | OnuID: o.ID, |
| 272 | }, |
| 273 | } |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 274 | o.Channel <- msg |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 275 | }, |
Pragya Arya | 324337e | 2020-02-20 14:35:08 +0530 | [diff] [blame] | 276 | "enter_eap_response_success_received": func(e *fsm.Event) { |
| 277 | publishEvent("ONU-authentication-done", int32(o.PonPortID), int32(o.ID), o.Sn()) |
| 278 | }, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 279 | "enter_auth_failed": func(e *fsm.Event) { |
| 280 | onuLogger.WithFields(log.Fields{ |
| 281 | "OnuId": o.ID, |
| 282 | "IntfId": o.PonPortID, |
| 283 | "OnuSn": o.Sn(), |
| 284 | }).Errorf("ONU failed to authenticate!") |
| 285 | }, |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 286 | "before_start_dhcp": func(e *fsm.Event) { |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 287 | |
| 288 | // we allow transition from eanbled to dhcp_started only if auth was set to false |
| 289 | if o.InternalState.Current() == "enabled" && o.Auth { |
| 290 | e.Cancel(errors.New("cannot-go-to-dhcp-started-as-authentication-is-required")) |
| 291 | return |
| 292 | } |
| 293 | |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 294 | if o.DhcpFlowReceived == false { |
| 295 | e.Cancel(errors.New("cannot-go-to-dhcp-started-as-dhcp-flow-is-missing")) |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 296 | return |
| 297 | } |
| 298 | |
| 299 | if o.GemPortAdded == false { |
| 300 | e.Cancel(errors.New("cannot-go-to-dhcp-started-as-gemport-is-missing")) |
| 301 | return |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 302 | } |
| 303 | }, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 304 | "enter_dhcp_started": func(e *fsm.Event) { |
| 305 | msg := Message{ |
| 306 | Type: StartDHCP, |
| 307 | Data: PacketMessage{ |
| 308 | PonPortID: o.PonPortID, |
| 309 | OnuID: o.ID, |
| 310 | }, |
| 311 | } |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 312 | o.Channel <- msg |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 313 | }, |
Pragya Arya | 324337e | 2020-02-20 14:35:08 +0530 | [diff] [blame] | 314 | "enter_dhcp_ack_received": func(e *fsm.Event) { |
| 315 | publishEvent("ONU-DHCP-ACK-received", int32(o.PonPortID), int32(o.ID), o.Sn()) |
| 316 | }, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 317 | "enter_dhcp_failed": func(e *fsm.Event) { |
| 318 | onuLogger.WithFields(log.Fields{ |
| 319 | "OnuId": o.ID, |
| 320 | "IntfId": o.PonPortID, |
| 321 | "OnuSn": o.Sn(), |
| 322 | }).Errorf("ONU failed to DHCP!") |
| 323 | }, |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 324 | "enter_eapol_flow_sent": func(e *fsm.Event) { |
| 325 | msg := Message{ |
| 326 | Type: SendEapolFlow, |
| 327 | } |
| 328 | o.Channel <- msg |
| 329 | }, |
| 330 | "enter_dhcp_flow_sent": func(e *fsm.Event) { |
| 331 | msg := Message{ |
| 332 | Type: SendDhcpFlow, |
| 333 | } |
| 334 | o.Channel <- msg |
| 335 | }, |
Arjun E K | 57a7fcb | 2020-01-30 06:44:45 +0000 | [diff] [blame] | 336 | "igmp_join_start": func(e *fsm.Event) { |
| 337 | msg := Message{ |
| 338 | Type: IGMPMembershipReportV2, |
| 339 | } |
| 340 | o.Channel <- msg |
| 341 | }, |
| 342 | "igmp_leave": func(e *fsm.Event) { |
| 343 | msg := Message{ |
| 344 | Type: IGMPLeaveGroup} |
| 345 | o.Channel <- msg |
| 346 | }, |
Anand S Katti | 0954135 | 2020-01-29 15:54:01 +0530 | [diff] [blame] | 347 | "igmp_join_startv3": func(e *fsm.Event) { |
| 348 | msg := Message{ |
| 349 | Type: IGMPMembershipReportV3, |
| 350 | } |
| 351 | o.Channel <- msg |
| 352 | }, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 353 | }, |
| 354 | ) |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 355 | |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 356 | return &o |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 357 | } |
| 358 | |
William Kurkian | 0418bc8 | 2019-11-06 12:16:24 -0500 | [diff] [blame] | 359 | func (o *Onu) logStateChange(src string, dst string) { |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 360 | onuLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 361 | "OnuId": o.ID, |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 362 | "IntfId": o.PonPortID, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 363 | "OnuSn": o.Sn(), |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 364 | }).Debugf("Changing ONU InternalState from %s to %s", src, dst) |
| 365 | } |
| 366 | |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 367 | // ProcessOnuMessages starts indication channel for each ONU |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 368 | 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] | 369 | onuLogger.WithFields(log.Fields{ |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 370 | "onuID": o.ID, |
| 371 | "onuSN": o.Sn(), |
| 372 | "ponPort": o.PonPortID, |
| 373 | }).Debug("Starting ONU Indication Channel") |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 374 | |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 375 | loop: |
| 376 | for { |
| 377 | select { |
| 378 | case <-ctx.Done(): |
| 379 | onuLogger.WithFields(log.Fields{ |
| 380 | "onuID": o.ID, |
| 381 | "onuSN": o.Sn(), |
| 382 | }).Tracef("ONU message handling canceled via context") |
| 383 | break loop |
| 384 | case message, ok := <-o.Channel: |
| 385 | if !ok || ctx.Err() != nil { |
| 386 | onuLogger.WithFields(log.Fields{ |
| 387 | "onuID": o.ID, |
| 388 | "onuSN": o.Sn(), |
| 389 | }).Tracef("ONU message handling canceled via channel close") |
| 390 | break loop |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 391 | } |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 392 | onuLogger.WithFields(log.Fields{ |
| 393 | "onuID": o.ID, |
| 394 | "onuSN": o.Sn(), |
| 395 | "messageType": message.Type, |
| 396 | }).Tracef("Received message on ONU Channel") |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 397 | |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 398 | switch message.Type { |
| 399 | case OnuDiscIndication: |
| 400 | msg, _ := message.Data.(OnuDiscIndicationMessage) |
| 401 | // 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] | 402 | time.Sleep(o.DiscoveryDelay) |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 403 | o.sendOnuDiscIndication(msg, stream) |
| 404 | case OnuIndication: |
| 405 | msg, _ := message.Data.(OnuIndicationMessage) |
| 406 | o.sendOnuIndication(msg, stream) |
| 407 | case OMCI: |
| 408 | msg, _ := message.Data.(OmciMessage) |
| 409 | o.handleOmciMessage(msg, stream) |
Matteo Scandolo | eb6b5af | 2020-06-24 16:23:58 -0700 | [diff] [blame] | 410 | case FlowAdd: |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 411 | msg, _ := message.Data.(OnuFlowUpdateMessage) |
Matteo Scandolo | eb6b5af | 2020-06-24 16:23:58 -0700 | [diff] [blame] | 412 | o.handleFlowAdd(msg) |
| 413 | case FlowRemoved: |
| 414 | msg, _ := message.Data.(OnuFlowUpdateMessage) |
| 415 | o.handleFlowRemove(msg) |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 416 | case StartEAPOL: |
Shrey Baid | f8abccc | 2020-06-15 19:41:22 +0530 | [diff] [blame] | 417 | o.handleEAPOLStart(stream) |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 418 | case StartDHCP: |
Shrey Baid | f8abccc | 2020-06-15 19:41:22 +0530 | [diff] [blame] | 419 | o.handleDHCPStart(stream) |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 420 | case OnuPacketOut: |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 421 | |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 422 | msg, _ := message.Data.(OnuPacketMessage) |
| 423 | |
| 424 | log.WithFields(log.Fields{ |
| 425 | "IntfId": msg.IntfId, |
| 426 | "OnuId": msg.OnuId, |
| 427 | "pktType": msg.Type, |
| 428 | }).Trace("Received OnuPacketOut Message") |
| 429 | |
| 430 | if msg.Type == packetHandlers.EAPOL { |
| 431 | eapol.HandleNextPacket(msg.OnuId, msg.IntfId, o.Sn(), o.PortNo, o.InternalState, msg.Packet, stream, client) |
| 432 | } else if msg.Type == packetHandlers.DHCP { |
| 433 | // NOTE here we receive packets going from the DHCP Server to the ONU |
| 434 | // for now we expect them to be double-tagged, but ideally the should be single tagged |
Matteo Scandolo | 378b8c9 | 2020-04-16 14:34:22 -0700 | [diff] [blame] | 435 | dhcp.HandleNextPacket(o.PonPort.Olt.ID, o.ID, o.PonPortID, o.Sn(), o.PortNo, o.HwAddress, o.CTag, o.InternalState, msg.Packet, stream) |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 436 | } |
| 437 | case OnuPacketIn: |
| 438 | // NOTE we only receive BBR packets here. |
| 439 | // Eapol.HandleNextPacket can handle both BBSim and BBr cases so the call is the same |
| 440 | // in the DHCP case VOLTHA only act as a proxy, the behaviour is completely different thus we have a dhcp.HandleNextBbrPacket |
| 441 | msg, _ := message.Data.(OnuPacketMessage) |
| 442 | |
| 443 | log.WithFields(log.Fields{ |
| 444 | "IntfId": msg.IntfId, |
| 445 | "OnuId": msg.OnuId, |
| 446 | "pktType": msg.Type, |
| 447 | }).Trace("Received OnuPacketIn Message") |
| 448 | |
| 449 | if msg.Type == packetHandlers.EAPOL { |
| 450 | eapol.HandleNextPacket(msg.OnuId, msg.IntfId, o.Sn(), o.PortNo, o.InternalState, msg.Packet, stream, client) |
| 451 | } else if msg.Type == packetHandlers.DHCP { |
| 452 | dhcp.HandleNextBbrPacket(o.ID, o.PonPortID, o.Sn(), o.STag, o.HwAddress, o.DoneChannel, msg.Packet, client) |
| 453 | } |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 454 | case OmciIndication: |
| 455 | msg, _ := message.Data.(OmciIndicationMessage) |
| 456 | o.handleOmci(msg, client) |
| 457 | case SendEapolFlow: |
| 458 | o.sendEapolFlow(client) |
| 459 | case SendDhcpFlow: |
| 460 | o.sendDhcpFlow(client) |
Arjun E K | 57a7fcb | 2020-01-30 06:44:45 +0000 | [diff] [blame] | 461 | case IGMPMembershipReportV2: |
| 462 | log.Infof("Recieved IGMPMembershipReportV2 message on ONU channel") |
| 463 | igmp.SendIGMPMembershipReportV2(o.PonPortID, o.ID, o.Sn(), o.PortNo, o.HwAddress, stream) |
| 464 | case IGMPLeaveGroup: |
| 465 | log.Infof("Recieved IGMPLeaveGroupV2 message on ONU channel") |
| 466 | igmp.SendIGMPLeaveGroupV2(o.PonPortID, o.ID, o.Sn(), o.PortNo, o.HwAddress, stream) |
Anand S Katti | 0954135 | 2020-01-29 15:54:01 +0530 | [diff] [blame] | 467 | case IGMPMembershipReportV3: |
| 468 | log.Infof("Recieved IGMPMembershipReportV3 message on ONU channel") |
| 469 | igmp.SendIGMPMembershipReportV3(o.PonPortID, o.ID, o.Sn(), o.PortNo, o.HwAddress, stream) |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 470 | default: |
| 471 | 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] | 472 | } |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 473 | } |
| 474 | } |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 475 | onuLogger.WithFields(log.Fields{ |
| 476 | "onuID": o.ID, |
| 477 | "onuSN": o.Sn(), |
| 478 | }).Debug("Stopped handling ONU Indication Channel") |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 479 | } |
| 480 | |
Matteo Scandolo | df3f85d | 2020-01-15 12:50:48 -0800 | [diff] [blame] | 481 | func (o *Onu) processOmciMessage(message omcisim.OmciChMessage, stream openolt.Openolt_EnableIndicationServer) { |
William Kurkian | 9dadc5b | 2019-10-22 13:51:57 -0400 | [diff] [blame] | 482 | switch message.Type { |
Matteo Scandolo | df3f85d | 2020-01-15 12:50:48 -0800 | [diff] [blame] | 483 | case omcisim.UniLinkUp, omcisim.UniLinkDown: |
| 484 | onuLogger.WithFields(log.Fields{ |
| 485 | "OnuId": message.Data.OnuId, |
| 486 | "IntfId": message.Data.IntfId, |
| 487 | "Type": message.Type, |
| 488 | }).Infof("UNI Link Alarm") |
| 489 | // TODO send to OLT |
| 490 | |
| 491 | omciInd := openolt.OmciIndication{ |
| 492 | IntfId: message.Data.IntfId, |
| 493 | OnuId: message.Data.OnuId, |
| 494 | Pkt: message.Packet, |
| 495 | } |
| 496 | |
| 497 | omci := &openolt.Indication_OmciInd{OmciInd: &omciInd} |
| 498 | if err := stream.Send(&openolt.Indication{Data: omci}); err != nil { |
| 499 | onuLogger.WithFields(log.Fields{ |
| 500 | "IntfId": o.PonPortID, |
| 501 | "SerialNumber": o.Sn(), |
| 502 | "Type": message.Type, |
| 503 | "omciPacket": omciInd.Pkt, |
| 504 | }).Errorf("Failed to send UNI Link Alarm: %v", err) |
| 505 | return |
| 506 | } |
| 507 | |
| 508 | onuLogger.WithFields(log.Fields{ |
| 509 | "IntfId": o.PonPortID, |
| 510 | "SerialNumber": o.Sn(), |
| 511 | "Type": message.Type, |
| 512 | "omciPacket": omciInd.Pkt, |
| 513 | }).Info("UNI Link alarm sent") |
| 514 | |
William Kurkian | 9dadc5b | 2019-10-22 13:51:57 -0400 | [diff] [blame] | 515 | case omcisim.GemPortAdded: |
| 516 | log.WithFields(log.Fields{ |
| 517 | "OnuId": message.Data.OnuId, |
| 518 | "IntfId": message.Data.IntfId, |
Matteo Scandolo | d74abba | 2020-04-16 16:36:44 -0700 | [diff] [blame] | 519 | "OnuSn": o.Sn(), |
William Kurkian | 9dadc5b | 2019-10-22 13:51:57 -0400 | [diff] [blame] | 520 | }).Infof("GemPort Added") |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 521 | |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 522 | o.GemPortAdded = true |
| 523 | |
| 524 | // broadcast the change to all listeners |
| 525 | // and close the channels as once the GemPort is set |
| 526 | // it won't change anymore |
| 527 | for _, ch := range o.GemPortChannels { |
| 528 | ch <- true |
| 529 | close(ch) |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 530 | } |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 531 | o.GemPortChannels = []chan bool{} |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 532 | } |
| 533 | } |
| 534 | |
Shrey Baid | f8abccc | 2020-06-15 19:41:22 +0530 | [diff] [blame] | 535 | func (o *Onu) handleEAPOLStart(stream openolt.Openolt_EnableIndicationServer) { |
| 536 | log.Infof("Receive StartEAPOL message on ONU Channel") |
| 537 | eapol.SendEapStart(o.ID, o.PonPortID, o.Sn(), o.PortNo, o.HwAddress, o.InternalState, stream) |
| 538 | go func(delay time.Duration) { |
| 539 | time.Sleep(delay) |
| 540 | if (o.InternalState.Current() == "eap_start_sent" || |
| 541 | o.InternalState.Current() == "eap_response_identity_sent" || |
| 542 | o.InternalState.Current() == "eap_response_challenge_sent" || |
| 543 | o.InternalState.Current() == "auth_failed") && common.Options.BBSim.AuthRetry { |
| 544 | o.InternalState.Event("start_auth") |
| 545 | } else if o.InternalState.Current() == "eap_response_success_received" { |
| 546 | o.Backoff.Reset() |
| 547 | } |
| 548 | }(o.Backoff.Duration()) |
| 549 | } |
| 550 | |
| 551 | func (o *Onu) handleDHCPStart(stream openolt.Openolt_EnableIndicationServer) { |
| 552 | log.Infof("Receive StartDHCP message on ONU Channel") |
| 553 | // FIXME use id, ponId as SendEapStart |
| 554 | dhcp.SendDHCPDiscovery(o.PonPort.Olt.ID, o.PonPortID, o.ID, o.Sn(), o.PortNo, o.InternalState, o.HwAddress, o.CTag, stream) |
| 555 | go func(delay time.Duration) { |
| 556 | time.Sleep(delay) |
| 557 | if (o.InternalState.Current() == "dhcp_discovery_sent" || |
| 558 | o.InternalState.Current() == "dhcp_request_sent" || |
| 559 | o.InternalState.Current() == "dhcp_failed") && common.Options.BBSim.DhcpRetry { |
| 560 | o.InternalState.Event("start_dhcp") |
| 561 | } else if o.InternalState.Current() == "dhcp_ack_received" { |
| 562 | o.Backoff.Reset() |
| 563 | } |
| 564 | }(o.Backoff.Duration()) |
| 565 | } |
| 566 | |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 567 | func (o Onu) NewSN(oltid int, intfid uint32, onuid uint32) *openolt.SerialNumber { |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 568 | |
| 569 | sn := new(openolt.SerialNumber) |
| 570 | |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 571 | //sn = new(openolt.SerialNumber) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 572 | sn.VendorId = []byte("BBSM") |
| 573 | sn.VendorSpecific = []byte{0, byte(oltid % 256), byte(intfid), byte(onuid)} |
| 574 | |
| 575 | return sn |
| 576 | } |
| 577 | |
William Kurkian | 0418bc8 | 2019-11-06 12:16:24 -0500 | [diff] [blame] | 578 | func (o *Onu) sendOnuDiscIndication(msg OnuDiscIndicationMessage, stream openolt.Openolt_EnableIndicationServer) { |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 579 | discoverData := &openolt.Indication_OnuDiscInd{OnuDiscInd: &openolt.OnuDiscIndication{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 580 | IntfId: msg.Onu.PonPortID, |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 581 | SerialNumber: msg.Onu.SerialNumber, |
| 582 | }} |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 583 | |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 584 | if err := stream.Send(&openolt.Indication{Data: discoverData}); err != nil { |
Matteo Scandolo | 1100699 | 2019-08-28 11:29:46 -0700 | [diff] [blame] | 585 | log.Errorf("Failed to send Indication_OnuDiscInd: %v", err) |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 586 | return |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 587 | } |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 588 | |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 589 | onuLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 590 | "IntfId": msg.Onu.PonPortID, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 591 | "OnuSn": msg.Onu.Sn(), |
| 592 | "OnuId": o.ID, |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 593 | }).Debug("Sent Indication_OnuDiscInd") |
Pragya Arya | 324337e | 2020-02-20 14:35:08 +0530 | [diff] [blame] | 594 | publishEvent("ONU-discovery-indication-sent", int32(msg.Onu.PonPortID), int32(o.ID), msg.Onu.Sn()) |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 595 | |
| 596 | // after DiscoveryRetryDelay check if the state is the same and in case send a new OnuDiscIndication |
| 597 | go func(delay time.Duration) { |
Matteo Scandolo | 569e717 | 2019-12-20 11:51:51 -0800 | [diff] [blame] | 598 | time.Sleep(delay) |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 599 | if o.InternalState.Current() == "discovered" { |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 600 | o.sendOnuDiscIndication(msg, stream) |
| 601 | } |
| 602 | }(o.DiscoveryRetryDelay) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 603 | } |
| 604 | |
William Kurkian | 0418bc8 | 2019-11-06 12:16:24 -0500 | [diff] [blame] | 605 | func (o *Onu) sendOnuIndication(msg OnuIndicationMessage, stream openolt.Openolt_EnableIndicationServer) { |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 606 | // NOTE voltha returns an ID, but if we use that ID then it complains: |
| 607 | // expected_onu_id: 1, received_onu_id: 1024, event: ONU-id-mismatch, can happen if both voltha and the olt rebooted |
| 608 | // so we're using the internal ID that is 1 |
| 609 | // o.ID = msg.OnuID |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 610 | |
| 611 | indData := &openolt.Indication_OnuInd{OnuInd: &openolt.OnuIndication{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 612 | IntfId: o.PonPortID, |
| 613 | OnuId: o.ID, |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 614 | OperState: msg.OperState.String(), |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 615 | AdminState: o.OperState.Current(), |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 616 | SerialNumber: o.SerialNumber, |
| 617 | }} |
| 618 | if err := stream.Send(&openolt.Indication{Data: indData}); err != nil { |
Matteo Scandolo | d7cc6d3 | 2020-02-26 16:51:12 -0800 | [diff] [blame] | 619 | // NOTE do we need to transition to a broken state? |
Matteo Scandolo | 1100699 | 2019-08-28 11:29:46 -0700 | [diff] [blame] | 620 | log.Errorf("Failed to send Indication_OnuInd: %v", err) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 621 | } |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 622 | onuLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 623 | "IntfId": o.PonPortID, |
| 624 | "OnuId": o.ID, |
| 625 | "OperState": msg.OperState.String(), |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 626 | "AdminState": msg.OperState.String(), |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 627 | "OnuSn": o.Sn(), |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 628 | }).Debug("Sent Indication_OnuInd") |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 629 | |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 630 | } |
| 631 | |
Pragya Arya | 324337e | 2020-02-20 14:35:08 +0530 | [diff] [blame] | 632 | func (o *Onu) publishOmciEvent(msg OmciMessage) { |
| 633 | if olt.PublishEvents { |
| 634 | _, _, msgType, _, _, _, err := omcisim.ParsePkt(HexDecode(msg.omciMsg.Pkt)) |
| 635 | if err != nil { |
| 636 | log.Errorf("error in getting msgType %v", err) |
| 637 | return |
| 638 | } |
| 639 | if msgType == omcisim.MibUpload { |
| 640 | o.seqNumber = 0 |
| 641 | publishEvent("MIB-upload-received", int32(o.PonPortID), int32(o.ID), common.OnuSnToString(o.SerialNumber)) |
| 642 | } else if msgType == omcisim.MibUploadNext { |
| 643 | o.seqNumber++ |
| 644 | if o.seqNumber > 290 { |
| 645 | publishEvent("MIB-upload-done", int32(o.PonPortID), int32(o.ID), common.OnuSnToString(o.SerialNumber)) |
| 646 | } |
| 647 | } |
| 648 | } |
| 649 | } |
| 650 | |
Scott Baker | b90c431 | 2020-03-12 21:33:25 -0700 | [diff] [blame] | 651 | // Create a TestResponse packet and send it |
| 652 | func (o *Onu) sendTestResult(msg OmciMessage, stream openolt.Openolt_EnableIndicationServer) error { |
| 653 | resp, err := omcilib.BuildTestResult(HexDecode(msg.omciMsg.Pkt)) |
| 654 | if err != nil { |
| 655 | return err |
| 656 | } |
| 657 | |
| 658 | var omciInd openolt.OmciIndication |
| 659 | omciInd.IntfId = o.PonPortID |
| 660 | omciInd.OnuId = o.ID |
| 661 | omciInd.Pkt = resp |
| 662 | |
| 663 | omci := &openolt.Indication_OmciInd{OmciInd: &omciInd} |
| 664 | if err := stream.Send(&openolt.Indication{Data: omci}); err != nil { |
| 665 | onuLogger.WithFields(log.Fields{ |
| 666 | "IntfId": o.PonPortID, |
| 667 | "SerialNumber": o.Sn(), |
| 668 | "omciPacket": omciInd.Pkt, |
| 669 | "msg": msg, |
| 670 | }).Errorf("send TestResult omcisim indication failed: %v", err) |
| 671 | return err |
| 672 | } |
| 673 | onuLogger.WithFields(log.Fields{ |
| 674 | "IntfId": o.PonPortID, |
| 675 | "SerialNumber": o.Sn(), |
| 676 | "omciPacket": omciInd.Pkt, |
| 677 | }).Tracef("Sent TestResult OMCI message") |
| 678 | |
| 679 | return nil |
| 680 | } |
| 681 | |
William Kurkian | 0418bc8 | 2019-11-06 12:16:24 -0500 | [diff] [blame] | 682 | func (o *Onu) handleOmciMessage(msg OmciMessage, stream openolt.Openolt_EnableIndicationServer) { |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 683 | |
| 684 | onuLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 685 | "IntfId": o.PonPortID, |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 686 | "SerialNumber": o.Sn(), |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 687 | "omciPacket": msg.omciMsg.Pkt, |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 688 | }).Tracef("Received OMCI message") |
| 689 | |
Pragya Arya | 324337e | 2020-02-20 14:35:08 +0530 | [diff] [blame] | 690 | o.publishOmciEvent(msg) |
| 691 | |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 692 | var omciInd openolt.OmciIndication |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 693 | respPkt, err := omcisim.OmciSim(o.PonPortID, o.ID, HexDecode(msg.omciMsg.Pkt)) |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 694 | if err != nil { |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 695 | onuLogger.WithFields(log.Fields{ |
| 696 | "IntfId": o.PonPortID, |
| 697 | "SerialNumber": o.Sn(), |
| 698 | "omciPacket": omciInd.Pkt, |
| 699 | "msg": msg, |
| 700 | }).Errorf("Error handling OMCI message %v", msg) |
| 701 | return |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | omciInd.IntfId = o.PonPortID |
| 705 | omciInd.OnuId = o.ID |
| 706 | omciInd.Pkt = respPkt |
| 707 | |
| 708 | omci := &openolt.Indication_OmciInd{OmciInd: &omciInd} |
| 709 | if err := stream.Send(&openolt.Indication{Data: omci}); err != nil { |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 710 | onuLogger.WithFields(log.Fields{ |
| 711 | "IntfId": o.PonPortID, |
| 712 | "SerialNumber": o.Sn(), |
| 713 | "omciPacket": omciInd.Pkt, |
| 714 | "msg": msg, |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 715 | }).Errorf("send omcisim indication failed: %v", err) |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 716 | return |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 717 | } |
| 718 | onuLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 719 | "IntfId": o.PonPortID, |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 720 | "SerialNumber": o.Sn(), |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 721 | "omciPacket": omciInd.Pkt, |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 722 | }).Tracef("Sent OMCI message") |
Scott Baker | b90c431 | 2020-03-12 21:33:25 -0700 | [diff] [blame] | 723 | |
| 724 | // Test message is special, it requires sending two packets: |
| 725 | // first packet: TestResponse, says whether test was started successully, handled by omci-sim |
| 726 | // second packet, TestResult, reports the result of running the self-test |
| 727 | // TestResult can come some time after a TestResponse |
| 728 | // TODO: Implement some delay between the TestResponse and the TestResult |
| 729 | isTest, err := omcilib.IsTestRequest(HexDecode(msg.omciMsg.Pkt)) |
| 730 | if (err == nil) && (isTest) { |
| 731 | o.sendTestResult(msg, stream) |
| 732 | } |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 733 | } |
| 734 | |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 735 | func (o *Onu) storePortNumber(portNo uint32) { |
Matteo Scandolo | 813402b | 2019-10-23 19:24:52 -0700 | [diff] [blame] | 736 | // 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] | 737 | // we need to add support for multiple UNIs |
| 738 | // the action plan is: |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 739 | // - 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] | 740 | // - change the library so that it reports a single UNI and remove this workaroung |
| 741 | // - add support for multiple UNIs in BBSim |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 742 | if o.PortNo == 0 || portNo < o.PortNo { |
Matteo Scandolo | 813402b | 2019-10-23 19:24:52 -0700 | [diff] [blame] | 743 | onuLogger.WithFields(log.Fields{ |
| 744 | "IntfId": o.PonPortID, |
| 745 | "OnuId": o.ID, |
| 746 | "SerialNumber": o.Sn(), |
| 747 | "OnuPortNo": o.PortNo, |
| 748 | "FlowPortNo": portNo, |
| 749 | }).Debug("Storing ONU portNo") |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 750 | o.PortNo = portNo |
| 751 | } |
| 752 | } |
| 753 | |
William Kurkian | 0418bc8 | 2019-11-06 12:16:24 -0500 | [diff] [blame] | 754 | func (o *Onu) SetID(id uint32) { |
Matteo Scandolo | 583f17d | 2020-02-13 10:35:17 -0800 | [diff] [blame] | 755 | onuLogger.WithFields(log.Fields{ |
| 756 | "IntfId": o.PonPortID, |
| 757 | "OnuId": id, |
| 758 | "SerialNumber": o.Sn(), |
| 759 | }).Debug("Storing OnuId ") |
William Kurkian | 0418bc8 | 2019-11-06 12:16:24 -0500 | [diff] [blame] | 760 | o.ID = id |
| 761 | } |
| 762 | |
Matteo Scandolo | eb6b5af | 2020-06-24 16:23:58 -0700 | [diff] [blame] | 763 | func (o *Onu) handleFlowAdd(msg OnuFlowUpdateMessage) { |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 764 | onuLogger.WithFields(log.Fields{ |
Matteo Scandolo | d7cc6d3 | 2020-02-26 16:51:12 -0800 | [diff] [blame] | 765 | "DstPort": msg.Flow.Classifier.DstPort, |
| 766 | "EthType": fmt.Sprintf("%x", msg.Flow.Classifier.EthType), |
| 767 | "FlowId": msg.Flow.FlowId, |
| 768 | "FlowType": msg.Flow.FlowType, |
| 769 | "GemportId": msg.Flow.GemportId, |
| 770 | "InnerVlan": msg.Flow.Classifier.IVid, |
| 771 | "IntfId": msg.Flow.AccessIntfId, |
| 772 | "IpProto": msg.Flow.Classifier.IpProto, |
| 773 | "OnuId": msg.Flow.OnuId, |
| 774 | "OnuSn": o.Sn(), |
| 775 | "OuterVlan": msg.Flow.Classifier.OVid, |
| 776 | "PortNo": msg.Flow.PortNo, |
| 777 | "SrcPort": msg.Flow.Classifier.SrcPort, |
| 778 | "UniID": msg.Flow.UniId, |
Matteo Scandolo | 3ac5279 | 2020-03-05 14:21:08 -0800 | [diff] [blame] | 779 | "ClassifierOPbits": msg.Flow.Classifier.OPbits, |
Matteo Scandolo | eb6b5af | 2020-06-24 16:23:58 -0700 | [diff] [blame] | 780 | }).Debug("ONU receives FlowAdd") |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 781 | |
Matteo Scandolo | 813402b | 2019-10-23 19:24:52 -0700 | [diff] [blame] | 782 | if msg.Flow.UniId != 0 { |
| 783 | // as of now BBSim only support a single UNI, so ignore everything that is not targeted to it |
| 784 | onuLogger.WithFields(log.Fields{ |
| 785 | "IntfId": o.PonPortID, |
| 786 | "OnuId": o.ID, |
| 787 | "SerialNumber": o.Sn(), |
| 788 | }).Debug("Ignoring flow as it's not for the first UNI") |
| 789 | return |
| 790 | } |
| 791 | |
Matteo Scandolo | eb6b5af | 2020-06-24 16:23:58 -0700 | [diff] [blame] | 792 | o.FlowIds = append(o.FlowIds, msg.Flow.FlowId) |
| 793 | |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 794 | 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] | 795 | // NOTE storing the PortNO, it's needed when sending PacketIndications |
Matteo Scandolo | 813402b | 2019-10-23 19:24:52 -0700 | [diff] [blame] | 796 | o.storePortNumber(uint32(msg.Flow.PortNo)) |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 797 | o.EapolFlowReceived = true |
| 798 | // if authentication is not enabled, do nothing |
| 799 | if o.Auth { |
| 800 | // NOTE if we receive the EAPOL flows but we don't have GemPorts |
| 801 | // wait for it before starting auth |
| 802 | if !o.GemPortAdded { |
| 803 | // wait for Gem and then start auth |
| 804 | go func() { |
| 805 | for v := range o.GetGemPortChan() { |
| 806 | if v == true { |
| 807 | if err := o.InternalState.Event("start_auth"); err != nil { |
| 808 | onuLogger.Warnf("Can't go to auth_started: %v", err) |
| 809 | } |
| 810 | } |
| 811 | } |
| 812 | onuLogger.Trace("GemPortChannel closed") |
| 813 | }() |
Matteo Scandolo | c114709 | 2019-10-29 09:38:33 -0700 | [diff] [blame] | 814 | } else { |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 815 | // start the EAPOL state machine |
| 816 | if err := o.InternalState.Event("start_auth"); err != nil { |
| 817 | onuLogger.Warnf("Can't go to auth_started: %v", err) |
| 818 | } |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 819 | } |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 820 | } else { |
| 821 | onuLogger.WithFields(log.Fields{ |
| 822 | "IntfId": o.PonPortID, |
| 823 | "OnuId": o.ID, |
| 824 | "SerialNumber": o.Sn(), |
| 825 | }).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] | 826 | } |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 827 | } else if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeIPv4) && |
| 828 | msg.Flow.Classifier.SrcPort == uint32(68) && |
Matteo Scandolo | 3ac5279 | 2020-03-05 14:21:08 -0800 | [diff] [blame] | 829 | msg.Flow.Classifier.DstPort == uint32(67) && |
Matteo Scandolo | d74abba | 2020-04-16 16:36:44 -0700 | [diff] [blame] | 830 | (msg.Flow.Classifier.OPbits == 0 || msg.Flow.Classifier.OPbits == 255) { |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 831 | |
Matteo Scandolo | c114709 | 2019-10-29 09:38:33 -0700 | [diff] [blame] | 832 | if o.Dhcp == true { |
Matteo Scandolo | d74abba | 2020-04-16 16:36:44 -0700 | [diff] [blame] | 833 | if o.DhcpFlowReceived == false { |
| 834 | // keep track that we received the DHCP Flows |
| 835 | // so that we can transition the state to dhcp_started |
| 836 | // this is needed as a check in case someone trigger DHCP from the CLI |
| 837 | o.DhcpFlowReceived = true |
| 838 | |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 839 | if !o.GemPortAdded { |
| 840 | // wait for Gem and then start DHCP |
| 841 | go func() { |
| 842 | for v := range o.GetGemPortChan() { |
| 843 | if v == true { |
| 844 | if err := o.InternalState.Event("start_dhcp"); err != nil { |
| 845 | log.Errorf("Can't go to dhcp_started: %v", err) |
| 846 | } |
| 847 | } |
| 848 | } |
| 849 | }() |
| 850 | } else { |
| 851 | // start the DHCP state machine |
| 852 | if err := o.InternalState.Event("start_dhcp"); err != nil { |
| 853 | log.Errorf("Can't go to dhcp_started: %v", err) |
| 854 | } |
Matteo Scandolo | d74abba | 2020-04-16 16:36:44 -0700 | [diff] [blame] | 855 | } |
| 856 | } else { |
| 857 | onuLogger.WithFields(log.Fields{ |
| 858 | "IntfId": o.PonPortID, |
| 859 | "OnuId": o.ID, |
| 860 | "SerialNumber": o.Sn(), |
| 861 | "DhcpFlowReceived": o.DhcpFlowReceived, |
| 862 | }).Warn("DHCP already started") |
Matteo Scandolo | c114709 | 2019-10-29 09:38:33 -0700 | [diff] [blame] | 863 | } |
| 864 | } else { |
| 865 | onuLogger.WithFields(log.Fields{ |
| 866 | "IntfId": o.PonPortID, |
| 867 | "OnuId": o.ID, |
| 868 | "SerialNumber": o.Sn(), |
| 869 | }).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] | 870 | } |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 871 | } |
| 872 | } |
| 873 | |
Matteo Scandolo | eb6b5af | 2020-06-24 16:23:58 -0700 | [diff] [blame] | 874 | func (o *Onu) handleFlowRemove(msg OnuFlowUpdateMessage) { |
| 875 | onuLogger.WithFields(log.Fields{ |
| 876 | "IntfId": o.PonPortID, |
| 877 | "OnuId": o.ID, |
| 878 | "SerialNumber": o.Sn(), |
| 879 | "FlowId": msg.Flow.FlowId, |
| 880 | "FlowType": msg.Flow.FlowType, |
| 881 | }).Debug("ONU receives FlowRemove") |
| 882 | |
| 883 | for idx, flow := range o.FlowIds { |
| 884 | // If the gemport is found, delete it from local cache. |
| 885 | if flow == msg.Flow.FlowId { |
| 886 | o.FlowIds = append(o.FlowIds[:idx], o.FlowIds[idx+1:]...) |
| 887 | break |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | if len(o.FlowIds) == 0 { |
| 892 | onuLogger.WithFields(log.Fields{ |
| 893 | "IntfId": o.PonPortID, |
| 894 | "OnuId": o.ID, |
| 895 | "SerialNumber": o.Sn(), |
| 896 | }).Info("Resetting GemPort") |
| 897 | o.GemPortAdded = false |
| 898 | |
| 899 | // TODO ideally we should keep track of the flow type (and not only the ID) |
| 900 | // so that we can properly set these two flag when the flow is removed |
| 901 | o.EapolFlowReceived = false |
| 902 | o.DhcpFlowReceived = false |
Hardik Windlass | 7b3405b | 2020-07-08 15:10:05 +0530 | [diff] [blame^] | 903 | |
| 904 | // check if ONU delete is performed and |
| 905 | // terminate the ONU's ProcessOnuMessages Go routine |
| 906 | if o.InternalState.Current() == "disabled" { |
| 907 | close(o.Channel) |
| 908 | } |
Matteo Scandolo | eb6b5af | 2020-06-24 16:23:58 -0700 | [diff] [blame] | 909 | } |
| 910 | } |
| 911 | |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 912 | // HexDecode converts the hex encoding to binary |
| 913 | func HexDecode(pkt []byte) []byte { |
| 914 | p := make([]byte, len(pkt)/2) |
| 915 | for i, j := 0, 0; i < len(pkt); i, j = i+2, j+1 { |
| 916 | // Go figure this ;) |
| 917 | u := (pkt[i] & 15) + (pkt[i]>>6)*9 |
| 918 | l := (pkt[i+1] & 15) + (pkt[i+1]>>6)*9 |
| 919 | p[j] = u<<4 + l |
| 920 | } |
| 921 | onuLogger.Tracef("Omci decoded: %x.", p) |
| 922 | return p |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 923 | } |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 924 | |
| 925 | // BBR methods |
| 926 | |
| 927 | func sendOmciMsg(pktBytes []byte, intfId uint32, onuId uint32, sn *openolt.SerialNumber, msgType string, client openolt.OpenoltClient) { |
| 928 | omciMsg := openolt.OmciMsg{ |
| 929 | IntfId: intfId, |
| 930 | OnuId: onuId, |
| 931 | Pkt: pktBytes, |
| 932 | } |
| 933 | |
| 934 | if _, err := client.OmciMsgOut(context.Background(), &omciMsg); err != nil { |
| 935 | log.WithFields(log.Fields{ |
| 936 | "IntfId": intfId, |
| 937 | "OnuId": onuId, |
| 938 | "SerialNumber": common.OnuSnToString(sn), |
| 939 | "Pkt": omciMsg.Pkt, |
| 940 | }).Fatalf("Failed to send MIB Reset") |
| 941 | } |
| 942 | log.WithFields(log.Fields{ |
| 943 | "IntfId": intfId, |
| 944 | "OnuId": onuId, |
| 945 | "SerialNumber": common.OnuSnToString(sn), |
| 946 | "Pkt": omciMsg.Pkt, |
| 947 | }).Tracef("Sent OMCI message %s", msgType) |
| 948 | } |
| 949 | |
| 950 | func (onu *Onu) getNextTid(highPriority ...bool) uint16 { |
| 951 | var next uint16 |
| 952 | if len(highPriority) > 0 && highPriority[0] { |
| 953 | next = onu.hpTid |
| 954 | onu.hpTid += 1 |
| 955 | if onu.hpTid < 0x8000 { |
| 956 | onu.hpTid = 0x8000 |
| 957 | } |
| 958 | } else { |
| 959 | next = onu.tid |
| 960 | onu.tid += 1 |
| 961 | if onu.tid >= 0x8000 { |
| 962 | onu.tid = 1 |
| 963 | } |
| 964 | } |
| 965 | return next |
| 966 | } |
| 967 | |
| 968 | // TODO move this method in responders/omcisim |
| 969 | func (o *Onu) StartOmci(client openolt.OpenoltClient) { |
| 970 | mibReset, _ := omcilib.CreateMibResetRequest(o.getNextTid(false)) |
| 971 | sendOmciMsg(mibReset, o.PonPortID, o.ID, o.SerialNumber, "mibReset", client) |
| 972 | } |
| 973 | |
| 974 | func (o *Onu) handleOmci(msg OmciIndicationMessage, client openolt.OpenoltClient) { |
| 975 | msgType, packet := omcilib.DecodeOmci(msg.OmciInd.Pkt) |
| 976 | |
| 977 | log.WithFields(log.Fields{ |
| 978 | "IntfId": msg.OmciInd.IntfId, |
| 979 | "OnuId": msg.OmciInd.OnuId, |
| 980 | "OnuSn": common.OnuSnToString(o.SerialNumber), |
| 981 | "Pkt": msg.OmciInd.Pkt, |
| 982 | "msgType": msgType, |
Anand S Katti | 0954135 | 2020-01-29 15:54:01 +0530 | [diff] [blame] | 983 | }).Trace("ONU Receives OMCI Msg") |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 984 | switch msgType { |
| 985 | default: |
Matteo Scandolo | 813402b | 2019-10-23 19:24:52 -0700 | [diff] [blame] | 986 | log.WithFields(log.Fields{ |
| 987 | "IntfId": msg.OmciInd.IntfId, |
| 988 | "OnuId": msg.OmciInd.OnuId, |
| 989 | "OnuSn": common.OnuSnToString(o.SerialNumber), |
| 990 | "Pkt": msg.OmciInd.Pkt, |
| 991 | "msgType": msgType, |
| 992 | }).Fatalf("unexpected frame: %v", packet) |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 993 | case omci.MibResetResponseType: |
| 994 | mibUpload, _ := omcilib.CreateMibUploadRequest(o.getNextTid(false)) |
| 995 | sendOmciMsg(mibUpload, o.PonPortID, o.ID, o.SerialNumber, "mibUpload", client) |
| 996 | case omci.MibUploadResponseType: |
| 997 | mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber) |
| 998 | sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client) |
| 999 | case omci.MibUploadNextResponseType: |
| 1000 | o.seqNumber++ |
| 1001 | |
| 1002 | if o.seqNumber > 290 { |
| 1003 | // NOTE we are done with the MIB Upload (290 is the number of messages the omci-sim library will respond to) |
| 1004 | galEnet, _ := omcilib.CreateGalEnetRequest(o.getNextTid(false)) |
| 1005 | sendOmciMsg(galEnet, o.PonPortID, o.ID, o.SerialNumber, "CreateGalEnetRequest", client) |
| 1006 | } else { |
| 1007 | mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber) |
| 1008 | sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client) |
| 1009 | } |
| 1010 | case omci.CreateResponseType: |
| 1011 | // NOTE Creating a GemPort, |
| 1012 | // BBsim actually doesn't care about the values, so we can do we want with the parameters |
| 1013 | // In the same way we can create a GemPort even without setting up UNIs/TConts/... |
| 1014 | // but we need the GemPort to trigger the state change |
| 1015 | |
Matteo Scandolo | eb6b5af | 2020-06-24 16:23:58 -0700 | [diff] [blame] | 1016 | if !o.GemPortAdded { |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 1017 | // NOTE this sends a CreateRequestType and BBSim replies with a CreateResponseType |
| 1018 | // thus we send this request only once |
| 1019 | gemReq, _ := omcilib.CreateGemPortRequest(o.getNextTid(false)) |
| 1020 | sendOmciMsg(gemReq, o.PonPortID, o.ID, o.SerialNumber, "CreateGemPortRequest", client) |
Matteo Scandolo | eb6b5af | 2020-06-24 16:23:58 -0700 | [diff] [blame] | 1021 | o.GemPortAdded = true |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 1022 | } else { |
| 1023 | if err := o.InternalState.Event("send_eapol_flow"); err != nil { |
| 1024 | onuLogger.WithFields(log.Fields{ |
| 1025 | "OnuId": o.ID, |
| 1026 | "IntfId": o.PonPortID, |
| 1027 | "OnuSn": o.Sn(), |
| 1028 | }).Errorf("Error while transitioning ONU State %v", err) |
| 1029 | } |
| 1030 | } |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 1031 | } |
| 1032 | } |
| 1033 | |
| 1034 | func (o *Onu) sendEapolFlow(client openolt.OpenoltClient) { |
| 1035 | |
| 1036 | classifierProto := openolt.Classifier{ |
| 1037 | EthType: uint32(layers.EthernetTypeEAPOL), |
| 1038 | OVid: 4091, |
| 1039 | } |
| 1040 | |
| 1041 | actionProto := openolt.Action{} |
| 1042 | |
| 1043 | downstreamFlow := openolt.Flow{ |
| 1044 | AccessIntfId: int32(o.PonPortID), |
| 1045 | OnuId: int32(o.ID), |
Matteo Scandolo | 813402b | 2019-10-23 19:24:52 -0700 | [diff] [blame] | 1046 | 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] | 1047 | FlowId: uint32(o.ID), |
| 1048 | FlowType: "downstream", |
| 1049 | AllocId: int32(0), |
| 1050 | NetworkIntfId: int32(0), |
| 1051 | GemportId: int32(1), // FIXME use the same value as CreateGemPortRequest PortID, do not hardcode |
| 1052 | Classifier: &classifierProto, |
| 1053 | Action: &actionProto, |
| 1054 | Priority: int32(100), |
| 1055 | Cookie: uint64(o.ID), |
| 1056 | PortNo: uint32(o.ID), // NOTE we are using this to map an incoming packetIndication to an ONU |
| 1057 | } |
| 1058 | |
| 1059 | if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil { |
| 1060 | log.WithFields(log.Fields{ |
| 1061 | "IntfId": o.PonPortID, |
| 1062 | "OnuId": o.ID, |
| 1063 | "FlowId": downstreamFlow.FlowId, |
| 1064 | "PortNo": downstreamFlow.PortNo, |
| 1065 | "SerialNumber": common.OnuSnToString(o.SerialNumber), |
Matteo Scandolo | b0e3e62 | 2020-04-23 17:00:29 -0700 | [diff] [blame] | 1066 | }).Fatalf("Failed to add EAPOL Flow") |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 1067 | } |
| 1068 | log.WithFields(log.Fields{ |
| 1069 | "IntfId": o.PonPortID, |
| 1070 | "OnuId": o.ID, |
| 1071 | "FlowId": downstreamFlow.FlowId, |
| 1072 | "PortNo": downstreamFlow.PortNo, |
| 1073 | "SerialNumber": common.OnuSnToString(o.SerialNumber), |
| 1074 | }).Info("Sent EAPOL Flow") |
| 1075 | } |
| 1076 | |
| 1077 | func (o *Onu) sendDhcpFlow(client openolt.OpenoltClient) { |
| 1078 | classifierProto := openolt.Classifier{ |
| 1079 | EthType: uint32(layers.EthernetTypeIPv4), |
| 1080 | SrcPort: uint32(68), |
| 1081 | DstPort: uint32(67), |
| 1082 | } |
| 1083 | |
| 1084 | actionProto := openolt.Action{} |
| 1085 | |
| 1086 | downstreamFlow := openolt.Flow{ |
| 1087 | AccessIntfId: int32(o.PonPortID), |
| 1088 | OnuId: int32(o.ID), |
Matteo Scandolo | 813402b | 2019-10-23 19:24:52 -0700 | [diff] [blame] | 1089 | UniId: int32(0), // FIXME do not hardcode this |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 1090 | FlowId: uint32(o.ID), |
| 1091 | FlowType: "downstream", |
| 1092 | AllocId: int32(0), |
| 1093 | NetworkIntfId: int32(0), |
| 1094 | GemportId: int32(1), // FIXME use the same value as CreateGemPortRequest PortID, do not hardcode |
| 1095 | Classifier: &classifierProto, |
| 1096 | Action: &actionProto, |
| 1097 | Priority: int32(100), |
| 1098 | Cookie: uint64(o.ID), |
| 1099 | PortNo: uint32(o.ID), // NOTE we are using this to map an incoming packetIndication to an ONU |
| 1100 | } |
| 1101 | |
| 1102 | if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil { |
| 1103 | log.WithFields(log.Fields{ |
| 1104 | "IntfId": o.PonPortID, |
| 1105 | "OnuId": o.ID, |
| 1106 | "FlowId": downstreamFlow.FlowId, |
| 1107 | "PortNo": downstreamFlow.PortNo, |
| 1108 | "SerialNumber": common.OnuSnToString(o.SerialNumber), |
| 1109 | }).Fatalf("Failed to send DHCP Flow") |
| 1110 | } |
| 1111 | log.WithFields(log.Fields{ |
| 1112 | "IntfId": o.PonPortID, |
| 1113 | "OnuId": o.ID, |
| 1114 | "FlowId": downstreamFlow.FlowId, |
| 1115 | "PortNo": downstreamFlow.PortNo, |
| 1116 | "SerialNumber": common.OnuSnToString(o.SerialNumber), |
| 1117 | }).Info("Sent DHCP Flow") |
| 1118 | } |
Pragya Arya | 8bdb453 | 2020-03-02 17:08:09 +0530 | [diff] [blame] | 1119 | |
| 1120 | // DeleteFlow method search and delete flowKey from the onu flows slice |
| 1121 | func (onu *Onu) DeleteFlow(key FlowKey) { |
| 1122 | for pos, flowKey := range onu.Flows { |
| 1123 | if flowKey == key { |
| 1124 | // delete the flowKey by shifting all flowKeys by one |
| 1125 | onu.Flows = append(onu.Flows[:pos], onu.Flows[pos+1:]...) |
| 1126 | t := make([]FlowKey, len(onu.Flows)) |
| 1127 | copy(t, onu.Flows) |
| 1128 | onu.Flows = t |
| 1129 | break |
| 1130 | } |
| 1131 | } |
| 1132 | } |
Hardik Windlass | 7b3405b | 2020-07-08 15:10:05 +0530 | [diff] [blame^] | 1133 | |
| 1134 | func (onu *Onu) ReDiscoverOnu() { |
| 1135 | // Wait for few seconds to be sure of the cleanup |
| 1136 | time.Sleep(5 * time.Second) |
| 1137 | |
| 1138 | onuLogger.WithFields(log.Fields{ |
| 1139 | "IntfId": onu.PonPortID, |
| 1140 | "OnuId": onu.ID, |
| 1141 | "OnuSn": onu.Sn(), |
| 1142 | }).Debug("Send ONU Re-Discovery") |
| 1143 | |
| 1144 | // ONU Re-Discovery |
| 1145 | if err := onu.InternalState.Event("initialize"); err != nil { |
| 1146 | log.WithFields(log.Fields{ |
| 1147 | "IntfId": onu.PonPortID, |
| 1148 | "OnuSn": onu.Sn(), |
| 1149 | "OnuId": onu.ID, |
| 1150 | }).Infof("Failed to transition ONU to initialized state: %s", err.Error()) |
| 1151 | } |
| 1152 | |
| 1153 | if err := onu.InternalState.Event("discover"); err != nil { |
| 1154 | log.WithFields(log.Fields{ |
| 1155 | "IntfId": onu.PonPortID, |
| 1156 | "OnuSn": onu.Sn(), |
| 1157 | "OnuId": onu.ID, |
| 1158 | }).Infof("Failed to transition ONU to discovered state: %s", err.Error()) |
| 1159 | } |
| 1160 | } |