xref: /linux/samples/bpf/hbm_kern.h (revision 90eea4086d5ed31936889a44d536bf77afa4ca8a)
1 /* SPDX-License-Identifier: GPL-2.0
2  *
3  * Copyright (c) 2019 Facebook
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of version 2 of the GNU General Public
7  * License as published by the Free Software Foundation.
8  *
9  * Include file for sample Host Bandwidth Manager (HBM) BPF programs
10  */
11 #define KBUILD_MODNAME "foo"
12 #include <stddef.h>
13 #include <stdbool.h>
14 #include <uapi/linux/bpf.h>
15 #include <uapi/linux/if_ether.h>
16 #include <uapi/linux/if_packet.h>
17 #include <uapi/linux/ip.h>
18 #include <uapi/linux/ipv6.h>
19 #include <uapi/linux/in.h>
20 #include <uapi/linux/tcp.h>
21 #include <uapi/linux/filter.h>
22 #include <uapi/linux/pkt_cls.h>
23 #include <net/ipv6.h>
24 #include <net/inet_ecn.h>
25 #include "bpf_endian.h"
26 #include "bpf_helpers.h"
27 #include "hbm.h"
28 
29 #define DROP_PKT	0
30 #define ALLOW_PKT	1
31 #define TCP_ECN_OK	1
32 
33 #ifndef HBM_DEBUG  // Define HBM_DEBUG to enable debugging
34 #undef bpf_printk
35 #define bpf_printk(fmt, ...)
36 #endif
37 
38 #define INITIAL_CREDIT_PACKETS	100
39 #define MAX_BYTES_PER_PACKET	1500
40 #define MARK_THRESH		(40 * MAX_BYTES_PER_PACKET)
41 #define DROP_THRESH		(80 * 5 * MAX_BYTES_PER_PACKET)
42 #define LARGE_PKT_DROP_THRESH	(DROP_THRESH - (15 * MAX_BYTES_PER_PACKET))
43 #define MARK_REGION_SIZE	(LARGE_PKT_DROP_THRESH - MARK_THRESH)
44 #define LARGE_PKT_THRESH	120
45 #define MAX_CREDIT		(100 * MAX_BYTES_PER_PACKET)
46 #define INIT_CREDIT		(INITIAL_CREDIT_PACKETS * MAX_BYTES_PER_PACKET)
47 
48 // rate in bytes per ns << 20
49 #define CREDIT_PER_NS(delta, rate) ((((u64)(delta)) * (rate)) >> 20)
50 
51 struct bpf_map_def SEC("maps") queue_state = {
52 	.type = BPF_MAP_TYPE_CGROUP_STORAGE,
53 	.key_size = sizeof(struct bpf_cgroup_storage_key),
54 	.value_size = sizeof(struct hbm_vqueue),
55 };
56 BPF_ANNOTATE_KV_PAIR(queue_state, struct bpf_cgroup_storage_key,
57 		     struct hbm_vqueue);
58 
59 struct bpf_map_def SEC("maps") queue_stats = {
60 	.type = BPF_MAP_TYPE_ARRAY,
61 	.key_size = sizeof(u32),
62 	.value_size = sizeof(struct hbm_queue_stats),
63 	.max_entries = 1,
64 };
65 BPF_ANNOTATE_KV_PAIR(queue_stats, int, struct hbm_queue_stats);
66 
67 struct hbm_pkt_info {
68 	bool	is_ip;
69 	bool	is_tcp;
70 	short	ecn;
71 };
72 
73 static __always_inline void hbm_get_pkt_info(struct __sk_buff *skb,
74 					     struct hbm_pkt_info *pkti)
75 {
76 	struct iphdr iph;
77 	struct ipv6hdr *ip6h;
78 
79 	bpf_skb_load_bytes(skb, 0, &iph, 12);
80 	if (iph.version == 6) {
81 		ip6h = (struct ipv6hdr *)&iph;
82 		pkti->is_ip = true;
83 		pkti->is_tcp = (ip6h->nexthdr == 6);
84 		pkti->ecn = (ip6h->flow_lbl[0] >> 4) & INET_ECN_MASK;
85 	} else if (iph.version == 4) {
86 		pkti->is_ip = true;
87 		pkti->is_tcp = (iph.protocol == 6);
88 		pkti->ecn = iph.tos & INET_ECN_MASK;
89 	} else {
90 		pkti->is_ip = false;
91 		pkti->is_tcp = false;
92 		pkti->ecn = 0;
93 	}
94 }
95 
96 static __always_inline void hbm_init_vqueue(struct hbm_vqueue *qdp, int rate)
97 {
98 		bpf_printk("Initializing queue_state, rate:%d\n", rate * 128);
99 		qdp->lasttime = bpf_ktime_get_ns();
100 		qdp->credit = INIT_CREDIT;
101 		qdp->rate = rate * 128;
102 }
103 
104 static __always_inline void hbm_update_stats(struct hbm_queue_stats *qsp,
105 					     int len,
106 					     unsigned long long curtime,
107 					     bool congestion_flag,
108 					     bool drop_flag)
109 {
110 	if (qsp != NULL) {
111 		// Following is needed for work conserving
112 		__sync_add_and_fetch(&(qsp->bytes_total), len);
113 		if (qsp->stats) {
114 			// Optionally update statistics
115 			if (qsp->firstPacketTime == 0)
116 				qsp->firstPacketTime = curtime;
117 			qsp->lastPacketTime = curtime;
118 			__sync_add_and_fetch(&(qsp->pkts_total), 1);
119 			if (congestion_flag || drop_flag) {
120 				__sync_add_and_fetch(&(qsp->pkts_marked), 1);
121 				__sync_add_and_fetch(&(qsp->bytes_marked), len);
122 			}
123 			if (drop_flag) {
124 				__sync_add_and_fetch(&(qsp->pkts_dropped), 1);
125 				__sync_add_and_fetch(&(qsp->bytes_dropped),
126 						     len);
127 			}
128 		}
129 	}
130 }
131