On Wed, Mar 21, 2012 at 05:03:42PM +0100, Nikola Pajkovsky wrote:
Vitezslav Samel vitezslav@samel.cz writes:
To reduce number of compares of interface name (to throw away packets we don't want) lock the socket to the given interface using bind() interface.
Signed-off-by: Vitezslav Samel vitezslav@samel.cz
src/hostmon.c | 16 ++++++++++++++-- src/ifstats.c | 25 ++++++++++++++++++++++--- src/iptraf-ng-compat.h | 2 ++ src/itrafmon.c | 20 ++++++++++++++++---- src/pktsize.c | 19 +++++++++++++++---- src/serv.c | 17 +++++++++++++++-- src/wrapper.c | 24 ++++++++++++++++++++++++ 7 files changed, 108 insertions(+), 15 deletions(-)
diff --git a/src/iptraf-ng-compat.h b/src/iptraf-ng-compat.h index 28af383..06b6cad 100644 --- a/src/iptraf-ng-compat.h +++ b/src/iptraf-ng-compat.h @@ -96,6 +96,8 @@ extern char *xstrdup(const char *s);
extern int xsocket(int domain, int type, int protocol); extern int xsocket_raw_eth_p_all(void); +int socket_bind_to_iface(const int fd, const int ifindex); +int socket_bind_to_iface_by_name(const int fd, const char const *ifname);
I'm going to add extern before
I'll fix it.
+int socket_bind_to_iface(const int fd, const int ifindex) +{
- struct sockaddr_ll fromaddr;
- socklen_t addrlen = sizeof(fromaddr);
- fromaddr.sll_family = AF_PACKET;
- fromaddr.sll_protocol = htons(ETH_P_ALL);
- fromaddr.sll_ifindex = ifindex;
- return(bind(fd, (struct sockaddr *) &fromaddr, addrlen));
+}
use return foo; instead of return(foo);
OK.
+int socket_bind_to_iface_by_name(const int fd, const char const *ifname) +{
- int ir;
- struct ifreq ifr;
- strcpy(ifr.ifr_name, ifname);
- ir = ioctl(fd, SIOCGIFINDEX, &ifr);
- if(ir != 0)
return(ir);
- return(socket_bind_to_iface(fd, ifr.ifr_ifindex));
+}
ditto
OK.
Do you have any statistic about performance? Is it better?
No, but it can't hurt: the filtering is done in kernel and the unwanted packets aren't transfered to user space. Right now I can't do any tests (I don't have any test machine to generate steady net flow to measure the CPU utilization).
And libpcap does the binding too (uncoditionally).
Vita
P.S. I'll send you v2 of this patch series soon.