psock_tpacket.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  1. /*
  2. * Copyright 2013 Red Hat, Inc.
  3. * Author: Daniel Borkmann <dborkman@redhat.com>
  4. *
  5. * A basic test of packet socket's TPACKET_V1/TPACKET_V2/TPACKET_V3 behavior.
  6. *
  7. * Control:
  8. * Test the setup of the TPACKET socket with different patterns that are
  9. * known to fail (TODO) resp. succeed (OK).
  10. *
  11. * Datapath:
  12. * Open a pair of packet sockets and send resp. receive an a priori known
  13. * packet pattern accross the sockets and check if it was received resp.
  14. * sent correctly. Fanout in combination with RX_RING is currently not
  15. * tested here.
  16. *
  17. * The test currently runs for
  18. * - TPACKET_V1: RX_RING, TX_RING
  19. * - TPACKET_V2: RX_RING, TX_RING
  20. * - TPACKET_V3: RX_RING
  21. *
  22. * License (GPLv2):
  23. *
  24. * This program is free software; you can redistribute it and/or modify it
  25. * under the terms and conditions of the GNU General Public License,
  26. * version 2, as published by the Free Software Foundation.
  27. *
  28. * This program is distributed in the hope it will be useful, but WITHOUT
  29. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  30. * FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General Public License for
  31. * more details.
  32. *
  33. * You should have received a copy of the GNU General Public License along with
  34. * this program; if not, write to the Free Software Foundation, Inc.,
  35. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  36. */
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <sys/types.h>
  40. #include <sys/stat.h>
  41. #include <sys/socket.h>
  42. #include <sys/mman.h>
  43. #include <linux/if_packet.h>
  44. #include <linux/filter.h>
  45. #include <ctype.h>
  46. #include <fcntl.h>
  47. #include <unistd.h>
  48. #include <bits/wordsize.h>
  49. #include <net/ethernet.h>
  50. #include <netinet/ip.h>
  51. #include <arpa/inet.h>
  52. #include <stdint.h>
  53. #include <string.h>
  54. #include <assert.h>
  55. #include <net/if.h>
  56. #include <inttypes.h>
  57. #include <poll.h>
  58. #include "psock_lib.h"
  59. #ifndef bug_on
  60. # define bug_on(cond) assert(!(cond))
  61. #endif
  62. #ifndef __aligned_tpacket
  63. # define __aligned_tpacket __attribute__((aligned(TPACKET_ALIGNMENT)))
  64. #endif
  65. #ifndef __align_tpacket
  66. # define __align_tpacket(x) __attribute__((aligned(TPACKET_ALIGN(x))))
  67. #endif
  68. #define BLOCK_STATUS(x) ((x)->h1.block_status)
  69. #define BLOCK_NUM_PKTS(x) ((x)->h1.num_pkts)
  70. #define BLOCK_O2FP(x) ((x)->h1.offset_to_first_pkt)
  71. #define BLOCK_LEN(x) ((x)->h1.blk_len)
  72. #define BLOCK_SNUM(x) ((x)->h1.seq_num)
  73. #define BLOCK_O2PRIV(x) ((x)->offset_to_priv)
  74. #define BLOCK_PRIV(x) ((void *) ((uint8_t *) (x) + BLOCK_O2PRIV(x)))
  75. #define BLOCK_HDR_LEN (ALIGN_8(sizeof(struct block_desc)))
  76. #define ALIGN_8(x) (((x) + 8 - 1) & ~(8 - 1))
  77. #define BLOCK_PLUS_PRIV(sz_pri) (BLOCK_HDR_LEN + ALIGN_8((sz_pri)))
  78. #define NUM_PACKETS 100
  79. struct ring {
  80. struct iovec *rd;
  81. uint8_t *mm_space;
  82. size_t mm_len, rd_len;
  83. struct sockaddr_ll ll;
  84. void (*walk)(int sock, struct ring *ring);
  85. int type, rd_num, flen, version;
  86. union {
  87. struct tpacket_req req;
  88. struct tpacket_req3 req3;
  89. };
  90. };
  91. struct block_desc {
  92. uint32_t version;
  93. uint32_t offset_to_priv;
  94. struct tpacket_hdr_v1 h1;
  95. };
  96. union frame_map {
  97. struct {
  98. struct tpacket_hdr tp_h __aligned_tpacket;
  99. struct sockaddr_ll s_ll __align_tpacket(sizeof(struct tpacket_hdr));
  100. } *v1;
  101. struct {
  102. struct tpacket2_hdr tp_h __aligned_tpacket;
  103. struct sockaddr_ll s_ll __align_tpacket(sizeof(struct tpacket2_hdr));
  104. } *v2;
  105. void *raw;
  106. };
  107. static unsigned int total_packets, total_bytes;
  108. static int pfsocket(int ver)
  109. {
  110. int ret, sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
  111. if (sock == -1) {
  112. perror("socket");
  113. exit(1);
  114. }
  115. ret = setsockopt(sock, SOL_PACKET, PACKET_VERSION, &ver, sizeof(ver));
  116. if (ret == -1) {
  117. perror("setsockopt");
  118. exit(1);
  119. }
  120. return sock;
  121. }
  122. static void status_bar_update(void)
  123. {
  124. if (total_packets % 10 == 0) {
  125. fprintf(stderr, ".");
  126. fflush(stderr);
  127. }
  128. }
  129. static void test_payload(void *pay, size_t len)
  130. {
  131. struct ethhdr *eth = pay;
  132. if (len < sizeof(struct ethhdr)) {
  133. fprintf(stderr, "test_payload: packet too "
  134. "small: %zu bytes!\n", len);
  135. exit(1);
  136. }
  137. if (eth->h_proto != htons(ETH_P_IP)) {
  138. fprintf(stderr, "test_payload: wrong ethernet "
  139. "type: 0x%x!\n", ntohs(eth->h_proto));
  140. exit(1);
  141. }
  142. }
  143. static void create_payload(void *pay, size_t *len)
  144. {
  145. int i;
  146. struct ethhdr *eth = pay;
  147. struct iphdr *ip = pay + sizeof(*eth);
  148. /* Lets create some broken crap, that still passes
  149. * our BPF filter.
  150. */
  151. *len = DATA_LEN + 42;
  152. memset(pay, 0xff, ETH_ALEN * 2);
  153. eth->h_proto = htons(ETH_P_IP);
  154. for (i = 0; i < sizeof(*ip); ++i)
  155. ((uint8_t *) pay)[i + sizeof(*eth)] = (uint8_t) rand();
  156. ip->ihl = 5;
  157. ip->version = 4;
  158. ip->protocol = 0x11;
  159. ip->frag_off = 0;
  160. ip->ttl = 64;
  161. ip->tot_len = htons((uint16_t) *len - sizeof(*eth));
  162. ip->saddr = htonl(INADDR_LOOPBACK);
  163. ip->daddr = htonl(INADDR_LOOPBACK);
  164. memset(pay + sizeof(*eth) + sizeof(*ip),
  165. DATA_CHAR, DATA_LEN);
  166. }
  167. static inline int __v1_rx_kernel_ready(struct tpacket_hdr *hdr)
  168. {
  169. return ((hdr->tp_status & TP_STATUS_USER) == TP_STATUS_USER);
  170. }
  171. static inline void __v1_rx_user_ready(struct tpacket_hdr *hdr)
  172. {
  173. hdr->tp_status = TP_STATUS_KERNEL;
  174. __sync_synchronize();
  175. }
  176. static inline int __v2_rx_kernel_ready(struct tpacket2_hdr *hdr)
  177. {
  178. return ((hdr->tp_status & TP_STATUS_USER) == TP_STATUS_USER);
  179. }
  180. static inline void __v2_rx_user_ready(struct tpacket2_hdr *hdr)
  181. {
  182. hdr->tp_status = TP_STATUS_KERNEL;
  183. __sync_synchronize();
  184. }
  185. static inline int __v1_v2_rx_kernel_ready(void *base, int version)
  186. {
  187. switch (version) {
  188. case TPACKET_V1:
  189. return __v1_rx_kernel_ready(base);
  190. case TPACKET_V2:
  191. return __v2_rx_kernel_ready(base);
  192. default:
  193. bug_on(1);
  194. return 0;
  195. }
  196. }
  197. static inline void __v1_v2_rx_user_ready(void *base, int version)
  198. {
  199. switch (version) {
  200. case TPACKET_V1:
  201. __v1_rx_user_ready(base);
  202. break;
  203. case TPACKET_V2:
  204. __v2_rx_user_ready(base);
  205. break;
  206. }
  207. }
  208. static void walk_v1_v2_rx(int sock, struct ring *ring)
  209. {
  210. struct pollfd pfd;
  211. int udp_sock[2];
  212. union frame_map ppd;
  213. unsigned int frame_num = 0;
  214. bug_on(ring->type != PACKET_RX_RING);
  215. pair_udp_open(udp_sock, PORT_BASE);
  216. pair_udp_setfilter(sock);
  217. memset(&pfd, 0, sizeof(pfd));
  218. pfd.fd = sock;
  219. pfd.events = POLLIN | POLLERR;
  220. pfd.revents = 0;
  221. pair_udp_send(udp_sock, NUM_PACKETS);
  222. while (total_packets < NUM_PACKETS * 2) {
  223. while (__v1_v2_rx_kernel_ready(ring->rd[frame_num].iov_base,
  224. ring->version)) {
  225. ppd.raw = ring->rd[frame_num].iov_base;
  226. switch (ring->version) {
  227. case TPACKET_V1:
  228. test_payload((uint8_t *) ppd.raw + ppd.v1->tp_h.tp_mac,
  229. ppd.v1->tp_h.tp_snaplen);
  230. total_bytes += ppd.v1->tp_h.tp_snaplen;
  231. break;
  232. case TPACKET_V2:
  233. test_payload((uint8_t *) ppd.raw + ppd.v2->tp_h.tp_mac,
  234. ppd.v2->tp_h.tp_snaplen);
  235. total_bytes += ppd.v2->tp_h.tp_snaplen;
  236. break;
  237. }
  238. status_bar_update();
  239. total_packets++;
  240. __v1_v2_rx_user_ready(ppd.raw, ring->version);
  241. frame_num = (frame_num + 1) % ring->rd_num;
  242. }
  243. poll(&pfd, 1, 1);
  244. }
  245. pair_udp_close(udp_sock);
  246. if (total_packets != 2 * NUM_PACKETS) {
  247. fprintf(stderr, "walk_v%d_rx: received %u out of %u pkts\n",
  248. ring->version, total_packets, NUM_PACKETS);
  249. exit(1);
  250. }
  251. fprintf(stderr, " %u pkts (%u bytes)", NUM_PACKETS, total_bytes >> 1);
  252. }
  253. static inline int __v1_tx_kernel_ready(struct tpacket_hdr *hdr)
  254. {
  255. return !(hdr->tp_status & (TP_STATUS_SEND_REQUEST | TP_STATUS_SENDING));
  256. }
  257. static inline void __v1_tx_user_ready(struct tpacket_hdr *hdr)
  258. {
  259. hdr->tp_status = TP_STATUS_SEND_REQUEST;
  260. __sync_synchronize();
  261. }
  262. static inline int __v2_tx_kernel_ready(struct tpacket2_hdr *hdr)
  263. {
  264. return !(hdr->tp_status & (TP_STATUS_SEND_REQUEST | TP_STATUS_SENDING));
  265. }
  266. static inline void __v2_tx_user_ready(struct tpacket2_hdr *hdr)
  267. {
  268. hdr->tp_status = TP_STATUS_SEND_REQUEST;
  269. __sync_synchronize();
  270. }
  271. static inline int __v1_v2_tx_kernel_ready(void *base, int version)
  272. {
  273. switch (version) {
  274. case TPACKET_V1:
  275. return __v1_tx_kernel_ready(base);
  276. case TPACKET_V2:
  277. return __v2_tx_kernel_ready(base);
  278. default:
  279. bug_on(1);
  280. return 0;
  281. }
  282. }
  283. static inline void __v1_v2_tx_user_ready(void *base, int version)
  284. {
  285. switch (version) {
  286. case TPACKET_V1:
  287. __v1_tx_user_ready(base);
  288. break;
  289. case TPACKET_V2:
  290. __v2_tx_user_ready(base);
  291. break;
  292. }
  293. }
  294. static void __v1_v2_set_packet_loss_discard(int sock)
  295. {
  296. int ret, discard = 1;
  297. ret = setsockopt(sock, SOL_PACKET, PACKET_LOSS, (void *) &discard,
  298. sizeof(discard));
  299. if (ret == -1) {
  300. perror("setsockopt");
  301. exit(1);
  302. }
  303. }
  304. static void walk_v1_v2_tx(int sock, struct ring *ring)
  305. {
  306. struct pollfd pfd;
  307. int rcv_sock, ret;
  308. size_t packet_len;
  309. union frame_map ppd;
  310. char packet[1024];
  311. unsigned int frame_num = 0, got = 0;
  312. struct sockaddr_ll ll = {
  313. .sll_family = PF_PACKET,
  314. .sll_halen = ETH_ALEN,
  315. };
  316. bug_on(ring->type != PACKET_TX_RING);
  317. bug_on(ring->rd_num < NUM_PACKETS);
  318. rcv_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
  319. if (rcv_sock == -1) {
  320. perror("socket");
  321. exit(1);
  322. }
  323. pair_udp_setfilter(rcv_sock);
  324. ll.sll_ifindex = if_nametoindex("lo");
  325. ret = bind(rcv_sock, (struct sockaddr *) &ll, sizeof(ll));
  326. if (ret == -1) {
  327. perror("bind");
  328. exit(1);
  329. }
  330. memset(&pfd, 0, sizeof(pfd));
  331. pfd.fd = sock;
  332. pfd.events = POLLOUT | POLLERR;
  333. pfd.revents = 0;
  334. total_packets = NUM_PACKETS;
  335. create_payload(packet, &packet_len);
  336. while (total_packets > 0) {
  337. while (__v1_v2_tx_kernel_ready(ring->rd[frame_num].iov_base,
  338. ring->version) &&
  339. total_packets > 0) {
  340. ppd.raw = ring->rd[frame_num].iov_base;
  341. switch (ring->version) {
  342. case TPACKET_V1:
  343. ppd.v1->tp_h.tp_snaplen = packet_len;
  344. ppd.v1->tp_h.tp_len = packet_len;
  345. memcpy((uint8_t *) ppd.raw + TPACKET_HDRLEN -
  346. sizeof(struct sockaddr_ll), packet,
  347. packet_len);
  348. total_bytes += ppd.v1->tp_h.tp_snaplen;
  349. break;
  350. case TPACKET_V2:
  351. ppd.v2->tp_h.tp_snaplen = packet_len;
  352. ppd.v2->tp_h.tp_len = packet_len;
  353. memcpy((uint8_t *) ppd.raw + TPACKET2_HDRLEN -
  354. sizeof(struct sockaddr_ll), packet,
  355. packet_len);
  356. total_bytes += ppd.v2->tp_h.tp_snaplen;
  357. break;
  358. }
  359. status_bar_update();
  360. total_packets--;
  361. __v1_v2_tx_user_ready(ppd.raw, ring->version);
  362. frame_num = (frame_num + 1) % ring->rd_num;
  363. }
  364. poll(&pfd, 1, 1);
  365. }
  366. bug_on(total_packets != 0);
  367. ret = sendto(sock, NULL, 0, 0, NULL, 0);
  368. if (ret == -1) {
  369. perror("sendto");
  370. exit(1);
  371. }
  372. while ((ret = recvfrom(rcv_sock, packet, sizeof(packet),
  373. 0, NULL, NULL)) > 0 &&
  374. total_packets < NUM_PACKETS) {
  375. got += ret;
  376. test_payload(packet, ret);
  377. status_bar_update();
  378. total_packets++;
  379. }
  380. close(rcv_sock);
  381. if (total_packets != NUM_PACKETS) {
  382. fprintf(stderr, "walk_v%d_rx: received %u out of %u pkts\n",
  383. ring->version, total_packets, NUM_PACKETS);
  384. exit(1);
  385. }
  386. fprintf(stderr, " %u pkts (%u bytes)", NUM_PACKETS, got);
  387. }
  388. static void walk_v1_v2(int sock, struct ring *ring)
  389. {
  390. if (ring->type == PACKET_RX_RING)
  391. walk_v1_v2_rx(sock, ring);
  392. else
  393. walk_v1_v2_tx(sock, ring);
  394. }
  395. static uint64_t __v3_prev_block_seq_num = 0;
  396. void __v3_test_block_seq_num(struct block_desc *pbd)
  397. {
  398. if (__v3_prev_block_seq_num + 1 != BLOCK_SNUM(pbd)) {
  399. fprintf(stderr, "\nprev_block_seq_num:%"PRIu64", expected "
  400. "seq:%"PRIu64" != actual seq:%"PRIu64"\n",
  401. __v3_prev_block_seq_num, __v3_prev_block_seq_num + 1,
  402. (uint64_t) BLOCK_SNUM(pbd));
  403. exit(1);
  404. }
  405. __v3_prev_block_seq_num = BLOCK_SNUM(pbd);
  406. }
  407. static void __v3_test_block_len(struct block_desc *pbd, uint32_t bytes, int block_num)
  408. {
  409. if (BLOCK_NUM_PKTS(pbd)) {
  410. if (bytes != BLOCK_LEN(pbd)) {
  411. fprintf(stderr, "\nblock:%u with %upackets, expected "
  412. "len:%u != actual len:%u\n", block_num,
  413. BLOCK_NUM_PKTS(pbd), bytes, BLOCK_LEN(pbd));
  414. exit(1);
  415. }
  416. } else {
  417. if (BLOCK_LEN(pbd) != BLOCK_PLUS_PRIV(13)) {
  418. fprintf(stderr, "\nblock:%u, expected len:%lu != "
  419. "actual len:%u\n", block_num, BLOCK_HDR_LEN,
  420. BLOCK_LEN(pbd));
  421. exit(1);
  422. }
  423. }
  424. }
  425. static void __v3_test_block_header(struct block_desc *pbd, const int block_num)
  426. {
  427. uint32_t block_status = BLOCK_STATUS(pbd);
  428. if ((block_status & TP_STATUS_USER) == 0) {
  429. fprintf(stderr, "\nblock %u: not in TP_STATUS_USER\n", block_num);
  430. exit(1);
  431. }
  432. __v3_test_block_seq_num(pbd);
  433. }
  434. static void __v3_walk_block(struct block_desc *pbd, const int block_num)
  435. {
  436. int num_pkts = BLOCK_NUM_PKTS(pbd), i;
  437. unsigned long bytes = 0;
  438. unsigned long bytes_with_padding = BLOCK_PLUS_PRIV(13);
  439. struct tpacket3_hdr *ppd;
  440. __v3_test_block_header(pbd, block_num);
  441. ppd = (struct tpacket3_hdr *) ((uint8_t *) pbd + BLOCK_O2FP(pbd));
  442. for (i = 0; i < num_pkts; ++i) {
  443. bytes += ppd->tp_snaplen;
  444. if (ppd->tp_next_offset)
  445. bytes_with_padding += ppd->tp_next_offset;
  446. else
  447. bytes_with_padding += ALIGN_8(ppd->tp_snaplen + ppd->tp_mac);
  448. test_payload((uint8_t *) ppd + ppd->tp_mac, ppd->tp_snaplen);
  449. status_bar_update();
  450. total_packets++;
  451. ppd = (struct tpacket3_hdr *) ((uint8_t *) ppd + ppd->tp_next_offset);
  452. __sync_synchronize();
  453. }
  454. __v3_test_block_len(pbd, bytes_with_padding, block_num);
  455. total_bytes += bytes;
  456. }
  457. void __v3_flush_block(struct block_desc *pbd)
  458. {
  459. BLOCK_STATUS(pbd) = TP_STATUS_KERNEL;
  460. __sync_synchronize();
  461. }
  462. static void walk_v3_rx(int sock, struct ring *ring)
  463. {
  464. unsigned int block_num = 0;
  465. struct pollfd pfd;
  466. struct block_desc *pbd;
  467. int udp_sock[2];
  468. bug_on(ring->type != PACKET_RX_RING);
  469. pair_udp_open(udp_sock, PORT_BASE);
  470. pair_udp_setfilter(sock);
  471. memset(&pfd, 0, sizeof(pfd));
  472. pfd.fd = sock;
  473. pfd.events = POLLIN | POLLERR;
  474. pfd.revents = 0;
  475. pair_udp_send(udp_sock, NUM_PACKETS);
  476. while (total_packets < NUM_PACKETS * 2) {
  477. pbd = (struct block_desc *) ring->rd[block_num].iov_base;
  478. while ((BLOCK_STATUS(pbd) & TP_STATUS_USER) == 0)
  479. poll(&pfd, 1, 1);
  480. __v3_walk_block(pbd, block_num);
  481. __v3_flush_block(pbd);
  482. block_num = (block_num + 1) % ring->rd_num;
  483. }
  484. pair_udp_close(udp_sock);
  485. if (total_packets != 2 * NUM_PACKETS) {
  486. fprintf(stderr, "walk_v3_rx: received %u out of %u pkts\n",
  487. total_packets, NUM_PACKETS);
  488. exit(1);
  489. }
  490. fprintf(stderr, " %u pkts (%u bytes)", NUM_PACKETS, total_bytes >> 1);
  491. }
  492. static void walk_v3(int sock, struct ring *ring)
  493. {
  494. if (ring->type == PACKET_RX_RING)
  495. walk_v3_rx(sock, ring);
  496. else
  497. bug_on(1);
  498. }
  499. static void __v1_v2_fill(struct ring *ring, unsigned int blocks)
  500. {
  501. ring->req.tp_block_size = getpagesize() << 2;
  502. ring->req.tp_frame_size = TPACKET_ALIGNMENT << 7;
  503. ring->req.tp_block_nr = blocks;
  504. ring->req.tp_frame_nr = ring->req.tp_block_size /
  505. ring->req.tp_frame_size *
  506. ring->req.tp_block_nr;
  507. ring->mm_len = ring->req.tp_block_size * ring->req.tp_block_nr;
  508. ring->walk = walk_v1_v2;
  509. ring->rd_num = ring->req.tp_frame_nr;
  510. ring->flen = ring->req.tp_frame_size;
  511. }
  512. static void __v3_fill(struct ring *ring, unsigned int blocks)
  513. {
  514. ring->req3.tp_retire_blk_tov = 64;
  515. ring->req3.tp_sizeof_priv = 13;
  516. ring->req3.tp_feature_req_word |= TP_FT_REQ_FILL_RXHASH;
  517. ring->req3.tp_block_size = getpagesize() << 2;
  518. ring->req3.tp_frame_size = TPACKET_ALIGNMENT << 7;
  519. ring->req3.tp_block_nr = blocks;
  520. ring->req3.tp_frame_nr = ring->req3.tp_block_size /
  521. ring->req3.tp_frame_size *
  522. ring->req3.tp_block_nr;
  523. ring->mm_len = ring->req3.tp_block_size * ring->req3.tp_block_nr;
  524. ring->walk = walk_v3;
  525. ring->rd_num = ring->req3.tp_block_nr;
  526. ring->flen = ring->req3.tp_block_size;
  527. }
  528. static void setup_ring(int sock, struct ring *ring, int version, int type)
  529. {
  530. int ret = 0;
  531. unsigned int blocks = 256;
  532. ring->type = type;
  533. ring->version = version;
  534. switch (version) {
  535. case TPACKET_V1:
  536. case TPACKET_V2:
  537. if (type == PACKET_TX_RING)
  538. __v1_v2_set_packet_loss_discard(sock);
  539. __v1_v2_fill(ring, blocks);
  540. ret = setsockopt(sock, SOL_PACKET, type, &ring->req,
  541. sizeof(ring->req));
  542. break;
  543. case TPACKET_V3:
  544. __v3_fill(ring, blocks);
  545. ret = setsockopt(sock, SOL_PACKET, type, &ring->req3,
  546. sizeof(ring->req3));
  547. break;
  548. }
  549. if (ret == -1) {
  550. perror("setsockopt");
  551. exit(1);
  552. }
  553. ring->rd_len = ring->rd_num * sizeof(*ring->rd);
  554. ring->rd = malloc(ring->rd_len);
  555. if (ring->rd == NULL) {
  556. perror("malloc");
  557. exit(1);
  558. }
  559. total_packets = 0;
  560. total_bytes = 0;
  561. }
  562. static void mmap_ring(int sock, struct ring *ring)
  563. {
  564. int i;
  565. ring->mm_space = mmap(0, ring->mm_len, PROT_READ | PROT_WRITE,
  566. MAP_SHARED | MAP_LOCKED | MAP_POPULATE, sock, 0);
  567. if (ring->mm_space == MAP_FAILED) {
  568. perror("mmap");
  569. exit(1);
  570. }
  571. memset(ring->rd, 0, ring->rd_len);
  572. for (i = 0; i < ring->rd_num; ++i) {
  573. ring->rd[i].iov_base = ring->mm_space + (i * ring->flen);
  574. ring->rd[i].iov_len = ring->flen;
  575. }
  576. }
  577. static void bind_ring(int sock, struct ring *ring)
  578. {
  579. int ret;
  580. ring->ll.sll_family = PF_PACKET;
  581. ring->ll.sll_protocol = htons(ETH_P_ALL);
  582. ring->ll.sll_ifindex = if_nametoindex("lo");
  583. ring->ll.sll_hatype = 0;
  584. ring->ll.sll_pkttype = 0;
  585. ring->ll.sll_halen = 0;
  586. ret = bind(sock, (struct sockaddr *) &ring->ll, sizeof(ring->ll));
  587. if (ret == -1) {
  588. perror("bind");
  589. exit(1);
  590. }
  591. }
  592. static void walk_ring(int sock, struct ring *ring)
  593. {
  594. ring->walk(sock, ring);
  595. }
  596. static void unmap_ring(int sock, struct ring *ring)
  597. {
  598. munmap(ring->mm_space, ring->mm_len);
  599. free(ring->rd);
  600. }
  601. static int test_kernel_bit_width(void)
  602. {
  603. char in[512], *ptr;
  604. int num = 0, fd;
  605. ssize_t ret;
  606. fd = open("/proc/kallsyms", O_RDONLY);
  607. if (fd == -1) {
  608. perror("open");
  609. exit(1);
  610. }
  611. ret = read(fd, in, sizeof(in));
  612. if (ret <= 0) {
  613. perror("read");
  614. exit(1);
  615. }
  616. close(fd);
  617. ptr = in;
  618. while(!isspace(*ptr)) {
  619. num++;
  620. ptr++;
  621. }
  622. return num * 4;
  623. }
  624. static int test_user_bit_width(void)
  625. {
  626. return __WORDSIZE;
  627. }
  628. static const char *tpacket_str[] = {
  629. [TPACKET_V1] = "TPACKET_V1",
  630. [TPACKET_V2] = "TPACKET_V2",
  631. [TPACKET_V3] = "TPACKET_V3",
  632. };
  633. static const char *type_str[] = {
  634. [PACKET_RX_RING] = "PACKET_RX_RING",
  635. [PACKET_TX_RING] = "PACKET_TX_RING",
  636. };
  637. static int test_tpacket(int version, int type)
  638. {
  639. int sock;
  640. struct ring ring;
  641. fprintf(stderr, "test: %s with %s ", tpacket_str[version],
  642. type_str[type]);
  643. fflush(stderr);
  644. if (version == TPACKET_V1 &&
  645. test_kernel_bit_width() != test_user_bit_width()) {
  646. fprintf(stderr, "test: skip %s %s since user and kernel "
  647. "space have different bit width\n",
  648. tpacket_str[version], type_str[type]);
  649. return 0;
  650. }
  651. sock = pfsocket(version);
  652. memset(&ring, 0, sizeof(ring));
  653. setup_ring(sock, &ring, version, type);
  654. mmap_ring(sock, &ring);
  655. bind_ring(sock, &ring);
  656. walk_ring(sock, &ring);
  657. unmap_ring(sock, &ring);
  658. close(sock);
  659. fprintf(stderr, "\n");
  660. return 0;
  661. }
  662. int main(void)
  663. {
  664. int ret = 0;
  665. ret |= test_tpacket(TPACKET_V1, PACKET_RX_RING);
  666. ret |= test_tpacket(TPACKET_V1, PACKET_TX_RING);
  667. ret |= test_tpacket(TPACKET_V2, PACKET_RX_RING);
  668. ret |= test_tpacket(TPACKET_V2, PACKET_TX_RING);
  669. ret |= test_tpacket(TPACKET_V3, PACKET_RX_RING);
  670. if (ret)
  671. return 1;
  672. printf("OK. All tests passed\n");
  673. return 0;
  674. }