summaryrefslogtreecommitdiff
path: root/proto/bmp/bmp.h
blob: 258a5089fa2e9f9b82c5c8f86b243a6f493c1e45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
 *	BIRD -- The BGP Monitoring Protocol (BMP)
 *
 *	(c) 2020 Akamai Technologies, Inc. (Pawel Maslanka, pmaslank@akamai.com)
 *
 *	Can be freely distributed and used under the terms of the GNU GPL.
 */

#ifndef _BIRD_BMP_H_
#define _BIRD_BMP_H_

#include "nest/bird.h"
#include "nest/protocol.h"
#include "lib/lists.h"
#include "nest/route.h"
#include "lib/event.h"
#include "lib/hash.h"
#include "lib/socket.h"
#include "proto/bmp/map.h"

#include <stdbool.h>

// Max length of MIB-II description object
#define MIB_II_STR_LEN 255

// The following fields of this structure controls whether there will be put
// specific routes into Route Monitoring message and send to BMP collector
struct monitoring_rib {
  bool in_pre_policy;  // Monitoring pre-policy Adj-Rib-In
  bool in_post_policy; // Monitoring post-policy Adj-Rib-In
  bool local;          // Monitoring Local Rib
};

struct bmp_config {
  struct proto_config c;
  const char *sys_descr;              // sysDescr MIB-II [RFC1213] object
  const char *sys_name;               // sysName MIB-II [RFC1213] object
  ip_addr local_addr;                 // Local IP address
  ip_addr station_ip;                 // Monitoring station address
  u16 station_port;                   // Monitoring station TCP port
  bool monitoring_rib_in_pre_policy;  // Route monitoring pre-policy Adj-Rib-In
};

/* Forward declarations */
struct bgp_proto;
struct bmp_proto;

// Keeps necessary information during composing BGP UPDATE MSG which is going
// to be sent to the BMP collector
struct rt_table_info {
  list update_msg_queue;         // Stores all composed BGP UPDATE MSGs
  size_t update_msg_size;        // Size of all BGP UPDATE MSGs
  struct timeval update_begin_time; // Keeps timestamp of starting BGP UPDATE MSGs composing
  bool update_in_progress;       // Holds information whether composing process is still in progress
};

struct bmp_proto {
  struct proto p;                  // Parent proto
  const struct bmp_config *cf;     // Shortcut to BMP configuration
  node bmp_node;                   // Node in bmp_proto_list
  sock *sk;                        // TCP connection
  event *tx_ev;                    // TX event
  char sys_descr[MIB_II_STR_LEN];  // sysDescr MIB-II [RFC1213] object
  char sys_name[MIB_II_STR_LEN];   // sysName MIB-II [RFC1213] object
  ip_addr local_addr;              // Source local IP address
  ip_addr station_ip;              // Monitoring station IP address
  u16 station_port;                // Monitoring station TCP port
  struct monitoring_rib monitoring_rib;
  // Below fields are for internal use
  // struct bmp_peer_map bgp_peers;   // Stores 'bgp_proto' structure per BGP peer
  pool *buffer_mpool;              // Memory pool used for BMP buffer allocations
  pool *map_mem_pool;              // Memory pool used for BMP map allocations
  pool *tx_mem_pool;               // Memory pool used for packet allocations designated to BMP collector
  pool *update_msg_mem_pool;       // Memory pool used for BPG UPDATE MSG allocations
  list tx_queue;                   // Stores queued packets going to be sent
  timer *connect_retry_timer;      // Timer for retrying connection to the BMP collector
  struct rt_table_info rt_table_in_pre_policy; // Pre-policy route import table
  bool started;                    // Flag that stores running status of BMP instance
  int sock_err;                    // Last socket error code
};


#ifdef CONFIG_BMP

/**
 * bmp_peer_up - send notification that BGP peer connection is established
 */
void
bmp_peer_up(const struct bgp_proto *bgp,
	    const byte *tx_open_msg, uint tx_open_length,
	    const byte *rx_open_msg, uint rx_open_length);

/**
 * The following 5 functions create BMP Route Monitoring message based on
 * pre-policy Adj-RIB-In. Composing Route Monitoring message consist of few
 * stages. First of all call bmp_route_monitor_update_in_pre_begin() in order
 * to start composing message. As a second step, call
 * bmp_route_monitor_update_in_notify() to announce each rte, which internally
 * calls bmp_route_monitor_put_update_in_pre_msg() in order to save BGP UPDATE
 * msg. As a third step call bmp_route_monitor_update_in_pre_commit() in order
 * to send BMP Route Monitoring message to the BMP collector. As a last step,
 * call bmp_route_monitor_update_in_pre_end() in order to release resources.
 */
void
bmp_route_monitor_update_in_pre_begin(void);

void
bmp_route_monitor_update_in_notify(struct channel *C, const net_addr *n,
				   const struct rte *new, const struct rte_src *src);

void
bmp_route_monitor_update_in_pre_commit(const struct bgp_proto *bgp);

void
bmp_route_monitor_update_in_pre_end(void);

void
bmp_route_monitor_put_update_in_pre_msg(struct bmp_proto *p, const byte *data, const size_t data_size);

/**
 * bmp_peer_down - send notification that BGP peer connection is not in
 * established state
 */
void
bmp_peer_down(const struct bgp_proto *bgp, const int err_class, const byte *pkt,
  size_t pkt_size);


#else /* BMP build disabled */

static inline void bmp_peer_up(const struct bgp_proto *bgp UNUSED, const byte *tx_open_msg UNUSED, uint tx_open_length UNUSED, const byte *rx_open_msg UNUSED, uint rx_open_length UNUSED) { }
static inline void bmp_route_monitor_update_in_pre_begin(void) { }
static inline void bmp_route_monitor_update_in_pre_commit(const struct bgp_proto *bgp UNUSED) { }
static inline void bmp_route_monitor_update_in_pre_end(void) { }
static inline void bmp_peer_down(const struct bgp_proto *bgp UNUSED, const int err_class UNUSED, const byte *pkt UNUSED, size_t pkt_size UNUSED) { }

#endif /* CONFIG_BMP */

#endif /* _BIRD_BMP_H_ */