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 | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 21 | "errors" |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 22 | "fmt" |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 23 | "github.com/cboling/omci" |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 24 | "github.com/google/gopacket/layers" |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 25 | "github.com/looplab/fsm" |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 26 | "github.com/opencord/bbsim/internal/bbsim/packetHandlers" |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 27 | "github.com/opencord/bbsim/internal/bbsim/responders/dhcp" |
| 28 | "github.com/opencord/bbsim/internal/bbsim/responders/eapol" |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 29 | "github.com/opencord/bbsim/internal/common" |
| 30 | omcilib "github.com/opencord/bbsim/internal/common/omci" |
| 31 | omcisim "github.com/opencord/omci-sim" |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 32 | "github.com/opencord/voltha-protos/go/openolt" |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 33 | log "github.com/sirupsen/logrus" |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 34 | "net" |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 35 | ) |
| 36 | |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 37 | var onuLogger = log.WithFields(log.Fields{ |
| 38 | "module": "ONU", |
| 39 | }) |
| 40 | |
Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame] | 41 | type Onu struct { |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 42 | ID uint32 |
| 43 | PonPortID uint32 |
| 44 | PonPort PonPort |
| 45 | STag int |
| 46 | CTag int |
Matteo Scandolo | c114709 | 2019-10-29 09:38:33 -0700 | [diff] [blame^] | 47 | Auth bool // automatically start EAPOL if set to true |
| 48 | Dhcp bool // automatically start DHCP if set to true |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 49 | // PortNo comes with flows and it's used when sending packetIndications, |
| 50 | // There is one PortNo per UNI Port, for now we're only storing the first one |
| 51 | // FIXME add support for multiple UNIs |
Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame] | 52 | HwAddress net.HardwareAddr |
| 53 | InternalState *fsm.FSM |
| 54 | |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 55 | // ONU State |
| 56 | PortNo uint32 |
| 57 | DhcpFlowReceived bool |
| 58 | |
Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame] | 59 | OperState *fsm.FSM |
| 60 | SerialNumber *openolt.SerialNumber |
| 61 | |
| 62 | 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] | 63 | |
| 64 | // OMCI params |
| 65 | tid uint16 |
| 66 | hpTid uint16 |
| 67 | seqNumber uint16 |
| 68 | HasGemPort bool |
| 69 | |
| 70 | DoneChannel chan bool // this channel is used to signal once the onu is complete (when the struct is used by BBR) |
Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 73 | func (o *Onu) Sn() string { |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 74 | return common.OnuSnToString(o.SerialNumber) |
Matteo Scandolo | 86e8ce6 | 2019-10-11 12:03:10 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Matteo Scandolo | c114709 | 2019-10-29 09:38:33 -0700 | [diff] [blame^] | 77 | func CreateONU(olt OltDevice, pon PonPort, id uint32, sTag int, cTag int, auth bool, dhcp bool) *Onu { |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 78 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 79 | o := Onu{ |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 80 | ID: id, |
| 81 | PonPortID: pon.ID, |
| 82 | PonPort: pon, |
| 83 | STag: sTag, |
| 84 | CTag: cTag, |
Matteo Scandolo | c114709 | 2019-10-29 09:38:33 -0700 | [diff] [blame^] | 85 | Auth: auth, |
| 86 | Dhcp: dhcp, |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 87 | HwAddress: net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, byte(pon.ID), byte(id)}, |
| 88 | PortNo: 0, |
| 89 | Channel: make(chan Message, 2048), |
| 90 | tid: 0x1, |
| 91 | hpTid: 0x8000, |
| 92 | seqNumber: 0, |
| 93 | DoneChannel: make(chan bool, 1), |
| 94 | DhcpFlowReceived: false, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 95 | } |
| 96 | o.SerialNumber = o.NewSN(olt.ID, pon.ID, o.ID) |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 97 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 98 | // NOTE this state machine is used to track the operational |
| 99 | // state as requested by VOLTHA |
| 100 | o.OperState = getOperStateFSM(func(e *fsm.Event) { |
| 101 | onuLogger.WithFields(log.Fields{ |
| 102 | "ID": o.ID, |
| 103 | }).Debugf("Changing ONU OperState from %s to %s", e.Src, e.Dst) |
| 104 | }) |
| 105 | |
| 106 | // NOTE this state machine is used to activate the OMCI, EAPOL and DHCP clients |
| 107 | o.InternalState = fsm.NewFSM( |
| 108 | "created", |
| 109 | fsm.Events{ |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 110 | // DEVICE Lifecycle |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 111 | {Name: "discover", Src: []string{"created"}, Dst: "discovered"}, |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 112 | {Name: "enable", Src: []string{"discovered", "disabled"}, Dst: "enabled"}, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 113 | {Name: "receive_eapol_flow", Src: []string{"enabled", "gem_port_added"}, Dst: "eapol_flow_received"}, |
| 114 | {Name: "add_gem_port", Src: []string{"enabled", "eapol_flow_received"}, Dst: "gem_port_added"}, |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 115 | // NOTE should disabled state be diffente for oper_disabled (emulating an error) and admin_disabled (received a disabled call via VOLTHA)? |
| 116 | {Name: "disable", Src: []string{"eap_response_success_received", "auth_failed", "dhcp_ack_received", "dhcp_failed"}, Dst: "disabled"}, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 117 | // EAPOL |
Matteo Scandolo | e383d5d | 2019-10-25 14:47:27 -0700 | [diff] [blame] | 118 | {Name: "start_auth", Src: []string{"eapol_flow_received", "gem_port_added", "eap_response_success_received", "auth_failed", "dhcp_ack_received", "dhcp_failed"}, Dst: "auth_started"}, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 119 | {Name: "eap_start_sent", Src: []string{"auth_started"}, Dst: "eap_start_sent"}, |
| 120 | {Name: "eap_response_identity_sent", Src: []string{"eap_start_sent"}, Dst: "eap_response_identity_sent"}, |
| 121 | {Name: "eap_response_challenge_sent", Src: []string{"eap_response_identity_sent"}, Dst: "eap_response_challenge_sent"}, |
| 122 | {Name: "eap_response_success_received", Src: []string{"eap_response_challenge_sent"}, Dst: "eap_response_success_received"}, |
| 123 | {Name: "auth_failed", Src: []string{"auth_started", "eap_start_sent", "eap_response_identity_sent", "eap_response_challenge_sent"}, Dst: "auth_failed"}, |
| 124 | // DHCP |
Matteo Scandolo | fe9ac25 | 2019-10-25 11:40:17 -0700 | [diff] [blame] | 125 | {Name: "start_dhcp", Src: []string{"eap_response_success_received", "dhcp_discovery_sent", "dhcp_request_sent", "dhcp_ack_received", "dhcp_failed"}, Dst: "dhcp_started"}, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 126 | {Name: "dhcp_discovery_sent", Src: []string{"dhcp_started"}, Dst: "dhcp_discovery_sent"}, |
| 127 | {Name: "dhcp_request_sent", Src: []string{"dhcp_discovery_sent"}, Dst: "dhcp_request_sent"}, |
| 128 | {Name: "dhcp_ack_received", Src: []string{"dhcp_request_sent"}, Dst: "dhcp_ack_received"}, |
| 129 | {Name: "dhcp_failed", Src: []string{"dhcp_started", "dhcp_discovery_sent", "dhcp_request_sent"}, Dst: "dhcp_failed"}, |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 130 | // BBR States |
| 131 | // TODO add start OMCI state |
| 132 | {Name: "send_eapol_flow", Src: []string{"created"}, Dst: "eapol_flow_sent"}, |
| 133 | {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] | 134 | }, |
| 135 | fsm.Callbacks{ |
| 136 | "enter_state": func(e *fsm.Event) { |
| 137 | o.logStateChange(e.Src, e.Dst) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 138 | }, |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 139 | "enter_enabled": func(event *fsm.Event) { |
| 140 | msg := Message{ |
| 141 | Type: OnuIndication, |
| 142 | Data: OnuIndicationMessage{ |
| 143 | OnuSN: o.SerialNumber, |
| 144 | PonPortID: o.PonPortID, |
| 145 | OperState: UP, |
| 146 | }, |
| 147 | } |
| 148 | o.Channel <- msg |
| 149 | }, |
| 150 | "enter_disabled": func(event *fsm.Event) { |
| 151 | msg := Message{ |
| 152 | Type: OnuIndication, |
| 153 | Data: OnuIndicationMessage{ |
| 154 | OnuSN: o.SerialNumber, |
| 155 | PonPortID: o.PonPortID, |
| 156 | OperState: DOWN, |
| 157 | }, |
| 158 | } |
| 159 | o.Channel <- msg |
| 160 | }, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 161 | "enter_auth_started": func(e *fsm.Event) { |
| 162 | o.logStateChange(e.Src, e.Dst) |
| 163 | msg := Message{ |
| 164 | Type: StartEAPOL, |
| 165 | Data: PacketMessage{ |
| 166 | PonPortID: o.PonPortID, |
| 167 | OnuID: o.ID, |
| 168 | }, |
| 169 | } |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 170 | o.Channel <- msg |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 171 | }, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 172 | "enter_auth_failed": func(e *fsm.Event) { |
| 173 | onuLogger.WithFields(log.Fields{ |
| 174 | "OnuId": o.ID, |
| 175 | "IntfId": o.PonPortID, |
| 176 | "OnuSn": o.Sn(), |
| 177 | }).Errorf("ONU failed to authenticate!") |
| 178 | }, |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 179 | "before_start_dhcp": func(e *fsm.Event) { |
| 180 | if o.DhcpFlowReceived == false { |
| 181 | e.Cancel(errors.New("cannot-go-to-dhcp-started-as-dhcp-flow-is-missing")) |
| 182 | } |
| 183 | }, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 184 | "enter_dhcp_started": func(e *fsm.Event) { |
| 185 | msg := Message{ |
| 186 | Type: StartDHCP, |
| 187 | Data: PacketMessage{ |
| 188 | PonPortID: o.PonPortID, |
| 189 | OnuID: o.ID, |
| 190 | }, |
| 191 | } |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 192 | o.Channel <- msg |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 193 | }, |
| 194 | "enter_dhcp_failed": func(e *fsm.Event) { |
| 195 | onuLogger.WithFields(log.Fields{ |
| 196 | "OnuId": o.ID, |
| 197 | "IntfId": o.PonPortID, |
| 198 | "OnuSn": o.Sn(), |
| 199 | }).Errorf("ONU failed to DHCP!") |
| 200 | }, |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 201 | "enter_eapol_flow_sent": func(e *fsm.Event) { |
| 202 | msg := Message{ |
| 203 | Type: SendEapolFlow, |
| 204 | } |
| 205 | o.Channel <- msg |
| 206 | }, |
| 207 | "enter_dhcp_flow_sent": func(e *fsm.Event) { |
| 208 | msg := Message{ |
| 209 | Type: SendDhcpFlow, |
| 210 | } |
| 211 | o.Channel <- msg |
| 212 | }, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 213 | }, |
| 214 | ) |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 215 | return &o |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 218 | func (o Onu) logStateChange(src string, dst string) { |
| 219 | onuLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 220 | "OnuId": o.ID, |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 221 | "IntfId": o.PonPortID, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 222 | "OnuSn": o.Sn(), |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 223 | }).Debugf("Changing ONU InternalState from %s to %s", src, dst) |
| 224 | } |
| 225 | |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 226 | func (o *Onu) ProcessOnuMessages(stream openolt.Openolt_EnableIndicationServer, client openolt.OpenoltClient) { |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 227 | onuLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 228 | "onuID": o.ID, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 229 | "onuSN": o.Sn(), |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 230 | }).Debug("Started ONU Indication Channel") |
| 231 | |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 232 | for message := range o.Channel { |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 233 | onuLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 234 | "onuID": o.ID, |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 235 | "onuSN": o.Sn(), |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 236 | "messageType": message.Type, |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 237 | }).Tracef("Received message on ONU Channel") |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 238 | |
| 239 | switch message.Type { |
| 240 | case OnuDiscIndication: |
| 241 | msg, _ := message.Data.(OnuDiscIndicationMessage) |
| 242 | o.sendOnuDiscIndication(msg, stream) |
| 243 | case OnuIndication: |
| 244 | msg, _ := message.Data.(OnuIndicationMessage) |
| 245 | o.sendOnuIndication(msg, stream) |
| 246 | case OMCI: |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 247 | msg, _ := message.Data.(OmciMessage) |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 248 | o.handleOmciMessage(msg, stream) |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 249 | case FlowUpdate: |
| 250 | msg, _ := message.Data.(OnuFlowUpdateMessage) |
Matteo Scandolo | 813402b | 2019-10-23 19:24:52 -0700 | [diff] [blame] | 251 | o.handleFlowUpdate(msg) |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 252 | case StartEAPOL: |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 253 | log.Infof("Receive StartEAPOL message on ONU Channel") |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 254 | eapol.SendEapStart(o.ID, o.PonPortID, o.Sn(), o.PortNo, o.HwAddress, o.InternalState, stream) |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 255 | case StartDHCP: |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 256 | log.Infof("Receive StartDHCP message on ONU Channel") |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 257 | // FIXME use id, ponId as SendEapStart |
| 258 | dhcp.SendDHCPDiscovery(o.PonPortID, o.ID, o.Sn(), o.PortNo, o.InternalState, o.HwAddress, o.CTag, stream) |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 259 | case OnuPacketOut: |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 260 | |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 261 | msg, _ := message.Data.(OnuPacketMessage) |
| 262 | |
| 263 | log.WithFields(log.Fields{ |
| 264 | "IntfId": msg.IntfId, |
| 265 | "OnuId": msg.OnuId, |
| 266 | "pktType": msg.Type, |
| 267 | }).Trace("Received OnuPacketOut Message") |
| 268 | |
| 269 | if msg.Type == packetHandlers.EAPOL { |
| 270 | eapol.HandleNextPacket(msg.OnuId, msg.IntfId, o.Sn(), o.PortNo, o.InternalState, msg.Packet, stream, client) |
| 271 | } else if msg.Type == packetHandlers.DHCP { |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 272 | // NOTE here we receive packets going from the DHCP Server to the ONU |
| 273 | // for now we expect them to be double-tagged, but ideally the should be single tagged |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 274 | dhcp.HandleNextPacket(o.ID, o.PonPortID, o.Sn(), o.PortNo, o.HwAddress, o.CTag, o.InternalState, msg.Packet, stream) |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 275 | } |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 276 | case OnuPacketIn: |
| 277 | // NOTE we only receive BBR packets here. |
| 278 | // Eapol.HandleNextPacket can handle both BBSim and BBr cases so the call is the same |
| 279 | // in the DHCP case VOLTHA only act as a proxy, the behaviour is completely different thus we have a dhcp.HandleNextBbrPacket |
| 280 | msg, _ := message.Data.(OnuPacketMessage) |
| 281 | |
| 282 | log.WithFields(log.Fields{ |
| 283 | "IntfId": msg.IntfId, |
| 284 | "OnuId": msg.OnuId, |
| 285 | "pktType": msg.Type, |
| 286 | }).Trace("Received OnuPacketIn Message") |
| 287 | |
| 288 | if msg.Type == packetHandlers.EAPOL { |
| 289 | eapol.HandleNextPacket(msg.OnuId, msg.IntfId, o.Sn(), o.PortNo, o.InternalState, msg.Packet, stream, client) |
| 290 | } else if msg.Type == packetHandlers.DHCP { |
| 291 | dhcp.HandleNextBbrPacket(o.ID, o.PonPortID, o.Sn(), o.STag, o.HwAddress, o.DoneChannel, msg.Packet, client) |
| 292 | } |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 293 | case DyingGaspIndication: |
| 294 | msg, _ := message.Data.(DyingGaspIndicationMessage) |
| 295 | o.sendDyingGaspInd(msg, stream) |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 296 | case OmciIndication: |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 297 | msg, _ := message.Data.(OmciIndicationMessage) |
| 298 | o.handleOmci(msg, client) |
| 299 | case SendEapolFlow: |
| 300 | o.sendEapolFlow(client) |
| 301 | case SendDhcpFlow: |
| 302 | o.sendDhcpFlow(client) |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 303 | default: |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 304 | onuLogger.Warnf("Received unknown message data %v for type %v in OLT Channel", message.Data, message.Type) |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 305 | } |
| 306 | } |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 307 | } |
| 308 | |
William Kurkian | 9dadc5b | 2019-10-22 13:51:57 -0400 | [diff] [blame] | 309 | func (o Onu) processOmciMessage(message omcisim.OmciChMessage) { |
| 310 | switch message.Type { |
| 311 | case omcisim.GemPortAdded: |
| 312 | log.WithFields(log.Fields{ |
| 313 | "OnuId": message.Data.OnuId, |
| 314 | "IntfId": message.Data.IntfId, |
| 315 | }).Infof("GemPort Added") |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 316 | |
William Kurkian | 9dadc5b | 2019-10-22 13:51:57 -0400 | [diff] [blame] | 317 | // NOTE if we receive the GemPort but we don't have EAPOL flows |
| 318 | // go an intermediate state, otherwise start auth |
| 319 | if o.InternalState.Is("enabled") { |
| 320 | if err := o.InternalState.Event("add_gem_port"); err != nil { |
| 321 | log.Errorf("Can't go to gem_port_added: %v", err) |
| 322 | } |
| 323 | } else if o.InternalState.Is("eapol_flow_received") { |
| 324 | if err := o.InternalState.Event("start_auth"); err != nil { |
| 325 | log.Errorf("Can't go to auth_started: %v", err) |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 331 | func (o Onu) NewSN(oltid int, intfid uint32, onuid uint32) *openolt.SerialNumber { |
| 332 | |
| 333 | sn := new(openolt.SerialNumber) |
| 334 | |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 335 | //sn = new(openolt.SerialNumber) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 336 | sn.VendorId = []byte("BBSM") |
| 337 | sn.VendorSpecific = []byte{0, byte(oltid % 256), byte(intfid), byte(onuid)} |
| 338 | |
| 339 | return sn |
| 340 | } |
| 341 | |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 342 | // NOTE handle_/process methods can change the ONU internal state as they are receiving messages |
| 343 | // send method should not change the ONU state |
| 344 | |
| 345 | func (o Onu) sendDyingGaspInd(msg DyingGaspIndicationMessage, stream openolt.Openolt_EnableIndicationServer) error { |
| 346 | alarmData := &openolt.AlarmIndication_DyingGaspInd{ |
| 347 | DyingGaspInd: &openolt.DyingGaspIndication{ |
| 348 | IntfId: msg.PonPortID, |
| 349 | OnuId: msg.OnuID, |
| 350 | Status: "on", |
| 351 | }, |
| 352 | } |
| 353 | data := &openolt.Indication_AlarmInd{AlarmInd: &openolt.AlarmIndication{Data: alarmData}} |
| 354 | |
| 355 | if err := stream.Send(&openolt.Indication{Data: data}); err != nil { |
| 356 | onuLogger.Errorf("Failed to send DyingGaspInd : %v", err) |
| 357 | return err |
| 358 | } |
| 359 | onuLogger.WithFields(log.Fields{ |
| 360 | "IntfId": msg.PonPortID, |
| 361 | "OnuSn": o.Sn(), |
| 362 | "OnuId": msg.OnuID, |
| 363 | }).Info("sendDyingGaspInd") |
| 364 | return nil |
| 365 | } |
| 366 | |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 367 | func (o Onu) sendOnuDiscIndication(msg OnuDiscIndicationMessage, stream openolt.Openolt_EnableIndicationServer) { |
| 368 | discoverData := &openolt.Indication_OnuDiscInd{OnuDiscInd: &openolt.OnuDiscIndication{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 369 | IntfId: msg.Onu.PonPortID, |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 370 | SerialNumber: msg.Onu.SerialNumber, |
| 371 | }} |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 372 | |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 373 | if err := stream.Send(&openolt.Indication{Data: discoverData}); err != nil { |
Matteo Scandolo | 1100699 | 2019-08-28 11:29:46 -0700 | [diff] [blame] | 374 | log.Errorf("Failed to send Indication_OnuDiscInd: %v", err) |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 375 | return |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 376 | } |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 377 | |
| 378 | if err := o.InternalState.Event("discover"); err != nil { |
| 379 | oltLogger.WithFields(log.Fields{ |
| 380 | "IntfId": o.PonPortID, |
| 381 | "OnuSn": o.Sn(), |
| 382 | "OnuId": o.ID, |
| 383 | }).Infof("Failed to transition ONU to discovered state: %s", err.Error()) |
| 384 | } |
| 385 | |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 386 | onuLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 387 | "IntfId": msg.Onu.PonPortID, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 388 | "OnuSn": msg.Onu.Sn(), |
| 389 | "OnuId": o.ID, |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 390 | }).Debug("Sent Indication_OnuDiscInd") |
| 391 | } |
| 392 | |
| 393 | func (o Onu) sendOnuIndication(msg OnuIndicationMessage, stream openolt.Openolt_EnableIndicationServer) { |
| 394 | // NOTE voltha returns an ID, but if we use that ID then it complains: |
| 395 | // expected_onu_id: 1, received_onu_id: 1024, event: ONU-id-mismatch, can happen if both voltha and the olt rebooted |
| 396 | // so we're using the internal ID that is 1 |
| 397 | // o.ID = msg.OnuID |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 398 | |
| 399 | indData := &openolt.Indication_OnuInd{OnuInd: &openolt.OnuIndication{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 400 | IntfId: o.PonPortID, |
| 401 | OnuId: o.ID, |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 402 | OperState: msg.OperState.String(), |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 403 | AdminState: o.OperState.Current(), |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 404 | SerialNumber: o.SerialNumber, |
| 405 | }} |
| 406 | if err := stream.Send(&openolt.Indication{Data: indData}); err != nil { |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 407 | // TODO do we need to transition to a broken state? |
Matteo Scandolo | 1100699 | 2019-08-28 11:29:46 -0700 | [diff] [blame] | 408 | log.Errorf("Failed to send Indication_OnuInd: %v", err) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 409 | } |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 410 | onuLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 411 | "IntfId": o.PonPortID, |
| 412 | "OnuId": o.ID, |
| 413 | "OperState": msg.OperState.String(), |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 414 | "AdminState": msg.OperState.String(), |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 415 | "OnuSn": o.Sn(), |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 416 | }).Debug("Sent Indication_OnuInd") |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 417 | |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | func (o Onu) handleOmciMessage(msg OmciMessage, stream openolt.Openolt_EnableIndicationServer) { |
| 421 | |
| 422 | onuLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 423 | "IntfId": o.PonPortID, |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 424 | "SerialNumber": o.Sn(), |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 425 | "omciPacket": msg.omciMsg.Pkt, |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 426 | }).Tracef("Received OMCI message") |
| 427 | |
| 428 | var omciInd openolt.OmciIndication |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 429 | respPkt, err := omcisim.OmciSim(o.PonPortID, o.ID, HexDecode(msg.omciMsg.Pkt)) |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 430 | if err != nil { |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 431 | onuLogger.WithFields(log.Fields{ |
| 432 | "IntfId": o.PonPortID, |
| 433 | "SerialNumber": o.Sn(), |
| 434 | "omciPacket": omciInd.Pkt, |
| 435 | "msg": msg, |
| 436 | }).Errorf("Error handling OMCI message %v", msg) |
| 437 | return |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | omciInd.IntfId = o.PonPortID |
| 441 | omciInd.OnuId = o.ID |
| 442 | omciInd.Pkt = respPkt |
| 443 | |
| 444 | omci := &openolt.Indication_OmciInd{OmciInd: &omciInd} |
| 445 | if err := stream.Send(&openolt.Indication{Data: omci}); err != nil { |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 446 | onuLogger.WithFields(log.Fields{ |
| 447 | "IntfId": o.PonPortID, |
| 448 | "SerialNumber": o.Sn(), |
| 449 | "omciPacket": omciInd.Pkt, |
| 450 | "msg": msg, |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 451 | }).Errorf("send omcisim indication failed: %v", err) |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 452 | return |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 453 | } |
| 454 | onuLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 455 | "IntfId": o.PonPortID, |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 456 | "SerialNumber": o.Sn(), |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 457 | "omciPacket": omciInd.Pkt, |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 458 | }).Tracef("Sent OMCI message") |
| 459 | } |
| 460 | |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 461 | func (o *Onu) storePortNumber(portNo uint32) { |
Matteo Scandolo | 813402b | 2019-10-23 19:24:52 -0700 | [diff] [blame] | 462 | // 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] | 463 | // we need to add support for multiple UNIs |
| 464 | // the action plan is: |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 465 | // - 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] | 466 | // - change the library so that it reports a single UNI and remove this workaroung |
| 467 | // - add support for multiple UNIs in BBSim |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 468 | if o.PortNo == 0 || portNo < o.PortNo { |
Matteo Scandolo | 813402b | 2019-10-23 19:24:52 -0700 | [diff] [blame] | 469 | onuLogger.WithFields(log.Fields{ |
| 470 | "IntfId": o.PonPortID, |
| 471 | "OnuId": o.ID, |
| 472 | "SerialNumber": o.Sn(), |
| 473 | "OnuPortNo": o.PortNo, |
| 474 | "FlowPortNo": portNo, |
| 475 | }).Debug("Storing ONU portNo") |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 476 | o.PortNo = portNo |
| 477 | } |
| 478 | } |
| 479 | |
Matteo Scandolo | 813402b | 2019-10-23 19:24:52 -0700 | [diff] [blame] | 480 | func (o *Onu) handleFlowUpdate(msg OnuFlowUpdateMessage) { |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 481 | onuLogger.WithFields(log.Fields{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 482 | "DstPort": msg.Flow.Classifier.DstPort, |
| 483 | "EthType": fmt.Sprintf("%x", msg.Flow.Classifier.EthType), |
| 484 | "FlowId": msg.Flow.FlowId, |
| 485 | "FlowType": msg.Flow.FlowType, |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 486 | "InnerVlan": msg.Flow.Classifier.IVid, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 487 | "IntfId": msg.Flow.AccessIntfId, |
| 488 | "IpProto": msg.Flow.Classifier.IpProto, |
| 489 | "OnuId": msg.Flow.OnuId, |
| 490 | "OnuSn": o.Sn(), |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 491 | "OuterVlan": msg.Flow.Classifier.OVid, |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 492 | "PortNo": msg.Flow.PortNo, |
| 493 | "SrcPort": msg.Flow.Classifier.SrcPort, |
| 494 | "UniID": msg.Flow.UniId, |
| 495 | }).Debug("ONU receives Flow") |
| 496 | |
Matteo Scandolo | 813402b | 2019-10-23 19:24:52 -0700 | [diff] [blame] | 497 | if msg.Flow.UniId != 0 { |
| 498 | // as of now BBSim only support a single UNI, so ignore everything that is not targeted to it |
| 499 | onuLogger.WithFields(log.Fields{ |
| 500 | "IntfId": o.PonPortID, |
| 501 | "OnuId": o.ID, |
| 502 | "SerialNumber": o.Sn(), |
| 503 | }).Debug("Ignoring flow as it's not for the first UNI") |
| 504 | return |
| 505 | } |
| 506 | |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 507 | 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] | 508 | // NOTE storing the PortNO, it's needed when sending PacketIndications |
Matteo Scandolo | 813402b | 2019-10-23 19:24:52 -0700 | [diff] [blame] | 509 | o.storePortNumber(uint32(msg.Flow.PortNo)) |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 510 | |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 511 | // NOTE if we receive the EAPOL flows but we don't have GemPorts |
| 512 | // go an intermediate state, otherwise start auth |
| 513 | if o.InternalState.Is("enabled") { |
| 514 | if err := o.InternalState.Event("receive_eapol_flow"); err != nil { |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 515 | log.Warnf("Can't go to eapol_flow_received: %v", err) |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 516 | } |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 517 | } else if o.InternalState.Is("gem_port_added") { |
Matteo Scandolo | c114709 | 2019-10-29 09:38:33 -0700 | [diff] [blame^] | 518 | |
| 519 | if o.Auth == true { |
| 520 | if err := o.InternalState.Event("start_auth"); err != nil { |
| 521 | log.Warnf("Can't go to auth_started: %v", err) |
| 522 | } |
| 523 | } else { |
| 524 | onuLogger.WithFields(log.Fields{ |
| 525 | "IntfId": o.PonPortID, |
| 526 | "OnuId": o.ID, |
| 527 | "SerialNumber": o.Sn(), |
| 528 | }).Warn("Not starting authentication as Auth bit is not set in CLI parameters") |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 529 | } |
Matteo Scandolo | c114709 | 2019-10-29 09:38:33 -0700 | [diff] [blame^] | 530 | |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 531 | } |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 532 | } else if msg.Flow.Classifier.EthType == uint32(layers.EthernetTypeIPv4) && |
| 533 | msg.Flow.Classifier.SrcPort == uint32(68) && |
| 534 | msg.Flow.Classifier.DstPort == uint32(67) { |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 535 | |
| 536 | // keep track that we reveived the DHCP Flows so that we can transition the state to dhcp_started |
| 537 | o.DhcpFlowReceived = true |
Matteo Scandolo | c114709 | 2019-10-29 09:38:33 -0700 | [diff] [blame^] | 538 | |
| 539 | if o.Dhcp == true { |
| 540 | // NOTE we are receiving mulitple DHCP flows but we shouldn't call the transition multiple times |
| 541 | if err := o.InternalState.Event("start_dhcp"); err != nil { |
| 542 | log.Errorf("Can't go to dhcp_started: %v", err) |
| 543 | } |
| 544 | } else { |
| 545 | onuLogger.WithFields(log.Fields{ |
| 546 | "IntfId": o.PonPortID, |
| 547 | "OnuId": o.ID, |
| 548 | "SerialNumber": o.Sn(), |
| 549 | }).Warn("Not starting DHCP as Dhcp bit is not set in CLI parameters") |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 550 | } |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 551 | } |
| 552 | } |
| 553 | |
Matteo Scandolo | c559ef1 | 2019-08-20 13:24:21 -0700 | [diff] [blame] | 554 | // HexDecode converts the hex encoding to binary |
| 555 | func HexDecode(pkt []byte) []byte { |
| 556 | p := make([]byte, len(pkt)/2) |
| 557 | for i, j := 0, 0; i < len(pkt); i, j = i+2, j+1 { |
| 558 | // Go figure this ;) |
| 559 | u := (pkt[i] & 15) + (pkt[i]>>6)*9 |
| 560 | l := (pkt[i+1] & 15) + (pkt[i+1]>>6)*9 |
| 561 | p[j] = u<<4 + l |
| 562 | } |
| 563 | onuLogger.Tracef("Omci decoded: %x.", p) |
| 564 | return p |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 565 | } |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 566 | |
| 567 | // BBR methods |
| 568 | |
| 569 | func sendOmciMsg(pktBytes []byte, intfId uint32, onuId uint32, sn *openolt.SerialNumber, msgType string, client openolt.OpenoltClient) { |
| 570 | omciMsg := openolt.OmciMsg{ |
| 571 | IntfId: intfId, |
| 572 | OnuId: onuId, |
| 573 | Pkt: pktBytes, |
| 574 | } |
| 575 | |
| 576 | if _, err := client.OmciMsgOut(context.Background(), &omciMsg); err != nil { |
| 577 | log.WithFields(log.Fields{ |
| 578 | "IntfId": intfId, |
| 579 | "OnuId": onuId, |
| 580 | "SerialNumber": common.OnuSnToString(sn), |
| 581 | "Pkt": omciMsg.Pkt, |
| 582 | }).Fatalf("Failed to send MIB Reset") |
| 583 | } |
| 584 | log.WithFields(log.Fields{ |
| 585 | "IntfId": intfId, |
| 586 | "OnuId": onuId, |
| 587 | "SerialNumber": common.OnuSnToString(sn), |
| 588 | "Pkt": omciMsg.Pkt, |
| 589 | }).Tracef("Sent OMCI message %s", msgType) |
| 590 | } |
| 591 | |
| 592 | func (onu *Onu) getNextTid(highPriority ...bool) uint16 { |
| 593 | var next uint16 |
| 594 | if len(highPriority) > 0 && highPriority[0] { |
| 595 | next = onu.hpTid |
| 596 | onu.hpTid += 1 |
| 597 | if onu.hpTid < 0x8000 { |
| 598 | onu.hpTid = 0x8000 |
| 599 | } |
| 600 | } else { |
| 601 | next = onu.tid |
| 602 | onu.tid += 1 |
| 603 | if onu.tid >= 0x8000 { |
| 604 | onu.tid = 1 |
| 605 | } |
| 606 | } |
| 607 | return next |
| 608 | } |
| 609 | |
| 610 | // TODO move this method in responders/omcisim |
| 611 | func (o *Onu) StartOmci(client openolt.OpenoltClient) { |
| 612 | mibReset, _ := omcilib.CreateMibResetRequest(o.getNextTid(false)) |
| 613 | sendOmciMsg(mibReset, o.PonPortID, o.ID, o.SerialNumber, "mibReset", client) |
| 614 | } |
| 615 | |
| 616 | func (o *Onu) handleOmci(msg OmciIndicationMessage, client openolt.OpenoltClient) { |
| 617 | msgType, packet := omcilib.DecodeOmci(msg.OmciInd.Pkt) |
| 618 | |
| 619 | log.WithFields(log.Fields{ |
| 620 | "IntfId": msg.OmciInd.IntfId, |
| 621 | "OnuId": msg.OmciInd.OnuId, |
| 622 | "OnuSn": common.OnuSnToString(o.SerialNumber), |
| 623 | "Pkt": msg.OmciInd.Pkt, |
| 624 | "msgType": msgType, |
| 625 | }).Trace("ONU Receveives OMCI Msg") |
| 626 | switch msgType { |
| 627 | default: |
Matteo Scandolo | 813402b | 2019-10-23 19:24:52 -0700 | [diff] [blame] | 628 | log.WithFields(log.Fields{ |
| 629 | "IntfId": msg.OmciInd.IntfId, |
| 630 | "OnuId": msg.OmciInd.OnuId, |
| 631 | "OnuSn": common.OnuSnToString(o.SerialNumber), |
| 632 | "Pkt": msg.OmciInd.Pkt, |
| 633 | "msgType": msgType, |
| 634 | }).Fatalf("unexpected frame: %v", packet) |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 635 | case omci.MibResetResponseType: |
| 636 | mibUpload, _ := omcilib.CreateMibUploadRequest(o.getNextTid(false)) |
| 637 | sendOmciMsg(mibUpload, o.PonPortID, o.ID, o.SerialNumber, "mibUpload", client) |
| 638 | case omci.MibUploadResponseType: |
| 639 | mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber) |
| 640 | sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client) |
| 641 | case omci.MibUploadNextResponseType: |
| 642 | o.seqNumber++ |
| 643 | |
| 644 | if o.seqNumber > 290 { |
| 645 | // NOTE we are done with the MIB Upload (290 is the number of messages the omci-sim library will respond to) |
| 646 | galEnet, _ := omcilib.CreateGalEnetRequest(o.getNextTid(false)) |
| 647 | sendOmciMsg(galEnet, o.PonPortID, o.ID, o.SerialNumber, "CreateGalEnetRequest", client) |
| 648 | } else { |
| 649 | mibUploadNext, _ := omcilib.CreateMibUploadNextRequest(o.getNextTid(false), o.seqNumber) |
| 650 | sendOmciMsg(mibUploadNext, o.PonPortID, o.ID, o.SerialNumber, "mibUploadNext", client) |
| 651 | } |
| 652 | case omci.CreateResponseType: |
| 653 | // NOTE Creating a GemPort, |
| 654 | // BBsim actually doesn't care about the values, so we can do we want with the parameters |
| 655 | // In the same way we can create a GemPort even without setting up UNIs/TConts/... |
| 656 | // but we need the GemPort to trigger the state change |
| 657 | |
| 658 | if !o.HasGemPort { |
| 659 | // NOTE this sends a CreateRequestType and BBSim replies with a CreateResponseType |
| 660 | // thus we send this request only once |
| 661 | gemReq, _ := omcilib.CreateGemPortRequest(o.getNextTid(false)) |
| 662 | sendOmciMsg(gemReq, o.PonPortID, o.ID, o.SerialNumber, "CreateGemPortRequest", client) |
| 663 | o.HasGemPort = true |
| 664 | } else { |
| 665 | if err := o.InternalState.Event("send_eapol_flow"); err != nil { |
| 666 | onuLogger.WithFields(log.Fields{ |
| 667 | "OnuId": o.ID, |
| 668 | "IntfId": o.PonPortID, |
| 669 | "OnuSn": o.Sn(), |
| 670 | }).Errorf("Error while transitioning ONU State %v", err) |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | func (o *Onu) sendEapolFlow(client openolt.OpenoltClient) { |
| 678 | |
| 679 | classifierProto := openolt.Classifier{ |
| 680 | EthType: uint32(layers.EthernetTypeEAPOL), |
| 681 | OVid: 4091, |
| 682 | } |
| 683 | |
| 684 | actionProto := openolt.Action{} |
| 685 | |
| 686 | downstreamFlow := openolt.Flow{ |
| 687 | AccessIntfId: int32(o.PonPortID), |
| 688 | OnuId: int32(o.ID), |
Matteo Scandolo | 813402b | 2019-10-23 19:24:52 -0700 | [diff] [blame] | 689 | UniId: int32(0), // NOTE do not hardcode this, we need to support multiple UNIs |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 690 | FlowId: uint32(o.ID), |
| 691 | FlowType: "downstream", |
| 692 | AllocId: int32(0), |
| 693 | NetworkIntfId: int32(0), |
| 694 | GemportId: int32(1), // FIXME use the same value as CreateGemPortRequest PortID, do not hardcode |
| 695 | Classifier: &classifierProto, |
| 696 | Action: &actionProto, |
| 697 | Priority: int32(100), |
| 698 | Cookie: uint64(o.ID), |
| 699 | PortNo: uint32(o.ID), // NOTE we are using this to map an incoming packetIndication to an ONU |
| 700 | } |
| 701 | |
| 702 | if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil { |
| 703 | log.WithFields(log.Fields{ |
| 704 | "IntfId": o.PonPortID, |
| 705 | "OnuId": o.ID, |
| 706 | "FlowId": downstreamFlow.FlowId, |
| 707 | "PortNo": downstreamFlow.PortNo, |
| 708 | "SerialNumber": common.OnuSnToString(o.SerialNumber), |
| 709 | }).Fatalf("Failed to EAPOL Flow") |
| 710 | } |
| 711 | log.WithFields(log.Fields{ |
| 712 | "IntfId": o.PonPortID, |
| 713 | "OnuId": o.ID, |
| 714 | "FlowId": downstreamFlow.FlowId, |
| 715 | "PortNo": downstreamFlow.PortNo, |
| 716 | "SerialNumber": common.OnuSnToString(o.SerialNumber), |
| 717 | }).Info("Sent EAPOL Flow") |
| 718 | } |
| 719 | |
| 720 | func (o *Onu) sendDhcpFlow(client openolt.OpenoltClient) { |
| 721 | classifierProto := openolt.Classifier{ |
| 722 | EthType: uint32(layers.EthernetTypeIPv4), |
| 723 | SrcPort: uint32(68), |
| 724 | DstPort: uint32(67), |
| 725 | } |
| 726 | |
| 727 | actionProto := openolt.Action{} |
| 728 | |
| 729 | downstreamFlow := openolt.Flow{ |
| 730 | AccessIntfId: int32(o.PonPortID), |
| 731 | OnuId: int32(o.ID), |
Matteo Scandolo | 813402b | 2019-10-23 19:24:52 -0700 | [diff] [blame] | 732 | UniId: int32(0), // FIXME do not hardcode this |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 733 | FlowId: uint32(o.ID), |
| 734 | FlowType: "downstream", |
| 735 | AllocId: int32(0), |
| 736 | NetworkIntfId: int32(0), |
| 737 | GemportId: int32(1), // FIXME use the same value as CreateGemPortRequest PortID, do not hardcode |
| 738 | Classifier: &classifierProto, |
| 739 | Action: &actionProto, |
| 740 | Priority: int32(100), |
| 741 | Cookie: uint64(o.ID), |
| 742 | PortNo: uint32(o.ID), // NOTE we are using this to map an incoming packetIndication to an ONU |
| 743 | } |
| 744 | |
| 745 | if _, err := client.FlowAdd(context.Background(), &downstreamFlow); err != nil { |
| 746 | log.WithFields(log.Fields{ |
| 747 | "IntfId": o.PonPortID, |
| 748 | "OnuId": o.ID, |
| 749 | "FlowId": downstreamFlow.FlowId, |
| 750 | "PortNo": downstreamFlow.PortNo, |
| 751 | "SerialNumber": common.OnuSnToString(o.SerialNumber), |
| 752 | }).Fatalf("Failed to send DHCP Flow") |
| 753 | } |
| 754 | log.WithFields(log.Fields{ |
| 755 | "IntfId": o.PonPortID, |
| 756 | "OnuId": o.ID, |
| 757 | "FlowId": downstreamFlow.FlowId, |
| 758 | "PortNo": downstreamFlow.PortNo, |
| 759 | "SerialNumber": common.OnuSnToString(o.SerialNumber), |
| 760 | }).Info("Sent DHCP Flow") |
| 761 | } |