ring: fix sll packet alignment
[netsniff-ng.git] / src / ring.h
blob17ef8cfd3a13de1f3e2bf3ec024d0e7ac928e4c0
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 "xutils.h"
27 #include "built_in.h"
28 #include "mtrand.h"
29 #include "die.h"
31 #ifndef PACKET_FANOUT
32 # define PACKET_FANOUT 18
33 # define PACKET_FANOUT_POLICY_HASH 0
34 # define PACKET_FANOUT_POLICY_LB 1
35 # define PACKET_FANOUT_POLICY_DEFAULT PACKET_FANOUT_HASH
36 #endif
38 struct frame_map {
39 struct tpacket2_hdr tp_h __aligned_tpacket;
40 struct sockaddr_ll s_ll __align_tpacket(sizeof(struct tpacket2_hdr));
43 struct ring {
44 struct iovec *frames __cacheline_aligned;
45 uint8_t *mm_space;
46 size_t mm_len;
47 struct tpacket_req layout;
48 struct sockaddr_ll s_ll;
51 static inline void next_slot(unsigned int *it, struct ring *ring)
53 (*it)++;
54 atomic_cmp_swp(it, ring->layout.tp_frame_nr, 0);
57 static inline void next_rnd_slot(unsigned int *it, struct ring *ring)
59 *it = mt_rand_int32() % ring->layout.tp_frame_nr;
62 #define RING_SIZE_FALLBACK (1 << 26)
64 static inline unsigned int ring_size(char *ifname, unsigned int size)
66 if (size > 0)
67 return size;
70 * Device bitrate in bytes times two as ring size.
71 * Fallback => ~ 64,00 MB
72 * 10 MBit => ~ 2,38 MB
73 * 54 MBit => ~ 12,88 MB
74 * 100 MBit => ~ 23,84 MB
75 * 300 MBit => ~ 71,52 MB
76 * 1.000 MBit => ~ 238,42 MB
77 * 10.000 MBit => ~ 2.384.18 MB
79 size = device_bitrate(ifname);
80 size = (size * 1000000) / 8;
81 size = size * 2;
82 if (size == 0)
83 size = RING_SIZE_FALLBACK;
85 return round_up_cacheline(size);
88 enum ring_mode {
89 RING_MODE_EGRESS,
90 RING_MODE_INGRESS,
93 static inline unsigned int ring_frame_size(struct ring *ring)
95 return ring->layout.tp_frame_size;
98 static inline void tpacket_hdr_clone(struct tpacket2_hdr *thdrd,
99 struct tpacket2_hdr *thdrs)
101 thdrd->tp_sec = thdrs->tp_sec;
102 thdrd->tp_nsec = thdrs->tp_nsec;
103 thdrd->tp_snaplen = thdrs->tp_snaplen;
104 thdrd->tp_len = thdrs->tp_len;
107 #ifndef POLLRDNORM
108 # define POLLRDNORM 0x0040
109 #endif
110 #ifndef POLLWRNORM
111 # define POLLWRNORM 0x0100
112 #endif
113 #ifndef POLLRDHUP
114 # define POLLRDHUP 0x2000
115 #endif
117 #define POLL_NEXT_PKT 0
118 #define POLL_MOVE_OUT 1
120 static inline void prepare_polling(int sock, struct pollfd *pfd)
122 memset(pfd, 0, sizeof(*pfd));
123 pfd->fd = sock;
124 pfd->revents = 0;
125 pfd->events = POLLIN | POLLRDNORM | POLLERR;
128 static inline void set_sockopt_fanout(int sock, unsigned int fanout_id,
129 unsigned int fanout_type)
131 unsigned int fanout_arg = (fanout_id | (fanout_type << 16));
132 int ret = setsockopt(sock, SOL_PACKET, PACKET_FANOUT, &fanout_arg,
133 sizeof(fanout_arg));
134 if (ret)
135 panic("No packet fanout support!\n");
138 static inline void set_sockopt_tpacket(int sock)
140 int ret, val = TPACKET_V2;
142 ret = setsockopt(sock, SOL_PACKET, PACKET_VERSION, &val, sizeof(val));
143 if (ret)
144 panic("Cannot set tpacketv2!\n");
147 #if defined(__WITH_HARDWARE_TIMESTAMPING)
148 # include <linux/net_tstamp.h>
150 static inline void set_sockopt_hwtimestamp(int sock, const char *dev)
152 int timesource, ret;
153 struct hwtstamp_config hwconfig;
154 struct ifreq ifr;
156 memset(&hwconfig, 0, sizeof(hwconfig));
157 hwconfig.tx_type = HWTSTAMP_TX_ON;
158 hwconfig.rx_filter = HWTSTAMP_FILTER_ALL;
160 memset(&ifr, 0, sizeof(ifr));
161 strlcpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
162 ifr.ifr_data = &hwconfig;
164 ret = ioctl(sock, SIOCSHWTSTAMP, &ifr);
165 if (ret < 0)
166 return;
168 timesource = SOF_TIMESTAMPING_RAW_HARDWARE;
170 ret = setsockopt(sock, SOL_PACKET, PACKET_TIMESTAMP, &timesource,
171 sizeof(timesource));
172 if (ret)
173 panic("Cannot set timestamping!\n");
175 #else
176 static inline void set_sockopt_hwtimestamp(int sock, const char *dev)
178 return;
180 #endif /* defined(__WITH_HARDWARE_TIMESTAMPING) */
181 #endif /* RING_H */