source: ksyslog/trunk/ksyslog.c @ 270

Revision 270, 15.8 KB checked in by atzm, 10 years ago (diff)

revert r269, this causes cpu soft lockup

Line 
1/*
2 * ksyslog: In-kernel syslog receiver
3 * Copyright(C) 2013 Atzm WATANABE All rights reserved
4 * Distributed under the GPL
5 */
6
7#include <linux/version.h>
8#include <linux/module.h>
9#include <linux/inet.h>
10#include <linux/ip.h>
11#include <linux/udp.h>
12#include <linux/namei.h>
13#include <linux/fsnotify.h>
14#include <linux/proc_fs.h>
15#include <linux/u64_stats_sync.h>
16#include <linux/percpu.h>
17#include <net/udp.h>
18#include "compat.h"
19#include "ksyslog.h"
20
21static struct ksyslog_queue ksyslog_queue;
22static struct socket *ksyslog_rcv_sk = NULL;
23
24static struct delayed_work ksyslog_work;
25static struct workqueue_struct *ksyslog_wq = NULL;
26
27#ifdef CONFIG_PROC_FS
28static struct proc_dir_entry *ksyslog_procdir = NULL;
29static struct proc_dir_entry *ksyslog_proc_queue = NULL;
30static struct proc_dir_entry *ksyslog_proc_size = NULL;
31static struct proc_dir_entry *ksyslog_proc_stats = NULL;
32#endif
33
34static char *ksyslog_host = "0.0.0.0";
35static ushort ksyslog_port = 514;
36static char *ksyslog_path = "/var/log/ksyslog.log";
37static ulong ksyslog_queue_size_max = 4096;
38static ulong ksyslog_flush_interval = 45;  /* milliseconds */
39
40static DEFINE_SPINLOCK(ksyslog_write_lock);
41
42module_param(ksyslog_host, charp, 0444);
43module_param(ksyslog_port, ushort, 0444);
44module_param(ksyslog_path, charp, 0644);
45module_param(ksyslog_queue_size_max, ulong, 0644);
46module_param(ksyslog_flush_interval, ulong, 0644);
47
48static int
49ksyslog_queue_init(struct ksyslog_queue *queue)
50{
51        memset(queue, 0, sizeof(*queue));
52        INIT_LIST_HEAD(&queue->head);
53        spin_lock_init(&queue->lock);
54        atomic64_set(&queue->size, 0);
55        queue->stats = alloc_percpu(struct ksyslog_stats);
56        if (unlikely(queue->stats == NULL))
57                return -ENOMEM;
58        return 0;
59}
60
61static void
62ksyslog_queue_uninit(struct ksyslog_queue *queue)
63{
64        if (likely(queue->stats))
65                free_percpu(queue->stats);
66        queue->stats = NULL;
67}
68
69static int
70ksyslog_close(struct file *file)
71{
72        int err;
73        mm_segment_t oldfs;
74
75        oldfs = get_fs();
76        set_fs(get_ds());
77
78        err = filp_close(file, NULL);
79
80        set_fs(oldfs);
81        return err;
82}
83
84static struct file *
85ksyslog_open(const char *path)
86{
87        struct file *file;
88        struct path ppath;
89        mm_segment_t oldfs;
90
91        oldfs = get_fs();
92        set_fs(get_ds());
93
94        if (unlikely(kern_path(path, LOOKUP_OPEN|LOOKUP_FOLLOW, &ppath)))
95                file = filp_open(path, O_CREAT|O_WRONLY|O_APPEND|O_LARGEFILE, 0600);
96        else
97                file = filp_open(path, O_WRONLY|O_APPEND|O_LARGEFILE, 0);
98
99        if (unlikely(IS_ERR(file)))
100                goto out;
101
102        compat_fsnotify_open(file);
103
104        if (unlikely(S_ISDIR(file->f_path.dentry->d_inode->i_mode))) {
105                ksyslog_close(file);
106                file = ERR_PTR(-EISDIR);
107                goto out;
108        }
109
110        if (unlikely(file->f_pos < 0)) {
111                ksyslog_close(file);
112                file = ERR_PTR(-EIO);
113                goto out;
114        }
115
116out:
117        set_fs(oldfs);
118        return file;
119}
120
121static int
122ksyslog_write(struct file *file, const char *buf, const size_t length)
123{
124        int err;
125        mm_segment_t oldfs;
126
127        oldfs = get_fs();
128        set_fs(get_ds());
129
130        err = vfs_write(file, (__force void __user *)buf, length, &file->f_pos);
131
132        set_fs(oldfs);
133        return err;
134}
135
136static void
137ksyslog_drop_warning(const struct ksyslog_entry *entry)
138{
139        net_warn_ratelimited("ksyslog: dropped: %llu %s.%s %u.%u.%u.%u %.*s\n",
140                             timeval_to_ns(&entry->tv) / 1000 / 1000 / 1000,
141                             ksyslog_facility_str(entry->facility),
142                             ksyslog_severity_str(entry->severity),
143                             entry->saddr.addr8[0], entry->saddr.addr8[1],
144                             entry->saddr.addr8[2], entry->saddr.addr8[3],
145                             (int)entry->length, entry->data);
146}
147
148static struct ksyslog_entry *
149ksyslog_entry_create(const struct sk_buff *skb,
150                     const struct iphdr *iph, const struct udphdr *udph)
151{
152        struct ksyslog_entry *entry;
153        unsigned int priority, facility, severity, month, day, hour, minute, second;
154        unsigned char *start, month_s[4];
155        struct tm tm;
156        int length, i;
157
158        if (sscanf(skb->data, "<%3u>%3s %2u %2u:%2u:%2u ",
159                   &priority, month_s, &day, &hour, &minute, &second) != 6)
160                return ERR_PTR(-EINVAL);
161
162        start = memchr(skb->data, '>', 5);
163        if (start == NULL)
164                return ERR_PTR(-EINVAL);
165        start++;
166
167        facility = priority >> 3;
168        severity = priority & 7;
169
170        if (facility >= __KSYSLOG_F_MAX)
171                return ERR_PTR(-EINVAL);
172        if (severity >= __KSYSLOG_S_MAX)
173                return ERR_PTR(-EINVAL);
174
175        month = ksyslog_month_num(month_s);
176        if (!month)
177                return ERR_PTR(-EINVAL);
178        if (day > 31)
179                return ERR_PTR(-EINVAL);
180        if (hour > 23)
181                return ERR_PTR(-EINVAL);
182        if (minute > 59)
183                return ERR_PTR(-EINVAL);
184        if (second > 59)
185                return ERR_PTR(-EINVAL);
186
187        entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
188        if (unlikely(entry == NULL))
189                return ERR_PTR(-ENOMEM);
190
191        length = skb->len - (start - skb->data);
192        entry->data = kzalloc(length, GFP_ATOMIC);
193        if (unlikely(entry->data == NULL)) {
194                kfree(entry);
195                return ERR_PTR(-ENOMEM);
196        }
197
198        if (skb->tstamp.tv64)
199                entry->tv = ktime_to_timeval(skb->tstamp);
200        else
201                do_gettimeofday(&entry->tv);
202
203        time_to_tm(entry->tv.tv_sec, 0, &tm);
204        entry->time = mktime(tm.tm_year + 1900, month, day, hour, minute, second);
205
206        entry->priority = priority;
207        entry->facility = facility;
208        entry->severity = severity;
209
210        entry->daddr.addr32 = iph->daddr;
211        entry->saddr.addr32 = iph->saddr;
212
213        entry->dport = udph->dest;
214        entry->sport = udph->source;
215
216        entry->length = length;
217        memcpy(entry->data, start, length);
218
219        for (i = 0; i < length; i++)
220                if (unlikely(entry->data[i] == '\n'))
221                        entry->data[i] = ' ';
222
223        return entry;
224}
225
226static void
227ksyslog_entry_free(struct rcu_head *head)
228{
229        struct ksyslog_entry *entry = container_of(head, struct ksyslog_entry, rcu);
230        kfree(entry->data);
231        kfree(entry);
232}
233
234static int
235ksyslog_entry_add(struct ksyslog_queue *queue, struct ksyslog_entry *entry)
236{
237        if (unlikely(atomic64_read(&queue->size) >= ksyslog_queue_size_max))
238                return -ENOBUFS;
239        list_add_tail_rcu(&entry->list, &queue->head);
240        WARN_ON(atomic64_inc_return(&queue->size) > ksyslog_queue_size_max);
241        return 0;
242}
243
244static void
245ksyslog_entry_del(struct ksyslog_queue *queue, struct ksyslog_entry *entry, bool free)
246{
247        WARN_ON(atomic64_dec_return(&queue->size) < 0);
248        list_del_rcu(&entry->list);
249        if (free)
250                call_rcu(&entry->rcu, ksyslog_entry_free);
251}
252
253static void
254ksyslog_entry_destroy(struct ksyslog_queue *queue)
255{
256        struct ksyslog_entry *entry, *next;
257
258        list_for_each_entry_safe(entry, next, &queue->head, list)
259                ksyslog_entry_del(queue, entry, true);
260}
261
262static int
263ksyslog_entry_format(char **buf, const struct ksyslog_entry *entry)
264{
265        *buf = kzalloc(54 + entry->length + 2, GFP_ATOMIC);
266        if (unlikely(*buf == NULL))
267                return -ENOMEM;
268
269        return sprintf(*buf, "%llu %s.%s %u.%u.%u.%u %.*s\n",
270                       timeval_to_ns(&entry->tv) / 1000 / 1000 / 1000,
271                       ksyslog_facility_str(entry->facility),
272                       ksyslog_severity_str(entry->severity),
273                       entry->saddr.addr8[0], entry->saddr.addr8[1],
274                       entry->saddr.addr8[2], entry->saddr.addr8[3],
275                       (int)entry->length, entry->data);
276}
277
278static bool
279ksyslog_entry_write(struct file *file, struct ksyslog_entry *entry)
280{
281        int length;
282        char *buf;
283
284        length = ksyslog_entry_format(&buf, entry);
285
286        if (unlikely(length < 0))
287                return false;
288
289        if (unlikely(ksyslog_write(file, buf, length) != length)) {
290                kfree(buf);
291                return false;
292        }
293
294        kfree(buf);
295        return true;
296}
297
298static void
299ksyslog_work_register(unsigned long timer)
300{
301        queue_delayed_work(ksyslog_wq, &ksyslog_work, timer * HZ / 1000);
302}
303
304static void
305ksyslog_work_unregister(void)
306{
307        cancel_delayed_work_sync(&ksyslog_work);
308}
309
310static void
311ksyslog_work_handler(struct work_struct *work)
312{
313        struct file *file = NULL;
314        struct ksyslog_entry *entry;
315
316        file = ksyslog_open(ksyslog_path);
317        if (unlikely(IS_ERR(file)))
318                goto out;
319
320        while (true) {
321                bool write_ok;
322
323                spin_lock_bh(&ksyslog_queue.lock);
324                entry = list_first_or_null_rcu(&ksyslog_queue.head,
325                                               struct ksyslog_entry, list);
326                if (!entry) {
327                        spin_unlock_bh(&ksyslog_queue.lock);
328                        break;
329                }
330                ksyslog_entry_del(&ksyslog_queue, entry, false);
331                spin_unlock_bh(&ksyslog_queue.lock);
332
333                spin_lock(&ksyslog_write_lock);
334                write_ok = ksyslog_entry_write(file, entry);
335                spin_unlock(&ksyslog_write_lock);
336
337                if (likely(write_ok)) {
338                        ksyslog_stats_add_write(&ksyslog_queue, entry->length);
339                } else {
340                        ksyslog_stats_add_drop(&ksyslog_queue, entry->length);
341                        ksyslog_drop_warning(entry);
342                }
343
344                call_rcu(&entry->rcu, ksyslog_entry_free);
345        }
346
347        ksyslog_close(file);
348
349out:
350        ksyslog_work_register(ksyslog_flush_interval);
351}
352
353static int
354ksyslog_rcv(struct sock *sk, struct sk_buff *skb)
355{
356        int err;
357        struct iphdr *iph;
358        struct udphdr *udph;
359        struct ksyslog_entry *entry;
360
361        if (unlikely(skb_linearize(skb))) {
362                ksyslog_stats_add_drop(&ksyslog_queue, skb->len);
363                goto out;
364        }
365
366        iph = ip_hdr(skb);
367        udph = udp_hdr(skb);
368
369        if (unlikely(!skb_pull(skb, sizeof(*udph)))) {
370                ksyslog_stats_add_drop(&ksyslog_queue, skb->len);
371                goto out;
372        }
373
374        entry = ksyslog_entry_create(skb, iph, udph);
375        if (unlikely(IS_ERR(entry))) {
376                if (PTR_ERR(entry) == -EINVAL) {
377                        ksyslog_stats_add_discard(&ksyslog_queue, skb->len);
378                        goto out;
379                }
380
381                ksyslog_stats_add_drop(&ksyslog_queue, skb->len);
382                goto out;
383        }
384
385        spin_lock_bh(&ksyslog_queue.lock);
386        err = ksyslog_entry_add(&ksyslog_queue, entry);
387        spin_unlock_bh(&ksyslog_queue.lock);
388
389        if (unlikely(err)) {
390                ksyslog_stats_add_drop(&ksyslog_queue, entry->length);
391                ksyslog_drop_warning(entry);
392                ksyslog_entry_free(&entry->rcu);
393                goto out;
394        }
395
396out:
397        consume_skb(skb);
398        return 0;
399}
400
401#ifdef CONFIG_PROC_FS
402static void *
403ksyslog_rculist_seq_start(struct seq_file *seq, loff_t *pos)
404{
405        struct list_head *lh, *head = seq->private;
406        loff_t ppos = *pos;
407
408        rcu_read_lock();
409
410        __list_for_each_rcu(lh, head)
411                if (ppos-- == 0)
412                        return lh;
413
414        return NULL;
415}
416
417static void *
418ksyslog_rculist_seq_next(struct seq_file *seq, void *v, loff_t *pos)
419{
420        struct list_head *lh = rcu_dereference(((struct list_head *)v)->next);
421        ++(*pos);
422        return lh == seq->private ? NULL : lh;
423}
424
425static void
426ksyslog_rculist_seq_stop(struct seq_file *seq, void *v)
427{
428        rcu_read_unlock();
429}
430
431static int
432ksyslog_queue_seq_show(struct seq_file *seq, void *v)
433{
434        const struct ksyslog_entry *entry = list_entry_rcu(v, struct ksyslog_entry, list);
435
436        seq_printf(seq, "%llu %s.%s %u.%u.%u.%u %.*s\n",
437                   timeval_to_ns(&entry->tv) / 1000 / 1000 / 1000,
438                   ksyslog_facility_str(entry->facility),
439                   ksyslog_severity_str(entry->severity),
440                   entry->saddr.addr8[0], entry->saddr.addr8[1],
441                   entry->saddr.addr8[2], entry->saddr.addr8[3],
442                   (int)entry->length, entry->data);
443
444        return 0;
445}
446
447static struct seq_operations ksyslog_queue_seq_ops = {
448        .start = ksyslog_rculist_seq_start,
449        .next  = ksyslog_rculist_seq_next,
450        .stop  = ksyslog_rculist_seq_stop,
451        .show  = ksyslog_queue_seq_show,
452};
453
454static int
455ksyslog_queue_seq_open(struct inode *inode, struct file *file)
456{
457        int err = seq_open(file, &ksyslog_queue_seq_ops);
458
459        if (!err)
460                ((struct seq_file *)file->private_data)->private = PDE_DATA(inode);
461
462        return err;
463}
464
465static struct file_operations ksyslog_queue_fops = {
466        .owner   = THIS_MODULE,
467        .open    = ksyslog_queue_seq_open,
468        .read    = seq_read,
469        .llseek  = seq_lseek,
470        .release = seq_release,
471};
472
473static int
474ksyslog_size_seq_show(struct seq_file *seq, void *v)
475{
476        seq_printf(seq, "%lu\n", atomic64_read(&ksyslog_queue.size));
477        return 0;
478}
479
480static int
481ksyslog_size_seq_open(struct inode *inode, struct file *file)
482{
483        return single_open(file, ksyslog_size_seq_show, PDE_DATA(inode));
484}
485
486static int
487ksyslog_stats_seq_show(struct seq_file *seq, void *v)
488{
489        int i;
490        struct ksyslog_stats stats;
491
492        memset(&stats, 0, sizeof(stats));
493
494        for_each_possible_cpu(i) {
495                const struct ksyslog_stats *percpu_stats;
496                struct ksyslog_stats local_stats;
497                unsigned int start;
498
499                percpu_stats = per_cpu_ptr(ksyslog_queue.stats, i);
500
501                do {
502                        start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
503                        local_stats = *percpu_stats;
504                } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
505
506                stats.write_bytes += local_stats.write_bytes;
507                stats.write_packets += local_stats.write_packets;
508                stats.drop_bytes += local_stats.drop_bytes;
509                stats.drop_packets += local_stats.drop_packets;
510                stats.discard_bytes += local_stats.discard_bytes;
511                stats.discard_packets += local_stats.discard_packets;
512        }
513
514        seq_puts(seq,   "{\n");
515        seq_puts(seq,   \"write\": {\n");
516        seq_printf(seq, "    \"bytes\":   \"%llu\",\n", stats.write_bytes);
517        seq_printf(seq, "    \"packets\": \"%llu\"\n", stats.write_packets);
518        seq_puts(seq,   "  },\n");
519        seq_puts(seq,   \"drop\": {\n");
520        seq_printf(seq, "    \"bytes\":   \"%llu\",\n", stats.drop_bytes);
521        seq_printf(seq, "    \"packets\": \"%llu\"\n", stats.drop_packets);
522        seq_puts(seq,   "  },\n");
523        seq_puts(seq,   \"discard\": {\n");
524        seq_printf(seq, "    \"bytes\":   \"%llu\",\n", stats.discard_bytes);
525        seq_printf(seq, "    \"packets\": \"%llu\"\n", stats.discard_packets);
526        seq_puts(seq,   "  }\n");
527        seq_puts(seq,   "}\n");
528
529        return 0;
530}
531
532static int
533ksyslog_stats_seq_open(struct inode *inode, struct file *file)
534{
535        return single_open(file, ksyslog_stats_seq_show, PDE_DATA(inode));
536}
537
538static struct file_operations ksyslog_size_fops = {
539        .owner   = THIS_MODULE,
540        .open    = ksyslog_size_seq_open,
541        .read    = seq_read,
542        .llseek  = seq_lseek,
543        .release = single_release,
544};
545
546static struct file_operations ksyslog_stats_fops = {
547        .owner   = THIS_MODULE,
548        .open    = ksyslog_stats_seq_open,
549        .read    = seq_read,
550        .llseek  = seq_lseek,
551        .release = single_release,
552};
553
554static void
555ksyslog_proc_destroy(void)
556{
557        if (ksyslog_proc_queue)
558                remove_proc_entry("queue", ksyslog_procdir);
559        ksyslog_proc_queue = NULL;
560
561        if (ksyslog_proc_size)
562                remove_proc_entry("size", ksyslog_procdir);
563        ksyslog_proc_size = NULL;
564
565        if (ksyslog_proc_stats)
566                remove_proc_entry("stats", ksyslog_procdir);
567        ksyslog_proc_stats = NULL;
568
569        if (ksyslog_procdir)
570                remove_proc_entry("ksyslog", NULL);
571        ksyslog_procdir = NULL;
572}
573
574static int
575ksyslog_proc_init(void)
576{
577        ksyslog_procdir = proc_mkdir("ksyslog", NULL);
578        if (ksyslog_procdir == NULL) {
579                pr_err("ksyslog: proc_mkdir failed\n");
580                goto err;
581        }
582
583        ksyslog_proc_queue = proc_create_data("queue", S_IRUGO, ksyslog_procdir,
584                                              &ksyslog_queue_fops, &ksyslog_queue.head);
585        if (ksyslog_proc_queue == NULL) {
586                pr_err("ksyslog: proc_create(queue) failed\n");
587                goto err;
588        }
589
590        ksyslog_proc_size = proc_create("size", S_IRUGO, ksyslog_procdir,
591                                        &ksyslog_size_fops);
592        if (ksyslog_proc_size == NULL) {
593                pr_err("ksyslog: proc_create(size) failed\n");
594                goto err;
595        }
596
597        ksyslog_proc_stats = proc_create("stats", S_IRUGO, ksyslog_procdir,
598                                         &ksyslog_stats_fops);
599        if (ksyslog_proc_stats == NULL) {
600                pr_err("ksyslog: proc_create(stats) failed\n");
601                goto err;
602        }
603
604        return 0;
605
606err:
607        ksyslog_proc_destroy();
608        return -ENOMEM;
609}
610#endif
611
612static void
613ksyslog_finish(void)
614{
615        if (ksyslog_rcv_sk)
616                sock_release(ksyslog_rcv_sk);
617        ksyslog_rcv_sk = NULL;
618
619        if (ksyslog_wq) {
620                ksyslog_work_unregister();
621                destroy_workqueue(ksyslog_wq);
622        }
623        ksyslog_wq = NULL;
624
625#ifdef CONFIG_PROC_FS
626        ksyslog_proc_destroy();
627#endif
628
629        ksyslog_entry_destroy(&ksyslog_queue);
630        rcu_barrier();
631
632        ksyslog_queue_uninit(&ksyslog_queue);
633}
634
635static int __init
636ksyslog_init(void)
637{
638        int err;
639        struct sockaddr_in sin;
640
641        err = ksyslog_queue_init(&ksyslog_queue);
642        if (err)
643                goto err;
644
645#ifdef CONFIG_PROC_FS
646        err = ksyslog_proc_init();
647        if (err)
648                goto err;
649#endif
650
651        ksyslog_wq = create_workqueue("ksyslog");
652        if (ksyslog_wq == NULL) {
653                pr_err("ksyslog: create_workqueue failed\n");
654                err = -ENOMEM;
655                goto err;
656        }
657
658        INIT_DELAYED_WORK(&ksyslog_work, ksyslog_work_handler);
659
660        err = sock_create(AF_INET, SOCK_DGRAM, 0, &ksyslog_rcv_sk);
661        if (err) {
662                pr_err("ksyslog: sock_create failed\n");
663                goto err;
664        }
665
666        sin.sin_family = AF_INET;
667        sin.sin_addr.s_addr = in_aton(ksyslog_host);
668        sin.sin_port = htons(ksyslog_port);
669
670        err = kernel_bind(ksyslog_rcv_sk, (struct sockaddr *)&sin,
671                          sizeof(struct sockaddr_in));
672        if (err) {
673                pr_err("ksyslog: kernel_bind failed\n");
674                goto err;
675        }
676
677        ksyslog_work_register(ksyslog_flush_interval);
678
679        udp_sk(ksyslog_rcv_sk->sk)->encap_type = UDP_ENCAP_KSYSLOG;
680        udp_sk(ksyslog_rcv_sk->sk)->encap_rcv = ksyslog_rcv;
681        udp_encap_enable();
682
683        return 0;
684
685err:
686        ksyslog_finish();
687        return err;
688}
689
690static void __exit
691ksyslog_exit(void)
692{
693        ksyslog_finish();
694}
695
696module_init(ksyslog_init);
697module_exit(ksyslog_exit);
698
699MODULE_AUTHOR("Atzm WATANABE");
700MODULE_DESCRIPTION("In-kernel syslog receiver");
701MODULE_LICENSE("GPL");
Note: See TracBrowser for help on using the repository browser.