psock_fanout.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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_ROLLOVER
  20. *
  21. * Todo:
  22. * - datapath: PACKET_FANOUT_LB
  23. * - datapath: PACKET_FANOUT_CPU
  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. #include <arpa/inet.h>
  42. #include <errno.h>
  43. #include <linux/filter.h>
  44. #include <linux/if_packet.h>
  45. #include <net/ethernet.h>
  46. #include <netinet/ip.h>
  47. #include <netinet/udp.h>
  48. #include <fcntl.h>
  49. #include <stdint.h>
  50. #include <stdio.h>
  51. #include <stdlib.h>
  52. #include <string.h>
  53. #include <sys/socket.h>
  54. #include <sys/stat.h>
  55. #include <sys/types.h>
  56. #include <unistd.h>
  57. /* Hack: build even if local includes are old */
  58. #ifndef PACKET_FANOUT
  59. #define PACKET_FANOUT 18
  60. #define PACKET_FANOUT_HASH 0
  61. #define PACKET_FANOUT_LB 1
  62. #define PACKET_FANOUT_CPU 2
  63. #define PACKET_FANOUT_FLAG_DEFRAG 0x8000
  64. #ifndef PACKET_FANOUT_ROLLOVER
  65. #define PACKET_FANOUT_ROLLOVER 3
  66. #endif
  67. #ifndef PACKET_FANOUT_FLAG_ROLLOVER
  68. #define PACKET_FANOUT_FLAG_ROLLOVER 0x1000
  69. #endif
  70. #endif
  71. #define DATA_LEN 100
  72. #define DATA_CHAR 'a'
  73. static void pair_udp_open(int fds[], uint16_t port)
  74. {
  75. struct sockaddr_in saddr, daddr;
  76. fds[0] = socket(PF_INET, SOCK_DGRAM, 0);
  77. fds[1] = socket(PF_INET, SOCK_DGRAM, 0);
  78. if (fds[0] == -1 || fds[1] == -1) {
  79. fprintf(stderr, "ERROR: socket dgram\n");
  80. exit(1);
  81. }
  82. memset(&saddr, 0, sizeof(saddr));
  83. saddr.sin_family = AF_INET;
  84. saddr.sin_port = htons(port);
  85. saddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  86. memset(&daddr, 0, sizeof(daddr));
  87. daddr.sin_family = AF_INET;
  88. daddr.sin_port = htons(port + 1);
  89. daddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  90. /* must bind both to get consistent hash result */
  91. if (bind(fds[1], (void *) &daddr, sizeof(daddr))) {
  92. perror("bind");
  93. exit(1);
  94. }
  95. if (bind(fds[0], (void *) &saddr, sizeof(saddr))) {
  96. perror("bind");
  97. exit(1);
  98. }
  99. if (connect(fds[0], (void *) &daddr, sizeof(daddr))) {
  100. perror("bind");
  101. exit(1);
  102. }
  103. }
  104. static void pair_udp_send(int fds[], int num)
  105. {
  106. char buf[DATA_LEN], rbuf[DATA_LEN];
  107. memset(buf, DATA_CHAR, sizeof(buf));
  108. while (num--) {
  109. /* Should really handle EINTR and EAGAIN */
  110. if (write(fds[0], buf, sizeof(buf)) != sizeof(buf)) {
  111. fprintf(stderr, "ERROR: send failed left=%d\n", num);
  112. exit(1);
  113. }
  114. if (read(fds[1], rbuf, sizeof(rbuf)) != sizeof(rbuf)) {
  115. fprintf(stderr, "ERROR: recv failed left=%d\n", num);
  116. exit(1);
  117. }
  118. if (memcmp(buf, rbuf, sizeof(buf))) {
  119. fprintf(stderr, "ERROR: data failed left=%d\n", num);
  120. exit(1);
  121. }
  122. }
  123. }
  124. static void sock_fanout_setfilter(int fd)
  125. {
  126. struct sock_filter bpf_filter[] = {
  127. { 0x80, 0, 0, 0x00000000 }, /* LD pktlen */
  128. { 0x35, 0, 5, DATA_LEN }, /* JGE DATA_LEN [f goto nomatch]*/
  129. { 0x30, 0, 0, 0x00000050 }, /* LD ip[80] */
  130. { 0x15, 0, 3, DATA_CHAR }, /* JEQ DATA_CHAR [f goto nomatch]*/
  131. { 0x30, 0, 0, 0x00000051 }, /* LD ip[81] */
  132. { 0x15, 0, 1, DATA_CHAR }, /* JEQ DATA_CHAR [f goto nomatch]*/
  133. { 0x6, 0, 0, 0x00000060 }, /* RET match */
  134. /* nomatch */ { 0x6, 0, 0, 0x00000000 }, /* RET no match */
  135. };
  136. struct sock_fprog bpf_prog;
  137. bpf_prog.filter = bpf_filter;
  138. bpf_prog.len = sizeof(bpf_filter) / sizeof(struct sock_filter);
  139. if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &bpf_prog,
  140. sizeof(bpf_prog))) {
  141. perror("setsockopt SO_ATTACH_FILTER");
  142. exit(1);
  143. }
  144. }
  145. /* Open a socket in a given fanout mode.
  146. * @return -1 if mode is bad, a valid socket otherwise */
  147. static int sock_fanout_open(uint16_t typeflags, int num_packets)
  148. {
  149. int fd, val;
  150. fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
  151. if (fd < 0) {
  152. perror("socket packet");
  153. exit(1);
  154. }
  155. /* fanout group ID is always 0: tests whether old groups are deleted */
  156. val = ((int) typeflags) << 16;
  157. if (setsockopt(fd, SOL_PACKET, PACKET_FANOUT, &val, sizeof(val))) {
  158. if (close(fd)) {
  159. perror("close packet");
  160. exit(1);
  161. }
  162. return -1;
  163. }
  164. val = sizeof(struct iphdr) + sizeof(struct udphdr) + DATA_LEN;
  165. val *= num_packets;
  166. /* hack: apparently, the above calculation is too small (TODO: fix) */
  167. val *= 3;
  168. if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &val, sizeof(val))) {
  169. perror("setsockopt SO_RCVBUF");
  170. exit(1);
  171. }
  172. sock_fanout_setfilter(fd);
  173. return fd;
  174. }
  175. static void sock_fanout_read(int fds[], const int expect[])
  176. {
  177. struct tpacket_stats stats;
  178. socklen_t ssize;
  179. int ret[2];
  180. ssize = sizeof(stats);
  181. if (getsockopt(fds[0], SOL_PACKET, PACKET_STATISTICS, &stats, &ssize)) {
  182. perror("getsockopt statistics 0");
  183. exit(1);
  184. }
  185. ret[0] = stats.tp_packets - stats.tp_drops;
  186. ssize = sizeof(stats);
  187. if (getsockopt(fds[1], SOL_PACKET, PACKET_STATISTICS, &stats, &ssize)) {
  188. perror("getsockopt statistics 1");
  189. exit(1);
  190. }
  191. ret[1] = stats.tp_packets - stats.tp_drops;
  192. fprintf(stderr, "info: count=%d,%d, expect=%d,%d\n",
  193. ret[0], ret[1], expect[0], expect[1]);
  194. if ((!(ret[0] == expect[0] && ret[1] == expect[1])) &&
  195. (!(ret[0] == expect[1] && ret[1] == expect[0]))) {
  196. fprintf(stderr, "ERROR: incorrect queue lengths\n");
  197. exit(1);
  198. }
  199. }
  200. /* Test illegal mode + flag combination */
  201. static void test_control_single(void)
  202. {
  203. fprintf(stderr, "test: control single socket\n");
  204. if (sock_fanout_open(PACKET_FANOUT_ROLLOVER |
  205. PACKET_FANOUT_FLAG_ROLLOVER, 0) != -1) {
  206. fprintf(stderr, "ERROR: opened socket with dual rollover\n");
  207. exit(1);
  208. }
  209. }
  210. /* Test illegal group with different modes or flags */
  211. static void test_control_group(void)
  212. {
  213. int fds[2];
  214. fprintf(stderr, "test: control multiple sockets\n");
  215. fds[0] = sock_fanout_open(PACKET_FANOUT_HASH, 20);
  216. if (fds[0] == -1) {
  217. fprintf(stderr, "ERROR: failed to open HASH socket\n");
  218. exit(1);
  219. }
  220. if (sock_fanout_open(PACKET_FANOUT_HASH |
  221. PACKET_FANOUT_FLAG_DEFRAG, 10) != -1) {
  222. fprintf(stderr, "ERROR: joined group with wrong flag defrag\n");
  223. exit(1);
  224. }
  225. if (sock_fanout_open(PACKET_FANOUT_HASH |
  226. PACKET_FANOUT_FLAG_ROLLOVER, 10) != -1) {
  227. fprintf(stderr, "ERROR: joined group with wrong flag ro\n");
  228. exit(1);
  229. }
  230. if (sock_fanout_open(PACKET_FANOUT_CPU, 10) != -1) {
  231. fprintf(stderr, "ERROR: joined group with wrong mode\n");
  232. exit(1);
  233. }
  234. fds[1] = sock_fanout_open(PACKET_FANOUT_HASH, 20);
  235. if (fds[1] == -1) {
  236. fprintf(stderr, "ERROR: failed to join group\n");
  237. exit(1);
  238. }
  239. if (close(fds[1]) || close(fds[0])) {
  240. fprintf(stderr, "ERROR: closing sockets\n");
  241. exit(1);
  242. }
  243. }
  244. static void test_datapath(uint16_t typeflags,
  245. const int expect1[], const int expect2[])
  246. {
  247. const int expect0[] = { 0, 0 };
  248. int fds[2], fds_udp[2][2];
  249. fprintf(stderr, "test: datapath 0x%hx\n", typeflags);
  250. fds[0] = sock_fanout_open(typeflags, 20);
  251. fds[1] = sock_fanout_open(typeflags, 20);
  252. if (fds[0] == -1 || fds[1] == -1) {
  253. fprintf(stderr, "ERROR: failed open\n");
  254. exit(1);
  255. }
  256. pair_udp_open(fds_udp[0], 8000);
  257. pair_udp_open(fds_udp[1], 8002);
  258. sock_fanout_read(fds, expect0);
  259. /* Send data, but not enough to overflow a queue */
  260. pair_udp_send(fds_udp[0], 15);
  261. pair_udp_send(fds_udp[1], 5);
  262. sock_fanout_read(fds, expect1);
  263. /* Send more data, overflow the queue */
  264. pair_udp_send(fds_udp[0], 15);
  265. /* TODO: ensure consistent order between expect1 and expect2 */
  266. sock_fanout_read(fds, expect2);
  267. if (close(fds_udp[1][1]) || close(fds_udp[1][0]) ||
  268. close(fds_udp[0][1]) || close(fds_udp[0][0]) ||
  269. close(fds[1]) || close(fds[0])) {
  270. fprintf(stderr, "close datapath\n");
  271. exit(1);
  272. }
  273. }
  274. int main(int argc, char **argv)
  275. {
  276. const int expect_hash[2][2] = { { 15, 5 }, { 5, 0 } };
  277. const int expect_hash_rb[2][2] = { { 15, 5 }, { 5, 10 } };
  278. const int expect_rb[2][2] = { { 20, 0 }, { 0, 15 } };
  279. test_control_single();
  280. test_control_group();
  281. test_datapath(PACKET_FANOUT_HASH, expect_hash[0], expect_hash[1]);
  282. test_datapath(PACKET_FANOUT_HASH | PACKET_FANOUT_FLAG_ROLLOVER,
  283. expect_hash_rb[0], expect_hash_rb[1]);
  284. test_datapath(PACKET_FANOUT_ROLLOVER, expect_rb[0], expect_rb[1]);
  285. printf("OK. All tests passed\n");
  286. return 0;
  287. }