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