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