proto_ipv6_mobility_hdr.h: Headerfixing
[netsniff-ng.git] / src / ring.h
blobc62b9da33dea4502418619a59110f024aa6572e1
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2009, 2010 Daniel Borkmann.
5 * Subject to the GPL, version 2.
6 */
8 #ifndef RING_H
9 #define RING_H
12 * "I love the smell of 10GbE in the morning. Smells like ... victory."
13 * - W. Richard Stevens, "Secret Teachings of the UNIX Environment"
16 #include <stdio.h>
17 #include <stdint.h>
18 #include <linux/if_packet.h>
19 #include <string.h>
20 #include <poll.h>
21 #include <sys/poll.h>
23 #include "xsys.h"
24 #include "built_in.h"
25 #include "mtrand.h"
27 struct frame_map {
28 struct tpacket_hdr tp_h __aligned_tpacket;
29 struct sockaddr_ll s_ll __aligned_tpacket;
32 struct ring {
33 struct iovec *frames __cacheline_aligned;
34 uint8_t *mm_space __cacheline_aligned;
35 size_t mm_len;
36 struct tpacket_req layout;
37 struct sockaddr_ll s_ll;
40 static inline void next_slot(unsigned int *it, struct ring *ring)
42 (*it)++;
43 atomic_cmp_swp(it, ring->layout.tp_frame_nr, 0);
46 static inline void next_slot_prerd(unsigned int *it, struct ring *ring)
48 (*it)++;
49 atomic_cmp_swp(it, ring->layout.tp_frame_nr, 0);
50 prefetch_rd_hi(ring->frames[*it].iov_base);
53 static inline void next_slot_prewr(unsigned int *it, struct ring *ring)
55 (*it)++;
56 atomic_cmp_swp(it, ring->layout.tp_frame_nr, 0);
57 prefetch_wr_hi(ring->frames[*it].iov_base);
60 static inline void next_rnd_slot(unsigned int *it, struct ring *ring)
62 *it = mt_rand_int32() % ring->layout.tp_frame_nr;
65 #define RING_SIZE_FALLBACK (1 << 26)
67 static inline unsigned int ring_size(char *ifname, unsigned int size)
69 if (size > 0)
70 return size;
73 * Device bitrate in bytes times two as ring size.
74 * Fallback => ~ 64,00 MB
75 * 10 MBit => ~ 2,38 MB
76 * 54 MBit => ~ 12,88 MB
77 * 100 MBit => ~ 23,84 MB
78 * 300 MBit => ~ 71,52 MB
79 * 1.000 MBit => ~ 238,42 MB
80 * 10.000 MBit => ~ 2.384.18 MB
82 size = device_bitrate(ifname);
83 size = (size * 1000000) / 8;
84 size = size * 2;
85 if (size == 0)
86 size = RING_SIZE_FALLBACK;
87 return round_up_cacheline(size);
90 enum ring_mode {
91 RING_MODE_EGRESS,
92 RING_MODE_INGRESS,
95 static inline unsigned int ring_frame_size(struct ring *ring)
97 return ring->layout.tp_frame_size;
100 static inline void tpacket_hdr_clone(struct tpacket_hdr *thdrd,
101 struct tpacket_hdr *thdrs)
103 thdrd->tp_sec = thdrs->tp_sec;
104 thdrd->tp_usec = thdrs->tp_usec;
105 thdrd->tp_snaplen = thdrs->tp_snaplen;
106 thdrd->tp_len = thdrs->tp_len;
109 #ifndef POLLRDNORM
110 # define POLLRDNORM 0x0040
111 #endif
112 #ifndef POLLWRNORM
113 # define POLLWRNORM 0x0100
114 #endif
115 #ifndef POLLRDHUP
116 # define POLLRDHUP 0x2000
117 #endif
119 #define POLL_NEXT_PKT 0
120 #define POLL_MOVE_OUT 1
122 static inline void prepare_polling(int sock, struct pollfd *pfd)
124 memset(pfd, 0, sizeof(*pfd));
125 pfd->fd = sock;
126 pfd->revents = 0;
127 pfd->events = POLLIN | POLLRDNORM | POLLERR;
130 #endif /* RING_H */