curvetun: Fix issues detected by the Coverity scanner
[netsniff-ng.git] / ring.h
bloba195f742d80c933218de1ee222a0fdc1336fa345
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 #ifndef POLLRDNORM
30 # define POLLRDNORM 0x0040
31 #endif
33 union tpacket_uhdr {
34 struct tpacket_hdr *h1;
35 struct tpacket2_hdr *h2;
36 #ifdef HAVE_TPACKET3
37 struct tpacket3_hdr *h3;
38 #endif
39 void *raw;
42 #ifdef HAVE_TPACKET3
43 #define tpacket_uhdr(hdr, member, v3) \
44 ((v3) ? ((hdr).h3)->member : ((hdr).h2)->member)
45 #else
46 #define tpacket_uhdr(hdr, member, v3) \
47 (((hdr).h2)->member)
48 #endif /* HAVE_TPACKET3 */
50 static inline uint16_t tpacket_uhdr_vlan_tci(union tpacket_uhdr *hdr, bool v3)
52 #ifdef HAVE_TPACKET3
53 if (v3)
54 return hdr->h3->hv1.tp_vlan_tci;
55 #endif
56 return 0;
59 static inline uint16_t tpacket_uhdr_vlan_proto(union tpacket_uhdr *hdr, bool v3)
61 #if defined(HAVE_TPACKET3) && defined(TP_STATUS_VLAN_TPID_VALID)
62 if (v3)
63 return hdr->h3->hv1.tp_vlan_tpid;
64 #endif
65 return 0;
68 static inline bool tpacket_has_vlan_info(union tpacket_uhdr *hdr)
70 uint32_t valid = TP_STATUS_VLAN_VALID;
72 #ifdef TP_STATUS_VLAN_TPID_VALID
73 valid |= TP_STATUS_VLAN_TPID_VALID;
74 #endif
76 return tpacket_uhdr(*hdr, tp_status, true) & valid;
79 struct frame_map {
80 struct tpacket2_hdr tp_h __aligned_tpacket;
81 struct sockaddr_ll s_ll __align_tpacket(sizeof(struct tpacket2_hdr));
84 #ifdef HAVE_TPACKET3
85 struct block_desc {
86 uint32_t version;
87 uint32_t offset_to_priv;
88 struct tpacket_hdr_v1 h1;
90 #endif
92 struct ring {
93 struct iovec *frames;
94 uint8_t *mm_space;
95 size_t mm_len;
96 struct sockaddr_ll s_ll;
97 union {
98 struct tpacket_req layout;
99 #ifdef HAVE_TPACKET3
100 struct tpacket_req3 layout3;
101 #endif
102 uint8_t raw;
106 static inline void next_rnd_slot(unsigned int *it, struct ring *ring)
108 *it = rand() % ring->layout.tp_frame_nr;
111 static inline size_t ring_size(const char *ifname, size_t size)
113 if (size > 0)
114 return size;
117 * Device bitrate in bytes times two as ring size.
118 * Fallback => ~ 64,00 MB
119 * 10 MBit => ~ 2,38 MB
120 * 54 MBit => ~ 12,88 MB
121 * 100 MBit => ~ 23,84 MB
122 * 300 MBit => ~ 71,52 MB
123 * 1.000 MBit => ~ 238,42 MB
124 * 10.000 MBit => ~ 2.384.18 MB
126 size = device_bitrate(ifname);
127 size = (size * 1000000) / 8;
128 size = size * 2;
129 if (size == 0)
130 size = 1 << 26;
132 return round_up_cacheline(size);
135 static inline unsigned int ring_frame_size(struct ring *ring)
137 return ring->layout.tp_frame_size;
140 static inline void ring_verify_layout(struct ring *ring)
142 bug_on(ring->layout.tp_block_size < ring->layout.tp_frame_size);
143 bug_on((ring->layout.tp_block_size % ring->layout.tp_frame_size) != 0);
144 bug_on((ring->layout.tp_block_size % RUNTIME_PAGE_SIZE) != 0);
147 static inline void tpacket_hdr_clone(struct tpacket2_hdr *thdrd,
148 struct tpacket2_hdr *thdrs)
150 thdrd->tp_sec = thdrs->tp_sec;
151 thdrd->tp_nsec = thdrs->tp_nsec;
152 thdrd->tp_snaplen = thdrs->tp_snaplen;
153 thdrd->tp_len = thdrs->tp_len;
156 static inline void prepare_polling(int sock, struct pollfd *pfd)
158 memset(pfd, 0, sizeof(*pfd));
159 pfd->fd = sock;
160 pfd->revents = 0;
161 pfd->events = POLLIN | POLLRDNORM | POLLERR;
164 static inline void __set_sockopt_tpacket(int sock, int val)
166 int ret = setsockopt(sock, SOL_PACKET, PACKET_VERSION, &val, sizeof(val));
167 if (ret)
168 panic("Cannot set tpacketv2!\n");
171 static inline void set_sockopt_tpacket_v2(int sock)
173 __set_sockopt_tpacket(sock, TPACKET_V2);
176 #ifdef HAVE_TPACKET3
177 static inline void set_sockopt_tpacket_v3(int sock)
179 __set_sockopt_tpacket(sock, TPACKET_V3);
181 #else
182 static inline void set_sockopt_tpacket_v3(int sock __maybe_unused)
185 #endif
187 static inline int get_sockopt_tpacket(int sock)
189 int val, ret;
190 socklen_t len = sizeof(val);
192 ret = getsockopt(sock, SOL_PACKET, PACKET_VERSION, &val, &len);
193 if (ret)
194 panic("Cannot get tpacket version!\n");
196 return val;
199 extern void mmap_ring_generic(int sock, struct ring *ring);
200 extern void alloc_ring_frames_generic(struct ring *ring, int num, size_t size);
201 extern void bind_ring_generic(int sock, struct ring *ring, int ifindex, bool tx_only);
203 #endif /* RING_H */