Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020-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 | |
| 17 | //Package adaptercoreonu provides the utility for onu devices, flows and statistics |
| 18 | package adaptercoreonu |
| 19 | |
| 20 | import ( |
| 21 | "context" |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 22 | "fmt" |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 23 | "strconv" |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 24 | "strings" |
Girish Gowdra | 50e5642 | 2021-06-01 16:46:04 -0700 | [diff] [blame] | 25 | "time" |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 26 | |
| 27 | //"sync" |
| 28 | //"time" |
| 29 | |
Girish Gowdra | 50e5642 | 2021-06-01 16:46:04 -0700 | [diff] [blame] | 30 | //"github.com/opencord/voltha-lib-go/v5/pkg/kafka" |
| 31 | "github.com/opencord/voltha-lib-go/v5/pkg/log" |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 32 | vc "github.com/opencord/voltha-protos/v4/go/common" |
| 33 | of "github.com/opencord/voltha-protos/v4/go/openflow_13" |
| 34 | "github.com/opencord/voltha-protos/v4/go/voltha" |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 35 | ) |
| 36 | |
Himani Chawla | 6d2ae15 | 2020-09-02 13:11:20 +0530 | [diff] [blame] | 37 | type uniPortType uint8 |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 38 | |
Holger Hildebrandt | dd23cc2 | 2020-05-19 13:32:18 +0000 | [diff] [blame] | 39 | // UniPPTP Interface type - re-use values from G.988 TP type definition (directly used in OMCI!) |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 40 | const ( |
Himani Chawla | 6d2ae15 | 2020-09-02 13:11:20 +0530 | [diff] [blame] | 41 | // uniPPTP relates to PPTP |
| 42 | uniPPTP uniPortType = 1 // relates to PPTP |
| 43 | // uniVEIP relates to VEIP |
| 44 | uniVEIP uniPortType = 11 // relates to VEIP |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 45 | ) |
| 46 | |
Himani Chawla | 6d2ae15 | 2020-09-02 13:11:20 +0530 | [diff] [blame] | 47 | //onuUniPort structure holds information about the ONU attached Uni Ports |
| 48 | type onuUniPort struct { |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 49 | enabled bool |
| 50 | name string |
| 51 | portNo uint32 |
Himani Chawla | 6d2ae15 | 2020-09-02 13:11:20 +0530 | [diff] [blame] | 52 | portType uniPortType |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 53 | ofpPortNo string |
Himani Chawla | 26e555c | 2020-08-31 12:30:20 +0530 | [diff] [blame] | 54 | uniID uint8 |
Holger Hildebrandt | dd23cc2 | 2020-05-19 13:32:18 +0000 | [diff] [blame] | 55 | macBpNo uint8 |
Himani Chawla | 26e555c | 2020-08-31 12:30:20 +0530 | [diff] [blame] | 56 | entityID uint16 |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 57 | adminState vc.AdminState_Types |
| 58 | operState vc.OperStatus_Types |
| 59 | pPort *voltha.Port |
| 60 | } |
| 61 | |
Himani Chawla | 6d2ae15 | 2020-09-02 13:11:20 +0530 | [diff] [blame] | 62 | //newOnuUniPort returns a new instance of a OnuUniPort |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 63 | func newOnuUniPort(ctx context.Context, aUniID uint8, aPortNo uint32, aInstNo uint16, |
Himani Chawla | 6d2ae15 | 2020-09-02 13:11:20 +0530 | [diff] [blame] | 64 | aPortType uniPortType) *onuUniPort { |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 65 | logger.Infow(ctx, "init-onuUniPort", log.Fields{"uniID": aUniID, |
Himani Chawla | 26e555c | 2020-08-31 12:30:20 +0530 | [diff] [blame] | 66 | "portNo": aPortNo, "InstNo": aInstNo, "type": aPortType}) |
Himani Chawla | 6d2ae15 | 2020-09-02 13:11:20 +0530 | [diff] [blame] | 67 | var onuUniPort onuUniPort |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 68 | onuUniPort.enabled = false |
Himani Chawla | 26e555c | 2020-08-31 12:30:20 +0530 | [diff] [blame] | 69 | onuUniPort.name = "uni-" + strconv.FormatUint(uint64(aPortNo), 10) |
| 70 | onuUniPort.portNo = aPortNo |
| 71 | onuUniPort.portType = aPortType |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 72 | // so far it seems as here ofpPortNo/Name ist the same as the original port name ...?? |
| 73 | onuUniPort.ofpPortNo = onuUniPort.name |
Himani Chawla | 26e555c | 2020-08-31 12:30:20 +0530 | [diff] [blame] | 74 | onuUniPort.uniID = aUniID |
| 75 | onuUniPort.macBpNo = aUniID + 1 //ensure >0 instanceNo |
| 76 | onuUniPort.entityID = aInstNo |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 77 | onuUniPort.adminState = vc.AdminState_ENABLED //enabled per create |
| 78 | onuUniPort.operState = vc.OperStatus_UNKNOWN |
| 79 | onuUniPort.pPort = nil // to be set on create |
| 80 | return &onuUniPort |
| 81 | } |
| 82 | |
Himani Chawla | 6d2ae15 | 2020-09-02 13:11:20 +0530 | [diff] [blame] | 83 | //createVolthaPort creates the Voltha port based on ONU UNI Port and informs the core about it |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 84 | func (oo *onuUniPort) createVolthaPort(ctx context.Context, apDeviceHandler *deviceHandler) error { |
| 85 | logger.Debugw(ctx, "creating-voltha-uni-port", log.Fields{ |
divyadesai | 4d29955 | 2020-08-18 07:13:49 +0000 | [diff] [blame] | 86 | "device-id": apDeviceHandler.device.Id, "portNo": oo.portNo}) |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 87 | //200630: per [VOL-3202] OF port info is now to be delivered within UniPort create |
| 88 | // not doing so crashes rw_core processing (at least still in 200630 version) |
| 89 | name := apDeviceHandler.device.SerialNumber + "-" + strconv.FormatUint(uint64(oo.macBpNo), 10) |
| 90 | var macOctets [6]uint8 |
| 91 | macOctets[5] = 0x08 |
| 92 | //ponPortNumber was copied from device.ParentPortNo |
| 93 | macOctets[4] = uint8(apDeviceHandler.ponPortNumber >> 8) |
| 94 | macOctets[3] = uint8(apDeviceHandler.ponPortNumber) |
| 95 | macOctets[2] = uint8(oo.portNo >> 16) |
| 96 | macOctets[1] = uint8(oo.portNo >> 8) |
| 97 | macOctets[0] = uint8(oo.portNo) |
| 98 | hwAddr := genMacFromOctets(macOctets) |
| 99 | ofHwAddr := macAddressToUint32Array(hwAddr) |
| 100 | capacity := uint32(of.OfpPortFeatures_OFPPF_1GB_FD | of.OfpPortFeatures_OFPPF_FIBER) |
| 101 | ofUniPortState := of.OfpPortState_OFPPS_LINK_DOWN |
| 102 | /* as the VOLTHA port create is only called directly after Uni Port create |
| 103 | the OfPortOperState is always Down |
| 104 | Note: this way the OfPortOperState won't ever change (directly in adapter) |
| 105 | maybe that was already always the case, but looks a bit weird - to be kept in mind ... |
| 106 | if pUniPort.operState == vc.OperStatus_ACTIVE { |
| 107 | ofUniPortState = of.OfpPortState_OFPPS_LIVE |
| 108 | } |
| 109 | */ |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 110 | logger.Debugw(ctx, "ofPort values", log.Fields{ |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 111 | "forUniPortName": oo.name, "forMacBase": hwAddr, |
| 112 | "name": name, "hwAddr": ofHwAddr, "OperState": ofUniPortState}) |
| 113 | |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 114 | pUniPort := &voltha.Port{ |
| 115 | PortNo: oo.portNo, |
| 116 | Label: oo.name, |
| 117 | Type: voltha.Port_ETHERNET_UNI, |
| 118 | AdminState: oo.adminState, |
| 119 | OperStatus: oo.operState, |
| 120 | // obviously empty peer setting |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 121 | OfpPort: &of.OfpPort{ |
| 122 | Name: name, |
| 123 | HwAddr: ofHwAddr, |
| 124 | Config: 0, |
| 125 | State: uint32(ofUniPortState), |
| 126 | Curr: capacity, |
| 127 | Advertised: capacity, |
| 128 | Peer: capacity, |
Andrea Campanella | 550c340 | 2021-01-21 14:59:40 +0100 | [diff] [blame] | 129 | CurrSpeed: 1000, |
| 130 | MaxSpeed: 1000, |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 131 | }, |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 132 | } |
Girish Gowdra | 50e5642 | 2021-06-01 16:46:04 -0700 | [diff] [blame] | 133 | maxRetry := 3 |
| 134 | retryCnt := 0 |
| 135 | var err error |
| 136 | for retryCnt = 0; retryCnt < maxRetry; retryCnt++ { |
| 137 | if err = apDeviceHandler.coreProxy.PortCreated(log.WithSpanFromContext(context.TODO(), ctx), |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 138 | apDeviceHandler.deviceID, pUniPort); err != nil { |
Girish Gowdra | 50e5642 | 2021-06-01 16:46:04 -0700 | [diff] [blame] | 139 | logger.Errorf(ctx, "Device FSM: PortCreated-failed-%s, retrying after a delay", err) |
| 140 | // retry after a sleep |
| 141 | time.Sleep(2 * time.Second) |
| 142 | } else { |
| 143 | // success, break from retry loop |
| 144 | break |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 145 | } |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 146 | } |
Girish Gowdra | 50e5642 | 2021-06-01 16:46:04 -0700 | [diff] [blame] | 147 | if retryCnt == maxRetry { // maxed out.. |
| 148 | logger.Errorf(ctx, "Device FSM: PortCreated-failed-%s", err) |
| 149 | return fmt.Errorf("device-fsm-port-create-failed-%s", err) |
| 150 | } |
| 151 | logger.Infow(ctx, "Voltha onuUniPort-added", log.Fields{ |
| 152 | "device-id": apDeviceHandler.device.Id, "PortNo": oo.portNo}) |
| 153 | oo.pPort = pUniPort |
| 154 | oo.operState = vc.OperStatus_DISCOVERED |
| 155 | |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 156 | return nil |
| 157 | } |
Holger Hildebrandt | 24d5195 | 2020-05-04 14:03:42 +0000 | [diff] [blame] | 158 | |
Himani Chawla | 6d2ae15 | 2020-09-02 13:11:20 +0530 | [diff] [blame] | 159 | //setOperState modifies OperState of the the UniPort |
| 160 | func (oo *onuUniPort) setOperState(aNewOperState vc.OperStatus_Types) { |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 161 | oo.operState = aNewOperState |
| 162 | } |
| 163 | |
| 164 | // uni port related utility functions (so far only used here) |
| 165 | func genMacFromOctets(aOctets [6]uint8) string { |
| 166 | return fmt.Sprintf("%02x:%02x:%02x:%02x:%02x:%02x", |
| 167 | aOctets[5], aOctets[4], aOctets[3], |
| 168 | aOctets[2], aOctets[1], aOctets[0]) |
| 169 | } |
| 170 | |
| 171 | //copied from OLT Adapter: unify centrally ? |
| 172 | func macAddressToUint32Array(mac string) []uint32 { |
| 173 | slist := strings.Split(mac, ":") |
| 174 | result := make([]uint32, len(slist)) |
| 175 | var err error |
| 176 | var tmp int64 |
| 177 | for index, val := range slist { |
| 178 | if tmp, err = strconv.ParseInt(val, 16, 32); err != nil { |
| 179 | return []uint32{1, 2, 3, 4, 5, 6} |
| 180 | } |
| 181 | result[index] = uint32(tmp) |
| 182 | } |
| 183 | return result |
Holger Hildebrandt | 24d5195 | 2020-05-04 14:03:42 +0000 | [diff] [blame] | 184 | } |