Kea: perfmon_callouts.cc Source File

| Kea 3.2.0-git

perfmon_callouts.cc
Go to the documentation of this file.
1// Copyright (C) 2024 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7// Functions accessed by the hooks framework use C linkage to avoid the name
8// mangling that accompanies use of the C++ compiler as well as to avoid
9// issues related to namespaces.
10
11#include <config.h>
12
13#include <perfmon_log.h>
14#include <perfmon_mgr.h>
15#include <cc/command_interpreter.h>
16#include <dhcpsrv/cfgmgr.h>
17#include <hooks/hooks.h>
18#include <process/daemon.h>
19
20namespace isc {
21namespace perfmon {
22
24 PerfMonMgrPtr mgr;
25
26} // end of namespace perfmon
27}
28
29using namespace isc::data;
30using namespace isc::dhcp;
31using namespace isc::hooks;
32using namespace isc::log;
33using namespace isc::process;
34using namespace isc::perfmon;
35
36extern "C" {
37
38 int dhcp4_srv_configured(CalloutHandle& /* handle */) {
39 // We do this here rather than in load() to ensure we check after the
40 // packet filter has been determined.
41 LOG_DEBUG(perfmon_logger, DBGLVL_TRACE_BASIC, PERFMON_DHCP4_SOCKET_RECEIVED_TIME_SUPPORT)
42 .arg(IfaceMgr::instance().isSocketReceivedTimeSupported() ? "Yes" : "No");
43 return (0);
44}
45
46 int dhcp6_srv_configured(CalloutHandle& /* handle */) {
47 // We do this here rather than in load() to ensure we check after the
48 // packet filter has been determined.
49 LOG_DEBUG(perfmon_logger, DBGLVL_TRACE_BASIC, PERFMON_DHCP6_SOCKET_RECEIVED_TIME_SUPPORT)
50 .arg(IfaceMgr::instance().isSocketReceivedTimeSupported() ? "Yes" : "No");
51 return (0);
52}
53
59 int pkt4_send(CalloutHandle& handle) {
60 CalloutHandle::CalloutNextStep status = handle.getStatus();
61 if (status == CalloutHandle::NEXT_STEP_DROP ||
62 status == CalloutHandle::NEXT_STEP_SKIP) {
63 return (0);
64 }
65
66 Pkt4Ptr query;
67 handle.getArgument("query4", query);
68
69 Pkt4Ptr response;
70 handle.getArgument("response4", response);
71
72 ConstSubnet4Ptr subnet;
73 handle.getArgument("subnet4", subnet);
74
75 try {
76 mgr->processPktEventStack(query, response, subnet);
77 } catch (const std::exception& ex) {
78 LOG_DEBUG(perfmon_logger, DBGLVL_TRACE_DETAIL, PERFMON_DHCP4_PKT_PROCESS_ERROR)
79 .arg(query->getLabel())
80 .arg(ex.what());
81 }
82
83 return (0);
84}
85
91 int pkt6_send(CalloutHandle& handle) {
92 CalloutHandle::CalloutNextStep status = handle.getStatus();
93 if (status == CalloutHandle::NEXT_STEP_DROP ||
94 status == CalloutHandle::NEXT_STEP_SKIP) {
95 return (0);
96 }
97
98 Pkt6Ptr query;
99 handle.getArgument("query6", query);
100
101 Pkt6Ptr response;
102 handle.getArgument("response6", response);
103
104 ConstSubnet6Ptr subnet;
105 handle.getArgument("subnet6", subnet);
106
107 try {
108 mgr->processPktEventStack(query, response, subnet);
109 } catch (const std::exception& ex) {
110 LOG_DEBUG(perfmon_logger, DBGLVL_TRACE_DETAIL, PERFMON_DHCP6_PKT_PROCESS_ERROR)
111 .arg(query->getLabel())
112 .arg(ex.what());
113 }
114
115 return (0);
116}
117
124 int perfmon_control(CalloutHandle& handle) {
125 return (mgr->perfmonControlHandler(handle));
126}
127
134 int perfmon_get_all_durations(CalloutHandle& handle) {
135 return (mgr->perfmonGetAllDurationsHandler(handle));
136}
137
142 int load(LibraryHandle& handle) {
143 try {
144 // Make the hook library only loadable for kea-dhcpX.
145 uint16_t family = CfgMgr::instance().getFamily();
146 const std::string& proc_name = Daemon::getProcName();
147 if (family == AF_INET) {
148 if (proc_name != "kea-dhcp4") {
149 isc_throw(isc::Unexpected, "Bad process name: " << proc_name
150 << ", expected kea-dhcp4");
151 }
152 } else if (proc_name != "kea-dhcp6") {
153 isc_throw(isc::Unexpected, "Bad process name: " << proc_name
154 << ", expected kea-dhcp6");
155 }
156
157 // Instantiate the manager singleton.
158 mgr.reset(new PerfMonMgr(family));
159
160 // Configure the manager using the hook library's parameters.
161 ConstElementPtr json = handle.getParameters();
162 mgr->configure(json);
163
165 handle.registerCommandCallout("perfmon-control", perfmon_control);
166 handle.registerCommandCallout("perfmon-get-all-durations", perfmon_get_all_durations);
167 } catch (const std::exception& ex) {
168 LOG_ERROR(perfmon_logger, PERFMON_INIT_FAILED)
169 .arg(ex.what());
170 return (1);
171 }
172
173 LOG_INFO(perfmon_logger, PERFMON_INIT_OK);
174 return (0);
175}
176
180 int unload() {
181 LOG_INFO(perfmon_logger, PERFMON_DEINIT_OK);
182 return (0);
183}
184
188 int multi_threading_compatible() {
189 return (1);
190}
191
192} // end extern "C"
cfgmgr.h
CalloutHandle::CalloutNextStep
CalloutNextStep
Specifies allowed next steps.
Definition callout_handle.h:86
CalloutHandle::NEXT_STEP_DROP
@ NEXT_STEP_DROP
drop the packet
Definition callout_handle.h:89
CalloutHandle::NEXT_STEP_SKIP
@ NEXT_STEP_SKIP
skip the next processing step
Definition callout_handle.h:88
isc::Unexpected
A generic exception that is thrown when an unexpected error condition occurs.
Definition exceptions/exceptions.h:153
isc::dhcp::CfgMgr::getFamily
uint16_t getFamily() const
Returns address family.
Definition cfgmgr.h:246
isc::dhcp::CfgMgr::instance
static CfgMgr & instance()
returns a single instance of Configuration Manager
Definition cfgmgr.cc:29
isc::dhcp::IfaceMgr::instance
static IfaceMgr & instance()
IfaceMgr is a singleton class.
Definition iface_mgr.cc:52
isc::hooks::CalloutHandle
Per-packet callout handle.
Definition callout_handle.h:78
isc::hooks::CalloutHandle::getStatus
CalloutNextStep getStatus() const
Returns the next processing step.
Definition callout_handle.h:241
isc::hooks::CalloutHandle::getArgument
void getArgument(const std::string &name, T &value) const
Get argument.
Definition callout_handle.h:164
isc::hooks::LibraryHandle
Library handle.
Definition library_handle.h:60
isc::hooks::LibraryHandle::registerCommandCallout
void registerCommandCallout(const std::string &command_name, CalloutPtr callout)
Register control command handler.
Definition library_handle.cc:36
isc::hooks::LibraryHandle::getParameters
isc::data::ConstElementPtr getParameters()
Get configuration parameter common code.
Definition library_handle.cc:74
isc::perfmon::PerfMonMgr
Singleton which provides overall configuration, control, and state of the PerfMon hook library.
Definition perfmon_mgr.h:29
isc::process::Daemon::getProcName
static std::string getProcName()
returns the process name This value is used as when forming the default PID file name
Definition daemon.cc:154
command_interpreter.h
This file contains several functions and constants that are used for handling commands and responses ...
daemon.h
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition exceptions/exceptions.h:210
hooks.h
LOG_ERROR
#define LOG_ERROR(LOGGER, MESSAGE)
Macro to conveniently test error output and log it.
Definition macros.h:32
LOG_INFO
#define LOG_INFO(LOGGER, MESSAGE)
Macro to conveniently test info output and log it.
Definition macros.h:20
LOG_DEBUG
#define LOG_DEBUG(LOGGER, LEVEL, MESSAGE)
Macro to conveniently test debug output and log it.
Definition macros.h:14
isc::data
Definition base_stamped_element.cc:12
isc::data::ConstElementPtr
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:30
isc::dhcp
Definition dhcp4/client_handler.cc:20
isc::dhcp::ConstSubnet6Ptr
boost::shared_ptr< const Subnet6 > ConstSubnet6Ptr
A const pointer to a Subnet6 object.
Definition subnet.h:620
isc::dhcp::ConstSubnet4Ptr
boost::shared_ptr< const Subnet4 > ConstSubnet4Ptr
A const pointer to a Subnet4 object.
Definition subnet.h:455
isc::dhcp::Pkt4Ptr
boost::shared_ptr< Pkt4 > Pkt4Ptr
A pointer to Pkt4 object.
Definition pkt4.h:556
isc::dhcp::Pkt6Ptr
boost::shared_ptr< Pkt6 > Pkt6Ptr
A pointer to Pkt6 packet.
Definition pkt6.h:31
isc::hooks
Definition callout_handle.cc:21
isc::log
Definition buffer_appender_impl.cc:17
isc::log::DBGLVL_TRACE_BASIC
const int DBGLVL_TRACE_BASIC
Trace basic operations.
Definition log_dbglevels.h:69
isc::log::DBGLVL_TRACE_DETAIL
const int DBGLVL_TRACE_DETAIL
Trace detailed operations.
Definition log_dbglevels.h:75
isc::perfmon
Definition alarm.cc:19
isc::perfmon::perfmon_logger
isc::log::Logger perfmon_logger("perfmon-hooks")
Definition perfmon_log.h:17
isc::perfmon::mgr
PerfMonMgrPtr mgr
PerfMonMgr singleton.
Definition perfmon_callouts.cc:24
isc::perfmon::PerfMonMgrPtr
boost::shared_ptr< PerfMonMgr > PerfMonMgrPtr
Defines a shared pointer to a PerfMonMgr.
Definition perfmon_mgr.h:314
isc::process
Definition cb_ctl_base.h:22
isc
Defines the logger used by the top-level component of kea-lfc.
Definition check_exists_add.cc:18
dhcp6_srv_configured
int dhcp6_srv_configured(CalloutHandle &)
Definition perfmon_callouts.cc:46
dhcp4_srv_configured
int dhcp4_srv_configured(CalloutHandle &)
Definition perfmon_callouts.cc:38
perfmon_control
int perfmon_control(CalloutHandle &handle)
This is a command callout for 'perfmon-control' command.
Definition perfmon_callouts.cc:124
multi_threading_compatible
int multi_threading_compatible()
This function is called to retrieve the multi-threading compatibility.
Definition perfmon_callouts.cc:188
pkt6_send
int pkt6_send(CalloutHandle &handle)
This callout is called at the "pkt6_send" hook.
Definition perfmon_callouts.cc:91
pkt4_send
int pkt4_send(CalloutHandle &handle)
This callout is called at the "pkt4_send" hook.
Definition perfmon_callouts.cc:59
unload
int unload()
This function is called when the library is unloaded.
Definition perfmon_callouts.cc:180
load
int load(LibraryHandle &handle)
This function is called when the library is loaded.
Definition perfmon_callouts.cc:142
perfmon_get_all_durations
int perfmon_get_all_durations(CalloutHandle &handle)
This is a command callout for 'perfmon-get-all-durations' command.
Definition perfmon_callouts.cc:134
perfmon_log.h
PERFMON_INIT_OK
const isc::log::MessageID PERFMON_INIT_OK
PERFMON_DHCP6_PKT_PROCESS_ERROR
const isc::log::MessageID PERFMON_DHCP6_PKT_PROCESS_ERROR
PERFMON_INIT_FAILED
const isc::log::MessageID PERFMON_INIT_FAILED
PERFMON_DHCP4_PKT_PROCESS_ERROR
const isc::log::MessageID PERFMON_DHCP4_PKT_PROCESS_ERROR
PERFMON_DEINIT_OK
const isc::log::MessageID PERFMON_DEINIT_OK
PERFMON_DHCP6_SOCKET_RECEIVED_TIME_SUPPORT
const isc::log::MessageID PERFMON_DHCP6_SOCKET_RECEIVED_TIME_SUPPORT
PERFMON_DHCP4_SOCKET_RECEIVED_TIME_SUPPORT
const isc::log::MessageID PERFMON_DHCP4_SOCKET_RECEIVED_TIME_SUPPORT
perfmon_mgr.h