diff options
-rw-r--r-- | server/peer.go | 4 | ||||
-rw-r--r-- | server/server.go | 17 | ||||
-rw-r--r-- | table/table_manager.go | 46 | ||||
-rw-r--r-- | table/table_manager_test.go | 48 | ||||
-rw-r--r-- | table/vrf.go | 1 |
5 files changed, 74 insertions, 42 deletions
diff --git a/server/peer.go b/server/peer.go index 4f0f9997..2d3b4083 100644 --- a/server/peer.go +++ b/server/peer.go @@ -383,9 +383,9 @@ type LocalRib struct { defaultExportPolicy config.DefaultPolicyType } -func NewLocalRib(owner string, rfList []bgp.RouteFamily, policyMap map[string]*policy.Policy) *LocalRib { +func NewLocalRib(owner string, rfList []bgp.RouteFamily, minLabel, maxLabel uint32) *LocalRib { return &LocalRib{ - rib: table.NewTableManager(owner, rfList), + rib: table.NewTableManager(owner, rfList, minLabel, maxLabel), } } diff --git a/server/server.go b/server/server.go index 9cb5aad4..a147580c 100644 --- a/server/server.go +++ b/server/server.go @@ -220,7 +220,7 @@ func (server *BgpServer) Serve() { return rfList }(g.AfiSafis.AfiSafiList) - server.addLocalRib(NewLocalRib(GLOBAL_RIB_NAME, rfList, make(map[string]*policy.Policy))) + server.addLocalRib(NewLocalRib(GLOBAL_RIB_NAME, rfList, g.MplsLabelRange.MinLabel, g.MplsLabelRange.MaxLabel)) listenerMap := make(map[string]*net.TCPListener) acceptCh := make(chan *net.TCPConn) @@ -350,7 +350,7 @@ func (server *BgpServer) Serve() { name := config.NeighborConfig.NeighborAddress.String() if config.RouteServer.RouteServerConfig.RouteServerClient { - loc := NewLocalRib(name, peer.configuredRFlist(), make(map[string]*policy.Policy)) + loc := NewLocalRib(name, peer.configuredRFlist(), g.MplsLabelRange.MinLabel, g.MplsLabelRange.MaxLabel) server.addLocalRib(loc) loc.setPolicy(peer, server.policyMap) // set in policy @@ -1128,20 +1128,19 @@ func (server *BgpServer) handleModPathRequest(grpcReq *GrpcRequest) []*table.Pat rf = bgp.AfiSafiToRouteFamily(nlri.AFI(), nlri.SAFI()) if arg.Resource == api.Resource_VRF { - vrfs := server.localRibMap[GLOBAL_RIB_NAME].rib.Vrfs - if _, ok := vrfs[arg.Name]; !ok { - result.ResponseErr = fmt.Errorf("vrf %s not found", arg.Name) + label, err := server.localRibMap[GLOBAL_RIB_NAME].rib.GetNextLabel(arg.Name, nexthop, path.IsWithdraw) + if err != nil { + result.ResponseErr = err goto ERR } - vrf := vrfs[arg.Name] - + vrf := server.localRibMap[GLOBAL_RIB_NAME].rib.Vrfs[arg.Name] switch rf { case bgp.RF_IPv4_UC: n := nlri.(*bgp.IPAddrPrefix) - nlri = bgp.NewLabeledVPNIPAddrPrefix(n.Length, n.Prefix.String(), *bgp.NewMPLSLabelStack(), vrf.Rd) + nlri = bgp.NewLabeledVPNIPAddrPrefix(n.Length, n.Prefix.String(), *bgp.NewMPLSLabelStack(label), vrf.Rd) case bgp.RF_IPv6_UC: n := nlri.(*bgp.IPv6AddrPrefix) - nlri = bgp.NewLabeledVPNIPv6AddrPrefix(n.Length, n.Prefix.String(), *bgp.NewMPLSLabelStack(), vrf.Rd) + nlri = bgp.NewLabeledVPNIPv6AddrPrefix(n.Length, n.Prefix.String(), *bgp.NewMPLSLabelStack(label), vrf.Rd) case bgp.RF_EVPN: n := nlri.(*bgp.EVPNNLRI) switch n.RouteType { diff --git a/table/table_manager.go b/table/table_manager.go index 4477182b..01651e1c 100644 --- a/table/table_manager.go +++ b/table/table_manager.go @@ -107,16 +107,22 @@ func ProcessMessage(m *bgp.BGPMessage, peerInfo *PeerInfo) []*Path { } type TableManager struct { - Tables map[bgp.RouteFamily]*Table - Vrfs map[string]*Vrf - owner string + Tables map[bgp.RouteFamily]*Table + Vrfs map[string]*Vrf + owner string + minLabel uint32 + maxLabel uint32 + nextLabel uint32 } -func NewTableManager(owner string, rfList []bgp.RouteFamily) *TableManager { +func NewTableManager(owner string, rfList []bgp.RouteFamily, minLabel, maxLabel uint32) *TableManager { t := &TableManager{ - Tables: make(map[bgp.RouteFamily]*Table), - Vrfs: make(map[string]*Vrf), - owner: owner, + Tables: make(map[bgp.RouteFamily]*Table), + Vrfs: make(map[string]*Vrf), + owner: owner, + minLabel: minLabel, + maxLabel: maxLabel, + nextLabel: minLabel, } for _, rf := range rfList { t.Tables[rf] = NewTable(rf) @@ -124,6 +130,31 @@ func NewTableManager(owner string, rfList []bgp.RouteFamily) *TableManager { return t } +func (manager *TableManager) GetNextLabel(name, nexthop string, isWithdraw bool) (uint32, error) { + var label uint32 + var err error + vrf, ok := manager.Vrfs[name] + if !ok { + return label, fmt.Errorf("vrf %s not found", name) + } + label, ok = vrf.LabelMap[nexthop] + if !ok { + label, err = manager.getNextLabel() + vrf.LabelMap[nexthop] = label + } + return label, err + +} + +func (manager *TableManager) getNextLabel() (uint32, error) { + if manager.nextLabel > manager.maxLabel { + return 0, fmt.Errorf("ran out of label resource. max label %d", manager.maxLabel) + } + label := manager.nextLabel + manager.nextLabel += 1 + return label, nil +} + func (manager *TableManager) OwnerName() string { return manager.owner } @@ -144,6 +175,7 @@ func (manager *TableManager) AddVrf(name string, rd bgp.RouteDistinguisherInterf Rd: rd, ImportRt: importRt, ExportRt: exportRt, + LabelMap: make(map[string]uint32), } msgs := make([]*Path, 0, len(importRt)) nexthop := "0.0.0.0" diff --git a/table/table_manager_test.go b/table/table_manager_test.go index 404ab553..122d60a1 100644 --- a/table/table_manager_test.go +++ b/table/table_manager_test.go @@ -69,7 +69,7 @@ func peerR3() *PeerInfo { // test best path calculation and check the result path is from R1 func TestProcessBGPUpdate_0_select_onlypath_ipv4(t *testing.T) { - tm := NewTableManager("TestProcessBGPUpdate_0_select_onlypath_ipv4", []bgp.RouteFamily{bgp.RF_IPv4_UC}) + tm := NewTableManager("TestProcessBGPUpdate_0_select_onlypath_ipv4", []bgp.RouteFamily{bgp.RF_IPv4_UC}, 0, 0) bgpMessage := update_fromR1() peer := peerR1() @@ -119,7 +119,7 @@ func TestProcessBGPUpdate_0_select_onlypath_ipv4(t *testing.T) { // test best path calculation and check the result path is from R1 func TestProcessBGPUpdate_0_select_onlypath_ipv6(t *testing.T) { - tm := NewTableManager("TestProcessBGPUpdate_0_select_onlypath_ipv6", []bgp.RouteFamily{bgp.RF_IPv6_UC}) + tm := NewTableManager("TestProcessBGPUpdate_0_select_onlypath_ipv6", []bgp.RouteFamily{bgp.RF_IPv6_UC}, 0, 0) bgpMessage := update_fromR1_ipv6() peer := peerR1() @@ -170,7 +170,7 @@ func TestProcessBGPUpdate_0_select_onlypath_ipv6(t *testing.T) { // test: compare localpref func TestProcessBGPUpdate_1_select_high_localpref_ipv4(t *testing.T) { - tm := NewTableManager("TestProcessBGPUpdate_1_select_high_localpref_ipv4", []bgp.RouteFamily{bgp.RF_IPv4_UC}) + tm := NewTableManager("TestProcessBGPUpdate_1_select_high_localpref_ipv4", []bgp.RouteFamily{bgp.RF_IPv4_UC}, 0, 0) var err error // low localpref message @@ -251,7 +251,7 @@ func TestProcessBGPUpdate_1_select_high_localpref_ipv4(t *testing.T) { func TestProcessBGPUpdate_1_select_high_localpref_ipv6(t *testing.T) { - tm := NewTableManager("TestProcessBGPUpdate_1_select_high_localpref_ipv6", []bgp.RouteFamily{bgp.RF_IPv6_UC}) + tm := NewTableManager("TestProcessBGPUpdate_1_select_high_localpref_ipv6", []bgp.RouteFamily{bgp.RF_IPv6_UC}, 0, 0) var err error origin1 := bgp.NewPathAttributeOrigin(0) @@ -334,7 +334,7 @@ func TestProcessBGPUpdate_1_select_high_localpref_ipv6(t *testing.T) { // test: compare localOrigin func TestProcessBGPUpdate_2_select_local_origin_ipv4(t *testing.T) { - tm := NewTableManager("TestProcessBGPUpdate_2_select_local_origin_ipv4", []bgp.RouteFamily{bgp.RF_IPv4_UC}) + tm := NewTableManager("TestProcessBGPUpdate_2_select_local_origin_ipv4", []bgp.RouteFamily{bgp.RF_IPv4_UC}, 0, 0) var err error // low localpref message @@ -417,7 +417,7 @@ func TestProcessBGPUpdate_2_select_local_origin_ipv4(t *testing.T) { func TestProcessBGPUpdate_2_select_local_origin_ipv6(t *testing.T) { - tm := NewTableManager("TestProcessBGPUpdate_2_select_local_origin_ipv6", []bgp.RouteFamily{bgp.RF_IPv6_UC}) + tm := NewTableManager("TestProcessBGPUpdate_2_select_local_origin_ipv6", []bgp.RouteFamily{bgp.RF_IPv6_UC}, 0, 0) var err error origin1 := bgp.NewPathAttributeOrigin(0) @@ -503,7 +503,7 @@ func TestProcessBGPUpdate_2_select_local_origin_ipv6(t *testing.T) { // test: compare AS_PATH func TestProcessBGPUpdate_3_select_aspath_ipv4(t *testing.T) { - tm := NewTableManager("TestProcessBGPUpdate_3_select_aspath_ipv4", []bgp.RouteFamily{bgp.RF_IPv4_UC}) + tm := NewTableManager("TestProcessBGPUpdate_3_select_aspath_ipv4", []bgp.RouteFamily{bgp.RF_IPv4_UC}, 0, 0) var err error bgpMessage1 := update_fromR2viaR1() @@ -559,7 +559,7 @@ func TestProcessBGPUpdate_3_select_aspath_ipv4(t *testing.T) { func TestProcessBGPUpdate_3_select_aspath_ipv6(t *testing.T) { - tm := NewTableManager("TestProcessBGPUpdate_3_select_aspath_ipv6", []bgp.RouteFamily{bgp.RF_IPv6_UC}) + tm := NewTableManager("TestProcessBGPUpdate_3_select_aspath_ipv6", []bgp.RouteFamily{bgp.RF_IPv6_UC}, 0, 0) var err error bgpMessage1 := update_fromR2viaR1_ipv6() @@ -617,7 +617,7 @@ func TestProcessBGPUpdate_3_select_aspath_ipv6(t *testing.T) { // test: compare Origin func TestProcessBGPUpdate_4_select_low_origin_ipv4(t *testing.T) { - tm := NewTableManager("TestProcessBGPUpdate_4_select_low_origin_ipv4", []bgp.RouteFamily{bgp.RF_IPv4_UC}) + tm := NewTableManager("TestProcessBGPUpdate_4_select_low_origin_ipv4", []bgp.RouteFamily{bgp.RF_IPv4_UC}, 0, 0) var err error // low origin message @@ -698,7 +698,7 @@ func TestProcessBGPUpdate_4_select_low_origin_ipv4(t *testing.T) { func TestProcessBGPUpdate_4_select_low_origin_ipv6(t *testing.T) { - tm := NewTableManager("TestProcessBGPUpdate_4_select_low_origin_ipv6", []bgp.RouteFamily{bgp.RF_IPv6_UC}) + tm := NewTableManager("TestProcessBGPUpdate_4_select_low_origin_ipv6", []bgp.RouteFamily{bgp.RF_IPv6_UC}, 0, 0) var err error origin1 := bgp.NewPathAttributeOrigin(1) @@ -781,7 +781,7 @@ func TestProcessBGPUpdate_4_select_low_origin_ipv6(t *testing.T) { // test: compare MED func TestProcessBGPUpdate_5_select_low_med_ipv4(t *testing.T) { - tm := NewTableManager("TestProcessBGPUpdate_5_select_low_med_ipv4", []bgp.RouteFamily{bgp.RF_IPv4_UC}) + tm := NewTableManager("TestProcessBGPUpdate_5_select_low_med_ipv4", []bgp.RouteFamily{bgp.RF_IPv4_UC}, 0, 0) var err error // low origin message @@ -862,7 +862,7 @@ func TestProcessBGPUpdate_5_select_low_med_ipv4(t *testing.T) { func TestProcessBGPUpdate_5_select_low_med_ipv6(t *testing.T) { - tm := NewTableManager("TestProcessBGPUpdate_5_select_low_med_ipv6", []bgp.RouteFamily{bgp.RF_IPv6_UC}) + tm := NewTableManager("TestProcessBGPUpdate_5_select_low_med_ipv6", []bgp.RouteFamily{bgp.RF_IPv6_UC}, 0, 0) var err error origin1 := bgp.NewPathAttributeOrigin(0) @@ -945,7 +945,7 @@ func TestProcessBGPUpdate_5_select_low_med_ipv6(t *testing.T) { // test: compare AS_NUMBER(prefer eBGP path) func TestProcessBGPUpdate_6_select_ebgp_path_ipv4(t *testing.T) { - tm := NewTableManager("TestProcessBGPUpdate_6_select_ebgp_path_ipv4", []bgp.RouteFamily{bgp.RF_IPv4_UC}) + tm := NewTableManager("TestProcessBGPUpdate_6_select_ebgp_path_ipv4", []bgp.RouteFamily{bgp.RF_IPv4_UC}, 0, 0) var err error @@ -1027,7 +1027,7 @@ func TestProcessBGPUpdate_6_select_ebgp_path_ipv4(t *testing.T) { func TestProcessBGPUpdate_6_select_ebgp_path_ipv6(t *testing.T) { - tm := NewTableManager("TestProcessBGPUpdate_6_select_ebgp_path_ipv6", []bgp.RouteFamily{bgp.RF_IPv6_UC}) + tm := NewTableManager("TestProcessBGPUpdate_6_select_ebgp_path_ipv6", []bgp.RouteFamily{bgp.RF_IPv6_UC}, 0, 0) var err error origin1 := bgp.NewPathAttributeOrigin(0) @@ -1112,7 +1112,7 @@ func TestProcessBGPUpdate_6_select_ebgp_path_ipv6(t *testing.T) { // test: compare Router ID func TestProcessBGPUpdate_7_select_low_routerid_path_ipv4(t *testing.T) { - tm := NewTableManager("TestProcessBGPUpdate_7_select_low_routerid_path_ipv4", []bgp.RouteFamily{bgp.RF_IPv4_UC}) + tm := NewTableManager("TestProcessBGPUpdate_7_select_low_routerid_path_ipv4", []bgp.RouteFamily{bgp.RF_IPv4_UC}, 0, 0) var err error @@ -1194,7 +1194,7 @@ func TestProcessBGPUpdate_7_select_low_routerid_path_ipv4(t *testing.T) { func TestProcessBGPUpdate_7_select_low_routerid_path_ipv6(t *testing.T) { - tm := NewTableManager("TestProcessBGPUpdate_7_select_low_routerid_path_ipv6", []bgp.RouteFamily{bgp.RF_IPv6_UC}) + tm := NewTableManager("TestProcessBGPUpdate_7_select_low_routerid_path_ipv6", []bgp.RouteFamily{bgp.RF_IPv6_UC}, 0, 0) var err error origin1 := bgp.NewPathAttributeOrigin(0) @@ -1277,7 +1277,7 @@ func TestProcessBGPUpdate_7_select_low_routerid_path_ipv6(t *testing.T) { // test: withdraw and mpunreach path func TestProcessBGPUpdate_8_withdraw_path_ipv4(t *testing.T) { - tm := NewTableManager("TestProcessBGPUpdate_8_withdraw_path_ipv4", []bgp.RouteFamily{bgp.RF_IPv4_UC}) + tm := NewTableManager("TestProcessBGPUpdate_8_withdraw_path_ipv4", []bgp.RouteFamily{bgp.RF_IPv4_UC}, 0, 0) //setLogger(getLogger(log.DebugLevel)) var err error @@ -1382,7 +1382,7 @@ func TestProcessBGPUpdate_8_withdraw_path_ipv4(t *testing.T) { // TODO MP_UNREACH func TestProcessBGPUpdate_8_mpunreach_path_ipv6(t *testing.T) { - tm := NewTableManager("TestProcessBGPUpdate_8_mpunreach_path_ipv6", []bgp.RouteFamily{bgp.RF_IPv6_UC}) + tm := NewTableManager("TestProcessBGPUpdate_8_mpunreach_path_ipv6", []bgp.RouteFamily{bgp.RF_IPv6_UC}, 0, 0) var err error origin1 := bgp.NewPathAttributeOrigin(0) @@ -1511,7 +1511,7 @@ func TestProcessBGPUpdate_8_mpunreach_path_ipv6(t *testing.T) { // handle bestpath lost func TestProcessBGPUpdate_bestpath_lost_ipv4(t *testing.T) { - tm := NewTableManager("TestProcessBGPUpdate_bestpath_lost_ipv4", []bgp.RouteFamily{bgp.RF_IPv4_UC}) + tm := NewTableManager("TestProcessBGPUpdate_bestpath_lost_ipv4", []bgp.RouteFamily{bgp.RF_IPv4_UC}, 0, 0) //setLogger(getLogger(log.DebugLevel)) var err error @@ -1583,7 +1583,7 @@ func TestProcessBGPUpdate_bestpath_lost_ipv4(t *testing.T) { func TestProcessBGPUpdate_bestpath_lost_ipv6(t *testing.T) { - tm := NewTableManager("TestProcessBGPUpdate_bestpath_lost_ipv6", []bgp.RouteFamily{bgp.RF_IPv6_UC}) + tm := NewTableManager("TestProcessBGPUpdate_bestpath_lost_ipv6", []bgp.RouteFamily{bgp.RF_IPv6_UC}, 0, 0) var err error origin1 := bgp.NewPathAttributeOrigin(0) @@ -1655,7 +1655,7 @@ func TestProcessBGPUpdate_bestpath_lost_ipv6(t *testing.T) { // test: implicit withdrawal case func TestProcessBGPUpdate_implicit_withdrwal_ipv4(t *testing.T) { - tm := NewTableManager("TestProcessBGPUpdate_implicit_withdrwal_ipv4", []bgp.RouteFamily{bgp.RF_IPv4_UC}) + tm := NewTableManager("TestProcessBGPUpdate_implicit_withdrwal_ipv4", []bgp.RouteFamily{bgp.RF_IPv4_UC}, 0, 0) //setLogger(getLogger(log.DebugLevel)) var err error @@ -1738,7 +1738,7 @@ func TestProcessBGPUpdate_implicit_withdrwal_ipv4(t *testing.T) { func TestProcessBGPUpdate_implicit_withdrwal_ipv6(t *testing.T) { - tm := NewTableManager("TestProcessBGPUpdate_implicit_withdrwal_ipv6", []bgp.RouteFamily{bgp.RF_IPv6_UC}) + tm := NewTableManager("TestProcessBGPUpdate_implicit_withdrwal_ipv6", []bgp.RouteFamily{bgp.RF_IPv6_UC}, 0, 0) var err error origin1 := bgp.NewPathAttributeOrigin(0) @@ -1846,7 +1846,7 @@ func TestProcessBGPUpdate_implicit_withdrwal_ipv6(t *testing.T) { // check multiple paths func TestProcessBGPUpdate_multiple_nlri_ipv4(t *testing.T) { - tm := NewTableManager("TestProcessBGPUpdate_multiple_nlri_ipv4", []bgp.RouteFamily{bgp.RF_IPv4_UC}) + tm := NewTableManager("TestProcessBGPUpdate_multiple_nlri_ipv4", []bgp.RouteFamily{bgp.RF_IPv4_UC}, 0, 0) //setLogger(getLogger(log.DebugLevel)) var err error @@ -1981,7 +1981,7 @@ func TestProcessBGPUpdate_multiple_nlri_ipv4(t *testing.T) { // check multiple paths func TestProcessBGPUpdate_multiple_nlri_ipv6(t *testing.T) { - tm := NewTableManager("TestProcessBGPUpdate_multiple_nlri_ipv6", []bgp.RouteFamily{bgp.RF_IPv6_UC}) + tm := NewTableManager("TestProcessBGPUpdate_multiple_nlri_ipv6", []bgp.RouteFamily{bgp.RF_IPv6_UC}, 0, 0) //setLogger(getLogger(log.DebugLevel)) var err error diff --git a/table/vrf.go b/table/vrf.go index 8f857a16..d7b429e7 100644 --- a/table/vrf.go +++ b/table/vrf.go @@ -25,6 +25,7 @@ type Vrf struct { Rd bgp.RouteDistinguisherInterface ImportRt []bgp.ExtendedCommunityInterface ExportRt []bgp.ExtendedCommunityInterface + LabelMap map[string]uint32 } func (v *Vrf) ToApiStruct() *api.Vrf { |