Skip to content

Commit

Permalink
reset session and lsp if receive Close Msg
Browse files Browse the repository at this point in the history
  • Loading branch information
Motok1 committed Jun 2, 2022
1 parent c4e7a32 commit 959c41a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 72 deletions.
89 changes: 36 additions & 53 deletions pkg/packet/pcep/pcep.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,21 +564,23 @@ func (o *LspObject) DecodeFromBytes(data []uint8) error {
o.RFlag = (data[3] & 0x04) != 0
o.SFlag = (data[3] & 0x02) != 0
o.DFlag = (data[3] & 0x01) != 0
byteTlvs := data[4:]
for {
var tlv Tlv
tlv.DecodeFromBytes(byteTlvs)
if tlv.Type == uint16(TLV_SYMBOLIC_PATH_NAME) {
o.Name = string(tlv.Value)
}
o.Tlvs = append(o.Tlvs, tlv)

if int(tlv.getByteLength()) < len(byteTlvs) {
byteTlvs = byteTlvs[tlv.getByteLength():]
} else if int(tlv.getByteLength()) == len(byteTlvs) {
break
} else {
return errors.New("[pcep] Lsp TLV decode Error.\n")
if len(data) > 4 {
byteTlvs := data[4:]
for {
var tlv Tlv
tlv.DecodeFromBytes(byteTlvs)
if tlv.Type == uint16(TLV_SYMBOLIC_PATH_NAME) {
o.Name = string(removePadding(tlv.Value))
}
o.Tlvs = append(o.Tlvs, tlv)

if int(tlv.getByteLength()) < len(byteTlvs) {
byteTlvs = byteTlvs[tlv.getByteLength():]
} else if int(tlv.getByteLength()) == len(byteTlvs) {
break
} else {
return errors.New("[pcep] Lsp TLV decode Error.\n")
}
}
}
return nil
Expand Down Expand Up @@ -632,11 +634,11 @@ func NewLspObject(lspName string, plspId uint32) LspObject {
lspObject := LspObject{
Name: lspName,
PlspId: plspId,
OFlag: uint8(0),
OFlag: uint8(1),
AFlag: true, // https://datatracker.ietf.org/doc/html/rfc8231#section-7.3
RFlag: false,
SFlag: false,
DFlag: false,
DFlag: true,
Tlvs: []Tlv{
{
Type: TLV_SYMBOLIC_PATH_NAME,
Expand All @@ -649,23 +651,6 @@ func NewLspObject(lspName string, plspId uint32) LspObject {
return lspObject
}

func DecodeLspTLVsFromBytes(data []uint8) ([]Tlv, error) {
tlvs := []Tlv{}
for {
var tlv Tlv
tlv.DecodeFromBytes(data)
tlvs = append(tlvs, tlv)
if int(tlv.getByteLength()) < len(data) {
data = data[tlv.getByteLength():]
} else if int(tlv.getByteLength()) < len(data) {
break
} else {
return nil, errors.New("TLVs decode error.\n")
}
}
return tlvs, nil
}

//////////////////////// ero object //////////////////////////////
type EroObject struct {
SrEroSubobjects []SrEroSubobject
Expand Down Expand Up @@ -1010,20 +995,16 @@ func (o *PCInitiateMessage) Serialize() ([]uint8, error) {

//////////////////////// PCUpdate Message //////////////////////////////
type PCUpdMessage struct {
SrpObject SrpObject
LspObject LspObject
EndpointObject EndpointObject
EroObject EroObject
VendorInformationObject VendorInformationObject
SrpObject SrpObject
LspObject LspObject
EroObject EroObject
}

func NewPCUpdMessage(srpId uint32, lspName string, plspId uint32, labels []Label, color uint32, preference uint32, srcIPv4 []uint8, dstIPv4 []uint8) PCUpdMessage {
func NewPCUpdMessage(srpId uint32, lspName string, plspId uint32, labels []Label) PCUpdMessage {
var pcUpdMessage PCUpdMessage
pcUpdMessage.SrpObject = NewSrpObject(srpId, false)
pcUpdMessage.LspObject = NewLspObject(lspName, plspId) // PLSP-ID = 0
pcUpdMessage.EndpointObject = NewEndpointObject(1, dstIPv4, srcIPv4) // objectType = 1 (IPv4)
pcUpdMessage.LspObject = NewLspObject(lspName, plspId) // PLSP-ID = 0
pcUpdMessage.EroObject = NewEroObject(labels)
pcUpdMessage.VendorInformationObject = NewVendorInformationObject("Cisco", color, preference)
return pcUpdMessage
}

Expand All @@ -1036,26 +1017,18 @@ func (o *PCUpdMessage) Serialize() ([]uint8, error) {
if err != nil {
return nil, err
}
byteEndpointObject, err := o.EndpointObject.Serialize()
if err != nil {
return nil, err
}
byteEroObject, err := o.EroObject.Serialize()
if err != nil {
return nil, err
}
byteVendorInformationObject, err := o.VendorInformationObject.Serialize()
if err != nil {
return nil, err
}
pcupdHeaderLength := COMMON_HEADER_LENGTH + o.SrpObject.getByteLength() + o.LspObject.getByteLength() + o.EndpointObject.getByteLength() + o.EroObject.getByteLength() + o.VendorInformationObject.getByteLength()
pcupdHeaderLength := COMMON_HEADER_LENGTH + o.SrpObject.getByteLength() + o.LspObject.getByteLength() + o.EroObject.getByteLength()

pcupdHeader := NewCommonHeader(MT_UPDATE, pcupdHeaderLength)
bytePCUpdHeader, err := pcupdHeader.Serialize()
if err != nil {
return nil, err
}
bytePCUpdMessage := AppendByteSlices(bytePCUpdHeader, byteSrpObject, byteLspObject, byteEndpointObject, byteEroObject, byteVendorInformationObject)
bytePCUpdMessage := AppendByteSlices(bytePCUpdHeader, byteSrpObject, byteLspObject, byteEroObject)

return bytePCUpdMessage, nil
}
Expand All @@ -1069,3 +1042,13 @@ func i32tob(value uint32) []uint8 {
}
return bytes
}

func removePadding(data []uint8) []uint8 {
for {
if data[len(data)-1] == 0x00 {
data = data[:len(data)-1]
} else {
return data
}
}
}
28 changes: 13 additions & 15 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ func NewPce(o *PceOptions) error {
// sessionList の表示
for {
select {

case lsp := <-lspChan:
// 更新用に旧 LSP 情報を削除する
s.removeLsp(lsp)
Expand All @@ -68,7 +67,6 @@ func NewPce(o *PceOptions) error {
s.printSessionList()
s.printLspList()
}

}
}

Expand Down Expand Up @@ -101,7 +99,7 @@ func (s *Server) Listen(address string, port string, lspChan chan Lsp) error {
s.sessionList = append(s.sessionList, session)
go func() {
session.Established()
s.removeSession(session.sessionId)
s.removeSession(session)
}()
sessionId += 1
}
Expand Down Expand Up @@ -138,7 +136,7 @@ func (s *Server) CreateLsp(ctx context.Context, lspData *pb.LspData) (*pb.LspSta
}
if plspId := s.getPlspId(lspData); plspId != 0 {
fmt.Printf("plspId check : %d\n\n", plspId)
if err := pcepSession.SendPCUpdate(lspData.GetPolicyName(), plspId, labels, lspData.GetColor(), uint32(100), lspData.GetSrcAddr(), lspData.GetDstAddr()); err != nil {
if err := pcepSession.SendPCUpdate(lspData.GetPolicyName(), plspId, labels); err != nil {
return &pb.LspStatus{IsSuccess: false}, err
}
} else {
Expand Down Expand Up @@ -174,32 +172,32 @@ func (s *Server) printLspList() {
fmt.Printf("*************************\n")
}

func (s *Server) removeSession(sessionId uint8) {
func (s *Server) removeSession(session *Session) {
// Session List の掃除
for i, v := range s.sessionList {
if v.sessionId == sessionId {
if v.sessionId == session.sessionId {
s.sessionList[i] = s.sessionList[len(s.sessionList)-1]
s.sessionList = s.sessionList[:len(s.sessionList)-1]
break
}
}
// Lsp List の掃除
newLspList := []Lsp{}
for _, v := range s.lspList {
if !v.peerAddr.Equal(session.peerAddr) {
newLspList = append(newLspList, v)
}
}
s.lspList = newLspList
}

func (s *Server) getPlspId(lspData *pb.LspData) uint32 {
for _, v := range s.lspList {
fmt.Printf("list name: %#v\n", []byte(v.name))
fmt.Printf("new name: %#v\n", []byte(lspData.GetPolicyName()))
fmt.Printf("list addr: %v\n", v.peerAddr)
fmt.Printf("new addr: %v\n\n", net.IP(lspData.GetPcepSessionAddr()))
fmt.Printf("first: %t\n", v.name == lspData.GetPolicyName())
fmt.Printf("second: %t\n\n", v.peerAddr.Equal(net.IP(lspData.GetPcepSessionAddr())))

if v.name == lspData.GetPolicyName() && v.peerAddr.Equal(net.IP(lspData.GetPcepSessionAddr())) {
fmt.Printf("[get!!!]PlspID: %d\n", v.plspId)
return v.plspId
}
}
// 存在しない場合は PCInitiate 用の PLSP-ID: 0 を返す
fmt.Printf("awefawefawefawe")
return 0
}

Expand Down
11 changes: 7 additions & 4 deletions pkg/server/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,11 @@ func (s *Session) ReceivePcepMessage() error {
case pcep.MT_CLOSE:
fmt.Printf("[PCEP] Received Close\n")
// receive を中断する
err := fmt.Errorf("PCEP session Close")
return err
// error 処理ではない気がする
// err := fmt.Errorf("PCEP session Close")
// return err
return nil

default:
fmt.Printf("[PCEP] Received Unimplemented Message-Type: %v\n", commonHeader.MessageType)
// TODO: このパケットを記録して捨てる
Expand All @@ -272,10 +275,10 @@ func (s *Session) SendPCInitiate(policyName string, labels []pcep.Label, color u
return nil
}

func (s *Session) SendPCUpdate(policyName string, plspId uint32, labels []pcep.Label, color uint32, preference uint32, srcIPv4 []uint8, dstIPv4 []uint8) error {
func (s *Session) SendPCUpdate(policyName string, plspId uint32, labels []pcep.Label) error {
// PLSP ID も入りそう
fmt.Printf(" *********************Start PCUpdate \n")
pcupdateMessage := pcep.NewPCUpdMessage(s.srpIdHead, policyName, plspId, labels, color, preference, srcIPv4, dstIPv4)
pcupdateMessage := pcep.NewPCUpdMessage(s.srpIdHead, policyName, plspId, labels)

bytePCUpdMessage, err := pcupdateMessage.Serialize()
if err != nil {
Expand Down

0 comments on commit 959c41a

Please sign in to comment.