1 /* SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2019 Facebook
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.
9 * Include file for sample Host Bandwidth Manager (HBM) BPF programs
11 #define KBUILD_MODNAME "foo"
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>
24 #include <net/inet_ecn.h>
25 #include "bpf_endian.h"
26 #include "bpf_helpers.h"
33 #ifndef HBM_DEBUG // Define HBM_DEBUG to enable debugging
35 #define bpf_printk(fmt, ...)
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)
48 // rate in bytes per ns << 20
49 #define CREDIT_PER_NS(delta, rate) ((((u64)(delta)) * (rate)) >> 20)
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),
56 BPF_ANNOTATE_KV_PAIR(queue_state, struct bpf_cgroup_storage_key,
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),
65 BPF_ANNOTATE_KV_PAIR(queue_stats, int, struct hbm_queue_stats);
73 static __always_inline void hbm_get_pkt_info(struct __sk_buff *skb,
74 struct hbm_pkt_info *pkti)
79 bpf_skb_load_bytes(skb, 0, &iph, 12);
80 if (iph.version == 6) {
81 ip6h = (struct ipv6hdr *)&iph;
83 pkti->is_tcp = (ip6h->nexthdr == 6);
84 pkti->ecn = (ip6h->flow_lbl[0] >> 4) & INET_ECN_MASK;
85 } else if (iph.version == 4) {
87 pkti->is_tcp = (iph.protocol == 6);
88 pkti->ecn = iph.tos & INET_ECN_MASK;
96 static __always_inline void hbm_init_vqueue(struct hbm_vqueue *qdp, int rate)
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;
104 static __always_inline void hbm_update_stats(struct hbm_queue_stats *qsp,
106 unsigned long long curtime,
107 bool congestion_flag,
111 // Following is needed for work conserving
112 __sync_add_and_fetch(&(qsp->bytes_total), len);
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);
124 __sync_add_and_fetch(&(qsp->pkts_dropped), 1);
125 __sync_add_and_fetch(&(qsp->bytes_dropped),