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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
// Copyright 2016 The Netstack Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package queue
import (
"encoding/binary"
"gvisor.googlesource.com/gvisor/pkg/log"
"gvisor.googlesource.com/gvisor/pkg/tcpip/link/sharedmem/pipe"
)
const (
// Offsets within a packet header.
packetID = 0
packetSize = 8
packetReserved = 12
sizeOfPacketHeader = 16
// Offsets with a buffer descriptor
bufferOffset = 0
bufferSize = 8
sizeOfBufferDescriptor = 12
)
// TxBuffer is the descriptor of a transmit buffer.
type TxBuffer struct {
Next *TxBuffer
Offset uint64
Size uint32
}
// Tx is a transmit queue. It is implemented with one tx and one rx pipe: the
// tx pipe is used to request the transmission of packets, while the rx pipe
// is used to receive which transmissions have completed.
//
// This struct is thread-compatible.
type Tx struct {
tx pipe.Tx
rx pipe.Rx
}
// Init initializes the transmit queue with the given pipes.
func (t *Tx) Init(tx, rx []byte) {
t.tx.Init(tx)
t.rx.Init(rx)
}
// Enqueue queues the given linked list of buffers for transmission as one
// packet. While it is queued, the caller must not modify them.
func (t *Tx) Enqueue(id uint64, totalDataLen, bufferCount uint32, buffer *TxBuffer) bool {
// Reserve room in the tx pipe.
totalLen := sizeOfPacketHeader + uint64(bufferCount)*sizeOfBufferDescriptor
b := t.tx.Push(totalLen)
if b == nil {
return false
}
// Initialize the packet and buffer descriptors.
binary.LittleEndian.PutUint64(b[packetID:], id)
binary.LittleEndian.PutUint32(b[packetSize:], totalDataLen)
binary.LittleEndian.PutUint32(b[packetReserved:], 0)
offset := sizeOfPacketHeader
for i := bufferCount; i != 0; i-- {
binary.LittleEndian.PutUint64(b[offset+bufferOffset:], buffer.Offset)
binary.LittleEndian.PutUint32(b[offset+bufferSize:], buffer.Size)
offset += sizeOfBufferDescriptor
buffer = buffer.Next
}
t.tx.Flush()
return true
}
// CompletedPacket returns the id of the last completed transmission. The
// returned id, if any, refers to a value passed on a previous call to
// Enqueue().
func (t *Tx) CompletedPacket() (id uint64, ok bool) {
for {
b := t.rx.Pull()
if b == nil {
return 0, false
}
if len(b) != 8 {
t.rx.Flush()
log.Warningf("Ignoring completed packet: size (%v) is less than expected (%v)", len(b), 8)
continue
}
v := binary.LittleEndian.Uint64(b)
t.rx.Flush()
return v, true
}
}
// Bytes returns the byte slices on which the queue operates.
func (t *Tx) Bytes() (tx, rx []byte) {
return t.tx.Bytes(), t.rx.Bytes()
}
// TxPacketInfo holds information about a packet sent on a tx queue.
type TxPacketInfo struct {
ID uint64
Size uint32
Reserved uint32
BufferCount int
}
// DecodeTxPacketHeader decodes the header of a packet sent over a tx queue.
func DecodeTxPacketHeader(b []byte) TxPacketInfo {
return TxPacketInfo{
ID: binary.LittleEndian.Uint64(b[packetID:]),
Size: binary.LittleEndian.Uint32(b[packetSize:]),
Reserved: binary.LittleEndian.Uint32(b[packetReserved:]),
BufferCount: (len(b) - sizeOfPacketHeader) / sizeOfBufferDescriptor,
}
}
// DecodeTxBufferHeader decodes the header of the i-th buffer of a packet sent
// over a tx queue.
func DecodeTxBufferHeader(b []byte, i int) TxBuffer {
b = b[sizeOfPacketHeader+i*sizeOfBufferDescriptor:]
return TxBuffer{
Offset: binary.LittleEndian.Uint64(b[bufferOffset:]),
Size: binary.LittleEndian.Uint32(b[bufferSize:]),
}
}
// EncodeTxCompletion encodes a tx completion header.
func EncodeTxCompletion(b []byte, id uint64) {
binary.LittleEndian.PutUint64(b, id)
}
|