Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/gnumaniacs/netsniff-ng
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus Amend committed May 21, 2012
2 parents 5ea3720 + 1f6a9ab commit 664d740
Show file tree
Hide file tree
Showing 11 changed files with 162 additions and 77 deletions.
4 changes: 4 additions & 0 deletions src/built_in.h
Expand Up @@ -218,6 +218,10 @@
# define bug assert(0)
#endif

#define PAGE_SIZE (getpagesize())
#define PAGE_MASK (~(PAGE_SIZE - 1))
#define PAGE_ALIGN(addr) (((addr) + PAGE_SIZE - 1) & PAGE_MASK)

#if __BYTE_ORDER == __LITTLE_ENDIAN
static inline uint64_t htonll(uint64_t x)
{
Expand Down
54 changes: 49 additions & 5 deletions src/netsniff-ng.c
Expand Up @@ -265,7 +265,7 @@ static unsigned long interval = TX_KERNEL_PULL_INT;
static struct itimerval itimer;
static volatile bool next_dump = false;

static const char *short_options = "d:i:o:rf:MJt:S:k:n:b:B:HQmcsqXlvhF:R";
static const char *short_options = "d:i:o:rf:MJt:S:k:n:b:B:HQmcsqXlvhF:Rg";

static struct option long_options[] = {
{"dev", required_argument, 0, 'd'},
Expand All @@ -274,6 +274,7 @@ static struct option long_options[] = {
{"rand", no_argument, 0, 'r'},
{"rfraw", no_argument, 0, 'R'},
{"mmap", no_argument, 0, 'm'},
{"sg", no_argument, 0, 'g'},
{"clrw", no_argument, 0, 'c'},
{"jumbo-support", no_argument, 0, 'J'},
{"filter", required_argument, 0, 'f'},
Expand Down Expand Up @@ -341,6 +342,7 @@ static void enter_mode_pcap_to_tx(struct mode *mode)
struct tx_stats stats;
uint8_t *out = NULL;
unsigned long trunced = 0;
struct timeval start, end, diff;

if (!device_up_and_running(mode->device_out))
panic("Device not up and running!\n");
Expand Down Expand Up @@ -408,9 +410,14 @@ static void enter_mode_pcap_to_tx(struct mode *mode)
printf("MD: TX %luus %s ", interval, pcap_ops[mode->pcap]->name);
if (mode->rfraw)
printf("802.11 raw via %s ", mode->device_out);
#ifdef _LARGEFILE64_SOURCE
printf("lf64 ");
#endif
ioprio_print();
printf("\n");

gettimeofday(&start, NULL);

while (likely(sigint == 0)) {
while (user_may_pull_from_tx(tx_ring.frames[it].iov_base)) {
struct pcap_pkthdr phdr;
Expand Down Expand Up @@ -453,11 +460,15 @@ static void enter_mode_pcap_to_tx(struct mode *mode)
}
}
out:
gettimeofday(&end, NULL);
diff = tv_subtract(end, start);

fflush(stdout);
printf("\n");
printf("\r%12lu frames outgoing\n", stats.tx_packets);
printf("\r%12lu frames truncated (larger than frame)\n", trunced);
printf("\r%12lu bytes outgoing\n", stats.tx_bytes);
printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);

bpf_release(&bpf_ops);
dissector_cleanup_all();
Expand Down Expand Up @@ -624,6 +635,7 @@ static void enter_mode_read_pcap(struct mode *mode)
uint8_t *out;
size_t out_len;
unsigned long trunced = 0;
struct timeval start, end, diff;

if (!pcap_ops[mode->pcap])
panic("pcap group not supported!\n");
Expand All @@ -650,6 +662,9 @@ static void enter_mode_read_pcap(struct mode *mode)
printf("BPF:\n");
bpf_dump_all(&bpf_ops);
printf("MD: RD %s ", pcap_ops[mode->pcap]->name);
#ifdef _LARGEFILE64_SOURCE
printf("lf64 ");
#endif
ioprio_print();
printf("\n");

Expand All @@ -658,6 +673,8 @@ static void enter_mode_read_pcap(struct mode *mode)
O_TRUNC | O_LARGEFILE, S_IRUSR | S_IWUSR);
}

gettimeofday(&start, NULL);

while (likely(sigint == 0)) {
do {
memset(&phdr, 0, sizeof(phdr));
Expand Down Expand Up @@ -719,11 +736,15 @@ static void enter_mode_read_pcap(struct mode *mode)
}
}
out:
gettimeofday(&end, NULL);
diff = tv_subtract(end, start);

fflush(stdout);
printf("\n");
printf("\r%12lu frames outgoing\n", stats.tx_packets);
printf("\r%12lu frames truncated (larger than mtu)\n", trunced);
printf("\r%12lu bytes outgoing\n", stats.tx_bytes);
printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec, diff.tv_usec);

xfree(out);

Expand Down Expand Up @@ -847,6 +868,7 @@ static void enter_mode_rx_only_or_dump(struct mode *mode)
struct pollfd rx_poll;
struct frame_map *hdr;
struct sock_fprog bpf_ops;
struct timeval start, end, diff;

if (!device_up_and_running(mode->device_in))
panic("Device not up and running!\n");
Expand Down Expand Up @@ -916,9 +938,14 @@ static void enter_mode_rx_only_or_dump(struct mode *mode)
printf("MD: RX %s ", mode->dump ? pcap_ops[mode->pcap]->name : "");
if (mode->rfraw)
printf("802.11 raw via %s ", mode->device_in);
#ifdef _LARGEFILE64_SOURCE
printf("lf64 ");
#endif
ioprio_print();
printf("\n");

gettimeofday(&start, NULL);

while (likely(sigint == 0)) {
while (user_may_pull_from_rx(rx_ring.frames[it].iov_base)) {
hdr = rx_ring.frames[it].iov_base;
Expand Down Expand Up @@ -979,9 +1006,14 @@ static void enter_mode_rx_only_or_dump(struct mode *mode)
poll_error_maybe_die(sock, &rx_poll);
}

if (!(mode->dump_dir && mode->print_mode == FNTTYPE_PRINT_NONE))
gettimeofday(&end, NULL);
diff = tv_subtract(end, start);

if (!(mode->dump_dir && mode->print_mode == FNTTYPE_PRINT_NONE)) {
sock_print_net_stats(sock, skipped);
else {
printf("\r%12lu sec, %lu usec in total\n", diff.tv_sec,
diff.tv_usec);
} else {
printf("\n\n");
fflush(stdout);
}
Expand Down Expand Up @@ -1035,7 +1067,7 @@ static void help(void)
printf(" -r|--rand Randomize packet forwarding order\n");
printf(" -M|--no-promisc No promiscuous mode for netdev\n");
printf(" -m|--mmap Mmap pcap file i.e., for replaying\n");
printf(" Default: scatter-gather I/O\n");
printf(" -g|--sg Scatter/gather pcap file I/O\n");
printf(" -c|--clrw Use slower read(2)/write(2) I/O\n");
printf(" -S|--ring-size <size> Manually set ring size to <size>:\n");
printf(" mmap space in KB/MB/GB, e.g. \'10MB\'\n");
Expand Down Expand Up @@ -1097,7 +1129,7 @@ static void header(void)

int main(int argc, char **argv)
{
int c, i, j, opt_index;
int c, i, j, opt_index, ops_touched = 0;
char *ptr;
bool prio_high = false;
struct mode mode;
Expand Down Expand Up @@ -1190,9 +1222,15 @@ int main(int argc, char **argv)
break;
case 'c':
mode.pcap = PCAP_OPS_RW;
ops_touched = 1;
break;
case 'm':
mode.pcap = PCAP_OPS_MMAP;
ops_touched = 1;
break;
case 'g':
mode.pcap = PCAP_OPS_SG;
ops_touched = 1;
break;
case 'Q':
mode.cpu = CPU_NOTOUCH;
Expand Down Expand Up @@ -1281,13 +1319,19 @@ int main(int argc, char **argv)
mode.dump = 1;
register_signal_f(SIGALRM, timer_next_dump, SA_SIGINFO);
enter_mode = enter_mode_rx_only_or_dump;
if (!ops_touched)
mode.pcap = PCAP_OPS_SG;
}
} else {
if (mode.device_out && device_mtu(mode.device_out)) {
register_signal_f(SIGALRM, timer_elapsed, SA_SIGINFO);
enter_mode = enter_mode_pcap_to_tx;
if (!ops_touched)
mode.pcap = PCAP_OPS_MMAP;
} else {
enter_mode = enter_mode_read_pcap;
if (!ops_touched)
mode.pcap = PCAP_OPS_SG;
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/netsniff-ng/CMakeLists.txt
Expand Up @@ -57,11 +57,9 @@ IF (CMAKE_HAVE_PTHREAD_CREATE)
-D__WITH_PROTOS)
POD2MAN(${CMAKE_SOURCE_DIR}/netsniff-ng.c netsniff-ng 8)
TARGET_LINK_LIBRARIES(netsniff-ng ${CMAKE_THREAD_LIBS_INIT} -lnl)
# FILE(GLOB rules_files "../examples/bpfc/*.bpf")
INSTALL(FILES ${rules_files} DESTINATION ${CONFIG_INSTALL_PATH}/rules/)
INSTALL(FILES ../conf/ether.conf ../conf/tcp.conf ../conf/udp.conf ../conf/oui.conf DESTINATION ${CONFIG_INSTALL_PATH})
INSTALL(TARGETS ${PROJECT_NAME} DESTINATION ${EXECUTABLE_INSTALL_PATH})
ELSE(CMAKE_HAVE_PTHREAD_CREATE)
MESSAGE("pthread is missing on target. Skipping ${PROJECT_NAME} build.")
ENDIF(CMAKE_HAVE_PTHREAD_CREATE)

18 changes: 7 additions & 11 deletions src/pcap_mmap.c
Expand Up @@ -21,10 +21,6 @@

#define DEFAULT_SLOTS 1000

#define PAGE_SIZE (getpagesize())
#define PAGE_MASK (~(PAGE_SIZE - 1))
#define PAGE_ALIGN(addr) (((addr) + PAGE_SIZE - 1) & PAGE_MASK)

static struct spinlock lock;
static off_t map_size = 0;
static char *pstart, *pcurr;
Expand All @@ -33,7 +29,6 @@ static int jumbo_frames = 0;
static inline off_t get_map_size(void)
{
int allocsz = jumbo_frames ? 16 : 3;

return PAGE_ALIGN(sizeof(struct pcap_filehdr) +
(PAGE_SIZE * allocsz) * DEFAULT_SLOTS);
}
Expand Down Expand Up @@ -74,6 +69,7 @@ static int pcap_mmap_push_file_header(int fd, uint32_t linktype)
static int pcap_mmap_prepare_writing_pcap(int fd)
{
int ret;
off_t pos;
struct stat sb;

set_ioprio_be();
Expand All @@ -88,9 +84,8 @@ static int pcap_mmap_prepare_writing_pcap(int fd)
if (!S_ISREG (sb.st_mode))
panic("pcap dump file is not a regular file!\n");

/* Expand file buffer, so that mmap can be done. */
ret = lseek(fd, map_size, SEEK_SET);
if (ret < 0)
pos = lseek(fd, map_size, SEEK_SET);
if (pos < 0)
panic("Cannot lseek pcap file!\n");

ret = write_or_die(fd, "", 1);
Expand All @@ -117,17 +112,18 @@ static ssize_t pcap_mmap_write_pcap_pkt(int fd, struct pcap_pkthdr *hdr,
uint8_t *packet, size_t len)
{
int ret;
off_t pos;

spinlock_lock(&lock);

if ((off_t) (pcurr - pstart) + sizeof(*hdr) + len > map_size) {
off_t map_size_old = map_size;
off_t offset = (pcurr - pstart);

map_size = PAGE_ALIGN(map_size_old * 3 / 2);
map_size = PAGE_ALIGN(map_size_old * 10 / 8);

ret = lseek(fd, map_size, SEEK_SET);
if (ret < 0)
pos = lseek(fd, map_size, SEEK_SET);
if (pos < 0)
panic("Cannot lseek pcap file!\n");

ret = write_or_die(fd, "", 1);
Expand Down
8 changes: 2 additions & 6 deletions src/pcap_sg.c
Expand Up @@ -19,12 +19,8 @@
#include "locking.h"
#include "built_in.h"

#define PAGE_SIZE (getpagesize())
#define PAGE_MASK (~(PAGE_SIZE - 1))
#define PAGE_ALIGN(addr) (((addr) + PAGE_SIZE - 1) & PAGE_MASK)

#define IOVSIZ 1000
#define ALLSIZ (PAGE_SIZE * 3)
#define IOVSIZ 1000
#define ALLSIZ (PAGE_SIZE * 3)
#define ALLSIZ_2K (PAGE_SIZE * 3) // 12K max
#define ALLSIZ_JUMBO (PAGE_SIZE * 16) // 64K max

Expand Down
12 changes: 8 additions & 4 deletions src/proto_ethernet.c
Expand Up @@ -18,6 +18,7 @@

static void ethernet(struct pkt_buff *pkt)
{
char *type;
uint8_t *src_mac, *dst_mac;
struct ethhdr *eth = (struct ethhdr *) pkt_pull(pkt, sizeof(*eth));

Expand All @@ -26,18 +27,21 @@ static void ethernet(struct pkt_buff *pkt)

src_mac = eth->h_source;
dst_mac = eth->h_dest;

tprintf(" [ Eth ");
tprintf("MAC (%.2x:%.2x:%.2x:%.2x:%.2x:%.2x => ",
src_mac[0], src_mac[1], src_mac[2],
src_mac[3], src_mac[4], src_mac[5]);
tprintf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x), ",
dst_mac[0], dst_mac[1], dst_mac[2],
dst_mac[3], dst_mac[4], dst_mac[5]);
tprintf("Proto (0x%.4x, %s%s%s)",
ntohs(eth->h_proto), colorize_start(bold),
lookup_ether_type(ntohs(eth->h_proto)), colorize_end());
tprintf(" ]\n");
tprintf("Proto (0x%.4x", ntohs(eth->h_proto));

type = lookup_ether_type(ntohs(eth->h_proto));
if (type)
tprintf(", %s%s%s", colorize_start(bold), type, colorize_end());

tprintf(") ]\n");
tprintf(" [ Vendor ");
tprintf("(%s => %s)",
lookup_vendor((src_mac[0] << 16) | (src_mac[1] << 8) |
Expand Down
4 changes: 2 additions & 2 deletions src/proto_none.c
Expand Up @@ -44,7 +44,7 @@ static void _ascii(uint8_t *ptr, size_t len)

tprintf(" [ chr ");
for (; ptr && len-- > 0; ptr++)
tprintf(" %c ", isprint(*ptr) ? *ptr : '.');
tprintf("%c", isprint(*ptr) ? *ptr : '.');
tprintf(" ]\n");
}

Expand All @@ -65,8 +65,8 @@ void hex_ascii(struct pkt_buff *pkt)
uint8_t *ptr = pkt_pull(pkt, len);

if (len) {
_hex(ptr, len);
_ascii(ptr, len);
_hex(ptr, len);
}

tprintf("\n");
Expand Down

0 comments on commit 664d740

Please sign in to comment.