-
Notifications
You must be signed in to change notification settings - Fork 0
/
sv_timestamp_logger.c
262 lines (224 loc) · 8.01 KB
/
sv_timestamp_logger.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
/**
* \file sv_timestamp_logger.c
* \brief Tool to timstamp received SV pcaps
*
* \copyright Copyright (C) 2024 Savoir-faire Linux, Inc
* \license SPDX-License-Identifier: Apache-2.0
*/
#include <linux/if_ether.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <getopt.h>
#include <stdlib.h>
#include <signal.h>
#include <arpa/inet.h>
#include <netinet/if_ether.h>
#include <time.h>
#include <sched.h>
#include "sv_timestamp_logger.h"
#include "lib/sv_parser/sv_parser.h"
#include "lib/sv_monitor.h"
// Manual define is needed for the arm version of the container
# define TIMESPEC_TO_TIMEVAL(tv, ts) { \
(tv)->tv_sec = (ts)->tv_sec; \
(tv)->tv_usec = (ts)->tv_nsec / 1000; \
}
static const struct option long_options[] = {
{ "help", no_argument, 0, 'h' },
{ "device", required_argument, 0, 'd' },
{ "stream", required_argument, 0, 's'},
{ "filename", required_argument, 0, 'f'},
{ "hardware_timestamping", no_argument, 0, 't' },
{ "first_SV_cnt", required_argument, 0, 'c'},
{ "max_SV_cnt", required_argument, 0, 'm' },
{ "log_only_SV_cnt_0", no_argument, 0, 'l'},
{ 0, 0, 0, 0 }
};
static const char * const HELP_MSG_FMT =
"Usage: %s <-d|--device <device>> [s|--stream <name>] [-t|--hardware_timestamping] [-f| --filename] [--first_SV_cnt <cnt>] [--max_SV_cnt <cnt>] [-l --log_only_SV_cnt_0]\n"
"\n"
"Get the timestamp of sample values.\n"
"\n"
"Options:\n"
"\tdevice: the device to listen on.\n"
"\tstream: the name of the stream on which to look for sample values. If not set, use all streams\n"
"\tfilename: the file to write the timestamps. If not set, use stdout\n"
"\thardware_timestamping: enable NIC hardware timestamping (PTP must be setup)\n"
"\tfirst_SV_cnt: counter of the first SV to be sent. If not set, SV drop will not be computed.\n"
"\tmax_SV_cnt: max counter of SV in the chosen IEC 61850 configuration. If not set, SV drop will not be computed.\n"
"\tlog_only_SV_cnt_0: if set, log only the SV number 0.\n"
;
/* Global Variables */
static struct sv_timestamp_logger_opts opts;
static struct sv_monitor * volatile monitor; // volatile because may be used
// from a signal handler
static FILE * SV_timestamp_file;
static struct SV_payload *sv;
static int compute_SV_drop;
static int current_SV_cnt;
static int total_SV_drop;
static int iteration_nb;
static void print_help(const char* program_name)
{
printf(HELP_MSG_FMT, program_name);
}
static int parse_args(int argc, char *argv[])
{
int opt;
int long_index = 0;
// default values
opts.device = NULL;
opts.stream = NULL;
opts.SV_filename = "/dev/stdout";
opts.first_SV_cnt = 0;
opts.max_SV_cnt = 0;
opts.log_only_SV_cnt_0 = 0;
while ((opt = getopt_long(argc, argv, "htld:s:n:f:c:m:", long_options,
&long_index)) != -1) {
switch (opt) {
case 'h':
print_help(argv[0]);
return 1;
case 'd':
opts.device = optarg;
break;
case 't':
opts.enable_hardware_ts = 1;
break;
case 's':
opts.stream = optarg;
break;
case 'f':
opts.SV_filename = optarg;
break;
case 'c':
opts.first_SV_cnt = atoi(optarg);
break;
case 'm':
opts.max_SV_cnt = atoi(optarg);
break;
case 'l':
opts.log_only_SV_cnt_0 = 1;
break;
case '?':
fprintf(stderr, "Invalid option: -%c\n", optopt);
print_help(argv[0]);
return 1;
}
}
iteration_nb = 0;
current_SV_cnt = opts.first_SV_cnt - 1;
if (opts.first_SV_cnt != 0 || opts.max_SV_cnt != 0 ) {
compute_SV_drop = 1;
}
if(opts.device != NULL) {
return 0;
} else {
fprintf(stderr, "No device provided\n");
return 1;
}
}
static void stop_capture_loop()
{
if (compute_SV_drop) printf("SV drop: %d\n", total_SV_drop);
if(monitor) {
stop_monitor(monitor);
}
}
static int get_ts(struct timeval* tv) {
int ret = 0;
struct timespec ts;
if (clock_gettime(CLOCK_REALTIME, &ts) == -1) {
perror("clock_gettime");
ret = -1;
}
TIMESPEC_TO_TIMEVAL(tv, &ts);
return ret;
}
static int is_vlan(const uint8_t *packet)
{
const struct ethhdr *eth_header = (const struct ethhdr *)packet;
if (ntohs(eth_header->h_proto) == 0x8100) { // check if vlan Tagging EtherType
return 1;
} else {
return 0;
}
}
static void gather_records(const struct pcap_pkthdr *header,
const uint8_t *packet)
{
if(is_vlan(packet)) {
parse_SV_payload(packet
+ sizeof(struct ethhdr) // skip Ethernet header
+ 4*sizeof(uint8_t), // skip vlan PCP/DEI
sv);
} else {
parse_SV_payload(packet
+ sizeof(struct ethhdr),
sv);
}
if (compute_SV_drop) {
int gap = (sv->seqASDU[0].smpCnt - current_SV_cnt + opts.max_SV_cnt) % opts.max_SV_cnt;
if (gap > 1) total_SV_drop += gap - 1;
}
if (opts.stream == NULL
|| (opts.stream != NULL
&& !strcmp(sv->seqASDU[0].svID, opts.stream))) {
if (sv->seqASDU[0].smpCnt < current_SV_cnt) iteration_nb++;
current_SV_cnt = sv->seqASDU[0].smpCnt;
}
struct timeval timestamp;
if(opts.enable_hardware_ts) {
timestamp = header->ts;
} else {
get_ts(×tamp);
}
if ((opts.log_only_SV_cnt_0 && sv->seqASDU[0].smpCnt == 0)
|| (!opts.log_only_SV_cnt_0)) {
if (opts.stream == NULL
|| (opts.stream != NULL
&& !strcmp(sv->seqASDU[0].svID, opts.stream))) {
fprintf(SV_timestamp_file, "%d:%s:%d:%ld\n",
iteration_nb,
sv->seqASDU[0].svID,
sv->seqASDU[0].smpCnt,
(timestamp.tv_sec * 1000 * 1000) + (timestamp.tv_usec));
}
}
}
int main(int argc, char *argv[]) {
struct sched_param sp = { .sched_priority = 1};
int ret;
ret = parse_args(argc, argv);
if(ret) return ret;
ret = sched_setscheduler(0, SCHED_FIFO, &sp);
if (ret == -1) {
perror("sched_setscheduler");
return 1;
}
monitor = init_monitor(opts.device, opts.enable_hardware_ts);
if(!monitor) {
ret = 1;
goto exit;
}
sv = create_SV();
set_sv_handler(monitor, gather_records);
SV_timestamp_file = fopen(opts.SV_filename, "w");
if(!SV_timestamp_file) {
ret = 2;
goto cleanup_SV_file;
}
signal(SIGSTOP, stop_capture_loop);
signal(SIGTERM, stop_capture_loop);
signal(SIGINT, stop_capture_loop);
signal(SIGPIPE, stop_capture_loop);
ret = run_monitor(monitor);
cleanup_SV_file:
if (opts.SV_filename) fclose(SV_timestamp_file);
cleanup_monitor:
free_monitor(monitor);
exit:
free_SV(sv);
return ret;
}