ring: fix timestamping code
[netsniff-ng.git] / src / ring.h
blobee2f32d5290685c505b8b0208e2362fbf9b4ed05
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 <linux/socket.h>
20 #include <linux/sockios.h>
21 #include <sys/ioctl.h>
22 #include <string.h>
23 #include <poll.h>
24 #include <sys/poll.h>
26 #include "xsys.h"
27 #include "built_in.h"
28 #include "mtrand.h"
29 #include "xstring.h"
30 #include "die.h"
32 #ifndef PACKET_FANOUT
33 # define PACKET_FANOUT 18
34 # define PACKET_FANOUT_POLICY_HASH 0
35 # define PACKET_FANOUT_POLICY_LB 1
36 # define PACKET_FANOUT_POLICY_DEFAULT PACKET_FANOUT_HASH
37 #endif
39 struct frame_map {
40 struct tpacket_hdr tp_h __aligned_tpacket;
41 struct sockaddr_ll s_ll __aligned_tpacket;
44 struct ring {
45 struct iovec *frames __cacheline_aligned;
46 uint8_t *mm_space __cacheline_aligned;
47 size_t mm_len;
48 struct tpacket_req layout;
49 struct sockaddr_ll s_ll;
52 static inline void next_slot(unsigned int *it, struct ring *ring)
54 (*it)++;
55 atomic_cmp_swp(it, ring->layout.tp_frame_nr, 0);
58 static inline void next_slot_prerd(unsigned int *it, struct ring *ring)
60 (*it)++;
61 atomic_cmp_swp(it, ring->layout.tp_frame_nr, 0);
62 prefetch_rd_hi(ring->frames[*it].iov_base);
65 static inline void next_slot_prewr(unsigned int *it, struct ring *ring)
67 (*it)++;
68 atomic_cmp_swp(it, ring->layout.tp_frame_nr, 0);
69 prefetch_wr_hi(ring->frames[*it].iov_base);
72 static inline void next_rnd_slot(unsigned int *it, struct ring *ring)
74 *it = mt_rand_int32() % ring->layout.tp_frame_nr;
77 #define RING_SIZE_FALLBACK (1 << 26)
79 static inline unsigned int ring_size(char *ifname, unsigned int size)
81 if (size > 0)
82 return size;
85 * Device bitrate in bytes times two as ring size.
86 * Fallback => ~ 64,00 MB
87 * 10 MBit => ~ 2,38 MB
88 * 54 MBit => ~ 12,88 MB
89 * 100 MBit => ~ 23,84 MB
90 * 300 MBit => ~ 71,52 MB
91 * 1.000 MBit => ~ 238,42 MB
92 * 10.000 MBit => ~ 2.384.18 MB
94 size = device_bitrate(ifname);
95 size = (size * 1000000) / 8;
96 size = size * 2;
97 if (size == 0)
98 size = RING_SIZE_FALLBACK;
99 return round_up_cacheline(size);
102 enum ring_mode {
103 RING_MODE_EGRESS,
104 RING_MODE_INGRESS,
107 static inline unsigned int ring_frame_size(struct ring *ring)
109 return ring->layout.tp_frame_size;
112 static inline void tpacket_hdr_clone(struct tpacket_hdr *thdrd,
113 struct tpacket_hdr *thdrs)
115 thdrd->tp_sec = thdrs->tp_sec;
116 thdrd->tp_usec = thdrs->tp_usec;
117 thdrd->tp_snaplen = thdrs->tp_snaplen;
118 thdrd->tp_len = thdrs->tp_len;
121 #ifndef POLLRDNORM
122 # define POLLRDNORM 0x0040
123 #endif
124 #ifndef POLLWRNORM
125 # define POLLWRNORM 0x0100
126 #endif
127 #ifndef POLLRDHUP
128 # define POLLRDHUP 0x2000
129 #endif
131 #define POLL_NEXT_PKT 0
132 #define POLL_MOVE_OUT 1
134 static inline void prepare_polling(int sock, struct pollfd *pfd)
136 memset(pfd, 0, sizeof(*pfd));
137 pfd->fd = sock;
138 pfd->revents = 0;
139 pfd->events = POLLIN | POLLRDNORM | POLLERR;
142 static inline void set_sockopt_fanout(int sock, unsigned int fanout_id,
143 unsigned int fanout_type)
145 unsigned int fanout_arg = (fanout_id | (fanout_type << 16));
146 int ret = setsockopt(sock, SOL_PACKET, PACKET_FANOUT, &fanout_arg,
147 sizeof(fanout_arg));
148 if (ret)
149 panic("No packet fanout support!\n");
152 #if !defined (SIOCSHWTSTAMP) || !defined (PACKET_TIMESTAMP)
153 static inline void set_sockopt_hwtimestamp(int sock, const char *dev)
155 return;
157 #else /* !defined (SIOCSHWTSTAMP) || !defined (PACKET_TIMESTAMP) */
158 # include <linux/net_tstamp.h>
160 static inline void set_sockopt_hwtimestamp(int sock, const char *dev)
162 int timesource, ret;
163 struct hwtstamp_config hwconfig;
164 struct ifreq ifr;
166 memset(&hwconfig, 0, sizeof(hwconfig));
167 hwconfig.tx_type = HWTSTAMP_TX_ON;
168 hwconfig.rx_filter = HWTSTAMP_FILTER_ALL;
170 memset(&ifr, 0, sizeof(ifr));
171 strlcpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
172 ifr.ifr_data = &hwconfig;
174 ret = ioctl(sock, SIOCSHWTSTAMP, &ifr);
175 if (ret < 0)
176 return;
178 timesource = SOF_TIMESTAMPING_RAW_HARDWARE;
180 ret = setsockopt(sock, SOL_PACKET, PACKET_TIMESTAMP, &timesource,
181 sizeof(timesource));
182 if (ret)
183 panic("Cannot set timestamping!\n");
185 #endif /* !defined (SIOCSHWTSTAMP) || !defined (PACKET_TIMESTAMP) */
186 #endif /* RING_H */