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

Fix PacketCapture pcapng file issue on macOS #6804

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

hangyan
Copy link
Member

@hangyan hangyan commented Nov 12, 2024

By default, gopacket will write snap length=0 in the pcapng file header, means unlimited snaplen. tcpdump on osx(libpcap version 1.10.1) cannot recognize this and will report error. This patch will set a default value(524288) for it. This patch also add packets file verification in e2e tests.

origin error message:

tcpdump: pcap_loop: invalid packet capture length 74, bigger than snaplen of 524288

pcapng format ref: https://pcapng.com/

@hangyan hangyan requested a review from tnqn November 12, 2024 04:21
@luolanzone luolanzone added kind/bug Categorizes issue or PR as related to a bug. action/backport Indicates a PR that requires backports. labels Nov 12, 2024
By default, gopacket will write snap length=0 in the pcapng file
header, means unlimited snaplen. tcpdump on osx(libpcap version 1.10.1)
cannot recognize this and will report error. This patch will set
a default value(524288) for it.

Signed-off-by: Hang Yan <[email protected]>
@antoninbas antoninbas changed the title Fix PacketCpature packets unrecognized issue on osx. Fix PacketCapture pcapng file issue on macOS Nov 12, 2024
pkg/agent/packetcapture/packetcapture_controller.go Outdated Show resolved Hide resolved
pkg/agent/packetcapture/packetcapture_controller.go Outdated Show resolved Hide resolved
test/e2e/packetcapture_test.go Outdated Show resolved Hide resolved
test/e2e/packetcapture_test.go Outdated Show resolved Hide resolved
test/e2e/packetcapture_test.go Outdated Show resolved Hide resolved
test/e2e/packetcapture_test.go Outdated Show resolved Hide resolved
@luolanzone luolanzone added the action/release-note Indicates a PR that should be included in release notes. label Nov 13, 2024
test/e2e/packetcapture_test.go Outdated Show resolved Hide resolved
test/e2e/packetcapture_test.go Outdated Show resolved Hide resolved
test/e2e/packetcapture_test.go Outdated Show resolved Hide resolved
Signed-off-by: Hang Yan <[email protected]>
// 0 and means unlimited, but tcpdump on Mac OSX will complain:
// 'tcpdump: pcap_loop: invalid packet capture length <len>, bigger than snaplen of 524288'
ngInterface := pcapgo.DefaultNgInterface
ngInterface.SnapLength = 65536
Copy link
Contributor

Choose a reason for hiding this comment

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

do you think the snapshot length could be a parameter to the Capture interface method, after which snapLength could be defined as a constant in this file, and we could use the constant here, as well as when invoking Capture?

Comment on lines +622 to +625
defer func() {
defer file.Close()
defer os.Remove(dstFileName)
}()
Copy link
Contributor

Choose a reason for hiding this comment

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

what is the point of having 2 defer statements in a deferred function?

Couldn't the code be like this:

require.NoError(t, data.copyPodFiles(antreaPodName, "antrea-agent", "kube-system", packetFile, os.TempDir()))
defer os.Remove(dstFileName)
file, err := os.Open(dstFileName)
require.NoError(t, err)
defer file.Close()

this would close the file first, then remove it (reversed order of execution for deferred statements)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
action/backport Indicates a PR that requires backports. action/release-note Indicates a PR that should be included in release notes. kind/bug Categorizes issue or PR as related to a bug.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants