-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbufmon.c
570 lines (449 loc) · 16.6 KB
/
bufmon.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
/* Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
* Copyright (C) 2015 Hewlett-Packard Development Company, L.P.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <errno.h>
#include <limits.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <config.h>
#include <compiler.h>
#include "coverage.h"
#include <dynamic-string.h>
#include <fatal-signal.h>
#include <hash.h>
#include <ovsdb-idl.h>
#include <openvswitch/vlog.h>
#include <ovs-thread.h>
#include <openswitch-idl.h>
#include <poll-loop.h>
#include <shash.h>
#include <unixctl.h>
#include <util.h>
#include <vswitch-idl.h>
#include "bufmon-provider.h"
#include "latch.h"
#include "plugins.h"
#include "seq.h"
#include "smap.h"
#include "timeval.h"
VLOG_DEFINE_THIS_MODULE(bufmon);
COVERAGE_DEFINE(bufmon_reconfigure);
#define DEFAULT_COLLECTION_INTERVAL 5 //seconds
#define DEFAULT_TRIGGER_RATE_LIMIT_COUNT 60 //per Min
#define DEFAULT_TRIGGER_RATE_LIMIT_DURATION 60 //seconds
#define COUNTER_MODE_PEAK "peak"
/* OVSDB IDL used to obtain configuration. */
extern struct ovsdb_idl *idl;
/* Most recently processed IDL sequence number. */
static unsigned int idl_seqno;
static struct ovs_mutex bufmon_mutex = OVS_MUTEX_INITIALIZER;
static bufmon_system_config_t bufmon_cfg; OVS_GUARDED_BY(bufmon_mutex);
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
OVS_NO_RETURN static void *bufmon_stats_thread(void *);
static bufmon_counter_info_t *g_counter_list OVS_GUARDED_BY(bufmon_mutex) = NULL;
static int num_active_counters OVS_GUARDED_BY(bufmon_mutex) = 0;
static struct latch bufmon_latch OVS_GUARDED_BY(bufmon_mutex);
void
bufmon_init(void)
{
ovsdb_idl_omit_alert(idl, &ovsrec_bufmon_col_status);
ovsdb_idl_omit_alert(idl, &ovsrec_bufmon_col_counter_value);
ovsdb_idl_omit_alert(idl, &ovsrec_open_vswitch_col_bufmon_info);
idl_seqno = ovsdb_idl_get_seqno(idl);
} /* bufmon_init */
static void
bufmon_enable_stats(bool enable)
OVS_REQUIRES(bufmon_mutex)
{
static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
if (enable) {
if (ovsthread_once_start(&once)) {
/* Register the bufmon provider class */
plugins_bufmon_register();
/* Create struct seq to get trigger notification */
bufmon_trigger_seq_get();
/* Create the new thread to poll the stats */
ovs_thread_create("bufmon_stats", bufmon_stats_thread, NULL);
ovsthread_once_done(&once);
latch_init(&bufmon_latch);
}
}
} /* bufmon_enable_stats */
static int
bufmon_enabled_counters_count(void)
{
const struct ovsrec_bufmon *counter_row = NULL;
int count = 0;
OVSREC_BUFMON_FOR_EACH(counter_row, idl) {
if (counter_row && counter_row->enabled) {
count++;
}
}
VLOG_DBG("bufmon_enabled_counters_count %d \n", count);
return count;
} /* bufmon_enabled_counters_count */
static void
bufmon_discard_counter_list(void)
OVS_REQUIRES(bufmon_mutex)
{
int i = 0;
while (i < num_active_counters) {
bufmon_counter_info_t *counter = &g_counter_list[i];
smap_destroy(&counter->counter_vendor_specific_info);
free(counter->name);
i++;
}
free(g_counter_list);
g_counter_list = NULL;
num_active_counters = 0;
} /* bufmon_discard_counter_list */
static void
bufmon_create_counters_list(void)
{
const struct ovsrec_bufmon *counter_row = NULL;
int count = 0, i = 0;
ovs_mutex_lock(&bufmon_mutex);
if (g_counter_list != NULL) {
bufmon_discard_counter_list();
}
count = bufmon_enabled_counters_count();
if (!count) {
goto End;
}
/* Allocate the memory for all the enabled counters */
g_counter_list = xcalloc(count, sizeof *g_counter_list);
if (g_counter_list == NULL) {
VLOG_ERR("bufmon_create_counters_list alloc failed \n");
goto End;
}
/* Copy the counter info from ovs to global list */
OVSREC_BUFMON_FOR_EACH (counter_row, idl) {
if (counter_row && counter_row->enabled) {
bufmon_counter_info_t *counter = &g_counter_list[i];
smap_clone(&counter->counter_vendor_specific_info,
&counter_row->counter_vendor_specific_info);
counter->enabled = counter_row->enabled;
counter->hw_unit_id = counter_row->hw_unit_id;
counter->name = xstrdup(counter_row->name);
i++;
}
}
num_active_counters = count;
End:
ovs_mutex_unlock(&bufmon_mutex);
} /* bufmon_create_counters_list */
static void
bufmon_ovsdb_update(void)
{
const struct ovsrec_bufmon *counter_row = NULL;
const struct ovsrec_open_vswitch *system_cfg = NULL;
int i = 0;
char status[32];
enum ovsdb_idl_txn_status txn_status = TXN_ERROR;
char time_stamp[256];
static struct ovsdb_idl_txn *bufmon_stats_txn = NULL;
/* Update the counter values and status changes to the dB. */
if (!bufmon_stats_txn) {
bufmon_stats_txn = ovsdb_idl_txn_create(idl);
system_cfg = ovsrec_open_vswitch_first(idl);
/* Update the timestamp */
if (system_cfg) {
sprintf(time_stamp, "%lld", (long long)time_now());
smap_replace((struct smap *)&system_cfg->bufmon_info,
BUFMON_INFO_MAP_LAST_COLLECTION_TIMESTAMP,
(const char *) time_stamp);
ovsrec_open_vswitch_set_bufmon_info(system_cfg ,
(const struct smap *)&system_cfg->bufmon_info);
}
OVSREC_BUFMON_FOR_EACH (counter_row, idl) {
if (counter_row && counter_row->enabled) {
bufmon_counter_info_t *counter = &g_counter_list[i];
ovsrec_bufmon_set_counter_value(counter_row,
&counter->counter_value, 1);
/* Update the counter status whether poll or trigger */
if (counter->status == BUFMON_STATUS_TRIGGERED) {
strcpy(status, OVSREC_BUFMON_STATUS_TRIGGERED);
} else {
strcpy(status, OVSREC_BUFMON_STATUS_OK);
}
ovsrec_bufmon_set_status(counter_row, status);
i++;
}
}
}
/* TODO Retry OVSDB update*/
if (bufmon_stats_txn) {
/* Some OVSDB write needs to happen.*/
txn_status = ovsdb_idl_txn_commit(bufmon_stats_txn);
ovsdb_idl_txn_destroy(bufmon_stats_txn);
bufmon_stats_txn = NULL;
}
VLOG_DBG("bufmon_ovsdb_update %d \n", txn_status);
} /* bufmon_ovsdb_update */
static void
bufmon_get_current_counters_value(bool triggered)
OVS_REQUIRES(bufmon_mutex)
{
int i = 0;
/* Active counters list is empty */
if (g_counter_list == NULL || !num_active_counters) {
return;
}
bufmon_stats_get(g_counter_list, num_active_counters);
/* Update the status */
for (i =0; i < num_active_counters; i++) {
bufmon_counter_info_t *counter = &g_counter_list[i];
counter->status = (triggered ? BUFMON_STATUS_TRIGGERED
: BUFMON_STATUS_OK);
}
/* latch is set to indicate the bufmon_run to update the OVS-DB */
latch_set(&bufmon_latch);
} /* bufmon_get_current_counters_value */
/* Counter stats polling */
static void
bufmon_run_stats_update(bool triggered)
OVS_EXCLUDED(bufmon_mutex)
{
long long int now = time_msec();
static long long int next_poll_interval;
ovs_mutex_lock(&bufmon_mutex);
/* periodic/trigger collection enabled? */
if (!bufmon_cfg.enabled
|| !(bufmon_cfg.periodic_collection_enabled
|| bufmon_cfg.threshold_trigger_collection_enabled)) {
goto End;
}
/* Time for a poll? */
if (!bufmon_cfg.periodic_collection_enabled ||
((now < next_poll_interval) && !triggered)) {
goto End;
}
bufmon_get_current_counters_value(triggered);
next_poll_interval = now + (bufmon_cfg.collection_period * 1000);
End:
ovs_mutex_unlock(&bufmon_mutex);
} /* bufmon_run_stats_update */
static void
bufmon_system_config_update(const struct ovsrec_open_vswitch *row)
OVS_EXCLUDED(bufmon_mutex)
{
const char *counter_mode = NULL;
ovs_mutex_lock(&bufmon_mutex);
bufmon_cfg.enabled = smap_get_bool(&row->bufmon_config,
BUFMON_CONFIG_MAP_ENABLED, false);
counter_mode = smap_get(&row->bufmon_config,
BUFMON_CONFIG_MAP_COUNTERS_MODE);
if (counter_mode && !strcmp(counter_mode, COUNTER_MODE_PEAK)) {
bufmon_cfg.counters_mode = MODE_PEAK;
} else {
bufmon_cfg.counters_mode = MODE_CURRENT;
}
bufmon_cfg.periodic_collection_enabled =
smap_get_bool(&row->bufmon_config,
BUFMON_CONFIG_MAP_PERIODIC_COLLECTION_ENABLED,
false);
bufmon_cfg.collection_period =
smap_get_int(&row->bufmon_config,
BUFMON_CONFIG_MAP_COLLECTION_PERIOD,
DEFAULT_COLLECTION_INTERVAL);
bufmon_cfg.threshold_trigger_collection_enabled =
smap_get_bool(&row->bufmon_config,
BUFMON_CONFIG_MAP_THRESHOLD_TRIGGER_COLLECTION_ENABLED,
false);
bufmon_cfg.threshold_trigger_rate_limit =
smap_get_int(&row->bufmon_config,
BUFMON_CONFIG_MAP_TRIGGER_RATE_LIMIT,
DEFAULT_TRIGGER_RATE_LIMIT_COUNT);
bufmon_cfg.snapshot_on_threshold_trigger =
smap_get_bool(&row->bufmon_config,
BUFMON_CONFIG_MAP_SNAPSHOT_ON_THRESHOLD_TRIGGER,
false);
VLOG_DBG("update %d %d %d %d %d %d \n", bufmon_cfg.enabled,
bufmon_cfg.periodic_collection_enabled,
bufmon_cfg.collection_period,
bufmon_cfg.threshold_trigger_collection_enabled,
bufmon_cfg.threshold_trigger_rate_limit,
bufmon_cfg.snapshot_on_threshold_trigger);
bufmon_set_system_config(&bufmon_cfg);
/* Spawn bufmon thread and trigger cond signal to start */
if (bufmon_cfg.enabled) {
bufmon_enable_stats(bufmon_cfg.enabled);
xpthread_cond_signal(&cond);
}
ovs_mutex_unlock(&bufmon_mutex);
} /* bufmon_system_config_update */
static void
bufmon_counter_config_update(const struct ovsrec_bufmon *row)
{
bufmon_counter_info_t counter_info;
ovs_mutex_lock(&bufmon_mutex);
if (row->trigger_threshold) {
counter_info.trigger_threshold = *row->trigger_threshold;
} else {
counter_info.trigger_threshold = 0;
}
counter_info.counter_vendor_specific_info =
row->counter_vendor_specific_info;
counter_info.hw_unit_id = row->hw_unit_id;
counter_info.name = row->name;
counter_info.counter_value = 0;
counter_info.enabled = row->enabled;
/* Call the provider function to set the configuration */
bufmon_set_counter_config(&counter_info);
ovs_mutex_unlock(&bufmon_mutex);
}/* bufmon_system_config_update */
static void
bufmon_reconfigure(void)
{
const struct ovsrec_open_vswitch *system_cfg = NULL;
const struct ovsrec_bufmon *counter_row = NULL;
bool bufmon_modified = false;
COVERAGE_INC(bufmon_reconfigure);
system_cfg = ovsrec_open_vswitch_first(idl);
/* Buffer monitoring configuration is empty? */
if (system_cfg == NULL
|| smap_is_empty((const struct smap *)&system_cfg->bufmon_config)) {
return;
}
if (OVSREC_IDL_IS_ROW_INSERTED(system_cfg, idl_seqno)
|| OVSREC_IDL_IS_ROW_MODIFIED(system_cfg, idl_seqno)) {
bufmon_system_config_update(system_cfg);
}
/* Any changes in the bufmon counter row */
OVSREC_BUFMON_FOR_EACH (counter_row, idl) {
if (OVSREC_IDL_IS_ROW_INSERTED(counter_row, idl_seqno)
|| OVSREC_IDL_IS_ROW_MODIFIED(counter_row, idl_seqno)) {
bufmon_counter_config_update(counter_row);
bufmon_modified = true;
}
}
if (bufmon_modified) {
bufmon_create_counters_list();
}
} /* bufmon_reconfigure */
void
bufmon_run(void)
{
if (ovsdb_idl_is_lock_contended(idl)) {
return;
} else if (!ovsdb_idl_has_lock(idl)) {
return;
}
if (ovsdb_idl_get_seqno(idl) != idl_seqno) {
bufmon_reconfigure();
idl_seqno = ovsdb_idl_get_seqno(idl);
}
ovs_mutex_lock(&bufmon_mutex);
if (bufmon_cfg.enabled && latch_poll(&bufmon_latch)) {
bufmon_ovsdb_update();
}
ovs_mutex_unlock(&bufmon_mutex);
} /* bufmon_run */
void
bufmon_wait(void)
{
ovs_mutex_lock(&bufmon_mutex);
if (ovsdb_idl_has_lock(idl)) {
if (bufmon_cfg.enabled) {
latch_wait(&bufmon_latch);
}
}
ovs_mutex_unlock(&bufmon_mutex);
} /* bufmon_wait */
void
bufmon_exit(void)
{
ovs_mutex_lock(&bufmon_mutex);
bufmon_discard_counter_list();
ovs_mutex_unlock(&bufmon_mutex);
} /* bufmon_exit */
static void
bufmon_trigger_rate_limit(bool flag)
OVS_EXCLUDED(bufmon_mutex)
{
ovs_mutex_lock(&bufmon_mutex);
/* Disabling the Trigger */
if (flag) {
bufmon_trigger_enable(false);
} else if (bufmon_cfg.threshold_trigger_collection_enabled) {
/* Enable Trigger notifications */
bufmon_trigger_enable(true);
}
ovs_mutex_unlock(&bufmon_mutex);
} /* bufmon_trigger_rate_limit */
static void *
bufmon_stats_thread(void *arg OVS_UNUSED)
OVS_EXCLUDED(bufmon_mutex)
{
int trigger_reports_count = 0;
int trigger_rate_limit;
static bool trigger_disabled = false;
uint64_t cur_seqno = seq_read(bufmon_trigger_seq_get());
long long int next_poll_msec = 0;
int last_rate_limit_time = time_now();
pthread_detach(pthread_self());
for (;;) {
bool trigger_collection = false;
ovs_mutex_lock(&bufmon_mutex);
/* Condition wait if the stats collection not enabled */
while (!bufmon_cfg.enabled) {
ovs_mutex_cond_wait(&cond, &bufmon_mutex);
}
trigger_rate_limit = bufmon_cfg.threshold_trigger_rate_limit;
next_poll_msec = time_msec() + (bufmon_cfg.collection_period * 1000);
ovs_mutex_unlock(&bufmon_mutex);
do {
/* Register Timer event for periodic_collection */
poll_timer_wait_until(next_poll_msec);
/* Monitor the triggers notification in poll_block? */
seq_wait(bufmon_trigger_seq_get(), cur_seqno);
poll_block();
/* Trigger Handling */
if (cur_seqno != seq_read(bufmon_trigger_seq_get())) {
trigger_reports_count++;
cur_seqno = seq_read(bufmon_trigger_seq_get());
/* Trigger Rate limit is crossed? */
if (trigger_reports_count > trigger_rate_limit) {
/* Disable Trigger notification */
trigger_disabled = true;
bufmon_trigger_rate_limit(trigger_disabled);
} else {
/* Process the trigger notification */
trigger_collection = true;
break;
}
} else { /* Periodic poll timeout? */
/* If the rate limit disabled the trigger notification
* re-enable trigger notification after one minute
*/
if ((time_now() - last_rate_limit_time) >
DEFAULT_TRIGGER_RATE_LIMIT_DURATION) {
/* Update the trigger rate limit time stamp */
last_rate_limit_time = time_now();
/* Reset the trigger reports count */
trigger_reports_count = 0;
if (trigger_disabled) {
trigger_disabled = false;
bufmon_trigger_rate_limit(false);
}
}
break; /* Periodic stats collection */
}
} while (1);
bufmon_run_stats_update(trigger_collection);
}
} /* bufmon_stats_thread */