2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2011 - 2013 Daniel Borkmann.
4 * Subject to the GPL, version 2.
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
));
31 panic("Writev I/O error: %s!\n", strerror(errno
));
36 memcpy(iov
[iov_slot
].iov_base
, &phdr
->raw
, hdrsize
);
37 iov
[iov_slot
].iov_len
= hdrsize
;
39 memcpy(iov
[iov_slot
].iov_base
+ iov
[iov_slot
].iov_len
, packet
, len
);
40 ret
= (iov
[iov_slot
].iov_len
+= len
);
46 static ssize_t
__pcap_sg_inter_iov_hdr_read(int fd
, pcap_pkthdr_t
*phdr
,
53 offset
= iov
[iov_slot
].iov_len
- iov_off_rd
;
54 remainder
= hdrsize
- offset
;
58 bug_on(offset
+ remainder
!= hdrsize
);
60 memcpy(&phdr
->raw
, iov
[iov_slot
].iov_base
+ iov_off_rd
, offset
);
64 if (iov_slot
== array_size(iov
)) {
66 ret
= readv(fd
, iov
, array_size(iov
));
67 if (unlikely(ret
<= 0))
71 memcpy(&phdr
->raw
+ offset
, iov
[iov_slot
].iov_base
+ iov_off_rd
, remainder
);
72 iov_off_rd
+= remainder
;
77 static ssize_t
__pcap_sg_inter_iov_data_read(int fd
, uint8_t *packet
, size_t hdrlen
)
83 offset
= iov
[iov_slot
].iov_len
- iov_off_rd
;
84 remainder
= hdrlen
- offset
;
88 bug_on(offset
+ remainder
!= hdrlen
);
90 memcpy(packet
, iov
[iov_slot
].iov_base
+ iov_off_rd
, offset
);
94 if (iov_slot
== array_size(iov
)) {
96 ret
= readv(fd
, iov
, array_size(iov
));
97 if (unlikely(ret
<= 0))
101 memcpy(packet
+ offset
, iov
[iov_slot
].iov_base
+ iov_off_rd
, remainder
);
102 iov_off_rd
+= remainder
;
107 static ssize_t
pcap_sg_read(int fd
, pcap_pkthdr_t
*phdr
, enum pcap_type type
,
108 uint8_t *packet
, size_t len
)
111 size_t hdrsize
= pcap_get_hdr_length(phdr
, type
), hdrlen
;
113 if (likely(iov
[iov_slot
].iov_len
- iov_off_rd
>= hdrsize
)) {
114 memcpy(&phdr
->raw
, iov
[iov_slot
].iov_base
+ iov_off_rd
, hdrsize
);
115 iov_off_rd
+= hdrsize
;
117 ret
= __pcap_sg_inter_iov_hdr_read(fd
, phdr
, hdrsize
);
118 if (unlikely(ret
< 0))
122 hdrlen
= pcap_get_length(phdr
, type
);
123 if (unlikely(hdrlen
== 0 || hdrlen
> len
))
126 if (likely(iov
[iov_slot
].iov_len
- iov_off_rd
>= hdrlen
)) {
127 memcpy(packet
, iov
[iov_slot
].iov_base
+ iov_off_rd
, hdrlen
);
128 iov_off_rd
+= hdrlen
;
130 ret
= __pcap_sg_inter_iov_data_read(fd
, packet
, hdrlen
);
131 if (unlikely(ret
< 0))
135 return hdrsize
+ hdrlen
;
138 static void pcap_sg_fsync(int fd
)
140 ssize_t ret
= writev(fd
, iov
, iov_slot
);
142 panic("Writev I/O error: %s!\n", strerror(errno
));
148 static void pcap_sg_init_once(bool enforce_prio
)
154 static int pcap_sg_prepare_access(int fd
, enum pcap_mode mode
, bool jumbo
)
160 len
= jumbo
? (RUNTIME_PAGE_SIZE
* 16) /* 64k max */ :
161 (RUNTIME_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
));
180 static void pcap_sg_prepare_close(int fd __maybe_unused
,
181 enum pcap_mode mode __maybe_unused
)
185 for (i
= 0; i
< array_size(iov
); ++i
)
186 xfree(iov
[i
].iov_base
);
189 const struct pcap_file_ops pcap_sg_ops
= {
190 .init_once_pcap
= pcap_sg_init_once
,
191 .pull_fhdr_pcap
= pcap_generic_pull_fhdr
,
192 .push_fhdr_pcap
= pcap_generic_push_fhdr
,
193 .prepare_access_pcap
= pcap_sg_prepare_access
,
194 .prepare_close_pcap
= pcap_sg_prepare_close
,
195 .read_pcap
= pcap_sg_read
,
196 .write_pcap
= pcap_sg_write
,
197 .fsync_pcap
= pcap_sg_fsync
,