2 *------------------------------------------------------------------
3 * Copyright (c) 2020 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
22 // ReadPacket reads one packet form the shared memory and
23 // returns the number of bytes read
24 func (q *Queue) ReadPacket(pkt []byte) (int, error) {
25 var mask int = q.ring.size - 1
32 var desc descBuf = newDescBuf()
34 if q.i.args.IsMaster {
35 slot = int(q.lastHead)
36 lastSlot = q.readHead()
38 slot = int(q.lastTail)
39 lastSlot = q.readTail()
42 nSlots = uint16(lastSlot - slot)
47 // copy descriptor from shm
48 q.getDescBuf(slot&mask, desc)
49 length = desc.getLength()
50 offset = desc.getOffset()
52 copy(pkt[:], q.i.regions[desc.getRegion()].data[offset:offset+length])
58 for (desc.getFlags() & descFlagNext) == descFlagNext {
60 return 0, fmt.Errorf("Incomplete chained buffer, may suggest peer error.")
63 q.getDescBuf(slot&mask, desc)
64 length = desc.getLength()
65 offset = desc.getOffset()
67 copy(pkt[pktOffset:], q.i.regions[desc.getRegion()].data[offset:offset+length])
75 if q.i.args.IsMaster {
76 q.lastHead = uint16(slot)
79 q.lastTail = uint16(slot)
83 for nSlots := uint16(q.ring.size - head + int(q.lastTail)); nSlots > 0; nSlots-- {
84 q.setDescLength(head&mask, int(q.i.run.PacketBufferSize))