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