Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [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 | |
| 17 | package port |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "fmt" |
| 22 | "sync" |
| 23 | |
| 24 | "github.com/opencord/voltha-go/db/model" |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 25 | "github.com/opencord/voltha-lib-go/v7/pkg/log" |
| 26 | "github.com/opencord/voltha-protos/v5/go/voltha" |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 27 | "google.golang.org/grpc/codes" |
| 28 | "google.golang.org/grpc/status" |
| 29 | ) |
| 30 | |
| 31 | // Loader hides all low-level locking & synchronization related to port state updates |
| 32 | type Loader struct { |
| 33 | dbProxy *model.Proxy |
| 34 | // this lock protects the ports map, it does not protect individual ports |
| 35 | lock sync.RWMutex |
| 36 | ports map[uint32]*chunk |
| 37 | deviceLookup map[string]map[uint32]struct{} |
| 38 | } |
| 39 | |
| 40 | // chunk keeps a port and the lock for this port |
| 41 | type chunk struct { |
| 42 | // this lock is used to synchronize all access to the port, and also to the "deleted" variable |
| 43 | lock sync.Mutex |
| 44 | deleted bool |
| 45 | |
| 46 | port *voltha.LogicalPort |
| 47 | } |
| 48 | |
| 49 | func NewLoader(dbProxy *model.Proxy) *Loader { |
| 50 | return &Loader{ |
| 51 | dbProxy: dbProxy, |
| 52 | ports: make(map[uint32]*chunk), |
| 53 | deviceLookup: make(map[string]map[uint32]struct{}), |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | // Load queries existing ports from the kv, |
| 58 | // and should only be called once when first created. |
| 59 | func (loader *Loader) Load(ctx context.Context) { |
| 60 | loader.lock.Lock() |
| 61 | defer loader.lock.Unlock() |
| 62 | |
| 63 | var ports []*voltha.LogicalPort |
| 64 | if err := loader.dbProxy.List(ctx, &ports); err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 65 | logger.Errorw(ctx, "failed-to-list-ports-from-cluster-data-proxy", log.Fields{"error": err}) |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 66 | return |
| 67 | } |
| 68 | for _, port := range ports { |
| 69 | loader.ports[port.OfpPort.PortNo] = &chunk{port: port} |
| 70 | loader.addLookup(port.DeviceId, port.OfpPort.PortNo) |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | // LockOrCreate locks this port if it exists, or creates a new port if it does not. |
| 75 | // In the case of port creation, the provided "port" must not be modified afterwards. |
| 76 | func (loader *Loader) LockOrCreate(ctx context.Context, port *voltha.LogicalPort) (*Handle, bool, error) { |
| 77 | // try to use read lock instead of full lock if possible |
| 78 | if handle, have := loader.Lock(port.OfpPort.PortNo); have { |
| 79 | return handle, false, nil |
| 80 | } |
| 81 | |
| 82 | loader.lock.Lock() |
| 83 | entry, have := loader.ports[port.OfpPort.PortNo] |
| 84 | if !have { |
| 85 | entry := &chunk{port: port} |
| 86 | loader.ports[port.OfpPort.PortNo] = entry |
| 87 | loader.addLookup(port.DeviceId, port.OfpPort.PortNo) |
| 88 | entry.lock.Lock() |
| 89 | loader.lock.Unlock() |
| 90 | |
| 91 | if err := loader.dbProxy.Set(ctx, fmt.Sprint(port.OfpPort.PortNo), port); err != nil { |
| 92 | // revert the map |
| 93 | loader.lock.Lock() |
| 94 | delete(loader.ports, port.OfpPort.PortNo) |
| 95 | loader.removeLookup(port.DeviceId, port.OfpPort.PortNo) |
| 96 | loader.lock.Unlock() |
| 97 | |
| 98 | entry.deleted = true |
| 99 | entry.lock.Unlock() |
| 100 | return nil, false, err |
| 101 | } |
| 102 | return &Handle{loader: loader, chunk: entry}, true, nil |
| 103 | } |
| 104 | loader.lock.Unlock() |
| 105 | |
| 106 | entry.lock.Lock() |
| 107 | if entry.deleted { |
| 108 | entry.lock.Unlock() |
| 109 | return loader.LockOrCreate(ctx, port) |
| 110 | } |
| 111 | return &Handle{loader: loader, chunk: entry}, false, nil |
| 112 | } |
| 113 | |
| 114 | // Lock acquires the lock for this port, and returns a handle which can be used to access the port until it's unlocked. |
| 115 | // This handle ensures that the port cannot be accessed if the lock is not held. |
| 116 | // Returns false if the port is not present. |
| 117 | // TODO: consider accepting a ctx and aborting the lock attempt on cancellation |
| 118 | func (loader *Loader) Lock(id uint32) (*Handle, bool) { |
| 119 | loader.lock.RLock() |
| 120 | entry, have := loader.ports[id] |
| 121 | loader.lock.RUnlock() |
| 122 | |
| 123 | if !have { |
| 124 | return nil, false |
| 125 | } |
| 126 | |
| 127 | entry.lock.Lock() |
| 128 | if entry.deleted { |
| 129 | entry.lock.Unlock() |
| 130 | return loader.Lock(id) |
| 131 | } |
| 132 | return &Handle{loader: loader, chunk: entry}, true |
| 133 | } |
| 134 | |
| 135 | // Handle is allocated for each Lock() call, all modifications are made using it, and it is invalidated by Unlock() |
| 136 | // This enforces correct Lock()-Usage()-Unlock() ordering. |
| 137 | type Handle struct { |
| 138 | loader *Loader |
| 139 | chunk *chunk |
| 140 | } |
| 141 | |
| 142 | // GetReadOnly returns an *voltha.LogicalPort which MUST NOT be modified externally, but which is safe to keep indefinitely |
| 143 | func (h *Handle) GetReadOnly() *voltha.LogicalPort { |
| 144 | return h.chunk.port |
| 145 | } |
| 146 | |
| 147 | // Update updates an existing port in the kv. |
| 148 | // The provided "port" must not be modified afterwards. |
| 149 | func (h *Handle) Update(ctx context.Context, port *voltha.LogicalPort) error { |
| 150 | if err := h.loader.dbProxy.Set(ctx, fmt.Sprint(port.OfpPort.PortNo), port); err != nil { |
| 151 | return status.Errorf(codes.Internal, "failed-update-port-%v: %s", port.OfpPort.PortNo, err) |
| 152 | } |
| 153 | h.chunk.port = port |
| 154 | return nil |
| 155 | } |
| 156 | |
| 157 | // Delete removes the device from the kv |
| 158 | func (h *Handle) Delete(ctx context.Context) error { |
| 159 | if err := h.loader.dbProxy.Remove(ctx, fmt.Sprint(h.chunk.port.OfpPort.PortNo)); err != nil { |
| 160 | return fmt.Errorf("couldnt-delete-port-from-store-%v", h.chunk.port.OfpPort.PortNo) |
| 161 | } |
| 162 | h.chunk.deleted = true |
| 163 | |
| 164 | h.loader.lock.Lock() |
| 165 | delete(h.loader.ports, h.chunk.port.OfpPort.PortNo) |
| 166 | h.loader.removeLookup(h.chunk.port.DeviceId, h.chunk.port.OfpPort.PortNo) |
| 167 | h.loader.lock.Unlock() |
| 168 | |
| 169 | h.Unlock() |
| 170 | return nil |
| 171 | } |
| 172 | |
| 173 | // Unlock releases the lock on the port |
| 174 | func (h *Handle) Unlock() { |
| 175 | if h.chunk != nil { |
| 176 | h.chunk.lock.Unlock() |
| 177 | h.chunk = nil // attempting to access the port through this handle in future will panic |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | // ListIDs returns a snapshot of all the managed port IDs |
| 182 | // TODO: iterating through ports safely is expensive now, since all ports are stored & locked separately |
| 183 | // should avoid this where possible |
| 184 | func (loader *Loader) ListIDs() map[uint32]struct{} { |
| 185 | loader.lock.RLock() |
| 186 | defer loader.lock.RUnlock() |
| 187 | // copy the IDs so caller can safely iterate |
| 188 | ret := make(map[uint32]struct{}, len(loader.ports)) |
| 189 | for id := range loader.ports { |
| 190 | ret[id] = struct{}{} |
| 191 | } |
| 192 | return ret |
| 193 | } |
| 194 | |
| 195 | // ListIDsForDevice lists ports belonging to the specified device |
| 196 | func (loader *Loader) ListIDsForDevice(deviceID string) map[uint32]struct{} { |
| 197 | loader.lock.RLock() |
| 198 | defer loader.lock.RUnlock() |
| 199 | // copy the IDs so caller can safely iterate |
| 200 | devicePorts := loader.deviceLookup[deviceID] |
| 201 | ret := make(map[uint32]struct{}, len(devicePorts)) |
| 202 | for id := range devicePorts { |
| 203 | ret[id] = struct{}{} |
| 204 | } |
| 205 | return ret |
| 206 | } |
| 207 | |
| 208 | func (loader *Loader) addLookup(deviceID string, portNo uint32) { |
| 209 | if devicePorts, have := loader.deviceLookup[deviceID]; have { |
| 210 | devicePorts[portNo] = struct{}{} |
| 211 | } else { |
| 212 | loader.deviceLookup[deviceID] = map[uint32]struct{}{portNo: {}} |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | func (loader *Loader) removeLookup(deviceID string, portNo uint32) { |
| 217 | if devicePorts, have := loader.deviceLookup[deviceID]; have { |
| 218 | delete(devicePorts, portNo) |
| 219 | if len(devicePorts) == 0 { |
| 220 | delete(loader.deviceLookup, deviceID) |
| 221 | } |
| 222 | } |
| 223 | } |