psock_fanout.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. * Copyright 2013 Google Inc.
  3. * Author: Willem de Bruijn (willemb@google.com)
  4. *
  5. * A basic test of packet socket fanout behavior.
  6. *
  7. * Control:
  8. * - create fanout fails as expected with illegal flag combinations
  9. * - join fanout fails as expected with diverging types or flags
  10. *
  11. * Datapath:
  12. * Open a pair of packet sockets and a pair of INET sockets, send a known
  13. * number of packets across the two INET sockets and count the number of
  14. * packets enqueued onto the two packet sockets.
  15. *
  16. * The test currently runs for
  17. * - PACKET_FANOUT_HASH
  18. * - PACKET_FANOUT_HASH with PACKET_FANOUT_FLAG_ROLLOVER
  19. * - PACKET_FANOUT_LB
  20. * - PACKET_FANOUT_CPU
  21. * - PACKET_FANOUT_ROLLOVER
  22. * - PACKET_FANOUT_CBPF
  23. * - PACKET_FANOUT_EBPF
  24. *
  25. * Todo:
  26. * - functionality: PACKET_FANOUT_FLAG_DEFRAG
  27. *
  28. * License (GPLv2):
  29. *
  30. * This program is free software; you can redistribute it and/or modify it
  31. * under the terms and conditions of the GNU General Public License,
  32. * version 2, as published by the Free Software Foundation.
  33. *
  34. * This program is distributed in the hope it will be useful, but WITHOUT
  35. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  36. * FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General Public License for
  37. * more details.
  38. *
  39. * You should have received a copy of the GNU General Public License along with
  40. * this program; if not, write to the Free Software Foundation, Inc.,
  41. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  42. */
  43. #define _GNU_SOURCE /* for sched_setaffinity */
  44. #include <arpa/inet.h>
  45. #include <errno.h>
  46. #include <fcntl.h>
  47. #include <linux/unistd.h> /* for __NR_bpf */
  48. #include <linux/filter.h>
  49. #include <linux/bpf.h>
  50. #include <linux/if_packet.h>
  51. #include <net/ethernet.h>
  52. #include <netinet/ip.h>
  53. #include <netinet/udp.h>
  54. #include <poll.h>
  55. #include <sched.h>
  56. #include <stdint.h>
  57. #include <stdio.h>
  58. #include <stdlib.h>
  59. #include <string.h>
  60. #include <sys/mman.h>
  61. #include <sys/socket.h>
  62. #include <sys/stat.h>
  63. #include <sys/types.h>
  64. #include <unistd.h>
  65. #include "psock_lib.h"
  66. #define RING_NUM_FRAMES 20
  67. /* Open a socket in a given fanout mode.
  68. * @return -1 if mode is bad, a valid socket otherwise */
  69. static int sock_fanout_open(uint16_t typeflags, int num_packets)
  70. {
  71. int fd, val;
  72. fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
  73. if (fd < 0) {
  74. perror("socket packet");
  75. exit(1);
  76. }
  77. /* fanout group ID is always 0: tests whether old groups are deleted */
  78. val = ((int) typeflags) << 16;
  79. if (setsockopt(fd, SOL_PACKET, PACKET_FANOUT, &val, sizeof(val))) {
  80. if (close(fd)) {
  81. perror("close packet");
  82. exit(1);
  83. }
  84. return -1;
  85. }
  86. pair_udp_setfilter(fd);
  87. return fd;
  88. }
  89. static void sock_fanout_set_ebpf(int fd)
  90. {
  91. static char log_buf[65536];
  92. const int len_off = __builtin_offsetof(struct __sk_buff, len);
  93. struct bpf_insn prog[] = {
  94. { BPF_ALU64 | BPF_MOV | BPF_X, 6, 1, 0, 0 },
  95. { BPF_LDX | BPF_W | BPF_MEM, 0, 6, len_off, 0 },
  96. { BPF_JMP | BPF_JGE | BPF_K, 0, 0, 1, DATA_LEN },
  97. { BPF_JMP | BPF_JA | BPF_K, 0, 0, 4, 0 },
  98. { BPF_LD | BPF_B | BPF_ABS, 0, 0, 0, 0x50 },
  99. { BPF_JMP | BPF_JEQ | BPF_K, 0, 0, 2, DATA_CHAR },
  100. { BPF_JMP | BPF_JEQ | BPF_K, 0, 0, 1, DATA_CHAR_1 },
  101. { BPF_ALU | BPF_MOV | BPF_K, 0, 0, 0, 0 },
  102. { BPF_JMP | BPF_EXIT, 0, 0, 0, 0 }
  103. };
  104. union bpf_attr attr;
  105. int pfd;
  106. memset(&attr, 0, sizeof(attr));
  107. attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
  108. attr.insns = (unsigned long) prog;
  109. attr.insn_cnt = sizeof(prog) / sizeof(prog[0]);
  110. attr.license = (unsigned long) "GPL";
  111. attr.log_buf = (unsigned long) log_buf,
  112. attr.log_size = sizeof(log_buf),
  113. attr.log_level = 1,
  114. pfd = syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr));
  115. if (pfd < 0) {
  116. perror("bpf");
  117. fprintf(stderr, "bpf verifier:\n%s\n", log_buf);
  118. exit(1);
  119. }
  120. if (setsockopt(fd, SOL_PACKET, PACKET_FANOUT_DATA, &pfd, sizeof(pfd))) {
  121. perror("fanout data ebpf");
  122. exit(1);
  123. }
  124. if (close(pfd)) {
  125. perror("close ebpf");
  126. exit(1);
  127. }
  128. }
  129. static char *sock_fanout_open_ring(int fd)
  130. {
  131. struct tpacket_req req = {
  132. .tp_block_size = getpagesize(),
  133. .tp_frame_size = getpagesize(),
  134. .tp_block_nr = RING_NUM_FRAMES,
  135. .tp_frame_nr = RING_NUM_FRAMES,
  136. };
  137. char *ring;
  138. int val = TPACKET_V2;
  139. if (setsockopt(fd, SOL_PACKET, PACKET_VERSION, (void *) &val,
  140. sizeof(val))) {
  141. perror("packetsock ring setsockopt version");
  142. exit(1);
  143. }
  144. if (setsockopt(fd, SOL_PACKET, PACKET_RX_RING, (void *) &req,
  145. sizeof(req))) {
  146. perror("packetsock ring setsockopt");
  147. exit(1);
  148. }
  149. ring = mmap(0, req.tp_block_size * req.tp_block_nr,
  150. PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  151. if (ring == MAP_FAILED) {
  152. perror("packetsock ring mmap");
  153. exit(1);
  154. }
  155. return ring;
  156. }
  157. static int sock_fanout_read_ring(int fd, void *ring)
  158. {
  159. struct tpacket2_hdr *header = ring;
  160. int count = 0;
  161. while (count < RING_NUM_FRAMES && header->tp_status & TP_STATUS_USER) {
  162. count++;
  163. header = ring + (count * getpagesize());
  164. }
  165. return count;
  166. }
  167. static int sock_fanout_read(int fds[], char *rings[], const int expect[])
  168. {
  169. int ret[2];
  170. ret[0] = sock_fanout_read_ring(fds[0], rings[0]);
  171. ret[1] = sock_fanout_read_ring(fds[1], rings[1]);
  172. fprintf(stderr, "info: count=%d,%d, expect=%d,%d\n",
  173. ret[0], ret[1], expect[0], expect[1]);
  174. if ((!(ret[0] == expect[0] && ret[1] == expect[1])) &&
  175. (!(ret[0] == expect[1] && ret[1] == expect[0]))) {
  176. fprintf(stderr, "ERROR: incorrect queue lengths\n");
  177. return 1;
  178. }
  179. return 0;
  180. }
  181. /* Test illegal mode + flag combination */
  182. static void test_control_single(void)
  183. {
  184. fprintf(stderr, "test: control single socket\n");
  185. if (sock_fanout_open(PACKET_FANOUT_ROLLOVER |
  186. PACKET_FANOUT_FLAG_ROLLOVER, 0) != -1) {
  187. fprintf(stderr, "ERROR: opened socket with dual rollover\n");
  188. exit(1);
  189. }
  190. }
  191. /* Test illegal group with different modes or flags */
  192. static void test_control_group(void)
  193. {
  194. int fds[2];
  195. fprintf(stderr, "test: control multiple sockets\n");
  196. fds[0] = sock_fanout_open(PACKET_FANOUT_HASH, 20);
  197. if (fds[0] == -1) {
  198. fprintf(stderr, "ERROR: failed to open HASH socket\n");
  199. exit(1);
  200. }
  201. if (sock_fanout_open(PACKET_FANOUT_HASH |
  202. PACKET_FANOUT_FLAG_DEFRAG, 10) != -1) {
  203. fprintf(stderr, "ERROR: joined group with wrong flag defrag\n");
  204. exit(1);
  205. }
  206. if (sock_fanout_open(PACKET_FANOUT_HASH |
  207. PACKET_FANOUT_FLAG_ROLLOVER, 10) != -1) {
  208. fprintf(stderr, "ERROR: joined group with wrong flag ro\n");
  209. exit(1);
  210. }
  211. if (sock_fanout_open(PACKET_FANOUT_CPU, 10) != -1) {
  212. fprintf(stderr, "ERROR: joined group with wrong mode\n");
  213. exit(1);
  214. }
  215. fds[1] = sock_fanout_open(PACKET_FANOUT_HASH, 20);
  216. if (fds[1] == -1) {
  217. fprintf(stderr, "ERROR: failed to join group\n");
  218. exit(1);
  219. }
  220. if (close(fds[1]) || close(fds[0])) {
  221. fprintf(stderr, "ERROR: closing sockets\n");
  222. exit(1);
  223. }
  224. }
  225. static int test_datapath(uint16_t typeflags, int port_off,
  226. const int expect1[], const int expect2[])
  227. {
  228. const int expect0[] = { 0, 0 };
  229. char *rings[2];
  230. uint8_t type = typeflags & 0xFF;
  231. int fds[2], fds_udp[2][2], ret;
  232. fprintf(stderr, "test: datapath 0x%hx\n", typeflags);
  233. fds[0] = sock_fanout_open(typeflags, 20);
  234. fds[1] = sock_fanout_open(typeflags, 20);
  235. if (fds[0] == -1 || fds[1] == -1) {
  236. fprintf(stderr, "ERROR: failed open\n");
  237. exit(1);
  238. }
  239. if (type == PACKET_FANOUT_CBPF)
  240. sock_setfilter(fds[0], SOL_PACKET, PACKET_FANOUT_DATA);
  241. else if (type == PACKET_FANOUT_EBPF)
  242. sock_fanout_set_ebpf(fds[0]);
  243. rings[0] = sock_fanout_open_ring(fds[0]);
  244. rings[1] = sock_fanout_open_ring(fds[1]);
  245. pair_udp_open(fds_udp[0], PORT_BASE);
  246. pair_udp_open(fds_udp[1], PORT_BASE + port_off);
  247. sock_fanout_read(fds, rings, expect0);
  248. /* Send data, but not enough to overflow a queue */
  249. pair_udp_send(fds_udp[0], 15);
  250. pair_udp_send_char(fds_udp[1], 5, DATA_CHAR_1);
  251. ret = sock_fanout_read(fds, rings, expect1);
  252. /* Send more data, overflow the queue */
  253. pair_udp_send_char(fds_udp[0], 15, DATA_CHAR_1);
  254. /* TODO: ensure consistent order between expect1 and expect2 */
  255. ret |= sock_fanout_read(fds, rings, expect2);
  256. if (munmap(rings[1], RING_NUM_FRAMES * getpagesize()) ||
  257. munmap(rings[0], RING_NUM_FRAMES * getpagesize())) {
  258. fprintf(stderr, "close rings\n");
  259. exit(1);
  260. }
  261. if (close(fds_udp[1][1]) || close(fds_udp[1][0]) ||
  262. close(fds_udp[0][1]) || close(fds_udp[0][0]) ||
  263. close(fds[1]) || close(fds[0])) {
  264. fprintf(stderr, "close datapath\n");
  265. exit(1);
  266. }
  267. return ret;
  268. }
  269. static int set_cpuaffinity(int cpuid)
  270. {
  271. cpu_set_t mask;
  272. CPU_ZERO(&mask);
  273. CPU_SET(cpuid, &mask);
  274. if (sched_setaffinity(0, sizeof(mask), &mask)) {
  275. if (errno != EINVAL) {
  276. fprintf(stderr, "setaffinity %d\n", cpuid);
  277. exit(1);
  278. }
  279. return 1;
  280. }
  281. return 0;
  282. }
  283. int main(int argc, char **argv)
  284. {
  285. const int expect_hash[2][2] = { { 15, 5 }, { 20, 5 } };
  286. const int expect_hash_rb[2][2] = { { 15, 5 }, { 20, 15 } };
  287. const int expect_lb[2][2] = { { 10, 10 }, { 18, 17 } };
  288. const int expect_rb[2][2] = { { 15, 5 }, { 20, 15 } };
  289. const int expect_cpu0[2][2] = { { 20, 0 }, { 20, 0 } };
  290. const int expect_cpu1[2][2] = { { 0, 20 }, { 0, 20 } };
  291. const int expect_bpf[2][2] = { { 15, 5 }, { 15, 20 } };
  292. int port_off = 2, tries = 5, ret;
  293. test_control_single();
  294. test_control_group();
  295. /* find a set of ports that do not collide onto the same socket */
  296. ret = test_datapath(PACKET_FANOUT_HASH, port_off,
  297. expect_hash[0], expect_hash[1]);
  298. while (ret && tries--) {
  299. fprintf(stderr, "info: trying alternate ports (%d)\n", tries);
  300. ret = test_datapath(PACKET_FANOUT_HASH, ++port_off,
  301. expect_hash[0], expect_hash[1]);
  302. }
  303. ret |= test_datapath(PACKET_FANOUT_HASH | PACKET_FANOUT_FLAG_ROLLOVER,
  304. port_off, expect_hash_rb[0], expect_hash_rb[1]);
  305. ret |= test_datapath(PACKET_FANOUT_LB,
  306. port_off, expect_lb[0], expect_lb[1]);
  307. ret |= test_datapath(PACKET_FANOUT_ROLLOVER,
  308. port_off, expect_rb[0], expect_rb[1]);
  309. ret |= test_datapath(PACKET_FANOUT_CBPF,
  310. port_off, expect_bpf[0], expect_bpf[1]);
  311. ret |= test_datapath(PACKET_FANOUT_EBPF,
  312. port_off, expect_bpf[0], expect_bpf[1]);
  313. set_cpuaffinity(0);
  314. ret |= test_datapath(PACKET_FANOUT_CPU, port_off,
  315. expect_cpu0[0], expect_cpu0[1]);
  316. if (!set_cpuaffinity(1))
  317. /* TODO: test that choice alternates with previous */
  318. ret |= test_datapath(PACKET_FANOUT_CPU, port_off,
  319. expect_cpu1[0], expect_cpu1[1]);
  320. if (ret)
  321. return 1;
  322. printf("OK. All tests passed\n");
  323. return 0;
  324. }