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