aboutsummaryrefslogtreecommitdiffstats
path: root/extras/gomemif/memif/memif.go
blob: 1a7857de03e5ea683f28d7394413a4ce1dfbde1a (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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
 *------------------------------------------------------------------
 * Copyright (c) 2020 Cisco and/or its affiliates.
 * 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 memif

import (
	"encoding/binary"
	"fmt"
	"syscall"
)

const cookie = 0x3E31F20

// VersionMajor is memif protocols major version
const VersionMajor = 2

// VersionMinor is memif protocols minor version
const VersionMinor = 0

// Version is memif protocols version as uint16
// (M-Major m-minor: MMMMMMMMmmmmmmmm)
const Version = ((VersionMajor << 8) | VersionMinor)

type msgType uint16

const (
	msgTypeNone msgType = iota
	msgTypeAck
	msgTypeHello
	msgTypeInit
	msgTypeAddRegion
	msgTypeAddRing
	msgTypeConnect
	msgTypeConnected
	msgTypeDisconnect
)

type interfaceMode uint8

const (
	interfaceModeEthernet interfaceMode = iota
	interfaceModeIp
	interfaceModePuntInject
)

const msgSize = 128
const msgTypeSize = 2

const msgAddRingFlagS2M = (1 << 0)

// Descriptor flags
//
// next buffer present
const descFlagNext = (1 << 0)

// Ring flags
//
// Interrupt
const ringFlagInterrupt = 1

func min16(a uint16, b uint16) uint16 {
	if a < b {
		return a
	}
	return b
}

func min8(a uint8, b uint8) uint8 {
	if a < b {
		return a
	}
	return b
}

type MsgHello struct {
	// app name
	Name            [32]byte
	VersionMin      uint16
	VersionMax      uint16
	MaxRegion       uint16
	MaxRingM2S      uint16
	MaxRingS2M      uint16
	MaxLog2RingSize uint8
}

type MsgInit struct {
	Version uint16
	Id      uint32
	Mode    interfaceMode
	Secret  [24]byte
	// app name
	Name [32]byte
}

type MsgAddRegion struct {
	Index uint16
	Size  uint64
}

type MsgAddRing struct {
	Flags          uint16
	Index          uint16
	Region         uint16
	Offset         uint32
	RingSizeLog2   uint8
	PrivateHdrSize uint16
}

type MsgConnect struct {
	// interface name
	Name [32]byte
}

type MsgConnected struct {
	// interface name
	Name [32]byte
}

type MsgDisconnect struct {
	Code   uint32
	String [96]byte
}

/* DESCRIPTOR BEGIN */

const descSize = 16

// desc field offsets
const descFlagsOffset = 0
const descRegionOffset = 2
const descLengthOffset = 4
const descOffsetOffset = 8
const descMetadataOffset = 12

// descBuf represents a memif descriptor as array of bytes
type descBuf []byte

// newDescBuf returns new descriptor buffer
func newDescBuf() descBuf {
	return make(descBuf, descSize)
}

// getDescBuff copies descriptor from shared memory to descBuf
func (q *Queue) getDescBuf(slot int, db descBuf) {
	copy(db, q.i.regions[q.ring.region].data[q.ring.offset+ringSize+slot*descSize:])
}

// putDescBuf copies contents of descriptor buffer into shared memory
func (q *Queue) putDescBuf(slot int, db descBuf) {
	copy(q.i.regions[q.ring.region].data[q.ring.offset+ringSize+slot*descSize:], db)
}

func (db descBuf) getFlags() int {
	return (int)(binary.LittleEndian.Uint16((db)[descFlagsOffset:]))
}

func (db descBuf) getRegion() int {
	return (int)(binary.LittleEndian.Uint16((db)[descRegionOffset:]))
}

func (db descBuf) getLength() int {
	return (int)(binary.LittleEndian.Uint32((db)[descLengthOffset:]))
}

func (db descBuf) getOffset() int {
	return (int)(binary.LittleEndian.Uint32((db)[descOffsetOffset:]))
}

func (db descBuf) getMetadata() int {
	return (int)(binary.LittleEndian.Uint32((db)[descMetadataOffset:]))
}

func (db descBuf) setFlags(val int) {
	binary.LittleEndian.PutUint16((db)[descFlagsOffset:], uint16(val))
}

func (db descBuf) setRegion(val int) {
	binary.LittleEndian.PutUint16((db)[descRegionOffset:], uint16(val))
}

func (db descBuf) setLength(val int) {
	binary.LittleEndian.PutUint32((db)[descLengthOffset:], uint32(val))
}

func (db descBuf) setOffset(val int) {
	binary.LittleEndian.PutUint32((db)[descOffsetOffset:], uint32(val))
}

func (db descBuf) setMetadata(val int) {
	binary.LittleEndian.PutUint32((db)[descMetadataOffset:], uint32(val))
}

/* DESCRIPTOR END */

/* RING BEGIN */

type ringType uint8

const (
	ringTypeS2M ringType = iota
	ringTypeM2S
)

const ringSize = 128

// ring field offsets
const ringCookieOffset = 0
const ringFlagsOffset = 4
const ringHeadOffset = 6
const ringTailOffset = 64

// ringBuf represents a memif ring as array of bytes
type ringBuf []byte

type ring struct {
	ringType ringType
	size     int
	log2Size int
	region   int
	rb       ringBuf
	offset   int
}

// newRing returns new memif ring based on data received in msgAddRing (master only)
func newRing(regionIndex int, ringType ringType, ringOffset int, log2RingSize int) *ring {
	r := &ring{
		ringType: ringType,
		size:     (1 << log2RingSize),
		log2Size: log2RingSize,
		rb:       make(ringBuf, ringSize),
		offset:   ringOffset,
	}

	return r
}

// newRing returns a new memif ring
func (i *Interface) newRing(regionIndex int, ringType ringType, ringIndex int) *ring {
	r := &ring{
		ringType: ringType,
		size:     (1 << i.run.Log2RingSize),
		log2Size: int(i.run.Log2RingSize),
		rb:       make(ringBuf, ringSize),
	}

	rSize := ringSize + descSize*r.size
	if r.ringType == ringTypeS2M {
		r.offset = 0
	} else {
		r.offset = int(i.run.NumQueuePairs) * rSize
	}
	r.offset += ringIndex * rSize

	return r
}

// putRing put the ring to the shared memory
func (q *Queue) putRing() {
	copy(q.i.regions[q.ring.region].data[q.ring.offset:], q.ring.rb)
}

// updateRing updates ring with data from shared memory
func (q *Queue) updateRing() {
	copy(q.ring.rb, q.i.regions[q.ring.region].data[q.ring.offset:])
}

func (r *ring) getCookie() int {
	return (int)(binary.LittleEndian.Uint32((r.rb)[ringCookieOffset:]))
}

// getFlags returns the flags value from ring buffer
// Use Queue.getFlags in fast-path to avoid updating the whole ring.
func (r *ring) getFlags() int {
	return (int)(binary.LittleEndian.Uint16((r.rb)[ringFlagsOffset:]))
}

// getHead returns the head pointer value from ring buffer.
// Use readHead in fast-path to avoid updating the whole ring.
func (r *ring) getHead() int {
	return (int)(binary.LittleEndian.Uint16((r.rb)[ringHeadOffset:]))
}

// getTail returns the tail pointer value from ring buffer.
// Use readTail in fast-path to avoid updating the whole ring.
func (r *ring) getTail() int {
	return (int)(binary.LittleEndian.Uint16((r.rb)[ringTailOffset:]))
}

func (r *ring) setCookie(val int) {
	binary.LittleEndian.PutUint32((r.rb)[ringCookieOffset:], uint32(val))
}

func (r *ring) setFlags(val int) {
	binary.LittleEndian.PutUint16((r.rb)[ringFlagsOffset:], uint16(val))
}

// setHead set the head pointer value int the ring buffer.
// Use writeHead in fast-path to avoid putting the whole ring into shared memory.
func (r *ring) setHead(val int) {
	binary.LittleEndian.PutUint16((r.rb)[ringHeadOffset:], uint16(val))
}

// setTail set the tail pointer value int the ring buffer.
// Use writeTail in fast-path to avoid putting the whole ring into shared memory.
func (r *ring) setTail(val int) {
	binary.LittleEndian.PutUint16((r.rb)[ringTailOffset:], uint16(val))
}

/* RING END */

// isInterrupt returns true if the queue is in interrupt mode
func (q *Queue) isInterrupt() bool {
	return (q.getFlags() & ringFlagInterrupt) == 0
}

// interrupt performs an interrupt if the queue is in interrupt mode
func (q *Queue) interrupt() error {
	if q.isInterrupt() {
		buf := make([]byte, 8)
		binary.PutUvarint(buf, 1)
		n, err := syscall.Write(q.interruptFd, buf[:])
		if err != nil {
			return err
		}
		if n != 8 {
			return fmt.Errorf("Faild to write to eventfd")
		}
	}

	return nil
}