ring: built_in: fix if cacheline_aligned is not defined
[netsniff-ng.git] / src / ring.h
blob1186491e081014551501c8016cda54e450f00fb6
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>
20 #include "xsys.h"
21 #include "built_in.h"
22 #include "mtrand.h"
24 #define RING_SIZE_FALLBACK (1 << 26)
26 struct frame_map {
27 struct tpacket_hdr tp_h __aligned_tpacket;
28 struct sockaddr_ll s_ll __aligned_tpacket;
31 struct ring {
32 struct iovec *frames;
33 struct tpacket_req layout;
34 struct sockaddr_ll s_ll;
35 uint8_t *mm_space;
36 size_t mm_len;
37 } __cacheline_aligned;
39 static inline void next_slot(unsigned int *it, struct ring *ring)
41 *it = (*it + 1);
42 if (*it >= ring->layout.tp_frame_nr)
43 *it = 0;
46 static inline void next_rnd_slot(unsigned int *it, struct ring *ring)
48 *it = mt_rand_int32() % ring->layout.tp_frame_nr;
51 static inline unsigned int ring_size(char *ifname, unsigned int size)
53 if (size > 0)
54 return size;
57 * Device bitrate in bytes times two as ring size.
58 * Fallback => ~ 64,00 MB
59 * 10 MBit => ~ 2,38 MB
60 * 54 MBit => ~ 12,88 MB
61 * 100 MBit => ~ 23,84 MB
62 * 300 MBit => ~ 71,52 MB
63 * 1.000 MBit => ~ 238,42 MB
64 * 10.000 MBit => ~ 2.384.18 MB
66 size = device_bitrate(ifname);
67 size = (size * 1000000) / 8;
68 size = size * 2;
69 if (size == 0)
70 size = RING_SIZE_FALLBACK;
72 return size;
75 enum ring_mode {
76 RING_MODE_EGRESS,
77 RING_MODE_INGRESS,
80 static inline unsigned int ring_frame_size(struct ring *ring)
82 return ring->layout.tp_frame_size;
85 static inline void tpacket_hdr_clone(struct tpacket_hdr *thdr_d,
86 struct tpacket_hdr *thdr_s)
88 thdr_d->tp_sec = thdr_s->tp_sec;
89 thdr_d->tp_usec = thdr_s->tp_usec;
90 thdr_d->tp_snaplen = thdr_s->tp_snaplen;
91 thdr_d->tp_len = thdr_s->tp_len;
94 #endif /* RING_H */