diff --git a/protocols/bgp/packet/decoder.go b/protocols/bgp/packet/decoder.go index 9bb71f1a8fa01945eca0a2764cdada5ad578595c..c4d583f2138edfbbfb5b9be33b2a1c11764ee0bd 100644 --- a/protocols/bgp/packet/decoder.go +++ b/protocols/bgp/packet/decoder.go @@ -9,12 +9,8 @@ import ( "github.com/taktv6/tflow2/convert" ) -type DecodingOptions struct { - Supports4OctetASN bool -} - // Decode decodes a BGP message -func Decode(buf *bytes.Buffer, opt *DecodingOptions) (*BGPMessage, error) { +func Decode(buf *bytes.Buffer, opt *Options) (*BGPMessage, error) { hdr, err := decodeHeader(buf) if err != nil { return nil, fmt.Errorf("Failed to decode header: %v", err) @@ -31,7 +27,7 @@ func Decode(buf *bytes.Buffer, opt *DecodingOptions) (*BGPMessage, error) { }, nil } -func decodeMsgBody(buf *bytes.Buffer, msgType uint8, l uint16, opt *DecodingOptions) (interface{}, error) { +func decodeMsgBody(buf *bytes.Buffer, msgType uint8, l uint16, opt *Options) (interface{}, error) { switch msgType { case OpenMsg: return decodeOpenMsg(buf) @@ -45,7 +41,7 @@ func decodeMsgBody(buf *bytes.Buffer, msgType uint8, l uint16, opt *DecodingOpti return nil, fmt.Errorf("Unknown message type: %d", msgType) } -func decodeUpdateMsg(buf *bytes.Buffer, l uint16, opt *DecodingOptions) (*BGPUpdate, error) { +func decodeUpdateMsg(buf *bytes.Buffer, l uint16, opt *Options) (*BGPUpdate, error) { msg := &BGPUpdate{} err := decode(buf, []interface{}{&msg.WithdrawnRoutesLen}) diff --git a/protocols/bgp/packet/decoder_test.go b/protocols/bgp/packet/decoder_test.go index a2ad9771d80f102ca8acccd2ff5d6a362160e566..56416186ba1d714a13fb4f248281d3d5f48451dd 100644 --- a/protocols/bgp/packet/decoder_test.go +++ b/protocols/bgp/packet/decoder_test.go @@ -71,7 +71,7 @@ func BenchmarkDecodeUpdateMsg(b *testing.B) { for i := 0; i < b.N; i++ { buf := bytes.NewBuffer(input) - _, err := decodeUpdateMsg(buf, uint16(len(input)), &DecodingOptions{}) + _, err := decodeUpdateMsg(buf, uint16(len(input)), &Options{}) if err != nil { fmt.Printf("decodeUpdateMsg failed: %v\n", err) } @@ -252,7 +252,7 @@ func TestDecode(t *testing.T) { for _, test := range tests { buf := bytes.NewBuffer(test.input) - msg, err := Decode(buf, &DecodingOptions{}) + msg, err := Decode(buf, &Options{}) if err != nil && !test.wantFail { t.Errorf("Unexpected error in test %d: %v", test.testNum, err) @@ -1465,7 +1465,7 @@ func TestDecodeUpdateMsg(t *testing.T) { if l == 0 { l = uint16(len(test.input)) } - msg, err := decodeUpdateMsg(buf, l, &DecodingOptions{}) + msg, err := decodeUpdateMsg(buf, l, &Options{}) if err != nil && !test.wantFail { t.Fatalf("Unexpected error in test %d: %v", test.testNum, err) @@ -1501,7 +1501,7 @@ func TestDecodeMsgBody(t *testing.T) { } for _, test := range tests { - res, err := decodeMsgBody(test.buffer, test.msgType, test.length, &DecodingOptions{}) + res, err := decodeMsgBody(test.buffer, test.msgType, test.length, &Options{}) if test.wantFail && err == nil { t.Errorf("Expected error dit not happen in test %q", test.name) } diff --git a/protocols/bgp/packet/encoder.go b/protocols/bgp/packet/encoder.go index a75e2441b3e499f9f8662ba0bb793d8e9e45d9eb..ac9bc4483199b81f4e8ecc25c6b91e3ec8361b9c 100644 --- a/protocols/bgp/packet/encoder.go +++ b/protocols/bgp/packet/encoder.go @@ -7,10 +7,6 @@ import ( "github.com/taktv6/tflow2/convert" ) -type EncodingOptions struct { - Supports4OctetASN bool -} - func SerializeKeepaliveMsg() []byte { keepaliveLen := uint16(19) buf := bytes.NewBuffer(make([]byte, 0, keepaliveLen)) @@ -67,7 +63,7 @@ func serializeHeader(buf *bytes.Buffer, length uint16, typ uint8) { buf.WriteByte(typ) } -func (b *BGPUpdateAddPath) SerializeUpdate(opt *EncodingOptions) ([]byte, error) { +func (b *BGPUpdateAddPath) SerializeUpdate(opt *Options) ([]byte, error) { budget := MaxLen - MinLen buf := bytes.NewBuffer(nil) @@ -126,7 +122,7 @@ func (b *BGPUpdateAddPath) SerializeUpdate(opt *EncodingOptions) ([]byte, error) return buf.Bytes(), nil } -func (b *BGPUpdate) SerializeUpdate(opt *EncodingOptions) ([]byte, error) { +func (b *BGPUpdate) SerializeUpdate(opt *Options) ([]byte, error) { budget := MaxLen - MinLen buf := bytes.NewBuffer(nil) diff --git a/protocols/bgp/packet/options.go b/protocols/bgp/packet/options.go new file mode 100644 index 0000000000000000000000000000000000000000..b2d35bc60cbe17490f94314c28da26de9a19e40a --- /dev/null +++ b/protocols/bgp/packet/options.go @@ -0,0 +1,5 @@ +package packet + +type Options struct { + Supports4OctetASN bool +} diff --git a/protocols/bgp/packet/path_attributes.go b/protocols/bgp/packet/path_attributes.go index 0cee22686658fbbafb9bc8868315a26dc8a7a8a1..acd5c78ab813c5b3cc6442f61067c85471366545 100644 --- a/protocols/bgp/packet/path_attributes.go +++ b/protocols/bgp/packet/path_attributes.go @@ -9,7 +9,7 @@ import ( "github.com/taktv6/tflow2/convert" ) -func decodePathAttrs(buf *bytes.Buffer, tpal uint16, opt *DecodingOptions) (*PathAttribute, error) { +func decodePathAttrs(buf *bytes.Buffer, tpal uint16, opt *Options) (*PathAttribute, error) { var ret *PathAttribute var eol *PathAttribute var pa *PathAttribute @@ -36,7 +36,7 @@ func decodePathAttrs(buf *bytes.Buffer, tpal uint16, opt *DecodingOptions) (*Pat return ret, nil } -func decodePathAttr(buf *bytes.Buffer, opt *DecodingOptions) (pa *PathAttribute, consumed uint16, err error) { +func decodePathAttr(buf *bytes.Buffer, opt *Options) (pa *PathAttribute, consumed uint16, err error) { pa = &PathAttribute{} err = decodePathAttrFlags(buf, pa) @@ -409,7 +409,7 @@ func dumpNBytes(buf *bytes.Buffer, n uint16) error { return nil } -func (pa *PathAttribute) serialize(buf *bytes.Buffer, opt *EncodingOptions) uint8 { +func (pa *PathAttribute) serialize(buf *bytes.Buffer, opt *Options) uint8 { pathAttrLen := uint8(0) switch pa.TypeCode { @@ -447,7 +447,7 @@ func (pa *PathAttribute) serializeOrigin(buf *bytes.Buffer) uint8 { return 4 } -func (pa *PathAttribute) serializeASPath(buf *bytes.Buffer, opt *EncodingOptions) uint8 { +func (pa *PathAttribute) serializeASPath(buf *bytes.Buffer, opt *Options) uint8 { attrFlags := uint8(0) attrFlags = setTransitive(attrFlags) buf.WriteByte(attrFlags) diff --git a/protocols/bgp/packet/path_attributes_test.go b/protocols/bgp/packet/path_attributes_test.go index 5889b913263f8fb4913ca18d3ea376365d7be8b4..c3d5ecda2e4d484ba48809651248e4ecd1c82188 100644 --- a/protocols/bgp/packet/path_attributes_test.go +++ b/protocols/bgp/packet/path_attributes_test.go @@ -50,7 +50,7 @@ func TestDecodePathAttrs(t *testing.T) { } for _, test := range tests { - res, err := decodePathAttrs(bytes.NewBuffer(test.input), uint16(len(test.input)), &DecodingOptions{}) + res, err := decodePathAttrs(bytes.NewBuffer(test.input), uint16(len(test.input)), &Options{}) if test.wantFail && err == nil { t.Errorf("Expected error did not happen for test %q", test.name) @@ -173,7 +173,7 @@ func TestDecodePathAttr(t *testing.T) { } for _, test := range tests { - res, _, err := decodePathAttr(bytes.NewBuffer(test.input), &DecodingOptions{}) + res, _, err := decodePathAttr(bytes.NewBuffer(test.input), &Options{}) if test.wantFail && err == nil { t.Errorf("Expected error did not happen for test %q", test.name) @@ -1330,7 +1330,7 @@ func TestSerializeASPath(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { buf := bytes.NewBuffer(nil) - opt := &EncodingOptions{ + opt := &Options{ Supports4OctetASN: test.use32BitASN, } n := test.input.serializeASPath(buf, opt) @@ -1625,7 +1625,7 @@ func TestSerialize(t *testing.T) { } for _, test := range tests { - opt := &EncodingOptions{} + opt := &Options{} res, err := test.msg.SerializeUpdate(opt) if err != nil { if test.wantFail { @@ -1832,7 +1832,7 @@ func TestSerializeAddPath(t *testing.T) { } for _, test := range tests { - opt := &EncodingOptions{} + opt := &Options{} res, err := test.msg.SerializeUpdate(opt) if err != nil { if test.wantFail { diff --git a/protocols/bgp/server/fsm.go b/protocols/bgp/server/fsm.go index ab2eff08cf4d2e20f16f56f9b5890f9c36cf5aa8..e5c98e1f1b0378c9a2184c9a87f60c5c0571b1c9 100644 --- a/protocols/bgp/server/fsm.go +++ b/protocols/bgp/server/fsm.go @@ -56,8 +56,7 @@ type FSM struct { capAddPathSend bool capAddPathRecv bool - decodingOptions *packet.DecodingOptions - encodingOptions *packet.EncodingOptions + options *packet.Options local net.IP @@ -102,8 +101,7 @@ func newFSM2(peer *peer) *FSM { msgRecvFailCh: make(chan error), stopMsgRecvCh: make(chan struct{}), rib: peer.rib, - decodingOptions: &packet.DecodingOptions{}, - encodingOptions: &packet.EncodingOptions{}, + options: &packet.Options{}, } } diff --git a/protocols/bgp/server/fsm_established.go b/protocols/bgp/server/fsm_established.go index bc5a460611aeed6f625d5a568f5e91a945febd32..307143549116f1afdbe59684f50bed1a8ea55252 100644 --- a/protocols/bgp/server/fsm_established.go +++ b/protocols/bgp/server/fsm_established.go @@ -154,7 +154,7 @@ func (s *establishedState) keepaliveTimerExpired() (state, string) { } func (s *establishedState) msgReceived(data []byte) (state, string) { - msg, err := packet.Decode(bytes.NewBuffer(data), s.fsm.decodingOptions) + msg, err := packet.Decode(bytes.NewBuffer(data), s.fsm.options) if err != nil { switch bgperr := err.(type) { case packet.BGPError: diff --git a/protocols/bgp/server/fsm_open_confirm.go b/protocols/bgp/server/fsm_open_confirm.go index 75947171112f5c29b528a25cd479a87cea594fe8..07b8a6152dd64c56055c7cbd0745980e462c3886 100644 --- a/protocols/bgp/server/fsm_open_confirm.go +++ b/protocols/bgp/server/fsm_open_confirm.go @@ -82,7 +82,7 @@ func (s *openConfirmState) keepaliveTimerExpired() (state, string) { } func (s *openConfirmState) msgReceived(data []byte) (state, string) { - msg, err := packet.Decode(bytes.NewBuffer(data), s.fsm.decodingOptions) + msg, err := packet.Decode(bytes.NewBuffer(data), s.fsm.options) if err != nil { switch bgperr := err.(type) { case packet.BGPError: diff --git a/protocols/bgp/server/fsm_open_sent.go b/protocols/bgp/server/fsm_open_sent.go index c885d1294256f1181333ba790697369f0685bacd..848c76697fb3a415b7e56131b67d3576d0e01722 100644 --- a/protocols/bgp/server/fsm_open_sent.go +++ b/protocols/bgp/server/fsm_open_sent.go @@ -74,7 +74,7 @@ func (s *openSentState) holdTimerExpired() (state, string) { } func (s *openSentState) msgReceived(data []byte) (state, string) { - msg, err := packet.Decode(bytes.NewBuffer(data), s.fsm.decodingOptions) + msg, err := packet.Decode(bytes.NewBuffer(data), s.fsm.options) if err != nil { switch bgperr := err.(type) { case packet.BGPError: @@ -199,8 +199,7 @@ func (s *openSentState) processAddPathCapability(addPathCap packet.AddPathCapabi } func (s *openSentState) processASN4Capability(cap packet.ASN4Capability) { - s.fsm.decodingOptions.Supports4OctetASN = true - s.fsm.encodingOptions.Supports4OctetASN = true + s.fsm.options.Supports4OctetASN = true if s.peerASNRcvd == packet.ASTransASN { s.peerASNRcvd = cap.ASN4 diff --git a/protocols/bgp/server/update_helper.go b/protocols/bgp/server/update_helper.go index 02a89bee791ca93aa03b5c22d778a2c320da2e07..f0435a808797fb9079809d381c514f4e4cf35b82 100644 --- a/protocols/bgp/server/update_helper.go +++ b/protocols/bgp/server/update_helper.go @@ -72,10 +72,10 @@ func addOptionalPathAttribues(p *route.Path, parent *packet.PathAttribute) error } type serializeAbleUpdate interface { - SerializeUpdate(opt *packet.EncodingOptions) ([]byte, error) + SerializeUpdate(opt *packet.Options) ([]byte, error) } -func serializeAndSendUpdate(out io.Writer, update serializeAbleUpdate, opt *packet.EncodingOptions) error { +func serializeAndSendUpdate(out io.Writer, update serializeAbleUpdate, opt *packet.Options) error { updateBytes, err := update.SerializeUpdate(opt) if err != nil { log.Errorf("Unable to serialize BGP Update: %v", err) diff --git a/protocols/bgp/server/update_sender.go b/protocols/bgp/server/update_sender.go index 0a0f51ae4c3b1f36288acdeb717e869d7ef8ef44..a6ae74afeb29d7e5c3b4ec9538a6ecc18ffce32f 100644 --- a/protocols/bgp/server/update_sender.go +++ b/protocols/bgp/server/update_sender.go @@ -42,12 +42,12 @@ func (u *UpdateSender) AddPath(pfx net.Prefix, p *route.Path) error { }, } - return serializeAndSendUpdate(u.fsm.con, update, u.fsm.encodingOptions) + return serializeAndSendUpdate(u.fsm.con, update, u.fsm.options) } // RemovePath withdraws prefix `pfx` from a peer func (u *UpdateSender) RemovePath(pfx net.Prefix, p *route.Path) bool { - err := withDrawPrefixes(u.fsm.con, u.fsm.encodingOptions, pfx) + err := withDrawPrefixes(u.fsm.con, u.fsm.options, pfx) return err == nil } diff --git a/protocols/bgp/server/update_sender_add_path.go b/protocols/bgp/server/update_sender_add_path.go index 6930465540710f3d5a778826bf89141940437f16..afdb71f31ba31618bed84f175478cc95f18f850f 100644 --- a/protocols/bgp/server/update_sender_add_path.go +++ b/protocols/bgp/server/update_sender_add_path.go @@ -39,12 +39,12 @@ func (u *UpdateSenderAddPath) AddPath(pfx net.Prefix, p *route.Path) error { Pfxlen: pfx.Pfxlen(), }, } - return serializeAndSendUpdate(u.fsm.con, update, u.fsm.encodingOptions) + return serializeAndSendUpdate(u.fsm.con, update, u.fsm.options) } // RemovePath withdraws prefix `pfx` from a peer func (u *UpdateSenderAddPath) RemovePath(pfx net.Prefix, p *route.Path) bool { - err := withDrawPrefixesAddPath(u.fsm.con, u.fsm.encodingOptions, pfx, p) + err := withDrawPrefixesAddPath(u.fsm.con, u.fsm.options, pfx, p) return err == nil } diff --git a/protocols/bgp/server/withdraw.go b/protocols/bgp/server/withdraw.go index 90c8fb548b24fc3283c4da0fd11c1bb1be65139f..089f9f2c0f7a0e25d1c5d1f2e9f292cdeec74a3c 100644 --- a/protocols/bgp/server/withdraw.go +++ b/protocols/bgp/server/withdraw.go @@ -11,7 +11,7 @@ import ( // withDrawPrefixes generates a BGPUpdate message and write it to the given // io.Writer. -func withDrawPrefixes(out io.Writer, opt *packet.EncodingOptions, prefixes ...net.Prefix) error { +func withDrawPrefixes(out io.Writer, opt *packet.Options, prefixes ...net.Prefix) error { if len(prefixes) < 1 { return nil } @@ -41,7 +41,7 @@ func withDrawPrefixes(out io.Writer, opt *packet.EncodingOptions, prefixes ...ne // withDrawPrefixesAddPath generates a BGPUpdateAddPath message and write it to the given // io.Writer. -func withDrawPrefixesAddPath(out io.Writer, opt *packet.EncodingOptions, pfx net.Prefix, p *route.Path) error { +func withDrawPrefixesAddPath(out io.Writer, opt *packet.Options, pfx net.Prefix, p *route.Path) error { if p.Type != route.BGPPathType { return errors.New("wrong path type, expected BGPPathType") }