Skip to content

Commit 3e68e58

Browse files
feat: iptables block using LSM BPF (#3871)
* feat: iptables block using LSM BPF * feat: makefile changes * chore: cleanup * Update bpf-prog/block-iptables/bpf/src/block_iptables.bpf.c Co-authored-by: Copilot <[email protected]> Signed-off-by: Santhosh Prabhu <[email protected]> * chore: fix const * fix: dockerfiles, bug * test: add test for bpf attach/detach * feat: add event tracking * feat: add event counting * chore: dockerfiles * fix: nolint for generated code * fix: nolint for generated code * fix: golangci skip * fix:skip directory with generated code * chore:skip block iptales generated code * chore: fix * fix: run go generate first * fix: use installed go * fix: install bpf libraries * fix: remove file * fix: roll back linter changes * fix: add code generation to tests * chore: switch to dual license * fix: run generate before lint * fix: install libs * fix: fix linting, address some comments * fix: address comments, fix lint * fix: missed change * fix: compile only on linux * fix: compile only on linux * fix: address comments * fix: address comments * chore: dockerfiles * fix: address comments * chore:dockerfiles * fix: address comments * chore: dockerfiles * fix: address comments --------- Signed-off-by: Santhosh Prabhu <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent 7833eec commit 3e68e58

File tree

16 files changed

+129346
-13
lines changed

16 files changed

+129346
-13
lines changed

.github/workflows/golangci.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,33 @@ on:
1111
types:
1212
- checks_requested
1313
jobs:
14+
generate:
15+
name: Generate BPF Handling Code
16+
runs-on: ubuntu-latest
17+
steps:
18+
- uses: actions/checkout@v4
19+
20+
- name: Set up Go
21+
uses: actions/setup-go@v5
22+
with:
23+
go-version-file: go.mod
24+
25+
- name: Run go generate
26+
run: make bpf-lib && go generate ./...
27+
28+
- name: Upload generated code
29+
uses: actions/upload-artifact@v4
30+
with:
31+
name: generated-bpf-program-code
32+
path: ./bpf-prog/block-iptables/pkg/blockservice
1433
golangci:
1534
strategy:
1635
fail-fast: false
1736
matrix:
1837
go-version: ['1.22.x', '1.23.x']
1938
os: [ubuntu-latest, windows-latest]
2039
name: Lint
40+
needs: generate
2141
runs-on: ${{ matrix.os }}
2242
steps:
2343
- uses: actions/setup-go@v5
@@ -26,6 +46,11 @@ jobs:
2646
- uses: actions/checkout@v4
2747
with:
2848
fetch-depth: 0
49+
- name: Download generated code
50+
uses: actions/download-artifact@v4
51+
with:
52+
name: generated-bpf-program-code
53+
path: ./bpf-prog/block-iptables/pkg/blockservice
2954
- name: golangci-lint
3055
uses: golangci/golangci-lint-action@v6
3156
with:

.pipelines/build/dockerfiles/cns.Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ ENTRYPOINT ["azure-cns.exe"]
1111
EXPOSE 10090
1212

1313
# mcr.microsoft.com/azurelinux/base/core:3.0
14-
FROM --platform=linux/${ARCH} mcr.microsoft.com/azurelinux/base/core@sha256:c09a4e011a092a45b5c46ac5633253eb1e1106df028912b89cbe225d9061ef0b AS build-helper
14+
FROM --platform=linux/${ARCH} mcr.microsoft.com/azurelinux/base/core@sha256:e9bb4e5a79123f2ae29dc601f68adf63a636a455c4259423712b06b798cb201e AS build-helper
1515
RUN tdnf install -y iptables
1616

1717
# mcr.microsoft.com/azurelinux/distroless/minimal:3.0
18-
FROM --platform=linux/${ARCH} mcr.microsoft.com/azurelinux/distroless/minimal@sha256:c37100f358ee19e62c60673c54fb43b83d43b2c305846e44b23b2e032e9caf30 AS linux
18+
FROM --platform=linux/${ARCH} mcr.microsoft.com/azurelinux/distroless/minimal@sha256:be0e4c34aaa7b369ff84907fa0a743f5d5904fa2d5b7da2e768ad8df285d4213 AS linux
1919
ARG ARTIFACT_DIR .
2020

2121
COPY --from=build-helper /usr/sbin/*tables* /usr/sbin/

.pipelines/pipeline.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ stages:
7575
name: "$(BUILD_POOL_NAME_DEFAULT)"
7676
steps:
7777
- script: |
78-
make ipv6-hp-bpf-lib
78+
make bpf-lib
7979
make all-binaries-platforms
8080
name: "BuildAllPlatformBinaries"
8181
displayName: "Build all platform binaries"

.pipelines/templates/run-unit-tests.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ stages:
1414
- script: |
1515
set -e
1616
make tools
17+
make bpf-lib
18+
go generate ./...
1719
1820
# run test, echo exit status code to fd 3, pipe output from test to tee, which splits output to stdout and go-junit-report (which converts test output to report.xml),
1921
# stdout from tee is redirected to fd 4. Take output written to fd 3 (which is the exit code of test), redirect to stdout, pipe to read from stdout then exit with that status code.

Makefile

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ endif
3333
# Interrogate the git repo and set some variables
3434
REPO_ROOT ?= $(shell git rev-parse --show-toplevel)
3535
REVISION ?= $(shell git rev-parse --short HEAD)
36-
ACN_VERSION ?= $(shell git describe --exclude "azure-iptables-monitor*" --exclude "azure-ip-masq-merger*" --exclude "azure-ipam*" --exclude "dropgz*" --exclude "zapai*" --exclude "ipv6-hp-bpf*" --tags --always)
36+
ACN_VERSION ?= $(shell git describe --exclude "azure-iptables-monitor*" --exclude "azure-ip-masq-merger*" --exclude "azure-ipam*" --exclude "dropgz*" --exclude "zapai*" --exclude "ipv6-hp-bpf*" --exclude "block-iptables*" --tags --always)
3737
IPV6_HP_BPF_VERSION ?= $(notdir $(shell git describe --match "ipv6-hp-bpf*" --tags --always))
38+
BLOCK_IPTABLES_VERSION ?= $(notdir $(shell git describe --match "block-iptables*" --tags --always))
3839
AZURE_IPAM_VERSION ?= $(notdir $(shell git describe --match "azure-ipam*" --tags --always))
3940
AZURE_IP_MASQ_MERGER_VERSION ?= $(notdir $(shell git describe --match "azure-ip-masq-merger*" --tags --always))
4041
AZURE_IPTABLES_MONITOR_VERSION ?= $(notdir $(shell git describe --match "azure-iptables-monitor*" --tags --always))
@@ -48,6 +49,7 @@ AZURE_IPAM_DIR = $(REPO_ROOT)/azure-ipam
4849
AZURE_IP_MASQ_MERGER_DIR = $(REPO_ROOT)/azure-ip-masq-merger
4950
AZURE_IPTABLES_MONITOR_DIR = $(REPO_ROOT)/azure-iptables-monitor
5051
IPV6_HP_BPF_DIR = $(REPO_ROOT)/bpf-prog/ipv6-hp-bpf
52+
BLOCK_IPTABLES_DIR = $(REPO_ROOT)/bpf-prog/block-iptables
5153

5254
CNI_NET_DIR = $(REPO_ROOT)/cni/network/plugin
5355
CNI_IPAM_DIR = $(REPO_ROOT)/cni/ipam/plugin
@@ -63,6 +65,7 @@ AZURE_IPAM_BUILD_DIR = $(BUILD_DIR)/azure-ipam
6365
AZURE_IP_MASQ_MERGER_BUILD_DIR = $(BUILD_DIR)/azure-ip-masq-merger
6466
AZURE_IPTABLES_MONITOR_BUILD_DIR = $(BUILD_DIR)/azure-iptables-monitor
6567
IPV6_HP_BPF_BUILD_DIR = $(BUILD_DIR)/bpf-prog/ipv6-hp-bpf
68+
BLOCK_IPTABLES_BUILD_DIR = $(BUILD_DIR)/bpf-prog/block-iptables
6669
IMAGE_DIR = $(OUTPUT_DIR)/images
6770

6871
CNI_BUILD_DIR = $(BUILD_DIR)/cni
@@ -112,6 +115,7 @@ AZURE_IPAM_ARCHIVE_NAME = azure-ipam-$(GOOS)-$(GOARCH)-$(AZURE_IPAM_VERSION).$(A
112115
AZURE_IP_MASQ_MERGER_ARCHIVE_NAME = azure-ip-masq-merger-$(GOOS)-$(GOARCH)-$(AZURE_IP_MASQ_MERGER_VERSION).$(ARCHIVE_EXT)
113116
AZURE_IPTABLES_MONITOR_ARCHIVE_NAME = azure-iptables-monitor-$(GOOS)-$(GOARCH)-$(AZURE_IPTABLES_MONITOR_VERSION).$(ARCHIVE_EXT)
114117
IPV6_HP_BPF_ARCHIVE_NAME = ipv6-hp-bpf-$(GOOS)-$(GOARCH)-$(IPV6_HP_BPF_VERSION).$(ARCHIVE_EXT)
118+
BLOCK_IPTABLES_ARCHIVE_NAME = block-iptables-$(GOOS)-$(GOARCH)-$(BLOCK_IPTABLES_VERSION).$(ARCHIVE_EXT)
115119

116120
# Image info file names.
117121
CNI_IMAGE_INFO_FILE = azure-cni-$(CNI_VERSION).txt
@@ -128,7 +132,7 @@ all-binaries-platforms: ## Make all platform binaries
128132

129133
# OS specific binaries/images
130134
ifeq ($(GOOS),linux)
131-
all-binaries: acncli azure-cni-plugin azure-cns azure-npm azure-ipam azure-ip-masq-merger azure-iptables-monitor ipv6-hp-bpf
135+
all-binaries: acncli azure-cni-plugin azure-cns azure-npm azure-ipam azure-ip-masq-merger azure-iptables-monitor ipv6-hp-bpf block-iptables
132136
all-images: npm-image cns-image cni-manager-image azure-ip-masq-merger-image azure-iptables-monitor-image ipv6-hp-bpf-image
133137
else
134138
all-binaries: azure-cni-plugin azure-cns azure-npm
@@ -143,6 +147,7 @@ acncli: acncli-binary acncli-archive
143147
azure-npm: azure-npm-binary npm-archive
144148
azure-ipam: azure-ipam-binary azure-ipam-archive
145149
ipv6-hp-bpf: ipv6-hp-bpf-binary ipv6-hp-bpf-archive
150+
block-iptables: block-iptables-binary block-iptables-archive
146151
azure-ip-masq-merger: azure-ip-masq-merger-binary azure-ip-masq-merger-archive
147152
azure-iptables-monitor: azure-iptables-monitor-binary azure-iptables-monitor-archive
148153

@@ -169,6 +174,9 @@ azure-iptables-monitor-version: ## prints the azure-iptables-monitor version
169174
ipv6-hp-bpf-version: ## prints the ipv6-hp-bpf version
170175
@echo $(IPV6_HP_BPF_VERSION)
171176

177+
block-iptables-version: ## prints the block-iptables version
178+
@echo $(BLOCK_IPTABLES_VERSION)
179+
172180
cni-version: ## prints the cni version
173181
@echo $(CNI_VERSION)
174182

@@ -192,8 +200,8 @@ ipv6-hp-bpf-binary:
192200
cd $(IPV6_HP_BPF_DIR) && CGO_ENABLED=0 go generate ./...
193201
cd $(IPV6_HP_BPF_DIR)/cmd/ipv6-hp-bpf && CGO_ENABLED=0 go build -v -o $(IPV6_HP_BPF_BUILD_DIR)/ipv6-hp-bpf$(EXE_EXT) -ldflags "-X main.version=$(IPV6_HP_BPF_VERSION) $(LD_BUILD_FLAGS)" -gcflags="-dwarflocationlists=true"
194202

195-
# Libraries for ipv6-hp-bpf
196-
ipv6-hp-bpf-lib:
203+
# Libraries for bpf
204+
bpf-lib:
197205
ifeq ($(GOARCH),amd64)
198206
sudo apt-get update && sudo apt-get install -y llvm clang linux-libc-dev linux-headers-generic libbpf-dev libc6-dev nftables iproute2 gcc-multilib
199207
for dir in /usr/include/x86_64-linux-gnu/*; do sudo ln -sfn "$$dir" /usr/include/$$(basename "$$dir"); done
@@ -202,6 +210,11 @@ else ifeq ($(GOARCH),arm64)
202210
for dir in /usr/include/aarch64-linux-gnu/*; do sudo ln -sfn "$$dir" /usr/include/$$(basename "$$dir"); done
203211
endif
204212

213+
# Build the block-iptables binary.
214+
block-iptables-binary:
215+
cd $(BLOCK_IPTABLES_DIR) && CGO_ENABLED=0 go generate ./...
216+
cd $(BLOCK_IPTABLES_DIR)/cmd/block-iptables && CGO_ENABLED=0 go build -v -o $(BLOCK_IPTABLES_BUILD_DIR)/block-iptables$(EXE_EXT) -ldflags "-X main.version=$(BLOCK_IPTABLES_VERSION)" -gcflags="-dwarflocationlists=true"
217+
205218
# Build the Azure CNI network binary.
206219
azure-vnet-binary:
207220
cd $(CNI_NET_DIR) && CGO_ENABLED=0 go build -v -o $(CNI_BUILD_DIR)/azure-vnet$(EXE_EXT) -ldflags "-X main.version=$(CNI_VERSION) $(LD_BUILD_FLAGS)" -gcflags="-dwarflocationlists=true"
@@ -848,6 +861,14 @@ ifeq ($(GOOS),linux)
848861
cd $(IPV6_HP_BPF_BUILD_DIR) && $(ARCHIVE_CMD) $(IPV6_HP_BPF_ARCHIVE_NAME) ipv6-hp-bpf$(EXE_EXT)
849862
endif
850863

864+
# Create a block-iptables archive for the target platform.
865+
.PHONY: block-iptables-archive
866+
block-iptables-archive: block-iptables-binary
867+
ifeq ($(GOOS),linux)
868+
$(MKDIR) $(BLOCK_IPTABLES_BUILD_DIR)
869+
cd $(BLOCK_IPTABLES_BUILD_DIR) && $(ARCHIVE_CMD) $(BLOCK_IPTABLES_ARCHIVE_NAME) block-iptables$(EXE_EXT)
870+
endif
871+
851872
##@ Utils
852873

853874
clean: ## Clean build artifacts.

0 commit comments

Comments
 (0)