#include "filter/filter.h"
#include "proto/bgp/bgp.h"
#include "sysdep/unix/unix.h"
+#include "sysdep/unix/io-loop.h"
#include "lib/event.h"
#include "lib/ip.h"
#include "lib/lists.h"
#include "lib/resource.h"
#include "lib/unaligned.h"
+#include "lib/tlists.h"
#include "nest/iface.h"
#include "nest/route.h"
-// List of BMP instances
-static list STATIC_LIST_INIT(bmp_proto_list);
-
#define HASH_PEER_KEY(n) n->bgp
#define HASH_PEER_NEXT(n) n->next
#define HASH_PEER_EQ(b1,b2) b1 == b2
IF_COND_TRUE_PRINT_ERR_MSG_AND_RETURN_OPT_VAL(!(p), msg, rv); \
} while (0)
+static const struct ea_class *bgp_next_hop_ea_class = NULL;
static void bmp_connected(struct birdsock *sk);
static void bmp_sock_err(sock *sk, int err);
static void bmp_close_socket(struct bmp_proto *p);
+static void bmp_check_routes(void *bt_);
+static void bmp_feed_end(struct rt_export_request *req);
+static void bmp_process_proto_state_change(struct bmp_proto *p, struct lfjour_item *last_up);
+static void bmp_proto_state_changed(void *_p);
static void
-bmp_send_peer_up_notif_msg(struct bmp_proto *p, const struct bgp_proto *bgp,
- const byte *tx_data, const size_t tx_data_size,
- const byte *rx_data, const size_t rx_data_size);
+bmp_send_peer_up_notif_msg(struct bmp_proto *p, ea_list *bgp,
+ const adata *tx_data, const adata *rx_data, struct bgp_conn_sk_ad *sk);
static void bmp_route_monitor_end_of_rib(struct bmp_proto *p, struct bmp_stream *bs);
{
ASSERT(p->started);
- struct bmp_data_node *tx_data = mb_alloc(p->tx_mem_pool, sizeof (struct bmp_data_node));
- tx_data->data = mb_alloc(p->tx_mem_pool, size);
+ struct bmp_data_node *tx_data = mb_allocz(p->tx_mem_pool, sizeof (struct bmp_data_node));
+ tx_data->data = mb_allocz(p->tx_mem_pool, size);
memcpy(tx_data->data, payload, size);
tx_data->data_size = size;
add_tail(&p->tx_queue, &tx_data->n);
if (sk_tx_buffer_empty(p->sk)
&& !ev_active(p->tx_ev))
{
- ev_schedule(p->tx_ev);
+ ev_send_loop(p->p.loop, p->tx_ev);
}
}
{
if (!ev_active(p->tx_ev))
{
- ev_schedule(p->tx_ev);
+ ev_send_loop(p->p.loop, p->tx_ev);
}
return;
bmp_peer_up_notif_msg_serialize(buffer *stream, const bool is_peer_global,
const u32 peer_as, const u32 peer_bgp_id, const bool as4_support,
const ip_addr local_addr, const ip_addr remote_addr, const u16 local_port,
- const u16 remote_port, const byte *sent_msg, const size_t sent_msg_length,
- const byte *recv_msg, const size_t recv_msg_length)
+ const u16 remote_port, const adata *sent_msg, const adata *recv_msg)
{
const size_t data_size =
BMP_PER_PEER_HDR_SIZE + BMP_PEER_UP_NOTIF_MSG_FIX_SIZE +
- BGP_HEADER_LENGTH + sent_msg_length + BGP_HEADER_LENGTH + recv_msg_length;
+ BGP_HEADER_LENGTH + sent_msg->length + BGP_HEADER_LENGTH + recv_msg->length;
bmp_buffer_need(stream, BMP_COMMON_HDR_SIZE + data_size);
bmp_common_hdr_serialize(stream, BMP_PEER_UP_NOTIF, data_size);
bmp_put_ipa(stream, local_addr);
bmp_put_u16(stream, local_port);
bmp_put_u16(stream, remote_port);
- bmp_put_bgp_hdr(stream, PKT_OPEN, BGP_HEADER_LENGTH + sent_msg_length);
- bmp_put_data(stream, sent_msg, sent_msg_length);
- bmp_put_bgp_hdr(stream, PKT_OPEN, BGP_HEADER_LENGTH + recv_msg_length);
- bmp_put_data(stream, recv_msg, recv_msg_length);
+ bmp_put_bgp_hdr(stream, PKT_OPEN, BGP_HEADER_LENGTH + sent_msg->length);
+ bmp_put_data(stream, sent_msg->data, sent_msg->length);
+ bmp_put_bgp_hdr(stream, PKT_OPEN, BGP_HEADER_LENGTH + recv_msg->length);
+ bmp_put_data(stream, recv_msg->data, recv_msg->length);
}
static void
*/
static struct bmp_table *
-bmp_find_table(struct bmp_proto *p, struct rtable *tab)
+bmp_find_table(struct bmp_proto *p, rtable *tab)
{
return HASH_FIND(p->table_map, HASH_TABLE, tab);
}
+const struct channel_class channel_bmp = {
+ .channel_size = sizeof(struct channel),
+ .config_size = sizeof(struct channel_config),
+ /*.init =
+ .start =
+ .shutdown =
+ .cleanup =
+ .reconfigure = */
+};
+
static struct bmp_table *
-bmp_add_table(struct bmp_proto *p, struct rtable *tab)
+bmp_add_table(struct bmp_proto *p, rtable *tab)
{
struct bmp_table *bt = mb_allocz(p->p.pool, sizeof(struct bmp_table));
bt->table = tab;
+ bt->p = p;
rt_lock_table(bt->table);
HASH_INSERT(p->table_map, HASH_TABLE, bt);
- struct channel_config cc = {
- .name = "monitor",
- .channel = &channel_basic,
- .table = tab->config,
- .in_filter = FILTER_REJECT,
- .net_type = tab->addr_type,
- .ra_mode = RA_ANY,
- .bmp_hack = 1,
+ bt->event.data = bt;
+
+ bt->event.hook = bmp_check_routes;
+ bt->out_req = (struct rt_export_request) {
+ .name = mb_sprintf(p->p.pool, "%s.export", p->p.name),
+ .r = (struct lfjour_recipient) {
+ .target = proto_event_list(&p->p),
+ .event = &bt->event,
+ },
+ .pool = p->p.pool,
+ .trace_routes = p->p.debug,
+ //.dump = channel_dump_export_req, TODO: this will crash on `dump tables` from CLI
+ .fed = bmp_feed_end,
};
- bt->channel = proto_add_channel(&p->p, &cc);
- channel_set_state(bt->channel, CS_UP);
-
+ rt_export_subscribe(tab, all, &bt->out_req);
return bt;
}
+
static void
bmp_remove_table(struct bmp_proto *p, struct bmp_table *bt)
{
- channel_set_state(bt->channel, CS_FLUSHING);
- channel_set_state(bt->channel, CS_DOWN);
- proto_remove_channel(&p->p, bt->channel);
+ if (bt->channel)
+ {
+ channel_set_state(bt->channel, CS_STOP);
+ channel_set_state(bt->channel, CS_DOWN);
+ }
+ rt_export_unsubscribe(all, &bt->out_req);
HASH_REMOVE(p->table_map, HASH_TABLE, bt);
mb_free(bt);
}
-static inline struct bmp_table *bmp_get_table(struct bmp_proto *p, struct rtable *tab)
-{ return bmp_find_table(p, tab) ?: bmp_add_table(p, tab); }
-
static inline void bmp_lock_table(struct bmp_proto *p UNUSED, struct bmp_table *bt)
{ bt->uc++; }
+struct bmp_table *
+bmp_get_table(struct bmp_proto *p, rtable *tab)
+{
+ struct bmp_table *bt = bmp_find_table(p, tab);
+ if (bt)
+ {
+ while (true) {
+ atomic_int i = bt->uc;
+ if (i == 0)
+ {
+ struct bmp_table *new = bmp_add_table(p, tab);
+ bmp_lock_table(p, new);
+ return new;
+ }
+ if (atomic_compare_exchange_strong_explicit(&bt->uc, &i, i+1, memory_order_acq_rel, memory_order_relaxed))
+ return bt;
+ }
+ }
+ struct bmp_table *new = bmp_add_table(p, tab);
+ bmp_lock_table(p, new);
+ return new;
+}
+
static inline void bmp_unlock_table(struct bmp_proto *p, struct bmp_table *bt)
-{ bt->uc--; if (!bt->uc) bmp_remove_table(p, bt); }
+{ atomic_int i = 1;
+ if (atomic_compare_exchange_strong_explicit(&bt->uc, &i, 0, memory_order_acq_rel, memory_order_relaxed))
+ bmp_remove_table(p, bt);
+ else
+ bt->uc--;
+}
/*
static inline u32 bmp_stream_key(u32 afi, bool policy)
{ return afi ^ (policy ? BMP_STREAM_KEY_POLICY : 0); }
-static inline u32 bmp_stream_afi(struct bmp_stream *bs)
-{ return bs->key & ~BMP_STREAM_KEY_POLICY; }
-
static inline bool bmp_stream_policy(struct bmp_stream *bs)
{ return !!(bs->key & BMP_STREAM_KEY_POLICY); }
static struct bmp_stream *
bmp_find_stream(struct bmp_proto *p, const struct bgp_proto *bgp, u32 afi, bool policy)
{
- return HASH_FIND(p->stream_map, HASH_STREAM, bgp, bmp_stream_key(afi, policy));
+ ea_list *bgp_attr = proto_get_state(bgp->p.id);
+ struct bmp_stream *s = HASH_FIND(p->stream_map, HASH_STREAM, bgp_attr, bmp_stream_key(afi, policy));
+
+ while (s == NULL)
+ {
+ struct lfjour_item *li = lfjour_get(&p->proto_state_reader);
+ if (!li)
+ return NULL;
+
+ bmp_process_proto_state_change(p, li);
+ s = HASH_FIND(p->stream_map, HASH_STREAM, bgp_attr, bmp_stream_key(afi, policy));
+ }
+ return s;
}
static struct bmp_stream *
-bmp_add_stream(struct bmp_proto *p, struct bmp_peer *bp, u32 afi, bool policy, struct rtable *tab, struct bgp_channel *sender)
+bmp_add_stream(struct bmp_proto *p, struct bmp_peer *bp, u32 afi, bool policy, rtable *tab, ea_list *sender, int in_pre_policy)
{
struct bmp_stream *bs = mb_allocz(p->p.pool, sizeof(struct bmp_stream));
bs->bgp = bp->bgp;
HASH_INSERT(p->stream_map, HASH_STREAM, bs);
bs->table = bmp_get_table(p, tab);
- bmp_lock_table(p, bs->table);
bs->sender = sender;
bs->sync = false;
+ bs->in_pre_policy = in_pre_policy;
return bs;
}
*/
static struct bmp_peer *
-bmp_find_peer(struct bmp_proto *p, const struct bgp_proto *bgp)
+bmp_find_peer(struct bmp_proto *p, ea_list *bgp_attr)
{
- return HASH_FIND(p->peer_map, HASH_PEER, bgp);
+ return HASH_FIND(p->peer_map, HASH_PEER, bgp_attr);
}
static struct bmp_peer *
-bmp_add_peer(struct bmp_proto *p, struct bgp_proto *bgp)
+bmp_add_peer(struct bmp_proto *p, ea_list *bgp_attr)
{
- struct bmp_peer *bp = mb_allocz(p->p.pool, sizeof(struct bmp_peer));
- bp->bgp = bgp;
+ struct bmp_peer *bp;
+ if (DG_IS_LOCKED(p->p.pool->domain))
+ bp = mb_allocz(p->p.pool, sizeof(struct bmp_peer));
+ else
+ {
+ DG_LOCK(p->p.pool->domain);
+ bp = mb_allocz(p->p.pool, sizeof(struct bmp_peer));
+ DG_UNLOCK(p->p.pool->domain);
+ }
+ bp->bgp = bgp_attr;
init_list(&bp->streams);
HASH_INSERT(p->peer_map, HASH_PEER, bp);
- struct bgp_channel *c;
- BGP_WALK_CHANNELS(bgp, c)
+ const adata *channels_adata = ea_get_adata(bgp_attr, &ea_proto_channel_list);
+ int id_count = channels_adata->length / sizeof(u32);
+ u32 *chann_ids = (u32 *) channels_adata->data;
+
+ for (int i = 0; i < id_count; i++)
{
- if (p->monitoring_rib.in_pre_policy && c->c.in_table)
- bmp_add_stream(p, bp, c->afi, false, c->c.in_table, c);
+ ea_list *chan_attr;
+ PST_LOCKED(ts)
+ chan_attr = ts->channels[chann_ids[i]];
+
+ if (chan_attr == NULL)
+ continue;
+
+ rtable *ch_table = (rtable *) ea_get_ptr(chan_attr, &ea_rtable, 0);
+ int in_keep = ea_get_int(chan_attr, &ea_in_keep, 0);
+
+ if (p->monitoring_rib.in_pre_policy && ch_table)
+ {
+ if (in_keep == RIK_PREFILTER)
+ bmp_add_stream(p, bp, ea_get_int(chan_attr, &ea_bgp_afi, 0), false, ch_table, chan_attr, 1);
+ else
+ log(L_WARN "%s: Failed to request pre-policy for %s.%s, import table disabled",
+ p->p.name,
+ ea_get_adata(bgp_attr, &ea_name)->data,
+ ea_get_adata(chan_attr, &ea_name)->data);
+ }
- if (p->monitoring_rib.in_post_policy && c->c.table)
- bmp_add_stream(p, bp, c->afi, true, c->c.table, c);
+ if (p->monitoring_rib.in_post_policy && ch_table)
+ bmp_add_stream(p, bp, ea_get_int(chan_attr, &ea_bgp_afi, 0), true, ch_table, chan_attr, 0);
}
return bp;
}
static void
-bmp_peer_up_(struct bmp_proto *p, struct bgp_proto *bgp, bool sync,
- const byte *tx_open_msg, uint tx_open_length,
- const byte *rx_open_msg, uint rx_open_length)
+bmp_peer_up_(struct bmp_proto *p, ea_list *bgp_attr, bool sync,
+ const adata *tx_open_msg, const adata *rx_open_msg,
+ struct bgp_conn_sk_ad *sk)
{
if (!p->started)
return;
- struct bmp_peer *bp = bmp_find_peer(p, bgp);
+ struct bmp_peer *bp = bmp_find_peer(p, bgp_attr);
if (bp)
return;
- TRACE(D_STATES, "Peer up for %s", bgp->p.name);
+ const char *name = ea_get_adata(bgp_attr, &ea_name)->data;
+ TRACE(D_STATES, "Peer up for %s", name);
- bp = bmp_add_peer(p, bgp);
+ bp = bmp_add_peer(p, bgp_attr);
- bmp_send_peer_up_notif_msg(p, bgp, tx_open_msg, tx_open_length, rx_open_msg, rx_open_length);
+ bmp_send_peer_up_notif_msg(p, bgp_attr, tx_open_msg, rx_open_msg, sk);
/*
* We asssume peer_up() notifications are received before any route
}
}
-void
-bmp_peer_up(struct bgp_proto *bgp,
- const byte *tx_open_msg, uint tx_open_length,
- const byte *rx_open_msg, uint rx_open_length)
-{
- struct bmp_proto *p; node *n;
- WALK_LIST2(p, n, bmp_proto_list, bmp_node)
- bmp_peer_up_(p, bgp, true, tx_open_msg, tx_open_length, rx_open_msg, rx_open_length);
-}
-
-static void
-bmp_peer_init(struct bmp_proto *p, struct bgp_proto *bgp)
+static bool
+bmp_peer_up_inout(struct bmp_proto *p, ea_list *bgp_attr, bool sync)
{
- struct bgp_conn *conn = bgp->conn;
-
- if (!conn || (conn->state != BS_ESTABLISHED) ||
- !conn->local_open_msg || !conn->remote_open_msg)
- return;
-
- bmp_peer_up_(p, bgp, false, conn->local_open_msg, conn->local_open_length,
- conn->remote_open_msg, conn->remote_open_length);
-}
+ int in_state = ea_get_int(bgp_attr, &ea_bgp_in_conn_state, 0);
+ int out_state = ea_get_int(bgp_attr, &ea_bgp_out_conn_state, 0);
+ if (in_state == BS_ESTABLISHED)
+ {
+ ASSERT_DIE(out_state != BS_ESTABLISHED);
+ const adata *loc_open = ea_get_adata(bgp_attr, &ea_bgp_in_conn_local_open_msg);
+ const adata *rem_open = ea_get_adata(bgp_attr, &ea_bgp_in_conn_remote_open_msg);
+ SKIP_BACK_DECLARE(struct bgp_conn_sk_ad, sk, ad, ea_get_adata(bgp_attr, &ea_bgp_in_conn_sk));
-static const struct birdsock *
-bmp_get_birdsock(const struct bgp_proto *bgp)
-{
- if (bgp->conn && bgp->conn->sk)
- return bgp->conn->sk;
-
- return NULL;
-}
-
-static const struct birdsock *
-bmp_get_birdsock_ext(const struct bgp_proto *bgp)
-{
- const struct birdsock *sk = bmp_get_birdsock(bgp);
- if (sk != NULL)
- return sk;
+ ASSERT_DIE(loc_open && rem_open);
+ bmp_peer_up_(p, bgp_attr, sync, loc_open, rem_open, sk);
- if (bgp->incoming_conn.sk)
- {
- sk = bgp->incoming_conn.sk;
+ return true;
}
- else if (bgp->outgoing_conn.sk)
- {
- sk = bgp->outgoing_conn.sk;
- }
-
- return sk;
-}
-
-static const struct bgp_caps *
-bmp_get_bgp_remote_caps(const struct bgp_proto *bgp)
-{
- if (bgp->conn && bgp->conn->remote_caps)
- return bgp->conn->remote_caps;
- return NULL;
-}
+ if (out_state == BS_ESTABLISHED)
+ {
+ const adata *loc_open = ea_get_adata(bgp_attr, &ea_bgp_out_conn_local_open_msg);
+ const adata *rem_open = ea_get_adata(bgp_attr, &ea_bgp_out_conn_remote_open_msg);
+ SKIP_BACK_DECLARE(struct bgp_conn_sk_ad, sk, ad, ea_get_adata(bgp_attr, &ea_bgp_out_conn_sk));
-static const struct bgp_caps *
-bmp_get_bgp_remote_caps_ext(const struct bgp_proto *bgp)
-{
- const struct bgp_caps *remote_caps = bmp_get_bgp_remote_caps(bgp);
- if (remote_caps != NULL)
- return remote_caps;
+ ASSERT_DIE(loc_open && rem_open);
+ bmp_peer_up_(p, bgp_attr, sync, loc_open, rem_open, sk);
- if (bgp->incoming_conn.remote_caps)
- {
- remote_caps = bgp->incoming_conn.remote_caps;
- }
- else if (bgp->outgoing_conn.remote_caps)
- {
- remote_caps = bgp->outgoing_conn.remote_caps;
+ return true;
}
- return remote_caps;
+ return false;
}
static bool
-bmp_is_peer_global_instance(const struct bgp_proto *bgp)
+bmp_is_peer_global_instance(ea_list *bgp)
{
- return (bgp->cf->peer_type != BGP_PT_EXTERNAL &&
- bgp->cf->peer_type != BGP_PT_INTERNAL)
- ? (bgp->local_as != bgp->remote_as)
- : (bgp->cf->peer_type == BGP_PT_EXTERNAL);
+ int peer_type = ea_get_int(bgp, &ea_bgp_peer_type, 0);
+ int local_as = ea_get_int(bgp, &ea_bgp_loc_as, 0);
+ int remote_as = ea_get_int(bgp, &ea_bgp_rem_as, 0);
+
+ return (peer_type != BGP_PT_EXTERNAL &&
+ peer_type != BGP_PT_INTERNAL)
+ ? (local_as != remote_as)
+ : (peer_type == BGP_PT_EXTERNAL);
}
static void
-bmp_send_peer_up_notif_msg(struct bmp_proto *p, const struct bgp_proto *bgp,
- const byte *tx_data, const size_t tx_data_size,
- const byte *rx_data, const size_t rx_data_size)
+bmp_send_peer_up_notif_msg(struct bmp_proto *p, ea_list *bgp,
+ const adata *tx_data, const adata *rx_data, struct bgp_conn_sk_ad *sk)
{
ASSERT(p->started);
- const struct birdsock *sk = bmp_get_birdsock_ext(bgp);
- IF_PTR_IS_NULL_PRINT_ERR_MSG_AND_RETURN_OPT_VAL(
- sk,
- "[BMP] No BGP socket"
- );
-
+ const int rem_as = ea_get_int(bgp, &ea_bgp_rem_as, 0);
+ const int rem_id = ea_get_int(bgp, &ea_bgp_rem_id, 0);
const bool is_global_instance_peer = bmp_is_peer_global_instance(bgp);
buffer payload = bmp_buffer_alloc(p->buffer_mpool, DEFAULT_MEM_BLOCK_SIZE);
+
bmp_peer_up_notif_msg_serialize(&payload, is_global_instance_peer,
- bgp->remote_as, bgp->remote_id, 1,
- sk->saddr, sk->daddr, sk->sport, sk->dport, tx_data, tx_data_size,
- rx_data, rx_data_size);
+ rem_as, rem_id, 1,
+ sk->saddr, sk->daddr, sk->sport, sk->dport, tx_data, rx_data);
bmp_schedule_tx_packet(p, bmp_buffer_data(&payload), bmp_buffer_pos(&payload));
bmp_buffer_free(&payload);
}
static void
bmp_route_monitor_put_update(struct bmp_proto *p, struct bmp_stream *bs, const byte *data, size_t length, btime timestamp)
{
- struct bmp_data_node *upd_msg = mb_alloc(p->update_msg_mem_pool,
+ struct bmp_data_node *upd_msg = mb_allocz(p->update_msg_mem_pool,
sizeof (struct bmp_data_node));
upd_msg->data = mb_alloc(p->update_msg_mem_pool, length);
memcpy(upd_msg->data, data, length);
upd_msg->data_size = length;
+
add_tail(&p->update_msg_queue, &upd_msg->n);
/* Save some metadata */
- struct bgp_proto *bgp = bs->bgp;
- upd_msg->remote_as = bgp->remote_as;
- upd_msg->remote_id = bgp->remote_id;
- upd_msg->remote_ip = bgp->remote_ip;
+ ea_list *bgp = bs->bgp;
+ upd_msg->remote_as = ea_get_int(bgp, &ea_bgp_rem_as, 0);
+ upd_msg->remote_id = ea_get_int(bgp, &ea_bgp_rem_id, 0);
+ upd_msg->remote_ip = ea_get_ip(bgp, &ea_bgp_rem_ip, IPA_NONE);
upd_msg->timestamp = timestamp;
upd_msg->global_peer = bmp_is_peer_global_instance(bgp);
upd_msg->policy = bmp_stream_policy(bs);
/* Kick the commit */
if (!ev_active(p->update_ev))
- ev_schedule(p->update_ev);
+ ev_send_loop(p->p.loop, p->update_ev);
}
static void
-bmp_route_monitor_notify(struct bmp_proto *p, struct bmp_stream *bs,
- const net_addr *n, const struct rte *new, const struct rte_src *src)
+bmp_route_monitor_notify(struct bmp_proto *p, struct bgp_proto *bgp_p, u32 afi, bool policy, const rte *new, ea_list *old)
{
+ /* Idempotent update */
+ if ((old == new->attrs) || old && new->attrs && ea_same(old, new->attrs))
+ return;
+
+ /* No stream, probably flushed already */
+ struct bmp_stream *bs = bmp_find_stream(p, bgp_p, afi, policy);
+ if (!bs)
+ return;
+
byte buf[BGP_MAX_EXT_MSG_LENGTH];
- byte *end = bgp_bmp_encode_rte(bs->sender, buf, n, new, src);
+ byte *end = bgp_bmp_encode_rte(bs->sender, bgp_p, buf, new);
- btime delta_t = new ? current_time() - new->lastmod : 0;
+ btime delta_t = new->attrs ? current_time() - new->lastmod : 0;
btime timestamp = current_real_time() - delta_t;
if (end)
bmp_route_monitor_put_update(p, bs, buf, end - buf, timestamp);
else
- log(L_WARN "%s: Cannot encode update for %N", p->p.name, n);
+ log(L_WARN "%s: Cannot encode update for %N", p->p.name, new->net);
}
static void
static void
bmp_route_monitor_end_of_rib(struct bmp_proto *p, struct bmp_stream *bs)
{
- TRACE(D_PACKETS, "Sending END-OF-RIB for %s.%s", bs->bgp->p.name, bs->sender->c.name);
+ TRACE(D_PACKETS, "Sending END-OF-RIB for %s.%s", ea_get_adata(bs->bgp, &ea_name)->data, ea_get_adata(bs->sender, &ea_name)->data);
byte rx_end_payload[DEFAULT_MEM_BLOCK_SIZE];
- byte *pos = bgp_create_end_mark_(bs->sender, rx_end_payload + BGP_HEADER_LENGTH);
+ byte *pos = bgp_create_end_mark_ea_(bs->sender, rx_end_payload + BGP_HEADER_LENGTH);
memset(rx_end_payload + BGP_MSG_HDR_MARKER_POS, 0xff,
BGP_MSG_HDR_MARKER_SIZE); // BGP UPDATE MSG marker
put_u16(rx_end_payload + BGP_MSG_HDR_LENGTH_POS, pos - rx_end_payload);
}
static void
-bmp_send_peer_down_notif_msg(struct bmp_proto *p, const struct bgp_proto *bgp,
+bmp_send_peer_down_notif_msg(struct bmp_proto *p, ea_list *bgp,
const byte *data, const size_t data_size)
{
ASSERT(p->started);
- const struct bgp_caps *remote_caps = bmp_get_bgp_remote_caps_ext(bgp);
+ //const struct bgp_caps *remote_caps = bmp_get_bgp_remote_caps_ext(bgp);
+ int remote_caps = ea_get_int(bgp, &ea_bgp_as4_session, 0);
+ int in_state = ea_get_int(bgp, &ea_bgp_in_conn_state, 0);
+ int out_state = ea_get_int(bgp, &ea_bgp_out_conn_state, 0);
+ int in_as4 = ea_get_int(bgp, &ea_bgp_as4_in_conn, 0);
+ int out_as4 = ea_get_int(bgp, &ea_bgp_as4_out_conn, 0);
+
+ if (in_state && in_as4)
+ remote_caps = in_as4;
+ else if (out_state && out_as4)
+ remote_caps = out_as4;
+
bool is_global_instance_peer = bmp_is_peer_global_instance(bgp);
- buffer payload
- = bmp_buffer_alloc(p->buffer_mpool, DEFAULT_MEM_BLOCK_SIZE);
- bmp_peer_down_notif_msg_serialize(&payload, is_global_instance_peer,
- bgp->remote_as, bgp->remote_id,
- remote_caps ? remote_caps->as4_support : bgp->as4_session,
- bgp->remote_ip, data, data_size);
+ buffer payload = bmp_buffer_alloc(p->buffer_mpool, DEFAULT_MEM_BLOCK_SIZE);
+ bmp_peer_down_notif_msg_serialize(
+ &payload,
+ is_global_instance_peer,
+ ea_get_int(bgp, &ea_bgp_rem_as, 0),
+ ea_get_int(bgp, &ea_bgp_rem_id, 0),
+ remote_caps,
+ *((ip_addr *) ea_get_adata(bgp, &ea_bgp_rem_ip)->data),
+ data,
+ data_size
+ );
bmp_schedule_tx_packet(p, bmp_buffer_data(&payload), bmp_buffer_pos(&payload));
bmp_buffer_free(&payload);
}
static void
-bmp_peer_down_(struct bmp_proto *p, const struct bgp_proto *bgp,
- int err_class, int err_code, int err_subcode, const byte *data, int length)
+bmp_peer_down_(struct bmp_proto *p, ea_list *bgp, struct bgp_session_close_ad *bscad)
{
if (!p->started)
return;
if (!bp)
return;
- TRACE(D_STATES, "Peer down for %s", bgp->p.name);
+ TRACE(D_STATES, "Peer down for %s", ea_find(bgp, &ea_name)->u.ad->data);
uint bmp_code = 0;
uint fsm_code = 0;
- switch (err_class)
+ switch (bscad->last_error_class)
{
case BE_BGP_RX:
bmp_code = BMP_PEER_DOWN_REASON_REMOTE_BGP_NOTIFICATION;
default:
bmp_code = BMP_PEER_DOWN_REASON_REMOTE_NO_NOTIFICATION;
- length = 0;
break;
}
+ uint length = bscad->ad.length - sizeof *bscad + sizeof bscad->ad;
buffer payload = bmp_buffer_alloc(p->buffer_mpool, 1 + BGP_HEADER_LENGTH + 2 + length);
bmp_put_u8(&payload, bmp_code);
case BMP_PEER_DOWN_REASON_LOCAL_BGP_NOTIFICATION:
case BMP_PEER_DOWN_REASON_REMOTE_BGP_NOTIFICATION:
bmp_put_bgp_hdr(&payload, BGP_HEADER_LENGTH + 2 + length, PKT_NOTIFICATION);
- bmp_put_u8(&payload, err_code);
- bmp_put_u8(&payload, err_subcode);
- bmp_put_data(&payload, data, length);
+ bmp_put_u8(&payload, bscad->notify_code);
+ bmp_put_u8(&payload, bscad->notify_subcode);
+ bmp_put_data(&payload, bscad->data, length);
break;
case BMP_PEER_DOWN_REASON_LOCAL_NO_NOTIFICATION:
bmp_remove_peer(p, bp);
}
-void
-bmp_peer_down(const struct bgp_proto *bgp,
- int err_class, int code, int subcode, const byte *data, int length)
-{
- struct bmp_proto *p; node *n;
- WALK_LIST2(p, n, bmp_proto_list, bmp_node)
- bmp_peer_down_(p, bgp, err_class, code, subcode, data, length);
-}
static void
bmp_send_termination_msg(struct bmp_proto *p,
bmp_buffer_free(&stream);
}
-int
-bmp_preexport(struct channel *C UNUSED, rte *e)
+static void
+bmp_split_policy(struct bmp_proto *p, const rte *new, const rte *old)
{
- /* Reject non-direct routes */
- if (e->src->proto != e->sender->proto)
- return -1;
+ rte loc = *(new ?: old);
- /* Reject non-BGP routes */
- if (e->sender->channel != &channel_bgp)
- return -1;
+ struct proto *rte_proto = (struct proto*) SKIP_BACK(struct proto, sources, loc.src->owner);
+ struct bgp_proto *bgp = (struct bgp_proto *) rte_proto;
+ struct bgp_channel *src_ch = SKIP_BACK(struct bgp_channel, c.in_req, loc.sender->req);
- return 1;
-}
+ /* Ignore piped routes */
+ if (src_ch->c.proto != rte_proto)
+ return;
-static void
-bmp_rt_notify(struct proto *P, struct channel *c, struct network *net,
- struct rte *new, struct rte *old)
-{
- struct bmp_proto *p = (void *) P;
+ /* Ignore non-BGP routes */
+ if (rte_proto->proto != &proto_bgp)
+ return;
- struct bgp_channel *src = (void *) (new ?: old)->sender;
- struct bgp_proto *bgp = (void *) src->c.proto;
- bool policy = (c->table == src->c.table);
+ /* Checking the pre policy */
+ if (p->monitoring_rib.in_pre_policy)
+ {
+ /* Compute the pre policy attributes */
+ loc.attrs = new ? ea_strip_to(new->attrs, BIT32_ALL(EALS_PREIMPORT)) : NULL;
+ ea_list *old_attrs = old ? ea_strip_to(old->attrs, BIT32_ALL(EALS_PREIMPORT)) : NULL;
- /*
- * We assume that we receive peer_up before the first route and peer_down
- * synchronously with BGP session close. So if bmp_stream exists, the related
- * BGP session is up and could be accessed. That may not be true in
- * multithreaded setup.
- */
+ bmp_route_monitor_notify(p, bgp, src_ch->afi, false, &loc, old_attrs);
+ }
- struct bmp_stream *bs = bmp_find_stream(p, bgp, src->afi, policy);
- if (!bs)
- return;
+ /* Checking the post policy */
+ if (p->monitoring_rib.in_post_policy)
+ {
+ /* Compute the post policy attributes */
+ loc.attrs = new ? ea_normalize(new->attrs, 0) : NULL;
+ ea_list *old_attrs = old ? ea_normalize(old->attrs, 0) : NULL;
- bmp_route_monitor_notify(p, bs, net->n.addr, new, (new ?: old)->src);
+ bmp_route_monitor_notify(p, bgp, src_ch->afi, true, &loc, old_attrs);
+ }
}
static void
-bmp_feed_end(struct channel *c)
+bmp_check_routes(void *bt_)
{
- struct bmp_proto *p = (void *) c->proto;
+ struct bmp_table *bt = (struct bmp_table *)bt_;
+ struct bmp_proto *p = bt->p;
- struct bmp_table *bt = bmp_find_table(p, c->table);
- if (!bt)
- return;
+ RT_EXPORT_WALK(&bt->out_req, u)
+ {
+ switch (u->kind)
+ {
+ case RT_EXPORT_STOP:
+ bug("Main table export stopped");
+
+ case RT_EXPORT_FEED:
+ /* Send updates one after another */
+ for (uint i = 0; i < u->feed->count_routes; i++)
+ {
+ rte *new = &u->feed->block[i];
+ if (new->flags & REF_OBSOLETE)
+ break;
+
+ bmp_split_policy(p, new, NULL);
+ }
+ break;
+
+ case RT_EXPORT_UPDATE:
+ bmp_split_policy(p, u->update->new, u->update->old);
+ break;
+ }
+ }
+}
+
+static void
+bmp_feed_end(struct rt_export_request *req)
+{
+ SKIP_BACK_DECLARE(struct bmp_table, bt, out_req, req);
+
+ struct bmp_proto *p = bt->p;
/*
* Unsynced streams are added in one moment during BMP session establishment,
bmp_buffer_free(&payload);
/* Send Peer Up messages */
- struct proto *peer;
- WALK_LIST(peer, proto_list)
- if ((peer->proto->class == PROTOCOL_BGP) && (peer->proto_state == PS_UP))
- bmp_peer_init(p, (struct bgp_proto *) peer);
+ u32 length;
+ PST_LOCKED(ts) /* The size of protos field will never decrease, the inconsistency caused by growing is not important */
+ length = ts->length_states;
+
+ /* Subscribe to protocol state changes */
+ p->proto_state_reader = (struct lfjour_recipient) {
+ .event = &p->proto_state_changed,
+ .target = proto_event_list(&p->p),
+ };
+
+ p->proto_state_changed = (event) {
+ .hook = bmp_proto_state_changed,
+ .data = p,
+ };
+
+ proto_states_subscribe(&p->proto_state_reader);
+
+ /* Load protocol states */
+ for (u32 i = 0; i < length; i++)
+ {
+ ea_list *proto_attr = proto_get_state(i);
+ if (proto_attr == NULL)
+ continue;
+
+ struct protocol *proto = (struct protocol *) ea_get_ptr(proto_attr, &ea_protocol_type, 0);
+ const int state = ea_get_int(proto_attr, &ea_state, 0);
+
+ if (proto != &proto_bgp || state != PS_UP)
+ continue;
+
+ bmp_peer_up_inout(p, proto_attr, false);
+ }
}
/**
TRACE(D_EVENTS, "BMP session closed");
+ proto_states_unsubscribe(&p->proto_state_reader);
+ ev_postpone(&p->proto_state_changed);
+
/* Unregister existing peer structures */
HASH_WALK_DELSAFE(p->peer_map, next, bp)
{
TRACE(D_EVENTS, "Connecting to %I port %u", sk->daddr, sk->dport);
- int rc = sk_open(sk);
+ int rc = sk_open(sk, p->p.loop);
if (rc < 0)
sk_log_error(sk, p->p.name);
- tm_start(p->connect_retry_timer, CONNECT_RETRY_TIME);
+ tm_start_in(p->connect_retry_timer, CONNECT_RETRY_TIME, p->p.loop);
}
/* BMP connect successful event - switch from Connect to Established state */
bmp_startup(p);
}
-/* BMP socket error event - switch from any state to Idle state */
+/* BMPbmp_startup socket error event - switch from any state to Idle state */
static void
bmp_sock_err(sock *sk, int err)
{
bmp_down(p);
bmp_close_socket(p);
- tm_start(p->connect_retry_timer, CONNECT_RETRY_TIME);
+ tm_start_in(p->connect_retry_timer, CONNECT_RETRY_TIME, p->p.loop);
- proto_notify_state(&p->p, PS_START);
+ if (p->p.proto_state == PS_UP)
+ proto_notify_state(&p->p, PS_START);
}
/* BMP connect timeout event - switch from Idle/Connect state to Connect state */
cf_error("Station port number not specified");
}
+
+static void
+bmp_process_proto_state_change(struct bmp_proto *p, struct lfjour_item *last_up)
+{
+ struct proto_pending_update *ppu = SKIP_BACK(struct proto_pending_update, li, last_up);
+ if (!ppu)
+ return;
+
+ if (bmp_peer_up_inout(p, ppu->proto_attr, true))
+ goto done;
+
+ SKIP_BACK_DECLARE(struct bgp_session_close_ad, bscad, ad, ea_get_adata(ppu->proto_attr, &ea_bgp_close_bmp));
+ if (bscad)
+ {
+ bmp_peer_down_(p, ppu->proto_attr, bscad);
+ goto done;
+ }
+
+done:
+ lfjour_release(&p->proto_state_reader, last_up);
+}
+
+static void
+bmp_proto_state_changed(void *_p)
+{
+ struct bmp_proto *p = _p;
+
+ ASSERT_DIE(birdloop_inside(p->p.loop));
+
+ struct lfjour_item *last_up;
+ while (last_up = lfjour_get(&p->proto_state_reader))
+ bmp_process_proto_state_change(p, last_up);
+}
+
/** Configuration handle section **/
static struct proto *
bmp_init(struct proto_config *CF)
struct bmp_proto *p = (void *) P;
struct bmp_config *cf = (void *) CF;
- P->rt_notify = bmp_rt_notify;
- P->preexport = bmp_preexport;
- P->feed_end = bmp_feed_end;
+ ASSERT_DIE(birdloop_inside(&main_birdloop));
+ if (!bgp_next_hop_ea_class)
+ bgp_next_hop_ea_class = ea_class_find_by_name("bgp_next_hop");
p->cf = cf;
p->local_addr = cf->local_addr;
{
struct bmp_proto *p = (void *) P;
- p->buffer_mpool = rp_new(P->pool, "BMP Buffer");
- p->map_mem_pool = rp_new(P->pool, "BMP Map");
- p->tx_mem_pool = rp_new(P->pool, "BMP Tx");
- p->update_msg_mem_pool = rp_new(P->pool, "BMP Update");
+ p->buffer_mpool = rp_new(P->pool, proto_domain(&p->p), "BMP Buffer");
+ p->map_mem_pool = rp_new(P->pool, proto_domain(&p->p), "BMP Map");
+ p->tx_mem_pool = rp_new(P->pool, proto_domain(&p->p), "BMP Tx");
+ p->update_msg_mem_pool = rp_new(P->pool, proto_domain(&p->p), "BMP Update");
p->tx_ev = ev_new_init(p->p.pool, bmp_fire_tx, p);
p->update_ev = ev_new_init(p->p.pool, bmp_route_monitor_commit, p);
p->connect_retry_timer = tm_new_init(p->p.pool, bmp_connection_retry, p, 0, 0);
init_list(&p->update_msg_queue);
p->started = false;
p->sock_err = 0;
- add_tail(&bmp_proto_list, &p->bmp_node);
- tm_start(p->connect_retry_timer, CONNECT_INIT_TIME);
+ tm_start_in(p->connect_retry_timer, CONNECT_INIT_TIME, p->p.loop);
return PS_START;
}
}
p->sock_err = 0;
- rem_node(&p->bmp_node);
return PS_FLUSH;
}
struct protocol proto_bmp = {
.name = "BMP",
.template = "bmp%d",
- .class = PROTOCOL_BMP,
.proto_size = sizeof(struct bmp_proto),
.config_size = sizeof(struct bmp_config),
.postconfig = bmp_postconfig,