-
Notifications
You must be signed in to change notification settings - Fork 5
/
pfm_log.c
313 lines (261 loc) · 6.99 KB
/
pfm_log.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <errno.h>
#include <ctype.h>
#include <syslog.h>
#include <rte_common.h>
#include <rte_eal.h>
#include <rte_log.h>
#include <rte_hexdump.h>
#include <rte_errno.h>
#include "pfm_log.h"
#define MAX_APP_NAME_LEN 8
#define MAX_LOG_MSG_LEN 1000
#define MAX_TOKEN_LEN 50
#define BYTES_PER_LINE 10
#define LOGTYPE_LOG RTE_LOGTYPE_USER7
#define LOGTYPE_TRACE RTE_LOGTYPE_USER8
#define LOGPRIORITY_TRACE PFM_LOG_DEBUG
static char app_name_g[MAX_APP_NAME_LEN+1];
#ifndef CONSLOG
static ssize_t
console_log_write(__attribute__((unused)) void *c,
const char *buf, size_t size)
{
/* Syslog error levels are from 0 to 7, so subtract 1 to convert */
syslog(rte_log_cur_msg_loglevel() - 1, "%.*s", (int)size, buf);
return size;
}
#endif
void pfm_log_open( const char *app_name, const pfm_log_priority_t priority)
{
#ifndef CONSLOG
FILE *log_stream;
static cookie_io_functions_t console_log_func =
{
.read = NULL,
.write = console_log_write,
.seek = NULL,
.close = NULL
};
log_stream = fopencookie(NULL, "w+", console_log_func);
if (log_stream != NULL)
{
rte_openlog_stream(log_stream);
}
#endif
strncpy(app_name_g,app_name,MAX_APP_NAME_LEN);
app_name_g[MAX_APP_NAME_LEN] = 0;
rte_log_set_global_level(priority);
rte_log_set_level (LOGTYPE_LOG, priority);
rte_log_set_level (LOGTYPE_TRACE, PFM_LOG_DEBUG);
return;
}
void pfm_log_close(void)
{
return;
}
void pfmlog_msg( const char *src_file_name,
const int line_no,
const char *func_name,
const pfm_log_priority_t priority,
const char *fmt, ...)
{
char msg[MAX_LOG_MSG_LEN];
va_list ap;
snprintf(msg,MAX_LOG_MSG_LEN,"%s|%s|%d|%s()|%s\n",
app_name_g,basename(src_file_name),line_no,func_name,fmt);
va_start (ap, fmt);
rte_vlog(priority,LOGTYPE_LOG,msg,ap);
va_end (ap);
return;
}
void pfmlog_std_err( const char *src_file_name,
const int line_no,
const char *func_name,
const pfm_log_priority_t priority,
const char *fmt, ...)
{
char msg[MAX_LOG_MSG_LEN];
va_list ap;
snprintf(msg,MAX_LOG_MSG_LEN,"%s|%s|%d|%s()|err=%s(%d)|%s\n",
app_name_g,basename(src_file_name),line_no,func_name,
strerror(errno),errno,fmt);
va_start (ap, fmt);
rte_vlog(priority,LOGTYPE_LOG,msg,ap);
va_end (ap);
return;
}
void pfmlog_rte_err( const char *src_file_name,
const int line_no,
const char *func_name,
const pfm_log_priority_t priority,
const char *fmt, ...)
{
char msg[MAX_LOG_MSG_LEN];
va_list ap;
snprintf(msg,MAX_LOG_MSG_LEN,
"%s|%s|%d|%s()|err=%s(%d)|rteErr=%s(%d)|%s\n",
app_name_g,basename(src_file_name),line_no,
func_name,strerror(errno),
errno,rte_strerror(rte_errno),rte_errno,fmt);
va_start (ap, fmt);
rte_vlog(priority,LOGTYPE_LOG,msg,ap);
va_end (ap);
return;
}
void pfmtrace_msg( const char *src_file_name,
const int line_no,
const char *func_name,
const char *fmt, ...)
{
char msg[MAX_LOG_MSG_LEN];
va_list ap;
snprintf(msg,MAX_LOG_MSG_LEN,"%s|%s|%d|%s()|%s\n",
app_name_g,basename(src_file_name),line_no,func_name,fmt);
va_start (ap, fmt);
rte_vlog(LOGPRIORITY_TRACE, LOGTYPE_TRACE, msg, ap);
va_end (ap);
}
void pfmtrace_pkt( const char *src_file_name,
const int line_no,
const char *func_name,
const unsigned char *pkt,
const int pkt_len,
const char *fmt, ...)
{
char msg[MAX_LOG_MSG_LEN];
char token[MAX_TOKEN_LEN];
va_list ap;
int idx;
int j;
snprintf(msg,MAX_LOG_MSG_LEN,
"%s|%s|%d|%s()|pkt=%p,pkt_len=%d|%s\n",
app_name_g,basename(src_file_name),
line_no,func_name,pkt,pkt_len,fmt);
va_start (ap, fmt);
rte_vlog(LOGPRIORITY_TRACE,LOGTYPE_TRACE,msg,ap);
va_end (ap);
strcpy(msg," ");
for (idx=0; idx < pkt_len; idx++)
{
if (0 == (idx % BYTES_PER_LINE))
{
snprintf(token,MAX_TOKEN_LEN,"%04d:",idx);
strncat(msg,token,MAX_LOG_MSG_LEN);
}
snprintf(token,MAX_TOKEN_LEN,"%02X:",pkt[idx]);
strncat(msg,token,MAX_LOG_MSG_LEN);
if (9 == (idx % 10 ))
{
strncat(msg," ",MAX_LOG_MSG_LEN);
if ((BYTES_PER_LINE-1) == (idx % BYTES_PER_LINE))
{
strncat(msg," ",MAX_LOG_MSG_LEN);
for(j=(idx-(BYTES_PER_LINE-1));
j <= idx; j++)
{
if (isprint(pkt[j]))
{
snprintf(token,
MAX_TOKEN_LEN,
"%c",pkt[j]);
strncat(msg,token,
MAX_LOG_MSG_LEN);
}
else
{
strncat(msg,".",
MAX_LOG_MSG_LEN);
}
}
rte_log(LOGPRIORITY_TRACE,LOGTYPE_TRACE,
"%s\n",msg);
strncpy(msg," ", MAX_LOG_MSG_LEN);
}
}
}
for (j=0; j < (BYTES_PER_LINE - (idx % BYTES_PER_LINE)); j++)
{
strncat(msg," ", MAX_LOG_MSG_LEN);
if (9 == (j % 10 ))
strncat(msg," ", MAX_LOG_MSG_LEN);
}
strncat(msg," ", MAX_LOG_MSG_LEN);
j = idx - (idx % BYTES_PER_LINE);
for(; j < idx; j++)
{
if (isprint(pkt[j]))
{
snprintf(token,MAX_TOKEN_LEN,"%c",pkt[j]);
strncat(msg,token, MAX_LOG_MSG_LEN);
}
else
{
strncat(msg,".", MAX_LOG_MSG_LEN);
}
}
rte_log(LOGPRIORITY_TRACE,LOGTYPE_TRACE, "%s\n",msg);
return;
}
void pfmtrace_pkt_hdr( const char *src_file_name,
const int line_no,
const char *func_name,
const unsigned char *pkt,
const int pkt_len,
const char *fmt, ...)
{
char msg[MAX_LOG_MSG_LEN];
char token[MAX_TOKEN_LEN];
unsigned short ether_type;
unsigned short tci;
va_list ap;
int idx;
snprintf(msg,MAX_LOG_MSG_LEN,
"%s|%s|%d|%s()|pkt=%p,pkt_len=%d|%s\n",
app_name_g,basename(src_file_name),line_no,
func_name,pkt, pkt_len,fmt);
va_start (ap, fmt);
rte_vlog(LOGPRIORITY_TRACE,LOGTYPE_TRACE,msg,ap);
va_end (ap);
if (14 > pkt_len)
{
rte_log(LOGPRIORITY_TRACE,LOGTYPE_TRACE,
"pkt_len=%d too short to print header\n",pkt_len);
return;
}
snprintf(msg,MAX_TOKEN_LEN,
"DstMAC=%02X:%02X:%02X:%02X:%02X:%02X "
"SrcMAC=%02X:%02X:%02X:%02X:%02X:%02X ",
pkt[0],pkt[1],pkt[2],pkt[3],pkt[4],pkt[5],
pkt[6],pkt[7],pkt[8],pkt[9],pkt[10],pkt[11]);
ether_type = ((pkt[12] << 8) | pkt[13]);
idx = 14;
if ( 0x8100 == ether_type)
{
tci = ((pkt[14] << 8) | pkt[15]);
snprintf(token,MAX_TOKEN_LEN,"TPID=%d, TCI=%d",
ether_type,tci);
strncat(msg,token, MAX_LOG_MSG_LEN);
ether_type = ((pkt[16] << 8) | pkt[17]);
idx = 18;
}
snprintf(token,MAX_TOKEN_LEN," EtherType=%d",ether_type);
strncat(msg,token, MAX_LOG_MSG_LEN);
if ((20 + idx) <= pkt_len)
{
snprintf(token,MAX_TOKEN_LEN," SrcIP=%d.%d.%d.%d",
pkt[idx+12],pkt[idx+13],pkt[idx+14],pkt[idx+15]);
strncat(msg,token, MAX_LOG_MSG_LEN);
snprintf(token,MAX_TOKEN_LEN," DstIP=%d.%d.%d.%d",
pkt[idx+16],pkt[idx+17],pkt[idx+18],pkt[idx+19]);
strncat(msg,token, MAX_LOG_MSG_LEN);
snprintf(token,MAX_TOKEN_LEN," Proto=%d",pkt[idx+9]);
strncat(msg,token, MAX_LOG_MSG_LEN);
}
rte_log(LOGPRIORITY_TRACE,LOGTYPE_TRACE,"%s\n",msg);
return;
}
/* End of file */