Created
October 31, 2017 17:01
-
-
Save nnao45/c21c9a443d14ac93f2993267c884c07f to your computer and use it in GitHub Desktop.
Enable AddPath() multipath (dirty snippet...)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| api "github.com/osrg/gobgp/api" | |
| "github.com/osrg/gobgp/config" | |
| "github.com/osrg/gobgp/gobgp/cmd" | |
| "github.com/osrg/gobgp/packet/bgp" | |
| gobgp "github.com/osrg/gobgp/server" | |
| "github.com/osrg/gobgp/table" | |
| "github.com/satori/go.uuid" | |
| "golang.org/x/net/context" | |
| "google.golang.org/grpc" | |
| "os" | |
| "time" | |
| ) | |
| func pullUuidFromByte(uAry []byte) (b []byte) { | |
| for i, u := range uAry { | |
| if i > 16 { | |
| break | |
| } | |
| b = append(b, u) | |
| } | |
| return | |
| } | |
| func main() { | |
| s := gobgp.NewBgpServer() | |
| go s.Serve() | |
| g := api.NewGrpcServer(s, ":50051") | |
| go g.Serve() | |
| global := &config.Global{ | |
| Config: config.GlobalConfig{ | |
| As: 65056, | |
| RouterId: "10.0.4.44", | |
| Port: -1, // gobgp won't listen on tcp:179 | |
| }, | |
| } | |
| if err := s.Start(global); err != nil { | |
| fmt.Println(err) | |
| } | |
| timeout := grpc.WithTimeout(time.Second) | |
| conn, err := grpc.Dial("localhost:50051", timeout, grpc.WithBlock(), grpc.WithInsecure()) | |
| if err != nil { | |
| fmt.Println(err) | |
| os.Exit(1) | |
| } | |
| args1 := []string{"10.10.0.0/24"} | |
| path1, err := cmd.ParsePath(bgp.RF_IPv4_UC, args1) | |
| if err != nil { | |
| fmt.Println(err) | |
| os.Exit(1) | |
| } | |
| args2 := []string{"10.20.0.0/24"} | |
| path2, err := cmd.ParsePath(bgp.RF_IPv4_UC, args2) | |
| if err != nil { | |
| fmt.Println(err) | |
| os.Exit(1) | |
| } | |
| args3 := []string{"10.30.0.0/24"} | |
| path3, err := cmd.ParsePath(bgp.RF_IPv4_UC, args3) | |
| if err != nil { | |
| fmt.Println(err) | |
| os.Exit(1) | |
| } | |
| b1, _ := s.AddPath("", []*table.Path{path1, path2}) | |
| c1 := len(b1) / 16 | |
| for i := 0; i < c1; i++ { | |
| u1, _ := uuid.FromBytes(b1[i*16 : (i+1)*16]) | |
| fmt.Println("1:Added a route's UUID is, ", u1.String()) | |
| } | |
| b2, _ := s.AddPath("", []*table.Path{path3}) | |
| c2 := len(b2) / 16 | |
| for i := 0; i < c2; i++ { | |
| u3, _ := uuid.FromBytes(b2[i*16 : (i+1)*16]) | |
| fmt.Println("2:Added a route's UUID is, ", u3.String()) | |
| } | |
| client := api.NewGobgpApiClient(conn) | |
| r, err := client.GetRib(context.Background(), &api.GetRibRequest{ | |
| Table: &api.Table{ | |
| Type: api.Resource_GLOBAL, | |
| Family: uint32(bgp.RF_IPv4_UC), | |
| Name: "", | |
| }, | |
| }) | |
| if err != nil { | |
| fmt.Println(err) | |
| os.Exit(1) | |
| } | |
| t, err := r.Table.ToNativeTable() | |
| if err != nil { | |
| fmt.Println(err) | |
| os.Exit(1) | |
| } | |
| for _, d := range t.GetSortedDestinations() { | |
| var ps []*table.Path | |
| ps = d.GetAllKnownPathList() | |
| for _, s := range ps { | |
| fmt.Printf("Show a route's Prefix is %s, UUID is %s\n", fmt.Sprint(s.GetNlri()), s.UUID().String()) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment