-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmulticast.go
More file actions
164 lines (137 loc) · 3.82 KB
/
Copy pathmulticast.go
File metadata and controls
164 lines (137 loc) · 3.82 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"errors"
"io"
"net/netip"
"github.com/sirupsen/logrus"
"github.com/slackhq/nebula"
"github.com/slackhq/nebula/config"
"github.com/slackhq/nebula/overlay"
)
const multicastQueueSize = 1024
type multicastDevice struct {
overlay.Device
l *logrus.Logger
targets func() []netip.Addr
queue chan []byte
}
func newMulticastDeviceFactory(c *config.C, ctrl **nebula.Control) (overlay.DeviceFactory, error) {
multicastGroups := c.GetStringSlice("multicast.groups", nil)
if tags := c.GetStringSlice("multicast.tags", nil); len(multicastGroups) == 0 && len(tags) > 0 {
multicastGroups = tags
}
if group := c.GetString("multicast.group", ""); group != "" && len(multicastGroups) == 0 {
multicastGroups = []string{group}
}
if tag := c.GetString("multicast.tag", ""); tag != "" && len(multicastGroups) == 0 {
multicastGroups = []string{tag}
}
multicastEnabled := c.GetBool("multicast.enabled", len(multicastGroups) > 0)
if !multicastEnabled {
return nil, nil
}
groupSet := make(map[string]struct{}, len(multicastGroups))
for _, group := range multicastGroups {
if group != "" {
groupSet[group] = struct{}{}
}
}
if len(groupSet) == 0 {
return nil, errors.New("multicast.enabled requires multicast.groups, multicast.group, multicast.tags, or multicast.tag")
}
return func(c *config.C, l *logrus.Logger, vpnNetworks []netip.Prefix, routines int) (overlay.Device, error) {
device, err := overlay.NewDeviceFromConfig(c, l, vpnNetworks, routines)
if err != nil {
return nil, err
}
return newMulticastDevice(device, l, func() []netip.Addr {
if *ctrl == nil {
return nil
}
return multicastTargets(*ctrl, groupSet)
}), nil
}, nil
}
func newMulticastDevice(device overlay.Device, l *logrus.Logger, targets func() []netip.Addr) *multicastDevice {
return &multicastDevice{
Device: device,
l: l,
targets: targets,
queue: make(chan []byte, multicastQueueSize),
}
}
func (d *multicastDevice) Read(p []byte) (int, error) {
for {
select {
case packet := <-d.queue:
return copy(p, packet), nil
default:
}
n, err := d.Device.Read(p)
if err != nil {
return n, err
}
if !isMulticastPacket(p[:n]) {
return n, nil
}
forwarded := d.forwardMulticast(p[:n])
if forwarded == 0 {
d.l.Debug("No multicast targets matched")
}
}
}
func (d *multicastDevice) SupportsMultiqueue() bool {
return false
}
func (d *multicastDevice) NewMultiQueueReader() (io.ReadWriteCloser, error) {
return nil, errors.New("multicast wrapper does not support multiqueue")
}
func (d *multicastDevice) forwardMulticast(packet []byte) int {
var forwarded int
for _, target := range d.targets() {
clone, ok := cloneAsUnicast(packet, target)
if !ok {
continue
}
select {
case d.queue <- clone:
forwarded++
default:
d.l.Warn("Multicast queue full, dropping forwarded packet")
return forwarded
}
}
if forwarded > 0 {
d.l.WithField("targets", forwarded).Debug("Forwarded multicast packet")
}
return forwarded
}
func multicastTargets(ctrl *nebula.Control, groups map[string]struct{}) []netip.Addr {
return multicastTargetAddrs(ctrl.ListHostmapHosts(false), groups)
}
func multicastTargetAddrs(hosts []nebula.ControlHostInfo, groups map[string]struct{}) []netip.Addr {
targets := make([]netip.Addr, 0, len(hosts))
seen := map[netip.Addr]struct{}{}
for _, host := range hosts {
if host.Cert == nil || !hasAnyGroup(host.Cert.Groups(), groups) {
continue
}
for _, addr := range host.VpnAddrs {
if _, ok := seen[addr]; ok {
continue
}
seen[addr] = struct{}{}
targets = append(targets, addr)
}
}
return targets
}
func hasAnyGroup(hostGroups []string, groups map[string]struct{}) bool {
for _, group := range hostGroups {
if _, ok := groups[group]; ok {
return true
}
}
return false
}
var _ overlay.Device = (*multicastDevice)(nil)