|
| 1 | +use crate::compressed_attrs::decompress_route_attrs; |
| 2 | +use crate::store::make_bgp_withdraw; |
| 3 | +use crate::store::TableSelector; |
| 4 | +use crate::store_impl::InMemoryStore; |
| 5 | +use crate::table_impl::Action; |
| 6 | +use crate::table_impl::InMemoryTable; |
| 7 | +use crate::table_stream::table_stream; |
| 8 | +use futures_util::pin_mut; |
| 9 | +use futures_util::StreamExt; |
| 10 | +use serde::Deserialize; |
| 11 | +use std::net::IpAddr; |
| 12 | +use std::net::Ipv4Addr; |
| 13 | +use std::net::SocketAddr; |
| 14 | +use std::time::Duration; |
| 15 | +use tokio::io::AsyncWriteExt; |
| 16 | +use tokio::net::TcpSocket; |
| 17 | +use tokio::net::TcpStream; |
| 18 | +use zettabgp::bmp::prelude::*; |
| 19 | +use zettabgp::prelude::*; |
| 20 | + |
| 21 | +#[derive(Debug, Deserialize)] |
| 22 | +pub struct RelayConfig { |
| 23 | + table: TableSelector, |
| 24 | + |
| 25 | + /// For LocRIB fake BGP open message |
| 26 | + router_id: Ipv4Addr, |
| 27 | + asn: u32, |
| 28 | + |
| 29 | + monitoring_station: SocketAddr, |
| 30 | + |
| 31 | + bind_addr: Option<IpAddr>, |
| 32 | + bind_port: Option<u16>, |
| 33 | +} |
| 34 | + |
| 35 | +async fn connect(cfg: &RelayConfig) -> std::io::Result<TcpStream> { |
| 36 | + let (sock, default_bind_addr) = match cfg.monitoring_station.ip() { |
| 37 | + IpAddr::V4(_) => (TcpSocket::new_v4()?, "0.0.0.0".parse().unwrap()), |
| 38 | + IpAddr::V6(_) => (TcpSocket::new_v6()?, "::".parse().unwrap()), |
| 39 | + }; |
| 40 | + let bind_addr = SocketAddr::new( |
| 41 | + cfg.bind_addr.unwrap_or(default_bind_addr), |
| 42 | + cfg.bind_port.unwrap_or(0), |
| 43 | + ); |
| 44 | + sock.bind(bind_addr)?; |
| 45 | + sock.connect(cfg.monitoring_station).await |
| 46 | +} |
| 47 | + |
| 48 | +async fn run_connection(cfg: &RelayConfig, table: &InMemoryTable, mut tcp_stream: TcpStream) { |
| 49 | + let mut buf = [0; 10000]; |
| 50 | + let updates_stream = table_stream(table); |
| 51 | + pin_mut!(updates_stream); |
| 52 | + |
| 53 | + let fake_open_message = BgpOpenMessage { |
| 54 | + as_num: cfg.asn, |
| 55 | + caps: vec![ |
| 56 | + BgpCapability::SafiIPv4u, |
| 57 | + BgpCapability::SafiIPv6u, |
| 58 | + BgpCapability::SafiVPNv4u, |
| 59 | + BgpCapability::SafiVPNv6u, |
| 60 | + BgpCapability::CapRR, |
| 61 | + BgpCapability::CapASN32(cfg.asn), |
| 62 | + ], |
| 63 | + hold_time: 0, |
| 64 | + router_id: cfg.router_id, |
| 65 | + }; |
| 66 | + let peer_hdr = BmpMessagePeerHeader { |
| 67 | + peertype: 3, |
| 68 | + flags: 0, |
| 69 | + peerdistinguisher: BgpRD::new(0, 0), |
| 70 | + peeraddress: "::".parse().unwrap(), |
| 71 | + asnum: cfg.asn, |
| 72 | + routerid: cfg.router_id, |
| 73 | + timestamp: 0, |
| 74 | + }; |
| 75 | + let mut bmp_messages = futures_util::stream::iter([ |
| 76 | + BmpMessage::Initiation(BmpMessageInitiation { |
| 77 | + str0: None, |
| 78 | + sys_descr: None, |
| 79 | + sys_name: None, |
| 80 | + }), |
| 81 | + BmpMessage::PeerUpNotification(BmpMessagePeerUp { |
| 82 | + peer: peer_hdr.clone(), |
| 83 | + localaddress: "::".parse().unwrap(), |
| 84 | + localport: 0, |
| 85 | + remoteport: 0, |
| 86 | + msg1: fake_open_message.clone(), |
| 87 | + msg2: fake_open_message, |
| 88 | + }), |
| 89 | + ]) |
| 90 | + .chain(updates_stream.map(|action| { |
| 91 | + let update = match action { |
| 92 | + (net, num, Action::Withdraw) => { |
| 93 | + if num != 0 { |
| 94 | + log::warn!("add-paths table is not yet implemented"); |
| 95 | + } |
| 96 | + make_bgp_withdraw(net) |
| 97 | + } |
| 98 | + (net, num, Action::Update(attrs)) => { |
| 99 | + if num != 0 { |
| 100 | + log::warn!("add-paths table is not yet implemented"); |
| 101 | + } |
| 102 | + decompress_route_attrs(&attrs).to_bgp_update(net) |
| 103 | + } |
| 104 | + }; |
| 105 | + |
| 106 | + BmpMessage::RouteMonitoring(BmpMessageRouteMonitoring { |
| 107 | + peer: peer_hdr.clone(), |
| 108 | + update, |
| 109 | + }) |
| 110 | + })); |
| 111 | + |
| 112 | + while let Some(bmp_msg) = bmp_messages.next().await { |
| 113 | + log::trace!("sending message {}: {:?}", cfg.monitoring_station, bmp_msg); |
| 114 | + let mut len = 0; |
| 115 | + match bmp_msg.encode_to(&mut buf[5..]) { |
| 116 | + Ok(i) => len += i, |
| 117 | + Err(e) => { |
| 118 | + log::warn!("error encoding BMP message {:?}: {}", bmp_msg, e); |
| 119 | + continue; |
| 120 | + } |
| 121 | + } |
| 122 | + let msg_hdr = BmpMessageHeader { |
| 123 | + version: 3, |
| 124 | + msglength: len + 5, |
| 125 | + }; |
| 126 | + len += msg_hdr.encode_to(&mut buf).unwrap(); |
| 127 | + |
| 128 | + if let Err(e) = tcp_stream.write_all(&buf[..len]).await { |
| 129 | + log::warn!( |
| 130 | + "resetting connection {:?}, reason: {}", |
| 131 | + cfg.monitoring_station, |
| 132 | + e |
| 133 | + ); |
| 134 | + return; |
| 135 | + } |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +async fn run_(cfg: RelayConfig, store: InMemoryStore) -> ! { |
| 140 | + let table = store.get_table(cfg.table.clone()); |
| 141 | + loop { |
| 142 | + let tcp_stream = match connect(&cfg).await { |
| 143 | + Err(_) => { |
| 144 | + log::info!("trying to connect {}", cfg.monitoring_station); |
| 145 | + tokio::time::sleep(Duration::from_secs(5)).await; |
| 146 | + continue; |
| 147 | + } |
| 148 | + Ok(v) => v, |
| 149 | + }; |
| 150 | + log::info!("connected {}", cfg.monitoring_station); |
| 151 | + |
| 152 | + run_connection(&cfg, &table, tcp_stream).await; |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +pub async fn run( |
| 157 | + cfg: RelayConfig, |
| 158 | + store: InMemoryStore, |
| 159 | + mut shutdown: tokio::sync::watch::Receiver<bool>, |
| 160 | +) -> anyhow::Result<()> { |
| 161 | + tokio::select! { |
| 162 | + _ = run_(cfg, store) => unreachable!(), |
| 163 | + _ = shutdown.changed() => Ok(()), |
| 164 | + } |
| 165 | +} |
0 commit comments