Skip to content

Commit 27fd8d9

Browse files
committed
netfilter: nf_log: move log buffering to core logging
This patch moves Eric Dumazet's log buffer implementation from the xt_log.h header file to the core net/netfilter/nf_log.c. This also includes the renaming of the structure and functions to avoid possible undesired namespace clashes. This change allows us to use it from the arp and bridge packet logging implementation in follow up patches.
1 parent 5962815 commit 27fd8d9

File tree

4 files changed

+217
-201
lines changed

4 files changed

+217
-201
lines changed

include/net/netfilter/nf_log.h

+6
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,10 @@ void nf_log_packet(struct net *net,
7272
const struct nf_loginfo *li,
7373
const char *fmt, ...);
7474

75+
struct nf_log_buf;
76+
77+
struct nf_log_buf *nf_log_buf_open(void);
78+
__printf(2, 3) int nf_log_buf_add(struct nf_log_buf *m, const char *f, ...);
79+
void nf_log_buf_close(struct nf_log_buf *m);
80+
7581
#endif /* _NF_LOG_H */

include/net/netfilter/xt_log.h

-54
This file was deleted.

net/netfilter/nf_log.c

+57
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,63 @@ void nf_log_packet(struct net *net,
157157
}
158158
EXPORT_SYMBOL(nf_log_packet);
159159

160+
#define S_SIZE (1024 - (sizeof(unsigned int) + 1))
161+
162+
struct nf_log_buf {
163+
unsigned int count;
164+
char buf[S_SIZE + 1];
165+
};
166+
static struct nf_log_buf emergency, *emergency_ptr = &emergency;
167+
168+
__printf(2, 3) int nf_log_buf_add(struct nf_log_buf *m, const char *f, ...)
169+
{
170+
va_list args;
171+
int len;
172+
173+
if (likely(m->count < S_SIZE)) {
174+
va_start(args, f);
175+
len = vsnprintf(m->buf + m->count, S_SIZE - m->count, f, args);
176+
va_end(args);
177+
if (likely(m->count + len < S_SIZE)) {
178+
m->count += len;
179+
return 0;
180+
}
181+
}
182+
m->count = S_SIZE;
183+
printk_once(KERN_ERR KBUILD_MODNAME " please increase S_SIZE\n");
184+
return -1;
185+
}
186+
EXPORT_SYMBOL_GPL(nf_log_buf_add);
187+
188+
struct nf_log_buf *nf_log_buf_open(void)
189+
{
190+
struct nf_log_buf *m = kmalloc(sizeof(*m), GFP_ATOMIC);
191+
192+
if (unlikely(!m)) {
193+
local_bh_disable();
194+
do {
195+
m = xchg(&emergency_ptr, NULL);
196+
} while (!m);
197+
}
198+
m->count = 0;
199+
return m;
200+
}
201+
EXPORT_SYMBOL_GPL(nf_log_buf_open);
202+
203+
void nf_log_buf_close(struct nf_log_buf *m)
204+
{
205+
m->buf[m->count] = 0;
206+
printk("%s\n", m->buf);
207+
208+
if (likely(m != &emergency))
209+
kfree(m);
210+
else {
211+
emergency_ptr = m;
212+
local_bh_enable();
213+
}
214+
}
215+
EXPORT_SYMBOL_GPL(nf_log_buf_close);
216+
160217
#ifdef CONFIG_PROC_FS
161218
static void *seq_start(struct seq_file *seq, loff_t *pos)
162219
{

0 commit comments

Comments
 (0)