psock_tpacket.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  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. #include "../kselftest.h"
  61. #ifndef bug_on
  62. # define bug_on(cond) assert(!(cond))
  63. #endif
  64. #ifndef __aligned_tpacket
  65. # define __aligned_tpacket __attribute__((aligned(TPACKET_ALIGNMENT)))
  66. #endif
  67. #ifndef __align_tpacket
  68. # define __align_tpacket(x) __attribute__((aligned(TPACKET_ALIGN(x))))
  69. #endif
  70. #define NUM_PACKETS 100
  71. #define ALIGN_8(x) (((x) + 8 - 1) & ~(8 - 1))
  72. struct ring {
  73. struct iovec *rd;
  74. uint8_t *mm_space;
  75. size_t mm_len, rd_len;
  76. struct sockaddr_ll ll;
  77. void (*walk)(int sock, struct ring *ring);
  78. int type, rd_num, flen, version;
  79. union {
  80. struct tpacket_req req;
  81. struct tpacket_req3 req3;
  82. };
  83. };
  84. struct block_desc {
  85. uint32_t version;
  86. uint32_t offset_to_priv;
  87. struct tpacket_hdr_v1 h1;
  88. };
  89. union frame_map {
  90. struct {
  91. struct tpacket_hdr tp_h __aligned_tpacket;
  92. struct sockaddr_ll s_ll __align_tpacket(sizeof(struct tpacket_hdr));
  93. } *v1;
  94. struct {
  95. struct tpacket2_hdr tp_h __aligned_tpacket;
  96. struct sockaddr_ll s_ll __align_tpacket(sizeof(struct tpacket2_hdr));
  97. } *v2;
  98. void *raw;
  99. };
  100. static unsigned int total_packets, total_bytes;
  101. static int pfsocket(int ver)
  102. {
  103. int ret, sock = socket(PF_PACKET, SOCK_RAW, 0);
  104. if (sock == -1) {
  105. perror("socket");
  106. exit(1);
  107. }
  108. ret = setsockopt(sock, SOL_PACKET, PACKET_VERSION, &ver, sizeof(ver));
  109. if (ret == -1) {
  110. perror("setsockopt");
  111. exit(1);
  112. }
  113. return sock;
  114. }
  115. static void status_bar_update(void)
  116. {
  117. if (total_packets % 10 == 0) {
  118. fprintf(stderr, ".");
  119. fflush(stderr);
  120. }
  121. }
  122. static void test_payload(void *pay, size_t len)
  123. {
  124. struct ethhdr *eth = pay;
  125. if (len < sizeof(struct ethhdr)) {
  126. fprintf(stderr, "test_payload: packet too "
  127. "small: %zu bytes!\n", len);
  128. exit(1);
  129. }
  130. if (eth->h_proto != htons(ETH_P_IP)) {
  131. fprintf(stderr, "test_payload: wrong ethernet "
  132. "type: 0x%x!\n", ntohs(eth->h_proto));
  133. exit(1);
  134. }
  135. }
  136. static void create_payload(void *pay, size_t *len)
  137. {
  138. int i;
  139. struct ethhdr *eth = pay;
  140. struct iphdr *ip = pay + sizeof(*eth);
  141. /* Lets create some broken crap, that still passes
  142. * our BPF filter.
  143. */
  144. *len = DATA_LEN + 42;
  145. memset(pay, 0xff, ETH_ALEN * 2);
  146. eth->h_proto = htons(ETH_P_IP);
  147. for (i = 0; i < sizeof(*ip); ++i)
  148. ((uint8_t *) pay)[i + sizeof(*eth)] = (uint8_t) rand();
  149. ip->ihl = 5;
  150. ip->version = 4;
  151. ip->protocol = 0x11;
  152. ip->frag_off = 0;
  153. ip->ttl = 64;
  154. ip->tot_len = htons((uint16_t) *len - sizeof(*eth));
  155. ip->saddr = htonl(INADDR_LOOPBACK);
  156. ip->daddr = htonl(INADDR_LOOPBACK);
  157. memset(pay + sizeof(*eth) + sizeof(*ip),
  158. DATA_CHAR, DATA_LEN);
  159. }
  160. static inline int __v1_rx_kernel_ready(struct tpacket_hdr *hdr)
  161. {
  162. return ((hdr->tp_status & TP_STATUS_USER) == TP_STATUS_USER);
  163. }
  164. static inline void __v1_rx_user_ready(struct tpacket_hdr *hdr)
  165. {
  166. hdr->tp_status = TP_STATUS_KERNEL;
  167. __sync_synchronize();
  168. }
  169. static inline int __v2_rx_kernel_ready(struct tpacket2_hdr *hdr)
  170. {
  171. return ((hdr->tp_status & TP_STATUS_USER) == TP_STATUS_USER);
  172. }
  173. static inline void __v2_rx_user_ready(struct tpacket2_hdr *hdr)
  174. {
  175. hdr->tp_status = TP_STATUS_KERNEL;
  176. __sync_synchronize();
  177. }
  178. static inline int __v1_v2_rx_kernel_ready(void *base, int version)
  179. {
  180. switch (version) {
  181. case TPACKET_V1:
  182. return __v1_rx_kernel_ready(base);
  183. case TPACKET_V2:
  184. return __v2_rx_kernel_ready(base);
  185. default:
  186. bug_on(1);
  187. return 0;
  188. }
  189. }
  190. static inline void __v1_v2_rx_user_ready(void *base, int version)
  191. {
  192. switch (version) {
  193. case TPACKET_V1:
  194. __v1_rx_user_ready(base);
  195. break;
  196. case TPACKET_V2:
  197. __v2_rx_user_ready(base);
  198. break;
  199. }
  200. }
  201. static void walk_v1_v2_rx(int sock, struct ring *ring)
  202. {
  203. struct pollfd pfd;
  204. int udp_sock[2];
  205. union frame_map ppd;
  206. unsigned int frame_num = 0;
  207. bug_on(ring->type != PACKET_RX_RING);
  208. pair_udp_open(udp_sock, PORT_BASE);
  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. memset(&pfd, 0, sizeof(pfd));
  500. pfd.fd = sock;
  501. pfd.events = POLLIN | POLLERR;
  502. pfd.revents = 0;
  503. pair_udp_send(udp_sock, NUM_PACKETS);
  504. while (total_packets < NUM_PACKETS * 2) {
  505. pbd = (struct block_desc *) ring->rd[block_num].iov_base;
  506. while ((pbd->h1.block_status & TP_STATUS_USER) == 0)
  507. poll(&pfd, 1, 1);
  508. __v3_walk_block(pbd, block_num);
  509. __v3_flush_block(pbd);
  510. block_num = (block_num + 1) % ring->rd_num;
  511. }
  512. pair_udp_close(udp_sock);
  513. if (total_packets != 2 * NUM_PACKETS) {
  514. fprintf(stderr, "walk_v3_rx: received %u out of %u pkts\n",
  515. total_packets, NUM_PACKETS);
  516. exit(1);
  517. }
  518. fprintf(stderr, " %u pkts (%u bytes)", NUM_PACKETS, total_bytes >> 1);
  519. }
  520. static void walk_v3(int sock, struct ring *ring)
  521. {
  522. if (ring->type == PACKET_RX_RING)
  523. walk_v3_rx(sock, ring);
  524. else
  525. walk_tx(sock, ring);
  526. }
  527. static void __v1_v2_fill(struct ring *ring, unsigned int blocks)
  528. {
  529. ring->req.tp_block_size = getpagesize() << 2;
  530. ring->req.tp_frame_size = TPACKET_ALIGNMENT << 7;
  531. ring->req.tp_block_nr = blocks;
  532. ring->req.tp_frame_nr = ring->req.tp_block_size /
  533. ring->req.tp_frame_size *
  534. ring->req.tp_block_nr;
  535. ring->mm_len = ring->req.tp_block_size * ring->req.tp_block_nr;
  536. ring->walk = walk_v1_v2;
  537. ring->rd_num = ring->req.tp_frame_nr;
  538. ring->flen = ring->req.tp_frame_size;
  539. }
  540. static void __v3_fill(struct ring *ring, unsigned int blocks, int type)
  541. {
  542. if (type == PACKET_RX_RING) {
  543. ring->req3.tp_retire_blk_tov = 64;
  544. ring->req3.tp_sizeof_priv = 0;
  545. ring->req3.tp_feature_req_word = TP_FT_REQ_FILL_RXHASH;
  546. }
  547. ring->req3.tp_block_size = getpagesize() << 2;
  548. ring->req3.tp_frame_size = TPACKET_ALIGNMENT << 7;
  549. ring->req3.tp_block_nr = blocks;
  550. ring->req3.tp_frame_nr = ring->req3.tp_block_size /
  551. ring->req3.tp_frame_size *
  552. ring->req3.tp_block_nr;
  553. ring->mm_len = ring->req3.tp_block_size * ring->req3.tp_block_nr;
  554. ring->walk = walk_v3;
  555. ring->rd_num = ring->req3.tp_block_nr;
  556. ring->flen = ring->req3.tp_block_size;
  557. }
  558. static void setup_ring(int sock, struct ring *ring, int version, int type)
  559. {
  560. int ret = 0;
  561. unsigned int blocks = 256;
  562. ring->type = type;
  563. ring->version = version;
  564. switch (version) {
  565. case TPACKET_V1:
  566. case TPACKET_V2:
  567. if (type == PACKET_TX_RING)
  568. __v1_v2_set_packet_loss_discard(sock);
  569. __v1_v2_fill(ring, blocks);
  570. ret = setsockopt(sock, SOL_PACKET, type, &ring->req,
  571. sizeof(ring->req));
  572. break;
  573. case TPACKET_V3:
  574. __v3_fill(ring, blocks, type);
  575. ret = setsockopt(sock, SOL_PACKET, type, &ring->req3,
  576. sizeof(ring->req3));
  577. break;
  578. }
  579. if (ret == -1) {
  580. perror("setsockopt");
  581. exit(1);
  582. }
  583. ring->rd_len = ring->rd_num * sizeof(*ring->rd);
  584. ring->rd = malloc(ring->rd_len);
  585. if (ring->rd == NULL) {
  586. perror("malloc");
  587. exit(1);
  588. }
  589. total_packets = 0;
  590. total_bytes = 0;
  591. }
  592. static void mmap_ring(int sock, struct ring *ring)
  593. {
  594. int i;
  595. ring->mm_space = mmap(0, ring->mm_len, PROT_READ | PROT_WRITE,
  596. MAP_SHARED | MAP_LOCKED | MAP_POPULATE, sock, 0);
  597. if (ring->mm_space == MAP_FAILED) {
  598. perror("mmap");
  599. exit(1);
  600. }
  601. memset(ring->rd, 0, ring->rd_len);
  602. for (i = 0; i < ring->rd_num; ++i) {
  603. ring->rd[i].iov_base = ring->mm_space + (i * ring->flen);
  604. ring->rd[i].iov_len = ring->flen;
  605. }
  606. }
  607. static void bind_ring(int sock, struct ring *ring)
  608. {
  609. int ret;
  610. pair_udp_setfilter(sock);
  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 KSFT_SKIP;
  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. }