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