-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
140 lines (113 loc) · 2.91 KB
/
main.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// SPDX-License-Identifier: Apache-2.0
// Copyright Yutaro Hayakawa
package main
import (
"context"
"errors"
"fmt"
"log"
"os"
"github.com/cilium/ebpf"
"github.com/cilium/ebpf/rlimit"
"github.com/vishvananda/netlink"
"golang.org/x/sys/unix"
)
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -cflags "-Wno-int-conversion -DBPF_NO_GLOBAL_DATA" ingress bpf/tc-ingress.bpf.c
func syncVrfTable2Ifindex(m *ebpf.Map, ctx context.Context) {
updateCh := make(chan netlink.LinkUpdate)
opt := netlink.LinkSubscribeOptions{
ListExisting: true,
}
if err := netlink.LinkSubscribeWithOptions(updateCh, ctx.Done(), opt); err != nil {
panic(err)
}
for update := range updateCh {
vrf, ok := update.Link.(*netlink.Vrf)
if !ok {
continue
}
// Sync VRF Table ID => VRF Ifindex mapping
switch update.Header.Type {
case unix.RTM_NEWLINK:
log.Printf("VRF Update: %s (table: %d, ifindex: %d)",
vrf.Name, vrf.Table, vrf.Index)
if err := m.Update(uint32(vrf.Table), uint32(vrf.Index), 0); err != nil {
panic(err)
}
case unix.RTM_DELLINK:
log.Printf("VRF Delete: %s (table: %d, ifindex: %d)",
vrf.Name, vrf.Table, vrf.Index)
if err := m.Delete(uint32(vrf.Table)); err != nil {
panic(err)
}
}
}
}
func attachTCIngress(link netlink.Link, prog *ebpf.Program) error {
attrs := netlink.QdiscAttrs{
LinkIndex: link.Attrs().Index,
Handle: netlink.MakeHandle(0xffff, 0),
Parent: netlink.HANDLE_CLSACT,
}
qdisc := &netlink.GenericQdisc{
QdiscAttrs: attrs,
QdiscType: "clsact",
}
if err := netlink.QdiscReplace(qdisc); err != nil {
return err
}
filter := &netlink.BpfFilter{
FilterAttrs: netlink.FilterAttrs{
LinkIndex: link.Attrs().Index,
Parent: netlink.HANDLE_MIN_INGRESS,
Handle: 1,
Protocol: unix.ETH_P_ALL,
},
Fd: prog.FD(),
DirectAction: true,
}
if err := netlink.FilterReplace(filter); err != nil {
return fmt.Errorf("failed to replace tc ingress: %w", err)
}
log.Printf("Attached TC Qdisc and BPF program to %s", link.Attrs().Name)
return nil
}
func main() {
if len(os.Args) == 1 {
fmt.Printf("Usage: %s <iface0> <iface1> ...\n", os.Args[0])
return
}
if err := rlimit.RemoveMemlock(); err != nil {
log.Fatal(err)
}
objs := ingressObjects{}
opts := ebpf.CollectionOptions{
Maps: ebpf.MapOptions{
PinPath: "/sys/fs/bpf",
},
Programs: ebpf.ProgramOptions{
LogLevel: ebpf.LogLevelInstruction | ebpf.LogLevelStats,
LogSize: 0xffff,
},
}
if err := loadIngressObjects(&objs, &opts); err != nil {
var verr *ebpf.VerifierError
if errors.As(err, &verr) {
for _, s := range verr.Log {
log.Println(s)
}
}
panic(err)
}
defer objs.Close()
for _, arg := range os.Args[1:] {
link, err := netlink.LinkByName(arg)
if err != nil {
panic(err)
}
if err := attachTCIngress(link, objs.IngressMain); err != nil {
panic(err)
}
}
syncVrfTable2Ifindex(objs.Vrftable2ifindex, context.TODO())
}