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