-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix(cubenet): resolve direct on-link neighbors via ARP #1321
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 3 commits
b88b328
f5267bb
8afbd05
a5538f8
0d1bccf
1699a2d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -57,6 +57,7 @@ func rewriteConstants(vars map[string]*ebpf.VariableSpec, params Params) error { | |
| err = errors.Join(err, v.Set(params.EgressRedirectFlags)) | ||
| } | ||
| err = errors.Join(err, vars[globalNameNodeIP].Set(ipToUint32(params.NodeIP))) | ||
| err = errors.Join(err, vars[globalNameNodeNetmask].Set(ipMaskToUint32(params.NodeIPMask))) | ||
| err = errors.Join(err, vars[globalNameNodeIfindex].Set(params.NodeIfindex)) | ||
| err = errors.Join(err, vars[globalNameNodeMacaddrP1].Set(hardwareAddrToUint32(params.NodeMacAddr))) | ||
| err = errors.Join(err, vars[globalNameNodeMacaddrP2].Set(hardwareAddrToUint16(params.NodeMacAddr))) | ||
|
|
@@ -228,10 +229,6 @@ func attachTCFilter(progName string, ifindex uint32, direction TCDirection) erro | |
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The PR description says "Clear the direct-neighbor map when CubeVS starts to avoid reusing stale entries", but this diff doesn't do that — and it removes the only two pin-cleanups that existed. The new To clear the map on start, Separately, removing the |
||
| // Init should be called once before invoking any other CubeVS APIs. | ||
|
jay3cx marked this conversation as resolved.
|
||
| func Init(params Params) error { | ||
|
jay3cx marked this conversation as resolved.
Outdated
|
||
| _ = os.Remove(pinPath("tungrp_to_tuns")) // NOCC:Path Traversal() | ||
| // dns_query_track is runtime pending-query state, not persisted policy. | ||
| _ = os.Remove(pinPath(MapNameDNSQueryTrack)) // NOCC:Path Traversal() | ||
|
|
||
| err := loadObject(params, loadLocalgw, "loadLocalgw") | ||
| if err != nil { | ||
| return err | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,6 +26,11 @@ func ipToUint32(ip net.IP) uint32 { | |
| return uint32(ip[0]) | uint32(ip[1])<<8 | uint32(ip[2])<<16 | uint32(ip[3])<<24 | ||
| } | ||
|
|
||
| // ipMaskToUint32 converts an IPv4 mask to the same byte layout as ipToUint32. | ||
| func ipMaskToUint32(mask net.IPMask) uint32 { | ||
| return uint32(mask[0]) | uint32(mask[1])<<8 | uint32(mask[2])<<16 | uint32(mask[3])<<24 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
jay3cx marked this conversation as resolved.
jay3cx marked this conversation as resolved.
|
||
| } | ||
|
|
||
| // hardwareAddrToUint32 converts the first 4 bytes of MAC address to a uint32. | ||
| func hardwareAddrToUint32(addr net.HardwareAddr) uint32 { | ||
| return uint32(addr[0]) | uint32(addr[1])<<8 | uint32(addr[2])<<16 | uint32(addr[3])<<24 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -96,6 +96,20 @@ struct { | |
| __uint(pinning, LIBBPF_PIN_BY_NAME); | ||
| } snat_iplist SEC(".maps"); | ||
|
|
||
| /* Direct-egress on-link neighbor cache. | ||
| * | ||
| * key: destination IPv4 address in packet-byte layout | ||
| * value: destination MAC plus probe deadline; an all-zero MAC means initial | ||
| * resolution is pending | ||
| */ | ||
| struct { | ||
| __uint(type, BPF_MAP_TYPE_LRU_HASH); | ||
|
jay3cx marked this conversation as resolved.
|
||
| __uint(max_entries, MAX_ENTRIES); | ||
| __type(key, __u32); | ||
| __type(value, struct direct_neighbor); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Stale The PR description says "Clear the direct-neighbor map when CubeVS starts to avoid reusing stale entries", but no such code exists anywhere in the diff. Meanwhile, |
||
| __uint(pinning, LIBBPF_PIN_BY_NAME); | ||
| } direct_neigh SEC(".maps"); | ||
|
|
||
| /* Egress allow list v2 (hash of maps) | ||
| * | ||
| * key: ifindex of the TAP device | ||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we change
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved it into the old set_mac_pair sites so we don't touch every redirect. |
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -106,6 +106,164 @@ static __always_inline bool should_do_nat(const struct iphdr *l3) | |||
| return true; | ||||
| } | ||||
|
|
||||
| /* Primary IPv4 prefix of the node NIC. Direct mode only. */ | ||||
| static __always_inline bool direct_egress_is_onlink(__u32 daddr) | ||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||
| { | ||||
| return egress_redirect_flags == 0 && | ||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prefix-based on-link classification will also match the subnet broadcast address (e.g.
jay3cx marked this conversation as resolved.
|
||||
| (daddr & nodenic_netmask) == (nodenic_ip & nodenic_netmask); | ||||
|
jay3cx marked this conversation as resolved.
jay3cx marked this conversation as resolved.
|
||||
| } | ||||
|
|
||||
| static __always_inline bool direct_neighbor_is_zero(const struct direct_neighbor *neighbor) | ||||
| { | ||||
| const union macaddr *macaddr = (const union macaddr *)neighbor->addr; | ||||
|
|
||||
| return macaddr->p1 == 0 && macaddr->p2 == 0; | ||||
| } | ||||
|
|
||||
| #define DIRECT_ARP_PRESERVED_LEN 96 | ||||
| #define DIRECT_ARP_HEADROOM 32 | ||||
| #define DIRECT_ARP_FRAME_LEN (DIRECT_ARP_PRESERVED_LEN + DIRECT_ARP_HEADROOM) | ||||
| #define DIRECT_ARP_ZERO_CHUNK_LEN 32 | ||||
|
|
||||
| static __always_inline long direct_egress_clear_arp_padding(struct __sk_buff *skb) | ||||
| { | ||||
| unsigned char zeroes[DIRECT_ARP_ZERO_CHUNK_LEN] = {}; | ||||
| long err; | ||||
|
|
||||
| err = bpf_skb_store_bytes(skb, sizeof(struct arp_packet), | ||||
| zeroes, sizeof(zeroes), 0); | ||||
| if (err) | ||||
| return err; | ||||
| err = bpf_skb_store_bytes(skb, | ||||
| sizeof(struct arp_packet) + DIRECT_ARP_ZERO_CHUNK_LEN, | ||||
| zeroes, sizeof(zeroes), 0); | ||||
| if (err) | ||||
| return err; | ||||
| return bpf_skb_store_bytes(skb, | ||||
| sizeof(struct arp_packet) + 2 * DIRECT_ARP_ZERO_CHUNK_LEN, | ||||
| zeroes, | ||||
| DIRECT_ARP_FRAME_LEN - sizeof(struct arp_packet) - | ||||
| 2 * DIRECT_ARP_ZERO_CHUNK_LEN, 0); | ||||
| } | ||||
|
|
||||
| static __always_inline long direct_egress_arp_request(struct __sk_buff *skb, __u32 daddr) | ||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See CubeSandbox/CubeNet/src/mvmtap.bpf.c Line 253 in 189d6ee
I still have doubt in the implementation here. Please explain the purpose of bpf_skb_change_tail/bpf_skb_change_head/direct_egress_clear_arp_padding here. I haven't consult an AI agent yet.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TAP packets can still have
Staying above that floor only keeps the write in bounds. It does not keep it off the ARP header. That is what
I'll fix the comment in the code too. "keeps that slot" is the wrong picture. |
||||
| { | ||||
| struct arp_packet packet = {}; | ||||
| union macaddr *macaddr; | ||||
| long err; | ||||
|
|
||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||
| __builtin_memset(packet.eth.h_dest, 0xff, ETH_ALEN); | ||||
| macaddr = (union macaddr *)packet.eth.h_source; | ||||
| macaddr->p1 = nodenic_macaddr_p1; | ||||
| macaddr->p2 = nodenic_macaddr_p2; | ||||
| packet.eth.h_proto = bpf_htons(ETH_P_ARP); | ||||
|
|
||||
| packet.arp.ar_hrd = bpf_htons(ARPHRD_ETHER); | ||||
| packet.arp.ar_pro = bpf_htons(ETH_P_IP); | ||||
| packet.arp.ar_hln = ETH_ALEN; | ||||
| packet.arp.ar_pln = sizeof(__be32); | ||||
| packet.arp.ar_op = bpf_htons(ARPOP_REQUEST); | ||||
| macaddr = (union macaddr *)packet.arp.ar_sha; | ||||
| macaddr->p1 = nodenic_macaddr_p1; | ||||
| macaddr->p2 = nodenic_macaddr_p2; | ||||
| packet.arp.ar_sip = nodenic_ip; | ||||
| packet.arp.ar_tip = daddr; | ||||
|
|
||||
| /* CHECKSUM_PARTIAL can write L4 csum after we return. | ||||
| * change_tail(96) keeps that slot. change_head(32) moves it past ARP. | ||||
| * Then store ARP and zero the rest (broadcast). | ||||
| */ | ||||
| err = bpf_skb_change_tail(skb, DIRECT_ARP_PRESERVED_LEN, 0); | ||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The existing
jay3cx marked this conversation as resolved.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On the target kernel (5.4), Because
jay3cx marked this conversation as resolved.
jay3cx marked this conversation as resolved.
|
||||
| if (err) | ||||
| return TC_ACT_SHOT; | ||||
| err = bpf_skb_change_head(skb, DIRECT_ARP_HEADROOM, 0); | ||||
| if (err) | ||||
| return TC_ACT_SHOT; | ||||
| err = bpf_skb_store_bytes(skb, 0, &packet, sizeof(packet), 0); | ||||
| if (err) | ||||
| return TC_ACT_SHOT; | ||||
| err = direct_egress_clear_arp_padding(skb); | ||||
| if (err) | ||||
| return TC_ACT_SHOT; | ||||
|
|
||||
| return 0; | ||||
| } | ||||
|
|
||||
| #define EGRESS_MAC_DROP (-1) | ||||
| #define EGRESS_MAC_READY 0 | ||||
| #define EGRESS_MAC_PROBE 1 | ||||
|
|
||||
| /* READY: L2 rewritten. PROBE: skb is now an ARP request. DROP: wait. */ | ||||
| static __always_inline int prepare_egress_l2(struct __sk_buff *skb, | ||||
| struct ethhdr *l2, __u32 daddr) | ||||
| { | ||||
| struct bpf_fib_lookup fib = { | ||||
| .family = AF_INET, | ||||
| .ifindex = nodenic_ifindex, | ||||
| .ipv4_src = nodenic_ip, | ||||
| .ipv4_dst = daddr, | ||||
| }; | ||||
| struct direct_neighbor pending = {}; | ||||
| struct direct_neighbor *neighbor; | ||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Revalidation consumes a data packet every |
||||
| union macaddr *neighbor_mac; | ||||
| const union macaddr *dmac; | ||||
| const union macaddr *smac; | ||||
| __u64 retry_at; | ||||
| __u64 now; | ||||
| long err; | ||||
|
|
||||
| if (!direct_egress_is_onlink(daddr)) { | ||||
| set_mac_pair(l2, egress_smacaddr_p1, egress_smacaddr_p2, | ||||
| egress_dmacaddr_p1, egress_dmacaddr_p2); | ||||
| return EGRESS_MAC_READY; | ||||
| } | ||||
|
|
||||
| err = bpf_fib_lookup(skb, &fib, sizeof(fib), | ||||
|
jay3cx marked this conversation as resolved.
|
||||
| BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT); | ||||
| if (err == BPF_FIB_LKUP_RET_SUCCESS && fib.ifindex == nodenic_ifindex) { | ||||
| smac = (const union macaddr *)fib.smac; | ||||
| dmac = (const union macaddr *)fib.dmac; | ||||
| set_mac_pair(l2, smac->p1, smac->p2, dmac->p1, dmac->p2); | ||||
| return EGRESS_MAC_READY; | ||||
| } | ||||
|
|
||||
| now = bpf_ktime_get_ns(); | ||||
| neighbor = bpf_map_lookup_elem(&direct_neigh, &daddr); | ||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Before doing a map lookup, can we try
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fib_lookup first, then ARP if it misses.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||||
| if (neighbor) { | ||||
| if (neighbor->next_probe_at_ns > now) { | ||||
| if (!direct_neighbor_is_zero(neighbor)) | ||||
| goto set_neighbor; | ||||
| return EGRESS_MAC_DROP; | ||||
| } | ||||
|
|
||||
| /* Touch only the deadline so a concurrent learn cannot lose its MAC. */ | ||||
| retry_at = now + DIRECT_NEIGH_PROBE_INTERVAL_NS; | ||||
| neighbor->next_probe_at_ns = retry_at; | ||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unlocked in-place write to the map value races with
The re-lookup after the write (intended to detect the concurrent learn) narrows but does not close this window. Consider making the MAC + deadline a single 64-bit-aligned store, or guarding the value with
jay3cx marked this conversation as resolved.
Outdated
|
||||
| neighbor = bpf_map_lookup_elem(&direct_neigh, &daddr); | ||||
| if (!neighbor) | ||||
| return EGRESS_MAC_DROP; | ||||
| /* Later deadline: another CPU already refreshed this entry. */ | ||||
| if (!direct_neighbor_is_zero(neighbor) && | ||||
| neighbor->next_probe_at_ns > retry_at) | ||||
| goto set_neighbor; | ||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can simply always do an ARP probe here, right? I am not sure how frequent this concurrent learning will happen.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed. Dropped the extra re-lookup and just probe now. |
||||
| } else { | ||||
| pending.next_probe_at_ns = now + DIRECT_NEIGH_PROBE_INTERVAL_NS; | ||||
| err = bpf_map_update_elem(&direct_neigh, &daddr, &pending, BPF_NOEXIST); | ||||
| if (err) | ||||
| return EGRESS_MAC_DROP; | ||||
| } | ||||
|
|
||||
| if (direct_egress_arp_request(skb, daddr)) | ||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Revalidation consumes a live data packet even when the cached MAC is still valid. When the entry has expired ( This is also fragile for GSO/large packets: this codebase already notes in Consider rewriting the current packet with the cached MAC (forward it now) and setting the deadline so a later packet triggers the probe, rather than sacrificing the triggering packet. |
||||
| return EGRESS_MAC_DROP; | ||||
| return EGRESS_MAC_PROBE; | ||||
|
|
||||
|
jay3cx marked this conversation as resolved.
|
||||
| set_neighbor: | ||||
| neighbor_mac = (union macaddr *)neighbor->addr; | ||||
| set_mac_pair(l2, nodenic_macaddr_p1, nodenic_macaddr_p2, | ||||
| neighbor_mac->p1, neighbor_mac->p2); | ||||
| return EGRESS_MAC_READY; | ||||
| } | ||||
|
|
||||
| /* | ||||
| * Check whether a TCP flow should be redirected to the L7 proxy. | ||||
| * | ||||
|
|
@@ -440,6 +598,7 @@ static __always_inline __u32 do_icmp_nat(struct __sk_buff *skb, struct mvm_meta | |||
| struct ethhdr *l2; | ||||
| struct iphdr *l3; | ||||
| struct icmphdr *l4; | ||||
| int mac_result; | ||||
| __u16 ip_hlen; | ||||
| __u16 snat_id; | ||||
| __u64 flags; | ||||
|
|
@@ -491,8 +650,11 @@ static __always_inline __u32 do_icmp_nat(struct __sk_buff *skb, struct mvm_meta | |||
| icmp_csum_off = ICMP_CSUM_OFF(ip_hlen); | ||||
|
|
||||
| /* update L2 first: csum/store helpers may invalidate packet pointers */ | ||||
| set_mac_pair(l2, egress_smacaddr_p1, egress_smacaddr_p2, | ||||
| egress_dmacaddr_p1, egress_dmacaddr_p2); | ||||
| mac_result = prepare_egress_l2(skb, l2, key.dst_ip); | ||||
| if (mac_result == EGRESS_MAC_DROP) | ||||
| return 0; | ||||
| if (mac_result == EGRESS_MAC_PROBE) | ||||
| return sess->node_ifindex; | ||||
|
|
||||
| /* update ICMP csum: ICMP has no pseudo-header, so no BPF_F_PSEUDO_HDR. | ||||
| * Only the echo identifier change affects the csum (IP saddr is not | ||||
|
|
@@ -543,6 +705,7 @@ static __always_inline __u32 do_udp_nat_inline(struct __sk_buff *skb, | |||
| struct ethhdr *l2; | ||||
| struct iphdr *l3; | ||||
| struct udphdr *l4; | ||||
| int mac_result; | ||||
| __u16 ip_hlen; | ||||
| __u16 snat_port; | ||||
| __u64 flags; | ||||
|
|
@@ -590,8 +753,11 @@ static __always_inline __u32 do_udp_nat_inline(struct __sk_buff *skb, | |||
| udp_csum_off = UDP_CSUM_OFF(ip_hlen); | ||||
|
|
||||
| /* update L2 first: csum/store helpers may invalidate packet pointers */ | ||||
| set_mac_pair(l2, egress_smacaddr_p1, egress_smacaddr_p2, | ||||
| egress_dmacaddr_p1, egress_dmacaddr_p2); | ||||
| mac_result = prepare_egress_l2(skb, l2, key.dst_ip); | ||||
| if (mac_result == EGRESS_MAC_DROP) | ||||
| return 0; | ||||
| if (mac_result == EGRESS_MAC_PROBE) | ||||
| return sess->node_ifindex; | ||||
|
|
||||
| /* update UDP csum only if it was non-zero (UDP csum is optional over IPv4). | ||||
| * BPF_F_MARK_MANGLED_0 keeps a 0 csum (= disabled) intact in case the | ||||
|
|
@@ -685,6 +851,7 @@ static __always_inline __u64 do_tcp_nat(struct __sk_buff *skb, struct mvm_meta * | |||
| struct ethhdr *l2; | ||||
| struct iphdr *l3; | ||||
| struct tcphdr *l4; | ||||
| int mac_result; | ||||
| __u16 ip_hlen; | ||||
| __u16 snat_port; | ||||
| __u64 flags; | ||||
|
|
@@ -758,8 +925,11 @@ static __always_inline __u64 do_tcp_nat(struct __sk_buff *skb, struct mvm_meta * | |||
| tcp_csum_off = TCP_CSUM_OFF(ip_hlen); | ||||
|
|
||||
| /* update L2 first: csum/store helpers may invalidate packet pointers */ | ||||
| set_mac_pair(l2, egress_smacaddr_p1, egress_smacaddr_p2, | ||||
| egress_dmacaddr_p1, egress_dmacaddr_p2); | ||||
| mac_result = prepare_egress_l2(skb, l2, key.dst_ip); | ||||
| if (mac_result == EGRESS_MAC_DROP) | ||||
| return TCP_NAT_DROP; | ||||
| if (mac_result == EGRESS_MAC_PROBE) | ||||
| return TCP_NAT_PACK(sess->node_ifindex, TCP_NAT_OK); | ||||
|
|
||||
| /* update TCP csum: IP saddr is part of pseudo-header, so BPF_F_PSEUDO_HDR */ | ||||
| flags = BPF_F_PSEUDO_HDR | sizeof(old_saddr); | ||||
|
|
||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ipMaskToUint32(nil)returns "invalid IPv4 mask length: 0", so any CubeVS consumer that constructsParamswithout the newNodeIPMaskfield now hard-fails atloadMvmtap— a backward-incompatibility for the new field. The new test only covers the localgw (nofrom_cube) path with a nil mask, so the mvmtap nil case is untested. Consider treating a nil mask as "skip, leave the BPF default", and nil-checkvars[globalNameNodeNetmask]before.Setso the dereference can't panic if the constant is ever absent from an object.