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 #define HBM_DEBUG 0 // Set to 1 to enable debugging
35 #define bpf_printk(fmt, ...) \
37 char ____fmt[] = fmt; \
38 bpf_trace_printk(____fmt, sizeof(____fmt), \
42 #define bpf_printk(fmt, ...)
45 #define INITIAL_CREDIT_PACKETS 100
46 #define MAX_BYTES_PER_PACKET 1500
47 #define MARK_THRESH (40 * MAX_BYTES_PER_PACKET)
48 #define DROP_THRESH (80 * 5 * MAX_BYTES_PER_PACKET)
49 #define LARGE_PKT_DROP_THRESH (DROP_THRESH - (15 * MAX_BYTES_PER_PACKET))
50 #define MARK_REGION_SIZE (LARGE_PKT_DROP_THRESH - MARK_THRESH)
51 #define LARGE_PKT_THRESH 120
52 #define MAX_CREDIT (100 * MAX_BYTES_PER_PACKET)
53 #define INIT_CREDIT (INITIAL_CREDIT_PACKETS * MAX_BYTES_PER_PACKET)
55 // rate in bytes per ns << 20
56 #define CREDIT_PER_NS(delta, rate) ((((u64)(delta)) * (rate)) >> 20)
58 struct bpf_map_def
SEC("maps") queue_state
= {
59 .type
= BPF_MAP_TYPE_CGROUP_STORAGE
,
60 .key_size
= sizeof(struct bpf_cgroup_storage_key
),
61 .value_size
= sizeof(struct hbm_vqueue
),
63 BPF_ANNOTATE_KV_PAIR(queue_state
, struct bpf_cgroup_storage_key
,
66 struct bpf_map_def
SEC("maps") queue_stats
= {
67 .type
= BPF_MAP_TYPE_ARRAY
,
68 .key_size
= sizeof(u32
),
69 .value_size
= sizeof(struct hbm_queue_stats
),
72 BPF_ANNOTATE_KV_PAIR(queue_stats
, int, struct hbm_queue_stats
);
80 static __always_inline
void hbm_get_pkt_info(struct __sk_buff
*skb
,
81 struct hbm_pkt_info
*pkti
)
86 bpf_skb_load_bytes(skb
, 0, &iph
, 12);
87 if (iph
.version
== 6) {
88 ip6h
= (struct ipv6hdr
*)&iph
;
90 pkti
->is_tcp
= (ip6h
->nexthdr
== 6);
91 pkti
->ecn
= (ip6h
->flow_lbl
[0] >> 4) & INET_ECN_MASK
;
92 } else if (iph
.version
== 4) {
94 pkti
->is_tcp
= (iph
.protocol
== 6);
95 pkti
->ecn
= iph
.tos
& INET_ECN_MASK
;
103 static __always_inline
void hbm_init_vqueue(struct hbm_vqueue
*qdp
, int rate
)
105 bpf_printk("Initializing queue_state, rate:%d\n", rate
* 128);
106 qdp
->lasttime
= bpf_ktime_get_ns();
107 qdp
->credit
= INIT_CREDIT
;
108 qdp
->rate
= rate
* 128;
111 static __always_inline
void hbm_update_stats(struct hbm_queue_stats
*qsp
,
113 unsigned long long curtime
,
114 bool congestion_flag
,
118 // Following is needed for work conserving
119 __sync_add_and_fetch(&(qsp
->bytes_total
), len
);
121 // Optionally update statistics
122 if (qsp
->firstPacketTime
== 0)
123 qsp
->firstPacketTime
= curtime
;
124 qsp
->lastPacketTime
= curtime
;
125 __sync_add_and_fetch(&(qsp
->pkts_total
), 1);
126 if (congestion_flag
|| drop_flag
) {
127 __sync_add_and_fetch(&(qsp
->pkts_marked
), 1);
128 __sync_add_and_fetch(&(qsp
->bytes_marked
), len
);
131 __sync_add_and_fetch(&(qsp
->pkts_dropped
), 1);
132 __sync_add_and_fetch(&(qsp
->bytes_dropped
),