コード例 #1
0
ファイル: grpc_server.go プロジェクト: jojimt/netplugin
func (s *Server) GetRib(ctx context.Context, arg *GetRibRequest) (*GetRibResponse, error) {
	f := func() []*table.LookupPrefix {
		l := make([]*table.LookupPrefix, 0, len(arg.Table.Destinations))
		for _, p := range arg.Table.Destinations {
			l = append(l, &table.LookupPrefix{
				Prefix: p.Prefix,
				LookupOption: func() table.LookupOption {
					if p.LongerPrefixes {
						return table.LOOKUP_LONGER
					} else if p.ShorterPrefixes {
						return table.LOOKUP_SHORTER
					}
					return table.LOOKUP_EXACT
				}(),
			})
		}
		return l
	}

	var in bool
	var err error
	var tbl *table.Table

	family := bgp.RouteFamily(arg.Table.Family)
	switch arg.Table.Type {
	case Resource_LOCAL, Resource_GLOBAL:
		tbl, err = s.bgpServer.GetRib(arg.Table.Name, family, f())
	case Resource_ADJ_IN:
		in = true
		fallthrough
	case Resource_ADJ_OUT:
		tbl, err = s.bgpServer.GetAdjRib(arg.Table.Name, family, in, f())
	case Resource_VRF:
		tbl, err = s.bgpServer.GetVrfRib(arg.Table.Name, family, []*table.LookupPrefix{})
	default:
		return nil, fmt.Errorf("unsupported resource type: %v", arg.Table.Type)
	}

	if err != nil {
		return nil, err
	}

	dsts := []*Destination{}
	for _, dst := range tbl.GetDestinations() {
		dsts = append(dsts, &Destination{
			Prefix: dst.GetNlri().String(),
			Paths: func(paths []*table.Path) []*Path {
				l := make([]*Path, 0, len(paths))
				for i, p := range paths {
					pp := ToPathApi(p)
					switch arg.Table.Type {
					case Resource_LOCAL, Resource_GLOBAL:
						if i == 0 {
							pp.Best = true
						}
					}
					l = append(l, pp)
				}
				return l
			}(dst.GetAllKnownPathList()),
		})
	}

	return &GetRibResponse{Table: &Table{
		Type:         arg.Table.Type,
		Family:       arg.Table.Family,
		Destinations: dsts},
	}, err
}
コード例 #2
0
ファイル: neighbor.go プロジェクト: osrg/gobgp
func showNeighborRib(r string, name string, args []string) error {
	showBest := false
	showAge := true
	showLabel := false
	def := addr2AddressFamily(net.ParseIP(name))
	switch r {
	case CMD_GLOBAL:
		def = bgp.RF_IPv4_UC
		showBest = true
	case CMD_LOCAL:
		showBest = true
	case CMD_ADJ_OUT:
		showAge = false
	case CMD_VRF:
		def = bgp.RF_IPv4_UC
	}
	family, err := checkAddressFamily(def)
	if err != nil {
		return err
	}
	switch family {
	case bgp.RF_IPv4_MPLS, bgp.RF_IPv6_MPLS, bgp.RF_IPv4_VPN, bgp.RF_IPv6_VPN:
		showLabel = true
	}

	var filter []*table.LookupPrefix
	if len(args) > 0 {
		var option table.LookupOption
		if len(args) > 1 {
			if args[1] == "longer-prefixes" {
				option = table.LOOKUP_LONGER
			} else if args[1] == "shorter-prefixes" {
				option = table.LOOKUP_SHORTER
			} else {
				return fmt.Errorf("invalid format for route filtering")
			}
		}
		filter = []*table.LookupPrefix{&table.LookupPrefix{
			Prefix:       args[0],
			LookupOption: option,
		},
		}
	}

	var rib *table.Table
	switch r {
	case CMD_GLOBAL:
		rib, err = client.GetRIB(family, filter)
	case CMD_LOCAL:
		rib, err = client.GetLocalRIB(name, family, filter)
	case CMD_ADJ_IN, CMD_ACCEPTED, CMD_REJECTED:
		rib, err = client.GetAdjRIBIn(name, family, filter)
	case CMD_ADJ_OUT:
		rib, err = client.GetAdjRIBOut(name, family, filter)
	case CMD_VRF:
		rib, err = client.GetVRFRIB(name, family, filter)
	}

	if err != nil {
		return err
	}

	switch r {
	case CMD_LOCAL, CMD_ADJ_IN, CMD_ACCEPTED, CMD_REJECTED, CMD_ADJ_OUT:
		if rib.Info("").NumDestination == 0 {
			peer, err := getNeighbor(name)
			if err != nil {
				return err
			}
			if peer.State.SessionState != config.SESSION_STATE_ESTABLISHED {
				return fmt.Errorf("Neighbor %v's BGP session is not established", name)
			}
		}
	}

	if globalOpts.Json {
		j, _ := json.Marshal(rib.GetDestinations())
		fmt.Println(string(j))
		return nil
	}

	counter := 0
	for _, d := range rib.GetSortedDestinations() {
		var ps []*table.Path
		if r == CMD_ACCEPTED || r == CMD_REJECTED {
			for _, p := range d.GetAllKnownPathList() {
				switch r {
				case CMD_ACCEPTED:
					if p.Filtered("") > table.POLICY_DIRECTION_NONE {
						continue
					}
				case CMD_REJECTED:
					if p.Filtered("") == table.POLICY_DIRECTION_NONE {
						continue
					}
				}
				ps = append(ps, p)
			}
		} else {
			ps = d.GetAllKnownPathList()
		}
		if counter == 0 {
			ShowRoute(ps, showAge, showBest, showLabel, false, true)
		} else {
			ShowRoute(ps, showAge, showBest, showLabel, false, false)
		}
		counter++
	}

	if counter == 0 {
		fmt.Println("Network not in table")
	}
	return nil
}