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