1 | /* |
---|
2 | * ksyslog: In-kernel syslog receiver |
---|
3 | * Copyright(C) 2013 Atzm WATANABE All rights reserved |
---|
4 | * Distributed under the GPL |
---|
5 | */ |
---|
6 | |
---|
7 | #ifndef KSYSLOG_H |
---|
8 | #define KSYSLOG_H |
---|
9 | |
---|
10 | #define UDP_ENCAP_KSYSLOG 16 |
---|
11 | |
---|
12 | enum ksyslog_facility { |
---|
13 | KSYSLOG_F_KERN, |
---|
14 | KSYSLOG_F_USER, |
---|
15 | KSYSLOG_F_MAIL, |
---|
16 | KSYSLOG_F_DAEMON, |
---|
17 | KSYSLOG_F_AUTH, |
---|
18 | KSYSLOG_F_SYSLOG, |
---|
19 | KSYSLOG_F_LPR, |
---|
20 | KSYSLOG_F_NEWS, |
---|
21 | KSYSLOG_F_UUCP, |
---|
22 | KSYSLOG_F_CRON, |
---|
23 | KSYSLOG_F_AUTHPRIV, |
---|
24 | KSYSLOG_F_FTP, |
---|
25 | KSYSLOG_F_NTP, |
---|
26 | KSYSLOG_F_AUDIT, |
---|
27 | KSYSLOG_F_ALERT, |
---|
28 | KSYSLOG_F_CRONPRIV, /* ? */ |
---|
29 | KSYSLOG_F_LOCAL0, |
---|
30 | KSYSLOG_F_LOCAL1, |
---|
31 | KSYSLOG_F_LOCAL2, |
---|
32 | KSYSLOG_F_LOCAL3, |
---|
33 | KSYSLOG_F_LOCAL4, |
---|
34 | KSYSLOG_F_LOCAL5, |
---|
35 | KSYSLOG_F_LOCAL6, |
---|
36 | KSYSLOG_F_LOCAL7, |
---|
37 | __KSYSLOG_F_MAX, |
---|
38 | }; |
---|
39 | |
---|
40 | enum ksyslog_severity { |
---|
41 | KSYSLOG_S_EMERG, |
---|
42 | KSYSLOG_S_ALERT, |
---|
43 | KSYSLOG_S_CRIT, |
---|
44 | KSYSLOG_S_ERR, |
---|
45 | KSYSLOG_S_WARN, |
---|
46 | KSYSLOG_S_NOTICE, |
---|
47 | KSYSLOG_S_INFO, |
---|
48 | KSYSLOG_S_DEBUG, |
---|
49 | __KSYSLOG_S_MAX, |
---|
50 | }; |
---|
51 | |
---|
52 | struct ksyslog_entry { |
---|
53 | struct list_head list; |
---|
54 | struct timeval tv; |
---|
55 | |
---|
56 | unsigned long time; |
---|
57 | unsigned int priority; |
---|
58 | enum ksyslog_facility facility; |
---|
59 | enum ksyslog_severity severity; |
---|
60 | |
---|
61 | union { |
---|
62 | __u8 addr8[4]; |
---|
63 | __be32 addr32; |
---|
64 | } daddr, saddr; |
---|
65 | |
---|
66 | __be16 dport, sport; |
---|
67 | |
---|
68 | size_t length; |
---|
69 | char *data; |
---|
70 | |
---|
71 | struct rcu_head rcu; |
---|
72 | }; |
---|
73 | |
---|
74 | struct ksyslog_stats { |
---|
75 | u64 write_bytes; |
---|
76 | u64 write_packets; |
---|
77 | u64 drop_bytes; |
---|
78 | u64 drop_packets; |
---|
79 | u64 discard_bytes; |
---|
80 | u64 discard_packets; |
---|
81 | struct u64_stats_sync sync; |
---|
82 | }; |
---|
83 | |
---|
84 | struct ksyslog_queue { |
---|
85 | struct list_head head; |
---|
86 | spinlock_t lock; |
---|
87 | atomic64_t size; |
---|
88 | struct ksyslog_stats __percpu *stats; |
---|
89 | }; |
---|
90 | |
---|
91 | static inline void |
---|
92 | ksyslog_stats_add_write(struct ksyslog_queue *queue, unsigned int len) |
---|
93 | { |
---|
94 | struct ksyslog_stats *stats; |
---|
95 | |
---|
96 | stats = per_cpu_ptr(queue->stats, smp_processor_id()); |
---|
97 | u64_stats_update_begin(&stats->sync); |
---|
98 | stats->write_bytes += len; |
---|
99 | stats->write_packets++; |
---|
100 | u64_stats_update_end(&stats->sync); |
---|
101 | } |
---|
102 | |
---|
103 | static inline void |
---|
104 | ksyslog_stats_add_drop(struct ksyslog_queue *queue, unsigned int len) |
---|
105 | { |
---|
106 | struct ksyslog_stats *stats; |
---|
107 | |
---|
108 | stats = per_cpu_ptr(queue->stats, smp_processor_id()); |
---|
109 | u64_stats_update_begin(&stats->sync); |
---|
110 | stats->drop_bytes += len; |
---|
111 | stats->drop_packets++; |
---|
112 | u64_stats_update_end(&stats->sync); |
---|
113 | } |
---|
114 | |
---|
115 | static inline void |
---|
116 | ksyslog_stats_add_discard(struct ksyslog_queue *queue, unsigned int len) |
---|
117 | { |
---|
118 | struct ksyslog_stats *stats; |
---|
119 | |
---|
120 | stats = per_cpu_ptr(queue->stats, smp_processor_id()); |
---|
121 | u64_stats_update_begin(&stats->sync); |
---|
122 | stats->discard_bytes += len; |
---|
123 | stats->discard_packets++; |
---|
124 | u64_stats_update_end(&stats->sync); |
---|
125 | } |
---|
126 | |
---|
127 | static inline const char * |
---|
128 | ksyslog_facility_str(const enum ksyslog_facility facility) |
---|
129 | { |
---|
130 | switch (facility) { |
---|
131 | case KSYSLOG_F_KERN: return "kern"; |
---|
132 | case KSYSLOG_F_USER: return "user"; |
---|
133 | case KSYSLOG_F_MAIL: return "mail"; |
---|
134 | case KSYSLOG_F_DAEMON: return "daemon"; |
---|
135 | case KSYSLOG_F_AUTH: return "auth"; |
---|
136 | case KSYSLOG_F_SYSLOG: return "syslog"; |
---|
137 | case KSYSLOG_F_LPR: return "lpr"; |
---|
138 | case KSYSLOG_F_NEWS: return "news"; |
---|
139 | case KSYSLOG_F_UUCP: return "uucp"; |
---|
140 | case KSYSLOG_F_CRON: return "cron"; |
---|
141 | case KSYSLOG_F_AUTHPRIV: return "authpriv"; |
---|
142 | case KSYSLOG_F_FTP: return "ftp"; |
---|
143 | case KSYSLOG_F_NTP: return "ntp"; |
---|
144 | case KSYSLOG_F_AUDIT: return "audit"; |
---|
145 | case KSYSLOG_F_ALERT: return "alert"; |
---|
146 | case KSYSLOG_F_CRONPRIV: return "cronpriv"; |
---|
147 | case KSYSLOG_F_LOCAL0: return "local0"; |
---|
148 | case KSYSLOG_F_LOCAL1: return "local1"; |
---|
149 | case KSYSLOG_F_LOCAL2: return "local2"; |
---|
150 | case KSYSLOG_F_LOCAL3: return "local3"; |
---|
151 | case KSYSLOG_F_LOCAL4: return "local4"; |
---|
152 | case KSYSLOG_F_LOCAL5: return "local5"; |
---|
153 | case KSYSLOG_F_LOCAL6: return "local6"; |
---|
154 | case KSYSLOG_F_LOCAL7: return "local7"; |
---|
155 | default: return "unknown"; |
---|
156 | } |
---|
157 | } |
---|
158 | |
---|
159 | static inline const char * |
---|
160 | ksyslog_severity_str(const enum ksyslog_severity severity) |
---|
161 | { |
---|
162 | switch (severity) { |
---|
163 | case KSYSLOG_S_EMERG: return "emerg"; |
---|
164 | case KSYSLOG_S_ALERT: return "alert"; |
---|
165 | case KSYSLOG_S_CRIT: return "crit"; |
---|
166 | case KSYSLOG_S_ERR: return "err"; |
---|
167 | case KSYSLOG_S_WARN: return "warn"; |
---|
168 | case KSYSLOG_S_NOTICE: return "notice"; |
---|
169 | case KSYSLOG_S_INFO: return "info"; |
---|
170 | case KSYSLOG_S_DEBUG: return "debug"; |
---|
171 | default: return "unknown"; |
---|
172 | } |
---|
173 | } |
---|
174 | |
---|
175 | static inline unsigned int |
---|
176 | ksyslog_month_num(const char *month) |
---|
177 | { |
---|
178 | const u32 m = *((u32 *)month); |
---|
179 | |
---|
180 | if (m == *((u32 *)"Jan")) |
---|
181 | return 1; |
---|
182 | else if (m == *((u32 *)"Feb")) |
---|
183 | return 2; |
---|
184 | else if (m == *((u32 *)"Mar")) |
---|
185 | return 3; |
---|
186 | else if (m == *((u32 *)"Apr")) |
---|
187 | return 4; |
---|
188 | else if (m == *((u32 *)"May")) |
---|
189 | return 5; |
---|
190 | else if (m == *((u32 *)"Jun")) |
---|
191 | return 6; |
---|
192 | else if (m == *((u32 *)"Jul")) |
---|
193 | return 7; |
---|
194 | else if (m == *((u32 *)"Aug")) |
---|
195 | return 8; |
---|
196 | else if (m == *((u32 *)"Sep")) |
---|
197 | return 9; |
---|
198 | else if (m == *((u32 *)"Oct")) |
---|
199 | return 10; |
---|
200 | else if (m == *((u32 *)"Nov")) |
---|
201 | return 11; |
---|
202 | else if (m == *((u32 *)"Dec")) |
---|
203 | return 12; |
---|
204 | return 0; |
---|
205 | } |
---|
206 | |
---|
207 | #endif |
---|