netsniff-ng: remove set_sockopt_hwtimestamp call
[netsniff-ng.git] / pcap_sg.c
blob6902001c7528fa2c55d3e977b99728f22c7de795
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2011 - 2013 Daniel Borkmann.
4 * Subject to the GPL, version 2.
5 */
7 #include <stdio.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <fcntl.h>
11 #include <sys/uio.h>
12 #include <unistd.h>
14 #include "pcap_io.h"
15 #include "xmalloc.h"
16 #include "xio.h"
17 #include "xutils.h"
18 #include "built_in.h"
20 static struct iovec iov[1024] __cacheline_aligned;
21 static off_t iov_off_rd = 0, iov_slot = 0;
23 static ssize_t pcap_sg_write(int fd, pcap_pkthdr_t *phdr, enum pcap_type type,
24 const uint8_t *packet, size_t len)
26 ssize_t ret, hdrsize = pcap_get_hdr_length(phdr, type);
28 if (unlikely(iov_slot == array_size(iov))) {
29 ret = writev(fd, iov, array_size(iov));
30 if (ret < 0)
31 panic("Writev I/O error: %s!\n", strerror(errno));
33 iov_slot = 0;
36 fmemcpy(iov[iov_slot].iov_base, &phdr->raw, hdrsize);
37 iov[iov_slot].iov_len = hdrsize;
39 fmemcpy(iov[iov_slot].iov_base + iov[iov_slot].iov_len, packet, len);
40 ret = (iov[iov_slot].iov_len += len);
42 iov_slot++;
43 return ret;
46 static ssize_t __pcap_sg_inter_iov_hdr_read(int fd, pcap_pkthdr_t *phdr, enum pcap_type type,
47 uint8_t *packet, size_t len, size_t hdrsize)
49 int ret;
50 size_t offset = 0;
51 ssize_t remainder;
53 offset = iov[iov_slot].iov_len - iov_off_rd;
54 remainder = hdrsize - offset;
55 if (remainder < 0)
56 remainder = 0;
58 bug_on(offset + remainder != hdrsize);
60 fmemcpy(&phdr->raw, iov[iov_slot].iov_base + iov_off_rd, offset);
61 iov_off_rd = 0;
62 iov_slot++;
64 if (iov_slot == array_size(iov)) {
65 iov_slot = 0;
66 ret = readv(fd, iov, array_size(iov));
67 if (unlikely(ret <= 0))
68 return -EIO;
71 fmemcpy(&phdr->raw + offset, iov[iov_slot].iov_base + iov_off_rd, remainder);
72 iov_off_rd += remainder;
74 return hdrsize;
77 static ssize_t __pcap_sg_inter_iov_data_read(int fd, uint8_t *packet, size_t len, size_t hdrlen)
79 int ret;
80 size_t offset = 0;
81 ssize_t remainder;
83 offset = iov[iov_slot].iov_len - iov_off_rd;
84 remainder = hdrlen - offset;
85 if (remainder < 0)
86 remainder = 0;
88 bug_on(offset + remainder != hdrlen);
90 fmemcpy(packet, iov[iov_slot].iov_base + iov_off_rd, offset);
91 iov_off_rd = 0;
92 iov_slot++;
94 if (iov_slot == array_size(iov)) {
95 iov_slot = 0;
96 ret = readv(fd, iov, array_size(iov));
97 if (unlikely(ret <= 0))
98 return -EIO;
101 fmemcpy(packet + offset, iov[iov_slot].iov_base + iov_off_rd, remainder);
102 iov_off_rd += remainder;
104 return hdrlen;
107 static ssize_t pcap_sg_read(int fd, pcap_pkthdr_t *phdr, enum pcap_type type,
108 uint8_t *packet, size_t len)
110 ssize_t ret = 0;
111 size_t hdrsize = pcap_get_hdr_length(phdr, type), hdrlen;
113 if (likely(iov[iov_slot].iov_len - iov_off_rd >= hdrsize)) {
114 fmemcpy(&phdr->raw, iov[iov_slot].iov_base + iov_off_rd, hdrsize);
115 iov_off_rd += hdrsize;
116 } else {
117 ret = __pcap_sg_inter_iov_hdr_read(fd, phdr, type, packet,
118 len, hdrsize);
119 if (unlikely(ret < 0))
120 return ret;
123 hdrlen = pcap_get_length(phdr, type);
124 if (unlikely(hdrlen == 0 || hdrlen > len))
125 return -EINVAL;
127 if (likely(iov[iov_slot].iov_len - iov_off_rd >= hdrlen)) {
128 fmemcpy(packet, iov[iov_slot].iov_base + iov_off_rd, hdrlen);
129 iov_off_rd += hdrlen;
130 } else {
131 ret = __pcap_sg_inter_iov_data_read(fd, packet, len, hdrlen);
132 if (unlikely(ret < 0))
133 return ret;
136 return hdrsize + hdrlen;
139 static void pcap_sg_fsync(int fd)
141 ssize_t ret = writev(fd, iov, iov_slot);
142 if (ret < 0)
143 panic("Writev I/O error: %s!\n", strerror(errno));
145 iov_slot = 0;
146 fdatasync(fd);
149 static void pcap_sg_init_once(void)
151 set_ioprio_rt();
154 static int pcap_sg_prepare_access(int fd, enum pcap_mode mode, bool jumbo)
156 int i, ret;
157 size_t len = 0;
159 iov_slot = 0;
160 len = jumbo ? (PAGE_SIZE * 16) /* 64k max */ :
161 (PAGE_SIZE * 3) /* 12k max */;
163 for (i = 0; i < array_size(iov); ++i) {
164 iov[i].iov_base = xzmalloc_aligned(len, 64);
165 iov[i].iov_len = len;
168 if (mode == PCAP_MODE_RD) {
169 ret = readv(fd, iov, array_size(iov));
170 if (ret <= 0)
171 return -EIO;
173 iov_off_rd = 0;
174 iov_slot = 0;
177 return 0;
180 static void pcap_sg_prepare_close(int fd, enum pcap_mode mode)
182 int i;
184 for (i = 0; i < array_size(iov); ++i)
185 xfree(iov[i].iov_base);
188 const struct pcap_file_ops pcap_sg_ops = {
189 .init_once_pcap = pcap_sg_init_once,
190 .pull_fhdr_pcap = pcap_generic_pull_fhdr,
191 .push_fhdr_pcap = pcap_generic_push_fhdr,
192 .prepare_access_pcap = pcap_sg_prepare_access,
193 .prepare_close_pcap = pcap_sg_prepare_close,
194 .read_pcap = pcap_sg_read,
195 .write_pcap = pcap_sg_write,
196 .fsync_pcap = pcap_sg_fsync,