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