Thread: [Netadm-devel] gwc/pf Makefile,1.6,1.7 sysklog.c,1.3,1.4
Status: Beta
Brought to you by:
linuxpark
From: linuxpark <lin...@us...> - 2006-05-09 01:05:46
|
Update of /cvsroot/netadm/gwc/pf In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv17018/pf Modified Files: Makefile sysklog.c Log Message: MOD: make enable logr ratelimit. (not not complete) Index: Makefile =================================================================== RCS file: /cvsroot/netadm/gwc/pf/Makefile,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** Makefile 6 May 2006 15:21:07 -0000 1.6 --- Makefile 6 May 2006 23:43:23 -0000 1.7 *************** *** 8,11 **** --- 8,22 ---- #ident "@(#) $Header$" + + # \ \ + # gwc_printk ratelimit functionality + # /-----------------------------------/ + # + # If you use this function, you can setup the rate in include/global.h + # MACROS: + # GWCPK_JIFFIES (kernel jiffies, default: 1 HZ), + # GWCPK_BURST (message count limit for GWCPK_JIFFIES, default: 10 messages) + DUSE_GWCPK_RATELIMIT = 1 + obj-m += sysktimer.o obj-m += pf.o *************** *** 13,16 **** --- 24,32 ---- KDIR := /lib/modules/$(shell uname -r)/build PWD := $(shell pwd) + + ifdef DUSE_GWCPK_RATELIMIT + CFLAGS += -DUSE_GWCPK_RATELIMIT + endif + all: default Index: sysklog.c =================================================================== RCS file: /cvsroot/netadm/gwc/pf/sysklog.c,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** sysklog.c 6 May 2006 20:48:08 -0000 1.3 --- sysklog.c 6 May 2006 23:43:23 -0000 1.4 *************** *** 71,74 **** --- 71,80 ---- static spinlock_t logbuf_lock [MAXSYSIDX]; static wait_queue_head_t log_wait [MAXSYSIDX]; + + static spinlock_t ratelimit_lock [MAXSYSIDX]; + static unsigned long last_msg [MAXSYSIDX]; + + static unsigned long toks [MAXSYSIDX]; + static int missed [MAXSYSIDX]; /* *************** *** 89,92 **** --- 95,100 ---- logbuf_lock [i] = SPIN_LOCK_UNLOCKED; log_buf [i] = __log_buf [i]; + ratelimit_lock [i] = SPIN_LOCK_UNLOCKED; + toks [i] = 10 * 5 * HZ; } } *************** *** 259,271 **** } - #if 0 /* i don't have interest this syscall 3 --LP */ - #ifdef CONFIG_GWC_SYSLOG_SYSCALL - asmlinkage long sys_gwc_syslog(int idx, int type, char __user * buf, int len) - { - return do_gwc_sysklog(idx, type, buf, len); - } - #endif /* #ifdef CONFIG_GWC_SYSLOG_SYSCALL */ - #endif - static void emit_log_char(int idx, char c) { --- 267,270 ---- *************** *** 312,352 **** * attack impossible. */ ! int __gwc_printk_ratelimit(int ratelimit_jiffies, int ratelimit_burst) { - static DEFINE_SPINLOCK(ratelimit_lock); - static unsigned long toks = 10*5*HZ; - static unsigned long last_msg; - static int missed; unsigned long flags; unsigned long now = jiffies; ! spin_lock_irqsave(&ratelimit_lock, flags); ! toks += now - last_msg; ! last_msg = now; ! if (toks > (ratelimit_burst * ratelimit_jiffies)) ! toks = ratelimit_burst * ratelimit_jiffies; ! if (toks >= ratelimit_jiffies) { ! int lost = missed; ! missed = 0; ! toks -= ratelimit_jiffies; ! spin_unlock_irqrestore(&ratelimit_lock, flags); if (lost) ! printk(KERN_WARNING "printk: %d messages suppressed.\n", lost); return 1; } ! missed++; ! spin_unlock_irqrestore(&ratelimit_lock, flags); return 0; } /* minimum time in jiffies between messages */ ! static int printk_ratelimit_jiffies = 1*HZ; /* number of messages we send before ratelimiting */ ! static int printk_ratelimit_burst = 1; ! int gwc_printk_ratelimit(void) { ! return __gwc_printk_ratelimit(printk_ratelimit_jiffies, printk_ratelimit_burst); } --- 311,356 ---- * attack impossible. */ ! int __gwc_printk_ratelimit(int idx, int ratelimit_jiffies, int ratelimit_burst) { unsigned long flags; unsigned long now = jiffies; ! spin_lock_irqsave(&ratelimit_lock[idx], flags); ! toks[idx] += now - last_msg [idx]; ! last_msg [idx] = now; ! if (toks[idx] > (ratelimit_burst * ratelimit_jiffies)) ! toks [idx] = ratelimit_burst * ratelimit_jiffies; ! if (toks [idx] >= ratelimit_jiffies) { ! int lost = missed [idx]; ! missed [idx] = 0; ! toks [idx] -= ratelimit_jiffies; ! spin_unlock_irqrestore(&ratelimit_lock[idx], flags); if (lost) ! gwc_printk (idx, "gwc_printk: %d messages suppressed.\n", lost); return 1; } ! missed [idx]++; ! spin_unlock_irqrestore(&ratelimit_lock[idx], flags); return 0; } + /* + * printk_ratelimit_jiffies: 1 * HZ (5sec) + * printk_ratelimit_burst: 10 occur + * means we don't allow above 10 messages for 1 sec + * + * As a result, we can't not see same message 10 times for 1 sec. + * --LP (linuxpark or Jeho-Park) + */ + /* minimum time in jiffies between messages */ ! static int printk_ratelimit_jiffies = GWCPK_JIFFIES * HZ; /* number of messages we send before ratelimiting */ ! static int printk_ratelimit_burst = GWCPK_BURST; ! int gwc_printk_ratelimit(int idx) { ! return __gwc_printk_ratelimit(idx, printk_ratelimit_jiffies, printk_ratelimit_burst); } *************** *** 407,411 **** nanosec_rem = do_div(t, 1000000000); tlen = sprintf(tbuf, ! "<%c>[%5lu.%06lu] ", loglev_char, (unsigned long)t, --- 411,415 ---- nanosec_rem = do_div(t, 1000000000); tlen = sprintf(tbuf, ! "<%c>[sec:%5lu:nanosec:%06lu] ", loglev_char, (unsigned long)t, *************** *** 461,464 **** --- 465,476 ---- va_list args; int r; + #ifdef USE_GWCPK_RATELIMIT + if (gwc_printk_ratelimit(idx)) + return 0; + #endif + if ( idx <= MINSYSIDX || idx >= MAXSYSIDX ) { + gwc_printk (GWC_KSYS_IDX, "Wrong idx (%d) received\n", idx); + return 0; + } va_start(args, fmt); *************** *** 630,634 **** init_gwc_logbuf (); init_gwc_kctl (); - gwc_printk_ratelimit (); return 0; } --- 642,645 ---- |