Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions docs/cni/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,34 @@ All nodes in the same VPC derive the same BGP Route Target by truncating the
configuration. The RT is also used as the `BGPVRFInstance`'s Route
Distinguisher and import/export Route Target.

### SRv6 ingress source filter

`usid_ingress` can check each claimed packet's outer source before any tenant
delivery. The check runs only after the destination matches `locator_table`,
so traffic not addressed to this node's locator (BGP, BFD, everything else)
never reaches it. The source must match an allowed prefix, and unless that
entry accepts any interface, the packet must arrive on an uplink whose slot bit
is set in the entry's mask.

- `off` (the default, and what a never-written config reads as) is a single
array read with no other effect. `audit` counts and records rejections but
still delivers. `enforce` drops them.
- Until the control plane marks the allow-list populated, every packet passes
and is counted as a bypass, so the filter cannot drop traffic before its
first complete sync.
- All state lives in new maps, so upgrading never trips the loader's
incompatible-pin recreation of the existing uSID maps.
- `internal/plumbing/ebpf/srcfiltermap` is the typed read/write API over these
maps, with an in-memory fake for tests.

| Map | Type | Holds |
|---------------------------|-------------------|--------------------------------------------------------------------------------------|
| `src_allow_table` | LPM trie (4096) | Allowed outer source prefixes, each with an uplink bitmask and an any-interface flag |
| `uplink_slot_table` | Hash (64) | Uplink ifindex to its bit (0-31) in the bitmask |
| `src_filter_config_table` | Array (1) | Mode (`off`/`audit`/`enforce`), populated flag, generation |
| `src_filter_stats` | Per-CPU array (8) | Checked, allowed, denied by prefix, denied by interface, bypassed before first sync |
| `src_filter_denied` | LRU hash (1024) | Denied source /64s with count, last interface, last reason and time, for triage |

---

## Data flow
Expand Down
2 changes: 2 additions & 0 deletions internal/plumbing/ebpf/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
// external event silently clearing the filter, requires it.
// - usidmap: the read/write API that populates and reconciles the three
// control-plane maps, used by the CNI ADD path and by garbage collection.
// - srcfiltermap: the read/write API for the SRv6 ingress source filter's
// allow-list, uplink slots, mode, counters, and denied-source record.
// - metrics: Prometheus metrics and health event hooks spanning the whole
// datapath.
//
Expand Down
2 changes: 1 addition & 1 deletion internal/plumbing/ebpf/prog/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ package prog
// -cc is deliberately omitted: bpf2go defaults to $BPF2GO_CC or "clang", so CI
// can pin an exact version without this directive changing.
//
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -cflags "-O2 -g -Wall -idirafter /usr/include/x86_64-linux-gnu -idirafter /usr/include/aarch64-linux-gnu" -target bpfel,bpfeb -type locator_value -type function_value -type vrf_value -type ifindex_vrf_value -type nptv6_value -type vip_xlat_range -type vip_xlat_key -type vip_xlat_value -type egress_route_key -type egress_route_value -type service_route_key -type service_route_value -type service_access_key -type service_access_value -type service_deny_key -type service_deny_value -type service_reverse_key -type service_reverse_value -type service_remote_grant_key -type service_remote_grant_value -type service_remote_target_value -type service_peer_key -type service_peer_value -type service_policy_state_value -type public_uplink_value -type egress_shard_group_value -type egress_shard_member -type egress_shard_pin_key -type egress_shard_pin_value -type egress_shard_counter -type mss_clamp_value -type tenant_gw_value -type host_only_ports_value -type vpc_attribution_value -type vrf_direction_counter -type vrf_traffic_value -type tp_state_value -type tp_iface_value -type tp_iface_addr_key -type tp_binding_key -type tp_entry -type tp_header -type tp_counter -type tp_fallback_key Usid usid.c
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -cflags "-O2 -g -Wall -idirafter /usr/include/x86_64-linux-gnu -idirafter /usr/include/aarch64-linux-gnu" -target bpfel,bpfeb -type locator_value -type function_value -type vrf_value -type ifindex_vrf_value -type nptv6_value -type vip_xlat_range -type vip_xlat_key -type vip_xlat_value -type egress_route_key -type egress_route_value -type service_route_key -type service_route_value -type service_access_key -type service_access_value -type service_deny_key -type service_deny_value -type service_reverse_key -type service_reverse_value -type service_remote_grant_key -type service_remote_grant_value -type service_remote_target_value -type service_peer_key -type service_peer_value -type service_policy_state_value -type public_uplink_value -type egress_shard_group_value -type egress_shard_member -type egress_shard_pin_key -type egress_shard_pin_value -type egress_shard_counter -type mss_clamp_value -type tenant_gw_value -type host_only_ports_value -type vpc_attribution_value -type vrf_direction_counter -type vrf_traffic_value -type tp_state_value -type tp_iface_value -type tp_iface_addr_key -type tp_binding_key -type tp_entry -type tp_header -type tp_counter -type tp_fallback_key -type src_allow_key -type src_allow_value -type src_filter_config -type src_denied_key -type src_denied_value Usid usid.c
37 changes: 37 additions & 0 deletions internal/plumbing/ebpf/prog/srcfilter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2026 Datum Cloud, Inc.
//
// SPDX-License-Identifier: AGPL-3.0-or-later

package prog

// Source filter constants mirroring usid.c's enum src_filter_mode,
// SRCF_ANY_IFACE, SRC_FILTER_MAX_SLOTS and enum src_filter_stat. Hand-kept in
// sync with the C source for the same reason as the drop reasons: the generator
// produces no Go symbol for an enum used only as a literal or for a #define.
const (
SrcFilterModeOff uint32 = 0
SrcFilterModeAudit uint32 = 1
SrcFilterModeEnforce uint32 = 2

SrcFilterFlagAnyIface uint32 = 1 << 0

SrcFilterMaxSlots = 32

SrcFilterStatChecked uint32 = 0
SrcFilterStatAllowed uint32 = 1
SrcFilterStatDenyPrefix uint32 = 2
SrcFilterStatDenyIface uint32 = 3
SrcFilterStatBypassUnpopulated uint32 = 4
SrcFilterStatCount uint32 = 5
SrcFilterStatSlots uint32 = 8
)

// SrcFilterStatNames maps each src_filter_stats index to a short, stable,
// metrics-friendly name.
var SrcFilterStatNames = map[uint32]string{
SrcFilterStatChecked: "checked",
SrcFilterStatAllowed: "allowed",
SrcFilterStatDenyPrefix: "deny_prefix",
SrcFilterStatDenyIface: "deny_iface",
SrcFilterStatBypassUnpopulated: "bypass_unpopulated",
}
227 changes: 223 additions & 4 deletions internal/plumbing/ebpf/prog/usid.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@
// usid.c implements the TC-BPF ingress datapath for the uFMT 48+16 SRv6 uSID
// carrier format.
//
// Packet path. Every lookup is an exact hash match; nothing here matches looser
// than a full /64, and none of the lookup maps below is an LPM trie:
// Packet path. Every uSID decode lookup is an exact hash match; nothing in
// steps 2 through 6 matches looser than a full /64. The source filter at step
// 2a is the one longest-prefix match on this path:
//
// 1. Parse the outer Ethernet and IPv6 header, bounds-checked. Not IPv6, or
// too short to parse: TC_ACT_UNSPEC, handed to the next tc filter
// unmodified.
// 2. Exact-match the destination's top 64 bits, Block and Node-ID read with no
// shift, against locator_table. No match: TC_ACT_UNSPEC.
// 2a. Source filter, when enabled: the outer source must fall inside an
// allowed fabric prefix, and the packet must arrive on an uplink that
// prefix is bound to. A rejected packet is dropped in enforce mode and
// only counted in audit mode. See src_filter_config_table.
// 3. Read Function from the unmutated packet at its fixed offset.
// 4. Exact-match (Block, Function) against function_table. No match: drop,
// counted, because step 2 already claimed this packet and passing it
Expand Down Expand Up @@ -1523,8 +1528,7 @@ struct {
__type(value, struct vip_xlat_key);
} vip_xlat_key_scratch SEC(".maps");

// egress_route_table: see struct egress_route_key. The only LPM trie in this
// file. Everything above is a hash map because uSID decode is always a
// egress_route_table: see struct egress_route_key. uSID decode uses a
// fixed-width exact match, but egress routing is inherently a longest-prefix
// problem: an intra-VPC peer's specific prefix must win over a VRF's ::/0
// default. BPF_F_NO_PREALLOC is required by the kernel for this map type, not a
Expand Down Expand Up @@ -2021,6 +2025,117 @@ struct {
__type(value, struct pmtu_icmp_bucket);
} pmtu_icmp_bucket SEC(".maps");

// ---------------------------------------------------------------------
// SRv6 ingress source filter, step 2a. Every map here is new rather than a
// field on an existing one, so the loader never treats an upgrade as an
// incompatible pin and recreates the uSID decode maps.
// ---------------------------------------------------------------------

// enum src_filter_mode is src_filter_config.mode. Any value other than
// SRC_FILTER_MODE_ENFORCE counts but never drops.
enum src_filter_mode {
SRC_FILTER_MODE_OFF = 0,
SRC_FILTER_MODE_AUDIT = 1,
SRC_FILTER_MODE_ENFORCE = 2,
};

// SRCF_ANY_IFACE marks an allow entry that is accepted on any interface,
// skipping the uplink binding check.
#define SRCF_ANY_IFACE (1U << 0)

// SRC_FILTER_MAX_SLOTS is the number of uplink slots iface_mask can name.
#define SRC_FILTER_MAX_SLOTS 32

// struct src_allow_key keys src_allow_table. prefixlen counts only addr's
// significant bits; a packet lookup uses 128.
struct src_allow_key {
__u32 prefixlen;
__u8 addr[16];
};

// struct src_allow_value is one allowed source prefix: iface_mask has bit N set
// for every uplink whose uplink_slot_table slot is N, and flags holds SRCF_*.
struct src_allow_value {
__u32 iface_mask;
__u32 flags;
};

// struct src_filter_config is src_filter_config_table's single value.
// populated stays zero until the control plane completes its first full sync
// of src_allow_table; until then every packet passes, counted as a bypass.
// generation is written by the control plane and never read here.
struct src_filter_config {
__u32 mode;
__u32 populated;
__u32 generation;
};

// enum src_filter_stat indexes src_filter_stats. Slots past
// SRC_FILTER_STAT_BYPASS_UNPOPULATED are reserved.
enum src_filter_stat {
SRC_FILTER_STAT_CHECKED = 0,
SRC_FILTER_STAT_ALLOWED = 1,
SRC_FILTER_STAT_DENY_PREFIX = 2,
SRC_FILTER_STAT_DENY_IFACE = 3,
SRC_FILTER_STAT_BYPASS_UNPOPULATED = 4,
__SRC_FILTER_STAT_SLOTS = 8,
};

// struct src_denied_key keys src_filter_denied: the top 64 bits of a denied
// outer source, in wire order.
struct src_denied_key {
__u8 prefix[8];
};

// struct src_denied_value records one denied source /64 for triage. last_reason
// is the enum src_filter_stat deny slot of the most recent denial.
struct src_denied_value {
__u64 count;
__u64 last_ns;
__u32 last_ifindex;
__u32 last_reason;
};

// src_allow_table: allowed outer source prefixes. BPF_F_NO_PREALLOC is required
// by the kernel for this map type.
struct {
__uint(type, BPF_MAP_TYPE_LPM_TRIE);
__uint(map_flags, BPF_F_NO_PREALLOC);
__uint(max_entries, 4096);
__type(key, struct src_allow_key);
__type(value, struct src_allow_value);
} src_allow_table SEC(".maps");

// uplink_slot_table: uplink ifindex to its bit position in iface_mask.
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, 64);
__type(key, __u32);
__type(value, __u32);
} uplink_slot_table SEC(".maps");

struct {
__uint(type, BPF_MAP_TYPE_ARRAY);
__uint(max_entries, 1);
__type(key, __u32);
__type(value, struct src_filter_config);
} src_filter_config_table SEC(".maps");

struct {
__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
__uint(max_entries, __SRC_FILTER_STAT_SLOTS);
__type(key, __u32);
__type(value, __u64);
} src_filter_stats SEC(".maps");

// src_filter_denied: a bounded, least-recently-used record of denied sources.
struct {
__uint(type, BPF_MAP_TYPE_LRU_HASH);
__uint(max_entries, 1024);
__type(key, struct src_denied_key);
__type(value, struct src_denied_value);
} src_filter_denied SEC(".maps");

// ---------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------
Expand Down Expand Up @@ -3021,6 +3136,104 @@ static USID_ALWAYS_INLINE void count_claimed_drop(__u32 reason, struct vrf_value
c->dropped_packets++;
}

static USID_ALWAYS_INLINE void count_src_filter(__u32 stat)
{
__u64 *count = bpf_map_lookup_elem(&src_filter_stats, &stat);

if (count)
__sync_fetch_and_add(count, 1);
}

static USID_ALWAYS_INLINE void record_src_denial(const struct src_allow_key *src, __u32 ifindex,
__u32 reason)
{
struct src_denied_key key;
__u64 now = bpf_ktime_get_ns();

__builtin_memcpy(key.prefix, src->addr, sizeof(key.prefix));

struct src_denied_value *seen = bpf_map_lookup_elem(&src_filter_denied, &key);

if (seen) {
__sync_fetch_and_add(&seen->count, 1);
seen->last_ns = now;
seen->last_ifindex = ifindex;
seen->last_reason = reason;
return;
}

struct src_denied_value fresh = {
.count = 1,
.last_ns = now,
.last_ifindex = ifindex,
.last_reason = reason,
};

bpf_map_update_elem(&src_filter_denied, &key, &fresh, BPF_NOEXIST);
}

// src_filter_allows reports whether an outer source may deliver into a tenant
// network through the uplink at ifindex, counting the decision. An unpopulated
// filter allows everything, counted as a bypass. A denial is also recorded in
// src_filter_denied.
static USID_ALWAYS_INLINE int src_filter_allows(const struct src_filter_config *cfg,
const __u8 *saddr, __u32 ifindex)
{
count_src_filter(SRC_FILTER_STAT_CHECKED);

if (!cfg->populated) {
count_src_filter(SRC_FILTER_STAT_BYPASS_UNPOPULATED);
return 1;
}

struct src_allow_key key = { .prefixlen = 128 };

__builtin_memcpy(key.addr, saddr, sizeof(key.addr));

struct src_allow_value *allow = bpf_map_lookup_elem(&src_allow_table, &key);

if (!allow) {
count_src_filter(SRC_FILTER_STAT_DENY_PREFIX);
record_src_denial(&key, ifindex, SRC_FILTER_STAT_DENY_PREFIX);
return 0;
}

if (!(allow->flags & SRCF_ANY_IFACE)) {
__u32 *slot = bpf_map_lookup_elem(&uplink_slot_table, &ifindex);

if (!slot || *slot >= SRC_FILTER_MAX_SLOTS ||
!(allow->iface_mask & (1U << (*slot & (SRC_FILTER_MAX_SLOTS - 1))))) {
count_src_filter(SRC_FILTER_STAT_DENY_IFACE);
record_src_denial(&key, ifindex, SRC_FILTER_STAT_DENY_IFACE);
return 0;
}
}

count_src_filter(SRC_FILTER_STAT_ALLOWED);
return 1;
}

// Verify the source-filter decision once. Its individual audit and allow paths
// return the same scalar verdict to the rest of the ingress program.
USID_NOINLINE int src_filter_drops(struct __sk_buff *skb)
{
__u32 zero = 0;
struct src_filter_config *cfg = bpf_map_lookup_elem(&src_filter_config_table, &zero);

if (!cfg || cfg->mode == SRC_FILTER_MODE_OFF)
return 0;

// The caller has already matched the outer IPv6 locator. A global BPF
// function must establish its own packet bounds before reading the source.
void *data = (void *) (long) skb->data;
void *data_end = (void *) (long) skb->data_end;
struct usid_ip6hdr *ip6 = data + sizeof(struct usid_ethhdr);

if ((void *) (ip6 + 1) > data_end)
return cfg->mode == SRC_FILTER_MODE_ENFORCE;
return !src_filter_allows(cfg, ip6->saddr, skb->ifindex) && cfg->mode == SRC_FILTER_MODE_ENFORCE;
}

// read_be64 composes an 8-byte big-endian buffer into a host-native u64 byte by
// byte, rather than casting: p is not guaranteed 8-byte aligned, pointing 38
// bytes into the packet, and some architectures reject unaligned wide loads at
Expand Down Expand Up @@ -4288,6 +4501,12 @@ int usid_ingress(struct __sk_buff *skb)
return TC_ACT_UNSPEC;
}

// Step 2a: source filter. Only packets already claimed by step 2 reach
// it, so traffic for any other destination is untouched. With the filter
// off this is a single array read.
if (src_filter_drops(skb))
return TC_ACT_SHOT;

__u64 block = locator_key >> 16;

// Step 3: read Function from the unmutated packet at its fixed offset, the
Expand Down
Loading
Loading