psock_tpacket.c 18 KB

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