Skip to content

Commit

Permalink
lookup: Support tbid option
Browse files Browse the repository at this point in the history
Support bpf_fib_lookup with tbid option. We expose table option similar
to the iproute2. It requires an additional flag BFP_FIB_LOOKUP_TBID to
distinguish table 0 and unspecified, but we don't expose the flag
explicitly. Since we can distinguish them with the table option
specified vs unspecifid. When the table option is specified, we set it
transparently with BPF_FIB_LOOKUP_DIRECT.

Signed-off-by: Yutaro Hayakawa <[email protected]>
  • Loading branch information
YutaroHayakawa committed Sep 1, 2024
1 parent f25b0de commit 092b095
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion cmd/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type lookupIn struct {
IPv6FlowInfo *uint32
SrcAddr netip.Addr
DstAddr netip.Addr
TableID *uint32
}

func (in *lookupIn) marshal() []byte {
Expand Down Expand Up @@ -93,8 +94,14 @@ func (in *lookupIn) marshal() []byte {
a := in.DstAddr.As16()
buf.Write(a[:])
}
// param->tbid
if in.TableID != nil {
binary.Write(buf, binary.NativeEndian, in.TableID)
} else {
binary.Write(buf, binary.NativeEndian, uint32(0))
}
// Padding
buf.Write(bytes.Repeat([]byte{0}, 16))
buf.Write(bytes.Repeat([]byte{0}, 12))
return buf.Bytes()
}

Expand Down Expand Up @@ -264,6 +271,13 @@ var lookupCmd = &cobra.Command{
if output, _ := cmd.Flags().GetBool("output"); output {
flags |= BFP_FIB_LOOKUP_OUTPUT
}
if in.TableID != nil {
if flags&BFP_FIB_LOOKUP_DIRECT == 0 {
cmd.PrintErrf("Forcefully setting BFP_FIB_LOOKUP_DIRECT option since you specified table option which requires direct lookup. To suppress this message, set --direct flag explicitly.\n")
flags |= BFP_FIB_LOOKUP_DIRECT
}
flags |= BFP_FIB_LOOKUP_TBID
}

// Serialize input parameters to write struct bpf_fib_lookup to map
param := in.marshal()
Expand Down Expand Up @@ -491,6 +505,22 @@ var lookupCmdOpts = map[string]lookupOpt{
},
probe: func() bool { return true },
},
"table": {
desc: []string{"table", "<table id>", "Table ID"},
handle: func(in *lookupIn, args []string) (int, error) {
if len(args) == 0 {
return 0, fmt.Errorf("table is unspecified")
}
tableID, err := strconv.ParseUint(args[0], 10, 32)
if err != nil {
return 0, fmt.Errorf("cannot parse table id: %w", err)
}
tableID32 := uint32(tableID)
in.TableID = &tableID32
return 1, nil
},
probe: func() bool { return true },
},
}

func parseLookupArgs(in *lookupIn, args []string) error {
Expand Down

0 comments on commit 092b095

Please sign in to comment.