test_sockmap_kern.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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_pull_bytes = {
  66. .type = BPF_MAP_TYPE_ARRAY,
  67. .key_size = sizeof(int),
  68. .value_size = sizeof(int),
  69. .max_entries = 2
  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;
  161. int *start, *end;
  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_pull_bytes, &zero);
  169. end = bpf_map_lookup_elem(&sock_pull_bytes, &one);
  170. if (start && end)
  171. bpf_msg_pull_data(msg, *start, *end, 0);
  172. return SK_PASS;
  173. }
  174. SEC("sk_msg2")
  175. int bpf_prog5(struct sk_msg_md *msg)
  176. {
  177. int err1 = -1, err2 = -1, zero = 0, one = 1;
  178. int *bytes, *start, *end, len1, len2;
  179. bytes = bpf_map_lookup_elem(&sock_apply_bytes, &zero);
  180. if (bytes)
  181. err1 = bpf_msg_apply_bytes(msg, *bytes);
  182. bytes = bpf_map_lookup_elem(&sock_cork_bytes, &zero);
  183. if (bytes)
  184. err2 = bpf_msg_cork_bytes(msg, *bytes);
  185. len1 = (__u64)msg->data_end - (__u64)msg->data;
  186. start = bpf_map_lookup_elem(&sock_pull_bytes, &zero);
  187. end = bpf_map_lookup_elem(&sock_pull_bytes, &one);
  188. if (start && end) {
  189. int err;
  190. bpf_printk("sk_msg2: pull(%i:%i)\n",
  191. start ? *start : 0, end ? *end : 0);
  192. err = bpf_msg_pull_data(msg, *start, *end, 0);
  193. if (err)
  194. bpf_printk("sk_msg2: pull_data err %i\n",
  195. err);
  196. len2 = (__u64)msg->data_end - (__u64)msg->data;
  197. bpf_printk("sk_msg2: length update %i->%i\n",
  198. len1, len2);
  199. }
  200. bpf_printk("sk_msg2: data length %i err1 %i err2 %i\n",
  201. len1, err1, err2);
  202. return SK_PASS;
  203. }
  204. SEC("sk_msg3")
  205. int bpf_prog6(struct sk_msg_md *msg)
  206. {
  207. int *bytes, zero = 0, one = 1, key = 0;
  208. int *start, *end, *f;
  209. __u64 flags = 0;
  210. bytes = bpf_map_lookup_elem(&sock_apply_bytes, &zero);
  211. if (bytes)
  212. bpf_msg_apply_bytes(msg, *bytes);
  213. bytes = bpf_map_lookup_elem(&sock_cork_bytes, &zero);
  214. if (bytes)
  215. bpf_msg_cork_bytes(msg, *bytes);
  216. start = bpf_map_lookup_elem(&sock_pull_bytes, &zero);
  217. end = bpf_map_lookup_elem(&sock_pull_bytes, &one);
  218. if (start && end)
  219. bpf_msg_pull_data(msg, *start, *end, 0);
  220. f = bpf_map_lookup_elem(&sock_redir_flags, &zero);
  221. if (f && *f) {
  222. key = 2;
  223. flags = *f;
  224. }
  225. #ifdef SOCKMAP
  226. return bpf_msg_redirect_map(msg, &sock_map_redir, key, flags);
  227. #else
  228. return bpf_msg_redirect_hash(msg, &sock_map_redir, &key, flags);
  229. #endif
  230. }
  231. SEC("sk_msg4")
  232. int bpf_prog7(struct sk_msg_md *msg)
  233. {
  234. int err1 = 0, err2 = 0, zero = 0, one = 1, key = 0;
  235. int *f, *bytes, *start, *end, len1, len2;
  236. __u64 flags = 0;
  237. int err;
  238. bytes = bpf_map_lookup_elem(&sock_apply_bytes, &zero);
  239. if (bytes)
  240. err1 = bpf_msg_apply_bytes(msg, *bytes);
  241. bytes = bpf_map_lookup_elem(&sock_cork_bytes, &zero);
  242. if (bytes)
  243. err2 = bpf_msg_cork_bytes(msg, *bytes);
  244. len1 = (__u64)msg->data_end - (__u64)msg->data;
  245. start = bpf_map_lookup_elem(&sock_pull_bytes, &zero);
  246. end = bpf_map_lookup_elem(&sock_pull_bytes, &one);
  247. if (start && end) {
  248. bpf_printk("sk_msg2: pull(%i:%i)\n",
  249. start ? *start : 0, end ? *end : 0);
  250. err = bpf_msg_pull_data(msg, *start, *end, 0);
  251. if (err)
  252. bpf_printk("sk_msg2: pull_data err %i\n",
  253. err);
  254. len2 = (__u64)msg->data_end - (__u64)msg->data;
  255. bpf_printk("sk_msg2: length update %i->%i\n",
  256. len1, len2);
  257. }
  258. f = bpf_map_lookup_elem(&sock_redir_flags, &zero);
  259. if (f && *f) {
  260. key = 2;
  261. flags = *f;
  262. }
  263. bpf_printk("sk_msg3: redirect(%iB) flags=%i err=%i\n",
  264. len1, flags, err1 ? err1 : err2);
  265. #ifdef SOCKMAP
  266. err = bpf_msg_redirect_map(msg, &sock_map_redir, key, flags);
  267. #else
  268. err = bpf_msg_redirect_hash(msg, &sock_map_redir, &key, flags);
  269. #endif
  270. bpf_printk("sk_msg3: err %i\n", err);
  271. return err;
  272. }
  273. SEC("sk_msg5")
  274. int bpf_prog8(struct sk_msg_md *msg)
  275. {
  276. void *data_end = (void *)(long) msg->data_end;
  277. void *data = (void *)(long) msg->data;
  278. int ret = 0, *bytes, zero = 0;
  279. bytes = bpf_map_lookup_elem(&sock_apply_bytes, &zero);
  280. if (bytes) {
  281. ret = bpf_msg_apply_bytes(msg, *bytes);
  282. if (ret)
  283. return SK_DROP;
  284. } else {
  285. return SK_DROP;
  286. }
  287. return SK_PASS;
  288. }
  289. SEC("sk_msg6")
  290. int bpf_prog9(struct sk_msg_md *msg)
  291. {
  292. void *data_end = (void *)(long) msg->data_end;
  293. void *data = (void *)(long) msg->data;
  294. int ret = 0, *bytes, zero = 0;
  295. bytes = bpf_map_lookup_elem(&sock_cork_bytes, &zero);
  296. if (bytes) {
  297. if (((__u64)data_end - (__u64)data) >= *bytes)
  298. return SK_PASS;
  299. ret = bpf_msg_cork_bytes(msg, *bytes);
  300. if (ret)
  301. return SK_DROP;
  302. }
  303. return SK_PASS;
  304. }
  305. SEC("sk_msg7")
  306. int bpf_prog10(struct sk_msg_md *msg)
  307. {
  308. int *bytes, zero = 0, one = 1;
  309. int *start, *end;
  310. bytes = bpf_map_lookup_elem(&sock_apply_bytes, &zero);
  311. if (bytes)
  312. bpf_msg_apply_bytes(msg, *bytes);
  313. bytes = bpf_map_lookup_elem(&sock_cork_bytes, &zero);
  314. if (bytes)
  315. bpf_msg_cork_bytes(msg, *bytes);
  316. start = bpf_map_lookup_elem(&sock_pull_bytes, &zero);
  317. end = bpf_map_lookup_elem(&sock_pull_bytes, &one);
  318. if (start && end)
  319. bpf_msg_pull_data(msg, *start, *end, 0);
  320. return SK_DROP;
  321. }
  322. int _version SEC("version") = 1;
  323. char _license[] SEC("license") = "GPL";