svn commit: r243711 - head/sys/netpfil/ipfw

Alexander V. Chernikov melifaro at FreeBSD.org
Fri Nov 30 19:36:56 UTC 2012


Author: melifaro
Date: Fri Nov 30 19:36:55 2012
New Revision: 243711
URL: http://svnweb.freebsd.org/changeset/base/243711

Log:
  Use common macros for working with rule/dynamic counters.
  This is done as preparation to introduce per-cpu ipfw counters.
  
  MFC after:	3 weeks

Modified:
  head/sys/netpfil/ipfw/ip_fw2.c
  head/sys/netpfil/ipfw/ip_fw_dynamic.c
  head/sys/netpfil/ipfw/ip_fw_private.h
  head/sys/netpfil/ipfw/ip_fw_sockopt.c

Modified: head/sys/netpfil/ipfw/ip_fw2.c
==============================================================================
--- head/sys/netpfil/ipfw/ip_fw2.c	Fri Nov 30 19:36:20 2012	(r243710)
+++ head/sys/netpfil/ipfw/ip_fw2.c	Fri Nov 30 19:36:55 2012	(r243711)
@@ -2034,8 +2034,7 @@ do {								\
 					 * the parent rule by setting
 					 * f, cmd, l and clearing cmdlen.
 					 */
-					q->pcnt++;
-					q->bcnt += pktlen;
+					IPFW_INC_DYN_COUNTER(q, pktlen);
 					/* XXX we would like to have f_pos
 					 * readily accessible in the dynamic
 				         * rule, instead of having to
@@ -2096,16 +2095,12 @@ do {								\
 				break;
 
 			case O_COUNT:
-				f->pcnt++;	/* update stats */
-				f->bcnt += pktlen;
-				f->timestamp = time_uptime;
+				IPFW_INC_RULE_COUNTER(f, pktlen);
 				l = 0;		/* exit inner loop */
 				break;
 
 			case O_SKIPTO:
-			    f->pcnt++;	/* update stats */
-			    f->bcnt += pktlen;
-			    f->timestamp = time_uptime;
+			    IPFW_INC_RULE_COUNTER(f, pktlen);
 			    /* If possible use cached f_pos (in f->next_rule),
 			     * whose version is written in f->next_rule
 			     * (horrible hacks to avoid changing the ABI).
@@ -2202,9 +2197,7 @@ do {								\
 					break;
 				}
 
-				f->pcnt++;	/* update stats */
-				f->bcnt += pktlen;
-				f->timestamp = time_uptime;
+				IPFW_INC_RULE_COUNTER(f, pktlen);
 				stack = (uint16_t *)(mtag + 1);
 
 				/*
@@ -2357,9 +2350,7 @@ do {								\
 			case O_SETFIB: {
 				uint32_t fib;
 
-				f->pcnt++;	/* update stats */
-				f->bcnt += pktlen;
-				f->timestamp = time_uptime;
+				IPFW_INC_RULE_COUNTER(f, pktlen);
 				fib = (cmd->arg1 == IP_FW_TABLEARG) ? tablearg:
 				    cmd->arg1;
 				if (fib >= rt_numfibs)
@@ -2409,8 +2400,7 @@ do {								\
 			case O_REASS: {
 				int ip_off;
 
-				f->pcnt++;
-				f->bcnt += pktlen;
+				IPFW_INC_RULE_COUNTER(f, pktlen);
 				l = 0;	/* in any case exit inner loop */
 				ip_off = ntohs(ip->ip_off);
 
@@ -2473,9 +2463,7 @@ do {								\
 	if (done) {
 		struct ip_fw *rule = chain->map[f_pos];
 		/* Update statistics */
-		rule->pcnt++;
-		rule->bcnt += pktlen;
-		rule->timestamp = time_uptime;
+		IPFW_INC_RULE_COUNTER(rule, pktlen);
 	} else {
 		retval = IP_FW_DENY;
 		printf("ipfw: ouch!, skip past end of rules, denying packet\n");

Modified: head/sys/netpfil/ipfw/ip_fw_dynamic.c
==============================================================================
--- head/sys/netpfil/ipfw/ip_fw_dynamic.c	Fri Nov 30 19:36:20 2012	(r243710)
+++ head/sys/netpfil/ipfw/ip_fw_dynamic.c	Fri Nov 30 19:36:55 2012	(r243711)
@@ -594,7 +594,7 @@ add_dyn_rule(struct ipfw_flow_id *id, in
 	r->expire = time_uptime + V_dyn_syn_lifetime;
 	r->rule = rule;
 	r->dyn_type = dyn_type;
-	r->pcnt = r->bcnt = 0;
+	IPFW_ZERO_DYN_COUNTER(r);
 	r->count = 0;
 
 	r->bucket = i;

Modified: head/sys/netpfil/ipfw/ip_fw_private.h
==============================================================================
--- head/sys/netpfil/ipfw/ip_fw_private.h	Fri Nov 30 19:36:20 2012	(r243710)
+++ head/sys/netpfil/ipfw/ip_fw_private.h	Fri Nov 30 19:36:55 2012	(r243711)
@@ -236,6 +236,28 @@ struct ip_fw_chain {
 
 struct sockopt;	/* used by tcp_var.h */
 
+/* Macro for working with various counters */
+#define	IPFW_INC_RULE_COUNTER(_cntr, _bytes)	do {	\
+	(_cntr)->pcnt++;				\
+	(_cntr)->bcnt += _bytes;			\
+	(_cntr)->timestamp = time_uptime;		\
+	} while (0)
+
+#define	IPFW_INC_DYN_COUNTER(_cntr, _bytes)	do {		\
+	(_cntr)->pcnt++;				\
+	(_cntr)->bcnt += _bytes;			\
+	} while (0)
+
+#define	IPFW_ZERO_RULE_COUNTER(_cntr) do {		\
+	(_cntr)->pcnt = 0;				\
+	(_cntr)->bcnt = 0;				\
+	(_cntr)->timestamp = 0;				\
+	} while (0)
+
+#define	IPFW_ZERO_DYN_COUNTER(_cntr) do {		\
+	(_cntr)->pcnt = 0;				\
+	(_cntr)->bcnt = 0;				\
+	} while (0)
 /*
  * The lock is heavily used by ip_fw2.c (the main file) and ip_fw_nat.c
  * so the variable and the macros must be here.

Modified: head/sys/netpfil/ipfw/ip_fw_sockopt.c
==============================================================================
--- head/sys/netpfil/ipfw/ip_fw_sockopt.c	Fri Nov 30 19:36:20 2012	(r243710)
+++ head/sys/netpfil/ipfw/ip_fw_sockopt.c	Fri Nov 30 19:36:55 2012	(r243711)
@@ -175,9 +175,7 @@ ipfw_add_rule(struct ip_fw_chain *chain,
 	/* clear fields not settable from userland */
 	rule->x_next = NULL;
 	rule->next_rule = NULL;
-	rule->pcnt = 0;
-	rule->bcnt = 0;
-	rule->timestamp = 0;
+	IPFW_ZERO_RULE_COUNTER(rule);
 
 	if (V_autoinc_step < 1)
 		V_autoinc_step = 1;
@@ -439,10 +437,8 @@ clear_counters(struct ip_fw *rule, int l
 {
 	ipfw_insn_log *l = (ipfw_insn_log *)ACTION_PTR(rule);
 
-	if (log_only == 0) {
-		rule->bcnt = rule->pcnt = 0;
-		rule->timestamp = 0;
-	}
+	if (log_only == 0)
+		IPFW_ZERO_RULE_COUNTER(rule);
 	if (l->o.opcode == O_LOG)
 		l->log_left = l->max_log;
 }


More information about the svn-src-head mailing list