1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
/*
 * Copyright 2020 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

mod sessions;

pub use sessions::SessionKey;

use std::{
    net::{Ipv4Addr, SocketAddrV4},
    sync::Arc,
};

use prometheus::HistogramTimer;
use tokio::{net::UdpSocket, sync::watch, time::Duration};

use crate::{
    endpoint::{Endpoint, EndpointAddress},
    filters::{Filter, ReadContext},
    proxy::sessions::{manager::SessionManager, SessionArgs, SESSION_TIMEOUT_SECONDS},
    utils::{debug, net},
    xds::ResourceType,
    Config, Result,
};

/// The UDP proxy service.
pub struct Proxy {
    pub config: Arc<Config>,
}

impl TryFrom<Config> for Proxy {
    type Error = eyre::Error;
    fn try_from(config: Config) -> Result<Self, Self::Error> {
        Ok(Self {
            config: Arc::from(config),
        })
    }
}

impl TryFrom<Arc<Config>> for Proxy {
    type Error = eyre::Error;
    fn try_from(config: Arc<Config>) -> Result<Self, Self::Error> {
        Ok(Self { config })
    }
}

/// Represents arguments to the `Proxy::run_recv_from` method.
struct RunRecvFromArgs {
    session_manager: SessionManager,
    session_ttl: Duration,
    shutdown_rx: watch::Receiver<()>,
}

/// Packet received from local port
#[derive(Debug)]
struct DownstreamPacket {
    source: EndpointAddress,
    contents: Vec<u8>,
    timer: HistogramTimer,
}

/// Represents the required arguments to run a worker task that
/// processes packets received downstream.
struct DownstreamReceiveWorkerConfig {
    /// ID of the worker.
    worker_id: usize,
    /// Socket with reused port from which the worker receives packets.
    socket: Arc<UdpSocket>,
    /// Configuration required to process a received downstream packet.
    receive_config: ProcessDownstreamReceiveConfig,
    /// The worker task exits when a value is received from this shutdown channel.
    shutdown_rx: watch::Receiver<()>,
}

/// Contains arguments to process a received downstream packet, through the
/// filter chain and session pipeline.
struct ProcessDownstreamReceiveConfig {
    config: Arc<Config>,
    session_manager: SessionManager,
    session_ttl: Duration,
    socket: Arc<UdpSocket>,
}

impl Proxy {
    /// Returns a builder for configuring a Quilkin proxy.
    pub fn builder() -> crate::config::Builder {
        <_>::default()
    }

    /// start the async processing of incoming UDP packets. Will block until an
    /// event is sent through the stop Receiver.
    pub async fn run(self, mut shutdown_rx: watch::Receiver<()>) -> Result<()> {
        tracing::info!(
            port = *self.config.port.load(),
            proxy_id = &*self.config.id.load(),
            "Starting"
        );

        let session_manager = SessionManager::new(shutdown_rx.clone());
        let session_ttl = Duration::from_secs(SESSION_TIMEOUT_SECONDS);

        let management_servers = self.config.management_servers.load();
        let _xds_stream = if !management_servers.is_empty() {
            let client = crate::xds::Client::connect(self.config.clone()).await?;
            let mut stream = client.stream().await?;

            tokio::time::sleep(std::time::Duration::from_nanos(1)).await;
            stream.send(ResourceType::Endpoint, &[]).await?;
            tokio::time::sleep(std::time::Duration::from_nanos(1)).await;
            stream.send(ResourceType::Listener, &[]).await?;
            Some(stream)
        } else {
            None
        };

        self.run_recv_from(RunRecvFromArgs {
            session_manager,
            session_ttl,
            shutdown_rx: shutdown_rx.clone(),
        })
        .await?;
        tracing::info!("Quilkin is ready");

        shutdown_rx
            .changed()
            .await
            .map_err(|error| eyre::eyre!(error))
    }

    /// Spawns a background task that sits in a loop, receiving packets from the passed in socket.
    /// Each received packet is placed on a queue to be processed by a worker task.
    /// This function also spawns the set of worker tasks responsible for consuming packets
    /// off the aforementioned queue and processing them through the filter chain and session
    /// pipeline.
    async fn run_recv_from(&self, args: RunRecvFromArgs) -> Result<()> {
        let session_manager = args.session_manager;

        // The number of worker tasks to spawn. Each task gets a dedicated queue to
        // consume packets off.
        let num_workers = num_cpus::get();

        // Contains config for each worker task.
        let mut worker_configs = vec![];
        for worker_id in 0..num_workers {
            let socket = Arc::new(self.bind(*self.config.port.load())?);
            worker_configs.push(DownstreamReceiveWorkerConfig {
                worker_id,
                socket: socket.clone(),
                shutdown_rx: args.shutdown_rx.clone(),
                receive_config: ProcessDownstreamReceiveConfig {
                    config: self.config.clone(),
                    session_manager: session_manager.clone(),
                    session_ttl: args.session_ttl,
                    socket,
                },
            })
        }

        // Start the worker tasks that pick up received packets from their queue
        // and processes them.
        Self::spawn_downstream_receive_workers(worker_configs);
        Ok(())
    }

    /// For each worker config provided, spawn a background task that sits in a
    /// loop, receiving packets from a socket and processing them through
    /// the filter chain.
    fn spawn_downstream_receive_workers(worker_configs: Vec<DownstreamReceiveWorkerConfig>) {
        for DownstreamReceiveWorkerConfig {
            worker_id,
            socket,
            mut shutdown_rx,
            receive_config,
        } in worker_configs
        {
            tokio::spawn(async move {
                // Initialize a buffer for the UDP packet. We use the maximum size of a UDP
                // packet, which is the maximum value of 16 a bit integer.
                let mut buf = vec![0; 1 << 16];
                loop {
                    tracing::debug!(
                        id = worker_id,
                        addr = ?socket.local_addr(),
                        "Awaiting packet"
                    );
                    tokio::select! {
                        recv = socket.recv_from(&mut buf) => {
                            let timer = crate::metrics::processing_time(crate::metrics::READ).start_timer();
                            match recv {
                                Ok((size, source)) => {
                                    let contents = buf[..size].to_vec();
                                    tracing::trace!(id = worker_id, size = size, source = %source, contents=&*debug::bytes_to_string(&contents), "received packet from downstream");
                                    let packet = DownstreamPacket {
                                        source: source.into(),
                                        contents,
                                        timer,
                                    };
                                    Self::process_downstream_received_packet(packet, &receive_config).await
                                },
                                Err(error) => {
                                    tracing::error!(%error);
                                    return;
                                }
                            }
                        }
                        _ = shutdown_rx.changed() => {
                            tracing::debug!(id = worker_id, "Received shutdown signal");
                            return;
                        }
                    }
                }
            });
        }
    }

    /// Processes a packet by running it through the filter chain.
    async fn process_downstream_received_packet(
        packet: DownstreamPacket,
        args: &ProcessDownstreamReceiveConfig,
    ) {
        let clusters = args.config.clusters.load();
        let endpoints: Vec<_> = clusters.endpoints().collect();
        if endpoints.is_empty() {
            tracing::trace!("dropping packet, no upstream endpoints available");
            crate::metrics::packets_dropped_total(crate::metrics::READ, "NoEndpointsAvailable")
                .inc();
            return;
        }

        let filters = args.config.filters.load();
        let mut context = ReadContext::new(endpoints, packet.source, packet.contents);
        let result = filters.read(&mut context);

        if let Some(()) = result {
            for endpoint in context.endpoints.iter() {
                Self::session_send_packet(
                    &context.contents,
                    context.source.clone(),
                    endpoint,
                    args,
                )
                .await;
            }
        }

        packet.timer.stop_and_record();
    }

    /// Send a packet received from `recv_addr` to an endpoint.
    #[tracing::instrument(skip_all, fields(source = %recv_addr, dest = %endpoint.address))]
    async fn session_send_packet(
        packet: &[u8],
        recv_addr: EndpointAddress,
        endpoint: &Endpoint,
        args: &ProcessDownstreamReceiveConfig,
    ) {
        let session_key = SessionKey {
            source: recv_addr,
            dest: endpoint.address.clone(),
        };

        // Grab a read lock and find the session.
        let guard = args.session_manager.get_sessions().await;
        if let Some(session) = guard.get(&session_key) {
            // If it exists then send the packet, we're done.
            session.send(packet, args.session_ttl).await
        } else {
            // If it does not exist, grab a write lock so that we can create it.
            //
            // NOTE: We must drop the lock guard to release the lock before
            // trying to acquire a write lock since these lock aren't reentrant,
            // otherwise we will deadlock with our self.
            drop(guard);

            // Grab a write lock.
            let mut guard = args.session_manager.get_sessions_mut().await;

            // Although we have the write lock now, check whether some other thread
            // managed to create the session in-between our dropping the read
            // lock and grabbing the write lock.
            if let Some(session) = guard.get(&session_key) {
                tracing::trace!("Reusing previous session");
                // If the session now exists then we have less work to do,
                // simply send the packet.
                session.send(packet, args.session_ttl).await;
            } else {
                tracing::trace!("Creating new session");
                // Otherwise, create the session and insert into the map.
                let session_args = SessionArgs {
                    config: args.config.clone(),
                    source: session_key.source.clone(),
                    downstream_socket: args.socket.clone(),
                    dest: endpoint.clone(),
                    ttl: args.session_ttl,
                };
                match session_args.into_session().await {
                    Ok(session) => {
                        // Insert the session into the map and release the write lock
                        // immediately since we don't want to block other threads while we send
                        // the packet. Instead, re-acquire a read lock and send the packet.
                        guard.insert(session.key(), session);

                        // Release the write lock.
                        drop(guard);

                        // Grab a read lock to send the packet.
                        let guard = args.session_manager.get_sessions().await;
                        if let Some(session) = guard.get(&session_key) {
                            session.send(packet, args.session_ttl).await;
                        } else {
                            tracing::warn!(
                                key = %format!("({}:{})", session_key.source, session_key.dest),
                                "Could not find session"
                            )
                        }
                    }
                    Err(error) => {
                        tracing::error!(%error, "Failed to ensure session exists");
                    }
                }
            }
        }
    }

    /// binds the local configured port with port and address reuse applied.
    fn bind(&self, port: u16) -> Result<UdpSocket> {
        let addr = SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, port);
        net::socket_with_reuse(addr.into())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use tokio::time::{timeout, Duration};

    use crate::{
        config,
        endpoint::Endpoint,
        test_utils::{available_addr, create_socket, load_test_filters, TestHelper},
    };

    #[tokio::test]
    async fn run_server() {
        let mut t = TestHelper::default();

        let endpoint1 = t.open_socket_and_recv_single_packet().await;
        let endpoint2 = t.open_socket_and_recv_single_packet().await;

        let local_addr = available_addr().await;
        let config = Config::builder()
            .port(local_addr.port())
            .endpoints(vec![
                Endpoint::new(endpoint1.socket.local_addr().unwrap().into()),
                Endpoint::new(endpoint2.socket.local_addr().unwrap().into()),
            ])
            .build()
            .unwrap();
        t.run_server_with_config(config);

        let msg = "hello";
        endpoint1
            .socket
            .send_to(msg.as_bytes(), &local_addr)
            .await
            .unwrap();
        assert_eq!(
            msg,
            timeout(Duration::from_secs(1), endpoint1.packet_rx)
                .await
                .expect("should get a packet")
                .unwrap()
        );
        assert_eq!(
            msg,
            timeout(Duration::from_secs(1), endpoint2.packet_rx)
                .await
                .expect("should get a packet")
                .unwrap()
        );
    }

    #[tokio::test]
    async fn run_client() {
        let mut t = TestHelper::default();

        let endpoint = t.open_socket_and_recv_single_packet().await;

        let local_addr = available_addr().await;
        let config = Config::builder()
            .port(local_addr.port())
            .endpoints(vec![Endpoint::new(
                endpoint.socket.local_addr().unwrap().into(),
            )])
            .build()
            .unwrap();
        t.run_server_with_config(config);

        let msg = "hello";
        endpoint
            .socket
            .send_to(msg.as_bytes(), &local_addr)
            .await
            .unwrap();
        assert_eq!(
            msg,
            timeout(Duration::from_millis(100), endpoint.packet_rx)
                .await
                .unwrap()
                .unwrap()
        );
    }

    #[tokio::test]
    async fn run_with_filter() {
        let mut t = TestHelper::default();

        load_test_filters();
        let endpoint = t.open_socket_and_recv_single_packet().await;
        let local_addr = available_addr().await;
        let config = Config::builder()
            .port(local_addr.port())
            .filters(vec![config::Filter {
                name: "TestFilter".to_string(),
                config: None,
            }])
            .endpoints(vec![Endpoint::new(
                endpoint.socket.local_addr().unwrap().into(),
            )])
            .build()
            .unwrap();
        t.run_server_with_config(config);

        let msg = "hello";
        endpoint
            .socket
            .send_to(msg.as_bytes(), &local_addr)
            .await
            .unwrap();

        // search for the filter strings.
        let result = timeout(Duration::from_millis(100), endpoint.packet_rx)
            .await
            .unwrap()
            .unwrap();
        assert!(result.contains(msg), "'{}' not found in '{}'", msg, result);
        assert!(result.contains(":odr:"), ":odr: not found in '{}'", result);
    }

    #[tokio::test]
    async fn spawn_downstream_receive_workers() {
        let t = TestHelper::default();

        let socket = Arc::new(create_socket().await);
        let addr = socket.local_addr().unwrap();
        let (_shutdown_tx, shutdown_rx) = watch::channel(());
        let endpoint = t.open_socket_and_recv_single_packet().await;
        let msg = "hello";

        // we'll test a single DownstreamReceiveWorkerConfig
        let config = DownstreamReceiveWorkerConfig {
            worker_id: 1,
            socket: socket.clone(),
            receive_config: ProcessDownstreamReceiveConfig {
                config: Arc::new(
                    Config::builder()
                        .endpoints(&[endpoint.socket.local_addr().unwrap().into()][..])
                        .build()
                        .unwrap(),
                ),
                session_manager: SessionManager::new(shutdown_rx.clone()),
                session_ttl: Duration::from_secs(10),
                socket,
            },
            shutdown_rx,
        };

        Proxy::spawn_downstream_receive_workers(vec![config]);

        let socket = create_socket().await;
        socket.send_to(msg.as_bytes(), &addr).await.unwrap();

        assert_eq!(
            msg,
            timeout(Duration::from_secs(1), endpoint.packet_rx)
                .await
                .expect("should receive a packet")
                .unwrap()
        );
    }

    #[tokio::test]
    async fn run_recv_from() {
        let t = TestHelper::default();
        let (_shutdown_tx, shutdown_rx) = watch::channel(());

        let msg = "hello";
        let endpoint = t.open_socket_and_recv_single_packet().await;
        let session_manager = SessionManager::new(shutdown_rx.clone());
        let local_addr = available_addr().await;
        let config = Config::builder()
            .port(local_addr.port())
            .endpoints(&[Endpoint::from(endpoint.socket.local_addr().unwrap())][..])
            .build()
            .unwrap();
        let server = Proxy::try_from(config).unwrap();

        server
            .run_recv_from(RunRecvFromArgs {
                session_manager: session_manager.clone(),
                session_ttl: Duration::from_secs(10),
                shutdown_rx,
            })
            .await
            .unwrap();

        let socket = create_socket().await;
        socket.send_to(msg.as_bytes(), &local_addr).await.unwrap();
        assert_eq!(
            msg,
            timeout(Duration::from_secs(1), endpoint.packet_rx)
                .await
                .expect("should receive a packet")
                .unwrap()
        );
    }
}