ring: use xzmalloc_aligned
[netsniff-ng.git] / ring.c
blobb5c626ca9b0a06853a307ccf5d4f11fc9da1c546
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2009, 2010 Daniel Borkmann.
4 * Copyright 2014, 2015 Tobias Klauser
5 * Subject to the GPL, version 2.
6 */
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h>
12 #include <sys/mman.h>
13 #include <sys/types.h>
14 #include <sys/socket.h>
15 #include <arpa/inet.h>
16 #include <linux/if_ether.h>
18 #include "xmalloc.h"
19 #include "die.h"
20 #include "ring.h"
21 #include "built_in.h"
23 void setup_ring_layout_generic(struct ring *ring, size_t size,
24 bool jumbo_support)
26 fmemset(&ring->layout, 0, sizeof(ring->layout));
28 ring->layout.tp_block_size = (jumbo_support ?
29 RUNTIME_PAGE_SIZE << 4 :
30 RUNTIME_PAGE_SIZE << 2);
32 ring->layout.tp_frame_size = (jumbo_support ?
33 TPACKET_ALIGNMENT << 12 :
34 TPACKET_ALIGNMENT << 7);
36 ring->layout.tp_block_nr = size / ring->layout.tp_block_size;
37 ring->layout.tp_frame_nr = size / ring->layout.tp_frame_size;
40 void mmap_ring_generic(int sock, struct ring *ring)
42 ring->mm_space = mmap(NULL, ring->mm_len, PROT_READ | PROT_WRITE,
43 MAP_SHARED | MAP_LOCKED | MAP_POPULATE, sock, 0);
44 if (ring->mm_space == MAP_FAILED)
45 panic("Cannot mmap {TX,RX}_RING!\n");
48 void alloc_ring_frames_generic(struct ring *ring, size_t num, size_t size)
50 size_t i, len = num * sizeof(*ring->frames);
52 ring->frames = xzmalloc_aligned(len, CO_CACHE_LINE_SIZE);
54 for (i = 0; i < num; ++i) {
55 ring->frames[i].iov_len = size;
56 ring->frames[i].iov_base = ring->mm_space + (i * size);
60 void bind_ring_generic(int sock, struct ring *ring, int ifindex, bool tx_only)
62 int ret;
64 /* The {TX,RX}_RING registers itself to the networking stack with
65 * dev_add_pack(), so we have one single RX_RING for all devs
66 * otherwise you'll get the packet twice.
68 fmemset(&ring->s_ll, 0, sizeof(ring->s_ll));
70 ring->s_ll.sll_family = AF_PACKET;
71 ring->s_ll.sll_ifindex = ifindex;
72 ring->s_ll.sll_protocol = tx_only ? 0 : htons(ETH_P_ALL);
74 ret = bind(sock, (struct sockaddr *) &ring->s_ll, sizeof(ring->s_ll));
75 if (ret < 0)
76 panic("Cannot bind {TX,RX}_RING!\n");