Skip to content

Commit

Permalink
lookup: Support BPF_FIB_LOOKUP_SRC
Browse files Browse the repository at this point in the history
Support setting BPF_FIB_LOOKUP_SRC through --src flag.

Signed-off-by: Yutaro Hayakawa <[email protected]>
  • Loading branch information
YutaroHayakawa committed Sep 1, 2024
1 parent 28b18dc commit b8a4003
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions cmd/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ type lookupOut struct {
MTU uint16
Iface string
Metric uint32
Source netip.Addr
NextHop netip.Addr
VlanProto uint16
VlanTCI uint16
Expand Down Expand Up @@ -142,7 +143,10 @@ func (out *lookupOut) String(queryAddr netip.Addr) string {
s += fmt.Sprintf("dmac %s ", net.HardwareAddr(out.DMAC[:]).String())
}
if out.Metric != 0 {
s += fmt.Sprintf("metric %d", out.Metric)
s += fmt.Sprintf("metric %d ", out.Metric)
}
if !out.Source.IsUnspecified() {
s += fmt.Sprintf("src %s ", out.Source)
}
return s
}
Expand Down Expand Up @@ -184,7 +188,23 @@ func (out *lookupOut) unmarshal(data []byte) {
// param->rt_metric
binary.Read(buf, binary.NativeEndian, &out.Metric)

// skip ipv6_src
// param->ipv4_src or ipv6_src
switch out.Family {
case "inet":
var (
addr [4]byte
pad [12]byte
)
binary.Read(buf, binary.BigEndian, &addr)
out.Source = netip.AddrFrom4(addr)
binary.Read(buf, binary.BigEndian, &pad)
case "inet6":
var addr [16]byte
binary.Read(buf, binary.BigEndian, &addr)
out.Source = netip.AddrFrom16(addr)
default:
out.Source = netip.Addr{}
}
binary.Read(buf, binary.NativeEndian, &[16]byte{})

// param->ipv4_dst or param->ipv6_dst
Expand Down Expand Up @@ -281,6 +301,9 @@ var lookupCmd = &cobra.Command{
}
flags |= BFP_FIB_LOOKUP_TBID
}
if src, _ := cmd.Flags().GetBool("src"); src {
flags |= BFP_FIB_LOOKUP_SRC
}

// Serialize input parameters to write struct bpf_fib_lookup to map
param := in.marshal()
Expand Down Expand Up @@ -389,6 +412,11 @@ var lookupCmd = &cobra.Command{
// then the MTU was exceeded and output
// params->mtu_result contains the MTU.
cmd.Printf("Fragmentation Needed (BPF_FIB_LKUP_RET_FRAG_NEEDED) mtu %d\n", out.MTU)
case BPF_FIB_LKUP_RET_NO_SRC_ADDR:
// No source address found, but the FIB lookup was successful.
// The output should contain the partial result.
cmd.Println("No Source Address (BPF_FIB_LKUP_RET_NO_SRC_ADDR)")
cmd.Println(out.String(dst))
case math.MinInt32:
cmd.Println("BUG: bpf_fib_lookup failed")
default:
Expand Down Expand Up @@ -586,4 +614,5 @@ func init() {
lookupCmd.Flags().Bool("direct", false, "Set direct option (BPF_FIB_LOOKUP_DIRECT)")
lookupCmd.Flags().Bool("output", false, "Set output option (BPF_FIB_LOOKUP_OUTPUT)")
lookupCmd.Flags().Bool("skip-neigh", false, "Set skip-neigh option (BPF_FIB_LOOKUP_SKIP_NEIGH)")
lookupCmd.Flags().Bool("src", false, "Set src option (BPF_FIB_LOOKUP_SRC)")
}

0 comments on commit b8a4003

Please sign in to comment.