This adds some setsockopt(SOL_PACKET) options for changing the behavior when
getting packet statistics from the kernel.

Signed-off-by: Kris Katterjohn <[EMAIL PROTECTED]>

This is a diff from 2.6.15 and I AM subscribed to netdev, so you dont' need to
CC me anymore.

I sent this to the linux-kernel mailing-list at the end of November, so I'm
resending it here now.

This adds PACKET_AUTO_STATISTICS, PACKET_MANUAL_STATISTICS, and
PACKET_RESET_STATISTICS setsockopt() options.

PACKET_AUTO_STATISTICS is the default and the kernel will zero the packet
statistics when the PACKET_STATISTICS getsockopt() call is used.
PACKET_MANUAL_STATISTICS changes is so that the kernel won't zero the stats
unless you use PACKET_RESET_STATISTICS or call PACKET_AUTO_STATISTICS to go
back to the default behavior.

This way you don't have to keep track of the stats in userland if you use
PACKET_MANUAL_STATISTICS/PACKET_RESET_STATISTICS.

You can zero the stats with PACKET_RESET_STATISTICS even if you are in "AUTO"
mode.

Thanks!

--- x/net/packet/af_packet.c    2006-01-07 11:31:07.000000000 -0600
+++ y/net/packet/af_packet.c    2006-01-07 11:28:56.000000000 -0600
@@ -41,6 +41,12 @@
  *                                     will simply extend the hardware address
  *                                     byte arrays at the end of sockaddr_ll 
  *                                     and packet_mreq.
+ *             Kris Katterjohn :       Added setsockopt options:
+ *                                     PACKET_AUTO_STATISTICS,
+ *                                     PACKET_MANUAL_STATISTICS, and
+ *                                     PACKET_RESET_STATISTICS to handle the
+ *                                     zero-ing of packet stats when using
+ *                                     PACKET_STATISTICS. 2005-11-29.
  *
  *             This program is free software; you can redistribute it and/or
  *             modify it under the terms of the GNU General Public License
@@ -189,6 +195,7 @@ struct packet_sock {
        /* struct sock has to be the first member of packet_sock */
        struct sock             sk;
        struct tpacket_stats    stats;
+       int                     auto_reset_stats;
 #ifdef CONFIG_PACKET_MMAP
        char *                  *pg_vec;
        unsigned int            head;
@@ -1020,6 +1027,7 @@ static int packet_create(struct socket *
        po = pkt_sk(sk);
        sk->sk_family = PF_PACKET;
        po->num = protocol;
+       po->auto_reset_stats = 1;
 
        sk->sk_destruct = packet_sock_destruct;
        atomic_inc(&packet_socks_nr);
@@ -1324,6 +1332,7 @@ static int
 packet_setsockopt(struct socket *sock, int level, int optname, char __user 
*optval, int optlen)
 {
        struct sock *sk = sock->sk;
+       struct packet_sock *po = pkt_sk(sk);
        int ret;
 
        if (level != SOL_PACKET)
@@ -1352,6 +1361,21 @@ packet_setsockopt(struct socket *sock, i
                return ret;
        }
 #endif
+
+       case PACKET_AUTO_STATISTICS:
+               po->auto_reset_stats = 1;
+               return 0;
+
+       case PACKET_MANUAL_STATISTICS:
+               po->auto_reset_stats = 0;
+               return 0;
+
+       case PACKET_RESET_STATISTICS:
+               spin_lock_bh(&sk->sk_receive_queue.lock);
+               memset(&po->stats, 0, sizeof po->stats);
+               spin_unlock_bh(&sk->sk_receive_queue.lock);
+               return 0;
+
 #ifdef CONFIG_PACKET_MMAP
        case PACKET_RX_RING:
        {
@@ -1406,7 +1430,8 @@ static int packet_getsockopt(struct sock
                        len = sizeof(struct tpacket_stats);
                spin_lock_bh(&sk->sk_receive_queue.lock);
                st = po->stats;
-               memset(&po->stats, 0, sizeof(st));
+               if (po->auto_reset_stats)
+                       memset(&po->stats, 0, sizeof po->stats);
                spin_unlock_bh(&sk->sk_receive_queue.lock);
                st.tp_packets += st.tp_drops;
 
--- x/include/linux/if_packet.h 2006-01-02 21:21:10.000000000 -0600
+++ y/include/linux/if_packet.h 2006-01-07 11:43:47.000000000 -0600
@@ -38,7 +38,10 @@ struct sockaddr_ll
 /* Value 4 is still used by obsolete turbo-packet. */
 #define PACKET_RX_RING                 5
 #define PACKET_STATISTICS              6
-#define PACKET_COPY_THRESH             7
+#define PACKET_AUTO_STATISTICS         7
+#define PACKET_MANUAL_STATISTICS       8
+#define PACKET_RESET_STATISTICS                9
+#define PACKET_COPY_THRESH             10
 
 struct tpacket_stats
 {


-
To unsubscribe from this list: send the line "unsubscribe netdev" in
the body of a message to [EMAIL PROTECTED]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to