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

Add bidirectional packet capture #6882

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
4 changes: 3 additions & 1 deletion build/yamls/antrea.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3035,7 +3035,9 @@ spec:
type: integer
minimum: 1
maximum: 65535

bidirection:
type: boolean
default: false
timeout:
type: integer
minimum: 1
Expand Down
42 changes: 39 additions & 3 deletions pkg/agent/packetcapture/packetcapture_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,10 +464,22 @@ func (c *Controller) performCapture(
}
defer pcapngWriter.Flush()
updateRateLimiter := rate.NewLimiter(rate.Every(captureStatusUpdatePeriod), 1)
packets, err := c.captureInterface.Capture(ctx, device, snapLen, srcIP, dstIP, pc.Spec.Packet)
if err != nil {
return false, err

var packets chan gopacket.Packet
if pc.Spec.Bidirection {
reqpackets, err1 := c.captureInterface.Capture(ctx, device, snapLen, srcIP, dstIP, pc.Spec.Packet)
respackets, err2 := c.captureInterface.Capture(ctx, device, snapLen, dstIP, srcIP, pc.Spec.Packet)
AryanBakliwal marked this conversation as resolved.
Show resolved Hide resolved
if err1 != nil || err2 != nil {
return false, fmt.Errorf("failed to set up bidirectional capture: %v, %v", err1, err2)
}
packets = c.mergeChannels(reqpackets, respackets)
} else {
packets, err = c.captureInterface.Capture(ctx, device, snapLen, srcIP, dstIP, pc.Spec.Packet)
if err != nil {
return false, err
}
}

// Track whether any packet is captured.
capturedAny := false
for {
Expand Down Expand Up @@ -503,6 +515,30 @@ func (c *Controller) performCapture(
}
}

func (c *Controller) mergeChannels(ch1, ch2 chan gopacket.Packet) chan gopacket.Packet {
merged := make(chan gopacket.Packet)
go func() {
defer close(merged)
for ch1 != nil || ch2 != nil {
select {
case pkt, ok := <-ch1:
if ok {
merged <- pkt
} else {
ch1 = nil
}
case pkt, ok := <-ch2:
if ok {
merged <- pkt
} else {
ch2 = nil
}
}
}
}()
return merged
}

func (c *Controller) getPodIP(ctx context.Context, podRef *crdv1alpha1.PodReference) (net.IP, error) {
podInterfaces := c.interfaceStore.GetContainerInterfacesByPod(podRef.Name, podRef.Namespace)
var podIP net.IP
Expand Down
2 changes: 2 additions & 0 deletions pkg/apis/crd/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,8 @@ type PacketCaptureSpec struct {
// for a capture session, and at least one `Pod` should be present either in the source or the destination.
Source Source `json:"source"`
Destination Destination `json:"destination"`
// Bidirection specifies whether to capture the return (response) traffic from the destination back to the source.
Bidirection bool `json:"bidirection"`
AryanBakliwal marked this conversation as resolved.
Show resolved Hide resolved
// Packet defines what kind of traffic we want to capture between the source and destination. If not specified,
// all kinds of traffic will count.
Packet *Packet `json:"packet,omitempty"`
Expand Down