|
| 1 | +// Copyright 2025 The gVisor Authors. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package inet |
| 16 | + |
| 17 | +import ( |
| 18 | + "gvisor.dev/gvisor/pkg/abi/linux" |
| 19 | + "gvisor.dev/gvisor/pkg/context" |
| 20 | +) |
| 21 | + |
| 22 | +const ( |
| 23 | + routeProtocol = linux.NETLINK_ROUTE |
| 24 | + routeLinkMcastGroup = linux.RTNLGRP_LINK |
| 25 | +) |
| 26 | + |
| 27 | +// InterfaceEventSubscriber allows clients to subscribe to events published by an inet.Stack. |
| 28 | +// |
| 29 | +// It is a rough parallel to the objects in Linux that subscribe to netdev |
| 30 | +// events by calling register_netdevice_notifier(). |
| 31 | +type InterfaceEventSubscriber interface { |
| 32 | + // OnInterfaceChangeEvent is called by InterfaceEventPublishers when an interface event takes place. |
| 33 | + OnInterfaceChangeEvent(ctx context.Context, idx int32, i Interface) |
| 34 | + |
| 35 | + // OnInterfaceDeleteEvent is called by InterfaceEventPublishers when an interface event takes place. |
| 36 | + OnInterfaceDeleteEvent(ctx context.Context, idx int32, i Interface) |
| 37 | +} |
| 38 | + |
| 39 | +// InterfaceEventPublisher is the interface event publishing aspect of an inet.Stack. |
| 40 | +// |
| 41 | +// The Linux parallel is how it notifies subscribers via call_netdev_notifiers(). |
| 42 | +type InterfaceEventPublisher interface { |
| 43 | + AddInterfaceEventSubscriber(sub InterfaceEventSubscriber) |
| 44 | +} |
| 45 | + |
| 46 | +// NetlinkSocket corresponds to a netlink socket. |
| 47 | +type NetlinkSocket interface { |
| 48 | + // Protocol returns the netlink protocol value. |
| 49 | + Protocol() int |
| 50 | + |
| 51 | + // Groups returns the bitmap of multicast groups the socket is bound to. |
| 52 | + Groups() uint64 |
| 53 | + |
| 54 | + // HandleInterfaceChangeEvent is called on NetlinkSockets that are members of the RTNLGRP_LINK |
| 55 | + // multicast group when an interface is modified. |
| 56 | + HandleInterfaceChangeEvent(context.Context, int32, Interface) |
| 57 | + |
| 58 | + // HandleInterfaceDeleteEvent is called on NetlinkSockets that are members of the RTNLGRP_LINK |
| 59 | + // multicast group when an interface is deleted. |
| 60 | + HandleInterfaceDeleteEvent(context.Context, int32, Interface) |
| 61 | +} |
| 62 | + |
| 63 | +// McastTable holds multicast group membership information for netlink netlinkSocket. |
| 64 | +// It corresponds roughly to Linux's struct netlink_table. |
| 65 | +// |
| 66 | +// +stateify savable |
| 67 | +type McastTable struct { |
| 68 | + mu nlmcastTableMutex `state:"nosave"` |
| 69 | + socks map[int]map[NetlinkSocket]struct{} |
| 70 | +} |
| 71 | + |
| 72 | +// WithTableLocked runs fn with the table mutex held. |
| 73 | +func (m *McastTable) WithTableLocked(fn func()) { |
| 74 | + m.mu.Lock() |
| 75 | + defer m.mu.Unlock() |
| 76 | + fn() |
| 77 | +} |
| 78 | + |
| 79 | +// AddSocket adds a netlinkSocket to the multicast-group table. |
| 80 | +// |
| 81 | +// Preconditions: the netlink multicast table is locked. |
| 82 | +func (m *McastTable) AddSocket(s NetlinkSocket) { |
| 83 | + p := s.Protocol() |
| 84 | + if _, ok := m.socks[p]; !ok { |
| 85 | + m.socks[p] = make(map[NetlinkSocket]struct{}) |
| 86 | + } |
| 87 | + if _, ok := m.socks[p][s]; ok { |
| 88 | + return |
| 89 | + } |
| 90 | + m.socks[p][s] = struct{}{} |
| 91 | +} |
| 92 | + |
| 93 | +// RemoveSocket removes a netlinkSocket from the multicast-group table. |
| 94 | +// |
| 95 | +// Preconditions: the netlink multicast table is locked. |
| 96 | +func (m *McastTable) RemoveSocket(s NetlinkSocket) { |
| 97 | + p := s.Protocol() |
| 98 | + if _, ok := m.socks[p]; !ok { |
| 99 | + return |
| 100 | + } |
| 101 | + if _, ok := m.socks[p][s]; !ok { |
| 102 | + return |
| 103 | + } |
| 104 | + delete(m.socks[p], s) |
| 105 | +} |
| 106 | + |
| 107 | +func (m *McastTable) forEachMcastSock(protocol int, mcastGroup int, fn func(s NetlinkSocket)) { |
| 108 | + m.mu.Lock() |
| 109 | + defer m.mu.Unlock() |
| 110 | + if _, ok := m.socks[protocol]; !ok { |
| 111 | + return |
| 112 | + } |
| 113 | + for s := range m.socks[protocol] { |
| 114 | + if s.Groups()&(1<<(mcastGroup-1)) == 0 { |
| 115 | + return |
| 116 | + } |
| 117 | + fn(s) |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +// OnInterfaceChangeEvent implements InterfaceEventSubscriber.OnInterfaceChangeEvent. |
| 122 | +func (m *McastTable) OnInterfaceChangeEvent(ctx context.Context, idx int32, i Interface) { |
| 123 | + // Relay the event to RTNLGRP_LINK subscribers. |
| 124 | + m.forEachMcastSock(routeProtocol, routeLinkMcastGroup, func(s NetlinkSocket) { |
| 125 | + s.HandleInterfaceChangeEvent(ctx, idx, i) |
| 126 | + }) |
| 127 | +} |
| 128 | + |
| 129 | +// OnInterfaceDeleteEvent implements InterfaceEventSubscriber.OnInterfaceDeleteEvent. |
| 130 | +func (m *McastTable) OnInterfaceDeleteEvent(ctx context.Context, idx int32, i Interface) { |
| 131 | + // Relay the event to RTNLGRP_LINK subscribers. |
| 132 | + m.forEachMcastSock(routeProtocol, routeLinkMcastGroup, func(s NetlinkSocket) { |
| 133 | + s.HandleInterfaceDeleteEvent(ctx, idx, i) |
| 134 | + }) |
| 135 | +} |
| 136 | + |
| 137 | +// NewNetlinkMcastTable creates a new McastTable. |
| 138 | +func NewNetlinkMcastTable() *McastTable { |
| 139 | + return &McastTable{ |
| 140 | + socks: make(map[int]map[NetlinkSocket]struct{}), |
| 141 | + } |
| 142 | +} |
0 commit comments