-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathpacket.go
113 lines (94 loc) · 3.02 KB
/
packet.go
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
// Package ndni implements NDN layer 2 and layer 3 packet representations for internal use.
package ndni
/*
#include "../csrc/ndni/packet.h"
*/
import "C"
import (
"bytes"
"unsafe"
"github.com/usnistgov/ndn-dpdk/core/cptr"
"github.com/usnistgov/ndn-dpdk/core/logging"
"github.com/usnistgov/ndn-dpdk/dpdk/pktmbuf"
"github.com/usnistgov/ndn-dpdk/ndn"
"github.com/usnistgov/ndn-dpdk/ndn/tlv"
"go.uber.org/zap"
)
var logger = logging.New("ndni")
// Packet represents a NDN network layer packet with parsed LP and Interest/Data headers.
type Packet C.Packet
// PacketFromPtr converts *C.Packet or *C.struct_rte_mbuf pointer to Packet.
func PacketFromPtr(ptr unsafe.Pointer) (pkt *Packet) {
if ptr == nil {
return nil
}
return (*Packet)(C.Packet_FromMbuf((*C.struct_rte_mbuf)(ptr)))
}
// Ptr returns *C.Packet or *C.struct_rte_mbuf pointer.
func (pkt *Packet) Ptr() unsafe.Pointer {
return unsafe.Pointer(pkt)
}
func (pkt *Packet) ptr() *C.Packet {
return (*C.Packet)(pkt)
}
// Mbuf converts to pktmbuf.Packet.
func (pkt *Packet) Mbuf() *pktmbuf.Packet {
return pktmbuf.PacketFromPtr(pkt.Ptr())
}
// Close discards the packet.
func (pkt *Packet) Close() error {
return pkt.Mbuf().Close()
}
// Type returns packet type.
func (pkt *Packet) Type() PktType {
return PktType(C.Packet_GetType(pkt.ptr()))
}
// PName returns *PName pointer.
func (pkt *Packet) PName() *PName {
return (*PName)(C.Packet_GetName(pkt.ptr()))
}
// PitToken retrieves the PIT token.
func (pkt *Packet) PitToken() (token []byte) {
tokenC := &C.Packet_GetLpL3Hdr(pkt.ptr()).pitToken
return bytes.Clone(cptr.AsByteSlice(tokenC.value[:tokenC.length]))
}
// SetPitToken updates the PIT token.
func (pkt *Packet) SetPitToken(token []byte) {
tokenC := &C.Packet_GetLpL3Hdr(pkt.ptr()).pitToken
tokenC.length = C.uint8_t(copy(cptr.AsByteSlice(tokenC.value[:]), token))
}
// ComputeDataImplicitDigest computes and stores implicit digest in *C.PData.
// Panics on non-Data.
func (pkt *Packet) ComputeDataImplicitDigest() []byte {
digest := pkt.ToNPacket().Data.ComputeDigest()
pdata := C.Packet_GetDataHdr(pkt.ptr())
copy(cptr.AsByteSlice(pdata.digest[:]), digest)
pdata.hasDigest = true
return digest
}
// Clone clones this packet to new mbufs, with specified alignment requirement.
// Returns nil upon allocation error.
func (pkt *Packet) Clone(mp *Mempools, align PacketTxAlign) *Packet {
m := C.Packet_Clone(pkt.ptr(), (*C.PacketMempools)(mp), *(*C.PacketTxAlign)(unsafe.Pointer(&align)))
return PacketFromPtr(unsafe.Pointer(m))
}
// ToNPacket copies this packet into ndn.Packet.
// Panics on error.
func (pkt *Packet) ToNPacket() (npkt ndn.Packet) {
wire := pkt.Mbuf().Bytes()
e := tlv.Decode(wire, &npkt)
if e != nil {
logger.Panic("tlv.Decode",
zap.Error(e),
zap.Binary("wire", wire),
)
}
lpl3 := C.Packet_GetLpL3Hdr(pkt.ptr())
npkt.Lp.PitToken = pkt.PitToken()
npkt.Lp.NackReason = uint8(lpl3.nackReason)
npkt.Lp.CongMark = uint8(lpl3.congMark)
if npkt.Lp.NackReason != 0 {
return *ndn.MakeNack(npkt.Interest, npkt.Lp.NackReason).ToPacket()
}
return npkt
}