Top Banner
Motivation Kernel MBuf Packet Processing Socket Splicing Interface Implementation Applications Zero-Copy Socket Splicing Alexander Bluhm [email protected] Sunday, 29. September 2013
38

Zero-Copy Socket Splicing fileMotivation KernelMBuf PacketProcessing SocketSplicing Interface Implementation Applications Agenda 1 Motivation 2 Kernel MBuf 3 Packet Processing 4 Socket

Oct 09, 2019

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: Zero-Copy Socket Splicing fileMotivation KernelMBuf PacketProcessing SocketSplicing Interface Implementation Applications Agenda 1 Motivation 2 Kernel MBuf 3 Packet Processing 4 Socket

Motivation Kernel MBuf Packet Processing Socket Splicing Interface Implementation Applications

Zero-Copy Socket Splicing

Alexander Bluhm

[email protected]

Sunday, 29. September 2013

Page 2: Zero-Copy Socket Splicing fileMotivation KernelMBuf PacketProcessing SocketSplicing Interface Implementation Applications Agenda 1 Motivation 2 Kernel MBuf 3 Packet Processing 4 Socket

Motivation Kernel MBuf Packet Processing Socket Splicing Interface Implementation Applications

Agenda

1 Motivation

2 Kernel MBuf

3 Packet Processing

4 Socket Splicing

5 Interface

6 Implementation

7 Applications

Page 3: Zero-Copy Socket Splicing fileMotivation KernelMBuf PacketProcessing SocketSplicing Interface Implementation Applications Agenda 1 Motivation 2 Kernel MBuf 3 Packet Processing 4 Socket

Motivation Kernel MBuf Packet Processing Socket Splicing Interface Implementation Applications

Agenda

1 Motivation

2 Kernel MBuf

3 Packet Processing

4 Socket Splicing

5 Interface

6 Implementation

7 Applications

Page 4: Zero-Copy Socket Splicing fileMotivation KernelMBuf PacketProcessing SocketSplicing Interface Implementation Applications Agenda 1 Motivation 2 Kernel MBuf 3 Packet Processing 4 Socket

Motivation Kernel MBuf Packet Processing Socket Splicing Interface Implementation Applications

Application Level Gateway

Physical

Data Link

Network IP

TCP/UDP

Application

Packet Filter

Relay

Kernel

User LandSocket Splicing

Page 5: Zero-Copy Socket Splicing fileMotivation KernelMBuf PacketProcessing SocketSplicing Interface Implementation Applications Agenda 1 Motivation 2 Kernel MBuf 3 Packet Processing 4 Socket

Motivation Kernel MBuf Packet Processing Socket Splicing Interface Implementation Applications

Persistent HTTP Filtering

Body

content length

Header Body

content length

Header

copy copy copy filter copy copy filter

Page 6: Zero-Copy Socket Splicing fileMotivation KernelMBuf PacketProcessing SocketSplicing Interface Implementation Applications Agenda 1 Motivation 2 Kernel MBuf 3 Packet Processing 4 Socket

Motivation Kernel MBuf Packet Processing Socket Splicing Interface Implementation Applications

HTTP Socket Splicing

Body

splice length

Header

Body

splice length

Header

splice

filter

splice

filter

Kernel

User Land

Page 7: Zero-Copy Socket Splicing fileMotivation KernelMBuf PacketProcessing SocketSplicing Interface Implementation Applications Agenda 1 Motivation 2 Kernel MBuf 3 Packet Processing 4 Socket

Motivation Kernel MBuf Packet Processing Socket Splicing Interface Implementation Applications

Agenda

1 Motivation

2 Kernel MBuf

3 Packet Processing

4 Socket Splicing

5 Interface

6 Implementation

7 Applications

Page 8: Zero-Copy Socket Splicing fileMotivation KernelMBuf PacketProcessing SocketSplicing Interface Implementation Applications Agenda 1 Motivation 2 Kernel MBuf 3 Packet Processing 4 Socket

Motivation Kernel MBuf Packet Processing Socket Splicing Interface Implementation Applications

MBuf Data

mbufm hdrm datam lenm dat

ether headerip headerudp header

size 256

42size 236

size 42

Page 9: Zero-Copy Socket Splicing fileMotivation KernelMBuf PacketProcessing SocketSplicing Interface Implementation Applications Agenda 1 Motivation 2 Kernel MBuf 3 Packet Processing 4 Socket

Motivation Kernel MBuf Packet Processing Socket Splicing Interface Implementation Applications

MBuf Data Chaining

mbufm hdrm nextm datam lenm pkthdrlenm pktdat

ether headerip headerudp header

size 256

42

142size 196

size 42

mbufm hdrm nextm datam lenm dat

payload

size 256

NULL

100size 236

size 100

Page 10: Zero-Copy Socket Splicing fileMotivation KernelMBuf PacketProcessing SocketSplicing Interface Implementation Applications Agenda 1 Motivation 2 Kernel MBuf 3 Packet Processing 4 Socket

Motivation Kernel MBuf Packet Processing Socket Splicing Interface Implementation Applications

MBuf Packet Chaining

mbufm hdrm nextm nextpktm pkthdr

mbufm hdrm nextm nextpkt

mbufm hdrm nextm nextpkt

mbufm hdrm nextm nextpktm pkthdr

mbufm hdrm nextm nextpkt

Page 11: Zero-Copy Socket Splicing fileMotivation KernelMBuf PacketProcessing SocketSplicing Interface Implementation Applications Agenda 1 Motivation 2 Kernel MBuf 3 Packet Processing 4 Socket

Motivation Kernel MBuf Packet Processing Socket Splicing Interface Implementation Applications

MBuf Cluster

mbufm hdrm datam lenm pkthdrm extext bufext size

size 256

1400

2048

ether headerip headerudp headerpayload

size 2048

size 1400

Page 12: Zero-Copy Socket Splicing fileMotivation KernelMBuf PacketProcessing SocketSplicing Interface Implementation Applications Agenda 1 Motivation 2 Kernel MBuf 3 Packet Processing 4 Socket

Motivation Kernel MBuf Packet Processing Socket Splicing Interface Implementation Applications

MBuf Cluster Copy

mbufm dataext buf

mbufm dataext buf

ether headerip headerudp headerpayload

Page 13: Zero-Copy Socket Splicing fileMotivation KernelMBuf PacketProcessing SocketSplicing Interface Implementation Applications Agenda 1 Motivation 2 Kernel MBuf 3 Packet Processing 4 Socket

Motivation Kernel MBuf Packet Processing Socket Splicing Interface Implementation Applications

Agenda

1 Motivation

2 Kernel MBuf

3 Packet Processing

4 Socket Splicing

5 Interface

6 Implementation

7 Applications

Page 14: Zero-Copy Socket Splicing fileMotivation KernelMBuf PacketProcessing SocketSplicing Interface Implementation Applications Agenda 1 Motivation 2 Kernel MBuf 3 Packet Processing 4 Socket

Motivation Kernel MBuf Packet Processing Socket Splicing Interface Implementation Applications

Packet Input

network driver interrupt handler

ether input()

ip interface receive queue, m nextpkt

ip input()

inetsw[] internet protocol switch

tcp input()

socket receive buffer, m next

soreceive()

read()

Kernel

User Land

Page 15: Zero-Copy Socket Splicing fileMotivation KernelMBuf PacketProcessing SocketSplicing Interface Implementation Applications Agenda 1 Motivation 2 Kernel MBuf 3 Packet Processing 4 Socket

Motivation Kernel MBuf Packet Processing Socket Splicing Interface Implementation Applications

Packet Output

write()

sosend()

socket send buffer, m next

tcp output()

ip output()

ether output()

interface send queue, m nextpkt

if start()

network driver start routine

Kernel

User Land

Page 16: Zero-Copy Socket Splicing fileMotivation KernelMBuf PacketProcessing SocketSplicing Interface Implementation Applications Agenda 1 Motivation 2 Kernel MBuf 3 Packet Processing 4 Socket

Motivation Kernel MBuf Packet Processing Socket Splicing Interface Implementation Applications

Data Copy

tcp input()

so rcv

soreceive()

uiomove()

copyout()

read() write()

copyin()

uiomove()

sosend()

so snd

tcp output()

Relay

Page 17: Zero-Copy Socket Splicing fileMotivation KernelMBuf PacketProcessing SocketSplicing Interface Implementation Applications Agenda 1 Motivation 2 Kernel MBuf 3 Packet Processing 4 Socket

Motivation Kernel MBuf Packet Processing Socket Splicing Interface Implementation Applications

Process Wakeup

tcp input()

so rcv

soreceive()

struct socket

file descriptor

read() select() write()

sosend()

so snd

tcp output()

sorwakeup()

sowwakeup()

ACK

Page 18: Zero-Copy Socket Splicing fileMotivation KernelMBuf PacketProcessing SocketSplicing Interface Implementation Applications Agenda 1 Motivation 2 Kernel MBuf 3 Packet Processing 4 Socket

Motivation Kernel MBuf Packet Processing Socket Splicing Interface Implementation Applications

Agenda

1 Motivation

2 Kernel MBuf

3 Packet Processing

4 Socket Splicing

5 Interface

6 Implementation

7 Applications

Page 19: Zero-Copy Socket Splicing fileMotivation KernelMBuf PacketProcessing SocketSplicing Interface Implementation Applications Agenda 1 Motivation 2 Kernel MBuf 3 Packet Processing 4 Socket

Motivation Kernel MBuf Packet Processing Socket Splicing Interface Implementation Applications

Socket Splicing

tcp input()

so rcv

somove()

sosplice()

setsockopt(SO SPLICE)

so snd

tcp output()tcp input()

sorwakeup()

sowwakeup()

ACK

Page 20: Zero-Copy Socket Splicing fileMotivation KernelMBuf PacketProcessing SocketSplicing Interface Implementation Applications Agenda 1 Motivation 2 Kernel MBuf 3 Packet Processing 4 Socket

Motivation Kernel MBuf Packet Processing Socket Splicing Interface Implementation Applications

UDP Sockets

udp input()

so rcv

soreceive()

somove()

sosend()

udp output()

Page 21: Zero-Copy Socket Splicing fileMotivation KernelMBuf PacketProcessing SocketSplicing Interface Implementation Applications Agenda 1 Motivation 2 Kernel MBuf 3 Packet Processing 4 Socket

Motivation Kernel MBuf Packet Processing Socket Splicing Interface Implementation Applications

Layer

ipintrq

ip input()

tcp input()

so rcv

soreceive()

read() write()

sosend()

so snd

tcp output()

ip output()

if snd

Relaying

Forwarding

Socket Splicing

Page 22: Zero-Copy Socket Splicing fileMotivation KernelMBuf PacketProcessing SocketSplicing Interface Implementation Applications Agenda 1 Motivation 2 Kernel MBuf 3 Packet Processing 4 Socket

Motivation Kernel MBuf Packet Processing Socket Splicing Interface Implementation Applications

Agenda

1 Motivation

2 Kernel MBuf

3 Packet Processing

4 Socket Splicing

5 Interface

6 Implementation

7 Applications

Page 23: Zero-Copy Socket Splicing fileMotivation KernelMBuf PacketProcessing SocketSplicing Interface Implementation Applications Agenda 1 Motivation 2 Kernel MBuf 3 Packet Processing 4 Socket

Motivation Kernel MBuf Packet Processing Socket Splicing Interface Implementation Applications

Simple API

Begin splicing from source to drainsetsockopt(source fd, SO SPLICE, drain fd)

Stop splicingsetsockopt(source fd, SO SPLICE, -1)

Get spliced data lengthgetsockopt(source fd, SO SPLICE, &length)

Page 24: Zero-Copy Socket Splicing fileMotivation KernelMBuf PacketProcessing SocketSplicing Interface Implementation Applications Agenda 1 Motivation 2 Kernel MBuf 3 Packet Processing 4 Socket

Motivation Kernel MBuf Packet Processing Socket Splicing Interface Implementation Applications

Extended API

struct splice {

int sp_fd; /* drain */

off_t sp_max; /* maximum */

struct timeval sp_idle; /* timeout */

};

setsockopt(source fd, SO SPLICE, &splice)

Page 25: Zero-Copy Socket Splicing fileMotivation KernelMBuf PacketProcessing SocketSplicing Interface Implementation Applications Agenda 1 Motivation 2 Kernel MBuf 3 Packet Processing 4 Socket

Motivation Kernel MBuf Packet Processing Socket Splicing Interface Implementation Applications

Properties

Splicing is unidirectional

Invoke it twice for bidirectional splicing

Process can turn it on and off

Works for TCP and UDP

Can mix IPv4 and IPv6 sockets

Page 26: Zero-Copy Socket Splicing fileMotivation KernelMBuf PacketProcessing SocketSplicing Interface Implementation Applications Agenda 1 Motivation 2 Kernel MBuf 3 Packet Processing 4 Socket

Motivation Kernel MBuf Packet Processing Socket Splicing Interface Implementation Applications

Unsplice

Dissolve socket splicing manually

read(2) or select(2) from the source

EOF source socket shutdown

EPIPE drain socket error

EFBIG maximum data length

ETIMEDOUT idle timeout

Page 27: Zero-Copy Socket Splicing fileMotivation KernelMBuf PacketProcessing SocketSplicing Interface Implementation Applications Agenda 1 Motivation 2 Kernel MBuf 3 Packet Processing 4 Socket

Motivation Kernel MBuf Packet Processing Socket Splicing Interface Implementation Applications

Agenda

1 Motivation

2 Kernel MBuf

3 Packet Processing

4 Socket Splicing

5 Interface

6 Implementation

7 Applications

Page 28: Zero-Copy Socket Splicing fileMotivation KernelMBuf PacketProcessing SocketSplicing Interface Implementation Applications Agenda 1 Motivation 2 Kernel MBuf 3 Packet Processing 4 Socket

Motivation Kernel MBuf Packet Processing Socket Splicing Interface Implementation Applications

Struct Socket

struct socket {

...

struct socket *so_splice;

struct socket *so_spliceback;

off_t so_splicelen;

off_t so_splicemax;

struct timeval so_idletv;

struct timeout so_idleto;

...

};

Page 29: Zero-Copy Socket Splicing fileMotivation KernelMBuf PacketProcessing SocketSplicing Interface Implementation Applications Agenda 1 Motivation 2 Kernel MBuf 3 Packet Processing 4 Socket

Motivation Kernel MBuf Packet Processing Socket Splicing Interface Implementation Applications

sosplice(9)

Protocol must match

Sockets must be connected

Double link sockets

Move existing data

Page 30: Zero-Copy Socket Splicing fileMotivation KernelMBuf PacketProcessing SocketSplicing Interface Implementation Applications Agenda 1 Motivation 2 Kernel MBuf 3 Packet Processing 4 Socket

Motivation Kernel MBuf Packet Processing Socket Splicing Interface Implementation Applications

somove(9)

Check for errors

Check for space

Handle maximum

Handle out of band data

Move socket buffer data

Page 31: Zero-Copy Socket Splicing fileMotivation KernelMBuf PacketProcessing SocketSplicing Interface Implementation Applications Agenda 1 Motivation 2 Kernel MBuf 3 Packet Processing 4 Socket

Motivation Kernel MBuf Packet Processing Socket Splicing Interface Implementation Applications

sounsplice()

Manual unsplice

Cannot receive

Cannot send

Maximum

Timeout

Socket closed

Page 32: Zero-Copy Socket Splicing fileMotivation KernelMBuf PacketProcessing SocketSplicing Interface Implementation Applications Agenda 1 Motivation 2 Kernel MBuf 3 Packet Processing 4 Socket

Motivation Kernel MBuf Packet Processing Socket Splicing Interface Implementation Applications

sorwakeup() sowwakeup()

Called from tcp input()

Source calls sorwakeup()

Drain calls sowwakeup()

Both invoke somove(9)

Page 33: Zero-Copy Socket Splicing fileMotivation KernelMBuf PacketProcessing SocketSplicing Interface Implementation Applications Agenda 1 Motivation 2 Kernel MBuf 3 Packet Processing 4 Socket

Motivation Kernel MBuf Packet Processing Socket Splicing Interface Implementation Applications

Agenda

1 Motivation

2 Kernel MBuf

3 Packet Processing

4 Socket Splicing

5 Interface

6 Implementation

7 Applications

Page 34: Zero-Copy Socket Splicing fileMotivation KernelMBuf PacketProcessing SocketSplicing Interface Implementation Applications Agenda 1 Motivation 2 Kernel MBuf 3 Packet Processing 4 Socket

Motivation Kernel MBuf Packet Processing Socket Splicing Interface Implementation Applications

Relayd

Plain TCP connections

HTTP connections

Filter persistent HTTP

HTTP Chunking

Page 35: Zero-Copy Socket Splicing fileMotivation KernelMBuf PacketProcessing SocketSplicing Interface Implementation Applications Agenda 1 Motivation 2 Kernel MBuf 3 Packet Processing 4 Socket

Motivation Kernel MBuf Packet Processing Socket Splicing Interface Implementation Applications

Tests

/usr/src/regress/sys/kern/sosplice/

15 API tests

18 UDP tests

76 TCP tests

perf/relay.c simple example

BSD::Socket::Splice Perl API

28 relayd tests

Page 36: Zero-Copy Socket Splicing fileMotivation KernelMBuf PacketProcessing SocketSplicing Interface Implementation Applications Agenda 1 Motivation 2 Kernel MBuf 3 Packet Processing 4 Socket

Motivation Kernel MBuf Packet Processing Socket Splicing Interface Implementation Applications

Performance

Factor 1 or 2 for TCP

Factor 6 or 8 for UDP

Page 37: Zero-Copy Socket Splicing fileMotivation KernelMBuf PacketProcessing SocketSplicing Interface Implementation Applications Agenda 1 Motivation 2 Kernel MBuf 3 Packet Processing 4 Socket

Motivation Kernel MBuf Packet Processing Socket Splicing Interface Implementation Applications

Documentation

Manpage setsockopt(2) SO SPLICE

Manpage sosplice(9) somove(9)

Page 38: Zero-Copy Socket Splicing fileMotivation KernelMBuf PacketProcessing SocketSplicing Interface Implementation Applications Agenda 1 Motivation 2 Kernel MBuf 3 Packet Processing 4 Socket

Motivation Kernel MBuf Packet Processing Socket Splicing Interface Implementation Applications

Questions

?