summaryrefslogtreecommitdiffstats
path: root/extras/libmemif/packethandle.go
blob: 83bf90a511a2485124ee7f39b4f879b02b8e44c3 (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
// Copyright (c) 2017 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 libmemif

import (
	"github.com/google/gopacket"
	"time"
	"sync"
	"io"
)

type memoizedPacket struct {
	data RawPacketData
	ci   gopacket.CaptureInfo
}

type MemifPacketHandle struct {
	memif   *Memif
	queueId uint8
	rxCount uint16

	// Used for caching packets when larger rxburst is called
	packetQueue []*memoizedPacket

	// Used for synchronization of read/write calls
	readMu  sync.Mutex
	writeMu sync.Mutex
	closeMu sync.Mutex
	stop    bool
}

// Create new GoPacket packet handle from libmemif queue. rxCount determines how many packets will be read
// at once, minimum value is 1
func (memif *Memif) NewPacketHandle(queueId uint8, rxCount uint16) *MemifPacketHandle {
	if rxCount == 0 {
		rxCount = 1
	}

	return &MemifPacketHandle{
		memif:   memif,
		queueId: queueId,
		rxCount: rxCount,
	}
}

// Reads packet data from memif in bursts, based on previously configured rxCount parameterer. Then caches the
// resulting packets and returns them 1 by 1 from this method until queue is empty then tries to call new rx burst
// to read more data. If no data is returned, io.EOF error is thrown and caller should stop reading.
func (handle *MemifPacketHandle) ReadPacketData() (data []byte, ci gopacket.CaptureInfo, err error) {
	handle.readMu.Lock()
	defer handle.readMu.Unlock()

	if handle.stop {
		err = io.EOF
		return
	}

	queueLen := len(handle.packetQueue)

	if queueLen == 0 {
		packets, burstErr := handle.memif.RxBurst(handle.queueId, handle.rxCount)
		packetsLen := len(packets)

		if burstErr != nil {
			err = burstErr
			return
		}

		if packetsLen == 0 {
			err = io.EOF
			return
		}

		handle.packetQueue = make([]*memoizedPacket, packetsLen)

		for i, packet := range packets {
			packetLen := len(packet)

			handle.packetQueue[i] = &memoizedPacket{
				data: []byte(packet),
				ci: gopacket.CaptureInfo{
					Timestamp:     time.Now(),
					CaptureLength: packetLen,
					Length:        packetLen,
				},
			}
		}
	}

	packet := handle.packetQueue[0]
	handle.packetQueue = handle.packetQueue[1:]
	data = packet.data
	ci = packet.ci

	return
}

// Writes packet data to memif in burst of 1 packet. In case no packet is sent, this method throws io.EOF error and
// called should stop trying to write packets.
func (handle *MemifPacketHandle) WritePacketData(data []byte) (err error) {
	handle.writeMu.Lock()
	defer handle.writeMu.Unlock()

	if handle.stop {
		err = io.EOF
		return
	}

	count, err := handle.memif.TxBurst(handle.queueId, []RawPacketData{data})

	if err != nil {
		return
	}

	if count == 0 {
		err = io.EOF
	}

	return
}

// Waits for all read and write operations to finish and then prevents more from occurring. Handle can be closed only
// once and then can never be opened again.
func (handle *MemifPacketHandle) Close() {
	handle.closeMu.Lock()
	defer handle.closeMu.Unlock()

	// wait for packet reader to stop
	handle.readMu.Lock()
	defer handle.readMu.Unlock()

	// wait for packet writer to stop
	handle.writeMu.Lock()
	defer handle.writeMu.Unlock()

	// stop reading and writing
	handle.stop = true
}