pion / dtls

DTLS 1.2 Server/Client implementation for Go
https://pion.ly/
MIT License
603 stars 160 forks source link

Support use of `net.PacketConn` in `dtls.Listener` #570

Closed hasheddan closed 1 year ago

hasheddan commented 1 year ago

Description

558 added support for DTLS 1.2 Connection IDs, but because the underlying routing mechanism in pion/transport/udp only accommodates associating packets to connection by the remote address, we are not yet able to actually receive packets on the connection with which the connection ID is associated. This patchset adds a new dtls/net/udp package that we consume by default in dtls.NewListener, which allows for customized routing. It closely resembles net/transport/udp, but allows for adding alternate IDs for a single connection.

This functionality is implemented through the use of two configuration options:

Note: this PR is currently in draft to demonstrate the new routing mechanism. Based on discussion on this PR, we may choose to implement some of this functionality in pion/transport rather than here. If we do move forward with the proposed functionality, we will also need to update packetio.Buffer to support sending the net.Addr alongside each packet (right now it is just hacked together by gob encoding / decoding the net.Addr with the associated packet data).

Reference issue

Fixes #568

Benchmarks

The same benchmarks, which are not necessarily the most realistic, are copied from pion/transport/packetio to pion/dtls/pkg/net. Running them on my local machine provided the following results.

pion/transport/packetio

go test ./packetio/... -bench=. -benchmem
goos: linux
goarch: amd64
pkg: github.com/pion/transport/v2/packetio
cpu: 12th Gen Intel(R) Core(TM) i7-12700H
BenchmarkBufferWR14-20          33276788            35.62 ns/op  393.01 MB/s           0 B/op          0 allocs/op
BenchmarkBufferWR140-20         25166851            41.77 ns/op 3352.02 MB/s           0 B/op          0 allocs/op
BenchmarkBufferWR1400-20        22657819            49.50 ns/op 28284.15 MB/s          0 B/op          0 allocs/op
BenchmarkBufferWWR14-20         33066428            36.71 ns/op  381.33 MB/s           0 B/op          0 allocs/op
BenchmarkBufferWWR140-20        26263891            42.61 ns/op 3285.74 MB/s           0 B/op          0 allocs/op
BenchmarkBufferWWR1400-20       18707992            62.39 ns/op 22439.13 MB/s          0 B/op          0 allocs/op
BenchmarkBuffer14-20            24692239            50.08 ns/op  279.57 MB/s           0 B/op          0 allocs/op
BenchmarkBuffer140-20           17865735            61.53 ns/op 2275.16 MB/s           1 B/op          0 allocs/op
BenchmarkBuffer1400-20           9494719           109.6 ns/op  12774.14 MB/s          2 B/op          0 allocs/op

pion/dtls/pkg/net

go test ./internal/net/... -bench=. -benchmem
goos: linux
goarch: amd64
pkg: github.com/pion/dtls/v2/internal/net
cpu: 12th Gen Intel(R) Core(TM) i7-12700H
BenchmarkBufferWR14-20          31277917            38.32 ns/op  365.33 MB/s           0 B/op          0 allocs/op
BenchmarkBufferWR140-20         25308450            45.09 ns/op 3105.11 MB/s           0 B/op          0 allocs/op
BenchmarkBufferWR1400-20        18748666            60.70 ns/op 23062.71 MB/s          0 B/op          0 allocs/op
BenchmarkBufferWWR14-20         30761960            36.54 ns/op  383.14 MB/s           0 B/op          0 allocs/op
BenchmarkBufferWWR140-20        25150832            44.51 ns/op 3145.05 MB/s           0 B/op          0 allocs/op
BenchmarkBufferWWR1400-20       18885183            62.52 ns/op 22391.09 MB/s          0 B/op          0 allocs/op
BenchmarkBuffer14-20             6214586           177.0 ns/op    79.08 MB/s         448 B/op          0 allocs/op
BenchmarkBuffer140-20            5931457           203.3 ns/op   688.79 MB/s         530 B/op          0 allocs/op
BenchmarkBuffer1400-20           3480960           433.2 ns/op  3231.97 MB/s        1313 B/op          0 allocs/op

We are clearly paying a performance penalty with the new buffer strategy. However, given that we are keeping this package internal and users can opt to keep using the existing udp / packetio packages if they don't care about clients being able to update remote address, this may be acceptable for the time-being with the knowledge that we can continue improving it without breaking users.

codecov[bot] commented 1 year ago

Codecov Report

Patch coverage: 84.39% and project coverage change: +0.53% :tada:

Comparison is base (f1d8b0a) 76.98% compared to head (29202df) 77.52%.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #570 +/- ## ========================================== + Coverage 76.98% 77.52% +0.53% ========================================== Files 99 101 +2 Lines 6018 6420 +402 ========================================== + Hits 4633 4977 +344 - Misses 1017 1066 +49 - Partials 368 377 +9 ``` | [Flag](https://app.codecov.io/gh/pion/dtls/pull/570/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion) | Coverage Δ | | |---|---|---| | [go](https://app.codecov.io/gh/pion/dtls/pull/570/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion) | `77.54% <84.39%> (+0.53%)` | :arrow_up: | | [wasm](https://app.codecov.io/gh/pion/dtls/pull/570/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion) | `62.90% <81.08%> (+0.84%)` | :arrow_up: | Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion#carryforward-flags-in-the-pull-request-comment) to find out more. | [Files Changed](https://app.codecov.io/gh/pion/dtls/pull/570?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion) | Coverage Δ | | |---|---|---| | [pkg/net/net.go](https://app.codecov.io/gh/pion/dtls/pull/570?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion#diff-cGtnL25ldC9uZXQuZ28=) | `50.00% <50.00%> (ø)` | | | [connection\_id.go](https://app.codecov.io/gh/pion/dtls/pull/570?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion#diff-Y29ubmVjdGlvbl9pZC5nbw==) | `81.48% <81.81%> (+1.48%)` | :arrow_up: | | [internal/net/udp/packet\_conn.go](https://app.codecov.io/gh/pion/dtls/pull/570?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion#diff-aW50ZXJuYWwvbmV0L3VkcC9wYWNrZXRfY29ubi5nbw==) | `85.43% <85.43%> (ø)` | | | [listener.go](https://app.codecov.io/gh/pion/dtls/pull/570?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion#diff-bGlzdGVuZXIuZ28=) | `51.11% <85.71%> (+4.76%)` | :arrow_up: | | [internal/net/buffer.go](https://app.codecov.io/gh/pion/dtls/pull/570?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion#diff-aW50ZXJuYWwvbmV0L2J1ZmZlci5nbw==) | `91.79% <91.79%> (ø)` | | ... and [3 files with indirect coverage changes](https://app.codecov.io/gh/pion/dtls/pull/570/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion)

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.