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

feat(pcapgo): support cancellation with context.Context #12

Merged

Conversation

prskr
Copy link
Contributor

@prskr prskr commented Feb 15, 2023

I originally prepared this PR for the google/gopacket repo.
Although it's already quite a while I finally managed to re-create it to (hopefully) merge it now 😄

It's mostly about adding support for context.Context but I also optimized some small things to avoid allocations in hot paths:

  • re-order the IPv4 header parsing
  • NewZeroCopyPacketSource

I'm using these changes already for quite some time so I'm 'optimistic' they're working as expected 😄

@prskr prskr force-pushed the feature/packetsource-packets-context-support branch from 2b73dc4 to d4f7dea Compare February 15, 2023 18:18
@mosajjal
Copy link
Contributor

Haven't read through it yet (on the phone) how much overhead doea this add?

@prskr
Copy link
Contributor Author

prskr commented Feb 16, 2023

What do you mean how much overhead does this add?

Edit: the context.Context support basically nothing. It's only used in a single loop which was previously unconditionally 😅

There was an issue of properly closing a packet source which didn't unlock the reading goroutine I resolved.

The re-ordering I honestly can't remember but I also don't think it really hurts because it basically only makes sure all the error cases are handled before the remaining information is extracted.

Regarding the 'zero allocation' + pooled packets: this was a huge improvement in my experience. I used to work in a company which is recording PCAPs all day long for analytics and it improved the overall memory consumption 10x so roughly from 250-300MB to 25-40MB

@prskr
Copy link
Contributor Author

prskr commented Feb 16, 2023

I just realized I might've missed one additional improvement for the context.Context support to unlock the read operation and directly return. Will check that tomorrow 😊

"syscall"
"time"
)

const maximumMTU = 1500
Copy link
Contributor

Choose a reason for hiding this comment

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

wondering if this creates any limitation for packets in lo or jumboframes?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

that's actually the previous value only in a constant instead of having it hard coded 😄

Copy link
Contributor

@mosajjal mosajjal left a comment

Choose a reason for hiding this comment

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

from what I understand, the API for packetReader won't change. eg any existing code should work with context.Background(). is that right?

"testing"
"time"

"github.com/vishvananda/netlink"
Copy link
Contributor

Choose a reason for hiding this comment

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

in my opinon, if we need to have the netlink dependency, we should make it clear that it must stay contained within testing.

Copy link
Contributor Author

@prskr prskr Feb 17, 2023

Choose a reason for hiding this comment

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

sure, although it's not new, it's in the go.mod since June 21 😅

@prskr
Copy link
Contributor Author

prskr commented Feb 17, 2023

Yes, that's right. No public API is touched.

It could make sense to either make a breaking change or introduce similar overloads for:

  • func (p *PacketSource) NextPacket() (Packet, error) like func (p *PacketSource) NextPacketCtx(ctx context.Context) (Packet, error)
  • ZeroCopyReadPacketData() (data []byte, ci CaptureInfo, err error) to ZeroCopyReadPacketDataCtx(ctx context.Context) (data []byte, ci CaptureInfo, err error)
  • ReadPacketData() (data []byte, ci CaptureInfo, err error) to ReadPacketDataCtx(ctx context.Context) (data []byte, ci CaptureInfo, err error)

to be able to unblock these routines as well but not really sure how to proceed with that

@mosajjal
Copy link
Contributor

for the time being, not touching the API is a better call. is this ready for merge?

@prskr
Copy link
Contributor Author

prskr commented Feb 21, 2023

Not a problem, just wanted to mention it 😄
If you don't have any further questions/objections I'd say this is ready, yes 😄
Tests are passing (also the one in my project where I'm already using my fork) so from my side I'm all set 😊

@mosajjal
Copy link
Contributor

yep tests are passing so hopefully nothing will break :) if it does, we need better tests :P

@mosajjal mosajjal merged commit 9860005 into gopacket:master Feb 22, 2023
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