bpf_qdisc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/types.h>
  3. #include <linux/bpf_verifier.h>
  4. #include <linux/bpf.h>
  5. #include <linux/btf.h>
  6. #include <linux/filter.h>
  7. #include <net/pkt_sched.h>
  8. #include <net/pkt_cls.h>
  9. #define QDISC_OP_IDX(op) (offsetof(struct Qdisc_ops, op) / sizeof(void (*)(void)))
  10. #define QDISC_MOFF_IDX(moff) (moff / sizeof(void (*)(void)))
  11. static struct bpf_struct_ops bpf_Qdisc_ops;
  12. struct bpf_sched_data {
  13. struct qdisc_watchdog watchdog;
  14. };
  15. struct bpf_sk_buff_ptr {
  16. struct sk_buff *skb;
  17. };
  18. static int bpf_qdisc_init(struct btf *btf)
  19. {
  20. return 0;
  21. }
  22. BTF_ID_LIST_SINGLE(bpf_qdisc_ids, struct, Qdisc)
  23. BTF_ID_LIST_SINGLE(bpf_sk_buff_ids, struct, sk_buff)
  24. BTF_ID_LIST_SINGLE(bpf_sk_buff_ptr_ids, struct, bpf_sk_buff_ptr)
  25. static bool bpf_qdisc_is_valid_access(int off, int size,
  26. enum bpf_access_type type,
  27. const struct bpf_prog *prog,
  28. struct bpf_insn_access_aux *info)
  29. {
  30. struct btf *btf = prog->aux->attach_btf;
  31. u32 arg;
  32. arg = btf_ctx_arg_idx(btf, prog->aux->attach_func_proto, off);
  33. if (prog->aux->attach_st_ops_member_off == offsetof(struct Qdisc_ops, enqueue)) {
  34. if (arg == 2 && type == BPF_READ) {
  35. info->reg_type = PTR_TO_BTF_ID | PTR_TRUSTED;
  36. info->btf = btf;
  37. info->btf_id = bpf_sk_buff_ptr_ids[0];
  38. return true;
  39. }
  40. }
  41. return bpf_tracing_btf_ctx_access(off, size, type, prog, info);
  42. }
  43. static int bpf_qdisc_qdisc_access(struct bpf_verifier_log *log,
  44. const struct bpf_reg_state *reg,
  45. int off, size_t *end)
  46. {
  47. switch (off) {
  48. case offsetof(struct Qdisc, limit):
  49. *end = offsetofend(struct Qdisc, limit);
  50. break;
  51. case offsetof(struct Qdisc, q) + offsetof(struct qdisc_skb_head, qlen):
  52. *end = offsetof(struct Qdisc, q) + offsetofend(struct qdisc_skb_head, qlen);
  53. break;
  54. case offsetof(struct Qdisc, qstats) ... offsetofend(struct Qdisc, qstats) - 1:
  55. *end = offsetofend(struct Qdisc, qstats);
  56. break;
  57. default:
  58. return -EACCES;
  59. }
  60. return 0;
  61. }
  62. static int bpf_qdisc_sk_buff_access(struct bpf_verifier_log *log,
  63. const struct bpf_reg_state *reg,
  64. int off, size_t *end)
  65. {
  66. switch (off) {
  67. case offsetof(struct sk_buff, tstamp):
  68. *end = offsetofend(struct sk_buff, tstamp);
  69. break;
  70. case offsetof(struct sk_buff, cb) + offsetof(struct qdisc_skb_cb, data[0]) ...
  71. offsetof(struct sk_buff, cb) + offsetof(struct qdisc_skb_cb,
  72. data[QDISC_CB_PRIV_LEN - 1]):
  73. *end = offsetof(struct sk_buff, cb) +
  74. offsetofend(struct qdisc_skb_cb, data[QDISC_CB_PRIV_LEN - 1]);
  75. break;
  76. default:
  77. return -EACCES;
  78. }
  79. return 0;
  80. }
  81. static int bpf_qdisc_btf_struct_access(struct bpf_verifier_log *log,
  82. const struct bpf_reg_state *reg,
  83. int off, int size)
  84. {
  85. const struct btf_type *t, *skbt, *qdisct;
  86. size_t end;
  87. int err;
  88. skbt = btf_type_by_id(reg->btf, bpf_sk_buff_ids[0]);
  89. qdisct = btf_type_by_id(reg->btf, bpf_qdisc_ids[0]);
  90. t = btf_type_by_id(reg->btf, reg->btf_id);
  91. if (t == skbt) {
  92. err = bpf_qdisc_sk_buff_access(log, reg, off, &end);
  93. } else if (t == qdisct) {
  94. err = bpf_qdisc_qdisc_access(log, reg, off, &end);
  95. } else {
  96. bpf_log(log, "only read is supported\n");
  97. return -EACCES;
  98. }
  99. if (err) {
  100. bpf_log(log, "no write support to %s at off %d\n",
  101. btf_name_by_offset(reg->btf, t->name_off), off);
  102. return -EACCES;
  103. }
  104. if (off + size > end) {
  105. bpf_log(log,
  106. "write access at off %d with size %d beyond the member of %s ended at %zu\n",
  107. off, size, btf_name_by_offset(reg->btf, t->name_off), end);
  108. return -EACCES;
  109. }
  110. return 0;
  111. }
  112. BTF_ID_LIST_SINGLE(bpf_qdisc_init_prologue_ids, func, bpf_qdisc_init_prologue)
  113. static int bpf_qdisc_gen_prologue(struct bpf_insn *insn_buf, bool direct_write,
  114. const struct bpf_prog *prog)
  115. {
  116. struct bpf_insn *insn = insn_buf;
  117. if (prog->aux->attach_st_ops_member_off != offsetof(struct Qdisc_ops, init))
  118. return 0;
  119. /* r6 = r1; // r6 will be "u64 *ctx". r1 is "u64 *ctx".
  120. * r2 = r1[16]; // r2 will be "struct netlink_ext_ack *extack"
  121. * r1 = r1[0]; // r1 will be "struct Qdisc *sch"
  122. * r0 = bpf_qdisc_init_prologue(r1, r2);
  123. * if r0 == 0 goto pc+1;
  124. * BPF_EXIT;
  125. * r1 = r6; // r1 will be "u64 *ctx".
  126. */
  127. *insn++ = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);
  128. *insn++ = BPF_LDX_MEM(BPF_DW, BPF_REG_2, BPF_REG_1, 16);
  129. *insn++ = BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_1, 0);
  130. *insn++ = BPF_CALL_KFUNC(0, bpf_qdisc_init_prologue_ids[0]);
  131. *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 1);
  132. *insn++ = BPF_EXIT_INSN();
  133. *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6);
  134. *insn++ = prog->insnsi[0];
  135. return insn - insn_buf;
  136. }
  137. BTF_ID_LIST_SINGLE(bpf_qdisc_reset_destroy_epilogue_ids, func, bpf_qdisc_reset_destroy_epilogue)
  138. static int bpf_qdisc_gen_epilogue(struct bpf_insn *insn_buf, const struct bpf_prog *prog,
  139. s16 ctx_stack_off)
  140. {
  141. struct bpf_insn *insn = insn_buf;
  142. if (prog->aux->attach_st_ops_member_off != offsetof(struct Qdisc_ops, reset) &&
  143. prog->aux->attach_st_ops_member_off != offsetof(struct Qdisc_ops, destroy))
  144. return 0;
  145. /* r1 = stack[ctx_stack_off]; // r1 will be "u64 *ctx"
  146. * r1 = r1[0]; // r1 will be "struct Qdisc *sch"
  147. * r0 = bpf_qdisc_reset_destroy_epilogue(r1);
  148. * BPF_EXIT;
  149. */
  150. *insn++ = BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_FP, ctx_stack_off);
  151. *insn++ = BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_1, 0);
  152. *insn++ = BPF_CALL_KFUNC(0, bpf_qdisc_reset_destroy_epilogue_ids[0]);
  153. *insn++ = BPF_EXIT_INSN();
  154. return insn - insn_buf;
  155. }
  156. __bpf_kfunc_start_defs();
  157. /* bpf_skb_get_hash - Get the flow hash of an skb.
  158. * @skb: The skb to get the flow hash from.
  159. */
  160. __bpf_kfunc u32 bpf_skb_get_hash(struct sk_buff *skb)
  161. {
  162. return skb_get_hash(skb);
  163. }
  164. /* bpf_kfree_skb - Release an skb's reference and drop it immediately.
  165. * @skb: The skb whose reference to be released and dropped.
  166. */
  167. __bpf_kfunc void bpf_kfree_skb(struct sk_buff *skb)
  168. {
  169. kfree_skb(skb);
  170. }
  171. __bpf_kfunc void bpf_kfree_skb_dtor(void *skb)
  172. {
  173. bpf_kfree_skb(skb);
  174. }
  175. CFI_NOSEAL(bpf_kfree_skb_dtor);
  176. /* bpf_qdisc_skb_drop - Drop an skb by adding it to a deferred free list.
  177. * @skb: The skb whose reference to be released and dropped.
  178. * @to_free_list: The list of skbs to be dropped.
  179. */
  180. __bpf_kfunc void bpf_qdisc_skb_drop(struct sk_buff *skb,
  181. struct bpf_sk_buff_ptr *to_free_list)
  182. {
  183. __qdisc_drop(skb, (struct sk_buff **)to_free_list);
  184. }
  185. /* bpf_qdisc_watchdog_schedule - Schedule a qdisc to a later time using a timer.
  186. * @sch: The qdisc to be scheduled.
  187. * @expire: The expiry time of the timer.
  188. * @delta_ns: The slack range of the timer.
  189. */
  190. __bpf_kfunc void bpf_qdisc_watchdog_schedule(struct Qdisc *sch, u64 expire, u64 delta_ns)
  191. {
  192. struct bpf_sched_data *q = qdisc_priv(sch);
  193. qdisc_watchdog_schedule_range_ns(&q->watchdog, expire, delta_ns);
  194. }
  195. /* bpf_qdisc_init_prologue - Hidden kfunc called in prologue of .init. */
  196. __bpf_kfunc int bpf_qdisc_init_prologue(struct Qdisc *sch,
  197. struct netlink_ext_ack *extack)
  198. {
  199. struct bpf_sched_data *q = qdisc_priv(sch);
  200. struct net_device *dev = qdisc_dev(sch);
  201. struct Qdisc *p;
  202. qdisc_watchdog_init(&q->watchdog, sch);
  203. if (sch->parent != TC_H_ROOT) {
  204. /* If qdisc_lookup() returns NULL, it means .init is called by
  205. * qdisc_create_dflt() in mq/mqprio_init and the parent qdisc
  206. * has not been added to qdisc_hash yet.
  207. */
  208. p = qdisc_lookup(dev, TC_H_MAJ(sch->parent));
  209. if (p && !(p->flags & TCQ_F_MQROOT)) {
  210. NL_SET_ERR_MSG(extack, "BPF qdisc only supported on root or mq");
  211. return -EINVAL;
  212. }
  213. }
  214. return 0;
  215. }
  216. /* bpf_qdisc_reset_destroy_epilogue - Hidden kfunc called in epilogue of .reset
  217. * and .destroy
  218. */
  219. __bpf_kfunc void bpf_qdisc_reset_destroy_epilogue(struct Qdisc *sch)
  220. {
  221. struct bpf_sched_data *q = qdisc_priv(sch);
  222. qdisc_watchdog_cancel(&q->watchdog);
  223. }
  224. /* bpf_qdisc_bstats_update - Update Qdisc basic statistics
  225. * @sch: The qdisc from which an skb is dequeued.
  226. * @skb: The skb to be dequeued.
  227. */
  228. __bpf_kfunc void bpf_qdisc_bstats_update(struct Qdisc *sch, const struct sk_buff *skb)
  229. {
  230. bstats_update(&sch->bstats, skb);
  231. }
  232. __bpf_kfunc_end_defs();
  233. BTF_KFUNCS_START(qdisc_kfunc_ids)
  234. BTF_ID_FLAGS(func, bpf_skb_get_hash)
  235. BTF_ID_FLAGS(func, bpf_kfree_skb, KF_RELEASE)
  236. BTF_ID_FLAGS(func, bpf_qdisc_skb_drop, KF_RELEASE)
  237. BTF_ID_FLAGS(func, bpf_dynptr_from_skb)
  238. BTF_ID_FLAGS(func, bpf_qdisc_watchdog_schedule)
  239. BTF_ID_FLAGS(func, bpf_qdisc_init_prologue)
  240. BTF_ID_FLAGS(func, bpf_qdisc_reset_destroy_epilogue)
  241. BTF_ID_FLAGS(func, bpf_qdisc_bstats_update)
  242. BTF_KFUNCS_END(qdisc_kfunc_ids)
  243. BTF_SET_START(qdisc_common_kfunc_set)
  244. BTF_ID(func, bpf_skb_get_hash)
  245. BTF_ID(func, bpf_kfree_skb)
  246. BTF_ID(func, bpf_dynptr_from_skb)
  247. BTF_SET_END(qdisc_common_kfunc_set)
  248. BTF_SET_START(qdisc_enqueue_kfunc_set)
  249. BTF_ID(func, bpf_qdisc_skb_drop)
  250. BTF_ID(func, bpf_qdisc_watchdog_schedule)
  251. BTF_SET_END(qdisc_enqueue_kfunc_set)
  252. BTF_SET_START(qdisc_dequeue_kfunc_set)
  253. BTF_ID(func, bpf_qdisc_watchdog_schedule)
  254. BTF_ID(func, bpf_qdisc_bstats_update)
  255. BTF_SET_END(qdisc_dequeue_kfunc_set)
  256. enum qdisc_ops_kf_flags {
  257. QDISC_OPS_KF_COMMON = 0,
  258. QDISC_OPS_KF_ENQUEUE = 1 << 0,
  259. QDISC_OPS_KF_DEQUEUE = 1 << 1,
  260. };
  261. static const u32 qdisc_ops_context_flags[] = {
  262. [QDISC_OP_IDX(enqueue)] = QDISC_OPS_KF_ENQUEUE,
  263. [QDISC_OP_IDX(dequeue)] = QDISC_OPS_KF_DEQUEUE,
  264. [QDISC_OP_IDX(init)] = QDISC_OPS_KF_COMMON,
  265. [QDISC_OP_IDX(reset)] = QDISC_OPS_KF_COMMON,
  266. [QDISC_OP_IDX(destroy)] = QDISC_OPS_KF_COMMON,
  267. };
  268. static int bpf_qdisc_kfunc_filter(const struct bpf_prog *prog, u32 kfunc_id)
  269. {
  270. u32 moff, flags;
  271. if (!btf_id_set8_contains(&qdisc_kfunc_ids, kfunc_id))
  272. return 0;
  273. if (prog->aux->st_ops != &bpf_Qdisc_ops)
  274. return -EACCES;
  275. moff = prog->aux->attach_st_ops_member_off;
  276. flags = qdisc_ops_context_flags[QDISC_MOFF_IDX(moff)];
  277. if ((flags & QDISC_OPS_KF_ENQUEUE) &&
  278. btf_id_set_contains(&qdisc_enqueue_kfunc_set, kfunc_id))
  279. return 0;
  280. if ((flags & QDISC_OPS_KF_DEQUEUE) &&
  281. btf_id_set_contains(&qdisc_dequeue_kfunc_set, kfunc_id))
  282. return 0;
  283. if (btf_id_set_contains(&qdisc_common_kfunc_set, kfunc_id))
  284. return 0;
  285. return -EACCES;
  286. }
  287. static const struct btf_kfunc_id_set bpf_qdisc_kfunc_set = {
  288. .owner = THIS_MODULE,
  289. .set = &qdisc_kfunc_ids,
  290. .filter = bpf_qdisc_kfunc_filter,
  291. };
  292. static const struct bpf_verifier_ops bpf_qdisc_verifier_ops = {
  293. .get_func_proto = bpf_base_func_proto,
  294. .is_valid_access = bpf_qdisc_is_valid_access,
  295. .btf_struct_access = bpf_qdisc_btf_struct_access,
  296. .gen_prologue = bpf_qdisc_gen_prologue,
  297. .gen_epilogue = bpf_qdisc_gen_epilogue,
  298. };
  299. static int bpf_qdisc_init_member(const struct btf_type *t,
  300. const struct btf_member *member,
  301. void *kdata, const void *udata)
  302. {
  303. const struct Qdisc_ops *uqdisc_ops;
  304. struct Qdisc_ops *qdisc_ops;
  305. u32 moff;
  306. uqdisc_ops = (const struct Qdisc_ops *)udata;
  307. qdisc_ops = (struct Qdisc_ops *)kdata;
  308. moff = __btf_member_bit_offset(t, member) / 8;
  309. switch (moff) {
  310. case offsetof(struct Qdisc_ops, priv_size):
  311. if (uqdisc_ops->priv_size)
  312. return -EINVAL;
  313. qdisc_ops->priv_size = sizeof(struct bpf_sched_data);
  314. return 1;
  315. case offsetof(struct Qdisc_ops, peek):
  316. qdisc_ops->peek = qdisc_peek_dequeued;
  317. return 0;
  318. case offsetof(struct Qdisc_ops, id):
  319. if (bpf_obj_name_cpy(qdisc_ops->id, uqdisc_ops->id,
  320. sizeof(qdisc_ops->id)) <= 0)
  321. return -EINVAL;
  322. return 1;
  323. }
  324. return 0;
  325. }
  326. static int bpf_qdisc_reg(void *kdata, struct bpf_link *link)
  327. {
  328. return register_qdisc(kdata);
  329. }
  330. static void bpf_qdisc_unreg(void *kdata, struct bpf_link *link)
  331. {
  332. return unregister_qdisc(kdata);
  333. }
  334. static int bpf_qdisc_validate(void *kdata)
  335. {
  336. struct Qdisc_ops *ops = (struct Qdisc_ops *)kdata;
  337. if (!ops->enqueue || !ops->dequeue || !ops->init ||
  338. !ops->reset || !ops->destroy)
  339. return -EINVAL;
  340. return 0;
  341. }
  342. static int Qdisc_ops__enqueue(struct sk_buff *skb__ref, struct Qdisc *sch,
  343. struct sk_buff **to_free)
  344. {
  345. return 0;
  346. }
  347. static struct sk_buff *Qdisc_ops__dequeue(struct Qdisc *sch)
  348. {
  349. return NULL;
  350. }
  351. static int Qdisc_ops__init(struct Qdisc *sch, struct nlattr *arg,
  352. struct netlink_ext_ack *extack)
  353. {
  354. return 0;
  355. }
  356. static void Qdisc_ops__reset(struct Qdisc *sch)
  357. {
  358. }
  359. static void Qdisc_ops__destroy(struct Qdisc *sch)
  360. {
  361. }
  362. static struct Qdisc_ops __bpf_ops_qdisc_ops = {
  363. .enqueue = Qdisc_ops__enqueue,
  364. .dequeue = Qdisc_ops__dequeue,
  365. .init = Qdisc_ops__init,
  366. .reset = Qdisc_ops__reset,
  367. .destroy = Qdisc_ops__destroy,
  368. };
  369. static struct bpf_struct_ops bpf_Qdisc_ops = {
  370. .verifier_ops = &bpf_qdisc_verifier_ops,
  371. .reg = bpf_qdisc_reg,
  372. .unreg = bpf_qdisc_unreg,
  373. .validate = bpf_qdisc_validate,
  374. .init_member = bpf_qdisc_init_member,
  375. .init = bpf_qdisc_init,
  376. .name = "Qdisc_ops",
  377. .cfi_stubs = &__bpf_ops_qdisc_ops,
  378. .owner = THIS_MODULE,
  379. };
  380. BTF_ID_LIST_SINGLE(bpf_sk_buff_dtor_ids, func, bpf_kfree_skb_dtor)
  381. static int __init bpf_qdisc_kfunc_init(void)
  382. {
  383. int ret;
  384. const struct btf_id_dtor_kfunc skb_kfunc_dtors[] = {
  385. {
  386. .btf_id = bpf_sk_buff_ids[0],
  387. .kfunc_btf_id = bpf_sk_buff_dtor_ids[0]
  388. },
  389. };
  390. ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &bpf_qdisc_kfunc_set);
  391. ret = ret ?: register_btf_id_dtor_kfuncs(skb_kfunc_dtors,
  392. ARRAY_SIZE(skb_kfunc_dtors),
  393. THIS_MODULE);
  394. ret = ret ?: register_bpf_struct_ops(&bpf_Qdisc_ops, Qdisc_ops);
  395. return ret;
  396. }
  397. late_initcall(bpf_qdisc_kfunc_init);