psock_tpacket.c 18 KB

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