-
Notifications
You must be signed in to change notification settings - Fork 2
/
etherconn_linux_test.go
90 lines (80 loc) · 2.24 KB
/
etherconn_linux_test.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
//go:build linux
// +build linux
// etherconn_test
/*
Test setup reuqirements:
- two interfaces name specified by argument testifA and testifB, these two interfaces are connected together
*/
package etherconn_test
import (
"context"
"fmt"
"github.com/google/gopacket/layers"
"github.com/hujun-open/etherconn"
"github.com/vishvananda/netlink"
)
const (
testifA = "A"
testifB = "B"
)
func testCreateVETHLink(a, b string) error {
linka := new(netlink.Veth)
linka.Name = a
linka.PeerName = b
netlink.LinkDel(linka)
err := netlink.LinkAdd(linka)
if err != nil {
return err
}
linkb, err := netlink.LinkByName(b)
if err != nil {
return err
}
err = netlink.LinkSetUp(linka)
if err != nil {
return err
}
err = netlink.LinkSetUp(linkb)
if err != nil {
return err
}
return nil
}
func getRawRelay(ctx context.Context, relayType etherconn.RelayType, ifname string, mods ...etherconn.RelayOption) (*etherconn.RawSocketRelay, error) {
switch relayType {
case etherconn.RelayTypePCAP:
return etherconn.NewRawSocketRelayPcap(ctx, ifname, mods...)
case etherconn.RelayTypeAFP:
return etherconn.NewRawSocketRelay(ctx, ifname, mods...)
}
return nil, fmt.Errorf("%v is not a supported raw relay type", relayType)
}
func getPKTRelay(ctx context.Context, rtype etherconn.RelayType, ifname string, defaultconn, defualtconnmirror bool) (etherconn.PacketRelay, error) {
switch rtype {
case etherconn.RelayTypeAFP, etherconn.RelayTypePCAP:
mods := []etherconn.RelayOption{
etherconn.WithDebug(true),
}
if defaultconn {
mods = append(mods, etherconn.WithDefaultReceival(defualtconnmirror))
}
return getRawRelay(ctx, rtype, ifname, mods...)
case etherconn.RelayTypeXDP:
mods := []etherconn.XDPRelayOption{
etherconn.WithQueueID([]int{0}),
etherconn.WithXDPUMEMNumOfTrunk(32768),
// etherconn.WithXDPPerClntRecvChanDepth(32768),
etherconn.WithXDPDebug(true),
etherconn.WithXDPUMEMChunkSize(4096),
etherconn.WithXDPEtherTypes([]uint16{
uint16(layers.EthernetTypeIPv4),
uint16(layers.EthernetTypeIPv6),
}),
}
if defaultconn {
mods = append(mods, etherconn.WithXDPDefaultReceival(defualtconnmirror))
}
return etherconn.NewXDPRelay(ctx, ifname, mods...)
}
return nil, fmt.Errorf("unknown relay type %v", rtype)
}