From 9e4720fe1d6ee92bfbe8d6f33d561b8cb300ca8d Mon Sep 17 00:00:00 2001 From: lpzgithub Date: Wed, 14 Mar 2018 11:58:02 +0800 Subject: [PATCH 1/4] 1. Add MultipartType_PortDesc 2. modify PortStatus UnmarshalBinary Parse field is incorrect --- openflow13/multipart.go | 49 ++++++++++++++++++++++++++++++++++++++++ openflow13/openflow13.go | 2 +- openflow13/port.go | 2 ++ 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/openflow13/multipart.go b/openflow13/multipart.go index 38cb1bc..dfd8f23 100644 --- a/openflow13/multipart.go +++ b/openflow13/multipart.go @@ -141,9 +141,13 @@ func (s *MultipartReply) UnmarshalBinary(data []byte) error { repl = new(TableStats) case MultipartType_Queue: repl = new(QueueStats) + case MultipartType_PortDesc: + repl = new(PortDesc) // FIXME: Support all types case MultipartType_Experimenter: break + default: + return fmt.Errorf("No kown mp type ") } err = repl.UnmarshalBinary(data[n:]) @@ -919,6 +923,8 @@ func NewPortStatus() *PortStatus { p := new(PortStatus) p.Header = NewOfp13Header() p.pad = make([]byte, 7) + + p.Desc = *NewPhyPort() return p } @@ -958,6 +964,49 @@ func (s *PortStatus) UnmarshalBinary(data []byte) error { return err } +///Port Description reply +type PortDesc struct { + ports []*PhyPort +} + +func (s PortDesc) Len() (n uint16) { + + n = 0 + + for _, r := range s.ports { + n += uint16(r.Len()) + } + return +} + +func (s *PortDesc) MarshalBinary() (data []byte, err error) { + + b := make([]byte, 8) + + for _, r := range s.ports { + b, err = r.MarshalBinary() + data = append(data, b...) + } + return +} +func (s *PortDesc) UnmarshalBinary(data []byte) error { + + nPorts := (len(data)) / 64 + if nPorts == 0 { + return nil + } + s.ports = make([]*PhyPort, nPorts) + for i := 0; i < nPorts; i++ { + buf := data[i*64:] + s.ports[i] = NewPhyPort() + if err := s.ports[i].UnmarshalBinary(buf[0:64]); err != nil { + return err + } + } + + return nil +} + // ofp_port_reason 1.0 const ( PR_ADD = iota diff --git a/openflow13/openflow13.go b/openflow13/openflow13.go index ea83c9b..72c008b 100644 --- a/openflow13/openflow13.go +++ b/openflow13/openflow13.go @@ -137,7 +137,7 @@ func Parse(b []byte) (message util.Message, err error) { message = NewFlowRemoved() err = message.UnmarshalBinary(b) case Type_PortStatus: - message = new(PortStatus) + message = NewPortStatus() err = message.UnmarshalBinary(b) case Type_PacketOut: break diff --git a/openflow13/port.go b/openflow13/port.go index 57c6b30..a5535c0 100644 --- a/openflow13/port.go +++ b/openflow13/port.go @@ -31,6 +31,8 @@ func NewPhyPort() *PhyPort { p := new(PhyPort) p.HWAddr = make([]byte, ETH_ALEN) p.Name = make([]byte, 16) + p.pad = make([]byte, 4) + p.pad2 = make([]byte, 2) return p } From 948d8e2b0d792958c7aa272d26761a10dea1f1a8 Mon Sep 17 00:00:00 2001 From: lpzgithub Date: Wed, 14 Mar 2018 12:06:58 +0800 Subject: [PATCH 2/4] modiy the Segment error if MultipartRequest is no body --- openflow13/multipart.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/openflow13/multipart.go b/openflow13/multipart.go index dfd8f23..f94e9d4 100644 --- a/openflow13/multipart.go +++ b/openflow13/multipart.go @@ -2,6 +2,7 @@ package openflow13 import ( "encoding/binary" + "fmt" log "github.com/Sirupsen/logrus" @@ -19,7 +20,11 @@ type MultipartRequest struct { } func (s *MultipartRequest) Len() (n uint16) { - return s.Header.Len() + 8 + s.Body.Len() + if s.Body != nil { + return s.Header.Len() + 8 + s.Body.Len() + } else { + return s.Header.Len() + 8 + } } func (s *MultipartRequest) MarshalBinary() (data []byte, err error) { @@ -34,10 +39,10 @@ func (s *MultipartRequest) MarshalBinary() (data []byte, err error) { n += 2 n += 4 // for padding data = append(data, b...) - - b, err = s.Body.MarshalBinary() - data = append(data, b...) - + if s.Body != nil { + b, err = s.Body.MarshalBinary() + data = append(data, b...) + } log.Debugf("Sending MultipartRequest (%d): %v", len(data), data) return From 41e91da8a00388a52f16bfe7d2cac4fe0071f58b Mon Sep 17 00:00:00 2001 From: lpzgithub Date: Sun, 18 Mar 2018 10:16:38 +0800 Subject: [PATCH 3/4] remove these extra spaces --- openflow13/multipart.go | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/openflow13/multipart.go b/openflow13/multipart.go index f94e9d4..8e7b963 100644 --- a/openflow13/multipart.go +++ b/openflow13/multipart.go @@ -20,11 +20,7 @@ type MultipartRequest struct { } func (s *MultipartRequest) Len() (n uint16) { - if s.Body != nil { - return s.Header.Len() + 8 + s.Body.Len() - } else { - return s.Header.Len() + 8 - } + return s.Header.Len() + 8 + s.Body.Len() } func (s *MultipartRequest) MarshalBinary() (data []byte, err error) { @@ -39,10 +35,10 @@ func (s *MultipartRequest) MarshalBinary() (data []byte, err error) { n += 2 n += 4 // for padding data = append(data, b...) - if s.Body != nil { - b, err = s.Body.MarshalBinary() - data = append(data, b...) - } + + b, err = s.Body.MarshalBinary() + data = append(data, b...) + log.Debugf("Sending MultipartRequest (%d): %v", len(data), data) return From ae054162acaa73a88889aac8689e9d76227a7b12 Mon Sep 17 00:00:00 2001 From: lpzgithub Date: Sun, 18 Mar 2018 10:18:40 +0800 Subject: [PATCH 4/4] remove these extra spaces --- openflow13/multipart.go | 6 ------ 1 file changed, 6 deletions(-) diff --git a/openflow13/multipart.go b/openflow13/multipart.go index 8e7b963..505a600 100644 --- a/openflow13/multipart.go +++ b/openflow13/multipart.go @@ -3,9 +3,7 @@ package openflow13 import ( "encoding/binary" "fmt" - log "github.com/Sirupsen/logrus" - "github.com/contiv/libOpenflow/common" "github.com/contiv/libOpenflow/util" ) @@ -924,7 +922,6 @@ func NewPortStatus() *PortStatus { p := new(PortStatus) p.Header = NewOfp13Header() p.pad = make([]byte, 7) - p.Desc = *NewPhyPort() return p } @@ -971,9 +968,7 @@ type PortDesc struct { } func (s PortDesc) Len() (n uint16) { - n = 0 - for _, r := range s.ports { n += uint16(r.Len()) } @@ -981,7 +976,6 @@ func (s PortDesc) Len() (n uint16) { } func (s *PortDesc) MarshalBinary() (data []byte, err error) { - b := make([]byte, 8) for _, r := range s.ports {