Skip to content

Commit c9e5bb6

Browse files
committed
internal reprensentation of bgp community change to plain community value
1 parent 5864f5a commit c9e5bb6

File tree

3 files changed

+19
-48
lines changed

3 files changed

+19
-48
lines changed

Diff for: pkg/bgpserver/connector.go

+8-37
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,8 @@ package bgpserver
1616

1717
import (
1818
"context"
19-
"errors"
2019
"fmt"
2120
"log/slog"
22-
"strconv"
23-
"strings"
2421

2522
gobgpapi "github.com/osrg/gobgp/v3/api"
2623
gobgpbgp "github.com/osrg/gobgp/v3/pkg/packet/bgp"
@@ -30,13 +27,13 @@ import (
3027

3128
type Route struct {
3229
Prefix string
33-
Community string
30+
Community uint32
3431
}
3532

3633
type Connector interface {
3734
Start(ctx context.Context) error
3835
AddPeer(neighbor string, remoteAS uint32, remotePort uint32, keepaliveIntervalSec uint64) error
39-
AddPath(prefix string, prefixLen uint32, nexthop string, community string) error
36+
AddPath(prefix string, prefixLen uint32, nexthop string, community uint32) error
4037
ListPath() ([]Route, error)
4138
}
4239

@@ -132,7 +129,7 @@ func (bs *bgpServerConnector) AddPeer(neighbor string, remoteAS uint32, remotePo
132129
)
133130
}
134131

135-
func (bs *bgpServerConnector) AddPath(prefix string, prefixLen uint32, nexthop string, community string) error {
132+
func (bs *bgpServerConnector) AddPath(prefix string, prefixLen uint32, nexthop string, community uint32) error {
136133
nlri1, err := apb.New(&gobgpapi.IPAddressPrefix{
137134
Prefix: prefix,
138135
PrefixLen: prefixLen,
@@ -149,12 +146,8 @@ func (bs *bgpServerConnector) AddPath(prefix string, prefixLen uint32, nexthop s
149146
attrNextHop, _ := apb.New(&gobgpapi.NextHopAttribute{
150147
NextHop: nexthop,
151148
})
152-
encodedCommunity, err := encodeCommunity(community)
153-
if err != nil {
154-
return err
155-
}
156149
attrCommunities, _ := apb.New(&gobgpapi.CommunitiesAttribute{
157-
Communities: []uint32{encodedCommunity},
150+
Communities: []uint32{community},
158151
})
159152
attrs = []*apb.Any{attrOrigin, attrNextHop, attrCommunities}
160153
}
@@ -199,11 +192,10 @@ func (bs *bgpServerConnector) ListPath() ([]Route, error) {
199192
}
200193

201194
for _, comm := range ca.Communities {
202-
decodedCommunity := decodeCommunity(comm)
203-
bs.logger.Debug("ListPath: found community", "community", decodedCommunity)
195+
bs.logger.Debug("ListPath: found community", "community", EncodeCommunity(comm))
204196
routes = append(routes, Route{
205197
Prefix: d.Prefix,
206-
Community: decodedCommunity,
198+
Community: comm,
207199
})
208200
}
209201
}
@@ -213,29 +205,8 @@ func (bs *bgpServerConnector) ListPath() ([]Route, error) {
213205
return routes, err
214206
}
215207

216-
// encodeCommunity converts plain community value to human readable notation(for example 65001:10)
217-
func encodeCommunity(comm string) (uint32, error) {
218-
nodes := strings.Split(comm, ":")
219-
if len(nodes) != 2 {
220-
return 0, errors.New("invaild community")
221-
}
222-
223-
upeer, err := strconv.Atoi(nodes[0])
224-
if err != nil {
225-
return 0, err
226-
}
227-
228-
lower, err := strconv.Atoi(nodes[1])
229-
if err != nil {
230-
return 0, err
231-
}
232-
commEncoded := uint32(upeer<<16 | lower)
233-
234-
return commEncoded, nil
235-
}
236-
237-
// decodeCommunity converts human readable notation to plain community value
238-
func decodeCommunity(comm uint32) string {
208+
// EncodeCommunity converts plain community value to human readable notation(for example 65001:10)
209+
func EncodeCommunity(comm uint32) string {
239210
upper := comm >> 16
240211
lower := comm & 0xffff
241212
commDecoded := fmt.Sprintf("%d:%d", upper, lower)

Diff for: pkg/bgpserver/fake_connector.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func (bs *FakeBgpServerConnector) AddPeer(_ string, _ uint32, _ uint32, _ uint64
3737
return nil
3838
}
3939

40-
func (bs *FakeBgpServerConnector) AddPath(prefix string, prefixLen uint32, _ string, _ string) error {
40+
func (bs *FakeBgpServerConnector) AddPath(prefix string, prefixLen uint32, _ string, _ uint32) error {
4141
p := fmt.Sprintf("%s/%d", prefix, prefixLen)
4242
bs.RouteConfigured[p] = true
4343

Diff for: pkg/controller/controller.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -72,22 +72,22 @@ const (
7272
)
7373

7474
const (
75-
bgpCommunity_Fault = "65001:1"
76-
bgpCommunity_Candidate = "65001:2"
77-
bgpCommunity_Primary = "65001:3"
78-
bgpCommunity_Replica = "65001:4"
79-
bgpCommunity_Anchor = "65001:10"
75+
bgpCommunity_Fault = 65001<<16 | 1 // 65001:1
76+
bgpCommunity_Candidate = 65001<<16 | 2 // 65001:2
77+
bgpCommunity_Primary = 65001<<16 | 3 // 65001:3
78+
bgpCommunity_Replica = 65001<<16 | 4 // 65001:4
79+
bgpCommunity_Anchor = 65001<<16 | 10 // 65001:10
8080
)
8181

8282
var (
83-
bgpCommunityToState = map[string]State{
83+
bgpCommunityToState = map[uint32]State{
8484
bgpCommunity_Fault: StateFault,
8585
bgpCommunity_Candidate: StateCandidate,
8686
bgpCommunity_Primary: StatePrimary,
8787
bgpCommunity_Replica: StateReplica,
8888
bgpCommunity_Anchor: StateAnchor,
8989
}
90-
stateToBgpCommunity = map[State]string{
90+
stateToBgpCommunity = map[State]uint32{
9191
StateFault: bgpCommunity_Fault,
9292
StateCandidate: bgpCommunity_Candidate,
9393
StatePrimary: bgpCommunity_Primary,
@@ -295,12 +295,12 @@ func (c *Controller) preDecideNextStateHandler() error {
295295

296296
currentNeighbors := newNeighborSet()
297297
for _, route := range routes {
298-
c.logger.Debug(fmt.Sprintf("found route %s %s", route.Prefix, route.Community))
298+
c.logger.Debug(fmt.Sprintf("found route %s %s", route.Prefix, bgpserver.EncodeCommunity(route.Community)))
299299

300300
// parse community
301301
state, ok := bgpCommunityToState[route.Community]
302302
if !ok {
303-
return fmt.Errorf("unknown community: %s", route.Community)
303+
return fmt.Errorf("unknown community: %s", bgpserver.EncodeCommunity(route.Community))
304304
}
305305

306306
// parse prefix
@@ -404,7 +404,7 @@ func (c *Controller) advertiseSelfNetIFAddress() error {
404404
if !ok {
405405
return errors.New("unknown state")
406406
}
407-
c.logger.Info("advertising my host address", "hostaddress", c.hostAddress, "community", comm)
407+
c.logger.Info("advertising my host address", "hostaddress", c.hostAddress, "community", bgpserver.EncodeCommunity(comm))
408408
return c.bgpServerConnector.AddPath(c.hostAddress, 32, c.hostAddress, comm)
409409
}
410410

0 commit comments

Comments
 (0)