Skip to content

Commit d16fb61

Browse files
authored
connlib: remove tun mutex (firezone#3743)
extracted from firezone#3738
1 parent 56e9e5e commit d16fb61

File tree

4 files changed

+18
-19
lines changed

4 files changed

+18
-19
lines changed

rust/connlib/tunnel/src/client.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,12 @@ where
150150

151151
#[tracing::instrument(level = "trace", skip(self))]
152152
pub fn add_route(&mut self, route: IpNetwork) -> connlib_shared::Result<()> {
153+
let callbacks = self.callbacks().clone();
153154
let maybe_new_device = self
154155
.device
155-
.as_ref()
156+
.as_mut()
156157
.ok_or(Error::ControlProtocolError)?
157-
.add_route(route, self.callbacks())?;
158+
.add_route(route, &callbacks)?;
158159

159160
if let Some(new_device) = maybe_new_device {
160161
self.device = Some(new_device);

rust/connlib/tunnel/src/device_channel.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ impl Device {
132132

133133
#[cfg(target_family = "unix")]
134134
pub(crate) fn add_route(
135-
&self,
135+
&mut self,
136136
route: IpNetwork,
137137
callbacks: &impl Callbacks<Error = Error>,
138138
) -> Result<Option<Device>, Error> {
@@ -149,8 +149,9 @@ impl Device {
149149
}
150150

151151
#[cfg(target_family = "windows")]
152+
#[allow(unused_mut)]
152153
pub(crate) fn add_route(
153-
&self,
154+
&mut self,
154155
route: IpNetwork,
155156
_: &impl Callbacks<Error = Error>,
156157
) -> Result<Option<Device>, Error> {

rust/connlib/tunnel/src/device_channel/tun_linux.rs

+11-14
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use libc::{
1313
};
1414
use netlink_packet_route::route::{RouteProtocol, RouteScope};
1515
use netlink_packet_route::rule::RuleAction;
16-
use parking_lot::Mutex;
1716
use rtnetlink::RuleAddRequest;
1817
use rtnetlink::{new_connection, Error::NetlinkError, Handle};
1918
use std::net::IpAddr;
@@ -50,7 +49,7 @@ pub struct Tun {
5049
connection: tokio::task::JoinHandle<()>,
5150
fd: AsyncFd<RawFd>,
5251

53-
worker: Mutex<Option<BoxFuture<'static, Result<()>>>>,
52+
worker: Option<BoxFuture<'static, Result<()>>>,
5453
}
5554

5655
impl fmt::Debug for Tun {
@@ -79,15 +78,14 @@ impl Tun {
7978
write(self.fd.as_raw_fd(), buf)
8079
}
8180

82-
pub fn poll_read(&self, buf: &mut [u8], cx: &mut Context<'_>) -> Poll<io::Result<usize>> {
83-
let mut guard = self.worker.lock();
84-
if let Some(worker) = guard.as_mut() {
81+
pub fn poll_read(&mut self, buf: &mut [u8], cx: &mut Context<'_>) -> Poll<io::Result<usize>> {
82+
if let Some(worker) = self.worker.as_mut() {
8583
match worker.poll_unpin(cx) {
8684
Poll::Ready(Ok(())) => {
87-
*guard = None;
85+
self.worker = None;
8886
}
8987
Poll::Ready(Err(e)) => {
90-
*guard = None;
88+
self.worker = None;
9189
return Poll::Ready(Err(io::Error::new(io::ErrorKind::Other, e)));
9290
}
9391
Poll::Pending => return Poll::Pending,
@@ -135,13 +133,13 @@ impl Tun {
135133
handle: handle.clone(),
136134
connection: join_handle,
137135
fd: AsyncFd::new(fd)?,
138-
worker: Mutex::new(Some(
136+
worker: Some(
139137
set_iface_config(config.clone(), dns_config, handle, dns_control_method).boxed(),
140-
)),
138+
),
141139
})
142140
}
143141

144-
pub fn add_route(&self, route: IpNetwork, _: &impl Callbacks) -> Result<Option<Self>> {
142+
pub fn add_route(&mut self, route: IpNetwork, _: &impl Callbacks) -> Result<Option<Self>> {
145143
let handle = self.handle.clone();
146144

147145
let add_route_worker = async move {
@@ -190,11 +188,10 @@ impl Tun {
190188
}
191189
};
192190

193-
let mut guard = self.worker.lock();
194-
match guard.take() {
195-
None => *guard = Some(add_route_worker.boxed()),
191+
match self.worker.take() {
192+
None => self.worker = Some(add_route_worker.boxed()),
196193
Some(current_worker) => {
197-
*guard = Some(
194+
self.worker = Some(
198195
async move {
199196
current_worker.await?;
200197
add_route_worker.await?;

rust/connlib/tunnel/src/gateway.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ where
2323
#[tracing::instrument(level = "trace", skip(self))]
2424
pub fn set_interface(&mut self, config: &InterfaceConfig) -> connlib_shared::Result<()> {
2525
// Note: the dns fallback strategy is irrelevant for gateways
26-
let device = Device::new(config, vec![], self.callbacks())?;
26+
let mut device = Device::new(config, vec![], self.callbacks())?;
2727

2828
let result_v4 = device.add_route(PEERS_IPV4.parse().unwrap(), self.callbacks());
2929
let result_v6 = device.add_route(PEERS_IPV6.parse().unwrap(), self.callbacks());

0 commit comments

Comments
 (0)