Resguardo inicial

This commit is contained in:
lastdragon
2026-09-06 12:53:21 -06:00
parent 662efe3c2a
commit ad2088d178
94853 changed files with 43200404 additions and 0 deletions
@@ -0,0 +1,4 @@
# SPDX-License-Identifier: GPL-2.0-only
csum
gro
xdp_helper
@@ -0,0 +1,25 @@
# SPDX-License-Identifier: GPL-2.0
CFLAGS += -Wall -Wl,--no-as-needed -O2 -g
CFLAGS += -I../../../../../usr/include/ $(KHDR_INCLUDES)
# Additional include paths needed by kselftest.h
CFLAGS += -I../../
TEST_FILES := \
../../../../net/ynl \
../../../../../Documentation/netlink/specs \
ksft_setup_loopback.sh \
# end of TEST_FILES
TEST_GEN_FILES := \
$(patsubst %.c,%.o,$(wildcard *.bpf.c)) \
csum \
gro \
xdp_helper \
# end of TEST_GEN_FILES
TEST_INCLUDES := $(wildcard py/*.py sh/*.sh)
include ../../lib.mk
include ../bpf.mk
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,58 @@
/* SPDX-License-Identifier: GPL-2.0 */
#if !defined(__NET_KSFT_H__)
#define __NET_KSFT_H__
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
static inline void ksft_ready(void)
{
const char msg[7] = "ready\n";
char *env_str;
int fd;
env_str = getenv("KSFT_READY_FD");
if (env_str) {
fd = atoi(env_str);
if (!fd) {
fprintf(stderr, "invalid KSFT_READY_FD = '%s'\n",
env_str);
return;
}
} else {
fd = STDOUT_FILENO;
}
if (write(fd, msg, sizeof(msg)) < 0)
perror("write()");
if (fd != STDOUT_FILENO)
close(fd);
}
static inline void ksft_wait(void)
{
char *env_str;
char byte;
int fd;
env_str = getenv("KSFT_WAIT_FD");
if (env_str) {
fd = atoi(env_str);
if (!fd) {
fprintf(stderr, "invalid KSFT_WAIT_FD = '%s'\n",
env_str);
return;
}
} else {
/* Not running in KSFT env, wait for input from STDIN instead */
fd = STDIN_FILENO;
}
if (read(fd, &byte, sizeof(byte)) < 0)
perror("read()");
if (fd != STDIN_FILENO)
close(fd);
}
#endif
@@ -0,0 +1,111 @@
#!/bin/bash
# SPDX-License-Identifier: GPL-2.0
# Setup script for running ksft tests over a real interface in loopback mode.
# This scripts replaces the historical setup_loopback.sh. It puts
# a (presumably) real hardware interface into loopback mode, creates macvlan
# interfaces on top and places them in a network namespace for isolation.
#
# NETIF env variable must be exported to indicate the real target device.
# Note that the test will override NETIF with one of the macvlans, the
# actual ksft test will only see the macvlans.
#
# Example use:
# export NETIF=eth0
# ./net/lib/ksft_setup_loopback.sh ./drivers/net/gro.py
if [ -z "$NETIF" ]; then
echo "Error: NETIF variable not set"
exit 1
fi
if ! [ -d "/sys/class/net/$NETIF" ]; then
echo "Error: Can't find $NETIF, invalid netdevice"
exit 1
fi
# Save original settings for cleanup
readonly FLUSH_PATH="/sys/class/net/${NETIF}/gro_flush_timeout"
readonly IRQ_PATH="/sys/class/net/${NETIF}/napi_defer_hard_irqs"
FLUSH_TIMEOUT="$(< "${FLUSH_PATH}")"
readonly FLUSH_TIMEOUT
HARD_IRQS="$(< "${IRQ_PATH}")"
readonly HARD_IRQS
SERVER_NS=$(mktemp -u server-XXXXXXXX)
readonly SERVER_NS
CLIENT_NS=$(mktemp -u client-XXXXXXXX)
readonly CLIENT_NS
readonly SERVER_MAC="aa:00:00:00:00:02"
readonly CLIENT_MAC="aa:00:00:00:00:01"
# ksft expects addresses to communicate with remote
export LOCAL_V6=2001:db8:1::1
export REMOTE_V6=2001:db8:1::2
cleanup() {
local exit_code=$?
echo "Cleaning up..."
# Remove macvlan interfaces and namespaces
ip -netns "${SERVER_NS}" link del dev server 2>/dev/null || true
ip netns del "${SERVER_NS}" 2>/dev/null || true
ip -netns "${CLIENT_NS}" link del dev client 2>/dev/null || true
ip netns del "${CLIENT_NS}" 2>/dev/null || true
# Disable loopback
ethtool -K "${NETIF}" loopback off 2>/dev/null || true
sleep 1
echo "${FLUSH_TIMEOUT}" >"${FLUSH_PATH}"
echo "${HARD_IRQS}" >"${IRQ_PATH}"
exit $exit_code
}
trap cleanup EXIT INT TERM
# Enable loopback mode
echo "Enabling loopback on ${NETIF}..."
ethtool -K "${NETIF}" loopback on || {
echo "Failed to enable loopback mode"
exit 1
}
# The interface may need time to get carrier back, but selftests
# will wait for carrier, so no need to wait / sleep here.
# Use timer on host to trigger the network stack
# Also disable device interrupt to not depend on NIC interrupt
# Reduce test flakiness caused by unexpected interrupts
echo 100000 >"${FLUSH_PATH}"
echo 50 >"${IRQ_PATH}"
# Create server namespace with macvlan
ip netns add "${SERVER_NS}"
ip link add link "${NETIF}" dev server address "${SERVER_MAC}" type macvlan
ip link set dev server netns "${SERVER_NS}"
ip -netns "${SERVER_NS}" link set dev server up
ip -netns "${SERVER_NS}" addr add $LOCAL_V6/64 dev server
ip -netns "${SERVER_NS}" link set dev lo up
# Create client namespace with macvlan
ip netns add "${CLIENT_NS}"
ip link add link "${NETIF}" dev client address "${CLIENT_MAC}" type macvlan
ip link set dev client netns "${CLIENT_NS}"
ip -netns "${CLIENT_NS}" link set dev client up
ip -netns "${CLIENT_NS}" addr add $REMOTE_V6/64 dev client
ip -netns "${CLIENT_NS}" link set dev lo up
echo "Setup complete!"
echo " Device: ${NETIF}"
echo " Server NS: ${SERVER_NS}"
echo " Client NS: ${CLIENT_NS}"
echo ""
# Setup environment variables for tests
export NETIF=server
export REMOTE_TYPE=netns
export REMOTE_ARGS="${CLIENT_NS}"
# Run the command
ip netns exec "${SERVER_NS}" "$@"
@@ -0,0 +1,37 @@
# SPDX-License-Identifier: GPL-2.0
"""
Python selftest helpers for netdev.
"""
from .consts import KSRC
from .ksft import KsftFailEx, KsftSkipEx, KsftXfailEx, ksft_pr, ksft_eq, \
ksft_ne, ksft_true, ksft_not_none, ksft_in, ksft_not_in, ksft_is, \
ksft_ge, ksft_gt, ksft_lt, ksft_raises, ksft_busy_wait, \
ktap_result, ksft_disruptive, ksft_setup, ksft_run, ksft_exit, \
ksft_variants, KsftNamedVariant
from .netns import NetNS, NetNSEnter, UserNetNS
from .nsim import NetdevSim, NetdevSimDev
from .utils import CmdExitFailure, fd_read_timeout, cmd, bkg, defer, \
bpftool, ip, ethtool, bpftrace, rand_port, rand_ports, wait_port_listen, \
wait_file, tool, tc
from .bpf import bpf_map_set, bpf_map_dump, bpf_prog_map_ids
from .ynl import NlError, NlctrlFamily, YnlFamily, \
EthtoolFamily, NetdevFamily, RtnlFamily, RtnlAddrFamily
from .ynl import NetshaperFamily, DevlinkFamily, PSPFamily, Netlink
__all__ = ["KSRC",
"KsftFailEx", "KsftSkipEx", "KsftXfailEx", "ksft_pr", "ksft_eq",
"ksft_ne", "ksft_true", "ksft_not_none", "ksft_in", "ksft_not_in",
"ksft_is", "ksft_ge", "ksft_gt", "ksft_lt", "ksft_raises",
"ksft_busy_wait", "ktap_result", "ksft_disruptive", "ksft_setup",
"ksft_run", "ksft_exit", "ksft_variants", "KsftNamedVariant",
"NetNS", "NetNSEnter", "UserNetNS",
"CmdExitFailure", "fd_read_timeout", "cmd", "bkg", "defer",
"bpftool", "ip", "ethtool", "bpftrace", "rand_port", "rand_ports",
"wait_port_listen", "wait_file", "tool", "tc",
"bpf_map_set", "bpf_map_dump", "bpf_prog_map_ids",
"NetdevSim", "NetdevSimDev",
"NetshaperFamily", "DevlinkFamily", "PSPFamily", "NlError",
"YnlFamily", "EthtoolFamily", "NetdevFamily", "RtnlFamily",
"NlctrlFamily", "RtnlAddrFamily", "Netlink"]
@@ -0,0 +1,68 @@
# SPDX-License-Identifier: GPL-2.0
"""
BPF helper utilities for kernel selftests.
Provides common operations for interacting with BPF maps and programs
via bpftool, used by XDP and other BPF-based test files.
"""
from .utils import bpftool
def _format_hex_bytes(value):
"""
Helper function that converts an integer into a formatted hexadecimal byte string.
Args:
value: An integer representing the number to be converted.
Returns:
A string representing hexadecimal equivalent of value, with bytes separated by spaces.
"""
hex_str = value.to_bytes(4, byteorder='little', signed=True)
return ' '.join(f'{byte:02x}' for byte in hex_str)
def bpf_map_set(map_name, key, value):
"""
Updates an XDP map with a given key-value pair using bpftool.
Args:
map_name: The name of the XDP map to update.
key: The key to update in the map, formatted as a hexadecimal string.
value: The value to associate with the key, formatted as a hexadecimal string.
"""
key_formatted = _format_hex_bytes(key)
value_formatted = _format_hex_bytes(value)
bpftool(
f"map update name {map_name} key hex {key_formatted} value hex {value_formatted}"
)
def bpf_map_dump(map_id):
"""Dump all entries of a BPF array map.
Args:
map_id: Numeric map ID (as returned by bpftool prog show).
Returns:
A dict mapping formatted key (int) to formatted value (int).
"""
raw = bpftool(f"map dump id {map_id}", json=True)
return {e["formatted"]["key"]: e["formatted"]["value"] for e in raw}
def bpf_prog_map_ids(prog_id):
"""Get the map name-to-ID mapping for a loaded BPF program.
Args:
prog_id: Numeric program ID.
Returns:
A dict mapping map name (str) to map ID (int).
"""
map_ids = bpftool(f"prog show id {prog_id}", json=True)["map_ids"]
maps = {}
for mid in map_ids:
name = bpftool(f"map show id {mid}", json=True)["name"]
maps[name] = mid
return maps
@@ -0,0 +1,9 @@
# SPDX-License-Identifier: GPL-2.0
import sys
from pathlib import Path
KSFT_DIR = (Path(__file__).parent / "../../..").resolve()
KSRC = (Path(__file__).parent / "../../../../../..").resolve()
KSFT_MAIN_NAME = Path(sys.argv[0]).with_suffix("").name
@@ -0,0 +1,468 @@
# SPDX-License-Identifier: GPL-2.0
import fnmatch
import functools
import getopt
import inspect
import os
import signal
import sys
import time
import traceback
from collections import namedtuple
from .consts import KSFT_MAIN_NAME
from . import utils
KSFT_RESULT = None
KSFT_RESULT_ALL = True
KSFT_DISRUPTIVE = True
class KsftFailEx(Exception):
pass
class KsftSkipEx(Exception):
pass
class KsftXfailEx(Exception):
pass
class KsftTerminate(KeyboardInterrupt):
pass
class _KsftArgs:
def __init__(self):
self.list_tests = False
self.filters = []
try:
opts, _ = getopt.getopt(sys.argv[1:], 'hlt:T:')
except getopt.GetoptError as e:
print(e, file=sys.stderr)
sys.exit(1)
for opt, val in opts:
if opt == '-h':
print(f"Usage: {sys.argv[0]} [-h|-l] [-t|-T name]\n"
f"\t-h print help\n"
f"\t-l list tests (filtered, if filters were specified)\n"
f"\t-t name include test\n"
f"\t-T name exclude test",
file=sys.stderr)
sys.exit(0)
elif opt == '-l':
self.list_tests = True
elif opt == '-t':
self.filters.append((True, val))
elif opt == '-T':
self.filters.append((False, val))
@functools.lru_cache()
def _ksft_supports_color():
if os.environ.get("NO_COLOR") is not None:
return False
if not hasattr(sys.stdout, "isatty") or not sys.stdout.isatty():
return False
if os.environ.get("TERM") == "dumb":
return False
return True
def ksft_pr(*objs, **kwargs):
"""
Print logs to stdout.
Behaves like print() but log lines will be prefixed
with # to prevent breaking the TAP output formatting.
Extra arguments (on top of what print() supports):
line_pfx - add extra string before each line
"""
sep = kwargs.pop("sep", " ")
pfx = kwargs.pop("line_pfx", "")
pfx = "#" + (" " + pfx if pfx else "")
kwargs["flush"] = True
text = sep.join(str(obj) for obj in objs)
prefixed = f"\n{pfx} ".join(text.split('\n'))
print(pfx, prefixed, **kwargs)
def _fail(*args):
global KSFT_RESULT
KSFT_RESULT = False
stack = inspect.stack()
started = False
for frame in reversed(stack[2:]):
# Start printing from the test case function
if not started:
if frame.function == 'ksft_run':
started = True
continue
ksft_pr("Check| At " + frame.filename + ", line " + str(frame.lineno) +
", in " + frame.function + ":")
ksft_pr("Check| " + frame.code_context[0].strip())
ksft_pr(*args)
def ksft_eq(a, b, comment=""):
global KSFT_RESULT
if a != b:
_fail("Check failed", a, "!=", b, comment)
def ksft_ne(a, b, comment=""):
global KSFT_RESULT
if a == b:
_fail("Check failed", a, "==", b, comment)
def ksft_true(a, comment=""):
if not a:
_fail("Check failed", a, "does not eval to True", comment)
def ksft_not_none(a, comment=""):
if a is None:
_fail("Check failed", a, "is None", comment)
def ksft_in(a, b, comment=""):
if a not in b:
_fail("Check failed", a, "not in", b, comment)
def ksft_not_in(a, b, comment=""):
if a in b:
_fail("Check failed", a, "in", b, comment)
def ksft_is(a, b, comment=""):
if a is not b:
_fail("Check failed", a, "is not", b, comment)
def ksft_ge(a, b, comment=""):
if a < b:
_fail("Check failed", a, "<", b, comment)
def ksft_gt(a, b, comment=""):
if a <= b:
_fail("Check failed", a, "<=", b, comment)
def ksft_lt(a, b, comment=""):
if a >= b:
_fail("Check failed", a, ">=", b, comment)
class ksft_raises:
def __init__(self, expected_type):
self.exception = None
self.expected_type = expected_type
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
if exc_type is None:
_fail(f"Expected exception {str(self.expected_type.__name__)}, none raised")
elif self.expected_type != exc_type:
_fail(f"Expected exception {str(self.expected_type.__name__)}, raised {str(exc_type.__name__)}")
self.exception = exc_val
# Suppress the exception if its the expected one
return self.expected_type == exc_type
def ksft_busy_wait(cond, sleep=0.005, deadline=1, comment=""):
end = time.monotonic() + deadline
while True:
if cond():
return
if time.monotonic() > end:
_fail("Waiting for condition timed out", comment)
return
time.sleep(sleep)
def ktap_result(ok, cnt=1, case_name="", comment=""):
global KSFT_RESULT_ALL
KSFT_RESULT_ALL = KSFT_RESULT_ALL and ok
res = ""
if not ok:
res += "not "
res += "ok "
res += str(cnt) + " "
res += KSFT_MAIN_NAME
if case_name:
res += "." + case_name
if comment:
res += " # " + comment
if _ksft_supports_color():
if comment.startswith(("SKIP", "XFAIL")):
color = "\033[33m"
elif ok:
color = "\033[32m"
else:
color = "\033[31m"
res = color + res + "\033[0m"
print(res, flush=True)
def _ksft_defer_arm(state):
""" Allow or disallow the use of defer() """
utils.GLOBAL_DEFER_ARMED = state
def ksft_flush_defer():
global KSFT_RESULT
i = 0
qlen_start = len(utils.GLOBAL_DEFER_QUEUE)
while utils.GLOBAL_DEFER_QUEUE:
i += 1
entry = utils.GLOBAL_DEFER_QUEUE.pop()
try:
entry.exec_only()
except Exception:
ksft_pr(f"Exception while handling defer / cleanup (callback {i} of {qlen_start})!")
ksft_pr(traceback.format_exc(), line_pfx="Defer Exception|")
KSFT_RESULT = False
KsftCaseFunction = namedtuple("KsftCaseFunction",
['name', 'original_func', 'variants'])
def ksft_disruptive(func):
"""
Decorator that marks the test as disruptive (e.g. the test
that can down the interface). Disruptive tests can be skipped
by passing DISRUPTIVE=False environment variable.
"""
@functools.wraps(func)
def wrapper(*args, **kwargs):
if not KSFT_DISRUPTIVE:
raise KsftSkipEx("marked as disruptive")
return func(*args, **kwargs)
return wrapper
class KsftNamedVariant:
""" Named string name + argument list tuple for @ksft_variants """
def __init__(self, name, *params):
self.params = params
self.name = name or "_".join([str(x) for x in self.params])
def ksft_variants(params):
"""
Decorator defining the sets of inputs for a test.
The parameters will be included in the name of the resulting sub-case.
Parameters can be either single object, tuple or a KsftNamedVariant.
The argument can be a list or a generator.
Example:
@ksft_variants([
(1, "a"),
(2, "b"),
KsftNamedVariant("three", 3, "c"),
])
def my_case(cfg, a, b):
pass # ...
ksft_run(cases=[my_case], args=(cfg, ))
Will generate cases:
my_case.1_a
my_case.2_b
my_case.three
"""
return lambda func: KsftCaseFunction(func.__name__, func, params)
def ksft_setup(env):
"""
Setup test framework global state from the environment.
"""
def get_bool(env, name):
value = env.get(name, "").lower()
if value in ["yes", "true"]:
return True
if value in ["no", "false"]:
return False
try:
return bool(int(value))
except Exception:
raise Exception(f"failed to parse {name}")
if "DISRUPTIVE" in env:
global KSFT_DISRUPTIVE
KSFT_DISRUPTIVE = get_bool(env, "DISRUPTIVE")
return env
def _ksft_intr(signum, frame):
# ksft runner.sh sends 2 SIGTERMs in a row on a timeout
# if we don't ignore the second one it will stop us from handling cleanup
global term_cnt
term_cnt += 1
if term_cnt == 1:
raise KsftTerminate()
else:
ksft_pr(f"Ignoring SIGTERM (cnt: {term_cnt}), already exiting...")
def _ksft_name_matches(name, pattern):
if '*' in pattern or '?' in pattern or '[' in pattern:
return fnmatch.fnmatchcase(name, pattern)
return name == pattern
def _ksft_test_enabled(name, filters):
has_positive = False
for include, pattern in filters:
has_positive |= include
if _ksft_name_matches(name, pattern):
return include
return not has_positive
def _ksft_generate_test_cases(cases, globs, case_pfx, args, cli_args):
"""Generate a filtered list of (func, args, name) tuples.
If -l is given, prints matching test names and exits.
"""
cases = cases or []
test_cases = []
# If using the globs method find all relevant functions
if globs and case_pfx:
for key, value in globs.items():
if not callable(value):
continue
for prefix in case_pfx:
if key.startswith(prefix):
cases.append(value)
break
for func in cases:
if isinstance(func, KsftCaseFunction):
# Parametrized test - create case for each param
for param in func.variants:
if not isinstance(param, KsftNamedVariant):
if not isinstance(param, tuple):
param = (param, )
param = KsftNamedVariant(None, *param)
test_cases.append((func.original_func,
(*args, *param.params),
func.name + "." + param.name))
else:
test_cases.append((func, args, func.__name__))
if cli_args.filters:
test_cases = [tc for tc in test_cases
if _ksft_test_enabled(tc[2], cli_args.filters)]
if cli_args.list_tests:
for _, _, name in test_cases:
print(name)
sys.exit(0)
return test_cases
def ksft_run(cases=None, globs=None, case_pfx=None, args=()):
cli_args = _KsftArgs()
test_cases = _ksft_generate_test_cases(cases, globs, case_pfx, args,
cli_args)
global term_cnt
term_cnt = 0
prev_sigterm = signal.signal(signal.SIGTERM, _ksft_intr)
totals = {"pass": 0, "fail": 0, "skip": 0, "xfail": 0}
global KSFT_RESULT
if KSFT_RESULT is not None:
raise RuntimeError("ksft_run() can't be called multiple times.")
print("TAP version 13", flush=True)
print("1.." + str(len(test_cases)), flush=True)
cnt = 0
stop = False
for func, args, name in test_cases:
KSFT_RESULT = True
cnt += 1
comment = ""
cnt_key = ""
_ksft_defer_arm(True)
try:
func(*args)
except KsftSkipEx as e:
comment = "SKIP " + str(e)
cnt_key = 'skip'
except KsftXfailEx as e:
comment = "XFAIL " + str(e)
cnt_key = 'xfail'
except BaseException as e:
stop |= isinstance(e, KeyboardInterrupt)
ksft_pr(traceback.format_exc(), line_pfx="Exception|")
if stop:
ksft_pr(f"Stopping tests due to {type(e).__name__}.")
KSFT_RESULT = False
cnt_key = 'fail'
_ksft_defer_arm(False)
try:
ksft_flush_defer()
except BaseException as e:
ksft_pr(traceback.format_exc(), line_pfx="Exception|")
if isinstance(e, KeyboardInterrupt):
ksft_pr()
ksft_pr("WARN: defer() interrupted, cleanup may be incomplete.")
ksft_pr(" Attempting to finish cleanup before exiting.")
ksft_pr(" Interrupt again to exit immediately.")
ksft_pr()
stop = True
# Flush was interrupted, try to finish the job best we can
ksft_flush_defer()
if not cnt_key:
cnt_key = 'pass' if KSFT_RESULT else 'fail'
ktap_result(KSFT_RESULT, cnt, name, comment=comment)
totals[cnt_key] += 1
if stop:
break
signal.signal(signal.SIGTERM, prev_sigterm)
print(
f"# Totals: pass:{totals['pass']} fail:{totals['fail']} xfail:{totals['xfail']} xpass:0 skip:{totals['skip']} error:0"
)
def ksft_exit():
global KSFT_RESULT_ALL
sys.exit(0 if KSFT_RESULT_ALL else 1)
@@ -0,0 +1,122 @@
# SPDX-License-Identifier: GPL-2.0
import ctypes
import os
import random
import string
import subprocess
import time
from pathlib import Path
from .utils import ip
libc = ctypes.cdll.LoadLibrary('libc.so.6')
class NetNS:
def __init__(self, name=None):
if name:
self.name = name
else:
self.name = ''.join(random.choice(string.ascii_lowercase) for _ in range(8))
ip('netns add ' + self.name)
def __del__(self):
if self.name:
ip('netns del ' + self.name)
self.name = None
def __enter__(self):
return self
def __exit__(self, ex_type, ex_value, ex_tb):
self.__del__()
def __str__(self):
return self.name
def __repr__(self):
return f"NetNS({self.name})"
class UserNetNS:
"""Network namespace owned by a non-init user namespace."""
def __init__(self):
self.name = ''.join(
random.choice(string.ascii_lowercase) for _ in range(8))
self.user_ns_path = f"/run/userns/{self.name}"
self.net_ns_path = f"/run/netns/{self.name}"
self._user_mounted = False
self._net_mounted = False
os.makedirs("/run/userns", exist_ok=True)
os.makedirs("/run/netns", exist_ok=True)
Path(self.user_ns_path).touch()
Path(self.net_ns_path).touch()
with subprocess.Popen(
["unshare", "--user", "--net", "--map-root-user",
"sleep", "infinity"]) as proc:
try:
pid = proc.pid
init_user = os.readlink("/proc/self/ns/user")
for _ in range(200):
try:
if os.readlink(f"/proc/{pid}/ns/user") != init_user:
break
except OSError:
pass
time.sleep(0.01)
else:
raise RuntimeError("unshare child did not create userns")
subprocess.run(["mount", "--bind", f"/proc/{pid}/ns/user",
self.user_ns_path], check=True)
self._user_mounted = True
subprocess.run(["mount", "--bind", f"/proc/{pid}/ns/net",
self.net_ns_path], check=True)
self._net_mounted = True
finally:
proc.kill()
def __del__(self):
if self._net_mounted:
subprocess.run(["umount", self.net_ns_path], check=False)
self._net_mounted = False
if self._user_mounted:
subprocess.run(["umount", self.user_ns_path], check=False)
self._user_mounted = False
for path in (self.net_ns_path, self.user_ns_path):
try:
os.unlink(path)
except OSError:
pass
def __enter__(self):
return self
def __exit__(self, ex_type, ex_value, ex_tb):
self.__del__()
def __str__(self):
return self.name
def __repr__(self):
return f"UserNetNS({self.name})"
class NetNSEnter:
def __init__(self, ns_name):
self.ns_path = f"/run/netns/{ns_name}"
def __enter__(self):
self.saved = open("/proc/thread-self/ns/net")
with open(self.ns_path) as ns_file:
libc.setns(ns_file.fileno(), 0)
return self
def __exit__(self, exc_type, exc_value, traceback):
libc.setns(self.saved.fileno(), 0)
self.saved.close()
@@ -0,0 +1,135 @@
# SPDX-License-Identifier: GPL-2.0
import errno
import json
import os
import random
import re
import time
from .utils import cmd, ip
class NetdevSim:
"""
Class for netdevsim netdevice and its attributes.
"""
def __init__(self, nsimdev, port_index, ifname, ns=None):
# In case udev renamed the netdev to according to new schema,
# check if the name matches the port_index.
nsimnamere = re.compile(r"eni\d+np(\d+)")
match = nsimnamere.match(ifname)
if match and int(match.groups()[0]) != port_index + 1:
raise Exception("netdevice name mismatches the expected one")
self.ifname = ifname
self.nsimdev = nsimdev
self.port_index = port_index
self.ns = ns
self.dfs_dir = "%s/ports/%u/" % (nsimdev.dfs_dir, port_index)
ret = ip("-d -j link show dev %s" % ifname, ns=ns)
self.dev = json.loads(ret.stdout)[0]
self.ifindex = self.dev["ifindex"]
def dfs_write(self, path, val):
self.nsimdev.dfs_write(f'ports/{self.port_index}/' + path, val)
class NetdevSimDev:
"""
Class for netdevsim bus device and its attributes.
"""
@staticmethod
def ctrl_write(path, val):
fullpath = os.path.join("/sys/bus/netdevsim/", path)
with open(fullpath, "w") as f:
f.write(val)
def dfs_write(self, path, val):
fullpath = os.path.join(f"/sys/kernel/debug/netdevsim/netdevsim{self.addr}/", path)
with open(fullpath, "w") as f:
f.write(val)
def __init__(self, port_count=1, queue_count=1, ns=None):
# nsim will spawn in init_net, we'll set to actual ns once we switch it there
self.ns = None
if not os.path.exists("/sys/bus/netdevsim"):
cmd("modprobe netdevsim")
addr = random.randrange(1 << 15)
while True:
try:
self.ctrl_write("new_device", "%u %u %u" % (addr, port_count, queue_count))
except OSError as e:
if e.errno == errno.ENOSPC:
addr = random.randrange(1 << 15)
continue
raise e
break
self.addr = addr
# As probe of netdevsim device might happen from a workqueue,
# so wait here until all netdevs appear.
self.wait_for_netdevs(port_count)
if ns:
cmd(f"devlink dev reload netdevsim/netdevsim{addr} netns {ns.name}")
self.ns = ns
cmd("udevadm settle", ns=self.ns)
ifnames = self.get_ifnames()
self.dfs_dir = "/sys/kernel/debug/netdevsim/netdevsim%u/" % addr
self.nsims = []
for port_index in range(port_count):
self.nsims.append(self._make_port(port_index, ifnames[port_index]))
self.removed = False
def __enter__(self):
return self
def __exit__(self, ex_type, ex_value, ex_tb):
"""
__exit__ gets called at the end of a "with" block.
"""
self.remove()
def _make_port(self, port_index, ifname):
return NetdevSim(self, port_index, ifname, self.ns)
def get_ifnames(self):
ifnames = []
listdir = cmd(f"ls /sys/bus/netdevsim/devices/netdevsim{self.addr}/net/",
ns=self.ns).stdout.split()
for ifname in listdir:
ifnames.append(ifname)
ifnames.sort()
return ifnames
def wait_for_netdevs(self, port_count):
timeout = 5
timeout_start = time.time()
while True:
try:
ifnames = self.get_ifnames()
except FileNotFoundError as e:
ifnames = []
if len(ifnames) == port_count:
break
if time.time() < timeout_start + timeout:
continue
raise Exception("netdevices did not appear within timeout")
def remove(self):
if not self.removed:
self.ctrl_write("del_device", "%u" % (self.addr, ))
self.removed = True
def remove_nsim(self, nsim):
self.nsims.remove(nsim)
self.ctrl_write("devices/netdevsim%u/del_port" % (self.addr, ),
"%u" % (nsim.port_index, ))
@@ -0,0 +1,375 @@
# SPDX-License-Identifier: GPL-2.0
import json as _json
import os
import re
import select
import socket
import subprocess
import time
class CmdInitFailure(Exception):
""" Command failed to start. Only raised by bkg(). """
def __init__(self, msg, cmd_obj):
super().__init__(msg + "\n" + repr(cmd_obj))
self.cmd = cmd_obj
class CmdExitFailure(Exception):
""" Command failed (returned non-zero exit code). """
def __init__(self, msg, cmd_obj):
super().__init__(msg + "\n" + repr(cmd_obj))
self.cmd = cmd_obj
class CmdExitZeroFailure(CmdExitFailure):
""" Command succeeded (returned zero exit code), but expected failure. """
def fd_read_timeout(fd, timeout):
rlist, _, _ = select.select([fd], [], [], timeout)
if rlist:
return os.read(fd, 1024)
raise TimeoutError("Timeout waiting for fd read")
class cmd:
"""
Execute a command on local or remote host.
@shell defaults to false, and class will try to split @comm into a list
if it's a string with spaces.
Use bkg() instead to run a command in the background.
"""
def __init__(self, comm, shell=None, fail=True, expect_fail=False, ns=None,
background=False, host=None, timeout=20, ksft_ready=None,
ksft_wait=None):
if ns:
if hasattr(ns, 'user_ns_path'):
comm = (f'nsenter --user={ns.user_ns_path} '
f'--net={ns.net_ns_path} --setuid=0 --setgid=0 -- '
+ comm)
else:
comm = f'ip netns exec {ns} ' + comm
self.stdout = None
self.stderr = None
self.ret = None
self.ksft_term_fd = None
self.host = host
self.comm = comm
if host:
self.proc = host.cmd(comm)
else:
# If user doesn't explicitly request shell try to avoid it.
if shell is None and isinstance(comm, str) and ' ' in comm:
comm = comm.split()
# ksft_wait lets us wait for the background process to fully start,
# we pass an FD to the child process, and wait for it to write back.
# Similarly term_fd tells child it's time to exit.
pass_fds = []
env = os.environ.copy()
if ksft_wait is not None:
wait_fd, self.ksft_term_fd = os.pipe()
pass_fds.append(wait_fd)
env["KSFT_WAIT_FD"] = str(wait_fd)
ksft_ready = True # ksft_wait implies ready
if ksft_ready is not None:
rfd, ready_fd = os.pipe()
pass_fds.append(ready_fd)
env["KSFT_READY_FD"] = str(ready_fd)
self.proc = subprocess.Popen(comm, shell=shell, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, pass_fds=pass_fds,
env=env)
if ksft_wait is not None:
os.close(wait_fd)
if ksft_ready is not None:
os.close(ready_fd)
msg = fd_read_timeout(rfd, ksft_wait)
os.close(rfd)
if not msg:
terminate = self.proc.poll() is None
self._process_terminate(terminate=terminate, timeout=1)
raise CmdInitFailure("Did not receive ready message", self)
if not background:
self.process(terminate=False, fail=fail, expect_fail=expect_fail,
timeout=timeout)
def _process_terminate(self, terminate, timeout):
if terminate:
self.proc.terminate()
stdout, stderr = self.proc.communicate(timeout=timeout)
self.stdout = stdout.decode("utf-8")
self.stderr = stderr.decode("utf-8")
self.proc.stdout.close()
self.proc.stderr.close()
self.ret = self.proc.returncode
return stdout, stderr
def process(self, terminate=True, fail=None, expect_fail=False, timeout=20):
if fail is None:
fail = not terminate
if self.ksft_term_fd:
os.write(self.ksft_term_fd, b"1")
stdout, stderr = self._process_terminate(terminate=terminate,
timeout=timeout)
# Fail on unexpected test failure if fail.
# Fail on unexpected test success if expect_fail.
# Fail on negative returncode if either:
# Set by subprocess on crash or signal, this is never expected failure.
if (self.proc.returncode != 0 and fail or
(self.proc.returncode < 0 and expect_fail)):
if len(stderr) > 0 and stderr[-1] == "\n":
stderr = stderr[:-1]
raise CmdExitFailure("Command failed", self)
elif self.proc.returncode == 0 and expect_fail:
raise CmdExitZeroFailure("Command succeeded (expected fail)", self)
def __repr__(self):
def str_fmt(name, s):
name += ': '
return (name + s.strip().replace('\n', '\n' + ' ' * len(name)))
ret = "CMD"
if self.host:
ret += "[remote]"
if self.ret is None:
ret += f" (unterminated): {self.comm}\n"
elif self.ret == 0:
ret += f" (success): {self.comm}\n"
else:
ret += f": {self.comm}\n"
ret += f" EXIT: {self.ret}\n"
if self.stdout:
ret += str_fmt(" STDOUT", self.stdout) + "\n"
if self.stderr:
ret += str_fmt(" STDERR", self.stderr) + "\n"
return ret.strip()
class bkg(cmd):
"""
Run a command in the background.
Examples usage:
Run a command on remote host, and wait for it to finish.
This is usually paired with wait_port_listen() to make sure
the command has initialized:
with bkg("socat ...", exit_wait=True, host=cfg.remote) as nc:
...
Run a command and expect it to let us know that it's ready
by writing to a special file descriptor passed via KSFT_READY_FD.
Command will be terminated when we exit the context manager:
with bkg("my_binary", ksft_wait=5):
"""
def __init__(self, comm, shell=None, fail=None, expect_fail=None,
ns=None, host=None, exit_wait=False, ksft_ready=None,
ksft_wait=None):
super().__init__(comm, background=True,
shell=shell, fail=fail, expect_fail=expect_fail,
ns=ns, host=host, ksft_ready=ksft_ready,
ksft_wait=ksft_wait)
self.terminate = not exit_wait and not ksft_wait
self._exit_wait = exit_wait
self.check_fail = fail
self.expect_fail = expect_fail
if shell and self.terminate:
print("# Warning: combining shell and terminate is risky!")
print("# SIGTERM may not reach the child on zsh/ksh!")
def __enter__(self):
return self
def __exit__(self, ex_type, ex_value, ex_tb):
terminate = self.terminate
# Force termination on exception, but only if bkg() didn't already exit
# since forcing termination silences failures with fail=None
if self.proc.poll() is None:
terminate = terminate or (self._exit_wait and ex_type is not None)
return self.process(terminate=terminate, fail=self.check_fail,
expect_fail=self.expect_fail)
GLOBAL_DEFER_QUEUE = []
GLOBAL_DEFER_ARMED = False
class defer:
def __init__(self, func, *args, **kwargs):
if not callable(func):
raise Exception("defer created with un-callable object, did you call the function instead of passing its name?")
self.func = func
self.args = args
self.kwargs = kwargs
if not GLOBAL_DEFER_ARMED:
raise Exception("defer queue not armed, did you use defer() outside of a test case?")
self._queue = GLOBAL_DEFER_QUEUE
self._queue.append(self)
def __enter__(self):
return self
def __exit__(self, ex_type, ex_value, ex_tb):
return self.exec()
def exec_only(self):
self.func(*self.args, **self.kwargs)
def cancel(self):
self._queue.remove(self)
def exec(self):
self.cancel()
self.exec_only()
def tool(name, args, json=None, ns=None, host=None):
cmd_str = name + ' '
if json:
if name == 'tc':
cmd_str += '-json '
else:
cmd_str += '--json '
cmd_str += args
cmd_obj = cmd(cmd_str, ns=ns, host=host)
if json:
return _json.loads(cmd_obj.stdout)
return cmd_obj
def bpftool(args, json=None, ns=None, host=None):
return tool('bpftool', args, json=json, ns=ns, host=host)
def ip(args, json=None, ns=None, host=None):
if ns:
args = f'-netns {ns} ' + args
return tool('ip', args, json=json, host=host)
def tc(args, json=None, ns=None, host=None):
""" Helper to call tc with standard set of optional args. """
if ns:
args = f'-netns {ns} ' + args
return tool('tc', args, json=json, host=host)
def ethtool(args, json=None, ns=None, host=None):
return tool('ethtool', args, json=json, ns=ns, host=host)
def bpftrace(expr, json=None, ns=None, host=None, timeout=None):
"""
Run bpftrace and return map data (if json=True).
The output of bpftrace is inconvenient, so the helper converts
to a dict indexed by map name, e.g.:
{
"@": { ... },
"@map2": { ... },
}
"""
cmd_arr = ['bpftrace']
# Throw in --quiet if json, otherwise the output has two objects
if json:
cmd_arr += ['-f', 'json', '-q']
if timeout:
expr += ' interval:s:' + str(timeout) + ' { exit(); }'
timeout += 20
cmd_arr += ['-e', expr]
cmd_obj = cmd(cmd_arr, ns=ns, host=host, shell=False, timeout=timeout)
if json:
# bpftrace prints objects as lines
ret = {}
for l in cmd_obj.stdout.split('\n'):
if not l.strip():
continue
one = _json.loads(l)
if one.get('type') != 'map':
continue
for k, v in one["data"].items():
if k.startswith('@'):
k = k.lstrip('@')
ret[k] = v
return ret
return cmd_obj
def rand_port(stype=socket.SOCK_STREAM):
"""
Get a random unprivileged port.
"""
return rand_ports(1, stype)[0]
def rand_ports(count, stype=socket.SOCK_STREAM):
"""
Get a unique set of random unprivileged ports.
"""
sockets = []
ports = []
try:
for _ in range(count):
s = socket.socket(socket.AF_INET6, stype)
sockets.append(s)
s.bind(("", 0))
ports.append(s.getsockname()[1])
finally:
for s in sockets:
s.close()
return ports
def wait_port_listen(port, proto="tcp", ns=None, host=None, sleep=0.005, deadline=5):
end = time.monotonic() + deadline
pattern = f":{port:04X} .* "
if proto == "tcp": # for tcp protocol additionally check the socket state
pattern += "0A"
pattern = re.compile(pattern)
while True:
data = cmd(f'cat /proc/net/{proto}*', ns=ns, host=host, shell=True).stdout
for row in data.split("\n"):
if pattern.search(row):
return
if time.monotonic() > end:
raise Exception("Waiting for port listen timed out")
time.sleep(sleep)
def wait_file(fname, test_fn, sleep=0.005, deadline=5, encoding='utf-8'):
"""
Wait for file contents on the local system to satisfy a condition.
test_fn() should take one argument (file contents) and return whether
condition is met.
"""
end = time.monotonic() + deadline
with open(fname, "r", encoding=encoding) as fp:
while True:
if test_fn(fp.read()):
break
fp.seek(0)
if time.monotonic() > end:
raise TimeoutError("Wait for file contents failed", fname)
time.sleep(sleep)
@@ -0,0 +1,82 @@
# SPDX-License-Identifier: GPL-2.0
import sys
from pathlib import Path
from .consts import KSRC, KSFT_DIR
from .ksft import ksft_pr, ktap_result
# Resolve paths
try:
if (KSFT_DIR / "kselftest-list.txt").exists():
# Running in "installed" selftests
tools_full_path = KSFT_DIR
SPEC_PATH = KSFT_DIR / "net/lib/specs"
sys.path.append(tools_full_path.as_posix())
from net.lib.ynl.pyynl.lib import YnlFamily, NlError, NlPolicy, Netlink
else:
# Running in tree
tools_full_path = KSRC / "tools"
SPEC_PATH = KSRC / "Documentation/netlink/specs"
sys.path.append(tools_full_path.as_posix())
from net.ynl.pyynl.lib import YnlFamily, NlError, NlPolicy, Netlink
except ModuleNotFoundError as e:
ksft_pr("Failed importing `ynl` library from kernel sources")
ksft_pr(str(e))
ktap_result(True, comment="SKIP")
sys.exit(4)
__all__ = [
"NlError", "NlPolicy", "Netlink", "YnlFamily", "SPEC_PATH",
"EthtoolFamily", "RtnlFamily", "RtnlAddrFamily",
"NetdevFamily", "NetshaperFamily", "NlctrlFamily", "DevlinkFamily",
"PSPFamily",
]
#
# Wrapper classes, loading the right specs
# Set schema='' to avoid jsonschema validation, it's slow
#
class EthtoolFamily(YnlFamily):
def __init__(self, recv_size=0):
super().__init__((SPEC_PATH / Path('ethtool.yaml')).as_posix(),
schema='', recv_size=recv_size)
class RtnlFamily(YnlFamily):
def __init__(self, recv_size=0):
super().__init__((SPEC_PATH / Path('rt-link.yaml')).as_posix(),
schema='', recv_size=recv_size)
class RtnlAddrFamily(YnlFamily):
def __init__(self, recv_size=0):
super().__init__((SPEC_PATH / Path('rt-addr.yaml')).as_posix(),
schema='', recv_size=recv_size)
class NetdevFamily(YnlFamily):
def __init__(self, recv_size=0):
super().__init__((SPEC_PATH / Path('netdev.yaml')).as_posix(),
schema='', recv_size=recv_size)
class NetshaperFamily(YnlFamily):
def __init__(self, recv_size=0):
super().__init__((SPEC_PATH / Path('net_shaper.yaml')).as_posix(),
schema='', recv_size=recv_size)
class NlctrlFamily(YnlFamily):
def __init__(self, recv_size=0):
super().__init__((SPEC_PATH / Path('nlctrl.yaml')).as_posix(),
schema='', recv_size=recv_size)
class DevlinkFamily(YnlFamily):
def __init__(self, recv_size=0):
super().__init__((SPEC_PATH / Path('devlink.yaml')).as_posix(),
schema='', recv_size=recv_size)
class PSPFamily(YnlFamily):
def __init__(self, recv_size=0):
super().__init__((SPEC_PATH / Path('psp.yaml')).as_posix(),
schema='', recv_size=recv_size)
@@ -0,0 +1,131 @@
#!/bin/bash
# SPDX-License-Identifier: GPL-2.0
# Whether to pause and allow debugging when an executed deferred command has a
# non-zero exit code.
: "${DEFER_PAUSE_ON_FAIL:=no}"
# map[(scope_id,track,cleanup_id) -> cleanup_command]
# track={d=default | p=priority}
declare -A __DEFER__JOBS
# map[(scope_id,track) -> # cleanup_commands]
declare -A __DEFER__NJOBS
# scope_id of the topmost scope.
__DEFER__SCOPE_ID=0
__defer__ndefer_key()
{
local track=$1; shift
echo $__DEFER__SCOPE_ID,$track
}
__defer__defer_key()
{
local track=$1; shift
local defer_ix=$1; shift
echo $__DEFER__SCOPE_ID,$track,$defer_ix
}
__defer__ndefers()
{
local track=$1; shift
echo ${__DEFER__NJOBS[$(__defer__ndefer_key $track)]}
}
__defer__run()
{
local track=$1; shift
local defer_ix=$1; shift
local defer_key=$(__defer__defer_key $track $defer_ix)
local ret
eval ${__DEFER__JOBS[$defer_key]}
ret=$?
if [[ "$DEFER_PAUSE_ON_FAIL" == yes && "$ret" -ne 0 ]]; then
echo "Deferred command (track $track index $defer_ix):"
echo " ${__DEFER__JOBS[$defer_key]}"
echo "... ended with an exit status of $ret"
echo "Hit enter to continue, 'q' to quit"
read a
[[ "$a" == q ]] && exit 1
fi
unset __DEFER__JOBS[$defer_key]
}
__defer__schedule()
{
local track=$1; shift
local ndefers=$(__defer__ndefers $track)
local ndefers_key=$(__defer__ndefer_key $track)
local defer_key=$(__defer__defer_key $track $ndefers)
local defer="${@@Q}"
__DEFER__JOBS[$defer_key]="$defer"
__DEFER__NJOBS[$ndefers_key]=$((ndefers + 1))
}
__defer__scope_wipe()
{
__DEFER__NJOBS[$(__defer__ndefer_key d)]=0
__DEFER__NJOBS[$(__defer__ndefer_key p)]=0
}
defer_scope_push()
{
((__DEFER__SCOPE_ID++))
__defer__scope_wipe
}
defer_scope_pop()
{
local defer_ix
for ((defer_ix=$(__defer__ndefers p); defer_ix-->0; )); do
__defer__run p $defer_ix
done
for ((defer_ix=$(__defer__ndefers d); defer_ix-->0; )); do
__defer__run d $defer_ix
done
__defer__scope_wipe
((__DEFER__SCOPE_ID--))
}
defer()
{
__defer__schedule d "$@"
}
defer_prio()
{
__defer__schedule p "$@"
}
defer_scopes_cleanup()
{
while ((__DEFER__SCOPE_ID >= 0)); do
defer_scope_pop
done
}
in_defer_scope()
{
local ret
defer_scope_push
"$@"
ret=$?
defer_scope_pop
return $ret
}
__defer__scope_wipe
@@ -0,0 +1,19 @@
// SPDX-License-Identifier: GPL-2.0
#define KBUILD_MODNAME "xdp_dummy"
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
SEC("xdp")
int xdp_dummy_prog(struct xdp_md *ctx)
{
return XDP_PASS;
}
SEC("xdp.frags")
int xdp_dummy_prog_frags(struct xdp_md *ctx)
{
return XDP_PASS;
}
char _license[] SEC("license") = "GPL";
@@ -0,0 +1,131 @@
// SPDX-License-Identifier: GPL-2.0
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <linux/if_xdp.h>
#include <linux/if_link.h>
#include <net/if.h>
#include <inttypes.h>
#include "ksft.h"
#define UMEM_SZ (1U << 16)
#define NUM_DESC (UMEM_SZ / 2048)
static void print_usage(const char *bin)
{
fprintf(stderr, "Usage: %s ifindex queue_id [-z]\n\n"
"where:\n\t-z: force zerocopy mode", bin);
}
/* this is a simple helper program that creates an XDP socket and does the
* minimum necessary to get bind() to succeed.
*
* this test program is not intended to actually process packets, but could be
* extended in the future if that is actually needed.
*
* it is used by queues.py to ensure the xsk netlinux attribute is set
* correctly.
*/
int main(int argc, char **argv)
{
struct xdp_umem_reg umem_reg = { 0 };
struct sockaddr_xdp sxdp = { 0 };
int num_desc = NUM_DESC;
void *umem_area;
int retry = 0;
int ifindex;
int sock_fd;
int queue;
if (argc != 3 && argc != 4) {
print_usage(argv[0]);
return 1;
}
sock_fd = socket(AF_XDP, SOCK_RAW, 0);
if (sock_fd < 0) {
perror("socket creation failed");
/* if the kernel doesn't support AF_XDP, let the test program
* know with -1. All other error paths return 1.
*/
if (errno == EAFNOSUPPORT)
return -1;
return 1;
}
/* "Probing mode", just checking if AF_XDP sockets are supported */
if (!strcmp(argv[1], "-") && !strcmp(argv[2], "-")) {
printf("AF_XDP support detected\n");
close(sock_fd);
return 0;
}
ifindex = atoi(argv[1]);
queue = atoi(argv[2]);
umem_area = mmap(NULL, UMEM_SZ, PROT_READ | PROT_WRITE, MAP_PRIVATE |
MAP_ANONYMOUS, -1, 0);
if (umem_area == MAP_FAILED) {
perror("mmap failed");
return 1;
}
umem_reg.addr = (uintptr_t)umem_area;
umem_reg.len = UMEM_SZ;
umem_reg.chunk_size = 2048;
umem_reg.headroom = 0;
setsockopt(sock_fd, SOL_XDP, XDP_UMEM_REG, &umem_reg,
sizeof(umem_reg));
setsockopt(sock_fd, SOL_XDP, XDP_UMEM_FILL_RING, &num_desc,
sizeof(num_desc));
setsockopt(sock_fd, SOL_XDP, XDP_UMEM_COMPLETION_RING, &num_desc,
sizeof(num_desc));
setsockopt(sock_fd, SOL_XDP, XDP_RX_RING, &num_desc, sizeof(num_desc));
sxdp.sxdp_family = AF_XDP;
sxdp.sxdp_ifindex = ifindex;
sxdp.sxdp_queue_id = queue;
sxdp.sxdp_flags = 0;
if (argc > 3) {
if (!strcmp(argv[3], "-z")) {
sxdp.sxdp_flags = XDP_ZEROCOPY;
} else {
print_usage(argv[0]);
return 1;
}
}
while (1) {
if (bind(sock_fd, (struct sockaddr *)&sxdp, sizeof(sxdp)) == 0)
break;
if (errno == EBUSY && retry < 3) {
retry++;
sleep(1);
continue;
} else {
perror("bind failed");
munmap(umem_area, UMEM_SZ);
close(sock_fd);
return 1;
}
}
ksft_ready();
ksft_wait();
/* parent program will write a byte to stdin when its ready for this
* helper to exit
*/
close(sock_fd);
return 0;
}
@@ -0,0 +1,163 @@
// SPDX-License-Identifier: GPL-2.0
#include <stddef.h>
#include <linux/bpf.h>
#include <linux/in.h>
#include <linux/if_ether.h>
#include <linux/ip.h>
#include <linux/ipv6.h>
#include <linux/udp.h>
#include <linux/tcp.h>
#include <bpf/bpf_endian.h>
#include <bpf/bpf_helpers.h>
enum {
XDP_PORT = 1,
XDP_PROTO = 4,
} xdp_map_setup_keys;
struct {
__uint(type, BPF_MAP_TYPE_ARRAY);
__uint(max_entries, 5);
__type(key, __u32);
__type(value, __s32);
} map_xdp_setup SEC(".maps");
/* RSS hash results: key 0 = hash, key 1 = hash type,
* key 2 = packet count, key 3 = error count.
*/
enum {
RSS_KEY_HASH = 0,
RSS_KEY_TYPE = 1,
RSS_KEY_PKT_CNT = 2,
RSS_KEY_ERR_CNT = 3,
};
struct {
__uint(type, BPF_MAP_TYPE_ARRAY);
__type(key, __u32);
__type(value, __u32);
__uint(max_entries, 4);
} map_rss SEC(".maps");
/* Mirror of enum xdp_rss_hash_type from include/net/xdp.h.
* Needed because the enum is not part of UAPI headers.
*/
enum xdp_rss_hash_type {
XDP_RSS_L3_IPV4 = 1U << 0,
XDP_RSS_L3_IPV6 = 1U << 1,
XDP_RSS_L3_DYNHDR = 1U << 2,
XDP_RSS_L4 = 1U << 3,
XDP_RSS_L4_TCP = 1U << 4,
XDP_RSS_L4_UDP = 1U << 5,
XDP_RSS_L4_SCTP = 1U << 6,
XDP_RSS_L4_IPSEC = 1U << 7,
XDP_RSS_L4_ICMP = 1U << 8,
};
extern int bpf_xdp_metadata_rx_hash(const struct xdp_md *ctx, __u32 *hash,
enum xdp_rss_hash_type *rss_type) __ksym;
static __always_inline __u16 get_dest_port(void *l4, void *data_end,
__u8 protocol)
{
if (protocol == IPPROTO_UDP) {
struct udphdr *udp = l4;
if ((void *)(udp + 1) > data_end)
return 0;
return udp->dest;
} else if (protocol == IPPROTO_TCP) {
struct tcphdr *tcp = l4;
if ((void *)(tcp + 1) > data_end)
return 0;
return tcp->dest;
}
return 0;
}
SEC("xdp")
int xdp_rss_hash(struct xdp_md *ctx)
{
void *data_end = (void *)(long)ctx->data_end;
void *data = (void *)(long)ctx->data;
enum xdp_rss_hash_type rss_type = 0;
struct ethhdr *eth = data;
__u8 l4_proto = 0;
__u32 hash = 0;
__u32 key, val;
void *l4 = NULL;
__u32 *cnt;
int ret;
if ((void *)(eth + 1) > data_end)
return XDP_PASS;
if (eth->h_proto == bpf_htons(ETH_P_IP)) {
struct iphdr *iph = (void *)(eth + 1);
if ((void *)(iph + 1) > data_end)
return XDP_PASS;
l4_proto = iph->protocol;
l4 = (void *)(iph + 1);
} else if (eth->h_proto == bpf_htons(ETH_P_IPV6)) {
struct ipv6hdr *ip6h = (void *)(eth + 1);
if ((void *)(ip6h + 1) > data_end)
return XDP_PASS;
l4_proto = ip6h->nexthdr;
l4 = (void *)(ip6h + 1);
}
if (!l4)
return XDP_PASS;
/* Filter on the configured protocol (map_xdp_setup key XDP_PROTO).
* When set, only process packets matching the requested L4 protocol.
*/
key = XDP_PROTO;
__s32 *proto_cfg = bpf_map_lookup_elem(&map_xdp_setup, &key);
if (proto_cfg && *proto_cfg != 0 && l4_proto != (__u8)*proto_cfg)
return XDP_PASS;
/* Filter on the configured port (map_xdp_setup key XDP_PORT).
* Only applies to protocols with ports (UDP, TCP).
*/
key = XDP_PORT;
__s32 *port_cfg = bpf_map_lookup_elem(&map_xdp_setup, &key);
if (port_cfg && *port_cfg != 0) {
__u16 dest = get_dest_port(l4, data_end, l4_proto);
if (!dest || bpf_ntohs(dest) != (__u16)*port_cfg)
return XDP_PASS;
}
ret = bpf_xdp_metadata_rx_hash(ctx, &hash, &rss_type);
if (ret < 0) {
key = RSS_KEY_ERR_CNT;
cnt = bpf_map_lookup_elem(&map_rss, &key);
if (cnt)
__sync_fetch_and_add(cnt, 1);
return XDP_PASS;
}
key = RSS_KEY_HASH;
bpf_map_update_elem(&map_rss, &key, &hash, BPF_ANY);
key = RSS_KEY_TYPE;
val = (__u32)rss_type;
bpf_map_update_elem(&map_rss, &key, &val, BPF_ANY);
key = RSS_KEY_PKT_CNT;
cnt = bpf_map_lookup_elem(&map_rss, &key);
if (cnt)
__sync_fetch_and_add(cnt, 1);
return XDP_PASS;
}
char _license[] SEC("license") = "GPL";
@@ -0,0 +1,685 @@
// SPDX-License-Identifier: GPL-2.0
#include <stddef.h>
#include <linux/bpf.h>
#include <linux/in.h>
#include <linux/if_ether.h>
#include <linux/ip.h>
#include <linux/ipv6.h>
#include <linux/udp.h>
#include <bpf/bpf_endian.h>
#include <bpf/bpf_helpers.h>
#define MAX_ADJST_OFFSET 256
#define MAX_PAYLOAD_LEN 5000
#define MAX_HDR_LEN 64
extern int bpf_xdp_pull_data(struct xdp_md *xdp, __u32 len) __ksym __weak;
enum {
XDP_MODE = 0,
XDP_PORT = 1,
XDP_ADJST_OFFSET = 2,
XDP_ADJST_TAG = 3,
} xdp_map_setup_keys;
enum {
XDP_MODE_PASS = 0,
XDP_MODE_DROP = 1,
XDP_MODE_TX = 2,
XDP_MODE_TAIL_ADJST = 3,
XDP_MODE_HEAD_ADJST = 4,
} xdp_map_modes;
enum {
STATS_RX = 0,
STATS_PASS = 1,
STATS_DROP = 2,
STATS_TX = 3,
STATS_ABORT = 4,
} xdp_stats;
struct {
__uint(type, BPF_MAP_TYPE_ARRAY);
__uint(max_entries, 5);
__type(key, __u32);
__type(value, __s32);
} map_xdp_setup SEC(".maps");
struct {
__uint(type, BPF_MAP_TYPE_ARRAY);
__uint(max_entries, 5);
__type(key, __u32);
__type(value, __u64);
} map_xdp_stats SEC(".maps");
static __u32 min(__u32 a, __u32 b)
{
return a < b ? a : b;
}
static void record_stats(struct xdp_md *ctx, __u32 stat_type)
{
__u64 *count;
count = bpf_map_lookup_elem(&map_xdp_stats, &stat_type);
if (count)
__sync_fetch_and_add(count, 1);
}
static struct udphdr *filter_udphdr(struct xdp_md *ctx, __u16 port)
{
struct udphdr *udph = NULL;
void *data, *data_end;
struct ethhdr *eth;
int err;
err = bpf_xdp_pull_data(ctx, sizeof(*eth));
if (err)
return NULL;
data_end = (void *)(long)ctx->data_end;
data = eth = (void *)(long)ctx->data;
if (data + sizeof(*eth) > data_end)
return NULL;
if (eth->h_proto == bpf_htons(ETH_P_IP)) {
struct iphdr *iph;
err = bpf_xdp_pull_data(ctx, sizeof(*eth) + sizeof(*iph) +
sizeof(*udph));
if (err)
return NULL;
data_end = (void *)(long)ctx->data_end;
data = (void *)(long)ctx->data;
iph = data + sizeof(*eth);
if (iph + 1 > (struct iphdr *)data_end ||
iph->protocol != IPPROTO_UDP)
return NULL;
udph = data + sizeof(*iph) + sizeof(*eth);
} else if (eth->h_proto == bpf_htons(ETH_P_IPV6)) {
struct ipv6hdr *ipv6h;
err = bpf_xdp_pull_data(ctx, sizeof(*eth) + sizeof(*ipv6h) +
sizeof(*udph));
if (err)
return NULL;
data_end = (void *)(long)ctx->data_end;
data = (void *)(long)ctx->data;
ipv6h = data + sizeof(*eth);
if (ipv6h + 1 > (struct ipv6hdr *)data_end ||
ipv6h->nexthdr != IPPROTO_UDP)
return NULL;
udph = data + sizeof(*ipv6h) + sizeof(*eth);
} else {
return NULL;
}
if (udph + 1 > (struct udphdr *)data_end)
return NULL;
if (udph->dest != bpf_htons(port))
return NULL;
record_stats(ctx, STATS_RX);
return udph;
}
static int xdp_mode_pass(struct xdp_md *ctx, __u16 port)
{
struct udphdr *udph = NULL;
udph = filter_udphdr(ctx, port);
if (!udph)
return XDP_PASS;
record_stats(ctx, STATS_PASS);
return XDP_PASS;
}
static int xdp_mode_drop_handler(struct xdp_md *ctx, __u16 port)
{
struct udphdr *udph = NULL;
udph = filter_udphdr(ctx, port);
if (!udph)
return XDP_PASS;
record_stats(ctx, STATS_DROP);
return XDP_DROP;
}
static void swap_machdr(void *data)
{
struct ethhdr *eth = data;
__u8 tmp_mac[ETH_ALEN];
__builtin_memcpy(tmp_mac, eth->h_source, ETH_ALEN);
__builtin_memcpy(eth->h_source, eth->h_dest, ETH_ALEN);
__builtin_memcpy(eth->h_dest, tmp_mac, ETH_ALEN);
}
static int xdp_mode_tx_handler(struct xdp_md *ctx, __u16 port)
{
struct udphdr *udph = NULL;
void *data, *data_end;
struct ethhdr *eth;
int err;
err = bpf_xdp_pull_data(ctx, sizeof(*eth));
if (err)
return XDP_PASS;
data_end = (void *)(long)ctx->data_end;
data = eth = (void *)(long)ctx->data;
if (data + sizeof(*eth) > data_end)
return XDP_PASS;
if (eth->h_proto == bpf_htons(ETH_P_IP)) {
struct iphdr *iph;
__be32 tmp_ip;
err = bpf_xdp_pull_data(ctx, sizeof(*eth) + sizeof(*iph) +
sizeof(*udph));
if (err)
return XDP_PASS;
data_end = (void *)(long)ctx->data_end;
data = (void *)(long)ctx->data;
iph = data + sizeof(*eth);
if (iph + 1 > (struct iphdr *)data_end ||
iph->protocol != IPPROTO_UDP)
return XDP_PASS;
udph = data + sizeof(*iph) + sizeof(*eth);
if (udph + 1 > (struct udphdr *)data_end)
return XDP_PASS;
if (udph->dest != bpf_htons(port))
return XDP_PASS;
record_stats(ctx, STATS_RX);
eth = data;
swap_machdr((void *)eth);
tmp_ip = iph->saddr;
iph->saddr = iph->daddr;
iph->daddr = tmp_ip;
record_stats(ctx, STATS_TX);
return XDP_TX;
} else if (eth->h_proto == bpf_htons(ETH_P_IPV6)) {
struct in6_addr tmp_ipv6;
struct ipv6hdr *ipv6h;
err = bpf_xdp_pull_data(ctx, sizeof(*eth) + sizeof(*ipv6h) +
sizeof(*udph));
if (err)
return XDP_PASS;
data_end = (void *)(long)ctx->data_end;
data = (void *)(long)ctx->data;
ipv6h = data + sizeof(*eth);
if (ipv6h + 1 > (struct ipv6hdr *)data_end ||
ipv6h->nexthdr != IPPROTO_UDP)
return XDP_PASS;
udph = data + sizeof(*ipv6h) + sizeof(*eth);
if (udph + 1 > (struct udphdr *)data_end)
return XDP_PASS;
if (udph->dest != bpf_htons(port))
return XDP_PASS;
record_stats(ctx, STATS_RX);
eth = data;
swap_machdr((void *)eth);
__builtin_memcpy(&tmp_ipv6, &ipv6h->saddr, sizeof(tmp_ipv6));
__builtin_memcpy(&ipv6h->saddr, &ipv6h->daddr,
sizeof(tmp_ipv6));
__builtin_memcpy(&ipv6h->daddr, &tmp_ipv6, sizeof(tmp_ipv6));
record_stats(ctx, STATS_TX);
return XDP_TX;
}
return XDP_PASS;
}
static __always_inline __u16 csum_fold_helper(__u32 csum)
{
csum = (csum & 0xffff) + (csum >> 16);
return ~((csum & 0xffff) + (csum >> 16));
}
static __always_inline __u16 csum_fold_udp_helper(__u32 csum)
{
return csum_fold_helper(csum) ? : 0xffff;
}
static void *update_pkt(struct xdp_md *ctx, __s16 offset, __u32 *udp_csum)
{
void *data_end = (void *)(long)ctx->data_end;
void *data = (void *)(long)ctx->data;
struct udphdr *udph = NULL;
struct ethhdr *eth = data;
__u32 len, len_new;
if (data + sizeof(*eth) > data_end)
return NULL;
if (eth->h_proto == bpf_htons(ETH_P_IP)) {
struct iphdr *iph = data + sizeof(*eth);
if (iph + 1 > (struct iphdr *)data_end)
return NULL;
udph = (void *)eth + sizeof(*iph) + sizeof(*eth);
if (!udph || udph + 1 > (struct udphdr *)data_end)
return NULL;
len = iph->tot_len;
len_new = bpf_htons(bpf_ntohs(len) + offset);
iph->tot_len = len_new;
iph->check = csum_fold_helper(
bpf_csum_diff(&len, sizeof(len), &len_new,
sizeof(len_new), ~((__u32)iph->check)));
} else if (eth->h_proto == bpf_htons(ETH_P_IPV6)) {
struct ipv6hdr *ipv6h = data + sizeof(*eth);
if (ipv6h + 1 > (struct ipv6hdr *)data_end)
return NULL;
udph = (void *)eth + sizeof(*ipv6h) + sizeof(*eth);
if (!udph || udph + 1 > (struct udphdr *)data_end)
return NULL;
len = ipv6h->payload_len;
len_new = bpf_htons(bpf_ntohs(len) + offset);
ipv6h->payload_len = len_new;
} else {
return NULL;
}
len = udph->len;
len_new = bpf_htons(bpf_ntohs(len) + offset);
*udp_csum = ~((__u32)udph->check);
*udp_csum = bpf_csum_diff(&len, sizeof(len), &len_new,
sizeof(len_new), *udp_csum);
*udp_csum = bpf_csum_diff(&len, sizeof(len), &len_new,
sizeof(len_new), *udp_csum);
udph->len = len_new;
return udph;
}
static int xdp_adjst_tail_shrnk_data(struct xdp_md *ctx, __u16 offset,
unsigned long hdr_len)
{
char tmp_buff[MAX_ADJST_OFFSET];
__u32 buff_pos, udp_csum = 0;
struct udphdr *udph = NULL;
__u32 buff_len;
udph = update_pkt(ctx, 0 - offset, &udp_csum);
if (!udph)
return -1;
buff_len = bpf_xdp_get_buff_len(ctx);
offset = (offset & 0x1ff) >= MAX_ADJST_OFFSET ? MAX_ADJST_OFFSET :
offset & 0xff;
if (offset == 0)
return -1;
/* Make sure we have enough data to avoid eating the header */
if (buff_len - offset < hdr_len)
return -1;
buff_pos = buff_len - offset;
if (bpf_xdp_load_bytes(ctx, buff_pos, tmp_buff, offset) < 0)
return -1;
udp_csum = bpf_csum_diff((__be32 *)tmp_buff, offset, 0, 0, udp_csum);
udph->check = (__u16)csum_fold_udp_helper(udp_csum);
if (bpf_xdp_adjust_tail(ctx, 0 - offset) < 0)
return -1;
return 0;
}
static int xdp_adjst_tail_grow_data(struct xdp_md *ctx, __u16 offset)
{
char tmp_buff[MAX_ADJST_OFFSET];
__u32 buff_pos, udp_csum = 0;
__u32 buff_len, hdr_len, key;
struct udphdr *udph;
__s32 *val;
__u8 tag;
/* Proceed to update the packet headers before attempting to adjuste
* the tail. Once the tail is adjusted we lose access to the offset
* amount of data at the end of the packet which is crucial to update
* the checksum.
* Since any failure beyond this would abort the packet, we should
* not worry about passing a packet up the stack with wrong headers
*/
udph = update_pkt(ctx, offset, &udp_csum);
if (!udph)
return -1;
key = XDP_ADJST_TAG;
val = bpf_map_lookup_elem(&map_xdp_setup, &key);
if (!val)
return -1;
tag = (__u8)(*val);
for (int i = 0; i < MAX_ADJST_OFFSET; i++)
__builtin_memcpy(&tmp_buff[i], &tag, 1);
offset = (offset & 0x1ff) >= MAX_ADJST_OFFSET ? MAX_ADJST_OFFSET :
offset & 0xff;
if (offset == 0)
return -1;
udp_csum = bpf_csum_diff(0, 0, (__be32 *)tmp_buff, offset, udp_csum);
udph->check = (__u16)csum_fold_udp_helper(udp_csum);
buff_len = bpf_xdp_get_buff_len(ctx);
if (bpf_xdp_adjust_tail(ctx, offset) < 0) {
bpf_printk("Failed to adjust tail\n");
return -1;
}
if (bpf_xdp_store_bytes(ctx, buff_len, tmp_buff, offset) < 0)
return -1;
return 0;
}
static int xdp_adjst_tail(struct xdp_md *ctx, __u16 port)
{
struct udphdr *udph = NULL;
__s32 *adjust_offset, *val;
unsigned long hdr_len;
void *offset_ptr;
__u32 key;
__u8 tag;
int ret;
udph = filter_udphdr(ctx, port);
if (!udph)
return XDP_PASS;
hdr_len = (void *)udph - (void *)(long)ctx->data +
sizeof(struct udphdr);
key = XDP_ADJST_OFFSET;
adjust_offset = bpf_map_lookup_elem(&map_xdp_setup, &key);
if (!adjust_offset)
return XDP_PASS;
if (*adjust_offset < 0)
ret = xdp_adjst_tail_shrnk_data(ctx,
(__u16)(0 - *adjust_offset),
hdr_len);
else
ret = xdp_adjst_tail_grow_data(ctx, (__u16)(*adjust_offset));
if (ret)
goto abort_pkt;
record_stats(ctx, STATS_PASS);
return XDP_PASS;
abort_pkt:
record_stats(ctx, STATS_ABORT);
return XDP_ABORTED;
}
static int xdp_adjst_head_shrnk_data(struct xdp_md *ctx, __u64 hdr_len,
__u32 offset)
{
char tmp_buff[MAX_ADJST_OFFSET];
struct udphdr *udph;
void *offset_ptr;
__u32 udp_csum = 0;
/* Update the length information in the IP and UDP headers before
* adjusting the headroom. This simplifies accessing the relevant
* fields in the IP and UDP headers for fragmented packets. Any
* failure beyond this point will result in the packet being aborted,
* so we don't need to worry about incorrect length information for
* passed packets.
*/
udph = update_pkt(ctx, (__s16)(0 - offset), &udp_csum);
if (!udph)
return -1;
offset = (offset & 0x1ff) >= MAX_ADJST_OFFSET ? MAX_ADJST_OFFSET :
offset & 0xff;
if (offset == 0)
return -1;
if (bpf_xdp_load_bytes(ctx, hdr_len, tmp_buff, offset) < 0)
return -1;
udp_csum = bpf_csum_diff((__be32 *)tmp_buff, offset, 0, 0, udp_csum);
udph->check = (__u16)csum_fold_udp_helper(udp_csum);
if (bpf_xdp_load_bytes(ctx, 0, tmp_buff, MAX_ADJST_OFFSET) < 0)
return -1;
if (bpf_xdp_adjust_head(ctx, offset) < 0)
return -1;
if (offset > MAX_ADJST_OFFSET)
return -1;
if (hdr_len > MAX_ADJST_OFFSET || hdr_len == 0)
return -1;
/* Added here to handle clang complain about negative value */
hdr_len = hdr_len & 0xff;
if (hdr_len == 0)
return -1;
if (bpf_xdp_store_bytes(ctx, 0, tmp_buff, hdr_len) < 0)
return -1;
return 0;
}
static int xdp_adjst_head_grow_data(struct xdp_md *ctx, __u64 hdr_len,
__u32 offset)
{
char hdr_buff[MAX_HDR_LEN];
char data_buff[MAX_ADJST_OFFSET];
void *offset_ptr;
__s32 *val;
__u32 key;
__u8 tag;
__u32 udp_csum = 0;
struct udphdr *udph;
udph = update_pkt(ctx, (__s16)(offset), &udp_csum);
if (!udph)
return -1;
key = XDP_ADJST_TAG;
val = bpf_map_lookup_elem(&map_xdp_setup, &key);
if (!val)
return -1;
tag = (__u8)(*val);
for (int i = 0; i < MAX_ADJST_OFFSET; i++)
__builtin_memcpy(&data_buff[i], &tag, 1);
offset = (offset & 0x1ff) >= MAX_ADJST_OFFSET ? MAX_ADJST_OFFSET :
offset & 0xff;
if (offset == 0)
return -1;
udp_csum = bpf_csum_diff(0, 0, (__be32 *)data_buff, offset, udp_csum);
udph->check = (__u16)csum_fold_udp_helper(udp_csum);
if (hdr_len > MAX_ADJST_OFFSET || hdr_len == 0)
return -1;
/* Added here to handle clang complain about negative value */
hdr_len = hdr_len & 0xff;
if (hdr_len == 0)
return -1;
if (bpf_xdp_load_bytes(ctx, 0, hdr_buff, hdr_len) < 0)
return -1;
if (offset > MAX_ADJST_OFFSET)
return -1;
if (bpf_xdp_adjust_head(ctx, 0 - offset) < 0)
return -1;
if (bpf_xdp_store_bytes(ctx, 0, hdr_buff, hdr_len) < 0)
return -1;
if (bpf_xdp_store_bytes(ctx, hdr_len, data_buff, offset) < 0)
return -1;
return 0;
}
static int xdp_head_adjst(struct xdp_md *ctx, __u16 port)
{
struct udphdr *udph_ptr = NULL;
__u32 key, size, hdr_len;
__s32 *val;
int res;
/* Filter packets based on UDP port */
udph_ptr = filter_udphdr(ctx, port);
if (!udph_ptr)
return XDP_PASS;
hdr_len = (void *)udph_ptr - (void *)(long)ctx->data +
sizeof(struct udphdr);
key = XDP_ADJST_OFFSET;
val = bpf_map_lookup_elem(&map_xdp_setup, &key);
if (!val)
return XDP_PASS;
switch (*val) {
case -16:
case 16:
size = 16;
break;
case -32:
case 32:
size = 32;
break;
case -64:
case 64:
size = 64;
break;
case -128:
case 128:
size = 128;
break;
case -256:
case 256:
size = 256;
break;
default:
bpf_printk("Invalid adjustment offset: %d\n", *val);
goto abort;
}
if (*val < 0)
res = xdp_adjst_head_grow_data(ctx, hdr_len, size);
else
res = xdp_adjst_head_shrnk_data(ctx, hdr_len, size);
if (res)
goto abort;
record_stats(ctx, STATS_PASS);
return XDP_PASS;
abort:
record_stats(ctx, STATS_ABORT);
return XDP_ABORTED;
}
static int xdp_prog_common(struct xdp_md *ctx)
{
__u32 key, *port;
__s32 *mode;
key = XDP_MODE;
mode = bpf_map_lookup_elem(&map_xdp_setup, &key);
if (!mode)
return XDP_PASS;
key = XDP_PORT;
port = bpf_map_lookup_elem(&map_xdp_setup, &key);
if (!port)
return XDP_PASS;
switch (*mode) {
case XDP_MODE_PASS:
return xdp_mode_pass(ctx, (__u16)(*port));
case XDP_MODE_DROP:
return xdp_mode_drop_handler(ctx, (__u16)(*port));
case XDP_MODE_TX:
return xdp_mode_tx_handler(ctx, (__u16)(*port));
case XDP_MODE_TAIL_ADJST:
return xdp_adjst_tail(ctx, (__u16)(*port));
case XDP_MODE_HEAD_ADJST:
return xdp_head_adjst(ctx, (__u16)(*port));
}
/* Default action is to simple pass */
return XDP_PASS;
}
SEC("xdp")
int xdp_prog(struct xdp_md *ctx)
{
return xdp_prog_common(ctx);
}
SEC("xdp.frags")
int xdp_prog_frags(struct xdp_md *ctx)
{
return xdp_prog_common(ctx);
}
char _license[] SEC("license") = "GPL";