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