blob: b1829141312892c7fcc913ffcb6eed99fca1411e [file] [log] [blame]
khenaidoo7585a962021-06-10 16:15:38 -04001/*
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
17package group
18
19import (
20 "context"
21 "sync"
22
khenaidood948f772021-08-11 17:49:24 -040023 ofp "github.com/opencord/voltha-protos/v5/go/openflow_13"
khenaidoo7585a962021-06-10 16:15:38 -040024)
25
26// Cache hides all low-level locking & synchronization related to group state updates
27type Cache struct {
28 // this lock protects the groups map, it does not protect individual groups
29 lock sync.RWMutex
30 groups map[uint32]*chunk
31}
32
33// chunk keeps a group and the lock for this group
34type chunk struct {
35 // this lock is used to synchronize all access to the group, and also to the "deleted" variable
36 lock sync.Mutex
37 deleted bool
38
39 group *ofp.OfpGroupEntry
40}
41
42func NewCache() *Cache {
43 return &Cache{
44 groups: make(map[uint32]*chunk),
45 }
46}
47
48// LockOrCreate locks this group if it exists, or creates a new group if it does not.
49// In the case of group creation, the provided "group" must not be modified afterwards.
50func (cache *Cache) LockOrCreate(ctx context.Context, group *ofp.OfpGroupEntry) (*Handle, bool, error) {
51 // try to use read lock instead of full lock if possible
52 if handle, have := cache.Lock(group.Desc.GroupId); have {
53 return handle, false, nil
54 }
55
56 cache.lock.Lock()
57 entry, have := cache.groups[group.Desc.GroupId]
58 if !have {
59 entry := &chunk{group: group}
60 cache.groups[group.Desc.GroupId] = entry
61 entry.lock.Lock()
62 cache.lock.Unlock()
63
64 return &Handle{loader: cache, chunk: entry}, true, nil
65 }
66 cache.lock.Unlock()
67
68 entry.lock.Lock()
69 if entry.deleted {
70 entry.lock.Unlock()
71 return cache.LockOrCreate(ctx, group)
72 }
73 return &Handle{loader: cache, chunk: entry}, false, nil
74}
75
76// Lock acquires the lock for this group, and returns a handle which can be used to access the group until it's unlocked.
77// This handle ensures that the group cannot be accessed if the lock is not held.
78// Returns false if the group is not present.
79// TODO: consider accepting a ctx and aborting the lock attempt on cancellation
80func (cache *Cache) Lock(id uint32) (*Handle, bool) {
81 cache.lock.RLock()
82 entry, have := cache.groups[id]
83 cache.lock.RUnlock()
84
85 if !have {
86 return nil, false
87 }
88
89 entry.lock.Lock()
90 if entry.deleted {
91 entry.lock.Unlock()
92 return cache.Lock(id)
93 }
94 return &Handle{loader: cache, chunk: entry}, true
95}
96
97// Handle is allocated for each Lock() call, all modifications are made using it, and it is invalidated by Unlock()
98// This enforces correct Lock()-Usage()-Unlock() ordering.
99type Handle struct {
100 loader *Cache
101 chunk *chunk
102}
103
104// GetReadOnly returns an *ofp.OfpGroupEntry which MUST NOT be modified externally, but which is safe to keep indefinitely
105func (h *Handle) GetReadOnly() *ofp.OfpGroupEntry {
106 return h.chunk.group
107}
108
109// Update updates an existing group in cache.
110// The provided "group" must not be modified afterwards.
111func (h *Handle) Update(ctx context.Context, group *ofp.OfpGroupEntry) error {
112 h.chunk.group = group
113 return nil
114}
115
116// Delete removes the group from the cache
117func (h *Handle) Delete(ctx context.Context) error {
118 h.chunk.deleted = true
119
120 h.loader.lock.Lock()
121 delete(h.loader.groups, h.chunk.group.Desc.GroupId)
122 h.loader.lock.Unlock()
123
124 h.Unlock()
125 return nil
126}
127
128// Unlock releases the lock on the group
129func (h *Handle) Unlock() {
130 if h.chunk != nil {
131 h.chunk.lock.Unlock()
132 h.chunk = nil // attempting to access the group through this handle in future will panic
133 }
134}
135
136// ListIDs returns a snapshot of all the managed group IDs
137// TODO: iterating through groups safely is expensive now, since all groups are stored & locked separately
138// should avoid this where possible
139func (cache *Cache) ListIDs() map[uint32]struct{} {
140 cache.lock.RLock()
141 defer cache.lock.RUnlock()
142 // copy the IDs so caller can safely iterate
143 ret := make(map[uint32]struct{}, len(cache.groups))
144 for id := range cache.groups {
145 ret[id] = struct{}{}
146 }
147 return ret
148}