psock_tpacket.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  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 __v3_tx_kernel_ready(struct tpacket3_hdr *hdr)
  264. {
  265. return !(hdr->tp_status & (TP_STATUS_SEND_REQUEST | TP_STATUS_SENDING));
  266. }
  267. static inline void __v3_tx_user_ready(struct tpacket3_hdr *hdr)
  268. {
  269. hdr->tp_status = TP_STATUS_SEND_REQUEST;
  270. __sync_synchronize();
  271. }
  272. static inline int __tx_kernel_ready(void *base, int version)
  273. {
  274. switch (version) {
  275. case TPACKET_V1:
  276. return __v1_tx_kernel_ready(base);
  277. case TPACKET_V2:
  278. return __v2_tx_kernel_ready(base);
  279. case TPACKET_V3:
  280. return __v3_tx_kernel_ready(base);
  281. default:
  282. bug_on(1);
  283. return 0;
  284. }
  285. }
  286. static inline void __tx_user_ready(void *base, int version)
  287. {
  288. switch (version) {
  289. case TPACKET_V1:
  290. __v1_tx_user_ready(base);
  291. break;
  292. case TPACKET_V2:
  293. __v2_tx_user_ready(base);
  294. break;
  295. case TPACKET_V3:
  296. __v3_tx_user_ready(base);
  297. break;
  298. }
  299. }
  300. static void __v1_v2_set_packet_loss_discard(int sock)
  301. {
  302. int ret, discard = 1;
  303. ret = setsockopt(sock, SOL_PACKET, PACKET_LOSS, (void *) &discard,
  304. sizeof(discard));
  305. if (ret == -1) {
  306. perror("setsockopt");
  307. exit(1);
  308. }
  309. }
  310. static inline void *get_next_frame(struct ring *ring, int n)
  311. {
  312. uint8_t *f0 = ring->rd[0].iov_base;
  313. switch (ring->version) {
  314. case TPACKET_V1:
  315. case TPACKET_V2:
  316. return ring->rd[n].iov_base;
  317. case TPACKET_V3:
  318. return f0 + (n * ring->req3.tp_frame_size);
  319. default:
  320. bug_on(1);
  321. }
  322. }
  323. static void walk_tx(int sock, struct ring *ring)
  324. {
  325. struct pollfd pfd;
  326. int rcv_sock, ret;
  327. size_t packet_len;
  328. union frame_map ppd;
  329. char packet[1024];
  330. unsigned int frame_num = 0, got = 0;
  331. struct sockaddr_ll ll = {
  332. .sll_family = PF_PACKET,
  333. .sll_halen = ETH_ALEN,
  334. };
  335. int nframes;
  336. /* TPACKET_V{1,2} sets up the ring->rd* related variables based
  337. * on frames (e.g., rd_num is tp_frame_nr) whereas V3 sets these
  338. * up based on blocks (e.g, rd_num is tp_block_nr)
  339. */
  340. if (ring->version <= TPACKET_V2)
  341. nframes = ring->rd_num;
  342. else
  343. nframes = ring->req3.tp_frame_nr;
  344. bug_on(ring->type != PACKET_TX_RING);
  345. bug_on(nframes < NUM_PACKETS);
  346. rcv_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
  347. if (rcv_sock == -1) {
  348. perror("socket");
  349. exit(1);
  350. }
  351. pair_udp_setfilter(rcv_sock);
  352. ll.sll_ifindex = if_nametoindex("lo");
  353. ret = bind(rcv_sock, (struct sockaddr *) &ll, sizeof(ll));
  354. if (ret == -1) {
  355. perror("bind");
  356. exit(1);
  357. }
  358. memset(&pfd, 0, sizeof(pfd));
  359. pfd.fd = sock;
  360. pfd.events = POLLOUT | POLLERR;
  361. pfd.revents = 0;
  362. total_packets = NUM_PACKETS;
  363. create_payload(packet, &packet_len);
  364. while (total_packets > 0) {
  365. void *next = get_next_frame(ring, frame_num);
  366. while (__tx_kernel_ready(next, ring->version) &&
  367. total_packets > 0) {
  368. ppd.raw = next;
  369. switch (ring->version) {
  370. case TPACKET_V1:
  371. ppd.v1->tp_h.tp_snaplen = packet_len;
  372. ppd.v1->tp_h.tp_len = packet_len;
  373. memcpy((uint8_t *) ppd.raw + TPACKET_HDRLEN -
  374. sizeof(struct sockaddr_ll), packet,
  375. packet_len);
  376. total_bytes += ppd.v1->tp_h.tp_snaplen;
  377. break;
  378. case TPACKET_V2:
  379. ppd.v2->tp_h.tp_snaplen = packet_len;
  380. ppd.v2->tp_h.tp_len = packet_len;
  381. memcpy((uint8_t *) ppd.raw + TPACKET2_HDRLEN -
  382. sizeof(struct sockaddr_ll), packet,
  383. packet_len);
  384. total_bytes += ppd.v2->tp_h.tp_snaplen;
  385. break;
  386. case TPACKET_V3: {
  387. struct tpacket3_hdr *tx = next;
  388. tx->tp_snaplen = packet_len;
  389. tx->tp_len = packet_len;
  390. tx->tp_next_offset = 0;
  391. memcpy((uint8_t *)tx + TPACKET3_HDRLEN -
  392. sizeof(struct sockaddr_ll), packet,
  393. packet_len);
  394. total_bytes += tx->tp_snaplen;
  395. break;
  396. }
  397. }
  398. status_bar_update();
  399. total_packets--;
  400. __tx_user_ready(next, ring->version);
  401. frame_num = (frame_num + 1) % nframes;
  402. }
  403. poll(&pfd, 1, 1);
  404. }
  405. bug_on(total_packets != 0);
  406. ret = sendto(sock, NULL, 0, 0, NULL, 0);
  407. if (ret == -1) {
  408. perror("sendto");
  409. exit(1);
  410. }
  411. while ((ret = recvfrom(rcv_sock, packet, sizeof(packet),
  412. 0, NULL, NULL)) > 0 &&
  413. total_packets < NUM_PACKETS) {
  414. got += ret;
  415. test_payload(packet, ret);
  416. status_bar_update();
  417. total_packets++;
  418. }
  419. close(rcv_sock);
  420. if (total_packets != NUM_PACKETS) {
  421. fprintf(stderr, "walk_v%d_rx: received %u out of %u pkts\n",
  422. ring->version, total_packets, NUM_PACKETS);
  423. exit(1);
  424. }
  425. fprintf(stderr, " %u pkts (%u bytes)", NUM_PACKETS, got);
  426. }
  427. static void walk_v1_v2(int sock, struct ring *ring)
  428. {
  429. if (ring->type == PACKET_RX_RING)
  430. walk_v1_v2_rx(sock, ring);
  431. else
  432. walk_tx(sock, ring);
  433. }
  434. static uint64_t __v3_prev_block_seq_num = 0;
  435. void __v3_test_block_seq_num(struct block_desc *pbd)
  436. {
  437. if (__v3_prev_block_seq_num + 1 != pbd->h1.seq_num) {
  438. fprintf(stderr, "\nprev_block_seq_num:%"PRIu64", expected "
  439. "seq:%"PRIu64" != actual seq:%"PRIu64"\n",
  440. __v3_prev_block_seq_num, __v3_prev_block_seq_num + 1,
  441. (uint64_t) pbd->h1.seq_num);
  442. exit(1);
  443. }
  444. __v3_prev_block_seq_num = pbd->h1.seq_num;
  445. }
  446. static void __v3_test_block_len(struct block_desc *pbd, uint32_t bytes, int block_num)
  447. {
  448. if (pbd->h1.num_pkts && bytes != pbd->h1.blk_len) {
  449. fprintf(stderr, "\nblock:%u with %upackets, expected "
  450. "len:%u != actual len:%u\n", block_num,
  451. pbd->h1.num_pkts, bytes, pbd->h1.blk_len);
  452. exit(1);
  453. }
  454. }
  455. static void __v3_test_block_header(struct block_desc *pbd, const int block_num)
  456. {
  457. if ((pbd->h1.block_status & TP_STATUS_USER) == 0) {
  458. fprintf(stderr, "\nblock %u: not in TP_STATUS_USER\n", block_num);
  459. exit(1);
  460. }
  461. __v3_test_block_seq_num(pbd);
  462. }
  463. static void __v3_walk_block(struct block_desc *pbd, const int block_num)
  464. {
  465. int num_pkts = pbd->h1.num_pkts, i;
  466. unsigned long bytes = 0, bytes_with_padding = ALIGN_8(sizeof(*pbd));
  467. struct tpacket3_hdr *ppd;
  468. __v3_test_block_header(pbd, block_num);
  469. ppd = (struct tpacket3_hdr *) ((uint8_t *) pbd +
  470. pbd->h1.offset_to_first_pkt);
  471. for (i = 0; i < num_pkts; ++i) {
  472. bytes += ppd->tp_snaplen;
  473. if (ppd->tp_next_offset)
  474. bytes_with_padding += ppd->tp_next_offset;
  475. else
  476. bytes_with_padding += ALIGN_8(ppd->tp_snaplen + ppd->tp_mac);
  477. test_payload((uint8_t *) ppd + ppd->tp_mac, ppd->tp_snaplen);
  478. status_bar_update();
  479. total_packets++;
  480. ppd = (struct tpacket3_hdr *) ((uint8_t *) ppd + ppd->tp_next_offset);
  481. __sync_synchronize();
  482. }
  483. __v3_test_block_len(pbd, bytes_with_padding, block_num);
  484. total_bytes += bytes;
  485. }
  486. void __v3_flush_block(struct block_desc *pbd)
  487. {
  488. pbd->h1.block_status = TP_STATUS_KERNEL;
  489. __sync_synchronize();
  490. }
  491. static void walk_v3_rx(int sock, struct ring *ring)
  492. {
  493. unsigned int block_num = 0;
  494. struct pollfd pfd;
  495. struct block_desc *pbd;
  496. int udp_sock[2];
  497. bug_on(ring->type != PACKET_RX_RING);
  498. pair_udp_open(udp_sock, PORT_BASE);
  499. pair_udp_setfilter(sock);
  500. memset(&pfd, 0, sizeof(pfd));
  501. pfd.fd = sock;
  502. pfd.events = POLLIN | POLLERR;
  503. pfd.revents = 0;
  504. pair_udp_send(udp_sock, NUM_PACKETS);
  505. while (total_packets < NUM_PACKETS * 2) {
  506. pbd = (struct block_desc *) ring->rd[block_num].iov_base;
  507. while ((pbd->h1.block_status & TP_STATUS_USER) == 0)
  508. poll(&pfd, 1, 1);
  509. __v3_walk_block(pbd, block_num);
  510. __v3_flush_block(pbd);
  511. block_num = (block_num + 1) % ring->rd_num;
  512. }
  513. pair_udp_close(udp_sock);
  514. if (total_packets != 2 * NUM_PACKETS) {
  515. fprintf(stderr, "walk_v3_rx: received %u out of %u pkts\n",
  516. total_packets, NUM_PACKETS);
  517. exit(1);
  518. }
  519. fprintf(stderr, " %u pkts (%u bytes)", NUM_PACKETS, total_bytes >> 1);
  520. }
  521. static void walk_v3(int sock, struct ring *ring)
  522. {
  523. if (ring->type == PACKET_RX_RING)
  524. walk_v3_rx(sock, ring);
  525. else
  526. walk_tx(sock, ring);
  527. }
  528. static void __v1_v2_fill(struct ring *ring, unsigned int blocks)
  529. {
  530. ring->req.tp_block_size = getpagesize() << 2;
  531. ring->req.tp_frame_size = TPACKET_ALIGNMENT << 7;
  532. ring->req.tp_block_nr = blocks;
  533. ring->req.tp_frame_nr = ring->req.tp_block_size /
  534. ring->req.tp_frame_size *
  535. ring->req.tp_block_nr;
  536. ring->mm_len = ring->req.tp_block_size * ring->req.tp_block_nr;
  537. ring->walk = walk_v1_v2;
  538. ring->rd_num = ring->req.tp_frame_nr;
  539. ring->flen = ring->req.tp_frame_size;
  540. }
  541. static void __v3_fill(struct ring *ring, unsigned int blocks, int type)
  542. {
  543. if (type == PACKET_RX_RING) {
  544. ring->req3.tp_retire_blk_tov = 64;
  545. ring->req3.tp_sizeof_priv = 0;
  546. ring->req3.tp_feature_req_word = TP_FT_REQ_FILL_RXHASH;
  547. }
  548. ring->req3.tp_block_size = getpagesize() << 2;
  549. ring->req3.tp_frame_size = TPACKET_ALIGNMENT << 7;
  550. ring->req3.tp_block_nr = blocks;
  551. ring->req3.tp_frame_nr = ring->req3.tp_block_size /
  552. ring->req3.tp_frame_size *
  553. ring->req3.tp_block_nr;
  554. ring->mm_len = ring->req3.tp_block_size * ring->req3.tp_block_nr;
  555. ring->walk = walk_v3;
  556. ring->rd_num = ring->req3.tp_block_nr;
  557. ring->flen = ring->req3.tp_block_size;
  558. }
  559. static void setup_ring(int sock, struct ring *ring, int version, int type)
  560. {
  561. int ret = 0;
  562. unsigned int blocks = 256;
  563. ring->type = type;
  564. ring->version = version;
  565. switch (version) {
  566. case TPACKET_V1:
  567. case TPACKET_V2:
  568. if (type == PACKET_TX_RING)
  569. __v1_v2_set_packet_loss_discard(sock);
  570. __v1_v2_fill(ring, blocks);
  571. ret = setsockopt(sock, SOL_PACKET, type, &ring->req,
  572. sizeof(ring->req));
  573. break;
  574. case TPACKET_V3:
  575. __v3_fill(ring, blocks, type);
  576. ret = setsockopt(sock, SOL_PACKET, type, &ring->req3,
  577. sizeof(ring->req3));
  578. break;
  579. }
  580. if (ret == -1) {
  581. perror("setsockopt");
  582. exit(1);
  583. }
  584. ring->rd_len = ring->rd_num * sizeof(*ring->rd);
  585. ring->rd = malloc(ring->rd_len);
  586. if (ring->rd == NULL) {
  587. perror("malloc");
  588. exit(1);
  589. }
  590. total_packets = 0;
  591. total_bytes = 0;
  592. }
  593. static void mmap_ring(int sock, struct ring *ring)
  594. {
  595. int i;
  596. ring->mm_space = mmap(0, ring->mm_len, PROT_READ | PROT_WRITE,
  597. MAP_SHARED | MAP_LOCKED | MAP_POPULATE, sock, 0);
  598. if (ring->mm_space == MAP_FAILED) {
  599. perror("mmap");
  600. exit(1);
  601. }
  602. memset(ring->rd, 0, ring->rd_len);
  603. for (i = 0; i < ring->rd_num; ++i) {
  604. ring->rd[i].iov_base = ring->mm_space + (i * ring->flen);
  605. ring->rd[i].iov_len = ring->flen;
  606. }
  607. }
  608. static void bind_ring(int sock, struct ring *ring)
  609. {
  610. int ret;
  611. ring->ll.sll_family = PF_PACKET;
  612. ring->ll.sll_protocol = htons(ETH_P_ALL);
  613. ring->ll.sll_ifindex = if_nametoindex("lo");
  614. ring->ll.sll_hatype = 0;
  615. ring->ll.sll_pkttype = 0;
  616. ring->ll.sll_halen = 0;
  617. ret = bind(sock, (struct sockaddr *) &ring->ll, sizeof(ring->ll));
  618. if (ret == -1) {
  619. perror("bind");
  620. exit(1);
  621. }
  622. }
  623. static void walk_ring(int sock, struct ring *ring)
  624. {
  625. ring->walk(sock, ring);
  626. }
  627. static void unmap_ring(int sock, struct ring *ring)
  628. {
  629. munmap(ring->mm_space, ring->mm_len);
  630. free(ring->rd);
  631. }
  632. static int test_kernel_bit_width(void)
  633. {
  634. char in[512], *ptr;
  635. int num = 0, fd;
  636. ssize_t ret;
  637. fd = open("/proc/kallsyms", O_RDONLY);
  638. if (fd == -1) {
  639. perror("open");
  640. exit(1);
  641. }
  642. ret = read(fd, in, sizeof(in));
  643. if (ret <= 0) {
  644. perror("read");
  645. exit(1);
  646. }
  647. close(fd);
  648. ptr = in;
  649. while(!isspace(*ptr)) {
  650. num++;
  651. ptr++;
  652. }
  653. return num * 4;
  654. }
  655. static int test_user_bit_width(void)
  656. {
  657. return __WORDSIZE;
  658. }
  659. static const char *tpacket_str[] = {
  660. [TPACKET_V1] = "TPACKET_V1",
  661. [TPACKET_V2] = "TPACKET_V2",
  662. [TPACKET_V3] = "TPACKET_V3",
  663. };
  664. static const char *type_str[] = {
  665. [PACKET_RX_RING] = "PACKET_RX_RING",
  666. [PACKET_TX_RING] = "PACKET_TX_RING",
  667. };
  668. static int test_tpacket(int version, int type)
  669. {
  670. int sock;
  671. struct ring ring;
  672. fprintf(stderr, "test: %s with %s ", tpacket_str[version],
  673. type_str[type]);
  674. fflush(stderr);
  675. if (version == TPACKET_V1 &&
  676. test_kernel_bit_width() != test_user_bit_width()) {
  677. fprintf(stderr, "test: skip %s %s since user and kernel "
  678. "space have different bit width\n",
  679. tpacket_str[version], type_str[type]);
  680. return 0;
  681. }
  682. sock = pfsocket(version);
  683. memset(&ring, 0, sizeof(ring));
  684. setup_ring(sock, &ring, version, type);
  685. mmap_ring(sock, &ring);
  686. bind_ring(sock, &ring);
  687. walk_ring(sock, &ring);
  688. unmap_ring(sock, &ring);
  689. close(sock);
  690. fprintf(stderr, "\n");
  691. return 0;
  692. }
  693. int main(void)
  694. {
  695. int ret = 0;
  696. ret |= test_tpacket(TPACKET_V1, PACKET_RX_RING);
  697. ret |= test_tpacket(TPACKET_V1, PACKET_TX_RING);
  698. ret |= test_tpacket(TPACKET_V2, PACKET_RX_RING);
  699. ret |= test_tpacket(TPACKET_V2, PACKET_TX_RING);
  700. ret |= test_tpacket(TPACKET_V3, PACKET_RX_RING);
  701. ret |= test_tpacket(TPACKET_V3, PACKET_TX_RING);
  702. if (ret)
  703. return 1;
  704. printf("OK. All tests passed\n");
  705. return 0;
  706. }