xref: /linux/tools/testing/selftests/bpf/progs/test_tc_link.c (revision 71e0ad345163c150ea15434b37036b0678d5f6f4)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2023 Isovalent */
3 #include <stdbool.h>
4 
5 #include <linux/bpf.h>
6 #include <linux/if_ether.h>
7 #include <linux/stddef.h>
8 #include <linux/if_packet.h>
9 #include <bpf/bpf_endian.h>
10 #include <bpf/bpf_helpers.h>
11 
12 char LICENSE[] SEC("license") = "GPL";
13 
14 bool seen_tc1;
15 bool seen_tc2;
16 bool seen_tc3;
17 bool seen_tc4;
18 bool seen_tc5;
19 bool seen_tc6;
20 bool seen_tc7;
21 bool seen_tc8;
22 
23 bool set_type;
24 
25 bool seen_eth;
26 bool seen_host;
27 bool seen_mcast;
28 
29 int mark, prio;
30 
31 SEC("tc/ingress")
32 int tc1(struct __sk_buff *skb)
33 {
34 	struct ethhdr eth = {};
35 
36 	if (skb->protocol != __bpf_constant_htons(ETH_P_IP))
37 		goto out;
38 	if (bpf_skb_load_bytes(skb, 0, &eth, sizeof(eth)))
39 		goto out;
40 	seen_eth = eth.h_proto == bpf_htons(ETH_P_IP);
41 	seen_host = skb->pkt_type == PACKET_HOST;
42 	if (seen_host && set_type) {
43 		eth.h_dest[0] = 4;
44 		if (bpf_skb_store_bytes(skb, 0, &eth, sizeof(eth), 0))
45 			goto fail;
46 		bpf_skb_change_type(skb, PACKET_MULTICAST);
47 	}
48 out:
49 	seen_tc1 = true;
50 fail:
51 	return TCX_NEXT;
52 }
53 
54 SEC("tc/egress")
55 int tc2(struct __sk_buff *skb)
56 {
57 	seen_tc2 = true;
58 	return TCX_NEXT;
59 }
60 
61 SEC("tc/egress")
62 int tc3(struct __sk_buff *skb)
63 {
64 	seen_tc3 = true;
65 	return TCX_NEXT;
66 }
67 
68 SEC("tc/egress")
69 int tc4(struct __sk_buff *skb)
70 {
71 	seen_tc4 = true;
72 	return TCX_NEXT;
73 }
74 
75 SEC("tc/egress")
76 int tc5(struct __sk_buff *skb)
77 {
78 	seen_tc5 = true;
79 	return TCX_PASS;
80 }
81 
82 SEC("tc/egress")
83 int tc6(struct __sk_buff *skb)
84 {
85 	seen_tc6 = true;
86 	return TCX_PASS;
87 }
88 
89 SEC("tc/ingress")
90 int tc7(struct __sk_buff *skb)
91 {
92 	struct ethhdr eth = {};
93 
94 	if (skb->protocol != __bpf_constant_htons(ETH_P_IP))
95 		goto out;
96 	if (bpf_skb_load_bytes(skb, 0, &eth, sizeof(eth)))
97 		goto out;
98 	if (eth.h_dest[0] == 4 && set_type) {
99 		seen_mcast = skb->pkt_type == PACKET_MULTICAST;
100 		bpf_skb_change_type(skb, PACKET_HOST);
101 	}
102 out:
103 	seen_tc7 = true;
104 	return TCX_PASS;
105 }
106 
107 SEC("tc/egress")
108 int tc8(struct __sk_buff *skb)
109 {
110 	seen_tc8 = true;
111 	mark = skb->mark;
112 	prio = skb->priority;
113 	return TCX_PASS;
114 }
115