Skip to content

Commit

Permalink
Modify the method of create/update SR Policy
Browse files Browse the repository at this point in the history
  • Loading branch information
Motok1 committed Jun 8, 2024
1 parent 24a62c5 commit ccd4d3c
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 34 deletions.
53 changes: 53 additions & 0 deletions internal/pkg/table/sr_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,59 @@ type SRPolicy struct {
State PolicyState
}

func NewSRPolicy(
plspId uint32,
name string,
segmentList []Segment,
srcAddr netip.Addr,
dstAddr netip.Addr,
color uint32,
preference uint32,
lspID uint16,
state PolicyState,
) *SRPolicy {
p := &SRPolicy{
PlspID: plspId,
Name: name,
SegmentList: segmentList,
SrcAddr: srcAddr,
DstAddr: dstAddr,
Color: color,
Preference: preference,
LspID: lspID,
State: state,
}

return p
}

// SR Policy parameter that can be changed
type PolicyDiff struct {
Name *string
Color *uint32
Preference *uint32
SegmentList []Segment
LspID uint16
State PolicyState
}

func (p *SRPolicy) Update(df PolicyDiff) {
p.State = df.State
p.LspID = df.LspID
if df.Name != nil {
p.Name = *df.Name
}
if df.Color != nil {
p.Color = *df.Color
}
if df.Preference != nil {
p.Preference = *df.Preference
}
if df.SegmentList != nil {
p.SegmentList = df.SegmentList
}
}

type Segment interface {
SidString() string
}
Expand Down
21 changes: 0 additions & 21 deletions pkg/packet/pcep/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,27 +287,6 @@ func (r *StateReport) decodeVendorInformationObject(objectType uint8, objectBody
return r.VendorInformationObject.DecodeFromBytes(objectType, objectBody)
}

func (r *StateReport) ToSRPolicy(pcc PccType) table.SRPolicy {
srPolicy := table.SRPolicy{
PlspID: r.LspObject.PlspID,
Name: r.LspObject.Name,
SegmentList: []table.Segment{},
SrcAddr: r.LspObject.SrcAddr,
DstAddr: r.LspObject.DstAddr,
}
if pcc == CISCO_LEGACY {
srPolicy.Color = r.VendorInformationObject.Color()
srPolicy.Preference = r.VendorInformationObject.Preference()
} else {
srPolicy.Color = r.AssociationObject.Color()
srPolicy.Preference = r.AssociationObject.Preference()
}

srPolicy.SegmentList = r.EroObject.ToSegmentList()

return srPolicy
}

// PCRpt Message
type PCRptMessage struct {
StateReports []*StateReport
Expand Down
4 changes: 2 additions & 2 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ func (s *Server) SearchSession(peerAddr netip.Addr) *Session {
}

// SRPolicies returns a map of registered SR Policy with key sessionAddr
func (s *Server) SRPolicies() map[netip.Addr][]table.SRPolicy {
srPolicies := make(map[netip.Addr][]table.SRPolicy)
func (s *Server) SRPolicies() map[netip.Addr][]*table.SRPolicy {
srPolicies := make(map[netip.Addr][]*table.SRPolicy)
for _, ss := range s.sessionList {
if ss.isSynced {
srPolicies[ss.peerAddr] = ss.srPolicies
Expand Down
92 changes: 81 additions & 11 deletions pkg/server/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type Session struct {
tcpConn *net.TCPConn
isSynced bool
srpIDHead uint32 // 0x00000000 and 0xFFFFFFFF are reserved.
srPolicies []table.SRPolicy
srPolicies []*table.SRPolicy
logger *zap.Logger
keepAlive uint8
pccType pcep.PccType
Expand Down Expand Up @@ -263,25 +263,34 @@ func (ss *Session) handlePCRpt(length uint16) error {
}

for _, sr := range message.StateReports {
// synchronization
if sr.LspObject.SFlag {
srPolicy := sr.ToSRPolicy(ss.pccType)
ss.logger.Info("Synchronize SR Policy information", zap.String("session", ss.peerAddr.String()), zap.Any("SRPolicy", srPolicy), zap.Any("Message", message))
go ss.RegisterSRPolicy(srPolicy)
ss.logger.Info("Synchronize SR Policy information", zap.String("session", ss.peerAddr.String()), zap.Any("Message", message))
ss.RegisterSRPolicy(*sr)
} else if !sr.LspObject.SFlag {
switch {
// finish synchronization
case sr.LspObject.PlspID == 0:
ss.logger.Info("Finish PCRpt state synchronization", zap.String("session", ss.peerAddr.String()))
ss.isSynced = true
// response to request from PCE
case sr.SrpObject.SrpID != 0:
srPolicy := sr.ToSRPolicy(ss.pccType)
ss.logger.Info("Finish Stateful PCE request", zap.String("session", ss.peerAddr.String()), zap.Uint32("srpID", sr.SrpObject.SrpID))
go ss.RegisterSRPolicy(srPolicy)
if sr.LspObject.RFlag {
ss.DeleteSRPolicy(*sr)
} else {
ss.RegisterSRPolicy(*sr)
}

default:
// TODO: Need to implementation of PCUpdate for Passive stateful PCE
if sr.LspObject.RFlag {
ss.DeleteSRPolicy(*sr)
} else {
ss.RegisterSRPolicy(*sr)
}
}
}
}

return nil
}

Expand Down Expand Up @@ -309,9 +318,61 @@ func (ss *Session) SendPCUpdate(srPolicy table.SRPolicy) error {
return err
}

func (ss *Session) RegisterSRPolicy(srPolicy table.SRPolicy) {
ss.DeleteSRPolicy(srPolicy.PlspID)
ss.srPolicies = append(ss.srPolicies, srPolicy)
func (ss *Session) RegisterSRPolicy(sr pcep.StateReport) {
var color, preference uint32

if ss.pccType == pcep.CISCO_LEGACY {
color = sr.VendorInformationObject.Color()
preference = sr.VendorInformationObject.Preference()
} else {
color = sr.AssociationObject.Color()
preference = sr.AssociationObject.Preference()
}

lspID := sr.LspObject.LspID

var state table.PolicyState
switch sr.LspObject.OFlag {
case uint8(0x00):
state = table.POLICY_DOWN
case uint8(0x01):
state = table.POLICY_UP
case uint8(0x02):
state = table.POLICY_ACTIVE
default:
state = table.POLICY_UNKNOWN
}

if p, ok := ss.SearchSRPolicy(sr.LspObject.PlspID); ok {
// update
// If the LSP ID is old, it is not the latest data update.
if p.LspID <= lspID {
p.Update(
table.PolicyDiff{
Name: &sr.LspObject.Name,
Color: &color,
Preference: &preference,
SegmentList: sr.EroObject.ToSegmentList(),
LspID: lspID,
State: state,
},
)
}
} else {
// create
p := table.NewSRPolicy(
sr.LspObject.PlspID,
sr.LspObject.Name,
sr.EroObject.ToSegmentList(),
sr.LspObject.SrcAddr,
sr.LspObject.DstAddr,
color,
preference,
lspID,
state,
)
ss.srPolicies = append(ss.srPolicies, p)
}
}

func (ss *Session) DeleteSRPolicy(sr pcep.StateReport) {
Expand All @@ -326,6 +387,15 @@ func (ss *Session) DeleteSRPolicy(sr pcep.StateReport) {
}
}

func (ss *Session) SearchSRPolicy(plspID uint32) (*table.SRPolicy, bool) {
for _, v := range ss.srPolicies {
if v.PlspID == plspID {
return v, true
}
}
return nil, false
}

// SearchSRPolicyPlspID returns the PLSP-ID of a registered SR Policy, along with a boolean value indicating if it was found.
func (ss *Session) SearchSRPolicyPlspID(color uint32, endpoint netip.Addr) (uint32, bool) {
for _, v := range ss.srPolicies {
Expand Down

0 comments on commit ccd4d3c

Please sign in to comment.