blob: 45adcbccb159cc600c6b03ae2931c9c6e31d1d07 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
// Copyright 2020 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package fifo
import (
"gvisor.dev/gvisor/pkg/sync"
"gvisor.dev/gvisor/pkg/tcpip/stack"
)
// packetBufferQueue is a bounded, thread-safe queue of PacketBuffers.
//
type packetBufferQueue struct {
mu sync.Mutex
list stack.PacketBufferList
limit int
used int
}
// emptyLocked determines if the queue is empty.
// Preconditions: q.mu must be held.
func (q *packetBufferQueue) emptyLocked() bool {
return q.used == 0
}
// empty determines if the queue is empty.
func (q *packetBufferQueue) empty() bool {
q.mu.Lock()
r := q.emptyLocked()
q.mu.Unlock()
return r
}
// setLimit updates the limit. No PacketBuffers are immediately dropped in case
// the queue becomes full due to the new limit.
func (q *packetBufferQueue) setLimit(limit int) {
q.mu.Lock()
q.limit = limit
q.mu.Unlock()
}
// enqueue adds the given packet to the queue.
//
// Returns true when the PacketBuffer is successfully added to the queue, in
// which case ownership of the reference is transferred to the queue. And
// returns false if the queue is full, in which case ownership is retained by
// the caller.
func (q *packetBufferQueue) enqueue(s *stack.PacketBuffer) bool {
q.mu.Lock()
r := q.used < q.limit
if r {
s.EgressRoute.Acquire()
q.list.PushBack(s)
q.used++
}
q.mu.Unlock()
return r
}
// dequeue removes and returns the next PacketBuffer from queue, if one exists.
// Ownership is transferred to the caller.
func (q *packetBufferQueue) dequeue() *stack.PacketBuffer {
q.mu.Lock()
s := q.list.Front()
if s != nil {
q.list.Remove(s)
q.used--
}
q.mu.Unlock()
return s
}
|