-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
device: reduce RoutineHandshake allocations #116
Open
AlexanderYastrebov
wants to merge
1
commit into
WireGuard:master
Choose a base branch
from
AlexanderYastrebov:device/reduce-RoutineHandshake-allocs
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,6 @@ | |
package device | ||
|
||
import ( | ||
"bytes" | ||
"encoding/binary" | ||
"errors" | ||
"net" | ||
|
@@ -276,6 +275,11 @@ func (device *Device) RoutineHandshake(id int) { | |
}() | ||
device.log.Verbosef("Routine: handshake worker %d - started", id) | ||
|
||
var ( | ||
msgCookieReply MessageCookieReply | ||
msgInitiation MessageInitiation | ||
msgResponse MessageResponse | ||
) | ||
for elem := range device.queue.handshake.c { | ||
|
||
// handle cookie fields and ratelimiting | ||
|
@@ -286,17 +290,15 @@ func (device *Device) RoutineHandshake(id int) { | |
|
||
// unmarshal packet | ||
|
||
var reply MessageCookieReply | ||
reader := bytes.NewReader(elem.packet) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here byte reader can be replaced with https://pkg.go.dev/encoding/binary#Decode added in 1.23 golang/go#60023 (comment) |
||
err := binary.Read(reader, binary.LittleEndian, &reply) | ||
err := msgCookieReply.unmarshal(elem.packet) | ||
if err != nil { | ||
device.log.Verbosef("Failed to decode cookie reply") | ||
goto skip | ||
} | ||
|
||
// lookup peer from index | ||
|
||
entry := device.indexTable.Lookup(reply.Receiver) | ||
entry := device.indexTable.Lookup(msgCookieReply.Receiver) | ||
|
||
if entry.peer == nil { | ||
goto skip | ||
|
@@ -306,7 +308,7 @@ func (device *Device) RoutineHandshake(id int) { | |
|
||
if peer := entry.peer; peer.isRunning.Load() { | ||
device.log.Verbosef("Receiving cookie response from %s", elem.endpoint.DstToString()) | ||
if !peer.cookieGenerator.ConsumeReply(&reply) { | ||
if !peer.cookieGenerator.ConsumeReply(&msgCookieReply) { | ||
device.log.Verbosef("Could not decrypt invalid cookie response") | ||
} | ||
} | ||
|
@@ -352,17 +354,15 @@ func (device *Device) RoutineHandshake(id int) { | |
|
||
// unmarshal | ||
|
||
var msg MessageInitiation | ||
reader := bytes.NewReader(elem.packet) | ||
err := binary.Read(reader, binary.LittleEndian, &msg) | ||
err := msgInitiation.unmarshal(elem.packet) | ||
if err != nil { | ||
device.log.Errorf("Failed to decode initiation message") | ||
goto skip | ||
} | ||
|
||
// consume initiation | ||
|
||
peer := device.ConsumeMessageInitiation(&msg) | ||
peer := device.ConsumeMessageInitiation(&msgInitiation) | ||
if peer == nil { | ||
device.log.Verbosef("Received invalid initiation message from %s", elem.endpoint.DstToString()) | ||
goto skip | ||
|
@@ -385,17 +385,15 @@ func (device *Device) RoutineHandshake(id int) { | |
|
||
// unmarshal | ||
|
||
var msg MessageResponse | ||
reader := bytes.NewReader(elem.packet) | ||
err := binary.Read(reader, binary.LittleEndian, &msg) | ||
err := msgResponse.unmarshal(elem.packet) | ||
if err != nil { | ||
device.log.Errorf("Failed to decode response message") | ||
goto skip | ||
} | ||
|
||
// consume response | ||
|
||
peer := device.ConsumeMessageResponse(&msg) | ||
peer := device.ConsumeMessageResponse(&msgResponse) | ||
if peer == nil { | ||
device.log.Verbosef("Received invalid response message from %s", elem.endpoint.DstToString()) | ||
goto skip | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe name it
reset
.Also the size of the packet is checked earlier
wireguard-go/device/receive.go
Lines 191 to 204 in 12269c2
so this check could be removed as well as error result check at the callsite.