blob: c600c2177bd6dd965a2389d206529bd31e9f872c [file] [log] [blame]
khenaidoo7585a962021-06-10 16:15:38 -04001/*
Joey Armstrong5f51f2e2023-01-17 17:06:26 -05002 * Copyright 2018-2023 Open Networking Foundation (ONF) and the ONF Contributors
khenaidoo7585a962021-06-10 16:15:38 -04003
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 flow
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 flow state updates
27type Cache struct {
28 // this lock protects the flows map, it does not protect individual flows
29 lock sync.RWMutex
30 flows map[uint64]*chunk
31}
32
33// chunk keeps a flow and the lock for this flow
34type chunk struct {
35 // this lock is used to synchronize all access to the flow, and also to the "deleted" variable
36 lock sync.Mutex
37 deleted bool
38
39 flow *ofp.OfpFlowStats
40}
41
42func NewCache() *Cache {
43 return &Cache{
44 flows: make(map[uint64]*chunk),
45 }
46}
47
48// LockOrCreate locks this flow if it exists, or creates a new flow if it does not.
49// In the case of flow creation, the provided "flow" must not be modified afterwards.
50func (cache *Cache) LockOrCreate(ctx context.Context, flow *ofp.OfpFlowStats) (*Handle, bool, error) {
51 // try to use read lock instead of full lock if possible
52 if handle, have := cache.Lock(flow.Id); have {
53 return handle, false, nil
54 }
55
56 cache.lock.Lock()
57 entry, have := cache.flows[flow.Id]
58 if !have {
59 entry := &chunk{flow: flow}
60 cache.flows[flow.Id] = 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, flow)
72 }
73 return &Handle{loader: cache, chunk: entry}, false, nil
74}
75
76// Lock acquires the lock for this flow, and returns a handle which can be used to access the flow until it's unlocked.
77// This handle ensures that the flow cannot be accessed if the lock is not held.
78// Returns false if the flow is not present.
79// TODO: consider accepting a ctx and aborting the lock attempt on cancellation
80func (cache *Cache) Lock(id uint64) (*Handle, bool) {
81 cache.lock.RLock()
82 entry, have := cache.flows[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.OfpFlowStats which MUST NOT be modified externally, but which is safe to keep indefinitely
105func (h *Handle) GetReadOnly() *ofp.OfpFlowStats {
106 return h.chunk.flow
107}
108
109// Update updates an existing flow in cache.
110// The provided "flow" must not be modified afterwards.
111func (h *Handle) Update(ctx context.Context, flow *ofp.OfpFlowStats) error {
112 h.chunk.flow = flow
113 return nil
114}
115
116// Delete removes the flow 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.flows, h.chunk.flow.Id)
122 h.loader.lock.Unlock()
123
124 h.Unlock()
125 return nil
126}
127
128// Unlock releases the lock on the flow
129func (h *Handle) Unlock() {
130 if h.chunk != nil {
131 h.chunk.lock.Unlock()
132 h.chunk = nil // attempting to access the flow through this handle in future will panic
133 }
134}
135
136// ListIDs returns a snapshot of all the managed flow IDs
137// TODO: iterating through flows safely is expensive now, since all flows are stored & locked separately
138// should avoid this where possible
139func (cache *Cache) ListIDs() map[uint64]struct{} {
140 cache.lock.RLock()
141 defer cache.lock.RUnlock()
142 // copy the IDs so caller can safely iterate
143 ret := make(map[uint64]struct{}, len(cache.flows))
144 for id := range cache.flows {
145 ret[id] = struct{}{}
146 }
147 return ret
148}