From ada5cb1db61b481378ad78ba2d78bf36eff67e2e Mon Sep 17 00:00:00 2001 From: IWASE Yusuke Date: Thu, 19 Jan 2017 10:04:01 +0900 Subject: server/fsm: Logging Administrative Shutdown Communication This patch enable to log the body of the Cease NOTIFICATION message with "Administrative Shutdown" and "Administrative Reset" subcodes. Signed-off-by: IWASE Yusuke --- server/fsm.go | 54 +++++++++++++++++++++++++++++++++++++++++------------- server/util.go | 16 ++++++++++++++++ 2 files changed, 57 insertions(+), 13 deletions(-) (limited to 'server') diff --git a/server/fsm.go b/server/fsm.go index c72f7f24..a8ca047a 100644 --- a/server/fsm.go +++ b/server/fsm.go @@ -696,13 +696,26 @@ func (h *FSMHandler) recvMessageWithError() (*FsmMsg, error) { } case bgp.BGP_MSG_NOTIFICATION: body := m.Body.(*bgp.BGPNotification) - log.WithFields(log.Fields{ - "Topic": "Peer", - "Key": h.fsm.pConf.Config.NeighborAddress, - "Code": body.ErrorCode, - "Subcode": body.ErrorSubcode, - "Data": body.Data, - }).Warn("received notification") + if body.ErrorCode == bgp.BGP_ERROR_CEASE && (body.ErrorSubcode == bgp.BGP_ERROR_SUB_ADMINISTRATIVE_SHUTDOWN || body.ErrorSubcode == bgp.BGP_ERROR_SUB_ADMINISTRATIVE_RESET) { + communication, rest := decodeAdministrativeCommunication(body.Data) + log.WithFields(log.Fields{ + "Topic": "Peer", + "Key": h.fsm.pConf.Config.NeighborAddress, + "Code": body.ErrorCode, + "Subcode": body.ErrorSubcode, + "Communicated-Reason": communication, + "Data": rest, + }).Warn("received notification") + } else { + log.WithFields(log.Fields{ + "Topic": "Peer", + "Key": h.fsm.pConf.Config.NeighborAddress, + "Code": body.ErrorCode, + "Subcode": body.ErrorSubcode, + "Data": body.Data, + }).Warn("received notification") + } + if s := h.fsm.pConf.GracefulRestart.State; s.Enabled && s.NotificationEnabled && body.ErrorCode == bgp.BGP_ERROR_CEASE && body.ErrorSubcode == bgp.BGP_ERROR_SUB_HARD_RESET { sendToErrorCh(FSM_HARD_RESET) } else { @@ -1097,13 +1110,28 @@ func (h *FSMHandler) sendMessageloop() error { switch m.Header.Type { case bgp.BGP_MSG_NOTIFICATION: - log.WithFields(log.Fields{ - "Topic": "Peer", - "Key": fsm.pConf.Config.NeighborAddress, - "State": fsm.state.String(), - "Data": m, - }).Warn("sent notification") body := m.Body.(*bgp.BGPNotification) + if body.ErrorCode == bgp.BGP_ERROR_CEASE && (body.ErrorSubcode == bgp.BGP_ERROR_SUB_ADMINISTRATIVE_SHUTDOWN || body.ErrorSubcode == bgp.BGP_ERROR_SUB_ADMINISTRATIVE_RESET) { + communication, rest := decodeAdministrativeCommunication(body.Data) + log.WithFields(log.Fields{ + "Topic": "Peer", + "Key": fsm.pConf.Config.NeighborAddress, + "State": fsm.state.String(), + "Code": body.ErrorCode, + "Subcode": body.ErrorSubcode, + "Communicated-Reason": communication, + "Data": rest, + }).Warn("sent notification") + } else { + log.WithFields(log.Fields{ + "Topic": "Peer", + "Key": fsm.pConf.Config.NeighborAddress, + "State": fsm.state.String(), + "Code": body.ErrorCode, + "Subcode": body.ErrorSubcode, + "Data": body.Data, + }).Warn("sent notification") + } h.errorCh <- FsmStateReason(fmt.Sprintf("%s %s", FSM_NOTIFICATION_SENT, bgp.NewNotificationErrorCode(body.ErrorCode, body.ErrorSubcode).String())) conn.Close() return fmt.Errorf("closed") diff --git a/server/util.go b/server/util.go index 9e11a0a1..951d712c 100644 --- a/server/util.go +++ b/server/util.go @@ -43,3 +43,19 @@ func newAdministrativeCommunication(communication string) (data []byte) { } return data } + +// Parses the given NOTIFICATION message data as a binary value and returns +// the Administrative Shutdown Communication in string and the rest binary. +func decodeAdministrativeCommunication(data []byte) (string, []byte) { + if len(data) == 0 { + return "", data + } + communicationLen := int(data[0]) + if communicationLen > bgp.BGP_ERROR_ADMINISTRATIVE_COMMUNICATION_MAX { + communicationLen = bgp.BGP_ERROR_ADMINISTRATIVE_COMMUNICATION_MAX + } + if communicationLen > len(data)+1 { + communicationLen = len(data) + 1 + } + return string(data[1 : communicationLen+1]), data[communicationLen+1:] +} -- cgit v1.2.3