qosify-bpf.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2021 Felix Fietkau <nbd@nbd.name>
  4. */
  5. #define KBUILD_MODNAME "foo"
  6. #include <uapi/linux/bpf.h>
  7. #include <uapi/linux/if_ether.h>
  8. #include <uapi/linux/if_packet.h>
  9. #include <uapi/linux/ip.h>
  10. #include <uapi/linux/ipv6.h>
  11. #include <uapi/linux/in.h>
  12. #include <uapi/linux/tcp.h>
  13. #include <uapi/linux/udp.h>
  14. #include <uapi/linux/filter.h>
  15. #include <uapi/linux/pkt_cls.h>
  16. #include <linux/ip.h>
  17. #include <net/ipv6.h>
  18. #include <bpf/bpf_helpers.h>
  19. #include <bpf/bpf_endian.h>
  20. #include "bpf_skb_utils.h"
  21. #include "qosify-bpf.h"
  22. #define INET_ECN_MASK 3
  23. #define FLOW_CHECK_INTERVAL ((u32)((1000000000ULL) >> 24))
  24. #define FLOW_TIMEOUT ((u32)((30ULL * 1000000000ULL) >> 24))
  25. #define FLOW_BULK_TIMEOUT 5
  26. #define EWMA_SHIFT 12
  27. const volatile static uint32_t module_flags = 0;
  28. struct flow_bucket {
  29. __u32 last_update;
  30. __u32 pkt_len_avg;
  31. __u32 pkt_count;
  32. __u32 bulk_timeout;
  33. };
  34. struct {
  35. __uint(type, BPF_MAP_TYPE_ARRAY);
  36. __uint(pinning, 1);
  37. __type(key, __u32);
  38. __type(value, struct qosify_config);
  39. __uint(max_entries, 1);
  40. } config SEC(".maps");
  41. struct {
  42. __uint(type, BPF_MAP_TYPE_ARRAY);
  43. __uint(pinning, 1);
  44. __type(key, __u32);
  45. __type(value, __u8);
  46. __uint(max_entries, 1 << 16);
  47. } tcp_ports SEC(".maps");
  48. struct {
  49. __uint(type, BPF_MAP_TYPE_ARRAY);
  50. __uint(pinning, 1);
  51. __type(key, __u32);
  52. __type(value, __u8);
  53. __uint(max_entries, 1 << 16);
  54. } udp_ports SEC(".maps");
  55. struct {
  56. __uint(type, BPF_MAP_TYPE_LRU_HASH);
  57. __uint(pinning, 1);
  58. __type(key, __u32);
  59. __type(value, struct flow_bucket);
  60. __uint(max_entries, QOSIFY_FLOW_BUCKETS);
  61. } flow_map SEC(".maps");
  62. struct {
  63. __uint(type, BPF_MAP_TYPE_HASH);
  64. __uint(pinning, 1);
  65. __uint(key_size, sizeof(struct in_addr));
  66. __type(value, struct qosify_ip_map_val);
  67. __uint(max_entries, 100000);
  68. __uint(map_flags, BPF_F_NO_PREALLOC);
  69. } ipv4_map SEC(".maps");
  70. struct {
  71. __uint(type, BPF_MAP_TYPE_HASH);
  72. __uint(pinning, 1);
  73. __uint(key_size, sizeof(struct in6_addr));
  74. __type(value, struct qosify_ip_map_val);
  75. __uint(max_entries, 100000);
  76. __uint(map_flags, BPF_F_NO_PREALLOC);
  77. } ipv6_map SEC(".maps");
  78. struct {
  79. __uint(type, BPF_MAP_TYPE_ARRAY);
  80. __uint(pinning, 1);
  81. __type(key, __u32);
  82. __type(value, struct qosify_class);
  83. __uint(max_entries, QOSIFY_MAX_CLASS_ENTRIES +
  84. QOSIFY_DEFAULT_CLASS_ENTRIES);
  85. } class_map SEC(".maps");
  86. static struct qosify_config *get_config(void)
  87. {
  88. __u32 key = 0;
  89. return bpf_map_lookup_elem(&config, &key);
  90. }
  91. static __always_inline __u32 cur_time(void)
  92. {
  93. __u32 val = bpf_ktime_get_ns() >> 24;
  94. if (!val)
  95. val = 1;
  96. return val;
  97. }
  98. static __always_inline __u32 ewma(__u32 *avg, __u32 val)
  99. {
  100. if (*avg)
  101. *avg = (*avg * 3) / 4 + (val << EWMA_SHIFT) / 4;
  102. else
  103. *avg = val << EWMA_SHIFT;
  104. return *avg >> EWMA_SHIFT;
  105. }
  106. static __always_inline __u8 dscp_val(struct qosify_dscp_val *val, bool ingress)
  107. {
  108. __u8 ival = val->ingress;
  109. __u8 eval = val->egress;
  110. return ingress ? ival : eval;
  111. }
  112. static __always_inline void
  113. ipv4_change_dsfield(struct __sk_buff *skb, __u32 offset,
  114. __u8 mask, __u8 value, bool force)
  115. {
  116. struct iphdr *iph;
  117. __u32 check;
  118. __u8 dsfield;
  119. iph = skb_ptr(skb, offset, sizeof(*iph));
  120. if (!iph)
  121. return;
  122. check = bpf_ntohs(iph->check);
  123. if ((iph->tos & mask) && !force)
  124. return;
  125. dsfield = (iph->tos & mask) | value;
  126. if (iph->tos == dsfield)
  127. return;
  128. check += iph->tos;
  129. if ((check + 1) >> 16)
  130. check = (check + 1) & 0xffff;
  131. check -= dsfield;
  132. check += check >> 16;
  133. iph->check = bpf_htons(check);
  134. iph->tos = dsfield;
  135. }
  136. static __always_inline void
  137. ipv6_change_dsfield(struct __sk_buff *skb, __u32 offset,
  138. __u8 mask, __u8 value, bool force)
  139. {
  140. struct ipv6hdr *ipv6h;
  141. __u16 *p;
  142. __u16 val;
  143. ipv6h = skb_ptr(skb, offset, sizeof(*ipv6h));
  144. if (!ipv6h)
  145. return;
  146. p = (__u16 *)ipv6h;
  147. if (((*p >> 4) & mask) && !force)
  148. return;
  149. val = (*p & bpf_htons((((__u16)mask << 4) | 0xf00f))) | bpf_htons((__u16)value << 4);
  150. if (val == *p)
  151. return;
  152. *p = val;
  153. }
  154. static void
  155. parse_l4proto(struct qosify_config *config, struct skb_parser_info *info,
  156. bool ingress, __u8 *out_val)
  157. {
  158. struct udphdr *udp;
  159. __u32 src, dest, key;
  160. __u8 *value;
  161. __u8 proto = info->proto;
  162. udp = skb_info_ptr(info, sizeof(*udp));
  163. if (!udp)
  164. return;
  165. if (config && (proto == IPPROTO_ICMP || proto == IPPROTO_ICMPV6)) {
  166. *out_val = config->dscp_icmp;
  167. return;
  168. }
  169. src = READ_ONCE(udp->source);
  170. dest = READ_ONCE(udp->dest);
  171. if (ingress)
  172. key = src;
  173. else
  174. key = dest;
  175. if (proto == IPPROTO_TCP) {
  176. value = bpf_map_lookup_elem(&tcp_ports, &key);
  177. } else {
  178. if (proto != IPPROTO_UDP)
  179. key = 0;
  180. value = bpf_map_lookup_elem(&udp_ports, &key);
  181. }
  182. if (value)
  183. *out_val = *value;
  184. }
  185. static __always_inline void
  186. check_flow_bulk(struct qosify_flow_config *config, struct __sk_buff *skb,
  187. struct flow_bucket *flow, __u8 *out_val)
  188. {
  189. bool trigger = false;
  190. __s32 delta;
  191. __u32 time;
  192. int segs = 1;
  193. if (!config->bulk_trigger_pps)
  194. return;
  195. time = cur_time();
  196. if (!flow->last_update)
  197. goto reset;
  198. delta = time - flow->last_update;
  199. if ((u32)delta > FLOW_TIMEOUT)
  200. goto reset;
  201. if (skb->gso_segs)
  202. segs = skb->gso_segs;
  203. flow->pkt_count += segs;
  204. if (flow->pkt_count > config->bulk_trigger_pps) {
  205. flow->bulk_timeout = config->bulk_trigger_timeout + 1;
  206. trigger = true;
  207. }
  208. if (delta >= FLOW_CHECK_INTERVAL) {
  209. if (flow->bulk_timeout && !trigger)
  210. flow->bulk_timeout--;
  211. goto clear;
  212. }
  213. goto out;
  214. reset:
  215. flow->pkt_len_avg = 0;
  216. clear:
  217. flow->pkt_count = 1;
  218. flow->last_update = time;
  219. out:
  220. if (flow->bulk_timeout)
  221. *out_val = config->dscp_bulk;
  222. }
  223. static __always_inline void
  224. check_flow_prio(struct qosify_flow_config *config, struct __sk_buff *skb,
  225. struct flow_bucket *flow, __u8 *out_val)
  226. {
  227. int cur_len = skb->len;
  228. if (flow->bulk_timeout)
  229. return;
  230. if (!config->prio_max_avg_pkt_len)
  231. return;
  232. if (skb->gso_segs > 1)
  233. cur_len /= skb->gso_segs;
  234. if (ewma(&flow->pkt_len_avg, cur_len) <= config->prio_max_avg_pkt_len)
  235. *out_val = config->dscp_prio;
  236. }
  237. static __always_inline void
  238. check_flow(struct qosify_flow_config *config, struct __sk_buff *skb,
  239. __u8 *out_val)
  240. {
  241. struct flow_bucket flow_data;
  242. struct flow_bucket *flow;
  243. __u32 hash;
  244. if (!config)
  245. return;
  246. hash = bpf_get_hash_recalc(skb);
  247. flow = bpf_map_lookup_elem(&flow_map, &hash);
  248. if (!flow) {
  249. memset(&flow_data, 0, sizeof(flow_data));
  250. bpf_map_update_elem(&flow_map, &hash, &flow_data, BPF_ANY);
  251. flow = bpf_map_lookup_elem(&flow_map, &hash);
  252. if (!flow)
  253. return;
  254. }
  255. check_flow_bulk(config, skb, flow, out_val);
  256. check_flow_prio(config, skb, flow, out_val);
  257. }
  258. static __always_inline struct qosify_ip_map_val *
  259. parse_ipv4(struct qosify_config *config, struct skb_parser_info *info,
  260. bool ingress, __u8 *out_val)
  261. {
  262. struct iphdr *iph;
  263. __u8 ipproto;
  264. int hdr_len;
  265. void *key;
  266. iph = skb_parse_ipv4(info, sizeof(struct udphdr));
  267. if (!iph)
  268. return NULL;
  269. parse_l4proto(config, info, ingress, out_val);
  270. if (ingress)
  271. key = &iph->saddr;
  272. else
  273. key = &iph->daddr;
  274. return bpf_map_lookup_elem(&ipv4_map, key);
  275. }
  276. static __always_inline struct qosify_ip_map_val *
  277. parse_ipv6(struct qosify_config *config, struct skb_parser_info *info,
  278. bool ingress, __u8 *out_val)
  279. {
  280. struct ipv6hdr *iph;
  281. __u8 ipproto;
  282. void *key;
  283. iph = skb_parse_ipv6(info, sizeof(struct udphdr));
  284. if (!iph)
  285. return NULL;
  286. if (ingress)
  287. key = &iph->saddr;
  288. else
  289. key = &iph->daddr;
  290. parse_l4proto(config, info, ingress, out_val);
  291. return bpf_map_lookup_elem(&ipv6_map, key);
  292. }
  293. static __always_inline int
  294. dscp_lookup_class(uint8_t *dscp, bool ingress, struct qosify_class **out_class)
  295. {
  296. struct qosify_class *class;
  297. __u8 fallback_flag;
  298. __u32 key;
  299. if (!(*dscp & QOSIFY_DSCP_CLASS_FLAG))
  300. return 0;
  301. fallback_flag = *dscp & QOSIFY_DSCP_FALLBACK_FLAG;
  302. key = *dscp & QOSIFY_DSCP_VALUE_MASK;
  303. class = bpf_map_lookup_elem(&class_map, &key);
  304. if (!class)
  305. return -1;
  306. if (!(class->flags & QOSIFY_CLASS_FLAG_PRESENT))
  307. return -1;
  308. *dscp = dscp_val(&class->val, ingress);
  309. *dscp |= fallback_flag;
  310. *out_class = class;
  311. return 0;
  312. }
  313. SEC("tc")
  314. int classify(struct __sk_buff *skb)
  315. {
  316. struct skb_parser_info info;
  317. bool ingress = module_flags & QOSIFY_INGRESS;
  318. struct qosify_config *config;
  319. struct qosify_class *class = NULL;
  320. struct qosify_ip_map_val *ip_val;
  321. __u32 iph_offset;
  322. __u8 dscp = 0;
  323. void *iph;
  324. bool force;
  325. int type;
  326. config = get_config();
  327. if (!config)
  328. return TC_ACT_UNSPEC;
  329. skb_parse_init(&info, skb);
  330. if (module_flags & QOSIFY_IP_ONLY) {
  331. type = info.proto = skb->protocol;
  332. } else if (skb_parse_ethernet(&info)) {
  333. skb_parse_vlan(&info);
  334. skb_parse_vlan(&info);
  335. type = info.proto;
  336. } else {
  337. return TC_ACT_UNSPEC;
  338. }
  339. iph_offset = info.offset;
  340. if (type == bpf_htons(ETH_P_IP))
  341. ip_val = parse_ipv4(config, &info, ingress, &dscp);
  342. else if (type == bpf_htons(ETH_P_IPV6))
  343. ip_val = parse_ipv6(config, &info, ingress, &dscp);
  344. else
  345. return TC_ACT_UNSPEC;
  346. if (ip_val) {
  347. if (!ip_val->seen)
  348. ip_val->seen = 1;
  349. dscp = ip_val->dscp;
  350. }
  351. if (dscp_lookup_class(&dscp, ingress, &class))
  352. return TC_ACT_UNSPEC;
  353. if (class) {
  354. check_flow(&class->config, skb, &dscp);
  355. if (dscp_lookup_class(&dscp, ingress, &class))
  356. return TC_ACT_UNSPEC;
  357. }
  358. dscp &= GENMASK(5, 0);
  359. dscp <<= 2;
  360. force = !(dscp & QOSIFY_DSCP_FALLBACK_FLAG);
  361. if (type == bpf_htons(ETH_P_IP))
  362. ipv4_change_dsfield(skb, iph_offset, INET_ECN_MASK, dscp, force);
  363. else if (type == bpf_htons(ETH_P_IPV6))
  364. ipv6_change_dsfield(skb, iph_offset, INET_ECN_MASK, dscp, force);
  365. return TC_ACT_UNSPEC;
  366. }
  367. char _license[] SEC("license") = "GPL";