html: some further minor tweaks on the index page
[netsniff-ng.git] / src / ring.h
blob0b206f1c7ea6467282ee8f3d3c3006cfb6e00206
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 "tprintf.h"
22 #include "dissector.h"
23 #include "compiler.h"
24 #include "mtrand.h"
26 #define RING_SIZE_FALLBACK (1 << 26)
28 static char *packet_types[]={
29 "<", /* Incoming */
30 "B", /* Broadcast */
31 "M", /* Multicast */
32 "P", /* Promisc */
33 ">", /* Outgoing */
34 "?", /* Unknown */
37 struct frame_map {
38 struct tpacket_hdr tp_h __attribute__((aligned(TPACKET_ALIGNMENT)));
39 struct sockaddr_ll s_ll __attribute__((aligned(TPACKET_ALIGNMENT)));
42 struct ring {
43 struct iovec *frames;
44 struct tpacket_req layout;
45 struct sockaddr_ll s_ll;
46 uint8_t *mm_space;
47 size_t mm_len;
48 } __cacheline_aligned;
50 static inline void next_slot(unsigned int *it, struct ring *ring)
52 *it = (*it + 1);
53 if (*it >= ring->layout.tp_frame_nr)
54 *it = 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 static inline unsigned int ring_size(char *ifname, unsigned int size)
64 if (size > 0)
65 return size;
68 * Device bitrate in bytes times two as ring size.
69 * Fallback => ~ 64,00 MB
70 * 10 MBit => ~ 2,38 MB
71 * 54 MBit => ~ 12,88 MB
72 * 100 MBit => ~ 23,84 MB
73 * 300 MBit => ~ 71,52 MB
74 * 1.000 MBit => ~ 238,42 MB
75 * 10.000 MBit => ~ 2.384.18 MB
77 size = device_bitrate(ifname);
78 size = (size * 1000000) / 8;
79 size = size * 2;
80 if (size == 0)
81 size = RING_SIZE_FALLBACK;
83 return size;
86 enum ring_mode {
87 RING_MODE_EGRESS,
88 RING_MODE_INGRESS,
91 static inline void show_frame_hdr(struct frame_map *hdr, int mode,
92 enum ring_mode rmode)
94 if (mode == FNTTYPE_PRINT_NONE)
95 return;
96 switch (mode) {
97 case FNTTYPE_PRINT_PAAC:
98 case FNTTYPE_PRINT_NOPA:
99 case FNTTYPE_PRINT_HEX1:
100 case FNTTYPE_PRINT_HEX2:
101 case FNTTYPE_PRINT_NORM:
102 default:
103 if (rmode == RING_MODE_INGRESS) {
104 tprintf("%s %u %u %u.%06u\n",
105 packet_types[hdr->s_ll.sll_pkttype],
106 hdr->s_ll.sll_ifindex, hdr->tp_h.tp_len,
107 hdr->tp_h.tp_sec, hdr->tp_h.tp_usec);
108 } else {
109 tprintf("%u %u.%06u\n", hdr->tp_h.tp_len,
110 hdr->tp_h.tp_sec, hdr->tp_h.tp_usec);
112 break;
113 case FNTTYPE_PRINT_LESS:
114 if (rmode == RING_MODE_INGRESS) {
115 tprintf("%s %u %u",
116 packet_types[hdr->s_ll.sll_pkttype],
117 hdr->s_ll.sll_ifindex, hdr->tp_h.tp_len);
118 } else {
119 tprintf("%u ", hdr->tp_h.tp_len);
121 break;
125 static inline unsigned int ring_frame_size(struct ring *ring)
127 return ring->layout.tp_frame_size;
130 static inline void tpacket_hdr_clone(struct tpacket_hdr *thdr_d,
131 struct tpacket_hdr *thdr_s)
133 thdr_d->tp_sec = thdr_s->tp_sec;
134 thdr_d->tp_usec = thdr_s->tp_usec;
135 thdr_d->tp_snaplen = thdr_s->tp_snaplen;
136 thdr_d->tp_len = thdr_s->tp_len;
139 #endif /* RING_H */