qosify-bpf.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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 bool
  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. bool ret = false;
  194. if (!config->bulk_trigger_pps)
  195. return false;
  196. time = cur_time();
  197. if (!flow->last_update)
  198. goto reset;
  199. delta = time - flow->last_update;
  200. if ((u32)delta > FLOW_TIMEOUT)
  201. goto reset;
  202. if (skb->gso_segs)
  203. segs = skb->gso_segs;
  204. flow->pkt_count += segs;
  205. if (flow->pkt_count > config->bulk_trigger_pps) {
  206. flow->bulk_timeout = config->bulk_trigger_timeout + 1;
  207. trigger = true;
  208. }
  209. if (delta >= FLOW_CHECK_INTERVAL) {
  210. if (flow->bulk_timeout && !trigger)
  211. flow->bulk_timeout--;
  212. goto clear;
  213. }
  214. goto out;
  215. reset:
  216. flow->pkt_len_avg = 0;
  217. clear:
  218. flow->pkt_count = 1;
  219. flow->last_update = time;
  220. out:
  221. if (flow->bulk_timeout) {
  222. *out_val = config->dscp_bulk;
  223. return true;
  224. }
  225. return false;
  226. }
  227. static __always_inline bool
  228. check_flow_prio(struct qosify_flow_config *config, struct __sk_buff *skb,
  229. struct flow_bucket *flow, __u8 *out_val)
  230. {
  231. int cur_len = skb->len;
  232. if (flow->bulk_timeout)
  233. return false;
  234. if (!config->prio_max_avg_pkt_len)
  235. return false;
  236. if (skb->gso_segs > 1)
  237. cur_len /= skb->gso_segs;
  238. if (ewma(&flow->pkt_len_avg, cur_len) <= config->prio_max_avg_pkt_len) {
  239. *out_val = config->dscp_prio;
  240. return true;
  241. }
  242. return false;
  243. }
  244. static __always_inline bool
  245. check_flow(struct qosify_flow_config *config, struct __sk_buff *skb,
  246. __u8 *out_val)
  247. {
  248. struct flow_bucket flow_data;
  249. struct flow_bucket *flow;
  250. __u32 hash;
  251. bool ret = false;
  252. if (!config)
  253. return false;
  254. if (!config->prio_max_avg_pkt_len && !config->bulk_trigger_pps)
  255. return false;
  256. hash = bpf_get_hash_recalc(skb);
  257. flow = bpf_map_lookup_elem(&flow_map, &hash);
  258. if (!flow) {
  259. memset(&flow_data, 0, sizeof(flow_data));
  260. bpf_map_update_elem(&flow_map, &hash, &flow_data, BPF_ANY);
  261. flow = bpf_map_lookup_elem(&flow_map, &hash);
  262. if (!flow)
  263. return false;
  264. }
  265. ret |= check_flow_bulk(config, skb, flow, out_val);
  266. ret |= check_flow_prio(config, skb, flow, out_val);
  267. return ret;
  268. }
  269. static __always_inline struct qosify_ip_map_val *
  270. parse_ipv4(struct qosify_config *config, struct skb_parser_info *info,
  271. bool ingress, __u8 *out_val)
  272. {
  273. struct iphdr *iph;
  274. __u8 ipproto;
  275. int hdr_len;
  276. void *key;
  277. iph = skb_parse_ipv4(info, sizeof(struct udphdr));
  278. if (!iph)
  279. return NULL;
  280. parse_l4proto(config, info, ingress, out_val);
  281. if (ingress)
  282. key = &iph->saddr;
  283. else
  284. key = &iph->daddr;
  285. return bpf_map_lookup_elem(&ipv4_map, key);
  286. }
  287. static __always_inline struct qosify_ip_map_val *
  288. parse_ipv6(struct qosify_config *config, struct skb_parser_info *info,
  289. bool ingress, __u8 *out_val)
  290. {
  291. struct ipv6hdr *iph;
  292. __u8 ipproto;
  293. void *key;
  294. iph = skb_parse_ipv6(info, sizeof(struct udphdr));
  295. if (!iph)
  296. return NULL;
  297. if (ingress)
  298. key = &iph->saddr;
  299. else
  300. key = &iph->daddr;
  301. parse_l4proto(config, info, ingress, out_val);
  302. return bpf_map_lookup_elem(&ipv6_map, key);
  303. }
  304. static __always_inline int
  305. dscp_lookup_class(uint8_t *dscp, bool ingress, struct qosify_class **out_class,
  306. bool counter)
  307. {
  308. struct qosify_class *class;
  309. __u8 fallback_flag;
  310. __u32 key;
  311. if (!(*dscp & QOSIFY_DSCP_CLASS_FLAG))
  312. return 0;
  313. fallback_flag = *dscp & QOSIFY_DSCP_FALLBACK_FLAG;
  314. key = *dscp & QOSIFY_DSCP_VALUE_MASK;
  315. class = bpf_map_lookup_elem(&class_map, &key);
  316. if (!class)
  317. return -1;
  318. if (!(class->flags & QOSIFY_CLASS_FLAG_PRESENT))
  319. return -1;
  320. if (counter)
  321. class->packets++;
  322. *dscp = dscp_val(&class->val, ingress);
  323. *dscp |= fallback_flag;
  324. *out_class = class;
  325. return 0;
  326. }
  327. SEC("tc")
  328. int classify(struct __sk_buff *skb)
  329. {
  330. struct skb_parser_info info;
  331. bool ingress = module_flags & QOSIFY_INGRESS;
  332. struct qosify_config *config;
  333. struct qosify_class *class = NULL;
  334. struct qosify_ip_map_val *ip_val;
  335. __u32 iph_offset;
  336. __u8 dscp = 0;
  337. void *iph;
  338. bool force;
  339. int type;
  340. config = get_config();
  341. if (!config)
  342. return TC_ACT_UNSPEC;
  343. skb_parse_init(&info, skb);
  344. if (module_flags & QOSIFY_IP_ONLY) {
  345. type = info.proto = skb->protocol;
  346. } else if (skb_parse_ethernet(&info)) {
  347. skb_parse_vlan(&info);
  348. skb_parse_vlan(&info);
  349. type = info.proto;
  350. } else {
  351. return TC_ACT_UNSPEC;
  352. }
  353. iph_offset = info.offset;
  354. if (type == bpf_htons(ETH_P_IP))
  355. ip_val = parse_ipv4(config, &info, ingress, &dscp);
  356. else if (type == bpf_htons(ETH_P_IPV6))
  357. ip_val = parse_ipv6(config, &info, ingress, &dscp);
  358. else
  359. return TC_ACT_UNSPEC;
  360. if (ip_val) {
  361. if (!ip_val->seen)
  362. ip_val->seen = 1;
  363. dscp = ip_val->dscp;
  364. }
  365. if (dscp_lookup_class(&dscp, ingress, &class, true))
  366. return TC_ACT_UNSPEC;
  367. if (class) {
  368. if (check_flow(&class->config, skb, &dscp) &&
  369. dscp_lookup_class(&dscp, ingress, &class, false))
  370. return TC_ACT_UNSPEC;
  371. }
  372. dscp &= GENMASK(5, 0);
  373. dscp <<= 2;
  374. force = !(dscp & QOSIFY_DSCP_FALLBACK_FLAG);
  375. if (type == bpf_htons(ETH_P_IP))
  376. ipv4_change_dsfield(skb, iph_offset, INET_ECN_MASK, dscp, force);
  377. else if (type == bpf_htons(ETH_P_IPV6))
  378. ipv6_change_dsfield(skb, iph_offset, INET_ECN_MASK, dscp, force);
  379. return TC_ACT_UNSPEC;
  380. }
  381. char _license[] SEC("license") = "GPL";