netsniff-ng: Only use TPACKET_V3 if HAVE_TPACKET3 is defined
[netsniff-ng.git] / ring.h
blob3b02c7608fdfa122db363da7ddb60e5413d0a91e
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2009, 2010 Daniel Borkmann.
4 * Subject to the GPL, version 2.
5 */
7 #ifndef RING_H
8 #define RING_H
11 * "I love the smell of 10GbE in the morning. Smells like ... victory."
12 * - W. Richard Stevens, "Secret Teachings of the UNIX Environment"
15 #include <stdio.h>
16 #include <stdint.h>
17 #include <linux/if_packet.h>
18 #include <linux/socket.h>
19 #include <linux/sockios.h>
20 #include <sys/ioctl.h>
21 #include <string.h>
22 #include <poll.h>
24 #include "built_in.h"
25 #include "die.h"
26 #include "dev.h"
27 #include "config.h"
29 union tpacket_uhdr {
30 struct tpacket_hdr *h1;
31 struct tpacket2_hdr *h2;
32 #ifdef HAVE_TPACKET3
33 struct tpacket3_hdr *h3;
34 #endif
35 void *raw;
38 #ifdef HAVE_TPACKET3
39 #define tpacket_uhdr(hdr, member, v3) \
40 ((v3) ? ((hdr).h3)->member : ((hdr).h2)->member)
41 #else
42 #define tpacket_uhdr(hdr, member, v3) \
43 (((hdr).h2)->member)
44 #endif /* HAVE_TPACKET3 */
46 struct frame_map {
47 struct tpacket2_hdr tp_h __aligned_tpacket;
48 struct sockaddr_ll s_ll __align_tpacket(sizeof(struct tpacket2_hdr));
51 #ifdef HAVE_TPACKET3
52 struct block_desc {
53 uint32_t version;
54 uint32_t offset_to_priv;
55 struct tpacket_hdr_v1 h1;
57 #endif
59 struct ring {
60 struct iovec *frames;
61 uint8_t *mm_space;
62 size_t mm_len;
63 struct sockaddr_ll s_ll;
64 union {
65 struct tpacket_req layout;
66 #ifdef HAVE_TPACKET3
67 struct tpacket_req3 layout3;
68 #endif
69 uint8_t raw;
73 static inline void next_rnd_slot(unsigned int *it, struct ring *ring)
75 *it = rand() % ring->layout.tp_frame_nr;
78 static inline size_t ring_size(char *ifname, size_t size)
80 if (size > 0)
81 return size;
84 * Device bitrate in bytes times two as ring size.
85 * Fallback => ~ 64,00 MB
86 * 10 MBit => ~ 2,38 MB
87 * 54 MBit => ~ 12,88 MB
88 * 100 MBit => ~ 23,84 MB
89 * 300 MBit => ~ 71,52 MB
90 * 1.000 MBit => ~ 238,42 MB
91 * 10.000 MBit => ~ 2.384.18 MB
93 size = device_bitrate(ifname);
94 size = (size * 1000000) / 8;
95 size = size * 2;
96 if (size == 0)
97 size = 1 << 26;
99 return round_up_cacheline(size);
102 static inline unsigned int ring_frame_size(struct ring *ring)
104 return ring->layout.tp_frame_size;
107 static inline void ring_verify_layout(struct ring *ring)
109 bug_on(ring->layout.tp_block_size < ring->layout.tp_frame_size);
110 bug_on((ring->layout.tp_block_size % ring->layout.tp_frame_size) != 0);
111 bug_on((ring->layout.tp_block_size % RUNTIME_PAGE_SIZE) != 0);
114 static inline void tpacket_hdr_clone(struct tpacket2_hdr *thdrd,
115 struct tpacket2_hdr *thdrs)
117 thdrd->tp_sec = thdrs->tp_sec;
118 thdrd->tp_nsec = thdrs->tp_nsec;
119 thdrd->tp_snaplen = thdrs->tp_snaplen;
120 thdrd->tp_len = thdrs->tp_len;
123 static inline void prepare_polling(int sock, struct pollfd *pfd)
125 memset(pfd, 0, sizeof(*pfd));
126 pfd->fd = sock;
127 pfd->revents = 0;
128 pfd->events = POLLIN | POLLRDNORM | POLLERR;
131 static inline void __set_sockopt_tpacket(int sock, int val)
133 int ret = setsockopt(sock, SOL_PACKET, PACKET_VERSION, &val, sizeof(val));
134 if (ret)
135 panic("Cannot set tpacketv2!\n");
138 static inline void set_sockopt_tpacket_v2(int sock)
140 __set_sockopt_tpacket(sock, TPACKET_V2);
143 #ifdef HAVE_TPACKET3
144 static inline void set_sockopt_tpacket_v3(int sock)
146 __set_sockopt_tpacket(sock, TPACKET_V3);
148 #endif
150 static inline int get_sockopt_tpacket(int sock)
152 int val, ret;
153 socklen_t len = sizeof(val);
155 ret = getsockopt(sock, SOL_PACKET, PACKET_VERSION, &val, &len);
156 if (ret)
157 panic("Cannot get tpacket version!\n");
159 return val;
162 extern void mmap_ring_generic(int sock, struct ring *ring);
163 extern void alloc_ring_frames_generic(struct ring *ring, int num, size_t size);
164 extern void bind_ring_generic(int sock, struct ring *ring, int ifindex, bool tx_only);
166 #endif /* RING_H */