diff --git a/cmd/riscli/lpm.go b/cmd/riscli/lpm.go new file mode 100644 index 0000000000000000000000000000000000000000..1c564bcfb3a771082366ecb24829fbd6c487993a --- /dev/null +++ b/cmd/riscli/lpm.go @@ -0,0 +1,71 @@ +package main + +import ( + "context" + "os" + + pb "github.com/bio-routing/bio-rd/cmd/ris/api" + bnet "github.com/bio-routing/bio-rd/net" + "github.com/pkg/errors" + log "github.com/sirupsen/logrus" + "github.com/urfave/cli" + "google.golang.org/grpc" +) + +// NewLPMCommand creates a new LPM command +func NewLPMCommand() cli.Command { + cmd := cli.Command{ + Name: "lpm", + Usage: "longest prefix match", + Flags: []cli.Flag{ + &cli.StringFlag{Name: "ip", Usage: "IP address"}, + }, + } + + cmd.Action = func(c *cli.Context) error { + conn, err := grpc.Dial(c.GlobalString("ris"), grpc.WithInsecure()) + if err != nil { + log.Errorf("GRPC dial failed: %v", err) + os.Exit(1) + } + defer conn.Close() + + ipAddr, err := bnet.IPFromString(c.String("ip")) + if err != nil { + log.Fatalf("Unable to parse address: %v", err) + } + + pfxLen := uint8(32) + if !ipAddr.IsIPv4() { + pfxLen = 128 + } + pfx := bnet.NewPfx(ipAddr, pfxLen) + + client := pb.NewRoutingInformationServiceClient(conn) + err = lpm(client, c.GlobalString("router"), c.GlobalUint64("vrf_id"), pfx) + if err != nil { + log.Fatalf("LPM failed: %v", err) + } + + return nil + } + + return cmd +} + +func lpm(c pb.RoutingInformationServiceClient, routerName string, vrfID uint64, pfx bnet.Prefix) error { + resp, err := c.LPM(context.Background(), &pb.LPMRequest{ + Router: routerName, + VrfId: vrfID, + Pfx: pfx.ToProto(), + }) + if err != nil { + return errors.Wrap(err, "Unable to get client") + } + + for _, r := range resp.Routes { + printRoute(r) + } + + return nil +} diff --git a/cmd/riscli/main.go b/cmd/riscli/main.go index a57f3a73f27e1f3623d017a381725084a1956117..3350b307fc255c58ef7ed855a12031c672f36bf3 100644 --- a/cmd/riscli/main.go +++ b/cmd/riscli/main.go @@ -31,6 +31,7 @@ func main() { app.Commands = []cli.Command{ NewDumpLocRIBCommand(), + NewLPMCommand(), } err := app.Run(os.Args)