Skip to content
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

router: reduce per-packet allocations #4030

Closed
wants to merge 4 commits into from

Conversation

matzf
Copy link
Member

@matzf matzf commented Apr 27, 2021

Avoid allocating scionPacketProcessor; re-organized packet processing
functions. The scionPacketProcessor is now the main entry point for
packet processing and contains all the pre-allocated per-packet state.

Avoid allocation of message buffer for writing with WriteBatch.
We could just pre-allocate a buffer, but as we always write a batch of
size one, just use WriteTo instead.
For writing single packets, this is both more convenient and faster.
Additional benefit: Read/WriteBatch make rather large allocations
internally (see golang/go#26838) which is
avoided by using WriteTo.

The largest remaining allocations when processing SCION packets are:

  • ReadBatch allocates temporary storage internally (as mentioned above).
    This is by far the biggest culprit, it accounts for almost 80% (after
    the changes in this patch).
  • slayers/path.NewPath called during slayers.SCION.DecodeFromBytes:
    Could be avoided by reusing Path objects, but requires larger changes.
  • slayers/path/scion.Raw.GetCurrentHopField and GetCurrentInfoField, called during parsePath:
    Both return pointer types. Should be possible to change this to value
    types instead, but requires larger changes.
  • DataPlane.resolveLocalDst: allocates IP and UDPAddr. Can only really
    be fixed with better address types.
  • slayers/path.FullMAC. Allocates both input and output. The function
    interface could be changed to allow reuse of the input buffer.
    The output buffer cannot be reused with this CMAC library.

Depends on (or actually includes) #4029.


This change is Reviewable

@lukedirtwalker
Copy link
Collaborator

/rebase

@lukedirtwalker lukedirtwalker self-assigned this May 4, 2021
Avoid allocating scionPacketProcessor; re-organized packet processing
functions. The scionPacketProcessor is now the main entry point for
packet processing and contains all the pre-allocated per-packet state.

Avoid allocation of message buffer for writing with WriteBatch.
We could just pre-allocate a buffer, but as we always write a batch of
size one, just use WriteTo instead.
For writing single packets, this is both more convenient and faster.
Additional benefit: Read/WriteBatch make rather large allocations
internally (see golang/go#26838) which is
avoided by using WriteTo.

The largest remaining allocations when processing SCION packets are:
- ReadBatch allocates temporary storage internally (as mentioned above).
  This is by far the biggest culprit, it accounts for almost 80% (after
  the changes in this patch).
- slayers/path.NewPath called during slayers.SCION.DecodeFromBytes:
  Could be avoided by reusing Path objects, but requires larger changes.
- slayers/path/scion.Raw.GetCurrentHopField and GetCurrentInfoField, called during parsePath:
  Both return pointer types. Should be possible to change this to value
  types instead, but requires larger changes.
- DataPlane.resolveLocalDst: allocates IP and UDPAddr. Can only really
  be fixed with better address types.
- slayers/path.FullMAC. Allocates both input and output. The function
  interface could be changed to allow reuse of the input buffer.
  The output buffer cannot be reused with this CMAC library.
Copy link
Collaborator

@lukedirtwalker lukedirtwalker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 5 of 5 files at r1.
Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @matzf)


go/pkg/router/dataplane.go, line 80 at r1 (raw file):

	ReadBatch(underlayconn.Messages) (int, error)
	WriteTo([]byte, *net.UDPAddr) (int, error)
	WriteBatch(underlayconn.Messages) (int, error)

this no longer seems to be used, please remove and update the mock.


go/pkg/router/dataplane.go, line 487 at r1 (raw file):

, _

Please check the cast succeeds and if it doesn't the packet should be discarded. I think this address comes from user input so we can not simply assume that the cast works.

Copy link
Member Author

@matzf matzf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 3 of 5 files reviewed, 2 unresolved discussions (waiting on @lukedirtwalker)


go/pkg/router/dataplane.go, line 80 at r1 (raw file):

Previously, lukedirtwalker (Lukas Vogel) wrote…

this no longer seems to be used, please remove and update the mock.

Done. I've not renamed it although the name doesn't seem very apt now. I cannot think of anything that's really better, both "Conn" or "UnderlayConn" would probably be net negative in clarity. The doc string is technically still correct: 'supports "batch reads" and "writes".' 😉


go/pkg/router/dataplane.go, line 487 at r1 (raw file):

Previously, lukedirtwalker (Lukas Vogel) wrote…
, _

Please check the cast succeeds and if it doesn't the packet should be discarded. I think this address comes from user input so we can not simply assume that the cast works.

I've checked the different code paths setting this, the address is either nil (uninitialized) or UDPAddr. I've changed this to use a panic-y type assertion to make this expectation explicit.

Copy link
Collaborator

@lukedirtwalker lukedirtwalker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 2 of 2 files at r2.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @matzf)


go/pkg/router/dataplane.go, line 487 at r1 (raw file):

Previously, matzf (Matthias Frei) wrote…

I've checked the different code paths setting this, the address is either nil (uninitialized) or UDPAddr. I've changed this to use a panic-y type assertion to make this expectation explicit.

Hm then it would be cleaner to change the type of result.OutAddr, if that isn't too much work.

Copy link
Member Author

@matzf matzf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 2 of 5 files reviewed, 1 unresolved discussion (waiting on @lukedirtwalker)


go/pkg/router/dataplane.go, line 487 at r1 (raw file):

Previously, lukedirtwalker (Lukas Vogel) wrote…

Hm then it would be cleaner to change the type of result.OutAddr, if that isn't too much work.

Done. This goes through quite a few places, now UDPAddr is now ubuquitous. I quite like that, though. I think it makes the previously hidden assumptions more clearly visible.

Copy link
Collaborator

@lukedirtwalker lukedirtwalker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 3 of 3 files at r3.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @matzf)

marcfrei pushed a commit to netsec-ethz/scion that referenced this pull request May 26, 2021
Avoid allocating scionPacketProcessor; re-organized packet processing
functions. The scionPacketProcessor is now the main entry point for
packet processing and contains all the pre-allocated per-packet state.

Avoid allocation of message buffer for writing with WriteBatch.
We could just pre-allocate a buffer, but as we always write a batch of
size one, just use WriteTo instead.
For writing single packets, this is both more convenient and faster.
Additional benefit: Read/WriteBatch make rather large allocations
internally (see golang/go#26838) which is
avoided by using WriteTo.

The largest remaining allocations when processing SCION packets are:
- ReadBatch allocates temporary storage internally (as mentioned above).
  This is by far the biggest culprit, it accounts for almost 80% (after
  the changes in this patch).
- slayers/path.NewPath called during slayers.SCION.DecodeFromBytes:
  Could be avoided by reusing Path objects, but requires larger changes.
- slayers/path/scion.Raw.GetCurrentHopField and GetCurrentInfoField, called during parsePath:
  Both return pointer types. Should be possible to change this to value
  types instead, but requires larger changes.
- DataPlane.resolveLocalDst: allocates IP and UDPAddr. Can only really
  be fixed with better address types.
- slayers/path.FullMAC. Allocates both input and output. The function
  interface could be changed to allow reuse of the input buffer.
  The output buffer cannot be reused with this CMAC library.

Depends on (or actually includes) scionproto#4029.

Closes scionproto#4030

GitOrigin-RevId: 72eb8918a54a25f74159ef7398ef3d98d756e501
marcfrei pushed a commit to netsec-ethz/scion that referenced this pull request Jun 3, 2021
Avoid allocating scionPacketProcessor; re-organized packet processing
functions. The scionPacketProcessor is now the main entry point for
packet processing and contains all the pre-allocated per-packet state.

Avoid allocation of message buffer for writing with WriteBatch.
We could just pre-allocate a buffer, but as we always write a batch of
size one, just use WriteTo instead.
For writing single packets, this is both more convenient and faster.
Additional benefit: Read/WriteBatch make rather large allocations
internally (see golang/go#26838) which is
avoided by using WriteTo.

The largest remaining allocations when processing SCION packets are:
- ReadBatch allocates temporary storage internally (as mentioned above).
  This is by far the biggest culprit, it accounts for almost 80% (after
  the changes in this patch).
- slayers/path.NewPath called during slayers.SCION.DecodeFromBytes:
  Could be avoided by reusing Path objects, but requires larger changes.
- slayers/path/scion.Raw.GetCurrentHopField and GetCurrentInfoField, called during parsePath:
  Both return pointer types. Should be possible to change this to value
  types instead, but requires larger changes.
- DataPlane.resolveLocalDst: allocates IP and UDPAddr. Can only really
  be fixed with better address types.
- slayers/path.FullMAC. Allocates both input and output. The function
  interface could be changed to allow reuse of the input buffer.
  The output buffer cannot be reused with this CMAC library.

Depends on (or actually includes) scionproto#4029.

Closes scionproto#4030

GitOrigin-RevId: 72eb8918a54a25f74159ef7398ef3d98d756e501
lukedirtwalker pushed a commit to lukedirtwalker/scion that referenced this pull request Jul 14, 2021
This PR extends scionproto#4030 with the suggested preallocation of the
SCION MAC input buffer. For this, FullMAC is changed to accept
the input buffer as argument.
Also the EPIC MAC buffers are now preallocated (both input and
output buffers, and the zero initialization vector). Only the AES-
cipher is still allocated for each packet. This is however
inherent to EPIC, because every packet can potentially have
a different authenticator (= key for the AES cipher).

Closes scionproto#4053

GitOrigin-RevId: ed2b273bb91f62c17d1deb6c17c831ed4a9cdb5e
matzf pushed a commit to netsec-ethz/scion that referenced this pull request Jul 14, 2021
This PR extends scionproto#4030 with the suggested preallocation of the
SCION MAC input buffer. For this, FullMAC is changed to accept
the input buffer as argument.
Also the EPIC MAC buffers are now preallocated (both input and
output buffers, and the zero initialization vector). Only the AES-
cipher is still allocated for each packet. This is however
inherent to EPIC, because every packet can potentially have
a different authenticator (= key for the AES cipher).

Closes scionproto#4053

GitOrigin-RevId: ed2b273bb91f62c17d1deb6c17c831ed4a9cdb5e
@matzf matzf deleted the router-reduce-alloc branch August 20, 2021 18:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants