psock_fanout.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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/if.h>
  52. #include <net/ethernet.h>
  53. #include <netinet/ip.h>
  54. #include <netinet/udp.h>
  55. #include <poll.h>
  56. #include <sched.h>
  57. #include <stdint.h>
  58. #include <stdio.h>
  59. #include <stdlib.h>
  60. #include <string.h>
  61. #include <sys/mman.h>
  62. #include <sys/socket.h>
  63. #include <sys/stat.h>
  64. #include <sys/types.h>
  65. #include <unistd.h>
  66. #include "psock_lib.h"
  67. #define RING_NUM_FRAMES 20
  68. /* Open a socket in a given fanout mode.
  69. * @return -1 if mode is bad, a valid socket otherwise */
  70. static int sock_fanout_open(uint16_t typeflags, uint16_t group_id)
  71. {
  72. struct sockaddr_ll addr = {0};
  73. int fd, val;
  74. fd = socket(PF_PACKET, SOCK_RAW, 0);
  75. if (fd < 0) {
  76. perror("socket packet");
  77. exit(1);
  78. }
  79. pair_udp_setfilter(fd);
  80. addr.sll_family = AF_PACKET;
  81. addr.sll_protocol = htons(ETH_P_IP);
  82. addr.sll_ifindex = if_nametoindex("lo");
  83. if (addr.sll_ifindex == 0) {
  84. perror("if_nametoindex");
  85. exit(1);
  86. }
  87. if (bind(fd, (void *) &addr, sizeof(addr))) {
  88. perror("bind packet");
  89. exit(1);
  90. }
  91. val = (((int) typeflags) << 16) | group_id;
  92. if (setsockopt(fd, SOL_PACKET, PACKET_FANOUT, &val, sizeof(val))) {
  93. if (close(fd)) {
  94. perror("close packet");
  95. exit(1);
  96. }
  97. return -1;
  98. }
  99. return fd;
  100. }
  101. static void sock_fanout_set_cbpf(int fd)
  102. {
  103. struct sock_filter bpf_filter[] = {
  104. BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 80), /* ldb [80] */
  105. BPF_STMT(BPF_RET+BPF_A, 0), /* ret A */
  106. };
  107. struct sock_fprog bpf_prog;
  108. bpf_prog.filter = bpf_filter;
  109. bpf_prog.len = sizeof(bpf_filter) / sizeof(struct sock_filter);
  110. if (setsockopt(fd, SOL_PACKET, PACKET_FANOUT_DATA, &bpf_prog,
  111. sizeof(bpf_prog))) {
  112. perror("fanout data cbpf");
  113. exit(1);
  114. }
  115. }
  116. static void sock_fanout_getopts(int fd, uint16_t *typeflags, uint16_t *group_id)
  117. {
  118. int sockopt;
  119. socklen_t sockopt_len = sizeof(sockopt);
  120. if (getsockopt(fd, SOL_PACKET, PACKET_FANOUT,
  121. &sockopt, &sockopt_len)) {
  122. perror("failed to getsockopt");
  123. exit(1);
  124. }
  125. *typeflags = sockopt >> 16;
  126. *group_id = sockopt & 0xfffff;
  127. }
  128. static void sock_fanout_set_ebpf(int fd)
  129. {
  130. static char log_buf[65536];
  131. const int len_off = __builtin_offsetof(struct __sk_buff, len);
  132. struct bpf_insn prog[] = {
  133. { BPF_ALU64 | BPF_MOV | BPF_X, 6, 1, 0, 0 },
  134. { BPF_LDX | BPF_W | BPF_MEM, 0, 6, len_off, 0 },
  135. { BPF_JMP | BPF_JGE | BPF_K, 0, 0, 1, DATA_LEN },
  136. { BPF_JMP | BPF_JA | BPF_K, 0, 0, 4, 0 },
  137. { BPF_LD | BPF_B | BPF_ABS, 0, 0, 0, 0x50 },
  138. { BPF_JMP | BPF_JEQ | BPF_K, 0, 0, 2, DATA_CHAR },
  139. { BPF_JMP | BPF_JEQ | BPF_K, 0, 0, 1, DATA_CHAR_1 },
  140. { BPF_ALU | BPF_MOV | BPF_K, 0, 0, 0, 0 },
  141. { BPF_JMP | BPF_EXIT, 0, 0, 0, 0 }
  142. };
  143. union bpf_attr attr;
  144. int pfd;
  145. memset(&attr, 0, sizeof(attr));
  146. attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
  147. attr.insns = (unsigned long) prog;
  148. attr.insn_cnt = sizeof(prog) / sizeof(prog[0]);
  149. attr.license = (unsigned long) "GPL";
  150. attr.log_buf = (unsigned long) log_buf,
  151. attr.log_size = sizeof(log_buf),
  152. attr.log_level = 1,
  153. pfd = syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr));
  154. if (pfd < 0) {
  155. perror("bpf");
  156. fprintf(stderr, "bpf verifier:\n%s\n", log_buf);
  157. exit(1);
  158. }
  159. if (setsockopt(fd, SOL_PACKET, PACKET_FANOUT_DATA, &pfd, sizeof(pfd))) {
  160. perror("fanout data ebpf");
  161. exit(1);
  162. }
  163. if (close(pfd)) {
  164. perror("close ebpf");
  165. exit(1);
  166. }
  167. }
  168. static char *sock_fanout_open_ring(int fd)
  169. {
  170. struct tpacket_req req = {
  171. .tp_block_size = getpagesize(),
  172. .tp_frame_size = getpagesize(),
  173. .tp_block_nr = RING_NUM_FRAMES,
  174. .tp_frame_nr = RING_NUM_FRAMES,
  175. };
  176. char *ring;
  177. int val = TPACKET_V2;
  178. if (setsockopt(fd, SOL_PACKET, PACKET_VERSION, (void *) &val,
  179. sizeof(val))) {
  180. perror("packetsock ring setsockopt version");
  181. exit(1);
  182. }
  183. if (setsockopt(fd, SOL_PACKET, PACKET_RX_RING, (void *) &req,
  184. sizeof(req))) {
  185. perror("packetsock ring setsockopt");
  186. exit(1);
  187. }
  188. ring = mmap(0, req.tp_block_size * req.tp_block_nr,
  189. PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  190. if (ring == MAP_FAILED) {
  191. perror("packetsock ring mmap");
  192. exit(1);
  193. }
  194. return ring;
  195. }
  196. static int sock_fanout_read_ring(int fd, void *ring)
  197. {
  198. struct tpacket2_hdr *header = ring;
  199. int count = 0;
  200. while (count < RING_NUM_FRAMES && header->tp_status & TP_STATUS_USER) {
  201. count++;
  202. header = ring + (count * getpagesize());
  203. }
  204. return count;
  205. }
  206. static int sock_fanout_read(int fds[], char *rings[], const int expect[])
  207. {
  208. int ret[2];
  209. ret[0] = sock_fanout_read_ring(fds[0], rings[0]);
  210. ret[1] = sock_fanout_read_ring(fds[1], rings[1]);
  211. fprintf(stderr, "info: count=%d,%d, expect=%d,%d\n",
  212. ret[0], ret[1], expect[0], expect[1]);
  213. if ((!(ret[0] == expect[0] && ret[1] == expect[1])) &&
  214. (!(ret[0] == expect[1] && ret[1] == expect[0]))) {
  215. fprintf(stderr, "warning: incorrect queue lengths\n");
  216. return 1;
  217. }
  218. return 0;
  219. }
  220. /* Test illegal mode + flag combination */
  221. static void test_control_single(void)
  222. {
  223. fprintf(stderr, "test: control single socket\n");
  224. if (sock_fanout_open(PACKET_FANOUT_ROLLOVER |
  225. PACKET_FANOUT_FLAG_ROLLOVER, 0) != -1) {
  226. fprintf(stderr, "ERROR: opened socket with dual rollover\n");
  227. exit(1);
  228. }
  229. }
  230. /* Test illegal group with different modes or flags */
  231. static void test_control_group(void)
  232. {
  233. int fds[2];
  234. fprintf(stderr, "test: control multiple sockets\n");
  235. fds[0] = sock_fanout_open(PACKET_FANOUT_HASH, 0);
  236. if (fds[0] == -1) {
  237. fprintf(stderr, "ERROR: failed to open HASH socket\n");
  238. exit(1);
  239. }
  240. if (sock_fanout_open(PACKET_FANOUT_HASH |
  241. PACKET_FANOUT_FLAG_DEFRAG, 0) != -1) {
  242. fprintf(stderr, "ERROR: joined group with wrong flag defrag\n");
  243. exit(1);
  244. }
  245. if (sock_fanout_open(PACKET_FANOUT_HASH |
  246. PACKET_FANOUT_FLAG_ROLLOVER, 0) != -1) {
  247. fprintf(stderr, "ERROR: joined group with wrong flag ro\n");
  248. exit(1);
  249. }
  250. if (sock_fanout_open(PACKET_FANOUT_CPU, 0) != -1) {
  251. fprintf(stderr, "ERROR: joined group with wrong mode\n");
  252. exit(1);
  253. }
  254. fds[1] = sock_fanout_open(PACKET_FANOUT_HASH, 0);
  255. if (fds[1] == -1) {
  256. fprintf(stderr, "ERROR: failed to join group\n");
  257. exit(1);
  258. }
  259. if (close(fds[1]) || close(fds[0])) {
  260. fprintf(stderr, "ERROR: closing sockets\n");
  261. exit(1);
  262. }
  263. }
  264. /* Test creating a unique fanout group ids */
  265. static void test_unique_fanout_group_ids(void)
  266. {
  267. int fds[3];
  268. uint16_t typeflags, first_group_id, second_group_id;
  269. fprintf(stderr, "test: unique ids\n");
  270. fds[0] = sock_fanout_open(PACKET_FANOUT_HASH |
  271. PACKET_FANOUT_FLAG_UNIQUEID, 0);
  272. if (fds[0] == -1) {
  273. fprintf(stderr, "ERROR: failed to create a unique id group.\n");
  274. exit(1);
  275. }
  276. sock_fanout_getopts(fds[0], &typeflags, &first_group_id);
  277. if (typeflags != PACKET_FANOUT_HASH) {
  278. fprintf(stderr, "ERROR: unexpected typeflags %x\n", typeflags);
  279. exit(1);
  280. }
  281. if (sock_fanout_open(PACKET_FANOUT_CPU, first_group_id) != -1) {
  282. fprintf(stderr, "ERROR: joined group with wrong type.\n");
  283. exit(1);
  284. }
  285. fds[1] = sock_fanout_open(PACKET_FANOUT_HASH, first_group_id);
  286. if (fds[1] == -1) {
  287. fprintf(stderr,
  288. "ERROR: failed to join previously created group.\n");
  289. exit(1);
  290. }
  291. fds[2] = sock_fanout_open(PACKET_FANOUT_HASH |
  292. PACKET_FANOUT_FLAG_UNIQUEID, 0);
  293. if (fds[2] == -1) {
  294. fprintf(stderr,
  295. "ERROR: failed to create a second unique id group.\n");
  296. exit(1);
  297. }
  298. sock_fanout_getopts(fds[2], &typeflags, &second_group_id);
  299. if (sock_fanout_open(PACKET_FANOUT_HASH | PACKET_FANOUT_FLAG_UNIQUEID,
  300. second_group_id) != -1) {
  301. fprintf(stderr,
  302. "ERROR: specified a group id when requesting unique id\n");
  303. exit(1);
  304. }
  305. if (close(fds[0]) || close(fds[1]) || close(fds[2])) {
  306. fprintf(stderr, "ERROR: closing sockets\n");
  307. exit(1);
  308. }
  309. }
  310. static int test_datapath(uint16_t typeflags, int port_off,
  311. const int expect1[], const int expect2[])
  312. {
  313. const int expect0[] = { 0, 0 };
  314. char *rings[2];
  315. uint8_t type = typeflags & 0xFF;
  316. int fds[2], fds_udp[2][2], ret;
  317. fprintf(stderr, "\ntest: datapath 0x%hx ports %hu,%hu\n",
  318. typeflags, (uint16_t)PORT_BASE,
  319. (uint16_t)(PORT_BASE + port_off));
  320. fds[0] = sock_fanout_open(typeflags, 0);
  321. fds[1] = sock_fanout_open(typeflags, 0);
  322. if (fds[0] == -1 || fds[1] == -1) {
  323. fprintf(stderr, "ERROR: failed open\n");
  324. exit(1);
  325. }
  326. if (type == PACKET_FANOUT_CBPF)
  327. sock_fanout_set_cbpf(fds[0]);
  328. else if (type == PACKET_FANOUT_EBPF)
  329. sock_fanout_set_ebpf(fds[0]);
  330. rings[0] = sock_fanout_open_ring(fds[0]);
  331. rings[1] = sock_fanout_open_ring(fds[1]);
  332. pair_udp_open(fds_udp[0], PORT_BASE);
  333. pair_udp_open(fds_udp[1], PORT_BASE + port_off);
  334. sock_fanout_read(fds, rings, expect0);
  335. /* Send data, but not enough to overflow a queue */
  336. pair_udp_send(fds_udp[0], 15);
  337. pair_udp_send_char(fds_udp[1], 5, DATA_CHAR_1);
  338. ret = sock_fanout_read(fds, rings, expect1);
  339. /* Send more data, overflow the queue */
  340. pair_udp_send_char(fds_udp[0], 15, DATA_CHAR_1);
  341. /* TODO: ensure consistent order between expect1 and expect2 */
  342. ret |= sock_fanout_read(fds, rings, expect2);
  343. if (munmap(rings[1], RING_NUM_FRAMES * getpagesize()) ||
  344. munmap(rings[0], RING_NUM_FRAMES * getpagesize())) {
  345. fprintf(stderr, "close rings\n");
  346. exit(1);
  347. }
  348. if (close(fds_udp[1][1]) || close(fds_udp[1][0]) ||
  349. close(fds_udp[0][1]) || close(fds_udp[0][0]) ||
  350. close(fds[1]) || close(fds[0])) {
  351. fprintf(stderr, "close datapath\n");
  352. exit(1);
  353. }
  354. return ret;
  355. }
  356. static int set_cpuaffinity(int cpuid)
  357. {
  358. cpu_set_t mask;
  359. CPU_ZERO(&mask);
  360. CPU_SET(cpuid, &mask);
  361. if (sched_setaffinity(0, sizeof(mask), &mask)) {
  362. if (errno != EINVAL) {
  363. fprintf(stderr, "setaffinity %d\n", cpuid);
  364. exit(1);
  365. }
  366. return 1;
  367. }
  368. return 0;
  369. }
  370. int main(int argc, char **argv)
  371. {
  372. const int expect_hash[2][2] = { { 15, 5 }, { 20, 5 } };
  373. const int expect_hash_rb[2][2] = { { 15, 5 }, { 20, 15 } };
  374. const int expect_lb[2][2] = { { 10, 10 }, { 18, 17 } };
  375. const int expect_rb[2][2] = { { 15, 5 }, { 20, 15 } };
  376. const int expect_cpu0[2][2] = { { 20, 0 }, { 20, 0 } };
  377. const int expect_cpu1[2][2] = { { 0, 20 }, { 0, 20 } };
  378. const int expect_bpf[2][2] = { { 15, 5 }, { 15, 20 } };
  379. const int expect_uniqueid[2][2] = { { 20, 20}, { 20, 20 } };
  380. int port_off = 2, tries = 20, ret;
  381. test_control_single();
  382. test_control_group();
  383. test_unique_fanout_group_ids();
  384. /* find a set of ports that do not collide onto the same socket */
  385. ret = test_datapath(PACKET_FANOUT_HASH, port_off,
  386. expect_hash[0], expect_hash[1]);
  387. while (ret) {
  388. fprintf(stderr, "info: trying alternate ports (%d)\n", tries);
  389. ret = test_datapath(PACKET_FANOUT_HASH, ++port_off,
  390. expect_hash[0], expect_hash[1]);
  391. if (!--tries) {
  392. fprintf(stderr, "too many collisions\n");
  393. return 1;
  394. }
  395. }
  396. ret |= test_datapath(PACKET_FANOUT_HASH | PACKET_FANOUT_FLAG_ROLLOVER,
  397. port_off, expect_hash_rb[0], expect_hash_rb[1]);
  398. ret |= test_datapath(PACKET_FANOUT_LB,
  399. port_off, expect_lb[0], expect_lb[1]);
  400. ret |= test_datapath(PACKET_FANOUT_ROLLOVER,
  401. port_off, expect_rb[0], expect_rb[1]);
  402. ret |= test_datapath(PACKET_FANOUT_CBPF,
  403. port_off, expect_bpf[0], expect_bpf[1]);
  404. ret |= test_datapath(PACKET_FANOUT_EBPF,
  405. port_off, expect_bpf[0], expect_bpf[1]);
  406. set_cpuaffinity(0);
  407. ret |= test_datapath(PACKET_FANOUT_CPU, port_off,
  408. expect_cpu0[0], expect_cpu0[1]);
  409. if (!set_cpuaffinity(1))
  410. /* TODO: test that choice alternates with previous */
  411. ret |= test_datapath(PACKET_FANOUT_CPU, port_off,
  412. expect_cpu1[0], expect_cpu1[1]);
  413. ret |= test_datapath(PACKET_FANOUT_FLAG_UNIQUEID, port_off,
  414. expect_uniqueid[0], expect_uniqueid[1]);
  415. if (ret)
  416. return 1;
  417. printf("OK. All tests passed\n");
  418. return 0;
  419. }