|
| 1 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +// file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 4 | + |
| 5 | +use crate::AddPeerError; |
| 6 | +use crate::AddPeerRequest; |
| 7 | +use crate::ListenerShutdownHandle; |
| 8 | +use crate::Session; |
| 9 | +use crate::SessionCounters; |
| 10 | +use crate::dispatcher::Dispatcher; |
| 11 | +use crate::egress_src_port_iter::EgressSrcPortIter; |
| 12 | +use slog::Logger; |
| 13 | +use slog::warn; |
| 14 | +use std::collections::HashMap; |
| 15 | +use std::collections::hash_map; |
| 16 | +use std::net::IpAddr; |
| 17 | +use std::net::SocketAddr; |
| 18 | +use std::sync::Arc; |
| 19 | + |
| 20 | +pub struct Daemon { |
| 21 | + dispatcher: Dispatcher, |
| 22 | + sessions: HashMap<IpAddr, Session>, |
| 23 | + egress_src_port: Arc<EgressSrcPortIter>, |
| 24 | + log: Logger, |
| 25 | +} |
| 26 | + |
| 27 | +impl Daemon { |
| 28 | + pub fn new(log: Logger) -> Self { |
| 29 | + Self::with_dispatcher(Dispatcher::new(), log) |
| 30 | + } |
| 31 | + |
| 32 | + // Non-public method to allow construction with a custom dispatcher. |
| 33 | + // |
| 34 | + // This is used by tests when they want to use a `Dispatcher` with a custom |
| 35 | + // backend (e.g., so we can use nonstandard listening ports in tests). |
| 36 | + pub(crate) fn with_dispatcher(dispatcher: Dispatcher, log: Logger) -> Self { |
| 37 | + Self { |
| 38 | + sessions: HashMap::new(), |
| 39 | + dispatcher, |
| 40 | + egress_src_port: Arc::new(EgressSrcPortIter::new()), |
| 41 | + log, |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + pub fn sessions_iter(&self) -> hash_map::Iter<'_, IpAddr, Session> { |
| 46 | + self.sessions.iter() |
| 47 | + } |
| 48 | + |
| 49 | + pub fn listen_addr_for_peer(&self, peer: &IpAddr) -> Option<SocketAddr> { |
| 50 | + self.dispatcher.listen_addr_for_peer(peer) |
| 51 | + } |
| 52 | + |
| 53 | + pub fn add_peer( |
| 54 | + &mut self, |
| 55 | + db: rdb::Db, |
| 56 | + rq: AddPeerRequest, |
| 57 | + ) -> Result<(), AddPeerError> { |
| 58 | + let peer = rq.remote_addr.ip(); |
| 59 | + match self.sessions.entry(peer) { |
| 60 | + hash_map::Entry::Occupied(_) => { |
| 61 | + // TODO-correctness Currently clients have no way to update an |
| 62 | + // existing peer: they have to remove it and recreate it. This |
| 63 | + // needs work both here and in omicron to fix. |
| 64 | + // <https://github.com/oxidecomputer/omicron/issues/4921> |
| 65 | + warn!( |
| 66 | + self.log, "attempt to add peer that already exists"; |
| 67 | + "component" => crate::COMPONENT_BFD, |
| 68 | + "module" => crate::MOD_DAEMON, |
| 69 | + "unit" => crate::UNIT_PEER, |
| 70 | + "peer" => %peer, |
| 71 | + ); |
| 72 | + Err(AddPeerError::PeerExists(peer)) |
| 73 | + } |
| 74 | + hash_map::Entry::Vacant(entry) => { |
| 75 | + let counters = Arc::new(SessionCounters::default()); |
| 76 | + |
| 77 | + // If `ensure` fails, we can immediately bail out. If it |
| 78 | + // succeeds, we've now modified state inside the dispatcher; |
| 79 | + // it's critical that `Session::new()` is infallible; if that |
| 80 | + // changes in the future, we need to be very careful to ensure |
| 81 | + // that we undo the dispatcher state change if we can't |
| 82 | + // successfully create the associated `Session`. |
| 83 | + let listener_rx = self.dispatcher.ensure( |
| 84 | + rq.listen_addr, |
| 85 | + peer, |
| 86 | + Arc::clone(&counters), |
| 87 | + &self.log, |
| 88 | + )?; |
| 89 | + |
| 90 | + let session = Session::new( |
| 91 | + db, |
| 92 | + rq, |
| 93 | + counters, |
| 94 | + Arc::clone(&self.egress_src_port), |
| 95 | + listener_rx, |
| 96 | + &self.log, |
| 97 | + ); |
| 98 | + |
| 99 | + entry.insert(session); |
| 100 | + Ok(()) |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + pub fn remove_peer( |
| 106 | + &mut self, |
| 107 | + peer: IpAddr, |
| 108 | + ) -> Option<ListenerShutdownHandle> { |
| 109 | + self.sessions.remove(&peer); |
| 110 | + self.dispatcher.remove(peer) |
| 111 | + } |
| 112 | +} |
0 commit comments