psock_fanout.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. *
  23. * Todo:
  24. * - functionality: PACKET_FANOUT_FLAG_DEFRAG
  25. *
  26. * License (GPLv2):
  27. *
  28. * This program is free software; you can redistribute it and/or modify it
  29. * under the terms and conditions of the GNU General Public License,
  30. * version 2, as published by the Free Software Foundation.
  31. *
  32. * This program is distributed in the hope it will be useful, but WITHOUT
  33. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  34. * FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General Public License for
  35. * more details.
  36. *
  37. * You should have received a copy of the GNU General Public License along with
  38. * this program; if not, write to the Free Software Foundation, Inc.,
  39. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  40. */
  41. #define _GNU_SOURCE /* for sched_setaffinity */
  42. #include <arpa/inet.h>
  43. #include <errno.h>
  44. #include <fcntl.h>
  45. #include <linux/filter.h>
  46. #include <linux/if_packet.h>
  47. #include <net/ethernet.h>
  48. #include <netinet/ip.h>
  49. #include <netinet/udp.h>
  50. #include <poll.h>
  51. #include <sched.h>
  52. #include <stdint.h>
  53. #include <stdio.h>
  54. #include <stdlib.h>
  55. #include <string.h>
  56. #include <sys/mman.h>
  57. #include <sys/socket.h>
  58. #include <sys/stat.h>
  59. #include <sys/types.h>
  60. #include <unistd.h>
  61. #include "psock_lib.h"
  62. #define RING_NUM_FRAMES 20
  63. /* Open a socket in a given fanout mode.
  64. * @return -1 if mode is bad, a valid socket otherwise */
  65. static int sock_fanout_open(uint16_t typeflags, int num_packets)
  66. {
  67. int fd, val;
  68. fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
  69. if (fd < 0) {
  70. perror("socket packet");
  71. exit(1);
  72. }
  73. /* fanout group ID is always 0: tests whether old groups are deleted */
  74. val = ((int) typeflags) << 16;
  75. if (setsockopt(fd, SOL_PACKET, PACKET_FANOUT, &val, sizeof(val))) {
  76. if (close(fd)) {
  77. perror("close packet");
  78. exit(1);
  79. }
  80. return -1;
  81. }
  82. pair_udp_setfilter(fd);
  83. return fd;
  84. }
  85. static char *sock_fanout_open_ring(int fd)
  86. {
  87. struct tpacket_req req = {
  88. .tp_block_size = getpagesize(),
  89. .tp_frame_size = getpagesize(),
  90. .tp_block_nr = RING_NUM_FRAMES,
  91. .tp_frame_nr = RING_NUM_FRAMES,
  92. };
  93. char *ring;
  94. int val = TPACKET_V2;
  95. if (setsockopt(fd, SOL_PACKET, PACKET_VERSION, (void *) &val,
  96. sizeof(val))) {
  97. perror("packetsock ring setsockopt version");
  98. exit(1);
  99. }
  100. if (setsockopt(fd, SOL_PACKET, PACKET_RX_RING, (void *) &req,
  101. sizeof(req))) {
  102. perror("packetsock ring setsockopt");
  103. exit(1);
  104. }
  105. ring = mmap(0, req.tp_block_size * req.tp_block_nr,
  106. PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  107. if (!ring) {
  108. fprintf(stderr, "packetsock ring mmap\n");
  109. exit(1);
  110. }
  111. return ring;
  112. }
  113. static int sock_fanout_read_ring(int fd, void *ring)
  114. {
  115. struct tpacket2_hdr *header = ring;
  116. int count = 0;
  117. while (header->tp_status & TP_STATUS_USER && count < RING_NUM_FRAMES) {
  118. count++;
  119. header = ring + (count * getpagesize());
  120. }
  121. return count;
  122. }
  123. static int sock_fanout_read(int fds[], char *rings[], const int expect[])
  124. {
  125. int ret[2];
  126. ret[0] = sock_fanout_read_ring(fds[0], rings[0]);
  127. ret[1] = sock_fanout_read_ring(fds[1], rings[1]);
  128. fprintf(stderr, "info: count=%d,%d, expect=%d,%d\n",
  129. ret[0], ret[1], expect[0], expect[1]);
  130. if ((!(ret[0] == expect[0] && ret[1] == expect[1])) &&
  131. (!(ret[0] == expect[1] && ret[1] == expect[0]))) {
  132. fprintf(stderr, "ERROR: incorrect queue lengths\n");
  133. return 1;
  134. }
  135. return 0;
  136. }
  137. /* Test illegal mode + flag combination */
  138. static void test_control_single(void)
  139. {
  140. fprintf(stderr, "test: control single socket\n");
  141. if (sock_fanout_open(PACKET_FANOUT_ROLLOVER |
  142. PACKET_FANOUT_FLAG_ROLLOVER, 0) != -1) {
  143. fprintf(stderr, "ERROR: opened socket with dual rollover\n");
  144. exit(1);
  145. }
  146. }
  147. /* Test illegal group with different modes or flags */
  148. static void test_control_group(void)
  149. {
  150. int fds[2];
  151. fprintf(stderr, "test: control multiple sockets\n");
  152. fds[0] = sock_fanout_open(PACKET_FANOUT_HASH, 20);
  153. if (fds[0] == -1) {
  154. fprintf(stderr, "ERROR: failed to open HASH socket\n");
  155. exit(1);
  156. }
  157. if (sock_fanout_open(PACKET_FANOUT_HASH |
  158. PACKET_FANOUT_FLAG_DEFRAG, 10) != -1) {
  159. fprintf(stderr, "ERROR: joined group with wrong flag defrag\n");
  160. exit(1);
  161. }
  162. if (sock_fanout_open(PACKET_FANOUT_HASH |
  163. PACKET_FANOUT_FLAG_ROLLOVER, 10) != -1) {
  164. fprintf(stderr, "ERROR: joined group with wrong flag ro\n");
  165. exit(1);
  166. }
  167. if (sock_fanout_open(PACKET_FANOUT_CPU, 10) != -1) {
  168. fprintf(stderr, "ERROR: joined group with wrong mode\n");
  169. exit(1);
  170. }
  171. fds[1] = sock_fanout_open(PACKET_FANOUT_HASH, 20);
  172. if (fds[1] == -1) {
  173. fprintf(stderr, "ERROR: failed to join group\n");
  174. exit(1);
  175. }
  176. if (close(fds[1]) || close(fds[0])) {
  177. fprintf(stderr, "ERROR: closing sockets\n");
  178. exit(1);
  179. }
  180. }
  181. static int test_datapath(uint16_t typeflags, int port_off,
  182. const int expect1[], const int expect2[])
  183. {
  184. const int expect0[] = { 0, 0 };
  185. char *rings[2];
  186. int fds[2], fds_udp[2][2], ret;
  187. fprintf(stderr, "test: datapath 0x%hx\n", typeflags);
  188. fds[0] = sock_fanout_open(typeflags, 20);
  189. fds[1] = sock_fanout_open(typeflags, 20);
  190. if (fds[0] == -1 || fds[1] == -1) {
  191. fprintf(stderr, "ERROR: failed open\n");
  192. exit(1);
  193. }
  194. rings[0] = sock_fanout_open_ring(fds[0]);
  195. rings[1] = sock_fanout_open_ring(fds[1]);
  196. pair_udp_open(fds_udp[0], PORT_BASE);
  197. pair_udp_open(fds_udp[1], PORT_BASE + port_off);
  198. sock_fanout_read(fds, rings, expect0);
  199. /* Send data, but not enough to overflow a queue */
  200. pair_udp_send(fds_udp[0], 15);
  201. pair_udp_send(fds_udp[1], 5);
  202. ret = sock_fanout_read(fds, rings, expect1);
  203. /* Send more data, overflow the queue */
  204. pair_udp_send(fds_udp[0], 15);
  205. /* TODO: ensure consistent order between expect1 and expect2 */
  206. ret |= sock_fanout_read(fds, rings, expect2);
  207. if (munmap(rings[1], RING_NUM_FRAMES * getpagesize()) ||
  208. munmap(rings[0], RING_NUM_FRAMES * getpagesize())) {
  209. fprintf(stderr, "close rings\n");
  210. exit(1);
  211. }
  212. if (close(fds_udp[1][1]) || close(fds_udp[1][0]) ||
  213. close(fds_udp[0][1]) || close(fds_udp[0][0]) ||
  214. close(fds[1]) || close(fds[0])) {
  215. fprintf(stderr, "close datapath\n");
  216. exit(1);
  217. }
  218. return ret;
  219. }
  220. static int set_cpuaffinity(int cpuid)
  221. {
  222. cpu_set_t mask;
  223. CPU_ZERO(&mask);
  224. CPU_SET(cpuid, &mask);
  225. if (sched_setaffinity(0, sizeof(mask), &mask)) {
  226. if (errno != EINVAL) {
  227. fprintf(stderr, "setaffinity %d\n", cpuid);
  228. exit(1);
  229. }
  230. return 1;
  231. }
  232. return 0;
  233. }
  234. int main(int argc, char **argv)
  235. {
  236. const int expect_hash[2][2] = { { 15, 5 }, { 20, 5 } };
  237. const int expect_hash_rb[2][2] = { { 15, 5 }, { 20, 15 } };
  238. const int expect_lb[2][2] = { { 10, 10 }, { 18, 17 } };
  239. const int expect_rb[2][2] = { { 20, 0 }, { 20, 15 } };
  240. const int expect_cpu0[2][2] = { { 20, 0 }, { 20, 0 } };
  241. const int expect_cpu1[2][2] = { { 0, 20 }, { 0, 20 } };
  242. int port_off = 2, tries = 5, ret;
  243. test_control_single();
  244. test_control_group();
  245. /* find a set of ports that do not collide onto the same socket */
  246. ret = test_datapath(PACKET_FANOUT_HASH, port_off,
  247. expect_hash[0], expect_hash[1]);
  248. while (ret && tries--) {
  249. fprintf(stderr, "info: trying alternate ports (%d)\n", tries);
  250. ret = test_datapath(PACKET_FANOUT_HASH, ++port_off,
  251. expect_hash[0], expect_hash[1]);
  252. }
  253. ret |= test_datapath(PACKET_FANOUT_HASH | PACKET_FANOUT_FLAG_ROLLOVER,
  254. port_off, expect_hash_rb[0], expect_hash_rb[1]);
  255. ret |= test_datapath(PACKET_FANOUT_LB,
  256. port_off, expect_lb[0], expect_lb[1]);
  257. ret |= test_datapath(PACKET_FANOUT_ROLLOVER,
  258. port_off, expect_rb[0], expect_rb[1]);
  259. set_cpuaffinity(0);
  260. ret |= test_datapath(PACKET_FANOUT_CPU, port_off,
  261. expect_cpu0[0], expect_cpu0[1]);
  262. if (!set_cpuaffinity(1))
  263. /* TODO: test that choice alternates with previous */
  264. ret |= test_datapath(PACKET_FANOUT_CPU, port_off,
  265. expect_cpu1[0], expect_cpu1[1]);
  266. if (ret)
  267. return 1;
  268. printf("OK. All tests passed\n");
  269. return 0;
  270. }