qosify-bpf.c 11 KB

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