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
// Copyright (C) 2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <config.h>

#include <perfmon_config.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <dhcp/dhcp6.h>
#include <dhcp/pkt4.h>
#include <dhcp/pkt6.h>

using namespace isc;
using namespace isc::data;
using namespace isc::dhcp;
using namespace boost::posix_time;

namespace isc {
namespace perfmon {

const data::SimpleKeywords
DurationKeyParser::CONFIG_KEYWORDS =
{
    {"query-type",        Element::string},
    {"response-type",     Element::string},
    {"start-event",       Element::string},
    {"stop-event",        Element::string},
    {"subnet-id",         Element::integer}
};

uint16_t
DurationKeyParser::getMessageNameType4(const std::string& name) {
    static std::map<std::string, uint16_t> name_type_map = {
        {"",                        DHCP_NOTYPE},
        {"*",                       DHCP_NOTYPE},
        {"DHCPDISCOVER",            DHCPDISCOVER},
        {"DHCPOFFER",               DHCPOFFER},
        {"DHCPREQUEST",             DHCPREQUEST},
        {"DHCPDECLINE",             DHCPDECLINE},
        {"DHCPACK",                 DHCPACK},
        {"DHCPNAK",                 DHCPNAK},
        {"DHCPRELEASE",             DHCPRELEASE},
        {"DHCPINFORM",              DHCPINFORM},
        {"DHCPLEASEQUERY",          DHCPLEASEQUERY},
        {"DHCPLEASEUNASSIGNED",     DHCPLEASEUNASSIGNED},
        {"DHCPLEASEUNKNOWN",        DHCPLEASEUNKNOWN},
        {"DHCPLEASEACTIVE",         DHCPLEASEACTIVE},
        {"DHCPBULKLEASEQUERY",      DHCPBULKLEASEQUERY},
        {"DHCPLEASEQUERYDONE",      DHCPLEASEQUERYDONE},
        {"DHCPLEASEQUERYSTATUS",    DHCPLEASEQUERYSTATUS},
        {"DHCPTLS",                 DHCPTLS}
    };

    try {
        const auto& found = name_type_map.at(name);
        return (found);
    } catch (const std::out_of_range& ex) {
        isc_throw(BadValue, "'" << name << "' is not a valid DHCP message type");
    }
}

uint16_t
DurationKeyParser::getMessageNameType6(const std::string& name) {
    static std::map<std::string, uint16_t> name_type_map = {
        {"",                        DHCPV6_NOTYPE},
        {"*",                       DHCPV6_NOTYPE},
        {"SOLICIT",                 DHCPV6_SOLICIT},
        {"ADVERTISE",               DHCPV6_ADVERTISE},
        {"REQUEST",                 DHCPV6_REQUEST},
        {"CONFIRM",                 DHCPV6_CONFIRM},
        {"RENEW",                   DHCPV6_RENEW},
        {"REBIND",                  DHCPV6_REBIND},
        {"REPLY",                   DHCPV6_REPLY},
        {"RELEASE",                 DHCPV6_RELEASE},
        {"DECLINE",                 DHCPV6_DECLINE},
        {"RECONFIGURE",             DHCPV6_RECONFIGURE},
        {"INFORMATION_REQUEST",     DHCPV6_INFORMATION_REQUEST},
        {"RELAY_FORW",              DHCPV6_RELAY_FORW},
        {"RELAY_REPL",              DHCPV6_RELAY_REPL},
        {"LEASEQUERY",              DHCPV6_LEASEQUERY},
        {"LEASEQUERY_REPLY",        DHCPV6_LEASEQUERY_REPLY},
        {"LEASEQUERY_DONE",         DHCPV6_LEASEQUERY_DONE},
        {"LEASEQUERY_DATA",         DHCPV6_LEASEQUERY_DATA},
        {"RECONFIGURE_REQUEST",     DHCPV6_RECONFIGURE_REQUEST},
        {"RECONFIGURE_REPLY",       DHCPV6_RECONFIGURE_REPLY},
        {"DHCPV4_QUERY",            DHCPV6_DHCPV4_QUERY},
        {"DHCPV4_RESPONSE",         DHCPV6_DHCPV4_RESPONSE},
        {"ACTIVELEASEQUERY",        DHCPV6_ACTIVELEASEQUERY},
        {"STARTTLS",                DHCPV6_STARTTLS},
        {"BNDUPD",                  DHCPV6_BNDUPD},
        {"BNDREPLY",                DHCPV6_BNDREPLY},
        {"POOLREQ",                 DHCPV6_POOLREQ},
        {"POOLRESP",                DHCPV6_POOLRESP},
        {"UPDREQ",                  DHCPV6_UPDREQ},
        {"UPDREQALL",               DHCPV6_UPDREQALL},
        {"UPDDONE",                 DHCPV6_UPDDONE},
        {"CONNECT",                 DHCPV6_CONNECT},
        {"CONNECTREPLY",            DHCPV6_CONNECTREPLY},
        {"DISCONNECT",              DHCPV6_DISCONNECT},
        {"STATE",                   DHCPV6_STATE},
        {"CONTACT",                 DHCPV6_CONTACT}
    };

    try {
        const auto& found = name_type_map.at(name);
        return (found);
    } catch (const std::out_of_range& ex) {
        isc_throw(BadValue, "'" << name << "' is not a valid DHCPV6 message type");
    }
}

uint16_t
DurationKeyParser::getMessageType(data::ConstElementPtr config,
                                  uint16_t family,
                                  const std::string& param_name,
                                  bool required /*= true */) {
    // Parse members.
    uint16_t msg_type = 0;
    ConstElementPtr elem = config->get(param_name);
    if (elem) {
        try {
            msg_type = (family == AF_INET ? getMessageNameType4(elem->stringValue())
                                          : getMessageNameType6(elem->stringValue()));
        } catch (const std::exception& ex) {
            isc_throw(DhcpConfigError, "'" << param_name << "' parameter is invalid, " << ex.what());
        }
    } else {
        if (required) {
            isc_throw(DhcpConfigError, "'" << param_name << "' parameter is required");
        }
    }

    return (msg_type);
}

DurationKeyPtr
DurationKeyParser::parse(data::ConstElementPtr config, uint16_t family) {
    // Note checkKeywords() will throw DhcpConfigError if there is a problem.
    SimpleParser::checkKeywords(CONFIG_KEYWORDS, config);

    // Parse members.
    auto query_type = getMessageType(config, family, "query-type");

    auto response_type = getMessageType(config, family, "response-type");

    std::string start_event;
    ConstElementPtr elem = config->get("start-event");
    if (elem) {
        start_event = elem->stringValue();
    } else {
        isc_throw(DhcpConfigError, "'start-event' parameter is required");
    }

    std::string stop_event;
    elem = config->get("stop-event");
    if (elem) {
        stop_event = elem->stringValue();
    } else {
        isc_throw(DhcpConfigError, "'stop-event' parameter is required");
    }

    SubnetID subnet_id = SUBNET_ID_GLOBAL;
    elem = config->get("subnet-id");
    if (elem) {
        subnet_id = static_cast<SubnetID>(elem->intValue());
    }

    return (DurationKeyPtr(new DurationKey(family, query_type, response_type,
                                           start_event, stop_event, subnet_id)));
}

data::ElementPtr
DurationKeyParser::toElement(DurationKeyPtr key) {
    if (!key) {
        isc_throw(BadValue, "DurationKeyParser::toElement() - key is empty");
    }

    ElementPtr map = Element::createMap();
    if (key->getFamily() == AF_INET) {
        map->set("query-type", Element::create(Pkt4::getName(key->getQueryType())));
        map->set("response-type", Element::create(Pkt4::getName(key->getResponseType())));
    } else {
        map->set("query-type", Element::create(Pkt6::getName(key->getQueryType())));
        map->set("response-type", Element::create(Pkt6::getName(key->getResponseType())));
    }

    map->set("start-event", Element::create(key->getStartEventLabel()));
    map->set("stop-event", Element::create(key->getStopEventLabel()));
    map->set("subnet-id", Element::create(static_cast<long long>(key->getSubnetId())));
    return (map);
}

const data::SimpleKeywords
AlarmParser::CONFIG_KEYWORDS =
{
    {"duration-key",    Element::map},
    {"enable-alarm",    Element::boolean},
    {"high-water-ms",   Element::integer},
    {"low-water-ms",    Element::integer}
};

AlarmPtr
AlarmParser::parse(data::ConstElementPtr config, uint16_t family) {
    // Note checkKeywords() will throw DhcpConfigError if there is a problem.
    SimpleParser::checkKeywords(CONFIG_KEYWORDS, config);

    // First parse the duration-key.
    ConstElementPtr elem = config->get("duration-key");
    if (!elem) {
        isc_throw(DhcpConfigError, "'duration-key'" <<" parameter is required");
    }

    DurationKeyPtr key = DurationKeyParser::parse(elem, family);

    // Parse scalar members.
    elem = config->get("enable-alarm");
    bool enable_alarm = (elem ? elem->boolValue() : true);

    elem = config->get("high-water-ms");
    uint64_t high_water_ms = 0;
    if (elem) {
        int64_t value = elem->intValue();
        if (value <= 0) {
            isc_throw(DhcpConfigError, "high-water-ms: '"
                       << value << "', must be greater than 0");
        }

        high_water_ms = value;
    } else {
        isc_throw(DhcpConfigError, "'high-water-ms'" <<" parameter is required");
    }

    elem = config->get("low-water-ms");
    uint64_t low_water_ms = 0;
    if (elem) {
        int64_t value = elem->intValue();
        if (value <= 0) {
            isc_throw(DhcpConfigError, "low-water-ms: '"
                       << value << "', must be greater than 0");
        }

        low_water_ms = value;
    } else {
        isc_throw(DhcpConfigError, "'low-water-ms'" <<" parameter is required");
    }

    if (low_water_ms >= high_water_ms) {
        isc_throw(DhcpConfigError, "'low-water-ms': " << low_water_ms
                   << ", must be less than 'high-water-ms': " << high_water_ms);
    }

    return (AlarmPtr(new Alarm(*key, milliseconds(low_water_ms),
                               milliseconds(high_water_ms), enable_alarm)));
}

const data::SimpleKeywords
PerfMonConfig::CONFIG_KEYWORDS =
{
    {"enable-monitoring",      Element::boolean},
    {"interval-width-secs",    Element::integer},
    {"stats-mgr-reporting",    Element::boolean},
    {"alarm-report-secs",      Element::integer},
    {"alarms",                 Element::list}
};

PerfMonConfig::PerfMonConfig(uint16_t family)
    : family_(family),
      enable_monitoring_(false),
      interval_width_secs_(60),
      stats_mgr_reporting_(true),
      alarm_report_secs_(300) {
    if (family_ != AF_INET && family_ != AF_INET6) {
        isc_throw (BadValue, "PerfmonConfig: family must be AF_INET or AF_INET6");
    }

    alarm_store_.reset(new AlarmStore(family_));
}

void
PerfMonConfig::parse(data::ConstElementPtr config) {
    // Use a local instance to collect values.  This way we
    // avoid corrupting current values if there are any errors.
    PerfMonConfig local(family_);

    // Note checkKeywords() will throw DhcpConfigError if there is a problem.
    SimpleParser::checkKeywords(CONFIG_KEYWORDS, config);

    // Parse members.
    ConstElementPtr elem = config->get("enable-monitoring");
    if (elem) {
        local.setEnableMonitoring(elem->boolValue());
    }

    elem = config->get("interval-width-secs");
    if (elem) {
        int64_t value = elem->intValue();
        if (value <= 0) {
            isc_throw(DhcpConfigError, "invalid interval-width-secs: '"
                       << value << "', must be greater than 0");
        }

        local.setIntervalWidthSecs(value);
    }

    elem = config->get("stats-mgr-reporting");
    if (elem) {
        local.setStatsMgrReporting(elem->boolValue());
    }

    elem = config->get("alarm-report-secs");
    if (elem) {
        int64_t value = elem->intValue();
        if (value < 0) {
            isc_throw(DhcpConfigError, "invalid alarm-report-secs: '"
                       << value << "', cannot be less than 0");
        }

        local.setAlarmReportSecs(value);
    }

    elem = config->get("alarms");
    if (elem) {
        local.parseAlarms(elem);
    }

    // All values good, shallow copy from local instance.
    *this = local;
}

void
PerfMonConfig::parseAlarms(data::ConstElementPtr config) {
    alarm_store_.reset(new AlarmStore(family_));
    for (auto const& alarm_elem : config->listValue()) {
        try {
            AlarmPtr alarm = AlarmParser::parse(alarm_elem, family_);
            alarm_store_->addAlarm(alarm);
        } catch (const std::exception& ex) {
            isc_throw(DhcpConfigError, "cannot add Alarm to store: " << ex.what());
        }
    }
}

} // end of namespace perfmon
} // end of namespace isc