Пример #1
0
func (s *Server) StartServer(ctx context.Context, arg *StartServerRequest) (*StartServerResponse, error) {
	g := arg.Global
	if net.ParseIP(g.RouterId) == nil {
		return nil, fmt.Errorf("invalid router-id format: %s", g.RouterId)
	}
	families := make([]config.AfiSafi, 0, len(g.Families))
	for _, f := range g.Families {
		name := config.AfiSafiType(bgp.RouteFamily(f).String())
		families = append(families, config.AfiSafi{
			Config: config.AfiSafiConfig{
				AfiSafiName: name,
				Enabled:     true,
			},
			State: config.AfiSafiState{
				AfiSafiName: name,
			},
		})
	}
	b := &config.BgpConfigSet{
		Global: config.Global{
			Config: config.GlobalConfig{
				As:               g.As,
				RouterId:         g.RouterId,
				Port:             g.ListenPort,
				LocalAddressList: g.ListenAddresses,
			},
			MplsLabelRange: config.MplsLabelRange{
				MinLabel: g.MplsLabelMin,
				MaxLabel: g.MplsLabelMax,
			},
			AfiSafis: families,
		},
	}
	return &StartServerResponse{}, s.bgpServer.Start(&b.Global)
}
Пример #2
0
func (s *Server) AddNeighbor(ctx context.Context, arg *AddNeighborRequest) (*AddNeighborResponse, error) {
	c, err := func(a *Peer) (*config.Neighbor, error) {
		pconf := &config.Neighbor{}
		if a.Conf != nil {
			pconf.Config.NeighborAddress = a.Conf.NeighborAddress
			pconf.Config.PeerAs = a.Conf.PeerAs
			pconf.Config.LocalAs = a.Conf.LocalAs
			pconf.Config.AuthPassword = a.Conf.AuthPassword
			pconf.Config.RemovePrivateAs = config.RemovePrivateAsOption(a.Conf.RemovePrivateAs)
			pconf.Config.RouteFlapDamping = a.Conf.RouteFlapDamping
			pconf.Config.SendCommunity = config.CommunityType(a.Conf.SendCommunity)
			pconf.Config.Description = a.Conf.Description
			pconf.Config.PeerGroup = a.Conf.PeerGroup
			pconf.Config.NeighborAddress = a.Conf.NeighborAddress
			pconf.Config.NeighborInterface = a.Conf.NeighborInterface
		}
		if a.Timers != nil && a.Timers.Config != nil {
			pconf.Timers.Config.ConnectRetry = float64(a.Timers.Config.ConnectRetry)
			pconf.Timers.Config.HoldTime = float64(a.Timers.Config.HoldTime)
			pconf.Timers.Config.KeepaliveInterval = float64(a.Timers.Config.KeepaliveInterval)
			pconf.Timers.Config.MinimumAdvertisementInterval = float64(a.Timers.Config.MinimumAdvertisementInterval)
		}
		if a.RouteReflector != nil {
			pconf.RouteReflector.Config.RouteReflectorClusterId = config.RrClusterIdType(a.RouteReflector.RouteReflectorClusterId)
			pconf.RouteReflector.Config.RouteReflectorClient = a.RouteReflector.RouteReflectorClient
		}
		if a.RouteServer != nil {
			pconf.RouteServer.Config.RouteServerClient = a.RouteServer.RouteServerClient
		}
		if a.ApplyPolicy != nil {
			if a.ApplyPolicy.ImportPolicy != nil {
				pconf.ApplyPolicy.Config.DefaultImportPolicy = config.DefaultPolicyType(a.ApplyPolicy.ImportPolicy.Default)
				for _, p := range a.ApplyPolicy.ImportPolicy.Policies {
					pconf.ApplyPolicy.Config.ImportPolicyList = append(pconf.ApplyPolicy.Config.ImportPolicyList, p.Name)
				}
			}
			if a.ApplyPolicy.ExportPolicy != nil {
				pconf.ApplyPolicy.Config.DefaultExportPolicy = config.DefaultPolicyType(a.ApplyPolicy.ExportPolicy.Default)
				for _, p := range a.ApplyPolicy.ExportPolicy.Policies {
					pconf.ApplyPolicy.Config.ExportPolicyList = append(pconf.ApplyPolicy.Config.ExportPolicyList, p.Name)
				}
			}
			if a.ApplyPolicy.InPolicy != nil {
				pconf.ApplyPolicy.Config.DefaultInPolicy = config.DefaultPolicyType(a.ApplyPolicy.InPolicy.Default)
				for _, p := range a.ApplyPolicy.InPolicy.Policies {
					pconf.ApplyPolicy.Config.InPolicyList = append(pconf.ApplyPolicy.Config.InPolicyList, p.Name)
				}
			}
		}
		if a.Families != nil {
			for _, family := range a.Families {
				name, ok := bgp.AddressFamilyNameMap[bgp.RouteFamily(family)]
				if !ok {
					return pconf, fmt.Errorf("invalid address family: %d", family)
				}
				cAfiSafi := config.AfiSafi{
					Config: config.AfiSafiConfig{
						AfiSafiName: config.AfiSafiType(name),
					},
				}
				pconf.AfiSafis = append(pconf.AfiSafis, cAfiSafi)
			}
		}
		if a.Transport != nil {
			pconf.Transport.Config.LocalAddress = a.Transport.LocalAddress
			pconf.Transport.Config.PassiveMode = a.Transport.PassiveMode
		}
		if a.EbgpMultihop != nil {
			pconf.EbgpMultihop.Config.Enabled = a.EbgpMultihop.Enabled
			pconf.EbgpMultihop.Config.MultihopTtl = uint8(a.EbgpMultihop.MultihopTtl)
		}
		return pconf, nil
	}(arg.Peer)
	if err != nil {
		return nil, err
	}
	return &AddNeighborResponse{}, s.bgpServer.AddNeighbor(c)
}