test_sockmap_kern.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /* Copyright (c) 2017-2018 Covalent IO, Inc. http://covalent.io */
  3. #include <stddef.h>
  4. #include <string.h>
  5. #include <linux/bpf.h>
  6. #include <linux/if_ether.h>
  7. #include <linux/if_packet.h>
  8. #include <linux/ip.h>
  9. #include <linux/ipv6.h>
  10. #include <linux/in.h>
  11. #include <linux/udp.h>
  12. #include <linux/tcp.h>
  13. #include <linux/pkt_cls.h>
  14. #include <sys/socket.h>
  15. #include "bpf_helpers.h"
  16. #include "bpf_endian.h"
  17. /* Sockmap sample program connects a client and a backend together
  18. * using cgroups.
  19. *
  20. * client:X <---> frontend:80 client:X <---> backend:80
  21. *
  22. * For simplicity we hard code values here and bind 1:1. The hard
  23. * coded values are part of the setup in sockmap.sh script that
  24. * is associated with this BPF program.
  25. *
  26. * The bpf_printk is verbose and prints information as connections
  27. * are established and verdicts are decided.
  28. */
  29. #define bpf_printk(fmt, ...) \
  30. ({ \
  31. char ____fmt[] = fmt; \
  32. bpf_trace_printk(____fmt, sizeof(____fmt), \
  33. ##__VA_ARGS__); \
  34. })
  35. struct bpf_map_def SEC("maps") sock_map = {
  36. .type = TEST_MAP_TYPE,
  37. .key_size = sizeof(int),
  38. .value_size = sizeof(int),
  39. .max_entries = 20,
  40. };
  41. struct bpf_map_def SEC("maps") sock_map_txmsg = {
  42. .type = TEST_MAP_TYPE,
  43. .key_size = sizeof(int),
  44. .value_size = sizeof(int),
  45. .max_entries = 20,
  46. };
  47. struct bpf_map_def SEC("maps") sock_map_redir = {
  48. .type = TEST_MAP_TYPE,
  49. .key_size = sizeof(int),
  50. .value_size = sizeof(int),
  51. .max_entries = 20,
  52. };
  53. struct bpf_map_def SEC("maps") sock_apply_bytes = {
  54. .type = BPF_MAP_TYPE_ARRAY,
  55. .key_size = sizeof(int),
  56. .value_size = sizeof(int),
  57. .max_entries = 1
  58. };
  59. struct bpf_map_def SEC("maps") sock_cork_bytes = {
  60. .type = BPF_MAP_TYPE_ARRAY,
  61. .key_size = sizeof(int),
  62. .value_size = sizeof(int),
  63. .max_entries = 1
  64. };
  65. struct bpf_map_def SEC("maps") sock_bytes = {
  66. .type = BPF_MAP_TYPE_ARRAY,
  67. .key_size = sizeof(int),
  68. .value_size = sizeof(int),
  69. .max_entries = 4
  70. };
  71. struct bpf_map_def SEC("maps") sock_redir_flags = {
  72. .type = BPF_MAP_TYPE_ARRAY,
  73. .key_size = sizeof(int),
  74. .value_size = sizeof(int),
  75. .max_entries = 1
  76. };
  77. struct bpf_map_def SEC("maps") sock_skb_opts = {
  78. .type = BPF_MAP_TYPE_ARRAY,
  79. .key_size = sizeof(int),
  80. .value_size = sizeof(int),
  81. .max_entries = 1
  82. };
  83. SEC("sk_skb1")
  84. int bpf_prog1(struct __sk_buff *skb)
  85. {
  86. return skb->len;
  87. }
  88. SEC("sk_skb2")
  89. int bpf_prog2(struct __sk_buff *skb)
  90. {
  91. __u32 lport = skb->local_port;
  92. __u32 rport = skb->remote_port;
  93. int len, *f, ret, zero = 0;
  94. __u64 flags = 0;
  95. if (lport == 10000)
  96. ret = 10;
  97. else
  98. ret = 1;
  99. len = (__u32)skb->data_end - (__u32)skb->data;
  100. f = bpf_map_lookup_elem(&sock_skb_opts, &zero);
  101. if (f && *f) {
  102. ret = 3;
  103. flags = *f;
  104. }
  105. bpf_printk("sk_skb2: redirect(%iB) flags=%i\n",
  106. len, flags);
  107. #ifdef SOCKMAP
  108. return bpf_sk_redirect_map(skb, &sock_map, ret, flags);
  109. #else
  110. return bpf_sk_redirect_hash(skb, &sock_map, &ret, flags);
  111. #endif
  112. }
  113. SEC("sockops")
  114. int bpf_sockmap(struct bpf_sock_ops *skops)
  115. {
  116. __u32 lport, rport;
  117. int op, err = 0, index, key, ret;
  118. op = (int) skops->op;
  119. switch (op) {
  120. case BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB:
  121. lport = skops->local_port;
  122. rport = skops->remote_port;
  123. if (lport == 10000) {
  124. ret = 1;
  125. #ifdef SOCKMAP
  126. err = bpf_sock_map_update(skops, &sock_map, &ret,
  127. BPF_NOEXIST);
  128. #else
  129. err = bpf_sock_hash_update(skops, &sock_map, &ret,
  130. BPF_NOEXIST);
  131. #endif
  132. bpf_printk("passive(%i -> %i) map ctx update err: %d\n",
  133. lport, bpf_ntohl(rport), err);
  134. }
  135. break;
  136. case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB:
  137. lport = skops->local_port;
  138. rport = skops->remote_port;
  139. if (bpf_ntohl(rport) == 10001) {
  140. ret = 10;
  141. #ifdef SOCKMAP
  142. err = bpf_sock_map_update(skops, &sock_map, &ret,
  143. BPF_NOEXIST);
  144. #else
  145. err = bpf_sock_hash_update(skops, &sock_map, &ret,
  146. BPF_NOEXIST);
  147. #endif
  148. bpf_printk("active(%i -> %i) map ctx update err: %d\n",
  149. lport, bpf_ntohl(rport), err);
  150. }
  151. break;
  152. default:
  153. break;
  154. }
  155. return 0;
  156. }
  157. SEC("sk_msg1")
  158. int bpf_prog4(struct sk_msg_md *msg)
  159. {
  160. int *bytes, zero = 0, one = 1, two = 2, three = 3;
  161. int *start, *end, *start_push, *end_push;
  162. bytes = bpf_map_lookup_elem(&sock_apply_bytes, &zero);
  163. if (bytes)
  164. bpf_msg_apply_bytes(msg, *bytes);
  165. bytes = bpf_map_lookup_elem(&sock_cork_bytes, &zero);
  166. if (bytes)
  167. bpf_msg_cork_bytes(msg, *bytes);
  168. start = bpf_map_lookup_elem(&sock_bytes, &zero);
  169. end = bpf_map_lookup_elem(&sock_bytes, &one);
  170. if (start && end)
  171. bpf_msg_pull_data(msg, *start, *end, 0);
  172. start_push = bpf_map_lookup_elem(&sock_bytes, &two);
  173. end_push = bpf_map_lookup_elem(&sock_bytes, &three);
  174. if (start_push && end_push)
  175. bpf_msg_push_data(msg, *start_push, *end_push, 0);
  176. return SK_PASS;
  177. }
  178. SEC("sk_msg2")
  179. int bpf_prog5(struct sk_msg_md *msg)
  180. {
  181. int zero = 0, one = 1, two = 2, three = 3;
  182. int *start, *end, *start_push, *end_push;
  183. int *bytes, len1, len2 = 0, len3;
  184. int err1 = -1, err2 = -1;
  185. bytes = bpf_map_lookup_elem(&sock_apply_bytes, &zero);
  186. if (bytes)
  187. err1 = bpf_msg_apply_bytes(msg, *bytes);
  188. bytes = bpf_map_lookup_elem(&sock_cork_bytes, &zero);
  189. if (bytes)
  190. err2 = bpf_msg_cork_bytes(msg, *bytes);
  191. len1 = (__u64)msg->data_end - (__u64)msg->data;
  192. start = bpf_map_lookup_elem(&sock_bytes, &zero);
  193. end = bpf_map_lookup_elem(&sock_bytes, &one);
  194. if (start && end) {
  195. int err;
  196. bpf_printk("sk_msg2: pull(%i:%i)\n",
  197. start ? *start : 0, end ? *end : 0);
  198. err = bpf_msg_pull_data(msg, *start, *end, 0);
  199. if (err)
  200. bpf_printk("sk_msg2: pull_data err %i\n",
  201. err);
  202. len2 = (__u64)msg->data_end - (__u64)msg->data;
  203. bpf_printk("sk_msg2: length update %i->%i\n",
  204. len1, len2);
  205. }
  206. start_push = bpf_map_lookup_elem(&sock_bytes, &two);
  207. end_push = bpf_map_lookup_elem(&sock_bytes, &three);
  208. if (start_push && end_push) {
  209. int err;
  210. bpf_printk("sk_msg2: push(%i:%i)\n",
  211. start_push ? *start_push : 0,
  212. end_push ? *end_push : 0);
  213. err = bpf_msg_push_data(msg, *start_push, *end_push, 0);
  214. if (err)
  215. bpf_printk("sk_msg2: push_data err %i\n", err);
  216. len3 = (__u64)msg->data_end - (__u64)msg->data;
  217. bpf_printk("sk_msg2: length push_update %i->%i\n",
  218. len2 ? len2 : len1, len3);
  219. }
  220. bpf_printk("sk_msg2: data length %i err1 %i err2 %i\n",
  221. len1, err1, err2);
  222. return SK_PASS;
  223. }
  224. SEC("sk_msg3")
  225. int bpf_prog6(struct sk_msg_md *msg)
  226. {
  227. int *bytes, *start, *end, *start_push, *end_push, *f;
  228. int zero = 0, one = 1, two = 2, three = 3, key = 0;
  229. __u64 flags = 0;
  230. bytes = bpf_map_lookup_elem(&sock_apply_bytes, &zero);
  231. if (bytes)
  232. bpf_msg_apply_bytes(msg, *bytes);
  233. bytes = bpf_map_lookup_elem(&sock_cork_bytes, &zero);
  234. if (bytes)
  235. bpf_msg_cork_bytes(msg, *bytes);
  236. start = bpf_map_lookup_elem(&sock_bytes, &zero);
  237. end = bpf_map_lookup_elem(&sock_bytes, &one);
  238. if (start && end)
  239. bpf_msg_pull_data(msg, *start, *end, 0);
  240. start_push = bpf_map_lookup_elem(&sock_bytes, &two);
  241. end_push = bpf_map_lookup_elem(&sock_bytes, &three);
  242. if (start_push && end_push)
  243. bpf_msg_push_data(msg, *start_push, *end_push, 0);
  244. f = bpf_map_lookup_elem(&sock_redir_flags, &zero);
  245. if (f && *f) {
  246. key = 2;
  247. flags = *f;
  248. }
  249. #ifdef SOCKMAP
  250. return bpf_msg_redirect_map(msg, &sock_map_redir, key, flags);
  251. #else
  252. return bpf_msg_redirect_hash(msg, &sock_map_redir, &key, flags);
  253. #endif
  254. }
  255. SEC("sk_msg4")
  256. int bpf_prog7(struct sk_msg_md *msg)
  257. {
  258. int zero = 0, one = 1, two = 2, three = 3, len1, len2 = 0, len3;
  259. int *bytes, *start, *end, *start_push, *end_push, *f;
  260. int err1 = 0, err2 = 0, key = 0;
  261. __u64 flags = 0;
  262. int err;
  263. bytes = bpf_map_lookup_elem(&sock_apply_bytes, &zero);
  264. if (bytes)
  265. err1 = bpf_msg_apply_bytes(msg, *bytes);
  266. bytes = bpf_map_lookup_elem(&sock_cork_bytes, &zero);
  267. if (bytes)
  268. err2 = bpf_msg_cork_bytes(msg, *bytes);
  269. len1 = (__u64)msg->data_end - (__u64)msg->data;
  270. start = bpf_map_lookup_elem(&sock_bytes, &zero);
  271. end = bpf_map_lookup_elem(&sock_bytes, &one);
  272. if (start && end) {
  273. bpf_printk("sk_msg2: pull(%i:%i)\n",
  274. start ? *start : 0, end ? *end : 0);
  275. err = bpf_msg_pull_data(msg, *start, *end, 0);
  276. if (err)
  277. bpf_printk("sk_msg2: pull_data err %i\n",
  278. err);
  279. len2 = (__u64)msg->data_end - (__u64)msg->data;
  280. bpf_printk("sk_msg2: length update %i->%i\n",
  281. len1, len2);
  282. }
  283. start_push = bpf_map_lookup_elem(&sock_bytes, &two);
  284. end_push = bpf_map_lookup_elem(&sock_bytes, &three);
  285. if (start_push && end_push) {
  286. bpf_printk("sk_msg4: push(%i:%i)\n",
  287. start_push ? *start_push : 0,
  288. end_push ? *end_push : 0);
  289. err = bpf_msg_push_data(msg, *start_push, *end_push, 0);
  290. if (err)
  291. bpf_printk("sk_msg4: push_data err %i\n",
  292. err);
  293. len3 = (__u64)msg->data_end - (__u64)msg->data;
  294. bpf_printk("sk_msg4: length push_update %i->%i\n",
  295. len2 ? len2 : len1, len3);
  296. }
  297. f = bpf_map_lookup_elem(&sock_redir_flags, &zero);
  298. if (f && *f) {
  299. key = 2;
  300. flags = *f;
  301. }
  302. bpf_printk("sk_msg3: redirect(%iB) flags=%i err=%i\n",
  303. len1, flags, err1 ? err1 : err2);
  304. #ifdef SOCKMAP
  305. err = bpf_msg_redirect_map(msg, &sock_map_redir, key, flags);
  306. #else
  307. err = bpf_msg_redirect_hash(msg, &sock_map_redir, &key, flags);
  308. #endif
  309. bpf_printk("sk_msg3: err %i\n", err);
  310. return err;
  311. }
  312. SEC("sk_msg5")
  313. int bpf_prog8(struct sk_msg_md *msg)
  314. {
  315. void *data_end = (void *)(long) msg->data_end;
  316. void *data = (void *)(long) msg->data;
  317. int ret = 0, *bytes, zero = 0;
  318. bytes = bpf_map_lookup_elem(&sock_apply_bytes, &zero);
  319. if (bytes) {
  320. ret = bpf_msg_apply_bytes(msg, *bytes);
  321. if (ret)
  322. return SK_DROP;
  323. } else {
  324. return SK_DROP;
  325. }
  326. return SK_PASS;
  327. }
  328. SEC("sk_msg6")
  329. int bpf_prog9(struct sk_msg_md *msg)
  330. {
  331. void *data_end = (void *)(long) msg->data_end;
  332. void *data = (void *)(long) msg->data;
  333. int ret = 0, *bytes, zero = 0;
  334. bytes = bpf_map_lookup_elem(&sock_cork_bytes, &zero);
  335. if (bytes) {
  336. if (((__u64)data_end - (__u64)data) >= *bytes)
  337. return SK_PASS;
  338. ret = bpf_msg_cork_bytes(msg, *bytes);
  339. if (ret)
  340. return SK_DROP;
  341. }
  342. return SK_PASS;
  343. }
  344. SEC("sk_msg7")
  345. int bpf_prog10(struct sk_msg_md *msg)
  346. {
  347. int *bytes, *start, *end, *start_push, *end_push;
  348. int zero = 0, one = 1, two = 2, three = 3;
  349. bytes = bpf_map_lookup_elem(&sock_apply_bytes, &zero);
  350. if (bytes)
  351. bpf_msg_apply_bytes(msg, *bytes);
  352. bytes = bpf_map_lookup_elem(&sock_cork_bytes, &zero);
  353. if (bytes)
  354. bpf_msg_cork_bytes(msg, *bytes);
  355. start = bpf_map_lookup_elem(&sock_bytes, &zero);
  356. end = bpf_map_lookup_elem(&sock_bytes, &one);
  357. if (start && end)
  358. bpf_msg_pull_data(msg, *start, *end, 0);
  359. start_push = bpf_map_lookup_elem(&sock_bytes, &two);
  360. end_push = bpf_map_lookup_elem(&sock_bytes, &three);
  361. if (start_push && end_push)
  362. bpf_msg_push_data(msg, *start_push, *end_push, 0);
  363. return SK_DROP;
  364. }
  365. int _version SEC("version") = 1;
  366. char _license[] SEC("license") = "GPL";