Skip to content

Commit

Permalink
🫜 service: cap coalesced message to 65535 bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
database64128 committed Oct 19, 2024
1 parent 62b37de commit 0d39260
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 10 deletions.
16 changes: 14 additions & 2 deletions service/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -627,8 +627,14 @@ func (c *client) relayWgToProxyGeneric(uplink clientNatUplinkGeneric) {
b := sqp.buf
segmentsRemaining := sqp.segmentCount

maxUDPGSOSegments := uplink.proxyConnInfo.MaxUDPGSOSegments
if maxUDPGSOSegments > 1 {
// Cap each coalesced message to 65535 bytes to prevent -EMSGSIZE.
maxUDPGSOSegments = max(1, 65535/sqp.segmentSize)
}

for segmentsRemaining > 0 {
sendSegmentCount := min(segmentsRemaining, uplink.proxyConnInfo.MaxUDPGSOSegments)
sendSegmentCount := min(segmentsRemaining, maxUDPGSOSegments)
segmentsRemaining -= sendSegmentCount

sendBufSize := min(len(b), int(sqp.segmentSize*sendSegmentCount))
Expand Down Expand Up @@ -870,8 +876,14 @@ func (c *client) relayProxyToWgGeneric(downlink clientNatDownlinkGeneric) {
b := qp.buf
segmentsRemaining := qp.segmentCount

maxUDPGSOSegments := downlink.wgConnInfo.MaxUDPGSOSegments
if maxUDPGSOSegments > 1 {
// Cap each coalesced message to 65535 bytes to prevent -EMSGSIZE.
maxUDPGSOSegments = max(1, 65535/qp.segmentSize)
}

for segmentsRemaining > 0 {
sendSegmentCount := min(segmentsRemaining, downlink.wgConnInfo.MaxUDPGSOSegments)
sendSegmentCount := min(segmentsRemaining, maxUDPGSOSegments)
segmentsRemaining -= sendSegmentCount

sendBufSize := min(len(b), int(qp.segmentSize*sendSegmentCount))
Expand Down
16 changes: 14 additions & 2 deletions service/client_mmsg.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,8 +527,14 @@ main:
b := sqp.buf
segmentsRemaining := sqp.segmentCount

maxUDPGSOSegments := uplink.proxyConnInfo.MaxUDPGSOSegments
if maxUDPGSOSegments > 1 {
// Cap each coalesced message to 65535 bytes to prevent -EMSGSIZE.
maxUDPGSOSegments = max(1, 65535/sqp.segmentSize)
}

for segmentsRemaining > 0 {
sendSegmentCount := min(segmentsRemaining, uplink.proxyConnInfo.MaxUDPGSOSegments)
sendSegmentCount := min(segmentsRemaining, maxUDPGSOSegments)
segmentsRemaining -= sendSegmentCount

sendBufSize := min(len(b), int(sqp.segmentSize*sendSegmentCount))
Expand Down Expand Up @@ -853,8 +859,14 @@ func (c *client) relayProxyToWgSendmmsg(downlink clientNatDownlinkMmsg) {
b := qp.buf
segmentsRemaining := qp.segmentCount

maxUDPGSOSegments := downlink.wgConnInfo.MaxUDPGSOSegments
if maxUDPGSOSegments > 1 {
// Cap each coalesced message to 65535 bytes to prevent -EMSGSIZE.
maxUDPGSOSegments = max(1, 65535/qp.segmentSize)
}

for segmentsRemaining > 0 {
sendSegmentCount := min(segmentsRemaining, downlink.wgConnInfo.MaxUDPGSOSegments)
sendSegmentCount := min(segmentsRemaining, maxUDPGSOSegments)
segmentsRemaining -= sendSegmentCount

sendBufSize := min(len(b), int(qp.segmentSize*sendSegmentCount))
Expand Down
16 changes: 14 additions & 2 deletions service/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -617,8 +617,14 @@ func (s *server) relayProxyToWgGeneric(uplink serverNatUplinkGeneric) {
b := qp.buf
segmentsRemaining := qp.segmentCount

maxUDPGSOSegments := uplink.wgConnInfo.MaxUDPGSOSegments
if maxUDPGSOSegments > 1 {
// Cap each coalesced message to 65535 bytes to prevent -EMSGSIZE.
maxUDPGSOSegments = max(1, 65535/qp.segmentSize)
}

for segmentsRemaining > 0 {
sendSegmentCount := min(segmentsRemaining, uplink.wgConnInfo.MaxUDPGSOSegments)
sendSegmentCount := min(segmentsRemaining, maxUDPGSOSegments)
segmentsRemaining -= sendSegmentCount

sendBufSize := min(len(b), int(qp.segmentSize*sendSegmentCount))
Expand Down Expand Up @@ -846,8 +852,14 @@ func (s *server) relayWgToProxyGeneric(downlink serverNatDownlinkGeneric) {
b := qp.buf
segmentsRemaining := qp.segmentCount

maxUDPGSOSegments := downlink.proxyConnInfo.MaxUDPGSOSegments
if maxUDPGSOSegments > 1 {
// Cap each coalesced message to 65535 bytes to prevent -EMSGSIZE.
maxUDPGSOSegments = max(1, 65535/qp.segmentSize)
}

for segmentsRemaining > 0 {
sendSegmentCount := min(segmentsRemaining, downlink.proxyConnInfo.MaxUDPGSOSegments)
sendSegmentCount := min(segmentsRemaining, maxUDPGSOSegments)
segmentsRemaining -= sendSegmentCount

sendBufSize := min(len(b), int(qp.segmentSize*sendSegmentCount))
Expand Down
20 changes: 16 additions & 4 deletions service/server_mmsg.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,14 +487,14 @@ func (s *server) relayProxyToWgSendmmsg(uplink serverNatUplinkMmsg) {
msgvec := make([]conn.Mmsghdr, 0, s.relayBatchSize)

for {
var isHandshake bool

// Block on first dequeue op.
qp, ok := <-uplink.wgConnSendCh
if !ok {
break
}

var isHandshake bool

dequeue:
for {
bufvec = append(bufvec, qp.buf)
Expand All @@ -507,8 +507,14 @@ func (s *server) relayProxyToWgSendmmsg(uplink serverNatUplinkMmsg) {
b := qp.buf
segmentsRemaining := qp.segmentCount

maxUDPGSOSegments := uplink.wgConnInfo.MaxUDPGSOSegments
if maxUDPGSOSegments > 1 {
// Cap each coalesced message to 65535 bytes to prevent -EMSGSIZE.
maxUDPGSOSegments = max(1, 65535/qp.segmentSize)
}

for segmentsRemaining > 0 {
sendSegmentCount := min(segmentsRemaining, uplink.wgConnInfo.MaxUDPGSOSegments)
sendSegmentCount := min(segmentsRemaining, maxUDPGSOSegments)
segmentsRemaining -= sendSegmentCount

sendBufSize := min(len(b), int(qp.segmentSize*sendSegmentCount))
Expand Down Expand Up @@ -848,8 +854,14 @@ func (s *server) relayWgToProxySendmmsg(downlink serverNatDownlinkMmsg) {
b := qp.buf
segmentsRemaining := qp.segmentCount

maxUDPGSOSegments := downlink.proxyConnInfo.MaxUDPGSOSegments
if maxUDPGSOSegments > 1 {
// Cap each coalesced message to 65535 bytes to prevent -EMSGSIZE.
maxUDPGSOSegments = max(1, 65535/qp.segmentSize)
}

for segmentsRemaining > 0 {
sendSegmentCount := min(segmentsRemaining, downlink.proxyConnInfo.MaxUDPGSOSegments)
sendSegmentCount := min(segmentsRemaining, maxUDPGSOSegments)
segmentsRemaining -= sendSegmentCount

sendBufSize := min(len(b), int(qp.segmentSize*sendSegmentCount))
Expand Down

0 comments on commit 0d39260

Please sign in to comment.