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 *------------------------------------------------------------------
20 // WritePacket writes one packet to the shared memory and
21 // returns the number of bytes written
22 func (q *Queue) WritePacket(pkt []byte) int {
23 var mask int = q.ring.size - 1
26 var packetBufferSize int = int(q.i.run.PacketBufferSize)
28 if q.i.args.IsMaster {
30 nFree = uint16(q.readHead() - slot)
33 nFree = uint16(q.ring.size - slot + q.readTail())
41 // copy descriptor from shm
43 q.getDescBuf(slot&mask, desc)
47 if q.i.args.IsMaster {
48 packetBufferSize = desc.getLength()
51 offset := desc.getOffset()
53 // write packet into memif buffer
54 n := copy(q.i.regions[desc.getRegion()].data[offset:offset+packetBufferSize], pkt[:])
62 desc.setFlags(descFlagNext)
63 q.putDescBuf(slot&mask, desc)
66 // copy descriptor from shm
67 q.getDescBuf(slot&mask, desc)
71 if q.i.args.IsMaster {
72 packetBufferSize = desc.getLength()
75 offset := desc.getOffset()
77 tmp := copy(q.i.regions[desc.getRegion()].data[offset:offset+packetBufferSize], pkt[:])
82 // copy descriptor to shm
83 q.putDescBuf(slot&mask, desc)
86 if q.i.args.IsMaster {