/
transport.go
116 lines (101 loc) · 2.97 KB
/
transport.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
114
115
116
// Package afpacket implements a transport that communicates over AF_PACKET sockets.
// This only works on Linux.
package afpacket
import (
"encoding/binary"
"fmt"
"net"
"reflect"
af "github.com/google/gopacket/afpacket"
"github.com/usnistgov/ndn-dpdk/core/macaddr"
"github.com/usnistgov/ndn-dpdk/ndn/l3"
"github.com/usnistgov/ndn-dpdk/ndn/packettransport"
"golang.org/x/sys/unix"
)
// Config contains Transport configuration.
type Config struct {
packettransport.Config
}
// Transport is an l3.Transport that communicates over AF_PACKET sockets.
type Transport interface {
packettransport.Transport
// Intf returns the underlying network interface.
Intf() net.Interface
}
// New creates a Transport.
func New(ifname string, cfg Config) (Transport, error) {
intf, e := net.InterfaceByName(ifname)
if e != nil {
return nil, fmt.Errorf("net.InterfaceByName(%s) %w", ifname, e)
}
if cfg.Local.Empty() {
cfg.Local.HardwareAddr = intf.HardwareAddr
}
if cfg.Remote.Empty() {
cfg.Remote.HardwareAddr = packettransport.MulticastAddressNDN
}
h, e := af.NewTPacket()
if e != nil {
return nil, fmt.Errorf("af.NewTPacket() %w", e)
}
tr := &transport{
intf: *intf,
}
if e = tr.prepare(h, cfg.Locator); e != nil {
return nil, e
}
tr.Transport, e = packettransport.New(h, cfg.Config)
if e != nil {
return nil, e
}
tr.Transport.OnStateChange(func(st l3.TransportState) {
if st == l3.TransportClosed {
h.Close()
}
})
return tr, nil
}
type transport struct {
packettransport.Transport
h *af.TPacket
intf net.Interface
}
func (tr *transport) prepare(h *af.TPacket, loc packettransport.Locator) error {
fd := int(reflect.ValueOf(h).Elem().FieldByName("fd").Int())
ifindex := tr.intf.Index
ethtype := make([]byte, 2)
binary.BigEndian.PutUint16(ethtype, packettransport.EthernetTypeNDN)
sockaddr := unix.SockaddrLinklayer{
Protocol: binary.LittleEndian.Uint16(ethtype),
Ifindex: ifindex,
}
if e := unix.Bind(fd, &sockaddr); e != nil {
return fmt.Errorf("bind(fd=%d, ifindex=%d) %w", fd, ifindex, e)
}
if macaddr.IsMulticast(loc.Remote.HardwareAddr) {
mreq := unix.PacketMreq{
Ifindex: int32(ifindex),
Type: unix.PACKET_MR_MULTICAST,
}
mreq.Alen = uint16(copy(mreq.Address[:], []byte(loc.Remote.HardwareAddr)))
if e := unix.SetsockoptPacketMreq(fd, unix.SOL_PACKET, unix.PACKET_ADD_MEMBERSHIP, &mreq); e != nil {
return fmt.Errorf("setsockopt(fd=%d, ifindex=%d, PACKET_ADD_MEMBERSHIP=%s) %w", fd, ifindex, loc.Remote, e)
}
} else if !macaddr.Equal(loc.Local.HardwareAddr, tr.intf.HardwareAddr) {
mreq := unix.PacketMreq{
Ifindex: int32(ifindex),
Type: unix.PACKET_MR_PROMISC,
}
if e := unix.SetsockoptPacketMreq(fd, unix.SOL_PACKET, unix.PACKET_ADD_MEMBERSHIP, &mreq); e != nil {
return fmt.Errorf("setsockopt(fd=%d, ifindex=%d, PACKET_ADD_MEMBERSHIP=PROMISC) %w", fd, ifindex, e)
}
}
return nil
}
func (tr *transport) Intf() net.Interface {
return tr.intf
}
func (tr *transport) Close() error {
tr.h.Close()
return nil
}