OpenSDN source code
bgp_session.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
6 #include "bgp/bgp_session.h"
7 
8 #include <algorithm>
9 #include <string>
10 
11 #include "bgp/bgp_log.h"
12 #include "bgp/bgp_peer.h"
13 #include "bgp/bgp_server.h"
15 #include "bgp/bgp_update_sender.h"
16 
17 using std::string;
18 
19 using boost::asio::mutable_buffer;
20 
21 // Extract the total BGP message length. This is a 2 byte field after the
22 // 16 byte marker. If the buffer doesn't have 18 bytes available return -1.
23 int BgpMessageReader::MsgLength(Buffer buffer, int offset) {
24  size_t size = TcpSession::BufferSize(buffer);
25  int remain = size - offset;
26  if (remain < BgpMessageReader::kHeaderLenSize) {
27  return -1;
28  }
29  const uint8_t *data = TcpSession::BufferData(buffer) + offset;
30  data += 16;
31  int length = get_value(data, 2);
33  assert(length <= 4096);
34  return length;
35 }
36 
38  ReceiveCallback callback)
39  : TcpMessageReader(session, callback) {
40 }
41 
43 }
44 
46  : TcpSession(session_mgr, socket),
47  session_mgr_(session_mgr),
48  peer_(NULL),
49  task_instance_(-1),
50  reader_(new BgpMessageReader(this,
51  boost::bind(&BgpSession::ReceiveMsg, this, _1, _2))) {
52 }
53 
55 }
56 
57 //
58 // Concurrency: called in the context of io::Reader task.
59 //
60 bool BgpSession::ReceiveMsg(const u_int8_t *msg, size_t size) {
61  return (peer_ ? peer_->ReceiveMsg(this, msg, size) : false);
62 }
63 
64 //
65 // Concurrency: called in the context of bgp::Config task.
66 //
67 // Process write ready callback.
68 //
69 // 1. Tell BgpUpdateSender that the IPeer is send ready.
70 // 2. Tell BgpPeer that it's send ready so that it can resume Keepalives.
71 //
73  if (!peer_)
74  return;
76  BgpUpdateSender *sender = server->update_sender();
77  sender->PeerSendReady(peer_);
79 }
80 
81 //
82 // Concurrency: called in the context of io thread.
83 //
84 // Handle write ready callback. Enqueue the session to a WorkQueue in the
85 // BgpSessionManager. The WorkQueue gets processed in the context of the
86 // bgp::Config task. This ensures that we don't access the BgpPeer while
87 // the BgpPeer is trying to clear our back pointer to it.
88 //
89 // We can ignore any errors since the StateMachine will get informed of the
90 // TcpSession close independently and react to it.
91 //
92 void BgpSession::WriteReady(const boost::system::error_code &error) {
93  if (error)
94  return;
96 }
97 
98 void BgpSession::LogNotification(int code, int subcode, const string &direction,
99  const string &peer_key,
100  const BgpProto::Notification &msg) const {
101  // Use SYS_DEBUG for connection collision, SYS_NOTICE for the rest.
102  if (code == BgpProto::Notification::Cease &&
104  BGP_LOG(BgpPeerNotification, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_ALL,
105  peer_key, direction, code, subcode, msg.ToString());
106  } else {
107  BGP_LOG(BgpPeerNotificationNotice, SandeshLevel::SYS_NOTICE,
108  BGP_LOG_FLAG_ALL, peer_key, direction, code, subcode,
109  msg.ToString());
110  }
111 }
112 
113 void BgpSession::SendNotification(int code, int subcode,
114  const string &data) {
116  msg.error = code;
117  msg.subcode = subcode;
118  msg.data = data;
119  uint8_t buf[BgpProto::kMaxMessageSize];
120  int msglen = BgpProto::Encode(&msg, buf, sizeof(buf));
121 
122  LogNotification(code, subcode, BGP_PEER_DIR_OUT,
123  peer_ ? peer_->ToUVEKey() : ToString(), msg);
124  if (msglen > BgpProto::kMinMessageSize) {
125  Send(buf, msglen, NULL);
126  }
127 }
128 
130  peer_ = peer;
132 }
133 
134 //
135 // Dissociate the peer from the this BgpSession.
136 // Do not invalidate the task_instance since it can be used to spawn an
137 // io::ReaderTask while this method is being executed.
138 //
140  peer_ = NULL;
141 }
#define BGP_LOG_FLAG_ALL
Definition: bgp_log.h:45
#define BGP_LOG(obj, level, flags,...)
Definition: bgp_log.h:60
#define BGP_PEER_DIR_OUT
Definition: bgp_log.h:139
virtual ~BgpMessageReader()
Definition: bgp_session.cc:42
BgpMessageReader(TcpSession *session, ReceiveCallback callback)
Definition: bgp_session.cc:37
virtual int MsgLength(Buffer buffer, int offset)
Definition: bgp_session.cc:23
static const int kHeaderLenSize
Definition: bgp_session.h:36
virtual const std::string & ToUVEKey() const
Definition: bgp_peer.h:103
virtual BgpServer * server()
Definition: bgp_peer.h:159
virtual bool ReceiveMsg(BgpSession *session, const u_int8_t *msg, size_t size)
Definition: bgp_peer.cc:2355
void SetSendReady()
Definition: bgp_peer.cc:2310
int GetTaskInstance() const
Definition: bgp_peer.cc:2477
static const int kMinMessageSize
Definition: bgp_proto.h:441
static const int kMaxMessageSize
Definition: bgp_proto.h:442
static int Encode(const BgpMessage *msg, uint8_t *data, size_t size, EncodeOffsets *offsets=NULL, bool as4=false)
Definition: bgp_proto.cc:2136
void EnqueueWriteReady(BgpSession *session)
virtual void WriteReady(const boost::system::error_code &error)
Definition: bgp_session.cc:92
BgpPeer * peer_
Definition: bgp_session.h:69
int task_instance_
Definition: bgp_session.h:70
void SendNotification(int code, int subcode, const std::string &data=std::string())
Definition: bgp_session.cc:113
virtual ~BgpSession()
Definition: bgp_session.cc:54
void ProcessWriteReady()
Definition: bgp_session.cc:72
bool ReceiveMsg(const u_int8_t *msg, size_t size)
Definition: bgp_session.cc:60
BgpSession(BgpSessionManager *session_mgr, Socket *socket)
Definition: bgp_session.cc:45
BgpPeer * peer()
Definition: bgp_session.h:54
void LogNotification(int code, int subcode, const std::string &direction, const std::string &peer_key, const BgpProto::Notification &msg) const
Definition: bgp_session.cc:98
void clear_peer()
Definition: bgp_session.cc:139
BgpSessionManager * session_mgr_
Definition: bgp_session.h:68
void set_peer(BgpPeer *peer)
Definition: bgp_session.cc:129
void PeerSendReady(IPeerUpdate *peer)
boost::function< bool(const uint8_t *, size_t)> ReceiveCallback
Definition: tcp_session.h:308
boost::asio::const_buffer Buffer
Definition: tcp_session.h:307
TcpServer * server()
Definition: tcp_session.h:85
static const uint8_t * BufferData(const Buffer &buffer)
Definition: tcp_session.h:110
virtual std::string ToString() const
Definition: tcp_session.h:80
static size_t BufferSize(const Buffer &buffer)
Definition: tcp_session.h:113
boost::asio::ip::tcp::socket Socket
Definition: tcp_session.h:57
virtual bool Send(const uint8_t *data, size_t size, size_t *sent)
Definition: tcp_session.cc:430
bool unit_test()
Definition: bgp_log.cc:54
static uint64_t get_value(const uint8_t *data, int size)
Definition: parse_object.h:40
virtual const std::string ToString() const
Definition: bgp_proto.cc:344