Top Banner
Next Gen Networking Infrastructure With Rust
82

Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

Oct 02, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

Next Gen Networking Infrastructure With Rust

Page 2: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

Hi, I’m @carllerche

Page 3: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

You may remember me from…

Page 4: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

Most newer databases are written in a language that includes a runtime

Page 5: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

C / C++

Page 6: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

we’ll do it live…Memory management

Page 7: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

SEGV

Page 8: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

Heartbleed, cloudbleed, WannaCry

Page 9: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

– Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers

“Just use linting tools”

Page 10: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

Speed Safety

Page 11: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting
Page 12: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

Speed Safety

Page 13: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

Speed Safety

Page 14: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

Checks at compile time

Page 15: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

Ownership

Page 16: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

Data has one owner

Page 17: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

Compiles

fn print(s: String) { println!("{}", s); }

let foo = String::new(); let bar = foo;

print(bar);

Page 18: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

Compiles

fn print(s: String) { println!("{}", s); }

let foo = String::new(); let bar = foo;

print(bar);

Page 19: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

fn print(s: String) { println!("{}", s); }

let foo = String::new(); let bar = foo;

print(foo);

| 8 | let bar = foo; | --- value moved here 9 | 10 | print(foo); | ^^^ value used here after move | = note: move occurs because `foo` has type `std::string::String`, which does not implement the `Copy` trait

Page 20: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

Data is borrowed

Page 21: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

Compiles

fn print(s: &String) { println!("{}", s); }

let foo = String::new(); let bar = &foo;

print(&foo); print(bar);

Page 22: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

fn print(s: &String) { println!("{}", s); }

fn drop(s: String) {}

let foo = String::new(); let bar = &foo;

print(&foo); drop(foo); print(bar);

| 10 | let bar = &foo; | --- borrow of `foo` occurs here ... 14 | drop(foo); | ^^^ move out of `foo` occurs here

Page 23: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

Mutable borrows

Page 24: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

fn print(s: &mut String) { println!("{}", s); }

let mut foo = String::new(); let bar = &mut foo;

print(&mut foo);

| 8 | let bar = &mut foo; | --- first mutable borrow | occurs here 9 | 10 | print(&mut foo); | ^^^ second mutable borrow | occurs here 11 | } | - first borrow ends here

Page 25: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

let mut vec = vec![];

vec.push(String::new());

// lots of code here

let val = &vec[0];

// lots of code here

vec.push(String::new());

// lots of code here

println!("{}", val);

| 9 | let val = &vec[0]; | --- immutable borrow occurs | here ... 13 | vec.push(String::new()); | ^^^ mutable borrow occurs here ... 19 | } | - immutable borrow ends here

Page 26: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

I’m sorry, Dave. I’m afraid I can’t do that.

Page 27: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

Modern Language Features

• Fearless concurrency

• Closures

• Type inference

• Traits

• Package manager

• Pattern matching

• Macros

Page 28: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

Complexities of concurrent programming

• What thread owns the data?

• What threads can read the data?

• What threads can mutate the data?

Page 29: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

Message Passing

let (tx, rx) = mpsc::channel();

thread::spawn(move || { while let Ok(v) = rx.recv() { // Use var } });

let val = "hello".to_string(); tx.send(val);

// Compile error // println!("val={}", val);

Page 30: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

Fast, Reliable, Productive (pick three)

Page 31: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

Can the principles of Rust be applied to a networking library?

Page 32: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting
Page 33: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

Fastest, Safest

Page 34: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

Cancellation

Page 35: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

Backpressure

Page 36: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting
Page 37: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting
Page 38: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

Secret: Do no work

Page 39: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

let server = TcpListener::bind(&local_addr)?;

let server = sever.incoming().for_each(move |src| { let connection = TcpStream::connect(&remote_addr) .and_then(|move |dst| copy(src, dst));

// Run asynchronously in the background task::spawn(connection); Ok(()) });

task::spawn(server);

Page 40: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

Futures

Page 41: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

TcpStream::connect(&remote_addr, &handle) .and_then(move |dst| { … }) .and_then(|foo| { … })

Page 42: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

let fut_1 = copy(src_rd, dst_wr);

let fut_2 = copy(dst_rd, src_wr);

let fut_3 = fut_1.join(fut_2);

Page 43: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

Zero cost

Page 44: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

Epoll

• Non-blocking sockets

• Event queue

Page 45: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

let socket = bind(remote_addr); epoll.register(socket, token: 0); let clients = State::new();

for event in epoll.poll(): match event.token: 0 => while socket.is_ready(): let client = socket.accept(); let token = clients.store(client); epoll.register(client, token: token);

token => let client = clients[token]; process(client);

Page 46: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

1. Connect a socket

2. Send handshake message

3. Receive handshake message

4. Send request

5. Receive response

enum SocketState { Connecting, SendingHandshake, ReceivingHandshake, SendingRequest, ReceivingResponse, }

Page 47: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

Fast

• No runtime allocations

• No dynamic dispatch

• No copying / growing the stack

• No garbage collection

Page 48: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

After compilation, Tokio is equivalent.

Page 49: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

Fast

• No runtime allocations

• No dynamic dispatch

• No copying / growing the stack

• No garbage collection

Page 50: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

Pull, not push

Page 51: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

struct DrainTcpStream { socket: TcpStream, nread: u64, callback: Option<Box<Fn(u64)>>, }

ClosureAllocation

Page 52: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

ConsumerProducer

DrainTcpStream

Callback

Page 53: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

drain_socket .then(...) .then(...) .then(...)

Page 54: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

Producer

DrainTcpStream

Callback

Then

Callback

Page 55: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

Val

drain_socket Callback Callback Callback

Page 56: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

Val

drain_socket Callback Callback Callback

Page 57: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

Val

drain_socket Callback Callback Callback

Page 58: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

drain_socket Callback Callback Callback

Val

Page 59: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

drain_socket Callback Callback Callback

Val

Page 60: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

drain_socket Callback Callback Callback

Callback

Val

Page 61: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

Callbackdrain_socket Callback Callback

Val

Page 62: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

Callbackdrain_socket Callback Callback

Val

Val

Val

Page 63: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

struct DrainTcpStream { socket: TcpStream, nread: u64, }

Page 64: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

drain_socket .then(...) .then(...) .then(...)

Page 65: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

Then

Consumer

Then

DrainTcpStream

Page 66: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

fn poll(&mut self) -> Async<Bytes> { let mut buf = [0; 1024]; loop { match self.socket.read(&mut buf) { Ok(0) => return Async::Ready(self.nread), Ok(n) => self.nread += n, Err(e) => return Async::NotReady, } } }

Page 67: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

Poll

drain_socket Callback Callback Consumer

Page 68: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

Not Ready

drain_socket Callback Callback Consumer

Page 69: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

Poll

drain_socket Callback Callback Consumer

Page 70: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

drain_socket

Val

Callback Callback Consumer

Page 71: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

Ready:

drain_socket

Val

Callback Callback Consumer

Page 72: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

Callbackdrain_socket Callback Consumer

Val

Page 73: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

Not Ready

drain_socket

Val

Callback Callback Consumer

Page 74: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

Poll

drain_socket

Val

Callback Callback Consumer

Page 75: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

Ready:

drain_socket

Val

Callback Callback Consumer

Page 76: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

Cancellation is just drop

Page 77: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

enum Then<A, B, F> { First(A, F), Second(B), }

fn poll(&mut self) -> Async<B::Item> { loop { let fut_b = match *self { Then::First(ref mut fut_a, ref f) => { match fut_a.poll() { Async::Ready(v) => f(v), Async::NotReady => Async::NotReady, } } Then::Second(ref mut fut_b) => return fut_b.poll(), }

*self = Then::Second(fut_b); } }

Page 78: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

1. Connect a socket

2. Send handshake message

3. Receive handshake message

4. Send request

5. Receive response

TcpStream::connect(&remote_addr) .then(|sock| io::write(sock, handshake)) .then(|sock| io::read_exact(sock, 10)) .then(|(sock, handshake)| { validate(handshake); io::write(sock, request) }) .then(|sock| io::read_exact(sock, 10)) .then(|(sock, response)| { process(response) })

Page 79: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

1. Connect a socket

2. Send handshake message

3. Receive handshake message

4. Send request

5. Receive response

enum SocketState { Connecting, SendingHandshake, ReceivingHandshake, SendingRequest, ReceivingResponse, }

Page 80: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

Have your 🍰 and 🍽 it too.

Page 81: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

Tokio + Rust gets you speed and safety

Page 82: Next Gen Networking Infrastructure With Rust · – Software developer who accidentally shipped remote code execution vulnerabilities to millions of computers “Just use linting

Thanks!

https://tokio.rs https://linkerd.io