Chapter 3. Getting started with XDP and eBPF
The eXpress Data Path (XDP) and the extended Berkeley Packet Filter (eBPF) features are a powerful combination for high-speed networks. XDP provides a high-performance framework for early packet processing, while eBPF allows safe, dynamic program execution in the kernel.
The synergy of XDP and eBPF improves network performance and enables robust traffic tracing, filtering, and monitoring.
3.1. Understanding the eBPF networking features in RHEL 10 Copy linkLink copied to clipboard!
The extended Berkeley Packet Filter (eBPF) is an in-kernel virtual machine that allows code execution in the kernel space. This code runs in a restricted sandbox environment with access only to a limited set of functions.
In networking, you can use eBPF to complement or replace kernel packet processing. Depending on the hook you use, eBPF programs have, for example:
- Read and write access to packet data and metadata
- Can look up sockets and routes
- Can set socket options
- Can redirect packets
3.1.1. Overview of networking eBPF features in RHEL 10 Copy linkLink copied to clipboard!
The Extended Berkeley Packet Filter (eBPF) lets developers run sandboxed programs in the Linux kernel. For networking, eBPF programs attach to hooks to inspect, modify, and filter traffic.
You can attach extended Berkeley Packet Filter (eBPF) networking programs to the following hooks in RHEL:
- eXpress Data Path (XDP): Provides early access to received packets before the kernel networking stack processes them.
-
tceBPF classifier with a direct-action flag: Provides powerful packet processing on ingress and egress. Programs can be attached as an eBPF classifier with a direct-action flag in theqdischierarchy, or using the link-basedtcxAPI. - Control Groups version 2 (cgroup v2): Enables filtering and overriding socket-based operations performed by programs in a control group.
- Socket filtering: Enables filtering of packets received from sockets. This feature was also available in the classic Berkeley Packet Filter (cBPF), but has been extended to support eBPF programs.
- Stream parser: Enables splitting up streams to individual messages, filtering, and redirecting them to sockets.
-
SO_REUSEPORTsocket selection: Provides a programmable selection of a receiving socket from areuseportsocket group. - Flow dissector: Enables overriding the way the kernel parses packet headers in certain situations.
- TCP congestion control callbacks: Enables implementing a custom TCP congestion control algorithm.
- Routes with encapsulation: Enables creating custom tunnel encapsulation.
Netfilter: Enables implementing custom Netfilter hooks.
- XDP
You can attach programs of the
BPF_PROG_TYPE_XDPtype to a network interface. The kernel then executes the program on received packets before the kernel network stack starts processing them. This allows fast packet forwarding in certain situations, such as fast packet dropping to prevent distributed denial of service (DDoS) attacks and fast packet redirects for load balancing scenarios.You can also use XDP for different forms of packet monitoring and sampling. The kernel allows XDP programs to modify packets and to pass them for further processing to the kernel network stack.
The following XDP modes are available:
- Native (driver) XDP: The kernel executes the program from the earliest possible point during packet reception. At this moment, the kernel did not parse the packet and, therefore, no metadata provided by the kernel is available. This mode requires that the network interface driver supports XDP but not all drivers support this native mode.
- Generic XDP: The kernel network stack executes the XDP program early in the processing. At that time, kernel data structures have been allocated, and the packet has been pre-processed. If a packet should be dropped or redirected, it requires a significant overhead compared to the native mode. However, the generic mode does not require network interface driver support and works with all network interfaces.
- Offloaded XDP: The kernel executes the XDP program on the network interface instead of on the host CPU. Note that this requires specific hardware, and only certain eBPF features are available in this mode.
On RHEL, load all XDP programs using the
libxdplibrary. This library enables system-controlled usage of XDP.NoteCurrently, there are some system configuration limitations for XDP programs. For example, you must disable certain hardware offload features on the receiving interface. Additionally, not all features are available with all drivers that support the native mode.
In RHEL 10, Red Hat supports the XDP features only if you use the
libxdplibrary to load the program into the kernel.- AF_XDP
-
Using an XDP program that filters and redirects packets to a given
AF_XDPsocket, you can use one or more sockets from theAF_XDPprotocol family to quickly copy packets from the kernel to the user space. - Traffic Control
The Traffic Control (
tc) subsystem offers the following types of eBPF programs:-
BPF_PROG_TYPE_SCHED_CLS -
BPF_PROG_TYPE_SCHED_ACT
These types enable you to write custom
tcclassifiers andtcactions in eBPF. Together with the parts of thetcecosystem, this provides the ability for powerful packet processing and is the core part of several container networking orchestration solutions.In most cases, only the classifier is used, as with the direct-action flag, the eBPF classifier can execute actions directly from the same eBPF program. The
clsactQueueing Discipline (qdisc) has been designed to enable this on the ingress side.Note that using a flow dissector eBPF program can influence operation of some other
qdiscsandtcclassifiers, such asflower.The link-based
tcxAPI is provided along with theqdiscAPI. It enables your applications to maintain ownership over a BPF program to prevent accidental removal of the BPF program. Also, thetcxAPI has multiprogram support that allows multiple applications to attach BPF programs in thetclayer in parallel.-
- Socket filter
Several utilities use or have used the classic Berkeley Packet Filter (cBPF) for filtering packets received on a socket. For example, the
tcpdumputility enables the user to specify expressions, whichtcpdumpthen translates into cBPF code.As an alternative to cBPF, the kernel allows eBPF programs of the
BPF_PROG_TYPE_SOCKET_FILTERtype for the same purpose.- Control Groups
In RHEL, you can use multiple types of eBPF programs that you can attach to a cgroup. The kernel executes these programs when a program in the given cgroup performs an operation. Note that you can use only cgroups version 2.
The following networking-related cgroup eBPF programs are available in RHEL:
-
BPF_PROG_TYPE_SOCK_OPS: The kernel calls this program on various TCP events. The program can adjust the behavior of the kernel TCP stack, including custom TCP header options, and so on. -
BPF_PROG_TYPE_CGROUP_SOCK_ADDR: The kernel calls this program duringconnect,bind,sendto,recvmsg,getpeername, andgetsocknameoperations. This program allows changing IP addresses and ports. This is useful when you implement socket-based network address translation (NAT) in eBPF. -
BPF_PROG_TYPE_CGROUP_SOCKOPT: The kernel calls this program duringsetsockoptandgetsockoptoperations and allows changing the options. -
BPF_PROG_TYPE_CGROUP_SOCK: The kernel calls this program during socket creation, socket releasing, and binding to addresses. You can use these programs to allow or deny the operation, or only to inspect socket creation for statistics. -
BPF_PROG_TYPE_CGROUP_SKB: This program filters individual packets on ingress and egress, and can accept or reject packets.
-
- Stream Parser
A stream parser operates on a group of sockets that are added to a special eBPF map. The eBPF program then processes packets that the kernel receives or sends on those sockets.
The following stream parser eBPF programs are available in RHEL:
-
BPF_PROG_TYPE_SK_SKB: An eBPF program parses packets received on the socket into individual messages, and instructs the kernel to drop those messages, accept them, or send them to another socket. -
BPF_PROG_TYPE_SK_MSG: This program filters egress messages. An eBPF program parses the packets and either approves or rejects them.
-
- SO_REUSEPORT socket selection
-
Using this socket option, you can bind multiple sockets to the same IP address and port. Without eBPF, the kernel selects the receiving socket based on a connection hash. With the
BPF_PROG_TYPE_SK_REUSEPORTprogram, the selection of the receiving socket is fully programmable. - Flow dissector
-
When the kernel needs to process packet headers without going through the full protocol decode, they are
dissected. For example, this happens in thetcsubsystem, in multipath routing, in bonding, or when calculating a packet hash. In this situation the kernel parses the packet headers and fills internal structures with the information from the packet headers. You can replace this internal parsing using theBPF_PROG_TYPE_FLOW_DISSECTORprogram. Note that you can only dissect TCP and UDP over IPv4 and IPv6 in eBPF in RHEL. - TCP Congestion Control
-
You can write a custom TCP congestion control algorithm using a group of
BPF_PROG_TYPE_STRUCT_OPSprograms that implementstruct tcp_congestion_oopscallbacks. An algorithm that is implemented this way is available to the system alongside the built-in kernel algorithms. - Routes with encapsulation
You can attach one of the following eBPF program types to routes in the routing table as a tunnel encapsulation attribute:
-
BPF_PROG_TYPE_LWT_IN -
BPF_PROG_TYPE_LWT_OUT -
BPF_PROG_TYPE_LWT_XMIT -
BPF_PROG_TYPE_LWT_SEG6LOCAL(Technology Preview)
The functionality of such an eBPF program is limited to specific tunnel configurations and does not allow creating a generic encapsulation or decapsulation solution.
-
- Socket lookup
-
To bypass limitations of the
bindsystem call, use an eBPF program of theBPF_PROG_TYPE_SK_LOOKUPtype. Such programs can select a listening socket for new incoming TCP connections or an unconnected socket for UDP packets. - Netfilter
-
You can implement custom Netfilter hooks by using the
BPF_PROG_TYPE_NETFILTERtype. Such hooks integrate into the Netfilter infrastructure, have read-only access to packets, and can either drop or accept them. Optionally, the eBPF program can run on the defragmented packet after the reassembly of IP fragments.
3.1.2. Overview of XDP features in RHEL 10 by network cards Copy linkLink copied to clipboard!
The XDP features available in RHEL depend on the network card and their drivers.
The following is an overview of XDP-enabled network cards and the XDP features you can use with them:
| Network card | Driver | Red Hat support status | Basic | Redirect | Target | Zero-copy | Large MTU |
|---|---|---|---|---|---|---|---|
| Amazon Elastic Network Adapter |
| enabled | yes | yes | yes [a] | no | no |
| aQuantia AQtion Ethernet card |
| enabled | yes | yes | yes | no | yes |
| Broadcom NetXtreme-C/E 10/25/40/50 gigabit Ethernet |
| supported | yes | yes | yes [a] | no | yes |
| Cavium Thunder Virtual function |
| enabled | yes | no | no | no | no |
| Freescale DPAA2 Ethernet |
| enabled | yes | yes | yes | yes [b] | no |
| Google Virtual NIC (gVNIC) support |
| enabled | yes | yes | yes | yes | no |
| Intel® 10GbE PCI Express Virtual Function Ethernet |
| enabled | yes | no | no | no | no |
| Intel® 10GbE PCI Express adapters |
| enabled | yes | yes | yes [a] | yes | yes [c] |
| Intel® Ethernet Connection E800 Series |
| supported | yes | yes | yes [a] | yes | yes |
| Intel® Ethernet Controller I225-LM/I225-V family |
| supported | yes | yes | yes [a] | yes | yes [c] |
| Intel® PCI Express Gigabit adapters |
| enabled | yes | yes | yes [a] | no | yes [c] |
| Intel® Ethernet Controller XL710 Family |
| supported | yes | yes | yes | no | |
| Marvell OcteonTX2 |
| enabled | yes | yes | no | no | |
| Mellanox 5th generation network adapters (ConnectX series) |
| supported | yes | yes | yes [d] | yes | yes |
| Mellanox Technologies 1/10/40Gbit Ethernet |
| enabled | yes | yes | no | no | no |
| Microsoft Azure Network Adapter |
| enabled | yes | yes | yes | no | no |
| Microsoft Hyper-V virtual network |
| enabled | yes | yes | yes | no | no |
| Netronome® NFP4000/NFP6000 NIC [e] |
| enabled | yes | no | no | yes | no |
| NXP ENETC Gigabit Ethernet |
| enabled | yes | yes | yes | no | yes |
| NXP Fast Ethernet Controller |
| enabled | yes | yes | no | no | no |
| Pensando Ethernet Adapter |
| enabled | yes | yes | yes | no | yes |
| QEMU Virtio network |
| supported | yes | yes | yes [a] | no | yes |
| QLogic QED 25/40/100Gb Ethernet NIC |
| enabled | yes | yes | yes | no | no |
| QorIQ DPAA Ethernet |
| enabled | yes | yes | yes | no | no |
| STMicroelectronics Multi-Gigabit Ethernet |
| enabled | yes | yes | yes | yes | no |
| Solarflare SFC9000/SFC9100/EF100-family |
| enabled | yes | yes | yes [d] | no | no |
| Universal TUN/TAP device |
| enabled | yes | yes | yes | no | no |
| Virtual Ethernet pair device |
| supported | yes | yes | yes | no | yes |
| VMware VMXNET3 ethernet driver |
| enabled | yes | yes | no | no | |
| Xen paravirtual network device |
| enabled | yes | yes | yes | no | no |
[a]
Only if an XDP program is loaded on the interface.
[b]
Not available on all hardware revisions.
[c]
Transmitting side only. Cannot receive large packets through XDP.
[d]
Requires at least one XDP TX queue for every CPU, for example, the number of queues must be larger than the largest CPU index.
[e]
Some of the listed features are not available for the Netronome® NFP3800 NIC.
| |||||||
Legend:
Red Hat support status:
-
supported: Red Hat fully supports the XDP features provided by this driver. -
enabled: XDP functionality is provided by upstream drivers. Red Hat addresses identified issues on a best-effort basis, but the hardware is not subjected to the full suite of internal quality assurance tests.
-
-
Basic: Supports basic return codes:
DROP,PASS,ABORTED, andTX. -
Redirect: Supports the
XDP_REDIRECTreturn code. -
Target: Can be a target of a
XDP_REDIRECTreturn code. -
Zero-copy: Supports the zero-copy mode for the
AF_XDPprotocol family. - Large MTU: Supports packets larger than page size.
3.2. Network tracing using the BPF compiler collection Copy linkLink copied to clipboard!
BPF Compiler Collection (BCC) is a library, which facilitates the creation of the extended Berkeley Packet Filter (eBPF) programs. The main utility of eBPF programs is analyzing the operating system performance and network performance without experiencing overhead or security issues.
BCC removes the need for users to know deep technical details of eBPF, and provides many out-of-the-box starting points, such as the bcc-tools package with pre-created eBPF programs.
The eBPF programs are triggered on events, such as disk I/O, TCP connections, and process creations. It is unlikely that the programs should cause the kernel to crash, loop or become unresponsive because they run in a safe virtual machine in the kernel.
3.2.1. Installing the bcc-tools package Copy linkLink copied to clipboard!
Install the bcc-tools package, which also installs the BPF Compiler Collection (BCC) library as a dependency.
Procedure
Install
bcc-tools:# dnf install bcc-toolsThe BCC tools are installed in the
/usr/share/bcc/tools/directory.
Verification
Inspect the installed tools:
# ls -l /usr/share/bcc/tools/... -rwxr-xr-x. 1 root root 4198 Dec 14 17:53 dcsnoop -rwxr-xr-x. 1 root root 3931 Dec 14 17:53 dcstat -rwxr-xr-x. 1 root root 20040 Dec 14 17:53 deadlock_detector -rw-r--r--. 1 root root 7105 Dec 14 17:53 deadlock_detector.c drwxr-xr-x. 3 root root 8192 Mar 11 10:28 doc -rwxr-xr-x. 1 root root 7588 Dec 14 17:53 execsnoop -rwxr-xr-x. 1 root root 6373 Dec 14 17:53 ext4dist -rwxr-xr-x. 1 root root 10401 Dec 14 17:53 ext4slower ...The
docdirectory in the listing above contains documentation for each tool.
3.2.2. Displaying TCP connections added to the Kernel’s accept queue Copy linkLink copied to clipboard!
By using the tcpaccept utility, you can monitor newly accepted connections by tracing the kernel’s accept() function.
After the kernel receives the ACK packet in a TCP 3-way handshake, the kernel moves the connection from the SYN queue to the accept queue after the connection’s state changes to ESTABLISHED. Therefore, only successful TCP connections are visible in this queue.
The tcpaccept utility uses eBPF features to display all connections the kernel adds to the accept queue. The utility is lightweight because it traces the accept() function of the kernel instead of capturing packets and filtering them. For example, use tcpaccept for general troubleshooting to display new connections the server has accepted.
Procedure
Enter the following command to start the tracing the kernel
acceptqueue:# /usr/share/bcc/tools/tcpaccept PID COMM IP RADDR RPORT LADDR LPORT 843 sshd 4 192.0.2.17 50598 192.0.2.1 22 1107 ns-slapd 4 198.51.100.6 38772 192.0.2.1 389 1107 ns-slapd 4 203.0.113.85 38774 192.0.2.1 389 ...Each time the kernel accepts a connection,
tcpacceptdisplays the details of the connections.For further details, see the
tcpaccept(8)man page and the/usr/share/bcc/tools/doc/tcpaccept_example.txtfile on your system.- Press Ctrl+C to stop the tracing process.
3.2.3. Tracing outgoing TCP connection attempts Copy linkLink copied to clipboard!
The tcpconnect utility uses eBPF features to trace outgoing TCP connection attempts. The output of the utility also includes connections that failed.
The tcpconnect utility is lightweight because it traces, for example, the connect() function of the kernel instead of capturing packets and filtering them.
Procedure
Enter the following command to start the tracing process that displays all outgoing connections:
# /usr/share/bcc/tools/tcpconnect PID COMM IP SADDR DADDR DPORT 31346 curl 4 192.0.2.1 198.51.100.16 80 31348 telnet 4 192.0.2.1 203.0.113.231 23 31361 isc-worker00 4 192.0.2.1 192.0.2.254 53 ...Each time the kernel processes an outgoing connection,
tcpconnectdisplays the details of the connections.For further details, see the
tcpconnect(8)man page and the/usr/share/bcc/tools/doc/tcpconnect_example.txtfile on your system.- Press Ctrl+C to stop the tracing process.
3.2.4. Measuring the latency of outgoing TCP connections Copy linkLink copied to clipboard!
The tcpconnlat utility uses eBPF features to measure the time between a sent SYN packet and the received response packet.
The TCP connection latency is the time taken to establish a connection. This typically involves the kernel TCP/IP processing and network round trip time, and not the application runtime.
Procedure
Start measuring the latency of outgoing connections:
# /usr/share/bcc/tools/tcpconnlat PID COMM IP SADDR DADDR DPORT LAT(ms) 32151 isc-worker00 4 192.0.2.1 192.0.2.254 53 0.60 32155 ssh 4 192.0.2.1 203.0.113.190 22 26.34 32319 curl 4 192.0.2.1 198.51.100.59 443 188.96 ...Each time the kernel processes an outgoing connection,
tcpconnlatdisplays the details of the connection after the kernel receives the response packet.For further details, see the
tcpconnlat(8)man page and the/usr/share/bcc/tools/doc/tcpconnlat_example.txtfile on your system.- Press Ctrl+C to stop the tracing process.
3.2.5. Displaying details about TCP packets and segments that were dropped by the kernel Copy linkLink copied to clipboard!
The tcpdrop utility enables administrators to display details about TCP packets and segments that were dropped by the kernel. Use this utility to debug high rates of dropped packets that can cause the remote system to send timer-based retransmits.
High rates of dropped packets and segments can impact the performance of a server. Instead of capturing and filtering packets, which is resource-intensive, the tcpdrop utility uses eBPF features to retrieve the information directly from the kernel.
Procedure
Enter the following command to start displaying details about dropped TCP packets and segments:
# /usr/share/bcc/tools/tcpdrop TIME PID IP SADDR:SPORT > DADDR:DPORT STATE (FLAGS) 13:28:39 32253 4 192.0.2.85:51616 > 192.0.2.1:22 CLOSE_WAIT (FIN|ACK) b'tcp_drop+0x1' b'tcp_data_queue+0x2b9' ... 13:28:39 1 4 192.0.2.85:51616 > 192.0.2.1:22 CLOSE (ACK) b'tcp_drop+0x1' b'tcp_rcv_state_process+0xe2' ...Each time the kernel drops TCP packets and segments,
tcpdropdisplays the details of the connection, including the kernel stack trace that led to the dropped package.For further details, see the
tcpdrop(8)man page and the/usr/share/bcc/tools/doc/tcpdrop_example.txtfile on your system.- Press Ctrl+C to stop the tracing process.
3.2.6. Tracing TCP sessions Copy linkLink copied to clipboard!
The tcplife utility uses eBPF to trace TCP sessions that open and close, and prints a line of output to summarize each one. Administrators can use tcplife to identify connections and the amount of transferred traffic.
For example, you can display connections to port 22 (SSH) to retrieve the following information:
- The local process ID (PID)
- The local process name
- The local IP address and port number
- The remote IP address and port number
- The amount of received and transmitted traffic in KB.
- The time in milliseconds the connection was active
Procedure
Enter the following command to start the tracing of connections to the local port
22:# /usr/share/bcc/tools/tcplife -L 22 PID COMM LADDR LPORT RADDR RPORT TX_KB RX_KB MS 19392 sshd 192.0.2.1 22 192.0.2.17 43892 53 52 6681.95 19431 sshd 192.0.2.1 22 192.0.2.245 43902 81 249381 7585.09 19487 sshd 192.0.2.1 22 192.0.2.121 43970 6998 7 16740.35 ...Each time a connection is closed,
tcplifedisplays the details of the connections.For further details, see the
tcplife(8)man page and the/usr/share/bcc/tools/doc/tcplife_example.txtfile on your system.- Press Ctrl+C to stop the tracing process.
3.2.7. Tracing TCP retransmissions Copy linkLink copied to clipboard!
The tcpretrans utility displays details about TCP retransmissions, such as the local and remote IP address and port number, as well as the TCP state at the time of the retransmissions.
The utility uses eBPF features and, therefore, has a very low overhead.
Procedure
Use the following command to start displaying TCP retransmission details:
# /usr/share/bcc/tools/tcpretrans TIME PID IP LADDR:LPORT T> RADDR:RPORT STATE 00:23:02 0 4 192.0.2.1:22 R> 198.51.100.0:26788 ESTABLISHED 00:23:02 0 4 192.0.2.1:22 R> 198.51.100.0:26788 ESTABLISHED 00:45:43 0 4 192.0.2.1:22 R> 198.51.100.0:17634 ESTABLISHED ...Each time the kernel calls the TCP retransmit function,
tcpretransdisplays the details of the connection.For further details, see the
tcpretrans(8)man page and the/usr/share/bcc/tools/doc/tcpretrans_example.txtfile on your system.- Press Ctrl+C to stop the tracing process.
3.2.8. Displaying TCP state change information Copy linkLink copied to clipboard!
During a TCP session, the TCP state changes. The tcpstates utility uses eBPF functions to trace these state changes, and prints details including the duration in each state. For example, use tcpstates to identify if connections spend too much time in the initialization state.
Procedure
Use the following command to start tracing TCP state changes:
# /usr/share/bcc/tools/tcpstates SKADDR C-PID C-COMM LADDR LPORT RADDR RPORT OLDSTATE -> NEWSTATE MS ffff9cd377b3af80 0 swapper/1 0.0.0.0 22 0.0.0.0 0 LISTEN -> SYN_RECV 0.000 ffff9cd377b3af80 0 swapper/1 192.0.2.1 22 192.0.2.45 53152 SYN_RECV -> ESTABLISHED 0.067 ffff9cd377b3af80 818 sssd_nss 192.0.2.1 22 192.0.2.45 53152 ESTABLISHED -> CLOSE_WAIT 65636.773 ffff9cd377b3af80 1432 sshd 192.0.2.1 22 192.0.2.45 53152 CLOSE_WAIT -> LAST_ACK 24.409 ffff9cd377b3af80 1267 pulseaudio 192.0.2.1 22 192.0.2.45 53152 LAST_ACK -> CLOSE 0.376 ...Each time a connection changes its state,
tcpstatesdisplays a new line with updated connection details.If multiple connections change their state at the same time, use the socket address in the first column (
SKADDR) to determine which entries belong to the same connection.For further details, see the
tcpstates(8)man page and the/usr/share/bcc/tools/doc/tcpstates_example.txtfile on your system.- Press Ctrl+C to stop the tracing process.
3.2.9. Summarizing and aggregating TCP traffic sent to specific subnets Copy linkLink copied to clipboard!
The tcpsubnet utility summarizes and aggregates IPv4 TCP traffic that the local host sends to subnets and displays the output on a fixed interval. The utility uses eBPF features to collect and summarize the data to reduce the overhead.
By default, tcpsubnet summarizes traffic for the following subnets:
-
127.0.0.1/32 -
10.0.0.0/8 -
172.16.0.0/12 -
192.0.2.0/24/16 -
0.0.0.0/0
Note that the last subnet (0.0.0.0/0) is a catch-all option. The tcpsubnet utility counts all traffic for subnets different from the first four in this catch-all entry.
Follow the procedure to count the traffic for the 192.0.2.0/24 and 198.51.100.0/24 subnets. Traffic to other subnets will be tracked in the 0.0.0.0/0 catch-all subnet entry.
Procedure
Start monitoring the amount of traffic sent to the
192.0.2.0/24,198.51.100.0/24, and other subnets:# /usr/share/bcc/tools/tcpsubnet 192.0.2.0/24,198.51.100.0/24,0.0.0.0/0 Tracing... Output every 1 secs. Hit Ctrl-C to end [02/21/20 10:04:50] 192.0.2.0/24 856 198.51.100.0/24 7467 [02/21/20 10:04:51] 192.0.2.0/24 1200 198.51.100.0/24 8763 0.0.0.0/0 673 ...This command displays the traffic in bytes for the specified subnets once per second.
For further details, see the
tcpsubnet(8)man page and the/usr/share/bcc/tools/doc/tcpsubnet.txtfile on your system.- Press Ctrl+C to stop the tracing process.
3.2.10. Displaying the network throughput by IP address and port Copy linkLink copied to clipboard!
The tcptop utility displays TCP traffic the host sends and receives in kilobytes. The report automatically refreshes and contains only active TCP connections. The utility uses eBPF features and, therefore, has only a very low overhead.
Procedure
To monitor the sent and received traffic, enter:
# /usr/share/bcc/tools/tcptop 13:46:29 loadavg: 0.10 0.03 0.01 1/215 3875 PID COMM LADDR RADDR RX_KB TX_KB 3853 3853 192.0.2.1:22 192.0.2.165:41838 32 102626 1285 sshd 192.0.2.1:22 192.0.2.45:39240 0 0 ...The output of the command includes only active TCP connections. If the local or remote system closes a connection, the connection is no longer visible in the output.
For further details, see the
tcptop(8)man page and the/usr/share/bcc/tools/doc/tcptop.txtfile on your system.- Press Ctrl+C to stop the tracing process.
3.2.11. Tracing established TCP connections Copy linkLink copied to clipboard!
The tcptracer utility traces the kernel functions that connect, accept, and close TCP connections. The utility uses eBPF features and, therefore, has a very low overhead.
Procedure
Use the following command to start the tracing process:
# /usr/share/bcc/tools/tcptracer Tracing TCP established connections. Ctrl-C to end. T PID COMM IP SADDR DADDR SPORT DPORT A 1088 ns-slapd 4 192.0.2.153 192.0.2.1 0 65535 A 845 sshd 4 192.0.2.1 192.0.2.67 22 42302 X 4502 sshd 4 192.0.2.1 192.0.2.67 22 42302 ...Each time the kernel connects, accepts, or closes a connection,
tcptracerdisplays the details of the connections.For further details, see the
tcptracer(8)man page and the/usr/share/bcc/tools/doc/tcptracer_example.txtfile on your system.- Press Ctrl+C to stop the tracing process.
3.2.12. Tracing IPv4 and IPv6 listen attempts Copy linkLink copied to clipboard!
The solisten utility traces all IPv4 and IPv6 listen attempts. It traces the attempts including which ultimately fail or the listening program that does not accept the connection. The utility traces functions that the kernel calls when a program wants to listen for TCP connections.
Procedure
Enter the following command to start the tracing process that displays all listen TCP attempts:
# /usr/share/bcc/tools/solisten PID COMM PROTO BACKLOG PORT ADDR 3643 nc TCPv4 1 4242 0.0.0.0 3659 nc TCPv6 1 4242 2001:db8:1::1 4221 redis-server TCPv6 128 6379 :: 4221 redis-server TCPv4 128 6379 0.0.0.0 ....For further details, see the
solisten(9)man page and the/usr/share/bcc/tools/doc/solisten_example.txtfile on your system.- Press Ctrl+C to stop the tracing process.
3.2.13. Summarizing the service time of soft interrupts Copy linkLink copied to clipboard!
The softirqs utility summarizes the time spent servicing soft interrupts (soft IRQs) and shows this time as either totals or histogram distributions. This utility uses the irq:softirq_enter and irq:softirq_exit kernel tracepoints, which is a stable tracing mechanism.
Procedure
Enter the following command to start the tracing
soft irqevent time:# /usr/share/bcc/tools/softirqs Tracing soft irq event time... Hit Ctrl-C to end. ^C SOFTIRQ TOTAL_usecs tasklet 166 block 9152 net_rx 12829 rcu 53140 sched 182360 timer 306256For further details, see the
softirqs(8)andmpstat(1)man pages and the/usr/share/bcc/tools/doc/softirqs_example.txtfile on your system.- Press Ctrl+C to stop the tracing process.
3.2.14. Summarizing packets size and count on a network interface Copy linkLink copied to clipboard!
The netqtop utility displays statistics about the attributes of received (RX) and transmitted (TX) packets on each network queue of a particular network interface.
The statistics include:
- Bytes per second (BPS)
- Packets per second (PPS)
- The average packet size
- Total number of packets
To generate these statistics, netqtop traces the kernel functions that perform events of transmitted packets net_dev_start_xmit and received packets netif_receive_skb.
Procedure
Display the number of packets within the range of bytes size of the time interval of
2seconds:# /usr/share/bcc/tools/netqtop -n enp1s0 -i 2 Fri Jan 31 18:08:55 2023 TX QueueID avg_size [0, 64) [64, 512) [512, 2K) [2K, 16K) [16K, 64K) 0 0 0 0 0 0 0 Total 0 0 0 0 0 0 RX QueueID avg_size [0, 64) [64, 512) [512, 2K) [2K, 16K) [16K, 64K) 0 38.0 1 0 0 0 0 Total 38.0 1 0 0 0 0 ----------------------------------------------------------------------------- Fri Jan 31 18:08:57 2023 TX QueueID avg_size [0, 64) [64, 512) [512, 2K) [2K, 16K) [16K, 64K) 0 0 0 0 0 0 0 Total 0 0 0 0 0 0 RX QueueID avg_size [0, 64) [64, 512) [512, 2K) [2K, 16K) [16K, 64K) 0 38.0 1 0 0 0 0 Total 38.0 1 0 0 0 0 -----------------------------------------------------------------------------For further details, see the
netqtop(8)man page and the/usr/share/bcc/tools/doc/netqtop_example.txtfile on your system.-
Press Ctrl+C to stop
netqtop.
3.3. Using xdp-filter for high-performance traffic filtering to prevent DDoS attacks Copy linkLink copied to clipboard!
The xdp-filter utility uses XDP to drop packets directly at the network interface, making it faster than filters, such as nftables. It helps block traffic quickly during DDoS attacks and you can filter by IP, MAC address, or port.
For example, during testing, Red Hat dropped 26 million network packets per second on a single core, which is significantly higher than the drop rate of nftables on the same hardware.
The xdp-filter utility allows or drops incoming network packets by using XDP. You can create rules to filter traffic to or from specific:
- IP addresses
- MAC addresses
- Ports
Note that, even if xdp-filter has a significantly higher packet-processing rate, it does not have the same capabilities as, for example, nftables. Consider xdp-filter a conceptual utility to demonstrate packet filtering by using XDP. Additionally, you can use the code of the utility for a better understanding of how to write your own XDP applications.
On other architectures than AMD and Intel 64-bit, the xdp-filter utility is provided as a Technology Preview only. Technology Preview features are not supported with Red Hat production Service Level Agreements (SLAs), might not be functionally complete, and Red Hat does not recommend using them for production. These previews provide early access to upcoming product features, enabling customers to test functionality and provide feedback during the development process.
3.3.1. Dropping network packets that match an xdp-filter rule Copy linkLink copied to clipboard!
The xdp-filter utility can use an allow policy that allows all traffic except packets that match specific rules, such as those from a particular source IP address, source MAC address, or to a specific destination port.
Prerequisites
-
The
xdp-toolspackage is installed. - A network driver that supports XDP programs.
Procedure
Load
xdp-filterto process incoming packets on a certain interface, such asenp1s0:# xdp-filter load enp1s0By default,
xdp-filteruses theallowpolicy, and the utility drops only traffic that matches any rule.Optional: Use the
-f <feature>option to enable only particular features, such astcp,ipv4, orethernet. Loading only the required features instead of all of them increases the speed of packet processing. To enable multiple features, separate them with a comma.If the command fails with an error, the network driver does not support XDP programs.
Add rules to drop packets that match them. For example:
To drop incoming packets to port
22, enter:# xdp-filter port 22This command adds a rule that matches TCP and UDP traffic. To match only a particular protocol, use the
-p protocoloption.To drop incoming packets from
192.0.2.1, enter:# xdp-filter ip 192.0.2.1 -m srcNote that
xdp-filterdoes not support IP ranges.To drop incoming packets from MAC address
00:53:00:AA:07:BE, enter:# xdp-filter ether 00:53:00:AA:07:BE -m src
Verification
Use the following command to display statistics about dropped and allowed packets:
# xdp-filter statusIf you are a developer and interested in the code of
xdp-filter, download and install the corresponding source RPM (SRPM) from the Red Hat Customer Portal.
3.3.2. Dropping all network packets except the ones that match an xdp-filter rule Copy linkLink copied to clipboard!
The xdp-filter utility can use a deny policy that drops all traffic except packets that match specific rules, such as those from a particular source IP address, source MAC address, or to a specific destination port.
If you set the default policy to deny when you load xdp-filter on an interface, the kernel immediately drops all packets from this interface until you create rules that allow certain traffic. To avoid being locked out from the system, enter the commands locally or connect through a different network interface to the host.
Prerequisites
-
The
xdp-toolspackage is installed. - You are logged in to the host either locally or by using a network interface for which you do not plan to filter the traffic.
- A network driver that supports XDP programs.
Procedure
Load
xdp-filterto process packets on a certain interface, such asenp1s0:# xdp-filter load enp1s0 -p denyOptional: Use the
-f <feature>option to enable only particular features, such astcp,ipv4, orethernet. Loading only the required features instead of all of them increases the speed of packet processing. To enable multiple features, separate them with a comma.If the command fails with an error, the network driver does not support XDP programs.
Add rules to allow packets that match them. For example:
To allow packets to port
22, enter:# xdp-filter port 22This command adds a rule that matches TCP and UDP traffic. To match only a particular protocol, pass the
-p protocoloption to the command.To allow packets to
192.0.2.1, enter:# xdp-filter ip 192.0.2.1Note that
xdp-filterdoes not support IP ranges.To allow packets to MAC address
00:53:00:AA:07:BE, enter:# xdp-filter ether 00:53:00:AA:07:BE
ImportantThe
xdp-filterutility does not support stateful packet inspection. This requires that you either do not set a mode using the-m modeoption or you add explicit rules to allow incoming traffic that the machine receives in reply to outgoing traffic.
Verification
Use the following command to display statistics about dropped and allowed packets:
# xdp-filter statusIf you are a developer and you are interested in the code of
xdp-filter, download and install the corresponding source RPM (SRPM) from the Red Hat Customer Portal.
3.4. Using xdpdump to capture network packets including packets dropped by XDP programs Copy linkLink copied to clipboard!
The xdpdump utility captures network packets, including those dropped or modified by XDP programs. Unlike tcpdump, xdpdump uses an eBPF program, allowing it to capture packets that traditional user-space utilities cannot.
You can use xdpdump to debug XDP programs that are already attached to an interface. Therefore, the utility can capture packets before an XDP program is started and after it has finished. In the latter case, xdpdump also captures the XDP action. By default, xdpdump captures incoming packets at the entry of the XDP program.
On other architectures than AMD and Intel 64-bit, the xdpdump utility is provided as a Technology Preview only. Technology Preview features are not supported with Red Hat production Service Level Agreements (SLAs), might not be functionally complete, and Red Hat does not recommend using them for production. These previews provide early access to upcoming product features, enabling customers to test functionality and provide feedback during the development process.
See Technology Preview Features Support Scope on the Red Hat Customer Portal for information about the support scope for Technology Preview features.
Note that xdpdump has no packet filter or decode capabilities. However, you can use it in combination with tcpdump for packet decoding.
Prerequisites
- A network driver that supports XDP programs.
-
An XDP program is loaded to the
enp1s0interface. If no program is loaded,xdpdumpcaptures packets in a similar waytcpdumpdoes, for backward compatibility.
Procedure
To capture packets on the
enp1s0interface and write them to the/root/capture.pcapfile, enter:# xdpdump -i enp1s0 -w /root/capture.pcapIf you are a developer and you are interested in the source code of
xdpdump, download and install the corresponding source RPM (SRPM) from the Red Hat Customer Portal.- To stop capturing packets, press Ctrl+C.