summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTobias Klauser <tklauser@distanz.ch>2014-05-09 15:46:38 +0200
committerTobias Klauser <tklauser@distanz.ch>2014-05-09 15:46:38 +0200
commit9a40de458b3884d9f526172110392501f075bd87 (patch)
treeff621d506b408a74a0ccea32e5f3f97b5ad0a210
parent61a233a283171832a1ac5d580cf9487f9b7aec0a (diff)
ring: Merge common ring_{rx,tx} initialization into own function
Instead of having to perform the individual steps to initialize a ring and open coding them in multiple places, provide convenience functions to do all at once. This has the nice side effect of allowing to make most of these *_{rx,tx}_ring() functions static in their respective module. Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
-rw-r--r--astraceroute.c4
-rw-r--r--netsniff-ng.c35
-rw-r--r--ring_rx.c23
-rw-r--r--ring_rx.h9
-rw-r--r--ring_tx.c24
-rw-r--r--ring_tx.h9
-rw-r--r--trafgen.c13
7 files changed, 39 insertions, 78 deletions
diff --git a/astraceroute.c b/astraceroute.c
index 19c8017..e9d876d 100644
--- a/astraceroute.c
+++ b/astraceroute.c
@@ -44,7 +44,7 @@
#include "csum.h"
#include "sock.h"
#include "geoip.h"
-#include "ring_rx.h"
+#include "ring.h"
#include "built_in.h"
struct ctx {
@@ -899,7 +899,7 @@ static int main_trace(struct ctx *ctx)
inject_filter(ctx, fd_cap);
ifindex = device_ifindex(ctx->dev);
- bind_rx_ring(fd_cap, &dummy_ring, ifindex);
+ bind_ring_generic(fd_cap, &dummy_ring, ifindex, false);
if (ctx->totlen < af_ops[ctx->proto].min_len_tcp) {
ctx->totlen = af_ops[ctx->proto].min_len_tcp;
diff --git a/netsniff-ng.c b/netsniff-ng.c
index b9077a3..bee7f98 100644
--- a/netsniff-ng.c
+++ b/netsniff-ng.c
@@ -209,7 +209,6 @@ static void pcap_to_xmit(struct ctx *ctx)
panic("Error prepare reading pcap!\n");
}
- fmemset(&tx_ring, 0, sizeof(tx_ring));
fmemset(&bpf_ops, 0, sizeof(bpf_ops));
if (ctx->rfraw) {
@@ -222,20 +221,13 @@ static void pcap_to_xmit(struct ctx *ctx)
}
ifindex = device_ifindex(ctx->device_out);
-
size = ring_size(ctx->device_out, ctx->reserve_size);
bpf_parse_rules(ctx->filter, &bpf_ops, ctx->link_type);
if (ctx->dump_bpf)
bpf_dump_all(&bpf_ops);
- set_packet_loss_discard(tx_sock);
-
- setup_tx_ring_layout(tx_sock, &tx_ring, size, ctx->jumbo);
- create_tx_ring(tx_sock, &tx_ring, ctx->verbose);
- mmap_tx_ring(tx_sock, &tx_ring);
- alloc_tx_ring_frames(tx_sock, &tx_ring);
- bind_tx_ring(tx_sock, &tx_ring, ifindex);
+ ring_tx_setup(&tx_ring, tx_sock, size, ifindex, ctx->jumbo, ctx->verbose);
dissector_init_all(ctx->print_mode);
@@ -362,8 +354,6 @@ static void receive_to_xmit(struct ctx *ctx)
rx_sock = pf_socket();
tx_sock = pf_socket();
- fmemset(&tx_ring, 0, sizeof(tx_ring));
- fmemset(&rx_ring, 0, sizeof(rx_ring));
fmemset(&bpf_ops, 0, sizeof(bpf_ops));
ifindex_in = device_ifindex(ctx->device_in);
@@ -379,19 +369,8 @@ static void receive_to_xmit(struct ctx *ctx)
bpf_dump_all(&bpf_ops);
bpf_attach_to_sock(rx_sock, &bpf_ops);
- setup_rx_ring_layout(rx_sock, &rx_ring, size_in, ctx->jumbo, false);
- create_rx_ring(rx_sock, &rx_ring, ctx->verbose);
- mmap_rx_ring(rx_sock, &rx_ring);
- alloc_rx_ring_frames(rx_sock, &rx_ring);
- bind_rx_ring(rx_sock, &rx_ring, ifindex_in);
- prepare_polling(rx_sock, &rx_poll);
-
- set_packet_loss_discard(tx_sock);
- setup_tx_ring_layout(tx_sock, &tx_ring, size_out, ctx->jumbo);
- create_tx_ring(tx_sock, &tx_ring, ctx->verbose);
- mmap_tx_ring(tx_sock, &tx_ring);
- alloc_tx_ring_frames(tx_sock, &tx_ring);
- bind_tx_ring(tx_sock, &tx_ring, ifindex_out);
+ ring_rx_setup(&rx_ring, rx_sock, size_in, ifindex_in, &rx_poll, false, ctx->jumbo, ctx->verbose);
+ ring_tx_setup(&tx_ring, tx_sock, size_out, ifindex_out, ctx->jumbo, ctx->verbose);
dissector_init_all(ctx->print_mode);
@@ -901,7 +880,6 @@ static void recv_only_or_dump(struct ctx *ctx)
ctx->link_type = LINKTYPE_IEEE802_11;
}
- fmemset(&rx_ring, 0, sizeof(rx_ring));
fmemset(&bpf_ops, 0, sizeof(bpf_ops));
ifindex = device_ifindex(ctx->device_in);
@@ -919,13 +897,8 @@ static void recv_only_or_dump(struct ctx *ctx)
if (ret == 0 && ctx->verbose)
printf("HW timestamping enabled\n");
- setup_rx_ring_layout(sock, &rx_ring, size, true, true);
- create_rx_ring(sock, &rx_ring, ctx->verbose);
- mmap_rx_ring(sock, &rx_ring);
- alloc_rx_ring_frames(sock, &rx_ring);
- bind_rx_ring(sock, &rx_ring, ifindex);
+ ring_rx_setup(&rx_ring, sock, size, ifindex, &rx_poll, true, true, ctx->verbose);
- prepare_polling(sock, &rx_poll);
dissector_init_all(ctx->print_mode);
if (ctx->cpu >= 0 && ifindex > 0) {
diff --git a/ring_rx.c b/ring_rx.c
index a63b126..7a3d21d 100644
--- a/ring_rx.c
+++ b/ring_rx.c
@@ -41,8 +41,8 @@ void destroy_rx_ring(int sock, struct ring *ring)
panic("Cannot destroy the RX_RING: %s!\n", strerror(errno));
}
-void setup_rx_ring_layout(int sock, struct ring *ring, size_t size,
- bool jumbo_support, bool v3)
+static void setup_rx_ring_layout(int sock, struct ring *ring, size_t size,
+ bool jumbo_support, bool v3)
{
fmemset(&ring->layout, 0, sizeof(ring->layout));
@@ -77,7 +77,7 @@ void setup_rx_ring_layout(int sock, struct ring *ring, size_t size,
ring_verify_layout(ring);
}
-void create_rx_ring(int sock, struct ring *ring, bool verbose)
+static void create_rx_ring(int sock, struct ring *ring, bool verbose)
{
int ret;
bool v3 = get_sockopt_tpacket(sock) == TPACKET_V3;
@@ -111,12 +111,7 @@ retry:
}
}
-void mmap_rx_ring(int sock, struct ring *ring)
-{
- mmap_ring_generic(sock, ring);
-}
-
-void alloc_rx_ring_frames(int sock, struct ring *ring)
+static void alloc_rx_ring_frames(int sock, struct ring *ring)
{
int num;
size_t size;
@@ -133,9 +128,17 @@ void alloc_rx_ring_frames(int sock, struct ring *ring)
alloc_ring_frames_generic(ring, num, size);
}
-void bind_rx_ring(int sock, struct ring *ring, int ifindex)
+void ring_rx_setup(struct ring *ring, int sock, size_t size, int ifindex,
+ struct pollfd *poll, bool v3, bool jumbo_support,
+ bool verbose)
{
+ fmemset(ring, 0, sizeof(*ring));
+ setup_rx_ring_layout(sock, ring, size, jumbo_support, v3);
+ create_rx_ring(sock, ring, verbose);
+ mmap_ring_generic(sock, ring);
+ alloc_rx_ring_frames(sock, ring);
bind_ring_generic(sock, ring, ifindex, false);
+ prepare_polling(sock, poll);
}
void sock_rx_net_stats(int sock, unsigned long seen)
diff --git a/ring_rx.h b/ring_rx.h
index 24bd806..02c65bc 100644
--- a/ring_rx.h
+++ b/ring_rx.h
@@ -11,13 +11,10 @@
#include "ring.h"
+extern void ring_rx_setup(struct ring *ring, int sock, size_t size, int ifindex,
+ struct pollfd *poll, bool v3, bool jumbo_support,
+ bool verbose);
extern void destroy_rx_ring(int sock, struct ring *ring);
-extern void create_rx_ring(int sock, struct ring *ring, bool verbose);
-extern void mmap_rx_ring(int sock, struct ring *ring);
-extern void alloc_rx_ring_frames(int sock, struct ring *ring);
-extern void bind_rx_ring(int sock, struct ring *ring, int ifindex);
-extern void setup_rx_ring_layout(int sock, struct ring *ring, size_t size,
- bool jumbo_support, bool v3);
extern void sock_rx_net_stats(int sock, unsigned long seen);
static inline int user_may_pull_from_rx(struct tpacket2_hdr *hdr)
diff --git a/ring_tx.c b/ring_tx.c
index 3d69752..27afe8b 100644
--- a/ring_tx.c
+++ b/ring_tx.c
@@ -20,7 +20,7 @@
#include "ring_tx.h"
#include "built_in.h"
-void set_packet_loss_discard(int sock)
+static void set_packet_loss_discard(int sock)
{
int ret, discard = 1;
ret = setsockopt(sock, SOL_PACKET, PACKET_LOSS, (void *) &discard,
@@ -45,8 +45,8 @@ void destroy_tx_ring(int sock, struct ring *ring)
xfree(ring->frames);
}
-void setup_tx_ring_layout(int sock, struct ring *ring, size_t size,
- bool jumbo_support)
+static void setup_tx_ring_layout(int sock, struct ring *ring, size_t size,
+ bool jumbo_support)
{
fmemset(&ring->layout, 0, sizeof(ring->layout));
@@ -68,7 +68,7 @@ void setup_tx_ring_layout(int sock, struct ring *ring, size_t size,
ring_verify_layout(ring);
}
-void create_tx_ring(int sock, struct ring *ring, bool verbose)
+static void create_tx_ring(int sock, struct ring *ring, bool verbose)
{
int ret;
retry:
@@ -95,18 +95,20 @@ retry:
}
}
-void mmap_tx_ring(int sock, struct ring *ring)
-{
- mmap_ring_generic(sock, ring);
-}
-
-void alloc_tx_ring_frames(int sock __maybe_unused, struct ring *ring)
+static void alloc_tx_ring_frames(int sock __maybe_unused, struct ring *ring)
{
alloc_ring_frames_generic(ring, ring->layout.tp_frame_nr,
ring->layout.tp_frame_size);
}
-void bind_tx_ring(int sock, struct ring *ring, int ifindex)
+void ring_tx_setup(struct ring *ring, int sock, size_t size, int ifindex,
+ bool jumbo_support, bool verbose)
{
+ fmemset(ring, 0, sizeof(*ring));
+ set_packet_loss_discard(sock);
+ setup_tx_ring_layout(sock, ring, size, jumbo_support);
+ create_tx_ring(sock, ring, verbose);
+ mmap_ring_generic(sock, ring);
+ alloc_tx_ring_frames(sock, ring);
bind_ring_generic(sock, ring, ifindex, true);
}
diff --git a/ring_tx.h b/ring_tx.h
index ba8407a..eaff414 100644
--- a/ring_tx.h
+++ b/ring_tx.h
@@ -14,14 +14,9 @@
/* Give userland 10 us time to push packets to the ring */
#define TX_KERNEL_PULL_INT 10
+void ring_tx_setup(struct ring *ring, int sock, size_t size, int ifindex,
+ bool jumbo_support, bool verbose);
extern void destroy_tx_ring(int sock, struct ring *ring);
-extern void create_tx_ring(int sock, struct ring *ring, bool verbose);
-extern void mmap_tx_ring(int sock, struct ring *ring);
-extern void alloc_tx_ring_frames(int sock, struct ring *ring);
-extern void bind_tx_ring(int sock, struct ring *ring, int ifindex);
-extern void setup_tx_ring_layout(int sock, struct ring *ring, size_t size,
- bool jumbo_support);
-extern void set_packet_loss_discard(int sock);
static inline int user_may_pull_from_tx(struct tpacket2_hdr *hdr)
{
diff --git a/trafgen.c b/trafgen.c
index 419988e..307d596 100644
--- a/trafgen.c
+++ b/trafgen.c
@@ -592,25 +592,16 @@ static void xmit_fastpath_or_die(struct ctx *ctx, int cpu, unsigned long orig_nu
uint8_t *out = NULL;
unsigned int it = 0;
unsigned long num = 1, i = 0;
- size_t size;
+ size_t size = ring_size(ctx->device, ctx->reserve_size);
struct ring tx_ring;
struct frame_map *hdr;
struct timeval start, end, diff;
struct packet_dyn *pktd;
unsigned long long tx_bytes = 0, tx_packets = 0;
- fmemset(&tx_ring, 0, sizeof(tx_ring));
-
- size = ring_size(ctx->device, ctx->reserve_size);
-
set_sock_prio(sock, 512);
- set_packet_loss_discard(sock);
- setup_tx_ring_layout(sock, &tx_ring, size, ctx->jumbo_support);
- create_tx_ring(sock, &tx_ring, ctx->verbose);
- mmap_tx_ring(sock, &tx_ring);
- alloc_tx_ring_frames(sock, &tx_ring);
- bind_tx_ring(sock, &tx_ring, ifindex);
+ ring_tx_setup(&tx_ring, sock, size, ifindex, ctx->jumbo_support, ctx->verbose);
drop_privileges(ctx->enforce, ctx->uid, ctx->gid);