From 0ba7897b0793fb545baf1a459aa829b45a0183a1 Mon Sep 17 00:00:00 2001 From: Francis Dupont Date: Wed, 12 Dec 2018 16:39:48 +0100 Subject: [PATCH] [283-perfdhcp-sending-thread] [343-put-socket-control-buffer-in-the-stack] Moved socket control buffers to stack --- src/lib/dhcp/pkt_filter_inet.cc | 24 ++++++++++-------------- src/lib/dhcp/pkt_filter_inet.h | 17 ++--------------- src/lib/dhcp/pkt_filter_inet6.cc | 22 ++++++++++------------ src/lib/dhcp/pkt_filter_inet6.h | 18 ++---------------- 4 files changed, 24 insertions(+), 57 deletions(-) diff --git a/src/lib/dhcp/pkt_filter_inet.cc b/src/lib/dhcp/pkt_filter_inet.cc index 9953e8780c..7245065e34 100644 --- a/src/lib/dhcp/pkt_filter_inet.cc +++ b/src/lib/dhcp/pkt_filter_inet.cc @@ -17,13 +17,8 @@ using namespace isc::asiolink; namespace isc { namespace dhcp { -PktFilterInet::PktFilterInet() - : recv_control_buf_len_(CMSG_SPACE(sizeof(struct in6_pktinfo))), - send_control_buf_len_(CMSG_SPACE(sizeof(struct in6_pktinfo))), - recv_control_buf_(new char[recv_control_buf_len_]), - send_control_buf_(new char[send_control_buf_len_]) -{ -} +const size_t +PktFilterInet::CONTROL_BUF_LEN = CMSG_SPACE(sizeof(struct in6_pktinfo)); SocketInfo PktFilterInet::openSocket(Iface& iface, @@ -31,7 +26,6 @@ PktFilterInet::openSocket(Iface& iface, const uint16_t port, const bool receive_bcast, const bool send_bcast) { - struct sockaddr_in addr4; memset(&addr4, 0, sizeof(sockaddr)); addr4.sin_family = AF_INET; @@ -113,8 +107,9 @@ Pkt4Ptr PktFilterInet::receive(Iface& iface, const SocketInfo& socket_info) { struct sockaddr_in from_addr; uint8_t buf[IfaceMgr::RCVBUFSIZE]; + uint8_t control_buf[CONTROL_BUF_LEN]; - memset(&recv_control_buf_[0], 0, recv_control_buf_len_); + memset(&control_buf[0], 0, CONTROL_BUF_LEN); memset(&from_addr, 0, sizeof(from_addr)); // Initialize our message header structure. @@ -137,8 +132,8 @@ PktFilterInet::receive(Iface& iface, const SocketInfo& socket_info) { // previously asked the kernel to give us packet // information (when we initialized the interface), so we // should get the destination address from that. - m.msg_control = &recv_control_buf_[0]; - m.msg_controllen = recv_control_buf_len_; + m.msg_control = &control_buf[0]; + m.msg_controllen = CONTROL_BUF_LEN; int result = recvmsg(socket_info.sockfd_, &m, 0); if (result < 0) { @@ -213,7 +208,8 @@ PktFilterInet::receive(Iface& iface, const SocketInfo& socket_info) { int PktFilterInet::send(const Iface&, uint16_t sockfd, const Pkt4Ptr& pkt) { - memset(&send_control_buf_[0], 0, send_control_buf_len_); + uint8_t control_buf[CONTROL_BUF_LEN]; + memset(&control_buf[0], 0, CONTROL_BUF_LEN); // Set the target address we're sending to. sockaddr_in to; @@ -249,8 +245,8 @@ PktFilterInet::send(const Iface&, uint16_t sockfd, // We have to create a "control message", and set that to // define the IPv4 packet information. We set the source address // to handle correctly interfaces with multiple addresses. - m.msg_control = &send_control_buf_[0]; - m.msg_controllen = send_control_buf_len_; + m.msg_control = &control_buf[0]; + m.msg_controllen = CONTROL_BUF_LEN; struct cmsghdr* cmsg = CMSG_FIRSTHDR(&m); cmsg->cmsg_level = IPPROTO_IP; cmsg->cmsg_type = IP_PKTINFO; diff --git a/src/lib/dhcp/pkt_filter_inet.h b/src/lib/dhcp/pkt_filter_inet.h index b1b6f93096..8b6b02c47c 100644 --- a/src/lib/dhcp/pkt_filter_inet.h +++ b/src/lib/dhcp/pkt_filter_inet.h @@ -20,11 +20,6 @@ namespace dhcp { class PktFilterInet : public PktFilter { public: - /// @brief Constructor - /// - /// Allocates control buffers. - PktFilterInet(); - /// @brief Check if packet can be sent to the host without address directly. /// /// This Packet Filter sends packets through AF_INET datagram sockets, so @@ -85,16 +80,8 @@ public: const Pkt4Ptr& pkt); private: - /// There are separate control buffers for sending and receiving to be able - /// to send and receive packets in parallel in two threads. - /// Length of the recv_control_buf_ array. - size_t recv_control_buf_len_; - /// Length of the send_control_buf_ array. - size_t send_control_buf_len_; - /// Control buffer, used in reception. - boost::scoped_array recv_control_buf_; - /// Control buffer, used in transmission. - boost::scoped_array send_control_buf_; + /// Length of the socket control buffer. + static const size_t CONTROL_BUF_LEN; }; } // namespace isc::dhcp diff --git a/src/lib/dhcp/pkt_filter_inet6.cc b/src/lib/dhcp/pkt_filter_inet6.cc index eede050a71..7e7a37c210 100644 --- a/src/lib/dhcp/pkt_filter_inet6.cc +++ b/src/lib/dhcp/pkt_filter_inet6.cc @@ -19,12 +19,8 @@ using namespace isc::asiolink; namespace isc { namespace dhcp { -PktFilterInet6::PktFilterInet6() -: recv_control_buf_len_(CMSG_SPACE(sizeof(struct in6_pktinfo))), - send_control_buf_len_(CMSG_SPACE(sizeof(struct in6_pktinfo))), - recv_control_buf_(new char[recv_control_buf_len_]), - send_control_buf_(new char[send_control_buf_len_]) { -} +const size_t +PktFilterInet6::CONTROL_BUF_LEN = CMSG_SPACE(sizeof(struct in6_pktinfo)); SocketInfo PktFilterInet6::openSocket(const Iface& iface, @@ -137,7 +133,8 @@ Pkt6Ptr PktFilterInet6::receive(const SocketInfo& socket_info) { // Now we have a socket, let's get some data from it! uint8_t buf[IfaceMgr::RCVBUFSIZE]; - memset(&recv_control_buf_[0], 0, recv_control_buf_len_); + uint8_t control_buf[CONTROL_BUF_LEN]; + memset(&control_buf[0], 0, CONTROL_BUF_LEN); struct sockaddr_in6 from; memset(&from, 0, sizeof(from)); @@ -165,8 +162,8 @@ PktFilterInet6::receive(const SocketInfo& socket_info) { // previously asked the kernel to give us packet // information (when we initialized the interface), so we // should get the destination address from that. - m.msg_control = &recv_control_buf_[0]; - m.msg_controllen = recv_control_buf_len_; + m.msg_control = &control_buf[0]; + m.msg_controllen = CONTROL_BUF_LEN; int result = recvmsg(socket_info.sockfd_, &m, 0); @@ -247,7 +244,8 @@ PktFilterInet6::receive(const SocketInfo& socket_info) { int PktFilterInet6::send(const Iface&, uint16_t sockfd, const Pkt6Ptr& pkt) { - memset(&send_control_buf_[0], 0, send_control_buf_len_); + uint8_t control_buf[CONTROL_BUF_LEN]; + memset(&control_buf[0], 0, CONTROL_BUF_LEN); // Set the target address we're sending to. sockaddr_in6 to; @@ -289,8 +287,8 @@ PktFilterInet6::send(const Iface&, uint16_t sockfd, const Pkt6Ptr& pkt) { // define the IPv6 packet information. We could set the // source address if we wanted, but we can safely let the // kernel decide what that should be. - m.msg_control = &send_control_buf_[0]; - m.msg_controllen = send_control_buf_len_; + m.msg_control = &control_buf[0]; + m.msg_controllen = CONTROL_BUF_LEN; struct cmsghdr *cmsg = CMSG_FIRSTHDR(&m); // FIXME: Code below assumes that cmsg is not NULL, but diff --git a/src/lib/dhcp/pkt_filter_inet6.h b/src/lib/dhcp/pkt_filter_inet6.h index 39a059d73d..d641065bdf 100644 --- a/src/lib/dhcp/pkt_filter_inet6.h +++ b/src/lib/dhcp/pkt_filter_inet6.h @@ -21,12 +21,6 @@ namespace dhcp { class PktFilterInet6 : public PktFilter6 { public: - /// @brief Constructor. - /// - /// Initializes control buffers used in message transmission and - /// reception. - PktFilterInet6(); - /// @brief Opens a socket. /// /// This function opens an IPv6 socket on an interface and binds it to a @@ -85,16 +79,8 @@ public: const Pkt6Ptr& pkt); private: - /// There are separate control buffers for sending and receiving to be able - /// to send and receive packets in parallel in two threads. - /// Length of the recv_control_buf_ array. - size_t recv_control_buf_len_; - /// Length of the send_control_buf_ array. - size_t send_control_buf_len_; - /// Control buffer, used in reception. - boost::scoped_array recv_control_buf_; - /// Control buffer, used in transmission. - boost::scoped_array send_control_buf_; + /// Length of the socket control buffer. + static const size_t CONTROL_BUF_LEN; }; } // namespace isc::dhcp -- 2.47.2