1 use std::sync::{Arc, Mutex};
2 use std::sync::atomic::{AtomicBool, Ordering};
4 use std::collections::{HashMap, hash_map};
5 use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
6 use std::time::{Duration, Instant};
8 use bgp_rs::{AFI, SAFI, AddPathDirection, Open, OpenCapability, OpenParameter, NLRIEncoding, PathAttribute};
9 use bgp_rs::Capabilities;
14 use tokio::prelude::*;
16 use tokio::codec::Framed;
17 use tokio::net::TcpStream;
18 use tokio::timer::Delay;
20 use futures::sync::mpsc;
22 use crate::printer::{Printer, Stat};
23 use crate::timeout_stream::TimeoutStream;
25 const PATH_SUFFIX_LEN: usize = 3;
27 struct Route { // 32 bytes with a path id u32
28 path_suffix: [u32; PATH_SUFFIX_LEN],
34 const ROUTE_LEN: usize = 36 - std::mem::size_of::<(u32, Route)>();
36 // To keep memory tight (and since we dont' need such close alignment), newtype the v4/v6 routing
37 // table entries to make sure they are aligned to single bytes.
40 #[derive(PartialEq, Eq, Hash)]
45 impl From<(Ipv4Addr, u8)> for V4Addr {
46 fn from(p: (Ipv4Addr, u8)) -> Self {
54 const V4_ALIGN: usize = 1 - std::mem::align_of::<V4Addr>();
56 const V4_SIZE: usize = 5 - std::mem::size_of::<V4Addr>();
59 #[derive(PartialEq, Eq, Hash)]
64 impl From<(Ipv6Addr, u8)> for V6Addr {
65 fn from(p: (Ipv6Addr, u8)) -> Self {
73 const V6_ALIGN: usize = 1 - std::mem::align_of::<V6Addr>();
75 const V6_SIZE: usize = 17 - std::mem::size_of::<V6Addr>();
78 // We really want a HashMap for the values here, but they'll only ever contain a few entries,
79 // and Vecs are way more memory-effecient in that case.
80 v4_table: HashMap<V4Addr, Vec<(u32, Route)>>,
81 v6_table: HashMap<V6Addr, Vec<(u32, Route)>>,
87 v4_table: HashMap::with_capacity(900_000),
88 v6_table: HashMap::with_capacity(100_000),
92 fn get_route_attrs(&self, ip: IpAddr) -> (u8, Vec<&Route>) {
93 macro_rules! lookup_res {
94 ($addrty: ty, $addr: expr, $table: expr, $addr_bits: expr) => { {
95 //TODO: Optimize this (probably means making the tables btrees)!
96 let mut lookup = <$addrty>::from(($addr, $addr_bits));
97 for i in 0..$addr_bits {
98 if let Some(routes) = $table.get(&lookup) {
100 return (lookup.pfxlen, routes.iter().map(|v| &v.1).collect());
103 lookup.addr[lookup.addr.len() - (i/8) - 1] &= !(1u8 << (i % 8));
110 IpAddr::V4(v4a) => lookup_res!(V4Addr, v4a, self.v4_table, 32),
111 IpAddr::V6(v6a) => lookup_res!(V6Addr, v6a, self.v6_table, 128)
115 fn withdraw(&mut self, route: NLRIEncoding) {
116 macro_rules! remove {
117 ($rt: expr, $v: expr, $id: expr) => { {
118 match $rt.entry($v.into()) {
119 hash_map::Entry::Occupied(mut entry) => {
120 entry.get_mut().retain(|e| e.0 != $id);
121 if entry.get_mut().is_empty() {
130 NLRIEncoding::IP(p) => {
131 let (ip, len) = <(IpAddr, u8)>::from(&p);
133 IpAddr::V4(v4a) => remove!(self.v4_table, (v4a, len), 0),
134 IpAddr::V6(v6a) => remove!(self.v6_table, (v6a, len), 0),
137 NLRIEncoding::IP_WITH_PATH_ID((p, id)) => {
138 let (ip, len) = <(IpAddr, u8)>::from(&p);
140 IpAddr::V4(v4a) => remove!(self.v4_table, (v4a, len), id),
141 IpAddr::V6(v6a) => remove!(self.v6_table, (v6a, len), id),
144 NLRIEncoding::IP_MPLS(_) => (),
145 NLRIEncoding::IP_MPLS_WITH_PATH_ID(_) => (),
146 NLRIEncoding::IP_VPN_MPLS(_) => (),
147 NLRIEncoding::L2VPN(_) => (),
151 fn announce(&mut self, prefix: NLRIEncoding, route: Route) {
152 macro_rules! insert {
153 ($rt: expr, $v: expr, $id: expr) => { {
154 let entry = $rt.entry($v.into()).or_insert(Vec::new());
155 entry.retain(|e| e.0 != $id);
156 entry.push(($id, route));
160 NLRIEncoding::IP(p) => {
161 let (ip, len) = <(IpAddr, u8)>::from(&p);
163 IpAddr::V4(v4a) => insert!(self.v4_table, (v4a, len), 0),
164 IpAddr::V6(v6a) => insert!(self.v6_table, (v6a, len), 0),
167 NLRIEncoding::IP_WITH_PATH_ID((p, id)) => {
168 let (ip, len) = <(IpAddr, u8)>::from(&p);
170 IpAddr::V4(v4a) => insert!(self.v4_table, (v4a, len), id),
171 IpAddr::V6(v6a) => insert!(self.v6_table, (v6a, len), id),
174 NLRIEncoding::IP_MPLS(_) => (),
175 NLRIEncoding::IP_MPLS_WITH_PATH_ID(_) => (),
176 NLRIEncoding::IP_VPN_MPLS(_) => (),
177 NLRIEncoding::L2VPN(_) => (),
182 struct BytesCoder<'a>(&'a mut bytes::BytesMut);
183 impl<'a> std::io::Write for BytesCoder<'a> {
184 fn write(&mut self, b: &[u8]) -> Result<usize, std::io::Error> {
185 self.0.extend_from_slice(&b);
188 fn flush(&mut self) -> Result<(), std::io::Error> {
192 struct BytesDecoder<'a> {
193 buf: &'a mut bytes::BytesMut,
196 impl<'a> std::io::Read for BytesDecoder<'a> {
197 fn read(&mut self, b: &mut [u8]) -> Result<usize, std::io::Error> {
198 let copy_len = cmp::min(b.len(), self.buf.len() - self.pos);
199 b[..copy_len].copy_from_slice(&self.buf[self.pos..self.pos + copy_len]);
200 self.pos += copy_len;
205 struct MsgCoder(Option<Capabilities>);
206 impl codec::Decoder for MsgCoder {
208 type Error = std::io::Error;
210 fn decode(&mut self, bytes: &mut bytes::BytesMut) -> Result<Option<Message>, std::io::Error> {
211 let mut decoder = BytesDecoder {
215 let def_cap = Default::default();
216 let mut reader = Reader {
217 stream: &mut decoder,
218 capabilities: if let Some(cap) = &self.0 { cap } else { &def_cap },
220 match reader.read() {
221 Ok((_header, msg)) => {
222 decoder.buf.advance(decoder.pos);
223 if let Message::Open(ref o) = &msg {
224 self.0 = Some(Capabilities::from_parameters(o.parameters.clone()));
228 Err(e) => match e.kind() {
229 std::io::ErrorKind::UnexpectedEof => Ok(None),
235 impl codec::Encoder for MsgCoder {
237 type Error = std::io::Error;
239 fn encode(&mut self, msg: Message, res: &mut bytes::BytesMut) -> Result<(), std::io::Error> {
240 msg.encode(&mut BytesCoder(res))?;
245 pub struct BGPClient {
246 routes: Mutex<RoutingTable>,
247 shutdown: AtomicBool,
250 pub fn get_asn(&self, addr: IpAddr) -> u32 {
251 let lock = self.routes.lock().unwrap();
252 let mut path_vecs = lock.get_route_attrs(addr).1;
253 if path_vecs.is_empty() { return 0; }
255 path_vecs.sort_unstable_by(|path_a, path_b| {
256 path_a.pref.cmp(&path_b.pref)
257 .then(path_b.path_len.cmp(&path_a.path_len))
258 .then(path_b.med.cmp(&path_a.med))
261 let primary_route = path_vecs.pop().unwrap();
262 'asn_candidates: for asn in primary_route.path_suffix.iter().rev() {
263 if *asn == 0 { continue 'asn_candidates; }
264 for secondary_route in path_vecs.iter() {
265 if !secondary_route.path_suffix.contains(asn) {
266 continue 'asn_candidates;
272 for asn in primary_route.path_suffix.iter().rev() {
280 pub fn get_path(&self, addr: IpAddr) -> (u8, [u32; PATH_SUFFIX_LEN]) {
281 let lock = self.routes.lock().unwrap();
282 let (prefixlen, mut path_vecs) = lock.get_route_attrs(addr);
283 if path_vecs.is_empty() { return (0, [0; PATH_SUFFIX_LEN]); }
285 path_vecs.sort_unstable_by(|path_a, path_b| {
286 path_a.pref.cmp(&path_b.pref)
287 .then(path_b.path_len.cmp(&path_a.path_len))
288 .then(path_b.med.cmp(&path_a.med))
291 let primary_route = path_vecs.pop().unwrap();
292 (prefixlen, primary_route.path_suffix)
295 pub fn disconnect(&self) {
296 self.shutdown.store(true, Ordering::Relaxed);
299 fn map_attrs(mut attrs: Vec<PathAttribute>) -> Option<Route> {
300 let mut as4_path = None;
301 let mut as_path = None;
304 for attr in attrs.drain(..) {
306 PathAttribute::AS4_PATH(path) => as4_path = Some(path),
307 PathAttribute::AS_PATH(path) => as_path = Some(path),
308 PathAttribute::LOCAL_PREF(p) => pref = p,
309 PathAttribute::MULTI_EXIT_DISC(m) => med = m,
313 if let Some(mut aspath) = as4_path.or(as_path) {
314 let mut pathvec = Vec::new();
315 for seg in aspath.segments.drain(..) {
317 Segment::AS_SEQUENCE(mut asn) => pathvec.append(&mut asn),
318 Segment::AS_SET(_) => {}, // Ignore sets for now, they're not that common anyway
321 let path_len = pathvec.len() as u32;
322 pathvec.dedup_by(|a, b| (*a).eq(b)); // Drop prepends, cause we don't care in this case
324 let mut path_suffix = [0; PATH_SUFFIX_LEN];
325 for (idx, asn) in pathvec.iter().rev().enumerate() {
326 path_suffix[PATH_SUFFIX_LEN - idx - 1] = *asn;
327 if idx == PATH_SUFFIX_LEN - 1 { break; }
339 fn connect_given_client(addr: SocketAddr, timeout: Duration, printer: &'static Printer, client: Arc<BGPClient>) {
340 tokio::spawn(Delay::new(Instant::now() + timeout / 4).then(move |_| {
341 let connect_timeout = Delay::new(Instant::now() + timeout.clone()).then(|_| {
342 future::err(std::io::Error::new(std::io::ErrorKind::TimedOut, "timeout reached"))
344 let client_reconn = Arc::clone(&client);
345 TcpStream::connect(&addr).select(connect_timeout)
347 Delay::new(Instant::now() + timeout / 2).then(|_| {
350 }).and_then(move |stream| {
351 let (write, read) = Framed::new(stream.0, MsgCoder(None)).split();
352 let (mut sender, receiver) = mpsc::channel(10); // We never really should send more than 10 messages unless they're dumb
353 tokio::spawn(write.sink_map_err(|_| { () }).send_all(receiver)
357 let _ = sender.try_send(Message::Open(Open {
360 hold_timer: timeout.as_secs() as u16,
361 identifier: 0x453b1215, // 69.59.18.21
362 parameters: vec![OpenParameter::Capabilities(vec![
363 OpenCapability::MultiProtocol((AFI::IPV4, SAFI::Unicast)),
364 OpenCapability::MultiProtocol((AFI::IPV6, SAFI::Unicast)),
365 OpenCapability::FourByteASN(397444),
366 OpenCapability::RouteRefresh,
367 OpenCapability::AddPath(vec![
368 (AFI::IPV4, SAFI::Unicast, AddPathDirection::ReceivePaths),
369 (AFI::IPV6, SAFI::Unicast, AddPathDirection::ReceivePaths)]),
372 TimeoutStream::new_persistent(read, timeout).for_each(move |bgp_msg| {
373 if client.shutdown.load(Ordering::Relaxed) {
374 return future::err(std::io::Error::new(std::io::ErrorKind::Other, "Shutting Down"));
377 Message::Open(_) => {
378 client.routes.lock().unwrap().v4_table.clear();
379 client.routes.lock().unwrap().v6_table.clear();
380 printer.add_line("Connected to BGP route provider".to_string(), false);
382 Message::KeepAlive => {
383 let _ = sender.try_send(Message::KeepAlive);
385 Message::Update(mut upd) => {
387 let mut route_table = client.routes.lock().unwrap();
388 for r in upd.withdrawn_routes {
389 route_table.withdraw(r);
391 if let Some(path) = Self::map_attrs(upd.attributes) {
392 for r in upd.announced_routes {
393 route_table.announce(r, path.clone());
396 printer.set_stat(Stat::V4RoutingTableSize(route_table.v4_table.len()));
397 printer.set_stat(Stat::V6RoutingTableSize(route_table.v6_table.len()));
402 }).or_else(move |e| {
403 printer.add_line(format!("Got error from BGP stream: {:?}", e), true);
407 if !client_reconn.shutdown.load(Ordering::Relaxed) {
408 BGPClient::connect_given_client(addr, timeout, printer, client_reconn);
416 pub fn new(addr: SocketAddr, timeout: Duration, printer: &'static Printer) -> Arc<BGPClient> {
417 let client = Arc::new(BGPClient {
418 routes: Mutex::new(RoutingTable::new()),
419 shutdown: AtomicBool::new(false),
421 BGPClient::connect_given_client(addr, timeout, printer, Arc::clone(&client));