From 0c2f201bd068a3cc7f7b62fa82d5d3eeec7a67c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Thu, 3 Apr 2025 00:00:21 +0900 Subject: [PATCH] fix: multi-segment hash checking --- README.md | 192 +++++++++++++-------------- go.mod | 2 +- go.sum | 4 +- gold/head/box.go | 15 ++- gold/head/builder.go | 16 +-- gold/head/packet.go | 22 +-- gold/head/unbox.go | 23 ++-- gold/link/crypto.go | 18 ++- gold/link/listen.go | 15 ++- gold/link/me.go | 4 +- gold/link/recv.go | 14 +- gold/link/send.go | 17 ++- internal/algo/hash.go | 3 +- upper/services/tunnel/tunnel.go | 5 +- upper/services/tunnel/tunnel_test.go | 86 ++++-------- 15 files changed, 227 insertions(+), 209 deletions(-) diff --git a/README.md b/README.md index 830b1fe..0a2c6b4 100644 --- a/README.md +++ b/README.md @@ -77,30 +77,30 @@ goos: darwin goarch: arm64 pkg: github.com/fumiama/WireGold/upper/services/tunnel cpu: Apple M1 -BenchmarkTunnelUDP/1024-plain-nob14-8 4753 250629 ns/op 4.09 MB/s 3570503 B/op 117 allocs/op -BenchmarkTunnelUDP/1024-normal-nob14-8 4473 261136 ns/op 3.92 MB/s 3570565 B/op 118 allocs/op -BenchmarkTunnelUDP/1024-plain-b14-8 4250 275495 ns/op 3.72 MB/s 3575369 B/op 121 allocs/op -BenchmarkTunnelUDP/1024-normal-b14-8 4204 278062 ns/op 3.68 MB/s 3575844 B/op 122 allocs/op -BenchmarkTunnelUDP/1024-preshared-nob14-8 4196 282707 ns/op 3.62 MB/s 3570427 B/op 118 allocs/op -BenchmarkTunnelUDP/1024-preshared-b14-8 4130 290609 ns/op 3.52 MB/s 3575814 B/op 121 allocs/op -BenchmarkTunnelUDP/2048-plain-nob14-8 4162 280754 ns/op 7.29 MB/s 3578825 B/op 117 allocs/op -BenchmarkTunnelUDP/2048-normal-nob14-8 4088 292733 ns/op 7.00 MB/s 3578858 B/op 118 allocs/op -BenchmarkTunnelUDP/2048-plain-b14-8 4023 290264 ns/op 7.06 MB/s 3589002 B/op 121 allocs/op -BenchmarkTunnelUDP/2048-normal-b14-8 3904 298841 ns/op 6.85 MB/s 3589116 B/op 122 allocs/op -BenchmarkTunnelUDP/2048-preshared-nob14-8 3861 285661 ns/op 7.17 MB/s 3578690 B/op 117 allocs/op -BenchmarkTunnelUDP/2048-preshared-b14-8 4344 273054 ns/op 7.50 MB/s 3589101 B/op 122 allocs/op -BenchmarkTunnelUDP/3072-plain-nob14-8 4293 273633 ns/op 11.23 MB/s 3582928 B/op 121 allocs/op -BenchmarkTunnelUDP/3072-normal-nob14-8 4086 287291 ns/op 10.69 MB/s 3583011 B/op 122 allocs/op -BenchmarkTunnelUDP/3072-plain-b14-8 4144 295141 ns/op 10.41 MB/s 3600854 B/op 128 allocs/op -BenchmarkTunnelUDP/3072-normal-b14-8 3759 310645 ns/op 9.89 MB/s 3601217 B/op 130 allocs/op -BenchmarkTunnelUDP/3072-preshared-nob14-8 3805 305162 ns/op 10.07 MB/s 3583013 B/op 122 allocs/op -BenchmarkTunnelUDP/3072-preshared-b14-8 3574 320329 ns/op 9.59 MB/s 3601409 B/op 130 allocs/op -BenchmarkTunnelUDP/4096-plain-nob14-8 2448 482246 ns/op 8.49 MB/s 7157679 B/op 241 allocs/op -BenchmarkTunnelUDP/4096-normal-nob14-8 2328 508454 ns/op 8.06 MB/s 7158430 B/op 244 allocs/op -BenchmarkTunnelUDP/4096-plain-b14-8 2290 527956 ns/op 7.76 MB/s 7181296 B/op 254 allocs/op -BenchmarkTunnelUDP/4096-normal-b14-8 2276 528681 ns/op 7.75 MB/s 7181960 B/op 256 allocs/op -BenchmarkTunnelUDP/4096-preshared-nob14-8 2284 515491 ns/op 7.95 MB/s 7158606 B/op 243 allocs/op -BenchmarkTunnelUDP/4096-preshared-b14-8 2026 560506 ns/op 7.31 MB/s 7181980 B/op 256 allocs/op +BenchmarkTunnelUDP/1024-plain-nob14-8 4938 228283 ns/op 4.49 MB/s 3642671 B/op 149 allocs/op +BenchmarkTunnelUDP/1024-normal-nob14-8 5100 234118 ns/op 4.37 MB/s 3642409 B/op 147 allocs/op +BenchmarkTunnelUDP/1024-plain-b14-8 4528 249429 ns/op 4.11 MB/s 3825461 B/op 179 allocs/op +BenchmarkTunnelUDP/1024-normal-b14-8 4885 242048 ns/op 4.23 MB/s 3818262 B/op 175 allocs/op +BenchmarkTunnelUDP/1024-preshared-nob14-8 4833 242460 ns/op 4.22 MB/s 3632537 B/op 144 allocs/op +BenchmarkTunnelUDP/1024-preshared-b14-8 4348 239630 ns/op 4.27 MB/s 3820118 B/op 174 allocs/op +BenchmarkTunnelUDP/2048-plain-nob14-8 4766 280419 ns/op 7.30 MB/s 3656588 B/op 148 allocs/op +BenchmarkTunnelUDP/2048-normal-nob14-8 4353 250150 ns/op 8.19 MB/s 3639053 B/op 145 allocs/op +BenchmarkTunnelUDP/2048-plain-b14-8 4136 278223 ns/op 7.36 MB/s 3848032 B/op 178 allocs/op +BenchmarkTunnelUDP/2048-normal-b14-8 4264 268694 ns/op 7.62 MB/s 3842609 B/op 176 allocs/op +BenchmarkTunnelUDP/2048-preshared-nob14-8 4154 262575 ns/op 7.80 MB/s 3640443 B/op 144 allocs/op +BenchmarkTunnelUDP/2048-preshared-b14-8 3932 287082 ns/op 7.13 MB/s 3846167 B/op 176 allocs/op +BenchmarkTunnelUDP/3072-plain-nob14-8 4006 267281 ns/op 11.49 MB/s 3690985 B/op 164 allocs/op +BenchmarkTunnelUDP/3072-normal-nob14-8 3942 271832 ns/op 11.30 MB/s 3670827 B/op 162 allocs/op +BenchmarkTunnelUDP/3072-plain-b14-8 3529 291120 ns/op 10.55 MB/s 3993371 B/op 211 allocs/op +BenchmarkTunnelUDP/3072-normal-b14-8 3614 298778 ns/op 10.28 MB/s 3994267 B/op 211 allocs/op +BenchmarkTunnelUDP/3072-preshared-nob14-8 4036 297819 ns/op 10.31 MB/s 3674026 B/op 162 allocs/op +BenchmarkTunnelUDP/3072-preshared-b14-8 3705 300820 ns/op 10.21 MB/s 3989965 B/op 210 allocs/op +BenchmarkTunnelUDP/4096-plain-nob14-8 2604 398308 ns/op 10.28 MB/s 7389986 B/op 320 allocs/op +BenchmarkTunnelUDP/4096-normal-nob14-8 2744 399739 ns/op 10.25 MB/s 7348911 B/op 316 allocs/op +BenchmarkTunnelUDP/4096-plain-b14-8 2788 430813 ns/op 9.51 MB/s 7965100 B/op 410 allocs/op +BenchmarkTunnelUDP/4096-normal-b14-8 2620 432984 ns/op 9.46 MB/s 7957374 B/op 407 allocs/op +BenchmarkTunnelUDP/4096-preshared-nob14-8 2750 395736 ns/op 10.35 MB/s 7348747 B/op 315 allocs/op +BenchmarkTunnelUDP/4096-preshared-b14-8 2628 431785 ns/op 9.49 MB/s 7961597 B/op 407 allocs/op ``` ### UDP MTU 1024 ```bash @@ -108,30 +108,30 @@ goos: darwin goarch: arm64 pkg: github.com/fumiama/WireGold/upper/services/tunnel cpu: Apple M1 -BenchmarkTunnelUDPSmallMTU/1024-plain-nob14-8 3766 326631 ns/op 3.14 MB/s 3568299 B/op 126 allocs/op -BenchmarkTunnelUDPSmallMTU/1024-normal-nob14-8 3728 319552 ns/op 3.20 MB/s 3568454 B/op 128 allocs/op -BenchmarkTunnelUDPSmallMTU/1024-plain-b14-8 3625 323816 ns/op 3.16 MB/s 3575638 B/op 137 allocs/op -BenchmarkTunnelUDPSmallMTU/1024-normal-b14-8 3446 333510 ns/op 3.07 MB/s 3575925 B/op 138 allocs/op -BenchmarkTunnelUDPSmallMTU/1024-preshared-nob14-8 3537 335220 ns/op 3.05 MB/s 3568481 B/op 128 allocs/op -BenchmarkTunnelUDPSmallMTU/1024-preshared-b14-8 3486 337967 ns/op 3.03 MB/s 3575890 B/op 138 allocs/op -BenchmarkTunnelUDPSmallMTU/2048-plain-nob14-8 3303 349278 ns/op 5.86 MB/s 3592804 B/op 140 allocs/op -BenchmarkTunnelUDPSmallMTU/2048-normal-nob14-8 3331 376226 ns/op 5.44 MB/s 3593065 B/op 142 allocs/op -BenchmarkTunnelUDPSmallMTU/2048-plain-b14-8 3116 421630 ns/op 4.86 MB/s 3605117 B/op 157 allocs/op -BenchmarkTunnelUDPSmallMTU/2048-normal-b14-8 2634 381676 ns/op 5.37 MB/s 3606455 B/op 158 allocs/op -BenchmarkTunnelUDPSmallMTU/2048-preshared-nob14-8 3138 391760 ns/op 5.23 MB/s 3591788 B/op 142 allocs/op -BenchmarkTunnelUDPSmallMTU/2048-preshared-b14-8 2959 391663 ns/op 5.23 MB/s 3605364 B/op 158 allocs/op -BenchmarkTunnelUDPSmallMTU/3072-plain-nob14-8 3046 421705 ns/op 7.28 MB/s 3620443 B/op 156 allocs/op -BenchmarkTunnelUDPSmallMTU/3072-normal-nob14-8 3001 413043 ns/op 7.44 MB/s 3631990 B/op 157 allocs/op -BenchmarkTunnelUDPSmallMTU/3072-plain-b14-8 2503 406906 ns/op 7.55 MB/s 3640574 B/op 177 allocs/op -BenchmarkTunnelUDPSmallMTU/3072-normal-b14-8 2776 416946 ns/op 7.37 MB/s 3643066 B/op 179 allocs/op -BenchmarkTunnelUDPSmallMTU/3072-preshared-nob14-8 2947 422378 ns/op 7.27 MB/s 3626004 B/op 157 allocs/op -BenchmarkTunnelUDPSmallMTU/3072-preshared-b14-8 2547 459951 ns/op 6.68 MB/s 3648033 B/op 179 allocs/op -BenchmarkTunnelUDPSmallMTU/4096-plain-nob14-8 1776 628904 ns/op 6.51 MB/s 7232490 B/op 285 allocs/op -BenchmarkTunnelUDPSmallMTU/4096-normal-nob14-8 1782 643967 ns/op 6.36 MB/s 7238574 B/op 288 allocs/op -BenchmarkTunnelUDPSmallMTU/4096-plain-b14-8 1549 674359 ns/op 6.07 MB/s 7262233 B/op 317 allocs/op -BenchmarkTunnelUDPSmallMTU/4096-normal-b14-8 1826 690961 ns/op 5.93 MB/s 7260027 B/op 319 allocs/op -BenchmarkTunnelUDPSmallMTU/4096-preshared-nob14-8 1868 649732 ns/op 6.30 MB/s 7242787 B/op 288 allocs/op -BenchmarkTunnelUDPSmallMTU/4096-preshared-b14-8 1654 682244 ns/op 6.00 MB/s 7255985 B/op 318 allocs/op +BenchmarkTunnelUDPSmallMTU/1024-plain-nob14-8 4770 256794 ns/op 3.99 MB/s 3715458 B/op 193 allocs/op +BenchmarkTunnelUDPSmallMTU/1024-normal-nob14-8 4945 242538 ns/op 4.22 MB/s 3681420 B/op 188 allocs/op +BenchmarkTunnelUDPSmallMTU/1024-plain-b14-8 4137 269202 ns/op 3.80 MB/s 4101089 B/op 254 allocs/op +BenchmarkTunnelUDPSmallMTU/1024-normal-b14-8 4592 253461 ns/op 4.04 MB/s 4109262 B/op 253 allocs/op +BenchmarkTunnelUDPSmallMTU/1024-preshared-nob14-8 4764 243752 ns/op 4.20 MB/s 3675691 B/op 186 allocs/op +BenchmarkTunnelUDPSmallMTU/1024-preshared-b14-8 4086 282682 ns/op 3.62 MB/s 4107240 B/op 253 allocs/op +BenchmarkTunnelUDPSmallMTU/2048-plain-nob14-8 4728 252759 ns/op 8.10 MB/s 3762231 B/op 234 allocs/op +BenchmarkTunnelUDPSmallMTU/2048-normal-nob14-8 4245 257036 ns/op 7.97 MB/s 3729842 B/op 232 allocs/op +BenchmarkTunnelUDPSmallMTU/2048-plain-b14-8 3615 308642 ns/op 6.64 MB/s 4469625 B/op 342 allocs/op +BenchmarkTunnelUDPSmallMTU/2048-normal-b14-8 3624 311780 ns/op 6.57 MB/s 4487346 B/op 345 allocs/op +BenchmarkTunnelUDPSmallMTU/2048-preshared-nob14-8 3999 260043 ns/op 7.88 MB/s 3723444 B/op 231 allocs/op +BenchmarkTunnelUDPSmallMTU/2048-preshared-b14-8 3558 315744 ns/op 6.49 MB/s 4476565 B/op 343 allocs/op +BenchmarkTunnelUDPSmallMTU/3072-plain-nob14-8 3814 265654 ns/op 11.56 MB/s 3802900 B/op 280 allocs/op +BenchmarkTunnelUDPSmallMTU/3072-normal-nob14-8 4380 291992 ns/op 10.52 MB/s 3760254 B/op 276 allocs/op +BenchmarkTunnelUDPSmallMTU/3072-plain-b14-8 3340 338760 ns/op 9.07 MB/s 4849826 B/op 434 allocs/op +BenchmarkTunnelUDPSmallMTU/3072-normal-b14-8 3302 345620 ns/op 8.89 MB/s 4852322 B/op 434 allocs/op +BenchmarkTunnelUDPSmallMTU/3072-preshared-nob14-8 4424 265290 ns/op 11.58 MB/s 3761816 B/op 277 allocs/op +BenchmarkTunnelUDPSmallMTU/3072-preshared-b14-8 3148 344490 ns/op 8.92 MB/s 4849613 B/op 434 allocs/op +BenchmarkTunnelUDPSmallMTU/4096-plain-nob14-8 2586 399489 ns/op 10.25 MB/s 7570823 B/op 467 allocs/op +BenchmarkTunnelUDPSmallMTU/4096-normal-nob14-8 2576 402297 ns/op 10.18 MB/s 7504731 B/op 464 allocs/op +BenchmarkTunnelUDPSmallMTU/4096-plain-b14-8 2240 484812 ns/op 8.45 MB/s 9081331 B/op 696 allocs/op +BenchmarkTunnelUDPSmallMTU/4096-normal-b14-8 2240 504749 ns/op 8.11 MB/s 9069168 B/op 693 allocs/op +BenchmarkTunnelUDPSmallMTU/4096-preshared-nob14-8 2594 392716 ns/op 10.43 MB/s 7480678 B/op 460 allocs/op +BenchmarkTunnelUDPSmallMTU/4096-preshared-b14-8 2234 506134 ns/op 8.09 MB/s 9066223 B/op 691 allocs/op ``` ### TCP MTU 4096 ```bash @@ -139,30 +139,30 @@ goos: darwin goarch: arm64 pkg: github.com/fumiama/WireGold/upper/services/tunnel cpu: Apple M1 -BenchmarkTunnelTCP/1024-plain-nob14-8 2323 459188 ns/op 2.23 MB/s 3576540 B/op 166 allocs/op -BenchmarkTunnelTCP/1024-normal-nob14-8 2472 438347 ns/op 2.34 MB/s 3576692 B/op 168 allocs/op -BenchmarkTunnelTCP/1024-plain-b14-8 2728 418395 ns/op 2.45 MB/s 3583603 B/op 171 allocs/op -BenchmarkTunnelTCP/1024-normal-b14-8 2668 463060 ns/op 2.21 MB/s 3584519 B/op 172 allocs/op -BenchmarkTunnelTCP/1024-preshared-nob14-8 2660 454945 ns/op 2.25 MB/s 3576708 B/op 168 allocs/op -BenchmarkTunnelTCP/1024-preshared-b14-8 2690 437373 ns/op 2.34 MB/s 3584515 B/op 172 allocs/op -BenchmarkTunnelTCP/2048-plain-nob14-8 2580 455416 ns/op 4.50 MB/s 3590368 B/op 168 allocs/op -BenchmarkTunnelTCP/2048-normal-nob14-8 2294 458178 ns/op 4.47 MB/s 3590512 B/op 171 allocs/op -BenchmarkTunnelTCP/2048-plain-b14-8 2414 462412 ns/op 4.43 MB/s 3605344 B/op 174 allocs/op -BenchmarkTunnelTCP/2048-normal-b14-8 2511 508527 ns/op 4.03 MB/s 3605658 B/op 177 allocs/op -BenchmarkTunnelTCP/2048-preshared-nob14-8 2433 482086 ns/op 4.25 MB/s 3590571 B/op 171 allocs/op -BenchmarkTunnelTCP/2048-preshared-b14-8 2361 494409 ns/op 4.14 MB/s 3605739 B/op 177 allocs/op -BenchmarkTunnelTCP/3072-plain-nob14-8 2487 498395 ns/op 6.16 MB/s 3600311 B/op 199 allocs/op -BenchmarkTunnelTCP/3072-normal-nob14-8 2170 542424 ns/op 5.66 MB/s 3600596 B/op 202 allocs/op -BenchmarkTunnelTCP/3072-plain-b14-8 2259 524854 ns/op 5.85 MB/s 3621274 B/op 205 allocs/op -BenchmarkTunnelTCP/3072-normal-b14-8 2307 537656 ns/op 5.71 MB/s 3621514 B/op 209 allocs/op -BenchmarkTunnelTCP/3072-preshared-nob14-8 1855 545493 ns/op 5.63 MB/s 3600418 B/op 201 allocs/op -BenchmarkTunnelTCP/3072-preshared-b14-8 2198 535328 ns/op 5.74 MB/s 3621536 B/op 208 allocs/op -BenchmarkTunnelTCP/4096-plain-nob14-8 2043 587272 ns/op 6.97 MB/s 7181814 B/op 391 allocs/op -BenchmarkTunnelTCP/4096-normal-nob14-8 1832 609909 ns/op 6.72 MB/s 7182940 B/op 394 allocs/op -BenchmarkTunnelTCP/4096-plain-b14-8 2044 572149 ns/op 7.16 MB/s 7209279 B/op 405 allocs/op -BenchmarkTunnelTCP/4096-normal-b14-8 2019 655180 ns/op 6.25 MB/s 7210261 B/op 409 allocs/op -BenchmarkTunnelTCP/4096-preshared-nob14-8 1652 636402 ns/op 6.44 MB/s 7182914 B/op 394 allocs/op -BenchmarkTunnelTCP/4096-preshared-b14-8 1885 624237 ns/op 6.56 MB/s 7210327 B/op 408 allocs/op +BenchmarkTunnelTCP/1024-plain-nob14-8 4627 246837 ns/op 4.15 MB/s 3684040 B/op 201 allocs/op +BenchmarkTunnelTCP/1024-normal-nob14-8 4833 257150 ns/op 3.98 MB/s 3682260 B/op 199 allocs/op +BenchmarkTunnelTCP/1024-plain-b14-8 4396 272838 ns/op 3.75 MB/s 3850134 B/op 231 allocs/op +BenchmarkTunnelTCP/1024-normal-b14-8 4104 252293 ns/op 4.06 MB/s 3844674 B/op 226 allocs/op +BenchmarkTunnelTCP/1024-preshared-nob14-8 4530 264767 ns/op 3.87 MB/s 3680243 B/op 197 allocs/op +BenchmarkTunnelTCP/1024-preshared-b14-8 4231 287111 ns/op 3.57 MB/s 3847164 B/op 227 allocs/op +BenchmarkTunnelTCP/2048-plain-nob14-8 4275 276425 ns/op 7.41 MB/s 3698728 B/op 200 allocs/op +BenchmarkTunnelTCP/2048-normal-nob14-8 4033 261234 ns/op 7.84 MB/s 3701433 B/op 200 allocs/op +BenchmarkTunnelTCP/2048-plain-b14-8 3680 303246 ns/op 6.75 MB/s 3875541 B/op 231 allocs/op +BenchmarkTunnelTCP/2048-normal-b14-8 3626 288219 ns/op 7.11 MB/s 3878505 B/op 230 allocs/op +BenchmarkTunnelTCP/2048-preshared-nob14-8 3868 287679 ns/op 7.12 MB/s 3696931 B/op 200 allocs/op +BenchmarkTunnelTCP/2048-preshared-b14-8 3586 305008 ns/op 6.71 MB/s 3878416 B/op 230 allocs/op +BenchmarkTunnelTCP/3072-plain-nob14-8 3666 298452 ns/op 10.29 MB/s 3767509 B/op 246 allocs/op +BenchmarkTunnelTCP/3072-normal-nob14-8 3450 304848 ns/op 10.08 MB/s 3761811 B/op 246 allocs/op +BenchmarkTunnelTCP/3072-plain-b14-8 3549 315641 ns/op 9.73 MB/s 4032830 B/op 291 allocs/op +BenchmarkTunnelTCP/3072-normal-b14-8 3440 327234 ns/op 9.39 MB/s 4038470 B/op 292 allocs/op +BenchmarkTunnelTCP/3072-preshared-nob14-8 3522 302663 ns/op 10.15 MB/s 3760304 B/op 245 allocs/op +BenchmarkTunnelTCP/3072-preshared-b14-8 3390 326384 ns/op 9.41 MB/s 4040489 B/op 293 allocs/op +BenchmarkTunnelTCP/4096-plain-nob14-8 2431 435457 ns/op 9.41 MB/s 7515476 B/op 480 allocs/op +BenchmarkTunnelTCP/4096-normal-nob14-8 2500 433178 ns/op 9.46 MB/s 7511114 B/op 478 allocs/op +BenchmarkTunnelTCP/4096-plain-b14-8 2337 457177 ns/op 8.96 MB/s 8033760 B/op 568 allocs/op +BenchmarkTunnelTCP/4096-normal-b14-8 2374 465704 ns/op 8.80 MB/s 8040812 B/op 567 allocs/op +BenchmarkTunnelTCP/4096-preshared-nob14-8 2532 436310 ns/op 9.39 MB/s 7510565 B/op 477 allocs/op +BenchmarkTunnelTCP/4096-preshared-b14-8 2360 459261 ns/op 8.92 MB/s 8037878 B/op 566 allocs/op ``` ### TCP MTU 1024 ```bash @@ -170,28 +170,28 @@ goos: darwin goarch: arm64 pkg: github.com/fumiama/WireGold/upper/services/tunnel cpu: Apple M1 -BenchmarkTunnelTCPSmallMTU/1024-plain-nob14-8 2061 582289 ns/op 1.76 MB/s 3577539 B/op 234 allocs/op -BenchmarkTunnelTCPSmallMTU/1024-normal-nob14-8 2172 561002 ns/op 1.83 MB/s 3577725 B/op 237 allocs/op -BenchmarkTunnelTCPSmallMTU/1024-plain-b14-8 2002 625224 ns/op 1.64 MB/s 3584694 B/op 244 allocs/op -BenchmarkTunnelTCPSmallMTU/1024-normal-b14-8 1957 590091 ns/op 1.74 MB/s 3585060 B/op 247 allocs/op -BenchmarkTunnelTCPSmallMTU/1024-preshared-nob14-8 2127 552614 ns/op 1.85 MB/s 3577669 B/op 236 allocs/op -BenchmarkTunnelTCPSmallMTU/1024-preshared-b14-8 2084 602128 ns/op 1.70 MB/s 3585057 B/op 247 allocs/op -BenchmarkTunnelTCPSmallMTU/2048-plain-nob14-8 1899 595303 ns/op 3.44 MB/s 3596277 B/op 320 allocs/op -BenchmarkTunnelTCPSmallMTU/2048-normal-nob14-8 1656 604450 ns/op 3.39 MB/s 3596115 B/op 323 allocs/op -BenchmarkTunnelTCPSmallMTU/2048-plain-b14-8 1729 624733 ns/op 3.28 MB/s 3610414 B/op 339 allocs/op -BenchmarkTunnelTCPSmallMTU/2048-normal-b14-8 1568 653317 ns/op 3.13 MB/s 3611234 B/op 342 allocs/op -BenchmarkTunnelTCPSmallMTU/2048-preshared-nob14-8 1858 664597 ns/op 3.08 MB/s 3595764 B/op 322 allocs/op -BenchmarkTunnelTCPSmallMTU/2048-preshared-b14-8 1404 767077 ns/op 2.67 MB/s 3609789 B/op 339 allocs/op -BenchmarkTunnelTCPSmallMTU/3072-plain-nob14-8 1761 846583 ns/op 3.63 MB/s 3614569 B/op 410 allocs/op -BenchmarkTunnelTCPSmallMTU/3072-normal-nob14-8 1887 743407 ns/op 4.13 MB/s 3612869 B/op 411 allocs/op -BenchmarkTunnelTCPSmallMTU/3072-plain-b14-8 1582 679431 ns/op 4.52 MB/s 3639650 B/op 435 allocs/op -BenchmarkTunnelTCPSmallMTU/3072-normal-b14-8 1688 720574 ns/op 4.26 MB/s 3634744 B/op 435 allocs/op -BenchmarkTunnelTCPSmallMTU/3072-preshared-nob14-8 1762 731901 ns/op 4.20 MB/s 3616570 B/op 414 allocs/op -BenchmarkTunnelTCPSmallMTU/3072-preshared-b14-8 1656 716281 ns/op 4.29 MB/s 3636078 B/op 434 allocs/op -BenchmarkTunnelTCPSmallMTU/4096-plain-nob14-8 1482 847378 ns/op 4.83 MB/s 7214173 B/op 666 allocs/op -BenchmarkTunnelTCPSmallMTU/4096-normal-nob14-8 1354 818199 ns/op 5.01 MB/s 7219760 B/op 665 allocs/op -BenchmarkTunnelTCPSmallMTU/4096-plain-b14-8 1557 784260 ns/op 5.22 MB/s 7243407 B/op 697 allocs/op -BenchmarkTunnelTCPSmallMTU/4096-normal-b14-8 1316 811760 ns/op 5.05 MB/s 7241275 B/op 699 allocs/op -BenchmarkTunnelTCPSmallMTU/4096-preshared-nob14-8 1299 806369 ns/op 5.08 MB/s 7216648 B/op 666 allocs/op -BenchmarkTunnelTCPSmallMTU/4096-preshared-b14-8 1278 858201 ns/op 4.77 MB/s 7242324 B/op 703 allocs/op +BenchmarkTunnelTCPSmallMTU/1024-plain-nob14-8 3318 312084 ns/op 3.28 MB/s 3797015 B/op 307 allocs/op +BenchmarkTunnelTCPSmallMTU/1024-normal-nob14-8 4102 303641 ns/op 3.37 MB/s 3795618 B/op 308 allocs/op +BenchmarkTunnelTCPSmallMTU/1024-plain-b14-8 3746 314102 ns/op 3.26 MB/s 4147318 B/op 368 allocs/op +BenchmarkTunnelTCPSmallMTU/1024-normal-b14-8 3609 315252 ns/op 3.25 MB/s 4152014 B/op 368 allocs/op +BenchmarkTunnelTCPSmallMTU/1024-preshared-nob14-8 3826 300693 ns/op 3.41 MB/s 3793725 B/op 304 allocs/op +BenchmarkTunnelTCPSmallMTU/1024-preshared-b14-8 3628 327852 ns/op 3.12 MB/s 4150869 B/op 367 allocs/op +BenchmarkTunnelTCPSmallMTU/2048-plain-nob14-8 3553 315709 ns/op 6.49 MB/s 3945193 B/op 426 allocs/op +BenchmarkTunnelTCPSmallMTU/2048-normal-nob14-8 3254 329794 ns/op 6.21 MB/s 3933224 B/op 427 allocs/op +BenchmarkTunnelTCPSmallMTU/2048-plain-b14-8 3222 357250 ns/op 5.73 MB/s 4538189 B/op 529 allocs/op +BenchmarkTunnelTCPSmallMTU/2048-normal-b14-8 3080 359401 ns/op 5.70 MB/s 4555108 B/op 535 allocs/op +BenchmarkTunnelTCPSmallMTU/2048-preshared-nob14-8 3463 320078 ns/op 6.40 MB/s 3936771 B/op 426 allocs/op +BenchmarkTunnelTCPSmallMTU/2048-preshared-b14-8 2990 363645 ns/op 5.63 MB/s 4555897 B/op 535 allocs/op +BenchmarkTunnelTCPSmallMTU/3072-plain-nob14-8 3228 336736 ns/op 9.12 MB/s 4090750 B/op 550 allocs/op +BenchmarkTunnelTCPSmallMTU/3072-normal-nob14-8 3076 347067 ns/op 8.85 MB/s 4084480 B/op 554 allocs/op +BenchmarkTunnelTCPSmallMTU/3072-plain-b14-8 2798 395353 ns/op 7.77 MB/s 4952186 B/op 700 allocs/op +BenchmarkTunnelTCPSmallMTU/3072-normal-b14-8 2725 403959 ns/op 7.60 MB/s 4965324 B/op 705 allocs/op +BenchmarkTunnelTCPSmallMTU/3072-preshared-nob14-8 3366 344086 ns/op 8.93 MB/s 4080821 B/op 549 allocs/op +BenchmarkTunnelTCPSmallMTU/3072-preshared-b14-8 2797 403142 ns/op 7.62 MB/s 4962100 B/op 703 allocs/op +BenchmarkTunnelTCPSmallMTU/4096-plain-nob14-8 2360 490867 ns/op 8.34 MB/s 7940290 B/op 871 allocs/op +BenchmarkTunnelTCPSmallMTU/4096-normal-nob14-8 2223 486839 ns/op 8.41 MB/s 7927235 B/op 872 allocs/op +BenchmarkTunnelTCPSmallMTU/4096-plain-b14-8 2002 557560 ns/op 7.35 MB/s 9201342 B/op 1087 allocs/op +BenchmarkTunnelTCPSmallMTU/4096-normal-b14-8 1868 564007 ns/op 7.26 MB/s 9216972 B/op 1091 allocs/op +BenchmarkTunnelTCPSmallMTU/4096-preshared-nob14-8 2263 491698 ns/op 8.33 MB/s 7925404 B/op 869 allocs/op +BenchmarkTunnelTCPSmallMTU/4096-preshared-b14-8 2050 559663 ns/op 7.32 MB/s 9211292 B/op 1086 allocs/op ``` diff --git a/go.mod b/go.mod index fd8a16f..a84ea0f 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/fumiama/blake2b-simd v0.0.0-20220412110131-4481822068bb github.com/fumiama/go-base16384 v1.7.0 github.com/fumiama/go-x25519 v1.0.0 - github.com/fumiama/orbyte v0.0.0-20250228175313-326f247ad703 + github.com/fumiama/orbyte v0.0.0-20250402105757-c427a15a7efb github.com/fumiama/water v0.0.0-20211231134027-da391938d6ac github.com/klauspost/compress v1.17.9 github.com/sirupsen/logrus v1.9.3 diff --git a/go.sum b/go.sum index 5e67ebd..651f5ae 100644 --- a/go.sum +++ b/go.sum @@ -11,8 +11,8 @@ github.com/fumiama/go-base16384 v1.7.0 h1:6fep7XPQWxRlh4Hu+KsdH+6+YdUp+w6CwRXtMW github.com/fumiama/go-base16384 v1.7.0/go.mod h1:OEn+947GV5gsbTAnyuUW/SrfxJYUdYupSIQXOuGOcXM= github.com/fumiama/go-x25519 v1.0.0 h1:hiGg9EhseVmGCc8T1jECVkj8Keu/aJ1ZK05RM8Vuavo= github.com/fumiama/go-x25519 v1.0.0/go.mod h1:8VOhfyGZzw4IUs4nCjQFqW9cA3V/QpSCtP3fo2dLNg4= -github.com/fumiama/orbyte v0.0.0-20250228175313-326f247ad703 h1:NJ3V9S03x9g8e8+patxO13CqR0+OqUq0DDQiMkIUtz4= -github.com/fumiama/orbyte v0.0.0-20250228175313-326f247ad703/go.mod h1:qkUllQ1+gTx5sGrmKvIsqUgsnOO21Hiq847YHJRifbk= +github.com/fumiama/orbyte v0.0.0-20250402105757-c427a15a7efb h1:BgdmqdaovYLxTNORdw/NvgFZh7OTABo0w0puyfrTWmc= +github.com/fumiama/orbyte v0.0.0-20250402105757-c427a15a7efb/go.mod h1:qkUllQ1+gTx5sGrmKvIsqUgsnOO21Hiq847YHJRifbk= github.com/fumiama/water v0.0.0-20211231134027-da391938d6ac h1:A/5A0rODsg+EQHH61Ew5mMUtDpRXaSNqHhPvW+fN4C4= github.com/fumiama/water v0.0.0-20211231134027-da391938d6ac/go.mod h1:BBnNY9PwK+UUn4trAU+H0qsMEypm7+3Bj1bVFuJItlo= github.com/fumiama/wintun v0.0.0-20211229152851-8bc97c8034c0 h1:WfrSFlIlCAtg6Rt2IGna0HhJYSDE45YVHiYqO4wwsEw= diff --git a/gold/head/box.go b/gold/head/box.go index f8e7702..2434cb8 100644 --- a/gold/head/box.go +++ b/gold/head/box.go @@ -17,18 +17,21 @@ import ( // PreCRC64 calculate crc64 checksum without idxdatsz. func (p *Packet) PreCRC64() (crc uint64) { w := bin.SelectWriter() - // 固定 TTL 为 0 计算 + // 固定 TTL 为 0, flag 为空 计算 if bin.IsLittleEndian { ttl := p.TTL + f := p.Proto p.TTL = 0 + p.Proto &= protobit w.Write((*[PacketHeadNoCRCLen]byte)( (unsafe.Pointer)(p), )[:]) p.TTL = ttl + p.Proto = f } else { w.WriteUInt32(p.idxdatsz) w.WriteUInt32(uint32(p.randn)) - w.WriteUInt16(uint16(p.Proto)) // TTL is set to 0 + w.WriteUInt16(uint16(p.Proto & protobit)) // TTL, flags is set to 0 w.WriteUInt16(p.SrcPort) w.WriteUInt16(p.DstPort) w.WriteUInt16(p.Offset) @@ -58,9 +61,9 @@ func (p *Packet) WriteHeaderTo(buf *bytes.Buffer) { pbuf.NewBytes(buf.Len()).V(func(b []byte) { copy(b, buf.Bytes()) ClearTTL(b) - p.md5h8rem = int64(algo.MD5Hash8(b)) + p.md5h8 = algo.MD5Hash8(b) }) - _ = binary.Write(buf, binary.LittleEndian, p.md5h8rem) + _ = binary.Write(buf, binary.LittleEndian, p.md5h8) return } w := bin.SelectWriter() @@ -76,10 +79,10 @@ func (p *Packet) WriteHeaderTo(buf *bytes.Buffer) { pbuf.NewBytes(buf.Len()).V(func(b []byte) { copy(b, buf.Bytes()) ClearTTL(b) - p.md5h8rem = int64(algo.MD5Hash8(b)) + p.md5h8 = algo.MD5Hash8(b) }) }) - w.WriteUInt64(uint64(p.md5h8rem)) + w.WriteUInt64(p.md5h8) w.P(func(b *pbuf.Buffer) { _, _ = buf.ReadFrom(b) }) diff --git a/gold/head/builder.go b/gold/head/builder.go index 68414a8..24235de 100644 --- a/gold/head/builder.go +++ b/gold/head/builder.go @@ -69,11 +69,11 @@ func (pb *HeaderBuilder) Dst(ip net.IP, p uint16) *HeaderBuilder { func (pb *HeaderBuilder) With(data []byte) *DataBuilder { return (*DataBuilder)(pb.p(func(ub *PacketBuf) { // header crc64 except idxdatasz - ub.DAT.md5h8rem = int64(ub.DAT.PreCRC64()) + ub.DAT.md5h8 = ub.DAT.PreCRC64() // plain data ub.Buffer.Write(data) if config.ShowDebugLog { - logrus.Debugln(file.Header(), strconv.FormatUint(uint64(ub.DAT.md5h8rem), 16), "build with data", file.ToLimitHexString(data, 64)) + logrus.Debugln(file.Header(), strconv.FormatUint(ub.DAT.md5h8, 16), "build with data", file.ToLimitHexString(data, 64)) } })) } @@ -89,16 +89,16 @@ func (pb *DataBuilder) Zstd() *DataBuilder { ub.Reset() data.V(func(b []byte) { ub.Write(b) }) if config.ShowDebugLog { - logrus.Debugln(file.Header(), strconv.FormatUint(uint64(ub.DAT.md5h8rem), 16), "data after zstd", file.ToLimitHexString(ub.Bytes(), 64)) + logrus.Debugln(file.Header(), strconv.FormatUint(ub.DAT.md5h8, 16), "data after zstd", file.ToLimitHexString(ub.Bytes(), 64)) } }) } func (pb *DataBuilder) Hash() *DataBuilder { return pb.p(func(ub *PacketBuf) { - ub.DAT.hash = algo.Blake2bHash8( - uint64(ub.DAT.md5h8rem), ub.Bytes(), - ) + ub.DAT.hashrem = int64(algo.Blake2bHash8( + ub.DAT.md5h8, ub.Bytes(), + )) }) } @@ -119,7 +119,7 @@ func (pb *DataBuilder) Seal(aead cipher.AEAD, teatyp uint8, additional uint16) * p(func(ub *PacketBuf) { // encrypted data: chacha20(hash + plain) w := bin.SelectWriter() - w.WriteUInt64(ub.DAT.hash) + w.WriteUInt64(uint64(ub.DAT.hashrem)) w.Write(ub.Bytes()) w.P(func(b *pbuf.Buffer) { data := algo.EncodeAEAD(aead, additional, b.Bytes()) @@ -133,7 +133,7 @@ func (pb *DataBuilder) Plain(teatyp uint8, additional uint16) *PacketBuilder { return (*PacketBuilder)(pb.tea(teatyp).additional(additional). p(func(ub *PacketBuf) { w := bin.SelectWriter() - w.WriteUInt64(ub.DAT.hash) + w.WriteUInt64(uint64(ub.DAT.hashrem)) w.Write(ub.Bytes()) w.P(func(b *pbuf.Buffer) { ub.Reset() diff --git a/gold/head/packet.go b/gold/head/packet.go index e1c0174..6bbeb3c 100644 --- a/gold/head/packet.go +++ b/gold/head/packet.go @@ -3,6 +3,7 @@ package head import ( "errors" "net" + "sync/atomic" "unsafe" "github.com/fumiama/orbyte" @@ -13,8 +14,8 @@ const ( // PacketHeadPreCRCIdx skip idxdatsz, which will be set at Seal(). PacketHeadPreCRCIdx = unsafe.Offsetof(Packet{}.randn) // PacketHeadNoCRCLen without final crc - PacketHeadNoCRCLen = unsafe.Offsetof(Packet{}.md5h8rem) - PacketHeadLen = unsafe.Offsetof(Packet{}.hash) + PacketHeadNoCRCLen = unsafe.Offsetof(Packet{}.md5h8) + PacketHeadLen = unsafe.Offsetof(Packet{}.hashrem) ) var ( @@ -57,19 +58,18 @@ type Packet struct { src [4]byte // dst 目的 ip (ipv4) dst [4]byte - // md5h8rem 发送时记录包头字段除自身外的 checksum 值, - // 接收时记录剩余字节数. + // md5h8 发送时记录包头字段除自身外的 checksum 值. // // 可以认为在一定时间内唯一 (现已更改算法为 md5 但名字未变)。 - md5h8rem int64 + md5h8 uint64 // 以下字段为包体, 与 data 一起加密 - // hash 使用 BLAKE2B 生成加密前 packet data+crc64 的摘要, - // 取其前 8 字节, 小端序读写. + // hashrem 使用 BLAKE2B 生成加密前 packet data+crc64 的摘要, + // 取其前 8 字节, 小端序读写. 接收时记录剩余字节数. // // https://github.com/fumiama/blake2b-simd - hash uint64 + hashrem int64 // Buffer 用于 builder with 暂存原始包体数据 // 以及接收时保存 body, 通过 PacketBytes 截取偏移. @@ -92,7 +92,7 @@ func (p *Packet) Size() int { // CRC64 extract md5h8rem field func (p *Packet) CRC64() uint64 { - return uint64(p.md5h8rem) + return p.md5h8 } func (p *Packet) Src() net.IP { @@ -102,3 +102,7 @@ func (p *Packet) Src() net.IP { func (p *Packet) Dst() net.IP { return append(net.IP{}, p.dst[:]...) } + +func (p *Packet) HasFinished() bool { + return atomic.LoadInt64(&p.hashrem) <= 0 +} diff --git a/gold/head/unbox.go b/gold/head/unbox.go index 2d84550..89cebbc 100644 --- a/gold/head/unbox.go +++ b/gold/head/unbox.go @@ -37,7 +37,7 @@ func ParsePacketHeader(data []byte) (pbytes PacketBytes, err error) { pb.DAT.Offset = binary.LittleEndian.Uint16(data[14:16]) copy(pb.DAT.src[:], data[16:20]) copy(pb.DAT.dst[:], data[20:24]) - pb.DAT.md5h8rem = int64(binary.LittleEndian.Uint64(data[24:32])) + pb.DAT.md5h8 = binary.LittleEndian.Uint64(data[24:32]) } sz = pb.DAT.Size() if !pb.DAT.Proto.IsValid() { @@ -60,10 +60,10 @@ func ParsePacketHeader(data []byte) (pbytes PacketBytes, err error) { ClearTTL(b) crc = algo.MD5Hash8(b) }) - if crc != uint64(pb.DAT.md5h8rem) { + if crc != pb.DAT.md5h8 { err = ErrBadCRCChecksum if config.ShowDebugLog { - logrus.Warnf("[unbox] exp crc %016x but got %016x", pb.DAT.md5h8rem, crc) + logrus.Warnf("[unbox] exp crc %016x but got %016x", pb.DAT.md5h8, crc) } return } @@ -72,12 +72,12 @@ func ParsePacketHeader(data []byte) (pbytes PacketBytes, err error) { } if sz+int(PacketHeadLen) == len(data) { pb.Buffer.Write(data[PacketHeadLen:]) - pb.DAT.md5h8rem = -1 + pb.DAT.hashrem = -1 return } pb.Buffer.Grow(sz) pb.Buffer.Write(make([]byte, sz)) - pb.DAT.md5h8rem = int64(sz) + pb.DAT.hashrem = int64(sz) }) if err != nil { return @@ -92,7 +92,7 @@ func ParsePacketHeader(data []byte) (pbytes PacketBytes, err error) { // // return: complete. func (p *Packet) WriteDataSegment(data, buf []byte) bool { - if atomic.LoadInt64(&p.md5h8rem) <= 0 { + if p.HasFinished() { return true } @@ -103,21 +103,24 @@ func (p *Packet) WriteDataSegment(data, buf []byte) bool { } if offset == 0 { + p.randn = int32(binary.LittleEndian.Uint32(data[4:8])) p.Proto = flags + p.TTL = data[9] p.Offset = 0 + p.md5h8 = binary.LittleEndian.Uint64(data[24:32]) if config.ShowDebugLog { logrus.Debugln("[unbox] parse data set zero offset flags", flags) } } - rembytes := atomic.LoadInt64(&p.md5h8rem) + rembytes := atomic.LoadInt64(&p.hashrem) if rembytes > 0 { n := int64(copy(buf[offset:], data[PacketHeadLen:])) newrem := rembytes - n - for !atomic.CompareAndSwapInt64(&p.md5h8rem, rembytes, newrem) { - rembytes = atomic.LoadInt64(&p.md5h8rem) + for !atomic.CompareAndSwapInt64(&p.hashrem, rembytes, newrem) { + rembytes = atomic.LoadInt64(&p.hashrem) newrem = rembytes - n } } - return atomic.LoadInt64(&p.md5h8rem) <= 0 + return p.HasFinished() } diff --git a/gold/link/crypto.go b/gold/link/crypto.go index dd6e38e..401a650 100644 --- a/gold/link/crypto.go +++ b/gold/link/crypto.go @@ -1,9 +1,14 @@ package link import ( - "github.com/fumiama/orbyte/pbuf" + "encoding/hex" + "github.com/fumiama/orbyte/pbuf" + "github.com/sirupsen/logrus" + + "github.com/fumiama/WireGold/config" "github.com/fumiama/WireGold/internal/algo" + "github.com/fumiama/WireGold/internal/file" ) func (l *Link) randkeyidx() uint8 { @@ -19,11 +24,20 @@ func (l *Link) decode(teatype uint8, additional uint16, b []byte) (db pbuf.Bytes return } if l.keys[0] == nil { + if config.ShowDebugLog { + n := len(b) + endl := "." + if n > 64 { + n = 64 + endl = "..." + } + logrus.Debugln(file.Header(), "copy plain text", hex.EncodeToString(b[:n]), endl) + } return pbuf.ParseBytes(b...).Copy(), nil } aead := l.keys[teatype] if aead == nil { - return + panic("unexpected empty aead") } return algo.DecodeAEAD(aead, additional, b) } diff --git a/gold/link/listen.go b/gold/link/listen.go index 3d0e50a..fcae9bc 100644 --- a/gold/link/listen.go +++ b/gold/link/listen.go @@ -69,7 +69,17 @@ func (m *Me) waitordispatch(addr p2p.EndPoint, buf pbuf.Bytes, n int) { recvlooptime := atomic.LoadInt64(&m.recvlooptime) if recvloopcnt%uintptr(m.speedloop) == 0 { now := time.Now().UnixMilli() - logrus.Infof("[listen] queue recv avg speed: %.2f KB/s", float64(recvtotlcnt)/float64(now-recvlooptime)) + kb := float64(recvtotlcnt) / float64(now-recvlooptime) + if kb < 1024 { + logrus.Infof("[listen] queue recv avg speed: %.2f KB/s", kb) + } else { + kb /= 1024 + if kb < 1024 { + logrus.Infof("[listen] queue recv avg speed: %.2f MB/s", kb) + } else { + logrus.Infof("[listen] queue recv avg speed: %.2f GB/s", kb/1024) + } + } atomic.StoreUint64(&m.recvtotlcnt, 0) atomic.StoreInt64(&m.recvlooptime, now) } @@ -85,6 +95,9 @@ func (m *Me) waitordispatch(addr p2p.EndPoint, buf pbuf.Bytes, n int) { if config.ShowDebugLog { logrus.Debugln("[listen] dispatch", len(b), "bytes packet") } + if !p.HasFinished() { + panic("unexpected unfinished") + } m.dispatch(p, b, addr) }) }) diff --git a/gold/link/me.go b/gold/link/me.go index 5d87ac9..633045e 100644 --- a/gold/link/me.go +++ b/gold/link/me.go @@ -49,7 +49,7 @@ type Me struct { // 本机未接收完全分片池 recving *ttl.Cache[uint16, head.PacketBytes] // 抗重放攻击记录池 - recved *ttl.Cache[uint32, struct{}] + recved *ttl.Cache[uint64, struct{}] // 本机上层配置 srcport, dstport, mtu, speedloop uint16 // 报头掩码 @@ -143,7 +143,7 @@ func NewMe(cfg *MyConfig) (m Me) { binary.BigEndian.PutUint64(buf[:], m.mask) logrus.Infoln("[me] xor mask", hex.EncodeToString(buf[:])) m.recving = ttl.NewCache[uint16, head.PacketBytes](time.Second * 10) - m.recved = ttl.NewCache[uint32, struct{}](time.Minute) + m.recved = ttl.NewCache[uint64, struct{}](time.Minute) return } diff --git a/gold/link/recv.go b/gold/link/recv.go index 3ba17fb..f64a067 100644 --- a/gold/link/recv.go +++ b/gold/link/recv.go @@ -78,9 +78,13 @@ func (m *Me) wait(data []byte, addr p2p.EndPoint) (h head.PacketBytes) { if config.ShowDebugLog { logrus.Debugf("[recv] packet seq %08x", seq) } - if _, got := m.recved.GetOrSet(seq, struct{}{}); got { + crc := uint64(0) + header.B(func(_ []byte, p *head.Packet) { + crc = p.CRC64() + }) + if _, got := m.recved.GetOrSet(uint64(seq)^crc, struct{}{}); got { if config.ShowDebugLog { - logrus.Debugln("[recv] ignore duplicated seq packet", strconv.FormatUint(uint64(seq), 16)) + logrus.Debugln("[recv] ignore duplicated seq^crc packet, seq", strconv.FormatUint(uint64(seq), 16), "crc", strconv.FormatUint(crc, 16)) } return } @@ -145,8 +149,9 @@ func (m *Me) wait(data []byte, addr p2p.EndPoint) (h head.PacketBytes) { if config.ShowDebugLog { logrus.Debugln("[recv]", strconv.FormatUint(uint64(seq&0xffff), 16), "get frag part isnew:", !got) } + ok := false h.B(func(buf []byte, p *head.Packet) { - ok := p.WriteDataSegment(data, buf) + ok = p.WriteDataSegment(data, buf) if !ok { if config.ShowDebugLog { logrus.Debugln("[recv]", strconv.FormatUint(uint64(seq&0xffff), 16), "wait other frag parts isnew:", !got) @@ -158,5 +163,8 @@ func (m *Me) wait(data []byte, addr p2p.EndPoint) (h head.PacketBytes) { logrus.Debugln("[recv]", strconv.FormatUint(uint64(seq&0xffff), 16), "all parts has reached") } }) + if !ok { + return head.PacketBytes{} + } return } diff --git a/gold/link/send.go b/gold/link/send.go index 2a3fe97..a6c47ec 100644 --- a/gold/link/send.go +++ b/gold/link/send.go @@ -5,6 +5,7 @@ import ( "encoding/binary" "encoding/hex" "errors" + "fmt" "io" "math/rand" @@ -40,7 +41,7 @@ func (l *Link) WritePacket(proto uint8, data []byte, ttl uint8) { mtu -= uint16(rand.Intn(int(l.mturandomrange))) } if config.ShowDebugLog { - logrus.Debugln("[send] write mtu:", mtu, ", addt:", sndcnt&0x07ff, ", key index:", teatype) + logrus.Debugln("[send] write mtu:", mtu, ", addt:", sndcnt&0x07ff, ", key index:", teatype, ", data len:", len(data)) } pb := head.NewPacketBuilder(). Src(l.me.me, l.me.srcport).Dst(l.peerip, l.me.dstport). @@ -55,7 +56,11 @@ func (l *Link) WritePacket(proto uint8, data []byte, ttl uint8) { } else { pktb = pb.Seal(l.keys[teatype], teatype, sndcnt&0x07ff) } - for _, b := range pktb.Split(int(mtu), false) { //TODO: impl. nofrag + bs := pktb.Split(int(mtu), false) + if config.ShowDebugLog { + logrus.Debugln("[send] split packet into", len(bs), "parts") + } + for _, b := range bs { //TODO: impl. nofrag go l.write2peer(head.BuildPacketFromBytes(b), randseq(sndcnt)) } } @@ -99,7 +104,7 @@ func (l *Link) write2peer1(b pbuf.Bytes, seq uint32) (err error) { bound = len(data) endl = "." } - logrus.Debugln("[send] raw data bytes", hex.EncodeToString(data[:bound]), endl) + logrus.Debugln("[send] crc seq", fmt.Sprintf("%08x", seq), "raw data bytes", hex.EncodeToString(data[:bound]), endl) } b = l.me.xorenc(data, seq) if config.ShowDebugLog { @@ -110,7 +115,7 @@ func (l *Link) write2peer1(b pbuf.Bytes, seq uint32) (err error) { endl = "." } b.V(func(b []byte) { - logrus.Debugln("[send] xored data bytes", hex.EncodeToString(b[:bound]), endl) + logrus.Debugln("[send] crc seq", fmt.Sprintf("%08x", seq), "xored data bytes", hex.EncodeToString(b[:bound]), endl) }) } }) @@ -125,14 +130,14 @@ func (l *Link) write2peer1(b pbuf.Bytes, seq uint32) (err error) { endl = "." } b.V(func(b []byte) { - logrus.Debugln("[send] xored data bytes", hex.EncodeToString(b[:bound]), endl) + logrus.Debugln("[send] crc seq", fmt.Sprintf("%08x", seq), "b14ed data bytes", hex.EncodeToString(b[:bound]), endl) }) } }) } b.V(func(b []byte) { if config.ShowDebugLog { - logrus.Debugln("[send] write", len(b), "bytes data from ep", conn.LocalAddr(), "to", peerep) + logrus.Debugln("[send] crc seq", fmt.Sprintf("%08x", seq), "write", len(b), "bytes data from ep", conn.LocalAddr(), "to", peerep) } _, err = conn.WriteToPeer(b, peerep) }) diff --git a/internal/algo/hash.go b/internal/algo/hash.go index 8841c4a..fcc8d12 100644 --- a/internal/algo/hash.go +++ b/internal/algo/hash.go @@ -5,6 +5,7 @@ import ( "crypto/md5" "encoding/binary" "encoding/hex" + "fmt" "github.com/fumiama/WireGold/config" "github.com/fumiama/blake2b-simd" @@ -20,7 +21,7 @@ func Blake2bHash8(precrc64 uint64, data []byte) uint64 { _, _ = h.Write(data) b := h.Sum(tgt[:0])[:8] if config.ShowDebugLog { - logrus.Debugln("[algo] blk2b hash:", hex.EncodeToString(b)) + logrus.Debugln("[algo] precrc64:", fmt.Sprintf("%04x", precrc64), "blk2b hash:", hex.EncodeToString(b)) } return binary.LittleEndian.Uint64(b) } diff --git a/upper/services/tunnel/tunnel.go b/upper/services/tunnel/tunnel.go index 839ea98..9f5a692 100644 --- a/upper/services/tunnel/tunnel.go +++ b/upper/services/tunnel/tunnel.go @@ -163,9 +163,8 @@ func (s *Tunnel) handleRead() { } end := 64 endl := "..." - pp := &p.H - if pp.Size() < 64 { - end = pp.Size() + if p.D.Len() < 64 { + end = p.D.Len() endl = "." } var recvseq uint32 diff --git a/upper/services/tunnel/tunnel_test.go b/upper/services/tunnel/tunnel_test.go index 2be5f39..7717f24 100644 --- a/upper/services/tunnel/tunnel_test.go +++ b/upper/services/tunnel/tunnel_test.go @@ -7,6 +7,7 @@ import ( "fmt" "io" "runtime" + "strconv" "strings" "testing" "time" @@ -18,42 +19,34 @@ import ( "github.com/fumiama/WireGold/internal/bin" ) -func TestTunnelUDP(t *testing.T) { - testTunnelNetwork(t, "udp", 4096) +func testTunnelMTUsNetwork(t *testing.T, nw string) { + for i := 1; i <= 4; i++ { + sz := 1024 * i + if !t.Run(strconv.Itoa(sz), func(t *testing.T) { + testTunnelNetwork(t, nw, uint16(sz)) + }) { + return + } + } } -func TestTunnelUDPSmallMTU(t *testing.T) { - testTunnelNetwork(t, "udp", 1024) +func TestTunnelUDP(t *testing.T) { + testTunnelMTUsNetwork(t, "udp") } func TestTunnelUDPLite(t *testing.T) { if runtime.GOOS == "darwin" { return } - testTunnelNetwork(t, "udplite", 4096) -} - -func TestTunnelUDPLiteSmallMTU(t *testing.T) { - if runtime.GOOS == "darwin" { - return - } - testTunnelNetwork(t, "udplite", 1024) + testTunnelMTUsNetwork(t, "udplite") } func TestTunnelTCP(t *testing.T) { - testTunnelNetwork(t, "tcp", 4096) -} - -func TestTunnelTCPSmallMTU(t *testing.T) { - testTunnelNetwork(t, "tcp", 1024) + testTunnelMTUsNetwork(t, "tcp") } func TestTunnelIP(t *testing.T) { - testTunnelNetwork(t, "ip", 4096) -} - -func TestTunnelIPSmallMTU(t *testing.T) { - testTunnelNetwork(t, "ip", 1024) + testTunnelMTUsNetwork(t, "ip") } func BenchmarkTunnelUDP(b *testing.B) { @@ -95,7 +88,7 @@ func BenchmarkTunnelIPSmallMTU(b *testing.B) { } func testTunnel(t *testing.T, nw string, isplain, isbase14 bool, pshk *[32]byte, mtu uint16) { - fmt.Println("start", nw, "testing") + fmt.Println("start", nw, "testing, mtu", mtu, "plain", isplain, "b14", isbase14, "pshk", pshk != nil) selfpk, err := curve.New(nil) if err != nil { panic(err) @@ -195,50 +188,25 @@ func testTunnel(t *testing.T, nw string, isplain, isbase14 bool, pshk *[32]byte, t.Fail() } - sendb = make([]byte, 4096) - for i := 0; i < 4096; i++ { + sendb = make([]byte, mtu+4) + for i := 0; i < len(sendb); i++ { sendb[i] = byte(i) } - go tunnme.Write(sendb) - buf = make([]byte, 4096) - _, err = io.ReadFull(&tunnpeer, buf) - if err != nil { - t.Fatal(err) - } - if string(sendb) != string(buf) { - t.Fatal("error: recv 4096 bytes data") - } - sendbufs := make(chan []byte, 32) - - go func() { - time.Sleep(time.Second) - for i := 0; i < 32; i++ { - sendb := make([]byte, 65535) - for j := 0; j < 65535; j++ { - sendb[j] = byte(i + j) - } - n, _ := tunnme.Write(sendb) - sendbufs <- sendb - logrus.Debugln("loop", i, "write", n, "bytes") - } - close(sendbufs) - }() - buf = make([]byte, 65535) - i := 0 - for sendb := range sendbufs { - n, err := io.ReadFull(&tunnpeer, buf) + for i := 1; i < len(sendb); i++ { + rand.Read(sendb[:i]) + go tunnme.Write(sendb[:i]) + buf := make([]byte, i) + _, err = io.ReadFull(&tunnpeer, buf) if err != nil { t.Fatal(err) } - logrus.Debugln("loop", i, "read", n, "bytes") - if string(sendb) != string(buf) { - t.Fatal("loop", i, "error: recv 65535 bytes data") + if !bytes.Equal(sendb[:i], buf) { + t.Fatal("error: recv", i, "bytes data") } - i++ } - for i := 0; i < 4096; i++ { + for i := 0; i < len(sendb); i++ { sendb[i] = ^byte(i) } tunnme.Write(sendb) @@ -255,7 +223,7 @@ func testTunnel(t *testing.T, nw string, isplain, isbase14 bool, pshk *[32]byte, t.Fatal(err) } if string(sendb) != rd.String() { - t.Fatal("error: recv fragmented 4096 bytes data") + t.Fatal("error: recv fragmented data") } }