psock_fanout.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. #define DATA_LEN 100
  62. #define DATA_CHAR 'a'
  63. #define RING_NUM_FRAMES 20
  64. #define PORT_BASE 8000
  65. static void pair_udp_open(int fds[], uint16_t port)
  66. {
  67. struct sockaddr_in saddr, daddr;
  68. fds[0] = socket(PF_INET, SOCK_DGRAM, 0);
  69. fds[1] = socket(PF_INET, SOCK_DGRAM, 0);
  70. if (fds[0] == -1 || fds[1] == -1) {
  71. fprintf(stderr, "ERROR: socket dgram\n");
  72. exit(1);
  73. }
  74. memset(&saddr, 0, sizeof(saddr));
  75. saddr.sin_family = AF_INET;
  76. saddr.sin_port = htons(port);
  77. saddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  78. memset(&daddr, 0, sizeof(daddr));
  79. daddr.sin_family = AF_INET;
  80. daddr.sin_port = htons(port + 1);
  81. daddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  82. /* must bind both to get consistent hash result */
  83. if (bind(fds[1], (void *) &daddr, sizeof(daddr))) {
  84. perror("bind");
  85. exit(1);
  86. }
  87. if (bind(fds[0], (void *) &saddr, sizeof(saddr))) {
  88. perror("bind");
  89. exit(1);
  90. }
  91. if (connect(fds[0], (void *) &daddr, sizeof(daddr))) {
  92. perror("bind");
  93. exit(1);
  94. }
  95. }
  96. static void pair_udp_send(int fds[], int num)
  97. {
  98. char buf[DATA_LEN], rbuf[DATA_LEN];
  99. memset(buf, DATA_CHAR, sizeof(buf));
  100. while (num--) {
  101. /* Should really handle EINTR and EAGAIN */
  102. if (write(fds[0], buf, sizeof(buf)) != sizeof(buf)) {
  103. fprintf(stderr, "ERROR: send failed left=%d\n", num);
  104. exit(1);
  105. }
  106. if (read(fds[1], rbuf, sizeof(rbuf)) != sizeof(rbuf)) {
  107. fprintf(stderr, "ERROR: recv failed left=%d\n", num);
  108. exit(1);
  109. }
  110. if (memcmp(buf, rbuf, sizeof(buf))) {
  111. fprintf(stderr, "ERROR: data failed left=%d\n", num);
  112. exit(1);
  113. }
  114. }
  115. }
  116. static void sock_fanout_setfilter(int fd)
  117. {
  118. struct sock_filter bpf_filter[] = {
  119. { 0x80, 0, 0, 0x00000000 }, /* LD pktlen */
  120. { 0x35, 0, 5, DATA_LEN }, /* JGE DATA_LEN [f goto nomatch]*/
  121. { 0x30, 0, 0, 0x00000050 }, /* LD ip[80] */
  122. { 0x15, 0, 3, DATA_CHAR }, /* JEQ DATA_CHAR [f goto nomatch]*/
  123. { 0x30, 0, 0, 0x00000051 }, /* LD ip[81] */
  124. { 0x15, 0, 1, DATA_CHAR }, /* JEQ DATA_CHAR [f goto nomatch]*/
  125. { 0x6, 0, 0, 0x00000060 }, /* RET match */
  126. /* nomatch */ { 0x6, 0, 0, 0x00000000 }, /* RET no match */
  127. };
  128. struct sock_fprog bpf_prog;
  129. bpf_prog.filter = bpf_filter;
  130. bpf_prog.len = sizeof(bpf_filter) / sizeof(struct sock_filter);
  131. if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &bpf_prog,
  132. sizeof(bpf_prog))) {
  133. perror("setsockopt SO_ATTACH_FILTER");
  134. exit(1);
  135. }
  136. }
  137. /* Open a socket in a given fanout mode.
  138. * @return -1 if mode is bad, a valid socket otherwise */
  139. static int sock_fanout_open(uint16_t typeflags, int num_packets)
  140. {
  141. int fd, val;
  142. fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
  143. if (fd < 0) {
  144. perror("socket packet");
  145. exit(1);
  146. }
  147. /* fanout group ID is always 0: tests whether old groups are deleted */
  148. val = ((int) typeflags) << 16;
  149. if (setsockopt(fd, SOL_PACKET, PACKET_FANOUT, &val, sizeof(val))) {
  150. if (close(fd)) {
  151. perror("close packet");
  152. exit(1);
  153. }
  154. return -1;
  155. }
  156. sock_fanout_setfilter(fd);
  157. return fd;
  158. }
  159. static char *sock_fanout_open_ring(int fd)
  160. {
  161. struct tpacket_req req = {
  162. .tp_block_size = getpagesize(),
  163. .tp_frame_size = getpagesize(),
  164. .tp_block_nr = RING_NUM_FRAMES,
  165. .tp_frame_nr = RING_NUM_FRAMES,
  166. };
  167. char *ring;
  168. if (setsockopt(fd, SOL_PACKET, PACKET_RX_RING, (void *) &req,
  169. sizeof(req))) {
  170. perror("packetsock ring setsockopt");
  171. exit(1);
  172. }
  173. ring = mmap(0, req.tp_block_size * req.tp_block_nr,
  174. PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  175. if (!ring) {
  176. fprintf(stderr, "packetsock ring mmap\n");
  177. exit(1);
  178. }
  179. return ring;
  180. }
  181. static int sock_fanout_read_ring(int fd, void *ring)
  182. {
  183. struct tpacket_hdr *header = ring;
  184. int count = 0;
  185. while (header->tp_status & TP_STATUS_USER && count < RING_NUM_FRAMES) {
  186. count++;
  187. header = ring + (count * getpagesize());
  188. }
  189. return count;
  190. }
  191. static int sock_fanout_read(int fds[], char *rings[], const int expect[])
  192. {
  193. int ret[2];
  194. ret[0] = sock_fanout_read_ring(fds[0], rings[0]);
  195. ret[1] = sock_fanout_read_ring(fds[1], rings[1]);
  196. fprintf(stderr, "info: count=%d,%d, expect=%d,%d\n",
  197. ret[0], ret[1], expect[0], expect[1]);
  198. if ((!(ret[0] == expect[0] && ret[1] == expect[1])) &&
  199. (!(ret[0] == expect[1] && ret[1] == expect[0]))) {
  200. fprintf(stderr, "ERROR: incorrect queue lengths\n");
  201. return 1;
  202. }
  203. return 0;
  204. }
  205. /* Test illegal mode + flag combination */
  206. static void test_control_single(void)
  207. {
  208. fprintf(stderr, "test: control single socket\n");
  209. if (sock_fanout_open(PACKET_FANOUT_ROLLOVER |
  210. PACKET_FANOUT_FLAG_ROLLOVER, 0) != -1) {
  211. fprintf(stderr, "ERROR: opened socket with dual rollover\n");
  212. exit(1);
  213. }
  214. }
  215. /* Test illegal group with different modes or flags */
  216. static void test_control_group(void)
  217. {
  218. int fds[2];
  219. fprintf(stderr, "test: control multiple sockets\n");
  220. fds[0] = sock_fanout_open(PACKET_FANOUT_HASH, 20);
  221. if (fds[0] == -1) {
  222. fprintf(stderr, "ERROR: failed to open HASH socket\n");
  223. exit(1);
  224. }
  225. if (sock_fanout_open(PACKET_FANOUT_HASH |
  226. PACKET_FANOUT_FLAG_DEFRAG, 10) != -1) {
  227. fprintf(stderr, "ERROR: joined group with wrong flag defrag\n");
  228. exit(1);
  229. }
  230. if (sock_fanout_open(PACKET_FANOUT_HASH |
  231. PACKET_FANOUT_FLAG_ROLLOVER, 10) != -1) {
  232. fprintf(stderr, "ERROR: joined group with wrong flag ro\n");
  233. exit(1);
  234. }
  235. if (sock_fanout_open(PACKET_FANOUT_CPU, 10) != -1) {
  236. fprintf(stderr, "ERROR: joined group with wrong mode\n");
  237. exit(1);
  238. }
  239. fds[1] = sock_fanout_open(PACKET_FANOUT_HASH, 20);
  240. if (fds[1] == -1) {
  241. fprintf(stderr, "ERROR: failed to join group\n");
  242. exit(1);
  243. }
  244. if (close(fds[1]) || close(fds[0])) {
  245. fprintf(stderr, "ERROR: closing sockets\n");
  246. exit(1);
  247. }
  248. }
  249. static int test_datapath(uint16_t typeflags, int port_off,
  250. const int expect1[], const int expect2[])
  251. {
  252. const int expect0[] = { 0, 0 };
  253. char *rings[2];
  254. int fds[2], fds_udp[2][2], ret;
  255. fprintf(stderr, "test: datapath 0x%hx\n", typeflags);
  256. fds[0] = sock_fanout_open(typeflags, 20);
  257. fds[1] = sock_fanout_open(typeflags, 20);
  258. if (fds[0] == -1 || fds[1] == -1) {
  259. fprintf(stderr, "ERROR: failed open\n");
  260. exit(1);
  261. }
  262. rings[0] = sock_fanout_open_ring(fds[0]);
  263. rings[1] = sock_fanout_open_ring(fds[1]);
  264. pair_udp_open(fds_udp[0], PORT_BASE);
  265. pair_udp_open(fds_udp[1], PORT_BASE + port_off);
  266. sock_fanout_read(fds, rings, expect0);
  267. /* Send data, but not enough to overflow a queue */
  268. pair_udp_send(fds_udp[0], 15);
  269. pair_udp_send(fds_udp[1], 5);
  270. ret = sock_fanout_read(fds, rings, expect1);
  271. /* Send more data, overflow the queue */
  272. pair_udp_send(fds_udp[0], 15);
  273. /* TODO: ensure consistent order between expect1 and expect2 */
  274. ret |= sock_fanout_read(fds, rings, expect2);
  275. if (munmap(rings[1], RING_NUM_FRAMES * getpagesize()) ||
  276. munmap(rings[0], RING_NUM_FRAMES * getpagesize())) {
  277. fprintf(stderr, "close rings\n");
  278. exit(1);
  279. }
  280. if (close(fds_udp[1][1]) || close(fds_udp[1][0]) ||
  281. close(fds_udp[0][1]) || close(fds_udp[0][0]) ||
  282. close(fds[1]) || close(fds[0])) {
  283. fprintf(stderr, "close datapath\n");
  284. exit(1);
  285. }
  286. return ret;
  287. }
  288. static int set_cpuaffinity(int cpuid)
  289. {
  290. cpu_set_t mask;
  291. CPU_ZERO(&mask);
  292. CPU_SET(cpuid, &mask);
  293. if (sched_setaffinity(0, sizeof(mask), &mask)) {
  294. if (errno != EINVAL) {
  295. fprintf(stderr, "setaffinity %d\n", cpuid);
  296. exit(1);
  297. }
  298. return 1;
  299. }
  300. return 0;
  301. }
  302. int main(int argc, char **argv)
  303. {
  304. const int expect_hash[2][2] = { { 15, 5 }, { 20, 5 } };
  305. const int expect_hash_rb[2][2] = { { 15, 5 }, { 20, 15 } };
  306. const int expect_lb[2][2] = { { 10, 10 }, { 18, 17 } };
  307. const int expect_rb[2][2] = { { 20, 0 }, { 20, 15 } };
  308. const int expect_cpu0[2][2] = { { 20, 0 }, { 20, 0 } };
  309. const int expect_cpu1[2][2] = { { 0, 20 }, { 0, 20 } };
  310. int port_off = 2, tries = 5, ret;
  311. test_control_single();
  312. test_control_group();
  313. /* find a set of ports that do not collide onto the same socket */
  314. ret = test_datapath(PACKET_FANOUT_HASH, port_off,
  315. expect_hash[0], expect_hash[1]);
  316. while (ret && tries--) {
  317. fprintf(stderr, "info: trying alternate ports (%d)\n", tries);
  318. ret = test_datapath(PACKET_FANOUT_HASH, ++port_off,
  319. expect_hash[0], expect_hash[1]);
  320. }
  321. ret |= test_datapath(PACKET_FANOUT_HASH | PACKET_FANOUT_FLAG_ROLLOVER,
  322. port_off, expect_hash_rb[0], expect_hash_rb[1]);
  323. ret |= test_datapath(PACKET_FANOUT_LB,
  324. port_off, expect_lb[0], expect_lb[1]);
  325. ret |= test_datapath(PACKET_FANOUT_ROLLOVER,
  326. port_off, expect_rb[0], expect_rb[1]);
  327. set_cpuaffinity(0);
  328. ret |= test_datapath(PACKET_FANOUT_CPU, port_off,
  329. expect_cpu0[0], expect_cpu0[1]);
  330. if (!set_cpuaffinity(1))
  331. /* TODO: test that choice alternates with previous */
  332. ret |= test_datapath(PACKET_FANOUT_CPU, port_off,
  333. expect_cpu1[0], expect_cpu1[1]);
  334. if (ret)
  335. return 1;
  336. printf("OK. All tests passed\n");
  337. return 0;
  338. }